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 #ifdef CONFIG_MODULE_SIG
272 static bool sig_enforce = IS_ENABLED(CONFIG_MODULE_SIG_FORCE);
273 module_param(sig_enforce, bool_enable_only, 0644);
275 void set_module_sig_enforced(void)
280 #define sig_enforce false
284 * Export sig_enforce kernel cmdline parameter to allow other subsystems rely
285 * on that instead of directly to CONFIG_MODULE_SIG_FORCE config.
287 bool is_module_sig_enforced(void)
291 EXPORT_SYMBOL(is_module_sig_enforced);
293 /* Block module loading/unloading? */
294 int modules_disabled = 0;
295 core_param(nomodule, modules_disabled, bint, 0);
297 /* Waiting for a module to finish initializing? */
298 static DECLARE_WAIT_QUEUE_HEAD(module_wq);
300 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
302 int register_module_notifier(struct notifier_block *nb)
304 return blocking_notifier_chain_register(&module_notify_list, nb);
306 EXPORT_SYMBOL(register_module_notifier);
308 int unregister_module_notifier(struct notifier_block *nb)
310 return blocking_notifier_chain_unregister(&module_notify_list, nb);
312 EXPORT_SYMBOL(unregister_module_notifier);
315 * We require a truly strong try_module_get(): 0 means success.
316 * Otherwise an error is returned due to ongoing or failed
317 * initialization etc.
319 static inline int strong_try_module_get(struct module *mod)
321 BUG_ON(mod && mod->state == MODULE_STATE_UNFORMED);
322 if (mod && mod->state == MODULE_STATE_COMING)
324 if (try_module_get(mod))
330 static inline void add_taint_module(struct module *mod, unsigned flag,
331 enum lockdep_ok lockdep_ok)
333 add_taint(flag, lockdep_ok);
334 set_bit(flag, &mod->taints);
338 * A thread that wants to hold a reference to a module only while it
339 * is running can call this to safely exit. nfsd and lockd use this.
341 void __noreturn __module_put_and_exit(struct module *mod, long code)
346 EXPORT_SYMBOL(__module_put_and_exit);
348 /* Find a module section: 0 means not found. */
349 static unsigned int find_sec(const struct load_info *info, const char *name)
353 for (i = 1; i < info->hdr->e_shnum; i++) {
354 Elf_Shdr *shdr = &info->sechdrs[i];
355 /* Alloc bit cleared means "ignore it." */
356 if ((shdr->sh_flags & SHF_ALLOC)
357 && strcmp(info->secstrings + shdr->sh_name, name) == 0)
363 /* Find a module section, or NULL. */
364 static void *section_addr(const struct load_info *info, const char *name)
366 /* Section 0 has sh_addr 0. */
367 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
370 /* Find a module section, or NULL. Fill in number of "objects" in section. */
371 static void *section_objs(const struct load_info *info,
376 unsigned int sec = find_sec(info, name);
378 /* Section 0 has sh_addr 0 and sh_size 0. */
379 *num = info->sechdrs[sec].sh_size / object_size;
380 return (void *)info->sechdrs[sec].sh_addr;
383 /* Provided by the linker */
384 extern const struct kernel_symbol __start___ksymtab[];
385 extern const struct kernel_symbol __stop___ksymtab[];
386 extern const struct kernel_symbol __start___ksymtab_gpl[];
387 extern const struct kernel_symbol __stop___ksymtab_gpl[];
388 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
389 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
390 extern const s32 __start___kcrctab[];
391 extern const s32 __start___kcrctab_gpl[];
392 extern const s32 __start___kcrctab_gpl_future[];
393 #ifdef CONFIG_UNUSED_SYMBOLS
394 extern const struct kernel_symbol __start___ksymtab_unused[];
395 extern const struct kernel_symbol __stop___ksymtab_unused[];
396 extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
397 extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
398 extern const s32 __start___kcrctab_unused[];
399 extern const s32 __start___kcrctab_unused_gpl[];
402 #ifndef CONFIG_MODVERSIONS
403 #define symversion(base, idx) NULL
405 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
408 static bool each_symbol_in_section(const struct symsearch *arr,
409 unsigned int arrsize,
410 struct module *owner,
411 bool (*fn)(const struct symsearch *syms,
412 struct module *owner,
418 for (j = 0; j < arrsize; j++) {
419 if (fn(&arr[j], owner, data))
426 /* Returns true as soon as fn returns true, otherwise false. */
427 static bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
428 struct module *owner,
433 static const struct symsearch arr[] = {
434 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
435 NOT_GPL_ONLY, false },
436 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
437 __start___kcrctab_gpl,
439 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
440 __start___kcrctab_gpl_future,
441 WILL_BE_GPL_ONLY, false },
442 #ifdef CONFIG_UNUSED_SYMBOLS
443 { __start___ksymtab_unused, __stop___ksymtab_unused,
444 __start___kcrctab_unused,
445 NOT_GPL_ONLY, true },
446 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
447 __start___kcrctab_unused_gpl,
452 module_assert_mutex_or_preempt();
454 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
457 list_for_each_entry_rcu(mod, &modules, list) {
458 struct symsearch arr[] = {
459 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
460 NOT_GPL_ONLY, false },
461 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
464 { mod->gpl_future_syms,
465 mod->gpl_future_syms + mod->num_gpl_future_syms,
466 mod->gpl_future_crcs,
467 WILL_BE_GPL_ONLY, false },
468 #ifdef CONFIG_UNUSED_SYMBOLS
470 mod->unused_syms + mod->num_unused_syms,
472 NOT_GPL_ONLY, true },
473 { mod->unused_gpl_syms,
474 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
475 mod->unused_gpl_crcs,
480 if (mod->state == MODULE_STATE_UNFORMED)
483 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
489 struct find_symbol_arg {
496 struct module *owner;
498 const struct kernel_symbol *sym;
499 enum mod_license license;
502 static bool check_symbol(const struct symsearch *syms,
503 struct module *owner,
504 unsigned int symnum, void *data)
506 struct find_symbol_arg *fsa = data;
509 if (syms->license == GPL_ONLY)
511 if (syms->license == WILL_BE_GPL_ONLY && fsa->warn) {
512 pr_warn("Symbol %s is being used by a non-GPL module, "
513 "which will not be allowed in the future\n",
518 #ifdef CONFIG_UNUSED_SYMBOLS
519 if (syms->unused && fsa->warn) {
520 pr_warn("Symbol %s is marked as UNUSED, however this module is "
521 "using it.\n", fsa->name);
522 pr_warn("This symbol will go away in the future.\n");
523 pr_warn("Please evaluate if this is the right api to use and "
524 "if it really is, submit a report to the linux kernel "
525 "mailing list together with submitting your code for "
531 fsa->crc = symversion(syms->crcs, symnum);
532 fsa->sym = &syms->start[symnum];
533 fsa->license = syms->license;
537 static unsigned long kernel_symbol_value(const struct kernel_symbol *sym)
539 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
540 return (unsigned long)offset_to_ptr(&sym->value_offset);
546 static const char *kernel_symbol_name(const struct kernel_symbol *sym)
548 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
549 return offset_to_ptr(&sym->name_offset);
555 static int cmp_name(const void *va, const void *vb)
558 const struct kernel_symbol *b;
560 return strcmp(a, kernel_symbol_name(b));
563 static bool find_symbol_in_section(const struct symsearch *syms,
564 struct module *owner,
567 struct find_symbol_arg *fsa = data;
568 struct kernel_symbol *sym;
570 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
571 sizeof(struct kernel_symbol), cmp_name);
573 if (sym != NULL && check_symbol(syms, owner, sym - syms->start, data))
579 /* Find a symbol and return it, along with, (optional) crc and
580 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
581 static const struct kernel_symbol *find_symbol(const char *name,
582 struct module **owner,
584 enum mod_license *license,
588 struct find_symbol_arg fsa;
594 if (each_symbol_section(find_symbol_in_section, &fsa)) {
600 *license = fsa.license;
604 pr_debug("Failed to find symbol %s\n", name);
609 * Search for module by name: must hold module_mutex (or preempt disabled
610 * for read-only access).
612 static struct module *find_module_all(const char *name, size_t len,
617 module_assert_mutex_or_preempt();
619 list_for_each_entry_rcu(mod, &modules, list) {
620 if (!even_unformed && mod->state == MODULE_STATE_UNFORMED)
622 if (strlen(mod->name) == len && !memcmp(mod->name, name, len))
628 struct module *find_module(const char *name)
630 module_assert_mutex();
631 return find_module_all(name, strlen(name), false);
633 EXPORT_SYMBOL_GPL(find_module);
637 static inline void __percpu *mod_percpu(struct module *mod)
642 static int percpu_modalloc(struct module *mod, struct load_info *info)
644 Elf_Shdr *pcpusec = &info->sechdrs[info->index.pcpu];
645 unsigned long align = pcpusec->sh_addralign;
647 if (!pcpusec->sh_size)
650 if (align > PAGE_SIZE) {
651 pr_warn("%s: per-cpu alignment %li > %li\n",
652 mod->name, align, PAGE_SIZE);
656 mod->percpu = __alloc_reserved_percpu(pcpusec->sh_size, align);
658 pr_warn("%s: Could not allocate %lu bytes percpu data\n",
659 mod->name, (unsigned long)pcpusec->sh_size);
662 mod->percpu_size = pcpusec->sh_size;
666 static void percpu_modfree(struct module *mod)
668 free_percpu(mod->percpu);
671 static unsigned int find_pcpusec(struct load_info *info)
673 return find_sec(info, ".data..percpu");
676 static void percpu_modcopy(struct module *mod,
677 const void *from, unsigned long size)
681 for_each_possible_cpu(cpu)
682 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
685 bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
692 list_for_each_entry_rcu(mod, &modules, list) {
693 if (mod->state == MODULE_STATE_UNFORMED)
695 if (!mod->percpu_size)
697 for_each_possible_cpu(cpu) {
698 void *start = per_cpu_ptr(mod->percpu, cpu);
699 void *va = (void *)addr;
701 if (va >= start && va < start + mod->percpu_size) {
703 *can_addr = (unsigned long) (va - start);
704 *can_addr += (unsigned long)
705 per_cpu_ptr(mod->percpu,
719 * is_module_percpu_address - test whether address is from module static percpu
720 * @addr: address to test
722 * Test whether @addr belongs to module static percpu area.
725 * %true if @addr is from module static percpu area
727 bool is_module_percpu_address(unsigned long addr)
729 return __is_module_percpu_address(addr, NULL);
732 #else /* ... !CONFIG_SMP */
734 static inline void __percpu *mod_percpu(struct module *mod)
738 static int percpu_modalloc(struct module *mod, struct load_info *info)
740 /* UP modules shouldn't have this section: ENOMEM isn't quite right */
741 if (info->sechdrs[info->index.pcpu].sh_size != 0)
745 static inline void percpu_modfree(struct module *mod)
748 static unsigned int find_pcpusec(struct load_info *info)
752 static inline void percpu_modcopy(struct module *mod,
753 const void *from, unsigned long size)
755 /* pcpusec should be 0, and size of that section should be 0. */
758 bool is_module_percpu_address(unsigned long addr)
763 bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
768 #endif /* CONFIG_SMP */
770 #define MODINFO_ATTR(field) \
771 static void setup_modinfo_##field(struct module *mod, const char *s) \
773 mod->field = kstrdup(s, GFP_KERNEL); \
775 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
776 struct module_kobject *mk, char *buffer) \
778 return scnprintf(buffer, PAGE_SIZE, "%s\n", mk->mod->field); \
780 static int modinfo_##field##_exists(struct module *mod) \
782 return mod->field != NULL; \
784 static void free_modinfo_##field(struct module *mod) \
789 static struct module_attribute modinfo_##field = { \
790 .attr = { .name = __stringify(field), .mode = 0444 }, \
791 .show = show_modinfo_##field, \
792 .setup = setup_modinfo_##field, \
793 .test = modinfo_##field##_exists, \
794 .free = free_modinfo_##field, \
797 MODINFO_ATTR(version);
798 MODINFO_ATTR(srcversion);
800 static char last_unloaded_module[MODULE_NAME_LEN+1];
802 #ifdef CONFIG_MODULE_UNLOAD
804 EXPORT_TRACEPOINT_SYMBOL(module_get);
806 /* MODULE_REF_BASE is the base reference count by kmodule loader. */
807 #define MODULE_REF_BASE 1
809 /* Init the unload section of the module. */
810 static int module_unload_init(struct module *mod)
813 * Initialize reference counter to MODULE_REF_BASE.
814 * refcnt == 0 means module is going.
816 atomic_set(&mod->refcnt, MODULE_REF_BASE);
818 INIT_LIST_HEAD(&mod->source_list);
819 INIT_LIST_HEAD(&mod->target_list);
821 /* Hold reference count during initialization. */
822 atomic_inc(&mod->refcnt);
827 /* Does a already use b? */
828 static int already_uses(struct module *a, struct module *b)
830 struct module_use *use;
832 list_for_each_entry(use, &b->source_list, source_list) {
833 if (use->source == a) {
834 pr_debug("%s uses %s!\n", a->name, b->name);
838 pr_debug("%s does not use %s!\n", a->name, b->name);
844 * - we add 'a' as a "source", 'b' as a "target" of module use
845 * - the module_use is added to the list of 'b' sources (so
846 * 'b' can walk the list to see who sourced them), and of 'a'
847 * targets (so 'a' can see what modules it targets).
849 static int add_module_usage(struct module *a, struct module *b)
851 struct module_use *use;
853 pr_debug("Allocating new usage for %s.\n", a->name);
854 use = kmalloc(sizeof(*use), GFP_ATOMIC);
860 list_add(&use->source_list, &b->source_list);
861 list_add(&use->target_list, &a->target_list);
865 /* Module a uses b: caller needs module_mutex() */
866 static int ref_module(struct module *a, struct module *b)
870 if (b == NULL || already_uses(a, b))
873 /* If module isn't available, we fail. */
874 err = strong_try_module_get(b);
878 err = add_module_usage(a, b);
886 /* Clear the unload stuff of the module. */
887 static void module_unload_free(struct module *mod)
889 struct module_use *use, *tmp;
891 mutex_lock(&module_mutex);
892 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
893 struct module *i = use->target;
894 pr_debug("%s unusing %s\n", mod->name, i->name);
896 list_del(&use->source_list);
897 list_del(&use->target_list);
900 mutex_unlock(&module_mutex);
903 #ifdef CONFIG_MODULE_FORCE_UNLOAD
904 static inline int try_force_unload(unsigned int flags)
906 int ret = (flags & O_TRUNC);
908 add_taint(TAINT_FORCED_RMMOD, LOCKDEP_NOW_UNRELIABLE);
912 static inline int try_force_unload(unsigned int flags)
916 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
918 /* Try to release refcount of module, 0 means success. */
919 static int try_release_module_ref(struct module *mod)
923 /* Try to decrement refcnt which we set at loading */
924 ret = atomic_sub_return(MODULE_REF_BASE, &mod->refcnt);
927 /* Someone can put this right now, recover with checking */
928 ret = atomic_add_unless(&mod->refcnt, MODULE_REF_BASE, 0);
933 static int try_stop_module(struct module *mod, int flags, int *forced)
935 /* If it's not unused, quit unless we're forcing. */
936 if (try_release_module_ref(mod) != 0) {
937 *forced = try_force_unload(flags);
942 /* Mark it as dying. */
943 mod->state = MODULE_STATE_GOING;
949 * module_refcount - return the refcount or -1 if unloading
951 * @mod: the module we're checking
954 * -1 if the module is in the process of unloading
955 * otherwise the number of references in the kernel to the module
957 int module_refcount(struct module *mod)
959 return atomic_read(&mod->refcnt) - MODULE_REF_BASE;
961 EXPORT_SYMBOL(module_refcount);
963 /* This exists whether we can unload or not */
964 static void free_module(struct module *mod);
966 SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
970 char name[MODULE_NAME_LEN];
973 if (!capable(CAP_SYS_MODULE) || modules_disabled)
976 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
978 name[MODULE_NAME_LEN-1] = '\0';
980 audit_log_kern_module(name);
982 if (mutex_lock_interruptible(&module_mutex) != 0)
985 mod = find_module(name);
991 if (!list_empty(&mod->source_list)) {
992 /* Other modules depend on us: get rid of them first. */
997 /* Doing init or already dying? */
998 if (mod->state != MODULE_STATE_LIVE) {
999 /* FIXME: if (force), slam module count damn the torpedoes */
1000 pr_debug("%s already dying\n", mod->name);
1005 /* If it has an init func, it must have an exit func to unload */
1006 if (mod->init && !mod->exit) {
1007 forced = try_force_unload(flags);
1009 /* This module can't be removed */
1015 /* Stop the machine so refcounts can't move and disable module. */
1016 ret = try_stop_module(mod, flags, &forced);
1020 mutex_unlock(&module_mutex);
1021 /* Final destruction now no one is using it. */
1022 if (mod->exit != NULL)
1024 blocking_notifier_call_chain(&module_notify_list,
1025 MODULE_STATE_GOING, mod);
1026 klp_module_going(mod);
1027 ftrace_release_mod(mod);
1029 async_synchronize_full();
1031 /* Store the name of the last unloaded module for diagnostic purposes */
1032 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
1035 /* someone could wait for the module in add_unformed_module() */
1036 wake_up_all(&module_wq);
1039 mutex_unlock(&module_mutex);
1043 static inline void print_unload_info(struct seq_file *m, struct module *mod)
1045 struct module_use *use;
1046 int printed_something = 0;
1048 seq_printf(m, " %i ", module_refcount(mod));
1051 * Always include a trailing , so userspace can differentiate
1052 * between this and the old multi-field proc format.
1054 list_for_each_entry(use, &mod->source_list, source_list) {
1055 printed_something = 1;
1056 seq_printf(m, "%s,", use->source->name);
1059 if (mod->init != NULL && mod->exit == NULL) {
1060 printed_something = 1;
1061 seq_puts(m, "[permanent],");
1064 if (!printed_something)
1068 void __symbol_put(const char *symbol)
1070 struct module *owner;
1073 if (!find_symbol(symbol, &owner, NULL, NULL, true, false))
1078 EXPORT_SYMBOL(__symbol_put);
1080 /* Note this assumes addr is a function, which it currently always is. */
1081 void symbol_put_addr(void *addr)
1083 struct module *modaddr;
1084 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
1086 if (core_kernel_text(a))
1090 * Even though we hold a reference on the module; we still need to
1091 * disable preemption in order to safely traverse the data structure.
1094 modaddr = __module_text_address(a);
1096 module_put(modaddr);
1099 EXPORT_SYMBOL_GPL(symbol_put_addr);
1101 static ssize_t show_refcnt(struct module_attribute *mattr,
1102 struct module_kobject *mk, char *buffer)
1104 return sprintf(buffer, "%i\n", module_refcount(mk->mod));
1107 static struct module_attribute modinfo_refcnt =
1108 __ATTR(refcnt, 0444, show_refcnt, NULL);
1110 void __module_get(struct module *module)
1114 atomic_inc(&module->refcnt);
1115 trace_module_get(module, _RET_IP_);
1119 EXPORT_SYMBOL(__module_get);
1121 bool try_module_get(struct module *module)
1127 /* Note: here, we can fail to get a reference */
1128 if (likely(module_is_live(module) &&
1129 atomic_inc_not_zero(&module->refcnt) != 0))
1130 trace_module_get(module, _RET_IP_);
1138 EXPORT_SYMBOL(try_module_get);
1140 void module_put(struct module *module)
1146 ret = atomic_dec_if_positive(&module->refcnt);
1147 WARN_ON(ret < 0); /* Failed to put refcount */
1148 trace_module_put(module, _RET_IP_);
1152 EXPORT_SYMBOL(module_put);
1154 #else /* !CONFIG_MODULE_UNLOAD */
1155 static inline void print_unload_info(struct seq_file *m, struct module *mod)
1157 /* We don't know the usage count, or what modules are using. */
1158 seq_puts(m, " - -");
1161 static inline void module_unload_free(struct module *mod)
1165 static int ref_module(struct module *a, struct module *b)
1167 return strong_try_module_get(b);
1170 static inline int module_unload_init(struct module *mod)
1174 #endif /* CONFIG_MODULE_UNLOAD */
1176 static size_t module_flags_taint(struct module *mod, char *buf)
1181 for (i = 0; i < TAINT_FLAGS_COUNT; i++) {
1182 if (taint_flags[i].module && test_bit(i, &mod->taints))
1183 buf[l++] = taint_flags[i].c_true;
1189 static ssize_t show_initstate(struct module_attribute *mattr,
1190 struct module_kobject *mk, char *buffer)
1192 const char *state = "unknown";
1194 switch (mk->mod->state) {
1195 case MODULE_STATE_LIVE:
1198 case MODULE_STATE_COMING:
1201 case MODULE_STATE_GOING:
1207 return sprintf(buffer, "%s\n", state);
1210 static struct module_attribute modinfo_initstate =
1211 __ATTR(initstate, 0444, show_initstate, NULL);
1213 static ssize_t store_uevent(struct module_attribute *mattr,
1214 struct module_kobject *mk,
1215 const char *buffer, size_t count)
1219 rc = kobject_synth_uevent(&mk->kobj, buffer, count);
1220 return rc ? rc : count;
1223 struct module_attribute module_uevent =
1224 __ATTR(uevent, 0200, NULL, store_uevent);
1226 static ssize_t show_coresize(struct module_attribute *mattr,
1227 struct module_kobject *mk, char *buffer)
1229 return sprintf(buffer, "%u\n", mk->mod->core_layout.size);
1232 static struct module_attribute modinfo_coresize =
1233 __ATTR(coresize, 0444, show_coresize, NULL);
1235 static ssize_t show_initsize(struct module_attribute *mattr,
1236 struct module_kobject *mk, char *buffer)
1238 return sprintf(buffer, "%u\n", mk->mod->init_layout.size);
1241 static struct module_attribute modinfo_initsize =
1242 __ATTR(initsize, 0444, show_initsize, NULL);
1244 static ssize_t show_taint(struct module_attribute *mattr,
1245 struct module_kobject *mk, char *buffer)
1249 l = module_flags_taint(mk->mod, buffer);
1254 static struct module_attribute modinfo_taint =
1255 __ATTR(taint, 0444, show_taint, NULL);
1257 static struct module_attribute *modinfo_attrs[] = {
1260 &modinfo_srcversion,
1265 #ifdef CONFIG_MODULE_UNLOAD
1271 static const char vermagic[] = VERMAGIC_STRING;
1273 static int try_to_force_load(struct module *mod, const char *reason)
1275 #ifdef CONFIG_MODULE_FORCE_LOAD
1276 if (!test_taint(TAINT_FORCED_MODULE))
1277 pr_warn("%s: %s: kernel tainted.\n", mod->name, reason);
1278 add_taint_module(mod, TAINT_FORCED_MODULE, LOCKDEP_NOW_UNRELIABLE);
1285 #ifdef CONFIG_MODVERSIONS
1287 static u32 resolve_rel_crc(const s32 *crc)
1289 return *(u32 *)((void *)crc + *crc);
1292 static int check_version(const struct load_info *info,
1293 const char *symname,
1297 Elf_Shdr *sechdrs = info->sechdrs;
1298 unsigned int versindex = info->index.vers;
1299 unsigned int i, num_versions;
1300 struct modversion_info *versions;
1302 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1306 /* No versions at all? modprobe --force does this. */
1308 return try_to_force_load(mod, symname) == 0;
1310 versions = (void *) sechdrs[versindex].sh_addr;
1311 num_versions = sechdrs[versindex].sh_size
1312 / sizeof(struct modversion_info);
1314 for (i = 0; i < num_versions; i++) {
1317 if (strcmp(versions[i].name, symname) != 0)
1320 if (IS_ENABLED(CONFIG_MODULE_REL_CRCS))
1321 crcval = resolve_rel_crc(crc);
1324 if (versions[i].crc == crcval)
1326 pr_debug("Found checksum %X vs module %lX\n",
1327 crcval, versions[i].crc);
1331 /* Broken toolchain. Warn once, then let it go.. */
1332 pr_warn_once("%s: no symbol version for %s\n", info->name, symname);
1336 pr_warn("%s: disagrees about version of symbol %s\n",
1337 info->name, symname);
1341 static inline int check_modstruct_version(const struct load_info *info,
1347 * Since this should be found in kernel (which can't be removed), no
1348 * locking is necessary -- use preempt_disable() to placate lockdep.
1351 if (!find_symbol("module_layout", NULL, &crc, NULL, true, false)) {
1356 return check_version(info, "module_layout", mod, crc);
1359 /* First part is kernel version, which we ignore if module has crcs. */
1360 static inline int same_magic(const char *amagic, const char *bmagic,
1364 amagic += strcspn(amagic, " ");
1365 bmagic += strcspn(bmagic, " ");
1367 return strcmp(amagic, bmagic) == 0;
1370 static inline int check_version(const struct load_info *info,
1371 const char *symname,
1378 static inline int check_modstruct_version(const struct load_info *info,
1384 static inline int same_magic(const char *amagic, const char *bmagic,
1387 return strcmp(amagic, bmagic) == 0;
1389 #endif /* CONFIG_MODVERSIONS */
1391 static bool inherit_taint(struct module *mod, struct module *owner)
1393 if (!owner || !test_bit(TAINT_PROPRIETARY_MODULE, &owner->taints))
1396 if (mod->using_gplonly_symbols) {
1397 pr_err("%s: module using GPL-only symbols uses symbols from proprietary module %s.\n",
1398 mod->name, owner->name);
1402 if (!test_bit(TAINT_PROPRIETARY_MODULE, &mod->taints)) {
1403 pr_warn("%s: module uses symbols from proprietary module %s, inheriting taint.\n",
1404 mod->name, owner->name);
1405 set_bit(TAINT_PROPRIETARY_MODULE, &mod->taints);
1410 /* Resolve a symbol for this module. I.e. if we find one, record usage. */
1411 static const struct kernel_symbol *resolve_symbol(struct module *mod,
1412 const struct load_info *info,
1416 struct module *owner;
1417 const struct kernel_symbol *sym;
1419 enum mod_license license;
1423 * The module_mutex should not be a heavily contended lock;
1424 * if we get the occasional sleep here, we'll go an extra iteration
1425 * in the wait_event_interruptible(), which is harmless.
1427 sched_annotate_sleep();
1428 mutex_lock(&module_mutex);
1429 sym = find_symbol(name, &owner, &crc, &license,
1430 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1434 if (license == GPL_ONLY)
1435 mod->using_gplonly_symbols = true;
1437 if (!inherit_taint(mod, owner)) {
1442 if (!check_version(info, name, mod, crc)) {
1443 sym = ERR_PTR(-EINVAL);
1447 err = ref_module(mod, owner);
1454 /* We must make copy under the lock if we failed to get ref. */
1455 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1457 mutex_unlock(&module_mutex);
1461 static const struct kernel_symbol *
1462 resolve_symbol_wait(struct module *mod,
1463 const struct load_info *info,
1466 const struct kernel_symbol *ksym;
1467 char owner[MODULE_NAME_LEN];
1469 if (wait_event_interruptible_timeout(module_wq,
1470 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1471 || PTR_ERR(ksym) != -EBUSY,
1473 pr_warn("%s: gave up waiting for init of module %s.\n",
1480 * /sys/module/foo/sections stuff
1481 * J. Corbet <corbet@lwn.net>
1485 #ifdef CONFIG_KALLSYMS
1486 static inline bool sect_empty(const Elf_Shdr *sect)
1488 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1491 struct module_sect_attr {
1492 struct bin_attribute battr;
1493 unsigned long address;
1496 struct module_sect_attrs {
1497 struct attribute_group grp;
1498 unsigned int nsections;
1499 struct module_sect_attr attrs[0];
1502 #define MODULE_SECT_READ_SIZE (3 /* "0x", "\n" */ + (BITS_PER_LONG / 4))
1503 static ssize_t module_sect_read(struct file *file, struct kobject *kobj,
1504 struct bin_attribute *battr,
1505 char *buf, loff_t pos, size_t count)
1507 struct module_sect_attr *sattr =
1508 container_of(battr, struct module_sect_attr, battr);
1509 char bounce[MODULE_SECT_READ_SIZE + 1];
1516 * Since we're a binary read handler, we must account for the
1517 * trailing NUL byte that sprintf will write: if "buf" is
1518 * too small to hold the NUL, or the NUL is exactly the last
1519 * byte, the read will look like it got truncated by one byte.
1520 * Since there is no way to ask sprintf nicely to not write
1521 * the NUL, we have to use a bounce buffer.
1523 wrote = scnprintf(bounce, sizeof(bounce), "0x%px\n",
1524 kallsyms_show_value(file->f_cred)
1525 ? (void *)sattr->address : NULL);
1526 count = min(count, wrote);
1527 memcpy(buf, bounce, count);
1532 static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1534 unsigned int section;
1536 for (section = 0; section < sect_attrs->nsections; section++)
1537 kfree(sect_attrs->attrs[section].battr.attr.name);
1541 static void add_sect_attrs(struct module *mod, const struct load_info *info)
1543 unsigned int nloaded = 0, i, size[2];
1544 struct module_sect_attrs *sect_attrs;
1545 struct module_sect_attr *sattr;
1546 struct bin_attribute **gattr;
1548 /* Count loaded sections and allocate structures */
1549 for (i = 0; i < info->hdr->e_shnum; i++)
1550 if (!sect_empty(&info->sechdrs[i]))
1552 size[0] = ALIGN(struct_size(sect_attrs, attrs, nloaded),
1553 sizeof(sect_attrs->grp.bin_attrs[0]));
1554 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.bin_attrs[0]);
1555 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1556 if (sect_attrs == NULL)
1559 /* Setup section attributes. */
1560 sect_attrs->grp.name = "sections";
1561 sect_attrs->grp.bin_attrs = (void *)sect_attrs + size[0];
1563 sect_attrs->nsections = 0;
1564 sattr = §_attrs->attrs[0];
1565 gattr = §_attrs->grp.bin_attrs[0];
1566 for (i = 0; i < info->hdr->e_shnum; i++) {
1567 Elf_Shdr *sec = &info->sechdrs[i];
1568 if (sect_empty(sec))
1570 sysfs_bin_attr_init(&sattr->battr);
1571 sattr->address = sec->sh_addr;
1572 sattr->battr.attr.name =
1573 kstrdup(info->secstrings + sec->sh_name, GFP_KERNEL);
1574 if (sattr->battr.attr.name == NULL)
1576 sect_attrs->nsections++;
1577 sattr->battr.read = module_sect_read;
1578 sattr->battr.size = MODULE_SECT_READ_SIZE;
1579 sattr->battr.attr.mode = 0400;
1580 *(gattr++) = &(sattr++)->battr;
1584 if (sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp))
1587 mod->sect_attrs = sect_attrs;
1590 free_sect_attrs(sect_attrs);
1593 static void remove_sect_attrs(struct module *mod)
1595 if (mod->sect_attrs) {
1596 sysfs_remove_group(&mod->mkobj.kobj,
1597 &mod->sect_attrs->grp);
1598 /* We are positive that no one is using any sect attrs
1599 * at this point. Deallocate immediately. */
1600 free_sect_attrs(mod->sect_attrs);
1601 mod->sect_attrs = NULL;
1606 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1609 struct module_notes_attrs {
1610 struct kobject *dir;
1612 struct bin_attribute attrs[0];
1615 static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
1616 struct bin_attribute *bin_attr,
1617 char *buf, loff_t pos, size_t count)
1620 * The caller checked the pos and count against our size.
1622 memcpy(buf, bin_attr->private + pos, count);
1626 static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1629 if (notes_attrs->dir) {
1631 sysfs_remove_bin_file(notes_attrs->dir,
1632 ¬es_attrs->attrs[i]);
1633 kobject_put(notes_attrs->dir);
1638 static void add_notes_attrs(struct module *mod, const struct load_info *info)
1640 unsigned int notes, loaded, i;
1641 struct module_notes_attrs *notes_attrs;
1642 struct bin_attribute *nattr;
1644 /* failed to create section attributes, so can't create notes */
1645 if (!mod->sect_attrs)
1648 /* Count notes sections and allocate structures. */
1650 for (i = 0; i < info->hdr->e_shnum; i++)
1651 if (!sect_empty(&info->sechdrs[i]) &&
1652 (info->sechdrs[i].sh_type == SHT_NOTE))
1658 notes_attrs = kzalloc(struct_size(notes_attrs, attrs, notes),
1660 if (notes_attrs == NULL)
1663 notes_attrs->notes = notes;
1664 nattr = ¬es_attrs->attrs[0];
1665 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1666 if (sect_empty(&info->sechdrs[i]))
1668 if (info->sechdrs[i].sh_type == SHT_NOTE) {
1669 sysfs_bin_attr_init(nattr);
1670 nattr->attr.name = mod->sect_attrs->attrs[loaded].battr.attr.name;
1671 nattr->attr.mode = S_IRUGO;
1672 nattr->size = info->sechdrs[i].sh_size;
1673 nattr->private = (void *) info->sechdrs[i].sh_addr;
1674 nattr->read = module_notes_read;
1680 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
1681 if (!notes_attrs->dir)
1684 for (i = 0; i < notes; ++i)
1685 if (sysfs_create_bin_file(notes_attrs->dir,
1686 ¬es_attrs->attrs[i]))
1689 mod->notes_attrs = notes_attrs;
1693 free_notes_attrs(notes_attrs, i);
1696 static void remove_notes_attrs(struct module *mod)
1698 if (mod->notes_attrs)
1699 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1704 static inline void add_sect_attrs(struct module *mod,
1705 const struct load_info *info)
1709 static inline void remove_sect_attrs(struct module *mod)
1713 static inline void add_notes_attrs(struct module *mod,
1714 const struct load_info *info)
1718 static inline void remove_notes_attrs(struct module *mod)
1721 #endif /* CONFIG_KALLSYMS */
1723 static void del_usage_links(struct module *mod)
1725 #ifdef CONFIG_MODULE_UNLOAD
1726 struct module_use *use;
1728 mutex_lock(&module_mutex);
1729 list_for_each_entry(use, &mod->target_list, target_list)
1730 sysfs_remove_link(use->target->holders_dir, mod->name);
1731 mutex_unlock(&module_mutex);
1735 static int add_usage_links(struct module *mod)
1738 #ifdef CONFIG_MODULE_UNLOAD
1739 struct module_use *use;
1741 mutex_lock(&module_mutex);
1742 list_for_each_entry(use, &mod->target_list, target_list) {
1743 ret = sysfs_create_link(use->target->holders_dir,
1744 &mod->mkobj.kobj, mod->name);
1748 mutex_unlock(&module_mutex);
1750 del_usage_links(mod);
1755 static void module_remove_modinfo_attrs(struct module *mod, int end);
1757 static int module_add_modinfo_attrs(struct module *mod)
1759 struct module_attribute *attr;
1760 struct module_attribute *temp_attr;
1764 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1765 (ARRAY_SIZE(modinfo_attrs) + 1)),
1767 if (!mod->modinfo_attrs)
1770 temp_attr = mod->modinfo_attrs;
1771 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1772 if (!attr->test || attr->test(mod)) {
1773 memcpy(temp_attr, attr, sizeof(*temp_attr));
1774 sysfs_attr_init(&temp_attr->attr);
1775 error = sysfs_create_file(&mod->mkobj.kobj,
1787 module_remove_modinfo_attrs(mod, --i);
1789 kfree(mod->modinfo_attrs);
1793 static void module_remove_modinfo_attrs(struct module *mod, int end)
1795 struct module_attribute *attr;
1798 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1799 if (end >= 0 && i > end)
1801 /* pick a field to test for end of list */
1802 if (!attr->attr.name)
1804 sysfs_remove_file(&mod->mkobj.kobj, &attr->attr);
1808 kfree(mod->modinfo_attrs);
1811 static void mod_kobject_put(struct module *mod)
1813 DECLARE_COMPLETION_ONSTACK(c);
1814 mod->mkobj.kobj_completion = &c;
1815 kobject_put(&mod->mkobj.kobj);
1816 wait_for_completion(&c);
1819 static int mod_sysfs_init(struct module *mod)
1822 struct kobject *kobj;
1824 if (!module_sysfs_initialized) {
1825 pr_err("%s: module sysfs not initialized\n", mod->name);
1830 kobj = kset_find_obj(module_kset, mod->name);
1832 pr_err("%s: module is already loaded\n", mod->name);
1838 mod->mkobj.mod = mod;
1840 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1841 mod->mkobj.kobj.kset = module_kset;
1842 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1845 mod_kobject_put(mod);
1851 static int mod_sysfs_setup(struct module *mod,
1852 const struct load_info *info,
1853 struct kernel_param *kparam,
1854 unsigned int num_params)
1858 err = mod_sysfs_init(mod);
1862 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1863 if (!mod->holders_dir) {
1868 err = module_param_sysfs_setup(mod, kparam, num_params);
1870 goto out_unreg_holders;
1872 err = module_add_modinfo_attrs(mod);
1874 goto out_unreg_param;
1876 err = add_usage_links(mod);
1878 goto out_unreg_modinfo_attrs;
1880 add_sect_attrs(mod, info);
1881 add_notes_attrs(mod, info);
1885 out_unreg_modinfo_attrs:
1886 module_remove_modinfo_attrs(mod, -1);
1888 module_param_sysfs_remove(mod);
1890 kobject_put(mod->holders_dir);
1892 mod_kobject_put(mod);
1897 static void mod_sysfs_fini(struct module *mod)
1899 remove_notes_attrs(mod);
1900 remove_sect_attrs(mod);
1901 mod_kobject_put(mod);
1904 static void init_param_lock(struct module *mod)
1906 mutex_init(&mod->param_lock);
1908 #else /* !CONFIG_SYSFS */
1910 static int mod_sysfs_setup(struct module *mod,
1911 const struct load_info *info,
1912 struct kernel_param *kparam,
1913 unsigned int num_params)
1918 static void mod_sysfs_fini(struct module *mod)
1922 static void module_remove_modinfo_attrs(struct module *mod, int end)
1926 static void del_usage_links(struct module *mod)
1930 static void init_param_lock(struct module *mod)
1933 #endif /* CONFIG_SYSFS */
1935 static void mod_sysfs_teardown(struct module *mod)
1937 del_usage_links(mod);
1938 module_remove_modinfo_attrs(mod, -1);
1939 module_param_sysfs_remove(mod);
1940 kobject_put(mod->mkobj.drivers_dir);
1941 kobject_put(mod->holders_dir);
1942 mod_sysfs_fini(mod);
1945 #ifdef CONFIG_ARCH_HAS_STRICT_MODULE_RWX
1947 * LKM RO/NX protection: protect module's text/ro-data
1948 * from modification and any data from execution.
1950 * General layout of module is:
1951 * [text] [read-only-data] [ro-after-init] [writable data]
1952 * text_size -----^ ^ ^ ^
1953 * ro_size ------------------------| | |
1954 * ro_after_init_size -----------------------------| |
1955 * size -----------------------------------------------------------|
1957 * These values are always page-aligned (as is base)
1959 static void frob_text(const struct module_layout *layout,
1960 int (*set_memory)(unsigned long start, int num_pages))
1962 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
1963 BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
1964 set_memory((unsigned long)layout->base,
1965 layout->text_size >> PAGE_SHIFT);
1968 #ifdef CONFIG_STRICT_MODULE_RWX
1969 static void frob_rodata(const struct module_layout *layout,
1970 int (*set_memory)(unsigned long start, int num_pages))
1972 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
1973 BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
1974 BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
1975 set_memory((unsigned long)layout->base + layout->text_size,
1976 (layout->ro_size - layout->text_size) >> PAGE_SHIFT);
1979 static void frob_ro_after_init(const struct module_layout *layout,
1980 int (*set_memory)(unsigned long start, int num_pages))
1982 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
1983 BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
1984 BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1));
1985 set_memory((unsigned long)layout->base + layout->ro_size,
1986 (layout->ro_after_init_size - layout->ro_size) >> PAGE_SHIFT);
1989 static void frob_writable_data(const struct module_layout *layout,
1990 int (*set_memory)(unsigned long start, int num_pages))
1992 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
1993 BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1));
1994 BUG_ON((unsigned long)layout->size & (PAGE_SIZE-1));
1995 set_memory((unsigned long)layout->base + layout->ro_after_init_size,
1996 (layout->size - layout->ro_after_init_size) >> PAGE_SHIFT);
1999 /* livepatching wants to disable read-only so it can frob module. */
2000 void module_disable_ro(const struct module *mod)
2002 if (!rodata_enabled)
2005 frob_text(&mod->core_layout, set_memory_rw);
2006 frob_rodata(&mod->core_layout, set_memory_rw);
2007 frob_ro_after_init(&mod->core_layout, set_memory_rw);
2008 frob_text(&mod->init_layout, set_memory_rw);
2009 frob_rodata(&mod->init_layout, set_memory_rw);
2012 void module_enable_ro(const struct module *mod, bool after_init)
2014 if (!rodata_enabled)
2017 frob_text(&mod->core_layout, set_memory_ro);
2019 frob_rodata(&mod->core_layout, set_memory_ro);
2020 frob_text(&mod->init_layout, set_memory_ro);
2021 frob_rodata(&mod->init_layout, set_memory_ro);
2024 frob_ro_after_init(&mod->core_layout, set_memory_ro);
2027 static void module_enable_nx(const struct module *mod)
2029 frob_rodata(&mod->core_layout, set_memory_nx);
2030 frob_ro_after_init(&mod->core_layout, set_memory_nx);
2031 frob_writable_data(&mod->core_layout, set_memory_nx);
2032 frob_rodata(&mod->init_layout, set_memory_nx);
2033 frob_writable_data(&mod->init_layout, set_memory_nx);
2036 static void module_disable_nx(const struct module *mod)
2038 frob_rodata(&mod->core_layout, set_memory_x);
2039 frob_ro_after_init(&mod->core_layout, set_memory_x);
2040 frob_writable_data(&mod->core_layout, set_memory_x);
2041 frob_rodata(&mod->init_layout, set_memory_x);
2042 frob_writable_data(&mod->init_layout, set_memory_x);
2045 /* Iterate through all modules and set each module's text as RW */
2046 void set_all_modules_text_rw(void)
2050 if (!rodata_enabled)
2053 mutex_lock(&module_mutex);
2054 list_for_each_entry_rcu(mod, &modules, list) {
2055 if (mod->state == MODULE_STATE_UNFORMED)
2058 frob_text(&mod->core_layout, set_memory_rw);
2059 frob_text(&mod->init_layout, set_memory_rw);
2061 mutex_unlock(&module_mutex);
2064 /* Iterate through all modules and set each module's text as RO */
2065 void set_all_modules_text_ro(void)
2069 if (!rodata_enabled)
2072 mutex_lock(&module_mutex);
2073 list_for_each_entry_rcu(mod, &modules, list) {
2075 * Ignore going modules since it's possible that ro
2076 * protection has already been disabled, otherwise we'll
2077 * run into protection faults at module deallocation.
2079 if (mod->state == MODULE_STATE_UNFORMED ||
2080 mod->state == MODULE_STATE_GOING)
2083 frob_text(&mod->core_layout, set_memory_ro);
2084 frob_text(&mod->init_layout, set_memory_ro);
2086 mutex_unlock(&module_mutex);
2089 static void disable_ro_nx(const struct module_layout *layout)
2091 if (rodata_enabled) {
2092 frob_text(layout, set_memory_rw);
2093 frob_rodata(layout, set_memory_rw);
2094 frob_ro_after_init(layout, set_memory_rw);
2096 frob_rodata(layout, set_memory_x);
2097 frob_ro_after_init(layout, set_memory_x);
2098 frob_writable_data(layout, set_memory_x);
2101 #else /* !CONFIG_STRICT_MODULE_RWX */
2102 static void disable_ro_nx(const struct module_layout *layout) { }
2103 static void module_enable_nx(const struct module *mod) { }
2104 static void module_disable_nx(const struct module *mod) { }
2105 #endif /* CONFIG_STRICT_MODULE_RWX */
2107 static void module_enable_x(const struct module *mod)
2109 frob_text(&mod->core_layout, set_memory_x);
2110 frob_text(&mod->init_layout, set_memory_x);
2112 #else /* !CONFIG_ARCH_HAS_STRICT_MODULE_RWX */
2113 static void disable_ro_nx(const struct module_layout *layout) { }
2114 static void module_enable_nx(const struct module *mod) { }
2115 static void module_disable_nx(const struct module *mod) { }
2116 static void module_enable_x(const struct module *mod) { }
2117 #endif /* CONFIG_ARCH_HAS_STRICT_MODULE_RWX */
2119 #ifdef CONFIG_LIVEPATCH
2121 * Persist Elf information about a module. Copy the Elf header,
2122 * section header table, section string table, and symtab section
2123 * index from info to mod->klp_info.
2125 static int copy_module_elf(struct module *mod, struct load_info *info)
2127 unsigned int size, symndx;
2130 size = sizeof(*mod->klp_info);
2131 mod->klp_info = kmalloc(size, GFP_KERNEL);
2132 if (mod->klp_info == NULL)
2136 size = sizeof(mod->klp_info->hdr);
2137 memcpy(&mod->klp_info->hdr, info->hdr, size);
2139 /* Elf section header table */
2140 size = sizeof(*info->sechdrs) * info->hdr->e_shnum;
2141 mod->klp_info->sechdrs = kmemdup(info->sechdrs, size, GFP_KERNEL);
2142 if (mod->klp_info->sechdrs == NULL) {
2147 /* Elf section name string table */
2148 size = info->sechdrs[info->hdr->e_shstrndx].sh_size;
2149 mod->klp_info->secstrings = kmemdup(info->secstrings, size, GFP_KERNEL);
2150 if (mod->klp_info->secstrings == NULL) {
2155 /* Elf symbol section index */
2156 symndx = info->index.sym;
2157 mod->klp_info->symndx = symndx;
2160 * For livepatch modules, core_kallsyms.symtab is a complete
2161 * copy of the original symbol table. Adjust sh_addr to point
2162 * to core_kallsyms.symtab since the copy of the symtab in module
2163 * init memory is freed at the end of do_init_module().
2165 mod->klp_info->sechdrs[symndx].sh_addr = \
2166 (unsigned long) mod->core_kallsyms.symtab;
2171 kfree(mod->klp_info->sechdrs);
2173 kfree(mod->klp_info);
2177 static void free_module_elf(struct module *mod)
2179 kfree(mod->klp_info->sechdrs);
2180 kfree(mod->klp_info->secstrings);
2181 kfree(mod->klp_info);
2183 #else /* !CONFIG_LIVEPATCH */
2184 static int copy_module_elf(struct module *mod, struct load_info *info)
2189 static void free_module_elf(struct module *mod)
2192 #endif /* CONFIG_LIVEPATCH */
2194 void __weak module_memfree(void *module_region)
2196 vfree(module_region);
2199 void __weak module_arch_cleanup(struct module *mod)
2203 void __weak module_arch_freeing_init(struct module *mod)
2207 /* Free a module, remove from lists, etc. */
2208 static void free_module(struct module *mod)
2210 trace_module_free(mod);
2212 mod_sysfs_teardown(mod);
2214 /* We leave it in list to prevent duplicate loads, but make sure
2215 * that noone uses it while it's being deconstructed. */
2216 mutex_lock(&module_mutex);
2217 mod->state = MODULE_STATE_UNFORMED;
2218 mutex_unlock(&module_mutex);
2220 /* Remove dynamic debug info */
2221 ddebug_remove_module(mod->name);
2223 /* Arch-specific cleanup. */
2224 module_arch_cleanup(mod);
2226 /* Module unload stuff */
2227 module_unload_free(mod);
2229 /* Free any allocated parameters. */
2230 destroy_params(mod->kp, mod->num_kp);
2232 if (is_livepatch_module(mod))
2233 free_module_elf(mod);
2235 /* Now we can delete it from the lists */
2236 mutex_lock(&module_mutex);
2237 /* Unlink carefully: kallsyms could be walking list. */
2238 list_del_rcu(&mod->list);
2239 mod_tree_remove(mod);
2240 /* Remove this module from bug list, this uses list_del_rcu */
2241 module_bug_cleanup(mod);
2242 /* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */
2243 synchronize_sched();
2244 mutex_unlock(&module_mutex);
2246 /* This may be empty, but that's OK */
2247 disable_ro_nx(&mod->init_layout);
2248 module_arch_freeing_init(mod);
2249 module_memfree(mod->init_layout.base);
2251 percpu_modfree(mod);
2253 /* Free lock-classes; relies on the preceding sync_rcu(). */
2254 lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);
2256 /* Finally, free the core (containing the module structure) */
2257 disable_ro_nx(&mod->core_layout);
2258 module_memfree(mod->core_layout.base);
2261 void *__symbol_get(const char *symbol)
2263 struct module *owner;
2264 const struct kernel_symbol *sym;
2267 sym = find_symbol(symbol, &owner, NULL, NULL, true, true);
2268 if (sym && strong_try_module_get(owner))
2272 return sym ? (void *)kernel_symbol_value(sym) : NULL;
2274 EXPORT_SYMBOL_GPL(__symbol_get);
2277 * Ensure that an exported symbol [global namespace] does not already exist
2278 * in the kernel or in some other module's exported symbol table.
2280 * You must hold the module_mutex.
2282 static int verify_export_symbols(struct module *mod)
2285 struct module *owner;
2286 const struct kernel_symbol *s;
2288 const struct kernel_symbol *sym;
2291 { mod->syms, mod->num_syms },
2292 { mod->gpl_syms, mod->num_gpl_syms },
2293 { mod->gpl_future_syms, mod->num_gpl_future_syms },
2294 #ifdef CONFIG_UNUSED_SYMBOLS
2295 { mod->unused_syms, mod->num_unused_syms },
2296 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
2300 for (i = 0; i < ARRAY_SIZE(arr); i++) {
2301 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
2302 if (find_symbol(kernel_symbol_name(s), &owner, NULL,
2303 NULL, true, false)) {
2304 pr_err("%s: exports duplicate symbol %s"
2306 mod->name, kernel_symbol_name(s),
2307 module_name(owner));
2315 static bool ignore_undef_symbol(Elf_Half emachine, const char *name)
2318 * On x86, PIC code and Clang non-PIC code may have call foo@PLT. GNU as
2319 * before 2.37 produces an unreferenced _GLOBAL_OFFSET_TABLE_ on x86-64.
2320 * i386 has a similar problem but may not deserve a fix.
2322 * If we ever have to ignore many symbols, consider refactoring the code to
2323 * only warn if referenced by a relocation.
2325 if (emachine == EM_386 || emachine == EM_X86_64)
2326 return !strcmp(name, "_GLOBAL_OFFSET_TABLE_");
2330 /* Change all symbols so that st_value encodes the pointer directly. */
2331 static int simplify_symbols(struct module *mod, const struct load_info *info)
2333 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
2334 Elf_Sym *sym = (void *)symsec->sh_addr;
2335 unsigned long secbase;
2338 const struct kernel_symbol *ksym;
2340 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
2341 const char *name = info->strtab + sym[i].st_name;
2343 switch (sym[i].st_shndx) {
2345 /* Ignore common symbols */
2346 if (!strncmp(name, "__gnu_lto", 9))
2349 /* We compiled with -fno-common. These are not
2350 supposed to happen. */
2351 pr_debug("Common symbol: %s\n", name);
2352 pr_warn("%s: please compile with -fno-common\n",
2358 /* Don't need to do anything */
2359 pr_debug("Absolute symbol: 0x%08lx\n",
2360 (long)sym[i].st_value);
2364 /* Livepatch symbols are resolved by livepatch */
2368 ksym = resolve_symbol_wait(mod, info, name);
2369 /* Ok if resolved. */
2370 if (ksym && !IS_ERR(ksym)) {
2371 sym[i].st_value = kernel_symbol_value(ksym);
2375 /* Ok if weak or ignored. */
2377 (ELF_ST_BIND(sym[i].st_info) == STB_WEAK ||
2378 ignore_undef_symbol(info->hdr->e_machine, name)))
2381 ret = PTR_ERR(ksym) ?: -ENOENT;
2382 pr_warn("%s: Unknown symbol %s (err %d)\n",
2383 mod->name, name, ret);
2387 /* Divert to percpu allocation if a percpu var. */
2388 if (sym[i].st_shndx == info->index.pcpu)
2389 secbase = (unsigned long)mod_percpu(mod);
2391 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
2392 sym[i].st_value += secbase;
2400 static int apply_relocations(struct module *mod, const struct load_info *info)
2405 /* Now do relocations. */
2406 for (i = 1; i < info->hdr->e_shnum; i++) {
2407 unsigned int infosec = info->sechdrs[i].sh_info;
2409 /* Not a valid relocation section? */
2410 if (infosec >= info->hdr->e_shnum)
2413 /* Don't bother with non-allocated sections */
2414 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
2417 /* Livepatch relocation sections are applied by livepatch */
2418 if (info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH)
2421 if (info->sechdrs[i].sh_type == SHT_REL)
2422 err = apply_relocate(info->sechdrs, info->strtab,
2423 info->index.sym, i, mod);
2424 else if (info->sechdrs[i].sh_type == SHT_RELA)
2425 err = apply_relocate_add(info->sechdrs, info->strtab,
2426 info->index.sym, i, mod);
2433 /* Additional bytes needed by arch in front of individual sections */
2434 unsigned int __weak arch_mod_section_prepend(struct module *mod,
2435 unsigned int section)
2437 /* default implementation just returns zero */
2441 /* Update size with this section: return offset. */
2442 static long get_offset(struct module *mod, unsigned int *size,
2443 Elf_Shdr *sechdr, unsigned int section)
2447 *size += arch_mod_section_prepend(mod, section);
2448 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
2449 *size = ret + sechdr->sh_size;
2453 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
2454 might -- code, read-only data, read-write data, small data. Tally
2455 sizes, and place the offsets into sh_entsize fields: high bit means it
2457 static void layout_sections(struct module *mod, struct load_info *info)
2459 static unsigned long const masks[][2] = {
2460 /* NOTE: all executable code must be the first section
2461 * in this array; otherwise modify the text_size
2462 * finder in the two loops below */
2463 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
2464 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
2465 { SHF_RO_AFTER_INIT | SHF_ALLOC, ARCH_SHF_SMALL },
2466 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
2467 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
2471 for (i = 0; i < info->hdr->e_shnum; i++)
2472 info->sechdrs[i].sh_entsize = ~0UL;
2474 pr_debug("Core section allocation order:\n");
2475 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
2476 for (i = 0; i < info->hdr->e_shnum; ++i) {
2477 Elf_Shdr *s = &info->sechdrs[i];
2478 const char *sname = info->secstrings + s->sh_name;
2480 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2481 || (s->sh_flags & masks[m][1])
2482 || s->sh_entsize != ~0UL
2483 || strstarts(sname, ".init"))
2485 s->sh_entsize = get_offset(mod, &mod->core_layout.size, s, i);
2486 pr_debug("\t%s\n", sname);
2489 case 0: /* executable */
2490 mod->core_layout.size = debug_align(mod->core_layout.size);
2491 mod->core_layout.text_size = mod->core_layout.size;
2493 case 1: /* RO: text and ro-data */
2494 mod->core_layout.size = debug_align(mod->core_layout.size);
2495 mod->core_layout.ro_size = mod->core_layout.size;
2497 case 2: /* RO after init */
2498 mod->core_layout.size = debug_align(mod->core_layout.size);
2499 mod->core_layout.ro_after_init_size = mod->core_layout.size;
2501 case 4: /* whole core */
2502 mod->core_layout.size = debug_align(mod->core_layout.size);
2507 pr_debug("Init section allocation order:\n");
2508 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
2509 for (i = 0; i < info->hdr->e_shnum; ++i) {
2510 Elf_Shdr *s = &info->sechdrs[i];
2511 const char *sname = info->secstrings + s->sh_name;
2513 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2514 || (s->sh_flags & masks[m][1])
2515 || s->sh_entsize != ~0UL
2516 || !strstarts(sname, ".init"))
2518 s->sh_entsize = (get_offset(mod, &mod->init_layout.size, s, i)
2519 | INIT_OFFSET_MASK);
2520 pr_debug("\t%s\n", sname);
2523 case 0: /* executable */
2524 mod->init_layout.size = debug_align(mod->init_layout.size);
2525 mod->init_layout.text_size = mod->init_layout.size;
2527 case 1: /* RO: text and ro-data */
2528 mod->init_layout.size = debug_align(mod->init_layout.size);
2529 mod->init_layout.ro_size = mod->init_layout.size;
2533 * RO after init doesn't apply to init_layout (only
2534 * core_layout), so it just takes the value of ro_size.
2536 mod->init_layout.ro_after_init_size = mod->init_layout.ro_size;
2538 case 4: /* whole init */
2539 mod->init_layout.size = debug_align(mod->init_layout.size);
2545 static void set_license(struct module *mod, const char *license)
2548 license = "unspecified";
2550 if (!license_is_gpl_compatible(license)) {
2551 if (!test_taint(TAINT_PROPRIETARY_MODULE))
2552 pr_warn("%s: module license '%s' taints kernel.\n",
2553 mod->name, license);
2554 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
2555 LOCKDEP_NOW_UNRELIABLE);
2559 /* Parse tag=value strings from .modinfo section */
2560 static char *next_string(char *string, unsigned long *secsize)
2562 /* Skip non-zero chars */
2565 if ((*secsize)-- <= 1)
2569 /* Skip any zero padding. */
2570 while (!string[0]) {
2572 if ((*secsize)-- <= 1)
2578 static char *get_modinfo(struct load_info *info, const char *tag)
2581 unsigned int taglen = strlen(tag);
2582 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2583 unsigned long size = infosec->sh_size;
2586 * get_modinfo() calls made before rewrite_section_headers()
2587 * must use sh_offset, as sh_addr isn't set!
2589 for (p = (char *)info->hdr + infosec->sh_offset; p; p = next_string(p, &size)) {
2590 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2591 return p + taglen + 1;
2596 static void setup_modinfo(struct module *mod, struct load_info *info)
2598 struct module_attribute *attr;
2601 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2603 attr->setup(mod, get_modinfo(info, attr->attr.name));
2607 static void free_modinfo(struct module *mod)
2609 struct module_attribute *attr;
2612 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2618 #ifdef CONFIG_KALLSYMS
2620 /* lookup symbol in given range of kernel_symbols */
2621 static const struct kernel_symbol *lookup_symbol(const char *name,
2622 const struct kernel_symbol *start,
2623 const struct kernel_symbol *stop)
2625 return bsearch(name, start, stop - start,
2626 sizeof(struct kernel_symbol), cmp_name);
2629 static int is_exported(const char *name, unsigned long value,
2630 const struct module *mod)
2632 const struct kernel_symbol *ks;
2634 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
2636 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
2637 return ks != NULL && kernel_symbol_value(ks) == value;
2641 static char elf_type(const Elf_Sym *sym, const struct load_info *info)
2643 const Elf_Shdr *sechdrs = info->sechdrs;
2645 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2646 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2651 if (sym->st_shndx == SHN_UNDEF)
2653 if (sym->st_shndx == SHN_ABS || sym->st_shndx == info->index.pcpu)
2655 if (sym->st_shndx >= SHN_LORESERVE)
2657 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2659 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2660 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2661 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2663 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2668 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2669 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2674 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2681 static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
2682 unsigned int shnum, unsigned int pcpundx)
2684 const Elf_Shdr *sec;
2686 if (src->st_shndx == SHN_UNDEF
2687 || src->st_shndx >= shnum
2691 #ifdef CONFIG_KALLSYMS_ALL
2692 if (src->st_shndx == pcpundx)
2696 sec = sechdrs + src->st_shndx;
2697 if (!(sec->sh_flags & SHF_ALLOC)
2698 #ifndef CONFIG_KALLSYMS_ALL
2699 || !(sec->sh_flags & SHF_EXECINSTR)
2701 || (sec->sh_entsize & INIT_OFFSET_MASK))
2708 * We only allocate and copy the strings needed by the parts of symtab
2709 * we keep. This is simple, but has the effect of making multiple
2710 * copies of duplicates. We could be more sophisticated, see
2711 * linux-kernel thread starting with
2712 * <73defb5e4bca04a6431392cc341112b1@localhost>.
2714 static void layout_symtab(struct module *mod, struct load_info *info)
2716 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2717 Elf_Shdr *strsect = info->sechdrs + info->index.str;
2719 unsigned int i, nsrc, ndst, strtab_size = 0;
2721 /* Put symbol section at end of init part of module. */
2722 symsect->sh_flags |= SHF_ALLOC;
2723 symsect->sh_entsize = get_offset(mod, &mod->init_layout.size, symsect,
2724 info->index.sym) | INIT_OFFSET_MASK;
2725 pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
2727 src = (void *)info->hdr + symsect->sh_offset;
2728 nsrc = symsect->sh_size / sizeof(*src);
2730 /* Compute total space required for the core symbols' strtab. */
2731 for (ndst = i = 0; i < nsrc; i++) {
2732 if (i == 0 || is_livepatch_module(mod) ||
2733 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum,
2734 info->index.pcpu)) {
2735 strtab_size += strlen(&info->strtab[src[i].st_name])+1;
2740 /* Append room for core symbols at end of core part. */
2741 info->symoffs = ALIGN(mod->core_layout.size, symsect->sh_addralign ?: 1);
2742 info->stroffs = mod->core_layout.size = info->symoffs + ndst * sizeof(Elf_Sym);
2743 mod->core_layout.size += strtab_size;
2744 mod->core_layout.size = debug_align(mod->core_layout.size);
2746 /* Put string table section at end of init part of module. */
2747 strsect->sh_flags |= SHF_ALLOC;
2748 strsect->sh_entsize = get_offset(mod, &mod->init_layout.size, strsect,
2749 info->index.str) | INIT_OFFSET_MASK;
2750 pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
2752 /* We'll tack temporary mod_kallsyms on the end. */
2753 mod->init_layout.size = ALIGN(mod->init_layout.size,
2754 __alignof__(struct mod_kallsyms));
2755 info->mod_kallsyms_init_off = mod->init_layout.size;
2756 mod->init_layout.size += sizeof(struct mod_kallsyms);
2757 mod->init_layout.size = debug_align(mod->init_layout.size);
2761 * We use the full symtab and strtab which layout_symtab arranged to
2762 * be appended to the init section. Later we switch to the cut-down
2765 static void add_kallsyms(struct module *mod, const struct load_info *info)
2767 unsigned int i, ndst;
2771 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
2773 /* Set up to point into init section. */
2774 mod->kallsyms = mod->init_layout.base + info->mod_kallsyms_init_off;
2776 mod->kallsyms->symtab = (void *)symsec->sh_addr;
2777 mod->kallsyms->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
2778 /* Make sure we get permanent strtab: don't use info->strtab. */
2779 mod->kallsyms->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
2781 /* Set types up while we still have access to sections. */
2782 for (i = 0; i < mod->kallsyms->num_symtab; i++)
2783 mod->kallsyms->symtab[i].st_info
2784 = elf_type(&mod->kallsyms->symtab[i], info);
2786 /* Now populate the cut down core kallsyms for after init. */
2787 mod->core_kallsyms.symtab = dst = mod->core_layout.base + info->symoffs;
2788 mod->core_kallsyms.strtab = s = mod->core_layout.base + info->stroffs;
2789 src = mod->kallsyms->symtab;
2790 for (ndst = i = 0; i < mod->kallsyms->num_symtab; i++) {
2791 if (i == 0 || is_livepatch_module(mod) ||
2792 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum,
2793 info->index.pcpu)) {
2795 dst[ndst++].st_name = s - mod->core_kallsyms.strtab;
2796 s += strlcpy(s, &mod->kallsyms->strtab[src[i].st_name],
2800 mod->core_kallsyms.num_symtab = ndst;
2803 static inline void layout_symtab(struct module *mod, struct load_info *info)
2807 static void add_kallsyms(struct module *mod, const struct load_info *info)
2810 #endif /* CONFIG_KALLSYMS */
2812 static void dynamic_debug_setup(struct module *mod, struct _ddebug *debug, unsigned int num)
2816 #ifdef CONFIG_DYNAMIC_DEBUG
2817 if (ddebug_add_module(debug, num, mod->name))
2818 pr_err("dynamic debug error adding module: %s\n",
2823 static void dynamic_debug_remove(struct module *mod, struct _ddebug *debug)
2826 ddebug_remove_module(mod->name);
2829 void * __weak module_alloc(unsigned long size)
2831 return vmalloc_exec(size);
2834 #ifdef CONFIG_DEBUG_KMEMLEAK
2835 static void kmemleak_load_module(const struct module *mod,
2836 const struct load_info *info)
2840 /* only scan the sections containing data */
2841 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
2843 for (i = 1; i < info->hdr->e_shnum; i++) {
2844 /* Scan all writable sections that's not executable */
2845 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) ||
2846 !(info->sechdrs[i].sh_flags & SHF_WRITE) ||
2847 (info->sechdrs[i].sh_flags & SHF_EXECINSTR))
2850 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2851 info->sechdrs[i].sh_size, GFP_KERNEL);
2855 static inline void kmemleak_load_module(const struct module *mod,
2856 const struct load_info *info)
2861 #ifdef CONFIG_MODULE_SIG
2862 static int module_sig_check(struct load_info *info, int flags)
2865 const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
2866 const void *mod = info->hdr;
2869 * Require flags == 0, as a module with version information
2870 * removed is no longer the module that was signed
2873 info->len > markerlen &&
2874 memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
2875 /* We truncate the module to discard the signature */
2876 info->len -= markerlen;
2877 err = mod_verify_sig(mod, info);
2881 info->sig_ok = true;
2885 /* Not having a signature is only an error if we're strict. */
2886 if (err == -ENOKEY && !is_module_sig_enforced())
2891 #else /* !CONFIG_MODULE_SIG */
2892 static int module_sig_check(struct load_info *info, int flags)
2896 #endif /* !CONFIG_MODULE_SIG */
2898 /* Sanity checks against invalid binaries, wrong arch, weird elf version. */
2899 static int elf_header_check(struct load_info *info)
2901 if (info->len < sizeof(*(info->hdr)))
2904 if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0
2905 || info->hdr->e_type != ET_REL
2906 || !elf_check_arch(info->hdr)
2907 || info->hdr->e_shentsize != sizeof(Elf_Shdr))
2910 if (info->hdr->e_shoff >= info->len
2911 || (info->hdr->e_shnum * sizeof(Elf_Shdr) >
2912 info->len - info->hdr->e_shoff))
2918 #define COPY_CHUNK_SIZE (16*PAGE_SIZE)
2920 static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned long len)
2923 unsigned long n = min(len, COPY_CHUNK_SIZE);
2925 if (copy_from_user(dst, usrc, n) != 0)
2935 #ifdef CONFIG_LIVEPATCH
2936 static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
2938 if (get_modinfo(info, "livepatch")) {
2940 add_taint_module(mod, TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
2941 pr_notice_once("%s: tainting kernel with TAINT_LIVEPATCH\n",
2947 #else /* !CONFIG_LIVEPATCH */
2948 static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
2950 if (get_modinfo(info, "livepatch")) {
2951 pr_err("%s: module is marked as livepatch module, but livepatch support is disabled",
2958 #endif /* CONFIG_LIVEPATCH */
2960 static void check_modinfo_retpoline(struct module *mod, struct load_info *info)
2962 if (retpoline_module_ok(get_modinfo(info, "retpoline")))
2965 pr_warn("%s: loading module not compiled with retpoline compiler.\n",
2969 /* Sets info->hdr and info->len. */
2970 static int copy_module_from_user(const void __user *umod, unsigned long len,
2971 struct load_info *info)
2976 if (info->len < sizeof(*(info->hdr)))
2979 err = security_kernel_load_data(LOADING_MODULE);
2983 /* Suck in entire file: we'll want most of it. */
2984 info->hdr = __vmalloc(info->len,
2985 GFP_KERNEL | __GFP_NOWARN, PAGE_KERNEL);
2989 if (copy_chunked_from_user(info->hdr, umod, info->len) != 0) {
2997 static void free_copy(struct load_info *info)
3002 static int rewrite_section_headers(struct load_info *info, int flags)
3006 /* This should always be true, but let's be sure. */
3007 info->sechdrs[0].sh_addr = 0;
3009 for (i = 1; i < info->hdr->e_shnum; i++) {
3010 Elf_Shdr *shdr = &info->sechdrs[i];
3011 if (shdr->sh_type != SHT_NOBITS
3012 && info->len < shdr->sh_offset + shdr->sh_size) {
3013 pr_err("Module len %lu truncated\n", info->len);
3017 /* Mark all sections sh_addr with their address in the
3019 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
3021 #ifndef CONFIG_MODULE_UNLOAD
3022 /* Don't load .exit sections */
3023 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
3024 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
3028 /* Track but don't keep modinfo and version sections. */
3029 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
3030 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
3036 * Set up our basic convenience variables (pointers to section headers,
3037 * search for module section index etc), and do some basic section
3040 * Set info->mod to the temporary copy of the module in info->hdr. The final one
3041 * will be allocated in move_module().
3043 static int setup_load_info(struct load_info *info, int flags)
3047 /* Set up the convenience variables */
3048 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
3049 info->secstrings = (void *)info->hdr
3050 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
3052 /* Try to find a name early so we can log errors with a module name */
3053 info->index.info = find_sec(info, ".modinfo");
3054 if (info->index.info)
3055 info->name = get_modinfo(info, "name");
3057 /* Find internal symbols and strings. */
3058 for (i = 1; i < info->hdr->e_shnum; i++) {
3059 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
3060 info->index.sym = i;
3061 info->index.str = info->sechdrs[i].sh_link;
3062 info->strtab = (char *)info->hdr
3063 + info->sechdrs[info->index.str].sh_offset;
3068 if (info->index.sym == 0) {
3069 pr_warn("%s: module has no symbols (stripped?)\n",
3070 info->name ?: "(missing .modinfo section or name field)");
3074 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
3075 if (!info->index.mod) {
3076 pr_warn("%s: No module found in object\n",
3077 info->name ?: "(missing .modinfo section or name field)");
3080 /* This is temporary: point mod into copy of data. */
3081 info->mod = (void *)info->hdr + info->sechdrs[info->index.mod].sh_offset;
3084 * If we didn't load the .modinfo 'name' field earlier, fall back to
3085 * on-disk struct mod 'name' field.
3088 info->name = info->mod->name;
3090 if (flags & MODULE_INIT_IGNORE_MODVERSIONS)
3091 info->index.vers = 0; /* Pretend no __versions section! */
3093 info->index.vers = find_sec(info, "__versions");
3095 info->index.pcpu = find_pcpusec(info);
3100 static int check_modinfo(struct module *mod, struct load_info *info, int flags)
3102 const char *modmagic = get_modinfo(info, "vermagic");
3105 if (flags & MODULE_INIT_IGNORE_VERMAGIC)
3108 /* This is allowed: modprobe --force will invalidate it. */
3110 err = try_to_force_load(mod, "bad vermagic");
3113 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
3114 pr_err("%s: version magic '%s' should be '%s'\n",
3115 info->name, modmagic, vermagic);
3119 if (!get_modinfo(info, "intree")) {
3120 if (!test_taint(TAINT_OOT_MODULE))
3121 pr_warn("%s: loading out-of-tree module taints kernel.\n",
3123 add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK);
3126 check_modinfo_retpoline(mod, info);
3128 if (get_modinfo(info, "staging")) {
3129 add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK);
3130 pr_warn("%s: module is from the staging directory, the quality "
3131 "is unknown, you have been warned.\n", mod->name);
3134 err = check_modinfo_livepatch(mod, info);
3138 /* Set up license info based on the info section */
3139 set_license(mod, get_modinfo(info, "license"));
3144 static int find_module_sections(struct module *mod, struct load_info *info)
3146 mod->kp = section_objs(info, "__param",
3147 sizeof(*mod->kp), &mod->num_kp);
3148 mod->syms = section_objs(info, "__ksymtab",
3149 sizeof(*mod->syms), &mod->num_syms);
3150 mod->crcs = section_addr(info, "__kcrctab");
3151 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
3152 sizeof(*mod->gpl_syms),
3153 &mod->num_gpl_syms);
3154 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
3155 mod->gpl_future_syms = section_objs(info,
3156 "__ksymtab_gpl_future",
3157 sizeof(*mod->gpl_future_syms),
3158 &mod->num_gpl_future_syms);
3159 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
3161 #ifdef CONFIG_UNUSED_SYMBOLS
3162 mod->unused_syms = section_objs(info, "__ksymtab_unused",
3163 sizeof(*mod->unused_syms),
3164 &mod->num_unused_syms);
3165 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
3166 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
3167 sizeof(*mod->unused_gpl_syms),
3168 &mod->num_unused_gpl_syms);
3169 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
3171 #ifdef CONFIG_CONSTRUCTORS
3172 mod->ctors = section_objs(info, ".ctors",
3173 sizeof(*mod->ctors), &mod->num_ctors);
3175 mod->ctors = section_objs(info, ".init_array",
3176 sizeof(*mod->ctors), &mod->num_ctors);
3177 else if (find_sec(info, ".init_array")) {
3179 * This shouldn't happen with same compiler and binutils
3180 * building all parts of the module.
3182 pr_warn("%s: has both .ctors and .init_array.\n",
3188 #ifdef CONFIG_TRACEPOINTS
3189 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
3190 sizeof(*mod->tracepoints_ptrs),
3191 &mod->num_tracepoints);
3193 #ifdef CONFIG_JUMP_LABEL
3194 mod->jump_entries = section_objs(info, "__jump_table",
3195 sizeof(*mod->jump_entries),
3196 &mod->num_jump_entries);
3198 #ifdef CONFIG_EVENT_TRACING
3199 mod->trace_events = section_objs(info, "_ftrace_events",
3200 sizeof(*mod->trace_events),
3201 &mod->num_trace_events);
3202 mod->trace_evals = section_objs(info, "_ftrace_eval_map",
3203 sizeof(*mod->trace_evals),
3204 &mod->num_trace_evals);
3206 #ifdef CONFIG_TRACING
3207 mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
3208 sizeof(*mod->trace_bprintk_fmt_start),
3209 &mod->num_trace_bprintk_fmt);
3211 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
3212 /* sechdrs[0].sh_size is always zero */
3213 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
3214 sizeof(*mod->ftrace_callsites),
3215 &mod->num_ftrace_callsites);
3217 #ifdef CONFIG_FUNCTION_ERROR_INJECTION
3218 mod->ei_funcs = section_objs(info, "_error_injection_whitelist",
3219 sizeof(*mod->ei_funcs),
3220 &mod->num_ei_funcs);
3222 mod->extable = section_objs(info, "__ex_table",
3223 sizeof(*mod->extable), &mod->num_exentries);
3225 if (section_addr(info, "__obsparm"))
3226 pr_warn("%s: Ignoring obsolete parameters\n", mod->name);
3228 info->debug = section_objs(info, "__verbose",
3229 sizeof(*info->debug), &info->num_debug);
3234 static int move_module(struct module *mod, struct load_info *info)
3239 /* Do the allocs. */
3240 ptr = module_alloc(mod->core_layout.size);
3242 * The pointer to this block is stored in the module structure
3243 * which is inside the block. Just mark it as not being a
3246 kmemleak_not_leak(ptr);
3250 memset(ptr, 0, mod->core_layout.size);
3251 mod->core_layout.base = ptr;
3253 if (mod->init_layout.size) {
3254 ptr = module_alloc(mod->init_layout.size);
3256 * The pointer to this block is stored in the module structure
3257 * which is inside the block. This block doesn't need to be
3258 * scanned as it contains data and code that will be freed
3259 * after the module is initialized.
3261 kmemleak_ignore(ptr);
3263 module_memfree(mod->core_layout.base);
3266 memset(ptr, 0, mod->init_layout.size);
3267 mod->init_layout.base = ptr;
3269 mod->init_layout.base = NULL;
3271 /* Transfer each section which specifies SHF_ALLOC */
3272 pr_debug("final section addresses:\n");
3273 for (i = 0; i < info->hdr->e_shnum; i++) {
3275 Elf_Shdr *shdr = &info->sechdrs[i];
3277 if (!(shdr->sh_flags & SHF_ALLOC))
3280 if (shdr->sh_entsize & INIT_OFFSET_MASK)
3281 dest = mod->init_layout.base
3282 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
3284 dest = mod->core_layout.base + shdr->sh_entsize;
3286 if (shdr->sh_type != SHT_NOBITS)
3287 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
3288 /* Update sh_addr to point to copy in image. */
3289 shdr->sh_addr = (unsigned long)dest;
3290 pr_debug("\t0x%lx %s\n",
3291 (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
3297 static int check_module_license_and_versions(struct module *mod)
3299 int prev_taint = test_taint(TAINT_PROPRIETARY_MODULE);
3302 * ndiswrapper is under GPL by itself, but loads proprietary modules.
3303 * Don't use add_taint_module(), as it would prevent ndiswrapper from
3304 * using GPL-only symbols it needs.
3306 if (strcmp(mod->name, "ndiswrapper") == 0)
3307 add_taint(TAINT_PROPRIETARY_MODULE, LOCKDEP_NOW_UNRELIABLE);
3309 /* driverloader was caught wrongly pretending to be under GPL */
3310 if (strcmp(mod->name, "driverloader") == 0)
3311 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
3312 LOCKDEP_NOW_UNRELIABLE);
3314 /* lve claims to be GPL but upstream won't provide source */
3315 if (strcmp(mod->name, "lve") == 0)
3316 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
3317 LOCKDEP_NOW_UNRELIABLE);
3319 if (!prev_taint && test_taint(TAINT_PROPRIETARY_MODULE))
3320 pr_warn("%s: module license taints kernel.\n", mod->name);
3322 #ifdef CONFIG_MODVERSIONS
3323 if ((mod->num_syms && !mod->crcs)
3324 || (mod->num_gpl_syms && !mod->gpl_crcs)
3325 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
3326 #ifdef CONFIG_UNUSED_SYMBOLS
3327 || (mod->num_unused_syms && !mod->unused_crcs)
3328 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
3331 return try_to_force_load(mod,
3332 "no versions for exported symbols");
3338 static void flush_module_icache(const struct module *mod)
3340 mm_segment_t old_fs;
3342 /* flush the icache in correct context */
3347 * Flush the instruction cache, since we've played with text.
3348 * Do it before processing of module parameters, so the module
3349 * can provide parameter accessor functions of its own.
3351 if (mod->init_layout.base)
3352 flush_icache_range((unsigned long)mod->init_layout.base,
3353 (unsigned long)mod->init_layout.base
3354 + mod->init_layout.size);
3355 flush_icache_range((unsigned long)mod->core_layout.base,
3356 (unsigned long)mod->core_layout.base + mod->core_layout.size);
3361 int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
3369 /* module_blacklist is a comma-separated list of module names */
3370 static char *module_blacklist;
3371 static bool blacklisted(const char *module_name)
3376 if (!module_blacklist)
3379 for (p = module_blacklist; *p; p += len) {
3380 len = strcspn(p, ",");
3381 if (strlen(module_name) == len && !memcmp(module_name, p, len))
3388 core_param(module_blacklist, module_blacklist, charp, 0400);
3390 static struct module *layout_and_allocate(struct load_info *info, int flags)
3396 err = check_modinfo(info->mod, info, flags);
3398 return ERR_PTR(err);
3400 /* Allow arches to frob section contents and sizes. */
3401 err = module_frob_arch_sections(info->hdr, info->sechdrs,
3402 info->secstrings, info->mod);
3404 return ERR_PTR(err);
3406 /* We will do a special allocation for per-cpu sections later. */
3407 info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
3410 * Mark ro_after_init section with SHF_RO_AFTER_INIT so that
3411 * layout_sections() can put it in the right place.
3412 * Note: ro_after_init sections also have SHF_{WRITE,ALLOC} set.
3414 ndx = find_sec(info, ".data..ro_after_init");
3416 info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT;
3418 /* Determine total sizes, and put offsets in sh_entsize. For now
3419 this is done generically; there doesn't appear to be any
3420 special cases for the architectures. */
3421 layout_sections(info->mod, info);
3422 layout_symtab(info->mod, info);
3424 /* Allocate and move to the final place */
3425 err = move_module(info->mod, info);
3427 return ERR_PTR(err);
3429 /* Module has been copied to its final place now: return it. */
3430 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
3431 kmemleak_load_module(mod, info);
3435 /* mod is no longer valid after this! */
3436 static void module_deallocate(struct module *mod, struct load_info *info)
3438 percpu_modfree(mod);
3439 module_arch_freeing_init(mod);
3440 module_memfree(mod->init_layout.base);
3441 module_memfree(mod->core_layout.base);
3444 int __weak module_finalize(const Elf_Ehdr *hdr,
3445 const Elf_Shdr *sechdrs,
3451 static int post_relocation(struct module *mod, const struct load_info *info)
3453 /* Sort exception table now relocations are done. */
3454 sort_extable(mod->extable, mod->extable + mod->num_exentries);
3456 /* Copy relocated percpu area over. */
3457 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
3458 info->sechdrs[info->index.pcpu].sh_size);
3460 /* Setup kallsyms-specific fields. */
3461 add_kallsyms(mod, info);
3463 /* Arch-specific module finalizing. */
3464 return module_finalize(info->hdr, info->sechdrs, mod);
3467 /* Is this module of this name done loading? No locks held. */
3468 static bool finished_loading(const char *name)
3474 * The module_mutex should not be a heavily contended lock;
3475 * if we get the occasional sleep here, we'll go an extra iteration
3476 * in the wait_event_interruptible(), which is harmless.
3478 sched_annotate_sleep();
3479 mutex_lock(&module_mutex);
3480 mod = find_module_all(name, strlen(name), true);
3481 ret = !mod || mod->state == MODULE_STATE_LIVE;
3482 mutex_unlock(&module_mutex);
3487 /* Call module constructors. */
3488 static void do_mod_ctors(struct module *mod)
3490 #ifdef CONFIG_CONSTRUCTORS
3493 for (i = 0; i < mod->num_ctors; i++)
3498 /* For freeing module_init on success, in case kallsyms traversing */
3499 struct mod_initfree {
3500 struct rcu_head rcu;
3504 static void do_free_init(struct rcu_head *head)
3506 struct mod_initfree *m = container_of(head, struct mod_initfree, rcu);
3507 module_memfree(m->module_init);
3512 * This is where the real work happens.
3514 * Keep it uninlined to provide a reliable breakpoint target, e.g. for the gdb
3515 * helper command 'lx-symbols'.
3517 static noinline int do_init_module(struct module *mod)
3520 struct mod_initfree *freeinit;
3522 freeinit = kmalloc(sizeof(*freeinit), GFP_KERNEL);
3527 freeinit->module_init = mod->init_layout.base;
3530 * We want to find out whether @mod uses async during init. Clear
3531 * PF_USED_ASYNC. async_schedule*() will set it.
3533 current->flags &= ~PF_USED_ASYNC;
3536 /* Start the module */
3537 if (mod->init != NULL)
3538 ret = do_one_initcall(mod->init);
3540 goto fail_free_freeinit;
3543 pr_warn("%s: '%s'->init suspiciously returned %d, it should "
3544 "follow 0/-E convention\n"
3545 "%s: loading module anyway...\n",
3546 __func__, mod->name, ret, __func__);
3550 /* Now it's a first class citizen! */
3551 mod->state = MODULE_STATE_LIVE;
3552 blocking_notifier_call_chain(&module_notify_list,
3553 MODULE_STATE_LIVE, mod);
3555 /* Delay uevent until module has finished its init routine */
3556 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
3559 * We need to finish all async code before the module init sequence
3560 * is done. This has potential to deadlock. For example, a newly
3561 * detected block device can trigger request_module() of the
3562 * default iosched from async probing task. Once userland helper
3563 * reaches here, async_synchronize_full() will wait on the async
3564 * task waiting on request_module() and deadlock.
3566 * This deadlock is avoided by perfomring async_synchronize_full()
3567 * iff module init queued any async jobs. This isn't a full
3568 * solution as it will deadlock the same if module loading from
3569 * async jobs nests more than once; however, due to the various
3570 * constraints, this hack seems to be the best option for now.
3571 * Please refer to the following thread for details.
3573 * http://thread.gmane.org/gmane.linux.kernel/1420814
3575 if (!mod->async_probe_requested && (current->flags & PF_USED_ASYNC))
3576 async_synchronize_full();
3578 ftrace_free_mem(mod, mod->init_layout.base, mod->init_layout.base +
3579 mod->init_layout.size);
3580 mutex_lock(&module_mutex);
3581 /* Drop initial reference. */
3583 trim_init_extable(mod);
3584 #ifdef CONFIG_KALLSYMS
3585 /* Switch to core kallsyms now init is done: kallsyms may be walking! */
3586 rcu_assign_pointer(mod->kallsyms, &mod->core_kallsyms);
3588 module_enable_ro(mod, true);
3589 mod_tree_remove_init(mod);
3590 disable_ro_nx(&mod->init_layout);
3591 module_arch_freeing_init(mod);
3592 mod->init_layout.base = NULL;
3593 mod->init_layout.size = 0;
3594 mod->init_layout.ro_size = 0;
3595 mod->init_layout.ro_after_init_size = 0;
3596 mod->init_layout.text_size = 0;
3598 * We want to free module_init, but be aware that kallsyms may be
3599 * walking this with preempt disabled. In all the failure paths, we
3600 * call synchronize_sched(), but we don't want to slow down the success
3601 * path, so use actual RCU here.
3602 * Note that module_alloc() on most architectures creates W+X page
3603 * mappings which won't be cleaned up until do_free_init() runs. Any
3604 * code such as mark_rodata_ro() which depends on those mappings to
3605 * be cleaned up needs to sync with the queued work - ie
3606 * rcu_barrier_sched()
3608 call_rcu_sched(&freeinit->rcu, do_free_init);
3609 mutex_unlock(&module_mutex);
3610 wake_up_all(&module_wq);
3617 /* Try to protect us from buggy refcounters. */
3618 mod->state = MODULE_STATE_GOING;
3619 synchronize_sched();
3621 blocking_notifier_call_chain(&module_notify_list,
3622 MODULE_STATE_GOING, mod);
3623 klp_module_going(mod);
3624 ftrace_release_mod(mod);
3626 wake_up_all(&module_wq);
3630 static int may_init_module(void)
3632 if (!capable(CAP_SYS_MODULE) || modules_disabled)
3639 * We try to place it in the list now to make sure it's unique before
3640 * we dedicate too many resources. In particular, temporary percpu
3641 * memory exhaustion.
3643 static int add_unformed_module(struct module *mod)
3648 mod->state = MODULE_STATE_UNFORMED;
3651 mutex_lock(&module_mutex);
3652 old = find_module_all(mod->name, strlen(mod->name), true);
3654 if (old->state != MODULE_STATE_LIVE) {
3655 /* Wait in case it fails to load. */
3656 mutex_unlock(&module_mutex);
3657 err = wait_event_interruptible(module_wq,
3658 finished_loading(mod->name));
3666 mod_update_bounds(mod);
3667 list_add_rcu(&mod->list, &modules);
3668 mod_tree_insert(mod);
3672 mutex_unlock(&module_mutex);
3677 static int complete_formation(struct module *mod, struct load_info *info)
3681 mutex_lock(&module_mutex);
3683 /* Find duplicate symbols (must be called under lock). */
3684 err = verify_export_symbols(mod);
3688 /* This relies on module_mutex for list integrity. */
3689 module_bug_finalize(info->hdr, info->sechdrs, mod);
3691 module_enable_ro(mod, false);
3692 module_enable_nx(mod);
3693 module_enable_x(mod);
3695 /* Mark state as coming so strong_try_module_get() ignores us,
3696 * but kallsyms etc. can see us. */
3697 mod->state = MODULE_STATE_COMING;
3698 mutex_unlock(&module_mutex);
3703 mutex_unlock(&module_mutex);
3707 static int prepare_coming_module(struct module *mod)
3711 ftrace_module_enable(mod);
3712 err = klp_module_coming(mod);
3716 blocking_notifier_call_chain(&module_notify_list,
3717 MODULE_STATE_COMING, mod);
3721 static int unknown_module_param_cb(char *param, char *val, const char *modname,
3724 struct module *mod = arg;
3727 if (strcmp(param, "async_probe") == 0) {
3728 mod->async_probe_requested = true;
3732 /* Check for magic 'dyndbg' arg */
3733 ret = ddebug_dyndbg_module_param_cb(param, val, modname);
3735 pr_warn("%s: unknown parameter '%s' ignored\n", modname, param);
3739 /* Allocate and load the module: note that size of section 0 is always
3740 zero, and we rely on this for optional sections. */
3741 static int load_module(struct load_info *info, const char __user *uargs,
3748 err = elf_header_check(info);
3752 err = setup_load_info(info, flags);
3756 if (blacklisted(info->name)) {
3761 err = module_sig_check(info, flags);
3765 err = rewrite_section_headers(info, flags);
3769 /* Check module struct version now, before we try to use module. */
3770 if (!check_modstruct_version(info, info->mod)) {
3775 /* Figure out module layout, and allocate all the memory. */
3776 mod = layout_and_allocate(info, flags);
3782 audit_log_kern_module(mod->name);
3784 /* Reserve our place in the list. */
3785 err = add_unformed_module(mod);
3789 #ifdef CONFIG_MODULE_SIG
3790 mod->sig_ok = info->sig_ok;
3792 pr_notice_once("%s: module verification failed: signature "
3793 "and/or required key missing - tainting "
3794 "kernel\n", mod->name);
3795 add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK);
3799 /* To avoid stressing percpu allocator, do this once we're unique. */
3800 err = percpu_modalloc(mod, info);
3804 /* Now module is in final location, initialize linked lists, etc. */
3805 err = module_unload_init(mod);
3809 init_param_lock(mod);
3811 /* Now we've got everything in the final locations, we can
3812 * find optional sections. */
3813 err = find_module_sections(mod, info);
3817 err = check_module_license_and_versions(mod);
3821 /* Set up MODINFO_ATTR fields */
3822 setup_modinfo(mod, info);
3824 /* Fix up syms, so that st_value is a pointer to location. */
3825 err = simplify_symbols(mod, info);
3829 err = apply_relocations(mod, info);
3833 err = post_relocation(mod, info);
3837 flush_module_icache(mod);
3839 /* Now copy in args */
3840 mod->args = strndup_user(uargs, ~0UL >> 1);
3841 if (IS_ERR(mod->args)) {
3842 err = PTR_ERR(mod->args);
3843 goto free_arch_cleanup;
3846 dynamic_debug_setup(mod, info->debug, info->num_debug);
3848 /* Ftrace init must be called in the MODULE_STATE_UNFORMED state */
3849 ftrace_module_init(mod);
3851 /* Finally it's fully formed, ready to start executing. */
3852 err = complete_formation(mod, info);
3854 goto ddebug_cleanup;
3856 err = prepare_coming_module(mod);
3860 /* Module is ready to execute: parsing args may do that. */
3861 after_dashes = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
3863 unknown_module_param_cb);
3864 if (IS_ERR(after_dashes)) {
3865 err = PTR_ERR(after_dashes);
3866 goto coming_cleanup;
3867 } else if (after_dashes) {
3868 pr_warn("%s: parameters '%s' after `--' ignored\n",
3869 mod->name, after_dashes);
3872 /* Link in to sysfs. */
3873 err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp);
3875 goto coming_cleanup;
3877 if (is_livepatch_module(mod)) {
3878 err = copy_module_elf(mod, info);
3883 /* Get rid of temporary copy. */
3887 trace_module_load(mod);
3889 return do_init_module(mod);
3892 mod_sysfs_teardown(mod);
3894 mod->state = MODULE_STATE_GOING;
3895 destroy_params(mod->kp, mod->num_kp);
3896 blocking_notifier_call_chain(&module_notify_list,
3897 MODULE_STATE_GOING, mod);
3898 klp_module_going(mod);
3900 mod->state = MODULE_STATE_GOING;
3901 /* module_bug_cleanup needs module_mutex protection */
3902 mutex_lock(&module_mutex);
3903 module_bug_cleanup(mod);
3904 mutex_unlock(&module_mutex);
3906 /* we can't deallocate the module until we clear memory protection */
3907 module_disable_ro(mod);
3908 module_disable_nx(mod);
3911 ftrace_release_mod(mod);
3912 dynamic_debug_remove(mod, info->debug);
3913 synchronize_sched();
3916 module_arch_cleanup(mod);
3920 module_unload_free(mod);
3922 mutex_lock(&module_mutex);
3923 /* Unlink carefully: kallsyms could be walking list. */
3924 list_del_rcu(&mod->list);
3925 mod_tree_remove(mod);
3926 wake_up_all(&module_wq);
3927 /* Wait for RCU-sched synchronizing before releasing mod->list. */
3928 synchronize_sched();
3929 mutex_unlock(&module_mutex);
3931 /* Free lock-classes; relies on the preceding sync_rcu() */
3932 lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);
3934 module_deallocate(mod, info);
3940 SYSCALL_DEFINE3(init_module, void __user *, umod,
3941 unsigned long, len, const char __user *, uargs)
3944 struct load_info info = { };
3946 err = may_init_module();
3950 pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n",
3953 err = copy_module_from_user(umod, len, &info);
3957 return load_module(&info, uargs, 0);
3960 SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
3962 struct load_info info = { };
3967 err = may_init_module();
3971 pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
3973 if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
3974 |MODULE_INIT_IGNORE_VERMAGIC))
3977 err = kernel_read_file_from_fd(fd, &hdr, &size, INT_MAX,
3984 return load_module(&info, uargs, flags);
3987 static inline int within(unsigned long addr, void *start, unsigned long size)
3989 return ((void *)addr >= start && (void *)addr < start + size);
3992 #ifdef CONFIG_KALLSYMS
3994 * This ignores the intensely annoying "mapping symbols" found
3995 * in ARM ELF files: $a, $t and $d.
3997 static inline int is_arm_mapping_symbol(const char *str)
3999 if (str[0] == '.' && str[1] == 'L')
4001 return str[0] == '$' && strchr("axtd", str[1])
4002 && (str[2] == '\0' || str[2] == '.');
4005 static const char *symname(struct mod_kallsyms *kallsyms, unsigned int symnum)
4007 return kallsyms->strtab + kallsyms->symtab[symnum].st_name;
4010 static const char *get_ksymbol(struct module *mod,
4012 unsigned long *size,
4013 unsigned long *offset)
4015 unsigned int i, best = 0;
4016 unsigned long nextval;
4017 struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
4019 /* At worse, next value is at end of module */
4020 if (within_module_init(addr, mod))
4021 nextval = (unsigned long)mod->init_layout.base+mod->init_layout.text_size;
4023 nextval = (unsigned long)mod->core_layout.base+mod->core_layout.text_size;
4025 /* Scan for closest preceding symbol, and next symbol. (ELF
4026 starts real symbols at 1). */
4027 for (i = 1; i < kallsyms->num_symtab; i++) {
4028 if (kallsyms->symtab[i].st_shndx == SHN_UNDEF)
4031 /* We ignore unnamed symbols: they're uninformative
4032 * and inserted at a whim. */
4033 if (*symname(kallsyms, i) == '\0'
4034 || is_arm_mapping_symbol(symname(kallsyms, i)))
4037 if (kallsyms->symtab[i].st_value <= addr
4038 && kallsyms->symtab[i].st_value > kallsyms->symtab[best].st_value)
4040 if (kallsyms->symtab[i].st_value > addr
4041 && kallsyms->symtab[i].st_value < nextval)
4042 nextval = kallsyms->symtab[i].st_value;
4049 *size = nextval - kallsyms->symtab[best].st_value;
4051 *offset = addr - kallsyms->symtab[best].st_value;
4052 return symname(kallsyms, best);
4055 void * __weak dereference_module_function_descriptor(struct module *mod,
4061 /* For kallsyms to ask for address resolution. NULL means not found. Careful
4062 * not to lock to avoid deadlock on oopses, simply disable preemption. */
4063 const char *module_address_lookup(unsigned long addr,
4064 unsigned long *size,
4065 unsigned long *offset,
4069 const char *ret = NULL;
4073 mod = __module_address(addr);
4076 *modname = mod->name;
4077 ret = get_ksymbol(mod, addr, size, offset);
4079 /* Make a copy in here where it's safe */
4081 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
4089 int lookup_module_symbol_name(unsigned long addr, char *symname)
4094 list_for_each_entry_rcu(mod, &modules, list) {
4095 if (mod->state == MODULE_STATE_UNFORMED)
4097 if (within_module(addr, mod)) {
4100 sym = get_ksymbol(mod, addr, NULL, NULL);
4103 strlcpy(symname, sym, KSYM_NAME_LEN);
4113 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
4114 unsigned long *offset, char *modname, char *name)
4119 list_for_each_entry_rcu(mod, &modules, list) {
4120 if (mod->state == MODULE_STATE_UNFORMED)
4122 if (within_module(addr, mod)) {
4125 sym = get_ksymbol(mod, addr, size, offset);
4129 strlcpy(modname, mod->name, MODULE_NAME_LEN);
4131 strlcpy(name, sym, KSYM_NAME_LEN);
4141 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
4142 char *name, char *module_name, int *exported)
4147 list_for_each_entry_rcu(mod, &modules, list) {
4148 struct mod_kallsyms *kallsyms;
4150 if (mod->state == MODULE_STATE_UNFORMED)
4152 kallsyms = rcu_dereference_sched(mod->kallsyms);
4153 if (symnum < kallsyms->num_symtab) {
4154 *value = kallsyms->symtab[symnum].st_value;
4155 *type = kallsyms->symtab[symnum].st_info;
4156 strlcpy(name, symname(kallsyms, symnum), KSYM_NAME_LEN);
4157 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
4158 *exported = is_exported(name, *value, mod);
4162 symnum -= kallsyms->num_symtab;
4168 static unsigned long mod_find_symname(struct module *mod, const char *name)
4171 struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
4173 for (i = 0; i < kallsyms->num_symtab; i++)
4174 if (strcmp(name, symname(kallsyms, i)) == 0 &&
4175 kallsyms->symtab[i].st_shndx != SHN_UNDEF)
4176 return kallsyms->symtab[i].st_value;
4180 /* Look for this name: can be of form module:name. */
4181 unsigned long module_kallsyms_lookup_name(const char *name)
4185 unsigned long ret = 0;
4187 /* Don't lock: we're in enough trouble already. */
4189 if ((colon = strnchr(name, MODULE_NAME_LEN, ':')) != NULL) {
4190 if ((mod = find_module_all(name, colon - name, false)) != NULL)
4191 ret = mod_find_symname(mod, colon+1);
4193 list_for_each_entry_rcu(mod, &modules, list) {
4194 if (mod->state == MODULE_STATE_UNFORMED)
4196 if ((ret = mod_find_symname(mod, name)) != 0)
4204 int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
4205 struct module *, unsigned long),
4212 module_assert_mutex();
4214 list_for_each_entry(mod, &modules, list) {
4215 /* We hold module_mutex: no need for rcu_dereference_sched */
4216 struct mod_kallsyms *kallsyms = mod->kallsyms;
4218 if (mod->state == MODULE_STATE_UNFORMED)
4220 for (i = 0; i < kallsyms->num_symtab; i++) {
4222 if (kallsyms->symtab[i].st_shndx == SHN_UNDEF)
4225 ret = fn(data, symname(kallsyms, i),
4226 mod, kallsyms->symtab[i].st_value);
4233 #endif /* CONFIG_KALLSYMS */
4235 /* Maximum number of characters written by module_flags() */
4236 #define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
4238 /* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */
4239 static char *module_flags(struct module *mod, char *buf)
4243 BUG_ON(mod->state == MODULE_STATE_UNFORMED);
4245 mod->state == MODULE_STATE_GOING ||
4246 mod->state == MODULE_STATE_COMING) {
4248 bx += module_flags_taint(mod, buf + bx);
4249 /* Show a - for module-is-being-unloaded */
4250 if (mod->state == MODULE_STATE_GOING)
4252 /* Show a + for module-is-being-loaded */
4253 if (mod->state == MODULE_STATE_COMING)
4262 #ifdef CONFIG_PROC_FS
4263 /* Called by the /proc file system to return a list of modules. */
4264 static void *m_start(struct seq_file *m, loff_t *pos)
4266 mutex_lock(&module_mutex);
4267 return seq_list_start(&modules, *pos);
4270 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
4272 return seq_list_next(p, &modules, pos);
4275 static void m_stop(struct seq_file *m, void *p)
4277 mutex_unlock(&module_mutex);
4280 static int m_show(struct seq_file *m, void *p)
4282 struct module *mod = list_entry(p, struct module, list);
4283 char buf[MODULE_FLAGS_BUF_SIZE];
4286 /* We always ignore unformed modules. */
4287 if (mod->state == MODULE_STATE_UNFORMED)
4290 seq_printf(m, "%s %u",
4291 mod->name, mod->init_layout.size + mod->core_layout.size);
4292 print_unload_info(m, mod);
4294 /* Informative for users. */
4295 seq_printf(m, " %s",
4296 mod->state == MODULE_STATE_GOING ? "Unloading" :
4297 mod->state == MODULE_STATE_COMING ? "Loading" :
4299 /* Used by oprofile and other similar tools. */
4300 value = m->private ? NULL : mod->core_layout.base;
4301 seq_printf(m, " 0x%px", value);
4305 seq_printf(m, " %s", module_flags(mod, buf));
4311 /* Format: modulename size refcount deps address
4313 Where refcount is a number or -, and deps is a comma-separated list
4316 static const struct seq_operations modules_op = {
4324 * This also sets the "private" pointer to non-NULL if the
4325 * kernel pointers should be hidden (so you can just test
4326 * "m->private" to see if you should keep the values private).
4328 * We use the same logic as for /proc/kallsyms.
4330 static int modules_open(struct inode *inode, struct file *file)
4332 int err = seq_open(file, &modules_op);
4335 struct seq_file *m = file->private_data;
4336 m->private = kallsyms_show_value(file->f_cred) ? NULL : (void *)8ul;
4342 static const struct file_operations proc_modules_operations = {
4343 .open = modules_open,
4345 .llseek = seq_lseek,
4346 .release = seq_release,
4349 static int __init proc_modules_init(void)
4351 proc_create("modules", 0, NULL, &proc_modules_operations);
4354 module_init(proc_modules_init);
4357 /* Given an address, look for it in the module exception tables. */
4358 const struct exception_table_entry *search_module_extables(unsigned long addr)
4360 const struct exception_table_entry *e = NULL;
4364 mod = __module_address(addr);
4368 if (!mod->num_exentries)
4371 e = search_extable(mod->extable,
4378 * Now, if we found one, we are running inside it now, hence
4379 * we cannot unload the module, hence no refcnt needed.
4385 * is_module_address - is this address inside a module?
4386 * @addr: the address to check.
4388 * See is_module_text_address() if you simply want to see if the address
4389 * is code (not data).
4391 bool is_module_address(unsigned long addr)
4396 ret = __module_address(addr) != NULL;
4403 * __module_address - get the module which contains an address.
4404 * @addr: the address.
4406 * Must be called with preempt disabled or module mutex held so that
4407 * module doesn't get freed during this.
4409 struct module *__module_address(unsigned long addr)
4413 if (addr < module_addr_min || addr > module_addr_max)
4416 module_assert_mutex_or_preempt();
4418 mod = mod_find(addr);
4420 BUG_ON(!within_module(addr, mod));
4421 if (mod->state == MODULE_STATE_UNFORMED)
4428 * is_module_text_address - is this address inside module code?
4429 * @addr: the address to check.
4431 * See is_module_address() if you simply want to see if the address is
4432 * anywhere in a module. See kernel_text_address() for testing if an
4433 * address corresponds to kernel or module code.
4435 bool is_module_text_address(unsigned long addr)
4440 ret = __module_text_address(addr) != NULL;
4447 * __module_text_address - get the module whose code contains an address.
4448 * @addr: the address.
4450 * Must be called with preempt disabled or module mutex held so that
4451 * module doesn't get freed during this.
4453 struct module *__module_text_address(unsigned long addr)
4455 struct module *mod = __module_address(addr);
4457 /* Make sure it's within the text section. */
4458 if (!within(addr, mod->init_layout.base, mod->init_layout.text_size)
4459 && !within(addr, mod->core_layout.base, mod->core_layout.text_size))
4465 /* Don't grab lock, we're oopsing. */
4466 void print_modules(void)
4469 char buf[MODULE_FLAGS_BUF_SIZE];
4471 printk(KERN_DEFAULT "Modules linked in:");
4472 /* Most callers should already have preempt disabled, but make sure */
4474 list_for_each_entry_rcu(mod, &modules, list) {
4475 if (mod->state == MODULE_STATE_UNFORMED)
4477 pr_cont(" %s%s", mod->name, module_flags(mod, buf));
4480 if (last_unloaded_module[0])
4481 pr_cont(" [last unloaded: %s]", last_unloaded_module);
4485 #ifdef CONFIG_MODVERSIONS
4486 /* Generate the signature for all relevant module structures here.
4487 * If these change, we don't want to try to parse the module. */
4488 void module_layout(struct module *mod,
4489 struct modversion_info *ver,
4490 struct kernel_param *kp,
4491 struct kernel_symbol *ks,
4492 struct tracepoint * const *tp)
4495 EXPORT_SYMBOL(module_layout);