1- Explanation of the Linux-Kernel Memory Model
2- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1+ Explanation of the Linux-Kernel Memory Consistency Model
2+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33
44:Author: Alan Stern <stern@rowland.harvard.edu>
55:Created: October 2017
@@ -35,25 +35,24 @@ Explanation of the Linux-Kernel Memory Model
3535INTRODUCTION
3636------------
3737
38- The Linux-kernel memory model (LKMM) is rather complex and obscure.
39- This is particularly evident if you read through the linux-kernel.bell
40- and linux-kernel.cat files that make up the formal version of the
41- memory model; they are extremely terse and their meanings are far from
42- clear.
38+ The Linux-kernel memory consistency model (LKMM) is rather complex and
39+ obscure. This is particularly evident if you read through the
40+ linux-kernel.bell and linux-kernel.cat files that make up the formal
41+ version of the model; they are extremely terse and their meanings are
42+ far from clear.
4343
4444This document describes the ideas underlying the LKMM. It is meant
45- for people who want to understand how the memory model was designed.
46- It does not go into the details of the code in the .bell and .cat
47- files; rather, it explains in English what the code expresses
48- symbolically.
45+ for people who want to understand how the model was designed. It does
46+ not go into the details of the code in the .bell and .cat files;
47+ rather, it explains in English what the code expresses symbolically.
4948
5049Sections 2 (BACKGROUND) through 5 (ORDERING AND CYCLES) are aimed
51- toward beginners; they explain what memory models are and the basic
52- notions shared by all such models. People already familiar with these
53- concepts can skim or skip over them. Sections 6 (EVENTS) through 12
54- (THE FROM_READS RELATION) describe the fundamental relations used in
55- many memory models. Starting in Section 13 (AN OPERATIONAL MODEL),
56- the workings of the LKMM itself are covered.
50+ toward beginners; they explain what memory consistency models are and
51+ the basic notions shared by all such models. People already familiar
52+ with these concepts can skim or skip over them. Sections 6 (EVENTS)
53+ through 12 (THE FROM_READS RELATION) describe the fundamental
54+ relations used in many models. Starting in Section 13 (AN OPERATIONAL
55+ MODEL), the workings of the LKMM itself are covered.
5756
5857Warning: The code examples in this document are not written in the
5958proper format for litmus tests. They don't include a header line, the
@@ -827,8 +826,8 @@ A-cumulative; they only affect the propagation of stores that are
827826executed on C before the fence (i.e., those which precede the fence in
828827program order).
829828
830- smp_read_barrier_depends (), rcu_read_lock (), rcu_read_unlock(), and
831- synchronize_rcu() fences have other properties which we discuss later.
829+ read_lock (), rcu_read_unlock (), and synchronize_rcu() fences have
830+ other properties which we discuss later.
832831
833832
834833PROPAGATION ORDER RELATION: cumul-fence
@@ -988,8 +987,8 @@ Another possibility, not mentioned earlier but discussed in the next
988987section, is:
989988
990989 X and Y are both loads, X ->addr Y (i.e., there is an address
991- dependency from X to Y), and an smp_read_barrier_depends()
992- fence occurs between them .
990+ dependency from X to Y), and X is a READ_ONCE() or an atomic
991+ access .
993992
994993Dependencies can also cause instructions to be executed in program
995994order. This is uncontroversial when the second instruction is a
@@ -1015,9 +1014,9 @@ After all, a CPU cannot ask the memory subsystem to load a value from
10151014a particular location before it knows what that location is. However,
10161015the split-cache design used by Alpha can cause it to behave in a way
10171016that looks as if the loads were executed out of order (see the next
1018- section for more details). For this reason, the LKMM does not include
1019- address dependencies between read events in the ppo relation unless an
1020- smp_read_barrier_depends() fence is present .
1017+ section for more details). The kernel includes a workaround for this
1018+ problem when the loads come from READ_ONCE(), and therefore the LKMM
1019+ includes address dependencies to loads in the ppo relation .
10211020
10221021On the other hand, dependencies can indirectly affect the ordering of
10231022two loads. This happens when there is a dependency from a load to a
@@ -1114,11 +1113,12 @@ code such as the following:
11141113 int *r1;
11151114 int r2;
11161115
1117- r1 = READ_ONCE( ptr) ;
1116+ r1 = ptr;
11181117 r2 = READ_ONCE(*r1);
11191118 }
11201119
1121- can malfunction on Alpha systems. It is quite possible that r1 = &x
1120+ can malfunction on Alpha systems (notice that P1 uses an ordinary load
1121+ to read ptr instead of READ_ONCE()). It is quite possible that r1 = &x
11221122and r2 = 0 at the end, in spite of the address dependency.
11231123
11241124At first glance this doesn't seem to make sense. We know that the
@@ -1141,25 +1141,31 @@ This could not have happened if the local cache had processed the
11411141incoming stores in FIFO order. In constrast, other architectures
11421142maintain at least the appearance of FIFO order.
11431143
1144- In practice, this difficulty is solved by inserting an
1145- smp_read_barrier_depends() fence between P1's two loads. The effect
1146- of this fence is to cause the CPU not to execute any po-later
1147- instructions until after the local cache has finished processing all
1148- the stores it has already received. Thus, if the code was changed to:
1144+ In practice, this difficulty is solved by inserting a special fence
1145+ between P1's two loads when the kernel is compiled for the Alpha
1146+ architecture. In fact, as of version 4.15, the kernel automatically
1147+ adds this fence (called smp_read_barrier_depends() and defined as
1148+ nothing at all on non-Alpha builds) after every READ_ONCE() and atomic
1149+ load. The effect of the fence is to cause the CPU not to execute any
1150+ po-later instructions until after the local cache has finished
1151+ processing all the stores it has already received. Thus, if the code
1152+ was changed to:
11491153
11501154 P1()
11511155 {
11521156 int *r1;
11531157 int r2;
11541158
11551159 r1 = READ_ONCE(ptr);
1156- smp_read_barrier_depends();
11571160 r2 = READ_ONCE(*r1);
11581161 }
11591162
11601163then we would never get r1 = &x and r2 = 0. By the time P1 executed
11611164its second load, the x = 1 store would already be fully processed by
1162- the local cache and available for satisfying the read request.
1165+ the local cache and available for satisfying the read request. Thus
1166+ we have yet another reason why shared data should always be read with
1167+ READ_ONCE() or another synchronization primitive rather than accessed
1168+ directly.
11631169
11641170The LKMM requires that smp_rmb(), acquire fences, and strong fences
11651171share this property with smp_read_barrier_depends(): They do not allow
@@ -1751,11 +1757,10 @@ no further involvement from the CPU. Since the CPU doesn't ever read
17511757the value of x, there is nothing for the smp_rmb() fence to act on.
17521758
17531759The LKMM defines a few extra synchronization operations in terms of
1754- things we have already covered. In particular, rcu_dereference() and
1755- lockless_dereference() are both treated as a READ_ONCE() followed by
1756- smp_read_barrier_depends() -- which also happens to be how they are
1757- defined in include/linux/rcupdate.h and include/linux/compiler.h,
1758- respectively.
1760+ things we have already covered. In particular, rcu_dereference() is
1761+ treated as READ_ONCE() and rcu_assign_pointer() is treated as
1762+ smp_store_release() -- which is basically how the Linux kernel treats
1763+ them.
17591764
17601765There are a few oddball fences which need special treatment:
17611766smp_mb__before_atomic(), smp_mb__after_atomic(), and
0 commit comments