From nobody Wed Feb 11 05:02:15 2026 Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 70C17363C77 for ; Fri, 9 Jan 2026 16:38:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767976687; cv=none; b=vDgl2TD9oPSeibRC+p8y0E0GkkOZOHG+cm3JJrNQZ+jiqTQKwco/ULXPT/r/JzlBNaJ3+6HXylEuW7cZs2wbstMQRE6nuJRErMQcPZ/EOkI7Xy2lFzc96f+t0GDb0vQ2Pe+9nvPiU03w2zn8wMZUQ047HjO6JMGQi4CbPf6ibiw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767976687; c=relaxed/simple; bh=JPp51UYXAdu5h5TZ9iFTxsw1s8VmAi+Cj1InFGSHlNE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NNizK/RkTWmAbWCsCdcWHT4O3qq4vUdfopdfjWt4LdzNYeDhGieqKV8e/S93x1i6JF+nV3VWTqiW1i7pDsZHR8cLUTYZ1byJzXWmxjBIKS/SskzagSR9GlCDkYE/4nfWJJLiq+mxQLX0XkbyicqMKbIb3mei3FVU2BwTjGA9iyk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b=LZMcJ4M4; arc=none smtp.client-ip=209.85.128.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b="LZMcJ4M4" Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-4775dbde730so4054165e9.3 for ; Fri, 09 Jan 2026 08:38:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1767976682; x=1768581482; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=TOsS0o5WhXmlABnZ+SEM7YDF3bMjaJp0tqP8URi3YSE=; b=LZMcJ4M4EfNGV+KtFK4KXvEFqmS+Siej4QFqhQvR8phxBHT1ZJCXyegA/nB70DlPWz cc98mNLwlKIf+nPOpIi/Od1esNP8P/Iy+pq8FxpZ8fTM4f5aJBJP5jxvM71a5OPlfg61 4U4PB6fWLXlxMda+MsXwWxA5c/gGilcHa5JX5Wa7wmkMLXGazhuppis+OxByDg201Ll5 GbKon8NCkcxxcgxvG8Omte29//j64ZZXKstWL6/vWjLX+OtlkHFzB+i8fGEG1ThV8BmR vpAoOfHxDN3bmUYkft92hAg0I24n7tEj4AoJkrl9fYre5x93laYDddNoS0PC5XPNvpvK oqVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767976682; x=1768581482; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=TOsS0o5WhXmlABnZ+SEM7YDF3bMjaJp0tqP8URi3YSE=; b=ornO/bZUIqPcb2sigujeXGQ+YbheK5wrHRlzZBVeaqqVVKbI2nqKgfzGP1xFIzQnci KU0bDgDrUMDPhyhef88o7y7UWSYK3wgI8khN2GXGuzzY1zGsJ6manyNxhBCA35AQyKuy aRE7iaewigoCzAFcf3zQIgoAMLxqnmsp5Oum4mJE1+3cORse6M2ynx/Xs/e+ettnW6m/ fyVr0KNSRD23TI2Pmdvt9T4GO4v/1/YmRiEkO7ia6Tiu8T0Z5aJHvhY00h2q2gOtji+L 0sseBBcxRIbjTBK1uv8XOAisUjzwynzgi8CzuZO9KDjGZaDqJIMIUE3a7bESNTpO+/VB EoMQ== X-Forwarded-Encrypted: i=1; AJvYcCUh4XMuygfwShts5rVBvN5law+AxcHMlOXQhKxF6d/3d7NIT68Gb5cGdbDUyDOwVS4K/asXvuqYtX1V89Q=@vger.kernel.org X-Gm-Message-State: AOJu0YybDPPj+2eRVYLtzRD95vcOqyFhU1T+CbHunkusoZEwE/T91v+0 WReQ6eKY0GSLSPTVS9WfSfZhmWSXuLhnLMv7zByIQojvpe3DQc9tcyX30oI3vTbOkLc= X-Gm-Gg: AY/fxX6aPe3TrgrdeziyeBEU/k/a3RhsR3UPwTLo1IvNw4R44LNpf65CcOC7fiR+fNj B/ZioY51ufTtYJxnzq71trBjtHQ5UVfbpixFU8g+3kwOLIY0FK69eqC95c2Egl6n07v794W2eTv 0X7LBZ3M+IV3UN7u8P14eUWMECK7U/DXgOfseoSRftzW74xOwUdvPtprzXzebn1EUQtK7Gre8FB 29+zD6s4HvNeTKZDJck6z8aUuea8fBiyQk6u+jdqJMSd9kujpRwkTiS37HRcN+YyZwqTSY2ESll 6ARkOoQMosgF3ObrSUS4PWjqNonVrdGYVX+14rNGRCjr3dJnHZ5L8/bOUyD9vK6SDNrzXjJJvvG xIyxmIkAzk6lBrRTcqiQn6j7B57OvvVCqhWC5oay7xhNscMINSgK4de2URYSPdjFIxCgGroHGs7 OI2DmBVWV5iS4uAHOv8VwJw+as/AiM/gm4ZZB5singdCUlnn1sMle+irJIQCdX8lH1HpXm4DElb 1HsrA== X-Google-Smtp-Source: AGHT+IFRiZEr4gA30761Oo3G+7mcb5dJTa4el2SKHgg6AE1oJTviaOcgofOte2zcQ8zPFgRyY0yCfQ== X-Received: by 2002:a05:600c:6489:b0:477:a6f1:499d with SMTP id 5b1f17b1804b1-47d84b338a0mr69543485e9.3.1767976681965; Fri, 09 Jan 2026 08:38:01 -0800 (PST) Received: from localhost (dynamic-2a00-1028-83b8-1e7a-3010-3bd6-8521-caf1.ipv6.o2.cz. [2a00:1028:83b8:1e7a:3010:3bd6:8521:caf1]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-47d7f620ac8sm209009855e9.0.2026.01.09.08.38.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Jan 2026 08:38:01 -0800 (PST) From: Petr Tesarik To: Yury Norov , Rasmus Villemoes Cc: Richard Henderson , Matt Turner , Magnus Lindholm , Vineet Gupta , Geert Uytterhoeven , "Maciej W. Rozycki" , Thomas Bogendoerfer , Madhavan Srinivasan , Michael Ellerman , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Chris Zankel , Max Filippov , Patrik Jakobsson , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Robin Murphy , Joerg Roedel , Will Deacon , Jakub Kicinski , Andrew Lunn , "David S. Miller" , Eric Dumazet , Paolo Abeni , Oliver Neukum , Arnd Bergmann , Kuan-Wei Chiu , Andrew Morton , Marcel Holtmann , Johan Hedberg , Luiz Augusto von Dentz , Pablo Neira Ayuso , Florian Westphal , linux-kernel@vger.kernel.org, Petr Tesarik Subject: [RFC PATCH 1/2] bits: introduce ffs_val() Date: Fri, 9 Jan 2026 17:37:56 +0100 Message-ID: <9767487fcab7dbe7a7282a48a492171629eb935b.1767975412.git.ptesarik@suse.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce a macro that can efficiently extract the least significant non-zero bit from a value. Interestingly, this bit-twiddling trick is open-coded in some places, but it also appears to be little known, leading to various inefficient implementations in other places. Let's make it part of the standard bitops arsenal. Define the macro in a separate header file included from , to allow using it in very low-level header files that may not want to include all of . Signed-off-by: Petr Tesarik --- MAINTAINERS | 1 + include/linux/bitops.h | 1 + include/linux/ffs_val.h | 21 +++++++++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 include/linux/ffs_val.h diff --git a/MAINTAINERS b/MAINTAINERS index a0dd762f5648b..8f15c76a67ea2 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -4466,6 +4466,7 @@ F: arch/*/lib/bitops.c F: include/asm-generic/bitops F: include/asm-generic/bitops.h F: include/linux/bitops.h +F: include/linux/ffs_val.h F: lib/hweight.c F: lib/test_bitops.c F: tools/*/bitops* diff --git a/include/linux/bitops.h b/include/linux/bitops.h index ea7898cc59039..209f0c3e07b9e 100644 --- a/include/linux/bitops.h +++ b/include/linux/bitops.h @@ -4,6 +4,7 @@ =20 #include #include +#include #include =20 #include diff --git a/include/linux/ffs_val.h b/include/linux/ffs_val.h new file mode 100644 index 0000000000000..193ec86d2b53b --- /dev/null +++ b/include/linux/ffs_val.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_LINUX_FFS_VAL_H_ +#define _ASM_LINUX_FFS_VAL_H_ + +/** + * ffs_val - find the value of the first set bit + * @x: the value to search + * + * Unlike ffs(), which returns a bit position, ffs_val() returns the bit + * value itself. + * + * Returns: + * least significant non-zero bit, 0 if all bits are zero + */ +#define ffs_val(x) \ +({ \ + const typeof(x) val__ =3D (x); \ + val__ & -val__; \ +}) + +#endif /* _ASM_LINUX_FFS_VAL_H_ */ --=20 2.52.0 From nobody Wed Feb 11 05:02:15 2026 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8951036403C for ; Fri, 9 Jan 2026 16:38:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767976695; cv=none; b=YTsvsG3oGqd+xZfOoXk2W6tYakdsK1Yw5r3XHZPjVSekXbhxgDeWVAonq55Kxlzkr1jua1w/PJX69hKoGKPx0RACbHHQcZAQymFKkFyxQQzlKDVjTArx32uiaNIWIodX4ct3GllcoN2JcOANJ6xv6rTET/Ry5Gs1fCQCSxgfCvA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767976695; c=relaxed/simple; bh=1Dy7YWHSrTe9KZK1QIWGh5qOgUu26+pDhbFTTlFOHco=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jqhJSPHxAJ6/XCJvokmAkJfZzWh9EyuDsdejp1ENWLRTexM+6BBiQdXe6iS7HOPtNIO1EZhrDpz8WJw/jxlp+GRSv5BE9qOJudBW9Nsjjd91PJsCBqCNP1z8ZtumbF8Dn785b1R8bvHNm+j0hYke03ibfbgy8szuZEhcgqMlTj0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b=bontN1/v; arc=none smtp.client-ip=209.85.128.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b="bontN1/v" Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-47928022b93so8492665e9.0 for ; Fri, 09 Jan 2026 08:38:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1767976683; x=1768581483; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=e3VFVa2IxSAfyZZZtdnT9Nw2nt1nw4P518/zl6JcxKU=; b=bontN1/vy5boTFSg4OW94ih4dqp+uhyS3xs4tOT1bmrYUIo/SzebvDCF95Iv7daiZz 1s1TF8zsnbs86Pcvjw0NWSqT7ICBtxzAL8d2Js+K4xa21IJx1KRKBt4yP43L756JhEHm lb87ujkMTri2RrLyJDP5vvNxx+QJFpo+DOqDKIfRv9jB5V04tV2Z+HTJTIVIZeNgJ7W6 PW6H9HrCFc4NkkHO89uwbS/Ukcd9TW4Vltf/vtDQoI4G5RUgUpm3SXtDU6dgZsUt/2g5 oqxV63GQOjrEoAvcywhOrKaLJBFxcdNN6JmUjNduvk6xXAfdaG4WxQiCBK7oQZwVYeGj XCPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767976683; x=1768581483; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=e3VFVa2IxSAfyZZZtdnT9Nw2nt1nw4P518/zl6JcxKU=; b=l27qEEOgoR4MycfUW00kOA0vozROq+yT5oU6vWRgfVj6SepE6jpWbUvuaGUfoHEwoS ufxK9kups/5OHTol1lMgqmT3J3nuh656M2nGtm7RirDZczT9DDWLzpEQKTH8q13rfncZ QYr5syihMmea2h/xFy6SPZmCvWocPFv/sHZXuv05Yqcpn2W++W7RJ8/Jnwm7iWMqyKtD nVz606TxIeEqBauzop5yWNt40UTnskS2Vz+5KImBtipsINAp2jb8VpXD0vwqJLvUGyFw IgM28Pl66QdCBcqBD0na+kGN9MhSReUsb6V24MoQLpJU5DAhPkgTlNlQxfxjzWEhMGUM A3QA== X-Forwarded-Encrypted: i=1; AJvYcCVGmBImJukdnTGGSf9VMjfB94kJJBavVR8pWYz3xzi8RdDCVnaLPK6Ohi/BLFIIaItQn4AEGktT+FzJp9E=@vger.kernel.org X-Gm-Message-State: AOJu0YzvqJ2+BmHdOvnA0tULOB/WznJvv9U5ugdKG6gkjCkZcjATJ3W2 TFF8F/2tBWr9eYx4x+qJVP8yEjKXOvq222BzMvm9prTCxbboeC/+0vX+G7d6kGa8TR4= X-Gm-Gg: AY/fxX5bSkqo4oBLNCdrHTrmUpESs7v991PrXVxrmZutwXMl+OLzSbjHZ0U6b6TweOl ddDgDtMk5xVTdDGNqx3Z//AbjkfDEZnF4HRTMMQzIMnHQ8e6qpnDYN6DFh9f2vwPtlQxxOIbbxO bTo1uNmvDP5KYJBgVjio4K5JOhjVBTLlvCr6A9vGSNUsHTLFpSZCgkwgy8HBubSHflpPy8Y5Dpe ZyPBn+zQO9U6Emk1fFPcPpGS4szQgVXNaBAGT/KgAvMe+ca9dSN1QXsOPRDejIyTM0X+7LcB/Mc NjS7WbXSF64QcjkpchngvZxa7zxHvrMjlHiOA2PXj85ejW4eJPCIljQ1L7AboQTpYPHDPawEJ3s KwfWrLcDIvK98YKh4eq938PTovh5EQwCmA43L8LClGvySqwoj/6D43HJCIYS5mdQMAAcXEPWSy6 LUI47GyOxMTZjTKWCNo+2UQ+PCsac3O+wTgj9rUR4tVW8U951FGJVafWLOsnvnt7UDRb7H5eFnO cIsCrRh3IMycwBs X-Google-Smtp-Source: AGHT+IH9ea/+OVUEbvOqdAnykos0vDiQIyYCYZcJv1IHD7rlw93jtzQU4u2YEuQFB7eIPDuI8jzwgA== X-Received: by 2002:a05:600c:630f:b0:477:a478:3f94 with SMTP id 5b1f17b1804b1-47d84b399c3mr66744405e9.5.1767976683223; Fri, 09 Jan 2026 08:38:03 -0800 (PST) Received: from localhost (dynamic-2a00-1028-83b8-1e7a-3010-3bd6-8521-caf1.ipv6.o2.cz. [2a00:1028:83b8:1e7a:3010:3bd6:8521:caf1]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-47d7f68f69dsm216359015e9.1.2026.01.09.08.38.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Jan 2026 08:38:02 -0800 (PST) From: Petr Tesarik To: Yury Norov , Rasmus Villemoes Cc: Richard Henderson , Matt Turner , Magnus Lindholm , Vineet Gupta , Geert Uytterhoeven , "Maciej W. Rozycki" , Thomas Bogendoerfer , Madhavan Srinivasan , Michael Ellerman , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Chris Zankel , Max Filippov , Patrik Jakobsson , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Robin Murphy , Joerg Roedel , Will Deacon , Jakub Kicinski , Andrew Lunn , "David S. Miller" , Eric Dumazet , Paolo Abeni , Oliver Neukum , Arnd Bergmann , Kuan-Wei Chiu , Andrew Morton , Marcel Holtmann , Johan Hedberg , Luiz Augusto von Dentz , Pablo Neira Ayuso , Florian Westphal , linux-kernel@vger.kernel.org, Petr Tesarik Subject: [RFC PATCH 2/2] treewide, bits: use ffs_val() where it is open-coded Date: Fri, 9 Jan 2026 17:37:57 +0100 Message-ID: <1ce341045ad0487c66ca21003f9974916aba0bce.1767975412.git.ptesarik@suse.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Replace the open-coded bit-twiddling trick to get the least significant bit with a call to ffs_val(). This patch has been generated by the following coccinelle script: @ depends on !(file in "include/asm-generic/bitops/ffs_val.h") && !(file in "tools/") @ expression E; @@ - ((E) & (-(E))) + ffs_val(E) Include where the result did not build. Signed-off-by: Petr Tesarik --- arch/alpha/include/asm/bitops.h | 2 +- arch/alpha/kernel/core_cia.c | 2 +- arch/alpha/kernel/smp.c | 2 +- arch/arc/include/asm/bitops.h | 2 +- arch/m68k/include/asm/bitops.h | 12 ++++++------ arch/mips/dec/ecc-berr.c | 2 +- arch/mips/include/asm/bitops.h | 4 ++-- arch/mips/pci/pci-malta.c | 4 ++-- arch/powerpc/include/asm/bitops.h | 4 ++-- arch/powerpc/kvm/e500_mmu_host.c | 2 +- arch/powerpc/lib/sstep.c | 2 +- arch/powerpc/xmon/ppc-dis.c | 3 ++- arch/powerpc/xmon/ppc-opc.c | 6 +++--- arch/s390/include/asm/bitops.h | 2 +- arch/xtensa/include/asm/bitops.h | 6 +++--- arch/xtensa/kernel/traps.c | 2 +- drivers/gpu/drm/gma500/psb_intel_sdvo.c | 2 +- drivers/iommu/dma-iommu.c | 2 +- drivers/net/ethernet/netronome/nfp/bpf/jit.c | 2 +- drivers/net/usb/cdc_ncm.c | 4 ++-- include/asm-generic/div64.h | 4 ++-- include/linux/bitfield.h | 3 ++- include/linux/log2.h | 2 +- include/linux/min_heap.h | 5 +++-- lib/math/gcd.c | 4 ++-- lib/sort.c | 3 ++- net/bluetooth/mgmt.c | 2 +- net/netfilter/nft_set_pipapo.c | 2 +- 28 files changed, 48 insertions(+), 44 deletions(-) diff --git a/arch/alpha/include/asm/bitops.h b/arch/alpha/include/asm/bitop= s.h index 76e4343c090f7..590e25862192f 100644 --- a/arch/alpha/include/asm/bitops.h +++ b/arch/alpha/include/asm/bitops.h @@ -317,7 +317,7 @@ static inline unsigned long ffz_b(unsigned long x) { unsigned long sum, x1, x2, x4; =20 - x =3D ~x & -~x; /* set first 0 bit, clear others */ + x =3D ffs_val(~x); /* set first 0 bit, clear others */ x1 =3D x & 0xAA; x2 =3D x & 0xCC; x4 =3D x & 0xF0; diff --git a/arch/alpha/kernel/core_cia.c b/arch/alpha/kernel/core_cia.c index 6e577228e175d..b3c273c13c104 100644 --- a/arch/alpha/kernel/core_cia.c +++ b/arch/alpha/kernel/core_cia.c @@ -1035,7 +1035,7 @@ cia_decode_ecc_error(struct el_CIA_sysdata_mcheck *ci= a, const char *msg) cia_decode_mem_error(cia, msg); =20 syn =3D cia->cia_syn & 0xff; - if (syn =3D=3D (syn & -syn)) { + if (syn =3D=3D ffs_val(syn)) { fmt =3D KERN_CRIT " ECC syndrome %#x -- check bit %d\n"; i =3D ffs(syn) - 1; } else { diff --git a/arch/alpha/kernel/smp.c b/arch/alpha/kernel/smp.c index ed06367ece574..2f3809c015b99 100644 --- a/arch/alpha/kernel/smp.c +++ b/arch/alpha/kernel/smp.c @@ -525,7 +525,7 @@ handle_ipi(struct pt_regs *regs) do { unsigned long which; =20 - which =3D ops & -ops; + which =3D ffs_val(ops); ops &=3D ~which; which =3D __ffs(which); =20 diff --git a/arch/arc/include/asm/bitops.h b/arch/arc/include/asm/bitops.h index df894235fdbc6..def870bd7e4c7 100644 --- a/arch/arc/include/asm/bitops.h +++ b/arch/arc/include/asm/bitops.h @@ -94,7 +94,7 @@ static inline __attribute__ ((const)) unsigned long __fls= (unsigned long x) * ffs =3D Find First Set in word (LSB to MSB) * @result: [1-32], 0 if all 0's */ -#define ffs(x) ({ unsigned long __t =3D (x); fls(__t & -__t); }) +#define ffs(x) ({ unsigned long __t =3D (x); fls(ffs_val(__t)); }) =20 /* * __ffs: Similar to ffs, but zero based (0-31) diff --git a/arch/m68k/include/asm/bitops.h b/arch/m68k/include/asm/bitops.h index e9639e48c6c3c..a67f08b8a7541 100644 --- a/arch/m68k/include/asm/bitops.h +++ b/arch/m68k/include/asm/bitops.h @@ -368,7 +368,7 @@ static inline unsigned long find_first_zero_bit(const u= nsigned long *vaddr, } =20 __asm__ __volatile__ ("bfffo %1{#0,#0},%0" - : "=3Dd" (res) : "d" (num & -num)); + : "=3Dd" (res) : "d" (ffs_val(num))); res ^=3D 31; out: res +=3D ((long)p - (long)vaddr - 4) * 8; @@ -392,7 +392,7 @@ static inline unsigned long find_next_zero_bit(const un= signed long *vaddr, =20 /* Look for zero in first longword */ __asm__ __volatile__ ("bfffo %1{#0,#0},%0" - : "=3Dd" (res) : "d" (num & -num)); + : "=3Dd" (res) : "d" (ffs_val(num))); if (res < 32) { offset +=3D res ^ 31; return offset < size ? offset : size; @@ -425,7 +425,7 @@ static inline unsigned long find_first_bit(const unsign= ed long *vaddr, } =20 __asm__ __volatile__ ("bfffo %1{#0,#0},%0" - : "=3Dd" (res) : "d" (num & -num)); + : "=3Dd" (res) : "d" (ffs_val(num))); res ^=3D 31; out: res +=3D ((long)p - (long)vaddr - 4) * 8; @@ -449,7 +449,7 @@ static inline unsigned long find_next_bit(const unsigne= d long *vaddr, =20 /* Look for one in first longword */ __asm__ __volatile__ ("bfffo %1{#0,#0},%0" - : "=3Dd" (res) : "d" (num & -num)); + : "=3Dd" (res) : "d" (ffs_val(num))); if (res < 32) { offset +=3D res ^ 31; return offset < size ? offset : size; @@ -473,7 +473,7 @@ static inline unsigned long __attribute_const__ ffz(uns= igned long word) int res; =20 __asm__ __volatile__ ("bfffo %1{#0,#0},%0" - : "=3Dd" (res) : "d" (~word & -~word)); + : "=3Dd" (res) : "d" (ffs_val(~word))); return res ^ 31; } =20 @@ -527,7 +527,7 @@ static inline __attribute_const__ int ffs(int x) =20 __asm__ ("bfffo %1{#0:#0},%0" : "=3Dd" (cnt) - : "dm" (x & -x)); + : "dm" (ffs_val(x))); return 32 - cnt; } =20 diff --git a/arch/mips/dec/ecc-berr.c b/arch/mips/dec/ecc-berr.c index 1eb356fdd8323..8934b8b1cf375 100644 --- a/arch/mips/dec/ecc-berr.c +++ b/arch/mips/dec/ecc-berr.c @@ -153,7 +153,7 @@ static int dec_ecc_be_backend(struct pt_regs *regs, int= is_fixup, int invoker) /* Ack now, now we've rewritten (or not). */ dec_ecc_be_ack(); =20 - if (syn && syn =3D=3D (syn & -syn)) { + if (syn && syn =3D=3D ffs_val(syn)) { if (syn =3D=3D 0x01) { fmt =3D KERN_ALERT "%s" "%#04x -- %s bit error " diff --git a/arch/mips/include/asm/bitops.h b/arch/mips/include/asm/bitops.h index 42f88452c920f..6e71999618876 100644 --- a/arch/mips/include/asm/bitops.h +++ b/arch/mips/include/asm/bitops.h @@ -395,7 +395,7 @@ static __always_inline __attribute_const__ unsigned lon= g __fls(unsigned long wor */ static __always_inline __attribute_const__ unsigned long __ffs(unsigned lo= ng word) { - return __fls(word & -word); + return __fls(ffs_val(word)); } =20 /* @@ -463,7 +463,7 @@ static inline __attribute_const__ int ffs(int word) if (!word) return 0; =20 - return fls(word & -word); + return fls(ffs_val(word)); } =20 #include diff --git a/arch/mips/pci/pci-malta.c b/arch/mips/pci/pci-malta.c index 2e35aeba45bc1..502f99f46eb75 100644 --- a/arch/mips/pci/pci-malta.c +++ b/arch/mips/pci/pci-malta.c @@ -117,7 +117,7 @@ void __init mips_pcibios_init(void) mask =3D ~(start ^ end); /* We don't support remapping with a discontiguous mask. */ BUG_ON((start & GT_PCI_HD_MSK) !=3D (map & GT_PCI_HD_MSK) && - mask !=3D ~((mask & -mask) - 1)); + mask !=3D ~(ffs_val(mask) - 1)); gt64120_mem_resource.start =3D start; gt64120_mem_resource.end =3D end; gt64120_controller.mem_offset =3D (start & mask) - (map & mask); @@ -134,7 +134,7 @@ void __init mips_pcibios_init(void) mask =3D ~(start ^ end); /* We don't support remapping with a discontiguous mask. */ BUG_ON((start & GT_PCI_HD_MSK) !=3D (map & GT_PCI_HD_MSK) && - mask !=3D ~((mask & -mask) - 1)); + mask !=3D ~(ffs_val(mask) - 1)); gt64120_io_resource.start =3D map & mask; gt64120_io_resource.end =3D (map & mask) | ~mask; gt64120_controller.io_offset =3D 0; diff --git a/arch/powerpc/include/asm/bitops.h b/arch/powerpc/include/asm/b= itops.h index 0d0470cd5ac31..571700bb41e46 100644 --- a/arch/powerpc/include/asm/bitops.h +++ b/arch/powerpc/include/asm/bitops.h @@ -87,8 +87,8 @@ static __always_inline bool is_rlwinm_mask_valid(unsigned= long x) if (!x) return false; if (x & 1) - x =3D ~x; // make the mask non-wrapping - x +=3D x & -x; // adding the low set bit results in at most one bit set + x =3D ~x; // make the mask non-wrapping + x +=3D ffs_val(x); // adding the low set bit results in at most one bit s= et =20 return !(x & (x - 1)); } diff --git a/arch/powerpc/kvm/e500_mmu_host.c b/arch/powerpc/kvm/e500_mmu_h= ost.c index 06caf8bbbe2b7..00ae88b3369fd 100644 --- a/arch/powerpc/kvm/e500_mmu_host.c +++ b/arch/powerpc/kvm/e500_mmu_host.c @@ -205,7 +205,7 @@ void inval_gtlbe_on_host(struct kvmppc_vcpu_e500 *vcpu_= e500, int tlbsel, =20 local_irq_save(flags); while (tmp) { - hw_tlb_indx =3D __ilog2_u64(tmp & -tmp); + hw_tlb_indx =3D __ilog2_u64(ffs_val(tmp)); mtspr(SPRN_MAS0, MAS0_TLBSEL(1) | MAS0_ESEL(to_htlb1_esel(hw_tlb_indx))); diff --git a/arch/powerpc/lib/sstep.c b/arch/powerpc/lib/sstep.c index ac3ee19531d8a..2a8a7e007dbd2 100644 --- a/arch/powerpc/lib/sstep.c +++ b/arch/powerpc/lib/sstep.c @@ -236,7 +236,7 @@ static nokprobe_inline unsigned long mlsd_8lsd_ea(unsig= ned int instr, static nokprobe_inline unsigned long max_align(unsigned long x) { x |=3D sizeof(unsigned long); - return x & -x; /* isolates rightmost bit */ + return ffs_val(x); /* isolates rightmost bit */ } =20 static nokprobe_inline unsigned long byterev_2(unsigned long x) diff --git a/arch/powerpc/xmon/ppc-dis.c b/arch/powerpc/xmon/ppc-dis.c index af105e1bc3fca..13b31964686ad 100644 --- a/arch/powerpc/xmon/ppc-dis.c +++ b/arch/powerpc/xmon/ppc-dis.c @@ -9,6 +9,7 @@ This file is part of GDB, GAS, and the GNU binutils. =20 #include #include +#include #include "nonstdio.h" #include "ansidecl.h" #include "ppc.h" @@ -44,7 +45,7 @@ operand_value_powerpc (const struct powerpc_operand *oper= and, unsigned long top =3D operand->bitm; /* top & -top gives the rightmost 1 bit, so this fills in any trailing zeros. */ - top |=3D (top & -top) - 1; + top |=3D ffs_val(top) - 1; top &=3D ~(top >> 1); value =3D (value ^ top) - top; } diff --git a/arch/powerpc/xmon/ppc-opc.c b/arch/powerpc/xmon/ppc-opc.c index de9b4236728c4..23d845439fa11 100644 --- a/arch/powerpc/xmon/ppc-opc.c +++ b/arch/powerpc/xmon/ppc-opc.c @@ -1417,7 +1417,7 @@ insert_fxm (unsigned long insn, one bit of the mask field is set. */ if ((insn & (1 << 20)) !=3D 0) { - if (value =3D=3D 0 || (value & -value) !=3D value) + if (value =3D=3D 0 || ffs_val(value) !=3D value) { *errmsg =3D _("invalid mask field"); value =3D 0; @@ -1430,7 +1430,7 @@ insert_fxm (unsigned long insn, new form unless -mpower4 has been given, or -many and the two operand form of mfcr was used. */ else if (value > 0 - && (value & -value) =3D=3D value + && ffs_val(value) =3D=3D value && ((dialect & PPC_OPCODE_POWER4) !=3D 0 || ((dialect & PPC_OPCODE_ANY) !=3D 0 && (insn & (0x3ff << 1)) =3D=3D 19 << 1))) @@ -1460,7 +1460,7 @@ extract_fxm (unsigned long insn, if ((insn & (1 << 20)) !=3D 0) { /* Exactly one bit of MASK should be set. */ - if (mask =3D=3D 0 || (mask & -mask) !=3D mask) + if (mask =3D=3D 0 || ffs_val(mask) !=3D mask) *invalid =3D 1; } =20 diff --git a/arch/s390/include/asm/bitops.h b/arch/s390/include/asm/bitops.h index 5f10074665b0c..dadd8a5bd5428 100644 --- a/arch/s390/include/asm/bitops.h +++ b/arch/s390/include/asm/bitops.h @@ -193,7 +193,7 @@ static __always_inline __flatten __attribute_const__ in= t ffs(int word) { unsigned int val =3D (unsigned int)word; =20 - return BITS_PER_LONG - __flogr(-val & val); + return BITS_PER_LONG - __flogr(ffs_val(val)); } =20 #else /* CONFIG_CC_HAS_BUILTIN_FFS */ diff --git a/arch/xtensa/include/asm/bitops.h b/arch/xtensa/include/asm/bit= ops.h index f7390b6761e1b..44e0debde1b3d 100644 --- a/arch/xtensa/include/asm/bitops.h +++ b/arch/xtensa/include/asm/bitops.h @@ -39,7 +39,7 @@ static inline unsigned long __cntlz (unsigned long x) =20 static inline int __attribute_const__ ffz(unsigned long x) { - return 31 - __cntlz(~x & -~x); + return 31 - __cntlz(ffs_val(~x)); } =20 /* @@ -48,7 +48,7 @@ static inline int __attribute_const__ ffz(unsigned long x) =20 static inline __attribute_const__ unsigned long __ffs(unsigned long x) { - return 31 - __cntlz(x & -x); + return 31 - __cntlz(ffs_val(x)); } =20 /* @@ -59,7 +59,7 @@ static inline __attribute_const__ unsigned long __ffs(uns= igned long x) =20 static inline __attribute_const__ int ffs(unsigned long x) { - return 32 - __cntlz(x & -x); + return 32 - __cntlz(ffs_val(x)); } =20 /* diff --git a/arch/xtensa/kernel/traps.c b/arch/xtensa/kernel/traps.c index 44c07c4e0833a..7ab71b21581f5 100644 --- a/arch/xtensa/kernel/traps.c +++ b/arch/xtensa/kernel/traps.c @@ -309,7 +309,7 @@ static void do_interrupt(struct pt_regs *regs) break; =20 /* clear lowest pending irq in the unhandled mask */ - unhandled ^=3D (int_at_level & -int_at_level); + unhandled ^=3D ffs_val(int_at_level); do_IRQ(__ffs(int_at_level), regs); } =20 diff --git a/drivers/gpu/drm/gma500/psb_intel_sdvo.c b/drivers/gpu/drm/gma5= 00/psb_intel_sdvo.c index 553e7c7d9bb83..56d3fa0bd0334 100644 --- a/drivers/gpu/drm/gma500/psb_intel_sdvo.c +++ b/drivers/gpu/drm/gma500/psb_intel_sdvo.c @@ -1226,7 +1226,7 @@ psb_intel_sdvo_multifunc_encoder(struct psb_intel_sdv= o *psb_intel_sdvo) { /* Is there more than one type of output? */ int caps =3D psb_intel_sdvo->caps.output_flags & 0xf; - return caps & -caps; + return ffs_val(caps); } =20 static struct edid * diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index c92088855450a..a0211b51d8043 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -943,7 +943,7 @@ static struct page **__iommu_dma_alloc_noncontiguous(st= ruct device *dev, iommu_deferred_attach(dev, domain)) return NULL; =20 - min_size =3D alloc_sizes & -alloc_sizes; + min_size =3D ffs_val(alloc_sizes); if (min_size < PAGE_SIZE) { min_size =3D PAGE_SIZE; alloc_sizes |=3D PAGE_SIZE; diff --git a/drivers/net/ethernet/netronome/nfp/bpf/jit.c b/drivers/net/eth= ernet/netronome/nfp/bpf/jit.c index 3a02eef58cc6d..25e0905a61721 100644 --- a/drivers/net/ethernet/netronome/nfp/bpf/jit.c +++ b/drivers/net/ethernet/netronome/nfp/bpf/jit.c @@ -1565,7 +1565,7 @@ static int wrp_div_imm(struct nfp_prog *nfp_prog, u8 = dst, u64 imm) rvalue =3D reciprocal_value_adv(imm, 32); exp =3D rvalue.exp; if (rvalue.is_wide_m && !(imm & 1)) { - pre_shift =3D fls(imm & -imm) - 1; + pre_shift =3D fls(ffs_val(imm)) - 1; rvalue =3D reciprocal_value_adv(imm >> pre_shift, 32 - pre_shift); } else { pre_shift =3D 0; diff --git a/drivers/net/usb/cdc_ncm.c b/drivers/net/usb/cdc_ncm.c index 5d123df0a866b..e132f222a026a 100644 --- a/drivers/net/usb/cdc_ncm.c +++ b/drivers/net/usb/cdc_ncm.c @@ -668,7 +668,7 @@ static void cdc_ncm_fix_modulus(struct usbnet *dev) val =3D ctx->tx_ndp_modulus; =20 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) || - (val !=3D ((-val) & val)) || (val >=3D ctx->tx_max)) { + (val !=3D ffs_val(val)) || (val >=3D ctx->tx_max)) { dev_dbg(&dev->intf->dev, "Using default alignment: 4 bytes\n"); ctx->tx_ndp_modulus =3D USB_CDC_NCM_NDP_ALIGN_MIN_SIZE; } @@ -682,7 +682,7 @@ static void cdc_ncm_fix_modulus(struct usbnet *dev) val =3D ctx->tx_modulus; =20 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) || - (val !=3D ((-val) & val)) || (val >=3D ctx->tx_max)) { + (val !=3D ffs_val(val)) || (val >=3D ctx->tx_max)) { dev_dbg(&dev->intf->dev, "Using default transmit modulus: 4 bytes\n"); ctx->tx_modulus =3D USB_CDC_NCM_NDP_ALIGN_MIN_SIZE; } diff --git a/include/asm-generic/div64.h b/include/asm-generic/div64.h index 25e7b4b58dcf5..d56208be887de 100644 --- a/include/asm-generic/div64.h +++ b/include/asm-generic/div64.h @@ -111,8 +111,8 @@ } \ \ /* Reduce m / p to help avoid overflow handling later. */ \ - ___p /=3D (___m & -___m); \ - ___m /=3D (___m & -___m); \ + ___p /=3D ffs_val(___m); \ + ___m /=3D ffs_val(___m); \ \ /* \ * Perform (m_bias + m * n) / (1 << 64). \ diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h index 126dc5b380afa..26fbb6cfc0719 100644 --- a/include/linux/bitfield.h +++ b/include/linux/bitfield.h @@ -8,6 +8,7 @@ #define _LINUX_BITFIELD_H =20 #include +#include #include #include =20 @@ -202,7 +203,7 @@ static __always_inline u64 field_multiplier(u64 field) { if ((field | (field - 1)) & ((field | (field - 1)) + 1)) __bad_mask(); - return field & -field; + return ffs_val(field); } static __always_inline u64 field_mask(u64 field) { diff --git a/include/linux/log2.h b/include/linux/log2.h index 2eac3fc9303d6..7615e513885e9 100644 --- a/include/linux/log2.h +++ b/include/linux/log2.h @@ -266,7 +266,7 @@ int __bits_per(unsigned long n) static inline __attribute__((const)) unsigned int max_pow_of_two_factor(unsigned int n) { - return n & -n; + return ffs_val(n); } =20 #endif /* _LINUX_LOG2_H */ diff --git a/include/linux/min_heap.h b/include/linux/min_heap.h index 79ddc0adbf2bf..e914919470c88 100644 --- a/include/linux/min_heap.h +++ b/include/linux/min_heap.h @@ -3,6 +3,7 @@ #define _LINUX_MIN_HEAP_H =20 #include +#include #include #include =20 @@ -257,7 +258,7 @@ static __always_inline void __min_heap_sift_down_inline(min_heap_char *heap, size_t pos, size_t e= lem_size, const struct min_heap_callbacks *func, void *args) { - const unsigned long lsbit =3D elem_size & -elem_size; + const unsigned long lsbit =3D ffs_val(elem_size); void *data =3D heap->data; void (*swp)(void *lhs, void *rhs, void *args) =3D func->swp; /* pre-scale counters for performance */ @@ -297,7 +298,7 @@ static __always_inline void __min_heap_sift_up_inline(min_heap_char *heap, size_t elem_size, size= _t idx, const struct min_heap_callbacks *func, void *args) { - const unsigned long lsbit =3D elem_size & -elem_size; + const unsigned long lsbit =3D ffs_val(elem_size); void *data =3D heap->data; void (*swp)(void *lhs, void *rhs, void *args) =3D func->swp; /* pre-scale counters for performance */ diff --git a/lib/math/gcd.c b/lib/math/gcd.c index 62efca6787aef..3706c84e68bc1 100644 --- a/lib/math/gcd.c +++ b/lib/math/gcd.c @@ -23,12 +23,12 @@ static unsigned long binary_gcd(unsigned long a, unsign= ed long b) =20 b >>=3D __ffs(b); if (b =3D=3D 1) - return r & -r; + return ffs_val(r); =20 for (;;) { a >>=3D __ffs(a); if (a =3D=3D 1) - return r & -r; + return ffs_val(r); if (a =3D=3D b) return a << __ffs(r); =20 diff --git a/lib/sort.c b/lib/sort.c index 52363995ccc5c..2489aaaa21a06 100644 --- a/lib/sort.c +++ b/lib/sort.c @@ -12,6 +12,7 @@ =20 #include #include +#include #include =20 /** @@ -196,7 +197,7 @@ static void __sort_r(void *base, size_t num, size_t siz= e, { /* pre-scale counters for performance */ size_t n =3D num * size, a =3D (num/2) * size; - const unsigned int lsbit =3D size & -size; /* Used to find parent */ + const unsigned int lsbit =3D ffs_val(size); /* Used to find parent */ size_t shift =3D 0; =20 if (!a) /* num < 2 || size =3D=3D 0 */ diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c index 5be9b8c919490..2b13998fdb0fe 100644 --- a/net/bluetooth/mgmt.c +++ b/net/bluetooth/mgmt.c @@ -8664,7 +8664,7 @@ static bool requested_adv_flags_are_valid(struct hci_= dev *hdev, u32 adv_flags) supported_flags =3D get_supported_adv_flags(hdev); phy_flags =3D adv_flags & MGMT_ADV_FLAG_SEC_MASK; if (adv_flags & ~supported_flags || - ((phy_flags && (phy_flags ^ (phy_flags & -phy_flags))))) + ((phy_flags && (phy_flags ^ ffs_val(phy_flags))))) return false; =20 return true; diff --git a/net/netfilter/nft_set_pipapo.c b/net/netfilter/nft_set_pipapo.c index 112fe46788b6f..f93f509cc3fbf 100644 --- a/net/netfilter/nft_set_pipapo.c +++ b/net/netfilter/nft_set_pipapo.c @@ -370,7 +370,7 @@ int pipapo_refill(unsigned long *map, unsigned int len,= unsigned int rules, for (k =3D 0; k < len; k++) { bitset =3D map[k]; while (bitset) { - unsigned long t =3D bitset & -bitset; + unsigned long t =3D ffs_val(bitset); int r =3D __builtin_ctzl(bitset); int i =3D k * BITS_PER_LONG + r; =20 --=20 2.52.0