blob: d25664e1857bd118189408fea9518ab0d318f7cd [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Rafael Aquini18468d92012-12-11 16:02:38 -08002/*
3 * mm/balloon_compaction.c
4 *
5 * Common interface for making balloon pages movable by compaction.
6 *
7 * Copyright (C) 2012, Red Hat, Inc. Rafael Aquini <aquini@redhat.com>
8 */
9#include <linux/mm.h>
10#include <linux/slab.h>
11#include <linux/export.h>
12#include <linux/balloon_compaction.h>
13
Nadav Amit418a3ab2019-04-25 04:54:42 -070014static void balloon_page_enqueue_one(struct balloon_dev_info *b_dev_info,
15 struct page *page)
16{
17 /*
18 * Block others from accessing the 'page' when we get around to
19 * establishing additional references. We should be the only one
20 * holding a reference to the 'page' at this point. If we are not, then
21 * memory corruption is possible and we should stop execution.
22 */
23 BUG_ON(!trylock_page(page));
Nadav Amit418a3ab2019-04-25 04:54:42 -070024 balloon_page_insert(b_dev_info, page);
25 unlock_page(page);
26 __count_vm_event(BALLOON_INFLATE);
27}
28
29/**
30 * balloon_page_list_enqueue() - inserts a list of pages into the balloon page
31 * list.
32 * @b_dev_info: balloon device descriptor where we will insert a new page to
33 * @pages: pages to enqueue - allocated using balloon_page_alloc.
34 *
35 * Driver must call it to properly enqueue a balloon pages before definitively
36 * removing it from the guest system.
37 *
38 * Return: number of pages that were enqueued.
39 */
40size_t balloon_page_list_enqueue(struct balloon_dev_info *b_dev_info,
41 struct list_head *pages)
42{
43 struct page *page, *tmp;
44 unsigned long flags;
45 size_t n_pages = 0;
46
47 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
48 list_for_each_entry_safe(page, tmp, pages, lru) {
Wei Wangdd422902019-07-18 17:27:20 +080049 list_del(&page->lru);
Nadav Amit418a3ab2019-04-25 04:54:42 -070050 balloon_page_enqueue_one(b_dev_info, page);
51 n_pages++;
52 }
53 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
54 return n_pages;
55}
56EXPORT_SYMBOL_GPL(balloon_page_list_enqueue);
57
58/**
59 * balloon_page_list_dequeue() - removes pages from balloon's page list and
60 * returns a list of the pages.
61 * @b_dev_info: balloon device decriptor where we will grab a page from.
62 * @pages: pointer to the list of pages that would be returned to the caller.
63 * @n_req_pages: number of requested pages.
64 *
65 * Driver must call this function to properly de-allocate a previous enlisted
66 * balloon pages before definetively releasing it back to the guest system.
67 * This function tries to remove @n_req_pages from the ballooned pages and
68 * return them to the caller in the @pages list.
69 *
70 * Note that this function may fail to dequeue some pages temporarily empty due
71 * to compaction isolated pages.
72 *
73 * Return: number of pages that were added to the @pages list.
74 */
75size_t balloon_page_list_dequeue(struct balloon_dev_info *b_dev_info,
76 struct list_head *pages, size_t n_req_pages)
77{
78 struct page *page, *tmp;
79 unsigned long flags;
80 size_t n_pages = 0;
81
82 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
83 list_for_each_entry_safe(page, tmp, &b_dev_info->pages, lru) {
84 if (n_pages == n_req_pages)
85 break;
86
87 /*
88 * Block others from accessing the 'page' while we get around to
89 * establishing additional references and preparing the 'page'
90 * to be released by the balloon driver.
91 */
92 if (!trylock_page(page))
93 continue;
94
95 if (IS_ENABLED(CONFIG_BALLOON_COMPACTION) &&
96 PageIsolated(page)) {
97 /* raced with isolation */
98 unlock_page(page);
99 continue;
100 }
101 balloon_page_delete(page);
102 __count_vm_event(BALLOON_DEFLATE);
103 list_add(&page->lru, pages);
104 unlock_page(page);
105 n_pages++;
106 }
107 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
108
109 return n_pages;
110}
111EXPORT_SYMBOL_GPL(balloon_page_list_dequeue);
112
Rafael Aquini18468d92012-12-11 16:02:38 -0800113/*
Michael S. Tsirkinc7cdff02017-10-13 16:11:48 +0300114 * balloon_page_alloc - allocates a new page for insertion into the balloon
115 * page list.
116 *
117 * Driver must call it to properly allocate a new enlisted balloon page.
118 * Driver must call balloon_page_enqueue before definitively removing it from
119 * the guest system. This function returns the page address for the recently
120 * allocated page or NULL in the case we fail to allocate a new page this turn.
121 */
122struct page *balloon_page_alloc(void)
123{
124 struct page *page = alloc_page(balloon_mapping_gfp_mask() |
125 __GFP_NOMEMALLOC | __GFP_NORETRY);
126 return page;
127}
128EXPORT_SYMBOL_GPL(balloon_page_alloc);
129
130/*
Wei Wangdd422902019-07-18 17:27:20 +0800131 * balloon_page_enqueue - inserts a new page into the balloon page list.
132 *
Bogdan Sikorabdb428c2015-12-27 14:58:23 +0100133 * @b_dev_info: balloon device descriptor where we will insert a new page to
Michael S. Tsirkinc7cdff02017-10-13 16:11:48 +0300134 * @page: new page to enqueue - allocated using balloon_page_alloc.
Rafael Aquini18468d92012-12-11 16:02:38 -0800135 *
Michael S. Tsirkinc7cdff02017-10-13 16:11:48 +0300136 * Driver must call it to properly enqueue a new allocated balloon page
Bogdan Sikorabdb428c2015-12-27 14:58:23 +0100137 * before definitively removing it from the guest system.
Wei Wangdd422902019-07-18 17:27:20 +0800138 *
139 * Drivers must not call balloon_page_enqueue on pages that have been
140 * pushed to a list with balloon_page_push before removing them with
141 * balloon_page_pop. To all pages on a list, use balloon_page_list_enqueue
142 * instead.
143 *
Rafael Aquini18468d92012-12-11 16:02:38 -0800144 * This function returns the page address for the recently enqueued page or
145 * NULL in the case we fail to allocate a new page this turn.
146 */
Michael S. Tsirkinc7cdff02017-10-13 16:11:48 +0300147void balloon_page_enqueue(struct balloon_dev_info *b_dev_info,
148 struct page *page)
Rafael Aquini18468d92012-12-11 16:02:38 -0800149{
150 unsigned long flags;
Rafael Aquini18468d92012-12-11 16:02:38 -0800151
Rafael Aquini18468d92012-12-11 16:02:38 -0800152 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
Nadav Amit418a3ab2019-04-25 04:54:42 -0700153 balloon_page_enqueue_one(b_dev_info, page);
Rafael Aquini18468d92012-12-11 16:02:38 -0800154 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
Rafael Aquini18468d92012-12-11 16:02:38 -0800155}
156EXPORT_SYMBOL_GPL(balloon_page_enqueue);
157
158/*
159 * balloon_page_dequeue - removes a page from balloon's page list and returns
160 * the its address to allow the driver release the page.
161 * @b_dev_info: balloon device decriptor where we will grab a page from.
162 *
163 * Driver must call it to properly de-allocate a previous enlisted balloon page
164 * before definetively releasing it back to the guest system.
165 * This function returns the page address for the recently dequeued page or
166 * NULL in the case we find balloon's page list temporarily empty due to
167 * compaction isolated pages.
168 */
169struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info)
170{
Rafael Aquini18468d92012-12-11 16:02:38 -0800171 unsigned long flags;
Nadav Amit418a3ab2019-04-25 04:54:42 -0700172 LIST_HEAD(pages);
173 int n_pages;
Rafael Aquini18468d92012-12-11 16:02:38 -0800174
Nadav Amit418a3ab2019-04-25 04:54:42 -0700175 n_pages = balloon_page_list_dequeue(b_dev_info, &pages, 1);
Rafael Aquini18468d92012-12-11 16:02:38 -0800176
Nadav Amit418a3ab2019-04-25 04:54:42 -0700177 if (n_pages != 1) {
Rafael Aquini18468d92012-12-11 16:02:38 -0800178 /*
179 * If we are unable to dequeue a balloon page because the page
180 * list is empty and there is no isolated pages, then something
181 * went out of track and some balloon pages are lost.
182 * BUG() here, otherwise the balloon driver may get stuck into
183 * an infinite loop while attempting to release all its pages.
184 */
185 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
186 if (unlikely(list_empty(&b_dev_info->pages) &&
187 !b_dev_info->isolated_pages))
188 BUG();
189 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
Nadav Amit418a3ab2019-04-25 04:54:42 -0700190 return NULL;
Rafael Aquini18468d92012-12-11 16:02:38 -0800191 }
Nadav Amit418a3ab2019-04-25 04:54:42 -0700192 return list_first_entry(&pages, struct page, lru);
Rafael Aquini18468d92012-12-11 16:02:38 -0800193}
194EXPORT_SYMBOL_GPL(balloon_page_dequeue);
195
196#ifdef CONFIG_BALLOON_COMPACTION
Rafael Aquini18468d92012-12-11 16:02:38 -0800197
Minchan Kimb1123ea62016-07-26 15:23:09 -0700198bool balloon_page_isolate(struct page *page, isolate_mode_t mode)
199
Rafael Aquini18468d92012-12-11 16:02:38 -0800200{
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700201 struct balloon_dev_info *b_dev_info = balloon_page_device(page);
Rafael Aquini18468d92012-12-11 16:02:38 -0800202 unsigned long flags;
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700203
Rafael Aquini18468d92012-12-11 16:02:38 -0800204 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
205 list_del(&page->lru);
206 b_dev_info->isolated_pages++;
207 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
Minchan Kimb1123ea62016-07-26 15:23:09 -0700208
209 return true;
Rafael Aquini18468d92012-12-11 16:02:38 -0800210}
211
Minchan Kimb1123ea62016-07-26 15:23:09 -0700212void balloon_page_putback(struct page *page)
Rafael Aquini18468d92012-12-11 16:02:38 -0800213{
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700214 struct balloon_dev_info *b_dev_info = balloon_page_device(page);
Rafael Aquini18468d92012-12-11 16:02:38 -0800215 unsigned long flags;
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700216
Rafael Aquini18468d92012-12-11 16:02:38 -0800217 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
218 list_add(&page->lru, &b_dev_info->pages);
219 b_dev_info->isolated_pages--;
220 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
221}
222
Rafael Aquini18468d92012-12-11 16:02:38 -0800223
224/* move_to_new_page() counterpart for a ballooned page */
Minchan Kimb1123ea62016-07-26 15:23:09 -0700225int balloon_page_migrate(struct address_space *mapping,
226 struct page *newpage, struct page *page,
227 enum migrate_mode mode)
Rafael Aquini18468d92012-12-11 16:02:38 -0800228{
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700229 struct balloon_dev_info *balloon = balloon_page_device(page);
Rafael Aquini18468d92012-12-11 16:02:38 -0800230
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700231 /*
232 * We can not easily support the no copy case here so ignore it as it
233 * is unlikely to be use with ballon pages. See include/linux/hmm.h for
234 * user of the MIGRATE_SYNC_NO_COPY mode.
235 */
236 if (mode == MIGRATE_SYNC_NO_COPY)
237 return -EINVAL;
238
Hugh Dickins7db76712015-11-05 18:49:49 -0800239 VM_BUG_ON_PAGE(!PageLocked(page), page);
240 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
Rafael Aquini18468d92012-12-11 16:02:38 -0800241
Minchan Kimb1123ea62016-07-26 15:23:09 -0700242 return balloon->migratepage(balloon, newpage, page, mode);
Rafael Aquini18468d92012-12-11 16:02:38 -0800243}
Minchan Kimb1123ea62016-07-26 15:23:09 -0700244
245const struct address_space_operations balloon_aops = {
246 .migratepage = balloon_page_migrate,
247 .isolate_page = balloon_page_isolate,
248 .putback_page = balloon_page_putback,
249};
250EXPORT_SYMBOL_GPL(balloon_aops);
251
Rafael Aquini18468d92012-12-11 16:02:38 -0800252#endif /* CONFIG_BALLOON_COMPACTION */