Skip to contentSkip to conversation

Difference between atomic operations and memory barriers

DISCUSSION Leonidas . 7 participants 16 messages Oct 26, 2009 – Oct 28, 2009 1 message not in the archive

lore
Share & tools
lore

Leonidas .

author
#0 lore raw

Trying to understand difference between atomic ops and memory barriers.

I was thinking all atomic operations must be using barrier internally, but I read
somewhere that only some of them use barriers. Sorry for being vague here.

Operations before call to smp_mb() will not be re-ordered and all cpus will see consistent value after the variable is updated.

E.g.
void * ptr = (void *) str;
smb_mb();

Will this not atomically update ptr? Ptr will be seen by all cpu's in same state
after it has been assigned str, right?

-Leo.

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#2 lore raw
Quoted 8 lines · Noah Watkins
Quoted 2 lines

Trying to understand difference between atomic ops and memory barriers.

There is a bit of info in:
   Documentation/atomic_ops.txt

-noah

Thanks for the pointer, there are no atomic operations for pointers, right? I guess barrier is what we need in that case.

-Leo.

#3 lore raw
Quoted 8 lines · Leonidas .
Quoted 4 lines · Noah Watkins
Quoted 2 lines

Trying to understand difference between atomic ops and memory barriers.

There is a bit of info in:
Documentation/atomic_ops.txt

Thanks for the pointer, there are no atomic operations for pointers, right? I guess barrier is what we need in that case.

You need barriers for the atomics too, at least in some cases. From that document:

*** WARNING: atomic_read() and atomic_set() DO NOT IMPLY BARRIERS! ***

For simple assignments, I'm fairly sure linux assumes that writes to individual variables are atomic. ie, if you're assigning to an int you won't get half of the assignment completed while the other half isn't.

What the atomic ops buy you is the ability to do increment-and-test, compare-and-exchange, etc. in an atomic fashion.

Barriers are needed to make sure that operations are visible on other cpus in the correct order, and to ensure that the compiler doesn't reorder the operations.

If you use the normal locking mechanisms (mutexes, spinlocks, etc.) the barriers are implicit in the lock.

Chris

author
#4 lore raw
Quoted 23 lines · Chris Friesen
Quoted 8 lines · Leonidas .
Quoted 4 lines · Noah Watkins
Quoted 2 lines

Trying to understand difference between atomic ops and memory barriers.

There is a bit of info in:
   Documentation/atomic_ops.txt

Thanks for the pointer, there are no atomic operations for pointers, right? I guess barrier is what we need in that case.

You need barriers for the atomics too, at least in some cases.  From that document:

*** WARNING: atomic_read() and atomic_set() DO NOT IMPLY BARRIERS! ***

For simple assignments, I'm fairly sure linux assumes that writes to individual variables are atomic.  ie, if you're assigning to an int you won't get half of the assignment completed while the other half isn't.

What the atomic ops buy you is the ability to do increment-and-test, compare-and-exchange, etc. in an atomic fashion.

Quoted 9 lines

Barriers are needed to make sure that operations are visible on other cpus in the correct order, and to ensure that the compiler doesn't reorder the operations.

If you use the normal locking mechanisms (mutexes, spinlocks, etc.) the barriers are implicit in the lock.

Chris

This takes it a step further ! Thanks for the info.

But locks are expensive for simpler assignments like integer assignment thats why there are atomic counter parts, but there is no such things for pointers right?

So we can safely assume that pointer assignment will be done in an atomic manner?

-Leo.

#5 lore raw
Quoted 3 lines

So we can safely assume that pointer assignment will be done in an atomic manner?

See the the comment above rcu_assign_pointer in
include/linux/rcupdate.h

-noah

Quoted 1 line

-Leo.

#6 lore raw
Quoted 5 lines · Noah Watkins
Quoted 2 lines

So we can safely assume that pointer assignment will be done in an atomic manner?

See the the comment above rcu_assign_pointer in
include/linux/rcupdate.h

This comment only talks about ordering, not about atomicity.

Again, AFAIR the ISO C spec should explain what is going to be guaranteed atomic and what might not be atomic.

rcu_assign_pointer() itself does rely on atomicity of pointer assignments though, like lots of code elsewhere in the kernel.

Signature · 3 lines
Stefan Richter
-=====-==--= =-=- ==-=-
http://arcgraph.de/sr/
#7 lore raw
Quoted 8 lines
Quoted 5 lines · Noah Watkins
Quoted 2 lines

So we can safely assume that pointer assignment will be done in an atomic manner?

See the the comment above rcu_assign_pointer in
include/linux/rcupdate.h

This comment only talks about ordering, not about atomicity.

Ahh, yeh that's true.

Quoted 3 lines

Again, AFAIR the ISO C spec should explain what is going to be guaranteed atomic and what might not be atomic.

This seems likely. I wonder if there are any strange architectures out there that have some insane implementation.

Quoted 1 line
#8 lore raw
In reply to Leonidas .
Quoted 2 lines · Leonidas .

So we can safely assume that pointer assignment will be done in an atomic manner?

Has anybody ever actually made a *production* CPU that had non-atomic pointer assignments? And how long before the crazed programmers lynched and burned the offending CPU designer at the stake? ;)

Non-atomic pointer assignments are the CPU design equivalent of Vogon poetry. Just Say No. With a shotgun if needed.

#9 lore raw
Quoted 11 lines · On 10/27/2009 01:51 PM, [email protected] wrote:
Quoted 2 lines · Leonidas .

So we can safely assume that pointer assignment will be done in an atomic manner?

Has anybody ever actually made a *production* CPU that had non-atomic pointer assignments? And how long before the crazed programmers lynched and burned the offending CPU designer at the stake? ;)

Non-atomic pointer assignments are the CPU design equivalent of Vogon poetry. Just Say No. With a shotgun if needed.

What don't you know? the CPU that started it all was like that, the x86 16-bit "large" and "huge" model had a double register seg:offset set, also in-memory was double-ints(2*16) even the i386 was running 16 bit modes for a long time.

Kernel still have 16-bit dosemu mode supported until today, no?

About the shotguns lynching and burning I'm not sure, but Intel survived just fine.

Boaz

#10 lore raw
Quoted 3 lines · Boaz Harrosh

What don't you know? the CPU that started it all was like that, the x86 16-bit "large" and "huge" model had a double register seg:offset set, also in-memory was double-ints(2*16) even the i386 was running 16 bit modes for a long time.

Yes, but there were instructions to load segment registers, and those were atomic, and there were instructions to compute effective addresses based on the segment registers, and those were basically atomic. But you never had a chance to see a partly loaded segment register (just like in today's virtual memory equivalents, a PTE's effect is basically atomic - you never see half the address bits of a PTE take effect and not the other half).

Yes, if a segment register wasn't set right, you'd load/store from the wrong place in memory (we *still* have that, playing with %fs and %gs). But that's different than an actual load or store only half-happening, or happening partly to an old place and partly to a new one, because a segment register is only half-finished laoding.

#11 lore raw
In reply to Leonidas .
Quoted 2 lines · Leonidas .
Quoted 1 line · Noah Watkins
Quoted 1 line

Trying to understand difference between atomic ops and memory barriers.

Atomic accesses and barriers are different concepts.

The former means that there won't be intermediary values visible (to another CPU or to a DMA capable device) at any time; there will only be either the old value or the new value, but nothing half-done.

The latter means that an order between two (or more) separate accesses is enforced.

You cannot use barriers to make intrinsically non-atomic accesses look atomically; you need a lock for such a purpose. (Or RCU.)

Quoted 16 lines
Quoted 16 lines
Quoted 6 lines

I was thinking all atomic operations must be using barrier internally, but I read somewhere that only some of them use barriers. Sorry for being vague here.

Operations before call to smp_mb() will not be re-ordered and all cpus will see consistent value after the variable is updated.

Quoted 6 lines

E.g.
void * ptr = (void *) str;
smb_mb();

Will this not atomically update ptr? Ptr will be seen by all cpu's in same state after it has been assigned str, right?

There is a bit of info in:
Documentation/atomic_ops.txt

Quoted 7 lines
Quoted 3 lines

-noah

Thanks for the pointer, there are no atomic operations for pointers, right? I guess barrier is what we need in that case.

There are more atomic accesses than those which deal with atomic_t. For example,

	any_t *ptr = something;

is definitely atomic, and we rely on this atomicity in the kernel at many places.

I guess these things can be found somewhere in the C language specification.

Signature · 3 lines
Stefan Richter
-=====-==--= =-=- ==-=-
http://arcgraph.de/sr/
author
#12 lore raw
Quoted 46 lines · Stefan Richter
Quoted 2 lines · Leonidas .
Quoted 1 line · Noah Watkins
Quoted 1 line

Trying to understand difference between atomic ops and memory barriers.

Atomic accesses and barriers are different concepts.

The former means that there won't be intermediary values visible (to another CPU or to a DMA capable device) at any time; there will only be either the old value or the new value, but nothing half-done.

The latter means that an order between two (or more) separate accesses is enforced.

You cannot use barriers to make intrinsically non-atomic accesses look atomically; you need a lock for such a purpose.  (Or RCU.)

Quoted 16 lines
Quoted 16 lines
Quoted 6 lines

I was thinking all atomic operations must be using barrier internally, but I read somewhere that only some of them use barriers. Sorry for being vague here.

Operations before call to smp_mb() will not be re-ordered and all cpus will see consistent value after the variable is updated.

Quoted 6 lines

E.g.
void * ptr = (void *) str;
smb_mb();

Will this not atomically update ptr? Ptr will be seen by all cpu's in same state after it has been assigned str, right?

There is a bit of info in:
   Documentation/atomic_ops.txt

Quoted 7 lines
Quoted 3 lines

-noah

Thanks for the pointer, there are no atomic operations for pointers, right? I guess barrier is what we need in that case.

There are more atomic accesses than those which deal with atomic_t.  For example,
       any_t *ptr = something;
is definitely atomic, and we rely on this atomicity in the kernel at many places.

Quoted 6 lines

I guess these things can be found somewhere in the C language specification. --
Stefan Richter
-=====-==--= =-=- ==-=-
http://arcgraph.de/sr/

If your answer matches my inference that,

any_t *ptr = something;

is always atomic even on SMPs without using locks, barriers then my doubt is cleared. Thanks.

-Leo.

#13 lore raw
Quoted 5 lines · Leonidas .

any_t *ptr = something;

is always atomic even on SMPs without using locks, barriers then my doubt is cleared. Thanks.

I assume that this only holds if the pointer (not the thing it points to) is denoted as volatile.

-Michael

author
#14 lore raw
Quoted 4 lines · Michael Schnell
Quoted 3 lines · Leonidas .

any_t *ptr = something;

Quoted 8 lines
Quoted 2 lines

is always atomic even on SMPs without using locks, barriers then my doubt is cleared. Thanks.

I assume that this only holds if the pointer (not the thing it points to) is denoted as volatile.

-Michael

I dont think so, volatile would only ensure no caching, so some cpus might see the cached
pointer (this is where you would want to use barriers), but pointer assignment would still be atomic.

-Leo.

#15 lore raw
Quoted 3 lines · Leonidas .

but pointer
assignment would still be atomic.

Yep. bit maybe the assignment to the memory cell that holds the pointer just would be optimized out and be done at all. This my be considered atomic but of course not desired for anything atomicness is considered for.

-Michael

#16 lore raw
In reply to Leonidas .
Quoted 10 lines · Leonidas .
Quoted 9 lines · Michael Schnell
Quoted 5 lines · Leonidas .

any_t *ptr = something;

is always atomic even on SMPs without using locks, barriers then my doubt is cleared. Thanks.

I assume that this only holds if the pointer (not the thing it points to) is denoted as volatile.

- Atomic access (either old or new value is visible to CPUs or devices, but never any intermediary, half-baked value),
- memory barrier (enforced order of accesses),
- volatile qualifier (disabled compiler optimization of accesses)

are three different concepts.

We rely on atomic pointer load and store all over the kernel, yet never qualify them as "volatile". (A look into the C language spec might bring clarity here. I don't have the spec at hand alas.)

Quoted 3 lines

I dont think so, volatile would only ensure no caching, so some cpus might see the cached pointer (this is where you would want to use barriers), but pointer assignment would still be atomic.

What "volatile" ensures or not ensures is not that well defined in the C language specification as far as I recall prior discussion. Its precise effects are compiler dependent. You are right in so far as volatile (given or missing) does not change whether an access is guaranteed atomic or not. Since it affects compiler optimizations, I think it could possibly affect atomicity of accesses which were never guaranteed to be atomic. But this should not be interesting to 99.99% of all kernel coders because they better use guaranteed atomic accesses when they really need them. (E.g. <linux/atomic.h> or pointer load/ store.)

Signature · 3 lines
Stefan Richter
-=====-==--= =-=- ==-==
http://arcgraph.de/sr/