xen/balloon: only hotplug additional memory if required
[sfrench/cifs-2.6.git] / drivers / xen / balloon.c
index c79329fcfa78c722ba00a14f147aab1df2989a1c..ac6391bd8029c5836906f9cd8be1ecb2b6061896 100644 (file)
@@ -54,6 +54,7 @@
 #include <linux/memory.h>
 #include <linux/memory_hotplug.h>
 #include <linux/percpu-defs.h>
+#include <linux/slab.h>
 
 #include <asm/page.h>
 #include <asm/pgalloc.h>
  * balloon_process() state:
  *
  * BP_DONE: done or nothing to do,
+ * BP_WAIT: wait to be rescheduled,
  * BP_EAGAIN: error, go to sleep,
  * BP_ECANCELED: error, balloon operation canceled.
  */
 
 enum bp_state {
        BP_DONE,
+       BP_WAIT,
        BP_EAGAIN,
        BP_ECANCELED
 };
@@ -166,6 +169,9 @@ static struct page *balloon_next_page(struct page *page)
 
 static enum bp_state update_schedule(enum bp_state state)
 {
+       if (state == BP_WAIT)
+               return BP_WAIT;
+
        if (state == BP_ECANCELED)
                return BP_ECANCELED;
 
@@ -193,41 +199,66 @@ static enum bp_state update_schedule(enum bp_state state)
 }
 
 #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
-static long current_credit(void)
+static struct resource *additional_memory_resource(phys_addr_t size)
 {
-       return balloon_stats.target_pages - balloon_stats.current_pages -
-               balloon_stats.hotplug_pages;
+       struct resource *res;
+       int ret;
+
+       res = kzalloc(sizeof(*res), GFP_KERNEL);
+       if (!res)
+               return NULL;
+
+       res->name = "System RAM";
+       res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
+
+       ret = allocate_resource(&iomem_resource, res,
+                               size, 0, -1,
+                               PAGES_PER_SECTION * PAGE_SIZE, NULL, NULL);
+       if (ret < 0) {
+               pr_err("Cannot allocate new System RAM resource\n");
+               kfree(res);
+               return NULL;
+       }
+
+       return res;
 }
 
-static bool balloon_is_inflated(void)
+static void release_memory_resource(struct resource *resource)
 {
-       if (balloon_stats.balloon_low || balloon_stats.balloon_high ||
-                       balloon_stats.balloon_hotplug)
-               return true;
-       else
-               return false;
-}
+       if (!resource)
+               return;
 
-/*
- * reserve_additional_memory() adds memory region of size >= credit above
- * max_pfn. New region is section aligned and size is modified to be multiple
- * of section size. Those features allow optimal use of address space and
- * establish proper alignment when this function is called first time after
- * boot (last section not fully populated at boot time contains unused memory
- * pages with PG_reserved bit not set; online_pages_range() does not allow page
- * onlining in whole range if first onlined page does not have PG_reserved
- * bit set). Real size of added memory is established at page onlining stage.
- */
+       /*
+        * No need to reset region to identity mapped since we now
+        * know that no I/O can be in this region
+        */
+       release_resource(resource);
+       kfree(resource);
+}
 
-static enum bp_state reserve_additional_memory(long credit)
+static enum bp_state reserve_additional_memory(void)
 {
+       long credit;
+       struct resource *resource;
        int nid, rc;
-       u64 hotplug_start_paddr;
-       unsigned long balloon_hotplug = credit;
+       unsigned long balloon_hotplug;
+
+       credit = balloon_stats.target_pages - balloon_stats.total_pages;
+
+       /*
+        * Already hotplugged enough pages?  Wait for them to be
+        * onlined.
+        */
+       if (credit <= 0)
+               return BP_WAIT;
+
+       balloon_hotplug = round_up(credit, PAGES_PER_SECTION);
+
+       resource = additional_memory_resource(balloon_hotplug * PAGE_SIZE);
+       if (!resource)
+               goto err;
 
-       hotplug_start_paddr = PFN_PHYS(SECTION_ALIGN_UP(max_pfn));
-       balloon_hotplug = round_up(balloon_hotplug, PAGES_PER_SECTION);
-       nid = memory_add_physaddr_to_nid(hotplug_start_paddr);
+       nid = memory_add_physaddr_to_nid(resource->start);
 
 #ifdef CONFIG_XEN_HAVE_PVMMU
         /*
@@ -242,29 +273,28 @@ static enum bp_state reserve_additional_memory(long credit)
        if (!xen_feature(XENFEAT_auto_translated_physmap)) {
                unsigned long pfn, i;
 
-               pfn = PFN_DOWN(hotplug_start_paddr);
+               pfn = PFN_DOWN(resource->start);
                for (i = 0; i < balloon_hotplug; i++) {
                        if (!set_phys_to_machine(pfn + i, INVALID_P2M_ENTRY)) {
                                pr_warn("set_phys_to_machine() failed, no memory added\n");
-                               return BP_ECANCELED;
+                               goto err;
                        }
                 }
        }
 #endif
 
-       rc = add_memory(nid, hotplug_start_paddr, balloon_hotplug << PAGE_SHIFT);
-
+       rc = add_memory_resource(nid, resource);
        if (rc) {
                pr_warn("Cannot add additional memory (%i)\n", rc);
-               return BP_ECANCELED;
+               goto err;
        }
 
-       balloon_hotplug -= credit;
-
-       balloon_stats.hotplug_pages += credit;
-       balloon_stats.balloon_hotplug = balloon_hotplug;
+       balloon_stats.total_pages += balloon_hotplug;
 
-       return BP_DONE;
+       return BP_WAIT;
+  err:
+       release_memory_resource(resource);
+       return BP_ECANCELED;
 }
 
 static void xen_online_page(struct page *page)
@@ -275,11 +305,6 @@ static void xen_online_page(struct page *page)
 
        __balloon_append(page);
 
-       if (balloon_stats.hotplug_pages)
-               --balloon_stats.hotplug_pages;
-       else
-               --balloon_stats.balloon_hotplug;
-
        mutex_unlock(&balloon_mutex);
 }
 
@@ -296,32 +321,22 @@ static struct notifier_block xen_memory_nb = {
        .priority = 0
 };
 #else
-static long current_credit(void)
+static enum bp_state reserve_additional_memory(void)
 {
-       unsigned long target = balloon_stats.target_pages;
-
-       target = min(target,
-                    balloon_stats.current_pages +
-                    balloon_stats.balloon_low +
-                    balloon_stats.balloon_high);
-
-       return target - balloon_stats.current_pages;
+       balloon_stats.target_pages = balloon_stats.current_pages;
+       return BP_DONE;
 }
+#endif /* CONFIG_XEN_BALLOON_MEMORY_HOTPLUG */
 
-static bool balloon_is_inflated(void)
+static long current_credit(void)
 {
-       if (balloon_stats.balloon_low || balloon_stats.balloon_high)
-               return true;
-       else
-               return false;
+       return balloon_stats.target_pages - balloon_stats.current_pages;
 }
 
-static enum bp_state reserve_additional_memory(long credit)
+static bool balloon_is_inflated(void)
 {
-       balloon_stats.target_pages = balloon_stats.current_pages;
-       return BP_DONE;
+       return balloon_stats.balloon_low || balloon_stats.balloon_high;
 }
-#endif /* CONFIG_XEN_BALLOON_MEMORY_HOTPLUG */
 
 static enum bp_state increase_reservation(unsigned long nr_pages)
 {
@@ -334,15 +349,6 @@ static enum bp_state increase_reservation(unsigned long nr_pages)
                .domid        = DOMID_SELF
        };
 
-#ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
-       if (!balloon_stats.balloon_low && !balloon_stats.balloon_high) {
-               nr_pages = min(nr_pages, balloon_stats.balloon_hotplug);
-               balloon_stats.hotplug_pages += nr_pages;
-               balloon_stats.balloon_hotplug -= nr_pages;
-               return BP_DONE;
-       }
-#endif
-
        if (nr_pages > ARRAY_SIZE(frame_list))
                nr_pages = ARRAY_SIZE(frame_list);
 
@@ -405,15 +411,6 @@ static enum bp_state decrease_reservation(unsigned long nr_pages, gfp_t gfp)
                .domid        = DOMID_SELF
        };
 
-#ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
-       if (balloon_stats.hotplug_pages) {
-               nr_pages = min(nr_pages, balloon_stats.hotplug_pages);
-               balloon_stats.hotplug_pages -= nr_pages;
-               balloon_stats.balloon_hotplug += nr_pages;
-               return BP_DONE;
-       }
-#endif
-
        if (nr_pages > ARRAY_SIZE(frame_list))
                nr_pages = ARRAY_SIZE(frame_list);
 
@@ -492,7 +489,7 @@ static void balloon_process(struct work_struct *work)
                        if (balloon_is_inflated())
                                state = increase_reservation(credit);
                        else
-                               state = reserve_additional_memory(credit);
+                               state = reserve_additional_memory();
                }
 
                if (credit < 0)
@@ -602,6 +599,8 @@ static void __init balloon_add_region(unsigned long start_pfn,
                   don't subtract from it. */
                __balloon_append(page);
        }
+
+       balloon_stats.total_pages += extra_pfn_end - start_pfn;
 }
 
 static int __init balloon_init(void)
@@ -619,6 +618,7 @@ static int __init balloon_init(void)
        balloon_stats.target_pages  = balloon_stats.current_pages;
        balloon_stats.balloon_low   = 0;
        balloon_stats.balloon_high  = 0;
+       balloon_stats.total_pages   = balloon_stats.current_pages;
 
        balloon_stats.schedule_delay = 1;
        balloon_stats.max_schedule_delay = 32;
@@ -626,9 +626,6 @@ static int __init balloon_init(void)
        balloon_stats.max_retry_count = RETRY_UNLIMITED;
 
 #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
-       balloon_stats.hotplug_pages = 0;
-       balloon_stats.balloon_hotplug = 0;
-
        set_online_page_callback(&xen_online_page);
        register_memory_notifier(&xen_memory_nb);
 #endif