"volatile considered harmful", take 3
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 4 lines · Satyam Sharma
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
^^^^^^^^^^^^^^
Quoted 1 line
doesn't, so what are we).
^^^^^^^
It does:
ISO/IEC 9899:1999 (E)
5.1.2.3 Program execution
5 The least requirements on a conforming implementation are:
At sequence points, volatile objects are stable in the sense that previous accesses are complete and subsequent accesses have not yet occurred.
9 Alternatively, an implementation might perform various optimizations
within each translation unit, such that the actual semantics would agree with the abstract semantics only when making function calls across translation unit boundaries. In such an implementation, at the time of each function entry and function return where the calling function and the called function are in different translation units, the values of all externally linked objects and of all objects accessible via pointers therein would agree with the abstract semantics. Furthermore, at the time of each such function entry the values of the parameters of the called function and of all objects accessible via pointers therein would agree with the abstract semantics. In this type of implementation, objects referred to by interrupt service routines activated by the signal function would require explicit specification of volatile storage, as well as other implementation-defined restrictions.
6.7.3 Type qualifiers
6 An object that has volatile-qualified type may be modified in ways unknown
to the implementation or have other unknown side effects. Therefore any expression referring to such an object shall be evaluated strictly according to the rules of the abstract machine, as described in 5.1.2.3. Furthermore, at every sequence point the value last stored in the object shall agree with that prescribed by the abstract machine, except as modified by the unknown factors mentioned previously.114) What constitutes an access to an object that has volatile-qualified type is implementation-defined.
<snip>
It does not guarantee ordering or consistency beyond the scope of the assembler layer, especially the CPU or other hardware may be free to cache or reorder load/stores to these volatile objects (see e.g. MTRR), which is why you'll usurally want to use something completely different. If you know an object to not be modified from outside the scope of the C state machine and it does not have relevant side effects, you can however use volatile (likely on an atomic value).
I think you should say something like:
1) Don't use volatile
2) Read memory-barriers.txt carefully!
3) If you still use volatile, explain
a) Why it's nescensary
b) Why it's enough (see (2))
to specify volatile on that object. Be sure to consider hardware cache and reordering, as well as prohibited optimizations on volatile access and on functions possibly doing volatile access.
Signature · 4 lines
Top 100 things you don't want the sysadmin to say: 14. Any more trouble from you and your account gets moved to the 750 Friß, Spammer: [email protected] [email protected]
Hi Bodo,
Quoted 10 lines · Bodo Eggert
Quoted 4 lines · Satyam Sharma
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^^^^^^^^^^^^^^Quoted 1 line
doesn't, so what are we).
^^^^^^^
It does:
I don't think so ... the ambiguity in 6.7.3:6 is why.
Quoted 7 lines
ISO/IEC 9899:1999 (E)
5.1.2.3 Program execution
5 The least requirements on a conforming implementation are:
At sequence points, volatile objects are stable in the sense that previous accesses are complete and subsequent accesses have not yet occurred.
Worthy goal, indeed, but you've yourself pointed out later in your mail why this may not be possible for a compiler/assembler to guarantee safely (in a sense that the programmer usually wants, anyway), even with the C standard-blessed "volatile" type-qualifier, what with the *hardware* optimizing memory accesses _independently_ at a granularity finer than the "sequence points" understood by a compiler.
Quoted 18 lines
9 Alternatively, an implementation might perform various optimizations
within each translation unit, such that the actual semantics would agree with the abstract semantics only when making function calls across translation unit boundaries. In such an implementation, at the time of each function entry and function return where the calling function and the called function are in different translation units, the values of all externally linked objects and of all objects accessible via pointers therein would agree with the abstract semantics. Furthermore, at the time of each such function entry the values of the parameters of the called function and of all objects accessible via pointers therein would agree with the abstract semantics. In this type of implementation, objects referred to by interrupt service routines activated by the signal function would require explicit specification of volatile storage, as well as other implementation-defined restrictions.6.7.3 Type qualifiers
Quoted 8 lines
6 An object that has volatile-qualified type may be modified in ways unknown
to the implementation or have other unknown side effects. Therefore any expression referring to such an object shall be evaluated strictly according to the rules of the abstract machine, as described in 5.1.2.3. Furthermore, at every sequence point the value last stored in the object shall agree with that prescribed by the abstract machine, except as modified by the unknown factors mentioned previously.114) What constitutes an access to an object that has volatile-qualified type is implementation-defined.
The way I read it, "What constitutes an access to an object that has volatile-qualified type is implementation-defined" is the standard's way of saying: "All bets are off." Actually I wonder how the various _compiler implementations_ read this clause (and whether *all* of them read it consistently and equally, which is what a portable codebase probably wants), because _their_ interpretation of this clause is more important than mine, after all. But I don't really see how generic C code that uses "volatile" can also claim dialect-independence.
So, IMO, the trouble with the volatile type-qualifier is that it is (what should have been defined as in the first place, and what is in effect) a _compiler extension_ *masquerading* as a _language keyword_.
*Unfortunately* (the trouble with C itself, is that a *committee* has made it into ... something ... that it should not have made it into) -- anyway, unfortunately C took it upon itself to solve a problem that it did not have (and does not even bring about) in the first place: and the half-hearted (or vague, call it what you will) attempt _then_ ends up being a problem -- by making people _feel_ as if they are doing things right, when that is probably not the case.
[ And we've not even touched the issue of whether the _same_ compiler's implementation of volatile across archs/platforms is consistent. ]
Quoted 4 lines
It does not guarantee ordering or consistency beyond the scope of the assembler layer, especially the CPU or other hardware may be free to cache or reorder load/stores to these volatile objects (see e.g. MTRR), which is why you'll usurally want to use something completely different. If you know
Exactly. So this combined with the issue above, is the last nail in volatile's coffin as far as I'm concerned.
Quoted 3 lines
an object to not be modified from outside the scope of the C state machine and it does not have relevant side effects, you can however use volatile (likely on an atomic value).
Yes, but I'd prefer to stick to the straight-and-narrow (well-defined-and- well-understood), and avoid volatile altogether. The kernel codebase does contain APIs that allow one to avoid it and are also what one probably wants for most situations.
Quoted 10 lines
I think you should say something like:
1) Don't use volatile
2) Read memory-barriers.txt carefully!
3) If you still use volatile, explain
a) Why it's nescensary
b) Why it's enough (see (2))
to specify volatile on that object. Be sure to consider hardware cache and reordering, as well as prohibited optimizations on volatile access and on functions possibly doing volatile access.
Yeah, I agree with the wording above, also with Jimmy's summary that Jeff and Peter agreed to already.
Satyam
Quoted 11 lines · Satyam Sharma
*Unfortunately* (the trouble with C itself, is that a *committee* has made it into ... something ... that it should not have made it into) -- anyway, unfortunately C took it upon itself to solve a problem that it did not have (and does not even bring about) in the first place: and the half-hearted (or vague, call it what you will) attempt _then_ ends up being a problem -- by making people _feel_ as if they are doing things right, when that is probably not the case.
[ And we've not even touched the issue of whether the _same_ compiler's implementation of volatile across archs/platforms is consistent. ]
Pardon, I was GE's representative to the original X3J11 committee, and 'volatile' was added to "codify existing practice" which is one of the goals of a standard. The extension existed, in at least two forms, to allow handling of memory mapped hardware. So the committee did not take it upon itself, it was a part of the defined duty of the committee.
The intents was simple, clear, and limited, to tell the compiler that every read of a variable in source code should result in a read, at that point in the logic, and similar for writes. In other words, the code should not be moved and should generate a real memory access every time. People have tried to do many things with that limited concept since, some with "clarification" and some with assuming the compiler knows when to ignore volatile.
As someone noted about a committee, a committee is a poor way to get innovation, and a good way to have a bunch of know legible people shoot down bad ideas.
It was a fun experience, where I first learned the modern equivalent of Occam's Razor, Plauger's "Law of least astonishment," which compiler writers regularly violate :-(
Signature · 3 lines
Bill Davidsen <[email protected]> "We have more to fear from the bungling of the incompetent than from the machinations of the wicked." - from Slashdot
Hi Bill,
Quoted 10 lines · Bill Davidsen
Quoted 8 lines · Satyam Sharma
*Unfortunately* (the trouble with C itself, is that a *committee* has made it into ... something ... that it should not have made it into) -- anyway, unfortunately C took it upon itself to solve a problem that it did not have (and does not even bring about) in the first place: and the half-hearted (or vague, call it what you will) attempt _then_ ends up being a problem -- by making people _feel_ as if they are doing things right, when that is probably not the case.
Quoted 6 lines
Quoted 2 lines
[ And we've not even touched the issue of whether the _same_ compiler's implementation of volatile across archs/platforms is consistent. ]
It was a fun experience, where I first learned the modern equivalent of Occam's Razor, Plauger's "Law of least astonishment," which compiler writers regularly violate :-(
Well, here it was a case of the standard committee violating the principle of solving a problem _where_ it exists in the first place. As far as volatile is concerned, the _language_ itself had absolutely no problems that needed fixing ...
Quoted 6 lines
Pardon, I was GE's representative to the original X3J11 committee, and 'volatile' was added to "codify existing practice" which is one of the goals of a standard. The extension existed, in at least two forms, to allow handling of memory mapped hardware. So the committee did not take it upon itself, it was a part of the defined duty of the committee.
Quoted 7 lines
The intents was simple, clear, and limited, to tell the compiler that every read of a variable in source code should result in a read, at that point in the logic, and similar for writes. In other words, the code should not be moved and should generate a real memory access every time. People have tried to do many things with that limited concept since, some with "clarification" and some with assuming the compiler knows when to ignore volatile.
In fact I wish the standard just left this functionality entirely up to the implementation.
After all, C (the _language_) does not mandate memory access reordering / optimization by implementations so dumb compilers (5.1.2.3:1) that do not do any are still conformant and hence don't require the volatile extension (5.1.2.3:8). Implementations that do perform optimization, otoh, _must_ then solve the possible problems _themselves_ by providing an extension that allows the programmer to turn it off (wherever necessary) to prevent those side-effects that the implementation itself brought about by performing optimization in the first place. That _compiler extension_ could then of course be called anything (volatile, foo, bar, barrier, ...) and implemented in any way -- whatever is appropriate for that compiler [1].
[ Analogically, C (or any language) does not mandate the hardware it runs on to reorder / optimize memory accesses. Those that do, must clearly then also provide instructions like lfence/sfence/mfence as part of their instruction set to solve potential issues that _their_ behaviour brings about in the first place. How horribly wrong it would be for a _language_ to try and go about solving this problem ... ]
So the way I see it, it's just a case of solving a problem _where_ it exists in the first place.
Satyam
[1] Which begs the question, so if we wished (note past tense here) to keep generic kernel code compiler-independent, what do we do in situations where we want to disable _compiler optimizations_ temporarily for some piece of code? => By defining macros / some API as part of our codebase in implementation-specific headers that does whatever needs to be done for _that_ compiler. => Which is precisely what we have already and why I like/prefer that.