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

Configure Feed

Select the types of activity you want to include in your feed.

kunit: add KUNIT_INIT_TABLE to init linker section

Add KUNIT_INIT_TABLE to the INIT_DATA linker section.

Alter the KUnit macros to create init tests:
kunit_test_init_section_suites

Update lib/kunit/executor.c to run both the suites in KUNIT_TABLE and
KUNIT_INIT_TABLE.

Reviewed-by: David Gow <davidgow@google.com>
Signed-off-by: Rae Moar <rmoar@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

authored by

Rae Moar and committed by
Shuah Khan
d81f0d7b 69dfdce1

+109 -25
+8 -1
include/asm-generic/vmlinux.lds.h
··· 700 700 THERMAL_TABLE(governor) \ 701 701 EARLYCON_TABLE() \ 702 702 LSM_TABLE() \ 703 - EARLY_LSM_TABLE() 703 + EARLY_LSM_TABLE() \ 704 + KUNIT_INIT_TABLE() 704 705 705 706 #define INIT_TEXT \ 706 707 *(.init.text .init.text.*) \ ··· 926 925 #define KUNIT_TABLE() \ 927 926 . = ALIGN(8); \ 928 927 BOUNDED_SECTION_POST_LABEL(.kunit_test_suites, __kunit_suites, _start, _end) 928 + 929 + /* Alignment must be consistent with (kunit_suite *) in include/kunit/test.h */ 930 + #define KUNIT_INIT_TABLE() \ 931 + . = ALIGN(8); \ 932 + BOUNDED_SECTION_POST_LABEL(.kunit_init_test_suites, \ 933 + __kunit_init_suites, _start, _end) 929 934 930 935 #ifdef CONFIG_BLK_DEV_INITRD 931 936 #define INIT_RAM_FS \
+19 -11
include/kunit/test.h
··· 337 337 void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin); 338 338 void kunit_exec_list_tests(struct kunit_suite_set *suite_set, bool include_attr); 339 339 340 + struct kunit_suite_set kunit_merge_suite_sets(struct kunit_suite_set init_suite_set, 341 + struct kunit_suite_set suite_set); 342 + 340 343 #if IS_BUILTIN(CONFIG_KUNIT) 341 344 int kunit_run_all_tests(void); 342 345 #else ··· 374 371 375 372 #define kunit_test_suite(suite) kunit_test_suites(&suite) 376 373 374 + #define __kunit_init_test_suites(unique_array, ...) \ 375 + static struct kunit_suite *unique_array[] \ 376 + __aligned(sizeof(struct kunit_suite *)) \ 377 + __used __section(".kunit_init_test_suites") = { __VA_ARGS__ } 378 + 377 379 /** 378 380 * kunit_test_init_section_suites() - used to register one or more &struct 379 381 * kunit_suite containing init functions or ··· 386 378 * 387 379 * @__suites: a statically allocated list of &struct kunit_suite. 388 380 * 389 - * This functions identically as kunit_test_suites() except that it suppresses 390 - * modpost warnings for referencing functions marked __init or data marked 391 - * __initdata; this is OK because currently KUnit only runs tests upon boot 392 - * during the init phase or upon loading a module during the init phase. 381 + * This functions similar to kunit_test_suites() except that it compiles the 382 + * list of suites during init phase. 393 383 * 394 - * NOTE TO KUNIT DEVS: If we ever allow KUnit tests to be run after boot, these 395 - * tests must be excluded. 384 + * This macro also suffixes the array and suite declarations it makes with 385 + * _probe; so that modpost suppresses warnings about referencing init data 386 + * for symbols named in this manner. 396 387 * 397 - * The only thing this macro does that's different from kunit_test_suites is 398 - * that it suffixes the array and suite declarations it makes with _probe; 399 - * modpost suppresses warnings about referencing init data for symbols named in 400 - * this manner. 388 + * Note: these init tests are not able to be run after boot so there is no 389 + * "run" debugfs file generated for these tests. 390 + * 391 + * Also, do not mark the suite or test case structs with __initdata because 392 + * they will be used after the init phase with debugfs. 401 393 */ 402 394 #define kunit_test_init_section_suites(__suites...) \ 403 - __kunit_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe), \ 395 + __kunit_init_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe), \ 404 396 ##__suites) 405 397 406 398 #define kunit_test_init_section_suite(suite) \
+2
include/linux/module.h
··· 540 540 struct static_call_site *static_call_sites; 541 541 #endif 542 542 #if IS_ENABLED(CONFIG_KUNIT) 543 + int num_kunit_init_suites; 544 + struct kunit_suite **kunit_init_suites; 543 545 int num_kunit_suites; 544 546 struct kunit_suite **kunit_suites; 545 547 #endif
+3
kernel/module/main.c
··· 2199 2199 mod->kunit_suites = section_objs(info, ".kunit_test_suites", 2200 2200 sizeof(*mod->kunit_suites), 2201 2201 &mod->num_kunit_suites); 2202 + mod->kunit_init_suites = section_objs(info, ".kunit_init_test_suites", 2203 + sizeof(*mod->kunit_init_suites), 2204 + &mod->num_kunit_init_suites); 2202 2205 #endif 2203 2206 2204 2207 mod->extable = section_objs(info, "__ex_table",
+58 -6
lib/kunit/executor.c
··· 12 12 */ 13 13 extern struct kunit_suite * const __kunit_suites_start[]; 14 14 extern struct kunit_suite * const __kunit_suites_end[]; 15 + extern struct kunit_suite * const __kunit_init_suites_start[]; 16 + extern struct kunit_suite * const __kunit_init_suites_end[]; 15 17 16 18 static char *action_param; 17 19 ··· 294 292 } 295 293 } 296 294 295 + struct kunit_suite_set kunit_merge_suite_sets(struct kunit_suite_set init_suite_set, 296 + struct kunit_suite_set suite_set) 297 + { 298 + struct kunit_suite_set total_suite_set = {NULL, NULL}; 299 + struct kunit_suite **total_suite_start = NULL; 300 + size_t init_num_suites, num_suites, suite_size; 301 + 302 + init_num_suites = init_suite_set.end - init_suite_set.start; 303 + num_suites = suite_set.end - suite_set.start; 304 + suite_size = sizeof(suite_set.start); 305 + 306 + /* Allocate memory for array of all kunit suites */ 307 + total_suite_start = kmalloc_array(init_num_suites + num_suites, suite_size, GFP_KERNEL); 308 + if (!total_suite_start) 309 + return total_suite_set; 310 + 311 + /* Append init suites and then all other kunit suites */ 312 + memcpy(total_suite_start, init_suite_set.start, init_num_suites * suite_size); 313 + memcpy(total_suite_start + init_num_suites, suite_set.start, num_suites * suite_size); 314 + 315 + /* Set kunit suite set start and end */ 316 + total_suite_set.start = total_suite_start; 317 + total_suite_set.end = total_suite_start + (init_num_suites + num_suites); 318 + 319 + return total_suite_set; 320 + } 321 + 297 322 #if IS_BUILTIN(CONFIG_KUNIT) 298 323 299 324 static char *kunit_shutdown; ··· 342 313 343 314 int kunit_run_all_tests(void) 344 315 { 345 - struct kunit_suite_set suite_set = { 316 + struct kunit_suite_set suite_set = {NULL, NULL}; 317 + struct kunit_suite_set filtered_suite_set = {NULL, NULL}; 318 + struct kunit_suite_set init_suite_set = { 319 + __kunit_init_suites_start, __kunit_init_suites_end, 320 + }; 321 + struct kunit_suite_set normal_suite_set = { 346 322 __kunit_suites_start, __kunit_suites_end, 347 323 }; 324 + size_t init_num_suites = init_suite_set.end - init_suite_set.start; 348 325 int err = 0; 326 + 327 + if (init_num_suites > 0) { 328 + suite_set = kunit_merge_suite_sets(init_suite_set, normal_suite_set); 329 + if (!suite_set.start) 330 + goto out; 331 + } else 332 + suite_set = normal_suite_set; 333 + 349 334 if (!kunit_enabled()) { 350 335 pr_info("kunit: disabled\n"); 351 - goto out; 336 + goto free_out; 352 337 } 353 338 354 339 if (filter_glob_param || filter_param) { 355 - suite_set = kunit_filter_suites(&suite_set, filter_glob_param, 340 + filtered_suite_set = kunit_filter_suites(&suite_set, filter_glob_param, 356 341 filter_param, filter_action_param, &err); 342 + 343 + /* Free original suite set before using filtered suite set */ 344 + if (init_num_suites > 0) 345 + kfree(suite_set.start); 346 + suite_set = filtered_suite_set; 347 + 357 348 if (err) { 358 349 pr_err("kunit executor: error filtering suites: %d\n", err); 359 - goto out; 350 + goto free_out; 360 351 } 361 352 } 362 353 ··· 389 340 else 390 341 pr_err("kunit executor: unknown action '%s'\n", action_param); 391 342 392 - if (filter_glob_param || filter_param) { /* a copy was made of each suite */ 343 + free_out: 344 + if (filter_glob_param || filter_param) 393 345 kunit_free_suite_set(suite_set); 394 - } 346 + else if (init_num_suites > 0) 347 + /* Don't use kunit_free_suite_set because suites aren't individually allocated */ 348 + kfree(suite_set.start); 395 349 396 350 out: 397 351 kunit_handle_shutdown();
+19 -7
lib/kunit/test.c
··· 742 742 #ifdef CONFIG_MODULES 743 743 static void kunit_module_init(struct module *mod) 744 744 { 745 - struct kunit_suite_set suite_set = { 745 + struct kunit_suite_set suite_set, filtered_set; 746 + struct kunit_suite_set normal_suite_set = { 746 747 mod->kunit_suites, mod->kunit_suites + mod->num_kunit_suites, 748 + }; 749 + struct kunit_suite_set init_suite_set = { 750 + mod->kunit_init_suites, mod->kunit_init_suites + mod->num_kunit_init_suites, 747 751 }; 748 752 const char *action = kunit_action(); 749 753 int err = 0; 750 754 751 - suite_set = kunit_filter_suites(&suite_set, 755 + if (mod->num_kunit_init_suites > 0) 756 + suite_set = kunit_merge_suite_sets(init_suite_set, normal_suite_set); 757 + else 758 + suite_set = normal_suite_set; 759 + 760 + filtered_set = kunit_filter_suites(&suite_set, 752 761 kunit_filter_glob() ?: "*.*", 753 762 kunit_filter(), kunit_filter_action(), 754 763 &err); 755 764 if (err) 756 765 pr_err("kunit module: error filtering suites: %d\n", err); 757 766 758 - mod->kunit_suites = (struct kunit_suite **)suite_set.start; 759 - mod->num_kunit_suites = suite_set.end - suite_set.start; 767 + mod->kunit_suites = (struct kunit_suite **)filtered_set.start; 768 + mod->num_kunit_suites = filtered_set.end - filtered_set.start; 769 + 770 + if (mod->num_kunit_init_suites > 0) 771 + kfree(suite_set.start); 760 772 761 773 if (!action) 762 - kunit_exec_run_tests(&suite_set, false); 774 + kunit_exec_run_tests(&filtered_set, false); 763 775 else if (!strcmp(action, "list")) 764 - kunit_exec_list_tests(&suite_set, false); 776 + kunit_exec_list_tests(&filtered_set, false); 765 777 else if (!strcmp(action, "list_attr")) 766 - kunit_exec_list_tests(&suite_set, true); 778 + kunit_exec_list_tests(&filtered_set, true); 767 779 else 768 780 pr_err("kunit: unknown action '%s'\n", action); 769 781 }