The comparison between C11 and Linux is based upon the following equivalence of constructs:
Linux | C11 | |
Ordinary store | WRITE_ONCE | atomic store relaxed |
Ordinary load | READ_ONCE | atomic load relaxed |
Strong fence | smp_mb() | SC Fence |
Write to Write fence | smp_wmb() | Release Acquire Fence |
Read to Read fence | smp_rmb() | Release Acquire Fence |
Equating Linux concurrency aware memory accesses with (volatile) relaxed atomics sounds straightforward and prevents data races in the case of the C11 model. Similarly both models feature a strongest fence, which we equate.
The assimilation of C11 Release Acquire fence with both lighter fences (i.e. smp_wmb() and smp_rmb()) looks more arbitrary but remains a decent choice of assimilating all those fences to a “second to strongest” fence. The release-acquire fence of C11 is the “second to strongest” fence, as it must be stronger than both the release and acquire fences and lighter than the SC fence. Similarly, if we assimilate Linux two remaining fences as a unique fence, this hypothetical fence is the second strongest one.
The table below shows that it does not suffice to insert SC fences in-between all pairs of memory accesses by the same thread to forbid non-SC behaviours. The two litmus tests RWC and IRIW are worth noticing. The first test RWC s of minimal size (five memory accesses performed by 3 three threads). The second test IRIW is the canonical example of non-multi-copy atomicity, which C11 “SC” fence fails to annihilate.
C11 | Model | |
RWC+fencembs | Allow | Forbid |
WRR+2W+fencembs | Allow | Forbid |
WRW+2W+fencembs | Allow | Forbid |
WRW+WR+fencembs | Allow | Forbid |
W+RWC+fencemb+fencermb+fencemb | Allow | Forbid |
W+RWC+fencewmb+fencemb+fencemb | Allow | Forbid |
Z6.0+fencewmb+fencemb+fencemb | Allow | Forbid |
Z6.1+fencemb+fencewmb+fencemb | Allow | Forbid |
Z6.3+fencemb+fencemb+fencermb | Allow | Forbid |
Z6.3+fencemb+fencewmb+fencemb | Allow | Forbid |
IRIW+fencembs | Allow | Forbid |
IRRWIW+fencembs | Allow | Forbid |
IRWIW+fencembs | Allow | Forbid |