From: "Jan Beulich" Subject: [PATCH 2/2] x86/HVM: fix preemption handling in do_hvm_op() Date: Tue, 25 Mar 2014 16:03:55 +0000 Message-ID: <5331B6FB0200007800001E1D@nat28.tlf.novell.com> References: <5331B4270200007800001DFA@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part596B36FB.0__=" Return-path: Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1WSTpZ-00037w-HB for xen-devel@lists.xenproject.org; Tue, 25 Mar 2014 16:04:02 +0000 In-Reply-To: <5331B4270200007800001DFA@nat28.tlf.novell.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel Cc: Keir Fraser List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part596B36FB.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Just like previously done for some mem-op hypercalls, undo preemption using the interface structures (altering it in ways the caller may not expect) and replace it by storing the continuation point in the high bits of sub-operation argument. This also changes the "nr" fields of struct xen_hvm_track_dirty_vram (operation already limited to 1Gb worth of pages) and struct xen_hvm_modified_memory to be only 32 bits wide, consistent with those of struct xen_set_mem{type,access}. If that's not acceptable for some reason, we'd need to shrink the HVMOP_op_bits (while still enforcing the [then higher] limit resulting from the need to be able to encode the continuation). Whether (and if so how) to adjust xc_hvm_track_dirty_vram(), xc_hvm_modified_memory(), xc_hvm_set_mem_type(), and xc_hvm_set_mem_access() to reflect the 32-bit restriction on "nr" is unclear: If the APIs need to remain stable, all four functions should probably check that there was no truncation. Preferably their parameters would be changed to uint32_t or unsigned int, though. As a minor cleanup, along with introducing the switch-wide "pfn" the redundant "d" is also being converted to a switch-wide one. Signed-off-by: Jan Beulich --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -4050,20 +4050,25 @@ static int hvm_replace_event_channel(str return 0; } =20 +#define HVMOP_op_bits 32 + long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg) =20 { struct domain *curr_d =3D current->domain; + unsigned long start_iter =3D op >> HVMOP_op_bits; long rc =3D 0; =20 - switch ( op ) + switch ( op &=3D ((1UL << HVMOP_op_bits) - 1) ) { + struct domain *d; + unsigned long pfn; + case HVMOP_set_param: case HVMOP_get_param: { struct xen_hvm_param a; struct hvm_ioreq_page *iorp; - struct domain *d; struct vcpu *v; =20 if ( copy_from_guest(&a, arg, 1) ) @@ -4327,7 +4332,6 @@ long do_hvm_op(unsigned long op, XEN_GUE case HVMOP_track_dirty_vram: { struct xen_hvm_track_dirty_vram a; - struct domain *d; =20 if ( copy_from_guest(&a, arg, 1) ) return -EFAULT; @@ -4368,7 +4372,6 @@ long do_hvm_op(unsigned long op, XEN_GUE case HVMOP_modified_memory: { struct xen_hvm_modified_memory a; - struct domain *d; =20 if ( copy_from_guest(&a, arg, 1) ) return -EFAULT; @@ -4386,7 +4389,8 @@ long do_hvm_op(unsigned long op, XEN_GUE goto param_fail3; =20 rc =3D -EINVAL; - if ( ((a.first_pfn + a.nr - 1) < a.first_pfn) || + if ( a.nr < start_iter || + ((a.first_pfn + a.nr - 1) < a.first_pfn) || ((a.first_pfn + a.nr - 1) > domain_get_maximum_gpfn(d)) ) goto param_fail3; =20 @@ -4394,9 +4398,8 @@ long do_hvm_op(unsigned long op, XEN_GUE if ( !paging_mode_log_dirty(d) ) goto param_fail3; =20 - while ( a.nr > 0 ) + for ( pfn =3D a.first_pfn + start_iter; a.nr > start_iter; ++pfn = ) { - unsigned long pfn =3D a.first_pfn; struct page_info *page; =20 page =3D get_page_from_gfn(d, pfn, NULL, P2M_UNSHARE); @@ -4409,16 +4412,13 @@ long do_hvm_op(unsigned long op, XEN_GUE put_page(page); } =20 - a.first_pfn++; - a.nr--; + ++pfn; + ++start_iter; =20 /* Check for continuation if it's not the last interation */ - if ( a.nr > 0 && hypercall_preempt_check() ) + if ( a.nr > start_iter && hypercall_preempt_check() ) { - if ( __copy_to_guest(arg, &a, 1) ) - rc =3D -EFAULT; - else - rc =3D -EAGAIN; + rc =3D -EAGAIN; break; } } @@ -4431,7 +4431,6 @@ long do_hvm_op(unsigned long op, XEN_GUE case HVMOP_get_mem_type: { struct xen_hvm_get_mem_type a; - struct domain *d; p2m_type_t t; =20 if ( copy_from_guest(&a, arg, 1) ) @@ -4475,7 +4474,6 @@ long do_hvm_op(unsigned long op, XEN_GUE case HVMOP_set_mem_type: { struct xen_hvm_set_mem_type a; - struct domain *d; =20 /* Interface types to internal p2m types */ static const p2m_type_t memtype[] =3D { @@ -4500,20 +4498,19 @@ long do_hvm_op(unsigned long op, XEN_GUE goto param_fail4; =20 rc =3D -EINVAL; - if ( ((a.first_pfn + a.nr - 1) < a.first_pfn) || + if ( a.nr < start_iter || + ((a.first_pfn + a.nr - 1) < a.first_pfn) || ((a.first_pfn + a.nr - 1) > domain_get_maximum_gpfn(d)) ) goto param_fail4; =20 if ( a.hvmmem_type >=3D ARRAY_SIZE(memtype) ) goto param_fail4; =20 - while ( a.nr ) + for ( pfn =3D a.first_pfn + start_iter; a.nr > start_iter; ++pfn = ) { - unsigned long pfn =3D a.first_pfn; - p2m_type_t t; - p2m_type_t nt; - mfn_t mfn; - mfn =3D get_gfn_unshare(d, pfn, &t); + p2m_type_t t, nt; + + get_gfn_unshare(d, pfn, &t); if ( p2m_is_paging(t) ) { put_gfn(d, pfn); @@ -4550,16 +4547,13 @@ long do_hvm_op(unsigned long op, XEN_GUE } put_gfn(d, pfn); =20 - a.first_pfn++; - a.nr--; + ++pfn; + ++start_iter; =20 /* Check for continuation if it's not the last interation */ - if ( a.nr > 0 && hypercall_preempt_check() ) + if ( a.nr > start_iter && hypercall_preempt_check() ) { - if ( __copy_to_guest(arg, &a, 1) ) - rc =3D -EFAULT; - else - rc =3D -EAGAIN; + rc =3D -EAGAIN; goto param_fail4; } } @@ -4574,7 +4568,6 @@ long do_hvm_op(unsigned long op, XEN_GUE case HVMOP_set_mem_access: { struct xen_hvm_set_mem_access a; - struct domain *d; =20 if ( copy_from_guest(&a, arg, 1) ) return -EFAULT; @@ -4593,19 +4586,17 @@ long do_hvm_op(unsigned long op, XEN_GUE =20 rc =3D -EINVAL; if ( (a.first_pfn !=3D ~0ull) && - (((a.first_pfn + a.nr - 1) < a.first_pfn) || + (a.nr < start_iter || + ((a.first_pfn + a.nr - 1) < a.first_pfn) || ((a.first_pfn + a.nr - 1) > domain_get_maximum_gpfn(d))) ) goto param_fail5; =20 - rc =3D p2m_set_mem_access(d, a.first_pfn, a.nr, a.hvmmem_access); + rc =3D p2m_set_mem_access(d, a.first_pfn + start_iter, a.nr - = start_iter, + a.hvmmem_access); if ( rc > 0 ) { - a.first_pfn +=3D a.nr - rc; - a.nr =3D rc; - if ( __copy_to_guest(arg, &a, 1) ) - rc =3D -EFAULT; - else - rc =3D -EAGAIN; + start_iter =3D a.nr - rc; + rc =3D -EAGAIN; } =20 param_fail5: @@ -4616,7 +4607,6 @@ long do_hvm_op(unsigned long op, XEN_GUE case HVMOP_get_mem_access: { struct xen_hvm_get_mem_access a; - struct domain *d; hvmmem_access_t access; =20 if ( copy_from_guest(&a, arg, 1) ) @@ -4653,7 +4643,6 @@ long do_hvm_op(unsigned long op, XEN_GUE case HVMOP_pagetable_dying: { struct xen_hvm_pagetable_dying a; - struct domain *d; =20 if ( copy_from_guest(&a, arg, 1) ) return -EFAULT; @@ -4706,7 +4695,6 @@ long do_hvm_op(unsigned long op, XEN_GUE case HVMOP_inject_trap:=20 { xen_hvm_inject_trap_t tr; - struct domain *d; struct vcpu *v; =20 if ( copy_from_guest(&tr, arg, 1 ) ) @@ -4754,8 +4742,9 @@ long do_hvm_op(unsigned long op, XEN_GUE } =20 if ( rc =3D=3D -EAGAIN ) - rc =3D hypercall_create_continuation( - __HYPERVISOR_hvm_op, "lh", op, arg); + rc =3D hypercall_create_continuation(__HYPERVISOR_hvm_op, "lh", + op | (start_iter << HVMOP_op_bi= ts), + arg); =20 return rc; } --- a/xen/include/public/hvm/hvm_op.h +++ b/xen/include/public/hvm/hvm_op.h @@ -90,10 +90,10 @@ typedef enum { struct xen_hvm_track_dirty_vram { /* Domain to be tracked. */ domid_t domid; + /* Number of pages to track. */ + uint32_t nr; /* First pfn to track. */ uint64_aligned_t first_pfn; - /* Number of pages to track. */ - uint64_aligned_t nr; /* OUT variable. */ /* Dirty bitmap buffer. */ XEN_GUEST_HANDLE_64(uint8) dirty_bitmap; @@ -106,10 +106,10 @@ DEFINE_XEN_GUEST_HANDLE(xen_hvm_track_di struct xen_hvm_modified_memory { /* Domain to be updated. */ domid_t domid; + /* Number of pages. */ + uint32_t nr; /* First pfn. */ uint64_aligned_t first_pfn; - /* Number of pages. */ - uint64_aligned_t nr; }; typedef struct xen_hvm_modified_memory xen_hvm_modified_memory_t; DEFINE_XEN_GUEST_HANDLE(xen_hvm_modified_memory_t); --=__Part596B36FB.0__= Content-Type: text/plain; name="x86-HVM-op-preemption.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-HVM-op-preemption.patch" x86/HVM: fix preemption handling in do_hvm_op()=0A=0AJust like previously = done for some mem-op hypercalls, undo preemption=0Ausing the interface = structures (altering it in ways the caller may not=0Aexpect) and replace = it by storing the continuation point in the high=0Abits of sub-operation = argument.=0A=0AThis also changes the "nr" fields of struct xen_hvm_track_di= rty_vram=0A(operation already limited to 1Gb worth of pages) and struct=0Ax= en_hvm_modified_memory to be only 32 bits wide, consistent with those=0Aof = struct xen_set_mem{type,access}. If that's not acceptable for some=0Areason= , we'd need to shrink the HVMOP_op_bits (while still enforcing=0Athe [then = higher] limit resulting from the need to be able to encode=0Athe continuati= on).=0A=0AWhether (and if so how) to adjust xc_hvm_track_dirty_vram(),=0Axc= _hvm_modified_memory(), xc_hvm_set_mem_type(), and=0Axc_hvm_set_mem_access(= ) to reflect the 32-bit restriction on "nr" is=0Aunclear: If the APIs need = to remain stable, all four functions should=0Aprobably check that there = was no truncation. Preferably their=0Aparameters would be changed to = uint32_t or unsigned int, though.=0A=0AAs a minor cleanup, along with = introducing the switch-wide "pfn" the=0Aredundant "d" is also being = converted to a switch-wide one.=0A=0ASigned-off-by: Jan Beulich =0A=0A--- a/xen/arch/x86/hvm/hvm.c=0A+++ b/xen/arch/x86/hvm/hvm.c= =0A@@ -4050,20 +4050,25 @@ static int hvm_replace_event_channel(str=0A = return 0;=0A }=0A =0A+#define HVMOP_op_bits 32=0A+=0A long do_hvm_op(unsign= ed long op, XEN_GUEST_HANDLE_PARAM(void) arg)=0A =0A {=0A struct = domain *curr_d =3D current->domain;=0A+ unsigned long start_iter =3D op = >> HVMOP_op_bits;=0A long rc =3D 0;=0A =0A- switch ( op )=0A+ = switch ( op &=3D ((1UL << HVMOP_op_bits) - 1) )=0A {=0A+ struct = domain *d;=0A+ unsigned long pfn;=0A+=0A case HVMOP_set_param:= =0A case HVMOP_get_param:=0A {=0A struct xen_hvm_param = a;=0A struct hvm_ioreq_page *iorp;=0A- struct domain *d;=0A = struct vcpu *v;=0A =0A if ( copy_from_guest(&a, arg, 1) = )=0A@@ -4327,7 +4332,6 @@ long do_hvm_op(unsigned long op, XEN_GUE=0A = case HVMOP_track_dirty_vram:=0A {=0A struct xen_hvm_track_dirty= _vram a;=0A- struct domain *d;=0A =0A if ( copy_from_guest(&= a, arg, 1) )=0A return -EFAULT;=0A@@ -4368,7 +4372,6 @@ long = do_hvm_op(unsigned long op, XEN_GUE=0A case HVMOP_modified_memory:=0A = {=0A struct xen_hvm_modified_memory a;=0A- struct domain = *d;=0A =0A if ( copy_from_guest(&a, arg, 1) )=0A = return -EFAULT;=0A@@ -4386,7 +4389,8 @@ long do_hvm_op(unsigned long op, = XEN_GUE=0A goto param_fail3;=0A =0A rc =3D -EINVAL;=0A-= if ( ((a.first_pfn + a.nr - 1) < a.first_pfn) ||=0A+ if ( = a.nr < start_iter ||=0A+ ((a.first_pfn + a.nr - 1) < a.first_pf= n) ||=0A ((a.first_pfn + a.nr - 1) > domain_get_maximum_gpfn(d= )) )=0A goto param_fail3;=0A =0A@@ -4394,9 +4398,8 @@ long = do_hvm_op(unsigned long op, XEN_GUE=0A if ( !paging_mode_log_dirty(= d) )=0A goto param_fail3;=0A =0A- while ( a.nr > 0 = )=0A+ for ( pfn =3D a.first_pfn + start_iter; a.nr > start_iter; = ++pfn )=0A {=0A- unsigned long pfn =3D a.first_pfn;=0A = struct page_info *page;=0A =0A page =3D get_page_fro= m_gfn(d, pfn, NULL, P2M_UNSHARE);=0A@@ -4409,16 +4412,13 @@ long do_hvm_op(= unsigned long op, XEN_GUE=0A put_page(page);=0A = }=0A =0A- a.first_pfn++;=0A- a.nr--;=0A+ = ++pfn;=0A+ ++start_iter;=0A =0A /* Check for = continuation if it's not the last interation */=0A- if ( a.nr > = 0 && hypercall_preempt_check() )=0A+ if ( a.nr > start_iter && = hypercall_preempt_check() )=0A {=0A- if ( = __copy_to_guest(arg, &a, 1) )=0A- rc =3D -EFAULT;=0A- = else=0A- rc =3D -EAGAIN;=0A+ = rc =3D -EAGAIN;=0A break;=0A }=0A = }=0A@@ -4431,7 +4431,6 @@ long do_hvm_op(unsigned long op, XEN_GUE=0A = case HVMOP_get_mem_type:=0A {=0A struct xen_hvm_get_mem_type = a;=0A- struct domain *d;=0A p2m_type_t t;=0A =0A if = ( copy_from_guest(&a, arg, 1) )=0A@@ -4475,7 +4474,6 @@ long do_hvm_op(unsi= gned long op, XEN_GUE=0A case HVMOP_set_mem_type:=0A {=0A = struct xen_hvm_set_mem_type a;=0A- struct domain *d;=0A =0A = /* Interface types to internal p2m types */=0A static = const p2m_type_t memtype[] =3D {=0A@@ -4500,20 +4498,19 @@ long do_hvm_op(u= nsigned long op, XEN_GUE=0A goto param_fail4;=0A =0A = rc =3D -EINVAL;=0A- if ( ((a.first_pfn + a.nr - 1) < a.first_pfn) = ||=0A+ if ( a.nr < start_iter ||=0A+ ((a.first_pfn + = a.nr - 1) < a.first_pfn) ||=0A ((a.first_pfn + a.nr - 1) > = domain_get_maximum_gpfn(d)) )=0A goto param_fail4;=0A = =0A if ( a.hvmmem_type >=3D ARRAY_SIZE(memtype) )=0A = goto param_fail4;=0A =0A- while ( a.nr )=0A+ for ( pfn =3D = a.first_pfn + start_iter; a.nr > start_iter; ++pfn )=0A {=0A- = unsigned long pfn =3D a.first_pfn;=0A- p2m_type_t t;=0A- = p2m_type_t nt;=0A- mfn_t mfn;=0A- mfn =3D = get_gfn_unshare(d, pfn, &t);=0A+ p2m_type_t t, nt;=0A+=0A+ = get_gfn_unshare(d, pfn, &t);=0A if ( p2m_is_paging(t) = )=0A {=0A put_gfn(d, pfn);=0A@@ -4550,16 = +4547,13 @@ long do_hvm_op(unsigned long op, XEN_GUE=0A }=0A = put_gfn(d, pfn);=0A =0A- a.first_pfn++;=0A- = a.nr--;=0A+ ++pfn;=0A+ ++start_iter;=0A =0A = /* Check for continuation if it's not the last interation */=0A- = if ( a.nr > 0 && hypercall_preempt_check() )=0A+ if ( a.nr = > start_iter && hypercall_preempt_check() )=0A {=0A- = if ( __copy_to_guest(arg, &a, 1) )=0A- rc =3D = -EFAULT;=0A- else=0A- rc =3D -EAGAIN;=0A+= rc =3D -EAGAIN;=0A goto param_fail4;=0A = }=0A }=0A@@ -4574,7 +4568,6 @@ long do_hvm_op(unsigned = long op, XEN_GUE=0A case HVMOP_set_mem_access:=0A {=0A = struct xen_hvm_set_mem_access a;=0A- struct domain *d;=0A =0A = if ( copy_from_guest(&a, arg, 1) )=0A return -EFAULT;=0A@@ = -4593,19 +4586,17 @@ long do_hvm_op(unsigned long op, XEN_GUE=0A =0A = rc =3D -EINVAL;=0A if ( (a.first_pfn !=3D ~0ull) &&=0A- = (((a.first_pfn + a.nr - 1) < a.first_pfn) ||=0A+ (a.nr < = start_iter ||=0A+ ((a.first_pfn + a.nr - 1) < a.first_pfn) = ||=0A ((a.first_pfn + a.nr - 1) > domain_get_maximum_gpfn(d))= ) )=0A goto param_fail5;=0A =0A- rc =3D = p2m_set_mem_access(d, a.first_pfn, a.nr, a.hvmmem_access);=0A+ rc = =3D p2m_set_mem_access(d, a.first_pfn + start_iter, a.nr - start_iter,=0A+ = a.hvmmem_access);=0A if ( rc > 0 = )=0A {=0A- a.first_pfn +=3D a.nr - rc;=0A- = a.nr =3D rc;=0A- if ( __copy_to_guest(arg, &a, 1) )=0A- = rc =3D -EFAULT;=0A- else=0A- rc =3D = -EAGAIN;=0A+ start_iter =3D a.nr - rc;=0A+ rc =3D = -EAGAIN;=0A }=0A =0A param_fail5:=0A@@ -4616,7 +4607,6 @@ long = do_hvm_op(unsigned long op, XEN_GUE=0A case HVMOP_get_mem_access:=0A = {=0A struct xen_hvm_get_mem_access a;=0A- struct domain = *d;=0A hvmmem_access_t access;=0A =0A if ( copy_from_guest(= &a, arg, 1) )=0A@@ -4653,7 +4643,6 @@ long do_hvm_op(unsigned long op, = XEN_GUE=0A case HVMOP_pagetable_dying:=0A {=0A struct = xen_hvm_pagetable_dying a;=0A- struct domain *d;=0A =0A if = ( copy_from_guest(&a, arg, 1) )=0A return -EFAULT;=0A@@ = -4706,7 +4695,6 @@ long do_hvm_op(unsigned long op, XEN_GUE=0A case = HVMOP_inject_trap: =0A {=0A xen_hvm_inject_trap_t tr;=0A- = struct domain *d;=0A struct vcpu *v;=0A =0A if ( = copy_from_guest(&tr, arg, 1 ) )=0A@@ -4754,8 +4742,9 @@ long do_hvm_op(unsi= gned long op, XEN_GUE=0A }=0A =0A if ( rc =3D=3D -EAGAIN )=0A- = rc =3D hypercall_create_continuation(=0A- __HYPERVISOR_hvm_op= , "lh", op, arg);=0A+ rc =3D hypercall_create_continuation(__HYPERVI= SOR_hvm_op, "lh",=0A+ op | = (start_iter << HVMOP_op_bits),=0A+ = arg);=0A =0A return rc;=0A }=0A--- a/xen/include/public/hvm/hvm_op.h= =0A+++ b/xen/include/public/hvm/hvm_op.h=0A@@ -90,10 +90,10 @@ typedef = enum {=0A struct xen_hvm_track_dirty_vram {=0A /* Domain to be = tracked. */=0A domid_t domid;=0A+ /* Number of pages to track. = */=0A+ uint32_t nr;=0A /* First pfn to track. */=0A uint64_align= ed_t first_pfn;=0A- /* Number of pages to track. */=0A- uint64_aligne= d_t nr;=0A /* OUT variable. */=0A /* Dirty bitmap buffer. */=0A = XEN_GUEST_HANDLE_64(uint8) dirty_bitmap;=0A@@ -106,10 +106,10 @@ = DEFINE_XEN_GUEST_HANDLE(xen_hvm_track_di=0A struct xen_hvm_modified_memory = {=0A /* Domain to be updated. */=0A domid_t domid;=0A+ /* = Number of pages. */=0A+ uint32_t nr;=0A /* First pfn. */=0A = uint64_aligned_t first_pfn;=0A- /* Number of pages. */=0A- uint64_ali= gned_t nr;=0A };=0A typedef struct xen_hvm_modified_memory xen_hvm_modified= _memory_t;=0A DEFINE_XEN_GUEST_HANDLE(xen_hvm_modified_memory_t);=0A --=__Part596B36FB.0__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__Part596B36FB.0__=--