Re: [PATCH v1 1/1] bitmap: Refactor bitmap_read() and bitmap_write() to avoid too big shift

From: Yury Norov

Date: Fri Nov 20 2026 - 09:42:44 EST


On Fri, Nov 20, 2026 at 10:42:24AM +0200, Andy Shevchenko wrote:
> On Thu, Nov 19, 2026 at 04:22:06PM -0500, Yury Norov wrote:
> > On Thu, Nov 19, 2026 at 11:03:53PM +0200, Andy Shevchenko wrote:
> > > On Thu, Nov 19, 2026 at 03:37:33PM -0500, Yury Norov wrote:
> > > > On Thu, Nov 19, 2026 at 08:15:30PM +0100, Andy Shevchenko wrote:
>
> ...
>
> > > > I guess... Let's keep the patch minimal?
> > > >
> > > > > - mask = BITMAP_LAST_WORD_MASK(nbits);
> > > > > - value &= mask;
> > > > > - offset = start % BITS_PER_LONG;
> > > > > - space = BITS_PER_LONG - offset;
> > > > > - fit = space >= nbits;
> > > >
> > > > I don't like this 'fit' thing, and would like to drop it. But
> > > > let's find a better motivation to do that?
> > >
> > > Why? The fit keeps the check I want to refactor, since it makes it twice
> > > the code with that ternary looks ugly and unreadable. This is exactly the
> > > chance to kill two birds with one stone.
> >
> > That's what I meant regarding the motivational part and minimizing the
> > patch.
>
> OK. So refactoring bitmap_write() to make it look like bitmap_read() can be
> done after your nice below patch.

Sure, I'll take the patch with the tags suggested. But you're very
welcome to continue optimizing it - my gut feeling says we can
squeeze a few more percents out of it with just a careful refactoring.

> > From dfac9a524aa8658fe63c03804cca9f0980b58ef7 Mon Sep 17 00:00:00 2001
> > From: Yury Norov <ynorov@xxxxxxxxxx>
> > Date: Thu, 19 Nov 2026 16:03:21 -0500
> > Subject: [PATCH] bitmap: Rephrase read and write bounds checks for sparse
> >
> > Reword bitmap_read/write() conditionals such that it improves on code
> > generation and performance.
> >
> > Tested with minimal x86-64 kernels with GCC 15.2 and CONFIG_TEST_BITMAP=y.
> > Run 400 fresh boots per kernel under virtme/KVM on a Core Ultra 7 268V,
> > using one vCPU and 256 MiB RAM.
> >
> > Before, us After, us Change, us t p
> > Read 277.00 (6.99) 265.33 (17.05) -4.21% -12.695 2.97e-31
> > Write 496.96 (14.38) 488.56 (10.41) -1.69% -9.615 8.03e-20
> >
> > Bloat-o-meter reports 60 fewer function bytes in vmlinux and in
> > lib/test_bitmap.o:
> >
> > Function Before After Delta
> > test_bitmap_read_perf 234 227 -7
> > test_bitmap_write_perf 298 286 -12
> > test_bitmap_write_helper 880 872 -8
> > test_bitmap_read_write 800 767 -33
> >
> > It also fixes the following sparse errors:
> >
> > include/linux/bitmap.h:854:55: warning: shift too big (64) for type unsigned long
> > include/linux/bitmap.h:854:55: warning: shift too big (64) for type unsigned long
>
> Tested-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx>
> > Link: https://lore.kernel.org/r/20261119191530.1716850-1-andriy.shevchenko@xxxxxxxxxxxxxxx
>
> Should be Closes: tag.
>
> --
> With Best Regards,
> Andy Shevchenko
>