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;