VM forking
v16 cover VM forking
The following patches are part of the series that implement 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 v16:
A better bugfix for fork reset issue
Minor fixes for the IOMMU allow patch based on feedback
Patch 1 fix for VM fork reset removing pages from the p2m that it shouldn't Patch 2 adds option to fork a domain with IOMMU active
Patch 3 adds the toolstack-side code implementing VM forking and reset
Tamas K Lengyel (3):
mem_sharing: fix sharability check during fork reset
mem_sharing: allow forking domain with IOMMU enabled xen/tools: VM forking toolstack side
docs/man/xl.1.pod.in | 44 +++++ tools/libxc/include/xenctrl.h | 14 ++ tools/libxc/xc_memshr.c | 26 +++ tools/libxl/libxl.h | 12 ++ tools/libxl/libxl_create.c | 361 +++++++++++++++++++--------------- tools/libxl/libxl_dm.c | 2 +- tools/libxl/libxl_dom.c | 43 +++- tools/libxl/libxl_internal.h | 7 + tools/libxl/libxl_types.idl | 1 + tools/libxl/libxl_x86.c | 42 ++++ tools/xl/Makefile | 2 +- tools/xl/xl.h | 5 + tools/xl/xl_cmdtable.c | 15 ++ tools/xl/xl_forkvm.c | 149 ++++++++++++++ tools/xl/xl_vmcontrol.c | 14 ++ xen/arch/x86/mm/mem_sharing.c | 99 ++++++---- xen/include/public/memory.h | 4 +- 17 files changed, 637 insertions(+), 203 deletions(-) create mode 100644 tools/xl/xl_forkvm.c
Signature · 1 line
2.20.1
PATCH v16 1/3 mem_sharing: fix sharability check during fork reset
When resetting a VM fork we ought to only remove pages that were allocated for the fork during it's execution and the contents copied over from the parent. This can be determined if the page is sharable as special pages used by the fork for other purposes will not pass this test. Unfortunately during the fork reset loop we only partially check whether that's the case. A page's type may indicate it is sharable (pass p2m_is_sharable) but that's not a sufficient check by itself. All checks that are normally performed before a page is converted to the sharable type need to be performed to avoid removing pages from the p2m that may be used for other purposes. For example, currently the reset loop also removes the vcpu info pages from the p2m, potentially putting the guest into infinite page-fault loops.
For this we extend the existing nominate_page and page_make_sharable functions to perform a validation-only run without actually converting the page.
--- xen/arch/x86/mm/mem_sharing.c | 79 ++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 29 deletions(-)
xen/arch/x86/mm/mem_sharing.c +50 -29
@@ -633,31 +633,35 @@ unsigned int mem_sharing_get_nr_shared_mfns(void)/* Functions that change a page's type and ownership */ static int page_make_sharable (struct domain *d,struct page_info *page, -int expected_refcnt) +int expected_refcnt, +bool validate_only) { -bool_t drop_dom_ref; +int rc; +bool drop_dom_ref = false; -spin_lock (&d->page_alloc_lock); +/* caller already has the lock when validating only */ +if ( !validate_only ) +spin_lock (&d->page_alloc_lock);if ( d->is_dying ) { -spin_unlock (&d->page_alloc_lock); -return -EBUSY ; + rc = -EBUSY ; + goto out; }/* Change page type and count atomically */ if ( !get_page_and_type (page, d, PGT_shared_page) ) { -spin_unlock (&d->page_alloc_lock); -return -EINVAL ; + rc = -EINVAL ; + goto out; }/* Check it wasn't already sharable and undo if it was */ if ( (page->u.inuse.type_info & PGT_count_mask) !=1 ) { -spin_unlock (&d->page_alloc_lock);put_page_and_type (page); -return -EEXIST ; + rc = -EEXIST ; + goto out; }/*
@@ -666,20 +670,31 @@ static int page_make_sharable(struct domain *d,*/ if ( page->count_info != (PGC_allocated | (2 + expected_refcnt)) ) { -spin_unlock (&d->page_alloc_lock);/* Return type count back to zero */ put_page_and_type (page); -return -E2BIG ; + rc = -E2BIG ; + goto out; + } + + rc =0 ; + +if ( validate_only ) + { +put_page_and_type (page); + goto out; }page_set_owner (page, dom_cow); drop_dom_ref = !domain_adjust_tot_pages (d,-1 );page_list_del (page, &d->page_list); -spin_unlock (&d->page_alloc_lock); +out: +if ( !validate_only ) +spin_unlock (&d->page_alloc_lock);if ( drop_dom_ref )put_domain (d); -return 0 ; + +return rc; }static int page_make_private (struct domain *d,struct page_info *page)
@@ -809,8 +824,8 @@ static int debug_gref(struct domain *d, grant_ref_t ref)return debug_gfn (d, gfn); } -static int nominate_page (struct domain *d,gfn_t gfn, -int expected_refcnt,shr_handle_t *phandle) +static int nominate_page (struct domain *d,gfn_t gfn,int expected_refcnt, +bool validate_only,shr_handle_t *phandle) {struct p2m_domain *hp2m =p2m_get_hostp2m (d);p2m_type_t p2mt;
@@ -879,8 +894,8 @@ static int nominate_page(struct domain *d, gfn_t gfn, }/* Try to convert the mfn to the sharable type */ - ret =page_make_sharable (d, page, expected_refcnt); -if ( ret ) + ret =page_make_sharable (d, page, expected_refcnt, validate_only); +if ( ret || validate_only ) goto out;/*
@@ -1392,13 +1407,13 @@ static int range_share(struct domain *d, struct domain *cd,* We only break out if we run out of memory as individual pages may * legitimately be unsharable and we just want to skip over those. */ - rc =nominate_page (d,_gfn (start),0 , &sh); + rc =nominate_page (d,_gfn (start),0 , false, &sh);if ( rc == -ENOMEM )break ;if ( !rc ) { - rc =nominate_page (cd,_gfn (start),0 , &ch); + rc =nominate_page (cd,_gfn (start),0 , false, &ch);if ( rc == -ENOMEM )break ;
@@ -1476,7 +1491,7 @@ int mem_sharing_fork_page(struct domain *d, gfn_t gfn, bool unsharing)/* For read-only accesses we just add a shared entry to the physmap */ while ( parent ) { -if ( !(rc =nominate_page (parent, gfn,0 , &handle)) ) +if ( !(rc =nominate_page (parent, gfn,0 , false, &handle)) )break ; parent = parent->parent;
@@ -1773,16 +1788,22 @@ static int mem_sharing_fork_reset(struct domain *d, struct domain *pd)spin_lock_recursive (&d->page_alloc_lock);page_list_for_each_safe (page, tmp, &d->page_list) { -p2m_type_t p2mt; -p2m_access_t p2ma; +shr_handle_t sh;mfn_t mfn =page_to_mfn (page);gfn_t gfn =mfn_to_gfn (d, mfn); - mfn =__get_gfn_type_access (p2m,gfn_x (gfn), &p2mt, &p2ma, -0 ,NULL , false); - -/* only reset pages that are sharable */ -if ( !p2m_is_sharable (p2mt) ) +/* +* We only want to remove pages from the fork here that were copied +* from the parent but could be potentially re-populated using memory +* sharing after the reset. These pages all must be regular pages with +* no extra reference held to them, thus should be possible to make +* them sharable. Unfortunately p2m_is_sharable check is not sufficient +* to test this as it doesn't check the page's reference count. We thus +* check whether the page is convertable to the shared type using +* nominate_page. In case the page is already shared (ie. a share +* handle is returned) then we don't remove it. +*/ +if ( (rc =nominate_page (d, gfn,0 , true, &sh)) || sh )continue ;/* take an extra reference or just skip if can't for whatever reason */
@@ -1836,7 +1857,7 @@ int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg) {shr_handle_t handle; - rc =nominate_page (d,_gfn (mso.u.nominate.u.gfn),0 , &handle); + rc =nominate_page (d,_gfn (mso.u.nominate.u.gfn),0 , false, &handle); mso.u.nominate.handle = handle; }break ;
@@ -1851,7 +1872,7 @@ int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg)if ( rc <0 ) goto out; - rc =nominate_page (d, gfn,3 , &handle); + rc =nominate_page (d, gfn,3 , false, &handle); mso.u.nominate.handle = handle; }break ;
Signature · 1 line
2.20.1
Re: [PATCH v16 1/3] mem_sharing: fix sharability check during fork reset
Quoted 4 lines · Tamas K Lengyel
When resetting a VM fork we ought to only remove pages that were allocated for the fork during it's execution and the contents copied over from the parent. This can be determined if the page is sharable as special pages used by the fork for other purposes will not pass this test.
Would it be easier to just check if the page refcount is > 1? (as I expect Xen is also holding a reference to this page)
Quoted 11 lines
Unfortunately during the fork
reset loop we only partially check whether that's the case. A page's type may indicate it is sharable (passp2m_is_sharable) but that's not a sufficient check by itself. All checks that are normally performed before a page is converted to the sharable type need to be performed to avoid removing pages from the p2m that may be used for other purposes. For example, currently the reset loop also removes the vcpu info pages from the p2m, potentially putting the guest into infinite page-fault loops.For this we extend the existing
nominate_pageandpage_make_sharablefunctions to perform a validation-only run without actually converting the page.
Maybe you could split that chunk into a separate helper that just performs the checks?
Quoted 15 lines
Signed-off-by Tamas K Lengyel <[email protected]>--- xen/arch/x86/mm/mem_sharing.c | 79 ++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 29 deletions(-)xen/arch/x86/mm/mem_sharing.c +1
-1@@ -633,31 +633,35 @@ unsigned int mem_sharing_get_nr_shared_mfns(void)/* Functions that change a page's type and ownership */ static int page_make_sharable(struct domain *d, struct page_info *page, -int expected_refcnt) + int expected_refcnt,
xen/arch/x86/mm/mem_sharing.c unsigned int mem_sharing_get_nr_shared_mfns(void) · line 644
Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +6
-2+ bool validate_only) { -bool_t drop_dom_ref; +int rc; +bool drop_dom_ref = false; -spin_lock (&d->page_alloc_lock); +/* caller already has the lock when validating only */ +if ( !validate_only ) +spin_lock (&d->page_alloc_lock);
page_alloc_lock seems to be used as a recursive lock by some callers, could you do the same here?
Quoted 36 lines
xen/arch/x86/mm/mem_sharing.c +6
-7if ( d->is_dying ) { -spin_unlock (&d->page_alloc_lock); -return -EBUSY ; + rc = -EBUSY ; + goto out; }/* Change page type and count atomically */ if ( !get_page_and_type (page, d, PGT_shared_page) ) { -spin_unlock (&d->page_alloc_lock); -return -EINVAL ; + rc = -EINVAL ; + goto out; }/* Check it wasn't already sharable and undo if it was */ if ( (page->u.inuse.type_info & PGT_count_mask) !=1 ) { -spin_unlock (&d->page_alloc_lock);put_page_and_type (page); -return -EEXIST ; + rc = -EEXIST ; + goto out; }/* @@ -666,20 +670,31 @@ static int page_make_sharable(struct domain *d,*/ if ( page->count_info != (PGC_allocated | (2 + expected_refcnt)) ) { -spin_unlock (&d->page_alloc_lock);/* Return type count back to zero */ put_page_and_type (page);
xen/arch/x86/mm/mem_sharing.c static int page_make_sharable(struct domain *d, · line 683
Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +9
-1-return -E2BIG ; + rc = -E2BIG ; + goto out; + } + + rc =0 ; + +if ( validate_only ) + { +put_page_and_type (page);
You seem to check some page attributes but then put the page again, which looks racy to me. Since you put the page, couldn't the checks that you have performed be stale by the point the data is consumed by the caller?
Quoted 20 lines
xen/arch/x86/mm/mem_sharing.c +6
-2+ goto out; }page_set_owner (page, dom_cow); drop_dom_ref = !domain_adjust_tot_pages (d,-1 );page_list_del (page, &d->page_list); -spin_unlock (&d->page_alloc_lock); +out: +if ( !validate_only ) +spin_unlock (&d->page_alloc_lock);if ( drop_dom_ref )put_domain (d); -return 0 ; + +return rc; }static int page_make_private (struct domain *d,struct page_info *page)
xen/arch/x86/mm/mem_sharing.c static int debug_gref(struct domain *d, grant_ref_t ref) · line 827
Quoted 6 lines
xen/arch/x86/mm/mem_sharing.c +1
-2@@ -809,8 +824,8 @@ static int debug_gref(struct domain *d, grant_ref_t ref)return debug_gfn (d, gfn); } -static int nominate_page (struct domain *d,gfn_t gfn, -int expected_refcnt,shr_handle_t *phandle) +static int nominate_page(struct domain *d, gfn_t gfn, int expected_refcnt,
Is there any reason for expected_refcnt to be signed? All callers use unsigned values.
Thanks, Roger.
Re: [PATCH v16 1/3] mem_sharing: fix sharability check during fork reset
Quoted 9 lines · Roger Pau Monné
Quoted 4 lines · Tamas K Lengyel
When resetting a VM fork we ought to only remove pages that were allocated for the fork during it's execution and the contents copied over from the parent. This can be determined if the page is sharable as special pages used by the fork for other purposes will not pass this test.
Would it be easier to just check if the page refcount is > 1? (as I expect Xen is also holding a reference to this page)
That by itself is not necessarily enough.
Quoted 10 lines
Quoted 10 lines
Unfortunately during the fork
reset loop we only partially check whether that's the case. A page's type may indicate it is sharable (passp2m_is_sharable) but that's not a sufficient check by itself. All checks that are normally performed before a page is converted to the sharable type need to be performed to avoid removing pages from the p2m that may be used for other purposes. For example, currently the reset loop also removes the vcpu info pages from the p2m, potentially putting the guest into infinite page-fault loops.
Quoted 5 lines
Quoted 2 lines
For this we extend the existing
nominate_pageandpage_make_sharablefunctions to perform a validation-only run without actually converting the page.Maybe you could split that chunk into a separate helper that just performs the checks?
I think it's fine this way that we just bail half-way through the process of making the page shared. Splitting this out to a helper would require a lot more code-shuffling.
Quoted 22 lines
Quoted 22 lines
Signed-off-by Tamas K Lengyel <[email protected]>--- xen/arch/x86/mm/mem_sharing.c | 79 ++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 29 deletions(-)xen/arch/x86/mm/mem_sharing.c +4
-2@@ -633,31 +633,35 @@ unsigned int mem_sharing_get_nr_shared_mfns(void)/* Functions that change a page's type and ownership */ static int page_make_sharable (struct domain *d,struct page_info *page, -int expected_refcnt) +int expected_refcnt, +bool validate_only) { -bool_t drop_dom_ref; +int rc; +bool drop_dom_ref = false;
Quoted 7 lines
xen/arch/x86/mm/mem_sharing.c unsigned int mem_sharing_get_nr_shared_mfns(void) · line 644
Quoted 4 lines
xen/arch/x86/mm/mem_sharing.c +3
-1-spin_lock (&d->page_alloc_lock); +/* caller already has the lock when validating only */ +if ( !validate_only ) +spin_lock (&d->page_alloc_lock);
page_alloc_lockseems to be used as a recursive lock by some callers, could you do the same here?
We can do that, yes.
Quoted 44 lines
Quoted 44 lines
xen/arch/x86/mm/mem_sharing.c +12
-8if ( d->is_dying ) { -spin_unlock (&d->page_alloc_lock); -return -EBUSY ; + rc = -EBUSY ; + goto out; }/* Change page type and count atomically */ if ( !get_page_and_type (page, d, PGT_shared_page) ) { -spin_unlock (&d->page_alloc_lock); -return -EINVAL ; + rc = -EINVAL ; + goto out; }/* Check it wasn't already sharable and undo if it was */ if ( (page->u.inuse.type_info & PGT_count_mask) !=1 ) { -spin_unlock (&d->page_alloc_lock);put_page_and_type (page); -return -EEXIST ; + rc = -EEXIST ; + goto out; }/* @@ -666,20 +670,31 @@ static int page_make_sharable(struct domain *d,*/ if ( page->count_info != (PGC_allocated | (2 + expected_refcnt)) ) { -spin_unlock (&d->page_alloc_lock);/* Return type count back to zero */ put_page_and_type (page); -return -E2BIG ; + rc = -E2BIG ; + goto out; + } + + rc =0 ; +
Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c static int page_make_sharable(struct domain *d, · line 683
Quoted 3 lines
xen/arch/x86/mm/mem_sharing.c +3
-0+if ( validate_only ) + { +put_page_and_type (page);You seem to check some page attributes but then put the page again, which looks racy to me. Since you put the page, couldn't the checks that you have performed be stale by the point the data is consumed by the caller?
During fork reset when this is called with validate_only = true the domain is paused. Furthermore, fork reset is only for forks that have no device model running, so nothing is interacting with its memory that could acquire extra references. So no, this isn't racy since there is nothing to race against that I'm aware of. Also, this check is really to check for special pages, all of which are setup during the initial fork process, not during runtime of the fork.
Quoted 24 lines
Quoted 24 lines
xen/arch/x86/mm/mem_sharing.c +6
-2+ goto out; }page_set_owner (page, dom_cow); drop_dom_ref = !domain_adjust_tot_pages (d,-1 );page_list_del (page, &d->page_list); -spin_unlock (&d->page_alloc_lock); +out: +if ( !validate_only ) +spin_unlock (&d->page_alloc_lock);if ( drop_dom_ref )put_domain (d); -return 0 ; + +return rc; }static int page_make_private (struct domain *d,struct page_info *page)@@ -809,8 +824,8 @@ static int debug_gref(struct domain *d, grant_ref_t ref)return debug_gfn (d, gfn); }
Quoted 6 lines
xen/arch/x86/mm/mem_sharing.c static int debug_gref(struct domain *d, grant_ref_t ref) · line 827
Quoted 3 lines
xen/arch/x86/mm/mem_sharing.c +1
-2-static int nominate_page (struct domain *d,gfn_t gfn, -int expected_refcnt,shr_handle_t *phandle) +static int nominate_page(struct domain *d, gfn_t gfn, int expected_refcnt,Is there any reason for
expected_refcntto be signed? All callers use unsigned values.
No reason. It's just how the code was written by the original author and we never changed it.
Tamas
Re: [PATCH v16 1/3] mem_sharing: fix sharability check during fork reset
Quoted 26 lines · Tamas K Lengyel
Quoted 16 lines · Roger Pau Monné
Quoted 14 lines · Tamas K Lengyel
xen/arch/x86/mm/mem_sharing.c +6
-2@@ -666,20 +670,31 @@ static int page_make_sharable(struct domain *d,*/ if ( page->count_info != (PGC_allocated | (2 + expected_refcnt)) ) { -spin_unlock (&d->page_alloc_lock);/* Return type count back to zero */ put_page_and_type (page); -return -E2BIG ; + rc = -E2BIG ; + goto out; + } + + rc =0 ; +Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c static int page_make_sharable(struct domain *d, · line 683
Quoted 3 lines
xen/arch/x86/mm/mem_sharing.c +3
-0+if ( validate_only ) + { +put_page_and_type (page);You seem to check some page attributes but then put the page again, which looks racy to me. Since you put the page, couldn't the checks that you have performed be stale by the point the data is consumed by the caller?
Quoted 7 lines
During fork reset when this is called with
validate_only= true the domain is paused. Furthermore, fork reset is only for forks that have no device model running, so nothing is interacting with its memory that could acquire extra references. So no, this isn't racy since there is nothing to race against that I'm aware of. Also, this check is really to check for special pages, all of which are setup during the initial fork process, not during runtime of the fork.
Right, it would feel safer to me however if you just return from page_make_sharable while having a page reference, and drop it in mem_sharing_fork_reset if the page shouldn't be removed from the fork.
This way you could also avoid having to take an extra reference just after returning from nominate_page in mem_sharing_fork_reset. page_make_sharable already returns while having taken an extra reference to the page in the non validate only case anyway.
Quoted 31 lines
Quoted 24 lines
Quoted 24 lines
xen/arch/x86/mm/mem_sharing.c +6
-2+ goto out; }page_set_owner (page, dom_cow); drop_dom_ref = !domain_adjust_tot_pages (d,-1 );page_list_del (page, &d->page_list); -spin_unlock (&d->page_alloc_lock); +out: +if ( !validate_only ) +spin_unlock (&d->page_alloc_lock);if ( drop_dom_ref )put_domain (d); -return 0 ; + +return rc; }static int page_make_private (struct domain *d,struct page_info *page)@@ -809,8 +824,8 @@ static int debug_gref(struct domain *d, grant_ref_t ref)return debug_gfn (d, gfn); }Quoted 6 lines
xen/arch/x86/mm/mem_sharing.c static int debug_gref(struct domain *d, grant_ref_t ref) · line 827
Quoted 3 lines
xen/arch/x86/mm/mem_sharing.c +1
-2-static int nominate_page (struct domain *d,gfn_t gfn, -int expected_refcnt,shr_handle_t *phandle) +static int nominate_page(struct domain *d, gfn_t gfn, int expected_refcnt,Is there any reason for
expected_refcntto be signed? All callers use unsigned values.
Quoted 2 lines
No reason. It's just how the code was written by the original author and we never changed it.
Since you are already changing those lines, can I ask you to also change it to unsigned in the places that you touch?
Thanks, Roger.
Re: [PATCH v16 1/3] mem_sharing: fix sharability check during fork reset
Quoted 36 lines · Roger Pau Monné
Quoted 26 lines · Tamas K Lengyel
Quoted 16 lines · Roger Pau Monné
Quoted 14 lines · Tamas K Lengyel
xen/arch/x86/mm/mem_sharing.c +6
-2@@ -666,20 +670,31 @@ static int page_make_sharable(struct domain *d,*/ if ( page->count_info != (PGC_allocated | (2 + expected_refcnt)) ) { -spin_unlock (&d->page_alloc_lock);/* Return type count back to zero */ put_page_and_type (page); -return -E2BIG ; + rc = -E2BIG ; + goto out; + } + + rc =0 ; +Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c static int page_make_sharable(struct domain *d, · line 683
Quoted 3 lines
xen/arch/x86/mm/mem_sharing.c +3
-0+if ( validate_only ) + { +put_page_and_type (page);You seem to check some page attributes but then put the page again, which looks racy to me. Since you put the page, couldn't the checks that you have performed be stale by the point the data is consumed by the caller?
Quoted 7 lines
During fork reset when this is called with
validate_only= true the domain is paused. Furthermore, fork reset is only for forks that have no device model running, so nothing is interacting with its memory that could acquire extra references. So no, this isn't racy since there is nothing to race against that I'm aware of. Also, this check is really to check for special pages, all of which are setup during the initial fork process, not during runtime of the fork.
Quoted 8 lines
Right, it would feel safer to me however if you just return from
page_make_sharablewhile having a page reference, and drop it inmem_sharing_fork_resetif the page shouldn't be removed from the fork.This way you could also avoid having to take an extra reference just after returning from
nominate_pageinmem_sharing_fork_reset.page_make_sharablealready returns while having taken an extra reference to the page in the non validate only case anyway.
Ah yea, that would make sense. Good idea!
Quoted 31 lines
Quoted 31 lines
Quoted 30 lines
Quoted 21 lines
xen/arch/x86/mm/mem_sharing.c +6
-2+ goto out; }page_set_owner (page, dom_cow); drop_dom_ref = !domain_adjust_tot_pages (d,-1 );page_list_del (page, &d->page_list); -spin_unlock (&d->page_alloc_lock); +out: +if ( !validate_only ) +spin_unlock (&d->page_alloc_lock);if ( drop_dom_ref )put_domain (d); -return 0 ; + +return rc; }static int page_make_private (struct domain *d,struct page_info *page)xen/arch/x86/mm/mem_sharing.c static int debug_gref(struct domain *d, grant_ref_t ref) · line 827
Quoted 6 lines
xen/arch/x86/mm/mem_sharing.c +1
-2@@ -809,8 +824,8 @@ static int debug_gref(struct domain *d, grant_ref_t ref)return debug_gfn (d, gfn); } -static int nominate_page (struct domain *d,gfn_t gfn, -int expected_refcnt,shr_handle_t *phandle) +static int nominate_page(struct domain *d, gfn_t gfn, int expected_refcnt,Is there any reason for
expected_refcntto be signed? All callers use unsigned values.
Quoted 5 lines
Quoted 2 lines
No reason. It's just how the code was written by the original author and we never changed it.
Since you are already changing those lines, can I ask you to also change it to unsigned in the places that you touch?
Sure thing.
Thanks,
Tamas
PATCH v16 2/3 mem_sharing: allow forking domain with IOMMU enabled
In reply to Tamas K LengyelThe memory sharing subsystem by default doesn't allow a domain to share memory if it has an IOMMU active for obvious security reasons. However, when fuzzing a VM fork, the same security restrictions don't necessarily apply. While it makes no sense to try to create a full fork of a VM that has an IOMMU attached as only one domain can own the pass-through device at a time, creating a shallow fork without a device model is still very useful for fuzzing kernel-mode drivers.
By allowing the parent VM to initialize the kernel-mode driver with a real device that's pass-through, the driver can enter into a state more suitable for fuzzing. Some of these initialization steps are quite complex and are easier to perform when a real device is present. After the initialization, shallow forks can be utilized for fuzzing code-segments in the device driver that don't directly interact with the device.
---
v16: Minor fixes based on feedback
--- xen/arch/x86/mm/mem_sharing.c | 20 +++++++++++++------- xen/include/public/memory.h | 4 +++- 2 files changed, 16 insertions(+), 8 deletions(-)
xen/arch/x86/mm/mem_sharing.c +13 -7
@@ -1445,7 +1445,8 @@ static int range_share(struct domain *d, struct domain *cd,return rc; } -static inline int mem_sharing_control (struct domain *d,bool enable) +static inline int mem_sharing_control (struct domain *d,bool enable, +uint16_t flags) {if ( enable ) {
@@ -1455,7 +1456,8 @@ static inline int mem_sharing_control(struct domain *d, bool enable)if (unlikely (!hap_enabled (d)) )return -ENODEV ; -if (unlikely (is_iommu_enabled (d)) ) +if (unlikely (is_iommu_enabled (d) && + !(flags &XENMEM_FORK_WITH_IOMMU_ALLOWED )) )return -EXDEV ; }
@@ -1848,7 +1850,8 @@ int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg)if ( rc ) goto out; -if ( !mem_sharing_enabled (d) && (rc =mem_sharing_control (d, true)) ) +if ( !mem_sharing_enabled (d) && + (rc =mem_sharing_control (d, true,0 )) )return rc; switch ( mso.op )
@@ -2086,7 +2089,9 @@ int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg)struct domain *pd; rc = -EINVAL ; -if ( mso.u.fork.pad[0 ] || mso.u.fork.pad[1 ] || mso.u.fork.pad[2 ] ) +if ( mso.u.fork.pad ) + goto out; +if ( mso.u.fork.flags & ~XENMEM_FORK_WITH_IOMMU_ALLOWED ) goto out; rc = rcu_lock_live_remote_domain_by_id(mso.u.fork.parent_domain,
@@ -2101,7 +2106,8 @@ int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg) goto out; } -if ( !mem_sharing_enabled (pd) && (rc =mem_sharing_control (pd, true)) ) +if ( !mem_sharing_enabled (pd) && + (rc =mem_sharing_control (pd, true, mso.u.fork.flags)) ) {rcu_unlock_domain (pd); goto out;
@@ -2122,7 +2128,7 @@ int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg)struct domain *pd; rc = -EINVAL ; -if ( mso.u.fork.pad[0 ] || mso.u.fork.pad[1 ] || mso.u.fork.pad[2 ] ) +if ( mso.u.fork.pad || mso.u.fork.flags ) goto out; rc = -ENOSYS ;
@@ -2159,7 +2165,7 @@ int mem_sharing_domctl(struct domain *d, struct xen_domctl_mem_sharing_op *mec)switch ( mec->op ) {case XEN_DOMCTL_MEM_SHARING_CONTROL : - rc =mem_sharing_control (d, mec->u.enable); + rc =mem_sharing_control (d, mec->u.enable,0 );break ;default :
xen/include/public/memory.h +3 -1
@@ -536,7 +536,9 @@ struct xen_mem_sharing_op { } 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 */ +#define XENMEM_FORK_WITH_IOMMU_ALLOWED (1u << 0) +uint16_t flags;/* IN: optional settings */ +uint32_t pad;/* Must be set to 0 */ } fork; } u; };
Signature · 1 line
2.20.1
Re: [PATCH v16 2/3] mem_sharing: allow forking domain with IOMMU enabled
Quoted 7 lines · Tamas K Lengyel
The memory sharing subsystem by default doesn't allow a domain to share memory if it has an IOMMU active for obvious security reasons. However, when fuzzing a VM fork, the same security restrictions don't necessarily apply. While it makes no sense to try to create a full fork of a VM that has an IOMMU attached as only one domain can own the pass-through device at a time, creating a shallow fork without a device model is still very useful for fuzzing kernel-mode drivers.
Quoted 8 lines
By allowing the parent VM to initialize the kernel-mode driver with a real device that's pass-through, the driver can enter into a state more suitable for fuzzing. Some of these initialization steps are quite complex and are easier to perform when a real device is present. After the initialization, shallow forks can be utilized for fuzzing code-segments in the device driver that don't directly interact with the device.
Signed-off-by Tamas K Lengyel <[email protected]>
Thanks.
Re: [PATCH v16 2/3] mem_sharing: allow forking domain with IOMMU enabled
Quoted 16 lines · Roger Pau Monné
Quoted 14 lines · Tamas K Lengyel
The memory sharing subsystem by default doesn't allow a domain to share memory if it has an IOMMU active for obvious security reasons. However, when fuzzing a VM fork, the same security restrictions don't necessarily apply. While it makes no sense to try to create a full fork of a VM that has an IOMMU attached as only one domain can own the pass-through device at a time, creating a shallow fork without a device model is still very useful for fuzzing kernel-mode drivers.
By allowing the parent VM to initialize the kernel-mode driver with a real device that's pass-through, the driver can enter into a state more suitable for fuzzing. Some of these initialization steps are quite complex and are easier to perform when a real device is present. After the initialization, shallow forks can be utilized for fuzzing code-segments in the device driver that don't directly interact with the device.
Quoted 3 lines
Quoted 1 line
Signed-off-by Tamas K Lengyel <[email protected]>Reviewed-by Roger Pau Monné <[email protected]>
Thanks! This can be merged independent of the other patches in the series.
Tamas
PATCH v16 3/3 xen/tools: VM forking toolstack side
In reply to Tamas K LengyelAdd 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 | 14 ++ tools/libxc/xc_memshr.c | 26 +++ tools/libxl/libxl.h | 12 ++ tools/libxl/libxl_create.c | 361 +++++++++++++++++++--------------- tools/libxl/libxl_dm.c | 2 +- tools/libxl/libxl_dom.c | 43 +++- tools/libxl/libxl_internal.h | 7 + tools/libxl/libxl_types.idl | 1 + tools/libxl/libxl_x86.c | 42 ++++ tools/xl/Makefile | 2 +- tools/xl/xl.h | 5 + tools/xl/xl_cmdtable.c | 15 ++ tools/xl/xl_forkvm.c | 149 ++++++++++++++ tools/xl/xl_vmcontrol.c | 14 ++ 15 files changed, 571 insertions(+), 166 deletions(-) create mode 100644 tools/xl/xl_forkvm.c
docs/man/xl.1.pod.in +44 -0
@@ -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 +14 -0
@@ -2232,6 +2232,20 @@ int xc_memshr_range_share(xc_interface *xch,uint64_t first_gfn, uint64_t last_gfn); +int xc_memshr_fork (xc_interface *xch, +uint32_t source_domain, +uint32_t client_domain, +bool allow_with_iommu); + +/* +* Note: this function is only intended to be used on short-lived forks that +* haven't yet aquired a lot of memory. In case the fork has a lot of memory +* it is likely more performant to create a new fork with xc_memshr_fork. +* +* With VMs that have a lot of memory this call may block for a long time. +*/ +int xc_memshr_fork_reset (xc_interface *xch,uint32_t forked_domain); +/* Debug calls: return the number of pages referencing the shared frame backing * the input argument. Should be one or greater. *
tools/libxc/xc_memshr.c +26 -0
@@ -239,6 +239,32 @@ int xc_memshr_debug_gref(xc_interface *xch,return xc_memshr_memop (xch, domid, &mso); } +int xc_memshr_fork (xc_interface *xch,uint32_t pdomid,uint32_t domid, +bool allow_with_iommu) +{ +xen_mem_sharing_op_t mso; + +memset (&mso,0 ,sizeof (mso)); + + mso.op = XENMEM_sharing_op_fork; + mso.u.fork.parent_domain = pdomid; + +if ( allow_with_iommu ) + mso.u.fork.flags |=XENMEM_FORK_WITH_IOMMU_ALLOWED ; + +return xc_memshr_memop (xch, domid, &mso); +} + +int xc_memshr_fork_reset (xc_interface *xch,uint32_t domid) +{ +xen_mem_sharing_op_t mso; + +memset (&mso,0 ,sizeof (mso)); + mso.op = XENMEM_sharing_op_fork_reset; + +return xc_memshr_memop (xch, domid, &mso); +} +int xc_memshr_audit (xc_interface *xch) {xen_mem_sharing_op_t mso;
tools/libxl/libxl.h +12 -0
@@ -2666,6 +2666,18 @@ int libxl_psr_get_hw_info(libxl_ctx *ctx, libxl_psr_feat_type type, unsigned int lvl, unsigned int *nr, libxl_psr_hw_info **info);void libxl_psr_hw_info_list_free (libxl_psr_hw_info *list,unsigned int nr); + +int libxl_domain_fork_vm(libxl_ctx *ctx, uint32_t pdomid, uint32_t max_vcpus, uint32_t *domid, + bool allow_with_iommu) + 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
@@ -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
@@ -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
@@ -958,6 +958,7 @@ libxl_domain_config = Struct("domain_config", [ ("on_watchdog", libxl_action_on_shutdown), ("on_crash", libxl_action_on_shutdown), ("on_soft_reset", libxl_action_on_shutdown), + ("dm_restore_file", string, {'const': True}), ], dir=DIR_IN) libxl_diskinfo = Struct("diskinfo", [
tools/libxl/libxl_x86.c +42 -0
54 lines not on this page · raw message
tools/xl/Makefile +1 -1
@@ -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
@@ -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
@@ -187,6 +187,21 @@ struct cmd_spec cmd_table[] = { "Restore a domain from a saved state", "- for internal use only", }, +#if defined(__i386__) || defined(__x86_64__) + { "fork-vm", + &main_fork_vm, 0, 1, + "Fork a domain from the running parent domid. Experimental. Most config settings must match parent.", + "[options] <Domid>", + "-h Print this help.\n" + "-C <config> Use config file for VM fork.\n" + "-Q <qemu-save-file> Use qemu save file for VM fork.\n" + "--launch-dm <yes|no|late> Launch device model (QEMU) for VM fork.\n" + "--fork-reset Reset VM fork.\n" + "--max-vcpus Specify max-vcpus matching the parent domain when not launching dm\n" + "-p Do not unpause fork VM after operation.\n" + "-d Enable debug messages.\n" + }, +#endif #endif { "dump-core", &main_dump_core, 0, 1,
tools/xl/xl_forkvm.c new file +149 -0
149 lines not on this page · raw message
tools/xl/xl_vmcontrol.c +14 -0
@@ -676,6 +676,12 @@ int create_domain(struct domain_create *dom_info)int restoring = (restore_file || (migrate_fd >=0 )); +#if defined(__i386__) || defined(__x86_64__) +/* VM forking */ +uint32_t ddomid = dom_info->ddomid;// launch dm for this domain iff set +const char *dm_restore_file = dom_info->dm_restore_file; +#endif +libxl_domain_config_init (&d_config);if (restoring) {
@@ -928,6 +934,14 @@ start:* restore/migrate-receive it again. */ restoring =0 ; +#if defined(__i386__) || defined(__x86_64__) + } else if ( ddomid ) { + d_config.dm_restore_file = dm_restore_file; + ret =libxl_domain_fork_launch_dm (ctx, &d_config, ddomid, + autoconnect_console_how); + domid = ddomid; + ddomid =INVALID_DOMID ; +#endif }else if (domid_soft_reset !=INVALID_DOMID ) {/* Do soft reset. */ ret = libxl_domain_soft_reset(ctx, &d_config, domid_soft_reset,
Signature · 1 line
2.20.1