[PATCH v15 012/115] KVM: TDX: Retry SEAMCALL on the lack of entropy error

From: isaku . yamahata
Date: Tue Jul 25 2023 - 18:16:43 EST


From: Isaku Yamahata <isaku.yamahata@xxxxxxxxx>

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@xxxxxxxxx>
---
arch/x86/kvm/vmx/tdx_errno.h | 1 +
arch/x86/kvm/vmx/tdx_ops.h | 8 +++++++-
2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kvm/vmx/tdx_errno.h b/arch/x86/kvm/vmx/tdx_errno.h
index 56cfd2f558fa..53dc14ba9107 100644
--- a/arch/x86/kvm/vmx/tdx_errno.h
+++ b/arch/x86/kvm/vmx/tdx_errno.h
@@ -14,6 +14,7 @@
#define TDX_INTERRUPTED_RESUMABLE 0x8000000300000000ULL
#define TDX_OPERAND_INVALID 0xC000010000000000ULL
#define TDX_OPERAND_BUSY 0x8000020000000000ULL
+#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 76eddecdca12..d588a5507f5a 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,9 +18,14 @@
static inline u64 tdx_seamcall(u64 op, u64 rcx, u64 rdx, u64 r8, u64 r9,
struct tdx_module_output *out)
{
+ int retry;
u64 ret;

- ret = __seamcall(op, rcx, rdx, r8, r9, out);
+ /* Mimic the existing rdrand_long() to retry RDRAND_RETRY_LOOPS times. */
+ retry = RDRAND_RETRY_LOOPS;
+ do {
+ ret = __seamcall(op, rcx, rdx, r8, r9, out);
+ } 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