]> xenbits.xensource.com Git - xen.git/commitdiff
x86+libxl: correct p2m (shadow) memory pool size calculation
authorJan Beulich <jbeulich@suse.com>
Thu, 28 Apr 2022 08:00:49 +0000 (10:00 +0200)
committerJan Beulich <jbeulich@suse.com>
Thu, 28 Apr 2022 08:00:49 +0000 (10:00 +0200)
The reference "to shadow the resident processes" is applicable to
domains (potentially) running in shadow mode only. Adjust the
calculations accordingly. This, however, requires further parameters.
Since the original function is deprecated anyway, and since it can't be
changed (for being part of a stable ABI), introduce a new (internal
only) function, with the deprecated one simply becoming a wrapper.

In dom0_paging_pages() also take the opportunity and stop open-coding
DIV_ROUND_UP().

Signed-off-by: Jan Beulich <jbeulich@suse.com>
Reviewed-by: Roger Pau Monné <roger.pau@citrix.com>
Reviewed-by: Anthony PERARD <anthony.perard@citrix.com>
tools/libs/light/libxl_create.c
tools/libs/light/libxl_internal.h
tools/libs/light/libxl_utils.c
xen/arch/x86/dom0_build.c

index 885675591feedc6cac917635efc387170f2bc842..69ec405858a8932dd263f24bd5ad318bfa17180a 100644 (file)
@@ -1027,6 +1027,24 @@ static bool ok_to_default_memkb_in_create(libxl__gc *gc)
      */
 }
 
+unsigned long libxl__get_required_paging_memory(unsigned long maxmem_kb,
+                                                unsigned int smp_cpus,
+                                                libxl_domain_type type,
+                                                bool hap)
+{
+    /*
+     * 256 pages (1MB) per vcpu,
+     * plus 1 page per MiB of RAM for the P2M map (for non-PV guests),
+     * plus 1 page per MiB of RAM to shadow the resident processes (for shadow
+     * mode guests).
+     * This is higher than the minimum that Xen would allocate if no value
+     * were given (but the Xen minimum is for safety, not performance).
+     */
+    return 4 * (256 * smp_cpus +
+                ((type != LIBXL_DOMAIN_TYPE_PV) + !hap) *
+                (maxmem_kb / 1024));
+}
+
 static unsigned long libxl__get_required_iommu_memory(unsigned long maxmem_kb)
 {
     unsigned long iommu_pages = 0, mem_pages = maxmem_kb / 4;
@@ -1194,10 +1212,16 @@ int libxl__domain_config_setdefault(libxl__gc *gc,
     }
 
     if (d_config->b_info.shadow_memkb == LIBXL_MEMKB_DEFAULT
-        && ok_to_default_memkb_in_create(gc))
+        && ok_to_default_memkb_in_create(gc)) {
+        bool hap = d_config->c_info.type != LIBXL_DOMAIN_TYPE_PV &&
+                   libxl_defbool_val(d_config->c_info.hap);
+
         d_config->b_info.shadow_memkb =
-            libxl_get_required_shadow_memory(d_config->b_info.max_memkb,
-                                             d_config->b_info.max_vcpus);
+            libxl__get_required_paging_memory(d_config->b_info.max_memkb,
+                                              d_config->b_info.max_vcpus,
+                                              d_config->c_info.type,
+                                              hap);
+    }
 
     /* No IOMMU reservation is needed if passthrough mode is not 'sync_pt' */
     if (d_config->b_info.iommu_memkb == LIBXL_MEMKB_DEFAULT
index a26daec04095157dc4e6f5dfa615c87e124df524..bdef5a605ea952225ee5d2a871f66e128362abc0 100644 (file)
@@ -1569,6 +1569,11 @@ _hidden int libxl__domain_need_memory_calculate(libxl__gc *gc,
                                       libxl_domain_build_info *b_info,
                                       uint64_t *need_memkb);
 
+_hidden unsigned long libxl__get_required_paging_memory(unsigned long maxmem_kb,
+                                                        unsigned int smp_cpus,
+                                                        libxl_domain_type type,
+                                                        bool hap);
+
 _hidden const char *libxl__device_nic_devname(libxl__gc *gc,
                                               uint32_t domid,
                                               uint32_t devid,
index b91c2cafa223fe4912617a497ad6641a9d3c3603..1d8a7f64ef4ab8bc718d45c899c9f11449e53d51 100644 (file)
@@ -38,13 +38,8 @@ char *libxl_basename(const char *name)
 
 unsigned long libxl_get_required_shadow_memory(unsigned long maxmem_kb, unsigned int smp_cpus)
 {
-    /* 256 pages (1MB) per vcpu,
-       plus 1 page per MiB of RAM for the P2M map,
-       plus 1 page per MiB of RAM to shadow the resident processes.
-       This is higher than the minimum that Xen would allocate if no value
-       were given (but the Xen minimum is for safety, not performance).
-     */
-    return 4 * (256 * smp_cpus + 2 * (maxmem_kb / 1024));
+    return libxl__get_required_paging_memory(maxmem_kb, smp_cpus,
+                                             LIBXL_DOMAIN_TYPE_INVALID, false);
 }
 
 char *libxl_domid_to_name(libxl_ctx *ctx, uint32_t domid)
index 4d1c5c60e407576632a858a00251b2f523ba9208..79234f18ff01b7491f1b8079bc6e8ea918acb721 100644 (file)
@@ -314,12 +314,15 @@ unsigned int __initdata dom0_memflags = MEMF_no_dma|MEMF_exact_node;
 unsigned long __init dom0_paging_pages(const struct domain *d,
                                        unsigned long nr_pages)
 {
-    /* Copied from: libxl_get_required_shadow_memory() */
+    /* Keep in sync with libxl__get_required_paging_memory(). */
     unsigned long memkb = nr_pages * (PAGE_SIZE / 1024);
 
-    memkb = 4 * (256 * d->max_vcpus + 2 * (memkb / 1024));
+    memkb = 4 * (256 * d->max_vcpus +
+                 (is_pv_domain(d) ? opt_dom0_shadow || opt_pv_l1tf_hwdom
+                                  : 1 + opt_dom0_shadow) *
+                 (memkb / 1024));
 
-    return ((memkb + 1023) / 1024) << (20 - PAGE_SHIFT);
+    return DIV_ROUND_UP(memkb, 1024) << (20 - PAGE_SHIFT);
 }