The x86 instruction WBINVD
writes back and invalidates all caches. It is described as:
Writes back all modified cache lines in the processor’s internal cache to main memory and invalidates (flushes) the internal caches. The instruction then issues a special-function bus cycle that directs external caches to also write back modified data and another bus cycle to indicate that the external caches should be invalidated.
Importantly, the instruction can only be executed in ring0, i.e. the operating system. So your userland programs can t simply use it. On Linux, you can write a kernel module that can execute that instruction on demand. Actually, someone already wrote such a kernel module: https://github.com/batmac/wbinvd
Luckily, the kernel module s code is really tiny, so you can actually check it before loading code from strangers on the internet into your kernel. You can use that module (and trigger executing the WBINVD
instruction) by reading /proc/wbinvd
, for example via cat /proc/wbinvd
.
However, I found that this instruction (or at least this kernel module) is really slow. On my i7-6700HQ I measured it to take 750µs! This number seems really high to me, so I might have made a mistake measuring this -- please keep that in mind! Explanation of that instruction just say:
The amount of time or cycles for WBINVD to complete will vary due to size and other factors of different cache hierarchies.