From nobody Mon Apr 6 08:13:35 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 7EB4BC54EE9 for ; Thu, 8 Sep 2022 23:26:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229891AbiIHX0I (ORCPT ); Thu, 8 Sep 2022 19:26:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229608AbiIHX0F (ORCPT ); Thu, 8 Sep 2022 19:26:05 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B7501E3D68; Thu, 8 Sep 2022 16:26: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=1662679563; x=1694215563; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=4yHIwH86LwwyESKSGChTujh8Vj2N18i6tKI9EL0jUr4=; b=hWj/WLckaVvfPS8PJAkkKNfiTLQ8WqKqjK5bsJjbuxgrPf22FSa7W+0z TqLf7ykZmmFP6Xv+CTwv1Iey4UScmaMxvw/o4hlfs4FSU2kfI4M+X18R1 PRF9Skm/EEZFxNlDVmVdASaXt3SDOrULO7OKHDoZk+KJXIDHTyIuRl55+ 3FG9thx5tjp8WRgGsigSuTRx91o0pzsspLOzpmjWRaNQ7Qec4arW9Dwbl QyXtweQprPbwHnhhvYeLLRWYD6pddAauPyOLSX0hUVlavsmuegWrKGykC Av+zAx1eCUmhXc7fRKeqcKm7OE8MM5LUyguDLt1O7PZaI+MFmvTvwqPeg w==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298686970" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298686970" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:03 -0700 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="610863138" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:02 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier , Will Deacon , Yuan Yao Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen , Yuan Yao Subject: [PATCH v4 01/26] KVM: x86: Drop kvm_user_return_msr_cpu_online() Date: Thu, 8 Sep 2022 16:25:17 -0700 Message-Id: <066b3d43586bab4ae1f6175951c3c7af945f9107.1662679124.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: Sean Christopherson 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. Signed-off-by: Sean Christopherson Signed-off-by: Isaku Yamahata Reviewed-by: Chao Gao Reviewed-by: Yuan Yao --- arch/x86/kvm/x86.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 205ebdc2b11b..73dccc952dd1 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -196,6 +196,7 @@ module_param(eager_page_split, bool, 0644); =20 struct kvm_user_return_msrs { struct user_return_notifier urn; + bool initialized; bool registered; struct kvm_user_return_msr_values { u64 host; @@ -409,18 +410,20 @@ int kvm_find_user_return_msr(u32 msr) } EXPORT_SYMBOL_GPL(kvm_find_user_return_msr); =20 -static void kvm_user_return_msr_cpu_online(void) +static void kvm_user_return_msr_init_cpu(struct kvm_user_return_msrs *msrs) { - unsigned int cpu =3D smp_processor_id(); - struct kvm_user_return_msrs *msrs =3D per_cpu_ptr(user_return_msrs, cpu); u64 value; int i; =20 + if (msrs->initialized) + return; + 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; } + msrs->initialized =3D true; } =20 int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask) @@ -429,6 +432,8 @@ int kvm_set_user_return_msr(unsigned slot, u64 value, u= 64 mask) struct kvm_user_return_msrs *msrs =3D per_cpu_ptr(user_return_msrs, cpu); int err; =20 + kvm_user_return_msr_init_cpu(msrs); + value =3D (value & mask) | (msrs->values[slot].host & ~mask); if (value =3D=3D msrs->values[slot].curr) return 0; @@ -9212,7 +9217,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.initialized =3D false. + * See kvm_user_return_msr_init_cpu(). + */ + 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 +11846,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 Mon Apr 6 08:13:35 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 062FDC6FA82 for ; Thu, 8 Sep 2022 23:26:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229609AbiIHX0L (ORCPT ); Thu, 8 Sep 2022 19:26:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43258 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229771AbiIHX0H (ORCPT ); Thu, 8 Sep 2022 19:26:07 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1AEAE3D77; Thu, 8 Sep 2022 16:26:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662679564; x=1694215564; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Q5um5aONO7+CjLGrGHvgwAkBekvzLE2bkGPRvzcCfEc=; b=Q39K/vKEvmoLdphyj5URhVrc7L6IN3NR2ZaSHlcy5nMlEcip1OK6rrOD PZhMCzEd6E8NwPDe8lbWEvtyK/U3Fk3HhYUk7ehg8jdDrEPcQ3kr9ZaKJ MV3ZtRDIEjCvpUEHjafPgPxL5BV8HtxdcNRFgSidqAHJcZ2GbImO5WiKp oUSvDeLYUe55e9kUvZNFUIs3puNmqmyz+T5ly9NwjP8rHf23PYlZn64hu kqsgsA9sijWdHoP7GVy0lzWPrj+NHmLohywhqFJsFgUkqv0UENA5laWli apAHA6QudqTxL0XUATQKBUBqxAhNDg8e9wHB9IohqwA5dTO5+B+yAJogY Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298686973" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298686973" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:03 -0700 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="610863144" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:03 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier , Will Deacon , Yuan Yao Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen , Yuan Yao Subject: [PATCH v4 02/26] KVM: x86: Use this_cpu_ptr() instead of per_cpu_ptr(smp_processor_id()) Date: Thu, 8 Sep 2022 16:25:18 -0700 Message-Id: <199c87dbdb8d3ec10e650c4919709257e887ba1c.1662679124.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 Reviewed-by: Yuan Yao --- 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 73dccc952dd1..0368eab6a7b5 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -428,8 +428,7 @@ static void kvm_user_return_msr_init_cpu(struct kvm_use= r_return_msrs *msrs) =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); int err; =20 kvm_user_return_msr_init_cpu(msrs); @@ -453,8 +452,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 Mon Apr 6 08:13:35 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 4F3EBC6FA82 for ; Thu, 8 Sep 2022 23:26:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230026AbiIHX0Q (ORCPT ); Thu, 8 Sep 2022 19:26:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43292 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229817AbiIHX0H (ORCPT ); Thu, 8 Sep 2022 19:26:07 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76246C167C; Thu, 8 Sep 2022 16:26:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662679565; x=1694215565; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=wfRlIdF7igCLq7ULumpJnznD7PdyklLJuoeH8ATvUgc=; b=jMPcBaz3Mh8yJMjxqNo64joZbz0Cs1OcqeiEweV88XmMLUCQnmOZGXMU Y7riw6j1NvZxJEi4zSzkwG95F9sEUDruCqSpnbZgmdUNAEV/rXZJJsa0h H7XAHpBe7mhZnlqGlHm6ixM0GgUdVWDxGP/ETXcc7EKHItgN248kUF5eJ lqy6W/+/wZefeBLRWkZRinQDmQBgpnqg/dXsOQvHwakJReZxK/P6u4cYA yoVk9ghrOfIb5abgovM4WOt1ILAFI2jQQK8IRsvhsUqCmSSN8OjIhLD7q XyBsjE1RDQpSP47+RZHDoczyAE6MmDpU9pa2shHrRuraUA3JLdhqzj7R7 w==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298686975" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298686975" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:04 -0700 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="610863151" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:03 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier , Will Deacon , Yuan Yao Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen , Yuan Yao Subject: [PATCH v4 03/26] KVM: x86: Move check_processor_compatibility from init ops to runtime ops Date: Thu, 8 Sep 2022 16:25:19 -0700 Message-Id: <6b43a79970d886877f87a357e2a152464fe5dde2.1662679124.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 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 Reviewed-by: Yuan Yao --- 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 0368eab6a7b5..a7fa55ba344d 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12001,7 +12001,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 @@ -12009,7 +12008,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 Mon Apr 6 08:13:35 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 0B8E4C54EE9 for ; Thu, 8 Sep 2022 23:26:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230085AbiIHX0T (ORCPT ); Thu, 8 Sep 2022 19:26:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43302 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229845AbiIHX0H (ORCPT ); Thu, 8 Sep 2022 19:26:07 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4FB6CE5802; Thu, 8 Sep 2022 16:26: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=1662679566; x=1694215566; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=53WzOSf0JGtTh5VFYsrLaKgBl/Q3W0LzCWVRRVwS+nw=; b=KMwaQELqlcE2e0/mFPRhPCF3qWsAoCzVqvTpWO9b71CGugnuD8/uDy6H S88zxSyJDFtsdQZTniFrBP17CJmcBJOUq4FbbUqjHGRNm6SnJlVRdqc7e yKp9KVARJn1u8pKXuLudWWKyaFkVI1c6UuuxX3The9t5YPUnXH2zylDM7 7kED03hxChZoZ/NA1zjnvCdI9JNEhgSpMOdzPbR2Ib4FC2JmEPCIViMdo t5FaFR6+BPCDfYZP5leXQYhlcGPyNQXVNsd2NdEMEM0StEGYaEIhAuACy 7mXK0JDq6Pup8nq/9ibj8eiY/lsiHKdCvc4eT/ZmOtv4Dm1WAwTn1jgYZ w==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298686977" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298686977" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:05 -0700 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="610863157" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:04 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier , Will Deacon , Yuan Yao Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen , Suzuki K Poulose , Anup Patel , Claudio Imbrenda , Yuan Yao Subject: [PATCH v4 04/26] Partially revert "KVM: Pass kvm_init()'s opaque param to additional arch funcs" Date: Thu, 8 Sep 2022 16:25: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 This partially reverts commit b99040853738 ("KVM: Pass kvm_init()'s opaque param to additional arch funcs") remove opaque from kvm_arch_check_processor_compat because no one uses this opaque now. Address conflicts for ARM (due to file movement) and manually handle RISC-V which comes after the commit. And changes about kvm_arch_hardware_setup() in original commit are still needed so they are not reverted. 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 Reviewed-by: Yuan Yao --- 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 a7fa55ba344d..f6f014e78ab9 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11998,7 +11998,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 Mon Apr 6 08:13:35 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 B1F47C6FA83 for ; Thu, 8 Sep 2022 23:26:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229862AbiIHX03 (ORCPT ); Thu, 8 Sep 2022 19:26:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43324 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229869AbiIHX0I (ORCPT ); Thu, 8 Sep 2022 19:26:08 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3CE5EE6BA1; Thu, 8 Sep 2022 16:26: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=1662679567; x=1694215567; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Xg0WmMFTfA/g4jLjPGWiDRFU62B4P5RAx9DcjKdahlo=; b=jt4+vvg8LDXTn8uaQYUggCjemcXodlms/P9fsBReVjICUauvcgnJfOjX RkZnu+6W9sNZwP1TC5Ez5zQNFrMZA1tI5+GNFspUe11+l9rRWr/61fggW uNAO1hG3WmMKO0qr0ARrf5Z3KRtRxxXIs6rPVIWjvbSeMZNVgDsM1zhWW wHX44Cdy/MWxX0hlHvT+WcGGTK77F9ZzKfCXMhnLEqdD8tHPjLhz7/M/H gdOnLONYLxdixDAcf40I5gbFl+YkMPik7K25uMROjyEII/Pbxxl6RXHLd e9o+xrDrOmXkDBqsuelBaccZzUSo3tnHrOfLGBWT+BzJbxtcSLX+kwKj9 w==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298686983" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298686983" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:05 -0700 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="610863164" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:05 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier , Will Deacon , Yuan Yao Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen , Yuan Yao Subject: [PATCH v4 05/26] KVM: Provide more information in kernel log if hardware enabling fails Date: Thu, 8 Sep 2022 16:25:21 -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: Sean Christopherson Provide the name of the calling function to hardware_enable_nolock() and include it in the error message to provide additional information on exactly what path failed. Opportunistically bump the pr_info() to pr_warn(), failure to enable virtualization support is warn-worthy as _something_ is wrong with the system. Signed-off-by: Sean Christopherson Signed-off-by: Chao Gao Link: https://lore.kernel.org/r/20220216031528.92558-4-chao.gao@intel.com Signed-off-by: Isaku Yamahata Reviewed-by: Yuan Yao --- virt/kvm/kvm_main.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 4243a9541543..278eb6cc7cbe 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -4991,7 +4991,7 @@ static struct miscdevice kvm_dev =3D { &kvm_chardev_ops, }; =20 -static void hardware_enable_nolock(void *junk) +static void hardware_enable_nolock(void *caller_name) { int cpu =3D raw_smp_processor_id(); int r; @@ -5006,7 +5006,8 @@ static void hardware_enable_nolock(void *junk) if (r) { cpumask_clear_cpu(cpu, cpus_hardware_enabled); atomic_inc(&hardware_enable_failed); - pr_info("kvm: enabling virtualization on CPU%d failed\n", cpu); + pr_warn("kvm: enabling virtualization on CPU%d failed during %s()\n", + cpu, (const char *)caller_name); } } =20 @@ -5014,7 +5015,7 @@ static int kvm_starting_cpu(unsigned int cpu) { raw_spin_lock(&kvm_count_lock); if (kvm_usage_count) - hardware_enable_nolock(NULL); + hardware_enable_nolock((void *)__func__); raw_spin_unlock(&kvm_count_lock); return 0; } @@ -5063,7 +5064,7 @@ static int hardware_enable_all(void) kvm_usage_count++; if (kvm_usage_count =3D=3D 1) { atomic_set(&hardware_enable_failed, 0); - on_each_cpu(hardware_enable_nolock, NULL, 1); + on_each_cpu(hardware_enable_nolock, (void *)__func__, 1); =20 if (atomic_read(&hardware_enable_failed)) { hardware_disable_all_nolock(); @@ -5686,7 +5687,7 @@ static void kvm_resume(void) { if (kvm_usage_count) { lockdep_assert_not_held(&kvm_count_lock); - hardware_enable_nolock(NULL); + hardware_enable_nolock((void *)__func__); } } =20 --=20 2.25.1 From nobody Mon Apr 6 08:13:35 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 52338C6FA82 for ; Thu, 8 Sep 2022 23:26:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229670AbiIHX0g (ORCPT ); Thu, 8 Sep 2022 19:26:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43372 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229946AbiIHX0J (ORCPT ); Thu, 8 Sep 2022 19:26:09 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 88495D5733; Thu, 8 Sep 2022 16:26:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662679567; x=1694215567; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=vFOBWrFX4i3pHDYScV9XRS4KfXUFySOBs3tEXCYSPH4=; b=J2A/aA7BFtIZd0Ey2gkNWkqbik5SDDIl+2qFjKWXhFdlsxcqUIRUuIJk EaxjOwFKC8fX+awIDw45sb8cmaXgpNjuvMV2cZXjLeEl3eayceBZeE/QS QXMrMfHz7NqxXcjV/OTFP7B/hCRGi4VFWR9UpLuhkCVL6ILfJFuLxwhv/ uxF3Jm5cZmA/YjutBqekPb9w4TWNHKDMk2/uIgcZMZHjKCh8W6kpC5t/Q c8AJLaT5jVznVz12AkFT9NZ9CerQqB3jf1npVqhRXEEjFW13nuK2UWnCU 3meQAV4YtSKNLO1q0Mo7IHohHB1kFiRnG9ZxBikYS0n39omG1ILQI44HO A==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298686989" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298686989" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:06 -0700 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="610863171" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:05 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier , Will Deacon , Yuan Yao Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen Subject: [PATCH v4 06/26] KVM: arm64: Simplify the CPUHP logic Date: Thu, 8 Sep 2022 16:25:22 -0700 Message-Id: <2b74ba0aa514e3e86c4513d12614664002996067.1662679124.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: Marc Zyngier For a number of historical reasons, the KVM/arm64 hotplug setup is pretty complicated, and we have two extra CPUHP notifiers for vGIC and timers. It looks pretty pointless, and gets in the way of further changes. So let's just expose some helpers that can be called from the core CPUHP callback, and get rid of everything else. This gives us the opportunity to drop a useless notifier entry, as well as tidy-up the timer enable/disable, which was a bit odd. Signed-off-by: Marc Zyngier Signed-off-by: Chao Gao Link: https://lore.kernel.org/r/20220216031528.92558-5-chao.gao@intel.com Signed-off-by: Isaku Yamahata --- arch/arm64/kvm/arch_timer.c | 27 ++++++++++----------------- arch/arm64/kvm/arm.c | 13 +++++++++++++ arch/arm64/kvm/vgic/vgic-init.c | 19 ++----------------- include/kvm/arm_arch_timer.h | 4 ++++ include/kvm/arm_vgic.h | 4 ++++ include/linux/cpuhotplug.h | 3 --- 6 files changed, 33 insertions(+), 37 deletions(-) diff --git a/arch/arm64/kvm/arch_timer.c b/arch/arm64/kvm/arch_timer.c index bb24a76b4224..33fca1a691a5 100644 --- a/arch/arm64/kvm/arch_timer.c +++ b/arch/arm64/kvm/arch_timer.c @@ -811,10 +811,18 @@ void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu) ptimer->host_timer_irq_flags =3D host_ptimer_irq_flags; } =20 -static void kvm_timer_init_interrupt(void *info) +void kvm_timer_cpu_up(void) { enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags); - enable_percpu_irq(host_ptimer_irq, host_ptimer_irq_flags); + if (host_ptimer_irq) + enable_percpu_irq(host_ptimer_irq, host_ptimer_irq_flags); +} + +void kvm_timer_cpu_down(void) +{ + disable_percpu_irq(host_vtimer_irq); + if (host_ptimer_irq) + disable_percpu_irq(host_ptimer_irq); } =20 int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value) @@ -976,18 +984,6 @@ void kvm_arm_timer_write_sysreg(struct kvm_vcpu *vcpu, preempt_enable(); } =20 -static int kvm_timer_starting_cpu(unsigned int cpu) -{ - kvm_timer_init_interrupt(NULL); - return 0; -} - -static int kvm_timer_dying_cpu(unsigned int cpu) -{ - disable_percpu_irq(host_vtimer_irq); - return 0; -} - static int timer_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu) { if (vcpu) @@ -1185,9 +1181,6 @@ int kvm_timer_hyp_init(bool has_gic) goto out_free_irq; } =20 - cpuhp_setup_state(CPUHP_AP_KVM_ARM_TIMER_STARTING, - "kvm/arm/timer:starting", kvm_timer_starting_cpu, - kvm_timer_dying_cpu); return 0; out_free_irq: free_percpu_irq(host_vtimer_irq, kvm_get_running_vcpus()); diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 3385fb57c11a..7e83498b83aa 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -1676,7 +1676,15 @@ static void _kvm_arch_hardware_enable(void *discard) =20 int kvm_arch_hardware_enable(void) { + int was_enabled =3D __this_cpu_read(kvm_arm_hardware_enabled); + _kvm_arch_hardware_enable(NULL); + + if (!was_enabled) { + kvm_vgic_cpu_up(); + kvm_timer_cpu_up(); + } + return 0; } =20 @@ -1690,6 +1698,11 @@ static void _kvm_arch_hardware_disable(void *discard) =20 void kvm_arch_hardware_disable(void) { + if (__this_cpu_read(kvm_arm_hardware_enabled)) { + kvm_timer_cpu_down(); + kvm_vgic_cpu_down(); + } + if (!is_protected_kvm_enabled()) _kvm_arch_hardware_disable(NULL); } diff --git a/arch/arm64/kvm/vgic/vgic-init.c b/arch/arm64/kvm/vgic/vgic-ini= t.c index f6d4f4052555..6c7f6ae21ec0 100644 --- a/arch/arm64/kvm/vgic/vgic-init.c +++ b/arch/arm64/kvm/vgic/vgic-init.c @@ -465,17 +465,15 @@ int kvm_vgic_map_resources(struct kvm *kvm) =20 /* GENERIC PROBE */ =20 -static int vgic_init_cpu_starting(unsigned int cpu) +void kvm_vgic_cpu_up(void) { enable_percpu_irq(kvm_vgic_global_state.maint_irq, 0); - return 0; } =20 =20 -static int vgic_init_cpu_dying(unsigned int cpu) +void kvm_vgic_cpu_down(void) { disable_percpu_irq(kvm_vgic_global_state.maint_irq); - return 0; } =20 static irqreturn_t vgic_maintenance_handler(int irq, void *data) @@ -584,19 +582,6 @@ int kvm_vgic_hyp_init(void) return ret; } =20 - ret =3D cpuhp_setup_state(CPUHP_AP_KVM_ARM_VGIC_INIT_STARTING, - "kvm/arm/vgic:starting", - vgic_init_cpu_starting, vgic_init_cpu_dying); - if (ret) { - kvm_err("Cannot register vgic CPU notifier\n"); - goto out_free_irq; - } - kvm_info("vgic interrupt IRQ%d\n", kvm_vgic_global_state.maint_irq); return 0; - -out_free_irq: - free_percpu_irq(kvm_vgic_global_state.maint_irq, - kvm_get_running_vcpus()); - return ret; } diff --git a/include/kvm/arm_arch_timer.h b/include/kvm/arm_arch_timer.h index cd6d8f260eab..1638418f72dd 100644 --- a/include/kvm/arm_arch_timer.h +++ b/include/kvm/arm_arch_timer.h @@ -104,4 +104,8 @@ void kvm_arm_timer_write_sysreg(struct kvm_vcpu *vcpu, u32 timer_get_ctl(struct arch_timer_context *ctxt); u64 timer_get_cval(struct arch_timer_context *ctxt); =20 +/* CPU HP callbacks */ +void kvm_timer_cpu_up(void); +void kvm_timer_cpu_down(void); + #endif diff --git a/include/kvm/arm_vgic.h b/include/kvm/arm_vgic.h index 4df9e73a8bb5..fc4acc91ba06 100644 --- a/include/kvm/arm_vgic.h +++ b/include/kvm/arm_vgic.h @@ -431,4 +431,8 @@ int vgic_v4_load(struct kvm_vcpu *vcpu); void vgic_v4_commit(struct kvm_vcpu *vcpu); int vgic_v4_put(struct kvm_vcpu *vcpu, bool need_db); =20 +/* CPU HP callbacks */ +void kvm_vgic_cpu_up(void); +void kvm_vgic_cpu_down(void); + #endif /* __KVM_ARM_VGIC_H */ diff --git a/include/linux/cpuhotplug.h b/include/linux/cpuhotplug.h index f61447913db9..7337414e4947 100644 --- a/include/linux/cpuhotplug.h +++ b/include/linux/cpuhotplug.h @@ -186,9 +186,6 @@ enum cpuhp_state { CPUHP_AP_TI_GP_TIMER_STARTING, CPUHP_AP_HYPERV_TIMER_STARTING, CPUHP_AP_KVM_STARTING, - CPUHP_AP_KVM_ARM_VGIC_INIT_STARTING, - CPUHP_AP_KVM_ARM_VGIC_STARTING, - CPUHP_AP_KVM_ARM_TIMER_STARTING, /* Must be the last timer callback */ CPUHP_AP_DUMMY_TIMER_STARTING, CPUHP_AP_ARM_XEN_STARTING, --=20 2.25.1 From nobody Mon Apr 6 08:13:35 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 98A0EC54EE9 for ; Thu, 8 Sep 2022 23:26:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230246AbiIHX0l (ORCPT ); Thu, 8 Sep 2022 19:26:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229950AbiIHX0J (ORCPT ); Thu, 8 Sep 2022 19:26:09 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA9E5E72FF; Thu, 8 Sep 2022 16:26:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662679567; x=1694215567; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=bjjM/7k9H5/EDjEZ2WY39fcLYpzeVRwTDof3/tbSHpk=; b=IXTAtLe4dySUGgYhICka1Zd0CYknqP/g3qCnkg6L7Q73Osa3oElQWqNW D4C/ASiord0eeNIohsrtgXsfruPWbfstQK6EmLDuHoZHpPDQ0GMoSPUct DNe9+277v54ePSkuMcg4le9XWlNl9nsxp8dP2hDbu4t/BfQP+XpRIlNPh vHO0yvQPRfO19CAyxJ+Ghj4yyVIgkn4nRAV/5sn8ODVviZdFhrGsdctR6 lIN6g2GuXE5kmQRViNaSuAEhA4YEtottCrrJTTXQHFRF6+ch8+tIpt20k RjgV2KXKK3wXEg7dbeKniPnpk8CYav0N0DXf6ooc54j+wWUTo6XWqu/cz A==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298686991" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298686991" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:06 -0700 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="610863180" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:06 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier , Will Deacon , Yuan Yao Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen , Thomas Gleixner , Yuan Yao Subject: [PATCH v4 07/26] KVM: Rename and move CPUHP_AP_KVM_STARTING to ONLINE section Date: Thu, 8 Sep 2022 16:25:23 -0700 Message-Id: <1d7b1068e05e4fbbeeac009fe1795c4c0170db21.1662679124.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 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 Reviewed-by: Sean Christopherson Signed-off-by: Isaku Yamahata Reviewed-by: Yuan Yao --- 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 7337414e4947..de45be38dd27 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, /* Must be the last timer callback */ CPUHP_AP_DUMMY_TIMER_STARTING, CPUHP_AP_ARM_XEN_STARTING, @@ -200,6 +199,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 278eb6cc7cbe..db1303e2abc9 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -5011,13 +5011,27 @@ static void hardware_enable_nolock(void *caller_nam= e) } } =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((void *)__func__); + 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) @@ -5030,7 +5044,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) @@ -5841,8 +5855,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); @@ -5903,7 +5917,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: @@ -5929,7 +5943,7 @@ void kvm_exit(void) kvm_async_pf_deinit(); unregister_syscore_ops(&kvm_syscore_ops); unregister_reboot_notifier(&kvm_reboot_notifier); - cpuhp_remove_state_nocalls(CPUHP_AP_KVM_STARTING); + cpuhp_remove_state_nocalls(CPUHP_AP_KVM_ONLINE); on_each_cpu(hardware_disable_nolock, NULL, 1); kvm_arch_hardware_unsetup(); kvm_arch_exit(); --=20 2.25.1 From nobody Mon Apr 6 08:13:35 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 A13E9C54EE9 for ; Thu, 8 Sep 2022 23:26:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230286AbiIHX0x (ORCPT ); Thu, 8 Sep 2022 19:26:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43972 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230095AbiIHX0U (ORCPT ); Thu, 8 Sep 2022 19:26:20 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A738BE3D78; Thu, 8 Sep 2022 16:26: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=1662679569; x=1694215569; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=rcsBig+l8i4wXZPyFgjZw0NvXS2qsmb7B5pWOnc3Pk8=; b=kWcqNzGCp8t6c/S/hDoehPKCVplpemKEIhNwKoBNQl2hF/mKSYAg/qjy pMZjku66DcjFHyzc7RsJPl5+40WU8lkg9v0ijbKZN3joEV9Q9rNJ06+K7 10+10fWAJm3OI1eu7c9HWxYBFXjr6PxnAxT7F2HPlJ+LgRNoahTFEgmy0 O7kG58XV3BjQLKkewb1scbCqfuzAHUgDIBdcDHTAw/kJnf8Ux7mxJmRxu kh9PKoR7ARhWyovKdVId7uW3EPfITTEJ8qLLwBlQesfrntX80VZvvi34F QtE/NgHhSxjSQatBl+YMvs7mc5d3EKGI+7HwaGuQJvP804tktKI8qR8mc Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298686994" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298686994" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:07 -0700 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="610863186" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:07 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier , Will Deacon , Yuan Yao Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen , Yuan Yao Subject: [PATCH v4 08/26] KVM: Do compatibility checks on hotplugged CPUs Date: Thu, 8 Sep 2022 16:25:24 -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 At init time, KVM does compatibility checks to ensure that all online CPUs support hardware virtualization and a common set of features. But KVM uses hotplugged CPUs without such compatibility checks. On Intel CPUs, this leads to #GP if the hotplugged CPU doesn't support VMX or vmentry failure if the hotplugged CPU doesn't meet minimal feature requirements. Do compatibility checks when onlining a CPU and abort the online process if the hotplugged CPU is incompatible with online CPUs. CPU hotplug is disabled during hardware_enable_all() to prevent the corner case as shown below. A hotplugged CPU marks itself online in cpu_online_mask (1) and enables interrupt (2) before invoking callbacks registered in ONLINE section (3). So, if hardware_enable_all() is invoked on another CPU right after (2), then on_each_cpu() in hardware_enable_all() invokes hardware_enable_nolock() on the hotplugged CPU before kvm_online_cpu() is called. This makes the CPU escape from compatibility checks, which is risky. start_secondary { ... set_cpu_online(smp_processor_id(), true); <- 1 ... local_irq_enable(); <- 2 ... cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); <- 3 } Keep compatibility checks at KVM init time. It can help to find incompatibility issues earlier and refuse to load arch KVM module (e.g., kvm-intel). Loosen the WARN_ON in kvm_arch_check_processor_compat so that it can be invoked from KVM's CPU hotplug callback (i.e., kvm_online_cpu). Other arch doesn't depends on prohibiting of preemption because powerpc has "strcmp(cur_cpu_spec->cpu_name, "model name")" and other arch has "return 0". Only x86 kvm_arch_check_processor_compat() has issue. Opportunistically, add a pr_err() for setup_vmcs_config() path in vmx_check_processor_compatibility() so that each possible error path has its own error message. Convert printk(KERN_ERR ... to pr_err to please checkpatch.pl Signed-off-by: Chao Gao Reviewed-by: Sean Christopherson Link: https://lore.kernel.org/r/20220216031528.92558-7-chao.gao@intel.com Signed-off-by: Isaku Yamahata Reviewed-by: Yuan Yao --- arch/x86/kvm/vmx/vmx.c | 10 ++++++---- arch/x86/kvm/x86.c | 11 +++++++++-- virt/kvm/kvm_main.c | 18 +++++++++++++++++- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 3cf7f18a4115..2a1ab6495299 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7421,20 +7421,22 @@ static int vmx_check_processor_compatibility(void) { struct vmcs_config vmcs_conf; struct vmx_capability vmx_cap; + int cpu =3D smp_processor_id(); =20 if (!this_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) || !this_cpu_has(X86_FEATURE_VMX)) { - pr_err("kvm: VMX is disabled on CPU %d\n", smp_processor_id()); + pr_err("kvm: VMX is disabled on CPU %d\n", cpu); return -EIO; } =20 - if (setup_vmcs_config(&vmcs_conf, &vmx_cap) < 0) + if (setup_vmcs_config(&vmcs_conf, &vmx_cap) < 0) { + pr_err("kvm: failed to setup vmcs config on CPU %d\n", cpu); return -EIO; + } if (nested) nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, vmx_cap.ept); if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) !=3D 0) { - printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", - smp_processor_id()); + pr_err("kvm: CPU %d feature inconsistency!\n", cpu); return -EIO; } return 0; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index f6f014e78ab9..0ab8db07fa0e 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12000,9 +12000,16 @@ void kvm_arch_hardware_unsetup(void) =20 int kvm_arch_check_processor_compat(void) { - struct cpuinfo_x86 *c =3D &cpu_data(smp_processor_id()); + int cpu =3D smp_processor_id(); + struct cpuinfo_x86 *c =3D &cpu_data(cpu); =20 - WARN_ON(!irqs_disabled()); + /* + * Compatibility checks are done when loading KVM or in KVM's CPU + * hotplug callback. It ensures all online CPUs are compatible to run + * vCPUs. For other cases, compatibility checks are unnecessary or + * even problematic. Try to detect improper usages here. + */ + WARN_ON(!irqs_disabled() && cpu_active(cpu)); =20 if (__cr4_reserved_bits(cpu_has, c) !=3D __cr4_reserved_bits(cpu_has, &boot_cpu_data)) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index db1303e2abc9..0ac00c711384 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -5013,7 +5013,11 @@ static void hardware_enable_nolock(void *caller_name) =20 static int kvm_online_cpu(unsigned int cpu) { - int ret =3D 0; + int ret; + + ret =3D kvm_arch_check_processor_compat(); + if (ret) + return ret; =20 raw_spin_lock(&kvm_count_lock); /* @@ -5073,6 +5077,17 @@ static int hardware_enable_all(void) { int r =3D 0; =20 + /* + * During onlining a CPU, cpu_online_mask is set before kvm_online_cpu() + * is called. on_each_cpu() between them includes the CPU. As a result, + * hardware_enable_nolock() may get invoked before kvm_online_cpu(). + * This would enable hardware virtualization on that cpu without + * compatibility checks, which can potentially crash system or break + * running VMs. + * + * Disable CPU hotplug to prevent this case from happening. + */ + cpus_read_lock(); raw_spin_lock(&kvm_count_lock); =20 kvm_usage_count++; @@ -5087,6 +5102,7 @@ static int hardware_enable_all(void) } =20 raw_spin_unlock(&kvm_count_lock); + cpus_read_unlock(); =20 return r; } --=20 2.25.1 From nobody Mon Apr 6 08:13:35 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 9C9D1C6FA82 for ; Thu, 8 Sep 2022 23:26:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230187AbiIHX0r (ORCPT ); Thu, 8 Sep 2022 19:26:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43958 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230089AbiIHX0U (ORCPT ); Thu, 8 Sep 2022 19:26:20 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 65635E7FA3; Thu, 8 Sep 2022 16:26:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662679569; x=1694215569; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=61DGXVR3NwOa0FQhJYw9VD35jgPmusPgu+hMaBxRsLA=; b=TzY+onBAXD1/ZwOf5jOtTpEWRYzUkxfzTrqsgi2GDVFpm6EFEWUH2iR1 HkdQiQLlZVa+vOCBdhvntlE8XHD7/q72KkaA7DwKuEVU+uyZljSjs2uOj NZh00u9aq5FQ+iPX8shNBPUOW0Ww9mSP9GWzpGgeEryJeLWRgR2cyUqC1 vEmUmylPGH0OhDTuvcAc4cPSTCzMxcbjx6JhDlyqLYG2LF4FaKiEpRF7t 15648UiwFDgf2OayPKVvj0o2ThSL8sFVgosquGRRt3sZkeB/5eAVA5Yj2 g78zRQXun2ZkF3OIEf5S/TRKdYO8wnjYBOlnlhLEMGcxbwggseNoRir7F w==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298686997" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298686997" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:08 -0700 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="610863191" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:07 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier , Will Deacon , Yuan Yao Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen Subject: [PATCH v4 09/26] KVM: Do processor compatibility check on resume Date: Thu, 8 Sep 2022 16:25:25 -0700 Message-Id: <1c302387e21e689f103bf954f355cf49f73d1e82.1662679124.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 on resume. It should be done. The resume is called for resuming from S3/S4. CPUs can be replaced or the kernel can resume from S4 on a different machine. So compatibility check is desirable. Signed-off-by: Isaku Yamahata --- virt/kvm/kvm_main.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 0ac00c711384..fc55447c4dba 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -5715,6 +5715,13 @@ static int kvm_suspend(void) =20 static void kvm_resume(void) { + if (kvm_arch_check_processor_compat()) + /* + * No warning here because kvm_arch_check_processor_compat() + * would have warned with more information. + */ + return; /* FIXME: disable KVM */ + if (kvm_usage_count) { lockdep_assert_not_held(&kvm_count_lock); hardware_enable_nolock((void *)__func__); --=20 2.25.1 From nobody Mon Apr 6 08:13:35 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 EC726C6FA83 for ; Thu, 8 Sep 2022 23:27:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229786AbiIHX05 (ORCPT ); Thu, 8 Sep 2022 19:26:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44036 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230103AbiIHX0V (ORCPT ); Thu, 8 Sep 2022 19:26:21 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6568AE7FA6; Thu, 8 Sep 2022 16:26:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662679569; x=1694215569; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/5ENgdTOlynTudWAHnQs6Wsaa7SqrlFRMqagoPUCpho=; b=W4CspEhs5jqEQkyTEfNxCQMGeuAF3srEaIkgyyWdY7czK8zyx4fRuIJI 4KPq33hP68nyBSV2GQ3iFrzEGE144m6RJT460PFIaft/3BxGgWI0YWS27 kBMy40KRf/mqXuEZaR/2goX4WijkjPsLPNiZaFUS2lC0FVLuF8+Xjzcz0 1tAgf+4D79+YRnyAGkJmVNjJNvfcWZPn/+AqEBHFwl2CYYCb6p5MARddM dMmiqiT4DmIwzy/RWKRHIE0449W9tHD3J7ZWimAxtXwt4E7RW4UTIM6G8 qZ2N0k47iXZuenQkulEFNn180EeTYw2ptWNNyUOCrX3RSfSoDJo9opmtm g==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298687000" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298687000" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:08 -0700 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="610863197" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:08 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier , Will Deacon , Yuan Yao Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen Subject: [PATCH v4 10/26] KVM: Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock Date: Thu, 8 Sep 2022 16:25:26 -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 Because kvm_count_lock unnecessarily complicates the KVM locking convention Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock for simplicity. kvm_arch_hardware_enable/disable() callbacks depend on non-preemptiblity with the spin lock. Add preempt_disable/enable() around hardware enable/disable callback to keep the assumption. Opportunistically add some comments on locking. Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- Documentation/virt/kvm/locking.rst | 14 +++----- virt/kvm/kvm_main.c | 56 +++++++++++++++++++++++------- 2 files changed, 49 insertions(+), 21 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 fc55447c4dba..05ede37edc31 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 *caller_name) int cpu =3D raw_smp_processor_id(); int r; =20 + WARN_ON_ONCE(preemptible()); + if (cpumask_test_cpu(cpu, cpus_hardware_enabled)) return; =20 @@ -5019,7 +5020,7 @@ static int kvm_online_cpu(unsigned int cpu) if (ret) return ret; =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 @@ -5028,13 +5029,20 @@ static int kvm_online_cpu(unsigned int cpu) if (kvm_usage_count) { WARN_ON_ONCE(atomic_read(&hardware_enable_failed)); =20 + /* + * arch callback kvm_arch_hardware_eanble() assumes that + * preemption is disabled for historical reason. Disable + * preemption until all arch callbacks are fixed. + */ + preempt_disable(); hardware_enable_nolock((void *)__func__); + preempt_enable(); if (atomic_read(&hardware_enable_failed)) { atomic_set(&hardware_enable_failed, 0); ret =3D -EIO; } } - raw_spin_unlock(&kvm_count_lock); + mutex_unlock(&kvm_lock); return ret; } =20 @@ -5042,6 +5050,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); @@ -5050,10 +5060,18 @@ static void hardware_disable_nolock(void *junk) =20 static int kvm_offline_cpu(unsigned int cpu) { - raw_spin_lock(&kvm_count_lock); - if (kvm_usage_count) + mutex_lock(&kvm_lock); + if (kvm_usage_count) { + /* + * arch callback kvm_arch_hardware_disable() assumes that + * preemption is disabled for historical reason. Disable + * preemption until all arch callbacks are fixed. + */ + preempt_disable(); hardware_disable_nolock(NULL); - raw_spin_unlock(&kvm_count_lock); + preempt_enable(); + } + mutex_unlock(&kvm_lock); return 0; } =20 @@ -5068,9 +5086,11 @@ 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) @@ -5088,7 +5108,7 @@ static int hardware_enable_all(void) * Disable CPU hotplug to prevent this case from happening. */ cpus_read_lock(); - raw_spin_lock(&kvm_count_lock); + mutex_lock(&kvm_lock); =20 kvm_usage_count++; if (kvm_usage_count =3D=3D 1) { @@ -5101,7 +5121,7 @@ static int hardware_enable_all(void) } } =20 - raw_spin_unlock(&kvm_count_lock); + mutex_unlock(&kvm_lock); cpus_read_unlock(); =20 return r; @@ -5708,8 +5728,18 @@ 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. + */ + lockdep_assert_not_held(&kvm_lock); + + if (kvm_usage_count) { + preempt_disable(); hardware_disable_nolock(NULL); + preempt_enable(); + } return 0; } =20 @@ -5723,8 +5753,10 @@ static void kvm_resume(void) return; /* FIXME: disable KVM */ =20 if (kvm_usage_count) { - lockdep_assert_not_held(&kvm_count_lock); + lockdep_assert_not_held(&kvm_lock); + preempt_disable(); hardware_enable_nolock((void *)__func__); + preempt_enable(); } } =20 --=20 2.25.1 From nobody Mon Apr 6 08:13:35 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 39ED0C6FA83 for ; Thu, 8 Sep 2022 23:27:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230335AbiIHX1C (ORCPT ); Thu, 8 Sep 2022 19:27:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44052 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230119AbiIHX0V (ORCPT ); Thu, 8 Sep 2022 19:26:21 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C6797E7FBF; Thu, 8 Sep 2022 16:26:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662679570; x=1694215570; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=1V8ZmSCzcSN1UW0gzv/AZRntQpG6/pty8E9+EuGOXDU=; b=fKsGWPPEqcKn0pLFW/U4AaiTkrLiDdZHXirZS5tsEh8RFFBmRh8VXF1N 0SuUeW9Go/SHcE7VILG2n8qTwm/isv1KaynVKcH5zAzhnniE3hFl3rf6u hrsVmThP/CG1gSslo481QcE/f4xy2h1hR8NK9gDrvcuBCQQnbZ5ORou+v IuEHOGHVeiV3FEW3/zrxV/pJ5Vzt78j2oJFnRutnec73ZYWlUCKsuAn2U WUwu4h6y0lvpCPh6J3j/Hcb5Y1TRLPyLpcMM7F6CR2MxdO956nCMaxucp bK4TfZf9eS7OA5/2bKuezQB/SRuYH9bpysiFSIi0hVU9GCmMVdflrZlyX A==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298687002" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298687002" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:09 -0700 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="610863202" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:08 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier , Will Deacon , Yuan Yao Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen Subject: [PATCH v4 11/26] KVM: Add arch hooks for PM events with empty stub Date: Thu, 8 Sep 2022 16:25:27 -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 | 58 +++++++++++++++++++++++++--------------- 4 files changed, 88 insertions(+), 22 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 05ede37edc31..951f853f6ac9 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. @@ -5040,6 +5032,10 @@ 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); @@ -5060,6 +5056,8 @@ 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) { /* @@ -5069,10 +5067,15 @@ static int kvm_offline_cpu(unsigned int cpu) */ preempt_disable(); 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); + } preempt_enable(); } mutex_unlock(&kvm_lock); - return 0; + return ret; } =20 static void hardware_disable_all_nolock(void) @@ -5130,6 +5133,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. @@ -5138,8 +5143,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 { @@ -5728,6 +5740,8 @@ static void kvm_init_debug(void) =20 static int kvm_suspend(void) { + int ret; + /* * The caller ensures that CPU hotlug is disabled by * cpu_hotplug_disable() and other CPUs are offlined. No need for @@ -5735,16 +5749,19 @@ static int kvm_suspend(void) */ lockdep_assert_not_held(&kvm_lock); =20 - if (kvm_usage_count) { - preempt_disable(); + preempt_disable(); + if (kvm_usage_count) hardware_disable_nolock(NULL); - preempt_enable(); - } - return 0; + ret =3D kvm_arch_suspend(kvm_usage_count); + preempt_enable(); + + return ret; } =20 static void kvm_resume(void) { + lockdep_assert_not_held(&kvm_lock); + if (kvm_arch_check_processor_compat()) /* * No warning here because kvm_arch_check_processor_compat() @@ -5752,12 +5769,11 @@ static void kvm_resume(void) */ return; /* FIXME: disable KVM */ =20 - if (kvm_usage_count) { - lockdep_assert_not_held(&kvm_lock); - preempt_disable(); + preempt_disable(); + if (kvm_usage_count) hardware_enable_nolock((void *)__func__); - preempt_enable(); - } + kvm_arch_resume(kvm_usage_count); + preempt_enable(); } =20 static struct syscore_ops kvm_syscore_ops =3D { --=20 2.25.1 From nobody Mon Apr 6 08:13:35 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 438F8C54EE9 for ; Thu, 8 Sep 2022 23:27:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229835AbiIHX1I (ORCPT ); Thu, 8 Sep 2022 19:27:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43956 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230130AbiIHX03 (ORCPT ); Thu, 8 Sep 2022 19:26:29 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7C233EB86B; Thu, 8 Sep 2022 16:26:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662679571; x=1694215571; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=eur7y1TP4jtJja2EWabGmkXbvuvmmXATztvuUODFrLI=; b=k2SMx2mHMicYKuB5K++kgYLoqIHX2PtE3wbizs1NyzwKNq6U9HyG46WU 5qwrugBIStCqYs//sQktCOUeAuVNu5NHbHFsKfouyfKOL2dSkwMj6TEWV 28wuZuysWyTw/+IRRqgUU/Ili9U8ivCgDsrS5eb+DEHkONPnZCntqfLFS 7W3TrVkPLaRL17ArP4CArvFZ48Gv7TH1HUlknntRMpkaq8HGdFVkKXWS/ tR0FDcwQMS3yy5hK1ccCXgdRR3trzLp1HpfBs/NPGgk3oFCJ9fEYfzDRN F6NKBfnrxIy3d2QhLz87uQnWTBYBlpfHBTvVFd0vGZca+9JdkTocabZpX Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298687004" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298687004" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:09 -0700 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="610863208" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:09 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier , Will Deacon , Yuan Yao Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen Subject: [PATCH v4 12/26] KVM: x86: Move TSC fixup logic to KVM arch resume callback Date: Thu, 8 Sep 2022 16:25:28 -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 | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 0ab8db07fa0e..c733f65aaf3c 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11835,18 +11835,27 @@ 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; =20 local_tsc =3D rdtsc(); stable =3D !kvm_check_tsc_unstable(); @@ -11921,13 +11930,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 Mon Apr 6 08:13:35 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 189B4C54EE9 for ; Thu, 8 Sep 2022 23:27:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230234AbiIHX1L (ORCPT ); Thu, 8 Sep 2022 19:27:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43958 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230132AbiIHX03 (ORCPT ); Thu, 8 Sep 2022 19:26:29 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4BA2AF1F15; Thu, 8 Sep 2022 16:26:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662679572; x=1694215572; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=zkkuRo7b1Y9KCKWZAKJpBJHGXbxql5InDVr+q8N93Wk=; b=ImxduLUmlX1+X6DMEu7wswtJHV66xbSvgcAcX9FwWAi8ekyt13eo8toR Pjj6XSNl69Xl+0rBmCSCMpjKLngN4U4UwcqWKfRCcdkq3Gqk+T8guZvMg vd1Yfq10fCrnC7Y0txDlRGpa87Vele+FIL0yH7GyIY9mt9P1Stco6YSnW 6V5P2QxAOFBG3uHChfTN43ZEWZ4xigtQEBXAVXi4fWlc6NbTixT6yTWh8 Wt223WilOP9T8pp36GEb/VbB+55r+liYH62hwhfiewl1Gew+KyOAOqFAJ QkvedRdwF2cnN/xFYE2zUOUyVGrgn1lWtJjvm/bnN647DRDhtsTo4nLTR g==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298687009" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298687009" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:10 -0700 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="610863217" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:09 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier , Will Deacon , Yuan Yao Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen Subject: [PATCH v4 13/26] KVM: Add arch hook when VM is added/deleted Date: Thu, 8 Sep 2022 16:25:29 -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 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 951f853f6ac9..7acc35e279ec 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); } @@ -5096,6 +5100,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 Mon Apr 6 08:13:35 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 42967C6FA82 for ; Thu, 8 Sep 2022 23:27:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230351AbiIHX1P (ORCPT ); Thu, 8 Sep 2022 19:27:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230143AbiIHX0a (ORCPT ); Thu, 8 Sep 2022 19:26:30 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 11F5BFF0A0; Thu, 8 Sep 2022 16:26:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662679573; x=1694215573; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=CB6pdMtWrveFUnO2jvTSztQL9r1rUdSNl46pxYfNIgQ=; b=MEE+M0ynZt+iqNyqnPFdAbNf0qTmTXJD0JFQj5cm9uzWwsbcy8iQ4McW v1ABDdmz1hDPakES/MHre4c+bGDNdXBV9pJbV2H41ZMhHMngFzxqE39EU o2/plgtHci9g6vmcD6H6A+uboaHORyU15K62l9GfBliEQqyRLcN/NSmex y0z7cGs/zIj6n1VDyN21jYYGkKpZFoc+RIyAzX7WqioYPioOYArOWxEYh kleyDIkCFLnB0S1k1SNMDleikzqUzuiMVP+0ZtNTPaWORJgXgXvz0TTmo zj+4D8IAP+DuCmuga8MAaW054rOHzezBdMu+nl3YXzWNPCWjFTDooVqR8 g==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298687012" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298687012" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:10 -0700 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="610863229" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:10 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier , Will Deacon , Yuan Yao Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen Subject: [PATCH v4 14/26] KVM: Move out KVM arch PM hooks and hardware enable/disable logic Date: Thu, 8 Sep 2022 16:25:30 -0700 Message-Id: <7face8cdec33c84f3aed7e8d400b5adf24fc5c75.1662679124.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. x86 kvm_arch_resume() needs to be adjusted to do CPU compatibility check and virtualization hardware enabling. Signed-off-by: Isaku Yamahata --- arch/x86/kvm/x86.c | 10 ++ include/linux/kvm_host.h | 1 + virt/kvm/kvm_arch.c | 124 +++++++++++++++++++++++- virt/kvm/kvm_main.c | 198 +++++---------------------------------- 4 files changed, 156 insertions(+), 177 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index c733f65aaf3c..23623b6a789b 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11854,9 +11854,19 @@ void kvm_arch_resume(int usage_count) u64 max_tsc =3D 0; bool stable, backwards_tsc =3D false; =20 + if (kvm_arch_check_processor_compat()) + return; /* FIXME: disable KVM. */ + if (!usage_count) return; =20 + preempt_disable(); + if (kvm_arch_hardware_enable()) { + preempt_enable(); + return; + } + preempt_enable(); + local_tsc =3D rdtsc(); stable =3D !kvm_check_tsc_unstable(); list_for_each_entry(kvm, &vm_list, vm_list) { 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..32befdbf7d6e 100644 --- a/virt/kvm/kvm_arch.c +++ b/virt/kvm/kvm_arch.c @@ -6,49 +6,169 @@ * 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 *caller_name) +{ + 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_warn("kvm: enabling virtualization on CPU%d failed during %s()\n", + cpu, (const char *)caller_name); + } +} + +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, (void *)__func__, 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; + + ret =3D kvm_arch_check_processor_compat(); + if (ret) + return ret; + + /* + * 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)); + + /* + * arch callback kvm_arch_hardware_eanble() assumes that + * preemption is disabled for historical reason. Disable + * preemption until all arch callbacks are fixed. + */ + preempt_disable(); + hardware_enable_nolock((void *)__func__); + preempt_enable(); + 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) { + /* + * arch callback kvm_arch_hardware_disable() assumes that + * preemption is disabled for historical reason. Disable + * preemption until all arch callbacks are fixed. + */ + preempt_disable(); + hardware_disable_nolock(NULL); + preempt_enable(); + } return 0; } =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) { + preempt_disable(); + hardware_disable_nolock(NULL); + preempt_enable(); + } return 0; } =20 __weak void kvm_arch_resume(int usage_count) { + if (kvm_arch_check_processor_compat()) + /* + * No warning here because kvm_arch_check_processor_compat() + * would have warned with more information. + */ + return; /* FIXME: disable KVM */ + + if (usage_count) { + preempt_disable(); + hardware_enable_nolock((void *)__func__); + preempt_enable(); + } } diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 7acc35e279ec..5373127dcdb6 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,28 @@ static struct kvm *kvm_create_vm(unsigned long type= , const char *fdname) if (r) goto out_err_no_debugfs; =20 + /* + * During onlining a CPU, cpu_online_mask is set before kvm_online_cpu() + * is called. on_each_cpu() between them includes the CPU. As a result, + * hardware_enable_nolock() may get invoked before kvm_online_cpu(). + * This would enable hardware virtualization on that cpu without + * compatibility checks, which can potentially crash system or break + * running VMs. + * + * Disable CPU hotplug to prevent this case from happening. + */ + cpus_read_lock(); 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 +1245,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 +1324,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,163 +4988,37 @@ static struct miscdevice kvm_dev =3D { &kvm_chardev_ops, }; =20 -static void hardware_enable_nolock(void *caller_name) -{ - 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_warn("kvm: enabling virtualization on CPU%d failed during %s()\n", - cpu, (const char *)caller_name); - } -} - static int kvm_online_cpu(unsigned int cpu) { int ret; =20 - ret =3D kvm_arch_check_processor_compat(); - if (ret) - return ret; - 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)); - - /* - * arch callback kvm_arch_hardware_eanble() assumes that - * preemption is disabled for historical reason. Disable - * preemption until all arch callbacks are fixed. - */ - preempt_disable(); - hardware_enable_nolock((void *)__func__); - preempt_enable(); - 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) { - /* - * arch callback kvm_arch_hardware_disable() assumes that - * preemption is disabled for historical reason. Disable - * preemption until all arch callbacks are fixed. - */ - preempt_disable(); - hardware_disable_nolock(NULL); - ret =3D kvm_arch_offline_cpu(cpu, kvm_usage_count); - if (ret) { - (void)hardware_enable_nolock(NULL); - atomic_set(&hardware_enable_failed, 0); - } - preempt_enable(); - } + 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; - - /* - * During onlining a CPU, cpu_online_mask is set before kvm_online_cpu() - * is called. on_each_cpu() between them includes the CPU. As a result, - * hardware_enable_nolock() may get invoked before kvm_online_cpu(). - * This would enable hardware virtualization on that cpu without - * compatibility checks, which can potentially crash system or break - * running VMs. - * - * Disable CPU hotplug to prevent this case from happening. - */ - cpus_read_lock(); - 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, (void *)__func__, 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) { @@ -5160,7 +5036,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(); @@ -5753,40 +5628,19 @@ static void kvm_init_debug(void) =20 static int kvm_suspend(void) { - int ret; - /* * The caller ensures that CPU hotlug is disabled by * cpu_hotplug_disable() and other CPUs are offlined. No need for * locking. */ lockdep_assert_not_held(&kvm_lock); - - preempt_disable(); - if (kvm_usage_count) - hardware_disable_nolock(NULL); - ret =3D kvm_arch_suspend(kvm_usage_count); - preempt_enable(); - - return ret; + return kvm_arch_suspend(kvm_usage_count); } =20 static void kvm_resume(void) { lockdep_assert_not_held(&kvm_lock); - - if (kvm_arch_check_processor_compat()) - /* - * No warning here because kvm_arch_check_processor_compat() - * would have warned with more information. - */ - return; /* FIXME: disable KVM */ - - preempt_disable(); - if (kvm_usage_count) - hardware_enable_nolock((void *)__func__); kvm_arch_resume(kvm_usage_count); - preempt_enable(); } =20 static struct syscore_ops kvm_syscore_ops =3D { @@ -5924,11 +5778,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; @@ -6005,8 +5854,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(); @@ -6028,11 +5875,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 Mon Apr 6 08:13:35 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 522B9C6FA82 for ; Thu, 8 Sep 2022 23:27:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230241AbiIHX1U (ORCPT ); Thu, 8 Sep 2022 19:27:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44482 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229869AbiIHX0a (ORCPT ); Thu, 8 Sep 2022 19:26:30 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F23B108700; Thu, 8 Sep 2022 16:26:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662679573; x=1694215573; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=SfxQxRIB04avwvu2iwgntHu0fjCQdA4+XwdzGmceViY=; b=keujRBCiVgyBYgCDYowImaC9YlsPnyLcqimz8uJ0RTJulNtAc/hpbNoo ghsrGgdYX4p9w81U9cmVOr/BMO3l3lZQwluW4EBnsqZ3pJP1BuM1lH2T0 nfaYzWfhu9vKIuLyEnuJ367kU20kKaWPuprJzryITivKfr2SVR68Ve8fu +6+pMrhTm+sOqInSEdamX+FFLkFNZj2SgZ9ke1aQNqqdxsNREw9Bt6Zgl 8zRK5Ad2FcoAywDjJzWx7DLtQWTwl94K/KcOClVWvX98gC0/MMZHmf4aG tF5m/zDSwSycCHHwIAgn1+oZ5ai0GRulApDHF/J9aGingEuO+9XHJHsXM A==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298687014" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298687014" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:11 -0700 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="610863237" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:10 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier , Will Deacon , Yuan Yao Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen Subject: [PATCH v4 15/26] KVM: kvm_arch.c: Remove _nolock post fix Date: Thu, 8 Sep 2022 16:25: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 all related callbacks are called under kvm_lock, no point for _nolock post fix. Remove _nolock post fix for readability with shorter function names. 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 32befdbf7d6e..4fe16e8ef2e5 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 *caller_name) +static void hardware_enable(void *caller_name) { int cpu =3D raw_smp_processor_id(); int r; @@ -42,7 +42,7 @@ static void hardware_enable_nolock(void *caller_name) } } =20 -static void hardware_disable_nolock(void *junk) +static void hardware_disable(void *junk) { int cpu =3D raw_smp_processor_id(); =20 @@ -56,7 +56,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 /* @@ -71,7 +71,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, (void *)__func__, 1); + on_each_cpu(hardware_enable, (void *)__func__, 1); =20 if (atomic_read(&hardware_enable_failed)) { r =3D -EBUSY; @@ -81,7 +81,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 @@ -90,7 +90,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 @@ -116,7 +116,7 @@ __weak int kvm_arch_online_cpu(unsigned int cpu, int us= age_count) * preemption until all arch callbacks are fixed. */ preempt_disable(); - hardware_enable_nolock((void *)__func__); + hardware_enable((void *)__func__); preempt_enable(); if (atomic_read(&hardware_enable_failed)) { atomic_set(&hardware_enable_failed, 0); @@ -135,7 +135,7 @@ __weak int kvm_arch_offline_cpu(unsigned int cpu, int u= sage_count) * preemption until all arch callbacks are fixed. */ preempt_disable(); - hardware_disable_nolock(NULL); + hardware_disable(NULL); preempt_enable(); } return 0; @@ -143,7 +143,7 @@ __weak int kvm_arch_offline_cpu(unsigned int cpu, int u= sage_count) =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 @@ -151,7 +151,7 @@ __weak int kvm_arch_suspend(int usage_count) { if (usage_count) { preempt_disable(); - hardware_disable_nolock(NULL); + hardware_disable(NULL); preempt_enable(); } return 0; @@ -168,7 +168,7 @@ __weak void kvm_arch_resume(int usage_count) =20 if (usage_count) { preempt_disable(); - hardware_enable_nolock((void *)__func__); + hardware_enable((void *)__func__); preempt_enable(); } } --=20 2.25.1 From nobody Mon Apr 6 08:13:35 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 A9C34C54EE9 for ; Thu, 8 Sep 2022 23:27:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230372AbiIHX1X (ORCPT ); Thu, 8 Sep 2022 19:27:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230152AbiIHX0b (ORCPT ); Thu, 8 Sep 2022 19:26:31 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61005108722; Thu, 8 Sep 2022 16:26:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662679574; x=1694215574; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=N9V8aeLBeDyUCKjwvv3vkqyPtF2+WfmFX4Ug1U3Osmg=; b=HnIznXfiQs5okBhy8EWZxhIb+1oQmqqDS495TPsYv1dlKAyUzZ0Bcedx sa7tU2esSaypJIZ3K9ZY5pK8Iii+2AL3EykpNKX5GqZ1k20TWCBDUYU4I FleJYFAawzAL3QgoXmTmx868x/To5VRV86HNRpXXIEtuHx/JXEeAfkd3t yV5056RRP9m08lJ0dMEBMxAsKwJjlhGNh84O+w59Fh+220buT1p96cwX7 4qDW/6FdJeDXyz25TZAuYkn/s0SFiriygVAZNh8bDI/zWJLY5P0DlpaVx Fg/l3pQQtwGH2fwg4EFNYsPwcueHD4EXH0tQ54uY93p06FmNmAvXsdtiH A==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298687017" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298687017" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:11 -0700 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="610863245" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:11 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier , Will Deacon , Yuan Yao Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen Subject: [PATCH v4 16/26] KVM: kvm_arch.c: Remove a global variable, hardware_enable_failed Date: Thu, 8 Sep 2022 16:25:32 -0700 Message-Id: <60ca73d7d931ec8046ac50b20f05723a97ef643b.1662679124.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 | 62 ++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c index 4fe16e8ef2e5..ad23537ebe3b 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 *caller_name) +static int __hardware_enable(void *caller_name) { int cpu =3D raw_smp_processor_id(); int r; @@ -28,18 +27,22 @@ static void hardware_enable(void *caller_name) 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_warn("kvm: enabling virtualization on CPU%d failed during %s()\n", cpu, (const char *)caller_name); - } + else + cpumask_set_cpu(cpu, &cpus_hardware_enabled); + return r; +} + +static void hardware_enable(void *arg) +{ + atomic_t *failed =3D arg; + + if (__hardware_enable((void *)__func__)) + atomic_inc(failed); } =20 static void hardware_disable(void *junk) @@ -65,15 +68,16 @@ __weak void kvm_arch_pre_hardware_unsetup(void) */ __weak int kvm_arch_add_vm(struct kvm *kvm, int usage_count) { + atomic_t failed; int r =3D 0; =20 if (usage_count !=3D 1) return 0; =20 - atomic_set(&hardware_enable_failed, 0); - on_each_cpu(hardware_enable, (void *)__func__, 1); + atomic_set(&failed, 0); + on_each_cpu(hardware_enable, &failed, 1); =20 - if (atomic_read(&hardware_enable_failed)) { + if (atomic_read(&failed)) { r =3D -EBUSY; goto err; } @@ -96,33 +100,29 @@ __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; + int ret; =20 ret =3D kvm_arch_check_processor_compat(); if (ret) return ret; =20 + if (!usage_count) + return 0; + + /* + * arch callback kvm_arch_hardware_eanble() assumes that + * preemption is disabled for historical reason. Disable + * preemption until all arch callbacks are fixed. + */ + preempt_disable(); /* * Abort the CPU online process if hardware virtualization cannot * be enabled. Otherwise running VMs would encounter unrecoverable * errors when scheduled to this CPU. */ - if (usage_count) { - WARN_ON_ONCE(atomic_read(&hardware_enable_failed)); + ret =3D __hardware_enable((void *)__func__); + preempt_enable(); =20 - /* - * arch callback kvm_arch_hardware_eanble() assumes that - * preemption is disabled for historical reason. Disable - * preemption until all arch callbacks are fixed. - */ - preempt_disable(); - hardware_enable((void *)__func__); - preempt_enable(); - if (atomic_read(&hardware_enable_failed)) { - atomic_set(&hardware_enable_failed, 0); - ret =3D -EIO; - } - } return ret; } =20 @@ -168,7 +168,7 @@ __weak void kvm_arch_resume(int usage_count) =20 if (usage_count) { preempt_disable(); - hardware_enable((void *)__func__); + (void)__hardware_enable((void *)__func__); preempt_enable(); } } --=20 2.25.1 From nobody Mon Apr 6 08:13:35 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 D6B97C6FA82 for ; Thu, 8 Sep 2022 23:27:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230415AbiIHX1j (ORCPT ); Thu, 8 Sep 2022 19:27:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44486 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230220AbiIHX0j (ORCPT ); Thu, 8 Sep 2022 19:26:39 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 080F51098C5; Thu, 8 Sep 2022 16:26:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662679576; x=1694215576; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=grUPy5igLiLP2txiwvePj0au0Jj6sSvLtTxS5GMrsGE=; b=GhB2VPTMStyBpiGTtoFnLx0sqFn0CYupQI3lo1AC0LD4Fu8KjMXoIaem NylB9i1xSUUej/8/k2h9pxBDtjOIr5XHKBFN+vjNrYknqSEn7hhnoWmOZ l0qFaea0JpCvnfYHJ7Gs3ihpmV5tMKZ4FAFFzQ62sAWxuYf18xwzw5Ovm /vtwxDGAVtBcbfdFDqUjToug3POGKpesi/Ec/YjnTWPEkQCTaaqbRsfEm H4YQh6NPG8DTLAqWInR6Hjj0s9f/00sFrJLtEm4CinaYKxiY/g87fhoDt Dcc+JGi93os5Jk2wqZdENOe23xWmDj0Rz3EtL3XrRa4Fux6ARK1vRgWcw g==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298687020" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298687020" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:12 -0700 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="610863250" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:12 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier , Will Deacon , Yuan Yao Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen Subject: [PATCH v4 17/26] KVM: Introduce a arch wrapper to check all processor compatibility Date: Thu, 8 Sep 2022 16:25: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 Introduce a arch wrapper to check all processor compatibility and define default implementation as weak symbol to keep the current logic. The hardware feature compatibility check is arch dependent, only x86 KVM does cpu feature check on all processors. It doesn't make much sense to enforce the current implementation to invoke check function on each processors. Introduce a arch callback, kvm_arch_check_processor_compat_all(), so that arch code can override it. Eventually feature check should be pushed down into arch callback, (kvm_arch_hardware_setup(), kvm_arch_online_cpu(), and kvm_arch_resume()), the two compatibility check, kvm_arch_check_processor_compat{,_all}(), will be eliminated. This is a transitional step for it. Signed-off-by: Isaku Yamahata --- include/linux/kvm_host.h | 1 + virt/kvm/kvm_arch.c | 18 ++++++++++++++++++ virt/kvm/kvm_main.c | 13 +++---------- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 60f4ae9d6f48..74cae99fbf09 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1440,6 +1440,7 @@ int kvm_arch_hardware_setup(void *opaque); void kvm_arch_pre_hardware_unsetup(void); void kvm_arch_hardware_unsetup(void); int kvm_arch_check_processor_compat(void); +int kvm_arch_check_processor_compat_all(void); int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu); bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu); int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu); diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c index ad23537ebe3b..9476c500d571 100644 --- a/virt/kvm/kvm_arch.c +++ b/virt/kvm/kvm_arch.c @@ -98,6 +98,24 @@ __weak int kvm_arch_del_vm(int usage_count) return 0; } =20 +static void check_processor_compat(void *rtn) +{ + *(int *)rtn =3D kvm_arch_check_processor_compat(); +} + +__weak int kvm_arch_check_processor_compat_all(void) +{ + int cpu; + int r; + + for_each_online_cpu(cpu) { + smp_call_function_single(cpu, check_processor_compat, &r, 1); + if (r < 0) + return r; + } + return 0; +} + __weak int kvm_arch_online_cpu(unsigned int cpu, int usage_count) { int ret; diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 5373127dcdb6..51315d454dc2 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -5752,11 +5752,6 @@ void kvm_unregister_perf_callbacks(void) } #endif =20 -static void check_processor_compat(void *rtn) -{ - *(int *)rtn =3D kvm_arch_check_processor_compat(); -} - int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, struct module *module) { @@ -5782,11 +5777,9 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsig= ned vcpu_align, if (r < 0) goto out_free_1; =20 - for_each_online_cpu(cpu) { - smp_call_function_single(cpu, check_processor_compat, &r, 1); - if (r < 0) - goto out_free_2; - } + r =3D kvm_arch_check_processor_compat_all(); + if (r < 0) + goto out_free_2; =20 r =3D cpuhp_setup_state_nocalls(CPUHP_AP_KVM_ONLINE, "kvm/cpu:online", kvm_online_cpu, kvm_offline_cpu); --=20 2.25.1 From nobody Mon Apr 6 08:13:35 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 230ACC54EE9 for ; Thu, 8 Sep 2022 23:27:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230394AbiIHX11 (ORCPT ); Thu, 8 Sep 2022 19:27:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43956 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230179AbiIHX0g (ORCPT ); Thu, 8 Sep 2022 19:26:36 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 928211098E1; Thu, 8 Sep 2022 16:26:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662679576; x=1694215576; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=BCW0b+RqTHG/HSgdegqcNoUHAj5WKFiSkIrcMdx/w4M=; b=AqVvACoAXGyMS6XxsSjLWnIVwdrcBw/iKxj7K92fxGhSEJHI8a6/kAFt qmXCkQSGkX+k0c20Zam8aqTG/mqThtxgDAafXuZIQqBKmknTtgSQ+LNKQ ELQ3Ku9hQCeAp4LpGu8ZSe9pGezWm7gnvRsl+KT8A79iBCNPnYkhiWiUQ UYYLin+R9eqTQkZuDiYMc1Ns9BkWJkm/tpi9T7n9DozS1lpW/oDV+TKIf YP2r/jO266ZJtCqTqCDIyVw0Y9oPnP9NsCAqdDAlEJgU68lYLFhBlw5gW UxEJAr66VWoK0Jb9MqfkBOIAbHXz35Qm+f2a8lx9BkkUEiPZI+EYab1D5 w==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298687023" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298687023" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:13 -0700 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="610863256" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:12 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier , Will Deacon , Yuan Yao Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen Subject: [PATCH v4 18/26] KVM: x86: Duplicate arch callbacks related to pm events and compat check Date: Thu, 8 Sep 2022 16:25:34 -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 can change those callbacks without worrying about breaking other archs. Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- arch/x86/kvm/x86.c | 168 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 163 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 23623b6a789b..feee7739219e 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11845,6 +11845,169 @@ 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 *caller_name) +{ + 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_warn("kvm: enabling virtualization on CPU%d failed during %s()\n", + cpu, (const char *)caller_name); + else + cpumask_set_cpu(cpu, &cpus_hardware_enabled); + return r; +} + +static void hardware_enable(void *arg) +{ + atomic_t *failed =3D arg; + + if (__hardware_enable((void *)__func__)) + 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; +} + +static void check_processor_compat(void *rtn) +{ + *(int *)rtn =3D kvm_arch_check_processor_compat(); +} + +int kvm_arch_check_processor_compat_all(void) +{ + int cpu; + int r; + + for_each_online_cpu(cpu) { + smp_call_function_single(cpu, check_processor_compat, &r, 1); + if (r < 0) + return r; + } + return 0; +} + +int kvm_arch_online_cpu(unsigned int cpu, int usage_count) +{ + int ret; + + ret =3D kvm_arch_check_processor_compat(); + if (ret) + return ret; + + if (!usage_count) + return 0; + + /* + * arch callback kvm_arch_hardware_eanble() assumes that + * preemption is disabled for historical reason. Disable + * preemption until all arch callbacks are fixed. + */ + preempt_disable(); + /* + * Abort the CPU online process if hardware virtualization cannot + * be enabled. Otherwise running VMs would encounter unrecoverable + * errors when scheduled to this CPU. + */ + ret =3D __hardware_enable((void *)__func__); + preempt_enable(); + + return ret; +} + +int kvm_arch_offline_cpu(unsigned int cpu, int usage_count) +{ + if (usage_count) { + /* + * arch callback kvm_arch_hardware_disable() assumes that + * preemption is disabled for historical reason. Disable + * preemption until all arch callbacks are fixed. + */ + preempt_disable(); + hardware_disable(NULL); + preempt_enable(); + } + return 0; +} + +int kvm_arch_reboot(int val) +{ + on_each_cpu(hardware_disable, NULL, 1); + return NOTIFY_OK; +} + +int kvm_arch_suspend(int usage_count) +{ + if (usage_count) { + preempt_disable(); + hardware_disable(NULL); + preempt_enable(); + } + return 0; +} + void kvm_arch_resume(int usage_count) { struct kvm *kvm; @@ -12122,11 +12285,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned 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 Mon Apr 6 08:13:35 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 72B03C54EE9 for ; Thu, 8 Sep 2022 23:27:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230309AbiIHX1l (ORCPT ); Thu, 8 Sep 2022 19:27:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44978 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230159AbiIHX0n (ORCPT ); Thu, 8 Sep 2022 19:26:43 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0DB1FE7F8B; Thu, 8 Sep 2022 16:26:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662679577; x=1694215577; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=UEVrSJBp6rdD40rMz/X3mQD/s7hSMW7ttIkeZZoCFOA=; b=kNCTQVuMEWnu1eX4oy/GT+z8B5aIo/5N3d5B8C2SVj2Ase7uLl/ElYAE pQt9Yu97hSqhJvqvNbw9GfPuvgkcrLjChqCKE+hZIYsaCmwQWjKW1EzLR XHe3LlKEGn02tVn3Ts82YxhRDmu7KTGye8aET/YnBHKTyi4jAaxrInuJX n3WDzB4WXR0AHVLbR7tS5+7Z+3IT0BUBgdJcp9N7viuLnaQDceSmRMGGe n7N0+dW3uJGpfrIBcGVR93nmJwWE+CDABbFiiZRazaPTeyW0NQOT7oF1A X6wrLjn416OZ3E5akbnFJJt4JBFPPDRHsdBvrQY0TY6DmEi9EG5dqRCJg Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298687027" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298687027" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:13 -0700 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="610863260" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:13 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier , Will Deacon , Yuan Yao Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen Subject: [PATCH v4 19/26] KVM: Eliminate kvm_arch_post_init_vm() Date: Thu, 8 Sep 2022 16:25:35 -0700 Message-Id: <7bbbbda4f6eacf76a3c664613d5db0558e1f94c9.1662679124.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 kvm_arch_post_init_vm() is used only by x86 kvm_arch_add_vm(). Other arch doesn't define it. Merge x86 kvm_arch_post_init_vm() into x86 kvm_arch_add_vm() and eliminate kvm_arch_post_init_vm(). 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 feee7739219e..6dcc6ed90421 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11847,11 +11847,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 *caller_name) { int cpu =3D raw_smp_processor_id(); @@ -11915,7 +11910,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 74cae99fbf09..d0b5fdb084f4 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1446,7 +1446,6 @@ bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu); int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu); bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu); bool kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu); -int kvm_arch_post_init_vm(struct kvm *kvm); int kvm_arch_add_vm(struct kvm *kvm, int usage_count); int kvm_arch_del_vm(int usage_count); void kvm_arch_pre_destroy_vm(struct kvm *kvm); diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c index 9476c500d571..9bf7b3920c44 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 *caller_name) { int cpu =3D raw_smp_processor_id(); @@ -79,13 +74,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 Mon Apr 6 08:13:35 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 99F69C54EE9 for ; Thu, 8 Sep 2022 23:27:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230057AbiIHX1b (ORCPT ); Thu, 8 Sep 2022 19:27:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44444 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230199AbiIHX0h (ORCPT ); Thu, 8 Sep 2022 19:26:37 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 14B9E10E851; Thu, 8 Sep 2022 16:26:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662679579; x=1694215579; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=5pQ3z02clD0r79Hqc0yHbGrlWpRrN1AcMCo979U69FA=; b=KEuQHnJFa03XK7DzjYwCUHJTlpnuFnx+gmJgjEUkANnBtmxVVs7qCd98 s2TacyLvfRtt3zIl9ViNfr4w7YRU4Hqo2u22rz154NL6jz/wrtneQ1+xs bjFX69wUR5nv8fzoSXJasK5ha5KyZtq2rbZTnM0gkVnnTsA2ar0s88Tfa gtBRFCihuKW1IfW0XgTZ87WUBqDaF71bArcBspQ8NWPW6OgY8bYm7wwgZ o2PHKELLvwd1DcmAcI6aUVbWHP6OCP3V384uRef0lC0/f7K83Wbp9EhDE G3FO/h+1hLjrtEuI9Tdcqtyt78T3u/UHnoxBNUDNXaMLSX18HE5X+e0dL A==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298687030" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298687030" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:14 -0700 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="610863266" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:13 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier , Will Deacon , Yuan Yao Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen Subject: [PATCH v4 20/26] KVM: Add config to not compile kvm_arch.c Date: Thu, 8 Sep 2022 16:25:36 -0700 Message-Id: <67780bc1160e6a6ff27a7d9c9374f19fc4b57a5d.1662679124.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 that kvm_arch_hardware_enable/disable() aren't required. Once the conversion of all KVM archs is done, this config and kvm_arch.c should be removed. Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- arch/x86/kvm/Kconfig | 1 + include/linux/kvm_host.h | 3 +++ virt/kvm/Kconfig | 3 +++ virt/kvm/Makefile.kvm | 5 ++++- 4 files changed, 11 insertions(+), 1 deletion(-) 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 d0b5fdb084f4..f538fc3356a9 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1434,8 +1434,11 @@ 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 Mon Apr 6 08:13:35 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 4C330C6FA83 for ; Thu, 8 Sep 2022 23:28:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230446AbiIHX2L (ORCPT ); Thu, 8 Sep 2022 19:28:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45762 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230332AbiIHX1C (ORCPT ); Thu, 8 Sep 2022 19:27:02 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 53723D5733; Thu, 8 Sep 2022 16:26:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662679581; x=1694215581; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=PXL7ArSkr52FAXL2U6zZPaAJzjUWlibVnvn3LNa2quU=; b=XNYjFe6YdTqOvD3ibWqpzWaDPKGHq1kBZgxYiQNvH5U7PCZ9+jB9LNm1 pjagHijfPBu/RDQBoNE2pjd7Qru84TqE1GSch8XZZ2/V7zjtariD9/AIq 0WXrbwRx8hjuJ11Ti6dhIUHwBbpI5YGnQOotedpHijuFoVxgVIsd7+dEq ZvZVDSqRe0pNyjT1GntobxitOhjQjd/WwqftDKjh3ZwBAk4pAOhPK1TLP vi+eHS9uE8QcArRaHVke8UMDzP+CWTiefhMEp7n+K4VNA0Z//kP7ZYtVQ zzXDQ4+w4aMk5f/+01NProzBNaFBsgcy+22v3oNk7H3q6RByfSy+jzrki Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298687034" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298687034" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:14 -0700 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="610863271" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:14 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier , Will Deacon , Yuan Yao Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen Subject: [PATCH v4 21/26] KVM: x86: Delete kvm_arch_hardware_enable/disable() Date: Thu, 8 Sep 2022 16:25:37 -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 | 27 +++++++-------------------- include/linux/kvm_host.h | 6 ++++-- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 6dcc6ed90421..0c9d965859c6 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) { @@ -11834,17 +11834,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 *caller_name) @@ -11856,7 +11845,7 @@ static int __hardware_enable(void *caller_name) =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_warn("kvm: enabling virtualization on CPU%d failed during %s()\n", cpu, (const char *)caller_name); @@ -11882,12 +11871,8 @@ static void hardware_disable(void *junk) if (!cpumask_test_cpu(cpu, &cpus_hardware_enabled)) return; cpumask_clear_cpu(cpu, &cpus_hardware_enabled); - kvm_arch_hardware_disable(); -} - -void kvm_arch_pre_hardware_unsetup(void) -{ - on_each_cpu(hardware_disable, NULL, 1); + static_call(kvm_x86_hardware_disable)(); + drop_user_return_notifiers(); } =20 /* @@ -12019,7 +12004,7 @@ void kvm_arch_resume(int usage_count) return; =20 preempt_disable(); - if (kvm_arch_hardware_enable()) { + if (static_call(kvm_x86_hardware_enable)()) { preempt_enable(); return; } @@ -12163,6 +12148,8 @@ int kvm_arch_hardware_setup(void *opaque) =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)(); diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index f538fc3356a9..5f4d6f641b03 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1434,13 +1434,15 @@ void kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *= vcpu, struct dentry *debugfs_ static inline void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu) {} #endif =20 -#ifndef CONFIG_HAVE_KVM_OVERRIDE_HARDWARE_ENABLE +#ifdef CONFIG_HAVE_KVM_OVERRIDE_HARDWARE_ENABLE +static inline void kvm_arch_pre_hardware_unsetup(void) {} +#else +void kvm_arch_pre_hardware_unsetup(void); int kvm_arch_hardware_enable(void); void kvm_arch_hardware_disable(void); #endif =20 int kvm_arch_hardware_setup(void *opaque); -void kvm_arch_pre_hardware_unsetup(void); void kvm_arch_hardware_unsetup(void); int kvm_arch_check_processor_compat(void); int kvm_arch_check_processor_compat_all(void); --=20 2.25.1 From nobody Mon Apr 6 08:13:35 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 14308C54EE9 for ; Thu, 8 Sep 2022 23:27:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230434AbiIHX1u (ORCPT ); Thu, 8 Sep 2022 19:27:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45688 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230204AbiIHX1A (ORCPT ); Thu, 8 Sep 2022 19:27:00 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6EC7F115CC3; Thu, 8 Sep 2022 16:26:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662679583; x=1694215583; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=IDO9vgRZGN9o4PRsD59O3z85CBSphz3E60aJtgsIp8c=; b=Jl1KRZ6aMhsDufaPPdWvxK5t+jPr/n4+ZFia8nNyQXja0JrDzTUjmc2l BWSQo9uZBjn0WHga3zwKAlnBIiGEz99EBb4cga6XZk1PGBMkvAL2VALsr kMl0AoosWcMmNkwq3YQxQnKqLxrlMyj0KG0Y4+fl3oJtJKU2PQx04033s DzXeuT4CQZRKhqNJlqSmB2Lp4y6C1otgYT4pglRBhpm9+tBK5h4DvyuJ3 qB32N5Io4+y+yyJD853oNmhK9zr7RGzRLpQ6llzA0x5P8PkY38qpuvHJZ Ak4YbXl2BD7KqGm1y2MVfZyskXCPBClVRQ99e9YnZ3PBQe1Zkl217BjsM Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298687039" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298687039" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:15 -0700 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="610863274" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:15 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier , Will Deacon , Yuan Yao Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen Subject: [PATCH v4 22/26] KVM: x86: Make x86 processor compat check callback empty Date: Thu, 8 Sep 2022 16:25:38 -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 Move processor compatibility check on all processors into kvm_arch_hardware_setup() and make kvm_arch_check_processor_compat{,_all}() empty. This is a preparation step to eliminate them. Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- arch/x86/kvm/x86.c | 47 +++++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 0c9d965859c6..9dd90f0521c3 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -129,6 +129,8 @@ static int kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu= ); static int __set_sregs2(struct kvm_vcpu *vcpu, struct kvm_sregs2 *sregs2); static void __get_sregs2(struct kvm_vcpu *vcpu, struct kvm_sregs2 *sregs2); =20 +static int kvm_check_processor_compatibility(void); + struct kvm_x86_ops kvm_x86_ops __read_mostly; =20 #define KVM_X86_OP(func) \ @@ -11911,21 +11913,8 @@ int kvm_arch_del_vm(int usage_count) return 0; } =20 -static void check_processor_compat(void *rtn) -{ - *(int *)rtn =3D kvm_arch_check_processor_compat(); -} - int kvm_arch_check_processor_compat_all(void) { - int cpu; - int r; - - for_each_online_cpu(cpu) { - smp_call_function_single(cpu, check_processor_compat, &r, 1); - if (r < 0) - return r; - } return 0; } =20 @@ -11933,7 +11922,7 @@ int kvm_arch_online_cpu(unsigned int cpu, int usage= _count) { int ret; =20 - ret =3D kvm_arch_check_processor_compat(); + ret =3D kvm_check_processor_compatibility(); if (ret) return ret; =20 @@ -11997,7 +11986,7 @@ void kvm_arch_resume(int usage_count) u64 max_tsc =3D 0; bool stable, backwards_tsc =3D false; =20 - if (kvm_arch_check_processor_compat()) + if (kvm_check_processor_compatibility()) return; /* FIXME: disable KVM. */ =20 if (!usage_count) @@ -12103,6 +12092,24 @@ static inline void kvm_ops_update(struct kvm_x86_i= nit_ops *ops) kvm_pmu_ops_update(ops->pmu_ops); } =20 +static void check_processor_compat(void *rtn) +{ + *(int *)rtn =3D kvm_check_processor_compatibility(); +} + +static int kvm_check_processor_compatibility_all(void) +{ + int cpu; + int r; + + for_each_online_cpu(cpu) { + smp_call_function_single(cpu, check_processor_compat, &r, 1); + if (r < 0) + return r; + } + return 0; +} + int kvm_arch_hardware_setup(void *opaque) { struct kvm_x86_init_ops *ops =3D opaque; @@ -12143,7 +12150,8 @@ int kvm_arch_hardware_setup(void *opaque) } kvm_caps.default_tsc_scaling_ratio =3D 1ULL << kvm_caps.tsc_scaling_ratio= _frac_bits; kvm_init_msr_list(); - return 0; + + return kvm_check_processor_compatibility_all(); } =20 void kvm_arch_hardware_unsetup(void) @@ -12155,7 +12163,7 @@ void kvm_arch_hardware_unsetup(void) static_call(kvm_x86_hardware_unsetup)(); } =20 -int kvm_arch_check_processor_compat(void) +static int kvm_check_processor_compatibility(void) { int cpu =3D smp_processor_id(); struct cpuinfo_x86 *c =3D &cpu_data(cpu); @@ -12175,6 +12183,11 @@ int kvm_arch_check_processor_compat(void) return static_call(kvm_x86_check_processor_compatibility)(); } =20 +int kvm_arch_check_processor_compat(void) +{ + return 0; +} + bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu) { return vcpu->kvm->arch.bsp_vcpu_id =3D=3D vcpu->vcpu_id; --=20 2.25.1 From nobody Mon Apr 6 08:13:35 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 6BB9FC6FA82 for ; Thu, 8 Sep 2022 23:28:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230461AbiIHX2P (ORCPT ); Thu, 8 Sep 2022 19:28:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44428 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230147AbiIHX1C (ORCPT ); Thu, 8 Sep 2022 19:27:02 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 054B7115CF2; Thu, 8 Sep 2022 16:26:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662679586; x=1694215586; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=jWuQBoK0q0gfpKRVUiGWH6TTCqtzESEI7BQKtOCQK/A=; b=VdH9HOw+wpKg/YHb1ccH+wr82NYVKGrSxAoymdPA+k69luFufPjukpal BcFl6jIn0onEea7Ji0wkniJvTCpn+nRhj/QAC1D+9DNywbyM3qTAP0Dv5 aV76/h4vr+rekk3fiOPDMh64Jq4dJOGY5KyB3InA5jHT1VrzvLCro57x7 ZWxw8/2pkxy1u4LjP+rDW2gdidtBirfU+OdFV3VbAZDtFeeNTbm9gQyEm Q6mYUal53/iiXXZs8Q0h3mK4vbWKGk2MClnUyvGdZDclpF95z7DlTtiQv Bk03Hnd+YXrhsfSv0f98lscqECeluYnBtMzU+71takedPf1x/+NtP1k1z A==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298687045" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298687045" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:16 -0700 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="610863281" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:15 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier , Will Deacon , Yuan Yao Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen , linuxppc-dev@lists.ozlabs.org, Fabiano Rosas Subject: [PATCH v4 23/26] RFC: KVM: powerpc: Move processor compatibility check to hardware setup Date: Thu, 8 Sep 2022 16:25:39 -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 Move processor compatibility check from kvm_arch_processor_compat() into kvm_arch_hardware_setup(). The check does model name comparison with a global variable, cur_cpu_spec. There is no point to check it at run time on all processors. Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata Cc: linuxppc-dev@lists.ozlabs.org Cc: Fabiano Rosas --- arch/powerpc/kvm/powerpc.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/arch/powerpc/kvm/powerpc.c b/arch/powerpc/kvm/powerpc.c index 7b56d6ccfdfb..7e3a6659f107 100644 --- a/arch/powerpc/kvm/powerpc.c +++ b/arch/powerpc/kvm/powerpc.c @@ -444,12 +444,21 @@ int kvm_arch_hardware_enable(void) =20 int kvm_arch_hardware_setup(void *opaque) { - return 0; + /* + * kvmppc_core_check_processor_compat() checks the global variable. + * No point to check on all processors or at runtime. + * arch/powerpc/kvm/book3s.c: return 0 + * arch/powerpc/kvm/e500.c: strcmp(cur_cpu_spec->cpu_name, "e500v2") + * arch/powerpc/kvm/e500mc.c: strcmp(cur_cpu_spec->cpu_name, "e500mc") + * strcmp(cur_cpu_spec->cpu_name, "e5500") + * strcmp(cur_cpu_spec->cpu_name, "e6500") + */ + return kvmppc_core_check_processor_compat(); } =20 int kvm_arch_check_processor_compat(void) { - return kvmppc_core_check_processor_compat(); + return 0; } =20 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) --=20 2.25.1 From nobody Mon Apr 6 08:13:35 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 56B34C6FA83 for ; Thu, 8 Sep 2022 23:28:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230478AbiIHX23 (ORCPT ); Thu, 8 Sep 2022 19:28:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46686 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230405AbiIHX12 (ORCPT ); Thu, 8 Sep 2022 19:27:28 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8793EE7FBF; Thu, 8 Sep 2022 16:26:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662679593; x=1694215593; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=fDmooBBcQHAZpR/dmiB8srIZ93JcteMYX0hXY6qhrTQ=; b=AH3WNSbLpImV4jEyIAAvTiRYGQAvNMb4MWZSUvJD2oq2WyUxdg2Lr01o IuQPfw4SfADy2WUrCo4rnW/HphNLm38aRXNVyRy0p3PJ5Ai7OwYHTkNnX jlPPDAZZP9zK1xXUaBcV894DhODvwRdqPiibTEqsfOokJelVCTaeW7y7M kh9v+W3sGtI9yH9vj/GanzeG79ECwa/9mTBhSek416J6Pfmx0oqh5IU7I ngzi13zCmjakmoOqO3JV2zujjsAL0WIfEVJv5prTje0Dn0XQPBeu0OvJp SbBSv2YTuWDvmkG14/xNpSptxGopaJbhTXjUPS9Q7uvVWHeUWS7IJE28J w==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298687050" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298687050" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:16 -0700 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="610863285" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:16 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier , Will Deacon , Yuan Yao Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen Subject: [PATCH v4 24/26] KVM: Eliminate kvm_arch_check_processor_compat() Date: Thu, 8 Sep 2022 16:25:40 -0700 Message-Id: <4d73ea052cfed118d1766cf039c2f1d27c6c7833.1662679124.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 arch has "return 0" implementation. Eliminate it. If feature compatibility check is needed, it should be done in kvm_arch_hardware_setup(), kvm_arch_online_cpu(), and kvm_arch_resume(). Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- arch/arm64/kvm/arm.c | 5 ----- arch/mips/kvm/mips.c | 5 ----- arch/powerpc/kvm/powerpc.c | 5 ----- arch/riscv/kvm/main.c | 5 ----- arch/s390/kvm/kvm-s390.c | 5 ----- arch/x86/kvm/x86.c | 10 ---------- include/linux/kvm_host.h | 2 -- virt/kvm/kvm_arch.c | 29 ----------------------------- virt/kvm/kvm_main.c | 4 ---- 9 files changed, 70 deletions(-) diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 7e83498b83aa..de0397bd7b18 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -68,11 +68,6 @@ int kvm_arch_hardware_setup(void *opaque) return 0; } =20 -int kvm_arch_check_processor_compat(void) -{ - return 0; -} - int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) { diff --git a/arch/mips/kvm/mips.c b/arch/mips/kvm/mips.c index 092d09fb6a7e..f4feae89075c 100644 --- a/arch/mips/kvm/mips.c +++ b/arch/mips/kvm/mips.c @@ -140,11 +140,6 @@ int kvm_arch_hardware_setup(void *opaque) return 0; } =20 -int kvm_arch_check_processor_compat(void) -{ - return 0; -} - extern void kvm_init_loongson_ipi(struct kvm *kvm); =20 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) diff --git a/arch/powerpc/kvm/powerpc.c b/arch/powerpc/kvm/powerpc.c index 7e3a6659f107..d840f6d498eb 100644 --- a/arch/powerpc/kvm/powerpc.c +++ b/arch/powerpc/kvm/powerpc.c @@ -456,11 +456,6 @@ int kvm_arch_hardware_setup(void *opaque) return kvmppc_core_check_processor_compat(); } =20 -int kvm_arch_check_processor_compat(void) -{ - return 0; -} - int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) { struct kvmppc_ops *kvm_ops =3D NULL; diff --git a/arch/riscv/kvm/main.c b/arch/riscv/kvm/main.c index f8d6372d208f..ebabcb3dfb8c 100644 --- a/arch/riscv/kvm/main.c +++ b/arch/riscv/kvm/main.c @@ -20,11 +20,6 @@ long kvm_arch_dev_ioctl(struct file *filp, return -EINVAL; } =20 -int kvm_arch_check_processor_compat(void) -{ - return 0; -} - int kvm_arch_hardware_setup(void *opaque) { return 0; diff --git a/arch/s390/kvm/kvm-s390.c b/arch/s390/kvm/kvm-s390.c index e26d4dd85668..9c5d3e4b464f 100644 --- a/arch/s390/kvm/kvm-s390.c +++ b/arch/s390/kvm/kvm-s390.c @@ -254,11 +254,6 @@ int kvm_arch_hardware_enable(void) return 0; } =20 -int kvm_arch_check_processor_compat(void) -{ - return 0; -} - /* forward declarations */ static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start, unsigned long end); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 9dd90f0521c3..84cc459575e2 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11913,11 +11913,6 @@ int kvm_arch_del_vm(int usage_count) return 0; } =20 -int kvm_arch_check_processor_compat_all(void) -{ - return 0; -} - int kvm_arch_online_cpu(unsigned int cpu, int usage_count) { int ret; @@ -12183,11 +12178,6 @@ static int kvm_check_processor_compatibility(void) return static_call(kvm_x86_check_processor_compatibility)(); } =20 -int kvm_arch_check_processor_compat(void) -{ - return 0; -} - bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu) { return vcpu->kvm->arch.bsp_vcpu_id =3D=3D vcpu->vcpu_id; diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 5f4d6f641b03..2cd835c8bc1b 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1444,8 +1444,6 @@ void kvm_arch_hardware_disable(void); =20 int kvm_arch_hardware_setup(void *opaque); void kvm_arch_hardware_unsetup(void); -int kvm_arch_check_processor_compat(void); -int kvm_arch_check_processor_compat_all(void); int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu); bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu); int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu); diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c index 9bf7b3920c44..68fb679d71f2 100644 --- a/virt/kvm/kvm_arch.c +++ b/virt/kvm/kvm_arch.c @@ -88,32 +88,10 @@ __weak int kvm_arch_del_vm(int usage_count) return 0; } =20 -static void check_processor_compat(void *rtn) -{ - *(int *)rtn =3D kvm_arch_check_processor_compat(); -} - -__weak int kvm_arch_check_processor_compat_all(void) -{ - int cpu; - int r; - - for_each_online_cpu(cpu) { - smp_call_function_single(cpu, check_processor_compat, &r, 1); - if (r < 0) - return r; - } - return 0; -} - __weak int kvm_arch_online_cpu(unsigned int cpu, int usage_count) { int ret; =20 - ret =3D kvm_arch_check_processor_compat(); - if (ret) - return ret; - if (!usage_count) return 0; =20 @@ -167,13 +145,6 @@ __weak int kvm_arch_suspend(int usage_count) =20 __weak void kvm_arch_resume(int usage_count) { - if (kvm_arch_check_processor_compat()) - /* - * No warning here because kvm_arch_check_processor_compat() - * would have warned with more information. - */ - return; /* FIXME: disable KVM */ - if (usage_count) { preempt_disable(); (void)__hardware_enable((void *)__func__); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 51315d454dc2..3f82162c8441 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -5777,10 +5777,6 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsig= ned vcpu_align, if (r < 0) goto out_free_1; =20 - r =3D kvm_arch_check_processor_compat_all(); - if (r < 0) - goto out_free_2; - r =3D cpuhp_setup_state_nocalls(CPUHP_AP_KVM_ONLINE, "kvm/cpu:online", kvm_online_cpu, kvm_offline_cpu); if (r) --=20 2.25.1 From nobody Mon Apr 6 08:13:36 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 0101EC6FA82 for ; Thu, 8 Sep 2022 23:28:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230249AbiIHX2U (ORCPT ); Thu, 8 Sep 2022 19:28:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230377AbiIHX1Y (ORCPT ); Thu, 8 Sep 2022 19:27:24 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44DB412BFAF; Thu, 8 Sep 2022 16:26:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662679596; x=1694215596; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Dn+T+C8mc7SutVui360EYL0Vvmak+kXGxLlZyx8Iru0=; b=WqsjdebmBxCXWFyitMAMOY6WwoEbsos3lmMo3w8VzkcIVAy3Q+I8dT4u 0bbTH2HfihFuftuRanfqUFTB+maceMMn5mVbb3u3xGQU08TzgKJZtBXXV eD3HI33FptvMKpM0fI96un53jLCIlfMh38YmwWc2s+fjeywT67qmNgSQN ZjeMsOL3sclgBkQeP+32M6jDFm95OK/gu8Z4f3mzkDiumcCHq0iyFfMnq m9oSoZth9/ohtnF9OpFuSvj5MGJ2KlhQ59GZYdr/bBP90IUkwJ8MB9DHW HQSDfQtFor+muTruF0mDpOHHH4a5hVwZS+BbMmO6iTg07I2GdGATEWGag A==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298687056" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298687056" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:17 -0700 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="610863292" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:16 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier , Will Deacon , Yuan Yao Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen Subject: [PATCH v4 25/26] RFC: KVM: x86: Remove cpus_hardware_enabled and related sanity check Date: Thu, 8 Sep 2022 16:25:41 -0700 Message-Id: <4da304db9dfa544564bbcb89957914a0f3dc136b.1662679124.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. Suggested-by: Sean Christopherson 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 84cc459575e2..623a5993d608 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11836,23 +11836,16 @@ 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 *caller_name) { - 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_warn("kvm: enabling virtualization on CPU%d failed during %s()\n", - cpu, (const char *)caller_name); - else - cpumask_set_cpu(cpu, &cpus_hardware_enabled); + smp_processor_id(), (const char *)caller_name); return r; } =20 @@ -11866,13 +11859,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 Mon Apr 6 08:13:36 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 826EBC6FA82 for ; Thu, 8 Sep 2022 23:28:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230422AbiIHX2f (ORCPT ); Thu, 8 Sep 2022 19:28:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44744 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230262AbiIHX1a (ORCPT ); Thu, 8 Sep 2022 19:27:30 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 66D0512BFB6; Thu, 8 Sep 2022 16:26:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662679596; x=1694215596; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=uTlu+lHOAwTFFFIR0Z5wSVQO4elx9U+QQnE27PbJEgI=; b=fmDXdacA3LOgbxkfXNzvL34Zi04ynPF3nvJlalXApcstUup+IhxArG2L 6ll1bf/hFXo2MDc3GXMhrIJ/lE4zhU+BQPBSTEPIJK6b0AZgwCWehYgdx JwFolvDcRv3WHoeXBBkTVwGXEXaNL24P7oUPSMhA777szxL4zx8QBb81w 6XCKTL8GLLVcdSq4Rw6AaKNu5XTN+b2jYe1J9MO2cJJ97SFUswTOH436R t0Q/tJI3TQJDR8pPG92zmr0y/a+c0FxDPp3snLhEvBgNngkpA2Zw1XIRr mpqxeGAGJ++uM4i0ZmRnwCj9gc1dzUAC57LhlfHudi9PN/TpIRMbVtcWT g==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298687060" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298687060" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:17 -0700 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="610863298" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:17 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier , Will Deacon , Yuan Yao Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen Subject: [PATCH v4 26/26] RFC: KVM: Remove cpus_hardware_enabled and related sanity check Date: Thu, 8 Sep 2022 16:25:42 -0700 Message-Id: <76ddec4c5fa3d84d422d3b90739c6a09d13d4bbc.1662679124.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. Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- virt/kvm/kvm_arch.c | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c index 68fb679d71f2..383b0065ecb0 100644 --- a/virt/kvm/kvm_arch.c +++ b/virt/kvm/kvm_arch.c @@ -12,23 +12,16 @@ =20 #include =20 -static cpumask_t cpus_hardware_enabled =3D CPU_MASK_NONE; - static int __hardware_enable(void *caller_name) { - 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_warn("kvm: enabling virtualization on CPU%d failed during %s()\n", - cpu, (const char *)caller_name); - else - cpumask_set_cpu(cpu, &cpus_hardware_enabled); + smp_processor_id(), (const char *)caller_name); return r; } =20 @@ -42,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