From patchwork Wed Apr 3 13:19:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 13616136 Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 442D3148FEC; Wed, 3 Apr 2024 13:19:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712150390; cv=none; b=uAPJRP7QBa13cqzU9ZmapE9o9lRQ5ROQta8d0pu1LL/rsZ+HaNNEHaURLBMe8/Kz0tKX4vLbg+w/lsQ6iB1BBSdx3uqJkrU1cTa1y9wOLK1lHuAGz4zMKQzWM6udwtGaWxRQYhNpnFdV/WSBSUBYd5CUyLokdrAzeVdPqAzfdfw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712150390; c=relaxed/simple; bh=+MSzk1/Cv1dDL7iePAv3xCaPMeFQUdRL5C1E7/gwMFI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=EsscygbeI7+13BsG9yEM4eOHEaVyTQ9gXAODME4ccrXfk8UwUrE4l0CSc7OVKSYt/l7ZvsBKFFGOucQ5Avv+/xP4Qi5SDlFtzMOkjOwtYcNfe/4SIvcJzLx79XTHSR3xOpDZjxZlHawYG148ywEE14zzs+tqrg/RVRlHfeNA1bA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=X0m9Nv+a; arc=none smtp.client-ip=209.85.216.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="X0m9Nv+a" Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-2a2474f2595so2124713a91.1; Wed, 03 Apr 2024 06:19:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712150388; x=1712755188; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=Pjv7Xp9MV0DSDQU0m3fWBF7u8xCRPonxB88RSacFUiY=; b=X0m9Nv+aOPGK4kWO4bMEKNKWH1LCEUp6JVF0739YoXHmB/NJDQd1+NV6f7RyW4phRL lx8g+S1zr+MJMBrWGxC6XU/FHZfHo7h2OC12XtSh1dp9tAswDXTud78Ok7rnVN9d8pLV +ZCSvjl6OO58F1Vk74pf7yyD7lR1Uk/Yrhm9SFo3OcB31msp8pjHC7I7Ft9Enp5mM3yO /v3HN2FI8DV4fpCb+m7JENoNb/3i69rV62/AoDt1VmPYQg6ZckkiwUMeN3nsEqvIc5Nl Wzbgf87tbr+jreIy4CfOku9NqGZAEa+9W+eECejM0K/N1zkyEeTu5Wxe1wwSZUMz2Ogm ziIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712150388; x=1712755188; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=Pjv7Xp9MV0DSDQU0m3fWBF7u8xCRPonxB88RSacFUiY=; b=J3BNIQO08Hl1KUsuUfDWq8TQhkEijbU6Or7Mmx0xY7RSzI66pIb2TXH5Z8+KW2izti RkqutjXHInxSU8+bq97GOZR3pbbLu//DKBn7nBpZcSYio0no3LFL8el65Xj+pX6iT0ig ke/bgArFYiwuq40VgzB+m0NdhBc9qmCb7W278enXgUPsy9bD3l1gyL359cBdaKFQ5Tu+ lBDIAX9nuJI6zNw/ym00vPV/iEwgHSw5qRZbveV3qut6GjOkLOznCL8S6JaaF1dOwOxF OobYWmW8ckltFumXQfk0tK3aVEnaX53w5o3U2Rkqm9mh65u41Zv+Ric4Y23zvf+Uj48z w8Vg== X-Forwarded-Encrypted: i=1; AJvYcCWqSxy3bZnzGwW2HchvvmpyttkrmgPWOm3mzZvhOJBtEq54dgAcCzfIwBk0wtXKXnIpFOh6qDEoGbG1dxXLA0qX1g+zcJcjXTFhijd6yvLZ5Cwsi/f9FeZbSNEF5ciW8R+3HJ7KwYgs5LplEMYma5wUUQRQO6J2I/QdRotc8TYV+VCp/GsnmyOgYK2CJVaaNmqdgmQphBFk66J8lWng4WJ0SGPKW+68VUTqn6he9X1Sdu8GPrklUtUi78/9x62hrGYxngVhB3EbcKTWYISTKNMxxSw+ImvMTdgMLKsVJZsSkAr/jIeeinZjGy9guzQ3aA== X-Gm-Message-State: AOJu0Yy6X+1DBCUMPtP/FnTu2uiBlY8msLIvB/fOJX88tbbadJwZ1Kj6 jdHZ6/C5UQeMqB/RPT6R+RupptoxjoL+dyHmqtmm3zmF6e4lVFp1r9GdjqXd X-Google-Smtp-Source: AGHT+IH9i3q5NLbj3L3SRhLxC/yUkLElgfzecWcrFNyAnqHWTVhzS/ed731TWMk8XQGzPC81E+r2yQ== X-Received: by 2002:a17:90a:fd8b:b0:2a2:b2bd:e1b1 with SMTP id cx11-20020a17090afd8b00b002a2b2bde1b1mr551165pjb.22.1712150388063; Wed, 03 Apr 2024 06:19:48 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id on4-20020a17090b1d0400b002a0777a2b1asm11473160pjb.10.2024.04.03.06.19.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 06:19:46 -0700 (PDT) Sender: Guenter Roeck From: Guenter Roeck 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 , 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, netdev@vger.kernel.org, x86@kernel.org, Guenter Roeck , Linux Kernel Functional Testing Subject: [PATCH v3 03/15] kunit: Add test cases for backtrace warning suppression Date: Wed, 3 Apr 2024 06:19:24 -0700 Message-Id: <20240403131936.787234-4-linux@roeck-us.net> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240403131936.787234-1-linux@roeck-us.net> References: <20240403131936.787234-1-linux@roeck-us.net> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 Acked-by: Dan Carpenter Reviewed-by: Kees Cook Signed-off-by: Guenter Roeck --- 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 + */ + +#include +#include + +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");