How to flush the CPU cache for a region of address space in Linux?

前端 未结 5 1005
你的背包
你的背包 2020-12-01 05:04

I am interested in flushing cache (L1, L2, and L3) only for a region of address space, for example all cache entries from address A to address B. Is there a mechanism to do

5条回答
  •  Happy的楠姐
    2020-12-01 05:28

    In x86 to flush the entire cache hierarchy you can use this

    native_wbinvd()
    

    Which is defined in arch/x86/include/asm/special_insns.h . If you look at its implementation, it simply calls the WBINVD instruction

    static inline void native_wbinvd(void)
    {
            asm volatile("wbinvd": : :"memory");
    }
    

    Note that you need to be in privileged mode to execute the WBINVD X86 instruction. This is contrast to the CLFLUSH x86 instruction which clears a single cacheline and doesnt need the caller to be in privileged mode.

    If you look at x86 Linux kernel code you will only see a handful (6 places when I write this) of this instruction. This is because it slows all entities running on that system. Imagine running this on a server with 100MB LLC. This instruction will mean moving the entire 100+ MB from cache to the RAM. Further it was brought to my notice that this instruction is non-interruptible. So its usage could significantly impact the determinism of a RT system for e.g.

    (Though the original question asks about how to clear a specific address range, I thought info on clearing the entire cache hierarchy would also be useful for some readers)

提交回复
热议问题