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

From: kirill . shutemov
Date: Tue Jun 27 2023 - 05:53:31 EST


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);

cmr_array = kmalloc(cmr_array_size, GFP_KERNEL);
if (!cmr_array) {
kfree(sysinfo);
return -ENOMEM;
}

?

--
Kiryl Shutsemau / Kirill A. Shutemov