On Wed, Oct 09, 2019 at 11:25:08AM +0200, Peter Zijlstra wrote:
> On Mon, Oct 07, 2019 at 04:46:58PM +0100, Will Deacon wrote:
> >  static inline __must_check bool refcount_sub_and_test(int i, refcount_t *r)
> >  {
> > +   int old = atomic_fetch_sub_release(i, &r->refs);
> >  
> > +   if (old == i) {
> >             smp_acquire__after_ctrl_dep();
> >             return true;
> >     }
> >  
> > +   if (unlikely(old - i < 0)) {
> > +           refcount_set(r, REFCOUNT_SATURATED);
> > +           WARN_ONCE(1, "refcount_t: underflow; use-after-free.\n");
> > +   }
> 
> I'm failing to see how this preserves REFCOUNT_SATURATED for
> non-underflow. AFAICT this should have:
> 
>       if (unlikely(old == REFCOUNT_SATURATED || old - i < 0))

Hmm, that is not sufficient, since you can be arbitrarily far away from
it due to all the races (and add/sub really suck as a refcount
interface). The same will make fixing the cmpxchg loops like
dec_not_one() 'interesting'.

It is important though; to keep saturated, otherwise something that can
do INT_MAX+n actual increments will get freed after INT_MAX decrements
and still have n 'proper' references, *whoopsie*.

> 
> > +   return false;
> >  }
> >  
> >  /**

Reply via email to