Please, explain the purpose of memory barrier functions (rmb(), wmb(), etc.) in the kernel.
Memory barriers force execution order for CPU and compiler. Compiler and CPU may reorder instructions for optimization purposes. Sometimes this may be the cause of undesired effects.
For example (list insertion):
new_element->next = old_element->next; old_element->next = new_element;
If the instructions will be reordered during compilation (compiler optimization) or execution (CPU) then it is possible that other kernel thread (because of irq or preemption) will read the corrupted list. To prevent such reordering it is required to insert write memory barrier (wmb) between instructions. Memory barrier means that all memory operations before the barrier are committed before those after the barrier. Memory barriers may be of different types. Write memory barrier (wmb) enforces store operations order. Read memory barrier (rmb) enforces read operations order. General memory barrier (mb) enforces an order of all operations. CPU support special instructions for memory barries. Compilers just support special directive.