tutorial question: where mb() is needed?

Andrea Arcangeli (andrea@e-mind.com)
Wed, 6 Jan 1999 01:58:50 +0100 (CET)


I looked at the new SMP rescheduling code in pre2.2.0. I think to
understand everything except a wmb(). I don't understand very well why
wmb() is needed there:

if (sched_data->prev->state != sched_data->prevstate)
current->need_resched = 1;

/*
* Release the previous process ...
*
* We have dropped all locks, and we must make sure that we
* only mark the previous process as no longer having a CPU
* after all other state has been seen by other CPU's. Thus
* the write memory barrier!
*/
wmb();
sched_data->prev->has_cpu = 0;
#endif /* __SMP__ */

I think to know what mb()/rmb()/wmb() (I also seen that the function names
are taken by the Alpha asm instruction ;) do, but I don't understand very
well which are the real world SMP/UP cases we need it...

Is there an example somewhere of a minimal multithreaded or device-driver
code that needs mb() just to see the point?

And why don't we use SMP-atomic operation (using lock) instead of use
mb() to avoid races? I know I am missing something somewhere about mb()...

The only example on the web I found is a PPC instruction reordering
example that looks like this:

*(int *) 0x1 = 1;
*(int *) 0x2 = 2;
*(int *) 0x3 = 3;

A = *(int *) 0x1;
B = *(int *) 0x2;
C = *(int *) 0x3;

and if run it will really run on the bus in sequence:

*(int *) 0x1 = 1;
A = *(int *) 0x1;

*(int *) 0x2 = 2;
B = *(int *) 0x2;

*(int *) 0x3 = 3;
C = *(int *) 0x3;

because the CPU (powerpc) is reordering the asm instructions. I understand
that putting an mb() in the source code it would generate the expected
executin sequence on the bus.

But I can't join my current understanding of mb() with SMP race issues....
:(

Thanks in advance to anyone will give some hints....

Andrea Arcangeli

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/