Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp1107305pxf; Fri, 12 Mar 2021 01:59:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJxPAyJcjFz74fzhsoqQjee2yjmjrpgzY5PsHNxH+AVMM6az7Xb3FcV6sX6ZvLnUoC3vrmSy X-Received: by 2002:a05:6402:1cc1:: with SMTP id ds1mr12947740edb.135.1615543155286; Fri, 12 Mar 2021 01:59:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615543155; cv=none; d=google.com; s=arc-20160816; b=LHuTH5X4n6Sy5l1wCLk0su5m8RBu188ZRlUyb6HcpptujsyhhY9wKTf/13IE/GJlJ8 ztGOaNNc8KjbMUZ91tWDRSjUj4EZANmacM4+0C1faAxOf+Yz2Hbi+75A09VmOcEq2T4H Ik1+2w9LQ+BtMjjqBKyp3OB3a445vIIvFUafFUniLKTJOCVA4KIxY2vG8Y3Cl0PIGek6 Ukzp/s4HB2K8hiSJ846nkiwIxEuC1CGTPAw/Wc9r0UMUe0CQR82uy5dM2hkoGKkSPxw0 pEWG6Rf4gRzYMZLwtaeFK6X0LkkPAnBdh4AMv52JQEH+1RmiEcjpFglZIkOAl/NvSGgA uJFA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=mG4xHSlWzPtu/BvBrAAYy7fi2NYsYS4zY+urTCKbOq0=; b=gcl9o/Y4t/2tu4dDgtlvFVfUym7NknFMNIE0ZBX9utYC7rU4ffqhc+G7EHL7hHlzVT sdiEvp/rGm11hWHcyNgUtbHonbMPyakYWZZK1MR8ZpVnqBB+fjGW/z2aXPFIoZw1gUNk 3QGzunC1Yq34kmjkbgToCMGK9y0hx0byz0fPNPoilfJ7oDQ47ZZf6lR6PJcchSCob/0Z AxzBsYjo4gUr4PEZnoUGs1TZ5s0ZHzIRJiZ4jV7RSy7Gqeo4l4mcmlpBLeeHVibLW8Np HSqsNHzRgjvnySALAZuVhpizxlqM8rc4L5x1FVWYd6PzZs9rztnoGjPItYpMxtIwp55u nnQA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z22si3746159edx.180.2021.03.12.01.58.53; Fri, 12 Mar 2021 01:59:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233312AbhCLJ4L (ORCPT + 99 others); Fri, 12 Mar 2021 04:56:11 -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 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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); -- 2.29.2