[PATCH 2/2] lib/test_bitmap: cover unused tail bits in bitmap_cut()

From: Jinseob Kim

Date: Sun Nov 22 2026 - 23:33:09 EST


Exercise 33-, 65-, and 127-bit bitmaps with pairs of inputs that
differ only in the first unused storage bit. Check the valid output
against a per-bit reference and compare it with the output from the
second input.

Cover in-place and disjoint buffers and both directions of partial
overlap when the bitmap spans multiple words. Check that disjoint
source storage is preserved.

Signed-off-by: Jinseob Kim <kimjinseob88@xxxxxxxxx>
---
lib/test_bitmap.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 90 insertions(+), 1 deletion(-)

diff --git a/lib/test_bitmap.c b/lib/test_bitmap.c
index eb925d576..104fc23a4 100644
--- a/lib/test_bitmap.c
+++ b/lib/test_bitmap.c
@@ -1145,6 +1145,7 @@ static struct test_bitmap_cut test_cut[] = {
static void __init test_bitmap_cut(void)
{
unsigned long b[5], *in = &b[1], *out = &b[0]; /* Partial overlap */
+ unsigned int initial_failures = failed_tests;
int i;

for (i = 0; i < ARRAY_SIZE(test_cut); i++) {
@@ -1154,10 +1155,97 @@ static void __init test_bitmap_cut(void)

bitmap_cut(out, in, t->first, t->cut, t->nbits);

- expect_eq_bitmap(t->expected, out, t->nbits);
+ if (expect_eq_bitmap(t->expected, out, t->nbits))
+ pr_warn("bitmap_cut existing row=%d first=%u cut=%u nbits=%u\n",
+ i + 1, t->first, t->cut, t->nbits);
}
+
+ pr_info("bitmap_cut existing rows=%zu failures=%u\n", ARRAY_SIZE(test_cut),
+ failed_tests - initial_failures);
}

+#define BITMAP_CUT_TEST_MAX_BITS 127
+
+static void __init test_bitmap_cut_padding_case(unsigned int nbits, unsigned int first,
+ unsigned long *dst, unsigned long *src,
+ const char *layout, bool separate)
+{
+ DECLARE_BITMAP(input, BITMAP_CUT_TEST_MAX_BITS);
+ DECLARE_BITMAP(expected, BITMAP_CUT_TEST_MAX_BITS);
+ DECLARE_BITMAP(clean, BITMAP_CUT_TEST_MAX_BITS);
+ unsigned int bit;
+ size_t bytes = bitmap_size(nbits);
+
+ bitmap_zero(input, nbits);
+ bitmap_zero(expected, nbits);
+
+ if (first) {
+ /* Preserve bit 1, remove bit 2, and move the bits above it. */
+ __set_bit(1, input);
+ __set_bit(2, input);
+ __set_bit(4, input);
+ __set_bit(nbits - 1, input);
+ }
+
+ for (bit = 0; bit < nbits; bit++) {
+ unsigned int source = bit + (bit >= first);
+
+ if (source < nbits && test_bit(source, input))
+ __set_bit(bit, expected);
+ }
+
+ bitmap_copy(src, input, nbits);
+ bitmap_cut(dst, src, first, 1, nbits);
+ if (expect_eq_bitmap(expected, dst, nbits))
+ pr_warn("bitmap_cut clean nbits=%u first=%u layout=%s\n", nbits, first, layout);
+ if (separate && expect_eq_uint(0, memcmp(src, input, bytes) != 0))
+ pr_warn("bitmap_cut clean input changed nbits=%u first=%u\n", nbits, first);
+ bitmap_copy(clean, dst, nbits);
+
+ /* The first padding bit is in storage, but outside the bitmap. */
+ __set_bit(nbits, input);
+ bitmap_copy(src, input, nbits);
+ bitmap_cut(dst, src, first, 1, nbits);
+ if (expect_eq_bitmap(clean, dst, nbits))
+ pr_warn("bitmap_cut padding nbits=%u first=%u layout=%s\n", nbits, first, layout);
+ if (separate && expect_eq_uint(0, memcmp(src, input, bytes) != 0))
+ pr_warn("bitmap_cut padded input changed nbits=%u first=%u\n", nbits, first);
+}
+
+static void __init test_bitmap_cut_padding(void)
+{
+ static const unsigned int sizes[] = { 33, 65, BITMAP_CUT_TEST_MAX_BITS };
+ static const unsigned int starts[] = { 0, 2 };
+ unsigned long buf[2 * BITS_TO_LONGS(BITMAP_CUT_TEST_MAX_BITS)];
+ unsigned int initial_failures = failed_tests;
+ unsigned int i, j, pairs = 0;
+
+ for (i = 0; i < ARRAY_SIZE(sizes); i++) {
+ unsigned int nbits = sizes[i];
+ unsigned int len = BITS_TO_LONGS(nbits);
+
+ for (j = 0; j < ARRAY_SIZE(starts); j++) {
+ test_bitmap_cut_padding_case(nbits, starts[j], buf, buf, "in-place", false);
+ test_bitmap_cut_padding_case(nbits, starts[j], buf, buf + len,
+ "separate", true);
+ pairs += 2;
+
+ if (len > 1) {
+ test_bitmap_cut_padding_case(nbits, starts[j], buf, buf + 1,
+ "dst-before-src", false);
+ test_bitmap_cut_padding_case(nbits, starts[j], buf + 1, buf,
+ "dst-after-src", false);
+ pairs += 2;
+ }
+ }
+ }
+
+ pr_info("bitmap_cut padding pairs=%u failures=%u\n", pairs,
+ failed_tests - initial_failures);
+}
+
+#undef BITMAP_CUT_TEST_MAX_BITS
+
struct test_bitmap_print {
const unsigned long *bitmap;
unsigned long nbits;
@@ -1587,6 +1675,7 @@ static void __init selftest(void)
test_bitmap_printlist();
test_mem_optimisations();
test_bitmap_cut();
+ test_bitmap_cut_padding();
test_bitmap_print_buf();
test_bitmap_const_eval();
test_bitmap_read_write();