Skip to content

Commit b67107a

Browse files
sean-jcbonzini
authored andcommitted
KVM: Add arch hooks for enabling/disabling virtualization
Add arch hooks that are invoked when KVM enables/disable virtualization. x86 will use the hooks to register an "emergency disable" callback, which is essentially an x86-specific shutdown notifier that is used when the kernel is doing an emergency reboot/shutdown/kexec. Add comments for the declarations to help arch code understand exactly when the callbacks are invoked. Alternatively, the APIs themselves could communicate most of the same info, but kvm_arch_pre_enable_virtualization() and kvm_arch_post_disable_virtualization() are a bit cumbersome, and make it a bit less obvious that they are intended to be implemented as a pair. Reviewed-by: Chao Gao <[email protected]> Reviewed-by: Kai Huang <[email protected]> Acked-by: Kai Huang <[email protected]> Tested-by: Farrah Chen <[email protected]> Signed-off-by: Sean Christopherson <[email protected]> Message-ID: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent b4886fa commit b67107a

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

include/linux/kvm_host.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,20 @@ static inline void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu) {}
15211521
#endif
15221522

15231523
#ifdef CONFIG_KVM_GENERIC_HARDWARE_ENABLING
1524+
/*
1525+
* kvm_arch_{enable,disable}_virtualization() are called on one CPU, under
1526+
* kvm_usage_lock, immediately after/before 0=>1 and 1=>0 transitions of
1527+
* kvm_usage_count, i.e. at the beginning of the generic hardware enabling
1528+
* sequence, and at the end of the generic hardware disabling sequence.
1529+
*/
1530+
void kvm_arch_enable_virtualization(void);
1531+
void kvm_arch_disable_virtualization(void);
1532+
/*
1533+
* kvm_arch_{enable,disable}_virtualization_cpu() are called on "every" CPU to
1534+
* do the actual twiddling of hardware bits. The hooks are called on all
1535+
* online CPUs when KVM enables/disabled virtualization, and on a single CPU
1536+
* when that CPU is onlined/offlined (including for Resume/Suspend).
1537+
*/
15241538
int kvm_arch_enable_virtualization_cpu(void);
15251539
void kvm_arch_disable_virtualization_cpu(void);
15261540
#endif

virt/kvm/kvm_main.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5581,6 +5581,16 @@ static DEFINE_PER_CPU(bool, virtualization_enabled);
55815581
static DEFINE_MUTEX(kvm_usage_lock);
55825582
static int kvm_usage_count;
55835583

5584+
__weak void kvm_arch_enable_virtualization(void)
5585+
{
5586+
5587+
}
5588+
5589+
__weak void kvm_arch_disable_virtualization(void)
5590+
{
5591+
5592+
}
5593+
55845594
static int kvm_enable_virtualization_cpu(void)
55855595
{
55865596
if (__this_cpu_read(virtualization_enabled))
@@ -5680,6 +5690,8 @@ static int kvm_enable_virtualization(void)
56805690
if (kvm_usage_count++)
56815691
return 0;
56825692

5693+
kvm_arch_enable_virtualization();
5694+
56835695
r = cpuhp_setup_state(CPUHP_AP_KVM_ONLINE, "kvm/cpu:online",
56845696
kvm_online_cpu, kvm_offline_cpu);
56855697
if (r)
@@ -5710,6 +5722,7 @@ static int kvm_enable_virtualization(void)
57105722
unregister_syscore_ops(&kvm_syscore_ops);
57115723
cpuhp_remove_state(CPUHP_AP_KVM_ONLINE);
57125724
err_cpuhp:
5725+
kvm_arch_disable_virtualization();
57135726
--kvm_usage_count;
57145727
return r;
57155728
}
@@ -5723,6 +5736,7 @@ static void kvm_disable_virtualization(void)
57235736

57245737
unregister_syscore_ops(&kvm_syscore_ops);
57255738
cpuhp_remove_state(CPUHP_AP_KVM_ONLINE);
5739+
kvm_arch_disable_virtualization();
57265740
}
57275741

57285742
static int kvm_init_virtualization(void)

0 commit comments

Comments
 (0)