alistair23-linux/arch
Andrea Parri 0123f4d76c
riscv/spinlock: Strengthen implementations with fences
Current implementations map locking operations using .rl and .aq
annotations.  However, this mapping is unsound w.r.t. the kernel
memory consistency model (LKMM) [1]:

Referring to the "unlock-lock-read-ordering" test reported below,
Daniel wrote:

  "I think an RCpc interpretation of .aq and .rl would in fact
   allow the two normal loads in P1 to be reordered [...]

   The intuition would be that the amoswap.w.aq can forward from
   the amoswap.w.rl while that's still in the store buffer, and
   then the lw x3,0(x4) can also perform while the amoswap.w.rl
   is still in the store buffer, all before the l1 x1,0(x2)
   executes.  That's not forbidden unless the amoswaps are RCsc,
   unless I'm missing something.

   Likewise even if the unlock()/lock() is between two stores.
   A control dependency might originate from the load part of
   the amoswap.w.aq, but there still would have to be something
   to ensure that this load part in fact performs after the store
   part of the amoswap.w.rl performs globally, and that's not
   automatic under RCpc."

Simulation of the RISC-V memory consistency model confirmed this
expectation.

In order to "synchronize" LKMM and RISC-V's implementation, this
commit strengthens the implementations of the locking operations
by replacing .rl and .aq with the use of ("lightweigth") fences,
resp., "fence rw,  w" and "fence r , rw".

C unlock-lock-read-ordering

{}
/* s initially owned by P1 */

P0(int *x, int *y)
{
        WRITE_ONCE(*x, 1);
        smp_wmb();
        WRITE_ONCE(*y, 1);
}

P1(int *x, int *y, spinlock_t *s)
{
        int r0;
        int r1;

        r0 = READ_ONCE(*y);
        spin_unlock(s);
        spin_lock(s);
        r1 = READ_ONCE(*x);
}

exists (1:r0=1 /\ 1:r1=0)

[1] https://marc.info/?l=linux-kernel&m=151930201102853&w=2
    https://groups.google.com/a/groups.riscv.org/forum/#!topic/isa-dev/hKywNHBkAXM
    https://marc.info/?l=linux-kernel&m=151633436614259&w=2

Signed-off-by: Andrea Parri <parri.andrea@gmail.com>
Cc: Palmer Dabbelt <palmer@sifive.com>
Cc: Albert Ou <albert@sifive.com>
Cc: Daniel Lustig <dlustig@nvidia.com>
Cc: Alan Stern <stern@rowland.harvard.edu>
Cc: Will Deacon <will.deacon@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: Nicholas Piggin <npiggin@gmail.com>
Cc: David Howells <dhowells@redhat.com>
Cc: Jade Alglave <j.alglave@ucl.ac.uk>
Cc: Luc Maranget <luc.maranget@inria.fr>
Cc: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
Cc: Akira Yokosawa <akiyks@gmail.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: linux-riscv@lists.infradead.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Palmer Dabbelt <palmer@sifive.com>
2018-04-02 19:59:43 -07:00
..
alpha locking/xchg/alpha: Fix xchg() and cmpxchg() memory ordering bugs 2018-02-23 08:38:16 +01:00
arc ARC fixes for 4.16-rc4 2018-03-01 14:32:23 -08:00
arm ARM: SoC fixes for 4.16 2018-03-28 13:52:13 -10:00
arm64 ARM: SoC fixes for 4.16 2018-03-28 13:52:13 -10:00
blackfin
c6x
cris
frv
h8300 h8300: remove extraneous __BIG_ENDIAN definition 2018-03-22 17:07:01 -07:00
hexagon
ia64 ia64/err-inject: fix spelling mistake: "capapbilities" -> "capabilities" 2018-03-05 14:44:53 -08:00
m32r
m68k
metag
microblaze microblaze: switch to NO_BOOTMEM 2018-03-16 12:51:27 +01:00
mips MIPS: ralink: Fix booting on MT7621 2018-03-22 00:06:30 +00:00
mn10300
nios2
openrisc
parisc parisc: Handle case where flush_cache_range is called with no context 2018-03-17 11:49:39 +01:00
powerpc KVM fixes for v4.16-rc8 2018-03-30 07:24:14 -10:00
riscv riscv/spinlock: Strengthen implementations with fences 2018-04-02 19:59:43 -07:00
s390 KVM fixes for v4.16-rc5 2018-03-09 16:59:19 -08:00
score
sh sh: fix build error for empty CONFIG_BUILTIN_DTB_SOURCE 2018-03-02 09:20:55 +09:00
sparc sparc64: Fix regression in pmdp_invalidate(). 2018-03-15 14:18:00 -07:00
tile
um
unicore32
x86 Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip 2018-03-31 07:59:00 -10:00
xtensa
.gitignore
Kconfig