Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp638535iog; Fri, 17 Jun 2022 10:13:07 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vVl31nOOQor8g3UEGN8eyMq0d7wG3I7GRmOtToqoGK1+fzJpL4yihPSNhAtPhSDYxXbasG X-Received: by 2002:a17:906:a219:b0:6e4:86a3:44ea with SMTP id r25-20020a170906a21900b006e486a344eamr10367845ejy.385.1655485986807; Fri, 17 Jun 2022 10:13:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655485986; cv=none; d=google.com; s=arc-20160816; b=goHmnkp4U4XNLseF6nUWT4c4nfJVnpef9eanuL3vBGzHwie5zdzJjh8sLlw3vmstKI B7pZYCD25LIdxIO/MJeVa1ZBz6MAuDxafZzyV24QeFbjhXOzxiVgKzlODdThvpLIUDZf i0wKjyD7m8DSJam5hQWDAPl/Enp/zjcNMy3H3Ou3201TkzcFe6EtggyIoR558Lpz1Q6E 7glEGBUGbe1x9bUWpcxXb1a2gjf334bytvb4mIa8yYk/U5M5nGVPFg65kGFGaNjqxk3b kvAABRyhm1eYKf2HWPCqtCHmlPoMlo9izPRVpxqn3YnR/6yOLaAKGcgD6pGSI32KK4O5 zXBg== 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 :dkim-signature; bh=o+4teGHk/iRPQmT2upK5BJs6QoR4iz9/X86cJ/Lho9c=; b=ZgftEZj/i/PHCKDedr4wcfTrIvTWrrQZSRmvEzDcloXBQtHVP9kkj6UzA0ayk3PlCA ugIAKwVCsMlvQzR2+i8CgZQPTDtJzobSgrQoa97hm9HJ6LbGqqM4nDMUcbA9utuuGiTN hxvFXLAlBcHC2s/cmlxCB3ze+D7ZywM17gopsqWNNZ/oNL27DT/hL0kdPkjCFdQ315cU n3NhTcH5U973nefBPCp2D6joSv77S4/ayGhoQZVlduAe6C/XdzcVqVF5fTvcQ6Gw+wRL NAynJ9BlZ9S9WL8JK0/5xby4VioTM9sD7IP6lnatVmSYvh03ts+8L88p2+ucv2g7VbHP O5AA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=WTjWhq1w; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id eb8-20020a0564020d0800b004355a352960si1861697edb.422.2022.06.17.10.12.41; Fri, 17 Jun 2022 10:13:06 -0700 (PDT) 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; dkim=pass header.i=@intel.com header.s=Intel header.b=WTjWhq1w; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1383540AbiFQQxg (ORCPT + 99 others); Fri, 17 Jun 2022 12:53:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57596 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234889AbiFQQxO (ORCPT ); Fri, 17 Jun 2022 12:53:14 -0400 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 483705D191; Fri, 17 Jun 2022 09:52:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1655484722; x=1687020722; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=RrGMYQ9AwVKb0nm3YeK2st1qMloseK6LRdC/eA92LbY=; b=WTjWhq1weTOUjK7ieqCpSvXuVZgWoStR/uL7Oatz2TZN5WEBuVX5Tpq/ ZIzOJHWiYy1v/n7vpglTffPNDIcbmIYO37hgEddKzslFtky4S3bxPY+Zx ZVt2G7ajgKbHBbGl14z58waBPqY3Vzus/1T+VLPjD2twuEhZ88HUIRUnM alkMYVUvo0/2OoZI0XKH1sfgudRh81nYkQdmFz0iCPiNu/ZUsxTrB7J6n 4rCt2wF/7MsOpGxGrgO5WfMBka+LDwcb/q9wT/S0rkCvSTfV2H3+WTGNa FntjeR0LsoKGgnDSqVPhfEgQyqVVKWL4nLswMcVL7YQMIC4JGRNu0CyAe A==; X-IronPort-AV: E=McAfee;i="6400,9594,10380"; a="279540803" X-IronPort-AV: E=Sophos;i="5.92,306,1650956400"; d="scan'208";a="279540803" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jun 2022 07:41:08 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.92,306,1650956400"; d="scan'208";a="642050130" Received: from irvmail001.ir.intel.com ([10.43.11.63]) by fmsmga008.fm.intel.com with ESMTP; 17 Jun 2022 07:40:48 -0700 Received: from newjersey.igk.intel.com (newjersey.igk.intel.com [10.102.20.203]) by irvmail001.ir.intel.com (8.14.3/8.13.6/MailSET/Hub) with ESMTP id 25HEeXl6024161; Fri, 17 Jun 2022 15:40:42 +0100 From: Alexander Lobakin To: Arnd Bergmann , Yury Norov Cc: Alexander Lobakin , Andy Shevchenko , Mark Rutland , Matt Turner , Brian Cain , Geert Uytterhoeven , Yoshinori Sato , Rich Felker , "David S. Miller" , Kees Cook , "Peter Zijlstra (Intel)" , Marco Elver , Borislav Petkov , Tony Luck , Maciej Fijalkowski , Jesse Brandeburg , Greg Kroah-Hartman , linux-alpha@vger.kernel.org, linux-hexagon@vger.kernel.org, linux-ia64@vger.kernel.org, linux-m68k@lists.linux-m68k.org, linux-sh@vger.kernel.org, sparclinux@vger.kernel.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 4/7] bitops: define const_*() versions of the non-atomics Date: Fri, 17 Jun 2022 16:40:28 +0200 Message-Id: <20220617144031.2549432-5-alexandr.lobakin@intel.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220617144031.2549432-1-alexandr.lobakin@intel.com> References: <20220617144031.2549432-1-alexandr.lobakin@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE 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 Define const_*() variants of the non-atomic bitops to be used when the input arguments are compile-time constants, so that the compiler will be always able to resolve those to compile-time constants as well. Those are mostly direct aliases for generic_*() with one exception for const_test_bit(): the original one is declared atomic-safe and thus doesn't discard the `volatile` qualifier, so in order to let optimize code, define it separately disregarding the qualifier. Add them to the compile-time type checks as well just in case. Suggested-by: Marco Elver Signed-off-by: Alexander Lobakin --- .../asm-generic/bitops/generic-non-atomic.h | 31 +++++++++++++++++++ include/linux/bitops.h | 1 + 2 files changed, 32 insertions(+) diff --git a/include/asm-generic/bitops/generic-non-atomic.h b/include/asm-generic/bitops/generic-non-atomic.h index b85b8a2ac239..3d5ebd24652b 100644 --- a/include/asm-generic/bitops/generic-non-atomic.h +++ b/include/asm-generic/bitops/generic-non-atomic.h @@ -127,4 +127,35 @@ generic_test_bit(unsigned long nr, const volatile unsigned long *addr) return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1))); } +/* + * const_*() definitions provide good compile-time optimizations when + * the passed arguments can be resolved at compile time. + */ +#define const___set_bit generic___set_bit +#define const___clear_bit generic___clear_bit +#define const___change_bit generic___change_bit +#define const___test_and_set_bit generic___test_and_set_bit +#define const___test_and_clear_bit generic___test_and_clear_bit +#define const___test_and_change_bit generic___test_and_change_bit + +/** + * const_test_bit - Determine whether a bit is set + * @nr: bit number to test + * @addr: Address to start counting from + * + * A version of generic_test_bit() which discards the `volatile` qualifier to + * allow a compiler to optimize code harder. Non-atomic and to be called only + * for testing compile-time constants, e.g. by the corresponding macros, not + * directly from "regular" code. + */ +static __always_inline bool +const_test_bit(unsigned long nr, const volatile unsigned long *addr) +{ + const unsigned long *p = (const unsigned long *)addr + BIT_WORD(nr); + unsigned long mask = BIT_MASK(nr); + unsigned long val = *p; + + return !!(val & mask); +} + #endif /* __ASM_GENERIC_BITOPS_GENERIC_NON_ATOMIC_H */ diff --git a/include/linux/bitops.h b/include/linux/bitops.h index 87087454a288..d393297287d5 100644 --- a/include/linux/bitops.h +++ b/include/linux/bitops.h @@ -37,6 +37,7 @@ extern unsigned long __sw_hweight64(__u64 w); /* Check that the bitops prototypes are sane */ #define __check_bitop_pr(name) \ static_assert(__same_type(arch_##name, generic_##name) && \ + __same_type(const_##name, generic_##name) && \ __same_type(name, generic_##name)) __check_bitop_pr(__set_bit); -- 2.36.1