Skip to contentSkip to conversation

lib/prime_numbers: convert self-test to KUnit

PATCH In review Tamir Duberstein 2 participants 9 messages Feb 9, 2025 – Feb 11, 2025

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

v5 cover lib/prime_numbers: convert self-test to KUnit

This is a clear example of a unit test.

I tested this using:

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

On success:
; [08:51:41] ============== math-prime_numbers (1 subtest) ============== ; [08:51:41] [PASSED] prime_numbers_test
; [08:51:41] =============== [PASSED] math-prime_numbers ================ ; [08:51:41] ============================================================ ; [08:51:41] Testing complete. Ran 1 tests: passed: 1

On failure:
; [08:50:19] ============== math-prime_numbers (1 subtest) ==============
; [08:50:19] # prime_numbers_test: ASSERTION FAILED at lib/math/tests/prime_numbers_kunit.c:28
; [08:50:19] Expected slow == fast, but
; [08:50:19] slow == 0 (0x0)
; [08:50:19] fast == 1 (0x1)
; [08:50:19] is-prime(2)
; [08:50:19] [FAILED] prime_numbers_test
; [08:50:19] # module: prime_numbers_kunit
; [08:50:19] # math-prime_numbers: primes.{last=61, .sz=64, .primes[]=...x28208a20a08a28ac} = 2-3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61 ; [08:50:19] =============== [FAILED] math-prime_numbers ================
; [08:50:19] ============================================================
; [08:50:19] Testing complete. Ran 1 tests: failed: 1

---
Changes in v5:
- Rebase against mm/mm-everything.
- Correctly EXPORT_SYMBOL(with_primes).
- Link to v4: https://lore.kernel.org/r/[email protected]

Apologies for the churn.

Changes in v4:
- Use #if IS_ENABLED to handle CONFIG_PRIME_NUMBERS_KUNIT_TEST=m.
(Andrew Morton)
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Remove selftest_max again; snuck into v2.
- Restore #include <linux/slab.h>.
- Sending v3 early because kernel test robot is sad.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Keep all the details hidden in prime_numbers.c; expose with_primes
for debug logging in the test. (David Gow)
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Tamir Duberstein (2):
lib/math: Hook up tests/Makefile lib/prime_numbers: convert self-test to KUnit

 lib/Kconfig.debug                            | 14 +++++
 lib/math/Makefile                            |  5 +-
 lib/math/prime_numbers.c                     | 87 +++++-----------------------
 lib/math/prime_numbers_private.h             | 17 ++++++
 lib/math/tests/Makefile                      |  1 +
 lib/math/tests/prime_numbers_kunit.c         | 59 +++++++++++++++++++
 tools/testing/selftests/lib/config           |  1 -
 tools/testing/selftests/lib/prime_numbers.sh |  4 --
 8 files changed, 108 insertions(+), 80 deletions(-)

---
base-commit: feb950f421d199acfe2503302f49632abfcf8663
change-id: 20250207-prime_numbers-kunit-convert-71c9b3c1d1d4

Best regards,

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

PATCH v5 1/2 lib/math: Hook up tests/Makefile

This Makefile has been dead code since it was added in commit 7fcc9b53216c ("lib/math: Add int_pow test suite"); the tests worked because of the duplicated rules in the parent directory's Makefile. Wire up tests/Makefile and remove the duplication.

Fixes 7fcc9b53216c ("lib/math: Add int_pow test suite")
---
 lib/math/Makefile | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
lib/math/Makefile +2 -3
index 3484078177af..d390da9a59fe 100644
@@ -5,9 +5,8 @@ obj-$(CONFIG_CORDIC)		+= cordic.o
 obj-$(CONFIG_PRIME_NUMBERS)	+= prime_numbers.o
 obj-$(CONFIG_RATIONAL)		+= rational.o
 
-obj-$(CONFIG_INT_POW_TEST)  += tests/int_pow_kunit.o
 obj-$(CONFIG_TEST_DIV64)	+= test_div64.o
 obj-$(CONFIG_TEST_MULDIV64)	+= test_mul_u64_u64_div_u64.o
 obj-$(CONFIG_RATIONAL_KUNIT_TEST) += rational-test.o
-obj-$(CONFIG_INT_SQRT_KUNIT_TEST) += tests/int_sqrt_kunit.o
-obj-$(CONFIG_GCD_KUNIT_TEST) += tests/gcd_kunit.o
+
+obj-y += tests/
Signature · 1 line
2.48.1
#2 lore raw

PATCH v5 2/2 lib/prime_numbers: convert self-test to KUnit

In reply to Tamir Duberstein

Extract a private header and convert the prime_numbers self-test to a KUnit test. I considered parameterizing the test using KUNIT_CASE_PARAM but didn't see how it was possible since the test logic is entangled with the test parameter generation logic.

---
 lib/Kconfig.debug                            | 14 +++++
 lib/math/prime_numbers.c                     | 87 +++++-----------------------
 lib/math/prime_numbers_private.h             | 17 ++++++
 lib/math/tests/Makefile                      |  1 +
 lib/math/tests/prime_numbers_kunit.c         | 59 +++++++++++++++++++
 tools/testing/selftests/lib/config           |  1 -
 tools/testing/selftests/lib/prime_numbers.sh |  4 --
 7 files changed, 106 insertions(+), 77 deletions(-)
lib/Kconfig.debug +14 -0
index 0ccfab3ecd21..7570caaacddb 100644
@@ -3210,6 +3210,20 @@ config GCD_KUNIT_TEST
 
 	  If unsure, say N
 
+config PRIME_NUMBERS_KUNIT_TEST
+	tristate "Prime number generator test" if !KUNIT_ALL_TESTS
+	depends on KUNIT
+	select PRIME_NUMBERS
+	default KUNIT_ALL_TESTS
+	help
+	  This option enables the KUnit test suite for the {is,next}_prime_number
+	  functions.
+
+	  Enabling this option will include tests that compare the prime number
+	  generator functions against a brute force implementation.
+
+	  If unsure, say N
+
 endif # RUNTIME_TESTING_MENU
 
 config ARCH_USE_MEMTEST
lib/math/prime_numbers.c +15 -72

117 lines not on this page · raw message

lib/math/prime_numbers_private.h new file +17 -0
new file mode 100644
index 000000000000..a4cab3c8c727
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#include <linux/types.h>
+
+struct primes {
+	struct rcu_head rcu;
+	unsigned long last, sz;
+	unsigned long primes[];
+};
+
+#if IS_ENABLED(CONFIG_PRIME_NUMBERS_KUNIT_TEST)
+bool slow_is_prime_number(unsigned long x);
+typedef void (*primes_fn)(void *, const struct primes *);
+
+// Calls the callback under RCU lock. The callback must not retain the primes pointer.
+void with_primes(void *ctx, primes_fn fn);
+#endif
lib/math/tests/Makefile +1 -0
index 6a04b4eb22b6..40ad9de29acc 100644
@@ -3,3 +3,4 @@
 obj-$(CONFIG_INT_POW_TEST) += int_pow_kunit.o
 obj-$(CONFIG_INT_SQRT_KUNIT_TEST) += int_sqrt_kunit.o
 obj-$(CONFIG_GCD_KUNIT_TEST) += gcd_kunit.o
+obj-$(CONFIG_PRIME_NUMBERS_KUNIT_TEST) += prime_numbers_kunit.o
lib/math/tests/prime_numbers_kunit.c new file +59 -0

59 lines not on this page · raw message

tools/testing/selftests/lib/config +0 -1
index dc15aba8d0a3..306a3d4dca98 100644
@@ -1,5 +1,4 @@
 CONFIG_TEST_PRINTF=m
 CONFIG_TEST_SCANF=m
 CONFIG_TEST_BITMAP=m
-CONFIG_PRIME_NUMBERS=m
 CONFIG_TEST_BITOPS=m
tools/testing/selftests/lib/prime_numbers.sh deleted +0 -4
deleted file mode 100755
index 370b79a9cb2e..000000000000
@@ -1,4 +0,0 @@
-#!/bin/sh
-# SPDX-License-Identifier: GPL-2.0
-# Checks fast/slow prime_number generation for inconsistencies
-$(dirname $0)/../kselftest/module.sh "prime numbers" prime_numbers selftest=65536
Signature · 1 line
2.48.1
#3 lore raw
Quoted 6 lines · Tamir Duberstein

Extract a private header and convert the prime_numbers self-test to a KUnit test. I considered parameterizing the test using KUNIT_CASE_PARAM but didn't see how it was possible since the test logic is entangled with the test parameter generation logic.

Quoted 9 lines
---
 lib/Kconfig.debug                            | 14 +++++
 lib/math/prime_numbers.c                     | 87 +++++-----------------------
 lib/math/prime_numbers_private.h             | 17 ++++++
 lib/math/tests/Makefile                      |  1 +
 lib/math/tests/prime_numbers_kunit.c         | 59 +++++++++++++++++++
 tools/testing/selftests/lib/config           |  1 -
 tools/testing/selftests/lib/prime_numbers.sh |  4 --
 7 files changed, 106 insertions(+), 77 deletions(-)

Thanks! I've applied this and rebased it onto:
https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git/log/?h=for-next/move-kunit-tests

Signature · 1 line
Kees Cook
#4 lore raw
Quoted 18 lines · Kees Cook
Quoted 6 lines · Tamir Duberstein

Extract a private header and convert the prime_numbers self-test to a KUnit test. I considered parameterizing the test using KUNIT_CASE_PARAM but didn't see how it was possible since the test logic is entangled with the test parameter generation logic.

Quoted 9 lines
---
 lib/Kconfig.debug                            | 14 +++++
 lib/math/prime_numbers.c                     | 87 +++++-----------------------
 lib/math/prime_numbers_private.h             | 17 ++++++
 lib/math/tests/Makefile                      |  1 +
 lib/math/tests/prime_numbers_kunit.c         | 59 +++++++++++++++++++
 tools/testing/selftests/lib/config           |  1 -
 tools/testing/selftests/lib/prime_numbers.sh |  4 --
 7 files changed, 106 insertions(+), 77 deletions(-)
Quoted 5 lines

Thanks! I've applied this and rebased it onto:
https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git/log/?h=for-next/move-kunit-tests

--
Kees Cook

Thanks! Could you also take the first patch in the series[0]? The new test won't build without it because lib/math/tests/Makefile is dead code.

[0] https://lore.kernel.org/all/[email protected]/

Cheers.
Tamir

#5 lore raw

Ah, I see https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git/commit/?h=for-next/move-kunit-tests&id=3e50ba8fc834cadead733e4feeb969fce2f3b6e1
now.

Quoted 34 lines · Tamir Duberstein
Quoted 18 lines · Kees Cook
Quoted 6 lines · Tamir Duberstein

Extract a private header and convert the prime_numbers self-test to a KUnit test. I considered parameterizing the test using KUNIT_CASE_PARAM but didn't see how it was possible since the test logic is entangled with the test parameter generation logic.

Quoted 9 lines
---
 lib/Kconfig.debug                            | 14 +++++
 lib/math/prime_numbers.c                     | 87 +++++-----------------------
 lib/math/prime_numbers_private.h             | 17 ++++++
 lib/math/tests/Makefile                      |  1 +
 lib/math/tests/prime_numbers_kunit.c         | 59 +++++++++++++++++++
 tools/testing/selftests/lib/config           |  1 -
 tools/testing/selftests/lib/prime_numbers.sh |  4 --
 7 files changed, 106 insertions(+), 77 deletions(-)
Quoted 5 lines

Thanks! I've applied this and rebased it onto:
https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git/log/?h=for-next/move-kunit-tests

--
Kees Cook

Thanks! Could you also take the first patch in the series[0]? The new test won't build without it because lib/math/tests/Makefile is dead code.

[0] https://lore.kernel.org/all/[email protected]/

Cheers.
Tamir

#6 lore raw

Kees, it looks like the private header didn't make it to your tree. This bit is missing:

lib/math/prime_numbers.c +1 -0
index f88d6e64dbdc..de59f001c8c7 100644
@@ -58,6 +58,7 @@ static DEFINE_MUTEX(lock);
 static const struct primes __rcu *primes = RCU_INITIALIZER(&small_primes);
 
 #if IS_ENABLED(CONFIG_PRIME_NUMBERS_KUNIT_TEST)
+// Calls the callback under RCU lock. The callback must not retain

the primes pointer.
void with_primes(void *ctx, primes_fn fn)

 {
        rcu_read_lock();
lib/math/prime_numbers_private.h new file +16 -0
new file mode 100644
index 000000000000..f3ebf5386e6b
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#include <linux/types.h>
+
+struct primes {
+       struct rcu_head rcu;
+       unsigned long last, sz;
+       unsigned long primes[];
+};
+
+#if IS_ENABLED(CONFIG_PRIME_NUMBERS_KUNIT_TEST)
+typedef void (*primes_fn)(void *, const struct primes *);
+
+void with_primes(void *ctx, primes_fn fn);
+bool slow_is_prime_number(unsigned long x);
+#endif

Tamir

Quoted 39 lines · Tamir Duberstein

Ah, I see https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git/commit/?h=for-next/move-kunit-tests&id=3e50ba8fc834cadead733e4feeb969fce2f3b6e1
now.

Quoted 34 lines · Tamir Duberstein
Quoted 18 lines · Kees Cook
Quoted 6 lines · Tamir Duberstein

Extract a private header and convert the prime_numbers self-test to a KUnit test. I considered parameterizing the test using KUNIT_CASE_PARAM but didn't see how it was possible since the test logic is entangled with the test parameter generation logic.

Quoted 9 lines
---
 lib/Kconfig.debug                            | 14 +++++
 lib/math/prime_numbers.c                     | 87 +++++-----------------------
 lib/math/prime_numbers_private.h             | 17 ++++++
 lib/math/tests/Makefile                      |  1 +
 lib/math/tests/prime_numbers_kunit.c         | 59 +++++++++++++++++++
 tools/testing/selftests/lib/config           |  1 -
 tools/testing/selftests/lib/prime_numbers.sh |  4 --
 7 files changed, 106 insertions(+), 77 deletions(-)
Quoted 5 lines

Thanks! I've applied this and rebased it onto:
https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git/log/?h=for-next/move-kunit-tests

--
Kees Cook

Thanks! Could you also take the first patch in the series[0]? The new test won't build without it because lib/math/tests/Makefile is dead code.

[0] https://lore.kernel.org/all/[email protected]/

Cheers.
Tamir

#7 lore raw
Quoted 2 lines · Tamir Duberstein

Kees, it looks like the private header didn't make it to your tree. This bit is missing:

Whoops! Thanks. I've added it (and fixed the comment style).

I really need to do my build/run testing from a fresh checkout instead of in the same work tree. :(

Signature · 1 line
Kees Cook
#8 lore raw
Quoted 5 lines · Kees Cook
Quoted 2 lines · Tamir Duberstein

Kees, it looks like the private header didn't make it to your tree. This bit is missing:

Quoted 7 lines

Whoops! Thanks. I've added it (and fixed the comment style).

I really need to do my build/run testing from a fresh checkout instead of in the same work tree. :(

--
Kees Cook

Thanks! I think it's customary to have comments only in the sources - I see you also included it in the header.