Flush CPU Cache in Linux: C Program Guide
In this article
Flushing the CPU cache from a C program on Linux is sometimes needed to ensure memory writes are visible to other processors or devices, or to test cache-related performance effects. The primary method is to use the cacheflush system call (on ARM) or the __builtin___clear_cache GCC builtin (on x86 and ARM). This guide focuses on the portable approach using GCC builtins and the Linux-specific cacheflush syscall where applicable. You'll need a Linux system with GCC installed and basic knowledge of C and memory concepts.
What You'll Need
- A Linux machine (x86, ARM, or other architecture)
- GCC compiler (or Clang with compatible builtins)
- Basic understanding of C and memory caching
Step-by-Step
- Identify your architecture. Run
uname -mto see your CPU architecture. x86_64, i386, ARM, etc. The method may vary. - Include necessary headers. For most cases, include
<stdint.h>for integer types and<stdlib.h>for general utilities. If using the syscall, include<unistd.h>and<sys/syscall.h>. - Use the GCC builtin for cache flush. The most portable way is
__builtin___clear_cache. It takes two pointers: the start and end address of the memory range to flush. Example:__builtin___clear_cache((char*)ptr, (char*)ptr + size); - For ARM, use the cacheflush syscall. On ARM, you can call
syscall(SYS_cacheflush, start, end, flags). The flags should be 0 for instruction and data cache flush. Ensure you include<sys/syscall.h>and<unistd.h>. - Flush the entire data cache (if needed). To flush the whole data cache, you may need to iterate over memory regions or use architecture-specific instructions. For x86, you can use
_mm_clflushon each cache line, but that's invasive. A simpler approach is to use__builtin___clear_cacheon the memory range you care about. - Ensure memory ordering. After flushing, you might need a memory barrier. Use
__sync_synchronize()orstd::atomic_thread_fencein C11 to ensure ordering. - Test your code. Write a small program that writes to a variable, flushes the cache, and then reads it from another thread or process to verify visibility.
Common Mistakes
- Using
__builtin___clear_cacheincorrectly: the arguments are pointers to the start and end (exclusive) of the memory range. Passing a single pointer or wrong size will cause undefined behavior. - Forgetting that the cache flush only affects the current CPU's cache. Other CPUs may still have stale copies unless you use appropriate synchronization.
- Assuming the cacheflush syscall is available on all architectures. It is not on x86; there you must use the builtin or inline assembly.
- Not including the right headers, leading to implicit declaration errors.
- Flushing more memory than necessary, which can degrade performance.
Tips
- Use
__builtin___clear_cachefor portability; it works on GCC and Clang for multiple architectures. - For performance testing, flush only the specific cache lines you need, not the entire cache.
- If you're dealing with DMA or hardware devices, also consider using
dma_sync_*APIs in the kernel, but that's outside user space. - Use
getconf LEVEL1_DCACHE_LINESIZEto know the cache line size, which helps in aligning your flush operations. - For x86, you can use inline assembly
clflushinstruction, but the builtin is simpler.
Troubleshooting
- Compilation error: '__builtin___clear_cache' not declared. This happens if you're using an older compiler. Update GCC or use the syscall on ARM.
- Segmentation fault when flushing. Ensure the memory range is valid and accessible. You cannot flush memory that is not mapped.
- Cache flush has no effect. Check that you're using the correct address range and that you haven't optimized away the writes. Use
volatileor compiler barriers. - Performance degradation. Flushing too often or too much memory can slow down your program. Profile and flush only when necessary.
- On ARM, syscall returns -1. Check errno. It might be ENOSYS if the kernel doesn't support it, or EFAULT if the address range is invalid.
Frequently Asked Questions
Does flushing the CPU cache guarantee that other processors see the data?
No. Flushing the cache only writes the data back to the memory hierarchy. For multi-core systems, you also need a memory barrier and proper synchronization (like locks or atomics) to ensure visibility across cores.
Is there a way to flush the entire cache from user space?
Not portably. On x86, you can use clflush on every cache line, but that's impractical. On ARM, the cacheflush syscall can flush a range, but not the whole cache without knowing all memory ranges.
What is the difference between __builtin___clear_cache and cacheflush syscall?
The builtin is a compiler intrinsic that works on both x86 and ARM, and it's more portable. The syscall is Linux-specific and mainly used on ARM. The builtin may generate the appropriate instructions for the architecture.
Can I flush the cache on a specific memory address?
Yes, both methods allow flushing a specific range. Use the start and end addresses of the range you want to flush.
Do I need to flush the cache after every write?
Usually not. Only flush when you need to ensure that the data is visible to other processors or devices, such as in inter-process communication or before DMA transfers.
Is there a performance cost to flushing the cache?
Yes, flushing the cache forces a write-back to main memory, which is slower than leaving data in cache. Overusing it can significantly degrade performance.