From nobody Fri Apr 10 18:34:59 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A00DDC0502C for ; Tue, 30 Aug 2022 12:02:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229652AbiH3MCF (ORCPT ); Tue, 30 Aug 2022 08:02:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38100 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229658AbiH3MB5 (ORCPT ); Tue, 30 Aug 2022 08:01:57 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 88B75E396B; Tue, 30 Aug 2022 05:01:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661860916; x=1693396916; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=8uxTMc931N62LSyYSZec4Q6h4MH1p09mPLFpxJiMDMs=; b=htcnDZrp5URoAp8QDKtTiQlZGHq+fQPV+dU3Bx7JEQ/t8b+LavSICrd/ 3OovxlfgIAIWL4RZNAMdSCibnj+eIxcMlHrhSzO4SehOz0bgR5YneNWkp Qrg0muydE+tdWciBQKT9oBM0IVI7hHudpnX88CqYarGsFU+J9C16dzWRI v9A/sJCjJZLpl9J56RUawsHyLRSHrPfpyQDeaHjTwYcjcvg/CoCmB3Hz0 FpW5kPH1vQ/nPdpiLj8aNl2m0z7JuME/h8t2BkDuv5LgUy+HKpY6NkHoM bhQ/KeZdG3M/gYx1E3gI/vYFVe36galuyZWjSL/mmzxNtLSwZsxOEq0jK A==; X-IronPort-AV: E=McAfee;i="6500,9779,10454"; a="356870954" X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="356870954" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:54 -0700 X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="787469608" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:54 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , Sean Christopherson , Kai Huang , Chao Gao , Will Deacon Subject: [PATCH v2 01/19] KVM: x86: Drop kvm_user_return_msr_cpu_online() Date: Tue, 30 Aug 2022 05:01:16 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata 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. Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- arch/x86/kvm/x86.c | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 205ebdc2b11b..16104a2f7d8e 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -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]; }; =20 @@ -363,6 +364,10 @@ static void kvm_on_user_return(struct user_return_noti= fier *urn) local_irq_restore(flags); for (slot =3D 0; slot < kvm_nr_uret_msrs; ++slot) { values =3D &msrs->values[slot]; + /* + * No need to check values->initialized because host =3D curr =3D 0 + * by __GFP_ZERO when !values->initialized. + */ if (values->host !=3D values->curr) { wrmsrl(kvm_uret_msrs_list[slot], values->host); values->curr =3D values->host; @@ -409,34 +414,30 @@ int kvm_find_user_return_msr(u32 msr) } EXPORT_SYMBOL_GPL(kvm_find_user_return_msr); =20 -static void kvm_user_return_msr_cpu_online(void) -{ - unsigned int cpu =3D smp_processor_id(); - struct kvm_user_return_msrs *msrs =3D per_cpu_ptr(user_return_msrs, cpu); - u64 value; - int i; - - for (i =3D 0; i < kvm_nr_uret_msrs; ++i) { - rdmsrl_safe(kvm_uret_msrs_list[i], &value); - msrs->values[i].host =3D value; - msrs->values[i].curr =3D value; - } -} - int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask) { unsigned int cpu =3D smp_processor_id(); struct kvm_user_return_msrs *msrs =3D per_cpu_ptr(user_return_msrs, cpu); + struct kvm_user_return_msr_values *values =3D &msrs->values[slot]; int err; =20 - value =3D (value & mask) | (msrs->values[slot].host & ~mask); - if (value =3D=3D msrs->values[slot].curr) + if (unlikely(!values->initialized)) { + u64 host_value; + + rdmsrl_safe(kvm_uret_msrs_list[slot], &host_value); + values->host =3D host_value; + values->curr =3D host_value; + values->initialized =3D true; + } + + value =3D (value & mask) | (values->host & ~mask); + if (value =3D=3D values->curr) return 0; err =3D wrmsrl_safe(kvm_uret_msrs_list[slot], value); if (err) return 1; =20 - msrs->values[slot].curr =3D value; + values->curr =3D value; if (!msrs->registered) { msrs->urn.on_user_return =3D kvm_on_user_return; user_return_notifier_register(&msrs->urn); @@ -9212,7 +9213,12 @@ int kvm_arch_init(void *opaque) return -ENOMEM; } =20 - user_return_msrs =3D 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 =3D 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 =3D -ENOMEM; @@ -11836,7 +11842,6 @@ int kvm_arch_hardware_enable(void) u64 max_tsc =3D 0; bool stable, backwards_tsc =3D false; =20 - kvm_user_return_msr_cpu_online(); ret =3D static_call(kvm_x86_hardware_enable)(); if (ret !=3D 0) return ret; --=20 2.25.1 From nobody Fri Apr 10 18:34:59 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 171E1C0502A for ; Tue, 30 Aug 2022 12:02:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230255AbiH3MCI (ORCPT ); Tue, 30 Aug 2022 08:02:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229759AbiH3MB5 (ORCPT ); Tue, 30 Aug 2022 08:01:57 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 94525E5892; Tue, 30 Aug 2022 05:01:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661860916; x=1693396916; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=uz16PyoQFhukf/H8M5ipcGMQZYTZukV44jdncTSqdg4=; b=LaHzgX5HONWfv61ahX7wnCj6Q+1pZNS0ESF5VHjD/+XIMKA/lJJaG34+ U/2AgSY7ZldcDo8WpEeyUBR0uBZbhBmsTb4EsH44St6tyfN79R6ZRv1Xb pb/eHAwl0iXaREcjo7wzclG6bW6VUiRLgXQXkNmaNjwIbhbh0tmvN/xly xQOLIxPUVeOypN3lD3/C2K1Rfod5yc3KcUVyKy86D8zCG8J75/6wuqU/Y gJjuwPlFObFAneVVSg3OQsP/yDGbejJySaqm1ZZSfAOAIGcYDBG9xlSiE IHKTIfmUkr0A+p9HIgpQ1SHWW05rrykRvC6iX47TuXx7Lt7DxYPKobb/J Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10454"; a="356870956" X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="356870956" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:54 -0700 X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="787469613" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:54 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , Sean Christopherson , Kai Huang , Chao Gao , Will Deacon Subject: [PATCH v2 02/19] KVM: x86: Use this_cpu_ptr() instead of per_cpu_ptr(smp_processor_id()) Date: Tue, 30 Aug 2022 05:01:17 -0700 Message-Id: <920dfe48e7255c2224a799386d720f34b4479e1a.1661860550.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata convert per_cpu_ptr(smp_processor_id()) to this_cpu_ptr() as trivial cleanup. Signed-off-by: Isaku Yamahata Reviewed-by: Chao Gao --- arch/x86/kvm/x86.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 16104a2f7d8e..7d5fff68befe 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -416,8 +416,7 @@ EXPORT_SYMBOL_GPL(kvm_find_user_return_msr); =20 int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask) { - unsigned int cpu =3D smp_processor_id(); - struct kvm_user_return_msrs *msrs =3D per_cpu_ptr(user_return_msrs, cpu); + struct kvm_user_return_msrs *msrs =3D this_cpu_ptr(user_return_msrs); struct kvm_user_return_msr_values *values =3D &msrs->values[slot]; int err; =20 @@ -449,8 +448,7 @@ EXPORT_SYMBOL_GPL(kvm_set_user_return_msr); =20 static void drop_user_return_notifiers(void) { - unsigned int cpu =3D smp_processor_id(); - struct kvm_user_return_msrs *msrs =3D per_cpu_ptr(user_return_msrs, cpu); + struct kvm_user_return_msrs *msrs =3D this_cpu_ptr(user_return_msrs); =20 if (msrs->registered) kvm_on_user_return(&msrs->urn); --=20 2.25.1 From nobody Fri Apr 10 18:34:59 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 99858C0502C for ; Tue, 30 Aug 2022 12:02:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230332AbiH3MCK (ORCPT ); Tue, 30 Aug 2022 08:02:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38128 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229823AbiH3MB6 (ORCPT ); Tue, 30 Aug 2022 08:01:58 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5CC3BE58B0; Tue, 30 Aug 2022 05:01:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661860917; x=1693396917; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ecJt3wo84iM3H4djjCHx3xWGtpp+Oi5DdaN0Ffz4IIQ=; b=HZLNJCdd/PFUoPFqL1D4quktOkYUlmwt/RRq72WB9+eEyHbRWNPu/nC9 heNaH2pCYNzsfhPaAHqzyOA+svHu9LEpfCqO16bBNi8sOA87RAxMqMxWz T6sCvdjA+Jj+2S/WSXNxOq5uMrZB6xihdb/8Et5oV73X3R5FtgW2lx38H BABtvJrgrZMnm6XHqiIwEtoIN5stbJGVAbeCLtN168VH684v8m1YZ8mw/ X+xm38ZPNgBJ3ztu9VOwTVH02SxgNp1u+LGHSAhn3M+EPcarBMB0EJRI7 OVwT7iqFzIghNWL5eUw49GTWyVWmhgtZLml5HH3UuKv/Lh1DLJm6uVfn2 Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10454"; a="356870957" X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="356870957" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:55 -0700 X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="787469616" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:55 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , Sean Christopherson , Kai Huang , Chao Gao , Will Deacon Subject: [PATCH v2 03/19] KVM: x86: Move check_processor_compatibility from init ops to runtime ops Date: Tue, 30 Aug 2022 05:01:18 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Chao Gao 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. Signed-off-by: Chao Gao Reviewed-by: Sean Christopherson Link: https://lore.kernel.org/r/20220216031528.92558-2-chao.gao@intel.com Signed-off-by: Isaku Yamahata --- 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(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-= x86-ops.h index 51f777071584..3bc45932e2d1 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -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) =20 #undef KVM_X86_OP #undef KVM_X86_OP_OPTIONAL diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 2c96c43c313a..5df5d88d345f 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1445,6 +1445,7 @@ static inline u16 kvm_lapic_irq_dest_mode(bool dest_m= ode_logical) struct kvm_x86_ops { const char *name; =20 + 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); =20 diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index f3813dbacb9f..371300f03f55 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4134,7 +4134,7 @@ svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned c= har *hypercall) hypercall[2] =3D 0xd9; } =20 -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 =3D { .name =3D "kvm_amd", =20 .hardware_unsetup =3D svm_hardware_unsetup, + .check_processor_compatibility =3D svm_check_processor_compatibility, .hardware_enable =3D svm_hardware_enable, .hardware_disable =3D svm_hardware_disable, .has_emulated_msr =3D svm_has_emulated_msr, @@ -5122,7 +5123,6 @@ static struct kvm_x86_init_ops svm_init_ops __initdat= a =3D { .cpu_has_kvm_support =3D has_svm, .disabled_by_bios =3D is_disabled, .hardware_setup =3D svm_hardware_setup, - .check_processor_compatibility =3D svm_check_processor_compat, =20 .runtime_ops =3D &svm_x86_ops, .pmu_ops =3D &amd_pmu_ops, diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index d7f8331d6f7e..3cf7f18a4115 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2495,8 +2495,8 @@ static bool cpu_has_sgx(void) return cpuid_eax(0) >=3D 0x12 && (cpuid_eax(0x12) & BIT(0)); } =20 -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 =3D ctl_min | ctl_opt; @@ -2514,7 +2514,7 @@ static __init int adjust_vmx_controls(u32 ctl_min, u3= 2 ctl_opt, return 0; } =20 -static __init u64 adjust_vmx_controls64(u64 ctl_opt, u32 msr) +static u64 adjust_vmx_controls64(u64 ctl_opt, u32 msr) { u64 allowed; =20 @@ -2523,8 +2523,8 @@ static __init u64 adjust_vmx_controls64(u64 ctl_opt, = u32 msr) return ctl_opt & allowed; } =20 -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; } =20 -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 =3D { =20 .hardware_unsetup =3D vmx_hardware_unsetup, =20 + .check_processor_compatibility =3D vmx_check_processor_compatibility, .hardware_enable =3D vmx_hardware_enable, .hardware_disable =3D vmx_hardware_disable, .has_emulated_msr =3D vmx_has_emulated_msr, @@ -8404,7 +8405,6 @@ static __init int hardware_setup(void) static struct kvm_x86_init_ops vmx_init_ops __initdata =3D { .cpu_has_kvm_support =3D cpu_has_kvm_support, .disabled_by_bios =3D vmx_disabled_by_bios, - .check_processor_compatibility =3D vmx_check_processor_compat, .hardware_setup =3D hardware_setup, .handle_intel_pt_intr =3D NULL, =20 diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 7d5fff68befe..985487fe0d63 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11997,7 +11997,6 @@ void kvm_arch_hardware_unsetup(void) int kvm_arch_check_processor_compat(void *opaque) { struct cpuinfo_x86 *c =3D &cpu_data(smp_processor_id()); - struct kvm_x86_init_ops *ops =3D opaque; =20 WARN_ON(!irqs_disabled()); =20 @@ -12005,7 +12004,7 @@ int kvm_arch_check_processor_compat(void *opaque) __cr4_reserved_bits(cpu_has, &boot_cpu_data)) return -EIO; =20 - return ops->check_processor_compatibility(); + return static_call(kvm_x86_check_processor_compatibility)(); } =20 bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu) --=20 2.25.1 From nobody Fri Apr 10 18:34:59 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3A094C0502A for ; Tue, 30 Aug 2022 12:02:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230448AbiH3MCT (ORCPT ); Tue, 30 Aug 2022 08:02:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38152 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229975AbiH3MB6 (ORCPT ); Tue, 30 Aug 2022 08:01:58 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E19E6E399B; Tue, 30 Aug 2022 05:01:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661860917; x=1693396917; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=496gn+dXXIi7M5XXbuRczLDPDm6MGxriSqNHgXRr8zI=; b=LW2Rd8abBtMkbWLgZ3f91iejoQay8frD3nCq7te3BIf2Qa++rZI8JoP/ G0j3JMiJBnB1I2S5jAayKk9VbVmD8VkhVPipfrqOtPE5dNNKCQOPRftGS Crtv9aNTEzv/ccHpGymWsybAvuUDQOBWPxICgAOZI3sKOX7sc9iEohqgG yBpkMNZOnj0CNhgt1U6cjv/oz1mW9rLSBTBQMNzM/Lj9o59NJRBeSCuXX W8CJKSu+DeW2FyAHsnR3kPN6wx3PYmJ7a37xyG6nD/U6gblLM/GhXndpX 8tcMlAbyvLxrGFHndu5dM/AYLgS/escw5mzws/US6oAq7Snx/+/4fvHmS w==; X-IronPort-AV: E=McAfee;i="6500,9779,10454"; a="356870959" X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="356870959" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:55 -0700 X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="787469619" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:55 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , Sean Christopherson , Kai Huang , Chao Gao , Will Deacon , Suzuki K Poulose , Anup Patel , Claudio Imbrenda Subject: [PATCH v2 04/19] Partially revert "KVM: Pass kvm_init()'s opaque param to additional arch funcs" Date: Tue, 30 Aug 2022 05:01:19 -0700 Message-Id: <7f4a3ef13cc4b22aa77e8c5022e1710fd4189eff.1661860550.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Chao Gao 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(). Signed-off-by: Chao Gao Reviewed-by: Sean Christopherson Reviewed-by: Suzuki K Poulose Acked-by: Anup Patel Acked-by: Claudio Imbrenda Link: https://lore.kernel.org/r/20220216031528.92558-3-chao.gao@intel.com Signed-off-by: Isaku Yamahata Reviewed-by: Kai Huang --- 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(-) diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 2ff0ef62abad..3385fb57c11a 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -68,7 +68,7 @@ int kvm_arch_hardware_setup(void *opaque) return 0; } =20 -int kvm_arch_check_processor_compat(void *opaque) +int kvm_arch_check_processor_compat(void) { return 0; } diff --git a/arch/mips/kvm/mips.c b/arch/mips/kvm/mips.c index a25e0b73ee70..092d09fb6a7e 100644 --- a/arch/mips/kvm/mips.c +++ b/arch/mips/kvm/mips.c @@ -140,7 +140,7 @@ int kvm_arch_hardware_setup(void *opaque) return 0; } =20 -int kvm_arch_check_processor_compat(void *opaque) +int kvm_arch_check_processor_compat(void) { return 0; } diff --git a/arch/powerpc/kvm/powerpc.c b/arch/powerpc/kvm/powerpc.c index fb1490761c87..7b56d6ccfdfb 100644 --- a/arch/powerpc/kvm/powerpc.c +++ b/arch/powerpc/kvm/powerpc.c @@ -447,7 +447,7 @@ int kvm_arch_hardware_setup(void *opaque) return 0; } =20 -int kvm_arch_check_processor_compat(void *opaque) +int kvm_arch_check_processor_compat(void) { return kvmppc_core_check_processor_compat(); } diff --git a/arch/riscv/kvm/main.c b/arch/riscv/kvm/main.c index 1549205fe5fe..f8d6372d208f 100644 --- a/arch/riscv/kvm/main.c +++ b/arch/riscv/kvm/main.c @@ -20,7 +20,7 @@ long kvm_arch_dev_ioctl(struct file *filp, return -EINVAL; } =20 -int kvm_arch_check_processor_compat(void *opaque) +int kvm_arch_check_processor_compat(void) { return 0; } diff --git a/arch/s390/kvm/kvm-s390.c b/arch/s390/kvm/kvm-s390.c index edfd4bbd0cba..e26d4dd85668 100644 --- a/arch/s390/kvm/kvm-s390.c +++ b/arch/s390/kvm/kvm-s390.c @@ -254,7 +254,7 @@ int kvm_arch_hardware_enable(void) return 0; } =20 -int kvm_arch_check_processor_compat(void *opaque) +int kvm_arch_check_processor_compat(void) { return 0; } diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 985487fe0d63..ca920b6b925d 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11994,7 +11994,7 @@ void kvm_arch_hardware_unsetup(void) static_call(kvm_x86_hardware_unsetup)(); } =20 -int kvm_arch_check_processor_compat(void *opaque) +int kvm_arch_check_processor_compat(void) { struct cpuinfo_x86 *c =3D &cpu_data(smp_processor_id()); =20 diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index f4519d3689e1..eab352902de7 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -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); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 584a5bab3af3..4243a9541543 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -5799,22 +5799,14 @@ void kvm_unregister_perf_callbacks(void) } #endif =20 -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 =3D data; - - *c->ret =3D kvm_arch_check_processor_compat(c->opaque); + *(int *)rtn =3D kvm_arch_check_processor_compat(); } =20 int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, struct module *module) { - struct kvm_cpu_compat_check c; int r; int cpu; =20 @@ -5842,10 +5834,8 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsig= ned vcpu_align, if (r < 0) goto out_free_1; =20 - c.ret =3D &r; - c.opaque =3D 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; } --=20 2.25.1 From nobody Fri Apr 10 18:34:59 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B49C4C0502E for ; Tue, 30 Aug 2022 12:02:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230369AbiH3MCO (ORCPT ); Tue, 30 Aug 2022 08:02:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229982AbiH3MB6 (ORCPT ); Tue, 30 Aug 2022 08:01:58 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC203E58B1; Tue, 30 Aug 2022 05:01:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661860917; x=1693396917; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=4lQQM3YEKOljyNg3+Qt8aEy+mGfQLUEBdfFaUdsEnJU=; b=QygY3acMhLh+6hYcFzQU7X2J1Cx1GslcLVg6/OR3bfuEyO7J2VsTa99R 75RauTihyzNBYand4a9Bpl7c8ZBDqGSpACPzsVXaTOHlivJ4XmDOh8q52 vwbxnOfFP0G/+UPBtYWkT3Sg3xpSqYa6rjMG0bUh3zCJcfNXwwC0iDrqs o522sdb3C0BzF8baU19rDdYh4svvcN4IbWjzZCc/V72u5nOlH0kNYGy2y rt2IwXrvfOOpJ2wHlszYmyNehGTWOZ5e5V/87YmM8pGRq/4re/txtv8Ig LU6SMc/1UfCzgbi04oO13XsMnya7Yk6nvGRG/fHlp0gDq0qM0HOQFkymV w==; X-IronPort-AV: E=McAfee;i="6500,9779,10454"; a="356870962" X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="356870962" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:56 -0700 X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="787469623" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:55 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , Sean Christopherson , Kai Huang , Chao Gao , Will Deacon , Thomas Gleixner Subject: [PATCH v2 05/19] KVM: Rename and move CPUHP_AP_KVM_STARTING to ONLINE section Date: Tue, 30 Aug 2022 05:01:20 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Chao Gao 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. Suggested-by: Thomas Gleixner Signed-off-by: Chao Gao Link: https://lore.kernel.org/r/20220216031528.92558-6-chao.gao@intel.com --- include/linux/cpuhotplug.h | 2 +- virt/kvm/kvm_main.c | 30 ++++++++++++++++++++++-------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/include/linux/cpuhotplug.h b/include/linux/cpuhotplug.h index f61447913db9..7972bd63e0cb 100644 --- a/include/linux/cpuhotplug.h +++ b/include/linux/cpuhotplug.h @@ -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 { =20 /* 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, diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 4243a9541543..6ce6f27f2934 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -5010,13 +5010,27 @@ static void hardware_enable_nolock(void *junk) } } =20 -static int kvm_starting_cpu(unsigned int cpu) +static int kvm_online_cpu(unsigned int cpu) { + int ret =3D 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 =3D -EIO; + } + } raw_spin_unlock(&kvm_count_lock); - return 0; + return ret; } =20 static void hardware_disable_nolock(void *junk) @@ -5029,7 +5043,7 @@ static void hardware_disable_nolock(void *junk) kvm_arch_hardware_disable(); } =20 -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, unsign= ed vcpu_align, goto out_free_2; } =20 - r =3D cpuhp_setup_state_nocalls(CPUHP_AP_KVM_STARTING, "kvm/cpu:starting", - kvm_starting_cpu, kvm_dying_cpu); + r =3D 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, unsign= ed 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(); --=20 2.25.1 From nobody Fri Apr 10 18:34:59 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 18F31ECAAD4 for ; Tue, 30 Aug 2022 12:02:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229767AbiH3MCX (ORCPT ); Tue, 30 Aug 2022 08:02:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38170 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230001AbiH3MB7 (ORCPT ); Tue, 30 Aug 2022 08:01:59 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6DC17E396B; Tue, 30 Aug 2022 05:01:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661860918; x=1693396918; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=pKFno8pgV7d1CtggUlggw1t/wDDYPlISODZCprE0zvU=; b=n1sWkEExpFKMW8SWJkY9+0OuLNHI7+XwdGPztG3+AQitjowlnLnxzfbG CCIkHFq0NEEBh2oD+2ij4wpfGJUKTJtNfa3QqL7QZ/OexkfylrNGpOMLj RtFjL2iWLkcaxsmoSVpbGNxOufl0/lcnMKrfA3Z5GytZ/at2QEvCN2GAq sDvA4ilnjviIbLXHb/ESN+2FJIx9LdXFmkY4M/o/gxN4IYHoDlIzuMOWF gQVcncOe6j41Tce3Bx10/3PASLHEomMVi/ctoZ2hgU4ineMewjdaye9xM uzKRYY5GIHXK9bWF3fIvg0yMtyl9gM2bXWOaxM1WBACz/+vF7E21d+qEi w==; X-IronPort-AV: E=McAfee;i="6500,9779,10454"; a="356870965" X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="356870965" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:56 -0700 X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="787469626" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:56 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , Sean Christopherson , Kai Huang , Chao Gao , Will Deacon Subject: [PATCH v2 06/19] KVM: Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock Date: Tue, 30 Aug 2022 05:01:21 -0700 Message-Id: <66d17ce63c4a71655d174c664bca8398ee1a060f.1661860550.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata 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. Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- Documentation/virt/kvm/locking.rst | 14 +++++------- virt/kvm/kvm_main.c | 36 +++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/Documentation/virt/kvm/locking.rst b/Documentation/virt/kvm/lo= cking.rst index 845a561629f1..8957e32aa724 100644 --- a/Documentation/virt/kvm/locking.rst +++ b/Documentation/virt/kvm/locking.rst @@ -216,15 +216,11 @@ time it will be set using the Dirty tracking mechanis= m 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 =3D> kvm_lock. =20 ``kvm->mn_invalidate_lock`` ^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 6ce6f27f2934..606ac6bb67d0 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -100,7 +100,6 @@ EXPORT_SYMBOL_GPL(halt_poll_ns_shrink); */ =20 DEFINE_MUTEX(kvm_lock); -static DEFINE_RAW_SPINLOCK(kvm_count_lock); LIST_HEAD(vm_list); =20 static cpumask_var_t cpus_hardware_enabled; @@ -4996,6 +4995,8 @@ static void hardware_enable_nolock(void *junk) int cpu =3D raw_smp_processor_id(); int r; =20 + WARN_ON_ONCE(preemptible()); + if (cpumask_test_cpu(cpu, cpus_hardware_enabled)) return; =20 @@ -5014,7 +5015,7 @@ static int kvm_online_cpu(unsigned int cpu) { int ret =3D 0; =20 - 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 =3D -EIO; } } - raw_spin_unlock(&kvm_count_lock); + mutex_unlock(&kvm_lock); return ret; } =20 @@ -5037,6 +5038,8 @@ static void hardware_disable_nolock(void *junk) { int cpu =3D raw_smp_processor_id(); =20 + 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) =20 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; } =20 @@ -5063,16 +5066,19 @@ static void hardware_disable_all_nolock(void) =20 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(); } =20 static int hardware_enable_all(void) { int r =3D 0; =20 - raw_spin_lock(&kvm_count_lock); + cpus_read_lock(); + mutex_lock(&kvm_lock); =20 kvm_usage_count++; if (kvm_usage_count =3D=3D 1) { @@ -5085,7 +5091,8 @@ static int hardware_enable_all(void) } } =20 - raw_spin_unlock(&kvm_count_lock); + mutex_unlock(&kvm_lock); + cpus_read_unlock(); =20 return r; } @@ -5691,15 +5698,22 @@ static void kvm_init_debug(void) =20 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; } =20 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); } } --=20 2.25.1 From nobody Fri Apr 10 18:34:59 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0A218C0502A for ; Tue, 30 Aug 2022 12:02:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229788AbiH3MC2 (ORCPT ); Tue, 30 Aug 2022 08:02:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38222 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230079AbiH3MCB (ORCPT ); Tue, 30 Aug 2022 08:02:01 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2C63AEDAF; Tue, 30 Aug 2022 05:01:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661860918; x=1693396918; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=CuQu42RSQieDcNebGbvI6E9KYE+foJn12IM/I0HeL9o=; b=jOLxZLDrKt0eOzTXHX0WlV/QJkJth2mp7bqycI0Ur5dYtH5j5sjtMF6u 7FhM14AOpeEUyyskVvrmQO+yglLpPwvEXg3SPdUH1XZbcq9VwBOkNZxD0 I8JC2vkdPxArzOgOtupSmqT25ewvnZj3dttzfU0usxRJ0fWX/TgJMZBMI MxJf/JwIFQS1cDsa26g33Qu1/mEYmeA8Bw4SqUUkmPSDYE01ZSmOdxV12 nPyml13lwVgNqGPIXLBizxYAr8y/z0uTxbv6ADnpyzzX+1/ac2hIFOwFu keguNOfMBfRJ3sH364t1J6B39nx7XwWcHLIS1R+ZYDOcdYX5Py5nRGFjU A==; X-IronPort-AV: E=McAfee;i="6500,9779,10454"; a="356870969" X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="356870969" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:56 -0700 X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="787469629" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:56 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , Sean Christopherson , Kai Huang , Chao Gao , Will Deacon Subject: [PATCH v2 07/19] KVM: Add arch hooks for PM events with empty stub Date: Tue, 30 Aug 2022 05:01:22 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata Add arch hooks for reboot, suspend, resume, and CPU-online/offline events with empty stub functions. Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- 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 diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index eab352902de7..dd2a6d98d4de 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -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); =20 +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 diff --git a/virt/kvm/Makefile.kvm b/virt/kvm/Makefile.kvm index 2c27d5d0c367..c4210acabd35 100644 --- a/virt/kvm/Makefile.kvm +++ b/virt/kvm/Makefile.kvm @@ -5,7 +5,7 @@ =20 KVM ?=3D ../../../virt/kvm =20 -kvm-y :=3D $(KVM)/kvm_main.o $(KVM)/eventfd.o $(KVM)/binary_stats.o +kvm-y :=3D $(KVM)/kvm_main.o $(KVM)/kvm_arch.o $(KVM)/eventfd.o $(KVM)/bin= ary_stats.o kvm-$(CONFIG_KVM_VFIO) +=3D $(KVM)/vfio.o kvm-$(CONFIG_KVM_MMIO) +=3D $(KVM)/coalesced_mmio.o kvm-$(CONFIG_KVM_ASYNC_PF) +=3D $(KVM)/async_pf.o diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c new file mode 100644 index 000000000000..4748a76bcb03 --- /dev/null +++ b/virt/kvm/kvm_arch.c @@ -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 + * + */ + +#include + +/* + * 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; +} + +__weak int kvm_arch_online_cpu(unsigned int cpu, int usage_count) +{ + return 0; +} + +__weak int kvm_arch_offline_cpu(unsigned int cpu, int usage_count) +{ + return 0; +} + +__weak int kvm_arch_reboot(int val) +{ + return NOTIFY_OK; +} + +__weak int kvm_arch_suspend(int usage_count) +{ + return 0; +} + +__weak void kvm_arch_resume(int usage_count) +{ +} diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 606ac6bb67d0..de336fba902b 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -144,6 +144,7 @@ static int kvm_no_compat_open(struct inode *inode, stru= ct file *file) #endif static int hardware_enable_all(void); static void hardware_disable_all(void); +static void hardware_disable_nolock(void *junk); =20 static void kvm_io_bus_destroy(struct kvm_io_bus *bus); =20 @@ -1097,15 +1098,6 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, co= nst char *fdname) return ret; } =20 -/* - * Called after the VM is otherwise initialized, but just before adding it= to - * the vm_list. - */ -int __weak kvm_arch_post_init_vm(struct kvm *kvm) -{ - return 0; -} - /* * Called just after removing the VM from the vm_list, but before doing any * other destruction. @@ -5028,6 +5020,11 @@ static int kvm_online_cpu(unsigned int cpu) if (atomic_read(&hardware_enable_failed)) { atomic_set(&hardware_enable_failed, 0); ret =3D -EIO; + } else { + ret =3D kvm_arch_online_cpu(cpu, kvm_usage_count); + if (ret) { + hardware_disable_nolock(NULL); + } } } mutex_unlock(&kvm_lock); @@ -5048,11 +5045,19 @@ static void hardware_disable_nolock(void *junk) =20 static int kvm_offline_cpu(unsigned int cpu) { + int ret =3D 0; + mutex_lock(&kvm_lock); - if (kvm_usage_count) + if (kvm_usage_count) { hardware_disable_nolock(NULL); + ret =3D kvm_arch_offline_cpu(cpu, kvm_usage_count); + if (ret) { + (void)hardware_enable_nolock(NULL); + atomic_set(&hardware_enable_failed, 0); + } + } mutex_unlock(&kvm_lock); - return 0; + return ret; } =20 static void hardware_disable_all_nolock(void) @@ -5100,6 +5105,8 @@ static int hardware_enable_all(void) static int kvm_reboot(struct notifier_block *notifier, unsigned long val, void *v) { + int r; + /* * Some (well, at least mine) BIOSes hang on reboot if * in vmx root mode. @@ -5108,8 +5115,15 @@ static int kvm_reboot(struct notifier_block *notifie= r, unsigned long val, */ pr_info("kvm: exiting hardware virtualization\n"); kvm_rebooting =3D true; + + /* This hook is called without cpuhotplug disabled. */ + cpus_read_lock(); + mutex_lock(&kvm_lock); on_each_cpu(hardware_disable_nolock, NULL, 1); - return NOTIFY_OK; + r =3D kvm_arch_reboot(val); + mutex_unlock(&kvm_lock); + cpus_read_unlock(); + return r; } =20 static struct notifier_block kvm_reboot_notifier =3D { @@ -5703,19 +5717,18 @@ static int kvm_suspend(void) * cpu_hotplug_disable() and other CPUs are offlined. No need for * locking. */ - if (kvm_usage_count) { - lockdep_assert_not_held(&kvm_lock); + lockdep_assert_not_held(&kvm_lock); + if (kvm_usage_count) hardware_disable_nolock(NULL); - } - return 0; + return kvm_arch_suspend(kvm_usage_count); } =20 static void kvm_resume(void) { - if (kvm_usage_count) { - lockdep_assert_not_held(&kvm_lock); + kvm_arch_resume(kvm_usage_count); + lockdep_assert_not_held(&kvm_lock); + if (kvm_usage_count) hardware_enable_nolock(NULL); - } } =20 static struct syscore_ops kvm_syscore_ops =3D { --=20 2.25.1 From nobody Fri Apr 10 18:34:59 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8CBD3ECAAD4 for ; Tue, 30 Aug 2022 12:02:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230133AbiH3MCl (ORCPT ); Tue, 30 Aug 2022 08:02:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230082AbiH3MCB (ORCPT ); Tue, 30 Aug 2022 08:02:01 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 06263E58B0; Tue, 30 Aug 2022 05:01:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661860919; x=1693396919; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=TYBvZOLwTg/r74aw4zfac896o38NzV2pCG9Didy3epg=; b=fYAbcL6yIjnpValKDsFxEWoHtE6MOYkI7m7tfYudui6ipqNHGeTVSUPJ 0BxE+LISWMAcRZFsnfZjQnfm6+Qcr3HbPef/mDEJ1dgWuL6Z48veoN06I qyd3/4eJ4BDMk+6I/3EgUhWjH0tC237FDgNB7Hm7LFkfoC8BNs+uHn2HY 4I1qotIYKEkJhbKc4ew/vBCI+YGqsi6Vx9T35ddhLcjFGYYZrAlq2FcZe Ri6bAWPcJRl7YL9ucozITjkA5FBSUporR5U3ePHwhS1CSz6mOL/4Tvicw xbSmn+vSn8Bcp/Zhp09d8iY9duOej1pRiIHM4SBczHaCPx18WN1fbreMO A==; X-IronPort-AV: E=McAfee;i="6500,9779,10454"; a="356870970" X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="356870970" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:57 -0700 X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="787469634" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:57 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , Sean Christopherson , Kai Huang , Chao Gao , Will Deacon Subject: [PATCH v2 08/19] KVM: x86: Move TSC fixup logic to KVM arch resume callback Date: Tue, 30 Aug 2022 05:01:23 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata 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. Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- arch/x86/kvm/x86.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index ca920b6b925d..0b112cd7de58 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11831,18 +11831,30 @@ void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu= *vcpu, u8 vector) EXPORT_SYMBOL_GPL(kvm_vcpu_deliver_sipi_vector); =20 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 =3D 0; bool stable, backwards_tsc =3D false; =20 - ret =3D static_call(kvm_x86_hardware_enable)(); - if (ret !=3D 0) - return ret; + if (!usage_count) + return; + + if (kvm_arch_hardware_enable()) + return; =20 local_tsc =3D rdtsc(); stable =3D !kvm_check_tsc_unstable(); @@ -11917,13 +11929,6 @@ int kvm_arch_hardware_enable(void) } =20 } - return 0; -} - -void kvm_arch_hardware_disable(void) -{ - static_call(kvm_x86_hardware_disable)(); - drop_user_return_notifiers(); } =20 static inline void kvm_ops_update(struct kvm_x86_init_ops *ops) --=20 2.25.1 From nobody Fri Apr 10 18:34:59 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 07396ECAAA1 for ; Tue, 30 Aug 2022 12:02:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230186AbiH3MCf (ORCPT ); Tue, 30 Aug 2022 08:02:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38226 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230092AbiH3MCB (ORCPT ); Tue, 30 Aug 2022 08:02:01 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 677E4E68D7; Tue, 30 Aug 2022 05:01:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661860919; x=1693396919; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=a+33dowbF5ovlUSliqsdgglFT7NjLciyOWmwB2FWMt8=; b=D0C8PHcTCLmaJwExTCmSVYftx705TWQQ9Qg18n56BBVVtdccLPEVKj7D WwZvssaVOnZgTyowMTuD13W9KwqSAXYM8rtSnQJLZHz+ZgIzfqAXlh4o8 i5pus+zDXUGXGR1u5mKZBHzokBWy70Sjkcf5slXh6iId5/Wcg8EYpyI4Q sUgmjfW2XCl0BZl8CorsbHZZxJQx4SL7E9EZ6OFGE96vD5aYv/HKMXzMg xYvwXg+/WifMiwsX1MvHUwinngt7k/RvRUi74wLGv6YZMZ0+7x+nAfNW8 muwKJQqAEnoqnUywmCXb9RmU7bZnAalKo3IUPQkYRfLEkKQAaZafXKmdI A==; X-IronPort-AV: E=McAfee;i="6500,9779,10454"; a="356870972" X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="356870972" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:57 -0700 X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="787469637" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:57 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , Sean Christopherson , Kai Huang , Chao Gao , Will Deacon Subject: [PATCH v2 09/19] KVM: Add arch hook when VM is added/deleted Date: Tue, 30 Aug 2022 05:01:24 -0700 Message-Id: <8474b0ec8c70e7791dfa77bc720672549f19c397.1661860550.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata 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(). Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- 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(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index dd2a6d98d4de..f78364e01ca9 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -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); =20 diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c index 4748a76bcb03..0eac996f4981 100644 --- a/virt/kvm/kvm_arch.c +++ b/virt/kvm/kvm_arch.c @@ -10,11 +10,21 @@ =20 #include =20 +__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; } diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index de336fba902b..5b9dc6d6ee28 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -145,6 +145,7 @@ static int kvm_no_compat_open(struct inode *inode, stru= ct 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); =20 static void kvm_io_bus_destroy(struct kvm_io_bus *bus); =20 @@ -1215,11 +1216,12 @@ static struct kvm *kvm_create_vm(unsigned long type= , const char *fdname) if (r) goto out_err_no_debugfs; =20 - r =3D kvm_arch_post_init_vm(kvm); - if (r) - goto out_err; - mutex_lock(&kvm_lock); + r =3D 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); =20 @@ -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(); } =20 +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 =3D 0; --=20 2.25.1 From nobody Fri Apr 10 18:34:59 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 19B8EECAAA1 for ; Tue, 30 Aug 2022 12:02:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231183AbiH3MCy (ORCPT ); Tue, 30 Aug 2022 08:02:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38354 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230126AbiH3MCD (ORCPT ); Tue, 30 Aug 2022 08:02:03 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E26A6E396B; Tue, 30 Aug 2022 05:02:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661860921; x=1693396921; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/K5eYLg1Qhyr4Vggv4k7T0T2GWHxGLQC4WO6GCO5INw=; b=C4x4C8vSQzHYCnW13iPCpMjgI2ihdfHyD8stxJzGWOkImTpnBTMBRnpK nZoTxqklJkYooPBSZcB1t87EplabfkJQijYI8YLfBIiswYROumKdiUawb miJO1uqyxXjP5tyU+OTDFM1DGvY1hf/IKO+aVoH/MZ+zDq/y2hmUXmXp3 vAl8tyi7yi3fkptGZ1zwCWuySQsrihQRE6cjYe2r7gtSQCihCvBLCb4YK 6BVMufgOg9aEduhNk/71YgdFBCqkLhFRe9cQwtak9D0CyML4Tnm5eIeg/ FT12QmBmUSgP/uuStaJfWmEi9jepAyKFeHQiLOEgI9Lw4U+pRyCq80Azp A==; X-IronPort-AV: E=McAfee;i="6500,9779,10454"; a="356870977" X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="356870977" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:58 -0700 X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="787469640" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:57 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , Sean Christopherson , Kai Huang , Chao Gao , Will Deacon Subject: [PATCH v2 10/19] KVM: Move out KVM arch PM hooks and hardware enable/disable logic Date: Tue, 30 Aug 2022 05:01:25 -0700 Message-Id: <1563b68b05201f39846c2b23a1edebfdfb37cafb.1661860550.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata 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. Signed-off-by: Isaku Yamahata --- 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(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index f78364e01ca9..60f4ae9d6f48 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -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); diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c index 0eac996f4981..51c6e9f03ed5 100644 --- a/virt/kvm/kvm_arch.c +++ b/virt/kvm/kvm_arch.c @@ -6,49 +6,136 @@ * Author: * Isaku Yamahata * + * + * TODO: Delete this file once the conversion of all KVM arch is done. */ =20 #include =20 +static cpumask_t cpus_hardware_enabled =3D CPU_MASK_NONE; +static atomic_t hardware_enable_failed; + __weak int kvm_arch_post_init_vm(struct kvm *kvm) { return 0; } =20 +static void hardware_enable_nolock(void *junk) +{ + int cpu =3D raw_smp_processor_id(); + int r; + + WARN_ON_ONCE(preemptible()); + + if (cpumask_test_cpu(cpu, &cpus_hardware_enabled)) + return; + + cpumask_set_cpu(cpu, &cpus_hardware_enabled); + + r =3D kvm_arch_hardware_enable(); + + 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); + } +} + +static void hardware_disable_nolock(void *junk) +{ + int cpu =3D 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(); +} + +__weak void kvm_arch_pre_hardware_unsetup(void) +{ + on_each_cpu(hardware_disable_nolock, NULL, 1); +} + /* * Called after the VM is otherwise initialized, but just before adding it= to * the vm_list. */ __weak int kvm_arch_add_vm(struct kvm *kvm, int usage_count) { - return kvm_arch_post_init_vm(kvm); + int r =3D 0; + + if (usage_count !=3D 1) + return 0; + + atomic_set(&hardware_enable_failed, 0); + on_each_cpu(hardware_enable_nolock, NULL, 1); + + if (atomic_read(&hardware_enable_failed)) { + r =3D -EBUSY; + goto err; + } + + r =3D kvm_arch_post_init_vm(kvm); +err: + if (r) + on_each_cpu(hardware_disable_nolock, NULL, 1); + return r; } =20 __weak int kvm_arch_del_vm(int usage_count) { + if (usage_count) + return 0; + + on_each_cpu(hardware_disable_nolock, NULL, 1); return 0; } =20 __weak int kvm_arch_online_cpu(unsigned int cpu, int usage_count) { - return 0; + int ret =3D 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_nolock(NULL); + if (atomic_read(&hardware_enable_failed)) { + atomic_set(&hardware_enable_failed, 0); + ret =3D -EIO; + } + } + return ret; } =20 __weak int kvm_arch_offline_cpu(unsigned int cpu, int usage_count) { + if (usage_count) + hardware_disable_nolock(NULL); return 0; } =20 __weak int kvm_arch_reboot(int val) { + on_each_cpu(hardware_disable_nolock, NULL, 1); return NOTIFY_OK; } =20 __weak int kvm_arch_suspend(int usage_count) { + if (usage_count) + hardware_disable_nolock(NULL); return 0; } =20 __weak void kvm_arch_resume(int usage_count) { + if (usage_count) + hardware_enable_nolock(NULL); } diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 5b9dc6d6ee28..752edf9bc1c7 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -102,9 +102,7 @@ EXPORT_SYMBOL_GPL(halt_poll_ns_shrink); DEFINE_MUTEX(kvm_lock); LIST_HEAD(vm_list); =20 -static cpumask_var_t cpus_hardware_enabled; static int kvm_usage_count; -static atomic_t hardware_enable_failed; =20 static struct kmem_cache *kvm_vcpu_cache; =20 @@ -142,9 +140,6 @@ static int kvm_no_compat_open(struct inode *inode, stru= ct file *file) #define KVM_COMPAT(c) .compat_ioctl =3D kvm_no_compat_ioctl, \ .open =3D kvm_no_compat_open #endif -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); =20 static void kvm_io_bus_destroy(struct kvm_io_bus *bus); @@ -1196,10 +1191,6 @@ static struct kvm *kvm_create_vm(unsigned long type,= const char *fdname) if (r) goto out_err_no_arch_destroy_vm; =20 - r =3D hardware_enable_all(); - if (r) - goto out_err_no_disable; - #ifdef CONFIG_HAVE_KVM_IRQFD INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list); #endif @@ -1216,14 +1207,18 @@ static struct kvm *kvm_create_vm(unsigned long type= , const char *fdname) if (r) goto out_err_no_debugfs; =20 + cpus_read_lock(); mutex_lock(&kvm_lock); + kvm_usage_count++; r =3D kvm_arch_add_vm(kvm, kvm_usage_count); if (r) { + /* the following kvm_del_vm() decrements kvm_usage_count. */ mutex_unlock(&kvm_lock); goto out_err; } list_add(&kvm->vm_list, &vm_list); mutex_unlock(&kvm_lock); + cpus_read_unlock(); =20 preempt_notifier_inc(); kvm_init_pm_notifier(kvm); @@ -1240,9 +1235,7 @@ static struct kvm *kvm_create_vm(unsigned long type, = const char *fdname) mmu_notifier_unregister(&kvm->mmu_notifier, current->mm); #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: WARN_ON_ONCE(!refcount_dec_and_test(&kvm->users_count)); @@ -1321,7 +1314,6 @@ static void kvm_destroy_vm(struct kvm *kvm) cleanup_srcu_struct(&kvm->srcu); kvm_arch_free_vm(kvm); preempt_notifier_dec(); - hardware_disable_all(); kvm_del_vm(); mmdrop(mm); module_put(kvm_chardev_ops.owner); @@ -4986,135 +4978,37 @@ static struct miscdevice kvm_dev =3D { &kvm_chardev_ops, }; =20 -static void hardware_enable_nolock(void *junk) -{ - int cpu =3D raw_smp_processor_id(); - int r; - - WARN_ON_ONCE(preemptible()); - - if (cpumask_test_cpu(cpu, cpus_hardware_enabled)) - return; - - cpumask_set_cpu(cpu, cpus_hardware_enabled); - - r =3D kvm_arch_hardware_enable(); - - 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); - } -} - static int kvm_online_cpu(unsigned int cpu) { - int ret =3D 0; + int ret; =20 mutex_lock(&kvm_lock); - /* - * 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 =3D -EIO; - } else { - ret =3D kvm_arch_online_cpu(cpu, kvm_usage_count); - if (ret) { - hardware_disable_nolock(NULL); - } - } - } + ret =3D kvm_arch_online_cpu(cpu, kvm_usage_count); mutex_unlock(&kvm_lock); return ret; } =20 -static void hardware_disable_nolock(void *junk) -{ - int cpu =3D 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(); -} - static int kvm_offline_cpu(unsigned int cpu) { - int ret =3D 0; + int ret; =20 mutex_lock(&kvm_lock); - if (kvm_usage_count) { - hardware_disable_nolock(NULL); - ret =3D kvm_arch_offline_cpu(cpu, kvm_usage_count); - if (ret) { - (void)hardware_enable_nolock(NULL); - atomic_set(&hardware_enable_failed, 0); - } - } + ret =3D kvm_arch_offline_cpu(cpu, kvm_usage_count); mutex_unlock(&kvm_lock); return ret; } =20 -static void hardware_disable_all_nolock(void) -{ - BUG_ON(!kvm_usage_count); - - kvm_usage_count--; - if (!kvm_usage_count) - on_each_cpu(hardware_disable_nolock, NULL, 1); -} - -static void hardware_disable_all(void) -{ - cpus_read_lock(); - mutex_lock(&kvm_lock); - hardware_disable_all_nolock(); - mutex_unlock(&kvm_lock); - cpus_read_unlock(); -} - static void kvm_del_vm(void) { cpus_read_lock(); mutex_lock(&kvm_lock); + WARN_ON_ONCE(!kvm_usage_count); + kvm_usage_count--; kvm_arch_del_vm(kvm_usage_count); mutex_unlock(&kvm_lock); cpus_read_unlock(); } =20 -static int hardware_enable_all(void) -{ - int r =3D 0; - - cpus_read_lock(); - mutex_lock(&kvm_lock); - - kvm_usage_count++; - if (kvm_usage_count =3D=3D 1) { - atomic_set(&hardware_enable_failed, 0); - on_each_cpu(hardware_enable_nolock, NULL, 1); - - if (atomic_read(&hardware_enable_failed)) { - hardware_disable_all_nolock(); - r =3D -EBUSY; - } - } - - mutex_unlock(&kvm_lock); - cpus_read_unlock(); - - return r; -} - static int kvm_reboot(struct notifier_block *notifier, unsigned long val, void *v) { @@ -5132,7 +5026,6 @@ static int kvm_reboot(struct notifier_block *notifier= , unsigned long val, /* This hook is called without cpuhotplug disabled. */ cpus_read_lock(); mutex_lock(&kvm_lock); - on_each_cpu(hardware_disable_nolock, NULL, 1); r =3D kvm_arch_reboot(val); mutex_unlock(&kvm_lock); cpus_read_unlock(); @@ -5731,17 +5624,13 @@ static int kvm_suspend(void) * locking. */ lockdep_assert_not_held(&kvm_lock); - if (kvm_usage_count) - hardware_disable_nolock(NULL); return kvm_arch_suspend(kvm_usage_count); } =20 static void kvm_resume(void) { - kvm_arch_resume(kvm_usage_count); lockdep_assert_not_held(&kvm_lock); - if (kvm_usage_count) - hardware_enable_nolock(NULL); + kvm_arch_resume(kvm_usage_count); } =20 static struct syscore_ops kvm_syscore_ops =3D { @@ -5879,11 +5768,6 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsig= ned vcpu_align, if (r) goto out_irqfd; =20 - if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) { - r =3D -ENOMEM; - goto out_free_0; - } - r =3D kvm_arch_hardware_setup(opaque); if (r < 0) goto out_free_1; @@ -5960,8 +5844,6 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsign= ed vcpu_align, out_free_2: kvm_arch_hardware_unsetup(); out_free_1: - free_cpumask_var(cpus_hardware_enabled); -out_free_0: kvm_irqfd_exit(); out_irqfd: kvm_arch_exit(); @@ -5983,11 +5865,12 @@ void kvm_exit(void) unregister_syscore_ops(&kvm_syscore_ops); unregister_reboot_notifier(&kvm_reboot_notifier); cpuhp_remove_state_nocalls(CPUHP_AP_KVM_ONLINE); - on_each_cpu(hardware_disable_nolock, NULL, 1); + cpus_read_lock(); + kvm_arch_pre_hardware_unsetup(); kvm_arch_hardware_unsetup(); + cpus_read_unlock(); kvm_arch_exit(); kvm_irqfd_exit(); - free_cpumask_var(cpus_hardware_enabled); kvm_vfio_ops_exit(); } EXPORT_SYMBOL_GPL(kvm_exit); --=20 2.25.1 From nobody Fri Apr 10 18:34:59 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 39D49ECAAD4 for ; Tue, 30 Aug 2022 12:03:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229880AbiH3MC6 (ORCPT ); Tue, 30 Aug 2022 08:02:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38356 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230136AbiH3MCD (ORCPT ); Tue, 30 Aug 2022 08:02:03 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4C7ADE9270; Tue, 30 Aug 2022 05:02:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661860921; x=1693396921; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=acOpBp0TwBzAoRqBKx4NbGM0ZNJfNy50Nx4gx4/JIVM=; b=iyrRPUhSW86ZS93avhRpdCAyOVLznH6MMI/VrAIhOwlUcwDNCV2mYPTW 20MtM8hcLxkrYE7040NZ8BsK4UwSovvT2Hq46UQxS2Owh1neK0CavlgsZ wkVAzAf8BZ5evLbTnGtCE3EYKO+1oNnLgapD+oWavojLOVVD/g0XV+IUt GOVwVge8cPjUBVO44htF3r1NS3Nrd7Ctb1NVMj9in8P327rMM3v9mUV6Y pJ+R8MpOAmcdzB/fF1m/OdwiK4HP7koxONgsAnG2gXgx9myJpBe5xJMIs uN5xECCibUkPKfDT2UCIe3EOjuoLFSUOowRKmk1xrDGiD2jQS0KmP/g0z A==; X-IronPort-AV: E=McAfee;i="6500,9779,10454"; a="356870982" X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="356870982" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:58 -0700 X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="787469644" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:58 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , Sean Christopherson , Kai Huang , Chao Gao , Will Deacon Subject: [PATCH v2 11/19] KVM: kvm_arch.c: Remove _nolock post fix Date: Tue, 30 Aug 2022 05:01:26 -0700 Message-Id: <284273dc6c1310812681c1de901c552788ab2897.1661860550.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata Now all related callbacks are called under kvm_lock, no point for _nolock post fix. Remove _nolock post fix for short function name. Signed-off-by: Isaku Yamahata --- virt/kvm/kvm_arch.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c index 51c6e9f03ed5..491e92ef9e3d 100644 --- a/virt/kvm/kvm_arch.c +++ b/virt/kvm/kvm_arch.c @@ -20,7 +20,7 @@ __weak int kvm_arch_post_init_vm(struct kvm *kvm) return 0; } =20 -static void hardware_enable_nolock(void *junk) +static void hardware_enable(void *junk) { int cpu =3D raw_smp_processor_id(); int r; @@ -41,7 +41,7 @@ static void hardware_enable_nolock(void *junk) } } =20 -static void hardware_disable_nolock(void *junk) +static void hardware_disable(void *junk) { int cpu =3D raw_smp_processor_id(); =20 @@ -55,7 +55,7 @@ static void hardware_disable_nolock(void *junk) =20 __weak void kvm_arch_pre_hardware_unsetup(void) { - on_each_cpu(hardware_disable_nolock, NULL, 1); + on_each_cpu(hardware_disable, NULL, 1); } =20 /* @@ -70,7 +70,7 @@ __weak int kvm_arch_add_vm(struct kvm *kvm, int usage_cou= nt) return 0; =20 atomic_set(&hardware_enable_failed, 0); - on_each_cpu(hardware_enable_nolock, NULL, 1); + on_each_cpu(hardware_enable, NULL, 1); =20 if (atomic_read(&hardware_enable_failed)) { r =3D -EBUSY; @@ -80,7 +80,7 @@ __weak int kvm_arch_add_vm(struct kvm *kvm, int usage_cou= nt) r =3D 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; } =20 @@ -89,7 +89,7 @@ __weak int kvm_arch_del_vm(int usage_count) if (usage_count) return 0; =20 - on_each_cpu(hardware_disable_nolock, NULL, 1); + on_each_cpu(hardware_disable, NULL, 1); return 0; } =20 @@ -105,7 +105,7 @@ __weak int kvm_arch_online_cpu(unsigned int cpu, int us= age_count) if (usage_count) { WARN_ON_ONCE(atomic_read(&hardware_enable_failed)); =20 - hardware_enable_nolock(NULL); + hardware_enable(NULL); if (atomic_read(&hardware_enable_failed)) { atomic_set(&hardware_enable_failed, 0); ret =3D -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; } =20 __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; } =20 __weak int kvm_arch_suspend(int usage_count) { if (usage_count) - hardware_disable_nolock(NULL); + hardware_disable(NULL); return 0; } =20 __weak void kvm_arch_resume(int usage_count) { if (usage_count) - hardware_enable_nolock(NULL); + hardware_enable(NULL); } --=20 2.25.1 From nobody Fri Apr 10 18:34:59 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CDD61ECAAD4 for ; Tue, 30 Aug 2022 12:02:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229938AbiH3MCs (ORCPT ); Tue, 30 Aug 2022 08:02:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38326 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230109AbiH3MCC (ORCPT ); Tue, 30 Aug 2022 08:02:02 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4C5BCE830A; Tue, 30 Aug 2022 05:02:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661860921; x=1693396921; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=tGT6aN2J7OkyrHuSTFcMkwLGe6Avk3mojHwqe3QkYZQ=; b=f4ylNustuQ8RJJdGNZY1dGRnZigmahv1TpRC28RHtwRNfmfoDtxg/XQr FLdo+7lWq3UNEgPepevrepRt8AL5cvcSmtSw6ID15c1tUjQgX0+3VQ7M0 dTDG37Z1TNz+QIPv1jw5c8c1hXqtHfbjNVAx3OTgnegm9E/spzSjwbXRU fo3JFABFKb7XHytgcNfzJoNIK/r7r4KlWhN5nLtJVDja5IP5m45M2gdLO z0V/AO+48r1OVKYqvySoYNjjlrkZBMMXf1/KDrdBG9RZRJqUl9AIk5mur cItWYcp7U2WXmv5S10Nxsqm8cmcjxExsjuggubdTyqJDn3KTlfqKrMKIZ Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10454"; a="356870985" X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="356870985" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:58 -0700 X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="787469647" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:58 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , Sean Christopherson , Kai Huang , Chao Gao , Will Deacon Subject: [PATCH v2 12/19] KVM: kvm_arch.c: Remove a global variable, hardware_enable_failed Date: Tue, 30 Aug 2022 05:01:27 -0700 Message-Id: <078f2c056134ffe867c5ce72a1e7608fa4064779.1661860550.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata 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. Signed-off-by: Isaku Yamahata --- virt/kvm/kvm_arch.c | 56 +++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c index 491e92ef9e3d..3990f85edab3 100644 --- a/virt/kvm/kvm_arch.c +++ b/virt/kvm/kvm_arch.c @@ -13,14 +13,13 @@ #include =20 static cpumask_t cpus_hardware_enabled =3D CPU_MASK_NONE; -static atomic_t hardware_enable_failed; =20 __weak int kvm_arch_post_init_vm(struct kvm *kvm) { return 0; } =20 -static void hardware_enable(void *junk) +static int __hardware_enable(void) { int cpu =3D raw_smp_processor_id(); int r; @@ -28,17 +27,21 @@ static void hardware_enable(void *junk) WARN_ON_ONCE(preemptible()); =20 if (cpumask_test_cpu(cpu, &cpus_hardware_enabled)) - return; - - cpumask_set_cpu(cpu, &cpus_hardware_enabled); - + return 0; r =3D 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 =3D arg; + + if (__hardware_enable()) + atomic_inc(failed); } =20 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 =3D 0; =20 if (usage_count !=3D 1) return 0; =20 - atomic_set(&hardware_enable_failed, 0); - on_each_cpu(hardware_enable, NULL, 1); + atomic_set(&failed, 0); + on_each_cpu(hardware_enable, &failed, 1); =20 - if (atomic_read(&hardware_enable_failed)) { + if (atomic_read(&failed)) { r =3D -EBUSY; goto err; } @@ -95,23 +99,15 @@ __weak int kvm_arch_del_vm(int usage_count) =20 __weak int kvm_arch_online_cpu(unsigned int cpu, int usage_count) { - int ret =3D 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 =3D -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; } =20 __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(); } --=20 2.25.1 From nobody Fri Apr 10 18:34:59 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BFDB8ECAAD4 for ; Tue, 30 Aug 2022 12:03:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231205AbiH3MDG (ORCPT ); Tue, 30 Aug 2022 08:03:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38768 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230329AbiH3MCK (ORCPT ); Tue, 30 Aug 2022 08:02:10 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3E834EA319; Tue, 30 Aug 2022 05:02:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661860923; x=1693396923; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9IEoA4ocQWS+BnaWE2eKSbUpco8c34vmEkNWuWscBno=; b=hHfKnQobAHiEoMzxgq07jRaP5BV/6b5IEWnB5iUWYzC9thg6BI3Et2CE eGEfs/RZeoTxwr02uxndbXGmv4dRqvFeJYUYM0f5obuOvJIrGInT4u1dh JrZZTrsTzI21RZqOuLCMCgiAlWIXGKzO0cSHG7Gs2mIC8fMHURXiN0LaQ 6ju1VsAqefG4tYVJsraw1HB18qnLll3e8o7k9HkWbSa5GAMmHODSVnWiP dKrDg4haDzhyOZcgJgsXC24Tyq4nkwCuKiRZEtI3Ssgc4zpBGx2VezqB7 jLwN9n/D/val6W/3QjfdSUAgvHiT/k8XLUQdH/EFRsLQiCLQDmRJRUX70 w==; X-IronPort-AV: E=McAfee;i="6500,9779,10454"; a="356870988" X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="356870988" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:59 -0700 X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="787469651" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:59 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , Sean Christopherson , Kai Huang , Chao Gao , Will Deacon Subject: [PATCH v2 13/19] KVM: Do processor compatibility check on cpu online and resume Date: Tue, 30 Aug 2022 05:01:28 -0700 Message-Id: <436cecd4b5a11056f89a46f2cd1cae67be18b192.1661860550.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata 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()). Signed-off-by: Isaku Yamahata --- arch/x86/kvm/x86.c | 2 -- virt/kvm/kvm_arch.c | 15 +++++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 0b112cd7de58..ac185e199f69 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12003,8 +12003,6 @@ int kvm_arch_check_processor_compat(void) { struct cpuinfo_x86 *c =3D &cpu_data(smp_processor_id()); =20 - WARN_ON(!irqs_disabled()); - if (__cr4_reserved_bits(cpu_has, c) !=3D __cr4_reserved_bits(cpu_has, &boot_cpu_data)) return -EIO; diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c index 3990f85edab3..e440d4a99c8a 100644 --- a/virt/kvm/kvm_arch.c +++ b/virt/kvm/kvm_arch.c @@ -99,6 +99,12 @@ __weak int kvm_arch_del_vm(int usage_count) =20 __weak int kvm_arch_online_cpu(unsigned int cpu, int usage_count) { + int r; + + r =3D 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) =20 __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(); } --=20 2.25.1 From nobody Fri Apr 10 18:34:59 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BB4F2ECAAA1 for ; Tue, 30 Aug 2022 12:03:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229849AbiH3MDL (ORCPT ); Tue, 30 Aug 2022 08:03:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38772 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230338AbiH3MCK (ORCPT ); Tue, 30 Aug 2022 08:02:10 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D672EA8A5; Tue, 30 Aug 2022 05:02:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661860923; x=1693396923; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=KB0aXNh0+CQxndncWszmaVeXEIeP3GeVpY9ut6U46AE=; b=L4+thYa7bSEqYpNMAzB9wv7swEdCQW5m37xa+8wqFvx+KdKn/G5wUhbI NYp0tUH0coapCEYqJTAutu/jkdBFhrB8YOj6mRum2xXBKL0Sgw2NPAFGV q9C/T0sFpczYPNOGTEKGtFfHKOsXh4yFHQwe3U99o9Ez98AdQvyZ8JBBG GFUFJm5lEnkvAkTM84yF0wz/wZTlQ2EsQwLmGCWA1D/KaewqP3hUi+9ls oyChMrA5tjv8qfetCOU/XrtqdV6D4FLfmNkaiE2WnyN4kmvv2L72NtdNk 8A2duSI+k0l9YDctOp1dUV1YlU1n4F78TnINr1ThuniGMU6+9z73uz7u8 w==; X-IronPort-AV: E=McAfee;i="6500,9779,10454"; a="356870992" X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="356870992" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:59 -0700 X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="787469654" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:59 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , Sean Christopherson , Kai Huang , Chao Gao , Will Deacon Subject: [PATCH v2 14/19] KVM: x86: Duplicate arch callbacks related to pm events Date: Tue, 30 Aug 2022 05:01:29 -0700 Message-Id: <214d73856dbb76237abf7a267a577ab3c312ad28.1661860550.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata KVM/X86 can change those callbacks without worrying about breaking other archs. Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- arch/x86/kvm/x86.c | 131 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 126 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index ac185e199f69..2485f3d792b2 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11841,6 +11841,130 @@ void kvm_arch_hardware_disable(void) drop_user_return_notifiers(); } =20 +static cpumask_t cpus_hardware_enabled =3D 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 =3D raw_smp_processor_id(); + int r; + + WARN_ON_ONCE(preemptible()); + + if (cpumask_test_cpu(cpu, &cpus_hardware_enabled)) + return 0; + r =3D 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 =3D arg; + + if (__hardware_enable()) + atomic_inc(failed); +} + +static void hardware_disable(void *junk) +{ + int cpu =3D 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 =3D 0; + + if (usage_count !=3D 1) + return 0; + + atomic_set(&failed, 0); + on_each_cpu(hardware_enable, &failed, 1); + + if (atomic_read(&failed)) { + r =3D -EBUSY; + goto err; + } + + r =3D 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 =3D 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; =20 + if (kvm_arch_check_processor_compat()) + return; if (kvm_arch_hardware_enable()) return; =20 @@ -12102,11 +12228,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned lon= g type) return ret; } =20 -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); --=20 2.25.1 From nobody Fri Apr 10 18:34:59 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B2435ECAAD4 for ; Tue, 30 Aug 2022 12:03:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229882AbiH3MDC (ORCPT ); Tue, 30 Aug 2022 08:03:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38766 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230326AbiH3MCK (ORCPT ); Tue, 30 Aug 2022 08:02:10 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B87FFEE48F; Tue, 30 Aug 2022 05:02:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661860923; x=1693396923; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9TGZpPYVEQBXmvcE4fwjJD774pKDGLk/k27m38LhX3M=; b=UwtqOZVEdZFNShF+2h8j0NeQFtoR3FyrPfkoobxzFDtFqbmEsXuoSoIf At5DURQ8l9h+HBrj2AVaA/UBlghff3rqMYIYCOUT2XSsIcC0fv0tbrHff GSr/k/FTnHYuPcTBOQIPq7Wwd8P8Cyi31OAJSeTHbpRy70R8KXE9TzABj X8G+YBoSPPI52uocWViUg8/RtrwGvL8rXF3AaOK+XylBqpmsbqwqcXbUL jk6SlJICqa5GR6Eg/O4oP0X/GPRwA9BCaikFf9ierJEWyE69xaS9DspU+ NOtBoayzXoy65IMLCG0Ze6ONXxfDG9uxWdNPXFtpeNgPgRZjKx7lzBc3Y A==; X-IronPort-AV: E=McAfee;i="6500,9779,10454"; a="356870993" X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="356870993" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:02:00 -0700 X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="787469658" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:01:59 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , Sean Christopherson , Kai Huang , Chao Gao , Will Deacon Subject: [PATCH v2 15/19] KVM: Eliminate kvm_arch_post_init_vm() Date: Tue, 30 Aug 2022 05:01:30 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata 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(). Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- 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(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 2485f3d792b2..e5f066138ee9 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11843,11 +11843,6 @@ void kvm_arch_hardware_disable(void) =20 static cpumask_t cpus_hardware_enabled =3D CPU_MASK_NONE; =20 -int kvm_arch_post_init_vm(struct kvm *kvm) -{ - return kvm_mmu_post_init_vm(kvm); -} - static int __hardware_enable(void) { int cpu =3D raw_smp_processor_id(); @@ -11910,7 +11905,7 @@ int kvm_arch_add_vm(struct kvm *kvm, int usage_coun= t) goto err; } =20 - r =3D kvm_arch_post_init_vm(kvm); + r =3D kvm_mmu_post_init_vm(kvm); err: if (r) on_each_cpu(hardware_disable, NULL, 1); diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 60f4ae9d6f48..8abbf7a1773b 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -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); diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c index e440d4a99c8a..8f2d920a2a8f 100644 --- a/virt/kvm/kvm_arch.c +++ b/virt/kvm/kvm_arch.c @@ -14,11 +14,6 @@ =20 static cpumask_t cpus_hardware_enabled =3D CPU_MASK_NONE; =20 -__weak int kvm_arch_post_init_vm(struct kvm *kvm) -{ - return 0; -} - static int __hardware_enable(void) { int cpu =3D raw_smp_processor_id(); @@ -78,13 +73,8 @@ __weak int kvm_arch_add_vm(struct kvm *kvm, int usage_co= unt) =20 if (atomic_read(&failed)) { r =3D -EBUSY; - goto err; - } - - r =3D kvm_arch_post_init_vm(kvm); -err: - if (r) on_each_cpu(hardware_disable, NULL, 1); + } return r; } =20 --=20 2.25.1 From nobody Fri Apr 10 18:34:59 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A3818ECAAA1 for ; Tue, 30 Aug 2022 12:03:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229978AbiH3MDO (ORCPT ); Tue, 30 Aug 2022 08:03:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230351AbiH3MCN (ORCPT ); Tue, 30 Aug 2022 08:02:13 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 709CEEEC61; Tue, 30 Aug 2022 05:02:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661860926; x=1693396926; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=4GLX7fTifeZyrCGo7kfB6NRlR+GNm5ga5j3w/Xl68d0=; b=mUyFsNGLqPeMsXecwvwgYcEy9cEE00SxC7EtLO0WZgAIkEs3fPFquxCb ivKOmdacTCHPC2z6lbVsoZ++IGnAaIpBpCWE0xEwv8R91XqNWT0ReDb7C QSIDdt1ToarVbN+g1TNf3TeB+r7p+wfPswfWSAfxQ4GK1iBay7e9zKWpm NL37/ma/OuYvJTvMRtUyskVvDQ+bT303/NImaOQEHVQ8a+lQIdWzqO2hv ULKLwjM5+7Src6BNmhpNa15n5BUR5t2B03VRpJhHdeff6lnyoPmn4cyD1 6RciqwxkEUBIp5V6kMekQouJCTcuOv/5RbU5LReGrzeIndZyp/wn+ZSI2 g==; X-IronPort-AV: E=McAfee;i="6500,9779,10454"; a="356870998" X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="356870998" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:02:00 -0700 X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="787469662" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:02:00 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , Sean Christopherson , Kai Huang , Chao Gao , Will Deacon Subject: [PATCH v2 16/19] KVM: x86: Delete kvm_arch_hardware_enable/disable() Date: Tue, 30 Aug 2022 05:01:31 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata Now they're function call and there is no point to keep them. Opportunistically make kvm_arch_pre_hardware_unsetup() empty. Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- arch/x86/kvm/x86.c | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index e5f066138ee9..14a464f7302b 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -354,7 +354,7 @@ static void kvm_on_user_return(struct user_return_notif= ier *urn) =20 /* * 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); =20 -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 =3D CPU_MASK_NONE; =20 static int __hardware_enable(void) @@ -11852,7 +11841,7 @@ static int __hardware_enable(void) =20 if (cpumask_test_cpu(cpu, &cpus_hardware_enabled)) return 0; - r =3D kvm_arch_hardware_enable(); + r =3D 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(); } =20 void kvm_arch_pre_hardware_unsetup(void) { - on_each_cpu(hardware_disable, NULL, 1); + /* TODO: eliminate this function */ } =20 /* @@ -11974,7 +11964,7 @@ void kvm_arch_resume(int usage_count) =20 if (kvm_arch_check_processor_compat()) return; - if (kvm_arch_hardware_enable()) + if (static_call(kvm_x86_hardware_enable)()) return; =20 local_tsc =3D rdtsc(); @@ -12115,6 +12105,8 @@ int kvm_arch_hardware_setup(void *opaque) =20 void kvm_arch_hardware_unsetup(void) { + on_each_cpu(hardware_disable, NULL, 1); + kvm_unregister_perf_callbacks(); =20 static_call(kvm_x86_hardware_unsetup)(); --=20 2.25.1 From nobody Fri Apr 10 18:34:59 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EE523ECAAA1 for ; Tue, 30 Aug 2022 12:03:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230171AbiH3MDV (ORCPT ); Tue, 30 Aug 2022 08:03:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38774 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230438AbiH3MCQ (ORCPT ); Tue, 30 Aug 2022 08:02:16 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B672AEEC73; Tue, 30 Aug 2022 05:02:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661860926; x=1693396926; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=kP8Ku5Lv8rqfw0Nma82+ZaeLrsDvE/lPJOPwAOWO6Gg=; b=V6VroCoJ2xHmV7u8ZVWMZVnx1fu9SvSLRyl3dYPdAlHo72FbO/hevdqG CeGrtJZLIOQytEKZKrlbYRt8WbhTZBccHubrmAKNnRt8ylWx+d1ozLioZ vIQxY/jR5XeIYnRRKBZY5IrX9e8mqfnPKlRMcS+FCjXJCjOYj9xRAEjBa PvJzNOgJFLbo3Ml8Q6/Q1SIWOlmLaWLr3oCw+f0lHMqw+DSP8EewhDj5J /jKfdeZhnaZOSQDT8NHjP2iz9ZAEfmWNpvmbg9GIui+dwWsLaQ0vnnx/u 8VbOkse7G09jft4n5i4DwMUXixfrgeFV6KM4BCRMKavxv7vR0QDAd1aa6 w==; X-IronPort-AV: E=McAfee;i="6500,9779,10454"; a="356871002" X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="356871002" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:02:00 -0700 X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="787469668" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:02:00 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , Sean Christopherson , Kai Huang , Chao Gao , Will Deacon Subject: [PATCH v2 17/19] KVM: Add config to not compile kvm_arch.c Date: Tue, 30 Aug 2022 05:01:32 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata 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. Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- 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(-) diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig index e3cbd7706136..e2e16205425d 100644 --- a/arch/x86/kvm/Kconfig +++ b/arch/x86/kvm/Kconfig @@ -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 diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 8abbf7a1773b..74111118db42 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1434,8 +1434,10 @@ void kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *v= cpu, struct dentry *debugfs_ static inline void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu) {} #endif =20 +#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); diff --git a/virt/kvm/Kconfig b/virt/kvm/Kconfig index a8c5c9f06b3c..917314a87696 100644 --- a/virt/kvm/Kconfig +++ b/virt/kvm/Kconfig @@ -72,3 +72,6 @@ config KVM_XFER_TO_GUEST_WORK =20 config HAVE_KVM_PM_NOTIFIER bool + +config HAVE_KVM_OVERRIDE_HARDWARE_ENABLE + def_bool n diff --git a/virt/kvm/Makefile.kvm b/virt/kvm/Makefile.kvm index c4210acabd35..c0187ec4f83c 100644 --- a/virt/kvm/Makefile.kvm +++ b/virt/kvm/Makefile.kvm @@ -5,7 +5,10 @@ =20 KVM ?=3D ../../../virt/kvm =20 -kvm-y :=3D $(KVM)/kvm_main.o $(KVM)/kvm_arch.o $(KVM)/eventfd.o $(KVM)/bin= ary_stats.o +kvm-y :=3D $(KVM)/kvm_main.o $(KVM)/eventfd.o $(KVM)/binary_stats.o +ifneq ($(CONFIG_HAVE_KVM_OVERRIDE_HARDWARE_ENABLE), y) +kvm-y +=3D $(KVM)/kvm_arch.o +endif kvm-$(CONFIG_KVM_VFIO) +=3D $(KVM)/vfio.o kvm-$(CONFIG_KVM_MMIO) +=3D $(KVM)/coalesced_mmio.o kvm-$(CONFIG_KVM_ASYNC_PF) +=3D $(KVM)/async_pf.o --=20 2.25.1 From nobody Fri Apr 10 18:34:59 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CA2C4ECAAA1 for ; Tue, 30 Aug 2022 12:03:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229591AbiH3MDS (ORCPT ); Tue, 30 Aug 2022 08:03:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38766 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230393AbiH3MCP (ORCPT ); Tue, 30 Aug 2022 08:02:15 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B6B2AEEF03; Tue, 30 Aug 2022 05:02:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661860926; x=1693396926; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=DKukNtisSOiD+eagzRE30K2nu8p0CySa/kXhW91x3mY=; b=TJtyW0zvQccpI3+wUx8aXF7K3yPlxLhge+Drf7iP9M015vuzHkrysB5z K/gaY1Jn4IFiOVi0KGTM/SfBg45yo3GFpjsf1sSdmF7qo6J8ef/6BNh41 +oeK8ajK7+92jjvmxPV/q0yCKLpQt0KC86uWR7hstOkT5ZDiKpjgSOn8t cCmu6Lq5ShYL5AVbqlWGGEXToWGKzImLTgbqzD9caeeBJNMbWmcUeN6WI 9LFECkyLitUdk/wZ1yvcXaBwWM6Y235C8Npy5NPQNXVp3m5RQhfu5Tfln EZKex8od5r2HoB7Q5Ri7tRH2YVpS4QSgbnDrJCNUksTKvZcQ3IpZ8d1hb A==; X-IronPort-AV: E=McAfee;i="6500,9779,10454"; a="356871005" X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="356871005" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:02:01 -0700 X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="787469675" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:02:01 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , Sean Christopherson , Kai Huang , Chao Gao , Will Deacon Subject: [PATCH v2 18/19] RFC: KVM: x86: Remove cpus_hardware_enabled and related sanity check Date: Tue, 30 Aug 2022 05:01:33 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata cpus_hardware_enabled mask seems incomplete protection against other kernel component using CPU virtualization feature. Because it's obscure and incomplete, remove the check. Signed-off-by: Isaku Yamahata --- arch/x86/kvm/x86.c | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 14a464f7302b..10b83cbb29ba 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11830,22 +11830,15 @@ void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu= *vcpu, u8 vector) } EXPORT_SYMBOL_GPL(kvm_vcpu_deliver_sipi_vector); =20 -static cpumask_t cpus_hardware_enabled =3D CPU_MASK_NONE; - static int __hardware_enable(void) { - int cpu =3D raw_smp_processor_id(); int r; =20 WARN_ON_ONCE(preemptible()); =20 - if (cpumask_test_cpu(cpu, &cpus_hardware_enabled)) - return 0; r =3D 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; } =20 @@ -11859,13 +11852,7 @@ static void hardware_enable(void *arg) =20 static void hardware_disable(void *junk) { - int cpu =3D 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(); } --=20 2.25.1 From nobody Fri Apr 10 18:34:59 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D72B4ECAAD4 for ; Tue, 30 Aug 2022 12:03:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230429AbiH3MD1 (ORCPT ); Tue, 30 Aug 2022 08:03:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229756AbiH3MCg (ORCPT ); Tue, 30 Aug 2022 08:02:36 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F4B2EF9D8; Tue, 30 Aug 2022 05:02:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661860928; x=1693396928; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=eeD6WJYKHUyfZ/PXC4poyF6VyKFe0V4LAqKVsAEhnLI=; b=CSqQfYURJd8rkqxNgLXcQ2YySp2cJZ49YZsXvGQmTFM47UqF307o167C M2AIfIJ/pzLxJek3sDX3iPRlkxmtgL+sIM9BEPH64jl9u5ceeAs/ASjxg EkqHM6eyfxM0cGi2PHkgZhD4tyb86Ncd4AmetbaeheQWTnEn3Kea/wJkl cCRUjXbpCocUB7kZEcz3VwxhptAomNQillKi/ZEw/wsR/dGITbodYYnup tBJcCOj2zchOhjndqlsFXQZM8eDzqC9S1hQLsXr1jDeuaR+wGTb+u7YcB wp3o83Y4AZjiNJp0Mnkkl06v0GnvaI0mO3fkPRXB9GViL0tIbGaxjSxNz Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10454"; a="356871006" X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="356871006" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:02:01 -0700 X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="787469680" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 05:02:01 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , Sean Christopherson , Kai Huang , Chao Gao , Will Deacon Subject: [PATCH v2 19/19] RFC: KVM: Remove cpus_hardware_enabled and related sanity check Date: Tue, 30 Aug 2022 05:01:34 -0700 Message-Id: <21b2efeb4b43605a8c1a327a3656650d96be0968.1661860550.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata cpus_hardware_enabled mask seems incomplete protection against other kernel component using CPU virtualization feature. Because it's obscure and incomplete, remove the check. Signed-off-by: Isaku Yamahata --- virt/kvm/kvm_arch.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c index 8f2d920a2a8f..cbad0181c177 100644 --- a/virt/kvm/kvm_arch.c +++ b/virt/kvm/kvm_arch.c @@ -12,22 +12,16 @@ =20 #include =20 -static cpumask_t cpus_hardware_enabled =3D CPU_MASK_NONE; - static int __hardware_enable(void) { - int cpu =3D raw_smp_processor_id(); int r; =20 WARN_ON_ONCE(preemptible()); =20 - if (cpumask_test_cpu(cpu, &cpus_hardware_enabled)) - return 0; r =3D 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; } =20 @@ -41,13 +35,7 @@ static void hardware_enable(void *arg) =20 static void hardware_disable(void *junk) { - int cpu =3D 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(); } =20 --=20 2.25.1