2
votes

My environment are x86, Linux and kernel space.

I would like to find out if there are any methods to flush the cache for a region of the memory WITHOUT invalidating the cache?

I have looked at clflush_cache_range(), but that method uses the clflushopt instruction which I believe will invalidate the cache.

1
What are you trying to do? Usually the cache is smart enough to manage itself. - SnakeDoc

1 Answers

2
votes

There are indeed some cases where you want to make sure the data is written back to memory in case the core crashes (reliability), or if you're working on transactions and you have some sort of versioned memory. In these cases you want to ensure the consistency and persistence of you data by syncing it to memory, but you don't want to incur the penalty of flushing the entire cache or even just the line you're working on, because you need to continue working with it.

In x86, for such cases you should check for CLWB (cache line WB) and PCOMMIT. Both were published very recently so they're probably not supported yet in most existing products (I've seen some possible references to Skylake, but nothing formal). Here's a nice summary including some performance analysis - http://danluu.com/clwb-pcommit/

CLWB:

CLWB acts like CLFLUSH, in that it forces the data to get written out to memory. However, it doesn’t force the cache to throw away the data, which makes future reads and writes a lot faster. Also, CLFLUSH is only ordered with respect to MFENCE, but CLWB is also ordered with respect to SFENCE.

And PCOMMIT:

PCOMMIT is applied to entire memory ranges and ensures that everything in the memory range is committed to persistent storage.

These 2 differ on the types of memory and storage they're working with, and some other subtleties like how they're ordered with respect to fencing or other stores. The full description is in the manuals.