Re: [PATCH v12 08/22] x86/virt/tdx: Get information about TDX module and TDX-capable memory

From: Huang, Kai
Date: Tue Jun 27 2023 - 07:46:23 EST


On Tue, 2023-06-27 at 14:37 +0300, kirill.shutemov@xxxxxxxxxxxxxxx wrote:
> On Tue, Jun 27, 2023 at 10:45:33AM +0000, Huang, Kai wrote:
> > On Tue, 2023-06-27 at 12:51 +0300, kirill.shutemov@xxxxxxxxxxxxxxx wrote:
> > > On Tue, Jun 27, 2023 at 02:12:38AM +1200, Kai Huang wrote:
> > > > static int init_tdx_module(void)
> > > > {
> > > > + struct tdsysinfo_struct *sysinfo;
> > > > + struct cmr_info *cmr_array;
> > > > + int ret;
> > > > +
> > > > + /*
> > > > + * Get the TDSYSINFO_STRUCT and CMRs from the TDX module.
> > > > + *
> > > > + * The buffers of the TDSYSINFO_STRUCT and the CMR array passed
> > > > + * to the TDX module must be 1024-bytes and 512-bytes aligned
> > > > + * respectively. Allocate one page to accommodate them both and
> > > > + * also meet those alignment requirements.
> > > > + */
> > > > + sysinfo = (struct tdsysinfo_struct *)__get_free_page(GFP_KERNEL);
> > > > + if (!sysinfo)
> > > > + return -ENOMEM;
> > > > + cmr_array = (struct cmr_info *)((unsigned long)sysinfo + PAGE_SIZE / 2);
> > > > +
> > > > + BUILD_BUG_ON(PAGE_SIZE / 2 < TDSYSINFO_STRUCT_SIZE);
> > > > + BUILD_BUG_ON(PAGE_SIZE / 2 < sizeof(struct cmr_info) * MAX_CMRS);
> > >
> > > This works, but why not just use slab for this? kmalloc has 512 and 1024
> > > pools already and you won't waste memory for rounding up.
> > >
> > > Something like this:
> > >
> > > sysinfo = kmalloc(TDSYSINFO_STRUCT_SIZE, GFP_KERNEL);
> > > if (!sysinfo)
> > > return -ENOMEM;
> > >
> > > cmr_array_size = sizeof(struct cmr_info) * MAX_CMRS;
> > >
> > > /* CMR array has to be 512-aligned */
> > > cmr_array_size = round_up(cmr_array_size, 512);
> >
> > Should we define a macro for 512
> >
> > +#define CMR_INFO_ARRAY_ALIGNMENT 512
> >
> > And get rid of this comment? AFAICT Dave didn't like such comment mentioning
> > 512-bytes aligned if we have a macro for that.
>
> Good idea.
>
> > > cmr_array = kmalloc(cmr_array_size, GFP_KERNEL);
> > > if (!cmr_array) {
> > > kfree(sysinfo);
> > > return -ENOMEM;
> > > }
> > >
> > > ?
> > >
> >
> > I confess the reason I used __get_free_page() was to avoid having to allocate
> > twice, and in case of failure, I need to handle additional memory free. But I
> > can do if you think it's clearer?
>
> Less trickery is always cleaner. Especially if the trick is not justified.
>
>

Alright. I'll change to allocating them separately if no opinion from others.