I'm trying to understand in a general sense how L1/L2 (and now L3 caches) are updated and how the updates are propagated in a multi-core x86/x86-64 CPU.

Assuming a 4 core CPU and 2 pairs of L1/L2 caches, where pairs of cores share a common L1/L2 pair and there's an interconnect between the 2 L1/L2 pairs. And the cache lines are 64-bit wide.
So we have:

Core-0/Core-1 on (L1/L2)-0

Core-2/Core-3 on (L1/L2)-1

(L1/L2)-0 is connected to (L1/L2)-1

Let's say there is a thread T0 running on Core-0 that is writing to a 64-bit integer variable called X, and there's another thread T1 on Core-3 continually reading variable X's value - Please ignore the logical race conditions for a moment.

Question:
Assuming X has been cached in Core-0's L1. When T0 writes a new value to X, Is the following sequence of events correct?

In the Wikipedia article for Cache coherency (which is what this question is about, and which is typically way beyond the typical CS/CE/ECE undergraduate education), a number of protocols (MSI, MESI, MOESI...) are listed. OP's question requires one to state explicitly which protocol the answer is applicable for. Also, different vendors implementing the same architecture could choose different protocol. Sometimes desktop, mainframe and mobile devices could choose different protocols as well.
– rwongJan 4 '15 at 4:34

1 Answer
1

First if you are concerned about recent (last 5-10 years, since Nahalem?) Intel x86 architecture, then you're a little off in your description of the caches.

Each core has their own 128K L1 cache split (64K data / 64K code). Above that, each core has its own L2 cache which basically acts as a buffer between the L1 and L3 cache. Each socket has its own L3 cache (up to 20MB, I think). The L1 and L2 caches are small, simple, and quick. The L3 cache is much larger and more complex (it is divided into small segments that all sit on a ring with other things like the memory controller and QPI bridge to other sockets). (Please ignore things like the load and store buffers that will make this even more complex.)

Also, cache lines are 64-bytes.

So it looks like this (on for example a dual socket machine, with 4 cores per CPU):

Pre Nahalem, L2 caches were shared among core pairs. I haven't had to do a lot of performance work on this in a while, so I'm not really sure of the subtleties there.

The L3 cache is fully inclusive of the L1 and L2 caches below it. The cache contains the "correct" values for all memory addresses.

More correct than main memory, since writes can sit in L3 for a while before going to memory (write-back caching). All caches are coherent. That is, you will never have two different values for the same memory location. This coherency is maintained by a version of the MESI protocol, called MESIF (for Intel, AMD has a different caching strategy and uses MEOSI and arranged their caching differently).

Since the L1 and L2 are private to the core, the coherence only has to be managed at the L3 level (I think, I've been unable to get a definite answer on this). The cache interconnects have four lanes: data, request, acknowledge, and snoop (to keep up to date on other memory operations).

Now, we can get down to your questions.

If a thread on Core 0 is reading an address, the address will reside in L1-0 and L3-0 in either the Exclusive, Shared, or Forward state (all three show that the address is unmodified and cached). Now, Core-4 wants to write to it. A Request/Read-For-Ownership will send the cache line from the other L3 cache (L3-0) and cause the other caches to mark their copies as Invalid. It will now be in L1-4 and L3-1 (marked as Exclusive).

(Here is where ignoring store buffers simplified a lot.)

Core-4 will write from a register to the L1-4 cache. Causing to transition the line to the Modified state. This gets propagated to the L3-1 cache (since it is fully inclusive).

Now, Core-0 wants to read again. The L1-0 cache is Invalid at that address, so it sends a read request that misses the L3-0 cache and causes the L3-1 to send the cache line back across. The L3-1 now states that address is Shared, and the L3-0 holds the line as Forward (the most recent requester gets the Forwarding responsibility).

Clear as mud? There might be a few edits to this to clean up some of the language that I might be too vague on.