@@ -2000,9 +2000,6 @@ config TEST_UUID
config TEST_XARRAY
tristate "Test the XArray code at runtime"
-config TEST_OVERFLOW
- tristate "Test check_*_overflow() functions at runtime"
-
config TEST_RHASHTABLE
tristate "Perform selftest on resizable hash table"
help
@@ -2155,6 +2152,23 @@ config SYSCTL_KUNIT_TEST
If unsure, say N.
+config OVERFLOW_KUNIT_TEST
+ tristate "KUnit test for overflow" if !KUNIT_ALL_TESTS
+ depends on KUNIT
+ default KUNIT_ALL_TESTS
+ help
+ This builds the overflow KUnit tests.
+
+ 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 KUnit test harness and are not 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 LIST_KUNIT_TEST
tristate "KUnit Test for Kernel Linked-list structures" if !KUNIT_ALL_TESTS
depends on KUNIT
@@ -75,7 +75,6 @@ obj-$(CONFIG_TEST_LIST_SORT) += test_list_sort.o
obj-$(CONFIG_TEST_MIN_HEAP) += test_min_heap.o
obj-$(CONFIG_TEST_LKM) += test_module.o
obj-$(CONFIG_TEST_VMALLOC) += test_vmalloc.o
-obj-$(CONFIG_TEST_OVERFLOW) += test_overflow.o
obj-$(CONFIG_TEST_RHASHTABLE) += test_rhashtable.o
obj-$(CONFIG_TEST_SORT) += test_sort.o
obj-$(CONFIG_TEST_USER_COPY) += test_user_copy.o
@@ -318,3 +317,4 @@ obj-$(CONFIG_OBJAGG) += objagg.o
# KUnit tests
obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o
obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o
+obj-$(CONFIG_OVERFLOW_KUNIT_TEST) += overflow-test.o
similarity index 96%
rename from lib/test_overflow.c
rename to lib/overflow-test.c
@@ -4,14 +4,11 @@
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+#include <kunit/test.h>
#include <linux/device.h>
#include <linux/init.h>
-#include <linux/kernel.h>
#include <linux/mm.h>
-#include <linux/module.h>
#include <linux/overflow.h>
-#include <linux/slab.h>
-#include <linux/types.h>
#include <linux/vmalloc.h>
#define DEFINE_TEST_ARRAY(t) \
@@ -270,7 +267,7 @@ DEFINE_TEST_FUNC(u64, "%llu");
DEFINE_TEST_FUNC(s64, "%lld");
#endif
-static int __init test_overflow_calculation(void)
+static void __init overflow_calculation_test(struct kunit *test)
{
int err = 0;
@@ -285,10 +282,10 @@ static int __init test_overflow_calculation(void)
err |= test_s64_overflow();
#endif
- return err;
+ KUNIT_EXPECT_FALSE(test, err);
}
-static int __init test_overflow_shift(void)
+static void __init overflow_shift_test(struct kunit *test)
{
int err = 0;
@@ -479,7 +476,7 @@ static int __init test_overflow_shift(void)
err |= TEST_ONE_SHIFT(0, 31, s32, 0, false);
err |= TEST_ONE_SHIFT(0, 63, s64, 0, false);
- return err;
+ KUNIT_EXPECT_FALSE(test, err);
}
/*
@@ -555,7 +552,7 @@ DEFINE_TEST_ALLOC(kvzalloc_node, kvfree, 0, 1, 1);
DEFINE_TEST_ALLOC(devm_kmalloc, devm_kfree, 1, 1, 0);
DEFINE_TEST_ALLOC(devm_kzalloc, devm_kfree, 1, 1, 0);
-static int __init test_overflow_allocation(void)
+static void __init overflow_allocation_test(struct kunit *test)
{
const char device_name[] = "overflow-test";
struct device *dev;
@@ -563,10 +560,8 @@ static int __init test_overflow_allocation(void)
/* Create dummy device for devm_kmalloc()-family tests. */
dev = root_device_register(device_name);
- if (IS_ERR(dev)) {
- pr_warn("Cannot register test device\n");
- return 1;
- }
+ if (IS_ERR(dev))
+ kunit_warn(test, "Cannot register test device\n");
err |= test_kmalloc(NULL);
err |= test_kmalloc_node(NULL);
@@ -585,30 +580,21 @@ static int __init test_overflow_allocation(void)
device_unregister(dev);
- return err;
+ KUNIT_EXPECT_FALSE(test, err);
}
-static int __init test_module_init(void)
-{
- int err = 0;
-
- err |= test_overflow_calculation();
- err |= test_overflow_shift();
- err |= test_overflow_allocation();
-
- if (err) {
- pr_warn("FAIL!\n");
- err = -EINVAL;
- } else {
- pr_info("all tests passed\n");
- }
+static struct kunit_case __refdata overflow_test_cases[] = {
+ KUNIT_CASE(overflow_calculation_test),
+ KUNIT_CASE(overflow_shift_test),
+ KUNIT_CASE(overflow_allocation_test),
+ {}
+};
- return err;
-}
+static struct kunit_suite overflow_test_suite = {
+ .name = "overflow",
+ .test_cases = overflow_test_cases,
+};
-static void __exit test_module_exit(void)
-{ }
+kunit_test_suites(&overflow_test_suite);
-module_init(test_module_init);
-module_exit(test_module_exit);
MODULE_LICENSE("Dual MIT/GPL");