Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp9104709pxu; Mon, 28 Dec 2020 06:45:23 -0800 (PST) X-Google-Smtp-Source: ABdhPJw3auzrbAv25lIvW36VGj6TubRjr02gfffuCL0+iLYHDdse1H5IYtFg2D0N1djB1QsreprQ X-Received: by 2002:a17:906:f1c8:: with SMTP id gx8mr42594509ejb.524.1609166722836; Mon, 28 Dec 2020 06:45:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609166722; cv=none; d=google.com; s=arc-20160816; b=aXYvWAzZsPCYhMkjbNhM7ujMLumFzdgb5twsTOubP6vy6YQirUtzbnYT4W3nwfeYYe B3UEUWJAtKzyJPnKgMcwjS9YNdBmLGOQb+n1F54z6Snjzqy4YHZI/x6Z4nb8QnsVLlFQ pMUo6MRTEF8TUBg1DYtVqLn+jdQ/1ZfZ85Hjuq2PngMMHhmVeEZDUx5h9i7FF/HRUIxJ TJSfh2pYw2bPKhmXJZxdXl+YXuW1YfVwHkK5w8zV33K6LCRJpxH3jHYcoggstAeZ07r9 YIovTpMeWhg8VQGTNiOGCIHhvGOMqEx8McGDTTRC1qWhlh/RzN9BV9/cMNFvvBeoNIcP 3vbQ== 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=Z/NZWP53DYg8Ff0hyr3Gks7sEG31Qb4jdIF62fXYtsY=; b=POLeYxPKFCZ6SP8Csuo9ptaNvn+4SUNCr5+oHUi2cE7aY2anry9wmESBhBWXpTCDCc aJgJ+awJfKICzNthtlQt4/9CQe1i6r6ykP0wWwjlhWlsZriB5jco4uHMVMJQCjm2v/1y 4Mvpq3QjP+ortJpQKf/GBZaYzz+VLjsTNtr5d3SAA+Qn6/1LFGsswShqSVXwF5n7Oc5X a6uPcsisq6DqMAL5xpcnYaIRmMKu73CRvy7JHRTMUR6hDJH+6stg7D6FtPtbevZZmZGh CSRQmnxvRzDKV8WLGmgSA03jRWr6zJe32x35F16imovpLmpvDYZgiYmUcBUusSpUIisz NqfQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=CF8mfltc; 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 a1si19038415ejd.577.2020.12.28.06.45.00; Mon, 28 Dec 2020 06:45:22 -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=CF8mfltc; 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 S2505821AbgL1Oko (ORCPT + 99 others); Mon, 28 Dec 2020 09:40:44 -0500 Received: from mail.kernel.org ([198.145.29.99]:36314 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2502550AbgL1O2q (ORCPT ); Mon, 28 Dec 2020 09:28:46 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 1C0EF207B2; Mon, 28 Dec 2020 14:28:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1609165710; bh=U1Z0flzWJBHH9gQy1+JgIvX8wmjjFX+LGw6oUg225xo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CF8mfltcnz5XFdE5hmSDcnxeWBAP8WfIEqSOvAbWku6XjcmeAxKgxuQuxpoSt6Dq+ Ce+qe+8Aw1ihjivYL9YisR+P7h+Ak6/+AsHhFzxAYTZKtTJtB5n2ax2j+WE2XJg3xN hFOCD8MVyEoaOPRqYxZxmqsv4ud8PNKIRhQ5FF8A= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Christophe Leroy , Segher Boessenkool , Michael Ellerman Subject: [PATCH 5.10 610/717] powerpc/bitops: Fix possible undefined behaviour with fls() and fls64() Date: Mon, 28 Dec 2020 13:50:08 +0100 Message-Id: <20201228125050.144130251@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201228125020.963311703@linuxfoundation.org> References: <20201228125020.963311703@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 commit 1891ef21d92c4801ea082ee8ed478e304ddc6749 upstream. 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: Greg Kroah-Hartman --- arch/powerpc/include/asm/bitops.h | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) --- a/arch/powerpc/include/asm/bitops.h +++ b/arch/powerpc/include/asm/bitops.h @@ -216,15 +216,34 @@ static inline void arch___clear_bit_unlo */ 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);