nSVM/SVM features
v4 cover nSVM/SVM features
This is a set of patches for optional SVM nested features.
V4: rebased on top of kvm/queue + my patch series 'SVM fixes + refactoring' and incorporated all review feedback.
This was tested with kvm unit test, running on L0,L1, and L2, and no new failures were seen.
This time I also tested this with all new features disabled in L1, and in L2, to avoid repeating an issue I had in nested tsc scaling.
Best regards,
Maxim Levitsky
Maxim Levitsky (6):
KVM: x86: nSVM: correctly virtualize LBR msrs when L2 is running
KVM: x86: nSVM: implement nested LBR virtualization
KVM: x86: nSVM: support PAUSE filtering when L0 doesn't intercept
PAUSE
KVM: x86: nSVM: implement nested vGIF
KVM: x86: allow per cpu apicv inhibit reasons
KVM: x86: SVM: allow AVIC to co-exist with a nested guest running
arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 6 ++ arch/x86/kvm/svm/avic.c | 7 ++ arch/x86/kvm/svm/nested.c | 83 +++++++++++++-- arch/x86/kvm/svm/svm.c | 162 +++++++++++++++++++++++------ arch/x86/kvm/svm/svm.h | 41 ++++++-- arch/x86/kvm/x86.c | 14 ++- 7 files changed, 264 insertions(+), 50 deletions(-)
Signature · 1 line
2.26.3
PATCH v4 1/6 KVM: x86: nSVM: correctly virtualize LBR msrs when L2 is running
When L2 is running without LBR virtualization, we should ensure that L1's LBR msrs continue to update as usual.
--- arch/x86/kvm/svm/nested.c | 12 +++++ arch/x86/kvm/svm/svm.c | 98 +++++++++++++++++++++++++++++++-------- arch/x86/kvm/svm/svm.h | 2 + 3 files changed, 93 insertions(+), 19 deletions(-)
arch/x86/kvm/svm/nested.c +12 -0
@@ -536,6 +536,7 @@ void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm)static void nested_vmcb02_prepare_save (struct vcpu_svm *svm,struct vmcb *vmcb12) {bool new_vmcb12 = false; +struct vmcb *vmcb01 = svm->vmcb01.ptr;struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;nested_vmcb02_compute_g_pat (svm);
@@ -586,6 +587,9 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12 svm->vcpu.arch.dr6 = svm->nested.save.dr6 |DR6_ACTIVE_LOW ;vmcb_mark_dirty (vmcb02,VMCB_DR ); } + +if (unlikely (vmcb01->control.virt_ext &LBR_CTL_ENABLE_MASK )) +svm_copy_lbrs (vmcb02, vmcb01); }static void nested_vmcb02_prepare_control (struct vcpu_svm *svm)
@@ -645,6 +649,9 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm) vmcb02->control.event_inj = svm->nested.ctl.event_inj; vmcb02->control.event_inj_err = svm->nested.ctl.event_inj_err; + vmcb02->control.virt_ext = vmcb01->control.virt_ext & +LBR_CTL_ENABLE_MASK ; +if (!nested_vmcb_needs_vls_intercept (svm)) vmcb02->control.virt_ext |=VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK ;
@@ -912,6 +919,11 @@ int nested_svm_vmexit(struct vcpu_svm *svm)svm_switch_vmcb (svm, &svm->vmcb01); +if (unlikely (vmcb01->control.virt_ext &LBR_CTL_ENABLE_MASK )) { +svm_copy_lbrs (vmcb01, vmcb02); +svm_update_lbrv (vcpu); + } +/* * On vmexit the GIF is set to false and * no event can be injected in L1.
arch/x86/kvm/svm/svm.c +79 -19
134 lines not on this page · raw message
arch/x86/kvm/svm/svm.h +2 -0
@@ -492,6 +492,8 @@ u32 svm_msrpm_offset(u32 msr);u32 *svm_vcpu_alloc_msrpm (void );void svm_vcpu_init_msrpm (struct kvm_vcpu *vcpu,u32 *msrpm);void svm_vcpu_free_msrpm (u32 *msrpm); +void svm_copy_lbrs (struct vmcb *from_vmcb,struct vmcb *to_vmcb); +void svm_update_lbrv (struct kvm_vcpu *vcpu);int svm_set_efer (struct kvm_vcpu *vcpu,u64 efer);void svm_set_cr0 (struct kvm_vcpu *vcpu,unsigned long cr0);
Signature · 1 line
2.26.3
PATCH v4 2/6 KVM: x86: nSVM: implement nested LBR virtualization
In reply to Maxim LevitskyThis was tested with kvm-unit-test that was developed for this purpose.
--- arch/x86/kvm/svm/nested.c | 19 +++++++++++++++++-- arch/x86/kvm/svm/svm.c | 7 +++++++ arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 25 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/nested.c +17 -2
@@ -588,8 +588,17 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12vmcb_mark_dirty (vmcb02,VMCB_DR ); } -if (unlikely (vmcb01->control.virt_ext &LBR_CTL_ENABLE_MASK )) +if (unlikely (svm->lbrv_enabled && (svm->nested.ctl.virt_ext &LBR_CTL_ENABLE_MASK ))) { +/* Copy LBR related registers from vmcb12, +* but make sure that we only pick LBR enable bit from the guest. +*/ +svm_copy_lbrs (vmcb02, vmcb12); + vmcb02->save.dbgctl &=LBR_CTL_ENABLE_MASK ; +svm_update_lbrv (&svm->vcpu); + + }else if (unlikely (vmcb01->control.virt_ext &LBR_CTL_ENABLE_MASK )) {svm_copy_lbrs (vmcb02, vmcb01); + } }static void nested_vmcb02_prepare_control (struct vcpu_svm *svm)
@@ -651,6 +660,9 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm) vmcb02->control.virt_ext = vmcb01->control.virt_ext &LBR_CTL_ENABLE_MASK ; +if (svm->lbrv_enabled) + vmcb02->control.virt_ext |= + (svm->nested.ctl.virt_ext &LBR_CTL_ENABLE_MASK );if (!nested_vmcb_needs_vls_intercept (svm)) vmcb02->control.virt_ext |=VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK ;
@@ -919,7 +931,10 @@ int nested_svm_vmexit(struct vcpu_svm *svm)svm_switch_vmcb (svm, &svm->vmcb01); -if (unlikely (vmcb01->control.virt_ext &LBR_CTL_ENABLE_MASK )) { +if (unlikely (svm->lbrv_enabled && (svm->nested.ctl.virt_ext &LBR_CTL_ENABLE_MASK ))) { +svm_copy_lbrs (vmcb12, vmcb02); +svm_update_lbrv (vcpu); + }else if (unlikely (vmcb01->control.virt_ext &LBR_CTL_ENABLE_MASK )) {svm_copy_lbrs (vmcb01, vmcb02);svm_update_lbrv (vcpu); }
arch/x86/kvm/svm/svm.c +7 -0
@@ -878,6 +878,10 @@ void svm_update_lbrv(struct kvm_vcpu *vcpu)bool current_enable_lbrv = !!(svm->vmcb->control.virt_ext &LBR_CTL_ENABLE_MASK ); +if (unlikely (is_guest_mode (vcpu) && svm->lbrv_enabled)) +if (unlikely (svm->nested.ctl.virt_ext &LBR_CTL_ENABLE_MASK )) + enable_lbrv = true; +if (enable_lbrv == current_enable_lbrv)return ;
@@ -4012,6 +4016,7 @@ static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)guest_cpuid_has (vcpu,X86_FEATURE_NRIPS ); svm->tsc_scaling_enabled = tsc_scaling &&guest_cpuid_has (vcpu,X86_FEATURE_TSCRATEMSR ); + svm->lbrv_enabled = lbrv &&guest_cpuid_has (vcpu,X86_FEATURE_LBRV ); svm->v_vmload_vmsave_enabled = vls &&guest_cpuid_has (vcpu,X86_FEATURE_V_VMSAVE_VMLOAD );
@@ -4765,6 +4770,8 @@ static __init void svm_set_cpu_caps(void)if (vls)kvm_cpu_cap_set (X86_FEATURE_V_VMSAVE_VMLOAD ); +if (lbrv) +kvm_cpu_cap_set (X86_FEATURE_LBRV );/* Nested VM can receive #VMEXIT instead of triggering #GP */ kvm_cpu_cap_set (X86_FEATURE_SVME_ADDR_CHK );
arch/x86/kvm/svm/svm.h +1 -0
@@ -236,6 +236,7 @@ struct vcpu_svm { bool nrips_enabled : 1; bool tsc_scaling_enabled : 1; bool v_vmload_vmsave_enabled : 1; + bool lbrv_enabled : 1;u32 ldr_reg;u32 dfr_reg;
Signature · 1 line
2.26.3
Paolo Bonzini
maintainerRe: [PATCH v4 2/6] KVM: x86: nSVM: implement nested LBR virtualization
Review
Quoted 5 lines · Maxim Levitsky
(quoted diff) +5
-0+ /* Copy LBR related registers from vmcb12, + * but make sure that we only pick LBR enable bit from the guest. + */ + svm_copy_lbrs(vmcb02, vmcb12); + vmcb02->save.dbgctl &= LBR_CTL_ENABLE_MASK;
I still do not understand why it is not copying all bits outside DEBUGCTL_RESERVED_BITS. That is:
arch/x86/kvm/svm/nested.c +4 -3
@@ -589,11 +589,12 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12 }if (unlikely (svm->lbrv_enabled && (svm->nested.ctl.virt_ext &LBR_CTL_ENABLE_MASK ))) { -/* Copy LBR related registers from vmcb12, -* but make sure that we only pick LBR enable bit from the guest. +/* +* Reserved bits of DEBUGCTL are ignored. Be consistent with +* svm_set_msr's definition of reserved bits. */ svm_copy_lbrs (vmcb02, vmcb12); - vmcb02->save.dbgctl &=LBR_CTL_ENABLE_MASK ; + vmcb02->save.dbgctl &= ~DEBUGCTL_RESERVED_BITS ;svm_update_lbrv (&svm->vcpu); }else if (unlikely (vmcb01->control.virt_ext &LBR_CTL_ENABLE_MASK )) {
arch/x86/kvm/svm/svm.c +0 -2
@@ -62,8 +62,6 @@ MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);#define SEG_TYPE_LDT 2#define SEG_TYPE_BUSY_TSS16 3 -#define DEBUGCTL_RESERVED_BITS (~(0x3fULL)) -static bool erratum_383_found__read_mostly ;u32 msrpm_offsets[MSRPM_OFFSETS ]__read_mostly ;
arch/x86/kvm/svm/svm.h +2 -0
@@ -487,6 +487,8 @@ static inline bool nested_npt_enabled(struct vcpu_svm *svm)/* svm.c */ #define MSR_INVALID 0xffffffffU +#define DEBUGCTL_RESERVED_BITS (~(0x3fULL)) +extern bool dump_invalid_vmcb;u32 svm_msrpm_offset (u32 msr);
Quoted 4 lines
(quoted diff) +3
-0+ svm_update_lbrv(&svm->vcpu); + + } else if (unlikely(vmcb01->control.virt_ext & LBR_CTL_ENABLE_MASK)) { svm_copy_lbrs(vmcb02, vmcb01);
Re: [PATCH v4 2/6] KVM: x86: nSVM: implement nested LBR virtualization
Quoted 9 lines · Paolo Bonzini
Review
Quoted 5 lines · Maxim Levitsky
(quoted diff) +5
-0+ /* Copy LBR related registers from vmcb12, + * but make sure that we only pick LBR enable bit from the guest. + */ + svm_copy_lbrs(vmcb02, vmcb12); + vmcb02->save.dbgctl &= LBR_CTL_ENABLE_MASK;I still do not understand why it is not copying all bits outside
DEBUGCTL_RESERVED_BITS. That is:
Honestly, you are right, I'll do this.
Note however about few issues that we have around MSR_IA32_DEBUGCTLMSR which needs to be eventually fixed (and if I get to it first, I'll do this):
On SVM:
- without LBR virtualization supported (!lbrv)
any attempt to set that msr is ignored and logged with pr_err_ratelimited.
Note that on AMD, MSR_IA32_DEBUGCTLMSR consists of:
bit 0 -
AMD's LBR bit
bit 1 -
BTF - when set, EFLAGS.TF flag causes debug exception only on control flow instructions, allowing you to do more efficient debugger controlled run of code under debug.
bit 2-5:
exposes perf counters on external CPU pins. Very likely NOP on anything remotely modern.
- with LBR virtualization supported, the guest can set this msr to any value as long as it doesn't set reserved bits and then read back the written value, but it is not used by the CPU, unless LBR bit is set in MSR_IA32_DEBUGCTLMSR, because only then LBR virtualization is enabled, which makes the CPU load the guest value on VM entry.
This means that MSR_IA32_DEBUGCTLMSR.BTF will magically start working when MSR_IA32_DEBUGCTLMSR.LBR is set as well, and will not work otherwise.
On VMX, we also have something a bit related (but I didn't do any homework on this):
If both LBR and BTF are set, they are both cleared and we also get vcpu_unimpl ratelimited message.
otherwise the value is written to GUEST_IA32_DEBUGCTL which I think isn't tied to LBR virtualization like on AMD
(also intel's LBR implementation is much more useful, since it has multiple records).
So since the only bit in question is BTF, I was thinking, lets just pick the LBR bit.
But I have absolutely no issue to be bug-consistent with non nested treatment of MSR_IA32_DEBUGCTLMSR and passthrough all non reserved bits.
Or I might at least document this in the errata document you added recently to KVM (which is a great idea).
Best regards,
Maxim Levitsky
Quoted 53 lines
arch/x86/kvm/svm/nested.c +4
-3@@ -589,11 +589,12 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12 }if (unlikely (svm->lbrv_enabled && (svm->nested.ctl.virt_ext &LBR_CTL_ENABLE_MASK ))) { -/* Copy LBR related registers from vmcb12, -* but make sure that we only pick LBR enable bit from the guest. +/* +* Reserved bits of DEBUGCTL are ignored. Be consistent with +* svm_set_msr's definition of reserved bits. */ svm_copy_lbrs (vmcb02, vmcb12); - vmcb02->save.dbgctl &=LBR_CTL_ENABLE_MASK ; + vmcb02->save.dbgctl &= ~DEBUGCTL_RESERVED_BITS ;svm_update_lbrv (&svm->vcpu); }else if (unlikely (vmcb01->control.virt_ext &LBR_CTL_ENABLE_MASK )) {arch/x86/kvm/svm/svm.c +0
-2@@ -62,8 +62,6 @@ MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);#define SEG_TYPE_LDT 2#define SEG_TYPE_BUSY_TSS16 3 -#define DEBUGCTL_RESERVED_BITS (~(0x3fULL)) -static bool erratum_383_found__read_mostly ;u32 msrpm_offsets[MSRPM_OFFSETS ]__read_mostly ;arch/x86/kvm/svm/svm.h +2
-0@@ -487,6 +487,8 @@ static inline bool nested_npt_enabled(struct vcpu_svm *svm)/* svm.c */ #define MSR_INVALID 0xffffffffU +#define DEBUGCTL_RESERVED_BITS (~(0x3fULL)) +extern bool dump_invalid_vmcb;u32 svm_msrpm_offset (u32 msr);Quoted 6 lines
(quoted diff) +3
-0+ svm_update_lbrv(&svm->vcpu); + + } else if (unlikely(vmcb01->control.virt_ext & LBR_CTL_ENABLE_MASK)) { svm_copy_lbrs(vmcb02, vmcb01);
Paolo Bonzini
maintainerRe: [PATCH v4 2/6] KVM: x86: nSVM: implement nested LBR virtualization
Quoted 8 lines · Maxim Levitsky
- with LBR virtualization supported, the guest can set this msr to any value as long as it doesn't set reserved bits and then read back the written value, but it is not used by the CPU, unless LBR bit is set in
MSR_IA32_DEBUGCTLMSR, because only then LBR virtualization is enabled, which makes the CPU load the guest value on VM entry.This means that MSR_IA32_DEBUGCTLMSR.BTF will magically start working when MSR_IA32_DEBUGCTLMSR.LBR is set as well, and will not work otherwise.
That can be fixed by context-switching DEBUGCTLMSR by hand when LBR=0 && BTF=1. Would you like to give it a shot?
Paolo
Re: [PATCH v4 2/6] KVM: x86: nSVM: implement nested LBR virtualization
Quoted 7 lines · Paolo Bonzini
Quoted 6 lines · Maxim Levitsky
- with LBR virtualization supported, the guest can set this msr to any value as long as it doesn't set reserved bits and then read back the written value, but it is not used by the CPU, unless LBR bit is set in
MSR_IA32_DEBUGCTLMSR, because only then LBR virtualization is enabled, which makes the CPU load the guest value on VM entry.
Quoted 8 lines
Quoted 2 lines
This means that MSR_IA32_DEBUGCTLMSR.BTF will magically start working when MSR_IA32_DEBUGCTLMSR.LBR is set as well, and will not work otherwise.
That can be fixed by context-switching DEBUGCTLMSR by hand when LBR=0 && BTF=1. Would you like to give it a shot?
Paolo
Yep exactly, I didn't do that yet only because mypatches didn't made it worse, so I wanted to do this in a separate patch (+unit test), and it kind of went to my backlog. I'll do that soon.
Best regards,
Maxim Levitsky
PATCH v4 3/6 KVM: x86: nSVM: support PAUSE filtering when L0 doesn't intercept PAUSE
In reply to Maxim LevitskyAllow L1 to use PAUSE filtering if L0 doesn't use it.
--- arch/x86/kvm/svm/nested.c | 26 ++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 22 +++++++++++++++++++--- arch/x86/kvm/svm/svm.h | 2 ++ 3 files changed, 47 insertions(+), 3 deletions(-)
arch/x86/kvm/svm/nested.c +26 -0
@@ -667,6 +667,29 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)if (!nested_vmcb_needs_vls_intercept (svm)) vmcb02->control.virt_ext |=VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK ; +if (kvm_pause_in_guest (svm->vcpu.kvm)) { +/* use guest values since host doesn't use them */ + vmcb02->control.pause_filter_count = + svm->pause_filter_enabled ? + svm->nested.ctl.pause_filter_count :0 ; + + vmcb02->control.pause_filter_thresh = + svm->pause_threshold_enabled ? + svm->nested.ctl.pause_filter_thresh :0 ; + + }else if (!vmcb12_is_intercept (&svm->nested.ctl,INTERCEPT_PAUSE )) { +/* use host values when guest doesn't use them */ + vmcb02->control.pause_filter_count = vmcb01->control.pause_filter_count; + vmcb02->control.pause_filter_thresh = vmcb01->control.pause_filter_thresh; + }else { +/* +* Intercept every PAUSE otherwise and +* ignore both host and guest values +*/ + vmcb02->control.pause_filter_count =0 ; + vmcb02->control.pause_filter_thresh =0 ; + } +nested_svm_transition_tlb_flush (vcpu);/* Enter Guest-Mode */
@@ -927,6 +950,9 @@ int nested_svm_vmexit(struct vcpu_svm *svm) vmcb12->control.event_inj = svm->nested.ctl.event_inj; vmcb12->control.event_inj_err = svm->nested.ctl.event_inj_err; +if (!kvm_pause_in_guest (vcpu->kvm) && vmcb02->control.pause_filter_count) + vmcb01->control.pause_filter_count = vmcb02->control.pause_filter_count; +nested_svm_copy_common_state (svm->nested.vmcb02.ptr, svm->vmcb01.ptr);svm_switch_vmcb (svm, &svm->vmcb01);
arch/x86/kvm/svm/svm.c +19 -3
@@ -910,6 +910,9 @@ static void grow_ple_window(struct kvm_vcpu *vcpu)struct vmcb_control_area *control = &svm->vmcb->control;int old = control->pause_filter_count; +if (kvm_pause_in_guest (vcpu->kvm) || !old) +return ; + control->pause_filter_count = __grow_ple_window(old, pause_filter_count, pause_filter_count_grow,
@@ -928,6 +931,9 @@ static void shrink_ple_window(struct kvm_vcpu *vcpu)struct vmcb_control_area *control = &svm->vmcb->control;int old = control->pause_filter_count; +if (kvm_pause_in_guest (vcpu->kvm) || !old) +return ; + control->pause_filter_count = __shrink_ple_window(old, pause_filter_count,
@@ -2984,7 +2990,6 @@ static int interrupt_window_interception(struct kvm_vcpu *vcpu)static int pause_interception (struct kvm_vcpu *vcpu) {bool in_kernel; -/* * CPL is not made available for an SEV-ES guest, therefore * vcpu->arch.preempted_in_kernel can never be true. Just
@@ -2992,8 +2997,7 @@ static int pause_interception(struct kvm_vcpu *vcpu)*/ in_kernel = !sev_es_guest (vcpu->kvm) &&svm_get_cpl (vcpu) ==0 ; -if (!kvm_pause_in_guest (vcpu->kvm)) -grow_ple_window (vcpu); +grow_ple_window (vcpu);kvm_vcpu_on_spin (vcpu, in_kernel);return kvm_skip_emulated_instruction (vcpu);
@@ -4020,6 +4024,12 @@ static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) svm->v_vmload_vmsave_enabled = vls &&guest_cpuid_has (vcpu,X86_FEATURE_V_VMSAVE_VMLOAD ); + svm->pause_filter_enabled =kvm_cpu_cap_has (X86_FEATURE_PAUSEFILTER ) && +guest_cpuid_has (vcpu,X86_FEATURE_PAUSEFILTER ); + + svm->pause_threshold_enabled =kvm_cpu_cap_has (X86_FEATURE_PFTHRESHOLD ) && +guest_cpuid_has (vcpu,X86_FEATURE_PFTHRESHOLD ); +svm_recalc_instruction_intercepts (vcpu, svm);/* For sev guests, the memory encryption bit is not reserved in CR3. */
@@ -4773,6 +4783,12 @@ static __init void svm_set_cpu_caps(void)if (lbrv)kvm_cpu_cap_set (X86_FEATURE_LBRV ); +if (boot_cpu_has (X86_FEATURE_PAUSEFILTER )) +kvm_cpu_cap_set (X86_FEATURE_PAUSEFILTER ); + +if (boot_cpu_has (X86_FEATURE_PFTHRESHOLD )) +kvm_cpu_cap_set (X86_FEATURE_PFTHRESHOLD ); +/* Nested VM can receive #VMEXIT instead of triggering #GP */ kvm_cpu_cap_set (X86_FEATURE_SVME_ADDR_CHK ); }
arch/x86/kvm/svm/svm.h +2 -0
@@ -237,6 +237,8 @@ struct vcpu_svm { bool tsc_scaling_enabled : 1; bool v_vmload_vmsave_enabled : 1; bool lbrv_enabled : 1; + bool pause_filter_enabled : 1; + bool pause_threshold_enabled : 1;u32 ldr_reg;u32 dfr_reg;
Signature · 1 line
2.26.3
Paolo Bonzini
maintainerRe: [PATCH v4 3/6] KVM: x86: nSVM: support PAUSE filtering when L0 doesn't intercept PAUSE
Quoted 3 lines · Maxim Levitsky
Allow L1 to use PAUSE filtering if L0 doesn't use it.
Signed-off-by Maxim Levitsky<[email protected]>
Can you enlarge the commit message to explain the logic in nested_vmcb02_prepare_control?
Thanks,
Paolo
Re: [PATCH v4 3/6] KVM: x86: nSVM: support PAUSE filtering when L0 doesn't intercept PAUSE
Quoted 7 lines · Paolo Bonzini
Quoted 3 lines · Maxim Levitsky
Allow L1 to use PAUSE filtering if L0 doesn't use it.
Signed-off-by Maxim Levitsky<[email protected]>Can you enlarge the commit message to explain the logic in
nested_vmcb02_prepare_control?
No problem, I will do in the next version.
How about this:
KVM: x86: nSVM: support nested PAUSE filtering when possible
Expose the pause filtering and threshold in the guest CPUID and support PAUSE filtering when possible:
- If the L0 doesn't intercept PAUSE
(cpu_pm=on, or pause_filter_count kvm_amd's parameter is 0), then allow L1 to have full control over PAUSE filtering.
- Otherwise if the L1 doesn't intercept PAUSE, use KVM's PAUSE thresholds, and update them even when running nested.
- Otherwise ignore both host and guest PAUSE thresholds,
because it is not really possible to merge them correctly.
It is expected that in this case, userspace hypervisor (e.g qemu)
will not enable this feature in the guest CPUID, to avoid having the guest to update both thresholds pointlessly.
Best regards,
Maxim Levitsky
Quoted 5 lines
Thanks,
Paolo
PATCH v4 4/6 KVM: x86: nSVM: implement nested vGIF
In reply to Maxim LevitskyIn case L1 enables vGIF for L2, the L2 cannot affect L1's GIF, regardless of STGI/CLGI intercepts, and since VM entry enables GIF, this means that L1's GIF is always 1 while L2 is running.
Thus in this case leave L1's vGIF in vmcb01, while letting L2 control the vGIF thus implementing nested vGIF.
Also allow KVM to toggle L1's GIF during nested entry/exit by always using vmcb01.
--- arch/x86/kvm/svm/nested.c | 17 +++++++++++++---- arch/x86/kvm/svm/svm.c | 5 +++++ arch/x86/kvm/svm/svm.h | 35 +++++++++++++++++++++++++++++------ 3 files changed, 47 insertions(+), 10 deletions(-)
arch/x86/kvm/svm/nested.c +13 -4
@@ -439,6 +439,10 @@ void nested_sync_control_from_vmcb02(struct vcpu_svm *svm)*/ mask &= ~V_IRQ_MASK ; } + +if (nested_vgif_enabled (svm)) + mask |=V_GIF_MASK ; + svm->nested.ctl.int_ctl &= ~mask; svm->nested.ctl.int_ctl |= svm->vmcb->control.int_ctl & mask; }
@@ -603,10 +607,8 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12static void nested_vmcb02_prepare_control (struct vcpu_svm *svm) { -const u32 int_ctl_vmcb01_bits = -V_INTR_MASKING_MASK |V_GIF_MASK |V_GIF_ENABLE_MASK ; - -const u32 int_ctl_vmcb12_bits =V_TPR_MASK |V_IRQ_INJECTION_BITS_MASK ; +u32 int_ctl_vmcb01_bits =V_INTR_MASKING_MASK ; +u32 int_ctl_vmcb12_bits =V_TPR_MASK |V_IRQ_INJECTION_BITS_MASK ;struct kvm_vcpu *vcpu = &svm->vcpu;struct vmcb *vmcb01 = svm->vmcb01.ptr;
@@ -623,6 +625,13 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)*/ WARN_ON (kvm_apicv_activated (svm->vcpu.kvm)); + + +if (svm->vgif_enabled && (svm->nested.ctl.int_ctl &V_GIF_ENABLE_MASK )) + int_ctl_vmcb12_bits |= (V_GIF_MASK |V_GIF_ENABLE_MASK ); +else + int_ctl_vmcb01_bits |= (V_GIF_MASK |V_GIF_ENABLE_MASK ); +/* Copied from vmcb01. msrpm_base can be overwritten later. */ vmcb02->control.nested_ctl = vmcb01->control.nested_ctl; vmcb02->control.iopm_base_pa = vmcb01->control.iopm_base_pa;
arch/x86/kvm/svm/svm.c +5 -0
@@ -4030,6 +4030,8 @@ static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) svm->pause_threshold_enabled =kvm_cpu_cap_has (X86_FEATURE_PFTHRESHOLD ) &&guest_cpuid_has (vcpu,X86_FEATURE_PFTHRESHOLD ); + svm->vgif_enabled = vgif &&guest_cpuid_has (vcpu,X86_FEATURE_VGIF ); +svm_recalc_instruction_intercepts (vcpu, svm);/* For sev guests, the memory encryption bit is not reserved in CR3. */
@@ -4789,6 +4791,9 @@ static __init void svm_set_cpu_caps(void)if (boot_cpu_has (X86_FEATURE_PFTHRESHOLD ))kvm_cpu_cap_set (X86_FEATURE_PFTHRESHOLD ); +if (vgif) +kvm_cpu_cap_set (X86_FEATURE_VGIF ); +/* Nested VM can receive #VMEXIT instead of triggering #GP */ kvm_cpu_cap_set (X86_FEATURE_SVME_ADDR_CHK ); }
arch/x86/kvm/svm/svm.h +29 -6
@@ -239,6 +239,7 @@ struct vcpu_svm { bool lbrv_enabled : 1; bool pause_filter_enabled : 1; bool pause_threshold_enabled : 1; + bool vgif_enabled : 1;u32 ldr_reg;u32 dfr_reg;
@@ -457,26 +458,48 @@ static inline bool svm_is_intercept(struct vcpu_svm *svm, int bit)return vmcb_is_intercept (&svm->vmcb->control, bit); } +static inline bool nested_vgif_enabled (struct vcpu_svm *svm) +{ +return svm->vgif_enabled && (svm->nested.ctl.int_ctl &V_GIF_ENABLE_MASK ); +} + +static inline struct vmcb *get_vgif_vmcb (struct vcpu_svm *svm) +{ +if (!vgif) +return NULL ; + +if (is_guest_mode (&svm->vcpu) && !nested_vgif_enabled (svm)) +return svm->nested.vmcb02.ptr; +else +return svm->vmcb01.ptr; +} +static inline void enable_gif (struct vcpu_svm *svm) { -if (vgif) - svm->vmcb->control.int_ctl |=V_GIF_MASK ; +struct vmcb *vmcb =get_vgif_vmcb (svm); + +if (vmcb) + vmcb->control.int_ctl |=V_GIF_MASK ;else svm->vcpu.arch.hflags |=HF_GIF_MASK ; }static inline void disable_gif (struct vcpu_svm *svm) { -if (vgif) - svm->vmcb->control.int_ctl &= ~V_GIF_MASK ; +struct vmcb *vmcb =get_vgif_vmcb (svm); + +if (vmcb) + vmcb->control.int_ctl &= ~V_GIF_MASK ;else svm->vcpu.arch.hflags &= ~HF_GIF_MASK ; }static inline bool gif_set (struct vcpu_svm *svm) { -if (vgif) -return !!(svm->vmcb->control.int_ctl &V_GIF_MASK ); +struct vmcb *vmcb =get_vgif_vmcb (svm); + +if (vmcb) +return !!(vmcb->control.int_ctl &V_GIF_MASK );else return !!(svm->vcpu.arch.hflags &HF_GIF_MASK ); }
Signature · 1 line
2.26.3
PATCH v4 5/6 KVM: x86: allow per cpu apicv inhibit reasons
In reply to Maxim LevitskyAdd optional callback .vcpu_get_apicv_inhibit_reasons returning extra inhibit reasons that prevent APICv from working on this vCPU.
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 6 ++++++ arch/x86/kvm/x86.c | 14 ++++++++++++-- 3 files changed, 19 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -126,6 +126,7 @@ KVM_X86_OP_OPTIONAL(migrate_timers) KVM_X86_OP(msr_filter_changed) KVM_X86_OP(complete_emulated_msr) KVM_X86_OP(vcpu_deliver_sipi_vector) +KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons);#undef KVM_X86_OP#undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +6 -0
@@ -1507,6 +1507,11 @@ struct kvm_x86_ops {int (*complete_emulated_msr)(struct kvm_vcpu *vcpu,int err);void (*vcpu_deliver_sipi_vector)(struct kvm_vcpu *vcpu,u8 vector); + +/* +* Returns vCPU specific APICv inhibit reasons +*/ +unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu); };struct kvm_x86_nested_ops {
@@ -1807,6 +1812,7 @@ gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva, struct x86_exception *exception);bool kvm_apicv_activated (struct kvm *kvm); +bool kvm_vcpu_apicv_activated (struct kvm_vcpu *vcpu);void kvm_vcpu_update_apicv (struct kvm_vcpu *vcpu);void __kvm_set_or_clear_apicv_inhibit (struct kvm *kvm,enum kvm_apicv_inhibit reason,bool set);
arch/x86/kvm/x86.c +12 -2
@@ -9114,6 +9114,14 @@ bool kvm_apicv_activated(struct kvm *kvm) }EXPORT_SYMBOL_GPL (kvm_apicv_activated); +bool kvm_vcpu_apicv_activated (struct kvm_vcpu *vcpu) +{ +ulong vm_reasons =READ_ONCE (vcpu->kvm->arch.apicv_inhibit_reasons); +ulong vcpu_reasons =static_call (kvm_x86_vcpu_get_apicv_inhibit_reasons)(vcpu); + +return (vm_reasons | vcpu_reasons) ==0 ; +} +EXPORT_SYMBOL_GPL (kvm_vcpu_apicv_activated);static void set_or_clear_apicv_inhibit (unsigned long *inhibits,enum kvm_apicv_inhibit reason,bool set)
@@ -9788,7 +9796,8 @@ void kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu)down_read (&vcpu->kvm->arch.apicv_update_lock); - activate =kvm_apicv_activated (vcpu->kvm); + activate =kvm_vcpu_apicv_activated (vcpu); +if (vcpu->arch.apicv_active == activate) goto out;
@@ -10189,7 +10198,8 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)* per-VM state, and responsing vCPUs must wait for the update * to complete before servicing KVM_REQ_APICV_UPDATE. */ -WARN_ON_ONCE (kvm_apicv_activated (vcpu->kvm) !=kvm_vcpu_apicv_active (vcpu)); +WARN_ON_ONCE (kvm_vcpu_apicv_activated (vcpu) !=kvm_vcpu_apicv_active (vcpu)); + exit_fastpath =static_call (kvm_x86_vcpu_run)(vcpu);if (likely (exit_fastpath !=EXIT_FASTPATH_REENTER_GUEST ))
Signature · 1 line
2.26.3
PATCH v4 6/6 KVM: x86: SVM: allow AVIC to co-exist with a nested guest running
In reply to Maxim LevitskyInhibit the AVIC of the vCPU that is running nested for the duration of the nested run, so that all interrupts arriving from both its vCPU siblings and from KVM are delivered using normal IPIs and cause that vCPU to vmexit.
Note that unlike normal AVIC inhibition, there is no need to update the AVIC mmio memslot, because the nested guest uses its own set of paging tables.
That also means that AVIC doesn't need to be inhibited VM wide.
--- arch/x86/kvm/svm/avic.c | 7 +++++++ arch/x86/kvm/svm/nested.c | 17 ++++++++++------- arch/x86/kvm/svm/svm.c | 30 +++++++++++++++++++----------- arch/x86/kvm/svm/svm.h | 1 + 4 files changed, 37 insertions(+), 18 deletions(-)
arch/x86/kvm/svm/avic.c +7 -0
@@ -357,6 +357,13 @@ int avic_incomplete_ipi_interception(struct kvm_vcpu *vcpu)return 1 ; } +unsigned long avic_vcpu_get_apicv_inhibit_reasons (struct kvm_vcpu *vcpu) +{ +if (is_guest_mode (vcpu)) +return APICV_INHIBIT_REASON_NESTED ; +return 0 ; +} +static u32 *avic_get_logical_id_entry (struct kvm_vcpu *vcpu,u32 ldr,bool flat) {struct kvm_svm *kvm_svm =to_kvm_svm (vcpu->kvm);
arch/x86/kvm/svm/nested.c +10 -7
@@ -619,13 +619,6 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)* exit_int_info, exit_int_info_err, next_rip, insn_len, insn_bytes. */ -/* -* Also covers avic_vapic_bar, avic_backing_page, avic_logical_id, -* avic_physical_id. -*/ -WARN_ON (kvm_apicv_activated (svm->vcpu.kvm)); - -if (svm->vgif_enabled && (svm->nested.ctl.int_ctl &V_GIF_ENABLE_MASK )) int_ctl_vmcb12_bits |= (V_GIF_MASK |V_GIF_ENABLE_MASK );
@@ -766,6 +759,9 @@ int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa,svm_set_gif (svm, true); +if (kvm_vcpu_apicv_active (vcpu)) +kvm_make_request (KVM_REQ_APICV_UPDATE , vcpu); +return 0 ; }
@@ -1043,6 +1039,13 @@ int nested_svm_vmexit(struct vcpu_svm *svm)if (unlikely (vmcb01->save.rflags &X86_EFLAGS_TF ))kvm_queue_exception (&(svm->vcpu),DB_VECTOR ); +/* +* Un-inhibit the AVIC right away, so that other vCPUs can start +* to benefit from it right away. +*/ +if (kvm_apicv_activated (vcpu->kvm)) +kvm_vcpu_update_apicv (vcpu); +return 0 ; }
arch/x86/kvm/svm/svm.c +19 -11
@@ -1468,7 +1468,7 @@ static void svm_set_vintr(struct vcpu_svm *svm)/* * The following fields are ignored when AVIC is enabled */ -WARN_ON (kvm_apicv_activated (svm->vcpu.kvm)); +WARN_ON (kvm_vcpu_apicv_activated (&svm->vcpu));svm_set_intercept (svm,INTERCEPT_VINTR );
@@ -2977,9 +2977,16 @@ static int interrupt_window_interception(struct kvm_vcpu *vcpu)svm_clear_vintr (to_svm (vcpu));/* -* For AVIC, the only reason to end up here is ExtINTs. +* If not running nested, for AVIC, the only reason to end up here is ExtINTs. * In this case AVIC was temporarily disabled for * requesting the IRQ window and we have to re-enable it. +* +* If running nested, still remove the VM wide AVIC inhibit to +* support case in which the interrupt window was requested when the +* vCPU was not running nested. + +* All vCPUs which run still run nested, will remain to have their +* AVIC still inhibited due to per-cpu AVIC inhibition. */ kvm_clear_apicv_inhibit (vcpu->kvm,APICV_INHIBIT_REASON_IRQWIN );
@@ -3574,10 +3581,16 @@ static void svm_enable_irq_window(struct kvm_vcpu *vcpu)/* * IRQ window is not needed when AVIC is enabled, * unless we have pending ExtINT since it cannot be injected -* via AVIC. In such case, we need to temporarily disable AVIC, +* via AVIC. In such case, KVM needs to temporarily disable AVIC, * and fallback to injecting IRQ via V_IRQ. +* +* If running nested, AVIC is already locally inhibited +* on this vCPU, therefore there is no need to request +* the VM wide AVIC inhibition. */ -kvm_set_apicv_inhibit (vcpu->kvm,APICV_INHIBIT_REASON_IRQWIN ); +if (!is_guest_mode (vcpu)) +kvm_set_apicv_inhibit (vcpu->kvm,APICV_INHIBIT_REASON_IRQWIN ); +svm_set_vintr (svm); } }
@@ -4048,13 +4061,6 @@ static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)*/ if (guest_cpuid_has (vcpu,X86_FEATURE_X2APIC ))kvm_set_apicv_inhibit (kvm,APICV_INHIBIT_REASON_X2APIC ); - -/* -* Currently, AVIC does not work with nested virtualization. -* So, we disable AVIC when cpuid for SVM is set in the L1 guest. -*/ -if (nested &&guest_cpuid_has (vcpu,X86_FEATURE_SVM )) -kvm_set_apicv_inhibit (kvm,APICV_INHIBIT_REASON_NESTED ); }init_vmcb_after_set_cpuid (vcpu); }
@@ -4717,6 +4723,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .complete_emulated_msr = svm_complete_emulated_msr, .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, + .vcpu_get_apicv_inhibit_reasons = avic_vcpu_get_apicv_inhibit_reasons, };/*
@@ -4918,6 +4925,7 @@ static __init int svm_hardware_setup(void) }else { svm_x86_ops.vcpu_blocking =NULL ; svm_x86_ops.vcpu_unblocking =NULL ; + svm_x86_ops.vcpu_get_apicv_inhibit_reasons =NULL ; }if (vls) {
arch/x86/kvm/svm/svm.h +1 -0
@@ -622,6 +622,7 @@ int avic_pi_update_irte(struct kvm *kvm, unsigned int host_irq,void avic_vcpu_blocking (struct kvm_vcpu *vcpu);void avic_vcpu_unblocking (struct kvm_vcpu *vcpu);void avic_ring_doorbell (struct kvm_vcpu *vcpu); +unsigned long avic_vcpu_get_apicv_inhibit_reasons (struct kvm_vcpu *vcpu);/* sev.c */
Signature · 1 line
2.26.3