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