mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Huang, Kai" <kai.huang@intel.com>
To: "kvm@vger.kernel.org" <kvm@vger.kernel.org>,
	"pbonzini@redhat.com" <pbonzini@redhat.com>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 3/3] KVM: VMX: Initialize TDX during KVM module load
Date: Thu, 28 Nov 2024 03:34:24 +0000	[thread overview]
Message-ID: <7109da789f52dac408f1fb9e0440ad418af6e3ab.camel@intel.com> (raw)
In-Reply-To: <20241127201019.136086-4-pbonzini@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 3323 bytes --]

Hi Paolo,

Thanks for doing this!

> +
> +static void __do_tdx_cleanup(void)
> +{
> +	/*
> +	 * Once TDX module is initialized, it cannot be disabled and
> +	 * re-initialized again w/o runtime update (which isn't
> +	 * supported by kernel).  Only need to remove the cpuhp here.
> +	 * The TDX host core code tracks TDX status and can handle
> +	 * 'multiple enabling' scenario.
> +	 */
> +	WARN_ON_ONCE(!tdx_cpuhp_state);
> +	cpuhp_remove_state_nocalls(tdx_cpuhp_state);
> +	tdx_cpuhp_state = 0;
> +}

As Gao, Chao pointed out, there's bug here since it is also called by
__do_tdx_bringup() which is called with cpus_read_lock() hold.  We need the
_cpuslocked() version here.

I pasted the fixup at the bottom of this reply for your reference and please
merge if it is fine to you.  The diff is also attached if that's easier.


[...]

> +int __init tdx_bringup(void)
> +{
> +	int r;
> +
> +	if (!enable_tdx)
> +		return 0;
> +
> +	if (!kvm_can_support_tdx()) {
> +		pr_err("tdx: no TDX private KeyIDs available\n");
> +		goto success_disable_tdx;
> +	}
> +
> +	if (!enable_virt_at_load) {
> +		pr_err("tdx: tdx requires kvm.enable_virt_at_load=1\n");
> +		goto success_disable_tdx;
> +	}

The intention of kvm_can_support_tdx() is to put all dependency checks to it.  I
think we should just put the check of 'enable_virt_at_load' inside.  And there
will be more checks in the later patches such as checking 'tdp_mmu_enabled' and
'enable_mmio_caching' so it doesn't make sense to check 'enable_virt_at_load'
outside.


diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
index 0666dfbe0bc0..d8c008437e79 100644
--- a/arch/x86/kvm/vmx/tdx.c
+++ b/arch/x86/kvm/vmx/tdx.c
@@ -38,10 +38,17 @@ static void __do_tdx_cleanup(void)
 	 * 'multiple enabling' scenario.
 	 */
 	WARN_ON_ONCE(!tdx_cpuhp_state);
-	cpuhp_remove_state_nocalls(tdx_cpuhp_state);
+	cpuhp_remove_state_nocalls_cpuslocked(tdx_cpuhp_state);
 	tdx_cpuhp_state = 0;
 }
 
+static void __tdx_cleanup(void)
+{
+	cpus_read_lock();
+	__do_tdx_cleanup();
+	cpus_read_unlock();
+}
+
 static int __init __do_tdx_bringup(void)
 {
 	int r;
@@ -68,7 +75,17 @@ static int __init __do_tdx_bringup(void)
 
 static bool __init kvm_can_support_tdx(void)
 {
-	return cpu_feature_enabled(X86_FEATURE_TDX_HOST_PLATFORM);
+	if (!cpu_feature_enabled(X86_FEATURE_TDX_HOST_PLATFORM)) {
+		pr_err("tdx: no TDX private KeyIDs available\n");
+		return false;
+	}
+
+	if (!enable_virt_at_load) {
+		pr_err("tdx: tdx requires kvm.enable_virt_at_load=1\n");
+		return false;
+	}
+
+	return true;
 }
 
 static int __init __tdx_bringup(void)
@@ -103,7 +120,7 @@ static int __init __tdx_bringup(void)
 void tdx_cleanup(void)
 {
 	if (enable_tdx) {
-		__do_tdx_cleanup();
+		__tdx_cleanup();
 		kvm_disable_virtualization();
 	}
 }
@@ -115,15 +132,8 @@ int __init tdx_bringup(void)
 	if (!enable_tdx)
 		return 0;
 
-	if (!kvm_can_support_tdx()) {
-		pr_err("tdx: no TDX private KeyIDs available\n");
-		goto success_disable_tdx;
-	}
-
-	if (!enable_virt_at_load) {
-		pr_err("tdx: tdx requires kvm.enable_virt_at_load=1\n");
+	if (!kvm_can_support_tdx())
 		goto success_disable_tdx;
-	}
 
 	/*
 	 * Ideally KVM should probe whether TDX module has been loaded


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: tdxinit-fix.diff --]
[-- Type: text/x-patch; name="tdxinit-fix.diff", Size: 1679 bytes --]

diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
index 0666dfbe0bc0..d8c008437e79 100644
--- a/arch/x86/kvm/vmx/tdx.c
+++ b/arch/x86/kvm/vmx/tdx.c
@@ -38,10 +38,17 @@ static void __do_tdx_cleanup(void)
 	 * 'multiple enabling' scenario.
 	 */
 	WARN_ON_ONCE(!tdx_cpuhp_state);
-	cpuhp_remove_state_nocalls(tdx_cpuhp_state);
+	cpuhp_remove_state_nocalls_cpuslocked(tdx_cpuhp_state);
 	tdx_cpuhp_state = 0;
 }
 
+static void __tdx_cleanup(void)
+{
+	cpus_read_lock();
+	__do_tdx_cleanup();
+	cpus_read_unlock();
+}
+
 static int __init __do_tdx_bringup(void)
 {
 	int r;
@@ -68,7 +75,17 @@ static int __init __do_tdx_bringup(void)
 
 static bool __init kvm_can_support_tdx(void)
 {
-	return cpu_feature_enabled(X86_FEATURE_TDX_HOST_PLATFORM);
+	if (!cpu_feature_enabled(X86_FEATURE_TDX_HOST_PLATFORM)) {
+		pr_err("tdx: no TDX private KeyIDs available\n");
+		return false;
+	}
+
+	if (!enable_virt_at_load) {
+		pr_err("tdx: tdx requires kvm.enable_virt_at_load=1\n");
+		return false;
+	}
+
+	return true;
 }
 
 static int __init __tdx_bringup(void)
@@ -103,7 +120,7 @@ static int __init __tdx_bringup(void)
 void tdx_cleanup(void)
 {
 	if (enable_tdx) {
-		__do_tdx_cleanup();
+		__tdx_cleanup();
 		kvm_disable_virtualization();
 	}
 }
@@ -115,15 +132,8 @@ int __init tdx_bringup(void)
 	if (!enable_tdx)
 		return 0;
 
-	if (!kvm_can_support_tdx()) {
-		pr_err("tdx: no TDX private KeyIDs available\n");
-		goto success_disable_tdx;
-	}
-
-	if (!enable_virt_at_load) {
-		pr_err("tdx: tdx requires kvm.enable_virt_at_load=1\n");
+	if (!kvm_can_support_tdx())
 		goto success_disable_tdx;
-	}
 
 	/*
 	 * Ideally KVM should probe whether TDX module has been loaded

  parent reply	other threads:[~2024-11-28  3:34 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-27 20:10 [PATCH v3 0/3] " Paolo Bonzini
2024-11-27 20:10 ` [PATCH 1/3] KVM: Export hardware virtualization enabling/disabling functions Paolo Bonzini
2024-11-27 20:10 ` [PATCH 2/3] KVM: VMX: Refactor VMX module init/exit functions Paolo Bonzini
2024-11-27 20:10 ` [PATCH 3/3] KVM: VMX: Initialize TDX during KVM module load Paolo Bonzini
2024-11-28  3:00   ` Chao Gao
2024-11-28  3:04     ` Huang, Kai
2024-11-28  3:34   ` Huang, Kai [this message]
  -- strict thread matches above, loose matches on Subject: below --
2024-10-28 13:20 [PATCH 0/3] KVM: VMX: Initialize TDX when loading KVM module Kai Huang
2024-10-28 13:20 ` [PATCH 3/3] KVM: VMX: Initialize TDX during KVM module load Kai Huang
2024-10-30 15:19   ` Sean Christopherson
2024-10-31 11:17     ` Huang, Kai
2024-10-31 20:22       ` Sean Christopherson
2024-10-31 21:21         ` Huang, Kai
2024-10-31 21:29           ` Edgecombe, Rick P
2024-11-06 14:19             ` Edgecombe, Rick P
2024-11-06 10:49         ` Huang, Kai
2024-11-06 15:01           ` Sean Christopherson
2024-11-06 20:06             ` Huang, Kai
2024-11-07 22:04               ` Sean Christopherson
2024-11-07 23:25                 ` Huang, Kai
2024-10-31 21:52       ` Dan Williams
2024-10-31 22:37         ` Huang, Kai
2024-10-31 22:56           ` Dan Williams

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7109da789f52dac408f1fb9e0440ad418af6e3ab.camel@intel.com \
    --to=kai.huang@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®