]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - kernel/kmod.c
kmod: split out umh code into its own file
[mirror_ubuntu-jammy-kernel.git] / kernel / kmod.c
CommitLineData
1da177e4 1/*
23558693
LR
2 * kmod - the kernel module loader
3 */
1da177e4
LT
4#include <linux/module.h>
5#include <linux/sched.h>
29930025 6#include <linux/sched/task.h>
5c2c5c55 7#include <linux/binfmts.h>
1da177e4
LT
8#include <linux/syscalls.h>
9#include <linux/unistd.h>
10#include <linux/kmod.h>
1da177e4 11#include <linux/slab.h>
1da177e4 12#include <linux/completion.h>
17f60a7d 13#include <linux/cred.h>
1da177e4 14#include <linux/file.h>
9f3acc31 15#include <linux/fdtable.h>
1da177e4
LT
16#include <linux/workqueue.h>
17#include <linux/security.h>
18#include <linux/mount.h>
19#include <linux/kernel.h>
20#include <linux/init.h>
d025c9db 21#include <linux/resource.h>
8cdd4936
RW
22#include <linux/notifier.h>
23#include <linux/suspend.h>
b298d289 24#include <linux/rwsem.h>
a74fb73c 25#include <linux/ptrace.h>
0fdff3ec 26#include <linux/async.h>
7c0f6ba6 27#include <linux/uaccess.h>
1da177e4 28
7ead8b83
LZ
29#include <trace/events/module.h>
30
a1ef5adb 31#ifdef CONFIG_MODULES
165d1cc0
LR
32/*
33 * Assuming:
34 *
35 * threads = div64_u64((u64) totalram_pages * (u64) PAGE_SIZE,
36 * (u64) THREAD_SIZE * 8UL);
37 *
38 * If you need less than 50 threads would mean we're dealing with systems
39 * smaller than 3200 pages. This assuems you are capable of having ~13M memory,
40 * and this would only be an be an upper limit, after which the OOM killer
41 * would take effect. Systems like these are very unlikely if modules are
42 * enabled.
43 */
44#define MAX_KMOD_CONCURRENT 50
45static atomic_t kmod_concurrent_max = ATOMIC_INIT(MAX_KMOD_CONCURRENT);
6d7964a7 46static DECLARE_WAIT_QUEUE_HEAD(kmod_wq);
1da177e4 47
2ba293c9
LR
48/*
49 * This is a restriction on having *all* MAX_KMOD_CONCURRENT threads
50 * running at the same time without returning. When this happens we
51 * believe you've somehow ended up with a recursive module dependency
52 * creating a loop.
53 *
54 * We have no option but to fail.
55 *
56 * Userspace should proactively try to detect and prevent these.
57 */
58#define MAX_KMOD_ALL_BUSY_TIMEOUT 5
59
1da177e4
LT
60/*
61 modprobe_path is set via /proc/sys.
62*/
63char modprobe_path[KMOD_PATH_LEN] = "/sbin/modprobe";
64
1cc684ab
ON
65static void free_modprobe_argv(struct subprocess_info *info)
66{
67 kfree(info->argv[3]); /* check call_modprobe() */
68 kfree(info->argv);
69}
70
3e63a93b
ON
71static int call_modprobe(char *module_name, int wait)
72{
f634460c 73 struct subprocess_info *info;
3e63a93b
ON
74 static char *envp[] = {
75 "HOME=/",
76 "TERM=linux",
77 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
78 NULL
79 };
80
1cc684ab
ON
81 char **argv = kmalloc(sizeof(char *[5]), GFP_KERNEL);
82 if (!argv)
83 goto out;
84
85 module_name = kstrdup(module_name, GFP_KERNEL);
86 if (!module_name)
87 goto free_argv;
88
89 argv[0] = modprobe_path;
90 argv[1] = "-q";
91 argv[2] = "--";
92 argv[3] = module_name; /* check free_modprobe_argv() */
93 argv[4] = NULL;
3e63a93b 94
f634460c
LDM
95 info = call_usermodehelper_setup(modprobe_path, argv, envp, GFP_KERNEL,
96 NULL, free_modprobe_argv, NULL);
97 if (!info)
98 goto free_module_name;
99
100 return call_usermodehelper_exec(info, wait | UMH_KILLABLE);
101
102free_module_name:
103 kfree(module_name);
1cc684ab
ON
104free_argv:
105 kfree(argv);
106out:
107 return -ENOMEM;
3e63a93b
ON
108}
109
1da177e4 110/**
acae0515
AV
111 * __request_module - try to load a kernel module
112 * @wait: wait (or not) for the operation to complete
bd4207c9
RD
113 * @fmt: printf style format string for the name of the module
114 * @...: arguments as specified in the format string
1da177e4
LT
115 *
116 * Load a module using the user mode module loader. The function returns
60b61a6f
N
117 * zero on success or a negative errno code or positive exit code from
118 * "modprobe" on failure. Note that a successful module load does not mean
119 * the module did not then unload and exit on an error of its own. Callers
120 * must check that the service they requested is now available not blindly
121 * invoke it.
1da177e4
LT
122 *
123 * If module auto-loading support is disabled then this function
124 * becomes a no-operation.
125 */
acae0515 126int __request_module(bool wait, const char *fmt, ...)
1da177e4
LT
127{
128 va_list args;
129 char module_name[MODULE_NAME_LEN];
1da177e4 130 int ret;
1da177e4 131
0fdff3ec
TH
132 /*
133 * We don't allow synchronous module loading from async. Module
134 * init may invoke async_synchronize_full() which will end up
135 * waiting for this task which already is waiting for the module
136 * loading to complete, leading to a deadlock.
137 */
138 WARN_ON_ONCE(wait && current_is_async());
139
7f57cfa4
ON
140 if (!modprobe_path[0])
141 return 0;
142
1da177e4
LT
143 va_start(args, fmt);
144 ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);
145 va_end(args);
146 if (ret >= MODULE_NAME_LEN)
147 return -ENAMETOOLONG;
148
dd8dbf2e
EP
149 ret = security_kernel_module_request(module_name);
150 if (ret)
151 return ret;
152
165d1cc0 153 if (atomic_dec_if_positive(&kmod_concurrent_max) < 0) {
6d7964a7
LR
154 pr_warn_ratelimited("request_module: kmod_concurrent_max (%u) close to 0 (max_modprobes: %u), for module %s, throttling...",
155 atomic_read(&kmod_concurrent_max),
156 MAX_KMOD_CONCURRENT, module_name);
2ba293c9
LR
157 ret = wait_event_killable_timeout(kmod_wq,
158 atomic_dec_if_positive(&kmod_concurrent_max) >= 0,
159 MAX_KMOD_ALL_BUSY_TIMEOUT * HZ);
160 if (!ret) {
161 pr_warn_ratelimited("request_module: modprobe %s cannot be processed, kmod busy with %d threads for more than %d seconds now",
162 module_name, MAX_KMOD_CONCURRENT, MAX_KMOD_ALL_BUSY_TIMEOUT);
163 return -ETIME;
164 } else if (ret == -ERESTARTSYS) {
165 pr_warn_ratelimited("request_module: sigkill sent for modprobe %s, giving up", module_name);
166 return ret;
167 }
1da177e4
LT
168 }
169
7ead8b83
LZ
170 trace_module_request(module_name, wait, _RET_IP_);
171
3e63a93b 172 ret = call_modprobe(module_name, wait ? UMH_WAIT_PROC : UMH_WAIT_EXEC);
a06a4dc3 173
165d1cc0 174 atomic_inc(&kmod_concurrent_max);
6d7964a7 175 wake_up(&kmod_wq);
165d1cc0 176
1da177e4
LT
177 return ret;
178}
acae0515 179EXPORT_SYMBOL(__request_module);
165d1cc0 180
118a9069 181#endif /* CONFIG_MODULES */