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/20201015014616.309000-1-vitor@massaru.org/
[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`.

Signed-off-by: Daniel Latypov <dlatypov@google.com>
Tested-by: David Gow <davidgow@google.com>
Acked-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
This commit is contained in:
Daniel Latypov 2021-05-03 13:58:35 -07:00 committed by Shuah Khan
parent 7122debb43
commit ebd09577be
2 changed files with 55 additions and 81 deletions

View File

@ -2047,8 +2047,9 @@ config LKDTM
Documentation/fault-injection/provoke-crashes.rst Documentation/fault-injection/provoke-crashes.rst
config TEST_LIST_SORT config TEST_LIST_SORT
tristate "Linked list sorting test" tristate "Linked list sorting test" if !KUNIT_ALL_TESTS
depends on DEBUG_KERNEL || m depends on KUNIT
default KUNIT_ALL_TESTS
help help
Enable this to turn on 'list_sort()' function test. This test is Enable this to turn on 'list_sort()' function test. This test is
executed only once during system boot (so affects only boot time), executed only once during system boot (so affects only boot time),

View File

@ -1,5 +1,5 @@
// SPDX-License-Identifier: GPL-2.0-only // SPDX-License-Identifier: GPL-2.0-only
#define pr_fmt(fmt) "list_sort_test: " fmt #include <kunit/test.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/list_sort.h> #include <linux/list_sort.h>
@ -23,68 +23,52 @@ struct debug_el {
struct list_head list; struct list_head list;
unsigned int poison2; unsigned int poison2;
int value; int value;
unsigned serial; unsigned int serial;
}; };
/* Array, containing pointers to all elements in the test list */ static void check(struct kunit *test, struct debug_el *ela, struct debug_el *elb)
static struct debug_el **elts __initdata;
static int __init check(struct debug_el *ela, struct debug_el *elb)
{ {
if (ela->serial >= TEST_LIST_LEN) { struct debug_el **elts = test->priv;
pr_err("error: incorrect serial %d\n", ela->serial);
return -EINVAL; KUNIT_EXPECT_LT_MSG(test, ela->serial, (unsigned int)TEST_LIST_LEN, "incorrect serial");
} KUNIT_EXPECT_LT_MSG(test, elb->serial, (unsigned int)TEST_LIST_LEN, "incorrect serial");
if (elb->serial >= TEST_LIST_LEN) {
pr_err("error: incorrect serial %d\n", elb->serial); KUNIT_EXPECT_PTR_EQ_MSG(test, elts[ela->serial], ela, "phantom element");
return -EINVAL; KUNIT_EXPECT_PTR_EQ_MSG(test, elts[elb->serial], elb, "phantom element");
}
if (elts[ela->serial] != ela || elts[elb->serial] != elb) { KUNIT_EXPECT_EQ_MSG(test, ela->poison1, TEST_POISON1, "bad poison");
pr_err("error: phantom element\n"); KUNIT_EXPECT_EQ_MSG(test, ela->poison2, TEST_POISON2, "bad poison");
return -EINVAL;
} KUNIT_EXPECT_EQ_MSG(test, elb->poison1, TEST_POISON1, "bad poison");
if (ela->poison1 != TEST_POISON1 || ela->poison2 != TEST_POISON2) { KUNIT_EXPECT_EQ_MSG(test, elb->poison2, TEST_POISON2, "bad poison");
pr_err("error: bad poison: %#x/%#x\n",
ela->poison1, ela->poison2);
return -EINVAL;
}
if (elb->poison1 != TEST_POISON1 || elb->poison2 != TEST_POISON2) {
pr_err("error: bad poison: %#x/%#x\n",
elb->poison1, elb->poison2);
return -EINVAL;
}
return 0;
} }
static int __init cmp(void *priv, const struct list_head *a, /* `priv` is the test pointer so check() can fail the test if the list is invalid. */
const struct list_head *b) static int cmp(void *priv, const struct list_head *a, const struct list_head *b)
{ {
struct debug_el *ela, *elb; struct debug_el *ela, *elb;
ela = container_of(a, struct debug_el, list); ela = container_of(a, struct debug_el, list);
elb = container_of(b, struct debug_el, list); elb = container_of(b, struct debug_el, list);
check(ela, elb); check(priv, ela, elb);
return ela->value - elb->value; return ela->value - elb->value;
} }
static int __init list_sort_test(void) static void list_sort_test(struct kunit *test)
{ {
int i, count = 1, err = -ENOMEM; int i, count = 1;
struct debug_el *el; struct debug_el *el, **elts;
struct list_head *cur; struct list_head *cur;
LIST_HEAD(head); LIST_HEAD(head);
pr_debug("start testing list_sort()\n"); elts = kunit_kcalloc(test, TEST_LIST_LEN, sizeof(*elts), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, elts);
elts = kcalloc(TEST_LIST_LEN, sizeof(*elts), GFP_KERNEL); test->priv = elts;
if (!elts)
return err;
for (i = 0; i < TEST_LIST_LEN; i++) { for (i = 0; i < TEST_LIST_LEN; i++) {
el = kmalloc(sizeof(*el), GFP_KERNEL); el = kunit_kmalloc(test, sizeof(*el), GFP_KERNEL);
if (!el) KUNIT_ASSERT_NOT_ERR_OR_NULL(test, el);
goto exit;
/* force some equivalencies */ /* force some equivalencies */
el->value = prandom_u32() % (TEST_LIST_LEN / 3); el->value = prandom_u32() % (TEST_LIST_LEN / 3);
@ -95,55 +79,44 @@ static int __init list_sort_test(void)
list_add_tail(&el->list, &head); list_add_tail(&el->list, &head);
} }
list_sort(NULL, &head, cmp); list_sort(test, &head, cmp);
err = -EINVAL;
for (cur = head.next; cur->next != &head; cur = cur->next) { for (cur = head.next; cur->next != &head; cur = cur->next) {
struct debug_el *el1; struct debug_el *el1;
int cmp_result; int cmp_result;
if (cur->next->prev != cur) { KUNIT_ASSERT_PTR_EQ_MSG(test, cur->next->prev, cur,
pr_err("error: list is corrupted\n"); "list is corrupted");
goto exit;
}
cmp_result = cmp(NULL, cur, cur->next); cmp_result = cmp(test, cur, cur->next);
if (cmp_result > 0) { KUNIT_ASSERT_LE_MSG(test, cmp_result, 0, "list is not sorted");
pr_err("error: list is not sorted\n");
goto exit;
}
el = container_of(cur, struct debug_el, list); el = container_of(cur, struct debug_el, list);
el1 = container_of(cur->next, struct debug_el, list); el1 = container_of(cur->next, struct debug_el, list);
if (cmp_result == 0 && el->serial >= el1->serial) { if (cmp_result == 0) {
pr_err("error: order of equivalent elements not " KUNIT_ASSERT_LE_MSG(test, el->serial, el1->serial,
"preserved\n"); "order of equivalent elements not preserved");
goto exit;
} }
if (check(el, el1)) { check(test, el, el1);
pr_err("error: element check failed\n");
goto exit;
}
count++; count++;
} }
if (head.prev != cur) { KUNIT_EXPECT_PTR_EQ_MSG(test, head.prev, cur, "list is corrupted");
pr_err("error: list is corrupted\n");
goto exit;
}
KUNIT_EXPECT_EQ_MSG(test, count, TEST_LIST_LEN,
if (count != TEST_LIST_LEN) { "list length changed after sorting!");
pr_err("error: bad list length %d", count);
goto exit;
}
err = 0;
exit:
for (i = 0; i < TEST_LIST_LEN; i++)
kfree(elts[i]);
kfree(elts);
return err;
} }
module_init(list_sort_test);
static struct kunit_case list_sort_cases[] = {
KUNIT_CASE(list_sort_test),
{}
};
static struct kunit_suite list_sort_suite = {
.name = "list_sort",
.test_cases = list_sort_cases,
};
kunit_test_suites(&list_sort_suite);
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL");