Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'linux_kselftest-kunit-6.17-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest

Pull kunit updates from Shuah Khan:
"Correct MODULE_IMPORT_NS() syntax documentation, make kunit_test
timeout configurable via a module parameter and a Kconfig option, fix
longest symbol length test, add a test for static stub, and adjust
kunit_test timeout based on test_{suite,case} speed"

* tag 'linux_kselftest-kunit-6.17-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest:
kunit: fix longest symbol length test
kunit: Make default kunit_test timeout configurable via both a module parameter and a Kconfig option
kunit: Adjust kunit_test timeout based on test_{suite,case} speed
kunit: Add test for static stub
Documentation: kunit: Correct MODULE_IMPORT_NS() syntax

+118 -37
+1 -1
Documentation/dev-tools/kunit/usage.rst
··· 699 699 #include <kunit/visibility.h> 700 700 #include <my_file.h> 701 701 ... 702 - MODULE_IMPORT_NS(EXPORTED_FOR_KUNIT_TESTING); 702 + MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING"); 703 703 ... 704 704 // Use do_interesting_thing() in tests 705 705
+1
include/kunit/try-catch.h
··· 47 47 int try_result; 48 48 kunit_try_catch_func_t try; 49 49 kunit_try_catch_func_t catch; 50 + unsigned long timeout; 50 51 void *context; 51 52 }; 52 53
+1
lib/Kconfig.debug
··· 2894 2894 config LONGEST_SYM_KUNIT_TEST 2895 2895 tristate "Test the longest symbol possible" if !KUNIT_ALL_TESTS 2896 2896 depends on KUNIT && KPROBES 2897 + depends on !PREFIX_SYMBOLS && !CFI_CLANG && !GCOV_KERNEL 2897 2898 default KUNIT_ALL_TESTS 2898 2899 help 2899 2900 Tests the longest symbol possible
+13
lib/kunit/Kconfig
··· 93 93 In most cases this should be left as Y. Only if additional opt-in 94 94 behavior is needed should this be set to N. 95 95 96 + config KUNIT_DEFAULT_TIMEOUT 97 + int "Default value of the timeout module parameter" 98 + default 300 99 + help 100 + Sets the default timeout, in seconds, for Kunit test cases. This value 101 + is further multiplied by a factor determined by the assigned speed 102 + setting: 1x for `DEFAULT`, 3x for `KUNIT_SPEED_SLOW`, and 12x for 103 + `KUNIT_SPEED_VERY_SLOW`. This allows slower tests on slower machines 104 + sufficient time to complete. 105 + 106 + If unsure, the default timeout of 300 seconds is suitable for most 107 + cases. 108 + 96 109 endif # KUNIT
+51 -4
lib/kunit/kunit-test.c
··· 8 8 #include "linux/gfp_types.h" 9 9 #include <kunit/test.h> 10 10 #include <kunit/test-bug.h> 11 + #include <kunit/static_stub.h> 11 12 12 13 #include <linux/device.h> 13 14 #include <kunit/device.h> ··· 44 43 kunit_try_catch_init(try_catch, 45 44 test, 46 45 kunit_test_successful_try, 47 - kunit_test_no_catch); 46 + kunit_test_no_catch, 47 + 300 * msecs_to_jiffies(MSEC_PER_SEC)); 48 48 kunit_try_catch_run(try_catch, test); 49 49 50 50 KUNIT_EXPECT_TRUE(test, ctx->function_called); ··· 77 75 kunit_try_catch_init(try_catch, 78 76 test, 79 77 kunit_test_unsuccessful_try, 80 - kunit_test_catch); 78 + kunit_test_catch, 79 + 300 * msecs_to_jiffies(MSEC_PER_SEC)); 81 80 kunit_try_catch_run(try_catch, test); 82 81 83 82 KUNIT_EXPECT_TRUE(test, ctx->function_called); ··· 132 129 kunit_try_catch_init(try_catch, 133 130 test, 134 131 kunit_test_null_dereference, 135 - kunit_test_catch); 132 + kunit_test_catch, 133 + 300 * msecs_to_jiffies(MSEC_PER_SEC)); 136 134 kunit_try_catch_run(try_catch, test); 137 135 138 136 KUNIT_EXPECT_EQ(test, try_catch->try_result, -EINTR); ··· 872 868 .test_cases = kunit_current_test_cases, 873 869 }; 874 870 871 + static void kunit_stub_test(struct kunit *test) 872 + { 873 + struct kunit fake_test; 874 + const unsigned long fake_real_fn_addr = 0x1234; 875 + const unsigned long fake_replacement_addr = 0x5678; 876 + struct kunit_resource *res; 877 + struct { 878 + void *real_fn_addr; 879 + void *replacement_addr; 880 + } *stub_ctx; 881 + 882 + kunit_init_test(&fake_test, "kunit_stub_fake_test", NULL); 883 + KUNIT_ASSERT_EQ(test, fake_test.status, KUNIT_SUCCESS); 884 + KUNIT_ASSERT_EQ(test, list_count_nodes(&fake_test.resources), 0); 885 + 886 + __kunit_activate_static_stub(&fake_test, (void *)fake_real_fn_addr, 887 + (void *)fake_replacement_addr); 888 + KUNIT_ASSERT_EQ(test, fake_test.status, KUNIT_SUCCESS); 889 + KUNIT_ASSERT_EQ(test, list_count_nodes(&fake_test.resources), 1); 890 + 891 + res = list_first_entry(&fake_test.resources, struct kunit_resource, node); 892 + KUNIT_EXPECT_NOT_NULL(test, res); 893 + 894 + stub_ctx = res->data; 895 + KUNIT_EXPECT_NOT_NULL(test, stub_ctx); 896 + KUNIT_EXPECT_EQ(test, (unsigned long)stub_ctx->real_fn_addr, fake_real_fn_addr); 897 + KUNIT_EXPECT_EQ(test, (unsigned long)stub_ctx->replacement_addr, fake_replacement_addr); 898 + 899 + __kunit_activate_static_stub(&fake_test, (void *)fake_real_fn_addr, NULL); 900 + KUNIT_ASSERT_EQ(test, fake_test.status, KUNIT_SUCCESS); 901 + KUNIT_ASSERT_EQ(test, list_count_nodes(&fake_test.resources), 0); 902 + } 903 + 904 + static struct kunit_case kunit_stub_test_cases[] = { 905 + KUNIT_CASE(kunit_stub_test), 906 + {} 907 + }; 908 + 909 + static struct kunit_suite kunit_stub_test_suite = { 910 + .name = "kunit_stub", 911 + .test_cases = kunit_stub_test_cases, 912 + }; 913 + 875 914 kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite, 876 915 &kunit_log_test_suite, &kunit_status_test_suite, 877 916 &kunit_current_test_suite, &kunit_device_test_suite, 878 - &kunit_fault_test_suite); 917 + &kunit_fault_test_suite, &kunit_stub_test_suite); 879 918 880 919 MODULE_DESCRIPTION("KUnit test for core test infrastructure"); 881 920 MODULE_LICENSE("GPL v2");
+45 -2
lib/kunit/test.c
··· 70 70 MODULE_PARM_DESC(enable, "Enable KUnit tests"); 71 71 72 72 /* 73 + * Configure the base timeout. 74 + */ 75 + static unsigned long kunit_base_timeout = CONFIG_KUNIT_DEFAULT_TIMEOUT; 76 + module_param_named(timeout, kunit_base_timeout, ulong, 0644); 77 + MODULE_PARM_DESC(timeout, "Set the base timeout for Kunit test cases"); 78 + 79 + /* 73 80 * KUnit statistic mode: 74 81 * 0 - disabled 75 82 * 1 - only when there is more than one subtest ··· 380 373 duration.tv_sec, duration.tv_nsec); 381 374 } 382 375 376 + /* Returns timeout multiplier based on speed. 377 + * DEFAULT: 1 378 + * KUNIT_SPEED_SLOW: 3 379 + * KUNIT_SPEED_VERY_SLOW: 12 380 + */ 381 + static int kunit_timeout_mult(enum kunit_speed speed) 382 + { 383 + switch (speed) { 384 + case KUNIT_SPEED_SLOW: 385 + return 3; 386 + case KUNIT_SPEED_VERY_SLOW: 387 + return 12; 388 + default: 389 + return 1; 390 + } 391 + } 392 + 393 + static unsigned long kunit_test_timeout(struct kunit_suite *suite, struct kunit_case *test_case) 394 + { 395 + int mult = 1; 396 + 397 + /* 398 + * The default test timeout is 300 seconds and will be adjusted by mult 399 + * based on the test speed. The test speed will be overridden by the 400 + * innermost test component. 401 + */ 402 + if (suite->attr.speed != KUNIT_SPEED_UNSET) 403 + mult = kunit_timeout_mult(suite->attr.speed); 404 + if (test_case->attr.speed != KUNIT_SPEED_UNSET) 405 + mult = kunit_timeout_mult(test_case->attr.speed); 406 + return mult * kunit_base_timeout * msecs_to_jiffies(MSEC_PER_SEC); 407 + } 408 + 409 + 383 410 /* 384 411 * Initializes and runs test case. Does not clean up or do post validations. 385 412 */ ··· 568 527 kunit_try_catch_init(try_catch, 569 528 test, 570 529 kunit_try_run_case, 571 - kunit_catch_run_case); 530 + kunit_catch_run_case, 531 + kunit_test_timeout(suite, test_case)); 572 532 context.test = test; 573 533 context.suite = suite; 574 534 context.test_case = test_case; ··· 579 537 kunit_try_catch_init(try_catch, 580 538 test, 581 539 kunit_try_run_case_cleanup, 582 - kunit_catch_run_case_cleanup); 540 + kunit_catch_run_case_cleanup, 541 + kunit_test_timeout(suite, test_case)); 583 542 kunit_try_catch_run(try_catch, &context); 584 543 585 544 /* Propagate the parameter result to the test case. */
+3 -1
lib/kunit/try-catch-impl.h
··· 17 17 static inline void kunit_try_catch_init(struct kunit_try_catch *try_catch, 18 18 struct kunit *test, 19 19 kunit_try_catch_func_t try, 20 - kunit_try_catch_func_t catch) 20 + kunit_try_catch_func_t catch, 21 + unsigned long timeout) 21 22 { 22 23 try_catch->test = test; 23 24 try_catch->try = try; 24 25 try_catch->catch = catch; 26 + try_catch->timeout = timeout; 25 27 } 26 28 27 29 #endif /* _KUNIT_TRY_CATCH_IMPL_H */
+2 -27
lib/kunit/try-catch.c
··· 34 34 return 0; 35 35 } 36 36 37 - static unsigned long kunit_test_timeout(void) 38 - { 39 - /* 40 - * TODO(brendanhiggins@google.com): We should probably have some type of 41 - * variable timeout here. The only question is what that timeout value 42 - * should be. 43 - * 44 - * The intention has always been, at some point, to be able to label 45 - * tests with some type of size bucket (unit/small, integration/medium, 46 - * large/system/end-to-end, etc), where each size bucket would get a 47 - * default timeout value kind of like what Bazel does: 48 - * https://docs.bazel.build/versions/master/be/common-definitions.html#test.size 49 - * There is still some debate to be had on exactly how we do this. (For 50 - * one, we probably want to have some sort of test runner level 51 - * timeout.) 52 - * 53 - * For more background on this topic, see: 54 - * https://mike-bland.com/2011/11/01/small-medium-large.html 55 - * 56 - * If tests timeout due to exceeding sysctl_hung_task_timeout_secs, 57 - * the task will be killed and an oops generated. 58 - */ 59 - return 300 * msecs_to_jiffies(MSEC_PER_SEC); /* 5 min */ 60 - } 61 - 62 37 void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context) 63 38 { 64 39 struct kunit *test = try_catch->test; ··· 60 85 task_done = task_struct->vfork_done; 61 86 wake_up_process(task_struct); 62 87 63 - time_remaining = wait_for_completion_timeout(task_done, 64 - kunit_test_timeout()); 88 + time_remaining = wait_for_completion_timeout( 89 + task_done, try_catch->timeout); 65 90 if (time_remaining == 0) { 66 91 try_catch->try_result = -ETIMEDOUT; 67 92 kthread_stop(task_struct);
+1 -2
lib/tests/longest_symbol_kunit.c
··· 3 3 * Test the longest symbol length. Execute with: 4 4 * ./tools/testing/kunit/kunit.py run longest-symbol 5 5 * --arch=x86_64 --kconfig_add CONFIG_KPROBES=y --kconfig_add CONFIG_MODULES=y 6 - * --kconfig_add CONFIG_RETPOLINE=n --kconfig_add CONFIG_CFI_CLANG=n 7 - * --kconfig_add CONFIG_MITIGATION_RETPOLINE=n 6 + * --kconfig_add CONFIG_CPU_MITIGATIONS=n --kconfig_add CONFIG_GCOV_KERNEL=n 8 7 */ 9 8 10 9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt