Introduction to Cache Coherency
Cache coherency refers to the mechanism that ensures data consistency across multiple caches in a system. When one core updates some data in its cache, other cores must either see that update or invalidate their own outdated copies. Without cache coherency, programs running on multi-core systems could produce unpredictable results, leading to bugs that are hard to debug.
How Cache Coherency Works in Systems
Modified: The cache line has been updated and differs from main memory; no other cache has a valid copy.
Owned: The cache line is modified and shared among multiple caches, with one cache acting as the owner.
Exclusive: The cache line matches main memory, and no other cache has it.
Shared: The cache line matches main memory and may exist in other caches.
Invalid: The cache line is outdated and cannot be used.
Cache Coherency in RISC-V
Memory Model: RISC-V defines a weak memory ordering model by default. For coherency, the architecture offers the Zifencei extension (fence instructions) to enforce ordering. The FENCE instruction ensures that memory operations before it complete before those after it, which is crucial for synchronizing cache updates across cores.
Atomic Instructions: The A extension (Atomic) adds instructions like LR (load-reserved) and SC (store-conditional) for atomic memory operations. These are essential for implementing locks and semaphores, which higher-level coherency mechanisms often rely on.
Cache Management Instructions: While the base ISA doesn’t include cache-specific instructions, the Zicbom (Cache Block Operations Management) extension, introduced in recent specifications, provides instructions like CBO.INVAL, CBO.CLEAN, and CBO.FLUSH. These allow software to manage cache lines explicitly—invalidating, cleaning, or flushing them as needed to maintain coherency in systems without hardware-managed coherence.
Invalidation – When a cache line is modified by one core in a multi-core system, the rest of the cores that have cached it should mark the address as invalid and force a load from the main memory.
True sharing – True sharing is a scenario in which the same address location in a cache line is accessed by multiple cores. This can be a write or read access. In these scenarios, the cache coherence mechanism must ensure that the modified data is propagated to the main memory and the stale copies of the cache are invalidated.
False sharing– False sharing is a scenario in which different address locations in the same cache line is accessed by multiple cores. This can cause performance degradation due to the cache line marked as invalid and the cores requiring to load the data from main memory even without a real necessity to do so.
Have questions or insights to share? Write to us at sales@vayavyalabs.com—we’d love to hear from you!