Skip to contentSkip to conversation

bitmap: Refactor bitmap_read() and bitmap_write() to avoid too big shift

PATCH In review Andy Shevchenko 2 participants 7 messages Nov 19 – Nov 20

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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

While compiling ad4691.c I have noticed the following warnings:

drivers/iio/adc/ad4691.c: note: in included file:
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

Since the bitmap_read() is called with statically define 'start', the 'space' local variable becomes equal to BITS_PER_LONG, or 64 on 64-bit machines. The 'nbits' parameter is coming from the run-time defined value. Static analyser doesn't see that the right shift by 64-bit can't happen — the space >= nbits check guards that. While Jonathan put it as false positive [1], we still can give a hint to it without growing in the code (bloat-o-meter for above mentioned driver shows no difference in size). So this is what this change does.

For completeness, bitmap_write() is also refactored in the similar manner. I have also checked a few users of it and bloat-o-meter showed no change.

Fixes 63c15822b8dd ("lib/bitmap: add bitmap_{read,write}()")
---
 include/linux/bitmap.h | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)
include/linux/bitmap.h +11 -13
index adafbcf2016b..f331cf06130d 100644
@@ -846,7 +846,7 @@ unsigned long bitmap_read(const unsigned long *map, unsigned long start, unsigne
 	if (unlikely(!nbits || nbits > BITS_PER_LONG))
 		return 0;
 
-	if (space >= nbits)
+	if (space >= nbits || offset == 0)
 		return (map[index] >> offset) & BITMAP_LAST_WORD_MASK(nbits);
 
 	value_low = map[index] & BITMAP_FIRST_WORD_MASK(start);
@@ -874,25 +874,23 @@ void bitmap_write(unsigned long *map, unsigned long value,
 		  unsigned long start, unsigned long nbits)
 {
 	size_t index;
-	unsigned long offset;
-	unsigned long space;
-	unsigned long mask;
-	bool fit;
+	unsigned long offset = start % BITS_PER_LONG;
+	unsigned long space = BITS_PER_LONG - offset;
 
 	if (unlikely(!nbits || nbits > BITS_PER_LONG))
 		return;
 
-	mask = BITMAP_LAST_WORD_MASK(nbits);
-	value &= mask;
-	offset = start % BITS_PER_LONG;
-	space = BITS_PER_LONG - offset;
-	fit = space >= nbits;
+	value &= BITMAP_LAST_WORD_MASK(nbits);
 	index = BIT_WORD(start);
 
-	map[index] &= (fit ? (~(mask << offset)) : ~BITMAP_FIRST_WORD_MASK(start));
-	map[index] |= value << offset;
-	if (fit)
+	if (space >= nbits || offset == 0) {
+		map[index] &= ~(BITMAP_LAST_WORD_MASK(nbits) << offset);
+		map[index] |= value << offset;
 		return;
+	}
+
+	map[index] &= ~BITMAP_FIRST_WORD_MASK(start);
+	map[index] |= value << offset;
 
 	map[index + 1] &= BITMAP_FIRST_WORD_MASK(start + nbits);
 	map[index + 1] |= (value >> space);
Signature · 1 line
2.50.1
#1 lore raw

Hi Andy!

+ sparse people.

Quoted 6 lines · Andy Shevchenko

While compiling ad4691.c I have noticed the following warnings:

drivers/iio/adc/ad4691.c: note: in included file:
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

Quoted 8 lines

Since the bitmap_read() is called with statically define 'start', the 'space' local variable becomes equal to BITS_PER_LONG, or 64 on 64-bit machines. The 'nbits' parameter is coming from the run-time defined value. Static analyser doesn't see that the right shift by 64-bit can't happen — the space >= nbits check guards that. While Jonathan put it as false positive [1], we still can give a hint to it without growing in the code (bloat-o-meter for above mentioned driver shows no difference in size). So this is what this change does.

Jonathan put it right.

If the analyzer (assuming sparse?) throws false positives, we don't need to change the kernel. This is simply a wrong motivation. We need to improve sparse instead.

Otherwise, the kernel will become a collection of hacks for various poorly written tools. Let's do it the other way, please.

Quoted 15 lines

For completeness, bitmap_write() is also refactored in the similar manner. I have also checked a few users of it and bloat-o-meter showed no change.

Fixes 63c15822b8dd ("lib/bitmap: add bitmap_{read,write}()")
---
 include/linux/bitmap.h | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)
include/linux/bitmap.h +0 -0
index adafbcf2016b..f331cf06130d 100644

include/linux/bitmap.h unsigned long bitmap_read(const unsigned long *map, unsigned long start, unsigne · line 850

Quoted 6 lines
include/linux/bitmap.h +1 -1
@@ -846,7 +846,7 @@ unsigned long bitmap_read(const unsigned long *map, unsigned long start, unsigne
 	if (unlikely(!nbits || nbits > BITS_PER_LONG))
 		return 0;
 
-	if (space >= nbits)
+	if (space >= nbits || offset == 0)
 		return (map[index] >> offset) & BITMAP_LAST_WORD_MASK(nbits);

So, doubling the number of conditions, and the new one duplicates the existing check. This version suppresses sparse false positive:

if (offset <= BITS_PER_LONG - nbits)

And even with that I'm not sure we need to touch the kernel to make sparse or whatever else tool happy...

Quoted 4 lines
include/linux/bitmap.h +0 -0
 	value_low = map[index] & BITMAP_FIRST_WORD_MASK(start);
@@ -874,25 +874,23 @@ void bitmap_write(unsigned long *map, unsigned long value,
 		  unsigned long start, unsigned long nbits)
 {

include/linux/bitmap.h void bitmap_write(unsigned long *map, unsigned long value, · line 881

Quoted 10 lines
include/linux/bitmap.h +2 -4
 	size_t index;
-	unsigned long offset;
-	unsigned long space;
-	unsigned long mask;
-	bool fit;
+	unsigned long offset = start % BITS_PER_LONG;
+	unsigned long space = BITS_PER_LONG - offset;
 
 	if (unlikely(!nbits || nbits > BITS_PER_LONG))
 		return;

Initialization of local variables before the sanity check may become useless waste of cycles if this sanity fails.

This is also not necessarily needed for sparse happiness, I guess... Let's keep the patch minimal?

include/linux/bitmap.h void bitmap_write(unsigned long *map, unsigned long value, · line 889

Quoted 5 lines
include/linux/bitmap.h +0 -5
-	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?

Thanks,
Yury

Quoted 19 lines
include/linux/bitmap.h +8 -3
+	value &= BITMAP_LAST_WORD_MASK(nbits);
 	index = BIT_WORD(start);
 
-	map[index] &= (fit ? (~(mask << offset)) : ~BITMAP_FIRST_WORD_MASK(start));
-	map[index] |= value << offset;
-	if (fit)
+	if (space >= nbits || offset == 0) {
+		map[index] &= ~(BITMAP_LAST_WORD_MASK(nbits) << offset);
+		map[index] |= value << offset;
 		return;
+	}
+
+	map[index] &= ~BITMAP_FIRST_WORD_MASK(start);
+	map[index] |= value << offset;
 
 	map[index + 1] &= BITMAP_FIRST_WORD_MASK(start + nbits);
 	map[index + 1] |= (value >> space);
Signature · 1 line
2.50.1
#2 lore raw
Quoted 1 line · Yury Norov

On Thu, Nov 19, 2026 at 08:15:30PM +0100, Andy Shevchenko wrote:

Thanks for review, my answers below.

Quoted 15 lines
Quoted 6 lines

While compiling ad4691.c I have noticed the following warnings:

drivers/iio/adc/ad4691.c: note: in included file:
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

Quoted 8 lines

Since the bitmap_read() is called with statically define 'start', the 'space' local variable becomes equal to BITS_PER_LONG, or 64 on 64-bit machines. The 'nbits' parameter is coming from the run-time defined value. Static analyser doesn't see that the right shift by 64-bit can't happen — the space >= nbits check guards that. While Jonathan put it as false positive [1], we still can give a hint to it without growing in the code (bloat-o-meter for above mentioned driver shows no difference in size). So this is what this change does.

Quoted 8 lines

Jonathan put it right.

If the analyzer (assuming sparse?) throws false positives, we don't need to change the kernel. This is simply a wrong motivation. We need to improve sparse instead.

Otherwise, the kernel will become a collection of hacks for various poorly written tools. Let's do it the other way, please.

I agree in general. However, we can sometimes give hints without breaking the code. Now, thinking of this, the mentioned check can be done against offset leaving code generation and sparse happy and have no unneeded bulk.

Quoted 2 lines
Quoted 2 lines

For completeness, bitmap_write() is also refactored in the similar manner. I have also checked a few users of it and bloat-o-meter showed no change.

...

Quoted 8 lines

include/linux/bitmap.h unsigned long bitmap_read(const unsigned long *map, unsigned long start, unsigne · line 850

Quoted 3 lines
include/linux/bitmap.h +1 -1
-	if (space >= nbits)
+	if (space >= nbits || offset == 0)
 		return (map[index] >> offset) & BITMAP_LAST_WORD_MASK(nbits);

So, doubling the number of conditions, and the new one duplicates the existing check. This version suppresses sparse false positive:

if (offset <= BITS_PER_LONG - nbits)

Yes, you beat me up to it. I just was thinking about similar check in the above comment.

Quoted 2 lines

And even with that I'm not sure we need to touch the kernel to make sparse or whatever else tool happy...

It might take ages for sparse to fix even more annoying issues (if you remember something related to cleanup or alike change in the kernel and sparse was lagging for weeks).

...

Quoted 8 lines
Quoted 8 lines
include/linux/bitmap.h +2 -4
 	size_t index;
-	unsigned long offset;
-	unsigned long space;
-	unsigned long mask;
-	bool fit;
+	unsigned long offset = start % BITS_PER_LONG;
+	unsigned long space = BITS_PER_LONG - offset;
Quoted 5 lines

include/linux/bitmap.h void bitmap_write(unsigned long *map, unsigned long value, · line 881

Quoted 2 lines
include/linux/bitmap.h +0 -0
 	if (unlikely(!nbits || nbits > BITS_PER_LONG))
 		return;

Initialization of local variables before the sanity check may become useless waste of cycles if this sanity fails.

How? they are not used and function is marked as always inline. I believe optimisation in the compiler does the right thing.

Quoted 1 line

This is also not necessarily needed for sparse happiness,

This is to make both look similar at a glance.

Quoted 10 lines

I guess... Let's keep the patch minimal?

include/linux/bitmap.h void bitmap_write(unsigned long *map, unsigned long value, · line 889

Quoted 5 lines
include/linux/bitmap.h +0 -5
-	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.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw
Quoted 67 lines · Andy Shevchenko
Quoted 1 line · Yury Norov

On Thu, Nov 19, 2026 at 08:15:30PM +0100, Andy Shevchenko wrote:

Thanks for review, my answers below.

Quoted 15 lines
Quoted 6 lines

While compiling ad4691.c I have noticed the following warnings:

drivers/iio/adc/ad4691.c: note: in included file:
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

Quoted 8 lines

Since the bitmap_read() is called with statically define 'start', the 'space' local variable becomes equal to BITS_PER_LONG, or 64 on 64-bit machines. The 'nbits' parameter is coming from the run-time defined value. Static analyser doesn't see that the right shift by 64-bit can't happen — the space >= nbits check guards that. While Jonathan put it as false positive [1], we still can give a hint to it without growing in the code (bloat-o-meter for above mentioned driver shows no difference in size). So this is what this change does.

Quoted 8 lines

Jonathan put it right.

If the analyzer (assuming sparse?) throws false positives, we don't need to change the kernel. This is simply a wrong motivation. We need to improve sparse instead.

Otherwise, the kernel will become a collection of hacks for various poorly written tools. Let's do it the other way, please.

I agree in general. However, we can sometimes give hints without breaking the code. Now, thinking of this, the mentioned check can be done against offset leaving code generation and sparse happy and have no unneeded bulk.

Quoted 2 lines
Quoted 2 lines

For completeness, bitmap_write() is also refactored in the similar manner. I have also checked a few users of it and bloat-o-meter showed no change.

...

Quoted 8 lines

Review

Quoted 3 lines
(quoted diff) +1 -1
-	if (space >= nbits)
+	if (space >= nbits || offset == 0)
 		return (map[index] >> offset) & BITMAP_LAST_WORD_MASK(nbits);

So, doubling the number of conditions, and the new one duplicates the existing check. This version suppresses sparse false positive:

if (offset <= BITS_PER_LONG - nbits)

Yes, you beat me up to it. I just was thinking about similar check in the above comment.

Quoted 2 lines

And even with that I'm not sure we need to touch the kernel to make sparse or whatever else tool happy...

It might take ages for sparse to fix even more annoying issues (if you remember something related to cleanup or alike change in the kernel and sparse was lagging for weeks).

...

Quoted 8 lines
Quoted 8 lines
(quoted diff) +2 -4
 	size_t index;
-	unsigned long offset;
-	unsigned long space;
-	unsigned long mask;
-	bool fit;
+	unsigned long offset = start % BITS_PER_LONG;
+	unsigned long space = BITS_PER_LONG - offset;
Quoted 8 lines
Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +0 -0
 	if (unlikely(!nbits || nbits > BITS_PER_LONG))
 		return;

Initialization of local variables before the sanity check may become useless waste of cycles if this sanity fails.

How? they are not used and function is marked as always inline. I believe optimisation in the compiler does the right thing.

Maybe yes. I've seen quite many non-trivial compiler decisions before, so I want to make sure...

Quoted 15 lines
Quoted 1 line

This is also not necessarily needed for sparse happiness,

This is to make both look similar at a glance.

Quoted 10 lines

I guess... Let's keep the patch minimal?

Review

Quoted 5 lines
(quoted diff) +0 -5
-	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?

Quoted 3 lines

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.

Thanks,
Yury

From dfac9a524aa8658fe63c03804cca9f0980b58ef7 Mon Sep 17 00:00:00 2001
From: Yury Norov <[email protected]>
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

---
 include/linux/bitmap.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/bitmap.h +2 -2
index adafbcf2016b..08de1467367e 100644
@@ -846,7 +846,7 @@ unsigned long bitmap_read(const unsigned long *map, unsigned long start, unsigne
 	if (unlikely(!nbits || nbits > BITS_PER_LONG))
 		return 0;
 
-	if (space >= nbits)
+	if (offset <= BITS_PER_LONG - nbits)
 		return (map[index] >> offset) & BITMAP_LAST_WORD_MASK(nbits);
 
 	value_low = map[index] & BITMAP_FIRST_WORD_MASK(start);
@@ -886,7 +886,7 @@ void bitmap_write(unsigned long *map, unsigned long value,
 	value &= mask;
 	offset = start % BITS_PER_LONG;
 	space = BITS_PER_LONG - offset;
-	fit = space >= nbits;
+	fit = offset <= BITS_PER_LONG - nbits;
 	index = BIT_WORD(start);
 
 	map[index] &= (fit ? (~(mask << offset)) : ~BITMAP_FIRST_WORD_MASK(start));
Signature · 1 line
2.53.0
author Tested-by
#4 lore raw
Quoted 3 lines · Yury Norov
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Yury Norov

On Thu, Nov 19, 2026 at 08:15:30PM +0100, Andy Shevchenko wrote:

...

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

I guess... Let's keep the patch minimal?

Review

Quoted 5 lines
(quoted diff) +0 -5
-	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?

Quoted 6 lines
Quoted 3 lines

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.

Quoted 25 lines

From dfac9a524aa8658fe63c03804cca9f0980b58ef7 Mon Sep 17 00:00:00 2001
From: Yury Norov <[email protected]>
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
Quoted 4 lines

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

Quoted 1 line

Should be Closes: tag.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Yury Norov

On Thu, Nov 19, 2026 at 11:03:53PM +0200, Andy Shevchenko wrote:

...

Quoted 4 lines
Quoted 1 line

Should be Closes: tag.

Thinking of this more, you should probably add Reported-by: Jonathan and the link I mentioned in my patch and drop this one.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw
In reply to Andy Shevchenko
Quoted 22 lines · Andy Shevchenko
Quoted 3 lines · Yury Norov
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Yury Norov

On Thu, Nov 19, 2026 at 08:15:30PM +0100, Andy Shevchenko wrote:

...

Quoted 15 lines
Quoted 14 lines
Quoted 10 lines

I guess... Let's keep the patch minimal?

Review

Quoted 5 lines
(quoted diff) +0 -5
-	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.

Quoted 5 lines
Quoted 2 lines

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.

Quoted 39 lines
Quoted 25 lines

From dfac9a524aa8658fe63c03804cca9f0980b58ef7 Mon Sep 17 00:00:00 2001
From: Yury Norov <[email protected]>
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
Quoted 4 lines

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

Quoted 1 line

Should be Closes: tag.

Signature · 2 lines
With Best Regards,
Andy Shevchenko