aboutsummaryrefslogtreecommitdiffstats
path: root/mm/balloon_compaction.c
diff options
context:
space:
mode:
Diffstat (limited to 'mm/balloon_compaction.c')
-rw-r--r--mm/balloon_compaction.c123
1 files changed, 20 insertions, 103 deletions
diff --git a/mm/balloon_compaction.c b/mm/balloon_compaction.c
index 6e45a5074bf0..b3cbe19f71b5 100644
--- a/mm/balloon_compaction.c
+++ b/mm/balloon_compaction.c
@@ -11,32 +11,6 @@
11#include <linux/balloon_compaction.h> 11#include <linux/balloon_compaction.h>
12 12
13/* 13/*
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.
17 *
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.
21 */
22struct balloon_dev_info *balloon_devinfo_alloc(void *balloon_dev_descriptor)
23{
24 struct balloon_dev_info *b_dev_info;
25 b_dev_info = kmalloc(sizeof(*b_dev_info), GFP_KERNEL);
26 if (!b_dev_info)
27 return ERR_PTR(-ENOMEM);
28
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);
34
35 return b_dev_info;
36}
37EXPORT_SYMBOL_GPL(balloon_devinfo_alloc);
38
39/*
40 * balloon_page_enqueue - allocates a new page and inserts it into the balloon 14 * balloon_page_enqueue - allocates a new page and inserts it into the balloon
41 * page list. 15 * page list.
42 * @b_dev_info: balloon device decriptor where we will insert a new page to 16 * @b_dev_info: balloon device decriptor where we will insert a new page to
@@ -61,7 +35,8 @@ struct page *balloon_page_enqueue(struct balloon_dev_info *b_dev_info)
61 */ 35 */
62 BUG_ON(!trylock_page(page)); 36 BUG_ON(!trylock_page(page));
63 spin_lock_irqsave(&b_dev_info->pages_lock, flags); 37 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
64 balloon_page_insert(page, b_dev_info->mapping, &b_dev_info->pages); 38 balloon_page_insert(b_dev_info, page);
39 __count_vm_event(BALLOON_INFLATE);
65 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags); 40 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
66 unlock_page(page); 41 unlock_page(page);
67 return page; 42 return page;
@@ -93,18 +68,14 @@ struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info)
93 * to be released by the balloon driver. 68 * to be released by the balloon driver.
94 */ 69 */
95 if (trylock_page(page)) { 70 if (trylock_page(page)) {
71 if (!PagePrivate(page)) {
72 /* raced with isolation */
73 unlock_page(page);
74 continue;
75 }
96 spin_lock_irqsave(&b_dev_info->pages_lock, flags); 76 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
97 /*
98 * Raise the page refcount here to prevent any wrong
99 * attempt to isolate this page, in case of coliding
100 * with balloon_page_isolate() just after we release
101 * the page lock.
102 *
103 * balloon_page_free() will take care of dropping
104 * this extra refcount later.
105 */
106 get_page(page);
107 balloon_page_delete(page); 77 balloon_page_delete(page);
78 __count_vm_event(BALLOON_DEFLATE);
108 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags); 79 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
109 unlock_page(page); 80 unlock_page(page);
110 dequeued_page = true; 81 dequeued_page = true;
@@ -132,62 +103,14 @@ struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info)
132EXPORT_SYMBOL_GPL(balloon_page_dequeue); 103EXPORT_SYMBOL_GPL(balloon_page_dequeue);
133 104
134#ifdef CONFIG_BALLOON_COMPACTION 105#ifdef CONFIG_BALLOON_COMPACTION
135/*
136 * balloon_mapping_alloc - allocates a special ->mapping for ballooned pages.
137 * @b_dev_info: holds the balloon device information descriptor.
138 * @a_ops: balloon_mapping address_space_operations descriptor.
139 *
140 * Driver must call it to properly allocate and initialize an instance of
141 * struct address_space which will be used as the special page->mapping for
142 * balloon device enlisted page instances.
143 */
144struct address_space *balloon_mapping_alloc(struct balloon_dev_info *b_dev_info,
145 const struct address_space_operations *a_ops)
146{
147 struct address_space *mapping;
148
149 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
150 if (!mapping)
151 return ERR_PTR(-ENOMEM);
152
153 /*
154 * Give a clean 'zeroed' status to all elements of this special
155 * balloon page->mapping struct address_space instance.
156 */
157 address_space_init_once(mapping);
158
159 /*
160 * Set mapping->flags appropriately, to allow balloon pages
161 * ->mapping identification.
162 */
163 mapping_set_balloon(mapping);
164 mapping_set_gfp_mask(mapping, balloon_mapping_gfp_mask());
165
166 /* balloon's page->mapping->a_ops callback descriptor */
167 mapping->a_ops = a_ops;
168
169 /*
170 * Establish a pointer reference back to the balloon device descriptor
171 * this particular page->mapping will be servicing.
172 * This is used by compaction / migration procedures to identify and
173 * access the balloon device pageset while isolating / migrating pages.
174 *
175 * As some balloon drivers can register multiple balloon devices
176 * for a single guest, this also helps compaction / migration to
177 * properly deal with multiple balloon pagesets, when required.
178 */
179 mapping->private_data = b_dev_info;
180 b_dev_info->mapping = mapping;
181
182 return mapping;
183}
184EXPORT_SYMBOL_GPL(balloon_mapping_alloc);
185 106
186static inline void __isolate_balloon_page(struct page *page) 107static inline void __isolate_balloon_page(struct page *page)
187{ 108{
188 struct balloon_dev_info *b_dev_info = page->mapping->private_data; 109 struct balloon_dev_info *b_dev_info = balloon_page_device(page);
189 unsigned long flags; 110 unsigned long flags;
111
190 spin_lock_irqsave(&b_dev_info->pages_lock, flags); 112 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
113 ClearPagePrivate(page);
191 list_del(&page->lru); 114 list_del(&page->lru);
192 b_dev_info->isolated_pages++; 115 b_dev_info->isolated_pages++;
193 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags); 116 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
@@ -195,20 +118,16 @@ static inline void __isolate_balloon_page(struct page *page)
195 118
196static inline void __putback_balloon_page(struct page *page) 119static inline void __putback_balloon_page(struct page *page)
197{ 120{
198 struct balloon_dev_info *b_dev_info = page->mapping->private_data; 121 struct balloon_dev_info *b_dev_info = balloon_page_device(page);
199 unsigned long flags; 122 unsigned long flags;
123
200 spin_lock_irqsave(&b_dev_info->pages_lock, flags); 124 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
125 SetPagePrivate(page);
201 list_add(&page->lru, &b_dev_info->pages); 126 list_add(&page->lru, &b_dev_info->pages);
202 b_dev_info->isolated_pages--; 127 b_dev_info->isolated_pages--;
203 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags); 128 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
204} 129}
205 130
206static inline int __migrate_balloon_page(struct address_space *mapping,
207 struct page *newpage, struct page *page, enum migrate_mode mode)
208{
209 return page->mapping->a_ops->migratepage(mapping, newpage, page, mode);
210}
211
212/* __isolate_lru_page() counterpart for a ballooned page */ 131/* __isolate_lru_page() counterpart for a ballooned page */
213bool balloon_page_isolate(struct page *page) 132bool balloon_page_isolate(struct page *page)
214{ 133{
@@ -235,12 +154,11 @@ bool balloon_page_isolate(struct page *page)
235 */ 154 */
236 if (likely(trylock_page(page))) { 155 if (likely(trylock_page(page))) {
237 /* 156 /*
238 * A ballooned page, by default, has just one refcount. 157 * A ballooned page, by default, has PagePrivate set.
239 * Prevent concurrent compaction threads from isolating 158 * Prevent concurrent compaction threads from isolating
240 * an already isolated balloon page by refcount check. 159 * an already isolated balloon page by clearing it.
241 */ 160 */
242 if (__is_movable_balloon_page(page) && 161 if (balloon_page_movable(page)) {
243 page_count(page) == 2) {
244 __isolate_balloon_page(page); 162 __isolate_balloon_page(page);
245 unlock_page(page); 163 unlock_page(page);
246 return true; 164 return true;
@@ -276,7 +194,7 @@ void balloon_page_putback(struct page *page)
276int balloon_page_migrate(struct page *newpage, 194int balloon_page_migrate(struct page *newpage,
277 struct page *page, enum migrate_mode mode) 195 struct page *page, enum migrate_mode mode)
278{ 196{
279 struct address_space *mapping; 197 struct balloon_dev_info *balloon = balloon_page_device(page);
280 int rc = -EAGAIN; 198 int rc = -EAGAIN;
281 199
282 /* 200 /*
@@ -292,9 +210,8 @@ int balloon_page_migrate(struct page *newpage,
292 return rc; 210 return rc;
293 } 211 }
294 212
295 mapping = page->mapping; 213 if (balloon && balloon->migratepage)
296 if (mapping) 214 rc = balloon->migratepage(balloon, newpage, page, mode);
297 rc = __migrate_balloon_page(mapping, newpage, page, mode);
298 215
299 unlock_page(newpage); 216 unlock_page(newpage);
300 return rc; 217 return rc;