ftrace: remove old pointers to mcount
[safe/jmp/linux-2.6] / kernel / module.c
1 /*
2    Copyright (C) 2002 Richard Henderson
3    Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
4
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.
9
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.
14
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
18 */
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/cacheflush.h>
47 #include <linux/license.h>
48 #include <asm/sections.h>
49 #include <linux/tracepoint.h>
50 #include <linux/ftrace.h>
51
52 #if 0
53 #define DEBUGP printk
54 #else
55 #define DEBUGP(fmt , a...)
56 #endif
57
58 #ifndef ARCH_SHF_SMALL
59 #define ARCH_SHF_SMALL 0
60 #endif
61
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))
64
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);
69
70 /* Waiting for a module to finish initializing? */
71 static DECLARE_WAIT_QUEUE_HEAD(module_wq);
72
73 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
74
75 /* Bounds of module allocation, for speeding __module_text_address */
76 static unsigned long module_addr_min = -1UL, module_addr_max = 0;
77
78 int register_module_notifier(struct notifier_block * nb)
79 {
80         return blocking_notifier_chain_register(&module_notify_list, nb);
81 }
82 EXPORT_SYMBOL(register_module_notifier);
83
84 int unregister_module_notifier(struct notifier_block * nb)
85 {
86         return blocking_notifier_chain_unregister(&module_notify_list, nb);
87 }
88 EXPORT_SYMBOL(unregister_module_notifier);
89
90 /* We require a truly strong try_module_get(): 0 means failure due to
91    ongoing or failed initialization etc. */
92 static inline int strong_try_module_get(struct module *mod)
93 {
94         if (mod && mod->state == MODULE_STATE_COMING)
95                 return -EBUSY;
96         if (try_module_get(mod))
97                 return 0;
98         else
99                 return -ENOENT;
100 }
101
102 static inline void add_taint_module(struct module *mod, unsigned flag)
103 {
104         add_taint(flag);
105         mod->taints |= flag;
106 }
107
108 /*
109  * A thread that wants to hold a reference to a module only while it
110  * is running can call this to safely exit.  nfsd and lockd use this.
111  */
112 void __module_put_and_exit(struct module *mod, long code)
113 {
114         module_put(mod);
115         do_exit(code);
116 }
117 EXPORT_SYMBOL(__module_put_and_exit);
118
119 /* Find a module section: 0 means not found. */
120 static unsigned int find_sec(Elf_Ehdr *hdr,
121                              Elf_Shdr *sechdrs,
122                              const char *secstrings,
123                              const char *name)
124 {
125         unsigned int i;
126
127         for (i = 1; i < hdr->e_shnum; i++)
128                 /* Alloc bit cleared means "ignore it." */
129                 if ((sechdrs[i].sh_flags & SHF_ALLOC)
130                     && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
131                         return i;
132         return 0;
133 }
134
135 /* Provided by the linker */
136 extern const struct kernel_symbol __start___ksymtab[];
137 extern const struct kernel_symbol __stop___ksymtab[];
138 extern const struct kernel_symbol __start___ksymtab_gpl[];
139 extern const struct kernel_symbol __stop___ksymtab_gpl[];
140 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
141 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
142 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
143 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
144 extern const unsigned long __start___kcrctab[];
145 extern const unsigned long __start___kcrctab_gpl[];
146 extern const unsigned long __start___kcrctab_gpl_future[];
147 #ifdef CONFIG_UNUSED_SYMBOLS
148 extern const struct kernel_symbol __start___ksymtab_unused[];
149 extern const struct kernel_symbol __stop___ksymtab_unused[];
150 extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
151 extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
152 extern const unsigned long __start___kcrctab_unused[];
153 extern const unsigned long __start___kcrctab_unused_gpl[];
154 #endif
155
156 #ifndef CONFIG_MODVERSIONS
157 #define symversion(base, idx) NULL
158 #else
159 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
160 #endif
161
162 struct symsearch {
163         const struct kernel_symbol *start, *stop;
164         const unsigned long *crcs;
165         enum {
166                 NOT_GPL_ONLY,
167                 GPL_ONLY,
168                 WILL_BE_GPL_ONLY,
169         } licence;
170         bool unused;
171 };
172
173 static bool each_symbol_in_section(const struct symsearch *arr,
174                                    unsigned int arrsize,
175                                    struct module *owner,
176                                    bool (*fn)(const struct symsearch *syms,
177                                               struct module *owner,
178                                               unsigned int symnum, void *data),
179                                    void *data)
180 {
181         unsigned int i, j;
182
183         for (j = 0; j < arrsize; j++) {
184                 for (i = 0; i < arr[j].stop - arr[j].start; i++)
185                         if (fn(&arr[j], owner, i, data))
186                                 return true;
187         }
188
189         return false;
190 }
191
192 /* Returns true as soon as fn returns true, otherwise false. */
193 static bool each_symbol(bool (*fn)(const struct symsearch *arr,
194                                    struct module *owner,
195                                    unsigned int symnum, void *data),
196                         void *data)
197 {
198         struct module *mod;
199         const struct symsearch arr[] = {
200                 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
201                   NOT_GPL_ONLY, false },
202                 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
203                   __start___kcrctab_gpl,
204                   GPL_ONLY, false },
205                 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
206                   __start___kcrctab_gpl_future,
207                   WILL_BE_GPL_ONLY, false },
208 #ifdef CONFIG_UNUSED_SYMBOLS
209                 { __start___ksymtab_unused, __stop___ksymtab_unused,
210                   __start___kcrctab_unused,
211                   NOT_GPL_ONLY, true },
212                 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
213                   __start___kcrctab_unused_gpl,
214                   GPL_ONLY, true },
215 #endif
216         };
217
218         if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
219                 return true;
220
221         list_for_each_entry(mod, &modules, list) {
222                 struct symsearch arr[] = {
223                         { mod->syms, mod->syms + mod->num_syms, mod->crcs,
224                           NOT_GPL_ONLY, false },
225                         { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
226                           mod->gpl_crcs,
227                           GPL_ONLY, false },
228                         { mod->gpl_future_syms,
229                           mod->gpl_future_syms + mod->num_gpl_future_syms,
230                           mod->gpl_future_crcs,
231                           WILL_BE_GPL_ONLY, false },
232 #ifdef CONFIG_UNUSED_SYMBOLS
233                         { mod->unused_syms,
234                           mod->unused_syms + mod->num_unused_syms,
235                           mod->unused_crcs,
236                           NOT_GPL_ONLY, true },
237                         { mod->unused_gpl_syms,
238                           mod->unused_gpl_syms + mod->num_unused_gpl_syms,
239                           mod->unused_gpl_crcs,
240                           GPL_ONLY, true },
241 #endif
242                 };
243
244                 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
245                         return true;
246         }
247         return false;
248 }
249
250 struct find_symbol_arg {
251         /* Input */
252         const char *name;
253         bool gplok;
254         bool warn;
255
256         /* Output */
257         struct module *owner;
258         const unsigned long *crc;
259         unsigned long value;
260 };
261
262 static bool find_symbol_in_section(const struct symsearch *syms,
263                                    struct module *owner,
264                                    unsigned int symnum, void *data)
265 {
266         struct find_symbol_arg *fsa = data;
267
268         if (strcmp(syms->start[symnum].name, fsa->name) != 0)
269                 return false;
270
271         if (!fsa->gplok) {
272                 if (syms->licence == GPL_ONLY)
273                         return false;
274                 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
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", fsa->name);
278                         printk(KERN_WARNING "Please see the file "
279                                "Documentation/feature-removal-schedule.txt "
280                                "in the kernel source tree for more details.\n");
281                 }
282         }
283
284 #ifdef CONFIG_UNUSED_SYMBOLS
285         if (syms->unused && fsa->warn) {
286                 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
287                        "however this module is using it.\n", fsa->name);
288                 printk(KERN_WARNING
289                        "This symbol will go away in the future.\n");
290                 printk(KERN_WARNING
291                        "Please evalute if this is the right api to use and if "
292                        "it really is, submit a report the linux kernel "
293                        "mailinglist together with submitting your code for "
294                        "inclusion.\n");
295         }
296 #endif
297
298         fsa->owner = owner;
299         fsa->crc = symversion(syms->crcs, symnum);
300         fsa->value = syms->start[symnum].value;
301         return true;
302 }
303
304 /* Find a symbol, return value, (optional) crc and (optional) module
305  * which owns it */
306 static unsigned long find_symbol(const char *name,
307                                  struct module **owner,
308                                  const unsigned long **crc,
309                                  bool gplok,
310                                  bool warn)
311 {
312         struct find_symbol_arg fsa;
313
314         fsa.name = name;
315         fsa.gplok = gplok;
316         fsa.warn = warn;
317
318         if (each_symbol(find_symbol_in_section, &fsa)) {
319                 if (owner)
320                         *owner = fsa.owner;
321                 if (crc)
322                         *crc = fsa.crc;
323                 return fsa.value;
324         }
325
326         DEBUGP("Failed to find symbol %s\n", name);
327         return -ENOENT;
328 }
329
330 /* Search for module by name: must hold module_mutex. */
331 static struct module *find_module(const char *name)
332 {
333         struct module *mod;
334
335         list_for_each_entry(mod, &modules, list) {
336                 if (strcmp(mod->name, name) == 0)
337                         return mod;
338         }
339         return NULL;
340 }
341
342 #ifdef CONFIG_SMP
343 /* Number of blocks used and allocated. */
344 static unsigned int pcpu_num_used, pcpu_num_allocated;
345 /* Size of each block.  -ve means used. */
346 static int *pcpu_size;
347
348 static int split_block(unsigned int i, unsigned short size)
349 {
350         /* Reallocation required? */
351         if (pcpu_num_used + 1 > pcpu_num_allocated) {
352                 int *new;
353
354                 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
355                                GFP_KERNEL);
356                 if (!new)
357                         return 0;
358
359                 pcpu_num_allocated *= 2;
360                 pcpu_size = new;
361         }
362
363         /* Insert a new subblock */
364         memmove(&pcpu_size[i+1], &pcpu_size[i],
365                 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
366         pcpu_num_used++;
367
368         pcpu_size[i+1] -= size;
369         pcpu_size[i] = size;
370         return 1;
371 }
372
373 static inline unsigned int block_size(int val)
374 {
375         if (val < 0)
376                 return -val;
377         return val;
378 }
379
380 static void *percpu_modalloc(unsigned long size, unsigned long align,
381                              const char *name)
382 {
383         unsigned long extra;
384         unsigned int i;
385         void *ptr;
386
387         if (align > PAGE_SIZE) {
388                 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
389                        name, align, PAGE_SIZE);
390                 align = PAGE_SIZE;
391         }
392
393         ptr = __per_cpu_start;
394         for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
395                 /* Extra for alignment requirement. */
396                 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
397                 BUG_ON(i == 0 && extra != 0);
398
399                 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
400                         continue;
401
402                 /* Transfer extra to previous block. */
403                 if (pcpu_size[i-1] < 0)
404                         pcpu_size[i-1] -= extra;
405                 else
406                         pcpu_size[i-1] += extra;
407                 pcpu_size[i] -= extra;
408                 ptr += extra;
409
410                 /* Split block if warranted */
411                 if (pcpu_size[i] - size > sizeof(unsigned long))
412                         if (!split_block(i, size))
413                                 return NULL;
414
415                 /* Mark allocated */
416                 pcpu_size[i] = -pcpu_size[i];
417                 return ptr;
418         }
419
420         printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
421                size);
422         return NULL;
423 }
424
425 static void percpu_modfree(void *freeme)
426 {
427         unsigned int i;
428         void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
429
430         /* First entry is core kernel percpu data. */
431         for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
432                 if (ptr == freeme) {
433                         pcpu_size[i] = -pcpu_size[i];
434                         goto free;
435                 }
436         }
437         BUG();
438
439  free:
440         /* Merge with previous? */
441         if (pcpu_size[i-1] >= 0) {
442                 pcpu_size[i-1] += pcpu_size[i];
443                 pcpu_num_used--;
444                 memmove(&pcpu_size[i], &pcpu_size[i+1],
445                         (pcpu_num_used - i) * sizeof(pcpu_size[0]));
446                 i--;
447         }
448         /* Merge with next? */
449         if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
450                 pcpu_size[i] += pcpu_size[i+1];
451                 pcpu_num_used--;
452                 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
453                         (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
454         }
455 }
456
457 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
458                                  Elf_Shdr *sechdrs,
459                                  const char *secstrings)
460 {
461         return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
462 }
463
464 static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
465 {
466         int cpu;
467
468         for_each_possible_cpu(cpu)
469                 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
470 }
471
472 static int percpu_modinit(void)
473 {
474         pcpu_num_used = 2;
475         pcpu_num_allocated = 2;
476         pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
477                             GFP_KERNEL);
478         /* Static in-kernel percpu data (used). */
479         pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
480         /* Free room. */
481         pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
482         if (pcpu_size[1] < 0) {
483                 printk(KERN_ERR "No per-cpu room for modules.\n");
484                 pcpu_num_used = 1;
485         }
486
487         return 0;
488 }
489 __initcall(percpu_modinit);
490 #else /* ... !CONFIG_SMP */
491 static inline void *percpu_modalloc(unsigned long size, unsigned long align,
492                                     const char *name)
493 {
494         return NULL;
495 }
496 static inline void percpu_modfree(void *pcpuptr)
497 {
498         BUG();
499 }
500 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
501                                         Elf_Shdr *sechdrs,
502                                         const char *secstrings)
503 {
504         return 0;
505 }
506 static inline void percpu_modcopy(void *pcpudst, const void *src,
507                                   unsigned long size)
508 {
509         /* pcpusec should be 0, and size of that section should be 0. */
510         BUG_ON(size != 0);
511 }
512 #endif /* CONFIG_SMP */
513
514 #define MODINFO_ATTR(field)     \
515 static void setup_modinfo_##field(struct module *mod, const char *s)  \
516 {                                                                     \
517         mod->field = kstrdup(s, GFP_KERNEL);                          \
518 }                                                                     \
519 static ssize_t show_modinfo_##field(struct module_attribute *mattr,   \
520                         struct module *mod, char *buffer)             \
521 {                                                                     \
522         return sprintf(buffer, "%s\n", mod->field);                   \
523 }                                                                     \
524 static int modinfo_##field##_exists(struct module *mod)               \
525 {                                                                     \
526         return mod->field != NULL;                                    \
527 }                                                                     \
528 static void free_modinfo_##field(struct module *mod)                  \
529 {                                                                     \
530         kfree(mod->field);                                            \
531         mod->field = NULL;                                            \
532 }                                                                     \
533 static struct module_attribute modinfo_##field = {                    \
534         .attr = { .name = __stringify(field), .mode = 0444 },         \
535         .show = show_modinfo_##field,                                 \
536         .setup = setup_modinfo_##field,                               \
537         .test = modinfo_##field##_exists,                             \
538         .free = free_modinfo_##field,                                 \
539 };
540
541 MODINFO_ATTR(version);
542 MODINFO_ATTR(srcversion);
543
544 static char last_unloaded_module[MODULE_NAME_LEN+1];
545
546 #ifdef CONFIG_MODULE_UNLOAD
547 /* Init the unload section of the module. */
548 static void module_unload_init(struct module *mod)
549 {
550         unsigned int i;
551
552         INIT_LIST_HEAD(&mod->modules_which_use_me);
553         for (i = 0; i < NR_CPUS; i++)
554                 local_set(&mod->ref[i].count, 0);
555         /* Hold reference count during initialization. */
556         local_set(&mod->ref[raw_smp_processor_id()].count, 1);
557         /* Backwards compatibility macros put refcount during init. */
558         mod->waiter = current;
559 }
560
561 /* modules using other modules */
562 struct module_use
563 {
564         struct list_head list;
565         struct module *module_which_uses;
566 };
567
568 /* Does a already use b? */
569 static int already_uses(struct module *a, struct module *b)
570 {
571         struct module_use *use;
572
573         list_for_each_entry(use, &b->modules_which_use_me, list) {
574                 if (use->module_which_uses == a) {
575                         DEBUGP("%s uses %s!\n", a->name, b->name);
576                         return 1;
577                 }
578         }
579         DEBUGP("%s does not use %s!\n", a->name, b->name);
580         return 0;
581 }
582
583 /* Module a uses b */
584 static int use_module(struct module *a, struct module *b)
585 {
586         struct module_use *use;
587         int no_warn, err;
588
589         if (b == NULL || already_uses(a, b)) return 1;
590
591         /* If we're interrupted or time out, we fail. */
592         if (wait_event_interruptible_timeout(
593                     module_wq, (err = strong_try_module_get(b)) != -EBUSY,
594                     30 * HZ) <= 0) {
595                 printk("%s: gave up waiting for init of module %s.\n",
596                        a->name, b->name);
597                 return 0;
598         }
599
600         /* If strong_try_module_get() returned a different error, we fail. */
601         if (err)
602                 return 0;
603
604         DEBUGP("Allocating new usage for %s.\n", a->name);
605         use = kmalloc(sizeof(*use), GFP_ATOMIC);
606         if (!use) {
607                 printk("%s: out of memory loading\n", a->name);
608                 module_put(b);
609                 return 0;
610         }
611
612         use->module_which_uses = a;
613         list_add(&use->list, &b->modules_which_use_me);
614         no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
615         return 1;
616 }
617
618 /* Clear the unload stuff of the module. */
619 static void module_unload_free(struct module *mod)
620 {
621         struct module *i;
622
623         list_for_each_entry(i, &modules, list) {
624                 struct module_use *use;
625
626                 list_for_each_entry(use, &i->modules_which_use_me, list) {
627                         if (use->module_which_uses == mod) {
628                                 DEBUGP("%s unusing %s\n", mod->name, i->name);
629                                 module_put(i);
630                                 list_del(&use->list);
631                                 kfree(use);
632                                 sysfs_remove_link(i->holders_dir, mod->name);
633                                 /* There can be at most one match. */
634                                 break;
635                         }
636                 }
637         }
638 }
639
640 #ifdef CONFIG_MODULE_FORCE_UNLOAD
641 static inline int try_force_unload(unsigned int flags)
642 {
643         int ret = (flags & O_TRUNC);
644         if (ret)
645                 add_taint(TAINT_FORCED_RMMOD);
646         return ret;
647 }
648 #else
649 static inline int try_force_unload(unsigned int flags)
650 {
651         return 0;
652 }
653 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
654
655 struct stopref
656 {
657         struct module *mod;
658         int flags;
659         int *forced;
660 };
661
662 /* Whole machine is stopped with interrupts off when this runs. */
663 static int __try_stop_module(void *_sref)
664 {
665         struct stopref *sref = _sref;
666
667         /* If it's not unused, quit unless we're forcing. */
668         if (module_refcount(sref->mod) != 0) {
669                 if (!(*sref->forced = try_force_unload(sref->flags)))
670                         return -EWOULDBLOCK;
671         }
672
673         /* Mark it as dying. */
674         sref->mod->state = MODULE_STATE_GOING;
675         return 0;
676 }
677
678 static int try_stop_module(struct module *mod, int flags, int *forced)
679 {
680         if (flags & O_NONBLOCK) {
681                 struct stopref sref = { mod, flags, forced };
682
683                 return stop_machine(__try_stop_module, &sref, NULL);
684         } else {
685                 /* We don't need to stop the machine for this. */
686                 mod->state = MODULE_STATE_GOING;
687                 synchronize_sched();
688                 return 0;
689         }
690 }
691
692 unsigned int module_refcount(struct module *mod)
693 {
694         unsigned int i, total = 0;
695
696         for (i = 0; i < NR_CPUS; i++)
697                 total += local_read(&mod->ref[i].count);
698         return total;
699 }
700 EXPORT_SYMBOL(module_refcount);
701
702 /* This exists whether we can unload or not */
703 static void free_module(struct module *mod);
704
705 static void wait_for_zero_refcount(struct module *mod)
706 {
707         /* Since we might sleep for some time, release the mutex first */
708         mutex_unlock(&module_mutex);
709         for (;;) {
710                 DEBUGP("Looking at refcount...\n");
711                 set_current_state(TASK_UNINTERRUPTIBLE);
712                 if (module_refcount(mod) == 0)
713                         break;
714                 schedule();
715         }
716         current->state = TASK_RUNNING;
717         mutex_lock(&module_mutex);
718 }
719
720 asmlinkage long
721 sys_delete_module(const char __user *name_user, unsigned int flags)
722 {
723         struct module *mod;
724         char name[MODULE_NAME_LEN];
725         int ret, forced = 0;
726
727         if (!capable(CAP_SYS_MODULE))
728                 return -EPERM;
729
730         if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
731                 return -EFAULT;
732         name[MODULE_NAME_LEN-1] = '\0';
733
734         if (mutex_lock_interruptible(&module_mutex) != 0)
735                 return -EINTR;
736
737         mod = find_module(name);
738         if (!mod) {
739                 ret = -ENOENT;
740                 goto out;
741         }
742
743         if (!list_empty(&mod->modules_which_use_me)) {
744                 /* Other modules depend on us: get rid of them first. */
745                 ret = -EWOULDBLOCK;
746                 goto out;
747         }
748
749         /* Doing init or already dying? */
750         if (mod->state != MODULE_STATE_LIVE) {
751                 /* FIXME: if (force), slam module count and wake up
752                    waiter --RR */
753                 DEBUGP("%s already dying\n", mod->name);
754                 ret = -EBUSY;
755                 goto out;
756         }
757
758         /* If it has an init func, it must have an exit func to unload */
759         if (mod->init && !mod->exit) {
760                 forced = try_force_unload(flags);
761                 if (!forced) {
762                         /* This module can't be removed */
763                         ret = -EBUSY;
764                         goto out;
765                 }
766         }
767
768         /* Set this up before setting mod->state */
769         mod->waiter = current;
770
771         /* Stop the machine so refcounts can't move and disable module. */
772         ret = try_stop_module(mod, flags, &forced);
773         if (ret != 0)
774                 goto out;
775
776         /* Never wait if forced. */
777         if (!forced && module_refcount(mod) != 0)
778                 wait_for_zero_refcount(mod);
779
780         mutex_unlock(&module_mutex);
781         /* Final destruction now noone is using it. */
782         if (mod->exit != NULL)
783                 mod->exit();
784         blocking_notifier_call_chain(&module_notify_list,
785                                      MODULE_STATE_GOING, mod);
786         mutex_lock(&module_mutex);
787         /* Store the name of the last unloaded module for diagnostic purposes */
788         strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
789         free_module(mod);
790
791  out:
792         mutex_unlock(&module_mutex);
793         return ret;
794 }
795
796 static void print_unload_info(struct seq_file *m, struct module *mod)
797 {
798         struct module_use *use;
799         int printed_something = 0;
800
801         seq_printf(m, " %u ", module_refcount(mod));
802
803         /* Always include a trailing , so userspace can differentiate
804            between this and the old multi-field proc format. */
805         list_for_each_entry(use, &mod->modules_which_use_me, list) {
806                 printed_something = 1;
807                 seq_printf(m, "%s,", use->module_which_uses->name);
808         }
809
810         if (mod->init != NULL && mod->exit == NULL) {
811                 printed_something = 1;
812                 seq_printf(m, "[permanent],");
813         }
814
815         if (!printed_something)
816                 seq_printf(m, "-");
817 }
818
819 void __symbol_put(const char *symbol)
820 {
821         struct module *owner;
822
823         preempt_disable();
824         if (IS_ERR_VALUE(find_symbol(symbol, &owner, NULL, true, false)))
825                 BUG();
826         module_put(owner);
827         preempt_enable();
828 }
829 EXPORT_SYMBOL(__symbol_put);
830
831 void symbol_put_addr(void *addr)
832 {
833         struct module *modaddr;
834
835         if (core_kernel_text((unsigned long)addr))
836                 return;
837
838         if (!(modaddr = module_text_address((unsigned long)addr)))
839                 BUG();
840         module_put(modaddr);
841 }
842 EXPORT_SYMBOL_GPL(symbol_put_addr);
843
844 static ssize_t show_refcnt(struct module_attribute *mattr,
845                            struct module *mod, char *buffer)
846 {
847         return sprintf(buffer, "%u\n", module_refcount(mod));
848 }
849
850 static struct module_attribute refcnt = {
851         .attr = { .name = "refcnt", .mode = 0444 },
852         .show = show_refcnt,
853 };
854
855 void module_put(struct module *module)
856 {
857         if (module) {
858                 unsigned int cpu = get_cpu();
859                 local_dec(&module->ref[cpu].count);
860                 /* Maybe they're waiting for us to drop reference? */
861                 if (unlikely(!module_is_live(module)))
862                         wake_up_process(module->waiter);
863                 put_cpu();
864         }
865 }
866 EXPORT_SYMBOL(module_put);
867
868 #else /* !CONFIG_MODULE_UNLOAD */
869 static void print_unload_info(struct seq_file *m, struct module *mod)
870 {
871         /* We don't know the usage count, or what modules are using. */
872         seq_printf(m, " - -");
873 }
874
875 static inline void module_unload_free(struct module *mod)
876 {
877 }
878
879 static inline int use_module(struct module *a, struct module *b)
880 {
881         return strong_try_module_get(b) == 0;
882 }
883
884 static inline void module_unload_init(struct module *mod)
885 {
886 }
887 #endif /* CONFIG_MODULE_UNLOAD */
888
889 static ssize_t show_initstate(struct module_attribute *mattr,
890                            struct module *mod, char *buffer)
891 {
892         const char *state = "unknown";
893
894         switch (mod->state) {
895         case MODULE_STATE_LIVE:
896                 state = "live";
897                 break;
898         case MODULE_STATE_COMING:
899                 state = "coming";
900                 break;
901         case MODULE_STATE_GOING:
902                 state = "going";
903                 break;
904         }
905         return sprintf(buffer, "%s\n", state);
906 }
907
908 static struct module_attribute initstate = {
909         .attr = { .name = "initstate", .mode = 0444 },
910         .show = show_initstate,
911 };
912
913 static struct module_attribute *modinfo_attrs[] = {
914         &modinfo_version,
915         &modinfo_srcversion,
916         &initstate,
917 #ifdef CONFIG_MODULE_UNLOAD
918         &refcnt,
919 #endif
920         NULL,
921 };
922
923 static const char vermagic[] = VERMAGIC_STRING;
924
925 static int try_to_force_load(struct module *mod, const char *symname)
926 {
927 #ifdef CONFIG_MODULE_FORCE_LOAD
928         if (!(tainted & TAINT_FORCED_MODULE))
929                 printk("%s: no version for \"%s\" found: kernel tainted.\n",
930                        mod->name, symname);
931         add_taint_module(mod, TAINT_FORCED_MODULE);
932         return 0;
933 #else
934         return -ENOEXEC;
935 #endif
936 }
937
938 #ifdef CONFIG_MODVERSIONS
939 static int check_version(Elf_Shdr *sechdrs,
940                          unsigned int versindex,
941                          const char *symname,
942                          struct module *mod, 
943                          const unsigned long *crc)
944 {
945         unsigned int i, num_versions;
946         struct modversion_info *versions;
947
948         /* Exporting module didn't supply crcs?  OK, we're already tainted. */
949         if (!crc)
950                 return 1;
951
952         /* No versions at all?  modprobe --force does this. */
953         if (versindex == 0)
954                 return try_to_force_load(mod, symname) == 0;
955
956         versions = (void *) sechdrs[versindex].sh_addr;
957         num_versions = sechdrs[versindex].sh_size
958                 / sizeof(struct modversion_info);
959
960         for (i = 0; i < num_versions; i++) {
961                 if (strcmp(versions[i].name, symname) != 0)
962                         continue;
963
964                 if (versions[i].crc == *crc)
965                         return 1;
966                 DEBUGP("Found checksum %lX vs module %lX\n",
967                        *crc, versions[i].crc);
968                 goto bad_version;
969         }
970
971         printk(KERN_WARNING "%s: no symbol version for %s\n",
972                mod->name, symname);
973         return 0;
974
975 bad_version:
976         printk("%s: disagrees about version of symbol %s\n",
977                mod->name, symname);
978         return 0;
979 }
980
981 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
982                                           unsigned int versindex,
983                                           struct module *mod)
984 {
985         const unsigned long *crc;
986
987         if (IS_ERR_VALUE(find_symbol("struct_module", NULL, &crc, true, false)))
988                 BUG();
989         return check_version(sechdrs, versindex, "struct_module", mod, crc);
990 }
991
992 /* First part is kernel version, which we ignore if module has crcs. */
993 static inline int same_magic(const char *amagic, const char *bmagic,
994                              bool has_crcs)
995 {
996         if (has_crcs) {
997                 amagic += strcspn(amagic, " ");
998                 bmagic += strcspn(bmagic, " ");
999         }
1000         return strcmp(amagic, bmagic) == 0;
1001 }
1002 #else
1003 static inline int check_version(Elf_Shdr *sechdrs,
1004                                 unsigned int versindex,
1005                                 const char *symname,
1006                                 struct module *mod, 
1007                                 const unsigned long *crc)
1008 {
1009         return 1;
1010 }
1011
1012 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1013                                           unsigned int versindex,
1014                                           struct module *mod)
1015 {
1016         return 1;
1017 }
1018
1019 static inline int same_magic(const char *amagic, const char *bmagic,
1020                              bool has_crcs)
1021 {
1022         return strcmp(amagic, bmagic) == 0;
1023 }
1024 #endif /* CONFIG_MODVERSIONS */
1025
1026 /* Resolve a symbol for this module.  I.e. if we find one, record usage.
1027    Must be holding module_mutex. */
1028 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
1029                                     unsigned int versindex,
1030                                     const char *name,
1031                                     struct module *mod)
1032 {
1033         struct module *owner;
1034         unsigned long ret;
1035         const unsigned long *crc;
1036
1037         ret = find_symbol(name, &owner, &crc,
1038                           !(mod->taints & TAINT_PROPRIETARY_MODULE), true);
1039         if (!IS_ERR_VALUE(ret)) {
1040                 /* use_module can fail due to OOM,
1041                    or module initialization or unloading */
1042                 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1043                     !use_module(mod, owner))
1044                         ret = -EINVAL;
1045         }
1046         return ret;
1047 }
1048
1049 /*
1050  * /sys/module/foo/sections stuff
1051  * J. Corbet <corbet@lwn.net>
1052  */
1053 #if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
1054 struct module_sect_attr
1055 {
1056         struct module_attribute mattr;
1057         char *name;
1058         unsigned long address;
1059 };
1060
1061 struct module_sect_attrs
1062 {
1063         struct attribute_group grp;
1064         unsigned int nsections;
1065         struct module_sect_attr attrs[0];
1066 };
1067
1068 static ssize_t module_sect_show(struct module_attribute *mattr,
1069                                 struct module *mod, char *buf)
1070 {
1071         struct module_sect_attr *sattr =
1072                 container_of(mattr, struct module_sect_attr, mattr);
1073         return sprintf(buf, "0x%lx\n", sattr->address);
1074 }
1075
1076 static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1077 {
1078         unsigned int section;
1079
1080         for (section = 0; section < sect_attrs->nsections; section++)
1081                 kfree(sect_attrs->attrs[section].name);
1082         kfree(sect_attrs);
1083 }
1084
1085 static void add_sect_attrs(struct module *mod, unsigned int nsect,
1086                 char *secstrings, Elf_Shdr *sechdrs)
1087 {
1088         unsigned int nloaded = 0, i, size[2];
1089         struct module_sect_attrs *sect_attrs;
1090         struct module_sect_attr *sattr;
1091         struct attribute **gattr;
1092
1093         /* Count loaded sections and allocate structures */
1094         for (i = 0; i < nsect; i++)
1095                 if (sechdrs[i].sh_flags & SHF_ALLOC)
1096                         nloaded++;
1097         size[0] = ALIGN(sizeof(*sect_attrs)
1098                         + nloaded * sizeof(sect_attrs->attrs[0]),
1099                         sizeof(sect_attrs->grp.attrs[0]));
1100         size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
1101         sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1102         if (sect_attrs == NULL)
1103                 return;
1104
1105         /* Setup section attributes. */
1106         sect_attrs->grp.name = "sections";
1107         sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1108
1109         sect_attrs->nsections = 0;
1110         sattr = &sect_attrs->attrs[0];
1111         gattr = &sect_attrs->grp.attrs[0];
1112         for (i = 0; i < nsect; i++) {
1113                 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1114                         continue;
1115                 sattr->address = sechdrs[i].sh_addr;
1116                 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1117                                         GFP_KERNEL);
1118                 if (sattr->name == NULL)
1119                         goto out;
1120                 sect_attrs->nsections++;
1121                 sattr->mattr.show = module_sect_show;
1122                 sattr->mattr.store = NULL;
1123                 sattr->mattr.attr.name = sattr->name;
1124                 sattr->mattr.attr.mode = S_IRUGO;
1125                 *(gattr++) = &(sattr++)->mattr.attr;
1126         }
1127         *gattr = NULL;
1128
1129         if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1130                 goto out;
1131
1132         mod->sect_attrs = sect_attrs;
1133         return;
1134   out:
1135         free_sect_attrs(sect_attrs);
1136 }
1137
1138 static void remove_sect_attrs(struct module *mod)
1139 {
1140         if (mod->sect_attrs) {
1141                 sysfs_remove_group(&mod->mkobj.kobj,
1142                                    &mod->sect_attrs->grp);
1143                 /* We are positive that no one is using any sect attrs
1144                  * at this point.  Deallocate immediately. */
1145                 free_sect_attrs(mod->sect_attrs);
1146                 mod->sect_attrs = NULL;
1147         }
1148 }
1149
1150 /*
1151  * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1152  */
1153
1154 struct module_notes_attrs {
1155         struct kobject *dir;
1156         unsigned int notes;
1157         struct bin_attribute attrs[0];
1158 };
1159
1160 static ssize_t module_notes_read(struct kobject *kobj,
1161                                  struct bin_attribute *bin_attr,
1162                                  char *buf, loff_t pos, size_t count)
1163 {
1164         /*
1165          * The caller checked the pos and count against our size.
1166          */
1167         memcpy(buf, bin_attr->private + pos, count);
1168         return count;
1169 }
1170
1171 static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1172                              unsigned int i)
1173 {
1174         if (notes_attrs->dir) {
1175                 while (i-- > 0)
1176                         sysfs_remove_bin_file(notes_attrs->dir,
1177                                               &notes_attrs->attrs[i]);
1178                 kobject_del(notes_attrs->dir);
1179         }
1180         kfree(notes_attrs);
1181 }
1182
1183 static void add_notes_attrs(struct module *mod, unsigned int nsect,
1184                             char *secstrings, Elf_Shdr *sechdrs)
1185 {
1186         unsigned int notes, loaded, i;
1187         struct module_notes_attrs *notes_attrs;
1188         struct bin_attribute *nattr;
1189
1190         /* Count notes sections and allocate structures.  */
1191         notes = 0;
1192         for (i = 0; i < nsect; i++)
1193                 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1194                     (sechdrs[i].sh_type == SHT_NOTE))
1195                         ++notes;
1196
1197         if (notes == 0)
1198                 return;
1199
1200         notes_attrs = kzalloc(sizeof(*notes_attrs)
1201                               + notes * sizeof(notes_attrs->attrs[0]),
1202                               GFP_KERNEL);
1203         if (notes_attrs == NULL)
1204                 return;
1205
1206         notes_attrs->notes = notes;
1207         nattr = &notes_attrs->attrs[0];
1208         for (loaded = i = 0; i < nsect; ++i) {
1209                 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1210                         continue;
1211                 if (sechdrs[i].sh_type == SHT_NOTE) {
1212                         nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1213                         nattr->attr.mode = S_IRUGO;
1214                         nattr->size = sechdrs[i].sh_size;
1215                         nattr->private = (void *) sechdrs[i].sh_addr;
1216                         nattr->read = module_notes_read;
1217                         ++nattr;
1218                 }
1219                 ++loaded;
1220         }
1221
1222         notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
1223         if (!notes_attrs->dir)
1224                 goto out;
1225
1226         for (i = 0; i < notes; ++i)
1227                 if (sysfs_create_bin_file(notes_attrs->dir,
1228                                           &notes_attrs->attrs[i]))
1229                         goto out;
1230
1231         mod->notes_attrs = notes_attrs;
1232         return;
1233
1234   out:
1235         free_notes_attrs(notes_attrs, i);
1236 }
1237
1238 static void remove_notes_attrs(struct module *mod)
1239 {
1240         if (mod->notes_attrs)
1241                 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1242 }
1243
1244 #else
1245
1246 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1247                 char *sectstrings, Elf_Shdr *sechdrs)
1248 {
1249 }
1250
1251 static inline void remove_sect_attrs(struct module *mod)
1252 {
1253 }
1254
1255 static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1256                                    char *sectstrings, Elf_Shdr *sechdrs)
1257 {
1258 }
1259
1260 static inline void remove_notes_attrs(struct module *mod)
1261 {
1262 }
1263 #endif
1264
1265 #ifdef CONFIG_SYSFS
1266 int module_add_modinfo_attrs(struct module *mod)
1267 {
1268         struct module_attribute *attr;
1269         struct module_attribute *temp_attr;
1270         int error = 0;
1271         int i;
1272
1273         mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1274                                         (ARRAY_SIZE(modinfo_attrs) + 1)),
1275                                         GFP_KERNEL);
1276         if (!mod->modinfo_attrs)
1277                 return -ENOMEM;
1278
1279         temp_attr = mod->modinfo_attrs;
1280         for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1281                 if (!attr->test ||
1282                     (attr->test && attr->test(mod))) {
1283                         memcpy(temp_attr, attr, sizeof(*temp_attr));
1284                         error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1285                         ++temp_attr;
1286                 }
1287         }
1288         return error;
1289 }
1290
1291 void module_remove_modinfo_attrs(struct module *mod)
1292 {
1293         struct module_attribute *attr;
1294         int i;
1295
1296         for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1297                 /* pick a field to test for end of list */
1298                 if (!attr->attr.name)
1299                         break;
1300                 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1301                 if (attr->free)
1302                         attr->free(mod);
1303         }
1304         kfree(mod->modinfo_attrs);
1305 }
1306
1307 int mod_sysfs_init(struct module *mod)
1308 {
1309         int err;
1310         struct kobject *kobj;
1311
1312         if (!module_sysfs_initialized) {
1313                 printk(KERN_ERR "%s: module sysfs not initialized\n",
1314                        mod->name);
1315                 err = -EINVAL;
1316                 goto out;
1317         }
1318
1319         kobj = kset_find_obj(module_kset, mod->name);
1320         if (kobj) {
1321                 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1322                 kobject_put(kobj);
1323                 err = -EINVAL;
1324                 goto out;
1325         }
1326
1327         mod->mkobj.mod = mod;
1328
1329         memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1330         mod->mkobj.kobj.kset = module_kset;
1331         err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1332                                    "%s", mod->name);
1333         if (err)
1334                 kobject_put(&mod->mkobj.kobj);
1335
1336         /* delay uevent until full sysfs population */
1337 out:
1338         return err;
1339 }
1340
1341 int mod_sysfs_setup(struct module *mod,
1342                            struct kernel_param *kparam,
1343                            unsigned int num_params)
1344 {
1345         int err;
1346
1347         mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1348         if (!mod->holders_dir) {
1349                 err = -ENOMEM;
1350                 goto out_unreg;
1351         }
1352
1353         err = module_param_sysfs_setup(mod, kparam, num_params);
1354         if (err)
1355                 goto out_unreg_holders;
1356
1357         err = module_add_modinfo_attrs(mod);
1358         if (err)
1359                 goto out_unreg_param;
1360
1361         kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1362         return 0;
1363
1364 out_unreg_param:
1365         module_param_sysfs_remove(mod);
1366 out_unreg_holders:
1367         kobject_put(mod->holders_dir);
1368 out_unreg:
1369         kobject_put(&mod->mkobj.kobj);
1370         return err;
1371 }
1372
1373 static void mod_sysfs_fini(struct module *mod)
1374 {
1375         kobject_put(&mod->mkobj.kobj);
1376 }
1377
1378 #else /* CONFIG_SYSFS */
1379
1380 static void mod_sysfs_fini(struct module *mod)
1381 {
1382 }
1383
1384 #endif /* CONFIG_SYSFS */
1385
1386 static void mod_kobject_remove(struct module *mod)
1387 {
1388         module_remove_modinfo_attrs(mod);
1389         module_param_sysfs_remove(mod);
1390         kobject_put(mod->mkobj.drivers_dir);
1391         kobject_put(mod->holders_dir);
1392         mod_sysfs_fini(mod);
1393 }
1394
1395 /*
1396  * link the module with the whole machine is stopped with interrupts off
1397  * - this defends against kallsyms not taking locks
1398  */
1399 static int __link_module(void *_mod)
1400 {
1401         struct module *mod = _mod;
1402         list_add(&mod->list, &modules);
1403         return 0;
1404 }
1405
1406 /*
1407  * unlink the module with the whole machine is stopped with interrupts off
1408  * - this defends against kallsyms not taking locks
1409  */
1410 static int __unlink_module(void *_mod)
1411 {
1412         struct module *mod = _mod;
1413         list_del(&mod->list);
1414         return 0;
1415 }
1416
1417 /* Free a module, remove from lists, etc (must hold module_mutex). */
1418 static void free_module(struct module *mod)
1419 {
1420         /* Delete from various lists */
1421         stop_machine(__unlink_module, mod, NULL);
1422         remove_notes_attrs(mod);
1423         remove_sect_attrs(mod);
1424         mod_kobject_remove(mod);
1425
1426         unwind_remove_table(mod->unwind_info, 0);
1427
1428         /* Arch-specific cleanup. */
1429         module_arch_cleanup(mod);
1430
1431         /* Module unload stuff */
1432         module_unload_free(mod);
1433
1434         /* release any pointers to mcount in this module */
1435         ftrace_release(mod->module_core, mod->core_size);
1436
1437         /* This may be NULL, but that's OK */
1438         module_free(mod, mod->module_init);
1439         kfree(mod->args);
1440         if (mod->percpu)
1441                 percpu_modfree(mod->percpu);
1442
1443         /* Free lock-classes: */
1444         lockdep_free_key_range(mod->module_core, mod->core_size);
1445
1446         /* Finally, free the core (containing the module structure) */
1447         module_free(mod, mod->module_core);
1448 }
1449
1450 void *__symbol_get(const char *symbol)
1451 {
1452         struct module *owner;
1453         unsigned long value;
1454
1455         preempt_disable();
1456         value = find_symbol(symbol, &owner, NULL, true, true);
1457         if (IS_ERR_VALUE(value))
1458                 value = 0;
1459         else if (strong_try_module_get(owner))
1460                 value = 0;
1461         preempt_enable();
1462
1463         return (void *)value;
1464 }
1465 EXPORT_SYMBOL_GPL(__symbol_get);
1466
1467 /*
1468  * Ensure that an exported symbol [global namespace] does not already exist
1469  * in the kernel or in some other module's exported symbol table.
1470  */
1471 static int verify_export_symbols(struct module *mod)
1472 {
1473         unsigned int i;
1474         struct module *owner;
1475         const struct kernel_symbol *s;
1476         struct {
1477                 const struct kernel_symbol *sym;
1478                 unsigned int num;
1479         } arr[] = {
1480                 { mod->syms, mod->num_syms },
1481                 { mod->gpl_syms, mod->num_gpl_syms },
1482                 { mod->gpl_future_syms, mod->num_gpl_future_syms },
1483 #ifdef CONFIG_UNUSED_SYMBOLS
1484                 { mod->unused_syms, mod->num_unused_syms },
1485                 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
1486 #endif
1487         };
1488
1489         for (i = 0; i < ARRAY_SIZE(arr); i++) {
1490                 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1491                         if (!IS_ERR_VALUE(find_symbol(s->name, &owner,
1492                                                       NULL, true, false))) {
1493                                 printk(KERN_ERR
1494                                        "%s: exports duplicate symbol %s"
1495                                        " (owned by %s)\n",
1496                                        mod->name, s->name, module_name(owner));
1497                                 return -ENOEXEC;
1498                         }
1499                 }
1500         }
1501         return 0;
1502 }
1503
1504 /* Change all symbols so that st_value encodes the pointer directly. */
1505 static int simplify_symbols(Elf_Shdr *sechdrs,
1506                             unsigned int symindex,
1507                             const char *strtab,
1508                             unsigned int versindex,
1509                             unsigned int pcpuindex,
1510                             struct module *mod)
1511 {
1512         Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1513         unsigned long secbase;
1514         unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1515         int ret = 0;
1516
1517         for (i = 1; i < n; i++) {
1518                 switch (sym[i].st_shndx) {
1519                 case SHN_COMMON:
1520                         /* We compiled with -fno-common.  These are not
1521                            supposed to happen.  */
1522                         DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1523                         printk("%s: please compile with -fno-common\n",
1524                                mod->name);
1525                         ret = -ENOEXEC;
1526                         break;
1527
1528                 case SHN_ABS:
1529                         /* Don't need to do anything */
1530                         DEBUGP("Absolute symbol: 0x%08lx\n",
1531                                (long)sym[i].st_value);
1532                         break;
1533
1534                 case SHN_UNDEF:
1535                         sym[i].st_value
1536                           = resolve_symbol(sechdrs, versindex,
1537                                            strtab + sym[i].st_name, mod);
1538
1539                         /* Ok if resolved.  */
1540                         if (!IS_ERR_VALUE(sym[i].st_value))
1541                                 break;
1542                         /* Ok if weak.  */
1543                         if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1544                                 break;
1545
1546                         printk(KERN_WARNING "%s: Unknown symbol %s\n",
1547                                mod->name, strtab + sym[i].st_name);
1548                         ret = -ENOENT;
1549                         break;
1550
1551                 default:
1552                         /* Divert to percpu allocation if a percpu var. */
1553                         if (sym[i].st_shndx == pcpuindex)
1554                                 secbase = (unsigned long)mod->percpu;
1555                         else
1556                                 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1557                         sym[i].st_value += secbase;
1558                         break;
1559                 }
1560         }
1561
1562         return ret;
1563 }
1564
1565 /* Update size with this section: return offset. */
1566 static long get_offset(unsigned int *size, Elf_Shdr *sechdr)
1567 {
1568         long ret;
1569
1570         ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1571         *size = ret + sechdr->sh_size;
1572         return ret;
1573 }
1574
1575 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1576    might -- code, read-only data, read-write data, small data.  Tally
1577    sizes, and place the offsets into sh_entsize fields: high bit means it
1578    belongs in init. */
1579 static void layout_sections(struct module *mod,
1580                             const Elf_Ehdr *hdr,
1581                             Elf_Shdr *sechdrs,
1582                             const char *secstrings)
1583 {
1584         static unsigned long const masks[][2] = {
1585                 /* NOTE: all executable code must be the first section
1586                  * in this array; otherwise modify the text_size
1587                  * finder in the two loops below */
1588                 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1589                 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1590                 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1591                 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1592         };
1593         unsigned int m, i;
1594
1595         for (i = 0; i < hdr->e_shnum; i++)
1596                 sechdrs[i].sh_entsize = ~0UL;
1597
1598         DEBUGP("Core section allocation order:\n");
1599         for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1600                 for (i = 0; i < hdr->e_shnum; ++i) {
1601                         Elf_Shdr *s = &sechdrs[i];
1602
1603                         if ((s->sh_flags & masks[m][0]) != masks[m][0]
1604                             || (s->sh_flags & masks[m][1])
1605                             || s->sh_entsize != ~0UL
1606                             || strncmp(secstrings + s->sh_name,
1607                                        ".init", 5) == 0)
1608                                 continue;
1609                         s->sh_entsize = get_offset(&mod->core_size, s);
1610                         DEBUGP("\t%s\n", secstrings + s->sh_name);
1611                 }
1612                 if (m == 0)
1613                         mod->core_text_size = mod->core_size;
1614         }
1615
1616         DEBUGP("Init section allocation order:\n");
1617         for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1618                 for (i = 0; i < hdr->e_shnum; ++i) {
1619                         Elf_Shdr *s = &sechdrs[i];
1620
1621                         if ((s->sh_flags & masks[m][0]) != masks[m][0]
1622                             || (s->sh_flags & masks[m][1])
1623                             || s->sh_entsize != ~0UL
1624                             || strncmp(secstrings + s->sh_name,
1625                                        ".init", 5) != 0)
1626                                 continue;
1627                         s->sh_entsize = (get_offset(&mod->init_size, s)
1628                                          | INIT_OFFSET_MASK);
1629                         DEBUGP("\t%s\n", secstrings + s->sh_name);
1630                 }
1631                 if (m == 0)
1632                         mod->init_text_size = mod->init_size;
1633         }
1634 }
1635
1636 static void set_license(struct module *mod, const char *license)
1637 {
1638         if (!license)
1639                 license = "unspecified";
1640
1641         if (!license_is_gpl_compatible(license)) {
1642                 if (!(tainted & TAINT_PROPRIETARY_MODULE))
1643                         printk(KERN_WARNING "%s: module license '%s' taints "
1644                                 "kernel.\n", mod->name, license);
1645                 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
1646         }
1647 }
1648
1649 /* Parse tag=value strings from .modinfo section */
1650 static char *next_string(char *string, unsigned long *secsize)
1651 {
1652         /* Skip non-zero chars */
1653         while (string[0]) {
1654                 string++;
1655                 if ((*secsize)-- <= 1)
1656                         return NULL;
1657         }
1658
1659         /* Skip any zero padding. */
1660         while (!string[0]) {
1661                 string++;
1662                 if ((*secsize)-- <= 1)
1663                         return NULL;
1664         }
1665         return string;
1666 }
1667
1668 static char *get_modinfo(Elf_Shdr *sechdrs,
1669                          unsigned int info,
1670                          const char *tag)
1671 {
1672         char *p;
1673         unsigned int taglen = strlen(tag);
1674         unsigned long size = sechdrs[info].sh_size;
1675
1676         for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1677                 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1678                         return p + taglen + 1;
1679         }
1680         return NULL;
1681 }
1682
1683 static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1684                           unsigned int infoindex)
1685 {
1686         struct module_attribute *attr;
1687         int i;
1688
1689         for (i = 0; (attr = modinfo_attrs[i]); i++) {
1690                 if (attr->setup)
1691                         attr->setup(mod,
1692                                     get_modinfo(sechdrs,
1693                                                 infoindex,
1694                                                 attr->attr.name));
1695         }
1696 }
1697
1698 #ifdef CONFIG_KALLSYMS
1699
1700 /* lookup symbol in given range of kernel_symbols */
1701 static const struct kernel_symbol *lookup_symbol(const char *name,
1702         const struct kernel_symbol *start,
1703         const struct kernel_symbol *stop)
1704 {
1705         const struct kernel_symbol *ks = start;
1706         for (; ks < stop; ks++)
1707                 if (strcmp(ks->name, name) == 0)
1708                         return ks;
1709         return NULL;
1710 }
1711
1712 static int is_exported(const char *name, const struct module *mod)
1713 {
1714         if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
1715                 return 1;
1716         else
1717                 if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
1718                         return 1;
1719                 else
1720                         return 0;
1721 }
1722
1723 /* As per nm */
1724 static char elf_type(const Elf_Sym *sym,
1725                      Elf_Shdr *sechdrs,
1726                      const char *secstrings,
1727                      struct module *mod)
1728 {
1729         if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1730                 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1731                         return 'v';
1732                 else
1733                         return 'w';
1734         }
1735         if (sym->st_shndx == SHN_UNDEF)
1736                 return 'U';
1737         if (sym->st_shndx == SHN_ABS)
1738                 return 'a';
1739         if (sym->st_shndx >= SHN_LORESERVE)
1740                 return '?';
1741         if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1742                 return 't';
1743         if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1744             && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1745                 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1746                         return 'r';
1747                 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1748                         return 'g';
1749                 else
1750                         return 'd';
1751         }
1752         if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1753                 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1754                         return 's';
1755                 else
1756                         return 'b';
1757         }
1758         if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1759                     ".debug", strlen(".debug")) == 0)
1760                 return 'n';
1761         return '?';
1762 }
1763
1764 static void add_kallsyms(struct module *mod,
1765                          Elf_Shdr *sechdrs,
1766                          unsigned int symindex,
1767                          unsigned int strindex,
1768                          const char *secstrings)
1769 {
1770         unsigned int i;
1771
1772         mod->symtab = (void *)sechdrs[symindex].sh_addr;
1773         mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1774         mod->strtab = (void *)sechdrs[strindex].sh_addr;
1775
1776         /* Set types up while we still have access to sections. */
1777         for (i = 0; i < mod->num_symtab; i++)
1778                 mod->symtab[i].st_info
1779                         = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1780 }
1781 #else
1782 static inline void add_kallsyms(struct module *mod,
1783                                 Elf_Shdr *sechdrs,
1784                                 unsigned int symindex,
1785                                 unsigned int strindex,
1786                                 const char *secstrings)
1787 {
1788 }
1789 #endif /* CONFIG_KALLSYMS */
1790
1791 static void *module_alloc_update_bounds(unsigned long size)
1792 {
1793         void *ret = module_alloc(size);
1794
1795         if (ret) {
1796                 /* Update module bounds. */
1797                 if ((unsigned long)ret < module_addr_min)
1798                         module_addr_min = (unsigned long)ret;
1799                 if ((unsigned long)ret + size > module_addr_max)
1800                         module_addr_max = (unsigned long)ret + size;
1801         }
1802         return ret;
1803 }
1804
1805 /* Allocate and load the module: note that size of section 0 is always
1806    zero, and we rely on this for optional sections. */
1807 static noinline struct module *load_module(void __user *umod,
1808                                   unsigned long len,
1809                                   const char __user *uargs)
1810 {
1811         Elf_Ehdr *hdr;
1812         Elf_Shdr *sechdrs;
1813         char *secstrings, *args, *modmagic, *strtab = NULL;
1814         unsigned int i;
1815         unsigned int symindex = 0;
1816         unsigned int strindex = 0;
1817         unsigned int setupindex;
1818         unsigned int exindex;
1819         unsigned int exportindex;
1820         unsigned int modindex;
1821         unsigned int obsparmindex;
1822         unsigned int infoindex;
1823         unsigned int gplindex;
1824         unsigned int crcindex;
1825         unsigned int gplcrcindex;
1826         unsigned int versindex;
1827         unsigned int pcpuindex;
1828         unsigned int gplfutureindex;
1829         unsigned int gplfuturecrcindex;
1830         unsigned int unwindex = 0;
1831 #ifdef CONFIG_UNUSED_SYMBOLS
1832         unsigned int unusedindex;
1833         unsigned int unusedcrcindex;
1834         unsigned int unusedgplindex;
1835         unsigned int unusedgplcrcindex;
1836 #endif
1837         unsigned int markersindex;
1838         unsigned int markersstringsindex;
1839         unsigned int tracepointsindex;
1840         unsigned int tracepointsstringsindex;
1841         unsigned int mcountindex;
1842         struct module *mod;
1843         long err = 0;
1844         void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1845         void *mseg;
1846         struct exception_table_entry *extable;
1847         mm_segment_t old_fs;
1848
1849         DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1850                umod, len, uargs);
1851         if (len < sizeof(*hdr))
1852                 return ERR_PTR(-ENOEXEC);
1853
1854         /* Suck in entire file: we'll want most of it. */
1855         /* vmalloc barfs on "unusual" numbers.  Check here */
1856         if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1857                 return ERR_PTR(-ENOMEM);
1858         if (copy_from_user(hdr, umod, len) != 0) {
1859                 err = -EFAULT;
1860                 goto free_hdr;
1861         }
1862
1863         /* Sanity checks against insmoding binaries or wrong arch,
1864            weird elf version */
1865         if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
1866             || hdr->e_type != ET_REL
1867             || !elf_check_arch(hdr)
1868             || hdr->e_shentsize != sizeof(*sechdrs)) {
1869                 err = -ENOEXEC;
1870                 goto free_hdr;
1871         }
1872
1873         if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1874                 goto truncated;
1875
1876         /* Convenience variables */
1877         sechdrs = (void *)hdr + hdr->e_shoff;
1878         secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1879         sechdrs[0].sh_addr = 0;
1880
1881         for (i = 1; i < hdr->e_shnum; i++) {
1882                 if (sechdrs[i].sh_type != SHT_NOBITS
1883                     && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1884                         goto truncated;
1885
1886                 /* Mark all sections sh_addr with their address in the
1887                    temporary image. */
1888                 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1889
1890                 /* Internal symbols and strings. */
1891                 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1892                         symindex = i;
1893                         strindex = sechdrs[i].sh_link;
1894                         strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1895                 }
1896 #ifndef CONFIG_MODULE_UNLOAD
1897                 /* Don't load .exit sections */
1898                 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1899                         sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1900 #endif
1901         }
1902
1903         modindex = find_sec(hdr, sechdrs, secstrings,
1904                             ".gnu.linkonce.this_module");
1905         if (!modindex) {
1906                 printk(KERN_WARNING "No module found in object\n");
1907                 err = -ENOEXEC;
1908                 goto free_hdr;
1909         }
1910         mod = (void *)sechdrs[modindex].sh_addr;
1911
1912         if (symindex == 0) {
1913                 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1914                        mod->name);
1915                 err = -ENOEXEC;
1916                 goto free_hdr;
1917         }
1918
1919         /* Optional sections */
1920         exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1921         gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
1922         gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future");
1923         crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1924         gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
1925         gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future");
1926 #ifdef CONFIG_UNUSED_SYMBOLS
1927         unusedindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused");
1928         unusedgplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused_gpl");
1929         unusedcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused");
1930         unusedgplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused_gpl");
1931 #endif
1932         setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1933         exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1934         obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1935         versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1936         infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1937         pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1938 #ifdef ARCH_UNWIND_SECTION_NAME
1939         unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
1940 #endif
1941
1942         /* Don't keep modinfo and version sections. */
1943         sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1944         sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1945 #ifdef CONFIG_KALLSYMS
1946         /* Keep symbol and string tables for decoding later. */
1947         sechdrs[symindex].sh_flags |= SHF_ALLOC;
1948         sechdrs[strindex].sh_flags |= SHF_ALLOC;
1949 #endif
1950         if (unwindex)
1951                 sechdrs[unwindex].sh_flags |= SHF_ALLOC;
1952
1953         /* Check module struct version now, before we try to use module. */
1954         if (!check_modstruct_version(sechdrs, versindex, mod)) {
1955                 err = -ENOEXEC;
1956                 goto free_hdr;
1957         }
1958
1959         modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1960         /* This is allowed: modprobe --force will invalidate it. */
1961         if (!modmagic) {
1962                 err = try_to_force_load(mod, "magic");
1963                 if (err)
1964                         goto free_hdr;
1965         } else if (!same_magic(modmagic, vermagic, versindex)) {
1966                 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1967                        mod->name, modmagic, vermagic);
1968                 err = -ENOEXEC;
1969                 goto free_hdr;
1970         }
1971
1972         /* Now copy in args */
1973         args = strndup_user(uargs, ~0UL >> 1);
1974         if (IS_ERR(args)) {
1975                 err = PTR_ERR(args);
1976                 goto free_hdr;
1977         }
1978
1979         if (find_module(mod->name)) {
1980                 err = -EEXIST;
1981                 goto free_mod;
1982         }
1983
1984         mod->state = MODULE_STATE_COMING;
1985
1986         /* Allow arches to frob section contents and sizes.  */
1987         err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1988         if (err < 0)
1989                 goto free_mod;
1990
1991         if (pcpuindex) {
1992                 /* We have a special allocation for this section. */
1993                 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
1994                                          sechdrs[pcpuindex].sh_addralign,
1995                                          mod->name);
1996                 if (!percpu) {
1997                         err = -ENOMEM;
1998                         goto free_mod;
1999                 }
2000                 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2001                 mod->percpu = percpu;
2002         }
2003
2004         /* Determine total sizes, and put offsets in sh_entsize.  For now
2005            this is done generically; there doesn't appear to be any
2006            special cases for the architectures. */
2007         layout_sections(mod, hdr, sechdrs, secstrings);
2008
2009         /* Do the allocs. */
2010         ptr = module_alloc_update_bounds(mod->core_size);
2011         if (!ptr) {
2012                 err = -ENOMEM;
2013                 goto free_percpu;
2014         }
2015         memset(ptr, 0, mod->core_size);
2016         mod->module_core = ptr;
2017
2018         ptr = module_alloc_update_bounds(mod->init_size);
2019         if (!ptr && mod->init_size) {
2020                 err = -ENOMEM;
2021                 goto free_core;
2022         }
2023         memset(ptr, 0, mod->init_size);
2024         mod->module_init = ptr;
2025
2026         /* Transfer each section which specifies SHF_ALLOC */
2027         DEBUGP("final section addresses:\n");
2028         for (i = 0; i < hdr->e_shnum; i++) {
2029                 void *dest;
2030
2031                 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2032                         continue;
2033
2034                 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2035                         dest = mod->module_init
2036                                 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2037                 else
2038                         dest = mod->module_core + sechdrs[i].sh_entsize;
2039
2040                 if (sechdrs[i].sh_type != SHT_NOBITS)
2041                         memcpy(dest, (void *)sechdrs[i].sh_addr,
2042                                sechdrs[i].sh_size);
2043                 /* Update sh_addr to point to copy in image. */
2044                 sechdrs[i].sh_addr = (unsigned long)dest;
2045                 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2046         }
2047         /* Module has been moved. */
2048         mod = (void *)sechdrs[modindex].sh_addr;
2049
2050         /* Now we've moved module, initialize linked lists, etc. */
2051         module_unload_init(mod);
2052
2053         /* add kobject, so we can reference it. */
2054         err = mod_sysfs_init(mod);
2055         if (err)
2056                 goto free_unload;
2057
2058         /* Set up license info based on the info section */
2059         set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2060
2061         /*
2062          * ndiswrapper is under GPL by itself, but loads proprietary modules.
2063          * Don't use add_taint_module(), as it would prevent ndiswrapper from
2064          * using GPL-only symbols it needs.
2065          */
2066         if (strcmp(mod->name, "ndiswrapper") == 0)
2067                 add_taint(TAINT_PROPRIETARY_MODULE);
2068
2069         /* driverloader was caught wrongly pretending to be under GPL */
2070         if (strcmp(mod->name, "driverloader") == 0)
2071                 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2072
2073         /* Set up MODINFO_ATTR fields */
2074         setup_modinfo(mod, sechdrs, infoindex);
2075
2076         /* Fix up syms, so that st_value is a pointer to location. */
2077         err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2078                                mod);
2079         if (err < 0)
2080                 goto cleanup;
2081
2082         /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
2083         mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
2084         mod->syms = (void *)sechdrs[exportindex].sh_addr;
2085         if (crcindex)
2086                 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
2087         mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
2088         mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
2089         if (gplcrcindex)
2090                 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
2091         mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size /
2092                                         sizeof(*mod->gpl_future_syms);
2093         mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr;
2094         if (gplfuturecrcindex)
2095                 mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr;
2096
2097 #ifdef CONFIG_UNUSED_SYMBOLS
2098         mod->num_unused_syms = sechdrs[unusedindex].sh_size /
2099                                         sizeof(*mod->unused_syms);
2100         mod->num_unused_gpl_syms = sechdrs[unusedgplindex].sh_size /
2101                                         sizeof(*mod->unused_gpl_syms);
2102         mod->unused_syms = (void *)sechdrs[unusedindex].sh_addr;
2103         if (unusedcrcindex)
2104                 mod->unused_crcs = (void *)sechdrs[unusedcrcindex].sh_addr;
2105         mod->unused_gpl_syms = (void *)sechdrs[unusedgplindex].sh_addr;
2106         if (unusedgplcrcindex)
2107                 mod->unused_gpl_crcs
2108                         = (void *)sechdrs[unusedgplcrcindex].sh_addr;
2109 #endif
2110
2111 #ifdef CONFIG_MODVERSIONS
2112         if ((mod->num_syms && !crcindex)
2113             || (mod->num_gpl_syms && !gplcrcindex)
2114             || (mod->num_gpl_future_syms && !gplfuturecrcindex)
2115 #ifdef CONFIG_UNUSED_SYMBOLS
2116             || (mod->num_unused_syms && !unusedcrcindex)
2117             || (mod->num_unused_gpl_syms && !unusedgplcrcindex)
2118 #endif
2119                 ) {
2120                 printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name);
2121                 err = try_to_force_load(mod, "nocrc");
2122                 if (err)
2123                         goto cleanup;
2124         }
2125 #endif
2126         markersindex = find_sec(hdr, sechdrs, secstrings, "__markers");
2127         markersstringsindex = find_sec(hdr, sechdrs, secstrings,
2128                                         "__markers_strings");
2129         tracepointsindex = find_sec(hdr, sechdrs, secstrings, "__tracepoints");
2130         tracepointsstringsindex = find_sec(hdr, sechdrs, secstrings,
2131                                         "__tracepoints_strings");
2132
2133         mcountindex = find_sec(hdr, sechdrs, secstrings,
2134                                "__mcount_loc");
2135
2136         /* Now do relocations. */
2137         for (i = 1; i < hdr->e_shnum; i++) {
2138                 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2139                 unsigned int info = sechdrs[i].sh_info;
2140
2141                 /* Not a valid relocation section? */
2142                 if (info >= hdr->e_shnum)
2143                         continue;
2144
2145                 /* Don't bother with non-allocated sections */
2146                 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2147                         continue;
2148
2149                 if (sechdrs[i].sh_type == SHT_REL)
2150                         err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2151                 else if (sechdrs[i].sh_type == SHT_RELA)
2152                         err = apply_relocate_add(sechdrs, strtab, symindex, i,
2153                                                  mod);
2154                 if (err < 0)
2155                         goto cleanup;
2156         }
2157 #ifdef CONFIG_MARKERS
2158         mod->markers = (void *)sechdrs[markersindex].sh_addr;
2159         mod->num_markers =
2160                 sechdrs[markersindex].sh_size / sizeof(*mod->markers);
2161 #endif
2162 #ifdef CONFIG_TRACEPOINTS
2163         mod->tracepoints = (void *)sechdrs[tracepointsindex].sh_addr;
2164         mod->num_tracepoints =
2165                 sechdrs[tracepointsindex].sh_size / sizeof(*mod->tracepoints);
2166 #endif
2167
2168
2169         /* Find duplicate symbols */
2170         err = verify_export_symbols(mod);
2171
2172         if (err < 0)
2173                 goto cleanup;
2174
2175         /* Set up and sort exception table */
2176         mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
2177         mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
2178         sort_extable(extable, extable + mod->num_exentries);
2179
2180         /* Finally, copy percpu area over. */
2181         percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2182                        sechdrs[pcpuindex].sh_size);
2183
2184         add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2185
2186         if (!mod->taints) {
2187 #ifdef CONFIG_MARKERS
2188                 marker_update_probe_range(mod->markers,
2189                         mod->markers + mod->num_markers);
2190 #endif
2191 #ifdef CONFIG_TRACEPOINTS
2192                 tracepoint_update_probe_range(mod->tracepoints,
2193                         mod->tracepoints + mod->num_tracepoints);
2194 #endif
2195         }
2196
2197         /* sechdrs[0].sh_size is always zero */
2198         mseg = (void *)sechdrs[mcountindex].sh_addr;
2199         ftrace_init_module(mseg, mseg + sechdrs[mcountindex].sh_size);
2200
2201         err = module_finalize(hdr, sechdrs, mod);
2202         if (err < 0)
2203                 goto cleanup;
2204
2205         /* flush the icache in correct context */
2206         old_fs = get_fs();
2207         set_fs(KERNEL_DS);
2208
2209         /*
2210          * Flush the instruction cache, since we've played with text.
2211          * Do it before processing of module parameters, so the module
2212          * can provide parameter accessor functions of its own.
2213          */
2214         if (mod->module_init)
2215                 flush_icache_range((unsigned long)mod->module_init,
2216                                    (unsigned long)mod->module_init
2217                                    + mod->init_size);
2218         flush_icache_range((unsigned long)mod->module_core,
2219                            (unsigned long)mod->module_core + mod->core_size);
2220
2221         set_fs(old_fs);
2222
2223         mod->args = args;
2224         if (obsparmindex)
2225                 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2226                        mod->name);
2227
2228         /* Now sew it into the lists so we can get lockdep and oops
2229          * info during argument parsing.  Noone should access us, since
2230          * strong_try_module_get() will fail. */
2231         stop_machine(__link_module, mod, NULL);
2232
2233         /* Size of section 0 is 0, so this works well if no params */
2234         err = parse_args(mod->name, mod->args,
2235                          (struct kernel_param *)
2236                          sechdrs[setupindex].sh_addr,
2237                          sechdrs[setupindex].sh_size
2238                          / sizeof(struct kernel_param),
2239                          NULL);
2240         if (err < 0)
2241                 goto unlink;
2242
2243         err = mod_sysfs_setup(mod,
2244                               (struct kernel_param *)
2245                               sechdrs[setupindex].sh_addr,
2246                               sechdrs[setupindex].sh_size
2247                               / sizeof(struct kernel_param));
2248         if (err < 0)
2249                 goto unlink;
2250         add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2251         add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2252
2253         /* Size of section 0 is 0, so this works well if no unwind info. */
2254         mod->unwind_info = unwind_add_table(mod,
2255                                             (void *)sechdrs[unwindex].sh_addr,
2256                                             sechdrs[unwindex].sh_size);
2257
2258         /* Get rid of temporary copy */
2259         vfree(hdr);
2260
2261         /* Done! */
2262         return mod;
2263
2264  unlink:
2265         stop_machine(__unlink_module, mod, NULL);
2266         module_arch_cleanup(mod);
2267  cleanup:
2268         kobject_del(&mod->mkobj.kobj);
2269         kobject_put(&mod->mkobj.kobj);
2270         ftrace_release(mod->module_core, mod->core_size);
2271  free_unload:
2272         module_unload_free(mod);
2273         module_free(mod, mod->module_init);
2274  free_core:
2275         module_free(mod, mod->module_core);
2276  free_percpu:
2277         if (percpu)
2278                 percpu_modfree(percpu);
2279  free_mod:
2280         kfree(args);
2281  free_hdr:
2282         vfree(hdr);
2283         return ERR_PTR(err);
2284
2285  truncated:
2286         printk(KERN_ERR "Module len %lu truncated\n", len);
2287         err = -ENOEXEC;
2288         goto free_hdr;
2289 }
2290
2291 /* This is where the real work happens */
2292 asmlinkage long
2293 sys_init_module(void __user *umod,
2294                 unsigned long len,
2295                 const char __user *uargs)
2296 {
2297         struct module *mod;
2298         int ret = 0;
2299
2300         /* Must have permission */
2301         if (!capable(CAP_SYS_MODULE))
2302                 return -EPERM;
2303
2304         /* Only one module load at a time, please */
2305         if (mutex_lock_interruptible(&module_mutex) != 0)
2306                 return -EINTR;
2307
2308         /* Do all the hard work */
2309         mod = load_module(umod, len, uargs);
2310         if (IS_ERR(mod)) {
2311                 mutex_unlock(&module_mutex);
2312                 return PTR_ERR(mod);
2313         }
2314
2315         /* Drop lock so they can recurse */
2316         mutex_unlock(&module_mutex);
2317
2318         blocking_notifier_call_chain(&module_notify_list,
2319                         MODULE_STATE_COMING, mod);
2320
2321         /* Start the module */
2322         if (mod->init != NULL)
2323                 ret = do_one_initcall(mod->init);
2324         if (ret < 0) {
2325                 /* Init routine failed: abort.  Try to protect us from
2326                    buggy refcounters. */
2327                 mod->state = MODULE_STATE_GOING;
2328                 synchronize_sched();
2329                 module_put(mod);
2330                 blocking_notifier_call_chain(&module_notify_list,
2331                                              MODULE_STATE_GOING, mod);
2332                 mutex_lock(&module_mutex);
2333                 free_module(mod);
2334                 mutex_unlock(&module_mutex);
2335                 wake_up(&module_wq);
2336                 return ret;
2337         }
2338         if (ret > 0) {
2339                 printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
2340                                     "it should follow 0/-E convention\n"
2341                        KERN_WARNING "%s: loading module anyway...\n",
2342                        __func__, mod->name, ret,
2343                        __func__);
2344                 dump_stack();
2345         }
2346
2347         /* Now it's a first class citizen!  Wake up anyone waiting for it. */
2348         mod->state = MODULE_STATE_LIVE;
2349         wake_up(&module_wq);
2350
2351         mutex_lock(&module_mutex);
2352         /* Drop initial reference. */
2353         module_put(mod);
2354         unwind_remove_table(mod->unwind_info, 1);
2355         module_free(mod, mod->module_init);
2356         mod->module_init = NULL;
2357         mod->init_size = 0;
2358         mod->init_text_size = 0;
2359         mutex_unlock(&module_mutex);
2360
2361         return 0;
2362 }
2363
2364 static inline int within(unsigned long addr, void *start, unsigned long size)
2365 {
2366         return ((void *)addr >= start && (void *)addr < start + size);
2367 }
2368
2369 #ifdef CONFIG_KALLSYMS
2370 /*
2371  * This ignores the intensely annoying "mapping symbols" found
2372  * in ARM ELF files: $a, $t and $d.
2373  */
2374 static inline int is_arm_mapping_symbol(const char *str)
2375 {
2376         return str[0] == '$' && strchr("atd", str[1])
2377                && (str[2] == '\0' || str[2] == '.');
2378 }
2379
2380 static const char *get_ksymbol(struct module *mod,
2381                                unsigned long addr,
2382                                unsigned long *size,
2383                                unsigned long *offset)
2384 {
2385         unsigned int i, best = 0;
2386         unsigned long nextval;
2387
2388         /* At worse, next value is at end of module */
2389         if (within(addr, mod->module_init, mod->init_size))
2390                 nextval = (unsigned long)mod->module_init+mod->init_text_size;
2391         else
2392                 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2393
2394         /* Scan for closest preceeding symbol, and next symbol. (ELF
2395            starts real symbols at 1). */
2396         for (i = 1; i < mod->num_symtab; i++) {
2397                 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2398                         continue;
2399
2400                 /* We ignore unnamed symbols: they're uninformative
2401                  * and inserted at a whim. */
2402                 if (mod->symtab[i].st_value <= addr
2403                     && mod->symtab[i].st_value > mod->symtab[best].st_value
2404                     && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2405                     && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2406                         best = i;
2407                 if (mod->symtab[i].st_value > addr
2408                     && mod->symtab[i].st_value < nextval
2409                     && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2410                     && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2411                         nextval = mod->symtab[i].st_value;
2412         }
2413
2414         if (!best)
2415                 return NULL;
2416
2417         if (size)
2418                 *size = nextval - mod->symtab[best].st_value;
2419         if (offset)
2420                 *offset = addr - mod->symtab[best].st_value;
2421         return mod->strtab + mod->symtab[best].st_name;
2422 }
2423
2424 /* For kallsyms to ask for address resolution.  NULL means not found.  Careful
2425  * not to lock to avoid deadlock on oopses, simply disable preemption. */
2426 const char *module_address_lookup(unsigned long addr,
2427                             unsigned long *size,
2428                             unsigned long *offset,
2429                             char **modname,
2430                             char *namebuf)
2431 {
2432         struct module *mod;
2433         const char *ret = NULL;
2434
2435         preempt_disable();
2436         list_for_each_entry(mod, &modules, list) {
2437                 if (within(addr, mod->module_init, mod->init_size)
2438                     || within(addr, mod->module_core, mod->core_size)) {
2439                         if (modname)
2440                                 *modname = mod->name;
2441                         ret = get_ksymbol(mod, addr, size, offset);
2442                         break;
2443                 }
2444         }
2445         /* Make a copy in here where it's safe */
2446         if (ret) {
2447                 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2448                 ret = namebuf;
2449         }
2450         preempt_enable();
2451         return ret;
2452 }
2453
2454 int lookup_module_symbol_name(unsigned long addr, char *symname)
2455 {
2456         struct module *mod;
2457
2458         preempt_disable();
2459         list_for_each_entry(mod, &modules, list) {
2460                 if (within(addr, mod->module_init, mod->init_size) ||
2461                     within(addr, mod->module_core, mod->core_size)) {
2462                         const char *sym;
2463
2464                         sym = get_ksymbol(mod, addr, NULL, NULL);
2465                         if (!sym)
2466                                 goto out;
2467                         strlcpy(symname, sym, KSYM_NAME_LEN);
2468                         preempt_enable();
2469                         return 0;
2470                 }
2471         }
2472 out:
2473         preempt_enable();
2474         return -ERANGE;
2475 }
2476
2477 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2478                         unsigned long *offset, char *modname, char *name)
2479 {
2480         struct module *mod;
2481
2482         preempt_disable();
2483         list_for_each_entry(mod, &modules, list) {
2484                 if (within(addr, mod->module_init, mod->init_size) ||
2485                     within(addr, mod->module_core, mod->core_size)) {
2486                         const char *sym;
2487
2488                         sym = get_ksymbol(mod, addr, size, offset);
2489                         if (!sym)
2490                                 goto out;
2491                         if (modname)
2492                                 strlcpy(modname, mod->name, MODULE_NAME_LEN);
2493                         if (name)
2494                                 strlcpy(name, sym, KSYM_NAME_LEN);
2495                         preempt_enable();
2496                         return 0;
2497                 }
2498         }
2499 out:
2500         preempt_enable();
2501         return -ERANGE;
2502 }
2503
2504 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2505                         char *name, char *module_name, int *exported)
2506 {
2507         struct module *mod;
2508
2509         preempt_disable();
2510         list_for_each_entry(mod, &modules, list) {
2511                 if (symnum < mod->num_symtab) {
2512                         *value = mod->symtab[symnum].st_value;
2513                         *type = mod->symtab[symnum].st_info;
2514                         strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
2515                                 KSYM_NAME_LEN);
2516                         strlcpy(module_name, mod->name, MODULE_NAME_LEN);
2517                         *exported = is_exported(name, mod);
2518                         preempt_enable();
2519                         return 0;
2520                 }
2521                 symnum -= mod->num_symtab;
2522         }
2523         preempt_enable();
2524         return -ERANGE;
2525 }
2526
2527 static unsigned long mod_find_symname(struct module *mod, const char *name)
2528 {
2529         unsigned int i;
2530
2531         for (i = 0; i < mod->num_symtab; i++)
2532                 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2533                     mod->symtab[i].st_info != 'U')
2534                         return mod->symtab[i].st_value;
2535         return 0;
2536 }
2537
2538 /* Look for this name: can be of form module:name. */
2539 unsigned long module_kallsyms_lookup_name(const char *name)
2540 {
2541         struct module *mod;
2542         char *colon;
2543         unsigned long ret = 0;
2544
2545         /* Don't lock: we're in enough trouble already. */
2546         preempt_disable();
2547         if ((colon = strchr(name, ':')) != NULL) {
2548                 *colon = '\0';
2549                 if ((mod = find_module(name)) != NULL)
2550                         ret = mod_find_symname(mod, colon+1);
2551                 *colon = ':';
2552         } else {
2553                 list_for_each_entry(mod, &modules, list)
2554                         if ((ret = mod_find_symname(mod, name)) != 0)
2555                                 break;
2556         }
2557         preempt_enable();
2558         return ret;
2559 }
2560 #endif /* CONFIG_KALLSYMS */
2561
2562 /* Called by the /proc file system to return a list of modules. */
2563 static void *m_start(struct seq_file *m, loff_t *pos)
2564 {
2565         mutex_lock(&module_mutex);
2566         return seq_list_start(&modules, *pos);
2567 }
2568
2569 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2570 {
2571         return seq_list_next(p, &modules, pos);
2572 }
2573
2574 static void m_stop(struct seq_file *m, void *p)
2575 {
2576         mutex_unlock(&module_mutex);
2577 }
2578
2579 static char *module_flags(struct module *mod, char *buf)
2580 {
2581         int bx = 0;
2582
2583         if (mod->taints ||
2584             mod->state == MODULE_STATE_GOING ||
2585             mod->state == MODULE_STATE_COMING) {
2586                 buf[bx++] = '(';
2587                 if (mod->taints & TAINT_PROPRIETARY_MODULE)
2588                         buf[bx++] = 'P';
2589                 if (mod->taints & TAINT_FORCED_MODULE)
2590                         buf[bx++] = 'F';
2591                 /*
2592                  * TAINT_FORCED_RMMOD: could be added.
2593                  * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2594                  * apply to modules.
2595                  */
2596
2597                 /* Show a - for module-is-being-unloaded */
2598                 if (mod->state == MODULE_STATE_GOING)
2599                         buf[bx++] = '-';
2600                 /* Show a + for module-is-being-loaded */
2601                 if (mod->state == MODULE_STATE_COMING)
2602                         buf[bx++] = '+';
2603                 buf[bx++] = ')';
2604         }
2605         buf[bx] = '\0';
2606
2607         return buf;
2608 }
2609
2610 static int m_show(struct seq_file *m, void *p)
2611 {
2612         struct module *mod = list_entry(p, struct module, list);
2613         char buf[8];
2614
2615         seq_printf(m, "%s %u",
2616                    mod->name, mod->init_size + mod->core_size);
2617         print_unload_info(m, mod);
2618
2619         /* Informative for users. */
2620         seq_printf(m, " %s",
2621                    mod->state == MODULE_STATE_GOING ? "Unloading":
2622                    mod->state == MODULE_STATE_COMING ? "Loading":
2623                    "Live");
2624         /* Used by oprofile and other similar tools. */
2625         seq_printf(m, " 0x%p", mod->module_core);
2626
2627         /* Taints info */
2628         if (mod->taints)
2629                 seq_printf(m, " %s", module_flags(mod, buf));
2630
2631         seq_printf(m, "\n");
2632         return 0;
2633 }
2634
2635 /* Format: modulename size refcount deps address
2636
2637    Where refcount is a number or -, and deps is a comma-separated list
2638    of depends or -.
2639 */
2640 const struct seq_operations modules_op = {
2641         .start  = m_start,
2642         .next   = m_next,
2643         .stop   = m_stop,
2644         .show   = m_show
2645 };
2646
2647 /* Given an address, look for it in the module exception tables. */
2648 const struct exception_table_entry *search_module_extables(unsigned long addr)
2649 {
2650         const struct exception_table_entry *e = NULL;
2651         struct module *mod;
2652
2653         preempt_disable();
2654         list_for_each_entry(mod, &modules, list) {
2655                 if (mod->num_exentries == 0)
2656                         continue;
2657
2658                 e = search_extable(mod->extable,
2659                                    mod->extable + mod->num_exentries - 1,
2660                                    addr);
2661                 if (e)
2662                         break;
2663         }
2664         preempt_enable();
2665
2666         /* Now, if we found one, we are running inside it now, hence
2667            we cannot unload the module, hence no refcnt needed. */
2668         return e;
2669 }
2670
2671 /*
2672  * Is this a valid module address?
2673  */
2674 int is_module_address(unsigned long addr)
2675 {
2676         struct module *mod;
2677
2678         preempt_disable();
2679
2680         list_for_each_entry(mod, &modules, list) {
2681                 if (within(addr, mod->module_core, mod->core_size)) {
2682                         preempt_enable();
2683                         return 1;
2684                 }
2685         }
2686
2687         preempt_enable();
2688
2689         return 0;
2690 }
2691
2692
2693 /* Is this a valid kernel address? */
2694 struct module *__module_text_address(unsigned long addr)
2695 {
2696         struct module *mod;
2697
2698         if (addr < module_addr_min || addr > module_addr_max)
2699                 return NULL;
2700
2701         list_for_each_entry(mod, &modules, list)
2702                 if (within(addr, mod->module_init, mod->init_text_size)
2703                     || within(addr, mod->module_core, mod->core_text_size))
2704                         return mod;
2705         return NULL;
2706 }
2707
2708 struct module *module_text_address(unsigned long addr)
2709 {
2710         struct module *mod;
2711
2712         preempt_disable();
2713         mod = __module_text_address(addr);
2714         preempt_enable();
2715
2716         return mod;
2717 }
2718
2719 /* Don't grab lock, we're oopsing. */
2720 void print_modules(void)
2721 {
2722         struct module *mod;
2723         char buf[8];
2724
2725         printk("Modules linked in:");
2726         list_for_each_entry(mod, &modules, list)
2727                 printk(" %s%s", mod->name, module_flags(mod, buf));
2728         if (last_unloaded_module[0])
2729                 printk(" [last unloaded: %s]", last_unloaded_module);
2730         printk("\n");
2731 }
2732
2733 #ifdef CONFIG_MODVERSIONS
2734 /* Generate the signature for struct module here, too, for modversions. */
2735 void struct_module(struct module *mod) { return; }
2736 EXPORT_SYMBOL(struct_module);
2737 #endif
2738
2739 #ifdef CONFIG_MARKERS
2740 void module_update_markers(void)
2741 {
2742         struct module *mod;
2743
2744         mutex_lock(&module_mutex);
2745         list_for_each_entry(mod, &modules, list)
2746                 if (!mod->taints)
2747                         marker_update_probe_range(mod->markers,
2748                                 mod->markers + mod->num_markers);
2749         mutex_unlock(&module_mutex);
2750 }
2751 #endif
2752
2753 #ifdef CONFIG_TRACEPOINTS
2754 void module_update_tracepoints(void)
2755 {
2756         struct module *mod;
2757
2758         mutex_lock(&module_mutex);
2759         list_for_each_entry(mod, &modules, list)
2760                 if (!mod->taints)
2761                         tracepoint_update_probe_range(mod->tracepoints,
2762                                 mod->tracepoints + mod->num_tracepoints);
2763         mutex_unlock(&module_mutex);
2764 }
2765
2766 /*
2767  * Returns 0 if current not found.
2768  * Returns 1 if current found.
2769  */
2770 int module_get_iter_tracepoints(struct tracepoint_iter *iter)
2771 {
2772         struct module *iter_mod;
2773         int found = 0;
2774
2775         mutex_lock(&module_mutex);
2776         list_for_each_entry(iter_mod, &modules, list) {
2777                 if (!iter_mod->taints) {
2778                         /*
2779                          * Sorted module list
2780                          */
2781                         if (iter_mod < iter->module)
2782                                 continue;
2783                         else if (iter_mod > iter->module)
2784                                 iter->tracepoint = NULL;
2785                         found = tracepoint_get_iter_range(&iter->tracepoint,
2786                                 iter_mod->tracepoints,
2787                                 iter_mod->tracepoints
2788                                         + iter_mod->num_tracepoints);
2789                         if (found) {
2790                                 iter->module = iter_mod;
2791                                 break;
2792                         }
2793                 }
2794         }
2795         mutex_unlock(&module_mutex);
2796         return found;
2797 }
2798 #endif