2 * mm/balloon_compaction.c
4 * Common interface for making balloon pages movable to 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);
57 BUG_ON(!trylock_page(page));
58 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
59 balloon_page_insert(page, b_dev_info->mapping, &b_dev_info->pages);
60 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
64 EXPORT_SYMBOL_GPL(balloon_page_enqueue);
67 * balloon_page_dequeue - removes a page from balloon's page list and returns
68 * the its address to allow the driver release the page.
69 * @b_dev_info: balloon device decriptor where we will grab a page from.
71 * Driver must call it to properly de-allocate a previous enlisted balloon page
72 * before definetively releasing it back to the guest system.
73 * This function returns the page address for the recently dequeued page or
74 * NULL in the case we find balloon's page list temporarely empty due to
75 * compaction isolated pages.
77 struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info)
79 struct page *page, *tmp;
83 dequeued_page = false;
84 list_for_each_entry_safe(page, tmp, &b_dev_info->pages, lru) {
85 if (trylock_page(page)) {
86 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
87 balloon_page_delete(page);
88 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
96 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
97 if (unlikely(list_empty(&b_dev_info->pages) &&
98 !b_dev_info->isolated_pages))
100 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
105 EXPORT_SYMBOL_GPL(balloon_page_dequeue);
107 #ifdef CONFIG_BALLOON_COMPACTION
109 * balloon_mapping_alloc - allocates a special ->mapping for ballooned pages.
110 * @b_dev_info: holds the balloon device information descriptor.
111 * @a_ops: balloon_mapping address_space_operations descriptor.
113 * Driver must call it to properly allocate and initialize an instance of
114 * struct address_space which will be used as the special page->mapping for
115 * balloon device enlisted page instances.
117 struct address_space *balloon_mapping_alloc(struct balloon_dev_info *b_dev_info,
118 const struct address_space_operations *a_ops)
120 struct address_space *mapping;
122 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
124 return ERR_PTR(-ENOMEM);
127 * Give a clean 'zeroed' status to all elements of this special
128 * balloon page->mapping struct address_space instance.
130 address_space_init_once(mapping);
133 * Set mapping->flags appropriately, to allow balloon pages
134 * ->mapping identification.
136 mapping_set_balloon(mapping);
137 mapping_set_gfp_mask(mapping, balloon_mapping_gfp_mask());
139 /* balloon's page->mapping->a_ops callback descriptor */
140 mapping->a_ops = a_ops;
143 * Establish a pointer reference back to the balloon device descriptor
144 * this particular page->mapping will be servicing.
145 * This is used by compaction / migration procedures to identify and
146 * access the balloon device pageset while isolating / migrating pages.
148 * As some balloon drivers can register multiple balloon devices
149 * for a single guest, this also helps compaction / migration to
150 * properly deal with multiple balloon pagesets, when required.
152 mapping->private_data = b_dev_info;
153 b_dev_info->mapping = mapping;
157 EXPORT_SYMBOL_GPL(balloon_mapping_alloc);
159 static inline void __isolate_balloon_page(struct page *page)
161 struct balloon_dev_info *b_dev_info = page->mapping->private_data;
163 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
164 list_del(&page->lru);
165 b_dev_info->isolated_pages++;
166 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
169 static inline void __putback_balloon_page(struct page *page)
171 struct balloon_dev_info *b_dev_info = page->mapping->private_data;
173 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
174 list_add(&page->lru, &b_dev_info->pages);
175 b_dev_info->isolated_pages--;
176 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
179 static inline int __migrate_balloon_page(struct address_space *mapping,
180 struct page *newpage, struct page *page, enum migrate_mode mode)
182 return page->mapping->a_ops->migratepage(mapping, newpage, page, mode);
185 /* __isolate_lru_page() counterpart for a ballooned page */
186 bool balloon_page_isolate(struct page *page)
189 * Avoid burning cycles with pages that are yet under __free_pages(),
190 * or just got freed under us.
192 * In case we 'win' a race for a balloon page being freed under us and
193 * raise its refcount preventing __free_pages() from doing its job
194 * the put_page() at the end of this block will take care of
195 * release this page, thus avoiding a nasty leakage.
197 if (likely(get_page_unless_zero(page))) {
199 * As balloon pages are not isolated from LRU lists, concurrent
200 * compaction threads can race against page migration functions
201 * as well as race against the balloon driver releasing a page.
203 * In order to avoid having an already isolated balloon page
204 * being (wrongly) re-isolated while it is under migration,
205 * or to avoid attempting to isolate pages being released by
206 * the balloon driver, lets be sure we have the page lock
207 * before proceeding with the balloon page isolation steps.
209 if (likely(trylock_page(page))) {
211 * A ballooned page, by default, has just one refcount.
212 * Prevent concurrent compaction threads from isolating
213 * an already isolated balloon page by refcount check.
215 if (__is_movable_balloon_page(page) &&
216 page_count(page) == 2) {
217 __isolate_balloon_page(page);
218 balloon_event_count(COMPACTBALLOONISOLATED);
229 /* putback_lru_page() counterpart for a ballooned page */
230 void balloon_page_putback(struct page *page)
233 * 'lock_page()' stabilizes the page and prevents races against
234 * concurrent isolation threads attempting to re-isolate it.
238 if (__is_movable_balloon_page(page)) {
239 __putback_balloon_page(page);
241 balloon_event_count(COMPACTBALLOONRETURNED);
249 /* move_to_new_page() counterpart for a ballooned page */
250 int balloon_page_migrate(struct page *newpage,
251 struct page *page, enum migrate_mode mode)
253 struct address_space *mapping;
256 BUG_ON(!trylock_page(newpage));
258 if (WARN_ON(!__is_movable_balloon_page(page))) {
260 unlock_page(newpage);
264 mapping = page->mapping;
266 rc = __migrate_balloon_page(mapping, newpage, page, mode);
268 unlock_page(newpage);
271 #endif /* CONFIG_BALLOON_COMPACTION */