From: Peter Zijlstra <peterz@infradead.org>
To: Marco Elver <elver@google.com>
Cc: "Paul E. McKenney" <paulmck@kernel.org>,
Andrey Konovalov <andreyknvl@google.com>,
Alexander Potapenko <glider@google.com>,
Dmitry Vyukov <dvyukov@google.com>,
kasan-dev <kasan-dev@googlegroups.com>,
LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] kcsan: Add option to allow watcher interruptions
Date: Sat, 25 Jul 2020 19:44:30 +0200 [thread overview]
Message-ID: <20200725174430.GH10769@hirez.programming.kicks-ass.net> (raw)
In-Reply-To: <CANpmjNPhuvrhRHAiuv2Zju1VNSe7dO0aaYn+1TB99OF2Hv0S_A@mail.gmail.com>
On Sat, Jul 25, 2020 at 05:17:43PM +0200, Marco Elver wrote:
> On Sat, 25 Jul 2020 at 16:56, Paul E. McKenney <paulmck@kernel.org> wrote:
> > On Thu, Feb 20, 2020 at 10:33:17PM +0100, Marco Elver wrote:
> > > On Thu, 20 Feb 2020, Paul E. McKenney wrote:
> > > > On Thu, Feb 20, 2020 at 03:15:51PM +0100, Marco Elver wrote:
> > > > > Add option to allow interrupts while a watchpoint is set up. This can be
> > > > > enabled either via CONFIG_KCSAN_INTERRUPT_WATCHER or via the boot
> > > > > parameter 'kcsan.interrupt_watcher=1'.
> [...]
> > > > > As an example, the first data race that this found:
> > > > >
> > > > > write to 0xffff88806b3324b8 of 4 bytes by interrupt on cpu 0:
> > > > > rcu_preempt_read_enter kernel/rcu/tree_plugin.h:353 [inline]
> > > > > __rcu_read_lock+0x3c/0x50 kernel/rcu/tree_plugin.h:373
> [...]
> > > > > read to 0xffff88806b3324b8 of 4 bytes by task 6131 on cpu 0: |
> > > > > rcu_preempt_read_enter kernel/rcu/tree_plugin.h:353 [inline] ----+
> [...]
> > > > >
> > > > > The writer is doing 'current->rcu_read_lock_nesting++'. The read is as
> > > > > vulnerable to compiler optimizations and would therefore conclude this
> > > > > is a valid data race.
> > > >
> > > > Heh! That one is a fun one! It is on a very hot fastpath. READ_ONCE()
> > > > and WRITE_ONCE() are likely to be measurable at the system level.
> > > >
> > > > Thoughts on other options?
> > > diff --git a/kernel/rcu/tree_plugin.h b/kernel/rcu/tree_plugin.h
> > > index c6ea81cd41890..e0595abd50c0f 100644
> > > --- a/kernel/rcu/tree_plugin.h
> > > +++ b/kernel/rcu/tree_plugin.h
> > > @@ -350,17 +350,17 @@ static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
> > >
> > > static void rcu_preempt_read_enter(void)
> > > {
> > > - current->rcu_read_lock_nesting++;
> > > + local_inc(¤t->rcu_read_lock_nesting);
> > > }
> > >
> > > static void rcu_preempt_read_exit(void)
> > > {
> > > - current->rcu_read_lock_nesting--;
> > > + local_dec(¤t->rcu_read_lock_nesting);
> > > }
> > >
> > > static void rcu_preempt_depth_set(int val)
> > > {
> > > - current->rcu_read_lock_nesting = val;
> > > + local_set(¤t->rcu_read_lock_nesting, val);
>
> > I agree that this removes the data races, and that the code for x86 is
> > quite nice, but aren't rcu_read_lock() and rcu_read_unlock() going to
> > have heavyweight atomic operations on many CPUs?
> >
> > Maybe I am stuck with arch-specific code in rcu_read_lock() and
> > rcu_preempt_read_exit(). I suppose worse things could happen.
>
> Peter also mentioned to me that while local_t on x86 generates
> reasonable code, on other architectures it's terrible. So I think
> something else is needed, and feel free to discard the above idea.
> With sufficient enough reasoning, how bad would a 'data_race(..)' be?
Right, so local_t it atrocious on many architectures, they fall back to
atomic_t.
Even architectures that have optimized variants (eg. Power), they're
quite a lot more expensive than what we actually need here.
Only architectures like x86 that have single instruction memops can
generate anywhere near the code that we'd want here.
So the thing is, since RCU count is 0 per context (an IRQ must have an
equal amount of rcu_read_unlock() as it has rcu_read_lock()), interrupts
are not in fact a problem, even on load-store (RISC) architectures
(preempt_count has the same thing).
So the addition/subtraction in rcu_preempt_read_{enter,exit}() doesn't
need to be atomic vs interrupts. The only thing we really do need is
them being single-copy-atomic.
The problem with READ/WRITE_ONCE is that if we were to use it, we'd end
up with a load-store, even on x86, which is sub-optimal.
I suppose the 'correct' code here would be something like:
*((volatile int *)¤t->rcu_read_lock_nesting)++;
then the compiler can either do a single memop (x86 and the like) or a
load-store that is free from tearing.
next prev parent reply other threads:[~2020-07-25 17:44 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-02-20 14:15 Marco Elver
2020-02-20 18:58 ` Paul E. McKenney
2020-02-20 21:33 ` Marco Elver
2020-07-25 14:56 ` Paul E. McKenney
2020-07-25 15:17 ` Marco Elver
2020-07-25 17:44 ` Peter Zijlstra [this message]
2020-07-25 19:39 ` Paul E. McKenney
2020-07-25 20:10 ` peterz
2020-07-25 20:21 ` peterz
2020-07-25 22:07 ` Paul E. McKenney
2020-07-26 11:52 ` peterz
2020-07-26 18:09 ` Paul E. McKenney
2020-07-27 1:48 ` Paul E. McKenney
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=20200725174430.GH10769@hirez.programming.kicks-ass.net \
--to=peterz@infradead.org \
--cc=andreyknvl@google.com \
--cc=dvyukov@google.com \
--cc=elver@google.com \
--cc=glider@google.com \
--cc=kasan-dev@googlegroups.com \
--cc=linux-kernel@vger.kernel.org \
--cc=paulmck@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
all inboxes | Powered by JetHome®