mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Randy Dunlap <randy.dunlap@oracle.com>
To: "Satyam Sharma" <satyam.sharma@gmail.com>
Cc: "Andrew Morton" <akpm@linux-foundation.org>,
	"Paul Sokolovsky" <pmiscml@gmail.com>,
	linux-kernel@vger.kernel.org, rientjes@google.com,
	jeremy@goop.org
Subject: [PATCH] doc: volatile considered evil
Date: Tue, 8 May 2007 16:34:52 -0700	[thread overview]
Message-ID: <20070508163452.8b71f682.randy.dunlap@oracle.com> (raw)
In-Reply-To: <a781481a0705081307r546f8889qecca3db16ea03fb8@mail.gmail.com>

On Tue, 8 May 2007 13:07:51 -0700 Satyam Sharma wrote:

> Yes, definitely. Say Documentation/volatile-usage.txt -- this raw
> version could be touched a little bit, to have sections that clearly
> explain (1) how volatile makes the compiler generate trashy code, (2)
> why volatile doesn't even do what people _think_ it does, considering
> code is executed out-of-order by _hardware_ these days and not due to
> compilers like was the case 20 years back, (3) and so volatile ends up
> _hiding_ bugs from people and thus should be consigned to the trash
> can of history, (4) _except_ for _really special_ usage cases like
> reading IO mapped as memory.

Hi Satyam,

If you would like to organize it like that, I'd be happy to
turn it over to you.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

From: Randy Dunlap <randy.dunlap@oracle.com>

Add information on the problems with the C-language "volatile" keyword
and why it should not be used (most of the time).

Signed-off-by: Randy Dunlap <randy.dunlap@oracle.com>
---
 Documentation/volatile-usage.txt |  129 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 129 insertions(+)

--- /dev/null
+++ linux-2.6.21-git10/Documentation/volatile-usage.txt
@@ -0,0 +1,129 @@
+***** "volatile" considered useless and evil:  Just Say NO! *****
+
+Do not use the C-language "volatile" keyword
+(extracted from lkml emails from Linus)
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+[Comment about a patch:]
+
+> Also made all the relevant mce_log fields volatile for further safety.
+
+I refuse to apply this part.
+
+If the memory barriers are right, then the "volatile" doesn't matter.
+
+And if the memory barriers aren't right, then "volatile" doesn't help.
+
+Using "volatile" in data structures is basically _always_ a bug.
+
+The only acceptable uses for "volatile" are:
+
+ - in _code_, i.e., for things like the definition of "readb()" etc, where we
+   use it to force a particular access.
+ - with inline asms
+ - on "jiffies", for stupid legacy reasons
+
+Basically, a volatile on a data structure can NEVER be right. If it makes
+a difference, it's a sign of improper locking.
+
+And the reason I refuse to apply that part of the patch is that anybody
+who even _thinks_ that they make a difference is horribly and utterly
+confused, and doesn't understand locking.
+
+So please _never_ use them like this.
+
+
+> mce_log is fully lockless - it deals with machine checks which act like NMIs.
+> That is it's problem.
+>
+> In theory the memory barriers should be sufficient, the volatiles are
+> just an additional safety net to make it clear to humans/compiler these memory
+> areas can change any time.
+
+If the memory barriers aren't sufficient, the volatiles are useless. If
+the memory barriers _are_ sufficient, the volatiles are useless.
+
+See? They're useless.
+
+The only thing they do is
+
+ - potentially make the compiler generate worse code for no reason (the
+   "no reason" being that if there aren't any barriers in between, the
+   compiler _should_ merge accesses)
+
+ - make some people _believe_ that that compiler does something "right".
+
+The first point doesn't much matter. The second point matters a LOT.
+
+Anybody who thinks "volatile" matters is WRONG. As such, a "volatile" is
+anti-documentation - it makes people think that something is true that is
+NOT true.
+
+In other words, volatile on data structures is _evil_, because it instills
+the wrong kind of beliefs in people. "volatility" is not a data structure
+issue. It's a matter of the _code_ working on the data structure.
+
+
+"volatile" really _is_ misdesigned. The semantics of it are so unclear as
+to be totally useless. The only thing "volatile" can ever do is generate
+worse code, WITH NO UPSIDES.
+
+Historically (and from the standpoint of the C standard), the definition
+of "volatile" is that any access is "visible" in the machine, and it
+really kind of makes sense for hardware accesses, except these days
+hardware accesses have other rules that are _not_ covered by "volatile",
+so you can't actually use them for that.
+
+And for accesses that have some software rules (i.e., not IO devices etc),
+the rules for "volatile" are too vague to be useful.
+
+So if you actually have rules about how to access a particular piece of
+memory, just make those rules _explicit_. Use the real rules. Not
+volatile, because volatile will always do the wrong thing.
+
+Also, more importantly, "volatile" is on the wrong _part_ of the whole
+system. In C, it's "data" that is volatile, but that is insane. Data
+isn't volatile - _accesses_ are volatile. So it may make sense to say
+"make this particular _access_ be careful", but not "make all accesses to
+this data use some random strategy".
+
+So the only thing "volatile" is potentially useful for is:
+
+ - actual accessor functions can use it in a _cast_ to make one particular
+   access follow the rules of "don't cache this one dereference". That is
+   useful as part of a _bigger_ set of rules about that access (i.e., it
+   might be the internal implementation of a "readb()", for example).
+
+ - for "random number generation" data locations, where you literally
+   don't _have_ any rules except "it's a random number". The only really
+   valid example of this is the "jiffy" timer tick.
+
+Any other use of "volatile" is almost certainly a bug, or just useless.
+
+Side note: it's also totally possible that a volatiles _hides_ a bug, i.e.,
+removing the volatile ends up having bad effects, but that's because the
+software itself isn't actually following the rules (or, more commonly, the
+rules are broken, and somebody added "volatile" to hide the problem).
+
+It's a bug if the volatile means that you don't follow the proper protocol
+for accessing the data, and it's useless (and generally generates worse
+code) if you already do.
+
+So just say NO! to volatile except under the above circumstances.
+
+
+
+[from another email thread:]
+
+I suspect we should just face up to the fact that:
+
+ (a) "volatile" on kernel data is basically always a bug, and you should
+     use locking. "volatile" doesn't help anything at all with memory
+     ordering and friends, so it's insane to think it "solves" anything on
+     its own.
+ (b) on "iomem" pointers it does make sense, but those need special
+     accessor functions _anyway_, so things like test_bit() wouldn't work
+     on them.
+ (c) if you spin on a value [that's] changing, you should use "cpu_relax()" or
+     "barrier()" anyway, which will force gcc to re-load any values from
+     memory over the loop.

  reply	other threads:[~2007-05-08 23:33 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-05-01  5:08 [RFC, PATCH 2/4] SoC base drivers: ASIC3 SoC hardware definitions Paul Sokolovsky
2007-05-01  6:56 ` Andrew Morton
2007-05-01 10:27   ` Alan Cox
2007-05-01 12:04     ` Paul Sokolovsky
2007-05-01 12:21       ` Jamey Hicks
2007-05-08 19:14   ` [RFC/PATCH] doc: volatile considered evil Randy Dunlap
2007-05-08 19:18     ` David Rientjes
2007-05-08 20:00       ` Krzysztof Halasa
2007-05-08 20:20         ` David Rientjes
2007-05-08 23:13           ` Randy Dunlap
2007-05-08 23:54             ` David Rientjes
2007-05-09  0:00               ` Randy Dunlap
2007-05-08 21:05         ` Jeremy Fitzhardinge
2007-05-08 21:10           ` Krzysztof Halasa
2007-05-08 21:16           ` Jeff Garzik
2007-05-08 21:26             ` Randy Dunlap
2007-05-08 21:25               ` Jeff Garzik
2007-05-08 21:20       ` Jeremy Fitzhardinge
2007-05-08 21:27         ` David Rientjes
2007-05-08 21:37           ` Jeremy Fitzhardinge
2007-05-08 21:59             ` David Rientjes
2007-05-08 22:04               ` Jeremy Fitzhardinge
2007-05-08 22:19                 ` David Rientjes
2007-05-08 22:29                   ` Jeremy Fitzhardinge
2007-05-08 22:35                     ` David Rientjes
2007-05-08 23:09           ` Randy Dunlap
2007-05-08 21:29         ` Randy Dunlap
2007-05-08 20:07     ` Satyam Sharma
2007-05-08 23:34       ` Randy Dunlap [this message]
2007-05-09  0:06         ` [PATCH] " David Rientjes
2007-05-09  2:08           ` Randy Dunlap
2007-05-09  2:38             ` David Rientjes
2007-05-09  3:15               ` Randy Dunlap
2007-05-09  9:21               ` Alan Cox
2007-05-09  9:26                 ` Nick Piggin
2007-05-09 13:31                   ` Alan Cox
2007-05-09 10:25                 ` David Rientjes
2007-05-09 13:36                   ` Alan Cox
2007-05-09 18:41                     ` David Rientjes
2007-05-09 20:23                       ` Alan Cox
2007-05-09 20:25                         ` David Rientjes
2007-05-09 22:47                         ` Rob Landley
2007-05-09  8:50         ` Stefan Richter
2007-05-09 15:52           ` Randy Dunlap
2007-05-09 19:04             ` Satyam Sharma
2007-05-09  1:47     ` [RFC/PATCH] " Jonathan Corbet
2007-05-09  9:43       ` Johannes Stezenbach
2007-05-09 19:34         ` Satyam Sharma

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=20070508163452.8b71f682.randy.dunlap@oracle.com \
    --to=randy.dunlap@oracle.com \
    --cc=akpm@linux-foundation.org \
    --cc=jeremy@goop.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pmiscml@gmail.com \
    --cc=rientjes@google.com \
    --cc=satyam.sharma@gmail.com \
    /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

Powered by JetHome