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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With