x86/shadow: misc tidying
v2 cover x86/shadow: misc tidying
... or so I hope. The main observation was that we still have both hash_vcpu_for_each() and hash_domain_for_each(), where the latter was introduced in 2014/15 to replace the former. Only some eight years later we can now complete this conversion. Everything else addresses other things noticed along the road.
Note that patch 8 conflicts contextually but not functionally with patches 3 and 4 from "x86/mm: address aspects noticed during XSA-410 work".
1: replace sh_reset_l3_up_pointers()
2: drop hash_vcpu_foreach()
3: rename hash_domain_foreach()
4: drop a few uses of mfn_valid()
5: L2H shadow type is PV32-only
6: x86/shadow: re-work 4-level SHADOW_FOREACH_L2E() 7: reduce effort of hash calculation
8: call sh_detach_old_tables() directly
9: harden shadow_size()
Jan
PATCH v2 1/9 x86/shadow: replace sh_reset_l3_up_pointers()
Rather than doing a separate hash walk (and then even using the vCPU variant, which is to go away), do the up-pointer-clearing right in sh_unpin(), as an alternative to the (now further limited) enlisting on a "free floating" list fragment. This utilizes the fact that such list fragments are traversed only for multi-page shadows (in shadow_free()). Furthermore sh_terminate_list() is a safe guard only anyway, which isn't in use in the common case (it actually does anything only for BIGMEM configurations).
xen/arch/x86/include/asm/domain.h +3 -0
@@ -116,6 +116,9 @@ struct shadow_domain {/* OOS */ bool_t oos_active; +/* Domain is in the process of leaving SHOPT_LINUX_L3_TOPLEVEL mode. */ +bool unpinning_l3; +#ifdef CONFIG_HVM /* Has this domain ever used HVMOP_pagetable_dying? */ bool_t pagetable_dying_op;
xen/arch/x86/mm/shadow/common.c +0 -23
@@ -2302,29 +2302,6 @@ void shadow_prepare_page_type_change(str/**************************************************************************/ -/* Reset the up-pointers of every L3 shadow to 0. -* This is called when l3 shadows stop being pinnable, to clear out all -* the list-head bits so the up-pointer field is properly inititalised. */ -static int cf_check sh_clear_up_pointer( -struct vcpu *v,mfn_t smfn,mfn_t unused) -{ -mfn_to_page (smfn)->up =0 ; -return 0 ; -} - -void sh_reset_l3_up_pointers (struct vcpu *v) -{ -static const hash_vcpu_callback_t callbacks[SH_type_unused] = { - [SH_type_l3_64_shadow] = sh_clear_up_pointer, - }; - -HASH_CALLBACKS_CHECK (SHF_L3_64 ); -hash_vcpu_foreach (v,SHF_L3_64 , callbacks,INVALID_MFN ); -} - - -/**************************************************************************/ -static void sh_update_paging_modes (struct vcpu *v) {struct domain *d = v->domain;
xen/arch/x86/mm/shadow/multi.c +4 -1
@@ -960,6 +960,8 @@ sh_make_shadow(struct vcpu *v, mfn_t gmf }if ( l4count >2 * d->max_vcpus ) { + d->arch.paging.shadow.unpinning_l3 = true; +/* Unpin all the pinned l3 tables, and don't pin any more. */ page_list_for_each_safe (sp, t, &d->arch.paging.shadow.pinned_shadows) {
@@ -967,7 +969,8 @@ sh_make_shadow(struct vcpu *v, mfn_t gmf sh_unpin(d, page_to_mfn(sp)); } d->arch.paging.shadow.opt_flags &= ~SHOPT_LINUX_L3_TOPLEVEL ; -sh_reset_l3_up_pointers (v); + + d->arch.paging.shadow.unpinning_l3 = false; } }#endif
xen/arch/x86/mm/shadow/private.h +16 -11
@@ -497,11 +497,6 @@ void shadow_blow_tables(struct domain *d*/ int sh_remove_all_mappings (struct domain *d,mfn_t gmfn,gfn_t gfn); -/* Reset the up-pointers of every L3 shadow to 0. -* This is called when l3 shadows stop being pinnable, to clear out all -* the list-head bits so the up-pointer field is properly inititalised. */ -void sh_reset_l3_up_pointers (struct vcpu *v); -/****************************************************************************** * Flags used in the return value of the shadow_set_lXe() functions... */
@@ -721,7 +716,7 @@ static inline void sh_unpin(struct domai {struct page_list_head tmp_list, *pin_list;struct page_info *sp, *next; -unsigned int i, head_type; +unsigned int i, head_type, sz;ASSERT (mfn_valid (smfn)); sp =mfn_to_page (smfn);
@@ -733,20 +728,30 @@ static inline void sh_unpin(struct domaireturn ; sp->u.sh.pinned =0 ; -/* Cut the sub-list out of the list of pinned shadows, -* stitching it back into a list fragment of its own. */ + sz =shadow_size (head_type); + +/* +* Cut the sub-list out of the list of pinned shadows, stitching +* multi-page shadows back into a list fragment of their own. +*/ pin_list = &d->arch.paging.shadow.pinned_shadows;INIT_PAGE_LIST_HEAD (&tmp_list); -for ( i =0 ; i <shadow_size (head_type); i++ ) +for ( i =0 ; i < sz; i++ ) {ASSERT (sp->u.sh.type == head_type);ASSERT (!i || !sp->u.sh.head); next =page_list_next (sp, pin_list);page_list_del (sp, pin_list); -page_list_add_tail (sp, &tmp_list); +if ( sz >1 ) +page_list_add_tail (sp, &tmp_list); +else if ( head_type == SH_type_l3_64_shadow && + d->arch.paging.shadow.unpinning_l3 ) + sp->up =0 ; sp = next; } -sh_terminate_list (&tmp_list); + +if ( sz >1 ) +sh_terminate_list (&tmp_list);sh_put_ref (d, smfn,0 ); }
Re: [PATCH v2 1/9] x86/shadow: replace sh_reset_l3_up_pointers()
Quoted 5 lines · Jan Beulich
Rather than doing a separate hash walk (and then even using the vCPU variant, which is to go away), do the up-pointer-clearing right in
sh_unpin(), as an alternative to the (now further limited) enlisting on a "free floating" list fragment. This utilizes the fact that such list fragments are traversed only for multi-page shadows (inshadow_free()).
I've added mention of sh_next_page() here as well. Not sure how I managed to miss that, but this doesn't change the reasoning.
Jan
Re: [PATCH v2 1/9] x86/shadow: replace sh_reset_l3_up_pointers()
In reply to Jan BeulichQuoted 9 lines · Jan Beulich
Rather than doing a separate hash walk (and then even using the vCPU variant, which is to go away), do the up-pointer-clearing right in
sh_unpin(), as an alternative to the (now further limited) enlisting on a "free floating" list fragment. This utilizes the fact that such list fragments are traversed only for multi-page shadows (inshadow_free()). Furthermoresh_terminate_list()is a safe guard only anyway, which isn't in use in the common case (it actually does anything only for BIGMEM configurations).
One thing that seems strange about this patch is that you're essentially adding a field to the domain shadow struct in lieu of adding another another argument to sh_unpin() (unless the bit is referenced elsewhere in subsequent patches, which I haven't reviewed, in part because about half of them don't apply cleanly to the current tree).
-George
Re: [PATCH v2 1/9] x86/shadow: replace sh_reset_l3_up_pointers()
Quoted 11 lines · George Dunlap
Quoted 8 lines · Jan Beulich
Rather than doing a separate hash walk (and then even using the vCPU variant, which is to go away), do the up-pointer-clearing right in
sh_unpin(), as an alternative to the (now further limited) enlisting on a "free floating" list fragment. This utilizes the fact that such list fragments are traversed only for multi-page shadows (inshadow_free()). Furthermoresh_terminate_list()is a safe guard only anyway, which isn't in use in the common case (it actually does anything only for BIGMEM configurations).
Quoted 5 lines
One thing that seems strange about this patch is that you're essentially adding a field to the domain shadow struct in lieu of adding another another argument to
sh_unpin()(unless the bit is referenced elsewhere in subsequent patches, which I haven't reviewed, in part because about half of them don't apply cleanly to the current tree).
Well, to me adding another parameter to sh_unpin() would have looked odd; the new field looks slightly cleaner to me. But changing that is merely a matter of taste, so if you and e.g. Andrew think that approach was better, I could switch to that. And no, I don't foresee further uses of the field.
As to half of the patches not applying: Some where already applied out of order, and others therefore need re-basing slightly. Till now I saw no reason to re-send the remaining patches just for that.
Jan
Re: [PATCH v2 1/9] x86/shadow: replace sh_reset_l3_up_pointers()
Quoted 16 lines · Jan Beulich
Quoted 11 lines · George Dunlap
Quoted 8 lines · Jan Beulich
Rather than doing a separate hash walk (and then even using the vCPU variant, which is to go away), do the up-pointer-clearing right in
sh_unpin(), as an alternative to the (now further limited) enlisting on a "free floating" list fragment. This utilizes the fact that such list fragments are traversed only for multi-page shadows (inshadow_free()). Furthermoresh_terminate_list()is a safe guard only anyway, which isn't in use in the common case (it actually does anything only for BIGMEM configurations).Quoted 4 lines
One thing that seems strange about this patch is that you're essentially adding a field to the domain shadow struct in lieu of adding another another argument to
sh_unpin()(unless the bit is referenced elsewhere in subsequent patches, which I haven't reviewed, in part because about half
Quoted 8 lines
of
Quoted 1 line
them don't apply cleanly to the current tree).
Well, to me adding another parameter to
sh_unpin()would have looked odd; the new field looks slightly cleaner to me. But changing that is merely a matter of taste, so if you and e.g. Andrew think that approach was better, I could switch to that. And no, I don't foresee further uses of the field.
You're about to call sh_unpin(), and you want to tell that function to change its behavior. What's so odd about adding an argument to the function to indicate the behavior? Instead you're adding a bit of global state which is carried around 100% of the time, even when that function isn't being called. That's not what people normally expect; it makes the code harder to reason about.
It would certainly be ugly to have to add "false" to every other instance of sh_unpin; but the normal way you get around that is to redefine sh_unpin() as a wrapper which calls the other function with the 'false' argument set.
You asked me to review this for a second opinion on the safety of clearing the up-pointer this way, not because you need an ack; so I don't really want to block the patch for non-functional reasons. But I think this is one of the "death by a thousand cuts" that makes the shadow code more fragile and difficult for new people to approach and understand.
Re the original question: I've stared at the code for a bit now, and I can't see anything obviously wrong or dangerous about it.
But it does make me ask, why do we need the "unpinning_l3" pseudo-argument at all? Is there any reason not to unconditionally zero out sp->up when we find a head_type of SH_type_l3_64_shadow? As far as I can tell, sp->list doesn't require any special state. Why do we make the effort to leave it alone when we're not unpinning all l3s?
In fact, is there a way to unpin an l3 shadow *other* than when we're unpinning all l3's? If so, then this patch, as written, is broken -- the original code clears the up-pointer for *all* L3_64 shadows, regardless of whether they're on the pinned list; the new patch will only clear the ones on the pinned list. But unconditionally clearing sp->up could actually fix that.
Thoughts?
As to half of the patches not applying: Some where already applied out of
Quoted 3 lines
order, and others therefore need re-basing slightly. Till now I saw no reason to re-send the remaining patches just for that.
Sorry if that sounded like complaining; I was only being preemptively defensive against the potential accusation that the answer would have been obvious if I'd just continued reviewing the series. :-). (And indeed if the whole series had applied I would have checked that the final result didn't have any other references to it.)
-George
Re: [PATCH v2 1/9] x86/shadow: replace sh_reset_l3_up_pointers()
Quoted 8 lines · George Dunlap
Re the original question: I've stared at the code for a bit now, and I can't see anything obviously wrong or dangerous about it.
But it does make me ask, why do we need the "
unpinning_l3" pseudo-argument at all? Is there any reason not to unconditionally zero outsp->upwhen we find ahead_typeofSH_type_l3_64_shadow? As far as I can tell,sp->listdoesn't require any special state. Why do we make the effort to leave it alone when we're not unpinning all l3s?
This was an attempt to retain original behavior as much as possible, but I'm afraid that, ...
Quoted 2 lines
In fact, is there a way to unpin an l3 shadow *other* than when we're unpinning all l3's?
... since the answer here is of course "yes", ...
Quoted 5 lines
If so, then this patch, as written, is broken -- the
original code clears the up-pointer for *all*L3_64shadows, regardless of whether they're on the pinned list; the new patch will only clear the ones on the pinned list. But unconditionally clearingsp->upcould actually fix that.
... you're right, and I failed (went too far) with that attempt. Plus it'll naturally resolve the parameter-vs-state aspect.
Jan
PATCH v2 2/9 x86/shadow: drop hash_vcpu_foreach()
In reply to Jan BeulichThe domain based variant is easily usable by shadow_audit_tables(); all that's needed is conversion of the callback functions.
xen/arch/x86/mm/shadow/common.c +2 -50
@@ -1640,59 +1640,11 @@ bool shadow_hash_delete(struct domain *dreturn true; } -typedef int (*hash_vcpu_callback_t )(struct vcpu *v,mfn_t smfn,mfn_t other_mfn);typedef int (*hash_domain_callback_t )(struct domain *d,mfn_t smfn,mfn_t other_mfn);#define HASH_CALLBACKS_CHECK (mask) \ BUILD_BUG_ON((mask) > (1U << ARRAY_SIZE(callbacks)) - 1) -static void hash_vcpu_foreach (struct vcpu *v,unsigned int callback_mask, -const hash_vcpu_callback_t callbacks[], -mfn_t callback_mfn) -/* Walk the hash table looking at the types of the entries and -* calling the appropriate callback function for each entry. -* The mask determines which shadow types we call back for, and the array -* of callbacks tells us which function to call. -* Any callback may return non-zero to let us skip the rest of the scan. -* -* WARNING: Callbacks MUST NOT add or remove hash entries unless they -* then return non-zero to terminate the scan. */ -{ -int i, done =0 ; -struct domain *d = v->domain; -struct page_info *x; - -ASSERT (paging_locked_by_me (d)); - -/* Can be called via p2m code &c after shadow teardown. */ -if (unlikely (!d->arch.paging.shadow.hash_table) ) -return ; - -/* Say we're here, to stop hash-lookups reordering the chains */ -ASSERT (d->arch.paging.shadow.hash_walking ==0 ); - d->arch.paging.shadow.hash_walking =1 ; - -for ( i =0 ; i <SHADOW_HASH_BUCKETS ; i++ ) - { -/* WARNING: This is not safe against changes to the hash table. -* The callback *must* return non-zero if it has inserted or -* deleted anything from the hash (lookups are OK, though). */ -for ( x = d->arch.paging.shadow.hash_table[i]; x; x =next_shadow (x) ) - { -if ( callback_mask & (1 << x->u.sh.type) ) - { -ASSERT (x->u.sh.type <= SH_type_max_shadow); -ASSERT (callbacks[x->u.sh.type] !=NULL ); - done = callbacks[x->u.sh.type](v,page_to_mfn (x), - callback_mfn); -if ( done )break ; - } - } -if ( done )break ; - } - d->arch.paging.shadow.hash_walking =0 ; -} - static void hash_domain_foreach(struct domain *d, unsigned int callback_mask, const hash_domain_callback_t callbacks[],
@@ -3211,7 +3163,7 @@ int shadow_domctl(struct domain *d,void shadow_audit_tables (struct vcpu *v) {/* Dispatch table for getting per-type functions */ -static const hash_vcpu_callback_t callbacks[SH_type_unused] = { +static const hash_domain_callback_t callbacks[SH_type_unused] = { #if SHADOW_AUDIT & (SHADOW_AUDIT_ENTRIES | SHADOW_AUDIT_ENTRIES_FULL) # ifdef CONFIG_HVM [SH_type_l1_32_shadow] = SHADOW_INTERNAL_NAME(sh_audit_l1_table, 2),
@@ -3258,7 +3210,7 @@ void shadow_audit_tables(struct vcpu *v)HASH_CALLBACKS_CHECK (SHADOW_AUDIT & (SHADOW_AUDIT_ENTRIES |SHADOW_AUDIT_ENTRIES_FULL ) ? SHF_page_type_mask :0 ); -hash_vcpu_foreach (v, mask, callbacks,INVALID_MFN ); +hash_domain_foreach (v->domain, mask, callbacks,INVALID_MFN ); }#ifdef CONFIG_PV
xen/arch/x86/mm/shadow/multi.c +11 -15
@@ -326,32 +326,32 @@ static void sh_audit_gw(struct vcpu *v,if (mfn_valid (gw->l4mfn) &&mfn_valid ((smfn =get_shadow_status (d, gw->l4mfn, SH_type_l4_shadow))) ) - (void )sh_audit_l4_table (v, smfn,INVALID_MFN ); +sh_audit_l4_table (d, smfn,INVALID_MFN );if (mfn_valid (gw->l3mfn) &&mfn_valid ((smfn =get_shadow_status (d, gw->l3mfn, SH_type_l3_shadow))) ) - (void )sh_audit_l3_table (v, smfn,INVALID_MFN ); +sh_audit_l3_table (d, smfn,INVALID_MFN );#endif /* PAE or 64... */ if (mfn_valid (gw->l2mfn) ) {if (mfn_valid ((smfn =get_shadow_status (d, gw->l2mfn, SH_type_l2_shadow))) ) - (void )sh_audit_l2_table (v, smfn,INVALID_MFN ); +sh_audit_l2_table (d, smfn,INVALID_MFN );#if GUEST_PAGING_LEVELS >=4 /* 32-bit PV only */ if (mfn_valid ((smfn =get_shadow_status (d, gw->l2mfn, SH_type_l2h_shadow))) ) - (void )sh_audit_l2_table (v, smfn,INVALID_MFN ); +sh_audit_l2_table (d, smfn,INVALID_MFN );#endif }if (mfn_valid (gw->l1mfn) &&mfn_valid ((smfn =get_shadow_status (d, gw->l1mfn, SH_type_l1_shadow))) ) - (void )sh_audit_l1_table (v, smfn,INVALID_MFN ); +sh_audit_l1_table (d, smfn,INVALID_MFN );else if ( (guest_l2e_get_flags (gw->l2e) & _PAGE_PRESENT) && (guest_l2e_get_flags (gw->l2e) & _PAGE_PSE) &&mfn_valid ( (smfn =get_fl1_shadow_status (d,guest_l2e_get_gfn (gw->l2e)))) ) - (void )sh_audit_fl1_table (v, smfn,INVALID_MFN ); +sh_audit_fl1_table (d, smfn,INVALID_MFN );#endif /* SHADOW_AUDIT & SHADOW_AUDIT_ENTRIES */ }
@@ -3950,9 +3950,8 @@ static const char *sh_audit_flags(constreturn NULL ; } -int cf_check sh_audit_l1_table(struct vcpu *v, mfn_t sl1mfn, mfn_t x) +int cf_check sh_audit_l1_table(struct domain *d, mfn_t sl1mfn, mfn_t x) { -struct domain *d = v->domain;guest_l1e_t *gl1e, *gp;shadow_l1e_t *sl1e;mfn_t mfn, gmfn, gl1mfn;
@@ -4019,7 +4018,7 @@ int cf_check sh_audit_l1_table(struct vcreturn done; } -int cf_check sh_audit_fl1_table(struct vcpu *v, mfn_t sl1mfn, mfn_t x) +int cf_check sh_audit_fl1_table(struct domain *d, mfn_t sl1mfn, mfn_t x) {guest_l1e_t *gl1e, e;shadow_l1e_t *sl1e;
@@ -4045,9 +4044,8 @@ int cf_check sh_audit_fl1_table(struct vreturn 0 ; } -int cf_check sh_audit_l2_table(struct vcpu *v, mfn_t sl2mfn, mfn_t x) +int cf_check sh_audit_l2_table(struct domain *d, mfn_t sl2mfn, mfn_t x) { -struct domain *d = v->domain;guest_l2e_t *gl2e, *gp;shadow_l2e_t *sl2e;mfn_t mfn, gmfn, gl2mfn;
@@ -4097,9 +4095,8 @@ int cf_check sh_audit_l2_table(struct vc }#if GUEST_PAGING_LEVELS >=4 -int cf_check sh_audit_l3_table(struct vcpu *v, mfn_t sl3mfn, mfn_t x) +int cf_check sh_audit_l3_table(struct domain *d, mfn_t sl3mfn, mfn_t x) { -struct domain *d = v->domain;guest_l3e_t *gl3e, *gp;shadow_l3e_t *sl3e;mfn_t mfn, gmfn, gl3mfn;
@@ -4145,9 +4142,8 @@ int cf_check sh_audit_l3_table(struct vcreturn 0 ; } -int cf_check sh_audit_l4_table(struct vcpu *v, mfn_t sl4mfn, mfn_t x) +int cf_check sh_audit_l4_table(struct domain *d, mfn_t sl4mfn, mfn_t x) { -struct domain *d = v->domain;guest_l4e_t *gl4e, *gp;shadow_l4e_t *sl4e;mfn_t mfn, gmfn, gl4mfn;
xen/arch/x86/mm/shadow/multi.h +5 -5
@@ -83,19 +83,19 @@ SHADOW_INTERNAL_NAME(sh_remove_l3_shadow#if SHADOW_AUDIT &SHADOW_AUDIT_ENTRIES int cf_checkSHADOW_INTERNAL_NAME (sh_audit_l1_table ,GUEST_LEVELS ) - (struct vcpu *v,mfn_t sl1mfn,mfn_t x); + (struct domain *d,mfn_t sl1mfn,mfn_t x); int cf_checkSHADOW_INTERNAL_NAME (sh_audit_fl1_table ,GUEST_LEVELS ) - (struct vcpu *v,mfn_t sl1mfn,mfn_t x); + (struct domain *d,mfn_t sl1mfn,mfn_t x); int cf_checkSHADOW_INTERNAL_NAME (sh_audit_l2_table ,GUEST_LEVELS ) - (struct vcpu *v,mfn_t sl2mfn,mfn_t x); + (struct domain *d,mfn_t sl2mfn,mfn_t x); int cf_checkSHADOW_INTERNAL_NAME (sh_audit_l3_table ,GUEST_LEVELS ) - (struct vcpu *v,mfn_t sl3mfn,mfn_t x); + (struct domain *d,mfn_t sl3mfn,mfn_t x); int cf_checkSHADOW_INTERNAL_NAME (sh_audit_l4_table ,GUEST_LEVELS ) - (struct vcpu *v,mfn_t sl4mfn,mfn_t x); + (struct domain *d,mfn_t sl4mfn,mfn_t x);#endif extern const struct paging_mode
PATCH v2 3/9 x86/shadow: rename hash_domain_foreach()
In reply to Jan BeulichThe "domain" in there has become meaningless; drop it.
xen/arch/x86/mm/shadow/common.c +16 -16
@@ -1640,15 +1640,15 @@ bool shadow_hash_delete(struct domain *dreturn true; } -typedef int (*hash_domain_callback_t )(struct domain *d,mfn_t smfn,mfn_t other_mfn); +typedef int (*hash_callback_t )(struct domain *d,mfn_t smfn,mfn_t other_mfn);#define HASH_CALLBACKS_CHECK (mask) \ BUILD_BUG_ON((mask) > (1U << ARRAY_SIZE(callbacks)) - 1) -static void hash_domain_foreach (struct domain *d, -unsigned int callback_mask, -const hash_domain_callback_t callbacks[], -mfn_t callback_mfn) +static void hash_foreach (struct domain *d, +unsigned int callback_mask, +const hash_callback_t callbacks[], +mfn_t callback_mfn)/* Walk the hash table looking at the types of the entries and * calling the appropriate callback function for each entry. * The mask determines which shadow types we call back for, and the array
@@ -1784,7 +1784,7 @@ int sh_remove_write_access(struct domain unsigned long fault_addr) {/* Dispatch table for getting per-type functions */ -static const hash_domain_callback_t callbacks[SH_type_unused] = { +static const hash_callback_t callbacks[SH_type_unused] = { #ifdef CONFIG_HVM [SH_type_l1_32_shadow] =SHADOW_INTERNAL_NAME (sh_rm_write_access_from_l1,2 ), [SH_type_fl1_32_shadow] = SHADOW_INTERNAL_NAME(sh_rm_write_access_from_l1, 2),
@@ -1969,7 +1969,7 @@ int sh_remove_write_access(struct domainelse perfc_incr (shadow_writeable_bf);HASH_CALLBACKS_CHECK (SHF_L1_ANY |SHF_FL1_ANY ); -hash_domain_foreach (d,SHF_L1_ANY |SHF_FL1_ANY , callbacks, gmfn); +hash_foreach (d,SHF_L1_ANY |SHF_FL1_ANY , callbacks, gmfn);/* If that didn't catch the mapping, then there's some non-pagetable * mapping -- ioreq page, grant mapping, &c. */
@@ -1997,7 +1997,7 @@ int sh_remove_all_mappings(struct domainstruct page_info *page =mfn_to_page (gmfn);/* Dispatch table for getting per-type functions */ -static const hash_domain_callback_t callbacks[SH_type_unused] = { +static const hash_callback_t callbacks[SH_type_unused] = { [SH_type_l1_32_shadow] =SHADOW_INTERNAL_NAME (sh_rm_mappings_from_l1,2 ), [SH_type_fl1_32_shadow] =SHADOW_INTERNAL_NAME (sh_rm_mappings_from_l1,2 ), [SH_type_l1_pae_shadow] =SHADOW_INTERNAL_NAME (sh_rm_mappings_from_l1,3 ),
@@ -2021,7 +2021,7 @@ int sh_remove_all_mappings(struct domain/* Brute-force search of all the shadows, by walking the hash */ perfc_incr (shadow_mappings_bf);HASH_CALLBACKS_CHECK (SHF_L1_ANY |SHF_FL1_ANY ); -hash_domain_foreach (d,SHF_L1_ANY |SHF_FL1_ANY , callbacks, gmfn); +hash_foreach (d,SHF_L1_ANY |SHF_FL1_ANY , callbacks, gmfn);/* If that didn't catch the mapping, something is very wrong */ if ( !sh_check_page_has_no_refs (page) )
@@ -2128,7 +2128,7 @@ void sh_remove_shadows(struct domain *d,/* Dispatch table for getting per-type functions: each level must * be called with the function to remove a lower-level shadow. */ -static const hash_domain_callback_t callbacks[SH_type_unused] = { +static const hash_callback_t callbacks[SH_type_unused] = { #ifdef CONFIG_HVM [SH_type_l2_32_shadow] =SHADOW_INTERNAL_NAME (sh_remove_l1_shadow,2 ), [SH_type_l2_pae_shadow] = SHADOW_INTERNAL_NAME(sh_remove_l1_shadow, 3),
@@ -2173,9 +2173,9 @@ void sh_remove_shadows(struct domain *d,/* * Lower-level shadows need to be excised from upper-level shadows. This -* call to hash_domain_foreach() looks dangerous but is in fact OK: each -* call will remove at most one shadow, and terminate immediately when -* it does remove it, so we never walk the hash after doing a deletion. +* call to hash_foreach() looks dangerous but is in fact OK: each call +* will remove at most one shadow, and terminate immediately when it does +* remove it, so we never walk the hash after doing a deletion. */ #define DO_UNSHADOW (_type) do { \ t = (_type); \
@@ -2199,7 +2199,7 @@ void sh_remove_shadows(struct domain *d, (pg->shadow_flags & (1 << t)) ) \ { \HASH_CALLBACKS_CHECK (SHF_page_type_mask); \ -hash_domain_foreach (d, masks[t], callbacks, smfn); \ +hash_foreach (d, masks[t], callbacks, smfn); \ } \ }while (0 )
@@ -3163,7 +3163,7 @@ int shadow_domctl(struct domain *d,void shadow_audit_tables (struct vcpu *v) {/* Dispatch table for getting per-type functions */ -static const hash_domain_callback_t callbacks[SH_type_unused] = { +static const hash_callback_t callbacks[SH_type_unused] = { #if SHADOW_AUDIT & (SHADOW_AUDIT_ENTRIES | SHADOW_AUDIT_ENTRIES_FULL) # ifdef CONFIG_HVM [SH_type_l1_32_shadow] = SHADOW_INTERNAL_NAME(sh_audit_l1_table, 2),
@@ -3210,7 +3210,7 @@ void shadow_audit_tables(struct vcpu *v)HASH_CALLBACKS_CHECK (SHADOW_AUDIT & (SHADOW_AUDIT_ENTRIES |SHADOW_AUDIT_ENTRIES_FULL ) ? SHF_page_type_mask :0 ); -hash_domain_foreach (v->domain, mask, callbacks,INVALID_MFN ); +hash_foreach (v->domain, mask, callbacks,INVALID_MFN ); }#ifdef CONFIG_PV
PATCH v2 4/9 x86/shadow: drop a few uses of mfn_valid()
In reply to Jan Beulichv->arch.paging.shadow.shadow_table[], v->arch.paging.shadow.oos[], v->arch.paging.shadow.oos_{snapshot[],fixup[].smfn[]} as well as the hash table are all only ever written with valid MFNs or INVALID_MFN. Avoid the somewhat expensive mfn_valid() when checking MFNs coming from these arrays.
---
There are many more uses which can likely be replaced, but I think we're better off doing this in piecemeal fashion.
xen/arch/x86/mm/shadow/common.c +10 -11
@@ -171,7 +171,7 @@ static void sh_oos_audit(struct domain *for ( idx =0 ; idx <SHADOW_OOS_PAGES ; idx++ ) {mfn_t *oos = v->arch.paging.shadow.oos; -if ( !mfn_valid (oos[idx]) ) +if (mfn_eq (oos[idx],INVALID_MFN ) )continue ; expected_idx =mfn_x (oos[idx]) %SHADOW_OOS_PAGES ;
@@ -327,8 +327,7 @@ void oos_fixup_add(struct domain *d, mfnint i;for ( i =0 ; i <SHADOW_OOS_FIXUPS ; i++ ) { -if (mfn_valid (oos_fixup[idx].smfn[i]) - &&mfn_eq (oos_fixup[idx].smfn[i], smfn) +if (mfn_eq (oos_fixup[idx].smfn[i], smfn) && (oos_fixup[idx].off[i] == off) )return ; }
@@ -461,7 +460,7 @@ static void oos_hash_add(struct vcpu *v, idx =mfn_x (gmfn) %SHADOW_OOS_PAGES ; oidx = idx; -if (mfn_valid (oos[idx]) +if ( !mfn_eq (oos[idx],INVALID_MFN ) && (mfn_x (oos[idx]) %SHADOW_OOS_PAGES ) == idx ) {/* Punt the current occupant into the next slot */
@@ -470,8 +469,8 @@ static void oos_hash_add(struct vcpu *v, swap =1 ; idx = (idx +1 ) %SHADOW_OOS_PAGES ; } -if (mfn_valid (oos[idx]) ) - { +if ( !mfn_eq (oos[idx],INVALID_MFN ) ) + {/* Crush the current occupant. */ _sh_resync (v, oos[idx], &oos_fixup[idx], oos_snapshot[idx]);perfc_incr (shadow_unsync_evict);
@@ -607,7 +606,7 @@ void sh_resync_all(struct vcpu *v, int s/* First: resync all of this vcpu's oos pages */ for ( idx =0 ; idx <SHADOW_OOS_PAGES ; idx++ ) -if (mfn_valid (oos[idx]) ) +if ( !mfn_eq (oos[idx],INVALID_MFN ) ) {/* Write-protect and sync contents */ _sh_resync (v, oos[idx], &oos_fixup[idx], oos_snapshot[idx]);
@@ -630,7 +629,7 @@ void sh_resync_all(struct vcpu *v, int sfor ( idx =0 ; idx <SHADOW_OOS_PAGES ; idx++ ) { -if ( !mfn_valid (oos[idx]) ) +if (mfn_eq (oos[idx],INVALID_MFN ) )continue ;if ( skip )
@@ -2183,7 +2182,7 @@ void sh_remove_shadows(struct domain *d, !(pg->shadow_flags & (1 << t)) ) \break ; \ smfn =shadow_hash_lookup (d,mfn_x (gmfn), t); \ -if (unlikely (!mfn_valid (smfn)) ) \ +if (unlikely (mfn_eq (smfn,INVALID_MFN )) ) \ { \ printk(XENLOG_G_ERR "gmfn %"PRI_mfn" has flags %#x" \ " but no type-%#x shadow\n", \
@@ -2751,7 +2750,7 @@ void shadow_teardown(struct domain *d, bint i;mfn_t *oos_snapshot = v->arch.paging.shadow.oos_snapshot;for ( i =0 ; i <SHADOW_OOS_PAGES ; i++ ) -if (mfn_valid (oos_snapshot[i]) ) +if ( !mfn_eq (oos_snapshot[i],INVALID_MFN ) ) {shadow_free (d, oos_snapshot[i]); oos_snapshot[i] =INVALID_MFN ;
@@ -2934,7 +2933,7 @@ static int shadow_one_bit_disable(structint i;mfn_t *oos_snapshot = v->arch.paging.shadow.oos_snapshot;for ( i =0 ; i <SHADOW_OOS_PAGES ; i++ ) -if (mfn_valid (oos_snapshot[i]) ) +if ( !mfn_eq (oos_snapshot[i],INVALID_MFN ) ) {shadow_free (d, oos_snapshot[i]); oos_snapshot[i] =INVALID_MFN ;
xen/arch/x86/mm/shadow/multi.c +4 -4
@@ -110,7 +110,7 @@ get_fl1_shadow_status(struct domain *d,/* Look for FL1 shadows in the hash table */ {mfn_t smfn =shadow_hash_lookup (d,gfn_x (gfn), SH_type_fl1_shadow); -ASSERT (!mfn_valid (smfn) ||mfn_to_page (smfn)->u.sh.head); +ASSERT (mfn_eq (smfn,INVALID_MFN ) ||mfn_to_page (smfn)->u.sh.head);return smfn; }
@@ -2680,7 +2680,7 @@ static int cf_check sh_page_fault(mfn_t smfn =pagetable_get_mfn ( v->arch.paging.shadow.shadow_table[i]); -if (mfn_valid (smfn) && (mfn_x (smfn) !=0 ) ) +if (mfn_x (smfn) ) { used |= (mfn_to_page (smfn)->v.sh.back ==mfn_x (gmfn));
@@ -3824,7 +3824,7 @@ static void cf_check sh_pagetable_dying( : shadow_hash_lookup(d, mfn_x(gmfn), SH_type_l2_pae_shadow); } -if (mfn_valid (smfn) ) +if ( !mfn_eq (smfn,INVALID_MFN ) ) { gmfn =_mfn (mfn_to_page (smfn)->v.sh.back);mfn_to_page (gmfn)->pagetable_dying = true;
@@ -3867,7 +3867,7 @@ static void cf_check sh_pagetable_dying( smfn =shadow_hash_lookup (d,mfn_x (gmfn), SH_type_l4_64_shadow);#endif -if (mfn_valid (smfn) ) +if ( !mfn_eq (smfn,INVALID_MFN ) ) {mfn_to_page (gmfn)->pagetable_dying = true;shadow_unhook_mappings (d, smfn,1 /* user pages only */ );
xen/arch/x86/mm/shadow/private.h +3 -1
@@ -769,8 +769,10 @@ get_shadow_status(struct domain *d, mfn_/* Look for shadows in the hash table */ {mfn_t smfn =shadow_hash_lookup (d,mfn_x (gmfn), shadow_type); -ASSERT (!mfn_valid (smfn) ||mfn_to_page (smfn)->u.sh.head); + +ASSERT (mfn_eq (smfn,INVALID_MFN ) ||mfn_to_page (smfn)->u.sh.head);perfc_incr (shadow_get_shadow_status); +return smfn; }
Re: [PATCH v2 4/9] x86/shadow: drop a few uses of mfn_valid()
Quoted 7 lines · Jan Beulich
v->arch.paging.shadow.shadow_table[], v->arch.paging.shadow.oos[], v->arch.paging.shadow.oos_{snapshot[],fixup[].smfn[]} as well as the hash table are all only ever written with valid MFNs or
INVALID_MFN. Avoid the somewhat expensivemfn_valid()when checking MFNs coming from these arrays.Signed-off-by Jan Beulich <[email protected]>
Technically I acked this in v1 because the comment wasn't a code comment, but Acked-by: Andrew Cooper <[email protected]> nevertheless
PATCH v2 5/9 x86/shadow: L2H shadow type is PV32-only
In reply to Jan BeulichLike for the various HVM-only types, save a little bit of code by suitably "masking" this type out when !PV32.
---
I wasn't really sure whether it would be worthwhile to also update the "#else" part of shadow_size(). Doing so would be a little tricky, as the type to return 0 for has no name right now; I'd need to move down the #undef to allow for that. Thoughts?
---
v2: Merely comment out the sh_type_to_size[] entry.
xen/arch/x86/mm/shadow/common.c +12 -0
@@ -1740,9 +1740,11 @@ void sh_destroy_shadow(struct domain *d,case SH_type_fl1_64_shadow:SHADOW_INTERNAL_NAME (sh_destroy_l1_shadow,4 )(d, smfn);break ; +#ifdef CONFIG_PV32 case SH_type_l2h_64_shadow:ASSERT (is_pv_32bit_domain (d));/* Fall through... */ +#endif case SH_type_l2_64_shadow:SHADOW_INTERNAL_NAME (sh_destroy_l2_shadow,4 )(d, smfn);break ;
@@ -2095,7 +2097,9 @@ static int sh_remove_shadow_via_pointer(#endif case SH_type_l1_64_shadow:case SH_type_l2_64_shadow: +#ifdef CONFIG_PV32 case SH_type_l2h_64_shadow: +#endif case SH_type_l3_64_shadow:case SH_type_l4_64_shadow:SHADOW_INTERNAL_NAME (sh_clear_shadow_entry,4 )(d, vaddr, pmfn);
@@ -2133,7 +2137,9 @@ void sh_remove_shadows(struct domain *d, [SH_type_l2_pae_shadow] = SHADOW_INTERNAL_NAME(sh_remove_l1_shadow, 3), #endif [SH_type_l2_64_shadow] =SHADOW_INTERNAL_NAME (sh_remove_l1_shadow,4 ), +#ifdef CONFIG_PV32 [SH_type_l2h_64_shadow] = SHADOW_INTERNAL_NAME(sh_remove_l1_shadow, 4), +#endif [SH_type_l3_64_shadow] = SHADOW_INTERNAL_NAME(sh_remove_l2_shadow, 4), [SH_type_l4_64_shadow] = SHADOW_INTERNAL_NAME(sh_remove_l3_shadow, 4), };
@@ -2146,7 +2152,9 @@ void sh_remove_shadows(struct domain *d, #endif [SH_type_l1_64_shadow] = SHF_L2H_64 | SHF_L2_64, [SH_type_l2_64_shadow] =SHF_L3_64 , +#ifdef CONFIG_PV32 [SH_type_l2h_64_shadow] = SHF_L3_64, +#endif [SH_type_l3_64_shadow] = SHF_L4_64, };
@@ -2210,7 +2218,9 @@ void sh_remove_shadows(struct domain *d,#endif DO_UNSHADOW (SH_type_l4_64_shadow);DO_UNSHADOW (SH_type_l3_64_shadow); +#ifdef CONFIG_PV32 DO_UNSHADOW (SH_type_l2h_64_shadow); +#endif DO_UNSHADOW (SH_type_l2_64_shadow);DO_UNSHADOW (SH_type_l1_64_shadow);
@@ -3175,7 +3185,9 @@ void shadow_audit_tables(struct vcpu *v) [SH_type_l1_64_shadow] = SHADOW_INTERNAL_NAME(sh_audit_l1_table, 4), [SH_type_fl1_64_shadow] = SHADOW_INTERNAL_NAME(sh_audit_fl1_table, 4), [SH_type_l2_64_shadow] =SHADOW_INTERNAL_NAME (sh_audit_l2_table,4 ), +# ifdef CONFIG_PV32 [SH_type_l2h_64_shadow] = SHADOW_INTERNAL_NAME(sh_audit_l2_table, 4), +# endif [SH_type_l3_64_shadow] = SHADOW_INTERNAL_NAME(sh_audit_l3_table, 4), [SH_type_l4_64_shadow] = SHADOW_INTERNAL_NAME(sh_audit_l4_table, 4), #endif
xen/arch/x86/mm/shadow/hvm.c +1 -1
@@ -56,7 +56,7 @@ const uint8_t sh_type_to_size[] = { [SH_type_l1_64_shadow] = 1, [SH_type_fl1_64_shadow] = 1, [SH_type_l2_64_shadow] = 1, - [SH_type_l2h_64_shadow] = 1, +/* [SH_type_l2h_64_shadow] = 1, PV32-only */ [SH_type_l3_64_shadow] = 1, [SH_type_l4_64_shadow] = 1, [SH_type_p2m_table] = 1,
xen/arch/x86/mm/shadow/multi.c +26 -17
114 lines not on this page · raw message
xen/arch/x86/mm/shadow/private.h +8 -0
@@ -209,6 +209,10 @@ extern void shadow_audit_tables(struct v#define SH_type_unused 10U#endif +#ifndef CONFIG_PV32 /* Unused (but uglier to #ifdef above): */ +#undef SH_type_l2h_64_shadow +#endif +/* * What counts as a pinnable shadow? */
@@ -286,7 +290,11 @@ static inline void sh_terminate_list(str#define SHF_L1_64 (1u << SH_type_l1_64_shadow)#define SHF_FL1_64 (1u << SH_type_fl1_64_shadow)#define SHF_L2_64 (1u << SH_type_l2_64_shadow) +#ifdef CONFIG_PV32 #define SHF_L2H_64 (1u << SH_type_l2h_64_shadow) +#else +#define SHF_L2H_64 0 +#endif #define SHF_L3_64 (1u << SH_type_l3_64_shadow)#define SHF_L4_64 (1u << SH_type_l4_64_shadow)
Re: [PATCH v2 5/9] x86/shadow: L2H shadow type is PV32-only
Quoted 4 lines · Jan Beulich
Like for the various HVM-only types, save a little bit of code by suitably "masking" this type out when !PV32.
Signed-off-by Jan Beulich <[email protected]>
PATCH v2 6/9 x86/shadow: re-work 4-level SHADOW_FOREACH_L2E()
In reply to Jan BeulichFirst of all move the almost loop-invariant condition out of the loop; transform it into an altered loop boundary. Since the new local variable wants to be "unsigned int" and named without violating name space rules, convert the loop induction variable accordingly.
---
v2: New.
xen/arch/x86/mm/shadow/multi.c +11 -14
@@ -863,23 +863,20 @@ do {/* 64-bit l2: touch all entries except for PAE compat guests. */ #define SHADOW_FOREACH_L2E (_sl2mfn, _sl2e, _gl2p, _done, _dom, _code) \ do { \ - int _i; \ - int _xen = !shadow_mode_external(_dom); \ + unsigned int i_, end_ = SHADOW_L2_PAGETABLE_ENTRIES; \ shadow_l2e_t *_sp = map_domain_page((_sl2mfn)); \ ASSERT_VALID_L2(mfn_to_page(_sl2mfn)->u.sh.type); \ - for ( _i = 0; _i < SHADOW_L2_PAGETABLE_ENTRIES; _i++ ) \ + if ( !shadow_mode_external(_dom) && \ + is_pv_32bit_domain(_dom) && \ + mfn_to_page(_sl2mfn)->u.sh.type != SH_type_l2_64_shadow ) \ + end_ = COMPAT_L2_PAGETABLE_FIRST_XEN_SLOT(_dom); \ + for ( i_ = 0; i_ < end_; ++i_ ) \ { \ - if ( (!(_xen)) \ - || !is_pv_32bit_domain(_dom) \ - || mfn_to_page(_sl2mfn)->u.sh.type == SH_type_l2_64_shadow \ - || (_i < COMPAT_L2_PAGETABLE_FIRST_XEN_SLOT(_dom)) ) \ - { \ - (_sl2e) = _sp + _i; \ - if ( shadow_l2e_get_flags(*(_sl2e)) & _PAGE_PRESENT ) \ - {_code} \ - if ( _done ) break; \ - increment_ptr_to_guest_entry(_gl2p); \ - } \ + (_sl2e) = _sp + i_; \ + if ( shadow_l2e_get_flags(*(_sl2e)) & _PAGE_PRESENT ) \ + { _code } \ + if ( _done ) break; \ + increment_ptr_to_guest_entry(_gl2p); \ } \ unmap_domain_page(_sp); \ } while (0)
Re: [PATCH v2 6/9] x86/shadow: re-work 4-level SHADOW_FOREACH_L2E()
Quoted 4 lines · Jan Beulich
First of all move the almost loop-invariant condition out of the loop; transform it into an altered loop boundary. Since the new local variable wants to be "unsigned int" and named without violating name space rules, convert the loop induction variable accordingly.
I'm firmly -1 against using trailing underscores.
Mainly, I object to the attempt to justify doing so based on a rule we explicitly choose to violate for code consistency and legibility reasons.
But in this case, you're taking a block of logic which was cleanly in one style, and making it mixed, even amongst only the local variables.
Quoted 4 lines
xen/arch/x86/mm/shadow/multi.c +0
-0@@ -863,23 +863,20 @@ do {/* 64-bit l2: touch all entries except for PAE compat guests. */
xen/arch/x86/mm/shadow/multi.c do { · line 870
Quoted 10 lines
xen/arch/x86/mm/shadow/multi.c +3
-3#define SHADOW_FOREACH_L2E (_sl2mfn, _sl2e, _gl2p, _done, _dom, _code) \ do { \ - int _i; \ - int _xen = !shadow_mode_external(_dom); \ + unsigned int i_, end_ = SHADOW_L2_PAGETABLE_ENTRIES; \ shadow_l2e_t *_sp = map_domain_page((_sl2mfn)); \ ASSERT_VALID_L2(mfn_to_page(_sl2mfn)->u.sh.type); \ - for ( _i = 0; _i < SHADOW_L2_PAGETABLE_ENTRIES; _i++ ) \ + if ( !shadow_mode_external(_dom) && \ + is_pv_32bit_domain(_dom) && \
The second clause here implies the first. Given that all we're trying to say here is "are there Xen entries to skip", I think we'd be fine dropping the external() check entirely.
Quoted 8 lines
xen/arch/x86/mm/shadow/multi.c +3
-4+ mfn_to_page(_sl2mfn)->u.sh.type != SH_type_l2_64_shadow ) \ + end_ =COMPAT_L2_PAGETABLE_FIRST_XEN_SLOT (_dom); \ +for ( i_ =0 ; i_ < end_; ++i_ ) \ { \ -if ( (!(_xen)) \ - || !is_pv_32bit_domain (_dom) \ - ||mfn_to_page (_sl2mfn)->u.sh.type == SH_type_l2_64_shadow \ - || (_i <COMPAT_L2_PAGETABLE_FIRST_XEN_SLOT (_dom)) ) \
xen/arch/x86/mm/shadow/multi.c do { · line 877
Quoted 10 lines
xen/arch/x86/mm/shadow/multi.c +3
-7- { \ - (_sl2e) = _sp + _i; \ -if (shadow_l2e_get_flags (*(_sl2e)) & _PAGE_PRESENT ) \ - {_code} \ -if ( _done )break ; \ -increment_ptr_to_guest_entry (_gl2p); \ - } \ + (_sl2e) = _sp + i_; \ +if (shadow_l2e_get_flags (*(_sl2e)) & _PAGE_PRESENT ) \ + { _code } \
This doesn't match either of our two styles.
if ( ... )
{ _code }
would be closer to Xen's normal style, but ...
xen/arch/x86/mm/shadow/multi.c do { · line 878
Quoted 1 line
xen/arch/x86/mm/shadow/multi.c +1
-0+if ( _done )break ; \
... with this too, I think it would still be better to write it out fully, so:
if ( ... )
{
_code
}
if ( _done )
break;
These macros are already big enough that trying to save 3 lines seems futile.
~Andrew
xen/arch/x86/mm/shadow/multi.c do { · line 879
Quoted 1 line
xen/arch/x86/mm/shadow/multi.c +1
-0+increment_ptr_to_guest_entry (_gl2p); \
P.S. I'm pretty sure I had encountered this before, but I'm re-reminded of how much this function seems like a bad idea, even in the context of this macroset taking arbitrary _done and _code blobs...
Re: [PATCH v2 6/9] x86/shadow: re-work 4-level SHADOW_FOREACH_L2E()
Quoted 7 lines · Andrew Cooper
Quoted 4 lines · Jan Beulich
First of all move the almost loop-invariant condition out of the loop; transform it into an altered loop boundary. Since the new local variable wants to be "unsigned int" and named without violating name space rules, convert the loop induction variable accordingly.
I'm firmly -1 against using trailing underscores.
Well, I can undo that aspect, but just to get done with the change. I do consider ...
Quoted 2 lines
Mainly, I object to the attempt to justify doing so based on a rule we explicitly choose to violate for code consistency and legibility reasons.
... your view here at least questionable: I'm unaware of us doing so explicitly, and I've pointed out numerous times that the C standard specifies very clearly what underscore-prefixed names may be used for.
Quoted 2 lines
But in this case, you're taking a block of logic which was cleanly in one style, and making it mixed, even amongst only the local variables.
That's simply the result of wanting to limit how much of a change I make to the macro, such that the actual changes are easier to spot.
Quoted 10 lines
Quoted 10 lines
xen/arch/x86/mm/shadow/multi.c +1
-2@@ -863,23 +863,20 @@ do {/* 64-bit l2: touch all entries except for PAE compat guests. */ #define SHADOW_FOREACH_L2E (_sl2mfn, _sl2e, _gl2p, _done, _dom, _code) \ do { \ - int _i; \ - int _xen = !shadow_mode_external(_dom); \ + unsigned int i_, end_ = SHADOW_L2_PAGETABLE_ENTRIES; \ shadow_l2e_t *_sp = map_domain_page((_sl2mfn)); \
Quoted 8 lines
xen/arch/x86/mm/shadow/multi.c do { · line 870
Quoted 4 lines
xen/arch/x86/mm/shadow/multi.c +2
-1ASSERT_VALID_L2 (mfn_to_page (_sl2mfn)->u.sh.type); \ -for ( _i =0 ; _i <SHADOW_L2_PAGETABLE_ENTRIES ; _i++ ) \ + if ( !shadow_mode_external(_dom) && \ + is_pv_32bit_domain(_dom) && \The second clause here implies the first. Given that all we're trying to say here is "are there Xen entries to skip", I think we'd be fine dropping the
external()check entirely.
Will do. I may retain this in some form of comment.
Quoted 19 lines
Quoted 8 lines
xen/arch/x86/mm/shadow/multi.c +3
-4+ mfn_to_page(_sl2mfn)->u.sh.type != SH_type_l2_64_shadow ) \ + end_ =COMPAT_L2_PAGETABLE_FIRST_XEN_SLOT (_dom); \ +for ( i_ =0 ; i_ < end_; ++i_ ) \ { \ -if ( (!(_xen)) \ - || !is_pv_32bit_domain (_dom) \ - ||mfn_to_page (_sl2mfn)->u.sh.type == SH_type_l2_64_shadow \ - || (_i <COMPAT_L2_PAGETABLE_FIRST_XEN_SLOT (_dom)) ) \xen/arch/x86/mm/shadow/multi.c do { · line 877
Quoted 10 lines
xen/arch/x86/mm/shadow/multi.c +3
-7- { \ - (_sl2e) = _sp + _i; \ -if (shadow_l2e_get_flags (*(_sl2e)) & _PAGE_PRESENT ) \ - {_code} \ -if ( _done )break ; \ -increment_ptr_to_guest_entry (_gl2p); \ - } \ + (_sl2e) = _sp + i_; \ +if (shadow_l2e_get_flags (*(_sl2e)) & _PAGE_PRESENT ) \ + { _code } \
Quoted 1 line
This doesn't match either of our two styles.
Indeed, and I was unable to come up with good criteria whether to leave it (for consistency with the other macros) or change it. Since you're
...
Quoted 17 lines
if ( ... )
{_code}would be closer to Xen's normal style, but ...
xen/arch/x86/mm/shadow/multi.c do { · line 878
Quoted 1 line
xen/arch/x86/mm/shadow/multi.c +1
-0+if ( _done )break ; \... with this too, I think it would still be better to write it out fully, so:
if ( ... )
{
_code
}
if (_done)
break;
Quoted 2 lines
These macros are already big enough that trying to save 3 lines seems futile.
... explicitly asking for it, I'll change then. Would you mind if I then also added a semicolon after _code, to make things look more sensible?
Jan
PATCH v2 7/9 x86/shadow: reduce effort of hash calculation
In reply to Jan BeulichThe "n" input is a GFN/MFN value and hence bounded by the physical address bits in use on a system. The hash quality won't improve by also including the upper always-zero bits in the calculation. To keep things as compile-time-constant as they were before, use PADDR_BITS (not paddr_bits) for loop bounding. This reduces loop iterations from 8 to 5.
While there also drop the unnecessary conversion to an array of unsigned char, moving the value off the stack altogether (at least with optimization enabled).
---
I was tempted to also change the type "i" (unsigned) right here, but then thought this might be going too far ...
---
v2: Also eliminate the unsigned char * alias of "n".
xen/arch/x86/mm/shadow/common.c +5 -2
@@ -1397,10 +1397,13 @@ static unsigned int shadow_get_allocatiotypedef u32 key_t ;static inline key_t sh_hash (unsigned long n,unsigned int t) { -unsigned char *p = (unsigned char *)&n;key_t k = t;int i; -for ( i =0 ; i <sizeof (n) ; i++ ) k = (u32 )p[i] + (k<<6 ) + (k<<16 ) - k; + +BUILD_BUG_ON (PADDR_BITS >BITS_PER_LONG +PAGE_SHIFT ); +for ( i =0 ; i < (PADDR_BITS -PAGE_SHIFT +7 ) /8 ; i++, n >>=8 ) + k = (uint8_t )n + (k <<6 ) + (k <<16 ) - k; +return k %SHADOW_HASH_BUCKETS ; }
Re: [PATCH v2 7/9] x86/shadow: reduce effort of hash calculation
Quoted 9 lines · Jan Beulich
The "n" input is a GFN/MFN value and hence bounded by the physical address bits in use on a system. The hash quality won't improve by also including the upper always-zero bits in the calculation. To keep things as compile-time-constant as they were before, use
PADDR_BITS(notpaddr_bits) for loop bounding. This reduces loop iterations from 8 to 5.While there also drop the unnecessary conversion to an array of unsigned char, moving the value off the stack altogether (at least with optimization enabled).
I'm not sure this final bit in brackets is relevant. It wouldn't be on the stack without optimisations either, because ABI-wise, it will be in %rsi.
I'd just drop it.
Quoted 2 lines
Signed-off-by Jan Beulich <[email protected]>
Re: [PATCH v2 7/9] x86/shadow: reduce effort of hash calculation
Quoted 7 lines · Andrew Cooper
Quoted 6 lines · Jan Beulich
The "n" input is a GFN/MFN value and hence bounded by the physical address bits in use on a system. The hash quality won't improve by also including the upper always-zero bits in the calculation. To keep things as compile-time-constant as they were before, use
PADDR_BITS(notpaddr_bits) for loop bounding. This reduces loop iterations from 8 to 5.
Quoted 7 lines
Quoted 3 lines
While there also drop the unnecessary conversion to an array of unsigned char, moving the value off the stack altogether (at least with optimization enabled).
I'm not sure this final bit in brackets is relevant. It wouldn't be on the stack without optimisations either, because ABI-wise, it will be in %rsi.
Without optimization whether an inline function is actually inlined is unclear. When it is, what register (or stack slot) an argument is in is simply unknown. When it is made an out-of-line function, a compiler may very well spill register arguments to the stack first thing, just to make all arguments (whatsoever, i.e. not just in this function) be consistently in memory.
Quoted 3 lines
Quoted 1 line
Signed-off-by Jan Beulich <[email protected]>Reviewed-by Andrew Cooper <[email protected]>
Thanks.
Jan
PATCH v2 8/9 x86/shadow: call sh_detach_old_tables() directly
In reply to Jan BeulichThere's nothing really mode specific in this function anymore (the varying number of valid entries in v->arch.paging.shadow.shadow_table[] is dealt with fine by the zero check, and we have other similar cases of iterating through the full array in common.c), and hence there's neither a need to have multiple instances of it, nor does it need calling through a function pointer.
---
I've retained the C++-style comment in the function as this style is used elsewhere as well in shadow code. I wouldn't mind changing the comment to conform to ./CODING_STYLE.
---
v2: New.
xen/arch/x86/include/asm/paging.h +0 -1
@@ -98,7 +98,6 @@struct shadow_paging_mode {#ifdef CONFIG_SHADOW_PAGING -void (*detach_old_tables )(struct vcpu *v);#ifdef CONFIG_PV void (*write_guest_entry )(struct vcpu *v,intpte_t *p,intpte_t new,mfn_t gmfn);
xen/arch/x86/mm/shadow/common.c +26 -3
@@ -2264,6 +2264,29 @@ void shadow_prepare_page_type_change(strshadow_remove_all_shadows (d,page_to_mfn (page)); } +/* +* Removes v->arch.paging.shadow.shadow_table[]. +* Does all appropriate management/bookkeeping/refcounting/etc... +*/ +static void sh_detach_old_tables (struct vcpu *v) +{ +struct domain *d = v->domain; +unsigned int i; + +//// +//// vcpu->arch.paging.shadow.shadow_table[] +//// + +for ( i =0 ; i <ARRAY_SIZE (v->arch.paging.shadow.shadow_table); ++i ) + { +mfn_t smfn =pagetable_get_mfn (v->arch.paging.shadow.shadow_table[i]); + +if (mfn_x (smfn) ) +sh_put_ref (d, smfn,0 ); + v->arch.paging.shadow.shadow_table[i] =pagetable_null (); + } +} +/**************************************************************************/ static void sh_update_paging_modes (struct vcpu *v)
@@ -2312,7 +2335,7 @@ static void sh_update_paging_modes(struc// First, tear down any old shadow tables held by this vcpu. // if ( v->arch.paging.mode ) - v->arch.paging.mode->shadow.detach_old_tables (v); +sh_detach_old_tables (v);#ifdef CONFIG_HVM if (is_hvm_domain (d) )
@@ -2700,7 +2723,7 @@ void shadow_vcpu_teardown(struct vcpu *vif ( !paging_mode_shadow (d) || !v->arch.paging.mode ) goto out; - v->arch.paging.mode->shadow.detach_old_tables (v); +sh_detach_old_tables (v);#ifdef CONFIG_HVM if (shadow_mode_external (d) ) {
@@ -2935,7 +2958,7 @@ static int shadow_one_bit_disable(structfor_each_vcpu (d, v) {if ( v->arch.paging.mode ) - v->arch.paging.mode->shadow.detach_old_tables (v); +sh_detach_old_tables (v);if ( !(v->arch.flags & TF_kernel_mode) )make_cr3 (v,pagetable_get_mfn (v->arch.guest_table_user));else
xen/arch/x86/mm/shadow/multi.c +0 -25
@@ -3207,30 +3207,6 @@ sh_update_linear_entries(struct vcpu *v)sh_flush_local (d); } - -/* -* Removes v->arch.paging.shadow.shadow_table[]. -* Does all appropriate management/bookkeeping/refcounting/etc... -*/ -static void cf_check sh_detach_old_tables(struct vcpu *v) -{ -struct domain *d = v->domain; -mfn_t smfn; -unsigned int i; - -//// -//// vcpu->arch.paging.shadow.shadow_table[] -//// - -for_each_shadow_table (v, i) - { - smfn =pagetable_get_mfn (v->arch.paging.shadow.shadow_table[i]); -if (mfn_x (smfn) ) -sh_put_ref (d, smfn,0 ); - v->arch.paging.shadow.shadow_table[i] =pagetable_null (); - } -} - static void cf_check sh_update_cr3(struct vcpu *v, int do_locking, bool noflush)/* Updates vcpu->arch.cr3 after the guest has changed CR3. * Paravirtual guests should set v->arch.guest_table (and guest_table_user,
@@ -4211,7 +4187,6 @@ const struct paging_mode sh_paging_mode .update_paging_modes = shadow_update_paging_modes, .flush_tlb = shadow_flush_tlb, .guest_levels =GUEST_PAGING_LEVELS , - .shadow.detach_old_tables = sh_detach_old_tables, #ifdef CONFIG_PV .shadow.write_guest_entry = sh_write_guest_entry, .shadow.cmpxchg_guest_entry = sh_cmpxchg_guest_entry,
xen/arch/x86/mm/shadow/types.h +0 -1
@@ -236,7 +236,6 @@ static inline shadow_l4e_t shadow_l4e_fr#define sh_unhook_pae_mappings INTERNAL_NAME(sh_unhook_pae_mappings)#define sh_unhook_64b_mappings INTERNAL_NAME(sh_unhook_64b_mappings)#define sh_paging_mode INTERNAL_NAME(sh_paging_mode) -#define sh_detach_old_tables INTERNAL_NAME(sh_detach_old_tables)#define sh_audit_l1_table INTERNAL_NAME(sh_audit_l1_table)#define sh_audit_fl1_table INTERNAL_NAME(sh_audit_fl1_table)#define sh_audit_l2_table INTERNAL_NAME(sh_audit_l2_table)
Re: [PATCH v2 8/9] x86/shadow: call sh_detach_old_tables() directly
Quoted 8 lines · Jan Beulich
xen/arch/x86/mm/shadow/common.c +2
-0@@ -2264,6 +2264,29 @@ void shadow_prepare_page_type_change(strshadow_remove_all_shadows (d,page_to_mfn (page)); } +/* +* Removes v->arch.paging.shadow.shadow_table[].
xen/arch/x86/mm/shadow/common.c void shadow_prepare_page_type_change(str · line 2278
Quoted 10 lines
xen/arch/x86/mm/shadow/common.c +10
-0+* Does all appropriate management/bookkeeping/refcounting/etc... +*/ +static void sh_detach_old_tables (struct vcpu *v) +{ +struct domain *d = v->domain; +unsigned int i; + +//// +//// vcpu->arch.paging.shadow.shadow_table[] +////
Honestly, I don't see what the point of this comment is at all. I'd suggest just dropping it as you move the function, which avoids the need to debate over C++ comments.
Preferably with this done, Acked-by: Andrew Cooper <[email protected]>
Re: [PATCH v2 8/9] x86/shadow: call sh_detach_old_tables() directly
Quoted 7 lines · Andrew Cooper
Quoted 6 lines · Jan Beulich
xen/arch/x86/mm/shadow/common.c +0
-0@@ -2264,6 +2264,29 @@ void shadow_prepare_page_type_change(strshadow_remove_all_shadows (d,page_to_mfn (page)); }
Quoted 16 lines
xen/arch/x86/mm/shadow/common.c void shadow_prepare_page_type_change(str · line 2278
Quoted 12 lines
xen/arch/x86/mm/shadow/common.c +12
-0+/* +* Removes v->arch.paging.shadow.shadow_table[]. +* Does all appropriate management/bookkeeping/refcounting/etc... +*/ +static void sh_detach_old_tables (struct vcpu *v) +{ +struct domain *d = v->domain; +unsigned int i; + +//// +//// vcpu->arch.paging.shadow.shadow_table[] +//// Honestly, I don't see what the point of this comment is at all. I'd suggest just dropping it as you move the function, which avoids the need to debate over C++ comments.
As said in the remark, this style of comments is used elsewhere as well, to indicate what data structure a certain piece of code in a function is updating. Earlier on the function here also played with vcpu->arch.paging.shadow.guest_vtable, at which point having such comments was certainly not entirely useless.
Quoted 2 lines
Preferably with this done, Acked-by: Andrew Cooper <[email protected]>
Thanks. I guess I'll drop it then; should the function become more involved again, we could clearly resurrect comments in whatever shape is then deemed best.
Jan
PATCH v2 9/9 x86/shadow: harden shadow_size()
In reply to Jan BeulichMake HVM=y release build behavior prone against array overrun, by (ab)using array_access_nospec(). This is in particular to guard against e.g. SH_type_unused making it here unintentionally.
---
v2: New.
xen/arch/x86/mm/shadow/private.h +2 -1
@@ -27,6 +27,7 @@// been included... #include <asm/page.h> #include <xen/domain_page.h> +#include <xen/nospec.h> #include <asm/x86_emulate.h> #include <asm/hvm/support.h> #include <asm/atomic.h>
@@ -368,7 +369,7 @@ shadow_size(unsigned int shadow_type) {#ifdef CONFIG_HVM ASSERT (shadow_type <ARRAY_SIZE (sh_type_to_size)); -return sh_type_to_size[shadow_type]; +return array_access_nospec (sh_type_to_size, shadow_type);#else ASSERT (shadow_type < SH_type_unused);return shadow_type != SH_type_none;
Re: [PATCH v2 9/9] x86/shadow: harden shadow_size()
Quoted 19 lines · Jan Beulich
Make HVM=y release build behavior prone against array overrun, by (ab)using
array_access_nospec(). This is in particular to guard against e.g.SH_type_unusedmaking it here unintentionally.Signed-off-by Jan Beulich <[email protected]>---
v2: New.xen/arch/x86/mm/shadow/private.h +1
-0@@ -27,6 +27,7 @@// been included... #include <asm/page.h> #include <xen/domain_page.h> +#include <xen/nospec.h> #include <asm/x86_emulate.h> #include <asm/hvm/support.h> #include <asm/atomic.h>
xen/arch/x86/mm/shadow/private.h shadow_size(unsigned int shadow_type) · line 372
Quoted 5 lines
xen/arch/x86/mm/shadow/private.h +1
-1@@ -368,7 +369,7 @@ shadow_size(unsigned int shadow_type) {#ifdef CONFIG_HVM ASSERT (shadow_type <ARRAY_SIZE (sh_type_to_size)); -return sh_type_to_size[shadow_type]; +return array_access_nospec (sh_type_to_size, shadow_type);
I don't think this is warranted.
First, if the commit message were accurate, then it's a problem for all arrays of size SH_type_unused, yet you've only adjusted a single instance.
Secondly, if it were reliably 16 then we could do the basically-free "type &= 15;" modification. (It appears my change to do this automatically hasn't been taken yet.), but we'll end up with lfence variation here.
But the value isn't attacker controlled. shadow_type always comes from Xen's metadata about the guest, not the guest itself. So I don't see how this can conceivably be a speculative issue even in principle.
~Andrew
Re: [PATCH v2 9/9] x86/shadow: harden shadow_size()
Quoted 28 lines · Andrew Cooper
Quoted 19 lines · Jan Beulich
Make HVM=y release build behavior prone against array overrun, by (ab)using
array_access_nospec(). This is in particular to guard against e.g.SH_type_unusedmaking it here unintentionally.Signed-off-by Jan Beulich <[email protected]>---
v2: New.xen/arch/x86/mm/shadow/private.h +1
-0@@ -27,6 +27,7 @@// been included... #include <asm/page.h> #include <xen/domain_page.h> +#include <xen/nospec.h> #include <asm/x86_emulate.h> #include <asm/hvm/support.h> #include <asm/atomic.h> xen/arch/x86/mm/shadow/private.h shadow_size(unsigned int shadow_type) · line 372
Quoted 5 lines
xen/arch/x86/mm/shadow/private.h +1
-1@@ -368,7 +369,7 @@ shadow_size(unsigned int shadow_type) {#ifdef CONFIG_HVM ASSERT (shadow_type <ARRAY_SIZE (sh_type_to_size)); -return sh_type_to_size[shadow_type]; +return array_access_nospec (sh_type_to_size, shadow_type);I don't think this is warranted.
Quoted 2 lines
First, if the commit message were accurate, then it's a problem for all arrays of size
SH_type_unused, yet you've only adjusted a single instance.
Because I think the risk is higher here than for other arrays. In other cases we have suitable build-time checks (HASH_CALLBACKS_CHECK() in particular) which would trip upon inappropriate use of one of the types which are aliased to SH_type_unused when !HVM.
Quoted 4 lines
Secondly, if it were reliably 16 then we could do the basically-free "type &= 15;" modification. (It appears my change to do this automatically hasn't been taken yet.), but we'll end up with lfence variation here.
How could anything be "reliably 16"? Such enums can change at any time:
They did when making HVM types conditional, and they will again when adding types needed for 5-level paging.
Quoted 3 lines
But the value isn't attacker controlled.
shadow_typealways comes from Xen's metadata about the guest, not the guest itself. So I don't see how this can conceivably be a speculative issue even in principle.
I didn't say anything about there being a speculative issue here. It is for this very reason that I wrote "(ab)using".
Jan
Re: [PATCH v2 9/9] x86/shadow: harden shadow_size()
Quoted 43 lines · Jan Beulich
Quoted 27 lines · Andrew Cooper
Quoted 19 lines · Jan Beulich
Make HVM=y release build behavior prone against array overrun, by (ab)using
array_access_nospec(). This is in particular to guard against e.g.SH_type_unusedmaking it here unintentionally.Signed-off-by Jan Beulich <[email protected]>---
v2: New.xen/arch/x86/mm/shadow/private.h +1
-0@@ -27,6 +27,7 @@// been included... #include <asm/page.h> #include <xen/domain_page.h> +#include <xen/nospec.h> #include <asm/x86_emulate.h> #include <asm/hvm/support.h> #include <asm/atomic.h> xen/arch/x86/mm/shadow/private.h shadow_size(unsigned int shadow_type) · line 372
Quoted 5 lines
xen/arch/x86/mm/shadow/private.h +1
-1@@ -368,7 +369,7 @@ shadow_size(unsigned int shadow_type) {#ifdef CONFIG_HVM ASSERT (shadow_type <ARRAY_SIZE (sh_type_to_size)); -return sh_type_to_size[shadow_type]; +return array_access_nospec (sh_type_to_size, shadow_type);I don't think this is warranted.
Quoted 2 lines
First, if the commit message were accurate, then it's a problem for all arrays of size
SH_type_unused, yet you've only adjusted a single instance.Because I think the risk is higher here than for other arrays. In other cases we have suitable build-time checks (
HASH_CALLBACKS_CHECK()in particular) which would trip upon inappropriate use of one of the types which are aliased toSH_type_unusedwhen !HVM.Quoted 4 lines
Secondly, if it were reliably 16 then we could do the basically-free "type &= 15;" modification. (It appears my change to do this automatically hasn't been taken yet.), but we'll end up with lfence variation here.
How could anything be "reliably 16"? Such enums can change at any time:
They did when making HVM types conditional, and they will again when adding types needed for 5-level paging.
Quoted 5 lines
Quoted 3 lines
But the value isn't attacker controlled.
shadow_typealways comes from Xen's metadata about the guest, not the guest itself. So I don't see how this can conceivably be a speculative issue even in principle.I didn't say anything about there being a speculative issue here. It is for this very reason that I wrote "(ab)using".
Then it is entirely wrong to be using a nospec accessor.
Speculation problems are subtle enough, without false uses of the safety helpers.
If you want to "harden" against runtime architectural errors, you want to up the ASSERT() to a BUG(), which will execute faster than sticking an hiding an lfence in here, and not hide what is obviously a major malfunction in the shadow pagetable logic.
~Andrew
Re: [PATCH v2 9/9] x86/shadow: harden shadow_size()
Quoted 52 lines · Andrew Cooper
Quoted 43 lines · Jan Beulich
Quoted 27 lines · Andrew Cooper
Quoted 19 lines · Jan Beulich
Make HVM=y release build behavior prone against array overrun, by (ab)using
array_access_nospec(). This is in particular to guard against e.g.SH_type_unusedmaking it here unintentionally.Signed-off-by Jan Beulich <[email protected]>---
v2: New.xen/arch/x86/mm/shadow/private.h +1
-0@@ -27,6 +27,7 @@// been included... #include <asm/page.h> #include <xen/domain_page.h> +#include <xen/nospec.h> #include <asm/x86_emulate.h> #include <asm/hvm/support.h> #include <asm/atomic.h> xen/arch/x86/mm/shadow/private.h shadow_size(unsigned int shadow_type) · line 372
Quoted 5 lines
xen/arch/x86/mm/shadow/private.h +1
-1@@ -368,7 +369,7 @@ shadow_size(unsigned int shadow_type) {#ifdef CONFIG_HVM ASSERT (shadow_type <ARRAY_SIZE (sh_type_to_size)); -return sh_type_to_size[shadow_type]; +return array_access_nospec (sh_type_to_size, shadow_type);I don't think this is warranted.
Quoted 2 lines
First, if the commit message were accurate, then it's a problem for all arrays of size
SH_type_unused, yet you've only adjusted a single instance.Because I think the risk is higher here than for other arrays. In other cases we have suitable build-time checks (
HASH_CALLBACKS_CHECK()in particular) which would trip upon inappropriate use of one of the types which are aliased toSH_type_unusedwhen !HVM.Quoted 4 lines
Secondly, if it were reliably 16 then we could do the basically-free "type &= 15;" modification. (It appears my change to do this automatically hasn't been taken yet.), but we'll end up with lfence variation here.
How could anything be "reliably 16"? Such enums can change at any time:
They did when making HVM types conditional, and they will again when adding types needed for 5-level paging.Quoted 5 lines
Quoted 3 lines
But the value isn't attacker controlled.
shadow_typealways comes from Xen's metadata about the guest, not the guest itself. So I don't see how this can conceivably be a speculative issue even in principle.I didn't say anything about there being a speculative issue here. It is for this very reason that I wrote "(ab)using".
Then it is entirely wrong to be using a nospec accessor.
Quoted 7 lines
Speculation problems are subtle enough, without false uses of the safety helpers.
If you want to "harden" against runtime architectural errors, you want to up the
ASSERT()to aBUG(), which will execute faster than sticking an hiding an lfence in here, and not hide what is obviously a major malfunction in the shadow pagetable logic.
I should have commented on this earlier already: What lfence are you talking about? As to BUG() - the goal here specifically is to avoid a crash in release builds, by forcing the function to return zero (via having it use array slot zero for out of range indexes).
Jan
Re: [PATCH v2 9/9] x86/shadow: harden shadow_size()
Quoted 55 lines · Jan Beulich
Quoted 54 lines · Andrew Cooper
Quoted 43 lines · Jan Beulich
Quoted 27 lines · Andrew Cooper
Quoted 19 lines · Jan Beulich
Make HVM=y release build behavior prone against array overrun, by (ab)using
array_access_nospec(). This is in particular to guard against e.g.SH_type_unusedmaking it here unintentionally.Signed-off-by Jan Beulich <[email protected]>---
v2: New.xen/arch/x86/mm/shadow/private.h +1
-0@@ -27,6 +27,7 @@// been included... #include <asm/page.h> #include <xen/domain_page.h> +#include <xen/nospec.h> #include <asm/x86_emulate.h> #include <asm/hvm/support.h> #include <asm/atomic.h> xen/arch/x86/mm/shadow/private.h shadow_size(unsigned int shadow_type) · line 372
Quoted 5 lines
xen/arch/x86/mm/shadow/private.h +1
-1@@ -368,7 +369,7 @@ shadow_size(unsigned int shadow_type) {#ifdef CONFIG_HVM ASSERT (shadow_type <ARRAY_SIZE (sh_type_to_size)); -return sh_type_to_size[shadow_type]; +return array_access_nospec (sh_type_to_size, shadow_type);I don't think this is warranted.
Quoted 2 lines
First, if the commit message were accurate, then it's a problem for all arrays of size
SH_type_unused, yet you've only adjusted a single instance.Because I think the risk is higher here than for other arrays. In other cases we have suitable build-time checks (
HASH_CALLBACKS_CHECK()in particular) which would trip upon inappropriate use of one of the types which are aliased toSH_type_unusedwhen !HVM.Quoted 4 lines
Secondly, if it were reliably 16 then we could do the basically-free "type &= 15;" modification. (It appears my change to do this automatically hasn't been taken yet.), but we'll end up with lfence variation here.
How could anything be "reliably 16"? Such enums can change at any time:
They did when making HVM types conditional, and they will again when adding types needed for 5-level paging.Quoted 5 lines
Quoted 3 lines
But the value isn't attacker controlled.
shadow_typealways comes from Xen's metadata about the guest, not the guest itself. So I don't see how this can conceivably be a speculative issue even in principle.I didn't say anything about there being a speculative issue here. It is for this very reason that I wrote "(ab)using".
Then it is entirely wrong to be using a nospec accessor.
Speculation problems are subtle enough, without false uses of the safety helpers.
Quoted 6 lines
Quoted 4 lines
If you want to "harden" against runtime architectural errors, you want to up the
ASSERT()to aBUG(), which will execute faster than sticking an hiding an lfence in here, and not hide what is obviously a major malfunction in the shadow pagetable logic.I should have commented on this earlier already: What lfence are you talking about?
The one I thought was hiding under array_access_nospec(), but I forgot we'd done the sbb trick.
Quoted 3 lines
As to
BUG()- the goal here specifically is to avoid a crash in release builds, by forcing the function to return zero (via having it use array slot zero for out of range indexes).
I'm very uneasy about having something this deep inside a component, which ASSERT()s correctness doing something custom like this "just to avoid crashing".
There is either something important which makes this more likely than most to go wrong at runtime, or there is not. And honestly, I can't see why it is more risky at runtime.
If we really do need to clamp it, then we need a brand new helper with a name that doesn't reference speculation at all. It's fine for *_nospec to reuse this helper, stating the safety of doing so, but at a code level there need to be two appropriately named helpers for their two logically-unrelated purposes.
~Andrew
Re: [PATCH v2 9/9] x86/shadow: harden shadow_size()
Quoted 70 lines · Andrew Cooper
Quoted 55 lines · Jan Beulich
Quoted 54 lines · Andrew Cooper
Quoted 43 lines · Jan Beulich
Quoted 27 lines · Andrew Cooper
Quoted 19 lines · Jan Beulich
Make HVM=y release build behavior prone against array overrun, by (ab)using
array_access_nospec(). This is in particular to guard against e.g.SH_type_unusedmaking it here unintentionally.Signed-off-by Jan Beulich <[email protected]>---
v2: New.xen/arch/x86/mm/shadow/private.h +1
-0@@ -27,6 +27,7 @@// been included... #include <asm/page.h> #include <xen/domain_page.h> +#include <xen/nospec.h> #include <asm/x86_emulate.h> #include <asm/hvm/support.h> #include <asm/atomic.h> xen/arch/x86/mm/shadow/private.h shadow_size(unsigned int shadow_type) · line 372
Quoted 5 lines
xen/arch/x86/mm/shadow/private.h +1
-1@@ -368,7 +369,7 @@ shadow_size(unsigned int shadow_type) {#ifdef CONFIG_HVM ASSERT (shadow_type <ARRAY_SIZE (sh_type_to_size)); -return sh_type_to_size[shadow_type]; +return array_access_nospec (sh_type_to_size, shadow_type);I don't think this is warranted.
Quoted 2 lines
First, if the commit message were accurate, then it's a problem for all arrays of size
SH_type_unused, yet you've only adjusted a single instance.Because I think the risk is higher here than for other arrays. In other cases we have suitable build-time checks (
HASH_CALLBACKS_CHECK()in particular) which would trip upon inappropriate use of one of the types which are aliased toSH_type_unusedwhen !HVM.Quoted 4 lines
Secondly, if it were reliably 16 then we could do the basically-free "type &= 15;" modification. (It appears my change to do this automatically hasn't been taken yet.), but we'll end up with lfence variation here.
How could anything be "reliably 16"? Such enums can change at any time:
They did when making HVM types conditional, and they will again when adding types needed for 5-level paging.Quoted 5 lines
Quoted 3 lines
But the value isn't attacker controlled.
shadow_typealways comes from Xen's metadata about the guest, not the guest itself. So I don't see how this can conceivably be a speculative issue even in principle.I didn't say anything about there being a speculative issue here. It is for this very reason that I wrote "(ab)using".
Then it is entirely wrong to be using a nospec accessor.
Speculation problems are subtle enough, without false uses of the safety helpers.
Quoted 6 lines
Quoted 4 lines
If you want to "harden" against runtime architectural errors, you want to up the
ASSERT()to aBUG(), which will execute faster than sticking an hiding an lfence in here, and not hide what is obviously a major malfunction in the shadow pagetable logic.I should have commented on this earlier already: What lfence are you talking about?
The one I thought was hiding under
array_access_nospec(), but I forgot we'd done the sbb trick.Quoted 3 lines
As to
BUG()- the goal here specifically is to avoid a crash in release builds, by forcing the function to return zero (via having it use array slot zero for out of range indexes).
Quoted 7 lines
I'm very uneasy about having something this deep inside a component, which ASSERT()s correctness doing something custom like this "just to avoid crashing".
There is either something important which makes this more likely than most to go wrong at runtime, or there is not. And honestly, I can't see why it is more risky at runtime.
Well, okay. I did explain why I think there is an increased risk here.
Quoted 5 lines
If we really do need to clamp it, then we need a brand new helper with a name that doesn't reference speculation at all. It's fine for
*_nospecto reuse this helper, stating the safety of doing so, but at a code level there need to be two appropriately named helpers for their two logically-unrelated purposes.
I don't think anything can sensibly be made for more general purpose (not speculation related) use. Here I'm specifically utilizing that array slot 0 is being picked as the fallback slot _and_ that slot is actually suitable. This may not be the case for other arrays.
Anyway - taking things together I will then simply consider the patch rejected, despite it being a seemingly easy step of hardening.
Jan
Re: [PATCH v2 9/9] x86/shadow: harden shadow_size()
Quoted 83 lines · Jan Beulich
Quoted 68 lines · Andrew Cooper
Quoted 55 lines · Jan Beulich
Quoted 54 lines · Andrew Cooper
Quoted 43 lines · Jan Beulich
Quoted 27 lines · Andrew Cooper
Quoted 19 lines · Jan Beulich
Make HVM=y release build behavior prone against array overrun, by (ab)using
array_access_nospec(). This is in particular to guard against e.g.SH_type_unusedmaking it here unintentionally.Signed-off-by Jan Beulich <[email protected]>---
v2: New.xen/arch/x86/mm/shadow/private.h +1
-0@@ -27,6 +27,7 @@// been included... #include <asm/page.h> #include <xen/domain_page.h> +#include <xen/nospec.h> #include <asm/x86_emulate.h> #include <asm/hvm/support.h> #include <asm/atomic.h> xen/arch/x86/mm/shadow/private.h shadow_size(unsigned int shadow_type) · line 372
Quoted 5 lines
xen/arch/x86/mm/shadow/private.h +1
-1@@ -368,7 +369,7 @@ shadow_size(unsigned int shadow_type) {#ifdef CONFIG_HVM ASSERT (shadow_type <ARRAY_SIZE (sh_type_to_size)); -return sh_type_to_size[shadow_type]; +return array_access_nospec (sh_type_to_size, shadow_type);I don't think this is warranted.
Quoted 2 lines
First, if the commit message were accurate, then it's a problem for all arrays of size
SH_type_unused, yet you've only adjusted a single instance.Because I think the risk is higher here than for other arrays. In other cases we have suitable build-time checks (
HASH_CALLBACKS_CHECK()in particular) which would trip upon inappropriate use of one of the types which are aliased toSH_type_unusedwhen !HVM.Quoted 4 lines
Secondly, if it were reliably 16 then we could do the basically-free "type &= 15;" modification. (It appears my change to do this automatically hasn't been taken yet.), but we'll end up with lfence variation here.
How could anything be "reliably 16"? Such enums can change at any time:
They did when making HVM types conditional, and they will again when adding types needed for 5-level paging.Quoted 5 lines
Quoted 3 lines
But the value isn't attacker controlled.
shadow_typealways comes from Xen's metadata about the guest, not the guest itself. So I don't see how this can conceivably be a speculative issue even in principle.I didn't say anything about there being a speculative issue here. It is for this very reason that I wrote "(ab)using".
Then it is entirely wrong to be using a nospec accessor.
Speculation problems are subtle enough, without false uses of the safety helpers.
Quoted 6 lines
Quoted 4 lines
If you want to "harden" against runtime architectural errors, you want to up the
ASSERT()to aBUG(), which will execute faster than sticking an hiding an lfence in here, and not hide what is obviously a major malfunction in the shadow pagetable logic.I should have commented on this earlier already: What lfence are you talking about?
The one I thought was hiding under
array_access_nospec(), but I forgot we'd done the sbb trick.Quoted 3 lines
As to
BUG()- the goal here specifically is to avoid a crash in release builds, by forcing the function to return zero (via having it use array slot zero for out of range indexes).Quoted 7 lines
I'm very uneasy about having something this deep inside a component, which ASSERT()s correctness doing something custom like this "just to avoid crashing".
There is either something important which makes this more likely than most to go wrong at runtime, or there is not. And honestly, I can't see why it is more risky at runtime.
Well, okay. I did explain why I think there is an increased risk here.
Quoted 5 lines
If we really do need to clamp it, then we need a brand new helper with a name that doesn't reference speculation at all. It's fine for
*_nospecto reuse this helper, stating the safety of doing so, but at a code level there need to be two appropriately named helpers for their two logically-unrelated purposes.
Quoted 7 lines
I don't think anything can sensibly be made for more general purpose (not speculation related) use. Here I'm specifically utilizing that array slot 0 is being picked as the fallback slot _and_ that slot is actually suitable. This may not be the case for other arrays.
Anyway - taking things together I will then simply consider the patch rejected, despite it being a seemingly easy step of hardening.
I don't have a problem, in principle, with something like array_clamp(arr, idx) as long as it comes with a API description making it clear that it turns out-of-bounds indices into 0.
But use of this in code also needs to come with a comment explaining why the piece of code is risky enough to justify it. (As much as anything else, so we can figure out when to remove it if the preconditions change.)
~Andrew