x86/mm: Add array_index_nospec to guest provided index values
PATCH v4 1/4 x86/mm: Add array_index_nospec to guest provided index values
This patch aims to sanitize indexes, potentially guest provided values, for altp2m_eptp[] and altp2m_p2m[] arrays.
---
CC: Razvan Cojocaru <[email protected]>
CC: Tamas K Lengyel <[email protected]>
CC: Petre Pircalabu <[email protected]>
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Wei Liu <[email protected]>
CC: "Roger Pau Monné" <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
--- xen/arch/x86/mm/mem_access.c | 15 +++++++++------ xen/arch/x86/mm/p2m-ept.c | 5 +++-- xen/arch/x86/mm/p2m.c | 27 +++++++++++++++++---------- 3 files changed, 29 insertions(+), 18 deletions(-)
xen/arch/x86/mm/mem_access.c +9 -6
@@ -367,10 +367,11 @@ long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr,if ( altp2m_idx ) {if ( altp2m_idx >=MAX_ALTP2M || - d->arch.altp2m_eptp[altp2m_idx] ==mfn_x (INVALID_MFN ) ) + d->arch.altp2m_eptp[array_index_nospec (altp2m_idx,MAX_EPTP )] == +mfn_x (INVALID_MFN ) )return -EINVAL ; - ap2m = d->arch.altp2m_p2m[altp2m_idx]; + ap2m = d->arch.altp2m_p2m[array_index_nospec (altp2m_idx,MAX_ALTP2M )]; }#else ASSERT (!altp2m_idx);
@@ -426,10 +427,11 @@ long p2m_set_mem_access_multi(struct domain *d,if ( altp2m_idx ) {if ( altp2m_idx >=MAX_ALTP2M || - d->arch.altp2m_eptp[altp2m_idx] ==mfn_x (INVALID_MFN ) ) + d->arch.altp2m_eptp[array_index_nospec (altp2m_idx,MAX_EPTP )] == +mfn_x (INVALID_MFN ) )return -EINVAL ; - ap2m = d->arch.altp2m_p2m[altp2m_idx]; + ap2m = d->arch.altp2m_p2m[array_index_nospec (altp2m_idx,MAX_ALTP2M )]; }#else ASSERT (!altp2m_idx);
@@ -492,10 +494,11 @@ int p2m_get_mem_access(struct domain *d, gfn_t gfn, xenmem_access_t *access, else if ( altp2m_idx ) /* altp2m view 0 is treated as the hostp2m */ {if ( altp2m_idx >=MAX_ALTP2M || - d->arch.altp2m_eptp[altp2m_idx] ==mfn_x (INVALID_MFN ) ) + d->arch.altp2m_eptp[array_index_nospec (altp2m_idx,MAX_EPTP )] == +mfn_x (INVALID_MFN ) )return -EINVAL ; - p2m = d->arch.altp2m_p2m[altp2m_idx]; + p2m = d->arch.altp2m_p2m[array_index_nospec (altp2m_idx,MAX_ALTP2M )]; }#else ASSERT (!altp2m_idx);
xen/arch/x86/mm/p2m-ept.c +3 -2
@@ -1353,7 +1353,8 @@ void setup_ept_dump(void)void p2m_init_altp2m_ept (struct domain *d,unsigned int i) { -struct p2m_domain *p2m = d->arch.altp2m_p2m[i]; +struct p2m_domain *p2m = + d->arch.altp2m_p2m[array_index_nospec (i,MAX_ALTP2M )];struct p2m_domain *hostp2m =p2m_get_hostp2m (d);struct ept_data *ept;
@@ -1366,7 +1367,7 @@ void p2m_init_altp2m_ept(struct domain *d, unsigned int i) p2m->max_mapped_pfn = p2m->max_remapped_gfn =0 ; ept = &p2m->ept; ept->mfn =pagetable_get_pfn (p2m_get_pagetable (p2m)); - d->arch.altp2m_eptp[i] = ept->eptp; + d->arch.altp2m_eptp[array_index_nospec (i,MAX_EPTP )] = ept->eptp; }unsigned int p2m_find_altp2m_by_eptp (struct domain *d,uint64_t eptp)
xen/arch/x86/mm/p2m.c +17 -10
@@ -2499,7 +2499,7 @@ static void p2m_reset_altp2m(struct domain *d, unsigned int idx,struct p2m_domain *p2m;ASSERT (idx <MAX_ALTP2M ); - p2m = d->arch.altp2m_p2m[idx]; + p2m = d->arch.altp2m_p2m[array_index_nospec (idx,MAX_ALTP2M )];p2m_lock (p2m);
@@ -2540,7 +2540,7 @@ static int p2m_activate_altp2m(struct domain *d, unsigned int idx)ASSERT (idx <MAX_ALTP2M ); - p2m = d->arch.altp2m_p2m[idx]; + p2m = d->arch.altp2m_p2m[array_index_nospec (idx,MAX_ALTP2M )]; hostp2m =p2m_get_hostp2m (d);p2m_lock (p2m);
@@ -2622,9 +2622,10 @@ int p2m_destroy_altp2m_by_id(struct domain *d, unsigned int idx) rc = -EBUSY ;altp2m_list_lock (d); -if ( d->arch.altp2m_eptp[idx] !=mfn_x (INVALID_MFN ) ) +if ( d->arch.altp2m_eptp[array_index_nospec (idx,MAX_EPTP )] != +mfn_x (INVALID_MFN ) ) { - p2m = d->arch.altp2m_p2m[idx]; + p2m = d->arch.altp2m_p2m[array_index_nospec (idx,MAX_ALTP2M )];if ( !_atomic_read (p2m->active_vcpus) ) {
@@ -2686,11 +2687,13 @@ int p2m_change_altp2m_gfn(struct domain *d, unsigned int idx,mfn_t mfn;int rc = -EINVAL ; -if ( idx >=MAX_ALTP2M || d->arch.altp2m_eptp[idx] ==mfn_x (INVALID_MFN ) ) +if ( idx >=MAX_ALTP2M || + d->arch.altp2m_eptp[array_index_nospec (idx,MAX_EPTP )] == +mfn_x (INVALID_MFN ) )return rc; hp2m =p2m_get_hostp2m (d); - ap2m = d->arch.altp2m_p2m[idx]; + ap2m = d->arch.altp2m_p2m[array_index_nospec (idx,MAX_ALTP2M )];p2m_lock (hp2m);p2m_lock (ap2m);
@@ -3030,10 +3033,12 @@ int p2m_set_suppress_ve(struct domain *d, gfn_t gfn, bool suppress_ve,if ( altp2m_idx >0 ) {if ( altp2m_idx >=MAX_ALTP2M || - d->arch.altp2m_eptp[altp2m_idx] ==mfn_x (INVALID_MFN ) ) + d->arch.altp2m_eptp[array_index_nospec (altp2m_idx,MAX_EPTP )] == +mfn_x (INVALID_MFN ) )return -EINVAL ; - p2m = ap2m = d->arch.altp2m_p2m[altp2m_idx]; + p2m = ap2m = d->arch.altp2m_p2m[array_index_nospec (altp2m_idx, +MAX_ALTP2M )]; }else p2m = host_p2m;
@@ -3073,10 +3078,12 @@ int p2m_get_suppress_ve(struct domain *d, gfn_t gfn, bool *suppress_ve,if ( altp2m_idx >0 ) {if ( altp2m_idx >=MAX_ALTP2M || - d->arch.altp2m_eptp[altp2m_idx] ==mfn_x (INVALID_MFN ) ) + d->arch.altp2m_eptp[array_index_nospec (altp2m_idx,MAX_EPTP )] == +mfn_x (INVALID_MFN ) )return -EINVAL ; - p2m = ap2m = d->arch.altp2m_p2m[altp2m_idx]; + p2m = ap2m = d->arch.altp2m_p2m[array_index_nospec (altp2m_idx, +MAX_ALTP2M )]; }else p2m = host_p2m;
Signature · 6 lines
2.17.1 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v4 2/4 x86/altp2m: Add hypercall to set a range of sve bits
By default the sve bits are not set.
This patch adds a new hypercall, xc_altp2m_set_supress_ve_multi(), to set a range of sve bits.
The core function, p2m_set_suppress_ve_multi(), does not brake in case of a error and it is doing a best effort for setting the bits in the given range. A check for continuation is made in order to have preemption on big ranges.
The gfn of the first error is stored in xen_hvm_altp2m_suppress_ve_multi.first_error and the error code is stored in xen_hvm_altp2m_suppress_ve_multi.first_error_code. If no error occurred the values will be 0.
---
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Konrad Rzeszutek Wilk <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: "Roger Pau Monné" <[email protected]>
CC: George Dunlap <[email protected]>
CC: Razvan Cojocaru <[email protected]>
CC: Tamas K Lengyel <[email protected]>
CC: Petre Pircalabu <[email protected]>
---
Changes since V3:
- Update commit message
- Check rc and __copy_to_guest() in the same if
- Fix style issue
- Fix comment typo
- Init p2m with host_p2m
- Use array_index_nospec() in altp2m_p2m[] and altp2m_eptp[]
- Drop opaque
- Use pad2 to return first error code
- Update first_gfn
- Stop the range loop at cpuid->extd.maxphysaddr.
--- tools/libxc/include/xenctrl.h | 4 +++ tools/libxc/xc_altp2m.c | 33 +++++++++++++++++ xen/arch/x86/hvm/hvm.c | 15 ++++++++ xen/arch/x86/mm/p2m.c | 64 +++++++++++++++++++++++++++++++++ xen/include/public/hvm/hvm_op.h | 13 +++++++ xen/include/xen/mem_access.h | 3 ++ 6 files changed, 132 insertions(+)
tools/libxc/include/xenctrl.h +4 -0
@@ -1923,6 +1923,10 @@ int xc_altp2m_switch_to_view(xc_interface *handle, uint32_t domid, uint16_t view_id);int xc_altp2m_set_suppress_ve (xc_interface *handle,uint32_t domid,uint16_t view_id,xen_pfn_t gfn,bool sve); +int xc_altp2m_set_supress_ve_multi (xc_interface *handle,uint32_t domid, +uint16_t view_id,xen_pfn_t first_gfn, +xen_pfn_t last_gfn,bool sve, +xen_pfn_t *error_gfn,uint32_t *error_code);int xc_altp2m_get_suppress_ve (xc_interface *handle,uint32_t domid,uint16_t view_id,xen_pfn_t gfn,bool *sve); int xc_altp2m_set_mem_access(xc_interface *handle, uint32_t domid,
tools/libxc/xc_altp2m.c +33 -0
@@ -234,6 +234,39 @@ int xc_altp2m_set_suppress_ve(xc_interface *handle, uint32_t domid,return rc; } +int xc_altp2m_set_supress_ve_multi (xc_interface *handle,uint32_t domid, +uint16_t view_id,xen_pfn_t first_gfn, +xen_pfn_t last_gfn,bool sve, +xen_pfn_t *error_gfn,uint32_t *error_code) +{ +int rc; +DECLARE_HYPERCALL_BUFFER (xen_hvm_altp2m_op_t, arg); + + arg =xc_hypercall_buffer_alloc (handle, arg,sizeof (*arg)); +if ( arg ==NULL ) +return -1 ; + + arg->version =HVMOP_ALTP2M_INTERFACE_VERSION ; + arg->cmd = HVMOP_altp2m_set_suppress_ve_multi; + arg->domain = domid; + arg->u.suppress_ve_multi.view = view_id; + arg->u.suppress_ve_multi.first_gfn = first_gfn; + arg->u.suppress_ve_multi.last_gfn = last_gfn; + arg->u.suppress_ve_multi.suppress_ve = sve; + + rc =xencall2 (handle->xcall, __HYPERVISOR_hvm_op, HVMOP_altp2m, +HYPERCALL_BUFFER_AS_ARG (arg)); + +if ( arg->u.suppress_ve_multi.first_error ) + { + *error_gfn = arg->u.suppress_ve_multi.first_error; + *error_code = arg->u.suppress_ve_multi.first_error_code; + } + +xc_hypercall_buffer_free (handle, arg); +return rc; +} +int xc_altp2m_set_mem_access (xc_interface *handle,uint32_t domid,uint16_t view_id,xen_pfn_t gfn,xenmem_access_t access)
xen/arch/x86/hvm/hvm.c +15 -0
@@ -4553,6 +4553,7 @@ static int do_altp2m_op(case HVMOP_altp2m_destroy_p2m:case HVMOP_altp2m_switch_p2m:case HVMOP_altp2m_set_suppress_ve: +case HVMOP_altp2m_set_suppress_ve_multi:case HVMOP_altp2m_get_suppress_ve:case HVMOP_altp2m_set_mem_access:case HVMOP_altp2m_set_mem_access_multi:
@@ -4711,6 +4712,20 @@ static int do_altp2m_op( }break ; +case HVMOP_altp2m_set_suppress_ve_multi: +if ( a.u.suppress_ve_multi.pad1 || + a.u.suppress_ve_multi.first_error_code || + a.u.suppress_ve_multi.first_error || + a.u.suppress_ve_multi.first_gfn > a.u.suppress_ve_multi.last_gfn ) + rc = -EINVAL ; +else + { + rc =p2m_set_suppress_ve_multi (d, &a.u.suppress_ve_multi); +if ( (!rc || rc == -ERESTART ) &&__copy_to_guest (arg, &a,1 ) ) + rc = -EFAULT ; + } +break ; +case HVMOP_altp2m_get_suppress_ve:if ( a.u.suppress_ve.pad1 || a.u.suppress_ve.pad2 ) rc = -EINVAL ;
xen/arch/x86/mm/p2m.c +64 -0
70 lines not on this page · raw message
xen/include/public/hvm/hvm_op.h +13 -0
@@ -46,6 +46,16 @@ struct xen_hvm_altp2m_suppress_ve {uint64_t gfn; }; +struct xen_hvm_altp2m_suppress_ve_multi { +uint16_t view; +uint8_t suppress_ve;/* Boolean type. */ +uint8_t pad1; +uint32_t first_error_code;/* Must be set to 0 . */ +uint64_t first_gfn;/* Value will be updated */ +uint64_t last_gfn; +uint64_t first_error;/* Gfn of the first error. Must be set to 0. */ +}; +#if __XEN_INTERFACE_VERSION__ <0x00040900 /* Set the logical level of one of a domain's PCI INTx wires. */
@@ -339,6 +349,8 @@ struct xen_hvm_altp2m_op {#define HVMOP_altp2m_vcpu_disable_notify 13/* Get the active vcpu p2m index */ #define HVMOP_altp2m_get_p2m_idx 14 +/* Set the "Supress #VE" bit for a range of pages */ +#define HVMOP_altp2m_set_suppress_ve_multi 15domid_t domain;uint16_t pad1;uint32_t pad2;
@@ -353,6 +365,7 @@ struct xen_hvm_altp2m_op {struct xen_hvm_altp2m_change_gfn change_gfn;struct xen_hvm_altp2m_set_mem_access_multi set_mem_access_multi;struct xen_hvm_altp2m_suppress_ve suppress_ve; +struct xen_hvm_altp2m_suppress_ve_multi suppress_ve_multi;struct xen_hvm_altp2m_vcpu_disable_notify disable_notify;struct xen_hvm_altp2m_get_vcpu_p2m_idx get_vcpu_p2m_idx;uint8_t pad[64 ];
xen/include/xen/mem_access.h +3 -0
@@ -75,6 +75,9 @@ long p2m_set_mem_access_multi(struct domain *d,int p2m_set_suppress_ve (struct domain *d,gfn_t gfn,bool suppress_ve,unsigned int altp2m_idx); +int p2m_set_suppress_ve_multi (struct domain *d, +struct xen_hvm_altp2m_suppress_ve_multi *suppress_ve); +int p2m_get_suppress_ve (struct domain *d,gfn_t gfn,bool *suppress_ve,unsigned int altp2m_idx);
Signature · 6 lines
2.17.1 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [Xen-devel] [PATCH V4 2/4] x86/altp2m: Add hypercall to set a range of sve bits
xen/arch/x86/hvm/hvm.c static int do_altp2m_op( · line 4720
Quoted 10 lines · Alexandru Stefan ISAILA
xen/arch/x86/hvm/hvm.c +6
-0@@ -4711,6 +4712,20 @@ static int do_altp2m_op( }break ; +case HVMOP_altp2m_set_suppress_ve_multi: +if ( a.u.suppress_ve_multi.pad1 || + a.u.suppress_ve_multi.first_error_code || + a.u.suppress_ve_multi.first_error || + a.u.suppress_ve_multi.first_gfn > a.u.suppress_ve_multi.last_gfn ) + rc = -EINVAL ;
An error having occurred doesn't prevent scheduling of a continuation. When you come back here, you'll then return -EINVAL instead of continuing the prior operation.
Quoted 26 lines
xen/arch/x86/mm/p2m.c +20
-0@@ -3064,6 +3064,70 @@ out:return rc; } +/* +* Set/clear the #VE suppress bit for multiple pages. Only available on VMX. +*/ +int p2m_set_suppress_ve_multi (struct domain *d, +struct xen_hvm_altp2m_suppress_ve_multi *sve) +{ +struct p2m_domain *host_p2m =p2m_get_hostp2m (d); +struct p2m_domain *ap2m =NULL ; +struct p2m_domain *p2m = host_p2m; +uint64_t start = sve->first_gfn; +int rc =0 ; +uint64_t max_phys_addr = (1UL << d->arch.cpuid->extd.maxphysaddr) -1 ; + +if ( sve->view >0 ) + { +if ( sve->view >=MAX_ALTP2M || + d->arch.altp2m_eptp[array_index_nospec (sve->view,MAX_EPTP )] == +mfn_x (INVALID_MFN ) ) +return -EINVAL ; +
xen/arch/x86/mm/p2m.c out: · line 3096
Quoted 10 lines
xen/arch/x86/mm/p2m.c +10
-0+ p2m = ap2m = d->arch.altp2m_p2m[array_index_nospec (sve->view, +MAX_ALTP2M )]; + } + +p2m_lock (host_p2m); + +if ( ap2m ) +p2m_lock (ap2m); + +while ( sve->last_gfn >= start && start < max_phys_addr )
Why don't you clip ->last_gfn ahead of the loop, saving one comparison per iteration?
Quoted 5 lines
xen/arch/x86/mm/p2m.c +5
-0+ { +p2m_access_t a; +p2m_type_t t; +mfn_t mfn; +int err =0 ;
xen/arch/x86/mm/p2m.c out: · line 3111
Quoted 10 lines
xen/arch/x86/mm/p2m.c +10
-0+ +if (altp2m_get_effective_entry (p2m,_gfn (start), &mfn, &t, &a, AP2MGET_query) ) + a = p2m->default_access; + +if ( (err = p2m->set_entry (p2m,_gfn (start), mfn,PAGE_ORDER_4K , t, a, + sve->suppress_ve)) && !sve->first_error ) + { + sve->first_error = start;/* Save the gfn of the first error */ + sve->first_error_code = err;/* Save the first error code */ + }
What if the first error occurs on GFN 0? I guess you want to check ->first_error_code against zero in the condition.
xen/include/public/hvm/hvm_op.h struct xen_hvm_altp2m_suppress_ve { · line 53
Quoted 11 lines
xen/include/public/hvm/hvm_op.h +5
-0@@ -46,6 +46,16 @@ struct xen_hvm_altp2m_suppress_ve {uint64_t gfn; }; +struct xen_hvm_altp2m_suppress_ve_multi { +uint16_t view; +uint8_t suppress_ve;/* Boolean type. */ +uint8_t pad1; +uint32_t first_error_code;/* Must be set to 0 . */
int32_t perhaps, since error codes are negative?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [Xen-devel] [PATCH V4 2/4] x86/altp2m: Add hypercall to set a range of sve bits
Quoted 26 lines
Quoted 26 lines
xen/arch/x86/mm/p2m.c +25
-0+/* +* Set/clear the #VE suppress bit for multiple pages. Only available on VMX. +*/ +int p2m_set_suppress_ve_multi (struct domain *d, +struct xen_hvm_altp2m_suppress_ve_multi *sve) +{ +struct p2m_domain *host_p2m =p2m_get_hostp2m (d); +struct p2m_domain *ap2m =NULL ; +struct p2m_domain *p2m = host_p2m; +uint64_t start = sve->first_gfn; +int rc =0 ; +uint64_t max_phys_addr = (1UL << d->arch.cpuid->extd.maxphysaddr) -1 ; + +if ( sve->view >0 ) + { +if ( sve->view >=MAX_ALTP2M || + d->arch.altp2m_eptp[array_index_nospec (sve->view,MAX_EPTP )] == +mfn_x (INVALID_MFN ) ) +return -EINVAL ; + + p2m = ap2m = d->arch.altp2m_p2m[array_index_nospec (sve->view, +MAX_ALTP2M )]; + } + +p2m_lock (host_p2m);
Quoted 8 lines
xen/arch/x86/mm/p2m.c out: · line 3096
Quoted 5 lines
xen/arch/x86/mm/p2m.c +5
-0+ +if ( ap2m ) +p2m_lock (ap2m); + +while ( sve->last_gfn >= start && start < max_phys_addr )Why don't you clip ->last_gfn ahead of the loop, saving one comparison per iteration?
I've done this so it will have fewer lines but sure, I can have the ->last_gfn check before the loop.
Quoted 11 lines
Quoted 11 lines
xen/arch/x86/mm/p2m.c +10
-0+ { + p2m_access_t a; + p2m_type_t t; + mfn_t mfn; + int err = 0; + + if ( altp2m_get_effective_entry(p2m, _gfn(start), &mfn, &t, &a, AP2MGET_query) ) + a = p2m->default_access; + + if ( (err = p2m->set_entry(p2m, _gfn(start), mfn, PAGE_ORDER_4K, t, a,
Quoted 8 lines
xen/arch/x86/mm/p2m.c out: · line 3111
Quoted 5 lines
xen/arch/x86/mm/p2m.c +5
-0+ sve->suppress_ve)) && !sve->first_error ) + { + sve->first_error = start;/* Save the gfn of the first error */ + sve->first_error_code = err;/* Save the first error code */ + }What if the first error occurs on GFN 0? I guess you want to check ->first_error_code against zero in the condition.
That is right, I will change to check ->first_error_code instead of ->first_error.
Thanks,
Alex
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [Xen-devel] [PATCH V4 2/4] x86/altp2m: Add hypercall to set a range of sve bits
Quoted 35 lines · Alexandru Stefan ISAILA
Quoted 26 lines
Quoted 26 lines
xen/arch/x86/mm/p2m.c +25
-0+/* +* Set/clear the #VE suppress bit for multiple pages. Only available on VMX. +*/ +int p2m_set_suppress_ve_multi (struct domain *d, +struct xen_hvm_altp2m_suppress_ve_multi *sve) +{ +struct p2m_domain *host_p2m =p2m_get_hostp2m (d); +struct p2m_domain *ap2m =NULL ; +struct p2m_domain *p2m = host_p2m; +uint64_t start = sve->first_gfn; +int rc =0 ; +uint64_t max_phys_addr = (1UL << d->arch.cpuid->extd.maxphysaddr) -1 ; + +if ( sve->view >0 ) + { +if ( sve->view >=MAX_ALTP2M || + d->arch.altp2m_eptp[array_index_nospec (sve->view,MAX_EPTP )] == +mfn_x (INVALID_MFN ) ) +return -EINVAL ; + + p2m = ap2m = d->arch.altp2m_p2m[array_index_nospec (sve->view, +MAX_ALTP2M )]; + } + +p2m_lock (host_p2m);Quoted 8 lines
xen/arch/x86/mm/p2m.c out: · line 3096
Quoted 5 lines
xen/arch/x86/mm/p2m.c +5
-0+ +if ( ap2m ) +p2m_lock (ap2m); + +while ( sve->last_gfn >= start && start < max_phys_addr )Why don't you clip ->last_gfn ahead of the loop, saving one comparison per iteration?
Quoted 3 lines
I've done this so it will have fewer lines but sure, I can have the ->last_gfn check before the loop.
Wouldn't there be a issue if start goes over ->last_gfn and there is no break for preemption? Then the loop will run until max_phys_addr.
Alex
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [Xen-devel] [PATCH V4 2/4] x86/altp2m: Add hypercall to set a range of sve bits
Quoted 38 lines · Alexandru Stefan ISAILA
Quoted 35 lines · Alexandru Stefan ISAILA
Quoted 34 lines
Quoted 21 lines
xen/arch/x86/mm/p2m.c +20
-0+/* +* Set/clear the #VE suppress bit for multiple pages. Only available on VMX. +*/ +int p2m_set_suppress_ve_multi (struct domain *d, +struct xen_hvm_altp2m_suppress_ve_multi *sve) +{ +struct p2m_domain *host_p2m =p2m_get_hostp2m (d); +struct p2m_domain *ap2m =NULL ; +struct p2m_domain *p2m = host_p2m; +uint64_t start = sve->first_gfn; +int rc =0 ; +uint64_t max_phys_addr = (1UL << d->arch.cpuid->extd.maxphysaddr) -1 ; + +if ( sve->view >0 ) + { +if ( sve->view >=MAX_ALTP2M || + d->arch.altp2m_eptp[array_index_nospec (sve->view,MAX_EPTP )] == +mfn_x (INVALID_MFN ) ) +return -EINVAL ; +xen/arch/x86/mm/p2m.c out: · line 3096
Quoted 10 lines
xen/arch/x86/mm/p2m.c +10
-0+ p2m = ap2m = d->arch.altp2m_p2m[array_index_nospec (sve->view, +MAX_ALTP2M )]; + } + +p2m_lock (host_p2m); + +if ( ap2m ) +p2m_lock (ap2m); + +while ( sve->last_gfn >= start && start < max_phys_addr )Why don't you clip ->last_gfn ahead of the loop, saving one comparison per iteration?
Quoted 5 lines
Quoted 2 lines
I've done this so it will have fewer lines but sure, I can have the ->last_gfn check before the loop.
Wouldn't there be a issue if start goes over ->last_gfn and there is no break for preemption? Then the loop will run until
max_phys_addr.
I'm not sure I understand. My guess is a misunderstanding - I'm asking to replace the right side of the &&, and it looks you understood me to mean the least side. Note how I said "clip" in my earlier reply, meaning you to update ->last_gfn ahead of the loop if it's above (1UL << d->arch.cpuid->extd.maxphysaddr) - 1. Perhaps this could even be done in the caller together with (and possibly ahead of) the other sanity checking of incoming values.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [Xen-devel] [PATCH V4 2/4] x86/altp2m: Add hypercall to set a range of sve bits
Quoted 45 lines · Jan Beulich
Quoted 38 lines · Alexandru Stefan ISAILA
Quoted 35 lines · Alexandru Stefan ISAILA
Quoted 34 lines
Quoted 21 lines
xen/arch/x86/mm/p2m.c +20
-0+/* +* Set/clear the #VE suppress bit for multiple pages. Only available on VMX. +*/ +int p2m_set_suppress_ve_multi (struct domain *d, +struct xen_hvm_altp2m_suppress_ve_multi *sve) +{ +struct p2m_domain *host_p2m =p2m_get_hostp2m (d); +struct p2m_domain *ap2m =NULL ; +struct p2m_domain *p2m = host_p2m; +uint64_t start = sve->first_gfn; +int rc =0 ; +uint64_t max_phys_addr = (1UL << d->arch.cpuid->extd.maxphysaddr) -1 ; + +if ( sve->view >0 ) + { +if ( sve->view >=MAX_ALTP2M || + d->arch.altp2m_eptp[array_index_nospec (sve->view,MAX_EPTP )] == +mfn_x (INVALID_MFN ) ) +return -EINVAL ; +xen/arch/x86/mm/p2m.c out: · line 3096
Quoted 10 lines
xen/arch/x86/mm/p2m.c +10
-0+ p2m = ap2m = d->arch.altp2m_p2m[array_index_nospec (sve->view, +MAX_ALTP2M )]; + } + +p2m_lock (host_p2m); + +if ( ap2m ) +p2m_lock (ap2m); + +while ( sve->last_gfn >= start && start < max_phys_addr )Why don't you clip ->last_gfn ahead of the loop, saving one comparison per iteration?
Quoted 5 lines
Quoted 2 lines
I've done this so it will have fewer lines but sure, I can have the ->last_gfn check before the loop.
Wouldn't there be a issue if start goes over ->last_gfn and there is no break for preemption? Then the loop will run until
max_phys_addr.
Quoted 8 lines
I'm not sure I understand. My guess is a misunderstanding - I'm asking to replace the right side of the &&, and it looks you understood me to mean the least side. Note how I said "clip" in my earlier reply, meaning you to update ->last_gfn ahead of the loop if it's above (1UL <<
d->arch.cpuid->extd.maxphysaddr) - 1. Perhaps this could even be done in the caller together with (and possibly ahead of) the other sanity checking of incoming values.
Then I could have "sve->last_gfn = min(sve->last_gfn, max_phys_addr)" and then drop the "start < max_phys_addr" check from the while loop.
Alex
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v4 3/4 x86/mm: Pull out the p2m specifics from p2m_init_altp2m_ept
In reply to Alexandru Stefan ISAILA---
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Wei Liu <[email protected]>
CC: "Roger Pau Monné" <[email protected]>
--- xen/arch/x86/mm/p2m-ept.c | 6 ------ xen/arch/x86/mm/p2m.c | 6 ++++++ 2 files changed, 6 insertions(+), 6 deletions(-)
xen/arch/x86/mm/p2m-ept.c +0 -6
@@ -1358,13 +1358,7 @@ void p2m_init_altp2m_ept(struct domain *d, unsigned int i)struct p2m_domain *hostp2m =p2m_get_hostp2m (d);struct ept_data *ept; - p2m->default_access = hostp2m->default_access; - p2m->domain = hostp2m->domain; - - p2m->global_logdirty = hostp2m->global_logdirty; p2m->ept.ad = hostp2m->ept.ad; - p2m->min_remapped_gfn =gfn_x (INVALID_GFN ); - p2m->max_mapped_pfn = p2m->max_remapped_gfn =0 ; ept = &p2m->ept; ept->mfn =pagetable_get_pfn (p2m_get_pagetable (p2m)); d->arch.altp2m_eptp[array_index_nospec (i,MAX_EPTP )] = ept->eptp;
xen/arch/x86/mm/p2m.c +6 -0
@@ -2559,6 +2559,12 @@ static int p2m_activate_altp2m(struct domain *d, unsigned int idx) goto out; } + p2m->default_access = hostp2m->default_access; + p2m->domain = hostp2m->domain; + p2m->global_logdirty = hostp2m->global_logdirty; + p2m->min_remapped_gfn =gfn_x (INVALID_GFN ); + p2m->max_mapped_pfn = p2m->max_remapped_gfn =0 ; +p2m_init_altp2m_ept (d, idx); out:
Signature · 6 lines
2.17.1 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [Xen-devel] [PATCH V4 3/4] x86/mm: Pull out the p2m specifics from p2m_init_altp2m_ept
Quoted 1 line · Alexandru Stefan ISAILA
Signed-off-by Alexandru Isaila <[email protected]>
or perhaps rather Suggested-by or Requested-by.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v4 4/4 x86/mm: Make use of the default access param from xc_altp2m_create_view
In reply to Alexandru Stefan ISAILAAt this moment the default_access param from xc_altp2m_create_view is not used.
This patch assigns default_access to p2m->default_access at the time of initializing a new altp2m view.
---
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Wei Liu <[email protected]>
CC: "Roger Pau Monné" <[email protected]>
CC: George Dunlap <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Julien Grall <[email protected]>
CC: Konrad Rzeszutek Wilk <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Razvan Cojocaru <[email protected]>
CC: Tamas K Lengyel <[email protected]>
CC: Petre Pircalabu <[email protected]>
CC: George Dunlap <[email protected]>
---
Changes since V3:
- Change type of hvmmem_default_access to xenmem_access_t
- Fix style issues
- Release lock before return.
--- xen/arch/x86/hvm/hvm.c | 3 ++- xen/arch/x86/mm/mem_access.c | 6 +++--- xen/arch/x86/mm/p2m.c | 27 ++++++++++++++++++++++----- xen/include/asm-x86/p2m.h | 3 ++- xen/include/public/hvm/hvm_op.h | 2 -- xen/include/xen/mem_access.h | 4 ++++ 6 files changed, 33 insertions(+), 12 deletions(-)
xen/arch/x86/hvm/hvm.c +2 -1
@@ -4687,7 +4687,8 @@ static int do_altp2m_op( }case HVMOP_altp2m_create_p2m: -if ( !(rc =p2m_init_next_altp2m (d, &a.u.view.view)) ) +if ( !(rc =p2m_init_next_altp2m (d, &a.u.view.view, + a.u.view.hvmmem_default_access)) ) rc =__copy_to_guest (arg, &a,1 ) ? -EFAULT :0 ;break ;
xen/arch/x86/mm/mem_access.c +3 -3
@@ -314,9 +314,9 @@ static int set_mem_access(struct domain *d, struct p2m_domain *p2m,return rc; } -static bool xenmem_access_to_p2m_access(struct p2m_domain *p2m, - xenmem_access_t xaccess, - p2m_access_t *paccess) +bool xenmem_access_to_p2m_access(struct p2m_domain *p2m, + xenmem_access_t xaccess, + p2m_access_t *paccess) { static const p2m_access_t memaccess[] = { #define ACCESS(ac) [XENMEM_access_##ac] = p2m_access_##ac
xen/arch/x86/mm/p2m.c +22 -5
@@ -25,6 +25,7 @@#include <xen/guest_access.h> /* copy_from_guest() */ #include <xen/iommu.h> +#include <xen/mem_access.h> #include <xen/vm_event.h> #include <xen/event.h> #include <public/vm_event.h>
@@ -2533,7 +2534,8 @@ void p2m_flush_altp2m(struct domain *d)altp2m_list_unlock (d); } -static int p2m_activate_altp2m (struct domain *d,unsigned int idx) +static int p2m_activate_altp2m (struct domain *d,unsigned int idx, +p2m_access_t hvmmem_default_access) {struct p2m_domain *hostp2m, *p2m;int rc;
@@ -2559,7 +2561,7 @@ static int p2m_activate_altp2m(struct domain *d, unsigned int idx) goto out; } - p2m->default_access = hostp2m->default_access; + p2m->default_access = hvmmem_default_access; p2m->domain = hostp2m->domain; p2m->global_logdirty = hostp2m->global_logdirty; p2m->min_remapped_gfn =gfn_x (INVALID_GFN );
@@ -2576,6 +2578,7 @@ static int p2m_activate_altp2m(struct domain *d, unsigned int idx)int p2m_init_altp2m_by_id (struct domain *d,unsigned int idx) {int rc = -EINVAL ; +struct p2m_domain *hostp2m =p2m_get_hostp2m (d);if ( idx >=MAX_ALTP2M )return rc;
@@ -2583,16 +2586,22 @@ int p2m_init_altp2m_by_id(struct domain *d, unsigned int idx)altp2m_list_lock (d);if ( d->arch.altp2m_eptp[idx] ==mfn_x (INVALID_MFN ) ) - rc =p2m_activate_altp2m (d, idx); + rc =p2m_activate_altp2m (d, idx, hostp2m->default_access);altp2m_list_unlock (d);return rc; } -int p2m_init_next_altp2m (struct domain *d,uint16_t *idx) +int p2m_init_next_altp2m (struct domain *d,uint16_t *idx, +xenmem_access_t hvmmem_default_access) {int rc = -EINVAL ;unsigned int i; +p2m_access_t a; +struct p2m_domain *p2m; + +if ( hvmmem_default_access > XENMEM_access_default ) +return rc;altp2m_list_lock (d);
@@ -2601,7 +2610,15 @@ int p2m_init_next_altp2m(struct domain *d, uint16_t *idx)if ( d->arch.altp2m_eptp[i] !=mfn_x (INVALID_MFN ) )continue ; - rc =p2m_activate_altp2m (d, i); + p2m = d->arch.altp2m_p2m[i]; + +if ( !xenmem_access_to_p2m_access (p2m, hvmmem_default_access, &a) ) + { +altp2m_list_unlock (d); +return -EINVAL ; + } + + rc =p2m_activate_altp2m (d, i, a);if ( !rc ) *idx = i;
xen/include/asm-x86/p2m.h +2 -1
@@ -884,7 +884,8 @@ bool p2m_altp2m_get_or_propagate(struct p2m_domain *ap2m, unsigned long gfn_l,int p2m_init_altp2m_by_id (struct domain *d,unsigned int idx);/* Find an available alternate p2m and make it valid */ -int p2m_init_next_altp2m (struct domain *d,uint16_t *idx); +int p2m_init_next_altp2m (struct domain *d,uint16_t *idx, +xenmem_access_t hvmmem_default_access);/* Make a specific alternate p2m invalid */ int p2m_destroy_altp2m_by_id (struct domain *d,unsigned int idx);
xen/include/public/hvm/hvm_op.h +0 -2
@@ -251,8 +251,6 @@ DEFINE_XEN_GUEST_HANDLE(xen_hvm_altp2m_vcpu_disable_notify_t);struct xen_hvm_altp2m_view {/* IN/OUT variable */ uint16_t view; -/* Create view only: default access type -* NOTE: currently ignored */ uint16_t hvmmem_default_access;/* xenmem_access_t */ };typedef struct xen_hvm_altp2m_view xen_hvm_altp2m_view_t ;
xen/include/xen/mem_access.h +4 -0
@@ -58,6 +58,10 @@ typedef enum {/* NOTE: Assumed to be only 4 bits right now on x86. */ } p2m_access_t; +bool xenmem_access_to_p2m_access (struct p2m_domain *p2m, +xenmem_access_t xaccess, +p2m_access_t *paccess); +/* * Set access type for a region of gfns. * If gfn == INVALID_GFN, sets the default access type.
Signature · 6 lines
2.17.1 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [Xen-devel] [PATCH V4 4/4] x86/mm: Make use of the default access param from xc_altp2m_create_view
xen/arch/x86/mm/mem_access.c static int set_mem_access(struct domain *d, struct p2m_domain *p2m, · line 319
Quoted 12 lines · Alexandru Stefan ISAILA
xen/arch/x86/mm/mem_access.c +3
-3@@ -314,9 +314,9 @@ static int set_mem_access(struct domain *d, struct p2m_domain *p2m,return rc; } -static bool xenmem_access_to_p2m_access (struct p2m_domain *p2m, -xenmem_access_t xaccess, -p2m_access_t *paccess) +bool xenmem_access_to_p2m_access (struct p2m_domain *p2m, +xenmem_access_t xaccess, +p2m_access_t *paccess)
Would you mind taking the opportunity and add const to the first parameter?
xen/arch/x86/mm/mem_access.c int p2m_init_next_altp2m(struct domain *d, uint16_t *idx) · line 2619
Quoted 12 lines
xen/arch/x86/mm/mem_access.c +7
-1@@ -2601,7 +2610,15 @@ int p2m_init_next_altp2m(struct domain *d, uint16_t *idx)if ( d->arch.altp2m_eptp[i] !=mfn_x (INVALID_MFN ) )continue ; - rc =p2m_activate_altp2m (d, i); + p2m = d->arch.altp2m_p2m[i]; + +if ( !xenmem_access_to_p2m_access (p2m, hvmmem_default_access, &a) ) + { +altp2m_list_unlock (d); +return -EINVAL ; + }
Can this be pulled out of the locked region, ahead of the loop? The p2m getting passed in here (which is why it's in the loop) shouldn't have been in use yet, i.e. its ->default_access should have a known value. Hence this case could be taken care of independently, e.g. by adjusting xenmem_access_to_p2m_access() to cope with a NULL p2m coming in (producing whatever the default of the default is).
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [Xen-devel] [PATCH V4 4/4] x86/mm: Make use of the default access param from xc_altp2m_create_view
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Alexandru Stefan ISAILA
xen/arch/x86/mm/mem_access.c +0
-0@@ -314,9 +314,9 @@ static int set_mem_access(struct domain *d, struct p2m_domain *p2m,return rc; }
Quoted 9 lines
xen/arch/x86/mm/mem_access.c static int set_mem_access(struct domain *d, struct p2m_domain *p2m, · line 319
Quoted 6 lines
xen/arch/x86/mm/mem_access.c +3
-3-static bool xenmem_access_to_p2m_access (struct p2m_domain *p2m, -xenmem_access_t xaccess, -p2m_access_t *paccess) +bool xenmem_access_to_p2m_access (struct p2m_domain *p2m, +xenmem_access_t xaccess, +p2m_access_t *paccess)Would you mind taking the opportunity and add const to the first parameter?
Sure, given that there will be a new version, it will add it.
Quoted 5 lines
Quoted 5 lines
xen/arch/x86/mm/mem_access.c +0
-0@@ -2601,7 +2610,15 @@ int p2m_init_next_altp2m(struct domain *d, uint16_t *idx)if ( d->arch.altp2m_eptp[i] !=mfn_x (INVALID_MFN ) )continue ;
Quoted 17 lines
xen/arch/x86/mm/mem_access.c int p2m_init_next_altp2m(struct domain *d, uint16_t *idx) · line 2619
Quoted 8 lines
xen/arch/x86/mm/mem_access.c +7
-1- rc =p2m_activate_altp2m (d, i); + p2m = d->arch.altp2m_p2m[i]; + +if ( !xenmem_access_to_p2m_access (p2m, hvmmem_default_access, &a) ) + { +altp2m_list_unlock (d); +return -EINVAL ; + }Can this be pulled out of the locked region, ahead of the loop? The p2m getting passed in here (which is why it's in the loop) shouldn't have been in use yet, i.e. its ->default_access should have a known value. Hence this case could be taken care of independently, e.g. by adjusting
xenmem_access_to_p2m_access()to cope with a NULL p2m coming in (producing whatever the default of the default is).
Yes this sounds good. In xenmem_access_to_p2m_access() there can be a check like:
if ( !p2m )
*paccess = p2m_access_rwx;
else
*paccess = p2m->default_access;But before I change this maybe Tamas or George have something to add? And can this stay in the same patch or should it have a prereq one?
Alex
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 5 lines · Alexandru Stefan ISAILA
This patch aims to sanitize indexes, potentially guest provided values, for altp2m_eptp[] and altp2m_p2m[] arrays.
Signed-off-by Alexandru Isaila <[email protected]>
LGTM, thanks!
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
xen/arch/x86/mm/mem_access.c long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr, · line 370
Quoted 8 lines · Alexandru Stefan ISAILA
xen/arch/x86/mm/mem_access.c +1
-1@@ -367,10 +367,11 @@ long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr, if ( altp2m_idx ) { if ( altp2m_idx >= MAX_ALTP2M || - d->arch.altp2m_eptp[altp2m_idx] ==mfn_x (INVALID_MFN ) ) + d->arch.altp2m_eptp[array_index_nospec(altp2m_idx, MAX_EPTP)] ==
The bounds check is against MAX_ALTP2M. Both MAX_ values look to be independent, which means bounds check and value passed to the helper need to match up (not just here).
Quoted 33 lines
xen/arch/x86/mm/p2m-ept.c +3
-2@@ -1353,7 +1353,8 @@ void setup_ept_dump(void)void p2m_init_altp2m_ept (struct domain *d,unsigned int i) { -struct p2m_domain *p2m = d->arch.altp2m_p2m[i]; +struct p2m_domain *p2m = + d->arch.altp2m_p2m[array_index_nospec (i,MAX_ALTP2M )];struct p2m_domain *hostp2m =p2m_get_hostp2m (d);struct ept_data *ept;@@ -1366,7 +1367,7 @@ void p2m_init_altp2m_ept(struct domain *d, unsigned int i) p2m->max_mapped_pfn = p2m->max_remapped_gfn =0 ; ept = &p2m->ept; ept->mfn =pagetable_get_pfn (p2m_get_pagetable (p2m)); - d->arch.altp2m_eptp[i] = ept->eptp; + d->arch.altp2m_eptp[array_index_nospec (i,MAX_EPTP )] = ept->eptp; }unsigned int p2m_find_altp2m_by_eptp (struct domain *d,uint64_t eptp)xen/arch/x86/mm/p2m.c +1
-1@@ -2499,7 +2499,7 @@ static void p2m_reset_altp2m(struct domain *d, unsigned int idx,struct p2m_domain *p2m;ASSERT (idx <MAX_ALTP2M ); - p2m = d->arch.altp2m_p2m[idx]; + p2m = d->arch.altp2m_p2m[array_index_nospec (idx,MAX_ALTP2M )];p2m_lock (p2m);
xen/arch/x86/mm/p2m.c static int p2m_activate_altp2m(struct domain *d, unsigned int idx) · line 2543
Quoted 5 lines
xen/arch/x86/mm/p2m.c +1
-1@@ -2540,7 +2540,7 @@ static int p2m_activate_altp2m(struct domain *d, unsigned int idx)ASSERT (idx <MAX_ALTP2M ); - p2m = d->arch.altp2m_p2m[idx]; + p2m = d->arch.altp2m_p2m[array_index_nospec (idx,MAX_ALTP2M )];
All of the above have a more or less significant disconnect between the bounds check and the use as array index. I think it would be quite helpful if these could live close to one another, so one can (see further up) easily prove that both specified bounds actually match up.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 13 lines · Jan Beulich
xen/arch/x86/mm/mem_access.c long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr, · line 370
Quoted 8 lines · Alexandru Stefan ISAILA
xen/arch/x86/mm/mem_access.c +1
-1@@ -367,10 +367,11 @@ long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr, if ( altp2m_idx ) { if ( altp2m_idx >= MAX_ALTP2M || - d->arch.altp2m_eptp[altp2m_idx] ==mfn_x (INVALID_MFN ) ) + d->arch.altp2m_eptp[array_index_nospec(altp2m_idx, MAX_EPTP)] ==The bounds check is against
MAX_ALTP2M. BothMAX_values look to be independent, which means bounds check and value passed to the helper need to match up (not just here).
I will have both checks against MAX_ALTP2M.
Quoted 37 lines
Quoted 37 lines
xen/arch/x86/mm/p2m-ept.c +3
-2@@ -1353,7 +1353,8 @@ void setup_ept_dump(void)void p2m_init_altp2m_ept (struct domain *d,unsigned int i) { -struct p2m_domain *p2m = d->arch.altp2m_p2m[i]; +struct p2m_domain *p2m = + d->arch.altp2m_p2m[array_index_nospec (i,MAX_ALTP2M )];struct p2m_domain *hostp2m =p2m_get_hostp2m (d);struct ept_data *ept;@@ -1366,7 +1367,7 @@ void p2m_init_altp2m_ept(struct domain *d, unsigned int i) p2m->max_mapped_pfn = p2m->max_remapped_gfn =0 ; ept = &p2m->ept; ept->mfn =pagetable_get_pfn (p2m_get_pagetable (p2m)); - d->arch.altp2m_eptp[i] = ept->eptp; + d->arch.altp2m_eptp[array_index_nospec (i,MAX_EPTP )] = ept->eptp; }unsigned int p2m_find_altp2m_by_eptp (struct domain *d,uint64_t eptp)xen/arch/x86/mm/p2m.c +1
-1@@ -2499,7 +2499,7 @@ static void p2m_reset_altp2m(struct domain *d, unsigned int idx,struct p2m_domain *p2m;ASSERT (idx <MAX_ALTP2M ); - p2m = d->arch.altp2m_p2m[idx]; + p2m = d->arch.altp2m_p2m[array_index_nospec (idx,MAX_ALTP2M )];p2m_lock (p2m);@@ -2540,7 +2540,7 @@ static int p2m_activate_altp2m(struct domain *d, unsigned int idx)ASSERT (idx <MAX_ALTP2M );
Quoted 9 lines
xen/arch/x86/mm/p2m.c static int p2m_activate_altp2m(struct domain *d, unsigned int idx) · line 2543
Quoted 2 lines
xen/arch/x86/mm/p2m.c +1
-1- p2m = d->arch.altp2m_p2m[idx]; + p2m = d->arch.altp2m_p2m[array_index_nospec (idx,MAX_ALTP2M )];All of the above have a more or less significant disconnect between the bounds check and the use as array index. I think it would be quite helpful if these could live close to one another, so one can (see further up) easily prove that both specified bounds actually match up.
Sure, I can move the array use closer together.
Alex
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 66 lines · Alexandru Stefan ISAILA
Quoted 13 lines · Jan Beulich
xen/arch/x86/mm/mem_access.c long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr, · line 370
Quoted 8 lines · Alexandru Stefan ISAILA
xen/arch/x86/mm/mem_access.c +1
-1@@ -367,10 +367,11 @@ long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr, if ( altp2m_idx ) { if ( altp2m_idx >= MAX_ALTP2M || - d->arch.altp2m_eptp[altp2m_idx] ==mfn_x (INVALID_MFN ) ) + d->arch.altp2m_eptp[array_index_nospec(altp2m_idx, MAX_EPTP)] ==The bounds check is against
MAX_ALTP2M. BothMAX_values look to be independent, which means bounds check and value passed to the helper need to match up (not just here).I will have both checks against
MAX_ALTP2M.Quoted 37 lines
Quoted 37 lines
xen/arch/x86/mm/p2m-ept.c +3
-2@@ -1353,7 +1353,8 @@ void setup_ept_dump(void)void p2m_init_altp2m_ept (struct domain *d,unsigned int i) { -struct p2m_domain *p2m = d->arch.altp2m_p2m[i]; +struct p2m_domain *p2m = + d->arch.altp2m_p2m[array_index_nospec (i,MAX_ALTP2M )];struct p2m_domain *hostp2m =p2m_get_hostp2m (d);struct ept_data *ept;@@ -1366,7 +1367,7 @@ void p2m_init_altp2m_ept(struct domain *d, unsigned int i) p2m->max_mapped_pfn = p2m->max_remapped_gfn =0 ; ept = &p2m->ept; ept->mfn =pagetable_get_pfn (p2m_get_pagetable (p2m)); - d->arch.altp2m_eptp[i] = ept->eptp; + d->arch.altp2m_eptp[array_index_nospec (i,MAX_EPTP )] = ept->eptp; }unsigned int p2m_find_altp2m_by_eptp (struct domain *d,uint64_t eptp)xen/arch/x86/mm/p2m.c +1
-1@@ -2499,7 +2499,7 @@ static void p2m_reset_altp2m(struct domain *d, unsigned int idx,struct p2m_domain *p2m;ASSERT (idx <MAX_ALTP2M ); - p2m = d->arch.altp2m_p2m[idx]; + p2m = d->arch.altp2m_p2m[array_index_nospec (idx,MAX_ALTP2M )];p2m_lock (p2m);@@ -2540,7 +2540,7 @@ static int p2m_activate_altp2m(struct domain *d, unsigned int idx)ASSERT (idx <MAX_ALTP2M );Quoted 9 lines
xen/arch/x86/mm/p2m.c static int p2m_activate_altp2m(struct domain *d, unsigned int idx) · line 2543
Quoted 2 lines
xen/arch/x86/mm/p2m.c +1
-1- p2m = d->arch.altp2m_p2m[idx]; + p2m = d->arch.altp2m_p2m[array_index_nospec (idx,MAX_ALTP2M )];All of the above have a more or less significant disconnect between the bounds check and the use as array index. I think it would be quite helpful if these could live close to one another, so one can (see further up) easily prove that both specified bounds actually match up.
Quoted 2 lines
Sure, I can move the array use closer together.
Sorry to come back on this but I was looking in the code and I am not sure I follow where is the disconnect. If you are talking about p2m_init_altp2m_ept() the eptp code will move up in patch 3/4.
Can you please clarify?
Thanks,
Alex
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 49 lines · Alexandru Stefan ISAILA
Quoted 48 lines · Alexandru Stefan ISAILA
Quoted 46 lines · Jan Beulich
Quoted 33 lines · Alexandru Stefan ISAILA
xen/arch/x86/mm/p2m-ept.c +3
-2@@ -1353,7 +1353,8 @@ void setup_ept_dump(void)void p2m_init_altp2m_ept (struct domain *d,unsigned int i) { -struct p2m_domain *p2m = d->arch.altp2m_p2m[i]; +struct p2m_domain *p2m = + d->arch.altp2m_p2m[array_index_nospec (i,MAX_ALTP2M )];struct p2m_domain *hostp2m =p2m_get_hostp2m (d);struct ept_data *ept;@@ -1366,7 +1367,7 @@ void p2m_init_altp2m_ept(struct domain *d, unsigned int i) p2m->max_mapped_pfn = p2m->max_remapped_gfn =0 ; ept = &p2m->ept; ept->mfn =pagetable_get_pfn (p2m_get_pagetable (p2m)); - d->arch.altp2m_eptp[i] = ept->eptp; + d->arch.altp2m_eptp[array_index_nospec (i,MAX_EPTP )] = ept->eptp; }unsigned int p2m_find_altp2m_by_eptp (struct domain *d,uint64_t eptp)xen/arch/x86/mm/p2m.c +1
-1@@ -2499,7 +2499,7 @@ static void p2m_reset_altp2m(struct domain *d, unsigned int idx,struct p2m_domain *p2m;ASSERT (idx <MAX_ALTP2M ); - p2m = d->arch.altp2m_p2m[idx]; + p2m = d->arch.altp2m_p2m[array_index_nospec (idx,MAX_ALTP2M )];p2m_lock (p2m);xen/arch/x86/mm/p2m.c static int p2m_activate_altp2m(struct domain *d, unsigned int idx) · line 2543
Quoted 5 lines
xen/arch/x86/mm/p2m.c +1
-1@@ -2540,7 +2540,7 @@ static int p2m_activate_altp2m(struct domain *d, unsigned int idx)ASSERT (idx <MAX_ALTP2M ); - p2m = d->arch.altp2m_p2m[idx]; + p2m = d->arch.altp2m_p2m[array_index_nospec (idx,MAX_ALTP2M )];All of the above have a more or less significant disconnect between the bounds check and the use as array index. I think it would be quite helpful if these could live close to one another, so one can (see further up) easily prove that both specified bounds actually match up.
Quoted 6 lines
Quoted 2 lines
Sure, I can move the array use closer together.
Sorry to come back on this but I was looking in the code and I am not sure I follow where is the disconnect. If you are talking about
p2m_init_altp2m_ept()the eptp code will move up in patch 3/4.
My remark was about all four hunks left in context (and then still possibly extending to other ones). Let's take the last one above:p2m_activate_altp2m() has two callers, one of which loops over altp2m-s (and hence doesn't need the guard). The other one is p2m_init_altp2m_by_id() which does the range check I'm talking about (ASSERT() doesn't count), and which therefore is the place to use array_index_nospec(). Once you look there you'll notice that the function also has an array access itself which you've left untouched.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 57 lines · Jan Beulich
Quoted 49 lines · Alexandru Stefan ISAILA
Quoted 48 lines · Alexandru Stefan ISAILA
Quoted 46 lines · Jan Beulich
Quoted 33 lines · Alexandru Stefan ISAILA
xen/arch/x86/mm/p2m-ept.c +3
-2@@ -1353,7 +1353,8 @@ void setup_ept_dump(void)void p2m_init_altp2m_ept (struct domain *d,unsigned int i) { -struct p2m_domain *p2m = d->arch.altp2m_p2m[i]; +struct p2m_domain *p2m = + d->arch.altp2m_p2m[array_index_nospec (i,MAX_ALTP2M )];struct p2m_domain *hostp2m =p2m_get_hostp2m (d);struct ept_data *ept;@@ -1366,7 +1367,7 @@ void p2m_init_altp2m_ept(struct domain *d, unsigned int i) p2m->max_mapped_pfn = p2m->max_remapped_gfn =0 ; ept = &p2m->ept; ept->mfn =pagetable_get_pfn (p2m_get_pagetable (p2m)); - d->arch.altp2m_eptp[i] = ept->eptp; + d->arch.altp2m_eptp[array_index_nospec (i,MAX_EPTP )] = ept->eptp; }unsigned int p2m_find_altp2m_by_eptp (struct domain *d,uint64_t eptp)xen/arch/x86/mm/p2m.c +1
-1@@ -2499,7 +2499,7 @@ static void p2m_reset_altp2m(struct domain *d, unsigned int idx,struct p2m_domain *p2m;ASSERT (idx <MAX_ALTP2M ); - p2m = d->arch.altp2m_p2m[idx]; + p2m = d->arch.altp2m_p2m[array_index_nospec (idx,MAX_ALTP2M )];p2m_lock (p2m);xen/arch/x86/mm/p2m.c static int p2m_activate_altp2m(struct domain *d, unsigned int idx) · line 2543
Quoted 5 lines
xen/arch/x86/mm/p2m.c +1
-1@@ -2540,7 +2540,7 @@ static int p2m_activate_altp2m(struct domain *d, unsigned int idx)ASSERT (idx <MAX_ALTP2M ); - p2m = d->arch.altp2m_p2m[idx]; + p2m = d->arch.altp2m_p2m[array_index_nospec (idx,MAX_ALTP2M )];All of the above have a more or less significant disconnect between the bounds check and the use as array index. I think it would be quite helpful if these could live close to one another, so one can (see further up) easily prove that both specified bounds actually match up.
Quoted 6 lines
Quoted 2 lines
Sure, I can move the array use closer together.
Sorry to come back on this but I was looking in the code and I am not sure I follow where is the disconnect. If you are talking about
p2m_init_altp2m_ept()the eptp code will move up in patch 3/4.
Quoted 10 lines
My remark was about all four hunks left in context (and then still possibly extending to other ones). Let's take the last one above:
p2m_activate_altp2m()has two callers, one of which loops over altp2m-s (and hence doesn't need the guard). The other one isp2m_init_altp2m_by_id()which does the range check I'm talking about (ASSERT()doesn't count), and which therefore is the place to usearray_index_nospec(). Once you look there you'll notice that the function also has an array access itself which you've left untouched.
So add a "idx = array_index_nospec(idx, MAX_ALTP2M)" in the callers where there is a need for this and drop checks in the lower functions.
Alex
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 15 lines · Alexandru Stefan ISAILA
Quoted 13 lines · Jan Beulich
xen/arch/x86/mm/mem_access.c long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr, · line 370
Quoted 8 lines · Alexandru Stefan ISAILA
xen/arch/x86/mm/mem_access.c +1
-1@@ -367,10 +367,11 @@ long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr, if ( altp2m_idx ) { if ( altp2m_idx >= MAX_ALTP2M || - d->arch.altp2m_eptp[altp2m_idx] ==mfn_x (INVALID_MFN ) ) + d->arch.altp2m_eptp[array_index_nospec(altp2m_idx, MAX_EPTP)] ==The bounds check is against
MAX_ALTP2M. BothMAX_values look to be independent, which means bounds check and value passed to the helper need to match up (not just here).
Quoted 1 line
I will have both checks against
MAX_ALTP2M.
An alternative would be something along the lines of
if ( altp2m_idx >= min(MAX_ALTP2M, MAX_EPTP) ||
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel