RE: [PATCH v12 5/5] x86/sgx: Enable automatic SVN updates for SGX enclaves
From: Reshetova, Elena
Date: Mon Aug 11 2025 - 08:14:41 EST
> -----Original Message-----
> From: Huang, Kai <kai.huang@xxxxxxxxx>
> Sent: Monday, August 11, 2025 1:50 PM
> To: Reshetova, Elena <elena.reshetova@xxxxxxxxx>; Hansen, Dave
> <dave.hansen@xxxxxxxxx>
> Cc: seanjc@xxxxxxxxxx; mingo@xxxxxxxxxx; Scarlata, Vincent R
> <vincent.r.scarlata@xxxxxxxxx>; x86@xxxxxxxxxx; jarkko@xxxxxxxxxx;
> Annapurve, Vishal <vannapurve@xxxxxxxxxx>; linux-kernel@xxxxxxxxxxxxxxx;
> Mallick, Asit K <asit.k.mallick@xxxxxxxxx>; Aktas, Erdem
> <erdemaktas@xxxxxxxxxx>; Cai, Chong <chongc@xxxxxxxxxx>; Bondarevska,
> Nataliia <bondarn@xxxxxxxxxx>; linux-sgx@xxxxxxxxxxxxxxx; Raynor, Scott
> <scott.raynor@xxxxxxxxx>
> Subject: Re: [PATCH v12 5/5] x86/sgx: Enable automatic SVN updates for SGX
> enclaves
>
>
> >
> > +/* Mutex to ensure no concurrent EPC accesses during EUPDATESVN */
> > +static DEFINE_MUTEX(sgx_svn_lock);
> > +
> > int sgx_inc_usage_count(void)
> > {
> > + guard(mutex)(&sgx_svn_lock);
> > +
> > + if (sgx_usage_count++ == 0)
> > + return sgx_update_svn();
> > +
>
> Hmm.. sorry for not noticing this before.. But I think we might have a
> problem here since the sgx_usage_count is increased regardless of the
> result of sgx_update_svn().
>
> If sgx_update_svn() fails, it makes sgx_inc_usage_count() return error
> too, so sgx_{vepc_}open() will fail and return immediately w/o calling
> sgx_dec_usage_count().
>
> But the sgx_usage_count has been increased.
>
> AFAICT when sgx_{vepc_}_open() fails, the sgx_{vepc_}release() is not
> called, so sgx_dec_usage_count() is never called and sgx_usage_count
> remains increased.
>
> So when sgx_{vepc_}open() calls sgx_inc_usage_count() again, it will skip
> calling sgx_update_svn(), and allow enclave/vEPC to be created
> successfully, which just defeats the purpose.
>
> So if I am not missing anything, I think we should only increase the count
> when sgx_update_svn() returns success?
Yes, you are right, thanks for catching this! In past the atomic version of
this patch did exactly, but after I went into this simplified version of counting,
this angle got broken.
Will fix.
Best Regards,
Elena.