Merge tag 'for-linus-6.9-rc1-tag' of git://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-2.6.git] / arch / x86 / xen / enlighten_pvh.c
index 9e9db601bd52a9bf48a2e5ec4c936c0b7db93b39..27a2a02ef8fb1402657754ad5f1ae8c91c6a9d0e 100644 (file)
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <linux/acpi.h>
 #include <linux/export.h>
+#include <linux/mm.h>
 
 #include <xen/hvc-console.h>
 
@@ -73,3 +74,70 @@ void __init mem_map_via_hcall(struct boot_params *boot_params_p)
        }
        boot_params_p->e820_entries = memmap.nr_entries;
 }
+
+/*
+ * Reserve e820 UNUSABLE regions to inflate the memory balloon.
+ *
+ * On PVH dom0 the host memory map is used, RAM regions available to dom0 are
+ * located as the same place as in the native memory map, but since dom0 gets
+ * less memory than the total amount of host RAM the ranges that can't be
+ * populated are converted from RAM -> UNUSABLE.  Use such regions (up to the
+ * ratio signaled in EXTRA_MEM_RATIO) in order to inflate the balloon driver at
+ * boot.  Doing so prevents the guest (even if just temporary) from using holes
+ * in the memory map in order to map grants or foreign addresses, and
+ * hopefully limits the risk of a clash with a device MMIO region.  Ideally the
+ * hypervisor should notify us which memory ranges are suitable for creating
+ * foreign mappings, but that's not yet implemented.
+ */
+void __init xen_reserve_extra_memory(struct boot_params *bootp)
+{
+       unsigned int i, ram_pages = 0, extra_pages;
+
+       for (i = 0; i < bootp->e820_entries; i++) {
+               struct boot_e820_entry *e = &bootp->e820_table[i];
+
+               if (e->type != E820_TYPE_RAM)
+                       continue;
+               ram_pages += PFN_DOWN(e->addr + e->size) - PFN_UP(e->addr);
+       }
+
+       /* Max amount of extra memory. */
+       extra_pages = EXTRA_MEM_RATIO * ram_pages;
+
+       /*
+        * Convert UNUSABLE ranges to RAM and reserve them for foreign mapping
+        * purposes.
+        */
+       for (i = 0; i < bootp->e820_entries && extra_pages; i++) {
+               struct boot_e820_entry *e = &bootp->e820_table[i];
+               unsigned long pages;
+
+               if (e->type != E820_TYPE_UNUSABLE)
+                       continue;
+
+               pages = min(extra_pages,
+                       PFN_DOWN(e->addr + e->size) - PFN_UP(e->addr));
+
+               if (pages != (PFN_DOWN(e->addr + e->size) - PFN_UP(e->addr))) {
+                       struct boot_e820_entry *next;
+
+                       if (bootp->e820_entries ==
+                           ARRAY_SIZE(bootp->e820_table))
+                               /* No space left to split - skip region. */
+                               continue;
+
+                       /* Split entry. */
+                       next = e + 1;
+                       memmove(next, e,
+                               (bootp->e820_entries - i) * sizeof(*e));
+                       bootp->e820_entries++;
+                       next->addr = PAGE_ALIGN(e->addr) + PFN_PHYS(pages);
+                       e->size = next->addr - e->addr;
+                       next->size -= e->size;
+               }
+               e->type = E820_TYPE_RAM;
+               extra_pages -= pages;
+
+               xen_add_extra_mem(PFN_UP(e->addr), pages);
+       }
+}