linux-kselftest.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alessandro Carminati <acarmina@redhat.com>
To: linux-kselftest@vger.kernel.org
Cc: Dan Carpenter <dan.carpenter@linaro.org>,
	Kees Cook <keescook@chromium.org>,
	Daniel Diaz <daniel.diaz@linaro.org>,
	David Gow <davidgow@google.com>,
	Arthur Grillo <arthurgrillo@riseup.net>,
	Brendan Higgins <brendan.higgins@linux.dev>,
	Naresh Kamboju <naresh.kamboju@linaro.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Maxime Ripard <mripard@kernel.org>,
	Ville Syrjala <ville.syrjala@linux.intel.com>,
	Daniel Vetter <daniel@ffwll.ch>,
	Guenter Roeck <linux@roeck-us.net>,
	Alessandro Carminati <alessandro.carminati@gmail.com>,
	Jani Nikula <jani.nikula@intel.com>,
	Jeff Johnson <jeff.johnson@oss.qualcomm.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Josh Poimboeuf <jpoimboe@kernel.org>,
	Shuah Khan <skhan@linuxfoundation.org>,
	Linux Kernel Functional Testing <lkft@linaro.org>,
	dri-devel@lists.freedesktop.org, kunit-dev@googlegroups.com,
	linux-kernel@vger.kernel.org,
	Alessandro Carminati <acarmina@redhat.com>
Subject: [PATCH v5 2/5] bug/kunit: Suppressing warning backtraces reduced impact on WARN*() sites
Date: Mon, 26 May 2025 13:27:52 +0000	[thread overview]
Message-ID: <20250526132755.166150-3-acarmina@redhat.com> (raw)
In-Reply-To: <20250526132755.166150-1-acarmina@redhat.com>

KUnit support is not consistently present across distributions, some
include it in their stock kernels, while others do not.
While both KUNIT and KUNIT_SUPPRESS_BACKTRACE can be considered debug
features, the fact that some distros ship with KUnit enabled means it's
important to minimize the runtime impact of this patch.
To that end, this patch introduces a counter for the number of
suppressed symbols and skips execution of __kunit_is_suppressed_warning()
entirely when no symbols are currently being suppressed.

Signed-off-by: Alessandro Carminati <acarmina@redhat.com>
---
 include/asm-generic/bug.h | 21 ++++++++++++++-------
 include/kunit/bug.h       |  1 +
 lib/kunit/bug.c           |  4 ++++
 3 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/include/asm-generic/bug.h b/include/asm-generic/bug.h
index 3cc8cb100ccd..c5587820bd8c 100644
--- a/include/asm-generic/bug.h
+++ b/include/asm-generic/bug.h
@@ -62,7 +62,8 @@ struct bug_entry {
  */
 #ifndef HAVE_ARCH_BUG
 #define BUG() do { \
-	if (!KUNIT_IS_SUPPRESSED_WARNING(__func__)) {			\
+	if (suppressed_symbols_cnt &&					\
+	    !KUNIT_IS_SUPPRESSED_WARNING(__func__)) {			\
 		printk("BUG: failure at %s:%d/%s()!\n", __FILE__,	\
 			__LINE__, __func__);				\
 		barrier_before_unreachable();				\
@@ -99,7 +100,8 @@ extern __printf(1, 2) void __warn_printk(const char *fmt, ...);
 #ifndef __WARN_FLAGS
 #define __WARN()		__WARN_printf(TAINT_WARN, NULL)
 #define __WARN_printf(taint, arg...) do {				\
-		if (!KUNIT_IS_SUPPRESSED_WARNING(__func__)) {		\
+		if (suppressed_symbols_cnt &&				\
+		    !KUNIT_IS_SUPPRESSED_WARNING(__func__)) {		\
 			instrumentation_begin();			\
 			warn_slowpath_fmt(__FILE__, __LINE__, taint, arg);\
 			instrumentation_end();				\
@@ -108,7 +110,8 @@ extern __printf(1, 2) void __warn_printk(const char *fmt, ...);
 #else
 #define __WARN()		__WARN_FLAGS(BUGFLAG_TAINT(TAINT_WARN))
 #define __WARN_printf(taint, arg...) do {				\
-		if (!KUNIT_IS_SUPPRESSED_WARNING(__func__)) {		\
+		if (suppressed_symbols_cnt &&				\
+		    !KUNIT_IS_SUPPRESSED_WARNING(__func__)) {		\
 			instrumentation_begin();			\
 			__warn_printk(arg);				\
 			__WARN_FLAGS(BUGFLAG_NO_CUT_HERE |		\
@@ -118,7 +121,8 @@ extern __printf(1, 2) void __warn_printk(const char *fmt, ...);
 	} while (0)
 #define WARN_ON_ONCE(condition) ({				\
 	int __ret_warn_on = !!(condition);			\
-	if (unlikely(__ret_warn_on) && !KUNIT_IS_SUPPRESSED_WARNING(__func__))	\
+	if (unlikely(__ret_warn_on) && suppressed_symbols_cnt &&\
+	    !KUNIT_IS_SUPPRESSED_WARNING(__func__))		\
 		__WARN_FLAGS(BUGFLAG_ONCE |			\
 			     BUGFLAG_TAINT(TAINT_WARN));	\
 	unlikely(__ret_warn_on);				\
@@ -130,7 +134,8 @@ extern __printf(1, 2) void __warn_printk(const char *fmt, ...);
 #ifndef WARN_ON
 #define WARN_ON(condition) ({						\
 	int __ret_warn_on = !!(condition);				\
-	if (unlikely(__ret_warn_on) && !KUNIT_IS_SUPPRESSED_WARNING(__func__))	\
+	if (unlikely(__ret_warn_on) && suppressed_symbols_cnt &&	\
+	    !KUNIT_IS_SUPPRESSED_WARNING(__func__))			\
 		__WARN();						\
 	unlikely(__ret_warn_on);					\
 })
@@ -147,7 +152,8 @@ extern __printf(1, 2) void __warn_printk(const char *fmt, ...);
 
 #define WARN_TAINT(condition, taint, format...) ({			\
 	int __ret_warn_on = !!(condition);				\
-	if (unlikely(__ret_warn_on) && !KUNIT_IS_SUPPRESSED_WARNING(__func__))	\
+	if (unlikely(__ret_warn_on) && suppressed_symbols_cnt &&	\
+	    !KUNIT_IS_SUPPRESSED_WARNING(__func__))			\
 		__WARN_printf(taint, format);				\
 	unlikely(__ret_warn_on);					\
 })
@@ -166,7 +172,8 @@ extern __printf(1, 2) void __warn_printk(const char *fmt, ...);
 #else /* !CONFIG_BUG */
 #ifndef HAVE_ARCH_BUG
 #define BUG() do {		\
-	if (!KUNIT_IS_SUPPRESSED_WARNING(__func__)) {			\
+	if (suppressed_symbols_cnt &&					\
+	    !KUNIT_IS_SUPPRESSED_WARNING(__func__)) {			\
 		do {} while (1);					\
 		unreachable();						\
 	}								\
diff --git a/include/kunit/bug.h b/include/kunit/bug.h
index 9a4eff2897e9..3256e3f2c165 100644
--- a/include/kunit/bug.h
+++ b/include/kunit/bug.h
@@ -23,6 +23,7 @@ struct __suppressed_warning {
 	const char *function;
 	int counter;
 };
+extern int suppressed_symbols_cnt;
 
 void __kunit_start_suppress_warning(struct __suppressed_warning *warning);
 void __kunit_end_suppress_warning(struct __suppressed_warning *warning);
diff --git a/lib/kunit/bug.c b/lib/kunit/bug.c
index 4da9ae478f25..5beaee1049eb 100644
--- a/lib/kunit/bug.c
+++ b/lib/kunit/bug.c
@@ -11,15 +11,19 @@
 #include <linux/list.h>
 
 static LIST_HEAD(suppressed_warnings);
+int suppressed_symbols_cnt = 0;
+EXPORT_SYMBOL_GPL(suppressed_symbols_cnt);
 
 void __kunit_start_suppress_warning(struct __suppressed_warning *warning)
 {
+	suppressed_symbols_cnt++;
 	list_add(&warning->node, &suppressed_warnings);
 }
 EXPORT_SYMBOL_GPL(__kunit_start_suppress_warning);
 
 void __kunit_end_suppress_warning(struct __suppressed_warning *warning)
 {
+	suppressed_symbols_cnt--;
 	list_del(&warning->node);
 }
 EXPORT_SYMBOL_GPL(__kunit_end_suppress_warning);
-- 
2.34.1


  parent reply	other threads:[~2025-05-26 13:28 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-26 13:27 [PATCH v5 0/5] kunit: Add support for suppressing warning backtraces Alessandro Carminati
2025-05-26 13:27 ` [PATCH v5 1/5] bug/kunit: Core " Alessandro Carminati
2025-05-28 22:47   ` Kees Cook
2025-05-29  9:02     ` Peter Zijlstra
2025-05-29 17:46       ` Kees Cook
2025-05-30  9:25         ` Peter Zijlstra
2025-05-29  9:01   ` Peter Zijlstra
2025-05-29 10:36     ` Alessandro Carminati
2025-05-30 14:01       ` Peter Zijlstra
2025-05-30 17:48         ` Kees Cook
2025-05-31 10:23           ` Peter Zijlstra
2025-05-31 13:51             ` Kees Cook
2025-06-02  7:57               ` Peter Zijlstra
2025-06-02 10:38                 ` Maxime Ripard
2025-06-03 11:40                   ` Simona Vetter
2025-06-02 11:13             ` Maxime Ripard
2025-06-03 12:26               ` Peter Zijlstra
2025-06-04  3:30                 ` Daniel Latypov
2025-06-06  8:05                 ` Maxime Ripard
2025-05-31 10:25           ` Peter Zijlstra
2025-05-31  7:46         ` Peter Zijlstra
2025-05-31  7:52         ` Alessandro Carminati
2025-05-30  9:26   ` Peter Zijlstra
2025-05-26 13:27 ` Alessandro Carminati [this message]
2025-05-28 22:52   ` [PATCH v5 2/5] bug/kunit: Suppressing warning backtraces reduced impact on WARN*() sites Kees Cook
2025-05-26 13:27 ` [PATCH v5 3/5] Add unit tests to verify that warning backtrace suppression works Alessandro Carminati
2025-05-26 13:27 ` [PATCH v5 4/5] drm: Suppress intentional warning backtraces in scaling unit tests Alessandro Carminati
2025-05-26 13:27 ` [PATCH v5 5/5] kunit: Add documentation for warning backtrace suppression API Alessandro Carminati
2025-06-02  7:24 ` [PATCH v5 0/5] kunit: Add support for suppressing warning backtraces Dan Carpenter
2025-06-02 10:47   ` Maxime Ripard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250526132755.166150-3-acarmina@redhat.com \
    --to=acarmina@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=alessandro.carminati@gmail.com \
    --cc=arthurgrillo@riseup.net \
    --cc=brendan.higgins@linux.dev \
    --cc=dan.carpenter@linaro.org \
    --cc=daniel.diaz@linaro.org \
    --cc=daniel@ffwll.ch \
    --cc=davidgow@google.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=jani.nikula@intel.com \
    --cc=jeff.johnson@oss.qualcomm.com \
    --cc=jpoimboe@kernel.org \
    --cc=keescook@chromium.org \
    --cc=kunit-dev@googlegroups.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=lkft@linaro.org \
    --cc=mripard@kernel.org \
    --cc=naresh.kamboju@linaro.org \
    --cc=peterz@infradead.org \
    --cc=skhan@linuxfoundation.org \
    --cc=ville.syrjala@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).