2 Copyright (C) 2002 Richard Henderson
3 Copyright (C) 2001 Rusty Russell, 2002, 2010 Rusty Russell IBM.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/export.h>
20 #include <linux/extable.h>
21 #include <linux/moduleloader.h>
22 #include <linux/trace_events.h>
23 #include <linux/init.h>
24 #include <linux/kallsyms.h>
25 #include <linux/file.h>
27 #include <linux/sysfs.h>
28 #include <linux/kernel.h>
29 #include <linux/slab.h>
30 #include <linux/vmalloc.h>
31 #include <linux/elf.h>
32 #include <linux/proc_fs.h>
33 #include <linux/security.h>
34 #include <linux/seq_file.h>
35 #include <linux/syscalls.h>
36 #include <linux/fcntl.h>
37 #include <linux/rcupdate.h>
38 #include <linux/capability.h>
39 #include <linux/cpu.h>
40 #include <linux/moduleparam.h>
41 #include <linux/errno.h>
42 #include <linux/err.h>
43 #include <linux/vermagic.h>
44 #include <linux/notifier.h>
45 #include <linux/sched.h>
46 #include <linux/device.h>
47 #include <linux/string.h>
48 #include <linux/mutex.h>
49 #include <linux/rculist.h>
50 #include <linux/uaccess.h>
51 #include <asm/cacheflush.h>
52 #include <linux/set_memory.h>
53 #include <asm/mmu_context.h>
54 #include <linux/license.h>
55 #include <asm/sections.h>
56 #include <linux/tracepoint.h>
57 #include <linux/ftrace.h>
58 #include <linux/livepatch.h>
59 #include <linux/async.h>
60 #include <linux/percpu.h>
61 #include <linux/kmemleak.h>
62 #include <linux/jump_label.h>
63 #include <linux/pfn.h>
64 #include <linux/bsearch.h>
65 #include <linux/dynamic_debug.h>
66 #include <linux/audit.h>
67 #include <uapi/linux/module.h>
68 #include "module-internal.h"
70 #define CREATE_TRACE_POINTS
71 #include <trace/events/module.h>
73 #ifndef ARCH_SHF_SMALL
74 #define ARCH_SHF_SMALL 0
78 * Modules' sections will be aligned on page boundaries
79 * to ensure complete separation of code and data
81 # define debug_align(X) ALIGN(X, PAGE_SIZE)
83 /* If this is set, the section belongs in the init part of the module */
84 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
88 * 1) List of modules (also safely readable with preempt_disable),
89 * 2) module_use links,
90 * 3) module_addr_min/module_addr_max.
91 * (delete and add uses RCU list operations). */
92 DEFINE_MUTEX(module_mutex);
93 EXPORT_SYMBOL_GPL(module_mutex);
94 static LIST_HEAD(modules);
96 #ifdef CONFIG_MODULES_TREE_LOOKUP
99 * Use a latched RB-tree for __module_address(); this allows us to use
100 * RCU-sched lookups of the address from any context.
102 * This is conditional on PERF_EVENTS || TRACING because those can really hit
103 * __module_address() hard by doing a lot of stack unwinding; potentially from
107 static __always_inline unsigned long __mod_tree_val(struct latch_tree_node *n)
109 struct module_layout *layout = container_of(n, struct module_layout, mtn.node);
111 return (unsigned long)layout->base;
114 static __always_inline unsigned long __mod_tree_size(struct latch_tree_node *n)
116 struct module_layout *layout = container_of(n, struct module_layout, mtn.node);
118 return (unsigned long)layout->size;
121 static __always_inline bool
122 mod_tree_less(struct latch_tree_node *a, struct latch_tree_node *b)
124 return __mod_tree_val(a) < __mod_tree_val(b);
127 static __always_inline int
128 mod_tree_comp(void *key, struct latch_tree_node *n)
130 unsigned long val = (unsigned long)key;
131 unsigned long start, end;
133 start = __mod_tree_val(n);
137 end = start + __mod_tree_size(n);
144 static const struct latch_tree_ops mod_tree_ops = {
145 .less = mod_tree_less,
146 .comp = mod_tree_comp,
149 static struct mod_tree_root {
150 struct latch_tree_root root;
151 unsigned long addr_min;
152 unsigned long addr_max;
153 } mod_tree __cacheline_aligned = {
157 #define module_addr_min mod_tree.addr_min
158 #define module_addr_max mod_tree.addr_max
160 static noinline void __mod_tree_insert(struct mod_tree_node *node)
162 latch_tree_insert(&node->node, &mod_tree.root, &mod_tree_ops);
165 static void __mod_tree_remove(struct mod_tree_node *node)
167 latch_tree_erase(&node->node, &mod_tree.root, &mod_tree_ops);
171 * These modifications: insert, remove_init and remove; are serialized by the
174 static void mod_tree_insert(struct module *mod)
176 mod->core_layout.mtn.mod = mod;
177 mod->init_layout.mtn.mod = mod;
179 __mod_tree_insert(&mod->core_layout.mtn);
180 if (mod->init_layout.size)
181 __mod_tree_insert(&mod->init_layout.mtn);
184 static void mod_tree_remove_init(struct module *mod)
186 if (mod->init_layout.size)
187 __mod_tree_remove(&mod->init_layout.mtn);
190 static void mod_tree_remove(struct module *mod)
192 __mod_tree_remove(&mod->core_layout.mtn);
193 mod_tree_remove_init(mod);
196 static struct module *mod_find(unsigned long addr)
198 struct latch_tree_node *ltn;
200 ltn = latch_tree_find((void *)addr, &mod_tree.root, &mod_tree_ops);
204 return container_of(ltn, struct mod_tree_node, node)->mod;
207 #else /* MODULES_TREE_LOOKUP */
209 static unsigned long module_addr_min = -1UL, module_addr_max = 0;
211 static void mod_tree_insert(struct module *mod) { }
212 static void mod_tree_remove_init(struct module *mod) { }
213 static void mod_tree_remove(struct module *mod) { }
215 static struct module *mod_find(unsigned long addr)
219 list_for_each_entry_rcu(mod, &modules, list) {
220 if (within_module(addr, mod))
227 #endif /* MODULES_TREE_LOOKUP */
230 * Bounds of module text, for speeding up __module_address.
231 * Protected by module_mutex.
233 static void __mod_update_bounds(void *base, unsigned int size)
235 unsigned long min = (unsigned long)base;
236 unsigned long max = min + size;
238 if (min < module_addr_min)
239 module_addr_min = min;
240 if (max > module_addr_max)
241 module_addr_max = max;
244 static void mod_update_bounds(struct module *mod)
246 __mod_update_bounds(mod->core_layout.base, mod->core_layout.size);
247 if (mod->init_layout.size)
248 __mod_update_bounds(mod->init_layout.base, mod->init_layout.size);
251 #ifdef CONFIG_KGDB_KDB
252 struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
253 #endif /* CONFIG_KGDB_KDB */
255 static void module_assert_mutex(void)
257 lockdep_assert_held(&module_mutex);
260 static void module_assert_mutex_or_preempt(void)
262 #ifdef CONFIG_LOCKDEP
263 if (unlikely(!debug_locks))
266 WARN_ON_ONCE(!rcu_read_lock_sched_held() &&
267 !lockdep_is_held(&module_mutex));
271 static bool sig_enforce = IS_ENABLED(CONFIG_MODULE_SIG_FORCE);
272 module_param(sig_enforce, bool_enable_only, 0644);
275 * Export sig_enforce kernel cmdline parameter to allow other subsystems rely
276 * on that instead of directly to CONFIG_MODULE_SIG_FORCE config.
278 bool is_module_sig_enforced(void)
282 EXPORT_SYMBOL(is_module_sig_enforced);
284 /* Block module loading/unloading? */
285 int modules_disabled = 0;
286 core_param(nomodule, modules_disabled, bint, 0);
288 /* Waiting for a module to finish initializing? */
289 static DECLARE_WAIT_QUEUE_HEAD(module_wq);
291 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
293 int register_module_notifier(struct notifier_block *nb)
295 return blocking_notifier_chain_register(&module_notify_list, nb);
297 EXPORT_SYMBOL(register_module_notifier);
299 int unregister_module_notifier(struct notifier_block *nb)
301 return blocking_notifier_chain_unregister(&module_notify_list, nb);
303 EXPORT_SYMBOL(unregister_module_notifier);
306 * We require a truly strong try_module_get(): 0 means success.
307 * Otherwise an error is returned due to ongoing or failed
308 * initialization etc.
310 static inline int strong_try_module_get(struct module *mod)
312 BUG_ON(mod && mod->state == MODULE_STATE_UNFORMED);
313 if (mod && mod->state == MODULE_STATE_COMING)
315 if (try_module_get(mod))
321 static inline void add_taint_module(struct module *mod, unsigned flag,
322 enum lockdep_ok lockdep_ok)
324 add_taint(flag, lockdep_ok);
325 set_bit(flag, &mod->taints);
329 * A thread that wants to hold a reference to a module only while it
330 * is running can call this to safely exit. nfsd and lockd use this.
332 void __noreturn __module_put_and_exit(struct module *mod, long code)
337 EXPORT_SYMBOL(__module_put_and_exit);
339 /* Find a module section: 0 means not found. */
340 static unsigned int find_sec(const struct load_info *info, const char *name)
344 for (i = 1; i < info->hdr->e_shnum; i++) {
345 Elf_Shdr *shdr = &info->sechdrs[i];
346 /* Alloc bit cleared means "ignore it." */
347 if ((shdr->sh_flags & SHF_ALLOC)
348 && strcmp(info->secstrings + shdr->sh_name, name) == 0)
354 /* Find a module section, or NULL. */
355 static void *section_addr(const struct load_info *info, const char *name)
357 /* Section 0 has sh_addr 0. */
358 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
361 /* Find a module section, or NULL. Fill in number of "objects" in section. */
362 static void *section_objs(const struct load_info *info,
367 unsigned int sec = find_sec(info, name);
369 /* Section 0 has sh_addr 0 and sh_size 0. */
370 *num = info->sechdrs[sec].sh_size / object_size;
371 return (void *)info->sechdrs[sec].sh_addr;
374 /* Provided by the linker */
375 extern const struct kernel_symbol __start___ksymtab[];
376 extern const struct kernel_symbol __stop___ksymtab[];
377 extern const struct kernel_symbol __start___ksymtab_gpl[];
378 extern const struct kernel_symbol __stop___ksymtab_gpl[];
379 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
380 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
381 extern const s32 __start___kcrctab[];
382 extern const s32 __start___kcrctab_gpl[];
383 extern const s32 __start___kcrctab_gpl_future[];
384 #ifdef CONFIG_UNUSED_SYMBOLS
385 extern const struct kernel_symbol __start___ksymtab_unused[];
386 extern const struct kernel_symbol __stop___ksymtab_unused[];
387 extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
388 extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
389 extern const s32 __start___kcrctab_unused[];
390 extern const s32 __start___kcrctab_unused_gpl[];
393 #ifndef CONFIG_MODVERSIONS
394 #define symversion(base, idx) NULL
396 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
399 static bool each_symbol_in_section(const struct symsearch *arr,
400 unsigned int arrsize,
401 struct module *owner,
402 bool (*fn)(const struct symsearch *syms,
403 struct module *owner,
409 for (j = 0; j < arrsize; j++) {
410 if (fn(&arr[j], owner, data))
417 /* Returns true as soon as fn returns true, otherwise false. */
418 static bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
419 struct module *owner,
424 static const struct symsearch arr[] = {
425 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
426 NOT_GPL_ONLY, false },
427 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
428 __start___kcrctab_gpl,
430 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
431 __start___kcrctab_gpl_future,
432 WILL_BE_GPL_ONLY, false },
433 #ifdef CONFIG_UNUSED_SYMBOLS
434 { __start___ksymtab_unused, __stop___ksymtab_unused,
435 __start___kcrctab_unused,
436 NOT_GPL_ONLY, true },
437 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
438 __start___kcrctab_unused_gpl,
443 module_assert_mutex_or_preempt();
445 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
448 list_for_each_entry_rcu(mod, &modules, list) {
449 struct symsearch arr[] = {
450 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
451 NOT_GPL_ONLY, false },
452 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
455 { mod->gpl_future_syms,
456 mod->gpl_future_syms + mod->num_gpl_future_syms,
457 mod->gpl_future_crcs,
458 WILL_BE_GPL_ONLY, false },
459 #ifdef CONFIG_UNUSED_SYMBOLS
461 mod->unused_syms + mod->num_unused_syms,
463 NOT_GPL_ONLY, true },
464 { mod->unused_gpl_syms,
465 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
466 mod->unused_gpl_crcs,
471 if (mod->state == MODULE_STATE_UNFORMED)
474 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
480 struct find_symbol_arg {
487 struct module *owner;
489 const struct kernel_symbol *sym;
490 enum mod_license license;
493 static bool check_symbol(const struct symsearch *syms,
494 struct module *owner,
495 unsigned int symnum, void *data)
497 struct find_symbol_arg *fsa = data;
500 if (syms->license == GPL_ONLY)
502 if (syms->license == WILL_BE_GPL_ONLY && fsa->warn) {
503 pr_warn("Symbol %s is being used by a non-GPL module, "
504 "which will not be allowed in the future\n",
509 #ifdef CONFIG_UNUSED_SYMBOLS
510 if (syms->unused && fsa->warn) {
511 pr_warn("Symbol %s is marked as UNUSED, however this module is "
512 "using it.\n", fsa->name);
513 pr_warn("This symbol will go away in the future.\n");
514 pr_warn("Please evaluate if this is the right api to use and "
515 "if it really is, submit a report to the linux kernel "
516 "mailing list together with submitting your code for "
522 fsa->crc = symversion(syms->crcs, symnum);
523 fsa->sym = &syms->start[symnum];
524 fsa->license = syms->license;
528 static unsigned long kernel_symbol_value(const struct kernel_symbol *sym)
530 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
531 return (unsigned long)offset_to_ptr(&sym->value_offset);
537 static const char *kernel_symbol_name(const struct kernel_symbol *sym)
539 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
540 return offset_to_ptr(&sym->name_offset);
546 static int cmp_name(const void *va, const void *vb)
549 const struct kernel_symbol *b;
551 return strcmp(a, kernel_symbol_name(b));
554 static bool find_symbol_in_section(const struct symsearch *syms,
555 struct module *owner,
558 struct find_symbol_arg *fsa = data;
559 struct kernel_symbol *sym;
561 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
562 sizeof(struct kernel_symbol), cmp_name);
564 if (sym != NULL && check_symbol(syms, owner, sym - syms->start, data))
570 /* Find a symbol and return it, along with, (optional) crc and
571 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
572 static const struct kernel_symbol *find_symbol(const char *name,
573 struct module **owner,
575 enum mod_license *license,
579 struct find_symbol_arg fsa;
585 if (each_symbol_section(find_symbol_in_section, &fsa)) {
591 *license = fsa.license;
595 pr_debug("Failed to find symbol %s\n", name);
600 * Search for module by name: must hold module_mutex (or preempt disabled
601 * for read-only access).
603 static struct module *find_module_all(const char *name, size_t len,
608 module_assert_mutex_or_preempt();
610 list_for_each_entry_rcu(mod, &modules, list) {
611 if (!even_unformed && mod->state == MODULE_STATE_UNFORMED)
613 if (strlen(mod->name) == len && !memcmp(mod->name, name, len))
619 struct module *find_module(const char *name)
621 module_assert_mutex();
622 return find_module_all(name, strlen(name), false);
624 EXPORT_SYMBOL_GPL(find_module);
628 static inline void __percpu *mod_percpu(struct module *mod)
633 static int percpu_modalloc(struct module *mod, struct load_info *info)
635 Elf_Shdr *pcpusec = &info->sechdrs[info->index.pcpu];
636 unsigned long align = pcpusec->sh_addralign;
638 if (!pcpusec->sh_size)
641 if (align > PAGE_SIZE) {
642 pr_warn("%s: per-cpu alignment %li > %li\n",
643 mod->name, align, PAGE_SIZE);
647 mod->percpu = __alloc_reserved_percpu(pcpusec->sh_size, align);
649 pr_warn("%s: Could not allocate %lu bytes percpu data\n",
650 mod->name, (unsigned long)pcpusec->sh_size);
653 mod->percpu_size = pcpusec->sh_size;
657 static void percpu_modfree(struct module *mod)
659 free_percpu(mod->percpu);
662 static unsigned int find_pcpusec(struct load_info *info)
664 return find_sec(info, ".data..percpu");
667 static void percpu_modcopy(struct module *mod,
668 const void *from, unsigned long size)
672 for_each_possible_cpu(cpu)
673 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
676 bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
683 list_for_each_entry_rcu(mod, &modules, list) {
684 if (mod->state == MODULE_STATE_UNFORMED)
686 if (!mod->percpu_size)
688 for_each_possible_cpu(cpu) {
689 void *start = per_cpu_ptr(mod->percpu, cpu);
690 void *va = (void *)addr;
692 if (va >= start && va < start + mod->percpu_size) {
694 *can_addr = (unsigned long) (va - start);
695 *can_addr += (unsigned long)
696 per_cpu_ptr(mod->percpu,
710 * is_module_percpu_address - test whether address is from module static percpu
711 * @addr: address to test
713 * Test whether @addr belongs to module static percpu area.
716 * %true if @addr is from module static percpu area
718 bool is_module_percpu_address(unsigned long addr)
720 return __is_module_percpu_address(addr, NULL);
723 #else /* ... !CONFIG_SMP */
725 static inline void __percpu *mod_percpu(struct module *mod)
729 static int percpu_modalloc(struct module *mod, struct load_info *info)
731 /* UP modules shouldn't have this section: ENOMEM isn't quite right */
732 if (info->sechdrs[info->index.pcpu].sh_size != 0)
736 static inline void percpu_modfree(struct module *mod)
739 static unsigned int find_pcpusec(struct load_info *info)
743 static inline void percpu_modcopy(struct module *mod,
744 const void *from, unsigned long size)
746 /* pcpusec should be 0, and size of that section should be 0. */
749 bool is_module_percpu_address(unsigned long addr)
754 bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
759 #endif /* CONFIG_SMP */
761 #define MODINFO_ATTR(field) \
762 static void setup_modinfo_##field(struct module *mod, const char *s) \
764 mod->field = kstrdup(s, GFP_KERNEL); \
766 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
767 struct module_kobject *mk, char *buffer) \
769 return scnprintf(buffer, PAGE_SIZE, "%s\n", mk->mod->field); \
771 static int modinfo_##field##_exists(struct module *mod) \
773 return mod->field != NULL; \
775 static void free_modinfo_##field(struct module *mod) \
780 static struct module_attribute modinfo_##field = { \
781 .attr = { .name = __stringify(field), .mode = 0444 }, \
782 .show = show_modinfo_##field, \
783 .setup = setup_modinfo_##field, \
784 .test = modinfo_##field##_exists, \
785 .free = free_modinfo_##field, \
788 MODINFO_ATTR(version);
789 MODINFO_ATTR(srcversion);
791 static char last_unloaded_module[MODULE_NAME_LEN+1];
793 #ifdef CONFIG_MODULE_UNLOAD
795 EXPORT_TRACEPOINT_SYMBOL(module_get);
797 /* MODULE_REF_BASE is the base reference count by kmodule loader. */
798 #define MODULE_REF_BASE 1
800 /* Init the unload section of the module. */
801 static int module_unload_init(struct module *mod)
804 * Initialize reference counter to MODULE_REF_BASE.
805 * refcnt == 0 means module is going.
807 atomic_set(&mod->refcnt, MODULE_REF_BASE);
809 INIT_LIST_HEAD(&mod->source_list);
810 INIT_LIST_HEAD(&mod->target_list);
812 /* Hold reference count during initialization. */
813 atomic_inc(&mod->refcnt);
818 /* Does a already use b? */
819 static int already_uses(struct module *a, struct module *b)
821 struct module_use *use;
823 list_for_each_entry(use, &b->source_list, source_list) {
824 if (use->source == a) {
825 pr_debug("%s uses %s!\n", a->name, b->name);
829 pr_debug("%s does not use %s!\n", a->name, b->name);
835 * - we add 'a' as a "source", 'b' as a "target" of module use
836 * - the module_use is added to the list of 'b' sources (so
837 * 'b' can walk the list to see who sourced them), and of 'a'
838 * targets (so 'a' can see what modules it targets).
840 static int add_module_usage(struct module *a, struct module *b)
842 struct module_use *use;
844 pr_debug("Allocating new usage for %s.\n", a->name);
845 use = kmalloc(sizeof(*use), GFP_ATOMIC);
851 list_add(&use->source_list, &b->source_list);
852 list_add(&use->target_list, &a->target_list);
856 /* Module a uses b: caller needs module_mutex() */
857 static int ref_module(struct module *a, struct module *b)
861 if (b == NULL || already_uses(a, b))
864 /* If module isn't available, we fail. */
865 err = strong_try_module_get(b);
869 err = add_module_usage(a, b);
877 /* Clear the unload stuff of the module. */
878 static void module_unload_free(struct module *mod)
880 struct module_use *use, *tmp;
882 mutex_lock(&module_mutex);
883 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
884 struct module *i = use->target;
885 pr_debug("%s unusing %s\n", mod->name, i->name);
887 list_del(&use->source_list);
888 list_del(&use->target_list);
891 mutex_unlock(&module_mutex);
894 #ifdef CONFIG_MODULE_FORCE_UNLOAD
895 static inline int try_force_unload(unsigned int flags)
897 int ret = (flags & O_TRUNC);
899 add_taint(TAINT_FORCED_RMMOD, LOCKDEP_NOW_UNRELIABLE);
903 static inline int try_force_unload(unsigned int flags)
907 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
909 /* Try to release refcount of module, 0 means success. */
910 static int try_release_module_ref(struct module *mod)
914 /* Try to decrement refcnt which we set at loading */
915 ret = atomic_sub_return(MODULE_REF_BASE, &mod->refcnt);
918 /* Someone can put this right now, recover with checking */
919 ret = atomic_add_unless(&mod->refcnt, MODULE_REF_BASE, 0);
924 static int try_stop_module(struct module *mod, int flags, int *forced)
926 /* If it's not unused, quit unless we're forcing. */
927 if (try_release_module_ref(mod) != 0) {
928 *forced = try_force_unload(flags);
933 /* Mark it as dying. */
934 mod->state = MODULE_STATE_GOING;
940 * module_refcount - return the refcount or -1 if unloading
942 * @mod: the module we're checking
945 * -1 if the module is in the process of unloading
946 * otherwise the number of references in the kernel to the module
948 int module_refcount(struct module *mod)
950 return atomic_read(&mod->refcnt) - MODULE_REF_BASE;
952 EXPORT_SYMBOL(module_refcount);
954 /* This exists whether we can unload or not */
955 static void free_module(struct module *mod);
957 SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
961 char name[MODULE_NAME_LEN];
964 if (!capable(CAP_SYS_MODULE) || modules_disabled)
967 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
969 name[MODULE_NAME_LEN-1] = '\0';
971 audit_log_kern_module(name);
973 if (mutex_lock_interruptible(&module_mutex) != 0)
976 mod = find_module(name);
982 if (!list_empty(&mod->source_list)) {
983 /* Other modules depend on us: get rid of them first. */
988 /* Doing init or already dying? */
989 if (mod->state != MODULE_STATE_LIVE) {
990 /* FIXME: if (force), slam module count damn the torpedoes */
991 pr_debug("%s already dying\n", mod->name);
996 /* If it has an init func, it must have an exit func to unload */
997 if (mod->init && !mod->exit) {
998 forced = try_force_unload(flags);
1000 /* This module can't be removed */
1006 /* Stop the machine so refcounts can't move and disable module. */
1007 ret = try_stop_module(mod, flags, &forced);
1011 mutex_unlock(&module_mutex);
1012 /* Final destruction now no one is using it. */
1013 if (mod->exit != NULL)
1015 blocking_notifier_call_chain(&module_notify_list,
1016 MODULE_STATE_GOING, mod);
1017 klp_module_going(mod);
1018 ftrace_release_mod(mod);
1020 async_synchronize_full();
1022 /* Store the name of the last unloaded module for diagnostic purposes */
1023 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
1026 /* someone could wait for the module in add_unformed_module() */
1027 wake_up_all(&module_wq);
1030 mutex_unlock(&module_mutex);
1034 static inline void print_unload_info(struct seq_file *m, struct module *mod)
1036 struct module_use *use;
1037 int printed_something = 0;
1039 seq_printf(m, " %i ", module_refcount(mod));
1042 * Always include a trailing , so userspace can differentiate
1043 * between this and the old multi-field proc format.
1045 list_for_each_entry(use, &mod->source_list, source_list) {
1046 printed_something = 1;
1047 seq_printf(m, "%s,", use->source->name);
1050 if (mod->init != NULL && mod->exit == NULL) {
1051 printed_something = 1;
1052 seq_puts(m, "[permanent],");
1055 if (!printed_something)
1059 void __symbol_put(const char *symbol)
1061 struct module *owner;
1064 if (!find_symbol(symbol, &owner, NULL, NULL, true, false))
1069 EXPORT_SYMBOL(__symbol_put);
1071 /* Note this assumes addr is a function, which it currently always is. */
1072 void symbol_put_addr(void *addr)
1074 struct module *modaddr;
1075 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
1077 if (core_kernel_text(a))
1081 * Even though we hold a reference on the module; we still need to
1082 * disable preemption in order to safely traverse the data structure.
1085 modaddr = __module_text_address(a);
1087 module_put(modaddr);
1090 EXPORT_SYMBOL_GPL(symbol_put_addr);
1092 static ssize_t show_refcnt(struct module_attribute *mattr,
1093 struct module_kobject *mk, char *buffer)
1095 return sprintf(buffer, "%i\n", module_refcount(mk->mod));
1098 static struct module_attribute modinfo_refcnt =
1099 __ATTR(refcnt, 0444, show_refcnt, NULL);
1101 void __module_get(struct module *module)
1105 atomic_inc(&module->refcnt);
1106 trace_module_get(module, _RET_IP_);
1110 EXPORT_SYMBOL(__module_get);
1112 bool try_module_get(struct module *module)
1118 /* Note: here, we can fail to get a reference */
1119 if (likely(module_is_live(module) &&
1120 atomic_inc_not_zero(&module->refcnt) != 0))
1121 trace_module_get(module, _RET_IP_);
1129 EXPORT_SYMBOL(try_module_get);
1131 void module_put(struct module *module)
1137 ret = atomic_dec_if_positive(&module->refcnt);
1138 WARN_ON(ret < 0); /* Failed to put refcount */
1139 trace_module_put(module, _RET_IP_);
1143 EXPORT_SYMBOL(module_put);
1145 #else /* !CONFIG_MODULE_UNLOAD */
1146 static inline void print_unload_info(struct seq_file *m, struct module *mod)
1148 /* We don't know the usage count, or what modules are using. */
1149 seq_puts(m, " - -");
1152 static inline void module_unload_free(struct module *mod)
1156 static int ref_module(struct module *a, struct module *b)
1158 return strong_try_module_get(b);
1161 static inline int module_unload_init(struct module *mod)
1165 #endif /* CONFIG_MODULE_UNLOAD */
1167 static size_t module_flags_taint(struct module *mod, char *buf)
1172 for (i = 0; i < TAINT_FLAGS_COUNT; i++) {
1173 if (taint_flags[i].module && test_bit(i, &mod->taints))
1174 buf[l++] = taint_flags[i].c_true;
1180 static ssize_t show_initstate(struct module_attribute *mattr,
1181 struct module_kobject *mk, char *buffer)
1183 const char *state = "unknown";
1185 switch (mk->mod->state) {
1186 case MODULE_STATE_LIVE:
1189 case MODULE_STATE_COMING:
1192 case MODULE_STATE_GOING:
1198 return sprintf(buffer, "%s\n", state);
1201 static struct module_attribute modinfo_initstate =
1202 __ATTR(initstate, 0444, show_initstate, NULL);
1204 static ssize_t store_uevent(struct module_attribute *mattr,
1205 struct module_kobject *mk,
1206 const char *buffer, size_t count)
1210 rc = kobject_synth_uevent(&mk->kobj, buffer, count);
1211 return rc ? rc : count;
1214 struct module_attribute module_uevent =
1215 __ATTR(uevent, 0200, NULL, store_uevent);
1217 static ssize_t show_coresize(struct module_attribute *mattr,
1218 struct module_kobject *mk, char *buffer)
1220 return sprintf(buffer, "%u\n", mk->mod->core_layout.size);
1223 static struct module_attribute modinfo_coresize =
1224 __ATTR(coresize, 0444, show_coresize, NULL);
1226 static ssize_t show_initsize(struct module_attribute *mattr,
1227 struct module_kobject *mk, char *buffer)
1229 return sprintf(buffer, "%u\n", mk->mod->init_layout.size);
1232 static struct module_attribute modinfo_initsize =
1233 __ATTR(initsize, 0444, show_initsize, NULL);
1235 static ssize_t show_taint(struct module_attribute *mattr,
1236 struct module_kobject *mk, char *buffer)
1240 l = module_flags_taint(mk->mod, buffer);
1245 static struct module_attribute modinfo_taint =
1246 __ATTR(taint, 0444, show_taint, NULL);
1248 static struct module_attribute *modinfo_attrs[] = {
1251 &modinfo_srcversion,
1256 #ifdef CONFIG_MODULE_UNLOAD
1262 static const char vermagic[] = VERMAGIC_STRING;
1264 static int try_to_force_load(struct module *mod, const char *reason)
1266 #ifdef CONFIG_MODULE_FORCE_LOAD
1267 if (!test_taint(TAINT_FORCED_MODULE))
1268 pr_warn("%s: %s: kernel tainted.\n", mod->name, reason);
1269 add_taint_module(mod, TAINT_FORCED_MODULE, LOCKDEP_NOW_UNRELIABLE);
1276 #ifdef CONFIG_MODVERSIONS
1278 static u32 resolve_rel_crc(const s32 *crc)
1280 return *(u32 *)((void *)crc + *crc);
1283 static int check_version(const struct load_info *info,
1284 const char *symname,
1288 Elf_Shdr *sechdrs = info->sechdrs;
1289 unsigned int versindex = info->index.vers;
1290 unsigned int i, num_versions;
1291 struct modversion_info *versions;
1293 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1297 /* No versions at all? modprobe --force does this. */
1299 return try_to_force_load(mod, symname) == 0;
1301 versions = (void *) sechdrs[versindex].sh_addr;
1302 num_versions = sechdrs[versindex].sh_size
1303 / sizeof(struct modversion_info);
1305 for (i = 0; i < num_versions; i++) {
1308 if (strcmp(versions[i].name, symname) != 0)
1311 if (IS_ENABLED(CONFIG_MODULE_REL_CRCS))
1312 crcval = resolve_rel_crc(crc);
1315 if (versions[i].crc == crcval)
1317 pr_debug("Found checksum %X vs module %lX\n",
1318 crcval, versions[i].crc);
1322 /* Broken toolchain. Warn once, then let it go.. */
1323 pr_warn_once("%s: no symbol version for %s\n", info->name, symname);
1327 pr_warn("%s: disagrees about version of symbol %s\n",
1328 info->name, symname);
1332 static inline int check_modstruct_version(const struct load_info *info,
1338 * Since this should be found in kernel (which can't be removed), no
1339 * locking is necessary -- use preempt_disable() to placate lockdep.
1342 if (!find_symbol("module_layout", NULL, &crc, NULL, true, false)) {
1347 return check_version(info, "module_layout", mod, crc);
1350 /* First part is kernel version, which we ignore if module has crcs. */
1351 static inline int same_magic(const char *amagic, const char *bmagic,
1355 amagic += strcspn(amagic, " ");
1356 bmagic += strcspn(bmagic, " ");
1358 return strcmp(amagic, bmagic) == 0;
1361 static inline int check_version(const struct load_info *info,
1362 const char *symname,
1369 static inline int check_modstruct_version(const struct load_info *info,
1375 static inline int same_magic(const char *amagic, const char *bmagic,
1378 return strcmp(amagic, bmagic) == 0;
1380 #endif /* CONFIG_MODVERSIONS */
1382 static bool inherit_taint(struct module *mod, struct module *owner)
1384 if (!owner || !test_bit(TAINT_PROPRIETARY_MODULE, &owner->taints))
1387 if (mod->using_gplonly_symbols) {
1388 pr_err("%s: module using GPL-only symbols uses symbols from proprietary module %s.\n",
1389 mod->name, owner->name);
1393 if (!test_bit(TAINT_PROPRIETARY_MODULE, &mod->taints)) {
1394 pr_warn("%s: module uses symbols from proprietary module %s, inheriting taint.\n",
1395 mod->name, owner->name);
1396 set_bit(TAINT_PROPRIETARY_MODULE, &mod->taints);
1401 /* Resolve a symbol for this module. I.e. if we find one, record usage. */
1402 static const struct kernel_symbol *resolve_symbol(struct module *mod,
1403 const struct load_info *info,
1407 struct module *owner;
1408 const struct kernel_symbol *sym;
1410 enum mod_license license;
1414 * The module_mutex should not be a heavily contended lock;
1415 * if we get the occasional sleep here, we'll go an extra iteration
1416 * in the wait_event_interruptible(), which is harmless.
1418 sched_annotate_sleep();
1419 mutex_lock(&module_mutex);
1420 sym = find_symbol(name, &owner, &crc, &license,
1421 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1425 if (license == GPL_ONLY)
1426 mod->using_gplonly_symbols = true;
1428 if (!inherit_taint(mod, owner)) {
1433 if (!check_version(info, name, mod, crc)) {
1434 sym = ERR_PTR(-EINVAL);
1438 err = ref_module(mod, owner);
1445 /* We must make copy under the lock if we failed to get ref. */
1446 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1448 mutex_unlock(&module_mutex);
1452 static const struct kernel_symbol *
1453 resolve_symbol_wait(struct module *mod,
1454 const struct load_info *info,
1457 const struct kernel_symbol *ksym;
1458 char owner[MODULE_NAME_LEN];
1460 if (wait_event_interruptible_timeout(module_wq,
1461 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1462 || PTR_ERR(ksym) != -EBUSY,
1464 pr_warn("%s: gave up waiting for init of module %s.\n",
1471 * /sys/module/foo/sections stuff
1472 * J. Corbet <corbet@lwn.net>
1476 #ifdef CONFIG_KALLSYMS
1477 static inline bool sect_empty(const Elf_Shdr *sect)
1479 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1482 struct module_sect_attr {
1483 struct bin_attribute battr;
1484 unsigned long address;
1487 struct module_sect_attrs {
1488 struct attribute_group grp;
1489 unsigned int nsections;
1490 struct module_sect_attr attrs[0];
1493 #define MODULE_SECT_READ_SIZE (3 /* "0x", "\n" */ + (BITS_PER_LONG / 4))
1494 static ssize_t module_sect_read(struct file *file, struct kobject *kobj,
1495 struct bin_attribute *battr,
1496 char *buf, loff_t pos, size_t count)
1498 struct module_sect_attr *sattr =
1499 container_of(battr, struct module_sect_attr, battr);
1500 char bounce[MODULE_SECT_READ_SIZE + 1];
1507 * Since we're a binary read handler, we must account for the
1508 * trailing NUL byte that sprintf will write: if "buf" is
1509 * too small to hold the NUL, or the NUL is exactly the last
1510 * byte, the read will look like it got truncated by one byte.
1511 * Since there is no way to ask sprintf nicely to not write
1512 * the NUL, we have to use a bounce buffer.
1514 wrote = scnprintf(bounce, sizeof(bounce), "0x%px\n",
1515 kallsyms_show_value(file->f_cred)
1516 ? (void *)sattr->address : NULL);
1517 count = min(count, wrote);
1518 memcpy(buf, bounce, count);
1523 static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1525 unsigned int section;
1527 for (section = 0; section < sect_attrs->nsections; section++)
1528 kfree(sect_attrs->attrs[section].battr.attr.name);
1532 static void add_sect_attrs(struct module *mod, const struct load_info *info)
1534 unsigned int nloaded = 0, i, size[2];
1535 struct module_sect_attrs *sect_attrs;
1536 struct module_sect_attr *sattr;
1537 struct bin_attribute **gattr;
1539 /* Count loaded sections and allocate structures */
1540 for (i = 0; i < info->hdr->e_shnum; i++)
1541 if (!sect_empty(&info->sechdrs[i]))
1543 size[0] = ALIGN(struct_size(sect_attrs, attrs, nloaded),
1544 sizeof(sect_attrs->grp.bin_attrs[0]));
1545 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.bin_attrs[0]);
1546 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1547 if (sect_attrs == NULL)
1550 /* Setup section attributes. */
1551 sect_attrs->grp.name = "sections";
1552 sect_attrs->grp.bin_attrs = (void *)sect_attrs + size[0];
1554 sect_attrs->nsections = 0;
1555 sattr = §_attrs->attrs[0];
1556 gattr = §_attrs->grp.bin_attrs[0];
1557 for (i = 0; i < info->hdr->e_shnum; i++) {
1558 Elf_Shdr *sec = &info->sechdrs[i];
1559 if (sect_empty(sec))
1561 sysfs_bin_attr_init(&sattr->battr);
1562 sattr->address = sec->sh_addr;
1563 sattr->battr.attr.name =
1564 kstrdup(info->secstrings + sec->sh_name, GFP_KERNEL);
1565 if (sattr->battr.attr.name == NULL)
1567 sect_attrs->nsections++;
1568 sattr->battr.read = module_sect_read;
1569 sattr->battr.size = MODULE_SECT_READ_SIZE;
1570 sattr->battr.attr.mode = 0400;
1571 *(gattr++) = &(sattr++)->battr;
1575 if (sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp))
1578 mod->sect_attrs = sect_attrs;
1581 free_sect_attrs(sect_attrs);
1584 static void remove_sect_attrs(struct module *mod)
1586 if (mod->sect_attrs) {
1587 sysfs_remove_group(&mod->mkobj.kobj,
1588 &mod->sect_attrs->grp);
1589 /* We are positive that no one is using any sect attrs
1590 * at this point. Deallocate immediately. */
1591 free_sect_attrs(mod->sect_attrs);
1592 mod->sect_attrs = NULL;
1597 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1600 struct module_notes_attrs {
1601 struct kobject *dir;
1603 struct bin_attribute attrs[0];
1606 static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
1607 struct bin_attribute *bin_attr,
1608 char *buf, loff_t pos, size_t count)
1611 * The caller checked the pos and count against our size.
1613 memcpy(buf, bin_attr->private + pos, count);
1617 static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1620 if (notes_attrs->dir) {
1622 sysfs_remove_bin_file(notes_attrs->dir,
1623 ¬es_attrs->attrs[i]);
1624 kobject_put(notes_attrs->dir);
1629 static void add_notes_attrs(struct module *mod, const struct load_info *info)
1631 unsigned int notes, loaded, i;
1632 struct module_notes_attrs *notes_attrs;
1633 struct bin_attribute *nattr;
1635 /* failed to create section attributes, so can't create notes */
1636 if (!mod->sect_attrs)
1639 /* Count notes sections and allocate structures. */
1641 for (i = 0; i < info->hdr->e_shnum; i++)
1642 if (!sect_empty(&info->sechdrs[i]) &&
1643 (info->sechdrs[i].sh_type == SHT_NOTE))
1649 notes_attrs = kzalloc(struct_size(notes_attrs, attrs, notes),
1651 if (notes_attrs == NULL)
1654 notes_attrs->notes = notes;
1655 nattr = ¬es_attrs->attrs[0];
1656 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1657 if (sect_empty(&info->sechdrs[i]))
1659 if (info->sechdrs[i].sh_type == SHT_NOTE) {
1660 sysfs_bin_attr_init(nattr);
1661 nattr->attr.name = mod->sect_attrs->attrs[loaded].battr.attr.name;
1662 nattr->attr.mode = S_IRUGO;
1663 nattr->size = info->sechdrs[i].sh_size;
1664 nattr->private = (void *) info->sechdrs[i].sh_addr;
1665 nattr->read = module_notes_read;
1671 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
1672 if (!notes_attrs->dir)
1675 for (i = 0; i < notes; ++i)
1676 if (sysfs_create_bin_file(notes_attrs->dir,
1677 ¬es_attrs->attrs[i]))
1680 mod->notes_attrs = notes_attrs;
1684 free_notes_attrs(notes_attrs, i);
1687 static void remove_notes_attrs(struct module *mod)
1689 if (mod->notes_attrs)
1690 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1695 static inline void add_sect_attrs(struct module *mod,
1696 const struct load_info *info)
1700 static inline void remove_sect_attrs(struct module *mod)
1704 static inline void add_notes_attrs(struct module *mod,
1705 const struct load_info *info)
1709 static inline void remove_notes_attrs(struct module *mod)
1712 #endif /* CONFIG_KALLSYMS */
1714 static void del_usage_links(struct module *mod)
1716 #ifdef CONFIG_MODULE_UNLOAD
1717 struct module_use *use;
1719 mutex_lock(&module_mutex);
1720 list_for_each_entry(use, &mod->target_list, target_list)
1721 sysfs_remove_link(use->target->holders_dir, mod->name);
1722 mutex_unlock(&module_mutex);
1726 static int add_usage_links(struct module *mod)
1729 #ifdef CONFIG_MODULE_UNLOAD
1730 struct module_use *use;
1732 mutex_lock(&module_mutex);
1733 list_for_each_entry(use, &mod->target_list, target_list) {
1734 ret = sysfs_create_link(use->target->holders_dir,
1735 &mod->mkobj.kobj, mod->name);
1739 mutex_unlock(&module_mutex);
1741 del_usage_links(mod);
1746 static void module_remove_modinfo_attrs(struct module *mod, int end);
1748 static int module_add_modinfo_attrs(struct module *mod)
1750 struct module_attribute *attr;
1751 struct module_attribute *temp_attr;
1755 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1756 (ARRAY_SIZE(modinfo_attrs) + 1)),
1758 if (!mod->modinfo_attrs)
1761 temp_attr = mod->modinfo_attrs;
1762 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1763 if (!attr->test || attr->test(mod)) {
1764 memcpy(temp_attr, attr, sizeof(*temp_attr));
1765 sysfs_attr_init(&temp_attr->attr);
1766 error = sysfs_create_file(&mod->mkobj.kobj,
1778 module_remove_modinfo_attrs(mod, --i);
1780 kfree(mod->modinfo_attrs);
1784 static void module_remove_modinfo_attrs(struct module *mod, int end)
1786 struct module_attribute *attr;
1789 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1790 if (end >= 0 && i > end)
1792 /* pick a field to test for end of list */
1793 if (!attr->attr.name)
1795 sysfs_remove_file(&mod->mkobj.kobj, &attr->attr);
1799 kfree(mod->modinfo_attrs);
1802 static void mod_kobject_put(struct module *mod)
1804 DECLARE_COMPLETION_ONSTACK(c);
1805 mod->mkobj.kobj_completion = &c;
1806 kobject_put(&mod->mkobj.kobj);
1807 wait_for_completion(&c);
1810 static int mod_sysfs_init(struct module *mod)
1813 struct kobject *kobj;
1815 if (!module_sysfs_initialized) {
1816 pr_err("%s: module sysfs not initialized\n", mod->name);
1821 kobj = kset_find_obj(module_kset, mod->name);
1823 pr_err("%s: module is already loaded\n", mod->name);
1829 mod->mkobj.mod = mod;
1831 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1832 mod->mkobj.kobj.kset = module_kset;
1833 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1836 mod_kobject_put(mod);
1842 static int mod_sysfs_setup(struct module *mod,
1843 const struct load_info *info,
1844 struct kernel_param *kparam,
1845 unsigned int num_params)
1849 err = mod_sysfs_init(mod);
1853 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1854 if (!mod->holders_dir) {
1859 err = module_param_sysfs_setup(mod, kparam, num_params);
1861 goto out_unreg_holders;
1863 err = module_add_modinfo_attrs(mod);
1865 goto out_unreg_param;
1867 err = add_usage_links(mod);
1869 goto out_unreg_modinfo_attrs;
1871 add_sect_attrs(mod, info);
1872 add_notes_attrs(mod, info);
1876 out_unreg_modinfo_attrs:
1877 module_remove_modinfo_attrs(mod, -1);
1879 module_param_sysfs_remove(mod);
1881 kobject_put(mod->holders_dir);
1883 mod_kobject_put(mod);
1888 static void mod_sysfs_fini(struct module *mod)
1890 remove_notes_attrs(mod);
1891 remove_sect_attrs(mod);
1892 mod_kobject_put(mod);
1895 static void init_param_lock(struct module *mod)
1897 mutex_init(&mod->param_lock);
1899 #else /* !CONFIG_SYSFS */
1901 static int mod_sysfs_setup(struct module *mod,
1902 const struct load_info *info,
1903 struct kernel_param *kparam,
1904 unsigned int num_params)
1909 static void mod_sysfs_fini(struct module *mod)
1913 static void module_remove_modinfo_attrs(struct module *mod, int end)
1917 static void del_usage_links(struct module *mod)
1921 static void init_param_lock(struct module *mod)
1924 #endif /* CONFIG_SYSFS */
1926 static void mod_sysfs_teardown(struct module *mod)
1928 del_usage_links(mod);
1929 module_remove_modinfo_attrs(mod, -1);
1930 module_param_sysfs_remove(mod);
1931 kobject_put(mod->mkobj.drivers_dir);
1932 kobject_put(mod->holders_dir);
1933 mod_sysfs_fini(mod);
1936 #ifdef CONFIG_ARCH_HAS_STRICT_MODULE_RWX
1938 * LKM RO/NX protection: protect module's text/ro-data
1939 * from modification and any data from execution.
1941 * General layout of module is:
1942 * [text] [read-only-data] [ro-after-init] [writable data]
1943 * text_size -----^ ^ ^ ^
1944 * ro_size ------------------------| | |
1945 * ro_after_init_size -----------------------------| |
1946 * size -----------------------------------------------------------|
1948 * These values are always page-aligned (as is base)
1950 static void frob_text(const struct module_layout *layout,
1951 int (*set_memory)(unsigned long start, int num_pages))
1953 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
1954 BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
1955 set_memory((unsigned long)layout->base,
1956 layout->text_size >> PAGE_SHIFT);
1959 #ifdef CONFIG_STRICT_MODULE_RWX
1960 static void frob_rodata(const struct module_layout *layout,
1961 int (*set_memory)(unsigned long start, int num_pages))
1963 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
1964 BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
1965 BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
1966 set_memory((unsigned long)layout->base + layout->text_size,
1967 (layout->ro_size - layout->text_size) >> PAGE_SHIFT);
1970 static void frob_ro_after_init(const struct module_layout *layout,
1971 int (*set_memory)(unsigned long start, int num_pages))
1973 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
1974 BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
1975 BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1));
1976 set_memory((unsigned long)layout->base + layout->ro_size,
1977 (layout->ro_after_init_size - layout->ro_size) >> PAGE_SHIFT);
1980 static void frob_writable_data(const struct module_layout *layout,
1981 int (*set_memory)(unsigned long start, int num_pages))
1983 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
1984 BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1));
1985 BUG_ON((unsigned long)layout->size & (PAGE_SIZE-1));
1986 set_memory((unsigned long)layout->base + layout->ro_after_init_size,
1987 (layout->size - layout->ro_after_init_size) >> PAGE_SHIFT);
1990 /* livepatching wants to disable read-only so it can frob module. */
1991 void module_disable_ro(const struct module *mod)
1993 if (!rodata_enabled)
1996 frob_text(&mod->core_layout, set_memory_rw);
1997 frob_rodata(&mod->core_layout, set_memory_rw);
1998 frob_ro_after_init(&mod->core_layout, set_memory_rw);
1999 frob_text(&mod->init_layout, set_memory_rw);
2000 frob_rodata(&mod->init_layout, set_memory_rw);
2003 void module_enable_ro(const struct module *mod, bool after_init)
2005 if (!rodata_enabled)
2008 frob_text(&mod->core_layout, set_memory_ro);
2010 frob_rodata(&mod->core_layout, set_memory_ro);
2011 frob_text(&mod->init_layout, set_memory_ro);
2012 frob_rodata(&mod->init_layout, set_memory_ro);
2015 frob_ro_after_init(&mod->core_layout, set_memory_ro);
2018 static void module_enable_nx(const struct module *mod)
2020 frob_rodata(&mod->core_layout, set_memory_nx);
2021 frob_ro_after_init(&mod->core_layout, set_memory_nx);
2022 frob_writable_data(&mod->core_layout, set_memory_nx);
2023 frob_rodata(&mod->init_layout, set_memory_nx);
2024 frob_writable_data(&mod->init_layout, set_memory_nx);
2027 static void module_disable_nx(const struct module *mod)
2029 frob_rodata(&mod->core_layout, set_memory_x);
2030 frob_ro_after_init(&mod->core_layout, set_memory_x);
2031 frob_writable_data(&mod->core_layout, set_memory_x);
2032 frob_rodata(&mod->init_layout, set_memory_x);
2033 frob_writable_data(&mod->init_layout, set_memory_x);
2036 /* Iterate through all modules and set each module's text as RW */
2037 void set_all_modules_text_rw(void)
2041 if (!rodata_enabled)
2044 mutex_lock(&module_mutex);
2045 list_for_each_entry_rcu(mod, &modules, list) {
2046 if (mod->state == MODULE_STATE_UNFORMED)
2049 frob_text(&mod->core_layout, set_memory_rw);
2050 frob_text(&mod->init_layout, set_memory_rw);
2052 mutex_unlock(&module_mutex);
2055 /* Iterate through all modules and set each module's text as RO */
2056 void set_all_modules_text_ro(void)
2060 if (!rodata_enabled)
2063 mutex_lock(&module_mutex);
2064 list_for_each_entry_rcu(mod, &modules, list) {
2066 * Ignore going modules since it's possible that ro
2067 * protection has already been disabled, otherwise we'll
2068 * run into protection faults at module deallocation.
2070 if (mod->state == MODULE_STATE_UNFORMED ||
2071 mod->state == MODULE_STATE_GOING)
2074 frob_text(&mod->core_layout, set_memory_ro);
2075 frob_text(&mod->init_layout, set_memory_ro);
2077 mutex_unlock(&module_mutex);
2080 static void disable_ro_nx(const struct module_layout *layout)
2082 if (rodata_enabled) {
2083 frob_text(layout, set_memory_rw);
2084 frob_rodata(layout, set_memory_rw);
2085 frob_ro_after_init(layout, set_memory_rw);
2087 frob_rodata(layout, set_memory_x);
2088 frob_ro_after_init(layout, set_memory_x);
2089 frob_writable_data(layout, set_memory_x);
2092 #else /* !CONFIG_STRICT_MODULE_RWX */
2093 static void disable_ro_nx(const struct module_layout *layout) { }
2094 static void module_enable_nx(const struct module *mod) { }
2095 static void module_disable_nx(const struct module *mod) { }
2096 #endif /* CONFIG_STRICT_MODULE_RWX */
2098 static void module_enable_x(const struct module *mod)
2100 frob_text(&mod->core_layout, set_memory_x);
2101 frob_text(&mod->init_layout, set_memory_x);
2103 #else /* !CONFIG_ARCH_HAS_STRICT_MODULE_RWX */
2104 static void disable_ro_nx(const struct module_layout *layout) { }
2105 static void module_enable_nx(const struct module *mod) { }
2106 static void module_disable_nx(const struct module *mod) { }
2107 static void module_enable_x(const struct module *mod) { }
2108 #endif /* CONFIG_ARCH_HAS_STRICT_MODULE_RWX */
2110 #ifdef CONFIG_LIVEPATCH
2112 * Persist Elf information about a module. Copy the Elf header,
2113 * section header table, section string table, and symtab section
2114 * index from info to mod->klp_info.
2116 static int copy_module_elf(struct module *mod, struct load_info *info)
2118 unsigned int size, symndx;
2121 size = sizeof(*mod->klp_info);
2122 mod->klp_info = kmalloc(size, GFP_KERNEL);
2123 if (mod->klp_info == NULL)
2127 size = sizeof(mod->klp_info->hdr);
2128 memcpy(&mod->klp_info->hdr, info->hdr, size);
2130 /* Elf section header table */
2131 size = sizeof(*info->sechdrs) * info->hdr->e_shnum;
2132 mod->klp_info->sechdrs = kmemdup(info->sechdrs, size, GFP_KERNEL);
2133 if (mod->klp_info->sechdrs == NULL) {
2138 /* Elf section name string table */
2139 size = info->sechdrs[info->hdr->e_shstrndx].sh_size;
2140 mod->klp_info->secstrings = kmemdup(info->secstrings, size, GFP_KERNEL);
2141 if (mod->klp_info->secstrings == NULL) {
2146 /* Elf symbol section index */
2147 symndx = info->index.sym;
2148 mod->klp_info->symndx = symndx;
2151 * For livepatch modules, core_kallsyms.symtab is a complete
2152 * copy of the original symbol table. Adjust sh_addr to point
2153 * to core_kallsyms.symtab since the copy of the symtab in module
2154 * init memory is freed at the end of do_init_module().
2156 mod->klp_info->sechdrs[symndx].sh_addr = \
2157 (unsigned long) mod->core_kallsyms.symtab;
2162 kfree(mod->klp_info->sechdrs);
2164 kfree(mod->klp_info);
2168 static void free_module_elf(struct module *mod)
2170 kfree(mod->klp_info->sechdrs);
2171 kfree(mod->klp_info->secstrings);
2172 kfree(mod->klp_info);
2174 #else /* !CONFIG_LIVEPATCH */
2175 static int copy_module_elf(struct module *mod, struct load_info *info)
2180 static void free_module_elf(struct module *mod)
2183 #endif /* CONFIG_LIVEPATCH */
2185 void __weak module_memfree(void *module_region)
2187 vfree(module_region);
2190 void __weak module_arch_cleanup(struct module *mod)
2194 void __weak module_arch_freeing_init(struct module *mod)
2198 /* Free a module, remove from lists, etc. */
2199 static void free_module(struct module *mod)
2201 trace_module_free(mod);
2203 mod_sysfs_teardown(mod);
2205 /* We leave it in list to prevent duplicate loads, but make sure
2206 * that noone uses it while it's being deconstructed. */
2207 mutex_lock(&module_mutex);
2208 mod->state = MODULE_STATE_UNFORMED;
2209 mutex_unlock(&module_mutex);
2211 /* Remove dynamic debug info */
2212 ddebug_remove_module(mod->name);
2214 /* Arch-specific cleanup. */
2215 module_arch_cleanup(mod);
2217 /* Module unload stuff */
2218 module_unload_free(mod);
2220 /* Free any allocated parameters. */
2221 destroy_params(mod->kp, mod->num_kp);
2223 if (is_livepatch_module(mod))
2224 free_module_elf(mod);
2226 /* Now we can delete it from the lists */
2227 mutex_lock(&module_mutex);
2228 /* Unlink carefully: kallsyms could be walking list. */
2229 list_del_rcu(&mod->list);
2230 mod_tree_remove(mod);
2231 /* Remove this module from bug list, this uses list_del_rcu */
2232 module_bug_cleanup(mod);
2233 /* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */
2234 synchronize_sched();
2235 mutex_unlock(&module_mutex);
2237 /* This may be empty, but that's OK */
2238 disable_ro_nx(&mod->init_layout);
2239 module_arch_freeing_init(mod);
2240 module_memfree(mod->init_layout.base);
2242 percpu_modfree(mod);
2244 /* Free lock-classes; relies on the preceding sync_rcu(). */
2245 lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);
2247 /* Finally, free the core (containing the module structure) */
2248 disable_ro_nx(&mod->core_layout);
2249 module_memfree(mod->core_layout.base);
2252 void *__symbol_get(const char *symbol)
2254 struct module *owner;
2255 const struct kernel_symbol *sym;
2258 sym = find_symbol(symbol, &owner, NULL, NULL, true, true);
2259 if (sym && strong_try_module_get(owner))
2263 return sym ? (void *)kernel_symbol_value(sym) : NULL;
2265 EXPORT_SYMBOL_GPL(__symbol_get);
2268 * Ensure that an exported symbol [global namespace] does not already exist
2269 * in the kernel or in some other module's exported symbol table.
2271 * You must hold the module_mutex.
2273 static int verify_export_symbols(struct module *mod)
2276 struct module *owner;
2277 const struct kernel_symbol *s;
2279 const struct kernel_symbol *sym;
2282 { mod->syms, mod->num_syms },
2283 { mod->gpl_syms, mod->num_gpl_syms },
2284 { mod->gpl_future_syms, mod->num_gpl_future_syms },
2285 #ifdef CONFIG_UNUSED_SYMBOLS
2286 { mod->unused_syms, mod->num_unused_syms },
2287 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
2291 for (i = 0; i < ARRAY_SIZE(arr); i++) {
2292 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
2293 if (find_symbol(kernel_symbol_name(s), &owner, NULL,
2294 NULL, true, false)) {
2295 pr_err("%s: exports duplicate symbol %s"
2297 mod->name, kernel_symbol_name(s),
2298 module_name(owner));
2306 static bool ignore_undef_symbol(Elf_Half emachine, const char *name)
2309 * On x86, PIC code and Clang non-PIC code may have call foo@PLT. GNU as
2310 * before 2.37 produces an unreferenced _GLOBAL_OFFSET_TABLE_ on x86-64.
2311 * i386 has a similar problem but may not deserve a fix.
2313 * If we ever have to ignore many symbols, consider refactoring the code to
2314 * only warn if referenced by a relocation.
2316 if (emachine == EM_386 || emachine == EM_X86_64)
2317 return !strcmp(name, "_GLOBAL_OFFSET_TABLE_");
2321 /* Change all symbols so that st_value encodes the pointer directly. */
2322 static int simplify_symbols(struct module *mod, const struct load_info *info)
2324 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
2325 Elf_Sym *sym = (void *)symsec->sh_addr;
2326 unsigned long secbase;
2329 const struct kernel_symbol *ksym;
2331 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
2332 const char *name = info->strtab + sym[i].st_name;
2334 switch (sym[i].st_shndx) {
2336 /* Ignore common symbols */
2337 if (!strncmp(name, "__gnu_lto", 9))
2340 /* We compiled with -fno-common. These are not
2341 supposed to happen. */
2342 pr_debug("Common symbol: %s\n", name);
2343 pr_warn("%s: please compile with -fno-common\n",
2349 /* Don't need to do anything */
2350 pr_debug("Absolute symbol: 0x%08lx\n",
2351 (long)sym[i].st_value);
2355 /* Livepatch symbols are resolved by livepatch */
2359 ksym = resolve_symbol_wait(mod, info, name);
2360 /* Ok if resolved. */
2361 if (ksym && !IS_ERR(ksym)) {
2362 sym[i].st_value = kernel_symbol_value(ksym);
2366 /* Ok if weak or ignored. */
2368 (ELF_ST_BIND(sym[i].st_info) == STB_WEAK ||
2369 ignore_undef_symbol(info->hdr->e_machine, name)))
2372 ret = PTR_ERR(ksym) ?: -ENOENT;
2373 pr_warn("%s: Unknown symbol %s (err %d)\n",
2374 mod->name, name, ret);
2378 /* Divert to percpu allocation if a percpu var. */
2379 if (sym[i].st_shndx == info->index.pcpu)
2380 secbase = (unsigned long)mod_percpu(mod);
2382 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
2383 sym[i].st_value += secbase;
2391 static int apply_relocations(struct module *mod, const struct load_info *info)
2396 /* Now do relocations. */
2397 for (i = 1; i < info->hdr->e_shnum; i++) {
2398 unsigned int infosec = info->sechdrs[i].sh_info;
2400 /* Not a valid relocation section? */
2401 if (infosec >= info->hdr->e_shnum)
2404 /* Don't bother with non-allocated sections */
2405 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
2408 /* Livepatch relocation sections are applied by livepatch */
2409 if (info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH)
2412 if (info->sechdrs[i].sh_type == SHT_REL)
2413 err = apply_relocate(info->sechdrs, info->strtab,
2414 info->index.sym, i, mod);
2415 else if (info->sechdrs[i].sh_type == SHT_RELA)
2416 err = apply_relocate_add(info->sechdrs, info->strtab,
2417 info->index.sym, i, mod);
2424 /* Additional bytes needed by arch in front of individual sections */
2425 unsigned int __weak arch_mod_section_prepend(struct module *mod,
2426 unsigned int section)
2428 /* default implementation just returns zero */
2432 /* Update size with this section: return offset. */
2433 static long get_offset(struct module *mod, unsigned int *size,
2434 Elf_Shdr *sechdr, unsigned int section)
2438 *size += arch_mod_section_prepend(mod, section);
2439 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
2440 *size = ret + sechdr->sh_size;
2444 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
2445 might -- code, read-only data, read-write data, small data. Tally
2446 sizes, and place the offsets into sh_entsize fields: high bit means it
2448 static void layout_sections(struct module *mod, struct load_info *info)
2450 static unsigned long const masks[][2] = {
2451 /* NOTE: all executable code must be the first section
2452 * in this array; otherwise modify the text_size
2453 * finder in the two loops below */
2454 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
2455 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
2456 { SHF_RO_AFTER_INIT | SHF_ALLOC, ARCH_SHF_SMALL },
2457 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
2458 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
2462 for (i = 0; i < info->hdr->e_shnum; i++)
2463 info->sechdrs[i].sh_entsize = ~0UL;
2465 pr_debug("Core section allocation order:\n");
2466 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
2467 for (i = 0; i < info->hdr->e_shnum; ++i) {
2468 Elf_Shdr *s = &info->sechdrs[i];
2469 const char *sname = info->secstrings + s->sh_name;
2471 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2472 || (s->sh_flags & masks[m][1])
2473 || s->sh_entsize != ~0UL
2474 || strstarts(sname, ".init"))
2476 s->sh_entsize = get_offset(mod, &mod->core_layout.size, s, i);
2477 pr_debug("\t%s\n", sname);
2480 case 0: /* executable */
2481 mod->core_layout.size = debug_align(mod->core_layout.size);
2482 mod->core_layout.text_size = mod->core_layout.size;
2484 case 1: /* RO: text and ro-data */
2485 mod->core_layout.size = debug_align(mod->core_layout.size);
2486 mod->core_layout.ro_size = mod->core_layout.size;
2488 case 2: /* RO after init */
2489 mod->core_layout.size = debug_align(mod->core_layout.size);
2490 mod->core_layout.ro_after_init_size = mod->core_layout.size;
2492 case 4: /* whole core */
2493 mod->core_layout.size = debug_align(mod->core_layout.size);
2498 pr_debug("Init section allocation order:\n");
2499 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
2500 for (i = 0; i < info->hdr->e_shnum; ++i) {
2501 Elf_Shdr *s = &info->sechdrs[i];
2502 const char *sname = info->secstrings + s->sh_name;
2504 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2505 || (s->sh_flags & masks[m][1])
2506 || s->sh_entsize != ~0UL
2507 || !strstarts(sname, ".init"))
2509 s->sh_entsize = (get_offset(mod, &mod->init_layout.size, s, i)
2510 | INIT_OFFSET_MASK);
2511 pr_debug("\t%s\n", sname);
2514 case 0: /* executable */
2515 mod->init_layout.size = debug_align(mod->init_layout.size);
2516 mod->init_layout.text_size = mod->init_layout.size;
2518 case 1: /* RO: text and ro-data */
2519 mod->init_layout.size = debug_align(mod->init_layout.size);
2520 mod->init_layout.ro_size = mod->init_layout.size;
2524 * RO after init doesn't apply to init_layout (only
2525 * core_layout), so it just takes the value of ro_size.
2527 mod->init_layout.ro_after_init_size = mod->init_layout.ro_size;
2529 case 4: /* whole init */
2530 mod->init_layout.size = debug_align(mod->init_layout.size);
2536 static void set_license(struct module *mod, const char *license)
2539 license = "unspecified";
2541 if (!license_is_gpl_compatible(license)) {
2542 if (!test_taint(TAINT_PROPRIETARY_MODULE))
2543 pr_warn("%s: module license '%s' taints kernel.\n",
2544 mod->name, license);
2545 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
2546 LOCKDEP_NOW_UNRELIABLE);
2550 /* Parse tag=value strings from .modinfo section */
2551 static char *next_string(char *string, unsigned long *secsize)
2553 /* Skip non-zero chars */
2556 if ((*secsize)-- <= 1)
2560 /* Skip any zero padding. */
2561 while (!string[0]) {
2563 if ((*secsize)-- <= 1)
2569 static char *get_modinfo(struct load_info *info, const char *tag)
2572 unsigned int taglen = strlen(tag);
2573 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2574 unsigned long size = infosec->sh_size;
2577 * get_modinfo() calls made before rewrite_section_headers()
2578 * must use sh_offset, as sh_addr isn't set!
2580 for (p = (char *)info->hdr + infosec->sh_offset; p; p = next_string(p, &size)) {
2581 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2582 return p + taglen + 1;
2587 static void setup_modinfo(struct module *mod, struct load_info *info)
2589 struct module_attribute *attr;
2592 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2594 attr->setup(mod, get_modinfo(info, attr->attr.name));
2598 static void free_modinfo(struct module *mod)
2600 struct module_attribute *attr;
2603 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2609 #ifdef CONFIG_KALLSYMS
2611 /* lookup symbol in given range of kernel_symbols */
2612 static const struct kernel_symbol *lookup_symbol(const char *name,
2613 const struct kernel_symbol *start,
2614 const struct kernel_symbol *stop)
2616 return bsearch(name, start, stop - start,
2617 sizeof(struct kernel_symbol), cmp_name);
2620 static int is_exported(const char *name, unsigned long value,
2621 const struct module *mod)
2623 const struct kernel_symbol *ks;
2625 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
2627 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
2628 return ks != NULL && kernel_symbol_value(ks) == value;
2632 static char elf_type(const Elf_Sym *sym, const struct load_info *info)
2634 const Elf_Shdr *sechdrs = info->sechdrs;
2636 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2637 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2642 if (sym->st_shndx == SHN_UNDEF)
2644 if (sym->st_shndx == SHN_ABS || sym->st_shndx == info->index.pcpu)
2646 if (sym->st_shndx >= SHN_LORESERVE)
2648 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2650 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2651 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2652 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2654 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2659 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2660 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2665 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2672 static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
2673 unsigned int shnum, unsigned int pcpundx)
2675 const Elf_Shdr *sec;
2677 if (src->st_shndx == SHN_UNDEF
2678 || src->st_shndx >= shnum
2682 #ifdef CONFIG_KALLSYMS_ALL
2683 if (src->st_shndx == pcpundx)
2687 sec = sechdrs + src->st_shndx;
2688 if (!(sec->sh_flags & SHF_ALLOC)
2689 #ifndef CONFIG_KALLSYMS_ALL
2690 || !(sec->sh_flags & SHF_EXECINSTR)
2692 || (sec->sh_entsize & INIT_OFFSET_MASK))
2699 * We only allocate and copy the strings needed by the parts of symtab
2700 * we keep. This is simple, but has the effect of making multiple
2701 * copies of duplicates. We could be more sophisticated, see
2702 * linux-kernel thread starting with
2703 * <73defb5e4bca04a6431392cc341112b1@localhost>.
2705 static void layout_symtab(struct module *mod, struct load_info *info)
2707 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2708 Elf_Shdr *strsect = info->sechdrs + info->index.str;
2710 unsigned int i, nsrc, ndst, strtab_size = 0;
2712 /* Put symbol section at end of init part of module. */
2713 symsect->sh_flags |= SHF_ALLOC;
2714 symsect->sh_entsize = get_offset(mod, &mod->init_layout.size, symsect,
2715 info->index.sym) | INIT_OFFSET_MASK;
2716 pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
2718 src = (void *)info->hdr + symsect->sh_offset;
2719 nsrc = symsect->sh_size / sizeof(*src);
2721 /* Compute total space required for the core symbols' strtab. */
2722 for (ndst = i = 0; i < nsrc; i++) {
2723 if (i == 0 || is_livepatch_module(mod) ||
2724 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum,
2725 info->index.pcpu)) {
2726 strtab_size += strlen(&info->strtab[src[i].st_name])+1;
2731 /* Append room for core symbols at end of core part. */
2732 info->symoffs = ALIGN(mod->core_layout.size, symsect->sh_addralign ?: 1);
2733 info->stroffs = mod->core_layout.size = info->symoffs + ndst * sizeof(Elf_Sym);
2734 mod->core_layout.size += strtab_size;
2735 mod->core_layout.size = debug_align(mod->core_layout.size);
2737 /* Put string table section at end of init part of module. */
2738 strsect->sh_flags |= SHF_ALLOC;
2739 strsect->sh_entsize = get_offset(mod, &mod->init_layout.size, strsect,
2740 info->index.str) | INIT_OFFSET_MASK;
2741 pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
2743 /* We'll tack temporary mod_kallsyms on the end. */
2744 mod->init_layout.size = ALIGN(mod->init_layout.size,
2745 __alignof__(struct mod_kallsyms));
2746 info->mod_kallsyms_init_off = mod->init_layout.size;
2747 mod->init_layout.size += sizeof(struct mod_kallsyms);
2748 mod->init_layout.size = debug_align(mod->init_layout.size);
2752 * We use the full symtab and strtab which layout_symtab arranged to
2753 * be appended to the init section. Later we switch to the cut-down
2756 static void add_kallsyms(struct module *mod, const struct load_info *info)
2758 unsigned int i, ndst;
2762 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
2764 /* Set up to point into init section. */
2765 mod->kallsyms = mod->init_layout.base + info->mod_kallsyms_init_off;
2767 mod->kallsyms->symtab = (void *)symsec->sh_addr;
2768 mod->kallsyms->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
2769 /* Make sure we get permanent strtab: don't use info->strtab. */
2770 mod->kallsyms->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
2772 /* Set types up while we still have access to sections. */
2773 for (i = 0; i < mod->kallsyms->num_symtab; i++)
2774 mod->kallsyms->symtab[i].st_info
2775 = elf_type(&mod->kallsyms->symtab[i], info);
2777 /* Now populate the cut down core kallsyms for after init. */
2778 mod->core_kallsyms.symtab = dst = mod->core_layout.base + info->symoffs;
2779 mod->core_kallsyms.strtab = s = mod->core_layout.base + info->stroffs;
2780 src = mod->kallsyms->symtab;
2781 for (ndst = i = 0; i < mod->kallsyms->num_symtab; i++) {
2782 if (i == 0 || is_livepatch_module(mod) ||
2783 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum,
2784 info->index.pcpu)) {
2786 dst[ndst++].st_name = s - mod->core_kallsyms.strtab;
2787 s += strlcpy(s, &mod->kallsyms->strtab[src[i].st_name],
2791 mod->core_kallsyms.num_symtab = ndst;
2794 static inline void layout_symtab(struct module *mod, struct load_info *info)
2798 static void add_kallsyms(struct module *mod, const struct load_info *info)
2801 #endif /* CONFIG_KALLSYMS */
2803 static void dynamic_debug_setup(struct module *mod, struct _ddebug *debug, unsigned int num)
2807 #ifdef CONFIG_DYNAMIC_DEBUG
2808 if (ddebug_add_module(debug, num, mod->name))
2809 pr_err("dynamic debug error adding module: %s\n",
2814 static void dynamic_debug_remove(struct module *mod, struct _ddebug *debug)
2817 ddebug_remove_module(mod->name);
2820 void * __weak module_alloc(unsigned long size)
2822 return vmalloc_exec(size);
2825 #ifdef CONFIG_DEBUG_KMEMLEAK
2826 static void kmemleak_load_module(const struct module *mod,
2827 const struct load_info *info)
2831 /* only scan the sections containing data */
2832 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
2834 for (i = 1; i < info->hdr->e_shnum; i++) {
2835 /* Scan all writable sections that's not executable */
2836 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) ||
2837 !(info->sechdrs[i].sh_flags & SHF_WRITE) ||
2838 (info->sechdrs[i].sh_flags & SHF_EXECINSTR))
2841 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2842 info->sechdrs[i].sh_size, GFP_KERNEL);
2846 static inline void kmemleak_load_module(const struct module *mod,
2847 const struct load_info *info)
2852 #ifdef CONFIG_MODULE_SIG
2853 static int module_sig_check(struct load_info *info, int flags)
2856 const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
2857 const void *mod = info->hdr;
2860 * Require flags == 0, as a module with version information
2861 * removed is no longer the module that was signed
2864 info->len > markerlen &&
2865 memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
2866 /* We truncate the module to discard the signature */
2867 info->len -= markerlen;
2868 err = mod_verify_sig(mod, info);
2872 info->sig_ok = true;
2876 /* Not having a signature is only an error if we're strict. */
2877 if (err == -ENOKEY && !is_module_sig_enforced())
2882 #else /* !CONFIG_MODULE_SIG */
2883 static int module_sig_check(struct load_info *info, int flags)
2887 #endif /* !CONFIG_MODULE_SIG */
2889 /* Sanity checks against invalid binaries, wrong arch, weird elf version. */
2890 static int elf_header_check(struct load_info *info)
2892 if (info->len < sizeof(*(info->hdr)))
2895 if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0
2896 || info->hdr->e_type != ET_REL
2897 || !elf_check_arch(info->hdr)
2898 || info->hdr->e_shentsize != sizeof(Elf_Shdr))
2901 if (info->hdr->e_shoff >= info->len
2902 || (info->hdr->e_shnum * sizeof(Elf_Shdr) >
2903 info->len - info->hdr->e_shoff))
2909 #define COPY_CHUNK_SIZE (16*PAGE_SIZE)
2911 static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned long len)
2914 unsigned long n = min(len, COPY_CHUNK_SIZE);
2916 if (copy_from_user(dst, usrc, n) != 0)
2926 #ifdef CONFIG_LIVEPATCH
2927 static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
2929 if (get_modinfo(info, "livepatch")) {
2931 add_taint_module(mod, TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
2932 pr_notice_once("%s: tainting kernel with TAINT_LIVEPATCH\n",
2938 #else /* !CONFIG_LIVEPATCH */
2939 static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
2941 if (get_modinfo(info, "livepatch")) {
2942 pr_err("%s: module is marked as livepatch module, but livepatch support is disabled",
2949 #endif /* CONFIG_LIVEPATCH */
2951 static void check_modinfo_retpoline(struct module *mod, struct load_info *info)
2953 if (retpoline_module_ok(get_modinfo(info, "retpoline")))
2956 pr_warn("%s: loading module not compiled with retpoline compiler.\n",
2960 /* Sets info->hdr and info->len. */
2961 static int copy_module_from_user(const void __user *umod, unsigned long len,
2962 struct load_info *info)
2967 if (info->len < sizeof(*(info->hdr)))
2970 err = security_kernel_load_data(LOADING_MODULE);
2974 /* Suck in entire file: we'll want most of it. */
2975 info->hdr = __vmalloc(info->len,
2976 GFP_KERNEL | __GFP_NOWARN, PAGE_KERNEL);
2980 if (copy_chunked_from_user(info->hdr, umod, info->len) != 0) {
2988 static void free_copy(struct load_info *info)
2993 static int rewrite_section_headers(struct load_info *info, int flags)
2997 /* This should always be true, but let's be sure. */
2998 info->sechdrs[0].sh_addr = 0;
3000 for (i = 1; i < info->hdr->e_shnum; i++) {
3001 Elf_Shdr *shdr = &info->sechdrs[i];
3002 if (shdr->sh_type != SHT_NOBITS
3003 && info->len < shdr->sh_offset + shdr->sh_size) {
3004 pr_err("Module len %lu truncated\n", info->len);
3008 /* Mark all sections sh_addr with their address in the
3010 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
3012 #ifndef CONFIG_MODULE_UNLOAD
3013 /* Don't load .exit sections */
3014 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
3015 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
3019 /* Track but don't keep modinfo and version sections. */
3020 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
3021 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
3027 * Set up our basic convenience variables (pointers to section headers,
3028 * search for module section index etc), and do some basic section
3031 * Set info->mod to the temporary copy of the module in info->hdr. The final one
3032 * will be allocated in move_module().
3034 static int setup_load_info(struct load_info *info, int flags)
3038 /* Set up the convenience variables */
3039 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
3040 info->secstrings = (void *)info->hdr
3041 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
3043 /* Try to find a name early so we can log errors with a module name */
3044 info->index.info = find_sec(info, ".modinfo");
3045 if (info->index.info)
3046 info->name = get_modinfo(info, "name");
3048 /* Find internal symbols and strings. */
3049 for (i = 1; i < info->hdr->e_shnum; i++) {
3050 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
3051 info->index.sym = i;
3052 info->index.str = info->sechdrs[i].sh_link;
3053 info->strtab = (char *)info->hdr
3054 + info->sechdrs[info->index.str].sh_offset;
3059 if (info->index.sym == 0) {
3060 pr_warn("%s: module has no symbols (stripped?)\n",
3061 info->name ?: "(missing .modinfo section or name field)");
3065 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
3066 if (!info->index.mod) {
3067 pr_warn("%s: No module found in object\n",
3068 info->name ?: "(missing .modinfo section or name field)");
3071 /* This is temporary: point mod into copy of data. */
3072 info->mod = (void *)info->hdr + info->sechdrs[info->index.mod].sh_offset;
3075 * If we didn't load the .modinfo 'name' field earlier, fall back to
3076 * on-disk struct mod 'name' field.
3079 info->name = info->mod->name;
3081 if (flags & MODULE_INIT_IGNORE_MODVERSIONS)
3082 info->index.vers = 0; /* Pretend no __versions section! */
3084 info->index.vers = find_sec(info, "__versions");
3086 info->index.pcpu = find_pcpusec(info);
3091 static int check_modinfo(struct module *mod, struct load_info *info, int flags)
3093 const char *modmagic = get_modinfo(info, "vermagic");
3096 if (flags & MODULE_INIT_IGNORE_VERMAGIC)
3099 /* This is allowed: modprobe --force will invalidate it. */
3101 err = try_to_force_load(mod, "bad vermagic");
3104 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
3105 pr_err("%s: version magic '%s' should be '%s'\n",
3106 info->name, modmagic, vermagic);
3110 if (!get_modinfo(info, "intree")) {
3111 if (!test_taint(TAINT_OOT_MODULE))
3112 pr_warn("%s: loading out-of-tree module taints kernel.\n",
3114 add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK);
3117 check_modinfo_retpoline(mod, info);
3119 if (get_modinfo(info, "staging")) {
3120 add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK);
3121 pr_warn("%s: module is from the staging directory, the quality "
3122 "is unknown, you have been warned.\n", mod->name);
3125 err = check_modinfo_livepatch(mod, info);
3129 /* Set up license info based on the info section */
3130 set_license(mod, get_modinfo(info, "license"));
3135 static int find_module_sections(struct module *mod, struct load_info *info)
3137 mod->kp = section_objs(info, "__param",
3138 sizeof(*mod->kp), &mod->num_kp);
3139 mod->syms = section_objs(info, "__ksymtab",
3140 sizeof(*mod->syms), &mod->num_syms);
3141 mod->crcs = section_addr(info, "__kcrctab");
3142 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
3143 sizeof(*mod->gpl_syms),
3144 &mod->num_gpl_syms);
3145 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
3146 mod->gpl_future_syms = section_objs(info,
3147 "__ksymtab_gpl_future",
3148 sizeof(*mod->gpl_future_syms),
3149 &mod->num_gpl_future_syms);
3150 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
3152 #ifdef CONFIG_UNUSED_SYMBOLS
3153 mod->unused_syms = section_objs(info, "__ksymtab_unused",
3154 sizeof(*mod->unused_syms),
3155 &mod->num_unused_syms);
3156 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
3157 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
3158 sizeof(*mod->unused_gpl_syms),
3159 &mod->num_unused_gpl_syms);
3160 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
3162 #ifdef CONFIG_CONSTRUCTORS
3163 mod->ctors = section_objs(info, ".ctors",
3164 sizeof(*mod->ctors), &mod->num_ctors);
3166 mod->ctors = section_objs(info, ".init_array",
3167 sizeof(*mod->ctors), &mod->num_ctors);
3168 else if (find_sec(info, ".init_array")) {
3170 * This shouldn't happen with same compiler and binutils
3171 * building all parts of the module.
3173 pr_warn("%s: has both .ctors and .init_array.\n",
3179 #ifdef CONFIG_TRACEPOINTS
3180 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
3181 sizeof(*mod->tracepoints_ptrs),
3182 &mod->num_tracepoints);
3184 #ifdef CONFIG_JUMP_LABEL
3185 mod->jump_entries = section_objs(info, "__jump_table",
3186 sizeof(*mod->jump_entries),
3187 &mod->num_jump_entries);
3189 #ifdef CONFIG_EVENT_TRACING
3190 mod->trace_events = section_objs(info, "_ftrace_events",
3191 sizeof(*mod->trace_events),
3192 &mod->num_trace_events);
3193 mod->trace_evals = section_objs(info, "_ftrace_eval_map",
3194 sizeof(*mod->trace_evals),
3195 &mod->num_trace_evals);
3197 #ifdef CONFIG_TRACING
3198 mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
3199 sizeof(*mod->trace_bprintk_fmt_start),
3200 &mod->num_trace_bprintk_fmt);
3202 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
3203 /* sechdrs[0].sh_size is always zero */
3204 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
3205 sizeof(*mod->ftrace_callsites),
3206 &mod->num_ftrace_callsites);
3208 #ifdef CONFIG_FUNCTION_ERROR_INJECTION
3209 mod->ei_funcs = section_objs(info, "_error_injection_whitelist",
3210 sizeof(*mod->ei_funcs),
3211 &mod->num_ei_funcs);
3213 mod->extable = section_objs(info, "__ex_table",
3214 sizeof(*mod->extable), &mod->num_exentries);
3216 if (section_addr(info, "__obsparm"))
3217 pr_warn("%s: Ignoring obsolete parameters\n", mod->name);
3219 info->debug = section_objs(info, "__verbose",
3220 sizeof(*info->debug), &info->num_debug);
3225 static int move_module(struct module *mod, struct load_info *info)
3230 /* Do the allocs. */
3231 ptr = module_alloc(mod->core_layout.size);
3233 * The pointer to this block is stored in the module structure
3234 * which is inside the block. Just mark it as not being a
3237 kmemleak_not_leak(ptr);
3241 memset(ptr, 0, mod->core_layout.size);
3242 mod->core_layout.base = ptr;
3244 if (mod->init_layout.size) {
3245 ptr = module_alloc(mod->init_layout.size);
3247 * The pointer to this block is stored in the module structure
3248 * which is inside the block. This block doesn't need to be
3249 * scanned as it contains data and code that will be freed
3250 * after the module is initialized.
3252 kmemleak_ignore(ptr);
3254 module_memfree(mod->core_layout.base);
3257 memset(ptr, 0, mod->init_layout.size);
3258 mod->init_layout.base = ptr;
3260 mod->init_layout.base = NULL;
3262 /* Transfer each section which specifies SHF_ALLOC */
3263 pr_debug("final section addresses:\n");
3264 for (i = 0; i < info->hdr->e_shnum; i++) {
3266 Elf_Shdr *shdr = &info->sechdrs[i];
3268 if (!(shdr->sh_flags & SHF_ALLOC))
3271 if (shdr->sh_entsize & INIT_OFFSET_MASK)
3272 dest = mod->init_layout.base
3273 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
3275 dest = mod->core_layout.base + shdr->sh_entsize;
3277 if (shdr->sh_type != SHT_NOBITS)
3278 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
3279 /* Update sh_addr to point to copy in image. */
3280 shdr->sh_addr = (unsigned long)dest;
3281 pr_debug("\t0x%lx %s\n",
3282 (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
3288 static int check_module_license_and_versions(struct module *mod)
3290 int prev_taint = test_taint(TAINT_PROPRIETARY_MODULE);
3293 * ndiswrapper is under GPL by itself, but loads proprietary modules.
3294 * Don't use add_taint_module(), as it would prevent ndiswrapper from
3295 * using GPL-only symbols it needs.
3297 if (strcmp(mod->name, "ndiswrapper") == 0)
3298 add_taint(TAINT_PROPRIETARY_MODULE, LOCKDEP_NOW_UNRELIABLE);
3300 /* driverloader was caught wrongly pretending to be under GPL */
3301 if (strcmp(mod->name, "driverloader") == 0)
3302 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
3303 LOCKDEP_NOW_UNRELIABLE);
3305 /* lve claims to be GPL but upstream won't provide source */
3306 if (strcmp(mod->name, "lve") == 0)
3307 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
3308 LOCKDEP_NOW_UNRELIABLE);
3310 if (!prev_taint && test_taint(TAINT_PROPRIETARY_MODULE))
3311 pr_warn("%s: module license taints kernel.\n", mod->name);
3313 #ifdef CONFIG_MODVERSIONS
3314 if ((mod->num_syms && !mod->crcs)
3315 || (mod->num_gpl_syms && !mod->gpl_crcs)
3316 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
3317 #ifdef CONFIG_UNUSED_SYMBOLS
3318 || (mod->num_unused_syms && !mod->unused_crcs)
3319 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
3322 return try_to_force_load(mod,
3323 "no versions for exported symbols");
3329 static void flush_module_icache(const struct module *mod)
3331 mm_segment_t old_fs;
3333 /* flush the icache in correct context */
3338 * Flush the instruction cache, since we've played with text.
3339 * Do it before processing of module parameters, so the module
3340 * can provide parameter accessor functions of its own.
3342 if (mod->init_layout.base)
3343 flush_icache_range((unsigned long)mod->init_layout.base,
3344 (unsigned long)mod->init_layout.base
3345 + mod->init_layout.size);
3346 flush_icache_range((unsigned long)mod->core_layout.base,
3347 (unsigned long)mod->core_layout.base + mod->core_layout.size);
3352 int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
3360 /* module_blacklist is a comma-separated list of module names */
3361 static char *module_blacklist;
3362 static bool blacklisted(const char *module_name)
3367 if (!module_blacklist)
3370 for (p = module_blacklist; *p; p += len) {
3371 len = strcspn(p, ",");
3372 if (strlen(module_name) == len && !memcmp(module_name, p, len))
3379 core_param(module_blacklist, module_blacklist, charp, 0400);
3381 static struct module *layout_and_allocate(struct load_info *info, int flags)
3387 err = check_modinfo(info->mod, info, flags);
3389 return ERR_PTR(err);
3391 /* Allow arches to frob section contents and sizes. */
3392 err = module_frob_arch_sections(info->hdr, info->sechdrs,
3393 info->secstrings, info->mod);
3395 return ERR_PTR(err);
3397 /* We will do a special allocation for per-cpu sections later. */
3398 info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
3401 * Mark ro_after_init section with SHF_RO_AFTER_INIT so that
3402 * layout_sections() can put it in the right place.
3403 * Note: ro_after_init sections also have SHF_{WRITE,ALLOC} set.
3405 ndx = find_sec(info, ".data..ro_after_init");
3407 info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT;
3409 /* Determine total sizes, and put offsets in sh_entsize. For now
3410 this is done generically; there doesn't appear to be any
3411 special cases for the architectures. */
3412 layout_sections(info->mod, info);
3413 layout_symtab(info->mod, info);
3415 /* Allocate and move to the final place */
3416 err = move_module(info->mod, info);
3418 return ERR_PTR(err);
3420 /* Module has been copied to its final place now: return it. */
3421 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
3422 kmemleak_load_module(mod, info);
3426 /* mod is no longer valid after this! */
3427 static void module_deallocate(struct module *mod, struct load_info *info)
3429 percpu_modfree(mod);
3430 module_arch_freeing_init(mod);
3431 module_memfree(mod->init_layout.base);
3432 module_memfree(mod->core_layout.base);
3435 int __weak module_finalize(const Elf_Ehdr *hdr,
3436 const Elf_Shdr *sechdrs,
3442 static int post_relocation(struct module *mod, const struct load_info *info)
3444 /* Sort exception table now relocations are done. */
3445 sort_extable(mod->extable, mod->extable + mod->num_exentries);
3447 /* Copy relocated percpu area over. */
3448 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
3449 info->sechdrs[info->index.pcpu].sh_size);
3451 /* Setup kallsyms-specific fields. */
3452 add_kallsyms(mod, info);
3454 /* Arch-specific module finalizing. */
3455 return module_finalize(info->hdr, info->sechdrs, mod);
3458 /* Is this module of this name done loading? No locks held. */
3459 static bool finished_loading(const char *name)
3465 * The module_mutex should not be a heavily contended lock;
3466 * if we get the occasional sleep here, we'll go an extra iteration
3467 * in the wait_event_interruptible(), which is harmless.
3469 sched_annotate_sleep();
3470 mutex_lock(&module_mutex);
3471 mod = find_module_all(name, strlen(name), true);
3472 ret = !mod || mod->state == MODULE_STATE_LIVE;
3473 mutex_unlock(&module_mutex);
3478 /* Call module constructors. */
3479 static void do_mod_ctors(struct module *mod)
3481 #ifdef CONFIG_CONSTRUCTORS
3484 for (i = 0; i < mod->num_ctors; i++)
3489 /* For freeing module_init on success, in case kallsyms traversing */
3490 struct mod_initfree {
3491 struct rcu_head rcu;
3495 static void do_free_init(struct rcu_head *head)
3497 struct mod_initfree *m = container_of(head, struct mod_initfree, rcu);
3498 module_memfree(m->module_init);
3503 * This is where the real work happens.
3505 * Keep it uninlined to provide a reliable breakpoint target, e.g. for the gdb
3506 * helper command 'lx-symbols'.
3508 static noinline int do_init_module(struct module *mod)
3511 struct mod_initfree *freeinit;
3513 freeinit = kmalloc(sizeof(*freeinit), GFP_KERNEL);
3518 freeinit->module_init = mod->init_layout.base;
3521 * We want to find out whether @mod uses async during init. Clear
3522 * PF_USED_ASYNC. async_schedule*() will set it.
3524 current->flags &= ~PF_USED_ASYNC;
3527 /* Start the module */
3528 if (mod->init != NULL)
3529 ret = do_one_initcall(mod->init);
3531 goto fail_free_freeinit;
3534 pr_warn("%s: '%s'->init suspiciously returned %d, it should "
3535 "follow 0/-E convention\n"
3536 "%s: loading module anyway...\n",
3537 __func__, mod->name, ret, __func__);
3541 /* Now it's a first class citizen! */
3542 mod->state = MODULE_STATE_LIVE;
3543 blocking_notifier_call_chain(&module_notify_list,
3544 MODULE_STATE_LIVE, mod);
3546 /* Delay uevent until module has finished its init routine */
3547 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
3550 * We need to finish all async code before the module init sequence
3551 * is done. This has potential to deadlock. For example, a newly
3552 * detected block device can trigger request_module() of the
3553 * default iosched from async probing task. Once userland helper
3554 * reaches here, async_synchronize_full() will wait on the async
3555 * task waiting on request_module() and deadlock.
3557 * This deadlock is avoided by perfomring async_synchronize_full()
3558 * iff module init queued any async jobs. This isn't a full
3559 * solution as it will deadlock the same if module loading from
3560 * async jobs nests more than once; however, due to the various
3561 * constraints, this hack seems to be the best option for now.
3562 * Please refer to the following thread for details.
3564 * http://thread.gmane.org/gmane.linux.kernel/1420814
3566 if (!mod->async_probe_requested && (current->flags & PF_USED_ASYNC))
3567 async_synchronize_full();
3569 ftrace_free_mem(mod, mod->init_layout.base, mod->init_layout.base +
3570 mod->init_layout.size);
3571 mutex_lock(&module_mutex);
3572 /* Drop initial reference. */
3574 trim_init_extable(mod);
3575 #ifdef CONFIG_KALLSYMS
3576 /* Switch to core kallsyms now init is done: kallsyms may be walking! */
3577 rcu_assign_pointer(mod->kallsyms, &mod->core_kallsyms);
3579 module_enable_ro(mod, true);
3580 mod_tree_remove_init(mod);
3581 disable_ro_nx(&mod->init_layout);
3582 module_arch_freeing_init(mod);
3583 mod->init_layout.base = NULL;
3584 mod->init_layout.size = 0;
3585 mod->init_layout.ro_size = 0;
3586 mod->init_layout.ro_after_init_size = 0;
3587 mod->init_layout.text_size = 0;
3589 * We want to free module_init, but be aware that kallsyms may be
3590 * walking this with preempt disabled. In all the failure paths, we
3591 * call synchronize_sched(), but we don't want to slow down the success
3592 * path, so use actual RCU here.
3593 * Note that module_alloc() on most architectures creates W+X page
3594 * mappings which won't be cleaned up until do_free_init() runs. Any
3595 * code such as mark_rodata_ro() which depends on those mappings to
3596 * be cleaned up needs to sync with the queued work - ie
3597 * rcu_barrier_sched()
3599 call_rcu_sched(&freeinit->rcu, do_free_init);
3600 mutex_unlock(&module_mutex);
3601 wake_up_all(&module_wq);
3608 /* Try to protect us from buggy refcounters. */
3609 mod->state = MODULE_STATE_GOING;
3610 synchronize_sched();
3612 blocking_notifier_call_chain(&module_notify_list,
3613 MODULE_STATE_GOING, mod);
3614 klp_module_going(mod);
3615 ftrace_release_mod(mod);
3617 wake_up_all(&module_wq);
3621 static int may_init_module(void)
3623 if (!capable(CAP_SYS_MODULE) || modules_disabled)
3630 * We try to place it in the list now to make sure it's unique before
3631 * we dedicate too many resources. In particular, temporary percpu
3632 * memory exhaustion.
3634 static int add_unformed_module(struct module *mod)
3639 mod->state = MODULE_STATE_UNFORMED;
3642 mutex_lock(&module_mutex);
3643 old = find_module_all(mod->name, strlen(mod->name), true);
3645 if (old->state != MODULE_STATE_LIVE) {
3646 /* Wait in case it fails to load. */
3647 mutex_unlock(&module_mutex);
3648 err = wait_event_interruptible(module_wq,
3649 finished_loading(mod->name));
3657 mod_update_bounds(mod);
3658 list_add_rcu(&mod->list, &modules);
3659 mod_tree_insert(mod);
3663 mutex_unlock(&module_mutex);
3668 static int complete_formation(struct module *mod, struct load_info *info)
3672 mutex_lock(&module_mutex);
3674 /* Find duplicate symbols (must be called under lock). */
3675 err = verify_export_symbols(mod);
3679 /* This relies on module_mutex for list integrity. */
3680 module_bug_finalize(info->hdr, info->sechdrs, mod);
3682 module_enable_ro(mod, false);
3683 module_enable_nx(mod);
3684 module_enable_x(mod);
3686 /* Mark state as coming so strong_try_module_get() ignores us,
3687 * but kallsyms etc. can see us. */
3688 mod->state = MODULE_STATE_COMING;
3689 mutex_unlock(&module_mutex);
3694 mutex_unlock(&module_mutex);
3698 static int prepare_coming_module(struct module *mod)
3702 ftrace_module_enable(mod);
3703 err = klp_module_coming(mod);
3707 blocking_notifier_call_chain(&module_notify_list,
3708 MODULE_STATE_COMING, mod);
3712 static int unknown_module_param_cb(char *param, char *val, const char *modname,
3715 struct module *mod = arg;
3718 if (strcmp(param, "async_probe") == 0) {
3719 mod->async_probe_requested = true;
3723 /* Check for magic 'dyndbg' arg */
3724 ret = ddebug_dyndbg_module_param_cb(param, val, modname);
3726 pr_warn("%s: unknown parameter '%s' ignored\n", modname, param);
3730 /* Allocate and load the module: note that size of section 0 is always
3731 zero, and we rely on this for optional sections. */
3732 static int load_module(struct load_info *info, const char __user *uargs,
3739 err = elf_header_check(info);
3743 err = setup_load_info(info, flags);
3747 if (blacklisted(info->name)) {
3752 err = module_sig_check(info, flags);
3756 err = rewrite_section_headers(info, flags);
3760 /* Check module struct version now, before we try to use module. */
3761 if (!check_modstruct_version(info, info->mod)) {
3766 /* Figure out module layout, and allocate all the memory. */
3767 mod = layout_and_allocate(info, flags);
3773 audit_log_kern_module(mod->name);
3775 /* Reserve our place in the list. */
3776 err = add_unformed_module(mod);
3780 #ifdef CONFIG_MODULE_SIG
3781 mod->sig_ok = info->sig_ok;
3783 pr_notice_once("%s: module verification failed: signature "
3784 "and/or required key missing - tainting "
3785 "kernel\n", mod->name);
3786 add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK);
3790 /* To avoid stressing percpu allocator, do this once we're unique. */
3791 err = percpu_modalloc(mod, info);
3795 /* Now module is in final location, initialize linked lists, etc. */
3796 err = module_unload_init(mod);
3800 init_param_lock(mod);
3802 /* Now we've got everything in the final locations, we can
3803 * find optional sections. */
3804 err = find_module_sections(mod, info);
3808 err = check_module_license_and_versions(mod);
3812 /* Set up MODINFO_ATTR fields */
3813 setup_modinfo(mod, info);
3815 /* Fix up syms, so that st_value is a pointer to location. */
3816 err = simplify_symbols(mod, info);
3820 err = apply_relocations(mod, info);
3824 err = post_relocation(mod, info);
3828 flush_module_icache(mod);
3830 /* Now copy in args */
3831 mod->args = strndup_user(uargs, ~0UL >> 1);
3832 if (IS_ERR(mod->args)) {
3833 err = PTR_ERR(mod->args);
3834 goto free_arch_cleanup;
3837 dynamic_debug_setup(mod, info->debug, info->num_debug);
3839 /* Ftrace init must be called in the MODULE_STATE_UNFORMED state */
3840 ftrace_module_init(mod);
3842 /* Finally it's fully formed, ready to start executing. */
3843 err = complete_formation(mod, info);
3845 goto ddebug_cleanup;
3847 err = prepare_coming_module(mod);
3851 /* Module is ready to execute: parsing args may do that. */
3852 after_dashes = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
3854 unknown_module_param_cb);
3855 if (IS_ERR(after_dashes)) {
3856 err = PTR_ERR(after_dashes);
3857 goto coming_cleanup;
3858 } else if (after_dashes) {
3859 pr_warn("%s: parameters '%s' after `--' ignored\n",
3860 mod->name, after_dashes);
3863 /* Link in to sysfs. */
3864 err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp);
3866 goto coming_cleanup;
3868 if (is_livepatch_module(mod)) {
3869 err = copy_module_elf(mod, info);
3874 /* Get rid of temporary copy. */
3878 trace_module_load(mod);
3880 return do_init_module(mod);
3883 mod_sysfs_teardown(mod);
3885 mod->state = MODULE_STATE_GOING;
3886 destroy_params(mod->kp, mod->num_kp);
3887 blocking_notifier_call_chain(&module_notify_list,
3888 MODULE_STATE_GOING, mod);
3889 klp_module_going(mod);
3891 mod->state = MODULE_STATE_GOING;
3892 /* module_bug_cleanup needs module_mutex protection */
3893 mutex_lock(&module_mutex);
3894 module_bug_cleanup(mod);
3895 mutex_unlock(&module_mutex);
3897 /* we can't deallocate the module until we clear memory protection */
3898 module_disable_ro(mod);
3899 module_disable_nx(mod);
3902 ftrace_release_mod(mod);
3903 dynamic_debug_remove(mod, info->debug);
3904 synchronize_sched();
3907 module_arch_cleanup(mod);
3911 module_unload_free(mod);
3913 mutex_lock(&module_mutex);
3914 /* Unlink carefully: kallsyms could be walking list. */
3915 list_del_rcu(&mod->list);
3916 mod_tree_remove(mod);
3917 wake_up_all(&module_wq);
3918 /* Wait for RCU-sched synchronizing before releasing mod->list. */
3919 synchronize_sched();
3920 mutex_unlock(&module_mutex);
3922 /* Free lock-classes; relies on the preceding sync_rcu() */
3923 lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);
3925 module_deallocate(mod, info);
3931 SYSCALL_DEFINE3(init_module, void __user *, umod,
3932 unsigned long, len, const char __user *, uargs)
3935 struct load_info info = { };
3937 err = may_init_module();
3941 pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n",
3944 err = copy_module_from_user(umod, len, &info);
3948 return load_module(&info, uargs, 0);
3951 SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
3953 struct load_info info = { };
3958 err = may_init_module();
3962 pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
3964 if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
3965 |MODULE_INIT_IGNORE_VERMAGIC))
3968 err = kernel_read_file_from_fd(fd, &hdr, &size, INT_MAX,
3975 return load_module(&info, uargs, flags);
3978 static inline int within(unsigned long addr, void *start, unsigned long size)
3980 return ((void *)addr >= start && (void *)addr < start + size);
3983 #ifdef CONFIG_KALLSYMS
3985 * This ignores the intensely annoying "mapping symbols" found
3986 * in ARM ELF files: $a, $t and $d.
3988 static inline int is_arm_mapping_symbol(const char *str)
3990 if (str[0] == '.' && str[1] == 'L')
3992 return str[0] == '$' && strchr("axtd", str[1])
3993 && (str[2] == '\0' || str[2] == '.');
3996 static const char *symname(struct mod_kallsyms *kallsyms, unsigned int symnum)
3998 return kallsyms->strtab + kallsyms->symtab[symnum].st_name;
4001 static const char *get_ksymbol(struct module *mod,
4003 unsigned long *size,
4004 unsigned long *offset)
4006 unsigned int i, best = 0;
4007 unsigned long nextval;
4008 struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
4010 /* At worse, next value is at end of module */
4011 if (within_module_init(addr, mod))
4012 nextval = (unsigned long)mod->init_layout.base+mod->init_layout.text_size;
4014 nextval = (unsigned long)mod->core_layout.base+mod->core_layout.text_size;
4016 /* Scan for closest preceding symbol, and next symbol. (ELF
4017 starts real symbols at 1). */
4018 for (i = 1; i < kallsyms->num_symtab; i++) {
4019 if (kallsyms->symtab[i].st_shndx == SHN_UNDEF)
4022 /* We ignore unnamed symbols: they're uninformative
4023 * and inserted at a whim. */
4024 if (*symname(kallsyms, i) == '\0'
4025 || is_arm_mapping_symbol(symname(kallsyms, i)))
4028 if (kallsyms->symtab[i].st_value <= addr
4029 && kallsyms->symtab[i].st_value > kallsyms->symtab[best].st_value)
4031 if (kallsyms->symtab[i].st_value > addr
4032 && kallsyms->symtab[i].st_value < nextval)
4033 nextval = kallsyms->symtab[i].st_value;
4040 *size = nextval - kallsyms->symtab[best].st_value;
4042 *offset = addr - kallsyms->symtab[best].st_value;
4043 return symname(kallsyms, best);
4046 void * __weak dereference_module_function_descriptor(struct module *mod,
4052 /* For kallsyms to ask for address resolution. NULL means not found. Careful
4053 * not to lock to avoid deadlock on oopses, simply disable preemption. */
4054 const char *module_address_lookup(unsigned long addr,
4055 unsigned long *size,
4056 unsigned long *offset,
4060 const char *ret = NULL;
4064 mod = __module_address(addr);
4067 *modname = mod->name;
4068 ret = get_ksymbol(mod, addr, size, offset);
4070 /* Make a copy in here where it's safe */
4072 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
4080 int lookup_module_symbol_name(unsigned long addr, char *symname)
4085 list_for_each_entry_rcu(mod, &modules, list) {
4086 if (mod->state == MODULE_STATE_UNFORMED)
4088 if (within_module(addr, mod)) {
4091 sym = get_ksymbol(mod, addr, NULL, NULL);
4094 strlcpy(symname, sym, KSYM_NAME_LEN);
4104 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
4105 unsigned long *offset, char *modname, char *name)
4110 list_for_each_entry_rcu(mod, &modules, list) {
4111 if (mod->state == MODULE_STATE_UNFORMED)
4113 if (within_module(addr, mod)) {
4116 sym = get_ksymbol(mod, addr, size, offset);
4120 strlcpy(modname, mod->name, MODULE_NAME_LEN);
4122 strlcpy(name, sym, KSYM_NAME_LEN);
4132 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
4133 char *name, char *module_name, int *exported)
4138 list_for_each_entry_rcu(mod, &modules, list) {
4139 struct mod_kallsyms *kallsyms;
4141 if (mod->state == MODULE_STATE_UNFORMED)
4143 kallsyms = rcu_dereference_sched(mod->kallsyms);
4144 if (symnum < kallsyms->num_symtab) {
4145 *value = kallsyms->symtab[symnum].st_value;
4146 *type = kallsyms->symtab[symnum].st_info;
4147 strlcpy(name, symname(kallsyms, symnum), KSYM_NAME_LEN);
4148 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
4149 *exported = is_exported(name, *value, mod);
4153 symnum -= kallsyms->num_symtab;
4159 static unsigned long mod_find_symname(struct module *mod, const char *name)
4162 struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
4164 for (i = 0; i < kallsyms->num_symtab; i++)
4165 if (strcmp(name, symname(kallsyms, i)) == 0 &&
4166 kallsyms->symtab[i].st_shndx != SHN_UNDEF)
4167 return kallsyms->symtab[i].st_value;
4171 /* Look for this name: can be of form module:name. */
4172 unsigned long module_kallsyms_lookup_name(const char *name)
4176 unsigned long ret = 0;
4178 /* Don't lock: we're in enough trouble already. */
4180 if ((colon = strnchr(name, MODULE_NAME_LEN, ':')) != NULL) {
4181 if ((mod = find_module_all(name, colon - name, false)) != NULL)
4182 ret = mod_find_symname(mod, colon+1);
4184 list_for_each_entry_rcu(mod, &modules, list) {
4185 if (mod->state == MODULE_STATE_UNFORMED)
4187 if ((ret = mod_find_symname(mod, name)) != 0)
4195 int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
4196 struct module *, unsigned long),
4203 module_assert_mutex();
4205 list_for_each_entry(mod, &modules, list) {
4206 /* We hold module_mutex: no need for rcu_dereference_sched */
4207 struct mod_kallsyms *kallsyms = mod->kallsyms;
4209 if (mod->state == MODULE_STATE_UNFORMED)
4211 for (i = 0; i < kallsyms->num_symtab; i++) {
4213 if (kallsyms->symtab[i].st_shndx == SHN_UNDEF)
4216 ret = fn(data, symname(kallsyms, i),
4217 mod, kallsyms->symtab[i].st_value);
4224 #endif /* CONFIG_KALLSYMS */
4226 /* Maximum number of characters written by module_flags() */
4227 #define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
4229 /* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */
4230 static char *module_flags(struct module *mod, char *buf)
4234 BUG_ON(mod->state == MODULE_STATE_UNFORMED);
4236 mod->state == MODULE_STATE_GOING ||
4237 mod->state == MODULE_STATE_COMING) {
4239 bx += module_flags_taint(mod, buf + bx);
4240 /* Show a - for module-is-being-unloaded */
4241 if (mod->state == MODULE_STATE_GOING)
4243 /* Show a + for module-is-being-loaded */
4244 if (mod->state == MODULE_STATE_COMING)
4253 #ifdef CONFIG_PROC_FS
4254 /* Called by the /proc file system to return a list of modules. */
4255 static void *m_start(struct seq_file *m, loff_t *pos)
4257 mutex_lock(&module_mutex);
4258 return seq_list_start(&modules, *pos);
4261 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
4263 return seq_list_next(p, &modules, pos);
4266 static void m_stop(struct seq_file *m, void *p)
4268 mutex_unlock(&module_mutex);
4271 static int m_show(struct seq_file *m, void *p)
4273 struct module *mod = list_entry(p, struct module, list);
4274 char buf[MODULE_FLAGS_BUF_SIZE];
4277 /* We always ignore unformed modules. */
4278 if (mod->state == MODULE_STATE_UNFORMED)
4281 seq_printf(m, "%s %u",
4282 mod->name, mod->init_layout.size + mod->core_layout.size);
4283 print_unload_info(m, mod);
4285 /* Informative for users. */
4286 seq_printf(m, " %s",
4287 mod->state == MODULE_STATE_GOING ? "Unloading" :
4288 mod->state == MODULE_STATE_COMING ? "Loading" :
4290 /* Used by oprofile and other similar tools. */
4291 value = m->private ? NULL : mod->core_layout.base;
4292 seq_printf(m, " 0x%px", value);
4296 seq_printf(m, " %s", module_flags(mod, buf));
4302 /* Format: modulename size refcount deps address
4304 Where refcount is a number or -, and deps is a comma-separated list
4307 static const struct seq_operations modules_op = {
4315 * This also sets the "private" pointer to non-NULL if the
4316 * kernel pointers should be hidden (so you can just test
4317 * "m->private" to see if you should keep the values private).
4319 * We use the same logic as for /proc/kallsyms.
4321 static int modules_open(struct inode *inode, struct file *file)
4323 int err = seq_open(file, &modules_op);
4326 struct seq_file *m = file->private_data;
4327 m->private = kallsyms_show_value(file->f_cred) ? NULL : (void *)8ul;
4333 static const struct file_operations proc_modules_operations = {
4334 .open = modules_open,
4336 .llseek = seq_lseek,
4337 .release = seq_release,
4340 static int __init proc_modules_init(void)
4342 proc_create("modules", 0, NULL, &proc_modules_operations);
4345 module_init(proc_modules_init);
4348 /* Given an address, look for it in the module exception tables. */
4349 const struct exception_table_entry *search_module_extables(unsigned long addr)
4351 const struct exception_table_entry *e = NULL;
4355 mod = __module_address(addr);
4359 if (!mod->num_exentries)
4362 e = search_extable(mod->extable,
4369 * Now, if we found one, we are running inside it now, hence
4370 * we cannot unload the module, hence no refcnt needed.
4376 * is_module_address - is this address inside a module?
4377 * @addr: the address to check.
4379 * See is_module_text_address() if you simply want to see if the address
4380 * is code (not data).
4382 bool is_module_address(unsigned long addr)
4387 ret = __module_address(addr) != NULL;
4394 * __module_address - get the module which contains an address.
4395 * @addr: the address.
4397 * Must be called with preempt disabled or module mutex held so that
4398 * module doesn't get freed during this.
4400 struct module *__module_address(unsigned long addr)
4404 if (addr < module_addr_min || addr > module_addr_max)
4407 module_assert_mutex_or_preempt();
4409 mod = mod_find(addr);
4411 BUG_ON(!within_module(addr, mod));
4412 if (mod->state == MODULE_STATE_UNFORMED)
4419 * is_module_text_address - is this address inside module code?
4420 * @addr: the address to check.
4422 * See is_module_address() if you simply want to see if the address is
4423 * anywhere in a module. See kernel_text_address() for testing if an
4424 * address corresponds to kernel or module code.
4426 bool is_module_text_address(unsigned long addr)
4431 ret = __module_text_address(addr) != NULL;
4438 * __module_text_address - get the module whose code contains an address.
4439 * @addr: the address.
4441 * Must be called with preempt disabled or module mutex held so that
4442 * module doesn't get freed during this.
4444 struct module *__module_text_address(unsigned long addr)
4446 struct module *mod = __module_address(addr);
4448 /* Make sure it's within the text section. */
4449 if (!within(addr, mod->init_layout.base, mod->init_layout.text_size)
4450 && !within(addr, mod->core_layout.base, mod->core_layout.text_size))
4456 /* Don't grab lock, we're oopsing. */
4457 void print_modules(void)
4460 char buf[MODULE_FLAGS_BUF_SIZE];
4462 printk(KERN_DEFAULT "Modules linked in:");
4463 /* Most callers should already have preempt disabled, but make sure */
4465 list_for_each_entry_rcu(mod, &modules, list) {
4466 if (mod->state == MODULE_STATE_UNFORMED)
4468 pr_cont(" %s%s", mod->name, module_flags(mod, buf));
4471 if (last_unloaded_module[0])
4472 pr_cont(" [last unloaded: %s]", last_unloaded_module);
4476 #ifdef CONFIG_MODVERSIONS
4477 /* Generate the signature for all relevant module structures here.
4478 * If these change, we don't want to try to parse the module. */
4479 void module_layout(struct module *mod,
4480 struct modversion_info *ver,
4481 struct kernel_param *kp,
4482 struct kernel_symbol *ks,
4483 struct tracepoint * const *tp)
4486 EXPORT_SYMBOL(module_layout);