diff mbox series

[v13,05/22] x86/virt/tdx: Handle SEAMCALL no entropy error in common code

Message ID c945c9a8db98b7a304c404a7ef18aa2f7770ffaf.1692962263.git.kai.huang@intel.com (mailing list archive)
State New, archived
Headers show
Series TDX host kernel support | expand

Commit Message

Huang, Kai Aug. 25, 2023, 12:14 p.m. UTC
Some SEAMCALLs use the RDRAND hardware and can fail for the same reasons
as RDRAND.  Use the kernel RDRAND retry logic for them.

There are three __seamcall*() variants.  Add a macro to do the SEAMCALL
retry in the common code and define a wrapper for each __seamcall*()
variant.

Signed-off-by: Kai Huang <kai.huang@intel.com>
---

v12 -> v13:
 - New implementation due to TDCALL assembly series.

---
 arch/x86/include/asm/tdx.h | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

Comments

Dave Hansen Sept. 8, 2023, 4:21 p.m. UTC | #1
On 8/25/23 05:14, Kai Huang wrote:
> Some SEAMCALLs use the RDRAND hardware and can fail for the same reasons
> as RDRAND.  Use the kernel RDRAND retry logic for them.
> 
> There are three __seamcall*() variants.  Add a macro to do the SEAMCALL
> retry in the common code and define a wrapper for each __seamcall*()
> variant.
> 
> Signed-off-by: Kai Huang <kai.huang@intel.com>
> ---
> 
> v12 -> v13:
>  - New implementation due to TDCALL assembly series.
> 
> ---
>  arch/x86/include/asm/tdx.h | 27 +++++++++++++++++++++++++++
>  1 file changed, 27 insertions(+)
> 
> diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h
> index a252328734c7..cfae8b31a2e9 100644
> --- a/arch/x86/include/asm/tdx.h
> +++ b/arch/x86/include/asm/tdx.h
> @@ -24,6 +24,11 @@
>  #define TDX_SEAMCALL_GP			(TDX_SW_ERROR | X86_TRAP_GP)
>  #define TDX_SEAMCALL_UD			(TDX_SW_ERROR | X86_TRAP_UD)
>  
> +/*
> + * TDX module SEAMCALL leaf function error codes
> + */
> +#define TDX_RND_NO_ENTROPY	0x8000020300000000ULL
> +
>  #ifndef __ASSEMBLY__
>  
>  /*
> @@ -82,6 +87,28 @@ u64 __seamcall(u64 fn, struct tdx_module_args *args);
>  u64 __seamcall_ret(u64 fn, struct tdx_module_args *args);
>  u64 __seamcall_saved_ret(u64 fn, struct tdx_module_args *args);
>  
> +#include <asm/archrandom.h>
> +
> +#define SEAMCALL_NO_ENTROPY_RETRY(__seamcall_func, __fn, __args)	\
> +({									\
> +	int ___retry = RDRAND_RETRY_LOOPS;				\
> +	u64 ___sret;							\
> +									\
> +	do {								\
> +		___sret = __seamcall_func((__fn), (__args));		\
> +	} while (___sret == TDX_RND_NO_ENTROPY && --___retry);		\
> +	___sret;							\
> +})

This is a *LOT* less eye-bleedy if you do it without macros:


typedef u64 (*sc_func_t)(u64 fn, struct tdx_module_args *args);

static inline
u64 sc_retry(sc_func_t func, u64 fn, struct tdx_module_args *args)
{
        int retry = RDRAND_RETRY_LOOPS;
        u64 ret;

        do {
                ret = func(fn, args);
        } while (ret == TDX_RND_NO_ENTROPY && --retry);

        return ret;
}

#define seamcall(_fn, _args)           sc_retry(_seamcall,
(_fn), (_args))
#define seamcall_ret(_fn, _args)       sc_retry(_seamcall_ret,
(_fn), (_args))
#define seamcall_saved_ret(_fn, _args) sc_retry(_seamcall_saved_ret,
(_fn), (_args))

The compiler can figure it out and avoid making func() an indirect call
since it knows the call location at compile time.

You can also do the seamcall() #define as a static inline, but it does
take up more screen real estate.  Oh, and going a wee bit over 80
columns is OK for those #defines.
Huang, Kai Sept. 11, 2023, 11:41 a.m. UTC | #2
On Fri, 2023-09-08 at 09:21 -0700, Dave Hansen wrote:
> On 8/25/23 05:14, Kai Huang wrote:
> > Some SEAMCALLs use the RDRAND hardware and can fail for the same reasons
> > as RDRAND.  Use the kernel RDRAND retry logic for them.
> > 
> > There are three __seamcall*() variants.  Add a macro to do the SEAMCALL
> > retry in the common code and define a wrapper for each __seamcall*()
> > variant.
> > 
> > Signed-off-by: Kai Huang <kai.huang@intel.com>
> > ---
> > 
> > v12 -> v13:
> >  - New implementation due to TDCALL assembly series.
> > 
> > ---
> >  arch/x86/include/asm/tdx.h | 27 +++++++++++++++++++++++++++
> >  1 file changed, 27 insertions(+)
> > 
> > diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h
> > index a252328734c7..cfae8b31a2e9 100644
> > --- a/arch/x86/include/asm/tdx.h
> > +++ b/arch/x86/include/asm/tdx.h
> > @@ -24,6 +24,11 @@
> >  #define TDX_SEAMCALL_GP			(TDX_SW_ERROR | X86_TRAP_GP)
> >  #define TDX_SEAMCALL_UD			(TDX_SW_ERROR | X86_TRAP_UD)
> >  
> > +/*
> > + * TDX module SEAMCALL leaf function error codes
> > + */
> > +#define TDX_RND_NO_ENTROPY	0x8000020300000000ULL
> > +
> >  #ifndef __ASSEMBLY__
> >  
> >  /*
> > @@ -82,6 +87,28 @@ u64 __seamcall(u64 fn, struct tdx_module_args *args);
> >  u64 __seamcall_ret(u64 fn, struct tdx_module_args *args);
> >  u64 __seamcall_saved_ret(u64 fn, struct tdx_module_args *args);
> >  
> > +#include <asm/archrandom.h>
> > +
> > +#define SEAMCALL_NO_ENTROPY_RETRY(__seamcall_func, __fn, __args)	\
> > +({									\
> > +	int ___retry = RDRAND_RETRY_LOOPS;				\
> > +	u64 ___sret;							\
> > +									\
> > +	do {								\
> > +		___sret = __seamcall_func((__fn), (__args));		\
> > +	} while (___sret == TDX_RND_NO_ENTROPY && --___retry);		\
> > +	___sret;							\
> > +})
> 
> This is a *LOT* less eye-bleedy if you do it without macros:
> 
> 
> typedef u64 (*sc_func_t)(u64 fn, struct tdx_module_args *args);
> 
> static inline
> u64 sc_retry(sc_func_t func, u64 fn, struct tdx_module_args *args)
> {
>         int retry = RDRAND_RETRY_LOOPS;
>         u64 ret;
> 
>         do {
>                 ret = func(fn, args);
>         } while (ret == TDX_RND_NO_ENTROPY && --retry);
> 
>         return ret;
> }
> 
> #define seamcall(_fn, _args)           sc_retry(_seamcall,
> (_fn), (_args))
> #define seamcall_ret(_fn, _args)       sc_retry(_seamcall_ret,
> (_fn), (_args))
> #define seamcall_saved_ret(_fn, _args) sc_retry(_seamcall_saved_ret,
> (_fn), (_args))
> 
> The compiler can figure it out and avoid making func() an indirect call
> since it knows the call location at compile time.

Indirect call was a concern when I was implementing those.  I didn't know for 
sure that the compiler can avoid it.  I'll change to use above.  Thanks!

> 
> You can also do the seamcall() #define as a static inline, but it does
> take up more screen real estate.  Oh, and going a wee bit over 80
> columns is OK for those #defines.

Yes I verified the checkpatch.pl wouldn't complain if the #define exceeded 80
characters in one line.  I'll use #define.  Thanks!
diff mbox series

Patch

diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h
index a252328734c7..cfae8b31a2e9 100644
--- a/arch/x86/include/asm/tdx.h
+++ b/arch/x86/include/asm/tdx.h
@@ -24,6 +24,11 @@ 
 #define TDX_SEAMCALL_GP			(TDX_SW_ERROR | X86_TRAP_GP)
 #define TDX_SEAMCALL_UD			(TDX_SW_ERROR | X86_TRAP_UD)
 
+/*
+ * TDX module SEAMCALL leaf function error codes
+ */
+#define TDX_RND_NO_ENTROPY	0x8000020300000000ULL
+
 #ifndef __ASSEMBLY__
 
 /*
@@ -82,6 +87,28 @@  u64 __seamcall(u64 fn, struct tdx_module_args *args);
 u64 __seamcall_ret(u64 fn, struct tdx_module_args *args);
 u64 __seamcall_saved_ret(u64 fn, struct tdx_module_args *args);
 
+#include <asm/archrandom.h>
+
+#define SEAMCALL_NO_ENTROPY_RETRY(__seamcall_func, __fn, __args)	\
+({									\
+	int ___retry = RDRAND_RETRY_LOOPS;				\
+	u64 ___sret;							\
+									\
+	do {								\
+		___sret = __seamcall_func((__fn), (__args));		\
+	} while (___sret == TDX_RND_NO_ENTROPY && --___retry);		\
+	___sret;							\
+})
+
+#define seamcall(__fn, __args)						\
+	SEAMCALL_NO_ENTROPY_RETRY(__seamcall, (__fn), (__args))
+
+#define seamcall_ret(__fn, __args)					\
+	SEAMCALL_NO_ENTROPY_RETRY(__seamcall_ret, (__fn), (__args))
+
+#define seamcall_saved_ret(__fn, __args)				\
+	SEAMCALL_NO_ENTROPY_RETRY(__seamcall_saved_ret, (__fn), (__args))
+
 bool platform_tdx_enabled(void);
 #else
 static inline bool platform_tdx_enabled(void) { return false; }