From nobody Fri Apr 10 20:28:26 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5921CC25B08 for ; Sat, 20 Aug 2022 06:01:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245722AbiHTGBw (ORCPT ); Sat, 20 Aug 2022 02:01:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47524 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244498AbiHTGAx (ORCPT ); Sat, 20 Aug 2022 02:00:53 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3915A260A; Fri, 19 Aug 2022 23:00:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1660975252; x=1692511252; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=tz2myrWkcxiLhdua2HaqakBscUxnMz1ROup5PGWdHyk=; b=jTS7XwU6MPL3DL0cjYIIm2jSpmI2cUaMipFWtSHdj8X4u50IpshE64gP d/vxNzu+EoBfBUzQkkqkemmJtt7FxlipAh0Xcw2aMpbvnTLQVZg2OQ8LI yQY1YqvThEME123pF9bikpFG3kFyCFZjuiYlQ4gwbX+/Y9v5Wf2bWmQes xDOfqErX1zArVT74qsx02Ilxh8CcleAIua5rj7N8PcYWPRb/jK9vo0Svu nEhZh4aQ9zmz1qAILCzaWRRIh+iHNbwnX5S3eoHa9O9Nq/8c6AAFoG+AV MyaJqp5QMhZwvJRoa2bLCqX7M13BVibg4dzxIryZe6FDS87obTu0mJwDj g==; X-IronPort-AV: E=McAfee;i="6500,9779,10444"; a="379448980" X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="379448980" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Aug 2022 23:00:50 -0700 X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="668857551" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Aug 2022 23:00:50 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , Sean Christopherson , Kai Huang , Chao Gao , Will Deacon Subject: [RFC PATCH 13/18] KVM: x86: Duplicate arch callbacks related to pm events Date: Fri, 19 Aug 2022 23:00:19 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata KVM/X86 can change those callbacks without worrying about breaking other archs. Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- arch/x86/kvm/x86.c | 125 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 120 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 0b112cd7de58..71e90d0f0da9 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11841,6 +11841,124 @@ void kvm_arch_hardware_disable(void) drop_user_return_notifiers(); } =20 +static cpumask_t cpus_hardware_enabled =3D CPU_MASK_NONE; + +int kvm_arch_post_init_vm(struct kvm *kvm) +{ + return kvm_mmu_post_init_vm(kvm); +} + +static int __hardware_enable(void) +{ + int cpu =3D raw_smp_processor_id(); + int r; + + WARN_ON_ONCE(preemptible()); + + if (cpumask_test_cpu(cpu, &cpus_hardware_enabled)) + return 0; + r =3D kvm_arch_hardware_enable(); + if (r) + pr_info("kvm: enabling virtualization on CPU%d failed\n", cpu); + else + cpumask_set_cpu(cpu, &cpus_hardware_enabled); + return r; +} + +static void hardware_enable(void *arg) +{ + atomic_t *failed =3D arg; + + if (__hardware_enable()) + atomic_inc(failed); +} + +static void hardware_disable(void *junk) +{ + int cpu =3D raw_smp_processor_id(); + + WARN_ON_ONCE(preemptible()); + + if (!cpumask_test_cpu(cpu, &cpus_hardware_enabled)) + return; + cpumask_clear_cpu(cpu, &cpus_hardware_enabled); + kvm_arch_hardware_disable(); +} + +void kvm_arch_pre_hardware_unsetup(void) +{ + on_each_cpu(hardware_disable, NULL, 1); +} + +/* + * Called after the VM is otherwise initialized, but just before adding it= to + * the vm_list. + */ +int kvm_arch_add_vm(struct kvm *kvm, int usage_count) +{ + atomic_t failed; + int r =3D 0; + + if (usage_count !=3D 1) + return 0; + + atomic_set(&failed, 0); + on_each_cpu(hardware_enable, &failed, 1); + + if (atomic_read(&failed)) { + r =3D -EBUSY; + goto err; + } + + r =3D kvm_arch_post_init_vm(kvm); +err: + if (r && usage_count =3D=3D 1) + on_each_cpu(hardware_disable, NULL, 1); + return r; +} + +int kvm_arch_del_vm(int usage_count) +{ + if (usage_count) + return 0; + + on_each_cpu(hardware_disable, NULL, 1); + return 0; +} + +int kvm_arch_online_cpu(unsigned int cpu, int usage_count) +{ + int r; + + if (!usage_count) + return 0; + + r =3D kvm_arch_check_processor_compat(); + if (r) + return r; + return __hardware_enable(); +} + +int kvm_arch_offline_cpu(unsigned int cpu, int usage_count) +{ + if (usage_count) + hardware_disable(NULL); + return 0; +} + +int kvm_arch_reboot(int val) +{ + on_each_cpu(hardware_disable, NULL, 1); + return NOTIFY_OK; +} + +int kvm_arch_suspend(int usage_count) +{ + if (usage_count) + hardware_disable(NULL); + return 0; +} + void kvm_arch_resume(int usage_count) { struct kvm *kvm; @@ -11853,6 +11971,8 @@ void kvm_arch_resume(int usage_count) if (!usage_count) return; =20 + if (kvm_arch_check_processor_compat()) + return; if (kvm_arch_hardware_enable()) return; =20 @@ -12104,11 +12224,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned lon= g type) return ret; } =20 -int kvm_arch_post_init_vm(struct kvm *kvm) -{ - return kvm_mmu_post_init_vm(kvm); -} - static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu) { vcpu_load(vcpu); --=20 2.25.1