* [PATCH] "volatile considered harmful", take 3
@ 2007-05-11 17:36 Jonathan Corbet
2007-05-11 21:25 ` Jesper Juhl
2007-05-12 3:21 ` Satyam Sharma
0 siblings, 2 replies; 22+ messages in thread
From: Jonathan Corbet @ 2007-05-11 17:36 UTC (permalink / raw)
To: akpm
Cc: linux-kernel, Johannes Stezenbach, Jesper Juhl, Randy Dunlap,
Heikki Orsila, H. Peter Anvin, Satyam Sharma, jimmy bahuleyan,
Stefan Richter
Here's another version of the volatile document. Once again, I've tried
to address all of the comments. There haven't really been any recent
comments addressing the correctness of the document; people have been
more concerned with how it's expressed. I'm glad to see files in
Documentation/ held to a high standard of writing, but, unless somebody
has a factual issue this time around I would like to declare Mission
Accomplished and move on.
Thanks,
jon
---
Encourage developers to avoid the volatile type class in kernel code.
Signed-off-by: Jonathan Corbet <corbet@lwn.net>
diff --git a/Documentation/volatile-considered-harmful.txt b/Documentation/volatile-considered-harmful.txt
new file mode 100644
index 0000000..955c309
--- /dev/null
+++ b/Documentation/volatile-considered-harmful.txt
@@ -0,0 +1,119 @@
+Why the "volatile" type class should not be used
+------------------------------------------------
+
+C programmers have often taken volatile to mean that the variable could be
+changed outside of the current thread of execution; as a result, they are
+sometimes tempted to use it in kernel code when shared data structures are
+being used. In other words, they have been known to treat volatile types
+as a sort of easy atomic variable, which they are not. The use of volatile in
+kernel code is almost never correct; this document describes why.
+
+The key point to understand with regard to volatile is that its purpose is
+to suppress optimization, which is almost never what one really wants to
+do. In the kernel, one must protect shared data structures against
+unwanted concurrent access, which is very much a different task. The
+process of protecting against unwanted concurrency will also avoid almost
+all optimization-related problems in a more efficient way.
+
+Like volatile, the kernel primitives which make concurrent access to data
+safe (spinlocks, mutexes, memory barriers, etc.) are designed to prevent
+unwanted optimization. If they are being used properly, there will be no
+need to use volatile as well. If volatile is still necessary, there is
+almost certainly a bug in the code somewhere. In properly-written kernel
+code, volatile can only serve to slow things down.
+
+Consider a typical block of kernel code:
+
+ spin_lock(&the_lock);
+ do_something_on(&shared_data);
+ do_something_else_with(&shared_data);
+ spin_unlock(&the_lock);
+
+If all the code follows the locking rules, the value of shared_data cannot
+change unexpectedly while the_lock is held. Any other code which might
+want to play with that data will be waiting on the lock. The spinlock
+primitives act as memory barriers - they are explicitly written to do so -
+meaning that data accesses will not be optimized across them. So the
+compiler might think it knows what will be in shared_data, but the
+spin_lock() call, since it acts as a memory barrier, will force it to
+forget anything it knows. There will be no optimization problems with
+accesses to that data.
+
+If shared_data were declared volatile, the locking would still be
+necessary. But the compiler would also be prevented from optimizing access
+to shared_data _within_ the critical section, when we know that nobody else
+can be working with it. While the lock is held, shared_data is not
+volatile. When dealing with shared data, proper locking makes volatile
+unnecessary - and potentially harmful.
+
+The volatile storage class was originally meant for memory-mapped I/O
+registers. Within the kernel, register accesses, too, should be protected
+by locks, but one also does not want the compiler "optimizing" register
+accesses within a critical section. But, within the kernel, I/O memory
+accesses are always done through accessor functions; accessing I/O memory
+directly through pointers is frowned upon and does not work on all
+architectures. Those accessors are written to prevent unwanted
+optimization, so, once again, volatile is unnecessary.
+
+Another situation where one might be tempted to use volatile is
+when the processor is busy-waiting on the value of a variable. The right
+way to perform a busy wait is:
+
+ while (my_variable != what_i_want)
+ cpu_relax();
+
+The cpu_relax() call can lower CPU power consumption or yield to a
+hyperthreaded twin processor; it also happens to serve as a memory barrier,
+so, once again, volatile is unnecessary. Of course, busy-waiting is
+generally an anti-social act to begin with.
+
+There are still a few rare situations where volatile makes sense in the
+kernel:
+
+ - The above-mentioned accessor functions might use volatile on
+ architectures where direct I/O memory access does work. Essentially,
+ each accessor call becomes a little critical section on its own and
+ ensures that the access happens as expected by the programmer.
+
+ - Inline assembly code which changes memory, but which has no other
+ visible side effects, risks being deleted by GCC. Adding the volatile
+ keyword to asm statements will prevent this removal.
+
+ - The jiffies variable is special in that it can have a different value
+ every time it is referenced, but it can be read without any special
+ locking. So jiffies can be volatile, but the addition of other
+ variables of this type is strongly frowned upon. Jiffies is considered
+ to be a "stupid legacy" issue (Linus's words) in this regard; fixing it
+ would be more trouble than it is worth.
+
+ - Pointers to data structures in coherent memory which might be modified
+ by I/O devices can, sometimes, legitimately be volatile. A ring buffer
+ used by a network adapter, where that adapter changes pointers to
+ indicate which descriptors have been processed, is an example of this
+ type of situation.
+
+For most code, none of the above justifications for volatile apply. As a
+result, the use of volatile is likely to be seen as a bug and will bring
+additional scrutiny to the code. Developers who are tempted to use
+volatile should take a step back and think about what they are truly trying
+to accomplish.
+
+Patches to remove volatile variables are generally welcome - as long as
+they come with a justification which shows that the concurrency issues have
+been properly thought through.
+
+
+NOTES
+-----
+
+[1] http://lwn.net/Articles/233481/
+[2] http://lwn.net/Articles/233482/
+
+CREDITS
+-------
+
+Original impetus and research by Randy Dunlap
+Written by Jonathan Corbet
+Improvements via coments from Satyam Sharma, Johannes Stezenbach, Jesper
+ Juhl, Heikki Orsila, H. Peter Anvin, Philipp Hahn, and Stefan
+ Richter.
^ permalink raw reply related [flat|nested] 22+ messages in thread
* Re: [PATCH] "volatile considered harmful", take 3
2007-05-11 17:36 [PATCH] "volatile considered harmful", take 3 Jonathan Corbet
@ 2007-05-11 21:25 ` Jesper Juhl
2007-05-12 3:21 ` Satyam Sharma
1 sibling, 0 replies; 22+ messages in thread
From: Jesper Juhl @ 2007-05-11 21:25 UTC (permalink / raw)
To: Jonathan Corbet
Cc: akpm, linux-kernel, Johannes Stezenbach, Randy Dunlap,
Heikki Orsila, H. Peter Anvin, Satyam Sharma, jimmy bahuleyan,
Stefan Richter
On 11/05/07, Jonathan Corbet <corbet@lwn.net> wrote:
> Here's another version of the volatile document. Once again, I've tried
> to address all of the comments. There haven't really been any recent
> comments addressing the correctness of the document; people have been
> more concerned with how it's expressed. I'm glad to see files in
> Documentation/ held to a high standard of writing, but, unless somebody
> has a factual issue this time around I would like to declare Mission
> Accomplished and move on.
>
> Thanks,
>
> jon
>
> ---
>
> Encourage developers to avoid the volatile type class in kernel code.
>
> Signed-off-by: Jonathan Corbet <corbet@lwn.net>
>
Looks good to me.
Signed-off-by: Jesper Juhl <jesper.juhl@gmail.com>
--
Jesper Juhl <jesper.juhl@gmail.com>
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] "volatile considered harmful", take 3
2007-05-11 17:36 [PATCH] "volatile considered harmful", take 3 Jonathan Corbet
2007-05-11 21:25 ` Jesper Juhl
@ 2007-05-12 3:21 ` Satyam Sharma
2007-05-12 4:29 ` Jeff Garzik
2007-05-12 5:34 ` H. Peter Anvin
1 sibling, 2 replies; 22+ messages in thread
From: Satyam Sharma @ 2007-05-12 3:21 UTC (permalink / raw)
To: Jonathan Corbet
Cc: akpm, linux-kernel, Johannes Stezenbach, Jesper Juhl,
Randy Dunlap, Heikki Orsila, H. Peter Anvin, jimmy bahuleyan,
Stefan Richter
On 5/11/07, Jonathan Corbet <corbet@lwn.net> wrote:
> Here's another version of the volatile document. Once again, I've tried
> to address all of the comments. There haven't really been any recent
> comments addressing the correctness of the document; people have been
> more concerned with how it's expressed. I'm glad to see files in
> Documentation/ held to a high standard of writing, but, unless somebody
> has a factual issue this time around I would like to declare Mission
> Accomplished and move on.
The document looks good, but whether:
> + - Pointers to data structures in coherent memory which might be modified
> + by I/O devices can, sometimes, legitimately be volatile. A ring buffer
> + used by a network adapter, where that adapter changes pointers to
> + indicate which descriptors have been processed, is an example of this
> + type of situation.
is a legitimate use case for volatile is still not clear to me (I
agree with Alan's
comment in a previous thread that this seems to be a case where a memory
barrier would be applicable^Wbetter, actually). I could be wrong here, so
would be nice if Peter explains why volatile is legitimate here.
Otherwise, it's fine with me.
Thanks,
Satyam
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] "volatile considered harmful", take 3
2007-05-12 3:21 ` Satyam Sharma
@ 2007-05-12 4:29 ` Jeff Garzik
2007-05-12 5:34 ` H. Peter Anvin
1 sibling, 0 replies; 22+ messages in thread
From: Jeff Garzik @ 2007-05-12 4:29 UTC (permalink / raw)
To: Satyam Sharma
Cc: Jonathan Corbet, akpm, linux-kernel, Johannes Stezenbach,
Jesper Juhl, Randy Dunlap, Heikki Orsila, H. Peter Anvin,
jimmy bahuleyan, Stefan Richter
Satyam Sharma wrote:
> On 5/11/07, Jonathan Corbet <corbet@lwn.net> wrote:
>> + - Pointers to data structures in coherent memory which might be
>> modified
>> + by I/O devices can, sometimes, legitimately be volatile. A ring
>> buffer
>> + used by a network adapter, where that adapter changes pointers to
>> + indicate which descriptors have been processed, is an example of
>> this
>> + type of situation.
>
> is a legitimate use case for volatile is still not clear to me (I
IMO it is not. We do /not/ want to encourage volatile use in those
cases, and indeed, it's not necessary even if you can rationalize the
use of the English word "volatile" to describe the situation.
Drivers work quite well without volatile in such situations.
Jeff
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] "volatile considered harmful", take 3
2007-05-12 3:21 ` Satyam Sharma
2007-05-12 4:29 ` Jeff Garzik
@ 2007-05-12 5:34 ` H. Peter Anvin
2007-05-12 5:41 ` H. Peter Anvin
2007-05-12 6:15 ` Satyam Sharma
1 sibling, 2 replies; 22+ messages in thread
From: H. Peter Anvin @ 2007-05-12 5:34 UTC (permalink / raw)
To: Satyam Sharma
Cc: Jonathan Corbet, akpm, linux-kernel, Johannes Stezenbach,
Jesper Juhl, Randy Dunlap, Heikki Orsila, jimmy bahuleyan,
Stefan Richter
Satyam Sharma wrote:
>
>> + - Pointers to data structures in coherent memory which might be
>> modified
>> + by I/O devices can, sometimes, legitimately be volatile. A ring
>> buffer
>> + used by a network adapter, where that adapter changes pointers to
>> + indicate which descriptors have been processed, is an example of
>> this
>> + type of situation.
>
> is a legitimate use case for volatile is still not clear to me (I
> agree with Alan's
> comment in a previous thread that this seems to be a case where a memory
> barrier would be applicable^Wbetter, actually). I could be wrong here, so
> would be nice if Peter explains why volatile is legitimate here.
>
> Otherwise, it's fine with me.
>
I don't see why Alan's way is necessarily better; it should work but is
more heavy-handed as it's disabling *all* optimization such as loop
invariants across the barrier.
-hpa
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] "volatile considered harmful", take 3
2007-05-12 5:34 ` H. Peter Anvin
@ 2007-05-12 5:41 ` H. Peter Anvin
2007-05-12 6:15 ` Satyam Sharma
1 sibling, 0 replies; 22+ messages in thread
From: H. Peter Anvin @ 2007-05-12 5:41 UTC (permalink / raw)
To: Satyam Sharma
Cc: Jonathan Corbet, akpm, linux-kernel, Johannes Stezenbach,
Jesper Juhl, Randy Dunlap, Heikki Orsila, jimmy bahuleyan,
Stefan Richter
H. Peter Anvin wrote:
>
> I don't see why Alan's way is necessarily better; it should work but is
> more heavy-handed as it's disabling *all* optimization such as loop
> invariants across the barrier.
>
To expand on this further: the way this probably *should* be handled,
Linux-style, is with internally-volatile versions of le32_to_cpup() and
friends. That obeys the concept that the volatility should be
associated with an operation, not a data structure, and, being related
to an I/O device, should have its endianness explicitly declared.
Right now those macros don't exist, however.
-hpa
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] "volatile considered harmful", take 3
2007-05-12 5:34 ` H. Peter Anvin
2007-05-12 5:41 ` H. Peter Anvin
@ 2007-05-12 6:15 ` Satyam Sharma
2007-05-12 6:21 ` H. Peter Anvin
1 sibling, 1 reply; 22+ messages in thread
From: Satyam Sharma @ 2007-05-12 6:15 UTC (permalink / raw)
To: H. Peter Anvin
Cc: Jonathan Corbet, akpm, linux-kernel, Johannes Stezenbach,
Jesper Juhl, Randy Dunlap, Heikki Orsila, jimmy bahuleyan,
Stefan Richter
On 5/12/07, H. Peter Anvin <hpa@zytor.com> wrote:
> Satyam Sharma wrote:
> >
> >> + - Pointers to data structures in coherent memory which might be
> >> modified
> >> + by I/O devices can, sometimes, legitimately be volatile. A ring
> >> buffer
> >> + used by a network adapter, where that adapter changes pointers to
> >> + indicate which descriptors have been processed, is an example of
> >> this
> >> + type of situation.
> >
> > is a legitimate use case for volatile is still not clear to me (I
> > agree with Alan's
> > comment in a previous thread that this seems to be a case where a memory
> > barrier would be applicable^Wbetter, actually). I could be wrong here, so
> > would be nice if Peter explains why volatile is legitimate here.
> >
> > Otherwise, it's fine with me.
> >
>
> I don't see why Alan's way is necessarily better;
Because volatile is ill-defined? Or actually, *undefined* (well,
implementation-defined is as good as that)? It's *so* _vague_,
one doesn't _feel_ like using it at all!
We already have a complete API containing optimization barriers,
load/store/full memory barriers. With well-defined and
well-understood semantics. Just ... _why_ use volatile?
> it should work but is
It will _always_ work. In fact you can't really say the same for
volatile. We already assume the compiler _actually_ took some
pains to stuff meaning into C's (lack of) definition of volatile and
implement it -- but in what sense, nobody knows (the C standard
doesn't, so what are we).
> more heavy-handed as it's disabling *all* optimization such as loop
> invariants across the barrier.
This is a legitimate criticism, I agree.
Thanks,
Satyam
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] "volatile considered harmful", take 3
2007-05-12 6:15 ` Satyam Sharma
@ 2007-05-12 6:21 ` H. Peter Anvin
2007-05-12 7:02 ` Satyam Sharma
2007-05-12 19:17 ` Dr. David Alan Gilbert
0 siblings, 2 replies; 22+ messages in thread
From: H. Peter Anvin @ 2007-05-12 6:21 UTC (permalink / raw)
To: Satyam Sharma
Cc: Jonathan Corbet, akpm, linux-kernel, Johannes Stezenbach,
Jesper Juhl, Randy Dunlap, Heikki Orsila, jimmy bahuleyan,
Stefan Richter
Satyam Sharma wrote:
>
> Because volatile is ill-defined? Or actually, *undefined* (well,
> implementation-defined is as good as that)? It's *so* _vague_,
> one doesn't _feel_ like using it at all!
>
Sorry, that's just utter crap. Linux isn't written in some mythical C
which only exists in standard document, it is written in a particular
subset of GNU C. "volatile" is well enough defined in that context, it
is just frequently misused.
> We already have a complete API containing optimization barriers,
> load/store/full memory barriers. With well-defined and
> well-understood semantics. Just ... _why_ use volatile?
See below.
> It will _always_ work. In fact you can't really say the same for
> volatile. We already assume the compiler _actually_ took some
> pains to stuff meaning into C's (lack of) definition of volatile and
> implement it -- but in what sense, nobody knows (the C standard
> doesn't, so what are we).
It will always work within the context of GNU C.
>> more heavy-handed as it's disabling *all* optimization such as loop
>> invariants across the barrier.
>
> This is a legitimate criticism, I agree.
There you have it.
-hpa
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] "volatile considered harmful", take 3
2007-05-12 6:21 ` H. Peter Anvin
@ 2007-05-12 7:02 ` Satyam Sharma
2007-05-12 7:13 ` H. Peter Anvin
2007-05-12 7:22 ` Stefan Richter
2007-05-12 19:17 ` Dr. David Alan Gilbert
1 sibling, 2 replies; 22+ messages in thread
From: Satyam Sharma @ 2007-05-12 7:02 UTC (permalink / raw)
To: H. Peter Anvin
Cc: Jonathan Corbet, akpm, linux-kernel, Johannes Stezenbach,
Jesper Juhl, Randy Dunlap, Heikki Orsila, jimmy bahuleyan,
Stefan Richter
On 5/12/07, H. Peter Anvin <hpa@zytor.com> wrote:
> Satyam Sharma wrote:
> >
> > Because volatile is ill-defined? Or actually, *undefined* (well,
> > implementation-defined is as good as that)? It's *so* _vague_,
> > one doesn't _feel_ like using it at all!
> >
>
> Sorry, that's just utter crap. Linux isn't written in some mythical C
> which only exists in standard document, it is written in a particular
> subset of GNU C. "volatile" is well enough defined in that context, it
> is just frequently misused.
Of course, volatile _is_ defined (well, _anything_ that is implemented
_is_ defined, after all) in the context of GNU C, and if you're saying
that the kernel (and all its subsystems) is and should _continue_ to
be (the purpose of this document) written within that context, then
that's your opinion and I would not disagree with you. If you do
prefer to continue using that dialect, then I wouldn't stop you either.
Personally, I'd prefer writing in a slightly more portable / larger
context (using well-defined and understood APIs), thank you, and
hope you'd allow me to do so myself.
Coming back to the document, we do need to document / find
consensus on the "preferred" way to do similar business in the
kernel, and my opinion as far as that is concerned is to shun
volatile wherever possible (which includes the case originally
discussed above).
Satyam
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] "volatile considered harmful", take 3
2007-05-12 7:02 ` Satyam Sharma
@ 2007-05-12 7:13 ` H. Peter Anvin
2007-05-12 7:28 ` Satyam Sharma
2007-05-12 7:53 ` Stefan Richter
2007-05-12 7:22 ` Stefan Richter
1 sibling, 2 replies; 22+ messages in thread
From: H. Peter Anvin @ 2007-05-12 7:13 UTC (permalink / raw)
To: Satyam Sharma
Cc: Jonathan Corbet, akpm, linux-kernel, Johannes Stezenbach,
Jesper Juhl, Randy Dunlap, Heikki Orsila, jimmy bahuleyan,
Stefan Richter
Satyam Sharma wrote:
>>
>> Sorry, that's just utter crap. Linux isn't written in some mythical C
>> which only exists in standard document, it is written in a particular
>> subset of GNU C. "volatile" is well enough defined in that context, it
>> is just frequently misused.
>
> Of course, volatile _is_ defined (well, _anything_ that is implemented
> _is_ defined, after all) in the context of GNU C, and if you're saying
> that the kernel (and all its subsystems) is and should _continue_ to
> be (the purpose of this document) written within that context, then
> that's your opinion and I would not disagree with you. If you do
> prefer to continue using that dialect, then I wouldn't stop you either.
>
This isn't just an opinion, this is the language the Linux kernel is
written in today, and has been for the duration of its 16-year
existence. It contains *many* constructs that are not defined in, for
example, C99, and it would in fact be impossible to write the Linux
kernel using only C99-compliant constructs.
> Personally, I'd prefer writing in a slightly more portable / larger
> context (using well-defined and understood APIs), thank you, and
> hope you'd allow me to do so myself.
There is no such "slightly more portable/larger context/well-defined and
understood" context in existence. If you think so, you're deluding
yourself.
-hpa
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] "volatile considered harmful", take 3
2007-05-12 7:13 ` H. Peter Anvin
@ 2007-05-12 7:28 ` Satyam Sharma
2007-05-12 7:53 ` Stefan Richter
1 sibling, 0 replies; 22+ messages in thread
From: Satyam Sharma @ 2007-05-12 7:28 UTC (permalink / raw)
To: H. Peter Anvin
Cc: Jonathan Corbet, akpm, linux-kernel, Johannes Stezenbach,
Jesper Juhl, Randy Dunlap, Heikki Orsila, jimmy bahuleyan,
Stefan Richter
On 5/12/07, H. Peter Anvin <hpa@zytor.com> wrote:
> Satyam Sharma wrote:
> >>
> >> Sorry, that's just utter crap. Linux isn't written in some mythical C
> >> which only exists in standard document, it is written in a particular
> >> subset of GNU C. "volatile" is well enough defined in that context, it
> >> is just frequently misused.
> >
> > Of course, volatile _is_ defined (well, _anything_ that is implemented
> > _is_ defined, after all) in the context of GNU C, and if you're saying
> > that the kernel (and all its subsystems) is and should _continue_ to
> > be (the purpose of this document) written within that context, then
> > that's your opinion and I would not disagree with you. If you do
> > prefer to continue using that dialect, then I wouldn't stop you either.
> >
>
> This isn't just an opinion, this is the language the Linux kernel is
> written in today, and has been for the duration of its 16-year
I said "and should _continue_ to be (the purpose of this document)
written within that context, then that's your opinion". That "is" before
the "and should ..." was not meant to be combined with "your
opinion".
> existence. It contains *many* constructs that are not defined in, for
> example, C99, and it would in fact be impossible to write the Linux
> kernel using only C99-compliant constructs.
And that is not what I attempt to do (or even suggest) either. Which is
why I do *not* disagree with the jiffies clause -- it exists in that
document merely to _inform_ readers about _current_ behaviour /
dialect of the kernel and not to influence code written in the future.
The problem with the last clause is that it explicitly influences
*future* code (by coming in as a "rare situation where volatile
makes sense in the kernel").
> > Personally, I'd prefer writing in a slightly more portable / larger
> > context (using well-defined and understood APIs), thank you, and
> > hope you'd allow me to do so myself.
>
> There is no such "slightly more portable/larger context/well-defined and
> understood" context in existence. If you think so, you're deluding
> yourself.
If you remember my original mail was in the "_why_ use volatile" tone,
which means to _dissuade_ future spurious additions of volatile to the
kernel. Now this _is_ a matter of opinion: I personally prefer as much
(if not all) future code to be dialect-independent, you might not.
Satyam
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] "volatile considered harmful", take 3
2007-05-12 7:13 ` H. Peter Anvin
2007-05-12 7:28 ` Satyam Sharma
@ 2007-05-12 7:53 ` Stefan Richter
2007-05-12 11:51 ` Heikki Orsila
2007-05-12 18:06 ` H. Peter Anvin
1 sibling, 2 replies; 22+ messages in thread
From: Stefan Richter @ 2007-05-12 7:53 UTC (permalink / raw)
To: H. Peter Anvin
Cc: Satyam Sharma, Jonathan Corbet, akpm, linux-kernel,
Johannes Stezenbach, Jesper Juhl, Randy Dunlap, Heikki Orsila,
jimmy bahuleyan
H. Peter Anvin wrote:
[slightly off topic: GCCisms in Linux kernel]
> It contains *many* constructs that are not defined in, for
> example, C99, and it would in fact be impossible to write the Linux
> kernel using only C99-compliant constructs.
True. On the other hand, it is possible to keep large parts of the
kernel independent of compiler implementation details. And it is not
only possible but also beneficial, e.g. because the compiler's
implementation changes over time.
--
Stefan Richter
-=====-=-=== -=-= -==--
http://arcgraph.de/sr/
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] "volatile considered harmful", take 3
2007-05-12 7:53 ` Stefan Richter
@ 2007-05-12 11:51 ` Heikki Orsila
2007-05-12 18:06 ` H. Peter Anvin
1 sibling, 0 replies; 22+ messages in thread
From: Heikki Orsila @ 2007-05-12 11:51 UTC (permalink / raw)
To: Stefan Richter
Cc: H. Peter Anvin, Satyam Sharma, Jonathan Corbet, akpm,
linux-kernel, Johannes Stezenbach, Jesper Juhl, Randy Dunlap,
jimmy bahuleyan
On Sat, May 12, 2007 at 09:53:03AM +0200, Stefan Richter wrote:
> H. Peter Anvin wrote:
> [slightly off topic: GCCisms in Linux kernel]
> > It contains *many* constructs that are not defined in, for
> > example, C99, and it would in fact be impossible to write the Linux
> > kernel using only C99-compliant constructs.
>
> True. On the other hand, it is possible to keep large parts of the
> kernel independent of compiler implementation details. And it is not
> only possible but also beneficial, e.g. because the compiler's
> implementation changes over time.
I think the most important reason for portable code is that new readers
are more familiar with effects of the code.
--
Heikki Orsila Barbie's law:
heikki.orsila@iki.fi "Math is hard, let's go shopping!"
http://www.iki.fi/shd
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] "volatile considered harmful", take 3
2007-05-12 7:53 ` Stefan Richter
2007-05-12 11:51 ` Heikki Orsila
@ 2007-05-12 18:06 ` H. Peter Anvin
1 sibling, 0 replies; 22+ messages in thread
From: H. Peter Anvin @ 2007-05-12 18:06 UTC (permalink / raw)
To: Stefan Richter
Cc: Satyam Sharma, Jonathan Corbet, akpm, linux-kernel,
Johannes Stezenbach, Jesper Juhl, Randy Dunlap, Heikki Orsila,
jimmy bahuleyan
Stefan Richter wrote:
> H. Peter Anvin wrote:
> [slightly off topic: GCCisms in Linux kernel]
>> It contains *many* constructs that are not defined in, for
>> example, C99, and it would in fact be impossible to write the Linux
>> kernel using only C99-compliant constructs.
>
> True. On the other hand, it is possible to keep large parts of the
> kernel independent of compiler implementation details. And it is not
> only possible but also beneficial, e.g. because the compiler's
> implementation changes over time.
It is, but this is not likely to be one of those things.
Either way, I fully agree with the following (from Jeff):
> jimmy bahuleyan wrote:
>> i believe, the doc here is pretty unambiguous regarding the fact that
>> volatile should be avoided. And as Stefan pointed out, anyone who feels
>> the need to use, must surely _know_ what he is doing & hence is in a
>> position t make that decision
>
> Honestly, the above quoted paragraph states the situation better than any long, complicated document.
-hpa
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] "volatile considered harmful", take 3
2007-05-12 7:02 ` Satyam Sharma
2007-05-12 7:13 ` H. Peter Anvin
@ 2007-05-12 7:22 ` Stefan Richter
2007-05-12 7:33 ` jimmy bahuleyan
1 sibling, 1 reply; 22+ messages in thread
From: Stefan Richter @ 2007-05-12 7:22 UTC (permalink / raw)
To: Satyam Sharma
Cc: H. Peter Anvin, Jonathan Corbet, akpm, linux-kernel,
Johannes Stezenbach, Jesper Juhl, Randy Dunlap, Heikki Orsila,
jimmy bahuleyan
Satyam Sharma wrote:
> Coming back to the document, we do need to document / find
> consensus on the "preferred" way to do similar business in the
> kernel, and my opinion as far as that is concerned is to shun
> volatile wherever possible (which includes the case originally
> discussed above).
I too recommend that volatile-considered-harmful.txt is not watered down
by an ever growing "but if" list. If anybody knows what he does, he
still can program in a deviating way --- provided that he leaves a brief
comment in the code, telling why it is possible and beneficial to use
the volatile qualifier in this special case.
--
Stefan Richter
-=====-=-=== -=-= -==--
http://arcgraph.de/sr/
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] "volatile considered harmful", take 3
2007-05-12 7:22 ` Stefan Richter
@ 2007-05-12 7:33 ` jimmy bahuleyan
2007-05-12 7:45 ` Jeff Garzik
0 siblings, 1 reply; 22+ messages in thread
From: jimmy bahuleyan @ 2007-05-12 7:33 UTC (permalink / raw)
To: Stefan Richter
Cc: Satyam Sharma, H. Peter Anvin, Jonathan Corbet, akpm,
linux-kernel, Johannes Stezenbach, Jesper Juhl, Randy Dunlap,
Heikki Orsila
Stefan Richter wrote:
> Satyam Sharma wrote:
>> Coming back to the document, we do need to document / find
>> consensus on the "preferred" way to do similar business in the
>> kernel, and my opinion as far as that is concerned is to shun
>> volatile wherever possible (which includes the case originally
>> discussed above).
>
> I too recommend that volatile-considered-harmful.txt is not watered down
> by an ever growing "but if" list. If anybody knows what he does, he
> still can program in a deviating way --- provided that he leaves a brief
> comment in the code, telling why it is possible and beneficial to use
> the volatile qualifier in this special case.
yes, this seems the better option. generally, the more complex rules you
have, the more people tend to break it (either through not being able t
comprehend it or cos it's too difficult to follow).
i believe, the doc here is pretty unambiguous regarding the fact that
volatile should be avoided. And as Stefan pointed out, anyone who feels
the need to use, must surely _know_ what he is doing & hence is in a
position t make that decision (followed ofcourse with a doc of why it
was done).
it would be better we didn't grow the list of exceptions - IMHO.
-jb
--
Tact is the art of making a point without making an enemy.
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] "volatile considered harmful", take 3
2007-05-12 7:33 ` jimmy bahuleyan
@ 2007-05-12 7:45 ` Jeff Garzik
0 siblings, 0 replies; 22+ messages in thread
From: Jeff Garzik @ 2007-05-12 7:45 UTC (permalink / raw)
To: jimmy bahuleyan
Cc: Stefan Richter, Satyam Sharma, H. Peter Anvin, Jonathan Corbet,
akpm, linux-kernel, Johannes Stezenbach, Jesper Juhl,
Randy Dunlap, Heikki Orsila
jimmy bahuleyan wrote:
> i believe, the doc here is pretty unambiguous regarding the fact that
> volatile should be avoided. And as Stefan pointed out, anyone who feels
> the need to use, must surely _know_ what he is doing & hence is in a
> position t make that decision
Honestly, the above quoted paragraph states the situation better than
any long, complicated document.
Jeff
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH] "volatile considered harmful", take 3
2007-05-12 6:21 ` H. Peter Anvin
2007-05-12 7:02 ` Satyam Sharma
@ 2007-05-12 19:17 ` Dr. David Alan Gilbert
1 sibling, 0 replies; 22+ messages in thread
From: Dr. David Alan Gilbert @ 2007-05-12 19:17 UTC (permalink / raw)
To: H. Peter Anvin; +Cc: linux-kernel
* H. Peter Anvin (hpa@zytor.com) wrote:
> Satyam Sharma wrote:
> >
> > Because volatile is ill-defined? Or actually, *undefined* (well,
> > implementation-defined is as good as that)? It's *so* _vague_,
> > one doesn't _feel_ like using it at all!
> >
>
> Sorry, that's just utter crap. Linux isn't written in some mythical C
> which only exists in standard document, it is written in a particular
> subset of GNU C. "volatile" is well enough defined in that context, it
> is just frequently misused.
Where? I don't ever recall seeing something that defines Gcc's behaviour
with volatile on different architectures.
I know on some architectures gcc generates different instructions
for volatile accesses (e.g. load acquire/store release on IA64); I'd
be pleasently surprised if gcc's behaviour was consistent accross
architectures.
Dave
--
-----Open up your eyes, open up your mind, open up your code -------
/ Dr. David Alan Gilbert | Running GNU/Linux on Alpha,68K| Happy \
\ gro.gilbert @ treblig.org | MIPS,x86,ARM,SPARC,PPC & HPPA | In Hex /
\ _________________________|_____ http://www.treblig.org |_______/
^ permalink raw reply [flat|nested] 22+ messages in thread
[parent not found: <8jHg3-1T2-5@gated-at.bofh.it>]
end of thread, other threads:[~2007-05-18 3:13 UTC | newest]
Thread overview: 22+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-05-11 17:36 [PATCH] "volatile considered harmful", take 3 Jonathan Corbet
2007-05-11 21:25 ` Jesper Juhl
2007-05-12 3:21 ` Satyam Sharma
2007-05-12 4:29 ` Jeff Garzik
2007-05-12 5:34 ` H. Peter Anvin
2007-05-12 5:41 ` H. Peter Anvin
2007-05-12 6:15 ` Satyam Sharma
2007-05-12 6:21 ` H. Peter Anvin
2007-05-12 7:02 ` Satyam Sharma
2007-05-12 7:13 ` H. Peter Anvin
2007-05-12 7:28 ` Satyam Sharma
2007-05-12 7:53 ` Stefan Richter
2007-05-12 11:51 ` Heikki Orsila
2007-05-12 18:06 ` H. Peter Anvin
2007-05-12 7:22 ` Stefan Richter
2007-05-12 7:33 ` jimmy bahuleyan
2007-05-12 7:45 ` Jeff Garzik
2007-05-12 19:17 ` Dr. David Alan Gilbert
[not found] <8jHg3-1T2-5@gated-at.bofh.it>
[not found] ` <8jQt5-7As-3@gated-at.bofh.it>
[not found] ` <8jSuQ-28J-21@gated-at.bofh.it>
[not found] ` <8jT7y-39x-9@gated-at.bofh.it>
2007-05-13 0:00 ` Bodo Eggert
2007-05-14 3:37 ` Satyam Sharma
2007-05-17 23:51 ` Bill Davidsen
2007-05-18 3:13 ` Satyam Sharma
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox