Linux Kernel Barriers

Sometimes you need to have the memory read (load) and write operation (store) to be executed in certain order. But it may be that complier (or processor) can reorder reads and writes for performance reason.

It is possible to instruct the compiler not to reorder read or write at a given point. These instructions are called barriers.

Let us take a simple example

x = 1;
y = 2;

On some processors “y” may store new value first before “x”. That is second store instruction above gets executed before 1st store instruction.

But this reorder will never happen if the instructions are related, for example

x = 1;
y = x;

A mb() call provides both read and write barrier. No load and store will get reordered across this barrier.

Thus the below instructions will not reorder now

x = 1;
mb(); /* barrier */
y = 2;

Further Read

https://en.wikipedia.org/wiki/Memory_barrier

https://www.kernel.org/doc/Documentation/memory-barriers.txt

Leave a Reply

Your email address will not be published. Required fields are marked *