All of lore.kernel.org
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@osdl.org>
To: Chris Wedgwood <cw@f00f.org>
Cc: James Bottomley <James.Bottomley@SteelEye.com>,
	Andrew Morton <akpm@osdl.org>, Paul Jackson <pj@sgi.com>,
	PARISC list <parisc-linux@lists.parisc-linux.org>,
	Linux Kernel <linux-kernel@vger.kernel.org>
Subject: Re: more (insane) jiffies ranting
Date: Sun, 27 Jun 2004 10:39:08 -0700 (PDT)	[thread overview]
Message-ID: <Pine.LNX.4.58.0406271022430.16079__43188.0799624652$1416623489$gmane$org@ppc970.osdl.org> (raw)
In-Reply-To: <20040627015501.GA14647@taniwha.stupidest.org>



On Sat, 26 Jun 2004, Chris Wedgwood wrote:
> 
> On Sat, Jun 26, 2004 at 03:48:34PM -0700, Linus Torvalds wrote:
> 
> > But for most data structures, the way to control access is either
> > with proper locking (at which point they aren't volatile any more)
> > or through proper accessor functions (ie "jiffies_64" should
> > generally only be accessed with something that understands about
> > low/high word and update ordering and re-testing).
> 
> I don't entirely buy this.  Right now x86 code just assumes 32-bit
> loads are atomic and does them blindly in lots of places (ie. every
> user of jiffies just about).
> 
> Without the volatile it seems entirely reasonable gcc will produce
> correct, but wrong code here so I would argue 'volatile' is a property
> of the data in this case.

It's a property of the data _iff_:
 - it is _always_ volatile
 - it is only ever used atomically: this also means that it must be 
   totally independent of _all_ other data structures and have no linkages 
   to anything else.

Snd basically, the above is pretty much never true except possibly for 
real I/O accesses and sometimes things like simple "flags" (ie it's fine 
to use "volatile sigatomic_t flag;" in user programs to have signal 
handlers say "something happened" in a single-threaded environment).

NOTE! The "single-threaded environment" part really is important, and is 
one of the historical reasons for volatile having been more useful than it 
is today. If you are single-threaded and don't have issues like CPU memory 
ordering etc, then you can let the compiler do more of the work, and there 
are a lot of lockless algorithms that you can use that only depend on 
fairly simple semantics for "volatile".

But the fact is, for the kernel none of the above is ever really true. 
A 32-bit-atomic "jiffies" comes the closest, but even there the "always" 
property wasn't true - it wasn't true in the update phase, and we 
literally used to have something like this:

	*((unsigned long *)&jiffies)++;

to update jiffies and still get good code generation (now that we have a
64-bit jiffies and need to do more complex stuff anyway, we don't have
that any more, but you should be able to find it in 2.3.x kernels if I
remember correctly).

And _anything_ that has any data dependencies, "volatile" is totally
useless. Even the (acceptable in single-threaded user-space) "flag" thing 
is not valid usage in the kernel, since for a flag in a multi-threaded 
environment you still need an explicit CPU memory barrier in the code, 
making it impossible for the compiler to do the right thing anyway.

> > I repeat: it is the _code_ that knows about volatile rules, not the
> > data structure.
> 
> Except as I mentioned we have exceptions to this right now.

No we don't. The _only_ accepted exception is the special case of "the low
bits of jiffies", and that's accepted partly because of historical
reasons, and partly because it's fundamentally a data structure we don't
really care that much about. There should be no other ones.

And that special case _literally_ is only for people who don't care that 
much. Anybody who cares about "real time" needs to get xtime_lock and do 
the proper magic to get a real date.

So I don't see your argument. I'm obviously saying that "yes, we have 
_one_ case where we make a data structure volatile", but at the same time, 
that case is very much a "we don't really care too much about precision 
there, and even so people think we should have real accessor functions".

So I stand by the rule: we should make _code_ have the access rules, and
the data itself should never be volatile. And yes, jiffies breaks that
rule, but hey, that's not something I'm proud of.

		Linus

  parent reply	other threads:[~2004-06-27 17:39 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-06-26 16:08 [PATCH] Fix the cpumask rewrite James Bottomley
2004-06-26 16:32 ` Linus Torvalds
2004-06-26 16:44   ` Linus Torvalds
2004-06-26 16:46   ` James Bottomley
2004-06-26 16:54     ` Linus Torvalds
2004-06-26 17:18       ` James Bottomley
2004-06-26 18:01         ` Linus Torvalds
2004-06-26 18:28           ` Vojtech Pavlik
2004-06-26 18:54             ` Linus Torvalds
2004-06-26 19:02               ` James Bottomley
2004-06-26 19:13                 ` Linus Torvalds
2004-06-26 19:13               ` Vojtech Pavlik
2004-06-26 18:59           ` James Bottomley
2004-06-26 19:11             ` Linus Torvalds
2004-06-26 19:33               ` James Bottomley
2004-06-28 23:16           ` Jonathan Lundell
2004-07-01 13:11       ` Pavel Machek
2004-07-01 14:07         ` [parisc-linux] " Alan Cox
2004-07-01 16:15           ` Linus Torvalds
2004-07-01 16:52             ` Jeff Garzik
2004-07-01 17:42               ` Linus Torvalds
2004-06-26 22:18   ` Chris Wedgwood
2004-06-26 22:48     ` Linus Torvalds
2004-06-26 22:54       ` [parisc-linux] " Alan Cox
2004-06-27  0:05         ` Chris Wedgwood
2004-06-27 12:00           ` Matthias Urlichs
2004-06-27 22:41             ` Chris Wedgwood
2004-06-28  1:24               ` Matthias Urlichs
2004-06-28  5:42                 ` Chris Wedgwood
2004-06-28  6:55                   ` Matthias Urlichs
2004-06-28  7:02                     ` Chris Wedgwood
2004-06-28  7:19                       ` Matthias Urlichs
2004-06-27 14:37           ` Alan Cox
2004-07-01 13:33             ` Pavel Machek
2004-07-01 17:43               ` Chris Wedgwood
2004-06-26 23:37       ` jiffies_64 Chris Wedgwood
2004-06-27  1:55       ` more (insane) jiffies ranting Chris Wedgwood
2004-06-27 17:39         ` Linus Torvalds
2004-06-27 17:39         ` [parisc-linux] " Linus Torvalds
2004-06-27 17:39         ` Linus Torvalds [this message]
2004-06-26 16:32 ` [PATCH] Fix the cpumask rewrite 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.58.0406271022430.16079__43188.0799624652$1416623489$gmane$org@ppc970.osdl.org' \
    --to=torvalds@osdl.org \
    --cc=James.Bottomley@SteelEye.com \
    --cc=akpm@osdl.org \
    --cc=cw@f00f.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=parisc-linux@lists.parisc-linux.org \
    --cc=pj@sgi.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 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.