kunit: Add support for suppressing warning backtraces
Subsystems (8)
v7 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 identify and suppress specific warning backtraces while executing test code. Support suppressing multiple backtraces while at the same time limiting changes to generic code to the absolute minimum.
Overview:
Patch#1 Introduces the suppression infrastructure. Patch#2 Mitigates the impact of suppression. Patch#3 Adds selftests to validate the functionality. Patch#4 Demonstrates real-world usage in the DRM subsystem. Patch#5 Documents the new API and usage guidelines.
Design Notes:
The objective is to suppress unwanted WARN*() generated messages.
Although most major architectures share common bug handling via lib/bug.c and report_bug(), some minor or legacy architectures still rely on their own platform-specific handling. This divergence must be considered in any such feature. Additionally, a key challenge in implementing this feature is the fragmentation of WARN*() message emission: part of the output is produced in the macro itself (via __warn_printk()), and part in the exception handler.
Lessons from the Previous Attempt:
In earlier iterations, suppression logic was added inside the __report_bug() function to intercept WARN*() output. To implement the check in the bug handler code, two strategies were considered:
* Strategy #1: Use kallsyms to infer the originating function. This approach proved unreliable due to compiler-induced transformations such as inlining, cloning, and code fragmentation.
* Strategy #2: Store function name __func__ in struct bug_entry in the __bug_table. However, __func__ is a compiler-generated symbol, which complicates relocation and linking in position-independent code. Additionally, architectures not using the unified BUG() path would still require ad-hoc handling.
A per-macro solution was also attempted (v5-v6), injecting checks directly into the WARN*() macros in include/asm-generic/bug.h. While this offered full control, it required modifying the generic bug header and was considered too invasive and damaging the critical path, and thus incorrect [1].
Current Proposal: Check in warn_slowpath_fmt() and __report_bug(). Suppression is checked at two 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 __report_bug() (lib/bug.c), for architectures that define __WARN_FLAGS. The check runs before __warn() is called, suppressing the backtrace and stack dump. On this path, the WARN() format message may still appear in the kernel log since __warn_printk() executes before the trap.
This approach avoids modifying include/asm-generic/bug.h entirely, requires no architecture-specific code, and limits changes to generic code to the absolute minimum.
A helper function, __kunit_is_suppressed_warning(), walks an RCU- protected list of active suppressions, matching by current task. 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.
To minimize runtime impact when no suppressions are active, an atomic counter tracks the number of active suppressions. __kunit_is_suppressed_warning() checks this counter first and returns immediately when it is zero, avoiding the RCU-protected list traversal in the common case.
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.
[1] https://lore.kernel.org/all/CAGegRW76X8Fk_5qqOBw_aqBwAkQTsc8kXKHEuu9ECeXzdJwMSw@mail.gmail.com/
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]
Alessandro Carminati (2):
bug/kunit: Core support for suppressing warning backtraces bug/kunit: Suppressing warning backtraces reduced impact on WARN*()
sites
Guenter Roeck (3):
Add unit tests to verify that warning backtrace suppression works.
drm: Suppress intentional warning backtraces in scaling unit tests
kunit: Add documentation for warning backtrace suppression API
Documentation/dev-tools/kunit/usage.rst | 30 ++++++- drivers/gpu/drm/tests/drm_rect_test.c | 16 ++++ include/asm-generic/bug.h | 48 +++++++---- include/kunit/bug.h | 62 ++++++++++++++ include/kunit/test.h | 1 + lib/kunit/Kconfig | 9 ++ lib/kunit/Makefile | 9 +- lib/kunit/backtrace-suppression-test.c | 105 ++++++++++++++++++++++++ lib/kunit/bug.c | 54 ++++++++++++ 9 files changed, 316 insertions(+), 18 deletions(-) create mode 100644 include/kunit/bug.h create mode 100644 lib/kunit/backtrace-suppression-test.c create mode 100644 lib/kunit/bug.c
--
2.34.1
---
Alessandro Carminati (2):
bug/kunit: Core support for suppressing warning backtraces bug/kunit: Reduce runtime impact of warning backtrace suppression
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 | 30 ++++++++++- drivers/gpu/drm/tests/drm_rect_test.c | 14 +++++ include/kunit/bug.h | 56 ++++++++++++++++++++ include/kunit/test.h | 1 + kernel/panic.c | 8 ++- lib/bug.c | 8 +++ lib/kunit/Kconfig | 9 ++++ lib/kunit/Makefile | 9 +++- lib/kunit/backtrace-suppression-test.c | 90 ++++++++++++++++++++++++++++++++ lib/kunit/bug.c | 91 +++++++++++++++++++++++++++++++++ 10 files changed, 312 insertions(+), 4 deletions(-)
---
base-commit: 80234b5ab240f52fa45d201e899e207b9265ef91
change-id: 20260312-kunit_add_support-2f35806b19dd
Best regards,
Signature · 1 line
Albert Esteve <[email protected]>
PATCH v7 1/5 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 identify and suppress specific warning backtraces while executing test code. Support suppressing multiple backtraces while at the same time limiting changes to generic code to the absolute minimum.
Implementation details:
Suppression is checked at two points in the warning path:
- In warn_slowpath_fmt(), the check runs before any output, fully suppressing both message and backtrace.
- In __report_bug(), the check runs before __warn() is called, suppressing the backtrace and stack dump. Note that on this path, the WARN() format message may still appear in the kernel log since __warn_printk() runs before the trap that enters __report_bug().
A helper function, __kunit_is_suppressed_warning(), walks an RCU-protected list of active suppressions, matching by current task. The suppression state is tied to the KUnit test lifecycle via kunit_add_action(), ensuring automatic cleanup at test exit.
The list of suppressed warnings is protected with RCU to allow concurrent read access without locks.
The implementation is deliberately simple and avoids architecture-specific optimizations to preserve portability.
--- include/kunit/bug.h | 56 +++++++++++++++++++++++++++++++++++ include/kunit/test.h | 1 + kernel/panic.c | 8 ++++- lib/bug.c | 8 +++++ lib/kunit/Kconfig | 9 ++++++ lib/kunit/Makefile | 6 ++-- lib/kunit/bug.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 169 insertions(+), 3 deletions(-)
include/kunit/bug.h new file +56 -0
@@ -0,0 +1,56 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* KUnit helpers for backtrace suppression +* +* Copyright (C) 2025 Alessandro Carminati <[email protected]> +* Copyright (C) 2024 Guenter Roeck <[email protected]> +*/ + +#ifndef _KUNIT_BUG_H +#define _KUNIT_BUG_H + +#ifndef __ASSEMBLY__ + +#include <linux/kconfig.h> + +struct kunit ; + +#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE + +#include <linux/types.h> + +struct task_struct ; + +struct __suppressed_warning { +struct list_head node; +struct task_struct *task; +int counter; +}; + +struct __suppressed_warning * +__kunit_start_suppress_warning (struct kunit *test); +void __kunit_end_suppress_warning (struct kunit *test, +struct __suppressed_warning *warning); +int __kunit_suppressed_warning_count (struct __suppressed_warning *warning); +bool __kunit_is_suppressed_warning (void ); + +#define KUNIT_START_SUPPRESSED_WARNING (test) \ + struct __suppressed_warning *__kunit_suppress = \ + __kunit_start_suppress_warning(test) + +#define KUNIT_END_SUPPRESSED_WARNING (test) \ + __kunit_end_suppress_warning(test, __kunit_suppress) + +#define KUNIT_SUPPRESSED_WARNING_COUNT () \ + __kunit_suppressed_warning_count(__kunit_suppress) + +#else /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */ + +#define KUNIT_START_SUPPRESSED_WARNING (test) +#define KUNIT_END_SUPPRESSED_WARNING (test) +#define KUNIT_SUPPRESSED_WARNING_COUNT () 0 +static inline bool __kunit_is_suppressed_warning (void ) {return false; } + +#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */ +#endif /* __ASSEMBLY__ */ +#endif /* _KUNIT_BUG_H */
include/kunit/test.h +1 -0
@@ -10,6 +10,7 @@#define _KUNIT_TEST_H#include <kunit/assert.h> +#include <kunit/bug.h> #include <kunit/try-catch.h> #include <linux/args.h>
kernel/panic.c +7 -1
@@ -39,6 +39,7 @@#include <linux/sys_info.h> #include <trace/events/error_report.h> #include <asm/sections.h> +#include <kunit/bug.h> #define PANIC_TIMER_STEP 100#define PANIC_BLINK_SPD 18
@@ -1080,9 +1081,14 @@ void __warn(const char *file, int line, void *caller, unsigned taint,void warn_slowpath_fmt (const char *file,int line,unsigned taint,const char *fmt, ...) { -bool rcu =warn_rcu_enter (); +bool rcu;struct warn_args args; +if (__kunit_is_suppressed_warning ()) +return ; + + rcu =warn_rcu_enter (); +pr_warn (CUT_HERE );if (!fmt) {
lib/bug.c +8 -0
@@ -48,6 +48,7 @@#include <linux/rculist.h> #include <linux/ftrace.h> #include <linux/context_tracking.h> +#include <kunit/bug.h> extern struct bug_entry __start___bug_table[], __stop___bug_table[];
@@ -223,6 +224,13 @@ 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 ; +/* +* Before the once logic so suppressed warnings do not consume +* the single-fire budget of WARN_ON_ONCE(). +*/ +if (warning &&__kunit_is_suppressed_warning ()) +return BUG_TRAP_TYPE_WARN ; +if (warning && once) {if (done)return BUG_TRAP_TYPE_WARN ;
lib/kunit/Kconfig +9 -0
@@ -15,6 +15,15 @@ menuconfig KUNIT if KUNIT +config KUNIT_SUPPRESS_BACKTRACE + bool "KUnit - Enable backtrace suppression" + default y + help + Enable backtrace suppression for KUnit. If enabled, backtraces + generated intentionally by KUnit tests are suppressed. Disable + to reduce kernel image size if image size is more important than + suppression of backtraces generated by KUnit tests. + config KUNIT_DEBUGFS bool "KUnit - Enable /sys/kernel/debug/kunit debugfs representation" if !KUNIT_ALL_TESTS default KUNIT_ALL_TESTS
lib/kunit/Makefile +4 -2
@@ -16,8 +16,10 @@ ifeq ($(CONFIG_KUNIT_DEBUGFS),y) kunit-objs += debugfs.o endif -# KUnit 'hooks' are built-in even when KUnit is built as a module. -obj-$(if $(CONFIG_KUNIT),y) += hooks.o +# KUnit 'hooks' and bug handling are built-in even when KUnit is built +# as a module. +obj-$(if $(CONFIG_KUNIT),y) += hooks.o \ + bug.o obj-$(CONFIG_KUNIT_TEST) += kunit-test.o obj-$(CONFIG_KUNIT_TEST) += platform-test.o
lib/kunit/bug.c new file +84 -0
84 lines not on this page · raw message
Signature · 1 line
2.52.0
Re: [PATCH v7 1/5] bug/kunit: Core support for suppressing warning backtraces
Quoted 20 lines · Albert Esteve
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 identify and suppress specific warning backtraces while executing test code. Support suppressing multiple backtraces while at the same time limiting changes to generic code to the absolute minimum.
Quoted 8 lines
Implementation details:
Suppression is checked at two points in the warning path:
- Inwarn_slowpath_fmt(), the check runs before any output, fully suppressing both message and backtrace.
- In__report_bug(), the check runs before__warn()is called, suppressing the backtrace and stack dump. Note that on this path, theWARN()format message may still appear in the kernel log since__warn_printk()runs before the trap that enters__report_bug().
This is for architectures that implement __WARN_FLAGS but not __WARN_printf right? (which is arm64, loongarch, parisc, powerpc, riscv, sh, afaict). ARM64 should eventually get __WARN_printf, but other than that this should be fixable by moving __WARN_FLAGS() into __warn_printk() or so. This is the only __warn_printk() user anyway.
Quoted 38 lines
include/kunit/bug.h new file +32
-0@@ -0,0 +1,56 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* KUnit helpers for backtrace suppression +* +* Copyright (C) 2025 Alessandro Carminati <[email protected]> +* Copyright (C) 2024 Guenter Roeck <[email protected]> +*/ + +#ifndef _KUNIT_BUG_H +#define _KUNIT_BUG_H + +#ifndef __ASSEMBLY__ + +#include <linux/kconfig.h> + +struct kunit ; + +#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE + +#include <linux/types.h> + +struct task_struct ; + +struct __suppressed_warning { +struct list_head node; +struct task_struct *task; +int counter; +}; + +struct __suppressed_warning * +__kunit_start_suppress_warning (struct kunit *test); +void __kunit_end_suppress_warning(struct kunit *test,
include/kunit/bug.h line 42
Quoted 10 lines
include/kunit/bug.h new file +10
-0+ struct __suppressed_warning *warning); +int __kunit_suppressed_warning_count (struct __suppressed_warning *warning); +bool __kunit_is_suppressed_warning (void ); + +#define KUNIT_START_SUPPRESSED_WARNING (test) \ + struct __suppressed_warning *__kunit_suppress = \ + __kunit_start_suppress_warning(test) + +#define KUNIT_END_SUPPRESSED_WARNING (test) \ + __kunit_end_suppress_warning(test, __kunit_suppress)
We have __cleanup for this?
Quoted 68 lines
include/kunit/bug.h new file +14
-0+ +#define KUNIT_SUPPRESSED_WARNING_COUNT () \ + __kunit_suppressed_warning_count(__kunit_suppress) + +#else /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */ + +#define KUNIT_START_SUPPRESSED_WARNING (test) +#define KUNIT_END_SUPPRESSED_WARNING (test) +#define KUNIT_SUPPRESSED_WARNING_COUNT () 0 +static inline bool __kunit_is_suppressed_warning (void ) {return false; } + +#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */ +#endif /* __ASSEMBLY__ */ +#endif /* _KUNIT_BUG_H */ lib/kunit/Makefile +4
-2@@ -16,8 +16,10 @@ ifeq ($(CONFIG_KUNIT_DEBUGFS),y) kunit-objs += debugfs.o endif -# KUnit 'hooks' are built-in even when KUnit is built as a module. -obj-$(if $(CONFIG_KUNIT),y) += hooks.o +# KUnit 'hooks' and bug handling are built-in even when KUnit is built +# as a module. +obj-$(if $(CONFIG_KUNIT),y) += hooks.o \ + bug.o obj-$(CONFIG_KUNIT_TEST) += kunit-test.o obj-$(CONFIG_KUNIT_TEST) += platform-test.olib/kunit/bug.c new file +30
-0@@ -0,0 +1,84 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* KUnit helpers for backtrace suppression +* +* Copyright (C) 2025 Alessandro Carminati <[email protected]> +* Copyright (C) 2024 Guenter Roeck <[email protected]> +*/ + +#include <kunit/bug.h> +#include <kunit/resource.h> +#include <linux/export.h> +#include <linux/rculist.h> +#include <linux/sched.h> + +#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE + +static LIST_HEAD (suppressed_warnings); + +static void __kunit_suppress_warning_remove (struct __suppressed_warning *warning) +{ +list_del_rcu (&warning->node); +synchronize_rcu ();/* Wait for readers to finish */ +} + +KUNIT_DEFINE_ACTION_WRAPPER (__kunit_suppress_warning_cleanup, + __kunit_suppress_warning_remove, + struct __suppressed_warning *); + +struct __suppressed_warning * +__kunit_start_suppress_warning (struct kunit *test)
lib/kunit/bug.c line 40
Quoted 10 lines
lib/kunit/bug.c new file +10
-0+{ +struct __suppressed_warning *warning; +int ret; + + warning =kunit_kzalloc (test,sizeof (*warning),GFP_KERNEL ); +if (!warning) +return NULL ; + + warning->task = current; +list_add_rcu (&warning->node, &suppressed_warnings);
What if anything serializes this global list?
Quoted 48 lines
lib/kunit/bug.c new file +44
-0+ + ret =kunit_add_action_or_reset (test, + __kunit_suppress_warning_cleanup, + warning); +if (ret) +return NULL ; + + return warning; +} +EXPORT_SYMBOL_GPL (__kunit_start_suppress_warning); + +void __kunit_end_suppress_warning (struct kunit *test, +struct __suppressed_warning *warning) +{ +if (!warning) +return ; +kunit_release_action (test, __kunit_suppress_warning_cleanup, warning); +} +EXPORT_SYMBOL_GPL (__kunit_end_suppress_warning); + +int __kunit_suppressed_warning_count (struct __suppressed_warning *warning) +{ +return warning ? warning->counter :0 ; +} +EXPORT_SYMBOL_GPL (__kunit_suppressed_warning_count); + +bool __kunit_is_suppressed_warning (void ) +{ +struct __suppressed_warning *warning; + +rcu_read_lock (); +list_for_each_entry_rcu (warning, &suppressed_warnings, node) { +if (warning->task == current) { + warning->counter++; +rcu_read_unlock (); +return true; + } + } +rcu_read_unlock (); + +return false; +} + +#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */ Signature · 2 lines
-- 2.52.0
Re: [PATCH v7 1/5] bug/kunit: Core support for suppressing warning backtraces
Quoted 31 lines · Peter Zijlstra
Quoted 20 lines · Albert Esteve
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 identify and suppress specific warning backtraces while executing test code. Support suppressing multiple backtraces while at the same time limiting changes to generic code to the absolute minimum.
Quoted 8 lines
Implementation details:
Suppression is checked at two points in the warning path:
- Inwarn_slowpath_fmt(), the check runs before any output, fully suppressing both message and backtrace.
- In__report_bug(), the check runs before__warn()is called, suppressing the backtrace and stack dump. Note that on this path, theWARN()format message may still appear in the kernel log since__warn_printk()runs before the trap that enters__report_bug().
Quoted 5 lines
This is for architectures that implement
__WARN_FLAGSbut not__WARN_printfright? (which is arm64, loongarch, parisc, powerpc, riscv, sh, afaict). ARM64 should eventually get__WARN_printf, but other than that this should be fixable by moving__WARN_FLAGS()into__warn_printk()or so. This is the only__warn_printk()user anyway.
Right. On that path, __warn_printk() prints the message before __WARN_FLAGS() triggers the trap into __report_bug(), so the suppression check in __report_bug() only catches the backtrace.
Adding the suppression check to __warn_printk() as well should close this gap, since it's the only caller in the generic __WARN_printf path. Something like:
```
diff --git a/kernel/panic.c b/kernel/panic.c
index d7a7a679f56c4..cd73038b7c0bd 100644
--- a/kernel/panic.c
+++ b/kernel/panic.c
@@ -1108,9 +1108,14 @@ EXPORT_SYMBOL(warn_slowpath_fmt);
#else
void __warn_printk(const char *fmt, ...)
{
- bool rcu = warn_rcu_enter();
+ bool rcu;
va_list args;
+ if (__kunit_is_suppressed_warning())
+ return;
+
+ rcu = warn_rcu_enter();
+
pr_warn(CUT_HERE);
va_start(args, fmt);
```Note that the suppression counter tracking may need a small adjustment to avoid double-counting, since __report_bug() will still see the same warning through the trap path, but the approach itself should be straightforward.
Quoted 44 lines
Quoted 44 lines
include/kunit/bug.h new file +36
-0@@ -0,0 +1,56 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* KUnit helpers for backtrace suppression +* +* Copyright (C) 2025 Alessandro Carminati <[email protected]> +* Copyright (C) 2024 Guenter Roeck <[email protected]> +*/ + +#ifndef _KUNIT_BUG_H +#define _KUNIT_BUG_H + +#ifndef __ASSEMBLY__ + +#include <linux/kconfig.h> + +struct kunit ; + +#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE + +#include <linux/types.h> + +struct task_struct ; + +struct __suppressed_warning { +struct list_head node; +struct task_struct *task; +int counter; +}; + +struct __suppressed_warning * +__kunit_start_suppress_warning (struct kunit *test); +void __kunit_end_suppress_warning (struct kunit *test, +struct __suppressed_warning *warning); +int __kunit_suppressed_warning_count (struct __suppressed_warning *warning); +bool __kunit_is_suppressed_warning (void ); +
Quoted 8 lines
include/kunit/bug.h line 42
Quoted 6 lines
include/kunit/bug.h new file +6
-0+#define KUNIT_START_SUPPRESSED_WARNING (test) \ + struct __suppressed_warning *__kunit_suppress = \ + __kunit_start_suppress_warning(test) + +#define KUNIT_END_SUPPRESSED_WARNING (test) \ + __kunit_end_suppress_warning(test, __kunit_suppress)We have
__cleanupfor this?
I was unfamiliar with this attribute. I have looked into it a bit, and it could be a good fit. I saw a good example in seqlock. I could try to use it for the next version.
Quoted 73 lines
Quoted 73 lines
include/kunit/bug.h new file +14
-0+ +#define KUNIT_SUPPRESSED_WARNING_COUNT () \ + __kunit_suppressed_warning_count(__kunit_suppress) + +#else /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */ + +#define KUNIT_START_SUPPRESSED_WARNING (test) +#define KUNIT_END_SUPPRESSED_WARNING (test) +#define KUNIT_SUPPRESSED_WARNING_COUNT () 0 +static inline bool __kunit_is_suppressed_warning (void ) {return false; } + +#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */ +#endif /* __ASSEMBLY__ */ +#endif /* _KUNIT_BUG_H */ lib/kunit/Makefile +4
-2@@ -16,8 +16,10 @@ ifeq ($(CONFIG_KUNIT_DEBUGFS),y) kunit-objs += debugfs.o endif -# KUnit 'hooks' are built-in even when KUnit is built as a module. -obj-$(if $(CONFIG_KUNIT),y) += hooks.o +# KUnit 'hooks' and bug handling are built-in even when KUnit is built +# as a module. +obj-$(if $(CONFIG_KUNIT),y) += hooks.o \ + bug.o obj-$(CONFIG_KUNIT_TEST) += kunit-test.o obj-$(CONFIG_KUNIT_TEST) += platform-test.olib/kunit/bug.c new file +34
-0@@ -0,0 +1,84 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* KUnit helpers for backtrace suppression +* +* Copyright (C) 2025 Alessandro Carminati <[email protected]> +* Copyright (C) 2024 Guenter Roeck <[email protected]> +*/ + +#include <kunit/bug.h> +#include <kunit/resource.h> +#include <linux/export.h> +#include <linux/rculist.h> +#include <linux/sched.h> + +#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE + +static LIST_HEAD (suppressed_warnings); + +static void __kunit_suppress_warning_remove (struct __suppressed_warning *warning) +{ +list_del_rcu (&warning->node); +synchronize_rcu ();/* Wait for readers to finish */ +} + +KUNIT_DEFINE_ACTION_WRAPPER (__kunit_suppress_warning_cleanup, + __kunit_suppress_warning_remove, + struct __suppressed_warning *); + +struct __suppressed_warning * +__kunit_start_suppress_warning (struct kunit *test) +{ +struct __suppressed_warning *warning; +int ret; +
Quoted 8 lines
lib/kunit/bug.c line 40
Quoted 6 lines
lib/kunit/bug.c new file +6
-0+ warning =kunit_kzalloc (test,sizeof (*warning),GFP_KERNEL ); +if (!warning) +return NULL ; + + warning->task = current; +list_add_rcu (&warning->node, &suppressed_warnings);What if anything serializes this global list?
I considered adding a spinlock, but since the KUnit executor runs tests sequentially and it had not been a concern in previous versions, I left it out to keep the changeset minimal unless someone complains. If we want a hard guarantee, I'm happy to add the spinlock for the next version.
Quoted 50 lines
Quoted 49 lines
lib/kunit/bug.c new file +44
-1+ + ret =kunit_add_action_or_reset (test, + __kunit_suppress_warning_cleanup, + warning); +if (ret) +return NULL ; + + return warning; +} +EXPORT_SYMBOL_GPL (__kunit_start_suppress_warning); + +void __kunit_end_suppress_warning (struct kunit *test, +struct __suppressed_warning *warning) +{ +if (!warning) +return ; +kunit_release_action (test, __kunit_suppress_warning_cleanup, warning); +} +EXPORT_SYMBOL_GPL (__kunit_end_suppress_warning); + +int __kunit_suppressed_warning_count (struct __suppressed_warning *warning) +{ +return warning ? warning->counter :0 ; +} +EXPORT_SYMBOL_GPL (__kunit_suppressed_warning_count); + +bool __kunit_is_suppressed_warning (void ) +{ +struct __suppressed_warning *warning; + +rcu_read_lock (); +list_for_each_entry_rcu (warning, &suppressed_warnings, node) { +if (warning->task == current) { + warning->counter++; +rcu_read_unlock (); +return true; + } + } +rcu_read_unlock (); + +return false; +} + +#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */ --2.52.0
Re: [PATCH v7 1/5] bug/kunit: Core support for suppressing warning backtraces
In reply to Albert Estevelib/kunit/bug.c line 71
Quoted 5 lines · Albert Esteve
lib/kunit/bug.c new file +5
-0+bool __kunit_is_suppressed_warning (void ) +{ +struct __suppressed_warning *warning; + +rcu_read_lock ();
guard(rcu)();
Quoted 17 lines
lib/kunit/bug.c new file +13
-0+list_for_each_entry_rcu (warning, &suppressed_warnings, node) { +if (warning->task == current) { + warning->counter++; +rcu_read_unlock (); +return true; + } + } +rcu_read_unlock (); + + return false; +} + +#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */ Signature · 2 lines
-- 2.52.0
Re: [PATCH v7 1/5] bug/kunit: Core support for suppressing warning backtraces
Quoted 9 lines · Peter Zijlstra
lib/kunit/bug.c line 71
Quoted 5 lines · Albert Esteve
lib/kunit/bug.c new file +5
-0+bool __kunit_is_suppressed_warning (void ) +{ +struct __suppressed_warning *warning; + +rcu_read_lock ();guard(rcu)();
Nice. I saw plenty of examples using explicit calls, but I had missed the guard. Thanks for pointing it out.
Quoted 19 lines
Quoted 18 lines
lib/kunit/bug.c new file +13
-1+list_for_each_entry_rcu (warning, &suppressed_warnings, node) { +if (warning->task == current) { + warning->counter++; +rcu_read_unlock (); +return true; + } + } +rcu_read_unlock (); + + return false; +} + +#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */ --2.52.0
David Gow
maintainerRe: [PATCH v7 1/5] bug/kunit: Core support for suppressing warning backtraces
In reply to Albert EsteveThanks very much for keeping this series alive! I'm very much in favour of it, and I think the overall design is good. Lots of more detailed nitpicks below, though.
Le 20/04/2026 à 8:28 PM, Albert Esteve a écrit :
Quoted 15 lines
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.
Quoted 4 lines
Solve the problem by providing a means to identify and suppress specific warning backtraces while executing test code. Support suppressing multiple backtraces while at the same time limiting changes to generic code to the absolute minimum.
It sounds from the description here that suppressing "specific backtraces" means that we're matching on the _contents_ of the stack trace. This sort-of does that implicitly by checking they're in the same kthread, but I think the fact that it's matching based on kthread should be explicit in the commit message, like it is in the documentation.
Quoted 9 lines
Implementation details:
Suppression is checked at two points in the warning path:
- Inwarn_slowpath_fmt(), the check runs before any output, fully suppressing both message and backtrace.
- In__report_bug(), the check runs before__warn()is called, suppressing the backtrace and stack dump. Note that on this path, theWARN()format message may still appear in the kernel log since__warn_printk()runs before the trap that enters__report_bug().
Would it make sense to output a 'backtrace suppressed due to running test' message in this latter case, so we don't just end up with the WARN() format message by itself? (My gut feeling is 'no, it isn't worth it', but it's food for thought.)
include/kunit/bug.h
Quoted 31 lines
A helper function,
__kunit_is_suppressed_warning(), walks an RCU-protected list of active suppressions, matching by current task. The suppression state is tied to the KUnit test lifecycle viakunit_add_action(), ensuring automatic cleanup at test exit.The list of suppressed warnings is protected with RCU to allow concurrent read access without locks.
The implementation is deliberately simple and avoids architecture-specific optimizations to preserve portability.
Signed-off-by Guenter Roeck <[email protected]>Signed-off-by Alessandro Carminati <[email protected]>Reviewed-by Kees Cook <[email protected]>Signed-off-by Albert Esteve <[email protected]>--- include/kunit/bug.h | 56 +++++++++++++++++++++++++++++++++++ include/kunit/test.h | 1 + kernel/panic.c | 8 ++++- lib/bug.c | 8 +++++ lib/kunit/Kconfig | 9 ++++++ lib/kunit/Makefile | 6 ++-- lib/kunit/bug.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 169 insertions(+), 3 deletions(-)include/kunit/bug.h new file +0
-0
It's a bit confusing to name this bug.h when we have the (admittedly terribly-named) test-bug.h header already. I'm pretty tempted to rename the latter to something like 'hooks.h', as that's really what it's for, and having a separate bug.h would be an incentive to do so, though, sit it's not a big problem.
I do think that it'd be reasonable to include the backtrace suppression tuff in the same file, though, if you'd rather. The __kunit_is_suppressed_warning() stuff in particular fits the category of "code called to change behaviour based on whether or not a test is running", which is generally what the hooks are for. (And, if you'd rather, there's a bunch of existing hooks and hook infrastructure you could use.)
Quoted 36 lines
include/kunit/bug.h new file +35
-0@@ -0,0 +1,56 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* KUnit helpers for backtrace suppression +* +* Copyright (C) 2025 Alessandro Carminati <[email protected]> +* Copyright (C) 2024 Guenter Roeck <[email protected]> +*/ + +#ifndef _KUNIT_BUG_H +#define _KUNIT_BUG_H + +#ifndef __ASSEMBLY__ + +#include <linux/kconfig.h> + +struct kunit ; + +#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE + +#include <linux/types.h> + +struct task_struct ; + +struct __suppressed_warning { +struct list_head node; +struct task_struct *task; +int counter; +}; + +struct __suppressed_warning * +__kunit_start_suppress_warning (struct kunit *test); +void __kunit_end_suppress_warning (struct kunit *test, +struct __suppressed_warning *warning); +int __kunit_suppressed_warning_count (struct __suppressed_warning *warning); +bool __kunit_is_suppressed_warning (void );
include/kunit/bug.h line 45
Quoted 10 lines
include/kunit/bug.h new file +10
-0+ +#define KUNIT_START_SUPPRESSED_WARNING (test) \ + struct __suppressed_warning *__kunit_suppress = \ + __kunit_start_suppress_warning(test) + +#define KUNIT_END_SUPPRESSED_WARNING (test) \ + __kunit_end_suppress_warning(test, __kunit_suppress) + +#define KUNIT_SUPPRESSED_WARNING_COUNT () \ + __kunit_suppressed_warning_count(__kunit_suppress)
Using a local variable (__kunit_suppress) here means that all of the above macros must live in the same function. This is probably okay for most use-cases, but more complicated tests may have to structure things carefully. It also prevents there from being multiple START/END pairs in the same function, and the KUNIT_SUPPRESSED_WARNING_COUNT() macro from appearing before _START().
It also makes it less obvious that this cleans up nicely if the test exits uncleanly, as the variable will have gone out-of-scope. (But given we're just storing a pointer to heap-allocated memory, and kunit_add_action() is used, it should be okay.)
One other option would be to allocate the suppression as a named resource, which could then be retrieved from anywhere within the test with kunit_find_named_resource(). (Though handling nested suppressions gets a little more complicated here.)
Or you could make the struct __suppressed_warning pointer returned explicitly user-visible, and let the user pass it around (though that seems more work).
Quoted 64 lines
include/kunit/bug.h new file +11
-0+ +#else /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */ + +#define KUNIT_START_SUPPRESSED_WARNING (test) +#define KUNIT_END_SUPPRESSED_WARNING (test) +#define KUNIT_SUPPRESSED_WARNING_COUNT () 0 +static inline bool __kunit_is_suppressed_warning (void ) {return false; } + +#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */ +#endif /* __ASSEMBLY__ */ +#endif /* _KUNIT_BUG_H */ include/kunit/test.h +1
-0@@ -10,6 +10,7 @@#define _KUNIT_TEST_H#include <kunit/assert.h> +#include <kunit/bug.h> #include <kunit/try-catch.h> #include <linux/args.h> kernel/panic.c +7
-1@@ -39,6 +39,7 @@#include <linux/sys_info.h> #include <trace/events/error_report.h> #include <asm/sections.h> +#include <kunit/bug.h> #define PANIC_TIMER_STEP 100#define PANIC_BLINK_SPD 18@@ -1080,9 +1081,14 @@ void __warn(const char *file, int line, void *caller, unsigned taint,void warn_slowpath_fmt (const char *file,int line,unsigned taint,const char *fmt, ...) { -bool rcu =warn_rcu_enter (); +bool rcu;struct warn_args args; +if (__kunit_is_suppressed_warning ()) +return ; + + rcu =warn_rcu_enter (); +pr_warn (CUT_HERE );if (!fmt) {lib/bug.c +1
-0@@ -48,6 +48,7 @@#include <linux/rculist.h> #include <linux/ftrace.h> #include <linux/context_tracking.h> +#include <kunit/bug.h> extern struct bug_entry __start___bug_table[], __stop___bug_table[];
lib/bug.c static enum bug_trap_type __report_bug(struct bug_entry *bug, unsigned long buga · line 233
Quoted 10 lines
lib/bug.c +7
-0@@ -223,6 +224,13 @@ 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 ; +/* +* Before the once logic so suppressed warnings do not consume +* the single-fire budget of WARN_ON_ONCE(). +*/ +if (warning &&__kunit_is_suppressed_warning ()) +return BUG_TRAP_TYPE_WARN ; +
While any competant optimiser should get rid of this entirely, it might be clearer to anyone reading it that this disappears if we just put it behind an #ifdef?
Quoted 28 lines
lib/bug.c +0
-0if (warning && once) {if (done)return BUG_TRAP_TYPE_WARN ;lib/kunit/Kconfig +9
-0@@ -15,6 +15,15 @@ menuconfig KUNIT if KUNIT +config KUNIT_SUPPRESS_BACKTRACE + bool "KUnit - Enable backtrace suppression" + default y + help + Enable backtrace suppression for KUnit. If enabled, backtraces + generated intentionally by KUnit tests are suppressed. Disable + to reduce kernel image size if image size is more important than + suppression of backtraces generated by KUnit tests. + config KUNIT_DEBUGFS bool "KUnit - Enable /sys/kernel/debug/kunit debugfs representation" if !KUNIT_ALL_TESTS default KUNIT_ALL_TESTSlib/kunit/Makefile +0
-0
lib/kunit/Makefile ifeq ($(CONFIG_KUNIT_DEBUGFS),y) · line 22
Quoted 9 lines
lib/kunit/Makefile +4
-2@@ -16,8 +16,10 @@ ifeq ($(CONFIG_KUNIT_DEBUGFS),y) kunit-objs += debugfs.o endif -# KUnit 'hooks' are built-in even when KUnit is built as a module. -obj-$(if $(CONFIG_KUNIT),y) += hooks.o +# KUnit 'hooks' and bug handling are built-in even when KUnit is built +# as a module. +obj-$(if $(CONFIG_KUNIT),y) += hooks.o \ + bug.o
Is there any reason we couldn't implement this on top of the hooks mechanism? Then we could include the bug suppression code in the kunit.ko module (albeit, with fewer possibilities for the compiler to optimise things, as they'd have to go through an indirect pointer).
Quoted 81 lines
lib/kunit/Makefile +0
-0obj-$(CONFIG_KUNIT_TEST) += kunit-test.o obj-$(CONFIG_KUNIT_TEST) += platform-test.olib/kunit/bug.c new file +72
-0@@ -0,0 +1,84 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* KUnit helpers for backtrace suppression +* +* Copyright (C) 2025 Alessandro Carminati <[email protected]> +* Copyright (C) 2024 Guenter Roeck <[email protected]> +*/ + +#include <kunit/bug.h> +#include <kunit/resource.h> +#include <linux/export.h> +#include <linux/rculist.h> +#include <linux/sched.h> + +#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE + +static LIST_HEAD (suppressed_warnings); + +static void __kunit_suppress_warning_remove (struct __suppressed_warning *warning) +{ +list_del_rcu (&warning->node); +synchronize_rcu ();/* Wait for readers to finish */ +} + +KUNIT_DEFINE_ACTION_WRAPPER (__kunit_suppress_warning_cleanup, + __kunit_suppress_warning_remove, + struct __suppressed_warning *); + +struct __suppressed_warning * +__kunit_start_suppress_warning (struct kunit *test) +{ +struct __suppressed_warning *warning; +int ret; + + warning =kunit_kzalloc (test,sizeof (*warning),GFP_KERNEL ); +if (!warning) +return NULL ; + + warning->task = current; +list_add_rcu (&warning->node, &suppressed_warnings); + + ret =kunit_add_action_or_reset (test, + __kunit_suppress_warning_cleanup, + warning); +if (ret) +return NULL ; + +return warning; +} +EXPORT_SYMBOL_GPL (__kunit_start_suppress_warning); + +void __kunit_end_suppress_warning (struct kunit *test, +struct __suppressed_warning *warning) +{ +if (!warning) +return ; +kunit_release_action (test, __kunit_suppress_warning_cleanup, warning); +} +EXPORT_SYMBOL_GPL (__kunit_end_suppress_warning); + +int __kunit_suppressed_warning_count (struct __suppressed_warning *warning) +{ +return warning ? warning->counter :0 ; +} +EXPORT_SYMBOL_GPL (__kunit_suppressed_warning_count); + +bool __kunit_is_suppressed_warning (void ) +{ +struct __suppressed_warning *warning; + +rcu_read_lock (); +list_for_each_entry_rcu (warning, &suppressed_warnings, node) {
lib/kunit/bug.c line 82
Quoted 10 lines
lib/kunit/bug.c new file +10
-0+if (warning->task == current) { + warning->counter++; +rcu_read_unlock (); +return true; + } + } +rcu_read_unlock (); + +return false; +}
Note to self: this is _not_ exported, as bug.c is being built-in regardless of whether or not KUnit is a module. If we used the hook system, it could live in kunit.ko, and would be manually exported by kunit_install_hooks()
lib/kunit/bug.c line 84
Quoted 3 lines
lib/kunit/bug.c new file +2
-0+ +#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */
Thanks again,
-- David
Re: [PATCH v7 1/5] bug/kunit: Core support for suppressing warning backtraces
Quoted 5 lines · David Gow
Thanks very much for keeping this series alive! I'm very much in favour of it, and I think the overall design is good. Lots of more detailed nitpicks below, though.
Hi David,
Thank you for taking the time to review the series!
Quoted 21 lines · Albert Esteve
Quoted 15 lines
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.Quoted 4 lines
Solve the problem by providing a means to identify and suppress specific warning backtraces while executing test code. Support suppressing multiple backtraces while at the same time limiting changes to generic code to the absolute minimum.
Quoted 6 lines
It sounds from the description here that suppressing "specific backtraces" means that we're matching on the _contents_ of the stack trace. This sort-of does that implicitly by checking they're in the same kthread, but I think the fact that it's matching based on kthread should be explicit in the commit message, like it is in the documentation.
You are right. Previous version was based on function name matches. I mostly fixed all commit messages, but this section remained. I will rewrite it.
Quoted 10 lines
Quoted 9 lines
Implementation details:
Suppression is checked at two points in the warning path:
- Inwarn_slowpath_fmt(), the check runs before any output, fully suppressing both message and backtrace.
- In__report_bug(), the check runs before__warn()is called, suppressing the backtrace and stack dump. Note that on this path, theWARN()format message may still appear in the kernel log since__warn_printk()runs before the trap that enters__report_bug().
Quoted 5 lines
Would it make sense to output a 'backtrace suppressed due to running test' message in this latter case, so we don't just end up with the
WARN()format message by itself? (My gut feeling is 'no, it isn't worth it', but it's food for thought.)
I had the same thought and landed on the same conclusion. It would also become moot if we add the suppression check to __warn_printk() as discussed with Peter, since the message would be fully suppressed on that path too.
Quoted 38 lines
include/kunit/bug.h
Quoted 31 lines
A helper function,
__kunit_is_suppressed_warning(), walks an RCU-protected list of active suppressions, matching by current task. The suppression state is tied to the KUnit test lifecycle viakunit_add_action(), ensuring automatic cleanup at test exit.The list of suppressed warnings is protected with RCU to allow concurrent read access without locks.
The implementation is deliberately simple and avoids architecture-specific optimizations to preserve portability.
Signed-off-by Guenter Roeck <[email protected]>Signed-off-by Alessandro Carminati <[email protected]>Reviewed-by Kees Cook <[email protected]>Signed-off-by Albert Esteve <[email protected]>--- include/kunit/bug.h | 56 +++++++++++++++++++++++++++++++++++ include/kunit/test.h | 1 + kernel/panic.c | 8 ++++- lib/bug.c | 8 +++++ lib/kunit/Kconfig | 9 ++++++ lib/kunit/Makefile | 6 ++-- lib/kunit/bug.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 169 insertions(+), 3 deletions(-)include/kunit/bug.h new file +0
-0It's a bit confusing to name this
bug.hwhen we have the (admittedly terribly-named)test-bug.hheader already. I'm pretty tempted to rename the latter to something like 'hooks.h', as that's really what it's for, and having a separatebug.hwould be an incentive to do so, though, sit it's not a big problem.
Quoted 7 lines
I do think that it'd be reasonable to include the backtrace suppression tuff in the same file, though, if you'd rather. The
__kunit_is_suppressed_warning()stuff in particular fits the category of "code called to change behaviour based on whether or not a test is running", which is generally what the hooks are for. (And, if you'd rather, there's a bunch of existing hooks and hook infrastructure you could use.)
I wasn't aware of the hooks infrastructure! I was so focused on the solution carried over from initial versions that I did not seek alternatives.
Honestly, that's a much better fit -- I'll integrate it into test-bug.h for next version.
Quoted 60 lines
Quoted 37 lines
include/kunit/bug.h new file +35
-0@@ -0,0 +1,56 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* KUnit helpers for backtrace suppression +* +* Copyright (C) 2025 Alessandro Carminati <[email protected]> +* Copyright (C) 2024 Guenter Roeck <[email protected]> +*/ + +#ifndef _KUNIT_BUG_H +#define _KUNIT_BUG_H + +#ifndef __ASSEMBLY__ + +#include <linux/kconfig.h> + +struct kunit ; + +#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE + +#include <linux/types.h> + +struct task_struct ; + +struct __suppressed_warning { +struct list_head node; +struct task_struct *task; +int counter; +}; + +struct __suppressed_warning * +__kunit_start_suppress_warning (struct kunit *test); +void __kunit_end_suppress_warning (struct kunit *test, +struct __suppressed_warning *warning); +int __kunit_suppressed_warning_count (struct __suppressed_warning *warning); +bool __kunit_is_suppressed_warning (void );include/kunit/bug.h line 45
Quoted 10 lines
include/kunit/bug.h new file +10
-0+ +#define KUNIT_START_SUPPRESSED_WARNING (test) \ + struct __suppressed_warning *__kunit_suppress = \ + __kunit_start_suppress_warning(test) + +#define KUNIT_END_SUPPRESSED_WARNING (test) \ + __kunit_end_suppress_warning(test, __kunit_suppress) + +#define KUNIT_SUPPRESSED_WARNING_COUNT () \ + __kunit_suppressed_warning_count(__kunit_suppress)Using a local variable (
__kunit_suppress) here means that all of the above macros must live in the same function. This is probably okay for most use-cases, but more complicated tests may have to structure things carefully. It also prevents there from being multiple START/END pairs in the same function, and theKUNIT_SUPPRESSED_WARNING_COUNT()macro from appearing before_START().It also makes it less obvious that this cleans up nicely if the test exits uncleanly, as the variable will have gone out-of-scope. (But given we're just storing a pointer to heap-allocated memory, and
kunit_add_action()is used, it should be okay.)
Quoted 9 lines
One other option would be to allocate the suppression as a named resource, which could then be retrieved from anywhere within the test with
kunit_find_named_resource(). (Though handling nested suppressions gets a little more complicated here.)Or you could make the struct
__suppressed_warningpointer returned explicitly user-visible, and let the user pass it around (though that seems more work).
Good points. The function matching approach used the function name to allow concurrent suppressions to coexist. This is a limitation of the current approach that I traded off for simplicity (without properly considering the more complicated scenarios). I'll add a name parameter to the macros (or expose the pointer directly) so multiple pairs and cross-function usage work naturally. I think I lean more towards making the return value user-visible? But I will decide based on how the API evolves, especially if I change to the scoped approach suggested in a different thread.
Quoted 68 lines
Quoted 68 lines
include/kunit/bug.h new file +11
-0+ +#else /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */ + +#define KUNIT_START_SUPPRESSED_WARNING (test) +#define KUNIT_END_SUPPRESSED_WARNING (test) +#define KUNIT_SUPPRESSED_WARNING_COUNT () 0 +static inline bool __kunit_is_suppressed_warning (void ) {return false; } + +#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */ +#endif /* __ASSEMBLY__ */ +#endif /* _KUNIT_BUG_H */ include/kunit/test.h +1
-0@@ -10,6 +10,7 @@#define _KUNIT_TEST_H#include <kunit/assert.h> +#include <kunit/bug.h> #include <kunit/try-catch.h> #include <linux/args.h> kernel/panic.c +7
-1@@ -39,6 +39,7 @@#include <linux/sys_info.h> #include <trace/events/error_report.h> #include <asm/sections.h> +#include <kunit/bug.h> #define PANIC_TIMER_STEP 100#define PANIC_BLINK_SPD 18@@ -1080,9 +1081,14 @@ void __warn(const char *file, int line, void *caller, unsigned taint,void warn_slowpath_fmt (const char *file,int line,unsigned taint,const char *fmt, ...) { -bool rcu =warn_rcu_enter (); +bool rcu;struct warn_args args; +if (__kunit_is_suppressed_warning ()) +return ; + + rcu =warn_rcu_enter (); +pr_warn (CUT_HERE );if (!fmt) {lib/bug.c +1
-0@@ -48,6 +48,7 @@#include <linux/rculist.h> #include <linux/ftrace.h> #include <linux/context_tracking.h> +#include <kunit/bug.h> extern struct bug_entry __start___bug_table[], __stop___bug_table[];@@ -223,6 +224,13 @@ 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 ;
Quoted 11 lines
lib/bug.c static enum bug_trap_type __report_bug(struct bug_entry *bug, unsigned long buga · line 233
Quoted 7 lines
lib/bug.c +7
-0+/* +* Before the once logic so suppressed warnings do not consume +* the single-fire budget of WARN_ON_ONCE(). +*/ +if (warning &&__kunit_is_suppressed_warning ()) +return BUG_TRAP_TYPE_WARN ; +While any competant optimiser should get rid of this entirely, it might be clearer to anyone reading it that this disappears if we just put it behind an #ifdef?
Makes sense. Will do.
Quoted 32 lines
Quoted 32 lines
lib/bug.c +0
-0if (warning && once) {if (done)return BUG_TRAP_TYPE_WARN ;lib/kunit/Kconfig +9
-0@@ -15,6 +15,15 @@ menuconfig KUNIT if KUNIT +config KUNIT_SUPPRESS_BACKTRACE + bool "KUnit - Enable backtrace suppression" + default y + help + Enable backtrace suppression for KUnit. If enabled, backtraces + generated intentionally by KUnit tests are suppressed. Disable + to reduce kernel image size if image size is more important than + suppression of backtraces generated by KUnit tests. + config KUNIT_DEBUGFS bool "KUnit - Enable /sys/kernel/debug/kunit debugfs representation" if !KUNIT_ALL_TESTS default KUNIT_ALL_TESTSlib/kunit/Makefile +0
-0@@ -16,8 +16,10 @@ ifeq ($(CONFIG_KUNIT_DEBUGFS),y) kunit-objs += debugfs.o endif
Quoted 12 lines
lib/kunit/Makefile ifeq ($(CONFIG_KUNIT_DEBUGFS),y) · line 22
Quoted 6 lines
lib/kunit/Makefile +4
-2-# KUnit 'hooks' are built-in even when KUnit is built as a module. -obj-$(if $(CONFIG_KUNIT),y) += hooks.o +# KUnit 'hooks' and bug handling are built-in even when KUnit is built +# as a module. +obj-$(if $(CONFIG_KUNIT),y) += hooks.o \ + bug.oIs there any reason we couldn't implement this on top of the hooks mechanism? Then we could include the bug suppression code in the kunit.ko module (albeit, with fewer possibilities for the compiler to optimise things, as they'd have to go through an indirect pointer).
No reason. I'll integrate this feature into the hooks infrastructure. Thanks for the suggestion!
Quoted 106 lines
Quoted 82 lines
lib/kunit/Makefile +0
-0obj-$(CONFIG_KUNIT_TEST) += kunit-test.o obj-$(CONFIG_KUNIT_TEST) += platform-test.olib/kunit/bug.c new file +72
-0@@ -0,0 +1,84 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* KUnit helpers for backtrace suppression +* +* Copyright (C) 2025 Alessandro Carminati <[email protected]> +* Copyright (C) 2024 Guenter Roeck <[email protected]> +*/ + +#include <kunit/bug.h> +#include <kunit/resource.h> +#include <linux/export.h> +#include <linux/rculist.h> +#include <linux/sched.h> + +#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE + +static LIST_HEAD (suppressed_warnings); + +static void __kunit_suppress_warning_remove (struct __suppressed_warning *warning) +{ +list_del_rcu (&warning->node); +synchronize_rcu ();/* Wait for readers to finish */ +} + +KUNIT_DEFINE_ACTION_WRAPPER (__kunit_suppress_warning_cleanup, + __kunit_suppress_warning_remove, + struct __suppressed_warning *); + +struct __suppressed_warning * +__kunit_start_suppress_warning (struct kunit *test) +{ +struct __suppressed_warning *warning; +int ret; + + warning =kunit_kzalloc (test,sizeof (*warning),GFP_KERNEL ); +if (!warning) +return NULL ; + + warning->task = current; +list_add_rcu (&warning->node, &suppressed_warnings); + + ret =kunit_add_action_or_reset (test, + __kunit_suppress_warning_cleanup, + warning); +if (ret) +return NULL ; + +return warning; +} +EXPORT_SYMBOL_GPL (__kunit_start_suppress_warning); + +void __kunit_end_suppress_warning (struct kunit *test, +struct __suppressed_warning *warning) +{ +if (!warning) +return ; +kunit_release_action (test, __kunit_suppress_warning_cleanup, warning); +} +EXPORT_SYMBOL_GPL (__kunit_end_suppress_warning); + +int __kunit_suppressed_warning_count (struct __suppressed_warning *warning) +{ +return warning ? warning->counter :0 ; +} +EXPORT_SYMBOL_GPL (__kunit_suppressed_warning_count); + +bool __kunit_is_suppressed_warning (void ) +{ +struct __suppressed_warning *warning; + +rcu_read_lock (); +list_for_each_entry_rcu (warning, &suppressed_warnings, node) {lib/kunit/bug.c line 82
Quoted 10 lines
lib/kunit/bug.c new file +10
-0+if (warning->task == current) { + warning->counter++; +rcu_read_unlock (); +return true; + } + } +rcu_read_unlock (); + +return false; +}Note to self: this is _not_ exported, as
bug.cis being built-in regardless of whether or not KUnit is a module. If we used the hook system, it could live in kunit.ko, and would be manually exported bykunit_install_hooks()lib/kunit/bug.c line 84
Quoted 3 lines
lib/kunit/bug.c new file +2
-0+ +#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */ Thanks again,
-- David
PATCH v7 2/5 bug/kunit: Reduce runtime impact of warning backtrace suppression
In reply to Albert EsteveFrom: Alessandro Carminati <[email protected]>
KUnit support is not consistently present across distributions, some include it in their stock kernels, while others do not. While both KUNIT and KUNIT_SUPPRESS_BACKTRACE can be considered debug features, the fact that some distros ship with KUnit enabled means it's important to minimize the runtime impact of this patch.
To that end, this patch adds an atomic counter that tracks the number of active suppressions. __kunit_is_suppressed_warning() checks this counter first and returns immediately when no suppressions are active, avoiding RCU-protected list traversal in the common case.
--- lib/kunit/bug.c | 7 +++++++ 1 file changed, 7 insertions(+)
lib/kunit/bug.c +7 -0
@@ -8,6 +8,7 @@#include <kunit/bug.h> #include <kunit/resource.h> +#include <linux/atomic.h> #include <linux/export.h> #include <linux/rculist.h> #include <linux/sched.h>
@@ -15,11 +16,13 @@#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE static LIST_HEAD (suppressed_warnings); +static atomic_t suppressed_warnings_cnt =ATOMIC_INIT (0 );static void __kunit_suppress_warning_remove (struct __suppressed_warning *warning) {list_del_rcu (&warning->node);synchronize_rcu ();/* Wait for readers to finish */ +atomic_dec (&suppressed_warnings_cnt); } KUNIT_DEFINE_ACTION_WRAPPER(__kunit_suppress_warning_cleanup,
@@ -37,6 +40,7 @@ __kunit_start_suppress_warning(struct kunit *test)return NULL ; warning->task = current; +atomic_inc (&suppressed_warnings_cnt);list_add_rcu (&warning->node, &suppressed_warnings); ret = kunit_add_action_or_reset(test,
@@ -68,6 +72,9 @@ bool __kunit_is_suppressed_warning(void) {struct __suppressed_warning *warning; +if (!atomic_read (&suppressed_warnings_cnt)) +return false; +rcu_read_lock ();list_for_each_entry_rcu (warning, &suppressed_warnings, node) {if (warning->task == current) {
Signature · 1 line
2.52.0
Re: [PATCH v7 2/5] bug/kunit: Reduce runtime impact of warning backtrace suppression
Quoted 55 lines · Albert Esteve
From: Alessandro Carminati <[email protected]>
KUnit support is not consistently present across distributions, some include it in their stock kernels, while others do not. While both KUNIT and
KUNIT_SUPPRESS_BACKTRACEcan be considered debug features, the fact that some distros ship with KUnit enabled means it's important to minimize the runtime impact of this patch.To that end, this patch adds an atomic counter that tracks the number of active suppressions.
__kunit_is_suppressed_warning()checks this counter first and returns immediately when no suppressions are active, avoiding RCU-protected list traversal in the common case.Signed-off-by Alessandro Carminati <[email protected]>Signed-off-by Albert Esteve <[email protected]>--- lib/kunit/bug.c | 7 +++++++ 1 file changed, 7 insertions(+)lib/kunit/bug.c +4
-0@@ -8,6 +8,7 @@#include <kunit/bug.h> #include <kunit/resource.h> +#include <linux/atomic.h> #include <linux/export.h> #include <linux/rculist.h> #include <linux/sched.h> @@ -15,11 +16,13 @@#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE static LIST_HEAD (suppressed_warnings); +static atomic_t suppressed_warnings_cnt =ATOMIC_INIT (0 );static void __kunit_suppress_warning_remove (struct __suppressed_warning *warning) {list_del_rcu (&warning->node);synchronize_rcu ();/* Wait for readers to finish */ +atomic_dec (&suppressed_warnings_cnt); } KUNIT_DEFINE_ACTION_WRAPPER(__kunit_suppress_warning_cleanup,@@ -37,6 +40,7 @@ __kunit_start_suppress_warning(struct kunit *test)return NULL ; warning->task = current; +atomic_inc (&suppressed_warnings_cnt);list_add_rcu (&warning->node, &suppressed_warnings); ret = kunit_add_action_or_reset(test,
lib/kunit/bug.c bool __kunit_is_suppressed_warning(void) · line 80
Quoted 9 lines
lib/kunit/bug.c +3
-0@@ -68,6 +72,9 @@ bool __kunit_is_suppressed_warning(void) {struct __suppressed_warning *warning; +if (!atomic_read (&suppressed_warnings_cnt)) +return false; +rcu_read_lock ();list_for_each_entry_rcu (warning, &suppressed_warnings, node) {if (warning->task == current) {
So the thing you're skipping is:
rcu_read_lock();
list_for_each_entry_rcu() {
}
rcu_read_unlock();Which is really cheap. Did you actually have performance numbers for this?
A possibly better option is to add a static_branch() that could elide any and all memory access.
Re: [PATCH v7 2/5] bug/kunit: Reduce runtime impact of warning backtrace suppression
Quoted 69 lines · Peter Zijlstra
Quoted 55 lines · Albert Esteve
From: Alessandro Carminati <[email protected]>
KUnit support is not consistently present across distributions, some include it in their stock kernels, while others do not. While both KUNIT and
KUNIT_SUPPRESS_BACKTRACEcan be considered debug features, the fact that some distros ship with KUnit enabled means it's important to minimize the runtime impact of this patch.To that end, this patch adds an atomic counter that tracks the number of active suppressions.
__kunit_is_suppressed_warning()checks this counter first and returns immediately when no suppressions are active, avoiding RCU-protected list traversal in the common case.Signed-off-by Alessandro Carminati <[email protected]>Signed-off-by Albert Esteve <[email protected]>--- lib/kunit/bug.c | 7 +++++++ 1 file changed, 7 insertions(+)lib/kunit/bug.c +4
-0@@ -8,6 +8,7 @@#include <kunit/bug.h> #include <kunit/resource.h> +#include <linux/atomic.h> #include <linux/export.h> #include <linux/rculist.h> #include <linux/sched.h> @@ -15,11 +16,13 @@#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE static LIST_HEAD (suppressed_warnings); +static atomic_t suppressed_warnings_cnt =ATOMIC_INIT (0 );static void __kunit_suppress_warning_remove (struct __suppressed_warning *warning) {list_del_rcu (&warning->node);synchronize_rcu ();/* Wait for readers to finish */ +atomic_dec (&suppressed_warnings_cnt); } KUNIT_DEFINE_ACTION_WRAPPER(__kunit_suppress_warning_cleanup,@@ -37,6 +40,7 @@ __kunit_start_suppress_warning(struct kunit *test)return NULL ; warning->task = current; +atomic_inc (&suppressed_warnings_cnt);list_add_rcu (&warning->node, &suppressed_warnings); ret = kunit_add_action_or_reset(test,lib/kunit/bug.c bool __kunit_is_suppressed_warning(void) · line 80
Quoted 9 lines
lib/kunit/bug.c +3
-0@@ -68,6 +72,9 @@ bool __kunit_is_suppressed_warning(void) {struct __suppressed_warning *warning; +if (!atomic_read (&suppressed_warnings_cnt)) +return false; +rcu_read_lock ();list_for_each_entry_rcu (warning, &suppressed_warnings, node) {if (warning->task == current) {So the thing you're skipping is:
Quoted 7 lines
rcu_read_lock(); list_for_each_entry_rcu() { } rcu_read_unlock();Which is really cheap. Did you actually have performance numbers for this?
No, I do not have performance numbers. I kept the counter and the separate patch for consistency with the previous version of the series. But you have a good point, the skipped part is really cheap.
Quoted 4 lines
A possibly better option is to add a
static_branch()that could elide any and all memory access.
Previous version had static_branch and I removed it because I understood from the discussion that the gains would not be significant as performance gains are irrelevant in warn slowpath. But I think it would make sense for a disabled feature. I will rework this for the next version, remove the counter and use static_branch as suggested.
David Gow
maintainerRe: [PATCH v7 2/5] bug/kunit: Reduce runtime impact of warning backtrace suppression
In reply to Albert EsteveLe 20/04/2026 à 8:28 PM, Albert Esteve a écrit :
Quoted 8 lines
From: Alessandro Carminati <[email protected]>
KUnit support is not consistently present across distributions, some include it in their stock kernels, while others do not. While both KUNIT and
KUNIT_SUPPRESS_BACKTRACEcan be considered debug features, the fact that some distros ship with KUnit enabled means it's important to minimize the runtime impact of this patch.
Quoted 8 lines
To that end, this patch adds an atomic counter that tracks the number of active suppressions.
__kunit_is_suppressed_warning()checks this counter first and returns immediately when no suppressions are active, avoiding RCU-protected list traversal in the common case.Signed-off-by Alessandro Carminati <[email protected]>Signed-off-by Albert Esteve <[email protected]>---
I'm not sure how much of an improvement this is.
It might be worth putting things behind the kunit_running static branch.
You could even add a new kunit_has_suppressed_warnings static branch, though I doubt anyone's too worried about the runtime performance of WARN() during testing, but with no suppressions, so it's likely not worth it.
Have a look at, e.g., kunit_fail_current_test() in include/kunit/test-bug.h, which uses the existing hooks implementation and static branch to reduce the overhead as much as I'd expect we need. That'd also potentially let you move the backtrace suppression code into the kunit.ko module, so you're not even paying more than ~a pointer's worth of memory if no tests are even loaded.
Cheers,
-- David
Re: [PATCH v7 2/5] bug/kunit: Reduce runtime impact of warning backtrace suppression
Quoted 28 lines · David Gow
Quoted 8 lines · Albert Esteve
From: Alessandro Carminati <[email protected]>
KUnit support is not consistently present across distributions, some include it in their stock kernels, while others do not. While both KUNIT and
KUNIT_SUPPRESS_BACKTRACEcan be considered debug features, the fact that some distros ship with KUnit enabled means it's important to minimize the runtime impact of this patch.Quoted 8 lines
To that end, this patch adds an atomic counter that tracks the number of active suppressions.
__kunit_is_suppressed_warning()checks this counter first and returns immediately when no suppressions are active, avoiding RCU-protected list traversal in the common case.Signed-off-by Alessandro Carminati <[email protected]>Signed-off-by Albert Esteve <[email protected]>---
I'm not sure how much of an improvement this is.
It might be worth putting things behind the
kunit_runningstatic branch.You could even add a new
kunit_has_suppressed_warningsstatic branch, though I doubt anyone's too worried about the runtime performance ofWARN()during testing, but with no suppressions, so it's likely not worth it.
Quoted 6 lines
Have a look at, e.g.,
kunit_fail_current_test()ininclude/kunit/test-bug.h, which uses the existing hooks implementation and static branch to reduce the overhead as much as I'd expect we need. That'd also potentially let you move the backtrace suppression code into the kunit.ko module, so you're not even paying more than ~a pointer's worth of memory if no tests are even loaded.
Agreed. This patch can be removed entirely once I integrate it into the hooks infrastructure as discussed. The kunit_running static branch is indeed a better fast-path than the counter, and leverages existing architecture.
Quoted 4 lines
Cheers,
-- David
PATCH v7 3/5 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, covering WARN() and WARN_ON() with direct calls, indirect calls through helper functions, and multiple warnings in a single window.
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 | 3 ++ lib/kunit/backtrace-suppression-test.c | 90 ++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+)
lib/kunit/Makefile +3 -0
@@ -23,6 +23,9 @@ obj-$(if $(CONFIG_KUNIT),y) += hooks.o \ obj-$(CONFIG_KUNIT_TEST) += kunit-test.o obj-$(CONFIG_KUNIT_TEST) += platform-test.o +ifeq ($(CONFIG_KUNIT_SUPPRESS_BACKTRACE),y) +obj-$(CONFIG_KUNIT_TEST) += backtrace-suppression-test.o +endif # string-stream-test compiles built-in only. ifeq ($(CONFIG_KUNIT_TEST),y)
lib/kunit/backtrace-suppression-test.c new file +90 -0
90 lines not on this page · raw message
Signature · 1 line
2.52.0
Re: [PATCH v7 3/5] kunit: Add backtrace suppression self-tests
Le 20/04/2026 à 8:28 PM, Albert Esteve a écrit :
Quoted 10 lines
From: Guenter Roeck <[email protected]>
Add unit tests to verify that warning backtrace suppression works, covering
WARN()andWARN_ON()with direct calls, indirect calls through helper functions, and multiple warnings in a single window.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.
Quoted 7 lines
Acked-by Dan Carpenter <[email protected]>Reviewed-by Kees Cook <[email protected]>Signed-off-by Guenter Roeck <[email protected]>Signed-off-by Alessandro Carminati <[email protected]>Signed-off-by Albert Esteve <[email protected]>---
Thanks very much for including tests!
Maybe it'd be nice to test that the suppression is disabled after KUNIT_END_SUPPRESSED_WARNING(). Of course, then triggering an actual stacktrace would be a pain, but maybe we could check that __kunit_is_suppressed_warning() returns false? If you wanted to be really fancy, you could test that it returns false on another kthread even while the suppression is active, too, but I won't hold you to it. Equally, you could try setting up a fake test context and ensuring the cleanup is called correctly, but I think that's mostly covered by the existing KUnit resource tests.
Otherwise, looking good. A couple of other minor suggestions below, which may require some reworking of the __kunit_suppress scope, but all optional suggestions.
Quoted 56 lines
lib/kunit/Makefile | 3 ++ lib/kunit/backtrace-suppression-test.c | 90 ++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+)lib/kunit/Makefile +3
-0@@ -23,6 +23,9 @@ obj-$(if $(CONFIG_KUNIT),y) += hooks.o \ obj-$(CONFIG_KUNIT_TEST) += kunit-test.o obj-$(CONFIG_KUNIT_TEST) += platform-test.o +ifeq ($(CONFIG_KUNIT_SUPPRESS_BACKTRACE),y) +obj-$(CONFIG_KUNIT_TEST) += backtrace-suppression-test.o +endif # string-stream-test compiles built-in only. ifeq ($(CONFIG_KUNIT_TEST),y)lib/kunit/backtrace-suppression-test.c new file +32
-0@@ -0,0 +1,90 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* KUnit test for suppressing warning tracebacks. +* +* Copyright (C) 2024, Guenter Roeck +* Author: Guenter Roeck <[email protected]> +*/ + +#include <kunit/test.h> +#include <linux/bug.h> + +static void backtrace_suppression_test_warn_direct (struct kunit *test) +{ +KUNIT_START_SUPPRESSED_WARNING (test); +WARN (1 ,"This backtrace should be suppressed" ); +KUNIT_END_SUPPRESSED_WARNING (test); + +KUNIT_EXPECT_EQ (test,KUNIT_SUPPRESSED_WARNING_COUNT (),1 ); +} + +static void trigger_backtrace_warn (void ) +{ +WARN (1 ,"This backtrace should be suppressed" ); +} + +static void backtrace_suppression_test_warn_indirect (struct kunit *test) +{ +KUNIT_START_SUPPRESSED_WARNING (test); +trigger_backtrace_warn (); +KUNIT_END_SUPPRESSED_WARNING (test); + +KUNIT_EXPECT_EQ (test,KUNIT_SUPPRESSED_WARNING_COUNT (),1 );
lib/kunit/backtrace-suppression-test.c line 42
Quoted 10 lines
lib/kunit/backtrace-suppression-test.c new file +10
-0+} + +static void backtrace_suppression_test_warn_multi (struct kunit *test) +{ +KUNIT_START_SUPPRESSED_WARNING (test); +WARN (1 ,"This backtrace should be suppressed" ); +trigger_backtrace_warn (); +KUNIT_END_SUPPRESSED_WARNING (test); + +KUNIT_EXPECT_EQ (test,KUNIT_SUPPRESSED_WARNING_COUNT (),2 );
Would it make sense to test KUNIT_SUPPRESSED_WARNING_COUNT() more thoroughly here by checking that it's 0 before any warnings, and checking that it's 1 in-between the two warnings?
Of course, the first case doesn't work due to __kunit_suppress not being defined, but if the implementation changes to support this, let's add it to the test, too.
Quoted 49 lines
lib/kunit/backtrace-suppression-test.c new file +48
-0+} + +static void backtrace_suppression_test_warn_on_direct (struct kunit *test) +{ +if (!IS_ENABLED (CONFIG_DEBUG_BUGVERBOSE ) && !IS_ENABLED (CONFIG_KALLSYMS )) +kunit_skip (test,"requires CONFIG_DEBUG_BUGVERBOSE or CONFIG_KALLSYMS" ); + +KUNIT_START_SUPPRESSED_WARNING (test); +WARN_ON (1 ); +KUNIT_END_SUPPRESSED_WARNING (test); + +KUNIT_EXPECT_EQ (test,KUNIT_SUPPRESSED_WARNING_COUNT (),1 ); +} + +static void trigger_backtrace_warn_on (void ) +{ +WARN_ON (1 ); +} + +static void backtrace_suppression_test_warn_on_indirect (struct kunit *test) +{ +if (!IS_ENABLED (CONFIG_DEBUG_BUGVERBOSE )) +kunit_skip (test,"requires CONFIG_DEBUG_BUGVERBOSE" ); + +KUNIT_START_SUPPRESSED_WARNING (test); +trigger_backtrace_warn_on (); +KUNIT_END_SUPPRESSED_WARNING (test); + +KUNIT_EXPECT_EQ (test,KUNIT_SUPPRESSED_WARNING_COUNT (),1 ); +} + +static struct kunit_case backtrace_suppression_test_cases[] = { +KUNIT_CASE (backtrace_suppression_test_warn_direct), +KUNIT_CASE (backtrace_suppression_test_warn_indirect), +KUNIT_CASE (backtrace_suppression_test_warn_multi), +KUNIT_CASE (backtrace_suppression_test_warn_on_direct), +KUNIT_CASE (backtrace_suppression_test_warn_on_indirect), + {} +}; + +static struct kunit_suite backtrace_suppression_test_suite = { + .name ="backtrace-suppression-test" , + .test_cases = backtrace_suppression_test_cases, +}; +kunit_test_suites (&backtrace_suppression_test_suite); + +MODULE_LICENSE ("GPL" ); +MODULE_DESCRIPTION ("KUnit test to verify warning backtrace suppression" );
Re: [PATCH v7 3/5] kunit: Add backtrace suppression self-tests
Quoted 22 lines · David Gow
Quoted 10 lines · Albert Esteve
From: Guenter Roeck <[email protected]>
Add unit tests to verify that warning backtrace suppression works, covering
WARN()andWARN_ON()with direct calls, indirect calls through helper functions, and multiple warnings in a single window.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.
Quoted 7 lines
Acked-by Dan Carpenter <[email protected]>Reviewed-by Kees Cook <[email protected]>Signed-off-by Guenter Roeck <[email protected]>Signed-off-by Alessandro Carminati <[email protected]>Signed-off-by Albert Esteve <[email protected]>---
Thanks very much for including tests!
Quoted 9 lines
Maybe it'd be nice to test that the suppression is disabled after
KUNIT_END_SUPPRESSED_WARNING(). Of course, then triggering an actual stacktrace would be a pain, but maybe we could check that__kunit_is_suppressed_warning()returns false? If you wanted to be really fancy, you could test that it returns false on another kthread even while the suppression is active, too, but I won't hold you to it. Equally, you could try setting up a fake test context and ensuring the cleanup is called correctly, but I think that's mostly covered by the existing KUnit resource tests.
Good suggestions. I'll add a test for __kunit_is_suppressed_warning()
returning false after END. The cross-kthread test is a nice idea too. I'll see if I can fit it in without overcomplicating things.
Quoted 6 lines
Otherwise, looking good. A couple of other minor suggestions below, which may require some reworking of the
__kunit_suppressscope, but all optional suggestions.Reviewed-by David Gow <[email protected]>
Thanks for the review!
Quoted 72 lines
Quoted 57 lines
lib/kunit/Makefile | 3 ++ lib/kunit/backtrace-suppression-test.c | 90 ++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+)lib/kunit/Makefile +3
-0@@ -23,6 +23,9 @@ obj-$(if $(CONFIG_KUNIT),y) += hooks.o \ obj-$(CONFIG_KUNIT_TEST) += kunit-test.o obj-$(CONFIG_KUNIT_TEST) += platform-test.o +ifeq ($(CONFIG_KUNIT_SUPPRESS_BACKTRACE),y) +obj-$(CONFIG_KUNIT_TEST) += backtrace-suppression-test.o +endif # string-stream-test compiles built-in only. ifeq ($(CONFIG_KUNIT_TEST),y)lib/kunit/backtrace-suppression-test.c new file +32
-0@@ -0,0 +1,90 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* KUnit test for suppressing warning tracebacks. +* +* Copyright (C) 2024, Guenter Roeck +* Author: Guenter Roeck <[email protected]> +*/ + +#include <kunit/test.h> +#include <linux/bug.h> + +static void backtrace_suppression_test_warn_direct (struct kunit *test) +{ +KUNIT_START_SUPPRESSED_WARNING (test); +WARN (1 ,"This backtrace should be suppressed" ); +KUNIT_END_SUPPRESSED_WARNING (test); + +KUNIT_EXPECT_EQ (test,KUNIT_SUPPRESSED_WARNING_COUNT (),1 ); +} + +static void trigger_backtrace_warn (void ) +{ +WARN (1 ,"This backtrace should be suppressed" ); +} + +static void backtrace_suppression_test_warn_indirect (struct kunit *test) +{ +KUNIT_START_SUPPRESSED_WARNING (test); +trigger_backtrace_warn (); +KUNIT_END_SUPPRESSED_WARNING (test); + +KUNIT_EXPECT_EQ (test,KUNIT_SUPPRESSED_WARNING_COUNT (),1 );lib/kunit/backtrace-suppression-test.c line 42
Quoted 10 lines
lib/kunit/backtrace-suppression-test.c new file +10
-0+} + +static void backtrace_suppression_test_warn_multi (struct kunit *test) +{ +KUNIT_START_SUPPRESSED_WARNING (test); +WARN (1 ,"This backtrace should be suppressed" ); +trigger_backtrace_warn (); +KUNIT_END_SUPPRESSED_WARNING (test); + +KUNIT_EXPECT_EQ (test,KUNIT_SUPPRESSED_WARNING_COUNT (),2 );Would it make sense to test
KUNIT_SUPPRESSED_WARNING_COUNT()more thoroughly here by checking that it's 0 before any warnings, and checking that it's 1 in-between the two warnings?
Quoted 3 lines
Of course, the first case doesn't work due to
__kunit_suppressnot being defined, but if the implementation changes to support this, let's add it to the test, too.
I thought this was a test gap when you commented the limitation in patch #1, so I agree to add more granular COUNT checks once the API supports it (which it should after the rework to expose the pointer). Thanks!
Quoted 51 lines
Quoted 50 lines
lib/kunit/backtrace-suppression-test.c new file +48
-0+} + +static void backtrace_suppression_test_warn_on_direct (struct kunit *test) +{ +if (!IS_ENABLED (CONFIG_DEBUG_BUGVERBOSE ) && !IS_ENABLED (CONFIG_KALLSYMS )) +kunit_skip (test,"requires CONFIG_DEBUG_BUGVERBOSE or CONFIG_KALLSYMS" ); + +KUNIT_START_SUPPRESSED_WARNING (test); +WARN_ON (1 ); +KUNIT_END_SUPPRESSED_WARNING (test); + +KUNIT_EXPECT_EQ (test,KUNIT_SUPPRESSED_WARNING_COUNT (),1 ); +} + +static void trigger_backtrace_warn_on (void ) +{ +WARN_ON (1 ); +} + +static void backtrace_suppression_test_warn_on_indirect (struct kunit *test) +{ +if (!IS_ENABLED (CONFIG_DEBUG_BUGVERBOSE )) +kunit_skip (test,"requires CONFIG_DEBUG_BUGVERBOSE" ); + +KUNIT_START_SUPPRESSED_WARNING (test); +trigger_backtrace_warn_on (); +KUNIT_END_SUPPRESSED_WARNING (test); + +KUNIT_EXPECT_EQ (test,KUNIT_SUPPRESSED_WARNING_COUNT (),1 ); +} + +static struct kunit_case backtrace_suppression_test_cases[] = { +KUNIT_CASE (backtrace_suppression_test_warn_direct), +KUNIT_CASE (backtrace_suppression_test_warn_indirect), +KUNIT_CASE (backtrace_suppression_test_warn_multi), +KUNIT_CASE (backtrace_suppression_test_warn_on_direct), +KUNIT_CASE (backtrace_suppression_test_warn_on_indirect), + {} +}; + +static struct kunit_suite backtrace_suppression_test_suite = { + .name ="backtrace-suppression-test" , + .test_cases = backtrace_suppression_test_cases, +}; +kunit_test_suites (&backtrace_suppression_test_suite); + +MODULE_LICENSE ("GPL" ); +MODULE_DESCRIPTION ("KUnit test to verify warning backtrace suppression" );
PATCH v7 4/5 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.
--- drivers/gpu/drm/tests/drm_rect_test.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
drivers/gpu/drm/tests/drm_rect_test.c +14 -0
@@ -409,8 +409,15 @@ static void drm_test_rect_calc_hscale(struct kunit *test)const struct drm_rect_scale_case *params = test->param_value;int scaling_factor; +/* +* drm_rect_calc_hscale() generates a warning backtrace whenever bad +* parameters are passed to it. This affects all unit tests with an +* error code in expected_scaling_factor. +*/ +KUNIT_START_SUPPRESSED_WARNING (test); scaling_factor =drm_rect_calc_hscale (¶ms->src, ¶ms->dst, params->min_range, params->max_range); +KUNIT_END_SUPPRESSED_WARNING (test);KUNIT_EXPECT_EQ (test, scaling_factor, params->expected_scaling_factor); }
@@ -420,8 +427,15 @@ static void drm_test_rect_calc_vscale(struct kunit *test)const struct drm_rect_scale_case *params = test->param_value;int scaling_factor; +/* +* drm_rect_calc_vscale() generates a warning backtrace whenever bad +* parameters are passed to it. This affects all unit tests with an +* error code in expected_scaling_factor. +*/ +KUNIT_START_SUPPRESSED_WARNING (test); scaling_factor =drm_rect_calc_vscale (¶ms->src, ¶ms->dst, params->min_range, params->max_range); +KUNIT_END_SUPPRESSED_WARNING (test);KUNIT_EXPECT_EQ (test, scaling_factor, params->expected_scaling_factor); }
Signature · 1 line
2.52.0
Re: [PATCH v7 4/5] drm: Suppress intentional warning backtraces in scaling unit tests
Quoted 28 lines · Albert Esteve
From: Guenter Roeck <[email protected]>
The
drm_test_rect_calc_hscaleanddrm_test_rect_calc_vscaleunit 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.Acked-by Dan Carpenter <[email protected]>Acked-by Maíra Canal <[email protected]>Cc Maarten Lankhorst <[email protected]>Cc David Airlie <[email protected]>Cc Daniel Vetter <[email protected]>Signed-off-by Guenter Roeck <[email protected]>Signed-off-by Alessandro Carminati <[email protected]>Signed-off-by Albert Esteve <[email protected]>--- drivers/gpu/drm/tests/drm_rect_test.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)drivers/gpu/drm/tests/drm_rect_test.c +0
-0@@ -409,8 +409,15 @@ static void drm_test_rect_calc_hscale(struct kunit *test)const struct drm_rect_scale_case *params = test->param_value;int scaling_factor;
drivers/gpu/drm/tests/drm_rect_test.c static void drm_test_rect_calc_hscale(struct kunit *test) · line 420
Quoted 10 lines
drivers/gpu/drm/tests/drm_rect_test.c +7
-0+/* +* drm_rect_calc_hscale() generates a warning backtrace whenever bad +* parameters are passed to it. This affects all unit tests with an +* error code in expected_scaling_factor. +*/ +KUNIT_START_SUPPRESSED_WARNING (test); scaling_factor =drm_rect_calc_hscale (¶ms->src, ¶ms->dst, params->min_range, params->max_range); +KUNIT_END_SUPPRESSED_WARNING (test);
Would not something like:
scoped_kunit_suppress() {
scaling_factor = drm_rect_calc_hscale(¶ms->src, ¶ms->dst,
params->min_range, params->max_range);
}be better?
Also, how can you stand all this screaming in the code?
Re: [PATCH v7 4/5] drm: Suppress intentional warning backtraces in scaling unit tests
Quoted 43 lines · Peter Zijlstra
Quoted 28 lines · Albert Esteve
From: Guenter Roeck <[email protected]>
The
drm_test_rect_calc_hscaleanddrm_test_rect_calc_vscaleunit 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.Acked-by Dan Carpenter <[email protected]>Acked-by Maíra Canal <[email protected]>Cc Maarten Lankhorst <[email protected]>Cc David Airlie <[email protected]>Cc Daniel Vetter <[email protected]>Signed-off-by Guenter Roeck <[email protected]>Signed-off-by Alessandro Carminati <[email protected]>Signed-off-by Albert Esteve <[email protected]>--- drivers/gpu/drm/tests/drm_rect_test.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)drivers/gpu/drm/tests/drm_rect_test.c +0
-0@@ -409,8 +409,15 @@ static void drm_test_rect_calc_hscale(struct kunit *test)const struct drm_rect_scale_case *params = test->param_value;int scaling_factor;drivers/gpu/drm/tests/drm_rect_test.c static void drm_test_rect_calc_hscale(struct kunit *test) · line 420
Quoted 10 lines
drivers/gpu/drm/tests/drm_rect_test.c +7
-0+/* +* drm_rect_calc_hscale() generates a warning backtrace whenever bad +* parameters are passed to it. This affects all unit tests with an +* error code in expected_scaling_factor. +*/ +KUNIT_START_SUPPRESSED_WARNING (test); scaling_factor =drm_rect_calc_hscale (¶ms->src, ¶ms->dst, params->min_range, params->max_range); +KUNIT_END_SUPPRESSED_WARNING (test);Would not something like:
Quoted 6 lines
scoped_kunit_suppress() { scaling_factor = drm_rect_calc_hscale(¶ms->src, ¶ms->dst, params->min_range, params->max_range); }be better?
Since KUnit already has a few macros in its API it didn't occur to me. Good idea, I like it. And I guess the scope approach matches well with your __cleanup comment in the first patch. If no one opposes, I will work toward that pattern for the next version.
Quoted 3 lines
Also, how can you stand all this screaming in the code?
Again, KUnit already contains many macros, so this use didn't register as such. Now I will not be able to unsee it.
Re: [PATCH v7 4/5] drm: Suppress intentional warning backtraces in scaling unit tests
Quoted 49 lines · Albert Esteve
Quoted 48 lines · Peter Zijlstra
Quoted 28 lines · Albert Esteve
From: Guenter Roeck <[email protected]>
The
drm_test_rect_calc_hscaleanddrm_test_rect_calc_vscaleunit 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.Acked-by Dan Carpenter <[email protected]>Acked-by Maíra Canal <[email protected]>Cc Maarten Lankhorst <[email protected]>Cc David Airlie <[email protected]>Cc Daniel Vetter <[email protected]>Signed-off-by Guenter Roeck <[email protected]>Signed-off-by Alessandro Carminati <[email protected]>Signed-off-by Albert Esteve <[email protected]>--- drivers/gpu/drm/tests/drm_rect_test.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)drivers/gpu/drm/tests/drm_rect_test.c +0
-0@@ -409,8 +409,15 @@ static void drm_test_rect_calc_hscale(struct kunit *test)const struct drm_rect_scale_case *params = test->param_value;int scaling_factor;drivers/gpu/drm/tests/drm_rect_test.c static void drm_test_rect_calc_hscale(struct kunit *test) · line 420
Quoted 10 lines
drivers/gpu/drm/tests/drm_rect_test.c +7
-0+/* +* drm_rect_calc_hscale() generates a warning backtrace whenever bad +* parameters are passed to it. This affects all unit tests with an +* error code in expected_scaling_factor. +*/ +KUNIT_START_SUPPRESSED_WARNING (test); scaling_factor =drm_rect_calc_hscale (¶ms->src, ¶ms->dst, params->min_range, params->max_range); +KUNIT_END_SUPPRESSED_WARNING (test);Would not something like:
scoped_kunit_suppress() { scaling_factor = drm_rect_calc_hscale(¶ms->src, ¶ms->dst, params->min_range, params->max_range); }
Quoted 6 lines
Quoted 1 line
be better?
Since KUnit already has a few macros in its API it didn't occur to me. Good idea, I like it. And I guess the scope approach matches well with your
__cleanupcomment in the first patch. If no one opposes, I will work toward that pattern for the next version.
There's a catch with kunit and __cleanup and thus (scoped) guards. Kunit runs in ktreads, asserts lead to kthread_exit() and the __cleanup won't be called.
Warning suppression being part of kunit infrastructure, asserts can and should end the suppression too. But setting the example (scoped) guards are safe in kunit tests in general feels like a trap waiting to happen.
BR,
Jani.
Quoted 9 lines
Quoted 4 lines
Also, how can you stand all this screaming in the code?
Again, KUnit already contains many macros, so this use didn't register as such. Now I will not be able to unsee it.
Signature · 1 line
Jani Nikula, Intel
Re: [PATCH v7 4/5] drm: Suppress intentional warning backtraces in scaling unit tests
Quoted 53 lines · Jani Nikula
Quoted 51 lines · Albert Esteve
Quoted 49 lines · Peter Zijlstra
Quoted 28 lines · Albert Esteve
From: Guenter Roeck <[email protected]>
The
drm_test_rect_calc_hscaleanddrm_test_rect_calc_vscaleunit 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.Acked-by Dan Carpenter <[email protected]>Acked-by Maíra Canal <[email protected]>Cc Maarten Lankhorst <[email protected]>Cc David Airlie <[email protected]>Cc Daniel Vetter <[email protected]>Signed-off-by Guenter Roeck <[email protected]>Signed-off-by Alessandro Carminati <[email protected]>Signed-off-by Albert Esteve <[email protected]>--- drivers/gpu/drm/tests/drm_rect_test.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)drivers/gpu/drm/tests/drm_rect_test.c +0
-0@@ -409,8 +409,15 @@ static void drm_test_rect_calc_hscale(struct kunit *test)const struct drm_rect_scale_case *params = test->param_value;int scaling_factor;drivers/gpu/drm/tests/drm_rect_test.c static void drm_test_rect_calc_hscale(struct kunit *test) · line 420
Quoted 10 lines
drivers/gpu/drm/tests/drm_rect_test.c +7
-0+/* +* drm_rect_calc_hscale() generates a warning backtrace whenever bad +* parameters are passed to it. This affects all unit tests with an +* error code in expected_scaling_factor. +*/ +KUNIT_START_SUPPRESSED_WARNING (test); scaling_factor =drm_rect_calc_hscale (¶ms->src, ¶ms->dst, params->min_range, params->max_range); +KUNIT_END_SUPPRESSED_WARNING (test);Would not something like:
scoped_kunit_suppress() { scaling_factor = drm_rect_calc_hscale(¶ms->src, ¶ms->dst, params->min_range, params->max_range); }be better?
Quoted 8 lines
Quoted 4 lines
Since KUnit already has a few macros in its API it didn't occur to me. Good idea, I like it. And I guess the scope approach matches well with your
__cleanupcomment in the first patch. If no one opposes, I will work toward that pattern for the next version.There's a catch with kunit and
__cleanupand thus (scoped) guards. Kunit runs in ktreads, asserts lead tokthread_exit()and the__cleanupwon't be called.
Hi Jani,
Good point. In this specific case, the actual cleanup is handled by kunit_add_action_or_reset(), so __cleanup not firing on assert is harmless.
Quoted 5 lines
Warning suppression being part of kunit infrastructure, asserts can and should end the suppression too. But setting the example (scoped) guards are safe in kunit tests in general feels like a trap waiting to happen.
... but I agree it sets a misleading precedent. I'll stick with the explicit start/end API, then? Or maybe we can clearly document why the scoped approach is safe in this case and use it.
Quoted 18 lines
BR,
Jani.Quoted 9 lines
Quoted 4 lines
Also, how can you stand all this screaming in the code?
Again, KUnit already contains many macros, so this use didn't register as such. Now I will not be able to unsee it.
--
Jani Nikula, Intel
Re: [PATCH v7 4/5] drm: Suppress intentional warning backtraces in scaling unit tests
Quoted 63 lines · Albert Esteve
Quoted 53 lines · Jani Nikula
Quoted 51 lines · Albert Esteve
Quoted 49 lines · Peter Zijlstra
Quoted 28 lines · Albert Esteve
From: Guenter Roeck <[email protected]>
The
drm_test_rect_calc_hscaleanddrm_test_rect_calc_vscaleunit 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.Acked-by Dan Carpenter <[email protected]>Acked-by Maíra Canal <[email protected]>Cc Maarten Lankhorst <[email protected]>Cc David Airlie <[email protected]>Cc Daniel Vetter <[email protected]>Signed-off-by Guenter Roeck <[email protected]>Signed-off-by Alessandro Carminati <[email protected]>Signed-off-by Albert Esteve <[email protected]>--- drivers/gpu/drm/tests/drm_rect_test.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)drivers/gpu/drm/tests/drm_rect_test.c +0
-0@@ -409,8 +409,15 @@ static void drm_test_rect_calc_hscale(struct kunit *test)const struct drm_rect_scale_case *params = test->param_value;int scaling_factor;drivers/gpu/drm/tests/drm_rect_test.c static void drm_test_rect_calc_hscale(struct kunit *test) · line 420
Quoted 10 lines
drivers/gpu/drm/tests/drm_rect_test.c +7
-0+/* +* drm_rect_calc_hscale() generates a warning backtrace whenever bad +* parameters are passed to it. This affects all unit tests with an +* error code in expected_scaling_factor. +*/ +KUNIT_START_SUPPRESSED_WARNING (test); scaling_factor =drm_rect_calc_hscale (¶ms->src, ¶ms->dst, params->min_range, params->max_range); +KUNIT_END_SUPPRESSED_WARNING (test);Would not something like:
scoped_kunit_suppress() { scaling_factor = drm_rect_calc_hscale(¶ms->src, ¶ms->dst, params->min_range, params->max_range); }be better?
Quoted 8 lines
Quoted 4 lines
Since KUnit already has a few macros in its API it didn't occur to me. Good idea, I like it. And I guess the scope approach matches well with your
__cleanupcomment in the first patch. If no one opposes, I will work toward that pattern for the next version.There's a catch with kunit and
__cleanupand thus (scoped) guards. Kunit runs in ktreads, asserts lead tokthread_exit()and the__cleanupwon't be called.
Quoted 5 lines
Hi Jani,
Good point. In this specific case, the actual cleanup is handled by
kunit_add_action_or_reset(), so__cleanupnot firing on assert is harmless.
Right.
Quoted 10 lines
Quoted 6 lines
Warning suppression being part of kunit infrastructure, asserts can and should end the suppression too. But setting the example (scoped) guards are safe in kunit tests in general feels like a trap waiting to happen.
... but I agree it sets a misleading precedent. I'll stick with the explicit start/end API, then? Or maybe we can clearly document why the scoped approach is safe in this case and use it.
I don't know what the right approach is, just wanted to make sure you're aware of this particular gotcha with guards and kunit.
BR,
Jani.
Quoted 20 lines
Quoted 19 lines
BR,
Jani.Quoted 9 lines
Quoted 4 lines
Also, how can you stand all this screaming in the code?
Again, KUnit already contains many macros, so this use didn't register as such. Now I will not be able to unsee it.
--
Jani Nikula, Intel
Signature · 1 line
Jani Nikula, Intel
Re: [PATCH v7 4/5] drm: Suppress intentional warning backtraces in scaling unit tests
In reply to Albert EsteveLe 20/04/2026 à 8:28 PM, Albert Esteve a écrit :
Quoted 17 lines
From: Guenter Roeck <[email protected]>
The
drm_test_rect_calc_hscaleanddrm_test_rect_calc_vscaleunit 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.Acked-by Dan Carpenter <[email protected]>Acked-by Maíra Canal <[email protected]>Cc Maarten Lankhorst <[email protected]>Cc David Airlie <[email protected]>Cc Daniel Vetter <[email protected]>Signed-off-by Guenter Roeck <[email protected]>Signed-off-by Alessandro Carminati <[email protected]>Signed-off-by Albert Esteve <[email protected]>
Quoted 1 line
---
Some of the suggestions in the thread around __cleanup() et al. sound good to me, too. I suspect that it should work given the kunit_add_action() / resource system use anyway, so even if an assertion fires, things should be cleaned up properly.
Cheers,
-- David
Quoted 40 lines
drivers/gpu/drm/tests/drm_rect_test.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)drivers/gpu/drm/tests/drm_rect_test.c +14
-0@@ -409,8 +409,15 @@ static void drm_test_rect_calc_hscale(struct kunit *test)const struct drm_rect_scale_case *params = test->param_value;int scaling_factor; +/* +* drm_rect_calc_hscale() generates a warning backtrace whenever bad +* parameters are passed to it. This affects all unit tests with an +* error code in expected_scaling_factor. +*/ +KUNIT_START_SUPPRESSED_WARNING (test); scaling_factor =drm_rect_calc_hscale (¶ms->src, ¶ms->dst, params->min_range, params->max_range); +KUNIT_END_SUPPRESSED_WARNING (test);KUNIT_EXPECT_EQ (test, scaling_factor, params->expected_scaling_factor); }@@ -420,8 +427,15 @@ static void drm_test_rect_calc_vscale(struct kunit *test)const struct drm_rect_scale_case *params = test->param_value;int scaling_factor; +/* +* drm_rect_calc_vscale() generates a warning backtrace whenever bad +* parameters are passed to it. This affects all unit tests with an +* error code in expected_scaling_factor. +*/ +KUNIT_START_SUPPRESSED_WARNING (test); scaling_factor =drm_rect_calc_vscale (¶ms->src, ¶ms->dst, params->min_range, params->max_range); +KUNIT_END_SUPPRESSED_WARNING (test);KUNIT_EXPECT_EQ (test, scaling_factor, params->expected_scaling_factor); }
PATCH v7 5/5 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 | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-)
Documentation/dev-tools/kunit/usage.rst +29 -1
@@ -157,6 +157,34 @@ 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 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. + +Such backtraces can be suppressed with **task scope suppression**: while +``START`` / ``END`` is active on the current task, the backtrace and stack +dump from warnings on that task are suppressed. Wrap the call from your test +in that window, like shown in the following code. + +.. code-block:: c + + static void some_test(struct kunit *test) + { + KUNIT_START_SUPPRESSED_WARNING(test); + trigger_backtrace(); + KUNIT_END_SUPPRESSED_WARNING(test); + } + +``KUNIT_SUPPRESSED_WARNING_COUNT()`` returns the number of suppressed backtraces. +If the suppressed backtrace was triggered on purpose, this can be used to check +if the backtrace was actually triggered. + +.. code-block:: c + + KUNIT_EXPECT_EQ(test, KUNIT_SUPPRESSED_WARNING_COUNT(), 1); Test Suites ~~~~~~~~~~~
@@ -1211,4 +1239,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.52.0
Re: [PATCH v7 5/5] kunit: Add documentation for warning backtrace suppression API
Le 20/04/2026 à 8:28 PM, Albert Esteve a écrit :
Quoted 4 lines
From: Guenter Roeck <[email protected]>
Document API functions for suppressing warning backtraces.
Quoted 8 lines
Acked-by Dan Carpenter <[email protected]>Reviewed-by Kees Cook <[email protected]>Signed-off-by Guenter Roeck <[email protected]>Reviewed-by David Gow <[email protected]>Signed-off-by Alessandro Carminati <[email protected]>Signed-off-by Albert Esteve <[email protected]>---
Thanks -- it's always good to have documentation.
Apart from one note below, this looks good to me.
Cheers,
-- David
Quoted 29 lines
Documentation/dev-tools/kunit/usage.rst | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-)Documentation/dev-tools/kunit/usage.rst +18
-0@@ -157,6 +157,34 @@ 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 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. + +Such backtraces can be suppressed with **task scope suppression**: while +``START`` / ``END`` is active on the current task, the backtrace and stack +dump from warnings on that task are suppressed. Wrap the call from your test +in that window, like shown in the following code. + +.. code-block:: c + + static void some_test(struct kunit *test) + { + KUNIT_START_SUPPRESSED_WARNING(test); + trigger_backtrace();
Documentation/dev-tools/kunit/usage.rst Alternatively, one can take full control over the error message by using · line 187
Quoted 10 lines
Documentation/dev-tools/kunit/usage.rst +10
-0+ KUNIT_END_SUPPRESSED_WARNING(test); + } + +``KUNIT_SUPPRESSED_WARNING_COUNT()`` returns the number of suppressed backtraces. +If the suppressed backtrace was triggered on purpose, this can be used to check +if the backtrace was actually triggered. + +.. code-block:: c + + KUNIT_EXPECT_EQ(test, KUNIT_SUPPRESSED_WARNING_COUNT(), 1);
It might be worth noting that all of these must be in the same function, and the KUNIT_START_SUPPRESSED_WARNING() must be first. (And, in addition, there can't be more than one START/END pair per-function).
Of course, if the implementation is changed, that wouldn't be necessary. :-)
Quoted 11 lines
Documentation/dev-tools/kunit/usage.rst +1
-1Test Suites ~~~~~~~~~~~@@ -1211,4 +1239,4 @@ For example: dev_managed_string = devm_kstrdup(fake_device, "Hello, World!"); // Everything is cleaned up automatically when the test ends. - } + }