How to Disable CPU Cache for Certain Memory Regions
In this article
You cannot disable the CPU cache for a specific memory region from user space on a normal operating system, and on most hardware you cannot do it at all. The CPU cache is managed by hardware; the only practical way to keep a region of memory out of the cache is to change the memory type of that region in the page tables (or the memory controller) so the hardware marks it as uncached, and that requires kernel-level code or a driver. If you are writing a driver, firmware, or bare-metal code, you can map a region as uncached using the platform's memory attributes. If you are writing an ordinary application, the realistic options are non-temporal (streaming) load and store instructions, which reduce cache pollution for a single access, or using a device driver that already exposes an uncached mapping such as /dev/mem with the right flags. This guide explains the real mechanisms, the order to do them in, and the mistakes that cause crashes or silent data corruption.
What You'll Need
- A clear idea of which memory region you mean: a physical address range (for example a device's MMIO window) or a virtual range in a process.
- Kernel or bare-metal access. On Linux this means a loadable kernel module, or a bootloader/UEFI stage where you control the page tables. On Windows it means a kernel-mode driver. User-space code cannot change cache attributes.
- The CPU architecture reference manual for your platform (x86-64, ARMv7, ARMv8-A, RISC-V) because the attribute names and registers differ.
- A development board or virtual machine you can reboot safely. A wrong memory type on a region the kernel uses will hang the machine.
- For non-temporal access from user space: a compiler that supports intrinsics such as
_mm_stream_si128or__builtin_nontemporal_store.
Step-by-Step
- Decide what "uncached" has to mean for your region. There are three different things people call "disabling the cache": mapping memory as uncached so every access goes to RAM or the device, using non-temporal instructions so a single loop does not fill the cache, and flushing the cache after writes so other observers see them. Pick one. Mapping as uncached is the only one that applies to a whole region permanently.
- Confirm the region is safe to remap. Never remap kernel text, the kernel's own data, or memory the page allocator is using. The safe targets are device MMIO ranges declared in the platform's device tree or ACPI tables, or a physically contiguous buffer you allocated yourself with
dma_alloc_coherentor an equivalent. On x86, check/proc/iomemto see which physical ranges belong to devices and which belong to System RAM. - On Linux, write a small kernel module that maps the range with the uncached attribute. For a device range, the cleanest route is
ioremap(ordevm_ioremap) which already returns an uncached mapping on most architectures. Do not useioremapon System RAM. If you must map RAM uncached, usememremapwith the appropriate flags, and expect a large performance penalty. - On ARM and RISC-V, set the memory type in the page tables. In a bare-metal or early-boot context you control the translation tables directly. ARMv8-A uses MAIR_EL1 to define memory attribute indices and then selects one in each block or page descriptor. Mark the region as Device-nGnRnE or Device-nGnRE for MMIO, or Normal Non-cacheable for RAM you want uncached but still speculatively accessible. ARMv7 uses the TEX, C and B bits in the short-descriptor format. RISC-V uses the PBMT extension where available, or a PMA region configured outside the page tables.
- On x86-64, change the PAT or MTRR entry for the range. The page tables alone only offer cacheable versus uncached through the PCD bit; the full set of memory types (write-combining, write-through, write-protected) comes from the Page Attribute Table. Set the PAT entry you want, then set the PAT/PCD/PWT bits in the page-table entry for the region. For large physical ranges that must be uncached system-wide, a variable MTRR can cover them, but MTRRs are a scarce resource and conflicts with the fixed MTRRs cause boot failures.
- Flush and invalidate the cache for the range after changing attributes. Existing cache lines for that address may still hold stale data. On ARM use
dc civacandic iallusequences or the kernel'sflush_cache_rangehelpers. On x86 usewbinvdonly in early boot or with extreme care, since it is very slow. In a driver, prefer the architecture's DMA sync APIs. - Verify with a timing test and a coherency test. Read the same word from the region in a tight loop and compare against a known-cached buffer; an uncached read is dramatically slower and its latency does not drop with repetition. Then have a second agent (a DMA engine or another core) write to the region and confirm your core sees the new value without any explicit cache maintenance.
- For ordinary applications, use non-temporal accesses instead. If you only need to avoid polluting the cache while streaming through a large buffer, use streaming loads and stores. In C or C++ with GCC or Clang,
__builtin_nontemporal_loadand__builtin_nontemporal_storegenerate the right instructions on x86 (MOVNTDQA, MOVNTDQ) and on AArch64 where the LDNP/STNP forms are available. This does not make the region uncached; it only changes how those particular instructions interact with the cache.
Common Mistakes
- Trying to do it from user space. There is no portable API, and the x86 CLFLUSH and ARM DC CIVAC instructions only clean or invalidate lines; they do not stop the hardware from caching the region again on the next access.
- Marking normal RAM as Device memory. On ARM this removes speculative access and unaligned-access support, and code that runs from or through that region can fault or behave unpredictably. Use Normal Non-cacheable for RAM.
- Forgetting cache maintenance after the change. Dirty lines from before the remap can be written back later and overwrite what a device wrote, which looks like random corruption.
- Assuming uncached means fast for MMIO. Uncached accesses are slow; write-combining exists precisely to batch them. Choose the weakest ordering that is still correct for your device.
- Using MTRRs for large ranges. There are only a handful of variable MTRRs, and overlapping or conflicting entries can cause the platform to disable caching entirely or fail to boot.
Tips
- Prefer
ioremapand device-tree or ACPI-described ranges over hard-coded physical addresses; firmware already knows which ranges are device memory. - Keep the uncached region as small as possible. Every uncached access costs far more than a cached one, and on some CPUs an uncached read stalls the whole memory pipeline.
- Document the memory type you chose and why. The next person to touch the driver will otherwise "optimise" it back to cacheable and reintroduce the bug.
- If the goal is coherent DMA, use the DMA API rather than hand-rolling uncached mappings; it handles the architecture differences for you.
Troubleshooting
- The machine hangs immediately after the remap. You almost certainly remapped memory the kernel is executing from or using for its own data. Reboot and restrict the range to device MMIO or a buffer you allocated.
- Reads return stale data. The mapping is still cacheable, or the cache was not invalidated after the change. Check the page-table bits and add an invalidate for the range.
- Writes disappear or arrive out of order. The device needs a stronger ordering than Normal Non-cacheable provides. Use Device-nGnRnE on ARM, or add the appropriate barrier instructions.
- Performance collapses. Expected for uncached memory. Narrow the region, switch to write-combining if the device tolerates it, or use non-temporal instructions for streaming buffers instead.
- It works on one board and fails on another. Memory attributes interact with the interconnect and with firmware configuration. Check the device tree or ACPI tables on the failing board and confirm the region is described as you expect.
Frequently Asked Questions
Can I disable the cache for one array in my C program?
No. Cacheability is a property of the page mapping, not of a variable. You can use non-temporal load and store intrinsics for that array, which avoids filling the cache with it, but the hardware still caches the lines if anything else touches them normally.
Does mmap have a flag for uncached memory?
Not portably. Some drivers expose their own mmap implementation that returns an uncached mapping for a device, and on some platforms there are architecture-specific ways to request it, but there is no standard flag, and mapping ordinary RAM uncached from user space is not supported.
Is disabling the cache the same as making memory coherent?
No. Coherency means all agents see the same value without software intervention, which on most modern systems is maintained by hardware for cacheable memory. Uncached memory sidesteps the cache but is not automatically ordered or coherent with respect to other agents unless the bus protocol guarantees it.
Can I do this on a phone or a game console?
Only with kernel or firmware access, which those platforms do not provide to ordinary developers. On a phone you would need to be writing a driver in the vendor's kernel tree; on a console the memory map is controlled by the platform and not exposed.
What is the difference between uncached and write-combining?
Uncached means every access goes to the memory or device individually. Write-combining lets the CPU gather writes into a buffer and send them in bursts, which is much faster for framebuffers and similar write-only regions, but reads from a write-combining region are not guaranteed to see recent writes.