From patchwork Sat Jun 18 09:03:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 12886270 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DE592C43334 for ; Sat, 18 Jun 2022 09:03:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231645AbiFRJDj (ORCPT ); Sat, 18 Jun 2022 05:03:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45288 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232693AbiFRJDi (ORCPT ); Sat, 18 Jun 2022 05:03:38 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3572B2F029 for ; Sat, 18 Jun 2022 02:03:36 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id u9-20020a17090a2b8900b001ec79a1050eso2212588pjd.4 for ; Sat, 18 Jun 2022 02:03:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=xcKhd2ovDrzW4TN5oKPRjlu0BegYI/Ed8HUs8ET36D0=; b=XAbzUsj91wrGwVolFVA6fl0C5dE56VX4gaU68QvHoo1q3J3md2ju8twxWO9ypQkCXo YLVk/g/eJWGTIbdLeaj1iVfuXi4pFirKgvm3QQO+2adT3Zyn0WhtT3kF/y+YrRov5iCB 1IG+AzyTtkagpco0Pied13GoHBw6TtqSUqXnZFl/mxEC19JfwK45blxTEmq5HPbvm0yS xMNmNyjXVVA+BDnILrP8sZc4XRNJysY9F32F83W41WgSeLJkkF15ABFdfUX2Vf1rzi9f s7e484WAj9LMLHRcoD1EwraInpSjASQFFoaoaT2/p4CS9pfTdFvSQT/RLjYT7XoYKHaJ lyaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=xcKhd2ovDrzW4TN5oKPRjlu0BegYI/Ed8HUs8ET36D0=; b=E7TS9WCvs3vpaPKxRcKaNj4Ie60OV9y289828ZAMGZPz0m1cK+vjNJpOrtoMylKK4D jxRbLp+4Nd36lbYNslnyV2WcbDHvihbd1ZxN5kTN4z3Bb50BzNX+xRPk2iaBujYS48gu BjaXphGh1tC+Rj8+56YgDuXU6FirvYJE9FB5IqgIBwjGWZbcNUbbFxiToB8uZH5gJNlz IFR/3IBGrrD2/P6ZV0IhdmE518dNzTg2GB1hFgGCYUDW4kjecS/aNpLEmCZH7lnO8O2P PSL93bfgf+zpGdxUZERSmT7mTVwH1LgpmJQFu7OHQ74GqvcoWVimvoLZvPM5pdhfFLTd pQTA== X-Gm-Message-State: AJIora+WpO44jVm7nMUUCuy0qf5MrxffELtBKVhqlssLaQo28ff0s4lT rN+lAd7CO40RUfQJ0btxr9ZrLpwdrX5NGA== X-Google-Smtp-Source: AGRyM1vDoXfonjmrvKRgfpNxP1jpWSSE8piUJc9c0Ybgfw+LPYyVsMqQgH5/9pfMsNlOGK3dDvBe5L4hQK9aAA== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a17:902:b610:b0:168:cd85:d7a2 with SMTP id b16-20020a170902b61000b00168cd85d7a2mr14297035pls.125.1655543015440; Sat, 18 Jun 2022 02:03:35 -0700 (PDT) Date: Sat, 18 Jun 2022 17:03:06 +0800 In-Reply-To: <20220618090310.1174932-1-davidgow@google.com> Message-Id: <20220618090310.1174932-2-davidgow@google.com> Mime-Version: 1.0 References: <20220618090310.1174932-1-davidgow@google.com> X-Mailer: git-send-email 2.36.1.476.g0c4daa206d-goog Subject: [PATCH 1/5] kunit: unify module and builtin suite definitions From: David Gow To: Brendan Higgins , Luis Chamberlain , Jeremy Kerr , Daniel Latypov , Shuah Khan , Andrew Jeffery , Mika Westerberg , Andra Paraschiv , Longpeng Cc: kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, " =?utf-8?q?Ma=C3=ADra_Canal?= " , linux-mmc@vger.kernel.org, linux-aspeed@lists.ozlabs.org, openbmc@lists.ozlabs.org, linux-usb@vger.kernel.org, linux-modules@vger.kernel.org, Matt Johnston , David Gow Precedence: bulk List-ID: From: Jeremy Kerr Currently, KUnit runs built-in tests and tests loaded from modules differently. For built-in tests, the kunit_test_suite{,s}() macro adds a list of suites in the .kunit_test_suites linker section. However, for kernel modules, a module_init() function is used to run the test suites. This causes problems if tests are included in a module which already defines module_init/exit_module functions, as they'll conflict with the kunit-provided ones. This change removes the kunit-defined module inits, and instead parses the kunit tests from their own section in the module. After module init, we call __kunit_test_suites_init() on the contents of that section, which prepares and runs the suite. This essentially unifies the module- and non-module kunit init formats. Signed-off-by: Jeremy Kerr Signed-off-by: Daniel Latypov Signed-off-by: David Gow Reported-by: kernel test robot Reported-by: kernel test robot --- This is essentially the patch at: https://lore.kernel.org/linux-kselftest/101d12fc9250b7a445ff50a9e7a25cd74d0e16eb.camel@codeconstruct.com.au/ I've basically just rebased it, tweaked some wording, and it made it still compile when CONFIG_MODULES is not set. include/kunit/test.h | 47 ++++---------------------------------- include/linux/module.h | 5 ++++ kernel/module/main.c | 6 +++++ lib/kunit/test.c | 52 +++++++++++++++++++++++++++++++++++++++++- 4 files changed, 67 insertions(+), 43 deletions(-) diff --git a/include/kunit/test.h b/include/kunit/test.h index 8ffcd7de9607..54306271cfbf 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -250,41 +250,8 @@ static inline int kunit_run_all_tests(void) } #endif /* IS_BUILTIN(CONFIG_KUNIT) */ -#ifdef MODULE -/** - * kunit_test_suites_for_module() - used to register one or more - * &struct kunit_suite with KUnit. - * - * @__suites: a statically allocated list of &struct kunit_suite. - * - * Registers @__suites with the test framework. See &struct kunit_suite for - * more information. - * - * If a test suite is built-in, module_init() gets translated into - * an initcall which we don't want as the idea is that for builtins - * the executor will manage execution. So ensure we do not define - * module_{init|exit} functions for the builtin case when registering - * suites via kunit_test_suites() below. - */ -#define kunit_test_suites_for_module(__suites) \ - static int __init kunit_test_suites_init(void) \ - { \ - return __kunit_test_suites_init(__suites); \ - } \ - module_init(kunit_test_suites_init); \ - \ - static void __exit kunit_test_suites_exit(void) \ - { \ - return __kunit_test_suites_exit(__suites); \ - } \ - module_exit(kunit_test_suites_exit) -#else -#define kunit_test_suites_for_module(__suites) -#endif /* MODULE */ - #define __kunit_test_suites(unique_array, unique_suites, ...) \ static struct kunit_suite *unique_array[] = { __VA_ARGS__, NULL }; \ - kunit_test_suites_for_module(unique_array); \ static struct kunit_suite **unique_suites \ __used __section(".kunit_test_suites") = unique_array @@ -294,16 +261,12 @@ static inline int kunit_run_all_tests(void) * * @__suites: a statically allocated list of &struct kunit_suite. * - * Registers @suites with the test framework. See &struct kunit_suite for - * more information. - * - * When builtin, KUnit tests are all run via executor; this is done - * by placing the array of struct kunit_suite * in the .kunit_test_suites - * ELF section. + * Registers @suites with the test framework. + * This is done by placing the array of struct kunit_suite * in the + * .kunit_test_suites ELF section. * - * An alternative is to build the tests as a module. Because modules do not - * support multiple initcall()s, we need to initialize an array of suites for a - * module. + * When builtin, KUnit tests are all run via the executor at boot, and when + * built as a module, they run on module load. * */ #define kunit_test_suites(__suites...) \ diff --git a/include/linux/module.h b/include/linux/module.h index abd9fa916b7d..e3cd6c325794 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -505,6 +505,11 @@ struct module { int num_static_call_sites; struct static_call_site *static_call_sites; #endif +#ifdef CONFIG_KUNIT + int num_kunit_suites; + struct kunit_suite ***kunit_suites; +#endif + #ifdef CONFIG_LIVEPATCH bool klp; /* Is this a livepatch module? */ diff --git a/kernel/module/main.c b/kernel/module/main.c index fed58d30725d..4542db7cdf54 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2087,6 +2087,12 @@ static int find_module_sections(struct module *mod, struct load_info *info) sizeof(*mod->static_call_sites), &mod->num_static_call_sites); #endif +#ifdef CONFIG_KUNIT + mod->kunit_suites = section_objs(info, ".kunit_test_suites", + sizeof(*mod->kunit_suites), + &mod->num_kunit_suites); +#endif + mod->extable = section_objs(info, "__ex_table", sizeof(*mod->extable), &mod->num_exentries); diff --git a/lib/kunit/test.c b/lib/kunit/test.c index a5053a07409f..3052526b9b89 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -609,6 +610,49 @@ void __kunit_test_suites_exit(struct kunit_suite **suites) } EXPORT_SYMBOL_GPL(__kunit_test_suites_exit); +#ifdef CONFIG_MODULES +static void kunit_module_init(struct module *mod) +{ + unsigned int i; + + for (i = 0; i < mod->num_kunit_suites; i++) + __kunit_test_suites_init(mod->kunit_suites[i]); +} + +static void kunit_module_exit(struct module *mod) +{ + unsigned int i; + + for (i = 0; i < mod->num_kunit_suites; i++) + __kunit_test_suites_exit(mod->kunit_suites[i]); +} + +static int kunit_module_notify(struct notifier_block *nb, unsigned long val, + void *data) +{ + struct module *mod = data; + + switch (val) { + case MODULE_STATE_LIVE: + kunit_module_init(mod); + break; + case MODULE_STATE_GOING: + kunit_module_exit(mod); + break; + case MODULE_STATE_COMING: + case MODULE_STATE_UNFORMED: + break; + } + + return 0; +} + +static struct notifier_block kunit_mod_nb = { + .notifier_call = kunit_module_notify, + .priority = 0, +}; +#endif + struct kunit_kmalloc_array_params { size_t n; size_t size; @@ -703,13 +747,19 @@ EXPORT_SYMBOL_GPL(kunit_cleanup); static int __init kunit_init(void) { kunit_debugfs_init(); - +#ifdef CONFIG_MODULES + return register_module_notifier(&kunit_mod_nb); +#else return 0; +#endif } late_initcall(kunit_init); static void __exit kunit_exit(void) { +#ifdef CONFIG_MODULES + unregister_module_notifier(&kunit_mod_nb); +#endif kunit_debugfs_cleanup(); } module_exit(kunit_exit);