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