KVM: hardware enable/disable reorganize
Subsystems (15)
v4 cover KVM: hardware enable/disable reorganize
From: Isaku Yamahata <[email protected]>
I've updated the patch series based on Marc's request and Yuan review and now eliminated kvm_arch_check_processor_compat().
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 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().
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. 17/18 and 18/18
[1] https://lore.kernel.org/kvm/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
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 (19):
KVM: x86: Use this_cpu_ptr() instead of
per_cpu_ptr(smp_processor_id())
KVM: Do processor compatibility check on resume
KVM: Drop kvm_count_lock and instead protect kvm_usage_count with
kvm_lock
KVM: Add arch hooks for PM events with empty stub
KVM: x86: Move TSC fixup logic to KVM arch resume callback
KVM: Add arch hook when VM is added/deleted
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 a arch wrapper to check all processor compatibility
KVM: x86: Duplicate arch callbacks related to pm events and compat
check
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 (2):
KVM: x86: Drop kvm_user_return_msr_cpu_online()
KVM: Provide more information in kernel log if hardware enabling fails
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 | 14 +- 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 | 221 ++++++++++++++++++++++++----- include/kvm/arm_arch_timer.h | 4 + include/kvm/arm_vgic.h | 4 + include/linux/cpuhotplug.h | 5 +- include/linux/kvm_host.h | 16 ++- virt/kvm/Kconfig | 3 + virt/kvm/Makefile.kvm | 3 + virt/kvm/kvm_arch.c | 140 ++++++++++++++++++ virt/kvm/kvm_main.c | 208 +++++++++------------------ 22 files changed, 481 insertions(+), 262 deletions(-) create mode 100644 virt/kvm/kvm_arch.c
base-commit: b90cb1053190353cc30f0fef0ef1f378ccc063c5
Signature · 1 line
2.25.1
PATCH v4 1/26 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 ;
@@ -9212,7 +9217,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 ;
@@ -11836,7 +11846,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 v4 2/26 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 v4 3/26 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
@@ -2495,8 +2495,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;
@@ -2514,7 +2514,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;
@@ -2523,8 +2523,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;
@@ -7417,7 +7417,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;
@@ -8015,6 +8015,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,
@@ -8404,7 +8405,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
@@ -12001,7 +12001,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 ());
@@ -12009,7 +12008,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 v4 4/26 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
@@ -11998,7 +11998,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 v4 5/26 KVM: Provide more information in kernel log if hardware enabling fails
In reply to [email protected]From: Sean Christopherson <[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 | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
virt/kvm/kvm_main.c +6 -5
@@ -4991,7 +4991,7 @@ static struct miscdevice kvm_dev = { &kvm_chardev_ops, }; -static void hardware_enable_nolock (void *junk) +static void hardware_enable_nolock (void *caller_name) {int cpu =raw_smp_processor_id ();int r;
@@ -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 %s()\n" , + cpu, (const char *)caller_name); } }
@@ -5014,7 +5015,7 @@ static int kvm_starting_cpu(unsigned int cpu) {raw_spin_lock (&kvm_count_lock);if (kvm_usage_count) -hardware_enable_nolock (NULL ); +hardware_enable_nolock ((void *)__func__);raw_spin_unlock (&kvm_count_lock);return 0 ; }
@@ -5063,7 +5064,7 @@ static int hardware_enable_all(void) kvm_usage_count++;if (kvm_usage_count ==1 ) {atomic_set (&hardware_enable_failed,0 ); -on_each_cpu (hardware_enable_nolock,NULL ,1 ); +on_each_cpu (hardware_enable_nolock, (void *)__func__,1 );if (atomic_read (&hardware_enable_failed)) {hardware_disable_all_nolock ();
@@ -5686,7 +5687,7 @@ static void kvm_resume(void) {if (kvm_usage_count) {lockdep_assert_not_held (&kvm_count_lock); -hardware_enable_nolock (NULL ); +hardware_enable_nolock ((void *)__func__); } }
Signature · 1 line
2.25.1
PATCH v4 6/26 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 v4 7/26 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 *caller_name) } } -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 ((void *)__func__); +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 v4 9/26 KVM: Do processor compatibility check on resume
In reply to [email protected]From: Isaku Yamahata <[email protected]>
So far the processor compatibility check is not done on resume. It should be done. The resume is called for resuming from S3/S4. CPUs can be replaced or the kernel can resume from S4 on a different machine. So compatibility check is desirable.
--- virt/kvm/kvm_main.c | 7 +++++++ 1 file changed, 7 insertions(+)
virt/kvm/kvm_main.c +7 -0
@@ -5715,6 +5715,13 @@ static int kvm_suspend(void)static void kvm_resume (void ) { +if (kvm_arch_check_processor_compat ()) +/* +* No warning here because kvm_arch_check_processor_compat() +* would have warned with more information. +*/ +return ;/* FIXME: disable KVM */ +if (kvm_usage_count) {lockdep_assert_not_held (&kvm_count_lock);hardware_enable_nolock ((void *)__func__);
Signature · 1 line
2.25.1
PATCH v4 8/26 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
@@ -7421,20 +7421,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
@@ -12000,9 +12000,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 *caller_name)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 v4 10/26 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.
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 + migration. The lock order is cpus lock => kvm_lock. ``kvm->mn_invalidate_lock`` ^^^^^^^^^^^^^^^^^^^^^^^^^^^
virt/kvm/kvm_main.c +44 -12
132 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v4 11/26 KVM: Add arch hooks for PM events with empty stub
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Add arch hooks for reboot, suspend, resume, and CPU-online/offline events with empty stub functions.
--- include/linux/kvm_host.h | 6 +++++ virt/kvm/Makefile.kvm | 2 +- virt/kvm/kvm_arch.c | 44 ++++++++++++++++++++++++++++++ virt/kvm/kvm_main.c | 58 +++++++++++++++++++++++++--------------- 4 files changed, 88 insertions(+), 22 deletions(-) create mode 100644 virt/kvm/kvm_arch.c
include/linux/kvm_host.h +6 -0
@@ -1448,6 +1448,12 @@ int kvm_arch_post_init_vm(struct kvm *kvm);void kvm_arch_pre_destroy_vm (struct kvm *kvm);int kvm_arch_create_vm_debugfs (struct kvm *kvm); +int kvm_arch_suspend (int usage_count); +void kvm_arch_resume (int usage_count); +int kvm_arch_reboot (int val); +int kvm_arch_online_cpu (unsigned int cpu,int usage_count); +int kvm_arch_offline_cpu (unsigned int cpu,int usage_count); +#ifndef __KVM_HAVE_ARCH_VM_ALLOC/* * All architectures that want to use vzalloc currently also
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)/kvm_arch.o $(KVM)/eventfd.o $(KVM)/binary_stats.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 +44 -0
44 lines not on this page · raw message
virt/kvm/kvm_main.c +37 -21
128 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v4 12/26 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 | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-)
arch/x86/kvm/x86.c +13 -11
@@ -11835,18 +11835,27 @@ 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)(); +} + +void kvm_arch_hardware_disable (void ) +{ +static_call (kvm_x86_hardware_disable)(); +drop_user_return_notifiers (); +} + +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 ; local_tsc =rdtsc (); stable = !kvm_check_tsc_unstable ();
@@ -11921,13 +11930,6 @@ int kvm_arch_hardware_enable(void) } } -return 0 ; -} - -void kvm_arch_hardware_disable (void ) -{ -static_call (kvm_x86_hardware_disable)(); -drop_user_return_notifiers (); }static inline void kvm_ops_update (struct kvm_x86_init_ops *ops)
Signature · 1 line
2.25.1
PATCH v4 13/26 KVM: Add arch hook 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(). Later kvm_arch_post_init_vm() is deleted once x86 overrides kvm_arch_add_vm().
--- include/linux/kvm_host.h | 2 ++ virt/kvm/kvm_arch.c | 12 +++++++++++- virt/kvm/kvm_main.c | 21 +++++++++++++++++---- 3 files changed, 30 insertions(+), 5 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_arch.c +11 -1
@@ -10,11 +10,21 @@#include <linux/kvm_host.h> +__weak int kvm_arch_post_init_vm (struct kvm *kvm) +{ +return 0 ; +} +/* * Called after the VM is otherwise initialized, but just before adding it to * the vm_list. */ -__weak int kvm_arch_post_init_vm (struct kvm *kvm) +__weak int kvm_arch_add_vm (struct kvm *kvm,int usage_count) +{ +return kvm_arch_post_init_vm (kvm); +} + +__weak int kvm_arch_del_vm (int usage_count) {return 0 ; }
virt/kvm/kvm_main.c +17 -4
@@ -145,6 +145,7 @@ static int kvm_no_compat_open(struct inode *inode, struct file *file)static int hardware_enable_all (void );static void hardware_disable_all (void );static void hardware_disable_nolock (void *junk); +static void kvm_del_vm (void );static void kvm_io_bus_destroy (struct kvm_io_bus *bus);
@@ -1215,11 +1216,12 @@ 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; -mutex_lock (&kvm_lock); + r =kvm_arch_add_vm (kvm, kvm_usage_count); +if (r) { +mutex_unlock (&kvm_lock); + goto out_err; + }list_add (&kvm->vm_list, &vm_list);mutex_unlock (&kvm_lock);
@@ -1239,6 +1241,7 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname)#endif out_err_no_mmu_notifier:hardware_disable_all (); +kvm_del_vm (); out_err_no_disable:kvm_arch_destroy_vm (kvm); out_err_no_arch_destroy_vm:
@@ -1319,6 +1322,7 @@ static void kvm_destroy_vm(struct kvm *kvm)kvm_arch_free_vm (kvm);preempt_notifier_dec ();hardware_disable_all (); +kvm_del_vm ();mmdrop (mm);module_put (kvm_chardev_ops.owner); }
@@ -5096,6 +5100,15 @@ static void hardware_disable_all(void)cpus_read_unlock (); } +static void kvm_del_vm (void ) +{ +cpus_read_lock (); +mutex_lock (&kvm_lock); +kvm_arch_del_vm (kvm_usage_count); +mutex_unlock (&kvm_lock); +cpus_read_unlock (); +} +static int hardware_enable_all (void ) {int r =0 ;
Signature · 1 line
2.25.1
PATCH v4 14/26 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 that KVM/x86 (and other KVM arch in future) will override them, split out those into a single file. Once conversions for all kvm archs are done, the file will be deleted. kvm_arch_pre_hardware_unsetup() is introduced to avoid cross-arch code churn for now. Once it's settled down, kvm_arch_pre_hardware_unsetup() can be merged into kvm_arch_hardware_unsetup() in each arch code. x86 kvm_arch_resume() needs to be adjusted to do CPU compatibility check and virtualization hardware enabling.
--- arch/x86/kvm/x86.c | 10 ++ include/linux/kvm_host.h | 1 + virt/kvm/kvm_arch.c | 124 +++++++++++++++++++++++- virt/kvm/kvm_main.c | 198 +++++---------------------------------- 4 files changed, 156 insertions(+), 177 deletions(-)
arch/x86/kvm/x86.c +10 -0
@@ -11854,9 +11854,19 @@ void kvm_arch_resume(int usage_count)u64 max_tsc =0 ;bool stable, backwards_tsc = false; +if (kvm_arch_check_processor_compat ()) +return ;/* FIXME: disable KVM. */ +if (!usage_count)return ; +preempt_disable (); +if (kvm_arch_hardware_enable ()) { +preempt_enable (); +return ; + } +preempt_enable (); + local_tsc =rdtsc (); stable = !kvm_check_tsc_unstable ();list_for_each_entry (kvm, &vm_list, vm_list) {
include/linux/kvm_host.h +1 -0
@@ -1437,6 +1437,7 @@ static inline void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu) {}int kvm_arch_hardware_enable (void );void kvm_arch_hardware_disable (void );int kvm_arch_hardware_setup (void *opaque); +void kvm_arch_pre_hardware_unsetup (void );void kvm_arch_hardware_unsetup (void );int kvm_arch_check_processor_compat (void );int kvm_arch_vcpu_runnable (struct kvm_vcpu *vcpu);
virt/kvm/kvm_arch.c +122 -2
171 lines not on this page · raw message
virt/kvm/kvm_main.c +23 -175
321 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v4 15/26 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 | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
virt/kvm/kvm_arch.c +11 -11
@@ -20,7 +20,7 @@ __weak int kvm_arch_post_init_vm(struct kvm *kvm)return 0 ; } -static void hardware_enable_nolock (void *caller_name) +static void hardware_enable (void *caller_name) {int cpu =raw_smp_processor_id ();int r;
@@ -42,7 +42,7 @@ static void hardware_enable_nolock(void *caller_name) } } -static void hardware_disable_nolock (void *junk) +static void hardware_disable (void *junk) {int cpu =raw_smp_processor_id ();
@@ -56,7 +56,7 @@ static void hardware_disable_nolock(void *junk)__weak void kvm_arch_pre_hardware_unsetup (void ) { -on_each_cpu (hardware_disable_nolock,NULL ,1 ); +on_each_cpu (hardware_disable,NULL ,1 ); }/*
@@ -71,7 +71,7 @@ __weak int kvm_arch_add_vm(struct kvm *kvm, int usage_count)return 0 ;atomic_set (&hardware_enable_failed,0 ); -on_each_cpu (hardware_enable_nolock, (void *)__func__,1 ); +on_each_cpu (hardware_enable, (void *)__func__,1 );if (atomic_read (&hardware_enable_failed)) { r = -EBUSY ;
@@ -81,7 +81,7 @@ __weak int 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; }
@@ -90,7 +90,7 @@ __weak int 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 ; }
@@ -116,7 +116,7 @@ __weak int kvm_arch_online_cpu(unsigned int cpu, int usage_count)* preemption until all arch callbacks are fixed. */ preempt_disable (); -hardware_enable_nolock ((void *)__func__); +hardware_enable ((void *)__func__);preempt_enable ();if (atomic_read (&hardware_enable_failed)) {atomic_set (&hardware_enable_failed,0 );
@@ -135,7 +135,7 @@ __weak int 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 ;
@@ -143,7 +143,7 @@ __weak int kvm_arch_offline_cpu(unsigned int cpu, int usage_count)__weak int kvm_arch_reboot (int val) { -on_each_cpu (hardware_disable_nolock,NULL ,1 ); +on_each_cpu (hardware_disable,NULL ,1 );return NOTIFY_OK ; }
@@ -151,7 +151,7 @@ __weak int kvm_arch_suspend(int usage_count) {if (usage_count) {preempt_disable (); -hardware_disable_nolock (NULL ); +hardware_disable (NULL );preempt_enable (); }return 0 ;
@@ -168,7 +168,7 @@ __weak void kvm_arch_resume(int usage_count)if (usage_count) {preempt_disable (); -hardware_enable_nolock ((void *)__func__); +hardware_enable ((void *)__func__);preempt_enable (); } }
Signature · 1 line
2.25.1
PATCH v4 16/26 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 | 62 ++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 31 deletions(-)
virt/kvm/kvm_arch.c +31 -31
@@ -13,14 +13,13 @@#include <linux/kvm_host.h> static cpumask_t cpus_hardware_enabled =CPU_MASK_NONE ; -static atomic_t hardware_enable_failed;__weak int kvm_arch_post_init_vm (struct kvm *kvm) {return 0 ; } -static void hardware_enable (void *caller_name) +static int __hardware_enable (void *caller_name) {int cpu =raw_smp_processor_id ();int r;
@@ -28,18 +27,22 @@ static void hardware_enable(void *caller_name)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 %s()\n" , cpu, (const char *)caller_name); - } +else +cpumask_set_cpu (cpu, &cpus_hardware_enabled); +return r; +} + +static void hardware_enable (void *arg) +{ +atomic_t *failed = arg; + +if (__hardware_enable ((void *)__func__)) +atomic_inc (failed); }static void hardware_disable (void *junk)
@@ -65,15 +68,16 @@ __weak void kvm_arch_pre_hardware_unsetup(void)*/ __weak 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 (&hardware_enable_failed,0 ); -on_each_cpu (hardware_enable, (void *)__func__,1 ); +atomic_set (&failed,0 ); +on_each_cpu (hardware_enable, &failed,1 ); -if (atomic_read (&hardware_enable_failed)) { +if (atomic_read (&failed)) { r = -EBUSY ; goto err; }
@@ -96,33 +100,29 @@ __weak int kvm_arch_del_vm(int usage_count)__weak int 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_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. */ -if (usage_count) { -WARN_ON_ONCE (atomic_read (&hardware_enable_failed)); + ret =__hardware_enable ((void *)__func__); +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 ((void *)__func__); -preempt_enable (); -if (atomic_read (&hardware_enable_failed)) { -atomic_set (&hardware_enable_failed,0 ); - ret = -EIO ; - } - }return ret; }
@@ -168,7 +168,7 @@ __weak void kvm_arch_resume(int usage_count)if (usage_count) {preempt_disable (); -hardware_enable ((void *)__func__); + (void )__hardware_enable ((void *)__func__);preempt_enable (); } }
Signature · 1 line
2.25.1
PATCH v4 18/26 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 | 168 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 163 insertions(+), 5 deletions(-)
arch/x86/kvm/x86.c +163 -5
@@ -11845,6 +11845,169 @@ 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 *caller_name) +{ +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 %s()\n" , + cpu, (const char *)caller_name); +else +cpumask_set_cpu (cpu, &cpus_hardware_enabled); +return r; +} + +static void hardware_enable (void *arg) +{ +atomic_t *failed = arg; + +if (__hardware_enable ((void *)__func__)) +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 (); +} + +void kvm_arch_pre_hardware_unsetup (void ) +{ +on_each_cpu (hardware_disable,NULL ,1 ); +} + +/* +* 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 ((void *)__func__); +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) { +preempt_disable (); +hardware_disable (NULL ); +preempt_enable (); + } +return 0 ; +} +void kvm_arch_resume (int usage_count) {struct kvm *kvm;
@@ -12122,11 +12285,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 v4 20/26 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 + include/linux/kvm_host.h | 3 +++ virt/kvm/Kconfig | 3 +++ virt/kvm/Makefile.kvm | 5 ++++- 4 files changed, 11 insertions(+), 1 deletion(-)
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
include/linux/kvm_host.h +3 -0
@@ -1434,8 +1434,11 @@ 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 +#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_pre_hardware_unsetup (void );void kvm_arch_hardware_unsetup (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)/kvm_arch.o $(KVM)/eventfd.o $(KVM)/binary_stats.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 v4 17/26 KVM: Introduce a arch wrapper to check all processor compatibility
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Introduce a 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
@@ -1440,6 +1440,7 @@ int kvm_arch_hardware_setup(void *opaque);void kvm_arch_pre_hardware_unsetup (void );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
@@ -98,6 +98,24 @@ __weak int kvm_arch_del_vm(int usage_count)return 0 ; } +static void check_processor_compat (void *rtn) +{ + *(int *)rtn =kvm_arch_check_processor_compat (); +} + +__weak 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 ; +} +__weak int 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 v4 19/26 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 | 12 +----------- 3 files changed, 2 insertions(+), 18 deletions(-)
arch/x86/kvm/x86.c +1 -6
@@ -11847,11 +11847,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 *caller_name) {int cpu =raw_smp_processor_id ();
@@ -11915,7 +11910,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
@@ -1446,7 +1446,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 -11
@@ -14,11 +14,6 @@static cpumask_t cpus_hardware_enabled =CPU_MASK_NONE ; -__weak int kvm_arch_post_init_vm (struct kvm *kvm) -{ -return 0 ; -} -static int __hardware_enable (void *caller_name) {int cpu =raw_smp_processor_id ();
@@ -79,13 +74,8 @@ __weak int 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 v4 22/26 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 | 47 +++++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 17 deletions(-)
arch/x86/kvm/x86.c +30 -17
@@ -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) \
@@ -11911,21 +11913,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 ; }
@@ -11933,7 +11922,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;
@@ -11997,7 +11986,7 @@ void kvm_arch_resume(int usage_count)u64 max_tsc =0 ;bool stable, backwards_tsc = false; -if (kvm_arch_check_processor_compat ()) +if (kvm_check_processor_compatibility ())return ;/* FIXME: disable KVM. */ if (!usage_count)
@@ -12103,6 +12092,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;
@@ -12143,7 +12150,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 )
@@ -12155,7 +12163,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);
@@ -12175,6 +12183,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 v4 21/26 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. Opportunistically make kvm_arch_pre_hardware_unsetup() empty.
--- arch/x86/kvm/x86.c | 27 +++++++-------------------- include/linux/kvm_host.h | 6 ++++-- 2 files changed, 11 insertions(+), 22 deletions(-)
arch/x86/kvm/x86.c +7 -20
@@ -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) {
@@ -11834,17 +11834,6 @@ 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)(); -} - -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 *caller_name)
@@ -11856,7 +11845,7 @@ static int __hardware_enable(void *caller_name)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 %s()\n" , cpu, (const char *)caller_name);
@@ -11882,12 +11871,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 (); -} - -void kvm_arch_pre_hardware_unsetup (void ) -{ -on_each_cpu (hardware_disable,NULL ,1 ); +static_call (kvm_x86_hardware_disable)(); +drop_user_return_notifiers (); }/*
@@ -12019,7 +12004,7 @@ void kvm_arch_resume(int usage_count)return ;preempt_disable (); -if (kvm_arch_hardware_enable ()) { +if (static_call (kvm_x86_hardware_enable)()) {preempt_enable ();return ; }
@@ -12163,6 +12148,8 @@ int kvm_arch_hardware_setup(void *opaque)void kvm_arch_hardware_unsetup (void ) { +on_each_cpu (hardware_disable,NULL ,1 ); +kvm_unregister_perf_callbacks ();static_call (kvm_x86_hardware_unsetup)();
include/linux/kvm_host.h +4 -2
@@ -1434,13 +1434,15 @@ 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 -#ifndef CONFIG_HAVE_KVM_OVERRIDE_HARDWARE_ENABLE +#ifdef CONFIG_HAVE_KVM_OVERRIDE_HARDWARE_ENABLE +static inline void kvm_arch_pre_hardware_unsetup (void ) {} +#else +void kvm_arch_pre_hardware_unsetup (void );int kvm_arch_hardware_enable (void );void kvm_arch_hardware_disable (void );#endif int kvm_arch_hardware_setup (void *opaque); -void kvm_arch_pre_hardware_unsetup (void );void kvm_arch_hardware_unsetup (void );int kvm_arch_check_processor_compat (void );int kvm_arch_check_processor_compat_all (void );
Signature · 1 line
2.25.1
PATCH v4 23/26 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.
--- arch/powerpc/kvm/powerpc.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
arch/powerpc/kvm/powerpc.c +11 -2
@@ -444,12 +444,21 @@ int kvm_arch_hardware_enable(void)int kvm_arch_hardware_setup (void *opaque) { -return 0 ; +/* +* kvmppc_core_check_processor_compat() checks the global variable. +* No point to check on all processors or at runtime. +* 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") +*/ +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 v4 25/26 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
@@ -11836,23 +11836,16 @@ void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) }EXPORT_SYMBOL_GPL (kvm_vcpu_deliver_sipi_vector); -static cpumask_t cpus_hardware_enabled =CPU_MASK_NONE ; -static int __hardware_enable (void *caller_name) { -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 %s()\n" , - cpu, (const char *)caller_name); -else -cpumask_set_cpu (cpu, &cpus_hardware_enabled); +smp_processor_id (), (const char *)caller_name);return r; }
@@ -11866,13 +11859,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 v4 24/26 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 | 29 ----------------------------- virt/kvm/kvm_main.c | 4 ---- 9 files changed, 70 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
@@ -456,11 +456,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
@@ -11913,11 +11913,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;
@@ -12183,11 +12178,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
@@ -1444,8 +1444,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 -29
@@ -88,32 +88,10 @@ __weak int kvm_arch_del_vm(int usage_count)return 0 ; } -static void check_processor_compat (void *rtn) -{ - *(int *)rtn =kvm_arch_check_processor_compat (); -} - -__weak 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 ; -} -__weak 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 ;
@@ -167,13 +145,6 @@ __weak int kvm_arch_suspend(int usage_count)__weak void kvm_arch_resume (int usage_count) { -if (kvm_arch_check_processor_compat ()) -/* -* No warning here because kvm_arch_check_processor_compat() -* would have warned with more information. -*/ -return ;/* FIXME: disable KVM */ -if (usage_count) {preempt_disable (); (void )__hardware_enable ((void *)__func__);
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 v4 26/26 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 *caller_name) { -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 %s()\n" , - cpu, (const char *)caller_name); -else -cpumask_set_cpu (cpu, &cpus_hardware_enabled); +smp_processor_id (), (const char *)caller_name);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 v4 09/26] KVM: Do processor compatibility check on resume
In reply to [email protected]Quoted 5 lines · On Thu, Sep 08, 2022 at 04:25:25PM -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
So far the processor compatibility check is not done on resume. It should be done. The resume is called for resuming from S3/S4. CPUs can be replaced or the kernel can resume from S4 on a different machine. So
Are they valid cases handled by other kernel components? Trying to handle them in KVM only doesn't help as kernel is likely to fail somewhere else.
Re: [PATCH v4 10/26] KVM: Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock
In reply to [email protected]Quoted 4 lines · On Thu, Sep 08, 2022 at 04:25:26PM -0700, [email protected] wrote:
Documentation/virt/kvm/locking.rst +0
-4- -``kvm_count_lock`` -^^^^^^^^^^^^^^^^^^ -
Documentation/virt/kvm/locking.rst time it will be set using the Dirty tracking mechanism described above. · line 223
Quoted 10 lines
Documentation/virt/kvm/locking.rst +5
-5-: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 + migration. The lock order is cpus lock => kvm_lock.
Probably "/wrt CPU hotplug" is better.
virt/kvm/kvm_main.c static void kvm_init_debug(void) · line 5732
Quoted 8 lines
virt/kvm/kvm_main.c +2
-1@@ -5708,8 +5728,18 @@ static void kvm_init_debug(void)static int kvm_suspend (void ) { -if (kvm_usage_count) +/* +* The caller ensures that CPU hotlug is disabled by
^hotplug
virt/kvm/kvm_main.c static void kvm_init_debug(void) · line 5741
Quoted 9 lines
virt/kvm/kvm_main.c +8
-0+* cpu_hotplug_disable() and other CPUs are offlined. No need for +* locking. +*/ +lockdep_assert_not_held (&kvm_lock); + +if (kvm_usage_count) { +preempt_disable ();hardware_disable_nolock (NULL ); +preempt_enable ();
kvm_suspend() is called with interrupt disabled. So, no need to disable preemption.
/**
* syscore_suspend - Execute all the registered system core suspend callbacks. *
* This function is executed with one CPU on-line and disabled interrupts. */
int syscore_suspend(void)
virt/kvm/kvm_main.c static void kvm_resume(void) · line 5759
Quoted 13 lines
virt/kvm/kvm_main.c +4
-1+ }return 0 ; }@@ -5723,8 +5753,10 @@ static void kvm_resume(void)return ;/* FIXME: disable KVM */ if (kvm_usage_count) { -lockdep_assert_not_held (&kvm_count_lock); +lockdep_assert_not_held (&kvm_lock); +preempt_disable ();hardware_enable_nolock ((void *)__func__); +preempt_enable ();
ditto.
Re: [PATCH v4 11/26] KVM: Add arch hooks for PM events with empty stub
In reply to [email protected]Quoted 20 lines · On Thu, Sep 08, 2022 at 04:25:27PM -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Add arch hooks for reboot, suspend, resume, and CPU-online/offline events with empty stub functions.
Suggested-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>---
include/linux/kvm_host.h | 6 +++++ virt/kvm/Makefile.kvm | 2 +- virt/kvm/kvm_arch.c | 44 ++++++++++++++++++++++++++++++ virt/kvm/kvm_main.c | 58 +++++++++++++++++++++++++--------------- 4 files changed, 88 insertions(+), 22 deletions(-)create mode 100644
virt/kvm/kvm_arch.cinclude/linux/kvm_host.h +0
-0
include/linux/kvm_host.h int kvm_arch_post_init_vm(struct kvm *kvm); · line 1455
Quoted 8 lines
include/linux/kvm_host.h +5
-0@@ -1448,6 +1448,12 @@ int kvm_arch_post_init_vm(struct kvm *kvm);void kvm_arch_pre_destroy_vm (struct kvm *kvm);int kvm_arch_create_vm_debugfs (struct kvm *kvm); +int kvm_arch_suspend (int usage_count); +void kvm_arch_resume (int usage_count); +int kvm_arch_reboot (int val); +int kvm_arch_online_cpu (unsigned int cpu,int usage_count); +int kvm_arch_offline_cpu (unsigned int cpu,int usage_count);
Why not extract each of them with one separate patch?
Quoted 33 lines
include/linux/kvm_host.h +1
-0+#ifndef __KVM_HAVE_ARCH_VM_ALLOC/* * All architectures that want to use vzalloc currently also virt/kvm/Makefile.kvm +0
-0@@ -5,7 +5,7 @@KVM ?= ../../../virt/kvm
virt/kvm/Makefile.kvm +1
-1-kvm-y := $(KVM)/kvm_main.o $(KVM)/eventfd.o $(KVM)/binary_stats.o +kvm-y := $(KVM)/kvm_main.o $(KVM)/kvm_arch.o $(KVM)/eventfd.o $(KVM)/binary_stats.okvm-$(CONFIG_KVM_VFIO) += $(KVM)/vfio.o
kvm-$(CONFIG_KVM_MMIO) += $(KVM)/coalesced_mmio.o
kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.ovirt/kvm/kvm_arch.c new file +10
-0@@ -0,0 +1,44 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* kvm_arch.c: kvm default arch hooks for hardware enabling/disabling +* Copyright (c) 2022 Intel Corporation. +* +* Author: +* Isaku Yamahata <[email protected]> +* <[email protected]> +*/ +
virt/kvm/kvm_arch.c line 20
Quoted 10 lines
virt/kvm/kvm_arch.c new file +10
-0+#include <linux/kvm_host.h> + +/* +* Called after the VM is otherwise initialized, but just before adding it to +* the vm_list. +*/ +__weak int kvm_arch_post_init_vm (struct kvm *kvm) +{ +return 0 ; +}
use "int __weak" to comply with kernel's convension.
Quoted 9 lines
static int
kvm_offline_cpu(unsigned int cpu)
{virt/kvm/kvm_arch.c new file +2
-0+int ret =0 ; +mutex_lock(&kvm_lock); if (kvm_usage_count) { /*
virt/kvm/kvm_arch.c static int kvm_offline_cpu(unsigned int cpu) · line 5073
Quoted 7 lines
virt/kvm/kvm_arch.c new file +4
-0@@ -5069,10 +5067,15 @@ static int kvm_offline_cpu(unsigned int cpu)*/ preempt_disable ();hardware_disable_nolock (NULL ); + ret =kvm_arch_offline_cpu (cpu, kvm_usage_count); +if (ret) { + (void )hardware_enable_nolock (NULL ); +atomic_set (&hardware_enable_failed,0 );
The error-handling code ignores hardware enabling failure which looks weird to me. If you extract kvm_arch_offline_cpu() directly like what you do in patch 14 (rather than add a stub function first and then move some code to the stub function), the error-handling code isn't needed.
Re: [PATCH v4 12/26] KVM: x86: Move TSC fixup logic to KVM arch resume callback
In reply to [email protected]Quoted 7 lines · On Thu, Sep 08, 2022 at 04:25:28PM -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
commit
0dd6a6edb012("KVM: Dont mark TSC unstable due to S4 suspend") made use ofkvm_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 tokvm_arch_resume()callback.
IIUC, kvm_arch_resume() is called on the first CPU waking up from suspension. But the detection was done on every CPU. Is it a problem (i.e., we fail to detect TSC goes backward on some CPUs)?
Re: [PATCH v4 23/26] RFC: KVM: powerpc: Move processor compatibility check to hardware setup
In reply to [email protected]Le 09/09/2022 à 01:25, [email protected] a écrit :
Quoted 25 lines
[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]
From: Isaku Yamahata <[email protected]>
Move processor compatibility check from
kvm_arch_processor_compat()intokvm_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.Suggested-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Cc Fabiano Rosas <[email protected]>--- arch/powerpc/kvm/powerpc.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)arch/powerpc/kvm/powerpc.c +0
-0@@ -444,12 +444,21 @@ 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 455
Quoted 10 lines
arch/powerpc/kvm/powerpc.c +9
-1-return 0 ; +/* +* kvmppc_core_check_processor_compat() checks the global variable. +* No point to check on all processors or at runtime. +* 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") +*/
This explanation shouldn't be in the code. The content of other file may change in the future, the files may be renamed or deleted, new files may be added. And there is no added value with that comment.
That detailed explanation should go in the commit message.
Quoted 13 lines
arch/powerpc/kvm/powerpc.c +2
-2+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) --2.25.1
Re: [PATCH v4 13/26] KVM: Add arch hook when VM is added/deleted
In reply to [email protected]Quoted 1 line · On Thu, Sep 08, 2022 at 04:25:29PM -0700, [email protected] wrote:
+__weak int
kvm_arch_post_init_vm(struct kvm *kvm)
int __weak
Quoted 8 lines
virt/kvm/kvm_arch.c +4
-0+{ +return 0 ; +} +/*
* Called after the VM is otherwise initialized, but just before adding it to
* thevm_list.
*/
virt/kvm/kvm_arch.c line 27
Quoted 7 lines
virt/kvm/kvm_arch.c +6
-1-__weak int kvm_arch_post_init_vm (struct kvm *kvm) +__weak int kvm_arch_add_vm (struct kvm *kvm,int usage_count) +{ +return kvm_arch_post_init_vm (kvm); +} + +__weak int kvm_arch_del_vm (int usage_count)
ditto
virt/kvm/kvm_main.c static void hardware_disable_all(void) · line 5109
Quoted 7 lines
virt/kvm/kvm_main.c +7
-0+static void kvm_del_vm (void ) +{ +cpus_read_lock (); +mutex_lock (&kvm_lock); +kvm_arch_del_vm (kvm_usage_count); +mutex_unlock (&kvm_lock); +cpus_read_unlock ();
kvm_arch_del_vm() is an empty function now. I don't understand why cpus_read_lock() is needed. Maybe add the lock when you fill up kvm_arch_del_vm().
Re: [PATCH v4 15/26] KVM: kvm_arch.c: Remove _nolock post fix
In reply to [email protected]Quoted 7 lines · On Thu, Sep 08, 2022 at 04:25:31PM -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Now all related callbacks are called under
kvm_lock, no point for_nolockpost fix. Remove_nolockpost fix for readability with shorter function names.Signed-off-by Isaku Yamahata <[email protected]>
Re: [PATCH v4 16/26] KVM: kvm_arch.c: Remove a global variable, hardware_enable_failed
In reply to [email protected]Quoted 7 lines · On Thu, Sep 08, 2022 at 04:25:32PM -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
A global variable
hardware_enable_failedinkvm_arch.cis used only bykvm_arch_add_vm()andhardware_enable(). It doesn't have to be a global variable. Make it function local.Signed-off-by Isaku Yamahata <[email protected]>
Quoted 11 lines
---
virt/kvm/kvm_arch.c| 62 ++++++++++++++++++++++-----------------------1 file changed, 31 insertions(+), 31 deletions(-)virt/kvm/kvm_arch.c +0
-0@@ -13,14 +13,13 @@#include <linux/kvm_host.h>
virt/kvm/kvm_arch.c line 22
Quoted 10 lines
virt/kvm/kvm_arch.c +1
-2static cpumask_t cpus_hardware_enabled =CPU_MASK_NONE ; -static atomic_t hardware_enable_failed;__weak int kvm_arch_post_init_vm (struct kvm *kvm) {return 0 ; } -static void hardware_enable (void *caller_name) +static int __hardware_enable (void *caller_name)
nit: caller_name can be dropped and use __builtin_return_address(0) instead.
Quoted 36 lines
virt/kvm/kvm_arch.c +13
-9{int cpu =raw_smp_processor_id ();int r;@@ -28,18 +27,22 @@ static void hardware_enable(void *caller_name)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 %s()\n" , cpu, (const char *)caller_name); - } +else +cpumask_set_cpu (cpu, &cpus_hardware_enabled); +return r; +} + +static void hardware_enable (void *arg) +{ +atomic_t *failed = arg; + +if (__hardware_enable ((void *)__func__)) +atomic_inc (failed); }static void hardware_disable (void *junk)
virt/kvm/kvm_arch.c __weak void kvm_arch_pre_hardware_unsetup(void) · line 71
Quoted 4 lines
virt/kvm/kvm_arch.c +1
-0@@ -65,15 +68,16 @@ __weak void kvm_arch_pre_hardware_unsetup(void)*/ __weak int kvm_arch_add_vm (struct kvm *kvm,int usage_count) { +atomic_t failed;
nit:
atomic_t failed = ATOMIC_INIT(0);
then you can drop the atomic_set() below.
Quoted 21 lines
virt/kvm/kvm_arch.c +4
-4int r =0 ;if (usage_count !=1 )return 0 ; -atomic_set (&hardware_enable_failed,0 ); -on_each_cpu (hardware_enable, (void *)__func__,1 ); +atomic_set (&failed,0 ); +on_each_cpu (hardware_enable, &failed,1 ); -if (atomic_read (&hardware_enable_failed)) { +if (atomic_read (&failed)) { r = -EBUSY ; goto err; }@@ -96,33 +100,29 @@ __weak int kvm_arch_del_vm(int usage_count)__weak int kvm_arch_online_cpu (unsigned int cpu,int usage_count) { -int ret =0 ; +int ret;
virt/kvm/kvm_arch.c __weak int kvm_arch_del_vm(int usage_count) · line 113
Quoted 10 lines
virt/kvm/kvm_arch.c +5
-0ret =kvm_arch_check_processor_compat ();if (ret)return ret; +if (!usage_count) +return 0 ; + +/* +* arch callback kvm_arch_hardware_eanble() assumes that
^ enable
Re: [PATCH v4 17/26] KVM: Introduce a arch wrapper to check all processor compatibility
In reply to [email protected]Quoted 11 lines · On Thu, Sep 08, 2022 at 04:25:33PM -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Introduce a 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.
Quoted 6 lines
Eventually feature check should be pushed down into arch callback, (
kvm_arch_hardware_setup(),kvm_arch_online_cpu(), andkvm_arch_resume()), the two compatibility check, kvm_arch_check_processor_compat{,_all}(), will be eliminated. This is a transitional step for it.Signed-off-by Isaku Yamahata <[email protected]>
Quoted 1 line
+__weak int
kvm_arch_check_processor_compat_all(void)
nit: int __weak
Re: [PATCH v4 23/26] RFC: KVM: powerpc: Move processor compatibility check to hardware setup
In reply to Christophe LeroyQuoted 43 lines · Christophe Leroy
Le 09/09/2022 à 01:25, [email protected] a écrit :
Quoted 25 lines
[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]
From: Isaku Yamahata <[email protected]>
Move processor compatibility check from
kvm_arch_processor_compat()intokvm_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.Suggested-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Cc Fabiano Rosas <[email protected]>--- arch/powerpc/kvm/powerpc.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)arch/powerpc/kvm/powerpc.c +0
-0@@ -444,12 +444,21 @@ 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 455
Quoted 10 lines
arch/powerpc/kvm/powerpc.c +9
-1-return 0 ; +/* +* kvmppc_core_check_processor_compat() checks the global variable. +* No point to check on all processors or at runtime. +* 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") +*/ This explanation shouldn't be in the code. The content of other file may change in the future, the files may be renamed or deleted, new files may be added. And there is no added value with that comment.
Quoted 1 line
That detailed explanation should go in the commit message.
Ok, will move the comment into the commit message.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v4 09/26] KVM: Do processor compatibility check on resume
In reply to Chao GaoQuoted 10 lines · Chao Gao
Quoted 5 lines · On Thu, Sep 08, 2022 at 04:25:25PM -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
So far the processor compatibility check is not done on resume. It should be done. The resume is called for resuming from S3/S4. CPUs can be replaced or the kernel can resume from S4 on a different machine. So
Are they valid cases handled by other kernel components? Trying to handle them in KVM only doesn't help as kernel is likely to fail somewhere else.
I guess no. Actually without the compatibility check, kvm_resume() tries to enable it by calling hardware_enable_nolock(). The real effect on error case of this patch is to print out (potentially) more helpful message gracefully by check_processor_compatibility instead of error message of haredware_enable.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v4 10/26] KVM: Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock
In reply to Chao GaoQuoted 13 lines · Chao Gao
Review
Quoted 9 lines
virt/kvm/kvm_main.c +7
-0+* cpu_hotplug_disable() and other CPUs are offlined. No need for +* locking. +*/ +lockdep_assert_not_held (&kvm_lock); + +if (kvm_usage_count) { +preempt_disable ();hardware_disable_nolock(NULL);(quoted diff) +1
-0+ preempt_enable();
kvm_suspend()is called with interrupt disabled. So, no need to disable preemption.
Quoted 6 lines
/**
*syscore_suspend- Execute all the registered system core suspend callbacks. *
* This function is executed with one CPU on-line and disabled interrupts. */
intsyscore_suspend(void)
Thanks, I'll fix it with a comment.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v4 11/26] KVM: Add arch hooks for PM events with empty stub
In reply to Chao GaoQuoted 24 lines · Chao Gao
Quoted 23 lines · On Thu, Sep 08, 2022 at 04:25:27PM -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Add arch hooks for reboot, suspend, resume, and CPU-online/offline events with empty stub functions.
Suggested-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>---
include/linux/kvm_host.h | 6 +++++ virt/kvm/Makefile.kvm | 2 +- virt/kvm/kvm_arch.c | 44 ++++++++++++++++++++++++++++++ virt/kvm/kvm_main.c | 58 +++++++++++++++++++++++++--------------- 4 files changed, 88 insertions(+), 22 deletions(-)create mode 100644
virt/kvm/kvm_arch.cinclude/linux/kvm_host.h +0
-0@@ -1448,6 +1448,12 @@ int kvm_arch_post_init_vm(struct kvm *kvm);void kvm_arch_pre_destroy_vm (struct kvm *kvm);int kvm_arch_create_vm_debugfs (struct kvm *kvm);
Quoted 7 lines
include/linux/kvm_host.h int kvm_arch_post_init_vm(struct kvm *kvm); · line 1455
Quoted 5 lines
include/linux/kvm_host.h +5
-0+int kvm_arch_suspend (int usage_count); +void kvm_arch_resume (int usage_count); +int kvm_arch_reboot (int val); +int kvm_arch_online_cpu (unsigned int cpu,int usage_count); +int kvm_arch_offline_cpu (unsigned int cpu,int usage_count);Why not extract each of them with one separate patch?
Do you mean one patch for each arch callback? They are convoluted. See the comment below.
Quoted 20 lines
Quoted 20 lines
virt/kvm/kvm_arch.c new file +14
-0@@ -0,0 +1,44 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* kvm_arch.c: kvm default arch hooks for hardware enabling/disabling +* Copyright (c) 2022 Intel Corporation. +* +* Author: +* Isaku Yamahata <[email protected]> +* <[email protected]> +*/ + +#include <linux/kvm_host.h> + +/* +* Called after the VM is otherwise initialized, but just before adding it to
Quoted 8 lines
virt/kvm/kvm_arch.c line 20
Quoted 6 lines
virt/kvm/kvm_arch.c new file +6
-0+* the vm_list. +*/ +__weak int kvm_arch_post_init_vm (struct kvm *kvm) +{ +return 0 ; +}use "int
__weak" to comply with kernel's convension.
Will fix.
Quoted 12 lines
Quoted 12 lines
static int
kvm_offline_cpu(unsigned int cpu)
{virt/kvm/kvm_arch.c new file +2
-0+int ret =0 ; +mutex_lock(&kvm_lock); if (kvm_usage_count) { /*virt/kvm/kvm_arch.c new file +1
-0@@ -5069,10 +5067,15 @@ static int kvm_offline_cpu(unsigned int cpu)*/ preempt_disable ();hardware_disable_nolock (NULL ); + ret =kvm_arch_offline_cpu (cpu, kvm_usage_count);
Quoted 8 lines
virt/kvm/kvm_arch.c static int kvm_offline_cpu(unsigned int cpu) · line 5073
Quoted 3 lines
virt/kvm/kvm_arch.c new file +3
-0+if (ret) { + (void )hardware_enable_nolock (NULL ); +atomic_set (&hardware_enable_failed,0 );The error-handling code ignores hardware enabling failure which looks weird to me. If you extract
kvm_arch_offline_cpu()directly like what you do in patch 14 (rather than add a stub function first and then move some code to the stub function), the error-handling code isn't needed.
I did it for x86 tsc fix. It relates to suspend/resume. I would split those
- introduce suspend/resuem/reboot arch hooks
- fix x86 tsc issue
- move
- introduce cpu online/offline arch hooks
- move out PM hooks. probably this can be combined into the previous one.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v4 12/26] KVM: x86: Move TSC fixup logic to KVM arch resume callback
In reply to Chao GaoQuoted 9 lines · Chao Gao
Quoted 7 lines · On Thu, Sep 08, 2022 at 04:25:28PM -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
commit
0dd6a6edb012("KVM: Dont mark TSC unstable due to S4 suspend") made use ofkvm_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 tokvm_arch_resume()callback.
Quoted 3 lines
IIUC,
kvm_arch_resume()is called on the first CPU waking up from suspension. But the detection was done on every CPU. Is it a problem (i.e., we fail to detect TSC goes backward on some CPUs)?
The problem is, TSC fixup logic is only needed once on resuming.
The current code calls TSC fixup logic on each cpu onlining. It's quick plumbing the logic to each cpu online. Although it won't harm to call the logic, it's ugly and this time is good occasion to clean it up.
I will clarify the commit message.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v4 13/26] KVM: Add arch hook when VM is added/deleted
In reply to Chao GaoQuoted 11 lines · Chao Gao
virt/kvm/kvm_main.c static void hardware_disable_all(void) · line 5109
Quoted 7 lines
virt/kvm/kvm_main.c +7
-0+static void kvm_del_vm (void ) +{ +cpus_read_lock (); +mutex_lock (&kvm_lock); +kvm_arch_del_vm (kvm_usage_count); +mutex_unlock (&kvm_lock); +cpus_read_unlock ();
kvm_arch_del_vm()is an empty function now. I don't understand whycpus_read_lock()is needed. Maybe add the lock when you fill upkvm_arch_del_vm().
The next patch populates it with on_each_cpu(hardware_disable_nolock, NULL, 1); I will move adding lock to the next one. Or revise how adding arch hooks and populating part.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v4 16/26] KVM: kvm_arch.c: Remove a global variable, hardware_enable_failed
In reply to Chao GaoQuoted 15 lines · Chao Gao
Quoted 15 lines
virt/kvm/kvm_arch.c +0
-1@@ -13,14 +13,13 @@#include <linux/kvm_host.h> static cpumask_t cpus_hardware_enabled =CPU_MASK_NONE ; -static atomic_t hardware_enable_failed;__weak int kvm_arch_post_init_vm (struct kvm *kvm) {return 0 ; }
Quoted 4 lines
virt/kvm/kvm_arch.c line 22
Quoted 2 lines
virt/kvm/kvm_arch.c +1
-1-static void hardware_enable (void *caller_name) +static int __hardware_enable (void *caller_name)nit:
caller_namecan be dropped and use__builtin_return_address(0)instead.
Do you want to update your patch
"KVM: Provide more information in kernel log if hardware enabling fails"?
Signature · 1 line
Isaku Yamahata <[email protected]>
Marc Zyngier
maintainerRe: [PATCH v4 06/26] KVM: arm64: Simplify the CPUHP logic
In reply to [email protected]Quoted 10 lines · On Fri, 09 Sep 2022 00:25:22 +0100, [email protected] wrote:
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.
Quoted 7 lines
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.
Signed-off-by Marc Zyngier <[email protected]>Signed-off-by Chao Gao <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>
Since this patch is substantially different from the version pointed to on the list, please drop Chao's SoB and the Link: tag, as they are not relevant anymore.
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.