From: Petr Tesarik <ptesarik@suse.com>
To: Kuan-Wei Chiu <visitorckw@gmail.com>
Cc: Yury Norov <yury.norov@gmail.com>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
Richard Henderson <richard.henderson@linaro.org>,
Matt Turner <mattst88@gmail.com>,
Magnus Lindholm <linmag7@gmail.com>,
Vineet Gupta <vgupta@kernel.org>,
Geert Uytterhoeven <geert@linux-m68k.org>,
"Maciej W. Rozycki" <macro@orcam.me.uk>,
Thomas Bogendoerfer <tsbogend@alpha.franken.de>,
Madhavan Srinivasan <maddy@linux.ibm.com>,
Michael Ellerman <mpe@ellerman.id.au>,
Heiko Carstens <hca@linux.ibm.com>,
Vasily Gorbik <gor@linux.ibm.com>,
Alexander Gordeev <agordeev@linux.ibm.com>,
Chris Zankel <chris@zankel.net>,
Max Filippov <jcmvbkbc@gmail.com>,
Patrik Jakobsson <patrik.r.jakobsson@gmail.com>,
Maarten Lankhorst <maarten.lankhorst@linux.intel.com>,
Maxime Ripard <mripard@kernel.org>,
Thomas Zimmermann <tzimmermann@suse.de>,
David Airlie <airlied@gmail.com>, Simona Vetter <simona@ffwll.ch>,
Robin Murphy <robin.murphy@arm.com>,
Joerg Roedel <joro@8bytes.org>, Will Deacon <will@kernel.org>,
Jakub Kicinski <kuba@kernel.org>,
Andrew Lunn <andrew+netdev@lunn.ch>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@google.com>,
Paolo Abeni <pabeni@redhat.com>,
Oliver Neukum <oliver@neukum.org>, Arnd Bergmann <arnd@arndb.de>,
Andrew Morton <akpm@linux-foundation.org>,
Marcel Holtmann <marcel@holtmann.org>,
Johan Hedberg <johan.hedberg@gmail.com>,
Luiz Augusto von Dentz <luiz.dentz@gmail.com>,
Pablo Neira Ayuso <pablo@netfilter.org>,
Florian Westphal <fw@strlen.de>,
linux-kernel@vger.kernel.org
Subject: Re: [RFC PATCH 0/2] Helper to isolate least-significant bit
Date: Fri, 9 Jan 2026 19:59:46 +0100 [thread overview]
Message-ID: <20260109195946.551a2693@mordecai> (raw)
In-Reply-To: <aWE4-zTTvHC4zhvj@google.com>
On Sat, 10 Jan 2026 01:20:59 +0800
Kuan-Wei Chiu <visitorckw@gmail.com> wrote:
> Hi Petr,
>
> On Fri, Jan 09, 2026 at 05:41:34PM +0100, Petr Tesarik wrote:
> > Isolation of the least significant bit can be achieved with 3 basic
> > ALU operations which are already open-coded in various places in the
> > kernel.
> >
> > However, since other places less efficient constructs, for example
> > `1UL << ffs(x)`, I assume the trick is known only to some authors, and
> > it's worth adding a helper to promote its use.
>
> Just out of curiosity, are there any existing users employing 1 <<
> ffs(x) (or other inefficient variants) in performance-critical
> hotpaths?
>
> From a quick grep, I only found one instance in drivers/clk/ti/mux.c
> matching the 1 << ffs(x) pattern. However, this doesn't appear to be a
> bottleneck since it is followed by ti_clk_ll_ops->clk_writel(...). The
> latency of the MMIO write would likely overshadow the savings of a few
> ALU cycles.
Most expressions are a bit more complex, like this one in
page_cache_async_ra():
align = 1UL << min(ra->order, ffs(max_pages) - 1);
Or split across multiple lines, like this one in sata_down_spd_limit():
bit = ffs(mask) - 1;
mask = 1 << bit;
I agree that there is most likely no measurable performance win. The
resulting machine code merely looks quite silly on architectures
without an instruction to do ffs() and a little bit silly on
architectures where the instruction has a slightly different semantics
(bit position numbering and/or handling of zero value).
> Additionally, it seems that patch #2 focuses on replacing the x & -x
> implementation with the new API, rather than converting inefficient
> constructs like 1 << ffs(x) to use ffs_val().
This is correct. This patch series merely introduces the helper. Patch
2/2 was created mechanically. I haven't sent any follow-up yet to
replace inefficient use, because that would require more effort, and I'm
trying to get some early feedback first (that's why the series is
tagged RFC).
If we agree that it's worth the effort, and I split the patches by
subsystem, it may become a rather long series.
Petr T
next prev parent reply other threads:[~2026-01-09 18:59 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-09 16:41 [RFC PATCH 0/2] Helper to isolate least-significant bit Petr Tesarik
2026-01-09 16:37 ` [RFC PATCH 1/2] bits: introduce ffs_val() Petr Tesarik
2026-01-09 17:01 ` Arnd Bergmann
2026-01-09 23:54 ` David Laight
2026-01-09 17:16 ` Yury Norov
2026-01-09 17:46 ` Petr Tesarik
2026-01-09 18:26 ` Yury Norov
2026-01-09 19:27 ` Petr Tesarik
2026-01-09 19:44 ` Yury Norov
2026-01-09 18:50 ` Arnd Bergmann
2026-01-10 10:50 ` David Laight
2026-01-12 8:15 ` Thomas Zimmermann
2026-01-12 8:58 ` Petr Tesarik
2026-01-12 11:22 ` David Laight
2026-01-13 1:40 ` Maciej W. Rozycki
2026-01-09 16:37 ` [RFC PATCH 2/2] treewide, bits: use ffs_val() where it is open-coded Petr Tesarik
2026-01-09 18:19 ` Yury Norov
2026-01-09 19:32 ` Petr Tesarik
2026-01-09 20:19 ` Yury Norov
2026-01-10 10:36 ` Maciej W. Rozycki
2026-01-10 11:54 ` David Laight
2026-01-11 3:15 ` Maciej W. Rozycki
2026-01-11 10:40 ` David Laight
2026-01-11 21:22 ` Maciej W. Rozycki
2026-01-11 23:57 ` David Laight
2026-01-12 11:21 ` Maciej W. Rozycki
2026-01-12 13:23 ` David Laight
2026-01-10 16:42 ` Kuan-Wei Chiu
2026-01-10 22:23 ` David Laight
2026-01-11 14:41 ` Kuan-Wei Chiu
2026-01-11 21:22 ` Maciej W. Rozycki
2026-01-09 17:20 ` [RFC PATCH 0/2] Helper to isolate least-significant bit Kuan-Wei Chiu
2026-01-09 18:59 ` Petr Tesarik [this message]
2026-01-09 19:26 ` Andrew Cooper
-- strict thread matches above, loose matches on Subject: below --
2026-01-09 16:37 Petr Tesarik
2026-01-09 19:11 ` Geert Uytterhoeven
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260109195946.551a2693@mordecai \
--to=ptesarik@suse.com \
--cc=agordeev@linux.ibm.com \
--cc=airlied@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=andrew+netdev@lunn.ch \
--cc=arnd@arndb.de \
--cc=chris@zankel.net \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=fw@strlen.de \
--cc=geert@linux-m68k.org \
--cc=gor@linux.ibm.com \
--cc=hca@linux.ibm.com \
--cc=jcmvbkbc@gmail.com \
--cc=johan.hedberg@gmail.com \
--cc=joro@8bytes.org \
--cc=kuba@kernel.org \
--cc=linmag7@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=luiz.dentz@gmail.com \
--cc=maarten.lankhorst@linux.intel.com \
--cc=macro@orcam.me.uk \
--cc=maddy@linux.ibm.com \
--cc=marcel@holtmann.org \
--cc=mattst88@gmail.com \
--cc=mpe@ellerman.id.au \
--cc=mripard@kernel.org \
--cc=oliver@neukum.org \
--cc=pabeni@redhat.com \
--cc=pablo@netfilter.org \
--cc=patrik.r.jakobsson@gmail.com \
--cc=richard.henderson@linaro.org \
--cc=robin.murphy@arm.com \
--cc=simona@ffwll.ch \
--cc=tsbogend@alpha.franken.de \
--cc=tzimmermann@suse.de \
--cc=vgupta@kernel.org \
--cc=visitorckw@gmail.com \
--cc=will@kernel.org \
--cc=yury.norov@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.