Message ID | 20231130102717.1297492-1-mailhol.vincent@wanadoo.fr |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp287702vqy; Thu, 30 Nov 2023 02:34:14 -0800 (PST) X-Google-Smtp-Source: AGHT+IFRVTSmN9iM6qnzYz+oMRN/xTKIvqux8BRHP/4yzyAvS+2Kokb7qDcHGN0X/9Q+YY5VC5x4 X-Received: by 2002:a17:90b:180c:b0:285:adb0:de3e with SMTP id lw12-20020a17090b180c00b00285adb0de3emr17440476pjb.34.1701340453901; Thu, 30 Nov 2023 02:34:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701340453; cv=none; d=google.com; s=arc-20160816; b=axRJC+7sHaQqsXDDAZN8tAqTG3tvpu3sg3RP6u0pV+j9jyXoTICwngm8PRmNQyvRWH dBn2ut0Kxp5QQfsR79xhxoUezKoF7k78oFvQp+v1JHgZg/Moi3rGUFOyHpox0LhEf9Qk xvdrllv/PCAm+t3GnO0cpHPRi8UernkV2dB/BfEgvYGql2wd5cgygFMakGGZ96wSrHZv QNSCJnzchmtzuzJsmhbftVw13lR4mOM7C6KJVVc29xKtMWbGRtCSdvTzIA/FMSv3LswK ZhHFV9GAWzFNncSjoLe09eZMPvtauXULWx66aglqdJXietnVUUso1gQOXvJ/5Y/UJU6H Hyvw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=i5biSPjpUhFmS7S7cTsjVh9feb56GTutqbWDSkSEiUc=; fh=HNW39XKkv/vvyf1FSHQt1DusXiIxBaqNTZMXjgJBYj0=; b=O5mEjbaiBkaowsUPCCr7TOzcVaSAOyYOnqCpjw0UHOLaZEFApqk2uglRHuA7m2HN8b UXAr/jhK36n/qtvSpVYYebq/jbmUq+44SRSmbLYnuE4oxnkfKTb/pEiY3OMVXI9KTpRA bkrLb27D/YPKZMSkpo2CKFEikoduVW5sXYp1sMjyMyV4bdG01gc/oBApvyTj2euQTKPF CYkLzN2aBj1Saij/mTn8ThPoQmDyI2avwU5FuysqERyf/19JqY1Nijb5ghT28o/2WaY4 suCnFINo62ZaGsvltUJ2564vEIhz9TqiLfw0PMe7mbT+pVcox3tc47DK5lEdtyxcU/4p hKxA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=fYgv3qVb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id lw1-20020a17090b180100b0028596b1c1bbsi3481468pjb.90.2023.11.30.02.34.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 02:34:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=fYgv3qVb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id B776582271A8; Thu, 30 Nov 2023 02:34:07 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235139AbjK3K1b (ORCPT <rfc822;ruipengqi7@gmail.com> + 99 others); Thu, 30 Nov 2023 05:27:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54408 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229550AbjK3K13 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 30 Nov 2023 05:27:29 -0500 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 911F093 for <linux-kernel@vger.kernel.org>; Thu, 30 Nov 2023 02:27:35 -0800 (PST) Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-6cdd4aab5f5so764305b3a.3 for <linux-kernel@vger.kernel.org>; Thu, 30 Nov 2023 02:27:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701340055; x=1701944855; 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=i5biSPjpUhFmS7S7cTsjVh9feb56GTutqbWDSkSEiUc=; b=fYgv3qVbFMyfaQ4OGZd3PacdUdOtlEdT4xl4JB7/LiO9QtKBU2kKOu0Pb5qOgM/cRr 2LeR2bga9nk1SrDntZ5kOzPoTHS8LMA0FZZ/6r+WujTYEBleln191XIR5daS3SE7fNEt g72qEyrdvctd4GAlgX0XW17h5UgyKWK7c1+oRF9I00EJPCenUwrrHYyAPWA4SfvXAFxE sBuEHmPmnfOg/RUR6lntnqopCQNjaBv1DUVrdRdyrNX4Ayz3JX0yg4EBa7Jga8c96Yn5 4+AQJsfxRVz0jPpED4psZjF1LDjkxUADTTzVK9Q87WdTiFN8kr01ut0KyECEgVs331oU 8ohQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701340055; x=1701944855; 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=i5biSPjpUhFmS7S7cTsjVh9feb56GTutqbWDSkSEiUc=; b=dHHFstJdh3pK0tyseSBLcW9TwaVKPmuVSfypuEForEXXQ3AhPscNUXx7W2gIKRvfyi y3oc3TiXrxxodr/EyFOemZ2VI/B1HS6jiZ046KSE3YH7aRyUtvGBYoQrU3B1XD8pXgzi RNPcpHvEzrr/vxF39ufbvmREbZHpCn1uvunlMFgO5QUfeXN/56NglaviO0sxOlV4VgPE mFw2nB18H8vyr3DxQe5jPDJfpUOa0x0MiSmpGrzLH/cneXV99CEt+7YlEtM2auFaBtsG XH28q5GcaGLV/dQpamhjGVnRa5CGiQRnDjNCWDin0HNoEfVsTElkJ5R/+JbohWXw0gRq tB/w== X-Gm-Message-State: AOJu0Yx+0leA55P2bA9OrTFu5HD64QjIzj29ejOuqCJ5PJDemI7gYfAW WwqsfMa5g3wbeae+dWTs/68= X-Received: by 2002:a05:6a00:2d06:b0:6cd:ec64:a886 with SMTP id fa6-20020a056a002d0600b006cdec64a886mr1017096pfb.2.1701340054813; Thu, 30 Nov 2023 02:27:34 -0800 (PST) Received: from XH22050090-L.ad.ts.tri-ad.global ([103.175.111.222]) by smtp.gmail.com with ESMTPSA id p24-20020a62ab18000000b006cb574445efsm874243pff.88.2023.11.30.02.27.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 02:27:34 -0800 (PST) Sender: Vincent Mailhol <vincent.mailhol@gmail.com> From: Vincent Mailhol <mailhol.vincent@wanadoo.fr> To: Andrew Morton <akpm@linux-foundation.org>, linux-kernel@vger.kernel.org, Yury Norov <yury.norov@gmail.com> Cc: Vincent Mailhol <mailhol.vincent@wanadoo.fr>, Nick Desaulniers <ndesaulniers@google.com>, Douglas Anderson <dianders@chromium.org>, Kees Cook <keescook@chromium.org>, Petr Mladek <pmladek@suse.com>, Randy Dunlap <rdunlap@infradead.org>, Zhaoyang Huang <zhaoyang.huang@unisoc.com>, Geert Uytterhoeven <geert+renesas@glider.be>, Marco Elver <elver@google.com> Subject: [PATCH v2] lib: test_bitops: add compile-time optimization/evaluations assertions Date: Thu, 30 Nov 2023 19:27:17 +0900 Message-Id: <20231130102717.1297492-1-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221111081316.30373-1-mailhol.vincent@wanadoo.fr> References: <20221111081316.30373-1-mailhol.vincent@wanadoo.fr> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Thu, 30 Nov 2023 02:34:07 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1749187239429687323 X-GMAIL-MSGID: 1783984767653002585 |
Series |
[v2] lib: test_bitops: add compile-time optimization/evaluations assertions
|
|
Commit Message
Vincent Mailhol
Nov. 30, 2023, 10:27 a.m. UTC
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 function doesn't perform any runtime tests and gets optimized out to
nothing after passing the build assertions.
Architectures which fail that test should adjust their
arch/*/include/asm/bitops.h in order to use the compiler's generic
__builitn implementation if the argument is a constant expression
(similar to [1]).
[1] commit 146034fed6ee ("x86/asm/bitops: Use __builtin_ffs() to evaluate
constant expressions")
Suggested-by: Yury Norov <yury.norov@gmail.com>
Signed-off-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
---
This is tested on x86, x86_64, arm and arm64.
Other architectures were not tested. My idea would be to add this
patch to any kind of CI which runs on all architecture (linux-next?)
and see if anything breaks.
Or maybe I should send a message to the maintainers of all of the
arch/*/include/asm/bitops.h files?
Tell me what you think.
** Changelog **
v1 -> v2:
- Drop the RFC patch. v1 was not ready to be applied on x86 because
of pending changes in arch/x86/include/asm/bitops.h. This was
finally fixed by Nick in commit 3dae5c43badf ("x86/asm/bitops: Use
__builtin_clz{l|ll} to evaluate constant expressions"). Thanks Nick!
- Update the commit description.
- Introduce the test_const_eval() macro to factorize code.
- No functional changes.
Link: https://lore.kernel.org/all/20221111081316.30373-1-mailhol.vincent@wanadoo.fr/
---
lib/Kconfig.debug | 4 ++++
lib/test_bitops.c | 31 +++++++++++++++++++++++++++++++
2 files changed, 35 insertions(+)
Comments
Hi Vincent,
kernel test robot noticed the following build errors:
[auto build test ERROR on akpm-mm/mm-everything]
[also build test ERROR on linus/master v6.7-rc3 next-20231130]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Vincent-Mailhol/lib-test_bitops-add-compile-time-optimization-evaluations-assertions/20231130-182837
base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link: https://lore.kernel.org/r/20231130102717.1297492-1-mailhol.vincent%40wanadoo.fr
patch subject: [PATCH v2] lib: test_bitops: add compile-time optimization/evaluations assertions
config: m68k-allmodconfig (https://download.01.org/0day-ci/archive/20231201/202312010058.JJKeeqvE-lkp@intel.com/config)
compiler: m68k-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231201/202312010058.JJKeeqvE-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202312010058.JJKeeqvE-lkp@intel.com/
All errors (new ones prefixed by >>):
In file included from <command-line>:
In function 'test_bitops_const_eval',
inlined from 'test_bitops_startup' at lib/test_bitops.c:126:2:
>> include/linux/compiler_types.h:435:45: error: call to '__compiletime_assert_183' declared with attribute error: BUILD_BUG_ON failed: !__builtin_constant_p(__test_expr)
435 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
| ^
include/linux/compiler_types.h:416:25: note: in definition of macro '__compiletime_assert'
416 | prefix ## suffix(); \
| ^~~~~~
include/linux/compiler_types.h:435:9: note: in expansion of macro '_compiletime_assert'
435 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
| ^~~~~~~~~~~~~~~~~~~
include/linux/build_bug.h:39:37: note: in expansion of macro 'compiletime_assert'
39 | #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
| ^~~~~~~~~~~~~~~~~~
include/linux/build_bug.h:50:9: note: in expansion of macro 'BUILD_BUG_ON_MSG'
50 | BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
| ^~~~~~~~~~~~~~~~
lib/test_bitops.c:59:9: note: in expansion of macro 'BUILD_BUG_ON'
59 | BUILD_BUG_ON(!__builtin_constant_p(__test_expr)); \
| ^~~~~~~~~~~~
lib/test_bitops.c:74:9: note: in expansion of macro 'test_const_eval'
74 | test_const_eval(__ffs(BIT(n)) == n);
| ^~~~~~~~~~~~~~~
vim +/__compiletime_assert_183 +435 include/linux/compiler_types.h
eb5c2d4b45e3d2 Will Deacon 2020-07-21 421
eb5c2d4b45e3d2 Will Deacon 2020-07-21 422 #define _compiletime_assert(condition, msg, prefix, suffix) \
eb5c2d4b45e3d2 Will Deacon 2020-07-21 423 __compiletime_assert(condition, msg, prefix, suffix)
eb5c2d4b45e3d2 Will Deacon 2020-07-21 424
eb5c2d4b45e3d2 Will Deacon 2020-07-21 425 /**
eb5c2d4b45e3d2 Will Deacon 2020-07-21 426 * compiletime_assert - break build and emit msg if condition is false
eb5c2d4b45e3d2 Will Deacon 2020-07-21 427 * @condition: a compile-time constant condition to check
eb5c2d4b45e3d2 Will Deacon 2020-07-21 428 * @msg: a message to emit if condition is false
eb5c2d4b45e3d2 Will Deacon 2020-07-21 429 *
eb5c2d4b45e3d2 Will Deacon 2020-07-21 430 * In tradition of POSIX assert, this macro will break the build if the
eb5c2d4b45e3d2 Will Deacon 2020-07-21 431 * supplied condition is *false*, emitting the supplied error message if the
eb5c2d4b45e3d2 Will Deacon 2020-07-21 432 * compiler has support to do so.
eb5c2d4b45e3d2 Will Deacon 2020-07-21 433 */
eb5c2d4b45e3d2 Will Deacon 2020-07-21 434 #define compiletime_assert(condition, msg) \
eb5c2d4b45e3d2 Will Deacon 2020-07-21 @435 _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
eb5c2d4b45e3d2 Will Deacon 2020-07-21 436
Hi Vincent, kernel test robot noticed the following build errors: [auto build test ERROR on akpm-mm/mm-everything] [also build test ERROR on linus/master v6.7-rc4 next-20231206] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch#_base_tree_information] url: https://github.com/intel-lab-lkp/linux/commits/Vincent-Mailhol/lib-test_bitops-add-compile-time-optimization-evaluations-assertions/20231130-182837 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything patch link: https://lore.kernel.org/r/20231130102717.1297492-1-mailhol.vincent%40wanadoo.fr patch subject: [PATCH v2] lib: test_bitops: add compile-time optimization/evaluations assertions config: hexagon-allmodconfig (https://download.01.org/0day-ci/archive/20231206/202312062158.60c1e6yt-lkp@intel.com/config) compiler: clang version 17.0.0 (https://github.com/llvm/llvm-project.git 4a5ac14ee968ff0ad5d2cc1ffa0299048db4c88a) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231206/202312062158.60c1e6yt-lkp@intel.com/reproduce) If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags | Reported-by: kernel test robot <lkp@intel.com> | Closes: https://lore.kernel.org/oe-kbuild-all/202312062158.60c1e6yt-lkp@intel.com/ All errors (new ones prefixed by >>): >> lib/test_bitops.c:74:2: error: call to '__compiletime_assert_178' declared with 'error' attribute: BUILD_BUG_ON failed: !__builtin_constant_p(__test_expr) 74 | test_const_eval(__ffs(BIT(n)) == n); | ^ lib/test_bitops.c:59:2: note: expanded from macro 'test_const_eval' 59 | BUILD_BUG_ON(!__builtin_constant_p(__test_expr)); \ | ^ include/linux/build_bug.h:50:2: note: expanded from macro 'BUILD_BUG_ON' 50 | BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition) | ^ include/linux/build_bug.h:39:37: note: expanded from macro 'BUILD_BUG_ON_MSG' 39 | #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg) | ^ note: (skipping 1 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all) include/linux/compiler_types.h:423:2: note: expanded from macro '_compiletime_assert' 423 | __compiletime_assert(condition, msg, prefix, suffix) | ^ include/linux/compiler_types.h:416:4: note: expanded from macro '__compiletime_assert' 416 | prefix ## suffix(); \ | ^ <scratch space>:18:1: note: expanded from here 18 | __compiletime_assert_178 | ^ 1 error generated. vim +74 lib/test_bitops.c 62 63 static void test_bitops_const_eval(void) 64 { 65 /* 66 * On any supported optimization level (-O2, -Os) and if 67 * invoked with a compile-time constant argument, the compiler 68 * must be able to fold into a constant expression all the bit 69 * find functions. Namely: __ffs(), ffs(), ffz(), __fls(), 70 * fls() and fls64(). Otherwise, trigger a build bug. 71 */ 72 const int n = 10; 73 > 74 test_const_eval(__ffs(BIT(n)) == n); 75 test_const_eval(ffs(BIT(n)) == n + 1); 76 test_const_eval(ffz(~BIT(n)) == n); 77 test_const_eval(__fls(BIT(n)) == n); 78 test_const_eval(fls(BIT(n)) == n + 1); 79 test_const_eval(fls64(BIT_ULL(n)) == n + 1); 80 } 81
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..49e2f76575e4 100644 --- a/lib/test_bitops.c +++ b/lib/test_bitops.c @@ -50,6 +50,35 @@ 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); \ +}) + +static void test_bitops_const_eval(void) +{ + /* + * 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. + */ + const int n = 10; + + 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); +} + static int __init test_bitops_startup(void) { int i, bit_set; @@ -94,6 +123,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;