mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] bitmap: keep unused tail bits out of bitmap_cut() results
@ 2026-11-23  4:31 Jinseob Kim
  2026-11-23  4:31 ` [PATCH 1/2] bitmap: ignore unused tail bits in bitmap_cut() Jinseob Kim
  2026-11-23  4:31 ` [PATCH 2/2] lib/test_bitmap: cover " Jinseob Kim
  0 siblings, 2 replies; 3+ messages in thread
From: Jinseob Kim @ 2026-11-23  4:31 UTC (permalink / raw)
  To: yury.norov; +Cc: linux, akpm, linux-kernel

bitmap_cut() can shift an unused bit from the last storage word
into the valid result. Patch 1 masks the copied tail before
shifting and corrects the existing test expectation. Patch 2
adds checks for partial words and overlapping buffers.

With identical CONFIG_TEST_BITMAP sources and configuration on
x86_64 UML with GCC 13.3.0, the original failed 21 of 391,596
checks and the fixed version passed all 391,596. Clang 18.1.3
compiled both modified objects. A 32-bit kernel and a
Clang-built kernel were not booted.

Jinseob Kim (2):
  bitmap: ignore unused tail bits in bitmap_cut()
  lib/test_bitmap: cover unused tail bits in bitmap_cut()

 lib/bitmap.c      |  2 +
 lib/test_bitmap.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 93 insertions(+), 2 deletions(-)


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/2] bitmap: ignore unused tail bits in bitmap_cut()
  2026-11-23  4:31 [PATCH 0/2] bitmap: keep unused tail bits out of bitmap_cut() results Jinseob Kim
@ 2026-11-23  4:31 ` Jinseob Kim
  2026-11-23  4:31 ` [PATCH 2/2] lib/test_bitmap: cover " Jinseob Kim
  1 sibling, 0 replies; 3+ messages in thread
From: Jinseob Kim @ 2026-11-23  4:31 UTC (permalink / raw)
  To: yury.norov; +Cc: linux, akpm, linux-kernel

When nbits is not a multiple of BITS_PER_LONG, bitmap_cut() copies
the entire last word. A subsequent right shift can move unused tail
bits into the valid result. For example, cutting bit 0 from a 65-bit
bitmap can move storage bit 65 into output bit 64.

The bitmap API requires unused tail bits to have no effect on the
result. Mask the copied destination word before shifting. Keep the
existing pre-memmove() read of the bits preceding the cut so that
overlapping buffers continue to work.

The last existing test case removes all valid bits above bit 15.
Change its expected value to 0x0000ffff, which contains only the
preserved bits.

Fixes: 2092767168f0 ("bitmap: Introduce bitmap_cut(): cut bits and shift remaining")
Signed-off-by: Jinseob Kim <kimjinseob88@gmail.com>
---
 lib/bitmap.c      | 2 ++
 lib/test_bitmap.c | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/lib/bitmap.c b/lib/bitmap.c
index d1cb8a507..50cef429e 100644
--- a/lib/bitmap.c
+++ b/lib/bitmap.c
@@ -210,6 +210,8 @@ void bitmap_cut(unsigned long *dst, const unsigned long *src,
 	}
 
 	memmove(dst, src, len * sizeof(*dst));
+	if (nbits % BITS_PER_LONG)
+		dst[len - 1] &= BITMAP_LAST_WORD_MASK(nbits);
 
 	while (cut--) {
 		for (i = first / BITS_PER_LONG; i < len; i++) {
diff --git a/lib/test_bitmap.c b/lib/test_bitmap.c
index ba09d057f..eb925d576 100644
--- a/lib/test_bitmap.c
+++ b/lib/test_bitmap.c
@@ -1138,7 +1138,7 @@ static struct test_bitmap_cut test_cut[] = {
 	},
 	{ 16, BITS_PER_LONG * 2 + 1, BITS_PER_LONG * 2 + 1 + 16,
 		{ 0x0000ffffUL, 0x5a5a5a5aUL, 0x5a5a5a5aUL, 0x5a5a5a5aUL },
-		{ 0x2d2dffffUL, },
+		{ 0x0000ffffUL, },
 	},
 };
 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 2/2] lib/test_bitmap: cover unused tail bits in bitmap_cut()
  2026-11-23  4:31 [PATCH 0/2] bitmap: keep unused tail bits out of bitmap_cut() results Jinseob Kim
  2026-11-23  4:31 ` [PATCH 1/2] bitmap: ignore unused tail bits in bitmap_cut() Jinseob Kim
@ 2026-11-23  4:31 ` Jinseob Kim
  1 sibling, 0 replies; 3+ messages in thread
From: Jinseob Kim @ 2026-11-23  4:31 UTC (permalink / raw)
  To: yury.norov; +Cc: linux, akpm, linux-kernel

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@gmail.com>
---
 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();

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-23  4:33 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-23  4:31 [PATCH 0/2] bitmap: keep unused tail bits out of bitmap_cut() results Jinseob Kim
2026-11-23  4:31 ` [PATCH 1/2] bitmap: ignore unused tail bits in bitmap_cut() Jinseob Kim
2026-11-23  4:31 ` [PATCH 2/2] lib/test_bitmap: cover " Jinseob Kim

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®