Skip to contentSkip to conversation

ww_mutex: convert self-test to KUnit

PATCH In review Tamir Duberstein 2 participants 6 messages Feb 13, 2025 – Feb 21, 2025

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

PATCH v2 1/1 ww_mutex: convert self-test to KUnit

Convert this unit test to a KUnit test. This allows the test to benefit from the KUnit tooling. Note that care is taken to avoid test-ending assertions in worker threads, which is unsafe in KUnit (and wasn't done before this change either).

---
I tested this using:

$ tools/testing/kunit/kunit.py run --arch arm64 --make_options LLVM=1 ww_mutex

; [12:48:16] ================== ww_mutex (5 subtests) ===================
; [12:48:16] ======================= test_mutex ========================
; [12:48:16] [PASSED] flags=0
; [12:48:16] [PASSED] flags=1
; [12:48:16] [PASSED] flags=2
; [12:48:16] [PASSED] flags=3
; [12:48:16] [PASSED] flags=4
; [12:48:17] [PASSED] flags=5
; [12:48:17] [PASSED] flags=6
; [12:48:17] [PASSED] flags=7
; [12:48:17] =================== [PASSED] test_mutex ====================
; [12:48:17] ========================= test_aa =========================
; [12:48:17] [PASSED] lock
; [12:48:17] [PASSED] trylock
; [12:48:17] ===================== [PASSED] test_aa =====================
; [12:48:17] ======================== test_abba ========================
; [12:48:17] [PASSED] trylock=0,resolve=0
; [12:48:17] [PASSED] trylock=1,resolve=1
; [12:48:17] [PASSED] trylock=0,resolve=0
; [12:48:17] [PASSED] trylock=1,resolve=1
; [12:48:17] ==================== [PASSED] test_abba ====================
; [12:48:17] ======================= test_cycle ========================
; [12:48:17] [PASSED] nthreads=2
; [12:48:17] =================== [PASSED] test_cycle ====================
; [12:48:21] ========================= stress ==========================
; [12:48:21] [PASSED] nlocks=16,nthreads_per_cpu=2,flags=1
; [12:48:23] [PASSED] nlocks=16,nthreads_per_cpu=2,flags=2
; [12:48:23] [PASSED] nlocks=2046,nthreads_per_cpu=3,flags=7
; [12:48:23] ===================== [PASSED] stress ======================
; [12:48:23] ==================== [PASSED] ww_mutex =====================
; [12:48:23] ============================================================
; [12:48:23] Testing complete. Ran 18 tests: passed: 18
---
Changes in v2:
- Avoid KUNIT_ASSERT_* in non-main thread. (David Gow)
- Include rationale and details in the commit message. (Boqun Feng)
- Introduce test_abba_param to avoid odd-looking bit shifting. (Dan
Carpenter)
- Rebase on linux-next.
- Move the test to tests/ per KUnit style guide.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 kernel/locking/Makefile                            |   3 +-
 kernel/locking/tests/Makefile                      |   3 +
 .../{test-ww_mutex.c => tests/ww_mutex_kunit.c}    | 319 +++++++++++----------
 lib/Kconfig.debug                                  |  12 +-
 tools/testing/selftests/locking/ww_mutex.sh        |  19 --
 5 files changed, 176 insertions(+), 180 deletions(-)
kernel/locking/Makefile +2 -1
index 0db4093d17b8..b5fa68d18823 100644
@@ -30,5 +30,6 @@ obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock.o
 obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock_debug.o
 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-y += tests/
kernel/locking/tests/Makefile new file +3 -0
new file mode 100644
index 000000000000..8ddde822c793
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+obj-$(CONFIG_WW_MUTEX_KUNIT_TEST) += ww_mutex_kunit.o
kernel/locking/tests/ww_mutex_kunit.c renamed from kernel/locking/test-ww_mutex.c +164 -155

549 lines not on this page · raw message

lib/Kconfig.debug +7 -5
index 85b95d645b10..2c9da8647eaf 100644
@@ -1595,16 +1595,18 @@ config LOCK_TORTURE_TEST
 	  Say M if you want these torture tests to build as a module.
 	  Say N if you are unsure.
 
-config WW_MUTEX_SELFTEST
-	tristate "Wait/wound mutex selftests"
+config WW_MUTEX_KUNIT_TEST
+	tristate "KUnit test for wait/wound mutex" if !KUNIT_ALL_TESTS
+	depends on KUNIT
+	default KUNIT_ALL_TESTS
 	help
-	  This option provides a kernel module that runs tests on the
-	  on the struct ww_mutex locking API.
+	  This option provides a KUnit test that exercises the struct
+	  ww_mutex locking API.
 
 	  It is recommended to enable DEBUG_WW_MUTEX_SLOWPATH in conjunction
 	  with this test harness.
 
-	  Say M if you want these self tests to build as a module.
+	  Say M if you want these tests to build as a module.
 	  Say N if you are unsure.
 
 config SCF_TORTURE_TEST
tools/testing/selftests/locking/ww_mutex.sh deleted +0 -19
deleted file mode 100755
index 91e4ac7566af..000000000000
@@ -1,19 +0,0 @@
-#!/bin/sh
-# SPDX-License-Identifier: GPL-2.0
-
-# Kselftest framework requirement - SKIP code is 4.
-ksft_skip=4
-
-# Runs API tests for struct ww_mutex (Wait/Wound mutexes)
-if ! /sbin/modprobe -q -n test-ww_mutex; then
-	echo "ww_mutex: module test-ww_mutex is not found [SKIP]"
-	exit $ksft_skip
-fi
-
-if /sbin/modprobe -q test-ww_mutex; then
-       /sbin/modprobe -q -r test-ww_mutex
-       echo "locking/ww_mutex: ok"
-else
-       echo "locking/ww_mutex: [FAIL]"
-       exit 1
-fi

---
base-commit: 7b7a883c7f4de1ee5040bd1c32aabaafde54d209
change-id: 20250208-ww_mutex-kunit-convert-71842a7a6be2

Best regards,

Signature · 1 line
Tamir Duberstein <[email protected]>
maintainer
#1 lore raw
Quoted 4 lines · Tamir Duberstein

Convert this unit test to a KUnit test. This allows the test to benefit from the KUnit tooling. Note that care is taken to avoid test-ending assertions in worker threads, which is unsafe in KUnit (and wasn't done before this change either).

So this was something simple, and now I need to know how to operate this kunit nonsense :-(

How is that an improvement?

#2 lore raw
Quoted 11 lines · Peter Zijlstra
Quoted 4 lines · Tamir Duberstein

Convert this unit test to a KUnit test. This allows the test to benefit from the KUnit tooling. Note that care is taken to avoid test-ending assertions in worker threads, which is unsafe in KUnit (and wasn't done before this change either).

So this was something simple, and now I need to know how to operate this kunit nonsense :-(

How is that an improvement?

Hi Peter,

David enumerated some of the benefits of KUnit in another
thread: https://lore.kernel.org/all/CABVgOS=KZrM2dWyp1HzVS0zh7vquLxmTY2T2Ti53DQADrW+sJg@mail.gmail.com/.

My personal reason for preferring KUnit is that it's much easier to run from userspace; the tooling takes care of building, starting the VM, running the tests, and producing a human-friendly report.

Anecdotally I've seen evidence that e.g. Intel's 0-day runs all KUnit tests but not all kselftests
(e.g. https://lore.kernel.org/all/[email protected]/).

Tamir

maintainer
#3 lore raw
Quoted 13 lines · Tamir Duberstein
Quoted 11 lines · Peter Zijlstra
Quoted 4 lines · Tamir Duberstein

Convert this unit test to a KUnit test. This allows the test to benefit from the KUnit tooling. Note that care is taken to avoid test-ending assertions in worker threads, which is unsafe in KUnit (and wasn't done before this change either).

So this was something simple, and now I need to know how to operate this kunit nonsense :-(

How is that an improvement?

Quoted 8 lines

Hi Peter,

David enumerated some of the benefits of KUnit in another
thread: https://lore.kernel.org/all/CABVgOS=KZrM2dWyp1HzVS0zh7vquLxmTY2T2Ti53DQADrW+sJg@mail.gmail.com/.

My personal reason for preferring KUnit is that it's much easier to run from userspace; the tooling takes care of building, starting the VM, running the tests, and producing a human-friendly report.

Wait what -- you have to run a VM just to use Kunit? That's a hard pass.

If I can't simply run it natively on my test box, its a no go. And it using python also don't help, you shouldn't be needing that to load a module.

#4 lore raw
Quoted 24 lines · Peter Zijlstra
Quoted 13 lines · Tamir Duberstein
Quoted 11 lines · Peter Zijlstra
Quoted 4 lines · Tamir Duberstein

Convert this unit test to a KUnit test. This allows the test to benefit from the KUnit tooling. Note that care is taken to avoid test-ending assertions in worker threads, which is unsafe in KUnit (and wasn't done before this change either).

So this was something simple, and now I need to know how to operate this kunit nonsense :-(

How is that an improvement?

Quoted 8 lines

Hi Peter,

David enumerated some of the benefits of KUnit in another
thread: https://lore.kernel.org/all/CABVgOS=KZrM2dWyp1HzVS0zh7vquLxmTY2T2Ti53DQADrW+sJg@mail.gmail.com/.

My personal reason for preferring KUnit is that it's much easier to run from userspace; the tooling takes care of building, starting the VM, running the tests, and producing a human-friendly report.

Quoted 5 lines

Wait what -- you have to run a VM just to use Kunit? That's a hard pass.

If I can't simply run it natively on my test box, its a no go. And it using python also don't help, you shouldn't be needing that to load a module.

The tooling I mention is all optional. You can still use modprobe to run the test as a module or at boot. In this case you'd set:

CONFIG_KUNIT=y
CONFIG_WW_MUTEX_KUNIT_TEST=m

and then run the test via:

```
modprobe ww_mutex_kunit
```

or to run at boot:

CONFIG_KUNIT=y
CONFIG_WW_MUTEX_KUNIT_TEST=y

#5 lore raw
Quoted 44 lines · Tamir Duberstein
Quoted 24 lines · Peter Zijlstra
Quoted 13 lines · Tamir Duberstein
Quoted 11 lines · Peter Zijlstra
Quoted 4 lines · Tamir Duberstein

Convert this unit test to a KUnit test. This allows the test to benefit from the KUnit tooling. Note that care is taken to avoid test-ending assertions in worker threads, which is unsafe in KUnit (and wasn't done before this change either).

So this was something simple, and now I need to know how to operate this kunit nonsense :-(

How is that an improvement?

Quoted 8 lines

Hi Peter,

David enumerated some of the benefits of KUnit in another
thread: https://lore.kernel.org/all/CABVgOS=KZrM2dWyp1HzVS0zh7vquLxmTY2T2Ti53DQADrW+sJg@mail.gmail.com/.

My personal reason for preferring KUnit is that it's much easier to run from userspace; the tooling takes care of building, starting the VM, running the tests, and producing a human-friendly report.

Quoted 5 lines

Wait what -- you have to run a VM just to use Kunit? That's a hard pass.

If I can't simply run it natively on my test box, its a no go. And it using python also don't help, you shouldn't be needing that to load a module.

The tooling I mention is all optional. You can still use modprobe to run the test as a module or at boot. In this case you'd set:

CONFIG_KUNIT=y
CONFIG_WW_MUTEX_KUNIT_TEST=m

and then run the test via:

```
modprobe ww_mutex_kunit
```
Quoted 7 lines

or to run at boot:

CONFIG_KUNIT=y
CONFIG_WW_MUTEX_KUNIT_TEST=y

Gentle ping. Peter, does the above address your concerns?