mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2026-06-21 15:43:21 +02:00
85347718ab
Some unit tests intentionally trigger warning backtraces by passing bad
parameters to kernel API functions. Such unit tests typically check the
return value from such calls, not the existence of the warning backtrace.
Such intentionally generated warning backtraces are neither desirable
nor useful for a number of reasons:
- They can result in overlooked real problems.
- A warning that suddenly starts to show up in unit tests needs to be
investigated and has to be marked to be ignored, for example by
adjusting filter scripts. Such filters are ad hoc because there is
no real standard format for warnings. On top of that, such filter
scripts would require constant maintenance.
Solve the problem by providing a means to suppress warning backtraces
originating from the current kthread while executing test code. Since
each KUnit test runs in its own kthread, this effectively scopes
suppression to the test that enabled it. Limit changes to generic code
to the absolute minimum.
Implementation details:
Suppression is integrated into the existing KUnit hooks infrastructure
in test-bug.h, reusing the kunit_running static branch for zero
overhead when no tests are running.
Suppression is checked at three points in the warning path:
- In warn_slowpath_fmt(), the check runs before any output, fully
suppressing both message and backtrace. This covers architectures
without __WARN_FLAGS.
- In __warn_printk(), the check suppresses the warning message text.
This covers architectures that define __WARN_FLAGS but not their own
__WARN_printf (arm64, loongarch, parisc, powerpc, riscv, sh), where
the message is printed before the trap enters __report_bug().
- In __report_bug(), the check runs before __warn() is called,
suppressing the backtrace and stack dump.
To avoid double-counting on architectures where both __warn_printk()
and __report_bug() run for the same warning, kunit_is_suppressed_warning()
takes a bool parameter: true to increment the suppression counter
(used in warn_slowpath_fmt and __report_bug), false to check only
(used in __warn_printk).
The suppression state is dynamically allocated via kunit_kzalloc() and
tied to the KUnit test lifecycle via kunit_add_action(), ensuring
automatic cleanup at test exit. Writer-side access to the global
suppression list is serialized with a spinlock; readers use RCU.
Two API forms are provided:
- kunit_warning_suppress(test) { ... }: scoped, uses __cleanup for
automatic teardown on scope exit, kunit_add_action() as safety net
for abnormal exits (e.g. kthread_exit from failed assertions).
Suppression handle is only accessible inside the block.
- kunit_start/end_suppress_warning(test): direct functions returning
an explicit handle, for retaining the handle within the test,
or for cross-function usage.
Link: https://lore.kernel.org/r/20260514-kunit_add_support-v11-1-b36a530a6d8f@redhat.com
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Signed-off-by: Alessandro Carminati <acarmina@redhat.com>
Reviewed-by: Kees Cook <kees@kernel.org>
Reviewed-by: David Gow <david@davidgow.net>
Signed-off-by: Albert Esteve <aesteve@redhat.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
121 lines
2.9 KiB
C
121 lines
2.9 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* KUnit helpers for backtrace suppression
|
|
*
|
|
* Copyright (C) 2025 Alessandro Carminati <acarmina@redhat.com>
|
|
* Copyright (C) 2024 Guenter Roeck <linux@roeck-us.net>
|
|
*/
|
|
|
|
#include <kunit/resource.h>
|
|
#include <linux/export.h>
|
|
#include <linux/rculist.h>
|
|
#include <linux/sched.h>
|
|
#include <linux/sched/task.h>
|
|
#include <linux/spinlock.h>
|
|
|
|
#include "hooks-impl.h"
|
|
|
|
struct kunit_suppressed_warning {
|
|
struct list_head node;
|
|
struct task_struct *task;
|
|
struct kunit *test;
|
|
atomic_t counter;
|
|
};
|
|
|
|
static LIST_HEAD(suppressed_warnings);
|
|
static DEFINE_SPINLOCK(suppressed_warnings_lock);
|
|
|
|
static void kunit_suppress_warning_remove(struct kunit_suppressed_warning *w)
|
|
{
|
|
unsigned long flags;
|
|
|
|
spin_lock_irqsave(&suppressed_warnings_lock, flags);
|
|
list_del_rcu(&w->node);
|
|
spin_unlock_irqrestore(&suppressed_warnings_lock, flags);
|
|
put_task_struct(w->task);
|
|
}
|
|
|
|
KUNIT_DEFINE_ACTION_WRAPPER(kunit_suppress_warning_cleanup,
|
|
kunit_suppress_warning_remove,
|
|
struct kunit_suppressed_warning *);
|
|
|
|
bool kunit_has_active_suppress_warning(void)
|
|
{
|
|
return __kunit_is_suppressed_warning_impl(false);
|
|
}
|
|
EXPORT_SYMBOL_GPL(kunit_has_active_suppress_warning);
|
|
|
|
struct kunit_suppressed_warning *
|
|
kunit_start_suppress_warning(struct kunit *test)
|
|
{
|
|
struct kunit_suppressed_warning *w;
|
|
unsigned long flags;
|
|
int ret;
|
|
|
|
if (kunit_has_active_suppress_warning()) {
|
|
KUNIT_FAIL(test, "Another suppression block is already active");
|
|
return NULL;
|
|
}
|
|
|
|
w = kunit_kzalloc(test, sizeof(*w), GFP_KERNEL);
|
|
if (!w) {
|
|
KUNIT_FAIL(test, "Failed to allocate suppression handle.");
|
|
return NULL;
|
|
}
|
|
|
|
w->task = get_task_struct(current);
|
|
w->test = test;
|
|
|
|
spin_lock_irqsave(&suppressed_warnings_lock, flags);
|
|
list_add_rcu(&w->node, &suppressed_warnings);
|
|
spin_unlock_irqrestore(&suppressed_warnings_lock, flags);
|
|
|
|
ret = kunit_add_action_or_reset(test,
|
|
kunit_suppress_warning_cleanup, w);
|
|
if (ret) {
|
|
KUNIT_FAIL(test, "Failed to add suppression cleanup action.");
|
|
return NULL;
|
|
}
|
|
|
|
return w;
|
|
}
|
|
EXPORT_SYMBOL_GPL(kunit_start_suppress_warning);
|
|
|
|
void kunit_end_suppress_warning(struct kunit *test,
|
|
struct kunit_suppressed_warning *w)
|
|
{
|
|
if (!w)
|
|
return;
|
|
kunit_release_action(test, kunit_suppress_warning_cleanup, w);
|
|
}
|
|
EXPORT_SYMBOL_GPL(kunit_end_suppress_warning);
|
|
|
|
void __kunit_suppress_auto_cleanup(struct kunit_suppressed_warning **wp)
|
|
{
|
|
if (*wp)
|
|
kunit_end_suppress_warning((*wp)->test, *wp);
|
|
}
|
|
EXPORT_SYMBOL_GPL(__kunit_suppress_auto_cleanup);
|
|
|
|
int kunit_suppressed_warning_count(struct kunit_suppressed_warning *w)
|
|
{
|
|
return w ? atomic_read(&w->counter) : 0;
|
|
}
|
|
EXPORT_SYMBOL_GPL(kunit_suppressed_warning_count);
|
|
|
|
bool __kunit_is_suppressed_warning_impl(bool count)
|
|
{
|
|
struct kunit_suppressed_warning *w;
|
|
|
|
guard(rcu)();
|
|
list_for_each_entry_rcu(w, &suppressed_warnings, node) {
|
|
if (w->task == current) {
|
|
if (count)
|
|
atomic_inc(&w->counter);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|