From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 04F2AC36005 for ; Mon, 28 Apr 2025 06:58:55 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.970114.1358878 (Exim 4.92) (envelope-from ) id 1u9IRv-00067n-0O; Mon, 28 Apr 2025 06:58:43 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 970114.1358878; Mon, 28 Apr 2025 06:58:42 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1u9IRu-00067g-Sk; Mon, 28 Apr 2025 06:58:42 +0000 Received: by outflank-mailman (input) for mailman id 970114; Mon, 28 Apr 2025 06:58:41 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1u9IRs-00067a-VB for xen-devel@lists.xenproject.org; Mon, 28 Apr 2025 06:58:40 +0000 Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 3614fbb3-23fe-11f0-9eb4-5ba50f476ded; Mon, 28 Apr 2025 08:58:39 +0200 (CEST) Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sea.source.kernel.org (Postfix) with ESMTP id A48E0435ED; Mon, 28 Apr 2025 06:58:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E5CD1C4CEE4; Mon, 28 Apr 2025 06:58:33 +0000 (UTC) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 3614fbb3-23fe-11f0-9eb4-5ba50f476ded DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745823517; bh=pPJ8R19J2/szrRXylX5/tWCymsrUsn/Hl5OGFoBEBSY=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=dBwmuAg8gdgySu+Dso+50czrM27i9adBTCNGKbFopGertwGmXvg+EXHTGSC/LII3w OOiQvXI6nmTe5mdAul07ryxnGeqbNKU2TJV55XvbjBOkhs+Sf0TAHsFtzzzAnN+El/ 4zfWFWERnPei7BLat2VsFSYo3DGbPWaTo+XG1yMW31vZbiMjOMZr2igKVwaKxRwOCc hA8L8g50+TPIGA6RNsgrXNeH8TuH33QJeDeOmpIszuvwc+j7gtFBgNJMyL3TZYFzE8 1UGB1hQLdXt52S3MeiHESNkgrVuaFhfLCbEgjghtIffhPpyD+KMZNihmVsINSybWxz 5Rin8vh5R2oig== Date: Mon, 28 Apr 2025 08:58:31 +0200 From: Ingo Molnar To: Linus Torvalds Cc: Andrew Cooper , Arnd Bergmann , Arnd Bergmann , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Juergen Gross , Boris Ostrovsky , Alexander Usyskin , Greg Kroah-Hartman , Mateusz =?utf-8?Q?Jo=C5=84czyk?= , Mike Rapoport , Ard Biesheuvel , Peter Zijlstra , linux-kernel@vger.kernel.org, xen-devel@lists.xenproject.org Subject: [PATCH] bitops/32: Convert variable_ffs() and fls() zero-case handling to C Message-ID: References: <20250425141740.734030-1-arnd@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: * Linus Torvalds wrote: > On Sun, 27 Apr 2025 at 12:17, Andrew Cooper wrote: > > > > ffs/fls are commonly found inside loops where x is the loop condition > > too. Therefore, using statically_true() to provide a form without the > > zero compatibility turns out to be a win. > > We already have the version without the zero capability - it's just > called "__ffs()" and "__fls()", and performance-critical code uses > those. > > So fls/ffs are the "standard" library functions that have to handle > zero, and add that stupid "+1" because that interface was designed by > some Pascal person who doesn't understand that we start counting from > 0. > > Standards bodies: "companies aren't sending their best people". > > But it's silly that we then spend effort on magic cmov in inline asm > on those things when it's literally the "don't use this version unless > you don't actually care about performance" case. > > I don't think it would be wrong to just make the x86-32 code just do > the check against zero ahead of time - in C. > > And yes, that will generate some extra code - you'll test for zero > before, and then the caller might also test for a zero result that > then results in another test for zero that can't actually happen (but > the compiler doesn't know that). But I suspect that on the whole, it > is likely to generate better code anyway just because the compiler > sees that first test and can DTRT. > > UNTESTED patch applied in case somebody wants to play with this. It > removes 10 lines of silly code, and along with them that 'cmov' use. > > Anybody? Makes sense - it seems to boot here, but I only did some very light testing. There's a minor text size increase on x86-32 defconfig, GCC 14.2.0: text data bss dec hex filename 16577728 7598826 1744896 25921450 18b87aa vmlinux.before 16577908 7598838 1744896 25921642 18b886a vmlinux.after bloatometer output: add/remove: 2/1 grow/shrink: 201/189 up/down: 5681/-3486 (2195) Patch with changelog and your SOB added attached. Does it look good to you? Thanks, Ingo ================> From: Linus Torvalds Date: Mon, 28 Apr 2025 08:38:35 +0200 Subject: [PATCH] bitops/32: Convert variable_ffs() and fls() zero-case handling to C Don't do the complicated and probably questionable BS*L+CMOVZL asm() optimization in variable_ffs() and fls(): performance-critical code is already using __ffs() and __fls() that use sane interfaces close to the machine instruction ABI. Check ahead for zero in C. There's a minor text size increase on x86-32 defconfig: text data bss dec hex filename 16577728 7598826 1744896 25921450 18b87aa vmlinux.before 16577908 7598838 1744896 25921642 18b886a vmlinux.after bloatometer output: add/remove: 2/1 grow/shrink: 201/189 up/down: 5681/-3486 (2195) Signed-off-by: Linus Torvalds Signed-off-by: Ingo Molnar --- arch/x86/include/asm/bitops.h | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/arch/x86/include/asm/bitops.h b/arch/x86/include/asm/bitops.h index 100413aff640..6061c87f14ac 100644 --- a/arch/x86/include/asm/bitops.h +++ b/arch/x86/include/asm/bitops.h @@ -321,15 +321,10 @@ static __always_inline int variable_ffs(int x) asm("bsfl %1,%0" : "=r" (r) : ASM_INPUT_RM (x), "0" (-1)); -#elif defined(CONFIG_X86_CMOV) - asm("bsfl %1,%0\n\t" - "cmovzl %2,%0" - : "=&r" (r) : "rm" (x), "r" (-1)); #else - asm("bsfl %1,%0\n\t" - "jnz 1f\n\t" - "movl $-1,%0\n" - "1:" : "=r" (r) : "rm" (x)); + if (!x) + return 0; + asm("bsfl %1,%0" : "=r" (r) : "rm" (x)); #endif return r + 1; } @@ -378,15 +373,10 @@ static __always_inline int fls(unsigned int x) asm("bsrl %1,%0" : "=r" (r) : ASM_INPUT_RM (x), "0" (-1)); -#elif defined(CONFIG_X86_CMOV) - asm("bsrl %1,%0\n\t" - "cmovzl %2,%0" - : "=&r" (r) : "rm" (x), "rm" (-1)); #else - asm("bsrl %1,%0\n\t" - "jnz 1f\n\t" - "movl $-1,%0\n" - "1:" : "=r" (r) : "rm" (x)); + if (!x) + return 0; + asm("bsrl %1,%0" : "=r" (r) : "rm" (x)); #endif return r + 1; }