2 * include/linux/balloon_compaction.h
4 * Common interface definitions for making balloon pages movable by compaction.
6 * Despite being perfectly possible to perform ballooned pages migration, they
7 * make a special corner case to compaction scans because balloon pages are not
8 * enlisted at any LRU list like the other pages we do compact / migrate.
10 * As the page isolation scanning step a compaction thread does is a lockless
11 * procedure (from a page standpoint), it might bring some racy situations while
12 * performing balloon page compaction. In order to sort out these racy scenarios
13 * and safely perform balloon's page compaction and migration we must, always,
14 * ensure following these three simple rules:
16 * i. when updating a balloon's page ->mapping element, strictly do it under
17 * the following lock order, independently of the far superior
18 * locking scheme (lru_lock, balloon_lock):
20 * +--spin_lock_irq(&b_dev_info->pages_lock);
21 * ... page->mapping updates here ...
23 * ii. before isolating or dequeueing a balloon page from the balloon device
24 * pages list, the page reference counter must be raised by one and the
25 * extra refcount must be dropped when the page is enqueued back into
26 * the balloon device page list, thus a balloon page keeps its reference
27 * counter raised only while it is under our special handling;
29 * iii. after the lockless scan step have selected a potential balloon page for
30 * isolation, re-test the page->mapping flags and the page ref counter
31 * under the proper page lock, to ensure isolating a valid balloon page
32 * (not yet isolated, nor under release procedure)
34 * The functions provided by this interface are placed to help on coping with
35 * the aforementioned balloon page corner case, as well as to ensure the simple
36 * set of exposed rules are satisfied while we are dealing with balloon pages
37 * compaction / migration.
39 * Copyright (C) 2012, Red Hat, Inc. Rafael Aquini <aquini@redhat.com>
41 #ifndef _LINUX_BALLOON_COMPACTION_H
42 #define _LINUX_BALLOON_COMPACTION_H
43 #include <linux/pagemap.h>
44 #include <linux/page-flags.h>
45 #include <linux/migrate.h>
46 #include <linux/gfp.h>
47 #include <linux/err.h>
50 * Balloon device information descriptor.
51 * This struct is used to allow the common balloon compaction interface
52 * procedures to find the proper balloon device holding memory pages they'll
53 * have to cope for page compaction / migration, as well as it serves the
54 * balloon driver as a page book-keeper for its registered balloon devices.
56 struct balloon_dev_info {
57 void *balloon_device; /* balloon device descriptor */
58 struct address_space *mapping; /* balloon special page->mapping */
59 unsigned long isolated_pages; /* # of isolated pages for migration */
60 spinlock_t pages_lock; /* Protection to pages list */
61 struct list_head pages; /* Pages enqueued & handled to Host */
64 extern struct page *balloon_page_enqueue(struct balloon_dev_info *b_dev_info);
65 extern struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info);
66 extern struct balloon_dev_info *balloon_devinfo_alloc(
67 void *balloon_dev_descriptor);
69 static inline void balloon_devinfo_free(struct balloon_dev_info *b_dev_info)
75 * balloon_page_free - release a balloon page back to the page free lists
76 * @page: ballooned page to be set free
78 * This function must be used to properly set free an isolated/dequeued balloon
79 * page at the end of a sucessful page migration, or at the balloon driver's
80 * page release procedure.
82 static inline void balloon_page_free(struct page *page)
85 * Balloon pages always get an extra refcount before being isolated
86 * and before being dequeued to help on sorting out fortuite colisions
87 * between a thread attempting to isolate and another thread attempting
88 * to release the very same balloon page.
90 * Before we handle the page back to Buddy, lets drop its extra refcnt.
96 #ifdef CONFIG_BALLOON_COMPACTION
97 extern bool balloon_page_isolate(struct page *page);
98 extern void balloon_page_putback(struct page *page);
99 extern int balloon_page_migrate(struct page *newpage,
100 struct page *page, enum migrate_mode mode);
101 extern struct address_space
102 *balloon_mapping_alloc(struct balloon_dev_info *b_dev_info,
103 const struct address_space_operations *a_ops);
105 static inline void balloon_mapping_free(struct address_space *balloon_mapping)
107 kfree(balloon_mapping);
111 * page_flags_cleared - helper to perform balloon @page ->flags tests.
113 * As balloon pages are obtained from buddy and we do not play with page->flags
114 * at driver level (exception made when we get the page lock for compaction),
115 * we can safely identify a ballooned page by checking if the
116 * PAGE_FLAGS_CHECK_AT_PREP page->flags are all cleared. This approach also
117 * helps us skip ballooned pages that are locked for compaction or release, thus
118 * mitigating their racy check at balloon_page_movable()
120 static inline bool page_flags_cleared(struct page *page)
122 return !(page->flags & PAGE_FLAGS_CHECK_AT_PREP);
126 * __is_movable_balloon_page - helper to perform @page mapping->flags tests
128 static inline bool __is_movable_balloon_page(struct page *page)
130 struct address_space *mapping = page->mapping;
131 return mapping_balloon(mapping);
135 * balloon_page_movable - test page->mapping->flags to identify balloon pages
136 * that can be moved by compaction/migration.
138 * This function is used at core compaction's page isolation scheme, therefore
139 * most pages exposed to it are not enlisted as balloon pages and so, to avoid
140 * undesired side effects like racing against __free_pages(), we cannot afford
141 * holding the page locked while testing page->mapping->flags here.
143 * As we might return false positives in the case of a balloon page being just
144 * released under us, the page->mapping->flags need to be re-tested later,
145 * under the proper page lock, at the functions that will be coping with the
148 static inline bool balloon_page_movable(struct page *page)
151 * Before dereferencing and testing mapping->flags, let's make sure
152 * this is not a page that uses ->mapping in a different way
154 if (page_flags_cleared(page) && !page_mapped(page) &&
155 page_count(page) == 1)
156 return __is_movable_balloon_page(page);
162 * isolated_balloon_page - identify an isolated balloon page on private
163 * compaction/migration page lists.
165 * After a compaction thread isolates a balloon page for migration, it raises
166 * the page refcount to prevent concurrent compaction threads from re-isolating
167 * the same page. For that reason putback_movable_pages(), or other routines
168 * that need to identify isolated balloon pages on private pagelists, cannot
169 * rely on balloon_page_movable() to accomplish the task.
171 static inline bool isolated_balloon_page(struct page *page)
173 /* Already isolated balloon pages, by default, have a raised refcount */
174 if (page_flags_cleared(page) && !page_mapped(page) &&
175 page_count(page) >= 2)
176 return __is_movable_balloon_page(page);
182 * balloon_page_insert - insert a page into the balloon's page list and make
183 * the page->mapping assignment accordingly.
184 * @page : page to be assigned as a 'balloon page'
185 * @mapping : allocated special 'balloon_mapping'
186 * @head : balloon's device page list head
188 * Caller must ensure the page is locked and the spin_lock protecting balloon
189 * pages list is held before inserting a page into the balloon device.
191 static inline void balloon_page_insert(struct page *page,
192 struct address_space *mapping,
193 struct list_head *head)
195 page->mapping = mapping;
196 list_add(&page->lru, head);
200 * balloon_page_delete - delete a page from balloon's page list and clear
201 * the page->mapping assignement accordingly.
202 * @page : page to be released from balloon's page list
204 * Caller must ensure the page is locked and the spin_lock protecting balloon
205 * pages list is held before deleting a page from the balloon device.
207 static inline void balloon_page_delete(struct page *page)
209 page->mapping = NULL;
210 list_del(&page->lru);
214 * balloon_page_device - get the b_dev_info descriptor for the balloon device
215 * that enqueues the given page.
217 static inline struct balloon_dev_info *balloon_page_device(struct page *page)
219 struct address_space *mapping = page->mapping;
221 return mapping->private_data;
226 static inline gfp_t balloon_mapping_gfp_mask(void)
228 return GFP_HIGHUSER_MOVABLE;
231 static inline bool balloon_compaction_check(void)
236 #else /* !CONFIG_BALLOON_COMPACTION */
238 static inline void *balloon_mapping_alloc(void *balloon_device,
239 const struct address_space_operations *a_ops)
241 return ERR_PTR(-EOPNOTSUPP);
244 static inline void balloon_mapping_free(struct address_space *balloon_mapping)
249 static inline void balloon_page_insert(struct page *page,
250 struct address_space *mapping,
251 struct list_head *head)
253 list_add(&page->lru, head);
256 static inline void balloon_page_delete(struct page *page)
258 list_del(&page->lru);
261 static inline bool balloon_page_movable(struct page *page)
266 static inline bool isolated_balloon_page(struct page *page)
271 static inline bool balloon_page_isolate(struct page *page)
276 static inline void balloon_page_putback(struct page *page)
281 static inline int balloon_page_migrate(struct page *newpage,
282 struct page *page, enum migrate_mode mode)
287 static inline gfp_t balloon_mapping_gfp_mask(void)
292 static inline bool balloon_compaction_check(void)
296 #endif /* CONFIG_BALLOON_COMPACTION */
297 #endif /* _LINUX_BALLOON_COMPACTION_H */