lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <aWE31mUxOSeg7-Ks@yury>
Date: Fri, 9 Jan 2026 12:16:06 -0500
From: Yury Norov <ynorov@...dia.com>
To: Petr Tesarik <ptesarik@...e.com>
Cc: Yury Norov <yury.norov@...il.com>,
	Rasmus Villemoes <linux@...musvillemoes.dk>,
	Richard Henderson <richard.henderson@...aro.org>,
	Matt Turner <mattst88@...il.com>,
	Magnus Lindholm <linmag7@...il.com>,
	Vineet Gupta <vgupta@...nel.org>,
	Geert Uytterhoeven <geert@...ux-m68k.org>,
	"Maciej W. Rozycki" <macro@...am.me.uk>,
	Thomas Bogendoerfer <tsbogend@...ha.franken.de>,
	Madhavan Srinivasan <maddy@...ux.ibm.com>,
	Michael Ellerman <mpe@...erman.id.au>,
	Heiko Carstens <hca@...ux.ibm.com>,
	Vasily Gorbik <gor@...ux.ibm.com>,
	Alexander Gordeev <agordeev@...ux.ibm.com>,
	Chris Zankel <chris@...kel.net>, Max Filippov <jcmvbkbc@...il.com>,
	Patrik Jakobsson <patrik.r.jakobsson@...il.com>,
	Maarten Lankhorst <maarten.lankhorst@...ux.intel.com>,
	Maxime Ripard <mripard@...nel.org>,
	Thomas Zimmermann <tzimmermann@...e.de>,
	David Airlie <airlied@...il.com>, Simona Vetter <simona@...ll.ch>,
	Robin Murphy <robin.murphy@....com>, Joerg Roedel <joro@...tes.org>,
	Will Deacon <will@...nel.org>, Jakub Kicinski <kuba@...nel.org>,
	Andrew Lunn <andrew+netdev@...n.ch>,
	"David S. Miller" <davem@...emloft.net>,
	Eric Dumazet <edumazet@...gle.com>, Paolo Abeni <pabeni@...hat.com>,
	Oliver Neukum <oliver@...kum.org>, Arnd Bergmann <arnd@...db.de>,
	Kuan-Wei Chiu <visitorckw@...il.com>,
	Andrew Morton <akpm@...ux-foundation.org>,
	Marcel Holtmann <marcel@...tmann.org>,
	Johan Hedberg <johan.hedberg@...il.com>,
	Luiz Augusto von Dentz <luiz.dentz@...il.com>,
	Pablo Neira Ayuso <pablo@...filter.org>,
	Florian Westphal <fw@...len.de>, linux-kernel@...r.kernel.org
Subject: Re: [RFC PATCH 1/2] bits: introduce ffs_val()

On Fri, Jan 09, 2026 at 05:37:56PM +0100, Petr Tesarik wrote:
> 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 <linux/bitops.h>,
> to allow using it in very low-level header files that may not want to
> include all of <linux/bitops.h>.

Nice catch. Thanks!

> 
> Signed-off-by: Petr Tesarik <ptesarik@...e.com>
> ---
>  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

No need for a separate header. Just put int straight in bitops.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 @@
>  
>  #include <asm/types.h>
>  #include <linux/bits.h>
> +#include <linux/ffs_val.h>
>  #include <linux/typecheck.h>
>  
>  #include <uapi/linux/kernel.h>
> 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

By definition, the value of 1st set bit is 1, just like any other 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__ = (x);	\

const auto? Also, are you sure it works OK with unsigned types? No
warnings? Maybe add a test?

> +	val__ & -val__;			\
> +})

This macro returns in fact a mask containing LSB only, so I'd suggest
to choose a name like lsb_mask().

This is also a replacement of BIT(ffs()), GENMASK(ffs(), 0) constructions.
Can you check the kernel, and convert those patterns too? I found at least
one in drivers/clk/nxp/clk-lpc32xx.c:lpc32xx_clk_div_quirk().

Thanks,
Yury

> +
> +#endif /* _ASM_LINUX_FFS_VAL_H_ */
> -- 
> 2.52.0

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ