2 Copyright (C) 2002 Richard Henderson
3 Copyright (C) 2001 Rusty Russell, 2002 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/module.h>
20 #include <linux/moduleloader.h>
21 #include <linux/init.h>
22 #include <linux/kallsyms.h>
23 #include <linux/sysfs.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/vmalloc.h>
27 #include <linux/elf.h>
28 #include <linux/seq_file.h>
29 #include <linux/syscalls.h>
30 #include <linux/fcntl.h>
31 #include <linux/rcupdate.h>
32 #include <linux/capability.h>
33 #include <linux/cpu.h>
34 #include <linux/moduleparam.h>
35 #include <linux/errno.h>
36 #include <linux/err.h>
37 #include <linux/vermagic.h>
38 #include <linux/notifier.h>
39 #include <linux/sched.h>
40 #include <linux/stop_machine.h>
41 #include <linux/device.h>
42 #include <linux/string.h>
43 #include <linux/mutex.h>
44 #include <linux/unwind.h>
45 #include <asm/uaccess.h>
46 #include <asm/semaphore.h>
47 #include <asm/cacheflush.h>
48 #include <linux/license.h>
50 extern int module_sysfs_initialized;
55 #define DEBUGP(fmt , a...)
58 #ifndef ARCH_SHF_SMALL
59 #define ARCH_SHF_SMALL 0
62 /* If this is set, the section belongs in the init part of the module */
63 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
65 /* List of modules, protected by module_mutex or preempt_disable
66 * (add/delete uses stop_machine). */
67 static DEFINE_MUTEX(module_mutex);
68 static LIST_HEAD(modules);
70 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
72 int register_module_notifier(struct notifier_block * nb)
74 return blocking_notifier_chain_register(&module_notify_list, nb);
76 EXPORT_SYMBOL(register_module_notifier);
78 int unregister_module_notifier(struct notifier_block * nb)
80 return blocking_notifier_chain_unregister(&module_notify_list, nb);
82 EXPORT_SYMBOL(unregister_module_notifier);
84 /* We require a truly strong try_module_get(): 0 means failure due to
85 ongoing or failed initialization etc. */
86 static inline int strong_try_module_get(struct module *mod)
88 if (mod && mod->state == MODULE_STATE_COMING)
90 return try_module_get(mod);
93 static inline void add_taint_module(struct module *mod, unsigned flag)
100 * A thread that wants to hold a reference to a module only while it
101 * is running can call this to safely exit. nfsd and lockd use this.
103 void __module_put_and_exit(struct module *mod, long code)
108 EXPORT_SYMBOL(__module_put_and_exit);
110 /* Find a module section: 0 means not found. */
111 static unsigned int find_sec(Elf_Ehdr *hdr,
113 const char *secstrings,
118 for (i = 1; i < hdr->e_shnum; i++)
119 /* Alloc bit cleared means "ignore it." */
120 if ((sechdrs[i].sh_flags & SHF_ALLOC)
121 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
126 /* Provided by the linker */
127 extern const struct kernel_symbol __start___ksymtab[];
128 extern const struct kernel_symbol __stop___ksymtab[];
129 extern const struct kernel_symbol __start___ksymtab_gpl[];
130 extern const struct kernel_symbol __stop___ksymtab_gpl[];
131 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
132 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
133 extern const struct kernel_symbol __start___ksymtab_unused[];
134 extern const struct kernel_symbol __stop___ksymtab_unused[];
135 extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
136 extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
137 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
138 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
139 extern const unsigned long __start___kcrctab[];
140 extern const unsigned long __start___kcrctab_gpl[];
141 extern const unsigned long __start___kcrctab_gpl_future[];
142 extern const unsigned long __start___kcrctab_unused[];
143 extern const unsigned long __start___kcrctab_unused_gpl[];
145 #ifndef CONFIG_MODVERSIONS
146 #define symversion(base, idx) NULL
148 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
151 /* lookup symbol in given range of kernel_symbols */
152 static const struct kernel_symbol *lookup_symbol(const char *name,
153 const struct kernel_symbol *start,
154 const struct kernel_symbol *stop)
156 const struct kernel_symbol *ks = start;
157 for (; ks < stop; ks++)
158 if (strcmp(ks->name, name) == 0)
163 static void printk_unused_warning(const char *name)
165 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
166 "however this module is using it.\n", name);
167 printk(KERN_WARNING "This symbol will go away in the future.\n");
168 printk(KERN_WARNING "Please evalute if this is the right api to use, "
169 "and if it really is, submit a report the linux kernel "
170 "mailinglist together with submitting your code for "
174 /* Find a symbol, return value, crc and module which owns it */
175 static unsigned long __find_symbol(const char *name,
176 struct module **owner,
177 const unsigned long **crc,
181 const struct kernel_symbol *ks;
183 /* Core kernel first. */
185 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
187 *crc = symversion(__start___kcrctab, (ks - __start___ksymtab));
191 ks = lookup_symbol(name, __start___ksymtab_gpl,
192 __stop___ksymtab_gpl);
194 *crc = symversion(__start___kcrctab_gpl,
195 (ks - __start___ksymtab_gpl));
199 ks = lookup_symbol(name, __start___ksymtab_gpl_future,
200 __stop___ksymtab_gpl_future);
203 printk(KERN_WARNING "Symbol %s is being used "
204 "by a non-GPL module, which will not "
205 "be allowed in the future\n", name);
206 printk(KERN_WARNING "Please see the file "
207 "Documentation/feature-removal-schedule.txt "
208 "in the kernel source tree for more "
211 *crc = symversion(__start___kcrctab_gpl_future,
212 (ks - __start___ksymtab_gpl_future));
216 ks = lookup_symbol(name, __start___ksymtab_unused,
217 __stop___ksymtab_unused);
219 printk_unused_warning(name);
220 *crc = symversion(__start___kcrctab_unused,
221 (ks - __start___ksymtab_unused));
226 ks = lookup_symbol(name, __start___ksymtab_unused_gpl,
227 __stop___ksymtab_unused_gpl);
229 printk_unused_warning(name);
230 *crc = symversion(__start___kcrctab_unused_gpl,
231 (ks - __start___ksymtab_unused_gpl));
235 /* Now try modules. */
236 list_for_each_entry(mod, &modules, list) {
238 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
240 *crc = symversion(mod->crcs, (ks - mod->syms));
245 ks = lookup_symbol(name, mod->gpl_syms,
246 mod->gpl_syms + mod->num_gpl_syms);
248 *crc = symversion(mod->gpl_crcs,
249 (ks - mod->gpl_syms));
253 ks = lookup_symbol(name, mod->unused_syms, mod->unused_syms + mod->num_unused_syms);
255 printk_unused_warning(name);
256 *crc = symversion(mod->unused_crcs, (ks - mod->unused_syms));
261 ks = lookup_symbol(name, mod->unused_gpl_syms,
262 mod->unused_gpl_syms + mod->num_unused_gpl_syms);
264 printk_unused_warning(name);
265 *crc = symversion(mod->unused_gpl_crcs,
266 (ks - mod->unused_gpl_syms));
270 ks = lookup_symbol(name, mod->gpl_future_syms,
271 (mod->gpl_future_syms +
272 mod->num_gpl_future_syms));
275 printk(KERN_WARNING "Symbol %s is being used "
276 "by a non-GPL module, which will not "
277 "be allowed in the future\n", name);
278 printk(KERN_WARNING "Please see the file "
279 "Documentation/feature-removal-schedule.txt "
280 "in the kernel source tree for more "
283 *crc = symversion(mod->gpl_future_crcs,
284 (ks - mod->gpl_future_syms));
288 DEBUGP("Failed to find symbol %s\n", name);
292 /* Search for module by name: must hold module_mutex. */
293 static struct module *find_module(const char *name)
297 list_for_each_entry(mod, &modules, list) {
298 if (strcmp(mod->name, name) == 0)
305 /* Number of blocks used and allocated. */
306 static unsigned int pcpu_num_used, pcpu_num_allocated;
307 /* Size of each block. -ve means used. */
308 static int *pcpu_size;
310 static int split_block(unsigned int i, unsigned short size)
312 /* Reallocation required? */
313 if (pcpu_num_used + 1 > pcpu_num_allocated) {
316 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
321 pcpu_num_allocated *= 2;
325 /* Insert a new subblock */
326 memmove(&pcpu_size[i+1], &pcpu_size[i],
327 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
330 pcpu_size[i+1] -= size;
335 static inline unsigned int block_size(int val)
342 /* Created by linker magic */
343 extern char __per_cpu_start[], __per_cpu_end[];
345 static void *percpu_modalloc(unsigned long size, unsigned long align,
352 if (align > PAGE_SIZE) {
353 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
354 name, align, PAGE_SIZE);
358 ptr = __per_cpu_start;
359 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
360 /* Extra for alignment requirement. */
361 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
362 BUG_ON(i == 0 && extra != 0);
364 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
367 /* Transfer extra to previous block. */
368 if (pcpu_size[i-1] < 0)
369 pcpu_size[i-1] -= extra;
371 pcpu_size[i-1] += extra;
372 pcpu_size[i] -= extra;
375 /* Split block if warranted */
376 if (pcpu_size[i] - size > sizeof(unsigned long))
377 if (!split_block(i, size))
381 pcpu_size[i] = -pcpu_size[i];
385 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
390 static void percpu_modfree(void *freeme)
393 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
395 /* First entry is core kernel percpu data. */
396 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
398 pcpu_size[i] = -pcpu_size[i];
405 /* Merge with previous? */
406 if (pcpu_size[i-1] >= 0) {
407 pcpu_size[i-1] += pcpu_size[i];
409 memmove(&pcpu_size[i], &pcpu_size[i+1],
410 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
413 /* Merge with next? */
414 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
415 pcpu_size[i] += pcpu_size[i+1];
417 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
418 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
422 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
424 const char *secstrings)
426 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
429 static int percpu_modinit(void)
432 pcpu_num_allocated = 2;
433 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
435 /* Static in-kernel percpu data (used). */
436 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
438 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
439 if (pcpu_size[1] < 0) {
440 printk(KERN_ERR "No per-cpu room for modules.\n");
446 __initcall(percpu_modinit);
447 #else /* ... !CONFIG_SMP */
448 static inline void *percpu_modalloc(unsigned long size, unsigned long align,
453 static inline void percpu_modfree(void *pcpuptr)
457 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
459 const char *secstrings)
463 static inline void percpu_modcopy(void *pcpudst, const void *src,
466 /* pcpusec should be 0, and size of that section should be 0. */
469 #endif /* CONFIG_SMP */
471 #define MODINFO_ATTR(field) \
472 static void setup_modinfo_##field(struct module *mod, const char *s) \
474 mod->field = kstrdup(s, GFP_KERNEL); \
476 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
477 struct module *mod, char *buffer) \
479 return sprintf(buffer, "%s\n", mod->field); \
481 static int modinfo_##field##_exists(struct module *mod) \
483 return mod->field != NULL; \
485 static void free_modinfo_##field(struct module *mod) \
490 static struct module_attribute modinfo_##field = { \
491 .attr = { .name = __stringify(field), .mode = 0444 }, \
492 .show = show_modinfo_##field, \
493 .setup = setup_modinfo_##field, \
494 .test = modinfo_##field##_exists, \
495 .free = free_modinfo_##field, \
498 MODINFO_ATTR(version);
499 MODINFO_ATTR(srcversion);
501 #ifdef CONFIG_MODULE_UNLOAD
502 /* Init the unload section of the module. */
503 static void module_unload_init(struct module *mod)
507 INIT_LIST_HEAD(&mod->modules_which_use_me);
508 for (i = 0; i < NR_CPUS; i++)
509 local_set(&mod->ref[i].count, 0);
510 /* Hold reference count during initialization. */
511 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
512 /* Backwards compatibility macros put refcount during init. */
513 mod->waiter = current;
516 /* modules using other modules */
519 struct list_head list;
520 struct module *module_which_uses;
523 /* Does a already use b? */
524 static int already_uses(struct module *a, struct module *b)
526 struct module_use *use;
528 list_for_each_entry(use, &b->modules_which_use_me, list) {
529 if (use->module_which_uses == a) {
530 DEBUGP("%s uses %s!\n", a->name, b->name);
534 DEBUGP("%s does not use %s!\n", a->name, b->name);
538 /* Module a uses b */
539 static int use_module(struct module *a, struct module *b)
541 struct module_use *use;
544 if (b == NULL || already_uses(a, b)) return 1;
546 if (!strong_try_module_get(b))
549 DEBUGP("Allocating new usage for %s.\n", a->name);
550 use = kmalloc(sizeof(*use), GFP_ATOMIC);
552 printk("%s: out of memory loading\n", a->name);
557 use->module_which_uses = a;
558 list_add(&use->list, &b->modules_which_use_me);
559 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
563 /* Clear the unload stuff of the module. */
564 static void module_unload_free(struct module *mod)
568 list_for_each_entry(i, &modules, list) {
569 struct module_use *use;
571 list_for_each_entry(use, &i->modules_which_use_me, list) {
572 if (use->module_which_uses == mod) {
573 DEBUGP("%s unusing %s\n", mod->name, i->name);
575 list_del(&use->list);
577 sysfs_remove_link(i->holders_dir, mod->name);
578 /* There can be at most one match. */
585 #ifdef CONFIG_MODULE_FORCE_UNLOAD
586 static inline int try_force_unload(unsigned int flags)
588 int ret = (flags & O_TRUNC);
590 add_taint(TAINT_FORCED_RMMOD);
594 static inline int try_force_unload(unsigned int flags)
598 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
607 /* Whole machine is stopped with interrupts off when this runs. */
608 static int __try_stop_module(void *_sref)
610 struct stopref *sref = _sref;
612 /* If it's not unused, quit unless we are told to block. */
613 if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
614 if (!(*sref->forced = try_force_unload(sref->flags)))
618 /* Mark it as dying. */
619 sref->mod->state = MODULE_STATE_GOING;
623 static int try_stop_module(struct module *mod, int flags, int *forced)
625 struct stopref sref = { mod, flags, forced };
627 return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
630 unsigned int module_refcount(struct module *mod)
632 unsigned int i, total = 0;
634 for (i = 0; i < NR_CPUS; i++)
635 total += local_read(&mod->ref[i].count);
638 EXPORT_SYMBOL(module_refcount);
640 /* This exists whether we can unload or not */
641 static void free_module(struct module *mod);
643 static void wait_for_zero_refcount(struct module *mod)
645 /* Since we might sleep for some time, drop the semaphore first */
646 mutex_unlock(&module_mutex);
648 DEBUGP("Looking at refcount...\n");
649 set_current_state(TASK_UNINTERRUPTIBLE);
650 if (module_refcount(mod) == 0)
654 current->state = TASK_RUNNING;
655 mutex_lock(&module_mutex);
659 sys_delete_module(const char __user *name_user, unsigned int flags)
662 char name[MODULE_NAME_LEN];
665 if (!capable(CAP_SYS_MODULE))
668 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
670 name[MODULE_NAME_LEN-1] = '\0';
672 if (mutex_lock_interruptible(&module_mutex) != 0)
675 mod = find_module(name);
681 if (!list_empty(&mod->modules_which_use_me)) {
682 /* Other modules depend on us: get rid of them first. */
687 /* Doing init or already dying? */
688 if (mod->state != MODULE_STATE_LIVE) {
689 /* FIXME: if (force), slam module count and wake up
691 DEBUGP("%s already dying\n", mod->name);
696 /* If it has an init func, it must have an exit func to unload */
697 if (mod->init && !mod->exit) {
698 forced = try_force_unload(flags);
700 /* This module can't be removed */
706 /* Set this up before setting mod->state */
707 mod->waiter = current;
709 /* Stop the machine so refcounts can't move and disable module. */
710 ret = try_stop_module(mod, flags, &forced);
714 /* Never wait if forced. */
715 if (!forced && module_refcount(mod) != 0)
716 wait_for_zero_refcount(mod);
718 /* Final destruction now noone is using it. */
719 if (mod->exit != NULL) {
720 mutex_unlock(&module_mutex);
722 mutex_lock(&module_mutex);
727 mutex_unlock(&module_mutex);
731 static void print_unload_info(struct seq_file *m, struct module *mod)
733 struct module_use *use;
734 int printed_something = 0;
736 seq_printf(m, " %u ", module_refcount(mod));
738 /* Always include a trailing , so userspace can differentiate
739 between this and the old multi-field proc format. */
740 list_for_each_entry(use, &mod->modules_which_use_me, list) {
741 printed_something = 1;
742 seq_printf(m, "%s,", use->module_which_uses->name);
745 if (mod->init != NULL && mod->exit == NULL) {
746 printed_something = 1;
747 seq_printf(m, "[permanent],");
750 if (!printed_something)
754 void __symbol_put(const char *symbol)
756 struct module *owner;
757 const unsigned long *crc;
760 if (!__find_symbol(symbol, &owner, &crc, 1))
765 EXPORT_SYMBOL(__symbol_put);
767 void symbol_put_addr(void *addr)
769 struct module *modaddr;
771 if (core_kernel_text((unsigned long)addr))
774 if (!(modaddr = module_text_address((unsigned long)addr)))
778 EXPORT_SYMBOL_GPL(symbol_put_addr);
780 static ssize_t show_refcnt(struct module_attribute *mattr,
781 struct module *mod, char *buffer)
783 return sprintf(buffer, "%u\n", module_refcount(mod));
786 static struct module_attribute refcnt = {
787 .attr = { .name = "refcnt", .mode = 0444 },
791 void module_put(struct module *module)
794 unsigned int cpu = get_cpu();
795 local_dec(&module->ref[cpu].count);
796 /* Maybe they're waiting for us to drop reference? */
797 if (unlikely(!module_is_live(module)))
798 wake_up_process(module->waiter);
802 EXPORT_SYMBOL(module_put);
804 #else /* !CONFIG_MODULE_UNLOAD */
805 static void print_unload_info(struct seq_file *m, struct module *mod)
807 /* We don't know the usage count, or what modules are using. */
808 seq_printf(m, " - -");
811 static inline void module_unload_free(struct module *mod)
815 static inline int use_module(struct module *a, struct module *b)
817 return strong_try_module_get(b);
820 static inline void module_unload_init(struct module *mod)
823 #endif /* CONFIG_MODULE_UNLOAD */
825 static ssize_t show_initstate(struct module_attribute *mattr,
826 struct module *mod, char *buffer)
828 const char *state = "unknown";
830 switch (mod->state) {
831 case MODULE_STATE_LIVE:
834 case MODULE_STATE_COMING:
837 case MODULE_STATE_GOING:
841 return sprintf(buffer, "%s\n", state);
844 static struct module_attribute initstate = {
845 .attr = { .name = "initstate", .mode = 0444 },
846 .show = show_initstate,
849 static struct module_attribute *modinfo_attrs[] = {
853 #ifdef CONFIG_MODULE_UNLOAD
859 static const char vermagic[] = VERMAGIC_STRING;
861 #ifdef CONFIG_MODVERSIONS
862 static int check_version(Elf_Shdr *sechdrs,
863 unsigned int versindex,
866 const unsigned long *crc)
868 unsigned int i, num_versions;
869 struct modversion_info *versions;
871 /* Exporting module didn't supply crcs? OK, we're already tainted. */
875 versions = (void *) sechdrs[versindex].sh_addr;
876 num_versions = sechdrs[versindex].sh_size
877 / sizeof(struct modversion_info);
879 for (i = 0; i < num_versions; i++) {
880 if (strcmp(versions[i].name, symname) != 0)
883 if (versions[i].crc == *crc)
885 printk("%s: disagrees about version of symbol %s\n",
887 DEBUGP("Found checksum %lX vs module %lX\n",
888 *crc, versions[i].crc);
891 /* Not in module's version table. OK, but that taints the kernel. */
892 if (!(tainted & TAINT_FORCED_MODULE))
893 printk("%s: no version for \"%s\" found: kernel tainted.\n",
895 add_taint_module(mod, TAINT_FORCED_MODULE);
899 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
900 unsigned int versindex,
903 const unsigned long *crc;
904 struct module *owner;
906 if (!__find_symbol("struct_module", &owner, &crc, 1))
908 return check_version(sechdrs, versindex, "struct_module", mod,
912 /* First part is kernel version, which we ignore. */
913 static inline int same_magic(const char *amagic, const char *bmagic)
915 amagic += strcspn(amagic, " ");
916 bmagic += strcspn(bmagic, " ");
917 return strcmp(amagic, bmagic) == 0;
920 static inline int check_version(Elf_Shdr *sechdrs,
921 unsigned int versindex,
924 const unsigned long *crc)
929 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
930 unsigned int versindex,
936 static inline int same_magic(const char *amagic, const char *bmagic)
938 return strcmp(amagic, bmagic) == 0;
940 #endif /* CONFIG_MODVERSIONS */
942 /* Resolve a symbol for this module. I.e. if we find one, record usage.
943 Must be holding module_mutex. */
944 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
945 unsigned int versindex,
949 struct module *owner;
951 const unsigned long *crc;
953 ret = __find_symbol(name, &owner, &crc,
954 !(mod->taints & TAINT_PROPRIETARY_MODULE));
956 /* use_module can fail due to OOM,
957 or module initialization or unloading */
958 if (!check_version(sechdrs, versindex, name, mod, crc) ||
959 !use_module(mod, owner))
967 * /sys/module/foo/sections stuff
968 * J. Corbet <corbet@lwn.net>
970 #ifdef CONFIG_KALLSYMS
971 static ssize_t module_sect_show(struct module_attribute *mattr,
972 struct module *mod, char *buf)
974 struct module_sect_attr *sattr =
975 container_of(mattr, struct module_sect_attr, mattr);
976 return sprintf(buf, "0x%lx\n", sattr->address);
979 static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
983 for (section = 0; section < sect_attrs->nsections; section++)
984 kfree(sect_attrs->attrs[section].name);
988 static void add_sect_attrs(struct module *mod, unsigned int nsect,
989 char *secstrings, Elf_Shdr *sechdrs)
991 unsigned int nloaded = 0, i, size[2];
992 struct module_sect_attrs *sect_attrs;
993 struct module_sect_attr *sattr;
994 struct attribute **gattr;
996 /* Count loaded sections and allocate structures */
997 for (i = 0; i < nsect; i++)
998 if (sechdrs[i].sh_flags & SHF_ALLOC)
1000 size[0] = ALIGN(sizeof(*sect_attrs)
1001 + nloaded * sizeof(sect_attrs->attrs[0]),
1002 sizeof(sect_attrs->grp.attrs[0]));
1003 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
1004 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1005 if (sect_attrs == NULL)
1008 /* Setup section attributes. */
1009 sect_attrs->grp.name = "sections";
1010 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1012 sect_attrs->nsections = 0;
1013 sattr = §_attrs->attrs[0];
1014 gattr = §_attrs->grp.attrs[0];
1015 for (i = 0; i < nsect; i++) {
1016 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1018 sattr->address = sechdrs[i].sh_addr;
1019 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1021 if (sattr->name == NULL)
1023 sect_attrs->nsections++;
1024 sattr->mattr.show = module_sect_show;
1025 sattr->mattr.store = NULL;
1026 sattr->mattr.attr.name = sattr->name;
1027 sattr->mattr.attr.mode = S_IRUGO;
1028 *(gattr++) = &(sattr++)->mattr.attr;
1032 if (sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp))
1035 mod->sect_attrs = sect_attrs;
1038 free_sect_attrs(sect_attrs);
1041 static void remove_sect_attrs(struct module *mod)
1043 if (mod->sect_attrs) {
1044 sysfs_remove_group(&mod->mkobj.kobj,
1045 &mod->sect_attrs->grp);
1046 /* We are positive that no one is using any sect attrs
1047 * at this point. Deallocate immediately. */
1048 free_sect_attrs(mod->sect_attrs);
1049 mod->sect_attrs = NULL;
1054 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1057 struct module_notes_attrs {
1058 struct kobject *dir;
1060 struct bin_attribute attrs[0];
1063 static ssize_t module_notes_read(struct kobject *kobj,
1064 struct bin_attribute *bin_attr,
1065 char *buf, loff_t pos, size_t count)
1068 * The caller checked the pos and count against our size.
1070 memcpy(buf, bin_attr->private + pos, count);
1074 static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1077 if (notes_attrs->dir) {
1079 sysfs_remove_bin_file(notes_attrs->dir,
1080 ¬es_attrs->attrs[i]);
1081 kobject_del(notes_attrs->dir);
1086 static void add_notes_attrs(struct module *mod, unsigned int nsect,
1087 char *secstrings, Elf_Shdr *sechdrs)
1089 unsigned int notes, loaded, i;
1090 struct module_notes_attrs *notes_attrs;
1091 struct bin_attribute *nattr;
1093 /* Count notes sections and allocate structures. */
1095 for (i = 0; i < nsect; i++)
1096 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1097 (sechdrs[i].sh_type == SHT_NOTE))
1103 notes_attrs = kzalloc(sizeof(*notes_attrs)
1104 + notes * sizeof(notes_attrs->attrs[0]),
1106 if (notes_attrs == NULL)
1109 notes_attrs->notes = notes;
1110 nattr = ¬es_attrs->attrs[0];
1111 for (loaded = i = 0; i < nsect; ++i) {
1112 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1114 if (sechdrs[i].sh_type == SHT_NOTE) {
1115 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1116 nattr->attr.mode = S_IRUGO;
1117 nattr->size = sechdrs[i].sh_size;
1118 nattr->private = (void *) sechdrs[i].sh_addr;
1119 nattr->read = module_notes_read;
1125 notes_attrs->dir = kobject_add_dir(&mod->mkobj.kobj, "notes");
1126 if (!notes_attrs->dir)
1129 for (i = 0; i < notes; ++i)
1130 if (sysfs_create_bin_file(notes_attrs->dir,
1131 ¬es_attrs->attrs[i]))
1134 mod->notes_attrs = notes_attrs;
1138 free_notes_attrs(notes_attrs, i);
1141 static void remove_notes_attrs(struct module *mod)
1143 if (mod->notes_attrs)
1144 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1149 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1150 char *sectstrings, Elf_Shdr *sechdrs)
1154 static inline void remove_sect_attrs(struct module *mod)
1158 static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1159 char *sectstrings, Elf_Shdr *sechdrs)
1163 static inline void remove_notes_attrs(struct module *mod)
1166 #endif /* CONFIG_KALLSYMS */
1169 int module_add_modinfo_attrs(struct module *mod)
1171 struct module_attribute *attr;
1172 struct module_attribute *temp_attr;
1176 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1177 (ARRAY_SIZE(modinfo_attrs) + 1)),
1179 if (!mod->modinfo_attrs)
1182 temp_attr = mod->modinfo_attrs;
1183 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1185 (attr->test && attr->test(mod))) {
1186 memcpy(temp_attr, attr, sizeof(*temp_attr));
1187 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1194 void module_remove_modinfo_attrs(struct module *mod)
1196 struct module_attribute *attr;
1199 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1200 /* pick a field to test for end of list */
1201 if (!attr->attr.name)
1203 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1207 kfree(mod->modinfo_attrs);
1212 int mod_sysfs_init(struct module *mod)
1216 if (!module_sysfs_initialized) {
1217 printk(KERN_ERR "%s: module sysfs not initialized\n",
1222 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1223 err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name);
1226 kobj_set_kset_s(&mod->mkobj, module_subsys);
1227 mod->mkobj.mod = mod;
1229 kobject_init(&mod->mkobj.kobj);
1235 int mod_sysfs_setup(struct module *mod,
1236 struct kernel_param *kparam,
1237 unsigned int num_params)
1241 /* delay uevent until full sysfs population */
1242 err = kobject_add(&mod->mkobj.kobj);
1246 mod->holders_dir = kobject_add_dir(&mod->mkobj.kobj, "holders");
1247 if (!mod->holders_dir) {
1252 err = module_param_sysfs_setup(mod, kparam, num_params);
1254 goto out_unreg_holders;
1256 err = module_add_modinfo_attrs(mod);
1258 goto out_unreg_param;
1260 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1264 module_param_sysfs_remove(mod);
1266 kobject_unregister(mod->holders_dir);
1268 kobject_del(&mod->mkobj.kobj);
1269 kobject_put(&mod->mkobj.kobj);
1275 static void mod_kobject_remove(struct module *mod)
1277 module_remove_modinfo_attrs(mod);
1278 module_param_sysfs_remove(mod);
1279 kobject_unregister(mod->mkobj.drivers_dir);
1280 kobject_unregister(mod->holders_dir);
1281 kobject_unregister(&mod->mkobj.kobj);
1285 * unlink the module with the whole machine is stopped with interrupts off
1286 * - this defends against kallsyms not taking locks
1288 static int __unlink_module(void *_mod)
1290 struct module *mod = _mod;
1291 list_del(&mod->list);
1295 /* Free a module, remove from lists, etc (must hold module_mutex). */
1296 static void free_module(struct module *mod)
1298 /* Delete from various lists */
1299 stop_machine_run(__unlink_module, mod, NR_CPUS);
1300 remove_notes_attrs(mod);
1301 remove_sect_attrs(mod);
1302 mod_kobject_remove(mod);
1304 unwind_remove_table(mod->unwind_info, 0);
1306 /* Arch-specific cleanup. */
1307 module_arch_cleanup(mod);
1309 /* Module unload stuff */
1310 module_unload_free(mod);
1312 /* This may be NULL, but that's OK */
1313 module_free(mod, mod->module_init);
1316 percpu_modfree(mod->percpu);
1318 /* Free lock-classes: */
1319 lockdep_free_key_range(mod->module_core, mod->core_size);
1321 /* Finally, free the core (containing the module structure) */
1322 module_free(mod, mod->module_core);
1325 void *__symbol_get(const char *symbol)
1327 struct module *owner;
1328 unsigned long value;
1329 const unsigned long *crc;
1332 value = __find_symbol(symbol, &owner, &crc, 1);
1333 if (value && !strong_try_module_get(owner))
1337 return (void *)value;
1339 EXPORT_SYMBOL_GPL(__symbol_get);
1342 * Ensure that an exported symbol [global namespace] does not already exist
1343 * in the kernel or in some other module's exported symbol table.
1345 static int verify_export_symbols(struct module *mod)
1347 const char *name = NULL;
1348 unsigned long i, ret = 0;
1349 struct module *owner;
1350 const unsigned long *crc;
1352 for (i = 0; i < mod->num_syms; i++)
1353 if (__find_symbol(mod->syms[i].name, &owner, &crc, 1)) {
1354 name = mod->syms[i].name;
1359 for (i = 0; i < mod->num_gpl_syms; i++)
1360 if (__find_symbol(mod->gpl_syms[i].name, &owner, &crc, 1)) {
1361 name = mod->gpl_syms[i].name;
1368 printk(KERN_ERR "%s: exports duplicate symbol %s (owned by %s)\n",
1369 mod->name, name, module_name(owner));
1374 /* Change all symbols so that st_value encodes the pointer directly. */
1375 static int simplify_symbols(Elf_Shdr *sechdrs,
1376 unsigned int symindex,
1378 unsigned int versindex,
1379 unsigned int pcpuindex,
1382 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1383 unsigned long secbase;
1384 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1387 for (i = 1; i < n; i++) {
1388 switch (sym[i].st_shndx) {
1390 /* We compiled with -fno-common. These are not
1391 supposed to happen. */
1392 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1393 printk("%s: please compile with -fno-common\n",
1399 /* Don't need to do anything */
1400 DEBUGP("Absolute symbol: 0x%08lx\n",
1401 (long)sym[i].st_value);
1406 = resolve_symbol(sechdrs, versindex,
1407 strtab + sym[i].st_name, mod);
1409 /* Ok if resolved. */
1410 if (sym[i].st_value != 0)
1413 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1416 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1417 mod->name, strtab + sym[i].st_name);
1422 /* Divert to percpu allocation if a percpu var. */
1423 if (sym[i].st_shndx == pcpuindex)
1424 secbase = (unsigned long)mod->percpu;
1426 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1427 sym[i].st_value += secbase;
1435 /* Update size with this section: return offset. */
1436 static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1440 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1441 *size = ret + sechdr->sh_size;
1445 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1446 might -- code, read-only data, read-write data, small data. Tally
1447 sizes, and place the offsets into sh_entsize fields: high bit means it
1449 static void layout_sections(struct module *mod,
1450 const Elf_Ehdr *hdr,
1452 const char *secstrings)
1454 static unsigned long const masks[][2] = {
1455 /* NOTE: all executable code must be the first section
1456 * in this array; otherwise modify the text_size
1457 * finder in the two loops below */
1458 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1459 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1460 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1461 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1465 for (i = 0; i < hdr->e_shnum; i++)
1466 sechdrs[i].sh_entsize = ~0UL;
1468 DEBUGP("Core section allocation order:\n");
1469 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1470 for (i = 0; i < hdr->e_shnum; ++i) {
1471 Elf_Shdr *s = &sechdrs[i];
1473 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1474 || (s->sh_flags & masks[m][1])
1475 || s->sh_entsize != ~0UL
1476 || strncmp(secstrings + s->sh_name,
1479 s->sh_entsize = get_offset(&mod->core_size, s);
1480 DEBUGP("\t%s\n", secstrings + s->sh_name);
1483 mod->core_text_size = mod->core_size;
1486 DEBUGP("Init section allocation order:\n");
1487 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1488 for (i = 0; i < hdr->e_shnum; ++i) {
1489 Elf_Shdr *s = &sechdrs[i];
1491 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1492 || (s->sh_flags & masks[m][1])
1493 || s->sh_entsize != ~0UL
1494 || strncmp(secstrings + s->sh_name,
1497 s->sh_entsize = (get_offset(&mod->init_size, s)
1498 | INIT_OFFSET_MASK);
1499 DEBUGP("\t%s\n", secstrings + s->sh_name);
1502 mod->init_text_size = mod->init_size;
1506 static void set_license(struct module *mod, const char *license)
1509 license = "unspecified";
1511 if (!license_is_gpl_compatible(license)) {
1512 if (!(tainted & TAINT_PROPRIETARY_MODULE))
1513 printk(KERN_WARNING "%s: module license '%s' taints "
1514 "kernel.\n", mod->name, license);
1515 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
1519 /* Parse tag=value strings from .modinfo section */
1520 static char *next_string(char *string, unsigned long *secsize)
1522 /* Skip non-zero chars */
1525 if ((*secsize)-- <= 1)
1529 /* Skip any zero padding. */
1530 while (!string[0]) {
1532 if ((*secsize)-- <= 1)
1538 static char *get_modinfo(Elf_Shdr *sechdrs,
1543 unsigned int taglen = strlen(tag);
1544 unsigned long size = sechdrs[info].sh_size;
1546 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1547 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1548 return p + taglen + 1;
1553 static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1554 unsigned int infoindex)
1556 struct module_attribute *attr;
1559 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1562 get_modinfo(sechdrs,
1568 #ifdef CONFIG_KALLSYMS
1569 static int is_exported(const char *name, const struct module *mod)
1571 if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
1574 if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
1581 static char elf_type(const Elf_Sym *sym,
1583 const char *secstrings,
1586 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1587 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1592 if (sym->st_shndx == SHN_UNDEF)
1594 if (sym->st_shndx == SHN_ABS)
1596 if (sym->st_shndx >= SHN_LORESERVE)
1598 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1600 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1601 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1602 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1604 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1609 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1610 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1615 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1616 ".debug", strlen(".debug")) == 0)
1621 static void add_kallsyms(struct module *mod,
1623 unsigned int symindex,
1624 unsigned int strindex,
1625 const char *secstrings)
1629 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1630 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1631 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1633 /* Set types up while we still have access to sections. */
1634 for (i = 0; i < mod->num_symtab; i++)
1635 mod->symtab[i].st_info
1636 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1639 static inline void add_kallsyms(struct module *mod,
1641 unsigned int symindex,
1642 unsigned int strindex,
1643 const char *secstrings)
1646 #endif /* CONFIG_KALLSYMS */
1648 /* Allocate and load the module: note that size of section 0 is always
1649 zero, and we rely on this for optional sections. */
1650 static struct module *load_module(void __user *umod,
1652 const char __user *uargs)
1656 char *secstrings, *args, *modmagic, *strtab = NULL;
1658 unsigned int symindex = 0;
1659 unsigned int strindex = 0;
1660 unsigned int setupindex;
1661 unsigned int exindex;
1662 unsigned int exportindex;
1663 unsigned int modindex;
1664 unsigned int obsparmindex;
1665 unsigned int infoindex;
1666 unsigned int gplindex;
1667 unsigned int crcindex;
1668 unsigned int gplcrcindex;
1669 unsigned int versindex;
1670 unsigned int pcpuindex;
1671 unsigned int gplfutureindex;
1672 unsigned int gplfuturecrcindex;
1673 unsigned int unwindex = 0;
1674 unsigned int unusedindex;
1675 unsigned int unusedcrcindex;
1676 unsigned int unusedgplindex;
1677 unsigned int unusedgplcrcindex;
1678 unsigned int markersindex;
1679 unsigned int markersstringsindex;
1682 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1683 struct exception_table_entry *extable;
1684 mm_segment_t old_fs;
1686 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1688 if (len < sizeof(*hdr))
1689 return ERR_PTR(-ENOEXEC);
1691 /* Suck in entire file: we'll want most of it. */
1692 /* vmalloc barfs on "unusual" numbers. Check here */
1693 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1694 return ERR_PTR(-ENOMEM);
1695 if (copy_from_user(hdr, umod, len) != 0) {
1700 /* Sanity checks against insmoding binaries or wrong arch,
1701 weird elf version */
1702 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1703 || hdr->e_type != ET_REL
1704 || !elf_check_arch(hdr)
1705 || hdr->e_shentsize != sizeof(*sechdrs)) {
1710 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1713 /* Convenience variables */
1714 sechdrs = (void *)hdr + hdr->e_shoff;
1715 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1716 sechdrs[0].sh_addr = 0;
1718 for (i = 1; i < hdr->e_shnum; i++) {
1719 if (sechdrs[i].sh_type != SHT_NOBITS
1720 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1723 /* Mark all sections sh_addr with their address in the
1725 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1727 /* Internal symbols and strings. */
1728 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1730 strindex = sechdrs[i].sh_link;
1731 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1733 #ifndef CONFIG_MODULE_UNLOAD
1734 /* Don't load .exit sections */
1735 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1736 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1740 modindex = find_sec(hdr, sechdrs, secstrings,
1741 ".gnu.linkonce.this_module");
1743 printk(KERN_WARNING "No module found in object\n");
1747 mod = (void *)sechdrs[modindex].sh_addr;
1749 if (symindex == 0) {
1750 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1756 /* Optional sections */
1757 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1758 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
1759 gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future");
1760 unusedindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused");
1761 unusedgplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused_gpl");
1762 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1763 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
1764 gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future");
1765 unusedcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused");
1766 unusedgplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused_gpl");
1767 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1768 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1769 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1770 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1771 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1772 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1773 #ifdef ARCH_UNWIND_SECTION_NAME
1774 unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
1777 /* Don't keep modinfo section */
1778 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1779 #ifdef CONFIG_KALLSYMS
1780 /* Keep symbol and string tables for decoding later. */
1781 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1782 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1785 sechdrs[unwindex].sh_flags |= SHF_ALLOC;
1787 /* Check module struct version now, before we try to use module. */
1788 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1793 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1794 /* This is allowed: modprobe --force will invalidate it. */
1796 add_taint_module(mod, TAINT_FORCED_MODULE);
1797 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1799 } else if (!same_magic(modmagic, vermagic)) {
1800 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1801 mod->name, modmagic, vermagic);
1806 /* Now copy in args */
1807 args = strndup_user(uargs, ~0UL >> 1);
1809 err = PTR_ERR(args);
1813 if (find_module(mod->name)) {
1818 mod->state = MODULE_STATE_COMING;
1820 /* Allow arches to frob section contents and sizes. */
1821 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1826 /* We have a special allocation for this section. */
1827 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
1828 sechdrs[pcpuindex].sh_addralign,
1834 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1835 mod->percpu = percpu;
1838 /* Determine total sizes, and put offsets in sh_entsize. For now
1839 this is done generically; there doesn't appear to be any
1840 special cases for the architectures. */
1841 layout_sections(mod, hdr, sechdrs, secstrings);
1843 /* Do the allocs. */
1844 ptr = module_alloc(mod->core_size);
1849 memset(ptr, 0, mod->core_size);
1850 mod->module_core = ptr;
1852 ptr = module_alloc(mod->init_size);
1853 if (!ptr && mod->init_size) {
1857 memset(ptr, 0, mod->init_size);
1858 mod->module_init = ptr;
1860 /* Transfer each section which specifies SHF_ALLOC */
1861 DEBUGP("final section addresses:\n");
1862 for (i = 0; i < hdr->e_shnum; i++) {
1865 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1868 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1869 dest = mod->module_init
1870 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1872 dest = mod->module_core + sechdrs[i].sh_entsize;
1874 if (sechdrs[i].sh_type != SHT_NOBITS)
1875 memcpy(dest, (void *)sechdrs[i].sh_addr,
1876 sechdrs[i].sh_size);
1877 /* Update sh_addr to point to copy in image. */
1878 sechdrs[i].sh_addr = (unsigned long)dest;
1879 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1881 /* Module has been moved. */
1882 mod = (void *)sechdrs[modindex].sh_addr;
1884 /* Now we've moved module, initialize linked lists, etc. */
1885 module_unload_init(mod);
1887 /* Initialize kobject, so we can reference it. */
1888 err = mod_sysfs_init(mod);
1892 /* Set up license info based on the info section */
1893 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1895 if (strcmp(mod->name, "ndiswrapper") == 0)
1896 add_taint(TAINT_PROPRIETARY_MODULE);
1897 if (strcmp(mod->name, "driverloader") == 0)
1898 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
1900 /* Set up MODINFO_ATTR fields */
1901 setup_modinfo(mod, sechdrs, infoindex);
1903 /* Fix up syms, so that st_value is a pointer to location. */
1904 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
1909 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1910 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1911 mod->syms = (void *)sechdrs[exportindex].sh_addr;
1913 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1914 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1915 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1917 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
1918 mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size /
1919 sizeof(*mod->gpl_future_syms);
1920 mod->num_unused_syms = sechdrs[unusedindex].sh_size /
1921 sizeof(*mod->unused_syms);
1922 mod->num_unused_gpl_syms = sechdrs[unusedgplindex].sh_size /
1923 sizeof(*mod->unused_gpl_syms);
1924 mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr;
1925 if (gplfuturecrcindex)
1926 mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr;
1928 mod->unused_syms = (void *)sechdrs[unusedindex].sh_addr;
1930 mod->unused_crcs = (void *)sechdrs[unusedcrcindex].sh_addr;
1931 mod->unused_gpl_syms = (void *)sechdrs[unusedgplindex].sh_addr;
1932 if (unusedgplcrcindex)
1933 mod->unused_crcs = (void *)sechdrs[unusedgplcrcindex].sh_addr;
1935 #ifdef CONFIG_MODVERSIONS
1936 if ((mod->num_syms && !crcindex) ||
1937 (mod->num_gpl_syms && !gplcrcindex) ||
1938 (mod->num_gpl_future_syms && !gplfuturecrcindex) ||
1939 (mod->num_unused_syms && !unusedcrcindex) ||
1940 (mod->num_unused_gpl_syms && !unusedgplcrcindex)) {
1941 printk(KERN_WARNING "%s: No versions for exported symbols."
1942 " Tainting kernel.\n", mod->name);
1943 add_taint_module(mod, TAINT_FORCED_MODULE);
1946 markersindex = find_sec(hdr, sechdrs, secstrings, "__markers");
1947 markersstringsindex = find_sec(hdr, sechdrs, secstrings,
1948 "__markers_strings");
1950 /* Now do relocations. */
1951 for (i = 1; i < hdr->e_shnum; i++) {
1952 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1953 unsigned int info = sechdrs[i].sh_info;
1955 /* Not a valid relocation section? */
1956 if (info >= hdr->e_shnum)
1959 /* Don't bother with non-allocated sections */
1960 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
1963 if (sechdrs[i].sh_type == SHT_REL)
1964 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1965 else if (sechdrs[i].sh_type == SHT_RELA)
1966 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1971 #ifdef CONFIG_MARKERS
1972 mod->markers = (void *)sechdrs[markersindex].sh_addr;
1974 sechdrs[markersindex].sh_size / sizeof(*mod->markers);
1977 /* Find duplicate symbols */
1978 err = verify_export_symbols(mod);
1983 /* Set up and sort exception table */
1984 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1985 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
1986 sort_extable(extable, extable + mod->num_exentries);
1988 /* Finally, copy percpu area over. */
1989 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
1990 sechdrs[pcpuindex].sh_size);
1992 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
1994 #ifdef CONFIG_MARKERS
1996 marker_update_probe_range(mod->markers,
1997 mod->markers + mod->num_markers, NULL, NULL);
1999 err = module_finalize(hdr, sechdrs, mod);
2003 /* flush the icache in correct context */
2008 * Flush the instruction cache, since we've played with text.
2009 * Do it before processing of module parameters, so the module
2010 * can provide parameter accessor functions of its own.
2012 if (mod->module_init)
2013 flush_icache_range((unsigned long)mod->module_init,
2014 (unsigned long)mod->module_init
2016 flush_icache_range((unsigned long)mod->module_core,
2017 (unsigned long)mod->module_core + mod->core_size);
2023 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2026 /* Size of section 0 is 0, so this works well if no params */
2027 err = parse_args(mod->name, mod->args,
2028 (struct kernel_param *)
2029 sechdrs[setupindex].sh_addr,
2030 sechdrs[setupindex].sh_size
2031 / sizeof(struct kernel_param),
2036 err = mod_sysfs_setup(mod,
2037 (struct kernel_param *)
2038 sechdrs[setupindex].sh_addr,
2039 sechdrs[setupindex].sh_size
2040 / sizeof(struct kernel_param));
2043 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2044 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2046 /* Size of section 0 is 0, so this works well if no unwind info. */
2047 mod->unwind_info = unwind_add_table(mod,
2048 (void *)sechdrs[unwindex].sh_addr,
2049 sechdrs[unwindex].sh_size);
2051 /* Get rid of temporary copy */
2058 module_arch_cleanup(mod);
2060 module_unload_free(mod);
2061 module_free(mod, mod->module_init);
2063 module_free(mod, mod->module_core);
2066 percpu_modfree(percpu);
2071 return ERR_PTR(err);
2074 printk(KERN_ERR "Module len %lu truncated\n", len);
2080 * link the module with the whole machine is stopped with interrupts off
2081 * - this defends against kallsyms not taking locks
2083 static int __link_module(void *_mod)
2085 struct module *mod = _mod;
2086 list_add(&mod->list, &modules);
2090 /* This is where the real work happens */
2092 sys_init_module(void __user *umod,
2094 const char __user *uargs)
2099 /* Must have permission */
2100 if (!capable(CAP_SYS_MODULE))
2103 /* Only one module load at a time, please */
2104 if (mutex_lock_interruptible(&module_mutex) != 0)
2107 /* Do all the hard work */
2108 mod = load_module(umod, len, uargs);
2110 mutex_unlock(&module_mutex);
2111 return PTR_ERR(mod);
2114 /* Now sew it into the lists. They won't access us, since
2115 strong_try_module_get() will fail. */
2116 stop_machine_run(__link_module, mod, NR_CPUS);
2118 /* Drop lock so they can recurse */
2119 mutex_unlock(&module_mutex);
2121 blocking_notifier_call_chain(&module_notify_list,
2122 MODULE_STATE_COMING, mod);
2124 /* Start the module */
2125 if (mod->init != NULL)
2128 /* Init routine failed: abort. Try to protect us from
2129 buggy refcounters. */
2130 mod->state = MODULE_STATE_GOING;
2131 synchronize_sched();
2133 mutex_lock(&module_mutex);
2135 mutex_unlock(&module_mutex);
2139 /* Now it's a first class citizen! */
2140 mutex_lock(&module_mutex);
2141 mod->state = MODULE_STATE_LIVE;
2142 /* Drop initial reference. */
2144 unwind_remove_table(mod->unwind_info, 1);
2145 module_free(mod, mod->module_init);
2146 mod->module_init = NULL;
2148 mod->init_text_size = 0;
2149 mutex_unlock(&module_mutex);
2154 static inline int within(unsigned long addr, void *start, unsigned long size)
2156 return ((void *)addr >= start && (void *)addr < start + size);
2159 #ifdef CONFIG_KALLSYMS
2161 * This ignores the intensely annoying "mapping symbols" found
2162 * in ARM ELF files: $a, $t and $d.
2164 static inline int is_arm_mapping_symbol(const char *str)
2166 return str[0] == '$' && strchr("atd", str[1])
2167 && (str[2] == '\0' || str[2] == '.');
2170 static const char *get_ksymbol(struct module *mod,
2172 unsigned long *size,
2173 unsigned long *offset)
2175 unsigned int i, best = 0;
2176 unsigned long nextval;
2178 /* At worse, next value is at end of module */
2179 if (within(addr, mod->module_init, mod->init_size))
2180 nextval = (unsigned long)mod->module_init+mod->init_text_size;
2182 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2184 /* Scan for closest preceeding symbol, and next symbol. (ELF
2185 starts real symbols at 1). */
2186 for (i = 1; i < mod->num_symtab; i++) {
2187 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2190 /* We ignore unnamed symbols: they're uninformative
2191 * and inserted at a whim. */
2192 if (mod->symtab[i].st_value <= addr
2193 && mod->symtab[i].st_value > mod->symtab[best].st_value
2194 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2195 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2197 if (mod->symtab[i].st_value > addr
2198 && mod->symtab[i].st_value < nextval
2199 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2200 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2201 nextval = mod->symtab[i].st_value;
2208 *size = nextval - mod->symtab[best].st_value;
2210 *offset = addr - mod->symtab[best].st_value;
2211 return mod->strtab + mod->symtab[best].st_name;
2214 /* For kallsyms to ask for address resolution. NULL means not found.
2215 We don't lock, as this is used for oops resolution and races are a
2217 const char *module_address_lookup(unsigned long addr,
2218 unsigned long *size,
2219 unsigned long *offset,
2224 list_for_each_entry(mod, &modules, list) {
2225 if (within(addr, mod->module_init, mod->init_size)
2226 || within(addr, mod->module_core, mod->core_size)) {
2228 *modname = mod->name;
2229 return get_ksymbol(mod, addr, size, offset);
2235 int lookup_module_symbol_name(unsigned long addr, char *symname)
2239 mutex_lock(&module_mutex);
2240 list_for_each_entry(mod, &modules, list) {
2241 if (within(addr, mod->module_init, mod->init_size) ||
2242 within(addr, mod->module_core, mod->core_size)) {
2245 sym = get_ksymbol(mod, addr, NULL, NULL);
2248 strlcpy(symname, sym, KSYM_NAME_LEN);
2249 mutex_unlock(&module_mutex);
2254 mutex_unlock(&module_mutex);
2258 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2259 unsigned long *offset, char *modname, char *name)
2263 mutex_lock(&module_mutex);
2264 list_for_each_entry(mod, &modules, list) {
2265 if (within(addr, mod->module_init, mod->init_size) ||
2266 within(addr, mod->module_core, mod->core_size)) {
2269 sym = get_ksymbol(mod, addr, size, offset);
2273 strlcpy(modname, mod->name, MODULE_NAME_LEN);
2275 strlcpy(name, sym, KSYM_NAME_LEN);
2276 mutex_unlock(&module_mutex);
2281 mutex_unlock(&module_mutex);
2285 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2286 char *name, char *module_name, int *exported)
2290 mutex_lock(&module_mutex);
2291 list_for_each_entry(mod, &modules, list) {
2292 if (symnum < mod->num_symtab) {
2293 *value = mod->symtab[symnum].st_value;
2294 *type = mod->symtab[symnum].st_info;
2295 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
2297 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
2298 *exported = is_exported(name, mod);
2299 mutex_unlock(&module_mutex);
2302 symnum -= mod->num_symtab;
2304 mutex_unlock(&module_mutex);
2308 static unsigned long mod_find_symname(struct module *mod, const char *name)
2312 for (i = 0; i < mod->num_symtab; i++)
2313 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2314 mod->symtab[i].st_info != 'U')
2315 return mod->symtab[i].st_value;
2319 /* Look for this name: can be of form module:name. */
2320 unsigned long module_kallsyms_lookup_name(const char *name)
2324 unsigned long ret = 0;
2326 /* Don't lock: we're in enough trouble already. */
2327 if ((colon = strchr(name, ':')) != NULL) {
2329 if ((mod = find_module(name)) != NULL)
2330 ret = mod_find_symname(mod, colon+1);
2333 list_for_each_entry(mod, &modules, list)
2334 if ((ret = mod_find_symname(mod, name)) != 0)
2339 #endif /* CONFIG_KALLSYMS */
2341 /* Called by the /proc file system to return a list of modules. */
2342 static void *m_start(struct seq_file *m, loff_t *pos)
2344 mutex_lock(&module_mutex);
2345 return seq_list_start(&modules, *pos);
2348 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2350 return seq_list_next(p, &modules, pos);
2353 static void m_stop(struct seq_file *m, void *p)
2355 mutex_unlock(&module_mutex);
2358 static char *taint_flags(unsigned int taints, char *buf)
2364 if (taints & TAINT_PROPRIETARY_MODULE)
2366 if (taints & TAINT_FORCED_MODULE)
2369 * TAINT_FORCED_RMMOD: could be added.
2370 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2380 static int m_show(struct seq_file *m, void *p)
2382 struct module *mod = list_entry(p, struct module, list);
2385 seq_printf(m, "%s %lu",
2386 mod->name, mod->init_size + mod->core_size);
2387 print_unload_info(m, mod);
2389 /* Informative for users. */
2390 seq_printf(m, " %s",
2391 mod->state == MODULE_STATE_GOING ? "Unloading":
2392 mod->state == MODULE_STATE_COMING ? "Loading":
2394 /* Used by oprofile and other similar tools. */
2395 seq_printf(m, " 0x%p", mod->module_core);
2399 seq_printf(m, " %s", taint_flags(mod->taints, buf));
2401 seq_printf(m, "\n");
2405 /* Format: modulename size refcount deps address
2407 Where refcount is a number or -, and deps is a comma-separated list
2410 const struct seq_operations modules_op = {
2417 /* Given an address, look for it in the module exception tables. */
2418 const struct exception_table_entry *search_module_extables(unsigned long addr)
2420 const struct exception_table_entry *e = NULL;
2424 list_for_each_entry(mod, &modules, list) {
2425 if (mod->num_exentries == 0)
2428 e = search_extable(mod->extable,
2429 mod->extable + mod->num_exentries - 1,
2436 /* Now, if we found one, we are running inside it now, hence
2437 we cannot unload the module, hence no refcnt needed. */
2442 * Is this a valid module address?
2444 int is_module_address(unsigned long addr)
2450 list_for_each_entry(mod, &modules, list) {
2451 if (within(addr, mod->module_core, mod->core_size)) {
2463 /* Is this a valid kernel address? */
2464 struct module *__module_text_address(unsigned long addr)
2468 list_for_each_entry(mod, &modules, list)
2469 if (within(addr, mod->module_init, mod->init_text_size)
2470 || within(addr, mod->module_core, mod->core_text_size))
2475 struct module *module_text_address(unsigned long addr)
2480 mod = __module_text_address(addr);
2486 /* Don't grab lock, we're oopsing. */
2487 void print_modules(void)
2492 printk("Modules linked in:");
2493 list_for_each_entry(mod, &modules, list)
2494 printk(" %s%s", mod->name, taint_flags(mod->taints, buf));
2499 static char *make_driver_name(struct device_driver *drv)
2503 driver_name = kmalloc(strlen(drv->name) + strlen(drv->bus->name) + 2,
2508 sprintf(driver_name, "%s:%s", drv->bus->name, drv->name);
2512 static void module_create_drivers_dir(struct module_kobject *mk)
2514 if (!mk || mk->drivers_dir)
2517 mk->drivers_dir = kobject_add_dir(&mk->kobj, "drivers");
2520 void module_add_driver(struct module *mod, struct device_driver *drv)
2524 struct module_kobject *mk = NULL;
2531 else if (drv->mod_name) {
2532 struct kobject *mkobj;
2534 /* Lookup built-in module entry in /sys/modules */
2535 mkobj = kset_find_obj(&module_subsys, drv->mod_name);
2537 mk = container_of(mkobj, struct module_kobject, kobj);
2538 /* remember our module structure */
2540 /* kset_find_obj took a reference */
2548 /* Don't check return codes; these calls are idempotent */
2549 no_warn = sysfs_create_link(&drv->kobj, &mk->kobj, "module");
2550 driver_name = make_driver_name(drv);
2552 module_create_drivers_dir(mk);
2553 no_warn = sysfs_create_link(mk->drivers_dir, &drv->kobj,
2558 EXPORT_SYMBOL(module_add_driver);
2560 void module_remove_driver(struct device_driver *drv)
2562 struct module_kobject *mk = NULL;
2568 sysfs_remove_link(&drv->kobj, "module");
2571 mk = &drv->owner->mkobj;
2572 else if (drv->mkobj)
2574 if (mk && mk->drivers_dir) {
2575 driver_name = make_driver_name(drv);
2577 sysfs_remove_link(mk->drivers_dir, driver_name);
2582 EXPORT_SYMBOL(module_remove_driver);
2585 #ifdef CONFIG_MODVERSIONS
2586 /* Generate the signature for struct module here, too, for modversions. */
2587 void struct_module(struct module *mod) { return; }
2588 EXPORT_SYMBOL(struct_module);
2591 #ifdef CONFIG_MARKERS
2592 void module_update_markers(struct module *probe_module, int *refcount)
2596 mutex_lock(&module_mutex);
2597 list_for_each_entry(mod, &modules, list)
2599 marker_update_probe_range(mod->markers,
2600 mod->markers + mod->num_markers,
2601 probe_module, refcount);
2602 mutex_unlock(&module_mutex);