module: add debugging auto-load duplicate module support
Subsystems (3)
Luis Chamberlain
maintainerPATCH v2 1/1 module: add debugging auto-load duplicate module support
The finit_module() system call can in the worst case use up to more than twice of a module's size in virtual memory. Duplicate finit_module() system calls are non fatal, however they unnecessarily strain virtual memory during bootup and in the worst case can cause a system to fail to boot. This is only known to currently be an issue on systems with larger number of CPUs.
To help debug this situation we need to consider the different sources for finit_module(). Requests from the kernel that rely on module auto-loading, ie, the kernel's *request_module() API, are one source of calls. Although modprobe checks to see if a module is already loaded prior to calling finit_module() there is a small race possible allowing userspace to trigger multiple modprobe calls racing against modprobe and this not seeing the module yet loaded.
This adds debugging support to the kernel module auto-loader (*request_module() calls) to easily detect duplicate module requests. To aid with possible bootup failure issues incurred by this, it will converge duplicates requests to a single request. This avoids any possible strain on virtual memory during bootup which could be incurred by duplicate module autoloading requests.
Folks debugging virtual memory abuse on bootup can and should enable this to see what pr_warn()s come on, to see if module auto-loading is to blame for their wores. If they see duplicates they can further debug this by enabling the module.enable_dups_trace kernel parameter or by enabling CONFIG_MODULE_DEBUG_AUTOLOAD_DUPS_TRACE.
Current evidence seems to point to only a few duplicates for module auto-loading. And so the source for other duplicates creating heavy virtual memory pressure due to larger number of CPUs should becoming from another place (likely udev).
---
Changes on this v2:
o Tons of language adjustments based on feedback by Greg KH so that folks understand this is for debugging purposes. The "lost" language has been modified to clarify the issue with duplicates is straining of virtual memory on bootup.
o The kconfig option was renamed to clarify the goal
o A new module paramter is added so to avoid WARN_ON() by default and have the bots go crazy. A new kconfig option is also added so to not have folks have to set this module parameter. Documentation is added for this too.
.../admin-guide/kernel-parameters.txt | 6 + kernel/module/Kconfig | 59 +++++ kernel/module/Makefile | 1 + kernel/module/dups.c | 246 ++++++++++++++++++ kernel/module/internal.h | 15 ++ kernel/module/kmod.c | 23 +- 6 files changed, 346 insertions(+), 4 deletions(-) create mode 100644 kernel/module/dups.c
Documentation/admin-guide/kernel-parameters.txt +6 -0
@@ -3332,6 +3332,12 @@ specified, <module>.async_probe takes precedence for the specific module. + module.enable_dups_trace + [KNL] When CONFIG_MODULE_DEBUG_AUTOLOAD_DUPS is set, + this means that duplicate request_module() calls will + trigger a WARN_ON() instead of a pr_warn(). Note that + if MODULE_DEBUG_AUTOLOAD_DUPS_TRACE is set, WARN_ON()s + will always be issued and this option does nothing. module.sig_enforce [KNL] When CONFIG_MODULE_SIG is set, this means that modules without (valid) signatures will fail to load.
kernel/module/Kconfig +59 -0
65 lines not on this page · raw message
kernel/module/Makefile +1 -0
@@ -10,6 +10,7 @@ KCOV_INSTRUMENT_module.o := n obj-y += main.o obj-y += strict_rwx.o obj-y += kmod.o +obj-$(CONFIG_MODULE_DEBUG_AUTOLOAD_DUPS) += dups.o obj-$(CONFIG_MODULE_DECOMPRESS) += decompress.o obj-$(CONFIG_MODULE_SIG) += signing.o obj-$(CONFIG_LIVEPATCH) += livepatch.o
kernel/module/dups.c new file +246 -0
246 lines not on this page · raw message
kernel/module/internal.h +15 -0
@@ -3,6 +3,7 @@* * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. * Written by David Howells ([email protected]) +* Copyright (C) 2023 Luis Chamberlain <[email protected]> */ #include <linux/elf.h>
@@ -221,6 +222,20 @@ static inline void mod_stat_bump_becoming(struct load_info *info, int flags)#endif /* CONFIG_MODULE_STATS */ +#ifdef CONFIG_MODULE_DEBUG_AUTOLOAD_DUPS +bool kmod_dup_request_exists_wait (char *module_name,bool wait,int *dup_ret); +void kmod_dup_request_announce (char *module_name,int ret); +#else +static inline bool kmod_dup_request_exists_wait (char *module_name,bool wait,int *dup_ret) +{ +return false; +} + +static inline void kmod_dup_request_announce (char *module_name,int ret) +{ +} +#endif + #ifdef CONFIG_MODULE_UNLOAD_TAINT_TRACKING struct mod_unload_taint { struct list_head list;
kernel/module/kmod.c +19 -4
@@ -1,6 +1,9 @@/* * kmod - the kernel module loader +* +* Copyright (C) 2023 Luis Chamberlain <[email protected]> */ +#include <linux/module.h> #include <linux/sched.h> #include <linux/sched/task.h>
@@ -27,6 +30,7 @@#include <linux/uaccess.h> #include <trace/events/module.h> +#include "internal.h" /* * Assuming:
@@ -65,7 +69,7 @@ static void free_modprobe_argv(struct subprocess_info *info)kfree (info->argv); } -static int call_modprobe (char *module_name,int wait) +static int call_modprobe (char *orig_module_name,int wait) {struct subprocess_info *info;static char *envp[] = {
@@ -74,12 +78,14 @@ static int call_modprobe(char *module_name, int wait)"PATH=/sbin:/usr/sbin:/bin:/usr/bin" , NULL }; +char *module_name; +int ret;char **argv =kmalloc (sizeof (char *[5 ]),GFP_KERNEL );if (!argv) goto out; - module_name =kstrdup (module_name,GFP_KERNEL ); + module_name =kstrdup (orig_module_name,GFP_KERNEL );if (!module_name) goto free_argv;
@@ -94,13 +100,16 @@ static int call_modprobe(char *module_name, int wait)if (!info) goto free_module_name; -return call_usermodehelper_exec (info, wait |UMH_KILLABLE ); + ret =call_usermodehelper_exec (info, wait |UMH_KILLABLE ); +kmod_dup_request_announce (orig_module_name, ret); +return ret; free_module_name:kfree (module_name); free_argv:kfree (argv); out: +kmod_dup_request_announce (orig_module_name, -ENOMEM );return -ENOMEM ; }
@@ -124,7 +133,7 @@ int __request_module(bool wait, const char *fmt, ...) {va_list args;char module_name[MODULE_NAME_LEN ]; -int ret; +int ret, dup_ret;/* * We don't allow synchronous module loading from async. Module
@@ -156,8 +165,14 @@ int __request_module(bool wait, const char *fmt, ...)trace_module_request (module_name, wait, _RET_IP_); +if (kmod_dup_request_exists_wait (module_name, wait, &dup_ret)) { + ret = dup_ret; + goto out; + } + ret =call_modprobe (module_name, wait ?UMH_WAIT_PROC :UMH_WAIT_EXEC ); +out:up (&kmod_concurrent_max);return ret;
Signature · 1 line
2.39.2