What is a memory barrier?
Short answer: A barrier constrains memory ordering. On ARM, DMB orders relevant accesses, DSB also waits for required completion, and ISB synchronizes instruction execution after context changes.
STR W0, [X1]
DMB ISHST
STR W2, [X3]
1. Without a memory barrier
Consider two CPU cores sharing data and flag, initially zero. The following is conceptual pseudocode, not safe portable C concurrency:
// CPU Core 0
data = 100;
flag = 1;
// CPU Core 1
if (flag == 1)
value = data;
On weakly ordered ARM systems, Core 1 may observe flag = 1 before data = 100 and obtain the old value 0.
2. With a memory barrier (Linux kernel)
// CPU Core 0
WRITE_ONCE(data, 100);
smp_wmb();
WRITE_ONCE(flag, 1);
// CPU Core 1
if (READ_ONCE(flag) == 1) {
smp_rmb();
value = READ_ONCE(data);
}
When the consumer observes the published flag, the paired barriers ensure it can read the earlier data in this one-time publication example.
3. ARM barrier instructions
| Instruction | Purpose |
|---|---|
| DMB | Orders relevant memory accesses |
| DSB | Also waits for required completion |
| ISB | Synchronizes subsequent instruction execution after context changes |
4. Linux kernel APIs
| API | Purpose |
|---|---|
| smp_rmb() | Orders reads |
| smp_wmb() | Orders writes |
| smp_mb() | Orders reads and writes |
| barrier() | Compiler ordering only |
| smp_store_release() | Store with release ordering |
| smp_load_acquire() | Load with acquire ordering |
5. Coherency vs barrier
| Cache coherency | Memory barrier |
|---|---|
| Consistency of cached copies | Ordering across memory accesses |
| Usually hardware-managed | Explicit ordering where needed |
6. Simpler Linux publication pattern
// Producer: one-time publication
WRITE_ONCE(data, 100);
smp_store_release(&flag, 1);
// Consumer
if (smp_load_acquire(&flag))
value = READ_ONCE(data);
Release orders prior accesses before publication; acquire orders later accesses after observing it. Use C11 atomics in portable userspace code. A device ownership flag instead needs the DMA API and appropriate DMA/MMIO barriers.
Interview takeaway: Barriers enforce memory ordering, not atomicity or automatic cache writeback. The barrier scope and memory type matter. Linux memory barriers.
Choosing the correct barrier for a concrete situation
| Situation | Relevant mechanism |
|---|---|
| Publish normal-memory data between CPU cores | Release/acquire or paired SMP barriers |
| Publish a DMA descriptor before ownership | DMA ordering primitives plus required mapping/sync |
| Complete specified cache maintenance before proceeding | Architecture-required DSB scope |
| Change a system register affecting instruction execution | Required context synchronization, often ISB |
The barrier domain matters: DMB ISH orders specified accesses for the inner-shareable domain, DMB ISHST restricts the ordering to the appropriate store class, and other scopes/variants serve different observers. DSB is not a universal instruction that flushes all caches or waits for every peripheral's internal operation; it waits for the operations required by its architectural type and scope. ISB synchronizes the instruction stream with context changes; it is not a substitute for data-memory ordering.
A typical one-time Linux publication can also be written more directly:
// Producer
WRITE_ONCE(data, 100);
smp_store_release(&flag, 1);
// Consumer
if (smp_load_acquire(&flag) == 1)
value = READ_ONCE(data);
Both sides must participate in the synchronization protocol; a producer barrier alone does not solve every consumer ordering issue. Use portable C11 atomic operations for userspace threads and Linux SMP/DMA/MMIO APIs for kernel code. Do not insert raw barriers blindly or rely on volatile for ordering.