Re: [PATCHv2 3/4] x86/tdx: Handle PENDING_EPT_VIOLATION_V2

Dave Hansen
2024-04-24T18:29:02Z

On 3/25/24 03:46, Kirill A. Shutemov wrote:
> PENDING_EPT_VIOLATION_V2 allows TD to control whether access to
> a pending page triggers #VE.
>
> Kernel doesn't want to see any #VEs on accesses to private memory:
> disable such #VEs.

This is really a bare bones changelog. The connection between "pending"
and "private" goes unmentioned. It's not obvious when the kernel might
be exposed to one of these #VE's.

> diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c
> index 08e2bb462ce8..860bfdd5a11d 100644
> --- a/arch/x86/coco/tdx/tdx.c
> +++ b/arch/x86/coco/tdx/tdx.c
> @@ -77,6 +77,17 @@ static inline void tdcall(u64 fn, struct tdx_module_args *args)
> panic("TDCALL %lld failed (Buggy TDX module!)\n", fn);
> }
>
> +static inline u64 tdg_vm_rd(u64 field)
> +{
> + struct tdx_module_args args = {
> + .rdx = field,
> + };
> +
> + tdcall(TDG_VM_RD, &args);
> +
> + return args.r8;
> +}
> +
> static inline u64 tdg_vm_wr(u64 field, u64 value, u64 mask)
> {
> struct tdx_module_args args = {
> @@ -91,6 +102,28 @@ static inline u64 tdg_vm_wr(u64 field, u64 value, u64 mask)
> return args.r8;
> }
>
> +static inline u64 tdg_sys_rd(u64 field)
> +{
> + struct tdx_module_args args = {
> + .rdx = field,
> + };
> +
> + tdcall(TDG_SYS_RD, &args);
> +
> + return args.r8;
> +}

I scratched my head for a minute on why these two identical functions
were introduced. Then I realized that they're different TDCALLs.

Please break them out into a separate patch where you can mention this.
Also, the least we can do is a one-line comment about what they do:

/* Read metadata about how *this* VM is configured. */
static inline u64 tdg_vm_rd(u64 field)
...

/* Read metadata about the TDX system as whole. */
static inline u64 tdg_sys_rd(u64 field)

> +static bool tdcs_ctls_set(u64 mask)
> +{
> + struct tdx_module_args args = {
> + .rdx = TDCS_TD_CTLS,
> + .r8 = mask,
> + .r9 = mask,
> + };
> +
> + return __tdcall(TDG_VM_WR, &args) == TDX_SUCCESS;
> +}

This is acronym soup. It's made worse by "TDCS_TD_CTLS" being
ungoogleable and not literally present in the first three TDX PDFs that
I dug in.

It's also not clear logically how this differs from tdg_vm_wr().

> /**
> * tdx_mcall_get_report0() - Wrapper to get TDREPORT0 (a.k.a. TDREPORT
> * subtype 0) using TDG.MR.REPORT TDCALL.
> @@ -185,7 +218,8 @@ static void tdx_setup(u64 *cc_mask)
> {
> struct tdx_module_args args = {};
> unsigned int gpa_width;
> - u64 td_attr;
> + u64 td_attr, features;
> + bool sept_ve_disabled;
>
> /*
> * TDINFO TDX module call is used to get the TD execution environment
> @@ -206,19 +240,50 @@ static void tdx_setup(u64 *cc_mask)
> gpa_width = args.rcx & GENMASK(5, 0);
> *cc_mask = BIT_ULL(gpa_width - 1);
>
> + td_attr = args.rdx;
> +
> /* Kernel does not use NOTIFY_ENABLES and does not need random #VEs */
> tdg_vm_wr(TDCS_NOTIFY_ENABLES, 0, -1ULL);
>
> + features = tdg_sys_rd(TDCS_TDX_FEATURES0);
> +
> /*
> * The kernel can not handle #VE's when accessing normal kernel
> * memory. Ensure that no #VE will be delivered for accesses to
> * TD-private memory. Only VMM-shared memory (MMIO) will #VE.
> + *
> + * Check if the TD is created with SEPT #VE disabled.
> */
> - td_attr = args.rdx;
> - if (!(td_attr & ATTR_SEPT_VE_DISABLE)) {
> - const char *msg = "TD misconfiguration: SEPT_VE_DISABLE attribute must be set.";
> + sept_ve_disabled = td_attr & ATTR_SEPT_VE_DISABLE;
>
> - /* Relax SEPT_VE_DISABLE check for debug TD. */
> + /*
> + * Check if flexible control of SEPT #VE is supported.
> + *
> + * The check consists of verifying if the feature is supported by the
> + * TDX module (the TDX_FEATURES0 check) and if the feature is enabled
> + * for this TD (CONFIG_FLAGS check).
> + *
> + * If flexible control is supported, disable SEPT #VE.
> + *
> + * Disable SEPT #VE regardless of ATTR_SEPT_VE_DISABLE status as
> + * flexible control allows software running before the kernel to
> + * enable it.
> + *
> + * Skip SEPT disabling for debug TD. SEPT #VE is unsafe but can be
> + * useful for debugging to produce a stack trace. Known to be useful
> + * for debugging unaccepted memory problems.
> + */
> + if (features & TDX_FEATURES0_PENDING_EPT_VIOLATION_V2 &&
> + (tdg_vm_rd(TDCS_CONFIG_FLAGS) & TDCS_CONFIG_FLEXIBLE_PENDING_VE) &&

Can't we just check TDCS_CONFIG_FLEXIBLE_PENDING_VE alone? Surely that
flag can't get set if !TDX_FEATURES0_PENDING_EPT_VIOLATION_V2.

In any case, this is, um, kinda messy. Look at what a helper could do:

/*
* Private memory #VE's are bad because the kernel has no way of
* recovering from them and can only die. Newer TDX modules can
* configured to inflict fewer #VE's on guests. Do it when available.
*/
static inline int try_disable_sept_ve(td_addr)
{
/* Does the TDX module support the #VE reduction feature? */
if (!(features & TDX_FEATURES0_PENDING_EPT_VIOLATION_V2))
return -ENOTSUP;

/* Is this TD allowed to use the feature? */
if (!(tdg_vm_rd(TDCS_CONFIG_FLAGS) &
TDCS_CONFIG_FLEXIBLE_PENDING_VE)))
return -ENOTSUP;

/* Keep #VE's enabled for splats in debugging environments: */
if (td_attr & ATTR_DEBUG)
return -ENOTSUP;

if (!tdcs_ctls_set(TD_CTLS_PENDING_VE_DISABLE))
return -ENOTSUP;

return 0;
}

Then the code becomes:

if (!try_disable_sept_ve(td_addr))
sept_ve_disabled = true;
else
sept_ve_disabled = td_attr & ATTR_SEPT_VE_DISABLE;

See how it sets the state in *ONE* easy-to-understand place?

Oh, 'sept_ve_disabled' either gets disabled dynamically if it can, or it
gets read from the static state if not.

Oh, and if you ever have a comment that long with a bunch of
explanations of individual if() conditions, *PLEASE* try to break it up
in some way. Otherwise readers just spent all their time trying to
match up the comment to the code. It's not a fun game.

> + !(td_attr & ATTR_DEBUG)) {
> + if (tdcs_ctls_set(TD_CTLS_PENDING_VE_DISABLE))
> + sept_ve_disabled = true;
> + }
> +
> + if (!sept_ve_disabled) {
> + const char *msg = "TD misconfiguration: SEPT #VE has to be disabled";
> +
> + /* Relax SEPT #VE disable check for debug TD. */
> if (td_attr & ATTR_DEBUG)
> pr_warn("%s\n", msg);
> else
> diff --git a/arch/x86/include/asm/shared/tdx.h b/arch/x86/include/asm/shared/tdx.h
> index fdfd41511b02..29a61c72e4dd 100644
> --- a/arch/x86/include/asm/shared/tdx.h
> +++ b/arch/x86/include/asm/shared/tdx.h
> @@ -16,11 +16,27 @@
> #define TDG_VP_VEINFO_GET 3
> #define TDG_MR_REPORT 4
> #define TDG_MEM_PAGE_ACCEPT 6
> +#define TDG_VM_RD 7
> #define TDG_VM_WR 8
> +#define TDG_SYS_RD 11
>
> -/* TDCS fields. To be used by TDG.VM.WR and TDG.VM.RD module calls */
> +/* TDX Global Metadata. To be used by TDG.SYS.RD */
> +#define TDCS_TDX_FEATURES0 0x0A00000300000008
> +
> +/* TDX TD-Scope Metadata. To be used by TDG.VM.WR and TDG.VM.RD */
> +#define TDCS_CONFIG_FLAGS 0x1110000300000016
> +#define TDCS_TD_CTLS 0x1110000300000017
> #define TDCS_NOTIFY_ENABLES 0x9100000000000010
>
> +/* TDCS_TDX_FEATURES0 bits */
> +#define TDX_FEATURES0_PENDING_EPT_VIOLATION_V2 BIT_ULL(16)
> +
> +/* TDCS_CONFIG_FLAGS bits */
> +#define TDCS_CONFIG_FLEXIBLE_PENDING_VE BIT_ULL(1)
> +
> +/* TDCS_TD_CTLS bits */
> +#define TD_CTLS_PENDING_VE_DISABLE BIT_ULL(0)
> +
> /* TDX hypercall Leaf IDs */
> #define TDVMCALL_MAP_GPA 0x10001
> #define TDVMCALL_GET_QUOTE 0x10002