Skip to contentSkip to conversation

mem_sharing: fix sharability check during fork reset

PATCH Superseded Tamas K Lengyel 6 participants 12 messages Apr 23, 2020 – May 6, 2020

v17 of 19 Superseded Latest is v19 →

lore
Share & tools
lore
#0 lore raw

PATCH v17 1/2 mem_sharing: fix sharability check during fork reset

When resetting a VM fork we ought to only remove pages that were allocated for the fork during it's execution and the contents copied over from the parent. This can be determined if the page is sharable as special pages used by the fork for other purposes will not pass this test. Unfortunately during the fork reset loop we only partially check whether that's the case. A page's type may indicate it is sharable (pass p2m_is_sharable) but that's not a sufficient check by itself. All checks that are normally performed before a page is converted to the sharable type need to be performed to avoid removing pages from the p2m that may be used for other purposes. For example, currently the reset loop also removes the vcpu info pages from the p2m, potentially putting the guest into infinite page-fault loops.

---
v17: Changes based on feedback from Roger

---
 xen/arch/x86/mm/mem_sharing.c | 83 ++++++++++++++++++++---------------
 1 file changed, 47 insertions(+), 36 deletions(-)
xen/arch/x86/mm/mem_sharing.c +47 -36
index bb74595351..344a5bfb3d 100644
@@ -633,31 +633,33 @@ unsigned int mem_sharing_get_nr_shared_mfns(void)
 /* Functions that change a page's type and ownership */
 static int page_make_sharable(struct domain *d,
                               struct page_info *page,
-                              int expected_refcnt)
+                              unsigned int expected_refcnt,
+                              bool validate_only)
 {
-    bool_t drop_dom_ref;
+    int rc = 0;
+    bool drop_dom_ref = false;
 
-    spin_lock(&d->page_alloc_lock);
+    spin_lock_recursive(&d->page_alloc_lock);
 
     if ( d->is_dying )
     {
-        spin_unlock(&d->page_alloc_lock);
-        return -EBUSY;
+        rc = -EBUSY;
+        goto out;
     }
 
     /* Change page type and count atomically */
     if ( !get_page_and_type(page, d, PGT_shared_page) )
     {
-        spin_unlock(&d->page_alloc_lock);
-        return -EINVAL;
+        rc = -EINVAL;
+        goto out;
     }
 
     /* Check it wasn't already sharable and undo if it was */
     if ( (page->u.inuse.type_info & PGT_count_mask) != 1 )
     {
-        spin_unlock(&d->page_alloc_lock);
         put_page_and_type(page);
-        return -EEXIST;
+        rc = -EEXIST;
+        goto out;
     }
 
     /*
@@ -666,20 +668,26 @@ static int page_make_sharable(struct domain *d,
      */
     if ( page->count_info != (PGC_allocated | (2 + expected_refcnt)) )
     {
-        spin_unlock(&d->page_alloc_lock);
         /* Return type count back to zero */
         put_page_and_type(page);
-        return -E2BIG;
+        rc = -E2BIG;
+        goto out;
     }
 
-    page_set_owner(page, dom_cow);
-    drop_dom_ref = !domain_adjust_tot_pages(d, -1);
-    page_list_del(page, &d->page_list);
-    spin_unlock(&d->page_alloc_lock);
+    if ( !validate_only )
+    {
+        page_set_owner(page, dom_cow);
+        drop_dom_ref = !domain_adjust_tot_pages(d, -1);
+        page_list_del(page, &d->page_list);
+    }
+
+out:
+    spin_unlock_recursive(&d->page_alloc_lock);
 
     if ( drop_dom_ref )
         put_domain(d);
-    return 0;
+
+    return rc;
 }
 
 static int page_make_private(struct domain *d, struct page_info *page)
@@ -810,7 +818,8 @@ static int debug_gref(struct domain *d, grant_ref_t ref)
 }
 
 static int nominate_page(struct domain *d, gfn_t gfn,
-                         int expected_refcnt, shr_handle_t *phandle)
+                         unsigned int expected_refcnt, bool validate_only,
+                         shr_handle_t *phandle)
 {
     struct p2m_domain *hp2m = p2m_get_hostp2m(d);
     p2m_type_t p2mt;
@@ -879,8 +888,8 @@ static int nominate_page(struct domain *d, gfn_t gfn,
     }
 
     /* Try to convert the mfn to the sharable type */
-    ret = page_make_sharable(d, page, expected_refcnt);
-    if ( ret )
+    ret = page_make_sharable(d, page, expected_refcnt, validate_only);
+    if ( ret || validate_only )
         goto out;
 
     /*
@@ -1392,13 +1401,13 @@ static int range_share(struct domain *d, struct domain *cd,
          * We only break out if we run out of memory as individual pages may
          * legitimately be unsharable and we just want to skip over those.
          */
-        rc = nominate_page(d, _gfn(start), 0, &sh);
+        rc = nominate_page(d, _gfn(start), 0, false, &sh);
         if ( rc == -ENOMEM )
             break;
 
         if ( !rc )
         {
-            rc = nominate_page(cd, _gfn(start), 0, &ch);
+            rc = nominate_page(cd, _gfn(start), 0, false, &ch);
             if ( rc == -ENOMEM )
                 break;
 
@@ -1478,7 +1487,7 @@ int mem_sharing_fork_page(struct domain *d, gfn_t gfn, bool unsharing)
         /* For read-only accesses we just add a shared entry to the physmap */
         while ( parent )
         {
-            if ( !(rc = nominate_page(parent, gfn, 0, &handle)) )
+            if ( !(rc = nominate_page(parent, gfn, 0, false, &handle)) )
                 break;
 
             parent = parent->parent;
@@ -1775,20 +1784,22 @@ static int mem_sharing_fork_reset(struct domain *d, struct domain *pd)
     spin_lock_recursive(&d->page_alloc_lock);
     page_list_for_each_safe(page, tmp, &d->page_list)
     {
-        p2m_type_t p2mt;
-        p2m_access_t p2ma;
+        shr_handle_t sh;
         mfn_t mfn = page_to_mfn(page);
         gfn_t gfn = mfn_to_gfn(d, mfn);
 
-        mfn = __get_gfn_type_access(p2m, gfn_x(gfn), &p2mt, &p2ma,
-                                    0, NULL, false);
-
-        /* only reset pages that are sharable */
-        if ( !p2m_is_sharable(p2mt) )
-            continue;
-
-        /* take an extra reference or just skip if can't for whatever reason */
-        if ( !get_page(page, d) )
+        /*
+         * We only want to remove pages from the fork here that were copied
+         * from the parent but could be potentially re-populated using memory
+         * sharing after the reset. These pages all must be regular pages with
+         * no extra reference held to them, thus should be possible to make
+         * them sharable. Unfortunately p2m_is_sharable check is not sufficient
+         * to test this as it doesn't check the page's reference count. We thus
+         * check whether the page is convertable to the shared type using
+         * nominate_page. In case the page is already shared (ie. a share
+         * handle is returned) then we don't remove it.
+         */
+        if ( (rc = nominate_page(d, gfn, 0, true, &sh)) || sh )
             continue;
 
         /* forked memory is 4k, not splitting large pages so this must work */
@@ -1797,7 +1808,7 @@ static int mem_sharing_fork_reset(struct domain *d, struct domain *pd)
         ASSERT(!rc);
 
         put_page_alloc_ref(page);
-        put_page(page);
+        put_page_and_type(page);
     }
     spin_unlock_recursive(&d->page_alloc_lock);
 
@@ -1839,7 +1850,7 @@ int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg)
     {
         shr_handle_t handle;
 
-        rc = nominate_page(d, _gfn(mso.u.nominate.u.gfn), 0, &handle);
+        rc = nominate_page(d, _gfn(mso.u.nominate.u.gfn), 0, false, &handle);
         mso.u.nominate.handle = handle;
     }
     break;
@@ -1854,7 +1865,7 @@ int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg)
         if ( rc < 0 )
             goto out;
 
-        rc = nominate_page(d, gfn, 3, &handle);
+        rc = nominate_page(d, gfn, 3, false, &handle);
         mso.u.nominate.handle = handle;
     }
     break;
Signature · 1 line
2.20.1
#1 lore raw

PATCH v17 2/2 xen/tools: VM forking toolstack side

Add necessary bits to implement "xl fork-vm" commands. The command allows the user to specify how to launch the device model allowing for a late-launch model in which the user can execute the fork without the device model and decide to only later launch it.

---
 docs/man/xl.1.pod.in          |  49 +++++
 tools/libxc/include/xenctrl.h |  14 ++
 tools/libxc/xc_memshr.c       |  26 +++
 tools/libxl/libxl.h           |  12 ++
 tools/libxl/libxl_create.c    | 361 +++++++++++++++++++---------------
 tools/libxl/libxl_dm.c        |   2 +-
 tools/libxl/libxl_dom.c       |  43 +++-
 tools/libxl/libxl_internal.h  |   7 +
 tools/libxl/libxl_types.idl   |   1 +
 tools/libxl/libxl_x86.c       |  42 ++++
 tools/xl/Makefile             |   2 +-
 tools/xl/xl.h                 |   5 +
 tools/xl/xl_cmdtable.c        |  15 ++
 tools/xl/xl_forkvm.c          | 149 ++++++++++++++
 tools/xl/xl_vmcontrol.c       |  14 ++
 15 files changed, 576 insertions(+), 166 deletions(-)
 create mode 100644 tools/xl/xl_forkvm.c
docs/man/xl.1.pod.in +49 -0
index 09339282e6..67b4e8588a 100644
@@ -708,6 +708,55 @@ above).
 
 =back
 
+=item B<fork-vm> [I<OPTIONS>] I<domain-id>
+
+Create a fork of a running VM.  The domain will be paused after the operation
+and remains paused while forks of it exist.  Experimental and x86 only.
+Forks can only be made of domains with HAP enabled and on Intel hardware.  The
+parent domain must be created with the xl toolstack and its configuration must
+not manually define max_grant_frames, max_maptrack_frames or max_event_channels.
+
+B<OPTIONS>
+
+=over 4
+
+=item B<-p>
+
+Leave the fork paused after creating it.
+
+=item B<--launch-dm>
+
+Specify whether the device model (QEMU) should be launched for the fork. Late
+launch allows to start the device model for an already running fork.
+
+=item B<-C>
+
+The config file to use when launching the device model.  Currently required when
+launching the device model.  Most config settings MUST match the parent domain
+exactly, only change VM name, disk path and network configurations.
+
+=item B<-Q>
+
+The path to the qemu save file to use when launching the device model.  Currently
+required when launching the device model.
+
+=item B<--fork-reset>
+
+Perform a reset operation of an already running fork.  Note that resetting may
+be less performant then creating a new fork depending on how much memory the
+fork has deduplicated during its runtime.
+
+=item B<--max-vcpus>
+
+Specify the max-vcpus matching the parent domain when not launching the dm.
+
+=item B<--allow-iommu>
+
+Specify to allow forking a domain that has IOMMU enabled. Only compatible with
+forks using --launch-dm no.
+
+=back
+
 =item B<sharing> [I<domain-id>]
 
 Display the number of shared pages for a specified domain. If no domain is
tools/libxc/include/xenctrl.h +14 -0
index 5f25c5a6d4..0a6ff93229 100644
@@ -2232,6 +2232,20 @@ int xc_memshr_range_share(xc_interface *xch,
                           uint64_t first_gfn,
                           uint64_t last_gfn);
 
+int xc_memshr_fork(xc_interface *xch,
+                   uint32_t source_domain,
+                   uint32_t client_domain,
+                   bool allow_with_iommu);
+
+/*
+ * Note: this function is only intended to be used on short-lived forks that
+ * haven't yet aquired a lot of memory. In case the fork has a lot of memory
+ * it is likely more performant to create a new fork with xc_memshr_fork.
+ *
+ * With VMs that have a lot of memory this call may block for a long time.
+ */
+int xc_memshr_fork_reset(xc_interface *xch, uint32_t forked_domain);
+
 /* Debug calls: return the number of pages referencing the shared frame backing
  * the input argument. Should be one or greater.
  *
tools/libxc/xc_memshr.c +26 -0
index 97e2e6a8d9..2300cc7075 100644
@@ -239,6 +239,32 @@ int xc_memshr_debug_gref(xc_interface *xch,
     return xc_memshr_memop(xch, domid, &mso);
 }
 
+int xc_memshr_fork(xc_interface *xch, uint32_t pdomid, uint32_t domid,
+                   bool allow_with_iommu)
+{
+    xen_mem_sharing_op_t mso;
+
+    memset(&mso, 0, sizeof(mso));
+
+    mso.op = XENMEM_sharing_op_fork;
+    mso.u.fork.parent_domain = pdomid;
+
+    if ( allow_with_iommu )
+        mso.u.fork.flags |= XENMEM_FORK_WITH_IOMMU_ALLOWED;
+
+    return xc_memshr_memop(xch, domid, &mso);
+}
+
+int xc_memshr_fork_reset(xc_interface *xch, uint32_t domid)
+{
+    xen_mem_sharing_op_t mso;
+
+    memset(&mso, 0, sizeof(mso));
+    mso.op = XENMEM_sharing_op_fork_reset;
+
+    return xc_memshr_memop(xch, domid, &mso);
+}
+
 int xc_memshr_audit(xc_interface *xch)
 {
     xen_mem_sharing_op_t mso;
tools/libxl/libxl.h +12 -0
index 71709dc585..4bbb0a773d 100644
@@ -2666,6 +2666,18 @@ int libxl_psr_get_hw_info(libxl_ctx *ctx, libxl_psr_feat_type type,
                           unsigned int lvl, unsigned int *nr,
                           libxl_psr_hw_info **info);
 void libxl_psr_hw_info_list_free(libxl_psr_hw_info *list, unsigned int nr);
+
+int libxl_domain_fork_vm(libxl_ctx *ctx, uint32_t pdomid, uint32_t max_vcpus,
+                         bool allow_with_iommu, uint32_t *domid)
+                         LIBXL_EXTERNAL_CALLERS_ONLY;
+
+int libxl_domain_fork_launch_dm(libxl_ctx *ctx, libxl_domain_config *d_config,
+                                uint32_t domid,
+                                const libxl_asyncprogress_how *aop_console_how)
+                                LIBXL_EXTERNAL_CALLERS_ONLY;
+
+int libxl_domain_fork_reset(libxl_ctx *ctx, uint32_t domid)
+                            LIBXL_EXTERNAL_CALLERS_ONLY;
 #endif
 
 /* misc */
tools/libxl/libxl_create.c +204 -157

482 lines not on this page · raw message

tools/libxl/libxl_dm.c +1 -1
index f4007bbe50..b615f1fc88 100644
@@ -2803,7 +2803,7 @@ static void device_model_spawn_outcome(libxl__egc *egc,
 
     libxl__domain_build_state *state = dmss->build_state;
 
-    if (state->saved_state) {
+    if (state->saved_state && !state->forked_vm) {
         ret2 = unlink(state->saved_state);
         if (ret2) {
             LOGED(ERROR, dmss->guest_domid, "%s: failed to remove device-model state %s",
tools/libxl/libxl_dom.c +36 -7

105 lines not on this page · raw message

tools/libxl/libxl_internal.h +7 -0
index 5f39e44cb9..d05ff31e83 100644
@@ -1374,6 +1374,7 @@ typedef struct {
 
     char *saved_state;
     int dm_monitor_fd;
+    bool forked_vm;
 
     libxl__file_reference pv_kernel;
     libxl__file_reference pv_ramdisk;
@@ -4818,6 +4819,12 @@ _hidden int libxl__domain_pvcontrol(libxl__egc *egc,
 /* Check whether a domid is recent */
 int libxl__is_domid_recent(libxl__gc *gc, uint32_t domid, bool *recent);
 
+_hidden int libxl__do_domain_create(libxl_ctx *ctx, libxl_domain_config *d_config,
+                                    uint32_t *domid, int restore_fd, int send_back_fd,
+                                    const libxl_domain_restore_params *params,
+                                    const libxl_asyncop_how *ao_how,
+                                    const libxl_asyncprogress_how *aop_console_how);
+
 #endif
 
 /*
tools/libxl/libxl_types.idl +1 -0
index f7c473be74..2bb5e6319e 100644
@@ -958,6 +958,7 @@ libxl_domain_config = Struct("domain_config", [
     ("on_watchdog", libxl_action_on_shutdown),
     ("on_crash", libxl_action_on_shutdown),
     ("on_soft_reset", libxl_action_on_shutdown),
+    ("dm_restore_file", string, {'const': True}),
     ], dir=DIR_IN)
 
 libxl_diskinfo = Struct("diskinfo", [
tools/libxl/libxl_x86.c +42 -0

54 lines not on this page · raw message

tools/xl/Makefile +1 -1
index af4912e67a..073222233b 100644
@@ -15,7 +15,7 @@ LDFLAGS += $(PTHREAD_LDFLAGS)
 CFLAGS_XL += $(CFLAGS_libxenlight)
 CFLAGS_XL += -Wshadow
 
-XL_OBJS-$(CONFIG_X86) = xl_psr.o
+XL_OBJS-$(CONFIG_X86) = xl_psr.o xl_forkvm.o
 XL_OBJS = xl.o xl_cmdtable.o xl_sxp.o xl_utils.o $(XL_OBJS-y)
 XL_OBJS += xl_parse.o xl_cpupool.o xl_flask.o
 XL_OBJS += xl_vtpm.o xl_block.o xl_nic.o xl_usb.o
tools/xl/xl.h +5 -0
index 06569c6c4a..1105c34b15 100644
@@ -31,6 +31,7 @@ struct cmd_spec {
 };
 
 struct domain_create {
+    uint32_t ddomid; /* fork launch dm for this domid */
     int debug;
     int daemonize;
     int monitor; /* handle guest reboots etc */
@@ -45,6 +46,7 @@ struct domain_create {
     const char *config_file;
     char *extra_config; /* extra config string */
     const char *restore_file;
+    const char *dm_restore_file;
     char *colo_proxy_script;
     bool userspace_colo_proxy;
     int migrate_fd; /* -1 means none */
@@ -128,6 +130,8 @@ int main_pciassignable_remove(int argc, char **argv);
 int main_pciassignable_list(int argc, char **argv);
 #ifndef LIBXL_HAVE_NO_SUSPEND_RESUME
 int main_restore(int argc, char **argv);
+int main_fork_launch_dm(int argc, char **argv);
+int main_fork_reset(int argc, char **argv);
 int main_migrate_receive(int argc, char **argv);
 int main_save(int argc, char **argv);
 int main_migrate(int argc, char **argv);
@@ -212,6 +216,7 @@ int main_psr_cat_cbm_set(int argc, char **argv);
 int main_psr_cat_show(int argc, char **argv);
 int main_psr_mba_set(int argc, char **argv);
 int main_psr_mba_show(int argc, char **argv);
+int main_fork_vm(int argc, char **argv);
 #endif
 int main_qemu_monitor_command(int argc, char **argv);
 
tools/xl/xl_cmdtable.c +15 -0
index 08335394e5..ef634abf32 100644
@@ -187,6 +187,21 @@ struct cmd_spec cmd_table[] = {
       "Restore a domain from a saved state",
       "- for internal use only",
     },
+#if defined(__i386__) || defined(__x86_64__)
+    { "fork-vm",
+      &main_fork_vm, 0, 1,
+      "Fork a domain from the running parent domid. Experimental. Most config settings must match parent.",
+      "[options] <Domid>",
+      "-h                           Print this help.\n"
+      "-C <config>                  Use config file for VM fork.\n"
+      "-Q <qemu-save-file>          Use qemu save file for VM fork.\n"
+      "--launch-dm <yes|no|late>    Launch device model (QEMU) for VM fork.\n"
+      "--fork-reset                 Reset VM fork.\n"
+      "--max-vcpus                  Specify max-vcpus matching the parent domain when not launching dm\n"
+      "-p                           Do not unpause fork VM after operation.\n"
+      "-d                           Enable debug messages.\n"
+    },
+#endif
 #endif
     { "dump-core",
       &main_dump_core, 0, 1,
tools/xl/xl_forkvm.c new file +149 -0

149 lines not on this page · raw message

tools/xl/xl_vmcontrol.c +14 -0
index 17b4514c94..c64123d0a1 100644
@@ -676,6 +676,12 @@ int create_domain(struct domain_create *dom_info)
 
     int restoring = (restore_file || (migrate_fd >= 0));
 
+#if defined(__i386__) || defined(__x86_64__)
+    /* VM forking */
+    uint32_t ddomid = dom_info->ddomid; // launch dm for this domain iff set
+    const char *dm_restore_file = dom_info->dm_restore_file;
+#endif
+
     libxl_domain_config_init(&d_config);
 
     if (restoring) {
@@ -928,6 +934,14 @@ start:
          * restore/migrate-receive it again.
          */
         restoring = 0;
+#if defined(__i386__) || defined(__x86_64__)
+    } else if ( ddomid ) {
+        d_config.dm_restore_file = dm_restore_file;
+        ret = libxl_domain_fork_launch_dm(ctx, &d_config, ddomid,
+                                          autoconnect_console_how);
+        domid = ddomid;
+        ddomid = INVALID_DOMID;
+#endif
     } else if (domid_soft_reset != INVALID_DOMID) {
         /* Do soft reset. */
         ret = libxl_domain_soft_reset(ctx, &d_config, domid_soft_reset,
Signature · 1 line
2.20.1
#2 lore raw

Re: [PATCH v17 2/2] xen/tools: VM forking toolstack side

Quoted 6 lines · Tamas K Lengyel

Add necessary bits to implement "xl fork-vm" commands. The command allows the user to specify how to launch the device model allowing for a late-launch model in which the user can execute the fork without the device model and decide to only later launch it.

As before this will be left for the tool stack maintainers to ack and commit.

Jan

#3 lore raw

Re: [PATCH v17 2/2] xen/tools: VM forking toolstack side

In reply to Tamas K Lengyel
Quoted 7 lines · Tamas K Lengyel

Add necessary bits to implement "xl fork-vm" commands. The command allows the user to specify how to launch the device model allowing for a late-launch model in which the user can execute the fork without the device model and decide to only later launch it.

Patch ping. If nothing else at least the libxc parts would be nice to get merged before the freeze.

#4 lore raw

Re: [PATCH v17 2/2] xen/tools: VM forking toolstack side

Quoted 11 lines · Tamas K Lengyel
Quoted 7 lines · Tamas K Lengyel

Add necessary bits to implement "xl fork-vm" commands. The command allows the user to specify how to launch the device model allowing for a late-launch model in which the user can execute the fork without the device model and decide to only later launch it.

Patch ping. If nothing else at least the libxc parts would be nice to get merged before the freeze.

Changes to libxc looks good to me.

Please split it out to a patch with proper commit message.

Wei.

#5 lore raw

Re: [PATCH v17 2/2] xen/tools: VM forking toolstack side

Quoted 9 lines · Wei Liu
Quoted 7 lines · Tamas K Lengyel
Quoted 6 lines · Tamas K Lengyel

Add necessary bits to implement "xl fork-vm" commands. The command allows the user to specify how to launch the device model allowing for a late-launch model in which the user can execute the fork without the device model and decide to only later launch it.

Quoted 9 lines
Quoted 4 lines
Quoted 1 line

Patch ping. If nothing else at least the libxc parts would be nice to get merged before the freeze.

Changes to libxc looks good to me.

Please split it out to a patch with proper commit message.

Sounds good, will do.

Thanks,
Tamas

Reviewed-by on v17 1/2
#6 lore raw
In reply to Tamas K Lengyel
Quoted 12 lines · Tamas K Lengyel

When resetting a VM fork we ought to only remove pages that were allocated for the fork during it's execution and the contents copied over from the parent. This can be determined if the page is sharable as special pages used by the fork for other purposes will not pass this test. Unfortunately during the fork reset loop we only partially check whether that's the case. A page's type may indicate it is sharable (pass p2m_is_sharable) but that's not a sufficient check by itself. All checks that are normally performed before a page is converted to the sharable type need to be performed to avoid removing pages from the p2m that may be used for other purposes. For example, currently the reset loop also removes the vcpu info pages from the p2m, potentially putting the guest into infinite page-fault loops.

Quoted 1 line

I've been looking however and I'm not able to spot where you copy the shared_info data, which I think it's also quite critical for the domain, as it contains the info about event channels (when using L2). Also for HVM forks the shared info should be mapped at the same gfn as the parent, or else the child trying to access it will trigger an EPT fault that won't be able to populate such gfn, because the shared_info on the parent is already shared between Xen and the parent.

Thanks, Roger.

#7 lore raw
Quoted 14 lines · Roger Pau Monné
Quoted 12 lines · Tamas K Lengyel

When resetting a VM fork we ought to only remove pages that were allocated for the fork during it's execution and the contents copied over from the parent. This can be determined if the page is sharable as special pages used by the fork for other purposes will not pass this test. Unfortunately during the fork reset loop we only partially check whether that's the case. A page's type may indicate it is sharable (pass p2m_is_sharable) but that's not a sufficient check by itself. All checks that are normally performed before a page is converted to the sharable type need to be performed to avoid removing pages from the p2m that may be used for other purposes. For example, currently the reset loop also removes the vcpu info pages from the p2m, potentially putting the guest into infinite page-fault loops.

Quoted 3 lines
Quoted 1 line

Thanks!

Quoted 8 lines

I've been looking however and I'm not able to spot where you copy the shared_info data, which I think it's also quite critical for the domain, as it contains the info about event channels (when using L2). Also for HVM forks the shared info should be mapped at the same gfn as the parent, or else the child trying to access it will trigger an EPT fault that won't be able to populate such gfn, because the shared_info on the parent is already shared between Xen and the parent.

Pages that cause an EPT fault but can't be made shared get a new page allocated for them and copied in mem_sharing_fork_page. There are many pages like that, mostly due to QEMU mapping them and thus holding an extra reference. That said, shared_info is manually copied during forking in copy_special_pages, at the end of the function you will find:

old_mfn = _mfn(virt_to_mfn(d->shared_info));
new_mfn = _mfn(virt_to_mfn(cd->shared_info));

copy_domain_page(new_mfn, old_mfn);

Cheers,
Tamas

#8 lore raw
Quoted 37 lines · Tamas K Lengyel
Quoted 14 lines · Roger Pau Monné
Quoted 12 lines · Tamas K Lengyel

When resetting a VM fork we ought to only remove pages that were allocated for the fork during it's execution and the contents copied over from the parent. This can be determined if the page is sharable as special pages used by the fork for other purposes will not pass this test. Unfortunately during the fork reset loop we only partially check whether that's the case. A page's type may indicate it is sharable (pass p2m_is_sharable) but that's not a sufficient check by itself. All checks that are normally performed before a page is converted to the sharable type need to be performed to avoid removing pages from the p2m that may be used for other purposes. For example, currently the reset loop also removes the vcpu info pages from the p2m, potentially putting the guest into infinite page-fault loops.

Quoted 3 lines
Quoted 1 line

Thanks!

Quoted 8 lines

I've been looking however and I'm not able to spot where you copy the shared_info data, which I think it's also quite critical for the domain, as it contains the info about event channels (when using L2). Also for HVM forks the shared info should be mapped at the same gfn as the parent, or else the child trying to access it will trigger an EPT fault that won't be able to populate such gfn, because the shared_info on the parent is already shared between Xen and the parent.

Pages that cause an EPT fault but can't be made shared get a new page allocated for them and copied in mem_sharing_fork_page. There are many pages like that, mostly due to QEMU mapping them and thus holding an extra reference. That said, shared_info is manually copied during forking in copy_special_pages, at the end of the function you will find:

Quoted 4 lines

old_mfn = _mfn(virt_to_mfn(d->shared_info));
new_mfn = _mfn(virt_to_mfn(cd->shared_info));

copy_domain_page(new_mfn, old_mfn);

Oh, sorry for the noise, I somehow missed it.

Roger.

#9 lore raw
In reply to Tamas K Lengyel
Quoted 37 lines · Tamas K Lengyel
Quoted 14 lines · Roger Pau Monné
Quoted 12 lines · Tamas K Lengyel

When resetting a VM fork we ought to only remove pages that were allocated for the fork during it's execution and the contents copied over from the parent. This can be determined if the page is sharable as special pages used by the fork for other purposes will not pass this test. Unfortunately during the fork reset loop we only partially check whether that's the case. A page's type may indicate it is sharable (pass p2m_is_sharable) but that's not a sufficient check by itself. All checks that are normally performed before a page is converted to the sharable type need to be performed to avoid removing pages from the p2m that may be used for other purposes. For example, currently the reset loop also removes the vcpu info pages from the p2m, potentially putting the guest into infinite page-fault loops.

Quoted 3 lines
Quoted 1 line

Thanks!

Quoted 8 lines

I've been looking however and I'm not able to spot where you copy the shared_info data, which I think it's also quite critical for the domain, as it contains the info about event channels (when using L2). Also for HVM forks the shared info should be mapped at the same gfn as the parent, or else the child trying to access it will trigger an EPT fault that won't be able to populate such gfn, because the shared_info on the parent is already shared between Xen and the parent.

Pages that cause an EPT fault but can't be made shared get a new page allocated for them and copied in mem_sharing_fork_page. There are many pages like that, mostly due to QEMU mapping them and thus holding an extra reference. That said, shared_info is manually copied during forking in copy_special_pages, at the end of the function you will find:

Quoted 4 lines

old_mfn = _mfn(virt_to_mfn(d->shared_info));
new_mfn = _mfn(virt_to_mfn(cd->shared_info));

copy_domain_page(new_mfn, old_mfn);

Yes, that's indeed fine, you need to copy the contents of the shared info page, but for HVM you also need to make sure the shared info page is mapped at the same gfn as the parent. HVM guest issue a hypercall to request the mapping of the shared info page to a specific gfn, since it's not added to the guest physmap by default. XENMAPSPACE_shared_info is used in order to request the shared info page to be mapped at a specific gfn.

AFAICT during fork such shared info mapping is not replicated to the child, and hence the child trying to access the gfn of the shared info page would just result in EPT faults that won't be fixed (because the parent shared info page cannot be shared with the child)?

You should be able to use get_gpfn_from_mfn in order to get the parent gfn where the shared info mfn is mapped (if any), and then replicate this in the child using it's own shared info.

On fork reset you should check if the child shared info gfn != parent shared info gfn and reinstate the parent state if different from the child.

Roger.

#10 lore raw
Quoted 57 lines · Roger Pau Monné
Quoted 37 lines · Tamas K Lengyel
Quoted 14 lines · Roger Pau Monné
Quoted 12 lines · Tamas K Lengyel

When resetting a VM fork we ought to only remove pages that were allocated for the fork during it's execution and the contents copied over from the parent. This can be determined if the page is sharable as special pages used by the fork for other purposes will not pass this test. Unfortunately during the fork reset loop we only partially check whether that's the case. A page's type may indicate it is sharable (pass p2m_is_sharable) but that's not a sufficient check by itself. All checks that are normally performed before a page is converted to the sharable type need to be performed to avoid removing pages from the p2m that may be used for other purposes. For example, currently the reset loop also removes the vcpu info pages from the p2m, potentially putting the guest into infinite page-fault loops.

Quoted 3 lines
Quoted 1 line

Thanks!

Quoted 8 lines

I've been looking however and I'm not able to spot where you copy the shared_info data, which I think it's also quite critical for the domain, as it contains the info about event channels (when using L2). Also for HVM forks the shared info should be mapped at the same gfn as the parent, or else the child trying to access it will trigger an EPT fault that won't be able to populate such gfn, because the shared_info on the parent is already shared between Xen and the parent.

Pages that cause an EPT fault but can't be made shared get a new page allocated for them and copied in mem_sharing_fork_page. There are many pages like that, mostly due to QEMU mapping them and thus holding an extra reference. That said, shared_info is manually copied during forking in copy_special_pages, at the end of the function you will find:

Quoted 4 lines

old_mfn = _mfn(virt_to_mfn(d->shared_info));
new_mfn = _mfn(virt_to_mfn(cd->shared_info));

copy_domain_page(new_mfn, old_mfn);

Yes, that's indeed fine, you need to copy the contents of the shared info page, but for HVM you also need to make sure the shared info page is mapped at the same gfn as the parent. HVM guest issue a hypercall to request the mapping of the shared info page to a specific gfn, since it's not added to the guest physmap by default. XENMAPSPACE_shared_info is used in order to request the shared info page to be mapped at a specific gfn.

AFAICT during fork such shared info mapping is not replicated to the child, and hence the child trying to access the gfn of the shared info page would just result in EPT faults that won't be fixed (because the parent shared info page cannot be shared with the child)?

Quoted 7 lines

You should be able to use get_gpfn_from_mfn in order to get the parent gfn where the shared info mfn is mapped (if any), and then replicate this in the child using it's own shared info.

On fork reset you should check if the child shared info gfn != parent shared info gfn and reinstate the parent state if different from the child.

OK, I see what you mean. In the way things set up currently the EPT fault-loop problem doesn't happen since the page does get copied, it just gets copied to a new mfn not the one d->shared_info points to. So whatever issue that may bring it must be subtle because we haven't noticed any instability.

Also, looking at the save/restore code in libxc it seems to me that shared_info is actually a PV specific page and it doesn't get saved/restored with an HVM domain. The hvm code paths don't process REC_TYPE_SHARED_INFO at all. So since forks are exclusively for HVM domains, do we really need it and if so, why doesn't HVM save/restore need it?

Tamas

#11 lore raw
Quoted 66 lines · Tamas K Lengyel
Quoted 57 lines · Roger Pau Monné
Quoted 37 lines · Tamas K Lengyel
Quoted 14 lines · Roger Pau Monné
Quoted 12 lines · Tamas K Lengyel

When resetting a VM fork we ought to only remove pages that were allocated for the fork during it's execution and the contents copied over from the parent. This can be determined if the page is sharable as special pages used by the fork for other purposes will not pass this test. Unfortunately during the fork reset loop we only partially check whether that's the case. A page's type may indicate it is sharable (pass p2m_is_sharable) but that's not a sufficient check by itself. All checks that are normally performed before a page is converted to the sharable type need to be performed to avoid removing pages from the p2m that may be used for other purposes. For example, currently the reset loop also removes the vcpu info pages from the p2m, potentially putting the guest into infinite page-fault loops.

Quoted 3 lines
Quoted 1 line

Thanks!

Quoted 8 lines

I've been looking however and I'm not able to spot where you copy the shared_info data, which I think it's also quite critical for the domain, as it contains the info about event channels (when using L2). Also for HVM forks the shared info should be mapped at the same gfn as the parent, or else the child trying to access it will trigger an EPT fault that won't be able to populate such gfn, because the shared_info on the parent is already shared between Xen and the parent.

Pages that cause an EPT fault but can't be made shared get a new page allocated for them and copied in mem_sharing_fork_page. There are many pages like that, mostly due to QEMU mapping them and thus holding an extra reference. That said, shared_info is manually copied during forking in copy_special_pages, at the end of the function you will find:

Quoted 4 lines

old_mfn = _mfn(virt_to_mfn(d->shared_info));
new_mfn = _mfn(virt_to_mfn(cd->shared_info));

copy_domain_page(new_mfn, old_mfn);

Yes, that's indeed fine, you need to copy the contents of the shared info page, but for HVM you also need to make sure the shared info page is mapped at the same gfn as the parent. HVM guest issue a hypercall to request the mapping of the shared info page to a specific gfn, since it's not added to the guest physmap by default. XENMAPSPACE_shared_info is used in order to request the shared info page to be mapped at a specific gfn.

AFAICT during fork such shared info mapping is not replicated to the child, and hence the child trying to access the gfn of the shared info page would just result in EPT faults that won't be fixed (because the parent shared info page cannot be shared with the child)?

Quoted 7 lines

You should be able to use get_gpfn_from_mfn in order to get the parent gfn where the shared info mfn is mapped (if any), and then replicate this in the child using it's own shared info.

On fork reset you should check if the child shared info gfn != parent shared info gfn and reinstate the parent state if different from the child.

Quoted 5 lines

OK, I see what you mean. In the way things set up currently the EPT fault-loop problem doesn't happen since the page does get copied, it just gets copied to a new mfn not the one d->shared_info points to. So whatever issue that may bring it must be subtle because we haven't noticed any instability.

In any case I think it would be good if you could add such mapping on domain fork and reset, as you already partially handle the shared info page by copying the data from the parent into the child. Or at least add a FIXME comment to note the fact that a child trying to access the shared info page won't work.

Quoted 6 lines

Also, looking at the save/restore code in libxc it seems to me that shared_info is actually a PV specific page and it doesn't get saved/restored with an HVM domain. The hvm code paths don't process REC_TYPE_SHARED_INFO at all. So since forks are exclusively for HVM domains, do we really need it and if so, why doesn't HVM save/restore need it?

HVM domains on suspend/resume are aware of the need to re-instate the shared info mapping, as it's part of the protocol and the guest is actively cooperating on such actions. The same happens with the vcpu info pages or the PV timers for example: they are not saved during a suspend/resume and the guest needs to setup them again on restore.

Fork however is completely transparent from a guest PoV, and hence there's no way to signal the child it needs to setup the shared info mapping (or any other mapping or interface FWIW).

Roger.