From patchwork Thu Sep 22 18:20:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985644 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 6AE81C6FA82 for ; Thu, 22 Sep 2022 18:21:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231347AbiIVSVY (ORCPT ); Thu, 22 Sep 2022 14:21:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34462 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230196AbiIVSVW (ORCPT ); Thu, 22 Sep 2022 14:21:22 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B704F10AB0E; Thu, 22 Sep 2022 11:21: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=1663870881; x=1695406881; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Pt6BjpZXYL0Zm6+tvuXv3a61ZSXvTaNg3/pz+MVj3o4=; b=EtCKsyQ883XUlEbV8W1ExAD7O5iUboJHWKD0RW3s/1wmz/+Hy3q84teq sjHQoQKqYnL5OXHdnuwY4+28HrLqkNCuOWO/sFK35wXLFTJ8PTjsIIFn2 0yylW0lyhMkIgzQcqFuAhg8NaonA4x9lyqJsxcuR7xN6kah3P9/Q9q2p2 iO7hzuRHUCwNGY2raMk0iiOh/sbWtzNeFSz+NBpO57STN0MwoB5p6NnXS fgxe0hYED8AzqFtMIgZZp65ZJSG9/iFmVgpXk+Fd9/CJgEcYoplhdRdDY ksDcoP7b+Xs8lL3beAfHYJS4TYP5DLdksHr0iIfjQvjsESbt2lDYXYcFh A==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="386675927" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="386675927" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:13 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086616" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:12 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov , Yuan Yao Subject: [PATCH v5 01/30] KVM: x86: Drop kvm_user_return_msr_cpu_online() Date: Thu, 22 Sep 2022 11:20:30 -0700 Message-Id: <38d3b15ede9583300e22380d1517b6ec9b591355.1663869838.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 43a6a7efc6ec..9e1b3af4a074 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -196,6 +196,7 @@ module_param(eager_page_split, bool, 0644); struct kvm_user_return_msrs { struct user_return_notifier urn; + bool initialized; bool registered; struct kvm_user_return_msr_values { u64 host; @@ -409,18 +410,20 @@ int kvm_find_user_return_msr(u32 msr) } EXPORT_SYMBOL_GPL(kvm_find_user_return_msr); -static void kvm_user_return_msr_cpu_online(void) +static void kvm_user_return_msr_init_cpu(struct kvm_user_return_msrs *msrs) { - unsigned int cpu = smp_processor_id(); - struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu); u64 value; int i; + if (msrs->initialized) + return; + for (i = 0; i < kvm_nr_uret_msrs; ++i) { rdmsrl_safe(kvm_uret_msrs_list[i], &value); msrs->values[i].host = value; msrs->values[i].curr = value; } + msrs->initialized = true; } int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask) @@ -429,6 +432,8 @@ int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask) struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu); int err; + kvm_user_return_msr_init_cpu(msrs); + value = (value & mask) | (msrs->values[slot].host & ~mask); if (value == msrs->values[slot].curr) return 0; @@ -9229,7 +9234,12 @@ int kvm_arch_init(void *opaque) return -ENOMEM; } - user_return_msrs = alloc_percpu(struct kvm_user_return_msrs); + /* + * __GFP_ZERO to ensure user_return_msrs.initialized = false. + * See kvm_user_return_msr_init_cpu(). + */ + user_return_msrs = alloc_percpu_gfp(struct kvm_user_return_msrs, + GFP_KERNEL | __GFP_ZERO); if (!user_return_msrs) { printk(KERN_ERR "kvm: failed to allocate percpu kvm_user_return_msrs\n"); r = -ENOMEM; @@ -11866,7 +11876,6 @@ int kvm_arch_hardware_enable(void) u64 max_tsc = 0; bool stable, backwards_tsc = false; - kvm_user_return_msr_cpu_online(); ret = static_call(kvm_x86_hardware_enable)(); if (ret != 0) return ret; From patchwork Thu Sep 22 18:20:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985645 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 4BE6EC54EE9 for ; Thu, 22 Sep 2022 18:21:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231658AbiIVSV0 (ORCPT ); Thu, 22 Sep 2022 14:21:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34464 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230161AbiIVSVW (ORCPT ); Thu, 22 Sep 2022 14:21:22 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC7F210AB0F; Thu, 22 Sep 2022 11:21: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=1663870881; x=1695406881; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=FvgmsUs8lZSxyh4Y20DUAz52tAJaGD/FMSGQt1yYizU=; b=hrQVtJDyWatL6xxGM/5SfG8asl7eIXjGs78PNuYijbsN0guws9YqjR0Y mzrumyZPkpTmgSTH9r0SutGOVJ+51Uahzjj/5S2FimO2z5BOVkXlO9QMg /QNDXdYD2KSnL9GpMIrqduQBN8/CKeMFev8mJl8pF31DDPT00dJSe7smi lHdJv6Imfax3kCaZ61DP0T52pRuqSL4B0LltcutnjyWmlpt8bHV5OnWO5 gkyqRmdV0p61Jmpeui5bZA0/GeVX4d7gtThwDkJuJxs+Mh9YYgygrIFcx 1h0C6QcKNKxYixL54qUv1FYIeDxUz9wvusirzlRbu1sc5qn25oDi22bhA Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="386675930" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="386675930" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:13 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086630" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:13 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov , Yuan Yao Subject: [PATCH v5 02/30] KVM: x86: Use this_cpu_ptr() instead of per_cpu_ptr(smp_processor_id()) Date: Thu, 22 Sep 2022 11:20:31 -0700 Message-Id: <851653aeb7230572e5b41cc617e89eeae38be24b.1663869838.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 9e1b3af4a074..7fc19533a484 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_user_return_msrs *msrs) int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask) { - unsigned int cpu = smp_processor_id(); - struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu); + struct kvm_user_return_msrs *msrs = this_cpu_ptr(user_return_msrs); int err; kvm_user_return_msr_init_cpu(msrs); @@ -453,8 +452,7 @@ EXPORT_SYMBOL_GPL(kvm_set_user_return_msr); static void drop_user_return_notifiers(void) { - unsigned int cpu = smp_processor_id(); - struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu); + struct kvm_user_return_msrs *msrs = this_cpu_ptr(user_return_msrs); if (msrs->registered) kvm_on_user_return(&msrs->urn); From patchwork Thu Sep 22 18:20:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985646 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 88560C54EE9 for ; Thu, 22 Sep 2022 18:21:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231772AbiIVSV3 (ORCPT ); Thu, 22 Sep 2022 14:21:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34510 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230526AbiIVSVY (ORCPT ); Thu, 22 Sep 2022 14:21:24 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E7D7B10AB06; Thu, 22 Sep 2022 11:21: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=1663870883; x=1695406883; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=iSiOB8K33KBB+MpqSlAXmmbAk5EpIN4mnNiO2Q4lzBk=; b=RUOdyFiImukkH4YOKpyu8DOuzyjh3gjNXBAWqPkZfdClUMkh4RUhEiLg c0Ilu4G9+m2lNbFfy+uruY4YaspfBN9B1Xf+9W/yphFmL3HSPraNxHHTk qIPPNkeq6nujy55uJqHsLC5CiRxdauDvbATcNx8hwQ1ctWQuh0x+LWL6n xYLa5eWogkeOu0d5s59CQvDte/z51/b1p6uHy5u4p4ZoJzaFWADiyCjL4 ztmgN3G438eo5F8zuV3/VAMSKiGRgiZem/yC4XOJ7u1aSr96KT/yINRB0 9pO272SNGDZO0fW8FDASnNsWm5PAVLxY4wKn01HUis1fpf/tkHcXNAh85 Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="386675938" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="386675938" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:14 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086639" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:14 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov , Yuan Yao Subject: [PATCH v5 03/30] KVM: x86: Move check_processor_compatibility from init ops to runtime ops Date: Thu, 22 Sep 2022 11:20:32 -0700 Message-Id: <82455a41cd39decdd1eb8765c37278aa78a3a72e.1663869838.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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) #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_host.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_mode_logical) struct kvm_x86_ops { const char *name; + int (*check_processor_compatibility)(void); int (*hardware_enable)(void); void (*hardware_disable)(void); void (*hardware_unsetup)(void); @@ -1655,7 +1656,6 @@ struct kvm_x86_nested_ops { struct kvm_x86_init_ops { int (*cpu_has_kvm_support)(void); int (*disabled_by_bios)(void); - int (*check_processor_compatibility)(void); int (*hardware_setup)(void); unsigned int (*handle_intel_pt_intr)(void); 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 char *hypercall) hypercall[2] = 0xd9; } -static int __init svm_check_processor_compat(void) +static int svm_check_processor_compatibility(void) { return 0; } @@ -4740,6 +4740,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .name = "kvm_amd", .hardware_unsetup = svm_hardware_unsetup, + .check_processor_compatibility = svm_check_processor_compatibility, .hardware_enable = svm_hardware_enable, .hardware_disable = svm_hardware_disable, .has_emulated_msr = svm_has_emulated_msr, @@ -5122,7 +5123,6 @@ static struct kvm_x86_init_ops svm_init_ops __initdata = { .cpu_has_kvm_support = has_svm, .disabled_by_bios = is_disabled, .hardware_setup = svm_hardware_setup, - .check_processor_compatibility = svm_check_processor_compat, .runtime_ops = &svm_x86_ops, .pmu_ops = &amd_pmu_ops, diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index c9b49a09e6b5..4ad058dc9794 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2494,8 +2494,8 @@ static bool cpu_has_sgx(void) return cpuid_eax(0) >= 0x12 && (cpuid_eax(0x12) & BIT(0)); } -static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, - u32 msr, u32 *result) +static int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, + u32 msr, u32 *result) { u32 vmx_msr_low, vmx_msr_high; u32 ctl = ctl_min | ctl_opt; @@ -2513,7 +2513,7 @@ static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, return 0; } -static __init u64 adjust_vmx_controls64(u64 ctl_opt, u32 msr) +static u64 adjust_vmx_controls64(u64 ctl_opt, u32 msr) { u64 allowed; @@ -2522,8 +2522,8 @@ static __init u64 adjust_vmx_controls64(u64 ctl_opt, u32 msr) return ctl_opt & allowed; } -static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf, - struct vmx_capability *vmx_cap) +static int setup_vmcs_config(struct vmcs_config *vmcs_conf, + struct vmx_capability *vmx_cap) { u32 vmx_msr_low, vmx_msr_high; u32 min, opt, min2, opt2; @@ -7416,7 +7416,7 @@ static int vmx_vm_init(struct kvm *kvm) return 0; } -static int __init vmx_check_processor_compat(void) +static int vmx_check_processor_compatibility(void) { struct vmcs_config vmcs_conf; struct vmx_capability vmx_cap; @@ -8014,6 +8014,7 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = { .hardware_unsetup = vmx_hardware_unsetup, + .check_processor_compatibility = vmx_check_processor_compatibility, .hardware_enable = vmx_hardware_enable, .hardware_disable = vmx_hardware_disable, .has_emulated_msr = vmx_has_emulated_msr, @@ -8403,7 +8404,6 @@ static __init int hardware_setup(void) static struct kvm_x86_init_ops vmx_init_ops __initdata = { .cpu_has_kvm_support = cpu_has_kvm_support, .disabled_by_bios = vmx_disabled_by_bios, - .check_processor_compatibility = vmx_check_processor_compat, .hardware_setup = hardware_setup, .handle_intel_pt_intr = NULL, diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 7fc19533a484..34c7336ac581 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12031,7 +12031,6 @@ void kvm_arch_hardware_unsetup(void) int kvm_arch_check_processor_compat(void *opaque) { struct cpuinfo_x86 *c = &cpu_data(smp_processor_id()); - struct kvm_x86_init_ops *ops = opaque; WARN_ON(!irqs_disabled()); @@ -12039,7 +12038,7 @@ int kvm_arch_check_processor_compat(void *opaque) __cr4_reserved_bits(cpu_has, &boot_cpu_data)) return -EIO; - return ops->check_processor_compatibility(); + return static_call(kvm_x86_check_processor_compatibility)(); } bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu) From patchwork Thu Sep 22 18:20:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985647 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 C8F9CECAAD8 for ; Thu, 22 Sep 2022 18:21:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232107AbiIVSVc (ORCPT ); Thu, 22 Sep 2022 14:21:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231220AbiIVSVY (ORCPT ); Thu, 22 Sep 2022 14:21:24 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F0C6410AB10; Thu, 22 Sep 2022 11:21: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=1663870883; x=1695406883; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=4rIEhITIti8CDX64fvcn1ALaGLiMyhto8tKD00HaSAA=; b=cDI3fXPkHpgCFYGBx86iIKPl8sCRXznXk5NPHwPE+s2ZRBGvu8iUNIZ4 QZPWvYF0dE5H0MSfdTHTYwrbysmZ5RrxOFtO1ElN8TWpDN6O2noYtP3Fq Z+13QoSz7aMh25/6ck+BKRbm+S25+B45rXDsDJri+ZhdJ7oFvHesIW97W tIhm9b3xtcmG6OugUI7qiYcC51eRwe769Jkj+rUNnybuY9w/G88CRdLhH q/F8930zk+MXaz1fQtNrB66JOrt23DmEj5Rvfvw7WCQHHr5O0L2c/4int wF86vykhhCF9Zs4l1f2+gEgYp+MIBk+K5cV3cAB/cnuj6WXq2Cv/hg/SS w==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="386675942" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="386675942" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:15 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086646" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:14 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov , Suzuki K Poulose , Anup Patel , Claudio Imbrenda , Yuan Yao Subject: [PATCH v5 04/30] Partially revert "KVM: Pass kvm_init()'s opaque param to additional arch funcs" Date: Thu, 22 Sep 2022 11:20:33 -0700 Message-Id: <0e20f8df9549cbee3b764a2a07735b78da4f50a4.1663869838.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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; } -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; } -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; } -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; } -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; } -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 34c7336ac581..230812d6cbfd 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12028,7 +12028,7 @@ void kvm_arch_hardware_unsetup(void) static_call(kvm_x86_hardware_unsetup)(); } -int kvm_arch_check_processor_compat(void *opaque) +int kvm_arch_check_processor_compat(void) { struct cpuinfo_x86 *c = &cpu_data(smp_processor_id()); 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 -struct kvm_cpu_compat_check { - void *opaque; - int *ret; -}; - -static void check_processor_compat(void *data) +static void check_processor_compat(void *rtn) { - struct kvm_cpu_compat_check *c = data; - - *c->ret = kvm_arch_check_processor_compat(c->opaque); + *(int *)rtn = kvm_arch_check_processor_compat(); } int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, struct module *module) { - struct kvm_cpu_compat_check c; int r; int cpu; @@ -5842,10 +5834,8 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, if (r < 0) goto out_free_1; - c.ret = &r; - c.opaque = opaque; for_each_online_cpu(cpu) { - smp_call_function_single(cpu, check_processor_compat, &c, 1); + smp_call_function_single(cpu, check_processor_compat, &r, 1); if (r < 0) goto out_free_2; } From patchwork Thu Sep 22 18:20:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985648 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 29CF5C6FA8B for ; Thu, 22 Sep 2022 18:21:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232027AbiIVSVe (ORCPT ); Thu, 22 Sep 2022 14:21:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34526 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230094AbiIVSVY (ORCPT ); Thu, 22 Sep 2022 14:21:24 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A092A10AB0E; Thu, 22 Sep 2022 11:21:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663870883; x=1695406883; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=xXzjDD2RfWE2U6k2WEiDMxRTTvA39oD3cMMwgQ6GMXc=; b=Qcb3NpWRyQhVgVDcss/itiY2oDFtFWNy+TL2azb/hpTbET0/xln/K1PU mXhPSjOfqugQ7/+0d14jiEFZKdY18k2YD2V7OBlWTOTx9r1yh3MqJ4Ngm tgkvSJZWOMmqCyQt2EB14E/SMx7DK57OQmkmwCeNDkYDhTUQA5wcNmXIE lhmUgc2B7Bu9V4NjapUuS5n2YlykKbB5bXzYrOe2JlSs2OPgwoUtAMrOV WDb/3iFRteWHrsGtQx8pDC1wVxPFpKBQbsSwN3QqnEhKFmYzyg9DQ3fqE oxFwqHVheZNx2REGGKEft3JaO/00Ly1yKzjx9r8FFvFg7BlNFBWTugxtY Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="386675950" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="386675950" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:15 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086658" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:15 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov Subject: [PATCH v5 05/30] KVM: Provide more information in kernel log if hardware enabling fails Date: Thu, 22 Sep 2022 11:20:34 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Isaku Yamahata 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. Suggested-by: Chao Gao Signed-off-by: Isaku Yamahata --- virt/kvm/kvm_main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 4243a9541543..4f19c47aab1c 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -5006,7 +5006,8 @@ static void hardware_enable_nolock(void *junk) if (r) { cpumask_clear_cpu(cpu, cpus_hardware_enabled); atomic_inc(&hardware_enable_failed); - pr_info("kvm: enabling virtualization on CPU%d failed\n", cpu); + pr_warn("kvm: enabling virtualization on CPU%d failed during %pSb\n", + cpu, __builtin_return_address(0)); } } From patchwork Thu Sep 22 18:20:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985650 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 121CEECAAD8 for ; Thu, 22 Sep 2022 18:21:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232366AbiIVSVj (ORCPT ); Thu, 22 Sep 2022 14:21:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34588 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231510AbiIVSVZ (ORCPT ); Thu, 22 Sep 2022 14:21:25 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5A97710AB19; Thu, 22 Sep 2022 11:21:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663870884; x=1695406884; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=zJVDnDziwHyux/2eNta6aSYKvcSSvYnXO9CPyXFOSmw=; b=gqvQqNC2K31GhUO4wDpoaG1o5ewnLtphVmcNsO/Sma9q9JFgFhXHZcmM BR8gqGAFYxbeO3oxgp5m5rxtwagvFQJa8nozC0tXUcE+p9U/Ei8Y1iWMh 09Gt8GirhAHC4P7qM9SdFpmOnli2ueVFO7Nc54T3S0LB+vUgR/mlwZ6Uj 9FS5koj2uDncpRH64DWcJd5g/9mVpr7ECCUpZI2nJF3UNROWd2nm8Pi9Q YZrIDOWafi119CDwE+9ml/LDd/9/NVorXNGSmcvSBvP0B3xzROSNBP2q4 OfmK8OUT9q1WWs3JYjulyeWmBGYDqMvj1ZAEERbS0ud8awGVBWdId4+97 A==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="386675960" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="386675960" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:16 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086663" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:15 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov Subject: [PATCH v5 06/30] KVM: arm64: Simplify the CPUHP logic Date: Thu, 22 Sep 2022 11:20:35 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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: 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 = host_ptimer_irq_flags; } -static void kvm_timer_init_interrupt(void *info) +void kvm_timer_cpu_up(void) { enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags); - enable_percpu_irq(host_ptimer_irq, host_ptimer_irq_flags); + if (host_ptimer_irq) + enable_percpu_irq(host_ptimer_irq, host_ptimer_irq_flags); +} + +void kvm_timer_cpu_down(void) +{ + disable_percpu_irq(host_vtimer_irq); + if (host_ptimer_irq) + disable_percpu_irq(host_ptimer_irq); } int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value) @@ -976,18 +984,6 @@ void kvm_arm_timer_write_sysreg(struct kvm_vcpu *vcpu, preempt_enable(); } -static int kvm_timer_starting_cpu(unsigned int cpu) -{ - kvm_timer_init_interrupt(NULL); - return 0; -} - -static int kvm_timer_dying_cpu(unsigned int cpu) -{ - disable_percpu_irq(host_vtimer_irq); - return 0; -} - static int timer_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu) { if (vcpu) @@ -1185,9 +1181,6 @@ int kvm_timer_hyp_init(bool has_gic) goto out_free_irq; } - cpuhp_setup_state(CPUHP_AP_KVM_ARM_TIMER_STARTING, - "kvm/arm/timer:starting", kvm_timer_starting_cpu, - kvm_timer_dying_cpu); return 0; out_free_irq: free_percpu_irq(host_vtimer_irq, kvm_get_running_vcpus()); 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) int kvm_arch_hardware_enable(void) { + int was_enabled = __this_cpu_read(kvm_arm_hardware_enabled); + _kvm_arch_hardware_enable(NULL); + + if (!was_enabled) { + kvm_vgic_cpu_up(); + kvm_timer_cpu_up(); + } + return 0; } @@ -1690,6 +1698,11 @@ static void _kvm_arch_hardware_disable(void *discard) void kvm_arch_hardware_disable(void) { + if (__this_cpu_read(kvm_arm_hardware_enabled)) { + kvm_timer_cpu_down(); + kvm_vgic_cpu_down(); + } + if (!is_protected_kvm_enabled()) _kvm_arch_hardware_disable(NULL); } diff --git a/arch/arm64/kvm/vgic/vgic-init.c b/arch/arm64/kvm/vgic/vgic-init.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) /* GENERIC PROBE */ -static int vgic_init_cpu_starting(unsigned int cpu) +void kvm_vgic_cpu_up(void) { enable_percpu_irq(kvm_vgic_global_state.maint_irq, 0); - return 0; } -static int vgic_init_cpu_dying(unsigned int cpu) +void kvm_vgic_cpu_down(void) { disable_percpu_irq(kvm_vgic_global_state.maint_irq); - return 0; } static irqreturn_t vgic_maintenance_handler(int irq, void *data) @@ -584,19 +582,6 @@ int kvm_vgic_hyp_init(void) return ret; } - ret = cpuhp_setup_state(CPUHP_AP_KVM_ARM_VGIC_INIT_STARTING, - "kvm/arm/vgic:starting", - vgic_init_cpu_starting, vgic_init_cpu_dying); - if (ret) { - kvm_err("Cannot register vgic CPU notifier\n"); - goto out_free_irq; - } - kvm_info("vgic interrupt IRQ%d\n", kvm_vgic_global_state.maint_irq); return 0; - -out_free_irq: - free_percpu_irq(kvm_vgic_global_state.maint_irq, - kvm_get_running_vcpus()); - return ret; } 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); +/* 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); +/* 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, From patchwork Thu Sep 22 18:20:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985653 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 1E5E4C6FA82 for ; Thu, 22 Sep 2022 18:21:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231837AbiIVSVr (ORCPT ); Thu, 22 Sep 2022 14:21:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34590 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231555AbiIVSVZ (ORCPT ); Thu, 22 Sep 2022 14:21:25 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AED1010AB10; Thu, 22 Sep 2022 11:21:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663870884; x=1695406884; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=drh4+aWR2QC13n5vYRunnLDCblNzsS8rJFTrdTBFtzk=; b=J7W3ya0YIOvYRyqx58ZU6khijaUjffuLSYTqFZHipg8BaC9sWfRN/6ML LUhQYMB/6tQvNYL8YDYYG88KizoDikmaW6rTbjW4u6zoSbU0rs95TMzX6 UYMXYzlHemgBX0XbSARb1PABceK5H8cCrkhdwo9qYxdra9x2KMFlSrw+i JqFvhoKG5BLGNBrgJLSdKXxInzOpGmAa5iT9Pm7kDRnQ0bhLrxot4qxk5 udj5G7p6+etXFAXUP0p7UH98X9Bo15LJlrZbpJhz2abIWIv8NYcvEb6A5 oX6C1WwlU3ky4ggpOdLYgSi+2hPmDPde2Jlzwd0tTYP7UhKpX5oEnG0cB w==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="386675967" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="386675967" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:16 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086673" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:16 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov , Yuan Yao Subject: [PATCH v5 07/30] KVM: Rename and move CPUHP_AP_KVM_STARTING to ONLINE section Date: Thu, 22 Sep 2022 11:20:36 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 { /* 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 4f19c47aab1c..1bb7038e1ecf 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -5011,13 +5011,27 @@ static void hardware_enable_nolock(void *junk) } } -static int kvm_starting_cpu(unsigned int cpu) +static int kvm_online_cpu(unsigned int cpu) { + int ret = 0; + raw_spin_lock(&kvm_count_lock); - if (kvm_usage_count) + /* + * Abort the CPU online process if hardware virtualization cannot + * be enabled. Otherwise running VMs would encounter unrecoverable + * errors when scheduled to this CPU. + */ + if (kvm_usage_count) { + WARN_ON_ONCE(atomic_read(&hardware_enable_failed)); + hardware_enable_nolock(NULL); + if (atomic_read(&hardware_enable_failed)) { + atomic_set(&hardware_enable_failed, 0); + ret = -EIO; + } + } raw_spin_unlock(&kvm_count_lock); - return 0; + return ret; } static void hardware_disable_nolock(void *junk) @@ -5030,7 +5044,7 @@ static void hardware_disable_nolock(void *junk) kvm_arch_hardware_disable(); } -static int kvm_dying_cpu(unsigned int cpu) +static int kvm_offline_cpu(unsigned int cpu) { raw_spin_lock(&kvm_count_lock); if (kvm_usage_count) @@ -5841,8 +5855,8 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, goto out_free_2; } - r = cpuhp_setup_state_nocalls(CPUHP_AP_KVM_STARTING, "kvm/cpu:starting", - kvm_starting_cpu, kvm_dying_cpu); + r = cpuhp_setup_state_nocalls(CPUHP_AP_KVM_ONLINE, "kvm/cpu:online", + kvm_online_cpu, kvm_offline_cpu); if (r) goto out_free_2; register_reboot_notifier(&kvm_reboot_notifier); @@ -5903,7 +5917,7 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, kmem_cache_destroy(kvm_vcpu_cache); out_free_3: unregister_reboot_notifier(&kvm_reboot_notifier); - cpuhp_remove_state_nocalls(CPUHP_AP_KVM_STARTING); + cpuhp_remove_state_nocalls(CPUHP_AP_KVM_ONLINE); out_free_2: kvm_arch_hardware_unsetup(); out_free_1: @@ -5929,7 +5943,7 @@ void kvm_exit(void) kvm_async_pf_deinit(); unregister_syscore_ops(&kvm_syscore_ops); unregister_reboot_notifier(&kvm_reboot_notifier); - cpuhp_remove_state_nocalls(CPUHP_AP_KVM_STARTING); + cpuhp_remove_state_nocalls(CPUHP_AP_KVM_ONLINE); on_each_cpu(hardware_disable_nolock, NULL, 1); kvm_arch_hardware_unsetup(); kvm_arch_exit(); From patchwork Thu Sep 22 18:20:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985654 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 A350AC54EE9 for ; Thu, 22 Sep 2022 18:21:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232350AbiIVSVv (ORCPT ); Thu, 22 Sep 2022 14:21:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232065AbiIVSVc (ORCPT ); Thu, 22 Sep 2022 14:21:32 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 384A110AB24; Thu, 22 Sep 2022 11:21:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663870886; x=1695406886; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=1Zdp6GSci2SrjPZucFtc1DQweQC9BKDGkU2/aUltD0k=; b=j+yQ+zcfZJdI5sD4ovn0dWWEO7xvqpJXnbj/UuVTcaXk5beO9T98SKTu b6PHTtD5pEkeUV7CUgtZIUDGAF/0xoYcYf9Cj+ET7pu7F8HGJBbiA5+ne 3Qgpt7sAiE7EqbTZ9bID/1sWOqTMUlOFOHECoY0QTcRGjHAbU6nRUduIe H/zygSVYK2esQDpyfhCyH5YaftEiO0I5F85HDACCnoMl1yEe3BlbTjoVM NYyhS7eDEvtcp2w9ZyJvQYZqUL1QvamIJe9HVis+8lOCbXmhqUMYJbomD fFdvqo95y5n6bMBhTVXfvt+HQToF8E+Ro9KuF60Z7scyw/4B2KFB+u8Sn g==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="386675972" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="386675972" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:17 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086685" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:16 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov , Yuan Yao Subject: [PATCH v5 08/30] KVM: Do compatibility checks on hotplugged CPUs Date: Thu, 22 Sep 2022 11:20:37 -0700 Message-Id: <6505137f5dc96214ca55caae3b04230148c54460.1663869838.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 4ad058dc9794..26f16e310869 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7420,20 +7420,22 @@ static int vmx_check_processor_compatibility(void) { struct vmcs_config vmcs_conf; struct vmx_capability vmx_cap; + int cpu = smp_processor_id(); if (!this_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) || !this_cpu_has(X86_FEATURE_VMX)) { - pr_err("kvm: VMX is disabled on CPU %d\n", smp_processor_id()); + pr_err("kvm: VMX is disabled on CPU %d\n", cpu); return -EIO; } - if (setup_vmcs_config(&vmcs_conf, &vmx_cap) < 0) + if (setup_vmcs_config(&vmcs_conf, &vmx_cap) < 0) { + pr_err("kvm: failed to setup vmcs config on CPU %d\n", cpu); return -EIO; + } if (nested) nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, vmx_cap.ept); if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { - printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", - smp_processor_id()); + pr_err("kvm: CPU %d feature inconsistency!\n", cpu); return -EIO; } return 0; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 230812d6cbfd..f5ebb69996d5 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12030,9 +12030,16 @@ void kvm_arch_hardware_unsetup(void) int kvm_arch_check_processor_compat(void) { - struct cpuinfo_x86 *c = &cpu_data(smp_processor_id()); + int cpu = smp_processor_id(); + struct cpuinfo_x86 *c = &cpu_data(cpu); - WARN_ON(!irqs_disabled()); + /* + * Compatibility checks are done when loading KVM or in KVM's CPU + * hotplug callback. It ensures all online CPUs are compatible to run + * vCPUs. For other cases, compatibility checks are unnecessary or + * even problematic. Try to detect improper usages here. + */ + WARN_ON(!irqs_disabled() && cpu_active(cpu)); if (__cr4_reserved_bits(cpu_has, c) != __cr4_reserved_bits(cpu_has, &boot_cpu_data)) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 1bb7038e1ecf..b1bf44af523c 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -5013,7 +5013,11 @@ static void hardware_enable_nolock(void *junk) static int kvm_online_cpu(unsigned int cpu) { - int ret = 0; + int ret; + + ret = kvm_arch_check_processor_compat(); + if (ret) + return ret; raw_spin_lock(&kvm_count_lock); /* @@ -5073,6 +5077,17 @@ static int hardware_enable_all(void) { int r = 0; + /* + * During onlining a CPU, cpu_online_mask is set before kvm_online_cpu() + * is called. on_each_cpu() between them includes the CPU. As a result, + * hardware_enable_nolock() may get invoked before kvm_online_cpu(). + * This would enable hardware virtualization on that cpu without + * compatibility checks, which can potentially crash system or break + * running VMs. + * + * Disable CPU hotplug to prevent this case from happening. + */ + cpus_read_lock(); raw_spin_lock(&kvm_count_lock); kvm_usage_count++; @@ -5087,6 +5102,7 @@ static int hardware_enable_all(void) } raw_spin_unlock(&kvm_count_lock); + cpus_read_unlock(); return r; } From patchwork Thu Sep 22 18:20:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985649 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 63F02ECAAD8 for ; Thu, 22 Sep 2022 18:21:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232338AbiIVSVg (ORCPT ); Thu, 22 Sep 2022 14:21:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34586 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231479AbiIVSVZ (ORCPT ); Thu, 22 Sep 2022 14:21:25 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5A89C10AB17; Thu, 22 Sep 2022 11:21:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663870884; x=1695406884; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=48U9dCYj/WeMAxDcm+7eitcQw4Ht0b3752iS3YU0zk8=; b=O1Imqpgp+JYoateUNcb+NiVQ1dhXgU4tDEgdfZrpn8bDP76mr0RPmSc3 jvHJyJGVEBUtsvqQCAJI0ceyj366yN+uFS351rJOk9CIm426SV2S4Wox6 ddt0r2Q3ax2RMCkV6lIAfRPW43s8HhGspMcc9rP4V7PzMDd3z+XYVUdcs 2Aq18wzRd8H8vHuTPcVG4PqqRHfdOux/XWJipwK8X+33UQWxZkBG2u4S3 6lHDnPLuZQ/L+NhiF58zuKFkXJe1sxKd/vv0YI2O98nq/5mZi+5KM4QKg nXduQdiiJe8GsZTUh6ROpphDwp+ZtrpW6LqZus/Lm1c+vcd9dddjz5az0 w==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="386675974" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="386675974" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:17 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086696" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:17 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov Subject: [PATCH v5 09/30] KVM: Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock Date: Thu, 22 Sep 2022 11:20:38 -0700 Message-Id: <92836b09c8e0f19f8e506008e45993881d22b6d1.1663869838.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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. Because kvm_suspend() and kvm_resume() is called with interrupt disabled, they don't need preempt_disable/enable() pair. Opportunistically add some comments on locking. 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/locking.rst index 845a561629f1..55d6559ace2a 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 mechanism described above. :Type: mutex :Arch: any :Protects: - vm_list - -``kvm_count_lock`` -^^^^^^^^^^^^^^^^^^ - -:Type: raw_spinlock_t -:Arch: any -:Protects: - hardware virtualization enable/disable -:Comment: 'raw' because hardware enabling/disabling must be atomic /wrt - migration. + - kvm_usage_count + - hardware virtualization enable/disable +:Comment: Use cpus_read_lock() for hardware virtualization enable/disable + because hardware enabling/disabling must be atomic /wrt + CPU hotplug. The lock order is cpus lock => kvm_lock. ``kvm->mn_invalidate_lock`` ^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index b1bf44af523c..c4b908553726 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -100,7 +100,6 @@ EXPORT_SYMBOL_GPL(halt_poll_ns_shrink); */ DEFINE_MUTEX(kvm_lock); -static DEFINE_RAW_SPINLOCK(kvm_count_lock); LIST_HEAD(vm_list); static cpumask_var_t cpus_hardware_enabled; @@ -4996,6 +4995,8 @@ static void hardware_enable_nolock(void *junk) int cpu = raw_smp_processor_id(); int r; + WARN_ON_ONCE(preemptible()); + if (cpumask_test_cpu(cpu, cpus_hardware_enabled)) return; @@ -5019,7 +5020,7 @@ static int kvm_online_cpu(unsigned int cpu) if (ret) return ret; - 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)); + /* + * 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(NULL); + preempt_enable(); if (atomic_read(&hardware_enable_failed)) { atomic_set(&hardware_enable_failed, 0); ret = -EIO; } } - raw_spin_unlock(&kvm_count_lock); + mutex_unlock(&kvm_lock); return ret; } @@ -5042,6 +5050,8 @@ static void hardware_disable_nolock(void *junk) { int cpu = raw_smp_processor_id(); + WARN_ON_ONCE(preemptible()); + if (!cpumask_test_cpu(cpu, cpus_hardware_enabled)) return; cpumask_clear_cpu(cpu, cpus_hardware_enabled); @@ -5050,10 +5060,18 @@ static void hardware_disable_nolock(void *junk) static int kvm_offline_cpu(unsigned int cpu) { - 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; } @@ -5068,9 +5086,11 @@ static void hardware_disable_all_nolock(void) static void hardware_disable_all(void) { - raw_spin_lock(&kvm_count_lock); + cpus_read_lock(); + mutex_lock(&kvm_lock); hardware_disable_all_nolock(); - raw_spin_unlock(&kvm_count_lock); + mutex_unlock(&kvm_lock); + cpus_read_unlock(); } static int hardware_enable_all(void) @@ -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); kvm_usage_count++; if (kvm_usage_count == 1) { @@ -5101,7 +5121,7 @@ static int hardware_enable_all(void) } } - raw_spin_unlock(&kvm_count_lock); + mutex_unlock(&kvm_lock); cpus_read_unlock(); return r; @@ -5708,15 +5728,27 @@ static void kvm_init_debug(void) static int kvm_suspend(void) { - if (kvm_usage_count) + /* + * The caller ensures that CPU hotplug is disabled by + * cpu_hotplug_disable() and other CPUs are offlined. No need for + * locking. + */ + lockdep_assert_not_held(&kvm_lock); + + if (kvm_usage_count) { + /* + * Because kvm_suspend() is called with interrupt disabled, no + * need to disable preemption. + */ hardware_disable_nolock(NULL); + } return 0; } static void kvm_resume(void) { if (kvm_usage_count) { - lockdep_assert_not_held(&kvm_count_lock); + lockdep_assert_not_held(&kvm_lock); hardware_enable_nolock(NULL); } } From patchwork Thu Sep 22 18:20:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985657 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 5A804C6FA82 for ; Thu, 22 Sep 2022 18:21:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232476AbiIVSVy (ORCPT ); Thu, 22 Sep 2022 14:21:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34870 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232106AbiIVSVc (ORCPT ); Thu, 22 Sep 2022 14:21:32 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A9529FFA41; Thu, 22 Sep 2022 11:21:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663870888; x=1695406888; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=zO1wIEhxQ5q5ko0adH4xh6bT/ltNKHCmmVgWcfEpVrI=; b=bkqY5mYGxflNeOmeZfvI4Hc1Rh9Snqov0damjqqtGcstDzo+8gmwkj5L 6Dfbtn7IU/keD9U1hdaeKx0s/G2903tzVwPQGfAKmKWh6UfOt7xTawcmS rj3gMTI7vxqDhyGxlSlmDKAsNZNd/nJDT6UpsL4RkbRSK5WTXyj3fjcBv l3zkLMZC3Aa2I9h1uqE6AGEUP8y+2SyHEfBB1BsAH9mNHTzuJtiSukOGv ceGRp7vjJp0BvVJ/qH6AYI8vTPf3t+iYMbJVicmMi7IPoW/Kd9x7J+h1j J09SDl03B0xWCIpwKyS/qJiS61nmxWRAwp5Xiev47zAaTZZnCe6JnWw5i w==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="386675979" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="386675979" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:18 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086707" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:18 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov Subject: [PATCH v5 10/30] KVM: Add arch hooks when VM is added/deleted Date: Thu, 22 Sep 2022 11:20:39 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Isaku Yamahata and pass kvm_usage_count with kvm_lock. Move kvm_arch_post_init_vm() under kvm_arch_add_vm(). Replace enable/disable_hardware_all() with the default implementation of kvm_arch_add/del_vm(). Later kvm_arch_post_init_vm() is deleted once x86 overrides kvm_arch_add_vm(). Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- include/linux/kvm_host.h | 2 + virt/kvm/kvm_main.c | 121 ++++++++++++++++++++------------------- 2 files changed, 65 insertions(+), 58 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index eab352902de7..3fbb01bbac98 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); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index c4b908553726..e2c8823786ff 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -142,8 +142,9 @@ static int kvm_no_compat_open(struct inode *inode, struct file *file) #define KVM_COMPAT(c) .compat_ioctl = kvm_no_compat_ioctl, \ .open = kvm_no_compat_open #endif -static int hardware_enable_all(void); -static void hardware_disable_all(void); +static void hardware_enable_nolock(void *junk); +static void hardware_disable_nolock(void *junk); +static void kvm_del_vm(void); static void kvm_io_bus_destroy(struct kvm_io_bus *bus); @@ -1106,6 +1107,41 @@ int __weak kvm_arch_post_init_vm(struct kvm *kvm) return 0; } +/* + * Called after the VM is otherwise initialized, but just before adding it to + * the vm_list. + */ +int __weak kvm_arch_add_vm(struct kvm *kvm, int usage_count) +{ + int r = 0; + + if (usage_count != 1) + return 0; + + atomic_set(&hardware_enable_failed, 0); + on_each_cpu(hardware_enable_nolock, NULL, 1); + + if (atomic_read(&hardware_enable_failed)) { + r = -EBUSY; + goto err; + } + + r = kvm_arch_post_init_vm(kvm); +err: + if (r) + on_each_cpu(hardware_disable_nolock, NULL, 1); + return r; +} + +int __weak kvm_arch_del_vm(int usage_count) +{ + if (usage_count) + return 0; + + on_each_cpu(hardware_disable_nolock, NULL, 1); + return 0; +} + /* * Called just after removing the VM from the vm_list, but before doing any * other destruction. @@ -1203,10 +1239,6 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname) if (r) goto out_err_no_arch_destroy_vm; - r = hardware_enable_all(); - if (r) - goto out_err_no_disable; - #ifdef CONFIG_HAVE_KVM_IRQFD INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list); #endif @@ -1223,13 +1255,28 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname) if (r) goto out_err_no_debugfs; - r = kvm_arch_post_init_vm(kvm); - if (r) - goto out_err; - + /* + * During onlining a CPU, cpu_online_mask is set before kvm_online_cpu() + * is called. on_each_cpu() between them includes the CPU. As a result, + * hardware_enable_nolock() may get invoked before kvm_online_cpu(). + * This would enable hardware virtualization on that cpu without + * compatibility checks, which can potentially crash system or break + * running VMs. + * + * Disable CPU hotplug to prevent this case from happening. + */ + cpus_read_lock(); mutex_lock(&kvm_lock); + kvm_usage_count++; + r = kvm_arch_add_vm(kvm, kvm_usage_count); + if (r) { + /* the following kvm_del_vm() decrements kvm_usage_count. */ + mutex_unlock(&kvm_lock); + goto out_err; + } list_add(&kvm->vm_list, &vm_list); mutex_unlock(&kvm_lock); + cpus_read_unlock(); preempt_notifier_inc(); kvm_init_pm_notifier(kvm); @@ -1246,8 +1293,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(); -out_err_no_disable: + kvm_del_vm(); kvm_arch_destroy_vm(kvm); out_err_no_arch_destroy_vm: WARN_ON_ONCE(!refcount_dec_and_test(&kvm->users_count)); @@ -1326,7 +1372,7 @@ 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); } @@ -5075,56 +5121,15 @@ static int kvm_offline_cpu(unsigned int cpu) return 0; } -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) +static void kvm_del_vm(void) { cpus_read_lock(); mutex_lock(&kvm_lock); - hardware_disable_all_nolock(); - mutex_unlock(&kvm_lock); - cpus_read_unlock(); -} - -static int hardware_enable_all(void) -{ - int r = 0; - - /* - * During onlining a CPU, cpu_online_mask is set before kvm_online_cpu() - * is called. on_each_cpu() between them includes the CPU. As a result, - * hardware_enable_nolock() may get invoked before kvm_online_cpu(). - * This would enable hardware virtualization on that cpu without - * compatibility checks, which can potentially crash system or break - * running VMs. - * - * Disable CPU hotplug to prevent this case from happening. - */ - cpus_read_lock(); - mutex_lock(&kvm_lock); - - kvm_usage_count++; - if (kvm_usage_count == 1) { - atomic_set(&hardware_enable_failed, 0); - on_each_cpu(hardware_enable_nolock, NULL, 1); - - if (atomic_read(&hardware_enable_failed)) { - hardware_disable_all_nolock(); - r = -EBUSY; - } - } - + WARN_ON_ONCE(!kvm_usage_count); + kvm_usage_count--; + kvm_arch_del_vm(kvm_usage_count); mutex_unlock(&kvm_lock); cpus_read_unlock(); - - return r; } static int kvm_reboot(struct notifier_block *notifier, unsigned long val, From patchwork Thu Sep 22 18:20:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985652 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 F40C9C54EE9 for ; Thu, 22 Sep 2022 18:21:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232280AbiIVSVp (ORCPT ); Thu, 22 Sep 2022 14:21:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34816 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231941AbiIVSVc (ORCPT ); Thu, 22 Sep 2022 14:21:32 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9227910AB28; Thu, 22 Sep 2022 11:21:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663870886; x=1695406886; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=0GoKHDbGSJCgFZPsDUaLsLiErz0a2t6BkzruRG0eeWc=; b=LhQl73/wWhsg06MUqd/9wDNzWGsiRRwUyMNJ3U2wXiNjiPKkAgeFhbKX DOo+uRE7BYbLxDhnqFDrJNkPNnilsuIyFa0RyfkPo9WIxhe99tYdcIL1w kuGx/qAYu7KoIrfduESyVTjC8XMS49Q2bHNQA7yYfEyjdB/t1FtD85cNA Kg+nuuuREqVWYijDe8EsjF6Bi2NGgO0ONb9EL8vYlcVSJtN9Zv8b3MP8r ntV9dYeWbvyYjZ0D0yCM5jcxoVzdWrHrZXjXONLrKyAgHRFoRZB/NbUPN +pfQHyBvXFVRxq39s83ely0pgYT4RR7Ow+5V148Ic4b9gAK/QQeJvNPua w==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="386675987" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="386675987" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:19 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086722" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:18 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov Subject: [PATCH v5 11/30] KVM: Add arch hook for reboot event Date: Thu, 22 Sep 2022 11:20:40 -0700 Message-Id: <61dcd6722743bf221f6c612e120a9e2350d5a72f.1663869838.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Isaku Yamahata Factor out the logic on reboot event as arch hook. Later kvm/x86 overrides it. Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- include/linux/kvm_host.h | 2 ++ virt/kvm/kvm_main.c | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 3fbb01bbac98..084ee8a13e9f 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1434,6 +1434,8 @@ void kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu, struct dentry *debugfs_ static inline void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu) {} #endif +int kvm_arch_reboot(int val); + int kvm_arch_hardware_enable(void); void kvm_arch_hardware_disable(void); int kvm_arch_hardware_setup(void *opaque); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index e2c8823786ff..58385000b73f 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1142,6 +1142,12 @@ int __weak kvm_arch_del_vm(int usage_count) return 0; } +int __weak kvm_arch_reboot(int val) +{ + on_each_cpu(hardware_disable_nolock, NULL, 1); + return NOTIFY_OK; +} + /* * Called just after removing the VM from the vm_list, but before doing any * other destruction. @@ -5135,6 +5141,8 @@ static void kvm_del_vm(void) static int kvm_reboot(struct notifier_block *notifier, unsigned long val, void *v) { + int r; + /* * Some (well, at least mine) BIOSes hang on reboot if * in vmx root mode. @@ -5143,8 +5151,14 @@ static int kvm_reboot(struct notifier_block *notifier, unsigned long val, */ pr_info("kvm: exiting hardware virtualization\n"); kvm_rebooting = true; - on_each_cpu(hardware_disable_nolock, NULL, 1); - return NOTIFY_OK; + + /* This hook is called without cpuhotplug disabled. */ + cpus_read_lock(); + mutex_lock(&kvm_lock); + r = kvm_arch_reboot(val); + mutex_unlock(&kvm_lock); + cpus_read_unlock(); + return r; } static struct notifier_block kvm_reboot_notifier = { From patchwork Thu Sep 22 18:20:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985651 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 A9712ECAAD8 for ; Thu, 22 Sep 2022 18:21:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232382AbiIVSVm (ORCPT ); Thu, 22 Sep 2022 14:21:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231972AbiIVSVc (ORCPT ); Thu, 22 Sep 2022 14:21:32 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 921CF10AB27; Thu, 22 Sep 2022 11:21:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663870886; x=1695406886; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=3j7AGT1eIC/BE0MhW+coP+8A6Ov/SyQoyWvgLPvXMnc=; b=SyvpkJdP7b70ZGFSZNoWMCFSyTUjmW89uB7vQViUZg2fqYVGuZmz2pZX g3YXqPLqHK++29qmF9Nns9hAfhYIVWoGf6OfHHuRecMfzzqTTC/YXA0q4 gefPLShTnwQjVP88WCcbjE8oKoGskmlMPP/5JdbQBQit5xeVm6W92nedh pfMKE9jMH07w5UsDb9FLIIs800TfC1f9gxfelivAyC/cyXlJTDP5E8O2N Nsh5ctFyaFdYkIebXhQllQm6bKq4rFiilPmHBU4Wh91KPKRJVHn6W9nqq 5qp/tp2oqp7tiPgI6gB+wz28N99xeXrOG+Y30z5pX3EAiKcY045UkoZrQ A==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="386675992" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="386675992" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:20 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086727" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:19 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov Subject: [PATCH v5 12/30] KVM: Add arch hook for suspend Date: Thu, 22 Sep 2022 11:20:41 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Isaku Yamahata Factor out the logic on suspend event as arch callback. Later kvm/x86 will override it. Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 084ee8a13e9f..861aad8812ff 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1435,6 +1435,7 @@ static inline void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu) {} #endif int kvm_arch_reboot(int val); +int kvm_arch_suspend(int usage_count); int kvm_arch_hardware_enable(void); void kvm_arch_hardware_disable(void); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 58385000b73f..0ebe43a695e5 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1148,6 +1148,17 @@ int __weak kvm_arch_reboot(int val) return NOTIFY_OK; } +int __weak kvm_arch_suspend(int usage_count) +{ + if (usage_count) + /* + * Because kvm_suspend() is called with interrupt disabled, no + * need to disable preemption. + */ + hardware_disable_nolock(NULL); + return 0; +} + /* * Called just after removing the VM from the vm_list, but before doing any * other destruction. @@ -5753,15 +5764,7 @@ static int kvm_suspend(void) * locking. */ lockdep_assert_not_held(&kvm_lock); - - if (kvm_usage_count) { - /* - * Because kvm_suspend() is called with interrupt disabled, no - * need to disable preemption. - */ - hardware_disable_nolock(NULL); - } - return 0; + return kvm_arch_suspend(kvm_usage_count); } static void kvm_resume(void) From patchwork Thu Sep 22 18:20:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985658 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 246ECECAAD8 for ; Thu, 22 Sep 2022 18:22:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232482AbiIVSV5 (ORCPT ); Thu, 22 Sep 2022 14:21:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232146AbiIVSVd (ORCPT ); Thu, 22 Sep 2022 14:21:33 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7661C10AB39; Thu, 22 Sep 2022 11:21:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663870889; x=1695406889; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=rkbhPAbj7cDgUM/73qRc/G+VqqyGPWDg7DKyVgG5eCc=; b=hmAIprvsgIthS4/HtgAkW6ZY3TH91+54z2/8L5jUDGwijBBR51M+Qkcx Lkas0rDegI551cCCIc+A9emNqWz1f/LLQ4xaXp378ft0Uj/3OvIcW0KH9 ehgrPtJ+3lZuAjuHDEL+Zk+N4+t0gVNhe5DWqTZkqvxSPQDjoXe3KSSBf aIa7ukKhAmBlXr0BBj7Bu/jsB9YDKt/0Wuz24m+nmQseEhn8I9QdSrrTw H2pBX3Qc2cij/+uGQyg07WiGw96yz1xUyXkFjJXUC5LwXIeNXQinuoWHy 5r6eNRLWcO3dtA6aACyByjzdMmRjhvpco6s/dRKTPmyesjhSTnyJ1ZF0M w==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="386675995" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="386675995" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:20 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086731" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:19 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov Subject: [PATCH v5 13/30] KVM: Add arch hook for resume event Date: Thu, 22 Sep 2022 11:20:42 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Isaku Yamahata Factor out the logic on resume event as arch callback. Later kvm/x86 will override it. Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 861aad8812ff..1adbf74e3047 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1436,6 +1436,7 @@ static inline void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu) {} int kvm_arch_reboot(int val); int kvm_arch_suspend(int usage_count); +void kvm_arch_resume(int usage_count); int kvm_arch_hardware_enable(void); void kvm_arch_hardware_disable(void); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 0ebe43a695e5..1270f88c2a1e 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1159,6 +1159,12 @@ int __weak kvm_arch_suspend(int usage_count) return 0; } +void __weak kvm_arch_resume(int usage_count) +{ + if (usage_count) + hardware_enable_nolock(NULL); +} + /* * Called just after removing the VM from the vm_list, but before doing any * other destruction. @@ -5769,10 +5775,8 @@ static int kvm_suspend(void) static void kvm_resume(void) { - if (kvm_usage_count) { - lockdep_assert_not_held(&kvm_lock); - hardware_enable_nolock(NULL); - } + lockdep_assert_not_held(&kvm_lock); + kvm_arch_resume(kvm_usage_count); } static struct syscore_ops kvm_syscore_ops = { From patchwork Thu Sep 22 18:20:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985659 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 77C2DECAAD8 for ; Thu, 22 Sep 2022 18:22:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232561AbiIVSWo (ORCPT ); Thu, 22 Sep 2022 14:22:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232468AbiIVSVu (ORCPT ); Thu, 22 Sep 2022 14:21:50 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1B4E10B5B6; Thu, 22 Sep 2022 11:21:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663870900; x=1695406900; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9rqrAWK9z6uKisn3U+yyVK3Y97E5qWOYWWtQX0dgrOg=; b=hw/MK0eeUvQm6vun7I72QfsNU1h0EY1a5ZMsFWZtL5w/DKNkxuCG3Qxi nQ0pWqDjT9E+e80T8Ti+z1UKoPmBL96ie6QBKC8NiHq6rqdWwQye2eVU4 oqLqwXu4aGcAt4LD3lIhGzYHbCP5hohI7IKqJJQdcMVFF/cUCVP5DlX91 J2p/9Bos9qO+orKjsP9ikPwa2E/df6pVd4G/oM4vRjPoJluo33t/dKiEv KdHepnKvnx0lRLqgKjQust8HX7gweEvSjR5auyRQbOhcjUtGuMdDVkiII 5t8fpu4gAxvd3NvdW00Ay7uri90hnQ2ZEMuJ0nK4hVA1qAF7CovJEUAYl w==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="326712884" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="326712884" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:21 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086737" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:20 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov Subject: [PATCH v5 14/30] KVM: Add arch hook for cpu online event Date: Thu, 22 Sep 2022 11:20:43 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Isaku Yamahata Factor out the logic on cpu hotplug event as arch callback. Later kvm/x86 will override it. Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 58 +++++++++++++++++++++++----------------- 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 1adbf74e3047..013b33a19030 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1434,6 +1434,7 @@ void kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu, struct dentry *debugfs_ static inline void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu) {} #endif +int kvm_arch_online_cpu(unsigned int cpu, int usage_count); int kvm_arch_reboot(int val); int kvm_arch_suspend(int usage_count); void kvm_arch_resume(int usage_count); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 1270f88c2a1e..4c535ae412a7 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1142,6 +1142,38 @@ int __weak kvm_arch_del_vm(int usage_count) return 0; } +int __weak kvm_arch_online_cpu(unsigned int cpu, int usage_count) +{ + int ret = 0; + + ret = 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(NULL); + preempt_enable(); + if (atomic_read(&hardware_enable_failed)) { + atomic_set(&hardware_enable_failed, 0); + ret = -EIO; + } + } + return ret; +} + int __weak kvm_arch_reboot(int val) { on_each_cpu(hardware_disable_nolock, NULL, 1); @@ -5085,32 +5117,8 @@ static int kvm_online_cpu(unsigned int cpu) { int ret; - ret = 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(NULL); - preempt_enable(); - if (atomic_read(&hardware_enable_failed)) { - atomic_set(&hardware_enable_failed, 0); - ret = -EIO; - } - } + ret = kvm_arch_online_cpu(cpu, kvm_usage_count); mutex_unlock(&kvm_lock); return ret; } From patchwork Thu Sep 22 18:20:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985661 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 16B4DC6FA82 for ; Thu, 22 Sep 2022 18:22:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232202AbiIVSW4 (ORCPT ); Thu, 22 Sep 2022 14:22:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231882AbiIVSWh (ORCPT ); Thu, 22 Sep 2022 14:22:37 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 19F6750508; Thu, 22 Sep 2022 11:21:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663870902; x=1695406902; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=HGmt/VHs6fVj/w1+ZNhXhm615xxkOdQNshHUklQnVBE=; b=BEk08WiFn0DwUveX1QNOcGIDNSjKzD5Np5v1eAbTaIBP/r9gkelxUxnT VzCJP2Z9Fwlf/+3DLQ5ODOUx3YINmhrsa4tg9g9Rb+JyoDrxj9rnHV7Jm nxJ3ndQvfDNsBTdB7r82xyrjRhtnHb+3109cvd80YllZq05UKVb+ST4fa Kq5syzpRpt6iJfNp/lX6YsGIIEXbUSqWAvDd2rJ00qvljcG8Mv0+xtaY0 GSk3i7XscB1jg4Bya+RI42BnKdWdeDCHWXirR5a2TUJU4hSlEBPG24wBv tbfEoiLQJUwJMf+NM/zErtv2iZcF2A8Tzl/FkX9Tewo3hyXkc24qGOupk A==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="326712886" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="326712886" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:21 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086745" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:21 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov Subject: [PATCH v5 15/30] KVM: Add arch hook for cpu offline event Date: Thu, 22 Sep 2022 11:20:44 -0700 Message-Id: <1bed181569380dfa3c7bc5c6b3f0acc1de01217e.1663869838.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Isaku Yamahata Factor out the logic on cpu offline event as arch callback. Later kvm/x86 will override it. Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 30 +++++++++++++++++++----------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 013b33a19030..f3a79d55ca8c 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1435,6 +1435,7 @@ static inline void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu) {} #endif int kvm_arch_online_cpu(unsigned int cpu, int usage_count); +int kvm_arch_offline_cpu(unsigned int cpu, int usage_count); int kvm_arch_reboot(int val); int kvm_arch_suspend(int usage_count); void kvm_arch_resume(int usage_count); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 4c535ae412a7..ad9b8b7d21fa 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1174,6 +1174,21 @@ int __weak kvm_arch_online_cpu(unsigned int cpu, int usage_count) return ret; } +int __weak kvm_arch_offline_cpu(unsigned int cpu, int usage_count) +{ + if (usage_count) { + /* + * arch callback kvm_arch_hardware_disable() assumes that + * preemption is disabled for historical reason. Disable + * preemption until all arch callbacks are fixed. + */ + preempt_disable(); + hardware_disable_nolock(NULL); + preempt_enable(); + } + return 0; +} + int __weak kvm_arch_reboot(int val) { on_each_cpu(hardware_disable_nolock, NULL, 1); @@ -5137,19 +5152,12 @@ static void hardware_disable_nolock(void *junk) static int kvm_offline_cpu(unsigned int cpu) { + int ret; + mutex_lock(&kvm_lock); - if (kvm_usage_count) { - /* - * arch callback kvm_arch_hardware_disable() assumes that - * preemption is disabled for historical reason. Disable - * preemption until all arch callbacks are fixed. - */ - preempt_disable(); - hardware_disable_nolock(NULL); - preempt_enable(); - } + ret = kvm_arch_offline_cpu(cpu, kvm_usage_count); mutex_unlock(&kvm_lock); - return 0; + return ret; } static void kvm_del_vm(void) From patchwork Thu Sep 22 18:20:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985660 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 83A2DC6FA82 for ; Thu, 22 Sep 2022 18:22:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232572AbiIVSWz (ORCPT ); Thu, 22 Sep 2022 14:22:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34952 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232439AbiIVSWg (ORCPT ); Thu, 22 Sep 2022 14:22:36 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 57E6D5051C; Thu, 22 Sep 2022 11:21:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663870903; x=1695406903; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=zLkocjxLuN7GyCmVtTNHRtyAuGH+UIMI58rO1Z93uSw=; b=d2gYifdrcDkB7Lzb/7WPNWC/uHXlfNy0MXn6q/tXa0qvQM4iImTnoDAk MwPmsqqHwCeqzgWbofsxvtp97Ub5Urrtw7t/Uf9BnKZSya8NQCH5Mxeu+ /7p7yUDbEJ1tuVEv7ErjeXEQmIYJZKE/5Pho7NU1EDsWrm7lM+dGcKlTj 7I6Co2Hydr3PzfdGxMdxDJaZCMABuxPe/m8MRRUMbGpnCJItdbftYZ66Q qL0jAUEGhfAavx2rso4nGNG/gYRCcJKUjQqRsyBsQqdQR4oHkLZq9RoAY n3sRwGgJHdjR+vx9j0/TU9JiWo62xmGlY0AXO6ovhjyjS2uX5dkVyM8Jp Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="326712889" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="326712889" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:22 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086754" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:21 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov Subject: [PATCH v5 16/30] KVM: Remove on_each_cpu(hardware_disable_nolock) in kvm_exit() Date: Thu, 22 Sep 2022 11:20:45 -0700 Message-Id: <54c7065fa08a65f1fcd7f47492f1a83e6f7a3746.1663869838.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Isaku Yamahata hardware_enable/disable_nolock() check if the hardware is already enabled/disabled and work as nop when they are called multiple times. When VM is created/destroyed, on_each_cpu(hardware_enable/disable_nolock) via kvm_arch_add/del_vm() and module_get/put() are called. It means when kvm module is removed, it's guaranteed that there is no vm and that hardware_disable_nolock() was called on each cpus. Although the module exit function, kvm_exit(), calls on_each_cpu(hardware_disable_nolock), it's essentially nop. Eliminate nop call in kvm_exit(). Signed-off-by: Isaku Yamahata --- virt/kvm/kvm_main.c | 1 - 1 file changed, 1 deletion(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index ad9b8b7d21fa..d7c3bc14691f 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -6034,7 +6034,6 @@ void kvm_exit(void) unregister_syscore_ops(&kvm_syscore_ops); unregister_reboot_notifier(&kvm_reboot_notifier); cpuhp_remove_state_nocalls(CPUHP_AP_KVM_ONLINE); - on_each_cpu(hardware_disable_nolock, NULL, 1); kvm_arch_hardware_unsetup(); kvm_arch_exit(); kvm_irqfd_exit(); From patchwork Thu Sep 22 18:20:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985662 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 A58C2ECAAD8 for ; Thu, 22 Sep 2022 18:23:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232591AbiIVSXO (ORCPT ); Thu, 22 Sep 2022 14:23:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35074 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232456AbiIVSWk (ORCPT ); Thu, 22 Sep 2022 14:22:40 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 47D2610AB20; Thu, 22 Sep 2022 11:21:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663870909; x=1695406909; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=fanfknRHqXqNCiNeSiwe63GR4M0QgQwb/iuuXUqHmiw=; b=d6kBaUFeyAW4iSTcSUUwUJ9JCh3kXvyKzQQKn9GW2oJYxUjCm/0gzmw0 j//VdLCnPuQZg1VPQ0ThaVv4U7KRVJJDlXEbONjgLlUCkmobWP6d2l0Xx hnGOSPKNHCpa2ZsNhca01JFPOurLx8tiJ6EpWWMuoBmTyzsiCt7TZVq5B kLqdASF/NYqrCV8c8as1lLvkub4KycurYFua6x66El+YKZea8y6KuBIUA zACtvXjpaOJZ9VUVLZnXoEHMi+rvBd+IY1Q+sjtm78sKag9r1t28O7vSB wbbNqXpchKdBQaf8F7PN1Zj2+gFzQGPlUPXHIxzp5nV1lJnxwZjT7vlLB g==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="326712896" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="326712896" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:22 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086765" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:22 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov Subject: [PATCH v5 17/30] KVM: Move out KVM arch PM hooks and hardware enable/disable logic Date: Thu, 22 Sep 2022 11:20:46 -0700 Message-Id: <245445b7326fb86cdae11ad2709d5f3dac3c9e53.1663869838.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Isaku Yamahata To make clear that those files are default implementation and that KVM/x86 (and other KVM arch in future) will override them, split out those into a dedicated file. Once conversions for all kvm archs are done, the file will be deleted. Signed-off-by: Isaku Yamahata --- virt/kvm/Makefile.kvm | 2 +- virt/kvm/kvm_arch.c | 164 ++++++++++++++++++++++++++++++++++++++++++ virt/kvm/kvm_main.c | 160 ----------------------------------------- 3 files changed, 165 insertions(+), 161 deletions(-) create mode 100644 virt/kvm/kvm_arch.c diff --git a/virt/kvm/Makefile.kvm b/virt/kvm/Makefile.kvm index 2c27d5d0c367..428b09b3f80a 100644 --- a/virt/kvm/Makefile.kvm +++ b/virt/kvm/Makefile.kvm @@ -5,7 +5,7 @@ KVM ?= ../../../virt/kvm -kvm-y := $(KVM)/kvm_main.o $(KVM)/eventfd.o $(KVM)/binary_stats.o +kvm-y := $(KVM)/kvm_main.o $(KVM)/eventfd.o $(KVM)/binary_stats.o $(KVM)/kvm_arch.o kvm-$(CONFIG_KVM_VFIO) += $(KVM)/vfio.o kvm-$(CONFIG_KVM_MMIO) += $(KVM)/coalesced_mmio.o kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c new file mode 100644 index 000000000000..bcf8b74144e3 --- /dev/null +++ b/virt/kvm/kvm_arch.c @@ -0,0 +1,164 @@ +// 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 + * + * + * TODO: Delete this file once the conversion of all KVM arch is done. + */ + +#include + +static cpumask_t cpus_hardware_enabled = CPU_MASK_NONE; +static atomic_t hardware_enable_failed; + +/* + * Called after the VM is otherwise initialized, but just before adding it to + * the vm_list. + */ +int __weak kvm_arch_post_init_vm(struct kvm *kvm) +{ + return 0; +} + +static void hardware_enable_nolock(void *junk) +{ + int cpu = raw_smp_processor_id(); + int r; + + WARN_ON_ONCE(preemptible()); + + if (cpumask_test_cpu(cpu, &cpus_hardware_enabled)) + return; + + cpumask_set_cpu(cpu, &cpus_hardware_enabled); + + r = 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 %pSb\n", + cpu, __builtin_return_address(0)); + } +} + +static void hardware_disable_nolock(void *junk) +{ + int cpu = raw_smp_processor_id(); + + WARN_ON_ONCE(preemptible()); + + if (!cpumask_test_cpu(cpu, &cpus_hardware_enabled)) + return; + cpumask_clear_cpu(cpu, &cpus_hardware_enabled); + kvm_arch_hardware_disable(); +} + +/* + * Called after the VM is otherwise initialized, but just before adding it to + * the vm_list. + */ +int __weak kvm_arch_add_vm(struct kvm *kvm, int usage_count) +{ + int r = 0; + + if (usage_count != 1) + return 0; + + atomic_set(&hardware_enable_failed, 0); + on_each_cpu(hardware_enable_nolock, NULL, 1); + + if (atomic_read(&hardware_enable_failed)) { + r = -EBUSY; + goto err; + } + + r = kvm_arch_post_init_vm(kvm); +err: + if (r) + on_each_cpu(hardware_disable_nolock, NULL, 1); + return r; +} + +int __weak kvm_arch_del_vm(int usage_count) +{ + if (usage_count) + return 0; + + on_each_cpu(hardware_disable_nolock, NULL, 1); + return 0; +} + +int __weak kvm_arch_online_cpu(unsigned int cpu, int usage_count) +{ + int ret = 0; + + ret = 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(NULL); + preempt_enable(); + if (atomic_read(&hardware_enable_failed)) { + atomic_set(&hardware_enable_failed, 0); + ret = -EIO; + } + } + return ret; +} + +int __weak kvm_arch_offline_cpu(unsigned int cpu, int usage_count) +{ + if (usage_count) { + /* + * arch callback kvm_arch_hardware_disable() assumes that + * preemption is disabled for historical reason. Disable + * preemption until all arch callbacks are fixed. + */ + preempt_disable(); + hardware_disable_nolock(NULL); + preempt_enable(); + } + return 0; +} + +int __weak kvm_arch_reboot(int val) +{ + on_each_cpu(hardware_disable_nolock, NULL, 1); + return NOTIFY_OK; +} + +int __weak kvm_arch_suspend(int usage_count) +{ + if (usage_count) + /* + * Because kvm_suspend() is called with interrupt disabled, no + * need to disable preemption. + */ + hardware_disable_nolock(NULL); + return 0; +} + +void __weak kvm_arch_resume(int usage_count) +{ + if (usage_count) + hardware_enable_nolock(NULL); +} diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index d7c3bc14691f..b1a09d2d5982 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); -static cpumask_var_t cpus_hardware_enabled; static int kvm_usage_count; -static atomic_t hardware_enable_failed; static struct kmem_cache *kvm_vcpu_cache; @@ -142,8 +140,6 @@ static int kvm_no_compat_open(struct inode *inode, struct file *file) #define KVM_COMPAT(c) .compat_ioctl = kvm_no_compat_ioctl, \ .open = kvm_no_compat_open #endif -static void hardware_enable_nolock(void *junk); -static void hardware_disable_nolock(void *junk); static void kvm_del_vm(void); static void kvm_io_bus_destroy(struct kvm_io_bus *bus); @@ -1098,120 +1094,6 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, const char *fdname) return ret; } -/* - * 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 after the VM is otherwise initialized, but just before adding it to - * the vm_list. - */ -int __weak kvm_arch_add_vm(struct kvm *kvm, int usage_count) -{ - int r = 0; - - if (usage_count != 1) - return 0; - - atomic_set(&hardware_enable_failed, 0); - on_each_cpu(hardware_enable_nolock, NULL, 1); - - if (atomic_read(&hardware_enable_failed)) { - r = -EBUSY; - goto err; - } - - r = kvm_arch_post_init_vm(kvm); -err: - if (r) - on_each_cpu(hardware_disable_nolock, NULL, 1); - return r; -} - -int __weak kvm_arch_del_vm(int usage_count) -{ - if (usage_count) - return 0; - - on_each_cpu(hardware_disable_nolock, NULL, 1); - return 0; -} - -int __weak kvm_arch_online_cpu(unsigned int cpu, int usage_count) -{ - int ret = 0; - - ret = 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(NULL); - preempt_enable(); - if (atomic_read(&hardware_enable_failed)) { - atomic_set(&hardware_enable_failed, 0); - ret = -EIO; - } - } - return ret; -} - -int __weak kvm_arch_offline_cpu(unsigned int cpu, int usage_count) -{ - if (usage_count) { - /* - * arch callback kvm_arch_hardware_disable() assumes that - * preemption is disabled for historical reason. Disable - * preemption until all arch callbacks are fixed. - */ - preempt_disable(); - hardware_disable_nolock(NULL); - preempt_enable(); - } - return 0; -} - -int __weak kvm_arch_reboot(int val) -{ - on_each_cpu(hardware_disable_nolock, NULL, 1); - return NOTIFY_OK; -} - -int __weak kvm_arch_suspend(int usage_count) -{ - if (usage_count) - /* - * Because kvm_suspend() is called with interrupt disabled, no - * need to disable preemption. - */ - hardware_disable_nolock(NULL); - return 0; -} - -void __weak kvm_arch_resume(int usage_count) -{ - if (usage_count) - hardware_enable_nolock(NULL); -} - /* * Called just after removing the VM from the vm_list, but before doing any * other destruction. @@ -5106,28 +4988,6 @@ static struct miscdevice kvm_dev = { &kvm_chardev_ops, }; -static void hardware_enable_nolock(void *junk) -{ - int cpu = raw_smp_processor_id(); - int r; - - WARN_ON_ONCE(preemptible()); - - if (cpumask_test_cpu(cpu, cpus_hardware_enabled)) - return; - - cpumask_set_cpu(cpu, cpus_hardware_enabled); - - r = 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 %pSb\n", - cpu, __builtin_return_address(0)); - } -} - static int kvm_online_cpu(unsigned int cpu) { int ret; @@ -5138,18 +4998,6 @@ static int kvm_online_cpu(unsigned int cpu) return ret; } -static void hardware_disable_nolock(void *junk) -{ - int cpu = raw_smp_processor_id(); - - WARN_ON_ONCE(preemptible()); - - if (!cpumask_test_cpu(cpu, cpus_hardware_enabled)) - return; - cpumask_clear_cpu(cpu, cpus_hardware_enabled); - kvm_arch_hardware_disable(); -} - static int kvm_offline_cpu(unsigned int cpu) { int ret; @@ -5930,11 +5778,6 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, if (r) goto out_irqfd; - if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) { - r = -ENOMEM; - goto out_free_0; - } - r = kvm_arch_hardware_setup(opaque); if (r < 0) goto out_free_1; @@ -6011,8 +5854,6 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned 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(); @@ -6037,7 +5878,6 @@ void kvm_exit(void) kvm_arch_hardware_unsetup(); kvm_arch_exit(); kvm_irqfd_exit(); - free_cpumask_var(cpus_hardware_enabled); kvm_vfio_ops_exit(); } EXPORT_SYMBOL_GPL(kvm_exit); From patchwork Thu Sep 22 18:20:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985663 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 1FFBBECAAD8 for ; Thu, 22 Sep 2022 18:23:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232474AbiIVSXR (ORCPT ); Thu, 22 Sep 2022 14:23:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232557AbiIVSWn (ORCPT ); Thu, 22 Sep 2022 14:22:43 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E54B10B5AB; Thu, 22 Sep 2022 11:21:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663870917; x=1695406917; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=L8+RR+UhC+idmPkn74NT4bHxkU564YnpQS2qQj/GxDE=; b=LOkl5siZfVR9UJpxPiSSPgnqEbyFtWEv3T+irwy9q/BfXbyNYMTNz+s8 2dg7KjbA0WbmYBcSu2ikU5+CmaZUtxM1znIjOLOh2Bwa+9ceFVZtKTu68 6pkaQUn/3WJ5JBywl2A6WFFsxYww4EK9Znb9iVUpS1xlz27CVy9dXQNN4 xG2l4YDxli1NNcTF/emxIGep8lnEaKkzopeyFlwI3ylRyYmIidpZIR+dd n1tyz38K7YPgB5bU8TzJE6Wt86+suZaRCYZVr+A2ZljlSP3xDRHOCA2cv Mp4ugpy0+Pwy64rjGQ7jUeEnAj7ZCjaZj4h7leq4zMOW34rEYYt4mGUsg Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="326712900" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="326712900" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:23 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086775" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:22 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov Subject: [PATCH v5 18/30] KVM: kvm_arch.c: Remove _nolock post fix Date: Thu, 22 Sep 2022 11:20:47 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 Reviewed-by: Chao Gao --- virt/kvm/kvm_arch.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c index bcf8b74144e3..e6bf9de18cba 100644 --- a/virt/kvm/kvm_arch.c +++ b/virt/kvm/kvm_arch.c @@ -24,7 +24,7 @@ int __weak kvm_arch_post_init_vm(struct kvm *kvm) return 0; } -static void hardware_enable_nolock(void *junk) +static void hardware_enable(void *junk) { int cpu = raw_smp_processor_id(); int r; @@ -46,7 +46,7 @@ static void hardware_enable_nolock(void *junk) } } -static void hardware_disable_nolock(void *junk) +static void hardware_disable(void *junk) { int cpu = raw_smp_processor_id(); @@ -70,7 +70,7 @@ int __weak kvm_arch_add_vm(struct kvm *kvm, int usage_count) return 0; atomic_set(&hardware_enable_failed, 0); - on_each_cpu(hardware_enable_nolock, NULL, 1); + on_each_cpu(hardware_enable, NULL, 1); if (atomic_read(&hardware_enable_failed)) { r = -EBUSY; @@ -80,7 +80,7 @@ int __weak kvm_arch_add_vm(struct kvm *kvm, int usage_count) r = kvm_arch_post_init_vm(kvm); err: if (r) - on_each_cpu(hardware_disable_nolock, NULL, 1); + on_each_cpu(hardware_disable, NULL, 1); return r; } @@ -89,7 +89,7 @@ int __weak kvm_arch_del_vm(int usage_count) if (usage_count) return 0; - on_each_cpu(hardware_disable_nolock, NULL, 1); + on_each_cpu(hardware_disable, NULL, 1); return 0; } @@ -115,7 +115,7 @@ int __weak kvm_arch_online_cpu(unsigned int cpu, int usage_count) * preemption until all arch callbacks are fixed. */ preempt_disable(); - hardware_enable_nolock(NULL); + hardware_enable(NULL); preempt_enable(); if (atomic_read(&hardware_enable_failed)) { atomic_set(&hardware_enable_failed, 0); @@ -134,7 +134,7 @@ int __weak kvm_arch_offline_cpu(unsigned int cpu, int usage_count) * preemption until all arch callbacks are fixed. */ preempt_disable(); - hardware_disable_nolock(NULL); + hardware_disable(NULL); preempt_enable(); } return 0; @@ -142,7 +142,7 @@ int __weak kvm_arch_offline_cpu(unsigned int cpu, int usage_count) int __weak kvm_arch_reboot(int val) { - on_each_cpu(hardware_disable_nolock, NULL, 1); + on_each_cpu(hardware_disable, NULL, 1); return NOTIFY_OK; } @@ -153,12 +153,12 @@ int __weak kvm_arch_suspend(int usage_count) * Because kvm_suspend() is called with interrupt disabled, no * need to disable preemption. */ - hardware_disable_nolock(NULL); + hardware_disable(NULL); return 0; } void __weak kvm_arch_resume(int usage_count) { if (usage_count) - hardware_enable_nolock(NULL); + hardware_enable(NULL); } From patchwork Thu Sep 22 18:20:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985664 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 77B73C6FA82 for ; Thu, 22 Sep 2022 18:23:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232570AbiIVSX1 (ORCPT ); Thu, 22 Sep 2022 14:23:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36316 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232511AbiIVSWq (ORCPT ); Thu, 22 Sep 2022 14:22:46 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B0DC109608; Thu, 22 Sep 2022 11:21:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663870919; x=1695406919; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=e6y3hm3ayzpDm9GqLzCdSOZ8vwYZDKw7HR+mXt4ONKk=; b=JdKoPH4gn0x+Waseq6+mHA1t/BQ16eTmtMy4bIVsc5n/JtLY+aLsOcTp zqBSs3VO8cZ2dSfjpzDVUqX1nos3wT6v/ojmoSMBvP+M1+08NmDJiiXBx OeHoy82E8f2AdNVmrJ89z0+GZddsvpapUuA5zmglfBHCOR5I7gI3G2bTB pwHehQZEmbkXtCyUBRk1zOAhLsFOUcMlwQvaVN0vU0s4ZWjCHqR+YWrVa 7C8apRa7hckKxux1J+emhLQoI8usm7WeAQBk3l5ASTBQJ45vQMhpQO75D PPQbMCIksIhDxei+bw4F8KQLel3SmewH6GL6bdyw8GJ7nr12EkV8mUkff w==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="326712904" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="326712904" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:23 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086790" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:23 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov Subject: [PATCH v5 19/30] KVM: kvm_arch.c: Remove a global variable, hardware_enable_failed Date: Thu, 22 Sep 2022 11:20:48 -0700 Message-Id: <7f85a41f7aaad91c062eef37669c1eda2632ba45.1663869838.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 | 61 ++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c index e6bf9de18cba..10fd29bdd6e1 100644 --- a/virt/kvm/kvm_arch.c +++ b/virt/kvm/kvm_arch.c @@ -13,7 +13,6 @@ #include static cpumask_t cpus_hardware_enabled = CPU_MASK_NONE; -static atomic_t hardware_enable_failed; /* * Called after the VM is otherwise initialized, but just before adding it to @@ -24,7 +23,7 @@ int __weak kvm_arch_post_init_vm(struct kvm *kvm) return 0; } -static void hardware_enable(void *junk) +static int __hardware_enable(void) { int cpu = raw_smp_processor_id(); int r; @@ -32,18 +31,22 @@ static void hardware_enable(void *junk) WARN_ON_ONCE(preemptible()); if (cpumask_test_cpu(cpu, &cpus_hardware_enabled)) - return; - - cpumask_set_cpu(cpu, &cpus_hardware_enabled); - + return 0; r = kvm_arch_hardware_enable(); - - if (r) { - cpumask_clear_cpu(cpu, &cpus_hardware_enabled); - atomic_inc(&hardware_enable_failed); + if (r) pr_warn("kvm: enabling virtualization on CPU%d failed during %pSb\n", cpu, __builtin_return_address(0)); - } + else + cpumask_set_cpu(cpu, &cpus_hardware_enabled); + return r; +} + +static void hardware_enable(void *arg) +{ + atomic_t *failed = arg; + + if (__hardware_enable()) + atomic_inc(failed); } static void hardware_disable(void *junk) @@ -64,15 +67,15 @@ static void hardware_disable(void *junk) */ int __weak kvm_arch_add_vm(struct kvm *kvm, int usage_count) { + atomic_t failed = ATOMIC_INIT(0); int r = 0; if (usage_count != 1) return 0; - atomic_set(&hardware_enable_failed, 0); - on_each_cpu(hardware_enable, NULL, 1); + on_each_cpu(hardware_enable, &failed, 1); - if (atomic_read(&hardware_enable_failed)) { + if (atomic_read(&failed)) { r = -EBUSY; goto err; } @@ -95,33 +98,29 @@ int __weak kvm_arch_del_vm(int usage_count) int __weak kvm_arch_online_cpu(unsigned int cpu, int usage_count) { - int ret = 0; + int ret; ret = kvm_arch_check_processor_compat(); if (ret) return ret; + if (!usage_count) + return 0; + + /* + * arch callback kvm_arch_hardware_enable() assumes that + * preemption is disabled for historical reason. Disable + * preemption until all arch callbacks are fixed. + */ + preempt_disable(); /* * Abort the CPU online process if hardware virtualization cannot * be enabled. Otherwise running VMs would encounter unrecoverable * errors when scheduled to this CPU. */ - if (usage_count) { - WARN_ON_ONCE(atomic_read(&hardware_enable_failed)); + ret = __hardware_enable(); + preempt_enable(); - /* - * arch callback kvm_arch_hardware_eanble() assumes that - * preemption is disabled for historical reason. Disable - * preemption until all arch callbacks are fixed. - */ - preempt_disable(); - hardware_enable(NULL); - preempt_enable(); - if (atomic_read(&hardware_enable_failed)) { - atomic_set(&hardware_enable_failed, 0); - ret = -EIO; - } - } return ret; } @@ -160,5 +159,5 @@ int __weak kvm_arch_suspend(int usage_count) void __weak kvm_arch_resume(int usage_count) { if (usage_count) - hardware_enable(NULL); + (void)__hardware_enable(); } From patchwork Thu Sep 22 18:20:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985665 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 C64B8ECAAD8 for ; Thu, 22 Sep 2022 18:23:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232449AbiIVSXv (ORCPT ); Thu, 22 Sep 2022 14:23:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34774 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232542AbiIVSW5 (ORCPT ); Thu, 22 Sep 2022 14:22:57 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2FEE8109621; Thu, 22 Sep 2022 11:22: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=1663870925; x=1695406925; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Voi8WuLjLcB8mdNn0W+tygmrtXDvZpUlJiE4WnwNggM=; b=H+ZPxMDCVZlKKgfK1EZaWIzETPbhPcrT5ueg46ntGoDOc7u6vyQ5zZdW itOP+Z4jk5T3ze6ZvAVFJejHwIF+z5TBW1iQW4onuX5AhPKwoyFxoJ84Z EE9PnErkPU8dyqnI9lGaSr1+Vt0lvd1aZKaRVJozcbexikTlc3uduuxG4 gjnvTRBQVFC5EXSuhZ4PQt5Wnkyh9f/hQktDRpEdD5Qejb4wMVom/Yo+R Xv+DHjNcwLShgZQBWs61D8o/omeWiNiwuP8iewaPGG91NMkMJpgkiZb7O 5TTGG8Rlib/Xyo0tIOfeYEHuyj1ePeu2PNDvH4jJvQaQlsVr1OBE1y1YX Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="326712908" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="326712908" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:24 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086797" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:24 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov Subject: [PATCH v5 20/30] KVM: Introduce an arch wrapper to check all processor compatibility Date: Thu, 22 Sep 2022 11:20:49 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Isaku Yamahata Introduce an arch wrapper to check all processor compatibility and define default implementation as weak symbol to keep the current logic. The hardware feature compatibility check is arch dependent, only x86 KVM does cpu feature check on all processors. It doesn't make much sense to enforce the current implementation to invoke check function on each processors. Introduce a arch callback, kvm_arch_check_processor_compat_all(), so that arch code can override it. Eventually feature check should be pushed down into arch callback, (kvm_arch_hardware_setup(), kvm_arch_online_cpu(), and kvm_arch_resume()), the two compatibility check, kvm_arch_check_processor_compat{,_all}(), will be eliminated. This is a transitional step for it. Signed-off-by: Isaku Yamahata Reviewed-by: Chao Gao --- 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 f3a79d55ca8c..2c5ca8741ca5 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1445,6 +1445,7 @@ void kvm_arch_hardware_disable(void); int kvm_arch_hardware_setup(void *opaque); void kvm_arch_hardware_unsetup(void); int kvm_arch_check_processor_compat(void); +int kvm_arch_check_processor_compat_all(void); int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu); bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu); int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu); diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c index 10fd29bdd6e1..87ee84c09634 100644 --- a/virt/kvm/kvm_arch.c +++ b/virt/kvm/kvm_arch.c @@ -96,6 +96,24 @@ int __weak kvm_arch_del_vm(int usage_count) return 0; } +static void check_processor_compat(void *rtn) +{ + *(int *)rtn = kvm_arch_check_processor_compat(); +} + +int __weak kvm_arch_check_processor_compat_all(void) +{ + int cpu; + int r; + + for_each_online_cpu(cpu) { + smp_call_function_single(cpu, check_processor_compat, &r, 1); + if (r < 0) + return r; + } + return 0; +} + int __weak kvm_arch_online_cpu(unsigned int cpu, int usage_count) { int ret; diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index b1a09d2d5982..d5f882fb9e0c 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -5752,11 +5752,6 @@ void kvm_unregister_perf_callbacks(void) } #endif -static void check_processor_compat(void *rtn) -{ - *(int *)rtn = kvm_arch_check_processor_compat(); -} - int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, struct module *module) { @@ -5782,11 +5777,9 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, if (r < 0) goto out_free_1; - for_each_online_cpu(cpu) { - smp_call_function_single(cpu, check_processor_compat, &r, 1); - if (r < 0) - goto out_free_2; - } + r = kvm_arch_check_processor_compat_all(); + if (r < 0) + goto out_free_2; r = cpuhp_setup_state_nocalls(CPUHP_AP_KVM_ONLINE, "kvm/cpu:online", kvm_online_cpu, kvm_offline_cpu); From patchwork Thu Sep 22 18:20:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985666 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 E3CE2C6FA8B for ; Thu, 22 Sep 2022 18:23:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232532AbiIVSXw (ORCPT ); Thu, 22 Sep 2022 14:23:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232332AbiIVSXK (ORCPT ); Thu, 22 Sep 2022 14:23:10 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 90A61109634; Thu, 22 Sep 2022 11:22: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=1663870927; x=1695406927; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=7L6JQ3duEiYpDJSnY7rr6Cjj65pecUDGDkupr2JKZ5Q=; b=bnroP5O3VWYDjUHYEaiKENsT0/qHbIKrkrwK9uvmWrzE4OlnK8SMxkbd 33DdxTqPUhCjzWVSfjOVDspqHDPLPUDz+NYl/AcsvJ6DGR5rMAjfYszeA A+FOeIaQOLaxq0FVHReXKh6VeUUSWvMbFI1XeAUqsW6gC8AxAvd0k2Yfb wngaCOPRcb6ylpGRQcrPuaAhlL8JpRSLySB1stb2oKbcN34fUNerf5XOs ULROU8Ms1OEJjjG/UAj8ryvf6gqxfrnLtdCCpoP0oJ8fHQeufB9G52tMx fI/JLABO8tWrhEAItF8v1K43mtoLDkh2A+jgOMOroOJrSs6sJEUTWuwpZ g==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="326712913" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="326712913" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:25 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086807" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:24 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov Subject: [PATCH v5 21/30] KVM: x86: Duplicate arch callbacks related to pm events and compat check Date: Thu, 22 Sep 2022 11:20:50 -0700 Message-Id: <242f31fa4382d28d5f3893d9148c79e976d94136.1663869838.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 | 166 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 161 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index f5ebb69996d5..b15eb59096b6 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11960,6 +11960,167 @@ void kvm_arch_hardware_disable(void) drop_user_return_notifiers(); } +static cpumask_t cpus_hardware_enabled = CPU_MASK_NONE; + +int kvm_arch_post_init_vm(struct kvm *kvm) +{ + return kvm_mmu_post_init_vm(kvm); +} + +static int __hardware_enable(void) +{ + int cpu = raw_smp_processor_id(); + int r; + + WARN_ON_ONCE(preemptible()); + + if (cpumask_test_cpu(cpu, &cpus_hardware_enabled)) + return 0; + r = kvm_arch_hardware_enable(); + if (r) + pr_warn("kvm: enabling virtualization on CPU%d failed during %pSb\n", + cpu, __builtin_return_address(0)); + else + cpumask_set_cpu(cpu, &cpus_hardware_enabled); + return r; +} + +static void hardware_enable(void *arg) +{ + atomic_t *failed = arg; + + if (__hardware_enable()) + atomic_inc(failed); +} + +static void hardware_disable(void *junk) +{ + int cpu = raw_smp_processor_id(); + + WARN_ON_ONCE(preemptible()); + + if (!cpumask_test_cpu(cpu, &cpus_hardware_enabled)) + return; + cpumask_clear_cpu(cpu, &cpus_hardware_enabled); + kvm_arch_hardware_disable(); +} + +/* + * Called after the VM is otherwise initialized, but just before adding it to + * the vm_list. + */ +int kvm_arch_add_vm(struct kvm *kvm, int usage_count) +{ + atomic_t failed; + int r = 0; + + if (usage_count != 1) + return 0; + + atomic_set(&failed, 0); + on_each_cpu(hardware_enable, &failed, 1); + + if (atomic_read(&failed)) { + r = -EBUSY; + goto err; + } + + r = kvm_arch_post_init_vm(kvm); +err: + if (r) + on_each_cpu(hardware_disable, NULL, 1); + return r; +} + +int kvm_arch_del_vm(int usage_count) +{ + if (usage_count) + return 0; + + on_each_cpu(hardware_disable, NULL, 1); + return 0; +} + +static void check_processor_compat(void *rtn) +{ + *(int *)rtn = kvm_arch_check_processor_compat(); +} + +int kvm_arch_check_processor_compat_all(void) +{ + int cpu; + int r; + + for_each_online_cpu(cpu) { + smp_call_function_single(cpu, check_processor_compat, &r, 1); + if (r < 0) + return r; + } + return 0; +} + +int kvm_arch_online_cpu(unsigned int cpu, int usage_count) +{ + int ret; + + ret = kvm_arch_check_processor_compat(); + if (ret) + return ret; + + if (!usage_count) + return 0; + + /* + * arch callback kvm_arch_hardware_eanble() assumes that + * preemption is disabled for historical reason. Disable + * preemption until all arch callbacks are fixed. + */ + preempt_disable(); + /* + * Abort the CPU online process if hardware virtualization cannot + * be enabled. Otherwise running VMs would encounter unrecoverable + * errors when scheduled to this CPU. + */ + ret = __hardware_enable(); + preempt_enable(); + + return ret; +} + +int kvm_arch_offline_cpu(unsigned int cpu, int usage_count) +{ + if (usage_count) { + /* + * arch callback kvm_arch_hardware_disable() assumes that + * preemption is disabled for historical reason. Disable + * preemption until all arch callbacks are fixed. + */ + preempt_disable(); + hardware_disable(NULL); + preempt_enable(); + } + return 0; +} + +int kvm_arch_reboot(int val) +{ + on_each_cpu(hardware_disable, NULL, 1); + return NOTIFY_OK; +} + +int kvm_arch_suspend(int usage_count) +{ + if (usage_count) + hardware_disable(NULL); + return 0; +} + +void kvm_arch_resume(int usage_count) +{ + if (usage_count) + (void)__hardware_enable(); +} + static inline void kvm_ops_update(struct kvm_x86_init_ops *ops) { memcpy(&kvm_x86_ops, ops->runtime_ops, sizeof(kvm_x86_ops)); @@ -12140,11 +12301,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) return ret; } -int kvm_arch_post_init_vm(struct kvm *kvm) -{ - return kvm_mmu_post_init_vm(kvm); -} - static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu) { vcpu_load(vcpu); From patchwork Thu Sep 22 18:20:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985668 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 60B60ECAAD8 for ; Thu, 22 Sep 2022 18:23:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232580AbiIVSXz (ORCPT ); Thu, 22 Sep 2022 14:23:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36586 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232568AbiIVSX0 (ORCPT ); Thu, 22 Sep 2022 14:23:26 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4EF1C10C79B; Thu, 22 Sep 2022 11:22:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663870939; x=1695406939; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=QeHsRbI/skAm2S/DEhUEl4+spTF4gixFEqyGdTM2YXs=; b=fLJleE3rXocP3njQo2mbZzCfiXaEnLrrsd16DIvIrGqcJPVYfJq069b+ hcnnNBVwWBeQcUB41mherMtwIbLUkwij1TwpVdA5rJUEkTjdUsemO9uXb QQ68iCHeWgaUd0v+r6/3i6T4f6oeDssLVRI4+Dn2IG5fdzeIuGTVg3JBJ UDTU9oocbYm+4/KOVThCOlvgkwiX6TA3PKEMPjuil+/Zuk5QKa89xaNrT ucy+MnCXEKrL+c3bggX9TWOxv5wLeDYRYtY726mph0CY4Cy34sDU5xocW hibDnuSofUMYQ1lndA7q1hPxb0WyPBSF4XTx25GsV+WZvVzCycg2tuWPd Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="326712916" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="326712916" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:25 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086819" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:25 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov Subject: [PATCH v5 22/30] KVM: x86: Move TSC fixup logic to KVM arch resume callback Date: Thu, 22 Sep 2022 11:20:51 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index b15eb59096b6..d49396bb6c2e 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11865,18 +11865,26 @@ void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) EXPORT_SYMBOL_GPL(kvm_vcpu_deliver_sipi_vector); int kvm_arch_hardware_enable(void) +{ + return static_call(kvm_x86_hardware_enable)(); +} + +static int __hardware_enable(void); + +void kvm_arch_resume(int usage_count) { struct kvm *kvm; struct kvm_vcpu *vcpu; unsigned long i; - int ret; u64 local_tsc; u64 max_tsc = 0; bool stable, backwards_tsc = false; - ret = static_call(kvm_x86_hardware_enable)(); - if (ret != 0) - return ret; + if (!usage_count) + return; + + if (__hardware_enable()) + return; local_tsc = rdtsc(); stable = !kvm_check_tsc_unstable(); @@ -11951,7 +11959,6 @@ int kvm_arch_hardware_enable(void) } } - return 0; } void kvm_arch_hardware_disable(void) @@ -12115,12 +12122,6 @@ int kvm_arch_suspend(int usage_count) return 0; } -void kvm_arch_resume(int usage_count) -{ - if (usage_count) - (void)__hardware_enable(); -} - static inline void kvm_ops_update(struct kvm_x86_init_ops *ops) { memcpy(&kvm_x86_ops, ops->runtime_ops, sizeof(kvm_x86_ops)); From patchwork Thu Sep 22 18:20:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985667 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 2D145C54EE9 for ; Thu, 22 Sep 2022 18:23:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232558AbiIVSXy (ORCPT ); Thu, 22 Sep 2022 14:23:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35506 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229828AbiIVSXO (ORCPT ); Thu, 22 Sep 2022 14:23:14 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46E9110B585; Thu, 22 Sep 2022 11:22: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=1663870932; x=1695406932; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=JEMsGnsZUhYCBVZocJG3MEHANA4O9MZIjmnjwfXxTVU=; b=lGEJPom6f8fi4vbIJ1Wg5rt7OsCYAe28LCGD2C9fVI9v3tVIhjR/E43C WL/Yuh+fWYxOFK9/htJzHTHhjq45IT6+UxA02OjIoY/XLOk3Ne1g6KzY8 pShDio7eJi7tRhyu7KI+2PMbyBeCPo8uCJulSnV5u5nV2ed6FXKkdjycL 6KD/2bAfkrxKF31dEEFusi+7E70wJOIcPSqN90U0splM9ye/Ee4X+NIiM 76v8poTMI/MEDc/Cc+Y/HDhQ2YIKcEkF+GNrVQnqftCYv2iqgjs6JwyQW wnPQnW3nyE0fqgh//DHm24WsqstbYlOzwoafGGQW0Uf6oYTTvArWsJxoj w==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="326712925" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="326712925" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:26 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086838" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:25 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov Subject: [PATCH v5 23/30] KVM: Eliminate kvm_arch_post_init_vm() Date: Thu, 22 Sep 2022 11:20:52 -0700 Message-Id: <7ea322131782fdeb8df0122c5f4a3b75fcb360b2.1663869838.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 | 16 +--------------- 3 files changed, 2 insertions(+), 22 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index d49396bb6c2e..bf8d3b901725 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11969,11 +11969,6 @@ void kvm_arch_hardware_disable(void) static cpumask_t cpus_hardware_enabled = CPU_MASK_NONE; -int kvm_arch_post_init_vm(struct kvm *kvm) -{ - return kvm_mmu_post_init_vm(kvm); -} - static int __hardware_enable(void) { int cpu = raw_smp_processor_id(); @@ -12032,7 +12027,7 @@ int kvm_arch_add_vm(struct kvm *kvm, int usage_count) goto err; } - r = kvm_arch_post_init_vm(kvm); + r = kvm_mmu_post_init_vm(kvm); err: if (r) on_each_cpu(hardware_disable, NULL, 1); diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 2c5ca8741ca5..8dfa212b4543 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1451,7 +1451,6 @@ bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu); int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu); bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu); bool kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu); -int kvm_arch_post_init_vm(struct kvm *kvm); int kvm_arch_add_vm(struct kvm *kvm, int usage_count); int kvm_arch_del_vm(int usage_count); void kvm_arch_pre_destroy_vm(struct kvm *kvm); diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c index 87ee84c09634..2509c2777a49 100644 --- a/virt/kvm/kvm_arch.c +++ b/virt/kvm/kvm_arch.c @@ -14,15 +14,6 @@ static cpumask_t cpus_hardware_enabled = CPU_MASK_NONE; -/* - * Called after the VM is otherwise initialized, but just before adding it to - * the vm_list. - */ -int __weak kvm_arch_post_init_vm(struct kvm *kvm) -{ - return 0; -} - static int __hardware_enable(void) { int cpu = raw_smp_processor_id(); @@ -77,13 +68,8 @@ int __weak kvm_arch_add_vm(struct kvm *kvm, int usage_count) if (atomic_read(&failed)) { r = -EBUSY; - goto err; - } - - r = kvm_arch_post_init_vm(kvm); -err: - if (r) on_each_cpu(hardware_disable, NULL, 1); + } return r; } From patchwork Thu Sep 22 18:20:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985669 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 E8757C6FA82 for ; Thu, 22 Sep 2022 18:23:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229818AbiIVSX6 (ORCPT ); Thu, 22 Sep 2022 14:23:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36722 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232406AbiIVSX2 (ORCPT ); Thu, 22 Sep 2022 14:23:28 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4DCF210961A; Thu, 22 Sep 2022 11:22:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663870940; x=1695406940; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=AxxjbbyyR0194wjXyMbCislo3hv78tU3zlzwW8OtOcw=; b=kUq0gvFlRAo9I/sbIpcCYcdTr1HkGGRiwfNFt7TIIzGVrcFqKoNsVkrH hWhVq79O39cd+EjMUbVhrNZ1cfQUk0tHDk7KbUj1+eP8ep9MfuBaOP3Tp dcyJIXjjxwS4QbixtkEQ6CRmYT69ky3Q0/uC5exsW4Lr10CUcpL2+qOGz A5f10yItXNAjSv0h7y1T9/40i4USMzj6nliaX8vkIOTpDCKXYwbihnVB5 OZx02cQHbUcaGFRejncsD+fLBJAapyNu5F+RqbBQixwetqtBwOCoxss48 B/S5MX8WPGGuww4JtXMwNDFncn5nR85fs/zSRBBpjEH+gTQsfupaN6M// Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="326712929" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="326712929" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:26 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086860" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:26 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov Subject: [PATCH v5 24/30] KVM: Add config to not compile kvm_arch.c Date: Thu, 22 Sep 2022 11:20:53 -0700 Message-Id: <66e9475874595880b0951edf6ad17b6317992bed.1663869838.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 + arch/x86/kvm/x86.c | 4 ++-- include/linux/kvm_host.h | 3 +++ virt/kvm/Kconfig | 3 +++ virt/kvm/Makefile.kvm | 5 ++++- 5 files changed, 13 insertions(+), 3 deletions(-) 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/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index bf8d3b901725..01687b553915 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11864,7 +11864,7 @@ void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) } EXPORT_SYMBOL_GPL(kvm_vcpu_deliver_sipi_vector); -int kvm_arch_hardware_enable(void) +static int kvm_arch_hardware_enable(void) { return static_call(kvm_x86_hardware_enable)(); } @@ -11961,7 +11961,7 @@ void kvm_arch_resume(int usage_count) } } -void kvm_arch_hardware_disable(void) +static void kvm_arch_hardware_disable(void) { static_call(kvm_x86_hardware_disable)(); drop_user_return_notifiers(); diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 8dfa212b4543..7efc4792ff5f 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1440,8 +1440,11 @@ int kvm_arch_reboot(int val); int kvm_arch_suspend(int usage_count); void kvm_arch_resume(int usage_count); +#ifndef CONFIG_HAVE_KVM_OVERRIDE_HARDWARE_ENABLE int kvm_arch_hardware_enable(void); void kvm_arch_hardware_disable(void); +#endif + int kvm_arch_hardware_setup(void *opaque); void kvm_arch_hardware_unsetup(void); int kvm_arch_check_processor_compat(void); 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 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 428b09b3f80a..c0187ec4f83c 100644 --- a/virt/kvm/Makefile.kvm +++ b/virt/kvm/Makefile.kvm @@ -5,7 +5,10 @@ KVM ?= ../../../virt/kvm -kvm-y := $(KVM)/kvm_main.o $(KVM)/eventfd.o $(KVM)/binary_stats.o $(KVM)/kvm_arch.o +kvm-y := $(KVM)/kvm_main.o $(KVM)/eventfd.o $(KVM)/binary_stats.o +ifneq ($(CONFIG_HAVE_KVM_OVERRIDE_HARDWARE_ENABLE), y) +kvm-y += $(KVM)/kvm_arch.o +endif kvm-$(CONFIG_KVM_VFIO) += $(KVM)/vfio.o kvm-$(CONFIG_KVM_MMIO) += $(KVM)/coalesced_mmio.o kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o From patchwork Thu Sep 22 18:20:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985671 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 1381AC54EE9 for ; Thu, 22 Sep 2022 18:24:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232632AbiIVSY0 (ORCPT ); Thu, 22 Sep 2022 14:24:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232640AbiIVSXl (ORCPT ); Thu, 22 Sep 2022 14:23:41 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 23BD910B580; Thu, 22 Sep 2022 11:22:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663870948; x=1695406948; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=xexmELbi5aH40qDujxrSWqQj4dEkU4fMT/enlz5uNnQ=; b=AB/8BM+mTCFs9ly15QjUz3y1vPXNnekDwSW4FQoRJE+c0CYWDkDBB46D 9h2Lmus+IKCRCz/OEI+Pis4VVo21Ti6MQ4f8DosxKB6SieDmaH6P+A2T6 a1+d+t6BKUf6qKsnkL/cAllfwqgQ7VYA6GwkSeszWDO0Bs4kXSCXMZSOs l4U9b9vGPSaO0p9ucGXnJprnUdcUh61qdV7+ymMSpVtcbcN/LPuX1ogiX fqs4dx90zGx9hxaPcGY5Gu3aAo4Fssjr6TKz/X9SC+Ums4bANGE9i+kGN WG1hE9qkLH/mSUNcny62XqVyTBdWilcbfLt/NWXoi3YeCQuHgt3j010cG g==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="326712932" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="326712932" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:27 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086875" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:27 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov Subject: [PATCH v5 25/30] KVM: x86: Delete kvm_arch_hardware_enable/disable() Date: Thu, 22 Sep 2022 11:20:54 -0700 Message-Id: <23e34c8aba1372d6e7c054b737242f08a614078d.1663869838.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Isaku Yamahata Now they're function call and there is no point to keep them. Open code them. Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- arch/x86/kvm/x86.c | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 01687b553915..aa6594de1fc1 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_notifier *urn) /* * Disabling irqs at this point since the following code could be - * interrupted and executed through kvm_arch_hardware_disable() + * interrupted and executed through hardware_disable() */ local_irq_save(flags); if (msrs->registered) { @@ -11864,11 +11864,6 @@ void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) } EXPORT_SYMBOL_GPL(kvm_vcpu_deliver_sipi_vector); -static int kvm_arch_hardware_enable(void) -{ - return static_call(kvm_x86_hardware_enable)(); -} - static int __hardware_enable(void); void kvm_arch_resume(int usage_count) @@ -11961,12 +11956,6 @@ void kvm_arch_resume(int usage_count) } } -static void kvm_arch_hardware_disable(void) -{ - static_call(kvm_x86_hardware_disable)(); - drop_user_return_notifiers(); -} - static cpumask_t cpus_hardware_enabled = CPU_MASK_NONE; static int __hardware_enable(void) @@ -11978,7 +11967,7 @@ static int __hardware_enable(void) if (cpumask_test_cpu(cpu, &cpus_hardware_enabled)) return 0; - r = kvm_arch_hardware_enable(); + r = static_call(kvm_x86_hardware_enable)(); if (r) pr_warn("kvm: enabling virtualization on CPU%d failed during %pSb\n", cpu, __builtin_return_address(0)); @@ -12004,7 +11993,8 @@ static void hardware_disable(void *junk) if (!cpumask_test_cpu(cpu, &cpus_hardware_enabled)) return; cpumask_clear_cpu(cpu, &cpus_hardware_enabled); - kvm_arch_hardware_disable(); + static_call(kvm_x86_hardware_disable)(); + drop_user_return_notifiers(); } /* From patchwork Thu Sep 22 18:20:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985670 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 7B941ECAAD8 for ; Thu, 22 Sep 2022 18:24:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232705AbiIVSYK (ORCPT ); Thu, 22 Sep 2022 14:24:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232409AbiIVSXc (ORCPT ); Thu, 22 Sep 2022 14:23:32 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 617AF10C7AB; Thu, 22 Sep 2022 11:22:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663870943; x=1695406943; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=K9aaefQYUfoduQuXfrOa7CqsIFkvX11RqK1UcOrUa/c=; b=N74Jol8cPa83oDa53rDXdNb8zcE/mMbTiYcCNaiL6hd1kzuD9B7tuKxT Falx2YSyoabjq+sOPs3nct6r9KIWIUNJsEbnd4XtUIO5Fsjp6FqaX1o4h Vme6NXkGir0bO4P0ETmhkfL+FG6ebUGgYtaHndWWIUjSWBPWEwVAx+VTy vtnYxMIb+e4yuIbQDRJtF6kZ0WIcRj88EhL8/3Px8rqxzcY5a/O7erqnF /HTKECNMbCrKHVlsTyM/Y4blklFjUE5gSZFjJEg732P3aXQULpv7jWa/c uzN/t09kM/+Zj+NemVHAwjXd7BYkqyrW3jlisvMCU7mqesRWVilGdUXi1 Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="326712936" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="326712936" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:28 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086887" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:27 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov Subject: [PATCH v5 26/30] KVM: x86: Make x86 processor compat check callback empty Date: Thu, 22 Sep 2022 11:20:55 -0700 Message-Id: <51853ef700087241e624fc531cd79e41972d51aa.1663869838.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index aa6594de1fc1..00cc74276819 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); +static int kvm_check_processor_compatibility(void); + struct kvm_x86_ops kvm_x86_ops __read_mostly; #define KVM_X86_OP(func) \ @@ -12033,21 +12035,8 @@ int kvm_arch_del_vm(int usage_count) return 0; } -static void check_processor_compat(void *rtn) -{ - *(int *)rtn = kvm_arch_check_processor_compat(); -} - int kvm_arch_check_processor_compat_all(void) { - int cpu; - int r; - - for_each_online_cpu(cpu) { - smp_call_function_single(cpu, check_processor_compat, &r, 1); - if (r < 0) - return r; - } return 0; } @@ -12055,7 +12044,7 @@ int kvm_arch_online_cpu(unsigned int cpu, int usage_count) { int ret; - ret = kvm_arch_check_processor_compat(); + ret = kvm_check_processor_compatibility(); if (ret) return ret; @@ -12125,6 +12114,24 @@ static inline void kvm_ops_update(struct kvm_x86_init_ops *ops) kvm_pmu_ops_update(ops->pmu_ops); } +static void check_processor_compat(void *rtn) +{ + *(int *)rtn = kvm_check_processor_compatibility(); +} + +static int kvm_check_processor_compatibility_all(void) +{ + int cpu; + int r; + + for_each_online_cpu(cpu) { + smp_call_function_single(cpu, check_processor_compat, &r, 1); + if (r < 0) + return r; + } + return 0; +} + int kvm_arch_hardware_setup(void *opaque) { struct kvm_x86_init_ops *ops = opaque; @@ -12165,7 +12172,8 @@ int kvm_arch_hardware_setup(void *opaque) } kvm_caps.default_tsc_scaling_ratio = 1ULL << kvm_caps.tsc_scaling_ratio_frac_bits; kvm_init_msr_list(); - return 0; + + return kvm_check_processor_compatibility_all(); } void kvm_arch_hardware_unsetup(void) @@ -12175,7 +12183,7 @@ void kvm_arch_hardware_unsetup(void) static_call(kvm_x86_hardware_unsetup)(); } -int kvm_arch_check_processor_compat(void) +static int kvm_check_processor_compatibility(void) { int cpu = smp_processor_id(); struct cpuinfo_x86 *c = &cpu_data(cpu); @@ -12195,6 +12203,11 @@ int kvm_arch_check_processor_compat(void) return static_call(kvm_x86_check_processor_compatibility)(); } +int kvm_arch_check_processor_compat(void) +{ + return 0; +} + bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu) { return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id; From patchwork Thu Sep 22 18:20:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985672 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 CB87EC6FA82 for ; Thu, 22 Sep 2022 18:24:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229649AbiIVSY2 (ORCPT ); Thu, 22 Sep 2022 14:24:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35060 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232650AbiIVSXm (ORCPT ); Thu, 22 Sep 2022 14:23:42 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6909010D647; Thu, 22 Sep 2022 11:22:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663870948; x=1695406948; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=W2Kk+Oh554M6hYppcd4bamgUFE8iODrNht1zMrNWv44=; b=DgMFcpt2hXTCBYItcSRRmP/nE2AN+siYptH8QwzyAMZu/6xwX6iwVwRu kySITF+P1+P5nUnSsbLnYZOp/YLLtDboItTULV2zUFZ5p7VcKzRN8VctQ izpn0W3ErqRPbRpAQZoQWiA2CCCo13Qq865NvMNnG1SPPoslrI2i/t9fc NTO7ku5WXw2XxLAZM6TSVWRyjjCG18HSuURtfpwJTGoN40fr3VzR/Flhb qoinFx6H0k5AgeCjB0lKs/ye5S96lFL0GlTf9lqeznkSPSNBmNAwvSxlM InMJb+PAB36AewAoYFNBWn/20h9MWqPdBNdRKW9uqLGhfe3hPOlY4NmcR A==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="326712941" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="326712941" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:29 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086909" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:28 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov , linuxppc-dev@lists.ozlabs.org, Fabiano Rosas Subject: [PATCH v5 27/30] RFC: KVM: powerpc: Move processor compatibility check to hardware setup Date: Thu, 22 Sep 2022 11:20:56 -0700 Message-Id: <574ca90fdaec0f37c197d9600d47d48a74f324bd.1663869838.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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. kvmppc_core_check_processor_compat() checks the global variable. There are five implementation for it as follows. arch/powerpc/include/asm/cputable.h: extern struct cpu_spec *cur_cpu_spec; arch/powerpc/kvm/book3s.c: return 0 arch/powerpc/kvm/e500.c: strcmp(cur_cpu_spec->cpu_name, "e500v2") arch/powerpc/kvm/e500mc.c: strcmp(cur_cpu_spec->cpu_name, "e500mc") strcmp(cur_cpu_spec->cpu_name, "e5500") strcmp(cur_cpu_spec->cpu_name, "e6500") Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata Cc: linuxppc-dev@lists.ozlabs.org Cc: Fabiano Rosas Acked-by: Michael Ellerman (powerpc) --- arch/powerpc/kvm/powerpc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/powerpc/kvm/powerpc.c b/arch/powerpc/kvm/powerpc.c index 7b56d6ccfdfb..31dc4f231e9d 100644 --- a/arch/powerpc/kvm/powerpc.c +++ b/arch/powerpc/kvm/powerpc.c @@ -444,12 +444,12 @@ int kvm_arch_hardware_enable(void) int kvm_arch_hardware_setup(void *opaque) { - return 0; + return kvmppc_core_check_processor_compat(); } int kvm_arch_check_processor_compat(void) { - return kvmppc_core_check_processor_compat(); + return 0; } int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) From patchwork Thu Sep 22 18:20:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985673 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 1A89FC54EE9 for ; Thu, 22 Sep 2022 18:24:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232670AbiIVSYl (ORCPT ); Thu, 22 Sep 2022 14:24:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35510 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232674AbiIVSXr (ORCPT ); Thu, 22 Sep 2022 14:23:47 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F0F5C109603; Thu, 22 Sep 2022 11:22:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663870951; x=1695406951; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=OXolcTNJkMpWfAxfM3a0npQERQkANrv/gXnofXUpGHE=; b=X6KZmNuFUEnCdpQfnH43Nz42/8rGnUkcaKfy6jc+9kBzFhZ9UJC+LqVU sbJI5OAEOLNEjcXmadhKki8hdr+asaicszMPP1dRTHl4a4K0PBmkOCzqo d9S+qLlWadHd72n5qYtMUp67xQm4TpBMplMXPMmYwqRxYoJblnvzCqu+e e3vQmEwKBV/Bi4ZLRvnS2D6JDc/7KUbwjMdsU0Sr4EVyhucb//M2U4M7p /txfMjMIHcUvDg2iowT2/veJGMf3sfQg35bBW+zApTABdXemkhM50uqu/ oHYLQc1NicmrRElAW3CGyj9LWOrnZI35UjYhBe4usuX1c37kneGi9mMcV w==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="326712946" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="326712946" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:29 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086928" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:29 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov Subject: [PATCH v5 28/30] KVM: Eliminate kvm_arch_check_processor_compat() Date: Thu, 22 Sep 2022 11:20:57 -0700 Message-Id: <4f920489411a21e99dd0e20d011286f73254afa8.1663869838.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 | 22 ---------------------- virt/kvm/kvm_main.c | 4 ---- 9 files changed, 63 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; } -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; } -int kvm_arch_check_processor_compat(void) -{ - return 0; -} - extern void kvm_init_loongson_ipi(struct kvm *kvm); int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) diff --git a/arch/powerpc/kvm/powerpc.c b/arch/powerpc/kvm/powerpc.c index 31dc4f231e9d..546ebbe16bfe 100644 --- a/arch/powerpc/kvm/powerpc.c +++ b/arch/powerpc/kvm/powerpc.c @@ -447,11 +447,6 @@ int kvm_arch_hardware_setup(void *opaque) return kvmppc_core_check_processor_compat(); } -int kvm_arch_check_processor_compat(void) -{ - return 0; -} - int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) { struct kvmppc_ops *kvm_ops = NULL; 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; } -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; } -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 00cc74276819..1f791c93391b 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12035,11 +12035,6 @@ int kvm_arch_del_vm(int usage_count) return 0; } -int kvm_arch_check_processor_compat_all(void) -{ - return 0; -} - int kvm_arch_online_cpu(unsigned int cpu, int usage_count) { int ret; @@ -12203,11 +12198,6 @@ static int kvm_check_processor_compatibility(void) return static_call(kvm_x86_check_processor_compatibility)(); } -int kvm_arch_check_processor_compat(void) -{ - return 0; -} - bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu) { return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id; diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 7efc4792ff5f..87d4f42f3ff9 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1447,8 +1447,6 @@ void kvm_arch_hardware_disable(void); int kvm_arch_hardware_setup(void *opaque); void kvm_arch_hardware_unsetup(void); -int kvm_arch_check_processor_compat(void); -int kvm_arch_check_processor_compat_all(void); int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu); bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu); int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu); diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c index 2509c2777a49..076a55d59988 100644 --- a/virt/kvm/kvm_arch.c +++ b/virt/kvm/kvm_arch.c @@ -82,32 +82,10 @@ int __weak kvm_arch_del_vm(int usage_count) return 0; } -static void check_processor_compat(void *rtn) -{ - *(int *)rtn = kvm_arch_check_processor_compat(); -} - -int __weak kvm_arch_check_processor_compat_all(void) -{ - int cpu; - int r; - - for_each_online_cpu(cpu) { - smp_call_function_single(cpu, check_processor_compat, &r, 1); - if (r < 0) - return r; - } - return 0; -} - int __weak kvm_arch_online_cpu(unsigned int cpu, int usage_count) { int ret; - ret = kvm_arch_check_processor_compat(); - if (ret) - return ret; - if (!usage_count) return 0; diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index d5f882fb9e0c..1c1a2b0630bc 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, unsigned vcpu_align, if (r < 0) goto out_free_1; - r = kvm_arch_check_processor_compat_all(); - if (r < 0) - goto out_free_2; - r = cpuhp_setup_state_nocalls(CPUHP_AP_KVM_ONLINE, "kvm/cpu:online", kvm_online_cpu, kvm_offline_cpu); if (r) From patchwork Thu Sep 22 18:20:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985674 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 95A0FC6FA82 for ; Thu, 22 Sep 2022 18:24:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232441AbiIVSYo (ORCPT ); Thu, 22 Sep 2022 14:24:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232692AbiIVSXt (ORCPT ); Thu, 22 Sep 2022 14:23:49 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C4F7810D66B; Thu, 22 Sep 2022 11:22:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663870954; x=1695406954; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=0GvMWbVPXwgA7CdFobND5PDqeVhAEZxtFBVTWjqYWSY=; b=DKH4f+sthQUZKp0X5/i9XD8Mt6YsivATG3eWmp0eGebXJFZDyS1lvVos dCQHfjQsCpVWhEEEFxyvO+b34z6MmG12J0IS6GK/Y674+foTBbrhgT/+n 8KpEzJBua7pX2maic5FYMka0WTmrOEKZvwVwqje8it09HrUVzqedrRP+c a1m2z/KI6aIokujfLf9DtGfgwmQaroNdHKK6jThb0ve1qKklr0/y78F78 YUbGFc3BQ2d1UMtZRLdNf7hTbd4JR//+S1ErGaJRmO+GBe2fZtRPNrLa1 H7q1akRzk8M4FP4dUnTzLUboNrojx1xBrydA8nqw1/NPIfFlIm/cRlble Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="326712948" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="326712948" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:30 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086946" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:29 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov Subject: [PATCH v5 29/30] RFC: KVM: x86: Remove cpus_hardware_enabled and related sanity check Date: Thu, 22 Sep 2022 11:20:58 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 1f791c93391b..7505dd4ae695 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11958,23 +11958,16 @@ void kvm_arch_resume(int usage_count) } } -static cpumask_t cpus_hardware_enabled = CPU_MASK_NONE; - static int __hardware_enable(void) { - int cpu = raw_smp_processor_id(); int r; WARN_ON_ONCE(preemptible()); - if (cpumask_test_cpu(cpu, &cpus_hardware_enabled)) - return 0; r = static_call(kvm_x86_hardware_enable)(); if (r) pr_warn("kvm: enabling virtualization on CPU%d failed during %pSb\n", - cpu, __builtin_return_address(0)); - else - cpumask_set_cpu(cpu, &cpus_hardware_enabled); + smp_processor_id(), __builtin_return_address(0)); return r; } @@ -11988,13 +11981,7 @@ static void hardware_enable(void *arg) static void hardware_disable(void *junk) { - int cpu = raw_smp_processor_id(); - WARN_ON_ONCE(preemptible()); - - if (!cpumask_test_cpu(cpu, &cpus_hardware_enabled)) - return; - cpumask_clear_cpu(cpu, &cpus_hardware_enabled); static_call(kvm_x86_hardware_disable)(); drop_user_return_notifiers(); } From patchwork Thu Sep 22 18:20:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 12985675 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 D6B0BECAAD8 for ; Thu, 22 Sep 2022 18:24:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232533AbiIVSYr (ORCPT ); Thu, 22 Sep 2022 14:24:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36212 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232380AbiIVSXv (ORCPT ); Thu, 22 Sep 2022 14:23:51 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0F4BD10D66E; Thu, 22 Sep 2022 11:22:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663870956; x=1695406956; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=e7eFeq+AhqW0LXyZEdeUIZ0W61VhBhwYbIGQ5iPG5f0=; b=AV/3lU3OvnOLkDEB851zm7NVzvzyGEpyvL1H2eqFJZO1L2bock6N79Cc YrIr6QCR0WWd5U7Ga6nqTQM2dhlUesvHHPT4N6Z4nBYJ42I7G3kPP6Cn7 9qOo9ZH7ot0ua0KA/pldBZmukrbkxSWfsh27I6kJJLeUT7Cue1LIhgkm5 gxqV3GsppHZJV2ZS2ktDUsz5a/k/WAs+RHOe6121LTexXk9Hm4H5/r4lf 0z5tYVf71a0zyZ80Jb88Qm6RwVcB/KC2XD83DCC0+TCUTrBpTiXzgVF1A rbMS0QUNQE2KLba6Lq+4pk+5Qz/CEJTOtFHgzVbO8HgBxgF3MQZ3oU5lI g==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="326712953" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="326712953" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:30 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086961" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:30 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov Subject: [PATCH v5 30/30] RFC: KVM: Remove cpus_hardware_enabled and related sanity check Date: Thu, 22 Sep 2022 11:20:59 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 076a55d59988..e7708bfa0360 100644 --- a/virt/kvm/kvm_arch.c +++ b/virt/kvm/kvm_arch.c @@ -12,23 +12,16 @@ #include -static cpumask_t cpus_hardware_enabled = CPU_MASK_NONE; - static int __hardware_enable(void) { - int cpu = raw_smp_processor_id(); int r; WARN_ON_ONCE(preemptible()); - if (cpumask_test_cpu(cpu, &cpus_hardware_enabled)) - return 0; r = kvm_arch_hardware_enable(); if (r) pr_warn("kvm: enabling virtualization on CPU%d failed during %pSb\n", - cpu, __builtin_return_address(0)); - else - cpumask_set_cpu(cpu, &cpus_hardware_enabled); + smp_processor_id(), __builtin_return_address(0)); return r; } @@ -42,13 +35,7 @@ static void hardware_enable(void *arg) static void hardware_disable(void *junk) { - int cpu = raw_smp_processor_id(); - WARN_ON_ONCE(preemptible()); - - if (!cpumask_test_cpu(cpu, &cpus_hardware_enabled)) - return; - cpumask_clear_cpu(cpu, &cpus_hardware_enabled); kvm_arch_hardware_disable(); }