x86/pvshim: improve tlb flush performance
cover x86/pvshim: improve tlb flush performance
Hello,
Enabling PGE in CR4 causes a huge performance penalty when running the shim on AMD hardware, this patch series avoids enabling PGE when in shim mode, and makes a small adjustment in do_tlb_flush to perform a flush by writing to CR3 if PGE is not enabled.
Roger Pau Monne (2):
x86/tlbflush: do not toggle the PGE CR4 bit unless necessary x86/pvshim: do not enable global pages in shim mode
xen/arch/x86/flushtlb.c | 9 +++++---- xen/arch/x86/pv/domain.c | 3 ++- 2 files changed, 7 insertions(+), 5 deletions(-)
Signature · 7 lines
2.24.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 1/2 x86/tlbflush: do not toggle the PGE CR4 bit unless necessary
When PCID is not available Xen does a full tlbflush by toggling the PGE bit in CR4. This is not necessary if PGE is not enabled, since a flush can be performed by writing to CR3 in that case.
Change the code in do_tlb_flush to only toggle the PGE bit in CR4 if it's already enabled, otherwise do the tlb flush by writing to CR3. This is relevant when running virtualized, since hypervisors don't usually trap accesses to CR3 when using hardware assisted paging, but do trap accesses to CR4 specially on AMD hardware, which makes such accesses much more expensive.
--- xen/arch/x86/flushtlb.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
xen/arch/x86/flushtlb.c +5 -4
@@ -84,6 +84,7 @@ static void post_flush(u32 t)static void do_tlb_flush (void ) {unsigned long flags; +unsigned long cr4;u32 t;/* This non-reentrant function is sometimes called in interrupt context. */
@@ -93,13 +94,13 @@ static void do_tlb_flush(void)if ( use_invpcid )invpcid_flush_all (); -else +else if ( (cr4 =read_cr4 ()) &X86_CR4_PGE ) { -unsigned long cr4 =read_cr4 (); - -write_cr4 (cr4 ^X86_CR4_PGE ); +write_cr4 (cr4 & ~X86_CR4_PGE );write_cr4 (cr4); } +else +write_cr3 (read_cr3 ());post_flush (t);
Signature · 7 lines
2.24.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [Xen-devel] [PATCH 1/2] x86/tlbflush: do not toggle the PGE CR4 bit unless necessary
Quoted 4 lines · Roger Pau Monne
When PCID is not available Xen does a full tlbflush by toggling the PGE bit in CR4. This is not necessary if PGE is not enabled, since a flush can be performed by writing to CR3 in that case.
Quoted 8 lines
Change the code in
do_tlb_flushto only toggle the PGE bit in CR4 if it's already enabled, otherwise do the tlb flush by writing to CR3. This is relevant when running virtualized, since hypervisors don't usually trap accesses to CR3 when using hardware assisted paging, but do trap accesses to CR4 specially on AMD hardware, which makes such accesses much more expensive.Signed-off-by Roger Pau Monné <[email protected]>
with one remark:
xen/arch/x86/flushtlb.c static void post_flush(u32 t) · line 87
Quoted 7 lines
xen/arch/x86/flushtlb.c +1
-0@@ -84,6 +84,7 @@ static void post_flush(u32 t)static void do_tlb_flush (void ) {unsigned long flags; +unsigned long cr4;
This would better be merged with the adjacent declaration. Can surely be done while committing.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 2/2 x86/pvshim: do not enable global pages in shim mode
In reply to Roger Pau MonneWhen using global pages a full tlb flush can only be performed by toggling the PGE bit in CR4, which is usually quite expensive in terms of performance when running virtualized. This is specially relevant on AMD hardware, which doesn't have the ability to do selective CR4 trapping, but can also be relevant on Intel if the underlying hypervisor also traps on accesses to the PGE CR4 bit.
In order to avoid this performance penalty, do not use global pages when running in shim mode. Note this is done when running on both Intel or AMD hardware, since older versions of Xen capable of running the shim don't make use of Intel selective CR4 trapping feature and will vmexit on every access to CR4.
The above figures are from a PV shim running on AMD hardware with 32 vCPUs:
PGE enabled, x2APIC mode:
(XEN) Global lock flush_lock: addr=ffff82d0804b01c0, lockval=1adb1adb, not locked
(XEN) lock:1841883(1375128998543), block:1658716(10193054890781)
Average lock time: 746588ns
Average block time: 6145147ns
PGE disabled, x2APIC mode:
(XEN) Global lock flush_lock: addr=ffff82d0804af1c0, lockval=a8bfa8bf, not locked
(XEN) lock:2730175(657505389886), block:2039716(2963768247738)
Average lock time: 240829ns
Average block time: 1453029ns
As seen from the above figures the block time of the flush lock is reduced to approximately 1/3 of the original value.
--- xen/arch/x86/pv/domain.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
xen/arch/x86/pv/domain.c +2 -1
@@ -13,6 +13,7 @@#include <asm/invpcid.h> #include <asm/spec_ctrl.h> #include <asm/pv/domain.h> +#include <asm/pv/shim.h> #include <asm/shadow.h> static __read_mostly enum {
@@ -130,7 +131,7 @@ unsigned long pv_make_cr4(const struct vcpu *v)*/ if ( d->arch.pv.pcid ) cr4 |=X86_CR4_PCIDE ; -else if ( !d->arch.pv.xpti ) +else if ( !d->arch.pv.xpti && !pv_shim ) cr4 |=X86_CR4_PGE ;/*
Signature · 7 lines
2.24.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [Xen-devel] [PATCH 2/2] x86/pvshim: do not enable global pages in shim mode
Quoted 7 lines · Roger Pau Monne
When using global pages a full tlb flush can only be performed by toggling the PGE bit in CR4, which is usually quite expensive in terms of performance when running virtualized. This is specially relevant on AMD hardware, which doesn't have the ability to do selective CR4 trapping, but can also be relevant on Intel if the underlying hypervisor also traps on accesses to the PGE CR4 bit.
Quoted 5 lines
In order to avoid this performance penalty, do not use global pages when running in shim mode. Note this is done when running on both Intel or AMD hardware, since older versions of Xen capable of running the shim don't make use of Intel selective CR4 trapping feature and will vmexit on every access to CR4.
So here you say you do this uniformly because of older Xen. What about newer Xen? Is this still a win (or at least not a loss) there? Independent of underlying hardware? In case of any kind of doubt I think this would want to be command line controllable.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [Xen-devel] [PATCH 2/2] x86/pvshim: do not enable global pages in shim mode
Quoted 13 lines · Jan Beulich
Quoted 7 lines · Roger Pau Monne
When using global pages a full tlb flush can only be performed by toggling the PGE bit in CR4, which is usually quite expensive in terms of performance when running virtualized. This is specially relevant on AMD hardware, which doesn't have the ability to do selective CR4 trapping, but can also be relevant on Intel if the underlying hypervisor also traps on accesses to the PGE CR4 bit.
Quoted 5 lines
In order to avoid this performance penalty, do not use global pages when running in shim mode. Note this is done when running on both Intel or AMD hardware, since older versions of Xen capable of running the shim don't make use of Intel selective CR4 trapping feature and will vmexit on every access to CR4.
Quoted 5 lines
So here you say you do this uniformly because of older Xen. What about newer Xen? Is this still a win (or at least not a loss) there? Independent of underlying hardware? In case of any kind of doubt I think this would want to be command line controllable.
Older Xen has VMExits for all CR4.PGE flips.
Newer Xen (since 4.10? iirc) on Intel hardware (with HAP) arranged for CR4.PGE flips not to vmexit.
There is no ability to cause CR4.PGE flips to not vmexit on AMD, other than to give the guest full control of CR4 which is a BadThing(tm).
I agree that this wants a command line control, but it wants to be enabled by default any time we find ourselves nested on AMD hardware, not just in shim.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [Xen-devel] [PATCH 2/2] x86/pvshim: do not enable global pages in shim mode
Quoted 9 lines · Andrew Cooper
Quoted 8 lines · Jan Beulich
Quoted 7 lines · Roger Pau Monne
When using global pages a full tlb flush can only be performed by toggling the PGE bit in CR4, which is usually quite expensive in terms of performance when running virtualized. This is specially relevant on AMD hardware, which doesn't have the ability to do selective CR4 trapping, but can also be relevant on Intel if the underlying hypervisor also traps on accesses to the PGE CR4 bit.
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
In order to avoid this performance penalty, do not use global pages when running in shim mode. Note this is done when running on both Intel or AMD hardware, since older versions of Xen capable of running the shim don't make use of Intel selective CR4 trapping feature and will vmexit on every access to CR4.
So here you say you do this uniformly because of older Xen. What about newer Xen? Is this still a win (or at least not a loss) there? Independent of underlying hardware?
So on Intel hw that don't trap CR4 PGE accesses disabling PGE seems to make performance slightly worse when doing a make -j8 xen on a 8 vCPU pv-shim guest:
CR4 PGE enabled:
real 2m40.889s
real 2m41.700s
real 2m40.453s
CR4 PGE disabled:
real 2m43.197s
real 2m41.940s
real 2m42.727s
Quoted 9 lines
Quoted 3 lines
In case of
any kind of doubt I think this would want to be command line controllable.Older Xen has VMExits for all CR4.PGE flips.
Newer Xen (since 4.10? iirc) on Intel hardware (with HAP) arranged for CR4.PGE flips not to vmexit.
Quoted 6 lines
There is no ability to cause CR4.PGE flips to not vmexit on AMD, other than to give the guest full control of CR4 which is a BadThing(tm).
I agree that this wants a command line control, but it wants to be enabled by default any time we find ourselves nested on AMD hardware, not just in shim.
Only on AMD hardware? Newer versions of Xen don't trap CR4 PGE writes, but what about other hypervisors?
I think it would be better to avoid using PGE when the hypervisor CPUID bit is set, regardless of whether the hardware is AMD or not. The performance penalty doesn't seem that bad, taking into account that using PGE when CR4 is trapped is much worse. Alternatively we could try to detect how slow a flush from CR4 is and act accordingly, but that seems tricky.
I can add a command line option to force or prevent the usage of PGE.
Thanks, Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [Xen-devel] [PATCH 2/2] x86/pvshim: do not enable global pages in shim mode
Quoted 7 lines · Roger Pau Monné
Quoted 3 lines · Andrew Cooper
I agree that this wants a command line control, but it wants to be enabled by default any time we find ourselves nested on AMD hardware, not just in shim.
Only on AMD hardware? Newer versions of Xen don't trap CR4 PGE writes, but what about other hypervisors?
I guess all hypervisors will strive to force as little exits as possible.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel