KVM: x86: Fix supported VM_TYPES caps
Sean Christopherson
maintainercover KVM: x86: Fix supported VM_TYPES caps
Fix a goof where KVM fails to re-initialize the set of supported VM types, resulting in KVM overreporting the set of supported types when a vendor module is reloaded with incompatible settings. E.g. unload kvm-intel.ko, reload with ept=0, and KVM will incorrectly treat SW_PROTECTED_VM as supported.
Fix a similar long-standing bug with supported_mce_cap that is much less benign, and then harden against us making the same mistake in the future.
Sean Christopherson (3):
KVM: x86: Fully re-initialize supported_vm_types on vendor module load
KVM: x86: Fully re-initialize supported_mce_cap on vendor module load
KVM: x86: Explicitly zero kvm_caps during vendor module load
arch/x86/kvm/x86.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
base-commit: a96cb3bf390eebfead5fc7a2092f8452a7997d1b
Signature · 1 line
2.44.0.769.g3c40516874-goog
Sean Christopherson
maintainerPATCH 1/3 KVM: x86: Fully re-initialize supported_vm_types on vendor module load
Recompute the entire set of supported VM types when a vendor module is loaded, as preserving supported_vm_types across vendor module unload and reload can result in VM types being incorrectly treated as supported.
E.g. if a vendor module is loaded with TDP enabled, unloaded, and then reloaded with TDP disabled, KVM_X86_SW_PROTECTED_VM will be incorrectly retained. Ditto for SEV_VM and SEV_ES_VM and their respective module params in kvm-amd.ko.
--- arch/x86/kvm/x86.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/x86.c +2 -1
@@ -94,7 +94,6 @@struct kvm_caps kvm_caps__read_mostly = { .supported_mce_cap =MCG_CTL_P |MCG_SER_P , - .supported_vm_types =BIT (KVM_X86_DEFAULT_VM ), };EXPORT_SYMBOL_GPL (kvm_caps);
@@ -9776,6 +9775,8 @@ int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops)if (r) goto out_free_percpu; + kvm_caps.supported_vm_types =BIT (KVM_X86_DEFAULT_VM ); +if (boot_cpu_has (X86_FEATURE_XSAVE )) { host_xcr0 =xgetbv (XCR_XFEATURE_ENABLED_MASK ); kvm_caps.supported_xcr0 = host_xcr0 &KVM_SUPPORTED_XCR0 ;
Signature · 1 line
2.44.0.769.g3c40516874-goog
Sean Christopherson
maintainerPATCH 2/3 KVM: x86: Fully re-initialize supported_mce_cap on vendor module load
In reply to Sean ChristophersonEffectively reset supported_mce_cap on vendor module load to ensure that capabilities aren't unintentionally preserved across module reload, e.g. if kvm-intel.ko added a module param to control LMCE support, or if someone somehow managed to load a vendor module that doesn't support LMCE after loading and unloading kvm-intel.ko.
Practically speaking, this bug is a non-issue as kvm-intel.ko doesn't have a module param for LMCE, and there is no system in the world that supports both kvm-intel.ko and kvm-amd.ko.
--- arch/x86/kvm/x86.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +2 -3
@@ -92,9 +92,7 @@#define MAX_IO_MSRS 256#define KVM_MAX_MCE_BANKS 32 -struct kvm_caps kvm_caps__read_mostly = { - .supported_mce_cap =MCG_CTL_P |MCG_SER_P , -}; +struct kvm_caps kvm_caps__read_mostly ;EXPORT_SYMBOL_GPL (kvm_caps);#define ERR_PTR_USR (e) ((void __user *)ERR_PTR(e))
@@ -9776,6 +9774,7 @@ int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops) goto out_free_percpu; kvm_caps.supported_vm_types =BIT (KVM_X86_DEFAULT_VM ); + kvm_caps.supported_mce_cap =MCG_CTL_P |MCG_SER_P ;if (boot_cpu_has (X86_FEATURE_XSAVE )) { host_xcr0 =xgetbv (XCR_XFEATURE_ENABLED_MASK );
Signature · 1 line
2.44.0.769.g3c40516874-goog
Sean Christopherson
maintainerPATCH 3/3 KVM: x86: Explicitly zero kvm_caps during vendor module load
In reply to Sean ChristophersonZero out all of kvm_caps when loading a new vendor module to ensure that KVM can't inadvertently rely on global initialization of a field, and add a comment above the definition of kvm_caps to call out that all fields needs to be explicitly computed during vendor module load.
--- arch/x86/kvm/x86.c | 7 +++++++ 1 file changed, 7 insertions(+)
arch/x86/kvm/x86.c +7 -0
@@ -92,6 +92,11 @@#define MAX_IO_MSRS 256#define KVM_MAX_MCE_BANKS 32 +/* +* Note, kvm_caps fields should *never* have default values, all fields must be +* recomputed from scratch during vendor module load, e.g. to account for a +* vendor module being reloaded with different module parameters. +*/ struct kvm_caps kvm_caps__read_mostly ;EXPORT_SYMBOL_GPL (kvm_caps);
@@ -9755,6 +9760,8 @@ int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops)return -EIO ; } +memset (&kvm_caps,0 ,sizeof (kvm_caps)); + x86_emulator_cache =kvm_alloc_emulator_cache ();if (!x86_emulator_cache) {pr_err ("failed to allocate cache for x86 emulator\n" );
Signature · 1 line
2.44.0.769.g3c40516874-goog
Re: [PATCH 3/3] KVM: x86: Explicitly zero kvm_caps during vendor module load
Quoted 27 lines · Sean Christopherson
Zero out all of
kvm_capswhen loading a new vendor module to ensure that KVM can't inadvertently rely on global initialization of a field, and add a comment above the definition ofkvm_capsto call out that all fields needs to be explicitly computed during vendor module load.Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/x86.c | 7 +++++++ 1 file changed, 7 insertions(+)arch/x86/kvm/x86.c +5
-0@@ -92,6 +92,11 @@#define MAX_IO_MSRS 256#define KVM_MAX_MCE_BANKS 32 +/* +* Note, kvm_caps fields should *never* have default values, all fields must be +* recomputed from scratch during vendor module load, e.g. to account for a +* vendor module being reloaded with different module parameters. +*/ struct kvm_caps kvm_caps__read_mostly ;EXPORT_SYMBOL_GPL (kvm_caps);
arch/x86/kvm/x86.c int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops) · line 9767
Quoted 8 lines
arch/x86/kvm/x86.c +2
-0@@ -9755,6 +9760,8 @@ int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops)return -EIO ; } +memset (&kvm_caps,0 ,sizeof (kvm_caps)); + x86_emulator_cache =kvm_alloc_emulator_cache ();if (!x86_emulator_cache) {pr_err ("failed to allocate cache for x86 emulator\n" );
Why do the memset() here particularly?
Isn't it better to put ...
memset(&kvm_caps, 0, sizeof(kvm_caps)); kvm_caps.supported_vm_types = BIT(KVM_X86_DEFAULT_VM); kvm_caps.supported_mce_cap = MCG_CTL_P | MCG_SER_P;
... together so it can be easily seen?
We can even have a helper to do above to "reset kvm_caps to default values" I think.
Sean Christopherson
maintainerRe: [PATCH 3/3] KVM: x86: Explicitly zero kvm_caps during vendor module load
Quoted 31 lines · Kai Huang
Quoted 30 lines · Sean Christopherson
Zero out all of
kvm_capswhen loading a new vendor module to ensure that KVM can't inadvertently rely on global initialization of a field, and add a comment above the definition ofkvm_capsto call out that all fields needs to be explicitly computed during vendor module load.Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/x86.c | 7 +++++++ 1 file changed, 7 insertions(+)arch/x86/kvm/x86.c +5
-0@@ -92,6 +92,11 @@#define MAX_IO_MSRS 256#define KVM_MAX_MCE_BANKS 32 +/* +* Note, kvm_caps fields should *never* have default values, all fields must be +* recomputed from scratch during vendor module load, e.g. to account for a +* vendor module being reloaded with different module parameters. +*/ struct kvm_caps kvm_caps__read_mostly ;EXPORT_SYMBOL_GPL (kvm_caps);@@ -9755,6 +9760,8 @@ int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops)return -EIO ; }
Quoted 7 lines
arch/x86/kvm/x86.c int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops) · line 9767
Quoted 5 lines
arch/x86/kvm/x86.c +2
-0+memset (&kvm_caps,0 ,sizeof (kvm_caps)); + x86_emulator_cache =kvm_alloc_emulator_cache ();if (!x86_emulator_cache) {pr_err ("failed to allocate cache for x86 emulator\n" );Why do the
memset()here particularly?
So that it happens as early as possible, e.g. in case kvm_mmu_vendor_module_init() or some other function comes along and modifies kvm_caps.
Quoted 10 lines
Isn't it better to put ...
memset(&kvm_caps, 0, sizeof(kvm_caps)); kvm_caps.supported_vm_types = BIT(KVM_X86_DEFAULT_VM); kvm_caps.supported_mce_cap = MCG_CTL_P | MCG_SER_P;... together so it can be easily seen?
We can even have a helper to do above to "reset
kvm_capsto default values" I think.
Hmm, I don't think a helper is necessary, but I do agree that having all of the explicit initialization in one place would be better. The alternative would be to use |= for BIT(KVM_X86_DEFAULT_VM), and MCG_CTL_P | MCG_SER_P, but I don't think that would be an improvement. I'll tweak the first two patches to set the hardcoded caps earlier.
The main reason I don't want to add a helper is that coming up with a name would be tricky. E.g. "kvm_reset_caps()" isn't a great fit because the caps are "reset" throughout module loading. "kvm_set_default_caps()" kinda fits, but they aren't so much that they are KVM's defaults, rather they are the caps that KVM can always support regardless of hardware support, e.g. supported_xcr0 isn't optional, it just depends on hardware.
Re: [PATCH 3/3] KVM: x86: Explicitly zero kvm_caps during vendor module load
Quoted 60 lines · Sean Christopherson
Quoted 31 lines · Kai Huang
Quoted 30 lines · Sean Christopherson
Zero out all of
kvm_capswhen loading a new vendor module to ensure that KVM can't inadvertently rely on global initialization of a field, and add a comment above the definition ofkvm_capsto call out that all fields needs to be explicitly computed during vendor module load.Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/x86.c | 7 +++++++ 1 file changed, 7 insertions(+)arch/x86/kvm/x86.c +5
-0@@ -92,6 +92,11 @@#define MAX_IO_MSRS 256#define KVM_MAX_MCE_BANKS 32 +/* +* Note, kvm_caps fields should *never* have default values, all fields must be +* recomputed from scratch during vendor module load, e.g. to account for a +* vendor module being reloaded with different module parameters. +*/ struct kvm_caps kvm_caps__read_mostly ;EXPORT_SYMBOL_GPL (kvm_caps);@@ -9755,6 +9760,8 @@ int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops)return -EIO ; }Quoted 7 lines
arch/x86/kvm/x86.c int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops) · line 9767
Quoted 5 lines
arch/x86/kvm/x86.c +2
-0+memset (&kvm_caps,0 ,sizeof (kvm_caps)); + x86_emulator_cache =kvm_alloc_emulator_cache ();if (!x86_emulator_cache) {pr_err ("failed to allocate cache for x86 emulator\n" );Why do the
memset()here particularly?So that it happens as early as possible, e.g. in case
kvm_mmu_vendor_module_init()or some other function comes along and modifieskvm_caps.Quoted 10 lines
Isn't it better to put ...
memset(&kvm_caps, 0, sizeof(kvm_caps)); kvm_caps.supported_vm_types = BIT(KVM_X86_DEFAULT_VM); kvm_caps.supported_mce_cap = MCG_CTL_P | MCG_SER_P;... together so it can be easily seen?
We can even have a helper to do above to "reset
kvm_capsto default values" I think.Hmm, I don't think a helper is necessary, but I do agree that having all of the explicit initialization in one place would be better. The alternative would be to use |= for
BIT(KVM_X86_DEFAULT_VM), andMCG_CTL_P|MCG_SER_P, but I don't think that would be an improvement. I'll tweak the first two patches to set the hardcoded caps earlier.
Quoted 6 lines
The main reason I don't want to add a helper is that coming up with a name would be tricky. E.g. "
kvm_reset_caps()" isn't a great fit because the caps are "reset" throughout module loading. "kvm_set_default_caps()" kinda fits, but they aren't so much that they are KVM's defaults, rather they are the caps that KVM can always support regardless of hardware support, e.g.supported_xcr0isn't optional, it just depends on hardware.
Personally I am fine with kvm_set_default_caps(), but obviously no strong opinion here. :-)
Quoted 5 lines · Sean Christopherson
Fix a goof where KVM fails to re-initialize the set of supported VM types, resulting in KVM overreporting the set of supported types when a vendor module is reloaded with incompatible settings. E.g. unload kvm-intel.ko, reload with ept=0, and KVM will incorrectly treat
SW_PROTECTED_VMas supported.
Hah, this reminds me of the bug of msrs_to_save[] and etc.
7a5ee6edb42e ("KVM: X86: Fix initialization of MSR lists")
The series looks good to me.
With v2 to move the reset of kvm_cap and set the
hardcoded caps earlier,
Quoted 13 lines
Fix a similar long-standing bug with
supported_mce_capthat is much less benign, and then harden against us making the same mistake in the future.Sean Christopherson (3):
KVM: x86: Fully re-initializesupported_vm_typeson vendor module load
KVM: x86: Fully re-initializesupported_mce_capon vendor module load
KVM: x86: Explicitly zerokvm_capsduring vendor module loadarch/x86/kvm/x86.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)base-commit:
a96cb3bf390eebfead5fc7a2092f8452a7997d1b
Sean Christopherson
maintainerQuoted 10 lines · Xiaoyao Li
Quoted 5 lines · Sean Christopherson
Fix a goof where KVM fails to re-initialize the set of supported VM types, resulting in KVM overreporting the set of supported types when a vendor module is reloaded with incompatible settings. E.g. unload kvm-intel.ko, reload with ept=0, and KVM will incorrectly treat
SW_PROTECTED_VMas supported.Hah, this reminds me of the bug of msrs_to_save[] and etc.
7a5ee6edb42e("KVM: X86: Fix initialization of MSR lists")
Yeah, and we had the same bug with allow_smaller_maxphyaddr
88213da23514 ("kvm: x86: disable the narrow guest module parameter on unload")
If the side effects of linking kvm.ko into kvm-{amd,intel}.ko weren't so painful for userspace, I would more seriously consider pursuing that in advance of multi-KVM[*]. Because having KVM be fully self-contained has some *really* nice properties, e.g. eliminates this entire class of bugs, eliminates a huge pile of exports, etc.
: > Since the symbols in the new module are invisible outside, I recommend:
: > new kvm_intel.ko = kvm_intel.ko + kvm.ko
: > new kvm_amd.ko = kvm_amd.ko + kvm.ko
:
: Yeah, Paolo also suggested this at LPC.
Quoted 10 lines · Sean Christopherson
Quoted 9 lines · Xiaoyao Li
Quoted 5 lines · Sean Christopherson
Fix a goof where KVM fails to re-initialize the set of supported VM types, resulting in KVM overreporting the set of supported types when a vendor module is reloaded with incompatible settings. E.g. unload kvm-intel.ko, reload with ept=0, and KVM will incorrectly treat
SW_PROTECTED_VMas supported.Hah, this reminds me of the bug of msrs_to_save[] and etc.
Quoted 9 lines
Quoted 1 line
7a5ee6edb42e("KVM: X86: Fix initialization of MSR lists")Yeah, and we had the same bug with
allow_smaller_maxphyaddr
88213da23514("kvm: x86: disable the narrow guest module parameter on unload")If the side effects of linking kvm.ko into kvm-{amd,intel}.ko weren't so painful for userspace,
Do we have any real side effects for _userspace_ here?
Quoted 5 lines
I would more seriously consider pursuing that in advance of multi-KVM[*]. Because having KVM be fully self-contained has some *really* nice properties, e.g. eliminates this entire class of bugs, eliminates a huge pile of exports, etc.
Quoted 8 lines
: > Since the symbols in the new module are invisible outside, I recommend:
: > new kvm_intel.ko = kvm_intel.ko + kvm.ko
: > new kvm_amd.ko = kvm_amd.ko + kvm.ko
:
: Yeah, Paolo also suggested this at LPC.
+1. This makes life a lot easier.
Sean Christopherson
maintainerQuoted 15 lines · Kai Huang
Quoted 14 lines · Sean Christopherson
Quoted 10 lines · Xiaoyao Li
Quoted 5 lines · Sean Christopherson
Fix a goof where KVM fails to re-initialize the set of supported VM types, resulting in KVM overreporting the set of supported types when a vendor module is reloaded with incompatible settings. E.g. unload kvm-intel.ko, reload with ept=0, and KVM will incorrectly treat
SW_PROTECTED_VMas supported.Hah, this reminds me of the bug of msrs_to_save[] and etc.
7a5ee6edb42e("KVM: X86: Fix initialization of MSR lists")Yeah, and we had the same bug with
allow_smaller_maxphyaddr
Quoted 7 lines
Quoted 5 lines
88213da23514("kvm: x86: disable the narrow guest module parameter on unload")If the side effects of linking kvm.ko into kvm-{amd,intel}.ko weren't so painful for userspace,
Do we have any real side effects for _userspace_ here?
kvm.ko ceasing to exist, and "everything" being tied to the vendor module is the big problem. E.g. params from the kernel command line for kvm.??? will become ineffective, etc. Some of that can be handled in the kernel, e.g. KVM can create a sysfs symlink so that the accesses through sysfs continue to work, but AFAIK params don't supporting such aliasing/links.
I don't think there are any deal breakers, but I don't expect it to Just Work either.
Quoted 24 lines · Sean Christopherson
Quoted 15 lines · Kai Huang
Quoted 14 lines · Sean Christopherson
Quoted 10 lines · Xiaoyao Li
Quoted 5 lines · Sean Christopherson
Fix a goof where KVM fails to re-initialize the set of supported VM types, resulting in KVM overreporting the set of supported types when a vendor module is reloaded with incompatible settings. E.g. unload kvm-intel.ko, reload with ept=0, and KVM will incorrectly treat
SW_PROTECTED_VMas supported.Hah, this reminds me of the bug of msrs_to_save[] and etc.
7a5ee6edb42e("KVM: X86: Fix initialization of MSR lists")Yeah, and we had the same bug with
allow_smaller_maxphyaddrQuoted 7 lines
Quoted 5 lines
88213da23514("kvm: x86: disable the narrow guest module parameter on unload")If the side effects of linking kvm.ko into kvm-{amd,intel}.ko weren't so painful for userspace,
Do we have any real side effects for _userspace_ here?
Quoted 7 lines
kvm.ko ceasing to exist, and "everything" being tied to the vendor module is the big problem. E.g. params from the kernel command line for kvm.??? will become ineffective, etc. Some of that can be handled in the kernel, e.g. KVM can create a sysfs symlink so that the accesses through sysfs continue to work, but AFAIK params don't supporting such aliasing/links.
I don't think there are any deal breakers, but I don't expect it to Just Work either.
Perhaps we can make the kvm.ko as a dummy module which only keeps the module parameters for backward compatibility?
Sean Christopherson
maintainerQuoted 31 lines · Kai Huang
Quoted 30 lines · Sean Christopherson
Quoted 15 lines · Kai Huang
Quoted 14 lines · Sean Christopherson
Quoted 10 lines · Xiaoyao Li
Quoted 5 lines · Sean Christopherson
Fix a goof where KVM fails to re-initialize the set of supported VM types, resulting in KVM overreporting the set of supported types when a vendor module is reloaded with incompatible settings. E.g. unload kvm-intel.ko, reload with ept=0, and KVM will incorrectly treat
SW_PROTECTED_VMas supported.Hah, this reminds me of the bug of msrs_to_save[] and etc.
7a5ee6edb42e("KVM: X86: Fix initialization of MSR lists")Yeah, and we had the same bug with
allow_smaller_maxphyaddrQuoted 7 lines
Quoted 5 lines
88213da23514("kvm: x86: disable the narrow guest module parameter on unload")If the side effects of linking kvm.ko into kvm-{amd,intel}.ko weren't so painful for userspace,
Do we have any real side effects for _userspace_ here?
kvm.ko ceasing to exist, and "everything" being tied to the vendor module is the big problem. E.g. params from the kernel command line for kvm.??? will become ineffective, etc. Some of that can be handled in the kernel, e.g. KVM can create a sysfs symlink so that the accesses through sysfs continue to work, but AFAIK params don't supporting such aliasing/links.
Quoted 4 lines
Quoted 1 line
I don't think there are any deal breakers, but I don't expect it to Just Work either.
Perhaps we can make the kvm.ko as a dummy module which only keeps the module parameters for backward compatibility?
Keeping parameters in a dummy kvm.ko would largely defeat the purpose of linking everything into vendor modules, i.e. would make it possible for the parameters to hold a stale value.
Paolo Bonzini
maintainerQuoted 6 lines · Sean Christopherson
Quoted 2 lines
Perhaps we can make the kvm.ko as a dummy module which only keeps the module parameters for backward compatibility?
Keeping parameters in a dummy kvm.ko would largely defeat the purpose of linking everything into vendor modules, i.e. would make it possible for the parameters to hold a stale value.
We have the following read-write params:
parm: nx_huge_pages:bool parm: nx_huge_pages_recovery_ratio:uint parm: nx_huge_pages_recovery_period_ms:uint parm: flush_on_reuse:bool parm: ignore_msrs:bool parm: report_ignored_msrs:bool parm: min_timer_period_us:uint parm: tsc_tolerance_ppm:uint parm: lapic_timer_advance_ns:int parm: force_emulation_prefix:int parm: pi_inject_timer:bint parm: eager_page_split:bool parm: halt_poll_ns:uint parm: halt_poll_ns_grow:uint parm: halt_poll_ns_grow_start:uint parm: halt_poll_ns_shrink:uint
Vendor modules do not muck with them (the only one that is exported is report_ignored_msrs for which permanency is obviously harmless).
And the following read-only params:
parm: tdp_mmu:bool parm: mmio_caching:bool parm: kvmclock_periodic_sync:bool parm: vector_hashing:bool parm: enable_vmware_backdoor:bool parm: enable_pmu:bool parm: mitigate_smt_rsb:bool
The only really bad one is tdp_mmu, which can change depending on the ept/npt parameters of kvm-intel/kvm-amd; everything else is okay to have in a common module.
mitigate_smt_rsb could (should?) move to kvm-amd.ko if the modules were unified with kvm.ko as a dummy one.
Paolo
Queued, thanks.
Paolo
Quoted 7 lines · Sean Christopherson
Fix a goof where KVM fails to re-initialize the set of supported VM types, resulting in KVM overreporting the set of supported types when a vendor module is reloaded with incompatible settings. E.g. unload kvm-intel.ko, reload with ept=0, and KVM will incorrectly treat
SW_PROTECTED_VMas supported.
Quoted 7 lines
Fix a similar long-standing bug with
supported_mce_capthat is much less benign, and then harden against us making the same mistake in the future.Sean Christopherson (3):
KVM: x86: Fully re-initializesupported_vm_typeson vendor module load
KVM: x86: Fully re-initializesupported_mce_capon vendor module load
KVM: x86: Explicitly zerokvm_capsduring vendor module load
Applied, thanks.
Paolo