[lkml]   [2006]   [Jul]   [8]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
SubjectRe: [patch] spinlocks: remove 'volatile'
On Saturday 08 July 2006 04:59, Krzysztof Halasa wrote:
> Chase Venters <> writes:
> > Locks are supposed to be syncronization points, which is why they
> > ALREADY HAVE "memory" on the clobber list! "memory" IS NECESSARY. The
> > fact that "=m" is changing to "+m" in Linus's patches is because "=m"
> > is in fact insufficient, because it would let the compiler believe
> > we're just going to over-write the value. "volatile" merely hides that
> > bug -- once that bug is _fixed_ (by going to "+m"), "volatile" is no
> > longer useful.
> This is a completely different story. "volatile", barrier() and "+m"/"=m"
> aren't sync points. If the variable access isn't atomic you must use
> locking even with volatiles, barriers etc.

You're mincing my words. The reason "memory" is on the clobber list is because
a lock is supposed to synchronize all memory accesses up to that point. It's
a fence / a barrier, because if the compiler re-orders your loads/stores
across the lock, you're in trouble. That's exactly what I was pointing out.

> > If "volatile" is in use elsewhere (other than locks), it's still
> > probably wrong. In these cases, you can use a barrier, a volatile
> > cast, or an inline asm with a specific clobber.
> A volatile cast is just a volatile, moved from data declaration to
> all access points. It doesn't buy you anything.
> barrier() is basically "all-volatile". All of them operate on the same,
> compiler level.

A volatile cast lets you prevent the compiler from always treating the
variable as volatile.

> If the "volatile" is used the wrong way (which is probably true for most
> cases), then volatile cast and barrier() will be wrong as well. You need
> locks or atomic access, meaningful on hardware level.

No. Linus already described what some example invalid uses of "volatile" are.
One example is the very one this whole thread is about - using 'volatile' on
the declaration of the spinlock counter. That usage is _wrong_, and barrier()
would not be. (Of course, it doesn't directly apply here, because barrier()
already existed, by necessity, due to the "memory" clobber. And the lock's
not done in pure C.)

Volatile originally existed to tell the compiler a variable could change at
will. Because of reordering, it's almost never sufficient with our modern
compilers and CPUs. That's precisely where barrier() (and/or its hardware
equivalents) help in places where 'volatile' is wrong. Your statement is
additionally wrong because one use-case of memory barriers is to safely write
lock-free code.

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to
More majordomo info at
Please read the FAQ at

 \ /
  Last update: 2006-07-08 15:45    [W:0.087 / U:0.928 seconds]
©2003-2018 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site