On Mon, 2025-05-19 at 11:47 +0000, Reshetova, Elena wrote: > > On Mon, 2025-05-19 at 10:24 +0300, Elena Reshetova wrote: > > > Currently SGX does not have a global counter to count the > > > active users from userspace or hypervisor. Implement such a counter, > > > sgx_usage_count. It will be used by the driver when attempting > > > to call EUPDATESVN SGX instruction. > > > > > > Suggested-by: Sean Christopherson <sea...@google.com> > > > Signed-off-by: Elena Reshetova <elena.reshet...@intel.com> > > > --- > > > arch/x86/kernel/cpu/sgx/driver.c | 1 + > > > arch/x86/kernel/cpu/sgx/encl.c | 1 + > > > arch/x86/kernel/cpu/sgx/main.c | 14 ++++++++++++++ > > > arch/x86/kernel/cpu/sgx/sgx.h | 3 +++ > > > arch/x86/kernel/cpu/sgx/virt.c | 2 ++ > > > 5 files changed, 21 insertions(+) > > > > > > diff --git a/arch/x86/kernel/cpu/sgx/driver.c > > b/arch/x86/kernel/cpu/sgx/driver.c > > > index 7f8d1e11dbee..b5ffe104af4c 100644 > > > --- a/arch/x86/kernel/cpu/sgx/driver.c > > > +++ b/arch/x86/kernel/cpu/sgx/driver.c > > > @@ -19,6 +19,7 @@ static int sgx_open(struct inode *inode, struct file > > > *file) > > > struct sgx_encl *encl; > > > int ret; > > > > > > + sgx_inc_usage_count(); > > > > This should be done at the end of sgx_open() where the open cannot fail, > > since > > sgx_release() is not called if sgx_open() failed AFAICT. > > > Could you please elaborate a bit more on this? > In case sgx_inc_usage_count fails, we dont allocate resources yet, so what is > wrong? >
I haven't looked into (details of) the last patch yet, but for _this_ patch only, doing sgx_inc_usage_count() at the beginning of sgx_open() will result in the usage count being increased even when sgx_open() fails at a later time. Since sgx_release() is not called when sgx_open() fails, the usage count will not be decreased correctly. No?