[RFC PATCH 0/5] Add support for suppressing warning backtraces

11 views
Skip to first unread message

Guenter Roeck

unread,
Mar 5, 2024, 1:40:41 PMMar 5
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, linux-...@vger.kernel.org, loon...@lists.linux.dev, linux-ar...@lists.infradead.org, kuni...@googlegroups.com, linux...@vger.kernel.org, Guenter Roeck
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 marks the warning message in drm_calc_scale() in the DRM
subsystem as intentional where warranted. This patch is intended to serve
as an example for the use of the functionality introduced with this series.
The last three patches in the series introduce the necessary architecture
specific changes for x86, arm64, and loongarch.

This series is based on the RFC patch and subsequent discussion at
https://patchwork.kernel.org/project/linux-kselftest/patch/02546e59-1afe-4b08...@moroto.mountain/
and offers a more comprehensive solution of the problem discussed there.

Checkpatch note:
Remaining checkpatch errors and warnings were deliberately ignored.
Some are triggered by matching coding style or by comments interpreted
as code, others by assembler macros which are disliked by checkpatch.
Suggestions for improvements are welcome.

Some questions:

- Is the general approach promising ? If not, are there other possible
solutions ?
- Function pointers are only added to the __bug_table section if both
CONFIG_KUNIT and CONFIG_DEBUG_BUGVERBOSE are enabled. This avoids image
size increases if CONFIG_KUNIT=n. Downside is slightly more complex
architecture specific assembler code. If function pointers were always
added to the __bug_table section, vmlinux image size would increase by
approximately 0.6-0.7%. Is the increased complexity in assembler code
worth the reduced image size ? I think so, but I would like to hear
other opinions.
- There are additional possibilities associated with storing the bug
function name in the __bug_table section. It could be independent of
KUNIT, it could be a configuration flag, and/or it could be used to
display the name of the offending function in BUG/WARN messages.
Is any of those of interest ?

----------------------------------------------------------------
Guenter Roeck (5):
bug: Core support for suppressing warning backtraces
drm: Suppress intentional warning backtraces in scaling unit tests
x86: Add support for suppressing warning tracebacks
arm64: Add support for suppressing warning tracebacks
loongarch: Add support for suppressing warning tracebacks

arch/arm64/include/asm/asm-bug.h | 29 +++++++++++++-------
arch/arm64/include/asm/bug.h | 8 +++++-
arch/loongarch/include/asm/bug.h | 38 ++++++++++++++++++--------
arch/x86/include/asm/bug.h | 21 +++++++++++----
drivers/gpu/drm/tests/drm_rect_test.c | 6 +++++
include/asm-generic/bug.h | 16 ++++++++---
include/kunit/bug.h | 51 +++++++++++++++++++++++++++++++++++
include/linux/bug.h | 13 +++++++++
lib/bug.c | 51 ++++++++++++++++++++++++++++++++---
lib/kunit/Makefile | 6 +++--
lib/kunit/bug.c | 40 +++++++++++++++++++++++++++
11 files changed, 243 insertions(+), 36 deletions(-)
create mode 100644 include/kunit/bug.h
create mode 100644 lib/kunit/bug.c

Guenter Roeck

unread,
Mar 5, 2024, 1:40:45 PMMar 5
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, linux-...@vger.kernel.org, loon...@lists.linux.dev, linux-ar...@lists.infradead.org, kuni...@googlegroups.com, linux...@vger.kernel.org, Guenter Roeck
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.

Cc: Dan Carpenter <dan.ca...@linaro.org>
Cc: Daniel Diaz <danie...@linaro.org>
Cc: Naresh Kamboju <naresh....@linaro.org>
Cc: Kees Cook <kees...@chromium.org>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
include/asm-generic/bug.h | 16 +++++++++---
include/kunit/bug.h | 51 +++++++++++++++++++++++++++++++++++++++
include/linux/bug.h | 13 ++++++++++
lib/bug.c | 51 ++++++++++++++++++++++++++++++++++++---
lib/kunit/Makefile | 6 +++--
lib/kunit/bug.c | 40 ++++++++++++++++++++++++++++++
6 files changed, 168 insertions(+), 9 deletions(-)
create mode 100644 include/kunit/bug.h
create mode 100644 lib/kunit/bug.c

diff --git a/include/asm-generic/bug.h b/include/asm-generic/bug.h
index 6e794420bd39..b0069564eb8f 100644
--- a/include/asm-generic/bug.h
+++ b/include/asm-generic/bug.h
@@ -2,6 +2,7 @@
#ifndef _ASM_GENERIC_BUG_H
#define _ASM_GENERIC_BUG_H

+#include <kunit/bug.h>
#include <linux/compiler.h>
#include <linux/instrumentation.h>
#include <linux/once_lite.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 (!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 (!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
index 000000000000..11b8ae795320
--- /dev/null
+++ b/include/kunit/bug.h
@@ -0,0 +1,51 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * KUnit helpers for backtrace suppression
+ *
+ * Copyright (c) 2024 Guenter Roeck <li...@roeck-us.net>
+ */
+
+#ifndef _KUNIT_BUG_H
+#define _KUNIT_BUG_H
+
+#ifndef __ASSEMBLY__
+
+#include <linux/kconfig.h>
+
+#if IS_ENABLED(CONFIG_KUNIT)
+
+#include <linux/types.h>
+#include <linux/stringify.h>
+
+struct __suppressed_warning {
+ struct list_head node;
+ const char *function;
+};
+
+void __start_suppress_warning(struct __suppressed_warning *warning);
+void __end_suppress_warning(struct __suppressed_warning *warning);
+bool __is_suppressed_warning(const char *function);
+
+#define DEFINE_SUPPRESSED_WARNING(func) \
+ struct __suppressed_warning __kunit_suppress_##func = \
+ { .function = __stringify(func) }
+
+#define START_SUPPRESSED_WARNING(func) \
+ __start_suppress_warning(&__kunit_suppress_##func)
+
+#define END_SUPPRESSED_WARNING(func) \
+ __end_suppress_warning(&__kunit_suppress_##func)
+
+#define IS_SUPPRESSED_WARNING(func) \
+ __is_suppressed_warning(func)
+
+#else /* CONFIG_KUNIT */
+
+#define DEFINE_SUPPRESSED_WARNING(func)
+#define START_SUPPRESSED_WARNING(func)
+#define END_SUPPRESSED_WARNING(func)
+#define IS_SUPPRESSED_WARNING(func) (false)
+
+#endif /* CONFIG_KUNIT */
+#endif /* __ASSEMBLY__ */
+#endif /* _KUNIT_BUG_H */
diff --git a/include/linux/bug.h b/include/linux/bug.h
index 348acf2558f3..c668762dc76a 100644
--- a/include/linux/bug.h
+++ b/include/linux/bug.h
@@ -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)
{
diff --git a/lib/bug.c b/lib/bug.c
index e0ff21989990..6b85d4042e07 100644
--- a/lib/bug.c
+++ b/lib/bug.c
@@ -26,6 +26,14 @@
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 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 IS_ENABLED(CONFIG_KUNIT) && 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 /* IS_ENABLED(CONFIG_KUNIT) && defined(CONFIG_KALLSYMS) */

warning = (bug->flags & BUGFLAG_WARNING) != 0;
once = (bug->flags & BUGFLAG_ONCE) != 0;
done = (bug->flags & BUGFLAG_DONE) != 0;

+ if (warning && IS_SUPPRESSED_WARNING(function))
+ return BUG_TRAP_TYPE_WARN;
+
if (warning && once) {
if (done)
return BUG_TRAP_TYPE_WARN;
diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile
index 309659a32a78..545b57c3be48 100644
--- a/lib/kunit/Makefile
+++ b/lib/kunit/Makefile
@@ -14,8 +14,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

diff --git a/lib/kunit/bug.c b/lib/kunit/bug.c
new file mode 100644
index 000000000000..f93544d7a9d1
--- /dev/null
+++ b/lib/kunit/bug.c
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * KUnit helpers for backtrace suppression
+ *
+ * Copyright (c) 2024 Guenter Roeck <li...@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 __start_suppress_warning(struct __suppressed_warning *warning)
+{
+ list_add(&warning->node, &suppressed_warnings);
+}
+EXPORT_SYMBOL_GPL(__start_suppress_warning);
+
+void __end_suppress_warning(struct __suppressed_warning *warning)
+{
+ list_del(&warning->node);
+}
+EXPORT_SYMBOL_GPL(__end_suppress_warning);
+
+bool __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(__is_suppressed_warning);
--
2.39.2

Guenter Roeck

unread,
Mar 5, 2024, 1:40:46 PMMar 5
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, linux-...@vger.kernel.org, loon...@lists.linux.dev, linux-ar...@lists.infradead.org, kuni...@googlegroups.com, linux...@vger.kernel.org, Guenter Roeck
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.

Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
drivers/gpu/drm/tests/drm_rect_test.c | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/drivers/gpu/drm/tests/drm_rect_test.c b/drivers/gpu/drm/tests/drm_rect_test.c
index 76332cd2ead8..75614cb4deb5 100644
--- a/drivers/gpu/drm/tests/drm_rect_test.c
+++ b/drivers/gpu/drm/tests/drm_rect_test.c
@@ -406,22 +406,28 @@ KUNIT_ARRAY_PARAM(drm_rect_scale, drm_rect_scale_cases, drm_rect_scale_case_desc

static void drm_test_rect_calc_hscale(struct kunit *test)
{
+ DEFINE_SUPPRESSED_WARNING(drm_calc_scale);
const struct drm_rect_scale_case *params = test->param_value;
int scaling_factor;

+ START_SUPPRESSED_WARNING(drm_calc_scale);
scaling_factor = drm_rect_calc_hscale(&params->src, &params->dst,
params->min_range, params->max_range);
+ END_SUPPRESSED_WARNING(drm_calc_scale);

KUNIT_EXPECT_EQ(test, scaling_factor, params->expected_scaling_factor);
}

static void drm_test_rect_calc_vscale(struct kunit *test)
{
+ DEFINE_SUPPRESSED_WARNING(drm_calc_scale);
const struct drm_rect_scale_case *params = test->param_value;
int scaling_factor;

+ START_SUPPRESSED_WARNING(drm_calc_scale);
scaling_factor = drm_rect_calc_vscale(&params->src, &params->dst,
params->min_range, params->max_range);
+ END_SUPPRESSED_WARNING(drm_calc_scale);

KUNIT_EXPECT_EQ(test, scaling_factor, params->expected_scaling_factor);
}
--
2.39.2

Guenter Roeck

unread,
Mar 5, 2024, 1:40:48 PMMar 5
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, linux-...@vger.kernel.org, loon...@lists.linux.dev, linux-ar...@lists.infradead.org, kuni...@googlegroups.com, linux...@vger.kernel.org, Guenter Roeck
Add support for selectively suppressing WARNING tracebacks to x86.
This requires adding the function triggering tracebacks to the __bug_table
object section.

To limit image size impact, the pointer to the function name is only added
to the __bug_table section if both CONFIG_KUNIT and CONFIG_DEBUG_BUGVERBOSE
are enabled. Otherwise, the __func__ assembly parameter is replaced with a
(dummy) NULL parameter to avoid an image size increase due to unused
__func__ entries (this is necessary because __func__ is not a define but a
virtual variable).

Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
arch/x86/include/asm/bug.h | 21 ++++++++++++++++-----
1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/arch/x86/include/asm/bug.h b/arch/x86/include/asm/bug.h
index a3ec87d198ac..8e45143fa676 100644
--- a/arch/x86/include/asm/bug.h
+++ b/arch/x86/include/asm/bug.h
@@ -21,6 +21,15 @@
# define __BUG_REL(val) ".long " __stringify(val) " - ."
#endif

+#if IS_ENABLED(CONFIG_KUNIT)
+# define HAVE_BUG_FUNCTION
+# define __BUG_FUNC_PTR __BUG_REL(%c1)
+# define __BUG_FUNC __func__
+#else
+# define __BUG_FUNC_PTR
+# define __BUG_FUNC NULL
+#endif /* IS_ENABLED(CONFIG_KUNIT) */
+
#ifdef CONFIG_DEBUG_BUGVERBOSE

#define _BUG_FLAGS(ins, flags, extra) \
@@ -29,12 +38,13 @@ do { \
".pushsection __bug_table,\"aw\"\n" \
"2:\t" __BUG_REL(1b) "\t# bug_entry::bug_addr\n" \
"\t" __BUG_REL(%c0) "\t# bug_entry::file\n" \
- "\t.word %c1" "\t# bug_entry::line\n" \
- "\t.word %c2" "\t# bug_entry::flags\n" \
- "\t.org 2b+%c3\n" \
+ "\t" __BUG_FUNC_PTR "\t# bug_entry::function\n" \
+ "\t.word %c2" "\t# bug_entry::line\n" \
+ "\t.word %c3" "\t# bug_entry::flags\n" \
+ "\t.org 2b+%c4\n" \
".popsection\n" \
extra \
- : : "i" (__FILE__), "i" (__LINE__), \
+ : : "i" (__FILE__), "i" (__BUG_FUNC), "i" (__LINE__),\
"i" (flags), \
"i" (sizeof(struct bug_entry))); \
} while (0)
@@ -80,7 +90,8 @@ do { \
do { \
__auto_type __flags = BUGFLAG_WARNING|(flags); \
instrumentation_begin(); \
- _BUG_FLAGS(ASM_UD2, __flags, ASM_REACHABLE); \
+ if (!IS_SUPPRESSED_WARNING(__func__)) \
+ _BUG_FLAGS(ASM_UD2, __flags, ASM_REACHABLE); \
instrumentation_end(); \
} while (0)

--
2.39.2

Guenter Roeck

unread,
Mar 5, 2024, 1:40:50 PMMar 5
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, linux-...@vger.kernel.org, loon...@lists.linux.dev, linux-ar...@lists.infradead.org, kuni...@googlegroups.com, linux...@vger.kernel.org, Guenter Roeck
Add support for selectively suppressing WARNING tracebacks to arm64.
This requires adding the function triggering tracebacks to the __bug_table
object section.

To limit image size impact, the pointer to the function name is only added
to the __bug_table section if both CONFIG_KUNIT and CONFIG_DEBUG_BUGVERBOSE
are enabled. Otherwise, the __func__ assembly parameter is replaced with a
(dummy) NULL parameter to avoid an image size increase due to unused
__func__ entries (this is necessary because __func__ is not a define but a
virtual variable).

Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
arch/arm64/include/asm/asm-bug.h | 29 +++++++++++++++++++----------
arch/arm64/include/asm/bug.h | 8 +++++++-
2 files changed, 26 insertions(+), 11 deletions(-)

diff --git a/arch/arm64/include/asm/asm-bug.h b/arch/arm64/include/asm/asm-bug.h
index c762038ba400..6884089a7191 100644
--- a/arch/arm64/include/asm/asm-bug.h
+++ b/arch/arm64/include/asm/asm-bug.h
@@ -8,36 +8,45 @@
#include <asm/brk-imm.h>

#ifdef CONFIG_DEBUG_BUGVERBOSE
-#define _BUGVERBOSE_LOCATION(file, line) __BUGVERBOSE_LOCATION(file, line)
-#define __BUGVERBOSE_LOCATION(file, line) \
+
+#if IS_ENABLED(CONFIG_KUNIT)
+# define HAVE_BUG_FUNCTION
+# define __BUG_FUNC_PTR(func) .long func - .;
+#else
+# define __BUG_FUNC_PTR(func)
+#endif
+
+#define _BUGVERBOSE_LOCATION(file, func, line) __BUGVERBOSE_LOCATION(file, func, line)
+#define __BUGVERBOSE_LOCATION(file, func, line) \
.pushsection .rodata.str,"aMS",@progbits,1; \
14472: .string file; \
.popsection; \
\
.long 14472b - .; \
+ __BUG_FUNC_PTR(func) \
.short line;
#else
-#define _BUGVERBOSE_LOCATION(file, line)
+#define _BUGVERBOSE_LOCATION(file, func, line)
#endif

#ifdef CONFIG_GENERIC_BUG

-#define __BUG_ENTRY(flags) \
+#define __BUG_ENTRY(flags, func) \
.pushsection __bug_table,"aw"; \
.align 2; \
14470: .long 14471f - .; \
-_BUGVERBOSE_LOCATION(__FILE__, __LINE__) \
- .short flags; \
+_BUGVERBOSE_LOCATION(__FILE__, func, __LINE__) \
+ .short flags; \
.popsection; \
14471:
#else
-#define __BUG_ENTRY(flags)
+#define __BUG_ENTRY(flags, func)
#endif

-#define ASM_BUG_FLAGS(flags) \
- __BUG_ENTRY(flags) \
+#define ASM_BUG_FLAGS(flags, func) \
+ __BUG_ENTRY(flags, func) \
brk BUG_BRK_IMM

-#define ASM_BUG() ASM_BUG_FLAGS(0)
+#define ASM_BUG() ASM_BUG_FLAGS(0, .)

#endif /* __ASM_ASM_BUG_H */
diff --git a/arch/arm64/include/asm/bug.h b/arch/arm64/include/asm/bug.h
index 28be048db3f6..044c5e24a17d 100644
--- a/arch/arm64/include/asm/bug.h
+++ b/arch/arm64/include/asm/bug.h
@@ -11,8 +11,14 @@

#include <asm/asm-bug.h>

+#ifdef HAVE_BUG_FUNCTION
+# define __BUG_FUNC __func__
+#else
+# define __BUG_FUNC NULL
+#endif
+
#define __BUG_FLAGS(flags) \
- asm volatile (__stringify(ASM_BUG_FLAGS(flags)));
+ asm volatile (__stringify(ASM_BUG_FLAGS(flags, %c0)) : : "i" (__BUG_FUNC));

#define BUG() do { \
__BUG_FLAGS(0); \
--
2.39.2

Guenter Roeck

unread,
Mar 5, 2024, 1:40:54 PMMar 5
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, linux-...@vger.kernel.org, loon...@lists.linux.dev, linux-ar...@lists.infradead.org, kuni...@googlegroups.com, linux...@vger.kernel.org, Guenter Roeck
Add support for selectively suppressing WARNING tracebacks to loongarch.
This requires adding the function triggering tracebacks to the __bug_table
object section.

To limit image size impact, the pointer to the function name is only added
to the __bug_table section if both CONFIG_KUNIT and CONFIG_DEBUG_BUGVERBOSE
are enabled. Otherwise, the __func__ assembly parameter is replaced with a
(dummy) NULL parameter to avoid an image size increase due to unused
__func__ entries (this is necessary because __func__ is not a define but a
virtual variable).

Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
arch/loongarch/include/asm/bug.h | 38 +++++++++++++++++++++++---------
1 file changed, 27 insertions(+), 11 deletions(-)

diff --git a/arch/loongarch/include/asm/bug.h b/arch/loongarch/include/asm/bug.h
index d4ca3ba25418..25f2b5ae7702 100644
--- a/arch/loongarch/include/asm/bug.h
+++ b/arch/loongarch/include/asm/bug.h
@@ -3,47 +3,63 @@
#define __ASM_BUG_H

#include <asm/break.h>
+#include <kunit/bug.h>
#include <linux/stringify.h>

#ifndef CONFIG_DEBUG_BUGVERBOSE
-#define _BUGVERBOSE_LOCATION(file, line)
+#define _BUGVERBOSE_LOCATION(file, func, line)
#else
-#define __BUGVERBOSE_LOCATION(file, line) \
+#if IS_ENABLED(CONFIG_KUNIT)
+# define HAVE_BUG_FUNCTION
+# define __BUG_FUNC_PTR(func) .long func - .;
+#else
+# define __BUG_FUNC_PTR(func)
+#endif
+
+#define __BUGVERBOSE_LOCATION(file, func, line) \
.pushsection .rodata.str, "aMS", @progbits, 1; \
10002: .string file; \
.popsection; \
\
.long 10002b - .; \
+ __BUG_FUNC_PTR(func) \
.short line;
-#define _BUGVERBOSE_LOCATION(file, line) __BUGVERBOSE_LOCATION(file, line)
+#define _BUGVERBOSE_LOCATION(file, func, line) __BUGVERBOSE_LOCATION(file, func, line)
#endif

#ifndef CONFIG_GENERIC_BUG
-#define __BUG_ENTRY(flags)
+#define __BUG_ENTRY(flags, func)
#else
-#define __BUG_ENTRY(flags) \
+#define __BUG_ENTRY(flags, func) \
.pushsection __bug_table, "aw"; \
.align 2; \
10000: .long 10001f - .; \
- _BUGVERBOSE_LOCATION(__FILE__, __LINE__) \
+ _BUGVERBOSE_LOCATION(__FILE__, func, __LINE__) \
.short flags; \
.popsection; \
10001:
#endif

-#define ASM_BUG_FLAGS(flags) \
- __BUG_ENTRY(flags) \
+#define ASM_BUG_FLAGS(flags, func) \
+ __BUG_ENTRY(flags, func) \
break BRK_BUG

-#define ASM_BUG() ASM_BUG_FLAGS(0)
+#define ASM_BUG() ASM_BUG_FLAGS(0, .)
+
+#ifdef HAVE_BUG_FUNCTION
+# define __BUG_FUNC __func__
+#else
+# define __BUG_FUNC NULL
+#endif

#define __BUG_FLAGS(flags) \
- asm_inline volatile (__stringify(ASM_BUG_FLAGS(flags)));
+ asm_inline volatile (__stringify(ASM_BUG_FLAGS(flags, %0)) : : "i" (__BUG_FUNC));

#define __WARN_FLAGS(flags) \
do { \
instrumentation_begin(); \
- __BUG_FLAGS(BUGFLAG_WARNING|(flags)); \
+ if (!IS_SUPPRESSED_WARNING(__func__)) \
+ __BUG_FLAGS(BUGFLAG_WARNING|(flags)); \

Kees Cook

unread,
Mar 5, 2024, 2:54:09 PMMar 5
to Guenter Roeck, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, linux-...@vger.kernel.org, loon...@lists.linux.dev, linux-ar...@lists.infradead.org, kuni...@googlegroups.com, linux...@vger.kernel.org
On Tue, Mar 05, 2024 at 10:40:29AM -0800, Guenter Roeck wrote:
> [...]
> warning = (bug->flags & BUGFLAG_WARNING) != 0;
> once = (bug->flags & BUGFLAG_ONCE) != 0;
> done = (bug->flags & BUGFLAG_DONE) != 0;
>
> + if (warning && IS_SUPPRESSED_WARNING(function))
> + return BUG_TRAP_TYPE_WARN;
> +

I had to re-read __report_bug() more carefully, but yes, this works --
it's basically leaving early, like "once" does.

This looks like a reasonable approach!

Something very similar to this is checking that a warning happens. i.e.
you talk about drm selftests checking function return values, but I've
got a bunch of tests (LKDTM) that live outside of KUnit because I haven't
had a clean way to check for specific warnings/bugs. I feel like future
changes built on top of this series could add counters or something that
KUnit could examine. E.g. I did this manually for some fortify tests:

https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git/commit/?h=for-next/hardening&id=4ce615e798a752d4431fcc52960478906dec2f0e

-Kees

--
Kees Cook

Guenter Roeck

unread,
Mar 5, 2024, 3:18:01 PMMar 5
to Kees Cook, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, linux-...@vger.kernel.org, loon...@lists.linux.dev, linux-ar...@lists.infradead.org, kuni...@googlegroups.com, linux...@vger.kernel.org
Sounds like a good idea. It should be straightforward to add a counter
to struct __suppressed_warning. This way the calling code could easily
check if an expected warning backtrace actually happened.

Thanks,
Guenter

Daniel Díaz

unread,
Mar 6, 2024, 1:24:18 PMMar 6
to Guenter Roeck, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, linux-...@vger.kernel.org, loon...@lists.linux.dev, linux-ar...@lists.infradead.org, kuni...@googlegroups.com, linux...@vger.kernel.org
Hello!
Thank you SO very much for this work! This is very much appreciated!
We run into these warnings at LKFT all the time, and making sure that
the noise doesn't drown the relevant signal is very important.

Greetings!

Daniel Díaz
danie...@linaro.org

Guenter Roeck

unread,
Mar 6, 2024, 1:57:17 PMMar 6
to Daniel Díaz, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, linux-...@vger.kernel.org, loon...@lists.linux.dev, linux-ar...@lists.infradead.org, kuni...@googlegroups.com, linux...@vger.kernel.org
Hi Daniel,

On 3/6/24 10:24, Daniel Díaz wrote:
[ ... ]
>
> Thank you SO very much for this work! This is very much appreciated!

Thanks a lot for the feedback.

> We run into these warnings at LKFT all the time, and making sure that
> the noise doesn't drown the relevant signal is very important.
>

Can you send me a list of all the warnings you are seeing ? I do see
lots of warnings when running drm tests in qemu, but I am not sure if
those are caused by emulation problems or if they are expected.
A list of warnings seen on real hardware would help me prepare
additional patches to address (or, rather, suppress) those.

Thanks,
Guenter

Guenter Roeck

unread,
Mar 11, 2024, 12:36:14 AMMar 11
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, linux-...@vger.kernel.org, loon...@lists.linux.dev, linux-ar...@lists.infradead.org, kuni...@googlegroups.com, linux...@vger.kernel.org
I am ready to send a full version of this series with support for
all affected architectures. I am undecided if I should send it now,
based on v6.8, and send v2 after rebasing it to v6.9-rc1, or if I
should just wait for v6.9-rc1.

I understand that some maintainers dislike getting new patch series
while the commit window is is open. On the ther side, I tested the
series thoroughly on top of v6.8-rc7, and initial v6.9 release candidates
may have their own problems. Given that, I tend to send the series now.

Any thoughts ? Unless there is strong negative feedback, I'll likely
do that in a day or two.

Thanks,
Guenter

Guenter Roeck

unread,
Mar 12, 2024, 1:03:17 PMMar 12
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev, Guenter Roeck
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.

This series is based on the RFC patch and subsequent discussion at
https://patchwork.kernel.org/project/linux-kselftest/patch/02546e59-1afe-4b08...@moroto.mountain/
and offers a more comprehensive solution of the problem discussed there.

Design note:
Function pointers are only added to the __bug_table section if both
CONFIG_KUNIT and CONFIG_DEBUG_BUGVERBOSE are enabled to avoid image
size increases if CONFIG_KUNIT=n. There would be some benefits to
adding those pointers all the time (reduced complexity, ability to
display function names in BUG/WARNING messages). That change, if
desired, can be made later.

Checkpatch note:
Remaining checkpatch errors and warnings were deliberately ignored.
Some are triggered by matching coding style or by comments interpreted
as code, others by assembler macros which are disliked by checkpatch.
Suggestions for improvements are welcome.

Changes since RFC:
- 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

Guenter Roeck

unread,
Mar 12, 2024, 1:03:18 PMMar 12
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev, Guenter Roeck
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.

Cc: Dan Carpenter <dan.ca...@linaro.org>
Cc: Daniel Diaz <danie...@linaro.org>
Cc: Naresh Kamboju <naresh....@linaro.org>
Cc: Kees Cook <kees...@chromium.org>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
include/asm-generic/bug.h | 16 +++++++++---
include/kunit/bug.h | 51 +++++++++++++++++++++++++++++++++++++++
include/kunit/test.h | 1 +
include/linux/bug.h | 13 ++++++++++
lib/bug.c | 51 ++++++++++++++++++++++++++++++++++++---
lib/kunit/Makefile | 6 +++--
lib/kunit/bug.c | 40 ++++++++++++++++++++++++++++++
7 files changed, 169 insertions(+), 9 deletions(-)
create mode 100644 include/kunit/bug.h
create mode 100644 lib/kunit/bug.c

diff --git a/include/asm-generic/bug.h b/include/asm-generic/bug.h
index 6e794420bd39..c170b6477689 100644
--- a/include/asm-generic/bug.h
+++ b/include/asm-generic/bug.h
@@ -18,6 +18,7 @@
#endif

#ifndef __ASSEMBLY__
+#include <kunit/bug.h>
#include <linux/panic.h>
#include <linux/printk.h>
index 000000000000..1e34da961599
--- /dev/null
+++ b/include/kunit/bug.h
@@ -0,0 +1,51 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * KUnit helpers for backtrace suppression
+ *
+ * Copyright (c) 2024 Guenter Roeck <li...@roeck-us.net>
+ */
+
+#ifndef _KUNIT_BUG_H
+#define _KUNIT_BUG_H
+
+#ifndef __ASSEMBLY__
+
+#include <linux/kconfig.h>
+
+#if IS_ENABLED(CONFIG_KUNIT)
+
+#include <linux/stringify.h>
+#include <linux/types.h>
diff --git a/include/kunit/test.h b/include/kunit/test.h
index fcb4a4940ace..894c9fd1495d 100644
--- a/include/kunit/test.h
+++ b/include/kunit/test.h
@@ -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>
warning = (bug->flags & BUGFLAG_WARNING) != 0;
once = (bug->flags & BUGFLAG_ONCE) != 0;
done = (bug->flags & BUGFLAG_DONE) != 0;

+ if (warning && IS_SUPPRESSED_WARNING(function))
+ return BUG_TRAP_TYPE_WARN;
+

Guenter Roeck

unread,
Mar 12, 2024, 1:03:21 PMMar 12
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev, Guenter Roeck
Count suppressed warning backtraces to enable code which suppresses
warning backtraces to check if the expected backtrace(s) have been
observed.

Using atomics for the backtrace count resulted in build errors on some
architectures due to include file recursion, so use a plain integer
for now.

Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
include/kunit/bug.h | 7 ++++++-
lib/kunit/bug.c | 4 +++-
2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/include/kunit/bug.h b/include/kunit/bug.h
index 1e34da961599..2097a854ac8c 100644
--- a/include/kunit/bug.h
+++ b/include/kunit/bug.h
@@ -20,6 +20,7 @@
struct __suppressed_warning {
struct list_head node;
const char *function;
+ int counter;
};

void __start_suppress_warning(struct __suppressed_warning *warning);
@@ -28,7 +29,7 @@ bool __is_suppressed_warning(const char *function);

#define DEFINE_SUPPRESSED_WARNING(func) \
struct __suppressed_warning __kunit_suppress_##func = \
- { .function = __stringify(func) }
+ { .function = __stringify(func), .counter = 0 }

#define START_SUPPRESSED_WARNING(func) \
__start_suppress_warning(&__kunit_suppress_##func)
@@ -39,12 +40,16 @@ bool __is_suppressed_warning(const char *function);
#define IS_SUPPRESSED_WARNING(func) \
__is_suppressed_warning(func)

+#define SUPPRESSED_WARNING_COUNT(func) \
+ (__kunit_suppress_##func.counter)
+
#else /* CONFIG_KUNIT */

#define DEFINE_SUPPRESSED_WARNING(func)
#define START_SUPPRESSED_WARNING(func)
#define END_SUPPRESSED_WARNING(func)
#define IS_SUPPRESSED_WARNING(func) (false)
+#define SUPPRESSED_WARNING_COUNT(func) (0)

#endif /* CONFIG_KUNIT */
#endif /* __ASSEMBLY__ */
diff --git a/lib/kunit/bug.c b/lib/kunit/bug.c
index f93544d7a9d1..13b3d896c114 100644
--- a/lib/kunit/bug.c
+++ b/lib/kunit/bug.c
@@ -32,8 +32,10 @@ bool __is_suppressed_warning(const char *function)
return false;

list_for_each_entry(warning, &suppressed_warnings, node) {
- if (!strcmp(function, warning->function))
+ if (!strcmp(function, warning->function)) {
+ warning->counter++;
return true;
+ }
}
return false;
}
--
2.39.2

Guenter Roeck

unread,
Mar 12, 2024, 1:03:22 PMMar 12
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev, Guenter Roeck
Add unit tests to verify that warning backtrace suppression works.

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.

Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
lib/kunit/Makefile | 3 +-
lib/kunit/backtrace-suppression-test.c | 104 +++++++++++++++++++++++++
2 files changed, 106 insertions(+), 1 deletion(-)
create mode 100644 lib/kunit/backtrace-suppression-test.c

diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile
index 545b57c3be48..6a44d2b54ea9 100644
--- a/lib/kunit/Makefile
+++ b/lib/kunit/Makefile
@@ -19,7 +19,8 @@ endif
obj-y += hooks.o \
bug.o

-obj-$(CONFIG_KUNIT_TEST) += kunit-test.o
+obj-$(CONFIG_KUNIT_TEST) += kunit-test.o \
+ backtrace-suppression-test.o

# string-stream-test compiles built-in only.
ifeq ($(CONFIG_KUNIT_TEST),y)
diff --git a/lib/kunit/backtrace-suppression-test.c b/lib/kunit/backtrace-suppression-test.c
new file mode 100644
index 000000000000..47c619283802
--- /dev/null
+++ b/lib/kunit/backtrace-suppression-test.c
@@ -0,0 +1,104 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * KUnit test for suppressing warning tracebacks
+ *
+ * Copyright (C) 2024, Guenter Roeck
+ * Author: Guenter Roeck <li...@roeck-us.net>
+ */
+
+#include <kunit/test.h>
+#include <linux/bug.h>
+
+static void backtrace_suppression_test_warn_direct(struct kunit *test)
+{
+ DEFINE_SUPPRESSED_WARNING(backtrace_suppression_test_warn_direct);
+
+ START_SUPPRESSED_WARNING(backtrace_suppression_test_warn_direct);
+ WARN(1, "This backtrace should be suppressed");
+ END_SUPPRESSED_WARNING(backtrace_suppression_test_warn_direct);
+
+ KUNIT_EXPECT_EQ(test, SUPPRESSED_WARNING_COUNT(backtrace_suppression_test_warn_direct), 1);
+}
+
+static void trigger_backtrace_warn(void)
+{
+ WARN(1, "This backtrace should be suppressed");
+}
+
+static void backtrace_suppression_test_warn_indirect(struct kunit *test)
+{
+ DEFINE_SUPPRESSED_WARNING(trigger_backtrace_warn);
+
+ START_SUPPRESSED_WARNING(trigger_backtrace_warn);
+ trigger_backtrace_warn();
+ END_SUPPRESSED_WARNING(trigger_backtrace_warn);
+
+ KUNIT_EXPECT_EQ(test, SUPPRESSED_WARNING_COUNT(trigger_backtrace_warn), 1);
+}
+
+static void backtrace_suppression_test_warn_multi(struct kunit *test)
+{
+ DEFINE_SUPPRESSED_WARNING(trigger_backtrace_warn);
+ DEFINE_SUPPRESSED_WARNING(backtrace_suppression_test_warn_multi);
+
+ START_SUPPRESSED_WARNING(backtrace_suppression_test_warn_multi);
+ START_SUPPRESSED_WARNING(trigger_backtrace_warn);
+ WARN(1, "This backtrace should be suppressed");
+ trigger_backtrace_warn();
+ END_SUPPRESSED_WARNING(trigger_backtrace_warn);
+ END_SUPPRESSED_WARNING(backtrace_suppression_test_warn_multi);
+
+ KUNIT_EXPECT_EQ(test, SUPPRESSED_WARNING_COUNT(backtrace_suppression_test_warn_multi), 1);
+ KUNIT_EXPECT_EQ(test, SUPPRESSED_WARNING_COUNT(trigger_backtrace_warn), 1);
+}
+
+static void backtrace_suppression_test_warn_on_direct(struct kunit *test)
+{
+ DEFINE_SUPPRESSED_WARNING(backtrace_suppression_test_warn_on_direct);
+
+ if (!IS_ENABLED(CONFIG_DEBUG_BUGVERBOSE) && !IS_ENABLED(CONFIG_KALLSYMS))
+ kunit_skip(test, "requires CONFIG_DEBUG_BUGVERBOSE or CONFIG_KALLSYMS");
+
+ START_SUPPRESSED_WARNING(backtrace_suppression_test_warn_on_direct);
+ WARN_ON(1);
+ END_SUPPRESSED_WARNING(backtrace_suppression_test_warn_on_direct);
+
+ KUNIT_EXPECT_EQ(test,
+ SUPPRESSED_WARNING_COUNT(backtrace_suppression_test_warn_on_direct), 1);
+}
+
+static void trigger_backtrace_warn_on(void)
+{
+ WARN_ON(1);
+}
+
+static void backtrace_suppression_test_warn_on_indirect(struct kunit *test)
+{
+ DEFINE_SUPPRESSED_WARNING(trigger_backtrace_warn_on);
+
+ if (!IS_ENABLED(CONFIG_DEBUG_BUGVERBOSE))
+ kunit_skip(test, "requires CONFIG_DEBUG_BUGVERBOSE");
+
+ START_SUPPRESSED_WARNING(trigger_backtrace_warn_on);
+ trigger_backtrace_warn_on();
+ END_SUPPRESSED_WARNING(trigger_backtrace_warn_on);
+
+ KUNIT_EXPECT_EQ(test, SUPPRESSED_WARNING_COUNT(trigger_backtrace_warn_on), 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");
--
2.39.2

Guenter Roeck

unread,
Mar 12, 2024, 1:03:24 PMMar 12
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev, Guenter Roeck
Document API functions for suppressing warning backtraces.

Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
Documentation/dev-tools/kunit/usage.rst | 30 ++++++++++++++++++++++++-
1 file changed, 29 insertions(+), 1 deletion(-)

diff --git a/Documentation/dev-tools/kunit/usage.rst b/Documentation/dev-tools/kunit/usage.rst
index 22955d56b379..8d3d36d4103d 100644
--- a/Documentation/dev-tools/kunit/usage.rst
+++ b/Documentation/dev-tools/kunit/usage.rst
@@ -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. To suppress a backtrace in some_function(),
+use the following code.
+
+.. code-block:: c
+
+ static void some_test(struct kunit *test)
+ {
+ DEFINE_SUPPRESSED_WARNING(some_function);
+
+ START_SUPPRESSED_WARNING(some_function);
+ trigger_backtrace();
+ END_SUPPRESSED_WARNING(some_function);
+ }
+
+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, SUPPRESSED_WARNING_COUNT(some_function), 1);

Test Suites
~~~~~~~~~~~
@@ -857,4 +885,4 @@ For example:
dev_managed_string = devm_kstrdup(fake_device, "Hello, World!");

// Everything is cleaned up automatically when the test ends.
- }
\ No newline at end of file
+ }
--
2.39.2

Guenter Roeck

unread,
Mar 12, 2024, 1:03:26 PMMar 12
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev, Guenter Roeck
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.

Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---

Guenter Roeck

unread,
Mar 12, 2024, 1:03:28 PMMar 12
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev, Guenter Roeck, Jakub Kicinski
dev_addr_lists_test generates lock warning noise at the end of tests
if lock debugging is enabled. There are two sets of warnings.

WARNING: CPU: 0 PID: 689 at kernel/locking/mutex.c:923 __mutex_unlock_slowpath.constprop.0+0x13c/0x368
DEBUG_LOCKS_WARN_ON(__owner_task(owner) != __get_current())

WARNING: kunit_try_catch/1336 still has locks held!

KUnit test cleanup is not guaranteed to run in the same thread as the test
itself. For this test, this means that rtnl_lock() and rtnl_unlock() may
be called from different threads. This triggers the warnings.
Suppress the warnings because they are irrelevant for the test and just
confusing.

The first warning can be suppressed by using START_SUPPRESSED_WARNING()
and END_SUPPRESSED_WARNING() around the call to rtnl_unlock(). To suppress
the second warning, it is necessary to set debug_locks_silent while the
rtnl lock is held.

Cc: David Gow <davi...@google.com>
Cc: Jakub Kicinski <ku...@kernel.org>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
net/core/dev_addr_lists_test.c | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/net/core/dev_addr_lists_test.c b/net/core/dev_addr_lists_test.c
index 4dbd0dc6aea2..b427dd1a3c93 100644
--- a/net/core/dev_addr_lists_test.c
+++ b/net/core/dev_addr_lists_test.c
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0-or-later

#include <kunit/test.h>
+#include <linux/debug_locks.h>
#include <linux/etherdevice.h>
#include <linux/netdevice.h>
#include <linux/rtnetlink.h>
@@ -49,6 +50,7 @@ static int dev_addr_test_init(struct kunit *test)
KUNIT_FAIL(test, "Can't register netdev %d", err);
}

+ debug_locks_silent = 1;
rtnl_lock();
return 0;
}
@@ -56,8 +58,12 @@ static int dev_addr_test_init(struct kunit *test)
static void dev_addr_test_exit(struct kunit *test)
{
struct net_device *netdev = test->priv;
+ DEFINE_SUPPRESSED_WARNING(__mutex_unlock_slowpath);

+ START_SUPPRESSED_WARNING(__mutex_unlock_slowpath);
rtnl_unlock();
+ END_SUPPRESSED_WARNING(__mutex_unlock_slowpath);
+ debug_locks_silent = 0;
unregister_netdev(netdev);
free_netdev(netdev);
}
--
2.39.2

Guenter Roeck

unread,
Mar 12, 2024, 1:03:30 PMMar 12
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev, Guenter Roeck
Add name of functions triggering warning backtraces to the __bug_table
object section to enable support for suppressing WARNING backtraces.

To limit image size impact, the pointer to the function name is only added
to the __bug_table section if both CONFIG_KUNIT and CONFIG_DEBUG_BUGVERBOSE
are enabled. Otherwise, the __func__ assembly parameter is replaced with a
(dummy) NULL parameter to avoid an image size increase due to unused
__func__ entries (this is necessary because __func__ is not a define but a
virtual variable).

Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
arch/x86/include/asm/bug.h | 21 ++++++++++++++++-----
1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/arch/x86/include/asm/bug.h b/arch/x86/include/asm/bug.h
index a3ec87d198ac..d87bf8bab238 100644
--- a/arch/x86/include/asm/bug.h
+++ b/arch/x86/include/asm/bug.h
@@ -23,18 +23,28 @@

#ifdef CONFIG_DEBUG_BUGVERBOSE

+#if IS_ENABLED(CONFIG_KUNIT)
+# define HAVE_BUG_FUNCTION
+# define __BUG_FUNC_PTR __BUG_REL(%c1)
+# define __BUG_FUNC __func__
+#else
+# define __BUG_FUNC_PTR
+# define __BUG_FUNC NULL
+#endif /* IS_ENABLED(CONFIG_KUNIT) */
+
#define _BUG_FLAGS(ins, flags, extra) \
do { \
asm_inline volatile("1:\t" ins "\n" \
".pushsection __bug_table,\"aw\"\n" \
"2:\t" __BUG_REL(1b) "\t# bug_entry::bug_addr\n" \
"\t" __BUG_REL(%c0) "\t# bug_entry::file\n" \
- "\t.word %c1" "\t# bug_entry::line\n" \
- "\t.word %c2" "\t# bug_entry::flags\n" \
- "\t.org 2b+%c3\n" \
+ "\t" __BUG_FUNC_PTR "\t# bug_entry::function\n" \
+ "\t.word %c2" "\t# bug_entry::line\n" \
+ "\t.word %c3" "\t# bug_entry::flags\n" \
+ "\t.org 2b+%c4\n" \
".popsection\n" \
extra \
- : : "i" (__FILE__), "i" (__LINE__), \
+ : : "i" (__FILE__), "i" (__BUG_FUNC), "i" (__LINE__),\
"i" (flags), \
"i" (sizeof(struct bug_entry))); \
} while (0)
@@ -80,7 +90,8 @@ do { \
do { \
__auto_type __flags = BUGFLAG_WARNING|(flags); \
instrumentation_begin(); \
- _BUG_FLAGS(ASM_UD2, __flags, ASM_REACHABLE); \
+ if (!IS_SUPPRESSED_WARNING(__func__)) \
+ _BUG_FLAGS(ASM_UD2, __flags, ASM_REACHABLE); \

Guenter Roeck

unread,
Mar 12, 2024, 1:03:33 PMMar 12
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev, Guenter Roeck
Add name of functions triggering warning backtraces to the __bug_table
object section to enable support for suppressing WARNING backtraces.

To limit image size impact, the pointer to the function name is only added
to the __bug_table section if both CONFIG_KUNIT and CONFIG_DEBUG_BUGVERBOSE
are enabled. Otherwise, the __func__ assembly parameter is replaced with a
(dummy) NULL parameter to avoid an image size increase due to unused
__func__ entries (this is necessary because __func__ is not a define but a
virtual variable).

Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
arch/arm64/include/asm/asm-bug.h | 29 +++++++++++++++++++----------
arch/arm64/include/asm/bug.h | 8 +++++++-
2 files changed, 26 insertions(+), 11 deletions(-)

diff --git a/arch/arm64/include/asm/asm-bug.h b/arch/arm64/include/asm/asm-bug.h
index c762038ba400..6884089a7191 100644
--- a/arch/arm64/include/asm/asm-bug.h
+++ b/arch/arm64/include/asm/asm-bug.h
@@ -8,36 +8,45 @@
#include <asm/brk-imm.h>

#ifdef CONFIG_DEBUG_BUGVERBOSE
-#define _BUGVERBOSE_LOCATION(file, line) __BUGVERBOSE_LOCATION(file, line)
-#define __BUGVERBOSE_LOCATION(file, line) \
+
+#if IS_ENABLED(CONFIG_KUNIT)
+# define HAVE_BUG_FUNCTION
+# define __BUG_FUNC_PTR(func) .long func - .;
+#else
+# define __BUG_FUNC_PTR(func)
+#endif
+
+#define _BUGVERBOSE_LOCATION(file, func, line) __BUGVERBOSE_LOCATION(file, func, line)
+#define __BUGVERBOSE_LOCATION(file, func, line) \
.pushsection .rodata.str,"aMS",@progbits,1; \
14472: .string file; \
.popsection; \
\
.long 14472b - .; \
+ __BUG_FUNC_PTR(func) \
.short line;
#else
-#define _BUGVERBOSE_LOCATION(file, line)
+#define _BUGVERBOSE_LOCATION(file, func, line)
#endif

#ifdef CONFIG_GENERIC_BUG

-#define __BUG_ENTRY(flags) \
+#define __BUG_ENTRY(flags, func) \
.pushsection __bug_table,"aw"; \
.align 2; \
14470: .long 14471f - .; \
-_BUGVERBOSE_LOCATION(__FILE__, __LINE__) \
- .short flags; \
+_BUGVERBOSE_LOCATION(__FILE__, func, __LINE__) \
+ .short flags; \
.popsection; \
14471:
#else
-#define __BUG_ENTRY(flags)
+#define __BUG_ENTRY(flags, func)
#endif

-#define ASM_BUG_FLAGS(flags) \
- __BUG_ENTRY(flags) \
+#define ASM_BUG_FLAGS(flags, func) \
+ __BUG_ENTRY(flags, func) \
brk BUG_BRK_IMM

-#define ASM_BUG() ASM_BUG_FLAGS(0)
+#define ASM_BUG() ASM_BUG_FLAGS(0, .)

#endif /* __ASM_ASM_BUG_H */
diff --git a/arch/arm64/include/asm/bug.h b/arch/arm64/include/asm/bug.h
index 28be048db3f6..044c5e24a17d 100644
--- a/arch/arm64/include/asm/bug.h
+++ b/arch/arm64/include/asm/bug.h
@@ -11,8 +11,14 @@

#include <asm/asm-bug.h>

+#ifdef HAVE_BUG_FUNCTION
+# define __BUG_FUNC __func__
+#else

Guenter Roeck

unread,
Mar 12, 2024, 1:03:35 PMMar 12
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev, Guenter Roeck
Add name of functions triggering warning backtraces to the __bug_table
object section to enable support for suppressing WARNING backtraces.

To limit image size impact, the pointer to the function name is only added
to the __bug_table section if both CONFIG_KUNIT and CONFIG_DEBUG_BUGVERBOSE
are enabled. Otherwise, the __func__ assembly parameter is replaced with a
(dummy) NULL parameter to avoid an image size increase due to unused
__func__ entries (this is necessary because __func__ is not a define but a
virtual variable).

Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
arch/loongarch/include/asm/bug.h | 38 +++++++++++++++++++++++---------
1 file changed, 27 insertions(+), 11 deletions(-)

diff --git a/arch/loongarch/include/asm/bug.h b/arch/loongarch/include/asm/bug.h
index d4ca3ba25418..25f2b5ae7702 100644
--- a/arch/loongarch/include/asm/bug.h
+++ b/arch/loongarch/include/asm/bug.h
@@ -3,47 +3,63 @@
#define __ASM_BUG_H

#include <asm/break.h>
+#include <kunit/bug.h>
#include <linux/stringify.h>

#ifndef CONFIG_DEBUG_BUGVERBOSE
-#define _BUGVERBOSE_LOCATION(file, line)
+#define _BUGVERBOSE_LOCATION(file, func, line)
#else
-#define __BUGVERBOSE_LOCATION(file, line) \
+#if IS_ENABLED(CONFIG_KUNIT)
+# define HAVE_BUG_FUNCTION
+# define __BUG_FUNC_PTR(func) .long func - .;
+#else
+# define __BUG_FUNC_PTR(func)
+#endif
+
+#define __BUGVERBOSE_LOCATION(file, func, line) \
.pushsection .rodata.str, "aMS", @progbits, 1; \
10002: .string file; \
.popsection; \
\
.long 10002b - .; \
+ __BUG_FUNC_PTR(func) \
.short line;
-#define _BUGVERBOSE_LOCATION(file, line) __BUGVERBOSE_LOCATION(file, line)
+#define _BUGVERBOSE_LOCATION(file, func, line) __BUGVERBOSE_LOCATION(file, func, line)
#endif

#ifndef CONFIG_GENERIC_BUG
-#define __BUG_ENTRY(flags)
+#define __BUG_ENTRY(flags, func)
#else
-#define __BUG_ENTRY(flags) \
+#define __BUG_ENTRY(flags, func) \
.pushsection __bug_table, "aw"; \
.align 2; \
10000: .long 10001f - .; \
- _BUGVERBOSE_LOCATION(__FILE__, __LINE__) \
+ _BUGVERBOSE_LOCATION(__FILE__, func, __LINE__) \
.short flags; \
.popsection; \
10001:
#endif

-#define ASM_BUG_FLAGS(flags) \
- __BUG_ENTRY(flags) \
+#define ASM_BUG_FLAGS(flags, func) \
+ __BUG_ENTRY(flags, func) \
break BRK_BUG

-#define ASM_BUG() ASM_BUG_FLAGS(0)
+#define ASM_BUG() ASM_BUG_FLAGS(0, .)
+
+#ifdef HAVE_BUG_FUNCTION
+# define __BUG_FUNC __func__
+#else
+# define __BUG_FUNC NULL
+#endif

#define __BUG_FLAGS(flags) \
- asm_inline volatile (__stringify(ASM_BUG_FLAGS(flags)));
+ asm_inline volatile (__stringify(ASM_BUG_FLAGS(flags, %0)) : : "i" (__BUG_FUNC));

#define __WARN_FLAGS(flags) \
do { \
instrumentation_begin(); \
- __BUG_FLAGS(BUGFLAG_WARNING|(flags)); \
+ if (!IS_SUPPRESSED_WARNING(__func__)) \
+ __BUG_FLAGS(BUGFLAG_WARNING|(flags)); \

Guenter Roeck

unread,
Mar 12, 2024, 1:03:39 PMMar 12
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev, Guenter Roeck
Add name of functions triggering warning backtraces to the __bug_table
object section to enable support for suppressing WARNING backtraces.

To limit image size impact, the pointer to the function name is only added
to the __bug_table section if both CONFIG_KUNIT and CONFIG_DEBUG_BUGVERBOSE
are enabled. Otherwise, the __func__ assembly parameter is replaced with a
(dummy) NULL parameter to avoid an image size increase due to unused
__func__ entries (this is necessary because __func__ is not a define but a
virtual variable).

While at it, declare assembler parameters as constants where possible.
Refine .blockz instructions to calculate the necessary padding instead
of using fixed values.

Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
arch/parisc/include/asm/bug.h | 29 +++++++++++++++++++++--------
1 file changed, 21 insertions(+), 8 deletions(-)

diff --git a/arch/parisc/include/asm/bug.h b/arch/parisc/include/asm/bug.h
index 833555f74ffa..792dacc2a653 100644
--- a/arch/parisc/include/asm/bug.h
+++ b/arch/parisc/include/asm/bug.h
@@ -23,8 +23,17 @@
# define __BUG_REL(val) ".word " __stringify(val)
#endif

-
#ifdef CONFIG_DEBUG_BUGVERBOSE
+
+#if IS_ENABLED(CONFIG_KUNIT)
+# define HAVE_BUG_FUNCTION
+# define __BUG_FUNC_PTR __BUG_REL(%c1)
+# define __BUG_FUNC __func__
+#else
+# define __BUG_FUNC_PTR
+# define __BUG_FUNC NULL
+#endif /* IS_ENABLED(CONFIG_KUNIT) */
+
#define BUG() \
do { \
asm volatile("\n" \
@@ -33,10 +42,12 @@
"\t.align 4\n" \
"2:\t" __BUG_REL(1b) "\n" \
"\t" __BUG_REL(%c0) "\n" \
- "\t.short %1, %2\n" \
- "\t.blockz %3-2*4-2*2\n" \
+ "\t" __BUG_FUNC_PTR "\n" \
+ "\t.short %c2, %c3\n" \
+ "\t.blockz %c4-(.-2b)\n" \
"\t.popsection" \
- : : "i" (__FILE__), "i" (__LINE__), \
+ : : "i" (__FILE__), "i" (__BUG_FUNC), \
+ "i" (__LINE__), \
"i" (0), "i" (sizeof(struct bug_entry)) ); \
unreachable(); \
} while(0)
@@ -58,10 +69,12 @@
"\t.align 4\n" \
"2:\t" __BUG_REL(1b) "\n" \
"\t" __BUG_REL(%c0) "\n" \
- "\t.short %1, %2\n" \
- "\t.blockz %3-2*4-2*2\n" \
+ "\t" __BUG_FUNC_PTR "\n" \
+ "\t.short %c2, %3\n" \
+ "\t.blockz %c4-(.-2b)\n" \
"\t.popsection" \
- : : "i" (__FILE__), "i" (__LINE__), \
+ : : "i" (__FILE__), "i" (__BUG_FUNC), \
+ "i" (__LINE__), \
"i" (BUGFLAG_WARNING|(flags)), \
"i" (sizeof(struct bug_entry)) ); \
} while(0)
@@ -74,7 +87,7 @@
"\t.align 4\n" \
"2:\t" __BUG_REL(1b) "\n" \
"\t.short %0\n" \
- "\t.blockz %1-4-2\n" \
+ "\t.blockz %c1-(.-2b)\n" \
"\t.popsection" \
: : "i" (BUGFLAG_WARNING|(flags)), \
"i" (sizeof(struct bug_entry)) ); \
--
2.39.2

Guenter Roeck

unread,
Mar 12, 2024, 1:03:40 PMMar 12
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev, Guenter Roeck
Add name of functions triggering warning backtraces to the __bug_table
object section to enable support for suppressing WARNING backtraces.

To limit image size impact, the pointer to the function name is only added
to the __bug_table section if both CONFIG_KUNIT and CONFIG_DEBUG_BUGVERBOSE
are enabled. Otherwise, the __func__ assembly parameter is replaced with a
(dummy) NULL parameter to avoid an image size increase due to unused
__func__ entries (this is necessary because __func__ is not a define but a
virtual variable).

Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
arch/s390/include/asm/bug.h | 19 +++++++++++++++----
1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/arch/s390/include/asm/bug.h b/arch/s390/include/asm/bug.h
index aebe1e22c7be..01e2aa4069d7 100644
--- a/arch/s390/include/asm/bug.h
+++ b/arch/s390/include/asm/bug.h
@@ -8,19 +8,30 @@

#ifdef CONFIG_DEBUG_BUGVERBOSE

+#if IS_ENABLED(CONFIG_KUNIT)
+# define HAVE_BUG_FUNCTION
+# define __BUG_FUNC_PTR " .long %0-.\n"
+# define __BUG_FUNC __func__
+#else
+# define __BUG_FUNC_PTR
+# define __BUG_FUNC NULL
+#endif /* IS_ENABLED(CONFIG_KUNIT) */
+
#define __EMIT_BUG(x) do { \
asm_inline volatile( \
"0: mc 0,0\n" \
".section .rodata.str,\"aMS\",@progbits,1\n" \
"1: .asciz \""__FILE__"\"\n" \
".previous\n" \
- ".section __bug_table,\"awM\",@progbits,%2\n" \
+ ".section __bug_table,\"awM\",@progbits,%3\n" \
"2: .long 0b-.\n" \
" .long 1b-.\n" \
- " .short %0,%1\n" \
- " .org 2b+%2\n" \
+ __BUG_FUNC_PTR \
+ " .short %1,%2\n" \
+ " .org 2b+%3\n" \
".previous\n" \
- : : "i" (__LINE__), \
+ : : "i" (__BUG_FUNC), \
+ "i" (__LINE__), \
"i" (x), \
"i" (sizeof(struct bug_entry))); \
} while (0)
--
2.39.2

Guenter Roeck

unread,
Mar 12, 2024, 1:03:42 PMMar 12
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev, Guenter Roeck
Add name of functions triggering warning backtraces to the __bug_table
object section to enable support for suppressing WARNING backtraces.

To limit image size impact, the pointer to the function name is only added
to the __bug_table section if both CONFIG_KUNIT and CONFIG_DEBUG_BUGVERBOSE
are enabled. Otherwise, the __func__ assembly parameter is replaced with a
(dummy) NULL parameter to avoid an image size increase due to unused
__func__ entries (this is necessary because __func__ is not a define but a
virtual variable).

Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
arch/sh/include/asm/bug.h | 26 ++++++++++++++++++++++----
1 file changed, 22 insertions(+), 4 deletions(-)

diff --git a/arch/sh/include/asm/bug.h b/arch/sh/include/asm/bug.h
index 05a485c4fabc..cadc335eb759 100644
--- a/arch/sh/include/asm/bug.h
+++ b/arch/sh/include/asm/bug.h
@@ -24,21 +24,36 @@
* The offending file and line are encoded in the __bug_table section.
*/
#ifdef CONFIG_DEBUG_BUGVERBOSE
+
+#if IS_ENABLED(CONFIG_KUNIT)
+# define HAVE_BUG_FUNCTION
+# define __BUG_FUNC_PTR "\t.long %O2\n"
+#else
+# define __BUG_FUNC_PTR
+#endif /* IS_ENABLED(CONFIG_KUNIT) */
+
#define _EMIT_BUG_ENTRY \
"\t.pushsection __bug_table,\"aw\"\n" \
"2:\t.long 1b, %O1\n" \
- "\t.short %O2, %O3\n" \
- "\t.org 2b+%O4\n" \
+ __BUG_FUNC_PTR \
+ "\t.short %O3, %O4\n" \
+ "\t.org 2b+%O5\n" \
"\t.popsection\n"
#else
#define _EMIT_BUG_ENTRY \
"\t.pushsection __bug_table,\"aw\"\n" \
"2:\t.long 1b\n" \
- "\t.short %O3\n" \
- "\t.org 2b+%O4\n" \
+ "\t.short %O4\n" \
+ "\t.org 2b+%O5\n" \
"\t.popsection\n"
#endif

+#ifdef HAVE_BUG_FUNCTION
+# define __BUG_FUNC __func__
+#else
+# define __BUG_FUNC NULL
+#endif
+
#define BUG() \
do { \
__asm__ __volatile__ ( \
@@ -47,6 +62,7 @@ do { \
: \
: "n" (TRAPA_BUG_OPCODE), \
"i" (__FILE__), \
+ "i" (__BUG_FUNC), \
"i" (__LINE__), "i" (0), \
"i" (sizeof(struct bug_entry))); \
unreachable(); \
@@ -60,6 +76,7 @@ do { \
: \
: "n" (TRAPA_BUG_OPCODE), \
"i" (__FILE__), \
+ "i" (__BUG_FUNC), \
"i" (__LINE__), \
"i" (BUGFLAG_WARNING|(flags)), \
"i" (sizeof(struct bug_entry))); \
@@ -85,6 +102,7 @@ do { \
: \
: "n" (TRAPA_BUG_OPCODE), \
"i" (__FILE__), \
+ "i" (__BUG_FUNC), \
"i" (__LINE__), \
"i" (BUGFLAG_UNWINDER), \
"i" (sizeof(struct bug_entry))); \
--
2.39.2

Guenter Roeck

unread,
Mar 12, 2024, 1:03:45 PMMar 12
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev, Guenter Roeck
Add name of functions triggering warning backtraces to the __bug_table
object section to enable support for suppressing WARNING backtraces.

To limit image size impact, the pointer to the function name is only added
to the __bug_table section if both CONFIG_KUNIT and CONFIG_DEBUG_BUGVERBOSE
are enabled. Otherwise, the __func__ assembly parameter is replaced with a
(dummy) NULL parameter to avoid an image size increase due to unused
__func__ entries (this is necessary because __func__ is not a define but a
virtual variable).

To simplify the implementation, unify the __BUG_ENTRY_ADDR and
__BUG_ENTRY_FILE macros into a single macro named __BUG_REL() which takes
the address, file, or function reference as parameter.

Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
arch/riscv/include/asm/bug.h | 38 ++++++++++++++++++++++++------------
1 file changed, 26 insertions(+), 12 deletions(-)

diff --git a/arch/riscv/include/asm/bug.h b/arch/riscv/include/asm/bug.h
index 1aaea81fb141..8955ee5c1c27 100644
--- a/arch/riscv/include/asm/bug.h
+++ b/arch/riscv/include/asm/bug.h
@@ -30,26 +30,39 @@
typedef u32 bug_insn_t;

#ifdef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
-#define __BUG_ENTRY_ADDR RISCV_INT " 1b - ."
-#define __BUG_ENTRY_FILE RISCV_INT " %0 - ."
+#define __BUG_REL(val) RISCV_INT " " __stringify(val) " - ."
#else
-#define __BUG_ENTRY_ADDR RISCV_PTR " 1b"
-#define __BUG_ENTRY_FILE RISCV_PTR " %0"
+#define __BUG_REL(val) RISCV_PTR " " __stringify(val)
#endif

#ifdef CONFIG_DEBUG_BUGVERBOSE
+
+#if IS_ENABLED(CONFIG_KUNIT)
+# define HAVE_BUG_FUNCTION
+# define __BUG_FUNC_PTR __BUG_REL(%1)
+#else
+# define __BUG_FUNC_PTR
+#endif /* IS_ENABLED(CONFIG_KUNIT) */
+
#define __BUG_ENTRY \
- __BUG_ENTRY_ADDR "\n\t" \
- __BUG_ENTRY_FILE "\n\t" \
- RISCV_SHORT " %1\n\t" \
- RISCV_SHORT " %2"
+ __BUG_REL(1b) "\n\t" \
+ __BUG_REL(%0) "\n\t" \
+ __BUG_FUNC_PTR "\n\t" \
+ RISCV_SHORT " %2\n\t" \
+ RISCV_SHORT " %3"
#else
#define __BUG_ENTRY \
- __BUG_ENTRY_ADDR "\n\t" \
- RISCV_SHORT " %2"
+ __BUG_REL(1b) "\n\t" \
+ RISCV_SHORT " %3"
#endif

#ifdef CONFIG_GENERIC_BUG
+#ifdef HAVE_BUG_FUNCTION
+# define __BUG_FUNC __func__
+#else
+# define __BUG_FUNC NULL
+#endif
+
#define __BUG_FLAGS(flags) \
do { \
__asm__ __volatile__ ( \
@@ -58,10 +71,11 @@ do { \
".pushsection __bug_table,\"aw\"\n\t" \
"2:\n\t" \
__BUG_ENTRY "\n\t" \
- ".org 2b + %3\n\t" \
+ ".org 2b + %4\n\t" \
".popsection" \
: \
- : "i" (__FILE__), "i" (__LINE__), \
+ : "i" (__FILE__), "i" (__BUG_FUNC), \
+ "i" (__LINE__), \
"i" (flags), \

Guenter Roeck

unread,
Mar 12, 2024, 1:03:47 PMMar 12
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev, Guenter Roeck
Add name of functions triggering warning backtraces to the __bug_table
object section to enable support for suppressing WARNING backtraces.

To limit image size impact, the pointer to the function name is only added
to the __bug_table section if both CONFIG_KUNIT and CONFIG_DEBUG_BUGVERBOSE
are enabled. Otherwise, the __func__ assembly parameter is replaced with a
(dummy) NULL parameter to avoid an image size increase due to unused
__func__ entries (this is necessary because __func__ is not a define but a
virtual variable).

Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
arch/powerpc/include/asm/bug.h | 37 +++++++++++++++++++++++++---------
1 file changed, 28 insertions(+), 9 deletions(-)

diff --git a/arch/powerpc/include/asm/bug.h b/arch/powerpc/include/asm/bug.h
index 1db485aacbd9..330d4983f90e 100644
--- a/arch/powerpc/include/asm/bug.h
+++ b/arch/powerpc/include/asm/bug.h
@@ -14,6 +14,9 @@
.section __bug_table,"aw"
5001: .4byte \addr - .
.4byte 5002f - .
+#if IS_ENABLED(CONFIG_KUNIT)
+ .4byte 0
+#endif
.short \line, \flags
.org 5001b+BUG_ENTRY_SIZE
.previous
@@ -32,30 +35,46 @@
#endif /* verbose */

#else /* !__ASSEMBLY__ */
-/* _EMIT_BUG_ENTRY expects args %0,%1,%2,%3 to be FILE, LINE, flags and
- sizeof(struct bug_entry), respectively */
+/* _EMIT_BUG_ENTRY expects args %0,%1,%2,%3,%4 to be FILE, __func__, LINE, flags
+ and sizeof(struct bug_entry), respectively */
#ifdef CONFIG_DEBUG_BUGVERBOSE
+
+#if IS_ENABLED(CONFIG_KUNIT)
+# define HAVE_BUG_FUNCTION
+# define __BUG_FUNC_PTR " .4byte %1 - .\n"
+#else
+# define __BUG_FUNC_PTR
+#endif /* IS_ENABLED(CONFIG_KUNIT) */
+
#define _EMIT_BUG_ENTRY \
".section __bug_table,\"aw\"\n" \
"2: .4byte 1b - .\n" \
" .4byte %0 - .\n" \
- " .short %1, %2\n" \
- ".org 2b+%3\n" \
+ __BUG_FUNC_PTR \
+ " .short %2, %3\n" \
+ ".org 2b+%4\n" \
".previous\n"
#else
#define _EMIT_BUG_ENTRY \
".section __bug_table,\"aw\"\n" \
"2: .4byte 1b - .\n" \
- " .short %2\n" \
- ".org 2b+%3\n" \
+ " .short %3\n" \
+ ".org 2b+%4\n" \
".previous\n"
#endif

+#ifdef HAVE_BUG_FUNCTION
+# define __BUG_FUNC __func__
+#else
+# define __BUG_FUNC NULL
+#endif
+
#define BUG_ENTRY(insn, flags, ...) \
__asm__ __volatile__( \
"1: " insn "\n" \
_EMIT_BUG_ENTRY \
- : : "i" (__FILE__), "i" (__LINE__), \
+ : : "i" (__FILE__), "i" (__BUG_FUNC), \
+ "i" (__LINE__), \
"i" (flags), \
"i" (sizeof(struct bug_entry)), \
##__VA_ARGS__)
@@ -80,7 +99,7 @@
if (x) \
BUG(); \
} else { \
- BUG_ENTRY(PPC_TLNEI " %4, 0", 0, "r" ((__force long)(x))); \
+ BUG_ENTRY(PPC_TLNEI " %5, 0", 0, "r" ((__force long)(x))); \
} \
} while (0)

@@ -90,7 +109,7 @@
if (__ret_warn_on) \
__WARN(); \
} else { \
- BUG_ENTRY(PPC_TLNEI " %4, 0", \
+ BUG_ENTRY(PPC_TLNEI " %5, 0", \
BUGFLAG_WARNING | BUGFLAG_TAINT(TAINT_WARN), \
"r" (__ret_warn_on)); \
} \
--
2.39.2

Kees Cook

unread,
Mar 12, 2024, 6:01:01 PMMar 12
to Guenter Roeck, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev
On Tue, Mar 12, 2024 at 10:02:56AM -0700, Guenter Roeck wrote:
> 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.
>
> Cc: Dan Carpenter <dan.ca...@linaro.org>
> Cc: Daniel Diaz <danie...@linaro.org>
> Cc: Naresh Kamboju <naresh....@linaro.org>
> Cc: Kees Cook <kees...@chromium.org>
> Signed-off-by: Guenter Roeck <li...@roeck-us.net>

Yup, this looks fine to me.

Reviewed-by: Kees Cook <kees...@chromium.org>

--
Kees Cook

Kees Cook

unread,
Mar 12, 2024, 6:02:08 PMMar 12
to Guenter Roeck, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev
On Tue, Mar 12, 2024 at 10:02:57AM -0700, Guenter Roeck wrote:
> Count suppressed warning backtraces to enable code which suppresses
> warning backtraces to check if the expected backtrace(s) have been
> observed.
>
> Using atomics for the backtrace count resulted in build errors on some
> architectures due to include file recursion, so use a plain integer
> for now.
>
> Signed-off-by: Guenter Roeck <li...@roeck-us.net>
> ---
> include/kunit/bug.h | 7 ++++++-
> lib/kunit/bug.c | 4 +++-
> 2 files changed, 9 insertions(+), 2 deletions(-)
>
> diff --git a/include/kunit/bug.h b/include/kunit/bug.h
> index 1e34da961599..2097a854ac8c 100644
> --- a/include/kunit/bug.h
> +++ b/include/kunit/bug.h
> @@ -20,6 +20,7 @@
> struct __suppressed_warning {
> struct list_head node;
> const char *function;
> + int counter;

Thanks for adding a counter!

Kees Cook

unread,
Mar 12, 2024, 6:02:48 PMMar 12
to Guenter Roeck, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev
On Tue, Mar 12, 2024 at 10:02:58AM -0700, Guenter Roeck wrote:
> Add unit tests to verify that warning backtrace suppression works.
>
> 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.
>
> Signed-off-by: Guenter Roeck <li...@roeck-us.net>

Kees Cook

unread,
Mar 12, 2024, 6:03:07 PMMar 12
to Guenter Roeck, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev
On Tue, Mar 12, 2024 at 10:02:59AM -0700, Guenter Roeck wrote:
> Document API functions for suppressing warning backtraces.
>
> Signed-off-by: Guenter Roeck <li...@roeck-us.net>

Dan Carpenter

unread,
Mar 13, 2024, 3:39:34 AMMar 13
to Guenter Roeck, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev
Thanks!

Acked-by: Dan Carpenter <dan.ca...@linaro.org>

regards,
dan carpenter

Naresh Kamboju

unread,
Mar 14, 2024, 3:19:43 AMMar 14
to Guenter Roeck, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev, Anders Roxell
On Tue, 12 Mar 2024 at 22:33, Guenter Roeck <li...@roeck-us.net> wrote:

<trim>

> This series is based on the RFC patch and subsequent discussion at
> https://patchwork.kernel.org/project/linux-kselftest/patch/02546e59-1afe-4b08...@moroto.mountain/
> and offers a more comprehensive solution of the problem discussed there.

Thanks for the patchset.
This patch series applied on top of Linux next and tested.

Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>


--
Linaro LKFT
https://lkft.linaro.org

Geert Uytterhoeven

unread,
Mar 14, 2024, 3:58:02 AMMar 14
to Guenter Roeck, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev
Hi Günter,

On Tue, Mar 12, 2024 at 6:06 PM Guenter Roeck <li...@roeck-us.net> wrote:
> Add name of functions triggering warning backtraces to the __bug_table
> object section to enable support for suppressing WARNING backtraces.
>
> To limit image size impact, the pointer to the function name is only added
> to the __bug_table section if both CONFIG_KUNIT and CONFIG_DEBUG_BUGVERBOSE
> are enabled. Otherwise, the __func__ assembly parameter is replaced with a
> (dummy) NULL parameter to avoid an image size increase due to unused
> __func__ entries (this is necessary because __func__ is not a define but a
> virtual variable).
>
> Signed-off-by: Guenter Roeck <li...@roeck-us.net>

Thanks for your patch!

> --- a/arch/s390/include/asm/bug.h
> +++ b/arch/s390/include/asm/bug.h
> @@ -8,19 +8,30 @@
>
> #ifdef CONFIG_DEBUG_BUGVERBOSE
>
> +#if IS_ENABLED(CONFIG_KUNIT)
> +# define HAVE_BUG_FUNCTION
> +# define __BUG_FUNC_PTR " .long %0-.\n"
> +# define __BUG_FUNC __func__
> +#else
> +# define __BUG_FUNC_PTR
> +# define __BUG_FUNC NULL
> +#endif /* IS_ENABLED(CONFIG_KUNIT) */
> +
> #define __EMIT_BUG(x) do { \
> asm_inline volatile( \
> "0: mc 0,0\n" \
> ".section .rodata.str,\"aMS\",@progbits,1\n" \
> "1: .asciz \""__FILE__"\"\n" \
> ".previous\n" \
> - ".section __bug_table,\"awM\",@progbits,%2\n" \
> + ".section __bug_table,\"awM\",@progbits,%3\n" \

This change conflicts with commit 3938490e78f443fb ("s390/bug:
remove entry size from __bug_table section") in linus/master.
I guess it should just be dropped?

> "2: .long 0b-.\n" \
> " .long 1b-.\n" \
> - " .short %0,%1\n" \
> - " .org 2b+%2\n" \
> + __BUG_FUNC_PTR \
> + " .short %1,%2\n" \
> + " .org 2b+%3\n" \
> ".previous\n" \
> - : : "i" (__LINE__), \
> + : : "i" (__BUG_FUNC), \
> + "i" (__LINE__), \
> "i" (x), \
> "i" (sizeof(struct bug_entry))); \
> } while (0)

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- ge...@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

Geert Uytterhoeven

unread,
Mar 14, 2024, 9:37:10 AMMar 14
to Guenter Roeck, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev
Hi Günter,
Thanks for your series!

I gave it a try on m68k, just running backtrace-suppression-test,
and that seems to work fine.

> Design note:
> Function pointers are only added to the __bug_table section if both
> CONFIG_KUNIT and CONFIG_DEBUG_BUGVERBOSE are enabled to avoid image
> size increases if CONFIG_KUNIT=n. There would be some benefits to
> adding those pointers all the time (reduced complexity, ability to
> display function names in BUG/WARNING messages). That change, if
> desired, can be made later.

Unfortunately this also increases kernel size in the CONFIG_KUNIT=m
case (ca. 80 KiB for atari_defconfig), making it less attractive to have
kunit and all tests enabled as modules in my standard kernel.

Guenter Roeck

unread,
Mar 14, 2024, 9:54:38 AMMar 14
to Geert Uytterhoeven, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev
Yes, I know. I'll send v2 rebased to v6.9-rc1 once it is available and,
yes, the change will be gone after that.

Thanks,
Guenter


Guenter Roeck

unread,
Mar 14, 2024, 10:37:19 AMMar 14
to Geert Uytterhoeven, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev
Good point. Indeed, it does. I wanted to avoid adding a configuration option,
but maybe I should add it after all. How about something like this ?

+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 can be suppressed. Disable
+ to reduce kernel image size if image size is more important than
+ suppression of backtraces generated by KUnit tests.
+

Thanks,
Guenter

Maxime Ripard

unread,
Mar 14, 2024, 11:02:06 AMMar 14
to Guenter Roeck, Geert Uytterhoeven, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev
How are tests using that API supposed to handle it then?

Select the config option or put an ifdef?

If the former, we end up in the same situation than without the symbol.
If the latter, we end up in a similar situation than disabling KUNIT
entirely, with some tests not being run which is just terrible.

Maxime
signature.asc

Guenter Roeck

unread,
Mar 14, 2024, 11:29:27 AMMar 14
to Maxime Ripard, Geert Uytterhoeven, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev
The API definitions are themselves within #ifdef and dummies if
KUNIT_SUPPRESS_BACKTRACE (currently CONFIG_KUNIT) is disabled.
In include/kunit/bug.h:

#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE
...
#else
#define DEFINE_SUPPRESSED_WARNING(func)
#define START_SUPPRESSED_WARNING(func)
#define END_SUPPRESSED_WARNING(func)
#define IS_SUPPRESSED_WARNING(func) (false)
#define SUPPRESSED_WARNING_COUNT(func) (0)
#endif

Only difference to the current patch series would be

- #if IS_ENABLED(CONFIG_KUNIT)
+ #ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE

in that file and elsewhere.

With KUNIT_SUPPRESS_BACKTRACE=n you'd still get warning backtraces
triggered by the tests, but the number of tests executed as well
as the test results would still be the same.

Thanks,
Guenter

Helge Deller

unread,
Mar 15, 2024, 7:45:48 AMMar 15
to Guenter Roeck, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev
On 3/12/24 18:03, Guenter Roeck wrote:
> Add name of functions triggering warning backtraces to the __bug_table
> object section to enable support for suppressing WARNING backtraces.
>
> To limit image size impact, the pointer to the function name is only added
> to the __bug_table section if both CONFIG_KUNIT and CONFIG_DEBUG_BUGVERBOSE
> are enabled. Otherwise, the __func__ assembly parameter is replaced with a
> (dummy) NULL parameter to avoid an image size increase due to unused
> __func__ entries (this is necessary because __func__ is not a define but a
> virtual variable).
>
> While at it, declare assembler parameters as constants where possible.
> Refine .blockz instructions to calculate the necessary padding instead
> of using fixed values.
>
> Signed-off-by: Guenter Roeck <li...@roeck-us.net>


Acked-by: Helge Deller <del...@gmx.de>

Helge

Guenter Roeck

unread,
Mar 16, 2024, 12:17:02 PMMar 16
to Geert Uytterhoeven, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev
Any more comments / feedback on this ? Otherwise I'll introduce the
above configuration option in v2 of the series.

In this context, any suggestions if it should be enabled or disabled by
default ? I personally think it would be more important to be able to
suppress backtraces, but I understand that others may not be willing to
accept a ~1% image size increase with CONFIG_KUNIT=m unless
KUNIT_SUPPRESS_BACKTRACE is explicitly disabled.

Thanks,
Guenter

Michael Ellerman

unread,
Mar 17, 2024, 11:25:10 PMMar 17
to Guenter Roeck, Geert Uytterhoeven, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@lists.linux.dev
> Any more comments / feedback on this ? Otherwise I'll introduce the
> above configuration option in v2 of the series.
>
> In this context, any suggestions if it should be enabled or disabled by
> default ? I personally think it would be more important to be able to
> suppress backtraces, but I understand that others may not be willing to
> accept a ~1% image size increase with CONFIG_KUNIT=m unless
> KUNIT_SUPPRESS_BACKTRACE is explicitly disabled.

Please enable it by default.

There are multiple CI systems that will benefit from it, whereas the
number of users enabling KUNIT in severely spaced constrainted
environments is surely small - perhaps just Geert ;).

cheers

Guenter Roeck

unread,
Mar 25, 2024, 1:52:57 PMMar 25
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, Guenter Roeck
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 series is based on the RFC patch and subsequent discussion at
https://patchwork.kernel.org/project/linux-kselftest/patch/02546e59-1afe-4b08...@moroto.mountain/
and offers a more comprehensive solution of the problem discussed there.

Design note:
Function pointers are only added to the __bug_table section if both
CONFIG_KUNIT_SUPPRESS_BACKTRACE and CONFIG_DEBUG_BUGVERBOSE are enabled
to avoid image size increases if CONFIG_KUNIT is disabled. There would be
some benefits to adding those pointers all the time (reduced complexity,
ability to display function names in BUG/WARNING messages). That change,
if desired, can be made later.

Checkpatch note:
Remaining checkpatch errors and warnings were deliberately ignored.
Some are triggered by matching coding style or by comments interpreted
as code, others by assembler macros which are disliked by checkpatch.
Suggestions for improvements are welcome.

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.

Guenter Roeck

unread,
Mar 25, 2024, 1:52:59 PMMar 25
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, Guenter Roeck, Linux Kernel Functional Testing
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.

Cc: Dan Carpenter <dan.ca...@linaro.org>
Cc: Daniel Diaz <danie...@linaro.org>
Cc: Naresh Kamboju <naresh....@linaro.org>
Cc: Kees Cook <kees...@chromium.org>
Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
Acked-by: Dan Carpenter <dan.ca...@linaro.org>
Reviewed-by: Kees Cook <kees...@chromium.org>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
v2:
- Rebased to v6.9-rc1
- Added Tested-by:, Acked-by:, and Reviewed-by: tags
- Added CONFIG_KUNIT_SUPPRESS_BACKTRACE configuration option,
enabled by default

include/asm-generic/bug.h | 16 +++++++++---
include/kunit/bug.h | 51 +++++++++++++++++++++++++++++++++++++++
include/kunit/test.h | 1 +
include/linux/bug.h | 13 ++++++++++
lib/bug.c | 51 ++++++++++++++++++++++++++++++++++++---
lib/kunit/Kconfig | 9 +++++++
lib/kunit/Makefile | 6 +++--
lib/kunit/bug.c | 40 ++++++++++++++++++++++++++++++
8 files changed, 178 insertions(+), 9 deletions(-)
create mode 100644 include/kunit/bug.h
create mode 100644 lib/kunit/bug.c

diff --git a/include/asm-generic/bug.h b/include/asm-generic/bug.h
index 6e794420bd39..c170b6477689 100644
--- a/include/asm-generic/bug.h
+++ b/include/asm-generic/bug.h
@@ -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 (!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 (!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
index 000000000000..bd0fe047572b
--- /dev/null
+++ b/include/kunit/bug.h
@@ -0,0 +1,51 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * KUnit helpers for backtrace suppression
+ *
+ * Copyright (c) 2024 Guenter Roeck <li...@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 __start_suppress_warning(struct __suppressed_warning *warning);
+void __end_suppress_warning(struct __suppressed_warning *warning);
+bool __is_suppressed_warning(const char *function);
+
+#define DEFINE_SUPPRESSED_WARNING(func) \
+ struct __suppressed_warning __kunit_suppress_##func = \
+ { .function = __stringify(func) }
+
+#define START_SUPPRESSED_WARNING(func) \
+ __start_suppress_warning(&__kunit_suppress_##func)
+
+#define END_SUPPRESSED_WARNING(func) \
+ __end_suppress_warning(&__kunit_suppress_##func)
+
+#define IS_SUPPRESSED_WARNING(func) \
+ __is_suppressed_warning(func)
+
+#else /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */
+
+#define DEFINE_SUPPRESSED_WARNING(func)
+#define START_SUPPRESSED_WARNING(func)
+#define END_SUPPRESSED_WARNING(func)
+#define IS_SUPPRESSED_WARNING(func) (false)
+
+#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */
+#endif /* __ASSEMBLY__ */
+#endif /* _KUNIT_BUG_H */
diff --git a/include/kunit/test.h b/include/kunit/test.h
index 61637ef32302..d0c44594d34c 100644
--- a/include/kunit/test.h
+++ b/include/kunit/test.h
@@ -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>
diff --git a/include/linux/bug.h b/include/linux/bug.h
index 348acf2558f3..c668762dc76a 100644
--- a/include/linux/bug.h
+++ b/include/linux/bug.h
@@ -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)
{
diff --git a/lib/bug.c b/lib/bug.c
index e0ff21989990..aa8bb12b9809 100644
--- a/lib/bug.c
+++ b/lib/bug.c
@@ -26,6 +26,14 @@
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 && IS_SUPPRESSED_WARNING(function))
+ return BUG_TRAP_TYPE_WARN;
+
if (warning && once) {
if (done)
return BUG_TRAP_TYPE_WARN;
diff --git a/lib/kunit/Kconfig b/lib/kunit/Kconfig
index 68a6daec0aef..b1b899265acc 100644
--- a/lib/kunit/Kconfig
+++ b/lib/kunit/Kconfig
@@ -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
diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile
index 309659a32a78..545b57c3be48 100644
--- a/lib/kunit/Makefile
+++ b/lib/kunit/Makefile
@@ -14,8 +14,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

diff --git a/lib/kunit/bug.c b/lib/kunit/bug.c
new file mode 100644
index 000000000000..f93544d7a9d1
--- /dev/null
+++ b/lib/kunit/bug.c
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * KUnit helpers for backtrace suppression
+ *
+ * Copyright (c) 2024 Guenter Roeck <li...@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 __start_suppress_warning(struct __suppressed_warning *warning)
+{
+ list_add(&warning->node, &suppressed_warnings);
+}
+EXPORT_SYMBOL_GPL(__start_suppress_warning);
+
+void __end_suppress_warning(struct __suppressed_warning *warning)
+{
+ list_del(&warning->node);
+}
+EXPORT_SYMBOL_GPL(__end_suppress_warning);
+
+bool __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(__is_suppressed_warning);
--
2.39.2

Guenter Roeck

unread,
Mar 25, 2024, 1:53:00 PMMar 25
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, Guenter Roeck, Linux Kernel Functional Testing
Count suppressed warning backtraces to enable code which suppresses
warning backtraces to check if the expected backtrace(s) have been
observed.

Using atomics for the backtrace count resulted in build errors on some
architectures due to include file recursion, so use a plain integer
for now.

Acked-by: Dan Carpenter <dan.ca...@linaro.org>
Reviewed-by: Kees Cook <kees...@chromium.org>
Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
- Rebased to v6.9-rc1
- Added Tested-by:, Acked-by:, and Reviewed-by: tags
- Introduced KUNIT_SUPPRESS_BACKTRACE configuration option

include/kunit/bug.h | 7 ++++++-
lib/kunit/bug.c | 4 +++-
2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/include/kunit/bug.h b/include/kunit/bug.h
index bd0fe047572b..72e9fb23bbd5 100644
--- a/include/kunit/bug.h
+++ b/include/kunit/bug.h
@@ -20,6 +20,7 @@
struct __suppressed_warning {
struct list_head node;
const char *function;
+ int counter;
};

void __start_suppress_warning(struct __suppressed_warning *warning);
@@ -28,7 +29,7 @@ bool __is_suppressed_warning(const char *function);

#define DEFINE_SUPPRESSED_WARNING(func) \
struct __suppressed_warning __kunit_suppress_##func = \
- { .function = __stringify(func) }
+ { .function = __stringify(func), .counter = 0 }

#define START_SUPPRESSED_WARNING(func) \
__start_suppress_warning(&__kunit_suppress_##func)
@@ -39,12 +40,16 @@ bool __is_suppressed_warning(const char *function);
#define IS_SUPPRESSED_WARNING(func) \
__is_suppressed_warning(func)

+#define SUPPRESSED_WARNING_COUNT(func) \
+ (__kunit_suppress_##func.counter)
+
#else /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */

#define DEFINE_SUPPRESSED_WARNING(func)
#define START_SUPPRESSED_WARNING(func)
#define END_SUPPRESSED_WARNING(func)
#define IS_SUPPRESSED_WARNING(func) (false)
+#define SUPPRESSED_WARNING_COUNT(func) (0)

#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */
#endif /* __ASSEMBLY__ */
diff --git a/lib/kunit/bug.c b/lib/kunit/bug.c
index f93544d7a9d1..13b3d896c114 100644
--- a/lib/kunit/bug.c
+++ b/lib/kunit/bug.c
@@ -32,8 +32,10 @@ bool __is_suppressed_warning(const char *function)
return false;

list_for_each_entry(warning, &suppressed_warnings, node) {
- if (!strcmp(function, warning->function))
+ if (!strcmp(function, warning->function)) {

Guenter Roeck

unread,
Mar 25, 2024, 1:53:02 PMMar 25
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, Guenter Roeck, Linux Kernel Functional Testing
Add unit tests to verify that warning backtrace suppression works.

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.

Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
Acked-by: Dan Carpenter <dan.ca...@linaro.org>
Reviewed-by: Kees Cook <kees...@chromium.org>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
- Rebased to v6.9-rc1
- Added Tested-by:, Acked-by:, and Reviewed-by: tags
- Introduced KUNIT_SUPPRESS_BACKTRACE configuration option

lib/kunit/Makefile | 7 +-
lib/kunit/backtrace-suppression-test.c | 104 +++++++++++++++++++++++++
2 files changed, 109 insertions(+), 2 deletions(-)
create mode 100644 lib/kunit/backtrace-suppression-test.c

diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile
index 545b57c3be48..3eee1bd0ce5e 100644
--- a/lib/kunit/Makefile
+++ b/lib/kunit/Makefile
@@ -16,10 +16,13 @@ endif

# KUnit 'hooks' and bug handling are built-in even when KUnit is built
# as a module.
-obj-y += hooks.o \
- bug.o
+obj-y += hooks.o
+obj-$(CONFIG_KUNIT_SUPPRESS_BACKTRACE) += bug.o

obj-$(CONFIG_KUNIT_TEST) += kunit-test.o
+ifeq ($(CCONFIG_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)
diff --git a/lib/kunit/backtrace-suppression-test.c b/lib/kunit/backtrace-suppression-test.c
new file mode 100644
index 000000000000..47c619283802
--- /dev/null
+++ b/lib/kunit/backtrace-suppression-test.c
@@ -0,0 +1,104 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * KUnit test for suppressing warning tracebacks
+ *
+ * Copyright (C) 2024, Guenter Roeck
+ * Author: Guenter Roeck <li...@roeck-us.net>
+ */
+
+#include <kunit/test.h>
+#include <linux/bug.h>
+
+static void backtrace_suppression_test_warn_direct(struct kunit *test)
+{
+ DEFINE_SUPPRESSED_WARNING(backtrace_suppression_test_warn_direct);
+
+ START_SUPPRESSED_WARNING(backtrace_suppression_test_warn_direct);
+ WARN(1, "This backtrace should be suppressed");
+ END_SUPPRESSED_WARNING(backtrace_suppression_test_warn_direct);
+
+ KUNIT_EXPECT_EQ(test, SUPPRESSED_WARNING_COUNT(backtrace_suppression_test_warn_direct), 1);
+}
+
+static void trigger_backtrace_warn(void)
+{
+ WARN(1, "This backtrace should be suppressed");
+}
+
+static void backtrace_suppression_test_warn_indirect(struct kunit *test)
+{
+ DEFINE_SUPPRESSED_WARNING(trigger_backtrace_warn);
+
+ START_SUPPRESSED_WARNING(trigger_backtrace_warn);
+ trigger_backtrace_warn();
+ END_SUPPRESSED_WARNING(trigger_backtrace_warn);
+
+ KUNIT_EXPECT_EQ(test, SUPPRESSED_WARNING_COUNT(trigger_backtrace_warn), 1);
+}
+
+static void backtrace_suppression_test_warn_multi(struct kunit *test)
+{
+ DEFINE_SUPPRESSED_WARNING(trigger_backtrace_warn);
+ DEFINE_SUPPRESSED_WARNING(backtrace_suppression_test_warn_multi);
+
+ START_SUPPRESSED_WARNING(backtrace_suppression_test_warn_multi);
+ START_SUPPRESSED_WARNING(trigger_backtrace_warn);
+ WARN(1, "This backtrace should be suppressed");
+ trigger_backtrace_warn();
+ END_SUPPRESSED_WARNING(trigger_backtrace_warn);
+ END_SUPPRESSED_WARNING(backtrace_suppression_test_warn_multi);
+
+ KUNIT_EXPECT_EQ(test, SUPPRESSED_WARNING_COUNT(backtrace_suppression_test_warn_multi), 1);
+ KUNIT_EXPECT_EQ(test, SUPPRESSED_WARNING_COUNT(trigger_backtrace_warn), 1);
+}
+
+static void backtrace_suppression_test_warn_on_direct(struct kunit *test)
+{
+ DEFINE_SUPPRESSED_WARNING(backtrace_suppression_test_warn_on_direct);
+
+ if (!IS_ENABLED(CONFIG_DEBUG_BUGVERBOSE) && !IS_ENABLED(CONFIG_KALLSYMS))
+ kunit_skip(test, "requires CONFIG_DEBUG_BUGVERBOSE or CONFIG_KALLSYMS");
+
+ START_SUPPRESSED_WARNING(backtrace_suppression_test_warn_on_direct);
+ WARN_ON(1);
+ END_SUPPRESSED_WARNING(backtrace_suppression_test_warn_on_direct);
+
+ KUNIT_EXPECT_EQ(test,
+ SUPPRESSED_WARNING_COUNT(backtrace_suppression_test_warn_on_direct), 1);
+}
+
+static void trigger_backtrace_warn_on(void)
+{
+ WARN_ON(1);
+}
+
+static void backtrace_suppression_test_warn_on_indirect(struct kunit *test)
+{
+ DEFINE_SUPPRESSED_WARNING(trigger_backtrace_warn_on);
+
+ if (!IS_ENABLED(CONFIG_DEBUG_BUGVERBOSE))
+ kunit_skip(test, "requires CONFIG_DEBUG_BUGVERBOSE");
+
+ START_SUPPRESSED_WARNING(trigger_backtrace_warn_on);
+ trigger_backtrace_warn_on();
+ END_SUPPRESSED_WARNING(trigger_backtrace_warn_on);
+
+ KUNIT_EXPECT_EQ(test, SUPPRESSED_WARNING_COUNT(trigger_backtrace_warn_on), 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");
--
2.39.2

Guenter Roeck

unread,
Mar 25, 2024, 1:53:03 PMMar 25
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, Guenter Roeck, Linux Kernel Functional Testing
Document API functions for suppressing warning backtraces.

Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
Acked-by: Dan Carpenter <dan.ca...@linaro.org>
Reviewed-by: Kees Cook <kees...@chromium.org>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
- Rebased to v6.9-rc1
- Added Tested-by:, Acked-by:, and Reviewed-by: tags

Documentation/dev-tools/kunit/usage.rst | 30 ++++++++++++++++++++++++-
1 file changed, 29 insertions(+), 1 deletion(-)

diff --git a/Documentation/dev-tools/kunit/usage.rst b/Documentation/dev-tools/kunit/usage.rst
index 22955d56b379..8d3d36d4103d 100644
--- a/Documentation/dev-tools/kunit/usage.rst
+++ b/Documentation/dev-tools/kunit/usage.rst
@@ -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. To suppress a backtrace in some_function(),
+use the following code.
+
+.. code-block:: c
+
+ static void some_test(struct kunit *test)
+ {
+ DEFINE_SUPPRESSED_WARNING(some_function);
+
+ START_SUPPRESSED_WARNING(some_function);
+ trigger_backtrace();
+ END_SUPPRESSED_WARNING(some_function);
+ }
+
+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, SUPPRESSED_WARNING_COUNT(some_function), 1);

Test Suites
~~~~~~~~~~~
@@ -857,4 +885,4 @@ For example:
dev_managed_string = devm_kstrdup(fake_device, "Hello, World!");

// Everything is cleaned up automatically when the test ends.
- }
\ No newline at end of file
+ }
--
2.39.2

Guenter Roeck

unread,
Mar 25, 2024, 1:53:05 PMMar 25
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, Guenter Roeck, Linux Kernel Functional Testing
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.

Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
Acked-by: Dan Carpenter <dan.ca...@linaro.org>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
- Rebased to v6.9-rc1
- Added Tested-by:, Acked-by:, and Reviewed-by: tags

drivers/gpu/drm/tests/drm_rect_test.c | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/drivers/gpu/drm/tests/drm_rect_test.c b/drivers/gpu/drm/tests/drm_rect_test.c
index 76332cd2ead8..75614cb4deb5 100644
--- a/drivers/gpu/drm/tests/drm_rect_test.c
+++ b/drivers/gpu/drm/tests/drm_rect_test.c
@@ -406,22 +406,28 @@ KUNIT_ARRAY_PARAM(drm_rect_scale, drm_rect_scale_cases, drm_rect_scale_case_desc

static void drm_test_rect_calc_hscale(struct kunit *test)
{
+ DEFINE_SUPPRESSED_WARNING(drm_calc_scale);
const struct drm_rect_scale_case *params = test->param_value;
int scaling_factor;

+ START_SUPPRESSED_WARNING(drm_calc_scale);
scaling_factor = drm_rect_calc_hscale(&params->src, &params->dst,
params->min_range, params->max_range);
+ END_SUPPRESSED_WARNING(drm_calc_scale);

KUNIT_EXPECT_EQ(test, scaling_factor, params->expected_scaling_factor);
}

static void drm_test_rect_calc_vscale(struct kunit *test)
{
+ DEFINE_SUPPRESSED_WARNING(drm_calc_scale);
const struct drm_rect_scale_case *params = test->param_value;
int scaling_factor;

+ START_SUPPRESSED_WARNING(drm_calc_scale);
scaling_factor = drm_rect_calc_vscale(&params->src, &params->dst,
params->min_range, params->max_range);
+ END_SUPPRESSED_WARNING(drm_calc_scale);

KUNIT_EXPECT_EQ(test, scaling_factor, params->expected_scaling_factor);
}
--
2.39.2

Guenter Roeck

unread,
Mar 25, 2024, 1:53:06 PMMar 25
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, Guenter Roeck, Linux Kernel Functional Testing, Jakub Kicinski
dev_addr_lists_test generates lock warning noise at the end of tests
if lock debugging is enabled. There are two sets of warnings.

WARNING: CPU: 0 PID: 689 at kernel/locking/mutex.c:923 __mutex_unlock_slowpath.constprop.0+0x13c/0x368
DEBUG_LOCKS_WARN_ON(__owner_task(owner) != __get_current())

WARNING: kunit_try_catch/1336 still has locks held!

KUnit test cleanup is not guaranteed to run in the same thread as the test
itself. For this test, this means that rtnl_lock() and rtnl_unlock() may
be called from different threads. This triggers the warnings.
Suppress the warnings because they are irrelevant for the test and just
confusing.

The first warning can be suppressed by using START_SUPPRESSED_WARNING()
and END_SUPPRESSED_WARNING() around the call to rtnl_unlock(). To suppress
the second warning, it is necessary to set debug_locks_silent while the
rtnl lock is held.

Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
Cc: David Gow <davi...@google.com>
Cc: Jakub Kicinski <ku...@kernel.org>
Acked-by: Dan Carpenter <dan.ca...@linaro.org>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
- Rebased to v6.9-rc1
- Added Tested-by:, Acked-by:, and Reviewed-by: tags

net/core/dev_addr_lists_test.c | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/net/core/dev_addr_lists_test.c b/net/core/dev_addr_lists_test.c
index 4dbd0dc6aea2..b427dd1a3c93 100644
--- a/net/core/dev_addr_lists_test.c
+++ b/net/core/dev_addr_lists_test.c
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0-or-later

#include <kunit/test.h>
+#include <linux/debug_locks.h>
#include <linux/etherdevice.h>
#include <linux/netdevice.h>
#include <linux/rtnetlink.h>
@@ -49,6 +50,7 @@ static int dev_addr_test_init(struct kunit *test)
KUNIT_FAIL(test, "Can't register netdev %d", err);
}

+ debug_locks_silent = 1;
rtnl_lock();
return 0;
}
@@ -56,8 +58,12 @@ static int dev_addr_test_init(struct kunit *test)
static void dev_addr_test_exit(struct kunit *test)
{
struct net_device *netdev = test->priv;
+ DEFINE_SUPPRESSED_WARNING(__mutex_unlock_slowpath);

+ START_SUPPRESSED_WARNING(__mutex_unlock_slowpath);
rtnl_unlock();
+ END_SUPPRESSED_WARNING(__mutex_unlock_slowpath);
+ debug_locks_silent = 0;
unregister_netdev(netdev);
free_netdev(netdev);
}
--
2.39.2

Guenter Roeck

unread,
Mar 25, 2024, 1:53:08 PMMar 25
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, Guenter Roeck, Linux Kernel Functional Testing
Add name of functions triggering warning backtraces to the __bug_table
object section to enable support for suppressing WARNING backtraces.

To limit image size impact, the pointer to the function name is only added
to the __bug_table section if both CONFIG_KUNIT_SUPPRESS_BACKTRACE and
CONFIG_DEBUG_BUGVERBOSE are enabled. Otherwise, the __func__ assembly
parameter is replaced with a (dummy) NULL parameter to avoid an image size
increase due to unused __func__ entries (this is necessary because __func__
is not a define but a virtual variable).

Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
Acked-by: Dan Carpenter <dan.ca...@linaro.org>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
- Rebased to v6.9-rc1
- Added Tested-by:, Acked-by:, and Reviewed-by: tags
- Introduced KUNIT_SUPPRESS_BACKTRACE configuration option

arch/x86/include/asm/bug.h | 21 ++++++++++++++++-----
1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/arch/x86/include/asm/bug.h b/arch/x86/include/asm/bug.h
index a3ec87d198ac..7698dfa74c98 100644
--- a/arch/x86/include/asm/bug.h
+++ b/arch/x86/include/asm/bug.h
@@ -23,18 +23,28 @@

#ifdef CONFIG_DEBUG_BUGVERBOSE

+#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE
+# define HAVE_BUG_FUNCTION
+# define __BUG_FUNC_PTR __BUG_REL(%c1)
+# define __BUG_FUNC __func__
+#else
+# define __BUG_FUNC_PTR
+# define __BUG_FUNC NULL
+#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */
+
#define _BUG_FLAGS(ins, flags, extra) \
do { \
asm_inline volatile("1:\t" ins "\n" \
".pushsection __bug_table,\"aw\"\n" \
"2:\t" __BUG_REL(1b) "\t# bug_entry::bug_addr\n" \
"\t" __BUG_REL(%c0) "\t# bug_entry::file\n" \
- "\t.word %c1" "\t# bug_entry::line\n" \
- "\t.word %c2" "\t# bug_entry::flags\n" \
- "\t.org 2b+%c3\n" \
+ "\t" __BUG_FUNC_PTR "\t# bug_entry::function\n" \
+ "\t.word %c2" "\t# bug_entry::line\n" \
+ "\t.word %c3" "\t# bug_entry::flags\n" \
+ "\t.org 2b+%c4\n" \
".popsection\n" \
extra \
- : : "i" (__FILE__), "i" (__LINE__), \
+ : : "i" (__FILE__), "i" (__BUG_FUNC), "i" (__LINE__),\
"i" (flags), \
"i" (sizeof(struct bug_entry))); \
} while (0)
@@ -80,7 +90,8 @@ do { \
do { \
__auto_type __flags = BUGFLAG_WARNING|(flags); \
instrumentation_begin(); \
- _BUG_FLAGS(ASM_UD2, __flags, ASM_REACHABLE); \
+ if (!IS_SUPPRESSED_WARNING(__func__)) \
+ _BUG_FLAGS(ASM_UD2, __flags, ASM_REACHABLE); \
instrumentation_end(); \
} while (0)

--
2.39.2

Guenter Roeck

unread,
Mar 25, 2024, 1:53:10 PMMar 25
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, Guenter Roeck, Linux Kernel Functional Testing
Add name of functions triggering warning backtraces to the __bug_table
object section to enable support for suppressing WARNING backtraces.

To limit image size impact, the pointer to the function name is only added
to the __bug_table section if both CONFIG_KUNIT_SUPPRESS_BACKTRACE and
CONFIG_DEBUG_BUGVERBOSE are enabled. Otherwise, the __func__ assembly
parameter is replaced with a (dummy) NULL parameter to avoid an image size
increase due to unused __func__ entries (this is necessary because __func__
is not a define but a virtual variable).

Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
Acked-by: Dan Carpenter <dan.ca...@linaro.org>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
- Rebased to v6.9-rc1
- Added Tested-by:, Acked-by:, and Reviewed-by: tags
- Introduced KUNIT_SUPPRESS_BACKTRACE configuration option

arch/arm64/include/asm/asm-bug.h | 29 +++++++++++++++++++----------
arch/arm64/include/asm/bug.h | 8 +++++++-
2 files changed, 26 insertions(+), 11 deletions(-)

diff --git a/arch/arm64/include/asm/asm-bug.h b/arch/arm64/include/asm/asm-bug.h
index c762038ba400..c6d22e3cd840 100644
--- a/arch/arm64/include/asm/asm-bug.h
+++ b/arch/arm64/include/asm/asm-bug.h
@@ -8,36 +8,45 @@
#include <asm/brk-imm.h>

#ifdef CONFIG_DEBUG_BUGVERBOSE
-#define _BUGVERBOSE_LOCATION(file, line) __BUGVERBOSE_LOCATION(file, line)
-#define __BUGVERBOSE_LOCATION(file, line) \
+
+#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE
+# define HAVE_BUG_FUNCTION
+# define __BUG_FUNC_PTR(func) .long func - .;
+#else
+# define __BUG_FUNC_PTR(func)
+#endif
+
+#define _BUGVERBOSE_LOCATION(file, func, line) __BUGVERBOSE_LOCATION(file, func, line)
+#define __BUGVERBOSE_LOCATION(file, func, line) \
.pushsection .rodata.str,"aMS",@progbits,1; \
14472: .string file; \
.popsection; \
\
.long 14472b - .; \
+ __BUG_FUNC_PTR(func) \
.short line;
#else
-#define _BUGVERBOSE_LOCATION(file, line)
+#define _BUGVERBOSE_LOCATION(file, func, line)
#endif

#ifdef CONFIG_GENERIC_BUG

-#define __BUG_ENTRY(flags) \
+#define __BUG_ENTRY(flags, func) \
.pushsection __bug_table,"aw"; \
.align 2; \
14470: .long 14471f - .; \
-_BUGVERBOSE_LOCATION(__FILE__, __LINE__) \
- .short flags; \
+_BUGVERBOSE_LOCATION(__FILE__, func, __LINE__) \
+ .short flags; \
.popsection; \
14471:
#else
-#define __BUG_ENTRY(flags)
+#define __BUG_ENTRY(flags, func)
#endif

-#define ASM_BUG_FLAGS(flags) \
- __BUG_ENTRY(flags) \
+#define ASM_BUG_FLAGS(flags, func) \
+ __BUG_ENTRY(flags, func) \
brk BUG_BRK_IMM

-#define ASM_BUG() ASM_BUG_FLAGS(0)
+#define ASM_BUG() ASM_BUG_FLAGS(0, .)

#endif /* __ASM_ASM_BUG_H */
diff --git a/arch/arm64/include/asm/bug.h b/arch/arm64/include/asm/bug.h
index 28be048db3f6..044c5e24a17d 100644
--- a/arch/arm64/include/asm/bug.h
+++ b/arch/arm64/include/asm/bug.h
@@ -11,8 +11,14 @@

#include <asm/asm-bug.h>

+#ifdef HAVE_BUG_FUNCTION
+# define __BUG_FUNC __func__
+#else
+# define __BUG_FUNC NULL
+#endif
+
#define __BUG_FLAGS(flags) \
- asm volatile (__stringify(ASM_BUG_FLAGS(flags)));
+ asm volatile (__stringify(ASM_BUG_FLAGS(flags, %c0)) : : "i" (__BUG_FUNC));

#define BUG() do { \
__BUG_FLAGS(0); \
--
2.39.2

Guenter Roeck

unread,
Mar 25, 2024, 1:53:11 PMMar 25
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, Guenter Roeck, Linux Kernel Functional Testing
Add name of functions triggering warning backtraces to the __bug_table
object section to enable support for suppressing WARNING backtraces.

To limit image size impact, the pointer to the function name is only added
to the __bug_table section if both CONFIG_KUNIT_SUPPRESS_BACKTRACE and
CONFIG_DEBUG_BUGVERBOSE are enabled. Otherwise, the __func__ assembly
parameter is replaced with a (dummy) NULL parameter to avoid an image size
increase due to unused __func__ entries (this is necessary because __func__
is not a define but a virtual variable).

Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
Acked-by: Dan Carpenter <dan.ca...@linaro.org>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
- Rebased to v6.9-rc1; resolved context conflict
- Added Tested-by:, Acked-by:, and Reviewed-by: tags
- Introduced KUNIT_SUPPRESS_BACKTRACE configuration option

arch/loongarch/include/asm/bug.h | 38 +++++++++++++++++++++++---------
1 file changed, 27 insertions(+), 11 deletions(-)

diff --git a/arch/loongarch/include/asm/bug.h b/arch/loongarch/include/asm/bug.h
index 08388876ade4..193f396d81a0 100644
--- a/arch/loongarch/include/asm/bug.h
+++ b/arch/loongarch/include/asm/bug.h
@@ -3,47 +3,63 @@
#define __ASM_BUG_H

#include <asm/break.h>
+#include <kunit/bug.h>
#include <linux/stringify.h>

#ifndef CONFIG_DEBUG_BUGVERBOSE
-#define _BUGVERBOSE_LOCATION(file, line)
+#define _BUGVERBOSE_LOCATION(file, func, line)
#else
-#define __BUGVERBOSE_LOCATION(file, line) \
+#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE
+# define HAVE_BUG_FUNCTION
+# define __BUG_FUNC_PTR(func) .long func - .;
+#else
+# define __BUG_FUNC_PTR(func)
+#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */
+
+#define __BUGVERBOSE_LOCATION(file, func, line) \
.pushsection .rodata.str, "aMS", @progbits, 1; \
10002: .string file; \
.popsection; \
\
.long 10002b - .; \
+ __BUG_FUNC_PTR(func) \
.short line;
-#define _BUGVERBOSE_LOCATION(file, line) __BUGVERBOSE_LOCATION(file, line)
+#define _BUGVERBOSE_LOCATION(file, func, line) __BUGVERBOSE_LOCATION(file, func, line)
#endif

#ifndef CONFIG_GENERIC_BUG
-#define __BUG_ENTRY(flags)
+#define __BUG_ENTRY(flags, func)
#else
-#define __BUG_ENTRY(flags) \
+#define __BUG_ENTRY(flags, func) \
.pushsection __bug_table, "aw"; \
.align 2; \
10000: .long 10001f - .; \
- _BUGVERBOSE_LOCATION(__FILE__, __LINE__) \
+ _BUGVERBOSE_LOCATION(__FILE__, func, __LINE__) \
.short flags; \
.popsection; \
10001:
#endif

-#define ASM_BUG_FLAGS(flags) \
- __BUG_ENTRY(flags) \
+#define ASM_BUG_FLAGS(flags, func) \
+ __BUG_ENTRY(flags, func) \
break BRK_BUG

-#define ASM_BUG() ASM_BUG_FLAGS(0)
+#define ASM_BUG() ASM_BUG_FLAGS(0, .)
+
+#ifdef HAVE_BUG_FUNCTION
+# define __BUG_FUNC __func__
+#else
+# define __BUG_FUNC NULL
+#endif

#define __BUG_FLAGS(flags) \
- asm_inline volatile (__stringify(ASM_BUG_FLAGS(flags)));
+ asm_inline volatile (__stringify(ASM_BUG_FLAGS(flags, %0)) : : "i" (__BUG_FUNC));

#define __WARN_FLAGS(flags) \
do { \
instrumentation_begin(); \
- __BUG_FLAGS(BUGFLAG_WARNING|(flags)); \
+ if (!IS_SUPPRESSED_WARNING(__func__)) \
+ __BUG_FLAGS(BUGFLAG_WARNING|(flags)); \
annotate_reachable(); \

Guenter Roeck

unread,
Mar 25, 2024, 1:53:13 PMMar 25
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, Guenter Roeck, Linux Kernel Functional Testing, Helge Deller
Add name of functions triggering warning backtraces to the __bug_table
object section to enable support for suppressing WARNING backtraces.

To limit image size impact, the pointer to the function name is only added
to the __bug_table section if both CONFIG_KUNIT_SUPPRESS_BACKTRACE and
CONFIG_DEBUG_BUGVERBOSE are enabled. Otherwise, the __func__ assembly
parameter is replaced with a (dummy) NULL parameter to avoid an image size
increase due to unused __func__ entries (this is necessary because __func__
is not a define but a virtual variable).

While at it, declare assembler parameters as constants where possible.
Refine .blockz instructions to calculate the necessary padding instead
of using fixed values.

Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
Acked-by: Dan Carpenter <dan.ca...@linaro.org>
Acked-by: Helge Deller <del...@gmx.de>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
- Rebased to v6.9-rc1
- Added Tested-by:, Acked-by:, and Reviewed-by: tags
- Introduced KUNIT_SUPPRESS_BACKTRACE configuration option

arch/parisc/include/asm/bug.h | 29 +++++++++++++++++++++--------
1 file changed, 21 insertions(+), 8 deletions(-)

diff --git a/arch/parisc/include/asm/bug.h b/arch/parisc/include/asm/bug.h
index 833555f74ffa..b59c3f7380bf 100644
--- a/arch/parisc/include/asm/bug.h
+++ b/arch/parisc/include/asm/bug.h
@@ -23,8 +23,17 @@
# define __BUG_REL(val) ".word " __stringify(val)
#endif

-
#ifdef CONFIG_DEBUG_BUGVERBOSE
+
+#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE
+# define HAVE_BUG_FUNCTION
+# define __BUG_FUNC_PTR __BUG_REL(%c1)
+# define __BUG_FUNC __func__
+#else
+# define __BUG_FUNC_PTR
+# define __BUG_FUNC NULL
+#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */
+
#define BUG() \
do { \
asm volatile("\n" \
@@ -33,10 +42,12 @@
"\t.align 4\n" \
"2:\t" __BUG_REL(1b) "\n" \
"\t" __BUG_REL(%c0) "\n" \
- "\t.short %1, %2\n" \
- "\t.blockz %3-2*4-2*2\n" \
+ "\t" __BUG_FUNC_PTR "\n" \
+ "\t.short %c2, %c3\n" \
+ "\t.blockz %c4-(.-2b)\n" \
"\t.popsection" \
- : : "i" (__FILE__), "i" (__LINE__), \
+ : : "i" (__FILE__), "i" (__BUG_FUNC), \
+ "i" (__LINE__), \
"i" (0), "i" (sizeof(struct bug_entry)) ); \
unreachable(); \
} while(0)
@@ -58,10 +69,12 @@
"\t.align 4\n" \
"2:\t" __BUG_REL(1b) "\n" \
"\t" __BUG_REL(%c0) "\n" \
- "\t.short %1, %2\n" \
- "\t.blockz %3-2*4-2*2\n" \
+ "\t" __BUG_FUNC_PTR "\n" \
+ "\t.short %c2, %3\n" \
+ "\t.blockz %c4-(.-2b)\n" \
"\t.popsection" \
- : : "i" (__FILE__), "i" (__LINE__), \
+ : : "i" (__FILE__), "i" (__BUG_FUNC), \
+ "i" (__LINE__), \
"i" (BUGFLAG_WARNING|(flags)), \
"i" (sizeof(struct bug_entry)) ); \
} while(0)
@@ -74,7 +87,7 @@
"\t.align 4\n" \
"2:\t" __BUG_REL(1b) "\n" \
"\t.short %0\n" \
- "\t.blockz %1-4-2\n" \
+ "\t.blockz %c1-(.-2b)\n" \
"\t.popsection" \
: : "i" (BUGFLAG_WARNING|(flags)), \
"i" (sizeof(struct bug_entry)) ); \
--
2.39.2

Guenter Roeck

unread,
Mar 25, 2024, 1:53:14 PMMar 25
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, Guenter Roeck, Linux Kernel Functional Testing
Add name of functions triggering warning backtraces to the __bug_table
object section to enable support for suppressing WARNING backtraces.

To limit image size impact, the pointer to the function name is only added
to the __bug_table section if both CONFIG_KUNIT_SUPPRESS_BACKTRACE and
CONFIG_DEBUG_BUGVERBOSE are enabled. Otherwise, the __func__ assembly
parameter is replaced with a (dummy) NULL parameter to avoid an image size
increase due to unused __func__ entries (this is necessary because
__func__ is not a define but a virtual variable).

Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
Acked-by: Dan Carpenter <dan.ca...@linaro.org>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
- Rebased to v6.9-rc1 (simplified assembler changes after upstream commit
3938490e78f4 ("s390/bug: remove entry size from __bug_table section")
- Added Tested-by:, Acked-by:, and Reviewed-by: tags
- Introduced KUNIT_SUPPRESS_BACKTRACE configuration option

arch/s390/include/asm/bug.h | 17 ++++++++++++++---
1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/arch/s390/include/asm/bug.h b/arch/s390/include/asm/bug.h
index c500d45fb465..44d4e9f24ae0 100644
--- a/arch/s390/include/asm/bug.h
+++ b/arch/s390/include/asm/bug.h
@@ -8,6 +8,15 @@

#ifdef CONFIG_DEBUG_BUGVERBOSE

+#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE
+# define HAVE_BUG_FUNCTION
+# define __BUG_FUNC_PTR " .long %0-.\n"
+# define __BUG_FUNC __func__
+#else
+# define __BUG_FUNC_PTR
+# define __BUG_FUNC NULL
+#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */
+
#define __EMIT_BUG(x) do { \
asm_inline volatile( \
"0: mc 0,0\n" \
@@ -17,10 +26,12 @@
".section __bug_table,\"aw\"\n" \
"2: .long 0b-.\n" \
" .long 1b-.\n" \
- " .short %0,%1\n" \
- " .org 2b+%2\n" \
+ __BUG_FUNC_PTR \
+ " .short %1,%2\n" \
+ " .org 2b+%3\n" \
".previous\n" \
- : : "i" (__LINE__), \
+ : : "i" (__BUG_FUNC), \
+ "i" (__LINE__), \
"i" (x), \
"i" (sizeof(struct bug_entry))); \
} while (0)
--
2.39.2

Guenter Roeck

unread,
Mar 25, 2024, 1:53:16 PMMar 25
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, Guenter Roeck, Linux Kernel Functional Testing
Add name of functions triggering warning backtraces to the __bug_table
object section to enable support for suppressing WARNING backtraces.

To limit image size impact, the pointer to the function name is only added
to the __bug_table section if both CONFIG_KUNIT_SUPPRESS_BACKTRACE and
CONFIG_DEBUG_BUGVERBOSE are enabled. Otherwise, the __func__ assembly
parameter is replaced with a (dummy) NULL parameter to avoid an image size
increase due to unused __func__ entries (this is necessary because __func__
is not a define but a virtual variable).

Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
Acked-by: Dan Carpenter <dan.ca...@linaro.org>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
- Rebased to v6.9-rc1
- Added Tested-by:, Acked-by:, and Reviewed-by: tags
- Introduced KUNIT_SUPPRESS_BACKTRACE configuration option

arch/sh/include/asm/bug.h | 26 ++++++++++++++++++++++----
1 file changed, 22 insertions(+), 4 deletions(-)

diff --git a/arch/sh/include/asm/bug.h b/arch/sh/include/asm/bug.h
index 05a485c4fabc..470ce6567d20 100644
--- a/arch/sh/include/asm/bug.h
+++ b/arch/sh/include/asm/bug.h
@@ -24,21 +24,36 @@
* The offending file and line are encoded in the __bug_table section.
*/
#ifdef CONFIG_DEBUG_BUGVERBOSE
+
+#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE
+# define HAVE_BUG_FUNCTION
+# define __BUG_FUNC_PTR "\t.long %O2\n"
+#else
+# define __BUG_FUNC_PTR
+#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */
+
#define _EMIT_BUG_ENTRY \
"\t.pushsection __bug_table,\"aw\"\n" \
"2:\t.long 1b, %O1\n" \
- "\t.short %O2, %O3\n" \
- "\t.org 2b+%O4\n" \
+ __BUG_FUNC_PTR \
+ "\t.short %O3, %O4\n" \
+ "\t.org 2b+%O5\n" \
"\t.popsection\n"
#else
#define _EMIT_BUG_ENTRY \
"\t.pushsection __bug_table,\"aw\"\n" \
"2:\t.long 1b\n" \
- "\t.short %O3\n" \
- "\t.org 2b+%O4\n" \
+ "\t.short %O4\n" \
+ "\t.org 2b+%O5\n" \
"\t.popsection\n"
#endif

+#ifdef HAVE_BUG_FUNCTION
+# define __BUG_FUNC __func__
+#else
+# define __BUG_FUNC NULL
+#endif
+
#define BUG() \
do { \
__asm__ __volatile__ ( \
@@ -47,6 +62,7 @@ do { \
: \
: "n" (TRAPA_BUG_OPCODE), \
"i" (__FILE__), \
+ "i" (__BUG_FUNC), \
"i" (__LINE__), "i" (0), \
"i" (sizeof(struct bug_entry))); \
unreachable(); \
@@ -60,6 +76,7 @@ do { \
: \
: "n" (TRAPA_BUG_OPCODE), \
"i" (__FILE__), \
+ "i" (__BUG_FUNC), \
"i" (__LINE__), \
"i" (BUGFLAG_WARNING|(flags)), \
"i" (sizeof(struct bug_entry))); \
@@ -85,6 +102,7 @@ do { \
: \
: "n" (TRAPA_BUG_OPCODE), \
"i" (__FILE__), \
+ "i" (__BUG_FUNC), \
"i" (__LINE__), \
"i" (BUGFLAG_UNWINDER), \
"i" (sizeof(struct bug_entry))); \
--
2.39.2

Guenter Roeck

unread,
Mar 25, 2024, 1:53:18 PMMar 25
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, Guenter Roeck, Linux Kernel Functional Testing
Add name of functions triggering warning backtraces to the __bug_table
object section to enable support for suppressing WARNING backtraces.

To limit image size impact, the pointer to the function name is only added
to the __bug_table section if both CONFIG_KUNIT_SUPPRESS_BACKTRACE and
CONFIG_DEBUG_BUGVERBOSE are enabled. Otherwise, the __func__ assembly
parameter is replaced with a (dummy) NULL parameter to avoid an image size
increase due to unused __func__ entries (this is necessary because __func__
is not a define but a virtual variable).

To simplify the implementation, unify the __BUG_ENTRY_ADDR and
__BUG_ENTRY_FILE macros into a single macro named __BUG_REL() which takes
the address, file, or function reference as parameter.

Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
Acked-by: Dan Carpenter <dan.ca...@linaro.org>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
- Rebased to v6.9-rc1
- Added Tested-by:, Acked-by:, and Reviewed-by: tags
- Introduced KUNIT_SUPPRESS_BACKTRACE configuration option

arch/riscv/include/asm/bug.h | 38 ++++++++++++++++++++++++------------
1 file changed, 26 insertions(+), 12 deletions(-)

diff --git a/arch/riscv/include/asm/bug.h b/arch/riscv/include/asm/bug.h
index 1aaea81fb141..79f360af4ad8 100644
--- a/arch/riscv/include/asm/bug.h
+++ b/arch/riscv/include/asm/bug.h
@@ -30,26 +30,39 @@
typedef u32 bug_insn_t;

#ifdef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
-#define __BUG_ENTRY_ADDR RISCV_INT " 1b - ."
-#define __BUG_ENTRY_FILE RISCV_INT " %0 - ."
+#define __BUG_REL(val) RISCV_INT " " __stringify(val) " - ."
#else
-#define __BUG_ENTRY_ADDR RISCV_PTR " 1b"
-#define __BUG_ENTRY_FILE RISCV_PTR " %0"
+#define __BUG_REL(val) RISCV_PTR " " __stringify(val)
#endif

#ifdef CONFIG_DEBUG_BUGVERBOSE
+
+#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE
+# define HAVE_BUG_FUNCTION
+# define __BUG_FUNC_PTR __BUG_REL(%1)
+#else
+# define __BUG_FUNC_PTR
+#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */
+
#define __BUG_ENTRY \
- __BUG_ENTRY_ADDR "\n\t" \
- __BUG_ENTRY_FILE "\n\t" \
- RISCV_SHORT " %1\n\t" \
- RISCV_SHORT " %2"
+ __BUG_REL(1b) "\n\t" \
+ __BUG_REL(%0) "\n\t" \
+ __BUG_FUNC_PTR "\n\t" \
+ RISCV_SHORT " %2\n\t" \
+ RISCV_SHORT " %3"
#else
#define __BUG_ENTRY \
- __BUG_ENTRY_ADDR "\n\t" \
- RISCV_SHORT " %2"
+ __BUG_REL(1b) "\n\t" \
+ RISCV_SHORT " %3"
#endif

#ifdef CONFIG_GENERIC_BUG
+#ifdef HAVE_BUG_FUNCTION
+# define __BUG_FUNC __func__
+#else
+# define __BUG_FUNC NULL
+#endif
+
#define __BUG_FLAGS(flags) \
do { \
__asm__ __volatile__ ( \
@@ -58,10 +71,11 @@ do { \
".pushsection __bug_table,\"aw\"\n\t" \
"2:\n\t" \
__BUG_ENTRY "\n\t" \
- ".org 2b + %3\n\t" \
+ ".org 2b + %4\n\t" \
".popsection" \
: \
- : "i" (__FILE__), "i" (__LINE__), \
+ : "i" (__FILE__), "i" (__BUG_FUNC), \
+ "i" (__LINE__), \
"i" (flags), \

Guenter Roeck

unread,
Mar 25, 2024, 1:53:19 PMMar 25
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, Guenter Roeck, Linux Kernel Functional Testing
Add name of functions triggering warning backtraces to the __bug_table
object section to enable support for suppressing WARNING backtraces.

To limit image size impact, the pointer to the function name is only added
to the __bug_table section if both CONFIG_KUNIT_SUPPRESS_BACKTRACE and
CONFIG_DEBUG_BUGVERBOSE are enabled. Otherwise, the __func__ assembly
parameter is replaced with a (dummy) NULL parameter to avoid an image size
increase due to unused __func__ entries (this is necessary because __func__
is not a define but a virtual variable).

Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
Acked-by: Dan Carpenter <dan.ca...@linaro.org>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
- Rebased to v6.9-rc1
- Added Tested-by:, Acked-by:, and Reviewed-by: tags
- Introduced KUNIT_SUPPRESS_BACKTRACE configuration option

arch/powerpc/include/asm/bug.h | 37 +++++++++++++++++++++++++---------
1 file changed, 28 insertions(+), 9 deletions(-)

diff --git a/arch/powerpc/include/asm/bug.h b/arch/powerpc/include/asm/bug.h
index 1db485aacbd9..5b06745d20aa 100644
--- a/arch/powerpc/include/asm/bug.h
+++ b/arch/powerpc/include/asm/bug.h
@@ -14,6 +14,9 @@
.section __bug_table,"aw"
5001: .4byte \addr - .
.4byte 5002f - .
+#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE
+ .4byte 0
+#endif
.short \line, \flags
.org 5001b+BUG_ENTRY_SIZE
.previous
@@ -32,30 +35,46 @@
#endif /* verbose */

#else /* !__ASSEMBLY__ */
-/* _EMIT_BUG_ENTRY expects args %0,%1,%2,%3 to be FILE, LINE, flags and
- sizeof(struct bug_entry), respectively */
+/* _EMIT_BUG_ENTRY expects args %0,%1,%2,%3,%4 to be FILE, __func__, LINE, flags
+ and sizeof(struct bug_entry), respectively */
#ifdef CONFIG_DEBUG_BUGVERBOSE
+
+#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE
+# define HAVE_BUG_FUNCTION
+# define __BUG_FUNC_PTR " .4byte %1 - .\n"
+#else
+# define __BUG_FUNC_PTR
+#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */
+
#define _EMIT_BUG_ENTRY \
".section __bug_table,\"aw\"\n" \
"2: .4byte 1b - .\n" \
" .4byte %0 - .\n" \
- " .short %1, %2\n" \
- ".org 2b+%3\n" \
+ __BUG_FUNC_PTR \
+ " .short %2, %3\n" \
+ ".org 2b+%4\n" \
".previous\n"
#else
#define _EMIT_BUG_ENTRY \
".section __bug_table,\"aw\"\n" \
"2: .4byte 1b - .\n" \
- " .short %2\n" \
- ".org 2b+%3\n" \
+ " .short %3\n" \
+ ".org 2b+%4\n" \
".previous\n"
#endif

+#ifdef HAVE_BUG_FUNCTION
+# define __BUG_FUNC __func__
+#else
+# define __BUG_FUNC NULL
+#endif
+
#define BUG_ENTRY(insn, flags, ...) \
__asm__ __volatile__( \
"1: " insn "\n" \
_EMIT_BUG_ENTRY \
- : : "i" (__FILE__), "i" (__LINE__), \
+ : : "i" (__FILE__), "i" (__BUG_FUNC), \
+ "i" (__LINE__), \
"i" (flags), \
"i" (sizeof(struct bug_entry)), \
##__VA_ARGS__)
@@ -80,7 +99,7 @@
if (x) \
BUG(); \
} else { \
- BUG_ENTRY(PPC_TLNEI " %4, 0", 0, "r" ((__force long)(x))); \
+ BUG_ENTRY(PPC_TLNEI " %5, 0", 0, "r" ((__force long)(x))); \
} \
} while (0)

@@ -90,7 +109,7 @@
if (__ret_warn_on) \
__WARN(); \
} else { \
- BUG_ENTRY(PPC_TLNEI " %4, 0", \
+ BUG_ENTRY(PPC_TLNEI " %5, 0", \
BUGFLAG_WARNING | BUGFLAG_TAINT(TAINT_WARN), \
"r" (__ret_warn_on)); \
} \
--
2.39.2

Maíra Canal

unread,
Mar 25, 2024, 3:05:58 PMMar 25
to Guenter Roeck, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, Linux Kernel Functional Testing
Hi Guenter,

On 3/25/24 14:52, Guenter Roeck wrote:
> 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.
>
> Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
> Acked-by: Dan Carpenter <dan.ca...@linaro.org>
> Signed-off-by: Guenter Roeck <li...@roeck-us.net>
> ---
> - Rebased to v6.9-rc1
> - Added Tested-by:, Acked-by:, and Reviewed-by: tags
>
> drivers/gpu/drm/tests/drm_rect_test.c | 6 ++++++
> 1 file changed, 6 insertions(+)
>
> diff --git a/drivers/gpu/drm/tests/drm_rect_test.c b/drivers/gpu/drm/tests/drm_rect_test.c
> index 76332cd2ead8..75614cb4deb5 100644
> --- a/drivers/gpu/drm/tests/drm_rect_test.c
> +++ b/drivers/gpu/drm/tests/drm_rect_test.c
> @@ -406,22 +406,28 @@ KUNIT_ARRAY_PARAM(drm_rect_scale, drm_rect_scale_cases, drm_rect_scale_case_desc
>
> static void drm_test_rect_calc_hscale(struct kunit *test)
> {
> + DEFINE_SUPPRESSED_WARNING(drm_calc_scale);
> const struct drm_rect_scale_case *params = test->param_value;
> int scaling_factor;
>
> + START_SUPPRESSED_WARNING(drm_calc_scale);

I'm not sure if it is not that obvious only to me, but it would be nice
to have a comment here, remembering that we provide bad parameters in
some test cases.

Best Regards,
- Maíra

Guenter Roeck

unread,
Mar 25, 2024, 3:24:45 PMMar 25
to Maíra Canal, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, Linux Kernel Functional Testing
Hi,
Sure. Something like this ?

/*
* 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.
*/

Thanks,
Guenter

Maíra Canal

unread,
Mar 25, 2024, 9:10:23 PMMar 25
to Guenter Roeck, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, Linux Kernel Functional Testing
Yeah, perfect. With that, feel free to add my

Acked-by: Maíra Canal <mca...@igalia.com>

Best Regards,
- Maíra

> Thanks,
> Guenter

Guenter Roeck

unread,
Mar 26, 2024, 1:02:39 AMMar 26
to Maíra Canal, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, Linux Kernel Functional Testing
Thanks!
Guenter

Simon Horman

unread,
Mar 27, 2024, 10:44:43 AMMar 27
to Guenter Roeck, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, Linux Kernel Functional Testing
Hi Guenter,

a minor nit from my side: this change results in a Kernel doc warning.

.../bug.h:29: warning: expecting prototype for _EMIT_BUG_ENTRY(). Prototype was for HAVE_BUG_FUNCTION() instead

Perhaps either the new code should be placed above the Kernel doc,
or scripts/kernel-doc should be enhanced?
...

Guenter Roeck

unread,
Mar 27, 2024, 11:10:57 AMMar 27
to Simon Horman, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, Linux Kernel Functional Testing
Thanks a lot for the feedback.

The definition block needs to be inside CONFIG_DEBUG_BUGVERBOSE,
so it would be a bit odd to move it above the documentation
just to make kerneldoc happy. I am not really sure that to do
about it.

I'll wait for comments from others before making any changes.

Thanks,
Guenter

Simon Horman

unread,
Mar 27, 2024, 3:39:29 PMMar 27
to Guenter Roeck, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, Linux Kernel Functional Testing
FWIIW, I agree that would be odd.
But perhaps the #ifdef could also move above the Kernel doc?
Maybe not a great idea, but the best one I've had so far.

Guenter Roeck

unread,
Mar 29, 2024, 11:40:10 AMMar 29
to Simon Horman, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, Linux Kernel Functional Testing
On Wed, Mar 27, 2024 at 07:39:20PM +0000, Simon Horman wrote:
[ ... ]
> > >
> > > Hi Guenter,
> > >
> > > a minor nit from my side: this change results in a Kernel doc warning.
> > >
> > > .../bug.h:29: warning: expecting prototype for _EMIT_BUG_ENTRY(). Prototype was for HAVE_BUG_FUNCTION() instead
> > >
> > > Perhaps either the new code should be placed above the Kernel doc,
> > > or scripts/kernel-doc should be enhanced?
> > >
> >
> > Thanks a lot for the feedback.
> >
> > The definition block needs to be inside CONFIG_DEBUG_BUGVERBOSE,
> > so it would be a bit odd to move it above the documentation
> > just to make kerneldoc happy. I am not really sure that to do
> > about it.
>
> FWIIW, I agree that would be odd.
> But perhaps the #ifdef could also move above the Kernel doc?
> Maybe not a great idea, but the best one I've had so far.
>

I did that for the next version of the patch series. It is a bit more
clumsy, so I left it as separate patch on top of this patch. I'd
still like to get input from others before making the change final.

Thanks,
Guenter

Guenter Roeck

unread,
Apr 3, 2024, 9:19:43 AMApr 3
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, x...@kernel.org, Guenter Roeck
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
- 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

----------------------------------------------------------------
Guenter Roeck (15):
bug/kunit: Core support for suppressing warning backtraces
kunit: bug: Count suppressed warning backtraces
kunit: Add test cases for backtrace warning suppression
kunit: Add documentation for warning backtrace suppression API
drm: Suppress intentional warning backtraces in scaling unit tests
net: kunit: Suppress lock warning noise at end of dev_addr_lists tests
x86: Add support for suppressing warning backtraces
arm64: Add support for suppressing warning backtraces
loongarch: Add support for suppressing warning backtraces
parisc: Add support for suppressing warning backtraces
s390: Add support for suppressing warning backtraces
sh: Add support for suppressing warning backtraces
sh: Move defines needed for suppressing warning backtraces
riscv: Add support for suppressing warning backtraces
powerpc: Add support for suppressing warning backtraces

Documentation/dev-tools/kunit/usage.rst | 30 ++++++++-
arch/arm64/include/asm/asm-bug.h | 29 ++++++---
arch/arm64/include/asm/bug.h | 8 ++-
arch/loongarch/include/asm/bug.h | 38 ++++++++----
arch/parisc/include/asm/bug.h | 29 ++++++---
arch/powerpc/include/asm/bug.h | 37 +++++++++---
arch/riscv/include/asm/bug.h | 38 ++++++++----
arch/s390/include/asm/bug.h | 17 +++++-
arch/sh/include/asm/bug.h | 28 +++++++--
arch/x86/include/asm/bug.h | 21 +++++--
drivers/gpu/drm/tests/drm_rect_test.c | 16 +++++
include/asm-generic/bug.h | 16 ++++-
include/kunit/bug.h | 56 +++++++++++++++++
include/kunit/test.h | 1 +
include/linux/bug.h | 13 ++++
lib/bug.c | 51 ++++++++++++++--
lib/kunit/Kconfig | 9 +++
lib/kunit/Makefile | 7 ++-
lib/kunit/backtrace-suppression-test.c | 104 ++++++++++++++++++++++++++++++++
lib/kunit/bug.c | 42 +++++++++++++
net/core/dev_addr_lists_test.c | 6 ++
21 files changed, 524 insertions(+), 72 deletions(-)
create mode 100644 include/kunit/bug.h
create mode 100644 lib/kunit/backtrace-suppression-test.c
create mode 100644 lib/kunit/bug.c

Guenter Roeck

unread,
Apr 3, 2024, 9:19:45 AMApr 3
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, x...@kernel.org, Guenter Roeck, Linux Kernel Functional Testing
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.

Cc: Dan Carpenter <dan.ca...@linaro.org>
Cc: Daniel Diaz <danie...@linaro.org>
Cc: Naresh Kamboju <naresh....@linaro.org>
Cc: Kees Cook <kees...@chromium.org>
Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
Acked-by: Dan Carpenter <dan.ca...@linaro.org>
Reviewed-by: Kees Cook <kees...@chromium.org>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
v2:
- Rebased to v6.9-rc1
- Added Tested-by:, Acked-by:, and Reviewed-by: tags
- Added CONFIG_KUNIT_SUPPRESS_BACKTRACE configuration option,
enabled by default
v3:
- Rebased to v6.9-rc2

include/asm-generic/bug.h | 16 +++++++++---
include/kunit/bug.h | 51 +++++++++++++++++++++++++++++++++++++++
include/kunit/test.h | 1 +
include/linux/bug.h | 13 ++++++++++
lib/bug.c | 51 ++++++++++++++++++++++++++++++++++++---
lib/kunit/Kconfig | 9 +++++++
lib/kunit/Makefile | 6 +++--
lib/kunit/bug.c | 40 ++++++++++++++++++++++++++++++
8 files changed, 178 insertions(+), 9 deletions(-)
create mode 100644 include/kunit/bug.h
new file mode 100644
index 000000000000..bd0fe047572b
--- /dev/null
+++ b/include/kunit/bug.h
@@ -0,0 +1,51 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * KUnit helpers for backtrace suppression
+ *
+ * Copyright (c) 2024 Guenter Roeck <li...@roeck-us.net>
+ */
+
+#else /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */
+
diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile
index 309659a32a78..545b57c3be48 100644
--- a/lib/kunit/Makefile
+++ b/lib/kunit/Makefile
@@ -14,8 +14,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

diff --git a/lib/kunit/bug.c b/lib/kunit/bug.c
new file mode 100644
index 000000000000..f93544d7a9d1
--- /dev/null
+++ b/lib/kunit/bug.c
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * KUnit helpers for backtrace suppression
+ *
+ * Copyright (c) 2024 Guenter Roeck <li...@roeck-us.net>
+ */
+
+ if (!strcmp(function, warning->function))

Guenter Roeck

unread,
Apr 3, 2024, 9:19:47 AMApr 3
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, x...@kernel.org, Guenter Roeck, Linux Kernel Functional Testing
Count suppressed warning backtraces to enable code which suppresses
warning backtraces to check if the expected backtrace(s) have been
observed.

Using atomics for the backtrace count resulted in build errors on some
architectures due to include file recursion, so use a plain integer
for now.

Acked-by: Dan Carpenter <dan.ca...@linaro.org>
Reviewed-by: Kees Cook <kees...@chromium.org>
Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
v2:
- Rebased to v6.9-rc1
- Added Tested-by:, Acked-by:, and Reviewed-by: tags
- Introduced KUNIT_SUPPRESS_BACKTRACE configuration option
v3:
- Rebased to v6.9-rc2

- if (!strcmp(function, warning->function))
+ if (!strcmp(function, warning->function)) {

Guenter Roeck

unread,
Apr 3, 2024, 9:19:51 AMApr 3
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, x...@kernel.org, Guenter Roeck, Linux Kernel Functional Testing
Add unit tests to verify that warning backtrace suppression works.

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.

Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
Acked-by: Dan Carpenter <dan.ca...@linaro.org>
Reviewed-by: Kees Cook <kees...@chromium.org>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
v2:
- Rebased to v6.9-rc1
- Added Tested-by:, Acked-by:, and Reviewed-by: tags
- Introduced KUNIT_SUPPRESS_BACKTRACE configuration option
v3:
- Rebased to v6.9-rc2

lib/kunit/Makefile | 7 +-
lib/kunit/backtrace-suppression-test.c | 104 +++++++++++++++++++++++++
2 files changed, 109 insertions(+), 2 deletions(-)
create mode 100644 lib/kunit/backtrace-suppression-test.c

diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile
index 545b57c3be48..3eee1bd0ce5e 100644
--- a/lib/kunit/Makefile
+++ b/lib/kunit/Makefile
@@ -16,10 +16,13 @@ endif

# KUnit 'hooks' and bug handling are built-in even when KUnit is built
# as a module.
-obj-y += hooks.o \
- bug.o
+obj-y += hooks.o
+obj-$(CONFIG_KUNIT_SUPPRESS_BACKTRACE) += bug.o

obj-$(CONFIG_KUNIT_TEST) += kunit-test.o
+ifeq ($(CCONFIG_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)
diff --git a/lib/kunit/backtrace-suppression-test.c b/lib/kunit/backtrace-suppression-test.c
new file mode 100644
index 000000000000..47c619283802
--- /dev/null
+++ b/lib/kunit/backtrace-suppression-test.c
@@ -0,0 +1,104 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * KUnit test for suppressing warning tracebacks
+ *
+ * Copyright (C) 2024, Guenter Roeck
+ * Author: Guenter Roeck <li...@roeck-us.net>
+ */
+

Guenter Roeck

unread,
Apr 3, 2024, 9:19:51 AMApr 3
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, x...@kernel.org, Guenter Roeck, Linux Kernel Functional Testing
Document API functions for suppressing warning backtraces.

Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
Acked-by: Dan Carpenter <dan.ca...@linaro.org>
Reviewed-by: Kees Cook <kees...@chromium.org>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
v2:
- Rebased to v6.9-rc1
- Added Tested-by:, Acked-by:, and Reviewed-by: tags
v3:
- Rebased to v6.9-rc2

Guenter Roeck

unread,
Apr 3, 2024, 9:19:53 AMApr 3
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, x...@kernel.org, Guenter Roeck, Linux Kernel Functional Testing
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.

Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
Acked-by: Dan Carpenter <dan.ca...@linaro.org>
Acked-by: Maíra Canal <mca...@igalia.com>
Cc: Maarten Lankhorst <maarten....@linux.intel.com>
Cc: David Airlie <air...@gmail.com>
Cc: Daniel Vetter <dan...@ffwll.ch>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
v2:
- Rebased to v6.9-rc1
- Added Tested-by:, Acked-by:, and Reviewed-by: tags
v3:
- Rebased to v6.9-rc2

drivers/gpu/drm/tests/drm_rect_test.c | 16 ++++++++++++++++
1 file changed, 16 insertions(+)

diff --git a/drivers/gpu/drm/tests/drm_rect_test.c b/drivers/gpu/drm/tests/drm_rect_test.c
index 76332cd2ead8..66851769ee32 100644
--- a/drivers/gpu/drm/tests/drm_rect_test.c
+++ b/drivers/gpu/drm/tests/drm_rect_test.c
@@ -406,22 +406,38 @@ KUNIT_ARRAY_PARAM(drm_rect_scale, drm_rect_scale_cases, drm_rect_scale_case_desc

static void drm_test_rect_calc_hscale(struct kunit *test)
{
+ DEFINE_SUPPRESSED_WARNING(drm_calc_scale);
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.
+ */
+ START_SUPPRESSED_WARNING(drm_calc_scale);
scaling_factor = drm_rect_calc_hscale(&params->src, &params->dst,
params->min_range, params->max_range);
+ END_SUPPRESSED_WARNING(drm_calc_scale);

KUNIT_EXPECT_EQ(test, scaling_factor, params->expected_scaling_factor);
}

static void drm_test_rect_calc_vscale(struct kunit *test)
{
+ DEFINE_SUPPRESSED_WARNING(drm_calc_scale);
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.
+ */
+ START_SUPPRESSED_WARNING(drm_calc_scale);
scaling_factor = drm_rect_calc_vscale(&params->src, &params->dst,
params->min_range, params->max_range);
+ END_SUPPRESSED_WARNING(drm_calc_scale);

KUNIT_EXPECT_EQ(test, scaling_factor, params->expected_scaling_factor);
}
--
2.39.2

Guenter Roeck

unread,
Apr 3, 2024, 9:19:55 AMApr 3
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, x...@kernel.org, Guenter Roeck, Linux Kernel Functional Testing, Jakub Kicinski, Eric Dumazet
dev_addr_lists_test generates lock warning noise at the end of tests
if lock debugging is enabled. There are two sets of warnings.

WARNING: CPU: 0 PID: 689 at kernel/locking/mutex.c:923 __mutex_unlock_slowpath.constprop.0+0x13c/0x368
DEBUG_LOCKS_WARN_ON(__owner_task(owner) != __get_current())

WARNING: kunit_try_catch/1336 still has locks held!

KUnit test cleanup is not guaranteed to run in the same thread as the test
itself. For this test, this means that rtnl_lock() and rtnl_unlock() may
be called from different threads. This triggers the warnings.
Suppress the warnings because they are irrelevant for the test and just
confusing and distracting.

The first warning can be suppressed by using START_SUPPRESSED_WARNING()
and END_SUPPRESSED_WARNING() around the call to rtnl_unlock(). To suppress
the second warning, it is necessary to set debug_locks_silent while the
rtnl lock is held.

Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
Cc: David Gow <davi...@google.com>
Cc: Jakub Kicinski <ku...@kernel.org>
Cc: Eric Dumazet <edum...@google.com>
Acked-by: Dan Carpenter <dan.ca...@linaro.org>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
v2:
- Rebased to v6.9-rc1
- Added Tested-by:, Acked-by:, and Reviewed-by: tags
v3:
- Rebased to v6.9-rc2

net/core/dev_addr_lists_test.c | 6 ++++++
1 file changed, 6 insertions(+)

Guenter Roeck

unread,
Apr 3, 2024, 9:19:56 AMApr 3
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, x...@kernel.org, Guenter Roeck, Linux Kernel Functional Testing, Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen
Add name of functions triggering warning backtraces to the __bug_table
object section to enable support for suppressing WARNING backtraces.

To limit image size impact, the pointer to the function name is only added
to the __bug_table section if both CONFIG_KUNIT_SUPPRESS_BACKTRACE and
CONFIG_DEBUG_BUGVERBOSE are enabled. Otherwise, the __func__ assembly
parameter is replaced with a (dummy) NULL parameter to avoid an image size
increase due to unused __func__ entries (this is necessary because __func__
is not a define but a virtual variable).

Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
Acked-by: Dan Carpenter <dan.ca...@linaro.org>
Cc: Thomas Gleixner <tg...@linutronix.de>
Cc: Ingo Molnar <mi...@redhat.com>
Cc: Borislav Petkov <b...@alien8.de>
Cc: Dave Hansen <dave....@linux.intel.com>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
v2:
- Rebased to v6.9-rc1
- Added Tested-by:, Acked-by:, and Reviewed-by: tags
- Introduced KUNIT_SUPPRESS_BACKTRACE configuration option
v3:
- Rebased to v6.9-rc2

arch/x86/include/asm/bug.h | 21 ++++++++++++++++-----
1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/arch/x86/include/asm/bug.h b/arch/x86/include/asm/bug.h
index a3ec87d198ac..7698dfa74c98 100644
--- a/arch/x86/include/asm/bug.h
+++ b/arch/x86/include/asm/bug.h
@@ -23,18 +23,28 @@

#ifdef CONFIG_DEBUG_BUGVERBOSE

+#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE
+# define HAVE_BUG_FUNCTION
+# define __BUG_FUNC_PTR __BUG_REL(%c1)
+# define __BUG_FUNC __func__
+#else
+# define __BUG_FUNC_PTR
+# define __BUG_FUNC NULL
+#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */
+
#define _BUG_FLAGS(ins, flags, extra) \
do { \
asm_inline volatile("1:\t" ins "\n" \
".pushsection __bug_table,\"aw\"\n" \
"2:\t" __BUG_REL(1b) "\t# bug_entry::bug_addr\n" \
"\t" __BUG_REL(%c0) "\t# bug_entry::file\n" \
- "\t.word %c1" "\t# bug_entry::line\n" \
- "\t.word %c2" "\t# bug_entry::flags\n" \
- "\t.org 2b+%c3\n" \
+ "\t" __BUG_FUNC_PTR "\t# bug_entry::function\n" \
+ "\t.word %c2" "\t# bug_entry::line\n" \
+ "\t.word %c3" "\t# bug_entry::flags\n" \
+ "\t.org 2b+%c4\n" \
".popsection\n" \
extra \
- : : "i" (__FILE__), "i" (__LINE__), \
+ : : "i" (__FILE__), "i" (__BUG_FUNC), "i" (__LINE__),\
"i" (flags), \
"i" (sizeof(struct bug_entry))); \
} while (0)
@@ -80,7 +90,8 @@ do { \
do { \
__auto_type __flags = BUGFLAG_WARNING|(flags); \
instrumentation_begin(); \
- _BUG_FLAGS(ASM_UD2, __flags, ASM_REACHABLE); \
+ if (!IS_SUPPRESSED_WARNING(__func__)) \
+ _BUG_FLAGS(ASM_UD2, __flags, ASM_REACHABLE); \

Guenter Roeck

unread,
Apr 3, 2024, 9:19:58 AMApr 3
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, x...@kernel.org, Guenter Roeck, Linux Kernel Functional Testing, Catalin Marinas, Will Deacon
Add name of functions triggering warning backtraces to the __bug_table
object section to enable support for suppressing WARNING backtraces.

To limit image size impact, the pointer to the function name is only added
to the __bug_table section if both CONFIG_KUNIT_SUPPRESS_BACKTRACE and
CONFIG_DEBUG_BUGVERBOSE are enabled. Otherwise, the __func__ assembly
parameter is replaced with a (dummy) NULL parameter to avoid an image size
increase due to unused __func__ entries (this is necessary because __func__
is not a define but a virtual variable).

Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
Acked-by: Dan Carpenter <dan.ca...@linaro.org>
Cc: Catalin Marinas <catalin...@arm.com>
Cc: Will Deacon <wi...@kernel.org>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
v2:
- Rebased to v6.9-rc1
- Added Tested-by:, Acked-by:, and Reviewed-by: tags
- Introduced KUNIT_SUPPRESS_BACKTRACE configuration option
v3:
- Rebased to v6.9-rc2

arch/arm64/include/asm/asm-bug.h | 29 +++++++++++++++++++----------
arch/arm64/include/asm/bug.h | 8 +++++++-
2 files changed, 26 insertions(+), 11 deletions(-)

diff --git a/arch/arm64/include/asm/asm-bug.h b/arch/arm64/include/asm/asm-bug.h
index c762038ba400..c6d22e3cd840 100644
--- a/arch/arm64/include/asm/asm-bug.h
+++ b/arch/arm64/include/asm/asm-bug.h
@@ -8,36 +8,45 @@
#include <asm/brk-imm.h>

#ifdef CONFIG_DEBUG_BUGVERBOSE
-#define _BUGVERBOSE_LOCATION(file, line) __BUGVERBOSE_LOCATION(file, line)
-#define __BUGVERBOSE_LOCATION(file, line) \
+
+#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE
+# define HAVE_BUG_FUNCTION
+# define __BUG_FUNC_PTR(func) .long func - .;
+#else
+# define __BUG_FUNC_PTR(func)
+#endif
+
+#define _BUGVERBOSE_LOCATION(file, func, line) __BUGVERBOSE_LOCATION(file, func, line)
+#define __BUGVERBOSE_LOCATION(file, func, line) \
.pushsection .rodata.str,"aMS",@progbits,1; \
14472: .string file; \
.popsection; \
\
.long 14472b - .; \
+ __BUG_FUNC_PTR(func) \
.short line;
#else
-#define _BUGVERBOSE_LOCATION(file, line)
+#define _BUGVERBOSE_LOCATION(file, func, line)
#endif

#ifdef CONFIG_GENERIC_BUG

-#define __BUG_ENTRY(flags) \
+#define __BUG_ENTRY(flags, func) \
.pushsection __bug_table,"aw"; \
.align 2; \
14470: .long 14471f - .; \
-_BUGVERBOSE_LOCATION(__FILE__, __LINE__) \
- .short flags; \
+_BUGVERBOSE_LOCATION(__FILE__, func, __LINE__) \
+ .short flags; \
.popsection; \
14471:
#else
-#define __BUG_ENTRY(flags)
+#define __BUG_ENTRY(flags, func)
#endif

-#define ASM_BUG_FLAGS(flags) \
- __BUG_ENTRY(flags) \
+#define ASM_BUG_FLAGS(flags, func) \
+ __BUG_ENTRY(flags, func) \
brk BUG_BRK_IMM

-#define ASM_BUG() ASM_BUG_FLAGS(0)
+#define ASM_BUG() ASM_BUG_FLAGS(0, .)

#endif /* __ASM_ASM_BUG_H */
diff --git a/arch/arm64/include/asm/bug.h b/arch/arm64/include/asm/bug.h
index 28be048db3f6..044c5e24a17d 100644
--- a/arch/arm64/include/asm/bug.h
+++ b/arch/arm64/include/asm/bug.h
@@ -11,8 +11,14 @@

#include <asm/asm-bug.h>

+#ifdef HAVE_BUG_FUNCTION
+# define __BUG_FUNC __func__
+#else
+# define __BUG_FUNC NULL
+#endif
+

Guenter Roeck

unread,
Apr 3, 2024, 9:20:00 AMApr 3
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, x...@kernel.org, Guenter Roeck, Linux Kernel Functional Testing, Huacai Chen
Add name of functions triggering warning backtraces to the __bug_table
object section to enable support for suppressing WARNING backtraces.

To limit image size impact, the pointer to the function name is only added
to the __bug_table section if both CONFIG_KUNIT_SUPPRESS_BACKTRACE and
CONFIG_DEBUG_BUGVERBOSE are enabled. Otherwise, the __func__ assembly
parameter is replaced with a (dummy) NULL parameter to avoid an image size
increase due to unused __func__ entries (this is necessary because __func__
is not a define but a virtual variable).

Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
Acked-by: Dan Carpenter <dan.ca...@linaro.org>
Cc: Huacai Chen <chenh...@kernel.org>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
v2:
- Rebased to v6.9-rc1; resolved context conflict
- Added Tested-by:, Acked-by:, and Reviewed-by: tags
- Introduced KUNIT_SUPPRESS_BACKTRACE configuration option
v3:
- Rebased to v6.9-rc2; resolved context conflict

arch/loongarch/include/asm/bug.h | 38 +++++++++++++++++++++++---------
1 file changed, 27 insertions(+), 11 deletions(-)

diff --git a/arch/loongarch/include/asm/bug.h b/arch/loongarch/include/asm/bug.h
index 08388876ade4..193f396d81a0 100644
--- a/arch/loongarch/include/asm/bug.h
+++ b/arch/loongarch/include/asm/bug.h
@@ -3,47 +3,63 @@
#define __ASM_BUG_H

#include <asm/break.h>
+#include <kunit/bug.h>
#include <linux/stringify.h>

#ifndef CONFIG_DEBUG_BUGVERBOSE
-#define _BUGVERBOSE_LOCATION(file, line)
+#define _BUGVERBOSE_LOCATION(file, func, line)
#else
-#define __BUGVERBOSE_LOCATION(file, line) \
+#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE
+# define HAVE_BUG_FUNCTION
+# define __BUG_FUNC_PTR(func) .long func - .;
+#else
+# define __BUG_FUNC_PTR(func)
+#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */
+
+#define __BUGVERBOSE_LOCATION(file, func, line) \
.pushsection .rodata.str, "aMS", @progbits, 1; \
10002: .string file; \
.popsection; \
\
.long 10002b - .; \
+ __BUG_FUNC_PTR(func) \
.short line;
-#define _BUGVERBOSE_LOCATION(file, line) __BUGVERBOSE_LOCATION(file, line)
+#define _BUGVERBOSE_LOCATION(file, func, line) __BUGVERBOSE_LOCATION(file, func, line)
#endif

#ifndef CONFIG_GENERIC_BUG
-#define __BUG_ENTRY(flags)
+#define __BUG_ENTRY(flags, func)
#else
-#define __BUG_ENTRY(flags) \
+#define __BUG_ENTRY(flags, func) \
.pushsection __bug_table, "aw"; \
.align 2; \
10000: .long 10001f - .; \
- _BUGVERBOSE_LOCATION(__FILE__, __LINE__) \
+ _BUGVERBOSE_LOCATION(__FILE__, func, __LINE__) \
.short flags; \
.popsection; \
10001:
#endif

-#define ASM_BUG_FLAGS(flags) \
- __BUG_ENTRY(flags) \
+#define ASM_BUG_FLAGS(flags, func) \
+ __BUG_ENTRY(flags, func) \
break BRK_BUG

-#define ASM_BUG() ASM_BUG_FLAGS(0)
+#define ASM_BUG() ASM_BUG_FLAGS(0, .)
+
+#ifdef HAVE_BUG_FUNCTION
+# define __BUG_FUNC __func__
+#else
+# define __BUG_FUNC NULL
+#endif

#define __BUG_FLAGS(flags) \
- asm_inline volatile (__stringify(ASM_BUG_FLAGS(flags)));
+ asm_inline volatile (__stringify(ASM_BUG_FLAGS(flags, %0)) : : "i" (__BUG_FUNC));

#define __WARN_FLAGS(flags) \
do { \
instrumentation_begin(); \
- __BUG_FLAGS(BUGFLAG_WARNING|(flags)); \
+ if (!IS_SUPPRESSED_WARNING(__func__)) \
+ __BUG_FLAGS(BUGFLAG_WARNING|(flags)); \
annotate_reachable(); \

Guenter Roeck

unread,
Apr 3, 2024, 9:20:01 AMApr 3
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, x...@kernel.org, Guenter Roeck, Linux Kernel Functional Testing, Helge Deller
Add name of functions triggering warning backtraces to the __bug_table
object section to enable support for suppressing WARNING backtraces.

To limit image size impact, the pointer to the function name is only added
to the __bug_table section if both CONFIG_KUNIT_SUPPRESS_BACKTRACE and
CONFIG_DEBUG_BUGVERBOSE are enabled. Otherwise, the __func__ assembly
parameter is replaced with a (dummy) NULL parameter to avoid an image size
increase due to unused __func__ entries (this is necessary because __func__
is not a define but a virtual variable).

While at it, declare assembler parameters as constants where possible.
Refine .blockz instructions to calculate the necessary padding instead
of using fixed values.

Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
Acked-by: Dan Carpenter <dan.ca...@linaro.org>
Acked-by: Helge Deller <del...@gmx.de>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
v2:
- Rebased to v6.9-rc1
- Added Tested-by:, Acked-by:, and Reviewed-by: tags
- Introduced KUNIT_SUPPRESS_BACKTRACE configuration option
v3:
- Rebased to v6.9-rc2

arch/parisc/include/asm/bug.h | 29 +++++++++++++++++++++--------
1 file changed, 21 insertions(+), 8 deletions(-)

diff --git a/arch/parisc/include/asm/bug.h b/arch/parisc/include/asm/bug.h
index 833555f74ffa..b59c3f7380bf 100644
--- a/arch/parisc/include/asm/bug.h
+++ b/arch/parisc/include/asm/bug.h
@@ -23,8 +23,17 @@
# define __BUG_REL(val) ".word " __stringify(val)
#endif

-
#ifdef CONFIG_DEBUG_BUGVERBOSE
+
+#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE
+# define HAVE_BUG_FUNCTION
+# define __BUG_FUNC_PTR __BUG_REL(%c1)
+# define __BUG_FUNC __func__
+#else
+# define __BUG_FUNC_PTR
+# define __BUG_FUNC NULL
+#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */
+
#define BUG() \
do { \
asm volatile("\n" \
@@ -33,10 +42,12 @@
"\t.align 4\n" \
"2:\t" __BUG_REL(1b) "\n" \
"\t" __BUG_REL(%c0) "\n" \
- "\t.short %1, %2\n" \
- "\t.blockz %3-2*4-2*2\n" \
+ "\t" __BUG_FUNC_PTR "\n" \
+ "\t.short %c2, %c3\n" \
+ "\t.blockz %c4-(.-2b)\n" \
"\t.popsection" \
- : : "i" (__FILE__), "i" (__LINE__), \
+ : : "i" (__FILE__), "i" (__BUG_FUNC), \
+ "i" (__LINE__), \
"i" (0), "i" (sizeof(struct bug_entry)) ); \
unreachable(); \
} while(0)
@@ -58,10 +69,12 @@
"\t.align 4\n" \
"2:\t" __BUG_REL(1b) "\n" \
"\t" __BUG_REL(%c0) "\n" \
- "\t.short %1, %2\n" \
- "\t.blockz %3-2*4-2*2\n" \
+ "\t" __BUG_FUNC_PTR "\n" \
+ "\t.short %c2, %3\n" \
+ "\t.blockz %c4-(.-2b)\n" \
"\t.popsection" \
- : : "i" (__FILE__), "i" (__LINE__), \
+ : : "i" (__FILE__), "i" (__BUG_FUNC), \
+ "i" (__LINE__), \
"i" (BUGFLAG_WARNING|(flags)), \
"i" (sizeof(struct bug_entry)) ); \
} while(0)
@@ -74,7 +87,7 @@
"\t.align 4\n" \
"2:\t" __BUG_REL(1b) "\n" \
"\t.short %0\n" \
- "\t.blockz %1-4-2\n" \
+ "\t.blockz %c1-(.-2b)\n" \
"\t.popsection" \
: : "i" (BUGFLAG_WARNING|(flags)), \
"i" (sizeof(struct bug_entry)) ); \
--
2.39.2

Guenter Roeck

unread,
Apr 3, 2024, 9:20:03 AMApr 3
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, x...@kernel.org, Guenter Roeck, Linux Kernel Functional Testing, Heiko Carstens, Vasily Gorbik, Alexander Gordeev
Add name of functions triggering warning backtraces to the __bug_table
object section to enable support for suppressing WARNING backtraces.

To limit image size impact, the pointer to the function name is only added
to the __bug_table section if both CONFIG_KUNIT_SUPPRESS_BACKTRACE and
CONFIG_DEBUG_BUGVERBOSE are enabled. Otherwise, the __func__ assembly
parameter is replaced with a (dummy) NULL parameter to avoid an image size
increase due to unused __func__ entries (this is necessary because
__func__ is not a define but a virtual variable).

Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
Acked-by: Dan Carpenter <dan.ca...@linaro.org>
Cc: Heiko Carstens <h...@linux.ibm.com>
Cc: Vasily Gorbik <g...@linux.ibm.com>
Cc: Alexander Gordeev <agor...@linux.ibm.com>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
v2:
- Rebased to v6.9-rc1 (simplified assembler changes after upstream commit
3938490e78f4 ("s390/bug: remove entry size from __bug_table section")
- Added Tested-by:, Acked-by:, and Reviewed-by: tags
- Introduced KUNIT_SUPPRESS_BACKTRACE configuration option
v3:
- Rebased to v6.9-rc2

arch/s390/include/asm/bug.h | 17 ++++++++++++++---
1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/arch/s390/include/asm/bug.h b/arch/s390/include/asm/bug.h
index c500d45fb465..44d4e9f24ae0 100644
--- a/arch/s390/include/asm/bug.h
+++ b/arch/s390/include/asm/bug.h
@@ -8,6 +8,15 @@

#ifdef CONFIG_DEBUG_BUGVERBOSE

+#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE
+# define HAVE_BUG_FUNCTION
+# define __BUG_FUNC_PTR " .long %0-.\n"
+# define __BUG_FUNC __func__
+#else
+# define __BUG_FUNC_PTR
+# define __BUG_FUNC NULL
+#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */
+
#define __EMIT_BUG(x) do { \
asm_inline volatile( \
"0: mc 0,0\n" \
@@ -17,10 +26,12 @@
".section __bug_table,\"aw\"\n" \
"2: .long 0b-.\n" \
" .long 1b-.\n" \
- " .short %0,%1\n" \
- " .org 2b+%2\n" \
+ __BUG_FUNC_PTR \
+ " .short %1,%2\n" \
+ " .org 2b+%3\n" \
".previous\n" \
- : : "i" (__LINE__), \
+ : : "i" (__BUG_FUNC), \
+ "i" (__LINE__), \
"i" (x), \
"i" (sizeof(struct bug_entry))); \
} while (0)
--
2.39.2

Guenter Roeck

unread,
Apr 3, 2024, 9:20:04 AMApr 3
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, x...@kernel.org, Guenter Roeck, Linux Kernel Functional Testing, Yoshinori Sato, Rich Felker, John Paul Adrian Glaubitz
Add name of functions triggering warning backtraces to the __bug_table
object section to enable support for suppressing WARNING backtraces.

To limit image size impact, the pointer to the function name is only added
to the __bug_table section if both CONFIG_KUNIT_SUPPRESS_BACKTRACE and
CONFIG_DEBUG_BUGVERBOSE are enabled. Otherwise, the __func__ assembly
parameter is replaced with a (dummy) NULL parameter to avoid an image size
increase due to unused __func__ entries (this is necessary because __func__
is not a define but a virtual variable).

Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
Acked-by: Dan Carpenter <dan.ca...@linaro.org>
Cc: Yoshinori Sato <ys...@users.sourceforge.jp>
Cc: Rich Felker <dal...@libc.org>
Cc: John Paul Adrian Glaubitz <glau...@physik.fu-berlin.de>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
v2:
- Rebased to v6.9-rc1
- Added Tested-by:, Acked-by:, and Reviewed-by: tags
- Introduced KUNIT_SUPPRESS_BACKTRACE configuration option
v3:
- Rebased to v6.9-rc2

arch/sh/include/asm/bug.h | 26 ++++++++++++++++++++++----
1 file changed, 22 insertions(+), 4 deletions(-)

diff --git a/arch/sh/include/asm/bug.h b/arch/sh/include/asm/bug.h
index 05a485c4fabc..470ce6567d20 100644
--- a/arch/sh/include/asm/bug.h
+++ b/arch/sh/include/asm/bug.h
@@ -24,21 +24,36 @@
* The offending file and line are encoded in the __bug_table section.
*/
#ifdef CONFIG_DEBUG_BUGVERBOSE
+
+#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE
+# define HAVE_BUG_FUNCTION
+# define __BUG_FUNC_PTR "\t.long %O2\n"
+#else
+# define __BUG_FUNC_PTR
+#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */
+
#define _EMIT_BUG_ENTRY \
"\t.pushsection __bug_table,\"aw\"\n" \
"2:\t.long 1b, %O1\n" \
- "\t.short %O2, %O3\n" \
- "\t.org 2b+%O4\n" \
+ __BUG_FUNC_PTR \
+ "\t.short %O3, %O4\n" \
+ "\t.org 2b+%O5\n" \
"\t.popsection\n"
#else
#define _EMIT_BUG_ENTRY \
"\t.pushsection __bug_table,\"aw\"\n" \
"2:\t.long 1b\n" \
- "\t.short %O3\n" \
- "\t.org 2b+%O4\n" \
+ "\t.short %O4\n" \
+ "\t.org 2b+%O5\n" \
"\t.popsection\n"
#endif

+#ifdef HAVE_BUG_FUNCTION
+# define __BUG_FUNC __func__
+#else
+# define __BUG_FUNC NULL
+#endif
+
#define BUG() \
do { \
__asm__ __volatile__ ( \
@@ -47,6 +62,7 @@ do { \
: \
: "n" (TRAPA_BUG_OPCODE), \
"i" (__FILE__), \
+ "i" (__BUG_FUNC), \
"i" (__LINE__), "i" (0), \
"i" (sizeof(struct bug_entry))); \
unreachable(); \
@@ -60,6 +76,7 @@ do { \
: \
: "n" (TRAPA_BUG_OPCODE), \
"i" (__FILE__), \
+ "i" (__BUG_FUNC), \
"i" (__LINE__), \
"i" (BUGFLAG_WARNING|(flags)), \
"i" (sizeof(struct bug_entry))); \
@@ -85,6 +102,7 @@ do { \
: \
: "n" (TRAPA_BUG_OPCODE), \
"i" (__FILE__), \
+ "i" (__BUG_FUNC), \
"i" (__LINE__), \

Guenter Roeck

unread,
Apr 3, 2024, 9:20:07 AMApr 3
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, x...@kernel.org, Guenter Roeck, Simon Horman, Yoshinori Sato, Rich Felker, John Paul Adrian Glaubitz
Declaring the defines needed for suppressing warning inside
'#ifdef CONFIG_DEBUG_BUGVERBOSE' results in a kerneldoc warning.

.../bug.h:29: warning: expecting prototype for _EMIT_BUG_ENTRY().
Prototype was for HAVE_BUG_FUNCTION() instead

Move the defines above the kerneldoc entry for _EMIT_BUG_ENTRY
to make kerneldoc happy.

Reported-by: Simon Horman <ho...@kernel.org>
Cc: Simon Horman <ho...@kernel.org>
Cc: Yoshinori Sato <ys...@users.sourceforge.jp>
Cc: Rich Felker <dal...@libc.org>
Cc: John Paul Adrian Glaubitz <glau...@physik.fu-berlin.de>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
v3: Added patch. Possibly squash into previous patch.

arch/sh/include/asm/bug.h | 16 +++++++++-------
1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/arch/sh/include/asm/bug.h b/arch/sh/include/asm/bug.h
index 470ce6567d20..bf4947d51d69 100644
--- a/arch/sh/include/asm/bug.h
+++ b/arch/sh/include/asm/bug.h
@@ -11,6 +11,15 @@
#define HAVE_ARCH_BUG
#define HAVE_ARCH_WARN_ON

+#ifdef CONFIG_DEBUG_BUGVERBOSE
+#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE
+# define HAVE_BUG_FUNCTION
+# define __BUG_FUNC_PTR "\t.long %O2\n"
+#else
+# define __BUG_FUNC_PTR
+#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */
+#endif /* CONFIG_DEBUG_BUGVERBOSE */
+
/**
* _EMIT_BUG_ENTRY
* %1 - __FILE__
@@ -25,13 +34,6 @@
*/
#ifdef CONFIG_DEBUG_BUGVERBOSE

-#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE
-# define HAVE_BUG_FUNCTION
-# define __BUG_FUNC_PTR "\t.long %O2\n"
-#else
-# define __BUG_FUNC_PTR
-#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */
-
#define _EMIT_BUG_ENTRY \
"\t.pushsection __bug_table,\"aw\"\n" \
"2:\t.long 1b, %O1\n" \
--
2.39.2

Guenter Roeck

unread,
Apr 3, 2024, 9:20:07 AMApr 3
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, x...@kernel.org, Guenter Roeck, Linux Kernel Functional Testing, Paul Walmsley, Palmer Dabbelt, Albert Ou
Add name of functions triggering warning backtraces to the __bug_table
object section to enable support for suppressing WARNING backtraces.

To limit image size impact, the pointer to the function name is only added
to the __bug_table section if both CONFIG_KUNIT_SUPPRESS_BACKTRACE and
CONFIG_DEBUG_BUGVERBOSE are enabled. Otherwise, the __func__ assembly
parameter is replaced with a (dummy) NULL parameter to avoid an image size
increase due to unused __func__ entries (this is necessary because __func__
is not a define but a virtual variable).

To simplify the implementation, unify the __BUG_ENTRY_ADDR and
__BUG_ENTRY_FILE macros into a single macro named __BUG_REL() which takes
the address, file, or function reference as parameter.

Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
Acked-by: Dan Carpenter <dan.ca...@linaro.org>
Cc: Paul Walmsley <paul.w...@sifive.com>
Cc: Palmer Dabbelt <pal...@dabbelt.com>
Cc: Albert Ou <a...@eecs.berkeley.edu>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
v2:
- Rebased to v6.9-rc1
- Added Tested-by:, Acked-by:, and Reviewed-by: tags
- Introduced KUNIT_SUPPRESS_BACKTRACE configuration option
v3:
- Rebased to v6.9-rc2

arch/riscv/include/asm/bug.h | 38 ++++++++++++++++++++++++------------
1 file changed, 26 insertions(+), 12 deletions(-)

diff --git a/arch/riscv/include/asm/bug.h b/arch/riscv/include/asm/bug.h
index 1aaea81fb141..79f360af4ad8 100644
--- a/arch/riscv/include/asm/bug.h
+++ b/arch/riscv/include/asm/bug.h
@@ -30,26 +30,39 @@
typedef u32 bug_insn_t;

#ifdef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
-#define __BUG_ENTRY_ADDR RISCV_INT " 1b - ."
-#define __BUG_ENTRY_FILE RISCV_INT " %0 - ."
+#define __BUG_REL(val) RISCV_INT " " __stringify(val) " - ."
#else
-#define __BUG_ENTRY_ADDR RISCV_PTR " 1b"
-#define __BUG_ENTRY_FILE RISCV_PTR " %0"
+#define __BUG_REL(val) RISCV_PTR " " __stringify(val)
#endif

#ifdef CONFIG_DEBUG_BUGVERBOSE
+
+#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE
+# define HAVE_BUG_FUNCTION
+# define __BUG_FUNC_PTR __BUG_REL(%1)
+#else
+# define __BUG_FUNC_PTR
+#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */
+
#define __BUG_ENTRY \
- __BUG_ENTRY_ADDR "\n\t" \
- __BUG_ENTRY_FILE "\n\t" \
- RISCV_SHORT " %1\n\t" \
- RISCV_SHORT " %2"
+ __BUG_REL(1b) "\n\t" \
+ __BUG_REL(%0) "\n\t" \
+ __BUG_FUNC_PTR "\n\t" \
+ RISCV_SHORT " %2\n\t" \
+ RISCV_SHORT " %3"
#else
#define __BUG_ENTRY \
- __BUG_ENTRY_ADDR "\n\t" \
- RISCV_SHORT " %2"
+ __BUG_REL(1b) "\n\t" \
+ RISCV_SHORT " %3"
#endif

#ifdef CONFIG_GENERIC_BUG
+#ifdef HAVE_BUG_FUNCTION
+# define __BUG_FUNC __func__
+#else
+# define __BUG_FUNC NULL
+#endif
+
#define __BUG_FLAGS(flags) \
do { \
__asm__ __volatile__ ( \
@@ -58,10 +71,11 @@ do { \
".pushsection __bug_table,\"aw\"\n\t" \
"2:\n\t" \
__BUG_ENTRY "\n\t" \
- ".org 2b + %3\n\t" \
+ ".org 2b + %4\n\t" \
".popsection" \
: \
- : "i" (__FILE__), "i" (__LINE__), \
+ : "i" (__FILE__), "i" (__BUG_FUNC), \
+ "i" (__LINE__), \
"i" (flags), \

Guenter Roeck

unread,
Apr 3, 2024, 9:20:09 AMApr 3
to linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, x...@kernel.org, Guenter Roeck, Linux Kernel Functional Testing, Michael Ellerman
Add name of functions triggering warning backtraces to the __bug_table
object section to enable support for suppressing WARNING backtraces.

To limit image size impact, the pointer to the function name is only added
to the __bug_table section if both CONFIG_KUNIT_SUPPRESS_BACKTRACE and
CONFIG_DEBUG_BUGVERBOSE are enabled. Otherwise, the __func__ assembly
parameter is replaced with a (dummy) NULL parameter to avoid an image size
increase due to unused __func__ entries (this is necessary because __func__
is not a define but a virtual variable).

Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
Acked-by: Dan Carpenter <dan.ca...@linaro.org>
Cc: Michael Ellerman <m...@ellerman.id.au>
Signed-off-by: Guenter Roeck <li...@roeck-us.net>
---
v2:
- Rebased to v6.9-rc1
- Added Tested-by:, Acked-by:, and Reviewed-by: tags
- Introduced KUNIT_SUPPRESS_BACKTRACE configuration option
v3:
- Rebased to v6.9-rc2

arch/powerpc/include/asm/bug.h | 37 +++++++++++++++++++++++++---------
1 file changed, 28 insertions(+), 9 deletions(-)

diff --git a/arch/powerpc/include/asm/bug.h b/arch/powerpc/include/asm/bug.h
index 1db485aacbd9..5b06745d20aa 100644
--- a/arch/powerpc/include/asm/bug.h
+++ b/arch/powerpc/include/asm/bug.h
@@ -14,6 +14,9 @@
.section __bug_table,"aw"
5001: .4byte \addr - .
.4byte 5002f - .
+#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE
+ .4byte 0
+#endif
.short \line, \flags
.org 5001b+BUG_ENTRY_SIZE
.previous
@@ -32,30 +35,46 @@
#endif /* verbose */

#else /* !__ASSEMBLY__ */
-/* _EMIT_BUG_ENTRY expects args %0,%1,%2,%3 to be FILE, LINE, flags and
- sizeof(struct bug_entry), respectively */
+/* _EMIT_BUG_ENTRY expects args %0,%1,%2,%3,%4 to be FILE, __func__, LINE, flags
+ and sizeof(struct bug_entry), respectively */
#ifdef CONFIG_DEBUG_BUGVERBOSE
+
+#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE
+# define HAVE_BUG_FUNCTION
+# define __BUG_FUNC_PTR " .4byte %1 - .\n"
+#else
+# define __BUG_FUNC_PTR
+#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */
+
#define _EMIT_BUG_ENTRY \
".section __bug_table,\"aw\"\n" \
"2: .4byte 1b - .\n" \
" .4byte %0 - .\n" \
- " .short %1, %2\n" \
- ".org 2b+%3\n" \
+ __BUG_FUNC_PTR \
+ " .short %2, %3\n" \
+ ".org 2b+%4\n" \
".previous\n"
#else
#define _EMIT_BUG_ENTRY \
".section __bug_table,\"aw\"\n" \
"2: .4byte 1b - .\n" \
- " .short %2\n" \
- ".org 2b+%3\n" \
+ " .short %3\n" \
+ ".org 2b+%4\n" \
".previous\n"
#endif

+#ifdef HAVE_BUG_FUNCTION
+# define __BUG_FUNC __func__
+#else
+# define __BUG_FUNC NULL
+#endif
+
#define BUG_ENTRY(insn, flags, ...) \
__asm__ __volatile__( \
"1: " insn "\n" \
_EMIT_BUG_ENTRY \
- : : "i" (__FILE__), "i" (__LINE__), \
+ : : "i" (__FILE__), "i" (__BUG_FUNC), \
+ "i" (__LINE__), \
"i" (flags), \

Kees Cook

unread,
Apr 3, 2024, 5:20:37 PMApr 3
to Guenter Roeck, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, x...@kernel.org
Tested-by: Kees Cook <kees...@chromium.org>

(for x86 and um)

I was planning to add warning suppression for the "overflow" KUnit
tests, but it seems the vmalloc routines aren't calling warn_alloc() any
more for impossible sizes. So, I think, no patches needed for
lib/overflow_kunit.c, but at the end of the day, I've tested this series
is working for me. :P

--
Kees Cook

Jakub Kicinski

unread,
Apr 3, 2024, 9:34:18 PMApr 3
to Guenter Roeck, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, x...@kernel.org, Linux Kernel Functional Testing, Eric Dumazet
On Wed, 3 Apr 2024 06:19:27 -0700 Guenter Roeck wrote:
> dev_addr_lists_test generates lock warning noise at the end of tests
> if lock debugging is enabled. There are two sets of warnings.
>
> WARNING: CPU: 0 PID: 689 at kernel/locking/mutex.c:923 __mutex_unlock_slowpath.constprop.0+0x13c/0x368
> DEBUG_LOCKS_WARN_ON(__owner_task(owner) != __get_current())
>
> WARNING: kunit_try_catch/1336 still has locks held!
>
> KUnit test cleanup is not guaranteed to run in the same thread as the test
> itself. For this test, this means that rtnl_lock() and rtnl_unlock() may
> be called from different threads. This triggers the warnings.
> Suppress the warnings because they are irrelevant for the test and just
> confusing and distracting.
>
> The first warning can be suppressed by using START_SUPPRESSED_WARNING()
> and END_SUPPRESSED_WARNING() around the call to rtnl_unlock(). To suppress
> the second warning, it is necessary to set debug_locks_silent while the
> rtnl lock is held.

Is it okay if I move the locking into the tests, instead?
It's only 4 lines more and no magic required, seems to work fine.

Michael Ellerman

unread,
Apr 3, 2024, 10:14:30 PMApr 3
to Guenter Roeck, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, x...@kernel.org, Guenter Roeck, Linux Kernel Functional Testing
Guenter Roeck <li...@roeck-us.net> writes:
> Add name of functions triggering warning backtraces to the __bug_table
> object section to enable support for suppressing WARNING backtraces.
>
> To limit image size impact, the pointer to the function name is only added
> to the __bug_table section if both CONFIG_KUNIT_SUPPRESS_BACKTRACE and
> CONFIG_DEBUG_BUGVERBOSE are enabled. Otherwise, the __func__ assembly
> parameter is replaced with a (dummy) NULL parameter to avoid an image size
> increase due to unused __func__ entries (this is necessary because __func__
> is not a define but a virtual variable).
>
> Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
> Acked-by: Dan Carpenter <dan.ca...@linaro.org>
> Cc: Michael Ellerman <m...@ellerman.id.au>
> Signed-off-by: Guenter Roeck <li...@roeck-us.net>
> ---
> v2:
> - Rebased to v6.9-rc1
> - Added Tested-by:, Acked-by:, and Reviewed-by: tags
> - Introduced KUNIT_SUPPRESS_BACKTRACE configuration option
> v3:
> - Rebased to v6.9-rc2
>
> arch/powerpc/include/asm/bug.h | 37 +++++++++++++++++++++++++---------
> 1 file changed, 28 insertions(+), 9 deletions(-)

I ran it through some build and boot tests, LGTM.

Acked-by: Michael Ellerman <m...@ellerman.id.au> (powerpc)

cheers

Simon Horman

unread,
Apr 5, 2024, 2:31:15 PMApr 5
to Guenter Roeck, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, x...@kernel.org, Yoshinori Sato, Rich Felker, John Paul Adrian Glaubitz
On Wed, Apr 03, 2024 at 06:19:34AM -0700, Guenter Roeck wrote:
> Declaring the defines needed for suppressing warning inside
> '#ifdef CONFIG_DEBUG_BUGVERBOSE' results in a kerneldoc warning.
>
> .../bug.h:29: warning: expecting prototype for _EMIT_BUG_ENTRY().
> Prototype was for HAVE_BUG_FUNCTION() instead
>
> Move the defines above the kerneldoc entry for _EMIT_BUG_ENTRY
> to make kerneldoc happy.
>
> Reported-by: Simon Horman <ho...@kernel.org>
> Cc: Simon Horman <ho...@kernel.org>
> Cc: Yoshinori Sato <ys...@users.sourceforge.jp>
> Cc: Rich Felker <dal...@libc.org>
> Cc: John Paul Adrian Glaubitz <glau...@physik.fu-berlin.de>
> Signed-off-by: Guenter Roeck <li...@roeck-us.net>
> ---
> v3: Added patch. Possibly squash into previous patch.

FWIIW, this looks good to me.

Guenter Roeck

unread,
Apr 8, 2024, 12:34:34 PMApr 8
to Jakub Kicinski, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, x...@kernel.org, Linux Kernel Functional Testing, Eric Dumazet
I don't think it is that simple. Unit tests run in a kernel thread
and exit immediately if a test fails. While the unit test code _looks_
sequential, that isn't really the case. Every instance of KUNIT_ASSERT_x
or KUNIT_FAIL() results in immediate kernel thread termination. If
that happens, any rtnl_unlock() in the failed function would not be
executed. I am not aware of an equivalent of atexit() for kernel threads
which would fix the problem. My understanding is that the kunit system
doesn't support an equivalent either, but at least sometimes executes
the exit function in a different thread context.

Guenter

David Gow

unread,
Apr 9, 2024, 4:29:44 AMApr 9
to Guenter Roeck, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, x...@kernel.org, Linux Kernel Functional Testing
Sorry it took so long to get to this.

I love the idea, we've needed this for a while.

There are some downsides to this being entirely based on the name of
the function which contains WARN(). Partly because there could be
several WARN()s within a function, and there'd be overlap, and partly
because the function name is never actually printed during a warning
(it may come from the stack trace, but that can be misleading with
inlined functions). I don't think either of these are showstoppers,
though, but it'd be nice to extend this in the future with (a) other
ways of identifying warnings, such as the format string, and (b) print
the function name in the report, if it's present. The function name is
probably a good middle ground, complexity-wise, though, so I'm happy
to have it thus far.

I also think we're missing some opportunities to integrate this
better with existing KUnit infrastructure, like the
action/resource/cleanup system. In particular, it'd be nice to have a
way of ensuring that suppressions won't get leaked if the test aborts
between START_SUPPRESSED_WARNING() and END_SUPPRESSED_WARNING(). It's
not difficult to use this as-is, but it'd be nice to have some
helpers, rather than having to, for instance:
KUNIT_DEFINE_ACTION_WRAPPER(kunit_stop_suppressing_warning,
__end_suppress_warning, struct __suppressed_warning *);
DEFINE_SUPPRESSED_WARNING(vfree);
START_SUPPRESSED_WARNING(vfree);
kunit_add_action(test, kunit_stop_suppressing_warning, (void
*)&__kunit_suppress_vfree);

(With the note that the DEFINE_SUPPRESSED_WARNING() will have to be
global, or put on the heap, lest it become a dangling pointer by the
time the suppression has stopped.)

Equally, do we want to make the
__{start,end,is}_suppress[ed]_warning() functions KUnit 'hooks'? This
would allow them to be used in modules which don't depend directly on
KUnit. I suspect it's not important in this case: but worth keeping in
mind in case we find a situation where we'd need to suppress a warning
elsewhere.

These are all things which could be added/changed in follow-up
patches, though, so I don't think they're blockers. Otherwise, this
looks good: perhaps the naming could be a bit more consistent with
other KUnit things, but that depends on how much we want this to be 'a
part of KUnit' versus an independent bit of functionality.
Do we want to call these '__kunit_start_suppress_warning', etc., to
match other similar functions exported by KUnit to be used in macros,
et al.

> +
> +#define DEFINE_SUPPRESSED_WARNING(func) \
> + struct __suppressed_warning __kunit_suppress_##func = \

We use the __kunit_ prefix here...

> + { .function = __stringify(func) }
> +
> +#define START_SUPPRESSED_WARNING(func) \
> + __start_suppress_warning(&__kunit_suppress_##func)
> +
> +#define END_SUPPRESSED_WARNING(func) \
> + __end_suppress_warning(&__kunit_suppress_##func)
> +
> +#define IS_SUPPRESSED_WARNING(func) \
> + __is_suppressed_warning(func)
> +

Similarly, do we want to give these KUNIT_ prefixes to match other KUnit macros.

One possibility would be to have both KUNIT_- and non-KUNIT_-
variants, the latter of which accepts a struct kunit*, and registers
the suppression with the test for automated cleanup.
As mentioned, I'd love to see the function plumbed through and
reported some day, both to make it easier to know what to suppress,
and also because it's possibly more reliable even outside the
suppression use-case. Could be a follow-up patch later, though.
Thanks,
-- David

David Gow

unread,
Apr 9, 2024, 4:29:50 AMApr 9
to Guenter Roeck, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, x...@kernel.org, Linux Kernel Functional Testing
On Wed, 3 Apr 2024 at 21:19, Guenter Roeck <li...@roeck-us.net> wrote:
>
> Count suppressed warning backtraces to enable code which suppresses
> warning backtraces to check if the expected backtrace(s) have been
> observed.
>
> Using atomics for the backtrace count resulted in build errors on some
> architectures due to include file recursion, so use a plain integer
> for now.
>
> Acked-by: Dan Carpenter <dan.ca...@linaro.org>
> Reviewed-by: Kees Cook <kees...@chromium.org>
> Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
> Signed-off-by: Guenter Roeck <li...@roeck-us.net>
> ---

Looks good to me, thanks.

Reviewed-by: David Gow <davi...@google.com>

Cheers,
-- David
> --
> You received this message because you are subscribed to the Google Groups "KUnit Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20240403131936.787234-3-linux%40roeck-us.net.

David Gow

unread,
Apr 9, 2024, 4:29:57 AMApr 9
to Guenter Roeck, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, x...@kernel.org, Linux Kernel Functional Testing
On Wed, 3 Apr 2024 at 21:19, Guenter Roeck <li...@roeck-us.net> wrote:
>
> Add unit tests to verify that warning backtrace suppression works.
>
> 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.
>
> Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
> Acked-by: Dan Carpenter <dan.ca...@linaro.org>
> Reviewed-by: Kees Cook <kees...@chromium.org>
> Signed-off-by: Guenter Roeck <li...@roeck-us.net>
> ---

There's a typo in the Makefile, which stops this from being built at
all. Otherwise, it seems good to me.

-- David

> v2:
> - Rebased to v6.9-rc1
> - Added Tested-by:, Acked-by:, and Reviewed-by: tags
> - Introduced KUNIT_SUPPRESS_BACKTRACE configuration option
> v3:
> - Rebased to v6.9-rc2
>
> lib/kunit/Makefile | 7 +-
> lib/kunit/backtrace-suppression-test.c | 104 +++++++++++++++++++++++++
> 2 files changed, 109 insertions(+), 2 deletions(-)
> create mode 100644 lib/kunit/backtrace-suppression-test.c
>
> diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile
> index 545b57c3be48..3eee1bd0ce5e 100644
> --- a/lib/kunit/Makefile
> +++ b/lib/kunit/Makefile
> @@ -16,10 +16,13 @@ endif
>
> # KUnit 'hooks' and bug handling are built-in even when KUnit is built
> # as a module.
> -obj-y += hooks.o \
> - bug.o
> +obj-y += hooks.o
> +obj-$(CONFIG_KUNIT_SUPPRESS_BACKTRACE) += bug.o
>
> obj-$(CONFIG_KUNIT_TEST) += kunit-test.o
> +ifeq ($(CCONFIG_KUNIT_SUPPRESS_BACKTRACE),y)

s/CCONFIG_/CONFIG_/ ?

David Gow

unread,
Apr 9, 2024, 4:30:03 AMApr 9
to Guenter Roeck, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, x...@kernel.org, Linux Kernel Functional Testing
On Wed, 3 Apr 2024 at 21:19, Guenter Roeck <li...@roeck-us.net> wrote:
>
> Document API functions for suppressing warning backtraces.
>
> Tested-by: Linux Kernel Functional Testing <lk...@linaro.org>
> Acked-by: Dan Carpenter <dan.ca...@linaro.org>
> Reviewed-by: Kees Cook <kees...@chromium.org>
> Signed-off-by: Guenter Roeck <li...@roeck-us.net>
> ---

This looks good to me: thanks for adding the documentation!

If we add integration between this and the KUnit resource system,
we'll need to add that to this documentation.

I wonder if it would make sense to have an example where the
DEFINE_SUPPRESSED_WARNING() is global, e.g., in the test init/exit
functions. That might overcomplicate it a bit.

It also might be nice to document the individual macros with kerneldoc
comments. (Though, that could equally fit in patch #1).

Still, this is the most important bit, so I'm happy to have it as-is.

Reviewed-by: David Gow <davi...@google.com>

Cheers,
-- David


Guenter Roeck

unread,
Apr 9, 2024, 2:11:07 PMApr 9
to David Gow, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, x...@kernel.org, Linux Kernel Functional Testing
On Tue, Apr 09, 2024 at 04:29:42PM +0800, David Gow wrote:
> > +ifeq ($(CCONFIG_KUNIT_SUPPRESS_BACKTRACE),y)
>
> s/CCONFIG_/CONFIG_/ ?
>
>
Odd, I know I tested this (and it still works ;-).
The additional "C" must have slipped in at some point.
Thanks for noticing!

Guenter

Charlie Jenkins

unread,
Apr 22, 2024, 1:40:59 PMApr 22
to Guenter Roeck, linux-k...@vger.kernel.org, David Airlie, Arnd Bergmann, Maíra Canal, Dan Carpenter, Kees Cook, Daniel Diaz, David Gow, Arthur Grillo, Brendan Higgins, Naresh Kamboju, Maarten Lankhorst, Andrew Morton, Maxime Ripard, Ville Syrjälä, Daniel Vetter, Thomas Zimmermann, dri-...@lists.freedesktop.org, kuni...@googlegroups.com, linux...@vger.kernel.org, linux-ar...@lists.infradead.org, linu...@vger.kernel.org, linux-...@vger.kernel.org, linux-...@vger.kernel.org, linuxp...@lists.ozlabs.org, linux...@lists.infradead.org, linux...@vger.kernel.org, linu...@vger.kernel.org, loon...@lists.linux.dev, net...@vger.kernel.org, x...@kernel.org, Linux Kernel Functional Testing, Paul Walmsley, Palmer Dabbelt, Albert Ou
> _______________________________________________
> linux-riscv mailing list
> linux...@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-riscv

Reviewed-by: Charlie Jenkins <cha...@rivosinc.com>

- Charlie

Reply all
Reply to author
Forward
0 new messages