What is the difference between a cache clean and invalidate?
Short answer: Cleaning writes dirty data toward the required coherency point; invalidation discards cached copies. Incorrect invalidation can lose dirty data. Use platform APIs for DMA.
DC CVAC, X0 // clean by VA to PoC
DSB SY // wait for completion; illustrative
1. Clean: A dirty cache line is written toward the specified coherency point, allowing required observers to obtain updated contents.
2. Invalidate: A cached copy is discarded so later access must obtain a valid copy again. Discarding dirty data without correct handling may lose updates.
| Operation | Typical ARM instruction |
|---|---|
| Clean by VA to PoC | DC CVAC |
| Invalidate by VA to PoC | DC IVAC |
| Clean and invalidate by VA to PoC | DC CIVAC |
3. Practical notes: Maintenance applies to cache lines; alignment, scope and barriers matter. PoC is the point of coherency, not necessarily DRAM. Privilege and cache configuration affect permitted instructions.
Interview takeaway: Clean makes dirty data available as required; invalidate removes cached copies. Prefer Linux DMA APIs in drivers.
Worked cache-line maintenance example
Assume a 64-byte cache line and a CPU buffer starting at 0x1020 with length 80 bytes. The range touches two complete cache lines: 0x1000–0x103F and 0x1040–0x107F. A raw per-line maintenance operation affects the whole corresponding line, not just the requested bytes. Inappropriate invalidation can discard dirty data belonging to a neighboring object. This is one reason non-coherent DMA buffers need suitable allocation/alignment and ownership rules.
Clean is useful before a non-coherent device reads data produced by the CPU; invalidate or the platform's prescribed synchronization is needed before the CPU reads data produced by a device. The DMA API may perform maintenance at mapping, synchronization and unmapping as required by the platform and direction.
PoC (point of coherency) is the point at which relevant observers are guaranteed to see a coherent copy; PoU (point of unification) concerns the point where relevant instruction and data accesses see a unified copy. Executable-code modification may require data-cache cleaning to PoU, instruction-cache invalidation and prescribed DSB/ISB operations. Use architecture-specific helpers rather than treating a data-cache invalidate as a general instruction-cache synchronization operation.