mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/2] kunit: add optional assertions to catch taint and lockdep warnings
@ 2026-08-24 13:32 Malte Wechter
  2026-08-24 13:32 ` [PATCH v2 1/2] kunit: add extra assertions to KUnit test cases Malte Wechter
  2026-08-24 13:32 ` [PATCH v2 2/2] kunit: add KUnit test to assert kernel state before KUnit suites are run Malte Wechter
  0 siblings, 2 replies; 3+ messages in thread
From: Malte Wechter @ 2026-08-24 13:32 UTC (permalink / raw)
  To: Brendan Higgins, David Gow, Rae Moar, Miguel Ojeda, Boqun Feng,
	Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Alice Ryhl, Trevor Gross, Danilo Krummrich, Daniel Almeida,
	Tamir Duberstein, Alexandre Courbot, Onur Özkan
  Cc: linux-kselftest, kunit-dev, linux-kernel, rust-for-linux, Malte Wechter

this patch tries to expand KUnit by using existing kernel diagnostics to
catch certain locking related bugs. Specifically this adds assertions for
`debug_locks` from `lockdep` and the `TAINT_WARN` flag from `panic`.
This tries to prevent bugs as: circular locking dependency and sleeping
function called from invalid context. 

Add config CONFIG_KUNIT_EXTRA_ASSERTS that enables extra assertions
to be emitted: First, before _any_ KUnit test suite is ran, this is to ensure
that the extra assertions are triggered by a KUnit test and not prior.
Secondly, assertions a made for each test case, failing them if the
assertions fail, even if the KUnit test is marked as passed.

Signed-off-by: Malte Wechter <maltewechter@gmail.com>
---
Changes in v2:
- Implement changes directly in C KUnit instead of Rust KUnit wrapper
- Remove rust specific configs and add general CONFIG_KUNIT_EXTRA_ASSERTS config
- Link to v1: https://patch.msgid.link/20260818-lockdep-kunit-v1-0-66ceb1a272e3@gmail.com

To: Brendan Higgins <brendan.higgins@linux.dev>
To: David Gow <david@davidgow.net>
To: Rae Moar <raemoar63@gmail.com>
To: Miguel Ojeda <ojeda@kernel.org>
To: Boqun Feng <boqun@kernel.org>
To: Gary Guo <gary@garyguo.net>
To: Björn Roy Baron <bjorn3_gh@protonmail.com>
To: Benno Lossin <lossin@kernel.org>
To: Andreas Hindborg <a.hindborg@kernel.org>
To: Alice Ryhl <aliceryhl@google.com>
To: Trevor Gross <tmgross@umich.edu>
To: Danilo Krummrich <dakr@kernel.org>
To: Daniel Almeida <daniel.almeida@collabora.com>
To: Tamir Duberstein <tamird@kernel.org>
To: Alexandre Courbot <acourbot@nvidia.com>
To: Onur Özkan <work@onurozkan.dev>
Cc: linux-kselftest@vger.kernel.org
Cc: kunit-dev@googlegroups.com
Cc: linux-kernel@vger.kernel.org
Cc: rust-for-linux@vger.kernel.org

---
Malte Wechter (2):
      kunit: add extra assertions to KUnit test cases
      kunit: add KUnit test to assert kernel state before KUnit suites are run

 lib/kunit/Kconfig     | 12 ++++++++++++
 lib/kunit/executor.c  |  8 +++++++-
 lib/kunit/test.c      | 30 ++++++++++++++++++++++++++++++
 lib/kunit/try-catch.c | 23 ++++++++++++++++++++++-
 4 files changed, 71 insertions(+), 2 deletions(-)
---
base-commit: 8cdeaa50eae8dad34885515f62559ee83e7e8dda
change-id: 20260812-lockdep-kunit-9628f4bcad90

Best regards,
--  
Malte Wechter <maltewechter@gmail.com>


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2 1/2] kunit: add extra assertions to KUnit test cases
  2026-08-24 13:32 [PATCH v2 0/2] kunit: add optional assertions to catch taint and lockdep warnings Malte Wechter
@ 2026-08-24 13:32 ` Malte Wechter
  2026-08-24 13:32 ` [PATCH v2 2/2] kunit: add KUnit test to assert kernel state before KUnit suites are run Malte Wechter
  1 sibling, 0 replies; 3+ messages in thread
From: Malte Wechter @ 2026-08-24 13:32 UTC (permalink / raw)
  To: Brendan Higgins, David Gow, Rae Moar, Miguel Ojeda, Boqun Feng,
	Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Alice Ryhl, Trevor Gross, Danilo Krummrich, Daniel Almeida,
	Tamir Duberstein, Alexandre Courbot, Onur Özkan
  Cc: linux-kselftest, kunit-dev, linux-kernel, rust-for-linux, Malte Wechter

add extra assertions for each individual test case, that checks that
both `debug_locks` and `TAINT_WARN` are intact after the test is run.
The assertions are optional behind CONFIG_KUNIT_EXTRA_ASSERTS.

Signed-off-by: Malte Wechter <maltewechter@gmail.com>
---
 lib/kunit/Kconfig     | 12 ++++++++++++
 lib/kunit/try-catch.c | 23 ++++++++++++++++++++++-
 2 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/lib/kunit/Kconfig b/lib/kunit/Kconfig
index 94ff8e4089bfb..38801f7493669 100644
--- a/lib/kunit/Kconfig
+++ b/lib/kunit/Kconfig
@@ -142,4 +142,16 @@ config KUNIT_UML_PCI
 
 	  If unsure, say N.
 
+config KUNIT_EXTRA_ASSERTS
+	bool "Enable extra assertions in KUnit tests"
+	depends on LOCKDEP
+	default n
+	help
+		Enables all extra assertions for KUnit which includes asserting `TAINT_WARN` and
+		`debug_locks` from lockdep. A KUnit test suite (and test case) is inserted
+		at the start of all KUnit test suites. This makes assertions prior to running any
+		tests, as a pre-test integrity check. Assertions are made after each test case which
+		asserts that each test case did not trigger either `TAINT_WARN` or `debug_locks`.
+
+		If unsure, say N.
 endif # KUNIT
diff --git a/lib/kunit/try-catch.c b/lib/kunit/try-catch.c
index d84a879f0a789..7eea3af4c9671 100644
--- a/lib/kunit/try-catch.c
+++ b/lib/kunit/try-catch.c
@@ -41,6 +41,11 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)
 	struct completion *task_done;
 	int exit_code, time_remaining;
 
+	#ifdef CONFIG_KUNIT_EXTRA_ASSERTS
+	int debug_locks_snapshot = debug_locks;
+	int tainted_warn_snapshot = test_taint(TAINT_WARN);
+	#endif
+
 	try_catch->context = context;
 	try_catch->try_result = 0;
 	task_struct = kthread_create(kunit_generic_run_threadfn_adapter,
@@ -70,7 +75,23 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)
 	put_task_struct(task_struct);
 	exit_code = try_catch->try_result;
 
-	if (!exit_code)
+	#ifdef CONFIG_KUNIT_EXTRA_ASSERTS
+	bool extra_assert = false;
+
+	if (debug_locks_snapshot != debug_locks && !exit_code) {
+		extra_assert = true;
+		try_catch->try_result = -EDEADLK;
+		kunit_err(test, "Test triggered lockdep\n");
+	} else if (tainted_warn_snapshot != test_taint(TAINT_WARN) && !exit_code) {
+		extra_assert = true;
+		try_catch->try_result = -EDEADLK;
+		kunit_err(test, "Test tainted kernel with TAINT_WARN\n");
+	}
+	#else
+	bool extra_assert = false;
+	#endif
+
+	if (!exit_code && !extra_assert)
 		return;
 
 	if (exit_code == -EFAULT)

-- 
2.51.2


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2 2/2] kunit: add KUnit test to assert kernel state before KUnit suites are run
  2026-08-24 13:32 [PATCH v2 0/2] kunit: add optional assertions to catch taint and lockdep warnings Malte Wechter
  2026-08-24 13:32 ` [PATCH v2 1/2] kunit: add extra assertions to KUnit test cases Malte Wechter
@ 2026-08-24 13:32 ` Malte Wechter
  1 sibling, 0 replies; 3+ messages in thread
From: Malte Wechter @ 2026-08-24 13:32 UTC (permalink / raw)
  To: Brendan Higgins, David Gow, Rae Moar, Miguel Ojeda, Boqun Feng,
	Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Alice Ryhl, Trevor Gross, Danilo Krummrich, Daniel Almeida,
	Tamir Duberstein, Alexandre Courbot, Onur Özkan
  Cc: linux-kselftest, kunit-dev, linux-kernel, rust-for-linux, Malte Wechter

add pre-defined KUnit test suite and test case that asserts both
`debug_locks` and `TAINT_WARN` prior to running any (user) KUnit tests.
This asserts integrity before tests are run.

Signed-off-by: Malte Wechter <maltewechter@gmail.com>
---
 lib/kunit/executor.c |  8 +++++++-
 lib/kunit/test.c     | 30 ++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/lib/kunit/executor.c b/lib/kunit/executor.c
index b0f8a41d61d36..0db67fe7f09f9 100644
--- a/lib/kunit/executor.c
+++ b/lib/kunit/executor.c
@@ -290,9 +290,15 @@ void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin)
 	size_t num_suites = suite_set->end - suite_set->start;
 	bool autorun = kunit_autorun();
 
+	#ifdef CONFIG_KUNIT_EXTRA_ASSERTS
+	size_t num_suites_plus_extra = num_suites+1;
+	#else
+	size_t num_suites_plus_extra = num_suites;
+	#endif
+
 	if (autorun && (builtin || num_suites)) {
 		pr_info("KTAP version 1\n");
-		pr_info("1..%zu\n", num_suites);
+		pr_info("1..%zu\n", num_suites_plus_extra);
 	}
 
 	__kunit_test_suites_init(suite_set->start, num_suites, autorun);
diff --git a/lib/kunit/test.c b/lib/kunit/test.c
index 99773e000e1b7..e64c6d1575280 100644
--- a/lib/kunit/test.c
+++ b/lib/kunit/test.c
@@ -835,6 +835,30 @@ bool kunit_enabled(void)
 	return enable_param;
 }
 
+#ifdef CONFIG_KUNIT_EXTRA_ASSERTS
+#define DEBUG_LOCKS_OK 1
+#define TAINT_WARN_OK 0
+
+static void pre_kunit_assert(struct kunit *test)
+{
+	KUNIT_EXPECT_EQ_MSG(test, debug_locks, DEBUG_LOCKS_OK,
+		"debug_locks are off before any test ran");
+	KUNIT_EXPECT_EQ_MSG(test, test_taint(TAINT_WARN), TAINT_WARN_OK,
+		"kernel already TAINT_WARN tainted before any test ran");
+}
+
+static struct kunit_case pre_kunit_assert_cases[] = {
+	KUNIT_CASE(pre_kunit_assert),
+	{}
+};
+
+static struct kunit_suite pre_kunit_assert_clean_state_suite = {
+	.name = "pre_kunit_extra_asserts",
+	.test_cases = pre_kunit_assert_cases,
+};
+
+#endif /* CONFIG_RUST_KUNIT_EXTRA_ASSERTS */
+
 int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites,
 			     bool run_tests)
 {
@@ -857,6 +881,12 @@ int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_
 	}
 	static_branch_inc(&kunit_running);
 
+	#ifdef CONFIG_KUNIT_EXTRA_ASSERTS
+	kunit_init_suite(&pre_kunit_assert_clean_state_suite);
+	if (run_tests)
+		kunit_run_tests(&pre_kunit_assert_clean_state_suite);
+	#endif
+
 	for (i = 0; i < num_suites; i++) {
 		kunit_init_suite(suites[i]);
 		if (run_tests)

-- 
2.51.2


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-08-24 13:32 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-08-24 13:32 [PATCH v2 0/2] kunit: add optional assertions to catch taint and lockdep warnings Malte Wechter
2026-08-24 13:32 ` [PATCH v2 1/2] kunit: add extra assertions to KUnit test cases Malte Wechter
2026-08-24 13:32 ` [PATCH v2 2/2] kunit: add KUnit test to assert kernel state before KUnit suites are run Malte Wechter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®