x86/mem-sharing: Bulk mem-sharing entire domains
PATCH v3 1/2 x86/mem-sharing: Bulk mem-sharing entire domains
Currently mem-sharing can be performed on a page-by-page base from the control domain. However, when completely deduplicating (cloning) a VM, this requires at least 3 hypercalls per page. As the user has to loop through all pages up to max_gpfn, this process is very slow and wasteful.
This patch introduces a new mem_sharing memop for bulk deduplication where the user doesn't have to separately nominate each page in both the source and destination domain, and the looping over all pages happen in the hypervisor. This significantly reduces the overhead of completely deduplicating entire domains.
---
v3: Bail if domains are not paused
Rename bulk_share struct to just bulk
Return -ENOMEM error if nomination fails
Return total number of shared pages (not keeping separate count)
v2: Stash hypercall continuation start point in xen_mem_sharing_op_t
Return number of successfully shared pages in xen_mem_sharing_op_t
--- tools/libxc/include/xenctrl.h | 15 +++++++ tools/libxc/xc_memshr.c | 19 +++++++++ xen/arch/x86/mm/mem_sharing.c | 99 +++++++++++++++++++++++++++++++++++++++++++ xen/include/public/memory.h | 12 +++++- 4 files changed, 144 insertions(+), 1 deletion(-)
tools/libxc/include/xenctrl.h +15 -0
@@ -2594,6 +2594,21 @@ int xc_memshr_add_to_physmap(xc_interface *xch,domid_t client_domain, unsigned long client_gfn); +/* Allows to deduplicate the entire memory of a client domain in bulk. Using +* this function is equivalent of calling xc_memshr_nominate_gfn for each gfn +* in the two domains followed by xc_memshr_share_gfns. If successfull, +* returns the number of shared pages in 'shared'. Both domains must be paused. +* +* May fail with -EINVAL if the source and client domain have different +* memory size or if memory sharing is not enabled on either of the domains. +* May also fail with -ENOMEM if there isn't enough memory available to store +* the sharing metadata before deduplication can happen. +*/ +int xc_memshr_bulk_share (xc_interface *xch, +domid_t source_domain, +domid_t client_domain, +uint64_t *shared); +/* 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 +19 -0
@@ -181,6 +181,25 @@ int xc_memshr_add_to_physmap(xc_interface *xch,return xc_memshr_memop (xch, source_domain, &mso); } +int xc_memshr_bulk_share (xc_interface *xch, +domid_t source_domain, +domid_t client_domain, +uint64_t *shared) +{ +int rc; +xen_mem_sharing_op_t mso; + +memset (&mso,0 ,sizeof (mso)); + + mso.op = XENMEM_sharing_op_bulk_share; + mso.u.bulk.client_domain = client_domain; + + rc =xc_memshr_memop (xch, source_domain, &mso); +if ( !rc && shared ) *shared = mso.u.bulk.applied; + +return rc; +} +int xc_memshr_domain_resume (xc_interface *xch,domid_t domid) {
xen/arch/x86/mm/mem_sharing.c +99 -0
111 lines not on this page · raw message
xen/include/public/memory.h +11 -1
@@ -447,6 +447,7 @@ DEFINE_XEN_GUEST_HANDLE(xen_mem_access_op_t);#define XENMEM_sharing_op_debug_gref 5#define XENMEM_sharing_op_add_physmap 6#define XENMEM_sharing_op_audit 7 +#define XENMEM_sharing_op_bulk_share 8#define XENMEM_SHARING_OP_S_HANDLE_INVALID (-10)#define XENMEM_SHARING_OP_C_HANDLE_INVALID (-9)
@@ -482,7 +483,16 @@ struct xen_mem_sharing_op {uint64_aligned_t client_gfn;/* IN: the client gfn */ uint64_aligned_t client_handle;/* IN: handle to the client page */ domid_t client_domain;/* IN: the client domain id */ - } share; + } share; +struct mem_sharing_op_bulk {/* OP_BULK_SHARE */ +domid_t client_domain;/* IN: the client domain id */ +uint64_aligned_t start;/* IN: start gfn. Set to 0 for +full deduplication. Field is +used internally and may change +when the hypercall returns. */ +uint64_aligned_t applied;/* OUT: the number of gfns +where the operation applied */ + } bulk;struct mem_sharing_op_debug {/* OP_DEBUG_xxx */ union {uint64_aligned_t gfn;/* IN: gfn to debug */
Signature · 1 line
2.1.4
PATCH v3 2/2 tests/mem-sharing: Add bulk option to memshrtool
Add the bulk option to the test tool to perform complete deduplication between domains.
--- tools/tests/mem-sharing/memshrtool.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
tools/tests/mem-sharing/memshrtool.c +22 -0
@@ -23,6 +23,8 @@ static int usage(const char* prog)printf (" nominate <domid> <gfn> - Nominate a page for sharing.\n" );printf (" share <domid> <gfn> <handle> <source> <source-gfn> <source-handle>\n" );printf (" - Share two pages.\n" ); +printf (" bulk <source-domid> <destination-domid>\n" ); +printf (" - Share all pages between domains.\n" );printf (" unshare <domid> <gfn> - Unshare a page by grabbing a writable map.\n" );printf (" add-to-physmap <domid> <gfn> <source> <source-gfn> <source-handle>\n" );printf (" - Populate a page in a domain with a shared page.\n" );
@@ -179,6 +181,26 @@ int main(int argc, const char** argv) }printf ("Audit returned %d errors.\n" , rc); } +else if ( !strcasecmp (cmd,"bulk" ) ) + { +domid_t sdomid, cdomid; +int rc; +unsigned long shared; + +if ( argc !=4 ) +return usage (argv[0 ]); + + sdomid =strtol (argv[2 ],NULL ,0 ); + cdomid =strtol (argv[3 ],NULL ,0 ); + rc =xc_memshr_bulk_share (xch, sdomid, cdomid, &shared); +if ( rc <0 ) + { +printf ("error executing xc_memshr_bulk_dedup: %s\n" ,strerror (errno)); +return rc; + } + +printf ("Successfully shared %lu pages between the domains\n" , shared); + }return 0 ; }
Signature · 1 line
2.1.4
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 15.10.15 at 20:09, <[email protected]> wrote:
xen/arch/x86/mm/mem_sharing.c +2
-0@@ -1293,6 +1293,42 @@ int relinquish_shared_pages(struct domain *d)return rc; } +static int bulk_share (struct domain *d,struct domain *cd,unsigned long max, +struct mem_sharing_op_bulk *bulk)
xen/arch/x86/mm/mem_sharing.c int relinquish_shared_pages(struct domain *d) · line 1307
Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10
-0+{ +int rc =0 ; +shr_handle_t sh, ch; + +while ( bulk->start <= max ) + { + rc =mem_sharing_nominate_page (d, bulk->start,0 , &sh); +if ( rc == -ENOMEM ) +break ; +else if ( rc )
Pointless else.
xen/arch/x86/mm/mem_sharing.c int relinquish_shared_pages(struct domain *d) · line 1318
Quoted 11 lines
xen/arch/x86/mm/mem_sharing.c +11
-0+ goto next; + + rc =mem_sharing_nominate_page (cd, bulk->start,0 , &ch); +if ( rc == -ENOMEM ) +break ; +else if ( rc ) + goto next; + +mem_sharing_share_pages (d, bulk->start, sh, cd, bulk->start, ch); + +next:
Labels indented by at least one space please.
Quoted 24 lines
xen/arch/x86/mm/mem_sharing.c +20
-0@@ -1467,6 +1503,69 @@ int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg) }break ; +case XENMEM_sharing_op_bulk_share: + { +unsigned long max_sgfn, max_cgfn; +struct domain *cd; + + rc = -EINVAL ; +if ( !mem_sharing_enabled (d) ) + goto out; + + rc =rcu_lock_live_remote_domain_by_id (mso.u.bulk.client_domain, + &cd); +if ( rc ) + goto out; + + rc =xsm_mem_sharing_op (XSM_DM_PRIV , d, cd, mso.op); +if ( rc ) + { +rcu_unlock_domain (cd); + goto out; + }
xen/arch/x86/mm/mem_sharing.c int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg) · line 1535
Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10
-0+ +if ( !mem_sharing_enabled (cd) ) + { +rcu_unlock_domain (cd); + rc = -EINVAL ; + goto out; + } + +if ( !atomic_read (&d->pause_count) || + !atomic_read (&cd->pause_count) )
As said in the reply to your respective inquiry - you want to look at controller_pause_count here instead.
xen/arch/x86/mm/mem_sharing.c int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg) · line 1537
Quoted 2 lines
xen/arch/x86/mm/mem_sharing.c +2
-0+ { +rcu_unlock_domain (cd);
Considering how many times you do this, perhaps worth putting a label at the (current) success path's unlock?
Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8
-0+ rc = -EINVAL ; + goto out; + } + + max_sgfn =domain_get_maximum_gpfn (d); + max_cgfn =domain_get_maximum_gpfn (cd); + +if ( max_sgfn != max_cgfn || max_sgfn < mso.u.bulk.start )
xen/arch/x86/mm/mem_sharing.c int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg) · line 1555
Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10
-0+ { +rcu_unlock_domain (cd); + rc = -EINVAL ; + goto out; + } + + rc =bulk_share (d, cd, max_sgfn, &mso.u.bulk); +if ( rc >0 ) + { +if (__copy_to_guest (arg, &mso,1 ) )
Wouldn't copying just the one field you care about suffice here?
xen/arch/x86/mm/mem_sharing.c int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg) · line 1561
Quoted 6 lines
xen/arch/x86/mm/mem_sharing.c +6
-0+ rc = -EFAULT ; + else + rc =hypercall_create_continuation (__HYPERVISOR_memory_op, +"lh" , XENMEM_sharing_op, + arg); + }else {
Coding style.
xen/arch/x86/mm/mem_sharing.c int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg) · line 1562
Quoted 1 line
xen/arch/x86/mm/mem_sharing.c +1
-0+ mso.u.bulk.applied =atomic_read (&d->shr_pages);
Is there no possibility for something else to also modify d->shr_pages in parallel, misguiding the caller when looking at the output field. Also you're not copying this back to guest memory...
xen/arch/x86/mm/mem_sharing.c struct xen_mem_sharing_op { · line 488
Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +3
-1@@ -482,7 +483,16 @@ struct xen_mem_sharing_op {uint64_aligned_t client_gfn;/* IN: the client gfn */ uint64_aligned_t client_handle;/* IN: handle to the client page */ domid_t client_domain;/* IN: the client domain id */ - } share; + } share; +struct mem_sharing_op_bulk {/* OP_BULK_SHARE */ +domid_t client_domain;/* IN: the client domain id */
Explicit padding here please (albeit I see already from the context that this isn't being done in e.g. the share sub-structure above either).
Jan
Quoted 7 lines
xen/arch/x86/mm/mem_sharing.c +7
-0+uint64_aligned_t start;/* IN: start gfn. Set to 0 for +full deduplication. Field is +used internally and may change +when the hypercall returns. */ +uint64_aligned_t applied;/* OUT: the number of gfns +where the operation applied */ + } bulk;
Quoted 9 lines · Jan Beulich
xen/arch/x86/mm/mem_sharing.c int relinquish_shared_pages(struct domain *d) · line 1296
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 15.10.15 at 20:09, <[email protected]> wrote:
xen/arch/x86/mm/mem_sharing.c +1
-0@@ -1293,6 +1293,42 @@ int relinquish_shared_pages(struct domain *d)return rc; } +static int bulk_share(struct domain *d, struct domain *cd, unsignedlong max,
Quoted 14 lines
xen/arch/x86/mm/mem_sharing.c int relinquish_shared_pages(struct domain *d) · line 1307
Quoted 11 lines
xen/arch/x86/mm/mem_sharing.c +11
-0+ struct mem_sharing_op_bulk *bulk) +{ +int rc =0 ; +shr_handle_t sh, ch; + +while ( bulk->start <= max ) + { + rc =mem_sharing_nominate_page (d, bulk->start,0 , &sh); +if ( rc == -ENOMEM ) +break ; +else if ( rc )Pointless else.
Pointless but harmless.
Quoted 11 lines
xen/arch/x86/mm/mem_sharing.c int relinquish_shared_pages(struct domain *d)
Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +9
-0+ goto next; + + rc =mem_sharing_nominate_page (cd, bulk->start,0 , &ch); +if ( rc == -ENOMEM ) +break ; +else if ( rc ) + goto next; + + mem_sharing_share_pages(d, bulk->start, sh, cd, bulk->start,ch);
Quoted 5 lines
xen/arch/x86/mm/mem_sharing.c int relinquish_shared_pages(struct domain *d) · line 1318
Quoted 2 lines
xen/arch/x86/mm/mem_sharing.c +2
-0+ +next:Labels indented by at least one space please.
Yes, keep forgetting..
Quoted 32 lines
xen/arch/x86/mm/mem_sharing.c int
Quoted 2 lines
xen/arch/x86/mm/mem_sharing.c +0
-0@@ -1467,6 +1503,69 @@ int
mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg)xen/arch/x86/mm/mem_sharing.c int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg)
Quoted 13 lines
xen/arch/x86/mm/mem_sharing.c +10
-0}break ; +case XENMEM_sharing_op_bulk_share: + { +unsigned long max_sgfn, max_cgfn; +struct domain *cd; + + rc = -EINVAL ; +if ( !mem_sharing_enabled (d) ) + goto out; + + rc =rcu_lock_live_remote_domain_by_id(mso.u.bulk.client_domain,
Quoted 15 lines
xen/arch/x86/mm/mem_sharing.c +15
-0+ &cd); +if ( rc ) + goto out; + + rc =xsm_mem_sharing_op (XSM_DM_PRIV , d, cd, mso.op); +if ( rc ) + { +rcu_unlock_domain (cd); + goto out; + } + +if ( !mem_sharing_enabled (cd) ) + { +rcu_unlock_domain (cd); + rc = -EINVAL ;
Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg) · line 1535
Quoted 5 lines
xen/arch/x86/mm/mem_sharing.c +5
-0+ goto out; + } + +if ( !atomic_read (&d->pause_count) || + !atomic_read (&cd->pause_count) )As said in the reply to your respective inquiry - you want to look at
controller_pause_counthere instead.
Thanks!
Quoted 7 lines
xen/arch/x86/mm/mem_sharing.c int · line 1537
Quoted 3 lines
xen/arch/x86/mm/mem_sharing.c +2
-0+ { +rcu_unlock_domain (cd);Considering how many times you do this, perhaps worth putting a label at the (current) success path's unlock?
I'll take a look.
Quoted 13 lines
Quoted 13 lines
xen/arch/x86/mm/mem_sharing.c +12
-0+ rc = -EINVAL ; + goto out; + } + + max_sgfn =domain_get_maximum_gpfn (d); + max_cgfn =domain_get_maximum_gpfn (cd); + +if ( max_sgfn != max_cgfn || max_sgfn < mso.u.bulk.start ) + { +rcu_unlock_domain (cd); + rc = -EINVAL ; + goto out;
Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg) · line 1555
Quoted 6 lines
xen/arch/x86/mm/mem_sharing.c +6
-0+ } + + rc =bulk_share (d, cd, max_sgfn, &mso.u.bulk); +if ( rc >0 ) + { +if (__copy_to_guest (arg, &mso,1 ) )Wouldn't copying just the one field you care about suffice here?
Sure.
Quoted 4 lines
xen/arch/x86/mm/mem_sharing.c int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg)
Quoted 4 lines
xen/arch/x86/mm/mem_sharing.c +3
-0+ rc = -EFAULT ; + else + rc =
Quoted 8 lines
hypercall_create_continuation(__HYPERVISOR_memory_op,
xen/arch/x86/mm/mem_sharing.c int · line 1559
Quoted 1 line
xen/arch/x86/mm/mem_sharing.c +1
-0+ "lh",
XENMEM_sharing_op,xen/arch/x86/mm/mem_sharing.c int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg) · line 1561
Quoted 2 lines
xen/arch/x86/mm/mem_sharing.c +2
-0+ arg); + }else {Coding style.
Coding style matches the rest of the file.
Quoted 7 lines
xen/arch/x86/mm/mem_sharing.c int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg) · line 1562
Quoted 2 lines
xen/arch/x86/mm/mem_sharing.c +1
-0+ mso.u.bulk.applied =atomic_read (&d->shr_pages);Is there no possibility for something else to also modify
d->shr_pagesin parallel, misguiding the caller when looking at the output field. Also you're not copying this back to guest memory...
It shouldn't be an issue as I don't really care about how many pages were shared by the operation itself, I care about the total number of pages shared on the domain. If there were already some pages shared before this operation, those should be counted here again. I could of course issue a separate getdomaininfo to get this same information, it's just more expedient to report it right away instead of having to do a separate call. By default shr_pages will be equivalent to the number of pages successfully shared during this operation. If someone decides to also do unsharing in parallel to this op (..how would that make sense?).. well, that's not supported right now so all bets are off from my perspective.
Also, we are copying it back to guest memory when the operation finishes for all mso. It's not bulk specific, applies to all !rc cases further down.
Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c struct xen_mem_sharing_op { · line 484
Quoted 4 lines
xen/arch/x86/mm/mem_sharing.c +0
-0@@ -482,7 +483,16 @@ struct xen_mem_sharing_op {uint64_aligned_t client_gfn;/* IN: the client gfn */ uint64_aligned_t client_handle;/* IN: handle to the client page */
xen/arch/x86/mm/mem_sharing.c struct xen_mem_sharing_op { · line 488
Quoted 5 lines
xen/arch/x86/mm/mem_sharing.c +3
-1domid_t client_domain;/* IN: the client domain id */ - } share; + } share; +struct mem_sharing_op_bulk {/* OP_BULK_SHARE */ +domid_t client_domain;/* IN: the client domain
Quoted 5 lines
id */
Explicit padding here please (albeit I see already from the context that this isn't being done in e.g. the share sub-structure above either).
Not sure what you mean by explicit padding here. The way it's right now matches pretty much what was already in place.
Tamas
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 16.10.15 at 19:02, <[email protected]> wrote:
Quoted 8 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 15.10.15 at 20:09, <[email protected]> wrote:
xen/arch/x86/mm/mem_sharing.c +6
-0+ rc = -EFAULT ; + else + rc =hypercall_create_continuation (__HYPERVISOR_memory_op, +"lh" , XENMEM_sharing_op, + arg); + }else {
Quoted 3 lines
Quoted 1 line
Coding style.
Coding style matches the rest of the file.
Sorry, but this is not Xen coding style, and hence if there are other bad examples in the file, these shouldn't be used as an excuse.
Quoted 6 lines
Quoted 5 lines
Quoted 1 line
+ mso.u.bulk.applied =
atomic_read(&d->shr_pages);Is there no possibility for something else to also modify
d->shr_pagesin parallel, misguiding the caller when looking at the output field. Also you're not copying this back to guest memory...
Quoted 10 lines
It shouldn't be an issue as I don't really care about how many pages were shared by the operation itself, I care about the total number of pages shared on the domain. If there were already some pages shared before this operation, those should be counted here again. I could of course issue a separate getdomaininfo to get this same information, it's just more expedient to report it right away instead of having to do a separate call. By default
shr_pageswill be equivalent to the number of pages successfully shared during this operation. If someone decides to also do unsharing in parallel to this op (..how would that make sense?).. well, that's not supported right now so all bets are off from my perspective.
But the field being named "applied" suggests a different meaning.
Quoted 2 lines
Also, we are copying it back to guest memory when the operation finishes for all mso. It's not bulk specific, applies to all !rc cases further down.
Oh, okay.
Quoted 9 lines
Quoted 9 lines
Review struct xen_mem_sharing_op { · line 484
Quoted 3 lines
(quoted diff) +0
-0@@ -482,7 +483,16 @@ struct xen_mem_sharing_op { uint64_aligned_t client_gfn; /* IN: the client gfn */ uint64_aligned_t client_handle; /* IN: handle to the clientpage */
Quoted 5 lines
xen/include/public/memory.h +3
-1domid_t client_domain;/* IN: the client domain id */ - } share; + } share; +struct mem_sharing_op_bulk {/* OP_BULK_SHARE */ +domid_t client_domain;/* IN: the client domain
Quoted 8 lines
Quoted 5 lines
id */
Explicit padding here please (albeit I see already from the context that this isn't being done in e.g. the share sub-structure above either).
Not sure what you mean by explicit padding here. The way it's right now matches pretty much what was already in place.
domid_t is a 16-bit value, i.e. there's a gap after this field which would better be made explicit (as we do in many places elsewhere, albeit - as said - sadly not in the example visible in patch context here).
Jan