KVM hardware enable/disable reorganize
Subsystems (15)
v2 cover KVM hardware enable/disable reorganize
From: Isaku Yamahata <[email protected]>
This patch series is to implement the suggestion by Sean Christopherson [1] to reorganize enable/disable cpu virtualization feature by replacing the arch-generic current enable/disable logic with PM related hooks. And convert kvm/x86 to use new hooks.
- 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 cpus_hardware_enabled. 17/18 and 18/18
[1] https://lore.kernel.org/kvm/[email protected]/
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 (3):
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
Isaku Yamahata (16):
KVM: x86: Drop kvm_user_return_msr_cpu_online()
KVM: x86: Use this_cpu_ptr() instead of
per_cpu_ptr(smp_processor_id())
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: Do processor compatibility check on cpu online and resume
KVM: x86: Duplicate arch callbacks related to pm events
KVM: Eliminate kvm_arch_post_init_vm()
KVM: x86: Delete kvm_arch_hardware_enable/disable()
KVM: Add config to not compile kvm_arch.c
RFC: KVM: x86: Remove cpus_hardware_enabled and related sanity check
RFC: KVM: Remove cpus_hardware_enabled and related sanity check
Documentation/virt/kvm/locking.rst | 14 +-- 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/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 | 14 +-- arch/x86/kvm/x86.c | 192 +++++++++++++++++++++------- include/linux/cpuhotplug.h | 2 +- include/linux/kvm_host.h | 14 ++- virt/kvm/Kconfig | 3 + virt/kvm/Makefile.kvm | 3 + virt/kvm/kvm_arch.c | 126 +++++++++++++++++++ virt/kvm/kvm_main.c | 193 +++++++++-------------------- 18 files changed, 373 insertions(+), 206 deletions(-) create mode 100644 virt/kvm/kvm_arch.c
Signature · 1 line
2.25.1
PATCH v2 1/19 KVM: x86: Drop kvm_user_return_msr_cpu_online()
From: Isaku Yamahata <[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 | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-)
arch/x86/kvm/x86.c +24 -19
@@ -200,6 +200,7 @@ struct kvm_user_return_msrs {struct kvm_user_return_msr_values {u64 host;u64 curr; +bool initialized; } values[KVM_MAX_NR_USER_RETURN_MSRS ]; };
@@ -363,6 +364,10 @@ static void kvm_on_user_return(struct user_return_notifier *urn)local_irq_restore (flags);for (slot =0 ; slot < kvm_nr_uret_msrs; ++slot) { values = &msrs->values[slot]; +/* +* No need to check values->initialized because host = curr = 0 +* by __GFP_ZERO when !values->initialized. +*/ if (values->host != values->curr) {wrmsrl (kvm_uret_msrs_list[slot], values->host); values->curr = values->host;
@@ -409,34 +414,30 @@ 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 ) -{ -unsigned int cpu =smp_processor_id (); -struct kvm_user_return_msrs *msrs =per_cpu_ptr (user_return_msrs, cpu); -u64 value; -int i; - -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; - } -} -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_msr_values *values = &msrs->values[slot];int err; - value = (value & mask) | (msrs->values[slot].host & ~mask); -if (value == msrs->values[slot].curr) +if (unlikely (!values->initialized)) { +u64 host_value; + +rdmsrl_safe (kvm_uret_msrs_list[slot], &host_value); + values->host = host_value; + values->curr = host_value; + values->initialized = true; + } + + value = (value & mask) | (values->host & ~mask); +if (value == values->curr)return 0 ; err =wrmsrl_safe (kvm_uret_msrs_list[slot], value);if (err)return 1 ; - msrs->values[slot].curr = value; + values->curr = value;if (!msrs->registered) { msrs->urn.on_user_return = kvm_on_user_return;user_return_notifier_register (&msrs->urn);
@@ -9212,7 +9213,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.values[].{host, curr} match. +* See kvm_on_user_return() +*/ + 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 +11842,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
Re: [PATCH v2 01/19] KVM: x86: Drop kvm_user_return_msr_cpu_online()
Quoted 23 lines · On Tue, Aug 30, 2022 at 05:01:16AM -0700, [email protected] wrote:
From: Isaku Yamahata <[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_enableby capturing the host value on the first change of the MSR value to guest VM instead of CPU online.Suggested-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>---
arch/x86/kvm/x86.c| 43 ++++++++++++++++++++++++-------------------1 file changed, 24 insertions(+), 19 deletions(-)arch/x86/kvm/x86.c +0
-0
arch/x86/kvm/x86.c struct kvm_user_return_msrs { · line 204
Quoted 5 lines
arch/x86/kvm/x86.c +1
-0@@ -200,6 +200,7 @@ struct kvm_user_return_msrs {struct kvm_user_return_msr_values {u64 host;u64 curr; +bool initialized; } values[KVM_MAX_NR_USER_RETURN_MSRS ];
The benefit of having an "initialized" state for each user return MSR on each CPU is small. A per-cpu state looks suffice. With it, you can keep kvm_user_return_msr_cpu_online() and simply call the function from kvm_set_user_return_msr() if initialized is false on current CPU.
Sean Christopherson
maintainerRe: [PATCH v2 01/19] KVM: x86: Drop kvm_user_return_msr_cpu_online()
Quoted 19 lines · Chao Gao
Quoted 18 lines · On Tue, Aug 30, 2022 at 05:01:16AM -0700, [email protected] wrote:
From: Isaku Yamahata <[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_enableby capturing the host value on the first change of the MSR value to guest VM instead of CPU online.Suggested-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>---
arch/x86/kvm/x86.c| 43 ++++++++++++++++++++++++-------------------1 file changed, 24 insertions(+), 19 deletions(-)
Quoted 15 lines
arch/x86/kvm/x86.c struct kvm_user_return_msrs { · line 204
Quoted 10 lines
arch/x86/kvm/x86.c +1
-0@@ -200,6 +200,7 @@ struct kvm_user_return_msrs {struct kvm_user_return_msr_values {u64 host;u64 curr; +bool initialized; } values[KVM_MAX_NR_USER_RETURN_MSRS ];The benefit of having an "initialized" state for each user return MSR on each CPU is small. A per-cpu state looks suffice. With it, you can keep
kvm_user_return_msr_cpu_online()and simply call the function fromkvm_set_user_return_msr()if initialized is false on current CPU.
Yep, a per-CPU flag is I intended. This is the completely untested patch that's sitting in a development branch of mine.
--- arch/x86/kvm/x86.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
arch/x86/kvm/x86.c +8 -4
@@ -194,6 +194,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;
@@ -400,18 +401,20 @@ int kvm_find_user_return_msr(u32 msr)return -1 ; } -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)
@@ -420,6 +423,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 ;
@@ -11740,7 +11745,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;
base-commit: a8f21d1980fbd7e877ed174142f7f572d547e611
Re: [PATCH v2 01/19] KVM: x86: Drop kvm_user_return_msr_cpu_online()
Quoted 36 lines · Sean Christopherson
Quoted 19 lines · Chao Gao
Quoted 18 lines · On Tue, Aug 30, 2022 at 05:01:16AM -0700, [email protected] wrote:
From: Isaku Yamahata <[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_enableby capturing the host value on the first change of the MSR value to guest VM instead of CPU online.Suggested-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>---
arch/x86/kvm/x86.c| 43 ++++++++++++++++++++++++-------------------1 file changed, 24 insertions(+), 19 deletions(-)Quoted 15 lines
arch/x86/kvm/x86.c struct kvm_user_return_msrs { · line 204
Quoted 10 lines
arch/x86/kvm/x86.c +1
-0@@ -200,6 +200,7 @@ struct kvm_user_return_msrs {struct kvm_user_return_msr_values {u64 host;u64 curr; +bool initialized; } values[KVM_MAX_NR_USER_RETURN_MSRS ];The benefit of having an "initialized" state for each user return MSR on each CPU is small. A per-cpu state looks suffice. With it, you can keep
kvm_user_return_msr_cpu_online()and simply call the function fromkvm_set_user_return_msr()if initialized is false on current CPU.
Quoted 2 lines
Yep, a per-CPU flag is I intended. This is the completely untested patch that's sitting in a development branch of mine.
With the following fix, it worked. I'll replace this patch with yours.
arch/x86/kvm/x86.c +6 -1
@@ -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.values[].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 ;
Signature · 1 line
Isaku Yamahata <[email protected]>
PATCH v2 2/19 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
@@ -416,8 +416,7 @@ EXPORT_SYMBOL_GPL(kvm_find_user_return_msr);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);struct kvm_user_return_msr_values *values = &msrs->values[slot];int err;
@@ -449,8 +448,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
Re: [PATCH v2 02/19] KVM: x86: Use this_cpu_ptr() instead of per_cpu_ptr(smp_processor_id())
Quoted 6 lines · On Tue, Aug 30, 2022 at 05:01:17AM -0700, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
convert per_cpu_ptr(smp_processor_id()) to
this_cpu_ptr()as trivial cleanup.Signed-off-by Isaku Yamahata <[email protected]>
Quoted 31 lines
---
arch/x86/kvm/x86.c| 6 ++----1 file changed, 2 insertions(+), 4 deletions(-)arch/x86/kvm/x86.c +2
-4@@ -416,8 +416,7 @@ EXPORT_SYMBOL_GPL(kvm_find_user_return_msr);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);struct kvm_user_return_msr_values *values = &msrs->values[slot];int err;@@ -449,8 +448,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 v2 3/19 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
@@ -11997,7 +11997,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 ());
@@ -12005,7 +12004,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 v2 4/19 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. The change about kvm_arch_hardware_setup() in original commit are still needed so they are not reverted.
The current implementation enables hardware (e.g. enable VMX on all CPUs), arch-specific initialization for the first VM creation, and disables hardware (in x86, disable VMX on all CPUs) for last VM destruction.
To support TDX, hardware_enable_all() will be done during module loading time. As a result, CPU compatibility check will be opportunistically moved to hardware_enable_nolock(), which doesn't take any argument. Instead of passing 'opaque' around to hardware_enable_nolock() and hardware_enable_all(), just remove the unused 'opaque' argument from kvm_arch_check_processor_compat().
--- 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
@@ -11994,7 +11994,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
Re: [PATCH v2 04/19] Partially revert "KVM: Pass kvm_init()'s opaque param to additional arch funcs"
Quoted 13 lines · On Tue, 2022-08-30 at 05:01 -0700, [email protected] wrote:
From: Chao Gao <[email protected]>
This partially reverts commit
b99040853738("KVM: Pass kvm_init()'s opaque param to additional arch funcs") remove opaque fromkvm_arch_check_processor_compatbecause no one uses this opaque now. Address conflicts for ARM (due to file movement) and manually handle RISC-V which comes after the commit. The change aboutkvm_arch_hardware_setup()in original commit are still needed so they are not reverted.The current implementation enables hardware (e.g. enable VMX on all CPUs), arch-specific initialization for the first VM creation, and disables hardware (in x86, disable VMX on all CPUs) for last VM destruction.
Quoted 6 lines
To support TDX,
hardware_enable_all()will be done during module loading time. As a result, CPU compatibility check will be opportunistically moved tohardware_enable_nolock(), which doesn't take any argument. Instead of passing 'opaque' around tohardware_enable_nolock()andhardware_enable_all(), just remove the unused 'opaque' argument fromkvm_arch_check_processor_compat().
This patch now is not part of TDX's series, so it doesn't make a lot sense to put the last two paragraphs here (because the purpose is different). I think you can just use Chao's original patch.
Quoted 152 lines
Signed-off-by Chao Gao <[email protected]>Reviewed-by Sean Christopherson <[email protected]>Reviewed-by Suzuki K Poulose <[email protected]>Acked-by Anup Patel <[email protected]>Acked-by Claudio Imbrenda <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Reviewed-by Kai Huang <[email protected]>--- 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@@ -11994,7 +11994,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; }
Re: [PATCH v2 04/19] Partially revert "KVM: Pass kvm_init()'s opaque param to additional arch funcs"
Quoted 21 lines · Kai
Quoted 13 lines · On Tue, 2022-08-30 at 05:01 -0700, [email protected] wrote:
From: Chao Gao <[email protected]>
This partially reverts commit
b99040853738("KVM: Pass kvm_init()'s opaque param to additional arch funcs") remove opaque fromkvm_arch_check_processor_compatbecause no one uses this opaque now. Address conflicts for ARM (due to file movement) and manually handle RISC-V which comes after the commit. The change aboutkvm_arch_hardware_setup()in original commit are still needed so they are not reverted.The current implementation enables hardware (e.g. enable VMX on all CPUs), arch-specific initialization for the first VM creation, and disables hardware (in x86, disable VMX on all CPUs) for last VM destruction.
Quoted 6 lines
To support TDX,
hardware_enable_all()will be done during module loading time. As a result, CPU compatibility check will be opportunistically moved tohardware_enable_nolock(), which doesn't take any argument. Instead of passing 'opaque' around tohardware_enable_nolock()andhardware_enable_all(), just remove the unused 'opaque' argument fromkvm_arch_check_processor_compat().
Quoted 3 lines
This patch now is not part of TDX's series, so it doesn't make a lot sense to put the last two paragraphs here (because the purpose is different). I think you can just use Chao's original patch.
Ok.
Signature · 1 line
Isaku Yamahata <[email protected]>
PATCH v2 5/19 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 ,CPUHP_AP_KVM_ARM_VGIC_INIT_STARTING ,CPUHP_AP_KVM_ARM_VGIC_STARTING , CPUHP_AP_KVM_ARM_TIMER_STARTING,
@@ -203,6 +202,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
@@ -5010,13 +5010,27 @@ static void hardware_enable_nolock(void *junk) } } -static int kvm_starting_cpu (unsigned int cpu) +static int kvm_online_cpu (unsigned int cpu) { +int ret =0 ; +raw_spin_lock (&kvm_count_lock); -if (kvm_usage_count) +/* +* Abort the CPU online process if hardware virtualization cannot +* be enabled. Otherwise running VMs would encounter unrecoverable +* errors when scheduled to this CPU. +*/ +if (kvm_usage_count) { +WARN_ON_ONCE (atomic_read (&hardware_enable_failed)); +hardware_enable_nolock (NULL ); +if (atomic_read (&hardware_enable_failed)) { +atomic_set (&hardware_enable_failed,0 ); + ret = -EIO ; + } + }raw_spin_unlock (&kvm_count_lock); -return 0 ; +return ret; }static void hardware_disable_nolock (void *junk)
@@ -5029,7 +5043,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)
@@ -5840,8 +5854,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);
@@ -5902,7 +5916,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:
@@ -5928,7 +5942,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
Re: [PATCH v2 05/19] KVM: Rename and move CPUHP_AP_KVM_STARTING to ONLINE section
Quoted 15 lines · On Tue, Aug 30, 2022 at 05:01:20AM -0700, [email protected] wrote:
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_EMPTYas 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.
Quoted 5 lines
KVM's CPU hotplug callbacks are renamed as well.
Suggested-by Thomas Gleixner <[email protected]>Signed-off-by Chao Gao <[email protected]>
Note that Sean gave his Reviewed-by for KVM changes.
Re: [PATCH v2 05/19] KVM: Rename and move CPUHP_AP_KVM_STARTING to ONLINE section
In reply to [email protected]Quoted 15 lines · On Tue, Aug 30, 2022 at 05:01:20AM -0700, [email protected] wrote:
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_EMPTYas 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.
Quoted 4 lines
KVM's CPU hotplug callbacks are renamed as well.
Suggested-by Thomas Gleixner <[email protected]>Signed-off-by Chao Gao <[email protected]>
Isaku, your signed-off-by is missing.
Quoted 11 lines
---
include/linux/cpuhotplug.h| 2 +-virt/kvm/kvm_main.c| 30 ++++++++++++++++++++++--------2 files changed, 23 insertions(+), 9 deletions(-)include/linux/cpuhotplug.h +0
-0
include/linux/cpuhotplug.h enum cpuhp_state { · line 190
Quoted 7 lines
include/linux/cpuhotplug.h +0
-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 ,CPUHP_AP_KVM_ARM_VGIC_INIT_STARTING ,CPUHP_AP_KVM_ARM_VGIC_STARTING , CPUHP_AP_KVM_ARM_TIMER_STARTING,
The movement of CPUHP_AP_KVM_STARTING changes the ordering between CPUHP_AP_KVM_STARTING and CPUHP_AP_KVM_ARM_* above [1]. We need the patch [2] from Marc to avoid breaking ARM.
[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
Re: [PATCH v2 05/19] KVM: Rename and move CPUHP_AP_KVM_STARTING to ONLINE section
Quoted 46 lines · Chao
Quoted 15 lines · On Tue, Aug 30, 2022 at 05:01:20AM -0700, [email protected] wrote:
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_EMPTYas 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.Quoted 4 lines
KVM's CPU hotplug callbacks are renamed as well.
Suggested-by Thomas Gleixner <[email protected]>Signed-off-by Chao Gao <[email protected]>Isaku, your signed-off-by is missing.
Quoted 11 lines
---
include/linux/cpuhotplug.h| 2 +-virt/kvm/kvm_main.c| 30 ++++++++++++++++++++++--------2 files changed, 23 insertions(+), 9 deletions(-)include/linux/cpuhotplug.h +0
-0include/linux/cpuhotplug.h enum cpuhp_state { · line 190
Quoted 7 lines
include/linux/cpuhotplug.h +0
-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 ,CPUHP_AP_KVM_ARM_VGIC_INIT_STARTING ,CPUHP_AP_KVM_ARM_VGIC_STARTING , CPUHP_AP_KVM_ARM_TIMER_STARTING,The movement of
CPUHP_AP_KVM_STARTINGchanges the ordering betweenCPUHP_AP_KVM_STARTINGand CPUHP_AP_KVM_ARM_* above [1]. We need the patch [2] from Marc to avoid breaking ARM.
Quoted 2 lines
[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
How about Isaku just to take your series directly (+his SoB) and add additional patches?
Signature · 2 lines
Thanks, -Kai
Re: [PATCH v2 05/19] KVM: Rename and move CPUHP_AP_KVM_STARTING to ONLINE section
Quoted 47 lines · Kai
Quoted 46 lines · Chao
Quoted 15 lines · On Tue, Aug 30, 2022 at 05:01:20AM -0700, [email protected] wrote:
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_EMPTYas 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.Quoted 4 lines
KVM's CPU hotplug callbacks are renamed as well.
Suggested-by Thomas Gleixner <[email protected]>Signed-off-by Chao Gao <[email protected]>Isaku, your signed-off-by is missing.
Quoted 11 lines
---
include/linux/cpuhotplug.h| 2 +-virt/kvm/kvm_main.c| 30 ++++++++++++++++++++++--------2 files changed, 23 insertions(+), 9 deletions(-)include/linux/cpuhotplug.h +0
-0include/linux/cpuhotplug.h enum cpuhp_state { · line 190
Quoted 7 lines
include/linux/cpuhotplug.h +0
-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 ,CPUHP_AP_KVM_ARM_VGIC_INIT_STARTING ,CPUHP_AP_KVM_ARM_VGIC_STARTING , CPUHP_AP_KVM_ARM_TIMER_STARTING,The movement of
CPUHP_AP_KVM_STARTINGchanges the ordering betweenCPUHP_AP_KVM_STARTINGand CPUHP_AP_KVM_ARM_* above [1]. We need the patch [2] from Marc to avoid breaking ARM.
Quoted 5 lines
Quoted 2 lines
[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/How about Isaku just to take your series directly (+his SoB) and add additional patches?
Ok will do. Although I hoped to slim it down, I've ended up to take most of it. four out of six. Now why not two more.
Signature · 1 line
Isaku Yamahata <[email protected]>
PATCH v2 6/19 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.
Opportunistically add some comments on locking.
--- Documentation/virt/kvm/locking.rst | 14 +++++------- virt/kvm/kvm_main.c | 36 +++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 20 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 +25 -11
@@ -100,7 +100,6 @@ EXPORT_SYMBOL_GPL(halt_poll_ns_shrink);*/ DEFINE_MUTEX (kvm_lock); -static DEFINE_RAW_SPINLOCK (kvm_count_lock);LIST_HEAD (vm_list);static cpumask_var_t cpus_hardware_enabled;
@@ -4996,6 +4995,8 @@ static void hardware_enable_nolock(void *junk)int cpu =raw_smp_processor_id ();int r; +WARN_ON_ONCE (preemptible ()); +if (cpumask_test_cpu (cpu, cpus_hardware_enabled))return ;
@@ -5014,7 +5015,7 @@ static int kvm_online_cpu(unsigned int cpu) {int ret =0 ; -raw_spin_lock (&kvm_count_lock); +mutex_lock (&kvm_lock);/* * Abort the CPU online process if hardware virtualization cannot * be enabled. Otherwise running VMs would encounter unrecoverable
@@ -5029,7 +5030,7 @@ static int kvm_online_cpu(unsigned int cpu) ret = -EIO ; } } -raw_spin_unlock (&kvm_count_lock); +mutex_unlock (&kvm_lock);return ret; }
@@ -5037,6 +5038,8 @@ static void hardware_disable_nolock(void *junk) {int cpu =raw_smp_processor_id (); +WARN_ON_ONCE (preemptible ()); +if (!cpumask_test_cpu (cpu, cpus_hardware_enabled))return ;cpumask_clear_cpu (cpu, cpus_hardware_enabled);
@@ -5045,10 +5048,10 @@ static void hardware_disable_nolock(void *junk)static int kvm_offline_cpu (unsigned int cpu) { -raw_spin_lock (&kvm_count_lock); +mutex_lock (&kvm_lock);if (kvm_usage_count)hardware_disable_nolock (NULL ); -raw_spin_unlock (&kvm_count_lock); +mutex_unlock (&kvm_lock);return 0 ; }
@@ -5063,16 +5066,19 @@ static void hardware_disable_all_nolock(void)static void hardware_disable_all (void ) { -raw_spin_lock (&kvm_count_lock); +cpus_read_lock (); +mutex_lock (&kvm_lock);hardware_disable_all_nolock (); -raw_spin_unlock (&kvm_count_lock); +mutex_unlock (&kvm_lock); +cpus_read_unlock (); }static int hardware_enable_all (void ) {int r =0 ; -raw_spin_lock (&kvm_count_lock); +cpus_read_lock (); +mutex_lock (&kvm_lock); kvm_usage_count++;if (kvm_usage_count ==1 ) {
@@ -5085,7 +5091,8 @@ static int hardware_enable_all(void) } } -raw_spin_unlock (&kvm_count_lock); +mutex_unlock (&kvm_lock); +cpus_read_unlock ();return r; }
@@ -5691,15 +5698,22 @@ static void kvm_init_debug(void)static int kvm_suspend (void ) { -if (kvm_usage_count) +/* +* The caller ensures that CPU hotlug is disabled by +* cpu_hotplug_disable() and other CPUs are offlined. No need for +* locking. +*/ +if (kvm_usage_count) { +lockdep_assert_not_held (&kvm_lock);hardware_disable_nolock (NULL ); + }return 0 ; }static void kvm_resume (void ) {if (kvm_usage_count) { -lockdep_assert_not_held (&kvm_count_lock); +lockdep_assert_not_held (&kvm_lock);hardware_enable_nolock (NULL ); } }
Signature · 1 line
2.25.1
PATCH v2 7/19 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 | 51 +++++++++++++++++++++++++--------------- 4 files changed, 83 insertions(+), 20 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 +32 -19
103 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v2 8/19 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 | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-)
arch/x86/kvm/x86.c +16 -11
@@ -11831,18 +11831,30 @@ 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 ; + +if (kvm_arch_hardware_enable ()) +return ; local_tsc =rdtsc (); stable = !kvm_check_tsc_unstable ();
@@ -11917,13 +11929,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 v2 9/19 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); }
@@ -5078,6 +5082,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 v2 10/19 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.
--- include/linux/kvm_host.h | 1 + virt/kvm/kvm_arch.c | 91 +++++++++++++++++++++++- virt/kvm/kvm_main.c | 145 ++++----------------------------------- 3 files changed, 104 insertions(+), 133 deletions(-)
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 +89 -2
138 lines not on this page · raw message
virt/kvm/kvm_main.c +14 -131
261 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v2 11/19 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 short function name.
--- 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 *junk) +static void hardware_enable (void *junk) {int cpu =raw_smp_processor_id ();int r;
@@ -41,7 +41,7 @@ static void hardware_enable_nolock(void *junk) } } -static void hardware_disable_nolock (void *junk) +static void hardware_disable (void *junk) {int cpu =raw_smp_processor_id ();
@@ -55,7 +55,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 ); }/*
@@ -70,7 +70,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,NULL ,1 ); +on_each_cpu (hardware_enable,NULL ,1 );if (atomic_read (&hardware_enable_failed)) { r = -EBUSY ;
@@ -80,7 +80,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; }
@@ -89,7 +89,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 ; }
@@ -105,7 +105,7 @@ __weak int kvm_arch_online_cpu(unsigned int cpu, int usage_count)if (usage_count) {WARN_ON_ONCE (atomic_read (&hardware_enable_failed)); -hardware_enable_nolock (NULL ); +hardware_enable (NULL );if (atomic_read (&hardware_enable_failed)) {atomic_set (&hardware_enable_failed,0 ); ret = -EIO ;
@@ -117,25 +117,25 @@ __weak int kvm_arch_online_cpu(unsigned int cpu, int usage_count)__weak int kvm_arch_offline_cpu (unsigned int cpu,int usage_count) {if (usage_count) -hardware_disable_nolock (NULL ); +hardware_disable (NULL );return 0 ; }__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 ; }__weak int kvm_arch_suspend (int usage_count) {if (usage_count) -hardware_disable_nolock (NULL ); +hardware_disable (NULL );return 0 ; }__weak void kvm_arch_resume (int usage_count) {if (usage_count) -hardware_enable_nolock (NULL ); +hardware_enable (NULL ); }
Signature · 1 line
2.25.1
PATCH v2 12/19 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 | 56 +++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 30 deletions(-)
virt/kvm/kvm_arch.c +26 -30
@@ -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 *junk) +static int __hardware_enable (void ) {int cpu =raw_smp_processor_id ();int r;
@@ -28,17 +27,21 @@ static void hardware_enable(void *junk)WARN_ON_ONCE (preemptible ());if (cpumask_test_cpu (cpu, &cpus_hardware_enabled)) -return ; - -cpumask_set_cpu (cpu, &cpus_hardware_enabled); - +return 0 ; r =kvm_arch_hardware_enable (); - -if (r) { -cpumask_clear_cpu (cpu, &cpus_hardware_enabled); -atomic_inc (&hardware_enable_failed); +if (r)pr_info ("kvm: enabling virtualization on CPU%d failed\n" , cpu); - } +else +cpumask_set_cpu (cpu, &cpus_hardware_enabled); +return r; +} + +static void hardware_enable (void *arg) +{ +atomic_t *failed = arg; + +if (__hardware_enable ()) +atomic_inc (failed); }static void hardware_disable (void *junk)
@@ -64,15 +67,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,NULL ,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; }
@@ -95,23 +99,15 @@ __weak int kvm_arch_del_vm(int usage_count)__weak int kvm_arch_online_cpu (unsigned int cpu,int usage_count) { -int ret =0 ; - -/* -* 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)); - -hardware_enable (NULL ); -if (atomic_read (&hardware_enable_failed)) { -atomic_set (&hardware_enable_failed,0 ); - ret = -EIO ; - } +/* +* Abort the CPU online process if hardware virtualization cannot +* be enabled. Otherwise running VMs would encounter unrecoverable +* errors when scheduled to this CPU. +*/ +return __hardware_enable (); } -return ret; +return 0 ; }__weak int kvm_arch_offline_cpu (unsigned int cpu,int usage_count)
@@ -137,5 +133,5 @@ __weak int kvm_arch_suspend(int usage_count)__weak void kvm_arch_resume (int usage_count) {if (usage_count) -hardware_enable (NULL ); + (void )__hardware_enable (); }
Signature · 1 line
2.25.1
PATCH v2 13/19 KVM: Do processor compatibility check on cpu online and resume
In reply to [email protected]From: Isaku Yamahata <[email protected]>
So far the processor compatibility check is not done for newly added CPU. It should be done. For online cpu case, the function is called by kernel thread bind to the cpu without irq disabled. So remove WARN_ON(!irq_disabled()).
--- arch/x86/kvm/x86.c | 2 -- virt/kvm/kvm_arch.c | 15 +++++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-)
arch/x86/kvm/x86.c +0 -2
@@ -12003,8 +12003,6 @@ int kvm_arch_check_processor_compat(void) {struct cpuinfo_x86 *c = &cpu_data (smp_processor_id ()); -WARN_ON (!irqs_disabled ()); -if (__cr4_reserved_bits (cpu_has, c) !=__cr4_reserved_bits (cpu_has, &boot_cpu_data))return -EIO ;
virt/kvm/kvm_arch.c +13 -2
@@ -99,6 +99,12 @@ __weak int kvm_arch_del_vm(int usage_count)__weak int kvm_arch_online_cpu (unsigned int cpu,int usage_count) { +int r; + + r =kvm_arch_check_processor_compat (); +if (r) +return r; +if (usage_count) {/* * Abort the CPU online process if hardware virtualization cannot
@@ -132,6 +138,11 @@ __weak int kvm_arch_suspend(int usage_count)__weak void kvm_arch_resume (int usage_count) { -if (usage_count) - (void )__hardware_enable (); +if (kvm_arch_check_processor_compat ()) +return ;/* FIXME: disable KVM */ + +if (!usage_count) +return ; + + (void )__hardware_enable (); }
Signature · 1 line
2.25.1
PATCH v2 14/19 KVM: x86: Duplicate arch callbacks related to pm events
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 | 131 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 126 insertions(+), 5 deletions(-)
arch/x86/kvm/x86.c +126 -5
@@ -11841,6 +11841,130 @@ void kvm_arch_hardware_disable(void)drop_user_return_notifiers (); } +static cpumask_t cpus_hardware_enabled =CPU_MASK_NONE ; + +int kvm_arch_post_init_vm (struct kvm *kvm) +{ +return kvm_mmu_post_init_vm (kvm); +} + +static int __hardware_enable (void ) +{ +int cpu =raw_smp_processor_id (); +int r; + +WARN_ON_ONCE (preemptible ()); + +if (cpumask_test_cpu (cpu, &cpus_hardware_enabled)) +return 0 ; + r =kvm_arch_hardware_enable (); +if (r) +pr_info ("kvm: enabling virtualization on CPU%d failed\n" , cpu); +else +cpumask_set_cpu (cpu, &cpus_hardware_enabled); +return r; +} + +static void hardware_enable (void *arg) +{ +atomic_t *failed = arg; + +if (__hardware_enable ()) +atomic_inc (failed); +} + +static void hardware_disable (void *junk) +{ +int cpu =raw_smp_processor_id (); + +WARN_ON_ONCE (preemptible ()); + +if (!cpumask_test_cpu (cpu, &cpus_hardware_enabled)) +return ; +cpumask_clear_cpu (cpu, &cpus_hardware_enabled); +kvm_arch_hardware_disable (); +} + +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 ; +} + +int kvm_arch_online_cpu (unsigned int cpu,int usage_count) +{ +int r; + + r =kvm_arch_check_processor_compat (); +if (r) +return r; + +if (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. +*/ +return __hardware_enable (); + } +return 0 ; +} + +int kvm_arch_offline_cpu (unsigned int cpu,int usage_count) +{ +if (usage_count) +hardware_disable (NULL ); +return 0 ; +} + +int kvm_arch_reboot (int val) +{ +on_each_cpu (hardware_disable,NULL ,1 ); +return NOTIFY_OK ; +} + +int kvm_arch_suspend (int usage_count) +{ +if (usage_count) +hardware_disable (NULL ); +return 0 ; +} +void kvm_arch_resume (int usage_count) {struct kvm *kvm;
@@ -11853,6 +11977,8 @@ void kvm_arch_resume(int usage_count)if (!usage_count)return ; +if (kvm_arch_check_processor_compat ()) +return ;if (kvm_arch_hardware_enable ())return ;
@@ -12102,11 +12228,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 v2 15/19 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() int 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
@@ -11843,11 +11843,6 @@ void kvm_arch_hardware_disable(void)static cpumask_t cpus_hardware_enabled =CPU_MASK_NONE ; -int kvm_arch_post_init_vm (struct kvm *kvm) -{ -return kvm_mmu_post_init_vm (kvm); -} -static int __hardware_enable (void ) {int cpu =raw_smp_processor_id ();
@@ -11910,7 +11905,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
@@ -1445,7 +1445,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 ) {int cpu =raw_smp_processor_id ();
@@ -78,13 +73,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 v2 16/19 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 | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-)
arch/x86/kvm/x86.c +8 -16
@@ -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) {
@@ -11830,17 +11830,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 )
@@ -11852,7 +11841,7 @@ static int __hardware_enable(void)if (cpumask_test_cpu (cpu, &cpus_hardware_enabled))return 0 ; - r =kvm_arch_hardware_enable (); + r =static_call (kvm_x86_hardware_enable)();if (r)pr_info ("kvm: enabling virtualization on CPU%d failed\n" , cpu);else
@@ -11877,12 +11866,13 @@ static void hardware_disable(void *junk)if (!cpumask_test_cpu (cpu, &cpus_hardware_enabled))return ;cpumask_clear_cpu (cpu, &cpus_hardware_enabled); -kvm_arch_hardware_disable (); +static_call (kvm_x86_hardware_disable)(); +drop_user_return_notifiers (); }void kvm_arch_pre_hardware_unsetup (void ) { -on_each_cpu (hardware_disable,NULL ,1 ); +/* TODO: eliminate this function */ }/*
@@ -11974,7 +11964,7 @@ void kvm_arch_resume(int usage_count)if (kvm_arch_check_processor_compat ())return ; -if (kvm_arch_hardware_enable ()) +if (static_call (kvm_x86_hardware_enable)())return ; local_tsc =rdtsc ();
@@ -12115,6 +12105,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)();
Signature · 1 line
2.25.1
PATCH v2 17/19 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 defined.
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 | 2 ++ virt/kvm/Kconfig | 3 +++ virt/kvm/Makefile.kvm | 5 ++++- 4 files changed, 10 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 +2 -0
@@ -1434,8 +1434,10 @@ 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 v2 18/19 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
@@ -11830,22 +11830,15 @@ 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 ) { -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_info ("kvm: enabling virtualization on CPU%d failed\n" , cpu); -else -cpumask_set_cpu (cpu, &cpus_hardware_enabled); +pr_info ("kvm: enabling virtualization on CPU%d failed\n" ,smp_processor_id ());return r; }
@@ -11859,13 +11852,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 v2 19/19 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 | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-)
virt/kvm/kvm_arch.c +2 -14
@@ -12,22 +12,16 @@#include <linux/kvm_host.h> -static cpumask_t cpus_hardware_enabled =CPU_MASK_NONE ; -static int __hardware_enable (void ) { -int cpu =raw_smp_processor_id ();int r;WARN_ON_ONCE (preemptible ()); -if (cpumask_test_cpu (cpu, &cpus_hardware_enabled)) -return 0 ; r =kvm_arch_hardware_enable ();if (r) -pr_info ("kvm: enabling virtualization on CPU%d failed\n" , cpu); -else -cpumask_set_cpu (cpu, &cpus_hardware_enabled); +pr_info ("kvm: enabling virtualization on CPU%d failed\n" , +smp_processor_id ());return r; }
@@ -41,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