Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4257366pxj; Wed, 12 May 2021 01:17:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyS6jkEgVZjzyt+XGkvxVv9En8eDpWJsSBRmpCBs1cO941TPR9LmFSvgQXoDnyhW8iZDOLZ X-Received: by 2002:a17:906:9892:: with SMTP id zc18mr853992ejb.354.1620807429995; Wed, 12 May 2021 01:17:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620807429; cv=none; d=google.com; s=arc-20160816; b=JXnhf+003Z2hD0h5S5er8WvFJgmgobdAMbQpRV18c/zEdLwxg8KVEHhYCCiCtSF+FK KvKwJoNinJ5YIaEipErp013Lzy47aOdxWMf3O/HWemuWSSUlgCOTP5+E2SXsFHAZmm4B X3/JpEKnI1RAS3zd6Hn2BLH/l0AWY9I7lupVWQl0iWKBcvb1/tR6lwQuz9E5e8Hl73Ry VoFvzKqqNscxuuEvTCGPPQtoRWlCMGnQxgSMYokqhNJx7OzmU8stN1w4+cYv7PmUEQyJ D6o1Eq3AEA9s+ncJRl2sv3vAsVSbvO71DTDbjf06Ak11Q+fRQk+3AMB7tekRf86G6rCm lyFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=ofYbiesQgwQaEexlewrml7cRxApQkWB2Nx460iDcnyo=; b=NHzWejc4ltTTETS182ooj3bbrgbcpDCzADrje31MYnKZSu+OxMBpQpaD6jyt/41OLz BGPkljE8yPenvWFQw8ui3L/xl4edtlnyke0XF1pwsEGVdwBC5QnoOuYAc3eL/8M5ynev qLRnfOSep9X7kz/5Bu8oZV9fKSCCBgJeP7Z3kT5wZf09vtWW/bPlIU9fnhOStycHGxOY L2rMJra1XEm3fJRg79b/UNNNjB3gb4WBnHhD9M2VHFNnu4kvzs3TMeojpNCC5eIZPvsp PYjqFkdD1UQ7uQ508+0+qd2HuvyDQofN/5JvERfT1dowrX0qSSXWrTI03IcMpRZJs9OF I+og== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=cHIOWKyz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j5si19253034edj.274.2021.05.12.01.16.46; Wed, 12 May 2021 01:17:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=cHIOWKyz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230272AbhELIQu (ORCPT + 99 others); Wed, 12 May 2021 04:16:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34196 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230026AbhELIQt (ORCPT ); Wed, 12 May 2021 04:16:49 -0400 Received: from mail-ej1-x62e.google.com (mail-ej1-x62e.google.com [IPv6:2a00:1450:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 01EE2C06175F for ; Wed, 12 May 2021 01:15:41 -0700 (PDT) Received: by mail-ej1-x62e.google.com with SMTP id m12so33714346eja.2 for ; Wed, 12 May 2021 01:15:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=ofYbiesQgwQaEexlewrml7cRxApQkWB2Nx460iDcnyo=; b=cHIOWKyzF0AfFHiktnENLignESiqbB75M/2S8bNYGwb4UmCWst1kzfVRtHWBUILKXs c7eSZ9hehTk8NLHKdrT9XYNXDarjEYNKlTulZQDt9ENuebCRNglJRbSanTXy8bx1NlVm YW7PRTEc6U7mjbRYuKiT9exW6nbvZazhgiCk4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=ofYbiesQgwQaEexlewrml7cRxApQkWB2Nx460iDcnyo=; b=icn4k/NniZdk1G5uWTHlGH0ozqlgWkZ/J1fUPn1GU+3z0JBG9bOBSNaoPvwgMfVfoi Lcvo4IOhGQG4AQ8+WZWKelNQHrl3xk2KWTeBpixCZxc2P1WJIbyXQwIJoimoRtqB5C/Y 1KcXrThl64tfPS9OlQGM7RoKpLoVyLAnHBjnPPTyR3XukZegUPts1ZG286MNhdmZ29cv x00YxoQE0VYkkDs6v5y6LXNTWvVbtyjB8GccrroZZajRZA1Wr6weSQhm6bexvt+C8pSH JBtGAq2GT0Os7S0qNg8zk4h3QtqJNTd7JO8iViT2RyZV9dLiA9eohRZIr0O74lHDfpVw 57rg== X-Gm-Message-State: AOAM530dzLl2nXtWTrGzDCLdIMuzHeI0bI7gfoLu5BIY0G2gy+AULXAe qEEv7/N9mgeq4eKqMB/HckcCpg== X-Received: by 2002:a17:906:f42:: with SMTP id h2mr37200326ejj.317.1620807339603; Wed, 12 May 2021 01:15:39 -0700 (PDT) Received: from [192.168.1.149] ([80.208.71.248]) by smtp.gmail.com with ESMTPSA id um28sm13885567ejb.63.2021.05.12.01.15.37 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 12 May 2021 01:15:39 -0700 (PDT) Subject: Re: [PATCH 11/12] tools: sync lib/find_bit implementation To: Arnd Bergmann , Rikard Falkeborn Cc: Tetsuo Handa , Andy Shevchenko , Yury Norov , Linux Kernel Mailing List , Andrew Morton , linux-m68k , Linux-Arch , Linux-SH , Alexey Klimov , David Sterba , Dennis Zhou , Geert Uytterhoeven , Jianpeng Ma , Joe Perches , John Paul Adrian Glaubitz , Josh Poimboeuf , Rich Felker , Stefano Brivio , Wei Yang , Wolfram Sang , Yoshinori Sato References: <20210401003153.97325-1-yury.norov@gmail.com> <20210401003153.97325-12-yury.norov@gmail.com> <1ac7bbc2-45d9-26ed-0b33-bf382b8d858b@I-love.SAKURA.ne.jp> <151de51e-9302-1f59-407a-e0d68bbaf11c@i-love.sakura.ne.jp> From: Rasmus Villemoes Message-ID: <030ae370-967c-22d4-56f8-cb0435be7540@rasmusvillemoes.dk> Date: Wed, 12 May 2021 10:15:37 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 12/05/2021 09.48, Arnd Bergmann wrote: > On Tue, May 11, 2021 at 10:39 PM Rikard Falkeborn > wrote: >> On Tue, May 11, 2021 at 08:53:53PM +0900, Tetsuo Handa wrote: > >>> #define GENMASK_INPUT_CHECK(h, l) \ >>> (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \ >>> __builtin_constant_p((l) > (h)), (l) > (h), 0))) >>> >>> __GENMASK() does not need "h" and "l" being a constant. >>> >>> Yes, small_const_nbits(size) in find_next_bit() can guarantee that "size" is a >>> constant and hence "h" argument in GENMASK_INPUT_CHECK() call is also a constant. >>> But nothing can guarantee that "offset" is a constant, and hence nothing can >>> guarantee that "l" argument in GENMASK_INPUT_CHECK() call is also a constant. >>> >>> Then, how can (l) > (h) in __builtin_constant_p((l) > (h)) be evaluated at build time >>> if either l or h (i.e. "offset" and "size - 1" in find_next_bit()) lacks a guarantee of >>> being a constant? >>> >> >> So the idea is that if (l > h) is constant, __builtin_constant_p should >> evaluate that, and if it is not it should use zero instead as input to >> __builtin_chose_expr(). This works with non-const inputs in many other >> places in the kernel, but apparently in this case with a certain >> compiler, it doesn't so I guess we need to work around it. > > I have a vague memory that __builtin_constant_p() inside of > __builtin_choose_expr() > always evaluates to false because of the order in which the compiler processes > those: If constant-folding only happens after __builtin_choose_expr(), then > __builtin_constant_p() has to be false. It's more complicated than that. __builtin_constant_p on something which is a bona-fide Integer Constant Expression (ICE) gets folded early to a 1. And then it turns out that such a __builtin_constant_p() that folds early to a 1 can be "stronger" than a literal 1, in the sense that when used as the controlling expression of a ?: with nonsense in the false branch, the former is OK but the latter fails: https://lore.kernel.org/lkml/c68a0f46-346c-70a0-a9b8-31747888f05f@rasmusvillemoes.dk/ Now what happens when the argument to __builtin_constant_p is not an ICE is a lot more complicated. The argument _may_ be so obviously non-constant that it can be folded early to a 0, hence still be suitable as first argument to __b_c_e. But it is also possible that the compiler leaves it unevaluated, in the "hope" that a later optimization stage could prove the argument constant. And that's the case where __b_c_e will then break, because that can't be left unevaluated for very long - the very _type_ of the result depends on which branch is chosen. tl;dr: there's no "order in which the compiler processes those", __b_c_p can get evaluated (folded) early, before __b_c_e inspects it, or be left for later stages. Rasmus