From: Peter Zijlstra <peterz@infradead.org>
To: Will Deacon <will@kernel.org>
Cc: linux-kernel@vger.kernel.org, Kees Cook <keescook@chromium.org>,
Ingo Molnar <mingo@kernel.org>,
Elena Reshetova <elena.reshetova@intel.com>,
Ard Biesheuvel <ard.biesheuvel@linaro.org>,
Hanjun Guo <guohanjun@huawei.com>,
Jan Glauber <jglauber@marvell.com>
Subject: Re: [PATCH v3 05/10] lib/refcount: Improve performance of generic REFCOUNT_FULL code
Date: Wed, 9 Oct 2019 19:41:24 +0200 [thread overview]
Message-ID: <20191009174124.GD22902@worktop.programming.kicks-ass.net> (raw)
In-Reply-To: <20191009164433.4dhgsmgkl4pe2nlx@willie-the-truck>
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.
next prev parent reply other threads:[~2019-10-09 17:41 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-10-07 15:46 [PATCH v3 00/10] Rework REFCOUNT_FULL using atomic_fetch_* operations Will Deacon
2019-10-07 15:46 ` [PATCH v3 01/10] lib/refcount: Define constants for saturation and max refcount values Will Deacon
2019-10-07 15:46 ` [PATCH v3 02/10] lib/refcount: Ensure integer operands are treated as signed Will Deacon
2019-10-07 15:46 ` [PATCH v3 03/10] lib/refcount: Remove unused refcount_*_checked() variants Will Deacon
2019-10-07 15:46 ` [PATCH v3 04/10] lib/refcount: Move bulk of REFCOUNT_FULL implementation into header Will Deacon
2019-10-07 15:46 ` [PATCH v3 05/10] lib/refcount: Improve performance of generic REFCOUNT_FULL code Will Deacon
2019-10-09 9:25 ` Peter Zijlstra
2019-10-09 11:35 ` Peter Zijlstra
2019-10-09 16:44 ` Will Deacon
2019-10-09 17:41 ` Peter Zijlstra [this message]
2019-10-07 15:46 ` [PATCH v3 06/10] lib/refcount: Move saturation warnings out of line Will Deacon
2019-10-10 20:48 ` Kees Cook
2019-10-11 12:09 ` Will Deacon
2019-10-07 15:47 ` [PATCH v3 07/10] lib/refcount: Consolidate REFCOUNT_{MAX,SATURATED} definitions Will Deacon
2019-10-07 15:47 ` [PATCH v3 08/10] refcount: Consolidate implementations of refcount_t Will Deacon
2019-10-10 20:49 ` Kees Cook
2019-10-07 15:47 ` [PATCH v3 09/10] lib/refcount: Remove unused 'refcount_error_report()' function Will Deacon
2019-10-10 20:50 ` Kees Cook
2019-10-11 12:12 ` Will Deacon
2019-10-07 15:47 ` [PATCH v3 10/10] drivers/lkdtm: Remove references to CONFIG_REFCOUNT_FULL Will Deacon
2019-10-10 20:50 ` Kees Cook
2019-10-09 10:01 ` [PATCH v3 00/10] Rework REFCOUNT_FULL using atomic_fetch_* operations Hanjun Guo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20191009174124.GD22902@worktop.programming.kicks-ass.net \
--to=peterz@infradead.org \
--cc=ard.biesheuvel@linaro.org \
--cc=elena.reshetova@intel.com \
--cc=guohanjun@huawei.com \
--cc=jglauber@marvell.com \
--cc=keescook@chromium.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).