From: Petko Manolov <petko.manolov@konsulko.com>
To: "Paul E. McKenney" <paulmck@kernel.org>
Cc: David Laight <David.Laight@ACULAB.COM>,
LKML <linux-kernel@vger.kernel.org>
Subject: Re: [RFC] WRITE_ONCE_INC() and friends
Date: Mon, 20 Apr 2020 19:32:15 +0300 [thread overview]
Message-ID: <20200420163215.GA43378@carbon> (raw)
In-Reply-To: <20200420150545.GB17661@paulmck-ThinkPad-P72>
On 20-04-20 08:05:45, Paul E. McKenney wrote:
> On Sun, Apr 19, 2020 at 09:37:10PM +0000, David Laight wrote:
> > From: Petko Manolov
> > > Sent: 19 April 2020 19:30
> > >
> > > On 20-04-19 18:02:50, David Laight wrote:
> > > > From: Petko Manolov
> > > > > Sent: 19 April 2020 10:45
> > > > > Recently I started reading up on KCSAN and at some point I ran into stuff like:
> > > > >
> > > > > WRITE_ONCE(ssp->srcu_lock_nesting[idx], ssp->srcu_lock_nesting[idx] + 1);
> > > > > WRITE_ONCE(p->mm->numa_scan_seq, READ_ONCE(p->mm->numa_scan_seq) + 1);
> > > >
> > > > If all the accesses use READ/WRITE_ONCE() why not just mark the structure
> > > > field 'volatile'?
> > >
> > > This is a bit heavy. I guess you've read this one:
> > >
> > > https://lwn.net/Articles/233479/
> >
> > I remember reading something similar before.
> > I also remember a very old gcc (2.95?) that did a readback
> > after every volatile write on sparc (to flush the store buffer).
> > That broke everything.
> >
> > I suspect there is a lot more code that is attempting to be lockless
> > these days.
> > Ring buffers (one writer and one reader) are a typical example where
> > you don't need locks but do need to use a consistent value.
> >
> > Now you may also need ordering between accesses - which I think needs
> > more than volatile.
>
> In Petko's patch, all needed ordering is supplied by the fact that it is the
> same variable being read and written. But yes, in many other cases, more
> ordering is required.
There's pros and cons, as usual. Yet another macro(s) versus sorter/more
readable code. This is why i decided to spam the list (and Paul) - in search
for another opinion.
> > > And no, i am not sure all accesses are through READ/WRITE_ONCE(). If, for
> > > example, all others are from withing spin_lock/unlock pairs then we _may_ not
> > > need READ/WRITE_ONCE().
> >
> > The cost of volatile accesses is probably minimal unless the
> > code is written assuming the compiler will only access things once.
>
> And there are variables marked as volatile, for example, jiffies.
>
> But one downside of declaring variables volatile is that it can prevent KCSAN
> from spotting violations of the concurrency design for those variables.
Which would be unfortunate.
I just wish there was C type declaration that would force the compiler to do
what READ/WRITE_ONCE() does now, but i also know this is too naive... :)
Petko
> > > I merely proposed the _INC() variant for better readability.
> >
> > More like shorter code lines :-)
>
> That too! ;-)
>
> Thanx, Paul
next prev parent reply other threads:[~2020-04-20 16:41 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-04-19 9:44 [RFC] WRITE_ONCE_INC() and friends Petko Manolov
2020-04-19 18:02 ` David Laight
2020-04-19 18:29 ` Petko Manolov
2020-04-19 21:37 ` David Laight
2020-04-20 15:05 ` Paul E. McKenney
2020-04-20 16:32 ` Petko Manolov [this message]
2020-04-21 8:00 ` David Laight
2020-04-21 9:30 ` Petko Manolov
2020-04-20 22:57 ` Marco Elver
2020-04-20 23:12 ` Paul E. McKenney
2020-04-21 9:33 ` Marco Elver
2020-04-21 13:19 ` 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=20200420163215.GA43378@carbon \
--to=petko.manolov@konsulko.com \
--cc=David.Laight@ACULAB.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.