]>
Commit | Line | Data |
---|---|---|
1775826c | 1 | /****************************************************************************** |
1775826c JF |
2 | * Xen balloon driver - enables returning/claiming memory to/from Xen. |
3 | * | |
4 | * Copyright (c) 2003, B Dragovic | |
5 | * Copyright (c) 2003-2004, M Williamson, K Fraser | |
6 | * Copyright (c) 2005 Dan M. Smith, IBM Corporation | |
080e2be7 DK |
7 | * Copyright (c) 2010 Daniel Kiper |
8 | * | |
9 | * Memory hotplug support was written by Daniel Kiper. Work on | |
10 | * it was sponsored by Google under Google Summer of Code 2010 | |
11 | * program. Jeremy Fitzhardinge from Citrix was the mentor for | |
12 | * this project. | |
1775826c JF |
13 | * |
14 | * This program is free software; you can redistribute it and/or | |
15 | * modify it under the terms of the GNU General Public License version 2 | |
16 | * as published by the Free Software Foundation; or, when distributed | |
17 | * separately from the Linux kernel or incorporated into other | |
18 | * software packages, subject to the following license: | |
19 | * | |
20 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
21 | * of this source file (the "Software"), to deal in the Software without | |
22 | * restriction, including without limitation the rights to use, copy, modify, | |
23 | * merge, publish, distribute, sublicense, and/or sell copies of the Software, | |
24 | * and to permit persons to whom the Software is furnished to do so, subject to | |
25 | * the following conditions: | |
26 | * | |
27 | * The above copyright notice and this permission notice shall be included in | |
28 | * all copies or substantial portions of the Software. | |
29 | * | |
30 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
31 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
32 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
33 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
34 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
35 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
36 | * IN THE SOFTWARE. | |
37 | */ | |
38 | ||
283c0972 JP |
39 | #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt |
40 | ||
cd9151e2 | 41 | #include <linux/cpu.h> |
1775826c | 42 | #include <linux/kernel.h> |
1775826c JF |
43 | #include <linux/sched.h> |
44 | #include <linux/errno.h> | |
72ee5112 | 45 | #include <linux/module.h> |
1775826c JF |
46 | #include <linux/mm.h> |
47 | #include <linux/bootmem.h> | |
48 | #include <linux/pagemap.h> | |
49 | #include <linux/highmem.h> | |
50 | #include <linux/mutex.h> | |
1775826c | 51 | #include <linux/list.h> |
5a0e3ad6 | 52 | #include <linux/gfp.h> |
080e2be7 DK |
53 | #include <linux/notifier.h> |
54 | #include <linux/memory.h> | |
55 | #include <linux/memory_hotplug.h> | |
cd9151e2 | 56 | #include <linux/percpu-defs.h> |
55b3da98 | 57 | #include <linux/slab.h> |
1775826c | 58 | |
1775826c JF |
59 | #include <asm/page.h> |
60 | #include <asm/pgalloc.h> | |
61 | #include <asm/pgtable.h> | |
1775826c JF |
62 | #include <asm/tlb.h> |
63 | ||
ecbf29cd JF |
64 | #include <asm/xen/hypervisor.h> |
65 | #include <asm/xen/hypercall.h> | |
1ccbf534 JF |
66 | |
67 | #include <xen/xen.h> | |
ecbf29cd | 68 | #include <xen/interface/xen.h> |
1775826c | 69 | #include <xen/interface/memory.h> |
803eb047 | 70 | #include <xen/balloon.h> |
1775826c JF |
71 | #include <xen/features.h> |
72 | #include <xen/page.h> | |
73 | ||
95d2ac4a DK |
74 | /* |
75 | * balloon_process() state: | |
76 | * | |
77 | * BP_DONE: done or nothing to do, | |
78 | * BP_EAGAIN: error, go to sleep, | |
79 | * BP_ECANCELED: error, balloon operation canceled. | |
80 | */ | |
1775826c | 81 | |
95d2ac4a DK |
82 | enum bp_state { |
83 | BP_DONE, | |
84 | BP_EAGAIN, | |
85 | BP_ECANCELED | |
1775826c JF |
86 | }; |
87 | ||
1775826c | 88 | |
1775826c | 89 | static DEFINE_MUTEX(balloon_mutex); |
1775826c | 90 | |
803eb047 DDG |
91 | struct balloon_stats balloon_stats; |
92 | EXPORT_SYMBOL_GPL(balloon_stats); | |
1775826c JF |
93 | |
94 | /* We increase/decrease in batches which fit in a page */ | |
965c0aaa | 95 | static xen_pfn_t frame_list[PAGE_SIZE / sizeof(unsigned long)]; |
cd9151e2 | 96 | |
1775826c | 97 | |
1775826c JF |
98 | /* List of ballooned pages, threaded through the mem_map array. */ |
99 | static LIST_HEAD(ballooned_pages); | |
100 | ||
101 | /* Main work function, always executed in process context. */ | |
102 | static void balloon_process(struct work_struct *work); | |
95170b2e | 103 | static DECLARE_DELAYED_WORK(balloon_worker, balloon_process); |
1775826c JF |
104 | |
105 | /* When ballooning out (allocating memory to return to Xen) we don't really | |
106 | want the kernel to try too hard since that can trigger the oom killer. */ | |
107 | #define GFP_BALLOON \ | |
108 | (GFP_HIGHUSER | __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC) | |
109 | ||
110 | static void scrub_page(struct page *page) | |
111 | { | |
112 | #ifdef CONFIG_XEN_SCRUB_PAGES | |
26a3e991 | 113 | clear_highpage(page); |
1775826c JF |
114 | #endif |
115 | } | |
116 | ||
117 | /* balloon_append: add the given page to the balloon. */ | |
9be4d457 | 118 | static void __balloon_append(struct page *page) |
1775826c JF |
119 | { |
120 | /* Lowmem is re-populated first, so highmem pages go at list tail. */ | |
121 | if (PageHighMem(page)) { | |
122 | list_add_tail(&page->lru, &ballooned_pages); | |
123 | balloon_stats.balloon_high++; | |
1775826c JF |
124 | } else { |
125 | list_add(&page->lru, &ballooned_pages); | |
126 | balloon_stats.balloon_low++; | |
127 | } | |
9be4d457 | 128 | } |
3d65c948 | 129 | |
9be4d457 JF |
130 | static void balloon_append(struct page *page) |
131 | { | |
132 | __balloon_append(page); | |
3dcc0571 | 133 | adjust_managed_page_count(page, -1); |
1775826c JF |
134 | } |
135 | ||
136 | /* balloon_retrieve: rescue a page from the balloon, if it is not empty. */ | |
b6f30679 | 137 | static struct page *balloon_retrieve(bool prefer_highmem) |
1775826c JF |
138 | { |
139 | struct page *page; | |
140 | ||
141 | if (list_empty(&ballooned_pages)) | |
142 | return NULL; | |
143 | ||
b6f30679 KRW |
144 | if (prefer_highmem) |
145 | page = list_entry(ballooned_pages.prev, struct page, lru); | |
146 | else | |
147 | page = list_entry(ballooned_pages.next, struct page, lru); | |
1775826c JF |
148 | list_del(&page->lru); |
149 | ||
3dcc0571 | 150 | if (PageHighMem(page)) |
1775826c | 151 | balloon_stats.balloon_high--; |
3dcc0571 | 152 | else |
1775826c JF |
153 | balloon_stats.balloon_low--; |
154 | ||
3dcc0571 | 155 | adjust_managed_page_count(page, 1); |
3d65c948 | 156 | |
1775826c JF |
157 | return page; |
158 | } | |
159 | ||
1775826c JF |
160 | static struct page *balloon_next_page(struct page *page) |
161 | { | |
162 | struct list_head *next = page->lru.next; | |
163 | if (next == &ballooned_pages) | |
164 | return NULL; | |
165 | return list_entry(next, struct page, lru); | |
166 | } | |
167 | ||
95d2ac4a | 168 | static enum bp_state update_schedule(enum bp_state state) |
1775826c | 169 | { |
fd8b7951 BO |
170 | if (state == BP_ECANCELED) |
171 | return BP_ECANCELED; | |
172 | ||
95d2ac4a DK |
173 | if (state == BP_DONE) { |
174 | balloon_stats.schedule_delay = 1; | |
175 | balloon_stats.retry_count = 1; | |
176 | return BP_DONE; | |
177 | } | |
178 | ||
95d2ac4a DK |
179 | ++balloon_stats.retry_count; |
180 | ||
181 | if (balloon_stats.max_retry_count != RETRY_UNLIMITED && | |
182 | balloon_stats.retry_count > balloon_stats.max_retry_count) { | |
95d2ac4a DK |
183 | balloon_stats.schedule_delay = 1; |
184 | balloon_stats.retry_count = 1; | |
185 | return BP_ECANCELED; | |
186 | } | |
187 | ||
188 | balloon_stats.schedule_delay <<= 1; | |
189 | ||
190 | if (balloon_stats.schedule_delay > balloon_stats.max_schedule_delay) | |
191 | balloon_stats.schedule_delay = balloon_stats.max_schedule_delay; | |
192 | ||
193 | return BP_EAGAIN; | |
1775826c JF |
194 | } |
195 | ||
080e2be7 | 196 | #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG |
55b3da98 DV |
197 | static struct resource *additional_memory_resource(phys_addr_t size) |
198 | { | |
199 | struct resource *res; | |
200 | int ret; | |
201 | ||
202 | res = kzalloc(sizeof(*res), GFP_KERNEL); | |
203 | if (!res) | |
204 | return NULL; | |
205 | ||
206 | res->name = "System RAM"; | |
207 | res->flags = IORESOURCE_MEM | IORESOURCE_BUSY; | |
208 | ||
209 | ret = allocate_resource(&iomem_resource, res, | |
210 | size, 0, -1, | |
211 | PAGES_PER_SECTION * PAGE_SIZE, NULL, NULL); | |
212 | if (ret < 0) { | |
213 | pr_err("Cannot allocate new System RAM resource\n"); | |
214 | kfree(res); | |
215 | return NULL; | |
216 | } | |
217 | ||
218 | return res; | |
219 | } | |
220 | ||
221 | static void release_memory_resource(struct resource *resource) | |
222 | { | |
223 | if (!resource) | |
224 | return; | |
225 | ||
226 | /* | |
227 | * No need to reset region to identity mapped since we now | |
228 | * know that no I/O can be in this region | |
229 | */ | |
230 | release_resource(resource); | |
231 | kfree(resource); | |
232 | } | |
080e2be7 DK |
233 | |
234 | static enum bp_state reserve_additional_memory(long credit) | |
235 | { | |
55b3da98 | 236 | struct resource *resource; |
080e2be7 | 237 | int nid, rc; |
55b3da98 | 238 | unsigned long balloon_hotplug; |
080e2be7 | 239 | |
55b3da98 DV |
240 | balloon_hotplug = round_up(credit, PAGES_PER_SECTION); |
241 | ||
242 | resource = additional_memory_resource(balloon_hotplug * PAGE_SIZE); | |
243 | if (!resource) | |
244 | goto err; | |
245 | ||
246 | nid = memory_add_physaddr_to_nid(resource->start); | |
080e2be7 | 247 | |
3c56b3a1 JG |
248 | #ifdef CONFIG_XEN_HAVE_PVMMU |
249 | /* | |
250 | * add_memory() will build page tables for the new memory so | |
251 | * the p2m must contain invalid entries so the correct | |
252 | * non-present PTEs will be written. | |
253 | * | |
254 | * If a failure occurs, the original (identity) p2m entries | |
255 | * are not restored since this region is now known not to | |
256 | * conflict with any devices. | |
257 | */ | |
258 | if (!xen_feature(XENFEAT_auto_translated_physmap)) { | |
259 | unsigned long pfn, i; | |
260 | ||
55b3da98 | 261 | pfn = PFN_DOWN(resource->start); |
3c56b3a1 JG |
262 | for (i = 0; i < balloon_hotplug; i++) { |
263 | if (!set_phys_to_machine(pfn + i, INVALID_P2M_ENTRY)) { | |
264 | pr_warn("set_phys_to_machine() failed, no memory added\n"); | |
55b3da98 | 265 | goto err; |
3c56b3a1 JG |
266 | } |
267 | } | |
268 | } | |
269 | #endif | |
270 | ||
55b3da98 | 271 | rc = add_memory_resource(nid, resource); |
080e2be7 | 272 | if (rc) { |
3dcf6367 | 273 | pr_warn("Cannot add additional memory (%i)\n", rc); |
55b3da98 | 274 | goto err; |
080e2be7 DK |
275 | } |
276 | ||
de5a77d8 | 277 | balloon_stats.total_pages += balloon_hotplug; |
080e2be7 DK |
278 | |
279 | return BP_DONE; | |
55b3da98 DV |
280 | err: |
281 | release_memory_resource(resource); | |
282 | return BP_ECANCELED; | |
080e2be7 DK |
283 | } |
284 | ||
285 | static void xen_online_page(struct page *page) | |
286 | { | |
287 | __online_page_set_limits(page); | |
288 | ||
289 | mutex_lock(&balloon_mutex); | |
290 | ||
291 | __balloon_append(page); | |
292 | ||
080e2be7 DK |
293 | mutex_unlock(&balloon_mutex); |
294 | } | |
295 | ||
296 | static int xen_memory_notifier(struct notifier_block *nb, unsigned long val, void *v) | |
297 | { | |
298 | if (val == MEM_ONLINE) | |
299 | schedule_delayed_work(&balloon_worker, 0); | |
300 | ||
301 | return NOTIFY_OK; | |
302 | } | |
303 | ||
304 | static struct notifier_block xen_memory_nb = { | |
305 | .notifier_call = xen_memory_notifier, | |
306 | .priority = 0 | |
307 | }; | |
308 | #else | |
de5a77d8 | 309 | static enum bp_state reserve_additional_memory(long credit) |
1775826c | 310 | { |
de5a77d8 DV |
311 | balloon_stats.target_pages = balloon_stats.current_pages; |
312 | return BP_DONE; | |
1775826c | 313 | } |
de5a77d8 | 314 | #endif /* CONFIG_XEN_BALLOON_MEMORY_HOTPLUG */ |
1775826c | 315 | |
de5a77d8 | 316 | static long current_credit(void) |
080e2be7 | 317 | { |
de5a77d8 | 318 | return balloon_stats.target_pages - balloon_stats.current_pages; |
080e2be7 DK |
319 | } |
320 | ||
de5a77d8 | 321 | static bool balloon_is_inflated(void) |
080e2be7 | 322 | { |
de5a77d8 | 323 | return balloon_stats.balloon_low || balloon_stats.balloon_high; |
080e2be7 | 324 | } |
080e2be7 | 325 | |
95d2ac4a | 326 | static enum bp_state increase_reservation(unsigned long nr_pages) |
1775826c | 327 | { |
95d2ac4a | 328 | int rc; |
2f70e0ac | 329 | unsigned long pfn, i; |
1775826c | 330 | struct page *page; |
1775826c JF |
331 | struct xen_memory_reservation reservation = { |
332 | .address_bits = 0, | |
333 | .extent_order = 0, | |
334 | .domid = DOMID_SELF | |
335 | }; | |
336 | ||
337 | if (nr_pages > ARRAY_SIZE(frame_list)) | |
338 | nr_pages = ARRAY_SIZE(frame_list); | |
339 | ||
9346c2a8 | 340 | page = list_first_entry_or_null(&ballooned_pages, struct page, lru); |
1775826c | 341 | for (i = 0; i < nr_pages; i++) { |
95d2ac4a DK |
342 | if (!page) { |
343 | nr_pages = i; | |
344 | break; | |
345 | } | |
a419aef8 | 346 | frame_list[i] = page_to_pfn(page); |
1775826c JF |
347 | page = balloon_next_page(page); |
348 | } | |
349 | ||
a90971eb | 350 | set_xen_guest_handle(reservation.extent_start, frame_list); |
fde28e8f JF |
351 | reservation.nr_extents = nr_pages; |
352 | rc = HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation); | |
40095de1 | 353 | if (rc <= 0) |
95d2ac4a | 354 | return BP_EAGAIN; |
1775826c | 355 | |
bc2c0303 | 356 | for (i = 0; i < rc; i++) { |
b6f30679 | 357 | page = balloon_retrieve(false); |
1775826c JF |
358 | BUG_ON(page == NULL); |
359 | ||
360 | pfn = page_to_pfn(page); | |
1775826c | 361 | |
c2374bf5 | 362 | #ifdef CONFIG_XEN_HAVE_PVMMU |
c1d15f5c SS |
363 | if (!xen_feature(XENFEAT_auto_translated_physmap)) { |
364 | set_phys_to_machine(pfn, frame_list[i]); | |
365 | ||
366 | /* Link back into the page tables if not highmem. */ | |
367 | if (!PageHighMem(page)) { | |
368 | int ret; | |
369 | ret = HYPERVISOR_update_va_mapping( | |
370 | (unsigned long)__va(pfn << PAGE_SHIFT), | |
371 | mfn_pte(frame_list[i], PAGE_KERNEL), | |
372 | 0); | |
373 | BUG_ON(ret); | |
374 | } | |
1775826c | 375 | } |
c2374bf5 | 376 | #endif |
1775826c JF |
377 | |
378 | /* Relinquish the page back to the allocator. */ | |
3dcc0571 | 379 | __free_reserved_page(page); |
1775826c JF |
380 | } |
381 | ||
bc2c0303 | 382 | balloon_stats.current_pages += rc; |
1775826c | 383 | |
95d2ac4a | 384 | return BP_DONE; |
1775826c JF |
385 | } |
386 | ||
b6f30679 | 387 | static enum bp_state decrease_reservation(unsigned long nr_pages, gfp_t gfp) |
1775826c | 388 | { |
95d2ac4a | 389 | enum bp_state state = BP_DONE; |
2f70e0ac | 390 | unsigned long pfn, i; |
1775826c | 391 | struct page *page; |
1775826c JF |
392 | int ret; |
393 | struct xen_memory_reservation reservation = { | |
394 | .address_bits = 0, | |
395 | .extent_order = 0, | |
396 | .domid = DOMID_SELF | |
397 | }; | |
398 | ||
399 | if (nr_pages > ARRAY_SIZE(frame_list)) | |
400 | nr_pages = ARRAY_SIZE(frame_list); | |
401 | ||
402 | for (i = 0; i < nr_pages; i++) { | |
fce92683 LN |
403 | page = alloc_page(gfp); |
404 | if (page == NULL) { | |
1775826c | 405 | nr_pages = i; |
95d2ac4a | 406 | state = BP_EAGAIN; |
1775826c JF |
407 | break; |
408 | } | |
09ed3d5b | 409 | scrub_page(page); |
1775826c | 410 | |
09ed3d5b WL |
411 | frame_list[i] = page_to_pfn(page); |
412 | } | |
1775826c | 413 | |
09ed3d5b WL |
414 | /* |
415 | * Ensure that ballooned highmem pages don't have kmaps. | |
416 | * | |
417 | * Do this before changing the p2m as kmap_flush_unused() | |
418 | * reads PTEs to obtain pages (and hence needs the original | |
419 | * p2m entry). | |
420 | */ | |
421 | kmap_flush_unused(); | |
422 | ||
423 | /* Update direct mapping, invalidate P2M, and add to balloon. */ | |
424 | for (i = 0; i < nr_pages; i++) { | |
425 | pfn = frame_list[i]; | |
0df4f266 | 426 | frame_list[i] = pfn_to_gfn(pfn); |
09ed3d5b | 427 | page = pfn_to_page(pfn); |
1058a75f | 428 | |
c1d15f5c | 429 | #ifdef CONFIG_XEN_HAVE_PVMMU |
04660bb5 | 430 | if (!xen_feature(XENFEAT_auto_translated_physmap)) { |
c1d15f5c SS |
431 | if (!PageHighMem(page)) { |
432 | ret = HYPERVISOR_update_va_mapping( | |
433 | (unsigned long)__va(pfn << PAGE_SHIFT), | |
0bb599fd | 434 | __pte_ma(0), 0); |
c1d15f5c | 435 | BUG_ON(ret); |
fb9a0c44 DV |
436 | } |
437 | __set_phys_to_machine(pfn, INVALID_P2M_ENTRY); | |
04660bb5 | 438 | } |
c1d15f5c | 439 | #endif |
24f69373 | 440 | |
09ed3d5b | 441 | balloon_append(page); |
1775826c JF |
442 | } |
443 | ||
24f69373 | 444 | flush_tlb_all(); |
2bad07ce | 445 | |
a90971eb | 446 | set_xen_guest_handle(reservation.extent_start, frame_list); |
1775826c JF |
447 | reservation.nr_extents = nr_pages; |
448 | ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation); | |
449 | BUG_ON(ret != nr_pages); | |
450 | ||
451 | balloon_stats.current_pages -= nr_pages; | |
1775826c | 452 | |
95d2ac4a | 453 | return state; |
1775826c JF |
454 | } |
455 | ||
456 | /* | |
929423fa | 457 | * As this is a work item it is guaranteed to run as a single instance only. |
1775826c JF |
458 | * We may of course race updates of the target counts (which are protected |
459 | * by the balloon lock), or with changes to the Xen hard limit, but we will | |
460 | * recover from these in time. | |
461 | */ | |
462 | static void balloon_process(struct work_struct *work) | |
463 | { | |
95d2ac4a | 464 | enum bp_state state = BP_DONE; |
1775826c JF |
465 | long credit; |
466 | ||
1775826c JF |
467 | |
468 | do { | |
929423fa JG |
469 | mutex_lock(&balloon_mutex); |
470 | ||
83be7e52 | 471 | credit = current_credit(); |
95d2ac4a | 472 | |
080e2be7 DK |
473 | if (credit > 0) { |
474 | if (balloon_is_inflated()) | |
475 | state = increase_reservation(credit); | |
476 | else | |
477 | state = reserve_additional_memory(credit); | |
478 | } | |
95d2ac4a | 479 | |
1775826c | 480 | if (credit < 0) |
b6f30679 | 481 | state = decrease_reservation(-credit, GFP_BALLOON); |
95d2ac4a DK |
482 | |
483 | state = update_schedule(state); | |
1775826c | 484 | |
929423fa JG |
485 | mutex_unlock(&balloon_mutex); |
486 | ||
487 | cond_resched(); | |
488 | ||
95d2ac4a | 489 | } while (credit && state == BP_DONE); |
1775826c JF |
490 | |
491 | /* Schedule more work if there is some still to be done. */ | |
95d2ac4a DK |
492 | if (state == BP_EAGAIN) |
493 | schedule_delayed_work(&balloon_worker, balloon_stats.schedule_delay * HZ); | |
1775826c JF |
494 | } |
495 | ||
496 | /* Resets the Xen limit, sets new target, and kicks off processing. */ | |
803eb047 | 497 | void balloon_set_new_target(unsigned long target) |
1775826c JF |
498 | { |
499 | /* No need for lock. Not read-modify-write updates. */ | |
1775826c | 500 | balloon_stats.target_pages = target; |
95170b2e | 501 | schedule_delayed_work(&balloon_worker, 0); |
1775826c | 502 | } |
803eb047 | 503 | EXPORT_SYMBOL_GPL(balloon_set_new_target); |
1775826c | 504 | |
b6f30679 KRW |
505 | /** |
506 | * alloc_xenballooned_pages - get pages that have been ballooned out | |
507 | * @nr_pages: Number of pages to get | |
508 | * @pages: pages returned | |
72e9cf2a | 509 | * @highmem: allow highmem pages |
b6f30679 KRW |
510 | * @return 0 on success, error otherwise |
511 | */ | |
693394b8 | 512 | int alloc_xenballooned_pages(int nr_pages, struct page **pages, bool highmem) |
1775826c | 513 | { |
b6f30679 | 514 | int pgno = 0; |
e882dc9c | 515 | struct page *page; |
b6f30679 KRW |
516 | mutex_lock(&balloon_mutex); |
517 | while (pgno < nr_pages) { | |
693394b8 | 518 | page = balloon_retrieve(highmem); |
72e9cf2a | 519 | if (page && (highmem || !PageHighMem(page))) { |
b6f30679 KRW |
520 | pages[pgno++] = page; |
521 | } else { | |
522 | enum bp_state st; | |
693394b8 SS |
523 | if (page) |
524 | balloon_append(page); | |
525 | st = decrease_reservation(nr_pages - pgno, | |
526 | highmem ? GFP_HIGHUSER : GFP_USER); | |
b6f30679 KRW |
527 | if (st != BP_DONE) |
528 | goto out_undo; | |
529 | } | |
1775826c | 530 | } |
b6f30679 KRW |
531 | mutex_unlock(&balloon_mutex); |
532 | return 0; | |
533 | out_undo: | |
534 | while (pgno) | |
535 | balloon_append(pages[--pgno]); | |
536 | /* Free the memory back to the kernel soon */ | |
537 | schedule_delayed_work(&balloon_worker, 0); | |
538 | mutex_unlock(&balloon_mutex); | |
539 | return -ENOMEM; | |
1775826c | 540 | } |
b6f30679 | 541 | EXPORT_SYMBOL(alloc_xenballooned_pages); |
1775826c | 542 | |
b6f30679 KRW |
543 | /** |
544 | * free_xenballooned_pages - return pages retrieved with get_ballooned_pages | |
545 | * @nr_pages: Number of pages | |
546 | * @pages: pages to return | |
547 | */ | |
e882dc9c | 548 | void free_xenballooned_pages(int nr_pages, struct page **pages) |
1775826c | 549 | { |
b6f30679 | 550 | int i; |
1775826c | 551 | |
b6f30679 | 552 | mutex_lock(&balloon_mutex); |
1775826c | 553 | |
b6f30679 KRW |
554 | for (i = 0; i < nr_pages; i++) { |
555 | if (pages[i]) | |
556 | balloon_append(pages[i]); | |
557 | } | |
558 | ||
559 | /* The balloon may be too large now. Shrink it if needed. */ | |
83be7e52 | 560 | if (current_credit()) |
b6f30679 | 561 | schedule_delayed_work(&balloon_worker, 0); |
1775826c | 562 | |
b6f30679 KRW |
563 | mutex_unlock(&balloon_mutex); |
564 | } | |
565 | EXPORT_SYMBOL(free_xenballooned_pages); | |
1775826c | 566 | |
8b5d44a5 DV |
567 | static void __init balloon_add_region(unsigned long start_pfn, |
568 | unsigned long pages) | |
1775826c | 569 | { |
4dfe22f5 | 570 | unsigned long pfn, extra_pfn_end; |
1775826c JF |
571 | struct page *page; |
572 | ||
8b5d44a5 DV |
573 | /* |
574 | * If the amount of usable memory has been limited (e.g., with | |
575 | * the 'mem' command line parameter), don't add pages beyond | |
576 | * this limit. | |
577 | */ | |
578 | extra_pfn_end = min(max_pfn, start_pfn + pages); | |
579 | ||
580 | for (pfn = start_pfn; pfn < extra_pfn_end; pfn++) { | |
581 | page = pfn_to_page(pfn); | |
582 | /* totalram_pages and totalhigh_pages do not | |
583 | include the boot-time balloon extension, so | |
584 | don't subtract from it. */ | |
585 | __balloon_append(page); | |
586 | } | |
de5a77d8 DV |
587 | |
588 | balloon_stats.total_pages += extra_pfn_end - start_pfn; | |
8b5d44a5 DV |
589 | } |
590 | ||
591 | static int __init balloon_init(void) | |
592 | { | |
0bb599fd | 593 | int i; |
8b5d44a5 | 594 | |
53d5522c | 595 | if (!xen_domain()) |
1775826c JF |
596 | return -ENODEV; |
597 | ||
283c0972 | 598 | pr_info("Initialising balloon driver\n"); |
1775826c | 599 | |
aa24411b DV |
600 | balloon_stats.current_pages = xen_pv_domain() |
601 | ? min(xen_start_info->nr_pages - xen_released_pages, max_pfn) | |
c275a57f | 602 | : get_num_physpages(); |
1775826c JF |
603 | balloon_stats.target_pages = balloon_stats.current_pages; |
604 | balloon_stats.balloon_low = 0; | |
605 | balloon_stats.balloon_high = 0; | |
de5a77d8 | 606 | balloon_stats.total_pages = balloon_stats.current_pages; |
1775826c | 607 | |
95d2ac4a DK |
608 | balloon_stats.schedule_delay = 1; |
609 | balloon_stats.max_schedule_delay = 32; | |
610 | balloon_stats.retry_count = 1; | |
40095de1 | 611 | balloon_stats.max_retry_count = RETRY_UNLIMITED; |
1775826c | 612 | |
080e2be7 | 613 | #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG |
080e2be7 DK |
614 | set_online_page_callback(&xen_online_page); |
615 | register_memory_notifier(&xen_memory_nb); | |
616 | #endif | |
617 | ||
2a4c92fa | 618 | /* |
b1cbf9b1 | 619 | * Initialize the balloon with pages from the extra memory |
8b5d44a5 | 620 | * regions (see arch/x86/xen/setup.c). |
2a4c92fa | 621 | */ |
8b5d44a5 | 622 | for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) |
626d7508 JG |
623 | if (xen_extra_mem[i].n_pfns) |
624 | balloon_add_region(xen_extra_mem[i].start_pfn, | |
625 | xen_extra_mem[i].n_pfns); | |
1775826c | 626 | |
1775826c JF |
627 | return 0; |
628 | } | |
629 | ||
630 | subsys_initcall(balloon_init); | |
631 | ||
1775826c | 632 | MODULE_LICENSE("GPL"); |