From: Linus Torvalds <torvalds@osdl.org>
To: Albert Cahalan <acahalan@gmail.com>
Cc: tglx@linutronix.de, joe.korty@ccur.com,
linux-kernel@vger.kernel.org, linux-os@analogic.com,
khc@pm.waw.pl, mingo@elte.hu, akpm@osdl.org, arjan@infradead.org
Subject: Re: [patch] spinlocks: remove 'volatile'
Date: Sat, 8 Jul 2006 13:11:01 -0700 (PDT) [thread overview]
Message-ID: <Pine.LNX.4.64.0607081256170.3869@g5.osdl.org> (raw)
In-Reply-To: <787b0d920607081233w3e0e99a9n706ff510c3de458b@mail.gmail.com>
On Sat, 8 Jul 2006, Albert Cahalan wrote:
> >
> > 1. The volatile implementation of gcc is correct. The standard does not
> > talk about busses, not even about SMP.
>
> The standard need not. An implementation must deal
> with whatever odd hardware happens to be in use.
Not really.
The fact is, "volatile" simply doesn't inform the compiler enough about
what the effect of an access could be under various different situations.
So the compiler really has no choice. It has to balance the fact that the
standard requires it to do _something_ different, with the fact that
there's really not a lot of information that the user gave, apart from the
one bit of "it's volatile".
So the compiler really has no choice.
Btw, I think that the whole standard definition of "volatile" is pretty
weak and useless. The standard could be improved, and a way to improve the
definition of volatile would actually be to say something like
"volatile" implies that the access to that entity can alias with
any other access.
That's actually a lot simpler for a compiler writer (a C compiler already
has to know about the notion of data aliasing), and gives a lot more
useful (and strict) semantics to the whole concept.
So to look at the previous example of
extern int a;
extern int volatile b;
void testfn(void)
{
a++;
b++;
}
_my_ definition of "volatile" is actually totally unambiguous, and not
just simpler than the current standard, it is also stronger. It would make
it clearly invalid to read the value of "b" until the value of "a" has
been written, because (by my definition), "b" may actually alias the value
of "a", so you clearly cannot read "b" until "a" has been updated.
At the same time, there's no question that
addl $1,a
addl $1,b
is a clearly valid instruction sequence by my simpler definition of
volatile. The fact that "b" can alias with itself is a tautology, and is
true of normal variables too, so any combination of ops on one variable
(any variable always aliases _itself_) is by definition clearly always
valid on a "volatile" variable too, and thus a compiler that can do the
combination of "load + increment + store" on a normal variable should
always do so on a volatile one too.
In contrast, the current C standard definition of "volatile" is not only
cumbersome and inconvenient, it's also badly defined when it comes to
accesses to _other_ data, making it clearly less useful.
I personally think that my simpler definition of volatile is actually a
perfectly valid implementation of the current definition of volatile, and
I suggested it to some gcc people as a better way to handle "volatile"
inside gcc while still being standards-conforming (ie the "can alias
anything" thing is not just clearer and simpler, it's strictly a subset of
what the C standard allows, meaning that I think you can adopt my
definition _without_ breaking any old programs or standards).
But there really is no way to "fix" volatile. You will always invariably
need other things too (inline assembly with "lock" prefixes etc) to
actually create true lock primitives. The suggested "can alias anything"
semantics just clarify what it means, and thus make it less ambiguous. It
doesn't make it fundamentally more useful in general.
Linus
next prev parent reply other threads:[~2006-07-08 20:11 UTC|newest]
Thread overview: 119+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-07-08 3:54 [patch] spinlocks: remove 'volatile' Albert Cahalan
2006-07-08 5:25 ` Linus Torvalds
2006-07-08 6:39 ` Nick Piggin
2006-07-08 18:53 ` Linus Torvalds
2006-07-08 9:45 ` Joe Korty
2006-07-08 9:52 ` Arjan van de Ven
2006-07-08 9:59 ` David Schwartz
2006-07-08 10:24 ` Thomas Gleixner
2006-07-08 15:49 ` Albert Cahalan
2006-07-08 18:31 ` Thomas Gleixner
2006-07-08 19:33 ` Albert Cahalan
2006-07-08 20:11 ` Linus Torvalds [this message]
2006-07-09 21:10 ` Pavel Machek
2006-07-09 22:18 ` Linus Torvalds
2006-07-10 16:25 ` marty fouts
2006-07-08 23:10 ` David Schwartz
2006-07-08 13:57 ` Andi Kleen
2006-07-08 19:13 ` Linus Torvalds
2006-07-08 10:45 ` Krzysztof Halasa
-- strict thread matches above, loose matches on Subject: below --
2006-07-08 6:12 trajce nedev
2006-07-08 6:19 ` Chase Venters
2006-07-08 6:45 ` trajce nedev
2006-07-08 6:58 ` Arjan van de Ven
2006-07-08 7:02 ` Vadim Lobanov
2006-07-08 13:46 ` Chase Venters
2006-07-09 4:39 ` Benjamin Herrenschmidt
2006-07-14 3:30 ` Steven Rostedt
2006-07-08 18:23 ` Linus Torvalds
2006-07-07 10:21 Chuck Ebbert
2006-07-07 17:09 ` Linus Torvalds
2006-07-05 11:46 [patch] uninline init_waitqueue_*() functions Ingo Molnar
2006-07-05 17:10 ` Andrew Morton
2006-07-05 19:35 ` Ingo Molnar
2006-07-05 20:18 ` Andrew Morton
2006-07-05 20:36 ` Linus Torvalds
2006-07-05 20:47 ` Ingo Molnar
2006-07-05 21:21 ` Linus Torvalds
2006-07-05 21:45 ` Ingo Molnar
2006-07-05 22:09 ` Linus Torvalds
2006-07-05 23:11 ` Linus Torvalds
2006-07-06 8:16 ` [patch] spinlocks: remove 'volatile' Ingo Molnar
2006-07-06 8:23 ` Ingo Molnar
2006-07-06 9:27 ` Heiko Carstens
2006-07-06 9:34 ` Arjan van de Ven
2006-07-06 11:59 ` linux-os (Dick Johnson)
2006-07-06 12:01 ` Arjan van de Ven
2006-07-06 12:29 ` linux-os (Dick Johnson)
2006-07-06 12:39 ` Arjan van de Ven
2006-07-06 13:39 ` J.A. Magallón
2006-07-06 13:43 ` Arjan van de Ven
2006-07-06 14:05 ` Chase Venters
2006-07-06 14:26 ` Andreas Schwab
2006-07-06 16:40 ` Nick Piggin
2006-07-06 23:19 ` David Schwartz
2006-07-06 18:15 ` Mark Lord
2006-07-06 19:15 ` Linus Torvalds
2006-07-06 19:33 ` Chris Friesen
2006-07-06 19:37 ` Mark Lord
2006-07-06 20:28 ` Chris Friesen
2006-07-06 20:32 ` Linus Torvalds
2006-07-06 19:38 ` Arjan van de Ven
2006-07-06 19:41 ` Måns Rullgård
2006-07-06 19:42 ` Jeff Garzik
2006-07-06 19:58 ` Linus Torvalds
2006-07-06 20:27 ` Mark Lord
2006-07-06 20:40 ` Chris Friesen
2006-07-06 21:00 ` Linus Torvalds
2006-07-08 8:40 ` Avi Kivity
2006-07-08 8:51 ` Arjan van de Ven
2006-07-08 9:20 ` Avi Kivity
2006-07-08 9:51 ` Arjan van de Ven
2006-07-08 10:18 ` Avi Kivity
2006-07-08 10:28 ` Thomas Gleixner
2006-07-09 4:19 ` Benjamin Herrenschmidt
2006-07-09 12:47 ` Avi Kivity
2006-07-09 19:16 ` David Schwartz
2006-07-09 19:51 ` Theodore Tso
2006-07-06 16:07 ` Linus Torvalds
2006-07-06 16:13 ` Linus Torvalds
2006-07-06 17:04 ` Jeff Garzik
2006-07-06 17:52 ` linux-os (Dick Johnson)
2006-07-06 18:00 ` Linus Torvalds
2006-07-06 21:02 ` J.A. Magallón
2006-07-06 21:12 ` Linus Torvalds
2006-07-06 18:10 ` Michael Buesch
2006-07-06 18:16 ` Chase Venters
2006-07-07 18:16 ` Krzysztof Halasa
2006-07-07 19:51 ` linux-os (Dick Johnson)
2006-07-07 20:25 ` Linus Torvalds
2006-07-07 21:22 ` linux-os (Dick Johnson)
2006-07-07 21:48 ` Chase Venters
2006-07-08 10:00 ` Krzysztof Halasa
2006-07-08 13:41 ` Chase Venters
2006-07-08 20:09 ` Krzysztof Halasa
2006-07-08 20:40 ` Chase Venters
2006-07-08 20:47 ` Chase Venters
2006-07-09 10:57 ` Krzysztof Halasa
2006-07-07 21:59 ` Linus Torvalds
2006-07-07 22:06 ` Linus Torvalds
2006-07-08 20:49 ` Pavel Machek
2006-07-08 21:43 ` Linus Torvalds
2006-07-07 22:05 ` J.A. Magallón
2006-07-07 22:22 ` Chase Venters
2006-07-07 22:37 ` J.A. Magallón
2006-07-08 9:33 ` David Schwartz
2006-07-07 22:49 ` J.A. Magallón
2006-07-07 22:59 ` Vadim Lobanov
2006-07-07 23:18 ` Chase Venters
2006-07-07 23:36 ` Davide Libenzi
2006-07-07 22:09 ` Ingo Molnar
2006-07-08 7:36 ` Ingo Molnar
2006-07-07 20:39 ` Krzysztof Halasa
2006-07-07 23:06 ` Björn Steinbrink
2006-07-08 8:36 ` Avi Kivity
2006-07-06 19:32 ` Jan Engelhardt
2006-07-06 20:26 ` Jeremy Fitzhardinge
2006-07-06 20:55 ` Jan Engelhardt
2006-07-06 21:07 ` Linus Torvalds
2006-07-06 19:56 ` Linus Torvalds
2006-07-06 20:34 ` Linus Torvalds
2006-07-08 22:50 ` Ralf Baechle
2006-07-09 3:09 ` Linus Torvalds
2006-07-09 3:07 ` Keith Owens
2006-07-09 3:29 ` Linus Torvalds
2006-07-09 3:43 ` Keith Owens
2006-07-09 3:58 ` Linus Torvalds
2006-07-09 6:13 ` David Miller
2006-07-09 14:28 ` Roman Zippel
2006-07-09 15:27 ` Linus Torvalds
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=Pine.LNX.4.64.0607081256170.3869@g5.osdl.org \
--to=torvalds@osdl.org \
--cc=acahalan@gmail.com \
--cc=akpm@osdl.org \
--cc=arjan@infradead.org \
--cc=joe.korty@ccur.com \
--cc=khc@pm.waw.pl \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-os@analogic.com \
--cc=mingo@elte.hu \
--cc=tglx@linutronix.de \
/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.