[PATCH 1/2] bitmap: add bitmap_all_set() and bitmap_all_clear()

From: Alessio Maroni

Date: Sun Dec 06 2026 - 08:52:35 EST


Add bitmap_all_set() and bitmap_all_clear() helpers to check whether
all bits in an arbitrary [start, start + nbits) range are set or
clear, generalizing bitmap_full() and bitmap_empty() with the same
(map, start, nbits) signature style as bitmap_set() and bitmap_clear().

Include compile-time fast paths for zero, single-bit, and small
single-word ranges, falling back to find_next_zero_bit() and
find_next_bit() otherwise. Add corresponding test cases in
lib/test_bitmap.c.

Signed-off-by: Alessio Maroni <alessiomaroni0@xxxxxxxxx>
---
include/linux/bitmap.h | 47 ++++++++++++++++++++++++++++++++++++++++++
lib/test_bitmap.c | 38 ++++++++++++++++++++++++++++++++++
2 files changed, 85 insertions(+)

diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h
index 08de1467367e..bc6eff615f27 100644
--- a/include/linux/bitmap.h
+++ b/include/linux/bitmap.h
@@ -576,6 +576,53 @@ void bitmap_clear(unsigned long *map, unsigned int start, unsigned int nbits)
__bitmap_clear(map, start, nbits);
}

+/**
+ * bitmap_all_set - check if all bits in a region are set
+ * @map: address to the bitmap memory region
+ * @start: bit offset of the region
+ * @nbits: number of bits to check
+ *
+ * Return: true if all @nbits bits starting at @start are set,
+ * false otherwise.
+ */
+static __always_inline
+bool bitmap_all_set(const unsigned long *map, unsigned int start,
+ unsigned int nbits)
+{
+ if (unlikely(!nbits))
+ return true;
+ if (__builtin_constant_p(nbits) && nbits == 1)
+ return test_bit(start, map);
+ if (small_const_nbits(start + nbits))
+ return (*map & GENMASK(start + nbits - 1, start)) ==
+ GENMASK(start + nbits - 1, start);
+
+ return find_next_zero_bit(map, start + nbits, start) >= start + nbits;
+}
+
+/**
+ * bitmap_all_clear - check if all bits in a region are clear
+ * @map: address to the bitmap memory region
+ * @start: bit offset of the region
+ * @nbits: number of bits to check
+ *
+ * Return: true if all @nbits bits starting at @start are clear,
+ * false otherwise.
+ */
+static __always_inline
+bool bitmap_all_clear(const unsigned long *map, unsigned int start,
+ unsigned int nbits)
+{
+ if (unlikely(!nbits))
+ return true;
+ if (__builtin_constant_p(nbits) && nbits == 1)
+ return !test_bit(start, map);
+ if (small_const_nbits(start + nbits))
+ return !(*map & GENMASK(start + nbits - 1, start));
+
+ return find_next_bit(map, start + nbits, start) >= start + nbits;
+}
+
static __always_inline
void bitmap_shift_right(unsigned long *dst, const unsigned long *src,
unsigned int shift, unsigned int nbits)
diff --git a/lib/test_bitmap.c b/lib/test_bitmap.c
index ba09d057f47f..5b64aa420005 100644
--- a/lib/test_bitmap.c
+++ b/lib/test_bitmap.c
@@ -1541,6 +1541,8 @@ static void __init test_zero_nbits(void)
bitmap_xor(NULL, NULL, NULL, 0);
bitmap_zero(NULL, 0);

+ ret = bitmap_all_clear(NULL, 0, 0);
+ ret = bitmap_all_set(NULL, 0, 0);
ret = bitmap_and(NULL, NULL, NULL, 0);
ret = bitmap_empty(NULL, 0);
ret = bitmap_equal(NULL, NULL, 0);
@@ -1570,6 +1572,40 @@ static void __init test_zero_nbits(void)
ret = find_random_bit(NULL, 0);
}

+static void __init test_bitmap_all_set_clear(void)
+{
+ DECLARE_BITMAP(bmap, 256);
+ unsigned int start, nbits;
+ bool expected_set, expected_clear;
+
+ bitmap_zero(bmap, 256);
+ bitmap_set(bmap, 3, 17);
+ bitmap_set(bmap, 60, 12);
+ bitmap_set(bmap, 128, 64);
+
+ expect_eq_uint(true, bitmap_all_set(bmap, 3, 0));
+ expect_eq_uint(true, bitmap_all_clear(bmap, 3, 0));
+ expect_eq_uint(true, bitmap_all_set(bmap, 3, 1));
+ expect_eq_uint(false, bitmap_all_set(bmap, 2, 1));
+ expect_eq_uint(true, bitmap_all_clear(bmap, 2, 1));
+ expect_eq_uint(true, bitmap_all_set(bmap, 3, 17));
+ expect_eq_uint(false, bitmap_all_set(bmap, 3, 18));
+
+ for (start = 0; start < 200; start++) {
+ for (nbits = 0; nbits <= 256 - start; nbits++) {
+ expected_set = find_next_zero_bit(bmap, start + nbits,
+ start) >= start + nbits;
+ expected_clear = find_next_bit(bmap, start + nbits,
+ start) >= start + nbits;
+
+ expect_eq_uint(expected_set,
+ bitmap_all_set(bmap, start, nbits));
+ expect_eq_uint(expected_clear,
+ bitmap_all_clear(bmap, start, nbits));
+ }
+ }
+}
+
#undef TEST_BIT_LEN

static void __init selftest(void)
@@ -1607,6 +1643,8 @@ static void __init selftest(void)
test_for_each_set_clump8();
test_for_each_set_bit_wrap();
test_bitmap_find_next_zero_area_off();
+ test_zero_nbits();
+ test_bitmap_all_set_clear();
}

KSTM_MODULE_LOADERS(test_bitmap);
--
2.56.1