]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/xen/balloon.c
xen/balloon: find non-conflicting regions to place hotplugged memory
[mirror_ubuntu-bionic-kernel.git] / drivers / xen / balloon.c
CommitLineData
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
82enum bp_state {
83 BP_DONE,
84 BP_EAGAIN,
85 BP_ECANCELED
1775826c
JF
86};
87
1775826c 88
1775826c 89static DEFINE_MUTEX(balloon_mutex);
1775826c 90
803eb047
DDG
91struct balloon_stats balloon_stats;
92EXPORT_SYMBOL_GPL(balloon_stats);
1775826c
JF
93
94/* We increase/decrease in batches which fit in a page */
965c0aaa 95static 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. */
99static LIST_HEAD(ballooned_pages);
100
101/* Main work function, always executed in process context. */
102static void balloon_process(struct work_struct *work);
95170b2e 103static 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
110static 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 118static 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
130static 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 137static 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
160static 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 168static 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
DK
196#ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
197static long current_credit(void)
198{
199 return balloon_stats.target_pages - balloon_stats.current_pages -
200 balloon_stats.hotplug_pages;
201}
202
203static bool balloon_is_inflated(void)
204{
205 if (balloon_stats.balloon_low || balloon_stats.balloon_high ||
206 balloon_stats.balloon_hotplug)
207 return true;
208 else
209 return false;
210}
211
55b3da98
DV
212static struct resource *additional_memory_resource(phys_addr_t size)
213{
214 struct resource *res;
215 int ret;
216
217 res = kzalloc(sizeof(*res), GFP_KERNEL);
218 if (!res)
219 return NULL;
220
221 res->name = "System RAM";
222 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
223
224 ret = allocate_resource(&iomem_resource, res,
225 size, 0, -1,
226 PAGES_PER_SECTION * PAGE_SIZE, NULL, NULL);
227 if (ret < 0) {
228 pr_err("Cannot allocate new System RAM resource\n");
229 kfree(res);
230 return NULL;
231 }
232
233 return res;
234}
235
236static void release_memory_resource(struct resource *resource)
237{
238 if (!resource)
239 return;
240
241 /*
242 * No need to reset region to identity mapped since we now
243 * know that no I/O can be in this region
244 */
245 release_resource(resource);
246 kfree(resource);
247}
080e2be7
DK
248
249static enum bp_state reserve_additional_memory(long credit)
250{
55b3da98 251 struct resource *resource;
080e2be7 252 int nid, rc;
55b3da98 253 unsigned long balloon_hotplug;
080e2be7 254
55b3da98
DV
255 balloon_hotplug = round_up(credit, PAGES_PER_SECTION);
256
257 resource = additional_memory_resource(balloon_hotplug * PAGE_SIZE);
258 if (!resource)
259 goto err;
260
261 nid = memory_add_physaddr_to_nid(resource->start);
080e2be7 262
3c56b3a1
JG
263#ifdef CONFIG_XEN_HAVE_PVMMU
264 /*
265 * add_memory() will build page tables for the new memory so
266 * the p2m must contain invalid entries so the correct
267 * non-present PTEs will be written.
268 *
269 * If a failure occurs, the original (identity) p2m entries
270 * are not restored since this region is now known not to
271 * conflict with any devices.
272 */
273 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
274 unsigned long pfn, i;
275
55b3da98 276 pfn = PFN_DOWN(resource->start);
3c56b3a1
JG
277 for (i = 0; i < balloon_hotplug; i++) {
278 if (!set_phys_to_machine(pfn + i, INVALID_P2M_ENTRY)) {
279 pr_warn("set_phys_to_machine() failed, no memory added\n");
55b3da98 280 goto err;
3c56b3a1
JG
281 }
282 }
283 }
284#endif
285
55b3da98 286 rc = add_memory_resource(nid, resource);
080e2be7 287 if (rc) {
3dcf6367 288 pr_warn("Cannot add additional memory (%i)\n", rc);
55b3da98 289 goto err;
080e2be7
DK
290 }
291
292 balloon_hotplug -= credit;
293
294 balloon_stats.hotplug_pages += credit;
295 balloon_stats.balloon_hotplug = balloon_hotplug;
296
297 return BP_DONE;
55b3da98
DV
298 err:
299 release_memory_resource(resource);
300 return BP_ECANCELED;
080e2be7
DK
301}
302
303static void xen_online_page(struct page *page)
304{
305 __online_page_set_limits(page);
306
307 mutex_lock(&balloon_mutex);
308
309 __balloon_append(page);
310
311 if (balloon_stats.hotplug_pages)
312 --balloon_stats.hotplug_pages;
313 else
314 --balloon_stats.balloon_hotplug;
315
316 mutex_unlock(&balloon_mutex);
317}
318
319static int xen_memory_notifier(struct notifier_block *nb, unsigned long val, void *v)
320{
321 if (val == MEM_ONLINE)
322 schedule_delayed_work(&balloon_worker, 0);
323
324 return NOTIFY_OK;
325}
326
327static struct notifier_block xen_memory_nb = {
328 .notifier_call = xen_memory_notifier,
329 .priority = 0
330};
331#else
83be7e52 332static long current_credit(void)
1775826c 333{
bc2c0303 334 unsigned long target = balloon_stats.target_pages;
1775826c
JF
335
336 target = min(target,
337 balloon_stats.current_pages +
338 balloon_stats.balloon_low +
339 balloon_stats.balloon_high);
340
83be7e52 341 return target - balloon_stats.current_pages;
1775826c
JF
342}
343
080e2be7
DK
344static bool balloon_is_inflated(void)
345{
346 if (balloon_stats.balloon_low || balloon_stats.balloon_high)
347 return true;
348 else
349 return false;
350}
351
352static enum bp_state reserve_additional_memory(long credit)
353{
354 balloon_stats.target_pages = balloon_stats.current_pages;
355 return BP_DONE;
356}
357#endif /* CONFIG_XEN_BALLOON_MEMORY_HOTPLUG */
358
95d2ac4a 359static enum bp_state increase_reservation(unsigned long nr_pages)
1775826c 360{
95d2ac4a 361 int rc;
2f70e0ac 362 unsigned long pfn, i;
1775826c 363 struct page *page;
1775826c
JF
364 struct xen_memory_reservation reservation = {
365 .address_bits = 0,
366 .extent_order = 0,
367 .domid = DOMID_SELF
368 };
369
080e2be7
DK
370#ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
371 if (!balloon_stats.balloon_low && !balloon_stats.balloon_high) {
372 nr_pages = min(nr_pages, balloon_stats.balloon_hotplug);
373 balloon_stats.hotplug_pages += nr_pages;
374 balloon_stats.balloon_hotplug -= nr_pages;
375 return BP_DONE;
376 }
377#endif
378
1775826c
JF
379 if (nr_pages > ARRAY_SIZE(frame_list))
380 nr_pages = ARRAY_SIZE(frame_list);
381
9346c2a8 382 page = list_first_entry_or_null(&ballooned_pages, struct page, lru);
1775826c 383 for (i = 0; i < nr_pages; i++) {
95d2ac4a
DK
384 if (!page) {
385 nr_pages = i;
386 break;
387 }
a419aef8 388 frame_list[i] = page_to_pfn(page);
1775826c
JF
389 page = balloon_next_page(page);
390 }
391
a90971eb 392 set_xen_guest_handle(reservation.extent_start, frame_list);
fde28e8f
JF
393 reservation.nr_extents = nr_pages;
394 rc = HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation);
40095de1 395 if (rc <= 0)
95d2ac4a 396 return BP_EAGAIN;
1775826c 397
bc2c0303 398 for (i = 0; i < rc; i++) {
b6f30679 399 page = balloon_retrieve(false);
1775826c
JF
400 BUG_ON(page == NULL);
401
402 pfn = page_to_pfn(page);
1775826c 403
c2374bf5 404#ifdef CONFIG_XEN_HAVE_PVMMU
c1d15f5c
SS
405 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
406 set_phys_to_machine(pfn, frame_list[i]);
407
408 /* Link back into the page tables if not highmem. */
409 if (!PageHighMem(page)) {
410 int ret;
411 ret = HYPERVISOR_update_va_mapping(
412 (unsigned long)__va(pfn << PAGE_SHIFT),
413 mfn_pte(frame_list[i], PAGE_KERNEL),
414 0);
415 BUG_ON(ret);
416 }
1775826c 417 }
c2374bf5 418#endif
1775826c
JF
419
420 /* Relinquish the page back to the allocator. */
3dcc0571 421 __free_reserved_page(page);
1775826c
JF
422 }
423
bc2c0303 424 balloon_stats.current_pages += rc;
1775826c 425
95d2ac4a 426 return BP_DONE;
1775826c
JF
427}
428
b6f30679 429static enum bp_state decrease_reservation(unsigned long nr_pages, gfp_t gfp)
1775826c 430{
95d2ac4a 431 enum bp_state state = BP_DONE;
2f70e0ac 432 unsigned long pfn, i;
1775826c 433 struct page *page;
1775826c
JF
434 int ret;
435 struct xen_memory_reservation reservation = {
436 .address_bits = 0,
437 .extent_order = 0,
438 .domid = DOMID_SELF
439 };
440
080e2be7
DK
441#ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
442 if (balloon_stats.hotplug_pages) {
443 nr_pages = min(nr_pages, balloon_stats.hotplug_pages);
444 balloon_stats.hotplug_pages -= nr_pages;
445 balloon_stats.balloon_hotplug += nr_pages;
446 return BP_DONE;
447 }
448#endif
449
1775826c
JF
450 if (nr_pages > ARRAY_SIZE(frame_list))
451 nr_pages = ARRAY_SIZE(frame_list);
452
453 for (i = 0; i < nr_pages; i++) {
fce92683
LN
454 page = alloc_page(gfp);
455 if (page == NULL) {
1775826c 456 nr_pages = i;
95d2ac4a 457 state = BP_EAGAIN;
1775826c
JF
458 break;
459 }
09ed3d5b 460 scrub_page(page);
1775826c 461
09ed3d5b
WL
462 frame_list[i] = page_to_pfn(page);
463 }
1775826c 464
09ed3d5b
WL
465 /*
466 * Ensure that ballooned highmem pages don't have kmaps.
467 *
468 * Do this before changing the p2m as kmap_flush_unused()
469 * reads PTEs to obtain pages (and hence needs the original
470 * p2m entry).
471 */
472 kmap_flush_unused();
473
474 /* Update direct mapping, invalidate P2M, and add to balloon. */
475 for (i = 0; i < nr_pages; i++) {
476 pfn = frame_list[i];
0df4f266 477 frame_list[i] = pfn_to_gfn(pfn);
09ed3d5b 478 page = pfn_to_page(pfn);
1058a75f 479
c1d15f5c 480#ifdef CONFIG_XEN_HAVE_PVMMU
04660bb5 481 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
c1d15f5c
SS
482 if (!PageHighMem(page)) {
483 ret = HYPERVISOR_update_va_mapping(
484 (unsigned long)__va(pfn << PAGE_SHIFT),
0bb599fd 485 __pte_ma(0), 0);
c1d15f5c 486 BUG_ON(ret);
fb9a0c44
DV
487 }
488 __set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
04660bb5 489 }
c1d15f5c 490#endif
24f69373 491
09ed3d5b 492 balloon_append(page);
1775826c
JF
493 }
494
24f69373 495 flush_tlb_all();
2bad07ce 496
a90971eb 497 set_xen_guest_handle(reservation.extent_start, frame_list);
1775826c
JF
498 reservation.nr_extents = nr_pages;
499 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
500 BUG_ON(ret != nr_pages);
501
502 balloon_stats.current_pages -= nr_pages;
1775826c 503
95d2ac4a 504 return state;
1775826c
JF
505}
506
507/*
929423fa 508 * As this is a work item it is guaranteed to run as a single instance only.
1775826c
JF
509 * We may of course race updates of the target counts (which are protected
510 * by the balloon lock), or with changes to the Xen hard limit, but we will
511 * recover from these in time.
512 */
513static void balloon_process(struct work_struct *work)
514{
95d2ac4a 515 enum bp_state state = BP_DONE;
1775826c
JF
516 long credit;
517
1775826c
JF
518
519 do {
929423fa
JG
520 mutex_lock(&balloon_mutex);
521
83be7e52 522 credit = current_credit();
95d2ac4a 523
080e2be7
DK
524 if (credit > 0) {
525 if (balloon_is_inflated())
526 state = increase_reservation(credit);
527 else
528 state = reserve_additional_memory(credit);
529 }
95d2ac4a 530
1775826c 531 if (credit < 0)
b6f30679 532 state = decrease_reservation(-credit, GFP_BALLOON);
95d2ac4a
DK
533
534 state = update_schedule(state);
1775826c 535
929423fa
JG
536 mutex_unlock(&balloon_mutex);
537
538 cond_resched();
539
95d2ac4a 540 } while (credit && state == BP_DONE);
1775826c
JF
541
542 /* Schedule more work if there is some still to be done. */
95d2ac4a
DK
543 if (state == BP_EAGAIN)
544 schedule_delayed_work(&balloon_worker, balloon_stats.schedule_delay * HZ);
1775826c
JF
545}
546
547/* Resets the Xen limit, sets new target, and kicks off processing. */
803eb047 548void balloon_set_new_target(unsigned long target)
1775826c
JF
549{
550 /* No need for lock. Not read-modify-write updates. */
1775826c 551 balloon_stats.target_pages = target;
95170b2e 552 schedule_delayed_work(&balloon_worker, 0);
1775826c 553}
803eb047 554EXPORT_SYMBOL_GPL(balloon_set_new_target);
1775826c 555
b6f30679
KRW
556/**
557 * alloc_xenballooned_pages - get pages that have been ballooned out
558 * @nr_pages: Number of pages to get
559 * @pages: pages returned
72e9cf2a 560 * @highmem: allow highmem pages
b6f30679
KRW
561 * @return 0 on success, error otherwise
562 */
693394b8 563int alloc_xenballooned_pages(int nr_pages, struct page **pages, bool highmem)
1775826c 564{
b6f30679 565 int pgno = 0;
e882dc9c 566 struct page *page;
b6f30679
KRW
567 mutex_lock(&balloon_mutex);
568 while (pgno < nr_pages) {
693394b8 569 page = balloon_retrieve(highmem);
72e9cf2a 570 if (page && (highmem || !PageHighMem(page))) {
b6f30679
KRW
571 pages[pgno++] = page;
572 } else {
573 enum bp_state st;
693394b8
SS
574 if (page)
575 balloon_append(page);
576 st = decrease_reservation(nr_pages - pgno,
577 highmem ? GFP_HIGHUSER : GFP_USER);
b6f30679
KRW
578 if (st != BP_DONE)
579 goto out_undo;
580 }
1775826c 581 }
b6f30679
KRW
582 mutex_unlock(&balloon_mutex);
583 return 0;
584 out_undo:
585 while (pgno)
586 balloon_append(pages[--pgno]);
587 /* Free the memory back to the kernel soon */
588 schedule_delayed_work(&balloon_worker, 0);
589 mutex_unlock(&balloon_mutex);
590 return -ENOMEM;
1775826c 591}
b6f30679 592EXPORT_SYMBOL(alloc_xenballooned_pages);
1775826c 593
b6f30679
KRW
594/**
595 * free_xenballooned_pages - return pages retrieved with get_ballooned_pages
596 * @nr_pages: Number of pages
597 * @pages: pages to return
598 */
e882dc9c 599void free_xenballooned_pages(int nr_pages, struct page **pages)
1775826c 600{
b6f30679 601 int i;
1775826c 602
b6f30679 603 mutex_lock(&balloon_mutex);
1775826c 604
b6f30679
KRW
605 for (i = 0; i < nr_pages; i++) {
606 if (pages[i])
607 balloon_append(pages[i]);
608 }
609
610 /* The balloon may be too large now. Shrink it if needed. */
83be7e52 611 if (current_credit())
b6f30679 612 schedule_delayed_work(&balloon_worker, 0);
1775826c 613
b6f30679
KRW
614 mutex_unlock(&balloon_mutex);
615}
616EXPORT_SYMBOL(free_xenballooned_pages);
1775826c 617
8b5d44a5
DV
618static void __init balloon_add_region(unsigned long start_pfn,
619 unsigned long pages)
1775826c 620{
4dfe22f5 621 unsigned long pfn, extra_pfn_end;
1775826c
JF
622 struct page *page;
623
8b5d44a5
DV
624 /*
625 * If the amount of usable memory has been limited (e.g., with
626 * the 'mem' command line parameter), don't add pages beyond
627 * this limit.
628 */
629 extra_pfn_end = min(max_pfn, start_pfn + pages);
630
631 for (pfn = start_pfn; pfn < extra_pfn_end; pfn++) {
632 page = pfn_to_page(pfn);
633 /* totalram_pages and totalhigh_pages do not
634 include the boot-time balloon extension, so
635 don't subtract from it. */
636 __balloon_append(page);
637 }
638}
639
640static int __init balloon_init(void)
641{
0bb599fd 642 int i;
8b5d44a5 643
53d5522c 644 if (!xen_domain())
1775826c
JF
645 return -ENODEV;
646
283c0972 647 pr_info("Initialising balloon driver\n");
1775826c 648
aa24411b
DV
649 balloon_stats.current_pages = xen_pv_domain()
650 ? min(xen_start_info->nr_pages - xen_released_pages, max_pfn)
c275a57f 651 : get_num_physpages();
1775826c
JF
652 balloon_stats.target_pages = balloon_stats.current_pages;
653 balloon_stats.balloon_low = 0;
654 balloon_stats.balloon_high = 0;
1775826c 655
95d2ac4a
DK
656 balloon_stats.schedule_delay = 1;
657 balloon_stats.max_schedule_delay = 32;
658 balloon_stats.retry_count = 1;
40095de1 659 balloon_stats.max_retry_count = RETRY_UNLIMITED;
1775826c 660
080e2be7
DK
661#ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
662 balloon_stats.hotplug_pages = 0;
663 balloon_stats.balloon_hotplug = 0;
664
665 set_online_page_callback(&xen_online_page);
666 register_memory_notifier(&xen_memory_nb);
667#endif
668
2a4c92fa 669 /*
b1cbf9b1 670 * Initialize the balloon with pages from the extra memory
8b5d44a5 671 * regions (see arch/x86/xen/setup.c).
2a4c92fa 672 */
8b5d44a5 673 for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++)
626d7508
JG
674 if (xen_extra_mem[i].n_pfns)
675 balloon_add_region(xen_extra_mem[i].start_pfn,
676 xen_extra_mem[i].n_pfns);
1775826c 677
1775826c
JF
678 return 0;
679}
680
681subsys_initcall(balloon_init);
682
1775826c 683MODULE_LICENSE("GPL");