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