From patchwork Fri Mar 12 09:55:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Berg X-Patchwork-Id: 12134091 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C740AC4332B for ; Fri, 12 Mar 2021 09:56:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9AA796508C for ; Fri, 12 Mar 2021 09:56:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232884AbhCLJ4D (ORCPT ); Fri, 12 Mar 2021 04:56:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56206 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232939AbhCLJzw (ORCPT ); Fri, 12 Mar 2021 04:55:52 -0500 Received: from sipsolutions.net (s3.sipsolutions.net [IPv6:2a01:4f8:191:4433::2]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 71FBDC061574; Fri, 12 Mar 2021 01:55:52 -0800 (PST) Received: by sipsolutions.net with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.94) (envelope-from ) id 1lKeW1-00F7m8-KI; Fri, 12 Mar 2021 10:55:29 +0100 From: Johannes Berg To: linux-kernel@vger.kernel.org, linux-um@lists.infradead.org Cc: Jessica Yu , Alexander Viro , linux-fsdevel@vger.kernel.org, Johannes Berg Subject: [PATCH 2/6] module: add support for CONFIG_MODULE_DESTRUCTORS Date: Fri, 12 Mar 2021 10:55:22 +0100 Message-Id: <20210312104627.8b2523b0593c.Ib0fb7906e3d7bd69ebe5eb877e2e9f33ef915d4b@changeid> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210312095526.197739-1-johannes@sipsolutions.net> References: <20210312095526.197739-1-johannes@sipsolutions.net> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org From: Johannes Berg At least in ARCH=um with CONFIG_GCOV (which writes all the coverage data directly out from the userspace binary rather than presenting it in debugfs) it's necessary to run all the atexit handlers (dtors/fini_array) so that gcov actually does write out the data. Add a new config option CONFIG_MODULE_DESTRUCTORS that can be selected via CONFIG_WANT_MODULE_DESTRUCTORS that the arch selects (this indirection exists so the architecture doesn't have to worry about whether or not CONFIG_MODULES is on). Additionally, the architecture must then (when it exits and no more module code can run) call run_all_module_destructors to run the code for all modules that are still loaded. When modules are unloaded, the handlers are called as well. Signed-off-by: Johannes Berg --- include/linux/module.h | 14 ++++++++++++++ init/Kconfig | 17 +++++++++++++++++ kernel/module.c | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) diff --git a/include/linux/module.h b/include/linux/module.h index 59f094fa6f74..8574d76a884d 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -517,6 +517,12 @@ struct module { unsigned int num_ctors; #endif +#ifdef CONFIG_MODULE_DESTRUCTORS + /* Destructor functions. */ + ctor_fn_t *dtors; + unsigned int num_dtors; +#endif + #ifdef CONFIG_FUNCTION_ERROR_INJECTION struct error_injection_entry *ei_funcs; unsigned int num_ei_funcs; @@ -853,4 +859,12 @@ int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *, unsigned long), void *data); +#ifdef CONFIG_MODULE_DESTRUCTORS +void run_all_module_destructors(void); +#else +static inline void run_all_module_destructors(void) +{ +} +#endif + #endif /* _LINUX_MODULE_H */ diff --git a/init/Kconfig b/init/Kconfig index 22946fe5ded9..b0f0f51f9d2c 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -2295,6 +2295,23 @@ config UNUSED_KSYMS_WHITELIST endif # MODULES +config WANT_MODULE_DESTRUCTORS + bool + help + Architectures may select this if they need atexit functions (such as + generated by the compiler for -ftest-coverage/gcov) to run in modules. + They're then responsible for calling run_all_module_destructors() at + shutdown so that module destructors are called for all still loaded + modules as well. + + Note that CONFIG_GCOV_KERNEL does *not* require this since it keeps + all the coverage data in the kernel, notably CONFIG_GCOV in ARCH=um + requires this. + +config MODULE_DESTRUCTORS + def_bool y + depends on WANT_MODULE_DESTRUCTORS && MODULES + config MODULES_TREE_LOOKUP def_bool y depends on PERF_EVENTS || TRACING diff --git a/kernel/module.c b/kernel/module.c index 30479355ab85..3023b5f054d4 100644 --- a/kernel/module.c +++ b/kernel/module.c @@ -904,6 +904,27 @@ EXPORT_SYMBOL(module_refcount); /* This exists whether we can unload or not */ static void free_module(struct module *mod); +#ifdef CONFIG_MODULE_DESTRUCTORS +static void do_mod_dtors(struct module *mod) +{ + unsigned long i; + + for (i = 0; i < mod->num_dtors; i++) + mod->dtors[i](); +} + +void run_all_module_destructors(void) +{ + struct module *mod; + + /* we no longer need to care about locking at this point */ + list_for_each_entry(mod, &modules, list) + do_mod_dtors(mod); +} +#else +static inline void do_mod_dtors(struct module *mod) {} +#endif + SYSCALL_DEFINE2(delete_module, const char __user *, name_user, unsigned int, flags) { @@ -966,6 +987,7 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user, MODULE_STATE_GOING, mod); klp_module_going(mod); ftrace_release_mod(mod); + do_mod_dtors(mod); async_synchronize_full(); @@ -3263,6 +3285,23 @@ static int find_module_sections(struct module *mod, struct load_info *info) } #endif +#ifdef CONFIG_MODULE_DESTRUCTORS + mod->dtors = section_objs(info, ".dtors", + sizeof(*mod->dtors), &mod->num_dtors); + if (!mod->dtors) + mod->dtors = section_objs(info, ".fini_array", + sizeof(*mod->dtors), &mod->num_dtors); + else if (find_sec(info, ".fini_array")) { + /* + * This shouldn't happen with same compiler and binutils + * building all parts of the module. + */ + pr_warn("%s: has both .dtors and .fini_array.\n", + mod->name); + return -EINVAL; + } +#endif + mod->noinstr_text_start = section_objs(info, ".noinstr.text", 1, &mod->noinstr_text_size);