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

From: Huang, Kai
Date: Thu Nov 24 2022 - 20:00:26 EST


On Wed, 2022-11-23 at 15:56 -0800, Dave Hansen wrote:
> 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?  
>

Correct.

> 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?

The alignment is assumed to be guaranteed based on the Documentation you quoted
below.

>
> 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.

tdmr_num * sizeof(u64) is not guaranteed.

> Hint #3: Comments don't actually affect the allocation

Sorry I don't understand the Hint #3.

Perhaps I should just allocate one page so it must be 512-byte aligned?

/*
* TDMR_INFO entries are configured to the TDX module via an array
* of physical address of each TDMR_INFO. The TDX module requires
* the array itself to be 512-byte aligned. Just allocate a page
* to use it as the array so the alignment can be guaranteed. The
* page will be freed after TDH.SYS.CONFIG anyway.
*/