Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp3597585iog; Tue, 21 Jun 2022 01:57:33 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tYu+YeuVWcs5s7smxx51PN/pudMz6gLotaz3FWLjdPTJ5sVqXSH+9hgAI1vNm/kZh4uU5Y X-Received: by 2002:a17:907:c1d:b0:70f:469a:3d73 with SMTP id ga29-20020a1709070c1d00b0070f469a3d73mr24420453ejc.248.1655801853301; Tue, 21 Jun 2022 01:57:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655801853; cv=none; d=google.com; s=arc-20160816; b=yshecKaiBA7gW78xXJH1e+CvcMTIhdaBCcYBzhPhPSuw1/qt9ihzXivM5dJxTM/P7K srj92G8lvCjxoUEmxMuvWWpmwUYX62tMNzFKkTiXWO3hXlaUKWSFBJtYv8SGwiTNDhIS ZtPuqvXpKWem/kIxhroVKhN9+1j1TzjHcRlsiTrUD4qDHTRNRpz3NrifKQMEsA664RX6 hlGzsupOB7tUlCwxU5lT5fN7bKC1WvOaKPo1J4uQose30gnt8y/GWuS/zNxsS5dl6kva zf4+LzjIGU+rFGxvIJOXgARnk5EK7u4DVbj5Cp9I3wVLxO33cTG6D9Ky4jAjOhvDaJWq 6esA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:from:subject :references:mime-version:message-id:in-reply-to:date:dkim-signature; bh=nJz31fD/Qn5aBBpUWgybP8ILVlSKb1nLFQuC5dZKu0w=; b=IeSSS8jnSCpTp5P6OTw8HgHXPDePW/3SEa/KY+LbaX9+y854RA6h8mrx4+0Tf+IKi+ sQVNpU3gRb0ep83oXX8i5Ct54RC4TSl/cdXZlT+vMHXcDNfIy94Wf5RlwY0MLON5KoBD INJ39rFR1cyd6ebKcrqTXcVjgjlELztIM49ms5sSuloVMfGP7uJC6xivMiTKT9sztoQI +pqTQCxoZqmluGE3SFr2Chft9N6Q9NV1b8ScZC3iHG57HqlvRxUuannXMR7MgjJXB+Q7 QvizdKHkDpUiXSUANUHH+I5/+PexYWGcfjY56XoeOgke0QBPF3p/rZfqaEaQJbGnWeoX 1hIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=W5Mqh+r4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p17-20020a1709061b5100b00711ffe66bf3si13500161ejg.612.2022.06.21.01.57.05; Tue, 21 Jun 2022 01:57:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=W5Mqh+r4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348034AbiFUIyD (ORCPT + 99 others); Tue, 21 Jun 2022 04:54:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38360 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347946AbiFUIyA (ORCPT ); Tue, 21 Jun 2022 04:54:00 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3714F1902A for ; Tue, 21 Jun 2022 01:53:58 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-31776c7cd7eso107765837b3.5 for ; Tue, 21 Jun 2022 01:53:58 -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:content-transfer-encoding; bh=nJz31fD/Qn5aBBpUWgybP8ILVlSKb1nLFQuC5dZKu0w=; b=W5Mqh+r4xo+C5OFuGYOVQuLPL+lEgUHN0AQLfdDyjYuVlh7yRrncm8NNiH0Lrku/Gd BvGiYUvWq+s/zpYN/6GnBBKw1eaDIVmHCK0JPVbi51jP95yiCpwpzTEGosmrweprbSX8 bN0xZl9GoJ9l7g/BRsPfsWQaLSBk1vUWjL7x7Er7P9m9OZkSt6SIB5ELhtTe3KQL96OU lfL5WkeaxWwM9uhNsefMESiw8fuX9EmTHAtTechHdqYKMR16pEzlZlNtF4PGFoF2NQpp c5lN5aNOJgKIoQk2e1GukF/dvisCXEmIlyXf8MzvPDyHMSaJwdSWNBKasb3b0Wr76shH eqGQ== 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:content-transfer-encoding; bh=nJz31fD/Qn5aBBpUWgybP8ILVlSKb1nLFQuC5dZKu0w=; b=dkp9vxDDnCTLTLRnkgs15Bfq7DXgyJWlD/juHp/3t+q7K+w9cVYazU+P/YA16Dv6s8 Inuqo3FkzZL1d7fhUDt4Ple8MvlTvxDYdthLSTZf2E+mmYMDWbHLI5Kmi2z9ecsHxIPZ oGNTlkHsaotAG1ht8Y90rPq7/jyHwAxUfPTy5z6/N0V7Gh52yOf+msxgcSDdhevq+m26 yZiDuFrtr0YsBfwHYiN1eeI4qDlPC+tTCJJCSabphH5vZt3WLBGY9tS+2SMlyiuBWIhN 67DcL/o0zoM7E8VnU498RClI9RBi3HkWDcEyYH//UZ+H02SnQu5PP7hwRPmL0gT4HUbs mu7Q== X-Gm-Message-State: AJIora84DkkgRqbD7kLX/3KeuCBWE24FW7TXAg3kc4y8rK4+ufIRaAaB c7DFUbbKkQ05bCAYYHXgN43of2tRkbMkoQ== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a81:3a83:0:b0:317:82a1:a74 with SMTP id h125-20020a813a83000000b0031782a10a74mr24570781ywa.404.1655801637397; Tue, 21 Jun 2022 01:53:57 -0700 (PDT) Date: Tue, 21 Jun 2022 16:53:41 +0800 In-Reply-To: <20220621085345.603820-1-davidgow@google.com> Message-Id: <20220621085345.603820-2-davidgow@google.com> Mime-Version: 1.0 References: <20220621085345.603820-1-davidgow@google.com> X-Mailer: git-send-email 2.37.0.rc0.104.g0611611a94-goog Subject: [PATCH v2 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: Paraschiv@google.com, Christophe Leroy , kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, "=?UTF-8?q?Ma=C3=ADra=20Canal?=" , 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Tested-by: Ma=C3=ADra Canal Signed-off-by: Jeremy Kerr Signed-off-by: Daniel Latypov Signed-off-by: David Gow --- Changes since v1: https://lore.kernel.org/linux-kselftest/20220618090310.1174932-2-davidgow@g= oogle.com/ - Fix a compile error with CONFIG_KUNIT=3Dm (Thanks Christophe Leroy, kernel test robot) - Add Ma=C3=ADra's Tested-by. Changes since RFC: https://lore.kernel.org/linux-kselftest/101d12fc9250b7a445ff50a9e7a25cd74d0= e16eb.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) */ =20 -#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 fo= r - * 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[] =3D { __VA_ARGS__, NULL }; = \ - kunit_test_suites_for_module(unique_array); \ static struct kunit_suite **unique_suites \ __used __section(".kunit_test_suites") =3D unique_array =20 @@ -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 n= ot - * 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 whe= n + * 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..2490223c975d 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 +#if IS_ENABLED(CONFIG_KUNIT) + int num_kunit_suites; + struct kunit_suite ***kunit_suites; +#endif + =20 #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 =3D section_objs(info, ".kunit_test_suites", + sizeof(*mod->kunit_suites), + &mod->num_kunit_suites); +#endif + mod->extable =3D section_objs(info, "__ex_table", sizeof(*mod->extable), &mod->num_exentries); =20 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 **sui= tes) } EXPORT_SYMBOL_GPL(__kunit_test_suites_exit); =20 +#ifdef CONFIG_MODULES +static void kunit_module_init(struct module *mod) +{ + unsigned int i; + + for (i =3D 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 =3D 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 va= l, + void *data) +{ + struct module *mod =3D 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 =3D { + .notifier_call =3D kunit_module_notify, + .priority =3D 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); =20 static void __exit kunit_exit(void) { +#ifdef CONFIG_MODULES + unregister_module_notifier(&kunit_mod_nb); +#endif kunit_debugfs_cleanup(); } module_exit(kunit_exit); --=20 2.37.0.rc0.104.g0611611a94-goog