Re: [PATCH 0/2] jump label: 2.6.38 updates

From: Matt Fleming
Date: Mon Feb 14 2011 - 17:23:41 EST


On Mon, 14 Feb 2011 16:39:36 -0500
Steven Rostedt <rostedt@xxxxxxxxxxx> wrote:

> On Mon, 2011-02-14 at 16:29 -0500, Steven Rostedt wrote:
>
> > > while (atomic_read(&foo) != n)
> > > cpu_relax();
> > >
> > > and the problem is that cpu_relax() doesn't know which particular
> > > cacheline to flush in order to make things go faster, hm?
> >
> > But what about any global variable? Can't we also just have:
> >
> > while (global != n)
> > cpu_relax();
> >
> > ?
>
> Matt Fleming answered this for me on IRC, and I'll share the answer
> here (for those that are dying to know ;)
>
> Seems that the atomic_inc() uses ll/sc operations that do not affect
> the cache. Thus the problem is only with atomic_read() as
>
> while(atomic_read(&foo) != n)
> cpu_relax();
>
> Will just check the cache version of foo. But because ll/sc skips the
> cache, the foo will never update. That is, atomic_inc() and friends do
> not touch the cache, and the CPU spinning in this loop will is only
> checking the cache, and will spin forever.

Right. When I wrote the atomic_read() implementation that Will is
talking about I used the ll-equivalent instruction to bypass the cache,
e.g. I wrote it assembly because the compiler didn't emit that
instruction.

And that is what it boils down to really, the ll/sc instructions are
different from any other instructions in the ISA as they bypass the
cache and are not emitted by the compiler. So, in order to maintain
coherence with other cpus doing atomic updates on memory addresses, or
rather to avoid reading stale values, it's necessary to use the ll
instruction - and this isn't possible from C.

> Thus it is not about global, as global is updated by normal means and
> will update the caches. atomic_t is updated via the ll/sc that ignores
> the cache and causes all this to break down. IOW... broken hardware ;)

Well, to be precise it's about read-modify-write operations - the
architecture does maintain cache coherence in that writes from one CPU
are immediately visible to other CPUs.

FYI spinlocks are also implemented with ll/sc instructions.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/