linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
To: Alan Stern <stern@rowland.harvard.edu>
Cc: Pavel Machek <pavel@ucw.cz>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Andrew Morton <akpm@osdl.org>,
	Zdenek Kabelac <zdenek.kabelac@gmail.com>,
	davem@davemloft.net, "Rafael J. Wysocki" <rjw@sisk.pl>,
	Pierre Ossman <drzeus-mmc@drzeus.cx>,
	Kernel development list <linux-kernel@vger.kernel.org>,
	pm list <linux-pm@lists.linux-foundation.org>
Subject: Re: [patch] Re: using long instead of atomic_t when only set/read is required
Date: Mon, 3 Mar 2008 09:22:30 -0800	[thread overview]
Message-ID: <20080303172230.GB12453@linux.vnet.ibm.com> (raw)
In-Reply-To: <Pine.LNX.4.44L0.0803031023550.3611-100000@iolanthe.rowland.org>

On Mon, Mar 03, 2008 at 10:42:35AM -0500, Alan Stern wrote:
> On Mon, 3 Mar 2008, Pavel Machek wrote:
> 
> > Hi!
> > 
> > > > Alan thinks that `subj` is correct...
> > > 
> > > More precisely, reads and writes of pointers are always atomic.  That 
> > > is, if a write and a read occur concurrently, it is guaranteed that the 
> > > read will obtain either the old or the new value of the pointer, never 
> > > a mish-mash of the two.  If this were not so then RCU wouldn't work.
> 
> Right, Paul?

Yep, as long as they aligned naturally, e.g., 32-bit pointers to a
four-byte boundary, 64-bit pointers to an eight-byte boundary.

Note that this is a backdoor agreement between the Linux kernel and gcc.
The C/C++ standard does -not- require atomic reads or writes for anything
other than a volatile sig_atomic_t.  In fact, there are a lot of things
that C/C++ doesn't guarantee, which is why Linux makes use of so many
of gcc's non-standard extensions.  ;-)

> > Ok, so linux actually atomicity of long?

Yep, same alignment rules as pointers.  Ah, and the alignment is
covered below.  Very good!

> > If so, this should probably be applied...
> > 
> > Signed-off-by: Pavel Machek <pavel@suse.cz>
> > 
> > diff --git a/Documentation/atomic_ops.txt b/Documentation/atomic_ops.txt
> > index 4ef2450..0a7d180 100644
> > --- a/Documentation/atomic_ops.txt
> > +++ b/Documentation/atomic_ops.txt
> > @@ -21,6 +21,9 @@ local_t is very similar to atomic_t. If 
> >  updated by one CPU, local_t is probably more appropriate. Please see
> >  Documentation/local_ops.txt for the semantics of local_t.
> >  
> > +long (and int and void *) can be used instead of atomic_t, if all you
> > +need is atomic setting and atomic reading.
> > +
> >  The first operations to implement for atomic_t's are the initializers and
> >  plain reads.
> 
> Yes indeed.  This fact doesn't seem to be documented anywhere, but it 
> is clearly a requirement of the kernel.  I would make the text a little 
> more explicit, see below.
> 
> Alan Stern
> 
> -------------------------------------------------------
> 
> 
> Atomicity of reads of write for pointers and integral types (other than 
> long long) should be documented.
> 
> Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
> 
> ---
> 
> Index: usb-2.6/Documentation/atomic_ops.txt
> ===================================================================
> --- usb-2.6.orig/Documentation/atomic_ops.txt
> +++ usb-2.6/Documentation/atomic_ops.txt
> @@ -21,6 +21,21 @@ local_t is very similar to atomic_t. If 
>  updated by one CPU, local_t is probably more appropriate. Please see
>  Documentation/local_ops.txt for the semantics of local_t.
> 
> +For all properly-aligned pointer and integral types other than long
> +long, the kernel requires simple reads and writes to be atomic with
> +respect to each other.  That is, if one CPU reads a pointer value at
> +the same time as another CPU overwrites the pointer, it is guaranteed
> +that the reader will obtain either the old or the new value of the
> +pointer, never some mish-mash combination of the two.  Likewise, if
> +one CPU writes a long value at the same time as another CPU does, it
> +is guaranteed that one or the other of the values will end up stored
> +in memory, not some mish-mash combination of bits.
> +
> +Thus, if all you need is atomicity of reading and writing then you can
> +use plain old ints, longs, or pointers; you don't need to use
> +atomic_t.  (But note: This guarantee emphatically does not apply to
> +long long values or unaligned values!)
> +
>  The first operations to implement for atomic_t's are the initializers and
>  plain reads.
> 
> 

  parent reply	other threads:[~2008-03-03 17:35 UTC|newest]

Thread overview: 93+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20080219232338.E9A24108068@picon.linux-foundation.org>
2008-02-20 16:42 ` [Bug 10030] Suspend doesn't work when SD card is inserted Alan Stern
2008-02-20 17:30   ` Pierre Ossman
2008-02-20 19:26     ` Alan Stern
2008-02-20 20:51       ` Pierre Ossman
2008-02-20 20:58         ` Rafael J. Wysocki
2008-02-20 21:06         ` Alan Stern
2008-02-20 22:15           ` Rafael J. Wysocki
2008-02-20 22:24             ` Alan Stern
2008-02-20 22:41               ` Rafael J. Wysocki
2008-02-21  0:02               ` Rafael J. Wysocki
2008-02-21 16:27                 ` Alan Stern
2008-02-21 16:38                   ` Rafael J. Wysocki
2008-02-21 17:48                     ` Alan Stern
2008-02-21 22:47                       ` Rafael J. Wysocki
2008-02-21 23:05                         ` Alan Stern
2008-02-23  1:30                           ` Rafael J. Wysocki
2008-02-23  4:39                             ` Alan Stern
2008-02-23 20:16                               ` Rafael J. Wysocki
2008-02-23 23:29                                 ` Alan Stern
2008-02-24  0:19                                   ` Rafael J. Wysocki
2008-02-24  3:25                                     ` Alan Stern
2008-02-24  4:26                                       ` [linux-pm] " Alan Stern
2008-02-24 14:00                                         ` Rafael J. Wysocki
2008-02-24 15:33                                           ` Bugs in MMC [was: [Bug 10030] Suspend doesn't work when SD card is inserted] Alan Stern
2008-02-25 17:41                                             ` Pierre Ossman
2008-02-25 17:58                                               ` Alan Stern
2008-02-25 18:31                                                 ` Pierre Ossman
2008-02-25 20:00                                                   ` Alan Stern
2008-03-01 14:11                                                     ` Pierre Ossman
2008-03-01 14:36                                                       ` Alan Stern
2008-03-01 14:47                                                         ` Pierre Ossman
2008-02-25 22:51                                               ` Felipe Balbi
2008-03-03 21:59                                               ` David Brownell
2008-03-04  6:03                                                 ` Pierre Ossman
2008-03-04  9:44                                                   ` David Brownell
2008-03-04  9:58                                                     ` Pierre Ossman
2008-03-06 21:23                                                       ` David Brownell
2008-03-04 17:53                                                     ` Alan Stern
2008-03-04 18:53                                                       ` David Brownell
2008-03-04 19:51                                                         ` Alan Stern
2008-03-04 20:30                                                           ` David Brownell
2008-03-04 21:00                                                             ` Alan Stern
2008-03-06 15:55                                                               ` Pavel Machek
2008-03-06 20:33                                                                 ` Alan Stern
2008-03-06 20:53                                                                   ` Zdenek Kabelac
2008-03-06 21:31                                                                     ` Rafael J. Wysocki
2008-03-04 17:50                                                 ` Alan Stern
2008-02-24 13:33                                       ` [Bug 10030] Suspend doesn't work when SD card is inserted Rafael J. Wysocki
2008-02-24 20:25                                         ` Rafael J. Wysocki
2008-02-24 20:45                                           ` Alan Stern
2008-02-24 20:56                                             ` Rafael J. Wysocki
2008-02-24 21:11                                               ` Alan Stern
2008-02-24 22:18                                                 ` Rafael J. Wysocki
2008-02-24 13:51                                       ` Rafael J. Wysocki
2008-02-24 19:27                                         ` Alan Stern
2008-02-24 19:42                                           ` Zdenek Kabelac
2008-02-24 20:09                                             ` Rafael J. Wysocki
2008-02-24 18:21                                       ` Pavel Machek
2008-02-24 19:03                                         ` Alan Stern
2008-02-24 20:11                                           ` Pavel Machek
2008-02-24 20:33                                             ` Alan Stern
2008-02-24 21:42                                               ` Pavel Machek
2008-02-24 22:21                                                 ` Rafael J. Wysocki
2008-02-25  2:21                                                   ` Alan Stern
2008-02-25 11:41                                                     ` Rafael J. Wysocki
     [not found]                                                       ` <c4e36d110802250402q7312a488idf752e07db7504e8@mail.gmail.com>
2008-02-25 12:31                                                         ` Rafael J. Wysocki
2008-02-25  2:19                                                 ` Alan Stern
2008-02-25  9:03                                                   ` using long instead of atomic_t when only set/read is required (was Re: [Bug 10030] Suspend doesn't work when SD card is inserted) Pavel Machek
2008-02-25 14:46                                                     ` Alan Stern
2008-03-03 12:08                                                       ` [patch] Re: using long instead of atomic_t when only set/read is required Pavel Machek
2008-03-03 15:42                                                         ` Alan Stern
2008-03-03 15:53                                                           ` Alan Cox
2008-03-03 17:11                                                             ` Alan Stern
2008-03-03 17:26                                                               ` Linus Torvalds
2008-03-03 17:44                                                                 ` Pavel Machek
2008-03-03 19:27                                                                   ` Alan Stern
2008-03-06 15:58                                                                 ` Mark Lord
2008-03-06 16:11                                                                   ` Linus Torvalds
2008-03-06 16:27                                                                     ` Mark Lord
2008-03-03 17:16                                                             ` Nick Piggin
2008-03-03 17:31                                                               ` Paul E. McKenney
2008-03-03 17:33                                                               ` Alan Cox
2008-03-03 17:22                                                           ` Paul E. McKenney [this message]
2008-03-03 15:48                                                         ` Alan Cox
2008-03-03 17:24                                                           ` Pavel Machek
2008-03-03 20:27                                                             ` Rafael J. Wysocki
2008-03-03 21:12                                                               ` Paul E. McKenney
2008-03-03 22:23                                                               ` Linus Torvalds
2008-03-04 23:32                                                             ` Peter Hartley
2008-03-04 23:54                                                               ` Rafael J. Wysocki
2008-03-05  0:26                                                                 ` Paul E. McKenney
2008-02-25 11:40                                                   ` [Bug 10030] Suspend doesn't work when SD card is inserted Rafael J. Wysocki
2008-03-05 17:07 [patch] Re: using long instead of atomic_t when only set/read is required Matti Linnanvuori

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=20080303172230.GB12453@linux.vnet.ibm.com \
    --to=paulmck@linux.vnet.ibm.com \
    --cc=akpm@osdl.org \
    --cc=davem@davemloft.net \
    --cc=drzeus-mmc@drzeus.cx \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@lists.linux-foundation.org \
    --cc=pavel@ucw.cz \
    --cc=rjw@sisk.pl \
    --cc=stern@rowland.harvard.edu \
    --cc=torvalds@linux-foundation.org \
    --cc=zdenek.kabelac@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;
as well as URLs for NNTP newsgroup(s).