aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/module.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/module.c')
-rw-r--r--kernel/module.c330
1 files changed, 249 insertions, 81 deletions
diff --git a/kernel/module.c b/kernel/module.c
index cfc9e843a924..4d2b82e610e2 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -18,7 +18,7 @@
18*/ 18*/
19#include <linux/export.h> 19#include <linux/export.h>
20#include <linux/moduleloader.h> 20#include <linux/moduleloader.h>
21#include <linux/ftrace_event.h> 21#include <linux/trace_events.h>
22#include <linux/init.h> 22#include <linux/init.h>
23#include <linux/kallsyms.h> 23#include <linux/kallsyms.h>
24#include <linux/file.h> 24#include <linux/file.h>
@@ -101,48 +101,201 @@
101DEFINE_MUTEX(module_mutex); 101DEFINE_MUTEX(module_mutex);
102EXPORT_SYMBOL_GPL(module_mutex); 102EXPORT_SYMBOL_GPL(module_mutex);
103static LIST_HEAD(modules); 103static LIST_HEAD(modules);
104#ifdef CONFIG_KGDB_KDB
105struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
106#endif /* CONFIG_KGDB_KDB */
107 104
108#ifdef CONFIG_MODULE_SIG 105#ifdef CONFIG_MODULES_TREE_LOOKUP
109#ifdef CONFIG_MODULE_SIG_FORCE 106
110static bool sig_enforce = true; 107/*
111#else 108 * Use a latched RB-tree for __module_address(); this allows us to use
112static bool sig_enforce = false; 109 * RCU-sched lookups of the address from any context.
110 *
111 * Because modules have two address ranges: init and core, we need two
112 * latch_tree_nodes entries. Therefore we need the back-pointer from
113 * mod_tree_node.
114 *
115 * Because init ranges are short lived we mark them unlikely and have placed
116 * them outside the critical cacheline in struct module.
117 *
118 * This is conditional on PERF_EVENTS || TRACING because those can really hit
119 * __module_address() hard by doing a lot of stack unwinding; potentially from
120 * NMI context.
121 */
113 122
114static int param_set_bool_enable_only(const char *val, 123static __always_inline unsigned long __mod_tree_val(struct latch_tree_node *n)
115 const struct kernel_param *kp)
116{ 124{
117 int err; 125 struct mod_tree_node *mtn = container_of(n, struct mod_tree_node, node);
118 bool test; 126 struct module *mod = mtn->mod;
119 struct kernel_param dummy_kp = *kp;
120 127
121 dummy_kp.arg = &test; 128 if (unlikely(mtn == &mod->mtn_init))
129 return (unsigned long)mod->module_init;
122 130
123 err = param_set_bool(val, &dummy_kp); 131 return (unsigned long)mod->module_core;
124 if (err) 132}
125 return err; 133
134static __always_inline unsigned long __mod_tree_size(struct latch_tree_node *n)
135{
136 struct mod_tree_node *mtn = container_of(n, struct mod_tree_node, node);
137 struct module *mod = mtn->mod;
138
139 if (unlikely(mtn == &mod->mtn_init))
140 return (unsigned long)mod->init_size;
126 141
127 /* Don't let them unset it once it's set! */ 142 return (unsigned long)mod->core_size;
128 if (!test && sig_enforce) 143}
129 return -EROFS; 144
145static __always_inline bool
146mod_tree_less(struct latch_tree_node *a, struct latch_tree_node *b)
147{
148 return __mod_tree_val(a) < __mod_tree_val(b);
149}
150
151static __always_inline int
152mod_tree_comp(void *key, struct latch_tree_node *n)
153{
154 unsigned long val = (unsigned long)key;
155 unsigned long start, end;
156
157 start = __mod_tree_val(n);
158 if (val < start)
159 return -1;
160
161 end = start + __mod_tree_size(n);
162 if (val >= end)
163 return 1;
130 164
131 if (test)
132 sig_enforce = true;
133 return 0; 165 return 0;
134} 166}
135 167
136static const struct kernel_param_ops param_ops_bool_enable_only = { 168static const struct latch_tree_ops mod_tree_ops = {
137 .flags = KERNEL_PARAM_OPS_FL_NOARG, 169 .less = mod_tree_less,
138 .set = param_set_bool_enable_only, 170 .comp = mod_tree_comp,
139 .get = param_get_bool,
140}; 171};
141#define param_check_bool_enable_only param_check_bool
142 172
173static struct mod_tree_root {
174 struct latch_tree_root root;
175 unsigned long addr_min;
176 unsigned long addr_max;
177} mod_tree __cacheline_aligned = {
178 .addr_min = -1UL,
179};
180
181#define module_addr_min mod_tree.addr_min
182#define module_addr_max mod_tree.addr_max
183
184static noinline void __mod_tree_insert(struct mod_tree_node *node)
185{
186 latch_tree_insert(&node->node, &mod_tree.root, &mod_tree_ops);
187}
188
189static void __mod_tree_remove(struct mod_tree_node *node)
190{
191 latch_tree_erase(&node->node, &mod_tree.root, &mod_tree_ops);
192}
193
194/*
195 * These modifications: insert, remove_init and remove; are serialized by the
196 * module_mutex.
197 */
198static void mod_tree_insert(struct module *mod)
199{
200 mod->mtn_core.mod = mod;
201 mod->mtn_init.mod = mod;
202
203 __mod_tree_insert(&mod->mtn_core);
204 if (mod->init_size)
205 __mod_tree_insert(&mod->mtn_init);
206}
207
208static void mod_tree_remove_init(struct module *mod)
209{
210 if (mod->init_size)
211 __mod_tree_remove(&mod->mtn_init);
212}
213
214static void mod_tree_remove(struct module *mod)
215{
216 __mod_tree_remove(&mod->mtn_core);
217 mod_tree_remove_init(mod);
218}
219
220static struct module *mod_find(unsigned long addr)
221{
222 struct latch_tree_node *ltn;
223
224 ltn = latch_tree_find((void *)addr, &mod_tree.root, &mod_tree_ops);
225 if (!ltn)
226 return NULL;
227
228 return container_of(ltn, struct mod_tree_node, node)->mod;
229}
230
231#else /* MODULES_TREE_LOOKUP */
232
233static unsigned long module_addr_min = -1UL, module_addr_max = 0;
234
235static void mod_tree_insert(struct module *mod) { }
236static void mod_tree_remove_init(struct module *mod) { }
237static void mod_tree_remove(struct module *mod) { }
238
239static struct module *mod_find(unsigned long addr)
240{
241 struct module *mod;
242
243 list_for_each_entry_rcu(mod, &modules, list) {
244 if (within_module(addr, mod))
245 return mod;
246 }
247
248 return NULL;
249}
250
251#endif /* MODULES_TREE_LOOKUP */
252
253/*
254 * Bounds of module text, for speeding up __module_address.
255 * Protected by module_mutex.
256 */
257static void __mod_update_bounds(void *base, unsigned int size)
258{
259 unsigned long min = (unsigned long)base;
260 unsigned long max = min + size;
261
262 if (min < module_addr_min)
263 module_addr_min = min;
264 if (max > module_addr_max)
265 module_addr_max = max;
266}
267
268static void mod_update_bounds(struct module *mod)
269{
270 __mod_update_bounds(mod->module_core, mod->core_size);
271 if (mod->init_size)
272 __mod_update_bounds(mod->module_init, mod->init_size);
273}
274
275#ifdef CONFIG_KGDB_KDB
276struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
277#endif /* CONFIG_KGDB_KDB */
278
279static void module_assert_mutex(void)
280{
281 lockdep_assert_held(&module_mutex);
282}
283
284static void module_assert_mutex_or_preempt(void)
285{
286#ifdef CONFIG_LOCKDEP
287 if (unlikely(!debug_locks))
288 return;
289
290 WARN_ON(!rcu_read_lock_sched_held() &&
291 !lockdep_is_held(&module_mutex));
292#endif
293}
294
295static bool sig_enforce = IS_ENABLED(CONFIG_MODULE_SIG_FORCE);
296#ifndef CONFIG_MODULE_SIG_FORCE
143module_param(sig_enforce, bool_enable_only, 0644); 297module_param(sig_enforce, bool_enable_only, 0644);
144#endif /* !CONFIG_MODULE_SIG_FORCE */ 298#endif /* !CONFIG_MODULE_SIG_FORCE */
145#endif /* CONFIG_MODULE_SIG */
146 299
147/* Block module loading/unloading? */ 300/* Block module loading/unloading? */
148int modules_disabled = 0; 301int modules_disabled = 0;
@@ -153,10 +306,6 @@ static DECLARE_WAIT_QUEUE_HEAD(module_wq);
153 306
154static BLOCKING_NOTIFIER_HEAD(module_notify_list); 307static BLOCKING_NOTIFIER_HEAD(module_notify_list);
155 308
156/* Bounds of module allocation, for speeding __module_address.
157 * Protected by module_mutex. */
158static unsigned long module_addr_min = -1UL, module_addr_max = 0;
159
160int register_module_notifier(struct notifier_block *nb) 309int register_module_notifier(struct notifier_block *nb)
161{ 310{
162 return blocking_notifier_chain_register(&module_notify_list, nb); 311 return blocking_notifier_chain_register(&module_notify_list, nb);
@@ -318,6 +467,8 @@ bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
318#endif 467#endif
319 }; 468 };
320 469
470 module_assert_mutex_or_preempt();
471
321 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data)) 472 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
322 return true; 473 return true;
323 474
@@ -457,6 +608,8 @@ static struct module *find_module_all(const char *name, size_t len,
457{ 608{
458 struct module *mod; 609 struct module *mod;
459 610
611 module_assert_mutex();
612
460 list_for_each_entry(mod, &modules, list) { 613 list_for_each_entry(mod, &modules, list) {
461 if (!even_unformed && mod->state == MODULE_STATE_UNFORMED) 614 if (!even_unformed && mod->state == MODULE_STATE_UNFORMED)
462 continue; 615 continue;
@@ -1169,11 +1322,17 @@ static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1169{ 1322{
1170 const unsigned long *crc; 1323 const unsigned long *crc;
1171 1324
1172 /* Since this should be found in kernel (which can't be removed), 1325 /*
1173 * no locking is necessary. */ 1326 * Since this should be found in kernel (which can't be removed), no
1327 * locking is necessary -- use preempt_disable() to placate lockdep.
1328 */
1329 preempt_disable();
1174 if (!find_symbol(VMLINUX_SYMBOL_STR(module_layout), NULL, 1330 if (!find_symbol(VMLINUX_SYMBOL_STR(module_layout), NULL,
1175 &crc, true, false)) 1331 &crc, true, false)) {
1332 preempt_enable();
1176 BUG(); 1333 BUG();
1334 }
1335 preempt_enable();
1177 return check_version(sechdrs, versindex, 1336 return check_version(sechdrs, versindex,
1178 VMLINUX_SYMBOL_STR(module_layout), mod, crc, 1337 VMLINUX_SYMBOL_STR(module_layout), mod, crc,
1179 NULL); 1338 NULL);
@@ -1661,6 +1820,10 @@ static void mod_sysfs_fini(struct module *mod)
1661 mod_kobject_put(mod); 1820 mod_kobject_put(mod);
1662} 1821}
1663 1822
1823static void init_param_lock(struct module *mod)
1824{
1825 mutex_init(&mod->param_lock);
1826}
1664#else /* !CONFIG_SYSFS */ 1827#else /* !CONFIG_SYSFS */
1665 1828
1666static int mod_sysfs_setup(struct module *mod, 1829static int mod_sysfs_setup(struct module *mod,
@@ -1683,6 +1846,9 @@ static void del_usage_links(struct module *mod)
1683{ 1846{
1684} 1847}
1685 1848
1849static void init_param_lock(struct module *mod)
1850{
1851}
1686#endif /* CONFIG_SYSFS */ 1852#endif /* CONFIG_SYSFS */
1687 1853
1688static void mod_sysfs_teardown(struct module *mod) 1854static void mod_sysfs_teardown(struct module *mod)
@@ -1852,10 +2018,11 @@ static void free_module(struct module *mod)
1852 mutex_lock(&module_mutex); 2018 mutex_lock(&module_mutex);
1853 /* Unlink carefully: kallsyms could be walking list. */ 2019 /* Unlink carefully: kallsyms could be walking list. */
1854 list_del_rcu(&mod->list); 2020 list_del_rcu(&mod->list);
2021 mod_tree_remove(mod);
1855 /* Remove this module from bug list, this uses list_del_rcu */ 2022 /* Remove this module from bug list, this uses list_del_rcu */
1856 module_bug_cleanup(mod); 2023 module_bug_cleanup(mod);
1857 /* Wait for RCU synchronizing before releasing mod->list and buglist. */ 2024 /* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */
1858 synchronize_rcu(); 2025 synchronize_sched();
1859 mutex_unlock(&module_mutex); 2026 mutex_unlock(&module_mutex);
1860 2027
1861 /* This may be NULL, but that's OK */ 2028 /* This may be NULL, but that's OK */
@@ -2384,22 +2551,6 @@ void * __weak module_alloc(unsigned long size)
2384 return vmalloc_exec(size); 2551 return vmalloc_exec(size);
2385} 2552}
2386 2553
2387static void *module_alloc_update_bounds(unsigned long size)
2388{
2389 void *ret = module_alloc(size);
2390
2391 if (ret) {
2392 mutex_lock(&module_mutex);
2393 /* Update module bounds. */
2394 if ((unsigned long)ret < module_addr_min)
2395 module_addr_min = (unsigned long)ret;
2396 if ((unsigned long)ret + size > module_addr_max)
2397 module_addr_max = (unsigned long)ret + size;
2398 mutex_unlock(&module_mutex);
2399 }
2400 return ret;
2401}
2402
2403#ifdef CONFIG_DEBUG_KMEMLEAK 2554#ifdef CONFIG_DEBUG_KMEMLEAK
2404static void kmemleak_load_module(const struct module *mod, 2555static void kmemleak_load_module(const struct module *mod,
2405 const struct load_info *info) 2556 const struct load_info *info)
@@ -2805,7 +2956,7 @@ static int move_module(struct module *mod, struct load_info *info)
2805 void *ptr; 2956 void *ptr;
2806 2957
2807 /* Do the allocs. */ 2958 /* Do the allocs. */
2808 ptr = module_alloc_update_bounds(mod->core_size); 2959 ptr = module_alloc(mod->core_size);
2809 /* 2960 /*
2810 * The pointer to this block is stored in the module structure 2961 * The pointer to this block is stored in the module structure
2811 * which is inside the block. Just mark it as not being a 2962 * which is inside the block. Just mark it as not being a
@@ -2819,7 +2970,7 @@ static int move_module(struct module *mod, struct load_info *info)
2819 mod->module_core = ptr; 2970 mod->module_core = ptr;
2820 2971
2821 if (mod->init_size) { 2972 if (mod->init_size) {
2822 ptr = module_alloc_update_bounds(mod->init_size); 2973 ptr = module_alloc(mod->init_size);
2823 /* 2974 /*
2824 * The pointer to this block is stored in the module structure 2975 * The pointer to this block is stored in the module structure
2825 * which is inside the block. This block doesn't need to be 2976 * which is inside the block. This block doesn't need to be
@@ -3107,7 +3258,7 @@ static noinline int do_init_module(struct module *mod)
3107 * 3258 *
3108 * http://thread.gmane.org/gmane.linux.kernel/1420814 3259 * http://thread.gmane.org/gmane.linux.kernel/1420814
3109 */ 3260 */
3110 if (current->flags & PF_USED_ASYNC) 3261 if (!mod->async_probe_requested && (current->flags & PF_USED_ASYNC))
3111 async_synchronize_full(); 3262 async_synchronize_full();
3112 3263
3113 mutex_lock(&module_mutex); 3264 mutex_lock(&module_mutex);
@@ -3119,6 +3270,7 @@ static noinline int do_init_module(struct module *mod)
3119 mod->symtab = mod->core_symtab; 3270 mod->symtab = mod->core_symtab;
3120 mod->strtab = mod->core_strtab; 3271 mod->strtab = mod->core_strtab;
3121#endif 3272#endif
3273 mod_tree_remove_init(mod);
3122 unset_module_init_ro_nx(mod); 3274 unset_module_init_ro_nx(mod);
3123 module_arch_freeing_init(mod); 3275 module_arch_freeing_init(mod);
3124 mod->module_init = NULL; 3276 mod->module_init = NULL;
@@ -3127,11 +3279,11 @@ static noinline int do_init_module(struct module *mod)
3127 mod->init_text_size = 0; 3279 mod->init_text_size = 0;
3128 /* 3280 /*
3129 * We want to free module_init, but be aware that kallsyms may be 3281 * We want to free module_init, but be aware that kallsyms may be
3130 * walking this with preempt disabled. In all the failure paths, 3282 * walking this with preempt disabled. In all the failure paths, we
3131 * we call synchronize_rcu/synchronize_sched, but we don't want 3283 * call synchronize_sched(), but we don't want to slow down the success
3132 * to slow down the success path, so use actual RCU here. 3284 * path, so use actual RCU here.
3133 */ 3285 */
3134 call_rcu(&freeinit->rcu, do_free_init); 3286 call_rcu_sched(&freeinit->rcu, do_free_init);
3135 mutex_unlock(&module_mutex); 3287 mutex_unlock(&module_mutex);
3136 wake_up_all(&module_wq); 3288 wake_up_all(&module_wq);
3137 3289
@@ -3188,7 +3340,9 @@ again:
3188 err = -EEXIST; 3340 err = -EEXIST;
3189 goto out; 3341 goto out;
3190 } 3342 }
3343 mod_update_bounds(mod);
3191 list_add_rcu(&mod->list, &modules); 3344 list_add_rcu(&mod->list, &modules);
3345 mod_tree_insert(mod);
3192 err = 0; 3346 err = 0;
3193 3347
3194out: 3348out:
@@ -3237,10 +3391,19 @@ out:
3237 return err; 3391 return err;
3238} 3392}
3239 3393
3240static int unknown_module_param_cb(char *param, char *val, const char *modname) 3394static int unknown_module_param_cb(char *param, char *val, const char *modname,
3395 void *arg)
3241{ 3396{
3397 struct module *mod = arg;
3398 int ret;
3399
3400 if (strcmp(param, "async_probe") == 0) {
3401 mod->async_probe_requested = true;
3402 return 0;
3403 }
3404
3242 /* Check for magic 'dyndbg' arg */ 3405 /* Check for magic 'dyndbg' arg */
3243 int ret = ddebug_dyndbg_module_param_cb(param, val, modname); 3406 ret = ddebug_dyndbg_module_param_cb(param, val, modname);
3244 if (ret != 0) 3407 if (ret != 0)
3245 pr_warn("%s: unknown parameter '%s' ignored\n", modname, param); 3408 pr_warn("%s: unknown parameter '%s' ignored\n", modname, param);
3246 return 0; 3409 return 0;
@@ -3295,6 +3458,8 @@ static int load_module(struct load_info *info, const char __user *uargs,
3295 if (err) 3458 if (err)
3296 goto unlink_mod; 3459 goto unlink_mod;
3297 3460
3461 init_param_lock(mod);
3462
3298 /* Now we've got everything in the final locations, we can 3463 /* Now we've got everything in the final locations, we can
3299 * find optional sections. */ 3464 * find optional sections. */
3300 err = find_module_sections(mod, info); 3465 err = find_module_sections(mod, info);
@@ -3342,7 +3507,8 @@ static int load_module(struct load_info *info, const char __user *uargs,
3342 3507
3343 /* Module is ready to execute: parsing args may do that. */ 3508 /* Module is ready to execute: parsing args may do that. */
3344 after_dashes = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, 3509 after_dashes = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
3345 -32768, 32767, unknown_module_param_cb); 3510 -32768, 32767, NULL,
3511 unknown_module_param_cb);
3346 if (IS_ERR(after_dashes)) { 3512 if (IS_ERR(after_dashes)) {
3347 err = PTR_ERR(after_dashes); 3513 err = PTR_ERR(after_dashes);
3348 goto bug_cleanup; 3514 goto bug_cleanup;
@@ -3391,9 +3557,10 @@ static int load_module(struct load_info *info, const char __user *uargs,
3391 mutex_lock(&module_mutex); 3557 mutex_lock(&module_mutex);
3392 /* Unlink carefully: kallsyms could be walking list. */ 3558 /* Unlink carefully: kallsyms could be walking list. */
3393 list_del_rcu(&mod->list); 3559 list_del_rcu(&mod->list);
3560 mod_tree_remove(mod);
3394 wake_up_all(&module_wq); 3561 wake_up_all(&module_wq);
3395 /* Wait for RCU synchronizing before releasing mod->list. */ 3562 /* Wait for RCU-sched synchronizing before releasing mod->list. */
3396 synchronize_rcu(); 3563 synchronize_sched();
3397 mutex_unlock(&module_mutex); 3564 mutex_unlock(&module_mutex);
3398 free_module: 3565 free_module:
3399 /* Free lock-classes; relies on the preceding sync_rcu() */ 3566 /* Free lock-classes; relies on the preceding sync_rcu() */
@@ -3517,19 +3684,15 @@ const char *module_address_lookup(unsigned long addr,
3517 char **modname, 3684 char **modname,
3518 char *namebuf) 3685 char *namebuf)
3519{ 3686{
3520 struct module *mod;
3521 const char *ret = NULL; 3687 const char *ret = NULL;
3688 struct module *mod;
3522 3689
3523 preempt_disable(); 3690 preempt_disable();
3524 list_for_each_entry_rcu(mod, &modules, list) { 3691 mod = __module_address(addr);
3525 if (mod->state == MODULE_STATE_UNFORMED) 3692 if (mod) {
3526 continue; 3693 if (modname)
3527 if (within_module(addr, mod)) { 3694 *modname = mod->name;
3528 if (modname) 3695 ret = get_ksymbol(mod, addr, size, offset);
3529 *modname = mod->name;
3530 ret = get_ksymbol(mod, addr, size, offset);
3531 break;
3532 }
3533 } 3696 }
3534 /* Make a copy in here where it's safe */ 3697 /* Make a copy in here where it's safe */
3535 if (ret) { 3698 if (ret) {
@@ -3537,6 +3700,7 @@ const char *module_address_lookup(unsigned long addr,
3537 ret = namebuf; 3700 ret = namebuf;
3538 } 3701 }
3539 preempt_enable(); 3702 preempt_enable();
3703
3540 return ret; 3704 return ret;
3541} 3705}
3542 3706
@@ -3660,6 +3824,8 @@ int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
3660 unsigned int i; 3824 unsigned int i;
3661 int ret; 3825 int ret;
3662 3826
3827 module_assert_mutex();
3828
3663 list_for_each_entry(mod, &modules, list) { 3829 list_for_each_entry(mod, &modules, list) {
3664 if (mod->state == MODULE_STATE_UNFORMED) 3830 if (mod->state == MODULE_STATE_UNFORMED)
3665 continue; 3831 continue;
@@ -3834,13 +4000,15 @@ struct module *__module_address(unsigned long addr)
3834 if (addr < module_addr_min || addr > module_addr_max) 4000 if (addr < module_addr_min || addr > module_addr_max)
3835 return NULL; 4001 return NULL;
3836 4002
3837 list_for_each_entry_rcu(mod, &modules, list) { 4003 module_assert_mutex_or_preempt();
4004
4005 mod = mod_find(addr);
4006 if (mod) {
4007 BUG_ON(!within_module(addr, mod));
3838 if (mod->state == MODULE_STATE_UNFORMED) 4008 if (mod->state == MODULE_STATE_UNFORMED)
3839 continue; 4009 mod = NULL;
3840 if (within_module(addr, mod))
3841 return mod;
3842 } 4010 }
3843 return NULL; 4011 return mod;
3844} 4012}
3845EXPORT_SYMBOL_GPL(__module_address); 4013EXPORT_SYMBOL_GPL(__module_address);
3846 4014