Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is wmb() in linux driver

In one of the PCI DMA driver i have seen wmb() many times. What actually does wmb() function do? Somewhere its mentioned as "holy water that doesn't hurts". Whats the purpose of that function?

like image 734
0xAB1E Avatar asked May 14 '15 11:05

0xAB1E


2 Answers

Stakx's comment on your question already points to the full answer: indeed it is a write memory barrier.

But if you are not so familiar with the concept, the document may not provide enough information on the "why".

Let's assume a serial port, where you have to write bytes to a certain address. The serial chip will then send these bytes over the wires. It is then important that you do not mess up the writes - they have to stay in order or everything is garbled.

But the following is not enough:

   *serial = 'h';
   *serial = 'e';
   *serial = 'l';
   *serial = 'l';
   *serial = 'o';

Because the compiler, the processor, the memory subsystems and the buses in between are allowed to reorder your stores as optimization (believe me, yes they are and yes, they do).

so you'll have to add code that will ensure the stores do not get tangled up. That's what, amongst others, the wmb() macro does: prevent reordering of the stores.

Note that just making the serial pointer volatile is not enough: while it ensures the compiler will not reorder, the other mechanisms mentioned can still wreak havoc. This is documented in another kernel doc piece.

A good read on the matter is Paul McKenney's book, available as free PDF and as paid-for dead tree version.

like image 126
Klaas van Gend Avatar answered Sep 23 '22 21:09

Klaas van Gend


Wmb means write memory barrier. This function inserts hardware memory barriers in the compiled instruction flow. Also it is platform dependent. This function guarantees ordering in write operation. The write operation before the wmb() function is completed prior to the execution of any subsequent write.

like image 33
Ranjini Avatar answered Sep 24 '22 21:09

Ranjini