39 #include <linux/kernel.h>
40 #include <linux/sched.h>
41 #include <linux/errno.h>
42 #include <linux/module.h>
48 #include <linux/list.h>
55 #include <asm/pgalloc.h>
56 #include <asm/pgtable.h>
59 #include <asm/xen/hypervisor.h>
60 #include <asm/xen/hypercall.h>
93 #define inc_totalhigh_pages() (totalhigh_pages++)
94 #define dec_totalhigh_pages() (totalhigh_pages--)
96 #define inc_totalhigh_pages() do {} while (0)
97 #define dec_totalhigh_pages() do {} while (0)
109 #define GFP_BALLOON \
110 (GFP_HIGHUSER | __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC)
114 #ifdef CONFIG_XEN_SCRUB_PAGES
115 clear_highpage(page);
120 static void __balloon_append(
struct page *
page)
123 if (PageHighMem(page)) {
127 list_add(&page->
lru, &ballooned_pages);
132 static void balloon_append(
struct page *page)
134 __balloon_append(page);
135 if (PageHighMem(page))
141 static struct page *balloon_retrieve(
bool prefer_highmem)
145 if (list_empty(&ballooned_pages))
154 if (PageHighMem(page)) {
165 static struct page *balloon_first_page(
void)
167 if (list_empty(&ballooned_pages))
172 static struct page *balloon_next_page(
struct page *page)
175 if (next == &ballooned_pages)
205 #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
206 static long current_credit(
void)
212 static bool balloon_is_inflated(
void)
232 static enum bp_state reserve_additional_memory(
long credit)
235 u64 hotplug_start_paddr;
236 unsigned long balloon_hotplug = credit;
239 balloon_hotplug =
round_up(balloon_hotplug, PAGES_PER_SECTION);
240 nid = memory_add_physaddr_to_nid(hotplug_start_paddr);
245 pr_info(
"xen_balloon: %s: add_memory() failed: %i\n", __func__, rc);
249 balloon_hotplug -= credit;
257 static void xen_online_page(
struct page *page)
259 __online_page_set_limits(page);
263 __balloon_append(page);
286 static long current_credit(
void)
298 static bool balloon_is_inflated(
void)
306 static enum bp_state reserve_additional_memory(
long credit)
313 static enum bp_state increase_reservation(
unsigned long nr_pages)
316 unsigned long pfn,
i;
324 #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
336 page = balloon_first_page();
337 for (i = 0; i < nr_pages; i++) {
343 page = balloon_next_page(page);
352 for (i = 0; i <
rc; i++) {
353 page = balloon_retrieve(
false);
373 ClearPageReserved(page);
374 init_page_count(page);
383 static enum bp_state decrease_reservation(
unsigned long nr_pages,
gfp_t gfp)
386 unsigned long pfn,
i;
395 #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
407 for (i = 0; i < nr_pages; i++) {
433 for (i = 0; i < nr_pages; i++) {
463 credit = current_credit();
466 if (balloon_is_inflated())
467 state = increase_reservation(credit);
469 state = reserve_additional_memory(credit);
473 state = decrease_reservation(-credit,
GFP_BALLOON);
475 state = update_schedule(state);
477 #ifndef CONFIG_PREEMPT
481 }
while (credit && state ==
BP_DONE);
511 while (pgno < nr_pages) {
512 page = balloon_retrieve(highmem);
513 if (page && (highmem || !PageHighMem(page))) {
514 pages[pgno++] =
page;
518 balloon_append(page);
519 st = decrease_reservation(nr_pages - pgno,
529 balloon_append(pages[--pgno]);
548 for (i = 0; i < nr_pages; i++) {
550 balloon_append(pages[i]);
554 if (current_credit())
561 static void __init balloon_add_region(
unsigned long start_pfn,
564 unsigned long pfn, extra_pfn_end;
572 extra_pfn_end =
min(
max_pfn, start_pfn + pages);
574 for (pfn = start_pfn; pfn < extra_pfn_end; pfn++) {
579 __balloon_append(page);
583 static int __init balloon_init(
void)
590 pr_info(
"xen/balloon: Initialising balloon driver.\n");
604 #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
608 set_online_page_callback(&xen_online_page);
617 if (xen_extra_mem[i].
size)