kunit: Add support for suppressing warning backtraces
Subsystems (9)
v10 cover kunit: Add support for suppressing warning backtraces
Some unit tests intentionally trigger warning backtraces by passing bad parameters to kernel API functions. Such unit tests typically check the return value from such calls, not the existence of the warning backtrace.
Such intentionally generated warning backtraces are neither desirable nor useful for a number of reasons:
- They can result in overlooked real problems.
- A warning that suddenly starts to show up in unit tests needs to be investigated and has to be marked to be ignored, for example by adjusting filter scripts. Such filters are ad hoc because there is no real standard format for warnings. On top of that, such filter scripts would require constant maintenance.
One option to address the problem would be to add messages such as "expected warning backtraces start/end here" to the kernel log. However, that would again require filter scripts, might result in missing real problematic warning backtraces triggered while the test is running, and the irrelevant backtrace(s) would still clog the kernel log.
Solve the problem by providing a means to suppress warning backtraces originating from the current kthread while executing test code. Since each KUnit test runs in its own kthread, this effectively scopes suppression to the test that enabled it, without requiring any architecture-specific code.
Overview:
Patch#1 Introduces the suppression infrastructure integrated into
KUnit's hook mechanism.
Patch#2 Adds selftests to validate the functionality. Patch#3 Demonstrates real-world usage in the DRM subsystem. Patch#4 Documents the new API and usage guidelines.
Design Notes:
Suppression is integrated into the existing KUnit hooks infrastructure, reusing the kunit_running static branch for zero overhead when no tests are running. The implementation lives entirely in the kunit module; only a static-inline wrapper and a function pointer slot are added to built-in code.
Suppression is checked at three points in the warning path:
- In warn_slowpath_fmt() (kernel/panic.c), for architectures without __WARN_FLAGS. The check runs before any output, fully suppressing both message and backtrace.
- In __warn_printk() (kernel/panic.c), for architectures that define __WARN_FLAGS but not their own __WARN_printf (arm64, loongarch, parisc, powerpc, riscv, sh). The check suppresses the warning message text that is printed before the trap enters __report_bug().
- In __report_bug() (lib/bug.c), for architectures that define __WARN_FLAGS. The check runs before __warn() is called, suppressing the backtrace and stack dump.
To avoid double-counting on architectures where both __warn_printk() and __report_bug() run for the same warning, the hook takes a bool
parameter: true to increment the suppression counter, false to suppress without counting.
The suppression state is dynamically allocated via kunit_kzalloc() and tied to the KUnit test lifecycle via kunit_add_action(), ensuring automatic cleanup at test exit. Writer-side access to the global suppression list is serialized with a spinlock; readers use RCU.
Two API forms are provided:
- kunit_warning_suppress(test) { ... }: scoped blocks with automatic cleanup. The suppression handle is not accessible outside the block, so warning counts (if needed) must be checked inside. Multiple suppression blocks are allowed.
- kunit_start/end_suppress_warning(test): direct functions that return an explicit handle. Use when the handle needs to be retained, or passed across helpers. Multiple suppression blocks are allowed.
This series is based on the RFC patch and subsequent discussion at
https://patchwork.kernel.org/project/linux-kselftest/patch/[email protected]/
and offers a more comprehensive solution of the problem discussed there.
Changes since RFC:
- Introduced CONFIG_KUNIT_SUPPRESS_BACKTRACE
- Minor cleanups and bug fixes
- Added support for all affected architectures
- Added support for counting suppressed warnings
- Added unit tests using those counters
- Added patch to suppress warning backtraces in dev_addr_lists tests
Changes since v1:
- Rebased to v6.9-rc1
- Added Tested-by:, Acked-by:, and Reviewed-by: tags
[I retained those tags since there have been no functional changes]
- Introduced KUNIT_SUPPRESS_BACKTRACE configuration option, enabled by default.
Changes since v2:
- Rebased to v6.9-rc2
- Added comments to drm warning suppression explaining why it is needed.
- Added patch to move conditional code in arch/sh/include/asm/bug.h to avoid kerneldoc warning
- Added architecture maintainers to Cc: for architecture specific patches
- No functional changes
Changes since v3:
- Rebased to v6.14-rc6
- Dropped net: "kunit: Suppress lock warning noise at end of dev_addr_lists tests" since 3db3b62955cd6d73afde05a17d7e8e106695c3b9
- Added __kunit_ and KUNIT_ prefixes.
- Tested on interessed architectures.
Changes since v4:
- Rebased to v6.15-rc7
- Dropped all code in __report_bug()
- Moved all checks in WARN*() macros.
- Dropped all architecture specific code.
- Made __kunit_is_suppressed_warning nice to noinstr functions.
Changes since v5:
- Rebased to v7.0-rc3
- Added RCU protection for the suppressed warnings list.
- Added static key and branching optimization.
- Removed custom strcmp implementation and reworked __kunit_is_suppressed_warning() entrypoint function.
Changes since v6:
- Moved suppression checks from WARN*() macros to warn_slowpath_fmt()
and __report_bug().
- Replaced stack-allocated suppression struct with kunit_kzalloc() heap allocation tied to the KUnit test lifecycle.
- Changed suppression strategy from function-name matching to task-scoped:
all warnings on the current task are suppressed between START and END,
rather than only warnings originating from a specific named function.
- Simplified macro API: removed KUNIT_DECLARE_SUPPRESSED_WARNING(),
the START macro now takes (test) and handles allocation internally.
- Removed static key and branching optiomization, as by the time it
was executed, callers are already in warn slowpaths.
- Link to v6: https://lore.kernel.org/r/[email protected]
Changes since v7:
- Integrated suppression into existing KUnit hooks infrastructure
- Removed CONFIG_KUNIT_SUPPRESS_BACKTRACE
- Added suppression check in __warn_printk()
- Added spinlock for writer-side RCU protection
- Replaced explicit rcu_read_lock/unlock with guard(rcu)()
- Added scoped API (kunit_warning_suppress) using __cleanup attribute
- Updated DRM patch to use scoped API
- Expanded self-tests: incremental counting, cross-kthread isolation
- Rewrote documentation covering all three API forms with examples
- Link to v7: https://lore.kernel.org/r/[email protected]
Changes since v8:
- Rebased to v7.1-rc2
- Remove KUNIT_START/END_SUPPRESSED_WARNING() macros
- Add KUNIT_EXPECT_SUPPRESSED_WARNING_COUNT checks to drm tests
- Link to v8: https://lore.kernel.org/r/[email protected]
Changes since v9:
- Fix silent false-pass when kunit_start_suppress_warning() returns NULL
- Fix RCU lockdep splat for kunit_is_suppressed_warning() calls
- Move disable_trace_on_warning() in __report_bug()
- Make suppress counter atomic
- Mark helper warn functions in selftest as noinline
- Add kunit_skip() for CONFIG_BUG=n in selftests
- Fix potentially uninitialized data.was_active in kthread seltest
- Add kthread_stop() in kthread selftest early exit
- Initialize scaling_factor to INT_MIN in DRM scaling tests
- Add include for bool in test-bug.h to fix CONFIG_KUNIT=n case
- Link to v9: https://lore.kernel.org/r/[email protected]
--
2.34.1
---
To: Brendan Higgins <[email protected]>
To: David Gow <[email protected]>
To: Rae Moar <[email protected]>
To: Andrew Morton <[email protected]>
To: Paul Walmsley <[email protected]>
To: Palmer Dabbelt <[email protected]>
To: Albert Ou <[email protected]>
To: Alexandre Ghiti <[email protected]>
To: Maarten Lankhorst <[email protected]>
To: Maxime Ripard <[email protected]>
To: Thomas Zimmermann <[email protected]>
To: David Airlie <[email protected]>
To: Simona Vetter <[email protected]>
To: Jonathan Corbet <[email protected]>
To: Shuah Khan <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
---
Alessandro Carminati (1):
bug/kunit: Core support for suppressing warning backtraces
Guenter Roeck (3):
kunit: Add backtrace suppression self-tests
drm: Suppress intentional warning backtraces in scaling unit tests
kunit: Add documentation for warning backtrace suppression API
Documentation/dev-tools/kunit/usage.rst | 46 +++++++- drivers/gpu/drm/tests/drm_rect_test.c | 32 +++++- include/kunit/test-bug.h | 26 +++++ include/kunit/test.h | 98 ++++++++++++++++ kernel/panic.c | 11 ++ lib/bug.c | 14 ++- lib/kunit/Makefile | 4 +- lib/kunit/backtrace-suppression-test.c | 196 ++++++++++++++++++++++++++++++++ lib/kunit/bug.c | 119 +++++++++++++++++++ lib/kunit/hooks-impl.h | 2 + 10 files changed, 538 insertions(+), 10 deletions(-)
---
base-commit: 74fe02ce122a6103f207d29fafc8b3a53de6abaf
change-id: 20260312-kunit_add_support-2f35806b19dd
Best regards,
Signature · 1 line
Albert Esteve <[email protected]>
PATCH v10 1/4 bug/kunit: Core support for suppressing warning backtraces
From: Alessandro Carminati <[email protected]>
Some unit tests intentionally trigger warning backtraces by passing bad parameters to kernel API functions. Such unit tests typically check the return value from such calls, not the existence of the warning backtrace.
Such intentionally generated warning backtraces are neither desirable nor useful for a number of reasons:
- They can result in overlooked real problems.
- A warning that suddenly starts to show up in unit tests needs to be investigated and has to be marked to be ignored, for example by adjusting filter scripts. Such filters are ad hoc because there is no real standard format for warnings. On top of that, such filter scripts would require constant maintenance.
Solve the problem by providing a means to suppress warning backtraces originating from the current kthread while executing test code. Since each KUnit test runs in its own kthread, this effectively scopes suppression to the test that enabled it. Limit changes to generic code to the absolute minimum.
Implementation details:
Suppression is integrated into the existing KUnit hooks infrastructure in test-bug.h, reusing the kunit_running static branch for zero overhead when no tests are running.
Suppression is checked at three points in the warning path:
- In warn_slowpath_fmt(), the check runs before any output, fully suppressing both message and backtrace. This covers architectures without __WARN_FLAGS.
- In __warn_printk(), the check suppresses the warning message text. This covers architectures that define __WARN_FLAGS but not their own __WARN_printf (arm64, loongarch, parisc, powerpc, riscv, sh), where the message is printed before the trap enters __report_bug().
- In __report_bug(), the check runs before __warn() is called, suppressing the backtrace and stack dump.
To avoid double-counting on architectures where both __warn_printk() and __report_bug() run for the same warning, kunit_is_suppressed_warning() takes a bool parameter: true to increment the suppression counter (used in warn_slowpath_fmt and __report_bug), false to check only (used in __warn_printk).
The suppression state is dynamically allocated via kunit_kzalloc() and tied to the KUnit test lifecycle via kunit_add_action(), ensuring automatic cleanup at test exit. Writer-side access to the global suppression list is serialized with a spinlock; readers use RCU.
Two API forms are provided:
- kunit_warning_suppress(test) { ... }: scoped, uses __cleanup for automatic teardown on scope exit, kunit_add_action() as safety net for abnormal exits (e.g. kthread_exit from failed assertions). Suppression handle is only accessible inside the block.
- kunit_start/end_suppress_warning(test): direct functions returning an explicit handle, for retaining the handle within the test, or for cross-function usage.
--- include/kunit/test-bug.h | 26 +++++++++++ include/kunit/test.h | 98 ++++++++++++++++++++++++++++++++++++++ kernel/panic.c | 11 +++++ lib/bug.c | 14 +++++- lib/kunit/Makefile | 3 +- lib/kunit/bug.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++ lib/kunit/hooks-impl.h | 2 + 7 files changed, 270 insertions(+), 3 deletions(-)
include/kunit/test-bug.h +26 -0
@@ -10,6 +10,7 @@#define _KUNIT_TEST_BUG_H#include <linux/stddef.h> /* for NULL */ +#include <linux/types.h> /* for bool */ #if IS_ENABLED (CONFIG_KUNIT )
@@ -23,6 +24,7 @@ DECLARE_STATIC_KEY_FALSE(kunit_running);extern struct kunit_hooks_table { __printf(3, 4) void (*fail_current_test)(const char*, int, const char*, ...); void *(*get_static_stub_address)(struct kunit *test, void *real_fn_addr); +bool (*is_suppressed_warning)(bool count); } kunit_hooks;/**
@@ -60,9 +62,33 @@ static inline struct kunit *kunit_get_current_test(void) } \ }while (0 ) +/** +* kunit_is_suppressed_warning() - Check if warnings are being suppressed +* by the current KUnit test. +* @count: if true, increment the suppression counter on match. +* +* Returns true if the current task has active warning suppression. +* Uses the kunit_running static branch for zero overhead when no tests run. +* +* A single WARN*() may traverse multiple call sites in the warning path +* (e.g., __warn_printk() and __report_bug()). Pass @count = true at the +* primary suppression point to count each warning exactly once, and +* @count = false at secondary points to suppress output without +* inflating the count. +*/ +static inline bool kunit_is_suppressed_warning(bool count) +{ +if (!static_branch_unlikely (&kunit_running)) +return false; + +return kunit_hooks.is_suppressed_warning && + kunit_hooks.is_suppressed_warning (count); +} +#else static inline struct kunit *kunit_get_current_test (void ) {return NULL ; } +static inline bool kunit_is_suppressed_warning (bool count) {return false; }#define kunit_fail_current_test (fmt, ...) do {} while (0)
include/kunit/test.h +98 -0
102 lines not on this page · raw message
kernel/panic.c +11 -0
@@ -39,6 +39,7 @@#include <linux/sys_info.h> #include <trace/events/error_report.h> #include <asm/sections.h> +#include <kunit/test-bug.h> #define PANIC_TIMER_STEP 100#define PANIC_BLINK_SPD 18
@@ -1124,6 +1125,11 @@ void warn_slowpath_fmt(const char *file, int line, unsigned taint,bool rcu =warn_rcu_enter ();struct warn_args args; +if (kunit_is_suppressed_warning (true)) { +warn_rcu_exit (rcu); +return ; + } +pr_warn (CUT_HERE );if (!fmt) {
@@ -1146,6 +1152,11 @@ void __warn_printk(const char *fmt, ...)bool rcu =warn_rcu_enter ();va_list args; +if (kunit_is_suppressed_warning (false)) { +warn_rcu_exit (rcu); +return ; + } +pr_warn (CUT_HERE );va_start (args, fmt);
lib/bug.c +12 -2
@@ -48,6 +48,7 @@#include <linux/rculist.h> #include <linux/ftrace.h> #include <linux/context_tracking.h> +#include <kunit/test-bug.h> extern struct bug_entry __start___bug_table[], __stop___bug_table[];
@@ -209,8 +210,6 @@ static enum bug_trap_type __report_bug(struct bug_entry *bug, unsigned long bugareturn BUG_TRAP_TYPE_NONE ; } -disable_trace_on_warning (); -bug_get_file_line (bug, &file, &line); fmt =bug_get_format (bug);
@@ -220,6 +219,17 @@ static enum bug_trap_type __report_bug(struct bug_entry *bug, unsigned long buga no_cut = bug->flags &BUGFLAG_NO_CUT_HERE ; has_args = bug->flags &BUGFLAG_ARGS ; +#ifdef CONFIG_KUNIT +/* +* Before the once logic so suppressed warnings do not consume +* the single-fire budget of WARN_ON_ONCE(). +*/ +if (warning &&kunit_is_suppressed_warning (true)) +return BUG_TRAP_TYPE_WARN ; +#endif + +disable_trace_on_warning (); +if (warning && once) {if (done)return BUG_TRAP_TYPE_WARN ;
lib/kunit/Makefile +2 -1
@@ -10,7 +10,8 @@ kunit-objs += test.o \ executor.o \ attributes.o \ device.o \ - platform.o + platform.o \ + bug.o ifeq ($(CONFIG_KUNIT_DEBUGFS),y) kunit-objs += debugfs.o
lib/kunit/bug.c new file +119 -0
119 lines not on this page · raw message
lib/kunit/hooks-impl.h +2 -0
@@ -19,6 +19,7 @@ void __printf(3, 4) __kunit_fail_current_test_impl(const char *file,int line, const char *fmt, ...);void *__kunit_get_static_stub_address_impl (struct kunit *test,void *real_fn_addr); +bool __kunit_is_suppressed_warning_impl (bool count);/* Code to set all of the function pointers. */ static inline void kunit_install_hooks (void )
@@ -26,6 +27,7 @@ static inline void kunit_install_hooks(void)/* Install the KUnit hook functions. */ kunit_hooks.fail_current_test = __kunit_fail_current_test_impl; kunit_hooks.get_static_stub_address = __kunit_get_static_stub_address_impl; + kunit_hooks.is_suppressed_warning = __kunit_is_suppressed_warning_impl; }#endif /* _KUNIT_HOOKS_IMPL_H */
Signature · 1 line
2.53.0
PATCH v10 2/4 kunit: Add backtrace suppression self-tests
In reply to Albert EsteveFrom: Guenter Roeck <[email protected]>
Add unit tests to verify that warning backtrace suppression works.
Tests cover both API forms:
- Scoped: kunit_warning_suppress() with in-block count verification and post-block inactivity check.
- Direct functions: kunit_start/end_suppress_warning() with sequential independent suppression blocks and per-block counts.
Furthermore, tests verify incremental warning counting, that kunit_has_active_suppress_warning() transitions correctly around suppression boundaries, and that suppression active in the test kthread does not leak to a separate kthread.
If backtrace suppression does _not_ work, the unit tests will likely trigger unsuppressed backtraces, which should actually help to get the affected architectures / platforms fixed.
--- lib/kunit/Makefile | 1 + lib/kunit/backtrace-suppression-test.c | 196 +++++++++++++++++++++++++++++++++ 2 files changed, 197 insertions(+)
lib/kunit/Makefile +1 -0
@@ -22,6 +22,7 @@ obj-$(if $(CONFIG_KUNIT),y) += hooks.o obj-$(CONFIG_KUNIT_TEST) += kunit-test.o obj-$(CONFIG_KUNIT_TEST) += platform-test.o +obj-$(CONFIG_KUNIT_TEST) += backtrace-suppression-test.o # string-stream-test compiles built-in only. ifeq ($(CONFIG_KUNIT_TEST),y)
lib/kunit/backtrace-suppression-test.c new file +196 -0
196 lines not on this page · raw message
Signature · 1 line
2.53.0
PATCH v10 3/4 drm: Suppress intentional warning backtraces in scaling unit tests
In reply to Albert EsteveFrom: Guenter Roeck <[email protected]>
The drm_test_rect_calc_hscale and drm_test_rect_calc_vscale unit tests intentionally trigger warning backtraces by providing bad parameters to the tested functions. What is tested is the return value, not the existence of a warning backtrace. Suppress the backtraces to avoid clogging the kernel log and distraction from real problems. Additionally, the suppression API allows to actually ensure a warning was triggered, without parsing any kernel logs and keeping them clean.
--- drivers/gpu/drm/tests/drm_rect_test.c | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-)
drivers/gpu/drm/tests/drm_rect_test.c +26 -6
@@ -10,6 +10,7 @@#include <drm/drm_rect.h> #include <drm/drm_mode.h> +#include <linux/limits.h> #include <linux/string_helpers.h> #include <linux/errno.h>
@@ -407,10 +408,20 @@ KUNIT_ARRAY_PARAM(drm_rect_scale, drm_rect_scale_cases, drm_rect_scale_case_descstatic void drm_test_rect_calc_hscale (struct kunit *test) {const struct drm_rect_scale_case *params = test->param_value; -int scaling_factor; +int expected_warnings = params->expected_scaling_factor == -EINVAL ; +int scaling_factor =INT_MIN ; - scaling_factor =drm_rect_calc_hscale (¶ms->src, ¶ms->dst, - params->min_range, params->max_range); +/* +* drm_rect_calc_hscale() generates a warning backtrace whenever bad +* parameters are passed to it. This affects unit tests with -EINVAL +* error code in expected_scaling_factor. +*/ +kunit_warning_suppress (test) { + scaling_factor =drm_rect_calc_hscale (¶ms->src, ¶ms->dst, + params->min_range, + params->max_range); +KUNIT_EXPECT_SUPPRESSED_WARNING_COUNT (test, expected_warnings); + }KUNIT_EXPECT_EQ (test, scaling_factor, params->expected_scaling_factor); }
@@ -418,10 +429,19 @@ static void drm_test_rect_calc_hscale(struct kunit *test)static void drm_test_rect_calc_vscale (struct kunit *test) {const struct drm_rect_scale_case *params = test->param_value; -int scaling_factor; +int expected_warnings = params->expected_scaling_factor == -EINVAL ; +int scaling_factor =INT_MIN ; - scaling_factor =drm_rect_calc_vscale (¶ms->src, ¶ms->dst, - params->min_range, params->max_range); +/* +* drm_rect_calc_vscale() generates a warning backtrace whenever bad +* parameters are passed to it. This affects unit tests with -EINVAL +* error code in expected_scaling_factor. +*/ +kunit_warning_suppress (test) { + scaling_factor =drm_rect_calc_vscale (¶ms->src, ¶ms->dst, + params->min_range, params->max_range); +KUNIT_EXPECT_SUPPRESSED_WARNING_COUNT (test, expected_warnings); + }KUNIT_EXPECT_EQ (test, scaling_factor, params->expected_scaling_factor); }
Signature · 1 line
2.53.0
PATCH v10 4/4 kunit: Add documentation for warning backtrace suppression API
In reply to Albert EsteveFrom: Guenter Roeck <[email protected]>
Document API functions for suppressing warning backtraces.
--- Documentation/dev-tools/kunit/usage.rst | 46 ++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-)
Documentation/dev-tools/kunit/usage.rst +45 -1
@@ -157,6 +157,50 @@ Alternatively, one can take full control over the error message by using if (some_setup_function()) KUNIT_FAIL(test, "Failed to setup thing for testing"); +Suppressing warning backtraces +------------------------------ + +Some unit tests trigger warning backtraces either intentionally or as a side +effect. Such backtraces are normally undesirable since they distract from +the actual test and may result in the impression that there is a problem. + +Backtraces can be suppressed with **task-scoped suppression**: while +suppression is active on the current task, the backtrace and stack dump from +``WARN*()``, ``WARN_ON*()``, and related macros on that task are suppressed. +Two API forms are available. + +- Scoped suppression is the simplest form. Wrap the code that triggers + warnings in a ``kunit_warning_suppress()`` block: + +.. code-block:: c + + static void some_test(struct kunit *test) + { + kunit_warning_suppress(test) { + trigger_backtrace(); + KUNIT_EXPECT_SUPPRESSED_WARNING_COUNT(test, 1); + } + } + +.. note:: + The warning count must be checked inside the block; the suppression handle + is not accessible after the block exits. + +- Direct functions return an explicit handle pointer. Use them when the handle + needs to be retained or passed across helper functions: + +.. code-block:: c + + static void some_test(struct kunit *test) + { + struct kunit_suppressed_warning *w; + + w = kunit_start_suppress_warning(test); + trigger_backtrace(); + kunit_end_suppress_warning(test, w); + + KUNIT_EXPECT_EQ(test, kunit_suppressed_warning_count(w), 1); + } Test Suites ~~~~~~~~~~~
@@ -1211,4 +1255,4 @@ For example: dev_managed_string = devm_kstrdup(fake_device, "Hello, World!"); // Everything is cleaned up automatically when the test ends. - } + }
Signature · 1 line
2.53.0
Quoted 152 lines · Albert Esteve
Some unit tests intentionally trigger warning backtraces by passing bad parameters to kernel API functions. Such unit tests typically check the return value from such calls, not the existence of the warning backtrace.
Such intentionally generated warning backtraces are neither desirable nor useful for a number of reasons:
- They can result in overlooked real problems.
- A warning that suddenly starts to show up in unit tests needs to be investigated and has to be marked to be ignored, for example by adjusting filter scripts. Such filters are ad hoc because there is no real standard format for warnings. On top of that, such filter scripts would require constant maintenance.One option to address the problem would be to add messages such as "expected warning backtraces start/end here" to the kernel log. However, that would again require filter scripts, might result in missing real problematic warning backtraces triggered while the test is running, and the irrelevant backtrace(s) would still clog the kernel log.
Solve the problem by providing a means to suppress warning backtraces originating from the current kthread while executing test code. Since each KUnit test runs in its own kthread, this effectively scopes suppression to the test that enabled it, without requiring any architecture-specific code.
Overview:
Patch#1 Introduces the suppression infrastructure integrated into
KUnit's hook mechanism.
Patch#2 Adds selftests to validate the functionality. Patch#3 Demonstrates real-world usage in the DRM subsystem. Patch#4 Documents the new API and usage guidelines.Design Notes:
Suppression is integrated into the existing KUnit hooks infrastructure, reusing thekunit_runningstatic branch for zero overhead when no tests are running. The implementation lives entirely in the kunit module; only a static-inline wrapper and a function pointer slot are added to built-in code.Suppression is checked at three points in the warning path:
- Inwarn_slowpath_fmt()(kernel/panic.c), for architectures without__WARN_FLAGS. The check runs before any output, fully suppressing both message and backtrace.
- In__warn_printk()(kernel/panic.c), for architectures that define__WARN_FLAGSbut not their own__WARN_printf(arm64, loongarch, parisc, powerpc, riscv, sh). The check suppresses the warning message text that is printed before the trap enters__report_bug().
- In__report_bug()(lib/bug.c), for architectures that define__WARN_FLAGS. The check runs before__warn()is called, suppressing the backtrace and stack dump.To avoid double-counting on architectures where both
__warn_printk()and__report_bug()run for the same warning, the hook takes a bool
parameter: true to increment the suppression counter, false to suppress without counting.The suppression state is dynamically allocated via
kunit_kzalloc()and tied to the KUnit test lifecycle viakunit_add_action(), ensuring automatic cleanup at test exit. Writer-side access to the global suppression list is serialized with a spinlock; readers use RCU.Two API forms are provided:
-kunit_warning_suppress(test){ ... }: scoped blocks with automatic cleanup. The suppression handle is not accessible outside the block, so warning counts (if needed) must be checked inside. Multiple suppression blocks are allowed.
- kunit_start/end_suppress_warning(test): direct functions that return an explicit handle. Use when the handle needs to be retained, or passed across helpers. Multiple suppression blocks are allowed.This series is based on the RFC patch and subsequent discussion at
https://patchwork.kernel.org/project/linux-kselftest/patch/[email protected]/
and offers a more comprehensive solution of the problem discussed there.Changes since RFC:
- IntroducedCONFIG_KUNIT_SUPPRESS_BACKTRACE
- Minor cleanups and bug fixes
- Added support for all affected architectures
- Added support for counting suppressed warnings
- Added unit tests using those counters
- Added patch to suppress warning backtraces indev_addr_liststestsChanges since v1:
- Rebased to v6.9-rc1
- Added Tested-by:, Acked-by:, and Reviewed-by: tags
[I retained those tags since there have been no functional changes]
- IntroducedKUNIT_SUPPRESS_BACKTRACEconfiguration option, enabled by default.Changes since v2:
- Rebased to v6.9-rc2
- Added comments to drm warning suppression explaining why it is needed.
- Added patch to move conditional code inarch/sh/include/asm/bug.hto avoid kerneldoc warning
- Added architecture maintainers to Cc: for architecture specific patches
- No functional changesChanges since v3:
- Rebased to v6.14-rc6
- Dropped net: "kunit: Suppress lock warning noise at end ofdev_addr_liststests" since3db3b62955cd6d73afde05a17d7e8e106695c3b9
- Added__kunit_andKUNIT_prefixes.
- Tested on interessed architectures.Changes since v4:
- Rebased to v6.15-rc7
- Dropped all code in__report_bug()
- Moved all checks in WARN*() macros.
- Dropped all architecture specific code.
- Made__kunit_is_suppressed_warningnice to noinstr functions.Changes since v5:
- Rebased to v7.0-rc3
- Added RCU protection for the suppressed warnings list.
- Added static key and branching optimization.
- Removed customstrcmpimplementation and reworked__kunit_is_suppressed_warning()entrypoint function.Changes since v6:
- Moved suppression checks from WARN*() macros towarn_slowpath_fmt()
and__report_bug().
- Replaced stack-allocated suppression struct withkunit_kzalloc()heap allocation tied to the KUnit test lifecycle.
- Changed suppression strategy from function-name matching to task-scoped:
all warnings on the current task are suppressed between START and END,
rather than only warnings originating from a specific named function.
- Simplified macro API: removedKUNIT_DECLARE_SUPPRESSED_WARNING(),
the START macro now takes (test) and handles allocation internally.
- Removed static key and branching optiomization, as by the time it
was executed, callers are already in warn slowpaths.
- Link to v6: https://lore.kernel.org/r/[email protected]Changes since v7:
- Integrated suppression into existing KUnit hooks infrastructure
- RemovedCONFIG_KUNIT_SUPPRESS_BACKTRACE
- Added suppression check in__warn_printk()
- Added spinlock for writer-side RCU protection
- Replaced explicit rcu_read_lock/unlock with guard(rcu)()
- Added scoped API (kunit_warning_suppress) using__cleanupattribute
- Updated DRM patch to use scoped API
- Expanded self-tests: incremental counting, cross-kthread isolation
- Rewrote documentation covering all three API forms with examples
- Link to v7: https://lore.kernel.org/r/[email protected]Changes since v8:
- Rebased to v7.1-rc2
- Remove KUNIT_START/END_SUPPRESSED_WARNING() macros
- AddKUNIT_EXPECT_SUPPRESSED_WARNING_COUNTchecks to drm tests
- Link to v8: https://lore.kernel.org/r/[email protected]
Quoted 11 lines
Changes since v9:
- Fix silent false-pass whenkunit_start_suppress_warning()returns NULL
- Fix RCU lockdep splat forkunit_is_suppressed_warning()calls
- Movedisable_trace_on_warning()in__report_bug()
- Make suppress counter atomic
- Mark helper warn functions in selftest as noinline
- Addkunit_skip()forCONFIG_BUG=nin selftests
- Fix potentially uninitialized data.was_active in kthread seltest
- Addkthread_stop()in kthread selftest early exit
- Initializescaling_factortoINT_MINin DRM scaling tests
- Add include for bool intest-bug.hto fixCONFIG_KUNIT=ncase
These were mostly sashiko + kernel test robot comments.
The current version is available at
https://sashiko.dev/#/patchset/20260513-kunit_add_support-v10-0-e379d206c8cd%40redhat.com,
which again has a few comments that I'd consider potential real issues.
I think synchronize_rcu() can be removed in patch 1, to avoid one of the mentioned issues. The struct kunit_suppressed_warning task would be safer if we increased its refcount. I'll take a reference on task_struct via get_task_struct() when starting suppression and release it on cleanup.
We can address the kthread use-after-free problem with the warn suppression selftest by actively looping while waiting for kthread_stop().
Finally, I am thinking of adding something like this to the drm test (similar to what we do with the suppression selftest):
```
if (!IS_ENABLED(CONFIG_BUG))
kunit_skip(test, "requires CONFIG_BUG");
```While sashiko is right about this, it is a bit annoying having to add this to tests that want to use the suppression API, especially since we are setting the drm test as a "real-life example". But I'd say that it is better to warn that the configuration is required rather than fail an assertion without knowing the reason.
Overall, these comments were slightly nitpicky already. So I hope I can clear them all with a new version. Sorry for the added noise, but it's better to fix these before merging!
Quoted 57 lines
- Link to v9: https://lore.kernel.org/r/[email protected]
--
2.34.1---
To: Brendan Higgins <[email protected]>
To: David Gow <[email protected]>
To: Rae Moar <[email protected]>
To: Andrew Morton <[email protected]>
To: Paul Walmsley <[email protected]>
To: Palmer Dabbelt <[email protected]>
To: Albert Ou <[email protected]>
To: Alexandre Ghiti <[email protected]>
To: Maarten Lankhorst <[email protected]>
To: Maxime Ripard <[email protected]>
To: Thomas Zimmermann <[email protected]>
To: David Airlie <[email protected]>
To: Simona Vetter <[email protected]>
To: Jonathan Corbet <[email protected]>
To: Shuah Khan <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]---
Alessandro Carminati (1):
bug/kunit: Core support for suppressing warning backtracesGuenter Roeck (3):
kunit: Add backtrace suppression self-tests
drm: Suppress intentional warning backtraces in scaling unit tests
kunit: Add documentation for warning backtrace suppression APIDocumentation/dev-tools/kunit/usage.rst | 46 +++++++- drivers/gpu/drm/tests/drm_rect_test.c | 32 +++++- include/kunit/test-bug.h | 26 +++++ include/kunit/test.h | 98 ++++++++++++++++ kernel/panic.c | 11 ++ lib/bug.c | 14 ++- lib/kunit/Makefile | 4 +- lib/kunit/backtrace-suppression-test.c | 196 ++++++++++++++++++++++++++++++++ lib/kunit/bug.c | 119 +++++++++++++++++++ lib/kunit/hooks-impl.h | 2 + 10 files changed, 538 insertions(+), 10 deletions(-)---
base-commit:74fe02ce122a6103f207d29fafc8b3a53de6abaf
change-id: 20260312-kunit_add_support-2f35806b19ddBest regards,
--
Albert Esteve <[email protected]>