@@ -233,7 +233,7 @@ size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
233233unsigned int kunit_test_case_num (struct kunit_suite * suite ,
234234 struct kunit_case * test_case );
235235
236- int __kunit_test_suites_init (struct kunit_suite * * suites );
236+ int __kunit_test_suites_init (struct kunit_suite * const * const suites );
237237
238238void __kunit_test_suites_exit (struct kunit_suite * * suites );
239239
@@ -246,34 +246,57 @@ void __kunit_test_suites_exit(struct kunit_suite **suites);
246246 * Registers @suites_list with the test framework. See &struct kunit_suite for
247247 * more information.
248248 *
249- * When builtin, KUnit tests are all run as late_initcalls; this means
250- * that they cannot test anything where tests must run at a different init
251- * phase. One significant restriction resulting from this is that KUnit
252- * cannot reliably test anything that is initialize in the late_init phase;
253- * another is that KUnit is useless to test things that need to be run in
254- * an earlier init phase.
255- *
256- * An alternative is to build the tests as a module. Because modules
257- * do not support multiple late_initcall()s, we need to initialize an
258- * array of suites for a module.
259- *
260- * TODO(brendanhiggins@google.com): Don't run all KUnit tests as
261- * late_initcalls. I have some future work planned to dispatch all KUnit
262- * tests from the same place, and at the very least to do so after
263- * everything else is definitely initialized.
249+ * If a test suite is built-in, module_init() gets translated into
250+ * an initcall which we don't want as the idea is that for builtins
251+ * the executor will manage execution. So ensure we do not define
252+ * module_{init|exit} functions for the builtin case when registering
253+ * suites via kunit_test_suites() below.
264254 */
265- #define kunit_test_suites ( suites_list ...) \
266- static struct kunit_suite *suites[] = {suites_list, NULL}; \
267- static int kunit_test_suites_init(void) \
255+ #ifdef MODULE
256+ #define kunit_test_suites_for_module ( __suites ) \
257+ static int __init kunit_test_suites_init(void) \
268258 { \
269- return __kunit_test_suites_init(suites ); \
259+ return __kunit_test_suites_init(__suites ); \
270260 } \
271- late_initcall(kunit_test_suites_init); \
261+ module_init(kunit_test_suites_init); \
262+ \
272263 static void __exit kunit_test_suites_exit(void) \
273264 { \
274- return __kunit_test_suites_exit(suites ); \
265+ return __kunit_test_suites_exit(__suites ); \
275266 } \
276267 module_exit(kunit_test_suites_exit)
268+ #else
269+ #define kunit_test_suites_for_module (__suites )
270+ #endif /* MODULE */
271+
272+ #define __kunit_test_suites (unique_array , unique_suites , ...) \
273+ static struct kunit_suite *unique_array[] = { __VA_ARGS__, NULL }; \
274+ kunit_test_suites_for_module(unique_array); \
275+ static struct kunit_suite **unique_suites \
276+ __used __section(.kunit_test_suites) = unique_array
277+
278+ /**
279+ * kunit_test_suites() - used to register one or more &struct kunit_suite
280+ * with KUnit.
281+ *
282+ * @suites: a statically allocated list of &struct kunit_suite.
283+ *
284+ * Registers @suites with the test framework. See &struct kunit_suite for
285+ * more information.
286+ *
287+ * When builtin, KUnit tests are all run via executor; this is done
288+ * by placing the array of struct kunit_suite * in the .kunit_test_suites
289+ * ELF section.
290+ *
291+ * An alternative is to build the tests as a module. Because modules do not
292+ * support multiple initcall()s, we need to initialize an array of suites for a
293+ * module.
294+ *
295+ */
296+ #define kunit_test_suites (...) \
297+ __kunit_test_suites(__UNIQUE_ID(array), \
298+ __UNIQUE_ID(suites), \
299+ __VA_ARGS__)
277300
278301#define kunit_test_suite (suite ) kunit_test_suites(&suite)
279302
0 commit comments