Skip to contentSkip to conversation

bitmap: convert self-test to KUnit

PATCH New Tamir Duberstein 2 participants 6 messages Dec 22, 2025 – Dec 23, 2025

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

v2 cover bitmap: convert self-test to KUnit

This is the last remaining "Test Module" kselftest, the rest having been converted to KUnit.

Relative to v1 this keeps benchmarks out of KUnit in light of Yury's concerns:

Quoted 7 lines · Yury Norov

[...]

This is my evidence: sometimes people report performance or whatever issues on their systems, suspecting bitmaps guilty. I ask them to run the bitmap or find_bit test to narrow the problem. Sometimes I need to test a hardware I have no access to, and I have to (kindly!) ask

people

Quoted 4 lines

to build a small test and run it. I don't want to ask them to rebuild the whole kernel, or even to build something else.

https://lore.kernel.org/all/YuWk3titnOiQACzC@yury-laptop/

I tested this using:

$ tools/testing/kunit/kunit.py run --arch arm64 --make_options LLVM=1 bitmap

There was a previous attempt[2] to do this in July 2024. Please bear with me as I try to understand and address the objections from that time. I've spoken with Muhammad Usama Anjum, the author of that series, and received their approval to "take over" this work. Here we go...

Quoted 4 lines · John Hubbard

This changes the situation from "works for Linus' tab completion case", to "causes a tab completion problem"! :)

Quoted 8 lines

I think a tests/ subdir is how we eventually decided to do this [1], right?

So:

lib/tests/bitmap_kunit.c

[1] https://lore.kernel.org/[email protected]

This is true and unfortunate, but not trivial to fix because new kallsyms tests were placed in lib/tests in commit 84b4a51fce4c ("selftests: add new kallsyms selftests") *after* the KUnit filename best practices were adopted.

I propose that the KUnit maintainers blaze this trail using string_kunit.c which currently still lives in lib/ despite the KUnit docs giving it as an example at lib/tests/.

Quoted 8 lines · Shuah Khan

This change will take away the ability to run bitmap tests during boot on a non-kunit kernel.

Nack on this change. I wan to see all tests that are being removed from lib because they have been converted - also it doesn't make sense to convert some tests like this one that add the ability test during boot.

This point was also discussed in another thread[3] in which:

Quoted 8 lines · Shuah Khan

Please make sure you aren't taking away the ability to run these tests during boot.

It doesn't make sense to convert every single test especially when it is intended to be run during boot without dependencies - not as a kunit test but a regression test during boot.

Quoted 5 lines

bitmap is one example - pay attention to the config help test - bitmap one clearly states it runs regression testing during boot. Any test that says that isn't a candidate for conversion.

I am going to nack any such conversions.

The crux of the argument seems to be that the config help text is taken to describe the author's intent with the fragment "at boot". I think this may be a case of confirmation bias: I see at least the following KUnit tests with "at boot" in their help text:
- CPUMASK_KUNIT_TEST
- BITFIELD_KUNIT
- CHECKSUM_KUNIT
- UTIL_MACROS_KUNIT

It seems to me that the inference being made is that any test that runs "at boot" is intended to be run by both developers and users, but I find no evidence that bitmap in particular would ever provide additional value when run by users.

There's further discussion about KUnit not being "ideal for cases where people would want to check a subsystem on a running kernel", but I find no evidence that bitmap in particular is actually testing the running kernel; it is a unit test of the bitmap functions, which is also stated in the config help text.

David Gow made many of the same points in his final reply[4], which was never replied to.

Thanks for your attention.

---
Changes in v2:
- Rebase on v6.19-rc1, dropping the first patch.
- Extract benchmarks into new module and deduplicate test_bitmap_{read,write}_perf.
- Remove tc_err() and use KUnit assertions.
- Parameterize test_bitmap_cut and test_bitmap_parse{,list}.
- Drop KUnit boilerplate from BITMAP_KUNIT_TEST help text.
- Drop arch changes.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Tamir Duberstein (3):
test_bitmap: extract benchmark module
bitmap: convert self-test to KUnit
bitmap: break kunit into test cases

 MAINTAINERS                           |   3 +-
 lib/Kconfig.debug                     |  16 +-
 lib/Makefile                          |   5 +-
 lib/bitmap_benchmark.c                |  89 +++++
 lib/{test_bitmap.c => bitmap_kunit.c} | 630 ++++++++++++++--------------------
 tools/testing/selftests/lib/Makefile  |   2 +-
 tools/testing/selftests/lib/bitmap.sh |   3 -
 tools/testing/selftests/lib/config    |   1 -
 8 files changed, 360 insertions(+), 389 deletions(-)

---
base-commit: 8f0b4cce4481fb22653697cced8d0d04027cb1e8
change-id: 20250207-bitmap-kunit-convert-92d3147b2eee

Best regards,

Signature · 1 line
Tamir Duberstein <[email protected]>
#1 lore raw

PATCH v2 1/3 test_bitmap: extract benchmark module

From: Tamir Duberstein <[email protected]>

Add CONFIG_BITMAP_BENCHMARK to build benchmarks separately from unit tests. The new module is marked authored by the author of commit 991e5583647d ("lib/test_bitmap: add tests for bitmap_{read,write}()") which added these benchmarks.

Extract duplicated code between test_bitmap_{read,write}_perf.

Cc Alexander Potapenko <[email protected]>
---
 MAINTAINERS            |  1 +
 lib/Kconfig.debug      |  8 +++++
 lib/Makefile           |  1 +
 lib/bitmap_benchmark.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/test_bitmap.c      | 50 ----------------------------
 5 files changed, 99 insertions(+), 50 deletions(-)
MAINTAINERS +1 -0
index 5b11839cba9d..4ddaef15b408 100644
@@ -4426,6 +4426,7 @@ F:	include/uapi/linux/bits.h
 F:	include/vdso/bits.h
 F:	lib/bitmap-str.c
 F:	lib/bitmap.c
+F:	lib/bitmap_benchmark.c
 F:	lib/cpumask.c
 F:	lib/find_bit.c
 F:	lib/find_bit_benchmark.c
lib/Kconfig.debug +8 -0
index ba36939fda79..60434b31a940 100644
@@ -2492,6 +2492,14 @@ config FFS_KUNIT_TEST
 config TEST_KSTRTOX
 	tristate "Test kstrto*() family of functions at runtime"
 
+config BITMAP_BENCHMARK
+	tristate "Benchmark bitmap_*() family of functions"
+	help
+	  This builds the "bitmap_benchmark_test" module that measure bitmap_*()
+	  family of functions performance.
+
+	  If unsure, say N.
+
 config TEST_BITMAP
 	tristate "Test bitmap_*() family of functions at runtime"
 	help
lib/Makefile +1 -0
index aaf677cf4527..892cb3152b65 100644
@@ -61,6 +61,7 @@ obj-y += string_helpers.o
 obj-y += hexdump.o
 obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o
 obj-y += kstrtox.o
+obj-$(CONFIG_BITMAP_BENCHMARK) += bitmap_benchmark.o
 obj-$(CONFIG_FIND_BIT_BENCHMARK) += find_bit_benchmark.o
 obj-$(CONFIG_FIND_BIT_BENCHMARK_RUST) += find_bit_benchmark_rust.o
 obj-$(CONFIG_TEST_BPF) += test_bpf.o
lib/bitmap_benchmark.c new file +89 -0

89 lines not on this page · raw message

lib/test_bitmap.c +0 -50

62 lines not on this page · raw message

Signature · 1 line
2.52.0
#2 lore raw

PATCH v2 2/3 bitmap: convert self-test to KUnit

In reply to Tamir Duberstein

From: Tamir Duberstein <[email protected]>

Convert the bitmap() self-test to a KUnit test.

In the interest of keeping the patch reasonably-sized this doesn't refactor the tests into proper parameterized tests - it's all one big test case.

---
 MAINTAINERS                           |   2 +-
 lib/Kconfig.debug                     |  16 +-
 lib/Makefile                          |   4 +-
 lib/{test_bitmap.c => bitmap_kunit.c} | 314 +++++++++++++---------------------
 tools/testing/selftests/lib/Makefile  |   2 +-
 tools/testing/selftests/lib/bitmap.sh |   3 -
 tools/testing/selftests/lib/config    |   1 -
 7 files changed, 135 insertions(+), 207 deletions(-)
MAINTAINERS +1 -1
index 4ddaef15b408..bb5269ff2bcd 100644
@@ -4430,7 +4430,7 @@ F:	lib/bitmap_benchmark.c
 F:	lib/cpumask.c
 F:	lib/find_bit.c
 F:	lib/find_bit_benchmark.c
-F:	lib/test_bitmap.c
+F:	lib/tests/bitmap_kunit.c
 F:	lib/tests/cpumask_kunit.c
 F:	tools/include/linux/bitfield.h
 F:	tools/include/linux/bitmap.h
lib/Kconfig.debug +9 -7
index 60434b31a940..e7a4991e5395 100644
@@ -2438,6 +2438,15 @@ config ASYNC_RAID6_TEST
 config TEST_HEXDUMP
 	tristate "Test functions located in the hexdump module at runtime"
 
+config BITMAP_KUNIT_TEST
+	tristate "KUnit test bitmap_*() family of functions at runtime" if !KUNIT_ALL_TESTS
+	depends on KUNIT
+	default KUNIT_ALL_TESTS
+	help
+	  Enable this option to test the bitmap functions at runtime.
+
+	  If unsure, say N.
+
 config PRINTF_KUNIT_TEST
 	tristate "KUnit test printf() family of functions at runtime" if !KUNIT_ALL_TESTS
 	depends on KUNIT
@@ -2500,13 +2509,6 @@ config BITMAP_BENCHMARK
 
 	  If unsure, say N.
 
-config TEST_BITMAP
-	tristate "Test bitmap_*() family of functions at runtime"
-	help
-	  Enable this option to test the bitmap functions at boot.
-
-	  If unsure, say N.
-
 config TEST_UUID
 	tristate "Test functions located in the uuid module at runtime"
 
lib/Makefile +2 -2
index 892cb3152b65..683084afb793 100644
@@ -84,10 +84,10 @@ obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_keys.o
 obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_key_base.o
 obj-$(CONFIG_TEST_DYNAMIC_DEBUG) += test_dynamic_debug.o
 
-obj-$(CONFIG_TEST_BITMAP) += test_bitmap.o
+obj-$(CONFIG_BITMAP_KUNIT_TEST) += bitmap_kunit.o
 ifeq ($(CONFIG_CC_IS_CLANG)$(CONFIG_KASAN),yy)
 # FIXME: Clang breaks test_bitmap_const_eval when KASAN and GCOV are enabled
-GCOV_PROFILE_test_bitmap.o := n
+GCOV_PROFILE_bitmap_kunit.o := n
 endif
 
 obj-$(CONFIG_TEST_UUID) += test_uuid.o
lib/bitmap_kunit.c renamed from lib/test_bitmap.c +122 -192

676 lines not on this page · raw message

tools/testing/selftests/lib/Makefile +1 -1
index f876bf4744e1..e4617f153542 100644
@@ -4,5 +4,5 @@
 # No binaries, but make sure arg-less "make" doesn't trigger "run_tests"
 all:
 
-TEST_PROGS := bitmap.sh
+TEST_PROGS :=
 include ../lib.mk
tools/testing/selftests/lib/bitmap.sh deleted +0 -3
deleted file mode 100755
index 00a416fbc0ef..000000000000
@@ -1,3 +0,0 @@
-#!/bin/sh
-# SPDX-License-Identifier: GPL-2.0
-$(dirname $0)/../kselftest/module.sh "bitmap" test_bitmap
tools/testing/selftests/lib/config +0 -1
index 377b3699ff31..3792212895e5 100644
@@ -1,3 +1,2 @@
-CONFIG_TEST_BITMAP=m
 CONFIG_PRIME_NUMBERS=m
 CONFIG_TEST_BITOPS=m
Signature · 1 line
2.52.0
#3 lore raw

PATCH v2 3/3 bitmap: break kunit into test cases

In reply to Tamir Duberstein

From: Tamir Duberstein <[email protected]>

Move individual tests into bitmap_test_cases.

Retire the tc_err macro. It was using during migration to reduce churn, but now obscures the opporunity for richer assertion.

Parameterize tests where possible to ensure KUnit assertions produce descriptive messages.

Reduce duplication between test_bitmap_parse{,list}. Note that test_bitmap_parse_fn is defined out-of-line to reduce churn.

---
 lib/bitmap_kunit.c | 388 ++++++++++++++++++++++++++---------------------------
 1 file changed, 191 insertions(+), 197 deletions(-)
lib/bitmap_kunit.c +191 -197
index 3a71e2039c1f..88770a1804cb 100644
@@ -10,6 +10,7 @@
 #include <linux/printk.h>
 #include <linux/slab.h>
 #include <linux/string.h>
+#include <linux/string_helpers.h>
 #include <linux/uaccess.h>
 
 #define EXP1_IN_BITS	(sizeof(exp1) * 8)
@@ -17,11 +18,6 @@
 static char pbl_buffer[PAGE_SIZE];
 static char print_buf[PAGE_SIZE * 2];
 
-static struct kunit *kunittest;
-
-#define tc_err(fmt, ...) \
-	KUNIT_FAIL(kunittest, fmt, ##__VA_ARGS__)
-
 static const unsigned long exp1[] = {
 	BITMAP_FROM_U64(1),
 	BITMAP_FROM_U64(2),
@@ -87,16 +83,9 @@ static const unsigned long exp3_1_0[] = {
 		}									\
 	} while (0)
 
-#define expect_eq_str(exp_str, str, len)				\
-	{								\
-		if (strncmp(exp_str, str, len) != 0) {			\
-			tc_err("expected %s, got %s", exp_str, str);	\
-		}							\
-	}
-
 #define expect_eq_uint(x, y)		expect_eq_ulong((unsigned int)(x), (unsigned int)(y))
 
-static void test_zero_clear(void)
+static void test_zero_clear(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(bmap, 1024);
 
@@ -125,7 +114,7 @@ static void test_zero_clear(void)
 	expect_eq_pbl("", bmap, 1024);
 }
 
-static void test_find_nth_bit(void)
+static void test_find_nth_bit(struct kunit *kunittest)
 {
 	unsigned long b, bit, cnt = 0;
 	DECLARE_BITMAP(bmap, 64 * 3);
@@ -166,7 +155,7 @@ static void test_find_nth_bit(void)
 	}
 }
 
-static void test_fill_set(void)
+static void test_fill_set(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(bmap, 1024);
 
@@ -195,7 +184,7 @@ static void test_fill_set(void)
 	expect_eq_pbl("0-1023", bmap, 1024);
 }
 
-static void test_copy(void)
+static void test_copy(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(bmap1, 1024);
 	DECLARE_BITMAP(bmap2, 1024);
@@ -234,7 +223,7 @@ static void test_copy(void)
 	expect_eq_pbl("0-108,128-1023", bmap2, 1024);
 }
 
-static void test_bitmap_region(void)
+static void test_bitmap_region(struct kunit *kunittest)
 {
 	int pos, order;
 
@@ -259,7 +248,7 @@ static void test_bitmap_region(void)
 
 #define EXP2_IN_BITS	(sizeof(exp2) * 8)
 
-static void test_replace(void)
+static void test_replace(struct kunit *kunittest)
 {
 	unsigned int nbits = 64;
 	unsigned int nlongs = DIV_ROUND_UP(nbits, BITS_PER_LONG);
@@ -300,7 +289,7 @@ static const unsigned long sg_scatter_exp[] = {
 	BITMAP_FROM_U64(0x000000000000021aULL),
 };
 
-static void test_bitmap_sg(void)
+static void test_bitmap_sg(struct kunit *kunittest)
 {
 	unsigned int nbits = 64;
 	DECLARE_BITMAP(bmap_gather, 100);
@@ -336,6 +325,19 @@ struct test_bitmap_parselist{
 	const int flags;
 };
 
+static void parselist_to_desc(const struct test_bitmap_parselist *param, char *desc)
+{
+	int n = 0;
+
+	n += snprintf(desc + n, KUNIT_PARAM_DESC_SIZE - n, "\"");
+	n += string_escape_str(param->in, desc + n, KUNIT_PARAM_DESC_SIZE - n, ESCAPE_SPACE, NULL);
+	n += snprintf(desc + n, KUNIT_PARAM_DESC_SIZE - n, "\"");
+}
+
+typedef int (*bitmap_parse_fn)(const struct test_bitmap_parselist *test, unsigned long *maskp);
+
+static void test_bitmap_parse_fn(struct kunit *kunittest, bitmap_parse_fn fn);
+
 static const struct test_bitmap_parselist parselist_tests[] = {
 #define step (sizeof(u64) / sizeof(unsigned long))
 
@@ -421,76 +423,60 @@ static const struct test_bitmap_parselist parselist_tests[] = {
 
 };
 
-static void test_bitmap_parselist(void)
+KUNIT_ARRAY_PARAM(test_parselist, parselist_tests, parselist_to_desc);
+
+static int do_bitmap_parselist(const struct test_bitmap_parselist *test, unsigned long *maskp)
 {
-	int i;
+	return bitmap_parselist(test->in, maskp, test->nbits);
+}
+
+static void test_bitmap_parselist(struct kunit *kunittest)
+{
+	test_bitmap_parse_fn(kunittest, do_bitmap_parselist);
+}
+
+static void test_bitmap_parse_fn(struct kunit *kunittest, bitmap_parse_fn fn)
+{
+	const struct test_bitmap_parselist *t = kunittest->param_value;
 	int err;
 	ktime_t time;
 	DECLARE_BITMAP(bmap, 2048);
 
-	for (i = 0; i < ARRAY_SIZE(parselist_tests); i++) {
-#define ptest parselist_tests[i]
-
-		time = ktime_get();
-		err = bitmap_parselist(ptest.in, bmap, ptest.nbits);
-		time = ktime_get() - time;
-
-		if (err != ptest.errno) {
-			tc_err("parselist: %d: input is %s, errno is %d, expected %d",
-					i, ptest.in, err, ptest.errno);
-			continue;
-		}
-
-		if (!err && ptest.expected
-			 && !__bitmap_equal(bmap, ptest.expected, ptest.nbits)) {
-			tc_err("parselist: %d: input is %s, result is 0x%lx, expected 0x%lx",
-					i, ptest.in, bmap[0],
-					*ptest.expected);
-			continue;
-		}
+	time = ktime_get();
+	err = fn(t, bmap);
+	time = ktime_get() - time;
 
-		if (ptest.flags & PARSE_TIME)
-			kunit_info(kunittest, "parselist: %d: input is '%s' OK, Time: %llu",
-					i, ptest.in, time);
+	KUNIT_ASSERT_EQ(kunittest, err, t->errno);
 
-#undef ptest
+	if (!err && t->expected && !__bitmap_equal(bmap, t->expected, t->nbits)) {
+		KUNIT_FAIL_AND_ABORT(kunittest,
+				     "result is 0x%lx, expected 0x%lx", bmap[0], *t->expected);
 	}
+
+	if (t->flags & PARSE_TIME)
+		kunit_info(kunittest, "Time: %llu", time);
 }
 
-static void test_bitmap_printlist(void)
+static void test_bitmap_printlist(struct kunit *kunittest)
 {
-	unsigned long *bmap = kmalloc(PAGE_SIZE, GFP_KERNEL);
-	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
+	static long bmap[PAGE_SIZE];
+	static char buf[PAGE_SIZE];
 	char expected[256];
 	int ret, slen;
 	ktime_t time;
 
-	if (!buf || !bmap)
-		goto out;
-
 	memset(bmap, -1, PAGE_SIZE);
-	slen = snprintf(expected, 256, "0-%ld", PAGE_SIZE * 8 - 1);
-	if (slen < 0)
-		goto out;
+	slen = snprintf(expected, sizeof(expected), "0-%ld\n", PAGE_SIZE * 8 - 1);
+	KUNIT_ASSERT_GT(kunittest, slen, 0);
 
 	time = ktime_get();
 	ret = bitmap_print_to_pagebuf(true, buf, bmap, PAGE_SIZE * 8);
 	time = ktime_get() - time;
 
-	if (ret != slen + 1) {
-		tc_err("bitmap_print_to_pagebuf: result is %d, expected %d", ret, slen);
-		goto out;
-	}
-
-	if (strncmp(buf, expected, slen)) {
-		tc_err("bitmap_print_to_pagebuf: result is %s, expected %s", buf, expected);
-		goto out;
-	}
+	KUNIT_ASSERT_EQ(kunittest, ret, slen);
+	KUNIT_ASSERT_STREQ(kunittest, buf, expected);
 
-	kunit_info(kunittest, "bitmap_print_to_pagebuf: input is '%s', Time: %llu", buf, time);
-out:
-	kfree(buf);
-	kfree(bmap);
+	kunit_info(kunittest, "Time: %llu", time);
 }
 
 static const unsigned long parse_test[] = {
@@ -533,42 +519,21 @@ static const struct test_bitmap_parselist parse_tests[] = {
 #undef step
 };
 
-static void test_bitmap_parse(void)
-{
-	int i;
-	int err;
-	ktime_t time;
-	DECLARE_BITMAP(bmap, 2048);
-
-	for (i = 0; i < ARRAY_SIZE(parse_tests); i++) {
-		struct test_bitmap_parselist test = parse_tests[i];
-		size_t len = test.flags & NO_LEN ? UINT_MAX : strlen(test.in);
-
-		time = ktime_get();
-		err = bitmap_parse(test.in, len, bmap, test.nbits);
-		time = ktime_get() - time;
+KUNIT_ARRAY_PARAM(test_parse, parse_tests, parselist_to_desc);
 
-		if (err != test.errno) {
-			tc_err("parse: %d: input is %s, errno is %d, expected %d",
-					i, test.in, err, test.errno);
-			continue;
-		}
+static int do_bitmap_parse(const struct test_bitmap_parselist *test, unsigned long *maskp)
+{
+	const size_t len = test->flags & NO_LEN ? UINT_MAX : strlen(test->in);
 
-		if (!err && test.expected
-			 && !__bitmap_equal(bmap, test.expected, test.nbits)) {
-			tc_err("parse: %d: input is %s, result is 0x%lx, expected 0x%lx",
-					i, test.in, bmap[0],
-					*test.expected);
-			continue;
-		}
+	return bitmap_parse(test->in, len, maskp, test->nbits);
+}
 
-		if (test.flags & PARSE_TIME)
-			kunit_info(kunittest, "parse: %d: input is '%s' OK, Time: %llu",
-					i, test.in, time);
-	}
+static void test_bitmap_parse(struct kunit *kunittest)
+{
+	test_bitmap_parse_fn(kunittest, do_bitmap_parse);
 }
 
-static void test_bitmap_arr32(void)
+static void test_bitmap_arr32(struct kunit *kunittest)
 {
 	unsigned int nbits, next_bit;
 	u32 arr[EXP1_IN_BITS / 32];
@@ -584,8 +549,9 @@ static void test_bitmap_arr32(void)
 		next_bit = find_next_bit(bmap2,
 				round_up(nbits, BITS_PER_LONG), nbits);
 		if (next_bit < round_up(nbits, BITS_PER_LONG)) {
-			tc_err("bitmap_copy_arr32(nbits == %d: tail is not safely cleared: %d",
-				nbits, next_bit);
+			KUNIT_FAIL(kunittest,
+				   "bitmap_copy_arr32(nbits == %d: tail is not safely cleared: %d",
+				   nbits, next_bit);
 		}
 
 		if (nbits < EXP1_IN_BITS - 32)
@@ -594,7 +560,7 @@ static void test_bitmap_arr32(void)
 	}
 }
 
-static void test_bitmap_arr64(void)
+static void test_bitmap_arr64(struct kunit *kunittest)
 {
 	unsigned int nbits, next_bit;
 	u64 arr[EXP1_IN_BITS / 64];
@@ -610,15 +576,16 @@ static void test_bitmap_arr64(void)
 
 		next_bit = find_next_bit(bmap2, round_up(nbits, BITS_PER_LONG), nbits);
 		if (next_bit < round_up(nbits, BITS_PER_LONG)) {
-			tc_err("bitmap_copy_arr64(nbits == %d: tail is not safely cleared: %d",
-				nbits, next_bit);
+			KUNIT_FAIL(kunittest,
+				   "bitmap_copy_arr64(nbits == %d: tail is not safely cleared: %d",
+				   nbits, next_bit);
 		}
 
 		if ((nbits % 64) &&
 		    (arr[(nbits - 1) / 64] & ~GENMASK_ULL((nbits - 1) % 64, 0))) {
-			tc_err("bitmap_to_arr64(nbits == %d): tail is not safely cleared: 0x%016llx (must be 0x%016llx)",
-			       nbits, arr[(nbits - 1) / 64],
-			       GENMASK_ULL((nbits - 1) % 64, 0));
+			KUNIT_FAIL(kunittest,
+				   "bitmap_to_arr64(nbits == %d): tail is not safely cleared: 0x%016llx (must be 0x%016llx)",
+				   nbits, arr[(nbits - 1) / 64], GENMASK_ULL((nbits - 1) % 64, 0));
 		}
 
 		if (nbits < EXP1_IN_BITS - 64)
@@ -626,7 +593,7 @@ static void test_bitmap_arr64(void)
 	}
 }
 
-static noinline void test_mem_optimisations(void)
+static noinline void test_mem_optimisations(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(bmap1, 1024);
 	DECLARE_BITMAP(bmap2, 1024);
@@ -640,19 +607,19 @@ static noinline void test_mem_optimisations(void)
 			bitmap_set(bmap1, start, nbits);
 			__bitmap_set(bmap2, start, nbits);
 			if (!bitmap_equal(bmap1, bmap2, 1024)) {
-				tc_err("set not equal %d %d", start, nbits);
+				KUNIT_FAIL(kunittest, "set not equal %d %d", start, nbits);
 			}
 			if (!__bitmap_equal(bmap1, bmap2, 1024)) {
-				tc_err("set not __equal %d %d", start, nbits);
+				KUNIT_FAIL(kunittest, "set not __equal %d %d", start, nbits);
 			}
 
 			bitmap_clear(bmap1, start, nbits);
 			__bitmap_clear(bmap2, start, nbits);
 			if (!bitmap_equal(bmap1, bmap2, 1024)) {
-				tc_err("clear not equal %d %d", start, nbits);
+				KUNIT_FAIL(kunittest, "clear not equal %d %d", start, nbits);
 			}
 			if (!__bitmap_equal(bmap1, bmap2, 1024)) {
-				tc_err("clear not __equal %d %d", start, nbits);
+				KUNIT_FAIL(kunittest, "clear not __equal %d %d", start, nbits);
 			}
 		}
 	}
@@ -669,7 +636,7 @@ static const unsigned char clump_exp[] = {
 	0x05,	/* non-adjacent 2 bits set */
 };
 
-static void test_for_each_set_clump8(void)
+static void test_for_each_set_clump8(struct kunit *kunittest)
 {
 #define CLUMP_EXP_NUMBITS 64
 	DECLARE_BITMAP(bits, CLUMP_EXP_NUMBITS);
@@ -691,7 +658,7 @@ static void test_for_each_set_clump8(void)
 		expect_eq_clump8(start, CLUMP_EXP_NUMBITS, clump_exp, &clump);
 }
 
-static void test_for_each_set_bit_wrap(void)
+static void test_for_each_set_bit_wrap(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(orig, 500);
 	DECLARE_BITMAP(copy, 500);
@@ -716,7 +683,7 @@ static void test_for_each_set_bit_wrap(void)
 	}
 }
 
-static void test_for_each_set_bit(void)
+static void test_for_each_set_bit(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(orig, 500);
 	DECLARE_BITMAP(copy, 500);
@@ -738,7 +705,7 @@ static void test_for_each_set_bit(void)
 	expect_eq_bitmap(orig, copy, 500);
 }
 
-static void test_for_each_set_bit_from(void)
+static void test_for_each_set_bit_from(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(orig, 500);
 	DECLARE_BITMAP(copy, 500);
@@ -768,7 +735,7 @@ static void test_for_each_set_bit_from(void)
 	}
 }
 
-static void test_for_each_clear_bit(void)
+static void test_for_each_clear_bit(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(orig, 500);
 	DECLARE_BITMAP(copy, 500);
@@ -790,7 +757,7 @@ static void test_for_each_clear_bit(void)
 	expect_eq_bitmap(orig, copy, 500);
 }
 
-static void test_for_each_clear_bit_from(void)
+static void test_for_each_clear_bit_from(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(orig, 500);
 	DECLARE_BITMAP(copy, 500);
@@ -820,7 +787,7 @@ static void test_for_each_clear_bit_from(void)
 	}
 }
 
-static void test_for_each_set_bitrange(void)
+static void test_for_each_set_bitrange(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(orig, 500);
 	DECLARE_BITMAP(copy, 500);
@@ -842,7 +809,7 @@ static void test_for_each_set_bitrange(void)
 	expect_eq_bitmap(orig, copy, 500);
 }
 
-static void test_for_each_clear_bitrange(void)
+static void test_for_each_clear_bitrange(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(orig, 500);
 	DECLARE_BITMAP(copy, 500);
@@ -864,7 +831,7 @@ static void test_for_each_clear_bitrange(void)
 	expect_eq_bitmap(orig, copy, 500);
 }
 
-static void test_for_each_set_bitrange_from(void)
+static void test_for_each_set_bitrange_from(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(orig, 500);
 	DECLARE_BITMAP(copy, 500);
@@ -894,7 +861,7 @@ static void test_for_each_set_bitrange_from(void)
 	}
 }
 
-static void test_for_each_clear_bitrange_from(void)
+static void test_for_each_clear_bitrange_from(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(orig, 500);
 	DECLARE_BITMAP(copy, 500);
@@ -965,20 +932,50 @@ static struct test_bitmap_cut test_cut[] = {
 	},
 };
 
-static void test_bitmap_cut(void)
+static void cut_to_desc(struct test_bitmap_cut *param, char * const desc)
 {
-	unsigned long b[5], *in = &b[1], *out = &b[0];	/* Partial overlap */
+	int n = 0;
 	int i;
 
-	for (i = 0; i < ARRAY_SIZE(test_cut); i++) {
-		struct test_bitmap_cut *t = &test_cut[i];
+	#define append(...) (n += snprintf(desc + n, KUNIT_PARAM_DESC_SIZE - n, ##__VA_ARGS__))
+
+	append("first=%d,cut=%d,nbits=%d", param->first, param->cut, param->nbits);
+
+	#define append_array(name) do {					\
+		{							\
+			append("," #name "={");				\
+			for (i = 0; i < ARRAY_SIZE(param->name); i++) {	\
+				if (param->name[i] == 0) {		\
+					break;				\
+				}					\
+				if (i != 0) {				\
+					append(",");			\
+				}					\
+				append("0x%08lxUL", param->name[i]);	\
+			}						\
+			append("}");					\
+		}							\
+	} while (0)
 
-		memcpy(in, t->in, sizeof(t->in));
+	append_array(in);
+	append_array(expected);
 
-		bitmap_cut(out, in, t->first, t->cut, t->nbits);
+	#undef append_array
+	#undef append
+}
 
-		expect_eq_bitmap(t->expected, out, t->nbits);
-	}
+KUNIT_ARRAY_PARAM(test_cut, test_cut, cut_to_desc);
+
+static void test_bitmap_cut(struct kunit *kunittest)
+{
+	const struct test_bitmap_cut *t = kunittest->param_value;
+	unsigned long b[5], *in = &b[1], *out = &b[0];	/* Partial overlap */
+
+	memcpy(in, t->in, sizeof(t->in));
+
+	bitmap_cut(out, in, t->first, t->cut, t->nbits);
+
+	expect_eq_bitmap(t->expected, out, t->nbits);
 }
 
 struct test_bitmap_print {
@@ -986,6 +983,7 @@ struct test_bitmap_print {
 	unsigned long nbits;
 	const char *mask;
 	const char *list;
+	const char *name;
 };
 
 static const unsigned long small_bitmap[] = {
@@ -1082,35 +1080,33 @@ static const char large_list[] = /* more than 4KB */
 	"49,2552-2553,2556-2557\n";
 
 static const struct test_bitmap_print test_print[] = {
-	{ small_bitmap, sizeof(small_bitmap) * BITS_PER_BYTE, small_mask, small_list },
-	{ large_bitmap, sizeof(large_bitmap) * BITS_PER_BYTE, large_mask, large_list },
+	{ small_bitmap, sizeof(small_bitmap) * BITS_PER_BYTE, small_mask, small_list, "small" },
+	{ large_bitmap, sizeof(large_bitmap) * BITS_PER_BYTE, large_mask, large_list, "large" },
 };
 
-static void test_bitmap_print_buf(void)
+KUNIT_ARRAY_PARAM_DESC(test_print, test_print, name);
+
+static void test_bitmap_print_buf(struct kunit *kunittest)
 {
-	int i;
+	const struct test_bitmap_print *t = kunittest->param_value;
+	int n;
 
-	for (i = 0; i < ARRAY_SIZE(test_print); i++) {
-		const struct test_bitmap_print *t = &test_print[i];
-		int n;
+	n = bitmap_print_bitmask_to_buf(print_buf, t->bitmap, t->nbits,
+					0, 2 * PAGE_SIZE);
+	expect_eq_uint(strlen(t->mask) + 1, n);
+	KUNIT_EXPECT_STREQ(kunittest, t->mask, print_buf);
 
-		n = bitmap_print_bitmask_to_buf(print_buf, t->bitmap, t->nbits,
-						0, 2 * PAGE_SIZE);
-		expect_eq_uint(strlen(t->mask) + 1, n);
-		expect_eq_str(t->mask, print_buf, n);
+	n = bitmap_print_list_to_buf(print_buf, t->bitmap, t->nbits,
+					0, 2 * PAGE_SIZE);
+	expect_eq_uint(strlen(t->list) + 1, n);
+	KUNIT_EXPECT_STREQ(kunittest, t->list, print_buf);
 
+	/* test by non-zero offset */
+	if (strlen(t->list) > PAGE_SIZE) {
 		n = bitmap_print_list_to_buf(print_buf, t->bitmap, t->nbits,
-					     0, 2 * PAGE_SIZE);
-		expect_eq_uint(strlen(t->list) + 1, n);
-		expect_eq_str(t->list, print_buf, n);
-
-		/* test by non-zero offset */
-		if (strlen(t->list) > PAGE_SIZE) {
-			n = bitmap_print_list_to_buf(print_buf, t->bitmap, t->nbits,
-						     PAGE_SIZE, PAGE_SIZE);
-			expect_eq_uint(strlen(t->list) + 1 - PAGE_SIZE, n);
-			expect_eq_str(t->list + PAGE_SIZE, print_buf, n);
-		}
+						PAGE_SIZE, PAGE_SIZE);
+		expect_eq_uint(strlen(t->list) + 1 - PAGE_SIZE, n);
+		KUNIT_EXPECT_STREQ(kunittest, t->list + PAGE_SIZE, print_buf);
 	}
 }
 
@@ -1118,7 +1114,7 @@ static void test_bitmap_print_buf(void)
  * FIXME: Clang breaks compile-time evaluations when KASAN and GCOV are enabled.
  * To workaround it, GCOV is force-disabled in Makefile for this configuration.
  */
-static void test_bitmap_const_eval(void)
+static void test_bitmap_const_eval(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(bitmap, BITS_PER_LONG);
 	unsigned long initvar = BIT(2);
@@ -1183,11 +1179,19 @@ static void test_bitmap_const_eval(void)
  */
 #define TEST_BIT_LEN (1000)
 
-/*
- * Helper function to test bitmap_write() overwriting the chosen byte pattern.
- */
-static void test_bitmap_write_helper(const char *pattern)
+static const char * const pattern[] = {"", "all:1/2", "all"};
+
+static void pattern_to_desc(const char * const *param, char *desc)
 {
+	snprintf(desc, KUNIT_PARAM_DESC_SIZE, "\"%s\"", *param);
+}
+
+KUNIT_ARRAY_PARAM(pattern, pattern, pattern_to_desc);
+
+static void test_bitmap_write_pattern(struct kunit *kunittest)
+{
+	const char * const *p = kunittest->param_value;
+	const char *pattern = *p;
 	DECLARE_BITMAP(bitmap, TEST_BIT_LEN);
 	DECLARE_BITMAP(exp_bitmap, TEST_BIT_LEN);
 	DECLARE_BITMAP(pat_bitmap, TEST_BIT_LEN);
@@ -1240,13 +1244,12 @@ static void test_bitmap_write_helper(const char *pattern)
 	}
 }
 
-static void test_bitmap_read_write(void)
+static void test_bitmap_read_write(struct kunit *kunittest)
 {
-	unsigned char *pattern[3] = {"", "all:1/2", "all"};
 	DECLARE_BITMAP(bitmap, TEST_BIT_LEN);
 	unsigned long zero_bits = 0, bits_per_long = BITS_PER_LONG;
 	unsigned long val;
-	int i, pi;
+	int i;
 
 	/*
 	 * Reading/writing zero bits should not crash the kernel.
@@ -1284,49 +1287,40 @@ static void test_bitmap_read_write(void)
 		val = bitmap_read(bitmap, i, 8);
 		expect_eq_ulong(0b10110101UL, val);
 	}
-
-	for (pi = 0; pi < ARRAY_SIZE(pattern); pi++)
-		test_bitmap_write_helper(pattern[pi]);
 }
 
 #undef TEST_BIT_LEN
 
-static void bitmap_test(struct kunit *test)
-{
-	kunittest = test;
-
-	test_zero_clear();
-	test_fill_set();
-	test_copy();
-	test_bitmap_region();
-	test_replace();
-	test_bitmap_sg();
-	test_bitmap_arr32();
-	test_bitmap_arr64();
-	test_bitmap_parse();
-	test_bitmap_parselist();
-	test_bitmap_printlist();
-	test_mem_optimisations();
-	test_bitmap_cut();
-	test_bitmap_print_buf();
-	test_bitmap_const_eval();
-	test_bitmap_read_write();
-
-	test_find_nth_bit();
-	test_for_each_set_bit();
-	test_for_each_set_bit_from();
-	test_for_each_clear_bit();
-	test_for_each_clear_bit_from();
-	test_for_each_set_bitrange();
-	test_for_each_clear_bitrange();
-	test_for_each_set_bitrange_from();
-	test_for_each_clear_bitrange_from();
-	test_for_each_set_clump8();
-	test_for_each_set_bit_wrap();
-}
-
 static struct kunit_case bitmap_test_cases[] = {
-	KUNIT_CASE(bitmap_test),
+	KUNIT_CASE(test_zero_clear),
+	KUNIT_CASE(test_fill_set),
+	KUNIT_CASE(test_copy),
+	KUNIT_CASE(test_bitmap_region),
+	KUNIT_CASE(test_replace),
+	KUNIT_CASE(test_bitmap_sg),
+	KUNIT_CASE(test_bitmap_arr32),
+	KUNIT_CASE(test_bitmap_arr64),
+	KUNIT_CASE_PARAM(test_bitmap_parse, test_parse_gen_params),
+	KUNIT_CASE_PARAM(test_bitmap_parselist, test_parselist_gen_params),
+	KUNIT_CASE(test_bitmap_printlist),
+	KUNIT_CASE(test_mem_optimisations),
+	KUNIT_CASE_PARAM(test_bitmap_cut, test_cut_gen_params),
+	KUNIT_CASE_PARAM(test_bitmap_print_buf, test_print_gen_params),
+	KUNIT_CASE(test_bitmap_const_eval),
+	KUNIT_CASE(test_bitmap_read_write),
+	KUNIT_CASE_PARAM(test_bitmap_write_pattern, pattern_gen_params),
+
+	KUNIT_CASE(test_find_nth_bit),
+	KUNIT_CASE(test_for_each_set_bit),
+	KUNIT_CASE(test_for_each_set_bit_from),
+	KUNIT_CASE(test_for_each_clear_bit),
+	KUNIT_CASE(test_for_each_clear_bit_from),
+	KUNIT_CASE(test_for_each_set_bitrange),
+	KUNIT_CASE(test_for_each_clear_bitrange),
+	KUNIT_CASE(test_for_each_set_bitrange_from),
+	KUNIT_CASE(test_for_each_clear_bitrange_from),
+	KUNIT_CASE(test_for_each_set_clump8),
+	KUNIT_CASE(test_for_each_set_bit_wrap),
 	{}
 };
 
Signature · 1 line
2.52.0
#4 lore raw

Re: [PATCH v2 3/3] bitmap: break kunit into test cases

kernel test robot noticed the following build warnings:

Hi Tamir,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 8f0b4cce4481fb22653697cced8d0d04027cb1e8]

url:    https://github.com/intel-lab-lkp/linux/commits/Tamir-Duberstein/test_bitmap-extract-benchmark-module/20251222-214306
base:   8f0b4cce4481fb22653697cced8d0d04027cb1e8
patch link:    https://lore.kernel.org/r/20251222-bitmap-kunit-convert-v2-3-6a61a5330eff%40gmail.com
patch subject: [PATCH v2 3/3] bitmap: break kunit into test cases
config: sparc-randconfig-001-20251223 (https://download.01.org/0day-ci/archive/20251223/[email protected]/config)
compiler: sparc64-linux-gcc (GCC) 15.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251223/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

In file included from include/linux/string.h:65,
from include/linux/bitmap.h:13, from include/linux/cpumask.h:11, from include/linux/smp.h:13, from include/linux/lockdep.h:14, from include/linux/spinlock.h:63, from include/linux/kref.h:16, from include/kunit/test.h:22, from lib/bitmap_kunit.c:6:
lib/bitmap_kunit.c: In function 'test_bitmap_printlist':

>> arch/sparc/include/asm/string.h:18:29: warning: 'memset' used with length equal to number of elements without multiplication by element size [-Wmemset-elt-size]
      18 | #define memset(s, c, count) __builtin_memset(s, c, count)
         |                             ^~~~~~~~~~~~~~~~
   lib/bitmap_kunit.c:468:9: note: in expansion of macro 'memset'
     468 |         memset(bmap, -1, PAGE_SIZE);
         |         ^~~~~~

vim +/memset +18 arch/sparc/include/asm/string.h

70a6fcf3283a0a Al Viro 2016-01-17 16
70a6fcf3283a0a Al Viro 2016-01-17 17 #define __HAVE_ARCH_MEMSET
70a6fcf3283a0a Al Viro 2016-01-17 @18 #define memset(s, c, count) __builtin_memset(s, c, count) 70a6fcf3283a0a Al Viro 2016-01-17 19

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#5 lore raw

Re: [PATCH v2 3/3] bitmap: break kunit into test cases

In reply to Tamir Duberstein
kernel test robot noticed the following build warnings:

Hi Tamir,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 8f0b4cce4481fb22653697cced8d0d04027cb1e8]

url:    https://github.com/intel-lab-lkp/linux/commits/Tamir-Duberstein/test_bitmap-extract-benchmark-module/20251222-214306
base:   8f0b4cce4481fb22653697cced8d0d04027cb1e8
patch link:    https://lore.kernel.org/r/20251222-bitmap-kunit-convert-v2-3-6a61a5330eff%40gmail.com
patch subject: [PATCH v2 3/3] bitmap: break kunit into test cases
config: arm64-randconfig-004-20251223 (https://download.01.org/0day-ci/archive/20251223/[email protected]/config)
compiler: aarch64-linux-gcc (GCC) 9.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251223/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

lib/bitmap_kunit.c: In function 'test_bitmap_printlist':

>> lib/bitmap_kunit.c:468:2: warning: 'memset' used with length equal to number of elements without multiplication by element size [-Wmemset-elt-size]
     468 |  memset(bmap, -1, PAGE_SIZE);
         |  ^~~~~~

vim +/memset +468 lib/bitmap_kunit.c

6ea86bdfc169ba lib/test_bitmap.c  Yury Norov       2019-05-14  459  
8ef3340af7139f lib/bitmap_kunit.c Tamir Duberstein 2025-12-22  460  static void test_bitmap_printlist(struct kunit *kunittest)
db7313005e9c2d lib/test_bitmap.c  Yury Norov       2021-08-14  461  {
8ef3340af7139f lib/bitmap_kunit.c Tamir Duberstein 2025-12-22  462  	static long bmap[PAGE_SIZE];
8ef3340af7139f lib/bitmap_kunit.c Tamir Duberstein 2025-12-22  463  	static char buf[PAGE_SIZE];
db7313005e9c2d lib/test_bitmap.c  Yury Norov       2021-08-14  464  	char expected[256];
db7313005e9c2d lib/test_bitmap.c  Yury Norov       2021-08-14  465  	int ret, slen;
db7313005e9c2d lib/test_bitmap.c  Yury Norov       2021-08-14  466  	ktime_t time;
db7313005e9c2d lib/test_bitmap.c  Yury Norov       2021-08-14  467  
db7313005e9c2d lib/test_bitmap.c  Yury Norov       2021-08-14 @468  	memset(bmap, -1, PAGE_SIZE);
8ef3340af7139f lib/bitmap_kunit.c Tamir Duberstein 2025-12-22  469  	slen = snprintf(expected, sizeof(expected), "0-%ld\n", PAGE_SIZE * 8 - 1);
8ef3340af7139f lib/bitmap_kunit.c Tamir Duberstein 2025-12-22  470  	KUNIT_ASSERT_GT(kunittest, slen, 0);
db7313005e9c2d lib/test_bitmap.c  Yury Norov       2021-08-14  471  
db7313005e9c2d lib/test_bitmap.c  Yury Norov       2021-08-14  472  	time = ktime_get();
db7313005e9c2d lib/test_bitmap.c  Yury Norov       2021-08-14  473  	ret = bitmap_print_to_pagebuf(true, buf, bmap, PAGE_SIZE * 8);
db7313005e9c2d lib/test_bitmap.c  Yury Norov       2021-08-14  474  	time = ktime_get() - time;
db7313005e9c2d lib/test_bitmap.c  Yury Norov       2021-08-14  475  
8ef3340af7139f lib/bitmap_kunit.c Tamir Duberstein 2025-12-22  476  	KUNIT_ASSERT_EQ(kunittest, ret, slen);
8ef3340af7139f lib/bitmap_kunit.c Tamir Duberstein 2025-12-22  477  	KUNIT_ASSERT_STREQ(kunittest, buf, expected);
db7313005e9c2d lib/test_bitmap.c  Yury Norov       2021-08-14  478  
8ef3340af7139f lib/bitmap_kunit.c Tamir Duberstein 2025-12-22  479  	kunit_info(kunittest, "Time: %llu", time);
db7313005e9c2d lib/test_bitmap.c  Yury Norov       2021-08-14  480  }
db7313005e9c2d lib/test_bitmap.c  Yury Norov       2021-08-14  481  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki