Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp343736pxu; Thu, 7 Jan 2021 06:33:49 -0800 (PST) X-Google-Smtp-Source: ABdhPJw4xEDNCOmlJlY8PUEO2icAkqTVNoxynZrYwzMdy+wY9/w65tFWNkwmZD01hJE8tCGI8w1l X-Received: by 2002:a17:906:ae4e:: with SMTP id lf14mr6848290ejb.310.1610030029485; Thu, 07 Jan 2021 06:33:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610030029; cv=none; d=google.com; s=arc-20160816; b=XfCE09o6yv/4qneIOmVgEaZQM8qLiDlUPIwDKlqqQYkMX3LQv4RveiDKnCLsMFrpX3 BiLZacNG4iVaG/KV2M4qMdiUJCjp/ALQVKTmyxnXXwmO4wZLScJiReYbxBzy6U0PLoEw ZCeEYk/JamNg/TVSnX45rIy+M4zZEPKUkWKzybdiBExx2v1F5Q/FJ9+3+TtXks/sgx9m aWH0JgH5HjAZRzcfBH1cl1F8QMYlR1BWQqcdwursPCs3dqnDeSvQnz+O79wHqPGSlK9i SuO7sZ9viJMv3/pTiJJcTlbYsTMaBr399lxwIVAE9CsPpYjoWsbO9xGEdrCWFWcAgvMX MrTA== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=gQZ5m8RhE1l3YAMEBOZmPxyorotwgafbpZHzpfaQndQ=; b=Kvoi1bC4LLQTczOgEgzMe40X3qFNtF3+Rp9bUkKDb1R4KNSAfikmiN8HBpknMQbvrr q2+itqRlX/Q32Nv+rosFm84iiEOOv+dTI6BoCWzjmAGI7gOr3KlMAIh5Rs0S8/McqpMw nnheGUPtikd/jXvEKmtee4WedV1aDcD+07ZeYjf5AntFayt+ZHL6WvoDYloxtsvhZOVx Fpv98s/Q4YWbpbAzscVzrdVTyp6PTzk8MURJY2iFkBJE5q0fGvSAXLP8gFW+oipKJyfO Zgv26vOmYtV2TQpQ4t9p4kTMfSNOdDGBemyOrZNRz9MJCM+lZN/VSk+ElnaJ11TrrWrj TtMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="P06p/uGF"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id lu18si2257851ejb.272.2021.01.07.06.33.25; Thu, 07 Jan 2021 06:33:49 -0800 (PST) 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=@linuxfoundation.org header.s=korg header.b="P06p/uGF"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728765AbhAGObY (ORCPT + 99 others); Thu, 7 Jan 2021 09:31:24 -0500 Received: from mail.kernel.org ([198.145.29.99]:45348 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728630AbhAGObX (ORCPT ); Thu, 7 Jan 2021 09:31:23 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 2092823355; Thu, 7 Jan 2021 14:30:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1610029838; bh=/cUAJkHsZFcvwbqWxhVQwEO1V4E8nA+8vHJiQIFaVV0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=P06p/uGF7TiskFrcwZfN5XYEqtXPyRz91/aA9LDO7hEVHk9LGa0GYYxzfpIItX9Qe 4DEsiXUOL8uiPANKokKIYPHn7iM4vShulj5vxXbehiYJ9ObVn52Qiq4Y2bjyKgwJ4L EC/2mP4u3nw7sXA0yEXcPh2tFUpei0Yn5C4zc/+8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Christophe Leroy , Segher Boessenkool , Michael Ellerman , Sasha Levin Subject: [PATCH 4.14 12/29] powerpc/bitops: Fix possible undefined behaviour with fls() and fls64() Date: Thu, 7 Jan 2021 15:31:27 +0100 Message-Id: <20210107143054.693870389@linuxfoundation.org> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210107143052.973437064@linuxfoundation.org> References: <20210107143052.973437064@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Christophe Leroy [ Upstream commit 1891ef21d92c4801ea082ee8ed478e304ddc6749 ] fls() and fls64() are using __builtin_ctz() and _builtin_ctzll(). On powerpc, those builtins trivially use ctlzw and ctlzd power instructions. Allthough those instructions provide the expected result with input argument 0, __builtin_ctz() and __builtin_ctzll() are documented as undefined for value 0. The easiest fix would be to use fls() and fls64() functions defined in include/asm-generic/bitops/builtin-fls.h and include/asm-generic/bitops/fls64.h, but GCC output is not optimal: 00000388 : 388: 2c 03 00 00 cmpwi r3,0 38c: 41 82 00 10 beq 39c 390: 7c 63 00 34 cntlzw r3,r3 394: 20 63 00 20 subfic r3,r3,32 398: 4e 80 00 20 blr 39c: 38 60 00 00 li r3,0 3a0: 4e 80 00 20 blr 000003b0 : 3b0: 2c 03 00 00 cmpwi r3,0 3b4: 40 82 00 1c bne 3d0 3b8: 2f 84 00 00 cmpwi cr7,r4,0 3bc: 38 60 00 00 li r3,0 3c0: 4d 9e 00 20 beqlr cr7 3c4: 7c 83 00 34 cntlzw r3,r4 3c8: 20 63 00 20 subfic r3,r3,32 3cc: 4e 80 00 20 blr 3d0: 7c 63 00 34 cntlzw r3,r3 3d4: 20 63 00 40 subfic r3,r3,64 3d8: 4e 80 00 20 blr When the input of fls(x) is a constant, just check x for nullity and return either 0 or __builtin_clz(x). Otherwise, use cntlzw instruction directly. For fls64() on PPC64, do the same but with __builtin_clzll() and cntlzd instruction. On PPC32, lets take the generic fls64() which will use our fls(). The result is as expected: 00000388 : 388: 7c 63 00 34 cntlzw r3,r3 38c: 20 63 00 20 subfic r3,r3,32 390: 4e 80 00 20 blr 000003a0 : 3a0: 2c 03 00 00 cmpwi r3,0 3a4: 40 82 00 10 bne 3b4 3a8: 7c 83 00 34 cntlzw r3,r4 3ac: 20 63 00 20 subfic r3,r3,32 3b0: 4e 80 00 20 blr 3b4: 7c 63 00 34 cntlzw r3,r3 3b8: 20 63 00 40 subfic r3,r3,64 3bc: 4e 80 00 20 blr Fixes: 2fcff790dcb4 ("powerpc: Use builtin functions for fls()/__fls()/fls64()") Cc: stable@vger.kernel.org Signed-off-by: Christophe Leroy Acked-by: Segher Boessenkool Signed-off-by: Michael Ellerman Link: https://lore.kernel.org/r/348c2d3f19ffcff8abe50d52513f989c4581d000.1603375524.git.christophe.leroy@csgroup.eu Signed-off-by: Sasha Levin --- arch/powerpc/include/asm/bitops.h | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/arch/powerpc/include/asm/bitops.h b/arch/powerpc/include/asm/bitops.h index b750ffef83c7d..0ec93d940d12c 100644 --- a/arch/powerpc/include/asm/bitops.h +++ b/arch/powerpc/include/asm/bitops.h @@ -220,15 +220,34 @@ static __inline__ void __clear_bit_unlock(int nr, volatile unsigned long *addr) */ static __inline__ int fls(unsigned int x) { - return 32 - __builtin_clz(x); + int lz; + + if (__builtin_constant_p(x)) + return x ? 32 - __builtin_clz(x) : 0; + asm("cntlzw %0,%1" : "=r" (lz) : "r" (x)); + return 32 - lz; } #include +/* + * 64-bit can do this using one cntlzd (count leading zeroes doubleword) + * instruction; for 32-bit we use the generic version, which does two + * 32-bit fls calls. + */ +#ifdef CONFIG_PPC64 static __inline__ int fls64(__u64 x) { - return 64 - __builtin_clzll(x); + int lz; + + if (__builtin_constant_p(x)) + return x ? 64 - __builtin_clzll(x) : 0; + asm("cntlzd %0,%1" : "=r" (lz) : "r" (x)); + return 64 - lz; } +#else +#include +#endif #ifdef CONFIG_PPC64 unsigned int __arch_hweight8(unsigned int w); -- 2.27.0