KVM: x86: EFER validity fixes and cleanups
Subsystems (5)
v2 cover KVM: x86: EFER validity fixes and cleanups
A couple of bug fixes for EFER validity checks, namely not doing any on KVM_SET_SREGS*, and incorrectly preserving EFER.SVME and EFER.LMSLE state after module unload+load.
The rest of the series reworks EFER supported bits and moves them into kvm_caps, ensuring they are always reinitialized on vendor module init. The final patch adds test cases to set_sregs that catch the bug fixed by patch 1.
v1 -> v2:
- Fix reversing the polarity of efer_reserved_bits to actually reverse the bitmask instead of the entire checks [Sashiko]
v1: https://lore.kernel.org/kvm/[email protected]/
Yosry Ahmed (7):
KVM: x86: Check EFER validity on KVM_SET_SREGS*
KVM: SVM: Disallow EFER.SVME and EFER.LSMLE if nested is disabled
KVM: x86: Disallow EFER.LME and EFER.LMA if long mode is not supported
KVM: x86: Add a per-vendor callback to setup EFER caps
KVM: x86: Reverse the polarity of efer_reserved_bits
KVM: x86: Move supported EFER bits to kvm_caps
KVM: selftests: Extend set_sregs test to cover EFER
arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 3 + arch/x86/kvm/msrs.c | 21 +---- arch/x86/kvm/msrs.h | 1 + arch/x86/kvm/regs.c | 3 +- arch/x86/kvm/svm/svm.c | 14 +++- arch/x86/kvm/x86.c | 18 ++-- .../selftests/kvm/include/x86/processor.h | 2 + .../selftests/kvm/x86/set_sregs_test.c | 83 ++++++++++++++----- 9 files changed, 96 insertions(+), 50 deletions(-)
base-commit: 50406d35f5635e1cc523e61409d57e851b5f5df8
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
PATCH v2 1/7 KVM: x86: Check EFER validity on KVM_SET_SREGS*
When handling userspace SREGS writes, check the validity of EFER (i.e. allowed bits) before writing the new value of EFER through the per-vendor set_efer callbacks. This prevents userspace from writing bogus values (e.g. EFER.SVME=1 with nested=0).
Note: on KVM_SET_MSRS, KVM only checks EFER validity in terms of KVM caps, not guest caps, so it is possible to set EFER bits that are supported by KVM but not by the guest CPUID. Potentially allowing userspace to set msrs before CPUID.
However, for KVM_SET_SREGS*, check the validity of the set bits against both KVM and guest caps. This is consistent with other validity checks (e.g. for CR4) that check validity against guest caps, which already imposes the need to set CPUID before SREGS.
--- arch/x86/kvm/regs.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/regs.c +2 -1
@@ -563,7 +563,8 @@ static bool kvm_is_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) }return kvm_is_valid_cr4 (vcpu, sregs->cr4) && -kvm_is_valid_cr0 (vcpu, sregs->cr0); +kvm_is_valid_cr0 (vcpu, sregs->cr0) && +kvm_valid_efer (vcpu, sregs->efer); } static int __set_sregs_common(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs,
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
PATCH v2 2/7 KVM: SVM: Disallow EFER.SVME and EFER.LSMLE if nested is disabled
In reply to Yosry AhmedExplicitly disallow setting EFER.SVME and EFER.LSMLE if nested virtualization is disabled on SVM, to prevent the bits remaining allowed if kvm_amd is loaded with nested=1 and then reloaded with nested=0.
This is a minimal fix for the benefit of stable backports, which will be followed by a more systematic fix (moving efer_reserved_bits to kvm_caps).
--- arch/x86/kvm/msrs.c | 8 +++++++- arch/x86/kvm/msrs.h | 1 + arch/x86/kvm/svm/svm.c | 3 +++ 3 files changed, 11 insertions(+), 1 deletion(-)
arch/x86/kvm/msrs.c +7 -1
@@ -660,10 +660,16 @@ static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info)void kvm_enable_efer_bits (u64 mask) { - efer_reserved_bits &= ~mask; + efer_reserved_bits &= ~mask; }EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_enable_efer_bits); +void kvm_disable_efer_bits (u64 mask) +{ + efer_reserved_bits |= mask; +} +EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_disable_efer_bits); +bool kvm_msr_allowed (struct kvm_vcpu *vcpu,u32 index,u32 type) {struct kvm_x86_msr_filter *msr_filter;
arch/x86/kvm/msrs.h +1 -0
@@ -59,6 +59,7 @@ int kvm_get_reg_list(struct kvm_vcpu *vcpu, struct kvm_reg_list __user *user_list);void kvm_enable_efer_bits (u64 ); +void kvm_disable_efer_bits (u64 );bool kvm_valid_efer (struct kvm_vcpu *vcpu,u64 efer);int kvm_emulate_msr_read (struct kvm_vcpu *vcpu,u32 index,u64 *data);int kvm_emulate_msr_write (struct kvm_vcpu *vcpu,u32 index,u64 data);
arch/x86/kvm/svm/svm.c +3 -0
@@ -5645,6 +5645,9 @@ static __init int svm_hardware_setup(void) r =nested_svm_init_msrpm_merge_offsets ();if (r)return r; + }else { +kvm_disable_efer_bits (EFER_SVME ); +kvm_disable_efer_bits (EFER_LMSLE ); }/*
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
PATCH v2 3/7 KVM: x86: Disallow EFER.LME and EFER.LMA if long mode is not supported
In reply to Yosry AhmedRemove EFER.LME and EFER.LMA from EFER reserved bits only if long mode is actually supported. KVM does check long-mode support before allowing the bits for guest writes and userspace writes through KVM_SET_SREGS* (in __kvm_valid_efer()), but userspace writes through KVM_SET_MSRS only check reserved bits.
A nice side effect (and the true motivation) is getting rid of the #ifdeffery when initializing efer_reserved_bits.
--- arch/x86/kvm/msrs.c | 10 +--------- arch/x86/kvm/x86.c | 3 +++ 2 files changed, 4 insertions(+), 9 deletions(-)
arch/x86/kvm/msrs.c +1 -9
@@ -19,16 +19,8 @@ bool __read_mostly report_ignored_msrs = true;module_param (report_ignored_msrs, bool,0644 );EXPORT_SYMBOL_FOR_KVM_INTERNAL (report_ignored_msrs); -/* EFER defaults: -* - enable syscall per default because its emulated by KVM -* - enable LME and LMA per default on 64 bit KVM -*/ -#ifdef CONFIG_X86_64 -static -u64 __read_mostly efer_reserved_bits = ~((u64)(EFER_SCE |EFER_LME |EFER_LMA )); -#else +/* Enable syscall by default because its emulated by KVM */ static u64 __read_mostly efer_reserved_bits = ~((u64 )EFER_SCE ); -#endif #define MAX_IO_MSRS 256
arch/x86/kvm/x86.c +3 -0
@@ -6892,6 +6892,9 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_setup_xss_caps);static void kvm_setup_efer_caps (void ) { +if (kvm_cpu_cap_has (X86_FEATURE_LM )) +kvm_enable_efer_bits (EFER_LME |EFER_LMA ); +if (kvm_cpu_cap_has (X86_FEATURE_NX ))kvm_enable_efer_bits (EFER_NX );
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
PATCH v2 4/7 KVM: x86: Add a per-vendor callback to setup EFER caps
In reply to Yosry AhmedMove handling EFER.SVME and EFER.LMSLE from hardware setup to a new optional per-vendor callback invoked from kvm_setup_efer_caps(). This centralizes allowed EFER bits handling to kvm_setup_efer_caps(), facilitating following changes to move efer_reserved_bits into kvm_caps.
Move the call to kvm_setup_efer_caps() after per-vendor ops are initialized.
No functional change intended.
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm/svm.c | 20 +++++++++++++------- arch/x86/kvm/x86.c | 6 ++++-- 4 files changed, 19 insertions(+), 9 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -45,6 +45,7 @@ KVM_X86_OP_OPTIONAL(post_set_cr3) KVM_X86_OP(is_valid_cr4) KVM_X86_OP(set_cr4) KVM_X86_OP(set_efer) +KVM_X86_OP_OPTIONAL(setup_efer_caps) KVM_X86_OP(get_idt) KVM_X86_OP(set_idt) KVM_X86_OP(get_gdt)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1730,6 +1730,7 @@ struct kvm_x86_ops {bool (*is_valid_cr4)(struct kvm_vcpu *vcpu,unsigned long cr4);void (*set_cr4)(struct kvm_vcpu *vcpu,unsigned long cr4);int (*set_efer)(struct kvm_vcpu *vcpu,u64 efer); +void (*setup_efer_caps)(void );void (*get_idt)(struct kvm_vcpu *vcpu,struct desc_ptr *dt);void (*set_idt)(struct kvm_vcpu *vcpu,struct desc_ptr *dt);void (*get_gdt)(struct kvm_vcpu *vcpu,struct desc_ptr *dt);
arch/x86/kvm/svm/svm.c +13 -7
@@ -274,6 +274,18 @@ int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)return 0 ; } +static void svm_setup_efer_caps (void ) +{ +if (nested) { +kvm_enable_efer_bits (EFER_SVME ); +if (!boot_cpu_has (X86_FEATURE_EFER_LMSLE_MBZ )) +kvm_enable_efer_bits (EFER_LMSLE ); + }else { +kvm_disable_efer_bits (EFER_SVME ); +kvm_disable_efer_bits (EFER_LMSLE ); + } +} +static u32 svm_get_interrupt_shadow (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu);
@@ -5364,6 +5376,7 @@ struct kvm_x86_ops svm_x86_ops __initdata = { .is_valid_cr4 = svm_is_valid_cr4, .set_cr4 = svm_set_cr4, .set_efer = svm_set_efer, + .setup_efer_caps = svm_setup_efer_caps, .get_idt = svm_get_idt, .set_idt = svm_set_idt, .get_gdt = svm_get_gdt,
@@ -5638,16 +5651,9 @@ static __init int svm_hardware_setup(void)if (nested) {pr_info ("Nested Virtualization enabled\n" ); -kvm_enable_efer_bits (EFER_SVME ); -if (!boot_cpu_has (X86_FEATURE_EFER_LMSLE_MBZ )) -kvm_enable_efer_bits (EFER_LMSLE ); - r =nested_svm_init_msrpm_merge_offsets ();if (r)return r; - }else { -kvm_disable_efer_bits (EFER_SVME ); -kvm_disable_efer_bits (EFER_LMSLE ); }/*
arch/x86/kvm/x86.c +4 -2
@@ -6903,6 +6903,8 @@ static void kvm_setup_efer_caps(void)if (kvm_cpu_cap_has (X86_FEATURE_AUTOIBRS ))kvm_enable_efer_bits (EFER_AUTOIBRS ); + +kvm_x86_call (setup_efer_caps)(); }static inline void kvm_ops_update (struct kvm_x86_init_ops *ops)
@@ -7041,13 +7043,13 @@ int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops)if (r !=0 ) goto out_mmu_exit; -kvm_setup_efer_caps (); - enable_device_posted_irqs &= enable_apicv &&irq_remapping_cap (IRQ_POSTING_CAP );kvm_ops_update (ops); +kvm_setup_efer_caps (); +for_each_online_cpu (cpu) {smp_call_function_single (cpu, kvm_x86_check_cpu_compat, &r,1 );if (r <0 )
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
PATCH v2 5/7 KVM: x86: Reverse the polarity of efer_reserved_bits
In reply to Yosry AhmedIn preparation for moving efer_reserved_bits into kvm_caps, reverse its polarity and make it efer_supported_bits, to be more consistent with other fields in kvm_caps.
No functional change intended.
--- arch/x86/kvm/msrs.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
arch/x86/kvm/msrs.c +5 -5
@@ -20,7 +20,7 @@ module_param(report_ignored_msrs, bool, 0644);EXPORT_SYMBOL_FOR_KVM_INTERNAL (report_ignored_msrs);/* Enable syscall by default because its emulated by KVM */ -static u64 __read_mostly efer_reserved_bits = ~((u64 )EFER_SCE ); +static u64 __read_mostly efer_supported_bits = ((u64 )EFER_SCE );#define MAX_IO_MSRS 256
@@ -606,7 +606,7 @@ static bool __kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer) }bool kvm_valid_efer (struct kvm_vcpu *vcpu,u64 efer) { -if (efer & efer_reserved_bits) +if (efer & ~efer_supported_bits)return false;return __kvm_valid_efer (vcpu, efer);
@@ -619,7 +619,7 @@ static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info)u64 efer = msr_info->data;int r; -if (efer & efer_reserved_bits) +if (efer & ~efer_supported_bits)return 1 ;if (!msr_info->host_initiated) {
@@ -652,13 +652,13 @@ static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info)void kvm_enable_efer_bits (u64 mask) { - efer_reserved_bits &= ~mask; + efer_supported_bits |= mask; }EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_enable_efer_bits);void kvm_disable_efer_bits (u64 mask) { - efer_reserved_bits |= mask; + efer_supported_bits &= ~mask; }EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_disable_efer_bits);
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
PATCH v2 6/7 KVM: x86: Move supported EFER bits to kvm_caps
In reply to Yosry AhmedSupported EFER bits naturally fits into kvm_caps because it needs to be recomputed during vendor init (e.g. to account for EFER.SVME being allowed/disallowed based on nested being enabled/disabled).
Move efer_supported_bits into kvm_caps as supported_efer_bits (for naming consistency), and reinitialize it at the beginning of kvm_setup_efer_caps(), removing the need to clear unsupported bits in vendor code (e.g. EFER.SVME).
As the bitmask is now globally visible as part of kvm_caps, there's little use for helpers to enable/disable specific bits, so drop them and open-code updates to kvm_caps.supported_efer_bits.
No functional change intended.
--- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/msrs.c | 19 ++----------------- arch/x86/kvm/svm/svm.c | 7 ++----- arch/x86/kvm/x86.c | 11 +++++++---- 4 files changed, 13 insertions(+), 26 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -329,6 +329,8 @@ struct kvm_caps {u64 supported_xss;u64 supported_perf_cap; +u64 supported_efer_bits; +u64 supported_quirks;u64 inapplicable_quirks; };
arch/x86/kvm/msrs.c +2 -17
@@ -19,9 +19,6 @@ bool __read_mostly report_ignored_msrs = true;module_param (report_ignored_msrs, bool,0644 );EXPORT_SYMBOL_FOR_KVM_INTERNAL (report_ignored_msrs); -/* Enable syscall by default because its emulated by KVM */ -static u64 __read_mostly efer_supported_bits = ((u64 )EFER_SCE ); -#define MAX_IO_MSRS 256struct msr_bitmap_range {
@@ -606,7 +603,7 @@ static bool __kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer) }bool kvm_valid_efer (struct kvm_vcpu *vcpu,u64 efer) { -if (efer & ~efer_supported_bits) +if (efer & ~kvm_caps.supported_efer_bits)return false;return __kvm_valid_efer (vcpu, efer);
@@ -619,7 +616,7 @@ static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info)u64 efer = msr_info->data;int r; -if (efer & ~efer_supported_bits) +if (efer & ~kvm_caps.supported_efer_bits)return 1 ;if (!msr_info->host_initiated) {
@@ -650,18 +647,6 @@ static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info)return 0 ; } -void kvm_enable_efer_bits (u64 mask) -{ - efer_supported_bits |= mask; -} -EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_enable_efer_bits); - -void kvm_disable_efer_bits (u64 mask) -{ - efer_supported_bits &= ~mask; -} -EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_disable_efer_bits); -bool kvm_msr_allowed (struct kvm_vcpu *vcpu,u32 index,u32 type) {struct kvm_x86_msr_filter *msr_filter;
arch/x86/kvm/svm/svm.c +2 -5
@@ -277,12 +277,9 @@ int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)static void svm_setup_efer_caps (void ) {if (nested) { -kvm_enable_efer_bits (EFER_SVME ); + kvm_caps.supported_efer_bits |=EFER_SVME ;if (!boot_cpu_has (X86_FEATURE_EFER_LMSLE_MBZ )) -kvm_enable_efer_bits (EFER_LMSLE ); - }else { -kvm_disable_efer_bits (EFER_SVME ); -kvm_disable_efer_bits (EFER_LMSLE ); + kvm_caps.supported_efer_bits |=EFER_LMSLE ; } }
arch/x86/kvm/x86.c +7 -4
@@ -6892,17 +6892,20 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_setup_xss_caps);static void kvm_setup_efer_caps (void ) { +/* Enable syscall by default because its emulated by KVM */ + kvm_caps.supported_efer_bits = (u64 )EFER_SCE ; +if (kvm_cpu_cap_has (X86_FEATURE_LM )) -kvm_enable_efer_bits (EFER_LME |EFER_LMA ); + kvm_caps.supported_efer_bits |= (EFER_LME |EFER_LMA );if (kvm_cpu_cap_has (X86_FEATURE_NX )) -kvm_enable_efer_bits (EFER_NX ); + kvm_caps.supported_efer_bits |=EFER_NX ;if (kvm_cpu_cap_has (X86_FEATURE_FXSR_OPT )) -kvm_enable_efer_bits (EFER_FFXSR ); + kvm_caps.supported_efer_bits |=EFER_FFXSR ;if (kvm_cpu_cap_has (X86_FEATURE_AUTOIBRS )) -kvm_enable_efer_bits (EFER_AUTOIBRS ); + kvm_caps.supported_efer_bits |=EFER_AUTOIBRS ;kvm_x86_call (setup_efer_caps)(); }
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
PATCH v2 7/7 KVM: selftests: Extend set_sregs test to cover EFER
In reply to Yosry AhmedExtend the set_sregs test to cover various bits in EFER. Update TEST_INVALID_CR_BIT() to operate on EFER as well as CRx (and rename it accordingly). Add test cases to check that EFER bits are disallowed without the relevant CPUID enablement.
--- .../selftests/kvm/include/x86/processor.h | 2 + .../selftests/kvm/x86/set_sregs_test.c | 83 ++++++++++++++----- 2 files changed, 64 insertions(+), 21 deletions(-)
tools/testing/selftests/kvm/include/x86/processor.h +2 -0
@@ -208,6 +208,7 @@ struct kvm_x86_cpu_feature {#define X86_FEATURE_PERFCTR_NB KVM_X86_CPU_FEATURE(0x80000001, 0, ECX, 24)#define X86_FEATURE_PERFCTR_LLC KVM_X86_CPU_FEATURE(0x80000001, 0, ECX, 28)#define X86_FEATURE_NX KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 20) +#define X86_FEATURE_FXSR_OPT KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 25)#define X86_FEATURE_GBPAGES KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 26)#define X86_FEATURE_RDTSCP KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 27)#define X86_FEATURE_LM KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 29)
@@ -226,6 +227,7 @@ struct kvm_x86_cpu_feature {#define X86_FEATURE_SEV KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 1)#define X86_FEATURE_SEV_ES KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 3)#define X86_FEATURE_SEV_SNP KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 4) +#define X86_FEATURE_AUTOIBRS KVM_X86_CPU_FEATURE(0x80000021, 0, EAX, 8)#define X86_FEATURE_GP_ON_USER_CPUID KVM_X86_CPU_FEATURE(0x80000021, 0, EAX, 17)#define X86_FEATURE_PERFMON_V2 KVM_X86_CPU_FEATURE(0x80000022, 0, EAX, 0)#define X86_FEATURE_LBR_PMC_FREEZE KVM_X86_CPU_FEATURE(0x80000022, 0, EAX, 2)
tools/testing/selftests/kvm/x86/set_sregs_test.c +62 -21
132 lines not on this page · raw message
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
[email protected]
AI reviewRe: [PATCH v2 6/7] KVM: x86: Move supported EFER bits to kvm_caps
In reply to Yosry AhmedThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Leftover function declarations for `kvm_enable_efer_bits()` and `kvm_disable_efer_bits()` in…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Leftover function declarations for kvm_enable_efer_bits() and kvm_disable_efer_bits() in arch/x86/kvm/msrs.h after their definitions were removed. --
commit eef68f2b444ee82f826a87d9dc12393124877c19
Author: Yosry Ahmed <[email protected]>
KVM: x86: Move supported EFER bits to kvm_caps
This commit moves the supported EFER bits into kvm_caps.supported_efer_bits to allow them to be recomputed during vendor initialization. It also removes the kvm_enable_efer_bits() and kvm_disable_efer_bits() helpers in favor of open-coding the updates directly to kvm_caps.
Quoted 4 lines
arch/x86/kvm/msrs.c +0
-0
[ ... ]
Quoted 6 lines
arch/x86/kvm/msrs.c +0
-2@@ -650,18 +647,6 @@ static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info)return 0 ; } -void kvm_enable_efer_bits (u64 mask) -{
arch/x86/kvm/msrs.c static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 664
Quoted 10 lines
arch/x86/kvm/msrs.c +0
-10- efer_supported_bits |= mask; -} -EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_enable_efer_bits); - -void kvm_disable_efer_bits (u64 mask) -{ - efer_supported_bits &= ~mask; -} -EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_disable_efer_bits); -
[Severity: Low]
This isn't a bug, but did this commit leave dangling function declarations for kvm_enable_efer_bits() and kvm_disable_efer_bits() in arch/x86/kvm/msrs.h?
It appears the prototypes were not removed from the header file when these implementations were deleted here.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Re: [PATCH v2 6/7] KVM: x86: Move supported EFER bits to kvm_caps
Quoted 40 lines · On Mon, Jul 06, 2026 at 08:05:33PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Leftover function declarations forkvm_enable_efer_bits()andkvm_disable_efer_bits()inarch/x86/kvm/msrs.hafter their definitions were removed. --commit
eef68f2b444ee82f826a87d9dc12393124877c19
Author: Yosry Ahmed <[email protected]>KVM: x86: Move supported EFER bits to
kvm_capsThis commit moves the supported EFER bits into kvm_caps.supported_efer_bits to allow them to be recomputed during vendor initialization. It also removes the
kvm_enable_efer_bits()andkvm_disable_efer_bits()helpers in favor of open-coding the updates directly tokvm_caps.Quoted 4 lines
arch/x86/kvm/msrs.c +0
-0[ ... ]
Quoted 6 lines
arch/x86/kvm/msrs.c +0
-2@@ -650,18 +647,6 @@ static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info)return 0 ; } -void kvm_enable_efer_bits (u64 mask) -{arch/x86/kvm/msrs.c static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 664
Quoted 10 lines
arch/x86/kvm/msrs.c +0
-10- efer_supported_bits |= mask; -} -EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_enable_efer_bits); - -void kvm_disable_efer_bits (u64 mask) -{ - efer_supported_bits &= ~mask; -} -EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_disable_efer_bits); -[Severity: Low]
This isn't a bug, but did this commit leave dangling function declarations forkvm_enable_efer_bits()andkvm_disable_efer_bits()inarch/x86/kvm/msrs.h?
Quoted 2 lines
It appears the prototypes were not removed from the header file when these implementations were deleted here.
Yeah, this needs to be folded in:
arch/x86/kvm/msrs.h +0 -2
@@ -58,8 +58,6 @@ int kvm_get_set_one_reg(struct kvm_vcpu *vcpu, unsigned int ioctl,int kvm_get_reg_list (struct kvm_vcpu *vcpu,struct kvm_reg_list __user *user_list); -void kvm_enable_efer_bits (u64 ); -void kvm_disable_efer_bits (u64 );bool kvm_valid_efer (struct kvm_vcpu *vcpu,u64 efer);int kvm_emulate_msr_read (struct kvm_vcpu *vcpu,u32 index,u64 *data);int kvm_emulate_msr_write (struct kvm_vcpu *vcpu,u32 index,u64 data);
Sean Christopherson
maintainerRe: [PATCH v2 4/7] KVM: x86: Add a per-vendor callback to setup EFER caps
In reply to Yosry AhmedQuoted 7 lines · Yosry Ahmed
Move handling EFER.SVME and EFER.LMSLE from hardware setup to a new optional per-vendor callback invoked from
kvm_setup_efer_caps(). This centralizes allowed EFER bits handling tokvm_setup_efer_caps(), facilitating following changes to moveefer_reserved_bitsintokvm_caps.Move the call to
kvm_setup_efer_caps()after per-vendor ops are initialized.
Why?
arch/x86/kvm/x86.c static void kvm_setup_efer_caps(void) · line 6907
Quoted 10 lines
arch/x86/kvm/x86.c +2
-0@@ -6903,6 +6903,8 @@ static void kvm_setup_efer_caps(void)if (kvm_cpu_cap_has (X86_FEATURE_AUTOIBRS ))kvm_enable_efer_bits (EFER_AUTOIBRS ); + +kvm_x86_call (setup_efer_caps)();
I would rather move the togging to kvm_setup_efer_caps(), e.g.
static void kvm_setup_efer_caps(void)
{
/* Enable syscall by default because its emulated by KVM */
kvm_caps.supported_efer_bits = (u64)EFER_SCE;
if (kvm_cpu_cap_has(X86_FEATURE_LM))
kvm_caps.supported_efer_bits |= (EFER_LME | EFER_LMA);
if (kvm_cpu_cap_has(X86_FEATURE_NX))
kvm_caps.supported_efer_bits |= EFER_NX;
if (kvm_cpu_cap_has(X86_FEATURE_FXSR_OPT))
kvm_caps.supported_efer_bits |= EFER_FFXSR;
if (kvm_cpu_cap_has(X86_FEATURE_AUTOIBRS))
kvm_caps.supported_efer_bits |= EFER_AUTOIBRS;
if (kvm_cpu_cap_has(X86_FEATURE_SVM)) {
kvm_caps.supported_efer_bits |= EFER_SVME;
if (!boot_cpu_has(X86_FEATURE_EFER_LMSLE_MBZ))
kvm_caps.supported_efer_bits |= EFER_LMSLE;
}}
Re: [PATCH v2 4/7] KVM: x86: Add a per-vendor callback to setup EFER caps
Quoted 18 lines · Sean Christopherson
Quoted 7 lines · Yosry Ahmed
Move handling EFER.SVME and EFER.LMSLE from hardware setup to a new optional per-vendor callback invoked from
kvm_setup_efer_caps(). This centralizes allowed EFER bits handling tokvm_setup_efer_caps(), facilitating following changes to moveefer_reserved_bitsintokvm_caps.Move the call to
kvm_setup_efer_caps()after per-vendor ops are initialized.Why?
Quoted 6 lines
arch/x86/kvm/x86.c +0
-0@@ -6903,6 +6903,8 @@ static void kvm_setup_efer_caps(void)
Quoted 6 lines
arch/x86/kvm/x86.c static void kvm_setup_efer_caps(void) · line 6907
Quoted 4 lines
arch/x86/kvm/x86.c +2
-0if (kvm_cpu_cap_has (X86_FEATURE_AUTOIBRS ))kvm_enable_efer_bits (EFER_AUTOIBRS ); + +kvm_x86_call (setup_efer_caps)();I would rather move the togging to
kvm_setup_efer_caps(), e.g.
I didn't do it this way because it creates a dependency on SVM setting the X86_FEATURE_SVM cap before kvm_setup_efer_caps() is called. For e.g. it would break if the call to kvm_setup_efer_caps() is moved before the vendor-specific hardware_setup(). Maybe that's fine, but it just seemed like the dependency can be easily avoided here by adding a new vendor-specific callback.
Sean Christopherson
maintainerRe: [PATCH v2 4/7] KVM: x86: Add a per-vendor callback to setup EFER caps
Quoted 26 lines · Yosry Ahmed
Quoted 18 lines · Sean Christopherson
Quoted 7 lines · Yosry Ahmed
Move handling EFER.SVME and EFER.LMSLE from hardware setup to a new optional per-vendor callback invoked from
kvm_setup_efer_caps(). This centralizes allowed EFER bits handling tokvm_setup_efer_caps(), facilitating following changes to moveefer_reserved_bitsintokvm_caps.Move the call to
kvm_setup_efer_caps()after per-vendor ops are initialized.Why?
Quoted 6 lines
arch/x86/kvm/x86.c +0
-0@@ -6903,6 +6903,8 @@ static void kvm_setup_efer_caps(void)Quoted 6 lines
arch/x86/kvm/x86.c static void kvm_setup_efer_caps(void) · line 6907
Quoted 4 lines
arch/x86/kvm/x86.c +2
-0if (kvm_cpu_cap_has (X86_FEATURE_AUTOIBRS ))kvm_enable_efer_bits (EFER_AUTOIBRS ); + +kvm_x86_call (setup_efer_caps)();I would rather move the togging to
kvm_setup_efer_caps(), e.g.
Quoted 2 lines
I didn't do it this way because it creates a dependency on SVM setting the
X86_FEATURE_SVMcap beforekvm_setup_efer_caps()is called.
For all intents and purposes, that dependency already exists due to the X86_FEATURE_{NX,FXSR_OPT,AUTOIBRS} checks. And thanks to kvm_is_configuring_cpu_caps, it's "impossible" for those caps to be toggled outside of svm_set_cpu_caps().
Quoted 2 lines
e.g. it would break if the call to
kvm_setup_efer_caps()is moved before the vendor-specifichardware_setup().
As above, that would break for other reasons.
Quoted 2 lines
Maybe that's fine, but it just seemed like the dependency can be easily avoided here by adding a new vendor-specific callback.
No, all it does is change what can go wrong. E.g. if KVM cleared "nested" insvm_hardware_setup(), which is *very* realistic given that we carry an internal patch to disable nested if TDP is disabled, then hoisting kvm_setup_efer_caps() above ops->hardware_setup() would still break (even ignoring the above issues).
Re: [PATCH v2 4/7] KVM: x86: Add a per-vendor callback to setup EFER caps
Quoted 28 lines · Sean Christopherson
Quoted 26 lines · Yosry Ahmed
Quoted 24 lines · Sean Christopherson
Quoted 7 lines · Yosry Ahmed
Move handling EFER.SVME and EFER.LMSLE from hardware setup to a new optional per-vendor callback invoked from
kvm_setup_efer_caps(). This centralizes allowed EFER bits handling tokvm_setup_efer_caps(), facilitating following changes to moveefer_reserved_bitsintokvm_caps.Move the call to
kvm_setup_efer_caps()after per-vendor ops are initialized.Why?
arch/x86/kvm/x86.c static void kvm_setup_efer_caps(void) · line 6907
Quoted 10 lines
arch/x86/kvm/x86.c +2
-0@@ -6903,6 +6903,8 @@ static void kvm_setup_efer_caps(void)if (kvm_cpu_cap_has (X86_FEATURE_AUTOIBRS ))kvm_enable_efer_bits (EFER_AUTOIBRS ); + +kvm_x86_call (setup_efer_caps)();I would rather move the togging to
kvm_setup_efer_caps(), e.g.
Quoted 6 lines
Quoted 2 lines
I didn't do it this way because it creates a dependency on SVM setting the
X86_FEATURE_SVMcap beforekvm_setup_efer_caps()is called.For all intents and purposes, that dependency already exists due to the X86_FEATURE_{NX,FXSR_OPT,AUTOIBRS} checks. And thanks to
kvm_is_configuring_cpu_caps, it's "impossible" for those caps to be toggled outside ofsvm_set_cpu_caps().
Right, I missed this. kvm_is_configuring_cpu_caps is neat. I wonder if we can make that an enum with values {UNINIT, CONFIGURING, INITIALIZED}, then we can be more paranoid and WARN if the the state isn't INITIALIZED in kvm_setup_efer_caps(). That might be too paranoid though.
Quoted 6 lines
Quoted 3 lines
e.g. it would break if the call to
kvm_setup_efer_caps()is moved before the vendor-specifichardware_setup().As above, that would break for other reasons.
Quoted 7 lines
Quoted 2 lines
Maybe that's fine, but it just seemed like the dependency can be easily avoided here by adding a new vendor-specific callback.
No, all it does is change what can go wrong. E.g. if KVM cleared "nested" in
svm_hardware_setup(), which is *very* realistic given that we carry an internal patch to disable nested if TDP is disabled, then hoistingkvm_setup_efer_caps()aboveops->hardware_setup()would still break (even ignoring the above issues).
Yeah, thanks for explaining that. I will drop the per-vendor callback and just do it directly in kvm_setup_efer_caps().
Sean Christopherson
maintainerRe: [PATCH v2 4/7] KVM: x86: Add a per-vendor callback to setup EFER caps
Quoted 36 lines · Yosry Ahmed
Quoted 28 lines · Sean Christopherson
Quoted 26 lines · Yosry Ahmed
Quoted 24 lines · Sean Christopherson
Quoted 7 lines · Yosry Ahmed
Move handling EFER.SVME and EFER.LMSLE from hardware setup to a new optional per-vendor callback invoked from
kvm_setup_efer_caps(). This centralizes allowed EFER bits handling tokvm_setup_efer_caps(), facilitating following changes to moveefer_reserved_bitsintokvm_caps.Move the call to
kvm_setup_efer_caps()after per-vendor ops are initialized.Why?
arch/x86/kvm/x86.c static void kvm_setup_efer_caps(void) · line 6907
Quoted 10 lines
arch/x86/kvm/x86.c +2
-0@@ -6903,6 +6903,8 @@ static void kvm_setup_efer_caps(void)if (kvm_cpu_cap_has (X86_FEATURE_AUTOIBRS ))kvm_enable_efer_bits (EFER_AUTOIBRS ); + +kvm_x86_call (setup_efer_caps)();I would rather move the togging to
kvm_setup_efer_caps(), e.g.Quoted 6 lines
Quoted 2 lines
I didn't do it this way because it creates a dependency on SVM setting the
X86_FEATURE_SVMcap beforekvm_setup_efer_caps()is called.For all intents and purposes, that dependency already exists due to the X86_FEATURE_{NX,FXSR_OPT,AUTOIBRS} checks. And thanks to
kvm_is_configuring_cpu_caps, it's "impossible" for those caps to be toggled outside ofsvm_set_cpu_caps().
Quoted 5 lines
Right, I missed this.
kvm_is_configuring_cpu_capsis neat. I wonder if we can make that an enum with values {UNINIT, CONFIGURING, INITIALIZED}, then we can be more paranoid and WARN if the the state isn't INITIALIZED inkvm_setup_efer_caps(). That might be too paranoid though.
I thought about trying to do something like that too, and mostly landed on "too paranoid". Or rather, landed on "mostly just shifts where the bugs will be". E.g. kvm_setup_efer_caps() needs to run after kvm_cpu_caps are configured, or more specificaly, before ops->hardware_setup(). But supported_{xcr0,xss} need to be initialized before ops->hardware_setup(), so that KVM can manipulate the support bits at the same time kvm_cpu_caps are updated. And so adding one-off WARNs still requires on having a priori knowledge of exactly where in the bootstrapping process the code needs to run, i.e. we still have to "manually" select the right stage of kvm_is_configuring_cpu_caps.
What _would_ scale a little better would be to have kvm_cpu_cap_get() WARN if kvm_is_configuring_cpu_caps isn't INITIALIZED, but I'm hesitant to do that because it'll pollute KVM with WARNs just to detect rare bugs that really should be caught during initial development anyways.
Re: [PATCH v2 4/7] KVM: x86: Add a per-vendor callback to setup EFER caps
Quoted 44 lines · Sean Christopherson
Quoted 36 lines · Yosry Ahmed
Quoted 28 lines · Sean Christopherson
Quoted 26 lines · Yosry Ahmed
Quoted 24 lines · Sean Christopherson
Quoted 7 lines · Yosry Ahmed
Move handling EFER.SVME and EFER.LMSLE from hardware setup to a new optional per-vendor callback invoked from
kvm_setup_efer_caps(). This centralizes allowed EFER bits handling tokvm_setup_efer_caps(), facilitating following changes to moveefer_reserved_bitsintokvm_caps.Move the call to
kvm_setup_efer_caps()after per-vendor ops are initialized.Why?
arch/x86/kvm/x86.c static void kvm_setup_efer_caps(void) · line 6907
Quoted 10 lines
arch/x86/kvm/x86.c +2
-0@@ -6903,6 +6903,8 @@ static void kvm_setup_efer_caps(void)if (kvm_cpu_cap_has (X86_FEATURE_AUTOIBRS ))kvm_enable_efer_bits (EFER_AUTOIBRS ); + +kvm_x86_call (setup_efer_caps)();I would rather move the togging to
kvm_setup_efer_caps(), e.g.Quoted 6 lines
Quoted 2 lines
I didn't do it this way because it creates a dependency on SVM setting the
X86_FEATURE_SVMcap beforekvm_setup_efer_caps()is called.For all intents and purposes, that dependency already exists due to the X86_FEATURE_{NX,FXSR_OPT,AUTOIBRS} checks. And thanks to
kvm_is_configuring_cpu_caps, it's "impossible" for those caps to be toggled outside ofsvm_set_cpu_caps().Quoted 5 lines
Right, I missed this.
kvm_is_configuring_cpu_capsis neat. I wonder if we can make that an enum with values {UNINIT, CONFIGURING, INITIALIZED}, then we can be more paranoid and WARN if the the state isn't INITIALIZED inkvm_setup_efer_caps(). That might be too paranoid though.
Quoted 9 lines
I thought about trying to do something like that too, and mostly landed on "too paranoid". Or rather, landed on "mostly just shifts where the bugs will be". E.g.
kvm_setup_efer_caps()needs to run afterkvm_cpu_capsare configured, or more specificaly, beforeops->hardware_setup(). But supported_{xcr0,xss} need to be initialized beforeops->hardware_setup(), so that KVM can manipulate the support bits at the same timekvm_cpu_capsare updated. And so adding one-off WARNs still requires on having a priori knowledge of exactly where in the bootstrapping process the code needs to run, i.e. we still have to "manually" select the right stage ofkvm_is_configuring_cpu_caps.
I think that's fine, but I get your point that the bugs this protects against will probably be caught otherwise anyway.
Quoted 4 lines
What _would_ scale a little better would be to have
kvm_cpu_cap_get()WARN ifkvm_is_configuring_cpu_capsisn't INITIALIZED, but I'm hesitant to do that because it'll pollute KVM with WARNs just to detect rare bugs that really should be caught during initial development anyways.
Yeah, I will resist the need to be paranoid and drop this :)
Re: [PATCH v2 5/7] KVM: x86: Reverse the polarity of efer_reserved_bits
In reply to Yosry AhmedQuoted 7 lines · Yosry Ahmed
In preparation for moving
efer_reserved_bitsintokvm_caps, reverse its polarity and make itefer_supported_bits, to be more consistent with other fields inkvm_caps.No functional change intended.
Signed-off-by Yosry Ahmed <[email protected]>
Re: [PATCH v2 6/7] KVM: x86: Move supported EFER bits to kvm_caps
In reply to Yosry AhmedQuoted 9 lines · Yosry Ahmed
Supported EFER bits naturally fits into
kvm_capsbecause it needs to be recomputed during vendor init (e.g. to account for EFER.SVME being allowed/disallowed based on nested being enabled/disabled).Move
efer_supported_bitsintokvm_capsassupported_efer_bits(for naming consistency), and reinitialize it at the beginning ofkvm_setup_efer_caps(), removing the need to clear unsupported bits in vendor code (e.g. EFER.SVME).
Quoted 8 lines
As the bitmask is now globally visible as part of
kvm_caps, there's little use for helpers to enable/disable specific bits, so drop them and open-code updates to kvm_caps.supported_efer_bits.No functional change intended.
Suggested-by Sean Christopherson <[email protected]>Signed-off-by Yosry Ahmed <[email protected]>
<snip>