Re: [patch] Re: using long instead of atomic_t when only set/read is required

3 messages, 3 authors, 2008-03-06 · open the first message on its own page

Re: [patch] Re: using long instead of atomic_t when only set/read is required

From: Linus Torvalds <torvalds@linux-foundation.org>
Date: 2008-03-03 17:38:39


On Mon, 3 Mar 2008, Alan Stern wrote:
Consider a routine like the following:

	static task_struct *the_task;

	void store_task(void)
	{
		the_task = current;
	}

Is it possible to say whether readers examining "the_task" are 
guaranteed to see a coherent value?
Yes, we do depend on this.  All the RCU stuff (and in general *anything* 
that depends on memory ordering as opposed to full locking, and we have 
quite a lot of it) is very fundamentally dependent on the fact that things 
like pointers get read and written atomically.

HOWEVER, it is worth pointing out that it's generally true in a 
"different" sense than the actual atomic accesses. For example, if you 
test a single bit of a word, it's still quite possible that gcc will have 
turned that "atomic" read into a single byte read, so it's not necessarily 
the case that we'll actually even read the whole word. 

(Writes are different: if you do things like bitwise updates they simply 
*will*not* be atomic, but that's simply not what we depend on anyway).

So in that sense, the atomicity guarantees are a lot weaker than the ones 
we do for IO accesses, but that's all fine. Memory isn't IO, and doesn't 
have side effects.

			Linus

Re: [patch] Re: using long instead of atomic_t when only set/read is required

From: Pavel Machek <hidden>
Date: 2008-03-03 17:44:22

Hi!
quoted
Consider a routine like the following:

	static task_struct *the_task;

	void store_task(void)
	{
		the_task = current;
	}

Is it possible to say whether readers examining "the_task" are 
guaranteed to see a coherent value?
Yes, we do depend on this.  All the RCU stuff (and in general *anything* 
that depends on memory ordering as opposed to full locking, and we have 
quite a lot of it) is very fundamentally dependent on the fact that things 
like pointers get read and written atomically.

HOWEVER, it is worth pointing out that it's generally true in a 
"different" sense than the actual atomic accesses. For example, if you 
test a single bit of a word, it's still quite possible that gcc will have 
turned that "atomic" read into a single byte read, so it's not necessarily 
the case that we'll actually even read the whole word. 

(Writes are different: if you do things like bitwise updates they simply 
*will*not* be atomic, but that's simply not what we depend on anyway).
Ok... can we get Alan Stern's patch into Documentation/atomic_ops.txt
, then? I was not aware of this, and there seems to be lot of
confusion around...

Plus... I really don't think we can "just access" this as normal
pointers... due to the compiler issues Alan Cox mentioned, and due to
the ACCESS_ONCE() issue.

								Pavel
-- 
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

Re: [patch] Re: using long instead of atomic_t when only set/read is required

From: Mark Lord <hidden>
Date: 2008-03-06 15:58:30

Linus Torvalds wrote:
On Mon, 3 Mar 2008, Alan Stern wrote:
quoted
Consider a routine like the following:

	static task_struct *the_task;

	void store_task(void)
	{
		the_task = current;
	}

Is it possible to say whether readers examining "the_task" are 
guaranteed to see a coherent value?
Yes, we do depend on this.  All the RCU stuff (and in general *anything* 
that depends on memory ordering as opposed to full locking, and we have 
quite a lot of it) is very fundamentally dependent on the fact that things 
like pointers get read and written atomically.
..

But also consider something like this:

 	void store_task(void)
 	{
 		*the_task = current;
 	}

In this case, there is no guarantee that the assignment
can be done atomically on all CPU types.  Some RISC archs
(eg. MIPS R2xxx) require an (interruptible) instruction pair
to store values to a potentially unaligned address.

This was a BIG issue on a different system that I once worked on.

Cheers
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help