Skip to contentSkip to conversation

VM forking

PATCH Superseded Tamas K Lengyel 5 participants 29 messages Mar 23, 2020 – Mar 26, 2020

v12 of 19 Superseded Latest is v19 →

lore
Share & tools
lore
#0 lore raw

v12 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> -Q <qemu-save-file> -m <max-vcpus> <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. Certain settings in the config file of both the parent and the fork have to be set to default. Details are documented.

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

    xl fork-vm --launch-dm no \
               -m <max-vcpus> \
               -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 Windows VMs and functions as expected. Linux VMs when forked from a running VM will have a frozen VNC screen. Linux VMs at this time can only be forked with a working device model when the parent VM was restored from a snapshot using "xl restore -p". This is a known limitation. Also note that PVHVM/PVH Linux guests have not been tested. Forking most likely works but PV devices and drivers would require additional wiring to set things up properly since the guests are unaware of the forking taking place, unlike the save/restore routine where the guest is made aware of the procedure.

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 v12:
style cleanups & minor adjustments
removing contiuation for fork reset and add TODO comment

Patch 1 implements the VM fork
Patch 2 implements fork reset operation
Patch 3 adds the toolstack-side code implementing VM forking and reset

Tamas K Lengyel (3):
xen/mem_sharing: VM forking
x86/mem_sharing: reset a fork
xen/tools: VM forking toolstack side

 docs/man/xl.1.pod.in              |  44 +++
 tools/libxc/include/xenctrl.h     |  13 +
 tools/libxc/xc_memshr.c           |  22 ++
 tools/libxl/libxl.h               |  11 +
 tools/libxl/libxl_create.c        | 361 +++++++++++++-----------
 tools/libxl/libxl_dm.c            |   2 +-
 tools/libxl/libxl_dom.c           |  43 ++-
 tools/libxl/libxl_internal.h      |   7 +
 tools/libxl/libxl_types.idl       |   1 +
 tools/libxl/libxl_x86.c           |  41 +++
 tools/xl/Makefile                 |   2 +-
 tools/xl/xl.h                     |   5 +
 tools/xl/xl_cmdtable.c            |  15 +
 tools/xl/xl_forkvm.c              | 147 ++++++++++
 tools/xl/xl_vmcontrol.c           |  14 +
 xen/arch/x86/domain.c             |  11 +
 xen/arch/x86/hvm/hvm.c            |   4 +-
 xen/arch/x86/mm/hap/hap.c         |   3 +-
 xen/arch/x86/mm/mem_sharing.c     | 445 ++++++++++++++++++++++++++++++
 xen/arch/x86/mm/p2m.c             |   9 +-
 xen/common/domain.c               |   3 +
 xen/include/asm-x86/hap.h         |   1 +
 xen/include/asm-x86/hvm/hvm.h     |   2 +
 xen/include/asm-x86/mem_sharing.h |  18 ++
 xen/include/public/memory.h       |   6 +
 xen/include/xen/sched.h           |   5 +
 26 files changed, 1064 insertions(+), 171 deletions(-)
 create mode 100644 tools/xl/xl_forkvm.c
Signature · 1 line
2.20.1
#1 lore raw

PATCH v12 1/3 xen/mem_sharing: VM forking

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).

---
v12: Minor style adjustments Jan pointed out
Convert mem_sharing_is_fork to inline function
v11: Fully copy vcpu_info pages
Setup vcpu_runstate for forks
Added TODO note for PV timers
Copy shared_info page
Add copy_settings function, to be shared with fork_reset in the next patch

---
 xen/arch/x86/domain.c             |  11 +
 xen/arch/x86/hvm/hvm.c            |   4 +-
 xen/arch/x86/mm/hap/hap.c         |   3 +-
 xen/arch/x86/mm/mem_sharing.c     | 368 ++++++++++++++++++++++++++++++
 xen/arch/x86/mm/p2m.c             |   9 +-
 xen/common/domain.c               |   3 +
 xen/include/asm-x86/hap.h         |   1 +
 xen/include/asm-x86/hvm/hvm.h     |   2 +
 xen/include/asm-x86/mem_sharing.h |  18 ++
 xen/include/public/memory.h       |   5 +
 xen/include/xen/sched.h           |   5 +
 11 files changed, 424 insertions(+), 5 deletions(-)
xen/arch/x86/domain.c +11 -0
index caf2ecad7e..11d3c2216e 100644
@@ -2202,6 +2202,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 ( mem_sharing_is_fork(d) )
+            {
+                domain_unpause(d->parent);
+                put_domain(d->parent);
+                d->parent = NULL;
+            }
         }
 #endif
 
xen/arch/x86/hvm/hvm.c +2 -2
index a3d115b650..304b3d1562 100644
@@ -1917,7 +1917,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;
@@ -4377,7 +4377,7 @@ static int hvm_allow_get_param(struct domain *d,
     return rc;
 }
 
-static int hvm_get_param(struct domain *d, uint32_t index, uint64_t *value)
+int hvm_get_param(struct domain *d, uint32_t index, uint64_t *value)
 {
     int rc;
 
xen/arch/x86/mm/hap/hap.c +1 -2
index a6d5e39b02..814d0c3253 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/arch/x86/mm/mem_sharing.c +368 -0

392 lines not on this page · raw message

xen/arch/x86/mm/p2m.c +8 -1
index 9f51370327..1ed7d13084 100644
@@ -509,6 +509,12 @@ 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 +594,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/common/domain.c +3 -0
index b4eb476a9c..62aed53a16 100644
@@ -1270,6 +1270,9 @@ int map_vcpu_info(struct vcpu *v, unsigned long gfn, unsigned offset)
 
     v->vcpu_info = new_info;
     v->vcpu_info_mfn = page_to_mfn(page);
+#ifdef CONFIG_MEM_SHARING
+    v->vcpu_info_offset = offset;
+#endif
 
     /* Set new vcpu_info pointer /before/ setting pending flags. */
     smp_wmb();
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 */
 
xen/include/asm-x86/hvm/hvm.h +2 -0
index b007b2e343..f283c7d187 100644
@@ -336,6 +336,8 @@ unsigned long hvm_cr4_guest_valid_bits(const struct domain *d, bool restore);
 
 int hvm_copy_context_and_params(struct domain *src, struct domain *dst);
 
+int hvm_get_param(struct domain *d, uint32_t index, uint64_t *value);
+
 #ifdef CONFIG_HVM
 
 #define hvm_get_guest_tsc(v) hvm_get_guest_tsc_fixed(v, 0)
xen/include/asm-x86/mem_sharing.h +18 -0
index 53b7929d0e..78c3a2c343 100644
@@ -77,6 +77,14 @@ static inline int mem_sharing_unshare_page(struct domain *d,
     return rc;
 }
 
+static inline bool mem_sharing_is_fork(struct domain *d)
+{
+    return d->parent;
+}
+
+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
@@ -130,6 +138,16 @@ static inline int mem_sharing_notify_enomem(struct domain *d, unsigned long gfn,
     return -EOPNOTSUPP;
 }
 
+static inline bool mem_sharing_is_fork(struct domain *d)
+{
+    return false;
+}
+
+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..5ee4e0da12 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 {      /* OP_FORK */
+            domid_t parent_domain;        /* IN: parent's domain id */
+            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 +5 -0
index e6813288ab..881f2bb0c2 100644
@@ -247,6 +247,9 @@ struct vcpu
 
     /* Guest-specified relocation of vcpu_info. */
     mfn_t            vcpu_info_mfn;
+#ifdef CONFIG_MEM_SHARING
+    unsigned short   vcpu_info_offset;
+#endif
 
     struct evtchn_fifo_vcpu *evtchn_fifo;
 
@@ -480,6 +483,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 · 1 line
2.20.1
#20 lore raw

PATCH v12 2/3 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.

---
v12: remove continuation & add comment back
address style issues pointed out by Jan

---
 xen/arch/x86/mm/mem_sharing.c | 77 +++++++++++++++++++++++++++++++++++
 xen/include/public/memory.h   |  1 +
 2 files changed, 78 insertions(+)
xen/arch/x86/mm/mem_sharing.c +77 -0
index 23deeddff2..930a5f58ef 100644
@@ -1775,6 +1775,60 @@ static int fork(struct domain *cd, struct domain *d)
     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 (or if this
+ * feature needs to be become "stable").
+ */
+static int mem_sharing_fork_reset(struct domain *d, struct domain *pd)
+{
+    int rc;
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+    struct page_info *page, *tmp;
+
+    spin_lock(&d->page_alloc_lock);
+    domain_pause(d);
+
+    page_list_for_each_safe(page, tmp, &d->page_list)
+    {
+        p2m_type_t p2mt;
+        p2m_access_t p2ma;
+        mfn_t mfn = page_to_mfn(page);
+        gfn_t gfn = mfn_to_gfn(d, mfn);
+
+        mfn = __get_gfn_type_access(p2m, gfn_x(gfn), &p2mt, &p2ma,
+                                    0, NULL, false);
+
+        /* only reset pages that are sharable */
+        if ( !p2m_is_sharable(p2mt) )
+            continue;
+
+        /* take an extra reference or just skip if can't for whatever reason */
+        if ( !get_page(page, d) )
+            continue;
+
+        /* forked memory is 4k, not splitting large pages so this must work */
+        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);
+    }
+
+    rc = copy_settings(d, pd);
+
+    domain_unpause(d);
+    spin_unlock(&d->page_alloc_lock);
+
+    return rc;
+}
+
 int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg)
 {
     int rc;
@@ -2066,6 +2120,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(d, pd);
+
+        rcu_unlock_domain(pd);
+        break;
+    }
+
     default:
         rc = -ENOSYS;
         break;
xen/include/public/memory.h +1 -0
index 5ee4e0da12..d36d64b8dc 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 · 1 line
2.20.1
#28 lore raw

PATCH v12 3/3 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          |  44 +++++
 tools/libxc/include/xenctrl.h |  13 ++
 tools/libxc/xc_memshr.c       |  22 +++
 tools/libxl/libxl.h           |  11 ++
 tools/libxl/libxl_create.c    | 361 +++++++++++++++++++---------------
 tools/libxl/libxl_dm.c        |   2 +-
 tools/libxl/libxl_dom.c       |  43 +++-
 tools/libxl/libxl_internal.h  |   7 +
 tools/libxl/libxl_types.idl   |   1 +
 tools/libxl/libxl_x86.c       |  41 ++++
 tools/xl/Makefile             |   2 +-
 tools/xl/xl.h                 |   5 +
 tools/xl/xl_cmdtable.c        |  15 ++
 tools/xl/xl_forkvm.c          | 147 ++++++++++++++
 tools/xl/xl_vmcontrol.c       |  14 ++
 15 files changed, 562 insertions(+), 166 deletions(-)
 create mode 100644 tools/xl/xl_forkvm.c
docs/man/xl.1.pod.in +44 -0
index 09339282e6..59c03c6427 100644
@@ -708,6 +708,50 @@ above).
 
 =back
 
+=item B<fork-vm> [I<OPTIONS>] I<domain-id>
+
+Create a fork of a running VM.  The domain will be paused after the operation
+and remains paused while forks of it exist.  Experimental and x86 only.
+Forks can only be made of domains with HAP enabled and on Intel hardware.  The
+parent domain must be created with the xl toolstack and its configuration must
+not manually define max_grant_frames, max_maptrack_frames or max_event_channels.
+
+B<OPTIONS>
+
+=over 4
+
+=item B<-p>
+
+Leave the fork paused after creating it.
+
+=item B<--launch-dm>
+
+Specify whether the device model (QEMU) should be launched for the fork. Late
+launch allows to start the device model for an already running fork.
+
+=item B<-C>
+
+The config file to use when launching the device model.  Currently required when
+launching the device model.  Most config settings MUST match the parent domain
+exactly, only change VM name, disk path and network configurations.
+
+=item B<-Q>
+
+The path to the qemu save file to use when launching the device model.  Currently
+required when launching the device model.
+
+=item B<--fork-reset>
+
+Perform a reset operation of an already running fork.  Note that resetting may
+be less performant then creating a new fork depending on how much memory the
+fork has deduplicated during its runtime.
+
+=item B<--max-vcpus>
+
+Specify the max-vcpus matching the parent domain when not launching the dm.
+
+=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 fc6e57a1a0..00cb4cf1f7 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 +11 -0
index 71709dc585..088e81c78b 100644
@@ -2666,6 +2666,17 @@ int libxl_psr_get_hw_info(libxl_ctx *ctx, libxl_psr_feat_type type,
                           unsigned int lvl, unsigned int *nr,
                           libxl_psr_hw_info **info);
 void libxl_psr_hw_info_list_free(libxl_psr_hw_info *list, unsigned int nr);
+
+int libxl_domain_fork_vm(libxl_ctx *ctx, uint32_t pdomid, uint32_t max_vcpus, uint32_t *domid)
+                         LIBXL_EXTERNAL_CALLERS_ONLY;
+
+int libxl_domain_fork_launch_dm(libxl_ctx *ctx, libxl_domain_config *d_config,
+                                uint32_t domid,
+                                const libxl_asyncprogress_how *aop_console_how)
+                                LIBXL_EXTERNAL_CALLERS_ONLY;
+
+int libxl_domain_fork_reset(libxl_ctx *ctx, uint32_t domid)
+                            LIBXL_EXTERNAL_CALLERS_ONLY;
 #endif
 
 /* misc */
tools/libxl/libxl_create.c +204 -157

482 lines not on this page · raw message

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

105 lines not on this page · raw message

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

53 lines not on this page · raw message

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

147 lines not on this page · raw message

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

Re: [Xen-devel] [PATCH v12 1/3] xen/mem_sharing: VM forking

In reply to Tamas K Lengyel

Sorry it has taken me a while to get to this.

Quoted 139 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).

---
v12: Minor style adjustments Jan pointed out
Convert mem_sharing_is_fork to inline function
v11: Fully copy vcpu_info pages
Setup vcpu_runstate for forks
Added TODO note for PV timers
Copy shared_info page
Add copy_settings function, to be shared with fork_reset in the next patch

---
 xen/arch/x86/domain.c             |  11 +
 xen/arch/x86/hvm/hvm.c            |   4 +-
 xen/arch/x86/mm/hap/hap.c         |   3 +-
 xen/arch/x86/mm/mem_sharing.c     | 368 ++++++++++++++++++++++++++++++
 xen/arch/x86/mm/p2m.c             |   9 +-
 xen/common/domain.c               |   3 +
 xen/include/asm-x86/hap.h         |   1 +
 xen/include/asm-x86/hvm/hvm.h     |   2 +
 xen/include/asm-x86/mem_sharing.h |  18 ++
 xen/include/public/memory.h       |   5 +
 xen/include/xen/sched.h           |   5 +
 11 files changed, 424 insertions(+), 5 deletions(-)
xen/arch/x86/domain.c +11 -0
index caf2ecad7e..11d3c2216e 100644
@@ -2202,6 +2202,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 ( mem_sharing_is_fork(d) )
+            {
+                domain_unpause(d->parent);
+                put_domain(d->parent);
+                d->parent = NULL;
+            }
         }
 #endif
 
xen/arch/x86/hvm/hvm.c +2 -2
index a3d115b650..304b3d1562 100644
@@ -1917,7 +1917,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;
@@ -4377,7 +4377,7 @@ static int hvm_allow_get_param(struct domain *d,
     return rc;
 }
 
-static int hvm_get_param(struct domain *d, uint32_t index, uint64_t *value)
+int hvm_get_param(struct domain *d, uint32_t index, uint64_t *value)
 {
     int rc;
 
xen/arch/x86/mm/hap/hap.c +1 -2
index a6d5e39b02..814d0c3253 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/arch/x86/mm/mem_sharing.c +31 -0
index 3835bc928f..23deeddff2 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,8 @@
 #include <asm/altp2m.h>
 #include <asm/atomic.h>
 #include <asm/event.h>
+#include <asm/hap.h>
+#include <asm/hvm/hvm.h>
 #include <xsm/xsm.h>
 
 #include "mm-locks.h"
@@ -1444,6 +1447,334 @@ 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.
+ */
+int mem_sharing_fork_page(struct domain *d, gfn_t gfn, bool unsharing)
+{
+    int rc = -ENOENT;
+    shr_handle_t handle;
+    struct domain *parent = d->parent;
+    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;
+
+    if ( !unsharing )
+    {
+        /* For read-only accesses we just add a shared entry to the physmap */
+        while ( parent )
+        {

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

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+            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);

Nit: I think you could just use the existing p2m local variable.

Quoted 13 lines
xen/arch/x86/mm/mem_sharing.c +13 -0
+            p2m_lock(pp2m);
+            rc = add_to_physmap(parent, gfn_l, handle, d, gfn_l, false);
+            p2m_unlock(pp2m);
+
+            if ( !rc )
+                return 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.
+     */

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

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+    p2m = p2m_get_hostp2m(d);
+    parent = d->parent;
+
+    while ( parent )
+    {
+        mfn = get_gfn_query(parent, gfn_l, &p2mt);
+
+        /*
+         * We can't fork grant memory from the parent, only regular ram.
+         */

Nit: single line comments should use /* ... */ (here and below).

Quoted 20 lines
xen/arch/x86/mm/mem_sharing.c +20 -0
+        if ( mfn_valid(mfn) && p2m_is_ram(p2mt) )
+            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);
+

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

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+    put_gfn(parent, gfn_l);
+
+    return p2m->set_entry(p2m, gfn, new_mfn, PAGE_ORDER_4K, p2m_ram_rw,
+                          p2m->default_access, -1);
+}
+
+static int bring_up_vcpus(struct domain *cd, struct domain *d)
+{
+    unsigned int i;
+    int ret = -EINVAL;

Nit: you can get rid of ret...

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

Quoted 4 lines
xen/arch/x86/mm/mem_sharing.c +4 -0
+
+    if ( d->max_vcpus != cd->max_vcpus ||
+        (ret = cpupool_move_domain(cd, d->cpupool)) )
+        return ret;

...and just return -EINVAL here. Seeing as it's not used anywhere else.

Quoted 61 lines
xen/arch/x86/mm/mem_sharing.c +61 -0
+
+    for ( i = 0; i < cd->max_vcpus; i++ )
+    {
+        if ( !d->vcpu[i] || cd->vcpu[i] )
+            continue;
+
+        if ( !vcpu_create(cd, i) )
+            return -EINVAL;
+    }
+
+    domain_update_node_affinity(cd);
+    return 0;
+}
+
+static int copy_vcpu_settings(struct domain *cd, struct domain *d)
+{
+    unsigned int i;
+    struct p2m_domain *p2m = p2m_get_hostp2m(cd);
+    int ret = -EINVAL;
+
+    for ( i = 0; i < cd->max_vcpus; i++ )
+    {
+        const struct vcpu *d_vcpu = d->vcpu[i];
+        struct vcpu *cd_vcpu = cd->vcpu[i];
+        struct vcpu_runstate_info runstate;
+        mfn_t vcpu_info_mfn;
+
+        if ( !d_vcpu || !cd_vcpu )
+            continue;
+
+        /*
+         * Copy & map in the vcpu_info page if the guest uses one
+         */
+        vcpu_info_mfn = d_vcpu->vcpu_info_mfn;
+        if ( !mfn_eq(vcpu_info_mfn, INVALID_MFN) )
+        {
+            mfn_t new_vcpu_info_mfn = cd_vcpu->vcpu_info_mfn;
+
+            /*
+             * Allocate & map the page for it if it hasn't been already
+             */
+            if ( mfn_eq(new_vcpu_info_mfn, INVALID_MFN) )
+            {
+                gfn_t gfn = mfn_to_gfn(d, vcpu_info_mfn);
+                unsigned long gfn_l = gfn_x(gfn);
+                struct page_info *page;
+
+                if ( !(page = alloc_domheap_page(cd, 0)) )
+                    return -ENOMEM;
+
+                new_vcpu_info_mfn = page_to_mfn(page);
+                set_gpfn_from_mfn(mfn_x(new_vcpu_info_mfn), gfn_l);
+
+                ret = p2m->set_entry(p2m, gfn, new_vcpu_info_mfn, PAGE_ORDER_4K,
+                                     p2m_ram_rw, p2m->default_access, -1);
+                if ( ret )
+                    return ret;
+
+                ret = map_vcpu_info(cd_vcpu, gfn_l,
+                                    d_vcpu->vcpu_info_offset);
+                if ( ret )

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

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+                    return ret;
+            }
+
+            copy_domain_page(new_vcpu_info_mfn, vcpu_info_mfn);
+        }
+
+        /*
+         * Setup the vCPU runstate area
+         */
+        if ( guest_handle_is_null(runstate_guest(cd_vcpu)) )

Maybe I'm confused, but isn't this the other way around and you need to check? If the parent runstate is not null copy it to the fork, ie:

if ( !guest_handle_is_null(runstate_guest(d_vcpu)) )

{
    ...

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

Quoted 4 lines
xen/arch/x86/mm/mem_sharing.c +4 -0
+        {
+            runstate_guest(cd_vcpu) = runstate_guest(d_vcpu);
+            vcpu_runstate_get(cd_vcpu, &runstate);
+            __copy_to_guest(runstate_guest(cd_vcpu), &runstate, 1);

You should check the return code I think.

Quoted 44 lines
xen/arch/x86/mm/mem_sharing.c +44 -0
+        }
+
+        /*
+         * TODO: to support VMs with PV interfaces copy additional
+         * settings here, such as PV timers.
+         */
+    }
+
+    return 0;
+}
+
+static int fork_hap_allocation(struct domain *cd, struct domain *d)
+{
+    int rc;
+    bool preempted;
+    unsigned long mb = hap_get_allocation(d);
+
+    if ( mb == hap_get_allocation(cd) )
+        return 0;
+
+    paging_lock(cd);
+    rc = hap_set_allocation(cd, mb << (20 - PAGE_SHIFT), &preempted);
+    paging_unlock(cd);
+
+    return preempted ? -ERESTART : rc;
+}
+
+static void copy_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);
+}
+
+static int copy_special_pages(struct domain *cd, struct domain *d)
+{
+    mfn_t new_mfn, old_mfn;
+    struct p2m_domain *p2m = p2m_get_hostp2m(cd);
+    static const unsigned int params[] =

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

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+    {
+        HVM_PARAM_STORE_PFN,
+        HVM_PARAM_IOREQ_PFN,
+        HVM_PARAM_BUFIOREQ_PFN,
+        HVM_PARAM_CONSOLE_PFN
+    };
+    unsigned int i;
+    int rc;
+
+    for ( i = 0; i < 4; i++ )

Please use ARRAY_SIZE instead of hard coding 4.

Quoted 32 lines
xen/arch/x86/mm/p2m.c +8 -1
index 9f51370327..1ed7d13084 100644
@@ -509,6 +509,12 @@ 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 +594,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/common/domain.c +0 -0
index b4eb476a9c..62aed53a16 100644

xen/common/domain.c int map_vcpu_info(struct vcpu *v, unsigned long gfn, unsigned offset) · line 1274

Quoted 5 lines
xen/common/domain.c +2 -0
@@ -1270,6 +1270,9 @@ int map_vcpu_info(struct vcpu *v, unsigned long gfn, unsigned offset)
 
     v->vcpu_info = new_info;
     v->vcpu_info_mfn = page_to_mfn(page);
+#ifdef CONFIG_MEM_SHARING
+    v->vcpu_info_offset = offset;

There's no need to introduce this field, you can just use v->vcpu_info & ~PAGE_MASK AFAICT.

Thanks, Roger.

Reviewed-by on v12 2/3
#21 lore raw

Re: [Xen-devel] [PATCH v12 2/3] x86/mem_sharing: reset a fork

In reply to Tamas K Lengyel
Quoted 10 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 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.

LGTM:

One minor nit below.

Quoted 42 lines

---
v12: remove continuation & add comment back
address style issues pointed out by Jan

---
 xen/arch/x86/mm/mem_sharing.c | 77 +++++++++++++++++++++++++++++++++++
 xen/include/public/memory.h   |  1 +
 2 files changed, 78 insertions(+)
xen/arch/x86/mm/mem_sharing.c +26 -0
index 23deeddff2..930a5f58ef 100644
@@ -1775,6 +1775,60 @@ static int fork(struct domain *cd, struct domain *d)
     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 (or if this
+ * feature needs to be become "stable").
+ */
+static int mem_sharing_fork_reset(struct domain *d, struct domain *pd)
+{
+    int rc;
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+    struct page_info *page, *tmp;
+
+    spin_lock(&d->page_alloc_lock);
+    domain_pause(d);
+
+    page_list_for_each_safe(page, tmp, &d->page_list)
+    {
+        p2m_type_t p2mt;
+        p2m_access_t p2ma;
+        mfn_t mfn = page_to_mfn(page);
+        gfn_t gfn = mfn_to_gfn(d, mfn);
+

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

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+        mfn = __get_gfn_type_access(p2m, gfn_x(gfn), &p2mt, &p2ma,
+                                    0, NULL, false);
+
+        /* only reset pages that are sharable */
+        if ( !p2m_is_sharable(p2mt) )
+            continue;
+
+        /* take an extra reference or just skip if can't for whatever reason */
+        if ( !get_page(page, d) )
+            continue;

You can join both conditions above into a single one, if both just need to perform a continue.

Thanks, Roger.

#22 lore raw

Re: [Xen-devel] [PATCH v12 2/3] x86/mem_sharing: reset a fork

Quoted 66 lines · Roger Pau Monné
Quoted 10 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 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.

LGTM:

One minor nit below.

Quoted 47 lines

---
v12: remove continuation & add comment back
address style issues pointed out by Jan

---
 xen/arch/x86/mm/mem_sharing.c | 77 +++++++++++++++++++++++++++++++++++
 xen/include/public/memory.h   |  1 +
 2 files changed, 78 insertions(+)
xen/arch/x86/mm/mem_sharing.c +31 -0
index 23deeddff2..930a5f58ef 100644
@@ -1775,6 +1775,60 @@ static int fork(struct domain *cd, struct domain *d)
     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 (or if this
+ * feature needs to be become "stable").
+ */
+static int mem_sharing_fork_reset(struct domain *d, struct domain *pd)
+{
+    int rc;
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+    struct page_info *page, *tmp;
+
+    spin_lock(&d->page_alloc_lock);
+    domain_pause(d);
+
+    page_list_for_each_safe(page, tmp, &d->page_list)
+    {
+        p2m_type_t p2mt;
+        p2m_access_t p2ma;
+        mfn_t mfn = page_to_mfn(page);
+        gfn_t gfn = mfn_to_gfn(d, mfn);
+
+        mfn = __get_gfn_type_access(p2m, gfn_x(gfn), &p2mt, &p2ma,
+                                    0, NULL, false);
+
+        /* only reset pages that are sharable */
+        if ( !p2m_is_sharable(p2mt) )
Quoted 8 lines

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

Quoted 5 lines
xen/arch/x86/mm/mem_sharing.c +5 -0
+            continue;
+
+        /* take an extra reference or just skip if can't for whatever reason */
+        if ( !get_page(page, d) )
+            continue;

You can join both conditions above into a single one, if both just need to perform a continue.

We could but I think it's easier to read it this way. So I prefer to keep it separate.

Thanks for the review!
Tamas

#3 lore raw

Re: [Xen-devel] [PATCH v12 1/3] xen/mem_sharing: VM forking

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

Sorry it has taken me a while to get to this.

Thanks for the review, I'm addressing all the items you noticed in the next revision.

Tamas

#4 lore raw

Re: [Xen-devel] [PATCH v12 1/3] xen/mem_sharing: VM forking

In reply to Roger Pau Monné
Quoted 7 lines

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

Quoted 4 lines
xen/arch/x86/mm/mem_sharing.c +4 -0
+static int bring_up_vcpus(struct domain *cd, struct domain *d)
+{
+    unsigned int i;
+    int ret = -EINVAL;

Nit: you can get rid of ret...

Quoted 8 lines

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

Quoted 4 lines
xen/arch/x86/mm/mem_sharing.c +4 -0
+
+    if ( d->max_vcpus != cd->max_vcpus ||
+        (ret = cpupool_move_domain(cd, d->cpupool)) )
+        return ret;

...and just return -EINVAL here. Seeing as it's not used anywhere else.

It is actually still needed, note that we store the return value of cpupool_move_domain in it.

Tamas

#5 lore raw

Re: [Xen-devel] [PATCH v12 1/3] xen/mem_sharing: VM forking

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

Sorry it has taken me a while to get to this.

Quoted 139 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).

---
v12: Minor style adjustments Jan pointed out
Convert mem_sharing_is_fork to inline function
v11: Fully copy vcpu_info pages
Setup vcpu_runstate for forks
Added TODO note for PV timers
Copy shared_info page
Add copy_settings function, to be shared with fork_reset in the next patch

---
 xen/arch/x86/domain.c             |  11 +
 xen/arch/x86/hvm/hvm.c            |   4 +-
 xen/arch/x86/mm/hap/hap.c         |   3 +-
 xen/arch/x86/mm/mem_sharing.c     | 368 ++++++++++++++++++++++++++++++
 xen/arch/x86/mm/p2m.c             |   9 +-
 xen/common/domain.c               |   3 +
 xen/include/asm-x86/hap.h         |   1 +
 xen/include/asm-x86/hvm/hvm.h     |   2 +
 xen/include/asm-x86/mem_sharing.h |  18 ++
 xen/include/public/memory.h       |   5 +
 xen/include/xen/sched.h           |   5 +
 11 files changed, 424 insertions(+), 5 deletions(-)
xen/arch/x86/domain.c +11 -0
index caf2ecad7e..11d3c2216e 100644
@@ -2202,6 +2202,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 ( mem_sharing_is_fork(d) )
+            {
+                domain_unpause(d->parent);
+                put_domain(d->parent);
+                d->parent = NULL;
+            }
         }
 #endif
 
xen/arch/x86/hvm/hvm.c +2 -2
index a3d115b650..304b3d1562 100644
@@ -1917,7 +1917,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;
@@ -4377,7 +4377,7 @@ static int hvm_allow_get_param(struct domain *d,
     return rc;
 }
 
-static int hvm_get_param(struct domain *d, uint32_t index, uint64_t *value)
+int hvm_get_param(struct domain *d, uint32_t index, uint64_t *value)
 {
     int rc;
 
xen/arch/x86/mm/hap/hap.c +1 -2
index a6d5e39b02..814d0c3253 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/arch/x86/mm/mem_sharing.c +31 -0
index 3835bc928f..23deeddff2 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,8 @@
 #include <asm/altp2m.h>
 #include <asm/atomic.h>
 #include <asm/event.h>
+#include <asm/hap.h>
+#include <asm/hvm/hvm.h>
 #include <xsm/xsm.h>
 
 #include "mm-locks.h"
@@ -1444,6 +1447,334 @@ 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.
+ */
+int mem_sharing_fork_page(struct domain *d, gfn_t gfn, bool unsharing)
+{
+    int rc = -ENOENT;
+    shr_handle_t handle;
+    struct domain *parent = d->parent;
+    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;
+
+    if ( !unsharing )
+    {
+        /* For read-only accesses we just add a shared entry to the physmap */
+        while ( parent )
+        {

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

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+            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);

Nit: I think you could just use the existing p2m local variable.

Quoted 13 lines
xen/arch/x86/mm/mem_sharing.c +13 -0
+            p2m_lock(pp2m);
+            rc = add_to_physmap(parent, gfn_l, handle, d, gfn_l, false);
+            p2m_unlock(pp2m);
+
+            if ( !rc )
+                return 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.
+     */

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

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+    p2m = p2m_get_hostp2m(d);
+    parent = d->parent;
+
+    while ( parent )
+    {
+        mfn = get_gfn_query(parent, gfn_l, &p2mt);
+
+        /*
+         * We can't fork grant memory from the parent, only regular ram.
+         */

Nit: single line comments should use /* ... */ (here and below).

Quoted 20 lines
xen/arch/x86/mm/mem_sharing.c +20 -0
+        if ( mfn_valid(mfn) && p2m_is_ram(p2mt) )
+            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);
+

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

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+    put_gfn(parent, gfn_l);
+
+    return p2m->set_entry(p2m, gfn, new_mfn, PAGE_ORDER_4K, p2m_ram_rw,
+                          p2m->default_access, -1);
+}
+
+static int bring_up_vcpus(struct domain *cd, struct domain *d)
+{
+    unsigned int i;
+    int ret = -EINVAL;

Nit: you can get rid of ret...

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

Quoted 4 lines
xen/arch/x86/mm/mem_sharing.c +4 -0
+
+    if ( d->max_vcpus != cd->max_vcpus ||
+        (ret = cpupool_move_domain(cd, d->cpupool)) )
+        return ret;

...and just return -EINVAL here. Seeing as it's not used anywhere else.

Quoted 61 lines
xen/arch/x86/mm/mem_sharing.c +61 -0
+
+    for ( i = 0; i < cd->max_vcpus; i++ )
+    {
+        if ( !d->vcpu[i] || cd->vcpu[i] )
+            continue;
+
+        if ( !vcpu_create(cd, i) )
+            return -EINVAL;
+    }
+
+    domain_update_node_affinity(cd);
+    return 0;
+}
+
+static int copy_vcpu_settings(struct domain *cd, struct domain *d)
+{
+    unsigned int i;
+    struct p2m_domain *p2m = p2m_get_hostp2m(cd);
+    int ret = -EINVAL;
+
+    for ( i = 0; i < cd->max_vcpus; i++ )
+    {
+        const struct vcpu *d_vcpu = d->vcpu[i];
+        struct vcpu *cd_vcpu = cd->vcpu[i];
+        struct vcpu_runstate_info runstate;
+        mfn_t vcpu_info_mfn;
+
+        if ( !d_vcpu || !cd_vcpu )
+            continue;
+
+        /*
+         * Copy & map in the vcpu_info page if the guest uses one
+         */
+        vcpu_info_mfn = d_vcpu->vcpu_info_mfn;
+        if ( !mfn_eq(vcpu_info_mfn, INVALID_MFN) )
+        {
+            mfn_t new_vcpu_info_mfn = cd_vcpu->vcpu_info_mfn;
+
+            /*
+             * Allocate & map the page for it if it hasn't been already
+             */
+            if ( mfn_eq(new_vcpu_info_mfn, INVALID_MFN) )
+            {
+                gfn_t gfn = mfn_to_gfn(d, vcpu_info_mfn);
+                unsigned long gfn_l = gfn_x(gfn);
+                struct page_info *page;
+
+                if ( !(page = alloc_domheap_page(cd, 0)) )
+                    return -ENOMEM;
+
+                new_vcpu_info_mfn = page_to_mfn(page);
+                set_gpfn_from_mfn(mfn_x(new_vcpu_info_mfn), gfn_l);
+
+                ret = p2m->set_entry(p2m, gfn, new_vcpu_info_mfn, PAGE_ORDER_4K,
+                                     p2m_ram_rw, p2m->default_access, -1);
+                if ( ret )
+                    return ret;
+
+                ret = map_vcpu_info(cd_vcpu, gfn_l,
+                                    d_vcpu->vcpu_info_offset);
+                if ( ret )

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

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+                    return ret;
+            }
+
+            copy_domain_page(new_vcpu_info_mfn, vcpu_info_mfn);
+        }
+
+        /*
+         * Setup the vCPU runstate area
+         */
+        if ( guest_handle_is_null(runstate_guest(cd_vcpu)) )

Maybe I'm confused, but isn't this the other way around and you need to check? If the parent runstate is not null copy it to the fork, ie:

if ( !guest_handle_is_null(runstate_guest(d_vcpu)) )

{
    ...

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

Quoted 4 lines
xen/arch/x86/mm/mem_sharing.c +4 -0
+        {
+            runstate_guest(cd_vcpu) = runstate_guest(d_vcpu);
+            vcpu_runstate_get(cd_vcpu, &runstate);
+            __copy_to_guest(runstate_guest(cd_vcpu), &runstate, 1);

You should check the return code I think.

Quoted 44 lines
xen/arch/x86/mm/mem_sharing.c +44 -0
+        }
+
+        /*
+         * TODO: to support VMs with PV interfaces copy additional
+         * settings here, such as PV timers.
+         */
+    }
+
+    return 0;
+}
+
+static int fork_hap_allocation(struct domain *cd, struct domain *d)
+{
+    int rc;
+    bool preempted;
+    unsigned long mb = hap_get_allocation(d);
+
+    if ( mb == hap_get_allocation(cd) )
+        return 0;
+
+    paging_lock(cd);
+    rc = hap_set_allocation(cd, mb << (20 - PAGE_SHIFT), &preempted);
+    paging_unlock(cd);
+
+    return preempted ? -ERESTART : rc;
+}
+
+static void copy_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);
+}
+
+static int copy_special_pages(struct domain *cd, struct domain *d)
+{
+    mfn_t new_mfn, old_mfn;
+    struct p2m_domain *p2m = p2m_get_hostp2m(cd);
+    static const unsigned int params[] =

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

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+    {
+        HVM_PARAM_STORE_PFN,
+        HVM_PARAM_IOREQ_PFN,
+        HVM_PARAM_BUFIOREQ_PFN,
+        HVM_PARAM_CONSOLE_PFN
+    };
+    unsigned int i;
+    int rc;
+
+    for ( i = 0; i < 4; i++ )

Please use ARRAY_SIZE instead of hard coding 4.

Quoted 33 lines
xen/arch/x86/mm/p2m.c +8 -1
index 9f51370327..1ed7d13084 100644
@@ -509,6 +509,12 @@ 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 +594,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/common/domain.c +0 -0
index b4eb476a9c..62aed53a16 100644
@@ -1270,6 +1270,9 @@ int map_vcpu_info(struct vcpu *v, unsigned long gfn, unsigned offset)
 
Quoted 7 lines

xen/common/domain.c int map_vcpu_info(struct vcpu *v, unsigned long gfn, unsigned offset) · line 1274

Quoted 4 lines
xen/common/domain.c +2 -0
     v->vcpu_info = new_info;
     v->vcpu_info_mfn = page_to_mfn(page);
+#ifdef CONFIG_MEM_SHARING
+    v->vcpu_info_offset = offset;

There's no need to introduce this field, you can just use v->vcpu_info & ~PAGE_MASK AFAICT.

Just doing what you suggest above results in:

mem_sharing.c:1603:55: error: invalid operands to binary & (have ‘vcpu_info_t * const’ {aka ‘union <anonymous> * const’} and ‘long int’)

                                     d_vcpu->vcpu_info & ~PAGE_MASK);

I can of course cast the vcpu_info pointer to (long int), it's just a bit ugly. Thoughts?

Tamas

#6 lore raw

Re: [Xen-devel] [PATCH v12 1/3] xen/mem_sharing: VM forking

Hi,

Quoted 43 lines · Tamas K Lengyel
Quoted 33 lines
Quoted 33 lines
xen/arch/x86/mm/p2m.c +8 -1
index 9f51370327..1ed7d13084 100644
@@ -509,6 +509,12 @@ 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 +594,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/common/domain.c +0 -0
index b4eb476a9c..62aed53a16 100644
@@ -1270,6 +1270,9 @@ int map_vcpu_info(struct vcpu *v, unsigned long gfn, unsigned offset)
 
Quoted 7 lines

xen/common/domain.c int map_vcpu_info(struct vcpu *v, unsigned long gfn, unsigned offset) · line 1274

Quoted 4 lines
xen/common/domain.c +2 -0
      v->vcpu_info = new_info;
      v->vcpu_info_mfn = page_to_mfn(page);
+#ifdef CONFIG_MEM_SHARING
+    v->vcpu_info_offset = offset;

There's no need to introduce this field, you can just use v->vcpu_info & ~PAGE_MASK AFAICT.

Just doing what you suggest above results in:

Quoted 7 lines

mem_sharing.c:1603:55: error: invalid operands to binary & (have ‘vcpu_info_t * const’ {aka ‘union <anonymous> * const’} and ‘long int’)

                                      d_vcpu->vcpu_info & ~PAGE_MASK);

I can of course cast the vcpu_info pointer to (long int), it's just a bit ugly. Thoughts?

FWIW, I will also need the offset for liveupdate. I have used (unsigned long)v->vcpu_info & ~PAGE_MASK so far but this is not really pretty.

So I am all for either a new field or a macro hiding this uglyness.

Cheers,

Signature · 1 line
Julien Grall
#7 lore raw

Re: [Xen-devel] [PATCH v12 1/3] xen/mem_sharing: VM forking

Quoted 52 lines · Julien Grall

Hi,

Quoted 48 lines · Tamas K Lengyel
Quoted 33 lines
Quoted 33 lines
xen/arch/x86/mm/p2m.c +8 -1
index 9f51370327..1ed7d13084 100644
@@ -509,6 +509,12 @@ 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 +594,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/common/domain.c +0 -0
index b4eb476a9c..62aed53a16 100644
@@ -1270,6 +1270,9 @@ int map_vcpu_info(struct vcpu *v, unsigned long gfn, unsigned offset)
 
Quoted 7 lines

xen/common/domain.c int map_vcpu_info(struct vcpu *v, unsigned long gfn, unsigned offset) · line 1274

Quoted 4 lines
xen/common/domain.c +2 -0
      v->vcpu_info = new_info;
      v->vcpu_info_mfn = page_to_mfn(page);
+#ifdef CONFIG_MEM_SHARING
+    v->vcpu_info_offset = offset;

There's no need to introduce this field, you can just use v->vcpu_info & ~PAGE_MASK AFAICT.

Just doing what you suggest above results in:

mem_sharing.c:1603:55: error: invalid operands to binary & (have ‘vcpu_info_t * const’ {aka ‘union <anonymous> * const’} and ‘long int’)

                                      d_vcpu->vcpu_info & ~PAGE_MASK);
Quoted 7 lines
Quoted 2 lines

I can of course cast the vcpu_info pointer to (long int), it's just a bit ugly. Thoughts?

FWIW, I will also need the offset for liveupdate. I have used (unsigned long)v->vcpu_info & ~PAGE_MASK so far but this is not really pretty.

So I am all for either a new field or a macro hiding this uglyness.

A macro sounds like a good way to go, no need for an extra field if we can calculate it based on the currently existing one. How about

#define VCPU_INFO_OFFSET(v) (((unsigned long)v->vcpu_info) & ~PAGE_MASK)

?

Tamas

#8 lore raw

Re: [Xen-devel] [PATCH v12 1/3] xen/mem_sharing: VM forking

Quoted 59 lines · Tamas K Lengyel
Quoted 58 lines · Julien Grall

Hi,

Quoted 43 lines · Tamas K Lengyel
Quoted 33 lines
Quoted 33 lines
xen/arch/x86/mm/p2m.c +8 -1
index 9f51370327..1ed7d13084 100644
@@ -509,6 +509,12 @@ 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 +594,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/common/domain.c +0 -0
index b4eb476a9c..62aed53a16 100644
@@ -1270,6 +1270,9 @@ int map_vcpu_info(struct vcpu *v, unsigned long gfn, unsigned offset)
 
Quoted 7 lines

xen/common/domain.c int map_vcpu_info(struct vcpu *v, unsigned long gfn, unsigned offset) · line 1274

Quoted 4 lines
xen/common/domain.c +2 -0
       v->vcpu_info = new_info;
       v->vcpu_info_mfn = page_to_mfn(page);
+#ifdef CONFIG_MEM_SHARING
+    v->vcpu_info_offset = offset;

There's no need to introduce this field, you can just use v->vcpu_info & ~PAGE_MASK AFAICT.

Just doing what you suggest above results in:

Quoted 7 lines

mem_sharing.c:1603:55: error: invalid operands to binary & (have ‘vcpu_info_t * const’ {aka ‘union <anonymous> * const’} and ‘long int’)

                                       d_vcpu->vcpu_info & ~PAGE_MASK);

I can of course cast the vcpu_info pointer to (long int), it's just a bit ugly. Thoughts?

FWIW, I will also need the offset for liveupdate. I have used (unsigned long)v->vcpu_info & ~PAGE_MASK so far but this is not really pretty.

Quoted 6 lines
Quoted 1 line

So I am all for either a new field or a macro hiding this uglyness.

A macro sounds like a good way to go, no need for an extra field if we can calculate it based on the currently existing one. How about

#define VCPU_INFO_OFFSET(v) (((unsigned long)v->vcpu_info) & ~PAGE_MASK)

I was more thinking a generic macro to find the offset in a page.

PAGE_OFFSET(ptr) ((unsigned long)(ptr) & ~PAGE_MASK)

Anyway, I am happy either way.

Cheers,

Signature · 1 line
Julien Grall
#12 lore raw

Re: [Xen-devel] [PATCH v12 1/3] xen/mem_sharing: VM forking

In reply to Julien Grall
Quoted 51 lines · Julien Grall

Hi,

Quoted 48 lines · Tamas K Lengyel
Quoted 33 lines
Quoted 33 lines
xen/arch/x86/mm/p2m.c +8 -1
index 9f51370327..1ed7d13084 100644
@@ -509,6 +509,12 @@ 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 +594,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/common/domain.c +0 -0
index b4eb476a9c..62aed53a16 100644
@@ -1270,6 +1270,9 @@ int map_vcpu_info(struct vcpu *v, unsigned long gfn, unsigned offset)
 
Quoted 7 lines

xen/common/domain.c int map_vcpu_info(struct vcpu *v, unsigned long gfn, unsigned offset) · line 1274

Quoted 4 lines
xen/common/domain.c +2 -0
      v->vcpu_info = new_info;
      v->vcpu_info_mfn = page_to_mfn(page);
+#ifdef CONFIG_MEM_SHARING
+    v->vcpu_info_offset = offset;

There's no need to introduce this field, you can just use v->vcpu_info & ~PAGE_MASK AFAICT.

Just doing what you suggest above results in:

mem_sharing.c:1603:55: error: invalid operands to binary & (have ‘vcpu_info_t * const’ {aka ‘union <anonymous> * const’} and ‘long int’)

                                      d_vcpu->vcpu_info & ~PAGE_MASK);
Quoted 5 lines
Quoted 2 lines

I can of course cast the vcpu_info pointer to (long int), it's just a bit ugly. Thoughts?

FWIW, I will also need the offset for liveupdate. I have used (unsigned long)v->vcpu_info & ~PAGE_MASK so far but this is not really pretty.

I think using:

(vaddr_t)v->vcpu_info & ~PAGE_MASK

is acceptable, but that would require adding a vaddr_t typedef to x86.

Adding a macro to do so would be OK by me, maybe PAGE_OFFSET or some such?

Quoted 1 line

So I am all for either a new field or a macro hiding this uglyness.

Macro would be better IMO, as it's redundant to store the offset when we can obtain it from an existing field.

Thanks, Roger.

#9 lore raw

Re: [Xen-devel] [PATCH v12 1/3] xen/mem_sharing: VM forking

In reply to Julien Grall
Quoted 65 lines · Julien Grall
Quoted 61 lines · Tamas K Lengyel
Quoted 59 lines · Julien Grall

Hi,

Quoted 43 lines · Tamas K Lengyel
Quoted 33 lines
Quoted 33 lines
xen/arch/x86/mm/p2m.c +8 -1
index 9f51370327..1ed7d13084 100644
@@ -509,6 +509,12 @@ 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 +594,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/common/domain.c +0 -0
index b4eb476a9c..62aed53a16 100644
@@ -1270,6 +1270,9 @@ int map_vcpu_info(struct vcpu *v, unsigned long gfn, unsigned offset)
 
Quoted 7 lines

xen/common/domain.c int map_vcpu_info(struct vcpu *v, unsigned long gfn, unsigned offset) · line 1274

Quoted 4 lines
xen/common/domain.c +2 -0
       v->vcpu_info = new_info;
       v->vcpu_info_mfn = page_to_mfn(page);
+#ifdef CONFIG_MEM_SHARING
+    v->vcpu_info_offset = offset;

There's no need to introduce this field, you can just use v->vcpu_info & ~PAGE_MASK AFAICT.

Just doing what you suggest above results in:

Quoted 7 lines

mem_sharing.c:1603:55: error: invalid operands to binary & (have ‘vcpu_info_t * const’ {aka ‘union <anonymous> * const’} and ‘long int’)

                                       d_vcpu->vcpu_info & ~PAGE_MASK);

I can of course cast the vcpu_info pointer to (long int), it's just a bit ugly. Thoughts?

FWIW, I will also need the offset for liveupdate. I have used (unsigned long)v->vcpu_info & ~PAGE_MASK so far but this is not really pretty.

So I am all for either a new field or a macro hiding this uglyness.

Quoted 8 lines
Quoted 4 lines

A macro sounds like a good way to go, no need for an extra field if we can calculate it based on the currently existing one. How about

#define VCPU_INFO_OFFSET(v) (((unsigned long)v->vcpu_info) & ~PAGE_MASK)

I was more thinking a generic macro to find the offset in a page.

PAGE_OFFSET(ptr) ((unsigned long)(ptr) & ~PAGE_MASK)

LGTM. Should we stuff this into xen/sched.h or asm/page.h?

Tamas

#10 lore raw

Re: [Xen-devel] [PATCH v12 1/3] xen/mem_sharing: VM forking

Quoted 69 lines · Tamas K Lengyel
Quoted 68 lines · Julien Grall
Quoted 64 lines · Tamas K Lengyel
Quoted 52 lines · Julien Grall

Hi,

Quoted 48 lines · Tamas K Lengyel
Quoted 33 lines
Quoted 33 lines
xen/arch/x86/mm/p2m.c +8 -1
index 9f51370327..1ed7d13084 100644
@@ -509,6 +509,12 @@ 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 +594,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/common/domain.c +0 -0
index b4eb476a9c..62aed53a16 100644
@@ -1270,6 +1270,9 @@ int map_vcpu_info(struct vcpu *v, unsigned long gfn, unsigned offset)
 
Quoted 7 lines

xen/common/domain.c int map_vcpu_info(struct vcpu *v, unsigned long gfn, unsigned offset) · line 1274

Quoted 4 lines
xen/common/domain.c +2 -0
       v->vcpu_info = new_info;
       v->vcpu_info_mfn = page_to_mfn(page);
+#ifdef CONFIG_MEM_SHARING
+    v->vcpu_info_offset = offset;

There's no need to introduce this field, you can just use v->vcpu_info & ~PAGE_MASK AFAICT.

Just doing what you suggest above results in:

mem_sharing.c:1603:55: error: invalid operands to binary & (have ‘vcpu_info_t * const’ {aka ‘union <anonymous> * const’} and ‘long int’)

                                       d_vcpu->vcpu_info & ~PAGE_MASK);
Quoted 7 lines
Quoted 2 lines

I can of course cast the vcpu_info pointer to (long int), it's just a bit ugly. Thoughts?

FWIW, I will also need the offset for liveupdate. I have used (unsigned long)v->vcpu_info & ~PAGE_MASK so far but this is not really pretty.

So I am all for either a new field or a macro hiding this uglyness.

A macro sounds like a good way to go, no need for an extra field if we can calculate it based on the currently existing one. How about

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

#define VCPU_INFO_OFFSET(v) (((unsigned long)v->vcpu_info) & ~PAGE_MASK)

I was more thinking a generic macro to find the offset in a page.

PAGE_OFFSET(ptr) ((unsigned long)(ptr) & ~PAGE_MASK)

LGTM. Should we stuff this into xen/sched.h or asm/page.h?

page.h would be better, albeit you will have to duplicate it for x86 and Arm. There's xen/pfn.h which is not arch specific, but feels a bit weird to place it there.

Thanks, Roger.

#11 lore raw

Re: [Xen-devel] [PATCH v12 1/3] xen/mem_sharing: VM forking

Quoted 75 lines · Roger Pau Monné
Quoted 73 lines · Tamas K Lengyel
Quoted 72 lines · Julien Grall
Quoted 59 lines · Tamas K Lengyel
Quoted 58 lines · Julien Grall

Hi,

Quoted 43 lines · Tamas K Lengyel
Quoted 33 lines
Quoted 33 lines
xen/arch/x86/mm/p2m.c +8 -1
index 9f51370327..1ed7d13084 100644
@@ -509,6 +509,12 @@ 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 +594,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/common/domain.c +0 -0
index b4eb476a9c..62aed53a16 100644
@@ -1270,6 +1270,9 @@ int map_vcpu_info(struct vcpu *v, unsigned long gfn, unsigned offset)
 
Quoted 7 lines

xen/common/domain.c int map_vcpu_info(struct vcpu *v, unsigned long gfn, unsigned offset) · line 1274

Quoted 4 lines
xen/common/domain.c +2 -0
       v->vcpu_info = new_info;
       v->vcpu_info_mfn = page_to_mfn(page);
+#ifdef CONFIG_MEM_SHARING
+    v->vcpu_info_offset = offset;

There's no need to introduce this field, you can just use v->vcpu_info & ~PAGE_MASK AFAICT.

Just doing what you suggest above results in:

Quoted 7 lines

mem_sharing.c:1603:55: error: invalid operands to binary & (have ‘vcpu_info_t * const’ {aka ‘union <anonymous> * const’} and ‘long int’)

                                       d_vcpu->vcpu_info & ~PAGE_MASK);

I can of course cast the vcpu_info pointer to (long int), it's just a bit ugly. Thoughts?

FWIW, I will also need the offset for liveupdate. I have used (unsigned long)v->vcpu_info & ~PAGE_MASK so far but this is not really pretty.

Quoted 6 lines
Quoted 1 line

So I am all for either a new field or a macro hiding this uglyness.

A macro sounds like a good way to go, no need for an extra field if we can calculate it based on the currently existing one. How about

#define VCPU_INFO_OFFSET(v) (((unsigned long)v->vcpu_info) & ~PAGE_MASK)

I was more thinking a generic macro to find the offset in a page.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

PAGE_OFFSET(ptr) ((unsigned long)(ptr) & ~PAGE_MASK)

LGTM. Should we stuff this into xen/sched.h or asm/page.h?

page.h would be better, albeit you will have to duplicate it for x86 and Arm. There's xen/pfn.h which is not arch specific, but feels a bit weird to place it there.

I'll go with page.h, that allows us to use vaddr_t for the ARM macro.

Tamas

#13 lore raw

Re: [Xen-devel] [PATCH v12 1/3] xen/mem_sharing: VM forking

In reply to Roger Pau Monné
Quoted 24 lines · Roger Pau Monné
Quoted 23 lines · Julien Grall
Quoted 21 lines · Tamas K Lengyel
Quoted 11 lines

xen/common/domain.c int map_vcpu_info(struct vcpu *v, unsigned long gfn, unsigned offset) · line 1274

Quoted 8 lines
xen/common/domain.c +2 -0
@@ -1270,6 +1270,9 @@ int map_vcpu_info(struct vcpu *v, unsigned long gfn, unsigned offset)
 
      v->vcpu_info = new_info;
      v->vcpu_info_mfn = page_to_mfn(page);
+#ifdef CONFIG_MEM_SHARING
+    v->vcpu_info_offset = offset;

There's no need to introduce this field, you can just use v->vcpu_info & ~PAGE_MASK AFAICT.

Just doing what you suggest above results in:

mem_sharing.c:1603:55: error: invalid operands to binary & (have ‘vcpu_info_t * const’ {aka ‘union <anonymous> * const’} and ‘long int’)

                                      d_vcpu->vcpu_info & ~PAGE_MASK);

I can of course cast the vcpu_info pointer to (long int), it's just a bit ugly. Thoughts?

Quoted 8 lines
Quoted 2 lines

FWIW, I will also need the offset for liveupdate. I have used (unsigned long)v->vcpu_info & ~PAGE_MASK so far but this is not really pretty.

I think using:

(vaddr_t)v->vcpu_info & ~PAGE_MASK

is acceptable, but that would require adding a vaddr_t typedef to x86.

I don't think vaddr_t is necessary given that all over the place we assume conversions between pointers and unsigned long to be fine.

Jan

#15 lore raw

Re: [Xen-devel] [PATCH v12 1/3] xen/mem_sharing: VM forking

In reply to Roger Pau Monné
Quoted 67 lines · Roger Pau Monné
Quoted 47 lines · Tamas K Lengyel
xen/arch/x86/mm/mem_sharing.c +47 -0
+static int copy_vcpu_settings(struct domain *cd, struct domain *d)
+{
+    unsigned int i;
+    struct p2m_domain *p2m = p2m_get_hostp2m(cd);
+    int ret = -EINVAL;
+
+    for ( i = 0; i < cd->max_vcpus; i++ )
+    {
+        const struct vcpu *d_vcpu = d->vcpu[i];
+        struct vcpu *cd_vcpu = cd->vcpu[i];
+        struct vcpu_runstate_info runstate;
+        mfn_t vcpu_info_mfn;
+
+        if ( !d_vcpu || !cd_vcpu )
+            continue;
+
+        /*
+         * Copy & map in the vcpu_info page if the guest uses one
+         */
+        vcpu_info_mfn = d_vcpu->vcpu_info_mfn;
+        if ( !mfn_eq(vcpu_info_mfn, INVALID_MFN) )
+        {
+            mfn_t new_vcpu_info_mfn = cd_vcpu->vcpu_info_mfn;
+
+            /*
+             * Allocate & map the page for it if it hasn't been already
+             */
+            if ( mfn_eq(new_vcpu_info_mfn, INVALID_MFN) )
+            {
+                gfn_t gfn = mfn_to_gfn(d, vcpu_info_mfn);
+                unsigned long gfn_l = gfn_x(gfn);
+                struct page_info *page;
+
+                if ( !(page = alloc_domheap_page(cd, 0)) )
+                    return -ENOMEM;
+
+                new_vcpu_info_mfn = page_to_mfn(page);
+                set_gpfn_from_mfn(mfn_x(new_vcpu_info_mfn), gfn_l);
+
+                ret = p2m->set_entry(p2m, gfn, new_vcpu_info_mfn, PAGE_ORDER_4K,
+                                     p2m_ram_rw, p2m->default_access, -1);
+                if ( ret )
+                    return ret;
+
+                ret = map_vcpu_info(cd_vcpu, gfn_l,
+                                    d_vcpu->vcpu_info_offset);
+                if ( ret )

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

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+                    return ret;
+            }
+
+            copy_domain_page(new_vcpu_info_mfn, vcpu_info_mfn);
+        }
+
+        /*
+         * Setup the vCPU runstate area
+         */
+        if ( guest_handle_is_null(runstate_guest(cd_vcpu)) )

Maybe I'm confused, but isn't this the other way around and you need to check? If the parent runstate is not null copy it to the fork, ie:

if ( !guest_handle_is_null(runstate_guest(d_vcpu)) )

{
    ...
Quoted 6 lines

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

Quoted 4 lines
xen/arch/x86/mm/mem_sharing.c +4 -0
+        {
+            runstate_guest(cd_vcpu) = runstate_guest(d_vcpu);
+            vcpu_runstate_get(cd_vcpu, &runstate);
+            __copy_to_guest(runstate_guest(cd_vcpu), &runstate, 1);

You should check the return code I think.

I don't think so - this is a best effort operation just like e.g. in the handling of VCPUOP_register_runstate_memory_area.

Jan

#14 lore raw

Re: [Xen-devel] [PATCH v12 1/3] xen/mem_sharing: VM forking

In reply to Jan Beulich
Quoted 28 lines · Jan Beulich
Quoted 27 lines · Roger Pau Monné
Quoted 25 lines · Julien Grall
Quoted 14 lines · Tamas K Lengyel
Quoted 11 lines

xen/common/domain.c int map_vcpu_info(struct vcpu *v, unsigned long gfn, unsigned offset) · line 1274

Quoted 8 lines
xen/common/domain.c +2 -0
@@ -1270,6 +1270,9 @@ int map_vcpu_info(struct vcpu *v, unsigned long gfn, unsigned offset)
 
      v->vcpu_info = new_info;
      v->vcpu_info_mfn = page_to_mfn(page);
+#ifdef CONFIG_MEM_SHARING
+    v->vcpu_info_offset = offset;

There's no need to introduce this field, you can just use v->vcpu_info & ~PAGE_MASK AFAICT.

Just doing what you suggest above results in:

Quoted 7 lines

mem_sharing.c:1603:55: error: invalid operands to binary & (have ‘vcpu_info_t * const’ {aka ‘union <anonymous> * const’} and ‘long int’)

                                      d_vcpu->vcpu_info & ~PAGE_MASK);

I can of course cast the vcpu_info pointer to (long int), it's just a bit ugly. Thoughts?

FWIW, I will also need the offset for liveupdate. I have used (unsigned long)v->vcpu_info & ~PAGE_MASK so far but this is not really pretty.

Quoted 8 lines
Quoted 5 lines

I think using:

(vaddr_t)v->vcpu_info & ~PAGE_MASK

is acceptable, but that would require adding a vaddr_t typedef to x86.

I don't think vaddr_t is necessary given that all over the place we assume conversions between pointers and unsigned long to be fine.

Right, using unsigned long is indeed fine, but I also agree with Tamas that it's slightly ugly and hence wanted to provide a 'cleaner' suggestion.

Roger.

#16 lore raw

Re: [Xen-devel] [PATCH v12 1/3] xen/mem_sharing: VM forking

In reply to Jan Beulich
Quoted 73 lines · Jan Beulich
Quoted 72 lines · Roger Pau Monné
Quoted 47 lines · Tamas K Lengyel
xen/arch/x86/mm/mem_sharing.c +47 -0
+static int copy_vcpu_settings(struct domain *cd, struct domain *d)
+{
+    unsigned int i;
+    struct p2m_domain *p2m = p2m_get_hostp2m(cd);
+    int ret = -EINVAL;
+
+    for ( i = 0; i < cd->max_vcpus; i++ )
+    {
+        const struct vcpu *d_vcpu = d->vcpu[i];
+        struct vcpu *cd_vcpu = cd->vcpu[i];
+        struct vcpu_runstate_info runstate;
+        mfn_t vcpu_info_mfn;
+
+        if ( !d_vcpu || !cd_vcpu )
+            continue;
+
+        /*
+         * Copy & map in the vcpu_info page if the guest uses one
+         */
+        vcpu_info_mfn = d_vcpu->vcpu_info_mfn;
+        if ( !mfn_eq(vcpu_info_mfn, INVALID_MFN) )
+        {
+            mfn_t new_vcpu_info_mfn = cd_vcpu->vcpu_info_mfn;
+
+            /*
+             * Allocate & map the page for it if it hasn't been already
+             */
+            if ( mfn_eq(new_vcpu_info_mfn, INVALID_MFN) )
+            {
+                gfn_t gfn = mfn_to_gfn(d, vcpu_info_mfn);
+                unsigned long gfn_l = gfn_x(gfn);
+                struct page_info *page;
+
+                if ( !(page = alloc_domheap_page(cd, 0)) )
+                    return -ENOMEM;
+
+                new_vcpu_info_mfn = page_to_mfn(page);
+                set_gpfn_from_mfn(mfn_x(new_vcpu_info_mfn), gfn_l);
+
+                ret = p2m->set_entry(p2m, gfn, new_vcpu_info_mfn, PAGE_ORDER_4K,
+                                     p2m_ram_rw, p2m->default_access, -1);
+                if ( ret )
+                    return ret;
+
+                ret = map_vcpu_info(cd_vcpu, gfn_l,
+                                    d_vcpu->vcpu_info_offset);
+                if ( ret )

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

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+                    return ret;
+            }
+
+            copy_domain_page(new_vcpu_info_mfn, vcpu_info_mfn);
+        }
+
+        /*
+         * Setup the vCPU runstate area
+         */
+        if ( guest_handle_is_null(runstate_guest(cd_vcpu)) )

Maybe I'm confused, but isn't this the other way around and you need to check? If the parent runstate is not null copy it to the fork, ie:

if ( !guest_handle_is_null(runstate_guest(d_vcpu)) )

{
    ...
Quoted 4 lines
xen/arch/x86/mm/mem_sharing.c +4 -0
+        {
+            runstate_guest(cd_vcpu) = runstate_guest(d_vcpu);
+            vcpu_runstate_get(cd_vcpu, &runstate);
+            __copy_to_guest(runstate_guest(cd_vcpu), &runstate, 1);
Quoted 4 lines
Quoted 1 line

You should check the return code I think.

I don't think so - this is a best effort operation just like e.g. in the handling of VCPUOP_register_runstate_memory_area.

I think printing a debug message might be helpful, not so much as for the importance of failing to copy the runstate area, but it could signal that something went wrong, anyway I don't have such a strong opinion.

Just to confirm, __copy_to_guest will cause the forked domain memory to be populated and the whole page to be copied over right? (and will also cause the page tables to be added to the fork physmap in write mode to set the accessed/dirty bits)

Thanks, Roger.

#23 lore raw

Re: [Xen-devel] [PATCH v12 2/3] x86/mem_sharing: reset a fork

In reply to Tamas K Lengyel

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

Quoted 8 lines · Tamas K Lengyel
xen/arch/x86/mm/mem_sharing.c +8 -0
+static int mem_sharing_fork_reset(struct domain *d, struct domain *pd)
+{
+    int rc;
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+    struct page_info *page, *tmp;
+
+    spin_lock(&d->page_alloc_lock);
+    domain_pause(d);

Why do you take the lock first?

Jan

Acked-by on v12 1/3
#18 lore raw

Re: [Xen-devel] [PATCH v12 1/3] xen/mem_sharing: VM forking

In reply to Tamas K Lengyel
Quoted 6 lines · Tamas K Lengyel
xen/arch/x86/domain.c +0 -0
@@ -2202,6 +2202,17 @@ int domain_relinquish_resources(struct domain *d)
             ret = relinquish_shared_pages(d);
             if ( ret )
                 return ret;

xen/arch/x86/domain.c int domain_relinquish_resources(struct domain *d) · line 2214

Quoted 10 lines
xen/arch/x86/domain.c +10 -0
+
+            /*
+             * If the domain is forked, decrement the parent's pause count
+             * and release the domain.
+             */
+            if ( mem_sharing_is_fork(d) )
+            {
+                domain_unpause(d->parent);
+                put_domain(d->parent);
+                d->parent = NULL;

I think you want to clear the field before putting the reference, to make sure possible readers of it won't see it non-NULL when the domain is already being cleaned up, or even gone.

With this, applicable parts of the change

I'll try to keep an eye on when you and Roger have settled on the remaining aspects, to determine when this (probably v13) can be committed.

xen/include/asm-x86/mem_sharing.h static inline int mem_sharing_unshare_page(struct domain *d, · line 80

Quoted 7 lines
xen/include/asm-x86/mem_sharing.h +1 -0
@@ -77,6 +77,14 @@ static inline int mem_sharing_unshare_page(struct domain *d,
     return rc;
 }
 
+static inline bool mem_sharing_is_fork(struct domain *d)

const? (then also in the stub further down)

Jan

#24 lore raw

Re: [Xen-devel] [PATCH v12 2/3] x86/mem_sharing: reset a fork

In reply to Jan Beulich
Quoted 12 lines · Jan Beulich

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

Quoted 8 lines · Tamas K Lengyel
xen/arch/x86/mm/mem_sharing.c +8 -0
+static int mem_sharing_fork_reset(struct domain *d, struct domain *pd)
+{
+    int rc;
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+    struct page_info *page, *tmp;
+
+    spin_lock(&d->page_alloc_lock);
+    domain_pause(d);

Why do you take the lock first?

No particular reason - does the order matter?

Tamas

#25 lore raw

Re: [Xen-devel] [PATCH v12 2/3] x86/mem_sharing: reset a fork

Quoted 14 lines · Tamas K Lengyel
Quoted 12 lines · Jan Beulich

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

Quoted 8 lines · Tamas K Lengyel
xen/arch/x86/mm/mem_sharing.c +8 -0
+static int mem_sharing_fork_reset(struct domain *d, struct domain *pd)
+{
+    int rc;
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+    struct page_info *page, *tmp;
+
+    spin_lock(&d->page_alloc_lock);
+    domain_pause(d);

Why do you take the lock first?

Quoted 1 line

No particular reason - does the order matter?

I think you'd better avoid holding a lock for extended periods of time. And what's perhaps worse, what if a vCPU of the domain sits in Xen trying to acquire this lock - you'd deadlock trying to pause the domain then.

Jan

#19 lore raw

Re: [Xen-devel] [PATCH v12 1/3] xen/mem_sharing: VM forking

In reply to Jan Beulich
Quoted 14 lines · Jan Beulich
Quoted 12 lines · Tamas K Lengyel
xen/arch/x86/domain.c +6 -0
@@ -2202,6 +2202,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 ( mem_sharing_is_fork(d) )
Quoted 8 lines

xen/arch/x86/domain.c int domain_relinquish_resources(struct domain *d) · line 2214

Quoted 4 lines
xen/arch/x86/domain.c +4 -0
+            {
+                domain_unpause(d->parent);
+                put_domain(d->parent);
+                d->parent = NULL;

I think you want to clear the field before putting the reference, to make sure possible readers of it won't see it non-NULL when the domain is already being cleaned up, or even gone.

Sure.

Quoted 7 lines

With this, applicable parts of the change

I'll try to keep an eye on when you and Roger have settled on the remaining aspects, to determine when this (probably v13) can be committed.

Thanks!

Quoted 10 lines

xen/include/asm-x86/mem_sharing.h static inline int mem_sharing_unshare_page(struct domain *d, · line 80

Quoted 8 lines
xen/include/asm-x86/mem_sharing.h +1 -0
@@ -77,6 +77,14 @@ static inline int mem_sharing_unshare_page(struct domain *d,
     return rc;
 }
 
+static inline bool mem_sharing_is_fork(struct domain *d)

const? (then also in the stub further down)

Sure.

Tamas

#26 lore raw

Re: [Xen-devel] [PATCH v12 2/3] x86/mem_sharing: reset a fork

In reply to Jan Beulich
Quoted 16 lines · Jan Beulich
Quoted 14 lines · Tamas K Lengyel
Quoted 12 lines · Jan Beulich

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

Quoted 8 lines · Tamas K Lengyel
xen/arch/x86/mm/mem_sharing.c +8 -0
+static int mem_sharing_fork_reset(struct domain *d, struct domain *pd)
+{
+    int rc;
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+    struct page_info *page, *tmp;
+
+    spin_lock(&d->page_alloc_lock);
+    domain_pause(d);

Why do you take the lock first?

Quoted 6 lines
Quoted 1 line

No particular reason - does the order matter?

I think you'd better avoid holding a lock for extended periods of time. And what's perhaps worse, what if a vCPU of the domain sits in Xen trying to acquire this lock - you'd deadlock trying to pause the domain then.

OK, I'll invert them order then.

Thanks,
Tamas

#17 lore raw

Re: [Xen-devel] [PATCH v12 1/3] xen/mem_sharing: VM forking

In reply to Roger Pau Monné
Quoted 85 lines · Roger Pau Monné
Quoted 73 lines · Jan Beulich
Quoted 72 lines · Roger Pau Monné
Quoted 47 lines · Tamas K Lengyel
xen/arch/x86/mm/mem_sharing.c +47 -0
+static int copy_vcpu_settings(struct domain *cd, struct domain *d)
+{
+    unsigned int i;
+    struct p2m_domain *p2m = p2m_get_hostp2m(cd);
+    int ret = -EINVAL;
+
+    for ( i = 0; i < cd->max_vcpus; i++ )
+    {
+        const struct vcpu *d_vcpu = d->vcpu[i];
+        struct vcpu *cd_vcpu = cd->vcpu[i];
+        struct vcpu_runstate_info runstate;
+        mfn_t vcpu_info_mfn;
+
+        if ( !d_vcpu || !cd_vcpu )
+            continue;
+
+        /*
+         * Copy & map in the vcpu_info page if the guest uses one
+         */
+        vcpu_info_mfn = d_vcpu->vcpu_info_mfn;
+        if ( !mfn_eq(vcpu_info_mfn, INVALID_MFN) )
+        {
+            mfn_t new_vcpu_info_mfn = cd_vcpu->vcpu_info_mfn;
+
+            /*
+             * Allocate & map the page for it if it hasn't been already
+             */
+            if ( mfn_eq(new_vcpu_info_mfn, INVALID_MFN) )
+            {
+                gfn_t gfn = mfn_to_gfn(d, vcpu_info_mfn);
+                unsigned long gfn_l = gfn_x(gfn);
+                struct page_info *page;
+
+                if ( !(page = alloc_domheap_page(cd, 0)) )
+                    return -ENOMEM;
+
+                new_vcpu_info_mfn = page_to_mfn(page);
+                set_gpfn_from_mfn(mfn_x(new_vcpu_info_mfn), gfn_l);
+
+                ret = p2m->set_entry(p2m, gfn, new_vcpu_info_mfn, PAGE_ORDER_4K,
+                                     p2m_ram_rw, p2m->default_access, -1);
+                if ( ret )
+                    return ret;
+
+                ret = map_vcpu_info(cd_vcpu, gfn_l,
+                                    d_vcpu->vcpu_info_offset);
+                if ( ret )

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

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10 -0
+                    return ret;
+            }
+
+            copy_domain_page(new_vcpu_info_mfn, vcpu_info_mfn);
+        }
+
+        /*
+         * Setup the vCPU runstate area
+         */
+        if ( guest_handle_is_null(runstate_guest(cd_vcpu)) )

Maybe I'm confused, but isn't this the other way around and you need to check? If the parent runstate is not null copy it to the fork, ie:

if ( !guest_handle_is_null(runstate_guest(d_vcpu)) )

{
    ...
Quoted 4 lines
xen/arch/x86/mm/mem_sharing.c +4 -0
+        {
+            runstate_guest(cd_vcpu) = runstate_guest(d_vcpu);
+            vcpu_runstate_get(cd_vcpu, &runstate);
+            __copy_to_guest(runstate_guest(cd_vcpu), &runstate, 1);
Quoted 4 lines
Quoted 1 line

You should check the return code I think.

I don't think so - this is a best effort operation just like e.g. in the handling of VCPUOP_register_runstate_memory_area.

I think printing a debug message might be helpful, not so much as for the importance of failing to copy the runstate area, but it could signal that something went wrong, anyway I don't have such a strong opinion.

Quoted 4 lines

Just to confirm, __copy_to_guest will cause the forked domain memory to be populated and the whole page to be copied over right? (and will also cause the page tables to be added to the fork physmap in write mode to set the accessed/dirty bits)

I checked this and it ends up calling hvm_translate_get_page which issues a call to get_page_from_gfn already with P2M_UNSHARE already. The problem is that we are still in the process of forking the VM, so mem_sharing_is_fork is not yet true, since we haven't finished the process yet completely. So what I'll do is set the parent pointer early which will allow memory to be populated from the parent. If there is an error during the fork operation the fork domain is getting destroyed by the toolstack anyway so we don't have to worry about unwinding a half-way completed fork.

Tamas

#27 lore raw

Re: [Xen-devel] [PATCH v12 2/3] x86/mem_sharing: reset a fork

In reply to Tamas K Lengyel
Quoted 18 lines · Tamas K Lengyel
Quoted 16 lines · Jan Beulich
Quoted 14 lines · Tamas K Lengyel
Quoted 12 lines · Jan Beulich

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

Quoted 8 lines · Tamas K Lengyel
xen/arch/x86/mm/mem_sharing.c +8 -0
+static int mem_sharing_fork_reset(struct domain *d, struct domain *pd)
+{
+    int rc;
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+    struct page_info *page, *tmp;
+
+    spin_lock(&d->page_alloc_lock);
+    domain_pause(d);

Why do you take the lock first?

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

No particular reason - does the order matter?

I think you'd better avoid holding a lock for extended periods of time. And what's perhaps worse, what if a vCPU of the domain sits in Xen trying to acquire this lock - you'd deadlock trying to pause the domain then.

OK, I'll invert them order then.

It turns out we also need to take the recursive lock here since we'll free the pages. Fixed now and everything works as expected.

Tamas