Re: [PATCH v11 10/20] x86/virt/tdx: Add placeholder to construct TDMRs to cover all TDX memory regions

From: Huang, Kai
Date: Thu Jun 08 2023 - 06:18:47 EST


On Wed, 2023-06-07 at 08:57 -0700, Dave Hansen wrote:
> On 6/4/23 07:27, Kai Huang wrote:
> > +struct tdmr_info_list {
> > + void *tdmrs; /* Flexible array to hold 'tdmr_info's */
> > + int nr_consumed_tdmrs; /* How many 'tdmr_info's are in use */
>
> I'm looking back here after seeing the weird cast in the next patch.
>
> Why is this a void* instead of a _real_ type?

I followed your suggestion in v8:

https://lore.kernel.org/linux-mm/725de6e9-e468-48ef-3bae-1e8a1b7ef0f7@xxxxxxxxx/

I quoted the relevant part here:

> +/* Get the TDMR from the list at the given index. */
> +static struct tdmr_info *tdmr_entry(struct tdmr_info_list *tdmr_list,
> + int idx)
> +{
> + return (struct tdmr_info *)((unsigned long)tdmr_list->first_tdmr +
> + tdmr_list->tdmr_sz * idx);
> +}

I think that's more complicated and has more casting than necessary.
This looks nicer:

int tdmr_info_offset = tdmr_list->tdmr_sz * idx;

return (void *)tdmr_list->first_tdmr + tdmr_info_offset;

Also, it might even be worth keeping ->first_tdmr as a void*. It isn't
a real C array and keeping it as void* would keep anyone from doing:

tdmr_foo = tdmr_list->first_tdmr[foo];