]> www.infradead.org Git - users/willy/pagecache.git/commitdiff
bug/kunit: core support for suppressing warning backtraces
authorGuenter Roeck <linux@roeck-us.net>
Thu, 13 Mar 2025 11:43:16 +0000 (11:43 +0000)
committerAndrew Morton <akpm@linux-foundation.org>
Fri, 14 Mar 2025 22:56:15 +0000 (15:56 -0700)
Patch series "Add support for suppressing warning backtraces", v4.

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 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, it 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.  Architecture specific changes are kept at
minimum by retaining function names only if both CONFIG_DEBUG_BUGVERBOSE
and CONFIG_KUNIT are enabled.

The first patch of the series introduces the necessary infrastructure.
The second patch introduces support for counting suppressed backtraces.
This capability is used in patch three to implement unit tests.  Patch
four documents the new API.

The next two patches add support for suppressing backtraces in drm_rect
and dev_addr_lists unit tests.  These patches are intended to serve as
examples for the use of the functionality introduced with this series.

The remaining patches implement the necessary changes for all
architectures with GENERIC_BUG support.

With CONFIG_KUNIT enabled, image size increase with this series applied is
approximately 1%.  The image size increase (and with it the functionality
introduced by this series) can be avoided by disabling
CONFIG_KUNIT_SUPPRESS_BACKTRACE.

This patch (of 14):

Some unit tests intentionally trigger warning backtraces by passing bad
parameters to API functions.  Such unit tests typically check the return
value from those 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 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, it 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.  Since the new functionality
results in an image size increase of about 1% if CONFIG_KUNIT is enabled,
provide configuration option KUNIT_SUPPRESS_BACKTRACE to be able to
disable the new functionality.  This option is by default enabled since
almost all systems with CONFIG_KUNIT enabled will want to benefit from it.

Link: https://lkml.kernel.org/r/20250313114329.284104-1-acarmina@redhat.com
Link: https://lkml.kernel.org/r/20250313114329.284104-2-acarmina@redhat.com
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Signed-off-by: Alessandro Carminati <acarmina@redhat.com>
Tested-by: Linux Kernel Functional Testing <lkft@linaro.org>
Acked-by: Dan Carpenter <dan.carpenter@linaro.org>
Reviewed-by: Kees Cook <keescook@chromium.org>
Cc: Daniel Diaz <daniel.diaz@linaro.org>
Cc: Naresh Kamboju <naresh.kamboju@linaro.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Arthur Grillo <arthurgrillo@riseup.net>
Cc: Brendan Higgins <brendan.higgins@linux.dev>
Cc: Daniel Vetter <daniel@ffwll.ch>
Cc: Dave Airlie <airlied@gmail.com>
Cc: David Gow <davidgow@google.com>
Cc: Guenetr Roeck <linux@roeck-us.net>
Cc: Jani Nikula <jani.nikula@intel.com>
Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Cc: MaĆ­ra Canal <mcanal@igalia.com>
Cc: Maxime Ripard <mripard@kernel.org>
Cc: Thomas Zimemrmann <tzimmermann@suse.de>
Cc: Ville Syrjala <ville.syrjala@linux.intel.com>
Cc: Albert Ou <aou@eecs.berkeley.edu>
Cc: Alexander Gordeev <agordeev@linux.ibm.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Charlie Jenkins <charlie@rivosinc.com>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Helge Deller <deller@gmx.de>
Cc: Huacai Chen <chenhuacai@kernel.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Paul Walmsley <paul.walmsley@sifive.com>
Cc: Rae Moar <rmoar@google.com>
Cc: Rich Felker <dalias@libc.org>
Cc: Simon Horman <horms@kernel.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Will Deacon <will@kernel.org>
Cc: Yoshinori Sato <ysato@users.sourceforge.jp>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
include/asm-generic/bug.h
include/kunit/bug.h [new file with mode: 0644]
include/kunit/test.h
include/linux/bug.h
lib/bug.c
lib/kunit/Kconfig
lib/kunit/Makefile
lib/kunit/bug.c [new file with mode: 0644]

index 387720933973b6313547dc9a4431473fb6e7c85d..9194cf743ec3aae5407071e98f0895d72fb2b302 100644 (file)
@@ -18,6 +18,7 @@
 #endif
 
 #ifndef __ASSEMBLY__
+#include <kunit/bug.h>
 #include <linux/panic.h>
 #include <linux/printk.h>
 
@@ -39,8 +40,14 @@ struct bug_entry {
 #ifdef CONFIG_DEBUG_BUGVERBOSE
 #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
        const char      *file;
+#ifdef HAVE_BUG_FUNCTION
+       const char      *function;
+#endif
 #else
        signed int      file_disp;
+#ifdef HAVE_BUG_FUNCTION
+       signed int      function_disp;
+#endif
 #endif
        unsigned short  line;
 #endif
@@ -96,15 +103,18 @@ extern __printf(1, 2) void __warn_printk(const char *fmt, ...);
 #define __WARN()               __WARN_printf(TAINT_WARN, NULL)
 #define __WARN_printf(taint, arg...) do {                              \
                instrumentation_begin();                                \
-               warn_slowpath_fmt(__FILE__, __LINE__, taint, arg);      \
+               if (!KUNIT_IS_SUPPRESSED_WARNING(__func__))                     \
+                       warn_slowpath_fmt(__FILE__, __LINE__, taint, arg);\
                instrumentation_end();                                  \
        } while (0)
 #else
 #define __WARN()               __WARN_FLAGS(BUGFLAG_TAINT(TAINT_WARN))
 #define __WARN_printf(taint, arg...) do {                              \
                instrumentation_begin();                                \
-               __warn_printk(arg);                                     \
-               __WARN_FLAGS(BUGFLAG_NO_CUT_HERE | BUGFLAG_TAINT(taint));\
+               if (!KUNIT_IS_SUPPRESSED_WARNING(__func__)) {                   \
+                       __warn_printk(arg);                             \
+                       __WARN_FLAGS(BUGFLAG_NO_CUT_HERE | BUGFLAG_TAINT(taint));\
+               }                                                       \
                instrumentation_end();                                  \
        } while (0)
 #define WARN_ON_ONCE(condition) ({                             \
diff --git a/include/kunit/bug.h b/include/kunit/bug.h
new file mode 100644 (file)
index 0000000..0a8e62c
--- /dev/null
@@ -0,0 +1,51 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * KUnit helpers for backtrace suppression
+ *
+ * Copyright (c) 2024 Guenter Roeck <linux@roeck-us.net>
+ */
+
+#ifndef _KUNIT_BUG_H
+#define _KUNIT_BUG_H
+
+#ifndef __ASSEMBLY__
+
+#include <linux/kconfig.h>
+
+#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE
+
+#include <linux/stringify.h>
+#include <linux/types.h>
+
+struct __suppressed_warning {
+       struct list_head node;
+       const char *function;
+};
+
+void __kunit_start_suppress_warning(struct __suppressed_warning *warning);
+void __kunit_end_suppress_warning(struct __suppressed_warning *warning);
+bool __kunit_is_suppressed_warning(const char *function);
+
+#define DEFINE_SUPPRESSED_WARNING(func)        \
+       struct __suppressed_warning __kunit_suppress_##func = \
+               { .function = __stringify(func) }
+
+#define KUNIT_START_SUPPRESSED_WARNING(func) \
+       __kunit_start_suppress_warning(&__kunit_suppress_##func)
+
+#define KUNIT_END_SUPPRESSED_WARNING(func) \
+       __kunit_end_suppress_warning(&__kunit_suppress_##func)
+
+#define KUNIT_IS_SUPPRESSED_WARNING(func) \
+       __kunit_is_suppressed_warning(func)
+
+#else /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */
+
+#define DEFINE_SUPPRESSED_WARNING(func)
+#define KUNIT_START_SUPPRESSED_WARNING(func)
+#define KUNIT_END_SUPPRESSED_WARNING(func)
+#define KUNIT_IS_SUPPRESSED_WARNING(func) (false)
+
+#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */
+#endif /* __ASSEMBLY__ */
+#endif /* _KUNIT_BUG_H */
index 58dbab60f8530588350f6e409ed3ab572cfa7b2f..cad32d1748720772347f7b2ef56532d07df142ca 100644 (file)
@@ -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>
index a9948a9f1093beeb112a55dcfa706d8cf6c74b75..e42f20cf8830e0ddb59d9236a7d78b112ac951d5 100644 (file)
@@ -36,6 +36,9 @@ static inline int is_warning_bug(const struct bug_entry *bug)
        return bug->flags & BUGFLAG_WARNING;
 }
 
+void bug_get_file_function_line(struct bug_entry *bug, const char **file,
+                               const char **function, unsigned int *line);
+
 void bug_get_file_line(struct bug_entry *bug, const char **file,
                       unsigned int *line);
 
@@ -62,6 +65,16 @@ static inline enum bug_trap_type report_bug(unsigned long bug_addr,
 }
 
 struct bug_entry;
+static inline void bug_get_file_function_line(struct bug_entry *bug,
+                                             const char **file,
+                                             const char **function,
+                                             unsigned int *line)
+{
+       *file = NULL;
+       *function = NULL;
+       *line = 0;
+}
+
 static inline void bug_get_file_line(struct bug_entry *bug, const char **file,
                                     unsigned int *line)
 {
index e0ff219899902f51815c7e08e3642d97af7721de..5eb2ee66916fb1876b485dcf6df8bc0775226817 100644 (file)
--- a/lib/bug.c
+++ b/lib/bug.c
        when CONFIG_DEBUG_BUGVERBOSE is not enabled, so you must generate
        the values accordingly.
 
+  2a.Optionally implement support for the "function" entry in struct
+     bug_entry. This entry must point to the name of the function triggering
+     the warning or bug trap (normally __func__). This is only needed if
+     both CONFIG_DEBUG_BUGVERBOSE and CONFIG_KUNIT_SUPPRESS_BACKTRACE are
+     enabled and if the architecture wants to implement support for suppressing
+     warning backtraces. The architecture must define HAVE_BUG_FUNCTION if it
+     adds pointers to function names to struct bug_entry.
+
   3. Implement the trap
      - In the illegal instruction trap handler (typically), verify
        that the fault was in kernel mode, and call report_bug()
@@ -127,14 +135,21 @@ static inline struct bug_entry *module_find_bug(unsigned long bugaddr)
 }
 #endif
 
-void bug_get_file_line(struct bug_entry *bug, const char **file,
-                      unsigned int *line)
+void bug_get_file_function_line(struct bug_entry *bug, const char **file,
+                               const char **function, unsigned int *line)
 {
+       *function = NULL;
 #ifdef CONFIG_DEBUG_BUGVERBOSE
 #ifdef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
        *file = (const char *)&bug->file_disp + bug->file_disp;
+#ifdef HAVE_BUG_FUNCTION
+       *function = (const char *)&bug->function_disp + bug->function_disp;
+#endif
 #else
        *file = bug->file;
+#ifdef HAVE_BUG_FUNCTION
+       *function = bug->function;
+#endif
 #endif
        *line = bug->line;
 #else
@@ -143,6 +158,13 @@ void bug_get_file_line(struct bug_entry *bug, const char **file,
 #endif
 }
 
+void bug_get_file_line(struct bug_entry *bug, const char **file, unsigned int *line)
+{
+       const char *function;
+
+       bug_get_file_function_line(bug, file, &function, line);
+}
+
 struct bug_entry *find_bug(unsigned long bugaddr)
 {
        struct bug_entry *bug;
@@ -157,8 +179,9 @@ struct bug_entry *find_bug(unsigned long bugaddr)
 static enum bug_trap_type __report_bug(unsigned long bugaddr, struct pt_regs *regs)
 {
        struct bug_entry *bug;
-       const char *file;
+       const char *file, *function;
        unsigned line, warning, once, done;
+       char __maybe_unused sym[KSYM_SYMBOL_LEN];
 
        if (!is_valid_bugaddr(bugaddr))
                return BUG_TRAP_TYPE_NONE;
@@ -169,12 +192,32 @@ static enum bug_trap_type __report_bug(unsigned long bugaddr, struct pt_regs *re
 
        disable_trace_on_warning();
 
-       bug_get_file_line(bug, &file, &line);
+       bug_get_file_function_line(bug, &file, &function, &line);
+#if defined(CONFIG_KUNIT_SUPPRESS_BACKTRACE) && defined(CONFIG_KALLSYMS)
+       if (!function) {
+               /*
+                * This will be seen if report_bug is called on an architecture
+                * with no architecture-specific support for suppressing warning
+                * backtraces, if CONFIG_DEBUG_BUGVERBOSE is not enabled, or if
+                * the calling code is from assembler which does not record a
+                * function name. Extracting the function name from the bug
+                * address is less than perfect since compiler optimization may
+                * result in 'bugaddr' pointing to a function which does not
+                * actually trigger the warning, but it is better than no
+                * suppression at all.
+                */
+               sprint_symbol_no_offset(sym, bugaddr);
+               function = sym;
+       }
+#endif /* defined(CONFIG_KUNIT_SUPPRESS_BACKTRACE) && defined(CONFIG_KALLSYMS) */
 
        warning = (bug->flags & BUGFLAG_WARNING) != 0;
        once = (bug->flags & BUGFLAG_ONCE) != 0;
        done = (bug->flags & BUGFLAG_DONE) != 0;
 
+       if (warning && KUNIT_IS_SUPPRESSED_WARNING(function))
+               return BUG_TRAP_TYPE_WARN;
+
        if (warning && once) {
                if (done)
                        return BUG_TRAP_TYPE_WARN;
index a97897edd9642f3e5df7fdd9dee26ee5cf00d6a4..201402f0ab49d80f769ea4c4544031accf89f543 100644 (file)
@@ -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
index 5aa51978e456ab3bb60c12071a26cf2bdcb1b508..3195e861d63c42253fb81d649b26fa63e25d9a4a 100644 (file)
@@ -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-y +=                               hooks.o
+# KUnit 'hooks' and bug handling are built-in even when KUnit is built
+# as a module.
+obj-y +=                               hooks.o \
+                                       bug.o
 
 obj-$(CONFIG_KUNIT_TEST) +=            kunit-test.o
 obj-$(CONFIG_KUNIT_TEST) +=            platform-test.o
diff --git a/lib/kunit/bug.c b/lib/kunit/bug.c
new file mode 100644 (file)
index 0000000..351f9a5
--- /dev/null
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * KUnit helpers for backtrace suppression
+ *
+ * Copyright (c) 2024 Guenter Roeck <linux@roeck-us.net>
+ */
+
+#include <kunit/bug.h>
+#include <linux/export.h>
+#include <linux/list.h>
+#include <linux/string.h>
+
+static LIST_HEAD(suppressed_warnings);
+
+void __kunit_start_suppress_warning(struct __suppressed_warning *warning)
+{
+       list_add(&warning->node, &suppressed_warnings);
+}
+EXPORT_SYMBOL_GPL(__kunit_start_suppress_warning);
+
+void __kunit_end_suppress_warning(struct __suppressed_warning *warning)
+{
+       list_del(&warning->node);
+}
+EXPORT_SYMBOL_GPL(__kunit_end_suppress_warning);
+
+bool __kunit_is_suppressed_warning(const char *function)
+{
+       struct __suppressed_warning *warning;
+
+       if (!function)
+               return false;
+
+       list_for_each_entry(warning, &suppressed_warnings, node) {
+               if (!strcmp(function, warning->function))
+                       return true;
+       }
+       return false;
+}
+EXPORT_SYMBOL_GPL(__kunit_is_suppressed_warning);