</>EmbeddedInterview

What is a memory barrier, and why is it needed on ARM?

MediumOperating System Concepts

A memory barrier constrains ordering or completion of memory operations. Arm permits weak ordering of normal memory accesses, so a producer and consumer cannot assume every store becomes visible in source order.

1. Example without ordering

A producer writes data=100 and then flag=1. A consumer observes flag=1 and reads data. Without synchronization, it may not observe the intended payload; in ordinary threaded C, a data race is also undefined.

2. Linux kernel publication

// Producer
WRITE_ONCE(data, 100);
smp_store_release(&flag, 1);

// Consumer
if (smp_load_acquire(&flag) == 1)
    value = READ_ONCE(data);

If the acquire reads the release in this one-time publication protocol, the ordering ensures the associated data is available to the consumer.

3. ARM barrier instructions

Instruction Purpose
DMB Orders specified memory accesses in the relevant scope
DSB Requires specified operations to complete before proceeding as defined
ISB Synchronizes subsequent instruction execution after context changes

A compiler barrier does not emit a hardware fence. Cache maintenance and DMA ownership may require additional APIs.

Interview takeaway: Coherency is not ordering, and volatile is not synchronization. Use acquire/release, locks or documented barrier APIs appropriate to the observer and memory type.