vmx/hap: optimize CR4 trapping
v3 cover vmx/hap: optimize CR4 trapping
Hello,
Following two-patch series optimize CR4 access for vmx/hap.
I couldn't figure out a similar approach for AMD, since according to section 15.9 (Instruction Intercepts), you either intercept all access to CR4 or none. In any case a similar optimization for AMD can always be added later.
Thanks, Roger.
Roger Pau Monne (2):
x86/hvm: introduce cr{0,4}_host_mask to store trapped bits of CR
accesses
vmx/hap: optimize CR4 trapping
xen/arch/x86/hvm/svm/vmcb.c | 1 + xen/arch/x86/hvm/vmx/vmcs.c | 1 + xen/arch/x86/hvm/vmx/vmx.c | 39 +++++++++++++++++++++++++++++++++++++++ xen/arch/x86/hvm/vmx/vvmx.c | 2 ++ xen/arch/x86/monitor.c | 5 +++-- xen/include/asm-x86/hvm/vcpu.h | 4 ++++ 6 files changed, 50 insertions(+), 2 deletions(-)
Signature · 7 lines
2.16.1 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v3 1/2 x86/hvm: introduce cr{0, 4}_host_mask to store trapped bits of CR accesses
At the moment this is currently set at VMC{S/B} creation and not changed, but further patches are going to change the CR4 mask at runtime.
---
Cc: Boris Ostrovsky <[email protected]>
Cc: Suravee Suthikulpanit <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Jun Nakajima <[email protected]>
Cc: Kevin Tian <[email protected]>
---
Changes since v2:
- Introduce masks only for CR4 and CR0.
Changes since v1:
- New in this version.
--- xen/arch/x86/hvm/svm/vmcb.c | 1 + xen/arch/x86/hvm/vmx/vmcs.c | 1 + xen/include/asm-x86/hvm/vcpu.h | 4 ++++ 3 files changed, 6 insertions(+)
xen/arch/x86/hvm/svm/vmcb.c +1 -0
@@ -169,6 +169,7 @@ static int construct_vmcb(struct vcpu *v) vmcb->tr.base =0 ; vmcb->tr.limit =0xff ; + v->arch.hvm_vcpu.cr0_host_mask = v->arch.hvm_vcpu.cr4_host_mask = ~0UL ; v->arch.hvm_vcpu.guest_cr[0 ] =X86_CR0_PE |X86_CR0_ET ;hvm_update_guest_cr (v,0 );
xen/arch/x86/hvm/vmx/vmcs.c +1 -0
@@ -1165,6 +1165,7 @@ static int construct_vmcs(struct vcpu *v)__vmwrite (CR0_GUEST_HOST_MASK , ~0UL );__vmwrite (CR4_GUEST_HOST_MASK , ~0UL ); + v->arch.hvm_vcpu.cr0_host_mask = v->arch.hvm_vcpu.cr4_host_mask = ~0UL ;__vmwrite (PAGE_FAULT_ERROR_CODE_MASK ,0 );__vmwrite (PAGE_FAULT_ERROR_CODE_MATCH ,0 );
xen/include/asm-x86/hvm/vcpu.h +4 -0
@@ -156,6 +156,10 @@ struct hvm_vcpu {*/ unsigned long hw_cr[5 ]; +/* Cached copy of the trapped bits of CRs. */ +unsigned long cr0_host_mask, + cr4_host_mask; +struct vlapic vlapic;s64 cache_tsc_offset;u64 guest_time;
Signature · 7 lines
2.16.1 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH v3 1/2] x86/hvm: introduce cr{0, 4}_host_mask to store trapped bits of CR accesses
Quoted 28 lines · Roger Pau Monne
At the moment this is currently set at VMC{S/B} creation and not changed, but further patches are going to change the CR4 mask at runtime.
Signed-off-by Roger Pau Monné <[email protected]>---
Cc: Boris Ostrovsky <[email protected]>
Cc: Suravee Suthikulpanit <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Jun Nakajima <[email protected]>
Cc: Kevin Tian <[email protected]>
---
Changes since v2:
- Introduce masks only for CR4 and CR0.Changes since v1:
- New in this version.--- xen/arch/x86/hvm/svm/vmcb.c | 1 + xen/arch/x86/hvm/vmx/vmcs.c | 1 + xen/include/asm-x86/hvm/vcpu.h | 4 ++++ 3 files changed, 6 insertions(+)xen/arch/x86/hvm/svm/vmcb.c +0
-0
xen/arch/x86/hvm/svm/vmcb.c static int construct_vmcb(struct vcpu *v) · line 174
Quoted 6 lines
xen/arch/x86/hvm/svm/vmcb.c +1
-0@@ -169,6 +169,7 @@ static int construct_vmcb(struct vcpu *v) vmcb->tr.base =0 ; vmcb->tr.limit =0xff ; + v->arch.hvm_vcpu.cr0_host_mask = v->arch.hvm_vcpu.cr4_host_mask = ~0UL ; v->arch.hvm_vcpu.guest_cr[0 ] =X86_CR0_PE |X86_CR0_ET ;hvm_update_guest_cr (v,0 );
Is there a reason for setting those fields? SVM won't use them, will it?
-boris
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH v3 1/2] x86/hvm: introduce cr{0, 4}_host_mask to store trapped bits of CR accesses
Quoted 32 lines · Boris Ostrovsky
Quoted 31 lines · Roger Pau Monne
At the moment this is currently set at VMC{S/B} creation and not changed, but further patches are going to change the CR4 mask at runtime.
Signed-off-by Roger Pau Monné <[email protected]>---
Cc: Boris Ostrovsky <[email protected]>
Cc: Suravee Suthikulpanit <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Jun Nakajima <[email protected]>
Cc: Kevin Tian <[email protected]>
---
Changes since v2:
- Introduce masks only for CR4 and CR0.Changes since v1:
- New in this version.--- xen/arch/x86/hvm/svm/vmcb.c | 1 + xen/arch/x86/hvm/vmx/vmcs.c | 1 + xen/include/asm-x86/hvm/vcpu.h | 4 ++++ 3 files changed, 6 insertions(+)xen/arch/x86/hvm/svm/vmcb.c +0
-0@@ -169,6 +169,7 @@ static int construct_vmcb(struct vcpu *v) vmcb->tr.base =0 ; vmcb->tr.limit =0xff ;
Quoted 6 lines
xen/arch/x86/hvm/svm/vmcb.c static int construct_vmcb(struct vcpu *v) · line 174
Quoted 3 lines
xen/arch/x86/hvm/svm/vmcb.c +1
-0+ v->arch.hvm_vcpu.cr0_host_mask = v->arch.hvm_vcpu.cr4_host_mask = ~0UL ; v->arch.hvm_vcpu.guest_cr[0 ] =X86_CR0_PE |X86_CR0_ET ;hvm_update_guest_cr (v,0 );Is there a reason for setting those fields? SVM won't use them, will it?
No, SVM won't use them but IMHO it's good for coherency to also set them. The initial plan was to do something similar for SVM, but I couldn't find how to do selective CR4 trapping for SVM.
I could move this inside of arch_vmx_struct if it seems better, but I was expecting to be able to do something similar for SVM in order to avoid needlessly trapping CR4.
Thanks, Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH v3 1/2] x86/hvm: introduce cr{0, 4}_host_mask to store trapped bits of CR accesses
Quoted 38 lines · Roger Pau Monné
Quoted 32 lines · Boris Ostrovsky
Quoted 31 lines · Roger Pau Monne
At the moment this is currently set at VMC{S/B} creation and not changed, but further patches are going to change the CR4 mask at runtime.
Signed-off-by Roger Pau Monné <[email protected]>---
Cc: Boris Ostrovsky <[email protected]>
Cc: Suravee Suthikulpanit <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Jun Nakajima <[email protected]>
Cc: Kevin Tian <[email protected]>
---
Changes since v2:
- Introduce masks only for CR4 and CR0.Changes since v1:
- New in this version.--- xen/arch/x86/hvm/svm/vmcb.c | 1 + xen/arch/x86/hvm/vmx/vmcs.c | 1 + xen/include/asm-x86/hvm/vcpu.h | 4 ++++ 3 files changed, 6 insertions(+)xen/arch/x86/hvm/svm/vmcb.c +0
-0@@ -169,6 +169,7 @@ static int construct_vmcb(struct vcpu *v) vmcb->tr.base =0 ; vmcb->tr.limit =0xff ;Quoted 5 lines
xen/arch/x86/hvm/svm/vmcb.c static int construct_vmcb(struct vcpu *v) · line 174
Quoted 3 lines
xen/arch/x86/hvm/svm/vmcb.c +1
-0+ v->arch.hvm_vcpu.cr0_host_mask = v->arch.hvm_vcpu.cr4_host_mask = ~0UL ; v->arch.hvm_vcpu.guest_cr[0 ] =X86_CR0_PE |X86_CR0_ET ;hvm_update_guest_cr (v,0 );Is there a reason for setting those fields? SVM won't use them, will it?
Quoted 7 lines
No, SVM won't use them but IMHO it's good for coherency to also set them. The initial plan was to do something similar for SVM, but I couldn't find how to do selective CR4 trapping for SVM.
I could move this inside of
arch_vmx_structif it seems better, but I was expecting to be able to do something similar for SVM in order to avoid needlessly trapping CR4.
I don't believe SVM has a similar feature. The closest it comes to this is clean bits, but that's still rather different.
So it seems to me you are targeting something very much VMX-specific and so I think it should indeed be part of arch_vmx_struct.
-boris
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH v3 1/2] x86/hvm: introduce cr{0, 4}_host_mask to store trapped bits of CR accesses
Quoted 47 lines · Boris Ostrovsky
Quoted 38 lines · Roger Pau Monné
Quoted 32 lines · Boris Ostrovsky
Quoted 31 lines · Roger Pau Monne
At the moment this is currently set at VMC{S/B} creation and not changed, but further patches are going to change the CR4 mask at runtime.
Signed-off-by Roger Pau Monné <[email protected]>---
Cc: Boris Ostrovsky <[email protected]>
Cc: Suravee Suthikulpanit <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Jun Nakajima <[email protected]>
Cc: Kevin Tian <[email protected]>
---
Changes since v2:
- Introduce masks only for CR4 and CR0.Changes since v1:
- New in this version.--- xen/arch/x86/hvm/svm/vmcb.c | 1 + xen/arch/x86/hvm/vmx/vmcs.c | 1 + xen/include/asm-x86/hvm/vcpu.h | 4 ++++ 3 files changed, 6 insertions(+)xen/arch/x86/hvm/svm/vmcb.c +0
-0@@ -169,6 +169,7 @@ static int construct_vmcb(struct vcpu *v) vmcb->tr.base =0 ; vmcb->tr.limit =0xff ;Quoted 5 lines
xen/arch/x86/hvm/svm/vmcb.c static int construct_vmcb(struct vcpu *v) · line 174
Quoted 3 lines
xen/arch/x86/hvm/svm/vmcb.c +1
-0+ v->arch.hvm_vcpu.cr0_host_mask = v->arch.hvm_vcpu.cr4_host_mask = ~0UL ; v->arch.hvm_vcpu.guest_cr[0 ] =X86_CR0_PE |X86_CR0_ET ;hvm_update_guest_cr (v,0 );Is there a reason for setting those fields? SVM won't use them, will it?
Quoted 7 lines
No, SVM won't use them but IMHO it's good for coherency to also set them. The initial plan was to do something similar for SVM, but I couldn't find how to do selective CR4 trapping for SVM.
I could move this inside of
arch_vmx_structif it seems better, but I was expecting to be able to do something similar for SVM in order to avoid needlessly trapping CR4.
Quoted 5 lines
I don't believe SVM has a similar feature. The closest it comes to this is clean bits, but that's still rather different.
So it seems to me you are targeting something very much VMX-specific and so I think it should indeed be part of
arch_vmx_struct.
Thanks, will change that and wait for further feedback before resending.
Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH v3 1/2] x86/hvm: introduce cr{0, 4}_host_mask to store trapped bits of CR accesses
In reply to Roger Pau MonneQuoted 7 lines
From: Roger Pau Monne [mailto:[email protected]]
Sent: Tuesday, February 20, 2018 4:57 PMAt the moment this is currently set at VMC{S/B} creation and not changed, but further patches are going to change the CR4 mask at runtime.
Signed-off-by Roger Pau Monné <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v3 2/2 vmx/hap: optimize CR4 trapping
In reply to Roger Pau MonneThere a bunch of bits in CR4 that should be allowed to be set directly by the guest without requiring Xen intervention, currently this is already done by passing through guest writes into the CR4 used when running in non-root mode, but taking an expensive vmexit in order to do so.
xenalyze reports the following when running a PV guest in shim mode:
CR_ACCESS 3885950 6.41s 17.04% 3957 cyc { 2361| 3378| 7920}
cr4 3885940 6.41s 17.04% 3957 cyc { 2361| 3378| 7920}
cr3 1 0.00s 0.00% 3480 cyc { 3480| 3480| 3480}
*[ 0] 1 0.00s 0.00% 3480 cyc { 3480| 3480| 3480} cr0 7 0.00s 0.00% 7112 cyc { 3248| 5960|17480}
clts 2 0.00s 0.00% 4588 cyc { 3456| 5720| 5720}
After this change this turns into:
CR_ACCESS 12 0.00s 0.00% 9972 cyc { 3680|11024|24032}
cr4 2 0.00s 0.00% 17528 cyc {11024|24032|24032}
cr3 1 0.00s 0.00% 3680 cyc { 3680| 3680| 3680}
*[ 0] 1 0.00s 0.00% 3680 cyc { 3680| 3680| 3680} cr0 7 0.00s 0.00% 9209 cyc { 4184| 7848|17488}
clts 2 0.00s 0.00% 8232 cyc { 5352|11112|11112}
Note that this optimized trapping is currently only applied to guests running with HAP on Intel hardware. If using shadow paging more CR4 bits need to be unconditionally trapped, which makes this approach unlikely to yield any important performance improvements.
---
Cc: Jun Nakajima <[email protected]>
Cc: Kevin Tian <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Razvan Cojocaru <[email protected]>
Cc: Tamas K Lengyel <[email protected]>
---
Changes since v2:
- Use cr4_host_mask.
Changes since v1:
- Use the mask_cr variable in order to cache the cr4 mask.
- Take into account write_ctrlreg_mask when introspection is enabled.
--- xen/arch/x86/hvm/vmx/vmx.c | 39 +++++++++++++++++++++++++++++++++++++++ xen/arch/x86/hvm/vmx/vvmx.c | 2 ++ xen/arch/x86/monitor.c | 5 +++-- 3 files changed, 44 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/vmx/vmx.c +39 -0
@@ -1684,6 +1684,36 @@ static void vmx_update_guest_cr(struct vcpu *v, unsigned int cr) }__vmwrite (GUEST_CR4 , v->arch.hvm_vcpu.hw_cr[4 ]); + +if ( !paging_mode_hap (v->domain) ) +/* +* Shadow path has not been optimized because it requires +* unconditionally trapping more CR4 bits, at which point the +* performance benefit of doing this is quite dubious. +*/ + v->arch.hvm_vcpu.cr4_host_mask = ~0UL ; +else + { +/* +* Update CR4 host mask to only trap when the guest tries to set +* bits that are controlled by the hypervisor. +*/ + v->arch.hvm_vcpu.cr4_host_mask =HVM_CR4_HOST_MASK |X86_CR4_PKE | + ~hvm_cr4_guest_valid_bits (v,0 ); + v->arch.hvm_vcpu.cr4_host_mask |= v->arch.hvm_vmx.vmx_realmode ? +X86_CR4_VME :0 ; + v->arch.hvm_vcpu.cr4_host_mask |= !hvm_paging_enabled (v) ? + (X86_CR4_PSE |X86_CR4_SMEP | +X86_CR4_SMAP ) + :0 ; +if ( v->domain->arch.monitor.write_ctrlreg_enabled & +monitor_ctrlreg_bitmask (VM_EVENT_X86_CR4 ) ) + v->arch.hvm_vcpu.cr4_host_mask |= + ~v->domain->arch.monitor.write_ctrlreg_mask[VM_EVENT_X86_CR4 ]; + + } +__vmwrite (CR4_GUEST_HOST_MASK , v->arch.hvm_vcpu.cr4_host_mask); +break ;case 2 :
@@ -3512,6 +3542,15 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)if (paging_mode_hap (v->domain) ) { +/* +* Xen allows the guest to modify some CR4 bits directly, update cached +* values to match. +*/ +__vmread (GUEST_CR4 , &v->arch.hvm_vcpu.hw_cr[4 ]); + v->arch.hvm_vcpu.guest_cr[4 ] &= v->arch.hvm_vcpu.cr4_host_mask; + v->arch.hvm_vcpu.guest_cr[4 ] |= v->arch.hvm_vcpu.hw_cr[4 ] & + ~v->arch.hvm_vcpu.cr4_host_mask; +__vmread (GUEST_CR3 , &v->arch.hvm_vcpu.hw_cr[3 ]);if (vmx_unrestricted_guest (v) ||hvm_paging_enabled (v) ) v->arch.hvm_vcpu.guest_cr[3 ] = v->arch.hvm_vcpu.hw_cr[3 ];
xen/arch/x86/hvm/vmx/vvmx.c +2 -0
@@ -1100,6 +1100,8 @@ static void load_shadow_guest_state(struct vcpu *v) cr_read_shadow = (get_vvmcs (v,GUEST_CR4 ) & ~cr_gh_mask) | (get_vvmcs (v,CR4_READ_SHADOW ) & cr_gh_mask);__vmwrite (CR4_READ_SHADOW , cr_read_shadow); +/* Add the nested host mask to the one set by vmx_update_guest_cr. */ +__vmwrite (CR4_GUEST_HOST_MASK , cr_gh_mask | v->arch.hvm_vcpu.cr4_host_mask);/* TODO: CR3 target control */ }
xen/arch/x86/monitor.c +3 -2
@@ -189,10 +189,11 @@ int arch_monitor_domctl_event(struct domain *d, ad->monitor.write_ctrlreg_enabled &= ~ctrlreg_bitmask; } -if (VM_EVENT_X86_CR3 == mop->u.mov_to_cr.index ) +if (VM_EVENT_X86_CR3 == mop->u.mov_to_cr.index || +VM_EVENT_X86_CR4 == mop->u.mov_to_cr.index ) {struct vcpu *v; -/* Latches new CR3 mask through CR0 code. */ +/* Latches new CR3 or CR4 mask through CR0 code. */ for_each_vcpu ( d, v )hvm_update_guest_cr (v,0 ); }
Signature · 7 lines
2.16.1 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 98 lines
From: Roger Pau Monne [mailto:[email protected]]
Sent: Tuesday, February 20, 2018 4:57 PMThere a bunch of bits in CR4 that should be allowed to be set directly by the guest without requiring Xen intervention, currently this is already done by passing through guest writes into the CR4 used when running in non-root mode, but taking an expensive vmexit in order to do so.
xenalyze reports the following when running a PV guest in shim mode:
CR_ACCESS 3885950 6.41s 17.04% 3957 cyc { 2361| 3378| 7920} cr4 3885940 6.41s 17.04% 3957 cyc { 2361| 3378| 7920} cr3 1 0.00s 0.00% 3480 cyc { 3480| 3480| 3480} *[ 0] 1 0.00s 0.00% 3480 cyc { 3480| 3480| 3480}cr0 7 0.00s 0.00% 7112 cyc { 3248| 5960|17480}
clts 2 0.00s 0.00% 4588 cyc { 3456| 5720| 5720}After this change this turns into:
CR_ACCESS 12 0.00s 0.00% 9972 cyc { 3680|11024|24032} cr4 2 0.00s 0.00% 17528 cyc {11024|24032|24032} cr3 1 0.00s 0.00% 3680 cyc { 3680| 3680| 3680} *[ 0] 1 0.00s 0.00% 3680 cyc { 3680| 3680| 3680}cr0 7 0.00s 0.00% 9209 cyc { 4184| 7848|17488}
clts 2 0.00s 0.00% 8232 cyc { 5352|11112|11112}Note that this optimized trapping is currently only applied to guests running with HAP on Intel hardware. If using shadow paging more CR4 bits need to be unconditionally trapped, which makes this approach unlikely to yield any important performance improvements.
Reported-by Andrew Cooper <[email protected]>Signed-off-by Roger Pau Monné <[email protected]>Acked-by Razvan Cojocaru <[email protected]>---
Cc: Jun Nakajima <[email protected]>
Cc: Kevin Tian <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Razvan Cojocaru <[email protected]>
Cc: Tamas K Lengyel <[email protected]>
---
Changes since v2:
- Usecr4_host_mask.Changes since v1:
- Use themask_crvariable in order to cache the cr4 mask.
- Take into accountwrite_ctrlreg_maskwhen introspection is enabled.--- xen/arch/x86/hvm/vmx/vmx.c | 39 +++++++++++++++++++++++++++++++++++++++ xen/arch/x86/hvm/vmx/vvmx.c | 2 ++ xen/arch/x86/monitor.c | 5 +++-- 3 files changed, 44 insertions(+), 2 deletions(-)xen/arch/x86/hvm/vmx/vmx.c +15
-0@@ -1684,6 +1684,36 @@ static void vmx_update_guest_cr(struct vcpu *v, unsigned int cr) }__vmwrite (GUEST_CR4 , v->arch.hvm_vcpu.hw_cr[4 ]); + +if ( !paging_mode_hap (v->domain) ) +/* +* Shadow path has not been optimized because it requires +* unconditionally trapping more CR4 bits, at which point the +* performance benefit of doing this is quite dubious. +*/ + v->arch.hvm_vcpu.cr4_host_mask = ~0UL ; +else + { +/* +* Update CR4 host mask to only trap when the guest tries to set +* bits that are controlled by the hypervisor. +*/ + v->arch.hvm_vcpu.cr4_host_mask = HVM_CR4_HOST_MASK |
X86_CR4_PKE|xen/arch/x86/hvm/vmx/vmx.c +2
-0+ ~hvm_cr4_guest_valid_bits (v,0 ); + v->arch.hvm_vcpu.cr4_host_mask |= v-Quoted 1 line
arch.hvm_vmx.vmx_realmode ?
xen/arch/x86/hvm/vmx/vmx.c +9
-0+ X86_CR4_VME :0 ; + v->arch.hvm_vcpu.cr4_host_mask |= !hvm_paging_enabled (v) ? + (X86_CR4_PSE |X86_CR4_SMEP | +X86_CR4_SMAP ) + :0 ; +if ( v->domain->arch.monitor.write_ctrlreg_enabled & +monitor_ctrlreg_bitmask (VM_EVENT_X86_CR4 ) ) + v->arch.hvm_vcpu.cr4_host_mask |= + ~v->domain-Quoted 1 line
arch.monitor.write_ctrlreg_mask[VM_EVENT_X86_CR4];
xen/arch/x86/hvm/vmx/vmx.c +3
-0+ + } + __vmwrite(CR4_GUEST_HOST_MASK, v-Quoted 1 line
arch.hvm_vcpu.cr4_host_mask);
Quoted 1 line
+
what about doing a comparison to avoid vmwrite if nothing changed?
Thanks
Kevin
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 93 lines · Kevin
Quoted 93 lines
From: Roger Pau Monne [mailto:[email protected]]
Sent: Tuesday, February 20, 2018 4:57 PMThere a bunch of bits in CR4 that should be allowed to be set directly by the guest without requiring Xen intervention, currently this is already done by passing through guest writes into the CR4 used when running in non-root mode, but taking an expensive vmexit in order to do so.
xenalyze reports the following when running a PV guest in shim mode:
CR_ACCESS 3885950 6.41s 17.04% 3957 cyc { 2361| 3378| 7920} cr4 3885940 6.41s 17.04% 3957 cyc { 2361| 3378| 7920} cr3 1 0.00s 0.00% 3480 cyc { 3480| 3480| 3480} *[ 0] 1 0.00s 0.00% 3480 cyc { 3480| 3480| 3480}cr0 7 0.00s 0.00% 7112 cyc { 3248| 5960|17480}
clts 2 0.00s 0.00% 4588 cyc { 3456| 5720| 5720}After this change this turns into:
CR_ACCESS 12 0.00s 0.00% 9972 cyc { 3680|11024|24032} cr4 2 0.00s 0.00% 17528 cyc {11024|24032|24032} cr3 1 0.00s 0.00% 3680 cyc { 3680| 3680| 3680} *[ 0] 1 0.00s 0.00% 3680 cyc { 3680| 3680| 3680}cr0 7 0.00s 0.00% 9209 cyc { 4184| 7848|17488}
clts 2 0.00s 0.00% 8232 cyc { 5352|11112|11112}Note that this optimized trapping is currently only applied to guests running with HAP on Intel hardware. If using shadow paging more CR4 bits need to be unconditionally trapped, which makes this approach unlikely to yield any important performance improvements.
Reported-by Andrew Cooper <[email protected]>Signed-off-by Roger Pau Monné <[email protected]>Acked-by Razvan Cojocaru <[email protected]>---
Cc: Jun Nakajima <[email protected]>
Cc: Kevin Tian <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Razvan Cojocaru <[email protected]>
Cc: Tamas K Lengyel <[email protected]>
---
Changes since v2:
- Usecr4_host_mask.Changes since v1:
- Use themask_crvariable in order to cache the cr4 mask.
- Take into accountwrite_ctrlreg_maskwhen introspection is enabled.--- xen/arch/x86/hvm/vmx/vmx.c | 39 +++++++++++++++++++++++++++++++++++++++ xen/arch/x86/hvm/vmx/vvmx.c | 2 ++ xen/arch/x86/monitor.c | 5 +++-- 3 files changed, 44 insertions(+), 2 deletions(-)xen/arch/x86/hvm/vmx/vmx.c +15
-0@@ -1684,6 +1684,36 @@ static void vmx_update_guest_cr(struct vcpu *v, unsigned int cr) }__vmwrite (GUEST_CR4 , v->arch.hvm_vcpu.hw_cr[4 ]); + +if ( !paging_mode_hap (v->domain) ) +/* +* Shadow path has not been optimized because it requires +* unconditionally trapping more CR4 bits, at which point the +* performance benefit of doing this is quite dubious. +*/ + v->arch.hvm_vcpu.cr4_host_mask = ~0UL ; +else + { +/* +* Update CR4 host mask to only trap when the guest tries to set +* bits that are controlled by the hypervisor. +*/ + v->arch.hvm_vcpu.cr4_host_mask = HVM_CR4_HOST_MASK |
X86_CR4_PKE|xen/arch/x86/hvm/vmx/vmx.c +2
-0+ ~hvm_cr4_guest_valid_bits (v,0 ); + v->arch.hvm_vcpu.cr4_host_mask |= v-Quoted 1 line
arch.hvm_vmx.vmx_realmode ?
xen/arch/x86/hvm/vmx/vmx.c +9
-0+ X86_CR4_VME :0 ; + v->arch.hvm_vcpu.cr4_host_mask |= !hvm_paging_enabled (v) ? + (X86_CR4_PSE |X86_CR4_SMEP | +X86_CR4_SMAP ) + :0 ; +if ( v->domain->arch.monitor.write_ctrlreg_enabled & +monitor_ctrlreg_bitmask (VM_EVENT_X86_CR4 ) ) + v->arch.hvm_vcpu.cr4_host_mask |= + ~v->domain-
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
arch.monitor.write_ctrlreg_mask[VM_EVENT_X86_CR4];
xen/arch/x86/hvm/vmx/vmx.c +3
-0+ + } + __vmwrite(CR4_GUEST_HOST_MASK, v-Quoted 1 line
arch.hvm_vcpu.cr4_host_mask);
+
what about doing a comparison to avoid vmwrite if nothing changed?
I can do that, but it doesn't seem like other writes in the same function do this (see GUEST_CR4 unconditional write above for example).
Is it really better from a performance PoV to avoid the write? (or take the penalty of doing a read + write?)
Thanks, Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Tian, Kevin
Reviewed-byQuoted 106 lines
From: Roger Pau Monné [mailto:[email protected]]
Sent: Friday, February 23, 2018 6:18 PMQuoted 56 lines · Kevin
Quoted 56 lines
From: Roger Pau Monne [mailto:[email protected]]
Sent: Tuesday, February 20, 2018 4:57 PMThere a bunch of bits in CR4 that should be allowed to be set directly by the guest without requiring Xen intervention, currently this is already done by passing through guest writes into the CR4 used when running in non-root mode, but taking an expensive vmexit in order to do so.
xenalyze reports the following when running a PV guest in shim mode:
CR_ACCESS 3885950 6.41s 17.04% 3957 cyc { 2361| 3378| 7920} cr4 3885940 6.41s 17.04% 3957 cyc { 2361| 3378| 7920} cr3 1 0.00s 0.00% 3480 cyc { 3480| 3480| 3480} *[ 0] 1 0.00s 0.00% 3480 cyc { 3480| 3480| 3480}cr0 7 0.00s 0.00% 7112 cyc { 3248| 5960|17480}
clts 2 0.00s 0.00% 4588 cyc { 3456| 5720| 5720}After this change this turns into:
CR_ACCESS 12 0.00s 0.00% 9972 cyc { 3680|11024|24032} cr4 2 0.00s 0.00% 17528 cyc {11024|24032|24032} cr3 1 0.00s 0.00% 3680 cyc { 3680| 3680| 3680} *[ 0] 1 0.00s 0.00% 3680 cyc { 3680| 3680| 3680}cr0 7 0.00s 0.00% 9209 cyc { 4184| 7848|17488}
clts 2 0.00s 0.00% 8232 cyc { 5352|11112|11112}Note that this optimized trapping is currently only applied to guests running with HAP on Intel hardware. If using shadow paging more CR4 bits need to be unconditionally trapped, which makes this approach unlikely to yield any important performance improvements.
Reported-by Andrew Cooper <[email protected]>Signed-off-by Roger Pau Monné <[email protected]>Acked-by Razvan Cojocaru <[email protected]>---
Cc: Jun Nakajima <[email protected]>
Cc: Kevin Tian <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Razvan Cojocaru <[email protected]>
Cc: Tamas K Lengyel <[email protected]>
---
Changes since v2:
- Usecr4_host_mask.Changes since v1:
- Use themask_crvariable in order to cache the cr4 mask.
- Take into accountwrite_ctrlreg_maskwhen introspection is enabled.--- xen/arch/x86/hvm/vmx/vmx.c | 39 +++++++++++++++++++++++++++++++++++++++ xen/arch/x86/hvm/vmx/vvmx.c | 2 ++ xen/arch/x86/monitor.c | 5 +++-- 3 files changed, 44 insertions(+), 2 deletions(-)Quoted 1 line
Review
Quoted 1 line
(quoted diff) +0
-0
b/xen/arch/x86/hvm/vmx/vmx.cQuoted 4 lines
xen/arch/x86/hvm/vmx/vmx.c static void vmx_update_guest_cr(struct vcpu
Quoted 4 lines
index
5cd689e823..27cbbe8823100644xen/arch/x86/hvm/vmx/vmx.c +0
-0@@ -1684,6 +1684,36 @@ static void vmx_update_guest_cr(struct vcpu*v,
Quoted 39 lines
Quoted 38 lines
xen/arch/x86/hvm/vmx/vmx.c +15
-0unsigned int cr) }__vmwrite (GUEST_CR4 , v->arch.hvm_vcpu.hw_cr[4 ]); + +if ( !paging_mode_hap (v->domain) ) +/* +* Shadow path has not been optimized because it requires +* unconditionally trapping more CR4 bits, at which point the +* performance benefit of doing this is quite dubious. +*/ + v->arch.hvm_vcpu.cr4_host_mask = ~0UL ; +else + { +/* +* Update CR4 host mask to only trap when the guest tries to set +* bits that are controlled by the hypervisor. +*/ + v->arch.hvm_vcpu.cr4_host_mask = HVM_CR4_HOST_MASK |
X86_CR4_PKE|xen/arch/x86/hvm/vmx/vmx.c +2
-0+ ~hvm_cr4_guest_valid_bits (v,0 ); + v->arch.hvm_vcpu.cr4_host_mask |= v-Quoted 1 line
arch.hvm_vmx.vmx_realmode ?
xen/arch/x86/hvm/vmx/vmx.c +9
-0+ X86_CR4_VME :0 ; + v->arch.hvm_vcpu.cr4_host_mask |= !hvm_paging_enabled (v) ? + (X86_CR4_PSE |X86_CR4_SMEP | +X86_CR4_SMAP ) + :0 ; +if ( v->domain->arch.monitor.write_ctrlreg_enabled & +monitor_ctrlreg_bitmask (VM_EVENT_X86_CR4 ) ) + v->arch.hvm_vcpu.cr4_host_mask |= + ~v->domain-Quoted 1 line
arch.monitor.write_ctrlreg_mask[VM_EVENT_X86_CR4];
xen/arch/x86/hvm/vmx/vmx.c +3
-0+ + } + __vmwrite(CR4_GUEST_HOST_MASK, v-Quoted 1 line
arch.hvm_vcpu.cr4_host_mask);
+
Quoted 9 lines
Quoted 1 line
what about doing a comparison to avoid vmwrite if nothing changed?
I can do that, but it doesn't seem like other writes in the same function do this (see
GUEST_CR4unconditional write above for example).Is it really better from a performance PoV to avoid the write? (or take the penalty of doing a read + write?)
not much. just personal favorite. :-)
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 15 lines · Kevin
Quoted 15 lines
From: Roger Pau Monné [mailto:[email protected]]
Sent: Friday, February 23, 2018 6:18 PMQuoted 6 lines · Kevin
Quoted 4 lines
From: Roger Pau Monne [mailto:[email protected]]
+ __vmwrite(CR4_GUEST_HOST_MASK, v-Quoted 1 line
arch.hvm_vcpu.cr4_host_mask);
+
what about doing a comparison to avoid vmwrite if nothing changed?
I can do that, but it doesn't seem like other writes in the same function do this (see
GUEST_CR4unconditional write above for example).
Quoted 7 lines
Quoted 3 lines
Is it really better from a performance PoV to avoid the write? (or take the penalty of doing a read + write?)
not much. just personal favorite. :-)
Reviewed-by Kevin Tian <[email protected]>
The AMD guys requested some changes so I had to slightly change the patch and didn't keep you ack. Could you please take a look at:
https://marc.info/?l=xen-devel&m=151963964317894
Thanks, Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel