From: Luis Chamberlain Date: Sun, 19 Mar 2023 21:35:42 +0000 (-0700) Subject: module: fold usermode helper kmod into modules directory X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=25be451aa4c0e9a96c59a626ab0e93d5cb7f6f48;p=linux.git module: fold usermode helper kmod into modules directory The kernel/kmod.c is already only built if we enabled modules, so just stuff it under kernel/module/kmod.c and unify the MAINTAINERS file for it. Signed-off-by: Luis Chamberlain --- diff --git a/MAINTAINERS b/MAINTAINERS index 8d5bc223f3053..1ca0e26aa9f82 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -11522,16 +11522,6 @@ F: include/linux/kmemleak.h F: mm/kmemleak.c F: samples/kmemleak/kmemleak-test.c -KMOD KERNEL MODULE LOADER - USERMODE HELPER -M: Luis Chamberlain -L: linux-kernel@vger.kernel.org -L: linux-modules@vger.kernel.org -S: Maintained -F: include/linux/kmod.h -F: kernel/kmod.c -F: lib/test_kmod.c -F: tools/testing/selftests/kmod/ - KMSAN M: Alexander Potapenko R: Marco Elver @@ -14083,8 +14073,11 @@ L: linux-kernel@vger.kernel.org S: Maintained T: git git://git.kernel.org/pub/scm/linux/kernel/git/mcgrof/linux.git modules-next F: include/linux/module.h +F: include/linux/kmod.h F: kernel/module/ F: scripts/module* +F: lib/test_kmod.c +F: tools/testing/selftests/kmod/ MONOLITHIC POWER SYSTEM PMIC DRIVER M: Saravanan Sekar diff --git a/kernel/Makefile b/kernel/Makefile index 10ef068f598d5..3dd4ea433ee99 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -13,7 +13,6 @@ obj-y = fork.o exec_domain.o panic.o \ async.o range.o smpboot.o ucount.o regset.o obj-$(CONFIG_USERMODE_DRIVER) += usermode_driver.o -obj-$(CONFIG_MODULES) += kmod.o obj-$(CONFIG_MULTIUSER) += groups.o ifdef CONFIG_FUNCTION_TRACER diff --git a/kernel/kmod.c b/kernel/kmod.c deleted file mode 100644 index b717134ebe170..0000000000000 --- a/kernel/kmod.c +++ /dev/null @@ -1,177 +0,0 @@ -/* - * kmod - the kernel module loader - */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -/* - * Assuming: - * - * threads = div64_u64((u64) totalram_pages * (u64) PAGE_SIZE, - * (u64) THREAD_SIZE * 8UL); - * - * If you need less than 50 threads would mean we're dealing with systems - * smaller than 3200 pages. This assumes you are capable of having ~13M memory, - * and this would only be an upper limit, after which the OOM killer would take - * effect. Systems like these are very unlikely if modules are enabled. - */ -#define MAX_KMOD_CONCURRENT 50 -static atomic_t kmod_concurrent_max = ATOMIC_INIT(MAX_KMOD_CONCURRENT); -static DECLARE_WAIT_QUEUE_HEAD(kmod_wq); - -/* - * This is a restriction on having *all* MAX_KMOD_CONCURRENT threads - * running at the same time without returning. When this happens we - * believe you've somehow ended up with a recursive module dependency - * creating a loop. - * - * We have no option but to fail. - * - * Userspace should proactively try to detect and prevent these. - */ -#define MAX_KMOD_ALL_BUSY_TIMEOUT 5 - -/* - modprobe_path is set via /proc/sys. -*/ -char modprobe_path[KMOD_PATH_LEN] = CONFIG_MODPROBE_PATH; - -static void free_modprobe_argv(struct subprocess_info *info) -{ - kfree(info->argv[3]); /* check call_modprobe() */ - kfree(info->argv); -} - -static int call_modprobe(char *module_name, int wait) -{ - struct subprocess_info *info; - static char *envp[] = { - "HOME=/", - "TERM=linux", - "PATH=/sbin:/usr/sbin:/bin:/usr/bin", - NULL - }; - - char **argv = kmalloc(sizeof(char *[5]), GFP_KERNEL); - if (!argv) - goto out; - - module_name = kstrdup(module_name, GFP_KERNEL); - if (!module_name) - goto free_argv; - - argv[0] = modprobe_path; - argv[1] = "-q"; - argv[2] = "--"; - argv[3] = module_name; /* check free_modprobe_argv() */ - argv[4] = NULL; - - info = call_usermodehelper_setup(modprobe_path, argv, envp, GFP_KERNEL, - NULL, free_modprobe_argv, NULL); - if (!info) - goto free_module_name; - - return call_usermodehelper_exec(info, wait | UMH_KILLABLE); - -free_module_name: - kfree(module_name); -free_argv: - kfree(argv); -out: - return -ENOMEM; -} - -/** - * __request_module - try to load a kernel module - * @wait: wait (or not) for the operation to complete - * @fmt: printf style format string for the name of the module - * @...: arguments as specified in the format string - * - * Load a module using the user mode module loader. The function returns - * zero on success or a negative errno code or positive exit code from - * "modprobe" on failure. Note that a successful module load does not mean - * the module did not then unload and exit on an error of its own. Callers - * must check that the service they requested is now available not blindly - * invoke it. - * - * If module auto-loading support is disabled then this function - * simply returns -ENOENT. - */ -int __request_module(bool wait, const char *fmt, ...) -{ - va_list args; - char module_name[MODULE_NAME_LEN]; - int ret; - - /* - * We don't allow synchronous module loading from async. Module - * init may invoke async_synchronize_full() which will end up - * waiting for this task which already is waiting for the module - * loading to complete, leading to a deadlock. - */ - WARN_ON_ONCE(wait && current_is_async()); - - if (!modprobe_path[0]) - return -ENOENT; - - va_start(args, fmt); - ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args); - va_end(args); - if (ret >= MODULE_NAME_LEN) - return -ENAMETOOLONG; - - ret = security_kernel_module_request(module_name); - if (ret) - return ret; - - if (atomic_dec_if_positive(&kmod_concurrent_max) < 0) { - pr_warn_ratelimited("request_module: kmod_concurrent_max (%u) close to 0 (max_modprobes: %u), for module %s, throttling...", - atomic_read(&kmod_concurrent_max), - MAX_KMOD_CONCURRENT, module_name); - ret = wait_event_killable_timeout(kmod_wq, - atomic_dec_if_positive(&kmod_concurrent_max) >= 0, - MAX_KMOD_ALL_BUSY_TIMEOUT * HZ); - if (!ret) { - pr_warn_ratelimited("request_module: modprobe %s cannot be processed, kmod busy with %d threads for more than %d seconds now", - module_name, MAX_KMOD_CONCURRENT, MAX_KMOD_ALL_BUSY_TIMEOUT); - return -ETIME; - } else if (ret == -ERESTARTSYS) { - pr_warn_ratelimited("request_module: sigkill sent for modprobe %s, giving up", module_name); - return ret; - } - } - - trace_module_request(module_name, wait, _RET_IP_); - - ret = call_modprobe(module_name, wait ? UMH_WAIT_PROC : UMH_WAIT_EXEC); - - atomic_inc(&kmod_concurrent_max); - wake_up(&kmod_wq); - - return ret; -} -EXPORT_SYMBOL(__request_module); diff --git a/kernel/module/Makefile b/kernel/module/Makefile index 948efea81e851..5b1d26b53b8d3 100644 --- a/kernel/module/Makefile +++ b/kernel/module/Makefile @@ -7,7 +7,9 @@ # and produce insane amounts of uninteresting coverage. KCOV_INSTRUMENT_module.o := n -obj-y += main.o strict_rwx.o +obj-y += main.o +obj-y += strict_rwx.o +obj-y += kmod.o obj-$(CONFIG_MODULE_DECOMPRESS) += decompress.o obj-$(CONFIG_MODULE_SIG) += signing.o obj-$(CONFIG_LIVEPATCH) += livepatch.o diff --git a/kernel/module/kmod.c b/kernel/module/kmod.c new file mode 100644 index 0000000000000..b717134ebe170 --- /dev/null +++ b/kernel/module/kmod.c @@ -0,0 +1,177 @@ +/* + * kmod - the kernel module loader + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +/* + * Assuming: + * + * threads = div64_u64((u64) totalram_pages * (u64) PAGE_SIZE, + * (u64) THREAD_SIZE * 8UL); + * + * If you need less than 50 threads would mean we're dealing with systems + * smaller than 3200 pages. This assumes you are capable of having ~13M memory, + * and this would only be an upper limit, after which the OOM killer would take + * effect. Systems like these are very unlikely if modules are enabled. + */ +#define MAX_KMOD_CONCURRENT 50 +static atomic_t kmod_concurrent_max = ATOMIC_INIT(MAX_KMOD_CONCURRENT); +static DECLARE_WAIT_QUEUE_HEAD(kmod_wq); + +/* + * This is a restriction on having *all* MAX_KMOD_CONCURRENT threads + * running at the same time without returning. When this happens we + * believe you've somehow ended up with a recursive module dependency + * creating a loop. + * + * We have no option but to fail. + * + * Userspace should proactively try to detect and prevent these. + */ +#define MAX_KMOD_ALL_BUSY_TIMEOUT 5 + +/* + modprobe_path is set via /proc/sys. +*/ +char modprobe_path[KMOD_PATH_LEN] = CONFIG_MODPROBE_PATH; + +static void free_modprobe_argv(struct subprocess_info *info) +{ + kfree(info->argv[3]); /* check call_modprobe() */ + kfree(info->argv); +} + +static int call_modprobe(char *module_name, int wait) +{ + struct subprocess_info *info; + static char *envp[] = { + "HOME=/", + "TERM=linux", + "PATH=/sbin:/usr/sbin:/bin:/usr/bin", + NULL + }; + + char **argv = kmalloc(sizeof(char *[5]), GFP_KERNEL); + if (!argv) + goto out; + + module_name = kstrdup(module_name, GFP_KERNEL); + if (!module_name) + goto free_argv; + + argv[0] = modprobe_path; + argv[1] = "-q"; + argv[2] = "--"; + argv[3] = module_name; /* check free_modprobe_argv() */ + argv[4] = NULL; + + info = call_usermodehelper_setup(modprobe_path, argv, envp, GFP_KERNEL, + NULL, free_modprobe_argv, NULL); + if (!info) + goto free_module_name; + + return call_usermodehelper_exec(info, wait | UMH_KILLABLE); + +free_module_name: + kfree(module_name); +free_argv: + kfree(argv); +out: + return -ENOMEM; +} + +/** + * __request_module - try to load a kernel module + * @wait: wait (or not) for the operation to complete + * @fmt: printf style format string for the name of the module + * @...: arguments as specified in the format string + * + * Load a module using the user mode module loader. The function returns + * zero on success or a negative errno code or positive exit code from + * "modprobe" on failure. Note that a successful module load does not mean + * the module did not then unload and exit on an error of its own. Callers + * must check that the service they requested is now available not blindly + * invoke it. + * + * If module auto-loading support is disabled then this function + * simply returns -ENOENT. + */ +int __request_module(bool wait, const char *fmt, ...) +{ + va_list args; + char module_name[MODULE_NAME_LEN]; + int ret; + + /* + * We don't allow synchronous module loading from async. Module + * init may invoke async_synchronize_full() which will end up + * waiting for this task which already is waiting for the module + * loading to complete, leading to a deadlock. + */ + WARN_ON_ONCE(wait && current_is_async()); + + if (!modprobe_path[0]) + return -ENOENT; + + va_start(args, fmt); + ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args); + va_end(args); + if (ret >= MODULE_NAME_LEN) + return -ENAMETOOLONG; + + ret = security_kernel_module_request(module_name); + if (ret) + return ret; + + if (atomic_dec_if_positive(&kmod_concurrent_max) < 0) { + pr_warn_ratelimited("request_module: kmod_concurrent_max (%u) close to 0 (max_modprobes: %u), for module %s, throttling...", + atomic_read(&kmod_concurrent_max), + MAX_KMOD_CONCURRENT, module_name); + ret = wait_event_killable_timeout(kmod_wq, + atomic_dec_if_positive(&kmod_concurrent_max) >= 0, + MAX_KMOD_ALL_BUSY_TIMEOUT * HZ); + if (!ret) { + pr_warn_ratelimited("request_module: modprobe %s cannot be processed, kmod busy with %d threads for more than %d seconds now", + module_name, MAX_KMOD_CONCURRENT, MAX_KMOD_ALL_BUSY_TIMEOUT); + return -ETIME; + } else if (ret == -ERESTARTSYS) { + pr_warn_ratelimited("request_module: sigkill sent for modprobe %s, giving up", module_name); + return ret; + } + } + + trace_module_request(module_name, wait, _RET_IP_); + + ret = call_modprobe(module_name, wait ? UMH_WAIT_PROC : UMH_WAIT_EXEC); + + atomic_inc(&kmod_concurrent_max); + wake_up(&kmod_wq); + + return ret; +} +EXPORT_SYMBOL(__request_module);