Skip to content

Commit 15553dc

Browse files
marangetingomolnar
authored andcommitted
tools/memory-model: Add model support for spin_is_locked()
This commit first adds a trivial macro for spin_is_locked() to linux-kernel.def. It also adds cat code for enumerating all possible matches of lock write events (set LKW) with islocked events returning true (set RL, for Read from Lock), and unlock write events (set UL) with islocked events returning false (set RU, for Read from Unlock). Note that this intentionally does not model uniprocessor kernels (CONFIG_SMP=n) built with CONFIG_DEBUG_SPINLOCK=n, in which spin_is_locked() unconditionally returns zero. It also adds a pair of litmus tests demonstrating the minimal ordering provided by spin_is_locked() in conjunction with spin_lock(). Will Deacon noted that this minimal ordering happens on ARMv8: https://lkml.kernel.org/r/20180226162426.GB17158@arm.com Notice that herd7 installations strictly older than version 7.49 do not handle the new constructs. Signed-off-by: Luc Maranget <luc.maranget@inria.fr> Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Reviewed-by: Alan Stern <stern@rowland.harvard.edu> Cc: Akira Yokosawa <akiyks@gmail.com> Cc: Andrea Parri <parri.andrea@gmail.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Boqun Feng <boqun.feng@gmail.com> Cc: David Howells <dhowells@redhat.com> Cc: Jade Alglave <j.alglave@ucl.ac.uk> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Luc Maranget <Luc.Maranget@inria.fr> Cc: Nicholas Piggin <npiggin@gmail.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Will Deacon <will.deacon@arm.com> Cc: linux-arch@vger.kernel.org Link: http://lkml.kernel.org/r/1526340837-12222-10-git-send-email-paulmck@linux.vnet.ibm.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 2fb6ae1 commit 15553dc

5 files changed

Lines changed: 129 additions & 4 deletions

File tree

‎tools/memory-model/linux-kernel.def‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ cmpxchg_release(X,V,W) __cmpxchg{release}(X,V,W)
3838
spin_lock(X) { __lock(X); }
3939
spin_unlock(X) { __unlock(X); }
4040
spin_trylock(X) __trylock(X)
41+
spin_is_locked(X) __islocked(X)
4142

4243
// RCU
4344
rcu_read_lock() { __fence{rcu-lock}; }
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
C MP+polockmbonce+poacquiresilsil
2+
3+
(*
4+
* Result: Never
5+
*
6+
* Do spinlocks combined with smp_mb__after_spinlock() provide order
7+
* to outside observers using spin_is_locked() to sense the lock-held
8+
* state, ordered by acquire? Note that when the first spin_is_locked()
9+
* returns false and the second true, we know that the smp_load_acquire()
10+
* executed before the lock was acquired (loosely speaking).
11+
*)
12+
13+
{
14+
}
15+
16+
P0(spinlock_t *lo, int *x)
17+
{
18+
spin_lock(lo);
19+
smp_mb__after_spinlock();
20+
WRITE_ONCE(*x, 1);
21+
spin_unlock(lo);
22+
}
23+
24+
P1(spinlock_t *lo, int *x)
25+
{
26+
int r1;
27+
int r2;
28+
int r3;
29+
30+
r1 = smp_load_acquire(x);
31+
r2 = spin_is_locked(lo);
32+
r3 = spin_is_locked(lo);
33+
}
34+
35+
exists (1:r1=1 /\ 1:r2=0 /\ 1:r3=1)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
C MP+polockonce+poacquiresilsil
2+
3+
(*
4+
* Result: Sometimes
5+
*
6+
* Do spinlocks provide order to outside observers using spin_is_locked()
7+
* to sense the lock-held state, ordered by acquire? Note that when the
8+
* first spin_is_locked() returns false and the second true, we know that
9+
* the smp_load_acquire() executed before the lock was acquired (loosely
10+
* speaking).
11+
*)
12+
13+
{
14+
}
15+
16+
P0(spinlock_t *lo, int *x)
17+
{
18+
spin_lock(lo);
19+
WRITE_ONCE(*x, 1);
20+
spin_unlock(lo);
21+
}
22+
23+
P1(spinlock_t *lo, int *x)
24+
{
25+
int r1;
26+
int r2;
27+
int r3;
28+
29+
r1 = smp_load_acquire(x);
30+
r2 = spin_is_locked(lo);
31+
r3 = spin_is_locked(lo);
32+
}
33+
34+
exists (1:r1=1 /\ 1:r2=0 /\ 1:r3=1)

‎tools/memory-model/litmus-tests/README‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ LB+poonceonces.litmus
6363
MP+onceassign+derefonce.litmus
6464
As below, but with rcu_assign_pointer() and an rcu_dereference().
6565

66+
MP+polockmbonce+poacquiresilsil.litmus
67+
Protect the access with a lock and an smp_mb__after_spinlock()
68+
in one process, and use an acquire load followed by a pair of
69+
spin_is_locked() calls in the other process.
70+
71+
MP+polockonce+poacquiresilsil.litmus
72+
Protect the access with a lock in one process, and use an
73+
acquire load followed by a pair of spin_is_locked() calls
74+
in the other process.
75+
6676
MP+polocks.litmus
6777
As below, but with the second access of the writer process
6878
and the first access of reader process protected by a lock.

‎tools/memory-model/lock.cat‎

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
*)
66

77
(* Generate coherence orders and handle lock operations *)
8-
8+
(*
9+
* Warning, crashes with herd7 versions strictly before 7.48.
10+
* spin_islocked is functional from version 7.49.
11+
*
12+
*)
913
include "cross.cat"
1014

1115
(* From lock reads to their partner lock writes *)
@@ -32,12 +36,16 @@ flag ~empty [M \ IW] ; loc ; [ALL-LOCKS] as mixed-lock-accesses
3236
(* The final value of a spinlock should not be tested *)
3337
flag ~empty [FW] ; loc ; [ALL-LOCKS] as lock-final
3438

35-
39+
(*
40+
* Backward compatibility
41+
*)
42+
let RL = try RL with emptyset (* defined herd7 >= 7.49 *)
43+
let RU = try RU with emptyset (* defined herd7 >= 7.49 *)
3644
(*
3745
* Put lock operations in their appropriate classes, but leave UL out of W
3846
* until after the co relation has been generated.
3947
*)
40-
let R = R | LKR | LF
48+
let R = R | LKR | LF | RL | RU
4149
let W = W | LKW
4250

4351
let Release = Release | UL
@@ -72,8 +80,45 @@ let all-possible-rfe-lf =
7280

7381
(* Generate all rf relations for LF events *)
7482
with rfe-lf from cross(all-possible-rfe-lf)
75-
let rf = rf | rfi-lf | rfe-lf
7683

84+
let rf-lf = rfe-lf | rfi-lf
85+
86+
(* rf for RL events, ie islocked returning true, similar to LF above *)
87+
88+
(* islocked returning true inside a critical section
89+
* must read from the opening lock
90+
*)
91+
let rfi-rl = ([LKW] ; po-loc ; [RL]) \ ([LKW] ; po-loc ; [UL] ; po-loc)
92+
93+
(* islocked returning true outside critical sections can match any
94+
* external lock.
95+
*)
96+
let all-possible-rfe-rl =
97+
let possible-rfe-lf r =
98+
let pair-to-relation p = p ++ 0
99+
in map pair-to-relation ((LKW * {r}) & loc & ext)
100+
in map possible-rfe-lf (RL \ range(rfi-rl))
101+
102+
with rfe-rl from cross(all-possible-rfe-rl)
103+
let rf-rl = rfe-rl | rfi-rl
104+
105+
(* Read from unlock, ie islocked returning false, slightly different *)
106+
107+
(* islocked returning false can read from the last po-previous unlock *)
108+
let rfi-ru = ([UL] ; po-loc ; [RU]) \ ([UL] ; po-loc ; [LKW] ; po-loc)
109+
110+
(* any islocked returning false can read from any external unlock *)
111+
let all-possible-rfe-ru =
112+
let possible-rfe-ru r =
113+
let pair-to-relation p = p ++ 0
114+
in map pair-to-relation (((UL|IW) * {r}) & loc & ext)
115+
in map possible-rfe-ru RU
116+
117+
with rfe-ru from cross(all-possible-rfe-ru)
118+
let rf-ru = rfe-ru | rfi-ru
119+
120+
(* Final rf relation *)
121+
let rf = rf | rf-lf | rf-rl | rf-ru
77122

78123
(* Generate all co relations, including LKW events but not UL *)
79124
let co0 = co0 | ([IW] ; loc ; [LKW]) |

0 commit comments

Comments
 (0)