From nobody Tue Apr 7 14:52:37 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A2451ECAAA3 for ; Fri, 26 Aug 2022 16:21:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344532AbiHZQVc (ORCPT ); Fri, 26 Aug 2022 12:21:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57508 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245347AbiHZQV3 (ORCPT ); Fri, 26 Aug 2022 12:21:29 -0400 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 543ACDD77F for ; Fri, 26 Aug 2022 09:21:28 -0700 (PDT) Received: by mail-pl1-f170.google.com with SMTP id io24so2002463plb.1 for ; Fri, 26 Aug 2022 09:21:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=3YS87apsAp4Y8ljTYzQ8qPu+YHJ1Qj3bERhNEEJVHos=; b=ITT4B7jasJMljb2ZZCJ2mYxIX9VOgUtMM7xym8gR2YY17zvIj17RoXZsUrh5QPiLfR r5RJAaWklQKWvJTi9KZIsdjD9g5V9GluUOply8k85jnjy+z7zset9bKVtgmArYv8TBE1 ZfEyjqhUcfSZjOYowJ+ctKMG9ZgRgjmyUXUsb8SoboBYlmOshEuwwxGTC0pzwljqtQSQ zs4+tkFGyK7E2rLLomETbPqYGdvFTWU2GTnkMe3O0HOnCbSB75Y1VYCLUjr9AaN8Xx6u dLASgZuy2Ru5ZhA/9+b5zJ8Ib8uYzRCwWC0w2ODBRrEcRmXd0H1BJF4XdVoZm3HXMLNr yaXw== X-Gm-Message-State: ACgBeo2QAqCbWKKTfNHvr3r3V6HpsCy2VCclDCWThO3CCZLuHMLP/Uit AbZCetGlLUl8n+ZXWpuCjYI= X-Google-Smtp-Source: AA6agR6CuwTBZT+7zVX1i5llqvpThok7e1DrFg4PvSjXrDy+GwEs1wAcX2EuWNyESt9ChuXZvvywow== X-Received: by 2002:a17:90b:4b4e:b0:1fb:3c38:28ea with SMTP id mi14-20020a17090b4b4e00b001fb3c3828eamr5265977pjb.131.1661530887732; Fri, 26 Aug 2022 09:21:27 -0700 (PDT) Received: from bvanassche-linux.mtv.corp.google.com ([2620:15c:211:201:a12:b4b9:f1b3:ec63]) by smtp.gmail.com with ESMTPSA id cp7-20020a170902e78700b0016ee328fd61sm1790077plb.198.2022.08.26.09.21.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Aug 2022 09:21:26 -0700 (PDT) From: Bart Van Assche To: Kees Cook Cc: linux-kernel@vger.kernel.org, Bart Van Assche , Andrew Morton , Arnd Bergmann , Dan Williams , Eric Dumazet , Ingo Molnar , Isabella Basso , "Jason A. Donenfeld" , Josh Poimboeuf , Luc Van Oostenryck , Masami Hiramatsu , Nathan Chancellor , Peter Zijlstra , Rasmus Villemoes , Sander Vanheule , Steven Rostedt , Vlastimil Babka , Yury Norov Subject: [PATCH 1/2] testing/selftests: Add tests for the is_signed_type() macro Date: Fri, 26 Aug 2022 09:21:15 -0700 Message-Id: <20220826162116.1050972-2-bvanassche@acm.org> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog In-Reply-To: <20220826162116.1050972-1-bvanassche@acm.org> References: <20220826162116.1050972-1-bvanassche@acm.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Although not documented, is_signed_type() must support the 'bool' and pointer types next to scalar and enumeration types. Add a selftest that verifies that this macro handles all supported types correctly. Cc: Andrew Morton Cc: Arnd Bergmann Cc: Dan Williams Cc: Eric Dumazet Cc: Ingo Molnar Cc: Isabella Basso Cc: "Jason A. Donenfeld" Cc: Josh Poimboeuf Cc: Luc Van Oostenryck Cc: Masami Hiramatsu Cc: Nathan Chancellor Cc: Peter Zijlstra Cc: Rasmus Villemoes Cc: Sander Vanheule Cc: Steven Rostedt Cc: Vlastimil Babka Cc: Yury Norov Signed-off-by: Bart Van Assche Acked-by: Rasmus Villemoes Reviewed-by: Kees Cook Tested-by: Isabella Basso --- lib/Kconfig.debug | 12 ++++++++++ lib/Makefile | 1 + lib/is_signed_type_test.c | 48 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 lib/is_signed_type_test.c diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 072e4b289c13..36455953d306 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -2506,6 +2506,18 @@ config MEMCPY_KUNIT_TEST =20 If unsure, say N. =20 +config IS_SIGNED_TYPE_KUNIT_TEST + tristate "Test is_signed_type() macro" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + Builds unit tests for the is_signed_type() macro. + + For more information on KUnit and unit tests in general please refer + to the KUnit documentation in Documentation/dev-tools/kunit/. + + If unsure, say N. + config OVERFLOW_KUNIT_TEST tristate "Test check_*_overflow() functions at runtime" if !KUNIT_ALL_TES= TS depends on KUNIT diff --git a/lib/Makefile b/lib/Makefile index 5927d7fa0806..70176ff17023 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -377,6 +377,7 @@ obj-$(CONFIG_BITS_TEST) +=3D test_bits.o obj-$(CONFIG_CMDLINE_KUNIT_TEST) +=3D cmdline_kunit.o obj-$(CONFIG_SLUB_KUNIT_TEST) +=3D slub_kunit.o obj-$(CONFIG_MEMCPY_KUNIT_TEST) +=3D memcpy_kunit.o +obj-$(CONFIG_IS_SIGNED_TYPE_KUNIT_TEST) +=3D is_signed_type_test.o obj-$(CONFIG_OVERFLOW_KUNIT_TEST) +=3D overflow_kunit.o CFLAGS_stackinit_kunit.o +=3D $(call cc-disable-warning, switch-unreachabl= e) obj-$(CONFIG_STACKINIT_KUNIT_TEST) +=3D stackinit_kunit.o diff --git a/lib/is_signed_type_test.c b/lib/is_signed_type_test.c new file mode 100644 index 000000000000..f2eedb1f0935 --- /dev/null +++ b/lib/is_signed_type_test.c @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0 OR MIT +/* + * ./tools/testing/kunit/kunit.py run is_signed_type [--raw_output] + */ +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include + +enum unsigned_enum { + constant_a =3D 3, +}; + +enum signed_enum { + constant_b =3D -1, + constant_c =3D 2, +}; + +static void is_signed_type_test(struct kunit *test) +{ + KUNIT_EXPECT_EQ(test, is_signed_type(bool), false); + KUNIT_EXPECT_EQ(test, is_signed_type(signed char), true); + KUNIT_EXPECT_EQ(test, is_signed_type(unsigned char), false); + KUNIT_EXPECT_EQ(test, is_signed_type(int), true); + KUNIT_EXPECT_EQ(test, is_signed_type(unsigned int), false); + KUNIT_EXPECT_EQ(test, is_signed_type(long), true); + KUNIT_EXPECT_EQ(test, is_signed_type(unsigned long), false); + KUNIT_EXPECT_EQ(test, is_signed_type(long long), true); + KUNIT_EXPECT_EQ(test, is_signed_type(unsigned long long), false); + KUNIT_EXPECT_EQ(test, is_signed_type(enum unsigned_enum), false); + KUNIT_EXPECT_EQ(test, is_signed_type(enum signed_enum), true); + KUNIT_EXPECT_EQ(test, is_signed_type(void *), false); + KUNIT_EXPECT_EQ(test, is_signed_type(const char *), false); +} + +static struct kunit_case is_signed_type_test_cases[] =3D { + KUNIT_CASE(is_signed_type_test), + {} +}; + +static struct kunit_suite is_signed_type_test_suite =3D { + .name =3D "is_signed_type", + .test_cases =3D is_signed_type_test_cases, +}; + +kunit_test_suite(is_signed_type_test_suite); + +MODULE_LICENSE("Dual MIT/GPL"); From nobody Tue Apr 7 14:52:37 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6FFA2ECAAA3 for ; Fri, 26 Aug 2022 16:21:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344594AbiHZQVh (ORCPT ); Fri, 26 Aug 2022 12:21:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57550 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344578AbiHZQVb (ORCPT ); Fri, 26 Aug 2022 12:21:31 -0400 Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 689F8DD77E for ; Fri, 26 Aug 2022 09:21:30 -0700 (PDT) Received: by mail-pj1-f42.google.com with SMTP id m10-20020a17090a730a00b001fa986fd8eeso8564497pjk.0 for ; Fri, 26 Aug 2022 09:21:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=UHxQNac7+y6T8HuGfiFe8GQdrtI4xLbtO21BSRiD3iY=; b=OLvxdcq+e9sygai/ODa7nXQFO3vWSFjSqk2uC515dUhqK80k6gD2Rcokg1T0cNSqI0 XQOHpxv8sLh1+11gYa6bnIw07Cm3+rYAN20O/cCVRxSmBiXUqn4F7dMg1zb577yNHJWR ALCk3TuHjRbcjcl0XQpHCa6KmxoFIFZJtqW82+F1y3X12YvwlP7FRo0iXBFuBJZNS5IW pmlYH9b9HJXOx3rKcWXPQkTcuHEdT27v9oh1ZFMcqIAl5Bdaagm3NVaHKW6T5OgCJoLC IirtG1jP2wt9dyKfEzuPK6JjiAZlvVpieWeR6ph/4hrL4soFNz7ub5CgwxGyd+VOgjLY FECg== X-Gm-Message-State: ACgBeo1a+X8YB8JOWfr99OdnGOfHFVJA14by/HbHsHA/SRix5cKbHy3R wEWV9XQxeE8KlFmAEETS95o= X-Google-Smtp-Source: AA6agR7569BCSuk2CkAI6DTAiTClaYf70CfQxCaRkJFxLbUSlRbdJ16kpGNe4GYk/hwJhFeqWR9qjQ== X-Received: by 2002:a17:903:134d:b0:172:e199:bb39 with SMTP id jl13-20020a170903134d00b00172e199bb39mr4301800plb.154.1661530889774; Fri, 26 Aug 2022 09:21:29 -0700 (PDT) Received: from bvanassche-linux.mtv.corp.google.com ([2620:15c:211:201:a12:b4b9:f1b3:ec63]) by smtp.gmail.com with ESMTPSA id cp7-20020a170902e78700b0016ee328fd61sm1790077plb.198.2022.08.26.09.21.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Aug 2022 09:21:29 -0700 (PDT) From: Bart Van Assche To: Kees Cook Cc: linux-kernel@vger.kernel.org, Bart Van Assche , Andrew Morton , Arnd Bergmann , Dan Williams , Eric Dumazet , Ingo Molnar , Isabella Basso , "Jason A. Donenfeld" , Josh Poimboeuf , Luc Van Oostenryck , Masami Hiramatsu , Nathan Chancellor , Peter Zijlstra , Rasmus Villemoes , Sander Vanheule , Steven Rostedt , Vlastimil Babka , Yury Norov Subject: [PATCH 2/2] overflow, tracing: Define the is_signed_type() macro once Date: Fri, 26 Aug 2022 09:21:16 -0700 Message-Id: <20220826162116.1050972-3-bvanassche@acm.org> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog In-Reply-To: <20220826162116.1050972-1-bvanassche@acm.org> References: <20220826162116.1050972-1-bvanassche@acm.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" There are two definitions of the is_signed_type() macro: one in and a second definition in . As suggested by Linus Torvalds, move the definition of the is_signed_type() macro into the header file. Change the definition of the is_signed_type() macro to make sure that it does not trigger any sparse warnings with future versions of sparse for bitwise types. See also: https://lore.kernel.org/all/CAHk-=3DwhjH6p+qzwUdx5SOVVHjS3WvzJQr6mDUwhEyTf6= pJWzaQ@mail.gmail.com/ https://lore.kernel.org/all/CAHk-=3DwjQGnVfb4jehFR0XyZikdQvCZouE96xR_nnf5kq= aM5qqQ@mail.gmail.com/ Cc: Andrew Morton Cc: Arnd Bergmann Cc: Dan Williams Cc: Eric Dumazet Cc: Ingo Molnar Cc: Isabella Basso Cc: "Jason A. Donenfeld" Cc: Josh Poimboeuf Cc: Luc Van Oostenryck Cc: Masami Hiramatsu Cc: Nathan Chancellor Cc: Peter Zijlstra Cc: Rasmus Villemoes Cc: Sander Vanheule Cc: Steven Rostedt Cc: Vlastimil Babka Cc: Yury Norov Signed-off-by: Bart Van Assche --- include/linux/compiler.h | 6 ++++++ include/linux/overflow.h | 1 - include/linux/trace_events.h | 2 -- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/include/linux/compiler.h b/include/linux/compiler.h index 01ce94b58b42..7713d7bcdaea 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -239,6 +239,12 @@ static inline void *offset_to_ptr(const int *off) /* &a[0] degrades to a pointer: a different type from an array */ #define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0])) =20 +/* + * Whether 'type' is a signed type or an unsigned type. Supports scalar ty= pes, + * bool and also pointer types. + */ +#define is_signed_type(type) (((type)(-1)) < (__force type)1) + /* * This is needed in functions which generate the stack canary, see * arch/x86/kernel/smpboot.c::start_secondary() for an example. diff --git a/include/linux/overflow.h b/include/linux/overflow.h index f1221d11f8e5..0eb3b192f07a 100644 --- a/include/linux/overflow.h +++ b/include/linux/overflow.h @@ -30,7 +30,6 @@ * https://mail-index.netbsd.org/tech-misc/2007/02/05/0000.html - * credit to Christian Biere. */ -#define is_signed_type(type) (((type)(-1)) < (type)1) #define __type_half_max(type) ((type)1 << (8*sizeof(type) - 1 - is_signed_= type(type))) #define type_max(T) ((T)((__type_half_max(T) - 1) + __type_half_max(T))) #define type_min(T) ((T)((T)-type_max(T)-(T)1)) diff --git a/include/linux/trace_events.h b/include/linux/trace_events.h index b18759a673c6..8401dec93c15 100644 --- a/include/linux/trace_events.h +++ b/include/linux/trace_events.h @@ -814,8 +814,6 @@ extern int trace_add_event_call(struct trace_event_call= *call); extern int trace_remove_event_call(struct trace_event_call *call); extern int trace_event_get_offsets(struct trace_event_call *call); =20 -#define is_signed_type(type) (((type)(-1)) < (type)1) - int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set); int trace_set_clr_event(const char *system, const char *event, int set); int trace_array_set_clr_event(struct trace_array *tr, const char *system,