1 // SPDX-License-Identifier: GPL-2.0-only
3 * mm/balloon_compaction.c
5 * Common interface for making balloon pages movable by compaction.
7 * Copyright (C) 2012, Red Hat, Inc. Rafael Aquini <aquini@redhat.com>
10 #include <linux/slab.h>
11 #include <linux/export.h>
12 #include <linux/balloon_compaction.h>
14 static void balloon_page_enqueue_one(struct balloon_dev_info *b_dev_info,
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.
23 BUG_ON(!trylock_page(page));
24 balloon_page_insert(b_dev_info, page);
26 __count_vm_event(BALLOON_INFLATE);
30 * balloon_page_list_enqueue() - inserts a list of pages into the balloon page
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.
35 * Driver must call this function to properly enqueue balloon pages before
36 * definitively removing them from the guest system.
38 * Return: number of pages that were enqueued.
40 size_t balloon_page_list_enqueue(struct balloon_dev_info *b_dev_info,
41 struct list_head *pages)
43 struct page *page, *tmp;
47 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
48 list_for_each_entry_safe(page, tmp, pages, lru) {
50 balloon_page_enqueue_one(b_dev_info, page);
53 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
56 EXPORT_SYMBOL_GPL(balloon_page_list_enqueue);
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.
65 * Driver must call this function to properly de-allocate a previous enlisted
66 * balloon pages before definitively 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.
70 * Note that this function may fail to dequeue some pages even if the balloon
71 * isn't empty - since the page list can be temporarily empty due to compaction
74 * Return: number of pages that were added to the @pages list.
76 size_t balloon_page_list_dequeue(struct balloon_dev_info *b_dev_info,
77 struct list_head *pages, size_t n_req_pages)
79 struct page *page, *tmp;
83 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
84 list_for_each_entry_safe(page, tmp, &b_dev_info->pages, lru) {
85 if (n_pages == n_req_pages)
89 * Block others from accessing the 'page' while we get around to
90 * establishing additional references and preparing the 'page'
91 * to be released by the balloon driver.
93 if (!trylock_page(page))
96 if (IS_ENABLED(CONFIG_BALLOON_COMPACTION) &&
98 /* raced with isolation */
102 balloon_page_delete(page);
103 __count_vm_event(BALLOON_DEFLATE);
104 list_add(&page->lru, pages);
108 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
112 EXPORT_SYMBOL_GPL(balloon_page_list_dequeue);
115 * balloon_page_alloc - allocates a new page for insertion into the balloon
118 * Driver must call this function to properly allocate a new balloon page.
119 * Driver must call balloon_page_enqueue before definitively removing the page
120 * from the guest system.
122 * Return: struct page for the allocated page or NULL on allocation failure.
124 struct page *balloon_page_alloc(void)
126 struct page *page = alloc_page(balloon_mapping_gfp_mask() |
127 __GFP_NOMEMALLOC | __GFP_NORETRY);
130 EXPORT_SYMBOL_GPL(balloon_page_alloc);
133 * balloon_page_enqueue - inserts a new page into the balloon page list.
135 * @b_dev_info: balloon device descriptor where we will insert a new page
136 * @page: new page to enqueue - allocated using balloon_page_alloc.
138 * Drivers must call this function to properly enqueue a new allocated balloon
139 * page before definitively removing the page from the guest system.
141 * Drivers must not call balloon_page_enqueue on pages that have been pushed to
142 * a list with balloon_page_push before removing them with balloon_page_pop. To
143 * enqueue a list of pages, use balloon_page_list_enqueue instead.
145 void balloon_page_enqueue(struct balloon_dev_info *b_dev_info,
150 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
151 balloon_page_enqueue_one(b_dev_info, page);
152 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
154 EXPORT_SYMBOL_GPL(balloon_page_enqueue);
157 * balloon_page_dequeue - removes a page from balloon's page list and returns
158 * its address to allow the driver to release the page.
159 * @b_dev_info: balloon device decriptor where we will grab a page from.
161 * Driver must call this function to properly dequeue a previously enqueued page
162 * before definitively releasing it back to the guest system.
164 * Caller must perform its own accounting to ensure that this
165 * function is called only if some pages are actually enqueued.
167 * Note that this function may fail to dequeue some pages even if there are
168 * some enqueued pages - since the page list can be temporarily empty due to
169 * the compaction of isolated pages.
171 * TODO: remove the caller accounting requirements, and allow caller to wait
172 * until all pages can be dequeued.
174 * Return: struct page for the dequeued page, or NULL if no page was dequeued.
176 struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info)
182 n_pages = balloon_page_list_dequeue(b_dev_info, &pages, 1);
186 * If we are unable to dequeue a balloon page because the page
187 * list is empty and there are no isolated pages, then something
188 * went out of track and some balloon pages are lost.
189 * BUG() here, otherwise the balloon driver may get stuck in
190 * an infinite loop while attempting to release all its pages.
192 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
193 if (unlikely(list_empty(&b_dev_info->pages) &&
194 !b_dev_info->isolated_pages))
196 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
199 return list_first_entry(&pages, struct page, lru);
201 EXPORT_SYMBOL_GPL(balloon_page_dequeue);
203 #ifdef CONFIG_BALLOON_COMPACTION
205 bool balloon_page_isolate(struct page *page, isolate_mode_t mode)
208 struct balloon_dev_info *b_dev_info = balloon_page_device(page);
211 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
212 list_del(&page->lru);
213 b_dev_info->isolated_pages++;
214 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
219 void balloon_page_putback(struct page *page)
221 struct balloon_dev_info *b_dev_info = balloon_page_device(page);
224 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
225 list_add(&page->lru, &b_dev_info->pages);
226 b_dev_info->isolated_pages--;
227 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
231 /* move_to_new_page() counterpart for a ballooned page */
232 int balloon_page_migrate(struct address_space *mapping,
233 struct page *newpage, struct page *page,
234 enum migrate_mode mode)
236 struct balloon_dev_info *balloon = balloon_page_device(page);
239 * We can not easily support the no copy case here so ignore it as it
240 * is unlikely to be used with balloon pages. See include/linux/hmm.h
241 * for a user of the MIGRATE_SYNC_NO_COPY mode.
243 if (mode == MIGRATE_SYNC_NO_COPY)
246 VM_BUG_ON_PAGE(!PageLocked(page), page);
247 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
249 return balloon->migratepage(balloon, newpage, page, mode);
252 const struct address_space_operations balloon_aops = {
253 .migratepage = balloon_page_migrate,
254 .isolate_page = balloon_page_isolate,
255 .putback_page = balloon_page_putback,
257 EXPORT_SYMBOL_GPL(balloon_aops);
259 #endif /* CONFIG_BALLOON_COMPACTION */