x86/shadow: original work for what became XSA-495
v15 cover x86/shadow: original work for what became XSA-495
Work was partially done in an attempt to correct behavior rather than deprecating shadow mode, but the part yet to be done were evaluated as too intrusive / involved for a component that was consider to deprecate already before. Nevertheless I'm submitting this, to consider whether to still commit some of it. Some parts are ack-ed, so the ones at the beginning of the series could in principle go in right away. Yet I think I'll leave some time to determine whether to put in any of this in the first place.
01: shadow: add preemption support to shadow_blow_tables() 02: shadow: blow away tables first when tearing down
03: shadow: 1-bit-disable doesn't need to detach old tables 04: shadow: reduce amount of work to do by shadow_unhook_mappings() 05: shadow: make clean-dirty-bitmap post-processing preemptable 06: mm: make more of log-dirty mode enable/disable preemptable 07: shadow: fold _shadow_prealloc() with shadow_blow_tables() 08: shadow: make log-dirty mode enable/disable properly preemptable 09: shadow: make forced (L1TF) mode enable properly preemptable 10: shadow: limit the number of pages which may be in use as shadows
Jan
PATCH v15 1/10 x86/shadow: add preemption support to shadow_blow_tables()
From: Roger Pau Monné <[email protected]>
The use of shadow_blow_tables() in the domain teardown path (added for XSA-410) actually requires preemption support itself for security reasons, specially as the pages freed by shadow_blow_tables() are not returned to the shadow page pool, but instead freed to the hypervisor.
Note that we require flushing the TLB when a need for preemption arises on the 2nd pass, or else we risk returning to guest context with stale TLB entries. (There's no similar need on the 1st pass, as per _shadow_prealloc().) While preemption will be enabled only when tearing down domains (and hence flushing has become meaningless by that point), keep the function structured to no bypass the flush, just in case.
---
Changes since v12:
- Re-base.
Changes since v11:
- Re-base in particular past the XSA-410 series.
Changes since v9:
- Extend a comment. Commit message adjustments.
Changes since v8:
- Use difference, not sum, of total and free pages to determine whether progress was made.
- Check for preemption on every iteration of the 2nd pass, as long as some progress was made.
- Don't "goto out" on the 1st pass, to skip the TLB flush.
Changes since v6:
- Rearrange order of preemption condition checks.
- Only avoid shadow_blow_tables() when the domain is dead (ie: all vCPUs are stopped).
Changes since v5:
- Add comment, remove no functional change.
- Do not check for preemption on every loop.
Changes since v4:
- New in this version.
xen/arch/x86/mm/shadow/common.c +47 -5
@@ -454,12 +454,23 @@ bool shadow_prealloc(struct domain *d, u/* Deliberately free all the memory we can: this will tear down all of * this domain's shadows */ -void shadow_blow_tables (struct domain *d) +void shadow_blow_tables (struct domain *d,bool *preempted) {struct page_info *sp, *t;struct vcpu *v;mfn_t smfn;int i; +unsigned int done =0 ; + +/* +* When the domain is dying a call to shadow_blow_tables() will be +* performed from the teardown path with preemption support, ignore any +* other calls as we want to do the final teardown with preemption support. +* Teardown of shadow related data can only be avoided when all domain +* vCPUs are stopped. +*/ +if (unlikely (d->is_dying) && !preempted ) +return ;/* Nothing to do when there are no vcpus yet. */ if ( !d->vcpu[0 ] )
@@ -470,17 +481,46 @@ void shadow_blow_tables(struct domain *d { smfn =page_to_mfn (sp);sh_unpin (d, smfn); +if ( preempted && !(++done &0xff ) &&general_preempt_check () ) + { + *preempted = true; +return ; + } }/* Second pass: unhook entries of in-use shadows */ for_each_vcpu (d, v)for ( i =0 ; i <ARRAY_SIZE (v->arch.paging.shadow.shadow_table); i++ )if ( !pagetable_is_null (v->arch.paging.shadow.shadow_table[i]) ) + { +unsigned int num = d->arch.paging.total_pages - + d->arch.paging.free_pages; +shadow_unhook_mappings ( d,pagetable_get_mfn (v->arch.paging.shadow.shadow_table[i]),0 ); +/* +* Make sure we are making progress before yielding: if domain +* is dying progress will be seen by total_pages decreasing, if +* not dying free_pages will increase. In any case the gap +* between both will shrink. +* +* Note that with the paging lock held the values used in the +* calculation are safe from being altered by other hypercalls. +*/ +if ( preempted && + (num != d->arch.paging.total_pages - + d->arch.paging.free_pages) && +general_preempt_check () ) + { + *preempted = true; + goto out; + } + } + + out:/* Make sure everyone sees the unshadowings */ guest_flush_tlb_mask (d, d->dirty_cpumask); }
@@ -490,7 +530,7 @@ void shadow_blow_tables_per_domain(strucif (shadow_mode_enabled (d) &&domain_vcpu (d,0 ) ) {paging_lock (d); -shadow_blow_tables (d); +shadow_blow_tables (d,NULL );paging_unlock (d); } }
@@ -2254,7 +2294,9 @@ void shadow_teardown(struct domain *d, b* in-use pages, as _shadow_prealloc() will no longer try to reclaim pages * because the domain is dying. */ -shadow_blow_tables (d); +shadow_blow_tables (d, preempted); +if ( preempted && *preempted ) + goto out;#if (SHADOW_OPTIMIZATIONS & (SHOPT_VIRTUAL_TLB |SHOPT_OUT_OF_SYNC ))/* Free the virtual-TLB array attached to each vcpu */
@@ -2492,7 +2534,7 @@ static int cf_check sh_enable_log_dirty(/* This domain already has some shadows: need to clear them out * of the way to make sure that all references to guest memory are * properly write-protected */ -shadow_blow_tables (d); +shadow_blow_tables (d,NULL ); }#if (SHADOW_OPTIMIZATIONS &SHOPT_LINUX_L3_TOPLEVEL )
@@ -2530,7 +2572,7 @@ static void cf_check sh_clean_dirty_bitm/* Need to revoke write access to the domain's pages again. * In future, we'll have a less heavy-handed approach to this, * but for now, we just unshadow everything except Xen. */ -shadow_blow_tables (d); +shadow_blow_tables (d,NULL );paging_unlock (d); }
xen/arch/x86/mm/shadow/hvm.c +2 -2
@@ -979,7 +979,7 @@ sh_write_p2m_entry_post(struct p2m_domaiagain), so it doesn't matter too much. */ if ( d->arch.paging.shadow.has_fast_mmio_entries ) { -shadow_blow_tables (d); +shadow_blow_tables (d,NULL ); d->arch.paging.shadow.has_fast_mmio_entries = false; } }
@@ -1049,7 +1049,7 @@ int shadow_track_dirty_vram(struct domai* Throw away all the shadows rather than walking through them * up to nr times getting rid of mappings of each pfn. */ -shadow_blow_tables (d); +shadow_blow_tables (d,NULL );gdprintk (XENLOG_INFO ,"tracking VRAM %lx - %lx\n" , begin_pfn, end_pfn);
xen/arch/x86/mm/shadow/private.h +1 -1
@@ -471,7 +471,7 @@ mfn_t oos_snapshot_lookup(struct domain#endif /* (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC) */ /* Deliberately free all the memory we can: tear down all of d's shadows. */ -void shadow_blow_tables (struct domain *d); +void shadow_blow_tables (struct domain *d,bool *preempted);/* * Remove all mappings of a guest frame from the shadow tables.
PATCH v15 2/10 x86/shadow: blow away tables first when tearing down
In reply to Jan BeulichWith shadow_blow_tables() but not sh_detach_old_tables() (called by shadow_vcpu_teardown()) being preemptable, move the invocation of the former ahead. This way future finer grained preemption checks applied to shadow_blow_tables() will further benefit teardown behavior.
---
v13: New.
xen/arch/x86/mm/shadow/common.c +11 -9
@@ -2279,6 +2279,17 @@ void shadow_teardown(struct domain *d, bASSERT (d->is_dying);ASSERT (d != current->domain); +/* +* Reclaim all shadow memory so that shadow_set_allocation() doesn't find +* in-use pages, as _shadow_prealloc() will no longer try to reclaim pages +* because the domain is dying. +*/ +paging_lock (d); +shadow_blow_tables (d, preempted); +paging_unlock (d); +if ( preempted && *preempted ) +return ; +/* TODO - Remove when the teardown path is better structured. */ for_each_vcpu ( d, v )shadow_vcpu_teardown (v);
@@ -2289,15 +2300,6 @@ void shadow_teardown(struct domain *d, bpaging_lock (d); -/* -* Reclaim all shadow memory so that shadow_set_allocation() doesn't find -* in-use pages, as _shadow_prealloc() will no longer try to reclaim pages -* because the domain is dying. -*/ -shadow_blow_tables (d, preempted); -if ( preempted && *preempted ) - goto out; -#if (SHADOW_OPTIMIZATIONS & (SHOPT_VIRTUAL_TLB |SHOPT_OUT_OF_SYNC ))/* Free the virtual-TLB array attached to each vcpu */ for_each_vcpu (d, v)
PATCH v15 3/10 x86/shadow: 1-bit-disable doesn't need to detach old tables
In reply to Jan BeulichJust ahead of the loop being modified sh_new_mode() is called, which in turn calls sh_update_paging_modes() and thus sh_detach_old_tables(). To retain the intended effect of not leaving any shadows in place, avoid re-establishing a new top-level shadow from sh_update_paging_modes().
---
This doesn't need to be part of the XSA, but it eliminates one point of concern wrt preemption checking. However, in the context of "x86/mm:
make more of log-dirty mode enable/disable preemptable" I started wondering whether things don't want doing differently. 1-bit-enables technically don't need to purge all shadows:
- log-dirty enable only needs to get rid of all L1 entries; this could be done via hash lookup (of all L1/FL1 shadows) instead of shadow_blow_tables(),
- test enable doesn't need any purging; it may be the intention though that it does a certain amount of purging,
- force enable doesn't need to do any purging either.
We could therefore pass a new boolean through sh_new_mode() to sh_update_paging_modes() to suppress the call to sh_detach_old_tables() for some (all?) of the 1-bit enables.
1-bit disables (log-dirty as well as test) match the above, but of course they will need to (at least) detach all tables when shadow mode is being turned off altogether (i.e. the very invocation that's being deleted here). As done in the other patch, the amount of work to do by the (then possibly retained) sh_detach_old_tables() here could be bounded by "preparatory" purging (in a properly preemptable way) in shadow_one_bit_disable() or its callers. Using shadow_blow_tables() for that purpose is probably preferable anyway, as sh_detach_old_tables() alone won't get rid of pinned shadows.
---
v15: Re-base.
v13: New.
xen/arch/x86/mm/shadow/common.c +2 -3
@@ -1982,7 +1982,8 @@ static void sh_update_paging_modes(struc }#endif /* OOS */ - v->arch.paging.mode->update_cr3 (v, false); +if (paging_mode_enabled (d) ) + v->arch.paging.mode->update_cr3 (v, false); }/*
@@ -2459,8 +2460,6 @@ static int shadow_one_bit_disable(struct d->arch.paging.free_pages, d->arch.paging.p2m_pages);for_each_vcpu (d, v) { -if ( v->arch.paging.mode ) -sh_detach_old_tables (v);if ( !(v->arch.flags & TF_kernel_mode) )make_cr3 (v,pagetable_get_mfn (v->arch.guest_table_user));else
PATCH v15 4/10 x86/shadow: reduce amount of work to do by shadow_unhook_mappings()
In reply to Jan BeulichWhen preemption is enabled for uses of shadow_blow_tables(), the granularity of preemption checks in both of the present two passes can be pretty coarse, as deep table hierarchies may need processing. Reduce shadow page table depth up front by introducing a third (earlier) pass processing first L2, then L3 tables by doing hash lookups instead of tree traversal.
---
The way need for preemption is being checked for is crude, but the least intrusive variant I could think of while still respecting hash_foreach() not permitting further traversal after a removal from the hash.
I've deliberately chosen 0 as the "wildcard", as using INVALID_MFN looks somewhat more fragile/risky to me. Of course we might consider introducing WILDCARD_MFN, which then could be non-zero but still distinct from INVALID_MFN.
I've further deliberately made hash_foreach() return "int", not "bool", since at least transiently I was also playing with returning -ERESTART from some of the callback functions.
We could avoid the hash walk for guests which never entered 64-bit mode. That would require tracking the maximum shadow paging level that was ever used (perhaps since the last [completed] shadow_blow_tables()) by a domain. (This would similarly apply to future 5-level support, where we could avoid the SHF_L4_ANY walk for guests never having entered 5-level mode.)
The same may want (need) adding to _shadow_prealloc() and perhaps elsewhere. However, for zapping entries from alive domains hash lookup pulling most recently used entries to the front means most recently used entries would then also be zapped first, which isn't very nice.
---
v14: Exclude 32-bit L2 types. Re-base.
v13: New.
xen/arch/x86/mm/shadow/common.c +66 -27
@@ -268,6 +268,32 @@ sh_validate_guest_entry(struct vcpu *v,return result; } +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 int hash_foreach (struct domain *d, +unsigned int callback_mask, +const hash_callback_t callbacks[], +mfn_t callback_mfn); + +/* +* Dispatch table for getting per-type functions: each level must +* be called with the function to remove a lower-level shadow. +*/ +static const hash_callback_t remove_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 ), +#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 ), +};/**************************************************************************/ /* Memory management for shadow pages. */
@@ -476,7 +502,35 @@ void shadow_blow_tables(struct domain *dif ( !d->vcpu[0 ] )return ; -/* Pass one: unpin all pinned pages */ +/* First pass: reduce page table depth */ +if ( preempted ) + { +#define callbacks remove_callbacks +static const unsigned int masks[] = { +SHF_L2_ANY & ~(SHF_L2_32 |SHF_L2_PAE ), +SHF_L3_ANY , +#if CONFIG_PAGING_LEVELS > 4 +SHF_L4_ANY , +#endif + }; + +HASH_CALLBACKS_CHECK (SHF_page_type_mask & ~(SHF_L1_ANY |SHF_FL1_ANY )); + +for ( i =0 ; i <ARRAY_SIZE (masks); ++i ) + { +while (hash_foreach (d, masks[i], callbacks,_mfn (0 )) ) + { +if (general_preempt_check () ) + { + *preempted = true; +return ; + } + } + } +#undef callbacks + } + +/* Second pass: unpin all pinned pages */ foreach_pinned_shadow(d, sp, t) { smfn =page_to_mfn (sp);
@@ -488,7 +542,7 @@ void shadow_blow_tables(struct domain *d } } -/* Second pass: unhook entries of in-use shadows */ +/* Third pass: unhook entries of in-use shadows */ for_each_vcpu (d, v)for ( i =0 ; i <ARRAY_SIZE (v->arch.paging.shadow.shadow_table); i++ )if ( !pagetable_is_null (v->arch.paging.shadow.shadow_table[i]) )
@@ -1151,15 +1205,10 @@ bool shadow_hash_delete(struct domain *dreturn true; } -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_foreach (struct domain *d, -unsigned int callback_mask, -const hash_callback_t callbacks[], -mfn_t callback_mfn) +static int 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
@@ -1176,7 +1225,7 @@ static void hash_foreach(struct domain */* Can be called via p2m code &c after shadow teardown. */ if (unlikely (!d->arch.paging.shadow.hash_table) ) -return ; +return 0 ;/* Say we're here, to stop hash-lookups reordering the chains */ ASSERT (d->arch.paging.shadow.hash_walking ==0 );
@@ -1201,6 +1250,8 @@ static void hash_foreach(struct domain *if ( done )break ; } d->arch.paging.shadow.hash_walking =0 ; + +return done; }
@@ -1640,21 +1691,6 @@ void sh_remove_shadows(struct domain *d,mfn_t smfn;unsigned char t; -/* Dispatch table for getting per-type functions: each level must -* be called with the function to remove a lower-level shadow. */ -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 ), -#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 ), - }; -/* Another lookup table, for choosing which mask to use */ static const unsigned int masks[SH_type_unused] = { #ifdef CONFIG_HVM
@@ -1689,6 +1725,8 @@ void sh_remove_shadows(struct domain *d,/* Search for this shadow in all appropriate shadows */ perfc_incr (shadow_unshadow); +#define callbacks remove_callbacks +/* * Lower-level shadows need to be excised from upper-level shadows. This * call to hash_foreach() looks dangerous but is in fact OK: each call
@@ -1736,6 +1774,7 @@ void sh_remove_shadows(struct domain *d,DO_UNSHADOW (SH_type_l1_64_shadow);#undef DO_UNSHADOW +#undef callbacks/* If that didn't catch the shadows, something is wrong */ if ( !fast && all && (pg->count_info & PGC_shadowed_pt) )
xen/arch/x86/mm/shadow/multi.c +12 -6
@@ -3644,10 +3644,12 @@ int cf_check sh_remove_l1_shadow(struct FOREACH_PRESENT_L2E(sl2mfn, sl2e, NULL, done, d, { - if ( mfn_eq(shadow_l2e_get_mfn(*sl2e), sl1mfn) ) + mfn_t mfn = shadow_l2e_get_mfn(*sl2e); + + if ( !mfn_x(sl1mfn) || mfn_eq(mfn, sl1mfn) ) { shadow_set_l2e(d, sl2e, shadow_l2e_empty(), sl2mfn); - if ( mfn_to_page(sl1mfn)->u.sh.type == 0 ) + if ( !mfn_to_page(mfn)->u.sh.type ) /* This breaks us cleanly out of the FOREACH macro */ done = 1; }
@@ -3664,10 +3666,12 @@ int cf_check sh_remove_l2_shadow(struct FOREACH_PRESENT_L3E(sl3mfn, sl3e, NULL, done, { - if ( mfn_eq(shadow_l3e_get_mfn(*sl3e), sl2mfn) ) + mfn_t mfn = shadow_l3e_get_mfn(*sl3e); + + if ( !mfn_x(sl2mfn) || mfn_eq(mfn, sl2mfn) ) { shadow_set_l3e(d, sl3e, shadow_l3e_empty(), sl3mfn); - if ( mfn_to_page(sl2mfn)->u.sh.type == 0 ) + if ( !mfn_to_page(mfn)->u.sh.type ) /* This breaks us cleanly out of the FOREACH macro */ done = 1; }
@@ -3683,10 +3687,12 @@ int cf_check sh_remove_l3_shadow(struct FOREACH_PRESENT_L4E(sl4mfn, sl4e, NULL, done, d, { - if ( mfn_eq(shadow_l4e_get_mfn(*sl4e), sl3mfn) ) + mfn_t mfn = shadow_l4e_get_mfn(*sl4e); + + if ( !mfn_x(sl3mfn) || mfn_eq(mfn, sl3mfn) ) { shadow_set_l4e(d, sl4e, shadow_l4e_empty(), sl4mfn); - if ( mfn_to_page(sl3mfn)->u.sh.type == 0 ) + if ( !mfn_to_page(mfn)->u.sh.type ) /* This breaks us cleanly out of the FOREACH macro */ done = 1; }
xen/arch/x86/mm/shadow/private.h +3 -0
@@ -282,6 +282,9 @@ static inline void sh_terminate_list(str#define SHF_L1_ANY (SHF_L1_32|SHF_L1_PAE|SHF_L1_64)#define SHF_FL1_ANY (SHF_FL1_32|SHF_FL1_PAE|SHF_FL1_64) +#define SHF_L2_ANY (SHF_L2_32|SHF_L2_PAE|SHF_L2H_64|SHF_L2_64) +#define SHF_L3_ANY SHF_L3_64 +#define SHF_L4_ANY SHF_L4_64#if (SHADOW_OPTIMIZATIONS &SHOPT_OUT_OF_SYNC )/* Marks a guest L1 page table which is shadowed but not write-protected.
PATCH v15 5/10 x86/shadow: make clean-dirty-bitmap post-processing preemptable
In reply to Jan BeulichWhat this currently amounts to is blowing the domain's tables. To make that call preemptable and to record state in a race-free manner, it needs to be moved inside the paging-locked region. Since the corresponding HAP function wants to acquire the P2M lock, the hook invocation itself cannot be moved. Call the shadow function directly, and prevent the hook from being invoked in that case.
To prevent "extra" accounting from being done more than once in paging_log_dirty_op(), the mapping of the top level table also becomes conditional (upon the subsequent loop doing nothing anyway).
Further note that this now also avoids sh{,adow}_clean_dirty_bitmap() needlessly being called when no paging mode was enabled at all for the domain. (The function continues to be called even when log-dirty mode isn't enabled.)
---
The hook functions were/are slightly mis-named, as they don't clean anything. However, to keep names similar (and hence possible to grep for) the new directly called function is still named in the same manner.
Of course we could purge the hook altogether, calling the HAP function directly as well. If so, question would be whether to do so right here or in a public follow-up (perhaps the latter).
In the course of doing this work I've noticed that libxenguest uses XEN_DOMCTL_SHADOW_OP_CLEAN even on the last iteration. Wouldn't it suffice to invoke the non-cleaning (peek) operation there instead, reducing the overhead of shadow's post-processing?
---
v15: Fix !SHADOW_PAGING build.
v14: New.
xen/arch/x86/include/asm/domain.h +1 -1
@@ -234,7 +234,7 @@ struct paging_domain {union {struct {unsigned long done:PADDR_BITS -PAGE_SHIFT ; -unsigned long i4:PAGETABLE_ORDER ; +unsigned long i4:PAGETABLE_ORDER +1 ;unsigned long i3:PAGETABLE_ORDER ; } log_dirty; };
xen/arch/x86/include/asm/shadow.h +3 -0
@@ -56,6 +56,9 @@ int shadow_domain_init(struct domain *d)* paging_vcpu_init() in paging.c */ void shadow_vcpu_init (struct vcpu *v); +/* Post-processing necessary after the log-dirty bitmap was cleaned. */ +bool shadow_clean_dirty_bitmap (struct domain *d); +#ifdef CONFIG_SHADOW_PAGING /* Enable an arbitrary shadow mode. Call once at domain creation. */
xen/arch/x86/mm/paging.c +20 -4
@@ -473,8 +473,9 @@ static int paging_log_dirty_op(struct do goto out; } - l4 =paging_map_log_dirty_bitmap (d); i4 = d->arch.paging.preempt.log_dirty.i4; +if ( i4 <LOGDIRTY_NODE_ENTRIES ) + l4 =paging_map_log_dirty_bitmap (d); i3 = d->arch.paging.preempt.log_dirty.i3; pages = d->arch.paging.preempt.log_dirty.done;
@@ -542,6 +543,19 @@ static int paging_log_dirty_op(struct doif ( l4 )unmap_domain_page (l4); +/* +* In the shadow case post-processing may take long and hence needs to be +* preemptable. Since continuation state is protected by the paging lock, +* we need to call its function (and record state) before dropping that +* lock. The hook invocation further down therefore will be skipped. +*/ +if ( !rv && clean &&paging_mode_shadow (d) && + !shadow_clean_dirty_bitmap (d) ) + { + d->arch.paging.preempt.log_dirty.i4 =LOGDIRTY_NODE_ENTRIES ; + rv = -ERESTART ; + } +if ( !rv ) { d->arch.paging.preempt.dom =NULL ;
@@ -569,10 +583,12 @@ static int paging_log_dirty_op(struct doif ( pages < sc->pages ) sc->pages = pages; -if ( clean ) +if ( clean &&paging_mode_hap (d) ) { -/* We need to further call clean_dirty_bitmap() functions of specific -* paging modes (shadow or hap). Safe because the domain is paused. */ +/* +* We need to further call HAP's clean_dirty_bitmap() function (shadow +* was dealt with above). Safe because the domain is paused. +*/ d->arch.paging.log_dirty.ops->clean (d); }domain_unpause (d);
xen/arch/x86/mm/shadow/common.c +9 -6
@@ -71,7 +71,6 @@ DEFINE_PER_CPU(uint32_t,trace_shadow_pat static int cf_check sh_enable_log_dirty(struct domain *d); static int cf_check sh_disable_log_dirty(struct domain *d); -static void cf_check sh_clean_dirty_bitmap(struct domain *d); static void cf_check shadow_update_paging_modes(struct vcpu *v);
@@ -82,7 +81,7 @@ int shadow_domain_init(struct domain *d)static const struct log_dirty_ops sh_ops = { .enable = sh_enable_log_dirty, .disable = sh_disable_log_dirty, - .clean = sh_clean_dirty_bitmap, + .clean =ZERO_BLOCK_PTR , };INIT_PAGE_LIST_HEAD (&d->arch.paging.shadow.pinned_shadows);
@@ -2606,14 +2605,18 @@ static int cf_check sh_disable_log_dirty/* This function is called when we CLEAN log dirty bitmap. See * paging_log_dirty_op() for details. */ -static void cf_check sh_clean_dirty_bitmap(struct domain *d) +bool shadow_clean_dirty_bitmap (struct domain *d) { -paging_lock (d); +bool preempted = false; + +ASSERT (paging_locked_by_me (d)); +/* Need to revoke write access to the domain's pages again. * In future, we'll have a less heavy-handed approach to this, * but for now, we just unshadow everything except Xen. */ -shadow_blow_tables (d,NULL ); -paging_unlock (d); +shadow_blow_tables (d, &preempted); + +return !preempted; }/**************************************************************************/
xen/arch/x86/mm/shadow/none.c +1 -6
@@ -7,11 +7,6 @@ static int cf_check _toggle_log_dirty(streturn -EOPNOTSUPP ; } -static void cf_check _clean_dirty_bitmap(struct domain *d) -{ -ASSERT (is_pv_domain (d)); -} - static void cf_check _update_paging_modes(struct vcpu *v) {ASSERT_UNREACHABLE ();
@@ -23,7 +18,7 @@ int shadow_domain_init(struct domain *d)static const struct log_dirty_ops sh_none_ops = { .enable = _toggle_log_dirty, .disable = _toggle_log_dirty, - .clean = _clean_dirty_bitmap, + .clean =ZERO_BLOCK_PTR , };paging_log_dirty_init (d, &sh_none_ops);
PATCH v15 6/10 x86/mm: make more of log-dirty mode enable/disable preemptable
In reply to Jan BeulichSo far only part of disable was preemptable. Shadow code, however, will need to purge all earlier shadows, which may take quite a bit of time. In the general (paging) layer arrange for the hook functions to possibly indicate the need for resuming. In shadow code leverage "enable" already calling shadow_blow_tables(), simply making that call preemptable, while adding a call to that function for "disable".
In order for resume state to be properly guarded, the paging lock now needs acquiring in paging_log_dirty_{en,dis}able(). HAP's need to acquire the P2M lock makes it necessary to allow the hook functions to drop the paging lock.
Note that when requesting resuming, the hook functions must not have changed the paging (shadow) mode yet; the actual mode change part is expected to be performed quickly enough to not require preemption. This is why "blow" (done ahead of mode switching) is preferred over "detach-old" (occurring with mode already updated). While this may mean more purging being done now for "disable", starting from a clean state afterwards may be better overall anyway.
Note further that HAP code is, except for the locking adjustments, (intended to be) largely unaffected, for not having a need for preemption checking.
---
Locking isn't really nice here, but I see no better way given HAP's need to acquire the P2M lock in its hook functions. Things may end up a little better if doing away with the hooks and handling HAP and shadow (in part) separately in paging.c, and then building upon HAP not needing any preemption checking. Yet whether purging the hooks right here is okay is unclear (see also "x86/shadow: make clean-dirty-bitmap post-processing preemptable", where the question also arises).
See also the comments in "x86/shadow: 1-bit-disable doesn't need to detach old tables". Specifically avoiding shadow_blow_tables() here would mean pinned shadows wouldn't be needlessly unpinned.
---
v15: Add missing domain_unpause() to paging_log_dirty_enable()'s HAP
path. Re-base.
v14: (Re)check and update preemption state under paging lock. Adjust
locking accordingly. Re-base.
v13: New.
xen/arch/x86/include/asm/domain.h +5 -0
@@ -199,6 +199,11 @@ struct log_dirty_domain {/* functions which are paging mode specific */ const struct log_dirty_ops { + /* + * enable() and disable() will be called with the paging lock held. + * They may drop the lock (but not drop and re-acquire it), but then + * need to indicate so by returning a positive value. + */ int (*enable )(struct domain *d); int (*disable )(struct domain *d); void (*clean )(struct domain *d);
xen/arch/x86/mm/hap/hap.c +4 -4
@@ -184,7 +184,7 @@ static int cf_check hap_enable_log_dirtyreturn -EBUSY ;/* turn on PG_log_dirty bit in paging mode */ -paging_lock (d); +ASSERT (paging_locked_by_me (d)); d->arch.paging.mode |= PG_log_dirty;paging_unlock (d);
@@ -198,12 +198,12 @@ static int cf_check hap_enable_log_dirtyp2m_change_entry_type_global (d, p2m_ram_rw, p2m_ram_logdirty);guest_flush_tlb_mask (d, d->dirty_cpumask); -return 0 ; +return 1 ; } static int cf_check hap_disable_log_dirty(struct domain *d) { -paging_lock (d); +ASSERT (paging_locked_by_me (d)); d->arch.paging.mode &= ~PG_log_dirty;paging_unlock (d);
@@ -215,7 +215,7 @@ static int cf_check hap_disable_log_dirt* normal mode, or via hardware-assisted log-dirty. */ p2m_change_entry_type_global (d, p2m_ram_logdirty, p2m_ram_rw); -return 0 ; +return 1 ; } static void cf_check hap_clean_dirty_bitmap(struct domain *d)
xen/arch/x86/mm/paging.c +77 -21
156 lines not on this page · raw message
xen/arch/x86/mm/shadow/common.c +23 -10
@@ -2567,13 +2567,22 @@ static int cf_check sh_enable_log_dirty( {int ret; -paging_lock (d); +ASSERT (paging_locked_by_me (d)); +if (shadow_mode_enabled (d) ) { -/* This domain already has some shadows: need to clear them out +bool preempted = false; + +/* +* This domain already has some shadows: need to clear them out * of the way to make sure that all references to guest memory are -* properly write-protected */ -shadow_blow_tables (d,NULL ); +* properly write-protected. +* +* Furthermore see sh_disable_log_dirty() below. +*/ +shadow_blow_tables (d, &preempted); +if ( preempted ) +return -ERESTART ; }#if (SHADOW_OPTIMIZATIONS &SHOPT_LINUX_L3_TOPLEVEL )
@@ -2585,7 +2594,6 @@ static int cf_check sh_enable_log_dirty(#endif ret =shadow_one_bit_enable (d, PG_log_dirty); -paging_unlock (d);return ret; }
@@ -2593,13 +2601,18 @@ static int cf_check sh_enable_log_dirty(/* shadow specfic code which is called in paging_log_dirty_disable() */ static int cf_check sh_disable_log_dirty(struct domain *d) { -int ret; +bool preempted = false; -paging_lock (d); - ret =shadow_one_bit_disable (d, PG_log_dirty); -paging_unlock (d); +ASSERT (paging_locked_by_me (d)); -return ret; +/* +* Limit the amount of work to do from sh_detach_old_tables() (called from +* shadow_one_bit_disable() via sh_new_mode() -> sh_update_paging_modes()), +* such that it doesn't also need to deal with preemption checks. +*/ +shadow_blow_tables (d, &preempted); + +return preempted ? -ERESTART :shadow_one_bit_disable (d, PG_log_dirty); }/* This function is called when we CLEAN log dirty bitmap. See
PATCH v15 7/10 x86/shadow: fold _shadow_prealloc() with shadow_blow_tables()
In reply to Jan BeulichThe two functions are pretty similar, and hence changes commonly need to occur in both places. Fold the functions by introducing a new shared helper, which then will be used from the three relevant places. Note that this way preemptability can then be communicated down from shadow_set_allocation() right away.
---
To have the compiler eliminate the extra conditionals, should we make sh_blow_tables() always-inline?
---
v14: New.
xen/arch/x86/include/asm/perfc_defn.h +2 -1
@@ -49,7 +49,8 @@ PERFCOUNTER(shadow_alloc_tlbflush, "shad/* STATUS counters do not reset when 'P' is hit */ PERFSTATUS(shadow_alloc_count, "number of shadow pages in use") PERFCOUNTER(shadow_free, "calls to shadow_free") -PERFCOUNTER(shadow_prealloc_1, "shadow recycles old shadows") +PERFCOUNTER(shadow_prealloc_0, "shadow recycles old shadows") +PERFCOUNTER(shadow_prealloc_1, "shadow recycles pinned shadows") PERFCOUNTER(shadow_prealloc_2, "shadow recycles in-use shadows") PERFCOUNTER(shadow_linear_map_failed, "shadow hit read-only linear map")PERFCOUNTER (shadow_a_update,"shadow A bit update" )
xen/arch/x86/mm/shadow/common.c +78 -85
261 lines not on this page · raw message
PATCH v15 8/10 x86/shadow: make log-dirty mode enable/disable properly preemptable
In reply to Jan BeulichTheir calls to shadow_set_allocation() are the last missing piece.
While it may seem a little odd, it actually turns out easier to deal with the continuation a level up from where the need for it was first recognized.
---
I think the point was raised before: It's questionable whether shadow_one_bit_enable(), upon error, should bring the allocation back down to zero. This is going to be especially bad for a domain which previously had shadow enabled already (which could be HVM or L1TF- affected PV). Even in shadow_one_bit_disable() it's not clear this is the best possible behavior - the pool may have been set to something larger than the default by the admin. For now I'm maintaining prior behavior, but of course things would end up simpler if we could just get rid of those set-to-zero operations (and then perhaps also on shadow_enable()'s similar error path); the possible caveat there would be that overall memory consumption may then appear to grow for people monitoring a system.
Originally I was considering to further qualify the d->arch.paging.preempt.drop_allocation checks by passing further down the "resuming" flag, but for a well-behaved tool stack (which allows one shadow-op to finish before starting another one) there shouldn't be a difference. Thoughts?
---
v14: New.
xen/arch/x86/include/asm/domain.h +1 -0
@@ -236,6 +236,7 @@ struct paging_domain { struct { const struct domain *dom; unsigned int op; + bool drop_allocation:1; union { struct { unsigned long done:PADDR_BITS - PAGE_SHIFT;
xen/arch/x86/mm/paging.c +8 -6
@@ -215,9 +215,10 @@ static int paging_log_dirty_enable(strucpaging_lock (d); -if ( d->arch.paging.preempt.dom && - (d->arch.paging.preempt.dom != current->domain || - d->arch.paging.preempt.op != op) ) +if ( !d->arch.paging.preempt.dom ) + d->arch.paging.preempt.drop_allocation = false; +else if ( d->arch.paging.preempt.dom != current->domain || + d->arch.paging.preempt.op != op ) {paging_unlock (d);if ( !resuming )
@@ -259,9 +260,10 @@ static int paging_log_dirty_disable(strupaging_lock (d); -if ( d->arch.paging.preempt.dom && - (d->arch.paging.preempt.dom != current->domain || - d->arch.paging.preempt.op !=XEN_DOMCTL_SHADOW_OP_OFF ) ) +if ( !d->arch.paging.preempt.dom ) + d->arch.paging.preempt.drop_allocation = false; +else if ( d->arch.paging.preempt.dom != current->domain || + d->arch.paging.preempt.op !=XEN_DOMCTL_SHADOW_OP_OFF ) {paging_unlock (d);if ( !resuming )
xen/arch/x86/mm/shadow/common.c +47 -6
93 lines not on this page · raw message
PATCH v15 9/10 x86/shadow: make forced (L1TF) mode enable properly preemptable
In reply to Jan BeulichLike for log-dirty enable/disable, reduce the amount of work to do underneath sh_new_mode() by calling shadow_blow_tables() (which is properly preemptable) up front. shadow_set_allocation() possibly taking long also is handled similar to log-dirty enable.
Since this code runs inside a tasklet, carrying out preemption isn't straightforward; see the code comment.
---
The single-CPU case could of course do with improvement. Plus even on multi-CPU systems there will still be problems if there are enough domains all being switched to shadow mode at roughly the same time.
Interaction with other shadow-op preemption is a little rough: Any attempt to issue a shadow-op (other than get-allocation) will result in -EBUSY while the operation here is still in progress.
Notes from discussion:
- if there weren't certain special situations (realtime scheduling requirements, null scheduler, vCPU pinning), simply handling softirqs _only_ in the tasklet might be okay
- instead of doing shadow_blow_tables() and shadow_set_allocation() from the tasklet, we may be able to arrange doing that in the context of the initiating vCPU (with all other vCPU-s paused), scheduling the tasklet only once done, and making sure we don't fully exit back to guest context in the process (but enough to allow scheduling to occur) [Could we have paravirt_ctxt_switch_to() schedule a softirq-tasklet for VMs in transitional state, which then does the work?]
---
v14: New.
xen/arch/x86/include/asm/domain.h +1 -0
@@ -237,6 +237,7 @@ struct paging_domain {const struct domain *dom;unsigned int op; bool drop_allocation:1; + bool pv_l1tf_paused:1; union { struct { unsigned long done:PADDR_BITS - PAGE_SHIFT;
xen/arch/x86/mm/shadow/common.c +69 -10
108 lines not on this page · raw message
PATCH v15 10/10 x86/shadow: limit the number of pages which may be in use as shadows
In reply to Jan BeulichIn order to bound the amount of work a single invocation of shadow_unhook_mappings() may be doing all in one go, constrain the number of pages which may be in use as shadows. To achieve that, simply adjust the "success" exit condition of _shadow_prealloc(), thus forcing removal of shadows not only when we're short of memory.
Note that, depending on workload, this may have a severe effect on performance, due to the potentially much larger rate of thrashed shadows.
In the context of "x86/shadow: account for log-dirty mode when pre- allocating" it is relevant to note that we will be too strict in sh_prealloc_okay() when log-dirty mode is enabled: Only part of the pages considered are actually to become shadows. But I think accepting this is better than further complicating the logic.
---
What exactly we want the upper bound to be is up for discussion. This may need to go together with an upper limit on the number of vCPU-s we deem supportable in a (shadow) guest.
Really 32-bit HVM guests have 3 monitor tables. But I think that not accounting for that in sh_prealloc_okay() is acceptable. How to correctly do such accounting there would be unclear anyway, as we mean to only take domain properties into account, whereas mode dependent properties are per-vCPU.
Backporting note: The placement of the setting of the new per-domain
field relies on d->max_vcpus being set right at domain creation. Hence this will need to move elsewhere for 4.11 and older (perhaps into shadow_set_allocation()'s "if ( pages > 0 )" block, conditional upon the value still being zero and max_vcpus already set).
Backporting note: 1d3668664df7 ("x86/shadow: restrict OOS allocation to
when it's really needed") is a necessary prereq for the respective part of sh_prealloc_okay().
---
v14: Re-base over XSA-427 and new earlier patches. Restrict allowance
for monitor tables to HVM. Restrict allowance for OOS to when that's actually in use.
v13: Prevent underflow in sh_prealloc_okay(). Re-base.
v12: Re-base past the XSA-410 series.
v11: Account for monitor tables and OOS snapshots in sh_prealloc_okay().
Calculate the (default) maximum value once during domain initialization, into a new per-domain field.
v10: Extend commit message.
v9: New.
xen/arch/x86/include/asm/domain.h +3 -0
@@ -108,6 +108,9 @@ void init_hypercall_page(struct domain *struct shadow_domain {#ifdef CONFIG_SHADOW_PAGING unsigned int opt_flags;/* runtime tunable optimizations on/off */ + +unsigned int max_pages;/* limit on the number of shadows in use */ +struct page_list_head pinned_shadows;/* 1-to-1 map for use when HVM vcpus have paging disabled */
xen/arch/x86/mm/shadow/common.c +60 -9
135 lines not on this page · raw message