2 * mm/balloon_compaction.c
4 * Common interface for making balloon pages movable by compaction.
6 * Copyright (C) 2012, Red Hat, Inc. Rafael Aquini <aquini@redhat.com>
9 #include <linux/slab.h>
10 #include <linux/export.h>
11 #include <linux/balloon_compaction.h>
14 * balloon_devinfo_alloc - allocates a balloon device information descriptor.
15 * @balloon_dev_descriptor: pointer to reference the balloon device which
16 * this struct balloon_dev_info will be servicing.
18 * Driver must call it to properly allocate and initialize an instance of
19 * struct balloon_dev_info which will be used to reference a balloon device
20 * as well as to keep track of the balloon device page list.
22 struct balloon_dev_info *balloon_devinfo_alloc(void *balloon_dev_descriptor)
24 struct balloon_dev_info *b_dev_info;
25 b_dev_info = kmalloc(sizeof(*b_dev_info), GFP_KERNEL);
27 return ERR_PTR(-ENOMEM);
29 b_dev_info->balloon_device = balloon_dev_descriptor;
30 b_dev_info->mapping = NULL;
31 b_dev_info->isolated_pages = 0;
32 spin_lock_init(&b_dev_info->pages_lock);
33 INIT_LIST_HEAD(&b_dev_info->pages);
37 EXPORT_SYMBOL_GPL(balloon_devinfo_alloc);
40 * balloon_page_enqueue - allocates a new page and inserts it into the balloon
42 * @b_dev_info: balloon device decriptor where we will insert a new page to
44 * Driver must call it to properly allocate a new enlisted balloon page
45 * before definetively removing it from the guest system.
46 * This function returns the page address for the recently enqueued page or
47 * NULL in the case we fail to allocate a new page this turn.
49 struct page *balloon_page_enqueue(struct balloon_dev_info *b_dev_info)
52 struct page *page = alloc_page(balloon_mapping_gfp_mask() |
53 __GFP_NOMEMALLOC | __GFP_NORETRY);
58 * Block others from accessing the 'page' when we get around to
59 * establishing additional references. We should be the only one
60 * holding a reference to the 'page' at this point.
62 BUG_ON(!trylock_page(page));
63 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
64 balloon_page_insert(page, b_dev_info->mapping, &b_dev_info->pages);
65 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
69 EXPORT_SYMBOL_GPL(balloon_page_enqueue);
72 * balloon_page_dequeue - removes a page from balloon's page list and returns
73 * the its address to allow the driver release the page.
74 * @b_dev_info: balloon device decriptor where we will grab a page from.
76 * Driver must call it to properly de-allocate a previous enlisted balloon page
77 * before definetively releasing it back to the guest system.
78 * This function returns the page address for the recently dequeued page or
79 * NULL in the case we find balloon's page list temporarily empty due to
80 * compaction isolated pages.
82 struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info)
84 struct page *page, *tmp;
88 dequeued_page = false;
89 list_for_each_entry_safe(page, tmp, &b_dev_info->pages, lru) {
91 * Block others from accessing the 'page' while we get around
92 * establishing additional references and preparing the 'page'
93 * to be released by the balloon driver.
95 if (trylock_page(page)) {
96 if (!PagePrivate(page)) {
97 /* raced with isolation */
101 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
102 balloon_page_delete(page);
103 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
105 dequeued_page = true;
110 if (!dequeued_page) {
112 * If we are unable to dequeue a balloon page because the page
113 * list is empty and there is no isolated pages, then something
114 * went out of track and some balloon pages are lost.
115 * BUG() here, otherwise the balloon driver may get stuck into
116 * an infinite loop while attempting to release all its pages.
118 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
119 if (unlikely(list_empty(&b_dev_info->pages) &&
120 !b_dev_info->isolated_pages))
122 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
127 EXPORT_SYMBOL_GPL(balloon_page_dequeue);
129 #ifdef CONFIG_BALLOON_COMPACTION
131 * balloon_mapping_alloc - allocates a special ->mapping for ballooned pages.
132 * @b_dev_info: holds the balloon device information descriptor.
133 * @a_ops: balloon_mapping address_space_operations descriptor.
135 * Driver must call it to properly allocate and initialize an instance of
136 * struct address_space which will be used as the special page->mapping for
137 * balloon device enlisted page instances.
139 struct address_space *balloon_mapping_alloc(struct balloon_dev_info *b_dev_info,
140 const struct address_space_operations *a_ops)
142 struct address_space *mapping;
144 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
146 return ERR_PTR(-ENOMEM);
149 * Give a clean 'zeroed' status to all elements of this special
150 * balloon page->mapping struct address_space instance.
152 address_space_init_once(mapping);
155 * Set mapping->flags appropriately, to allow balloon pages
156 * ->mapping identification.
158 mapping_set_balloon(mapping);
159 mapping_set_gfp_mask(mapping, balloon_mapping_gfp_mask());
161 /* balloon's page->mapping->a_ops callback descriptor */
162 mapping->a_ops = a_ops;
165 * Establish a pointer reference back to the balloon device descriptor
166 * this particular page->mapping will be servicing.
167 * This is used by compaction / migration procedures to identify and
168 * access the balloon device pageset while isolating / migrating pages.
170 * As some balloon drivers can register multiple balloon devices
171 * for a single guest, this also helps compaction / migration to
172 * properly deal with multiple balloon pagesets, when required.
174 mapping->private_data = b_dev_info;
175 b_dev_info->mapping = mapping;
179 EXPORT_SYMBOL_GPL(balloon_mapping_alloc);
181 static inline void __isolate_balloon_page(struct page *page)
183 struct balloon_dev_info *b_dev_info = page->mapping->private_data;
186 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
187 ClearPagePrivate(page);
188 list_del(&page->lru);
189 b_dev_info->isolated_pages++;
190 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
193 static inline void __putback_balloon_page(struct page *page)
195 struct balloon_dev_info *b_dev_info = page->mapping->private_data;
198 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
199 SetPagePrivate(page);
200 list_add(&page->lru, &b_dev_info->pages);
201 b_dev_info->isolated_pages--;
202 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
205 static inline int __migrate_balloon_page(struct address_space *mapping,
206 struct page *newpage, struct page *page, enum migrate_mode mode)
208 return page->mapping->a_ops->migratepage(mapping, newpage, page, mode);
211 /* __isolate_lru_page() counterpart for a ballooned page */
212 bool balloon_page_isolate(struct page *page)
215 * Avoid burning cycles with pages that are yet under __free_pages(),
216 * or just got freed under us.
218 * In case we 'win' a race for a balloon page being freed under us and
219 * raise its refcount preventing __free_pages() from doing its job
220 * the put_page() at the end of this block will take care of
221 * release this page, thus avoiding a nasty leakage.
223 if (likely(get_page_unless_zero(page))) {
225 * As balloon pages are not isolated from LRU lists, concurrent
226 * compaction threads can race against page migration functions
227 * as well as race against the balloon driver releasing a page.
229 * In order to avoid having an already isolated balloon page
230 * being (wrongly) re-isolated while it is under migration,
231 * or to avoid attempting to isolate pages being released by
232 * the balloon driver, lets be sure we have the page lock
233 * before proceeding with the balloon page isolation steps.
235 if (likely(trylock_page(page))) {
237 * A ballooned page, by default, has PagePrivate set.
238 * Prevent concurrent compaction threads from isolating
239 * an already isolated balloon page by clearing it.
241 if (balloon_page_movable(page)) {
242 __isolate_balloon_page(page);
253 /* putback_lru_page() counterpart for a ballooned page */
254 void balloon_page_putback(struct page *page)
257 * 'lock_page()' stabilizes the page and prevents races against
258 * concurrent isolation threads attempting to re-isolate it.
262 if (__is_movable_balloon_page(page)) {
263 __putback_balloon_page(page);
264 /* drop the extra ref count taken for page isolation */
268 dump_page(page, "not movable balloon page");
273 /* move_to_new_page() counterpart for a ballooned page */
274 int balloon_page_migrate(struct page *newpage,
275 struct page *page, enum migrate_mode mode)
277 struct address_space *mapping;
281 * Block others from accessing the 'newpage' when we get around to
282 * establishing additional references. We should be the only one
283 * holding a reference to the 'newpage' at this point.
285 BUG_ON(!trylock_page(newpage));
287 if (WARN_ON(!__is_movable_balloon_page(page))) {
288 dump_page(page, "not movable balloon page");
289 unlock_page(newpage);
293 mapping = page->mapping;
295 rc = __migrate_balloon_page(mapping, newpage, page, mode);
297 unlock_page(newpage);
300 #endif /* CONFIG_BALLOON_COMPACTION */