[PATCH 12/21] bitmap: don't use GENMASK()

Yury Norov (NVIDIA)
2025-10-25T16:33:21Z

GENMASK(high, low) notation is confusing. Switch to a more natural
BITS(low, high) mask generator, or FIRST/LAST_BITS() as appropriate.

Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
---
include/linux/bitmap.h | 4 ++--
include/linux/find.h | 34 +++++++++++++++++-----------------
lib/bitmap.c | 2 +-
lib/test_bitmap.c | 14 +++++++-------
4 files changed, 27 insertions(+), 27 deletions(-)

diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h
index fbe2d12bceab..3bed29f1780b 100644
--- a/include/linux/bitmap.h
+++ b/include/linux/bitmap.h
@@ -473,7 +473,7 @@ void bitmap_set(unsigned long *map, unsigned int start, unsigned int nbits)
if (__builtin_constant_p(nbits) && nbits == 1)
__set_bit(start, map);
else if (small_const_nbits(start + nbits))
- *map |= GENMASK(start + nbits - 1, start);
+ *map |= BITS(start, start + nbits - 1);
else if (__builtin_constant_p(start & BITMAP_MEM_MASK) &&
IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) &&
__builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
@@ -489,7 +489,7 @@ void bitmap_clear(unsigned long *map, unsigned int start, unsigned int nbits)
if (__builtin_constant_p(nbits) && nbits == 1)
__clear_bit(start, map);
else if (small_const_nbits(start + nbits))
- *map &= ~GENMASK(start + nbits - 1, start);
+ *map &= ~BITS(start, start + nbits - 1);
else if (__builtin_constant_p(start & BITMAP_MEM_MASK) &&
IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) &&
__builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
diff --git a/include/linux/find.h b/include/linux/find.h
index 9d720ad92bc1..24d7266fd02b 100644
--- a/include/linux/find.h
+++ b/include/linux/find.h
@@ -66,7 +66,7 @@ unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
if (unlikely(offset >= size))
return size;

- val = *addr & GENMASK(size - 1, offset);
+ val = *addr & BITS(offset, size - 1);
return val ? __ffs(val) : size;
}

@@ -96,7 +96,7 @@ unsigned long find_next_and_bit(const unsigned long *addr1,
if (unlikely(offset >= size))
return size;

- val = *addr1 & *addr2 & GENMASK(size - 1, offset);
+ val = *addr1 & *addr2 & BITS(offset, size - 1);
return val ? __ffs(val) : size;
}

@@ -127,7 +127,7 @@ unsigned long find_next_andnot_bit(const unsigned long *addr1,
if (unlikely(offset >= size))
return size;

- val = *addr1 & ~*addr2 & GENMASK(size - 1, offset);
+ val = *addr1 & ~*addr2 & BITS(offset, size - 1);
return val ? __ffs(val) : size;
}

@@ -157,7 +157,7 @@ unsigned long find_next_or_bit(const unsigned long *addr1,
if (unlikely(offset >= size))
return size;

- val = (*addr1 | *addr2) & GENMASK(size - 1, offset);
+ val = (*addr1 | *addr2) & BITS(offset, size - 1);
return val ? __ffs(val) : size;
}

@@ -185,7 +185,7 @@ unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
if (unlikely(offset >= size))
return size;

- val = *addr | ~GENMASK(size - 1, offset);
+ val = *addr | ~BITS(offset, size - 1);
return val == ~0UL ? size : ffz(val);
}

@@ -206,7 +206,7 @@ static __always_inline
unsigned long find_first_bit(const unsigned long *addr, unsigned long size)
{
if (small_const_nbits(size)) {
- unsigned long val = *addr & GENMASK(size - 1, 0);
+ unsigned long val = *addr & FIRST_BITS(size);

return val ? __ffs(val) : size;
}
@@ -235,7 +235,7 @@ unsigned long find_nth_bit(const unsigned long *addr, unsigned long size, unsign
return size;

if (small_const_nbits(size)) {
- unsigned long val = *addr & GENMASK(size - 1, 0);
+ unsigned long val = *addr & FIRST_BITS(size);

return val ? fns(val, n) : size;
}
@@ -261,7 +261,7 @@ unsigned long find_nth_and_bit(const unsigned long *addr1, const unsigned long *
return size;

if (small_const_nbits(size)) {
- unsigned long val = *addr1 & *addr2 & GENMASK(size - 1, 0);
+ unsigned long val = *addr1 & *addr2 & FIRST_BITS(size);

return val ? fns(val, n) : size;
}
@@ -291,7 +291,7 @@ unsigned long find_nth_and_andnot_bit(const unsigned long *addr1,
return size;

if (small_const_nbits(size)) {
- unsigned long val = *addr1 & *addr2 & (~*addr3) & GENMASK(size - 1, 0);
+ unsigned long val = *addr1 & *addr2 & (~*addr3) & FIRST_BITS(size);

return val ? fns(val, n) : size;
}
@@ -315,7 +315,7 @@ unsigned long find_first_and_bit(const unsigned long *addr1,
unsigned long size)
{
if (small_const_nbits(size)) {
- unsigned long val = *addr1 & *addr2 & GENMASK(size - 1, 0);
+ unsigned long val = *addr1 & *addr2 & FIRST_BITS(size);

return val ? __ffs(val) : size;
}
@@ -339,7 +339,7 @@ unsigned long find_first_andnot_bit(const unsigned long *addr1,
unsigned long size)
{
if (small_const_nbits(size)) {
- unsigned long val = *addr1 & (~*addr2) & GENMASK(size - 1, 0);
+ unsigned long val = *addr1 & (~*addr2) & FIRST_BITS(size);

return val ? __ffs(val) : size;
}
@@ -364,7 +364,7 @@ unsigned long find_first_and_and_bit(const unsigned long *addr1,
unsigned long size)
{
if (small_const_nbits(size)) {
- unsigned long val = *addr1 & *addr2 & *addr3 & GENMASK(size - 1, 0);
+ unsigned long val = *addr1 & *addr2 & *addr3 & FIRST_BITS(size);

return val ? __ffs(val) : size;
}
@@ -385,7 +385,7 @@ static __always_inline
unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)
{
if (small_const_nbits(size)) {
- unsigned long val = *addr | ~GENMASK(size - 1, 0);
+ unsigned long val = *addr | ~FIRST_BITS(size);

return val == ~0UL ? size : ffz(val);
}
@@ -406,7 +406,7 @@ static __always_inline
unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
{
if (small_const_nbits(size)) {
- unsigned long val = *addr & GENMASK(size - 1, 0);
+ unsigned long val = *addr & FIRST_BITS(size);

return val ? __fls(val) : size;
}
@@ -537,7 +537,7 @@ unsigned long find_next_zero_bit_le(const void *addr, unsigned
if (unlikely(offset >= size))
return size;

- val = swab(val) | ~GENMASK(size - 1, offset);
+ val = swab(val) | ~BITS(offset, size - 1);
return val == ~0UL ? size : ffz(val);
}

@@ -550,7 +550,7 @@ static __always_inline
unsigned long find_first_zero_bit_le(const void *addr, unsigned long size)
{
if (small_const_nbits(size)) {
- unsigned long val = swab(*(const unsigned long *)addr) | ~GENMASK(size - 1, 0);
+ unsigned long val = swab(*(const unsigned long *)addr) | ~FIRST_BITS(size);

return val == ~0UL ? size : ffz(val);
}
@@ -570,7 +570,7 @@ unsigned long find_next_bit_le(const void *addr, unsigned
if (unlikely(offset >= size))
return size;

- val = swab(val) & GENMASK(size - 1, offset);
+ val = swab(val) & BITS(offset, size - 1);
return val ? __ffs(val) : size;
}

diff --git a/lib/bitmap.c b/lib/bitmap.c
index b97692854966..ec11cc36624e 100644
--- a/lib/bitmap.c
+++ b/lib/bitmap.c
@@ -876,7 +876,7 @@ void bitmap_to_arr64(u64 *buf, const unsigned long *bitmap, unsigned int nbits)

/* Clear tail bits in the last element of array beyond nbits. */
if (nbits % 64)
- buf[-1] &= GENMASK_ULL((nbits - 1) % 64, 0);
+ buf[-1] &= FIRST_BITS_ULL(nbits);
}
EXPORT_SYMBOL(bitmap_to_arr64);
#endif
diff --git a/lib/test_bitmap.c b/lib/test_bitmap.c
index c83829ef557f..c198fc7a66d2 100644
--- a/lib/test_bitmap.c
+++ b/lib/test_bitmap.c
@@ -692,10 +692,10 @@ static void __init test_bitmap_arr64(void)
}

if ((nbits % 64) &&
- (arr[(nbits - 1) / 64] & ~GENMASK_ULL((nbits - 1) % 64, 0))) {
+ (arr[(nbits - 1) / 64] & ~FIRST_BITS_ULL(nbits))) {
pr_err("bitmap_to_arr64(nbits == %d): tail is not safely cleared: 0x%016llx (must be 0x%016llx)\n",
nbits, arr[(nbits - 1) / 64],
- GENMASK_ULL((nbits - 1) % 64, 0));
+ FIRST_BITS_ULL(nbits));
failed_tests++;
}

@@ -1217,7 +1217,7 @@ static void __init test_bitmap_const_eval(void)
* in runtime.
*/

- /* Equals to `unsigned long bitmap[1] = { GENMASK(6, 5), }` */
+ /* Equals to `unsigned long bitmap[1] = { BITS(5, 6), }` */
bitmap_clear(bitmap, 0, BITS_PER_LONG);
if (!test_bit(7, bitmap))
bitmap_set(bitmap, 5, 2);
@@ -1229,9 +1229,9 @@ static void __init test_bitmap_const_eval(void)
/* Equals to `unsigned long var = BIT(25)` */
var |= BIT(25);
if (var & BIT(0))
- var ^= GENMASK(9, 6);
+ var ^= BITS(6, 9);

- /* __const_hweight<32|64>(GENMASK(6, 5)) == 2 */
+ /* __const_hweight<32|64>(BITS(5, 6)) == 2 */
res = bitmap_weight(bitmap, 20);
BUILD_BUG_ON(!__builtin_constant_p(res));
BUILD_BUG_ON(res != 2);
@@ -1241,8 +1241,8 @@ static void __init test_bitmap_const_eval(void)
BUILD_BUG_ON(!__builtin_constant_p(res));
BUILD_BUG_ON(!res);

- /* BIT(2) & GENMASK(14, 8) == 0 */
- res = initvar & GENMASK(14, 8);
+ /* BIT(2) & BITS(8, 14) == 0 */
+ res = initvar & BITS(8, 14);
BUILD_BUG_ON(!__builtin_constant_p(res));
BUILD_BUG_ON(res);

--
2.43.0