6e89e831a9
More than one kernel developer has expressed the opinion that the LKMM should enforce ordering of writes by locking. In other words, given the following code: WRITE_ONCE(x, 1); spin_unlock(&s): spin_lock(&s); WRITE_ONCE(y, 1); the stores to x and y should be propagated in order to all other CPUs, even though those other CPUs might not access the lock s. In terms of the memory model, this means expanding the cumul-fence relation. Locks should also provide read-read (and read-write) ordering in a similar way. Given: READ_ONCE(x); spin_unlock(&s); spin_lock(&s); READ_ONCE(y); // or WRITE_ONCE(y, 1); the load of x should be executed before the load of (or store to) y. The LKMM already provides this ordering, but it provides it even in the case where the two accesses are separated by a release/acquire pair of fences rather than unlock/lock. This would prevent architectures from using weakly ordered implementations of release and acquire, which seems like an unnecessary restriction. The patch therefore removes the ordering requirement from the LKMM for that case. There are several arguments both for and against this change. Let us refer to these enhanced ordering properties by saying that the LKMM would require locks to be RCtso (a bit of a misnomer, but analogous to RCpc and RCsc) and it would require ordinary acquire/release only to be RCpc. (Note: In the following, the phrase "all supported architectures" is meant not to include RISC-V. Although RISC-V is indeed supported by the kernel, the implementation is still somewhat in a state of flux and therefore statements about it would be premature.) Pros: The kernel already provides RCtso ordering for locks on all supported architectures, even though this is not stated explicitly anywhere. Therefore the LKMM should formalize it. In theory, guaranteeing RCtso ordering would reduce the need for additional barrier-like constructs meant to increase the ordering strength of locks. Will Deacon and Peter Zijlstra are strongly in favor of formalizing the RCtso requirement. Linus Torvalds and Will would like to go even further, requiring locks to have RCsc behavior (ordering preceding writes against later reads), but they recognize that this would incur a noticeable performance degradation on the POWER architecture. Linus also points out that people have made the mistake, in the past, of assuming that locking has stronger ordering properties than is currently guaranteed, and this change would reduce the likelihood of such mistakes. Not requiring ordinary acquire/release to be any stronger than RCpc may prove advantageous for future architectures, allowing them to implement smp_load_acquire() and smp_store_release() with more efficient machine instructions than would be possible if the operations had to be RCtso. Will and Linus approve this rationale, hypothetical though it is at the moment (it may end up affecting the RISC-V implementation). The same argument may or may not apply to RMW-acquire/release; see also the second Con entry below. Linus feels that locks should be easy for people to use without worrying about memory consistency issues, since they are so pervasive in the kernel, whereas acquire/release is much more of an "experts only" tool. Requiring locks to be RCtso is a step in this direction. Cons: Andrea Parri and Luc Maranget think that locks should have the same ordering properties as ordinary acquire/release (indeed, Luc points out that the names "acquire" and "release" derive from the usage of locks). Andrea points out that having different ordering properties for different forms of acquires and releases is not only unnecessary, it would also be confusing and unmaintainable. Locks are constructed from lower-level primitives, typically RMW-acquire (for locking) and ordinary release (for unlock). It is illogical to require stronger ordering properties from the high-level operations than from the low-level operations they comprise. Thus, this change would make while (cmpxchg_acquire(&s, 0, 1) != 0) cpu_relax(); an incorrect implementation of spin_lock(&s) as far as the LKMM is concerned. In theory this weakness can be ameliorated by changing the LKMM even further, requiring RMW-acquire/release also to be RCtso (which it already is on all supported architectures). As far as I know, nobody has singled out any examples of code in the kernel that actually relies on locks being RCtso. (People mumble about RCU and the scheduler, but nobody has pointed to any actual code. If there are any real cases, their number is likely quite small.) If RCtso ordering is not needed, why require it? A handful of locking constructs (qspinlocks, qrwlocks, and mcs_spinlocks) are built on top of smp_cond_load_acquire() instead of an RMW-acquire instruction. It currently provides only the ordinary acquire semantics, not the stronger ordering this patch would require of locks. In theory this could be ameliorated by requiring smp_cond_load_acquire() in combination with ordinary release also to be RCtso (which is currently true on all supported architectures). On future weakly ordered architectures, people may be able to implement locks in a non-RCtso fashion with significant performance improvement. Meeting the RCtso requirement would necessarily add run-time overhead. Overall, the technical aspects of these arguments seem relatively minor, and it appears mostly to boil down to a matter of opinion. Since the opinions of senior kernel maintainers such as Linus, Peter, and Will carry more weight than those of Luc and Andrea, this patch changes the model in accordance with the maintainers' wishes. Signed-off-by: Alan Stern <stern@rowland.harvard.edu> Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Reviewed-by: Will Deacon <will.deacon@arm.com> Reviewed-by: Andrea Parri <andrea.parri@amarulasolutions.com> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Stephane Eranian <eranian@google.com> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Vince Weaver <vincent.weaver@maine.edu> Cc: akiyks@gmail.com Cc: boqun.feng@gmail.com Cc: dhowells@redhat.com Cc: j.alglave@ucl.ac.uk Cc: linux-arch@vger.kernel.org Cc: luc.maranget@inria.fr Cc: npiggin@gmail.com Cc: parri.andrea@gmail.com Link: http://lkml.kernel.org/r/20180926182920.27644-2-paulmck@linux.ibm.com Signed-off-by: Ingo Molnar <mingo@kernel.org> |
||
---|---|---|
.. | ||
Documentation | ||
litmus-tests | ||
scripts | ||
linux-kernel.bell | ||
linux-kernel.cat | ||
linux-kernel.cfg | ||
linux-kernel.def | ||
lock.cat | ||
README |
===================================== LINUX KERNEL MEMORY CONSISTENCY MODEL ===================================== ============ INTRODUCTION ============ This directory contains the memory consistency model (memory model, for short) of the Linux kernel, written in the "cat" language and executable by the externally provided "herd7" simulator, which exhaustively explores the state space of small litmus tests. In addition, the "klitmus7" tool (also externally provided) may be used to convert a litmus test to a Linux kernel module, which in turn allows that litmus test to be exercised within the Linux kernel. ============ REQUIREMENTS ============ Version 7.49 of the "herd7" and "klitmus7" tools must be downloaded separately: https://github.com/herd/herdtools7 See "herdtools7/INSTALL.md" for installation instructions. ================== BASIC USAGE: HERD7 ================== The memory model is used, in conjunction with "herd7", to exhaustively explore the state space of small litmus tests. For example, to run SB+fencembonceonces.litmus against the memory model: $ herd7 -conf linux-kernel.cfg litmus-tests/SB+fencembonceonces.litmus Here is the corresponding output: Test SB+fencembonceonces Allowed States 3 0:r0=0; 1:r0=1; 0:r0=1; 1:r0=0; 0:r0=1; 1:r0=1; No Witnesses Positive: 0 Negative: 3 Condition exists (0:r0=0 /\ 1:r0=0) Observation SB+fencembonceonces Never 0 3 Time SB+fencembonceonces 0.01 Hash=d66d99523e2cac6b06e66f4c995ebb48 The "Positive: 0 Negative: 3" and the "Never 0 3" each indicate that this litmus test's "exists" clause can not be satisfied. See "herd7 -help" or "herdtools7/doc/" for more information. ===================== BASIC USAGE: KLITMUS7 ===================== The "klitmus7" tool converts a litmus test into a Linux kernel module, which may then be loaded and run. For example, to run SB+fencembonceonces.litmus against hardware: $ mkdir mymodules $ klitmus7 -o mymodules litmus-tests/SB+fencembonceonces.litmus $ cd mymodules ; make $ sudo sh run.sh The corresponding output includes: Test SB+fencembonceonces Allowed Histogram (3 states) 644580 :>0:r0=1; 1:r0=0; 644328 :>0:r0=0; 1:r0=1; 711092 :>0:r0=1; 1:r0=1; No Witnesses Positive: 0, Negative: 2000000 Condition exists (0:r0=0 /\ 1:r0=0) is NOT validated Hash=d66d99523e2cac6b06e66f4c995ebb48 Observation SB+fencembonceonces Never 0 2000000 Time SB+fencembonceonces 0.16 The "Positive: 0 Negative: 2000000" and the "Never 0 2000000" indicate that during two million trials, the state specified in this litmus test's "exists" clause was not reached. And, as with "herd7", please see "klitmus7 -help" or "herdtools7/doc/" for more information. ==================== DESCRIPTION OF FILES ==================== Documentation/cheatsheet.txt Quick-reference guide to the Linux-kernel memory model. Documentation/explanation.txt Describes the memory model in detail. Documentation/recipes.txt Lists common memory-ordering patterns. Documentation/references.txt Provides background reading. linux-kernel.bell Categorizes the relevant instructions, including memory references, memory barriers, atomic read-modify-write operations, lock acquisition/release, and RCU operations. More formally, this file (1) lists the subtypes of the various event types used by the memory model and (2) performs RCU read-side critical section nesting analysis. linux-kernel.cat Specifies what reorderings are forbidden by memory references, memory barriers, atomic read-modify-write operations, and RCU. More formally, this file specifies what executions are forbidden by the memory model. Allowed executions are those which satisfy the model's "coherence", "atomic", "happens-before", "propagation", and "rcu" axioms, which are defined in the file. linux-kernel.cfg Convenience file that gathers the common-case herd7 command-line arguments. linux-kernel.def Maps from C-like syntax to herd7's internal litmus-test instruction-set architecture. litmus-tests Directory containing a few representative litmus tests, which are listed in litmus-tests/README. A great deal more litmus tests are available at https://github.com/paulmckrcu/litmus. lock.cat Provides a front-end analysis of lock acquisition and release, for example, associating a lock acquisition with the preceding and following releases and checking for self-deadlock. More formally, this file defines a performance-enhanced scheme for generation of the possible reads-from and coherence order relations on the locking primitives. README This file. =========== LIMITATIONS =========== The Linux-kernel memory model has the following limitations: 1. Compiler optimizations are not modeled. Of course, the use of READ_ONCE() and WRITE_ONCE() limits the compiler's ability to optimize, but there is Linux-kernel code that uses bare C memory accesses. Handling this code is on the to-do list. For more information, see Documentation/explanation.txt (in particular, the "THE PROGRAM ORDER RELATION: po AND po-loc" and "A WARNING" sections). 2. Multiple access sizes for a single variable are not supported, and neither are misaligned or partially overlapping accesses. 3. Exceptions and interrupts are not modeled. In some cases, this limitation can be overcome by modeling the interrupt or exception with an additional process. 4. I/O such as MMIO or DMA is not supported. 5. Self-modifying code (such as that found in the kernel's alternatives mechanism, function tracer, Berkeley Packet Filter JIT compiler, and module loader) is not supported. 6. Complete modeling of all variants of atomic read-modify-write operations, locking primitives, and RCU is not provided. For example, call_rcu() and rcu_barrier() are not supported. However, a substantial amount of support is provided for these operations, as shown in the linux-kernel.def file. The "herd7" tool has some additional limitations of its own, apart from the memory model: 1. Non-trivial data structures such as arrays or structures are not supported. However, pointers are supported, allowing trivial linked lists to be constructed. 2. Dynamic memory allocation is not supported, although this can be worked around in some cases by supplying multiple statically allocated variables. Some of these limitations may be overcome in the future, but others are more likely to be addressed by incorporating the Linux-kernel memory model into other tools.