Skip to content

Commit 7f8e859

Browse files
sean-jcSasha Levin
authored andcommitted
KVM: x86/mmu: Resolve nx_huge_pages when kvm.ko is loaded
commit 1d0e848 upstream. Resolve nx_huge_pages to true/false when kvm.ko is loaded, leaving it as -1 is technically undefined behavior when its value is read out by param_get_bool(), as boolean values are supposed to be '0' or '1'. Alternatively, KVM could define a custom getter for the param, but the auto value doesn't depend on the vendor module in any way, and printing "auto" would be unnecessarily unfriendly to the user. In addition to fixing the undefined behavior, resolving the auto value also fixes the scenario where the auto value resolves to N and no vendor module is loaded. Previously, -1 would result in Y being printed even though KVM would ultimately disable the mitigation. Rename the existing MMU module init/exit helpers to clarify that they're invoked with respect to the vendor module, and add comments to document why KVM has two separate "module init" flows. ========================================================================= UBSAN: invalid-load in kernel/params.c:320:33 load of value 255 is not a valid value for type '_Bool' CPU: 6 PID: 892 Comm: tail Not tainted 5.17.0-rc3+ torvalds#799 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015 Call Trace: <TASK> dump_stack_lvl+0x34/0x44 ubsan_epilogue+0x5/0x40 __ubsan_handle_load_invalid_value.cold+0x43/0x48 param_get_bool.cold+0xf/0x14 param_attr_show+0x55/0x80 module_attr_show+0x1c/0x30 sysfs_kf_seq_show+0x93/0xc0 seq_read_iter+0x11c/0x450 new_sync_read+0x11b/0x1a0 vfs_read+0xf0/0x190 ksys_read+0x5f/0xe0 do_syscall_64+0x3b/0xc0 entry_SYSCALL_64_after_hwframe+0x44/0xae </TASK> ========================================================================= Fixes: b8e8c83 ("kvm: mmu: ITLB_MULTIHIT mitigation") Cc: [email protected] Reported-by: Bruno Goncalves <[email protected]> Reported-by: Jan Stancek <[email protected]> Signed-off-by: Sean Christopherson <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent cdc2466 commit 7f8e859

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,8 +1559,9 @@ static inline int kvm_arch_flush_remote_tlb(struct kvm *kvm)
15591559
return -ENOTSUPP;
15601560
}
15611561

1562-
int kvm_mmu_module_init(void);
1563-
void kvm_mmu_module_exit(void);
1562+
void kvm_mmu_x86_module_init(void);
1563+
int kvm_mmu_vendor_module_init(void);
1564+
void kvm_mmu_vendor_module_exit(void);
15641565

15651566
void kvm_mmu_destroy(struct kvm_vcpu *vcpu);
15661567
int kvm_mmu_create(struct kvm_vcpu *vcpu);

arch/x86/kvm/mmu/mmu.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6105,12 +6105,24 @@ static int set_nx_huge_pages(const char *val, const struct kernel_param *kp)
61056105
return 0;
61066106
}
61076107

6108-
int kvm_mmu_module_init(void)
6108+
/*
6109+
* nx_huge_pages needs to be resolved to true/false when kvm.ko is loaded, as
6110+
* its default value of -1 is technically undefined behavior for a boolean.
6111+
*/
6112+
void kvm_mmu_x86_module_init(void)
61096113
{
6110-
int ret = -ENOMEM;
6111-
61126114
if (nx_huge_pages == -1)
61136115
__set_nx_huge_pages(get_nx_auto_mode());
6116+
}
6117+
6118+
/*
6119+
* The bulk of the MMU initialization is deferred until the vendor module is
6120+
* loaded as many of the masks/values may be modified by VMX or SVM, i.e. need
6121+
* to be reset when a potentially different vendor module is loaded.
6122+
*/
6123+
int kvm_mmu_vendor_module_init(void)
6124+
{
6125+
int ret = -ENOMEM;
61146126

61156127
/*
61166128
* MMU roles use union aliasing which is, generally speaking, an
@@ -6182,7 +6194,7 @@ void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
61826194
mmu_free_memory_caches(vcpu);
61836195
}
61846196

6185-
void kvm_mmu_module_exit(void)
6197+
void kvm_mmu_vendor_module_exit(void)
61866198
{
61876199
mmu_destroy_caches();
61886200
percpu_counter_destroy(&kvm_total_used_mmu_pages);

arch/x86/kvm/x86.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8562,7 +8562,7 @@ int kvm_arch_init(void *opaque)
85628562
}
85638563
kvm_nr_uret_msrs = 0;
85648564

8565-
r = kvm_mmu_module_init();
8565+
r = kvm_mmu_vendor_module_init();
85668566
if (r)
85678567
goto out_free_percpu;
85688568

@@ -8612,7 +8612,7 @@ void kvm_arch_exit(void)
86128612
cancel_work_sync(&pvclock_gtod_work);
86138613
#endif
86148614
kvm_x86_ops.hardware_enable = NULL;
8615-
kvm_mmu_module_exit();
8615+
kvm_mmu_vendor_module_exit();
86168616
free_percpu(user_return_msrs);
86178617
kmem_cache_destroy(x86_emulator_cache);
86188618
kmem_cache_destroy(x86_fpu_cache);
@@ -12618,3 +12618,19 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_enter);
1261812618
EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_exit);
1261912619
EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_msr_protocol_enter);
1262012620
EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_msr_protocol_exit);
12621+
12622+
static int __init kvm_x86_init(void)
12623+
{
12624+
kvm_mmu_x86_module_init();
12625+
return 0;
12626+
}
12627+
module_init(kvm_x86_init);
12628+
12629+
static void __exit kvm_x86_exit(void)
12630+
{
12631+
/*
12632+
* If module_init() is implemented, module_exit() must also be
12633+
* implemented to allow module unload.
12634+
*/
12635+
}
12636+
module_exit(kvm_x86_exit);

0 commit comments

Comments
 (0)