Skip to content

Commit

Permalink
KVM: VAC: Move user_return_msrs allocation into the vac
Browse files Browse the repository at this point in the history
Allocating in KVM meant module load would overwrite
the table; and unloading one kvm would free the table from
other running ones (oops).

Move alloc/dealloc to vac and make the table private.

Signed-off-by: Venkatesh Srinivas <[email protected]>
  • Loading branch information
vsrinivas committed Jun 28, 2023
1 parent 333fbf0 commit 09087b2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
14 changes: 13 additions & 1 deletion arch/x86/kvm/vac.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
u32 __read_mostly kvm_uret_msrs_list[KVM_MAX_NR_USER_RETURN_MSRS];
EXPORT_SYMBOL(kvm_uret_msrs_list);
struct kvm_user_return_msrs __percpu *user_return_msrs;
EXPORT_SYMBOL(user_return_msrs);

u32 __read_mostly kvm_nr_uret_msrs;
EXPORT_SYMBOL(kvm_nr_uret_msrs);
Expand Down Expand Up @@ -160,6 +159,13 @@ void kvm_arch_hardware_disable(void)

int __init vac_init(void)
{
user_return_msrs = alloc_percpu(struct kvm_user_return_msrs);
if (!user_return_msrs) {
pr_err("failed to allocate percpu kvm_user_return_msrs\n");
return -ENOMEM;
}
kvm_nr_uret_msrs = 0;

#ifdef CONFIG_KVM_INTEL
if (cpu_has_vmx())
return vac_vmx_init();
Expand All @@ -172,6 +178,12 @@ int __init vac_init(void)
}
module_init(vac_init);

void __exit vac_exit(void)
{
free_percpu(user_return_msrs);
}
module_exit(vac_exit);

/*
* Handle a fault on a hardware virtualization (VMX or SVM) instruction.
*
Expand Down
11 changes: 0 additions & 11 deletions arch/x86/kvm/x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -9244,14 +9244,6 @@ static int __kvm_x86_vendor_init(struct kvm_x86_init_ops *ops)
return -ENOMEM;
}

user_return_msrs = alloc_percpu(struct kvm_user_return_msrs);
if (!user_return_msrs) {
pr_err("failed to allocate percpu kvm_user_return_msrs\n");
r = -ENOMEM;
goto out_free_x86_emulator_cache;
}
kvm_nr_uret_msrs = 0;

r = kvm_mmu_vendor_module_init();
if (r)
goto out_free_percpu;
Expand Down Expand Up @@ -9325,8 +9317,6 @@ static int __kvm_x86_vendor_init(struct kvm_x86_init_ops *ops)
out_mmu_exit:
kvm_mmu_vendor_module_exit();
out_free_percpu:
free_percpu(user_return_msrs);
out_free_x86_emulator_cache:
kmem_cache_destroy(x86_emulator_cache);
return r;
}
Expand Down Expand Up @@ -9364,7 +9354,6 @@ void kvm_x86_vendor_exit(void)
#endif
static_call(kvm_x86_hardware_unsetup)();
kvm_mmu_vendor_module_exit();
free_percpu(user_return_msrs);
kmem_cache_destroy(x86_emulator_cache);
#ifdef CONFIG_KVM_XEN
static_key_deferred_flush(&kvm_xen_enabled);
Expand Down

0 comments on commit 09087b2

Please sign in to comment.