Backport crypto and CRC KUnit improvements
Subsystems (2)
cover Backport crypto and CRC KUnit improvements
This series backports improvements to the KUnit testing for the crypto and CRC libraries to 6.18, including:
- Finish fixing the test kconfig options to follow standard conventions
- Add kunitconfig files
- Make the tests be included in 'kunit.py run --alltests'
The crypto patches were adjusted to drop parts that pertain to tests added in later kernels.
While stable backports are normally limited to "fixes", I think it's worth also backporting the kunitconfig files and the all_tests.config support, since these are safe test-only changes that are helpful for testing 6.18. For example, this should make KernelCI run all these tests on 6.18, as it uses 'kunit.py run --alltests'.
Eric Biggers (8):
lib/crc: tests: Make crc_kunit test only the enabled CRC variants lib/crc: tests: Add CRC_ENABLE_ALL_FOR_KUNIT
lib/crc: tests: Add a .kunitconfig file
kunit: configs: Enable all CRC tests in all_tests.config lib/crypto: tests: Add a .kunitconfig file
lib/crypto: tests: Introduce CRYPTO_LIB_ENABLE_ALL_FOR_KUNIT
kunit: configs: Enable all crypto library tests in all_tests.config lib/crypto: tests: Drop the default to CRYPTO_SELFTESTS
lib/crc/.kunitconfig | 3 ++ lib/crc/Kconfig | 17 ++++++++--- lib/crc/tests/crc_kunit.c | 28 +++++++++++++---- lib/crypto/.kunitconfig | 11 +++++++ lib/crypto/tests/Kconfig | 32 +++++++++++++++----- tools/testing/kunit/configs/all_tests.config | 4 +++ 6 files changed, 78 insertions(+), 17 deletions(-) create mode 100644 lib/crc/.kunitconfig create mode 100644 lib/crypto/.kunitconfig
base-commit: 47a33eea6d5145d53e42315381ef28286c2218fb
Signature · 1 line
2.53.0
PATCH 1/8 lib/crc: tests: Make crc_kunit test only the enabled CRC variants
commit 85c9f3a2b805eb96d899da7bcc38a16459aa3c16 upstream.
Like commit 4478e8eeb871 ("lib/crypto: tests: Depend on library options rather than selecting them") did with the crypto library tests, make crc_kunit depend on the code it tests rather than selecting it. This follows the standard convention for KUnit and fixes an issue where enabling KUNIT_ALL_TESTS enabled non-test code.
crc_kunit does differ from the crypto library tests in that it consolidates the tests for multiple CRC variants, with 5 kconfig options, into one KUnit suite. Since depending on *all* of these kconfig options would greatly restrict the ability to enable crc_kunit, instead just depend on *any* of these options. Update crc_kunit accordingly to test only the reachable code.
Alternatively we could split crc_kunit into 5 test suites. But keeping it as one is simpler for now.
--- lib/crc/Kconfig | 7 +------ lib/crc/tests/crc_kunit.c | 28 ++++++++++++++++++++++------ 2 files changed, 23 insertions(+), 12 deletions(-)
lib/crc/Kconfig +1 -6
@@ -97,17 +97,12 @@ config CRC_OPTIMIZATIONS Keep this enabled unless you're really trying to minimize the size of the kernel. config CRC_KUNIT_TEST tristate "KUnit tests for CRC functions" if !KUNIT_ALL_TESTS - depends on KUNIT + depends on KUNIT && (CRC7 || CRC16 || CRC_T10DIF || CRC32 || CRC64) default KUNIT_ALL_TESTS - select CRC7 - select CRC16 - select CRC_T10DIF - select CRC32 - select CRC64 help Unit tests for the CRC library functions. This is intended to help people writing architecture-specific optimized versions. If unsure, say N.
lib/crc/tests/crc_kunit.c +22 -6
@@ -266,12 +266,11 @@ crc_benchmark(struct kunit *test,kunit_info (test,"len=%zu: %llu MB/s\n" , len,div64_u64 ((u64 )len * num_iters *1000 , t)); } } -/* crc7_be */ - +#if IS_REACHABLE (CONFIG_CRC7 )static u64 crc7_be_wrapper (u64 crc,const u8 *p,size_t len) {/* * crc7_be() left-aligns the 7-bit CRC in a u8, whereas the test wants a * right-aligned CRC (in a u64). Convert between the conventions.
@@ -292,13 +291,13 @@ static void crc7_be_test(struct kunit *test)static void crc7_be_benchmark (struct kunit *test) {crc_benchmark (test, crc7_be_wrapper); } +#endif /* CONFIG_CRC7 */ -/* crc16 */ - +#if IS_REACHABLE (CONFIG_CRC16 )static u64 crc16_wrapper (u64 crc,const u8 *p,size_t len) {return crc16 (crc, p, len); }
@@ -316,13 +315,13 @@ static void crc16_test(struct kunit *test)static void crc16_benchmark (struct kunit *test) {crc_benchmark (test, crc16_wrapper); } +#endif /* CONFIG_CRC16 */ -/* crc_t10dif */ - +#if IS_REACHABLE (CONFIG_CRC_T10DIF )static u64 crc_t10dif_wrapper (u64 crc,const u8 *p,size_t len) {return crc_t10dif_update (crc, p, len); }
@@ -340,10 +339,13 @@ static void crc_t10dif_test(struct kunit *test)static void crc_t10dif_benchmark (struct kunit *test) {crc_benchmark (test, crc_t10dif_wrapper); } +#endif /* CONFIG_CRC_T10DIF */ + +#if IS_REACHABLE (CONFIG_CRC32 )/* crc32_le */ static u64 crc32_le_wrapper (u64 crc,const u8 *p,size_t len) {
@@ -412,10 +414,13 @@ static void crc32c_test(struct kunit *test)static void crc32c_benchmark (struct kunit *test) {crc_benchmark (test, crc32c_wrapper); } +#endif /* CONFIG_CRC32 */ + +#if IS_REACHABLE (CONFIG_CRC64 )/* crc64_be */ static u64 crc64_be_wrapper (u64 crc,const u8 *p,size_t len) {
@@ -461,28 +466,39 @@ static void crc64_nvme_test(struct kunit *test)static void crc64_nvme_benchmark (struct kunit *test) {crc_benchmark (test, crc64_nvme_wrapper); } +#endif /* CONFIG_CRC64 */ static struct kunit_case crc_test_cases[] = { +#if IS_REACHABLE(CONFIG_CRC7)KUNIT_CASE (crc7_be_test),KUNIT_CASE (crc7_be_benchmark), +#endif +#if IS_REACHABLE(CONFIG_CRC16)KUNIT_CASE (crc16_test),KUNIT_CASE (crc16_benchmark), +#endif +#if IS_REACHABLE(CONFIG_CRC_T10DIF)KUNIT_CASE (crc_t10dif_test),KUNIT_CASE (crc_t10dif_benchmark), +#endif +#if IS_REACHABLE(CONFIG_CRC32)KUNIT_CASE (crc32_le_test),KUNIT_CASE (crc32_le_benchmark),KUNIT_CASE (crc32_be_test),KUNIT_CASE (crc32_be_benchmark),KUNIT_CASE (crc32c_test),KUNIT_CASE (crc32c_benchmark), +#endif +#if IS_REACHABLE(CONFIG_CRC64)KUNIT_CASE (crc64_be_test),KUNIT_CASE (crc64_be_benchmark),KUNIT_CASE (crc64_nvme_test),KUNIT_CASE (crc64_nvme_benchmark), +#endif {}, };static struct kunit_suite crc_test_suite = { .name ="crc" ,
Signature · 1 line
2.53.0
PATCH 2/8 lib/crc: tests: Add CRC_ENABLE_ALL_FOR_KUNIT
In reply to Eric Biggerscommit cdf22aeaad8430905c3aa3b3d0f2686c65395c22 upstream.
Now that crc_kunit uses the standard "depends on" pattern, enabling the full set of CRC tests is a bit difficult, mainly due to CRC7 being rarely used. Add a kconfig option to make it easier. It is visible only when KUNIT, so hopefully the extra prompt won't be too annoying.
--- lib/crc/Kconfig | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
lib/crc/Kconfig +14 -0
@@ -105,10 +105,24 @@ config CRC_KUNIT_TEST Unit tests for the CRC library functions. This is intended to help people writing architecture-specific optimized versions. If unsure, say N. +config CRC_ENABLE_ALL_FOR_KUNIT + tristate "Enable all CRC functions for KUnit test" + depends on KUNIT + select CRC7 + select CRC16 + select CRC_T10DIF + select CRC32 + select CRC64 + help + Enable all CRC functions that have test code in CRC_KUNIT_TEST. + + Enable this only if you'd like the CRC KUnit test suite to test all + the CRC variants, even ones that wouldn't otherwise need to be built. + config CRC_BENCHMARK bool "Benchmark for the CRC functions" depends on CRC_KUNIT_TEST help Include benchmarks in the KUnit test suite for the CRC functions.
Signature · 1 line
2.53.0
PATCH 3/8 lib/crc: tests: Add a .kunitconfig file
In reply to Eric Biggerscommit c13cee2fc7f137dd25ed50c63eddcc578624f204 upstream.
Add a .kunitconfig file to the lib/crc/ directory so that the CRC library tests can be run more easily using kunit.py. Example with UML:
tools/testing/kunit/kunit.py run --kunitconfig=lib/crc
Example with QEMU:
tools/testing/kunit/kunit.py run --kunitconfig=lib/crc --arch=arm64 --make_options LLVM=1
--- lib/crc/.kunitconfig | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 lib/crc/.kunitconfig
lib/crc/.kunitconfig new file +3 -0
@@ -0,0 +1,3 @@ +CONFIG_KUNIT=y +CONFIG_CRC_ENABLE_ALL_FOR_KUNIT=y +CONFIG_CRC_KUNIT_TEST=y
Signature · 1 line
2.53.0
PATCH 4/8 kunit: configs: Enable all CRC tests in all_tests.config
In reply to Eric Biggerscommit 44ff3791d6295f7b51dd2711aad6a03dd79aef22 upstream.
The new option CONFIG_CRC_ENABLE_ALL_FOR_KUNIT enables all the CRC code that has KUnit tests, causing CONFIG_KUNIT_ALL_TESTS to enable all these tests. Add this option to all_tests.config so that kunit.py will run them when passed the --alltests option.
--- tools/testing/kunit/configs/all_tests.config | 2 ++ 1 file changed, 2 insertions(+)
tools/testing/kunit/configs/all_tests.config +2 -0
@@ -44,10 +44,12 @@ CONFIG_REGMAP_BUILD=y CONFIG_AUDIT=y CONFIG_PRIME_NUMBERS=y +CONFIG_CRC_ENABLE_ALL_FOR_KUNIT=y + CONFIG_SECURITY=y CONFIG_SECURITY_APPARMOR=y CONFIG_SECURITY_LANDLOCK=y CONFIG_SOUND=y
Signature · 1 line
2.53.0
PATCH 5/8 lib/crypto: tests: Add a .kunitconfig file
In reply to Eric Biggerscommit 20d6f07004d639967dcb00994d56ce6d16118e9e upstream.
Add a .kunitconfig file to the lib/crypto/ directory so that the crypto library tests can be run more easily using kunit.py. Example with UML:
tools/testing/kunit/kunit.py run --kunitconfig=lib/crypto
Example with QEMU:
tools/testing/kunit/kunit.py run --kunitconfig=lib/crypto --arch=arm64 --make_options LLVM=1
--- lib/crypto/.kunitconfig | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 lib/crypto/.kunitconfig
lib/crypto/.kunitconfig new file +23 -0
@@ -0,0 +1,23 @@ +CONFIG_KUNIT=y + +# These kconfig options select all the CONFIG_CRYPTO_LIB_* symbols that have a +# corresponding KUnit test. Those symbols cannot be directly enabled here, +# since they are hidden symbols. +CONFIG_CRYPTO=y +CONFIG_CRYPTO_MD5=y +CONFIG_CRYPTO_SHA1=y +CONFIG_CRYPTO_SHA256=y +CONFIG_CRYPTO_SHA512=y +CONFIG_INET=y +CONFIG_IPV6=y +CONFIG_NET=y +CONFIG_NETDEVICES=y +CONFIG_WIREGUARD=y + +CONFIG_CRYPTO_LIB_BLAKE2S_KUNIT_TEST=y +CONFIG_CRYPTO_LIB_CURVE25519_KUNIT_TEST=y +CONFIG_CRYPTO_LIB_MD5_KUNIT_TEST=y +CONFIG_CRYPTO_LIB_POLY1305_KUNIT_TEST=y +CONFIG_CRYPTO_LIB_SHA1_KUNIT_TEST=y +CONFIG_CRYPTO_LIB_SHA256_KUNIT_TEST=y +CONFIG_CRYPTO_LIB_SHA512_KUNIT_TEST=y
Signature · 1 line
2.53.0
PATCH 6/8 lib/crypto: tests: Introduce CRYPTO_LIB_ENABLE_ALL_FOR_KUNIT
In reply to Eric Biggerscommit ed1767442d919f57aaf83d69c33853da2644d902 upstream.
For kunit.py to run all the crypto library tests when passed the --alltests option, tools/testing/kunit/configs/all_tests.config needs to enable options that satisfy the test dependencies.
This is the same as what lib/crypto/.kunitconfig already does. However, the strategy that lib/crypto/.kunitconfig currently uses to select all the hidden library options isn't going to scale up well when it needs to be repeated in two places.
Instead let's go ahead and introduce an option CRYPTO_LIB_ENABLE_ALL_FOR_KUNIT that depends on KUNIT and selects all the crypto library options that have corresponding KUnit tests.
Update lib/crypto/.kunitconfig to use this option.
--- lib/crypto/.kunitconfig | 14 +------------- lib/crypto/tests/Kconfig | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 13 deletions(-)
lib/crypto/.kunitconfig +1 -13
@@ -1,20 +1,8 @@ CONFIG_KUNIT=y -# These kconfig options select all the CONFIG_CRYPTO_LIB_* symbols that have a -# corresponding KUnit test. Those symbols cannot be directly enabled here, -# since they are hidden symbols. -CONFIG_CRYPTO=y -CONFIG_CRYPTO_MD5=y -CONFIG_CRYPTO_SHA1=y -CONFIG_CRYPTO_SHA256=y -CONFIG_CRYPTO_SHA512=y -CONFIG_INET=y -CONFIG_IPV6=y -CONFIG_NET=y -CONFIG_NETDEVICES=y -CONFIG_WIREGUARD=y +CONFIG_CRYPTO_LIB_ENABLE_ALL_FOR_KUNIT=y CONFIG_CRYPTO_LIB_BLAKE2S_KUNIT_TEST=y CONFIG_CRYPTO_LIB_CURVE25519_KUNIT_TEST=y CONFIG_CRYPTO_LIB_MD5_KUNIT_TEST=y CONFIG_CRYPTO_LIB_POLY1305_KUNIT_TEST=y
lib/crypto/tests/Kconfig +18 -0
@@ -64,10 +64,28 @@ config CRYPTO_LIB_SHA512_KUNIT_TEST select CRYPTO_LIB_BENCHMARK_VISIBLE help KUnit tests for the SHA-384 and SHA-512 cryptographic hash functions and their corresponding HMACs. +config CRYPTO_LIB_ENABLE_ALL_FOR_KUNIT + tristate "Enable all crypto library code for KUnit tests" + depends on KUNIT + select CRYPTO_LIB_CURVE25519 + select CRYPTO_LIB_MD5 + select CRYPTO_LIB_POLY1305 + select CRYPTO_LIB_SHA1 + select CRYPTO_LIB_SHA256 + select CRYPTO_LIB_SHA512 + help + Enable all the crypto library code that has KUnit tests. + + Enable this only if you'd like to test all the crypto library code, + even code that wouldn't otherwise need to be built. + + You'll still need to enable the tests themselves, either individually + or using KUNIT_ALL_TESTS. + config CRYPTO_LIB_BENCHMARK_VISIBLE bool config CRYPTO_LIB_BENCHMARK bool "Include benchmarks in KUnit tests for cryptographic functions"
Signature · 1 line
2.53.0
PATCH 7/8 kunit: configs: Enable all crypto library tests in all_tests.config
In reply to Eric Biggerscommit 8d547482231fef30d0d6440629b73560ad3e937c upstream.
The new option CONFIG_CRYPTO_LIB_ENABLE_ALL_FOR_KUNIT enables all the crypto library code that has KUnit tests, causing CONFIG_KUNIT_ALL_TESTS to enable all these tests. Add this option to all_tests.config so that kunit.py will run them when passed the --alltests option.
--- tools/testing/kunit/configs/all_tests.config | 2 ++ 1 file changed, 2 insertions(+)
tools/testing/kunit/configs/all_tests.config +2 -0
@@ -42,10 +42,12 @@ CONFIG_DAMON_PADDR=y CONFIG_REGMAP_BUILD=y CONFIG_AUDIT=y +CONFIG_CRYPTO_LIB_ENABLE_ALL_FOR_KUNIT=y + CONFIG_PRIME_NUMBERS=y CONFIG_CRC_ENABLE_ALL_FOR_KUNIT=y CONFIG_SECURITY=y
Signature · 1 line
2.53.0
PATCH 8/8 lib/crypto: tests: Drop the default to CRYPTO_SELFTESTS
In reply to Eric Biggerscommit 6d80749becf8fc5ffa004194e578f79b558235ef upstream.
Defaulting the crypto KUnit tests to KUNIT_ALL_TESTS || CRYPTO_SELFTESTS instead of simply KUNIT_ALL_TESTS was originally intended to make it easy to enable all the crypto KUnit tests. This additional default is nonstandard for KUnit tests, though, and it can cause all the KUnit tests to be built-in unexpectedly if CRYPTO_SELFTESTS is set. It also constitutes a back-reference to crypto/ from lib/crypto/, which is something that we should be avoiding in order to get clean layering.
Now that we provide a lib/crypto/.kunitconfig file that enables all crypto KUnit tests, let's consider that to be the supported way to enable all these tests, and drop the default of CRYPTO_SELFTESTS.
--- lib/crypto/tests/Kconfig | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
lib/crypto/tests/Kconfig +7 -7
@@ -1,68 +1,68 @@ # SPDX-License-Identifier: GPL-2.0-or-later config CRYPTO_LIB_BLAKE2S_KUNIT_TEST tristate "KUnit tests for BLAKE2s" if !KUNIT_ALL_TESTS depends on KUNIT - default KUNIT_ALL_TESTS || CRYPTO_SELFTESTS + default KUNIT_ALL_TESTS select CRYPTO_LIB_BENCHMARK_VISIBLE # No need to depend on CRYPTO_LIB_BLAKE2S here, as that option doesn't # exist; the BLAKE2s code is always built-in for the /dev/random driver. help KUnit tests for the BLAKE2s cryptographic hash function. config CRYPTO_LIB_CURVE25519_KUNIT_TEST tristate "KUnit tests for Curve25519" if !KUNIT_ALL_TESTS depends on KUNIT && CRYPTO_LIB_CURVE25519 - default KUNIT_ALL_TESTS || CRYPTO_SELFTESTS + default KUNIT_ALL_TESTS select CRYPTO_LIB_BENCHMARK_VISIBLE help KUnit tests for the Curve25519 Diffie-Hellman function. config CRYPTO_LIB_MD5_KUNIT_TEST tristate "KUnit tests for MD5" if !KUNIT_ALL_TESTS depends on KUNIT && CRYPTO_LIB_MD5 - default KUNIT_ALL_TESTS || CRYPTO_SELFTESTS + default KUNIT_ALL_TESTS select CRYPTO_LIB_BENCHMARK_VISIBLE help KUnit tests for the MD5 cryptographic hash function and its corresponding HMAC. config CRYPTO_LIB_POLY1305_KUNIT_TEST tristate "KUnit tests for Poly1305" if !KUNIT_ALL_TESTS depends on KUNIT && CRYPTO_LIB_POLY1305 - default KUNIT_ALL_TESTS || CRYPTO_SELFTESTS + default KUNIT_ALL_TESTS select CRYPTO_LIB_BENCHMARK_VISIBLE help KUnit tests for the Poly1305 library functions. config CRYPTO_LIB_SHA1_KUNIT_TEST tristate "KUnit tests for SHA-1" if !KUNIT_ALL_TESTS depends on KUNIT && CRYPTO_LIB_SHA1 - default KUNIT_ALL_TESTS || CRYPTO_SELFTESTS + default KUNIT_ALL_TESTS select CRYPTO_LIB_BENCHMARK_VISIBLE help KUnit tests for the SHA-1 cryptographic hash function and its corresponding HMAC. # Option is named *_SHA256_KUNIT_TEST, though both SHA-224 and SHA-256 tests are # included, for consistency with the naming used elsewhere (e.g. CRYPTO_SHA256). config CRYPTO_LIB_SHA256_KUNIT_TEST tristate "KUnit tests for SHA-224 and SHA-256" if !KUNIT_ALL_TESTS depends on KUNIT && CRYPTO_LIB_SHA256 - default KUNIT_ALL_TESTS || CRYPTO_SELFTESTS + default KUNIT_ALL_TESTS select CRYPTO_LIB_BENCHMARK_VISIBLE help KUnit tests for the SHA-224 and SHA-256 cryptographic hash functions and their corresponding HMACs. # Option is named *_SHA512_KUNIT_TEST, though both SHA-384 and SHA-512 tests are # included, for consistency with the naming used elsewhere (e.g. CRYPTO_SHA512). config CRYPTO_LIB_SHA512_KUNIT_TEST tristate "KUnit tests for SHA-384 and SHA-512" if !KUNIT_ALL_TESTS depends on KUNIT && CRYPTO_LIB_SHA512 - default KUNIT_ALL_TESTS || CRYPTO_SELFTESTS + default KUNIT_ALL_TESTS select CRYPTO_LIB_BENCHMARK_VISIBLE help KUnit tests for the SHA-384 and SHA-512 cryptographic hash functions and their corresponding HMACs.
Signature · 1 line
2.53.0