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));
25 balloon_page_insert(b_dev_info, page);
27 __count_vm_event(BALLOON_INFLATE);
31 * balloon_page_list_enqueue() - inserts a list of pages into the balloon page
33 * @b_dev_info: balloon device descriptor where we will insert a new page to
34 * @pages: pages to enqueue - allocated using balloon_page_alloc.
36 * Driver must call it to properly enqueue a balloon pages before definitively
37 * removing it from the guest system.
39 * Return: number of pages that were enqueued.
41 size_t balloon_page_list_enqueue(struct balloon_dev_info *b_dev_info,
42 struct list_head *pages)
44 struct page *page, *tmp;
48 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
49 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 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.
70 * Note that this function may fail to dequeue some pages temporarily empty due
71 * to compaction isolated pages.
73 * Return: number of pages that were added to the @pages list.
75 size_t balloon_page_list_dequeue(struct balloon_dev_info *b_dev_info,
76 struct list_head *pages, size_t n_req_pages)
78 struct page *page, *tmp;
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)
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.
92 if (!trylock_page(page))
95 if (IS_ENABLED(CONFIG_BALLOON_COMPACTION) &&
97 /* raced with isolation */
101 balloon_page_delete(page);
102 __count_vm_event(BALLOON_DEFLATE);
103 list_add(&page->lru, pages);
107 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
111 EXPORT_SYMBOL_GPL(balloon_page_list_dequeue);
114 * balloon_page_alloc - allocates a new page for insertion into the balloon
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.
122 struct page *balloon_page_alloc(void)
124 struct page *page = alloc_page(balloon_mapping_gfp_mask() |
125 __GFP_NOMEMALLOC | __GFP_NORETRY);
128 EXPORT_SYMBOL_GPL(balloon_page_alloc);
131 * balloon_page_enqueue - allocates a new page and inserts it into the balloon
133 * @b_dev_info: balloon device descriptor where we will insert a new page to
134 * @page: new page to enqueue - allocated using balloon_page_alloc.
136 * Driver must call it to properly enqueue a new allocated balloon page
137 * before definitively removing it from the guest system.
138 * This function returns the page address for the recently enqueued page or
139 * NULL in the case we fail to allocate a new page this turn.
141 void balloon_page_enqueue(struct balloon_dev_info *b_dev_info,
146 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
147 balloon_page_enqueue_one(b_dev_info, page);
148 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
150 EXPORT_SYMBOL_GPL(balloon_page_enqueue);
153 * balloon_page_dequeue - removes a page from balloon's page list and returns
154 * the its address to allow the driver release the page.
155 * @b_dev_info: balloon device decriptor where we will grab a page from.
157 * Driver must call it to properly de-allocate a previous enlisted balloon page
158 * before definetively releasing it back to the guest system.
159 * This function returns the page address for the recently dequeued page or
160 * NULL in the case we find balloon's page list temporarily empty due to
161 * compaction isolated pages.
163 struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info)
169 n_pages = balloon_page_list_dequeue(b_dev_info, &pages, 1);
173 * If we are unable to dequeue a balloon page because the page
174 * list is empty and there is no isolated pages, then something
175 * went out of track and some balloon pages are lost.
176 * BUG() here, otherwise the balloon driver may get stuck into
177 * an infinite loop while attempting to release all its pages.
179 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
180 if (unlikely(list_empty(&b_dev_info->pages) &&
181 !b_dev_info->isolated_pages))
183 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
186 return list_first_entry(&pages, struct page, lru);
188 EXPORT_SYMBOL_GPL(balloon_page_dequeue);
190 #ifdef CONFIG_BALLOON_COMPACTION
192 bool balloon_page_isolate(struct page *page, isolate_mode_t mode)
195 struct balloon_dev_info *b_dev_info = balloon_page_device(page);
198 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
199 list_del(&page->lru);
200 b_dev_info->isolated_pages++;
201 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
206 void balloon_page_putback(struct page *page)
208 struct balloon_dev_info *b_dev_info = balloon_page_device(page);
211 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
212 list_add(&page->lru, &b_dev_info->pages);
213 b_dev_info->isolated_pages--;
214 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
218 /* move_to_new_page() counterpart for a ballooned page */
219 int balloon_page_migrate(struct address_space *mapping,
220 struct page *newpage, struct page *page,
221 enum migrate_mode mode)
223 struct balloon_dev_info *balloon = balloon_page_device(page);
226 * We can not easily support the no copy case here so ignore it as it
227 * is unlikely to be use with ballon pages. See include/linux/hmm.h for
228 * user of the MIGRATE_SYNC_NO_COPY mode.
230 if (mode == MIGRATE_SYNC_NO_COPY)
233 VM_BUG_ON_PAGE(!PageLocked(page), page);
234 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
236 return balloon->migratepage(balloon, newpage, page, mode);
239 const struct address_space_operations balloon_aops = {
240 .migratepage = balloon_page_migrate,
241 .isolate_page = balloon_page_isolate,
242 .putback_page = balloon_page_putback,
244 EXPORT_SYMBOL_GPL(balloon_aops);
246 #endif /* CONFIG_BALLOON_COMPACTION */