[PATCH v18 015/121] KVM: TDX: Retry SEAMCALL on the lack of entropy error

isaku.yamahata@intel.com posted 121 patches 1 year, 11 months ago
There is a newer version of this series
[PATCH v18 015/121] KVM: TDX: Retry SEAMCALL on the lack of entropy error
Posted by isaku.yamahata@intel.com 1 year, 11 months ago
From: Isaku Yamahata <isaku.yamahata@intel.com>

Some SEAMCALL may return TDX_RND_NO_ENTROPY error when the entropy is
lacking.  Retry SEAMCALL on the error following rdrand_long() to retry
RDRAND_RETRY_LOOPS times.

Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com>
---
v18:
- update to use struct tdx_modules_args for inputs.
---
 arch/x86/kvm/vmx/tdx_errno.h |  1 +
 arch/x86/kvm/vmx/tdx_ops.h   | 15 +++++++++++----
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/arch/x86/kvm/vmx/tdx_errno.h b/arch/x86/kvm/vmx/tdx_errno.h
index 7f96696b8e7c..bb093e292fef 100644
--- a/arch/x86/kvm/vmx/tdx_errno.h
+++ b/arch/x86/kvm/vmx/tdx_errno.h
@@ -14,6 +14,7 @@
 #define TDX_OPERAND_INVALID			0xC000010000000000ULL
 #define TDX_OPERAND_BUSY			0x8000020000000000ULL
 #define TDX_PREVIOUS_TLB_EPOCH_BUSY		0x8000020100000000ULL
+#define TDX_RND_NO_ENTROPY			0x8000020300000000ULL
 #define TDX_VCPU_NOT_ASSOCIATED			0x8000070200000000ULL
 #define TDX_KEY_GENERATION_FAILED		0x8000080000000000ULL
 #define TDX_KEY_STATE_INCORRECT			0xC000081100000000ULL
diff --git a/arch/x86/kvm/vmx/tdx_ops.h b/arch/x86/kvm/vmx/tdx_ops.h
index 0e26cf22240e..f4c16e5265f0 100644
--- a/arch/x86/kvm/vmx/tdx_ops.h
+++ b/arch/x86/kvm/vmx/tdx_ops.h
@@ -6,6 +6,7 @@
 
 #include <linux/compiler.h>
 
+#include <asm/archrandom.h>
 #include <asm/cacheflush.h>
 #include <asm/asm.h>
 #include <asm/kvm_host.h>
@@ -17,14 +18,20 @@
 static inline u64 tdx_seamcall(u64 op, struct tdx_module_args *in,
 			       struct tdx_module_args *out)
 {
+	struct tdx_module_args args;
+	int retry;
 	u64 ret;
 
-	if (out) {
+	if (!out)
+		out = &args;
+
+	/* Mimic the existing rdrand_long() to retry RDRAND_RETRY_LOOPS times. */
+	retry = RDRAND_RETRY_LOOPS;
+	do {
+		/* As __seamcall_ret() overwrites out, init out on each loop. */
 		*out = *in;
 		ret = __seamcall_ret(op, out);
-	} else
-		ret = __seamcall(op, in);
-
+	} while (unlikely(ret == TDX_RND_NO_ENTROPY) && --retry);
 	if (unlikely(ret == TDX_SEAMCALL_UD)) {
 		/*
 		 * SEAMCALLs fail with TDX_SEAMCALL_UD returned when VMX is off.
-- 
2.25.1
Re: [PATCH v18 015/121] KVM: TDX: Retry SEAMCALL on the lack of entropy error
Posted by Paolo Bonzini 1 year, 10 months ago
On Tue, Jan 23, 2024 at 12:55 AM <isaku.yamahata@intel.com> wrote:
>
> From: Isaku Yamahata <isaku.yamahata@intel.com>
>
> Some SEAMCALL may return TDX_RND_NO_ENTROPY error when the entropy is
> lacking.  Retry SEAMCALL on the error following rdrand_long() to retry
> RDRAND_RETRY_LOOPS times.
>
> Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com>

This patch should not be needed anymore, just...

> +       /* Mimic the existing rdrand_long() to retry RDRAND_RETRY_LOOPS times. */
> +       retry = RDRAND_RETRY_LOOPS;
> +       do {
> +               /* As __seamcall_ret() overwrites out, init out on each loop. */
>                 *out = *in;
>                 ret = __seamcall_ret(op, out);
> -       } else
> -               ret = __seamcall(op, in);

... use seamcall() and seamcall_ret() here instead of the "__" versions.

Paolo
Re: [PATCH v18 015/121] KVM: TDX: Retry SEAMCALL on the lack of entropy error
Posted by Binbin Wu 1 year, 11 months ago

On 1/23/2024 7:52 AM, isaku.yamahata@intel.com wrote:
> From: Isaku Yamahata <isaku.yamahata@intel.com>
>
> Some SEAMCALL may return TDX_RND_NO_ENTROPY error when the entropy is
> lacking.  Retry SEAMCALL on the error following rdrand_long() to retry
> RDRAND_RETRY_LOOPS times.
>
> Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com>
> ---
> v18:
> - update to use struct tdx_modules_args for inputs.
> ---
>   arch/x86/kvm/vmx/tdx_errno.h |  1 +
>   arch/x86/kvm/vmx/tdx_ops.h   | 15 +++++++++++----
>   2 files changed, 12 insertions(+), 4 deletions(-)
>
> diff --git a/arch/x86/kvm/vmx/tdx_errno.h b/arch/x86/kvm/vmx/tdx_errno.h
> index 7f96696b8e7c..bb093e292fef 100644
> --- a/arch/x86/kvm/vmx/tdx_errno.h
> +++ b/arch/x86/kvm/vmx/tdx_errno.h
> @@ -14,6 +14,7 @@
>   #define TDX_OPERAND_INVALID			0xC000010000000000ULL
>   #define TDX_OPERAND_BUSY			0x8000020000000000ULL
>   #define TDX_PREVIOUS_TLB_EPOCH_BUSY		0x8000020100000000ULL
> +#define TDX_RND_NO_ENTROPY			0x8000020300000000ULL
>   #define TDX_VCPU_NOT_ASSOCIATED			0x8000070200000000ULL
>   #define TDX_KEY_GENERATION_FAILED		0x8000080000000000ULL
>   #define TDX_KEY_STATE_INCORRECT			0xC000081100000000ULL
> diff --git a/arch/x86/kvm/vmx/tdx_ops.h b/arch/x86/kvm/vmx/tdx_ops.h
> index 0e26cf22240e..f4c16e5265f0 100644
> --- a/arch/x86/kvm/vmx/tdx_ops.h
> +++ b/arch/x86/kvm/vmx/tdx_ops.h
> @@ -6,6 +6,7 @@
>   
>   #include <linux/compiler.h>
>   
> +#include <asm/archrandom.h>
>   #include <asm/cacheflush.h>
>   #include <asm/asm.h>
>   #include <asm/kvm_host.h>
> @@ -17,14 +18,20 @@
>   static inline u64 tdx_seamcall(u64 op, struct tdx_module_args *in,
>   			       struct tdx_module_args *out)
>   {
> +	struct tdx_module_args args;
> +	int retry;
>   	u64 ret;
>   
> -	if (out) {
> +	if (!out)
> +		out = &args;
> +
> +	/* Mimic the existing rdrand_long() to retry RDRAND_RETRY_LOOPS times. */
> +	retry = RDRAND_RETRY_LOOPS;
> +	do {
> +		/* As __seamcall_ret() overwrites out, init out on each loop. */
>   		*out = *in;
>   		ret = __seamcall_ret(op, out);
> -	} else
> -		ret = __seamcall(op, in);
> -
> +	} while (unlikely(ret == TDX_RND_NO_ENTROPY) && --retry);
>   	if (unlikely(ret == TDX_SEAMCALL_UD)) {
>   		/*
>   		 * SEAMCALLs fail with TDX_SEAMCALL_UD returned when VMX is off.
One question is when the return code is TDX_RND_NO_ENTROPY, does the input
tdx_module_args value will be modified or not?

As mentioned by Kai in previous review, there are seamcall*() variants which
handle TDX_RND_NO_ENTROPY already.

If the input tdx_module_args is not modified by a seamcall when the return
code is TDX_RND_NO_ENTROPY, the seamcall_ret() can be used directly.

But if the input tdx_module_args could be modified by a seamcall when the
return code is TDX_RND_NO_ENTROPY, the implementations of seamcall_ret() and
seamcall_saved_ret() have problem then, because the input tdx_module_args is
not re-initialized when retry.
They need to be fixed or deleted (no one is using them currently).