# Litmus tests

---

# Litmus tests

_Peios Kernel / Kernel development / Litmus tests_

> Kernel reference: Litmus tests.

---

Each subdirectory contains litmus tests that are typical to describe the
semantics of respective kernel APIs.
For more information about how to "run" a litmus test or how to generate
a kernel test module based on a litmus test, please see
tools/memory-model/README.

## atomic (/atomic directory)

Atomic-RMW+mb\_\_after\_atomic-is-stronger-than-acquire.litmus

```text
    Test that an atomic RMW followed by a smp_mb__after_atomic() is
    stronger than a normal acquire: both the read and write parts of
    the RMW are ordered before the subsequential memory accesses.
```

Atomic-RMW-ops-are-atomic-WRT-atomic\_set.litmus

```text
    Test that atomic_set() cannot break the atomicity of atomic RMWs.
    NOTE: Require herd7 7.56 or later which supports "(void)expr".
```

cmpxchg-fail-ordered-1.litmus

```text
    Demonstrate that a failing cmpxchg() operation acts as a full barrier
    when followed by smp_mb__after_atomic().
```

cmpxchg-fail-ordered-2.litmus

```text
    Demonstrate that a failing cmpxchg() operation acts as an acquire
    operation when followed by smp_mb__after_atomic().
```

cmpxchg-fail-unordered-1.litmus

```text
    Demonstrate that a failing cmpxchg() operation does not act as a
    full barrier.
```

cmpxchg-fail-unordered-2.litmus

```text
    Demonstrate that a failing cmpxchg() operation does not act as an
    acquire operation.
```

## locking (/locking directory)

DCL-broken.litmus

```text
    Demonstrates that double-checked locking needs more than just
    the obvious lock acquisitions and releases.
```

DCL-fixed.litmus

```text
    Demonstrates corrected double-checked locking that uses
    smp_store_release() and smp_load_acquire() in addition to the
    obvious lock acquisitions and releases.
```

RM-broken.litmus

```text
    Demonstrates problems with "roach motel" locking, where code is
    freely moved into lock-based critical sections.  This example also
    shows how to use the "filter" clause to discard executions that
    would be excluded by other code not modeled in the litmus test.
    Note also that this "roach motel" optimization is emulated by
    physically moving P1()'s two reads from x under the lock.
```

```text
    What is a roach motel?  This is from an old advertisement for
    a cockroach trap, much later featured in one of the "Men in
    Black" movies.  "The roaches check in.  They don't check out."
```

RM-fixed.litmus

```text
    The counterpart to RM-broken.litmus, showing P0()'s two loads from
    x safely outside of the critical section.
```

## RCU (/rcu directory)

MP+onceassign+derefonce.litmus (under tools/memory-model/litmus-tests/)

```text
    Demonstrates the use of rcu_assign_pointer() and rcu_dereference() to
    ensure that an RCU reader will not see pre-initialization garbage.
```

RCU+sync+read.litmus
RCU+sync+free.litmus

```text
    Both the above litmus tests demonstrate the RCU grace period guarantee
    that an RCU read-side critical section can never span a grace period.
```

---

# Atomic-RMW+mb__after_atomic-is-stronger-than-acquire litmus test

_Peios Kernel / Kernel development / Litmus tests / atomic_

> Source and expected outcome of this Linux memory-model litmus test.

Source and expected outcome of this Linux memory-model litmus test.


```text
C Atomic-RMW+mb__after_atomic-is-stronger-than-acquire

(*
 * Result: Never
 *
 * Test that an atomic RMW followed by a smp_mb__after_atomic() is
 * stronger than a normal acquire: both the read and write parts of
 * the RMW are ordered before the subsequential memory accesses.
 *)

{
}

P0(int *x, atomic_t *y)
{
	int r0;
	int r1;

	r0 = READ_ONCE(*x);
	smp_rmb();
	r1 = atomic_read(y);
}

P1(int *x, atomic_t *y)
{
	atomic_inc(y);
	smp_mb__after_atomic();
	WRITE_ONCE(*x, 1);
}

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

```

---

# Atomic-RMW-ops-are-atomic-WRT-atomic_set litmus test

_Peios Kernel / Kernel development / Litmus tests / atomic_

> Source and expected outcome of this Linux memory-model litmus test.

Source and expected outcome of this Linux memory-model litmus test.


```text
C Atomic-RMW-ops-are-atomic-WRT-atomic_set

(*
 * Result: Never
 *
 * Test that atomic_set() cannot break the atomicity of atomic RMWs.
 * NOTE: This requires herd7 7.56 or later which supports "(void)expr".
 *)

{
	atomic_t v = ATOMIC_INIT(1);
}

P0(atomic_t *v)
{
	(void)atomic_add_unless(v, 1, 0);
}

P1(atomic_t *v)
{
	atomic_set(v, 0);
}

exists
(v=2)

```

---

# cmpxchg-fail-ordered-1 litmus test

_Peios Kernel / Kernel development / Litmus tests / atomic_

> Source and expected outcome of this Linux memory-model litmus test.

Source and expected outcome of this Linux memory-model litmus test.


```text
C cmpxchg-fail-ordered-1

(*
 * Result: Never
 *
 * Demonstrate that a failing cmpxchg() operation will act as a full
 * barrier when followed by smp_mb__after_atomic().
 *)

{}

P0(int *x, int *y, int *z)
{
	int r0;
	int r1;

	WRITE_ONCE(*x, 1);
	r1 = cmpxchg(z, 1, 0);
	smp_mb__after_atomic();
	r0 = READ_ONCE(*y);
}

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

	WRITE_ONCE(*y, 1);
	r1 = cmpxchg(z, 1, 0);
	smp_mb__after_atomic();
	r0 = READ_ONCE(*x);
}

locations[0:r1;1:r1]
exists (0:r0=0 /\ 1:r0=0)

```

---

# cmpxchg-fail-ordered-2 litmus test

_Peios Kernel / Kernel development / Litmus tests / atomic_

> Source and expected outcome of this Linux memory-model litmus test.

Source and expected outcome of this Linux memory-model litmus test.


```text
C cmpxchg-fail-ordered-2

(*
 * Result: Never
 *
 * Demonstrate use of smp_mb__after_atomic() to make a failing cmpxchg
 * operation have acquire ordering.
 *)

{}

P0(int *x, int *y)
{
	int r1;

	WRITE_ONCE(*x, 1);
	r1 = cmpxchg(y, 0, 1);
}

P1(int *x, int *y)
{
	int r1;
	int r2;

	r1 = cmpxchg(y, 0, 1);
	smp_mb__after_atomic();
	r2 = READ_ONCE(*x);
}

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

```

---

# cmpxchg-fail-unordered-1 litmus test

_Peios Kernel / Kernel development / Litmus tests / atomic_

> Source and expected outcome of this Linux memory-model litmus test.

Source and expected outcome of this Linux memory-model litmus test.


```text
C cmpxchg-fail-unordered-1

(*
 * Result: Sometimes
 *
 * Demonstrate that a failing cmpxchg() operation does not act as a
 * full barrier.  (In contrast, a successful cmpxchg() does act as a
 * full barrier.)
 *)

{}

P0(int *x, int *y, int *z)
{
	int r0;
	int r1;

	WRITE_ONCE(*x, 1);
	r1 = cmpxchg(z, 1, 0);
	r0 = READ_ONCE(*y);
}

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

	WRITE_ONCE(*y, 1);
	r1 = cmpxchg(z, 1, 0);
	r0 = READ_ONCE(*x);
}

locations[0:r1;1:r1]
exists (0:r0=0 /\ 1:r0=0)

```

---

# cmpxchg-fail-unordered-2 litmus test

_Peios Kernel / Kernel development / Litmus tests / atomic_

> Source and expected outcome of this Linux memory-model litmus test.

Source and expected outcome of this Linux memory-model litmus test.


```text
C cmpxchg-fail-unordered-2

(*
 * Result: Sometimes
 *
 * Demonstrate that a failing cmpxchg() operation does not act as either
 * an acquire release operation.  (In contrast, a successful cmpxchg()
 * does act as both an acquire and a release operation.)
 *)

{}

P0(int *x, int *y)
{
	int r1;

	WRITE_ONCE(*x, 1);
	r1 = cmpxchg(y, 0, 1);
}

P1(int *x, int *y)
{
	int r1;
	int r2;

	r1 = cmpxchg(y, 0, 1);
	r2 = READ_ONCE(*x);
}

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

```

---

# DCL-broken litmus test

_Peios Kernel / Kernel development / Litmus tests / locking_

> Source and expected outcome of this Linux memory-model litmus test.

Source and expected outcome of this Linux memory-model litmus test.


```text
C DCL-broken

(*
 * Result: Sometimes
 *
 * This litmus test demonstrates more than just locking is required to
 * correctly implement double-checked locking.
 *)

{
	int flag;
	int data;
}

P0(int *flag, int *data, spinlock_t *lck)
{
	int r0;
	int r1;
	int r2;

	r0 = READ_ONCE(*flag);
	if (r0 == 0) {
		spin_lock(lck);
		r1 = READ_ONCE(*flag);
		if (r1 == 0) {
			WRITE_ONCE(*data, 1);
			WRITE_ONCE(*flag, 1);
		}
		spin_unlock(lck);
	}
	r2 = READ_ONCE(*data);
}

P1(int *flag, int *data, spinlock_t *lck)
{
	int r0;
	int r1;
	int r2;

	r0 = READ_ONCE(*flag);
	if (r0 == 0) {
		spin_lock(lck);
		r1 = READ_ONCE(*flag);
		if (r1 == 0) {
			WRITE_ONCE(*data, 1);
			WRITE_ONCE(*flag, 1);
		}
		spin_unlock(lck);
	}
	r2 = READ_ONCE(*data);
}

locations [flag;data;0:r0;0:r1;1:r0;1:r1]
exists (0:r2=0 \/ 1:r2=0)

```

---

# DCL-fixed litmus test

_Peios Kernel / Kernel development / Litmus tests / locking_

> Source and expected outcome of this Linux memory-model litmus test.

Source and expected outcome of this Linux memory-model litmus test.


```text
C DCL-fixed

(*
 * Result: Never
 *
 * This litmus test demonstrates that double-checked locking can be
 * reliable given proper use of smp_load_acquire() and smp_store_release()
 * in addition to the locking.
 *)

{
	int flag;
	int data;
}

P0(int *flag, int *data, spinlock_t *lck)
{
	int r0;
	int r1;
	int r2;

	r0 = smp_load_acquire(flag);
	if (r0 == 0) {
		spin_lock(lck);
		r1 = READ_ONCE(*flag);
		if (r1 == 0) {
			WRITE_ONCE(*data, 1);
			smp_store_release(flag, 1);
		}
		spin_unlock(lck);
	}
	r2 = READ_ONCE(*data);
}

P1(int *flag, int *data, spinlock_t *lck)
{
	int r0;
	int r1;
	int r2;

	r0 = smp_load_acquire(flag);
	if (r0 == 0) {
		spin_lock(lck);
		r1 = READ_ONCE(*flag);
		if (r1 == 0) {
			WRITE_ONCE(*data, 1);
			smp_store_release(flag, 1);
		}
		spin_unlock(lck);
	}
	r2 = READ_ONCE(*data);
}

locations [flag;data;0:r0;0:r1;1:r0;1:r1]
exists (0:r2=0 \/ 1:r2=0)

```

---

# RM-broken litmus test

_Peios Kernel / Kernel development / Litmus tests / locking_

> Source and expected outcome of this Linux memory-model litmus test.

Source and expected outcome of this Linux memory-model litmus test.


```text
C RM-broken

(*
 * Result: DEADLOCK
 *
 * This litmus test demonstrates that the old "roach motel" approach
 * to locking, where code can be freely moved into critical sections,
 * cannot be used in the Linux kernel.
 *)

{
	int x;
	atomic_t y;
}

P0(int *x, atomic_t *y, spinlock_t *lck)
{
	int r2;

	spin_lock(lck);
	r2 = atomic_inc_return(y);
	WRITE_ONCE(*x, 1);
	spin_unlock(lck);
}

P1(int *x, atomic_t *y, spinlock_t *lck)
{
	int r0;
	int r1;
	int r2;

	spin_lock(lck);
	r0 = READ_ONCE(*x);
	r1 = READ_ONCE(*x);
	r2 = atomic_inc_return(y);
	spin_unlock(lck);
}

locations [x;0:r2;1:r0;1:r1;1:r2]
filter (1:r0=0 /\ 1:r1=1)
exists (1:r2=1)

```

---

# RM-fixed litmus test

_Peios Kernel / Kernel development / Litmus tests / locking_

> Source and expected outcome of this Linux memory-model litmus test.

Source and expected outcome of this Linux memory-model litmus test.


```text
C RM-fixed

(*
 * Result: Never
 *
 * This litmus test demonstrates that the old "roach motel" approach
 * to locking, where code can be freely moved into critical sections,
 * cannot be used in the Linux kernel.
 *)

{
	int x;
	atomic_t y;
}

P0(int *x, atomic_t *y, spinlock_t *lck)
{
	int r2;

	spin_lock(lck);
	r2 = atomic_inc_return(y);
	WRITE_ONCE(*x, 1);
	spin_unlock(lck);
}

P1(int *x, atomic_t *y, spinlock_t *lck)
{
	int r0;
	int r1;
	int r2;

	r0 = READ_ONCE(*x);
	r1 = READ_ONCE(*x);
	spin_lock(lck);
	r2 = atomic_inc_return(y);
	spin_unlock(lck);
}

locations [x;0:r2;1:r0;1:r1;1:r2]
filter (1:r0=0 /\ 1:r1=1)
exists (1:r2=1)

```

---

# RCU+sync+free litmus test

_Peios Kernel / Kernel development / Litmus tests / rcu_

> Source and expected outcome of this Linux memory-model litmus test.

Source and expected outcome of this Linux memory-model litmus test.


```text
C RCU+sync+free

(*
 * Result: Never
 *
 * This litmus test demonstrates that an RCU reader can never see a write that
 * follows a grace period, if it did not see writes that precede that grace
 * period.
 *
 * This is a typical pattern of RCU usage, where the write before the grace
 * period assigns a pointer, and the writes following the grace period destroy
 * the object that the pointer used to point to.
 *
 * This is one implication of the RCU grace-period guarantee, which says (among
 * other things) that an RCU read-side critical section cannot span a grace period.
 *)

{
int x = 1;
int *y = &x;
int z = 1;
}

P0(int *x, int *z, int **y)
{
	int *r0;
	int r1;

	rcu_read_lock();
	r0 = rcu_dereference(*y);
	r1 = READ_ONCE(*r0);
	rcu_read_unlock();
}

P1(int *x, int *z, int **y)
{
	rcu_assign_pointer(*y, z);
	synchronize_rcu();
	WRITE_ONCE(*x, 0);
}

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

```

---

# RCU+sync+read litmus test

_Peios Kernel / Kernel development / Litmus tests / rcu_

> Source and expected outcome of this Linux memory-model litmus test.

Source and expected outcome of this Linux memory-model litmus test.


```text
C RCU+sync+read

(*
 * Result: Never
 *
 * This litmus test demonstrates that after a grace period, an RCU updater always
 * sees all stores done in prior RCU read-side critical sections. Such
 * read-side critical sections would have ended before the grace period ended.
 *
 * This is one implication of the RCU grace-period guarantee, which says (among
 * other things) that an RCU read-side critical section cannot span a grace period.
 *)

{
int x = 0;
int y = 0;
}

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

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

	r0 = READ_ONCE(*x);
	synchronize_rcu();
	r1 = READ_ONCE(*y);
}

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

```
