Skip to contentSkip to conversation

VM forking

PATCH Superseded Tamas K Lengyel 8 participants 45 messages Feb 21, 2020 – Feb 25, 2020

v9 of 19 Superseded Latest is v19 →

lore
Share & tools
lore
#14 lore raw

PATCH v9 3/5 xen/mem_sharing: VM forking

In reply to Tamas K Lengyel

VM forking is the process of creating a domain with an empty memory space and a parent domain specified from which to populate the memory when necessary. For the new domain to be functional the VM state is copied over as part of the fork operation (HVM params, hap allocation, etc).

---
v9: remove stale header
fix tsc incarnation being bumped on set

---
 xen/arch/x86/domain.c             |  11 ++
 xen/arch/x86/hvm/hvm.c            |   2 +-
 xen/arch/x86/mm/mem_sharing.c     | 222 ++++++++++++++++++++++++++++++
 xen/arch/x86/mm/p2m.c             |  11 +-
 xen/include/asm-x86/mem_sharing.h |  17 +++
 xen/include/public/memory.h       |   5 +
 xen/include/xen/sched.h           |   2 +
 7 files changed, 268 insertions(+), 2 deletions(-)
xen/arch/x86/domain.c +11 -0
index fe63c23676..1ab0ca0942 100644
@@ -2203,6 +2203,17 @@ int domain_relinquish_resources(struct domain *d)
             ret = relinquish_shared_pages(d);
             if ( ret )
                 return ret;
+
+            /*
+             * If the domain is forked, decrement the parent's pause count
+             * and release the domain.
+             */
+            if ( d->parent )
+            {
+                domain_unpause(d->parent);
+                put_domain(d->parent);
+                d->parent = NULL;
+            }
         }
 #endif
 
xen/arch/x86/hvm/hvm.c +1 -1
index a339b36a0d..9bfa603f8c 100644
@@ -1915,7 +1915,7 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
     }
 #endif
 
-    /* Spurious fault? PoD and log-dirty also take this path. */
+    /* Spurious fault? PoD, log-dirty and VM forking also take this path. */
     if ( p2m_is_ram(p2mt) )
     {
         rc = 1;
xen/arch/x86/mm/mem_sharing.c +222 -0

246 lines not on this page · raw message

xen/arch/x86/mm/p2m.c +10 -1
index c5f428d67c..7c4d2fd7a0 100644
@@ -509,6 +509,14 @@ mfn_t __get_gfn_type_access(struct p2m_domain *p2m, unsigned long gfn_l,
 
     mfn = p2m->get_entry(p2m, gfn, t, a, q, page_order, NULL);
 
+    /* Check if we need to fork the page */
+    if ( (q & P2M_ALLOC) && p2m_is_hole(*t) &&
+         !mem_sharing_fork_page(p2m->domain, gfn, !!(q & P2M_UNSHARE)) )
+    {
+        mfn = p2m->get_entry(p2m, gfn, t, a, q, page_order, NULL);
+    }
+
+    /* Check if we need to unshare the page */
     if ( (q & P2M_UNSHARE) && p2m_is_shared(*t) )
     {
         ASSERT(p2m_is_hostp2m(p2m));
@@ -588,7 +596,8 @@ struct page_info *p2m_get_page_from_gfn(
             return page;
 
         /* Error path: not a suitable GFN at all */
-        if ( !p2m_is_ram(*t) && !p2m_is_paging(*t) && !p2m_is_pod(*t) )
+        if ( !p2m_is_ram(*t) && !p2m_is_paging(*t) && !p2m_is_pod(*t) &&
+             !mem_sharing_is_fork(p2m->domain) )
             return NULL;
     }
 
xen/include/asm-x86/mem_sharing.h +17 -0
index 53760a2896..ac968fae3f 100644
@@ -39,6 +39,9 @@ struct mem_sharing_domain
 
 #define mem_sharing_enabled(d) ((d)->arch.hvm.mem_sharing.enabled)
 
+#define mem_sharing_is_fork(d) \
+    (mem_sharing_enabled(d) && !!((d)->parent))
+
 /* Auditing of memory sharing code? */
 #ifndef NDEBUG
 #define MEM_SHARING_AUDIT 1
@@ -88,6 +91,9 @@ static inline int mem_sharing_unshare_page(struct domain *d,
     return rc;
 }
 
+int mem_sharing_fork_page(struct domain *d, gfn_t gfn,
+                          bool unsharing);
+
 /*
  * If called by a foreign domain, possible errors are
  *   -EBUSY -> ring full
@@ -117,6 +123,7 @@ int relinquish_shared_pages(struct domain *d);
 #else
 
 #define mem_sharing_enabled(d) false
+#define mem_sharing_is_fork(p2m) false
 
 static inline unsigned int mem_sharing_get_nr_saved_mfns(void)
 {
@@ -141,6 +148,16 @@ static inline int mem_sharing_notify_enomem(struct domain *d, unsigned long gfn,
     return -EOPNOTSUPP;
 }
 
+static inline int mem_sharing_fork(struct domain *d, struct domain *cd, bool vcpu)
+{
+    return -EOPNOTSUPP;
+}
+
+static inline int mem_sharing_fork_page(struct domain *d, gfn_t gfn, bool lock)
+{
+    return -EOPNOTSUPP;
+}
+
 #endif
 
 #endif /* __MEM_SHARING_H__ */
xen/include/public/memory.h +5 -0
index 126d0ff06e..24106c6c2f 100644
@@ -482,6 +482,7 @@ DEFINE_XEN_GUEST_HANDLE(xen_mem_access_op_t);
 #define XENMEM_sharing_op_add_physmap       6
 #define XENMEM_sharing_op_audit             7
 #define XENMEM_sharing_op_range_share       8
+#define XENMEM_sharing_op_fork              9
 
 #define XENMEM_SHARING_OP_S_HANDLE_INVALID  (-10)
 #define XENMEM_SHARING_OP_C_HANDLE_INVALID  (-9)
@@ -532,6 +533,10 @@ struct xen_mem_sharing_op {
                 uint32_t gref;     /* IN: gref to debug         */
             } u;
         } debug;
+        struct mem_sharing_op_fork {
+            domid_t parent_domain;
+            uint16_t _pad[3];                /* Must be set to 0 */
+        } fork;
     } u;
 };
 typedef struct xen_mem_sharing_op xen_mem_sharing_op_t;
xen/include/xen/sched.h +2 -0
index 3a4f43098c..d98e999fcc 100644
@@ -503,6 +503,8 @@ struct domain
     /* Memory sharing support */
 #ifdef CONFIG_MEM_SHARING
     struct vm_event_domain *vm_event_share;
+    struct domain *parent; /* VM fork parent */
+    bool parent_paused;
 #endif
     /* Memory paging support */
 #ifdef CONFIG_HAS_MEM_PAGING
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#30 lore raw

PATCH v9 4/5 x86/mem_sharing: reset a fork

In reply to Tamas K Lengyel

Implement hypercall that allows a fork to shed all memory that got allocated for it during its execution and re-load its vCPU context from the parent VM. This allows the forked VM to reset into the same state the parent VM is in a faster way then creating a new fork would be. Measurements show about a 2x speedup during normal fuzzing operations. Performance may vary depending how much memory got allocated for the forked VM. If it has been completely deduplicated from the parent VM then creating a new fork would likely be more performant.

---
 xen/arch/x86/mm/mem_sharing.c | 76 +++++++++++++++++++++++++++++++++++
 xen/include/public/memory.h   |  1 +
 2 files changed, 77 insertions(+)
xen/arch/x86/mm/mem_sharing.c +76 -0
index ad5db9d8d5..fb6892aaa6 100644
@@ -1636,6 +1636,59 @@ static int mem_sharing_fork(struct domain *d, struct domain *cd)
     return rc;
 }
 
+/*
+ * The fork reset operation is intended to be used on short-lived forks only.
+ * There is no hypercall continuation operation implemented for this reason.
+ * For forks that obtain a larger memory footprint it is likely going to be
+ * more performant to create a new fork instead of resetting an existing one.
+ *
+ * TODO: In case this hypercall would become useful on forks with larger memory
+ * footprints the hypercall continuation should be implemented.
+ */
+static int mem_sharing_fork_reset(struct domain *d, struct domain *cd)
+{
+    int rc;
+    struct p2m_domain* p2m = p2m_get_hostp2m(cd);
+    struct page_info *page, *tmp;
+
+    domain_pause(cd);
+
+    page_list_for_each_safe(page, tmp, &cd->page_list)
+    {
+        p2m_type_t p2mt;
+        p2m_access_t p2ma;
+        gfn_t gfn;
+        mfn_t mfn = page_to_mfn(page);
+
+        if ( !mfn_valid(mfn) )
+            continue;
+
+        gfn = mfn_to_gfn(cd, mfn);
+        mfn = __get_gfn_type_access(p2m, gfn_x(gfn), &p2mt, &p2ma,
+                                    0, NULL, false);
+
+        if ( !p2m_is_ram(p2mt) || p2m_is_shared(p2mt) )
+            continue;
+
+        /* take an extra reference */
+        if ( !get_page(page, cd) )
+            continue;
+
+        rc = p2m->set_entry(p2m, gfn, INVALID_MFN, PAGE_ORDER_4K,
+                            p2m_invalid, p2m_access_rwx, -1);
+        ASSERT(!rc);
+
+        put_page_alloc_ref(page);
+        put_page(page);
+    }
+
+    if ( !(rc = hvm_copy_context_and_params(cd, d)) )
+        fork_tsc(cd, d);
+
+    domain_unpause(cd);
+    return rc;
+}
+
 int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg)
 {
     int rc;
@@ -1920,6 +1973,29 @@ int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg)
         break;
     }
 
+    case XENMEM_sharing_op_fork_reset:
+    {
+        struct domain *pd;
+
+        rc = -EINVAL;
+        if ( mso.u.fork._pad[0] || mso.u.fork._pad[1] ||
+             mso.u.fork._pad[2] )
+            goto out;
+
+        rc = -ENOSYS;
+        if ( !d->parent )
+            goto out;
+
+        rc = rcu_lock_live_remote_domain_by_id(d->parent->domain_id, &pd);
+        if ( rc )
+            goto out;
+
+        rc = mem_sharing_fork_reset(pd, d);
+
+        rcu_unlock_domain(pd);
+        break;
+    }
+
     default:
         rc = -ENOSYS;
         break;
xen/include/public/memory.h +1 -0
index 24106c6c2f..defab3a5bc 100644
@@ -483,6 +483,7 @@ DEFINE_XEN_GUEST_HANDLE(xen_mem_access_op_t);
 #define XENMEM_sharing_op_audit             7
 #define XENMEM_sharing_op_range_share       8
 #define XENMEM_sharing_op_fork              9
+#define XENMEM_sharing_op_fork_reset        10
 
 #define XENMEM_SHARING_OP_S_HANDLE_INVALID  (-10)
 #define XENMEM_SHARING_OP_C_HANDLE_INVALID  (-9)
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

PATCH v9 1/5 xen/x86: Make hap_get_allocation accessible

In reply to Tamas K Lengyel

During VM forking we'll copy the parent domain's parameters to the client, including the HAP shadow memory setting that is used for storing the domain's EPT. We'll copy this in the hypervisor instead doing it during toolstack launch to allow the domain to start executing and unsharing memory before (or even completely without) the toolstack.

---
 xen/arch/x86/mm/hap/hap.c | 3 +--
 xen/include/asm-x86/hap.h | 1 +
 2 files changed, 2 insertions(+), 2 deletions(-)
xen/arch/x86/mm/hap/hap.c +1 -2
index 3d93f3451c..c7c7ff6e99 100644
@@ -321,8 +321,7 @@ static void hap_free_p2m_page(struct domain *d, struct page_info *pg)
 }
 
 /* Return the size of the pool, rounded up to the nearest MB */
-static unsigned int
-hap_get_allocation(struct domain *d)
+unsigned int hap_get_allocation(struct domain *d)
 {
     unsigned int pg = d->arch.paging.hap.total_pages
         + d->arch.paging.hap.p2m_pages;
xen/include/asm-x86/hap.h +1 -0
index b94bfb4ed0..1bf07e49fe 100644
@@ -45,6 +45,7 @@ int   hap_track_dirty_vram(struct domain *d,
 
 extern const struct paging_mode *hap_paging_get_mode(struct vcpu *);
 int hap_set_allocation(struct domain *d, unsigned int pages, bool *preempted);
+unsigned int hap_get_allocation(struct domain *d);
 
 #endif /* XEN_HAP_H */
 
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#0 lore raw

v9 cover VM forking

The following series implements VM forking for Intel HVM guests to allow for the fast creation of identical VMs without the assosciated high startup costs of booting or restoring the VM from a savefile.

JIRA issue: https://xenproject.atlassian.net/browse/XEN-89

The fork operation is implemented as part of the "xl fork-vm" command:
xl fork-vm -C <config_file_for_fork> -Q <qemu_save_file> <parent_domid>

By default a fully functional fork is created. The user is in charge however to create the appropriate config file for the fork and to generate the QEMU save file before the fork-vm call is made. The config file needs to give the fork a new name at minimum but other settings may also require changes.

The interface also allows to split the forking into two steps:

    xl fork-vm --launch-dm no \
               -p <parent_domid>
    xl fork-vm --launch-dm late \
               -C <config_file_for_fork> \
               -Q <qemu_save_file> \
               <fork_domid>

The split creation model is useful when the VM needs to be created as fast as possible. The forked VM can be unpaused without the device model being launched to be monitored and accessed via VMI. Note however that without its device model running (depending on what is executing in the VM) it is bound to misbehave or even crash when its trying to access devices that would be emulated by QEMU. We anticipate that for certain use-cases this would be an acceptable situation, in case for example when fuzzing is performed of code segments that don't access such devices.

Launching the device model requires the QEMU Xen savefile to be generated manually from the parent VM. This can be accomplished simply by connecting to its QMP socket and issuing the "xen-save-devices-state" command. For example using the standard tool socat these commands can be used to generate the file:

    socat - UNIX-CONNECT:/var/run/xen/qmp-libxl-<parent_domid>
    { "execute": "qmp_capabilities" }
    { "execute": "xen-save-devices-state", \
        "arguments": { "filename": "/path/to/save/qemu_state", \
                        "live": false} }

At runtime the forked VM starts running with an empty p2m which gets lazily populated when the VM generates EPT faults, similar to how altp2m views are populated. If the memory access is a read-only access, the p2m entry is populated with a memory shared entry with its parent. For write memory accesses or in case memory sharing wasn't possible (for example in case a reference is held by a third party), a new page is allocated and the page contents are copied over from the parent VM. Forks can be further forked if needed, thus allowing for further memory savings.

A VM fork reset hypercall is also added that allows the fork to be reset to the state it was just after a fork, also accessible via xl:

    xl fork-vm --fork-reset -p <fork_domid>

This is an optimization for cases where the forks are very short-lived and run without a device model, so resetting saves some time compared to creating a brand new fork provided the fork has not aquired a lot of memory. If the fork has a lot of memory deduplicated it is likely going to be faster to create a new fork from scratch and asynchronously destroying the old one.

The series has been tested with both Linux and Windows VMs and functions as expected. VM forking time has been measured to be 0.0007s, device model launch to be around 1s depending largely on the number of devices being emulated. Fork resets have been measured to be 0.0001s under the optimal circumstances.

New in v9:
Patch 2
Rebased on staging and minor fixes for things pointed out by Andrew

Patch 1 exposes a hap internal function that will be used during VM forking

Patch 2 extends the createdomain domctl with an extra used during VM forking

Patch 3-4 implements the VM fork & reset operation hypervisor side bits

Patch 5 adds the toolstack-side code implementing VM forking and reset

Tamas K Lengyel (5):
xen/x86: Make hap_get_allocation accessible
xen: add parent_domid field to createdomain domctl xen/mem_sharing: VM forking x86/mem_sharing: reset a fork
xen/tools: VM forking toolstack side

 docs/man/xl.1.pod.in              |  36 ++++
 tools/libxc/include/xenctrl.h     |  13 ++
 tools/libxc/xc_memshr.c           |  22 +++
 tools/libxl/libxl.h               |   7 +
 tools/libxl/libxl_create.c        | 256 ++++++++++++++++---------
 tools/libxl/libxl_dm.c            |   2 +-
 tools/libxl/libxl_dom.c           |  43 ++++-
 tools/libxl/libxl_internal.h      |   1 +
 tools/libxl/libxl_types.idl       |   1 +
 tools/xl/xl.h                     |   5 +
 tools/xl/xl_cmdtable.c            |  12 ++
 tools/xl/xl_saverestore.c         |  97 ++++++++++
 tools/xl/xl_vmcontrol.c           |   8 +
 xen/arch/x86/domain.c             |  11 ++
 xen/arch/x86/hvm/hvm.c            |   2 +-
 xen/arch/x86/mm/hap/hap.c         |   3 +-
 xen/arch/x86/mm/mem_sharing.c     | 298 ++++++++++++++++++++++++++++++
 xen/arch/x86/mm/p2m.c             |  11 +-
 xen/common/domctl.c               |  14 ++
 xen/include/asm-x86/hap.h         |   1 +
 xen/include/asm-x86/mem_sharing.h |  17 ++
 xen/include/public/domctl.h       |   3 +-
 xen/include/public/memory.h       |   6 +
 xen/include/xen/sched.h           |   2 +
 24 files changed, 772 insertions(+), 99 deletions(-)
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#5 lore raw

PATCH v9 2/5 xen: add parent_domid field to createdomain domctl

When creating a domain that will be used as a VM fork some information is required to set things up properly, like the max_vcpus count. Instead of the toolstack having to gather this information for each fork in a separate hypercall we can just include the parent domain's id in the createdomain domctl so that Xen can copy the setting without the extra toolstack queries.

---
 xen/common/domctl.c         | 14 ++++++++++++++
 xen/include/public/domctl.h |  3 ++-
 2 files changed, 16 insertions(+), 1 deletion(-)
xen/common/domctl.c +14 -0
index a69b3b59a8..22aceb3860 100644
@@ -489,6 +489,7 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
     case XEN_DOMCTL_createdomain:
     {
         domid_t        dom;
+        domid_t        parent_dom;
         static domid_t rover = 0;
 
         dom = op->domain;
@@ -515,6 +516,19 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
             rover = dom;
         }
 
+        parent_dom = op->u.createdomain.parent_domid;
+        if ( parent_dom )
+        {
+            struct domain *pd = rcu_lock_domain_by_id(parent_dom);
+
+            ret = -EINVAL;
+            if ( !pd )
+                break;
+
+            op->u.createdomain.max_vcpus = pd->max_vcpus;
+            rcu_unlock_domain(pd);
+        }
+
         d = domain_create(dom, &op->u.createdomain, false);
         if ( IS_ERR(d) )
         {
xen/include/public/domctl.h +2 -1
index fec6f6fdd1..251cc40ef6 100644
@@ -38,7 +38,7 @@
 #include "hvm/save.h"
 #include "memory.h"
 
-#define XEN_DOMCTL_INTERFACE_VERSION 0x00000012
+#define XEN_DOMCTL_INTERFACE_VERSION 0x00000013
 
 /*
  * NB. xen_domctl.domain is an IN/OUT parameter for this operation.
@@ -92,6 +92,7 @@ struct xen_domctl_createdomain {
     uint32_t max_evtchn_port;
     int32_t max_grant_frames;
     int32_t max_maptrack_frames;
+    domid_t parent_domid;
 
     struct xen_arch_domainconfig arch;
 };
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#40 lore raw

PATCH v9 5/5 xen/tools: VM forking toolstack side

In reply to 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.

---
 docs/man/xl.1.pod.in          |  36 +++++
 tools/libxc/include/xenctrl.h |  13 ++
 tools/libxc/xc_memshr.c       |  22 +++
 tools/libxl/libxl.h           |   7 +
 tools/libxl/libxl_create.c    | 256 ++++++++++++++++++++++------------
 tools/libxl/libxl_dm.c        |   2 +-
 tools/libxl/libxl_dom.c       |  43 +++++-
 tools/libxl/libxl_internal.h  |   1 +
 tools/libxl/libxl_types.idl   |   1 +
 tools/xl/xl.h                 |   5 +
 tools/xl/xl_cmdtable.c        |  12 ++
 tools/xl/xl_saverestore.c     |  97 +++++++++++++
 tools/xl/xl_vmcontrol.c       |   8 ++
 13 files changed, 409 insertions(+), 94 deletions(-)
docs/man/xl.1.pod.in +36 -0
index 33ad2ebd71..c4012939f5 100644
@@ -694,6 +694,42 @@ Leave the domain paused after creating the snapshot.
 
 =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 needs to remain paused while forks of it exist.
+
+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.
+
+=item B<-Q>
+
+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.
+
+=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 +13 -0
index 99552a5f73..90fce83196 100644
@@ -2225,6 +2225,19 @@ 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);
+
+/*
+ * 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 +22 -0
index 97e2e6a8d9..d0e4ee225b 100644
@@ -239,6 +239,28 @@ 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)
+{
+    xen_mem_sharing_op_t mso;
+
+    memset(&mso, 0, sizeof(mso));
+
+    mso.op = XENMEM_sharing_op_fork;
+    mso.u.fork.parent_domain = pdomid;
+
+    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 +7 -0
index fde8548847..dcdabe8f80 100644
@@ -1538,6 +1538,13 @@ int libxl_domain_create_new(libxl_ctx *ctx, libxl_domain_config *d_config,
                             const libxl_asyncop_how *ao_how,
                             const libxl_asyncprogress_how *aop_console_how)
                             LIBXL_EXTERNAL_CALLERS_ONLY;
+int libxl_domain_fork_vm(libxl_ctx *ctx, uint32_t pdomid, 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);
 int libxl_domain_create_restore(libxl_ctx *ctx, libxl_domain_config *d_config,
                                 uint32_t *domid, int restore_fd,
                                 int send_back_fd,
tools/libxl/libxl_create.c +170 -86

365 lines not on this page · raw message

tools/libxl/libxl_dm.c +1 -1
index 3b1da90167..87ae1478cf 100644
@@ -2787,7 +2787,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 +1 -0
index 4936446069..d7a7ce163d 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;
tools/libxl/libxl_types.idl +1 -0
index 7921950f6a..7c4c4057a9 100644
@@ -956,6 +956,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/xl/xl.h +5 -0
index 60bdad8ffb..9bdad6526e 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 */
@@ -127,6 +129,9 @@ 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_vm(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);
tools/xl/xl_cmdtable.c +12 -0
index 3b302b2f20..3a5d371057 100644
@@ -185,6 +185,18 @@ struct cmd_spec cmd_table[] = {
       "Restore a domain from a saved state",
       "- for internal use only",
     },
+    { "fork-vm",
+      &main_fork_vm, 0, 1,
+      "Fork a domain from the running parent domid",
+      "[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"
+      "-p                           Do not unpause fork VM after operation.\n"
+      "-d                           Enable debug messages.\n"
+    },
 #endif
     { "dump-core",
       &main_dump_core, 0, 1,
tools/xl/xl_saverestore.c +97 -0

103 lines not on this page · raw message

tools/xl/xl_vmcontrol.c +8 -0
index e520b1da79..d9cb19c599 100644
@@ -645,6 +645,7 @@ int create_domain(struct domain_create *dom_info)
 
     libxl_domain_config d_config;
 
+    uint32_t ddomid = dom_info->ddomid; // launch dm for this domain iff set
     int debug = dom_info->debug;
     int daemonize = dom_info->daemonize;
     int monitor = dom_info->monitor;
@@ -655,6 +656,7 @@ int create_domain(struct domain_create *dom_info)
     const char *restore_file = dom_info->restore_file;
     const char *config_source = NULL;
     const char *restore_source = NULL;
+    const char *dm_restore_file = dom_info->dm_restore_file;
     int migrate_fd = dom_info->migrate_fd;
     bool config_in_json;
 
@@ -923,6 +925,12 @@ start:
          * restore/migrate-receive it again.
          */
         restoring = 0;
+    } 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;
     } else if (domid_soft_reset != INVALID_DOMID) {
         /* Do soft reset. */
         ret = libxl_domain_soft_reset(ctx, &d_config, domid_soft_reset,
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#6 lore raw

Re: [Xen-devel] [PATCH v9 2/5] xen: add parent_domid field to createdomain domctl

In reply to Tamas K Lengyel

Hi Tamas,

Quoted 5 lines · Tamas K Lengyel

When creating a domain that will be used as a VM fork some information is required to set things up properly, like the max_vcpus count. Instead of the toolstack having to gather this information for each fork in a separate hypercall we can just include the parent domain's id in the createdomain domctl so that Xen can copy the setting without the extra toolstack queries.

It is not entirely clear why you only want to copy max_vcpus. From my understanding, when you are going to fork a domain you will want the domain to be nearly identical. So how do you decide what to copy?

Quoted 20 lines
---
  xen/common/domctl.c         | 14 ++++++++++++++
  xen/include/public/domctl.h |  3 ++-
  2 files changed, 16 insertions(+), 1 deletion(-)
xen/common/domctl.c +1 -0
index a69b3b59a8..22aceb3860 100644
@@ -489,6 +489,7 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
      case XEN_DOMCTL_createdomain:
      {
          domid_t        dom;
+        domid_t        parent_dom;
          static domid_t rover = 0;
 
          dom = op->domain;

xen/common/domctl.c long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl) · line 520

Quoted 5 lines
xen/common/domctl.c +2 -0
@@ -515,6 +516,19 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
              rover = dom;
          }
 
+        parent_dom = op->u.createdomain.parent_domid;
+        if ( parent_dom )

I would rather avoid to assume that parent_dom will not be 0 for a few reasons:
1) Most of Xen (if not all) now avoid to assume that dom0->domain_id
== 0.
2) I can see usecases where it we may want to recreate dom0 setup.

So we should consider a different value to indicate whether we want to clone from a domain. Maybe by setting bit 16 of the parent_domid?

Quoted 28 lines
xen/common/domctl.c +11 -0
+        {
+            struct domain *pd = rcu_lock_domain_by_id(parent_dom);
+
+            ret = -EINVAL;
+            if ( !pd )
+                break;
+
+            op->u.createdomain.max_vcpus = pd->max_vcpus;
+            rcu_unlock_domain(pd);
+        }
+
          d = domain_create(dom, &op->u.createdomain, false);
          if ( IS_ERR(d) )
          {
xen/include/public/domctl.h +1 -1
index fec6f6fdd1..251cc40ef6 100644
@@ -38,7 +38,7 @@
  #include "hvm/save.h"
  #include "memory.h"
 
-#define XEN_DOMCTL_INTERFACE_VERSION 0x00000012
+#define XEN_DOMCTL_INTERFACE_VERSION 0x00000013
 
  /*
   * NB. xen_domctl.domain is an IN/OUT parameter for this operation.

xen/include/public/domctl.h struct xen_domctl_createdomain { · line 95

Quoted 4 lines
xen/include/public/domctl.h +1 -0
@@ -92,6 +92,7 @@ struct xen_domctl_createdomain {
      uint32_t max_evtchn_port;
      int32_t max_grant_frames;
      int32_t max_maptrack_frames;
+    domid_t parent_domid;

By just looking at the name, it is not clear what the field is for. It also suggest that one domain will be linked to the other. But this is not the case here. I would recommend to add a comment explaining how this is used by Xen.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#7 lore raw

Re: [Xen-devel] [PATCH v9 2/5] xen: add parent_domid field to createdomain domctl

Quoted 10 lines · Julien Grall

Hi Tamas,

Quoted 5 lines · Tamas K Lengyel

When creating a domain that will be used as a VM fork some information is required to set things up properly, like the max_vcpus count. Instead of the toolstack having to gather this information for each fork in a separate hypercall we can just include the parent domain's id in the createdomain domctl so that Xen can copy the setting without the extra toolstack queries.

Quoted 3 lines

It is not entirely clear why you only want to copy max_vcpus. From my understanding, when you are going to fork a domain you will want the domain to be nearly identical. So how do you decide what to copy?

All parameters of the parent domain need to be copied, but during createdomain domctl only max_vcpus is required. The rest are copied during XENMEM_sharing_op_fork.

Quoted 24 lines
Quoted 24 lines
---
  xen/common/domctl.c         | 14 ++++++++++++++
  xen/include/public/domctl.h |  3 ++-
  2 files changed, 16 insertions(+), 1 deletion(-)
xen/common/domctl.c +1 -0
index a69b3b59a8..22aceb3860 100644
@@ -489,6 +489,7 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
      case XEN_DOMCTL_createdomain:
      {
          domid_t        dom;
+        domid_t        parent_dom;
          static domid_t rover = 0;
 
          dom = op->domain;
@@ -515,6 +516,19 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
              rover = dom;
          }
 
Quoted 8 lines

xen/common/domctl.c long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl) · line 520

Quoted 2 lines
xen/common/domctl.c +2 -0
+        parent_dom = op->u.createdomain.parent_domid;
+        if ( parent_dom )

I would rather avoid to assume that parent_dom will not be 0 for a few reasons:
1) Most of Xen (if not all) now avoid to assume that dom0->domain_id
== 0.
2) I can see usecases where it we may want to recreate dom0 setup.

That's an interesting thought, I don't expect that will be a usecase but it's interesting. Currently I don't think it would work, so I consider that to be out-of-scope.

Quoted 3 lines

So we should consider a different value to indicate whether we want to clone from a domain. Maybe by setting bit 16 of the parent_domid?

I can add a XEN_DOMCTL_CDF_fork flag, but I think that's an overkill.

Quoted 26 lines
Quoted 26 lines
xen/common/domctl.c +11 -0
+        {
+            struct domain *pd = rcu_lock_domain_by_id(parent_dom);
+
+            ret = -EINVAL;
+            if ( !pd )
+                break;
+
+            op->u.createdomain.max_vcpus = pd->max_vcpus;
+            rcu_unlock_domain(pd);
+        }
+
          d = domain_create(dom, &op->u.createdomain, false);
          if ( IS_ERR(d) )
          {
xen/include/public/domctl.h +1 -1
index fec6f6fdd1..251cc40ef6 100644
@@ -38,7 +38,7 @@
  #include "hvm/save.h"
  #include "memory.h"
 
-#define XEN_DOMCTL_INTERFACE_VERSION 0x00000012
+#define XEN_DOMCTL_INTERFACE_VERSION 0x00000013
 
Quoted 12 lines

xen/include/public/domctl.h struct xen_domctl_createdomain { · line 95

Quoted 7 lines
xen/include/public/domctl.h +1 -0
  /*
   * NB. xen_domctl.domain is an IN/OUT parameter for this operation.
@@ -92,6 +92,7 @@ struct xen_domctl_createdomain {
      uint32_t max_evtchn_port;
      int32_t max_grant_frames;
      int32_t max_maptrack_frames;
+    domid_t parent_domid;

By just looking at the name, it is not clear what the field is for. It also suggest that one domain will be linked to the other. But this is not the case here. I would recommend to add a comment explaining how this is used by Xen.

No, during createdomain the domains won't get linked. Only once the XENMEM_sharing_op_fork finishes do the domains get linked. I explain this in the patch message, I can copy that as a comment into the header if you prefer.

Tamas

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#8 lore raw

Re: [Xen-devel] [PATCH v9 2/5] xen: add parent_domid field to createdomain domctl

Hi Tamas,

Quoted 11 lines · Tamas K Lengyel
Quoted 10 lines · Julien Grall

Hi Tamas,

Quoted 5 lines · Tamas K Lengyel

When creating a domain that will be used as a VM fork some information is required to set things up properly, like the max_vcpus count. Instead of the toolstack having to gather this information for each fork in a separate hypercall we can just include the parent domain's id in the createdomain domctl so that Xen can copy the setting without the extra toolstack queries.

Quoted 7 lines
Quoted 3 lines

It is not entirely clear why you only want to copy max_vcpus. From my understanding, when you are going to fork a domain you will want the domain to be nearly identical. So how do you decide what to copy?

All parameters of the parent domain need to be copied, but during createdomain domctl only max_vcpus is required. The rest are copied during XENMEM_sharing_op_fork.

I don't believe max_vcpus is the only field required here. Most of the information hold in the structure are required at creation time so the domain is configured correctly. For instance, on Arm, the version of interrupt controller can only be configured at creation time. For x86, I am pretty sure the emuflags have to be correct at creation time as well.

So it feels weird to me that you only need to copy max_vcpus here. Because if you are able to fill up the other fields of the structure, then most likely you have the max_vcpus in hand as well.

The current suggestion is too restrictive and only save you one hypercall. IMHO, if we are going to update createdomain, then all the fields but parent_domid should be ignored when the latter is valid. The fields can then be filled up and copied back to the toolstack so it can consumed the information.

Quoted 34 lines
Quoted 25 lines
Quoted 25 lines
---
   xen/common/domctl.c         | 14 ++++++++++++++
   xen/include/public/domctl.h |  3 ++-
   2 files changed, 16 insertions(+), 1 deletion(-)
xen/common/domctl.c +1 -0
index a69b3b59a8..22aceb3860 100644
@@ -489,6 +489,7 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
       case XEN_DOMCTL_createdomain:
       {
           domid_t        dom;
+        domid_t        parent_dom;
           static domid_t rover = 0;
 
           dom = op->domain;
@@ -515,6 +516,19 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
               rover = dom;
           }
 
Quoted 8 lines

xen/common/domctl.c long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl) · line 520

Quoted 2 lines
xen/common/domctl.c +2 -0
+        parent_dom = op->u.createdomain.parent_domid;
+        if ( parent_dom )

I would rather avoid to assume that parent_dom will not be 0 for a few reasons:
1) Most of Xen (if not all) now avoid to assume that dom0->domain_id
== 0.
2) I can see usecases where it we may want to recreate dom0 setup.

Quoted 2 lines

That's an interesting thought, I don't expect that will be a usecase but it's interesting.

Maybe not in the context VM fork. But ...

Quoted 2 lines

Currently I don't think it would work, so I
consider that to be out-of-scope.

... this is a generic hypercall and therefore we should be open to other usecase. I am not asking to check whether we can recreate a domain based on dom0. I am only asking to be mindful with your interface and not put ourself in a corner just because this is out-of-scope for you.

The more important bit for me my point 1). I.e not assuming that 0 is an invalid value for domid.

Quoted 6 lines
Quoted 4 lines

So we should consider a different value to indicate whether we want to clone from a domain. Maybe by setting bit 16 of the parent_domid?

I can add a XEN_DOMCTL_CDF_fork flag, but I think that's an overkill.

See above. If you are going to modify a common interface then you need to bear in mind how this can be used.

Quoted 40 lines
Quoted 27 lines
Quoted 27 lines
xen/common/domctl.c +11 -0
+        {
+            struct domain *pd = rcu_lock_domain_by_id(parent_dom);
+
+            ret = -EINVAL;
+            if ( !pd )
+                break;
+
+            op->u.createdomain.max_vcpus = pd->max_vcpus;
+            rcu_unlock_domain(pd);
+        }
+
           d = domain_create(dom, &op->u.createdomain, false);
           if ( IS_ERR(d) )
           {
xen/include/public/domctl.h +1 -1
index fec6f6fdd1..251cc40ef6 100644
@@ -38,7 +38,7 @@
   #include "hvm/save.h"
   #include "memory.h"
 
-#define XEN_DOMCTL_INTERFACE_VERSION 0x00000012
+#define XEN_DOMCTL_INTERFACE_VERSION 0x00000013
 
Quoted 12 lines

xen/include/public/domctl.h struct xen_domctl_createdomain { · line 95

Quoted 7 lines
xen/include/public/domctl.h +1 -0
   /*
    * NB. xen_domctl.domain is an IN/OUT parameter for this operation.
@@ -92,6 +92,7 @@ struct xen_domctl_createdomain {
       uint32_t max_evtchn_port;
       int32_t max_grant_frames;
       int32_t max_maptrack_frames;
+    domid_t parent_domid;

By just looking at the name, it is not clear what the field is for. It also suggest that one domain will be linked to the other. But this is not the case here. I would recommend to add a comment explaining how this is used by Xen.

Quoted 4 lines

No, during createdomain the domains won't get linked. Only once the XENMEM_sharing_op_fork finishes do the domains get linked. I explain this in the patch message, I can copy that as a comment into the header if you prefer.

Bear in mind that developpers don't want to play the blame game everytime they want to understand an interfaces. So you either want to use a meaningful name or a comment in your code.

Maybe "clone_domid" would be clearer here. Yet it is not clear that only "max_vcpus" is going to be copied.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#9 lore raw

Re: [Xen-devel] [PATCH v9 2/5] xen: add parent_domid field to createdomain domctl

Quoted 29 lines · Julien Grall

Hi Tamas,

Quoted 11 lines · Tamas K Lengyel
Quoted 10 lines · Julien Grall

Hi Tamas,

Quoted 5 lines · Tamas K Lengyel

When creating a domain that will be used as a VM fork some information is required to set things up properly, like the max_vcpus count. Instead of the toolstack having to gather this information for each fork in a separate hypercall we can just include the parent domain's id in the createdomain domctl so that Xen can copy the setting without the extra toolstack queries.

Quoted 7 lines
Quoted 3 lines

It is not entirely clear why you only want to copy max_vcpus. From my understanding, when you are going to fork a domain you will want the domain to be nearly identical. So how do you decide what to copy?

All parameters of the parent domain need to be copied, but during createdomain domctl only max_vcpus is required. The rest are copied during XENMEM_sharing_op_fork.

I don't believe max_vcpus is the only field required here. Most of the information hold in the structure are required at creation time so the domain is configured correctly. For instance, on Arm, the version of interrupt controller can only be configured at creation time. For x86, I am pretty sure the emuflags have to be correct at creation time as well.

Quoted 3 lines

So it feels weird to me that you only need to copy max_vcpus here. Because if you are able to fill up the other fields of the structure, then most likely you have the max_vcpus in hand as well.

Look at patch 5 and see how libxl statically define most of these values and why we don't need to copy them.

Quoted 112 lines

The current suggestion is too restrictive and only save you one hypercall. IMHO, if we are going to update createdomain, then all the fields but parent_domid should be ignored when the latter is valid. The fields can then be filled up and copied back to the toolstack so it can consumed the information.

Quoted 34 lines
Quoted 25 lines
Quoted 25 lines
---
   xen/common/domctl.c         | 14 ++++++++++++++
   xen/include/public/domctl.h |  3 ++-
   2 files changed, 16 insertions(+), 1 deletion(-)
xen/common/domctl.c +1 -0
index a69b3b59a8..22aceb3860 100644
@@ -489,6 +489,7 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
       case XEN_DOMCTL_createdomain:
       {
           domid_t        dom;
+        domid_t        parent_dom;
           static domid_t rover = 0;
 
           dom = op->domain;
@@ -515,6 +516,19 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
               rover = dom;
           }
 
Quoted 8 lines

xen/common/domctl.c long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl) · line 520

Quoted 2 lines
xen/common/domctl.c +2 -0
+        parent_dom = op->u.createdomain.parent_domid;
+        if ( parent_dom )

I would rather avoid to assume that parent_dom will not be 0 for a few reasons:
1) Most of Xen (if not all) now avoid to assume that dom0->domain_id
== 0.
2) I can see usecases where it we may want to recreate dom0 setup.

Quoted 2 lines

That's an interesting thought, I don't expect that will be a usecase but it's interesting.

Maybe not in the context VM fork. But ...

Quoted 2 lines

Currently I don't think it would work, so I
consider that to be out-of-scope.

... this is a generic hypercall and therefore we should be open to other usecase. I am not asking to check whether we can recreate a domain based on dom0. I am only asking to be mindful with your interface and not put ourself in a corner just because this is out-of-scope for you.

The more important bit for me my point 1). I.e not assuming that 0 is an invalid value for domid.

Quoted 6 lines
Quoted 4 lines

So we should consider a different value to indicate whether we want to clone from a domain. Maybe by setting bit 16 of the parent_domid?

I can add a XEN_DOMCTL_CDF_fork flag, but I think that's an overkill.

See above. If you are going to modify a common interface then you need to bear in mind how this can be used.

Quoted 40 lines
Quoted 27 lines
Quoted 27 lines
xen/common/domctl.c +11 -0
+        {
+            struct domain *pd = rcu_lock_domain_by_id(parent_dom);
+
+            ret = -EINVAL;
+            if ( !pd )
+                break;
+
+            op->u.createdomain.max_vcpus = pd->max_vcpus;
+            rcu_unlock_domain(pd);
+        }
+
           d = domain_create(dom, &op->u.createdomain, false);
           if ( IS_ERR(d) )
           {
xen/include/public/domctl.h +1 -1
index fec6f6fdd1..251cc40ef6 100644
@@ -38,7 +38,7 @@
   #include "hvm/save.h"
   #include "memory.h"
 
-#define XEN_DOMCTL_INTERFACE_VERSION 0x00000012
+#define XEN_DOMCTL_INTERFACE_VERSION 0x00000013
 
Quoted 12 lines

xen/include/public/domctl.h struct xen_domctl_createdomain { · line 95

Quoted 7 lines
xen/include/public/domctl.h +1 -0
   /*
    * NB. xen_domctl.domain is an IN/OUT parameter for this operation.
@@ -92,6 +92,7 @@ struct xen_domctl_createdomain {
       uint32_t max_evtchn_port;
       int32_t max_grant_frames;
       int32_t max_maptrack_frames;
+    domid_t parent_domid;

By just looking at the name, it is not clear what the field is for. It also suggest that one domain will be linked to the other. But this is not the case here. I would recommend to add a comment explaining how this is used by Xen.

Quoted 4 lines

No, during createdomain the domains won't get linked. Only once the XENMEM_sharing_op_fork finishes do the domains get linked. I explain this in the patch message, I can copy that as a comment into the header if you prefer.

Quoted 6 lines

Bear in mind that developpers don't want to play the blame game everytime they want to understand an interfaces. So you either want to use a meaningful name or a comment in your code.

Maybe "clone_domid" would be clearer here. Yet it is not clear that only "max_vcpus" is going to be copied.

Julien,
you have valid points but at this time I won't be able to refactor this series any more. This patch series was first posted in September, it's now almost March. So at this point I'm just going to say drop this patch and we'll live with the limitation that VM forking only works with single vCPU domains until at some later time someone needs it to work with guests that have more then 1 vCPUs. If that's also unacceptable for whatever reason, then we'll live with the feature not being merged upstream.

Cheers,
Tamas

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#10 lore raw

Re: [Xen-devel] [PATCH v9 2/5] xen: add parent_domid field to createdomain domctl

Quoted 31 lines · Tamas K Lengyel
Quoted 29 lines · Julien Grall

Hi Tamas,

Quoted 11 lines · Tamas K Lengyel
Quoted 10 lines · Julien Grall

Hi Tamas,

Quoted 5 lines · Tamas K Lengyel

When creating a domain that will be used as a VM fork some information is required to set things up properly, like the max_vcpus count. Instead of the toolstack having to gather this information for each fork in a separate hypercall we can just include the parent domain's id in the createdomain domctl so that Xen can copy the setting without the extra toolstack queries.

Quoted 7 lines
Quoted 3 lines

It is not entirely clear why you only want to copy max_vcpus. From my understanding, when you are going to fork a domain you will want the domain to be nearly identical. So how do you decide what to copy?

All parameters of the parent domain need to be copied, but during createdomain domctl only max_vcpus is required. The rest are copied during XENMEM_sharing_op_fork.

I don't believe max_vcpus is the only field required here. Most of the information hold in the structure are required at creation time so the domain is configured correctly. For instance, on Arm, the version of interrupt controller can only be configured at creation time. For x86, I am pretty sure the emuflags have to be correct at creation time as well.

Quoted 6 lines
Quoted 3 lines

So it feels weird to me that you only need to copy max_vcpus here. Because if you are able to fill up the other fields of the structure, then most likely you have the max_vcpus in hand as well.

Look at patch 5 and see how libxl statically define most of these values and why we don't need to copy them.

I looked at patch 5, this is an example of the implementation. You limit yourself quite a bit and that's your choice. But I am afraid this does not mean the interface with the hypervisor should do the same.

[...]

Quoted 7 lines

Julien,
you have valid points but at this time I won't be able to refactor this series any more. This patch series was first posted in September, it's now almost March. So at this point I'm just going to say drop this patch and we'll live with the limitation that VM forking only works with single vCPU domains until at some later time someone needs it to work with guests that have more then 1 vCPUs.

To be honest, I don't have a vested interest for the VM forking. So the limitation
of 1 vCPU is fine with me.

Anyone who will want to support more than 1 vCPU with forking will have to come up with a proper interface. If you don't want to invest time on it that's fine, the rest of the code is still useful to have.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#11 lore raw

Re: [Xen-devel] [PATCH v9 2/5] xen: add parent_domid field to createdomain domctl

Quoted 55 lines · Julien Grall
Quoted 31 lines · Tamas K Lengyel
Quoted 29 lines · Julien Grall

Hi Tamas,

Quoted 11 lines · Tamas K Lengyel
Quoted 10 lines · Julien Grall

Hi Tamas,

Quoted 5 lines · Tamas K Lengyel

When creating a domain that will be used as a VM fork some information is required to set things up properly, like the max_vcpus count. Instead of the toolstack having to gather this information for each fork in a separate hypercall we can just include the parent domain's id in the createdomain domctl so that Xen can copy the setting without the extra toolstack queries.

Quoted 7 lines
Quoted 3 lines

It is not entirely clear why you only want to copy max_vcpus. From my understanding, when you are going to fork a domain you will want the domain to be nearly identical. So how do you decide what to copy?

All parameters of the parent domain need to be copied, but during createdomain domctl only max_vcpus is required. The rest are copied during XENMEM_sharing_op_fork.

I don't believe max_vcpus is the only field required here. Most of the information hold in the structure are required at creation time so the domain is configured correctly. For instance, on Arm, the version of interrupt controller can only be configured at creation time. For x86, I am pretty sure the emuflags have to be correct at creation time as well.

Quoted 6 lines
Quoted 3 lines

So it feels weird to me that you only need to copy max_vcpus here. Because if you are able to fill up the other fields of the structure, then most likely you have the max_vcpus in hand as well.

Look at patch 5 and see how libxl statically define most of these values and why we don't need to copy them.

I looked at patch 5, this is an example of the implementation. You limit yourself quite a bit and that's your choice. But I am afraid this does not mean the interface with the hypervisor should do the same.

[...]

Quoted 7 lines

Julien,
you have valid points but at this time I won't be able to refactor this series any more. This patch series was first posted in September, it's now almost March. So at this point I'm just going to say drop this patch and we'll live with the limitation that VM forking only works with single vCPU domains until at some later time someone needs it to work with guests that have more then 1 vCPUs.

Quoted 7 lines

To be honest, I don't have a vested interest for the VM forking. So the limitation
of 1 vCPU is fine with me.

Anyone who will want to support more than 1 vCPU with forking will have to come up with a proper interface. If you don't want to invest time on it that's fine, the rest of the code is still useful to have.

The toolstack can always just decide to do the extra hypercall query for the max_vcpus and make things work that way. In our usecase we have single vCPU domains so doing that extra query is something I want to avoid.

Tamas

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#15 lore raw

Re: [Xen-devel] [PATCH v9 3/5] xen/mem_sharing: VM forking

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

VM forking is the process of creating a domain with an empty memory space and a parent domain specified from which to populate the memory when necessary. For the new domain to be functional the VM state is copied over as part of the fork operation (HVM params, hap allocation, etc).

---
v9: remove stale header
fix tsc incarnation being bumped on set

---
 xen/arch/x86/domain.c             |  11 ++
 xen/arch/x86/hvm/hvm.c            |   2 +-
 xen/arch/x86/mm/mem_sharing.c     | 222 ++++++++++++++++++++++++++++++
 xen/arch/x86/mm/p2m.c             |  11 +-
 xen/include/asm-x86/mem_sharing.h |  17 +++
 xen/include/public/memory.h       |   5 +
 xen/include/xen/sched.h           |   2 +
 7 files changed, 268 insertions(+), 2 deletions(-)
xen/arch/x86/domain.c +11 -0
index fe63c23676..1ab0ca0942 100644
@@ -2203,6 +2203,17 @@ int domain_relinquish_resources(struct domain *d)
             ret = relinquish_shared_pages(d);
             if ( ret )
                 return ret;
+
+            /*
+             * If the domain is forked, decrement the parent's pause count
+             * and release the domain.
+             */
+            if ( d->parent )
+            {
+                domain_unpause(d->parent);
+                put_domain(d->parent);
+                d->parent = NULL;
+            }
         }
 #endif
 
xen/arch/x86/hvm/hvm.c +1 -1
index a339b36a0d..9bfa603f8c 100644
@@ -1915,7 +1915,7 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
     }
 #endif
 
-    /* Spurious fault? PoD and log-dirty also take this path. */
+    /* Spurious fault? PoD, log-dirty and VM forking also take this path. */
     if ( p2m_is_ram(p2mt) )
     {
         rc = 1;
xen/arch/x86/mm/mem_sharing.c +8 -0
index 3835bc928f..ad5db9d8d5 100644
@@ -22,6 +22,7 @@
 
 #include <xen/types.h>
 #include <xen/domain_page.h>
+#include <xen/event.h>
 #include <xen/spinlock.h>
 #include <xen/rwlock.h>
 #include <xen/mm.h>
@@ -36,6 +37,9 @@
 #include <asm/altp2m.h>
 #include <asm/atomic.h>
 #include <asm/event.h>
+#include <asm/hap.h>
+#include <asm/hvm/hvm.h>
+#include <asm/hvm/save.h>
 #include <xsm/xsm.h>
 
 #include "mm-locks.h"
@@ -1444,6 +1448,194 @@ static inline int mem_sharing_control(struct domain *d, bool enable)
     return 0;
 }
 
+/*
+ * Forking a page only gets called when the VM faults due to no entry being
+ * in the EPT for the access. Depending on the type of access we either
+ * populate the physmap with a shared entry for read-only access or

xen/arch/x86/mm/mem_sharing.c static inline int mem_sharing_control(struct domain *d, bool enable) · line 1464

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+ * fork the page if its a write access.
+ *
+ * The client p2m is already locked so we only need to lock
+ * the parent's here.
+ */
+int mem_sharing_fork_page(struct domain *d, gfn_t gfn, bool unsharing)
+{
+    int rc = -ENOENT;
+    shr_handle_t handle;
+    struct domain *parent;

Can you constify parent, I assume there are no changes made to the parent domain, just the forked one.

xen/arch/x86/mm/mem_sharing.c static inline int mem_sharing_control(struct domain *d, bool enable) · line 1474

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+    struct p2m_domain *p2m;
+    unsigned long gfn_l = gfn_x(gfn);
+    mfn_t mfn, new_mfn;
+    p2m_type_t p2mt;
+    struct page_info *page;
+
+    if ( !mem_sharing_is_fork(d) )
+        return -ENOENT;
+
+    parent = d->parent;

You can initialize at declaration time.

Quoted 14 lines
xen/arch/x86/mm/mem_sharing.c +14 -0
+
+    if ( !unsharing )
+    {
+        /* For read-only accesses we just add a shared entry to the physmap */
+        while ( parent )
+        {
+            if ( !(rc = nominate_page(parent, gfn, 0, &handle)) )
+                break;
+
+            parent = parent->parent;
+        }
+
+        if ( !rc )
+        {

xen/arch/x86/mm/mem_sharing.c static inline int mem_sharing_control(struct domain *d, bool enable) · line 1498

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+            /* The client's p2m is already locked */
+            struct p2m_domain *pp2m = p2m_get_hostp2m(parent);
+
+            p2m_lock(pp2m);
+            rc = add_to_physmap(parent, gfn_l, handle, d, gfn_l, false);
+            p2m_unlock(pp2m);
+
+            if ( !rc )
+                return 0;
+        }

Don't you need to return here, or else you will fallback into the unsharing path?

Quoted 4 lines
xen/arch/x86/mm/mem_sharing.c +4 -0
+    }
+
+    /*
+     * If it's a write access (ie. unsharing) or if adding a shared entry to

xen/arch/x86/mm/mem_sharing.c static inline int mem_sharing_control(struct domain *d, bool enable) · line 1512

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+     * the physmap failed we'll fork the page directly.
+     */
+    p2m = p2m_get_hostp2m(d);
+    parent = d->parent;
+
+    while ( parent )
+    {
+        mfn = get_gfn_query(parent, gfn_l, &p2mt);
+
+        if ( mfn_valid(mfn) && p2m_is_any_ram(p2mt) )

This would also cover grants, but I'm not sure how those are handled by forking, as access to those is granted on a per-domain basis. Ie:
the parent will have access to the grant, but not the child.

Quoted 12 lines
xen/arch/x86/mm/mem_sharing.c +12 -0
+            break;
+
+        put_gfn(parent, gfn_l);
+        parent = parent->parent;
+    }
+
+    if ( !parent )
+        return -ENOENT;
+
+    if ( !(page = alloc_domheap_page(d, 0)) )
+    {
+        put_gfn(parent, gfn_l);

xen/arch/x86/mm/mem_sharing.c static inline int mem_sharing_control(struct domain *d, bool enable) · line 1534

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+        return -ENOMEM;
+    }
+
+    new_mfn = page_to_mfn(page);
+    copy_domain_page(new_mfn, mfn);
+    set_gpfn_from_mfn(mfn_x(new_mfn), gfn_l);
+
+    put_gfn(parent, gfn_l);
+
+    return p2m->set_entry(p2m, gfn, new_mfn, PAGE_ORDER_4K, p2m_ram_rw,

So the child p2m is going to be populated using 4K pages exclusively? Maybe it would make sense to try to use 2M if the parent domain page is also a 2M page or larger?

Quoted 15 lines
xen/arch/x86/mm/mem_sharing.c +15 -0
+                          p2m->default_access, -1);
+}
+
+static int bring_up_vcpus(struct domain *cd, struct cpupool *cpupool)
+{
+    int ret;
+    unsigned int i;
+
+    if ( (ret = cpupool_move_domain(cd, cpupool)) )
+        return ret;
+
+    for ( i = 0; i < cd->max_vcpus; i++ )
+    {
+        if ( cd->vcpu[i] )
+            continue;

xen/arch/x86/mm/mem_sharing.c static inline int mem_sharing_control(struct domain *d, bool enable) · line 1559

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+
+        if ( !vcpu_create(cd, i) )
+            return -EINVAL;
+    }
+
+    domain_update_node_affinity(cd);
+    return 0;
+}
+
+static int fork_hap_allocation(struct domain *cd, struct domain *d)

Can you constify the parent domain?

Also cd and d and not very helpful names, I would just use child and parent.

Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c +9 -0
+{
+    int rc;
+    bool preempted;
+    unsigned long mb = hap_get_allocation(d);
+
+    if ( mb == hap_get_allocation(cd) )
+        return 0;
+
+    paging_lock(cd);

xen/arch/x86/mm/mem_sharing.c static inline int mem_sharing_control(struct domain *d, bool enable) · line 1578

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+    rc = hap_set_allocation(cd, mb << (20 - PAGE_SHIFT), &preempted);
+    paging_unlock(cd);
+
+    if ( rc )
+        return rc;
+
+    if ( preempted )
+        return -ERESTART;
+
+    return 0;

You can join all the checks into a single return:

return preempted ? -ERESTART : rc;

xen/arch/x86/mm/mem_sharing.c static inline int mem_sharing_control(struct domain *d, bool enable) · line 1590

Quoted 12 lines
xen/arch/x86/mm/mem_sharing.c +12 -0
+}
+
+static void fork_tsc(struct domain *cd, struct domain *d)
+{
+    uint32_t tsc_mode;
+    uint32_t gtsc_khz;
+    uint32_t incarnation;
+    uint64_t elapsed_nsec;
+
+    tsc_get_info(d, &tsc_mode, &elapsed_nsec, &gtsc_khz, &incarnation);
+    /* Don't bump incarnation on set */
+    tsc_set_info(cd, tsc_mode, elapsed_nsec, gtsc_khz, incarnation - 1);

Why is the incarnation not bumped?

xen/arch/x86/mm/mem_sharing.c static inline int mem_sharing_control(struct domain *d, bool enable) · line 1598

Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8 -0
+}
+
+static int mem_sharing_fork(struct domain *d, struct domain *cd)
+{
+    int rc = -EINVAL;
+
+    if ( !cd->controller_pause_count )
+        return rc;

-EBUSY might be better here.

xen/arch/x86/mm/mem_sharing.c static inline int mem_sharing_control(struct domain *d, bool enable) · line 1606

Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8 -0
+
+    /*
+     * We only want to get and pause the parent once, not each time this
+     * operation is restarted due to preemption.
+     */
+    if ( !cd->parent_paused )
+    {
+        ASSERT(get_domain(d));

We are trying to avoid such constructs, instead I suggest:

if ( !get_domain(parent) )

{
    ASSERT_UNREACHABLE();
    return -EBUSY;
}
Quoted 47 lines
xen/arch/x86/mm/mem_sharing.c +40 -0
+        domain_pause(d);
+
+        cd->parent_paused = true;
+        cd->max_pages = d->max_pages;
+        cd->max_vcpus = d->max_vcpus;
+    }
+
+    /* this is preemptible so it's the first to get done */
+    if ( (rc = fork_hap_allocation(cd, d)) )
+        goto done;
+
+    if ( (rc = bring_up_vcpus(cd, d->cpupool)) )
+        goto done;
+
+    if ( (rc = hvm_copy_context_and_params(cd, d)) )
+        goto done;
+
+    fork_tsc(cd, d);
+
+    cd->parent = d;
+
+ done:
+    if ( rc && rc != -ERESTART )
+    {
+        domain_unpause(d);
+        put_domain(d);
+        cd->parent_paused = false;
+    }
+
+    return rc;
+}
+
 int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg)
 {
     int rc;
@@ -1698,6 +1890,36 @@ int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg)
         rc = debug_gref(d, mso.u.debug.u.gref);
         break;
 
+    case XENMEM_sharing_op_fork:
+    {
+        struct domain *pd;
+
+        rc = -EINVAL;
+        if ( mso.u.fork._pad[0] || mso.u.fork._pad[1] ||
+             mso.u.fork._pad[2] )
+            goto out;

xen/arch/x86/mm/mem_sharing.c int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg) · line 1910

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+
+        rc = rcu_lock_live_remote_domain_by_id(mso.u.fork.parent_domain,
+                                               &pd);
+        if ( rc )
+            goto out;
+
+        if ( !mem_sharing_enabled(pd) )
+        {
+            if ( (rc = mem_sharing_control(pd, true)) )
+                goto out;

Please join both conditions:

if ( !mem_sharing_enabled(pd) &&
(rc = mem_sharing_control(pd, true)) )
goto out;

Quoted 20 lines
xen/arch/x86/mm/mem_sharing.c +12 -0
+        }
+
+        rc = mem_sharing_fork(pd, d);
+
+        if ( rc == -ERESTART )
+            rc = hypercall_create_continuation(__HYPERVISOR_memory_op,
+                                               "lh", XENMEM_sharing_op,
+                                               arg);
+        rcu_unlock_domain(pd);
+        break;
+    }
+
     default:
         rc = -ENOSYS;
         break;
xen/arch/x86/mm/p2m.c +0 -0
index c5f428d67c..7c4d2fd7a0 100644

xen/arch/x86/mm/p2m.c mfn_t __get_gfn_type_access(struct p2m_domain *p2m, unsigned long gfn_l, · line 517

Quoted 9 lines
xen/arch/x86/mm/p2m.c +6 -0
@@ -509,6 +509,14 @@ mfn_t __get_gfn_type_access(struct p2m_domain *p2m, unsigned long gfn_l,
 
     mfn = p2m->get_entry(p2m, gfn, t, a, q, page_order, NULL);
 
+    /* Check if we need to fork the page */
+    if ( (q & P2M_ALLOC) && p2m_is_hole(*t) &&
+         !mem_sharing_fork_page(p2m->domain, gfn, !!(q & P2M_UNSHARE)) )
+    {
+        mfn = p2m->get_entry(p2m, gfn, t, a, q, page_order, NULL);
+    }

No need for the braces.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#31 lore raw

Re: [Xen-devel] [PATCH v9 4/5] x86/mem_sharing: reset a fork

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

Implement hypercall that allows a fork to shed all memory that got allocated for it during its execution and re-load its vCPU context from the parent VM. This allows the forked VM to reset into the same state the parent VM is in a faster way then creating a new fork would be. Measurements show about a 2x speedup during normal fuzzing operations. Performance may vary depending how

^ on

Quoted 16 lines

much memory got allocated for the forked VM. If it has been completely deduplicated from the parent VM then creating a new fork would likely be more performant.

---
 xen/arch/x86/mm/mem_sharing.c | 76 +++++++++++++++++++++++++++++++++++
 xen/include/public/memory.h   |  1 +
 2 files changed, 77 insertions(+)
xen/arch/x86/mm/mem_sharing.c +0 -0
index ad5db9d8d5..fb6892aaa6 100644
@@ -1636,6 +1636,59 @@ static int mem_sharing_fork(struct domain *d, struct domain *cd)
     return rc;

xen/arch/x86/mm/mem_sharing.c static int mem_sharing_fork(struct domain *d, struct domain *cd) · line 1646

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +8 -0
 }
 
+/*
+ * The fork reset operation is intended to be used on short-lived forks only.
+ * There is no hypercall continuation operation implemented for this reason.
+ * For forks that obtain a larger memory footprint it is likely going to be
+ * more performant to create a new fork instead of resetting an existing one.
+ *
+ * TODO: In case this hypercall would become useful on forks with larger memory
+ * footprints the hypercall continuation should be implemented.

I'm afraid this is not safe, as users don't have an easy way to know whether a fork will have a large memory footprint or not.

IMO you either need some kind of check that prevents this function from being executed when the domain as too much memory assigned, or you need to implement continuations.

Or else this is very likely to trip over the watchdog.

xen/arch/x86/mm/mem_sharing.c static int mem_sharing_fork(struct domain *d, struct domain *cd) · line 1648

Quoted 2 lines
xen/arch/x86/mm/mem_sharing.c +2 -0
+ */
+static int mem_sharing_fork_reset(struct domain *d, struct domain *cd)

No need for the mem_sharing prefix, as it's a static function.

xen/arch/x86/mm/mem_sharing.c static int mem_sharing_fork(struct domain *d, struct domain *cd) · line 1656

Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8 -0
+{
+    int rc;
+    struct p2m_domain* p2m = p2m_get_hostp2m(cd);
+    struct page_info *page, *tmp;
+
+    domain_pause(cd);
+
+    page_list_for_each_safe(page, tmp, &cd->page_list)

You can use something like:

while ( (page = page_list_remove_head(&cd->page_list) != NULL )

{
    ...
}

And avoid the *tmp local variable.

Quoted 13 lines
xen/arch/x86/mm/mem_sharing.c +13 -0
+    {
+        p2m_type_t p2mt;
+        p2m_access_t p2ma;
+        gfn_t gfn;
+        mfn_t mfn = page_to_mfn(page);
+
+        if ( !mfn_valid(mfn) )
+            continue;
+
+        gfn = mfn_to_gfn(cd, mfn);
+        mfn = __get_gfn_type_access(p2m, gfn_x(gfn), &p2mt, &p2ma,
+                                    0, NULL, false);
+

xen/arch/x86/mm/mem_sharing.c static int mem_sharing_fork(struct domain *d, struct domain *cd) · line 1679

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+        if ( !p2m_is_ram(p2mt) || p2m_is_shared(p2mt) )
+            continue;
+
+        /* take an extra reference */
+        if ( !get_page(page, cd) )
+            continue;
+
+        rc = p2m->set_entry(p2m, gfn, INVALID_MFN, PAGE_ORDER_4K,
+                            p2m_invalid, p2m_access_rwx, -1);
+        ASSERT(!rc);

Can you handle this gracefully?

Quoted 20 lines
xen/arch/x86/mm/mem_sharing.c +13 -0
+
+        put_page_alloc_ref(page);
+        put_page(page);
+    }
+
+    if ( !(rc = hvm_copy_context_and_params(cd, d)) )
+        fork_tsc(cd, d);
+
+    domain_unpause(cd);
+    return rc;
+}
+
 int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg)
 {
     int rc;
@@ -1920,6 +1973,29 @@ int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg)
         break;
     }
 
+    case XENMEM_sharing_op_fork_reset:

xen/arch/x86/mm/mem_sharing.c int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg) · line 1986

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+    {
+        struct domain *pd;
+
+        rc = -EINVAL;
+        if ( mso.u.fork._pad[0] || mso.u.fork._pad[1] ||
+             mso.u.fork._pad[2] )
+            goto out;
+
+        rc = -ENOSYS;
+        if ( !d->parent )

Why not use mem_sharing_is_fork?

Also I think EINVAL would be more suitable here, as the passed domid doesn't belong to a fork?

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Reviewed-by on v9 1/5
#2 lore raw

Re: [Xen-devel] [PATCH v9 1/5] xen/x86: Make hap_get_allocation accessible

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

During VM forking we'll copy the parent domain's parameters to the client, including the HAP shadow memory setting that is used for storing the domain's EPT. We'll copy this in the hypervisor instead doing it during toolstack launch to allow the domain to start executing and unsharing memory before (or even completely without) the toolstack.

I would also be fine with this merged into the respective patch where hap_get_allocation gets called.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#32 lore raw

Re: [Xen-devel] [PATCH v9 4/5] x86/mem_sharing: reset a fork

In reply to Roger Pau Monné
Quoted 26 lines · Roger Pau Monné
Quoted 5 lines · Tamas K Lengyel

Implement hypercall that allows a fork to shed all memory that got allocated for it during its execution and re-load its vCPU context from the parent VM. This allows the forked VM to reset into the same state the parent VM is in a faster way then creating a new fork would be. Measurements show about a 2x speedup during normal fuzzing operations. Performance may vary depending how

^ on

Quoted 18 lines

much memory got allocated for the forked VM. If it has been completely deduplicated from the parent VM then creating a new fork would likely be more performant.

---
 xen/arch/x86/mm/mem_sharing.c | 76 +++++++++++++++++++++++++++++++++++
 xen/include/public/memory.h   |  1 +
 2 files changed, 77 insertions(+)
xen/arch/x86/mm/mem_sharing.c +0 -0
index ad5db9d8d5..fb6892aaa6 100644
@@ -1636,6 +1636,59 @@ static int mem_sharing_fork(struct domain *d, struct domain *cd)
     return rc;
 }
 
Quoted 11 lines

xen/arch/x86/mm/mem_sharing.c static int mem_sharing_fork(struct domain *d, struct domain *cd) · line 1646

Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8 -0
+/*
+ * The fork reset operation is intended to be used on short-lived forks only.
+ * There is no hypercall continuation operation implemented for this reason.
+ * For forks that obtain a larger memory footprint it is likely going to be
+ * more performant to create a new fork instead of resetting an existing one.
+ *
+ * TODO: In case this hypercall would become useful on forks with larger memory
+ * footprints the hypercall continuation should be implemented.

I'm afraid this is not safe, as users don't have an easy way to know whether a fork will have a large memory footprint or not.

They do, getdomaininfo tells a user exactly how much memory has been allocated for a domain.

Quoted 4 lines

IMO you either need some kind of check that prevents this function from being executed when the domain as too much memory assigned, or you need to implement continuations.

I really don't think we need continuation here with the usecase we have for this function but I'm also tired of arguing about it, so I'll just add it even if its going to be dead code.

Quoted 2 lines

Or else this is very likely to trip over the watchdog.

The watchdog?

Quoted 5 lines

xen/arch/x86/mm/mem_sharing.c static int mem_sharing_fork(struct domain *d, struct domain *cd) · line 1648

Quoted 3 lines
xen/arch/x86/mm/mem_sharing.c +2 -0
+ */
+static int mem_sharing_fork_reset(struct domain *d, struct domain *cd)

No need for the mem_sharing prefix, as it's a static function.

Sure.

Quoted 12 lines

xen/arch/x86/mm/mem_sharing.c static int mem_sharing_fork(struct domain *d, struct domain *cd) · line 1656

Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c +8 -0
+{
+    int rc;
+    struct p2m_domain* p2m = p2m_get_hostp2m(cd);
+    struct page_info *page, *tmp;
+
+    domain_pause(cd);
+
+    page_list_for_each_safe(page, tmp, &cd->page_list)

You can use something like:

Quoted 6 lines

while ( (page = page_list_remove_head(&cd->page_list) != NULL )

{
    ...
}

And avoid the *tmp local variable.

Sure.

Quoted 18 lines
Quoted 18 lines
xen/arch/x86/mm/mem_sharing.c +17 -0
+    {
+        p2m_type_t p2mt;
+        p2m_access_t p2ma;
+        gfn_t gfn;
+        mfn_t mfn = page_to_mfn(page);
+
+        if ( !mfn_valid(mfn) )
+            continue;
+
+        gfn = mfn_to_gfn(cd, mfn);
+        mfn = __get_gfn_type_access(p2m, gfn_x(gfn), &p2mt, &p2ma,
+                                    0, NULL, false);
+
+        if ( !p2m_is_ram(p2mt) || p2m_is_shared(p2mt) )
+            continue;
+
+        /* take an extra reference */
Quoted 8 lines

xen/arch/x86/mm/mem_sharing.c static int mem_sharing_fork(struct domain *d, struct domain *cd) · line 1679

Quoted 6 lines
xen/arch/x86/mm/mem_sharing.c +6 -0
+        if ( !get_page(page, cd) )
+            continue;
+
+        rc = p2m->set_entry(p2m, gfn, INVALID_MFN, PAGE_ORDER_4K,
+                            p2m_invalid, p2m_access_rwx, -1);
+        ASSERT(!rc);

Can you handle this gracefully?

Nope. This should never happen, so if it does, something is very wrong in some other part of Xen.

Quoted 20 lines
Quoted 20 lines
xen/arch/x86/mm/mem_sharing.c +12 -0
+
+        put_page_alloc_ref(page);
+        put_page(page);
+    }
+
+    if ( !(rc = hvm_copy_context_and_params(cd, d)) )
+        fork_tsc(cd, d);
+
+    domain_unpause(cd);
+    return rc;
+}
+
 int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg)
 {
     int rc;
@@ -1920,6 +1973,29 @@ int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg)
         break;
     }
 
Quoted 13 lines

xen/arch/x86/mm/mem_sharing.c int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg) · line 1986

Quoted 11 lines
xen/arch/x86/mm/mem_sharing.c +11 -0
+    case XENMEM_sharing_op_fork_reset:
+    {
+        struct domain *pd;
+
+        rc = -EINVAL;
+        if ( mso.u.fork._pad[0] || mso.u.fork._pad[1] ||
+             mso.u.fork._pad[2] )
+            goto out;
+
+        rc = -ENOSYS;
+        if ( !d->parent )

Why not use mem_sharing_is_fork?

I could.

Quoted 3 lines

Also I think EINVAL would be more suitable here, as the passed domid doesn't belong to a fork?

Sure.

Thanks,
Tamas

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#33 lore raw

Re: [Xen-devel] [PATCH v9 4/5] x86/mem_sharing: reset a fork

Quoted 47 lines · Tamas K Lengyel
Quoted 26 lines · Roger Pau Monné
Quoted 5 lines · Tamas K Lengyel

Implement hypercall that allows a fork to shed all memory that got allocated for it during its execution and re-load its vCPU context from the parent VM. This allows the forked VM to reset into the same state the parent VM is in a faster way then creating a new fork would be. Measurements show about a 2x speedup during normal fuzzing operations. Performance may vary depending how

^ on

Quoted 18 lines

much memory got allocated for the forked VM. If it has been completely deduplicated from the parent VM then creating a new fork would likely be more performant.

---
 xen/arch/x86/mm/mem_sharing.c | 76 +++++++++++++++++++++++++++++++++++
 xen/include/public/memory.h   |  1 +
 2 files changed, 77 insertions(+)
xen/arch/x86/mm/mem_sharing.c +0 -0
index ad5db9d8d5..fb6892aaa6 100644
@@ -1636,6 +1636,59 @@ static int mem_sharing_fork(struct domain *d, struct domain *cd)
     return rc;
 }
 
Quoted 11 lines

xen/arch/x86/mm/mem_sharing.c static int mem_sharing_fork(struct domain *d, struct domain *cd) · line 1646

Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8 -0
+/*
+ * The fork reset operation is intended to be used on short-lived forks only.
+ * There is no hypercall continuation operation implemented for this reason.
+ * For forks that obtain a larger memory footprint it is likely going to be
+ * more performant to create a new fork instead of resetting an existing one.
+ *
+ * TODO: In case this hypercall would become useful on forks with larger memory
+ * footprints the hypercall continuation should be implemented.

I'm afraid this is not safe, as users don't have an easy way to know whether a fork will have a large memory footprint or not.

They do, getdomaininfo tells a user exactly how much memory has been allocated for a domain.

Quoted 4 lines

IMO you either need some kind of check that prevents this function from being executed when the domain as too much memory assigned, or you need to implement continuations.

Quoted 8 lines

I really don't think we need continuation here with the usecase we have for this function but I'm also tired of arguing about it, so I'll just add it even if its going to be dead code.

Quoted 2 lines

Or else this is very likely to trip over the watchdog.

The watchdog?

Yes, Xen has a watchdog and this loop is likely to trigger it if it takes > 5s to complete. The watchdog triggering is a fatal event and leads to host crash.

Note that watchdog is not enabled by default, you need to enable it on the Xen command line.

Quoted 26 lines
Quoted 17 lines
Quoted 17 lines
xen/arch/x86/mm/mem_sharing.c +17 -0
+    {
+        p2m_type_t p2mt;
+        p2m_access_t p2ma;
+        gfn_t gfn;
+        mfn_t mfn = page_to_mfn(page);
+
+        if ( !mfn_valid(mfn) )
+            continue;
+
+        gfn = mfn_to_gfn(cd, mfn);
+        mfn = __get_gfn_type_access(p2m, gfn_x(gfn), &p2mt, &p2ma,
+                                    0, NULL, false);
+
+        if ( !p2m_is_ram(p2mt) || p2m_is_shared(p2mt) )
+            continue;
+
+        /* take an extra reference */
Quoted 8 lines

xen/arch/x86/mm/mem_sharing.c static int mem_sharing_fork(struct domain *d, struct domain *cd) · line 1679

Quoted 6 lines
xen/arch/x86/mm/mem_sharing.c +6 -0
+        if ( !get_page(page, cd) )
+            continue;
+
+        rc = p2m->set_entry(p2m, gfn, INVALID_MFN, PAGE_ORDER_4K,
+                            p2m_invalid, p2m_access_rwx, -1);
+        ASSERT(!rc);

Can you handle this gracefully?

Quoted 2 lines

Nope. This should never happen, so if it does, something is very wrong in some other part of Xen.

OK, please switch it to BUG_ON then instead of ASSERT. It's better to crash here than to misbehave later.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#12 lore raw

Re: [Xen-devel] [PATCH v9 2/5] xen: add parent_domid field to createdomain domctl

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

When creating a domain that will be used as a VM fork some information is required to set things up properly, like the max_vcpus count. Instead of the toolstack having to gather this information for each fork in a separate hypercall we can just include the parent domain's id in the createdomain domctl so that Xen can copy the setting without the extra toolstack queries.

Right, but when I said this wasn't safe, I did mean it...

What happens when parent and the current domain have different gnttab or evtchn limits, or different emulation settings?

If you want to fork a domain safely, you either need to have no parameters passed by the toolstack (and let Xen copy appropriate values), or cross check every provided parameter and bail early on a mismatch.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#16 lore raw

Re: [Xen-devel] [PATCH v9 3/5] xen/mem_sharing: VM forking

In reply to Roger Pau Monné
Quoted 91 lines · Roger Pau Monné
Quoted 89 lines · Tamas K Lengyel

VM forking is the process of creating a domain with an empty memory space and a parent domain specified from which to populate the memory when necessary. For the new domain to be functional the VM state is copied over as part of the fork operation (HVM params, hap allocation, etc).

---
v9: remove stale header
fix tsc incarnation being bumped on set

---
 xen/arch/x86/domain.c             |  11 ++
 xen/arch/x86/hvm/hvm.c            |   2 +-
 xen/arch/x86/mm/mem_sharing.c     | 222 ++++++++++++++++++++++++++++++
 xen/arch/x86/mm/p2m.c             |  11 +-
 xen/include/asm-x86/mem_sharing.h |  17 +++
 xen/include/public/memory.h       |   5 +
 xen/include/xen/sched.h           |   2 +
 7 files changed, 268 insertions(+), 2 deletions(-)
xen/arch/x86/domain.c +11 -0
index fe63c23676..1ab0ca0942 100644
@@ -2203,6 +2203,17 @@ int domain_relinquish_resources(struct domain *d)
             ret = relinquish_shared_pages(d);
             if ( ret )
                 return ret;
+
+            /*
+             * If the domain is forked, decrement the parent's pause count
+             * and release the domain.
+             */
+            if ( d->parent )
+            {
+                domain_unpause(d->parent);
+                put_domain(d->parent);
+                d->parent = NULL;
+            }
         }
 #endif
 
xen/arch/x86/hvm/hvm.c +1 -1
index a339b36a0d..9bfa603f8c 100644
@@ -1915,7 +1915,7 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
     }
 #endif
 
-    /* Spurious fault? PoD and log-dirty also take this path. */
+    /* Spurious fault? PoD, log-dirty and VM forking also take this path. */
     if ( p2m_is_ram(p2mt) )
     {
         rc = 1;
xen/arch/x86/mm/mem_sharing.c +13 -0
index 3835bc928f..ad5db9d8d5 100644
@@ -22,6 +22,7 @@
 
 #include <xen/types.h>
 #include <xen/domain_page.h>
+#include <xen/event.h>
 #include <xen/spinlock.h>
 #include <xen/rwlock.h>
 #include <xen/mm.h>
@@ -36,6 +37,9 @@
 #include <asm/altp2m.h>
 #include <asm/atomic.h>
 #include <asm/event.h>
+#include <asm/hap.h>
+#include <asm/hvm/hvm.h>
+#include <asm/hvm/save.h>
 #include <xsm/xsm.h>
 
 #include "mm-locks.h"
@@ -1444,6 +1448,194 @@ static inline int mem_sharing_control(struct domain *d, bool enable)
     return 0;
 }
 
+/*
+ * Forking a page only gets called when the VM faults due to no entry being
+ * in the EPT for the access. Depending on the type of access we either
+ * populate the physmap with a shared entry for read-only access or
+ * fork the page if its a write access.
+ *
+ * The client p2m is already locked so we only need to lock
+ * the parent's here.
+ */
Quoted 8 lines

xen/arch/x86/mm/mem_sharing.c static inline int mem_sharing_control(struct domain *d, bool enable) · line 1464

Quoted 5 lines
xen/arch/x86/mm/mem_sharing.c +5 -0
+int mem_sharing_fork_page(struct domain *d, gfn_t gfn, bool unsharing)
+{
+    int rc = -ENOENT;
+    shr_handle_t handle;
+    struct domain *parent;

Can you constify parent, I assume there are no changes made to the parent domain, just the forked one.

Sure.

Quoted 13 lines

xen/arch/x86/mm/mem_sharing.c static inline int mem_sharing_control(struct domain *d, bool enable) · line 1474

Quoted 11 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+    struct p2m_domain *p2m;
+    unsigned long gfn_l = gfn_x(gfn);
+    mfn_t mfn, new_mfn;
+    p2m_type_t p2mt;
+    struct page_info *page;
+
+    if ( !mem_sharing_is_fork(d) )
+        return -ENOENT;
+
+    parent = d->parent;

You can initialize at declaration time.

Sure.

Quoted 20 lines
Quoted 20 lines
xen/arch/x86/mm/mem_sharing.c +19 -0
+
+    if ( !unsharing )
+    {
+        /* For read-only accesses we just add a shared entry to the physmap */
+        while ( parent )
+        {
+            if ( !(rc = nominate_page(parent, gfn, 0, &handle)) )
+                break;
+
+            parent = parent->parent;
+        }
+
+        if ( !rc )
+        {
+            /* The client's p2m is already locked */
+            struct p2m_domain *pp2m = p2m_get_hostp2m(parent);
+
+            p2m_lock(pp2m);
+            rc = add_to_physmap(parent, gfn_l, handle, d, gfn_l, false);
Quoted 8 lines

xen/arch/x86/mm/mem_sharing.c static inline int mem_sharing_control(struct domain *d, bool enable) · line 1498

Quoted 5 lines
xen/arch/x86/mm/mem_sharing.c +5 -0
+            p2m_unlock(pp2m);
+
+            if ( !rc )
+                return 0;
+        }

Don't you need to return here, or else you will fallback into the unsharing path?

No, we want to fall-back to unsharing path if populating with a shared entry failed. Notice above we return in case there was no error with add_to_physmap.

Quoted 11 lines
Quoted 11 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+    }
+
+    /*
+     * If it's a write access (ie. unsharing) or if adding a shared entry to
+     * the physmap failed we'll fork the page directly.
+     */
+    p2m = p2m_get_hostp2m(d);
+    parent = d->parent;
+
+    while ( parent )
Quoted 8 lines

xen/arch/x86/mm/mem_sharing.c static inline int mem_sharing_control(struct domain *d, bool enable) · line 1512

Quoted 4 lines
xen/arch/x86/mm/mem_sharing.c +4 -0
+    {
+        mfn = get_gfn_query(parent, gfn_l, &p2mt);
+
+        if ( mfn_valid(mfn) && p2m_is_any_ram(p2mt) )

This would also cover grants, but I'm not sure how those are handled by forking, as access to those is granted on a per-domain basis. Ie:
the parent will have access to the grant, but not the child.

Good question. Grants are not sharable because their reference count will prevent sharing, so here the page content would just get copied as a regular page into the fork. I can check that if in the usecase we have anything breaks if we just skip grants completely, I don't think a regular domain has any grants by default.

Quoted 19 lines
Quoted 19 lines
xen/arch/x86/mm/mem_sharing.c +18 -0
+            break;
+
+        put_gfn(parent, gfn_l);
+        parent = parent->parent;
+    }
+
+    if ( !parent )
+        return -ENOENT;
+
+    if ( !(page = alloc_domheap_page(d, 0)) )
+    {
+        put_gfn(parent, gfn_l);
+        return -ENOMEM;
+    }
+
+    new_mfn = page_to_mfn(page);
+    copy_domain_page(new_mfn, mfn);
+    set_gpfn_from_mfn(mfn_x(new_mfn), gfn_l);
Quoted 8 lines

xen/arch/x86/mm/mem_sharing.c static inline int mem_sharing_control(struct domain *d, bool enable) · line 1534

Quoted 4 lines
xen/arch/x86/mm/mem_sharing.c +4 -0
+
+    put_gfn(parent, gfn_l);
+
+    return p2m->set_entry(p2m, gfn, new_mfn, PAGE_ORDER_4K, p2m_ram_rw,

So the child p2m is going to be populated using 4K pages exclusively? Maybe it would make sense to try to use 2M if the parent domain page is also a 2M page or larger?

No, memory sharing only works on a 4k granularity so that's what we are going with. No reason to copy 2M pages when most of it can just be shared when broken up.

Quoted 20 lines
Quoted 20 lines
xen/arch/x86/mm/mem_sharing.c +19 -0
+                          p2m->default_access, -1);
+}
+
+static int bring_up_vcpus(struct domain *cd, struct cpupool *cpupool)
+{
+    int ret;
+    unsigned int i;
+
+    if ( (ret = cpupool_move_domain(cd, cpupool)) )
+        return ret;
+
+    for ( i = 0; i < cd->max_vcpus; i++ )
+    {
+        if ( cd->vcpu[i] )
+            continue;
+
+        if ( !vcpu_create(cd, i) )
+            return -EINVAL;
+    }
Quoted 8 lines

xen/arch/x86/mm/mem_sharing.c static inline int mem_sharing_control(struct domain *d, bool enable) · line 1559

Quoted 6 lines
xen/arch/x86/mm/mem_sharing.c +6 -0
+
+    domain_update_node_affinity(cd);
+    return 0;
+}
+
+static int fork_hap_allocation(struct domain *cd, struct domain *d)

Can you constify the parent domain?

Sure

Quoted 3 lines

Also cd and d and not very helpful names, I would just use child and parent.

Sure.

Quoted 22 lines
Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c +9 -0
+{
+    int rc;
+    bool preempted;
+    unsigned long mb = hap_get_allocation(d);
+
+    if ( mb == hap_get_allocation(cd) )
+        return 0;
+
+    paging_lock(cd);

xen/arch/x86/mm/mem_sharing.c static inline int mem_sharing_control(struct domain *d, bool enable) · line 1578

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+    rc = hap_set_allocation(cd, mb << (20 - PAGE_SHIFT), &preempted);
+    paging_unlock(cd);
+
+    if ( rc )
+        return rc;
+
+    if ( preempted )
+        return -ERESTART;
+
+    return 0;

You can join all the checks into a single return:

Quoted 1 line

return preempted ? -ERESTART : rc;

Sure.

Quoted 6 lines
Quoted 6 lines
xen/arch/x86/mm/mem_sharing.c +6 -0
+}
+
+static void fork_tsc(struct domain *cd, struct domain *d)
+{
+    uint32_t tsc_mode;
+    uint32_t gtsc_khz;
Quoted 8 lines

xen/arch/x86/mm/mem_sharing.c static inline int mem_sharing_control(struct domain *d, bool enable) · line 1590

Quoted 6 lines
xen/arch/x86/mm/mem_sharing.c +6 -0
+    uint32_t incarnation;
+    uint64_t elapsed_nsec;
+
+    tsc_get_info(d, &tsc_mode, &elapsed_nsec, &gtsc_khz, &incarnation);
+    /* Don't bump incarnation on set */
+    tsc_set_info(cd, tsc_mode, elapsed_nsec, gtsc_khz, incarnation - 1);

Why is the incarnation not bumped?

See Andrew's comment in the previous version of the path. Apparently tsc_set_info bumps it itself.

Quoted 11 lines

xen/arch/x86/mm/mem_sharing.c static inline int mem_sharing_control(struct domain *d, bool enable) · line 1598

Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c +8 -0
+}
+
+static int mem_sharing_fork(struct domain *d, struct domain *cd)
+{
+    int rc = -EINVAL;
+
+    if ( !cd->controller_pause_count )
+        return rc;

-EBUSY might be better here.

Sure.

Quoted 10 lines

xen/arch/x86/mm/mem_sharing.c static inline int mem_sharing_control(struct domain *d, bool enable) · line 1606

Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c +8 -0
+
+    /*
+     * We only want to get and pause the parent once, not each time this
+     * operation is restarted due to preemption.
+     */
+    if ( !cd->parent_paused )
+    {
+        ASSERT(get_domain(d));
Quoted 7 lines

We are trying to avoid such constructs, instead I suggest:

if ( !get_domain(parent) )

{
    ASSERT_UNREACHABLE();
    return -EBUSY;
}

Sure.

Quoted 60 lines
Quoted 47 lines
xen/arch/x86/mm/mem_sharing.c +40 -0
+        domain_pause(d);
+
+        cd->parent_paused = true;
+        cd->max_pages = d->max_pages;
+        cd->max_vcpus = d->max_vcpus;
+    }
+
+    /* this is preemptible so it's the first to get done */
+    if ( (rc = fork_hap_allocation(cd, d)) )
+        goto done;
+
+    if ( (rc = bring_up_vcpus(cd, d->cpupool)) )
+        goto done;
+
+    if ( (rc = hvm_copy_context_and_params(cd, d)) )
+        goto done;
+
+    fork_tsc(cd, d);
+
+    cd->parent = d;
+
+ done:
+    if ( rc && rc != -ERESTART )
+    {
+        domain_unpause(d);
+        put_domain(d);
+        cd->parent_paused = false;
+    }
+
+    return rc;
+}
+
 int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg)
 {
     int rc;
@@ -1698,6 +1890,36 @@ int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg)
         rc = debug_gref(d, mso.u.debug.u.gref);
         break;
 
+    case XENMEM_sharing_op_fork:
+    {
+        struct domain *pd;
+
+        rc = -EINVAL;
+        if ( mso.u.fork._pad[0] || mso.u.fork._pad[1] ||
+             mso.u.fork._pad[2] )
+            goto out;

xen/arch/x86/mm/mem_sharing.c int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg) · line 1910

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+
+        rc = rcu_lock_live_remote_domain_by_id(mso.u.fork.parent_domain,
+                                               &pd);
+        if ( rc )
+            goto out;
+
+        if ( !mem_sharing_enabled(pd) )
+        {
+            if ( (rc = mem_sharing_control(pd, true)) )
+                goto out;

Please join both conditions:

Quoted 3 lines

if ( !mem_sharing_enabled(pd) &&
(rc = mem_sharing_control(pd, true)) )
goto out;

Sure.

Quoted 24 lines
Quoted 24 lines
xen/arch/x86/mm/mem_sharing.c +12 -0
+        }
+
+        rc = mem_sharing_fork(pd, d);
+
+        if ( rc == -ERESTART )
+            rc = hypercall_create_continuation(__HYPERVISOR_memory_op,
+                                               "lh", XENMEM_sharing_op,
+                                               arg);
+        rcu_unlock_domain(pd);
+        break;
+    }
+
     default:
         rc = -ENOSYS;
         break;
xen/arch/x86/mm/p2m.c +0 -0
index c5f428d67c..7c4d2fd7a0 100644
@@ -509,6 +509,14 @@ mfn_t __get_gfn_type_access(struct p2m_domain *p2m, unsigned long gfn_l,
 
     mfn = p2m->get_entry(p2m, gfn, t, a, q, page_order, NULL);
 
Quoted 8 lines

xen/arch/x86/mm/p2m.c mfn_t __get_gfn_type_access(struct p2m_domain *p2m, unsigned long gfn_l, · line 517

Quoted 6 lines
xen/arch/x86/mm/p2m.c +6 -0
+    /* Check if we need to fork the page */
+    if ( (q & P2M_ALLOC) && p2m_is_hole(*t) &&
+         !mem_sharing_fork_page(p2m->domain, gfn, !!(q & P2M_UNSHARE)) )
+    {
+        mfn = p2m->get_entry(p2m, gfn, t, a, q, page_order, NULL);
+    }

No need for the braces.

I would keep them, it helps with readability in this case.

Tamas

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#34 lore raw

Re: [Xen-devel] [PATCH v9 4/5] x86/mem_sharing: reset a fork

In reply to Roger Pau Monné
Quoted 54 lines · Roger Pau Monné
Quoted 52 lines · Tamas K Lengyel
Quoted 26 lines · Roger Pau Monné
Quoted 5 lines · Tamas K Lengyel

Implement hypercall that allows a fork to shed all memory that got allocated for it during its execution and re-load its vCPU context from the parent VM. This allows the forked VM to reset into the same state the parent VM is in a faster way then creating a new fork would be. Measurements show about a 2x speedup during normal fuzzing operations. Performance may vary depending how

^ on

Quoted 18 lines

much memory got allocated for the forked VM. If it has been completely deduplicated from the parent VM then creating a new fork would likely be more performant.

---
 xen/arch/x86/mm/mem_sharing.c | 76 +++++++++++++++++++++++++++++++++++
 xen/include/public/memory.h   |  1 +
 2 files changed, 77 insertions(+)
xen/arch/x86/mm/mem_sharing.c +0 -0
index ad5db9d8d5..fb6892aaa6 100644
@@ -1636,6 +1636,59 @@ static int mem_sharing_fork(struct domain *d, struct domain *cd)
     return rc;
 }
 
Quoted 11 lines

xen/arch/x86/mm/mem_sharing.c static int mem_sharing_fork(struct domain *d, struct domain *cd) · line 1646

Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8 -0
+/*
+ * The fork reset operation is intended to be used on short-lived forks only.
+ * There is no hypercall continuation operation implemented for this reason.
+ * For forks that obtain a larger memory footprint it is likely going to be
+ * more performant to create a new fork instead of resetting an existing one.
+ *
+ * TODO: In case this hypercall would become useful on forks with larger memory
+ * footprints the hypercall continuation should be implemented.

I'm afraid this is not safe, as users don't have an easy way to know whether a fork will have a large memory footprint or not.

They do, getdomaininfo tells a user exactly how much memory has been allocated for a domain.

Quoted 4 lines

IMO you either need some kind of check that prevents this function from being executed when the domain as too much memory assigned, or you need to implement continuations.

I really don't think we need continuation here with the usecase we have for this function but I'm also tired of arguing about it, so I'll just add it even if its going to be dead code.

Quoted 1 line
Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Or else this is very likely to trip over the watchdog.

The watchdog?

Yes, Xen has a watchdog and this loop is likely to trigger it if it takes > 5s to complete. The watchdog triggering is a fatal event and leads to host crash.

OK, just to give you numbers, in the usecase this function is targeted at we call it about ~100/s. Even in our worst case scenario we've seen so far we haven't had a domain with enough memory deduplication where this function took longer then 1s to finish. But again, at this point we spent more time arguing about continuation then it takes to add it so lets just move on.

Quoted 3 lines

Note that watchdog is not enabled by default, you need to enable it on the Xen command line.

In that case I don't think the current setup would really bother anyone.

Quoted 27 lines
Quoted 27 lines
Quoted 26 lines
Quoted 14 lines
xen/arch/x86/mm/mem_sharing.c +13 -0
+    {
+        p2m_type_t p2mt;
+        p2m_access_t p2ma;
+        gfn_t gfn;
+        mfn_t mfn = page_to_mfn(page);
+
+        if ( !mfn_valid(mfn) )
+            continue;
+
+        gfn = mfn_to_gfn(cd, mfn);
+        mfn = __get_gfn_type_access(p2m, gfn_x(gfn), &p2mt, &p2ma,
+                                    0, NULL, false);
+

xen/arch/x86/mm/mem_sharing.c static int mem_sharing_fork(struct domain *d, struct domain *cd) · line 1679

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+        if ( !p2m_is_ram(p2mt) || p2m_is_shared(p2mt) )
+            continue;
+
+        /* take an extra reference */
+        if ( !get_page(page, cd) )
+            continue;
+
+        rc = p2m->set_entry(p2m, gfn, INVALID_MFN, PAGE_ORDER_4K,
+                            p2m_invalid, p2m_access_rwx, -1);
+        ASSERT(!rc);

Can you handle this gracefully?

Quoted 5 lines
Quoted 2 lines

Nope. This should never happen, so if it does, something is very wrong in some other part of Xen.

OK, please switch it to BUG_ON then instead of ASSERT. It's better to crash here than to misbehave later.

Sure

Thanks,
Tamas

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#13 lore raw

Re: [Xen-devel] [PATCH v9 2/5] xen: add parent_domid field to createdomain domctl

In reply to Andrew Cooper
Quoted 10 lines · Andrew Cooper
Quoted 5 lines · Tamas K Lengyel

When creating a domain that will be used as a VM fork some information is required to set things up properly, like the max_vcpus count. Instead of the toolstack having to gather this information for each fork in a separate hypercall we can just include the parent domain's id in the createdomain domctl so that Xen can copy the setting without the extra toolstack queries.

Right, but when I said this wasn't safe, I did mean it...

Quoted 7 lines

What happens when parent and the current domain have different gnttab or evtchn limits, or different emulation settings?

If you want to fork a domain safely, you either need to have no parameters passed by the toolstack (and let Xen copy appropriate values), or cross check every provided parameter and bail early on a mismatch.

If you are using the toolstack code we add in patch 5 that doesn't happen. So, for the situation you describe to happen: 1) you have to custom compile Xen with the EXPERT setting enable this experimental feature 2) write your own toolstack code 3) screw up doing so. This is such an unlikely scenario that I'm not really bothered by it.

Tamas

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#17 lore raw

Re: [Xen-devel] [PATCH v9 3/5] xen/mem_sharing: VM forking

In reply to Tamas K Lengyel
Quoted 22 lines · Tamas K Lengyel
Quoted 12 lines · Roger Pau Monné
Quoted 10 lines · Tamas K Lengyel
xen/arch/x86/mm/mem_sharing.c +10 -0
+    }
+
+    /*
+     * If it's a write access (ie. unsharing) or if adding a shared entry to
+     * the physmap failed we'll fork the page directly.
+     */
+    p2m = p2m_get_hostp2m(d);
+    parent = d->parent;
+
+    while ( parent )
Quoted 8 lines

xen/arch/x86/mm/mem_sharing.c static inline int mem_sharing_control(struct domain *d, bool enable) · line 1512

Quoted 4 lines
xen/arch/x86/mm/mem_sharing.c +4 -0
+    {
+        mfn = get_gfn_query(parent, gfn_l, &p2mt);
+
+        if ( mfn_valid(mfn) && p2m_is_any_ram(p2mt) )

This would also cover grants, but I'm not sure how those are handled by forking, as access to those is granted on a per-domain basis. Ie:
the parent will have access to the grant, but not the child.

Quoted 5 lines

Good question. Grants are not sharable because their reference count will prevent sharing, so here the page content would just get copied as a regular page into the fork. I can check that if in the usecase we have anything breaks if we just skip grants completely, I don't think a regular domain has any grants by default.

Hm, I don't have a good suggestion for this. Since the domain is not aware of the fork there's no way for it to notice grant maps have become stale.

Can you add a note in this regard? And maybe crashing the fork when a grant is found would be fine, until we figure out how to handle them properly.

Quoted 28 lines
Quoted 19 lines
Quoted 19 lines
xen/arch/x86/mm/mem_sharing.c +18 -0
+            break;
+
+        put_gfn(parent, gfn_l);
+        parent = parent->parent;
+    }
+
+    if ( !parent )
+        return -ENOENT;
+
+    if ( !(page = alloc_domheap_page(d, 0)) )
+    {
+        put_gfn(parent, gfn_l);
+        return -ENOMEM;
+    }
+
+    new_mfn = page_to_mfn(page);
+    copy_domain_page(new_mfn, mfn);
+    set_gpfn_from_mfn(mfn_x(new_mfn), gfn_l);
Quoted 8 lines

xen/arch/x86/mm/mem_sharing.c static inline int mem_sharing_control(struct domain *d, bool enable) · line 1534

Quoted 4 lines
xen/arch/x86/mm/mem_sharing.c +4 -0
+
+    put_gfn(parent, gfn_l);
+
+    return p2m->set_entry(p2m, gfn, new_mfn, PAGE_ORDER_4K, p2m_ram_rw,

So the child p2m is going to be populated using 4K pages exclusively? Maybe it would make sense to try to use 2M if the parent domain page is also a 2M page or larger?

Quoted 3 lines

No, memory sharing only works on a 4k granularity so that's what we are going with. No reason to copy 2M pages when most of it can just be shared when broken up.

Oh, OK. For your use-case it likely doesn't matter that much, but long-running forks would get better performance if using large pages.

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
xen/arch/x86/mm/p2m.c +0 -0
@@ -509,6 +509,14 @@ mfn_t __get_gfn_type_access(struct p2m_domain *p2m, unsigned long gfn_l,
 
     mfn = p2m->get_entry(p2m, gfn, t, a, q, page_order, NULL);
 
Quoted 8 lines

xen/arch/x86/mm/p2m.c mfn_t __get_gfn_type_access(struct p2m_domain *p2m, unsigned long gfn_l, · line 517

Quoted 6 lines
xen/arch/x86/mm/p2m.c +6 -0
+    /* Check if we need to fork the page */
+    if ( (q & P2M_ALLOC) && p2m_is_hole(*t) &&
+         !mem_sharing_fork_page(p2m->domain, gfn, !!(q & P2M_UNSHARE)) )
+    {
+        mfn = p2m->get_entry(p2m, gfn, t, a, q, page_order, NULL);
+    }

No need for the braces.

Quoted 1 line

I would keep them, it helps with readability in this case.

CODING_STYLE mentions that braces should be omitted for blocks with a single statement, but I'm not sure how strictly we enforce this.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#35 lore raw

Re: [Xen-devel] [PATCH v9 4/5] x86/mem_sharing: reset a fork

In reply to Tamas K Lengyel
Quoted 63 lines · Tamas K Lengyel
Quoted 54 lines · Roger Pau Monné
Quoted 52 lines · Tamas K Lengyel
Quoted 26 lines · Roger Pau Monné
Quoted 5 lines · Tamas K Lengyel

Implement hypercall that allows a fork to shed all memory that got allocated for it during its execution and re-load its vCPU context from the parent VM. This allows the forked VM to reset into the same state the parent VM is in a faster way then creating a new fork would be. Measurements show about a 2x speedup during normal fuzzing operations. Performance may vary depending how

^ on

Quoted 18 lines

much memory got allocated for the forked VM. If it has been completely deduplicated from the parent VM then creating a new fork would likely be more performant.

---
 xen/arch/x86/mm/mem_sharing.c | 76 +++++++++++++++++++++++++++++++++++
 xen/include/public/memory.h   |  1 +
 2 files changed, 77 insertions(+)
xen/arch/x86/mm/mem_sharing.c +0 -0
index ad5db9d8d5..fb6892aaa6 100644
@@ -1636,6 +1636,59 @@ static int mem_sharing_fork(struct domain *d, struct domain *cd)
     return rc;
 }
 
Quoted 11 lines

xen/arch/x86/mm/mem_sharing.c static int mem_sharing_fork(struct domain *d, struct domain *cd) · line 1646

Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8 -0
+/*
+ * The fork reset operation is intended to be used on short-lived forks only.
+ * There is no hypercall continuation operation implemented for this reason.
+ * For forks that obtain a larger memory footprint it is likely going to be
+ * more performant to create a new fork instead of resetting an existing one.
+ *
+ * TODO: In case this hypercall would become useful on forks with larger memory
+ * footprints the hypercall continuation should be implemented.

I'm afraid this is not safe, as users don't have an easy way to know whether a fork will have a large memory footprint or not.

They do, getdomaininfo tells a user exactly how much memory has been allocated for a domain.

Quoted 4 lines

IMO you either need some kind of check that prevents this function from being executed when the domain as too much memory assigned, or you need to implement continuations.

I really don't think we need continuation here with the usecase we have for this function but I'm also tired of arguing about it, so I'll just add it even if its going to be dead code.

Quoted 1 line
Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Or else this is very likely to trip over the watchdog.

The watchdog?

Yes, Xen has a watchdog and this loop is likely to trigger it if it takes > 5s to complete. The watchdog triggering is a fatal event and leads to host crash.

Quoted 6 lines

OK, just to give you numbers, in the usecase this function is targeted at we call it about ~100/s. Even in our worst case scenario we've seen so far we haven't had a domain with enough memory deduplication where this function took longer then 1s to finish. But again, at this point we spent more time arguing about continuation then it takes to add it so lets just move on.

Right, adding continuation support is fairly trivial, and it would keep us on the safe side.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#41 lore raw

Re: [Xen-devel] [PATCH v9 5/5] xen/tools: VM forking toolstack side

In reply to Tamas K Lengyel

Hi Tamas,

tools/libxl/libxl_create.c int libxl_domain_create_new(libxl_ctx *ctx, libxl_domain_config *d_config, · line 2138

Quoted 8 lines · Tamas K Lengyel
tools/libxl/libxl_create.c +8 -0
+int libxl_domain_fork_vm(libxl_ctx *ctx, uint32_t pdomid, uint32_t *domid)
+{
+    int rc;
+    struct xen_domctl_createdomain create = {0};
+    create.flags |= XEN_DOMCTL_CDF_hvm;
+    create.flags |= XEN_DOMCTL_CDF_hap;
+    create.flags |= XEN_DOMCTL_CDF_oos_off;
+    create.arch.emulation_flags = (XEN_X86_EMU_ALL & ~XEN_X86_EMU_VPCI);

This is x86 specific but seems to be written in common code. Did you build it on Arm?

tools/libxl/libxl_create.c int libxl_domain_create_new(libxl_ctx *ctx, libxl_domain_config *d_config, · line 2144

Quoted 6 lines
tools/libxl/libxl_create.c +6 -0
+
+    create.ssidref = SECINITSID_DOMU;
+    create.parent_domid = pdomid;
+    create.max_evtchn_port = 1023;
+    create.max_grant_frames = LIBXL_MAX_GRANT_FRAMES_DEFAULT;
+    create.max_maptrack_frames = LIBXL_MAX_MAPTRACK_FRAMES_DEFAULT;

The domain you fork may have different values here. From my understanding, the fork requires to have the same parameters as the parent. So how do you ensure they are the same?

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#42 lore raw

Re: [Xen-devel] [PATCH v9 5/5] xen/tools: VM forking toolstack side

Quoted 4 lines · Julien Grall

Hi Tamas,

On 21/02/2020 18:49, Tamas K Lengyel wrote:

Quoted 11 lines

tools/libxl/libxl_create.c int libxl_domain_create_new(libxl_ctx *ctx, libxl_domain_config *d_config, · line 2138

Quoted 8 lines
tools/libxl/libxl_create.c +8 -0
+int libxl_domain_fork_vm(libxl_ctx *ctx, uint32_t pdomid, uint32_t *domid)
+{
+    int rc;
+    struct xen_domctl_createdomain create = {0};
+    create.flags |= XEN_DOMCTL_CDF_hvm;
+    create.flags |= XEN_DOMCTL_CDF_hap;
+    create.flags |= XEN_DOMCTL_CDF_oos_off;
+    create.arch.emulation_flags = (XEN_X86_EMU_ALL & ~XEN_X86_EMU_VPCI);

This is x86 specific but seems to be written in common code. Did you build it on Arm?

I have not, don't have a setup for ARM at the moment. I guess I'll just move this function to libxl_x86.c to resolve the issue.

Quoted 11 lines

tools/libxl/libxl_create.c int libxl_domain_create_new(libxl_ctx *ctx, libxl_domain_config *d_config, · line 2144

Quoted 7 lines
tools/libxl/libxl_create.c +6 -0
+
+    create.ssidref = SECINITSID_DOMU;
+    create.parent_domid = pdomid;
+    create.max_evtchn_port = 1023;
+    create.max_grant_frames = LIBXL_MAX_GRANT_FRAMES_DEFAULT;
+    create.max_maptrack_frames = LIBXL_MAX_MAPTRACK_FRAMES_DEFAULT;

The domain you fork may have different values here. From my understanding, the fork requires to have the same parameters as the parent. So how do you ensure they are the same?

The parent domain is created by xl. If you create a domain with xl it will have these parameters set by default.

Tamas

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#43 lore raw

Re: [Xen-devel] [PATCH v9 5/5] xen/tools: VM forking toolstack side

Hi,

Quoted 17 lines · Tamas K Lengyel
Quoted 4 lines · Julien Grall

Hi Tamas,

On 21/02/2020 18:49, Tamas K Lengyel wrote:

Quoted 11 lines

tools/libxl/libxl_create.c int libxl_domain_create_new(libxl_ctx *ctx, libxl_domain_config *d_config, · line 2138

Quoted 8 lines
tools/libxl/libxl_create.c +8 -0
+int libxl_domain_fork_vm(libxl_ctx *ctx, uint32_t pdomid, uint32_t *domid)
+{
+    int rc;
+    struct xen_domctl_createdomain create = {0};
+    create.flags |= XEN_DOMCTL_CDF_hvm;
+    create.flags |= XEN_DOMCTL_CDF_hap;
+    create.flags |= XEN_DOMCTL_CDF_oos_off;
+    create.arch.emulation_flags = (XEN_X86_EMU_ALL & ~XEN_X86_EMU_VPCI);

This is x86 specific but seems to be written in common code. Did you build it on Arm?

Quoted 2 lines

I have not, don't have a setup for ARM at the moment. I guess I'll just move this function to libxl_x86.c to resolve the issue.

It is fairly easy to setup a build environment for Arm. You can use QEMU user emulation and a Arm rootfs.

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
tools/libxl/libxl_create.c +6 -0
+
+    create.ssidref = SECINITSID_DOMU;
+    create.parent_domid = pdomid;
+    create.max_evtchn_port = 1023;
+    create.max_grant_frames = LIBXL_MAX_GRANT_FRAMES_DEFAULT;
+    create.max_maptrack_frames = LIBXL_MAX_MAPTRACK_FRAMES_DEFAULT;
Quoted 6 lines
Quoted 3 lines

The domain you fork may have different values here. From my understanding, the fork requires to have the same parameters as the parent. So how do you ensure they are the same?

The parent domain is created by xl. If you create a domain with xl it will have these parameters set by default.

I hope you are aware that you can override most of those parameters in the guest configuration file. It would be good to at least write down the limitations so people doesn't spend ages figuring out why it does not work.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#44 lore raw

Re: [Xen-devel] [PATCH v9 5/5] xen/tools: VM forking toolstack side

Quoted 40 lines · Julien Grall

Hi,

Quoted 17 lines · Tamas K Lengyel
Quoted 4 lines · Julien Grall

Hi Tamas,

On 21/02/2020 18:49, Tamas K Lengyel wrote:

Quoted 11 lines

tools/libxl/libxl_create.c int libxl_domain_create_new(libxl_ctx *ctx, libxl_domain_config *d_config, · line 2138

Quoted 8 lines
tools/libxl/libxl_create.c +8 -0
+int libxl_domain_fork_vm(libxl_ctx *ctx, uint32_t pdomid, uint32_t *domid)
+{
+    int rc;
+    struct xen_domctl_createdomain create = {0};
+    create.flags |= XEN_DOMCTL_CDF_hvm;
+    create.flags |= XEN_DOMCTL_CDF_hap;
+    create.flags |= XEN_DOMCTL_CDF_oos_off;
+    create.arch.emulation_flags = (XEN_X86_EMU_ALL & ~XEN_X86_EMU_VPCI);

This is x86 specific but seems to be written in common code. Did you build it on Arm?

Quoted 2 lines

I have not, don't have a setup for ARM at the moment. I guess I'll just move this function to libxl_x86.c to resolve the issue.

It is fairly easy to setup a build environment for Arm. You can use QEMU user emulation and a Arm rootfs.

Quoted 13 lines
Quoted 12 lines

tools/libxl/libxl_create.c int libxl_domain_create_new(libxl_ctx *ctx, libxl_domain_config *d_config, · line 2144

Quoted 8 lines
tools/libxl/libxl_create.c +6 -0
+
+    create.ssidref = SECINITSID_DOMU;
+    create.parent_domid = pdomid;
+    create.max_evtchn_port = 1023;
+    create.max_grant_frames = LIBXL_MAX_GRANT_FRAMES_DEFAULT;
+    create.max_maptrack_frames = LIBXL_MAX_MAPTRACK_FRAMES_DEFAULT;

The domain you fork may have different values here. From my understanding, the fork requires to have the same parameters as the parent. So how do you ensure they are the same?

Quoted 7 lines
Quoted 2 lines

The parent domain is created by xl. If you create a domain with xl it will have these parameters set by default.

I hope you are aware that you can override most of those parameters in the guest configuration file. It would be good to at least write down the limitations so people doesn't spend ages figuring out why it does not work.

Fair enough.

Tamas

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#18 lore raw

Re: [Xen-devel] [PATCH v9 3/5] xen/mem_sharing: VM forking

In reply to Tamas K Lengyel
Quoted 10 lines
Quoted 8 lines

xen/arch/x86/mm/mem_sharing.c static inline int mem_sharing_control(struct domain *d, bool enable) · line 1464

Quoted 5 lines
xen/arch/x86/mm/mem_sharing.c +5 -0
+int mem_sharing_fork_page(struct domain *d, gfn_t gfn, bool unsharing)
+{
+    int rc = -ENOENT;
+    shr_handle_t handle;
+    struct domain *parent;

Can you constify parent, I assume there are no changes made to the parent domain, just the forked one.

Sure.

Actually, no can't do. The parent could get modified here, since we could end up making the parent's memory into shared entries. So using const here is not justified since the parent can and does get modified.

Tamas

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#20 lore raw

Re: [Xen-devel] [PATCH v9 3/5] xen/mem_sharing: VM forking

In reply to Tamas K Lengyel
Quoted 4 lines
Quoted 2 lines

Also cd and d and not very helpful names, I would just use child and parent.

Sure.

Looking at this in the context of the whole pre-existing code-base, the names cd & d are used throughout mem_sharing. So for consistency I will keep it for now.

Tamas

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#19 lore raw

Re: [Xen-devel] [PATCH v9 3/5] xen/mem_sharing: VM forking

In reply to Tamas K Lengyel
Quoted 9 lines · Tamas K Lengyel
Quoted 9 lines
Quoted 8 lines

xen/arch/x86/mm/mem_sharing.c static inline int mem_sharing_control(struct domain *d, bool enable) · line 1464

Quoted 5 lines
xen/arch/x86/mm/mem_sharing.c +5 -0
+int mem_sharing_fork_page(struct domain *d, gfn_t gfn, bool unsharing)
+{
+    int rc = -ENOENT;
+    shr_handle_t handle;
+    struct domain *parent;

Can you constify parent, I assume there are no changes made to the parent domain, just the forked one.

Quoted 6 lines
Quoted 1 line

Sure.

Actually, no can't do. The parent could get modified here, since we could end up making the parent's memory into shared entries. So using const here is not justified since the parent can and does get modified.

Oh, that's fine.

Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#21 lore raw

Re: [Xen-devel] [PATCH v9 3/5] xen/mem_sharing: VM forking

In reply to Tamas K Lengyel
Quoted 8 lines · Tamas K Lengyel
Quoted 4 lines
Quoted 2 lines

Also cd and d and not very helpful names, I would just use child and parent.

Sure.

Looking at this in the context of the whole pre-existing code-base, the names cd & d are used throughout mem_sharing. So for consistency I will keep it for now.

Ack, if that's the prevailing style of the file and maintainers are fine with it.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#25 lore raw

Re: [Xen-devel] [PATCH v9 3/5] xen/mem_sharing: VM forking

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

VM forking is the process of creating a domain with an empty memory space and a parent domain specified from which to populate the memory when necessary. For the new domain to be functional the VM state is copied over as part of the fork operation (HVM params, hap allocation, etc).

I've just realized that I'm not sure how are special pages handled, what happens to the xenstore, console, shared info pages, or the vcpu info pages if the parent has those registered?

Also, what happens to pages that are being used for DMA with emulated devices? Will QEMU foreign mappings also trigger the populate on demand routine, so that external emulators can access those?

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#26 lore raw

Re: [Xen-devel] [PATCH v9 3/5] xen/mem_sharing: VM forking

Quoted 10 lines · Roger Pau Monné
Quoted 4 lines · Tamas K Lengyel

VM forking is the process of creating a domain with an empty memory space and a parent domain specified from which to populate the memory when necessary. For the new domain to be functional the VM state is copied over as part of the fork operation (HVM params, hap allocation, etc).

I've just realized that I'm not sure how are special pages handled, what happens to the xenstore, console, shared info pages, or the vcpu info pages if the parent has those registered?

The fork starts out with an empty p2m so these pages are not present. In case the guest itself tries to access these pages, or a foreign mapping is set up for them, then that will trigger fork_page from the parent. I see that in the VM restore path clears the pages for HVM_PARAM_CONSOLE_PFN, HVM_PARAM_STORE_PFN, HVM_PARAM_IOREQ_PFN and HVM_PARAM_BUFIOREQ_PFN but doesn't really explain why that would be required. I can certainly add the same special handling for these HVM params when they get copied during the fork hypercall.

As for the vcpu info page, I'm not sure where that gets allocated and mapped normally. I don't see any special handling for that in the save/restore paths. We use the standard createdomain hypercall to setup the VM that will be used for the fork, then use vcpu_create to bring up the vCPUs and just load them with the hvm context, pretty much the same way the restore path would.

Quoted 3 lines

Also, what happens to pages that are being used for DMA with emulated devices? Will QEMU foreign mappings also trigger the populate on demand routine, so that external emulators can access those?

Foreign mappings do trigger the fork_page routine, yes. Same for setting mem_access permissions.

Tamas

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#27 lore raw

Re: [Xen-devel] [PATCH v9 3/5] xen/mem_sharing: VM forking

Quoted 12 lines · Tamas K Lengyel
Quoted 10 lines · Roger Pau Monné
Quoted 4 lines · Tamas K Lengyel

VM forking is the process of creating a domain with an empty memory space and a parent domain specified from which to populate the memory when necessary. For the new domain to be functional the VM state is copied over as part of the fork operation (HVM params, hap allocation, etc).

I've just realized that I'm not sure how are special pages handled, what happens to the xenstore, console, shared info pages, or the vcpu info pages if the parent has those registered?

Quoted 8 lines

The fork starts out with an empty p2m so these pages are not present. In case the guest itself tries to access these pages, or a foreign mapping is set up for them, then that will trigger fork_page from the parent. I see that in the VM restore path clears the pages for HVM_PARAM_CONSOLE_PFN, HVM_PARAM_STORE_PFN, HVM_PARAM_IOREQ_PFN and HVM_PARAM_BUFIOREQ_PFN but doesn't really explain why that would be required. I can certainly add the same special handling for these HVM params when they get copied during the fork hypercall.

Those params are likely set by the toolstack when creating the domain. I think you will have to at least copy the values from the parent and maybe pre-populate them when forking, that depends on whether internal Xen accesses will also trigger the populate logic.

Quoted 3 lines

As for the vcpu info page, I'm not sure where that gets allocated and mapped normally. I don't see any special handling for that in the save/restore paths.

The shared info page, the vcpu info and the stolen time pages are tear down during suspend/restore, and the guest re-registers them when resuming. Since the guest is not aware of the fork happening, you will have to populate those on fork and make sure the domain fields pointing to them are updated, so that Xen can continue updating this shared areas.

Quoted 4 lines

We use the standard createdomain hypercall to
setup the VM that will be used for the fork, then use vcpu_create to bring up the vCPUs and just load them with the hvm context, pretty much the same way the restore path would.

Depending on how much of the creation process you skip you might end up missing bits, there are a bunch of hypercalls involved in domain creation.

Note also that during a restore the guest is aware of such process, and will know that it needs to re-register some stuff, but that's not the case when forking, since the guest is not aware you need to make sure everything is in place. There are also the grant table pages, which I think should also be handled somehow (or we need to at least note this isn't handled and will need fixing).

Quoted 6 lines
Quoted 3 lines

Also, what happens to pages that are being used for DMA with emulated devices? Will QEMU foreign mappings also trigger the populate on demand routine, so that external emulators can access those?

Foreign mappings do trigger the fork_page routine, yes. Same for setting mem_access permissions.

OK, that's fine then.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#28 lore raw

Re: [Xen-devel] [PATCH v9 3/5] xen/mem_sharing: VM forking

Quoted 23 lines · Roger Pau Monné
Quoted 12 lines · Tamas K Lengyel
Quoted 10 lines · Roger Pau Monné
Quoted 4 lines · Tamas K Lengyel

VM forking is the process of creating a domain with an empty memory space and a parent domain specified from which to populate the memory when necessary. For the new domain to be functional the VM state is copied over as part of the fork operation (HVM params, hap allocation, etc).

I've just realized that I'm not sure how are special pages handled, what happens to the xenstore, console, shared info pages, or the vcpu info pages if the parent has those registered?

Quoted 8 lines

The fork starts out with an empty p2m so these pages are not present. In case the guest itself tries to access these pages, or a foreign mapping is set up for them, then that will trigger fork_page from the parent. I see that in the VM restore path clears the pages for HVM_PARAM_CONSOLE_PFN, HVM_PARAM_STORE_PFN, HVM_PARAM_IOREQ_PFN and HVM_PARAM_BUFIOREQ_PFN but doesn't really explain why that would be required. I can certainly add the same special handling for these HVM params when they get copied during the fork hypercall.

Quoted 4 lines

Those params are likely set by the toolstack when creating the domain. I think you will have to at least copy the values from the parent and maybe pre-populate them when forking, that depends on whether internal Xen accesses will also trigger the populate logic.

All params and the hvm context gets copied from the parent already. Whether or not these pages need to be manually populated, I'm not sure. I haven't noticed any issues when forking a standard VM and not pre-populating these pages. But I guess it doesn't hurt to be as close to the save/restore routine as possible.

Quoted 11 lines
Quoted 4 lines

As for the vcpu info page, I'm not sure where that gets allocated and mapped normally. I don't see any special handling for that in the save/restore paths.

The shared info page, the vcpu info and the stolen time pages are tear down during suspend/restore, and the guest re-registers them when resuming. Since the guest is not aware of the fork happening, you will have to populate those on fork and make sure the domain fields pointing to them are updated, so that Xen can continue updating this shared areas.

Could you point me to where this code lives or where these pages are being mapped into the guest? I've been grepping for a while now and it's not clear to me still.

Quoted 9 lines
Quoted 5 lines

We use the standard createdomain hypercall to
setup the VM that will be used for the fork, then use vcpu_create to bring up the vCPUs and just load them with the hvm context, pretty much the same way the restore path would.

Depending on how much of the creation process you skip you might end up missing bits, there are a bunch of hypercalls involved in domain creation.

I modeled the forking processes heavily on the save/restore routines. So everything that gets called during a VM restore gets called during a VM fork, except it happens within Xen instead of the toolstack issuing a bunch of hypercalls since that's a lot a faster. The only difference between a save/restore and a fork should be that the p2m of the fork isn't populated since that happens during runtime.

Quoted 7 lines

Note also that during a restore the guest is aware of such process, and will know that it needs to re-register some stuff, but that's not the case when forking, since the guest is not aware you need to make sure everything is in place. There are also the grant table pages, which I think should also be handled somehow (or we need to at least note this isn't handled and will need fixing).

True, the fork is not aware that something happened (and we want to keep it that way). But right now everything seems to "just work" as far as a standard VM is used. There must be a million cornercases left that I haven't covered for sure.

Tamas

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Acked-by
#3 lore raw

Re: [Xen-devel] [PATCH v9 1/5] xen/x86: Make hap_get_allocation accessible

In reply to Roger Pau Monné
Quoted 10 lines · Roger Pau Monné
Quoted 7 lines · Tamas K Lengyel

During VM forking we'll copy the parent domain's parameters to the client, including the HAP shadow memory setting that is used for storing the domain's EPT. We'll copy this in the hypervisor instead doing it during toolstack launch to allow the domain to start executing and unsharing memory before (or even completely without) the toolstack.

And with maintainership now having changed

Quoted 2 lines

I would also be fine with this merged into the respective patch where hap_get_allocation gets called.

Indeed that's the reason why this hasn't gone in yet, I think. There's no use from putting it in without also putting in the patch where this is actually needed. In fact it introduces a "stray lack of static" instance until then, which typically we try to clean up.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw

Re: [Xen-devel] [PATCH v9 1/5] xen/x86: Make hap_get_allocation accessible

Quoted 17 lines · Jan Beulich
Quoted 10 lines · Roger Pau Monné
Quoted 7 lines · Tamas K Lengyel

During VM forking we'll copy the parent domain's parameters to the client, including the HAP shadow memory setting that is used for storing the domain's EPT. We'll copy this in the hypervisor instead doing it during toolstack launch to allow the domain to start executing and unsharing memory before (or even completely without) the toolstack.

And with maintainership now having changed

Quoted 8 lines
Quoted 2 lines

I would also be fine with this merged into the respective patch where hap_get_allocation gets called.

Indeed that's the reason why this hasn't gone in yet, I think. There's no use from putting it in without also putting in the patch where this is actually needed. In fact it introduces a "stray lack of static" instance until then, which typically we try to clean up.

I can certainly fold it in to the larger patch if that's preferred.

Tamas

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#23 lore raw

Re: [Xen-devel] [PATCH v9 3/5] xen/mem_sharing: VM forking

In reply to Roger Pau Monné
Quoted 19 lines · Roger Pau Monné

xen/arch/x86/mm/mem_sharing.c static inline int mem_sharing_control(struct domain *d, bool enable) · line 1598

Quoted 6 lines · Tamas K Lengyel
xen/arch/x86/mm/mem_sharing.c +6 -0
+static int mem_sharing_fork(struct domain *d, struct domain *cd)
+{
+    int rc = -EINVAL;
+
+    if ( !cd->controller_pause_count )
+        return rc;

-EBUSY might be better here.

xen/arch/x86/mm/mem_sharing.c static inline int mem_sharing_control(struct domain *d, bool enable) · line 1606

Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8 -0
+
+    /*
+     * We only want to get and pause the parent once, not each time this
+     * operation is restarted due to preemption.
+     */
+    if ( !cd->parent_paused )
+    {
+        ASSERT(get_domain(d));
Quoted 7 lines

We are trying to avoid such constructs, instead I suggest:

if ( !get_domain(parent) )

{
    ASSERT_UNREACHABLE();
    return -EBUSY;
}

But isn't the ASSERT() here wrong anyway? I.e. what is it that guarantees that d hasn't gone away? If it's the caller of this function, then wouldn't it be get_knownalive_domain() that wants using here?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#22 lore raw

Re: [Xen-devel] [PATCH v9 3/5] xen/mem_sharing: VM forking

In reply to Tamas K Lengyel
Quoted 17 lines · Tamas K Lengyel
Quoted 7 lines · Roger Pau Monné
Quoted 6 lines · Tamas K Lengyel
xen/arch/x86/mm/p2m.c +0 -0
@@ -509,6 +509,14 @@ mfn_t __get_gfn_type_access(struct p2m_domain *p2m, unsigned long gfn_l,
 
     mfn = p2m->get_entry(p2m, gfn, t, a, q, page_order, NULL);
 
Quoted 8 lines

xen/arch/x86/mm/p2m.c mfn_t __get_gfn_type_access(struct p2m_domain *p2m, unsigned long gfn_l, · line 517

Quoted 6 lines
xen/arch/x86/mm/p2m.c +6 -0
+    /* Check if we need to fork the page */
+    if ( (q & P2M_ALLOC) && p2m_is_hole(*t) &&
+         !mem_sharing_fork_page(p2m->domain, gfn, !!(q & P2M_UNSHARE)) )
+    {
+        mfn = p2m->get_entry(p2m, gfn, t, a, q, page_order, NULL);
+    }

No need for the braces.

Quoted 1 line

I would keep them, it helps with readability in this case.

I agree with Roger here, fwiw.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#36 lore raw

Re: [Xen-devel] [PATCH v9 4/5] x86/mem_sharing: reset a fork

In reply to Tamas K Lengyel
Quoted 20 lines · Tamas K Lengyel
Quoted 7 lines · Roger Pau Monné
Quoted 6 lines · Tamas K Lengyel
xen/arch/x86/mm/mem_sharing.c +0 -0
@@ -1636,6 +1636,59 @@ static int mem_sharing_fork(struct domain *d, struct domain *cd)
     return rc;
 }
 
Quoted 11 lines

xen/arch/x86/mm/mem_sharing.c static int mem_sharing_fork(struct domain *d, struct domain *cd) · line 1646

Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8 -0
+/*
+ * The fork reset operation is intended to be used on short-lived forks only.
+ * There is no hypercall continuation operation implemented for this reason.
+ * For forks that obtain a larger memory footprint it is likely going to be
+ * more performant to create a new fork instead of resetting an existing one.
+ *
+ * TODO: In case this hypercall would become useful on forks with larger memory
+ * footprints the hypercall continuation should be implemented.

I'm afraid this is not safe, as users don't have an easy way to know whether a fork will have a large memory footprint or not.

Quoted 2 lines

They do, getdomaininfo tells a user exactly how much memory has been allocated for a domain.

This tells the tool stack how much memory a guest has in absolute numbers, but it doesn't tell it whether Xen would consider this "large".

Quoted 26 lines
Quoted 17 lines
Quoted 17 lines
xen/arch/x86/mm/mem_sharing.c +17 -0
+    {
+        p2m_type_t p2mt;
+        p2m_access_t p2ma;
+        gfn_t gfn;
+        mfn_t mfn = page_to_mfn(page);
+
+        if ( !mfn_valid(mfn) )
+            continue;
+
+        gfn = mfn_to_gfn(cd, mfn);
+        mfn = __get_gfn_type_access(p2m, gfn_x(gfn), &p2mt, &p2ma,
+                                    0, NULL, false);
+
+        if ( !p2m_is_ram(p2mt) || p2m_is_shared(p2mt) )
+            continue;
+
+        /* take an extra reference */
Quoted 8 lines

xen/arch/x86/mm/mem_sharing.c static int mem_sharing_fork(struct domain *d, struct domain *cd) · line 1679

Quoted 6 lines
xen/arch/x86/mm/mem_sharing.c +6 -0
+        if ( !get_page(page, cd) )
+            continue;
+
+        rc = p2m->set_entry(p2m, gfn, INVALID_MFN, PAGE_ORDER_4K,
+                            p2m_invalid, p2m_access_rwx, -1);
+        ASSERT(!rc);

Can you handle this gracefully?

Quoted 2 lines

Nope. This should never happen, so if it does, something is very wrong in some other part of Xen.

In such a case, please put in a comment explaining why failure is impossible. In the general case e.g. a 2Mb page may need splitting, which may yield -ENOMEM. Such a comment will then also be useful in case a new failure mode gets added to ->set_entry(), where it then will need judging whether the assumption here still holds. (This is also why in general it'd be better to handle the error. It'll still be better to crash the guest than the host in case you can't. See the bottom of ./CODING_STYLE.)

Janan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#24 lore raw

Re: [Xen-devel] [PATCH v9 3/5] xen/mem_sharing: VM forking

In reply to Jan Beulich
Quoted 29 lines · Jan Beulich
Quoted 19 lines · Roger Pau Monné

xen/arch/x86/mm/mem_sharing.c static inline int mem_sharing_control(struct domain *d, bool enable) · line 1598

Quoted 6 lines · Tamas K Lengyel
xen/arch/x86/mm/mem_sharing.c +6 -0
+static int mem_sharing_fork(struct domain *d, struct domain *cd)
+{
+    int rc = -EINVAL;
+
+    if ( !cd->controller_pause_count )
+        return rc;

-EBUSY might be better here.

xen/arch/x86/mm/mem_sharing.c static inline int mem_sharing_control(struct domain *d, bool enable) · line 1606

Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8 -0
+
+    /*
+     * We only want to get and pause the parent once, not each time this
+     * operation is restarted due to preemption.
+     */
+    if ( !cd->parent_paused )
+    {
+        ASSERT(get_domain(d));
Quoted 7 lines

We are trying to avoid such constructs, instead I suggest:

if ( !get_domain(parent) )

{
    ASSERT_UNREACHABLE();
    return -EBUSY;
}
Quoted 4 lines

But isn't the ASSERT() here wrong anyway? I.e. what is it that guarantees that d hasn't gone away? If it's the caller of this function, then wouldn't it be get_knownalive_domain() that wants using here?

During the fork hypercall the parent's pause count is incremented via domain_pause. It can't go away until the reference count goes down to zero, which only happens when it's forks are all gone.

Tamas

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#37 lore raw

Re: [Xen-devel] [PATCH v9 4/5] x86/mem_sharing: reset a fork

In reply to Jan Beulich
Quoted 59 lines · Jan Beulich
Quoted 20 lines · Tamas K Lengyel
Quoted 7 lines · Roger Pau Monné
Quoted 6 lines · Tamas K Lengyel
xen/arch/x86/mm/mem_sharing.c +0 -0
@@ -1636,6 +1636,59 @@ static int mem_sharing_fork(struct domain *d, struct domain *cd)
     return rc;
 }
 
Quoted 11 lines

xen/arch/x86/mm/mem_sharing.c static int mem_sharing_fork(struct domain *d, struct domain *cd) · line 1646

Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8 -0
+/*
+ * The fork reset operation is intended to be used on short-lived forks only.
+ * There is no hypercall continuation operation implemented for this reason.
+ * For forks that obtain a larger memory footprint it is likely going to be
+ * more performant to create a new fork instead of resetting an existing one.
+ *
+ * TODO: In case this hypercall would become useful on forks with larger memory
+ * footprints the hypercall continuation should be implemented.

I'm afraid this is not safe, as users don't have an easy way to know whether a fork will have a large memory footprint or not.

Quoted 2 lines

They do, getdomaininfo tells a user exactly how much memory has been allocated for a domain.

This tells the tool stack how much memory a guest has in absolute numbers, but it doesn't tell it whether Xen would consider this "large".

Quoted 26 lines
Quoted 17 lines
Quoted 17 lines
xen/arch/x86/mm/mem_sharing.c +17 -0
+    {
+        p2m_type_t p2mt;
+        p2m_access_t p2ma;
+        gfn_t gfn;
+        mfn_t mfn = page_to_mfn(page);
+
+        if ( !mfn_valid(mfn) )
+            continue;
+
+        gfn = mfn_to_gfn(cd, mfn);
+        mfn = __get_gfn_type_access(p2m, gfn_x(gfn), &p2mt, &p2ma,
+                                    0, NULL, false);
+
+        if ( !p2m_is_ram(p2mt) || p2m_is_shared(p2mt) )
+            continue;
+
+        /* take an extra reference */
Quoted 8 lines

xen/arch/x86/mm/mem_sharing.c static int mem_sharing_fork(struct domain *d, struct domain *cd) · line 1679

Quoted 6 lines
xen/arch/x86/mm/mem_sharing.c +6 -0
+        if ( !get_page(page, cd) )
+            continue;
+
+        rc = p2m->set_entry(p2m, gfn, INVALID_MFN, PAGE_ORDER_4K,
+                            p2m_invalid, p2m_access_rwx, -1);
+        ASSERT(!rc);

Can you handle this gracefully?

Quoted 2 lines

Nope. This should never happen, so if it does, something is very wrong in some other part of Xen.

Quoted 8 lines

In such a case, please put in a comment explaining why failure is impossible. In the general case e.g. a 2Mb page may need splitting, which may yield -ENOMEM. Such a comment will then also be useful in case a new failure mode gets added to ->set_entry(), where it then will need judging whether the assumption here still holds. (This is also why in general it'd be better to handle the error. It'll still be better to crash the guest than the host in case you can't. See the bottom of ./CODING_STYLE.)

The mem_sharing codebase uses ASSERT(!rc) on p2m->set_entry already when removing pages like we do here (see relinquish_shared_pages). This only gets called on shared pages that we know for sure are present. Since these are shared pages we know that their size is 4k thus there is no splitting. I can certainly add a comment to this effect to spell it out why the ASSERT is appropriate.

Tamas

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#38 lore raw

Re: [Xen-devel] [PATCH v9 4/5] x86/mem_sharing: reset a fork

Quoted 69 lines · Tamas K Lengyel
Quoted 59 lines · Jan Beulich
Quoted 20 lines · Tamas K Lengyel
Quoted 7 lines · Roger Pau Monné
Quoted 6 lines · Tamas K Lengyel
xen/arch/x86/mm/mem_sharing.c +0 -0
@@ -1636,6 +1636,59 @@ static int mem_sharing_fork(struct domain *d, struct domain *cd)
     return rc;
 }
 
Quoted 11 lines

xen/arch/x86/mm/mem_sharing.c static int mem_sharing_fork(struct domain *d, struct domain *cd) · line 1646

Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8 -0
+/*
+ * The fork reset operation is intended to be used on short-lived forks only.
+ * There is no hypercall continuation operation implemented for this reason.
+ * For forks that obtain a larger memory footprint it is likely going to be
+ * more performant to create a new fork instead of resetting an existing one.
+ *
+ * TODO: In case this hypercall would become useful on forks with larger memory
+ * footprints the hypercall continuation should be implemented.

I'm afraid this is not safe, as users don't have an easy way to know whether a fork will have a large memory footprint or not.

Quoted 2 lines

They do, getdomaininfo tells a user exactly how much memory has been allocated for a domain.

This tells the tool stack how much memory a guest has in absolute numbers, but it doesn't tell it whether Xen would consider this "large".

Quoted 26 lines
Quoted 17 lines
Quoted 17 lines
xen/arch/x86/mm/mem_sharing.c +17 -0
+    {
+        p2m_type_t p2mt;
+        p2m_access_t p2ma;
+        gfn_t gfn;
+        mfn_t mfn = page_to_mfn(page);
+
+        if ( !mfn_valid(mfn) )
+            continue;
+
+        gfn = mfn_to_gfn(cd, mfn);
+        mfn = __get_gfn_type_access(p2m, gfn_x(gfn), &p2mt, &p2ma,
+                                    0, NULL, false);
+
+        if ( !p2m_is_ram(p2mt) || p2m_is_shared(p2mt) )
+            continue;
+
+        /* take an extra reference */
Quoted 8 lines

xen/arch/x86/mm/mem_sharing.c static int mem_sharing_fork(struct domain *d, struct domain *cd) · line 1679

Quoted 6 lines
xen/arch/x86/mm/mem_sharing.c +6 -0
+        if ( !get_page(page, cd) )
+            continue;
+
+        rc = p2m->set_entry(p2m, gfn, INVALID_MFN, PAGE_ORDER_4K,
+                            p2m_invalid, p2m_access_rwx, -1);
+        ASSERT(!rc);

Can you handle this gracefully?

Quoted 2 lines

Nope. This should never happen, so if it does, something is very wrong in some other part of Xen.

Quoted 8 lines

In such a case, please put in a comment explaining why failure is impossible. In the general case e.g. a 2Mb page may need splitting, which may yield -ENOMEM. Such a comment will then also be useful in case a new failure mode gets added to ->set_entry(), where it then will need judging whether the assumption here still holds. (This is also why in general it'd be better to handle the error. It'll still be better to crash the guest than the host in case you can't. See the bottom of ./CODING_STYLE.)

Quoted 6 lines

The mem_sharing codebase uses ASSERT(!rc) on p2m->set_entry already when removing pages like we do here (see relinquish_shared_pages). This only gets called on shared pages that we know for sure are present. Since these are shared pages we know that their size is 4k thus there is no splitting. I can certainly add a comment to this effect to spell it out why the ASSERT is appropriate.

Well, if this is a pre-existing pattern in the file, then - you being the maintainer - so be it. I consider this bad practice though, and I would suggest that every such site needs a comment (possibly all but one simply referring to the one where things get actually explained).

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#29 lore raw

Re: [Xen-devel] [PATCH v9 3/5] xen/mem_sharing: VM forking

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

Note also that during a restore the guest is aware of such process, and will know that it needs to re-register some stuff, but that's not the case when forking, since the guest is not aware you need to make sure everything is in place. There are also the grant table pages, which I think should also be handled somehow (or we need to at least note this isn't handled and will need fixing).

Quoted 5 lines

True, the fork is not aware that something happened (and we want to keep it that way). But right now everything seems to "just work" as far as a standard VM is used. There must be a million cornercases left that I haven't covered for sure.

I've implemented forking for the vcpu_info pages and while I was testing I noticed that a Linux VM behaves differently during forking compared to a Windows VM. Forking a Windows VM at runtime works all fine, VNC screen is responsive, can log in and in every way the fork behaves like a regular domain. Forking the Linux VM however at runtime results in a frozen VNC screen. The VM runs fine otherwise, I use it in my fuzz tests and tracing it via VMI shows that both the kernel and userspace programs continue to execute in the fork normally. So there must be some "glue" the Linux guest needs to wire things up with the toolstack that I'm missing. When I first save the Linux VM and restore it with "xl restore -p" and then create forks the VNC console becomes responsive just as with the Windows VM.

At the moment it's not entirely clear to me what I'm missing that the Linux guest needs so I'm inclined to declare this a known limitation and move on since it's not critical for our use-case.

Tamas

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#39 lore raw

Re: [Xen-devel] [PATCH v9 4/5] x86/mem_sharing: reset a fork

In reply to Jan Beulich
Quoted 16 lines
Quoted 9 lines
Quoted 8 lines

In such a case, please put in a comment explaining why failure is impossible. In the general case e.g. a 2Mb page may need splitting, which may yield -ENOMEM. Such a comment will then also be useful in case a new failure mode gets added to ->set_entry(), where it then will need judging whether the assumption here still holds. (This is also why in general it'd be better to handle the error. It'll still be better to crash the guest than the host in case you can't. See the bottom of ./CODING_STYLE.)

Quoted 6 lines

The mem_sharing codebase uses ASSERT(!rc) on p2m->set_entry already when removing pages like we do here (see relinquish_shared_pages). This only gets called on shared pages that we know for sure are present. Since these are shared pages we know that their size is 4k thus there is no splitting. I can certainly add a comment to this effect to spell it out why the ASSERT is appropriate.

Quoted 6 lines

Well, if this is a pre-existing pattern in the file, then - you being the maintainer - so be it. I consider this bad practice though, and I would suggest that every such site needs a comment (possibly all but one simply referring to the one where things get actually explained).

Noted. I think for an experimental code-base it's fine.

Tamas

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel