[PATCH] lib/tests: add KUnit test for bitops

Ryota Sakamoto posted 1 patch 4 weeks, 1 day ago
There is a newer version of this series
MAINTAINERS              |   1 +
lib/Kconfig.debug        |  17 ++++++
lib/tests/Makefile       |   1 +
lib/tests/bitops_kunit.c | 142 +++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 161 insertions(+)
[PATCH] lib/tests: add KUnit test for bitops
Posted by Ryota Sakamoto 4 weeks, 1 day ago
Add a KUnit test suite for the bitops API.

The existing 'lib/test_bitops.c' is preserved as-is because it contains
ad-hoc micro-benchmarks 'test_fns' and is intended to ensure no compiler
warnings from C=1 sparse checker or -Wextra compilations.

Introduce 'lib/tests/bitops_kunit.c' for functional regression testing. It
ports the test logic and data patterns from 'lib/test_bitops.c' to KUnit,
verifying correct behavior across various input patterns and
architecture-specific edge cases using isolated stack-allocated bitmaps.

Also improve the find_first_bit() test to check the full bitmap length
(BITOPS_LENGTH) instead of omitting the last bit, ensuring the bitmap is
completely empty after cleanup.

Verified on x86_64, i386, and arm64 architectures.

Signed-off-by: Ryota Sakamoto <sakamo.ryota@gmail.com>
---
 MAINTAINERS              |   1 +
 lib/Kconfig.debug        |  17 ++++++
 lib/tests/Makefile       |   1 +
 lib/tests/bitops_kunit.c | 142 +++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 161 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index a0dd762f5648b7e4e6fc62560662e43720422e01..ad978698deedca3e6acdf62145a48d45b579cec2 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -4468,6 +4468,7 @@ F:	include/asm-generic/bitops.h
 F:	include/linux/bitops.h
 F:	lib/hweight.c
 F:	lib/test_bitops.c
+F:	lib/tests/bitops_kunit.c
 F:	tools/*/bitops*
 
 BITOPS API BINDINGS [RUST]
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index ba36939fda79bf890834b586c366a28acd434ef9..3d7fdea11354a421ac5b7283a14f0752261d4a63 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -2652,6 +2652,23 @@ config TEST_SYSCTL
 
 	  If unsure, say N.
 
+config BITOPS_KUNIT
+	tristate "KUnit test bitops functions at runtime" if !KUNIT_ALL_TESTS
+	depends on KUNIT
+	default KUNIT_ALL_TESTS
+	help
+	  Enable this option to test the bitops functions at boot.
+
+	  KUnit tests run during boot and output the results to the debug log
+	  in TAP format (http://testanything.org/). Only useful for kernel devs
+	  running the KUnit test harness, and not intended for inclusion into a
+	  production build.
+
+	  For more information on KUnit and unit tests in general please refer
+	  to the KUnit documentation in Documentation/dev-tools/kunit/.
+
+	  If unsure, say N.
+
 config BITFIELD_KUNIT
 	tristate "KUnit test bitfield functions at runtime" if !KUNIT_ALL_TESTS
 	depends on KUNIT
diff --git a/lib/tests/Makefile b/lib/tests/Makefile
index 601dba4b7d966d568d0bb6671dffaf4d68489549..0f24048f36845b13daebcb504e182f738e0a807f 100644
--- a/lib/tests/Makefile
+++ b/lib/tests/Makefile
@@ -5,6 +5,7 @@
 # KUnit tests
 CFLAGS_bitfield_kunit.o := $(DISABLE_STRUCTLEAK_PLUGIN)
 obj-$(CONFIG_BASE64_KUNIT) += base64_kunit.o
+obj-$(CONFIG_BITOPS_KUNIT) += bitops_kunit.o
 obj-$(CONFIG_BITFIELD_KUNIT) += bitfield_kunit.o
 obj-$(CONFIG_BITS_TEST) += test_bits.o
 obj-$(CONFIG_BLACKHOLE_DEV_KUNIT_TEST) += blackhole_dev_kunit.o
diff --git a/lib/tests/bitops_kunit.c b/lib/tests/bitops_kunit.c
new file mode 100644
index 0000000000000000000000000000000000000000..5c47a12760611a0445feb37c252d00f3bf73f6a1
--- /dev/null
+++ b/lib/tests/bitops_kunit.c
@@ -0,0 +1,142 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2020 Intel Corporation
+ * Copyright (C) 2026 Ryota Sakamoto <sakamo.ryota@gmail.com>
+ */
+
+#include <linux/bitops.h>
+#include <linux/module.h>
+#include <kunit/test.h>
+
+/* use an enum because that's the most common BITMAP usage */
+enum bitops_fun {
+	BITOPS_4 = 4,
+	BITOPS_7 = 7,
+	BITOPS_11 = 11,
+	BITOPS_31 = 31,
+	BITOPS_88 = 88,
+	BITOPS_LENGTH = 256
+};
+
+struct bitops_test_case {
+	const char *str;
+	const long nr;
+};
+
+static struct bitops_test_case bitops_cases[] = {
+	{
+		.str = "BITOPS_4",
+		.nr = BITOPS_4,
+	},
+	{
+		.str = "BITOPS_7",
+		.nr = BITOPS_7,
+	},
+	{
+		.str = "BITOPS_11",
+		.nr = BITOPS_11,
+	},
+	{
+		.str = "BITOPS_31",
+		.nr = BITOPS_31,
+	},
+	{
+		.str = "BITOPS_88",
+		.nr = BITOPS_88,
+	},
+};
+
+KUNIT_ARRAY_PARAM_DESC(bitops, bitops_cases, str);
+
+static void test_set_bit_clear_bit(struct kunit *test)
+{
+	const struct bitops_test_case *params = test->param_value;
+	DECLARE_BITMAP(bitmap, BITOPS_LENGTH);
+	int bit_set;
+
+	bitmap_zero(bitmap, BITOPS_LENGTH);
+
+	set_bit(params->nr, bitmap);
+	KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));
+
+	clear_bit(params->nr, bitmap);
+	KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));
+
+	bit_set = find_first_bit(bitmap, BITOPS_LENGTH);
+	KUNIT_EXPECT_EQ(test, bit_set, BITOPS_LENGTH);
+}
+
+struct order_test_case {
+	const char *str;
+	const unsigned int count;
+	const int expected;
+};
+
+static struct order_test_case order_test_cases[] = {
+	{"0x00000003", 0x00000003,  2},
+	{"0x00000004", 0x00000004,  2},
+	{"0x00001fff", 0x00001fff, 13},
+	{"0x00002000", 0x00002000, 13},
+	{"0x50000000", 0x50000000, 31},
+	{"0x80000000", 0x80000000, 31},
+	{"0x80003000", 0x80003000, 32},
+};
+
+KUNIT_ARRAY_PARAM_DESC(order, order_test_cases, str);
+
+static void test_get_count_order(struct kunit *test)
+{
+	const struct order_test_case *params = test->param_value;
+
+	KUNIT_EXPECT_EQ(test, get_count_order(params->count), params->expected);
+	KUNIT_EXPECT_EQ(test, get_count_order_long(params->count), params->expected);
+}
+
+#ifdef CONFIG_64BIT
+struct order_long_test_case {
+	const char *str;
+	const unsigned long count;
+	const int expected;
+};
+
+static struct order_long_test_case order_long_test_cases[] = {
+	{"0x0000000300000000", 0x0000000300000000, 34},
+	{"0x0000000400000000", 0x0000000400000000, 34},
+	{"0x00001fff00000000", 0x00001fff00000000, 45},
+	{"0x0000200000000000", 0x0000200000000000, 45},
+	{"0x5000000000000000", 0x5000000000000000, 63},
+	{"0x8000000000000000", 0x8000000000000000, 63},
+	{"0x8000300000000000", 0x8000300000000000, 64},
+};
+
+KUNIT_ARRAY_PARAM_DESC(order_long, order_long_test_cases, str);
+
+static void test_get_count_order_long(struct kunit *test)
+{
+	const struct order_long_test_case *params = test->param_value;
+
+	KUNIT_EXPECT_EQ(test, get_count_order_long(params->count), params->expected);
+}
+#endif
+
+static struct kunit_case bitops_test_cases[] = {
+	KUNIT_CASE_PARAM(test_set_bit_clear_bit, bitops_gen_params),
+	KUNIT_CASE_PARAM(test_get_count_order, order_gen_params),
+#ifdef CONFIG_64BIT
+	KUNIT_CASE_PARAM(test_get_count_order_long, order_long_gen_params),
+#endif
+	{},
+};
+
+static struct kunit_suite bitops_test_suite = {
+	.name = "bitops",
+	.test_cases = bitops_test_cases,
+};
+
+kunit_test_suite(bitops_test_suite);
+
+MODULE_AUTHOR("Jesse Brandeburg <jesse.brandeburg@intel.com>");
+MODULE_AUTHOR("Wei Yang <richard.weiyang@gmail.com>");
+MODULE_AUTHOR("Ryota Sakamoto <sakamo.ryota@gmail.com>");
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Bit testing module");

---
base-commit: 79b95d74470dd97d7d0908d5a3c0734a23e51aa4
change-id: 20260109-kunit-bitops-9abb5cac8cba

Best regards,
-- 
Ryota Sakamoto <sakamo.ryota@gmail.com>
Re: [PATCH] lib/tests: add KUnit test for bitops
Posted by Yury Norov 4 weeks, 1 day ago
Hi Ryota,

Is this your first commitment? If so - congrats and welcome to the
community!

On Sat, Jan 10, 2026 at 03:42:21AM +0900, Ryota Sakamoto wrote:
> Add a KUnit test suite for the bitops API.
> 
> The existing 'lib/test_bitops.c' is preserved as-is because it contains
> ad-hoc micro-benchmarks 'test_fns' and is intended to ensure no compiler
> warnings from C=1 sparse checker or -Wextra compilations.
> 
> Introduce 'lib/tests/bitops_kunit.c' for functional regression testing. It
> ports the test logic and data patterns from 'lib/test_bitops.c' to KUnit,

'ports' her is an euphemism for copy-pasting?

> verifying correct behavior across various input patterns and
> architecture-specific edge cases using isolated stack-allocated bitmaps.
> 
> Also improve the find_first_bit() test to check the full bitmap length
> (BITOPS_LENGTH) instead of omitting the last bit, ensuring the bitmap is
> completely empty after cleanup.
>
> Verified on x86_64, i386, and arm64 architectures.
> 
> Signed-off-by: Ryota Sakamoto <sakamo.ryota@gmail.com>

OK, it doesn't look like a pure duplicate.

I'm not a user of kunit, but some people are. And seemingly it would help
to improve testing coverage through those enabling KUNIT_ALL_TESTS. So I
don't object against this test.

Can you please resend this patch, and in commit message list all the
tests from the original lib/test_bitops? Please mention those you move
to the new test. Is there any new coverage in your version?

Please attach whatever it prints as an output, if any.

> ---
>  MAINTAINERS              |   1 +
>  lib/Kconfig.debug        |  17 ++++++
>  lib/tests/Makefile       |   1 +
>  lib/tests/bitops_kunit.c | 142 +++++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 161 insertions(+)
> 
> diff --git a/MAINTAINERS b/MAINTAINERS
> index a0dd762f5648b7e4e6fc62560662e43720422e01..ad978698deedca3e6acdf62145a48d45b579cec2 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -4468,6 +4468,7 @@ F:	include/asm-generic/bitops.h
>  F:	include/linux/bitops.h
>  F:	lib/hweight.c
>  F:	lib/test_bitops.c
> +F:	lib/tests/bitops_kunit.c
>  F:	tools/*/bitops*
>  
>  BITOPS API BINDINGS [RUST]
> diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
> index ba36939fda79bf890834b586c366a28acd434ef9..3d7fdea11354a421ac5b7283a14f0752261d4a63 100644
> --- a/lib/Kconfig.debug
> +++ b/lib/Kconfig.debug
> @@ -2652,6 +2652,23 @@ config TEST_SYSCTL
>  
>  	  If unsure, say N.
>  
> +config BITOPS_KUNIT
> +	tristate "KUnit test bitops functions at runtime" if !KUNIT_ALL_TESTS
> +	depends on KUNIT
> +	default KUNIT_ALL_TESTS
> +	help
> +	  Enable this option to test the bitops functions at boot.

It's tested at boot only if 'y'. If 'm' - it's not.

> +
> +	  KUnit tests run during boot and output the results to the debug log
> +	  in TAP format (http://testanything.org/). Only useful for kernel devs
> +	  running the KUnit test harness, and not intended for inclusion into a
> +	  production build.
> +
> +	  For more information on KUnit and unit tests in general please refer
> +	  to the KUnit documentation in Documentation/dev-tools/kunit/.

Those 2 paragraphs don't relate to the new test, don't even mention
it. Please drop them.

I would add a note that this is a partial copy of test_bitops, and in case
of _any_ doubt, one could enable config TEST_BITOPS and run the original
test.

> +
> +	  If unsure, say N.
> +
>  config BITFIELD_KUNIT
>  	tristate "KUnit test bitfield functions at runtime" if !KUNIT_ALL_TESTS
>  	depends on KUNIT
> diff --git a/lib/tests/Makefile b/lib/tests/Makefile
> index 601dba4b7d966d568d0bb6671dffaf4d68489549..0f24048f36845b13daebcb504e182f738e0a807f 100644
> --- a/lib/tests/Makefile
> +++ b/lib/tests/Makefile
> @@ -5,6 +5,7 @@
>  # KUnit tests
>  CFLAGS_bitfield_kunit.o := $(DISABLE_STRUCTLEAK_PLUGIN)
>  obj-$(CONFIG_BASE64_KUNIT) += base64_kunit.o
> +obj-$(CONFIG_BITOPS_KUNIT) += bitops_kunit.o
>  obj-$(CONFIG_BITFIELD_KUNIT) += bitfield_kunit.o
>  obj-$(CONFIG_BITS_TEST) += test_bits.o
>  obj-$(CONFIG_BLACKHOLE_DEV_KUNIT_TEST) += blackhole_dev_kunit.o
> diff --git a/lib/tests/bitops_kunit.c b/lib/tests/bitops_kunit.c
> new file mode 100644
> index 0000000000000000000000000000000000000000..5c47a12760611a0445feb37c252d00f3bf73f6a1
> --- /dev/null
> +++ b/lib/tests/bitops_kunit.c
> @@ -0,0 +1,142 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Copyright (C) 2020 Intel Corporation
> + * Copyright (C) 2026 Ryota Sakamoto <sakamo.ryota@gmail.com>
> + */
> +
> +#include <linux/bitops.h>
> +#include <linux/module.h>
> +#include <kunit/test.h>
> +
> +/* use an enum because that's the most common BITMAP usage */
> +enum bitops_fun {
> +	BITOPS_4 = 4,
> +	BITOPS_7 = 7,
> +	BITOPS_11 = 11,
> +	BITOPS_31 = 31,
> +	BITOPS_88 = 88,
> +	BITOPS_LENGTH = 256
> +};
> +
> +struct bitops_test_case {
> +	const char *str;
> +	const long nr;
> +};
> +
> +static struct bitops_test_case bitops_cases[] = {
> +	{
> +		.str = "BITOPS_4",
> +		.nr = BITOPS_4,
> +	},
> +	{
> +		.str = "BITOPS_7",
> +		.nr = BITOPS_7,
> +	},
> +	{
> +		.str = "BITOPS_11",
> +		.nr = BITOPS_11,
> +	},
> +	{
> +		.str = "BITOPS_31",
> +		.nr = BITOPS_31,
> +	},
> +	{
> +		.str = "BITOPS_88",
> +		.nr = BITOPS_88,
> +	},
> +};
> +
> +KUNIT_ARRAY_PARAM_DESC(bitops, bitops_cases, str);
> +
> +static void test_set_bit_clear_bit(struct kunit *test)
> +{
> +	const struct bitops_test_case *params = test->param_value;
> +	DECLARE_BITMAP(bitmap, BITOPS_LENGTH);
> +	int bit_set;
> +
> +	bitmap_zero(bitmap, BITOPS_LENGTH);
> +
> +	set_bit(params->nr, bitmap);
> +	KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));
> +
> +	clear_bit(params->nr, bitmap);
> +	KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));
> +
> +	bit_set = find_first_bit(bitmap, BITOPS_LENGTH);
> +	KUNIT_EXPECT_EQ(test, bit_set, BITOPS_LENGTH);
> +}
> +
> +struct order_test_case {
> +	const char *str;
> +	const unsigned int count;
> +	const int expected;
> +};
> +
> +static struct order_test_case order_test_cases[] = {
> +	{"0x00000003", 0x00000003,  2},
> +	{"0x00000004", 0x00000004,  2},
> +	{"0x00001fff", 0x00001fff, 13},
> +	{"0x00002000", 0x00002000, 13},
> +	{"0x50000000", 0x50000000, 31},
> +	{"0x80000000", 0x80000000, 31},
> +	{"0x80003000", 0x80003000, 32},
> +};
> +
> +KUNIT_ARRAY_PARAM_DESC(order, order_test_cases, str);
> +
> +static void test_get_count_order(struct kunit *test)
> +{
> +	const struct order_test_case *params = test->param_value;
> +
> +	KUNIT_EXPECT_EQ(test, get_count_order(params->count), params->expected);
> +	KUNIT_EXPECT_EQ(test, get_count_order_long(params->count), params->expected);
> +}
> +
> +#ifdef CONFIG_64BIT
> +struct order_long_test_case {
> +	const char *str;
> +	const unsigned long count;
> +	const int expected;
> +};
> +
> +static struct order_long_test_case order_long_test_cases[] = {
> +	{"0x0000000300000000", 0x0000000300000000, 34},
> +	{"0x0000000400000000", 0x0000000400000000, 34},
> +	{"0x00001fff00000000", 0x00001fff00000000, 45},
> +	{"0x0000200000000000", 0x0000200000000000, 45},
> +	{"0x5000000000000000", 0x5000000000000000, 63},
> +	{"0x8000000000000000", 0x8000000000000000, 63},
> +	{"0x8000300000000000", 0x8000300000000000, 64},
> +};
> +
> +KUNIT_ARRAY_PARAM_DESC(order_long, order_long_test_cases, str);
> +
> +static void test_get_count_order_long(struct kunit *test)
> +{
> +	const struct order_long_test_case *params = test->param_value;
> +
> +	KUNIT_EXPECT_EQ(test, get_count_order_long(params->count), params->expected);
> +}
> +#endif
> +
> +static struct kunit_case bitops_test_cases[] = {
> +	KUNIT_CASE_PARAM(test_set_bit_clear_bit, bitops_gen_params),
> +	KUNIT_CASE_PARAM(test_get_count_order, order_gen_params),
> +#ifdef CONFIG_64BIT
> +	KUNIT_CASE_PARAM(test_get_count_order_long, order_long_gen_params),
> +#endif
> +	{},
> +};
> +
> +static struct kunit_suite bitops_test_suite = {
> +	.name = "bitops",
> +	.test_cases = bitops_test_cases,
> +};
> +
> +kunit_test_suite(bitops_test_suite);
> +
> +MODULE_AUTHOR("Jesse Brandeburg <jesse.brandeburg@intel.com>");
> +MODULE_AUTHOR("Wei Yang <richard.weiyang@gmail.com>");

Those gentlemen are not even in CC. Are you sure they agree to
be in this list?

Thanks,
Yury

> +MODULE_AUTHOR("Ryota Sakamoto <sakamo.ryota@gmail.com>");
> +MODULE_LICENSE("GPL");
> +MODULE_DESCRIPTION("Bit testing module");
> 
> ---
> base-commit: 79b95d74470dd97d7d0908d5a3c0734a23e51aa4
> change-id: 20260109-kunit-bitops-9abb5cac8cba
> 
> Best regards,
> -- 
> Ryota Sakamoto <sakamo.ryota@gmail.com>
Re: [PATCH] lib/tests: add KUnit test for bitops
Posted by Ryota Sakamoto 4 weeks ago
Hi Yury,

Thank you for your warm welcome and detailed review.

On Sat, Jan 10, 2026 at 11:25 AM Yury Norov <ynorov@nvidia.com> wrote:
> I'm not a user of kunit, but some people are. And seemingly it would help
> to improve testing coverage through those enabling KUNIT_ALL_TESTS. So I
> don't object against this test.

I'm glad you agree. As you noted, while it is based on the logic from
lib/test_bitops.c, it adapts the test to KUnit framework and fixes the coverage
gap in find_first_bit().

> Can you please resend this patch, and in commit message list all the
> tests from the original lib/test_bitops? Please mention those you move
> to the new test. Is there any new coverage in your version?
>
> Please attach whatever it prints as an output, if any.

Sure, in v2, I will include the list for ported test cases and include
the execution output.

> It's tested at boot only if 'y'. If 'm' - it's not.
> Those 2 paragraphs don't relate to the new test, don't even mention
> it. Please drop them.
> I would add a note that this is a partial copy of test_bitops, and in case
> of _any_ doubt, one could enable config TEST_BITOPS and run the original
> test.

You are right. I will remove the generic KUnit description and add
a reference to the original test.

> Those gentlemen are not even in CC. Are you sure they agree to
> be in this list?

I kept their names to credit the original authors from lib/test_bitops.c. I will
include them in CC for v2.

Regards,
Ryota Sakamoto