From patchwork Fri Nov 11 08:13:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Mailhol X-Patchwork-Id: 18536 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp613285wru; Fri, 11 Nov 2022 00:22:24 -0800 (PST) X-Google-Smtp-Source: AA0mqf7ZGCmpTL35POOEJFEIiuxIqmCWWKuHyqdd7N89bMNnVQ7dhwaCOrcbakj1lyHk7cdsb14z X-Received: by 2002:a17:902:da92:b0:187:4ace:e20c with SMTP id j18-20020a170902da9200b001874acee20cmr1551751plx.75.1668154944532; Fri, 11 Nov 2022 00:22:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668154944; cv=none; d=google.com; s=arc-20160816; b=JVM3v8/iD6fUOaD818outpMT8TACZMnf2HDypB8xfcS7PoIW+wc8p4KJdVwghS5knu n8rBvPTd6Fq6C/puWBo2emrvb3rZwwkqchr1XVI/WvyIjZXC7Lp+Xxazfx/olCChQcn/ 8Gc5tc17asah9/FtwSWl1CFA1byQ8vG96o5rIfCpCpsmhagd/dyO8G/qIhVcNCok7Jmo aiPc1A81EYEkgjVu3F91ZDFWVPhEPAYAyRQ/3dYE7JWHXuIvVQ3TRB4v31i4b3IixpaH 2XHzjDW70aEXdZdyhx+jyRTI1Bdvy2b+c/26+56HI04ILfP4ua5BEWuK6ZJkhRZe6R0p RCQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=4TCRV1/qBl17v1F3RlJwRpUnEFfKgAg+tleE1YFqsO0=; b=pBqqwJOq//6J3QoUvHfYcdqkYildeI63/q3Zy0vKYnB1WW+HbMOx68qNEKo6C7Isau iGm2z6nwFfoEnwa+ryDROabGKckCabwxGmqFS6YoV8O1JMOyzpBPtgVhoat1Tkv1kKG9 KOy7LhjgWtANJap1Juh0mfu14YsGCKkSdWU4sM39oL1H+yggSgL9gst1Wz6PTaEK6MlZ f4CktMbvjmTRcWdZkvUj+nv287wClLbUP+bTK9yCsBL2EoqJgRBL0M0QWlZVi6Nhig1l 45JDLuWPb7tORfY0cbqnP15VgfZoD3r0BQVdDlDe6w5H1/KRVTqa4w7Yh8jQZfc0eeJV hjLw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t11-20020a170902e1cb00b00178a33f89f1si1606170pla.267.2022.11.11.00.22.08; Fri, 11 Nov 2022 00:22:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233033AbiKKINn (ORCPT + 99 others); Fri, 11 Nov 2022 03:13:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230377AbiKKINl (ORCPT ); Fri, 11 Nov 2022 03:13:41 -0500 Received: from smtp.smtpout.orange.fr (smtp-15.smtpout.orange.fr [80.12.242.15]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E21096AEEC for ; Fri, 11 Nov 2022 00:13:38 -0800 (PST) Received: from YC20090004.ad.ts.tri-ad.global ([103.175.111.222]) by smtp.orange.fr with ESMTPA id tPA6omw6rPMmatPAFocD5t; Fri, 11 Nov 2022 09:13:36 +0100 X-ME-Helo: YC20090004.ad.ts.tri-ad.global X-ME-Auth: bWFpbGhvbC52aW5jZW50QHdhbmFkb28uZnI= X-ME-Date: Fri, 11 Nov 2022 09:13:36 +0100 X-ME-IP: 103.175.111.222 From: Vincent Mailhol To: Yury Norov , linux-kernel@vger.kernel.org Cc: Vincent Mailhol , Andrew Morton , Nathan Chancellor , "Peter Zijlstra (Intel)" , David Gow , Kees Cook , Josh Poimboeuf , Dan Williams , Miguel Ojeda , Isabella Basso , Vlastimil Babka , Rasmus Villemoes , Nick Desaulniers Subject: [RFC PATCH] lib: test_bitops: add compile-time optimization/evaluations assertions Date: Fri, 11 Nov 2022 17:13:16 +0900 Message-Id: <20221111081316.30373-1-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749187239429687323?= X-GMAIL-MSGID: =?utf-8?q?1749187239429687323?= Add a function to the bitops test suite to assert that the bitops helper correctly fold constant expressions (or trigger a build bug otherwise). This should work on all the optimization levels supported by Kbuild. The function doesn't perform any runtime tests and gets optimized out to nothing after passing the build assertions. At the time of writing, we have not yet asserted that this will work on all architectures. Architectures which fail that test should adjust their arch/*/include/asm/bitops.h by either: - using __builtin_constant_p() to select between the architecture specific asm and the generic __builitn implementation if the architecture specific asm produces better code (similar to [1]). - always calling the generic __builtin implementation if the architecture specific asm does not produce better code (similar to [2]). [1] commit 146034fed6ee ("x86/asm/bitops: Use __builtin_ffs() to evaluate constant expressions") [2] patch "x86/asm/bitops: Replace __fls() by its generic builtin implementation" Link: https://lore.kernel.org/lkml/20221106095106.849154-2-mailhol.vincent@wanadoo.fr/ Suggested-by: Yury Norov Signed-off-by: Vincent Mailhol --- This is a follow up of this thread: https://lore.kernel.org/all/Yw8hJS9f6SofG4%2F6@yury-laptop/ in which I promised to eventually implement a check. This patch requires below series in order to work on x86: https://lore.kernel.org/lkml/20221106095106.849154-1-mailhol.vincent@wanadoo.fr/ Because that series is not yet merge, I am sending it as RFC for now. Concerning architectures other than x86, I am fine to write patches but I will not test it (I do not have the build environment). One idea would be to add this patch to any kind of CI which runs on all architecture and see which architectures need to be fixed before making this mainstream. Tell me what you think. --- lib/Kconfig.debug | 4 ++++ lib/test_bitops.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 3fc7abffc7aa..233a82cd3b6e 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -2300,6 +2300,10 @@ config TEST_BITOPS compilations. It has no dependencies and doesn't run or load unless explicitly requested by name. for example: modprobe test_bitops. + In addition, check that the compiler is able to fold the bitops + function into a compile-time constant (given that the argument is also + a compile-time constant) and trigger a build bug otherwise. + If unsure, say N. config TEST_VMALLOC diff --git a/lib/test_bitops.c b/lib/test_bitops.c index 3b7bcbee84db..e6e3d22ce52a 100644 --- a/lib/test_bitops.c +++ b/lib/test_bitops.c @@ -50,6 +50,50 @@ static unsigned long order_comb_long[][2] = { }; #endif +static void __init test_bitops_const_eval(void) +{ + int res; + u64 res64; + + /* + * On any supported optimization level (-O2, -Os) and if + * invoked with a compile-time constant argument, the compiler + * must be able to fold into a constant expression all the bit + * find functions. Namely: __ffs(), ffs(), ffz(), __fls(), + * fls() and fls64(). Otherwise, trigger a build bug. + */ + + /* __ffs(BIT(10)) == 10 */ + res = __ffs(BIT(10)) == 10; + BUILD_BUG_ON(!__builtin_constant_p(res)); + BUILD_BUG_ON(!res); + + /* ffs(BIT(10)) == 11 */ + res = ffs(BIT(10)) == 11; + BUILD_BUG_ON(!__builtin_constant_p(res)); + BUILD_BUG_ON(!res); + + /* ffz(~BIT(10)) == 10 */ + res = ffz(~BIT(10)) == 10; + BUILD_BUG_ON(!__builtin_constant_p(res)); + BUILD_BUG_ON(!res); + + /* __fls(BIT(10)) == 10 */ + res = __fls(BIT(10)) == 10; + BUILD_BUG_ON(!__builtin_constant_p(res)); + BUILD_BUG_ON(!res); + + /* fls(BIT(10)) == 11 */ + res = fls(BIT(10)) == 11; + BUILD_BUG_ON(!__builtin_constant_p(res)); + BUILD_BUG_ON(!res); + + /* fls64(BIT_ULL(10)) == 11 */ + res64 = fls64(BIT_ULL(10)) == 11; + BUILD_BUG_ON(!__builtin_constant_p(res64)); + BUILD_BUG_ON(!res64); +} + static int __init test_bitops_startup(void) { int i, bit_set; @@ -94,6 +138,8 @@ static int __init test_bitops_startup(void) if (bit_set != BITOPS_LAST) pr_err("ERROR: FOUND SET BIT %d\n", bit_set); + test_bitops_const_eval(); + pr_info("Completed bitops test\n"); return 0; From patchwork Sun Dec 17 07:12:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Mailhol X-Patchwork-Id: 179963 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp583961dyi; Sat, 16 Dec 2023 23:14:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IEWWx8AJYz44y6YxemuUSmncfWd//Th8YOU8paMM6M2DSL/OAt38HitSwM7f4hqsiNMNqxf X-Received: by 2002:a92:cda4:0:b0:35f:addc:b2cd with SMTP id g4-20020a92cda4000000b0035faddcb2cdmr311381ild.61.1702797259062; Sat, 16 Dec 2023 23:14:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702797259; cv=none; d=google.com; s=arc-20160816; b=Bk835o0qTHvqQHo9hZbiPLfwOb3BPlST9pfnXNae9w3KP8hckGZMBY8o8lqzqi1cTS IU23Q7iQNrTNLBL8iXnqV8L08Uv82gbSI7zf4I0MCsEWWYI6xaoUA3wHAFA6OdF1sfIE /F0OYxrktvi3+/khuUZoVGrSnwUJY1gt1B/njrRWY3jOT/Dd5+W7gaCEtMSnQJSNoHeF 07e/SKLqtCfGvCCLFqGm+HFX4mmoPN/fQkGc0gPpTRLqvti0JmDGQxyY8pL2S2ZTJWCb vlXZq3+ldX3a6bPX/w/iIYutVAmY9opev0BGN5ujW9K8HGFBwLXfbn0qmsUUuckqfU+b KPmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:sender:dkim-signature; bh=ZV393vzARRuX2pwqmw5hE6OeimRr0obKDvqio21m2gk=; fh=O+LHMXuc4QZ+LwJ18sh+OSxw9k9Vm9ydCfTOjonl8SY=; b=pHxzIyRldInH+u0R+xX2adhGdjpA6t8B2vrmwRRUXMAUHJndLLP0T3vsK5RNsY/Taf uIAveYB/dY+wrCfzH7M1zzh38OJcYwDkLAlKV5xTu5WCM+7SCqu+/P1NylcIeH25aML+ 1Kva8NCWLW1VooGWm7ddxuawLnIyyUcqhpECnAm/lUgM0zua5ecBl6pk0b7Z/EA9mIBg Lz7IZfjt7Nkx2MI+GfvpFD2uEhZdx1QGOyjbhKx7uiXPB78X7lfevryP4uN6IIrzI5ep Ii55jX/FP7rgi3+Jg0N7DQUizkTyU/RgP8tG3dmzjKlVYaEL77Qn0aPIKI9hd4PR0N1n Ip/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=aroyU7BJ; spf=pass (google.com: domain of linux-kernel+bounces-2492-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2492-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id k10-20020a170902694a00b001cfd622b80dsi15775357plt.541.2023.12.16.23.14.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Dec 2023 23:14:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2492-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=aroyU7BJ; spf=pass (google.com: domain of linux-kernel+bounces-2492-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2492-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 128DB283DB2 for ; Sun, 17 Dec 2023 07:14:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2A4EB6FDD; Sun, 17 Dec 2023 07:13:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="aroyU7BJ" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-oi1-f176.google.com (mail-oi1-f176.google.com [209.85.167.176]) (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 6695D63AA; Sun, 17 Dec 2023 07:13:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=wanadoo.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-oi1-f176.google.com with SMTP id 5614622812f47-3b844357f7cso1696781b6e.1; Sat, 16 Dec 2023 23:13:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702797210; x=1703402010; 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=ZV393vzARRuX2pwqmw5hE6OeimRr0obKDvqio21m2gk=; b=aroyU7BJ9ZlRKHGgR74IRmjfYNtOpZUSPvCmssf5fHitT+f9GRoOtub54UyCsuS/bh zR/kVGnGFK+29Pm7Jk9oJlBNvn3R2rCe4Y+gYt+w/c0U/tNHtDi7kkblkVATZN8kZPDb rGivcnP3RWvjOl66y3A4N5EBuVqNQQ9EJixBTm+WKh7BiJEdn++fVF7x18DR3Zsf3dD8 EMmGdBBE1ibhlFbcjdSPujg0cLEWGlkPEpsXlL/gZKZI3UoZ9NW4uA5T0M7jMPkchsQb OwaW6m4SCFODGAuuHfnSZjxIeoE8EZTu584Y4/Pg/mqwjlBlvwsh/StOXyRcPf76xQaJ n4eA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702797210; x=1703402010; 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=ZV393vzARRuX2pwqmw5hE6OeimRr0obKDvqio21m2gk=; b=EeoBEIA9XdeZqJkn5K5nwyB0m64T9iYHnWpfE2jKZRBNViC4laeJdD/vzP4m77/EjT ImEimEqRCkAZ4GYxwZvOOUrb4gmDp+E0n63uVLce93lmyXEngrKT3n9G2oVEuPl3qT2n uuC1vaOuik8jOl3LqJDkx70IwxtM+gXfVqLv4pnNsTJaoMxpgoMjBf2LnzWNHji9r5Za UEND++N28AYZ4l+v4QzuxEG126ewRKxKjgIebTn4N8v6n6sNWRAWhTLxuyhmBRpReJUY xMVWmCB8zwpoXdvK44q3mBsGQsjqQHPbgCa4IdnuZdWCeCxW/rHV1Hduos/FxzYuQ5Mc LxNA== X-Gm-Message-State: AOJu0YzIqnZq2SVZY93zEXG0uhPVJ1UWLL0XawxhxWfljmG9ZZ0se4QF BgHz5ZHAf92VBTTkA7U8Cmg= X-Received: by 2002:a05:6808:2125:b0:3b8:b063:6b96 with SMTP id r37-20020a056808212500b003b8b0636b96mr19083767oiw.69.1702797210271; Sat, 16 Dec 2023 23:13:30 -0800 (PST) Received: from XH22050090-L.ad.ts.tri-ad.global ([103.175.111.222]) by smtp.gmail.com with ESMTPSA id b17-20020a056a000cd100b0068ffb8da107sm15740231pfv.212.2023.12.16.23.13.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Dec 2023 23:13:29 -0800 (PST) Sender: Vincent Mailhol From: Vincent Mailhol To: Andrew Morton , linux-kernel@vger.kernel.org, Yury Norov Cc: Nick Desaulniers , Douglas Anderson , Kees Cook , Petr Mladek , Randy Dunlap , Zhaoyang Huang , Geert Uytterhoeven , Marco Elver , Brian Cain , Geert Uytterhoeven , Matthew Wilcox , "Paul E . McKenney" , linux-hexagon@vger.kernel.org, linux-m68k@lists.linux-m68k.org, Vincent Mailhol Subject: [PATCH v3 3/5] hexagon/bitops: force inlining of all bitops functions Date: Sun, 17 Dec 2023 16:12:48 +0900 Message-Id: <20231217071250.892867-4-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231217071250.892867-1-mailhol.vincent@wanadoo.fr> References: <20221111081316.30373-1-mailhol.vincent@wanadoo.fr> <20231217071250.892867-1-mailhol.vincent@wanadoo.fr> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785512338547009267 X-GMAIL-MSGID: 1785512338547009267 The inline keyword actually does not guarantee that the compiler will inline a functions. Whenever the goal is to actually inline a function, __always_inline should always be preferred instead. Reference: commit 8dd5032d9c54 ("x86/asm/bitops: Force inlining of test_and_set_bit and friends") Link: https://git.kernel.org/torvalds/c/8dd5032d9c54 Signed-off-by: Vincent Mailhol --- arch/hexagon/include/asm/bitops.h | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/arch/hexagon/include/asm/bitops.h b/arch/hexagon/include/asm/bitops.h index 160d8f37fa1a..950d4acc2edc 100644 --- a/arch/hexagon/include/asm/bitops.h +++ b/arch/hexagon/include/asm/bitops.h @@ -28,7 +28,7 @@ * @nr: bit number to clear * @addr: pointer to memory */ -static inline int test_and_clear_bit(int nr, volatile void *addr) +static __always_inline int test_and_clear_bit(int nr, volatile void *addr) { int oldval; @@ -52,7 +52,7 @@ static inline int test_and_clear_bit(int nr, volatile void *addr) * @nr: bit number to set * @addr: pointer to memory */ -static inline int test_and_set_bit(int nr, volatile void *addr) +static __always_inline int test_and_set_bit(int nr, volatile void *addr) { int oldval; @@ -78,7 +78,7 @@ static inline int test_and_set_bit(int nr, volatile void *addr) * @nr: bit number to set * @addr: pointer to memory */ -static inline int test_and_change_bit(int nr, volatile void *addr) +static __always_inline int test_and_change_bit(int nr, volatile void *addr) { int oldval; @@ -103,17 +103,17 @@ static inline int test_and_change_bit(int nr, volatile void *addr) * Rewrite later to save a cycle or two. */ -static inline void clear_bit(int nr, volatile void *addr) +static __always_inline void clear_bit(int nr, volatile void *addr) { test_and_clear_bit(nr, addr); } -static inline void set_bit(int nr, volatile void *addr) +static __always_inline void set_bit(int nr, volatile void *addr) { test_and_set_bit(nr, addr); } -static inline void change_bit(int nr, volatile void *addr) +static __always_inline void change_bit(int nr, volatile void *addr) { test_and_change_bit(nr, addr); } @@ -200,7 +200,7 @@ arch_test_bit_acquire(unsigned long nr, const volatile unsigned long *addr) * * Undefined if no zero exists, so code should check against ~0UL first. */ -static inline long ffz(int x) +static __always_inline long ffz(int x) { int r; @@ -217,7 +217,7 @@ static inline long ffz(int x) * This is defined the same way as ffs. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. */ -static inline int fls(unsigned int x) +static __always_inline int fls(unsigned int x) { int r; @@ -238,7 +238,7 @@ static inline int fls(unsigned int x) * the libc and compiler builtin ffs routines, therefore * differs in spirit from the above ffz (man ffs). */ -static inline int ffs(int x) +static __always_inline int ffs(int x) { int r; @@ -260,7 +260,7 @@ static inline int ffs(int x) * bits_per_long assumed to be 32 * numbering starts at 0 I think (instead of 1 like ffs) */ -static inline unsigned long __ffs(unsigned long word) +static __always_inline unsigned long __ffs(unsigned long word) { int num; @@ -278,7 +278,7 @@ static inline unsigned long __ffs(unsigned long word) * Undefined if no set bit exists, so code should check against 0 first. * bits_per_long assumed to be 32 */ -static inline unsigned long __fls(unsigned long word) +static __always_inline unsigned long __fls(unsigned long word) { int num; From patchwork Sun Dec 17 07:12:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Mailhol X-Patchwork-Id: 179964 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp584312dyi; Sat, 16 Dec 2023 23:15:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IGujH7Ck6mw+26LWbadkfGj7MEkY/KnFOBNuQWdgh/UUFzpFFoLXwxTxHKpPD5wfNDN/4Nd X-Received: by 2002:a05:6e02:3d08:b0:35f:9a52:260c with SMTP id db8-20020a056e023d0800b0035f9a52260cmr3968132ilb.92.1702797325232; Sat, 16 Dec 2023 23:15:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702797325; cv=none; d=google.com; s=arc-20160816; b=sX3l8Z8tvSj4MrsmM2URM2Ay0sE0+3/gPFlVeOuyMlLf41qqLZCvanfo68ZlcnMpr3 VAaLmh5QZM5tt7wv3HZX4qeueAf/fTLqlGS3+DC5Ky+hNJ19qHpwIvl5/WCut308l9BK 6boImQxLob6pv3EApHzEU/dSIsAAnYfmYTDuIZeCasl1AEy0S/Mgmit87I7K7+KWFOan vaRT54dVReHMgzNdJFi6Nop/YOsAfbQAESoLFlPr8/eBV7cjt2E+lzEPnzPbOwkHirxe WmUymxTZ7uGc6D0wuURzxsTGXGWD3uN4hiMdpITABfUT3IWAMg62KNJrQBsojqpjPVRa ijPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:sender:dkim-signature; bh=JsNa9K53QEIWDiiqOBuAFIxQwF1kCxA5oMZgz893y4w=; fh=O+LHMXuc4QZ+LwJ18sh+OSxw9k9Vm9ydCfTOjonl8SY=; b=w04e3V5PCn8q0ls+ImZ0zvOFGmjr0GTDnJHsHiXQRscntkSuC9r6kEH2kW4ShG8AaU tjnAX4t3Hz7gAp/gIZcEcwSKrXdObT6cSWtN9tiR03LQiMtNKQzaFYX9Rin68Y+5FO9T mOGKggLDYnGGyuEOIn06WViGsdNz/mkWoc9VrO25xRr2MeJIePN6j/QRbXxO5Bi8llbh XVTyitOuCNXf7UNjQlEaym0m0Dc8dGF6MbeI+JgBrNEyf6iVsJtq6sbunxDtYuyNCZFG 2ZXEWhAAxGmMqGdvpfZcjvialNzsd6xv3ui/Sb9doT1Rl06Jqz4qg891wlxzA5AjHGm1 OsiQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=TZMjzs+Q; spf=pass (google.com: domain of linux-kernel+bounces-2494-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2494-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id t6-20020a17090abc4600b00286f3fe9e84si17162509pjv.45.2023.12.16.23.15.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Dec 2023 23:15:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2494-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=TZMjzs+Q; spf=pass (google.com: domain of linux-kernel+bounces-2494-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2494-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id C93DCB22C18 for ; Sun, 17 Dec 2023 07:14:39 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BF81EF9CD; Sun, 17 Dec 2023 07:13:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="TZMjzs+Q" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-oi1-f182.google.com (mail-oi1-f182.google.com [209.85.167.182]) (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 2886EDDB2; Sun, 17 Dec 2023 07:13:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=wanadoo.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-oi1-f182.google.com with SMTP id 5614622812f47-3ba2dd905f9so1376544b6e.2; Sat, 16 Dec 2023 23:13:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702797220; x=1703402020; 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=JsNa9K53QEIWDiiqOBuAFIxQwF1kCxA5oMZgz893y4w=; b=TZMjzs+QukM7cdi3bcOcVsGeJQzJtJtG1caB1xo7KqHzvB87Q/fm93wYXls+4wPHah fYPMe9AnnMnwumjHXT6v0s23V2I5r8dlfQ6KmACKnzKFBOQa89oHhGiSesu0d0fQLRK8 E+/PdAdpbptsaLXbtae3Ibqx+0icHll2HwRkxxs3KQSgBlZ8WiSMZ6br1lqEe9G+K2JA NWYCe92BVaLRg5lzFBiJdN0PV7TwEuidHtG8Zn4P2nBRlCcqIyhziZ6tPtTegp7rnPQ7 tszJZjr30G++D1U6Ksf1eJ75FrxpVd50lEBFflmlRTrBBKgAZYG2+ExzFO3zRnrE/9VL H6uQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702797220; x=1703402020; 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=JsNa9K53QEIWDiiqOBuAFIxQwF1kCxA5oMZgz893y4w=; b=ZVS7MYyrvdYqCuajwLnS3dizNN7gFXrKEV2bgXfTs+YsN/G++WqMl4uAsAmESP6jz1 EI1NiJuZJfmDgBLxKV19f2l2iPbJEcFQKvlksUnA9erWOc/XNstEm9kLzRCvSOFlHeOJ 5xMStgGYbk1O3ZJ8n3DrBoj1NcFGGse4MOB3CHa/pDM65oj3xBQxe3yR1zGwK/0875aW i4/3Pa1vSID3ZEDpx1QulOs5yII3PFPVax2x9KDZkmksB5iJJRymIvUyixFzCEyq4OeF i3kCLnxrO0uWGIom9P2qlD180Xev12hqjGlUYdv2rRyJJAQG/8jUupLIEn2kdnmwV4Ub ygPQ== X-Gm-Message-State: AOJu0Yy0q4NjnSFL6kSNXTQDgGVqvoBqIG6EFGaNiTXWpn4QlAHV4CVE zxCATM+57u0IY1YZ5RhznF4= X-Received: by 2002:a05:6808:1924:b0:3b9:f017:28f with SMTP id bf36-20020a056808192400b003b9f017028fmr18156464oib.67.1702797220239; Sat, 16 Dec 2023 23:13:40 -0800 (PST) Received: from XH22050090-L.ad.ts.tri-ad.global ([103.175.111.222]) by smtp.gmail.com with ESMTPSA id b17-20020a056a000cd100b0068ffb8da107sm15740231pfv.212.2023.12.16.23.13.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Dec 2023 23:13:39 -0800 (PST) Sender: Vincent Mailhol From: Vincent Mailhol To: Andrew Morton , linux-kernel@vger.kernel.org, Yury Norov Cc: Nick Desaulniers , Douglas Anderson , Kees Cook , Petr Mladek , Randy Dunlap , Zhaoyang Huang , Geert Uytterhoeven , Marco Elver , Brian Cain , Geert Uytterhoeven , Matthew Wilcox , "Paul E . McKenney" , linux-hexagon@vger.kernel.org, linux-m68k@lists.linux-m68k.org, Vincent Mailhol Subject: [PATCH v3 5/5] lib: test_bitops: add compile-time optimization/evaluations assertions Date: Sun, 17 Dec 2023 16:12:50 +0900 Message-Id: <20231217071250.892867-6-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231217071250.892867-1-mailhol.vincent@wanadoo.fr> References: <20221111081316.30373-1-mailhol.vincent@wanadoo.fr> <20231217071250.892867-1-mailhol.vincent@wanadoo.fr> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1749187239429687323 X-GMAIL-MSGID: 1785512407663367703 Add a function in the bitops test suite to assert that the bitops helper correctly fold constant expressions (or trigger a build bug otherwise). This should work on all the optimization levels supported by Kbuild. The added function doesn't perform any runtime tests and gets optimized out to nothing after passing the build assertions. Suggested-by: Yury Norov Signed-off-by: Vincent Mailhol --- lib/Kconfig.debug | 4 ++++ lib/test_bitops.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index cc7d53d9dc01..c97d818dbc30 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -2454,6 +2454,10 @@ config TEST_BITOPS compilations. It has no dependencies and doesn't run or load unless explicitly requested by name. for example: modprobe test_bitops. + In addition, check that the compiler is able to fold the bitops + function into a compile-time constant (given that the argument is also + a compile-time constant) and trigger a build bug otherwise. + If unsure, say N. config TEST_VMALLOC diff --git a/lib/test_bitops.c b/lib/test_bitops.c index 3b7bcbee84db..99b612515eb6 100644 --- a/lib/test_bitops.c +++ b/lib/test_bitops.c @@ -50,6 +50,34 @@ static unsigned long order_comb_long[][2] = { }; #endif +/* Assert that a boolean expression can be folded in a constant and is true. */ +#define test_const_eval(test_expr) \ +({ \ + /* Evaluate once so that compiler can fold it. */ \ + bool __test_expr = test_expr; \ + \ + BUILD_BUG_ON(!__builtin_constant_p(__test_expr)); \ + BUILD_BUG_ON(!__test_expr); \ +}) + +/* + * On any supported optimization level (-O2, -Os) and if invoked with + * a compile-time constant argument, the compiler must be able to fold + * into a constant expression all the bit find functions. Namely: + * __ffs(), ffs(), ffz(), __fls(), fls() and fls64(). Otherwise, + * trigger a build bug. + */ +static __always_inline void test_bitops_const_eval(unsigned int n) +{ + test_const_eval(__ffs(BIT(n)) == n); + test_const_eval(ffs(BIT(n)) == n + 1); + test_const_eval(ffz(~BIT(n)) == n); + test_const_eval(__fls(BIT(n)) == n); + test_const_eval(fls(BIT(n)) == n + 1); + test_const_eval(fls64(BIT_ULL(n)) == n + 1); + test_const_eval(fls64(BIT_ULL(n + 32)) == n + 33); +} + static int __init test_bitops_startup(void) { int i, bit_set; @@ -94,6 +122,10 @@ static int __init test_bitops_startup(void) if (bit_set != BITOPS_LAST) pr_err("ERROR: FOUND SET BIT %d\n", bit_set); + test_bitops_const_eval(0); + test_bitops_const_eval(10); + test_bitops_const_eval(31); + pr_info("Completed bitops test\n"); return 0;