From patchwork Thu Mar 13 11:43:16 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alessandro Carminati X-Patchwork-Id: 14014865 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4FF63C282DE for ; Thu, 13 Mar 2025 11:51:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: Content-type:MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc: To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=c9whUtdSVycPepKkB48hTehRX04VaeLWlAoVnLwkI/o=; b=aRF85CWKSnX1VrsdpXC0eLRMMb O6MwwR8B86jb9f0mlGwKzqnSjQ7zgYJcsXfOc2HKyE7yFHr0HYcLIJSzN3HdJ16iIM73WZRm8b7go OxRZ8FR+stJZl9ASRkkpXTlVcnz/gJOnR2xrJ+MjPOA9X+uKcr5jcZ5RzTYkQOOVIino/0Typ7UmE y8XFQtVdsMmJw8RsCdDwgPGA+bUE2TRFmAUGPK0zFVwfWeY5Ua7O0YSoBlwH1SraH7Dx57cWgBCGE tA9cQaF/cAOwuFQMdXYJMFgYO08dHo7gN710RXoRWlnsgQ4yzMdO2ERTu6/7ADa3Y2Psu6/JMf9nR kLnEb7Ng==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tsh5d-0000000B7eO-34G7; Thu, 13 Mar 2025 11:51:05 +0000 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tsh0k-0000000B5nF-2967 for linux-arm-kernel@lists.infradead.org; Thu, 13 Mar 2025 11:46:04 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1741866360; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=c9whUtdSVycPepKkB48hTehRX04VaeLWlAoVnLwkI/o=; b=XAFy9Qszg1K4dV2YbkBDJ9YED0aSxG6B3K6HoidmVrdeimyPhFs2D8VxVySGkfUlOb6cis kdr87LslXZAIuG0w5Zgz2JAZB/5GvM02ghE8L98qukJXZmq1ascDia4Is92nlBtMTr3wuG 1mwV17ImCaGqCorjvVqWucHBLsHZsAw= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-457-0X0cvSldPymITwqlIN9Iig-1; Thu, 13 Mar 2025 07:44:32 -0400 X-MC-Unique: 0X0cvSldPymITwqlIN9Iig-1 X-Mimecast-MFC-AGG-ID: 0X0cvSldPymITwqlIN9Iig_1741866271 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-4394040fea1so4691765e9.0 for ; Thu, 13 Mar 2025 04:44:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741866271; x=1742471071; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=c9whUtdSVycPepKkB48hTehRX04VaeLWlAoVnLwkI/o=; b=hvxFHC7LXxaJGNDxQ4PtaMiJ7bDvXW3jEKj2uDjeqpTE8Wo80x+0koWHEt9NP4NEAn Wg32S31gWbcGTWMhgL5CvETFGfuJXrj1EpnrL8OYFx1kEhBR5caftbFgu/ElRIR/WCAo Y7+hhO1T4ILlIoYKA9ODWhKFKyv7M2Tk2ZtTEz6gCaew+Mj5R6QFVF4fUL4CEreOABX0 XJpmXJrp2l/g37UL7if3uyTkTJpXLK6i8nOjFpvDsObkI58DT7QMCPuoSDZWn4rr7FXR yyyCPvjwsgTQWMVP3b6nua811w8TZvJ/Vysh+H2XeWgvTxM0iecusYnIxBuRsgXVHqIr QHEg== X-Forwarded-Encrypted: i=1; AJvYcCVeE3Kf8Oze+Y036RWMTCpc5rRCg18EOP07qU9XI035RCwzNo89iV7oN/NauXcTcO6f6L4nMJoAqWLj7v5+XLHu@lists.infradead.org X-Gm-Message-State: AOJu0Yx9eYxUcU573u5wArfZtD+mJMxG8zNCcDJmBnMlXNlCYMHhBZzk y5NgV2h3kXGeDQKoMPh89O6nXOyF12x+OncmjcPMzEwCMcs2SC8oAoz7GXjwKz4oLgh8SS8OFgQ 1Qv//nacFKMCXMeK34hqRCX+3Dvsp9auBt2lzVTZgLeWS1kt42Rv0KlyaMxMQ/2hqzNXXRfTB X-Gm-Gg: ASbGncuLw8Ac2gcve7yrJd+DrvzUCVyeQ9zzPvwYjnSTjnUeZonSjRVpXdEJu5+hQ9q ZQHqn9FHs0888wkH7YPRzdR0wNz54f1GGDkbRch5lDd/cnPR9xhs6WcWw1D6NbGEzamrRgk4lsX 92vHixk22nk1W4jAC0FZo3vp7HngsctwZG6Gpsr+fg03O/MSq6v+N8Hi25Ti4ObhDCLjDkN09wq YMvYmZINhyFaQpNQp/1sUKaoq1/Bjl3cik/dCDrPi9Y0gOkaEQA8jdxEZAU3X41HACT5JVDlCD0 QfMBSy9B+hSa/caMH+GZ X-Received: by 2002:a05:600c:46d2:b0:439:8e95:796a with SMTP id 5b1f17b1804b1-43d180a4435mr17959585e9.13.1741866271298; Thu, 13 Mar 2025 04:44:31 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEwbP4l4FVtrSDnlyy+D6/JgQ5XIs1l5aoFtF0Hno/5cr8h07s4+r9vz3ImSRA+QwHoshLsPA== X-Received: by 2002:a05:600c:46d2:b0:439:8e95:796a with SMTP id 5b1f17b1804b1-43d180a4435mr17959115e9.13.1741866270833; Thu, 13 Mar 2025 04:44:30 -0700 (PDT) Received: from lab.hqhome163.com ([81.57.75.210]) by smtp.googlemail.com with ESMTPSA id 5b1f17b1804b1-43d188b754asm17844115e9.14.2025.03.13.04.44.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Mar 2025 04:44:30 -0700 (PDT) From: Alessandro Carminati To: linux-kselftest@vger.kernel.org Cc: David Airlie , Arnd Bergmann , =?utf-8?q?Ma=C3=ADra_Canal?= , Dan Carpenter , Kees Cook , Daniel Diaz , David Gow , Arthur Grillo , Brendan Higgins , Naresh Kamboju , Maarten Lankhorst , Andrew Morton , Maxime Ripard , =?utf-8?b?VmlsbGUgU3lyasOkbMOk?= , Daniel Vetter , Thomas Zimmermann , Guenter Roeck , Alessandro Carminati , Jani Nikula , dri-devel@lists.freedesktop.org, kunit-dev@googlegroups.com, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-parisc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-sh@vger.kernel.org, loongarch@lists.linux.dev, x86@kernel.org, Linux Kernel Functional Testing , Alessandro Carminati Subject: [PATCH v4 01/14] bug/kunit: Core support for suppressing warning backtraces Date: Thu, 13 Mar 2025 11:43:16 +0000 Message-Id: <20250313114329.284104-2-acarmina@redhat.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250313114329.284104-1-acarmina@redhat.com> References: <20250313114329.284104-1-acarmina@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: C3RObYtyvOoUuXPxzSnT8m2OOtYQiZnMz-ajPdObpps_1741866271 X-Mimecast-Originator: redhat.com Content-type: text/plain X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250313_044602_704005_469BDAAE X-CRM114-Status: GOOD ( 35.44 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: 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. 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 Cc: Daniel Diaz Cc: Naresh Kamboju Cc: Kees Cook Tested-by: Linux Kernel Functional Testing Acked-by: Dan Carpenter Reviewed-by: Kees Cook Signed-off-by: Guenter Roeck Signed-off-by: Alessandro Carminati --- 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 387720933973..9194cf743ec3 100644 --- a/include/asm-generic/bug.h +++ b/include/asm-generic/bug.h @@ -18,6 +18,7 @@ #endif #ifndef __ASSEMBLY__ +#include #include #include @@ -39,8 +40,14 @@ struct bug_entry { #ifdef CONFIG_DEBUG_BUGVERBOSE #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS const char *file; +#ifdef HAVE_BUG_FUNCTION + const char *function; +#endif #else signed int file_disp; +#ifdef HAVE_BUG_FUNCTION + signed int function_disp; +#endif #endif unsigned short line; #endif @@ -96,15 +103,18 @@ extern __printf(1, 2) void __warn_printk(const char *fmt, ...); #define __WARN() __WARN_printf(TAINT_WARN, NULL) #define __WARN_printf(taint, arg...) do { \ instrumentation_begin(); \ - warn_slowpath_fmt(__FILE__, __LINE__, taint, arg); \ + if (!KUNIT_IS_SUPPRESSED_WARNING(__func__)) \ + warn_slowpath_fmt(__FILE__, __LINE__, taint, arg);\ instrumentation_end(); \ } while (0) #else #define __WARN() __WARN_FLAGS(BUGFLAG_TAINT(TAINT_WARN)) #define __WARN_printf(taint, arg...) do { \ instrumentation_begin(); \ - __warn_printk(arg); \ - __WARN_FLAGS(BUGFLAG_NO_CUT_HERE | BUGFLAG_TAINT(taint));\ + if (!KUNIT_IS_SUPPRESSED_WARNING(__func__)) { \ + __warn_printk(arg); \ + __WARN_FLAGS(BUGFLAG_NO_CUT_HERE | BUGFLAG_TAINT(taint));\ + } \ instrumentation_end(); \ } while (0) #define WARN_ON_ONCE(condition) ({ \ diff --git a/include/kunit/bug.h b/include/kunit/bug.h new file mode 100644 index 000000000000..0a8e62c1fcaf --- /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 + */ + +#ifndef _KUNIT_BUG_H +#define _KUNIT_BUG_H + +#ifndef __ASSEMBLY__ + +#include + +#ifdef CONFIG_KUNIT_SUPPRESS_BACKTRACE + +#include +#include + +struct __suppressed_warning { + struct list_head node; + const char *function; +}; + +void __kunit_start_suppress_warning(struct __suppressed_warning *warning); +void __kunit_end_suppress_warning(struct __suppressed_warning *warning); +bool __kunit_is_suppressed_warning(const char *function); + +#define DEFINE_SUPPRESSED_WARNING(func) \ + struct __suppressed_warning __kunit_suppress_##func = \ + { .function = __stringify(func) } + +#define KUNIT_START_SUPPRESSED_WARNING(func) \ + __kunit_start_suppress_warning(&__kunit_suppress_##func) + +#define KUNIT_END_SUPPRESSED_WARNING(func) \ + __kunit_end_suppress_warning(&__kunit_suppress_##func) + +#define KUNIT_IS_SUPPRESSED_WARNING(func) \ + __kunit_is_suppressed_warning(func) + +#else /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */ + +#define DEFINE_SUPPRESSED_WARNING(func) +#define KUNIT_START_SUPPRESSED_WARNING(func) +#define KUNIT_END_SUPPRESSED_WARNING(func) +#define KUNIT_IS_SUPPRESSED_WARNING(func) (false) + +#endif /* CONFIG_KUNIT_SUPPRESS_BACKTRACE */ +#endif /* __ASSEMBLY__ */ +#endif /* _KUNIT_BUG_H */ diff --git a/include/kunit/test.h b/include/kunit/test.h index 58dbab60f853..cad32d174872 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -10,6 +10,7 @@ #define _KUNIT_TEST_H #include +#include #include #include diff --git a/include/linux/bug.h b/include/linux/bug.h index a9948a9f1093..e42f20cf8830 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..5eb2ee66916f 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 && KUNIT_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 a97897edd964..201402f0ab49 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 5aa51978e456..3195e861d63c 100644 --- a/lib/kunit/Makefile +++ b/lib/kunit/Makefile @@ -16,8 +16,10 @@ ifeq ($(CONFIG_KUNIT_DEBUGFS),y) kunit-objs += debugfs.o endif -# KUnit 'hooks' are built-in even when KUnit is built as a module. -obj-y += hooks.o +# KUnit 'hooks' and bug handling are built-in even when KUnit is built +# as a module. +obj-y += hooks.o \ + bug.o obj-$(CONFIG_KUNIT_TEST) += kunit-test.o obj-$(CONFIG_KUNIT_TEST) += platform-test.o diff --git a/lib/kunit/bug.c b/lib/kunit/bug.c new file mode 100644 index 000000000000..351f9a595a71 --- /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 + */ + +#include +#include +#include +#include + +static LIST_HEAD(suppressed_warnings); + +void __kunit_start_suppress_warning(struct __suppressed_warning *warning) +{ + list_add(&warning->node, &suppressed_warnings); +} +EXPORT_SYMBOL_GPL(__kunit_start_suppress_warning); + +void __kunit_end_suppress_warning(struct __suppressed_warning *warning) +{ + list_del(&warning->node); +} +EXPORT_SYMBOL_GPL(__kunit_end_suppress_warning); + +bool __kunit_is_suppressed_warning(const char *function) +{ + struct __suppressed_warning *warning; + + if (!function) + return false; + + list_for_each_entry(warning, &suppressed_warnings, node) { + if (!strcmp(function, warning->function)) + return true; + } + return false; +} +EXPORT_SYMBOL_GPL(__kunit_is_suppressed_warning);