KVM: SVM: Enable AVIC for Zen4+ (if x2AVIC)
Sean Christopherson
maintainerv3 cover KVM: SVM: Enable AVIC for Zen4+ (if x2AVIC)
Enable AVIC by default for Zen4+, so long as x2AVIC is supported (which should be the case if AVIC is supported).
v3:
- Don't advise the user to enable force_avic. [Naveen]
- Gather AVIC related module params in avic.c (by moving code/helpers to avic.c).
- Print "AVIC enabled" even when it's forced.
- Enable by default iff x2AVIC is supported.
- Use "auto" to select KVM's automatic/default behavior.
v2: https://lore.kernel.org/all/[email protected]
v1: http://lkml.kernel.org/r/[email protected]
Naveen N Rao (1):
KVM: SVM: Enable AVIC by default for Zen4+ if x2AVIC is support
Sean Christopherson (5):
KVM: SVM: Move x2AVIC MSR interception helper to avic.c
KVM: SVM: Update "APICv in x2APIC without x2AVIC" in avic.c, not svm.c
KVM: SVM: Always print "AVIC enabled" separately, even when force
enabled
KVM: SVM: Don't advise the user to do force_avic=y (when x2AVIC is
detected)
KVM: SVM: Move global "avic" variable to avic.c
arch/x86/kvm/svm/avic.c | 149 +++++++++++++++++++++++++++++++++------- arch/x86/kvm/svm/svm.c | 62 +---------------- arch/x86/kvm/svm/svm.h | 4 +- 3 files changed, 125 insertions(+), 90 deletions(-)
base-commit: c8fbf7ceb2ae3f64b0c377c8c21f6df577a13eb4
Signature · 1 line
2.51.0.470.ga7dc726c21-goog
Sean Christopherson
maintainerPATCH v3 1/6 KVM: SVM: Move x2AVIC MSR interception helper to avic.c
Move svm_set_x2apic_msr_interception() to avic.c as it's only relevant when x2AVIC is enabled/supported and only called by AVIC code. In addition to scoping AVIC code to avic.c, this will allow burying the global x2avic_enabled variable in avic.
Opportunistically rename the helper to explicitly scope it to "avic".
No functional change intended.
--- arch/x86/kvm/svm/avic.c | 57 ++++++++++++++++++++++++++++++++++++++--- arch/x86/kvm/svm/svm.c | 49 ----------------------------------- arch/x86/kvm/svm/svm.h | 1 - 3 files changed, 54 insertions(+), 53 deletions(-)
arch/x86/kvm/svm/avic.c +54 -3
@@ -79,6 +79,57 @@ static bool next_vm_id_wrapped = 0;static DEFINE_SPINLOCK (svm_vm_data_hash_lock);bool x2avic_enabled; + +static void avic_set_x2apic_msr_interception (struct vcpu_svm *svm, +bool intercept) +{ +static const u32 x2avic_passthrough_msrs[] = { +X2APIC_MSR (APIC_ID ), +X2APIC_MSR (APIC_LVR ), +X2APIC_MSR (APIC_TASKPRI ), +X2APIC_MSR (APIC_ARBPRI ), +X2APIC_MSR (APIC_PROCPRI ), +X2APIC_MSR (APIC_EOI ), +X2APIC_MSR (APIC_RRR ), +X2APIC_MSR (APIC_LDR ), +X2APIC_MSR (APIC_DFR ), +X2APIC_MSR (APIC_SPIV ), +X2APIC_MSR (APIC_ISR ), +X2APIC_MSR (APIC_TMR ), +X2APIC_MSR (APIC_IRR ), +X2APIC_MSR (APIC_ESR ), +X2APIC_MSR (APIC_ICR ), +X2APIC_MSR (APIC_ICR2 ), + +/* +* Note! Always intercept LVTT, as TSC-deadline timer mode +* isn't virtualized by hardware, and the CPU will generate a +* #GP instead of a #VMEXIT. +*/ +X2APIC_MSR (APIC_LVTTHMR ), +X2APIC_MSR (APIC_LVTPC ), +X2APIC_MSR (APIC_LVT0 ), +X2APIC_MSR (APIC_LVT1 ), +X2APIC_MSR (APIC_LVTERR ), +X2APIC_MSR (APIC_TMICT ), +X2APIC_MSR (APIC_TMCCT ), +X2APIC_MSR (APIC_TDCR ), + }; +int i; + +if (intercept == svm->x2avic_msrs_intercepted) +return ; + +if (!x2avic_enabled) +return ; + +for (i =0 ; i <ARRAY_SIZE (x2avic_passthrough_msrs); i++) +svm_set_intercept_for_msr (&svm->vcpu, x2avic_passthrough_msrs[i], +MSR_TYPE_RW , intercept); + + svm->x2avic_msrs_intercepted = intercept; +} +static void avic_activate_vmcb (struct vcpu_svm *svm) {struct vmcb *vmcb = svm->vmcb01.ptr;
@@ -99,7 +150,7 @@ static void avic_activate_vmcb(struct vcpu_svm *svm) vmcb->control.int_ctl |=X2APIC_MODE_MASK ; vmcb->control.avic_physical_id |=X2AVIC_MAX_PHYSICAL_ID ;/* Disabling MSR intercept for x2APIC registers */ -svm_set_x2apic_msr_interception (svm, false); +avic_set_x2apic_msr_interception (svm, false); }else {/* * Flush the TLB, the guest may have inserted a non-APIC
@@ -110,7 +161,7 @@ static void avic_activate_vmcb(struct vcpu_svm *svm)/* For xAVIC and hybrid-xAVIC modes */ vmcb->control.avic_physical_id |=AVIC_MAX_PHYSICAL_ID ;/* Enabling MSR intercept for x2APIC registers */ -svm_set_x2apic_msr_interception (svm, true); +avic_set_x2apic_msr_interception (svm, true); } }
@@ -130,7 +181,7 @@ static void avic_deactivate_vmcb(struct vcpu_svm *svm)return ;/* Enabling MSR intercept for x2APIC registers */ -svm_set_x2apic_msr_interception (svm, true); +avic_set_x2apic_msr_interception (svm, true); }/* Note:
arch/x86/kvm/svm/svm.c +0 -49
55 lines not on this page · raw message
arch/x86/kvm/svm/svm.h +0 -1
@@ -699,7 +699,6 @@ void svm_set_gif(struct vcpu_svm *svm, bool value);int svm_invoke_exit_handler (struct kvm_vcpu *vcpu,u64 exit_code);void set_msr_interception (struct kvm_vcpu *vcpu,u32 *msrpm,u32 msr,int read,int write); -void svm_set_x2apic_msr_interception (struct vcpu_svm *svm,bool disable);void svm_complete_interrupt_delivery (struct kvm_vcpu *vcpu,int delivery_mode,int trig_mode,int vec);
Signature · 1 line
2.51.0.470.ga7dc726c21-goog
Re: [PATCH v3 1/6] KVM: SVM: Move x2AVIC MSR interception helper to avic.c
Quoted 7 lines · Sean Christopherson
Move
svm_set_x2apic_msr_interception()toavic.cas it's only relevant when x2AVIC is enabled/supported and only called by AVIC code. In addition to scoping AVIC code toavic.c, this will allow burying the globalx2avic_enabledvariable in avic.Opportunistically rename the helper to explicitly scope it to "avic".
Quoted 8 lines
No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/svm/avic.c | 57 ++++++++++++++++++++++++++++++++++++++--- arch/x86/kvm/svm/svm.c | 49 ----------------------------------- arch/x86/kvm/svm/svm.h | 1 - 3 files changed, 54 insertions(+), 53 deletions(-)
- Naveen
Sean Christopherson
maintainerPATCH v3 2/6 KVM: SVM: Update "APICv in x2APIC without x2AVIC" in avic.c, not svm.c
In reply to Sean ChristophersonSet the "allow_apicv_in_x2apic_without_x2apic_virtualization" flag as part of avic_hardware_setup() instead of handling in svm_hardware_setup(), and make x2avic_enabled local to avic.c (setting the flag was the only use in svm.c).
Opportunistically tag avic_hardware_setup() with __init to make it clear that nothing untoward is happening with svm_x86_ops.
No functional change intended (aside from the side effects of tagging avic_hardware_setup() with __init).
--- arch/x86/kvm/svm/avic.c | 6 ++++-- arch/x86/kvm/svm/svm.c | 4 +--- arch/x86/kvm/svm/svm.h | 3 +-- 3 files changed, 6 insertions(+), 7 deletions(-)
arch/x86/kvm/svm/avic.c +4 -2
@@ -77,7 +77,7 @@ static DEFINE_HASHTABLE(svm_vm_data_hash, SVM_VM_DATA_HASH_BITS);static u32 next_vm_id =0 ;static bool next_vm_id_wrapped =0 ;static DEFINE_SPINLOCK (svm_vm_data_hash_lock); -bool x2avic_enabled; +static bool x2avic_enabled; static void avic_set_x2apic_msr_interception(struct vcpu_svm *svm,
@@ -1147,7 +1147,7 @@ void avic_vcpu_unblocking(struct kvm_vcpu *vcpu)* - Hypervisor can support both xAVIC and x2AVIC in the same guest. * - The mode can be switched at run-time. */ -bool avic_hardware_setup (void ) +bool __init avic_hardware_setup (struct kvm_x86_ops *svm_ops) {if (!npt_enabled)return false;
@@ -1182,6 +1182,8 @@ bool avic_hardware_setup(void) x2avic_enabled =boot_cpu_has (X86_FEATURE_X2AVIC );if (x2avic_enabled)pr_info ("x2AVIC enabled\n" ); +else + svm_ops->allow_apicv_in_x2apic_without_x2apic_virtualization = true;/* * Disable IPI virtualization for AMD Family 17h CPUs (Zen1 and Zen2)
arch/x86/kvm/svm/svm.c +1 -3
@@ -5354,15 +5354,13 @@ static __init int svm_hardware_setup(void) goto err; } - enable_apicv = avic = avic &&avic_hardware_setup (); + enable_apicv = avic = avic &&avic_hardware_setup (&svm_x86_ops);if (!enable_apicv) { enable_ipiv = false; svm_x86_ops.vcpu_blocking =NULL ; svm_x86_ops.vcpu_unblocking =NULL ; svm_x86_ops.vcpu_get_apicv_inhibit_reasons =NULL ; - }else if (!x2avic_enabled) { - svm_x86_ops.allow_apicv_in_x2apic_without_x2apic_virtualization = true; }if (vls) {
arch/x86/kvm/svm/svm.h +1 -2
@@ -48,7 +48,6 @@ extern bool npt_enabled;extern int nrips;extern int vgif;extern bool intercept_smi; -extern bool x2avic_enabled;extern bool vnmi;extern int lbrv;
@@ -800,7 +799,7 @@ extern struct kvm_x86_nested_ops svm_nested_ops; BIT(APICV_INHIBIT_REASON_PHYSICAL_ID_TOO_BIG) \ ) -bool avic_hardware_setup (void ); +bool __init avic_hardware_setup (struct kvm_x86_ops *svm_ops);int avic_ga_log_notifier (u32 ga_tag);void avic_vm_destroy (struct kvm *kvm);int avic_vm_init (struct kvm *kvm);
Signature · 1 line
2.51.0.470.ga7dc726c21-goog
Re: [PATCH v3 2/6] KVM: SVM: Update "APICv in x2APIC without x2AVIC" in avic.c, not svm.c
Quoted 41 lines · Sean Christopherson
Set the "
allow_apicv_in_x2apic_without_x2apic_virtualization" flag as part ofavic_hardware_setup()instead of handling insvm_hardware_setup(), and makex2avic_enabledlocal toavic.c(setting the flag was the only use insvm.c).Opportunistically tag
avic_hardware_setup()with__initto make it clear that nothing untoward is happening withsvm_x86_ops.No functional change intended (aside from the side effects of tagging
avic_hardware_setup()with__init).Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/svm/avic.c | 6 ++++-- arch/x86/kvm/svm/svm.c | 4 +--- arch/x86/kvm/svm/svm.h | 3 +-- 3 files changed, 6 insertions(+), 7 deletions(-)arch/x86/kvm/svm/avic.c +2
-2@@ -77,7 +77,7 @@ static DEFINE_HASHTABLE(svm_vm_data_hash, SVM_VM_DATA_HASH_BITS);static u32 next_vm_id =0 ;static bool next_vm_id_wrapped =0 ;static DEFINE_SPINLOCK (svm_vm_data_hash_lock); -bool x2avic_enabled; +static bool x2avic_enabled; static void avic_set_x2apic_msr_interception(struct vcpu_svm *svm,@@ -1147,7 +1147,7 @@ void avic_vcpu_unblocking(struct kvm_vcpu *vcpu)* - Hypervisor can support both xAVIC and x2AVIC in the same guest. * - The mode can be switched at run-time. */ -bool avic_hardware_setup (void ) +bool __init avic_hardware_setup (struct kvm_x86_ops *svm_ops) {if (!npt_enabled)return false;
arch/x86/kvm/svm/avic.c bool avic_hardware_setup(void) · line 1186
Quoted 5 lines
arch/x86/kvm/svm/avic.c +2
-0@@ -1182,6 +1182,8 @@ bool avic_hardware_setup(void) x2avic_enabled =boot_cpu_has (X86_FEATURE_X2AVIC );if (x2avic_enabled)pr_info ("x2AVIC enabled\n" ); +else + svm_ops->allow_apicv_in_x2apic_without_x2apic_virtualization = true;
I'm not entirely convinced that this is better since svm_x86_ops fields are now being updated outside of svm.c. But, I do see your point about limiting x2avic_enabled to avic.c
Would it be better to name this field as svm_x86_ops here too, so it is at least easy to grep and find?
Otherwise, for this patch:
- Naveen
Sean Christopherson
maintainerRe: [PATCH v3 2/6] KVM: SVM: Update "APICv in x2APIC without x2AVIC" in avic.c, not svm.c
+Intel folks (question at the bottom regarding vt_x86_ops)
Quoted 42 lines · Naveen N Rao
Quoted 41 lines · Sean Christopherson
Set the "
allow_apicv_in_x2apic_without_x2apic_virtualization" flag as part ofavic_hardware_setup()instead of handling insvm_hardware_setup(), and makex2avic_enabledlocal toavic.c(setting the flag was the only use insvm.c).Opportunistically tag
avic_hardware_setup()with__initto make it clear that nothing untoward is happening withsvm_x86_ops.No functional change intended (aside from the side effects of tagging
avic_hardware_setup()with__init).Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/svm/avic.c | 6 ++++-- arch/x86/kvm/svm/svm.c | 4 +--- arch/x86/kvm/svm/svm.h | 3 +-- 3 files changed, 6 insertions(+), 7 deletions(-)arch/x86/kvm/svm/avic.c +2
-2@@ -77,7 +77,7 @@ static DEFINE_HASHTABLE(svm_vm_data_hash, SVM_VM_DATA_HASH_BITS);static u32 next_vm_id =0 ;static bool next_vm_id_wrapped =0 ;static DEFINE_SPINLOCK (svm_vm_data_hash_lock); -bool x2avic_enabled; +static bool x2avic_enabled; static void avic_set_x2apic_msr_interception(struct vcpu_svm *svm,@@ -1147,7 +1147,7 @@ void avic_vcpu_unblocking(struct kvm_vcpu *vcpu)* - Hypervisor can support both xAVIC and x2AVIC in the same guest. * - The mode can be switched at run-time. */ -bool avic_hardware_setup (void ) +bool __init avic_hardware_setup (struct kvm_x86_ops *svm_ops) {if (!npt_enabled)return false;@@ -1182,6 +1182,8 @@ bool avic_hardware_setup(void)
Quoted 8 lines
arch/x86/kvm/svm/avic.c bool avic_hardware_setup(void) · line 1186
Quoted 5 lines
arch/x86/kvm/svm/avic.c +2
-0x2avic_enabled =boot_cpu_has (X86_FEATURE_X2AVIC );if (x2avic_enabled)pr_info ("x2AVIC enabled\n" ); +else + svm_ops->allow_apicv_in_x2apic_without_x2apic_virtualization = true;I'm not entirely convinced that this is better since
svm_x86_opsfields are now being updated outside ofsvm.c.
That doesn't bother me, e.g. the pmu_ops buried in svm_x86_ops come from arch/x86/kvm/svm/pmu.c. Eww, and arch/x86/kvm/svm/svm_onhyperv.h already accesses svm_x86_ops, but in the grossest way possible.
FWIW, I don't love splitting the APIC virtualization updates between svm_hardware_setup() and avic_hardware_setup(), but overall I do think that's the best balance between bundling all code together and making it easy(ish) for readers to figure out what's going on.
Quoted 4 lines
But, I do see your point about limiting
x2avic_enabledtoavic.cWould it be better to name this field as
svm_x86_opshere too, so it is at least easy to grep and find?
I didn't want to create a potential variable shadowing "problem". The alternative would be to expose svm_x86_ops via svm.h. That's not my first choice, but it's the route that was taken for the TDX vs. VMX split (vt_init_ops and vt_x86_ops are globally visible, even though they _could_ have been explicitly passed in as parameters to {tdx,vmx}_hardware_setup()).
Question then. Does anyone have a preference/opinion between explicitly passing in ops to vendor specific helpers, vs. making {svm,vt}_x86_ops globally visible?
I don't love creating "hidden" dependencies, in quotes because in this case it's relatively easy to establish that the setup() helpers modify {svm,vt}_x86_ops, i.e. surprises should be rare.
On the other hand, I do agree it's helpful to be able to see exactly where {svm,vt}_x86_ops are updated.
And most importantly, I want to be consistent between VMX and SVM, i.e. I want to pick one and stick with it.
Re: [PATCH v3 2/6] KVM: SVM: Update "APICv in x2APIC without x2AVIC" in avic.c, not svm.c
Quoted 9 lines
Question then. Does anyone have a preference/opinion between explicitly passing in ops to vendor specific helpers, vs. making {svm,vt}_x86_ops globally visible?
I don't love creating "hidden" dependencies, in quotes because in this case it's relatively easy to establish that the
setup()helpers modify {svm,vt}_x86_ops, i.e. surprises should be rare.On the other hand, I do agree it's helpful to be able to see exactly where {svm,vt}_x86_ops are updated.
I think passing in ops to vendor-specific helpers looks a bit indirect as the parameter should always be svm_x86_ops for AMD or vt_x86_ops for Intel.
I slightly prefer making {svm,vt}_x86_ops globally visible.
Quoted 3 lines
And most importantly, I want to be consistent between VMX and SVM, i.e. I want to pick one and stick with it.
Sean Christopherson
maintainerPATCH v3 3/6 KVM: SVM: Always print "AVIC enabled" separately, even when force enabled
In reply to Sean ChristophersonPrint the customary "AVIC enabled" informational message even when AVIC is force enabled on a system that doesn't advertise supported for AVIC in CPUID, as not printing the standard message can confuse users and tools.
Opportunistically clean up the scary message when AVIC is force enabled, but keep it as separate message so that it is printed at level "warn", versus the standard message only being printed for level "info".
--- arch/x86/kvm/svm/avic.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-)
arch/x86/kvm/svm/avic.c +9 -10
@@ -1167,16 +1167,15 @@ bool __init avic_hardware_setup(struct kvm_x86_ops *svm_ops)return false; } -if (boot_cpu_has (X86_FEATURE_AVIC )) { -pr_info ("AVIC enabled\n" ); - }else if (force_avic) { -/* -* Some older systems does not advertise AVIC support. -* See Revision Guide for specific AMD processor for more detail. -*/ -pr_warn ("AVIC is not supported in CPUID but force enabled" ); -pr_warn ("Your system might crash and burn" ); - } +/* +* Print a scary message if AVIC is force enabled to make it abundantly +* clear that ignoring CPUID could have repercussions. See Revision +* Guide for specific AMD processor for more details. +*/ +if (!boot_cpu_has (X86_FEATURE_AVIC )) +pr_warn ("AVIC unsupported in CPUID but force enabled, your system might crash and burn\n" ); + +pr_info ("AVIC enabled\n" );/* AVIC is a prerequisite for x2AVIC. */ x2avic_enabled =boot_cpu_has (X86_FEATURE_X2AVIC );
Signature · 1 line
2.51.0.470.ga7dc726c21-goog
Re: [PATCH v3 3/6] KVM: SVM: Always print "AVIC enabled" separately, even when force enabled
Quoted 2 lines · Sean Christopherson
Print the customary "AVIC enabled" informational message even when AVIC is force enabled on a system that doesn't advertise supported for AVIC in
^^ support
Quoted 4 lines
CPUID, as not printing the standard message can confuse users and tools.
Opportunistically clean up the scary message when AVIC is force enabled, but keep it as separate message so that it is printed at level "warn",
a separate message
Quoted 3 lines
versus the standard message only being printed for level "info".
Suggested-by Naveen N Rao <[email protected]>
Please use (with attribution):
Naveen N Rao (AMD) <[email protected]>
Quoted 4 lines
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/svm/avic.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-)
- Naveen
Sean Christopherson
maintainerPATCH v3 4/6 KVM: SVM: Don't advise the user to do force_avic=y (when x2AVIC is detected)
In reply to Sean ChristophersonDon't advise the end user to try to force enable AVIC when x2AVIC is reported as supported in CPUID, as forcefully enabling AVIC isn't something that should be done lightly. E.g. some Zen4 client systems hide AVIC but leave x2AVIC behind, and while such a configuration is indeed due to buggy firmware in the sense the reporting x2AVIC without AVIC is nonsensical, KVM has no idea _why_ firmware disabled AVIC in the first place.
Suggesting that the user try to run with force_avic=y is sketchy even if the user explicitly tries to enable AVIC, and will be downright irresponsible once KVM starts enabling AVIC by default. Alternatively, KVM could print the message only when the user explicitly asks for AVIC, but running with force_avic=y isn't something that should be encouraged for random users. force_avic is a useful knob for developers and perhaps even advanced users, but isn't something that KVM should advertise broadly.
Opportunistically append a newline to the pr_warn() so that it prints out immediately, and tweak the message to say that AVIC is unsupported instead of disabled (disabled suggests that the kernel/KVM is somehow responsible).
--- arch/x86/kvm/svm/avic.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/avic.c +2 -4
@@ -1154,10 +1154,8 @@ bool __init avic_hardware_setup(struct kvm_x86_ops *svm_ops)/* AVIC is a prerequisite for x2AVIC. */ if (!boot_cpu_has (X86_FEATURE_AVIC ) && !force_avic) { -if (boot_cpu_has (X86_FEATURE_X2AVIC )) { -pr_warn (FW_BUG "Cannot support x2AVIC due to AVIC is disabled" ); -pr_warn (FW_BUG "Try enable AVIC using force_avic option" ); - } +if (boot_cpu_has (X86_FEATURE_X2AVIC )) +pr_warn (FW_BUG "Cannot enable x2AVIC, AVIC is unsupported\n" );return false; }
Signature · 1 line
2.51.0.470.ga7dc726c21-goog
Re: [PATCH v3 4/6] KVM: SVM: Don't advise the user to do force_avic=y (when x2AVIC is detected)
Quoted 2 lines · Sean Christopherson
Don't advise the end user to try to force enable AVIC when x2AVIC is reported as supported in CPUID,
in CPUID but AVIC isn't,
Quoted 4 lines
as forcefully enabling AVIC isn't something
that should be done lightly. E.g. some Zen4 client systems hide AVIC but leave x2AVIC behind, and while such a configuration is indeed due to buggy firmware in the sense the reporting x2AVIC without AVIC is nonsensical,
^^ that
Quoted 10 lines
KVM has no idea _why_ firmware disabled AVIC in the first place.
Suggesting that the user try to run with force_avic=y is sketchy even if the user explicitly tries to enable AVIC, and will be downright irresponsible once KVM starts enabling AVIC by default. Alternatively, KVM could print the message only when the user explicitly asks for AVIC, but running with force_avic=y isn't something that should be encouraged for random users.
force_avicis a useful knob for developers and perhaps even advanced users, but isn't something that KVM should advertise broadly.
Quoted 5 lines
Opportunistically append a newline to the
pr_warn()so that it prints out immediately, and tweak the message to say that AVIC is unsupported instead of disabled (disabled suggests that the kernel/KVM is somehow responsible).Suggested-by Naveen N Rao <[email protected]>
Quoted 4 lines
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/svm/avic.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
- Naveen
Sean Christopherson
maintainerPATCH v3 5/6 KVM: SVM: Move global "avic" variable to avic.c
In reply to Sean ChristophersonMove "avic" to avic.c so that it's colocated with the other AVIC specific globals and module params, and so that avic_hardware_setup() is a bit more self-contained, e.g. similar to sev_hardware_setup().
Deliberately set enable_apicv in svm.c as it's already globally visible (defined by kvm.ko, not by kvm-amd.ko), and to clearly capture the dependency on enable_apicv being initialized (svm_hardware_setup() clears several AVIC-specific hooks when enable_apicv is disabled).
Alternatively, clearing of the hooks (and enable_ipiv) could be moved to avic_hardware_setup(), but that's not obviously better, e.g. it's helpful to isolate the setting of enable_apicv when reading code from the generic x86 side of the world.
No functional change intended.
--- arch/x86/kvm/svm/avic.c | 32 ++++++++++++++++++++++++-------- arch/x86/kvm/svm/svm.c | 11 +---------- 2 files changed, 25 insertions(+), 18 deletions(-)
arch/x86/kvm/svm/avic.c +24 -8
@@ -64,6 +64,14 @@static_assert (__AVIC_GATAG (AVIC_VM_ID_MASK ,AVIC_VCPU_IDX_MASK ) ==-1u ); +/* +* enable / disable AVIC. Because the defaults differ for APICv +* support between VMX and SVM we cannot use module_param_named. +*/ +static bool avic; +module_param (avic, bool,0444 ); +module_param (enable_ipiv, bool,0444 ); +static bool force_avic;module_param_unsafe (force_avic, bool,0444 );
@@ -1141,15 +1149,9 @@ void avic_vcpu_unblocking(struct kvm_vcpu *vcpu)avic_vcpu_load (vcpu, vcpu->cpu); } -/* -* Note: -* - The module param avic enable both xAPIC and x2APIC mode. -* - Hypervisor can support both xAVIC and x2AVIC in the same guest. -* - The mode can be switched at run-time. -*/ -bool __init avic_hardware_setup (struct kvm_x86_ops *svm_ops) +static bool __init avic_want_avic_enable (void ) { -if (!npt_enabled) +if (!avic || !npt_enabled)return false;/* AVIC is a prerequisite for x2AVIC. */
@@ -1174,6 +1176,20 @@ bool __init avic_hardware_setup(struct kvm_x86_ops *svm_ops)pr_warn ("AVIC unsupported in CPUID but force enabled, your system might crash and burn\n" );pr_info ("AVIC enabled\n" ); +return true; +} + +/* +* Note: +* - The module param avic enable both xAPIC and x2APIC mode. +* - Hypervisor can support both xAVIC and x2AVIC in the same guest. +* - The mode can be switched at run-time. +*/ +bool __init avic_hardware_setup (struct kvm_x86_ops *svm_ops) +{ + avic =avic_want_avic_enable (); +if (!avic) +return false;/* AVIC is a prerequisite for x2AVIC. */ x2avic_enabled =boot_cpu_has (X86_FEATURE_X2AVIC );
arch/x86/kvm/svm/svm.c +1 -10
@@ -158,14 +158,6 @@ module_param(lbrv, int, 0444);static int tsc_scaling = true;module_param (tsc_scaling, int,0444 ); -/* -* enable / disable AVIC. Because the defaults differ for APICv -* support between VMX and SVM we cannot use module_param_named. -*/ -static bool avic; -module_param (avic, bool,0444 ); -module_param (enable_ipiv, bool,0444 ); -module_param (enable_device_posted_irqs, bool,0444 );bool __read_mostly dump_invalid_vmcb;
@@ -5354,8 +5346,7 @@ static __init int svm_hardware_setup(void) goto err; } - enable_apicv = avic = avic &&avic_hardware_setup (&svm_x86_ops); - + enable_apicv =avic_hardware_setup (&svm_x86_ops);if (!enable_apicv) { enable_ipiv = false; svm_x86_ops.vcpu_blocking =NULL ;
Signature · 1 line
2.51.0.470.ga7dc726c21-goog
Re: [PATCH v3 5/6] KVM: SVM: Move global "avic" variable to avic.c
Quoted 44 lines · Sean Christopherson
Move "avic" to
avic.cso that it's colocated with the other AVIC specific globals and module params, and so thatavic_hardware_setup()is a bit more self-contained, e.g. similar tosev_hardware_setup().Deliberately set
enable_apicvinsvm.cas it's already globally visible (defined by kvm.ko, not by kvm-amd.ko), and to clearly capture the dependency onenable_apicvbeing initialized (svm_hardware_setup()clears several AVIC-specific hooks whenenable_apicvis disabled).Alternatively, clearing of the hooks (and
enable_ipiv) could be moved toavic_hardware_setup(), but that's not obviously better, e.g. it's helpful to isolate the setting ofenable_apicvwhen reading code from the generic x86 side of the world.No functional change intended.
Cc Naveen N Rao (AMD) <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/svm/avic.c | 32 ++++++++++++++++++++++++-------- arch/x86/kvm/svm/svm.c | 11 +---------- 2 files changed, 25 insertions(+), 18 deletions(-)arch/x86/kvm/svm/avic.c +8
-0@@ -64,6 +64,14 @@static_assert (__AVIC_GATAG (AVIC_VM_ID_MASK ,AVIC_VCPU_IDX_MASK ) ==-1u ); +/* +* enable / disable AVIC. Because the defaults differ for APICv +* support between VMX and SVM we cannot use module_param_named. +*/ +static bool avic; +module_param (avic, bool,0444 ); +module_param (enable_ipiv, bool,0444 ); +static bool force_avic;module_param_unsafe (force_avic, bool,0444 );@@ -1141,15 +1149,9 @@ void avic_vcpu_unblocking(struct kvm_vcpu *vcpu)avic_vcpu_load (vcpu, vcpu->cpu);
arch/x86/kvm/svm/avic.c void avic_vcpu_unblocking(struct kvm_vcpu *vcpu) · line 1152
Quoted 10 lines
arch/x86/kvm/svm/avic.c +1
-7} -/* -* Note: -* - The module param avic enable both xAPIC and x2APIC mode. -* - Hypervisor can support both xAVIC and x2AVIC in the same guest. -* - The mode can be switched at run-time. -*/ -bool __init avic_hardware_setup (struct kvm_x86_ops *svm_ops) +static bool __init avic_want_avic_enable (void )
Maybe avic_can_enable()?
arch/x86/kvm/svm/avic.c bool __init avic_hardware_setup(struct kvm_x86_ops *svm_ops) · line 1178
Quoted 10 lines
arch/x86/kvm/svm/avic.c +1
-1{ -if (!npt_enabled) +if (!avic || !npt_enabled)return false;/* AVIC is a prerequisite for x2AVIC. */ @@ -1174,6 +1176,20 @@ bool __init avic_hardware_setup(struct kvm_x86_ops *svm_ops)pr_warn ("AVIC unsupported in CPUID but force enabled, your system might crash and burn\n" );pr_info ("AVIC enabled\n" );
I think it would be good to keep this in avic_hardware_setup() alongside the message printing "x2AVIC enabled".
Otherwise:
- Naveen
Sean Christopherson
maintainerRe: [PATCH v3 5/6] KVM: SVM: Move global "avic" variable to avic.c
Quoted 5 lines · Naveen N Rao
Quoted 4 lines · Sean Christopherson
arch/x86/kvm/svm/avic.c +0
-0@@ -1141,15 +1149,9 @@ void avic_vcpu_unblocking(struct kvm_vcpu *vcpu)avic_vcpu_load (vcpu, vcpu->cpu); }
Quoted 10 lines
arch/x86/kvm/svm/avic.c void avic_vcpu_unblocking(struct kvm_vcpu *vcpu) · line 1152
Quoted 8 lines
arch/x86/kvm/svm/avic.c +1
-7-/* -* Note: -* - The module param avic enable both xAPIC and x2APIC mode. -* - Hypervisor can support both xAVIC and x2AVIC in the same guest. -* - The mode can be switched at run-time. -*/ -bool __init avic_hardware_setup (struct kvm_x86_ops *svm_ops) +static bool __init avic_want_avic_enable (void )Maybe
avic_can_enable()?
That was actualy one of my first names, but I didn't want to use "can" because (to me at least) that doesn't capture that the helper is incorporating input from the user, i.e. that it's also checking what the user "wants".
I agree the name isn't great. Does avic_want_avic_enabled() read any better?
Quoted 5 lines
Quoted 5 lines
arch/x86/kvm/svm/avic.c +1
-1{ -if (!npt_enabled) +if (!avic || !npt_enabled)return false;
Quoted 8 lines
arch/x86/kvm/svm/avic.c bool __init avic_hardware_setup(struct kvm_x86_ops *svm_ops) · line 1178
Quoted 5 lines
arch/x86/kvm/svm/avic.c +0
-0/* AVIC is a prerequisite for x2AVIC. */ @@ -1174,6 +1176,20 @@ bool __init avic_hardware_setup(struct kvm_x86_ops *svm_ops)pr_warn ("AVIC unsupported in CPUID but force enabled, your system might crash and burn\n" );pr_info ("AVIC enabled\n" );I think it would be good to keep this in
avic_hardware_setup()alongside the message printing "x2AVIC enabled".
+1, looks waaay better that way.
Re: [PATCH v3 5/6] KVM: SVM: Move global "avic" variable to avic.c
Quoted 17 lines · Sean Christopherson
Quoted 5 lines · Naveen N Rao
Quoted 4 lines · Sean Christopherson
arch/x86/kvm/svm/avic.c +0
-0@@ -1141,15 +1149,9 @@ void avic_vcpu_unblocking(struct kvm_vcpu *vcpu)avic_vcpu_load (vcpu, vcpu->cpu); }Quoted 10 lines
arch/x86/kvm/svm/avic.c void avic_vcpu_unblocking(struct kvm_vcpu *vcpu) · line 1152
Quoted 8 lines
arch/x86/kvm/svm/avic.c +1
-7-/* -* Note: -* - The module param avic enable both xAPIC and x2APIC mode. -* - Hypervisor can support both xAVIC and x2AVIC in the same guest. -* - The mode can be switched at run-time. -*/ -bool __init avic_hardware_setup (struct kvm_x86_ops *svm_ops) +static bool __init avic_want_avic_enable (void )Maybe
avic_can_enable()?
Quoted 3 lines
That was actualy one of my first names, but I didn't want to use "can" because (to me at least) that doesn't capture that the helper is incorporating input from the user, i.e. that it's also checking what the user "wants".
Makes sense.
Quoted 2 lines
I agree the name isn't great. Does
avic_want_avic_enabled()read any better?
Yes, though I think avic_want_enabled() suffices. But, I'm ok if you want it to be explicit.
- Naveen
Sean Christopherson
maintainerPATCH v3 6/6 KVM: SVM: Enable AVIC by default for Zen4+ if x2AVIC is support
In reply to Sean ChristophersonFrom: Naveen N Rao <[email protected]>
AVIC and x2AVIC are fully functional since Zen 4, with no known hardware errata. Enable AVIC and x2AVIC by default on Zen4+ so long as x2AVIC is supported (to avoid enabling partial support for APIC virtualization by default).
Internally, convert "avic" to an integer so that KVM can identify if the user has asked to explicitly enable or disable AVIC, i.e. so that KVM doesn't override an explicit 'y' from the user. Arbitrarily use -1 to denote auto-mode, and accept the string "auto" for the module param in addition to standard boolean values, i.e. continue to allow to the user configure the "avic" module parameter to explicitly enable/disable AVIC.
To again maintain backward compatibility with a standard boolean param, set KERNEL_PARAM_OPS_FL_NOARG, which tells the params infrastructure to allow empty values for %true, i.e. to interpret a bare "avic" as "avic=y". Take care to check for a NULL @val when looking for "auto"!
Lastly, always print "avic" as a boolean, since auto-mode is resolved during module initialization, i.e. the user should never see "auto" in sysfs.
--- arch/x86/kvm/svm/avic.c | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/avic.c +35 -4
@@ -64,12 +64,31 @@static_assert (__AVIC_GATAG (AVIC_VM_ID_MASK ,AVIC_VCPU_IDX_MASK ) ==-1u ); +#define AVIC_AUTO_MODE -1 + +static int avic_param_set (const char *val,const struct kernel_param *kp) +{ +if (val &&sysfs_streq (val,"auto" )) { + *(int *)kp->arg =AVIC_AUTO_MODE ; +return 0 ; + } + +return param_set_bint (val, kp); +} +static const struct kernel_param_ops avic_ops = { + .flags =KERNEL_PARAM_OPS_FL_NOARG , + .set = avic_param_set, + .get = param_get_bool, +}; +/* -* enable / disable AVIC. Because the defaults differ for APICv -* support between VMX and SVM we cannot use module_param_named. +* Enable / disable AVIC. In "auto" mode (default behavior), AVIC is enabled +* for Zen4+ CPUs with x2AVIC (and all other criteria for enablement are met). */ -static bool avic; -module_param (avic, bool,0444 ); +static int avic =AVIC_AUTO_MODE ; +module_param_cb (avic, &avic_ops, &avic,0444 ); +__MODULE_PARM_TYPE (avic,"bool" ); +module_param (enable_ipiv, bool,0444 );static bool force_avic;
@@ -1151,6 +1170,18 @@ void avic_vcpu_unblocking(struct kvm_vcpu *vcpu)static bool __init avic_want_avic_enable (void ) { +/* +* In "auto" mode, enable AVIC by default for Zen4+ if x2AVIC is +* supported (to avoid enabling partial support by default, and because +* x2AVIC should be supported by all Zen4+ CPUs). Explicitly check for +* family 0x19 and later (Zen5+), as the kernel's synthetic ZenX flags +* aren't inclusive of previous generations, i.e. the kernel will set +* at most one ZenX feature flag. +*/ +if (avic ==AVIC_AUTO_MODE ) + avic =boot_cpu_has (X86_FEATURE_X2AVIC ) && + (boot_cpu_data.x86 >0x19 ||cpu_feature_enabled (X86_FEATURE_ZEN4 )); +if (!avic || !npt_enabled)return false;
Signature · 1 line
2.51.0.470.ga7dc726c21-goog
Re: [PATCH v3 6/6] KVM: SVM: Enable AVIC by default for Zen4+ if x2AVIC is support
Quoted 1 line · Sean Christopherson
KVM: SVM: Enable AVIC by default for Zen4+ if x2AVIC is support
^^ supported
Quoted 1 line
From: Naveen N Rao <[email protected]>
_ From: Naveen N Rao (AMD) <[email protected]>
Quoted 11 lines
AVIC and x2AVIC are fully functional since Zen 4, with no known hardware errata. Enable AVIC and x2AVIC by default on Zen4+ so long as x2AVIC is supported (to avoid enabling partial support for APIC virtualization by default).
Internally, convert "avic" to an integer so that KVM can identify if the user has asked to explicitly enable or disable AVIC, i.e. so that KVM doesn't override an explicit 'y' from the user. Arbitrarily use -1 to denote auto-mode, and accept the string "auto" for the module param in addition to standard boolean values, i.e. continue to allow to the user
allow the user to
Quoted 27 lines
configure the "avic" module parameter to explicitly enable/disable AVIC.
To again maintain backward compatibility with a standard boolean param, set
KERNEL_PARAM_OPS_FL_NOARG, which tells the params infrastructure to allow empty values for %true, i.e. to interpret a bare "avic" as "avic=y". Take care to check for a NULL @val when looking for "auto"!Lastly, always print "avic" as a boolean, since auto-mode is resolved during module initialization, i.e. the user should never see "auto" in sysfs.
Signed-off-by Naveen N Rao (AMD) <[email protected]>Co-developed-by Sean Christopherson <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/svm/avic.c | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-)arch/x86/kvm/svm/avic.c +1
-0@@ -64,12 +64,31 @@static_assert (__AVIC_GATAG (AVIC_VM_ID_MASK ,AVIC_VCPU_IDX_MASK ) ==-1u ); +#define AVIC_AUTO_MODE -1
arch/x86/kvm/svm/avic.c line 77
Quoted 10 lines
arch/x86/kvm/svm/avic.c +10
-0+ +static int avic_param_set (const char *val,const struct kernel_param *kp) +{ +if (val &&sysfs_streq (val,"auto" )) { + *(int *)kp->arg =AVIC_AUTO_MODE ; +return 0 ; + } + +return param_set_bint (val, kp); +}
Nit: missing newline.
Quoted 25 lines
arch/x86/kvm/svm/avic.c +12
-4+static const struct kernel_param_ops avic_ops = { + .flags =KERNEL_PARAM_OPS_FL_NOARG , + .set = avic_param_set, + .get = param_get_bool, +}; +/* -* enable / disable AVIC. Because the defaults differ for APICv -* support between VMX and SVM we cannot use module_param_named. +* Enable / disable AVIC. In "auto" mode (default behavior), AVIC is enabled +* for Zen4+ CPUs with x2AVIC (and all other criteria for enablement are met). */ -static bool avic; -module_param (avic, bool,0444 ); +static int avic =AVIC_AUTO_MODE ; +module_param_cb (avic, &avic_ops, &avic,0444 ); +__MODULE_PARM_TYPE (avic,"bool" ); +module_param (enable_ipiv, bool,0444 );static bool force_avic;@@ -1151,6 +1170,18 @@ void avic_vcpu_unblocking(struct kvm_vcpu *vcpu)static bool __init avic_want_avic_enable (void ) {
arch/x86/kvm/svm/avic.c void avic_vcpu_unblocking(struct kvm_vcpu *vcpu) · line 1182
Quoted 10 lines
arch/x86/kvm/svm/avic.c +10
-0+/* +* In "auto" mode, enable AVIC by default for Zen4+ if x2AVIC is +* supported (to avoid enabling partial support by default, and because +* x2AVIC should be supported by all Zen4+ CPUs). Explicitly check for +* family 0x19 and later (Zen5+), as the kernel's synthetic ZenX flags +* aren't inclusive of previous generations, i.e. the kernel will set +* at most one ZenX feature flag. +*/ +if (avic ==AVIC_AUTO_MODE ) + avic = boot_cpu_has(X86_FEATURE_X2AVIC) &&
This can use cpu_feature_enabled() as well, I think.
arch/x86/kvm/svm/avic.c void avic_vcpu_unblocking(struct kvm_vcpu *vcpu) · line 1186
Quoted 4 lines
arch/x86/kvm/svm/avic.c +2
-0+ (boot_cpu_data.x86 >0x19 ||cpu_feature_enabled (X86_FEATURE_ZEN4 )); +if (!avic || !npt_enabled)return false;
Otherwise, this LGTM.
- Naveen
Sean Christopherson
maintainerRe: [PATCH v3 6/6] KVM: SVM: Enable AVIC by default for Zen4+ if x2AVIC is support
Quoted 17 lines · Naveen N Rao
Quoted 4 lines · Sean Christopherson
arch/x86/kvm/svm/avic.c +0
-0@@ -1151,6 +1170,18 @@ void avic_vcpu_unblocking(struct kvm_vcpu *vcpu)static bool __init avic_want_avic_enable (void ) {arch/x86/kvm/svm/avic.c void avic_vcpu_unblocking(struct kvm_vcpu *vcpu) · line 1182
Quoted 10 lines
arch/x86/kvm/svm/avic.c +10
-0+/* +* In "auto" mode, enable AVIC by default for Zen4+ if x2AVIC is +* supported (to avoid enabling partial support by default, and because +* x2AVIC should be supported by all Zen4+ CPUs). Explicitly check for +* family 0x19 and later (Zen5+), as the kernel's synthetic ZenX flags +* aren't inclusive of previous generations, i.e. the kernel will set +* at most one ZenX feature flag. +*/ +if (avic ==AVIC_AUTO_MODE ) + avic = boot_cpu_has(X86_FEATURE_X2AVIC) &&This can use
cpu_feature_enabled()as well, I think.
It could, but I'm going to leave it as boot_cpu_has() for now, purely because the existing code uses boot_cpu_has() for X2AVIC and mixing the two adds "complexity" where none exists.
I'm definitely not opposed to using cpu_feature_enabled() in general, just not in this case (of course, we could just swap them all, but meh, it's init code).
Quoted 27 lines · Sean Christopherson
Enable AVIC by default for Zen4+, so long as x2AVIC is supported (which should be the case if AVIC is supported).
v3:
- Don't advise the user to enableforce_avic. [Naveen]
- Gather AVIC related module params inavic.c(by moving code/helpers toavic.c).
- Print "AVIC enabled" even when it's forced.
- Enable by default iff x2AVIC is supported.
- Use "auto" to select KVM's automatic/default behavior.v2: https://lore.kernel.org/all/[email protected]
v1: http://lkml.kernel.org/r/[email protected]
Naveen N Rao (1):
KVM: SVM: Enable AVIC by default for Zen4+ if x2AVIC is supportSean Christopherson (5):
KVM: SVM: Move x2AVIC MSR interception helper toavic.c
KVM: SVM: Update "APICv in x2APIC without x2AVIC" inavic.c, notsvm.c
KVM: SVM: Always print "AVIC enabled" separately, even when force
enabled
KVM: SVM: Don't advise the user to do force_avic=y (when x2AVIC is
detected)
KVM: SVM: Move global "avic" variable toavic.c
Quoted 4 lines
arch/x86/kvm/svm/avic.c | 149 +++++++++++++++++++++++++++++++++------- arch/x86/kvm/svm/svm.c | 62 +---------------- arch/x86/kvm/svm/svm.h | 4 +- 3 files changed, 125 insertions(+), 90 deletions(-)
Thanks for putting this together! I have confirmed that this correctly enables AVIC by default on a Zen 5 system and does not do so on a Zen 1 system.
For the series:
- Naveen