lib/list-test: add a test for the 'list' doubly linked list
Subsystems (2)
PATCH v4 1/1 lib/list-test: add a test for the 'list' doubly linked list
Add a KUnit test for the kernel doubly linked list implementation in include/linux/list.h
Each test case (list_test_x) is focused on testing the behaviour of the list function/macro 'x'. None of the tests pass invalid lists to these macros, and so should behave identically with DEBUG_LIST enabled and disabled.
Note that, at present, it only tests the list_ types (not the singly-linked hlist_), and does not yet test all of the list_for_each_entry* macros (and some related things like list_prepare_entry).
---
The changes from v3 are mostly to do with naming:
- The Kconfig entry has been renamed from LIST_TEST to LIST_KUNIT_TEST, which matches the SYSCTL_KUNIT_TEST entry,
- The Kconfig description was updated to better match other KUnit tests, specifying that the test is not intended for use in a production kernel. A now-redundant mention of the test running a boot was removed.
- The MAINTAINERS entry refers to a "KUNIT TEST" rather than a "UNIT TEST"
- The module name has changed from "list-test" to "list-kunit-test".
Earlier versions of the test can be found:
v3:
https://lore.kernel.org/linux-kselftest/[email protected]/
v2:
https://lore.kernel.org/linux-kselftest/[email protected]/
v1:
https://lore.kernel.org/linux-kselftest/[email protected]/
MAINTAINERS | 5 + lib/Kconfig.debug | 18 ++ lib/Makefile | 3 + lib/list-test.c | 740 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 766 insertions(+) create mode 100644 lib/list-test.c
MAINTAINERS +5 -0
@@ -9504,6 +9504,11 @@ F: Documentation/misc-devices/lis3lv02d.rst F: drivers/misc/lis3lv02d/ F: drivers/platform/x86/hp_accel.c +LIST KUNIT TEST +M: David Gow <[email protected]> +S: Maintained +F: lib/list-test.c + LIVE PATCHING M: Josh Poimboeuf <[email protected]> M: Jiri Kosina <[email protected]>
lib/Kconfig.debug +18 -0
@@ -1961,6 +1961,24 @@ config SYSCTL_KUNIT_TEST If unsure, say N. +config LIST_KUNIT_TEST + bool "KUnit Test for Kernel Linked-list structures" + depends on KUNIT + help + This builds the linked list KUnit test suite. + It tests that the API and basic functionality of the list_head type + and associated macros. + + 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 TEST_UDELAY tristate "udelay test driver" help
lib/Makefile +3 -0
@@ -292,3 +292,6 @@ obj-$(CONFIG_GENERIC_LIB_MULDI3) += muldi3.o obj-$(CONFIG_GENERIC_LIB_CMPDI2) += cmpdi2.o obj-$(CONFIG_GENERIC_LIB_UCMPDI2) += ucmpdi2.o obj-$(CONFIG_OBJAGG) += objagg.o + +# KUnit tests +obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o
lib/list-test.c new file +740 -0
740 lines not on this page · raw message
Signature · 1 line
2.23.0.866.gb869b98d4c-goog
Quoted 123 lines · David Gow
Add a KUnit test for the kernel doubly linked list implementation in
include/linux/list.hEach test case (
list_test_x) is focused on testing the behaviour of the list function/macro 'x'. None of the tests pass invalid lists to these macros, and so should behave identically withDEBUG_LISTenabled and disabled.Note that, at present, it only tests the
list_types (not the singly-linkedhlist_), and does not yet test all of the list_for_each_entry* macros (and some related things likelist_prepare_entry).Signed-off-by David Gow <[email protected]>---
The changes from v3 are mostly to do with naming:
- TheKconfigentry has been renamed fromLIST_TESTtoLIST_KUNIT_TEST, which matches theSYSCTL_KUNIT_TESTentry,
- TheKconfigdescription was updated to better match other KUnit tests, specifying that the test is not intended for use in a production kernel. A now-redundant mention of the test running a boot was removed.
- TheMAINTAINERSentry refers to a "KUNIT TEST" rather than a "UNIT TEST"
- The module name has changed from "list-test" to "list-kunit-test".Earlier versions of the test can be found:
v3:
https://lore.kernel.org/linux-kselftest/[email protected]/
v2:
https://lore.kernel.org/linux-kselftest/[email protected]/
v1:
https://lore.kernel.org/linux-kselftest/[email protected]/MAINTAINERS | 5 + lib/Kconfig.debug | 18 ++ lib/Makefile | 3 + lib/list-test.c | 740 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 766 insertions(+) create mode 100644 lib/list-test.cMAINTAINERS +5
-0@@ -9504,6 +9504,11 @@ F: Documentation/misc-devices/lis3lv02d.rst F: drivers/misc/lis3lv02d/ F: drivers/platform/x86/hp_accel.c +LIST KUNIT TEST +M: David Gow <[email protected]> +S: Maintained +F: lib/list-test.c + LIVE PATCHING M: Josh Poimboeuf <[email protected]> M: Jiri Kosina <[email protected]>lib/Kconfig.debug +18
-0@@ -1961,6 +1961,24 @@ config SYSCTL_KUNIT_TEST If unsure, say N. +config LIST_KUNIT_TEST + bool "KUnit Test for Kernel Linked-list structures" + depends on KUNIT + help + This builds the linked list KUnit test suite. + It tests that the API and basic functionality of the list_head type + and associated macros. + + 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 TEST_UDELAY tristate "udelay test driver" helplib/Makefile +3
-0@@ -292,3 +292,6 @@ obj-$(CONFIG_GENERIC_LIB_MULDI3) += muldi3.o obj-$(CONFIG_GENERIC_LIB_CMPDI2) += cmpdi2.o obj-$(CONFIG_GENERIC_LIB_UCMPDI2) += ucmpdi2.o obj-$(CONFIG_OBJAGG) += objagg.o + +# KUnit tests +obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.olib/list-test.c new file +19
-0@@ -0,0 +1,740 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* KUnit test for the Kernel Linked-list structures. +* +* Copyright (C) 2019, Google LLC. +* Author: David Gow <[email protected]> +*/ +#include <kunit/test.h> + +#include <linux/list.h> + +struct list_test_struct { +int data; +struct list_head list; +}; + +static void list_test_list_init (struct kunit *test) +{ +/* Test the different ways of initialising a list. */
lib/list-test.c line 29
Quoted 10 lines
lib/list-test.c new file +10
-0+struct list_head list1 =LIST_HEAD_INIT (list1); +struct list_head list2; +LIST_HEAD (list3); +struct list_head *list4; +struct list_head *list5; + +INIT_LIST_HEAD (&list2); + + list4 =kzalloc (sizeof (*list4),GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, list4);
Why not just use GFP_KERNEL | GFP_NOFAIL and remove the check?
kzalloc() can't return error pointers. If this were an IS_ERR_OR_NULL() check then it would generate a static checker warning, but static checkers don't know about KUNIT_ASSERT_NOT_ERR_OR_NULL() yet so you're safe.
But generally NULL is a special case of success. A common situation is where the user deliberately disables a feature, that means it's not an error but we also don't have a valid pointer to return because it's disabled.
regards,
dan carpenter
Quoted 6 lines · Dan Carpenter
lib/list-test.c line 29
Quoted 2 lines · David Gow
lib/list-test.c new file +2
-0+ list4 =kzalloc (sizeof (*list4),GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, list4);Why not just use
GFP_KERNEL|GFP_NOFAILand remove the check?
I've sent a new version of the patch out (v5) which uses __GFP_NOFAIL instead.
The idea had been to exercise KUnit's assertion functionality, in the hope that it'd allow the test to fail (but potentially allow other tests to still run) in the case of allocation failure. Given that we're only allocating enough to store ~4 pointers in total, though, that's probably of little use.
Quoted 4 lines
kzalloc()can't return error pointers. If this were anIS_ERR_OR_NULL()check then it would generate a static checker warning, but static checkers don't know aboutKUNIT_ASSERT_NOT_ERR_OR_NULL()yet so you're safe.
Alas, KUnit doesn't have a KUNIT_ASSERT_NOT_NULL() macro, and I'd assumed it was not dangerous (even if not ideal) to check for error pointers, even if kzalloc() can't return them.
Perhaps it'd make sense to add a convenient way of checking the NULL-ness of pointers to KUnit (it's possible with the KUNIT_ASSERT_PTR_EQ(), but requires a bit of casting to make the type checker happy) in the future. Once KUnit is properly upstream, it may be worth teaching the static analysis tools about these functions to avoid having warnings in these sorts of tests.
For now, though, (and for this test in particular), I agree with the suggestion of just using __GFP_NOFAIL.
Thanks a lot for the comments,
-- David
Quoted 17 lines · David Gow
Quoted 6 lines · Dan Carpenter
lib/list-test.c line 29
Quoted 2 lines · David Gow
lib/list-test.c new file +2
-0+ list4 =kzalloc (sizeof (*list4),GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, list4);Why not just use
GFP_KERNEL|GFP_NOFAILand remove the check?I've sent a new version of the patch out (v5) which uses
__GFP_NOFAILinstead.The idea had been to exercise KUnit's assertion functionality, in the hope that it'd allow the test to fail (but potentially allow other tests to still run) in the case of allocation failure. Given that we're only allocating enough to store ~4 pointers in total, though, that's probably of little use.
Quoted 8 lines
Quoted 4 lines
kzalloc()can't return error pointers. If this were anIS_ERR_OR_NULL()check then it would generate a static checker warning, but static checkers don't know aboutKUNIT_ASSERT_NOT_ERR_OR_NULL()yet so you're safe.Alas, KUnit doesn't have a
KUNIT_ASSERT_NOT_NULL()macro, and I'd assumed it was not dangerous (even if not ideal) to check for error pointers, even ifkzalloc()can't return them.
Maybe it would be good for us (not in this case, just generally speaking) to add a KUNIT_ASSERT_NOT_NULL() and friends?
Quoted 12 lines
Perhaps it'd make sense to add a convenient way of checking the NULL-ness of pointers to KUnit (it's possible with the
KUNIT_ASSERT_PTR_EQ(), but requires a bit of casting to make the type checker happy) in the future. Once KUnit is properly upstream, it may be worth teaching the static analysis tools about these functions to avoid having warnings in these sorts of tests.For now, though, (and for this test in particular), I agree with the suggestion of just using
__GFP_NOFAIL.Thanks a lot for the comments,
-- David