From: Eric Dumazet <eric.dumazet@gmail.com>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Ben Hutchings <bhutchings@solarflare.com>,
Andi Kleen <andi@firstfloor.org>,
linux-kernel <linux-kernel@vger.kernel.org>,
netdev <netdev@vger.kernel.org>,
Andrew Morton <akpm@linux-foundation.org>
Subject: Re: >Re: [RFC] should VM_BUG_ON(cond) really evaluate cond
Date: Fri, 28 Oct 2011 14:09:41 +0200 [thread overview]
Message-ID: <1319803781.23112.113.camel@edumazet-laptop> (raw)
In-Reply-To: <CA+55aFzHf563auNYdNeJnsbbry1OUfkepkiVHt7Fy-bG64Fruw@mail.gmail.com>
Le vendredi 28 octobre 2011 à 04:37 -0700, Linus Torvalds a écrit :
> On Thu, Oct 27, 2011 at 9:43 PM, Eric Dumazet <eric.dumazet@gmail.com> wrote:
> >
> > The only requirement of atomic_read() is that it must return value
> > before or after an atomic_write(), not a garbled value.
>
> The problem is that gcc *can* return a garbled value.
>
> > In fact, if a compiler is stupid enough to issue two reads on following
> > code :
>
> The compiler really *can* be that "stupid". Except the code tends to
> look like this:
>
> int value = atomic_read(&atomic_var);
> if (value > 10)
> return;
> .. do something with value ..
>
> and gcc may decide - under register pressure, and in the absense of a
> 'volatile' - to read 'value' first once for that "> 10" check, and
> then it drops the registers and instead of saving it on the stack
> frame, it can decide to re-load it from atomic_var.
>
> IOW, "value" could be two or more different values: one value when
> testing, and *another* value in "do something with value".
>
> This is why we have "ACCESS_ONCE()".
>
> Whether atomics guarantee ACCESS_ONCE() semantics or not is not
> entirely clear. But afaik, there is no way to tell gcc "access at
> *most* once, and never ever reload".
>
What you describe is true for non atomic variables as well, its not part
of the atomic_ops documented semantic.
And we do use ACCESS_ONCE() on the rare cases we need to make sure no
reload is done.
RCU use makes this implied (ACCESS_ONCE() being done in
rcu_dereference()), so we dont have many raw ACCESS_ONCE() in our code.
int value = ACCESS_ONCE(atomic_read(&atomic_var));
if (value > 10)
return;
.. do something with value ..
One of such rare use is explained in commit f1987257
(tcp: protect sysctl_tcp_cookie_size reads)
Since its a bit ugly, I suggested :
int value = atomic_read_once(&atomic_var);
if (value > 10)
return;
.. do something with value ..
I dont know, it seems the right way, but yes it might break things.
We can take the otherway and patch thousand atomic_read() to
atomic_read_stable(), its safer but very boring :)
next prev parent reply other threads:[~2011-10-28 12:09 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-10-28 1:19 [RFC] should VM_BUG_ON(cond) really evaluate cond Eric Dumazet
2011-10-28 1:25 ` Andi Kleen
2011-10-28 1:34 ` Linus Torvalds
2011-10-28 1:44 ` Ben Hutchings
2011-10-28 2:52 ` Eric Dumazet
2011-10-28 3:29 ` Ben Hutchings
2011-10-28 4:43 ` >Re: " Eric Dumazet
2011-10-28 11:37 ` Linus Torvalds
2011-10-28 12:09 ` Eric Dumazet [this message]
2011-10-28 12:19 ` Linus Torvalds
2011-10-28 12:40 ` Linus Torvalds
2011-10-28 14:47 ` Eric Dumazet
2011-10-28 14:55 ` Linus Torvalds
2011-10-29 15:43 ` Eric Dumazet
2011-10-29 17:34 ` Linus Torvalds
2011-10-30 8:52 ` Eric Dumazet
2011-10-30 9:59 ` Andi Kleen
2011-10-30 15:16 ` Eric Dumazet
2011-10-30 17:07 ` Linus Torvalds
2011-10-30 17:41 ` Eric Dumazet
2011-10-30 17:48 ` Linus Torvalds
2011-10-30 17:59 ` Eric Dumazet
2011-10-30 18:09 ` Linus Torvalds
2011-11-02 0:14 ` Eric Dumazet
2011-11-01 4:06 ` Stephen Rothwell
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=1319803781.23112.113.camel@edumazet-laptop \
--to=eric.dumazet@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=andi@firstfloor.org \
--cc=bhutchings@solarflare.com \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=torvalds@linux-foundation.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