kunit: introduce kunit_kmalloc_array/kunit_kcalloc() helpers
Subsystems (3)
PATCH v2 1/2 kunit: introduce kunit_kmalloc_array/kunit_kcalloc() helpers
Add in:
* kunit_kmalloc_array() and wire up kunit_kmalloc() to be a special case of it.
* kunit_kcalloc() for symmetry with kunit_kzalloc()
This should using KUnit more natural by making it more similar to the existing *alloc() APIs.
And while we shouldn't necessarily be writing unit tests where overflow should be a concern, it can't hurt to be safe.
---
v1 -> v2: s/kzalloc/kcalloc in doc comment.
--- include/kunit/test.h | 36 ++++++++++++++++++++++++++++++++---- lib/kunit/test.c | 22 ++++++++++++---------- 2 files changed, 44 insertions(+), 14 deletions(-)
include/kunit/test.h +32 -4
@@ -577,16 +577,30 @@ static inline int kunit_destroy_named_resource(struct kunit *test,void kunit_remove_resource (struct kunit *test,struct kunit_resource *res);/** -* kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*. +* kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*. * @test: The test context object. +* @n: number of elements. * @size: The size in bytes of the desired memory. * @gfp: flags passed to underlying kmalloc(). * -* Just like `kmalloc(...)`, except the allocation is managed by the test case +* Just like `kmalloc_array(...)`, except the allocation is managed by the test case * and is automatically cleaned up after the test case concludes. See &struct * kunit_resource for more information. */ -void *kunit_kmalloc (struct kunit *test,size_t size,gfp_t gfp); +void *kunit_kmalloc_array (struct kunit *test,size_t n,size_t size,gfp_t flags); + +/** +* kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*. +* @test: The test context object. +* @size: The size in bytes of the desired memory. +* @gfp: flags passed to underlying kmalloc(). +* +* See kmalloc() and kunit_kmalloc_array() for more information. +*/ +static inline void *kunit_kmalloc (struct kunit *test,size_t size,gfp_t gfp) +{ +return kunit_kmalloc_array (test,1 , size, gfp); +}/** * kunit_kfree() - Like kfree except for allocations managed by KUnit.
@@ -601,13 +615,27 @@ void kunit_kfree(struct kunit *test, const void *ptr);* @size: The size in bytes of the desired memory. * @gfp: flags passed to underlying kmalloc(). * -* See kzalloc() and kunit_kmalloc() for more information. +* See kzalloc() and kunit_kmalloc_array() for more information. */ static inline void *kunit_kzalloc (struct kunit *test,size_t size,gfp_t gfp) {return kunit_kmalloc (test, size, gfp | __GFP_ZERO); } +/** +* kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation. +* @test: The test context object. +* @n: number of elements. +* @size: The size in bytes of the desired memory. +* @gfp: flags passed to underlying kmalloc(). +* +* See kcalloc() and kunit_kmalloc_array() for more information. +*/ +static inline void *kunit_kcalloc (struct kunit *test,size_t n,size_t size,gfp_t flags) +{ +return kunit_kmalloc_array (test, n, size, flags | __GFP_ZERO); +} +void kunit_cleanup (struct kunit *test);void kunit_log_append (char *log,const char *fmt, ...);
lib/kunit/test.c +12 -10
@@ -573,41 +573,43 @@ int kunit_destroy_resource(struct kunit *test, kunit_resource_match_t match, }EXPORT_SYMBOL_GPL (kunit_destroy_resource); -struct kunit_kmalloc_params { +struct kunit_kmalloc_array_params { +size_t n;size_t size;gfp_t gfp; }; -static int kunit_kmalloc_init (struct kunit_resource *res,void *context) +static int kunit_kmalloc_array_init (struct kunit_resource *res,void *context) { -struct kunit_kmalloc_params *params = context; +struct kunit_kmalloc_array_params *params = context; - res->data =kmalloc (params->size, params->gfp); + res->data =kmalloc_array (params->n, params->size, params->gfp);if (!res->data)return -ENOMEM ;return 0 ; } -static void kunit_kmalloc_free (struct kunit_resource *res) +static void kunit_kmalloc_array_free (struct kunit_resource *res) {kfree (res->data); } -void *kunit_kmalloc (struct kunit *test,size_t size,gfp_t gfp) +void *kunit_kmalloc_array (struct kunit *test,size_t n,size_t size,gfp_t gfp) { -struct kunit_kmalloc_params params = { +struct kunit_kmalloc_array_params params = { .size = size, + .n = n, .gfp = gfp };return kunit_alloc_resource (test, - kunit_kmalloc_init, - kunit_kmalloc_free, + kunit_kmalloc_array_init, + kunit_kmalloc_array_free, gfp, ¶ms); } -EXPORT_SYMBOL_GPL (kunit_kmalloc); +EXPORT_SYMBOL_GPL (kunit_kmalloc_array);void kunit_kfree (struct kunit *test,const void *ptr) {
base-commit: cda689f8708b6bef0b921c3a17fcdecbe959a079
Signature · 1 line
2.31.1.527.g47e6f16901-goog
PATCH v2 2/2 lib/test: convert lib/test_list_sort.c to use KUnit
Functionally, this just means that the test output will be slightly changed and it'll now depend on CONFIG_KUNIT=y/m.
It'll still run at boot time and can still be built as a loadable module.
There was a pre-existing patch to convert this test that I found later, here [1]. Compared to [1], this patch doesn't rename files and uses KUnit features more heavily (i.e. does more than converting pr_err() calls to KUNIT_FAIL()).
What this conversion gives us:
* a shorter test thanks to KUnit's macros
* a way to run this a bit more easily via kunit.py (and CONFIG_KUNIT_ALL_TESTS=y) [2]
* a structured way of reporting pass/fail
* uses kunit-managed allocations to avoid the risk of memory leaks
* more descriptive error messages:
* i.e. it prints out which fields are invalid, what the expected values are, etc.
What this conversion does not do:
* change the name of the file (and thus the name of the module)
* change the name of the config option
Leaving these as-is for now to minimize the impact to people wanting to run this test. IMO, that concern trumps following KUnit's style guide for both names, at least for now.
[1] https://lore.kernel.org/linux-kselftest/[email protected]/
[2] Can be run via
$ ./tools/testing/kunit/kunit.py run --kunitconfig /dev/stdin <<EOF CONFIG_KUNIT=y CONFIG_TEST_LIST_SORT=y EOF
[16:55:56] Configuring KUnit Kernel ...
[16:55:56] Building KUnit Kernel ...
[16:56:29] Starting KUnit Kernel ...
[16:56:32] ============================================================ [16:56:32] ======== [PASSED] list_sort ========
[16:56:32] [PASSED] list_sort_test
[16:56:32] ============================================================ [16:56:32] Testing complete. 1 tests run. 0 failed. 0 crashed.
[16:56:32] Elapsed time: 35.668s total, 0.001s configuring, 32.725s building, 0.000s running
Note: the build time is as after a make mrproper.
---
v1 -> v2: no change. Might make a v3 if we want to rename the file/config options to make it match.
--- lib/Kconfig.debug | 5 +- lib/test_list_sort.c | 129 +++++++++++++++++-------------------------- 2 files changed, 54 insertions(+), 80 deletions(-)
lib/Kconfig.debug +3 -2
@@ -2047,8 +2047,9 @@ config LKDTM Documentation/fault-injection/provoke-crashes.rst config TEST_LIST_SORT - tristate "Linked list sorting test" - depends on DEBUG_KERNEL || m + tristate "Linked list sorting test" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS help Enable this to turn on 'list_sort()' function test. This test is executed only once during system boot (so affects only boot time),
lib/test_list_sort.c +51 -78
179 lines not on this page · raw message
Signature · 1 line
2.31.1.527.g47e6f16901-goog