Re: [PATCH v19 004/130] x86/virt/tdx: Support global metadata read for all element sizes

Huang, Kai
2024-02-27T08:40:42Z

On Mon, 2024-02-26 at 15:32 +0100, Juergen Gross wrote:
> On 26.02.24 09:25, isaku.yamahata@intel.com wrote:
> > From: Kai Huang <kai.huang@intel.com>
> >
> > For now the kernel only reads TDMR related global metadata fields for
> > module initialization. All these fields are 16-bits, and the kernel
> > only supports reading 16-bits fields.
> >
> > KVM will need to read a bunch of non-TDMR related metadata to create and
> > run TDX guests. It's essential to provide a generic metadata read
> > infrastructure which supports reading all 8/16/32/64 bits element sizes.
> >
> > Extend the metadata read to support reading all these element sizes.
> >
> > Signed-off-by: Kai Huang <kai.huang@intel.com>
> > Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com>
> > ---
> > arch/x86/virt/vmx/tdx/tdx.c | 59 +++++++++++++++++++++++++------------
> > arch/x86/virt/vmx/tdx/tdx.h | 2 --
> > 2 files changed, 40 insertions(+), 21 deletions(-)
> >
> > diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c
> > index eb208da4ff63..a19adc898df6 100644
> > --- a/arch/x86/virt/vmx/tdx/tdx.c
> > +++ b/arch/x86/virt/vmx/tdx/tdx.c
> > @@ -271,23 +271,35 @@ static int read_sys_metadata_field(u64 field_id, u64 *data)
> > return 0;
> > }
> >
> > -static int read_sys_metadata_field16(u64 field_id,
> > - int offset,
> > - void *stbuf)
> > +/* Return the metadata field element size in bytes */
> > +static int get_metadata_field_bytes(u64 field_id)
> > {
> > - u16 *st_member = stbuf + offset;
> > + /*
> > + * TDX supports 8/16/32/64 bits metadata field element sizes.
> > + * TDX module determines the metadata element size based on the
> > + * "element size code" encoded in the field ID (see the comment
> > + * of MD_FIELD_ID_ELE_SIZE_CODE macro for specific encodings).
> > + */
> > + return 1 << MD_FIELD_ID_ELE_SIZE_CODE(field_id);
> > +}
> > +
> > +static int stbuf_read_sys_metadata_field(u64 field_id,
> > + int offset,
> > + int bytes,
> > + void *stbuf)
> > +{
> > + void *st_member = stbuf + offset;
> > u64 tmp;
> > int ret;
> >
> > - if (WARN_ON_ONCE(MD_FIELD_ID_ELE_SIZE_CODE(field_id) !=
> > - MD_FIELD_ID_ELE_SIZE_16BIT))
> > + if (WARN_ON_ONCE(get_metadata_field_bytes(field_id) != bytes))
> > return -EINVAL;
> >
> > ret = read_sys_metadata_field(field_id, &tmp);
> > if (ret)
> > return ret;
> >
> > - *st_member = tmp;
> > + memcpy(st_member, &tmp, bytes);
> >
> > return 0;
> > }
> > @@ -295,11 +307,30 @@ static int read_sys_metadata_field16(u64 field_id,
> > struct field_mapping {
> > u64 field_id;
> > int offset;
> > + int size;
> > };
> >
> > #define TD_SYSINFO_MAP(_field_id, _struct, _member) \
> > { .field_id = MD_FIELD_ID_##_field_id, \
> > - .offset = offsetof(_struct, _member) }
> > + .offset = offsetof(_struct, _member), \
> > + .size = sizeof(typeof(((_struct *)0)->_member)) }
> > +
> > +static int read_sys_metadata(struct field_mapping *fields, int nr_fields,
>
> The first parameter should be "const struct field_mapping *fields".
>
>

Indeed. :-)