KVM: hardware enable/disable reorganize
Subsystems (15)
v5 cover KVM: hardware enable/disable reorganize
From: Isaku Yamahata <[email protected]>
I've updated the patch series based on Chao's review and eliminated kvm_arch_pre_hardware_unsetup() and compatibility check on resume.
Those two patch are compile only tested.
- "KVM: arm64: Simplify the CPUHP logic"
- "RFC: KVM: powerpc: Move processor compatibility check to hardware setup"
- "RFC: KVM: Remove cpus_hardware_enabled and related sanity check"
Changes from v4:
- reorder/reorganize the patch. Especially introduced one patch for each PM events. reboot, suspend, resume, and cpu online/offline.
- eliminated kvm_arch_pre_hardware_unsetup().
- removed compatibility check on resume
- "KVM: arm64: Simplify the CPUHP logic": fixed up signed-off-by and the link.
- "RFC: KVM: powerpc: Move processor compatibility check to hardware setup" move the comment in the code to the commit message.
This patch series is to implement the suggestion by Sean Christopherson [1] to reorganize enable/disable cpu virtualization feature by replacing the arch-generic current enable/disable logic with PM related hooks. And convert kvm/x86 to use new hooks.
- Untable x86 hardware enable logic, snapshot MSRs for user return notifier, enabling cpu virtualization on cpu online and platform resume. and real enabling of CPU virtualization feature
- Introduce hooks related to PM.
- Convert kvm/x86 code to user those hooks.
- Split out hardware enabling/disabling logic into a separate file. Compile it for non-x86 code. Once conversion of other KVM archs is done, this file can be dropped.
- Delete kvm_arch_check_processor_compat()
- Delete cpus_hardware_enabled. 29/30 and 30/30
[1] https://lore.kernel.org/kvm/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
Changes from v3:
- Updated "KVM: arm64: Simplify the CPUHP logic".
- add preempt_disable/enable() around hardware_enable/disable() to keep the assumption of the arch callback.
- Eliminated arch compat check callback, kvm_arch_check_processor_compat().
Changes from v2:
- Replace the first patch("KVM: x86: Drop kvm_user_return_msr_cpu_online()") with Sean's implementation
- Included all patches of "Improve KVM's interaction with CPU hotplug" [2] Until v2, Tried to cherry-pick the least patches of it. It turned out that all the patches are desirable.
Changes from v1:
- Add a patch "KVM: Rename and move CPUHP_AP_KVM_STARTING to ONLINE section" to make online/offline callback to run thread context to use mutex instead of spin lock
- fixes pointed by Chao Gao
Chao Gao (4):
KVM: x86: Move check_processor_compatibility from init ops to runtime
ops
Partially revert "KVM: Pass kvm_init()'s opaque param to additional
arch funcs"
KVM: Rename and move CPUHP_AP_KVM_STARTING to ONLINE section
KVM: Do compatibility checks on hotplugged CPUs
Isaku Yamahata (24):
KVM: x86: Use this_cpu_ptr() instead of
per_cpu_ptr(smp_processor_id())
KVM: Provide more information in kernel log if hardware enabling fails
KVM: Drop kvm_count_lock and instead protect kvm_usage_count with
kvm_lock
KVM: Add arch hooks when VM is added/deleted
KVM: Add arch hook for reboot event
KVM: Add arch hook for suspend
KVM: Add arch hook for resume event
KVM: Add arch hook for cpu online event
KVM: Add arch hook for cpu offline event
KVM: Remove on_each_cpu(hardware_disable_nolock) in kvm_exit()
KVM: Move out KVM arch PM hooks and hardware enable/disable logic
KVM: kvm_arch.c: Remove _nolock post fix
KVM: kvm_arch.c: Remove a global variable, hardware_enable_failed
KVM: Introduce an arch wrapper to check all processor compatibility
KVM: x86: Duplicate arch callbacks related to pm events and compat
check
KVM: x86: Move TSC fixup logic to KVM arch resume callback
KVM: Eliminate kvm_arch_post_init_vm()
KVM: Add config to not compile kvm_arch.c
KVM: x86: Delete kvm_arch_hardware_enable/disable()
KVM: x86: Make x86 processor compat check callback empty
RFC: KVM: powerpc: Move processor compatibility check to hardware
setup
KVM: Eliminate kvm_arch_check_processor_compat()
RFC: KVM: x86: Remove cpus_hardware_enabled and related sanity check
RFC: KVM: Remove cpus_hardware_enabled and related sanity check
Marc Zyngier (1):
KVM: arm64: Simplify the CPUHP logic
Sean Christopherson (1):
KVM: x86: Drop kvm_user_return_msr_cpu_online()
Documentation/virt/kvm/locking.rst | 14 +- arch/arm64/kvm/arch_timer.c | 27 ++-- arch/arm64/kvm/arm.c | 18 ++- arch/arm64/kvm/vgic/vgic-init.c | 19 +-- arch/mips/kvm/mips.c | 5 - arch/powerpc/kvm/powerpc.c | 5 - arch/riscv/kvm/main.c | 5 - arch/s390/kvm/kvm-s390.c | 5 - arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/svm/svm.c | 4 +- arch/x86/kvm/vmx/vmx.c | 24 ++-- arch/x86/kvm/x86.c | 201 ++++++++++++++++++++++++---- include/kvm/arm_arch_timer.h | 4 + include/kvm/arm_vgic.h | 4 + include/linux/cpuhotplug.h | 5 +- include/linux/kvm_host.h | 13 +- virt/kvm/Kconfig | 3 + virt/kvm/Makefile.kvm | 3 + virt/kvm/kvm_arch.c | 132 +++++++++++++++++++ virt/kvm/kvm_main.c | 205 +++++++++-------------------- 22 files changed, 443 insertions(+), 257 deletions(-) create mode 100644 virt/kvm/kvm_arch.c
base-commit: 521a547ced6477c54b4b0cc206000406c221b4d6
Signature · 1 line
2.25.1
PATCH v5 1/30 KVM: x86: Drop kvm_user_return_msr_cpu_online()
From: Sean Christopherson <[email protected]>
KVM/X86 uses user return notifier to switch MSR for guest or user space. Snapshot host values on CPU online, change MSR values for guest, and restore them on returning to user space. The current code abuses kvm_arch_hardware_enable() which is called on kvm module initialization or CPU online.
Remove such the abuse of kvm_arch_hardware_enable() by capturing the host value on the first change of the MSR value to guest VM instead of CPU online.
--- arch/x86/kvm/x86.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-)
arch/x86/kvm/x86.c +14 -5
@@ -196,6 +196,7 @@ module_param(eager_page_split, bool, 0644);struct kvm_user_return_msrs {struct user_return_notifier urn; +bool initialized;bool registered;struct kvm_user_return_msr_values {u64 host;
@@ -409,18 +410,20 @@ int kvm_find_user_return_msr(u32 msr) }EXPORT_SYMBOL_GPL (kvm_find_user_return_msr); -static void kvm_user_return_msr_cpu_online (void ) +static void kvm_user_return_msr_init_cpu (struct kvm_user_return_msrs *msrs) { -unsigned int cpu =smp_processor_id (); -struct kvm_user_return_msrs *msrs =per_cpu_ptr (user_return_msrs, cpu);u64 value;int i; +if (msrs->initialized) +return ; +for (i =0 ; i < kvm_nr_uret_msrs; ++i) {rdmsrl_safe (kvm_uret_msrs_list[i], &value); msrs->values[i].host = value; msrs->values[i].curr = value; } + msrs->initialized = true; }int kvm_set_user_return_msr (unsigned slot,u64 value,u64 mask)
@@ -429,6 +432,8 @@ int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask)struct kvm_user_return_msrs *msrs =per_cpu_ptr (user_return_msrs, cpu);int err; +kvm_user_return_msr_init_cpu (msrs); + value = (value & mask) | (msrs->values[slot].host & ~mask);if (value == msrs->values[slot].curr)return 0 ;
@@ -9229,7 +9234,12 @@ int kvm_arch_init(void *opaque)return -ENOMEM ; } - user_return_msrs = alloc_percpu(struct kvm_user_return_msrs); +/* +* __GFP_ZERO to ensure user_return_msrs.initialized = false. +* See kvm_user_return_msr_init_cpu(). +*/ + user_return_msrs = alloc_percpu_gfp(struct kvm_user_return_msrs, +GFP_KERNEL | __GFP_ZERO);if (!user_return_msrs) {printk (KERN_ERR "kvm: failed to allocate percpu kvm_user_return_msrs\n" ); r = -ENOMEM ;
@@ -11866,7 +11876,6 @@ int kvm_arch_hardware_enable(void)u64 max_tsc =0 ;bool stable, backwards_tsc = false; -kvm_user_return_msr_cpu_online (); ret =static_call (kvm_x86_hardware_enable)();if (ret !=0 )return ret;
Signature · 1 line
2.25.1
PATCH v5 2/30 KVM: x86: Use this_cpu_ptr() instead of per_cpu_ptr(smp_processor_id())
In reply to [email protected]From: Isaku Yamahata <[email protected]>
convert per_cpu_ptr(smp_processor_id()) to this_cpu_ptr() as trivial cleanup.
--- arch/x86/kvm/x86.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
arch/x86/kvm/x86.c +2 -4
@@ -428,8 +428,7 @@ static void kvm_user_return_msr_init_cpu(struct kvm_user_return_msrs *msrs)int kvm_set_user_return_msr (unsigned slot,u64 value,u64 mask) { -unsigned int cpu =smp_processor_id (); -struct kvm_user_return_msrs *msrs =per_cpu_ptr (user_return_msrs, cpu); +struct kvm_user_return_msrs *msrs =this_cpu_ptr (user_return_msrs);int err;kvm_user_return_msr_init_cpu (msrs);
@@ -453,8 +452,7 @@ EXPORT_SYMBOL_GPL(kvm_set_user_return_msr);static void drop_user_return_notifiers (void ) { -unsigned int cpu =smp_processor_id (); -struct kvm_user_return_msrs *msrs =per_cpu_ptr (user_return_msrs, cpu); +struct kvm_user_return_msrs *msrs =this_cpu_ptr (user_return_msrs);if (msrs->registered)kvm_on_user_return (&msrs->urn);
Signature · 1 line
2.25.1
PATCH v5 3/30 KVM: x86: Move check_processor_compatibility from init ops to runtime ops
In reply to [email protected]From: Chao Gao <[email protected]>
so that KVM can do compatibility checks on hotplugged CPUs. Drop __init from check_processor_compatibility() and its callees.
use a static_call() to invoke .check_processor_compatibility.
Opportunistically rename {svm,vmx}_check_processor_compat to conform to the naming convention of fields of kvm_x86_ops.
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/svm/svm.c | 4 ++-- arch/x86/kvm/vmx/vmx.c | 14 +++++++------- arch/x86/kvm/x86.c | 3 +-- 5 files changed, 12 insertions(+), 12 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -129,6 +129,7 @@ KVM_X86_OP(msr_filter_changed) KVM_X86_OP(complete_emulated_msr) KVM_X86_OP(vcpu_deliver_sipi_vector) KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons); +KVM_X86_OP(check_processor_compatibility) #undef KVM_X86_OP #undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +1 -1
@@ -1445,6 +1445,7 @@ static inline u16 kvm_lapic_irq_dest_mode(bool dest_mode_logical)struct kvm_x86_ops {const char *name; +int (*check_processor_compatibility)(void );int (*hardware_enable)(void );void (*hardware_disable)(void );void (*hardware_unsetup)(void );
@@ -1655,7 +1656,6 @@ struct kvm_x86_nested_ops {struct kvm_x86_init_ops {int (*cpu_has_kvm_support)(void );int (*disabled_by_bios)(void ); -int (*check_processor_compatibility)(void );int (*hardware_setup)(void );unsigned int (*handle_intel_pt_intr)(void );
arch/x86/kvm/svm/svm.c +2 -2
@@ -4134,7 +4134,7 @@ svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) hypercall[2 ] =0xd9 ; } -static int __init svm_check_processor_compat (void ) +static int svm_check_processor_compatibility (void ) {return 0 ; }
@@ -4740,6 +4740,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .name = "kvm_amd", .hardware_unsetup = svm_hardware_unsetup, + .check_processor_compatibility = svm_check_processor_compatibility, .hardware_enable = svm_hardware_enable, .hardware_disable = svm_hardware_disable, .has_emulated_msr = svm_has_emulated_msr,
@@ -5122,7 +5123,6 @@ static struct kvm_x86_init_ops svm_init_ops __initdata = { .cpu_has_kvm_support = has_svm, .disabled_by_bios = is_disabled, .hardware_setup = svm_hardware_setup, - .check_processor_compatibility = svm_check_processor_compat, .runtime_ops = &svm_x86_ops, .pmu_ops = &amd_pmu_ops,
arch/x86/kvm/vmx/vmx.c +7 -7
@@ -2494,8 +2494,8 @@ static bool cpu_has_sgx(void)return cpuid_eax (0 ) >=0x12 && (cpuid_eax (0x12 ) &BIT (0 )); } -static __init int adjust_vmx_controls (u32 ctl_min,u32 ctl_opt, -u32 msr,u32 *result) +static int adjust_vmx_controls (u32 ctl_min,u32 ctl_opt, +u32 msr,u32 *result) {u32 vmx_msr_low, vmx_msr_high;u32 ctl = ctl_min | ctl_opt;
@@ -2513,7 +2513,7 @@ static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,return 0 ; } -static __init u64 adjust_vmx_controls64 (u64 ctl_opt,u32 msr) +static u64 adjust_vmx_controls64 (u64 ctl_opt,u32 msr) {u64 allowed;
@@ -2522,8 +2522,8 @@ static __init u64 adjust_vmx_controls64(u64 ctl_opt, u32 msr)return ctl_opt & allowed; } -static __init int setup_vmcs_config (struct vmcs_config *vmcs_conf, -struct vmx_capability *vmx_cap) +static int setup_vmcs_config (struct vmcs_config *vmcs_conf, +struct vmx_capability *vmx_cap) {u32 vmx_msr_low, vmx_msr_high;u32 min, opt, min2, opt2;
@@ -7416,7 +7416,7 @@ static int vmx_vm_init(struct kvm *kvm)return 0 ; } -static int __init vmx_check_processor_compat (void ) +static int vmx_check_processor_compatibility (void ) {struct vmcs_config vmcs_conf;struct vmx_capability vmx_cap;
@@ -8014,6 +8014,7 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = { .hardware_unsetup = vmx_hardware_unsetup, + .check_processor_compatibility = vmx_check_processor_compatibility, .hardware_enable = vmx_hardware_enable, .hardware_disable = vmx_hardware_disable, .has_emulated_msr = vmx_has_emulated_msr,
@@ -8403,7 +8404,6 @@ static __init int hardware_setup(void)static struct kvm_x86_init_ops vmx_init_ops__initdata = { .cpu_has_kvm_support = cpu_has_kvm_support, .disabled_by_bios = vmx_disabled_by_bios, - .check_processor_compatibility = vmx_check_processor_compat, .hardware_setup = hardware_setup, .handle_intel_pt_intr =NULL ,
arch/x86/kvm/x86.c +1 -2
@@ -12031,7 +12031,6 @@ void kvm_arch_hardware_unsetup(void)int kvm_arch_check_processor_compat (void *opaque) {struct cpuinfo_x86 *c = &cpu_data (smp_processor_id ()); -struct kvm_x86_init_ops *ops = opaque;WARN_ON (!irqs_disabled ());
@@ -12039,7 +12038,7 @@ int kvm_arch_check_processor_compat(void *opaque) __cr4_reserved_bits(cpu_has, &boot_cpu_data)) return -EIO; -return ops->check_processor_compatibility (); +return static_call (kvm_x86_check_processor_compatibility)(); }bool kvm_vcpu_is_reset_bsp (struct kvm_vcpu *vcpu)
Signature · 1 line
2.25.1
PATCH v5 4/30 Partially revert "KVM: Pass kvm_init()'s opaque param to additional arch funcs"
In reply to [email protected]From: Chao Gao <[email protected]>
This partially reverts commit b99040853738 ("KVM: Pass kvm_init()'s opaque param to additional arch funcs") remove opaque from kvm_arch_check_processor_compat because no one uses this opaque now. Address conflicts for ARM (due to file movement) and manually handle RISC-V which comes after the commit.
And changes about kvm_arch_hardware_setup() in original commit are still needed so they are not reverted.
--- arch/arm64/kvm/arm.c | 2 +- arch/mips/kvm/mips.c | 2 +- arch/powerpc/kvm/powerpc.c | 2 +- arch/riscv/kvm/main.c | 2 +- arch/s390/kvm/kvm-s390.c | 2 +- arch/x86/kvm/x86.c | 2 +- include/linux/kvm_host.h | 2 +- virt/kvm/kvm_main.c | 16 +++------------- 8 files changed, 10 insertions(+), 20 deletions(-)
arch/arm64/kvm/arm.c +1 -1
@@ -68,7 +68,7 @@ int kvm_arch_hardware_setup(void *opaque)return 0 ; } -int kvm_arch_check_processor_compat (void *opaque) +int kvm_arch_check_processor_compat (void ) {return 0 ; }
arch/mips/kvm/mips.c +1 -1
@@ -140,7 +140,7 @@ int kvm_arch_hardware_setup(void *opaque)return 0 ; } -int kvm_arch_check_processor_compat (void *opaque) +int kvm_arch_check_processor_compat (void ) {return 0 ; }
arch/powerpc/kvm/powerpc.c +1 -1
@@ -447,7 +447,7 @@ int kvm_arch_hardware_setup(void *opaque)return 0 ; } -int kvm_arch_check_processor_compat (void *opaque) +int kvm_arch_check_processor_compat (void ) {return kvmppc_core_check_processor_compat (); }
arch/riscv/kvm/main.c +1 -1
@@ -20,7 +20,7 @@ long kvm_arch_dev_ioctl(struct file *filp,return -EINVAL ; } -int kvm_arch_check_processor_compat (void *opaque) +int kvm_arch_check_processor_compat (void ) {return 0 ; }
arch/s390/kvm/kvm-s390.c +1 -1
@@ -254,7 +254,7 @@ int kvm_arch_hardware_enable(void)return 0 ; } -int kvm_arch_check_processor_compat (void *opaque) +int kvm_arch_check_processor_compat (void ) {return 0 ; }
arch/x86/kvm/x86.c +1 -1
@@ -12028,7 +12028,7 @@ void kvm_arch_hardware_unsetup(void)static_call (kvm_x86_hardware_unsetup)(); } -int kvm_arch_check_processor_compat (void *opaque) +int kvm_arch_check_processor_compat (void ) {struct cpuinfo_x86 *c = &cpu_data (smp_processor_id ());
include/linux/kvm_host.h +1 -1
@@ -1438,7 +1438,7 @@ int kvm_arch_hardware_enable(void);void kvm_arch_hardware_disable (void );int kvm_arch_hardware_setup (void *opaque);void kvm_arch_hardware_unsetup (void ); -int kvm_arch_check_processor_compat (void *opaque); +int kvm_arch_check_processor_compat (void );int kvm_arch_vcpu_runnable (struct kvm_vcpu *vcpu);bool kvm_arch_vcpu_in_kernel (struct kvm_vcpu *vcpu);int kvm_arch_vcpu_should_kick (struct kvm_vcpu *vcpu);
virt/kvm/kvm_main.c +3 -13
@@ -5799,22 +5799,14 @@ void kvm_unregister_perf_callbacks(void) }#endif -struct kvm_cpu_compat_check { -void *opaque; -int *ret; -}; - -static void check_processor_compat (void *data) +static void check_processor_compat (void *rtn) { -struct kvm_cpu_compat_check *c = data; - - *c->ret =kvm_arch_check_processor_compat (c->opaque); + *(int *)rtn =kvm_arch_check_processor_compat (); }int kvm_init (void *opaque,unsigned vcpu_size,unsigned vcpu_align,struct module *module) { -struct kvm_cpu_compat_check c;int r;int cpu;
@@ -5842,10 +5834,8 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,if (r <0 ) goto out_free_1; - c.ret = &r; - c.opaque = opaque;for_each_online_cpu (cpu) { -smp_call_function_single (cpu, check_processor_compat, &c,1 ); +smp_call_function_single (cpu, check_processor_compat, &r,1 );if (r <0 ) goto out_free_2; }
Signature · 1 line
2.25.1
PATCH v5 5/30 KVM: Provide more information in kernel log if hardware enabling fails
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Provide the name of the calling function to hardware_enable_nolock() and include it in the error message to provide additional information on exactly what path failed.
Opportunistically bump the pr_info() to pr_warn(), failure to enable virtualization support is warn-worthy as _something_ is wrong with the system.
--- virt/kvm/kvm_main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
virt/kvm/kvm_main.c +2 -1
@@ -5006,7 +5006,8 @@ static void hardware_enable_nolock(void *junk)if (r) {cpumask_clear_cpu (cpu, cpus_hardware_enabled);atomic_inc (&hardware_enable_failed); -pr_info ("kvm: enabling virtualization on CPU%d failed\n" , cpu); +pr_warn ("kvm: enabling virtualization on CPU%d failed during %pSb\n" , + cpu,__builtin_return_address (0 )); } }
Signature · 1 line
2.25.1
PATCH v5 9/30 KVM: Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Because kvm_count_lock unnecessarily complicates the KVM locking convention Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock for simplicity. kvm_arch_hardware_enable/disable() callbacks depend on non-preemptiblity with the spin lock. Add preempt_disable/enable() around hardware enable/disable callback to keep the assumption.
Because kvm_suspend() and kvm_resume() is called with interrupt disabled, they don't need preempt_disable/enable() pair.
Opportunistically add some comments on locking.
--- Documentation/virt/kvm/locking.rst | 14 +++----- virt/kvm/kvm_main.c | 56 +++++++++++++++++++++++------- 2 files changed, 49 insertions(+), 21 deletions(-)
Documentation/virt/kvm/locking.rst +5 -9
@@ -216,15 +216,11 @@ time it will be set using the Dirty tracking mechanism described above. :Type: mutex :Arch: any :Protects: - vm_list - -``kvm_count_lock`` -^^^^^^^^^^^^^^^^^^ - -:Type: raw_spinlock_t -:Arch: any -:Protects: - hardware virtualization enable/disable -:Comment: 'raw' because hardware enabling/disabling must be atomic /wrt - migration. + - kvm_usage_count + - hardware virtualization enable/disable +:Comment: Use cpus_read_lock() for hardware virtualization enable/disable + because hardware enabling/disabling must be atomic /wrt + CPU hotplug. The lock order is cpus lock => kvm_lock. ``kvm->mn_invalidate_lock`` ^^^^^^^^^^^^^^^^^^^^^^^^^^^
virt/kvm/kvm_main.c +44 -12
131 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v5 6/30 KVM: arm64: Simplify the CPUHP logic
In reply to [email protected]From: Marc Zyngier <[email protected]>
For a number of historical reasons, the KVM/arm64 hotplug setup is pretty complicated, and we have two extra CPUHP notifiers for vGIC and timers.
It looks pretty pointless, and gets in the way of further changes. So let's just expose some helpers that can be called from the core CPUHP callback, and get rid of everything else.
This gives us the opportunity to drop a useless notifier entry, as well as tidy-up the timer enable/disable, which was a bit odd.
--- arch/arm64/kvm/arch_timer.c | 27 ++++++++++----------------- arch/arm64/kvm/arm.c | 13 +++++++++++++ arch/arm64/kvm/vgic/vgic-init.c | 19 ++----------------- include/kvm/arm_arch_timer.h | 4 ++++ include/kvm/arm_vgic.h | 4 ++++ include/linux/cpuhotplug.h | 3 --- 6 files changed, 33 insertions(+), 37 deletions(-)
arch/arm64/kvm/arch_timer.c +10 -17
@@ -811,10 +811,18 @@ void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu) ptimer->host_timer_irq_flags = host_ptimer_irq_flags; } -static void kvm_timer_init_interrupt (void *info) +void kvm_timer_cpu_up (void ) {enable_percpu_irq (host_vtimer_irq, host_vtimer_irq_flags); -enable_percpu_irq (host_ptimer_irq, host_ptimer_irq_flags); +if (host_ptimer_irq) +enable_percpu_irq (host_ptimer_irq, host_ptimer_irq_flags); +} + +void kvm_timer_cpu_down (void ) +{ +disable_percpu_irq (host_vtimer_irq); +if (host_ptimer_irq) +disable_percpu_irq (host_ptimer_irq); }int kvm_arm_timer_set_reg (struct kvm_vcpu *vcpu,u64 regid,u64 value)
@@ -976,18 +984,6 @@ void kvm_arm_timer_write_sysreg(struct kvm_vcpu *vcpu,preempt_enable (); } -static int kvm_timer_starting_cpu (unsigned int cpu) -{ -kvm_timer_init_interrupt (NULL ); -return 0 ; -} - -static int kvm_timer_dying_cpu (unsigned int cpu) -{ -disable_percpu_irq (host_vtimer_irq); -return 0 ; -} -static int timer_irq_set_vcpu_affinity (struct irq_data *d,void *vcpu) {if (vcpu)
@@ -1185,9 +1181,6 @@ int kvm_timer_hyp_init(bool has_gic) goto out_free_irq; } -cpuhp_setup_state (CPUHP_AP_KVM_ARM_TIMER_STARTING , -"kvm/arm/timer:starting" , kvm_timer_starting_cpu, - kvm_timer_dying_cpu);return 0 ; out_free_irq:free_percpu_irq (host_vtimer_irq,kvm_get_running_vcpus ());
arch/arm64/kvm/arm.c +13 -0
@@ -1676,7 +1676,15 @@ static void _kvm_arch_hardware_enable(void *discard)int kvm_arch_hardware_enable (void ) { +int was_enabled =__this_cpu_read (kvm_arm_hardware_enabled); +_kvm_arch_hardware_enable (NULL ); + +if (!was_enabled) { +kvm_vgic_cpu_up (); +kvm_timer_cpu_up (); + } +return 0 ; }
@@ -1690,6 +1698,11 @@ static void _kvm_arch_hardware_disable(void *discard)void kvm_arch_hardware_disable (void ) { +if (__this_cpu_read (kvm_arm_hardware_enabled)) { +kvm_timer_cpu_down (); +kvm_vgic_cpu_down (); + } +if (!is_protected_kvm_enabled ())_kvm_arch_hardware_disable (NULL ); }
arch/arm64/kvm/vgic/vgic-init.c +2 -17
@@ -465,17 +465,15 @@ int kvm_vgic_map_resources(struct kvm *kvm)/* GENERIC PROBE */ -static int vgic_init_cpu_starting (unsigned int cpu) +void kvm_vgic_cpu_up (void ) {enable_percpu_irq (kvm_vgic_global_state.maint_irq,0 ); -return 0 ; } -static int vgic_init_cpu_dying (unsigned int cpu) +void kvm_vgic_cpu_down (void ) {disable_percpu_irq (kvm_vgic_global_state.maint_irq); -return 0 ; }static irqreturn_t vgic_maintenance_handler (int irq,void *data)
@@ -584,19 +582,6 @@ int kvm_vgic_hyp_init(void)return ret; } - ret =cpuhp_setup_state (CPUHP_AP_KVM_ARM_VGIC_INIT_STARTING , -"kvm/arm/vgic:starting" , - vgic_init_cpu_starting, vgic_init_cpu_dying); -if (ret) { -kvm_err ("Cannot register vgic CPU notifier\n" ); - goto out_free_irq; - } -kvm_info ("vgic interrupt IRQ%d\n" , kvm_vgic_global_state.maint_irq);return 0 ; - -out_free_irq: -free_percpu_irq (kvm_vgic_global_state.maint_irq, -kvm_get_running_vcpus ()); -return ret; }
include/kvm/arm_arch_timer.h +4 -0
@@ -104,4 +104,8 @@ void kvm_arm_timer_write_sysreg(struct kvm_vcpu *vcpu,u32 timer_get_ctl (struct arch_timer_context *ctxt);u64 timer_get_cval (struct arch_timer_context *ctxt); +/* CPU HP callbacks */ +void kvm_timer_cpu_up (void ); +void kvm_timer_cpu_down (void ); +#endif
include/kvm/arm_vgic.h +4 -0
@@ -431,4 +431,8 @@ int vgic_v4_load(struct kvm_vcpu *vcpu);void vgic_v4_commit (struct kvm_vcpu *vcpu);int vgic_v4_put (struct kvm_vcpu *vcpu,bool need_db); +/* CPU HP callbacks */ +void kvm_vgic_cpu_up (void ); +void kvm_vgic_cpu_down (void ); +#endif /* __KVM_ARM_VGIC_H */
include/linux/cpuhotplug.h +0 -3
@@ -186,9 +186,6 @@ enum cpuhp_state {CPUHP_AP_TI_GP_TIMER_STARTING ,CPUHP_AP_HYPERV_TIMER_STARTING ,CPUHP_AP_KVM_STARTING , -CPUHP_AP_KVM_ARM_VGIC_INIT_STARTING , -CPUHP_AP_KVM_ARM_VGIC_STARTING , -CPUHP_AP_KVM_ARM_TIMER_STARTING ,/* Must be the last timer callback */ CPUHP_AP_DUMMY_TIMER_STARTING , CPUHP_AP_ARM_XEN_STARTING,
Signature · 1 line
2.25.1
PATCH v5 12/30 KVM: Add arch hook for suspend
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Factor out the logic on suspend event as arch callback. Later kvm/x86 will override it.
--- include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-)
include/linux/kvm_host.h +1 -0
@@ -1435,6 +1435,7 @@ static inline void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu) {}#endif int kvm_arch_reboot (int val); +int kvm_arch_suspend (int usage_count);int kvm_arch_hardware_enable (void );void kvm_arch_hardware_disable (void );
virt/kvm/kvm_main.c +12 -9
@@ -1148,6 +1148,17 @@ int __weak kvm_arch_reboot(int val)return NOTIFY_OK ; } +int __weak kvm_arch_suspend (int usage_count) +{ +if (usage_count) +/* +* Because kvm_suspend() is called with interrupt disabled, no +* need to disable preemption. +*/ +hardware_disable_nolock (NULL ); +return 0 ; +} +/* * Called just after removing the VM from the vm_list, but before doing any * other destruction.
@@ -5753,15 +5764,7 @@ static int kvm_suspend(void)* locking. */ lockdep_assert_not_held (&kvm_lock); - -if (kvm_usage_count) { -/* -* Because kvm_suspend() is called with interrupt disabled, no -* need to disable preemption. -*/ -hardware_disable_nolock (NULL ); - } -return 0 ; +return kvm_arch_suspend (kvm_usage_count); }static void kvm_resume (void )
Signature · 1 line
2.25.1
PATCH v5 11/30 KVM: Add arch hook for reboot event
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Factor out the logic on reboot event as arch hook. Later kvm/x86 overrides it.
--- include/linux/kvm_host.h | 2 ++ virt/kvm/kvm_main.c | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-)
include/linux/kvm_host.h +2 -0
@@ -1434,6 +1434,8 @@ void kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu, struct dentry *debugfs_static inline void kvm_create_vcpu_debugfs (struct kvm_vcpu *vcpu) {}#endif +int kvm_arch_reboot (int val); +int kvm_arch_hardware_enable (void );void kvm_arch_hardware_disable (void );int kvm_arch_hardware_setup (void *opaque);
virt/kvm/kvm_main.c +16 -2
@@ -1142,6 +1142,12 @@ int __weak kvm_arch_del_vm(int usage_count)return 0 ; } +int __weak kvm_arch_reboot (int val) +{ +on_each_cpu (hardware_disable_nolock,NULL ,1 ); +return NOTIFY_OK ; +} +/* * Called just after removing the VM from the vm_list, but before doing any * other destruction.
@@ -5135,6 +5141,8 @@ static void kvm_del_vm(void)static int kvm_reboot (struct notifier_block *notifier,unsigned long val,void *v) { +int r; +/* * Some (well, at least mine) BIOSes hang on reboot if * in vmx root mode.
@@ -5143,8 +5151,14 @@ static int kvm_reboot(struct notifier_block *notifier, unsigned long val,*/ pr_info ("kvm: exiting hardware virtualization\n" ); kvm_rebooting = true; -on_each_cpu (hardware_disable_nolock,NULL ,1 ); -return NOTIFY_OK ; + +/* This hook is called without cpuhotplug disabled. */ +cpus_read_lock (); +mutex_lock (&kvm_lock); + r =kvm_arch_reboot (val); +mutex_unlock (&kvm_lock); +cpus_read_unlock (); +return r; }static struct notifier_block kvm_reboot_notifier = {
Signature · 1 line
2.25.1
PATCH v5 7/30 KVM: Rename and move CPUHP_AP_KVM_STARTING to ONLINE section
In reply to [email protected]From: Chao Gao <[email protected]>
The CPU STARTING section doesn't allow callbacks to fail. Move KVM's hotplug callback to ONLINE section so that it can abort onlining a CPU in certain cases to avoid potentially breaking VMs running on existing CPUs. For example, when kvm fails to enable hardware virtualization on the hotplugged CPU.
Place KVM's hotplug state before CPUHP_AP_SCHED_WAIT_EMPTY as it ensures when offlining a CPU, all user tasks and non-pinned kernel tasks have left the CPU, i.e. there cannot be a vCPU task around. So, it is safe for KVM's CPU offline callback to disable hardware virtualization at that point. Likewise, KVM's online callback can enable hardware virtualization before any vCPU task gets a chance to run on hotplugged CPUs.
KVM's CPU hotplug callbacks are renamed as well.
--- include/linux/cpuhotplug.h | 2 +- virt/kvm/kvm_main.c | 30 ++++++++++++++++++++++-------- 2 files changed, 23 insertions(+), 9 deletions(-)
include/linux/cpuhotplug.h +1 -1
@@ -185,7 +185,6 @@ enum cpuhp_state {CPUHP_AP_CSKY_TIMER_STARTING ,CPUHP_AP_TI_GP_TIMER_STARTING ,CPUHP_AP_HYPERV_TIMER_STARTING , -CPUHP_AP_KVM_STARTING ,/* Must be the last timer callback */ CPUHP_AP_DUMMY_TIMER_STARTING , CPUHP_AP_ARM_XEN_STARTING,
@@ -200,6 +199,7 @@ enum cpuhp_state {/* Online section invoked on the hotplugged CPU from the hotplug thread */ CPUHP_AP_ONLINE_IDLE , +CPUHP_AP_KVM_ONLINE ,CPUHP_AP_SCHED_WAIT_EMPTY ,CPUHP_AP_SMPBOOT_THREADS , CPUHP_AP_X86_VDSO_VMA_ONLINE,
virt/kvm/kvm_main.c +22 -8
@@ -5011,13 +5011,27 @@ static void hardware_enable_nolock(void *junk) } } -static int kvm_starting_cpu (unsigned int cpu) +static int kvm_online_cpu (unsigned int cpu) { +int ret =0 ; +raw_spin_lock (&kvm_count_lock); -if (kvm_usage_count) +/* +* Abort the CPU online process if hardware virtualization cannot +* be enabled. Otherwise running VMs would encounter unrecoverable +* errors when scheduled to this CPU. +*/ +if (kvm_usage_count) { +WARN_ON_ONCE (atomic_read (&hardware_enable_failed)); +hardware_enable_nolock (NULL ); +if (atomic_read (&hardware_enable_failed)) { +atomic_set (&hardware_enable_failed,0 ); + ret = -EIO ; + } + }raw_spin_unlock (&kvm_count_lock); -return 0 ; +return ret; }static void hardware_disable_nolock (void *junk)
@@ -5030,7 +5044,7 @@ static void hardware_disable_nolock(void *junk)kvm_arch_hardware_disable (); } -static int kvm_dying_cpu (unsigned int cpu) +static int kvm_offline_cpu (unsigned int cpu) {raw_spin_lock (&kvm_count_lock);if (kvm_usage_count)
@@ -5841,8 +5855,8 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, goto out_free_2; } - r =cpuhp_setup_state_nocalls (CPUHP_AP_KVM_STARTING ,"kvm/cpu:starting" , - kvm_starting_cpu, kvm_dying_cpu); + r =cpuhp_setup_state_nocalls (CPUHP_AP_KVM_ONLINE ,"kvm/cpu:online" , + kvm_online_cpu, kvm_offline_cpu);if (r) goto out_free_2;register_reboot_notifier (&kvm_reboot_notifier);
@@ -5903,7 +5917,7 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,kmem_cache_destroy (kvm_vcpu_cache); out_free_3:unregister_reboot_notifier (&kvm_reboot_notifier); -cpuhp_remove_state_nocalls (CPUHP_AP_KVM_STARTING ); +cpuhp_remove_state_nocalls (CPUHP_AP_KVM_ONLINE ); out_free_2:kvm_arch_hardware_unsetup (); out_free_1:
@@ -5929,7 +5943,7 @@ void kvm_exit(void)kvm_async_pf_deinit ();unregister_syscore_ops (&kvm_syscore_ops);unregister_reboot_notifier (&kvm_reboot_notifier); -cpuhp_remove_state_nocalls (CPUHP_AP_KVM_STARTING ); +cpuhp_remove_state_nocalls (CPUHP_AP_KVM_ONLINE );on_each_cpu (hardware_disable_nolock,NULL ,1 );kvm_arch_hardware_unsetup ();kvm_arch_exit ();
Signature · 1 line
2.25.1
PATCH v5 8/30 KVM: Do compatibility checks on hotplugged CPUs
In reply to [email protected]From: Chao Gao <[email protected]>
At init time, KVM does compatibility checks to ensure that all online CPUs support hardware virtualization and a common set of features. But KVM uses hotplugged CPUs without such compatibility checks. On Intel CPUs, this leads to #GP if the hotplugged CPU doesn't support VMX or vmentry failure if the hotplugged CPU doesn't meet minimal feature requirements.
Do compatibility checks when onlining a CPU and abort the online process if the hotplugged CPU is incompatible with online CPUs.
CPU hotplug is disabled during hardware_enable_all() to prevent the corner case as shown below. A hotplugged CPU marks itself online in cpu_online_mask (1) and enables interrupt (2) before invoking callbacks registered in ONLINE section (3). So, if hardware_enable_all() is invoked on another CPU right after (2), then on_each_cpu() in hardware_enable_all() invokes hardware_enable_nolock() on the hotplugged CPU before kvm_online_cpu() is called. This makes the CPU escape from compatibility checks, which is risky.
start_secondary { ...
set_cpu_online(smp_processor_id(), true); <- 1
...
local_irq_enable(); <- 2
...
cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); <- 3
}Keep compatibility checks at KVM init time. It can help to find incompatibility issues earlier and refuse to load arch KVM module (e.g., kvm-intel).
Loosen the WARN_ON in kvm_arch_check_processor_compat so that it can be invoked from KVM's CPU hotplug callback (i.e., kvm_online_cpu). Other arch doesn't depends on prohibiting of preemption because powerpc has "strcmp(cur_cpu_spec->cpu_name, "model name")" and other arch has "return 0". Only x86 kvm_arch_check_processor_compat() has issue.
Opportunistically, add a pr_err() for setup_vmcs_config() path in vmx_check_processor_compatibility() so that each possible error path has its own error message. Convert printk(KERN_ERR ... to pr_err to please checkpatch.pl
--- arch/x86/kvm/vmx/vmx.c | 10 ++++++---- arch/x86/kvm/x86.c | 11 +++++++++-- virt/kvm/kvm_main.c | 18 +++++++++++++++++- 3 files changed, 32 insertions(+), 7 deletions(-)
arch/x86/kvm/vmx/vmx.c +6 -4
@@ -7420,20 +7420,22 @@ static int vmx_check_processor_compatibility(void) {struct vmcs_config vmcs_conf;struct vmx_capability vmx_cap; +int cpu =smp_processor_id ();if (!this_cpu_has (X86_FEATURE_MSR_IA32_FEAT_CTL ) || !this_cpu_has (X86_FEATURE_VMX )) { -pr_err ("kvm: VMX is disabled on CPU %d\n" ,smp_processor_id ()); +pr_err ("kvm: VMX is disabled on CPU %d\n" , cpu);return -EIO ; } -if (setup_vmcs_config (&vmcs_conf, &vmx_cap) <0 ) +if (setup_vmcs_config (&vmcs_conf, &vmx_cap) <0 ) { +pr_err ("kvm: failed to setup vmcs config on CPU %d\n" , cpu);return -EIO ; + }if (nested)nested_vmx_setup_ctls_msrs (&vmcs_conf.nested, vmx_cap.ept);if (memcmp (&vmcs_config, &vmcs_conf,sizeof (struct vmcs_config )) !=0 ) { -printk (KERN_ERR "kvm: CPU %d feature inconsistency!\n" , -smp_processor_id ()); +pr_err ("kvm: CPU %d feature inconsistency!\n" , cpu);return -EIO ; }return 0 ;
arch/x86/kvm/x86.c +9 -2
@@ -12030,9 +12030,16 @@ void kvm_arch_hardware_unsetup(void)int kvm_arch_check_processor_compat (void ) { -struct cpuinfo_x86 *c = &cpu_data (smp_processor_id ()); +int cpu =smp_processor_id (); +struct cpuinfo_x86 *c = &cpu_data (cpu); -WARN_ON (!irqs_disabled ()); +/* +* Compatibility checks are done when loading KVM or in KVM's CPU +* hotplug callback. It ensures all online CPUs are compatible to run +* vCPUs. For other cases, compatibility checks are unnecessary or +* even problematic. Try to detect improper usages here. +*/ +WARN_ON (!irqs_disabled () &&cpu_active (cpu));if (__cr4_reserved_bits (cpu_has, c) !=__cr4_reserved_bits (cpu_has, &boot_cpu_data))
virt/kvm/kvm_main.c +17 -1
@@ -5013,7 +5013,11 @@ static void hardware_enable_nolock(void *junk)static int kvm_online_cpu (unsigned int cpu) { -int ret =0 ; +int ret; + + ret =kvm_arch_check_processor_compat (); +if (ret) +return ret;raw_spin_lock (&kvm_count_lock);/*
@@ -5073,6 +5077,17 @@ static int hardware_enable_all(void) {int r =0 ; +/* +* During onlining a CPU, cpu_online_mask is set before kvm_online_cpu() +* is called. on_each_cpu() between them includes the CPU. As a result, +* hardware_enable_nolock() may get invoked before kvm_online_cpu(). +* This would enable hardware virtualization on that cpu without +* compatibility checks, which can potentially crash system or break +* running VMs. +* +* Disable CPU hotplug to prevent this case from happening. +*/ +cpus_read_lock ();raw_spin_lock (&kvm_count_lock); kvm_usage_count++;
@@ -5087,6 +5102,7 @@ static int hardware_enable_all(void) }raw_spin_unlock (&kvm_count_lock); +cpus_read_unlock ();return r; }
Signature · 1 line
2.25.1
PATCH v5 10/30 KVM: Add arch hooks when VM is added/deleted
In reply to [email protected]From: Isaku Yamahata <[email protected]>
and pass kvm_usage_count with kvm_lock. Move kvm_arch_post_init_vm() under kvm_arch_add_vm(). Replace enable/disable_hardware_all() with the default implementation of kvm_arch_add/del_vm(). Later kvm_arch_post_init_vm() is deleted once x86 overrides kvm_arch_add_vm().
--- include/linux/kvm_host.h | 2 + virt/kvm/kvm_main.c | 121 ++++++++++++++++++++------------------- 2 files changed, 65 insertions(+), 58 deletions(-)
include/linux/kvm_host.h +2 -0
@@ -1445,6 +1445,8 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu);bool kvm_arch_dy_runnable (struct kvm_vcpu *vcpu);bool kvm_arch_dy_has_pending_interrupt (struct kvm_vcpu *vcpu);int kvm_arch_post_init_vm (struct kvm *kvm); +int kvm_arch_add_vm (struct kvm *kvm,int usage_count); +int kvm_arch_del_vm (int usage_count);void kvm_arch_pre_destroy_vm (struct kvm *kvm);int kvm_arch_create_vm_debugfs (struct kvm *kvm);
virt/kvm/kvm_main.c +63 -58
171 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v5 13/30 KVM: Add arch hook for resume event
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Factor out the logic on resume event as arch callback. Later kvm/x86 will override it.
--- include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-)
include/linux/kvm_host.h +1 -0
@@ -1436,6 +1436,7 @@ static inline void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu) {}int kvm_arch_reboot (int val);int kvm_arch_suspend (int usage_count); +void kvm_arch_resume (int usage_count);int kvm_arch_hardware_enable (void );void kvm_arch_hardware_disable (void );
virt/kvm/kvm_main.c +8 -4
@@ -1159,6 +1159,12 @@ int __weak kvm_arch_suspend(int usage_count)return 0 ; } +void __weak kvm_arch_resume (int usage_count) +{ +if (usage_count) +hardware_enable_nolock (NULL ); +} +/* * Called just after removing the VM from the vm_list, but before doing any * other destruction.
@@ -5769,10 +5775,8 @@ static int kvm_suspend(void)static void kvm_resume (void ) { -if (kvm_usage_count) { -lockdep_assert_not_held (&kvm_lock); -hardware_enable_nolock (NULL ); - } +lockdep_assert_not_held (&kvm_lock); +kvm_arch_resume (kvm_usage_count); }static struct syscore_ops kvm_syscore_ops = {
Signature · 1 line
2.25.1
PATCH v5 27/30 RFC: KVM: powerpc: Move processor compatibility check to hardware setup
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Move processor compatibility check from kvm_arch_processor_compat() into kvm_arch_hardware_setup(). The check does model name comparison with a global variable, cur_cpu_spec. There is no point to check it at run time on all processors.
kvmppc_core_check_processor_compat() checks the global variable. There are five implementation for it as follows.
arch/powerpc/include/asm/cputable.h: extern struct cpu_spec *cur_cpu_spec; arch/powerpc/kvm/book3s.c: return 0
arch/powerpc/kvm/e500.c: strcmp(cur_cpu_spec->cpu_name, "e500v2") arch/powerpc/kvm/e500mc.c: strcmp(cur_cpu_spec->cpu_name, "e500mc")
strcmp(cur_cpu_spec->cpu_name, "e5500")
strcmp(cur_cpu_spec->cpu_name, "e6500")--- arch/powerpc/kvm/powerpc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/powerpc/kvm/powerpc.c +2 -2
@@ -444,12 +444,12 @@ int kvm_arch_hardware_enable(void)int kvm_arch_hardware_setup (void *opaque) { -return 0 ; +return kvmppc_core_check_processor_compat (); }int kvm_arch_check_processor_compat (void ) { -return kvmppc_core_check_processor_compat (); +return 0 ; }int kvm_arch_init_vm (struct kvm *kvm,unsigned long type)
Signature · 1 line
2.25.1
PATCH v5 14/30 KVM: Add arch hook for cpu online event
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Factor out the logic on cpu hotplug event as arch callback. Later kvm/x86 will override it.
--- include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 58 +++++++++++++++++++++++----------------- 2 files changed, 34 insertions(+), 25 deletions(-)
include/linux/kvm_host.h +1 -0
@@ -1434,6 +1434,7 @@ void kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu, struct dentry *debugfs_static inline void kvm_create_vcpu_debugfs (struct kvm_vcpu *vcpu) {}#endif +int kvm_arch_online_cpu (unsigned int cpu,int usage_count);int kvm_arch_reboot (int val);int kvm_arch_suspend (int usage_count);void kvm_arch_resume (int usage_count);
virt/kvm/kvm_main.c +33 -25
71 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v5 16/30 KVM: Remove on_each_cpu(hardware_disable_nolock) in kvm_exit()
In reply to [email protected]From: Isaku Yamahata <[email protected]>
hardware_enable/disable_nolock() check if the hardware is already enabled/disabled and work as nop when they are called multiple times.
When VM is created/destroyed, on_each_cpu(hardware_enable/disable_nolock) via kvm_arch_add/del_vm() and module_get/put() are called. It means when kvm module is removed, it's guaranteed that there is no vm and that hardware_disable_nolock() was called on each cpus.
Although the module exit function, kvm_exit(), calls on_each_cpu(hardware_disable_nolock), it's essentially nop. Eliminate nop call in kvm_exit().
--- virt/kvm/kvm_main.c | 1 - 1 file changed, 1 deletion(-)
virt/kvm/kvm_main.c +0 -1
@@ -6034,7 +6034,6 @@ void kvm_exit(void)unregister_syscore_ops (&kvm_syscore_ops);unregister_reboot_notifier (&kvm_reboot_notifier);cpuhp_remove_state_nocalls (CPUHP_AP_KVM_ONLINE ); -on_each_cpu (hardware_disable_nolock,NULL ,1 );kvm_arch_hardware_unsetup ();kvm_arch_exit ();kvm_irqfd_exit ();
Signature · 1 line
2.25.1
PATCH v5 15/30 KVM: Add arch hook for cpu offline event
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Factor out the logic on cpu offline event as arch callback. Later kvm/x86 will override it.
--- include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 30 +++++++++++++++++++----------- 2 files changed, 20 insertions(+), 11 deletions(-)
include/linux/kvm_host.h +1 -0
@@ -1435,6 +1435,7 @@ static inline void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu) {}#endif int kvm_arch_online_cpu (unsigned int cpu,int usage_count); +int kvm_arch_offline_cpu (unsigned int cpu,int usage_count);int kvm_arch_reboot (int val);int kvm_arch_suspend (int usage_count);void kvm_arch_resume (int usage_count);
virt/kvm/kvm_main.c +19 -11
@@ -1174,6 +1174,21 @@ int __weak kvm_arch_online_cpu(unsigned int cpu, int usage_count)return ret; } +int __weak kvm_arch_offline_cpu (unsigned int cpu,int usage_count) +{ +if (usage_count) { +/* +* arch callback kvm_arch_hardware_disable() assumes that +* preemption is disabled for historical reason. Disable +* preemption until all arch callbacks are fixed. +*/ +preempt_disable (); +hardware_disable_nolock (NULL ); +preempt_enable (); + } +return 0 ; +} +int __weak kvm_arch_reboot (int val) {on_each_cpu (hardware_disable_nolock,NULL ,1 );
@@ -5137,19 +5152,12 @@ static void hardware_disable_nolock(void *junk)static int kvm_offline_cpu (unsigned int cpu) { +int ret; +mutex_lock (&kvm_lock); -if (kvm_usage_count) { -/* -* arch callback kvm_arch_hardware_disable() assumes that -* preemption is disabled for historical reason. Disable -* preemption until all arch callbacks are fixed. -*/ -preempt_disable (); -hardware_disable_nolock (NULL ); -preempt_enable (); - } + ret =kvm_arch_offline_cpu (cpu, kvm_usage_count);mutex_unlock (&kvm_lock); -return 0 ; +return ret; }static void kvm_del_vm (void )
Signature · 1 line
2.25.1
PATCH v5 17/30 KVM: Move out KVM arch PM hooks and hardware enable/disable logic
In reply to [email protected]From: Isaku Yamahata <[email protected]>
To make clear that those files are default implementation and that KVM/x86 (and other KVM arch in future) will override them, split out those into a dedicated file. Once conversions for all kvm archs are done, the file will be deleted.
--- virt/kvm/Makefile.kvm | 2 +- virt/kvm/kvm_arch.c | 164 ++++++++++++++++++++++++++++++++++++++++++ virt/kvm/kvm_main.c | 160 ----------------------------------------- 3 files changed, 165 insertions(+), 161 deletions(-) create mode 100644 virt/kvm/kvm_arch.c
virt/kvm/Makefile.kvm +1 -1
@@ -5,7 +5,7 @@ KVM ?= ../../../virt/kvm -kvm-y := $(KVM)/kvm_main.o $(KVM)/eventfd.o $(KVM)/binary_stats.o +kvm-y := $(KVM)/kvm_main.o $(KVM)/eventfd.o $(KVM)/binary_stats.o $(KVM)/kvm_arch.o kvm-$(CONFIG_KVM_VFIO) += $(KVM)/vfio.o kvm-$(CONFIG_KVM_MMIO) += $(KVM)/coalesced_mmio.o kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o
virt/kvm/kvm_arch.c new file +164 -0
164 lines not on this page · raw message
virt/kvm/kvm_main.c +0 -160
209 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v5 18/30 KVM: kvm_arch.c: Remove _nolock post fix
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Now all related callbacks are called under kvm_lock, no point for _nolock post fix. Remove _nolock post fix for readability with shorter function names.
--- virt/kvm/kvm_arch.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
virt/kvm/kvm_arch.c +10 -10
@@ -24,7 +24,7 @@ int __weak kvm_arch_post_init_vm(struct kvm *kvm)return 0 ; } -static void hardware_enable_nolock (void *junk) +static void hardware_enable (void *junk) {int cpu =raw_smp_processor_id ();int r;
@@ -46,7 +46,7 @@ static void hardware_enable_nolock(void *junk) } } -static void hardware_disable_nolock (void *junk) +static void hardware_disable (void *junk) {int cpu =raw_smp_processor_id ();
@@ -70,7 +70,7 @@ int __weak kvm_arch_add_vm(struct kvm *kvm, int usage_count)return 0 ;atomic_set (&hardware_enable_failed,0 ); -on_each_cpu (hardware_enable_nolock,NULL ,1 ); +on_each_cpu (hardware_enable,NULL ,1 );if (atomic_read (&hardware_enable_failed)) { r = -EBUSY ;
@@ -80,7 +80,7 @@ int __weak kvm_arch_add_vm(struct kvm *kvm, int usage_count) r =kvm_arch_post_init_vm (kvm); err:if (r) -on_each_cpu (hardware_disable_nolock,NULL ,1 ); +on_each_cpu (hardware_disable,NULL ,1 );return r; }
@@ -89,7 +89,7 @@ int __weak kvm_arch_del_vm(int usage_count)if (usage_count)return 0 ; -on_each_cpu (hardware_disable_nolock,NULL ,1 ); +on_each_cpu (hardware_disable,NULL ,1 );return 0 ; }
@@ -115,7 +115,7 @@ int __weak kvm_arch_online_cpu(unsigned int cpu, int usage_count)* preemption until all arch callbacks are fixed. */ preempt_disable (); -hardware_enable_nolock (NULL ); +hardware_enable (NULL );preempt_enable ();if (atomic_read (&hardware_enable_failed)) {atomic_set (&hardware_enable_failed,0 );
@@ -134,7 +134,7 @@ int __weak kvm_arch_offline_cpu(unsigned int cpu, int usage_count)* preemption until all arch callbacks are fixed. */ preempt_disable (); -hardware_disable_nolock (NULL ); +hardware_disable (NULL );preempt_enable (); }return 0 ;
@@ -142,7 +142,7 @@ int __weak kvm_arch_offline_cpu(unsigned int cpu, int usage_count)int __weak kvm_arch_reboot (int val) { -on_each_cpu (hardware_disable_nolock,NULL ,1 ); +on_each_cpu (hardware_disable,NULL ,1 );return NOTIFY_OK ; }
@@ -153,12 +153,12 @@ int __weak kvm_arch_suspend(int usage_count)* Because kvm_suspend() is called with interrupt disabled, no * need to disable preemption. */ -hardware_disable_nolock (NULL ); +hardware_disable (NULL );return 0 ; }void __weak kvm_arch_resume (int usage_count) {if (usage_count) -hardware_enable_nolock (NULL ); +hardware_enable (NULL ); }
Signature · 1 line
2.25.1
PATCH v5 19/30 KVM: kvm_arch.c: Remove a global variable, hardware_enable_failed
In reply to [email protected]From: Isaku Yamahata <[email protected]>
A global variable hardware_enable_failed in kvm_arch.c is used only by kvm_arch_add_vm() and hardware_enable(). It doesn't have to be a global variable. Make it function local.
--- virt/kvm/kvm_arch.c | 61 ++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 31 deletions(-)
virt/kvm/kvm_arch.c +30 -31
@@ -13,7 +13,6 @@#include <linux/kvm_host.h> static cpumask_t cpus_hardware_enabled =CPU_MASK_NONE ; -static atomic_t hardware_enable_failed;/* * Called after the VM is otherwise initialized, but just before adding it to
@@ -24,7 +23,7 @@ int __weak kvm_arch_post_init_vm(struct kvm *kvm)return 0 ; } -static void hardware_enable (void *junk) +static int __hardware_enable (void ) {int cpu =raw_smp_processor_id ();int r;
@@ -32,18 +31,22 @@ static void hardware_enable(void *junk)WARN_ON_ONCE (preemptible ());if (cpumask_test_cpu (cpu, &cpus_hardware_enabled)) -return ; - -cpumask_set_cpu (cpu, &cpus_hardware_enabled); - +return 0 ; r =kvm_arch_hardware_enable (); - -if (r) { -cpumask_clear_cpu (cpu, &cpus_hardware_enabled); -atomic_inc (&hardware_enable_failed); +if (r)pr_warn ("kvm: enabling virtualization on CPU%d failed during %pSb\n" , cpu,__builtin_return_address (0 )); - } +else +cpumask_set_cpu (cpu, &cpus_hardware_enabled); +return r; +} + +static void hardware_enable (void *arg) +{ +atomic_t *failed = arg; + +if (__hardware_enable ()) +atomic_inc (failed); }static void hardware_disable (void *junk)
@@ -64,15 +67,15 @@ static void hardware_disable(void *junk)*/ int __weak kvm_arch_add_vm (struct kvm *kvm,int usage_count) { +atomic_t failed =ATOMIC_INIT (0 );int r =0 ;if (usage_count !=1 )return 0 ; -atomic_set (&hardware_enable_failed,0 ); -on_each_cpu (hardware_enable,NULL ,1 ); +on_each_cpu (hardware_enable, &failed,1 ); -if (atomic_read (&hardware_enable_failed)) { +if (atomic_read (&failed)) { r = -EBUSY ; goto err; }
@@ -95,33 +98,29 @@ int __weak kvm_arch_del_vm(int usage_count)int __weak kvm_arch_online_cpu (unsigned int cpu,int usage_count) { -int ret =0 ; +int ret; ret =kvm_arch_check_processor_compat ();if (ret)return ret; +if (!usage_count) +return 0 ; + +/* +* arch callback kvm_arch_hardware_enable() assumes that +* preemption is disabled for historical reason. Disable +* preemption until all arch callbacks are fixed. +*/ +preempt_disable ();/* * Abort the CPU online process if hardware virtualization cannot * be enabled. Otherwise running VMs would encounter unrecoverable * errors when scheduled to this CPU. */ -if (usage_count) { -WARN_ON_ONCE (atomic_read (&hardware_enable_failed)); + ret =__hardware_enable (); +preempt_enable (); -/* -* arch callback kvm_arch_hardware_eanble() assumes that -* preemption is disabled for historical reason. Disable -* preemption until all arch callbacks are fixed. -*/ -preempt_disable (); -hardware_enable (NULL ); -preempt_enable (); -if (atomic_read (&hardware_enable_failed)) { -atomic_set (&hardware_enable_failed,0 ); - ret = -EIO ; - } - }return ret; }
@@ -160,5 +159,5 @@ int __weak kvm_arch_suspend(int usage_count)void __weak kvm_arch_resume (int usage_count) {if (usage_count) -hardware_enable (NULL ); + (void )__hardware_enable (); }
Signature · 1 line
2.25.1
PATCH v5 20/30 KVM: Introduce an arch wrapper to check all processor compatibility
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Introduce an arch wrapper to check all processor compatibility and define default implementation as weak symbol to keep the current logic.
The hardware feature compatibility check is arch dependent, only x86 KVM does cpu feature check on all processors. It doesn't make much sense to enforce the current implementation to invoke check function on each processors. Introduce a arch callback, kvm_arch_check_processor_compat_all(), so that arch code can override it.
Eventually feature check should be pushed down into arch callback, (kvm_arch_hardware_setup(), kvm_arch_online_cpu(), and kvm_arch_resume()), the two compatibility check, kvm_arch_check_processor_compat{,_all}(), will be eliminated. This is a transitional step for it.
--- include/linux/kvm_host.h | 1 + virt/kvm/kvm_arch.c | 18 ++++++++++++++++++ virt/kvm/kvm_main.c | 13 +++---------- 3 files changed, 22 insertions(+), 10 deletions(-)
include/linux/kvm_host.h +1 -0
@@ -1445,6 +1445,7 @@ void kvm_arch_hardware_disable(void);int kvm_arch_hardware_setup (void *opaque);void kvm_arch_hardware_unsetup (void );int kvm_arch_check_processor_compat (void ); +int kvm_arch_check_processor_compat_all (void );int kvm_arch_vcpu_runnable (struct kvm_vcpu *vcpu);bool kvm_arch_vcpu_in_kernel (struct kvm_vcpu *vcpu);int kvm_arch_vcpu_should_kick (struct kvm_vcpu *vcpu);
virt/kvm/kvm_arch.c +18 -0
@@ -96,6 +96,24 @@ int __weak kvm_arch_del_vm(int usage_count)return 0 ; } +static void check_processor_compat (void *rtn) +{ + *(int *)rtn =kvm_arch_check_processor_compat (); +} + +int __weak kvm_arch_check_processor_compat_all (void ) +{ +int cpu; +int r; + +for_each_online_cpu (cpu) { +smp_call_function_single (cpu, check_processor_compat, &r,1 ); +if (r <0 ) +return r; + } +return 0 ; +} +int __weak kvm_arch_online_cpu (unsigned int cpu,int usage_count) {int ret;
virt/kvm/kvm_main.c +3 -10
@@ -5752,11 +5752,6 @@ void kvm_unregister_perf_callbacks(void) }#endif -static void check_processor_compat (void *rtn) -{ - *(int *)rtn =kvm_arch_check_processor_compat (); -} -int kvm_init (void *opaque,unsigned vcpu_size,unsigned vcpu_align,struct module *module) {
@@ -5782,11 +5777,9 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,if (r <0 ) goto out_free_1; -for_each_online_cpu (cpu) { -smp_call_function_single (cpu, check_processor_compat, &r,1 ); -if (r <0 ) - goto out_free_2; - } + r =kvm_arch_check_processor_compat_all (); +if (r <0 ) + goto out_free_2; r =cpuhp_setup_state_nocalls (CPUHP_AP_KVM_ONLINE ,"kvm/cpu:online" , kvm_online_cpu, kvm_offline_cpu);
Signature · 1 line
2.25.1
PATCH v5 21/30 KVM: x86: Duplicate arch callbacks related to pm events and compat check
In reply to [email protected]From: Isaku Yamahata <[email protected]>
KVM/X86 can change those callbacks without worrying about breaking other archs.
--- arch/x86/kvm/x86.c | 166 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 161 insertions(+), 5 deletions(-)
arch/x86/kvm/x86.c +161 -5
@@ -11960,6 +11960,167 @@ void kvm_arch_hardware_disable(void)drop_user_return_notifiers (); } +static cpumask_t cpus_hardware_enabled =CPU_MASK_NONE ; + +int kvm_arch_post_init_vm (struct kvm *kvm) +{ +return kvm_mmu_post_init_vm (kvm); +} + +static int __hardware_enable (void ) +{ +int cpu =raw_smp_processor_id (); +int r; + +WARN_ON_ONCE (preemptible ()); + +if (cpumask_test_cpu (cpu, &cpus_hardware_enabled)) +return 0 ; + r =kvm_arch_hardware_enable (); +if (r) +pr_warn ("kvm: enabling virtualization on CPU%d failed during %pSb\n" , + cpu,__builtin_return_address (0 )); +else +cpumask_set_cpu (cpu, &cpus_hardware_enabled); +return r; +} + +static void hardware_enable (void *arg) +{ +atomic_t *failed = arg; + +if (__hardware_enable ()) +atomic_inc (failed); +} + +static void hardware_disable (void *junk) +{ +int cpu =raw_smp_processor_id (); + +WARN_ON_ONCE (preemptible ()); + +if (!cpumask_test_cpu (cpu, &cpus_hardware_enabled)) +return ; +cpumask_clear_cpu (cpu, &cpus_hardware_enabled); +kvm_arch_hardware_disable (); +} + +/* +* Called after the VM is otherwise initialized, but just before adding it to +* the vm_list. +*/ +int kvm_arch_add_vm (struct kvm *kvm,int usage_count) +{ +atomic_t failed; +int r =0 ; + +if (usage_count !=1 ) +return 0 ; + +atomic_set (&failed,0 ); +on_each_cpu (hardware_enable, &failed,1 ); + +if (atomic_read (&failed)) { + r = -EBUSY ; + goto err; + } + + r =kvm_arch_post_init_vm (kvm); +err: +if (r) +on_each_cpu (hardware_disable,NULL ,1 ); +return r; +} + +int kvm_arch_del_vm (int usage_count) +{ +if (usage_count) +return 0 ; + +on_each_cpu (hardware_disable,NULL ,1 ); +return 0 ; +} + +static void check_processor_compat (void *rtn) +{ + *(int *)rtn =kvm_arch_check_processor_compat (); +} + +int kvm_arch_check_processor_compat_all (void ) +{ +int cpu; +int r; + +for_each_online_cpu (cpu) { +smp_call_function_single (cpu, check_processor_compat, &r,1 ); +if (r <0 ) +return r; + } +return 0 ; +} + +int kvm_arch_online_cpu (unsigned int cpu,int usage_count) +{ +int ret; + + ret =kvm_arch_check_processor_compat (); +if (ret) +return ret; + +if (!usage_count) +return 0 ; + +/* +* arch callback kvm_arch_hardware_eanble() assumes that +* preemption is disabled for historical reason. Disable +* preemption until all arch callbacks are fixed. +*/ +preempt_disable (); +/* +* Abort the CPU online process if hardware virtualization cannot +* be enabled. Otherwise running VMs would encounter unrecoverable +* errors when scheduled to this CPU. +*/ + ret =__hardware_enable (); +preempt_enable (); + +return ret; +} + +int kvm_arch_offline_cpu (unsigned int cpu,int usage_count) +{ +if (usage_count) { +/* +* arch callback kvm_arch_hardware_disable() assumes that +* preemption is disabled for historical reason. Disable +* preemption until all arch callbacks are fixed. +*/ +preempt_disable (); +hardware_disable (NULL ); +preempt_enable (); + } +return 0 ; +} + +int kvm_arch_reboot (int val) +{ +on_each_cpu (hardware_disable,NULL ,1 ); +return NOTIFY_OK ; +} + +int kvm_arch_suspend (int usage_count) +{ +if (usage_count) +hardware_disable (NULL ); +return 0 ; +} + +void kvm_arch_resume (int usage_count) +{ +if (usage_count) + (void )__hardware_enable (); +} +static inline void kvm_ops_update (struct kvm_x86_init_ops *ops) {memcpy (&kvm_x86_ops, ops->runtime_ops,sizeof (kvm_x86_ops));
@@ -12140,11 +12301,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)return ret; } -int kvm_arch_post_init_vm (struct kvm *kvm) -{ -return kvm_mmu_post_init_vm (kvm); -} -static void kvm_unload_vcpu_mmu (struct kvm_vcpu *vcpu) {vcpu_load (vcpu);
Signature · 1 line
2.25.1
PATCH v5 23/30 KVM: Eliminate kvm_arch_post_init_vm()
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Now kvm_arch_post_init_vm() is used only by x86 kvm_arch_add_vm(). Other arch doesn't define it. Merge x86 kvm_arch_post_init_vm() into x86 kvm_arch_add_vm() and eliminate kvm_arch_post_init_vm().
--- arch/x86/kvm/x86.c | 7 +------ include/linux/kvm_host.h | 1 - virt/kvm/kvm_arch.c | 16 +--------------- 3 files changed, 2 insertions(+), 22 deletions(-)
arch/x86/kvm/x86.c +1 -6
@@ -11969,11 +11969,6 @@ void kvm_arch_hardware_disable(void)static cpumask_t cpus_hardware_enabled =CPU_MASK_NONE ; -int kvm_arch_post_init_vm (struct kvm *kvm) -{ -return kvm_mmu_post_init_vm (kvm); -} -static int __hardware_enable (void ) {int cpu =raw_smp_processor_id ();
@@ -12032,7 +12027,7 @@ int kvm_arch_add_vm(struct kvm *kvm, int usage_count) goto err; } - r =kvm_arch_post_init_vm (kvm); + r =kvm_mmu_post_init_vm (kvm); err:if (r)on_each_cpu (hardware_disable,NULL ,1 );
include/linux/kvm_host.h +0 -1
@@ -1451,7 +1451,6 @@ bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu);int kvm_arch_vcpu_should_kick (struct kvm_vcpu *vcpu);bool kvm_arch_dy_runnable (struct kvm_vcpu *vcpu);bool kvm_arch_dy_has_pending_interrupt (struct kvm_vcpu *vcpu); -int kvm_arch_post_init_vm (struct kvm *kvm);int kvm_arch_add_vm (struct kvm *kvm,int usage_count);int kvm_arch_del_vm (int usage_count);void kvm_arch_pre_destroy_vm (struct kvm *kvm);
virt/kvm/kvm_arch.c +1 -15
@@ -14,15 +14,6 @@static cpumask_t cpus_hardware_enabled =CPU_MASK_NONE ; -/* -* Called after the VM is otherwise initialized, but just before adding it to -* the vm_list. -*/ -int __weak kvm_arch_post_init_vm (struct kvm *kvm) -{ -return 0 ; -} -static int __hardware_enable (void ) {int cpu =raw_smp_processor_id ();
@@ -77,13 +68,8 @@ int __weak kvm_arch_add_vm(struct kvm *kvm, int usage_count)if (atomic_read (&failed)) { r = -EBUSY ; - goto err; - } - - r =kvm_arch_post_init_vm (kvm); -err: -if (r)on_each_cpu (hardware_disable,NULL ,1 ); + }return r; }
Signature · 1 line
2.25.1
PATCH v5 22/30 KVM: x86: Move TSC fixup logic to KVM arch resume callback
In reply to [email protected]From: Isaku Yamahata <[email protected]>
commit 0dd6a6edb012 ("KVM: Dont mark TSC unstable due to S4 suspend") made use of kvm_arch_hardware_enable() callback to detect that TSC goes backward due to S4 suspend. It has to check it only when resuming from S4. Not every time virtualization hardware ennoblement. Move the logic to kvm_arch_resume() callback.
--- arch/x86/kvm/x86.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-)
arch/x86/kvm/x86.c +12 -11
@@ -11865,18 +11865,26 @@ void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)EXPORT_SYMBOL_GPL (kvm_vcpu_deliver_sipi_vector);int kvm_arch_hardware_enable (void ) +{ +return static_call (kvm_x86_hardware_enable)(); +} + +static int __hardware_enable (void ); + +void kvm_arch_resume (int usage_count) {struct kvm *kvm;struct kvm_vcpu *vcpu;unsigned long i; -int ret;u64 local_tsc;u64 max_tsc =0 ;bool stable, backwards_tsc = false; - ret =static_call (kvm_x86_hardware_enable)(); -if (ret !=0 ) -return ret; +if (!usage_count) +return ; + +if (__hardware_enable ()) +return ; local_tsc =rdtsc (); stable = !kvm_check_tsc_unstable ();
@@ -11951,7 +11959,6 @@ int kvm_arch_hardware_enable(void) } } -return 0 ; }void kvm_arch_hardware_disable (void )
@@ -12115,12 +12122,6 @@ int kvm_arch_suspend(int usage_count)return 0 ; } -void kvm_arch_resume (int usage_count) -{ -if (usage_count) - (void )__hardware_enable (); -} -static inline void kvm_ops_update (struct kvm_x86_init_ops *ops) {memcpy (&kvm_x86_ops, ops->runtime_ops,sizeof (kvm_x86_ops));
Signature · 1 line
2.25.1
PATCH v5 24/30 KVM: Add config to not compile kvm_arch.c
In reply to [email protected]From: Isaku Yamahata <[email protected]>
So that kvm_arch_hardware_enable/disable() aren't required.
Once the conversion of all KVM archs is done, this config and kvm_arch.c should be removed.
--- arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/x86.c | 4 ++-- include/linux/kvm_host.h | 3 +++ virt/kvm/Kconfig | 3 +++ virt/kvm/Makefile.kvm | 5 ++++- 5 files changed, 13 insertions(+), 3 deletions(-)
arch/x86/kvm/Kconfig +1 -0
@@ -25,6 +25,7 @@ config KVM depends on X86_LOCAL_APIC select PREEMPT_NOTIFIERS select MMU_NOTIFIER + select HAVE_KVM_OVERRIDE_HARDWARE_ENABLE select HAVE_KVM_IRQCHIP select HAVE_KVM_PFNCACHE select HAVE_KVM_IRQFD
arch/x86/kvm/x86.c +2 -2
@@ -11864,7 +11864,7 @@ void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) }EXPORT_SYMBOL_GPL (kvm_vcpu_deliver_sipi_vector); -int kvm_arch_hardware_enable (void ) +static int kvm_arch_hardware_enable (void ) {return static_call (kvm_x86_hardware_enable)(); }
@@ -11961,7 +11961,7 @@ void kvm_arch_resume(int usage_count) } } -void kvm_arch_hardware_disable (void ) +static void kvm_arch_hardware_disable (void ) {static_call (kvm_x86_hardware_disable)();drop_user_return_notifiers ();
include/linux/kvm_host.h +3 -0
@@ -1440,8 +1440,11 @@ int kvm_arch_reboot(int val);int kvm_arch_suspend (int usage_count);void kvm_arch_resume (int usage_count); +#ifndef CONFIG_HAVE_KVM_OVERRIDE_HARDWARE_ENABLE int kvm_arch_hardware_enable (void );void kvm_arch_hardware_disable (void ); +#endif +int kvm_arch_hardware_setup (void *opaque);void kvm_arch_hardware_unsetup (void );int kvm_arch_check_processor_compat (void );
virt/kvm/Kconfig +3 -0
@@ -72,3 +72,6 @@ config KVM_XFER_TO_GUEST_WORK config HAVE_KVM_PM_NOTIFIER bool + +config HAVE_KVM_OVERRIDE_HARDWARE_ENABLE + def_bool n
virt/kvm/Makefile.kvm +4 -1
@@ -5,7 +5,10 @@ KVM ?= ../../../virt/kvm -kvm-y := $(KVM)/kvm_main.o $(KVM)/eventfd.o $(KVM)/binary_stats.o $(KVM)/kvm_arch.o +kvm-y := $(KVM)/kvm_main.o $(KVM)/eventfd.o $(KVM)/binary_stats.o +ifneq ($(CONFIG_HAVE_KVM_OVERRIDE_HARDWARE_ENABLE), y) +kvm-y += $(KVM)/kvm_arch.o +endif kvm-$(CONFIG_KVM_VFIO) += $(KVM)/vfio.o kvm-$(CONFIG_KVM_MMIO) += $(KVM)/coalesced_mmio.o kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o
Signature · 1 line
2.25.1
PATCH v5 26/30 KVM: x86: Make x86 processor compat check callback empty
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Move processor compatibility check on all processors into kvm_arch_hardware_setup() and make kvm_arch_check_processor_compat{,_all}() empty. This is a preparation step to eliminate them.
--- arch/x86/kvm/x86.c | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-)
arch/x86/kvm/x86.c +29 -16
@@ -129,6 +129,8 @@ static int kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu);static int __set_sregs2 (struct kvm_vcpu *vcpu,struct kvm_sregs2 *sregs2);static void __get_sregs2 (struct kvm_vcpu *vcpu,struct kvm_sregs2 *sregs2); +static int kvm_check_processor_compatibility (void ); +struct kvm_x86_ops kvm_x86_ops__read_mostly ;#define KVM_X86_OP (func) \
@@ -12033,21 +12035,8 @@ int kvm_arch_del_vm(int usage_count)return 0 ; } -static void check_processor_compat (void *rtn) -{ - *(int *)rtn =kvm_arch_check_processor_compat (); -} -int kvm_arch_check_processor_compat_all (void ) { -int cpu; -int r; - -for_each_online_cpu (cpu) { -smp_call_function_single (cpu, check_processor_compat, &r,1 ); -if (r <0 ) -return r; - }return 0 ; }
@@ -12055,7 +12044,7 @@ int kvm_arch_online_cpu(unsigned int cpu, int usage_count) {int ret; - ret =kvm_arch_check_processor_compat (); + ret =kvm_check_processor_compatibility ();if (ret)return ret;
@@ -12125,6 +12114,24 @@ static inline void kvm_ops_update(struct kvm_x86_init_ops *ops)kvm_pmu_ops_update (ops->pmu_ops); } +static void check_processor_compat (void *rtn) +{ + *(int *)rtn =kvm_check_processor_compatibility (); +} + +static int kvm_check_processor_compatibility_all (void ) +{ +int cpu; +int r; + +for_each_online_cpu (cpu) { +smp_call_function_single (cpu, check_processor_compat, &r,1 ); +if (r <0 ) +return r; + } +return 0 ; +} +int kvm_arch_hardware_setup (void *opaque) {struct kvm_x86_init_ops *ops = opaque;
@@ -12165,7 +12172,8 @@ int kvm_arch_hardware_setup(void *opaque) } kvm_caps.default_tsc_scaling_ratio =1ULL << kvm_caps.tsc_scaling_ratio_frac_bits;kvm_init_msr_list (); -return 0 ; + +return kvm_check_processor_compatibility_all (); }void kvm_arch_hardware_unsetup (void )
@@ -12175,7 +12183,7 @@ void kvm_arch_hardware_unsetup(void)static_call (kvm_x86_hardware_unsetup)(); } -int kvm_arch_check_processor_compat (void ) +static int kvm_check_processor_compatibility (void ) {int cpu =smp_processor_id ();struct cpuinfo_x86 *c = &cpu_data (cpu);
@@ -12195,6 +12203,11 @@ int kvm_arch_check_processor_compat(void)return static_call (kvm_x86_check_processor_compatibility)(); } +int kvm_arch_check_processor_compat (void ) +{ +return 0 ; +} +bool kvm_vcpu_is_reset_bsp (struct kvm_vcpu *vcpu) {return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id;
Signature · 1 line
2.25.1
PATCH v5 25/30 KVM: x86: Delete kvm_arch_hardware_enable/disable()
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Now they're function call and there is no point to keep them. Open code them.
--- arch/x86/kvm/x86.c | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-)
arch/x86/kvm/x86.c +4 -14
@@ -354,7 +354,7 @@ static void kvm_on_user_return(struct user_return_notifier *urn)/* * Disabling irqs at this point since the following code could be -* interrupted and executed through kvm_arch_hardware_disable() +* interrupted and executed through hardware_disable() */ local_irq_save (flags);if (msrs->registered) {
@@ -11864,11 +11864,6 @@ void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) }EXPORT_SYMBOL_GPL (kvm_vcpu_deliver_sipi_vector); -static int kvm_arch_hardware_enable (void ) -{ -return static_call (kvm_x86_hardware_enable)(); -} -static int __hardware_enable (void );void kvm_arch_resume (int usage_count)
@@ -11961,12 +11956,6 @@ void kvm_arch_resume(int usage_count) } } -static void kvm_arch_hardware_disable (void ) -{ -static_call (kvm_x86_hardware_disable)(); -drop_user_return_notifiers (); -} -static cpumask_t cpus_hardware_enabled =CPU_MASK_NONE ;static int __hardware_enable (void )
@@ -11978,7 +11967,7 @@ static int __hardware_enable(void)if (cpumask_test_cpu (cpu, &cpus_hardware_enabled))return 0 ; - r =kvm_arch_hardware_enable (); + r =static_call (kvm_x86_hardware_enable)();if (r)pr_warn ("kvm: enabling virtualization on CPU%d failed during %pSb\n" , cpu,__builtin_return_address (0 ));
@@ -12004,7 +11993,8 @@ static void hardware_disable(void *junk)if (!cpumask_test_cpu (cpu, &cpus_hardware_enabled))return ;cpumask_clear_cpu (cpu, &cpus_hardware_enabled); -kvm_arch_hardware_disable (); +static_call (kvm_x86_hardware_disable)(); +drop_user_return_notifiers (); }/*
Signature · 1 line
2.25.1
PATCH v5 28/30 KVM: Eliminate kvm_arch_check_processor_compat()
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Now all arch has "return 0" implementation. Eliminate it. If feature compatibility check is needed, it should be done in kvm_arch_hardware_setup(), kvm_arch_online_cpu(), and kvm_arch_resume().
--- arch/arm64/kvm/arm.c | 5 ----- arch/mips/kvm/mips.c | 5 ----- arch/powerpc/kvm/powerpc.c | 5 ----- arch/riscv/kvm/main.c | 5 ----- arch/s390/kvm/kvm-s390.c | 5 ----- arch/x86/kvm/x86.c | 10 ---------- include/linux/kvm_host.h | 2 -- virt/kvm/kvm_arch.c | 22 ---------------------- virt/kvm/kvm_main.c | 4 ---- 9 files changed, 63 deletions(-)
arch/arm64/kvm/arm.c +0 -5
@@ -68,11 +68,6 @@ int kvm_arch_hardware_setup(void *opaque)return 0 ; } -int kvm_arch_check_processor_compat (void ) -{ -return 0 ; -} -int kvm_vm_ioctl_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) {
arch/mips/kvm/mips.c +0 -5
@@ -140,11 +140,6 @@ int kvm_arch_hardware_setup(void *opaque)return 0 ; } -int kvm_arch_check_processor_compat (void ) -{ -return 0 ; -} -extern void kvm_init_loongson_ipi (struct kvm *kvm);int kvm_arch_init_vm (struct kvm *kvm,unsigned long type)
arch/powerpc/kvm/powerpc.c +0 -5
@@ -447,11 +447,6 @@ int kvm_arch_hardware_setup(void *opaque)return kvmppc_core_check_processor_compat (); } -int kvm_arch_check_processor_compat (void ) -{ -return 0 ; -} -int kvm_arch_init_vm (struct kvm *kvm,unsigned long type) {struct kvmppc_ops *kvm_ops =NULL ;
arch/riscv/kvm/main.c +0 -5
@@ -20,11 +20,6 @@ long kvm_arch_dev_ioctl(struct file *filp,return -EINVAL ; } -int kvm_arch_check_processor_compat (void ) -{ -return 0 ; -} -int kvm_arch_hardware_setup (void *opaque) {return 0 ;
arch/s390/kvm/kvm-s390.c +0 -5
@@ -254,11 +254,6 @@ int kvm_arch_hardware_enable(void)return 0 ; } -int kvm_arch_check_processor_compat (void ) -{ -return 0 ; -} -/* forward declarations */ static void kvm_gmap_notifier (struct gmap *gmap,unsigned long start,unsigned long end);
arch/x86/kvm/x86.c +0 -10
@@ -12035,11 +12035,6 @@ int kvm_arch_del_vm(int usage_count)return 0 ; } -int kvm_arch_check_processor_compat_all (void ) -{ -return 0 ; -} -int kvm_arch_online_cpu (unsigned int cpu,int usage_count) {int ret;
@@ -12203,11 +12198,6 @@ static int kvm_check_processor_compatibility(void)return static_call (kvm_x86_check_processor_compatibility)(); } -int kvm_arch_check_processor_compat (void ) -{ -return 0 ; -} -bool kvm_vcpu_is_reset_bsp (struct kvm_vcpu *vcpu) {return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id;
include/linux/kvm_host.h +0 -2
@@ -1447,8 +1447,6 @@ void kvm_arch_hardware_disable(void);int kvm_arch_hardware_setup (void *opaque);void kvm_arch_hardware_unsetup (void ); -int kvm_arch_check_processor_compat (void ); -int kvm_arch_check_processor_compat_all (void );int kvm_arch_vcpu_runnable (struct kvm_vcpu *vcpu);bool kvm_arch_vcpu_in_kernel (struct kvm_vcpu *vcpu);int kvm_arch_vcpu_should_kick (struct kvm_vcpu *vcpu);
virt/kvm/kvm_arch.c +0 -22
@@ -82,32 +82,10 @@ int __weak kvm_arch_del_vm(int usage_count)return 0 ; } -static void check_processor_compat (void *rtn) -{ - *(int *)rtn =kvm_arch_check_processor_compat (); -} - -int __weak kvm_arch_check_processor_compat_all (void ) -{ -int cpu; -int r; - -for_each_online_cpu (cpu) { -smp_call_function_single (cpu, check_processor_compat, &r,1 ); -if (r <0 ) -return r; - } -return 0 ; -} -int __weak kvm_arch_online_cpu (unsigned int cpu,int usage_count) {int ret; - ret =kvm_arch_check_processor_compat (); -if (ret) -return ret; -if (!usage_count)return 0 ;
virt/kvm/kvm_main.c +0 -4
@@ -5777,10 +5777,6 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,if (r <0 ) goto out_free_1; - r =kvm_arch_check_processor_compat_all (); -if (r <0 ) - goto out_free_2; - r =cpuhp_setup_state_nocalls (CPUHP_AP_KVM_ONLINE ,"kvm/cpu:online" , kvm_online_cpu, kvm_offline_cpu);if (r)
Signature · 1 line
2.25.1
PATCH v5 29/30 RFC: KVM: x86: Remove cpus_hardware_enabled and related sanity check
In reply to [email protected]From: Isaku Yamahata <[email protected]>
cpus_hardware_enabled mask seems incomplete protection against other kernel component using CPU virtualization feature. Because it's obscure and incomplete, remove the check.
--- arch/x86/kvm/x86.c | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-)
arch/x86/kvm/x86.c +1 -14
@@ -11958,23 +11958,16 @@ void kvm_arch_resume(int usage_count) } } -static cpumask_t cpus_hardware_enabled =CPU_MASK_NONE ; -static int __hardware_enable (void ) { -int cpu =raw_smp_processor_id ();int r;WARN_ON_ONCE (preemptible ()); -if (cpumask_test_cpu (cpu, &cpus_hardware_enabled)) -return 0 ; r =static_call (kvm_x86_hardware_enable)();if (r)pr_warn ("kvm: enabling virtualization on CPU%d failed during %pSb\n" , - cpu,__builtin_return_address (0 )); -else -cpumask_set_cpu (cpu, &cpus_hardware_enabled); +smp_processor_id (),__builtin_return_address (0 ));return r; }
@@ -11988,13 +11981,7 @@ static void hardware_enable(void *arg)static void hardware_disable (void *junk) { -int cpu =raw_smp_processor_id (); -WARN_ON_ONCE (preemptible ()); - -if (!cpumask_test_cpu (cpu, &cpus_hardware_enabled)) -return ; -cpumask_clear_cpu (cpu, &cpus_hardware_enabled);static_call (kvm_x86_hardware_disable)();drop_user_return_notifiers (); }
Signature · 1 line
2.25.1
PATCH v5 30/30 RFC: KVM: Remove cpus_hardware_enabled and related sanity check
In reply to [email protected]From: Isaku Yamahata <[email protected]>
cpus_hardware_enabled mask seems incomplete protection against other kernel component using CPU virtualization feature. Because it's obscure and incomplete, remove the check.
--- virt/kvm/kvm_arch.c | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-)
virt/kvm/kvm_arch.c +1 -14
@@ -12,23 +12,16 @@#include <linux/kvm_host.h> -static cpumask_t cpus_hardware_enabled =CPU_MASK_NONE ; -static int __hardware_enable (void ) { -int cpu =raw_smp_processor_id ();int r;WARN_ON_ONCE (preemptible ()); -if (cpumask_test_cpu (cpu, &cpus_hardware_enabled)) -return 0 ; r =kvm_arch_hardware_enable ();if (r)pr_warn ("kvm: enabling virtualization on CPU%d failed during %pSb\n" , - cpu,__builtin_return_address (0 )); -else -cpumask_set_cpu (cpu, &cpus_hardware_enabled); +smp_processor_id (),__builtin_return_address (0 ));return r; }
@@ -42,13 +35,7 @@ static void hardware_enable(void *arg)static void hardware_disable (void *junk) { -int cpu =raw_smp_processor_id (); -WARN_ON_ONCE (preemptible ()); - -if (!cpumask_test_cpu (cpu, &cpus_hardware_enabled)) -return ; -cpumask_clear_cpu (cpu, &cpus_hardware_enabled);kvm_arch_hardware_disable (); }
Signature · 1 line
2.25.1
Re: [PATCH v5 27/30] RFC: KVM: powerpc: Move processor compatibility check to hardware setup
In reply to [email protected]Quoted 3 lines · [email protected] writes:
From: Isaku Yamahata <[email protected]>
Move processor compatibility check from
kvm_arch_processor_compat()into
^
kvm_arch_check_processor_compat()Quoted 3 lines
kvm_arch_hardware_setup(). The check does model name comparison with a global variable,cur_cpu_spec. There is no point to check it at run time on all processors.
A key detail I had to look up is that both kvm_arch_hardware_setup() and kvm_arch_check_processor_compat() are called from kvm_init(), one after the other. But the latter is called on each CPU.
And because the powerpc implementation of kvm_arch_check_processor_compat() just checks a global, there's no need to call it on every CPU.
Quoted 2 lines
kvmppc_core_check_processor_compat()checks the global variable. There are five implementation for it as follows.
There are three implementations not five.
Quoted 22 lines
arch/powerpc/include/asm/cputable.h: extern structcpu_spec*cur_cpu_spec;arch/powerpc/kvm/book3s.c: return 0
arch/powerpc/kvm/e500.c:strcmp(cur_cpu_spec->cpu_name, "e500v2")arch/powerpc/kvm/e500mc.c:strcmp(cur_cpu_spec->cpu_name, "e500mc")strcmp(cur_cpu_spec->cpu_name, "e5500") strcmp(cur_cpu_spec->cpu_name, "e6500")Suggested-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Cc Fabiano Rosas <[email protected]>--- arch/powerpc/kvm/powerpc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/powerpc/kvm/powerpc.c +0
-0@@ -444,12 +444,12 @@ int kvm_arch_hardware_enable(void)int kvm_arch_hardware_setup (void *opaque)
arch/powerpc/kvm/powerpc.c int kvm_arch_hardware_enable(void) · line 453
Quoted 10 lines
arch/powerpc/kvm/powerpc.c +2
-2{ -return 0 ; +return kvmppc_core_check_processor_compat (); }int kvm_arch_check_processor_compat (void ) { -return kvmppc_core_check_processor_compat (); +return 0 ; }
The actual change seems OK. I gave it a quick test boot and ran some VMs, everything seems to work as before.
cheers
Re: [PATCH v5 27/30] RFC: KVM: powerpc: Move processor compatibility check to hardware setup
Quoted 15 lines · Michael Ellerman
Quoted 3 lines · [email protected] writes:
From: Isaku Yamahata <[email protected]>
Move processor compatibility check from
kvm_arch_processor_compat()into^ kvm_arch_check_processor_compat()Quoted 3 lines
kvm_arch_hardware_setup(). The check does model name comparison with a global variable,cur_cpu_spec. There is no point to check it at run time on all processors.A key detail I had to look up is that both
kvm_arch_hardware_setup()andkvm_arch_check_processor_compat()are called fromkvm_init(), one after the other. But the latter is called on each CPU.
Quoted 7 lines
And because the powerpc implementation of
kvm_arch_check_processor_compat()just checks a global, there's no need to call it on every CPU.Quoted 2 lines
kvmppc_core_check_processor_compat()checks the global variable. There are five implementation for it as follows.There are three implementations not five.
Thanks. I'll update the commit message.
Quoted 36 lines
Quoted 22 lines
arch/powerpc/include/asm/cputable.h: extern structcpu_spec*cur_cpu_spec;arch/powerpc/kvm/book3s.c: return 0
arch/powerpc/kvm/e500.c:strcmp(cur_cpu_spec->cpu_name, "e500v2")arch/powerpc/kvm/e500mc.c:strcmp(cur_cpu_spec->cpu_name, "e500mc")strcmp(cur_cpu_spec->cpu_name, "e5500") strcmp(cur_cpu_spec->cpu_name, "e6500")Suggested-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Cc Fabiano Rosas <[email protected]>--- arch/powerpc/kvm/powerpc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/powerpc/kvm/powerpc.c +0
-0@@ -444,12 +444,12 @@ int kvm_arch_hardware_enable(void)int kvm_arch_hardware_setup (void *opaque)arch/powerpc/kvm/powerpc.c int kvm_arch_hardware_enable(void) · line 453
Quoted 10 lines
arch/powerpc/kvm/powerpc.c +2
-2{ -return 0 ; +return kvmppc_core_check_processor_compat (); }int kvm_arch_check_processor_compat (void ) { -return kvmppc_core_check_processor_compat (); +return 0 ; }The actual change seems OK. I gave it a quick test boot and ran some VMs, everything seems to work as before.
Quoted 1 line
Thanks so much for testing. I'll remove RFC.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v5 10/30] KVM: Add arch hooks when VM is added/deleted
In reply to [email protected]Quoted 47 lines · On Thu, Sep 22, 2022 at 11:20:39AM -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
and pass
kvm_usage_countwithkvm_lock. Movekvm_arch_post_init_vm()underkvm_arch_add_vm(). Replace enable/disable_hardware_all() with the default implementation of kvm_arch_add/del_vm(). Laterkvm_arch_post_init_vm()is deleted once x86 overrideskvm_arch_add_vm().Suggested-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- include/linux/kvm_host.h | 2 + virt/kvm/kvm_main.c | 121 ++++++++++++++++++++------------------- 2 files changed, 65 insertions(+), 58 deletions(-)include/linux/kvm_host.h +2
-0@@ -1445,6 +1445,8 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu);bool kvm_arch_dy_runnable (struct kvm_vcpu *vcpu);bool kvm_arch_dy_has_pending_interrupt (struct kvm_vcpu *vcpu);int kvm_arch_post_init_vm (struct kvm *kvm); +int kvm_arch_add_vm (struct kvm *kvm,int usage_count); +int kvm_arch_del_vm (int usage_count);void kvm_arch_pre_destroy_vm (struct kvm *kvm);int kvm_arch_create_vm_debugfs (struct kvm *kvm);virt/kvm/kvm_main.c +3
-2@@ -142,8 +142,9 @@ static int kvm_no_compat_open(struct inode *inode, struct file *file)#define KVM_COMPAT (c) .compat_ioctl = kvm_no_compat_ioctl, \ .open = kvm_no_compat_open#endif -static int hardware_enable_all (void ); -static void hardware_disable_all (void ); +static void hardware_enable_nolock (void *junk); +static void hardware_disable_nolock (void *junk); +static void kvm_del_vm (void );static void kvm_io_bus_destroy (struct kvm_io_bus *bus);@@ -1106,6 +1107,41 @@ int __weak kvm_arch_post_init_vm(struct kvm *kvm)return 0 ; }
virt/kvm/kvm_main.c int __weak kvm_arch_post_init_vm(struct kvm *kvm) · line 1119
Quoted 10 lines
virt/kvm/kvm_main.c +10
-0+/* +* Called after the VM is otherwise initialized, but just before adding it to +* the vm_list. +*/ +int __weak kvm_arch_add_vm (struct kvm *kvm,int usage_count) +{ +int r =0 ; + +if (usage_count !=1 ) +return 0 ;
Oops. This line should be.
+ return kvm_arch_post_init_vm(kvm);
"KVM: x86: Duplicate arch callbacks related to pm events and compat check" should have same line. "KVM: Eliminate kvm_arch_post_init_vm()" should include kvm_mmu_post_init_vm().
It creates a kernel thread. kvm unit test didn't catch it.
Thanks,
Quoted 28 lines
virt/kvm/kvm_main.c +25
-0+ +atomic_set (&hardware_enable_failed,0 ); +on_each_cpu (hardware_enable_nolock,NULL ,1 ); + +if (atomic_read (&hardware_enable_failed)) { + r = -EBUSY ; + goto err; + } + + r =kvm_arch_post_init_vm (kvm); +err: +if (r) +on_each_cpu (hardware_disable_nolock,NULL ,1 ); +return r; +} + +int __weak kvm_arch_del_vm (int usage_count) +{ +if (usage_count) +return 0 ; + +on_each_cpu (hardware_disable_nolock,NULL ,1 ); +return 0 ; +} +/* * Called just after removing the VM from the vm_list, but before doing any * other destruction.
Signature · 1 line
Isaku Yamahata <[email protected]>
Sean Christopherson
maintainerRe: [PATCH v5 03/30] KVM: x86: Move check_processor_compatibility from init ops to runtime ops
In reply to [email protected]Quoted 28 lines · On Thu, Sep 22, 2022, [email protected] wrote:
From: Chao Gao <[email protected]>
so that KVM can do compatibility checks on hotplugged CPUs. Drop
__initfromcheck_processor_compatibility()and its callees.use a
static_call()to invoke .check_processor_compatibility.Opportunistically rename {svm,vmx}_check_processor_compat to conform to the naming convention of fields of
kvm_x86_ops.Signed-off-by Chao Gao <[email protected]>Reviewed-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Reviewed-by Yuan Yao <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/svm/svm.c | 4 ++-- arch/x86/kvm/vmx/vmx.c | 14 +++++++------- arch/x86/kvm/x86.c | 3 +-- 5 files changed, 12 insertions(+), 12 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +0
-0
arch/x86/include/asm/kvm-x86-ops.h KVM_X86_OP(msr_filter_changed) · line 132
Quoted 4 lines
arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -129,6 +129,7 @@ KVM_X86_OP(msr_filter_changed) KVM_X86_OP(complete_emulated_msr) KVM_X86_OP(vcpu_deliver_sipi_vector) KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons); +KVM_X86_OP(check_processor_compatibility)
Nit I missed in all previous versions: please add this new entry above "hardware_enable" so as to keep the order similar to the order in struct kvm_x86_ops. E.g.
KVM_X86_OP(check_processor_compatibility)KVM_X86_OP(hardware_enable)
...
Sean Christopherson
maintainerRe: [PATCH v5 05/30] KVM: Provide more information in kernel log if hardware enabling fails
In reply to [email protected]Quoted 5 lines · On Thu, Sep 22, 2022, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Provide the name of the calling function to
hardware_enable_nolock()and include it in the error message to provide additional information on exactly what path failed.
Changelog doesn't match the code.
Quoted 15 lines
Opportunistically bump the
pr_info()topr_warn(), failure to enable virtualization support is warn-worthy as _something_ is wrong with the system.Suggested-by Chao Gao <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- virt/kvm/kvm_main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)virt/kvm/kvm_main.c +0
-0
virt/kvm/kvm_main.c static void hardware_enable_nolock(void *junk) · line 5010
Quoted 6 lines
virt/kvm/kvm_main.c +2
-1@@ -5006,7 +5006,8 @@ static void hardware_enable_nolock(void *junk)if (r) {cpumask_clear_cpu (cpu, cpus_hardware_enabled);atomic_inc (&hardware_enable_failed); -pr_info ("kvm: enabling virtualization on CPU%d failed\n" , cpu); +pr_warn ("kvm: enabling virtualization on CPU%d failed during %pSb\n" , + cpu,__builtin_return_address (0 ));
I vote to drop this patch. Trying to capture the caller is just a poor man's version of WARN, and at the end of this series KVM should be at a point where KVM can WARN when hardware enabling indicates a potentially fatal issue.
Specifically, kvm_arch_add_vm() shouldn't WARN since x86 can fail due a misbehaving userspace. kvm_arch_online_cpu() on the other hand can and should WARN since failure in that case means hardware enabling succeeded on other CPUs, and in the x86 case, that KVM is actively running VMs.
Sean Christopherson
maintainerRe: [PATCH v5 09/30] KVM: Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock
In reply to [email protected]Quoted 7 lines · On Thu, Sep 22, 2022, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Because
kvm_count_lockunnecessarily complicates the KVM locking convention Dropkvm_count_lockand instead protectkvm_usage_countwithkvm_lockfor simplicity. kvm_arch_hardware_enable/disable() callbacks depend on non-preemptiblity with the spin lock. Add preempt_disable/enable() around hardware enable/disable callback to keep the assumption.
There's the other "minor" wrinkle that prior to patch 7, "KVM: Rename and move CPUHP_AP_KVM_STARTING to ONLINE section, kvm_online_cpu() was called with IRQs disabled and couldn't sleep, i.e. couldn't acquire a mutex. That's very important to capture in the changelog.
Quoted 7 lines
Because
kvm_suspend()andkvm_resume()is called with interrupt disabled, they don't need preempt_disable/enable() pair.Opportunistically add some comments on locking.
Suggested-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>
...
virt/kvm/kvm_main.c static int kvm_online_cpu(unsigned int cpu) · line 5033
Quoted 6 lines
virt/kvm/kvm_main.c +2
-0@@ -5028,13 +5029,20 @@ static int kvm_online_cpu(unsigned int cpu)if (kvm_usage_count) {WARN_ON_ONCE (atomic_read (&hardware_enable_failed)); +/* +* arch callback kvm_arch_hardware_eanble() assumes that
s/eanble/enable
Though even better would be to avoid function names entirely.
virt/kvm/kvm_main.c static int kvm_online_cpu(unsigned int cpu) · line 5036
Quoted 3 lines
virt/kvm/kvm_main.c +3
-0+* preemption is disabled for historical reason. Disable +* preemption until all arch callbacks are fixed. +*/
Probably better to put this comment above to the WARN_ON_ONCE() in hardware_enable_nolock() since that's where the oddity and dependency on arch behavior lies. And then it can be turned into a FIXME, e.g.
/* * FIXME: drop the "preemption disabled" requirement here and in the * disable path once all arch code plays nice with preemption. */
Quoted 25 lines
virt/kvm/kvm_main.c +5
-1+preempt_disable ();hardware_enable_nolock (NULL ); +preempt_enable ();if (atomic_read (&hardware_enable_failed)) {atomic_set (&hardware_enable_failed,0 ); ret = -EIO ; } } -raw_spin_unlock (&kvm_count_lock); +mutex_unlock (&kvm_lock);return ret; }@@ -5042,6 +5050,8 @@ static void hardware_disable_nolock(void *junk) {int cpu =raw_smp_processor_id (); +WARN_ON_ONCE (preemptible ()); +if (!cpumask_test_cpu (cpu, cpus_hardware_enabled))return ;cpumask_clear_cpu (cpu, cpus_hardware_enabled);@@ -5050,10 +5060,18 @@ static void hardware_disable_nolock(void *junk)static int kvm_offline_cpu (unsigned int cpu)
virt/kvm/kvm_main.c static void hardware_disable_nolock(void *junk) · line 5069
Quoted 10 lines
virt/kvm/kvm_main.c +7
-2{ -raw_spin_lock (&kvm_count_lock); -if (kvm_usage_count) +mutex_lock (&kvm_lock); +if (kvm_usage_count) { +/* +* arch callback kvm_arch_hardware_disable() assumes that +* preemption is disabled for historical reason. Disable +* preemption until all arch callbacks are fixed. +*/
I vote to drop this comment and instead document everything in the enable FIXME (see above).
Quoted 8 lines
virt/kvm/kvm_main.c +4
-1+preempt_disable ();hardware_disable_nolock (NULL ); -raw_spin_unlock (&kvm_count_lock); +preempt_enable (); + } +mutex_unlock (&kvm_lock);return 0 ; }
...
virt/kvm/kvm_main.c static void kvm_init_debug(void) · line 5734
Quoted 9 lines
virt/kvm/kvm_main.c +4
-1@@ -5708,15 +5728,27 @@ static void kvm_init_debug(void)static int kvm_suspend (void ) { -if (kvm_usage_count) +/* +* The caller ensures that CPU hotplug is disabled by +* cpu_hotplug_disable() and other CPUs are offlined. No need for +* locking.
Disabling CPU hotplug prevents racing with kvm_online_cpu()/kvm_offline_cpu(), but doesn't prevent racing with hardware_enable_all()/hardware_disable_all().
And the lockdep doesn't mesh with the comment, which explains why kvm_lock doesn't
_need_ to be held, but not why kvm_lock _can't_ be held.
Maybe this?
/*
* Secondary CPUs and CPU hotplug are disabled across the suspend/resume
* callbacks, i.e. no need to acquire kvm_lock to ensure the usage count
* is stable. Assert that kvm_lock is not held as a paranoid sanity
* check that the system isn't suspended when KVM is enabling hardware. */
virt/kvm/kvm_main.c static void kvm_init_debug(void) · line 5742
Quoted 8 lines
virt/kvm/kvm_main.c +8
-0+*/ +lockdep_assert_not_held (&kvm_lock); + +if (kvm_usage_count) { +/* +* Because kvm_suspend() is called with interrupt disabled, no +* need to disable preemption. +*/
Add a lockdep and drop the comment, e.g. below the lockdep_assert_not_held(), add
lockdep_assert_irqs_disabled();
That covers the "why doesn't this disable preemption" _and_ enforces that IRQs are indeed disabled.
Quoted 16 lines
virt/kvm/kvm_main.c +2
-1hardware_disable_nolock (NULL ); + }return 0 ; }static void kvm_resume (void ) {if (kvm_usage_count) { -lockdep_assert_not_held (&kvm_count_lock); +lockdep_assert_not_held (&kvm_lock);hardware_enable_nolock (NULL ); } }Signature · 1 line
2.25.1
Sean Christopherson
maintainerRe: [PATCH v5 10/30] KVM: Add arch hooks when VM is added/deleted
In reply to [email protected]Quoted 6 lines · On Thu, Sep 22, 2022, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
and pass
kvm_usage_countwithkvm_lock. Movekvm_arch_post_init_vm()underkvm_arch_add_vm(). Replace enable/disable_hardware_all() with the default implementation of kvm_arch_add/del_vm(). Laterkvm_arch_post_init_vm()is deleted once x86 overrideskvm_arch_add_vm().
This needs to explain _why_ KVM is pivoting to add/remove hooks.
Quoted 25 lines
Suggested-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- include/linux/kvm_host.h | 2 + virt/kvm/kvm_main.c | 121 ++++++++++++++++++++------------------- 2 files changed, 65 insertions(+), 58 deletions(-)include/linux/kvm_host.h +2
-0@@ -1445,6 +1445,8 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu);bool kvm_arch_dy_runnable (struct kvm_vcpu *vcpu);bool kvm_arch_dy_has_pending_interrupt (struct kvm_vcpu *vcpu);int kvm_arch_post_init_vm (struct kvm *kvm); +int kvm_arch_add_vm (struct kvm *kvm,int usage_count); +int kvm_arch_del_vm (int usage_count);void kvm_arch_pre_destroy_vm (struct kvm *kvm);int kvm_arch_create_vm_debugfs (struct kvm *kvm);virt/kvm/kvm_main.c +0
-0
virt/kvm/kvm_main.c static int kvm_no_compat_open(struct inode *inode, struct file *file) · line 147
Quoted 8 lines
virt/kvm/kvm_main.c +3
-2@@ -142,8 +142,9 @@ static int kvm_no_compat_open(struct inode *inode, struct file *file)#define KVM_COMPAT (c) .compat_ioctl = kvm_no_compat_ioctl, \ .open = kvm_no_compat_open#endif -static int hardware_enable_all (void ); -static void hardware_disable_all (void ); +static void hardware_enable_nolock (void *junk); +static void hardware_disable_nolock (void *junk); +static void kvm_del_vm (void );
I think kvm_remove_vm() will be less confusing as "remove" is almost never used to describe freeing something, whereas "delete" is somewhat interchangeable with "free. I.e. make it more obvious that the hook isn't intented to actually delete/free a VM, rather it's there to remove/delete a VM from global tracking.
Ah, and this is especially true since the VM needs to be deleted from vm_list before the is destroyed, but hardware needs to stay enabled until the VM is fully destroyed.
Hmm, actually, I think even better would be to have kvm_remove_vm() and kvm_drop_vm() to make it more obvious that there isn't 100% symmetry between "add" and "remove".
E.g. rename kvm_arch_pre_destroy_vm() => kvm_arch_remove_vm() and then we end up with (see comments below for more details):
static int kvm_add_vm(struct kvm *kvm)
{
/*
* During onlining a CPU, cpu_online_mask is set before kvm_online_cpu()
* is called, i.e. on_each_cpu() includes CPUs that have not yet been
* onlined by KVM. Disable CPU hotplug to prevent enabling hardware on
* a CPU that hasn't yet done compatibility checks.
*/
cpus_read_lock();
mutex_lock(&kvm_lock);
r = kvm_arch_add_vm(kvm, ++kvm_usage_count);
if (r) {
--kvm_usage_count;
goto out;
}
list_add(&kvm->vm_list, &vm_list);out:
mutex_unlock(&kvm_lock); cpus_read_unlock(); }
static void kvm_remove_vm(struct kvm *kvm)
{
mutex_lock(&kvm_lock);
list_del(&kvm->vm_list);
mutex_unlock(&kvm_lock);
kvm_arch_remove_vm(kvm);
}static void kvm_drop_vm(void)
{
cpus_read_lock();
mutex_lock(&kvm_lock);
WARN_ON_ONCE(!kvm_usage_count);
kvm_usage_count--;
kvm_arch_drop_vm(kvm_usage_count);
mutex_unlock(&kvm_lock);
cpus_read_unlock();
}Quoted 14 lines
virt/kvm/kvm_main.c +6
-4@@ -1223,13 +1255,28 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname)if (r) goto out_err_no_debugfs; - r =kvm_arch_post_init_vm (kvm); -if (r) - goto out_err; - +/* +* During onlining a CPU, cpu_online_mask is set before kvm_online_cpu() +* is called. on_each_cpu() between them includes the CPU. As a result, +* hardware_enable_nolock() may get invoked before kvm_online_cpu(). +* This would enable hardware virtualization on that cpu without +* compatibility checks, which can potentially crash system or break
virt/kvm/kvm_main.c static struct kvm *kvm_create_vm(unsigned long type, const char *fdname) · line 1273
Quoted 10 lines
virt/kvm/kvm_main.c +9
-0+* running VMs. +* +* Disable CPU hotplug to prevent this case from happening. +*/ +cpus_read_lock ();mutex_lock (&kvm_lock); + kvm_usage_count++; + r =kvm_arch_add_vm (kvm, kvm_usage_count); +if (r) { +/* the following kvm_del_vm() decrements kvm_usage_count. */
This is buggy on two fronts. kvm_usage_count needs to be protected with kvm_lock, and AFAICT cpus_read_unlock() isn't called.
Invoking kvm_del_vm() in the error path is the source of both bugs. Typically, the paired "undo" of an operation should only be used if the "do" operation was fully successful.
As above, move this to a helper to make juggling the locks less painful.
Sean Christopherson
maintainerRe: [PATCH v5 11/30] KVM: Add arch hook for reboot event
In reply to [email protected]Quoted 8 lines · On Thu, Sep 22, 2022, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Factor out the logic on reboot event as arch hook. Later kvm/x86 overrides it.
Suggested-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>---
...
Quoted 10 lines
virt/kvm/kvm_main.c +2
-0@@ -5135,6 +5141,8 @@ static void kvm_del_vm(void)static int kvm_reboot (struct notifier_block *notifier,unsigned long val,void *v) { +int r; +/* * Some (well, at least mine) BIOSes hang on reboot if * in vmx root mode.
virt/kvm/kvm_main.c static int kvm_reboot(struct notifier_block *notifier, unsigned long val, · line 5158
Quoted 10 lines
virt/kvm/kvm_main.c +5
-2@@ -5143,8 +5151,14 @@ static int kvm_reboot(struct notifier_block *notifier, unsigned long val,*/ pr_info ("kvm: exiting hardware virtualization\n" ); kvm_rebooting = true; -on_each_cpu (hardware_disable_nolock,NULL ,1 ); -return NOTIFY_OK ; + +/* This hook is called without cpuhotplug disabled. */ +cpus_read_lock (); +mutex_lock (&kvm_lock); + r =kvm_arch_reboot (val);
Unless there's a valid use case for rejecting/stopping reboot, which I'm pretty sure there isn't, don't allow arch code to return a value. I.e. return NOTIFY_OK unconditionally from kvm_reboot() and drop the return from kvm_arch_reboot().
Quoted 9 lines
virt/kvm/kvm_main.c +3
-0+mutex_unlock (&kvm_lock); +cpus_read_unlock (); +return r; }static struct notifier_block kvm_reboot_notifier = {Signature · 1 line
2.25.1
Sean Christopherson
maintainerRe: [PATCH v5 16/30] KVM: Remove on_each_cpu(hardware_disable_nolock) in kvm_exit()
In reply to [email protected]Quoted 5 lines · On Thu, Sep 22, 2022, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
hardware_enable/disable_nolock() check if the hardware is already enabled/disabled and work as nop when they are called multiple times.
Quoted 8 lines
When VM is created/destroyed,
on_each_cpu(hardware_enable/disable_nolock)via kvm_arch_add/del_vm() and module_get/put() are called. It means when kvm module is removed, it's guaranteed that there is no vm and thathardware_disable_nolock()was called on each cpus.Although the module exit function,
kvm_exit(), callson_each_cpu(hardware_disable_nolock), it's essentially nop. Eliminate nop call inkvm_exit().
Add a WARN to "prove" that this is a nop, to guard against future bugs, and to document that it should be impossible for KVM to be unloaded with active VMs. E.g. do this in addition to dropping the hardware disabling.
virt/kvm/kvm_main.c +2 -0
@@ -5855,6 +5855,8 @@ void kvm_exit(void) {int cpu; +WARN_ON_ONCE (kvm_usage_count); +debugfs_remove_recursive (kvm_debugfs_dir);misc_deregister (&kvm_dev);for_each_possible_cpu (cpu)
Quoted 18 lines
Signed-off-by Isaku Yamahata <[email protected]>--- virt/kvm/kvm_main.c | 1 - 1 file changed, 1 deletion(-)virt/kvm/kvm_main.c +0
-1@@ -6034,7 +6034,6 @@ void kvm_exit(void)unregister_syscore_ops (&kvm_syscore_ops);unregister_reboot_notifier (&kvm_reboot_notifier);cpuhp_remove_state_nocalls (CPUHP_AP_KVM_ONLINE ); -on_each_cpu (hardware_disable_nolock,NULL ,1 );kvm_arch_hardware_unsetup ();kvm_arch_exit ();kvm_irqfd_exit ();
Sean Christopherson
maintainerRe: [PATCH v5 17/30] KVM: Move out KVM arch PM hooks and hardware enable/disable logic
In reply to [email protected]Quoted 3 lines · On Thu, Sep 22, 2022, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
To make clear that those files are default implementation
Heh, this is debatable, I was quite confused/surprised by seeing kvm_arch.c. It's also incomplete, which is further confusing, since there are a lot of default hooks that are left behind. And we most definitely don't want to move all of them, since many of those hooks are pure nops, i.e. get completely compiled out.
Given that we want this code to go away sooner than later, I don't think adding a temporarily to-be-deleted file makes sense.
Sean Christopherson
maintainerQuoted 6 lines · On Thu, Sep 22, 2022, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
This patch series is to implement the suggestion by Sean Christopherson [1] to reorganize enable/disable cpu virtualization feature by replacing the arch-generic current enable/disable logic with PM related hooks. And convert kvm/x86 to use new hooks.
Thanks for putting this together, actually seeing code is super helpful!
Unfortunately, after seeing the code, I think my suggestion was a bad one. At the end of this series, there's a rather gross amount of duplicate code between x86 and common KVM, and no clear line of sight to improving things.
Even if we move ARM, s390, and PPC away from the generic hooks, MIPS and RISC-V still need the generic implementation, i.e. we'll still have duplicate code.
Rather than force arch code to implement most/all power management hooks, I think we can achieve a similar outcome (let ARM do its own thing, turn s390 and PPC into nops) by wrapping the hardware enable/disable (and thus PM code) in a Kconfig, e.g. KVM_GENERIC_HARDWARE_ENABLING.
I'll throw together a rough prototype tomorrow (got partway through and then got distracted by other stuff) and hopefully post an RFC series.
Thanks again!
Sean Christopherson
maintainerQuoted 17 lines · Sean Christopherson
Quoted 6 lines · On Thu, Sep 22, 2022, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
This patch series is to implement the suggestion by Sean Christopherson [1] to reorganize enable/disable cpu virtualization feature by replacing the arch-generic current enable/disable logic with PM related hooks. And convert kvm/x86 to use new hooks.
Thanks for putting this together, actually seeing code is super helpful!
Unfortunately, after seeing the code, I think my suggestion was a bad one. At the end of this series, there's a rather gross amount of duplicate code between x86 and common KVM, and no clear line of sight to improving things.
Even if we move ARM, s390, and PPC away from the generic hooks, MIPS and RISC-V still need the generic implementation, i.e. we'll still have duplicate code.
Quoted 7 lines
Rather than force arch code to implement most/all power management hooks, I think we can achieve a similar outcome (let ARM do its own thing, turn s390 and PPC into nops) by wrapping the hardware enable/disable (and thus PM code) in a
Kconfig, e.g.KVM_GENERIC_HARDWARE_ENABLING.I'll throw together a rough prototype tomorrow (got partway through and then got distracted by other stuff) and hopefully post an RFC series.
Good news and bad news.
Bad news:
The KVM_GENERIC_HARDWARE_ENABLING idea is a bit of a bust. It works, but it's little more than a nice-to-have for s390 and PPC.
Good news (from a certain point of view):
The reason that the "generic h/w enabling" doesn't help much is because after sorting out the myriad issues in KVM initialization, which is even more of a bug-ridden mess than I initially thought, kvm_init() no longer has _any_ arch hooks. All the compat checks and whatnot are handled in x86, so tweaking those for TDX should be easier, or at the very least, we should have more options.
Sorting everything out is proving to be a nightmare, but I think I have the initial coding done. Testing will be fun. It'll likely be next week before I can post anything.
Paolo Bonzini
maintainerQuoted 10 lines · Sean Christopherson
Good news (from a certain point of view):
The reason that the "generic h/w enabling" doesn't help much is because after sorting out the myriad issues in KVM initialization, which is even more of a bug-ridden mess than I initially thought,kvm_init()no longerhas_any_arch hooks. All the compat checks and whatnot are handled in x86, so tweaking those for TDX should be easier, or at the very least, we should have more options.Sorting everything out is proving to be a nightmare, but I think I have the initial coding done. Testing will be fun. It'll likely be next week before I can post anything.
Hi Sean,
is this the same series that you mentioned a couple hours ago in reply to Isaku?
Thanks,
Paolo
Sean Christopherson
maintainerQuoted 8 lines · Paolo Bonzini
Quoted 7 lines · Sean Christopherson
Good news (from a certain point of view):
The reason that the "generic h/w enabling" doesn't help much is because after sorting out the myriad issues in KVM initialization, which is even more of a bug-ridden mess than I initially thought,kvm_init()no longerhas_any_arch hooks. All the compat checks and whatnot are handled in x86, so tweaking those for TDX should be easier, or at the very least, we should have more options.
Quoted 8 lines
Quoted 3 lines
Sorting everything out is proving to be a nightmare, but I think I have the initial coding done. Testing will be fun. It'll likely be next week before I can post anything.
Hi Sean,
is this the same series that you mentioned a couple hours ago in reply to Isaku?
Yep.