From nobody Fri Apr 10 18:35:27 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 A6C33C25B08 for ; Sat, 20 Aug 2022 06:00:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244430AbiHTGAw (ORCPT ); Sat, 20 Aug 2022 02:00:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229458AbiHTGAt (ORCPT ); Sat, 20 Aug 2022 02:00:49 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED3A2A2237; Fri, 19 Aug 2022 23:00:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1660975248; x=1692511248; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=8uxTMc931N62LSyYSZec4Q6h4MH1p09mPLFpxJiMDMs=; b=CewhIl6ghhuIdaGvkrFc4OZFbfbIEDYwG4QF6MlW8onpRE+uQEiqXNLf g206XtIrrsdHcVnR94dGVJ4pGFKKdBneQwc1SFt89R6kEo6awZ6usb8m+ g2aXbzOfmvqENu/N6NZC+6Rq3YkDgEeav5Wf+iRf8FaJzYKBlfGUjBQZw M5jY12bnFW8kU58OSY6RFtyi54JDHJYO8zQTD/i3PwADUKg67nbMTgwmf RDO0nJBU2bUfWtlBAffuZc3ZMQgAf3OAiA42j/Sm8YcsL0H0TB+HA5Q5a WESY5/aLZqFdJnAUWAWYm5ylQ+54l4EbFhL7Aeg5zzYw0k3cG5mxIvLo4 w==; X-IronPort-AV: E=McAfee;i="6500,9779,10444"; a="379448966" X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="379448966" 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:48 -0700 X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="668857511" 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:48 -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 01/18] KVM: x86: Drop kvm_user_return_msr_cpu_online() Date: Fri, 19 Aug 2022 23:00:07 -0700 Message-Id: <9e0f84ece483bfeffe7e0b254363845af910ea84.1660974106.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata KVM/X86 uses user return notifier to switch MSR for guest or user space. Snapshot host values on CPU online, change MSR values for guest, and restore them on returning to user space. The current code abuses kvm_arch_hardware_enable() which is called on kvm module initialization or CPU online. Remove such the abuse of kvm_arch_hardware_enable by capturing the host value on the first change of the MSR value to guest VM instead of CPU online. Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- arch/x86/kvm/x86.c | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 205ebdc2b11b..16104a2f7d8e 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -200,6 +200,7 @@ struct kvm_user_return_msrs { struct kvm_user_return_msr_values { u64 host; u64 curr; + bool initialized; } values[KVM_MAX_NR_USER_RETURN_MSRS]; }; =20 @@ -363,6 +364,10 @@ static void kvm_on_user_return(struct user_return_noti= fier *urn) local_irq_restore(flags); for (slot =3D 0; slot < kvm_nr_uret_msrs; ++slot) { values =3D &msrs->values[slot]; + /* + * No need to check values->initialized because host =3D curr =3D 0 + * by __GFP_ZERO when !values->initialized. + */ if (values->host !=3D values->curr) { wrmsrl(kvm_uret_msrs_list[slot], values->host); values->curr =3D values->host; @@ -409,34 +414,30 @@ int kvm_find_user_return_msr(u32 msr) } EXPORT_SYMBOL_GPL(kvm_find_user_return_msr); =20 -static void kvm_user_return_msr_cpu_online(void) -{ - unsigned int cpu =3D smp_processor_id(); - struct kvm_user_return_msrs *msrs =3D per_cpu_ptr(user_return_msrs, cpu); - u64 value; - int i; - - for (i =3D 0; i < kvm_nr_uret_msrs; ++i) { - rdmsrl_safe(kvm_uret_msrs_list[i], &value); - msrs->values[i].host =3D value; - msrs->values[i].curr =3D value; - } -} - int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask) { unsigned int cpu =3D smp_processor_id(); struct kvm_user_return_msrs *msrs =3D per_cpu_ptr(user_return_msrs, cpu); + struct kvm_user_return_msr_values *values =3D &msrs->values[slot]; int err; =20 - value =3D (value & mask) | (msrs->values[slot].host & ~mask); - if (value =3D=3D msrs->values[slot].curr) + if (unlikely(!values->initialized)) { + u64 host_value; + + rdmsrl_safe(kvm_uret_msrs_list[slot], &host_value); + values->host =3D host_value; + values->curr =3D host_value; + values->initialized =3D true; + } + + value =3D (value & mask) | (values->host & ~mask); + if (value =3D=3D values->curr) return 0; err =3D wrmsrl_safe(kvm_uret_msrs_list[slot], value); if (err) return 1; =20 - msrs->values[slot].curr =3D value; + values->curr =3D value; if (!msrs->registered) { msrs->urn.on_user_return =3D kvm_on_user_return; user_return_notifier_register(&msrs->urn); @@ -9212,7 +9213,12 @@ int kvm_arch_init(void *opaque) return -ENOMEM; } =20 - user_return_msrs =3D alloc_percpu(struct kvm_user_return_msrs); + /* + * __GFP_ZERO to ensure user_return_msrs.values[].{host, curr} match. + * See kvm_on_user_return() + */ + user_return_msrs =3D alloc_percpu_gfp(struct kvm_user_return_msrs, + GFP_KERNEL | __GFP_ZERO); if (!user_return_msrs) { printk(KERN_ERR "kvm: failed to allocate percpu kvm_user_return_msrs\n"); r =3D -ENOMEM; @@ -11836,7 +11842,6 @@ int kvm_arch_hardware_enable(void) u64 max_tsc =3D 0; bool stable, backwards_tsc =3D false; =20 - kvm_user_return_msr_cpu_online(); ret =3D static_call(kvm_x86_hardware_enable)(); if (ret !=3D 0) return ret; --=20 2.25.1 From nobody Fri Apr 10 18:35:27 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 2B060C32793 for ; Sat, 20 Aug 2022 06:01:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244772AbiHTGA6 (ORCPT ); Sat, 20 Aug 2022 02:00:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47466 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244041AbiHTGAu (ORCPT ); Sat, 20 Aug 2022 02:00:50 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B4200A223E; Fri, 19 Aug 2022 23:00: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=1660975249; x=1692511249; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=uz16PyoQFhukf/H8M5ipcGMQZYTZukV44jdncTSqdg4=; b=i7DsDSbDCAsU0x5fuUJQk69h6mWNHdaGSXf/n7hbUcs1Kmsx9317UaQA Y3iv0IprKBxl/bcQ1GcKEsSGtB4PU5me+NGh5ULN3+mOBePcq3G1Sjaut 3O5c2Jb1RItq8eAGWhPt9EXagWJ4tEwj7a9I4P7Srbx+9yQhMeRGpqV/B gJw6ebRt3cvLWcUjrAyN8z9eWKONawJfvRu8PhHXRspD5NOovdECQKUfq P0XiMCSefijY4ZGlasH6Gr7BalcU4rmwPvhTIboooxr8gmy52S4frX5Wd RV2Mu9dl6wgUCZ78rKR0zOYIrKbXtJ/yiHZfrAvJjqsaTTk1d7H5oqnSO A==; X-IronPort-AV: E=McAfee;i="6500,9779,10444"; a="379448967" X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="379448967" 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:48 -0700 X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="668857514" 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:48 -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 02/18] KVM: x86: Use this_cpu_ptr() instead of per_cpu_ptr(smp_processor_id()) Date: Fri, 19 Aug 2022 23:00:08 -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 convert per_cpu_ptr(smp_processor_id()) to this_cpu_ptr() as trivial cleanup. Signed-off-by: Isaku Yamahata --- arch/x86/kvm/x86.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 16104a2f7d8e..7d5fff68befe 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -416,8 +416,7 @@ EXPORT_SYMBOL_GPL(kvm_find_user_return_msr); =20 int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask) { - unsigned int cpu =3D smp_processor_id(); - struct kvm_user_return_msrs *msrs =3D per_cpu_ptr(user_return_msrs, cpu); + struct kvm_user_return_msrs *msrs =3D this_cpu_ptr(user_return_msrs); struct kvm_user_return_msr_values *values =3D &msrs->values[slot]; int err; =20 @@ -449,8 +448,7 @@ EXPORT_SYMBOL_GPL(kvm_set_user_return_msr); =20 static void drop_user_return_notifiers(void) { - unsigned int cpu =3D smp_processor_id(); - struct kvm_user_return_msrs *msrs =3D per_cpu_ptr(user_return_msrs, cpu); + struct kvm_user_return_msrs *msrs =3D this_cpu_ptr(user_return_msrs); =20 if (msrs->registered) kvm_on_user_return(&msrs->urn); --=20 2.25.1 From nobody Fri Apr 10 18:35:27 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 9EA93C32774 for ; Sat, 20 Aug 2022 06:01:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244963AbiHTGBA (ORCPT ); Sat, 20 Aug 2022 02:01:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47478 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244217AbiHTGAu (ORCPT ); Sat, 20 Aug 2022 02:00:50 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D8DBBA260A; Fri, 19 Aug 2022 23:00: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=1660975249; x=1692511249; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=0AYhMY6fJqrli/t5ys1IPzbYaiXeLEr/7lceDSDM3tw=; b=FMFKAOJX+8eiMsYlL5ED4+LvG1wf1iZI2/QkTFtYsejdBIaXlefKTAO/ CCfhIpRM7PkLChoOzdQ75avpV4PBHi4jplpPP17W1VNHINkfJciBw36DJ gPa56ruf2mHwTvEhQ66eRvJdglJWtz6lqpQjNng3n0ichMrcnHdSNtRYT qvcWnn3cxb6FQ+RK9HE3Cvd7K9JrGVBMArmIyxJKo4B86WvaZLq8SzW7C Bt2shy0JQ/lHlpjrROsb04GliTklxbDR+7nFhfsKRZFi7h8skHWp56E7f vNXGCznFsShm3ZBYkU0Z1XaP+UAYmgb4ANzUrTasmpehAZ4VC+6DevXIT A==; X-IronPort-AV: E=McAfee;i="6500,9779,10444"; a="379448969" X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="379448969" 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:48 -0700 X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="668857517" 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:48 -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 03/18] KVM: Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock Date: Fri, 19 Aug 2022 23:00:09 -0700 Message-Id: <6b07c02dd361f834fea442eb8dae53f23618f983.1660974106.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata Because kvm_count_lock unnecessarily complicates the KVM locking convention Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock for simplicity. Opportunistically add some comments on locking. Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- Documentation/virt/kvm/locking.rst | 14 +++++------- virt/kvm/kvm_main.c | 36 +++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/Documentation/virt/kvm/locking.rst b/Documentation/virt/kvm/lo= cking.rst index 845a561629f1..8957e32aa724 100644 --- a/Documentation/virt/kvm/locking.rst +++ b/Documentation/virt/kvm/locking.rst @@ -216,15 +216,11 @@ time it will be set using the Dirty tracking mechanis= m described above. :Type: mutex :Arch: any :Protects: - vm_list - -``kvm_count_lock`` -^^^^^^^^^^^^^^^^^^ - -:Type: raw_spinlock_t -:Arch: any -:Protects: - hardware virtualization enable/disable -:Comment: 'raw' because hardware enabling/disabling must be atomic /wrt - migration. + - kvm_usage_count + - hardware virtualization enable/disable +:Comment: Use cpus_read_lock() for hardware virtualization enable/disable + because hardware enabling/disabling must be atomic /wrt + migration. The lock order is cpus lock =3D> kvm_lock. =20 ``kvm->mn_invalidate_lock`` ^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 515dfe9d3bcf..c6781fa30461 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -100,7 +100,6 @@ EXPORT_SYMBOL_GPL(halt_poll_ns_shrink); */ =20 DEFINE_MUTEX(kvm_lock); -static DEFINE_RAW_SPINLOCK(kvm_count_lock); LIST_HEAD(vm_list); =20 static cpumask_var_t cpus_hardware_enabled; @@ -4999,6 +4998,8 @@ static void hardware_enable_nolock(void *junk) int cpu =3D raw_smp_processor_id(); int r; =20 + WARN_ON_ONCE(preemptible()); + if (cpumask_test_cpu(cpu, cpus_hardware_enabled)) return; =20 @@ -5015,10 +5016,10 @@ static void hardware_enable_nolock(void *junk) =20 static int kvm_starting_cpu(unsigned int cpu) { - raw_spin_lock(&kvm_count_lock); + mutex_lock(&kvm_lock); if (kvm_usage_count) hardware_enable_nolock(NULL); - raw_spin_unlock(&kvm_count_lock); + mutex_unlock(&kvm_lock); return 0; } =20 @@ -5026,6 +5027,8 @@ static void hardware_disable_nolock(void *junk) { int cpu =3D raw_smp_processor_id(); =20 + WARN_ON_ONCE(preemptible()); + if (!cpumask_test_cpu(cpu, cpus_hardware_enabled)) return; cpumask_clear_cpu(cpu, cpus_hardware_enabled); @@ -5034,10 +5037,10 @@ static void hardware_disable_nolock(void *junk) =20 static int kvm_dying_cpu(unsigned int cpu) { - raw_spin_lock(&kvm_count_lock); + mutex_lock(&kvm_lock); if (kvm_usage_count) hardware_disable_nolock(NULL); - raw_spin_unlock(&kvm_count_lock); + mutex_unlock(&kvm_lock); return 0; } =20 @@ -5052,16 +5055,19 @@ static void hardware_disable_all_nolock(void) =20 static void hardware_disable_all(void) { - raw_spin_lock(&kvm_count_lock); + cpus_read_lock(); + mutex_lock(&kvm_lock); hardware_disable_all_nolock(); - raw_spin_unlock(&kvm_count_lock); + mutex_unlock(&kvm_lock); + cpus_read_unlock(); } =20 static int hardware_enable_all(void) { int r =3D 0; =20 - raw_spin_lock(&kvm_count_lock); + cpus_read_lock(); + mutex_lock(&kvm_lock); =20 kvm_usage_count++; if (kvm_usage_count =3D=3D 1) { @@ -5074,7 +5080,8 @@ static int hardware_enable_all(void) } } =20 - raw_spin_unlock(&kvm_count_lock); + mutex_unlock(&kvm_lock); + cpus_read_unlock(); =20 return r; } @@ -5680,15 +5687,22 @@ static void kvm_init_debug(void) =20 static int kvm_suspend(void) { - if (kvm_usage_count) + /* + * The caller ensures that CPU hotlug is disabled by + * cpu_hotplug_disable() and other CPUs are offlined. No need for + * locking. + */ + if (kvm_usage_count) { + lockdep_assert_not_held(&kvm_lock); hardware_disable_nolock(NULL); + } return 0; } =20 static void kvm_resume(void) { if (kvm_usage_count) { - lockdep_assert_not_held(&kvm_count_lock); + lockdep_assert_not_held(&kvm_lock); hardware_enable_nolock(NULL); } } --=20 2.25.1 From nobody Fri Apr 10 18:35:27 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 BE54DC3F6B0 for ; Sat, 20 Aug 2022 06:01:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245137AbiHTGBH (ORCPT ); Sat, 20 Aug 2022 02:01:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47466 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244283AbiHTGAv (ORCPT ); Sat, 20 Aug 2022 02:00:51 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B9DAA2237; Fri, 19 Aug 2022 23:00:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1660975250; x=1692511250; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=0VoHVBGgGCiqNYxpIdpSYtKotvAu6f6dsiGvmJLhrGw=; b=OJsqD0mfjj3datvCkTy/yTzdDc4Z+iEsxaxxFn6xK250u0X3U0BSmoNJ xnduV6HZTcxgvqxCuMnrfTVloTpDrIkBsGhbMtx8yr4nloeOR2JRSyf9v +jaPZtei1SpkttXsKr0tx5cfTvE4mS/Bcn1cR8G16kDUPxakgc8xS4ybg La6Ez0/C1CwYGMi1llQ10kmAPLJqeP5y4KqP6XYzukKotBzp5W6LrzlGl 4nEI+s9kGQPSF2fzwnYSxlHKvSkTbDKxaqinRzm3rw7bqp9G6pKRORhGJ TUtXKyqZVd66CjtZn2oHD2OrIVKY0x5SnUzTiBQNeKEJ390EF5WLBrR2z Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10444"; a="379448970" X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="379448970" 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:49 -0700 X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="668857520" 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:49 -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 04/18] KVM: Add arch hooks for PM events with empty stub Date: Fri, 19 Aug 2022 23:00:10 -0700 Message-Id: <7f603a52229f8a31faf14a299daea114ec17b0e2.1660974106.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata Add arch hooks for reboot, suspend, resume, and CPU-online/offline events with empty stub functions. Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- include/linux/kvm_host.h | 6 ++++++ virt/kvm/Makefile.kvm | 2 +- virt/kvm/kvm_arch.c | 44 ++++++++++++++++++++++++++++++++++++++++ virt/kvm/kvm_main.c | 35 ++++++++++++++++---------------- 4 files changed, 69 insertions(+), 18 deletions(-) create mode 100644 virt/kvm/kvm_arch.c diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 1c480b1821e1..c38f382f3808 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1448,6 +1448,12 @@ int kvm_arch_post_init_vm(struct kvm *kvm); void kvm_arch_pre_destroy_vm(struct kvm *kvm); int kvm_arch_create_vm_debugfs(struct kvm *kvm); =20 +int kvm_arch_suspend(int usage_count); +void kvm_arch_resume(int usage_count); +int kvm_arch_reboot(int val); +int kvm_arch_online_cpu(unsigned int cpu, int usage_count); +int kvm_arch_offline_cpu(unsigned int cpu, int usage_count); + #ifndef __KVM_HAVE_ARCH_VM_ALLOC /* * All architectures that want to use vzalloc currently also diff --git a/virt/kvm/Makefile.kvm b/virt/kvm/Makefile.kvm index 2c27d5d0c367..c4210acabd35 100644 --- a/virt/kvm/Makefile.kvm +++ b/virt/kvm/Makefile.kvm @@ -5,7 +5,7 @@ =20 KVM ?=3D ../../../virt/kvm =20 -kvm-y :=3D $(KVM)/kvm_main.o $(KVM)/eventfd.o $(KVM)/binary_stats.o +kvm-y :=3D $(KVM)/kvm_main.o $(KVM)/kvm_arch.o $(KVM)/eventfd.o $(KVM)/bin= ary_stats.o kvm-$(CONFIG_KVM_VFIO) +=3D $(KVM)/vfio.o kvm-$(CONFIG_KVM_MMIO) +=3D $(KVM)/coalesced_mmio.o kvm-$(CONFIG_KVM_ASYNC_PF) +=3D $(KVM)/async_pf.o diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c new file mode 100644 index 000000000000..4748a76bcb03 --- /dev/null +++ b/virt/kvm/kvm_arch.c @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * kvm_arch.c: kvm default arch hooks for hardware enabling/disabling + * Copyright (c) 2022 Intel Corporation. + * + * Author: + * Isaku Yamahata + * + */ + +#include + +/* + * Called after the VM is otherwise initialized, but just before adding it= to + * the vm_list. + */ +__weak int kvm_arch_post_init_vm(struct kvm *kvm) +{ + return 0; +} + +__weak int kvm_arch_online_cpu(unsigned int cpu, int usage_count) +{ + return 0; +} + +__weak int kvm_arch_offline_cpu(unsigned int cpu, int usage_count) +{ + return 0; +} + +__weak int kvm_arch_reboot(int val) +{ + return NOTIFY_OK; +} + +__weak int kvm_arch_suspend(int usage_count) +{ + return 0; +} + +__weak void kvm_arch_resume(int usage_count) +{ +} diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index c6781fa30461..8d08126e6c74 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1095,15 +1095,6 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, co= nst char *fdname) return ret; } =20 -/* - * Called after the VM is otherwise initialized, but just before adding it= to - * the vm_list. - */ -int __weak kvm_arch_post_init_vm(struct kvm *kvm) -{ - return 0; -} - /* * Called just after removing the VM from the vm_list, but before doing any * other destruction. @@ -5019,6 +5010,7 @@ static int kvm_starting_cpu(unsigned int cpu) mutex_lock(&kvm_lock); if (kvm_usage_count) hardware_enable_nolock(NULL); + (void)kvm_arch_online_cpu(cpu, kvm_usage_count); mutex_unlock(&kvm_lock); return 0; } @@ -5040,6 +5032,7 @@ static int kvm_dying_cpu(unsigned int cpu) mutex_lock(&kvm_lock); if (kvm_usage_count) hardware_disable_nolock(NULL); + (void)kvm_arch_offline_cpu(cpu, kvm_usage_count); mutex_unlock(&kvm_lock); return 0; } @@ -5089,6 +5082,8 @@ static int hardware_enable_all(void) static int kvm_reboot(struct notifier_block *notifier, unsigned long val, void *v) { + int r; + /* * Some (well, at least mine) BIOSes hang on reboot if * in vmx root mode. @@ -5097,8 +5092,15 @@ static int kvm_reboot(struct notifier_block *notifie= r, unsigned long val, */ pr_info("kvm: exiting hardware virtualization\n"); kvm_rebooting =3D true; + + /* This hook is called without cpuhotplug disabled. */ + cpus_read_lock(); + mutex_lock(&kvm_lock); on_each_cpu(hardware_disable_nolock, NULL, 1); - return NOTIFY_OK; + r =3D kvm_arch_reboot(val); + mutex_unlock(&kvm_lock); + cpus_read_unlock(); + return r; } =20 static struct notifier_block kvm_reboot_notifier =3D { @@ -5692,19 +5694,18 @@ static int kvm_suspend(void) * cpu_hotplug_disable() and other CPUs are offlined. No need for * locking. */ - if (kvm_usage_count) { - lockdep_assert_not_held(&kvm_lock); + lockdep_assert_not_held(&kvm_lock); + if (kvm_usage_count) hardware_disable_nolock(NULL); - } - return 0; + return kvm_arch_suspend(kvm_usage_count); } =20 static void kvm_resume(void) { - if (kvm_usage_count) { - lockdep_assert_not_held(&kvm_lock); + kvm_arch_resume(kvm_usage_count); + lockdep_assert_not_held(&kvm_lock); + if (kvm_usage_count) hardware_enable_nolock(NULL); - } } =20 static struct syscore_ops kvm_syscore_ops =3D { --=20 2.25.1 From nobody Fri Apr 10 18:35:27 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 392FFC32774 for ; Sat, 20 Aug 2022 06:01:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245225AbiHTGBL (ORCPT ); Sat, 20 Aug 2022 02:01:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47478 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244300AbiHTGAv (ORCPT ); Sat, 20 Aug 2022 02:00:51 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B41C6A260F; Fri, 19 Aug 2022 23:00:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1660975250; x=1692511250; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=eC/FmkI+0wbcW1yT5moTG/B/uNqw/iJshQXbZJpzhw4=; b=ftFa88ZbGO4NSyQwP1KWpSeFyG3MJeaPvGEFqXN0AnZEBZlvd28jLewq kWr6xahh1wFpYcsgN2inaj0v6Xl9sRTtJM8OEYbsv8xHMz4hRvcM+Al1S O9AnFC/LaS04deRYInNN6r92Oit4BS8Ku57JREYLT9KRyznnMUkHENo7M dJu/KRpYppwSvYioyMEB+DhaGAiDIaoT5jaQP66sP0PZx7jbHzq7ldZvH j/YosBRB/5YHG4eO1R8m8WLT81NjIxEqFb1iXZYqLaMccZW43MEwPQTIc fbnYmYlo2+U6A3kd07srb8BaQ1cEMw+HvIn+hAeHeQZxGu3wGivEEAwo8 g==; X-IronPort-AV: E=McAfee;i="6500,9779,10444"; a="379448971" X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="379448971" 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:49 -0700 X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="668857524" 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:49 -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 05/18] KVM: x86: Move TSC fixup logic to KVM arch resume callback Date: Fri, 19 Aug 2022 23:00:11 -0700 Message-Id: <054173a5afbcc79e596dbcdac32588bf9a2242e4.1660974106.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata commit 0dd6a6edb012 ("KVM: Dont mark TSC unstable due to S4 suspend") made use of kvm_arch_hardware_enable() callback to detect that TSC goes backward due to S4 suspend. It has to check it only when resuming from S4. Not every time virtualization hardware ennoblement. Move the logic to kvm_arch_resume() callback. Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- arch/x86/kvm/x86.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 7d5fff68befe..a7ab9984a236 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11831,18 +11831,30 @@ void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu= *vcpu, u8 vector) EXPORT_SYMBOL_GPL(kvm_vcpu_deliver_sipi_vector); =20 int kvm_arch_hardware_enable(void) +{ + return static_call(kvm_x86_hardware_enable)(); +} + +void kvm_arch_hardware_disable(void) +{ + static_call(kvm_x86_hardware_disable)(); + drop_user_return_notifiers(); +} + +void kvm_arch_resume(int usage_count) { struct kvm *kvm; struct kvm_vcpu *vcpu; unsigned long i; - int ret; u64 local_tsc; u64 max_tsc =3D 0; bool stable, backwards_tsc =3D false; =20 - ret =3D static_call(kvm_x86_hardware_enable)(); - if (ret !=3D 0) - return ret; + if (!usage_count) + return; + + if (kvm_arch_hardware_enable()) + return; =20 local_tsc =3D rdtsc(); stable =3D !kvm_check_tsc_unstable(); @@ -11917,13 +11929,6 @@ int kvm_arch_hardware_enable(void) } =20 } - return 0; -} - -void kvm_arch_hardware_disable(void) -{ - static_call(kvm_x86_hardware_disable)(); - drop_user_return_notifiers(); } =20 static inline void kvm_ops_update(struct kvm_x86_init_ops *ops) --=20 2.25.1 From nobody Fri Apr 10 18:35:27 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 CCE1CC25B08 for ; Sat, 20 Aug 2022 06:01:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245284AbiHTGBO (ORCPT ); Sat, 20 Aug 2022 02:01:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47516 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244304AbiHTGAv (ORCPT ); Sat, 20 Aug 2022 02:00:51 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D9B26A2628; Fri, 19 Aug 2022 23:00:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1660975250; x=1692511250; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Tr3yA3TW4wQhy3y6XXgJ33VEExdUImA9iqqmlfQuqZk=; b=MguFDtAoF5mgYdt08wAuTqFALgCIMUvbKPa+KP5emjVHtzaBqDHbrIPl HGrg+9yekHSbELGsu08YTUY/gK/AecWZ3o0ZqdKIZO74VuArW9CFBBJJ2 3JhLZe+lL0KA7wE9g4MCn6kPZRqofIvjpBnYr4BxaRpYeRkisSydG8TOi UQ3MmyldxmsN3gfZjHpnKjTP4tYIkmQ8WkjXrhjmDoZ2mJQxKZPPfOR+R eV0Dkdz0PwCrCaK/ViA+TYOwqRXUHzukuFrLetf24tXR116qAZPj6UZcM xPb7MgqkiJSo19AzJAZfWidxhQp/unMDNP/mq4TGuzHy4qRJH5JKPzUyf A==; X-IronPort-AV: E=McAfee;i="6500,9779,10444"; a="379448972" X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="379448972" 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:49 -0700 X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="668857527" 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:49 -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 06/18] KVM: Add arch hook when VM is added/deleted Date: Fri, 19 Aug 2022 23:00:12 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata and pass kvm_usage_count with kvm_lock. Move kvm_arch_post_init_vm() under kvm_arch_add_vm(). Later kvm_arch_post_init_vm() is deleted once x86 overrides kvm_arch_add_vm(). Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- include/linux/kvm_host.h | 2 ++ virt/kvm/kvm_arch.c | 12 +++++++++++- virt/kvm/kvm_main.c | 21 +++++++++++++++++---- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index c38f382f3808..5ae66062620c 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1445,6 +1445,8 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu); bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu); bool kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu); int kvm_arch_post_init_vm(struct kvm *kvm); +int kvm_arch_add_vm(struct kvm *kvm, int usage_count); +int kvm_arch_del_vm(int usage_count); void kvm_arch_pre_destroy_vm(struct kvm *kvm); int kvm_arch_create_vm_debugfs(struct kvm *kvm); =20 diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c index 4748a76bcb03..0eac996f4981 100644 --- a/virt/kvm/kvm_arch.c +++ b/virt/kvm/kvm_arch.c @@ -10,11 +10,21 @@ =20 #include =20 +__weak int kvm_arch_post_init_vm(struct kvm *kvm) +{ + return 0; +} + /* * Called after the VM is otherwise initialized, but just before adding it= to * the vm_list. */ -__weak int kvm_arch_post_init_vm(struct kvm *kvm) +__weak int kvm_arch_add_vm(struct kvm *kvm, int usage_count) +{ + return kvm_arch_post_init_vm(kvm); +} + +__weak int kvm_arch_del_vm(int usage_count) { return 0; } diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 8d08126e6c74..0ba370acff8d 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -144,6 +144,7 @@ static int kvm_no_compat_open(struct inode *inode, stru= ct file *file) #endif static int hardware_enable_all(void); static void hardware_disable_all(void); +static void kvm_del_vm(void); =20 static void kvm_io_bus_destroy(struct kvm_io_bus *bus); =20 @@ -1201,11 +1202,12 @@ static struct kvm *kvm_create_vm(unsigned long type= , const char *fdname) if (r) goto out_err_no_mmu_notifier; =20 - r =3D kvm_arch_post_init_vm(kvm); - if (r) - goto out_err_mmu_notifier; - mutex_lock(&kvm_lock); + r =3D kvm_arch_add_vm(kvm, kvm_usage_count); + if (r) { + mutex_unlock(&kvm_lock); + goto out_err_mmu_notifier; + } list_add(&kvm->vm_list, &vm_list); mutex_unlock(&kvm_lock); =20 @@ -1237,6 +1239,7 @@ static struct kvm *kvm_create_vm(unsigned long type, = const char *fdname) #endif out_err_no_mmu_notifier: hardware_disable_all(); + kvm_del_vm(); out_err_no_disable: kvm_arch_destroy_vm(kvm); out_err_no_arch_destroy_vm: @@ -1316,6 +1319,7 @@ static void kvm_destroy_vm(struct kvm *kvm) kvm_arch_free_vm(kvm); preempt_notifier_dec(); hardware_disable_all(); + kvm_del_vm(); mmdrop(mm); module_put(kvm_chardev_ops.owner); } @@ -5055,6 +5059,15 @@ static void hardware_disable_all(void) cpus_read_unlock(); } =20 +static void kvm_del_vm(void) +{ + cpus_read_lock(); + mutex_lock(&kvm_lock); + kvm_arch_del_vm(kvm_usage_count); + mutex_unlock(&kvm_lock); + cpus_read_unlock(); +} + static int hardware_enable_all(void) { int r =3D 0; --=20 2.25.1 From nobody Fri Apr 10 18:35:27 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 0DDA4C32774 for ; Sat, 20 Aug 2022 06:01:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245533AbiHTGB0 (ORCPT ); Sat, 20 Aug 2022 02:01:26 -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 S244366AbiHTGAw (ORCPT ); Sat, 20 Aug 2022 02:00:52 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B04EA223E; Fri, 19 Aug 2022 23:00:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1660975251; x=1692511251; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=xYGzaGJr0EP3zeyzmub13+Yw5366HEl9Zu7TH0xvoIw=; b=GyHv9vRzZeKZNobw3RWw6GgxSaubgleLTiI8P7t1aKB0iyHoc4P25y50 Rg9+lCH9FhI4fPbC5NgOyNTYuxe2DmXeUlwrsf3Zgf5U+gjOvW4C7oUjV oSqm8NsTdxCv65YhQeVsNh7zsOTnitlavg6GG+/vqDuYj2/JuJT6Uv9xt mlUeP1S7gT6aZLBG+WPUtdl1oKvUPIKRli3hYBF3LOm1xa6sVnt1ijN5d EIZHqbJEU4sAGMIE2FE/mUc0Y0RUT9MjaYBn2Eld1aG+jIl8cVbOe3O5w T8o6G9+FBvwkjDdEcOplGMy03ybWhIfoqGdPMXSrsQNINPuXbAe8YFoGV g==; X-IronPort-AV: E=McAfee;i="6500,9779,10444"; a="379448973" X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="379448973" 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:49 -0700 X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="668857530" 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:49 -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 07/18] KVM: Move out KVM arch PM hooks and hardware enable/disable logic Date: Fri, 19 Aug 2022 23:00:13 -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 To make clear that those files are default implementation that KVM/x86 (and other KVM arch in future) will override them, split out those into a single file. Once conversions for all kvm archs are done, the file will be deleted. kvm_arch_pre_hardware_unsetup() is introduced to avoid cross-arch code churn for now. Once it's settled down, kvm_arch_pre_hardware_unsetup() can be merged into kvm_arch_hardware_unsetup() in each arch code. Signed-off-by: Isaku Yamahata --- include/linux/kvm_host.h | 1 + virt/kvm/kvm_arch.c | 75 ++++++++++++++++++++++++- virt/kvm/kvm_main.c | 115 ++++----------------------------------- 3 files changed, 85 insertions(+), 106 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 5ae66062620c..fdde9c59756d 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1437,6 +1437,7 @@ static inline void kvm_create_vcpu_debugfs(struct kvm= _vcpu *vcpu) {} int kvm_arch_hardware_enable(void); void kvm_arch_hardware_disable(void); int kvm_arch_hardware_setup(void *opaque); +void kvm_arch_pre_hardware_unsetup(void); void kvm_arch_hardware_unsetup(void); int kvm_arch_check_processor_compat(void *opaque); int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu); diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c index 0eac996f4981..d593610edb0a 100644 --- a/virt/kvm/kvm_arch.c +++ b/virt/kvm/kvm_arch.c @@ -6,49 +6,122 @@ * Author: * Isaku Yamahata * + * + * TODO: Delete this file once the conversion of all KVM arch is done. */ =20 #include =20 +static cpumask_t cpus_hardware_enabled =3D CPU_MASK_NONE; +static atomic_t hardware_enable_failed; + __weak int kvm_arch_post_init_vm(struct kvm *kvm) { return 0; } =20 +static void hardware_enable_nolock(void *junk) +{ + int cpu =3D raw_smp_processor_id(); + int r; + + WARN_ON_ONCE(preemptible()); + + if (cpumask_test_cpu(cpu, &cpus_hardware_enabled)) + return; + + cpumask_set_cpu(cpu, &cpus_hardware_enabled); + + r =3D kvm_arch_hardware_enable(); + + if (r) { + cpumask_clear_cpu(cpu, &cpus_hardware_enabled); + atomic_inc(&hardware_enable_failed); + pr_info("kvm: enabling virtualization on CPU%d failed\n", cpu); + } +} + +static void hardware_disable_nolock(void *junk) +{ + int cpu =3D raw_smp_processor_id(); + + WARN_ON_ONCE(preemptible()); + + if (!cpumask_test_cpu(cpu, &cpus_hardware_enabled)) + return; + cpumask_clear_cpu(cpu, &cpus_hardware_enabled); + kvm_arch_hardware_disable(); +} + +__weak void kvm_arch_pre_hardware_unsetup(void) +{ + on_each_cpu(hardware_disable_nolock, NULL, 1); +} + /* * Called after the VM is otherwise initialized, but just before adding it= to * the vm_list. */ __weak int kvm_arch_add_vm(struct kvm *kvm, int usage_count) { - return kvm_arch_post_init_vm(kvm); + int r =3D 0; + + if (usage_count !=3D 1) + return 0; + + atomic_set(&hardware_enable_failed, 0); + on_each_cpu(hardware_enable_nolock, NULL, 1); + + if (atomic_read(&hardware_enable_failed)) { + r =3D -EBUSY; + goto err; + } + + r =3D kvm_arch_post_init_vm(kvm); +err: + if (r) + on_each_cpu(hardware_disable_nolock, NULL, 1); + return r; } =20 __weak int kvm_arch_del_vm(int usage_count) { + if (usage_count) + return 0; + + on_each_cpu(hardware_disable_nolock, NULL, 1); return 0; } =20 __weak int kvm_arch_online_cpu(unsigned int cpu, int usage_count) { + if (usage_count) + hardware_enable_nolock(NULL); return 0; } =20 __weak int kvm_arch_offline_cpu(unsigned int cpu, int usage_count) { + if (usage_count) + hardware_disable_nolock(NULL); return 0; } =20 __weak int kvm_arch_reboot(int val) { + on_each_cpu(hardware_disable_nolock, NULL, 1); return NOTIFY_OK; } =20 __weak int kvm_arch_suspend(int usage_count) { + if (usage_count) + hardware_disable_nolock(NULL); return 0; } =20 __weak void kvm_arch_resume(int usage_count) { + if (usage_count) + hardware_enable_nolock(NULL); } diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 0ba370acff8d..5b8e8addd1e5 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -102,9 +102,7 @@ EXPORT_SYMBOL_GPL(halt_poll_ns_shrink); DEFINE_MUTEX(kvm_lock); LIST_HEAD(vm_list); =20 -static cpumask_var_t cpus_hardware_enabled; static int kvm_usage_count; -static atomic_t hardware_enable_failed; =20 static struct kmem_cache *kvm_vcpu_cache; =20 @@ -142,8 +140,6 @@ static int kvm_no_compat_open(struct inode *inode, stru= ct file *file) #define KVM_COMPAT(c) .compat_ioctl =3D kvm_no_compat_ioctl, \ .open =3D kvm_no_compat_open #endif -static int hardware_enable_all(void); -static void hardware_disable_all(void); static void kvm_del_vm(void); =20 static void kvm_io_bus_destroy(struct kvm_io_bus *bus); @@ -1190,10 +1186,6 @@ static struct kvm *kvm_create_vm(unsigned long type,= const char *fdname) if (r) goto out_err_no_arch_destroy_vm; =20 - r =3D hardware_enable_all(); - if (r) - goto out_err_no_disable; - #ifdef CONFIG_HAVE_KVM_IRQFD INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list); #endif @@ -1202,14 +1194,18 @@ static struct kvm *kvm_create_vm(unsigned long type= , const char *fdname) if (r) goto out_err_no_mmu_notifier; =20 + cpus_read_lock(); mutex_lock(&kvm_lock); + kvm_usage_count++; r =3D kvm_arch_add_vm(kvm, kvm_usage_count); if (r) { + /* the following kvm_del_vm() decrements kvm_usage_count. */ mutex_unlock(&kvm_lock); goto out_err_mmu_notifier; } list_add(&kvm->vm_list, &vm_list); mutex_unlock(&kvm_lock); + cpus_read_unlock(); =20 preempt_notifier_inc(); kvm_init_pm_notifier(kvm); @@ -1238,9 +1234,7 @@ static struct kvm *kvm_create_vm(unsigned long type, = const char *fdname) mmu_notifier_unregister(&kvm->mmu_notifier, current->mm); #endif out_err_no_mmu_notifier: - hardware_disable_all(); kvm_del_vm(); -out_err_no_disable: kvm_arch_destroy_vm(kvm); out_err_no_arch_destroy_vm: WARN_ON_ONCE(!refcount_dec_and_test(&kvm->users_count)); @@ -1318,7 +1312,6 @@ static void kvm_destroy_vm(struct kvm *kvm) cleanup_srcu_struct(&kvm->srcu); kvm_arch_free_vm(kvm); preempt_notifier_dec(); - hardware_disable_all(); kvm_del_vm(); mmdrop(mm); module_put(kvm_chardev_ops.owner); @@ -4988,110 +4981,33 @@ static struct miscdevice kvm_dev =3D { &kvm_chardev_ops, }; =20 -static void hardware_enable_nolock(void *junk) -{ - int cpu =3D raw_smp_processor_id(); - int r; - - WARN_ON_ONCE(preemptible()); - - if (cpumask_test_cpu(cpu, cpus_hardware_enabled)) - return; - - cpumask_set_cpu(cpu, cpus_hardware_enabled); - - r =3D kvm_arch_hardware_enable(); - - if (r) { - cpumask_clear_cpu(cpu, cpus_hardware_enabled); - atomic_inc(&hardware_enable_failed); - pr_info("kvm: enabling virtualization on CPU%d failed\n", cpu); - } -} - static int kvm_starting_cpu(unsigned int cpu) { mutex_lock(&kvm_lock); - if (kvm_usage_count) - hardware_enable_nolock(NULL); (void)kvm_arch_online_cpu(cpu, kvm_usage_count); mutex_unlock(&kvm_lock); return 0; } =20 -static void hardware_disable_nolock(void *junk) -{ - int cpu =3D raw_smp_processor_id(); - - WARN_ON_ONCE(preemptible()); - - if (!cpumask_test_cpu(cpu, cpus_hardware_enabled)) - return; - cpumask_clear_cpu(cpu, cpus_hardware_enabled); - kvm_arch_hardware_disable(); -} - static int kvm_dying_cpu(unsigned int cpu) { mutex_lock(&kvm_lock); - if (kvm_usage_count) - hardware_disable_nolock(NULL); (void)kvm_arch_offline_cpu(cpu, kvm_usage_count); mutex_unlock(&kvm_lock); return 0; } =20 -static void hardware_disable_all_nolock(void) -{ - BUG_ON(!kvm_usage_count); - - kvm_usage_count--; - if (!kvm_usage_count) - on_each_cpu(hardware_disable_nolock, NULL, 1); -} - -static void hardware_disable_all(void) -{ - cpus_read_lock(); - mutex_lock(&kvm_lock); - hardware_disable_all_nolock(); - mutex_unlock(&kvm_lock); - cpus_read_unlock(); -} - static void kvm_del_vm(void) { cpus_read_lock(); mutex_lock(&kvm_lock); + WARN_ON_ONCE(!kvm_usage_count); + kvm_usage_count--; kvm_arch_del_vm(kvm_usage_count); mutex_unlock(&kvm_lock); cpus_read_unlock(); } =20 -static int hardware_enable_all(void) -{ - int r =3D 0; - - cpus_read_lock(); - mutex_lock(&kvm_lock); - - kvm_usage_count++; - if (kvm_usage_count =3D=3D 1) { - atomic_set(&hardware_enable_failed, 0); - on_each_cpu(hardware_enable_nolock, NULL, 1); - - if (atomic_read(&hardware_enable_failed)) { - hardware_disable_all_nolock(); - r =3D -EBUSY; - } - } - - mutex_unlock(&kvm_lock); - cpus_read_unlock(); - - return r; -} - static int kvm_reboot(struct notifier_block *notifier, unsigned long val, void *v) { @@ -5109,7 +5025,6 @@ static int kvm_reboot(struct notifier_block *notifier= , unsigned long val, /* This hook is called without cpuhotplug disabled. */ cpus_read_lock(); mutex_lock(&kvm_lock); - on_each_cpu(hardware_disable_nolock, NULL, 1); r =3D kvm_arch_reboot(val); mutex_unlock(&kvm_lock); cpus_read_unlock(); @@ -5708,17 +5623,13 @@ static int kvm_suspend(void) * locking. */ lockdep_assert_not_held(&kvm_lock); - if (kvm_usage_count) - hardware_disable_nolock(NULL); return kvm_arch_suspend(kvm_usage_count); } =20 static void kvm_resume(void) { - kvm_arch_resume(kvm_usage_count); lockdep_assert_not_held(&kvm_lock); - if (kvm_usage_count) - hardware_enable_nolock(NULL); + kvm_arch_resume(kvm_usage_count); } =20 static struct syscore_ops kvm_syscore_ops =3D { @@ -5864,11 +5775,6 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsig= ned vcpu_align, if (r) goto out_irqfd; =20 - if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) { - r =3D -ENOMEM; - goto out_free_0; - } - r =3D kvm_arch_hardware_setup(opaque); if (r < 0) goto out_free_1; @@ -5947,8 +5853,6 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsign= ed vcpu_align, out_free_2: kvm_arch_hardware_unsetup(); out_free_1: - free_cpumask_var(cpus_hardware_enabled); -out_free_0: kvm_irqfd_exit(); out_irqfd: kvm_arch_exit(); @@ -5970,11 +5874,12 @@ void kvm_exit(void) unregister_syscore_ops(&kvm_syscore_ops); unregister_reboot_notifier(&kvm_reboot_notifier); cpuhp_remove_state_nocalls(CPUHP_AP_KVM_STARTING); - on_each_cpu(hardware_disable_nolock, NULL, 1); + cpus_read_lock(); + kvm_arch_pre_hardware_unsetup(); kvm_arch_hardware_unsetup(); + cpus_read_unlock(); kvm_arch_exit(); kvm_irqfd_exit(); - free_cpumask_var(cpus_hardware_enabled); kvm_vfio_ops_exit(); } EXPORT_SYMBOL_GPL(kvm_exit); --=20 2.25.1 From nobody Fri Apr 10 18:35:27 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 60FBCC32772 for ; Sat, 20 Aug 2022 06:01:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245346AbiHTGBU (ORCPT ); Sat, 20 Aug 2022 02:01:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47466 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244375AbiHTGAw (ORCPT ); Sat, 20 Aug 2022 02:00:52 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 54A6CA260A; Fri, 19 Aug 2022 23:00:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1660975251; x=1692511251; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=a+b7bWmrpg+akx27e5IPdnpc7BaBJM21hcXB7NFDQzI=; b=n0NZrU0PR/YN6KO+NrNplcgMqa1r9no09XuGxgFTGBL5wpSUx0iPVzgc f2qtEbW9VqFShHcR0NSV/HB98louruBPPQ4ALEgSQOmfRdhWIWpFnlzZE L6F4tyItpKqoZhzQ+XYq2Zzrw1ZkYyQnl0WA5Ktt1NTh2uXCYums26EqA /XajkWpa4HafmvZ9YPF4ASlRIugtHfjs3MXc3k1IZxTt63crqrW3dgbQM JJOPKpbY3qgmjcDMWvwcWzBsqAp5H6YAlMCmu5c6OVcoT0atQi+c9tJIW TMuh2uZxFG44TrmHWqv7oXXIj4jRNiJJOQ3mUSBqSsHN/W2W4IiteCo5A A==; X-IronPort-AV: E=McAfee;i="6500,9779,10444"; a="379448974" X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="379448974" 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:49 -0700 X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="668857535" 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:49 -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 08/18] KVM: kvm_arch.c: Remove _nolock post fix Date: Fri, 19 Aug 2022 23:00:14 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata Now all related callbacks are called under kvm_lock, no point for _nolock post fix. Remove _nolock post fix for short function name. Signed-off-by: Isaku Yamahata --- virt/kvm/kvm_arch.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c index d593610edb0a..8a5d88b02aab 100644 --- a/virt/kvm/kvm_arch.c +++ b/virt/kvm/kvm_arch.c @@ -20,7 +20,7 @@ __weak int kvm_arch_post_init_vm(struct kvm *kvm) return 0; } =20 -static void hardware_enable_nolock(void *junk) +static void hardware_enable(void *junk) { int cpu =3D raw_smp_processor_id(); int r; @@ -41,7 +41,7 @@ static void hardware_enable_nolock(void *junk) } } =20 -static void hardware_disable_nolock(void *junk) +static void hardware_disable(void *junk) { int cpu =3D raw_smp_processor_id(); =20 @@ -55,7 +55,7 @@ static void hardware_disable_nolock(void *junk) =20 __weak void kvm_arch_pre_hardware_unsetup(void) { - on_each_cpu(hardware_disable_nolock, NULL, 1); + on_each_cpu(hardware_disable, NULL, 1); } =20 /* @@ -70,7 +70,7 @@ __weak int kvm_arch_add_vm(struct kvm *kvm, int usage_cou= nt) return 0; =20 atomic_set(&hardware_enable_failed, 0); - on_each_cpu(hardware_enable_nolock, NULL, 1); + on_each_cpu(hardware_enable, NULL, 1); =20 if (atomic_read(&hardware_enable_failed)) { r =3D -EBUSY; @@ -80,7 +80,7 @@ __weak int kvm_arch_add_vm(struct kvm *kvm, int usage_cou= nt) r =3D kvm_arch_post_init_vm(kvm); err: if (r) - on_each_cpu(hardware_disable_nolock, NULL, 1); + on_each_cpu(hardware_disable, NULL, 1); return r; } =20 @@ -89,39 +89,39 @@ __weak int kvm_arch_del_vm(int usage_count) if (usage_count) return 0; =20 - on_each_cpu(hardware_disable_nolock, NULL, 1); + on_each_cpu(hardware_disable, NULL, 1); return 0; } =20 __weak int kvm_arch_online_cpu(unsigned int cpu, int usage_count) { if (usage_count) - hardware_enable_nolock(NULL); + hardware_enable(NULL); return 0; } =20 __weak int kvm_arch_offline_cpu(unsigned int cpu, int usage_count) { if (usage_count) - hardware_disable_nolock(NULL); + hardware_disable(NULL); return 0; } =20 __weak int kvm_arch_reboot(int val) { - on_each_cpu(hardware_disable_nolock, NULL, 1); + on_each_cpu(hardware_disable, NULL, 1); return NOTIFY_OK; } =20 __weak int kvm_arch_suspend(int usage_count) { if (usage_count) - hardware_disable_nolock(NULL); + hardware_disable(NULL); return 0; } =20 __weak void kvm_arch_resume(int usage_count) { if (usage_count) - hardware_enable_nolock(NULL); + hardware_enable(NULL); } --=20 2.25.1 From nobody Fri Apr 10 18:35:27 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 0CB45C25B08 for ; Sat, 20 Aug 2022 06:01:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245636AbiHTGBm (ORCPT ); Sat, 20 Aug 2022 02:01:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47540 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244404AbiHTGAw (ORCPT ); Sat, 20 Aug 2022 02:00:52 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A4E2FA262F; Fri, 19 Aug 2022 23:00:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1660975251; x=1692511251; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=aW3CSh7QaSGHtsfiFbSKKW1Df9z424fLpULpazmR+PQ=; b=c7XkvdSEG8flaPB5IPxWY8MFdnTTv0ediaffw4S9VWqzOHCaI/Y65QAi C9RPvCbTl7JGrqygcFJNCGP8eHYQDKxWwnhJM0FRCNshQ1+hINzAzFuc0 Ln4RJcp01ycYd83TzaaebCDq4eUTgNoXqdQwA4Oxf4vsMwA8NZ29CIO0i K3nTNh28pwBGuAI4e9ttbQt4D7MUAoDcW8yejqTS3vcRaqG9/g6t8RQr/ nKOmGhqejufdLFLHXXw47ekYBwKii47nXGzRJPb7ND80bBcnkaSfJ4vW5 PavFzQp2RJBDo0RTZT0XSrH75cb2UabmV0FUuFpftgt6iyq8RmQs/LQyI g==; X-IronPort-AV: E=McAfee;i="6500,9779,10444"; a="379448975" X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="379448975" 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:49 -0700 X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="668857538" 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:49 -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 09/18] KVM: kvm_arch.c: Remove a global variable, hardware_enable_failed Date: Fri, 19 Aug 2022 23:00:15 -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 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 | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c index 8a5d88b02aab..2ed8de0591c9 100644 --- a/virt/kvm/kvm_arch.c +++ b/virt/kvm/kvm_arch.c @@ -13,14 +13,13 @@ #include =20 static cpumask_t cpus_hardware_enabled =3D CPU_MASK_NONE; -static atomic_t hardware_enable_failed; =20 __weak int kvm_arch_post_init_vm(struct kvm *kvm) { return 0; } =20 -static void hardware_enable(void *junk) +static int __hardware_enable(void) { int cpu =3D raw_smp_processor_id(); int r; @@ -28,17 +27,21 @@ static void hardware_enable(void *junk) WARN_ON_ONCE(preemptible()); =20 if (cpumask_test_cpu(cpu, &cpus_hardware_enabled)) - return; - - cpumask_set_cpu(cpu, &cpus_hardware_enabled); - + return 0; r =3D kvm_arch_hardware_enable(); - - if (r) { - cpumask_clear_cpu(cpu, &cpus_hardware_enabled); - atomic_inc(&hardware_enable_failed); + if (r) pr_info("kvm: enabling virtualization on CPU%d failed\n", cpu); - } + else + cpumask_set_cpu(cpu, &cpus_hardware_enabled); + return r; +} + +static void hardware_enable(void *arg) +{ + atomic_t *failed =3D arg; + + if (__hardware_enable()) + atomic_inc(failed); } =20 static void hardware_disable(void *junk) @@ -64,15 +67,16 @@ __weak void kvm_arch_pre_hardware_unsetup(void) */ __weak int kvm_arch_add_vm(struct kvm *kvm, int usage_count) { + atomic_t failed; int r =3D 0; =20 if (usage_count !=3D 1) return 0; =20 - atomic_set(&hardware_enable_failed, 0); - on_each_cpu(hardware_enable, NULL, 1); + atomic_set(&failed, 0); + on_each_cpu(hardware_enable, &failed, 1); =20 - if (atomic_read(&hardware_enable_failed)) { + if (atomic_read(&failed)) { r =3D -EBUSY; goto err; } @@ -96,7 +100,7 @@ __weak int kvm_arch_del_vm(int usage_count) __weak int kvm_arch_online_cpu(unsigned int cpu, int usage_count) { if (usage_count) - hardware_enable(NULL); + return __hardware_enable(); return 0; } =20 @@ -123,5 +127,5 @@ __weak int kvm_arch_suspend(int usage_count) __weak void kvm_arch_resume(int usage_count) { if (usage_count) - hardware_enable(NULL); + (void)__hardware_enable(); } --=20 2.25.1 From nobody Fri Apr 10 18:35:27 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 A093DC25B08 for ; Sat, 20 Aug 2022 06:01:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245702AbiHTGBt (ORCPT ); Sat, 20 Aug 2022 02:01:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47478 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244461AbiHTGAx (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 E9109A284A; Fri, 19 Aug 2022 23:00:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1660975251; x=1692511251; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=t+rLRYDAct7zkLPDlhPK4cPnON71BWnRTU9KRa2JiSQ=; b=hoKulrKt6aqETlbKCxULmk24HZozjqlsMYVgqCC7yLiAdHrBOsLegwaN N6F8ViwG6an+lwt0Xrp8YDXIoWXDb4wWzCPqwDk5L/5CDDSQ9ZRgyGU3K uvwHENMlzCiMrNA0vPh0e6ltgY5F7SG0OKFms37zllxFjGWuYWh1/KhjF uckPxBqYMr8Iv6kvlTsba/yxYk7rTCkt6ZsJs8TKfZsIRTdMFj8IDZYAU 7hgcGH7dYNaB/b0Fb5eG4HVodRd8Ty+9xWqZhqlS9mi9itQw/hGjrEyrD l1RsrcLvUqrtO3uy2RdCEGSAv14HY2hAfWJuSDallA/S4cQ+n/oWxCFxi g==; X-IronPort-AV: E=McAfee;i="6500,9779,10444"; a="379448976" X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="379448976" 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="668857541" 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 10/18] KVM: x86: Move check_processor_compatibility from init ops to runtime ops Date: Fri, 19 Aug 2022 23:00:16 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Chao Gao so that KVM can do compatibility checks on hotplugged CPUs. Drop __init from check_processor_compatibility() and its callees. use a static_call() to invoke .check_processor_compatibility. Opportunistically rename {svm,vmx}_check_processor_compat to conform to the naming convention of fields of kvm_x86_ops. Signed-off-by: Chao Gao Reviewed-by: Sean Christopherson Link: https://lore.kernel.org/r/20220216031528.92558-2-chao.gao@intel.com Signed-off-by: Isaku Yamahata --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/svm/svm.c | 4 ++-- arch/x86/kvm/vmx/vmx.c | 14 +++++++------- arch/x86/kvm/x86.c | 3 +-- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-= x86-ops.h index 51f777071584..3bc45932e2d1 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -129,6 +129,7 @@ KVM_X86_OP(msr_filter_changed) KVM_X86_OP(complete_emulated_msr) KVM_X86_OP(vcpu_deliver_sipi_vector) KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons); +KVM_X86_OP(check_processor_compatibility) =20 #undef KVM_X86_OP #undef KVM_X86_OP_OPTIONAL diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 5ffa578cafe1..f23d9852b07a 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1445,6 +1445,7 @@ static inline u16 kvm_lapic_irq_dest_mode(bool dest_m= ode_logical) struct kvm_x86_ops { const char *name; =20 + int (*check_processor_compatibility)(void); int (*hardware_enable)(void); void (*hardware_disable)(void); void (*hardware_unsetup)(void); @@ -1655,7 +1656,6 @@ struct kvm_x86_nested_ops { struct kvm_x86_init_ops { int (*cpu_has_kvm_support)(void); int (*disabled_by_bios)(void); - int (*check_processor_compatibility)(void); int (*hardware_setup)(void); unsigned int (*handle_intel_pt_intr)(void); =20 diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index f3813dbacb9f..371300f03f55 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4134,7 +4134,7 @@ svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned c= har *hypercall) hypercall[2] =3D 0xd9; } =20 -static int __init svm_check_processor_compat(void) +static int svm_check_processor_compatibility(void) { return 0; } @@ -4740,6 +4740,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata =3D { .name =3D "kvm_amd", =20 .hardware_unsetup =3D svm_hardware_unsetup, + .check_processor_compatibility =3D svm_check_processor_compatibility, .hardware_enable =3D svm_hardware_enable, .hardware_disable =3D svm_hardware_disable, .has_emulated_msr =3D svm_has_emulated_msr, @@ -5122,7 +5123,6 @@ static struct kvm_x86_init_ops svm_init_ops __initdat= a =3D { .cpu_has_kvm_support =3D has_svm, .disabled_by_bios =3D is_disabled, .hardware_setup =3D svm_hardware_setup, - .check_processor_compatibility =3D svm_check_processor_compat, =20 .runtime_ops =3D &svm_x86_ops, .pmu_ops =3D &amd_pmu_ops, diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index d7f8331d6f7e..3cf7f18a4115 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2495,8 +2495,8 @@ static bool cpu_has_sgx(void) return cpuid_eax(0) >=3D 0x12 && (cpuid_eax(0x12) & BIT(0)); } =20 -static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, - u32 msr, u32 *result) +static int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, + u32 msr, u32 *result) { u32 vmx_msr_low, vmx_msr_high; u32 ctl =3D ctl_min | ctl_opt; @@ -2514,7 +2514,7 @@ static __init int adjust_vmx_controls(u32 ctl_min, u3= 2 ctl_opt, return 0; } =20 -static __init u64 adjust_vmx_controls64(u64 ctl_opt, u32 msr) +static u64 adjust_vmx_controls64(u64 ctl_opt, u32 msr) { u64 allowed; =20 @@ -2523,8 +2523,8 @@ static __init u64 adjust_vmx_controls64(u64 ctl_opt, = u32 msr) return ctl_opt & allowed; } =20 -static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf, - struct vmx_capability *vmx_cap) +static int setup_vmcs_config(struct vmcs_config *vmcs_conf, + struct vmx_capability *vmx_cap) { u32 vmx_msr_low, vmx_msr_high; u32 min, opt, min2, opt2; @@ -7417,7 +7417,7 @@ static int vmx_vm_init(struct kvm *kvm) return 0; } =20 -static int __init vmx_check_processor_compat(void) +static int vmx_check_processor_compatibility(void) { struct vmcs_config vmcs_conf; struct vmx_capability vmx_cap; @@ -8015,6 +8015,7 @@ static struct kvm_x86_ops vmx_x86_ops __initdata =3D { =20 .hardware_unsetup =3D vmx_hardware_unsetup, =20 + .check_processor_compatibility =3D vmx_check_processor_compatibility, .hardware_enable =3D vmx_hardware_enable, .hardware_disable =3D vmx_hardware_disable, .has_emulated_msr =3D vmx_has_emulated_msr, @@ -8404,7 +8405,6 @@ static __init int hardware_setup(void) static struct kvm_x86_init_ops vmx_init_ops __initdata =3D { .cpu_has_kvm_support =3D cpu_has_kvm_support, .disabled_by_bios =3D vmx_disabled_by_bios, - .check_processor_compatibility =3D vmx_check_processor_compat, .hardware_setup =3D hardware_setup, .handle_intel_pt_intr =3D NULL, =20 diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index a7ab9984a236..890985c284be 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12002,7 +12002,6 @@ void kvm_arch_hardware_unsetup(void) int kvm_arch_check_processor_compat(void *opaque) { struct cpuinfo_x86 *c =3D &cpu_data(smp_processor_id()); - struct kvm_x86_init_ops *ops =3D opaque; =20 WARN_ON(!irqs_disabled()); =20 @@ -12010,7 +12009,7 @@ int kvm_arch_check_processor_compat(void *opaque) __cr4_reserved_bits(cpu_has, &boot_cpu_data)) return -EIO; =20 - return ops->check_processor_compatibility(); + return static_call(kvm_x86_check_processor_compatibility)(); } =20 bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu) --=20 2.25.1 From nobody Fri Apr 10 18:35:27 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 9851EC25B08 for ; Sat, 20 Aug 2022 06:02:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244589AbiHTGCD (ORCPT ); Sat, 20 Aug 2022 02:02:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47516 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244457AbiHTGAx (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 05A99A2237; 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=1660975251; x=1692511251; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=3nRzOljyClKkdxyoueQO+Gp/x0PfqUJfsGG+3EL3avA=; b=lRSMnJKxvlX4T+hOnzkrowS04Z4BqqICbY+0YJnti1n/uJV134cH8eW2 aBQ/jXzsjum0i3HlW3YQG+Cck3FGu5qXTgIwQAoyhpn2SpYNGu9BsH0Rs EG7L8/UDbMKtHivFESYlP+EDA1Q3mRqw5dZGW9lTPS344B23FjWo6Jt46 bFgx1QloSfIUCZqi2ckJczH6hYkOFA6kxdQrb+9NYRK2Jvxxr0Y7kgUaD 2hrazAEBTtk0KDHaMJluVGCFOUKfD3Bpn7qBvmupQ5HT5Lq/KWFWKCsv7 583dgcpIupjYIyvFP1XcR5sWYzBaUROVGDPr/L326jX3Ajtg/whlsmVpw w==; X-IronPort-AV: E=McAfee;i="6500,9779,10444"; a="379448978" X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="379448978" 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="668857544" 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 , Suzuki K Poulose , Anup Patel , Claudio Imbrenda Subject: [RFC PATCH 11/18] Partially revert "KVM: Pass kvm_init()'s opaque param to additional arch funcs" Date: Fri, 19 Aug 2022 23:00:17 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Chao Gao This partially reverts commit b99040853738 ("KVM: Pass kvm_init()'s opaque param to additional arch funcs") remove opaque from kvm_arch_check_processor_compat because no one uses this opaque now. Address conflicts for ARM (due to file movement) and manually handle RISC-V which comes after the commit. The change about kvm_arch_hardware_setup() in original commit are still needed so they are not reverted. The current implementation enables hardware (e.g. enable VMX on all CPUs), arch-specific initialization for VM creation, and disables hardware (in x86, disable VMX on all CPUs) for last VM destruction. TDX requires its initialization on loading KVM module with VMX enabled on all available CPUs. It needs to enable/disable hardware on module initialization. To reuse the same logic, one way is to pass around the unused opaque argument, another way is to remove the unused opaque argument. This patch is a preparation for the latter by removing the argument. 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 --- 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 986cee6fbc7f..e66dacfe7f74 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -68,7 +68,7 @@ int kvm_arch_hardware_setup(void *opaque) return 0; } =20 -int kvm_arch_check_processor_compat(void *opaque) +int kvm_arch_check_processor_compat(void) { return 0; } diff --git a/arch/mips/kvm/mips.c b/arch/mips/kvm/mips.c index a25e0b73ee70..092d09fb6a7e 100644 --- a/arch/mips/kvm/mips.c +++ b/arch/mips/kvm/mips.c @@ -140,7 +140,7 @@ int kvm_arch_hardware_setup(void *opaque) return 0; } =20 -int kvm_arch_check_processor_compat(void *opaque) +int kvm_arch_check_processor_compat(void) { return 0; } diff --git a/arch/powerpc/kvm/powerpc.c b/arch/powerpc/kvm/powerpc.c index fb1490761c87..7b56d6ccfdfb 100644 --- a/arch/powerpc/kvm/powerpc.c +++ b/arch/powerpc/kvm/powerpc.c @@ -447,7 +447,7 @@ int kvm_arch_hardware_setup(void *opaque) return 0; } =20 -int kvm_arch_check_processor_compat(void *opaque) +int kvm_arch_check_processor_compat(void) { return kvmppc_core_check_processor_compat(); } diff --git a/arch/riscv/kvm/main.c b/arch/riscv/kvm/main.c index 1549205fe5fe..f8d6372d208f 100644 --- a/arch/riscv/kvm/main.c +++ b/arch/riscv/kvm/main.c @@ -20,7 +20,7 @@ long kvm_arch_dev_ioctl(struct file *filp, return -EINVAL; } =20 -int kvm_arch_check_processor_compat(void *opaque) +int kvm_arch_check_processor_compat(void) { return 0; } diff --git a/arch/s390/kvm/kvm-s390.c b/arch/s390/kvm/kvm-s390.c index edfd4bbd0cba..e26d4dd85668 100644 --- a/arch/s390/kvm/kvm-s390.c +++ b/arch/s390/kvm/kvm-s390.c @@ -254,7 +254,7 @@ int kvm_arch_hardware_enable(void) return 0; } =20 -int kvm_arch_check_processor_compat(void *opaque) +int kvm_arch_check_processor_compat(void) { return 0; } diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 890985c284be..0b112cd7de58 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11999,7 +11999,7 @@ void kvm_arch_hardware_unsetup(void) static_call(kvm_x86_hardware_unsetup)(); } =20 -int kvm_arch_check_processor_compat(void *opaque) +int kvm_arch_check_processor_compat(void) { struct cpuinfo_x86 *c =3D &cpu_data(smp_processor_id()); =20 diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index fdde9c59756d..9584500eb4fa 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1439,7 +1439,7 @@ void kvm_arch_hardware_disable(void); int kvm_arch_hardware_setup(void *opaque); void kvm_arch_pre_hardware_unsetup(void); void kvm_arch_hardware_unsetup(void); -int kvm_arch_check_processor_compat(void *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 5b8e8addd1e5..9d917e3e47a9 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -5741,22 +5741,14 @@ void kvm_unregister_perf_callbacks(void) } #endif =20 -struct kvm_cpu_compat_check { - void *opaque; - int *ret; -}; - -static void check_processor_compat(void *data) +static void check_processor_compat(void *rtn) { - struct kvm_cpu_compat_check *c =3D data; - - *c->ret =3D kvm_arch_check_processor_compat(c->opaque); + *(int *)rtn =3D kvm_arch_check_processor_compat(); } =20 int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, struct module *module) { - struct kvm_cpu_compat_check c; int r; int cpu; =20 @@ -5779,10 +5771,8 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsig= ned vcpu_align, if (r < 0) goto out_free_1; =20 - c.ret =3D &r; - c.opaque =3D opaque; for_each_online_cpu(cpu) { - smp_call_function_single(cpu, check_processor_compat, &c, 1); + smp_call_function_single(cpu, check_processor_compat, &r, 1); if (r < 0) goto out_free_2; } --=20 2.25.1 From nobody Fri Apr 10 18:35:27 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 17549C25B08 for ; Sat, 20 Aug 2022 06:01:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244404AbiHTGBp (ORCPT ); Sat, 20 Aug 2022 02:01:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47466 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244490AbiHTGAx (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 89364A223E; 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=iF07MtJ4WwZr9SnrFNX6veyTtKvMyfVQ+1Bb5LokAFM=; b=h8EIFz7czho/0F4UB/cLeZTT+CuA2gOAvjNS2Hyxiu8hORB6J4vT+FTu 3B/nrhX3OdNXY4G7VLB4Qt/pr9DxrklrFTihKcQx2rWiY9RKYxnBAi3K+ pHF2tJjP3b8jupA4/c1gWHZ/laKNptnv0dnzNb5u8XcFuSm3nndiFzThQ /l76Dvx2dtKhfmPB28PGLMHeNaWGtAdQmxfuUkBJbBk5mCyR9xQERuvCr l2+xe/debF6Cg3zD8eIEQBdFCyba3VS1hhZoQH9JSZV1Wgk4e6PAAgABt E4oQcHpSpMar4Yf5IanXR9XMZU8ISzv3pyoe1HeRs/lcZYkGJWK0HuZ+5 A==; X-IronPort-AV: E=McAfee;i="6500,9779,10444"; a="379448979" X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="379448979" 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="668857547" 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 12/18] KVM: Do processor compatibility check on cpu online and resume Date: Fri, 19 Aug 2022 23:00:18 -0700 Message-Id: <60f9ec74499c673c474e9d909c2f3176bc6711c3.1660974106.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata So far the processor compatibility check is not done for newly added CPU. It should be done. Signed-off-by: Isaku Yamahata --- virt/kvm/kvm_arch.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c index 2ed8de0591c9..20971f43df95 100644 --- a/virt/kvm/kvm_arch.c +++ b/virt/kvm/kvm_arch.c @@ -99,9 +99,15 @@ __weak int kvm_arch_del_vm(int usage_count) =20 __weak int kvm_arch_online_cpu(unsigned int cpu, int usage_count) { - if (usage_count) - return __hardware_enable(); - return 0; + int r; + + if (!usage_count) + return 0; + + r =3D kvm_arch_check_processor_compat(); + if (r) + return r; + return __hardware_enable(); } =20 __weak int kvm_arch_offline_cpu(unsigned int cpu, int usage_count) @@ -126,6 +132,10 @@ __weak int kvm_arch_suspend(int usage_count) =20 __weak void kvm_arch_resume(int usage_count) { - if (usage_count) - (void)__hardware_enable(); + if (!usage_count) + return; + + if (kvm_arch_check_processor_compat()) + return; /* FIXME: disable KVM */ + (void)__hardware_enable(); } --=20 2.25.1 From nobody Fri Apr 10 18:35:27 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 From nobody Fri Apr 10 18:35:27 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 6C0C9C25B08 for ; Sat, 20 Aug 2022 06:02:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245748AbiHTGCF (ORCPT ); Sat, 20 Aug 2022 02:02:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47540 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244516AbiHTGAx (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 F1B07A260F; 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=1660975253; x=1692511253; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=XOa/Zkdq2+BEEU85FVBPfCIeeCZZ/VaM1Oss3PEpGzE=; b=UMloBvlnEcmZlkR5O7EZ4lbdUsyc9xcgf7oAeuMvWqcCJilE1KH5RGIk hAu0f5mqlJuXRfrRlIxWZO3HGUpdvD1H8WGYMTIBLacMHy+ZT9LGN3fji w8+YLGdOxw4TqOy4ahXGBldYujz/O65hKLNrpi4VGesf3iRE3Er4pEKgo ar/VPGzPdPgb6k9rQpoW5yw6HVR25kfA7CVX+Bfo1HHtA54NS1HOFG0vw Jt+0B/ebB80J63z7vIrhBCuS2hsd8EOZS3/c4X4P+YgZro/LKAhHil2p0 rG5dyn/Ll1VG9qRJcFb72yRCqjUHDytrk9M2QeiydX5Z6V7bplfZjqtIu A==; X-IronPort-AV: E=McAfee;i="6500,9779,10444"; a="379448981" X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="379448981" 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="668857555" 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 14/18] KVM: Eliminate kvm_arch_post_init_vm() Date: Fri, 19 Aug 2022 23:00:20 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata Now kvm_arch_post_init_vm() is used only by x86 kvm_arch_add_vm(). Other arch doesn't define it. Merge x86 kvm_arch_post_init_vm() int x86 kvm_arch_add_vm() and eliminate kvm_arch_post_init_vm(). Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- arch/x86/kvm/x86.c | 7 +------ include/linux/kvm_host.h | 1 - virt/kvm/kvm_arch.c | 12 +----------- 3 files changed, 2 insertions(+), 18 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 71e90d0f0da9..a1e8d15aa6b8 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11843,11 +11843,6 @@ void kvm_arch_hardware_disable(void) =20 static cpumask_t cpus_hardware_enabled =3D CPU_MASK_NONE; =20 -int kvm_arch_post_init_vm(struct kvm *kvm) -{ - return kvm_mmu_post_init_vm(kvm); -} - static int __hardware_enable(void) { int cpu =3D raw_smp_processor_id(); @@ -11910,7 +11905,7 @@ int kvm_arch_add_vm(struct kvm *kvm, int usage_coun= t) goto err; } =20 - r =3D kvm_arch_post_init_vm(kvm); + r =3D kvm_mmu_post_init_vm(kvm); err: if (r && usage_count =3D=3D 1) on_each_cpu(hardware_disable, NULL, 1); diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 9584500eb4fa..7983744addbf 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1445,7 +1445,6 @@ bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu); int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu); bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu); bool kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu); -int kvm_arch_post_init_vm(struct kvm *kvm); int kvm_arch_add_vm(struct kvm *kvm, int usage_count); int kvm_arch_del_vm(int usage_count); void kvm_arch_pre_destroy_vm(struct kvm *kvm); diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c index 20971f43df95..94dd57bbc8bd 100644 --- a/virt/kvm/kvm_arch.c +++ b/virt/kvm/kvm_arch.c @@ -14,11 +14,6 @@ =20 static cpumask_t cpus_hardware_enabled =3D CPU_MASK_NONE; =20 -__weak int kvm_arch_post_init_vm(struct kvm *kvm) -{ - return 0; -} - static int __hardware_enable(void) { int cpu =3D raw_smp_processor_id(); @@ -78,13 +73,8 @@ __weak int kvm_arch_add_vm(struct kvm *kvm, int usage_co= unt) =20 if (atomic_read(&failed)) { r =3D -EBUSY; - goto err; - } - - r =3D kvm_arch_post_init_vm(kvm); -err: - if (r) on_each_cpu(hardware_disable, NULL, 1); + } return r; } =20 --=20 2.25.1 From nobody Fri Apr 10 18:35:27 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 38CD6C25B08 for ; Sat, 20 Aug 2022 06:02:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343496AbiHTGCA (ORCPT ); Sat, 20 Aug 2022 02:02:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47588 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244589AbiHTGAy (ORCPT ); Sat, 20 Aug 2022 02:00:54 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 366BFA2628; Fri, 19 Aug 2022 23:00:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1660975253; x=1692511253; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=IHSa0WBYx0BgB2/RDKlw4uOS3JdT2/kSZliV76NwxJ8=; b=k3VcEZhkz9WFbeBZbbo9MnLW1HNtDZZoSrf11hfSqVdAa58Wa/qLbIVD z7Xaa0Z9NWalueumBGYAUISGDGUO6PpWN3vWlJcpc9N0r5Frfb//8Ey19 grRzJY0+ePrfUngwGTAq9bGM3pu07pvh4itJqXgABMsAx3UPTRDNZP4i2 Mn1p2XzqAwYCe3jrYpuv2ZDhiPM1FuqnmvoLH/AEKCKZhAjZ84umJ31uf WO2OU/B/FSx+trV84I+uo4PbZ9r1mQEx7YZJeqjtB2QczR+rMPKAO/HyP p7NuueULFHa1jP5q4qFylR+Np9NDfiY30/O3D2Pom2Ja8XA9OK35/pXxq A==; X-IronPort-AV: E=McAfee;i="6500,9779,10444"; a="379448982" X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="379448982" 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:51 -0700 X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="668857558" 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 15/18] KVM: x86: Delete kvm_arch_hardware_enable/disable() Date: Fri, 19 Aug 2022 23:00:21 -0700 Message-Id: <49aa53f11283b826ccbca3755e2548464b7f8db7.1660974106.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata Now they're function call and there is no point to keep them. Opportunistically make kvm_arch_pre_hardware_unsetup() empty. Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- arch/x86/kvm/x86.c | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index a1e8d15aa6b8..5aa6d5308ee8 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -354,7 +354,7 @@ static void kvm_on_user_return(struct user_return_notif= ier *urn) =20 /* * Disabling irqs at this point since the following code could be - * interrupted and executed through kvm_arch_hardware_disable() + * interrupted and executed through hardware_disable() */ local_irq_save(flags); if (msrs->registered) { @@ -11830,17 +11830,6 @@ void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu = *vcpu, u8 vector) } EXPORT_SYMBOL_GPL(kvm_vcpu_deliver_sipi_vector); =20 -int kvm_arch_hardware_enable(void) -{ - return static_call(kvm_x86_hardware_enable)(); -} - -void kvm_arch_hardware_disable(void) -{ - static_call(kvm_x86_hardware_disable)(); - drop_user_return_notifiers(); -} - static cpumask_t cpus_hardware_enabled =3D CPU_MASK_NONE; =20 static int __hardware_enable(void) @@ -11852,7 +11841,7 @@ static int __hardware_enable(void) =20 if (cpumask_test_cpu(cpu, &cpus_hardware_enabled)) return 0; - r =3D kvm_arch_hardware_enable(); + r =3D static_call(kvm_x86_hardware_enable)(); if (r) pr_info("kvm: enabling virtualization on CPU%d failed\n", cpu); else @@ -11877,12 +11866,13 @@ static void hardware_disable(void *junk) if (!cpumask_test_cpu(cpu, &cpus_hardware_enabled)) return; cpumask_clear_cpu(cpu, &cpus_hardware_enabled); - kvm_arch_hardware_disable(); + static_call(kvm_x86_hardware_disable)(); + drop_user_return_notifiers(); } =20 void kvm_arch_pre_hardware_unsetup(void) { - on_each_cpu(hardware_disable, NULL, 1); + /* TODO: eliminate this function */ } =20 /* @@ -11968,7 +11958,7 @@ void kvm_arch_resume(int usage_count) =20 if (kvm_arch_check_processor_compat()) return; - if (kvm_arch_hardware_enable()) + if (static_call(kvm_x86_hardware_enable)()) return; =20 local_tsc =3D rdtsc(); @@ -12109,6 +12099,8 @@ int kvm_arch_hardware_setup(void *opaque) =20 void kvm_arch_hardware_unsetup(void) { + on_each_cpu(hardware_disable, NULL, 1); + kvm_unregister_perf_callbacks(); =20 static_call(kvm_x86_hardware_unsetup)(); --=20 2.25.1 From nobody Fri Apr 10 18:35:27 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 6F891C25B08 for ; Sat, 20 Aug 2022 06:02:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343652AbiHTGCY (ORCPT ); Sat, 20 Aug 2022 02:02:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47602 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229458AbiHTGAy (ORCPT ); Sat, 20 Aug 2022 02:00:54 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AACDEA262F; Fri, 19 Aug 2022 23:00:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1660975253; x=1692511253; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=HrmPC8RfziJsITYc01VGOv40y/+qnaX0p4MPPXpFpbU=; b=H8MVAx+wzWKKfOoSCS7uZf2MuGWjq5jobORGyQQpJIc72pn5B4pCUSSA sj10wEUtEkn7FR+TvZ/V5XkNVcqpL3qh2zY/bu7tRis0wQmelWVxA1EqF cr8XrCG8AXFQKBysRwZBC1Fx6kpc/vsr+snXUAx9+5BkPG6uYrkQQkD4U /f9YdvNydxGH57iF6TQ5RWAAhq3mU9qXur3cTGvWx0U+yhuBUdirFfyA5 YqGW+8GrsQHyuNpEk+0ZuaK9DBOlSv7KU28Db6lVAP5XqXz2RFo/Iwutb YMr2FImytgte5YetG98aPErbywf3zBubbsDud/T7Db9QdlNVt1j93mYvT Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10444"; a="379448983" X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="379448983" 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:51 -0700 X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="668857561" 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:51 -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 16/18] KVM: Add config to not compile kvm_arch.c Date: Fri, 19 Aug 2022 23:00:22 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata So that kvm_arch_hardware_enable/disable() aren't defined. Once the conversion of all KVM archs is done, this config and kvm_arch.c should be removed. Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- arch/x86/kvm/Kconfig | 1 + include/linux/kvm_host.h | 2 ++ virt/kvm/Kconfig | 3 +++ virt/kvm/Makefile.kvm | 5 ++++- 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig index e3cbd7706136..e2e16205425d 100644 --- a/arch/x86/kvm/Kconfig +++ b/arch/x86/kvm/Kconfig @@ -25,6 +25,7 @@ config KVM depends on X86_LOCAL_APIC select PREEMPT_NOTIFIERS select MMU_NOTIFIER + select HAVE_KVM_OVERRIDE_HARDWARE_ENABLE select HAVE_KVM_IRQCHIP select HAVE_KVM_PFNCACHE select HAVE_KVM_IRQFD diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 7983744addbf..1a05438ef33f 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1434,8 +1434,10 @@ void kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *v= cpu, struct dentry *debugfs_ static inline void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu) {} #endif =20 +#ifndef CONFIG_HAVE_KVM_OVERRIDE_HARDWARE_ENABLE int kvm_arch_hardware_enable(void); void kvm_arch_hardware_disable(void); +#endif int kvm_arch_hardware_setup(void *opaque); void kvm_arch_pre_hardware_unsetup(void); void kvm_arch_hardware_unsetup(void); diff --git a/virt/kvm/Kconfig b/virt/kvm/Kconfig index a8c5c9f06b3c..917314a87696 100644 --- a/virt/kvm/Kconfig +++ b/virt/kvm/Kconfig @@ -72,3 +72,6 @@ config KVM_XFER_TO_GUEST_WORK =20 config HAVE_KVM_PM_NOTIFIER bool + +config HAVE_KVM_OVERRIDE_HARDWARE_ENABLE + def_bool n diff --git a/virt/kvm/Makefile.kvm b/virt/kvm/Makefile.kvm index c4210acabd35..c0187ec4f83c 100644 --- a/virt/kvm/Makefile.kvm +++ b/virt/kvm/Makefile.kvm @@ -5,7 +5,10 @@ =20 KVM ?=3D ../../../virt/kvm =20 -kvm-y :=3D $(KVM)/kvm_main.o $(KVM)/kvm_arch.o $(KVM)/eventfd.o $(KVM)/bin= ary_stats.o +kvm-y :=3D $(KVM)/kvm_main.o $(KVM)/eventfd.o $(KVM)/binary_stats.o +ifneq ($(CONFIG_HAVE_KVM_OVERRIDE_HARDWARE_ENABLE), y) +kvm-y +=3D $(KVM)/kvm_arch.o +endif kvm-$(CONFIG_KVM_VFIO) +=3D $(KVM)/vfio.o kvm-$(CONFIG_KVM_MMIO) +=3D $(KVM)/coalesced_mmio.o kvm-$(CONFIG_KVM_ASYNC_PF) +=3D $(KVM)/async_pf.o --=20 2.25.1 From nobody Fri Apr 10 18:35:27 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 27AABC25B08 for ; Sat, 20 Aug 2022 06:02:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343559AbiHTGCL (ORCPT ); Sat, 20 Aug 2022 02:02:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47604 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244618AbiHTGAy (ORCPT ); Sat, 20 Aug 2022 02:00:54 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE84EA287E; Fri, 19 Aug 2022 23:00:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1660975253; x=1692511253; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=k5e1P3AKVcgtCAeIF9Lz4zIaHf3w2yygo+9il05lYr4=; b=SMEehBLMSA+4yoWH1wiYWsdtvyqGzo4ASPsoRzcDmwK+Uv70iTh2CxxN y3UTBl3XRujWXUMvxIuSGgBr/Pxsel9Q0jUHheKApjcVe8aCimpVw5fgL xbvc9TTsjOiueTdNZ7ZbdVK5FCFrmLUO8xUOwpJBNT6yrmKOcNdK6IsMA j/PyKP3hlZYZpf70qLu8p8cdHyEGo95a9jL8tlOf5ZlwnHcFqdp/0HGUS K0NjdWBhXy7aXzJ6wicYQiM4MabZBBN3pn5qFv0u4OQ4G2celOKFaI5ic 6PQba/AuJgUt+vj026KLLnalvN9oPHw2O83jFDRkujF4IGDzz2S9nXZEX w==; X-IronPort-AV: E=McAfee;i="6500,9779,10444"; a="379448984" X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="379448984" 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:51 -0700 X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="668857565" 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:51 -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 17/18] KVM: x86: Remove cpus_hardware_enabled and related sanity check Date: Fri, 19 Aug 2022 23:00:23 -0700 Message-Id: <305ce2cbf6bc01736091f7e7ef1302fb994a48ad.1660974106.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata cpus_hardware_enabled mask seems incomplete protection against other kernel component using CPU virtualization feature. Because it's obscure and incomplete, remove the check. Signed-off-by: Isaku Yamahata --- 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 5aa6d5308ee8..f43caa8919ea 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11830,22 +11830,15 @@ void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu= *vcpu, u8 vector) } EXPORT_SYMBOL_GPL(kvm_vcpu_deliver_sipi_vector); =20 -static cpumask_t cpus_hardware_enabled =3D CPU_MASK_NONE; - static int __hardware_enable(void) { - int cpu =3D raw_smp_processor_id(); int r; =20 WARN_ON_ONCE(preemptible()); =20 - if (cpumask_test_cpu(cpu, &cpus_hardware_enabled)) - return 0; r =3D static_call(kvm_x86_hardware_enable)(); if (r) - pr_info("kvm: enabling virtualization on CPU%d failed\n", cpu); - else - cpumask_set_cpu(cpu, &cpus_hardware_enabled); + pr_info("kvm: enabling virtualization on CPU%d failed\n", smp_processor_= id()); return r; } =20 @@ -11859,13 +11852,7 @@ static void hardware_enable(void *arg) =20 static void hardware_disable(void *junk) { - int cpu =3D raw_smp_processor_id(); - WARN_ON_ONCE(preemptible()); - - if (!cpumask_test_cpu(cpu, &cpus_hardware_enabled)) - return; - cpumask_clear_cpu(cpu, &cpus_hardware_enabled); static_call(kvm_x86_hardware_disable)(); drop_user_return_notifiers(); } --=20 2.25.1 From nobody Fri Apr 10 18:35:27 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 6A850C25B08 for ; Sat, 20 Aug 2022 06:02:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343583AbiHTGCR (ORCPT ); Sat, 20 Aug 2022 02:02:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47606 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244609AbiHTGAy (ORCPT ); Sat, 20 Aug 2022 02:00:54 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 497F8A344F; Fri, 19 Aug 2022 23:00:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1660975254; x=1692511254; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=u4ijRhNdXBygE+nv8mY+JqN2kz1+O4efqM0hIzJAU60=; b=Z7QVp+LHwcHYj5ohuC0c0l4ze+PiwXvUYJxrXB7OK87ywSNotUcAmk9Y nqBOb8xNRb6RyjEMyLRcCnZSk1MxoOJ4DjiAFcjQxeWjohOTCYwHRl0vk 2Xy+iMT7IAglhMDfp+a4d2PVTDELDEgzpGpKeVy39Tlx+GNLuy7eArV/y B5WqjNWGUvmrqt2N26mIt/CiOMViH3j6rcR/jR6jYEOwvHlAap4Xb2fNB EIDbZnBY6ZtX4/dA7zb6Fhb/DZUimBOotZlKtqtoQxeAaUC+JlViY17GD TzPRpVoZzkO+rZlRkIwFNvrzVtCuGW17de+msB/8DKtgH8Pe7Gd51k/Ua A==; X-IronPort-AV: E=McAfee;i="6500,9779,10444"; a="379448986" X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="379448986" 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:51 -0700 X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="668857568" 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:51 -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 18/18] KVM: Remove cpus_hardware_enabled and related sanity check Date: Fri, 19 Aug 2022 23:00:24 -0700 Message-Id: <5dc935a4f03af56a38128701847935afe676307c.1660974107.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata cpus_hardware_enabled mask seems incomplete protection against other kernel component using CPU virtualization feature. Because it's obscure and incomplete, remove the check. Signed-off-by: Isaku Yamahata --- virt/kvm/kvm_arch.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c index 94dd57bbc8bd..03946321a21c 100644 --- a/virt/kvm/kvm_arch.c +++ b/virt/kvm/kvm_arch.c @@ -12,22 +12,16 @@ =20 #include =20 -static cpumask_t cpus_hardware_enabled =3D CPU_MASK_NONE; - static int __hardware_enable(void) { - int cpu =3D raw_smp_processor_id(); int r; =20 WARN_ON_ONCE(preemptible()); =20 - if (cpumask_test_cpu(cpu, &cpus_hardware_enabled)) - return 0; r =3D kvm_arch_hardware_enable(); if (r) - pr_info("kvm: enabling virtualization on CPU%d failed\n", cpu); - else - cpumask_set_cpu(cpu, &cpus_hardware_enabled); + pr_info("kvm: enabling virtualization on CPU%d failed\n", + smp_processor_id()); return r; } =20 @@ -41,13 +35,7 @@ static void hardware_enable(void *arg) =20 static void hardware_disable(void *junk) { - int cpu =3D raw_smp_processor_id(); - WARN_ON_ONCE(preemptible()); - - if (!cpumask_test_cpu(cpu, &cpus_hardware_enabled)) - return; - cpumask_clear_cpu(cpu, &cpus_hardware_enabled); kvm_arch_hardware_disable(); } =20 --=20 2.25.1