aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/module.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/module.c')
-rw-r--r--kernel/module.c1352
1 files changed, 767 insertions, 585 deletions
diff --git a/kernel/module.c b/kernel/module.c
index 333fbcc96978..d0b5f8db11b4 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -1,6 +1,6 @@
1/* 1/*
2 Copyright (C) 2002 Richard Henderson 2 Copyright (C) 2002 Richard Henderson
3 Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM. 3 Copyright (C) 2001 Rusty Russell, 2002, 2010 Rusty Russell IBM.
4 4
5 This program is free software; you can redistribute it and/or modify 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 6 it under the terms of the GNU General Public License as published by
@@ -72,7 +72,11 @@
72/* If this is set, the section belongs in the init part of the module */ 72/* If this is set, the section belongs in the init part of the module */
73#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1)) 73#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
74 74
75/* List of modules, protected by module_mutex or preempt_disable 75/*
76 * Mutex protects:
77 * 1) List of modules (also safely readable with preempt_disable),
78 * 2) module_use links,
79 * 3) module_addr_min/module_addr_max.
76 * (delete uses stop_machine/add uses RCU list operations). */ 80 * (delete uses stop_machine/add uses RCU list operations). */
77DEFINE_MUTEX(module_mutex); 81DEFINE_MUTEX(module_mutex);
78EXPORT_SYMBOL_GPL(module_mutex); 82EXPORT_SYMBOL_GPL(module_mutex);
@@ -90,7 +94,8 @@ static DECLARE_WAIT_QUEUE_HEAD(module_wq);
90 94
91static BLOCKING_NOTIFIER_HEAD(module_notify_list); 95static BLOCKING_NOTIFIER_HEAD(module_notify_list);
92 96
93/* Bounds of module allocation, for speeding __module_address */ 97/* Bounds of module allocation, for speeding __module_address.
98 * Protected by module_mutex. */
94static unsigned long module_addr_min = -1UL, module_addr_max = 0; 99static unsigned long module_addr_min = -1UL, module_addr_max = 0;
95 100
96int register_module_notifier(struct notifier_block * nb) 101int register_module_notifier(struct notifier_block * nb)
@@ -105,6 +110,20 @@ int unregister_module_notifier(struct notifier_block * nb)
105} 110}
106EXPORT_SYMBOL(unregister_module_notifier); 111EXPORT_SYMBOL(unregister_module_notifier);
107 112
113struct load_info {
114 Elf_Ehdr *hdr;
115 unsigned long len;
116 Elf_Shdr *sechdrs;
117 char *secstrings, *strtab;
118 unsigned long *strmap;
119 unsigned long symoffs, stroffs;
120 struct _ddebug *debug;
121 unsigned int num_debug;
122 struct {
123 unsigned int sym, str, mod, vers, info, pcpu;
124 } index;
125};
126
108/* We require a truly strong try_module_get(): 0 means failure due to 127/* We require a truly strong try_module_get(): 0 means failure due to
109 ongoing or failed initialization etc. */ 128 ongoing or failed initialization etc. */
110static inline int strong_try_module_get(struct module *mod) 129static inline int strong_try_module_get(struct module *mod)
@@ -135,42 +154,38 @@ void __module_put_and_exit(struct module *mod, long code)
135EXPORT_SYMBOL(__module_put_and_exit); 154EXPORT_SYMBOL(__module_put_and_exit);
136 155
137/* Find a module section: 0 means not found. */ 156/* Find a module section: 0 means not found. */
138static unsigned int find_sec(Elf_Ehdr *hdr, 157static unsigned int find_sec(const struct load_info *info, const char *name)
139 Elf_Shdr *sechdrs,
140 const char *secstrings,
141 const char *name)
142{ 158{
143 unsigned int i; 159 unsigned int i;
144 160
145 for (i = 1; i < hdr->e_shnum; i++) 161 for (i = 1; i < info->hdr->e_shnum; i++) {
162 Elf_Shdr *shdr = &info->sechdrs[i];
146 /* Alloc bit cleared means "ignore it." */ 163 /* Alloc bit cleared means "ignore it." */
147 if ((sechdrs[i].sh_flags & SHF_ALLOC) 164 if ((shdr->sh_flags & SHF_ALLOC)
148 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0) 165 && strcmp(info->secstrings + shdr->sh_name, name) == 0)
149 return i; 166 return i;
167 }
150 return 0; 168 return 0;
151} 169}
152 170
153/* Find a module section, or NULL. */ 171/* Find a module section, or NULL. */
154static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs, 172static void *section_addr(const struct load_info *info, const char *name)
155 const char *secstrings, const char *name)
156{ 173{
157 /* Section 0 has sh_addr 0. */ 174 /* Section 0 has sh_addr 0. */
158 return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr; 175 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
159} 176}
160 177
161/* Find a module section, or NULL. Fill in number of "objects" in section. */ 178/* Find a module section, or NULL. Fill in number of "objects" in section. */
162static void *section_objs(Elf_Ehdr *hdr, 179static void *section_objs(const struct load_info *info,
163 Elf_Shdr *sechdrs,
164 const char *secstrings,
165 const char *name, 180 const char *name,
166 size_t object_size, 181 size_t object_size,
167 unsigned int *num) 182 unsigned int *num)
168{ 183{
169 unsigned int sec = find_sec(hdr, sechdrs, secstrings, name); 184 unsigned int sec = find_sec(info, name);
170 185
171 /* Section 0 has sh_addr 0 and sh_size 0. */ 186 /* Section 0 has sh_addr 0 and sh_size 0. */
172 *num = sechdrs[sec].sh_size / object_size; 187 *num = info->sechdrs[sec].sh_size / object_size;
173 return (void *)sechdrs[sec].sh_addr; 188 return (void *)info->sechdrs[sec].sh_addr;
174} 189}
175 190
176/* Provided by the linker */ 191/* Provided by the linker */
@@ -222,7 +237,7 @@ bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
222 unsigned int symnum, void *data), void *data) 237 unsigned int symnum, void *data), void *data)
223{ 238{
224 struct module *mod; 239 struct module *mod;
225 const struct symsearch arr[] = { 240 static const struct symsearch arr[] = {
226 { __start___ksymtab, __stop___ksymtab, __start___kcrctab, 241 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
227 NOT_GPL_ONLY, false }, 242 NOT_GPL_ONLY, false },
228 { __start___ksymtab_gpl, __stop___ksymtab_gpl, 243 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
@@ -329,7 +344,7 @@ static bool find_symbol_in_section(const struct symsearch *syms,
329} 344}
330 345
331/* Find a symbol and return it, along with, (optional) crc and 346/* Find a symbol and return it, along with, (optional) crc and
332 * (optional) module which owns it */ 347 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
333const struct kernel_symbol *find_symbol(const char *name, 348const struct kernel_symbol *find_symbol(const char *name,
334 struct module **owner, 349 struct module **owner,
335 const unsigned long **crc, 350 const unsigned long **crc,
@@ -387,7 +402,8 @@ static int percpu_modalloc(struct module *mod,
387 mod->percpu = __alloc_reserved_percpu(size, align); 402 mod->percpu = __alloc_reserved_percpu(size, align);
388 if (!mod->percpu) { 403 if (!mod->percpu) {
389 printk(KERN_WARNING 404 printk(KERN_WARNING
390 "Could not allocate %lu bytes percpu data\n", size); 405 "%s: Could not allocate %lu bytes percpu data\n",
406 mod->name, size);
391 return -ENOMEM; 407 return -ENOMEM;
392 } 408 }
393 mod->percpu_size = size; 409 mod->percpu_size = size;
@@ -399,11 +415,9 @@ static void percpu_modfree(struct module *mod)
399 free_percpu(mod->percpu); 415 free_percpu(mod->percpu);
400} 416}
401 417
402static unsigned int find_pcpusec(Elf_Ehdr *hdr, 418static unsigned int find_pcpusec(struct load_info *info)
403 Elf_Shdr *sechdrs,
404 const char *secstrings)
405{ 419{
406 return find_sec(hdr, sechdrs, secstrings, ".data.percpu"); 420 return find_sec(info, ".data..percpu");
407} 421}
408 422
409static void percpu_modcopy(struct module *mod, 423static void percpu_modcopy(struct module *mod,
@@ -463,9 +477,7 @@ static inline int percpu_modalloc(struct module *mod,
463static inline void percpu_modfree(struct module *mod) 477static inline void percpu_modfree(struct module *mod)
464{ 478{
465} 479}
466static inline unsigned int find_pcpusec(Elf_Ehdr *hdr, 480static unsigned int find_pcpusec(struct load_info *info)
467 Elf_Shdr *sechdrs,
468 const char *secstrings)
469{ 481{
470 return 0; 482 return 0;
471} 483}
@@ -519,36 +531,30 @@ static char last_unloaded_module[MODULE_NAME_LEN+1];
519EXPORT_TRACEPOINT_SYMBOL(module_get); 531EXPORT_TRACEPOINT_SYMBOL(module_get);
520 532
521/* Init the unload section of the module. */ 533/* Init the unload section of the module. */
522static void module_unload_init(struct module *mod) 534static int module_unload_init(struct module *mod)
523{ 535{
524 int cpu; 536 mod->refptr = alloc_percpu(struct module_ref);
537 if (!mod->refptr)
538 return -ENOMEM;
525 539
526 INIT_LIST_HEAD(&mod->modules_which_use_me); 540 INIT_LIST_HEAD(&mod->source_list);
527 for_each_possible_cpu(cpu) { 541 INIT_LIST_HEAD(&mod->target_list);
528 per_cpu_ptr(mod->refptr, cpu)->incs = 0;
529 per_cpu_ptr(mod->refptr, cpu)->decs = 0;
530 }
531 542
532 /* Hold reference count during initialization. */ 543 /* Hold reference count during initialization. */
533 __this_cpu_write(mod->refptr->incs, 1); 544 __this_cpu_write(mod->refptr->incs, 1);
534 /* Backwards compatibility macros put refcount during init. */ 545 /* Backwards compatibility macros put refcount during init. */
535 mod->waiter = current; 546 mod->waiter = current;
536}
537 547
538/* modules using other modules */ 548 return 0;
539struct module_use 549}
540{
541 struct list_head list;
542 struct module *module_which_uses;
543};
544 550
545/* Does a already use b? */ 551/* Does a already use b? */
546static int already_uses(struct module *a, struct module *b) 552static int already_uses(struct module *a, struct module *b)
547{ 553{
548 struct module_use *use; 554 struct module_use *use;
549 555
550 list_for_each_entry(use, &b->modules_which_use_me, list) { 556 list_for_each_entry(use, &b->source_list, source_list) {
551 if (use->module_which_uses == a) { 557 if (use->source == a) {
552 DEBUGP("%s uses %s!\n", a->name, b->name); 558 DEBUGP("%s uses %s!\n", a->name, b->name);
553 return 1; 559 return 1;
554 } 560 }
@@ -557,62 +563,70 @@ static int already_uses(struct module *a, struct module *b)
557 return 0; 563 return 0;
558} 564}
559 565
560/* Module a uses b */ 566/*
561int use_module(struct module *a, struct module *b) 567 * Module a uses b
568 * - we add 'a' as a "source", 'b' as a "target" of module use
569 * - the module_use is added to the list of 'b' sources (so
570 * 'b' can walk the list to see who sourced them), and of 'a'
571 * targets (so 'a' can see what modules it targets).
572 */
573static int add_module_usage(struct module *a, struct module *b)
562{ 574{
563 struct module_use *use; 575 struct module_use *use;
564 int no_warn, err;
565 576
566 if (b == NULL || already_uses(a, b)) return 1; 577 DEBUGP("Allocating new usage for %s.\n", a->name);
578 use = kmalloc(sizeof(*use), GFP_ATOMIC);
579 if (!use) {
580 printk(KERN_WARNING "%s: out of memory loading\n", a->name);
581 return -ENOMEM;
582 }
567 583
568 /* If we're interrupted or time out, we fail. */ 584 use->source = a;
569 if (wait_event_interruptible_timeout( 585 use->target = b;
570 module_wq, (err = strong_try_module_get(b)) != -EBUSY, 586 list_add(&use->source_list, &b->source_list);
571 30 * HZ) <= 0) { 587 list_add(&use->target_list, &a->target_list);
572 printk("%s: gave up waiting for init of module %s.\n", 588 return 0;
573 a->name, b->name); 589}
590
591/* Module a uses b: caller needs module_mutex() */
592int ref_module(struct module *a, struct module *b)
593{
594 int err;
595
596 if (b == NULL || already_uses(a, b))
574 return 0; 597 return 0;
575 }
576 598
577 /* If strong_try_module_get() returned a different error, we fail. */ 599 /* If module isn't available, we fail. */
600 err = strong_try_module_get(b);
578 if (err) 601 if (err)
579 return 0; 602 return err;
580 603
581 DEBUGP("Allocating new usage for %s.\n", a->name); 604 err = add_module_usage(a, b);
582 use = kmalloc(sizeof(*use), GFP_ATOMIC); 605 if (err) {
583 if (!use) {
584 printk("%s: out of memory loading\n", a->name);
585 module_put(b); 606 module_put(b);
586 return 0; 607 return err;
587 } 608 }
588 609 return 0;
589 use->module_which_uses = a;
590 list_add(&use->list, &b->modules_which_use_me);
591 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
592 return 1;
593} 610}
594EXPORT_SYMBOL_GPL(use_module); 611EXPORT_SYMBOL_GPL(ref_module);
595 612
596/* Clear the unload stuff of the module. */ 613/* Clear the unload stuff of the module. */
597static void module_unload_free(struct module *mod) 614static void module_unload_free(struct module *mod)
598{ 615{
599 struct module *i; 616 struct module_use *use, *tmp;
600
601 list_for_each_entry(i, &modules, list) {
602 struct module_use *use;
603 617
604 list_for_each_entry(use, &i->modules_which_use_me, list) { 618 mutex_lock(&module_mutex);
605 if (use->module_which_uses == mod) { 619 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
606 DEBUGP("%s unusing %s\n", mod->name, i->name); 620 struct module *i = use->target;
607 module_put(i); 621 DEBUGP("%s unusing %s\n", mod->name, i->name);
608 list_del(&use->list); 622 module_put(i);
609 kfree(use); 623 list_del(&use->source_list);
610 sysfs_remove_link(i->holders_dir, mod->name); 624 list_del(&use->target_list);
611 /* There can be at most one match. */ 625 kfree(use);
612 break;
613 }
614 }
615 } 626 }
627 mutex_unlock(&module_mutex);
628
629 free_percpu(mod->refptr);
616} 630}
617 631
618#ifdef CONFIG_MODULE_FORCE_UNLOAD 632#ifdef CONFIG_MODULE_FORCE_UNLOAD
@@ -735,7 +749,7 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
735 goto out; 749 goto out;
736 } 750 }
737 751
738 if (!list_empty(&mod->modules_which_use_me)) { 752 if (!list_empty(&mod->source_list)) {
739 /* Other modules depend on us: get rid of them first. */ 753 /* Other modules depend on us: get rid of them first. */
740 ret = -EWOULDBLOCK; 754 ret = -EWOULDBLOCK;
741 goto out; 755 goto out;
@@ -779,13 +793,13 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
779 blocking_notifier_call_chain(&module_notify_list, 793 blocking_notifier_call_chain(&module_notify_list,
780 MODULE_STATE_GOING, mod); 794 MODULE_STATE_GOING, mod);
781 async_synchronize_full(); 795 async_synchronize_full();
782 mutex_lock(&module_mutex); 796
783 /* Store the name of the last unloaded module for diagnostic purposes */ 797 /* Store the name of the last unloaded module for diagnostic purposes */
784 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module)); 798 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
785 ddebug_remove_module(mod->name);
786 free_module(mod);
787 799
788 out: 800 free_module(mod);
801 return 0;
802out:
789 mutex_unlock(&module_mutex); 803 mutex_unlock(&module_mutex);
790 return ret; 804 return ret;
791} 805}
@@ -799,9 +813,9 @@ static inline void print_unload_info(struct seq_file *m, struct module *mod)
799 813
800 /* Always include a trailing , so userspace can differentiate 814 /* Always include a trailing , so userspace can differentiate
801 between this and the old multi-field proc format. */ 815 between this and the old multi-field proc format. */
802 list_for_each_entry(use, &mod->modules_which_use_me, list) { 816 list_for_each_entry(use, &mod->source_list, source_list) {
803 printed_something = 1; 817 printed_something = 1;
804 seq_printf(m, "%s,", use->module_which_uses->name); 818 seq_printf(m, "%s,", use->source->name);
805 } 819 }
806 820
807 if (mod->init != NULL && mod->exit == NULL) { 821 if (mod->init != NULL && mod->exit == NULL) {
@@ -880,14 +894,15 @@ static inline void module_unload_free(struct module *mod)
880{ 894{
881} 895}
882 896
883int use_module(struct module *a, struct module *b) 897int ref_module(struct module *a, struct module *b)
884{ 898{
885 return strong_try_module_get(b) == 0; 899 return strong_try_module_get(b);
886} 900}
887EXPORT_SYMBOL_GPL(use_module); 901EXPORT_SYMBOL_GPL(ref_module);
888 902
889static inline void module_unload_init(struct module *mod) 903static inline int module_unload_init(struct module *mod)
890{ 904{
905 return 0;
891} 906}
892#endif /* CONFIG_MODULE_UNLOAD */ 907#endif /* CONFIG_MODULE_UNLOAD */
893 908
@@ -1001,6 +1016,8 @@ static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1001{ 1016{
1002 const unsigned long *crc; 1017 const unsigned long *crc;
1003 1018
1019 /* Since this should be found in kernel (which can't be removed),
1020 * no locking is necessary. */
1004 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL, 1021 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1005 &crc, true, false)) 1022 &crc, true, false))
1006 BUG(); 1023 BUG();
@@ -1043,35 +1060,68 @@ static inline int same_magic(const char *amagic, const char *bmagic,
1043} 1060}
1044#endif /* CONFIG_MODVERSIONS */ 1061#endif /* CONFIG_MODVERSIONS */
1045 1062
1046/* Resolve a symbol for this module. I.e. if we find one, record usage. 1063/* Resolve a symbol for this module. I.e. if we find one, record usage. */
1047 Must be holding module_mutex. */ 1064static const struct kernel_symbol *resolve_symbol(struct module *mod,
1048static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs, 1065 const struct load_info *info,
1049 unsigned int versindex,
1050 const char *name, 1066 const char *name,
1051 struct module *mod) 1067 char ownername[])
1052{ 1068{
1053 struct module *owner; 1069 struct module *owner;
1054 const struct kernel_symbol *sym; 1070 const struct kernel_symbol *sym;
1055 const unsigned long *crc; 1071 const unsigned long *crc;
1072 int err;
1056 1073
1074 mutex_lock(&module_mutex);
1057 sym = find_symbol(name, &owner, &crc, 1075 sym = find_symbol(name, &owner, &crc,
1058 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true); 1076 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1059 /* use_module can fail due to OOM, 1077 if (!sym)
1060 or module initialization or unloading */ 1078 goto unlock;
1061 if (sym) { 1079
1062 if (!check_version(sechdrs, versindex, name, mod, crc, owner) 1080 if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
1063 || !use_module(mod, owner)) 1081 owner)) {
1064 sym = NULL; 1082 sym = ERR_PTR(-EINVAL);
1083 goto getname;
1084 }
1085
1086 err = ref_module(mod, owner);
1087 if (err) {
1088 sym = ERR_PTR(err);
1089 goto getname;
1065 } 1090 }
1091
1092getname:
1093 /* We must make copy under the lock if we failed to get ref. */
1094 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1095unlock:
1096 mutex_unlock(&module_mutex);
1066 return sym; 1097 return sym;
1067} 1098}
1068 1099
1100static const struct kernel_symbol *
1101resolve_symbol_wait(struct module *mod,
1102 const struct load_info *info,
1103 const char *name)
1104{
1105 const struct kernel_symbol *ksym;
1106 char owner[MODULE_NAME_LEN];
1107
1108 if (wait_event_interruptible_timeout(module_wq,
1109 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1110 || PTR_ERR(ksym) != -EBUSY,
1111 30 * HZ) <= 0) {
1112 printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
1113 mod->name, owner);
1114 }
1115 return ksym;
1116}
1117
1069/* 1118/*
1070 * /sys/module/foo/sections stuff 1119 * /sys/module/foo/sections stuff
1071 * J. Corbet <corbet@lwn.net> 1120 * J. Corbet <corbet@lwn.net>
1072 */ 1121 */
1073#if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS) 1122#ifdef CONFIG_SYSFS
1074 1123
1124#ifdef CONFIG_KALLSYMS
1075static inline bool sect_empty(const Elf_Shdr *sect) 1125static inline bool sect_empty(const Elf_Shdr *sect)
1076{ 1126{
1077 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0; 1127 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
@@ -1108,8 +1158,7 @@ static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1108 kfree(sect_attrs); 1158 kfree(sect_attrs);
1109} 1159}
1110 1160
1111static void add_sect_attrs(struct module *mod, unsigned int nsect, 1161static void add_sect_attrs(struct module *mod, const struct load_info *info)
1112 char *secstrings, Elf_Shdr *sechdrs)
1113{ 1162{
1114 unsigned int nloaded = 0, i, size[2]; 1163 unsigned int nloaded = 0, i, size[2];
1115 struct module_sect_attrs *sect_attrs; 1164 struct module_sect_attrs *sect_attrs;
@@ -1117,8 +1166,8 @@ static void add_sect_attrs(struct module *mod, unsigned int nsect,
1117 struct attribute **gattr; 1166 struct attribute **gattr;
1118 1167
1119 /* Count loaded sections and allocate structures */ 1168 /* Count loaded sections and allocate structures */
1120 for (i = 0; i < nsect; i++) 1169 for (i = 0; i < info->hdr->e_shnum; i++)
1121 if (!sect_empty(&sechdrs[i])) 1170 if (!sect_empty(&info->sechdrs[i]))
1122 nloaded++; 1171 nloaded++;
1123 size[0] = ALIGN(sizeof(*sect_attrs) 1172 size[0] = ALIGN(sizeof(*sect_attrs)
1124 + nloaded * sizeof(sect_attrs->attrs[0]), 1173 + nloaded * sizeof(sect_attrs->attrs[0]),
@@ -1135,11 +1184,12 @@ static void add_sect_attrs(struct module *mod, unsigned int nsect,
1135 sect_attrs->nsections = 0; 1184 sect_attrs->nsections = 0;
1136 sattr = &sect_attrs->attrs[0]; 1185 sattr = &sect_attrs->attrs[0];
1137 gattr = &sect_attrs->grp.attrs[0]; 1186 gattr = &sect_attrs->grp.attrs[0];
1138 for (i = 0; i < nsect; i++) { 1187 for (i = 0; i < info->hdr->e_shnum; i++) {
1139 if (sect_empty(&sechdrs[i])) 1188 Elf_Shdr *sec = &info->sechdrs[i];
1189 if (sect_empty(sec))
1140 continue; 1190 continue;
1141 sattr->address = sechdrs[i].sh_addr; 1191 sattr->address = sec->sh_addr;
1142 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name, 1192 sattr->name = kstrdup(info->secstrings + sec->sh_name,
1143 GFP_KERNEL); 1193 GFP_KERNEL);
1144 if (sattr->name == NULL) 1194 if (sattr->name == NULL)
1145 goto out; 1195 goto out;
@@ -1207,8 +1257,7 @@ static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1207 kfree(notes_attrs); 1257 kfree(notes_attrs);
1208} 1258}
1209 1259
1210static void add_notes_attrs(struct module *mod, unsigned int nsect, 1260static void add_notes_attrs(struct module *mod, const struct load_info *info)
1211 char *secstrings, Elf_Shdr *sechdrs)
1212{ 1261{
1213 unsigned int notes, loaded, i; 1262 unsigned int notes, loaded, i;
1214 struct module_notes_attrs *notes_attrs; 1263 struct module_notes_attrs *notes_attrs;
@@ -1220,9 +1269,9 @@ static void add_notes_attrs(struct module *mod, unsigned int nsect,
1220 1269
1221 /* Count notes sections and allocate structures. */ 1270 /* Count notes sections and allocate structures. */
1222 notes = 0; 1271 notes = 0;
1223 for (i = 0; i < nsect; i++) 1272 for (i = 0; i < info->hdr->e_shnum; i++)
1224 if (!sect_empty(&sechdrs[i]) && 1273 if (!sect_empty(&info->sechdrs[i]) &&
1225 (sechdrs[i].sh_type == SHT_NOTE)) 1274 (info->sechdrs[i].sh_type == SHT_NOTE))
1226 ++notes; 1275 ++notes;
1227 1276
1228 if (notes == 0) 1277 if (notes == 0)
@@ -1236,15 +1285,15 @@ static void add_notes_attrs(struct module *mod, unsigned int nsect,
1236 1285
1237 notes_attrs->notes = notes; 1286 notes_attrs->notes = notes;
1238 nattr = &notes_attrs->attrs[0]; 1287 nattr = &notes_attrs->attrs[0];
1239 for (loaded = i = 0; i < nsect; ++i) { 1288 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1240 if (sect_empty(&sechdrs[i])) 1289 if (sect_empty(&info->sechdrs[i]))
1241 continue; 1290 continue;
1242 if (sechdrs[i].sh_type == SHT_NOTE) { 1291 if (info->sechdrs[i].sh_type == SHT_NOTE) {
1243 sysfs_bin_attr_init(nattr); 1292 sysfs_bin_attr_init(nattr);
1244 nattr->attr.name = mod->sect_attrs->attrs[loaded].name; 1293 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1245 nattr->attr.mode = S_IRUGO; 1294 nattr->attr.mode = S_IRUGO;
1246 nattr->size = sechdrs[i].sh_size; 1295 nattr->size = info->sechdrs[i].sh_size;
1247 nattr->private = (void *) sechdrs[i].sh_addr; 1296 nattr->private = (void *) info->sechdrs[i].sh_addr;
1248 nattr->read = module_notes_read; 1297 nattr->read = module_notes_read;
1249 ++nattr; 1298 ++nattr;
1250 } 1299 }
@@ -1275,8 +1324,8 @@ static void remove_notes_attrs(struct module *mod)
1275 1324
1276#else 1325#else
1277 1326
1278static inline void add_sect_attrs(struct module *mod, unsigned int nsect, 1327static inline void add_sect_attrs(struct module *mod,
1279 char *sectstrings, Elf_Shdr *sechdrs) 1328 const struct load_info *info)
1280{ 1329{
1281} 1330}
1282 1331
@@ -1284,18 +1333,44 @@ static inline void remove_sect_attrs(struct module *mod)
1284{ 1333{
1285} 1334}
1286 1335
1287static inline void add_notes_attrs(struct module *mod, unsigned int nsect, 1336static inline void add_notes_attrs(struct module *mod,
1288 char *sectstrings, Elf_Shdr *sechdrs) 1337 const struct load_info *info)
1289{ 1338{
1290} 1339}
1291 1340
1292static inline void remove_notes_attrs(struct module *mod) 1341static inline void remove_notes_attrs(struct module *mod)
1293{ 1342{
1294} 1343}
1344#endif /* CONFIG_KALLSYMS */
1345
1346static void add_usage_links(struct module *mod)
1347{
1348#ifdef CONFIG_MODULE_UNLOAD
1349 struct module_use *use;
1350 int nowarn;
1351
1352 mutex_lock(&module_mutex);
1353 list_for_each_entry(use, &mod->target_list, target_list) {
1354 nowarn = sysfs_create_link(use->target->holders_dir,
1355 &mod->mkobj.kobj, mod->name);
1356 }
1357 mutex_unlock(&module_mutex);
1295#endif 1358#endif
1359}
1296 1360
1297#ifdef CONFIG_SYSFS 1361static void del_usage_links(struct module *mod)
1298int module_add_modinfo_attrs(struct module *mod) 1362{
1363#ifdef CONFIG_MODULE_UNLOAD
1364 struct module_use *use;
1365
1366 mutex_lock(&module_mutex);
1367 list_for_each_entry(use, &mod->target_list, target_list)
1368 sysfs_remove_link(use->target->holders_dir, mod->name);
1369 mutex_unlock(&module_mutex);
1370#endif
1371}
1372
1373static int module_add_modinfo_attrs(struct module *mod)
1299{ 1374{
1300 struct module_attribute *attr; 1375 struct module_attribute *attr;
1301 struct module_attribute *temp_attr; 1376 struct module_attribute *temp_attr;
@@ -1321,7 +1396,7 @@ int module_add_modinfo_attrs(struct module *mod)
1321 return error; 1396 return error;
1322} 1397}
1323 1398
1324void module_remove_modinfo_attrs(struct module *mod) 1399static void module_remove_modinfo_attrs(struct module *mod)
1325{ 1400{
1326 struct module_attribute *attr; 1401 struct module_attribute *attr;
1327 int i; 1402 int i;
@@ -1337,7 +1412,7 @@ void module_remove_modinfo_attrs(struct module *mod)
1337 kfree(mod->modinfo_attrs); 1412 kfree(mod->modinfo_attrs);
1338} 1413}
1339 1414
1340int mod_sysfs_init(struct module *mod) 1415static int mod_sysfs_init(struct module *mod)
1341{ 1416{
1342 int err; 1417 int err;
1343 struct kobject *kobj; 1418 struct kobject *kobj;
@@ -1371,12 +1446,17 @@ out:
1371 return err; 1446 return err;
1372} 1447}
1373 1448
1374int mod_sysfs_setup(struct module *mod, 1449static int mod_sysfs_setup(struct module *mod,
1450 const struct load_info *info,
1375 struct kernel_param *kparam, 1451 struct kernel_param *kparam,
1376 unsigned int num_params) 1452 unsigned int num_params)
1377{ 1453{
1378 int err; 1454 int err;
1379 1455
1456 err = mod_sysfs_init(mod);
1457 if (err)
1458 goto out;
1459
1380 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj); 1460 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1381 if (!mod->holders_dir) { 1461 if (!mod->holders_dir) {
1382 err = -ENOMEM; 1462 err = -ENOMEM;
@@ -1391,6 +1471,10 @@ int mod_sysfs_setup(struct module *mod,
1391 if (err) 1471 if (err)
1392 goto out_unreg_param; 1472 goto out_unreg_param;
1393 1473
1474 add_usage_links(mod);
1475 add_sect_attrs(mod, info);
1476 add_notes_attrs(mod, info);
1477
1394 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD); 1478 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1395 return 0; 1479 return 0;
1396 1480
@@ -1400,24 +1484,44 @@ out_unreg_holders:
1400 kobject_put(mod->holders_dir); 1484 kobject_put(mod->holders_dir);
1401out_unreg: 1485out_unreg:
1402 kobject_put(&mod->mkobj.kobj); 1486 kobject_put(&mod->mkobj.kobj);
1487out:
1403 return err; 1488 return err;
1404} 1489}
1405 1490
1406static void mod_sysfs_fini(struct module *mod) 1491static void mod_sysfs_fini(struct module *mod)
1407{ 1492{
1493 remove_notes_attrs(mod);
1494 remove_sect_attrs(mod);
1408 kobject_put(&mod->mkobj.kobj); 1495 kobject_put(&mod->mkobj.kobj);
1409} 1496}
1410 1497
1411#else /* CONFIG_SYSFS */ 1498#else /* !CONFIG_SYSFS */
1499
1500static int mod_sysfs_setup(struct module *mod,
1501 const struct load_info *info,
1502 struct kernel_param *kparam,
1503 unsigned int num_params)
1504{
1505 return 0;
1506}
1412 1507
1413static void mod_sysfs_fini(struct module *mod) 1508static void mod_sysfs_fini(struct module *mod)
1414{ 1509{
1415} 1510}
1416 1511
1512static void module_remove_modinfo_attrs(struct module *mod)
1513{
1514}
1515
1516static void del_usage_links(struct module *mod)
1517{
1518}
1519
1417#endif /* CONFIG_SYSFS */ 1520#endif /* CONFIG_SYSFS */
1418 1521
1419static void mod_kobject_remove(struct module *mod) 1522static void mod_sysfs_teardown(struct module *mod)
1420{ 1523{
1524 del_usage_links(mod);
1421 module_remove_modinfo_attrs(mod); 1525 module_remove_modinfo_attrs(mod);
1422 module_param_sysfs_remove(mod); 1526 module_param_sysfs_remove(mod);
1423 kobject_put(mod->mkobj.drivers_dir); 1527 kobject_put(mod->mkobj.drivers_dir);
@@ -1436,16 +1540,19 @@ static int __unlink_module(void *_mod)
1436 return 0; 1540 return 0;
1437} 1541}
1438 1542
1439/* Free a module, remove from lists, etc (must hold module_mutex). */ 1543/* Free a module, remove from lists, etc. */
1440static void free_module(struct module *mod) 1544static void free_module(struct module *mod)
1441{ 1545{
1442 trace_module_free(mod); 1546 trace_module_free(mod);
1443 1547
1444 /* Delete from various lists */ 1548 /* Delete from various lists */
1549 mutex_lock(&module_mutex);
1445 stop_machine(__unlink_module, mod, NULL); 1550 stop_machine(__unlink_module, mod, NULL);
1446 remove_notes_attrs(mod); 1551 mutex_unlock(&module_mutex);
1447 remove_sect_attrs(mod); 1552 mod_sysfs_teardown(mod);
1448 mod_kobject_remove(mod); 1553
1554 /* Remove dynamic debug info */
1555 ddebug_remove_module(mod->name);
1449 1556
1450 /* Arch-specific cleanup. */ 1557 /* Arch-specific cleanup. */
1451 module_arch_cleanup(mod); 1558 module_arch_cleanup(mod);
@@ -1460,10 +1567,7 @@ static void free_module(struct module *mod)
1460 module_free(mod, mod->module_init); 1567 module_free(mod, mod->module_init);
1461 kfree(mod->args); 1568 kfree(mod->args);
1462 percpu_modfree(mod); 1569 percpu_modfree(mod);
1463#if defined(CONFIG_MODULE_UNLOAD) 1570
1464 if (mod->refptr)
1465 free_percpu(mod->refptr);
1466#endif
1467 /* Free lock-classes: */ 1571 /* Free lock-classes: */
1468 lockdep_free_key_range(mod->module_core, mod->core_size); 1572 lockdep_free_key_range(mod->module_core, mod->core_size);
1469 1573
@@ -1493,6 +1597,8 @@ EXPORT_SYMBOL_GPL(__symbol_get);
1493/* 1597/*
1494 * Ensure that an exported symbol [global namespace] does not already exist 1598 * Ensure that an exported symbol [global namespace] does not already exist
1495 * in the kernel or in some other module's exported symbol table. 1599 * in the kernel or in some other module's exported symbol table.
1600 *
1601 * You must hold the module_mutex.
1496 */ 1602 */
1497static int verify_export_symbols(struct module *mod) 1603static int verify_export_symbols(struct module *mod)
1498{ 1604{
@@ -1527,25 +1633,23 @@ static int verify_export_symbols(struct module *mod)
1527} 1633}
1528 1634
1529/* Change all symbols so that st_value encodes the pointer directly. */ 1635/* Change all symbols so that st_value encodes the pointer directly. */
1530static int simplify_symbols(Elf_Shdr *sechdrs, 1636static int simplify_symbols(struct module *mod, const struct load_info *info)
1531 unsigned int symindex, 1637{
1532 const char *strtab, 1638 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
1533 unsigned int versindex, 1639 Elf_Sym *sym = (void *)symsec->sh_addr;
1534 unsigned int pcpuindex,
1535 struct module *mod)
1536{
1537 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1538 unsigned long secbase; 1640 unsigned long secbase;
1539 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym); 1641 unsigned int i;
1540 int ret = 0; 1642 int ret = 0;
1541 const struct kernel_symbol *ksym; 1643 const struct kernel_symbol *ksym;
1542 1644
1543 for (i = 1; i < n; i++) { 1645 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
1646 const char *name = info->strtab + sym[i].st_name;
1647
1544 switch (sym[i].st_shndx) { 1648 switch (sym[i].st_shndx) {
1545 case SHN_COMMON: 1649 case SHN_COMMON:
1546 /* We compiled with -fno-common. These are not 1650 /* We compiled with -fno-common. These are not
1547 supposed to happen. */ 1651 supposed to happen. */
1548 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name); 1652 DEBUGP("Common symbol: %s\n", name);
1549 printk("%s: please compile with -fno-common\n", 1653 printk("%s: please compile with -fno-common\n",
1550 mod->name); 1654 mod->name);
1551 ret = -ENOEXEC; 1655 ret = -ENOEXEC;
@@ -1558,29 +1662,28 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
1558 break; 1662 break;
1559 1663
1560 case SHN_UNDEF: 1664 case SHN_UNDEF:
1561 ksym = resolve_symbol(sechdrs, versindex, 1665 ksym = resolve_symbol_wait(mod, info, name);
1562 strtab + sym[i].st_name, mod);
1563 /* Ok if resolved. */ 1666 /* Ok if resolved. */
1564 if (ksym) { 1667 if (ksym && !IS_ERR(ksym)) {
1565 sym[i].st_value = ksym->value; 1668 sym[i].st_value = ksym->value;
1566 break; 1669 break;
1567 } 1670 }
1568 1671
1569 /* Ok if weak. */ 1672 /* Ok if weak. */
1570 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK) 1673 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1571 break; 1674 break;
1572 1675
1573 printk(KERN_WARNING "%s: Unknown symbol %s\n", 1676 printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
1574 mod->name, strtab + sym[i].st_name); 1677 mod->name, name, PTR_ERR(ksym));
1575 ret = -ENOENT; 1678 ret = PTR_ERR(ksym) ?: -ENOENT;
1576 break; 1679 break;
1577 1680
1578 default: 1681 default:
1579 /* Divert to percpu allocation if a percpu var. */ 1682 /* Divert to percpu allocation if a percpu var. */
1580 if (sym[i].st_shndx == pcpuindex) 1683 if (sym[i].st_shndx == info->index.pcpu)
1581 secbase = (unsigned long)mod_percpu(mod); 1684 secbase = (unsigned long)mod_percpu(mod);
1582 else 1685 else
1583 secbase = sechdrs[sym[i].st_shndx].sh_addr; 1686 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
1584 sym[i].st_value += secbase; 1687 sym[i].st_value += secbase;
1585 break; 1688 break;
1586 } 1689 }
@@ -1589,6 +1692,35 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
1589 return ret; 1692 return ret;
1590} 1693}
1591 1694
1695static int apply_relocations(struct module *mod, const struct load_info *info)
1696{
1697 unsigned int i;
1698 int err = 0;
1699
1700 /* Now do relocations. */
1701 for (i = 1; i < info->hdr->e_shnum; i++) {
1702 unsigned int infosec = info->sechdrs[i].sh_info;
1703
1704 /* Not a valid relocation section? */
1705 if (infosec >= info->hdr->e_shnum)
1706 continue;
1707
1708 /* Don't bother with non-allocated sections */
1709 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
1710 continue;
1711
1712 if (info->sechdrs[i].sh_type == SHT_REL)
1713 err = apply_relocate(info->sechdrs, info->strtab,
1714 info->index.sym, i, mod);
1715 else if (info->sechdrs[i].sh_type == SHT_RELA)
1716 err = apply_relocate_add(info->sechdrs, info->strtab,
1717 info->index.sym, i, mod);
1718 if (err < 0)
1719 break;
1720 }
1721 return err;
1722}
1723
1592/* Additional bytes needed by arch in front of individual sections */ 1724/* Additional bytes needed by arch in front of individual sections */
1593unsigned int __weak arch_mod_section_prepend(struct module *mod, 1725unsigned int __weak arch_mod_section_prepend(struct module *mod,
1594 unsigned int section) 1726 unsigned int section)
@@ -1613,10 +1745,7 @@ static long get_offset(struct module *mod, unsigned int *size,
1613 might -- code, read-only data, read-write data, small data. Tally 1745 might -- code, read-only data, read-write data, small data. Tally
1614 sizes, and place the offsets into sh_entsize fields: high bit means it 1746 sizes, and place the offsets into sh_entsize fields: high bit means it
1615 belongs in init. */ 1747 belongs in init. */
1616static void layout_sections(struct module *mod, 1748static void layout_sections(struct module *mod, struct load_info *info)
1617 const Elf_Ehdr *hdr,
1618 Elf_Shdr *sechdrs,
1619 const char *secstrings)
1620{ 1749{
1621 static unsigned long const masks[][2] = { 1750 static unsigned long const masks[][2] = {
1622 /* NOTE: all executable code must be the first section 1751 /* NOTE: all executable code must be the first section
@@ -1629,21 +1758,22 @@ static void layout_sections(struct module *mod,
1629 }; 1758 };
1630 unsigned int m, i; 1759 unsigned int m, i;
1631 1760
1632 for (i = 0; i < hdr->e_shnum; i++) 1761 for (i = 0; i < info->hdr->e_shnum; i++)
1633 sechdrs[i].sh_entsize = ~0UL; 1762 info->sechdrs[i].sh_entsize = ~0UL;
1634 1763
1635 DEBUGP("Core section allocation order:\n"); 1764 DEBUGP("Core section allocation order:\n");
1636 for (m = 0; m < ARRAY_SIZE(masks); ++m) { 1765 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1637 for (i = 0; i < hdr->e_shnum; ++i) { 1766 for (i = 0; i < info->hdr->e_shnum; ++i) {
1638 Elf_Shdr *s = &sechdrs[i]; 1767 Elf_Shdr *s = &info->sechdrs[i];
1768 const char *sname = info->secstrings + s->sh_name;
1639 1769
1640 if ((s->sh_flags & masks[m][0]) != masks[m][0] 1770 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1641 || (s->sh_flags & masks[m][1]) 1771 || (s->sh_flags & masks[m][1])
1642 || s->sh_entsize != ~0UL 1772 || s->sh_entsize != ~0UL
1643 || strstarts(secstrings + s->sh_name, ".init")) 1773 || strstarts(sname, ".init"))
1644 continue; 1774 continue;
1645 s->sh_entsize = get_offset(mod, &mod->core_size, s, i); 1775 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
1646 DEBUGP("\t%s\n", secstrings + s->sh_name); 1776 DEBUGP("\t%s\n", name);
1647 } 1777 }
1648 if (m == 0) 1778 if (m == 0)
1649 mod->core_text_size = mod->core_size; 1779 mod->core_text_size = mod->core_size;
@@ -1651,17 +1781,18 @@ static void layout_sections(struct module *mod,
1651 1781
1652 DEBUGP("Init section allocation order:\n"); 1782 DEBUGP("Init section allocation order:\n");
1653 for (m = 0; m < ARRAY_SIZE(masks); ++m) { 1783 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1654 for (i = 0; i < hdr->e_shnum; ++i) { 1784 for (i = 0; i < info->hdr->e_shnum; ++i) {
1655 Elf_Shdr *s = &sechdrs[i]; 1785 Elf_Shdr *s = &info->sechdrs[i];
1786 const char *sname = info->secstrings + s->sh_name;
1656 1787
1657 if ((s->sh_flags & masks[m][0]) != masks[m][0] 1788 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1658 || (s->sh_flags & masks[m][1]) 1789 || (s->sh_flags & masks[m][1])
1659 || s->sh_entsize != ~0UL 1790 || s->sh_entsize != ~0UL
1660 || !strstarts(secstrings + s->sh_name, ".init")) 1791 || !strstarts(sname, ".init"))
1661 continue; 1792 continue;
1662 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i) 1793 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
1663 | INIT_OFFSET_MASK); 1794 | INIT_OFFSET_MASK);
1664 DEBUGP("\t%s\n", secstrings + s->sh_name); 1795 DEBUGP("\t%s\n", sname);
1665 } 1796 }
1666 if (m == 0) 1797 if (m == 0)
1667 mod->init_text_size = mod->init_size; 1798 mod->init_text_size = mod->init_size;
@@ -1700,33 +1831,28 @@ static char *next_string(char *string, unsigned long *secsize)
1700 return string; 1831 return string;
1701} 1832}
1702 1833
1703static char *get_modinfo(Elf_Shdr *sechdrs, 1834static char *get_modinfo(struct load_info *info, const char *tag)
1704 unsigned int info,
1705 const char *tag)
1706{ 1835{
1707 char *p; 1836 char *p;
1708 unsigned int taglen = strlen(tag); 1837 unsigned int taglen = strlen(tag);
1709 unsigned long size = sechdrs[info].sh_size; 1838 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
1839 unsigned long size = infosec->sh_size;
1710 1840
1711 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) { 1841 for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
1712 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=') 1842 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1713 return p + taglen + 1; 1843 return p + taglen + 1;
1714 } 1844 }
1715 return NULL; 1845 return NULL;
1716} 1846}
1717 1847
1718static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs, 1848static void setup_modinfo(struct module *mod, struct load_info *info)
1719 unsigned int infoindex)
1720{ 1849{
1721 struct module_attribute *attr; 1850 struct module_attribute *attr;
1722 int i; 1851 int i;
1723 1852
1724 for (i = 0; (attr = modinfo_attrs[i]); i++) { 1853 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1725 if (attr->setup) 1854 if (attr->setup)
1726 attr->setup(mod, 1855 attr->setup(mod, get_modinfo(info, attr->attr.name));
1727 get_modinfo(sechdrs,
1728 infoindex,
1729 attr->attr.name));
1730 } 1856 }
1731} 1857}
1732 1858
@@ -1767,11 +1893,10 @@ static int is_exported(const char *name, unsigned long value,
1767} 1893}
1768 1894
1769/* As per nm */ 1895/* As per nm */
1770static char elf_type(const Elf_Sym *sym, 1896static char elf_type(const Elf_Sym *sym, const struct load_info *info)
1771 Elf_Shdr *sechdrs,
1772 const char *secstrings,
1773 struct module *mod)
1774{ 1897{
1898 const Elf_Shdr *sechdrs = info->sechdrs;
1899
1775 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) { 1900 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1776 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT) 1901 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1777 return 'v'; 1902 return 'v';
@@ -1801,8 +1926,10 @@ static char elf_type(const Elf_Sym *sym,
1801 else 1926 else
1802 return 'b'; 1927 return 'b';
1803 } 1928 }
1804 if (strstarts(secstrings + sechdrs[sym->st_shndx].sh_name, ".debug")) 1929 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
1930 ".debug")) {
1805 return 'n'; 1931 return 'n';
1932 }
1806 return '?'; 1933 return '?';
1807} 1934}
1808 1935
@@ -1827,127 +1954,96 @@ static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
1827 return true; 1954 return true;
1828} 1955}
1829 1956
1830static unsigned long layout_symtab(struct module *mod, 1957static void layout_symtab(struct module *mod, struct load_info *info)
1831 Elf_Shdr *sechdrs,
1832 unsigned int symindex,
1833 unsigned int strindex,
1834 const Elf_Ehdr *hdr,
1835 const char *secstrings,
1836 unsigned long *pstroffs,
1837 unsigned long *strmap)
1838{ 1958{
1839 unsigned long symoffs; 1959 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
1840 Elf_Shdr *symsect = sechdrs + symindex; 1960 Elf_Shdr *strsect = info->sechdrs + info->index.str;
1841 Elf_Shdr *strsect = sechdrs + strindex;
1842 const Elf_Sym *src; 1961 const Elf_Sym *src;
1843 const char *strtab;
1844 unsigned int i, nsrc, ndst; 1962 unsigned int i, nsrc, ndst;
1845 1963
1846 /* Put symbol section at end of init part of module. */ 1964 /* Put symbol section at end of init part of module. */
1847 symsect->sh_flags |= SHF_ALLOC; 1965 symsect->sh_flags |= SHF_ALLOC;
1848 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect, 1966 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
1849 symindex) | INIT_OFFSET_MASK; 1967 info->index.sym) | INIT_OFFSET_MASK;
1850 DEBUGP("\t%s\n", secstrings + symsect->sh_name); 1968 DEBUGP("\t%s\n", info->secstrings + symsect->sh_name);
1851 1969
1852 src = (void *)hdr + symsect->sh_offset; 1970 src = (void *)info->hdr + symsect->sh_offset;
1853 nsrc = symsect->sh_size / sizeof(*src); 1971 nsrc = symsect->sh_size / sizeof(*src);
1854 strtab = (void *)hdr + strsect->sh_offset;
1855 for (ndst = i = 1; i < nsrc; ++i, ++src) 1972 for (ndst = i = 1; i < nsrc; ++i, ++src)
1856 if (is_core_symbol(src, sechdrs, hdr->e_shnum)) { 1973 if (is_core_symbol(src, info->sechdrs, info->hdr->e_shnum)) {
1857 unsigned int j = src->st_name; 1974 unsigned int j = src->st_name;
1858 1975
1859 while(!__test_and_set_bit(j, strmap) && strtab[j]) 1976 while (!__test_and_set_bit(j, info->strmap)
1977 && info->strtab[j])
1860 ++j; 1978 ++j;
1861 ++ndst; 1979 ++ndst;
1862 } 1980 }
1863 1981
1864 /* Append room for core symbols at end of core part. */ 1982 /* Append room for core symbols at end of core part. */
1865 symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1); 1983 info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
1866 mod->core_size = symoffs + ndst * sizeof(Elf_Sym); 1984 mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
1867 1985
1868 /* Put string table section at end of init part of module. */ 1986 /* Put string table section at end of init part of module. */
1869 strsect->sh_flags |= SHF_ALLOC; 1987 strsect->sh_flags |= SHF_ALLOC;
1870 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect, 1988 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
1871 strindex) | INIT_OFFSET_MASK; 1989 info->index.str) | INIT_OFFSET_MASK;
1872 DEBUGP("\t%s\n", secstrings + strsect->sh_name); 1990 DEBUGP("\t%s\n", info->secstrings + strsect->sh_name);
1873 1991
1874 /* Append room for core symbols' strings at end of core part. */ 1992 /* Append room for core symbols' strings at end of core part. */
1875 *pstroffs = mod->core_size; 1993 info->stroffs = mod->core_size;
1876 __set_bit(0, strmap); 1994 __set_bit(0, info->strmap);
1877 mod->core_size += bitmap_weight(strmap, strsect->sh_size); 1995 mod->core_size += bitmap_weight(info->strmap, strsect->sh_size);
1878
1879 return symoffs;
1880} 1996}
1881 1997
1882static void add_kallsyms(struct module *mod, 1998static void add_kallsyms(struct module *mod, const struct load_info *info)
1883 Elf_Shdr *sechdrs,
1884 unsigned int shnum,
1885 unsigned int symindex,
1886 unsigned int strindex,
1887 unsigned long symoffs,
1888 unsigned long stroffs,
1889 const char *secstrings,
1890 unsigned long *strmap)
1891{ 1999{
1892 unsigned int i, ndst; 2000 unsigned int i, ndst;
1893 const Elf_Sym *src; 2001 const Elf_Sym *src;
1894 Elf_Sym *dst; 2002 Elf_Sym *dst;
1895 char *s; 2003 char *s;
2004 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
1896 2005
1897 mod->symtab = (void *)sechdrs[symindex].sh_addr; 2006 mod->symtab = (void *)symsec->sh_addr;
1898 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym); 2007 mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
1899 mod->strtab = (void *)sechdrs[strindex].sh_addr; 2008 /* Make sure we get permanent strtab: don't use info->strtab. */
2009 mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
1900 2010
1901 /* Set types up while we still have access to sections. */ 2011 /* Set types up while we still have access to sections. */
1902 for (i = 0; i < mod->num_symtab; i++) 2012 for (i = 0; i < mod->num_symtab; i++)
1903 mod->symtab[i].st_info 2013 mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
1904 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1905 2014
1906 mod->core_symtab = dst = mod->module_core + symoffs; 2015 mod->core_symtab = dst = mod->module_core + info->symoffs;
1907 src = mod->symtab; 2016 src = mod->symtab;
1908 *dst = *src; 2017 *dst = *src;
1909 for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) { 2018 for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
1910 if (!is_core_symbol(src, sechdrs, shnum)) 2019 if (!is_core_symbol(src, info->sechdrs, info->hdr->e_shnum))
1911 continue; 2020 continue;
1912 dst[ndst] = *src; 2021 dst[ndst] = *src;
1913 dst[ndst].st_name = bitmap_weight(strmap, dst[ndst].st_name); 2022 dst[ndst].st_name = bitmap_weight(info->strmap,
2023 dst[ndst].st_name);
1914 ++ndst; 2024 ++ndst;
1915 } 2025 }
1916 mod->core_num_syms = ndst; 2026 mod->core_num_syms = ndst;
1917 2027
1918 mod->core_strtab = s = mod->module_core + stroffs; 2028 mod->core_strtab = s = mod->module_core + info->stroffs;
1919 for (*s = 0, i = 1; i < sechdrs[strindex].sh_size; ++i) 2029 for (*s = 0, i = 1; i < info->sechdrs[info->index.str].sh_size; ++i)
1920 if (test_bit(i, strmap)) 2030 if (test_bit(i, info->strmap))
1921 *++s = mod->strtab[i]; 2031 *++s = mod->strtab[i];
1922} 2032}
1923#else 2033#else
1924static inline unsigned long layout_symtab(struct module *mod, 2034static inline void layout_symtab(struct module *mod, struct load_info *info)
1925 Elf_Shdr *sechdrs,
1926 unsigned int symindex,
1927 unsigned int strindex,
1928 const Elf_Ehdr *hdr,
1929 const char *secstrings,
1930 unsigned long *pstroffs,
1931 unsigned long *strmap)
1932{ 2035{
1933 return 0;
1934} 2036}
1935 2037
1936static inline void add_kallsyms(struct module *mod, 2038static void add_kallsyms(struct module *mod, struct load_info *info)
1937 Elf_Shdr *sechdrs,
1938 unsigned int shnum,
1939 unsigned int symindex,
1940 unsigned int strindex,
1941 unsigned long symoffs,
1942 unsigned long stroffs,
1943 const char *secstrings,
1944 const unsigned long *strmap)
1945{ 2039{
1946} 2040}
1947#endif /* CONFIG_KALLSYMS */ 2041#endif /* CONFIG_KALLSYMS */
1948 2042
1949static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num) 2043static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
1950{ 2044{
2045 if (!debug)
2046 return;
1951#ifdef CONFIG_DYNAMIC_DEBUG 2047#ifdef CONFIG_DYNAMIC_DEBUG
1952 if (ddebug_add_module(debug, num, debug->modname)) 2048 if (ddebug_add_module(debug, num, debug->modname))
1953 printk(KERN_ERR "dynamic debug error adding module: %s\n", 2049 printk(KERN_ERR "dynamic debug error adding module: %s\n",
@@ -1955,77 +2051,70 @@ static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
1955#endif 2051#endif
1956} 2052}
1957 2053
2054static void dynamic_debug_remove(struct _ddebug *debug)
2055{
2056 if (debug)
2057 ddebug_remove_module(debug->modname);
2058}
2059
1958static void *module_alloc_update_bounds(unsigned long size) 2060static void *module_alloc_update_bounds(unsigned long size)
1959{ 2061{
1960 void *ret = module_alloc(size); 2062 void *ret = module_alloc(size);
1961 2063
1962 if (ret) { 2064 if (ret) {
2065 mutex_lock(&module_mutex);
1963 /* Update module bounds. */ 2066 /* Update module bounds. */
1964 if ((unsigned long)ret < module_addr_min) 2067 if ((unsigned long)ret < module_addr_min)
1965 module_addr_min = (unsigned long)ret; 2068 module_addr_min = (unsigned long)ret;
1966 if ((unsigned long)ret + size > module_addr_max) 2069 if ((unsigned long)ret + size > module_addr_max)
1967 module_addr_max = (unsigned long)ret + size; 2070 module_addr_max = (unsigned long)ret + size;
2071 mutex_unlock(&module_mutex);
1968 } 2072 }
1969 return ret; 2073 return ret;
1970} 2074}
1971 2075
1972#ifdef CONFIG_DEBUG_KMEMLEAK 2076#ifdef CONFIG_DEBUG_KMEMLEAK
1973static void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr, 2077static void kmemleak_load_module(const struct module *mod,
1974 Elf_Shdr *sechdrs, char *secstrings) 2078 const struct load_info *info)
1975{ 2079{
1976 unsigned int i; 2080 unsigned int i;
1977 2081
1978 /* only scan the sections containing data */ 2082 /* only scan the sections containing data */
1979 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL); 2083 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
1980 2084
1981 for (i = 1; i < hdr->e_shnum; i++) { 2085 for (i = 1; i < info->hdr->e_shnum; i++) {
1982 if (!(sechdrs[i].sh_flags & SHF_ALLOC)) 2086 const char *name = info->secstrings + info->sechdrs[i].sh_name;
2087 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC))
1983 continue; 2088 continue;
1984 if (strncmp(secstrings + sechdrs[i].sh_name, ".data", 5) != 0 2089 if (!strstarts(name, ".data") && !strstarts(name, ".bss"))
1985 && strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) != 0)
1986 continue; 2090 continue;
1987 2091
1988 kmemleak_scan_area((void *)sechdrs[i].sh_addr, 2092 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
1989 sechdrs[i].sh_size, GFP_KERNEL); 2093 info->sechdrs[i].sh_size, GFP_KERNEL);
1990 } 2094 }
1991} 2095}
1992#else 2096#else
1993static inline void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr, 2097static inline void kmemleak_load_module(const struct module *mod,
1994 Elf_Shdr *sechdrs, char *secstrings) 2098 const struct load_info *info)
1995{ 2099{
1996} 2100}
1997#endif 2101#endif
1998 2102
1999/* Allocate and load the module: note that size of section 0 is always 2103/* Sets info->hdr and info->len. */
2000 zero, and we rely on this for optional sections. */ 2104static int copy_and_check(struct load_info *info,
2001static noinline struct module *load_module(void __user *umod, 2105 const void __user *umod, unsigned long len,
2002 unsigned long len, 2106 const char __user *uargs)
2003 const char __user *uargs)
2004{ 2107{
2108 int err;
2005 Elf_Ehdr *hdr; 2109 Elf_Ehdr *hdr;
2006 Elf_Shdr *sechdrs;
2007 char *secstrings, *args, *modmagic, *strtab = NULL;
2008 char *staging;
2009 unsigned int i;
2010 unsigned int symindex = 0;
2011 unsigned int strindex = 0;
2012 unsigned int modindex, versindex, infoindex, pcpuindex;
2013 struct module *mod;
2014 long err = 0;
2015 void *ptr = NULL; /* Stops spurious gcc warning */
2016 unsigned long symoffs, stroffs, *strmap;
2017
2018 mm_segment_t old_fs;
2019 2110
2020 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
2021 umod, len, uargs);
2022 if (len < sizeof(*hdr)) 2111 if (len < sizeof(*hdr))
2023 return ERR_PTR(-ENOEXEC); 2112 return -ENOEXEC;
2024 2113
2025 /* Suck in entire file: we'll want most of it. */ 2114 /* Suck in entire file: we'll want most of it. */
2026 /* vmalloc barfs on "unusual" numbers. Check here */ 2115 /* vmalloc barfs on "unusual" numbers. Check here */
2027 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL) 2116 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
2028 return ERR_PTR(-ENOMEM); 2117 return -ENOMEM;
2029 2118
2030 if (copy_from_user(hdr, umod, len) != 0) { 2119 if (copy_from_user(hdr, umod, len) != 0) {
2031 err = -EFAULT; 2120 err = -EFAULT;
@@ -2033,138 +2122,225 @@ static noinline struct module *load_module(void __user *umod,
2033 } 2122 }
2034 2123
2035 /* Sanity checks against insmoding binaries or wrong arch, 2124 /* Sanity checks against insmoding binaries or wrong arch,
2036 weird elf version */ 2125 weird elf version */
2037 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0 2126 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
2038 || hdr->e_type != ET_REL 2127 || hdr->e_type != ET_REL
2039 || !elf_check_arch(hdr) 2128 || !elf_check_arch(hdr)
2040 || hdr->e_shentsize != sizeof(*sechdrs)) { 2129 || hdr->e_shentsize != sizeof(Elf_Shdr)) {
2041 err = -ENOEXEC; 2130 err = -ENOEXEC;
2042 goto free_hdr; 2131 goto free_hdr;
2043 } 2132 }
2044 2133
2045 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) 2134 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) {
2046 goto truncated; 2135 err = -ENOEXEC;
2136 goto free_hdr;
2137 }
2047 2138
2048 /* Convenience variables */ 2139 info->hdr = hdr;
2049 sechdrs = (void *)hdr + hdr->e_shoff; 2140 info->len = len;
2050 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; 2141 return 0;
2051 sechdrs[0].sh_addr = 0;
2052 2142
2053 for (i = 1; i < hdr->e_shnum; i++) { 2143free_hdr:
2054 if (sechdrs[i].sh_type != SHT_NOBITS 2144 vfree(hdr);
2055 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size) 2145 return err;
2056 goto truncated; 2146}
2147
2148static void free_copy(struct load_info *info)
2149{
2150 vfree(info->hdr);
2151}
2152
2153static int rewrite_section_headers(struct load_info *info)
2154{
2155 unsigned int i;
2156
2157 /* This should always be true, but let's be sure. */
2158 info->sechdrs[0].sh_addr = 0;
2159
2160 for (i = 1; i < info->hdr->e_shnum; i++) {
2161 Elf_Shdr *shdr = &info->sechdrs[i];
2162 if (shdr->sh_type != SHT_NOBITS
2163 && info->len < shdr->sh_offset + shdr->sh_size) {
2164 printk(KERN_ERR "Module len %lu truncated\n",
2165 info->len);
2166 return -ENOEXEC;
2167 }
2057 2168
2058 /* Mark all sections sh_addr with their address in the 2169 /* Mark all sections sh_addr with their address in the
2059 temporary image. */ 2170 temporary image. */
2060 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset; 2171 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2061 2172
2062 /* Internal symbols and strings. */
2063 if (sechdrs[i].sh_type == SHT_SYMTAB) {
2064 symindex = i;
2065 strindex = sechdrs[i].sh_link;
2066 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
2067 }
2068#ifndef CONFIG_MODULE_UNLOAD 2173#ifndef CONFIG_MODULE_UNLOAD
2069 /* Don't load .exit sections */ 2174 /* Don't load .exit sections */
2070 if (strstarts(secstrings+sechdrs[i].sh_name, ".exit")) 2175 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2071 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC; 2176 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2072#endif 2177#endif
2073 } 2178 }
2074 2179
2075 modindex = find_sec(hdr, sechdrs, secstrings, 2180 /* Track but don't keep modinfo and version sections. */
2076 ".gnu.linkonce.this_module"); 2181 info->index.vers = find_sec(info, "__versions");
2077 if (!modindex) { 2182 info->index.info = find_sec(info, ".modinfo");
2183 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2184 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
2185 return 0;
2186}
2187
2188/*
2189 * Set up our basic convenience variables (pointers to section headers,
2190 * search for module section index etc), and do some basic section
2191 * verification.
2192 *
2193 * Return the temporary module pointer (we'll replace it with the final
2194 * one when we move the module sections around).
2195 */
2196static struct module *setup_load_info(struct load_info *info)
2197{
2198 unsigned int i;
2199 int err;
2200 struct module *mod;
2201
2202 /* Set up the convenience variables */
2203 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
2204 info->secstrings = (void *)info->hdr
2205 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
2206
2207 err = rewrite_section_headers(info);
2208 if (err)
2209 return ERR_PTR(err);
2210
2211 /* Find internal symbols and strings. */
2212 for (i = 1; i < info->hdr->e_shnum; i++) {
2213 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2214 info->index.sym = i;
2215 info->index.str = info->sechdrs[i].sh_link;
2216 info->strtab = (char *)info->hdr
2217 + info->sechdrs[info->index.str].sh_offset;
2218 break;
2219 }
2220 }
2221
2222 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
2223 if (!info->index.mod) {
2078 printk(KERN_WARNING "No module found in object\n"); 2224 printk(KERN_WARNING "No module found in object\n");
2079 err = -ENOEXEC; 2225 return ERR_PTR(-ENOEXEC);
2080 goto free_hdr;
2081 } 2226 }
2082 /* This is temporary: point mod into copy of data. */ 2227 /* This is temporary: point mod into copy of data. */
2083 mod = (void *)sechdrs[modindex].sh_addr; 2228 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2084 2229
2085 if (symindex == 0) { 2230 if (info->index.sym == 0) {
2086 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n", 2231 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2087 mod->name); 2232 mod->name);
2088 err = -ENOEXEC; 2233 return ERR_PTR(-ENOEXEC);
2089 goto free_hdr;
2090 } 2234 }
2091 2235
2092 versindex = find_sec(hdr, sechdrs, secstrings, "__versions"); 2236 info->index.pcpu = find_pcpusec(info);
2093 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
2094 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
2095
2096 /* Don't keep modinfo and version sections. */
2097 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2098 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2099 2237
2100 /* Check module struct version now, before we try to use module. */ 2238 /* Check module struct version now, before we try to use module. */
2101 if (!check_modstruct_version(sechdrs, versindex, mod)) { 2239 if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2102 err = -ENOEXEC; 2240 return ERR_PTR(-ENOEXEC);
2103 goto free_hdr; 2241
2104 } 2242 return mod;
2243}
2244
2245static int check_modinfo(struct module *mod, struct load_info *info)
2246{
2247 const char *modmagic = get_modinfo(info, "vermagic");
2248 int err;
2105 2249
2106 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
2107 /* This is allowed: modprobe --force will invalidate it. */ 2250 /* This is allowed: modprobe --force will invalidate it. */
2108 if (!modmagic) { 2251 if (!modmagic) {
2109 err = try_to_force_load(mod, "bad vermagic"); 2252 err = try_to_force_load(mod, "bad vermagic");
2110 if (err) 2253 if (err)
2111 goto free_hdr; 2254 return err;
2112 } else if (!same_magic(modmagic, vermagic, versindex)) { 2255 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
2113 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n", 2256 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2114 mod->name, modmagic, vermagic); 2257 mod->name, modmagic, vermagic);
2115 err = -ENOEXEC; 2258 return -ENOEXEC;
2116 goto free_hdr;
2117 } 2259 }
2118 2260
2119 staging = get_modinfo(sechdrs, infoindex, "staging"); 2261 if (get_modinfo(info, "staging")) {
2120 if (staging) {
2121 add_taint_module(mod, TAINT_CRAP); 2262 add_taint_module(mod, TAINT_CRAP);
2122 printk(KERN_WARNING "%s: module is from the staging directory," 2263 printk(KERN_WARNING "%s: module is from the staging directory,"
2123 " the quality is unknown, you have been warned.\n", 2264 " the quality is unknown, you have been warned.\n",
2124 mod->name); 2265 mod->name);
2125 } 2266 }
2126 2267
2127 /* Now copy in args */ 2268 /* Set up license info based on the info section */
2128 args = strndup_user(uargs, ~0UL >> 1); 2269 set_license(mod, get_modinfo(info, "license"));
2129 if (IS_ERR(args)) {
2130 err = PTR_ERR(args);
2131 goto free_hdr;
2132 }
2133 2270
2134 strmap = kzalloc(BITS_TO_LONGS(sechdrs[strindex].sh_size) 2271 return 0;
2135 * sizeof(long), GFP_KERNEL); 2272}
2136 if (!strmap) {
2137 err = -ENOMEM;
2138 goto free_mod;
2139 }
2140 2273
2141 if (find_module(mod->name)) { 2274static void find_module_sections(struct module *mod, struct load_info *info)
2142 err = -EEXIST; 2275{
2143 goto free_mod; 2276 mod->kp = section_objs(info, "__param",
2144 } 2277 sizeof(*mod->kp), &mod->num_kp);
2278 mod->syms = section_objs(info, "__ksymtab",
2279 sizeof(*mod->syms), &mod->num_syms);
2280 mod->crcs = section_addr(info, "__kcrctab");
2281 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
2282 sizeof(*mod->gpl_syms),
2283 &mod->num_gpl_syms);
2284 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
2285 mod->gpl_future_syms = section_objs(info,
2286 "__ksymtab_gpl_future",
2287 sizeof(*mod->gpl_future_syms),
2288 &mod->num_gpl_future_syms);
2289 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
2145 2290
2146 mod->state = MODULE_STATE_COMING; 2291#ifdef CONFIG_UNUSED_SYMBOLS
2292 mod->unused_syms = section_objs(info, "__ksymtab_unused",
2293 sizeof(*mod->unused_syms),
2294 &mod->num_unused_syms);
2295 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
2296 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
2297 sizeof(*mod->unused_gpl_syms),
2298 &mod->num_unused_gpl_syms);
2299 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
2300#endif
2301#ifdef CONFIG_CONSTRUCTORS
2302 mod->ctors = section_objs(info, ".ctors",
2303 sizeof(*mod->ctors), &mod->num_ctors);
2304#endif
2147 2305
2148 /* Allow arches to frob section contents and sizes. */ 2306#ifdef CONFIG_TRACEPOINTS
2149 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod); 2307 mod->tracepoints = section_objs(info, "__tracepoints",
2150 if (err < 0) 2308 sizeof(*mod->tracepoints),
2151 goto free_mod; 2309 &mod->num_tracepoints);
2310#endif
2311#ifdef CONFIG_EVENT_TRACING
2312 mod->trace_events = section_objs(info, "_ftrace_events",
2313 sizeof(*mod->trace_events),
2314 &mod->num_trace_events);
2315 /*
2316 * This section contains pointers to allocated objects in the trace
2317 * code and not scanning it leads to false positives.
2318 */
2319 kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2320 mod->num_trace_events, GFP_KERNEL);
2321#endif
2322#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2323 /* sechdrs[0].sh_size is always zero */
2324 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
2325 sizeof(*mod->ftrace_callsites),
2326 &mod->num_ftrace_callsites);
2327#endif
2152 2328
2153 if (pcpuindex) { 2329 mod->extable = section_objs(info, "__ex_table",
2154 /* We have a special allocation for this section. */ 2330 sizeof(*mod->extable), &mod->num_exentries);
2155 err = percpu_modalloc(mod, sechdrs[pcpuindex].sh_size,
2156 sechdrs[pcpuindex].sh_addralign);
2157 if (err)
2158 goto free_mod;
2159 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2160 }
2161 2331
2162 /* Determine total sizes, and put offsets in sh_entsize. For now 2332 if (section_addr(info, "__obsparm"))
2163 this is done generically; there doesn't appear to be any 2333 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2164 special cases for the architectures. */ 2334 mod->name);
2165 layout_sections(mod, hdr, sechdrs, secstrings); 2335
2166 symoffs = layout_symtab(mod, sechdrs, symindex, strindex, hdr, 2336 info->debug = section_objs(info, "__verbose",
2167 secstrings, &stroffs, strmap); 2337 sizeof(*info->debug), &info->num_debug);
2338}
2339
2340static int move_module(struct module *mod, struct load_info *info)
2341{
2342 int i;
2343 void *ptr;
2168 2344
2169 /* Do the allocs. */ 2345 /* Do the allocs. */
2170 ptr = module_alloc_update_bounds(mod->core_size); 2346 ptr = module_alloc_update_bounds(mod->core_size);
@@ -2174,10 +2350,9 @@ static noinline struct module *load_module(void __user *umod,
2174 * leak. 2350 * leak.
2175 */ 2351 */
2176 kmemleak_not_leak(ptr); 2352 kmemleak_not_leak(ptr);
2177 if (!ptr) { 2353 if (!ptr)
2178 err = -ENOMEM; 2354 return -ENOMEM;
2179 goto free_percpu; 2355
2180 }
2181 memset(ptr, 0, mod->core_size); 2356 memset(ptr, 0, mod->core_size);
2182 mod->module_core = ptr; 2357 mod->module_core = ptr;
2183 2358
@@ -2190,55 +2365,40 @@ static noinline struct module *load_module(void __user *umod,
2190 */ 2365 */
2191 kmemleak_ignore(ptr); 2366 kmemleak_ignore(ptr);
2192 if (!ptr && mod->init_size) { 2367 if (!ptr && mod->init_size) {
2193 err = -ENOMEM; 2368 module_free(mod, mod->module_core);
2194 goto free_core; 2369 return -ENOMEM;
2195 } 2370 }
2196 memset(ptr, 0, mod->init_size); 2371 memset(ptr, 0, mod->init_size);
2197 mod->module_init = ptr; 2372 mod->module_init = ptr;
2198 2373
2199 /* Transfer each section which specifies SHF_ALLOC */ 2374 /* Transfer each section which specifies SHF_ALLOC */
2200 DEBUGP("final section addresses:\n"); 2375 DEBUGP("final section addresses:\n");
2201 for (i = 0; i < hdr->e_shnum; i++) { 2376 for (i = 0; i < info->hdr->e_shnum; i++) {
2202 void *dest; 2377 void *dest;
2378 Elf_Shdr *shdr = &info->sechdrs[i];
2203 2379
2204 if (!(sechdrs[i].sh_flags & SHF_ALLOC)) 2380 if (!(shdr->sh_flags & SHF_ALLOC))
2205 continue; 2381 continue;
2206 2382
2207 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK) 2383 if (shdr->sh_entsize & INIT_OFFSET_MASK)
2208 dest = mod->module_init 2384 dest = mod->module_init
2209 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK); 2385 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
2210 else 2386 else
2211 dest = mod->module_core + sechdrs[i].sh_entsize; 2387 dest = mod->module_core + shdr->sh_entsize;
2212 2388
2213 if (sechdrs[i].sh_type != SHT_NOBITS) 2389 if (shdr->sh_type != SHT_NOBITS)
2214 memcpy(dest, (void *)sechdrs[i].sh_addr, 2390 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
2215 sechdrs[i].sh_size);
2216 /* Update sh_addr to point to copy in image. */ 2391 /* Update sh_addr to point to copy in image. */
2217 sechdrs[i].sh_addr = (unsigned long)dest; 2392 shdr->sh_addr = (unsigned long)dest;
2218 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name); 2393 DEBUGP("\t0x%lx %s\n",
2219 } 2394 shdr->sh_addr, info->secstrings + shdr->sh_name);
2220 /* Module has been moved. */
2221 mod = (void *)sechdrs[modindex].sh_addr;
2222 kmemleak_load_module(mod, hdr, sechdrs, secstrings);
2223
2224#if defined(CONFIG_MODULE_UNLOAD)
2225 mod->refptr = alloc_percpu(struct module_ref);
2226 if (!mod->refptr) {
2227 err = -ENOMEM;
2228 goto free_init;
2229 } 2395 }
2230#endif
2231 /* Now we've moved module, initialize linked lists, etc. */
2232 module_unload_init(mod);
2233 2396
2234 /* add kobject, so we can reference it. */ 2397 return 0;
2235 err = mod_sysfs_init(mod); 2398}
2236 if (err)
2237 goto free_unload;
2238
2239 /* Set up license info based on the info section */
2240 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2241 2399
2400static int check_module_license_and_versions(struct module *mod)
2401{
2242 /* 2402 /*
2243 * ndiswrapper is under GPL by itself, but loads proprietary modules. 2403 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2244 * Don't use add_taint_module(), as it would prevent ndiswrapper from 2404 * Don't use add_taint_module(), as it would prevent ndiswrapper from
@@ -2251,77 +2411,6 @@ static noinline struct module *load_module(void __user *umod,
2251 if (strcmp(mod->name, "driverloader") == 0) 2411 if (strcmp(mod->name, "driverloader") == 0)
2252 add_taint_module(mod, TAINT_PROPRIETARY_MODULE); 2412 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2253 2413
2254 /* Set up MODINFO_ATTR fields */
2255 setup_modinfo(mod, sechdrs, infoindex);
2256
2257 /* Fix up syms, so that st_value is a pointer to location. */
2258 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2259 mod);
2260 if (err < 0)
2261 goto cleanup;
2262
2263 /* Now we've got everything in the final locations, we can
2264 * find optional sections. */
2265 mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
2266 sizeof(*mod->kp), &mod->num_kp);
2267 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2268 sizeof(*mod->syms), &mod->num_syms);
2269 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2270 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2271 sizeof(*mod->gpl_syms),
2272 &mod->num_gpl_syms);
2273 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2274 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2275 "__ksymtab_gpl_future",
2276 sizeof(*mod->gpl_future_syms),
2277 &mod->num_gpl_future_syms);
2278 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2279 "__kcrctab_gpl_future");
2280
2281#ifdef CONFIG_UNUSED_SYMBOLS
2282 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2283 "__ksymtab_unused",
2284 sizeof(*mod->unused_syms),
2285 &mod->num_unused_syms);
2286 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2287 "__kcrctab_unused");
2288 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2289 "__ksymtab_unused_gpl",
2290 sizeof(*mod->unused_gpl_syms),
2291 &mod->num_unused_gpl_syms);
2292 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2293 "__kcrctab_unused_gpl");
2294#endif
2295#ifdef CONFIG_CONSTRUCTORS
2296 mod->ctors = section_objs(hdr, sechdrs, secstrings, ".ctors",
2297 sizeof(*mod->ctors), &mod->num_ctors);
2298#endif
2299
2300#ifdef CONFIG_TRACEPOINTS
2301 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2302 "__tracepoints",
2303 sizeof(*mod->tracepoints),
2304 &mod->num_tracepoints);
2305#endif
2306#ifdef CONFIG_EVENT_TRACING
2307 mod->trace_events = section_objs(hdr, sechdrs, secstrings,
2308 "_ftrace_events",
2309 sizeof(*mod->trace_events),
2310 &mod->num_trace_events);
2311 /*
2312 * This section contains pointers to allocated objects in the trace
2313 * code and not scanning it leads to false positives.
2314 */
2315 kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2316 mod->num_trace_events, GFP_KERNEL);
2317#endif
2318#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2319 /* sechdrs[0].sh_size is always zero */
2320 mod->ftrace_callsites = section_objs(hdr, sechdrs, secstrings,
2321 "__mcount_loc",
2322 sizeof(*mod->ftrace_callsites),
2323 &mod->num_ftrace_callsites);
2324#endif
2325#ifdef CONFIG_MODVERSIONS 2414#ifdef CONFIG_MODVERSIONS
2326 if ((mod->num_syms && !mod->crcs) 2415 if ((mod->num_syms && !mod->crcs)
2327 || (mod->num_gpl_syms && !mod->gpl_crcs) 2416 || (mod->num_gpl_syms && !mod->gpl_crcs)
@@ -2331,67 +2420,16 @@ static noinline struct module *load_module(void __user *umod,
2331 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs) 2420 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2332#endif 2421#endif
2333 ) { 2422 ) {
2334 err = try_to_force_load(mod, 2423 return try_to_force_load(mod,
2335 "no versions for exported symbols"); 2424 "no versions for exported symbols");
2336 if (err)
2337 goto cleanup;
2338 } 2425 }
2339#endif 2426#endif
2427 return 0;
2428}
2340 2429
2341 /* Now do relocations. */ 2430static void flush_module_icache(const struct module *mod)
2342 for (i = 1; i < hdr->e_shnum; i++) { 2431{
2343 const char *strtab = (char *)sechdrs[strindex].sh_addr; 2432 mm_segment_t old_fs;
2344 unsigned int info = sechdrs[i].sh_info;
2345
2346 /* Not a valid relocation section? */
2347 if (info >= hdr->e_shnum)
2348 continue;
2349
2350 /* Don't bother with non-allocated sections */
2351 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2352 continue;
2353
2354 if (sechdrs[i].sh_type == SHT_REL)
2355 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2356 else if (sechdrs[i].sh_type == SHT_RELA)
2357 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2358 mod);
2359 if (err < 0)
2360 goto cleanup;
2361 }
2362
2363 /* Find duplicate symbols */
2364 err = verify_export_symbols(mod);
2365 if (err < 0)
2366 goto cleanup;
2367
2368 /* Set up and sort exception table */
2369 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2370 sizeof(*mod->extable), &mod->num_exentries);
2371 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2372
2373 /* Finally, copy percpu area over. */
2374 percpu_modcopy(mod, (void *)sechdrs[pcpuindex].sh_addr,
2375 sechdrs[pcpuindex].sh_size);
2376
2377 add_kallsyms(mod, sechdrs, hdr->e_shnum, symindex, strindex,
2378 symoffs, stroffs, secstrings, strmap);
2379 kfree(strmap);
2380 strmap = NULL;
2381
2382 if (!mod->taints) {
2383 struct _ddebug *debug;
2384 unsigned int num_debug;
2385
2386 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2387 sizeof(*debug), &num_debug);
2388 if (debug)
2389 dynamic_debug_setup(debug, num_debug);
2390 }
2391
2392 err = module_finalize(hdr, sechdrs, mod);
2393 if (err < 0)
2394 goto cleanup;
2395 2433
2396 /* flush the icache in correct context */ 2434 /* flush the icache in correct context */
2397 old_fs = get_fs(); 2435 old_fs = get_fs();
@@ -2410,11 +2448,160 @@ static noinline struct module *load_module(void __user *umod,
2410 (unsigned long)mod->module_core + mod->core_size); 2448 (unsigned long)mod->module_core + mod->core_size);
2411 2449
2412 set_fs(old_fs); 2450 set_fs(old_fs);
2451}
2413 2452
2414 mod->args = args; 2453static struct module *layout_and_allocate(struct load_info *info)
2415 if (section_addr(hdr, sechdrs, secstrings, "__obsparm")) 2454{
2416 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n", 2455 /* Module within temporary copy. */
2417 mod->name); 2456 struct module *mod;
2457 Elf_Shdr *pcpusec;
2458 int err;
2459
2460 mod = setup_load_info(info);
2461 if (IS_ERR(mod))
2462 return mod;
2463
2464 err = check_modinfo(mod, info);
2465 if (err)
2466 return ERR_PTR(err);
2467
2468 /* Allow arches to frob section contents and sizes. */
2469 err = module_frob_arch_sections(info->hdr, info->sechdrs,
2470 info->secstrings, mod);
2471 if (err < 0)
2472 goto out;
2473
2474 pcpusec = &info->sechdrs[info->index.pcpu];
2475 if (pcpusec->sh_size) {
2476 /* We have a special allocation for this section. */
2477 err = percpu_modalloc(mod,
2478 pcpusec->sh_size, pcpusec->sh_addralign);
2479 if (err)
2480 goto out;
2481 pcpusec->sh_flags &= ~(unsigned long)SHF_ALLOC;
2482 }
2483
2484 /* Determine total sizes, and put offsets in sh_entsize. For now
2485 this is done generically; there doesn't appear to be any
2486 special cases for the architectures. */
2487 layout_sections(mod, info);
2488
2489 info->strmap = kzalloc(BITS_TO_LONGS(info->sechdrs[info->index.str].sh_size)
2490 * sizeof(long), GFP_KERNEL);
2491 if (!info->strmap) {
2492 err = -ENOMEM;
2493 goto free_percpu;
2494 }
2495 layout_symtab(mod, info);
2496
2497 /* Allocate and move to the final place */
2498 err = move_module(mod, info);
2499 if (err)
2500 goto free_strmap;
2501
2502 /* Module has been copied to its final place now: return it. */
2503 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2504 kmemleak_load_module(mod, info);
2505 return mod;
2506
2507free_strmap:
2508 kfree(info->strmap);
2509free_percpu:
2510 percpu_modfree(mod);
2511out:
2512 return ERR_PTR(err);
2513}
2514
2515/* mod is no longer valid after this! */
2516static void module_deallocate(struct module *mod, struct load_info *info)
2517{
2518 kfree(info->strmap);
2519 percpu_modfree(mod);
2520 module_free(mod, mod->module_init);
2521 module_free(mod, mod->module_core);
2522}
2523
2524static int post_relocation(struct module *mod, const struct load_info *info)
2525{
2526 /* Sort exception table now relocations are done. */
2527 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2528
2529 /* Copy relocated percpu area over. */
2530 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
2531 info->sechdrs[info->index.pcpu].sh_size);
2532
2533 /* Setup kallsyms-specific fields. */
2534 add_kallsyms(mod, info);
2535
2536 /* Arch-specific module finalizing. */
2537 return module_finalize(info->hdr, info->sechdrs, mod);
2538}
2539
2540/* Allocate and load the module: note that size of section 0 is always
2541 zero, and we rely on this for optional sections. */
2542static struct module *load_module(void __user *umod,
2543 unsigned long len,
2544 const char __user *uargs)
2545{
2546 struct load_info info = { NULL, };
2547 struct module *mod;
2548 long err;
2549
2550 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
2551 umod, len, uargs);
2552
2553 /* Copy in the blobs from userspace, check they are vaguely sane. */
2554 err = copy_and_check(&info, umod, len, uargs);
2555 if (err)
2556 return ERR_PTR(err);
2557
2558 /* Figure out module layout, and allocate all the memory. */
2559 mod = layout_and_allocate(&info);
2560 if (IS_ERR(mod)) {
2561 err = PTR_ERR(mod);
2562 goto free_copy;
2563 }
2564
2565 /* Now module is in final location, initialize linked lists, etc. */
2566 err = module_unload_init(mod);
2567 if (err)
2568 goto free_module;
2569
2570 /* Now we've got everything in the final locations, we can
2571 * find optional sections. */
2572 find_module_sections(mod, &info);
2573
2574 err = check_module_license_and_versions(mod);
2575 if (err)
2576 goto free_unload;
2577
2578 /* Set up MODINFO_ATTR fields */
2579 setup_modinfo(mod, &info);
2580
2581 /* Fix up syms, so that st_value is a pointer to location. */
2582 err = simplify_symbols(mod, &info);
2583 if (err < 0)
2584 goto free_modinfo;
2585
2586 err = apply_relocations(mod, &info);
2587 if (err < 0)
2588 goto free_modinfo;
2589
2590 err = post_relocation(mod, &info);
2591 if (err < 0)
2592 goto free_modinfo;
2593
2594 flush_module_icache(mod);
2595
2596 /* Now copy in args */
2597 mod->args = strndup_user(uargs, ~0UL >> 1);
2598 if (IS_ERR(mod->args)) {
2599 err = PTR_ERR(mod->args);
2600 goto free_arch_cleanup;
2601 }
2602
2603 /* Mark state as coming so strong_try_module_get() ignores us. */
2604 mod->state = MODULE_STATE_COMING;
2418 2605
2419 /* Now sew it into the lists so we can get lockdep and oops 2606 /* Now sew it into the lists so we can get lockdep and oops
2420 * info during argument parsing. Noone should access us, since 2607 * info during argument parsing. Noone should access us, since
@@ -2423,58 +2610,64 @@ static noinline struct module *load_module(void __user *umod,
2423 * function to insert in a way safe to concurrent readers. 2610 * function to insert in a way safe to concurrent readers.
2424 * The mutex protects against concurrent writers. 2611 * The mutex protects against concurrent writers.
2425 */ 2612 */
2613 mutex_lock(&module_mutex);
2614 if (find_module(mod->name)) {
2615 err = -EEXIST;
2616 goto unlock;
2617 }
2618
2619 /* This has to be done once we're sure module name is unique. */
2620 if (!mod->taints)
2621 dynamic_debug_setup(info.debug, info.num_debug);
2622
2623 /* Find duplicate symbols */
2624 err = verify_export_symbols(mod);
2625 if (err < 0)
2626 goto ddebug;
2627
2426 list_add_rcu(&mod->list, &modules); 2628 list_add_rcu(&mod->list, &modules);
2629 mutex_unlock(&module_mutex);
2427 2630
2631 /* Module is ready to execute: parsing args may do that. */
2428 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL); 2632 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
2429 if (err < 0) 2633 if (err < 0)
2430 goto unlink; 2634 goto unlink;
2431 2635
2432 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp); 2636 /* Link in to syfs. */
2637 err = mod_sysfs_setup(mod, &info, mod->kp, mod->num_kp);
2433 if (err < 0) 2638 if (err < 0)
2434 goto unlink; 2639 goto unlink;
2435 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2436 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2437
2438 /* Get rid of temporary copy */
2439 vfree(hdr);
2440 2640
2441 trace_module_load(mod); 2641 /* Get rid of temporary copy and strmap. */
2642 kfree(info.strmap);
2643 free_copy(&info);
2442 2644
2443 /* Done! */ 2645 /* Done! */
2646 trace_module_load(mod);
2444 return mod; 2647 return mod;
2445 2648
2446 unlink: 2649 unlink:
2650 mutex_lock(&module_mutex);
2447 /* Unlink carefully: kallsyms could be walking list. */ 2651 /* Unlink carefully: kallsyms could be walking list. */
2448 list_del_rcu(&mod->list); 2652 list_del_rcu(&mod->list);
2653 ddebug:
2654 if (!mod->taints)
2655 dynamic_debug_remove(info.debug);
2656 unlock:
2657 mutex_unlock(&module_mutex);
2449 synchronize_sched(); 2658 synchronize_sched();
2659 kfree(mod->args);
2660 free_arch_cleanup:
2450 module_arch_cleanup(mod); 2661 module_arch_cleanup(mod);
2451 cleanup: 2662 free_modinfo:
2452 free_modinfo(mod); 2663 free_modinfo(mod);
2453 kobject_del(&mod->mkobj.kobj);
2454 kobject_put(&mod->mkobj.kobj);
2455 free_unload: 2664 free_unload:
2456 module_unload_free(mod); 2665 module_unload_free(mod);
2457#if defined(CONFIG_MODULE_UNLOAD) 2666 free_module:
2458 free_percpu(mod->refptr); 2667 module_deallocate(mod, &info);
2459 free_init: 2668 free_copy:
2460#endif 2669 free_copy(&info);
2461 module_free(mod, mod->module_init);
2462 free_core:
2463 module_free(mod, mod->module_core);
2464 /* mod will be freed with core. Don't access it beyond this line! */
2465 free_percpu:
2466 percpu_modfree(mod);
2467 free_mod:
2468 kfree(args);
2469 kfree(strmap);
2470 free_hdr:
2471 vfree(hdr);
2472 return ERR_PTR(err); 2670 return ERR_PTR(err);
2473
2474 truncated:
2475 printk(KERN_ERR "Module len %lu truncated\n", len);
2476 err = -ENOEXEC;
2477 goto free_hdr;
2478} 2671}
2479 2672
2480/* Call module constructors. */ 2673/* Call module constructors. */
@@ -2499,19 +2692,10 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
2499 if (!capable(CAP_SYS_MODULE) || modules_disabled) 2692 if (!capable(CAP_SYS_MODULE) || modules_disabled)
2500 return -EPERM; 2693 return -EPERM;
2501 2694
2502 /* Only one module load at a time, please */
2503 if (mutex_lock_interruptible(&module_mutex) != 0)
2504 return -EINTR;
2505
2506 /* Do all the hard work */ 2695 /* Do all the hard work */
2507 mod = load_module(umod, len, uargs); 2696 mod = load_module(umod, len, uargs);
2508 if (IS_ERR(mod)) { 2697 if (IS_ERR(mod))
2509 mutex_unlock(&module_mutex);
2510 return PTR_ERR(mod); 2698 return PTR_ERR(mod);
2511 }
2512
2513 /* Drop lock so they can recurse */
2514 mutex_unlock(&module_mutex);
2515 2699
2516 blocking_notifier_call_chain(&module_notify_list, 2700 blocking_notifier_call_chain(&module_notify_list,
2517 MODULE_STATE_COMING, mod); 2701 MODULE_STATE_COMING, mod);
@@ -2528,9 +2712,7 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
2528 module_put(mod); 2712 module_put(mod);
2529 blocking_notifier_call_chain(&module_notify_list, 2713 blocking_notifier_call_chain(&module_notify_list,
2530 MODULE_STATE_GOING, mod); 2714 MODULE_STATE_GOING, mod);
2531 mutex_lock(&module_mutex);
2532 free_module(mod); 2715 free_module(mod);
2533 mutex_unlock(&module_mutex);
2534 wake_up(&module_wq); 2716 wake_up(&module_wq);
2535 return ret; 2717 return ret;
2536 } 2718 }