Re: [PATCH v7 09/20] x86/virt/tdx: Get information about TDX module and TDX-capable memory

From: Dave Hansen
Date: Fri Dec 02 2022 - 12:10:18 EST


On 12/2/22 03:11, Huang, Kai wrote:
> And also to address you concern that not all 892 bytes are reserved, how about
> below:
>
> union {
> - struct cpuid_config cpuid_configs[0];
> - u8 reserved5[892];
> + DECLARE_FLEX_ARRAY(struct cpuid_config, cpuid_configs);
> + u8 padding[892];
> };
> } __packed __aligned(TDSYSINFO_STRUCT_ALIGNMENT);
>
> The goal is to make the size of 'struct tdsysinfo_struct' to be 1024B so we can
> use a static variable for it, and at the meantime, it can still have 1024B
> (enough space) for the TDH.SYS.INFO to write to.

I just don't like the open-coded sizes.

For instance, wouldn't it be great if you didn't have to know the size
of *ANYTHING* else to properly size the '892'?

Maybe we just need some helpers to hide the gunk:

#define DECLARE_PADDED_STRUCT(type, name, alignment) \
struct type##_padded { \
union { \
struct type name; \
u8 padding[alignment]; \
} \
} name##_padded;

#define PADDED_STRUCT(name) (name##_padded.name)

That can get used like this:

DECLARE_PADDED_STRUCT(struct tdsysinfo_struct, tdsysinfo,
TDSYSINFO_STRUCT_ALIGNMENT);


struct tdsysinfo_struct sysinfo = PADDED_STRUCT(tdsysinfo)