[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20191009174124.GD22902@worktop.programming.kicks-ass.net>
Date: Wed, 9 Oct 2019 19:41:24 +0200
From: Peter Zijlstra <peterz@...radead.org>
To: Will Deacon <will@...nel.org>
Cc: linux-kernel@...r.kernel.org, Kees Cook <keescook@...omium.org>,
Ingo Molnar <mingo@...nel.org>,
Elena Reshetova <elena.reshetova@...el.com>,
Ard Biesheuvel <ard.biesheuvel@...aro.org>,
Hanjun Guo <guohanjun@...wei.com>,
Jan Glauber <jglauber@...vell.com>
Subject: Re: [PATCH v3 05/10] lib/refcount: Improve performance of generic
REFCOUNT_FULL code
On Wed, Oct 09, 2019 at 05:44:33PM +0100, Will Deacon wrote:
> > > @@ -224,26 +208,19 @@ static inline void refcount_inc(refcount_t *r)
> > > */
> > > 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))
>
> Well spotted! I think we just want:
>
> if (unlikely(old < 0 || old - i < 0))
>
> here, which is reassuringly similar to the logic in refcount_add() and
> refcount_add_not_zero().
Oh indeed, I missed that saturated was negative. That should work.
> > > + return false;
> > > }
> > >
> > > /**
> > > @@ -276,9 +253,13 @@ static inline __must_check bool refcount_dec_and_test(refcount_t *r)
> > > */
> > > static inline void refcount_dec(refcount_t *r)
> > > {
> > > + int old = atomic_fetch_sub_release(1, &r->refs);
> > >
> > > + if (unlikely(old <= 1)) {
> >
> > Idem.
>
> Hmm, I don't get what you mean with the one, since we're looking at the
> old value. REFCOUNT_SATURATED is negative, so it will do the right thing.
Yep, missed that.
> > > + refcount_set(r, REFCOUNT_SATURATED);
> > > + WARN_ONCE(1, "refcount_t: decrement hit 0; leaking memory.\n");
> > > + }
> > > +}
> >
> > Also, things like refcount_dec_not_one() might need fixing to preserve
> > REFCOUNT_SATURATED, because they're not expecting that value to actually
> > change, but you do!
>
> refcount_dec_not_one() already checks for REFCOUNT_SATURATED and, in the
> case of a racing thread setting the saturated value, the cmpxchg() will
> fail if the saturated value is written after the check or the saturated
> value will overwrite the value written by the cmpxchg(). Is there another
> race that you're thinking of?
Hmm, yes. I was afraid that by not recognising SATURATED it'd go wrong,
but now that I try I can't make it go wrong.
Powered by blists - more mailing lists