Re: [PATCH v10 015/108] x86/cpu: Add helper functions to allocate/free TDX private host key id

Huang, Kai
2022-11-08T09:17:21Z

On Sat, 2022-10-29 at 23:22 -0700, isaku.yamahata@intel.com wrote:
> From: Isaku Yamahata <isaku.yamahata@intel.com>
>
> TDX private host key id is assigned to guest TD. The memory controller
> encrypts guest TD memory with the assigned TDX private host key id (HIKD).
^
HKID.

And since you already mentioned in the first sentence, you can put (HKID) part
there. And I think you can just use HKID in the rest places to save some typing
as this is the purpose of using (HKID) I suppose.

[...]

>
> +/* TDX KeyID pool */
> +static DEFINE_IDA(tdx_keyid_pool);
> +
> +int tdx_keyid_alloc(void)
> +{
> + if (WARN_ON_ONCE(!tdx_keyid_start || !tdx_keyid_num))
> + return -EINVAL;
> +
> + /* The first keyID is reserved for the global key. */
> + return ida_alloc_range(&tdx_keyid_pool, tdx_keyid_start + 1,
> + tdx_keyid_start + tdx_keyid_num - 1,
> + GFP_KERNEL);
> +}
> +EXPORT_SYMBOL_GPL(tdx_keyid_alloc);
> +
> +void tdx_keyid_free(int keyid)
> +{
> + /* keyid = 0 is reserved. */
> + if (!keyid || keyid <= 0)
> + return;

Double check of keyid == 0.

I think you can just use:

if (keyid <= tdx_keyid_start)
return;

And/or add a WARN() as it's a bug if above happens.

> +
> + ida_free(&tdx_keyid_pool, keyid);
> +}
> +EXPORT_SYMBOL_GPL(tdx_keyid_free);
> +
> static void __init tdx_memory_destroy(void)
> {
> while (!list_empty(&tdx_memlist)) {