Re: [PATCH v4 1/1] x86/sgx: Enable automatic SVN updates for SGX enclaves

From: Jarkko Sakkinen
Date: Thu May 08 2025 - 16:13:35 EST


On Wed, May 07, 2025 at 02:14:00PM +0300, Elena Reshetova wrote:

> diff --git a/arch/x86/kernel/cpu/sgx/driver.c b/arch/x86/kernel/cpu/sgx/driver.c
> index 7f8d1e11dbee..669e44d61f9f 100644
> --- a/arch/x86/kernel/cpu/sgx/driver.c
> +++ b/arch/x86/kernel/cpu/sgx/driver.c
> @@ -19,6 +19,10 @@ static int sgx_open(struct inode *inode, struct file *file)
> struct sgx_encl *encl;
> int ret;
>
> + ret = sgx_inc_usage_count();
> + if (ret)
> + return ret;
> +
> encl = kzalloc(sizeof(*encl), GFP_KERNEL);
> if (!encl)
> return -ENOMEM;

The rollback looks broken to me.

Let's clean up error handling a bit:

encl = kzalloc(sizeof(*encl), GFP_KERNEL);
if (!encl) {
ret = -ENOMEM;
goto err_usage_count;
}

And later on in the same function:

ret = init_srcu_struct(&encl->srcu);
if (ret)
goto err_encl;

And finally tail:

return 0;

err_encl:
kfree(encl);

err_usage_count:
sgx_dec_usage_count();
return ret;
}

BR, Jarkko