x86: reduce cache flushing overhead
cover x86: reduce cache flushing overhead
..., first and foremost by using cache write-back operations instead of flushing ones when available (and sufficient for the purpose).
In the context of making the last patch I started wondering whether for PV we don't flush (write back) too little for MMUEXT_FLUSH_CACHE:
Just like for HVM, pCPU-s a vCPU has run on before could still hold data in their caches. (We clearly still flush / write back too much in MMUEXT_FLUSH_CACHE_GLOBAL even with this series in place.) We also can't call this the guest's responsibility, as it may not have any means to have one of its vCPU-s run on the intended pCPU.
1: x86: support cache-writeback in flush_area_local() et al 2: x86/HVM: restrict guest-induced WBINVD to cache writeback 3: x86/PV: restrict guest-induced WBINVD (or alike) to cache writeback 4: VT-d: restrict iommu_flush_all() to cache writeback
5: x86/HVM: limit cache writeback overhead
Jan
PATCH 1/5 x86: support cache-writeback in flush_area_local() et al
The majority of the present callers really aren't after invalidating cache contents, but only after writeback. Make this available by simply extending the FLUSH_CACHE handling accordingly. No feature checks are required here: cache_writeback() falls back to cache_flush() as necessary, while WBNOINVD degenerates to WBINVD on older hardware.
xen/arch/x86/flushtlb.c +9 -6
@@ -232,7 +232,7 @@ unsigned int flush_area_local(const voidif ( flags &FLUSH_HVM_ASID_CORE )hvm_flush_guest_tlbs (); -if ( flags &FLUSH_CACHE ) +if ( flags & (FLUSH_CACHE |FLUSH_WRITEBACK ) ) {const struct cpuinfo_x86 *c = ¤t_cpu_data;unsigned long sz =0 ;
@@ -245,13 +245,16 @@ unsigned int flush_area_local(const void c->x86_clflush_size && c->x86_cache_size && sz && ((sz >> 10) < c->x86_cache_size) ) { - cache_flush(va, sz); - flags &= ~FLUSH_CACHE; +if ( flags &FLUSH_CACHE ) +cache_flush (va, sz); +else +cache_writeback (va, sz); + flags &= ~(FLUSH_CACHE |FLUSH_WRITEBACK ); } - else - { + else if ( flags & FLUSH_CACHE ) wbinvd(); - } + else +wbnoinvd (); }if ( flags &FLUSH_ROOT_PGTBL )
xen/arch/x86/include/asm/flushtlb.h +7 -1
@@ -135,6 +135,8 @@ void switch_cr3_cr4(unsigned long cr3, u#else # define FLUSH_NO_ASSIST 0#endif +/* Write back data cache contents */ +#define FLUSH_WRITEBACK 0x10000/* Flush local TLBs/caches. */ unsigned int flush_area_local (const void *va,unsigned int flags);
@@ -194,7 +196,11 @@ static inline int clean_and_invalidate_d }static inline int clean_dcache_va_range (const void *p,unsigned long size) { -return clean_and_invalidate_dcache_va_range (p, size); +unsigned int order =get_order_from_bytes (size); + +/* sub-page granularity support needs to be added if necessary */ +flush_area_local (p,FLUSH_WRITEBACK |FLUSH_ORDER (order)); +return 0 ; }unsigned int guest_flush_tlb_flags (const struct domain *d);
Re: [PATCH 1/5] x86: support cache-writeback in flush_area_local() et al
xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void · line 235
Quoted 8 lines · Jan Beulich
xen/arch/x86/flushtlb.c +1
-1@@ -232,7 +232,7 @@ unsigned int flush_area_local(const voidif ( flags &FLUSH_HVM_ASID_CORE )hvm_flush_guest_tlbs (); -if ( flags &FLUSH_CACHE ) +if ( flags & (FLUSH_CACHE |FLUSH_WRITEBACK ) )
Given that we already have FLUSH_CACHE, then adding writeback also seems fine, but we need to get the naming corrected first.
We've got a file called flushtlb.c which flushes more than the TLBs now, and various APIs in it.
We have a bunch of ARM specific APIs which AFAICT exist purely to prop up the ARM-specific gnttab_cache_flush(). That needs to go and live behind an ifdef and stop polluting other architectures with an incredibly short sighted hypercall interface decision.
The "area" in the low level calls isn't good. Range might be better, but I'm not sure. The "mask" part of the name would be better as "some" or perhaps "others", to be a better counterpoint to "local". Some of the wrappers too really ought to be dropped - there are lots of them, and too few users to justify.
But on to the main thing which caught my eye...
The FLUSH in FLUSH_CACHE means the flush infrastructure, not "cache flushing", and FLUSH_WRITEBACK is nonsensical next to this. All other things we flush have a qualification that makes them clear in context. (other than the assist one which I'm going to time out objections to and revert back to name which made more sense.)
At an absolutely minimum, FLUSH_CACHE first needs renaming to FLUSH_CACHE_EVICT and then this new one you're adding needs to be FLUSH_CACHE_WRITEBACK.
Except...
Is there any credible reason to have EVICT as an option by the end of this cleanup?
CLDEMOTE does exist for a reason (reducing coherency traffic overhead when you know the consumer is on a different CPU), but it would be totally bogus to use this in an all or mask form, and you wouldn't want to use it in local form either, simply from an overhead point of view.
We have evict behaviour simply because clflush was the only game in town for decades, not because evicting the cacheline is what you want actually want to do.
~Andrew
Re: [PATCH 1/5] x86: support cache-writeback in flush_area_local() et al
Quoted 27 lines · Andrew Cooper
xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void · line 235
Quoted 8 lines · Jan Beulich
xen/arch/x86/flushtlb.c +1
-1@@ -232,7 +232,7 @@ unsigned int flush_area_local(const voidif ( flags &FLUSH_HVM_ASID_CORE )hvm_flush_guest_tlbs (); -if ( flags &FLUSH_CACHE ) +if ( flags & (FLUSH_CACHE |FLUSH_WRITEBACK ) )Given that we already have
FLUSH_CACHE, then adding writeback also seems fine, but we need to get the naming corrected first.We've got a file called
flushtlb.cwhich flushes more than the TLBs now, and various APIs in it.We have a bunch of ARM specific APIs which AFAICT exist purely to prop up the ARM-specific gnttab_cache_flush(). That needs to go and live behind an ifdef and stop polluting other architectures with an incredibly short sighted hypercall interface decision.
The "area" in the low level calls isn't good. Range might be better, but I'm not sure. The "mask" part of the name would be better as "some" or perhaps "others", to be a better counterpoint to "local". Some of the wrappers too really ought to be dropped - there are lots of them, and too few users to justify.
Quoted 4 lines
But on to the main thing which caught my eye...
The FLUSH in
FLUSH_CACHEmeans the flush infrastructure, not "cache flushing", andFLUSH_WRITEBACKis nonsensical next to this.
I agree; I chose the name simply to avoid further changes, while still fitting the naming scheme (i.e. the FLUSH_ prefix). I'm okay to change to ...
Quoted 8 lines
All other
things we flush have a qualification that makes them clear in context. (other than the assist one which I'm going to time out objections to and revert back to name which made more sense.)At an absolutely minimum,
FLUSH_CACHEfirst needs renaming toFLUSH_CACHE_EVICTand then this new one you're adding needs to beFLUSH_CACHE_WRITEBACK.
... these, but I don't think they're much better (still primarily because of the FLUSH_ prefixes).
FTAOD - while I'm going to make these adjustments (adding a single prereq patch to carry out the initial rename), I don't really see me doing any of there other adjustments you were outlining above (at least not within this series).
Quoted 4 lines
Except...
Is there any credible reason to have EVICT as an option by the end of this cleanup?
Yes: map_pages_to_xen(), hvm_shadow_handle_cd(), memory_type_changed(), and hvm_set_mem_pinned_cacheattr(), all mean to evict caches aiui.
clean_and_invalidate_dcache_va_range(), as you say, really shouldn't be required, but I'm unconvinced we can easily drop support for a gnttab sub-op that's been around for a number of years. (The more with there (still) not being any support for GNTTAB_CACHE_SOURCE_GREF, I'm (still) thinking this shouldn't have been a gnttab sub-op in the first place, but something truly arch-specific [a platform-op, or a memory one, or ...].)
Jan
Quoted 10 lines
CLDEMOTE does exist for a reason (reducing coherency traffic overhead when you know the consumer is on a different CPU), but it would be totally bogus to use this in an all or mask form, and you wouldn't want to use it in local form either, simply from an overhead point of view.
We have evict behaviour simply because
clflushwas the only game in town for decades, not because evicting the cacheline is what you want actually want to do.~Andrew
Re: [PATCH 1/5] x86: support cache-writeback in flush_area_local() et al
Quoted 15 lines · Jan Beulich
Quoted 4 lines · Andrew Cooper
But on to the main thing which caught my eye...
The FLUSH in
FLUSH_CACHEmeans the flush infrastructure, not "cache flushing", andFLUSH_WRITEBACKis nonsensical next to this.I agree; I chose the name simply to avoid further changes, while still fitting the naming scheme (i.e. the
FLUSH_prefix). I'm okay to change to ...Quoted 5 lines
All other
things we flush have a qualification that makes them clear in context. (other than the assist one which I'm going to time out objections to and revert back to name which made more sense.)
Quoted 6 lines
Quoted 3 lines
At an absolutely minimum,
FLUSH_CACHEfirst needs renaming toFLUSH_CACHE_EVICTand then this new one you're adding needs to beFLUSH_CACHE_WRITEBACK.... these, but I don't think they're much better (still primarily because of the
FLUSH_prefixes).
Actually - are you going to insist on "first"? There would be less code churn if first I introduced and used FLUSH_CACHE_WRITEBACK, and only then renamed the few remaining FLUSH_CACHE to FLUSH_CACHE_EVICT.
Jan
PATCH 2/5 x86/HVM: restrict guest-induced WBINVD to cache writeback
In reply to Jan BeulichWe allow its use for writeback purposes only anyway, so let's also carry these out that way on capable hardware.
With it now known that WBNOINVD uses the same VM exit code as WBINVD for both SVM and VT-x, we can now also expose the feature that way without further distinguishing the specific cases of those VM exits. Note that on SVM this builds upon INSTR_WBINVD also covering WBNOINVD, as the decoder won't set prefix related bits for this encoding in the resulting canonicalized opcode.
xen/arch/x86/hvm/svm/svm.c +1 -1
@@ -2364,7 +2364,7 @@ static void svm_vmexit_mce_intercept( static void cf_check svm_wbinvd_intercept(void) {if (cache_flush_permitted (current->domain) ) -flush_all (FLUSH_CACHE ); +flush_all (FLUSH_WRITEBACK ); } static void svm_vmexit_do_invalidate_cache(struct cpu_user_regs *regs,
xen/arch/x86/hvm/vmx/vmcs.c +6 -6
@@ -1881,12 +1881,12 @@ void cf_check vmx_do_resume(void) {/* * For pass-through domain, guest PCI-E device driver may leverage the -* "Non-Snoop" I/O, and explicitly WBINVD or CLFLUSH to a RAM space. -* Since migration may occur before WBINVD or CLFLUSH, we need to -* maintain data consistency either by: -* 1: flushing cache (wbinvd) when the guest is scheduled out if +* "Non-Snoop" I/O, and explicitly WB{NO,}INVD or CL{WB,FLUSH} RAM space. +* Since migration may occur before WB{NO,}INVD or CL{WB,FLUSH}, we need +* to maintain data consistency either by: +* 1: flushing cache (wbnoinvd) when the guest is scheduled out if * there is no wbinvd exit, or -* 2: execute wbinvd on all dirty pCPUs when guest wbinvd exits. +* 2: execute wbnoinvd on all dirty pCPUs when guest wbinvd exits. * If VT-d engine can force snooping, we don't need to do these. */ if ( has_arch_pdevs(v->domain) && !iommu_snoop
@@ -1894,7 +1894,7 @@ void cf_check vmx_do_resume(void) {int cpu = v->arch.hvm.vmx.active_cpu;if ( cpu !=-1 ) -flush_mask (cpumask_of (cpu),FLUSH_CACHE ); +flush_mask (cpumask_of (cpu),FLUSH_WRITEBACK ); }vmx_clear_vmcs (v);
xen/arch/x86/hvm/vmx/vmx.c +2 -2
@@ -3714,9 +3714,9 @@ static void cf_check vmx_wbinvd_intercepreturn ;if ( cpu_has_wbinvd_exiting ) -flush_all (FLUSH_CACHE ); +flush_all (FLUSH_WRITEBACK );else -wbinvd (); +wbnoinvd (); }static void ept_handle_violation (ept_qual_t q,paddr_t gpa)
xen/include/public/arch-x86/cpufeatureset.h +1 -1
@@ -238,7 +238,7 @@ XEN_CPUFEATURE(EFRO, 7*32+10) //* AMD-defined CPU features, CPUID level 0x80000008.ebx, word 8 */ XEN_CPUFEATURE(CLZERO, 8*32+ 0) /*A CLZERO instruction */ XEN_CPUFEATURE(RSTR_FP_ERR_PTRS, 8*32+ 2) /*A (F)X{SAVE,RSTOR} always saves/restores FPU Error pointers */ -XEN_CPUFEATURE(WBNOINVD, 8*32+ 9) /* WBNOINVD instruction */ +XEN_CPUFEATURE(WBNOINVD, 8*32+ 9) /*S WBNOINVD instruction */ XEN_CPUFEATURE(IBPB, 8*32+12) /*A IBPB support only (no IBRS, used by AMD) */ XEN_CPUFEATURE(IBRS, 8*32+14) /*S MSR_SPEC_CTRL.IBRS */XEN_CPUFEATURE (AMD_STIBP ,8 *32 +15 )/*S MSR_SPEC_CTRL.STIBP */
PATCH 3/5 x86/PV: restrict guest-induced WBINVD (or alike) to cache writeback
In reply to Jan BeulichWe allow its use for writeback purposes only anyway, so let's also carry these out that way on capable hardware.
We can then also expose the WBNOINVD feature, as there's no difference to WBINVD anymore. Note that respective emulation logic has already been in place since ad3abc47dd23 ("x86emul: support WBNOINVD").
xen/arch/x86/mm.c +2 -2
@@ -3772,7 +3772,7 @@ long do_mmuext_op(else if (unlikely (!cache_flush_permitted (currd)) ) rc = -EACCES ;else -wbinvd (); +wbnoinvd ();break ;case MMUEXT_FLUSH_CACHE_GLOBAL :
@@ -3788,7 +3788,7 @@ long do_mmuext_op(if ( !cpumask_intersects (mask,per_cpu (cpu_sibling_mask, cpu)) )__cpumask_set_cpu (cpu, mask); -flush_mask (mask,FLUSH_CACHE ); +flush_mask (mask,FLUSH_WRITEBACK ); }else rc = -EINVAL ;
xen/arch/x86/pv/emul-priv-op.c +1 -3
@@ -1196,10 +1196,8 @@ static int cf_check cache_op(* newer linux uses this in some start-of-day timing loops. */ ; - else if ( op == x86emul_wbnoinvd /* && cpu_has_wbnoinvd */ ) - wbnoinvd(); else -wbinvd (); +wbnoinvd ();return X86EMUL_OKAY ; }
xen/include/public/arch-x86/cpufeatureset.h +1 -1
@@ -238,7 +238,7 @@ XEN_CPUFEATURE(EFRO, 7*32+10) //* AMD-defined CPU features, CPUID level 0x80000008.ebx, word 8 */ XEN_CPUFEATURE(CLZERO, 8*32+ 0) /*A CLZERO instruction */ XEN_CPUFEATURE(RSTR_FP_ERR_PTRS, 8*32+ 2) /*A (F)X{SAVE,RSTOR} always saves/restores FPU Error pointers */ -XEN_CPUFEATURE(WBNOINVD, 8*32+ 9) /*S WBNOINVD instruction */ +XEN_CPUFEATURE(WBNOINVD, 8*32+ 9) /*A WBNOINVD instruction */ XEN_CPUFEATURE(IBPB, 8*32+12) /*A IBPB support only (no IBRS, used by AMD) */ XEN_CPUFEATURE(IBRS, 8*32+14) /*S MSR_SPEC_CTRL.IBRS */XEN_CPUFEATURE (AMD_STIBP ,8 *32 +15 )/*S MSR_SPEC_CTRL.STIBP */
Re: [PATCH 3/5] x86/PV: restrict guest-induced WBINVD (or alike) to cache writeback
xen/arch/x86/mm.c long do_mmuext_op( · line 3776
Quoted 9 lines · Jan Beulich
xen/arch/x86/mm.c +1
-1@@ -3772,7 +3772,7 @@ long do_mmuext_op(else if (unlikely (!cache_flush_permitted (currd)) ) rc = -EACCES ;else -wbinvd (); +wbnoinvd ();break ;
It occurs to me that this is fundamentally broken.
The guest is not in any position to know (or control) whether it gets rescheduled elsewhere between now and it logically continuing execution.
So if there is actually any cache maintenance to do, it can't be a WB{...} of any form on just this CPU alone.
xen/include/public/arch-x86/cpufeatureset.h XEN_CPUFEATURE(EFRO, 7*32+10) / · line 241
Quoted 8 lines
xen/include/public/arch-x86/cpufeatureset.h +1
-1@@ -238,7 +238,7 @@ XEN_CPUFEATURE(EFRO, 7*32+10) //* AMD-defined CPU features, CPUID level 0x80000008.ebx, word 8 */ XEN_CPUFEATURE(CLZERO, 8*32+ 0) /*A CLZERO instruction */ XEN_CPUFEATURE(RSTR_FP_ERR_PTRS, 8*32+ 2) /*A (F)X{SAVE,RSTOR} always saves/restores FPU Error pointers */ -XEN_CPUFEATURE (WBNOINVD ,8 *32 +9 )/*S WBNOINVD instruction */ +XEN_CPUFEATURE (WBNOINVD ,8 *32 +9 )/*A WBNOINVD instruction */
Given that PV guests do have several real hypercalls for doing this, I'm not particularly inclined to let them do it via an emulated path, however easy it might be at a technical level.
I doubt anything good can come from it.
~Andrew
Re: [PATCH 3/5] x86/PV: restrict guest-induced WBINVD (or alike) to cache writeback
Quoted 13 lines · Andrew Cooper
xen/arch/x86/mm.c long do_mmuext_op( · line 3776
Quoted 9 lines · Jan Beulich
xen/arch/x86/mm.c +1
-1@@ -3772,7 +3772,7 @@ long do_mmuext_op(else if (unlikely (!cache_flush_permitted (currd)) ) rc = -EACCES ;else -wbinvd (); +wbnoinvd ();break ;It occurs to me that this is fundamentally broken.
Quoted 5 lines
The guest is not in any position to know (or control) whether it gets rescheduled elsewhere between now and it logically continuing execution.
So if there is actually any cache maintenance to do, it can't be a WB{...} of any form on just this CPU alone.
Aren't you merely confirming the respective paragraph in the cover letter here? (The implication would be that at least the data added in the last patch wants to be guest-type-independent, so that it can be re-used for PV, even if maybe not in the same patch.)
Quoted 13 lines
xen/include/public/arch-x86/cpufeatureset.h XEN_CPUFEATURE(EFRO, 7*32+10) / · line 241
Quoted 8 lines
xen/include/public/arch-x86/cpufeatureset.h +1
-1@@ -238,7 +238,7 @@ XEN_CPUFEATURE(EFRO, 7*32+10) //* AMD-defined CPU features, CPUID level 0x80000008.ebx, word 8 */ XEN_CPUFEATURE(CLZERO, 8*32+ 0) /*A CLZERO instruction */ XEN_CPUFEATURE(RSTR_FP_ERR_PTRS, 8*32+ 2) /*A (F)X{SAVE,RSTOR} always saves/restores FPU Error pointers */ -XEN_CPUFEATURE (WBNOINVD ,8 *32 +9 )/*S WBNOINVD instruction */ +XEN_CPUFEATURE (WBNOINVD ,8 *32 +9 )/*A WBNOINVD instruction */ Given that PV guests do have several real hypercalls for doing this, I'm not particularly inclined to let them do it via an emulated path, however easy it might be at a technical level.
Quoted 1 line
I doubt anything good can come from it.
We permit use of WBINVD; it looks inconsistent to me to not also permit WBNOINVD then. Furthermore exposing the feature then can serve as a (better) hint that behind the scenes we actually carry out the mmuext ops as write-back. not evict.
Jan
PATCH 4/5 VT-d: restrict iommu_flush_all() to cache writeback
In reply to Jan BeulichWe don't need to invalidate caches here; all we're after is that earlier writes have made it to main memory (and aiui even that just in case).
---
This, aiui, being an analogue to uses of iommu_sync_cache() (just not range restricted), I wonder whether it shouldn't be conditional upon iommu_non_coherent. Then again I'm vaguely under the impression that we had been here before, possibly even as far as questioning the need for this call altogether.
xen/drivers/passthrough/vtd/iommu.c +1 -1
@@ -693,7 +693,7 @@ static int __must_check iommu_flush_all(bool_t flush_dev_iotlb;int rc =0 ; -flush_local (FLUSH_CACHE ); +flush_local (FLUSH_WRITEBACK );for_each_drhd_unit ( drhd ) {
Re: [PATCH 4/5] VT-d: restrict iommu_flush_all() to cache writeback
Quoted 10 lines · Jan Beulich
We don't need to invalidate caches here; all we're after is that earlier writes have made it to main memory (and aiui even that just in case).
Signed-off-by Jan Beulich <[email protected]>---
This, aiui, being an analogue to uses ofiommu_sync_cache()(just not range restricted), I wonder whether it shouldn't be conditional uponiommu_non_coherent. Then again I'm vaguely under the impression that we had been here before, possibly even as far as questioning the need for this call altogether.
I'd far rather we fix it property than continue to massage around the sides of known-broken logic.
Coherency, or not, of the memory accesses of an IOMMU is a per-IOMMU property, not a system-wide property. What the iommu_non_coherent global boolean currently does for us is enforce cache maintenance on all IOMMUs, even the coherent ones, when any single IOMMU in the system is non-coherent.
Inside the IOMMU driver, cache maintenance should depend on iommu->ecap alone, disregarding anything else. This will improve the performance on any system with mixed coherent and non-coherent IOMMUs, without any other behavioural impact.
The complication comes in in p2m-ept when we're sharing EPT and IOMMU pagetables, because the EPT can be accessed by more than one IOMMU if the guest has devices behind different IOMMUs.
But we know the devices assigned to the domain. They're almost always static for the lifetime of the domain, and generally single device only, so there may be value in considering a per-domain "I've got a non-coherent IOMMU" flag, because we absolutely don't want to be walking the list of attached IOMMUs on each EPT update.
But...
SandyBridge era systems are the only ones I'm aware of where the IOMMUs advertise themselves as non-coherent. And on that generation we quirk the superpage capability off anyway, meaning they are ineligible for HAP-PT sharing anyway.
And if we are doing HAP-PT sharing, the cache maintenance for regular EPT updates will be crippling for CPU performance, especially as the software bit updates are not relevant to the IOMMU anyway.
A better option would be to simply disallow HAP-PT sharing when there's a non-coherent IOMMU in the system, or (slightly more fine grained) to disallow adding a device behind a non-coherent IOMMU to a domain using HAP-PT sharing (as there's a disable now anyway).
Either will reduce the complexity of Xen's code without any loss of functionality in real systems AFAICT.
~Andrew
Re: [PATCH 4/5] VT-d: restrict iommu_flush_all() to cache writeback
Quoted 12 lines · Andrew Cooper
Quoted 10 lines · Jan Beulich
We don't need to invalidate caches here; all we're after is that earlier writes have made it to main memory (and aiui even that just in case).
Signed-off-by Jan Beulich <[email protected]>---
This, aiui, being an analogue to uses ofiommu_sync_cache()(just not range restricted), I wonder whether it shouldn't be conditional uponiommu_non_coherent. Then again I'm vaguely under the impression that we had been here before, possibly even as far as questioning the need for this call altogether.
Quoted 2 lines
I'd far rather we fix it property than continue to massage around the sides of known-broken logic.
I agree in principle, but you're not asking that I actually amend this single-line change with all the work you outline, are you? To be quite honest, what you ask for really is something the VT-d maintainer(s) (i.e. Kevin as it presently stands) ought to be doing (or really have done long ago) ...
Jan
Quoted 42 lines
Coherency, or not, of the memory accesses of an IOMMU is a per-IOMMU property, not a system-wide property. What the
iommu_non_coherentglobal boolean currently does for us is enforce cache maintenance on all IOMMUs, even the coherent ones, when any single IOMMU in the system is non-coherent.Inside the IOMMU driver, cache maintenance should depend on
iommu->ecapalone, disregarding anything else. This will improve the performance on any system with mixed coherent and non-coherent IOMMUs, without any other behavioural impact.The complication comes in in p2m-ept when we're sharing EPT and IOMMU pagetables, because the EPT can be accessed by more than one IOMMU if the guest has devices behind different IOMMUs.
But we know the devices assigned to the domain. They're almost always static for the lifetime of the domain, and generally single device only, so there may be value in considering a per-domain "I've got a non-coherent IOMMU" flag, because we absolutely don't want to be walking the list of attached IOMMUs on each EPT update.
But...
SandyBridge era systems are the only ones I'm aware of where the IOMMUs advertise themselves as non-coherent. And on that generation we quirk the superpage capability off anyway, meaning they are ineligible for HAP-PT sharing anyway.
And if we are doing HAP-PT sharing, the cache maintenance for regular EPT updates will be crippling for CPU performance, especially as the software bit updates are not relevant to the IOMMU anyway.
A better option would be to simply disallow HAP-PT sharing when there's a non-coherent IOMMU in the system, or (slightly more fine grained) to disallow adding a device behind a non-coherent IOMMU to a domain using HAP-PT sharing (as there's a disable now anyway).
Either will reduce the complexity of Xen's code without any loss of functionality in real systems AFAICT.
~Andrew
PATCH 5/5 x86/HVM: limit cache writeback overhead
In reply to Jan BeulichThere's no need to write back caches on all CPUs upon seeing a WBINVD exit; ones that a vCPU hasn't run on since the last writeback (or since it was started) can't hold data which may need writing back.
---
With us not running AMD IOMMUs in non-coherent ways, I wonder whether svm_wbinvd_intercept() really needs to do anything (or whether it couldn't check iommu_snoop just like VMX does, knowing that as of c609108b2190 ["x86/shadow: make iommu_snoop usage consistent with HAP's"] that's always set; this would largely serve as grep fodder then, to make sure this code is updated once / when we do away with this global variable, and it would be the penultimate step to being able to fold SVM's and VT-x'es functions).
xen/arch/x86/hvm/hvm.c +9 -0
@@ -537,6 +537,8 @@ void hvm_do_resume(struct vcpu *v) v->arch.hvm.inject_event.vector =HVM_EVENT_VECTOR_UNSET ; } +__cpumask_set_cpu (v->processor, v->arch.hvm.cache_dirty_mask); +if (unlikely (v->arch.vm_event) && v->arch.monitor.next_interrupt_enabled ) {struct x86_event info;
@@ -1592,6 +1594,10 @@ int hvm_vcpu_initialise(struct vcpu *v)if ( rc ) goto fail6; + rc = -ENOMEM ; +if ( !zalloc_cpumask_var (&v->arch.hvm.cache_dirty_mask) ) + goto fail6; + rc =ioreq_server_add_vcpu_all (d, v);if ( rc !=0 ) goto fail6;
@@ -1621,6 +1627,7 @@ int hvm_vcpu_initialise(struct vcpu *v)hvm_vcpu_cacheattr_destroy (v); fail1:viridian_vcpu_deinit (v); +FREE_CPUMASK_VAR (v->arch.hvm.cache_dirty_mask);return rc; }
@@ -1628,6 +1635,8 @@ void hvm_vcpu_destroy(struct vcpu *v) {viridian_vcpu_deinit (v); +FREE_CPUMASK_VAR (v->arch.hvm.cache_dirty_mask); +ioreq_server_remove_vcpu_all (v->domain, v);if (hvm_altp2m_supported () )
xen/arch/x86/hvm/svm/svm.c +8 -2
@@ -2363,8 +2363,14 @@ static void svm_vmexit_mce_intercept( static void cf_check svm_wbinvd_intercept(void) { -if (cache_flush_permitted (current->domain) ) -flush_all (FLUSH_WRITEBACK ); +struct vcpu *curr = current; + +if ( !cache_flush_permitted (curr->domain) ) +return ; + +flush_mask (curr->arch.hvm.cache_dirty_mask,FLUSH_WRITEBACK ); +cpumask_copy (curr->arch.hvm.cache_dirty_mask, +cpumask_of (curr->processor)); } static void svm_vmexit_do_invalidate_cache(struct cpu_user_regs *regs,
xen/arch/x86/hvm/vmx/vmx.c +8 -2
@@ -3710,11 +3710,17 @@ static void vmx_do_extint(struct cpu_use static void cf_check vmx_wbinvd_intercept(void) { -if ( !cache_flush_permitted (current->domain) || iommu_snoop ) +struct vcpu *curr = current; + +if ( !cache_flush_permitted (curr->domain) || iommu_snoop )return ;if ( cpu_has_wbinvd_exiting ) -flush_all (FLUSH_WRITEBACK ); + { +flush_mask (curr->arch.hvm.cache_dirty_mask,FLUSH_WRITEBACK ); +cpumask_copy (curr->arch.hvm.cache_dirty_mask, +cpumask_of (curr->processor)); + }else wbnoinvd (); }
xen/arch/x86/include/asm/hvm/vcpu.h +2 -0
@@ -161,6 +161,8 @@ struct hvm_vcpu {struct svm_vcpu svm; }; +cpumask_var_t cache_dirty_mask; +struct tasklet assert_evtchn_irq_tasklet;struct nestedvcpu nvcpu;
Re: [PATCH 5/5] x86/HVM: limit cache writeback overhead
Quoted 5 lines · Jan Beulich
There's no need to write back caches on all CPUs upon seeing a WBINVD exit; ones that a vCPU hasn't run on since the last writeback (or since it was started) can't hold data which may need writing back.
Signed-off-by Jan Beulich <[email protected]>
I find it unlikely that this is an improvement in any way at all.
You're adding a memory allocation, and making the fastpath slower, for all HVM domains even the ~100% of them in practice which never get given a device in the first place.
Just so you can skip the WBINVD side effect on the L1/L2 caches of the CPUs this domain happens not to have run on since the last time they flushed (which is already an under estimate). Note how this does not change the behaviour for the L3 caches, which form the overwhelming majority of the WBINVD overhead in the first place.
So my response was going to be "definitely not without the per-domain 'reduced cacheability permitted' setting we've discussed". And even then, not without numbers suggesting it's a problem in the first place, or at least a better explanation of why it might plausibly be an issue.
But, in writing this, I've realised a real bug.
Cache snoops can occur and pull lines sideways for microarchitectural reasons. And even if we want to hand-wave that away as being unlikely (it is), you can't hand-wave away rogue speculation in the directmap.
By not issuing WBINVD on all cores, you've got a real chance of letting some lines escape the attempt to evict them.
But it's worse than that - even IPIing all cores, there's a speculation pattern which can cause some lines to survive. Rare, sure, but not impossible.
Right now, I'm not sure that WBINVD can even be used safely without the extra careful use of CR0.{CD,NW}, which provides a workaround for native, but nothing helpful for hypervisors...
~Andrew
Re: [PATCH 5/5] x86/HVM: limit cache writeback overhead
Quoted 13 lines · Andrew Cooper
Quoted 5 lines · Jan Beulich
There's no need to write back caches on all CPUs upon seeing a WBINVD exit; ones that a vCPU hasn't run on since the last writeback (or since it was started) can't hold data which may need writing back.
Signed-off-by Jan Beulich <[email protected]>I find it unlikely that this is an improvement in any way at all.
You're adding a memory allocation, and making the fastpath slower, for all HVM domains even the ~100% of them in practice which never get given a device in the first place.
Quoted 5 lines
Just so you can skip the WBINVD side effect on the L1/L2 caches of the CPUs this domain happens not to have run on since the last time they flushed (which is already an under estimate). Note how this does not change the behaviour for the L3 caches, which form the overwhelming majority of the WBINVD overhead in the first place.
You're thinking of only single-node systems here, it seems? I view this change as particularly relevant for node-constrained domains on NUMA systems.
As to "making the fastpath slower": That can only be the __cpumask_set_cpu() added to hvm_do_resume(). What do you suggest? I can certainly add a conditional there (and then I could also make the memory allocation conditional), but I thought a LOCK-less RMW memory operation might better be done in straight line code.
As an aside - after having thought of making this change, I did go and check what KVM does. And (surprise or not) they do exactly this.
Quoted 15 lines
So my response was going to be "definitely not without the per-domain 'reduced cacheability permitted' setting we've discussed". And even then, not without numbers suggesting it's a problem in the first place, or at least a better explanation of why it might plausibly be an issue.
But, in writing this, I've realised a real bug.
Cache snoops can occur and pull lines sideways for microarchitectural reasons. And even if we want to hand-wave that away as being unlikely (it is), you can't hand-wave away rogue speculation in the directmap.
By not issuing WBINVD on all cores, you've got a real chance of letting some lines escape the attempt to evict them.
Quoted 7 lines
But it's worse than that - even IPIing all cores, there's a speculation pattern which can cause some lines to survive. Rare, sure, but not impossible.
Right now, I'm not sure that WBINVD can even be used safely without the extra careful use of CR0.{CD,NW}, which provides a workaround for native, but nothing helpful for hypervisors...
Wait: See the title and the earlier patches. We're not talking of "evict" here anymore, but of "write-back". The few cases of "evict" are left alone by this change. If any of those are affected by what you say (and it looks like some might be), then fixing that definitely is unrelated work. (You may have meant that latter part of your reply like this, but you haven't said so in any way I would recognize.)
Jan