Re: [PATCH v3 07/16] x86/virt/tdx: Add tdx_alloc/free_page() helpers

Edgecombe, Rick P
2025-09-29T17:20:09Z

On Mon, 2025-09-29 at 15:56 +0800, Yan Zhao wrote:
> > +/*
> > + * The TDX spec treats the registers like an array, as they are ordered
> > + * in the struct. The array size is limited by the number or registers,
> > + * so define the max size it could be for worst case allocations and sanity
> > + * checking.
> > + */
> > +#define MAX_DPAMT_ARG_SIZE (sizeof(struct tdx_module_args) - \
> > + offsetof(struct tdx_module_args, rdx))
> The rdx doesn't work for tdh_mem_page_demote(), which copies the pamt pages
> array starting from r12.
>
> Is there a way to make this more generic?

Sure we could just pass rdx/r12 as an arg. But I'd think to leave it to the
huage pages patches to add that tweak.

>
> > +/*
> > + * Treat struct the registers like an array that starts at RDX, per
> > + * TDX spec. Do some sanitychecks, and return an indexable type.
> > + */
> > +static u64 *dpamt_args_array_ptr(struct tdx_module_args *args)
> > +{
> > + WARN_ON_ONCE(tdx_dpamt_entry_pages() > MAX_DPAMT_ARG_SIZE);
> > +
> > + /*
> > + * FORTIFY_SOUCE could inline this and complain when callers copy
> > + * across fields, which is exactly what this is supposed to be
> > + * used for. Obfuscate it.
> > + */
> > + return (u64 *)((u8 *)args + offsetof(struct tdx_module_args, rdx));
> > +}
> > +
> > +static int alloc_pamt_array(u64 *pa_array)
> > +{
> > + struct page *page;
> > +
> > + for (int i = 0; i < tdx_dpamt_entry_pages(); i++) {
> > + page = alloc_page(GFP_KERNEL);
> > + if (!page)
> > + return -ENOMEM;
> When the 1st alloc_page() succeeds but the 2nd alloc_page() fails, the 1st
> page
> must be freed before returning an error.

Oops, yes. It needs a proper free path here. Thanks.

>
> > + pa_array[i] = page_to_phys(page);
> > + }
> > +
> > + return 0;
> > +}
> > +
>