Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/geert/linux...
[pandora-kernel.git] / drivers / xen / balloon.c
index 5dfd8f8..5876e1a 100644 (file)
@@ -501,20 +501,24 @@ EXPORT_SYMBOL_GPL(balloon_set_new_target);
  * alloc_xenballooned_pages - get pages that have been ballooned out
  * @nr_pages: Number of pages to get
  * @pages: pages returned
+ * @highmem: highmem or lowmem pages
  * @return 0 on success, error otherwise
  */
-int alloc_xenballooned_pages(int nr_pages, struct page** pages)
+int alloc_xenballooned_pages(int nr_pages, struct page **pages, bool highmem)
 {
        int pgno = 0;
        struct page* page;
        mutex_lock(&balloon_mutex);
        while (pgno < nr_pages) {
-               page = balloon_retrieve(true);
-               if (page) {
+               page = balloon_retrieve(highmem);
+               if (page && PageHighMem(page) == highmem) {
                        pages[pgno++] = page;
                } else {
                        enum bp_state st;
-                       st = decrease_reservation(nr_pages - pgno, GFP_HIGHUSER);
+                       if (page)
+                               balloon_append(page);
+                       st = decrease_reservation(nr_pages - pgno,
+                                       highmem ? GFP_HIGHUSER : GFP_USER);
                        if (st != BP_DONE)
                                goto out_undo;
                }
@@ -555,17 +559,40 @@ void free_xenballooned_pages(int nr_pages, struct page** pages)
 }
 EXPORT_SYMBOL(free_xenballooned_pages);
 
-static int __init balloon_init(void)
+static void __init balloon_add_region(unsigned long start_pfn,
+                                     unsigned long pages)
 {
        unsigned long pfn, extra_pfn_end;
        struct page *page;
 
+       /*
+        * If the amount of usable memory has been limited (e.g., with
+        * the 'mem' command line parameter), don't add pages beyond
+        * this limit.
+        */
+       extra_pfn_end = min(max_pfn, start_pfn + pages);
+
+       for (pfn = start_pfn; pfn < extra_pfn_end; pfn++) {
+               page = pfn_to_page(pfn);
+               /* totalram_pages and totalhigh_pages do not
+                  include the boot-time balloon extension, so
+                  don't subtract from it. */
+               __balloon_append(page);
+       }
+}
+
+static int __init balloon_init(void)
+{
+       int i;
+
        if (!xen_domain())
                return -ENODEV;
 
        pr_info("xen/balloon: Initialising balloon driver.\n");
 
-       balloon_stats.current_pages = xen_pv_domain() ? min(xen_start_info->nr_pages, max_pfn) : max_pfn;
+       balloon_stats.current_pages = xen_pv_domain()
+               ? min(xen_start_info->nr_pages - xen_released_pages, max_pfn)
+               : max_pfn;
        balloon_stats.target_pages  = balloon_stats.current_pages;
        balloon_stats.balloon_low   = 0;
        balloon_stats.balloon_high  = 0;
@@ -584,24 +611,13 @@ static int __init balloon_init(void)
 #endif
 
        /*
-        * Initialise the balloon with excess memory space.  We need
-        * to make sure we don't add memory which doesn't exist or
-        * logically exist.  The E820 map can be trimmed to be smaller
-        * than the amount of physical memory due to the mem= command
-        * line parameter.  And if this is a 32-bit non-HIGHMEM kernel
-        * on a system with memory which requires highmem to access,
-        * don't try to use it.
+        * Initialize the balloon with pages from the extra memory
+        * regions (see arch/x86/xen/setup.c).
         */
-       extra_pfn_end = min(min(max_pfn, e820_end_of_ram_pfn()),
-                           (unsigned long)PFN_DOWN(xen_extra_mem_start + xen_extra_mem_size));
-       for (pfn = PFN_UP(xen_extra_mem_start);
-            pfn < extra_pfn_end;
-            pfn++) {
-               page = pfn_to_page(pfn);
-               /* totalram_pages and totalhigh_pages do not include the boot-time
-                  balloon extension, so don't subtract from it. */
-               __balloon_append(page);
-       }
+       for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++)
+               if (xen_extra_mem[i].size)
+                       balloon_add_region(PFN_UP(xen_extra_mem[i].start),
+                                          PFN_DOWN(xen_extra_mem[i].size));
 
        return 0;
 }