Skip to contentSkip to conversation

kernel/locking: Add mutual exclusion self-test

PATCH RESEND New Håkon Bugge 1 participant 2 messages Nov 3

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover kernel/locking: Add mutual exclusion self-test

Hi,

Please consider the following patch for the v7.4 merge window.

This patch adds a stress-based selftest for kernel locking and atomic synchronization primitives. The test runs concurrent workers against an array of counters while maintaining a simple invariant: every increment is paired with a decrement, so the sum of all counters must remain zero.

A common test interface allows the same workload to exercise a range of locking primitives, including spinlocks and rwlocks, as well as variants using atomic and bit operations without an explicit lock. After the workers complete, the module checks the invariant and reports a failure if the resulting sum is nonzero.

The test provides regression coverage for memory-ordering issues that may otherwise be difficult to reproduce. In particular, it can expose the regression fixed by commit 415d83249709 ("locking/atomic: Make test_and_*_bit() ordered on failure") on weakly ordered architectures such as Arm. It also includes a variant modeling the incorrect acquire/release ordering formerly used by RDS before commit 1422f28826d2 ("rds: introduce acquire/release ordering in acquire/release_in_xmit()"). This variant fails on Arm systems.

The patch also adds a kselftest script that:

* first runs an intentionally unsynchronized variant to verify that the workload is capable of exposing lost updates;
* obtains the available test variants from the module;
* runs each non-broken variant; and
* supports low, medium, and high effort levels for interactive and CI use.

The module exposes parameters for selecting the test variant, run duration, and number of elements.

A successful run does not prove correctness, but the test provides a practical way to detect synchronization regressions and reproduce known classes of ordering bugs under load.

If this commit gets accepted, I have the intention, subject to other work priorities, to expand on the locking primitives, and in particular add a double set of ww_mutexes for each counter and attempt to acquire both in a random order. Using both the Wound-Wait and Wait-Die variants.

Thxs, Håkon
Håkon Bugge (1):
kernel/locking: Add mutual exclusion self-test

 kernel/locking/Makefile                    |   1 +
 kernel/locking/mx_test.c                   | 723 +++++++++++++++++++++
 lib/Kconfig.debug                          |  10 +
 tools/testing/selftests/locking/Makefile   |   2 +-
 tools/testing/selftests/locking/mx_test.sh | 171 +++++
 5 files changed, 906 insertions(+), 1 deletion(-)
 create mode 100644 kernel/locking/mx_test.c
 create mode 100755 tools/testing/selftests/locking/mx_test.sh

--
2.43.5

#1 lore raw

PATCH RESEND v2 1/1 kernel/locking: Add mutual exclusion self-test

Add a stress test for locking and atomic synchronization primitives.

The test maintains an array of elements containing counters initialized to zero. It creates four worker threads per online CPU. On each iteration, a worker chooses two elements at random, increments the first counter, and decrements the second. Each completed pair of updates therefore preserves the sum of all counters.

The harness supports variants that protect the counters with an explicit locking primitive and variants that use allegedly lock-free primitives. A common operations interface allows the same workload to exercise different primitives.

The module accepts the following parameters:

mx_nmbr_elems:
Number of elements in the array. The default is 2048.

mx_test:
Test variant to run. Supported variants include spin_lock, spin_lock_irq, spin_lock_irqsave, and write_lock. Specifying an unknown variant causes the module to print the complete list of supported variants. The default is osq_lock.

mx_scnds_per_test:
Duration of each test run, in seconds. The default is 1.

After all workers have stopped, the test sums the counters and reports a failure if the result is nonzero. Such a result indicates that the expected invariant was violated. Because this is a stress test, a zero result means only that no failure was observed during that run.

The test can expose ordering regressions in atomic bit operations. For example, reverting commit 415d83249709 ("locking/atomic: Make test_and_*_bit() ordered on failure") causes the "test_and_clear_bit_innov" and "test_and_set_bit_innov" variants to fail on weakly ordered Arm systems. These variants rely on a failing test_and_*_bit() operation to enforce visibility of updates to other processors, rather than using smp_mb__before_atomic(). They therefore provide regression coverage for commit 415d83249709.

The harness also includes variants that reproduce known incorrect uses of these primitives. Before commit 1422f28826d2 ("rds: introduce acquire/release ordering in acquire/release_in_xmit()"), RDS used them without the required acquire/release ordering. The "rds_busted" variant models that bug and fails on Arm systems.

The "osq_lock_busted" locking type fails when the Optimistic Spin Queue (OSQ) helpers are treated as providing mutual exclusion: under the tested conditions, they do not. Whether the helpers should provide that guarantee remains under discussion.

Regardless of the intended semantics, this test documents the behavior observed in v7.3-rc4. If the helpers are changed to guarantee mutual exclusion, the test can then serve as regression coverage for that change.

The patch also adds a shell script for the CI kselftest framework. The script first runs the "busted" variant, which performs the updates without synchronization and is expected to fail. If that variant does not fail, the script skips the remaining tests because the run did not expose even the intentional race, making successful results from the other variants inconclusive.

The script then loads the module with an unsupported mx_test value to obtain the list of available variants from the kernel log. It parses that list and runs every variant whose name does not contain "busted".

For interactive use, the script accepts "-e low", "-e medium", or "-e high" to select the test effort.

---

v1 -> v2:
* Added "osq_lock_busted" as locking primitive. It is "busted" because the osq_lock helpers do not provide mutual exclusion.
* Added "atomic_xchg" locking type, in order to mimic the sequence atomic_xchg() in osq_lock() and corresponding atomic_try_cmpxchg_release() in osq_unlock()
* Refactored the big union to simplify mx_add
* Added "atomic64_add" lockless test type
* Having the counters, when needed, as a separate array, which removes the need for the padding logic
* Rebased on v7.3-rc4

---
 kernel/locking/Makefile                    |   1 +
 kernel/locking/mx_test.c                   | 723 +++++++++++++++++++++
 lib/Kconfig.debug                          |  10 +
 tools/testing/selftests/locking/Makefile   |   2 +-
 tools/testing/selftests/locking/mx_test.sh | 171 +++++
 5 files changed, 906 insertions(+), 1 deletion(-)
 create mode 100644 kernel/locking/mx_test.c
 create mode 100755 tools/testing/selftests/locking/mx_test.sh
kernel/locking/Makefile +1 -0
index cee1901d4cff8..240084b4abbb7 100644
@@ -38,3 +38,4 @@ obj-$(CONFIG_QUEUED_RWLOCKS) += qrwlock.o
 obj-$(CONFIG_LOCK_TORTURE_TEST) += locktorture.o
 obj-$(CONFIG_WW_MUTEX_SELFTEST) += test-ww_mutex.o
 obj-$(CONFIG_LOCK_EVENT_COUNTS) += lock_events.o
+obj-$(CONFIG_MX_TEST) += mx_test.o
kernel/locking/mx_test.c new file +723 -0

723 lines not on this page · raw message

lib/Kconfig.debug +10 -0
index 134b15a44625e..6b60c66e92627 100644
@@ -1691,6 +1691,16 @@ config CSD_LOCK_WAIT_DEBUG_DEFAULT
 	  This option causes the csdlock_debug= kernel boot parameter to
 	  default to 1 (basic debugging) instead of 0 (no debugging).
 
+config MX_TEST
+	tristate "miscellaneous lock-full and lock-less selftests"
+	help
+	  This option provides a kernel module that runs tests using
+	  different locking primitives and lockless operations.
+
+	  Say M if you want these self tests to build as a module.
+	  Say N if you are unsure.
+
+
 endmenu # lock debugging
 
 config TRACE_IRQFLAGS
tools/testing/selftests/locking/Makefile +1 -1
index 6e7761ab35360..d9cfb0e565275 100644
@@ -5,6 +5,6 @@
 # No binaries, but make sure arg-less "make" doesn't trigger "run_tests"
 all:
 
-TEST_PROGS := ww_mutex.sh
+TEST_PROGS := ww_mutex.sh mx_test.sh
 
 include ../lib.mk
tools/testing/selftests/locking/mx_test.sh new file +171 -0

171 lines not on this page · raw message

--
2.43.5