Re: [PATCH v7 16/20] x86/virt/tdx: Configure TDX module with TDMRs and global KeyID

From: Dave Hansen
Date: Wed Nov 23 2022 - 18:57:03 EST


On 11/20/22 16:26, Kai Huang wrote:
> After the TDX-usable memory regions are constructed in an array of TDMRs
> and the global KeyID is reserved, configure them to the TDX module using
> TDH.SYS.CONFIG SEAMCALL. TDH.SYS.CONFIG can only be called once and can
> be done on any logical cpu.
>
> Reviewed-by: Isaku Yamahata <isaku.yamahata@xxxxxxxxx>
> Signed-off-by: Kai Huang <kai.huang@xxxxxxxxx>
> ---
> arch/x86/virt/vmx/tdx/tdx.c | 37 +++++++++++++++++++++++++++++++++++++
> arch/x86/virt/vmx/tdx/tdx.h | 2 ++
> 2 files changed, 39 insertions(+)
>
> diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c
> index e2cbeeb7f0dc..3a032930e58a 100644
> --- a/arch/x86/virt/vmx/tdx/tdx.c
> +++ b/arch/x86/virt/vmx/tdx/tdx.c
> @@ -979,6 +979,37 @@ static int construct_tdmrs(struct tdmr_info *tdmr_array, int *tdmr_num)
> return ret;
> }
>
> +static int config_tdx_module(struct tdmr_info *tdmr_array, int tdmr_num,
> + u64 global_keyid)
> +{
> + u64 *tdmr_pa_array;
> + int i, array_sz;
> + u64 ret;
> +
> + /*
> + * TDMR_INFO entries are configured to the TDX module via an
> + * array of the physical address of each TDMR_INFO. TDX module
> + * requires the array itself to be 512-byte aligned. Round up
> + * the array size to 512-byte aligned so the buffer allocated
> + * by kzalloc() will meet the alignment requirement.
> + */

Aagh. Return of (a different) 512-byte aligned structure.

> + array_sz = ALIGN(tdmr_num * sizeof(u64), TDMR_INFO_PA_ARRAY_ALIGNMENT);
> + tdmr_pa_array = kzalloc(array_sz, GFP_KERNEL);

Just to be clear, all that chatter about alignment is because the
*START* of the array has to be aligned. Right? I see alignment for
'array_sz', but that's not the start of the array.

tdmr_pa_array is the start of the array. Where is *THAT* aligned?

How does rounding up the size make kzalloc() magically know how to align
the *START* of the allocation?

Because I'm actually questioning my own sanity at this point, I went and
double-checked the docs (Documentation/core-api/memory-allocation.rst):

> The address of a chunk allocated with `kmalloc` is aligned to at least
> ARCH_KMALLOC_MINALIGN bytes. For sizes which are a power of two, the
> alignment is also guaranteed to be at least the respective size.

Hint #1: ARCH_KMALLOC_MINALIGN is way less than 512.
Hint #2: tdmr_num is not guaranteed to be a power of two.
Hint #3: Comments don't actually affect the allocation

<snip>