* Reshetova, Elena <elena.reshet...@intel.com> wrote:

>  
> > * Elena Reshetova <elena.reshet...@intel.com> wrote:
> > 
> > > @@ -19,10 +19,15 @@ static int sgx_open(struct inode *inode, struct file
> > *file)
> > >   struct sgx_encl *encl;
> > >   int ret;
> > >
> > > + ret = sgx_inc_usage_count();
> > > + if (ret)
> > > +         return -EBUSY;
> > 
> > So if sgx_inc_usage_count() returns nonzero, it's in use already and we
> > return -EBUSY, right?
> 
> I guess my selection of error code here was wrong. 
> The intended logic is if sgx_inc_usage_count() returns nonzero,
> the *incrementing of counter failed* (due to failed EUPDATESVN)
> and we want to stop and report error.
>   
> > 
> > But:
> > 
> > >  int sgx_inc_usage_count(void)
> > >  {
> > > + int ret;
> > > +
> > > + /*
> > > +  * Increments from non-zero indicate EPC other
> > > +  * active EPC users and EUPDATESVN is not attempted.
> > > +  */
> > > + if (atomic64_inc_not_zero(&sgx_usage_count))
> > > +         return 0;
> > 
> > If sgx_usage_count is 1 here (ie. it's busy), this will return *zero*,
> > and sgx_open() will not run into the -EBUSY condition and will continue
> > assuming it has claimed the usage count, while it hasn't ...
> 
> Yes, meaning is different, see above. 

So that's rather convoluted:

        atomic64_inc_not_zero():   returns 1 on successful increase, 0 on 
failure
        sgx_inc_usage_count():     returns 0 on successful increase, 1 on 
failure
        sgx_open():                returns 0 on successful increase, -EBUSY on 
failure

Could we at least standardize sgx_inc_usage_count() on -EBUSY in the 
failure case, so it's a more obvious pattern:

+       ret = sgx_inc_usage_count();
+       if (ret < 0)
+               return ret;

Thanks,

        Ingo

Reply via email to