]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - kernel/module.c
treewide: convert uses of ATTRIB_NORETURN to __noreturn
[karo-tx-linux.git] / kernel / module.c
1 /*
2    Copyright (C) 2002 Richard Henderson
3    Copyright (C) 2001 Rusty Russell, 2002, 2010 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/export.h>
20 #include <linux/moduleloader.h>
21 #include <linux/ftrace_event.h>
22 #include <linux/init.h>
23 #include <linux/kallsyms.h>
24 #include <linux/fs.h>
25 #include <linux/sysfs.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/vmalloc.h>
29 #include <linux/elf.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <linux/syscalls.h>
33 #include <linux/fcntl.h>
34 #include <linux/rcupdate.h>
35 #include <linux/capability.h>
36 #include <linux/cpu.h>
37 #include <linux/moduleparam.h>
38 #include <linux/errno.h>
39 #include <linux/err.h>
40 #include <linux/vermagic.h>
41 #include <linux/notifier.h>
42 #include <linux/sched.h>
43 #include <linux/stop_machine.h>
44 #include <linux/device.h>
45 #include <linux/string.h>
46 #include <linux/mutex.h>
47 #include <linux/rculist.h>
48 #include <asm/uaccess.h>
49 #include <asm/cacheflush.h>
50 #include <asm/mmu_context.h>
51 #include <linux/license.h>
52 #include <asm/sections.h>
53 #include <linux/tracepoint.h>
54 #include <linux/ftrace.h>
55 #include <linux/async.h>
56 #include <linux/percpu.h>
57 #include <linux/kmemleak.h>
58 #include <linux/jump_label.h>
59 #include <linux/pfn.h>
60 #include <linux/bsearch.h>
61
62 #define CREATE_TRACE_POINTS
63 #include <trace/events/module.h>
64
65 #if 0
66 #define DEBUGP printk
67 #else
68 #define DEBUGP(fmt , a...)
69 #endif
70
71 #ifndef ARCH_SHF_SMALL
72 #define ARCH_SHF_SMALL 0
73 #endif
74
75 /*
76  * Modules' sections will be aligned on page boundaries
77  * to ensure complete separation of code and data, but
78  * only when CONFIG_DEBUG_SET_MODULE_RONX=y
79  */
80 #ifdef CONFIG_DEBUG_SET_MODULE_RONX
81 # define debug_align(X) ALIGN(X, PAGE_SIZE)
82 #else
83 # define debug_align(X) (X)
84 #endif
85
86 /*
87  * Given BASE and SIZE this macro calculates the number of pages the
88  * memory regions occupies
89  */
90 #define MOD_NUMBER_OF_PAGES(BASE, SIZE) (((SIZE) > 0) ?         \
91                 (PFN_DOWN((unsigned long)(BASE) + (SIZE) - 1) - \
92                          PFN_DOWN((unsigned long)BASE) + 1)     \
93                 : (0UL))
94
95 /* If this is set, the section belongs in the init part of the module */
96 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
97
98 /*
99  * Mutex protects:
100  * 1) List of modules (also safely readable with preempt_disable),
101  * 2) module_use links,
102  * 3) module_addr_min/module_addr_max.
103  * (delete uses stop_machine/add uses RCU list operations). */
104 DEFINE_MUTEX(module_mutex);
105 EXPORT_SYMBOL_GPL(module_mutex);
106 static LIST_HEAD(modules);
107 #ifdef CONFIG_KGDB_KDB
108 struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
109 #endif /* CONFIG_KGDB_KDB */
110
111
112 /* Block module loading/unloading? */
113 int modules_disabled = 0;
114
115 /* Waiting for a module to finish initializing? */
116 static DECLARE_WAIT_QUEUE_HEAD(module_wq);
117
118 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
119
120 /* Bounds of module allocation, for speeding __module_address.
121  * Protected by module_mutex. */
122 static unsigned long module_addr_min = -1UL, module_addr_max = 0;
123
124 int register_module_notifier(struct notifier_block * nb)
125 {
126         return blocking_notifier_chain_register(&module_notify_list, nb);
127 }
128 EXPORT_SYMBOL(register_module_notifier);
129
130 int unregister_module_notifier(struct notifier_block * nb)
131 {
132         return blocking_notifier_chain_unregister(&module_notify_list, nb);
133 }
134 EXPORT_SYMBOL(unregister_module_notifier);
135
136 struct load_info {
137         Elf_Ehdr *hdr;
138         unsigned long len;
139         Elf_Shdr *sechdrs;
140         char *secstrings, *strtab;
141         unsigned long symoffs, stroffs;
142         struct _ddebug *debug;
143         unsigned int num_debug;
144         struct {
145                 unsigned int sym, str, mod, vers, info, pcpu;
146         } index;
147 };
148
149 /* We require a truly strong try_module_get(): 0 means failure due to
150    ongoing or failed initialization etc. */
151 static inline int strong_try_module_get(struct module *mod)
152 {
153         if (mod && mod->state == MODULE_STATE_COMING)
154                 return -EBUSY;
155         if (try_module_get(mod))
156                 return 0;
157         else
158                 return -ENOENT;
159 }
160
161 static inline void add_taint_module(struct module *mod, unsigned flag)
162 {
163         add_taint(flag);
164         mod->taints |= (1U << flag);
165 }
166
167 /*
168  * A thread that wants to hold a reference to a module only while it
169  * is running can call this to safely exit.  nfsd and lockd use this.
170  */
171 void __module_put_and_exit(struct module *mod, long code)
172 {
173         module_put(mod);
174         do_exit(code);
175 }
176 EXPORT_SYMBOL(__module_put_and_exit);
177
178 /* Find a module section: 0 means not found. */
179 static unsigned int find_sec(const struct load_info *info, const char *name)
180 {
181         unsigned int i;
182
183         for (i = 1; i < info->hdr->e_shnum; i++) {
184                 Elf_Shdr *shdr = &info->sechdrs[i];
185                 /* Alloc bit cleared means "ignore it." */
186                 if ((shdr->sh_flags & SHF_ALLOC)
187                     && strcmp(info->secstrings + shdr->sh_name, name) == 0)
188                         return i;
189         }
190         return 0;
191 }
192
193 /* Find a module section, or NULL. */
194 static void *section_addr(const struct load_info *info, const char *name)
195 {
196         /* Section 0 has sh_addr 0. */
197         return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
198 }
199
200 /* Find a module section, or NULL.  Fill in number of "objects" in section. */
201 static void *section_objs(const struct load_info *info,
202                           const char *name,
203                           size_t object_size,
204                           unsigned int *num)
205 {
206         unsigned int sec = find_sec(info, name);
207
208         /* Section 0 has sh_addr 0 and sh_size 0. */
209         *num = info->sechdrs[sec].sh_size / object_size;
210         return (void *)info->sechdrs[sec].sh_addr;
211 }
212
213 /* Provided by the linker */
214 extern const struct kernel_symbol __start___ksymtab[];
215 extern const struct kernel_symbol __stop___ksymtab[];
216 extern const struct kernel_symbol __start___ksymtab_gpl[];
217 extern const struct kernel_symbol __stop___ksymtab_gpl[];
218 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
219 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
220 extern const unsigned long __start___kcrctab[];
221 extern const unsigned long __start___kcrctab_gpl[];
222 extern const unsigned long __start___kcrctab_gpl_future[];
223 #ifdef CONFIG_UNUSED_SYMBOLS
224 extern const struct kernel_symbol __start___ksymtab_unused[];
225 extern const struct kernel_symbol __stop___ksymtab_unused[];
226 extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
227 extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
228 extern const unsigned long __start___kcrctab_unused[];
229 extern const unsigned long __start___kcrctab_unused_gpl[];
230 #endif
231
232 #ifndef CONFIG_MODVERSIONS
233 #define symversion(base, idx) NULL
234 #else
235 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
236 #endif
237
238 static bool each_symbol_in_section(const struct symsearch *arr,
239                                    unsigned int arrsize,
240                                    struct module *owner,
241                                    bool (*fn)(const struct symsearch *syms,
242                                               struct module *owner,
243                                               void *data),
244                                    void *data)
245 {
246         unsigned int j;
247
248         for (j = 0; j < arrsize; j++) {
249                 if (fn(&arr[j], owner, data))
250                         return true;
251         }
252
253         return false;
254 }
255
256 /* Returns true as soon as fn returns true, otherwise false. */
257 bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
258                                     struct module *owner,
259                                     void *data),
260                          void *data)
261 {
262         struct module *mod;
263         static const struct symsearch arr[] = {
264                 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
265                   NOT_GPL_ONLY, false },
266                 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
267                   __start___kcrctab_gpl,
268                   GPL_ONLY, false },
269                 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
270                   __start___kcrctab_gpl_future,
271                   WILL_BE_GPL_ONLY, false },
272 #ifdef CONFIG_UNUSED_SYMBOLS
273                 { __start___ksymtab_unused, __stop___ksymtab_unused,
274                   __start___kcrctab_unused,
275                   NOT_GPL_ONLY, true },
276                 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
277                   __start___kcrctab_unused_gpl,
278                   GPL_ONLY, true },
279 #endif
280         };
281
282         if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
283                 return true;
284
285         list_for_each_entry_rcu(mod, &modules, list) {
286                 struct symsearch arr[] = {
287                         { mod->syms, mod->syms + mod->num_syms, mod->crcs,
288                           NOT_GPL_ONLY, false },
289                         { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
290                           mod->gpl_crcs,
291                           GPL_ONLY, false },
292                         { mod->gpl_future_syms,
293                           mod->gpl_future_syms + mod->num_gpl_future_syms,
294                           mod->gpl_future_crcs,
295                           WILL_BE_GPL_ONLY, false },
296 #ifdef CONFIG_UNUSED_SYMBOLS
297                         { mod->unused_syms,
298                           mod->unused_syms + mod->num_unused_syms,
299                           mod->unused_crcs,
300                           NOT_GPL_ONLY, true },
301                         { mod->unused_gpl_syms,
302                           mod->unused_gpl_syms + mod->num_unused_gpl_syms,
303                           mod->unused_gpl_crcs,
304                           GPL_ONLY, true },
305 #endif
306                 };
307
308                 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
309                         return true;
310         }
311         return false;
312 }
313 EXPORT_SYMBOL_GPL(each_symbol_section);
314
315 struct find_symbol_arg {
316         /* Input */
317         const char *name;
318         bool gplok;
319         bool warn;
320
321         /* Output */
322         struct module *owner;
323         const unsigned long *crc;
324         const struct kernel_symbol *sym;
325 };
326
327 static bool check_symbol(const struct symsearch *syms,
328                                  struct module *owner,
329                                  unsigned int symnum, void *data)
330 {
331         struct find_symbol_arg *fsa = data;
332
333         if (!fsa->gplok) {
334                 if (syms->licence == GPL_ONLY)
335                         return false;
336                 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
337                         printk(KERN_WARNING "Symbol %s is being used "
338                                "by a non-GPL module, which will not "
339                                "be allowed in the future\n", fsa->name);
340                         printk(KERN_WARNING "Please see the file "
341                                "Documentation/feature-removal-schedule.txt "
342                                "in the kernel source tree for more details.\n");
343                 }
344         }
345
346 #ifdef CONFIG_UNUSED_SYMBOLS
347         if (syms->unused && fsa->warn) {
348                 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
349                        "however this module is using it.\n", fsa->name);
350                 printk(KERN_WARNING
351                        "This symbol will go away in the future.\n");
352                 printk(KERN_WARNING
353                        "Please evalute if this is the right api to use and if "
354                        "it really is, submit a report the linux kernel "
355                        "mailinglist together with submitting your code for "
356                        "inclusion.\n");
357         }
358 #endif
359
360         fsa->owner = owner;
361         fsa->crc = symversion(syms->crcs, symnum);
362         fsa->sym = &syms->start[symnum];
363         return true;
364 }
365
366 static int cmp_name(const void *va, const void *vb)
367 {
368         const char *a;
369         const struct kernel_symbol *b;
370         a = va; b = vb;
371         return strcmp(a, b->name);
372 }
373
374 static bool find_symbol_in_section(const struct symsearch *syms,
375                                    struct module *owner,
376                                    void *data)
377 {
378         struct find_symbol_arg *fsa = data;
379         struct kernel_symbol *sym;
380
381         sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
382                         sizeof(struct kernel_symbol), cmp_name);
383
384         if (sym != NULL && check_symbol(syms, owner, sym - syms->start, data))
385                 return true;
386
387         return false;
388 }
389
390 /* Find a symbol and return it, along with, (optional) crc and
391  * (optional) module which owns it.  Needs preempt disabled or module_mutex. */
392 const struct kernel_symbol *find_symbol(const char *name,
393                                         struct module **owner,
394                                         const unsigned long **crc,
395                                         bool gplok,
396                                         bool warn)
397 {
398         struct find_symbol_arg fsa;
399
400         fsa.name = name;
401         fsa.gplok = gplok;
402         fsa.warn = warn;
403
404         if (each_symbol_section(find_symbol_in_section, &fsa)) {
405                 if (owner)
406                         *owner = fsa.owner;
407                 if (crc)
408                         *crc = fsa.crc;
409                 return fsa.sym;
410         }
411
412         DEBUGP("Failed to find symbol %s\n", name);
413         return NULL;
414 }
415 EXPORT_SYMBOL_GPL(find_symbol);
416
417 /* Search for module by name: must hold module_mutex. */
418 struct module *find_module(const char *name)
419 {
420         struct module *mod;
421
422         list_for_each_entry(mod, &modules, list) {
423                 if (strcmp(mod->name, name) == 0)
424                         return mod;
425         }
426         return NULL;
427 }
428 EXPORT_SYMBOL_GPL(find_module);
429
430 #ifdef CONFIG_SMP
431
432 static inline void __percpu *mod_percpu(struct module *mod)
433 {
434         return mod->percpu;
435 }
436
437 static int percpu_modalloc(struct module *mod,
438                            unsigned long size, unsigned long align)
439 {
440         if (align > PAGE_SIZE) {
441                 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
442                        mod->name, align, PAGE_SIZE);
443                 align = PAGE_SIZE;
444         }
445
446         mod->percpu = __alloc_reserved_percpu(size, align);
447         if (!mod->percpu) {
448                 printk(KERN_WARNING
449                        "%s: Could not allocate %lu bytes percpu data\n",
450                        mod->name, size);
451                 return -ENOMEM;
452         }
453         mod->percpu_size = size;
454         return 0;
455 }
456
457 static void percpu_modfree(struct module *mod)
458 {
459         free_percpu(mod->percpu);
460 }
461
462 static unsigned int find_pcpusec(struct load_info *info)
463 {
464         return find_sec(info, ".data..percpu");
465 }
466
467 static void percpu_modcopy(struct module *mod,
468                            const void *from, unsigned long size)
469 {
470         int cpu;
471
472         for_each_possible_cpu(cpu)
473                 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
474 }
475
476 /**
477  * is_module_percpu_address - test whether address is from module static percpu
478  * @addr: address to test
479  *
480  * Test whether @addr belongs to module static percpu area.
481  *
482  * RETURNS:
483  * %true if @addr is from module static percpu area
484  */
485 bool is_module_percpu_address(unsigned long addr)
486 {
487         struct module *mod;
488         unsigned int cpu;
489
490         preempt_disable();
491
492         list_for_each_entry_rcu(mod, &modules, list) {
493                 if (!mod->percpu_size)
494                         continue;
495                 for_each_possible_cpu(cpu) {
496                         void *start = per_cpu_ptr(mod->percpu, cpu);
497
498                         if ((void *)addr >= start &&
499                             (void *)addr < start + mod->percpu_size) {
500                                 preempt_enable();
501                                 return true;
502                         }
503                 }
504         }
505
506         preempt_enable();
507         return false;
508 }
509
510 #else /* ... !CONFIG_SMP */
511
512 static inline void __percpu *mod_percpu(struct module *mod)
513 {
514         return NULL;
515 }
516 static inline int percpu_modalloc(struct module *mod,
517                                   unsigned long size, unsigned long align)
518 {
519         return -ENOMEM;
520 }
521 static inline void percpu_modfree(struct module *mod)
522 {
523 }
524 static unsigned int find_pcpusec(struct load_info *info)
525 {
526         return 0;
527 }
528 static inline void percpu_modcopy(struct module *mod,
529                                   const void *from, unsigned long size)
530 {
531         /* pcpusec should be 0, and size of that section should be 0. */
532         BUG_ON(size != 0);
533 }
534 bool is_module_percpu_address(unsigned long addr)
535 {
536         return false;
537 }
538
539 #endif /* CONFIG_SMP */
540
541 #define MODINFO_ATTR(field)     \
542 static void setup_modinfo_##field(struct module *mod, const char *s)  \
543 {                                                                     \
544         mod->field = kstrdup(s, GFP_KERNEL);                          \
545 }                                                                     \
546 static ssize_t show_modinfo_##field(struct module_attribute *mattr,   \
547                         struct module_kobject *mk, char *buffer)      \
548 {                                                                     \
549         return sprintf(buffer, "%s\n", mk->mod->field);               \
550 }                                                                     \
551 static int modinfo_##field##_exists(struct module *mod)               \
552 {                                                                     \
553         return mod->field != NULL;                                    \
554 }                                                                     \
555 static void free_modinfo_##field(struct module *mod)                  \
556 {                                                                     \
557         kfree(mod->field);                                            \
558         mod->field = NULL;                                            \
559 }                                                                     \
560 static struct module_attribute modinfo_##field = {                    \
561         .attr = { .name = __stringify(field), .mode = 0444 },         \
562         .show = show_modinfo_##field,                                 \
563         .setup = setup_modinfo_##field,                               \
564         .test = modinfo_##field##_exists,                             \
565         .free = free_modinfo_##field,                                 \
566 };
567
568 MODINFO_ATTR(version);
569 MODINFO_ATTR(srcversion);
570
571 static char last_unloaded_module[MODULE_NAME_LEN+1];
572
573 #ifdef CONFIG_MODULE_UNLOAD
574
575 EXPORT_TRACEPOINT_SYMBOL(module_get);
576
577 /* Init the unload section of the module. */
578 static int module_unload_init(struct module *mod)
579 {
580         mod->refptr = alloc_percpu(struct module_ref);
581         if (!mod->refptr)
582                 return -ENOMEM;
583
584         INIT_LIST_HEAD(&mod->source_list);
585         INIT_LIST_HEAD(&mod->target_list);
586
587         /* Hold reference count during initialization. */
588         __this_cpu_write(mod->refptr->incs, 1);
589         /* Backwards compatibility macros put refcount during init. */
590         mod->waiter = current;
591
592         return 0;
593 }
594
595 /* Does a already use b? */
596 static int already_uses(struct module *a, struct module *b)
597 {
598         struct module_use *use;
599
600         list_for_each_entry(use, &b->source_list, source_list) {
601                 if (use->source == a) {
602                         DEBUGP("%s uses %s!\n", a->name, b->name);
603                         return 1;
604                 }
605         }
606         DEBUGP("%s does not use %s!\n", a->name, b->name);
607         return 0;
608 }
609
610 /*
611  * Module a uses b
612  *  - we add 'a' as a "source", 'b' as a "target" of module use
613  *  - the module_use is added to the list of 'b' sources (so
614  *    'b' can walk the list to see who sourced them), and of 'a'
615  *    targets (so 'a' can see what modules it targets).
616  */
617 static int add_module_usage(struct module *a, struct module *b)
618 {
619         struct module_use *use;
620
621         DEBUGP("Allocating new usage for %s.\n", a->name);
622         use = kmalloc(sizeof(*use), GFP_ATOMIC);
623         if (!use) {
624                 printk(KERN_WARNING "%s: out of memory loading\n", a->name);
625                 return -ENOMEM;
626         }
627
628         use->source = a;
629         use->target = b;
630         list_add(&use->source_list, &b->source_list);
631         list_add(&use->target_list, &a->target_list);
632         return 0;
633 }
634
635 /* Module a uses b: caller needs module_mutex() */
636 int ref_module(struct module *a, struct module *b)
637 {
638         int err;
639
640         if (b == NULL || already_uses(a, b))
641                 return 0;
642
643         /* If module isn't available, we fail. */
644         err = strong_try_module_get(b);
645         if (err)
646                 return err;
647
648         err = add_module_usage(a, b);
649         if (err) {
650                 module_put(b);
651                 return err;
652         }
653         return 0;
654 }
655 EXPORT_SYMBOL_GPL(ref_module);
656
657 /* Clear the unload stuff of the module. */
658 static void module_unload_free(struct module *mod)
659 {
660         struct module_use *use, *tmp;
661
662         mutex_lock(&module_mutex);
663         list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
664                 struct module *i = use->target;
665                 DEBUGP("%s unusing %s\n", mod->name, i->name);
666                 module_put(i);
667                 list_del(&use->source_list);
668                 list_del(&use->target_list);
669                 kfree(use);
670         }
671         mutex_unlock(&module_mutex);
672
673         free_percpu(mod->refptr);
674 }
675
676 #ifdef CONFIG_MODULE_FORCE_UNLOAD
677 static inline int try_force_unload(unsigned int flags)
678 {
679         int ret = (flags & O_TRUNC);
680         if (ret)
681                 add_taint(TAINT_FORCED_RMMOD);
682         return ret;
683 }
684 #else
685 static inline int try_force_unload(unsigned int flags)
686 {
687         return 0;
688 }
689 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
690
691 struct stopref
692 {
693         struct module *mod;
694         int flags;
695         int *forced;
696 };
697
698 /* Whole machine is stopped with interrupts off when this runs. */
699 static int __try_stop_module(void *_sref)
700 {
701         struct stopref *sref = _sref;
702
703         /* If it's not unused, quit unless we're forcing. */
704         if (module_refcount(sref->mod) != 0) {
705                 if (!(*sref->forced = try_force_unload(sref->flags)))
706                         return -EWOULDBLOCK;
707         }
708
709         /* Mark it as dying. */
710         sref->mod->state = MODULE_STATE_GOING;
711         return 0;
712 }
713
714 static int try_stop_module(struct module *mod, int flags, int *forced)
715 {
716         if (flags & O_NONBLOCK) {
717                 struct stopref sref = { mod, flags, forced };
718
719                 return stop_machine(__try_stop_module, &sref, NULL);
720         } else {
721                 /* We don't need to stop the machine for this. */
722                 mod->state = MODULE_STATE_GOING;
723                 synchronize_sched();
724                 return 0;
725         }
726 }
727
728 unsigned int module_refcount(struct module *mod)
729 {
730         unsigned int incs = 0, decs = 0;
731         int cpu;
732
733         for_each_possible_cpu(cpu)
734                 decs += per_cpu_ptr(mod->refptr, cpu)->decs;
735         /*
736          * ensure the incs are added up after the decs.
737          * module_put ensures incs are visible before decs with smp_wmb.
738          *
739          * This 2-count scheme avoids the situation where the refcount
740          * for CPU0 is read, then CPU0 increments the module refcount,
741          * then CPU1 drops that refcount, then the refcount for CPU1 is
742          * read. We would record a decrement but not its corresponding
743          * increment so we would see a low count (disaster).
744          *
745          * Rare situation? But module_refcount can be preempted, and we
746          * might be tallying up 4096+ CPUs. So it is not impossible.
747          */
748         smp_rmb();
749         for_each_possible_cpu(cpu)
750                 incs += per_cpu_ptr(mod->refptr, cpu)->incs;
751         return incs - decs;
752 }
753 EXPORT_SYMBOL(module_refcount);
754
755 /* This exists whether we can unload or not */
756 static void free_module(struct module *mod);
757
758 static void wait_for_zero_refcount(struct module *mod)
759 {
760         /* Since we might sleep for some time, release the mutex first */
761         mutex_unlock(&module_mutex);
762         for (;;) {
763                 DEBUGP("Looking at refcount...\n");
764                 set_current_state(TASK_UNINTERRUPTIBLE);
765                 if (module_refcount(mod) == 0)
766                         break;
767                 schedule();
768         }
769         current->state = TASK_RUNNING;
770         mutex_lock(&module_mutex);
771 }
772
773 SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
774                 unsigned int, flags)
775 {
776         struct module *mod;
777         char name[MODULE_NAME_LEN];
778         int ret, forced = 0;
779
780         if (!capable(CAP_SYS_MODULE) || modules_disabled)
781                 return -EPERM;
782
783         if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
784                 return -EFAULT;
785         name[MODULE_NAME_LEN-1] = '\0';
786
787         if (mutex_lock_interruptible(&module_mutex) != 0)
788                 return -EINTR;
789
790         mod = find_module(name);
791         if (!mod) {
792                 ret = -ENOENT;
793                 goto out;
794         }
795
796         if (!list_empty(&mod->source_list)) {
797                 /* Other modules depend on us: get rid of them first. */
798                 ret = -EWOULDBLOCK;
799                 goto out;
800         }
801
802         /* Doing init or already dying? */
803         if (mod->state != MODULE_STATE_LIVE) {
804                 /* FIXME: if (force), slam module count and wake up
805                    waiter --RR */
806                 DEBUGP("%s already dying\n", mod->name);
807                 ret = -EBUSY;
808                 goto out;
809         }
810
811         /* If it has an init func, it must have an exit func to unload */
812         if (mod->init && !mod->exit) {
813                 forced = try_force_unload(flags);
814                 if (!forced) {
815                         /* This module can't be removed */
816                         ret = -EBUSY;
817                         goto out;
818                 }
819         }
820
821         /* Set this up before setting mod->state */
822         mod->waiter = current;
823
824         /* Stop the machine so refcounts can't move and disable module. */
825         ret = try_stop_module(mod, flags, &forced);
826         if (ret != 0)
827                 goto out;
828
829         /* Never wait if forced. */
830         if (!forced && module_refcount(mod) != 0)
831                 wait_for_zero_refcount(mod);
832
833         mutex_unlock(&module_mutex);
834         /* Final destruction now no one is using it. */
835         if (mod->exit != NULL)
836                 mod->exit();
837         blocking_notifier_call_chain(&module_notify_list,
838                                      MODULE_STATE_GOING, mod);
839         async_synchronize_full();
840
841         /* Store the name of the last unloaded module for diagnostic purposes */
842         strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
843
844         free_module(mod);
845         return 0;
846 out:
847         mutex_unlock(&module_mutex);
848         return ret;
849 }
850
851 static inline void print_unload_info(struct seq_file *m, struct module *mod)
852 {
853         struct module_use *use;
854         int printed_something = 0;
855
856         seq_printf(m, " %u ", module_refcount(mod));
857
858         /* Always include a trailing , so userspace can differentiate
859            between this and the old multi-field proc format. */
860         list_for_each_entry(use, &mod->source_list, source_list) {
861                 printed_something = 1;
862                 seq_printf(m, "%s,", use->source->name);
863         }
864
865         if (mod->init != NULL && mod->exit == NULL) {
866                 printed_something = 1;
867                 seq_printf(m, "[permanent],");
868         }
869
870         if (!printed_something)
871                 seq_printf(m, "-");
872 }
873
874 void __symbol_put(const char *symbol)
875 {
876         struct module *owner;
877
878         preempt_disable();
879         if (!find_symbol(symbol, &owner, NULL, true, false))
880                 BUG();
881         module_put(owner);
882         preempt_enable();
883 }
884 EXPORT_SYMBOL(__symbol_put);
885
886 /* Note this assumes addr is a function, which it currently always is. */
887 void symbol_put_addr(void *addr)
888 {
889         struct module *modaddr;
890         unsigned long a = (unsigned long)dereference_function_descriptor(addr);
891
892         if (core_kernel_text(a))
893                 return;
894
895         /* module_text_address is safe here: we're supposed to have reference
896          * to module from symbol_get, so it can't go away. */
897         modaddr = __module_text_address(a);
898         BUG_ON(!modaddr);
899         module_put(modaddr);
900 }
901 EXPORT_SYMBOL_GPL(symbol_put_addr);
902
903 static ssize_t show_refcnt(struct module_attribute *mattr,
904                            struct module_kobject *mk, char *buffer)
905 {
906         return sprintf(buffer, "%u\n", module_refcount(mk->mod));
907 }
908
909 static struct module_attribute refcnt = {
910         .attr = { .name = "refcnt", .mode = 0444 },
911         .show = show_refcnt,
912 };
913
914 void module_put(struct module *module)
915 {
916         if (module) {
917                 preempt_disable();
918                 smp_wmb(); /* see comment in module_refcount */
919                 __this_cpu_inc(module->refptr->decs);
920
921                 trace_module_put(module, _RET_IP_);
922                 /* Maybe they're waiting for us to drop reference? */
923                 if (unlikely(!module_is_live(module)))
924                         wake_up_process(module->waiter);
925                 preempt_enable();
926         }
927 }
928 EXPORT_SYMBOL(module_put);
929
930 #else /* !CONFIG_MODULE_UNLOAD */
931 static inline void print_unload_info(struct seq_file *m, struct module *mod)
932 {
933         /* We don't know the usage count, or what modules are using. */
934         seq_printf(m, " - -");
935 }
936
937 static inline void module_unload_free(struct module *mod)
938 {
939 }
940
941 int ref_module(struct module *a, struct module *b)
942 {
943         return strong_try_module_get(b);
944 }
945 EXPORT_SYMBOL_GPL(ref_module);
946
947 static inline int module_unload_init(struct module *mod)
948 {
949         return 0;
950 }
951 #endif /* CONFIG_MODULE_UNLOAD */
952
953 static ssize_t show_initstate(struct module_attribute *mattr,
954                               struct module_kobject *mk, char *buffer)
955 {
956         const char *state = "unknown";
957
958         switch (mk->mod->state) {
959         case MODULE_STATE_LIVE:
960                 state = "live";
961                 break;
962         case MODULE_STATE_COMING:
963                 state = "coming";
964                 break;
965         case MODULE_STATE_GOING:
966                 state = "going";
967                 break;
968         }
969         return sprintf(buffer, "%s\n", state);
970 }
971
972 static struct module_attribute initstate = {
973         .attr = { .name = "initstate", .mode = 0444 },
974         .show = show_initstate,
975 };
976
977 static ssize_t store_uevent(struct module_attribute *mattr,
978                             struct module_kobject *mk,
979                             const char *buffer, size_t count)
980 {
981         enum kobject_action action;
982
983         if (kobject_action_type(buffer, count, &action) == 0)
984                 kobject_uevent(&mk->kobj, action);
985         return count;
986 }
987
988 struct module_attribute module_uevent = {
989         .attr = { .name = "uevent", .mode = 0200 },
990         .store = store_uevent,
991 };
992
993 static struct module_attribute *modinfo_attrs[] = {
994         &modinfo_version,
995         &modinfo_srcversion,
996         &initstate,
997         &module_uevent,
998 #ifdef CONFIG_MODULE_UNLOAD
999         &refcnt,
1000 #endif
1001         NULL,
1002 };
1003
1004 static const char vermagic[] = VERMAGIC_STRING;
1005
1006 static int try_to_force_load(struct module *mod, const char *reason)
1007 {
1008 #ifdef CONFIG_MODULE_FORCE_LOAD
1009         if (!test_taint(TAINT_FORCED_MODULE))
1010                 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
1011                        mod->name, reason);
1012         add_taint_module(mod, TAINT_FORCED_MODULE);
1013         return 0;
1014 #else
1015         return -ENOEXEC;
1016 #endif
1017 }
1018
1019 #ifdef CONFIG_MODVERSIONS
1020 /* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
1021 static unsigned long maybe_relocated(unsigned long crc,
1022                                      const struct module *crc_owner)
1023 {
1024 #ifdef ARCH_RELOCATES_KCRCTAB
1025         if (crc_owner == NULL)
1026                 return crc - (unsigned long)reloc_start;
1027 #endif
1028         return crc;
1029 }
1030
1031 static int check_version(Elf_Shdr *sechdrs,
1032                          unsigned int versindex,
1033                          const char *symname,
1034                          struct module *mod, 
1035                          const unsigned long *crc,
1036                          const struct module *crc_owner)
1037 {
1038         unsigned int i, num_versions;
1039         struct modversion_info *versions;
1040
1041         /* Exporting module didn't supply crcs?  OK, we're already tainted. */
1042         if (!crc)
1043                 return 1;
1044
1045         /* No versions at all?  modprobe --force does this. */
1046         if (versindex == 0)
1047                 return try_to_force_load(mod, symname) == 0;
1048
1049         versions = (void *) sechdrs[versindex].sh_addr;
1050         num_versions = sechdrs[versindex].sh_size
1051                 / sizeof(struct modversion_info);
1052
1053         for (i = 0; i < num_versions; i++) {
1054                 if (strcmp(versions[i].name, symname) != 0)
1055                         continue;
1056
1057                 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
1058                         return 1;
1059                 DEBUGP("Found checksum %lX vs module %lX\n",
1060                        maybe_relocated(*crc, crc_owner), versions[i].crc);
1061                 goto bad_version;
1062         }
1063
1064         printk(KERN_WARNING "%s: no symbol version for %s\n",
1065                mod->name, symname);
1066         return 0;
1067
1068 bad_version:
1069         printk("%s: disagrees about version of symbol %s\n",
1070                mod->name, symname);
1071         return 0;
1072 }
1073
1074 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1075                                           unsigned int versindex,
1076                                           struct module *mod)
1077 {
1078         const unsigned long *crc;
1079
1080         /* Since this should be found in kernel (which can't be removed),
1081          * no locking is necessary. */
1082         if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1083                          &crc, true, false))
1084                 BUG();
1085         return check_version(sechdrs, versindex, "module_layout", mod, crc,
1086                              NULL);
1087 }
1088
1089 /* First part is kernel version, which we ignore if module has crcs. */
1090 static inline int same_magic(const char *amagic, const char *bmagic,
1091                              bool has_crcs)
1092 {
1093         if (has_crcs) {
1094                 amagic += strcspn(amagic, " ");
1095                 bmagic += strcspn(bmagic, " ");
1096         }
1097         return strcmp(amagic, bmagic) == 0;
1098 }
1099 #else
1100 static inline int check_version(Elf_Shdr *sechdrs,
1101                                 unsigned int versindex,
1102                                 const char *symname,
1103                                 struct module *mod, 
1104                                 const unsigned long *crc,
1105                                 const struct module *crc_owner)
1106 {
1107         return 1;
1108 }
1109
1110 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1111                                           unsigned int versindex,
1112                                           struct module *mod)
1113 {
1114         return 1;
1115 }
1116
1117 static inline int same_magic(const char *amagic, const char *bmagic,
1118                              bool has_crcs)
1119 {
1120         return strcmp(amagic, bmagic) == 0;
1121 }
1122 #endif /* CONFIG_MODVERSIONS */
1123
1124 /* Resolve a symbol for this module.  I.e. if we find one, record usage. */
1125 static const struct kernel_symbol *resolve_symbol(struct module *mod,
1126                                                   const struct load_info *info,
1127                                                   const char *name,
1128                                                   char ownername[])
1129 {
1130         struct module *owner;
1131         const struct kernel_symbol *sym;
1132         const unsigned long *crc;
1133         int err;
1134
1135         mutex_lock(&module_mutex);
1136         sym = find_symbol(name, &owner, &crc,
1137                           !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1138         if (!sym)
1139                 goto unlock;
1140
1141         if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
1142                            owner)) {
1143                 sym = ERR_PTR(-EINVAL);
1144                 goto getname;
1145         }
1146
1147         err = ref_module(mod, owner);
1148         if (err) {
1149                 sym = ERR_PTR(err);
1150                 goto getname;
1151         }
1152
1153 getname:
1154         /* We must make copy under the lock if we failed to get ref. */
1155         strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1156 unlock:
1157         mutex_unlock(&module_mutex);
1158         return sym;
1159 }
1160
1161 static const struct kernel_symbol *
1162 resolve_symbol_wait(struct module *mod,
1163                     const struct load_info *info,
1164                     const char *name)
1165 {
1166         const struct kernel_symbol *ksym;
1167         char owner[MODULE_NAME_LEN];
1168
1169         if (wait_event_interruptible_timeout(module_wq,
1170                         !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1171                         || PTR_ERR(ksym) != -EBUSY,
1172                                              30 * HZ) <= 0) {
1173                 printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
1174                        mod->name, owner);
1175         }
1176         return ksym;
1177 }
1178
1179 /*
1180  * /sys/module/foo/sections stuff
1181  * J. Corbet <corbet@lwn.net>
1182  */
1183 #ifdef CONFIG_SYSFS
1184
1185 #ifdef CONFIG_KALLSYMS
1186 static inline bool sect_empty(const Elf_Shdr *sect)
1187 {
1188         return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1189 }
1190
1191 struct module_sect_attr
1192 {
1193         struct module_attribute mattr;
1194         char *name;
1195         unsigned long address;
1196 };
1197
1198 struct module_sect_attrs
1199 {
1200         struct attribute_group grp;
1201         unsigned int nsections;
1202         struct module_sect_attr attrs[0];
1203 };
1204
1205 static ssize_t module_sect_show(struct module_attribute *mattr,
1206                                 struct module_kobject *mk, char *buf)
1207 {
1208         struct module_sect_attr *sattr =
1209                 container_of(mattr, struct module_sect_attr, mattr);
1210         return sprintf(buf, "0x%pK\n", (void *)sattr->address);
1211 }
1212
1213 static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1214 {
1215         unsigned int section;
1216
1217         for (section = 0; section < sect_attrs->nsections; section++)
1218                 kfree(sect_attrs->attrs[section].name);
1219         kfree(sect_attrs);
1220 }
1221
1222 static void add_sect_attrs(struct module *mod, const struct load_info *info)
1223 {
1224         unsigned int nloaded = 0, i, size[2];
1225         struct module_sect_attrs *sect_attrs;
1226         struct module_sect_attr *sattr;
1227         struct attribute **gattr;
1228
1229         /* Count loaded sections and allocate structures */
1230         for (i = 0; i < info->hdr->e_shnum; i++)
1231                 if (!sect_empty(&info->sechdrs[i]))
1232                         nloaded++;
1233         size[0] = ALIGN(sizeof(*sect_attrs)
1234                         + nloaded * sizeof(sect_attrs->attrs[0]),
1235                         sizeof(sect_attrs->grp.attrs[0]));
1236         size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
1237         sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1238         if (sect_attrs == NULL)
1239                 return;
1240
1241         /* Setup section attributes. */
1242         sect_attrs->grp.name = "sections";
1243         sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1244
1245         sect_attrs->nsections = 0;
1246         sattr = &sect_attrs->attrs[0];
1247         gattr = &sect_attrs->grp.attrs[0];
1248         for (i = 0; i < info->hdr->e_shnum; i++) {
1249                 Elf_Shdr *sec = &info->sechdrs[i];
1250                 if (sect_empty(sec))
1251                         continue;
1252                 sattr->address = sec->sh_addr;
1253                 sattr->name = kstrdup(info->secstrings + sec->sh_name,
1254                                         GFP_KERNEL);
1255                 if (sattr->name == NULL)
1256                         goto out;
1257                 sect_attrs->nsections++;
1258                 sysfs_attr_init(&sattr->mattr.attr);
1259                 sattr->mattr.show = module_sect_show;
1260                 sattr->mattr.store = NULL;
1261                 sattr->mattr.attr.name = sattr->name;
1262                 sattr->mattr.attr.mode = S_IRUGO;
1263                 *(gattr++) = &(sattr++)->mattr.attr;
1264         }
1265         *gattr = NULL;
1266
1267         if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1268                 goto out;
1269
1270         mod->sect_attrs = sect_attrs;
1271         return;
1272   out:
1273         free_sect_attrs(sect_attrs);
1274 }
1275
1276 static void remove_sect_attrs(struct module *mod)
1277 {
1278         if (mod->sect_attrs) {
1279                 sysfs_remove_group(&mod->mkobj.kobj,
1280                                    &mod->sect_attrs->grp);
1281                 /* We are positive that no one is using any sect attrs
1282                  * at this point.  Deallocate immediately. */
1283                 free_sect_attrs(mod->sect_attrs);
1284                 mod->sect_attrs = NULL;
1285         }
1286 }
1287
1288 /*
1289  * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1290  */
1291
1292 struct module_notes_attrs {
1293         struct kobject *dir;
1294         unsigned int notes;
1295         struct bin_attribute attrs[0];
1296 };
1297
1298 static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
1299                                  struct bin_attribute *bin_attr,
1300                                  char *buf, loff_t pos, size_t count)
1301 {
1302         /*
1303          * The caller checked the pos and count against our size.
1304          */
1305         memcpy(buf, bin_attr->private + pos, count);
1306         return count;
1307 }
1308
1309 static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1310                              unsigned int i)
1311 {
1312         if (notes_attrs->dir) {
1313                 while (i-- > 0)
1314                         sysfs_remove_bin_file(notes_attrs->dir,
1315                                               &notes_attrs->attrs[i]);
1316                 kobject_put(notes_attrs->dir);
1317         }
1318         kfree(notes_attrs);
1319 }
1320
1321 static void add_notes_attrs(struct module *mod, const struct load_info *info)
1322 {
1323         unsigned int notes, loaded, i;
1324         struct module_notes_attrs *notes_attrs;
1325         struct bin_attribute *nattr;
1326
1327         /* failed to create section attributes, so can't create notes */
1328         if (!mod->sect_attrs)
1329                 return;
1330
1331         /* Count notes sections and allocate structures.  */
1332         notes = 0;
1333         for (i = 0; i < info->hdr->e_shnum; i++)
1334                 if (!sect_empty(&info->sechdrs[i]) &&
1335                     (info->sechdrs[i].sh_type == SHT_NOTE))
1336                         ++notes;
1337
1338         if (notes == 0)
1339                 return;
1340
1341         notes_attrs = kzalloc(sizeof(*notes_attrs)
1342                               + notes * sizeof(notes_attrs->attrs[0]),
1343                               GFP_KERNEL);
1344         if (notes_attrs == NULL)
1345                 return;
1346
1347         notes_attrs->notes = notes;
1348         nattr = &notes_attrs->attrs[0];
1349         for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1350                 if (sect_empty(&info->sechdrs[i]))
1351                         continue;
1352                 if (info->sechdrs[i].sh_type == SHT_NOTE) {
1353                         sysfs_bin_attr_init(nattr);
1354                         nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1355                         nattr->attr.mode = S_IRUGO;
1356                         nattr->size = info->sechdrs[i].sh_size;
1357                         nattr->private = (void *) info->sechdrs[i].sh_addr;
1358                         nattr->read = module_notes_read;
1359                         ++nattr;
1360                 }
1361                 ++loaded;
1362         }
1363
1364         notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
1365         if (!notes_attrs->dir)
1366                 goto out;
1367
1368         for (i = 0; i < notes; ++i)
1369                 if (sysfs_create_bin_file(notes_attrs->dir,
1370                                           &notes_attrs->attrs[i]))
1371                         goto out;
1372
1373         mod->notes_attrs = notes_attrs;
1374         return;
1375
1376   out:
1377         free_notes_attrs(notes_attrs, i);
1378 }
1379
1380 static void remove_notes_attrs(struct module *mod)
1381 {
1382         if (mod->notes_attrs)
1383                 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1384 }
1385
1386 #else
1387
1388 static inline void add_sect_attrs(struct module *mod,
1389                                   const struct load_info *info)
1390 {
1391 }
1392
1393 static inline void remove_sect_attrs(struct module *mod)
1394 {
1395 }
1396
1397 static inline void add_notes_attrs(struct module *mod,
1398                                    const struct load_info *info)
1399 {
1400 }
1401
1402 static inline void remove_notes_attrs(struct module *mod)
1403 {
1404 }
1405 #endif /* CONFIG_KALLSYMS */
1406
1407 static void add_usage_links(struct module *mod)
1408 {
1409 #ifdef CONFIG_MODULE_UNLOAD
1410         struct module_use *use;
1411         int nowarn;
1412
1413         mutex_lock(&module_mutex);
1414         list_for_each_entry(use, &mod->target_list, target_list) {
1415                 nowarn = sysfs_create_link(use->target->holders_dir,
1416                                            &mod->mkobj.kobj, mod->name);
1417         }
1418         mutex_unlock(&module_mutex);
1419 #endif
1420 }
1421
1422 static void del_usage_links(struct module *mod)
1423 {
1424 #ifdef CONFIG_MODULE_UNLOAD
1425         struct module_use *use;
1426
1427         mutex_lock(&module_mutex);
1428         list_for_each_entry(use, &mod->target_list, target_list)
1429                 sysfs_remove_link(use->target->holders_dir, mod->name);
1430         mutex_unlock(&module_mutex);
1431 #endif
1432 }
1433
1434 static int module_add_modinfo_attrs(struct module *mod)
1435 {
1436         struct module_attribute *attr;
1437         struct module_attribute *temp_attr;
1438         int error = 0;
1439         int i;
1440
1441         mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1442                                         (ARRAY_SIZE(modinfo_attrs) + 1)),
1443                                         GFP_KERNEL);
1444         if (!mod->modinfo_attrs)
1445                 return -ENOMEM;
1446
1447         temp_attr = mod->modinfo_attrs;
1448         for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1449                 if (!attr->test ||
1450                     (attr->test && attr->test(mod))) {
1451                         memcpy(temp_attr, attr, sizeof(*temp_attr));
1452                         sysfs_attr_init(&temp_attr->attr);
1453                         error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1454                         ++temp_attr;
1455                 }
1456         }
1457         return error;
1458 }
1459
1460 static void module_remove_modinfo_attrs(struct module *mod)
1461 {
1462         struct module_attribute *attr;
1463         int i;
1464
1465         for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1466                 /* pick a field to test for end of list */
1467                 if (!attr->attr.name)
1468                         break;
1469                 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1470                 if (attr->free)
1471                         attr->free(mod);
1472         }
1473         kfree(mod->modinfo_attrs);
1474 }
1475
1476 static int mod_sysfs_init(struct module *mod)
1477 {
1478         int err;
1479         struct kobject *kobj;
1480
1481         if (!module_sysfs_initialized) {
1482                 printk(KERN_ERR "%s: module sysfs not initialized\n",
1483                        mod->name);
1484                 err = -EINVAL;
1485                 goto out;
1486         }
1487
1488         kobj = kset_find_obj(module_kset, mod->name);
1489         if (kobj) {
1490                 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1491                 kobject_put(kobj);
1492                 err = -EINVAL;
1493                 goto out;
1494         }
1495
1496         mod->mkobj.mod = mod;
1497
1498         memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1499         mod->mkobj.kobj.kset = module_kset;
1500         err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1501                                    "%s", mod->name);
1502         if (err)
1503                 kobject_put(&mod->mkobj.kobj);
1504
1505         /* delay uevent until full sysfs population */
1506 out:
1507         return err;
1508 }
1509
1510 static int mod_sysfs_setup(struct module *mod,
1511                            const struct load_info *info,
1512                            struct kernel_param *kparam,
1513                            unsigned int num_params)
1514 {
1515         int err;
1516
1517         err = mod_sysfs_init(mod);
1518         if (err)
1519                 goto out;
1520
1521         mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1522         if (!mod->holders_dir) {
1523                 err = -ENOMEM;
1524                 goto out_unreg;
1525         }
1526
1527         err = module_param_sysfs_setup(mod, kparam, num_params);
1528         if (err)
1529                 goto out_unreg_holders;
1530
1531         err = module_add_modinfo_attrs(mod);
1532         if (err)
1533                 goto out_unreg_param;
1534
1535         add_usage_links(mod);
1536         add_sect_attrs(mod, info);
1537         add_notes_attrs(mod, info);
1538
1539         kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1540         return 0;
1541
1542 out_unreg_param:
1543         module_param_sysfs_remove(mod);
1544 out_unreg_holders:
1545         kobject_put(mod->holders_dir);
1546 out_unreg:
1547         kobject_put(&mod->mkobj.kobj);
1548 out:
1549         return err;
1550 }
1551
1552 static void mod_sysfs_fini(struct module *mod)
1553 {
1554         remove_notes_attrs(mod);
1555         remove_sect_attrs(mod);
1556         kobject_put(&mod->mkobj.kobj);
1557 }
1558
1559 #else /* !CONFIG_SYSFS */
1560
1561 static int mod_sysfs_setup(struct module *mod,
1562                            const struct load_info *info,
1563                            struct kernel_param *kparam,
1564                            unsigned int num_params)
1565 {
1566         return 0;
1567 }
1568
1569 static void mod_sysfs_fini(struct module *mod)
1570 {
1571 }
1572
1573 static void module_remove_modinfo_attrs(struct module *mod)
1574 {
1575 }
1576
1577 static void del_usage_links(struct module *mod)
1578 {
1579 }
1580
1581 #endif /* CONFIG_SYSFS */
1582
1583 static void mod_sysfs_teardown(struct module *mod)
1584 {
1585         del_usage_links(mod);
1586         module_remove_modinfo_attrs(mod);
1587         module_param_sysfs_remove(mod);
1588         kobject_put(mod->mkobj.drivers_dir);
1589         kobject_put(mod->holders_dir);
1590         mod_sysfs_fini(mod);
1591 }
1592
1593 /*
1594  * unlink the module with the whole machine is stopped with interrupts off
1595  * - this defends against kallsyms not taking locks
1596  */
1597 static int __unlink_module(void *_mod)
1598 {
1599         struct module *mod = _mod;
1600         list_del(&mod->list);
1601         module_bug_cleanup(mod);
1602         return 0;
1603 }
1604
1605 #ifdef CONFIG_DEBUG_SET_MODULE_RONX
1606 /*
1607  * LKM RO/NX protection: protect module's text/ro-data
1608  * from modification and any data from execution.
1609  */
1610 void set_page_attributes(void *start, void *end, int (*set)(unsigned long start, int num_pages))
1611 {
1612         unsigned long begin_pfn = PFN_DOWN((unsigned long)start);
1613         unsigned long end_pfn = PFN_DOWN((unsigned long)end);
1614
1615         if (end_pfn > begin_pfn)
1616                 set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1617 }
1618
1619 static void set_section_ro_nx(void *base,
1620                         unsigned long text_size,
1621                         unsigned long ro_size,
1622                         unsigned long total_size)
1623 {
1624         /* begin and end PFNs of the current subsection */
1625         unsigned long begin_pfn;
1626         unsigned long end_pfn;
1627
1628         /*
1629          * Set RO for module text and RO-data:
1630          * - Always protect first page.
1631          * - Do not protect last partial page.
1632          */
1633         if (ro_size > 0)
1634                 set_page_attributes(base, base + ro_size, set_memory_ro);
1635
1636         /*
1637          * Set NX permissions for module data:
1638          * - Do not protect first partial page.
1639          * - Always protect last page.
1640          */
1641         if (total_size > text_size) {
1642                 begin_pfn = PFN_UP((unsigned long)base + text_size);
1643                 end_pfn = PFN_UP((unsigned long)base + total_size);
1644                 if (end_pfn > begin_pfn)
1645                         set_memory_nx(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1646         }
1647 }
1648
1649 static void unset_module_core_ro_nx(struct module *mod)
1650 {
1651         set_page_attributes(mod->module_core + mod->core_text_size,
1652                 mod->module_core + mod->core_size,
1653                 set_memory_x);
1654         set_page_attributes(mod->module_core,
1655                 mod->module_core + mod->core_ro_size,
1656                 set_memory_rw);
1657 }
1658
1659 static void unset_module_init_ro_nx(struct module *mod)
1660 {
1661         set_page_attributes(mod->module_init + mod->init_text_size,
1662                 mod->module_init + mod->init_size,
1663                 set_memory_x);
1664         set_page_attributes(mod->module_init,
1665                 mod->module_init + mod->init_ro_size,
1666                 set_memory_rw);
1667 }
1668
1669 /* Iterate through all modules and set each module's text as RW */
1670 void set_all_modules_text_rw(void)
1671 {
1672         struct module *mod;
1673
1674         mutex_lock(&module_mutex);
1675         list_for_each_entry_rcu(mod, &modules, list) {
1676                 if ((mod->module_core) && (mod->core_text_size)) {
1677                         set_page_attributes(mod->module_core,
1678                                                 mod->module_core + mod->core_text_size,
1679                                                 set_memory_rw);
1680                 }
1681                 if ((mod->module_init) && (mod->init_text_size)) {
1682                         set_page_attributes(mod->module_init,
1683                                                 mod->module_init + mod->init_text_size,
1684                                                 set_memory_rw);
1685                 }
1686         }
1687         mutex_unlock(&module_mutex);
1688 }
1689
1690 /* Iterate through all modules and set each module's text as RO */
1691 void set_all_modules_text_ro(void)
1692 {
1693         struct module *mod;
1694
1695         mutex_lock(&module_mutex);
1696         list_for_each_entry_rcu(mod, &modules, list) {
1697                 if ((mod->module_core) && (mod->core_text_size)) {
1698                         set_page_attributes(mod->module_core,
1699                                                 mod->module_core + mod->core_text_size,
1700                                                 set_memory_ro);
1701                 }
1702                 if ((mod->module_init) && (mod->init_text_size)) {
1703                         set_page_attributes(mod->module_init,
1704                                                 mod->module_init + mod->init_text_size,
1705                                                 set_memory_ro);
1706                 }
1707         }
1708         mutex_unlock(&module_mutex);
1709 }
1710 #else
1711 static inline void set_section_ro_nx(void *base, unsigned long text_size, unsigned long ro_size, unsigned long total_size) { }
1712 static void unset_module_core_ro_nx(struct module *mod) { }
1713 static void unset_module_init_ro_nx(struct module *mod) { }
1714 #endif
1715
1716 void __weak module_free(struct module *mod, void *module_region)
1717 {
1718         vfree(module_region);
1719 }
1720
1721 void __weak module_arch_cleanup(struct module *mod)
1722 {
1723 }
1724
1725 /* Free a module, remove from lists, etc. */
1726 static void free_module(struct module *mod)
1727 {
1728         trace_module_free(mod);
1729
1730         /* Delete from various lists */
1731         mutex_lock(&module_mutex);
1732         stop_machine(__unlink_module, mod, NULL);
1733         mutex_unlock(&module_mutex);
1734         mod_sysfs_teardown(mod);
1735
1736         /* Remove dynamic debug info */
1737         ddebug_remove_module(mod->name);
1738
1739         /* Arch-specific cleanup. */
1740         module_arch_cleanup(mod);
1741
1742         /* Module unload stuff */
1743         module_unload_free(mod);
1744
1745         /* Free any allocated parameters. */
1746         destroy_params(mod->kp, mod->num_kp);
1747
1748         /* This may be NULL, but that's OK */
1749         unset_module_init_ro_nx(mod);
1750         module_free(mod, mod->module_init);
1751         kfree(mod->args);
1752         percpu_modfree(mod);
1753
1754         /* Free lock-classes: */
1755         lockdep_free_key_range(mod->module_core, mod->core_size);
1756
1757         /* Finally, free the core (containing the module structure) */
1758         unset_module_core_ro_nx(mod);
1759         module_free(mod, mod->module_core);
1760
1761 #ifdef CONFIG_MPU
1762         update_protections(current->mm);
1763 #endif
1764 }
1765
1766 void *__symbol_get(const char *symbol)
1767 {
1768         struct module *owner;
1769         const struct kernel_symbol *sym;
1770
1771         preempt_disable();
1772         sym = find_symbol(symbol, &owner, NULL, true, true);
1773         if (sym && strong_try_module_get(owner))
1774                 sym = NULL;
1775         preempt_enable();
1776
1777         return sym ? (void *)sym->value : NULL;
1778 }
1779 EXPORT_SYMBOL_GPL(__symbol_get);
1780
1781 /*
1782  * Ensure that an exported symbol [global namespace] does not already exist
1783  * in the kernel or in some other module's exported symbol table.
1784  *
1785  * You must hold the module_mutex.
1786  */
1787 static int verify_export_symbols(struct module *mod)
1788 {
1789         unsigned int i;
1790         struct module *owner;
1791         const struct kernel_symbol *s;
1792         struct {
1793                 const struct kernel_symbol *sym;
1794                 unsigned int num;
1795         } arr[] = {
1796                 { mod->syms, mod->num_syms },
1797                 { mod->gpl_syms, mod->num_gpl_syms },
1798                 { mod->gpl_future_syms, mod->num_gpl_future_syms },
1799 #ifdef CONFIG_UNUSED_SYMBOLS
1800                 { mod->unused_syms, mod->num_unused_syms },
1801                 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
1802 #endif
1803         };
1804
1805         for (i = 0; i < ARRAY_SIZE(arr); i++) {
1806                 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1807                         if (find_symbol(s->name, &owner, NULL, true, false)) {
1808                                 printk(KERN_ERR
1809                                        "%s: exports duplicate symbol %s"
1810                                        " (owned by %s)\n",
1811                                        mod->name, s->name, module_name(owner));
1812                                 return -ENOEXEC;
1813                         }
1814                 }
1815         }
1816         return 0;
1817 }
1818
1819 /* Change all symbols so that st_value encodes the pointer directly. */
1820 static int simplify_symbols(struct module *mod, const struct load_info *info)
1821 {
1822         Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
1823         Elf_Sym *sym = (void *)symsec->sh_addr;
1824         unsigned long secbase;
1825         unsigned int i;
1826         int ret = 0;
1827         const struct kernel_symbol *ksym;
1828
1829         for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
1830                 const char *name = info->strtab + sym[i].st_name;
1831
1832                 switch (sym[i].st_shndx) {
1833                 case SHN_COMMON:
1834                         /* We compiled with -fno-common.  These are not
1835                            supposed to happen.  */
1836                         DEBUGP("Common symbol: %s\n", name);
1837                         printk("%s: please compile with -fno-common\n",
1838                                mod->name);
1839                         ret = -ENOEXEC;
1840                         break;
1841
1842                 case SHN_ABS:
1843                         /* Don't need to do anything */
1844                         DEBUGP("Absolute symbol: 0x%08lx\n",
1845                                (long)sym[i].st_value);
1846                         break;
1847
1848                 case SHN_UNDEF:
1849                         ksym = resolve_symbol_wait(mod, info, name);
1850                         /* Ok if resolved.  */
1851                         if (ksym && !IS_ERR(ksym)) {
1852                                 sym[i].st_value = ksym->value;
1853                                 break;
1854                         }
1855
1856                         /* Ok if weak.  */
1857                         if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1858                                 break;
1859
1860                         printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
1861                                mod->name, name, PTR_ERR(ksym));
1862                         ret = PTR_ERR(ksym) ?: -ENOENT;
1863                         break;
1864
1865                 default:
1866                         /* Divert to percpu allocation if a percpu var. */
1867                         if (sym[i].st_shndx == info->index.pcpu)
1868                                 secbase = (unsigned long)mod_percpu(mod);
1869                         else
1870                                 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
1871                         sym[i].st_value += secbase;
1872                         break;
1873                 }
1874         }
1875
1876         return ret;
1877 }
1878
1879 int __weak apply_relocate(Elf_Shdr *sechdrs,
1880                           const char *strtab,
1881                           unsigned int symindex,
1882                           unsigned int relsec,
1883                           struct module *me)
1884 {
1885         pr_err("module %s: REL relocation unsupported\n", me->name);
1886         return -ENOEXEC;
1887 }
1888
1889 int __weak apply_relocate_add(Elf_Shdr *sechdrs,
1890                               const char *strtab,
1891                               unsigned int symindex,
1892                               unsigned int relsec,
1893                               struct module *me)
1894 {
1895         pr_err("module %s: RELA relocation unsupported\n", me->name);
1896         return -ENOEXEC;
1897 }
1898
1899 static int apply_relocations(struct module *mod, const struct load_info *info)
1900 {
1901         unsigned int i;
1902         int err = 0;
1903
1904         /* Now do relocations. */
1905         for (i = 1; i < info->hdr->e_shnum; i++) {
1906                 unsigned int infosec = info->sechdrs[i].sh_info;
1907
1908                 /* Not a valid relocation section? */
1909                 if (infosec >= info->hdr->e_shnum)
1910                         continue;
1911
1912                 /* Don't bother with non-allocated sections */
1913                 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
1914                         continue;
1915
1916                 if (info->sechdrs[i].sh_type == SHT_REL)
1917                         err = apply_relocate(info->sechdrs, info->strtab,
1918                                              info->index.sym, i, mod);
1919                 else if (info->sechdrs[i].sh_type == SHT_RELA)
1920                         err = apply_relocate_add(info->sechdrs, info->strtab,
1921                                                  info->index.sym, i, mod);
1922                 if (err < 0)
1923                         break;
1924         }
1925         return err;
1926 }
1927
1928 /* Additional bytes needed by arch in front of individual sections */
1929 unsigned int __weak arch_mod_section_prepend(struct module *mod,
1930                                              unsigned int section)
1931 {
1932         /* default implementation just returns zero */
1933         return 0;
1934 }
1935
1936 /* Update size with this section: return offset. */
1937 static long get_offset(struct module *mod, unsigned int *size,
1938                        Elf_Shdr *sechdr, unsigned int section)
1939 {
1940         long ret;
1941
1942         *size += arch_mod_section_prepend(mod, section);
1943         ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1944         *size = ret + sechdr->sh_size;
1945         return ret;
1946 }
1947
1948 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1949    might -- code, read-only data, read-write data, small data.  Tally
1950    sizes, and place the offsets into sh_entsize fields: high bit means it
1951    belongs in init. */
1952 static void layout_sections(struct module *mod, struct load_info *info)
1953 {
1954         static unsigned long const masks[][2] = {
1955                 /* NOTE: all executable code must be the first section
1956                  * in this array; otherwise modify the text_size
1957                  * finder in the two loops below */
1958                 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1959                 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1960                 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1961                 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1962         };
1963         unsigned int m, i;
1964
1965         for (i = 0; i < info->hdr->e_shnum; i++)
1966                 info->sechdrs[i].sh_entsize = ~0UL;
1967
1968         DEBUGP("Core section allocation order:\n");
1969         for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1970                 for (i = 0; i < info->hdr->e_shnum; ++i) {
1971                         Elf_Shdr *s = &info->sechdrs[i];
1972                         const char *sname = info->secstrings + s->sh_name;
1973
1974                         if ((s->sh_flags & masks[m][0]) != masks[m][0]
1975                             || (s->sh_flags & masks[m][1])
1976                             || s->sh_entsize != ~0UL
1977                             || strstarts(sname, ".init"))
1978                                 continue;
1979                         s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
1980                         DEBUGP("\t%s\n", name);
1981                 }
1982                 switch (m) {
1983                 case 0: /* executable */
1984                         mod->core_size = debug_align(mod->core_size);
1985                         mod->core_text_size = mod->core_size;
1986                         break;
1987                 case 1: /* RO: text and ro-data */
1988                         mod->core_size = debug_align(mod->core_size);
1989                         mod->core_ro_size = mod->core_size;
1990                         break;
1991                 case 3: /* whole core */
1992                         mod->core_size = debug_align(mod->core_size);
1993                         break;
1994                 }
1995         }
1996
1997         DEBUGP("Init section allocation order:\n");
1998         for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1999                 for (i = 0; i < info->hdr->e_shnum; ++i) {
2000                         Elf_Shdr *s = &info->sechdrs[i];
2001                         const char *sname = info->secstrings + s->sh_name;
2002
2003                         if ((s->sh_flags & masks[m][0]) != masks[m][0]
2004                             || (s->sh_flags & masks[m][1])
2005                             || s->sh_entsize != ~0UL
2006                             || !strstarts(sname, ".init"))
2007                                 continue;
2008                         s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
2009                                          | INIT_OFFSET_MASK);
2010                         DEBUGP("\t%s\n", sname);
2011                 }
2012                 switch (m) {
2013                 case 0: /* executable */
2014                         mod->init_size = debug_align(mod->init_size);
2015                         mod->init_text_size = mod->init_size;
2016                         break;
2017                 case 1: /* RO: text and ro-data */
2018                         mod->init_size = debug_align(mod->init_size);
2019                         mod->init_ro_size = mod->init_size;
2020                         break;
2021                 case 3: /* whole init */
2022                         mod->init_size = debug_align(mod->init_size);
2023                         break;
2024                 }
2025         }
2026 }
2027
2028 static void set_license(struct module *mod, const char *license)
2029 {
2030         if (!license)
2031                 license = "unspecified";
2032
2033         if (!license_is_gpl_compatible(license)) {
2034                 if (!test_taint(TAINT_PROPRIETARY_MODULE))
2035                         printk(KERN_WARNING "%s: module license '%s' taints "
2036                                 "kernel.\n", mod->name, license);
2037                 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2038         }
2039 }
2040
2041 /* Parse tag=value strings from .modinfo section */
2042 static char *next_string(char *string, unsigned long *secsize)
2043 {
2044         /* Skip non-zero chars */
2045         while (string[0]) {
2046                 string++;
2047                 if ((*secsize)-- <= 1)
2048                         return NULL;
2049         }
2050
2051         /* Skip any zero padding. */
2052         while (!string[0]) {
2053                 string++;
2054                 if ((*secsize)-- <= 1)
2055                         return NULL;
2056         }
2057         return string;
2058 }
2059
2060 static char *get_modinfo(struct load_info *info, const char *tag)
2061 {
2062         char *p;
2063         unsigned int taglen = strlen(tag);
2064         Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2065         unsigned long size = infosec->sh_size;
2066
2067         for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
2068                 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2069                         return p + taglen + 1;
2070         }
2071         return NULL;
2072 }
2073
2074 static void setup_modinfo(struct module *mod, struct load_info *info)
2075 {
2076         struct module_attribute *attr;
2077         int i;
2078
2079         for (i = 0; (attr = modinfo_attrs[i]); i++) {
2080                 if (attr->setup)
2081                         attr->setup(mod, get_modinfo(info, attr->attr.name));
2082         }
2083 }
2084
2085 static void free_modinfo(struct module *mod)
2086 {
2087         struct module_attribute *attr;
2088         int i;
2089
2090         for (i = 0; (attr = modinfo_attrs[i]); i++) {
2091                 if (attr->free)
2092                         attr->free(mod);
2093         }
2094 }
2095
2096 #ifdef CONFIG_KALLSYMS
2097
2098 /* lookup symbol in given range of kernel_symbols */
2099 static const struct kernel_symbol *lookup_symbol(const char *name,
2100         const struct kernel_symbol *start,
2101         const struct kernel_symbol *stop)
2102 {
2103         return bsearch(name, start, stop - start,
2104                         sizeof(struct kernel_symbol), cmp_name);
2105 }
2106
2107 static int is_exported(const char *name, unsigned long value,
2108                        const struct module *mod)
2109 {
2110         const struct kernel_symbol *ks;
2111         if (!mod)
2112                 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
2113         else
2114                 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
2115         return ks != NULL && ks->value == value;
2116 }
2117
2118 /* As per nm */
2119 static char elf_type(const Elf_Sym *sym, const struct load_info *info)
2120 {
2121         const Elf_Shdr *sechdrs = info->sechdrs;
2122
2123         if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2124                 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2125                         return 'v';
2126                 else
2127                         return 'w';
2128         }
2129         if (sym->st_shndx == SHN_UNDEF)
2130                 return 'U';
2131         if (sym->st_shndx == SHN_ABS)
2132                 return 'a';
2133         if (sym->st_shndx >= SHN_LORESERVE)
2134                 return '?';
2135         if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2136                 return 't';
2137         if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2138             && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2139                 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2140                         return 'r';
2141                 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2142                         return 'g';
2143                 else
2144                         return 'd';
2145         }
2146         if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2147                 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2148                         return 's';
2149                 else
2150                         return 'b';
2151         }
2152         if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2153                       ".debug")) {
2154                 return 'n';
2155         }
2156         return '?';
2157 }
2158
2159 static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
2160                            unsigned int shnum)
2161 {
2162         const Elf_Shdr *sec;
2163
2164         if (src->st_shndx == SHN_UNDEF
2165             || src->st_shndx >= shnum
2166             || !src->st_name)
2167                 return false;
2168
2169         sec = sechdrs + src->st_shndx;
2170         if (!(sec->sh_flags & SHF_ALLOC)
2171 #ifndef CONFIG_KALLSYMS_ALL
2172             || !(sec->sh_flags & SHF_EXECINSTR)
2173 #endif
2174             || (sec->sh_entsize & INIT_OFFSET_MASK))
2175                 return false;
2176
2177         return true;
2178 }
2179
2180 /*
2181  * We only allocate and copy the strings needed by the parts of symtab
2182  * we keep.  This is simple, but has the effect of making multiple
2183  * copies of duplicates.  We could be more sophisticated, see
2184  * linux-kernel thread starting with
2185  * <73defb5e4bca04a6431392cc341112b1@localhost>.
2186  */
2187 static void layout_symtab(struct module *mod, struct load_info *info)
2188 {
2189         Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2190         Elf_Shdr *strsect = info->sechdrs + info->index.str;
2191         const Elf_Sym *src;
2192         unsigned int i, nsrc, ndst, strtab_size;
2193
2194         /* Put symbol section at end of init part of module. */
2195         symsect->sh_flags |= SHF_ALLOC;
2196         symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
2197                                          info->index.sym) | INIT_OFFSET_MASK;
2198         DEBUGP("\t%s\n", info->secstrings + symsect->sh_name);
2199
2200         src = (void *)info->hdr + symsect->sh_offset;
2201         nsrc = symsect->sh_size / sizeof(*src);
2202
2203         /* Compute total space required for the core symbols' strtab. */
2204         for (ndst = i = strtab_size = 1; i < nsrc; ++i, ++src)
2205                 if (is_core_symbol(src, info->sechdrs, info->hdr->e_shnum)) {
2206                         strtab_size += strlen(&info->strtab[src->st_name]) + 1;
2207                         ndst++;
2208                 }
2209
2210         /* Append room for core symbols at end of core part. */
2211         info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
2212         info->stroffs = mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
2213         mod->core_size += strtab_size;
2214
2215         /* Put string table section at end of init part of module. */
2216         strsect->sh_flags |= SHF_ALLOC;
2217         strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
2218                                          info->index.str) | INIT_OFFSET_MASK;
2219         DEBUGP("\t%s\n", info->secstrings + strsect->sh_name);
2220 }
2221
2222 static void add_kallsyms(struct module *mod, const struct load_info *info)
2223 {
2224         unsigned int i, ndst;
2225         const Elf_Sym *src;
2226         Elf_Sym *dst;
2227         char *s;
2228         Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
2229
2230         mod->symtab = (void *)symsec->sh_addr;
2231         mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
2232         /* Make sure we get permanent strtab: don't use info->strtab. */
2233         mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
2234
2235         /* Set types up while we still have access to sections. */
2236         for (i = 0; i < mod->num_symtab; i++)
2237                 mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
2238
2239         mod->core_symtab = dst = mod->module_core + info->symoffs;
2240         mod->core_strtab = s = mod->module_core + info->stroffs;
2241         src = mod->symtab;
2242         *dst = *src;
2243         *s++ = 0;
2244         for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
2245                 if (!is_core_symbol(src, info->sechdrs, info->hdr->e_shnum))
2246                         continue;
2247
2248                 dst[ndst] = *src;
2249                 dst[ndst++].st_name = s - mod->core_strtab;
2250                 s += strlcpy(s, &mod->strtab[src->st_name], KSYM_NAME_LEN) + 1;
2251         }
2252         mod->core_num_syms = ndst;
2253 }
2254 #else
2255 static inline void layout_symtab(struct module *mod, struct load_info *info)
2256 {
2257 }
2258
2259 static void add_kallsyms(struct module *mod, const struct load_info *info)
2260 {
2261 }
2262 #endif /* CONFIG_KALLSYMS */
2263
2264 static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
2265 {
2266         if (!debug)
2267                 return;
2268 #ifdef CONFIG_DYNAMIC_DEBUG
2269         if (ddebug_add_module(debug, num, debug->modname))
2270                 printk(KERN_ERR "dynamic debug error adding module: %s\n",
2271                                         debug->modname);
2272 #endif
2273 }
2274
2275 static void dynamic_debug_remove(struct _ddebug *debug)
2276 {
2277         if (debug)
2278                 ddebug_remove_module(debug->modname);
2279 }
2280
2281 void * __weak module_alloc(unsigned long size)
2282 {
2283         return size == 0 ? NULL : vmalloc_exec(size);
2284 }
2285
2286 static void *module_alloc_update_bounds(unsigned long size)
2287 {
2288         void *ret = module_alloc(size);
2289
2290         if (ret) {
2291                 mutex_lock(&module_mutex);
2292                 /* Update module bounds. */
2293                 if ((unsigned long)ret < module_addr_min)
2294                         module_addr_min = (unsigned long)ret;
2295                 if ((unsigned long)ret + size > module_addr_max)
2296                         module_addr_max = (unsigned long)ret + size;
2297                 mutex_unlock(&module_mutex);
2298         }
2299         return ret;
2300 }
2301
2302 #ifdef CONFIG_DEBUG_KMEMLEAK
2303 static void kmemleak_load_module(const struct module *mod,
2304                                  const struct load_info *info)
2305 {
2306         unsigned int i;
2307
2308         /* only scan the sections containing data */
2309         kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
2310
2311         for (i = 1; i < info->hdr->e_shnum; i++) {
2312                 const char *name = info->secstrings + info->sechdrs[i].sh_name;
2313                 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC))
2314                         continue;
2315                 if (!strstarts(name, ".data") && !strstarts(name, ".bss"))
2316                         continue;
2317
2318                 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2319                                    info->sechdrs[i].sh_size, GFP_KERNEL);
2320         }
2321 }
2322 #else
2323 static inline void kmemleak_load_module(const struct module *mod,
2324                                         const struct load_info *info)
2325 {
2326 }
2327 #endif
2328
2329 /* Sets info->hdr and info->len. */
2330 static int copy_and_check(struct load_info *info,
2331                           const void __user *umod, unsigned long len,
2332                           const char __user *uargs)
2333 {
2334         int err;
2335         Elf_Ehdr *hdr;
2336
2337         if (len < sizeof(*hdr))
2338                 return -ENOEXEC;
2339
2340         /* Suck in entire file: we'll want most of it. */
2341         /* vmalloc barfs on "unusual" numbers.  Check here */
2342         if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
2343                 return -ENOMEM;
2344
2345         if (copy_from_user(hdr, umod, len) != 0) {
2346                 err = -EFAULT;
2347                 goto free_hdr;
2348         }
2349
2350         /* Sanity checks against insmoding binaries or wrong arch,
2351            weird elf version */
2352         if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
2353             || hdr->e_type != ET_REL
2354             || !elf_check_arch(hdr)
2355             || hdr->e_shentsize != sizeof(Elf_Shdr)) {
2356                 err = -ENOEXEC;
2357                 goto free_hdr;
2358         }
2359
2360         if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) {
2361                 err = -ENOEXEC;
2362                 goto free_hdr;
2363         }
2364
2365         info->hdr = hdr;
2366         info->len = len;
2367         return 0;
2368
2369 free_hdr:
2370         vfree(hdr);
2371         return err;
2372 }
2373
2374 static void free_copy(struct load_info *info)
2375 {
2376         vfree(info->hdr);
2377 }
2378
2379 static int rewrite_section_headers(struct load_info *info)
2380 {
2381         unsigned int i;
2382
2383         /* This should always be true, but let's be sure. */
2384         info->sechdrs[0].sh_addr = 0;
2385
2386         for (i = 1; i < info->hdr->e_shnum; i++) {
2387                 Elf_Shdr *shdr = &info->sechdrs[i];
2388                 if (shdr->sh_type != SHT_NOBITS
2389                     && info->len < shdr->sh_offset + shdr->sh_size) {
2390                         printk(KERN_ERR "Module len %lu truncated\n",
2391                                info->len);
2392                         return -ENOEXEC;
2393                 }
2394
2395                 /* Mark all sections sh_addr with their address in the
2396                    temporary image. */
2397                 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2398
2399 #ifndef CONFIG_MODULE_UNLOAD
2400                 /* Don't load .exit sections */
2401                 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2402                         shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2403 #endif
2404         }
2405
2406         /* Track but don't keep modinfo and version sections. */
2407         info->index.vers = find_sec(info, "__versions");
2408         info->index.info = find_sec(info, ".modinfo");
2409         info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2410         info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
2411         return 0;
2412 }
2413
2414 /*
2415  * Set up our basic convenience variables (pointers to section headers,
2416  * search for module section index etc), and do some basic section
2417  * verification.
2418  *
2419  * Return the temporary module pointer (we'll replace it with the final
2420  * one when we move the module sections around).
2421  */
2422 static struct module *setup_load_info(struct load_info *info)
2423 {
2424         unsigned int i;
2425         int err;
2426         struct module *mod;
2427
2428         /* Set up the convenience variables */
2429         info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
2430         info->secstrings = (void *)info->hdr
2431                 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
2432
2433         err = rewrite_section_headers(info);
2434         if (err)
2435                 return ERR_PTR(err);
2436
2437         /* Find internal symbols and strings. */
2438         for (i = 1; i < info->hdr->e_shnum; i++) {
2439                 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2440                         info->index.sym = i;
2441                         info->index.str = info->sechdrs[i].sh_link;
2442                         info->strtab = (char *)info->hdr
2443                                 + info->sechdrs[info->index.str].sh_offset;
2444                         break;
2445                 }
2446         }
2447
2448         info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
2449         if (!info->index.mod) {
2450                 printk(KERN_WARNING "No module found in object\n");
2451                 return ERR_PTR(-ENOEXEC);
2452         }
2453         /* This is temporary: point mod into copy of data. */
2454         mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2455
2456         if (info->index.sym == 0) {
2457                 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2458                        mod->name);
2459                 return ERR_PTR(-ENOEXEC);
2460         }
2461
2462         info->index.pcpu = find_pcpusec(info);
2463
2464         /* Check module struct version now, before we try to use module. */
2465         if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2466                 return ERR_PTR(-ENOEXEC);
2467
2468         return mod;
2469 }
2470
2471 static int check_modinfo(struct module *mod, struct load_info *info)
2472 {
2473         const char *modmagic = get_modinfo(info, "vermagic");
2474         int err;
2475
2476         /* This is allowed: modprobe --force will invalidate it. */
2477         if (!modmagic) {
2478                 err = try_to_force_load(mod, "bad vermagic");
2479                 if (err)
2480                         return err;
2481         } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
2482                 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2483                        mod->name, modmagic, vermagic);
2484                 return -ENOEXEC;
2485         }
2486
2487         if (!get_modinfo(info, "intree"))
2488                 add_taint_module(mod, TAINT_OOT_MODULE);
2489
2490         if (get_modinfo(info, "staging")) {
2491                 add_taint_module(mod, TAINT_CRAP);
2492                 printk(KERN_WARNING "%s: module is from the staging directory,"
2493                        " the quality is unknown, you have been warned.\n",
2494                        mod->name);
2495         }
2496
2497         /* Set up license info based on the info section */
2498         set_license(mod, get_modinfo(info, "license"));
2499
2500         return 0;
2501 }
2502
2503 static void find_module_sections(struct module *mod, struct load_info *info)
2504 {
2505         mod->kp = section_objs(info, "__param",
2506                                sizeof(*mod->kp), &mod->num_kp);
2507         mod->syms = section_objs(info, "__ksymtab",
2508                                  sizeof(*mod->syms), &mod->num_syms);
2509         mod->crcs = section_addr(info, "__kcrctab");
2510         mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
2511                                      sizeof(*mod->gpl_syms),
2512                                      &mod->num_gpl_syms);
2513         mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
2514         mod->gpl_future_syms = section_objs(info,
2515                                             "__ksymtab_gpl_future",
2516                                             sizeof(*mod->gpl_future_syms),
2517                                             &mod->num_gpl_future_syms);
2518         mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
2519
2520 #ifdef CONFIG_UNUSED_SYMBOLS
2521         mod->unused_syms = section_objs(info, "__ksymtab_unused",
2522                                         sizeof(*mod->unused_syms),
2523                                         &mod->num_unused_syms);
2524         mod->unused_crcs = section_addr(info, "__kcrctab_unused");
2525         mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
2526                                             sizeof(*mod->unused_gpl_syms),
2527                                             &mod->num_unused_gpl_syms);
2528         mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
2529 #endif
2530 #ifdef CONFIG_CONSTRUCTORS
2531         mod->ctors = section_objs(info, ".ctors",
2532                                   sizeof(*mod->ctors), &mod->num_ctors);
2533 #endif
2534
2535 #ifdef CONFIG_TRACEPOINTS
2536         mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
2537                                              sizeof(*mod->tracepoints_ptrs),
2538                                              &mod->num_tracepoints);
2539 #endif
2540 #ifdef HAVE_JUMP_LABEL
2541         mod->jump_entries = section_objs(info, "__jump_table",
2542                                         sizeof(*mod->jump_entries),
2543                                         &mod->num_jump_entries);
2544 #endif
2545 #ifdef CONFIG_EVENT_TRACING
2546         mod->trace_events = section_objs(info, "_ftrace_events",
2547                                          sizeof(*mod->trace_events),
2548                                          &mod->num_trace_events);
2549         /*
2550          * This section contains pointers to allocated objects in the trace
2551          * code and not scanning it leads to false positives.
2552          */
2553         kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2554                            mod->num_trace_events, GFP_KERNEL);
2555 #endif
2556 #ifdef CONFIG_TRACING
2557         mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
2558                                          sizeof(*mod->trace_bprintk_fmt_start),
2559                                          &mod->num_trace_bprintk_fmt);
2560         /*
2561          * This section contains pointers to allocated objects in the trace
2562          * code and not scanning it leads to false positives.
2563          */
2564         kmemleak_scan_area(mod->trace_bprintk_fmt_start,
2565                            sizeof(*mod->trace_bprintk_fmt_start) *
2566                            mod->num_trace_bprintk_fmt, GFP_KERNEL);
2567 #endif
2568 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
2569         /* sechdrs[0].sh_size is always zero */
2570         mod->ftrace_callsites = section_objs(info, "__mcount_loc",
2571                                              sizeof(*mod->ftrace_callsites),
2572                                              &mod->num_ftrace_callsites);
2573 #endif
2574
2575         mod->extable = section_objs(info, "__ex_table",
2576                                     sizeof(*mod->extable), &mod->num_exentries);
2577
2578         if (section_addr(info, "__obsparm"))
2579                 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2580                        mod->name);
2581
2582         info->debug = section_objs(info, "__verbose",
2583                                    sizeof(*info->debug), &info->num_debug);
2584 }
2585
2586 static int move_module(struct module *mod, struct load_info *info)
2587 {
2588         int i;
2589         void *ptr;
2590
2591         /* Do the allocs. */
2592         ptr = module_alloc_update_bounds(mod->core_size);
2593         /*
2594          * The pointer to this block is stored in the module structure
2595          * which is inside the block. Just mark it as not being a
2596          * leak.
2597          */
2598         kmemleak_not_leak(ptr);
2599         if (!ptr)
2600                 return -ENOMEM;
2601
2602         memset(ptr, 0, mod->core_size);
2603         mod->module_core = ptr;
2604
2605         ptr = module_alloc_update_bounds(mod->init_size);
2606         /*
2607          * The pointer to this block is stored in the module structure
2608          * which is inside the block. This block doesn't need to be
2609          * scanned as it contains data and code that will be freed
2610          * after the module is initialized.
2611          */
2612         kmemleak_ignore(ptr);
2613         if (!ptr && mod->init_size) {
2614                 module_free(mod, mod->module_core);
2615                 return -ENOMEM;
2616         }
2617         memset(ptr, 0, mod->init_size);
2618         mod->module_init = ptr;
2619
2620         /* Transfer each section which specifies SHF_ALLOC */
2621         DEBUGP("final section addresses:\n");
2622         for (i = 0; i < info->hdr->e_shnum; i++) {
2623                 void *dest;
2624                 Elf_Shdr *shdr = &info->sechdrs[i];
2625
2626                 if (!(shdr->sh_flags & SHF_ALLOC))
2627                         continue;
2628
2629                 if (shdr->sh_entsize & INIT_OFFSET_MASK)
2630                         dest = mod->module_init
2631                                 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
2632                 else
2633                         dest = mod->module_core + shdr->sh_entsize;
2634
2635                 if (shdr->sh_type != SHT_NOBITS)
2636                         memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
2637                 /* Update sh_addr to point to copy in image. */
2638                 shdr->sh_addr = (unsigned long)dest;
2639                 DEBUGP("\t0x%lx %s\n",
2640                        shdr->sh_addr, info->secstrings + shdr->sh_name);
2641         }
2642
2643         return 0;
2644 }
2645
2646 static int check_module_license_and_versions(struct module *mod)
2647 {
2648         /*
2649          * ndiswrapper is under GPL by itself, but loads proprietary modules.
2650          * Don't use add_taint_module(), as it would prevent ndiswrapper from
2651          * using GPL-only symbols it needs.
2652          */
2653         if (strcmp(mod->name, "ndiswrapper") == 0)
2654                 add_taint(TAINT_PROPRIETARY_MODULE);
2655
2656         /* driverloader was caught wrongly pretending to be under GPL */
2657         if (strcmp(mod->name, "driverloader") == 0)
2658                 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2659
2660 #ifdef CONFIG_MODVERSIONS
2661         if ((mod->num_syms && !mod->crcs)
2662             || (mod->num_gpl_syms && !mod->gpl_crcs)
2663             || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2664 #ifdef CONFIG_UNUSED_SYMBOLS
2665             || (mod->num_unused_syms && !mod->unused_crcs)
2666             || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2667 #endif
2668                 ) {
2669                 return try_to_force_load(mod,
2670                                          "no versions for exported symbols");
2671         }
2672 #endif
2673         return 0;
2674 }
2675
2676 static void flush_module_icache(const struct module *mod)
2677 {
2678         mm_segment_t old_fs;
2679
2680         /* flush the icache in correct context */
2681         old_fs = get_fs();
2682         set_fs(KERNEL_DS);
2683
2684         /*
2685          * Flush the instruction cache, since we've played with text.
2686          * Do it before processing of module parameters, so the module
2687          * can provide parameter accessor functions of its own.
2688          */
2689         if (mod->module_init)
2690                 flush_icache_range((unsigned long)mod->module_init,
2691                                    (unsigned long)mod->module_init
2692                                    + mod->init_size);
2693         flush_icache_range((unsigned long)mod->module_core,
2694                            (unsigned long)mod->module_core + mod->core_size);
2695
2696         set_fs(old_fs);
2697 }
2698
2699 int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
2700                                      Elf_Shdr *sechdrs,
2701                                      char *secstrings,
2702                                      struct module *mod)
2703 {
2704         return 0;
2705 }
2706
2707 static struct module *layout_and_allocate(struct load_info *info)
2708 {
2709         /* Module within temporary copy. */
2710         struct module *mod;
2711         Elf_Shdr *pcpusec;
2712         int err;
2713
2714         mod = setup_load_info(info);
2715         if (IS_ERR(mod))
2716                 return mod;
2717
2718         err = check_modinfo(mod, info);
2719         if (err)
2720                 return ERR_PTR(err);
2721
2722         /* Allow arches to frob section contents and sizes.  */
2723         err = module_frob_arch_sections(info->hdr, info->sechdrs,
2724                                         info->secstrings, mod);
2725         if (err < 0)
2726                 goto out;
2727
2728         pcpusec = &info->sechdrs[info->index.pcpu];
2729         if (pcpusec->sh_size) {
2730                 /* We have a special allocation for this section. */
2731                 err = percpu_modalloc(mod,
2732                                       pcpusec->sh_size, pcpusec->sh_addralign);
2733                 if (err)
2734                         goto out;
2735                 pcpusec->sh_flags &= ~(unsigned long)SHF_ALLOC;
2736         }
2737
2738         /* Determine total sizes, and put offsets in sh_entsize.  For now
2739            this is done generically; there doesn't appear to be any
2740            special cases for the architectures. */
2741         layout_sections(mod, info);
2742         layout_symtab(mod, info);
2743
2744         /* Allocate and move to the final place */
2745         err = move_module(mod, info);
2746         if (err)
2747                 goto free_percpu;
2748
2749         /* Module has been copied to its final place now: return it. */
2750         mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2751         kmemleak_load_module(mod, info);
2752         return mod;
2753
2754 free_percpu:
2755         percpu_modfree(mod);
2756 out:
2757         return ERR_PTR(err);
2758 }
2759
2760 /* mod is no longer valid after this! */
2761 static void module_deallocate(struct module *mod, struct load_info *info)
2762 {
2763         percpu_modfree(mod);
2764         module_free(mod, mod->module_init);
2765         module_free(mod, mod->module_core);
2766 }
2767
2768 int __weak module_finalize(const Elf_Ehdr *hdr,
2769                            const Elf_Shdr *sechdrs,
2770                            struct module *me)
2771 {
2772         return 0;
2773 }
2774
2775 static int post_relocation(struct module *mod, const struct load_info *info)
2776 {
2777         /* Sort exception table now relocations are done. */
2778         sort_extable(mod->extable, mod->extable + mod->num_exentries);
2779
2780         /* Copy relocated percpu area over. */
2781         percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
2782                        info->sechdrs[info->index.pcpu].sh_size);
2783
2784         /* Setup kallsyms-specific fields. */
2785         add_kallsyms(mod, info);
2786
2787         /* Arch-specific module finalizing. */
2788         return module_finalize(info->hdr, info->sechdrs, mod);
2789 }
2790
2791 /* Allocate and load the module: note that size of section 0 is always
2792    zero, and we rely on this for optional sections. */
2793 static struct module *load_module(void __user *umod,
2794                                   unsigned long len,
2795                                   const char __user *uargs)
2796 {
2797         struct load_info info = { NULL, };
2798         struct module *mod;
2799         long err;
2800
2801         DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
2802                umod, len, uargs);
2803
2804         /* Copy in the blobs from userspace, check they are vaguely sane. */
2805         err = copy_and_check(&info, umod, len, uargs);
2806         if (err)
2807                 return ERR_PTR(err);
2808
2809         /* Figure out module layout, and allocate all the memory. */
2810         mod = layout_and_allocate(&info);
2811         if (IS_ERR(mod)) {
2812                 err = PTR_ERR(mod);
2813                 goto free_copy;
2814         }
2815
2816         /* Now module is in final location, initialize linked lists, etc. */
2817         err = module_unload_init(mod);
2818         if (err)
2819                 goto free_module;
2820
2821         /* Now we've got everything in the final locations, we can
2822          * find optional sections. */
2823         find_module_sections(mod, &info);
2824
2825         err = check_module_license_and_versions(mod);
2826         if (err)
2827                 goto free_unload;
2828
2829         /* Set up MODINFO_ATTR fields */
2830         setup_modinfo(mod, &info);
2831
2832         /* Fix up syms, so that st_value is a pointer to location. */
2833         err = simplify_symbols(mod, &info);
2834         if (err < 0)
2835                 goto free_modinfo;
2836
2837         err = apply_relocations(mod, &info);
2838         if (err < 0)
2839                 goto free_modinfo;
2840
2841         err = post_relocation(mod, &info);
2842         if (err < 0)
2843                 goto free_modinfo;
2844
2845         flush_module_icache(mod);
2846
2847         /* Now copy in args */
2848         mod->args = strndup_user(uargs, ~0UL >> 1);
2849         if (IS_ERR(mod->args)) {
2850                 err = PTR_ERR(mod->args);
2851                 goto free_arch_cleanup;
2852         }
2853
2854         /* Mark state as coming so strong_try_module_get() ignores us. */
2855         mod->state = MODULE_STATE_COMING;
2856
2857         /* Now sew it into the lists so we can get lockdep and oops
2858          * info during argument parsing.  No one should access us, since
2859          * strong_try_module_get() will fail.
2860          * lockdep/oops can run asynchronous, so use the RCU list insertion
2861          * function to insert in a way safe to concurrent readers.
2862          * The mutex protects against concurrent writers.
2863          */
2864         mutex_lock(&module_mutex);
2865         if (find_module(mod->name)) {
2866                 err = -EEXIST;
2867                 goto unlock;
2868         }
2869
2870         /* This has to be done once we're sure module name is unique. */
2871         dynamic_debug_setup(info.debug, info.num_debug);
2872
2873         /* Find duplicate symbols */
2874         err = verify_export_symbols(mod);
2875         if (err < 0)
2876                 goto ddebug;
2877
2878         module_bug_finalize(info.hdr, info.sechdrs, mod);
2879         list_add_rcu(&mod->list, &modules);
2880         mutex_unlock(&module_mutex);
2881
2882         /* Module is ready to execute: parsing args may do that. */
2883         err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
2884         if (err < 0)
2885                 goto unlink;
2886
2887         /* Link in to syfs. */
2888         err = mod_sysfs_setup(mod, &info, mod->kp, mod->num_kp);
2889         if (err < 0)
2890                 goto unlink;
2891
2892         /* Get rid of temporary copy. */
2893         free_copy(&info);
2894
2895         /* Done! */
2896         trace_module_load(mod);
2897         return mod;
2898
2899  unlink:
2900         mutex_lock(&module_mutex);
2901         /* Unlink carefully: kallsyms could be walking list. */
2902         list_del_rcu(&mod->list);
2903         module_bug_cleanup(mod);
2904
2905  ddebug:
2906         dynamic_debug_remove(info.debug);
2907  unlock:
2908         mutex_unlock(&module_mutex);
2909         synchronize_sched();
2910         kfree(mod->args);
2911  free_arch_cleanup:
2912         module_arch_cleanup(mod);
2913  free_modinfo:
2914         free_modinfo(mod);
2915  free_unload:
2916         module_unload_free(mod);
2917  free_module:
2918         module_deallocate(mod, &info);
2919  free_copy:
2920         free_copy(&info);
2921         return ERR_PTR(err);
2922 }
2923
2924 /* Call module constructors. */
2925 static void do_mod_ctors(struct module *mod)
2926 {
2927 #ifdef CONFIG_CONSTRUCTORS
2928         unsigned long i;
2929
2930         for (i = 0; i < mod->num_ctors; i++)
2931                 mod->ctors[i]();
2932 #endif
2933 }
2934
2935 /* This is where the real work happens */
2936 SYSCALL_DEFINE3(init_module, void __user *, umod,
2937                 unsigned long, len, const char __user *, uargs)
2938 {
2939         struct module *mod;
2940         int ret = 0;
2941
2942         /* Must have permission */
2943         if (!capable(CAP_SYS_MODULE) || modules_disabled)
2944                 return -EPERM;
2945
2946         /* Do all the hard work */
2947         mod = load_module(umod, len, uargs);
2948         if (IS_ERR(mod))
2949                 return PTR_ERR(mod);
2950
2951         blocking_notifier_call_chain(&module_notify_list,
2952                         MODULE_STATE_COMING, mod);
2953
2954         /* Set RO and NX regions for core */
2955         set_section_ro_nx(mod->module_core,
2956                                 mod->core_text_size,
2957                                 mod->core_ro_size,
2958                                 mod->core_size);
2959
2960         /* Set RO and NX regions for init */
2961         set_section_ro_nx(mod->module_init,
2962                                 mod->init_text_size,
2963                                 mod->init_ro_size,
2964                                 mod->init_size);
2965
2966         do_mod_ctors(mod);
2967         /* Start the module */
2968         if (mod->init != NULL)
2969                 ret = do_one_initcall(mod->init);
2970         if (ret < 0) {
2971                 /* Init routine failed: abort.  Try to protect us from
2972                    buggy refcounters. */
2973                 mod->state = MODULE_STATE_GOING;
2974                 synchronize_sched();
2975                 module_put(mod);
2976                 blocking_notifier_call_chain(&module_notify_list,
2977                                              MODULE_STATE_GOING, mod);
2978                 free_module(mod);
2979                 wake_up(&module_wq);
2980                 return ret;
2981         }
2982         if (ret > 0) {
2983                 printk(KERN_WARNING
2984 "%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
2985 "%s: loading module anyway...\n",
2986                        __func__, mod->name, ret,
2987                        __func__);
2988                 dump_stack();
2989         }
2990
2991         /* Now it's a first class citizen!  Wake up anyone waiting for it. */
2992         mod->state = MODULE_STATE_LIVE;
2993         wake_up(&module_wq);
2994         blocking_notifier_call_chain(&module_notify_list,
2995                                      MODULE_STATE_LIVE, mod);
2996
2997         /* We need to finish all async code before the module init sequence is done */
2998         async_synchronize_full();
2999
3000         mutex_lock(&module_mutex);
3001         /* Drop initial reference. */
3002         module_put(mod);
3003         trim_init_extable(mod);
3004 #ifdef CONFIG_KALLSYMS
3005         mod->num_symtab = mod->core_num_syms;
3006         mod->symtab = mod->core_symtab;
3007         mod->strtab = mod->core_strtab;
3008 #endif
3009         unset_module_init_ro_nx(mod);
3010         module_free(mod, mod->module_init);
3011         mod->module_init = NULL;
3012         mod->init_size = 0;
3013         mod->init_ro_size = 0;
3014         mod->init_text_size = 0;
3015         mutex_unlock(&module_mutex);
3016
3017         return 0;
3018 }
3019
3020 static inline int within(unsigned long addr, void *start, unsigned long size)
3021 {
3022         return ((void *)addr >= start && (void *)addr < start + size);
3023 }
3024
3025 #ifdef CONFIG_KALLSYMS
3026 /*
3027  * This ignores the intensely annoying "mapping symbols" found
3028  * in ARM ELF files: $a, $t and $d.
3029  */
3030 static inline int is_arm_mapping_symbol(const char *str)
3031 {
3032         return str[0] == '$' && strchr("atd", str[1])
3033                && (str[2] == '\0' || str[2] == '.');
3034 }
3035
3036 static const char *get_ksymbol(struct module *mod,
3037                                unsigned long addr,
3038                                unsigned long *size,
3039                                unsigned long *offset)
3040 {
3041         unsigned int i, best = 0;
3042         unsigned long nextval;
3043
3044         /* At worse, next value is at end of module */
3045         if (within_module_init(addr, mod))
3046                 nextval = (unsigned long)mod->module_init+mod->init_text_size;
3047         else
3048                 nextval = (unsigned long)mod->module_core+mod->core_text_size;
3049
3050         /* Scan for closest preceding symbol, and next symbol. (ELF
3051            starts real symbols at 1). */
3052         for (i = 1; i < mod->num_symtab; i++) {
3053                 if (mod->symtab[i].st_shndx == SHN_UNDEF)
3054                         continue;
3055
3056                 /* We ignore unnamed symbols: they're uninformative
3057                  * and inserted at a whim. */
3058                 if (mod->symtab[i].st_value <= addr
3059                     && mod->symtab[i].st_value > mod->symtab[best].st_value
3060                     && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3061                     && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3062                         best = i;
3063                 if (mod->symtab[i].st_value > addr
3064                     && mod->symtab[i].st_value < nextval
3065                     && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3066                     && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3067                         nextval = mod->symtab[i].st_value;
3068         }
3069
3070         if (!best)
3071                 return NULL;
3072
3073         if (size)
3074                 *size = nextval - mod->symtab[best].st_value;
3075         if (offset)
3076                 *offset = addr - mod->symtab[best].st_value;
3077         return mod->strtab + mod->symtab[best].st_name;
3078 }
3079
3080 /* For kallsyms to ask for address resolution.  NULL means not found.  Careful
3081  * not to lock to avoid deadlock on oopses, simply disable preemption. */
3082 const char *module_address_lookup(unsigned long addr,
3083                             unsigned long *size,
3084                             unsigned long *offset,
3085                             char **modname,
3086                             char *namebuf)
3087 {
3088         struct module *mod;
3089         const char *ret = NULL;
3090
3091         preempt_disable();
3092         list_for_each_entry_rcu(mod, &modules, list) {
3093                 if (within_module_init(addr, mod) ||
3094                     within_module_core(addr, mod)) {
3095                         if (modname)
3096                                 *modname = mod->name;
3097                         ret = get_ksymbol(mod, addr, size, offset);
3098                         break;
3099                 }
3100         }
3101         /* Make a copy in here where it's safe */
3102         if (ret) {
3103                 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
3104                 ret = namebuf;
3105         }
3106         preempt_enable();
3107         return ret;
3108 }
3109
3110 int lookup_module_symbol_name(unsigned long addr, char *symname)
3111 {
3112         struct module *mod;
3113
3114         preempt_disable();
3115         list_for_each_entry_rcu(mod, &modules, list) {
3116                 if (within_module_init(addr, mod) ||
3117                     within_module_core(addr, mod)) {
3118                         const char *sym;
3119
3120                         sym = get_ksymbol(mod, addr, NULL, NULL);
3121                         if (!sym)
3122                                 goto out;
3123                         strlcpy(symname, sym, KSYM_NAME_LEN);
3124                         preempt_enable();
3125                         return 0;
3126                 }
3127         }
3128 out:
3129         preempt_enable();
3130         return -ERANGE;
3131 }
3132
3133 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
3134                         unsigned long *offset, char *modname, char *name)
3135 {
3136         struct module *mod;
3137
3138         preempt_disable();
3139         list_for_each_entry_rcu(mod, &modules, list) {
3140                 if (within_module_init(addr, mod) ||
3141                     within_module_core(addr, mod)) {
3142                         const char *sym;
3143
3144                         sym = get_ksymbol(mod, addr, size, offset);
3145                         if (!sym)
3146                                 goto out;
3147                         if (modname)
3148                                 strlcpy(modname, mod->name, MODULE_NAME_LEN);
3149                         if (name)
3150                                 strlcpy(name, sym, KSYM_NAME_LEN);
3151                         preempt_enable();
3152                         return 0;
3153                 }
3154         }
3155 out:
3156         preempt_enable();
3157         return -ERANGE;
3158 }
3159
3160 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
3161                         char *name, char *module_name, int *exported)
3162 {
3163         struct module *mod;
3164
3165         preempt_disable();
3166         list_for_each_entry_rcu(mod, &modules, list) {
3167                 if (symnum < mod->num_symtab) {
3168                         *value = mod->symtab[symnum].st_value;
3169                         *type = mod->symtab[symnum].st_info;
3170                         strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
3171                                 KSYM_NAME_LEN);
3172                         strlcpy(module_name, mod->name, MODULE_NAME_LEN);
3173                         *exported = is_exported(name, *value, mod);
3174                         preempt_enable();
3175                         return 0;
3176                 }
3177                 symnum -= mod->num_symtab;
3178         }
3179         preempt_enable();
3180         return -ERANGE;
3181 }
3182
3183 static unsigned long mod_find_symname(struct module *mod, const char *name)
3184 {
3185         unsigned int i;
3186
3187         for (i = 0; i < mod->num_symtab; i++)
3188                 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
3189                     mod->symtab[i].st_info != 'U')
3190                         return mod->symtab[i].st_value;
3191         return 0;
3192 }
3193
3194 /* Look for this name: can be of form module:name. */
3195 unsigned long module_kallsyms_lookup_name(const char *name)
3196 {
3197         struct module *mod;
3198         char *colon;
3199         unsigned long ret = 0;
3200
3201         /* Don't lock: we're in enough trouble already. */
3202         preempt_disable();
3203         if ((colon = strchr(name, ':')) != NULL) {
3204                 *colon = '\0';
3205                 if ((mod = find_module(name)) != NULL)
3206                         ret = mod_find_symname(mod, colon+1);
3207                 *colon = ':';
3208         } else {
3209                 list_for_each_entry_rcu(mod, &modules, list)
3210                         if ((ret = mod_find_symname(mod, name)) != 0)
3211                                 break;
3212         }
3213         preempt_enable();
3214         return ret;
3215 }
3216
3217 int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
3218                                              struct module *, unsigned long),
3219                                    void *data)
3220 {
3221         struct module *mod;
3222         unsigned int i;
3223         int ret;
3224
3225         list_for_each_entry(mod, &modules, list) {
3226                 for (i = 0; i < mod->num_symtab; i++) {
3227                         ret = fn(data, mod->strtab + mod->symtab[i].st_name,
3228                                  mod, mod->symtab[i].st_value);
3229                         if (ret != 0)
3230                                 return ret;
3231                 }
3232         }
3233         return 0;
3234 }
3235 #endif /* CONFIG_KALLSYMS */
3236
3237 static char *module_flags(struct module *mod, char *buf)
3238 {
3239         int bx = 0;
3240
3241         if (mod->taints ||
3242             mod->state == MODULE_STATE_GOING ||
3243             mod->state == MODULE_STATE_COMING) {
3244                 buf[bx++] = '(';
3245                 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
3246                         buf[bx++] = 'P';
3247                 else if (mod->taints & (1 << TAINT_OOT_MODULE))
3248                         buf[bx++] = 'O';
3249                 if (mod->taints & (1 << TAINT_FORCED_MODULE))
3250                         buf[bx++] = 'F';
3251                 if (mod->taints & (1 << TAINT_CRAP))
3252                         buf[bx++] = 'C';
3253                 /*
3254                  * TAINT_FORCED_RMMOD: could be added.
3255                  * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
3256                  * apply to modules.
3257                  */
3258
3259                 /* Show a - for module-is-being-unloaded */
3260                 if (mod->state == MODULE_STATE_GOING)
3261                         buf[bx++] = '-';
3262                 /* Show a + for module-is-being-loaded */
3263                 if (mod->state == MODULE_STATE_COMING)
3264                         buf[bx++] = '+';
3265                 buf[bx++] = ')';
3266         }
3267         buf[bx] = '\0';
3268
3269         return buf;
3270 }
3271
3272 #ifdef CONFIG_PROC_FS
3273 /* Called by the /proc file system to return a list of modules. */
3274 static void *m_start(struct seq_file *m, loff_t *pos)
3275 {
3276         mutex_lock(&module_mutex);
3277         return seq_list_start(&modules, *pos);
3278 }
3279
3280 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
3281 {
3282         return seq_list_next(p, &modules, pos);
3283 }
3284
3285 static void m_stop(struct seq_file *m, void *p)
3286 {
3287         mutex_unlock(&module_mutex);
3288 }
3289
3290 static int m_show(struct seq_file *m, void *p)
3291 {
3292         struct module *mod = list_entry(p, struct module, list);
3293         char buf[8];
3294
3295         seq_printf(m, "%s %u",
3296                    mod->name, mod->init_size + mod->core_size);
3297         print_unload_info(m, mod);
3298
3299         /* Informative for users. */
3300         seq_printf(m, " %s",
3301                    mod->state == MODULE_STATE_GOING ? "Unloading":
3302                    mod->state == MODULE_STATE_COMING ? "Loading":
3303                    "Live");
3304         /* Used by oprofile and other similar tools. */
3305         seq_printf(m, " 0x%pK", mod->module_core);
3306
3307         /* Taints info */
3308         if (mod->taints)
3309                 seq_printf(m, " %s", module_flags(mod, buf));
3310
3311         seq_printf(m, "\n");
3312         return 0;
3313 }
3314
3315 /* Format: modulename size refcount deps address
3316
3317    Where refcount is a number or -, and deps is a comma-separated list
3318    of depends or -.
3319 */
3320 static const struct seq_operations modules_op = {
3321         .start  = m_start,
3322         .next   = m_next,
3323         .stop   = m_stop,
3324         .show   = m_show
3325 };
3326
3327 static int modules_open(struct inode *inode, struct file *file)
3328 {
3329         return seq_open(file, &modules_op);
3330 }
3331
3332 static const struct file_operations proc_modules_operations = {
3333         .open           = modules_open,
3334         .read           = seq_read,
3335         .llseek         = seq_lseek,
3336         .release        = seq_release,
3337 };
3338
3339 static int __init proc_modules_init(void)
3340 {
3341         proc_create("modules", 0, NULL, &proc_modules_operations);
3342         return 0;
3343 }
3344 module_init(proc_modules_init);
3345 #endif
3346
3347 /* Given an address, look for it in the module exception tables. */
3348 const struct exception_table_entry *search_module_extables(unsigned long addr)
3349 {
3350         const struct exception_table_entry *e = NULL;
3351         struct module *mod;
3352
3353         preempt_disable();
3354         list_for_each_entry_rcu(mod, &modules, list) {
3355                 if (mod->num_exentries == 0)
3356                         continue;
3357
3358                 e = search_extable(mod->extable,
3359                                    mod->extable + mod->num_exentries - 1,
3360                                    addr);
3361                 if (e)
3362                         break;
3363         }
3364         preempt_enable();
3365
3366         /* Now, if we found one, we are running inside it now, hence
3367            we cannot unload the module, hence no refcnt needed. */
3368         return e;
3369 }
3370
3371 /*
3372  * is_module_address - is this address inside a module?
3373  * @addr: the address to check.
3374  *
3375  * See is_module_text_address() if you simply want to see if the address
3376  * is code (not data).
3377  */
3378 bool is_module_address(unsigned long addr)
3379 {
3380         bool ret;
3381
3382         preempt_disable();
3383         ret = __module_address(addr) != NULL;
3384         preempt_enable();
3385
3386         return ret;
3387 }
3388
3389 /*
3390  * __module_address - get the module which contains an address.
3391  * @addr: the address.
3392  *
3393  * Must be called with preempt disabled or module mutex held so that
3394  * module doesn't get freed during this.
3395  */
3396 struct module *__module_address(unsigned long addr)
3397 {
3398         struct module *mod;
3399
3400         if (addr < module_addr_min || addr > module_addr_max)
3401                 return NULL;
3402
3403         list_for_each_entry_rcu(mod, &modules, list)
3404                 if (within_module_core(addr, mod)
3405                     || within_module_init(addr, mod))
3406                         return mod;
3407         return NULL;
3408 }
3409 EXPORT_SYMBOL_GPL(__module_address);
3410
3411 /*
3412  * is_module_text_address - is this address inside module code?
3413  * @addr: the address to check.
3414  *
3415  * See is_module_address() if you simply want to see if the address is
3416  * anywhere in a module.  See kernel_text_address() for testing if an
3417  * address corresponds to kernel or module code.
3418  */
3419 bool is_module_text_address(unsigned long addr)
3420 {
3421         bool ret;
3422
3423         preempt_disable();
3424         ret = __module_text_address(addr) != NULL;
3425         preempt_enable();
3426
3427         return ret;
3428 }
3429
3430 /*
3431  * __module_text_address - get the module whose code contains an address.
3432  * @addr: the address.
3433  *
3434  * Must be called with preempt disabled or module mutex held so that
3435  * module doesn't get freed during this.
3436  */
3437 struct module *__module_text_address(unsigned long addr)
3438 {
3439         struct module *mod = __module_address(addr);
3440         if (mod) {
3441                 /* Make sure it's within the text section. */
3442                 if (!within(addr, mod->module_init, mod->init_text_size)
3443                     && !within(addr, mod->module_core, mod->core_text_size))
3444                         mod = NULL;
3445         }
3446         return mod;
3447 }
3448 EXPORT_SYMBOL_GPL(__module_text_address);
3449
3450 /* Don't grab lock, we're oopsing. */
3451 void print_modules(void)
3452 {
3453         struct module *mod;
3454         char buf[8];
3455
3456         printk(KERN_DEFAULT "Modules linked in:");
3457         /* Most callers should already have preempt disabled, but make sure */
3458         preempt_disable();
3459         list_for_each_entry_rcu(mod, &modules, list)
3460                 printk(" %s%s", mod->name, module_flags(mod, buf));
3461         preempt_enable();
3462         if (last_unloaded_module[0])
3463                 printk(" [last unloaded: %s]", last_unloaded_module);
3464         printk("\n");
3465 }
3466
3467 #ifdef CONFIG_MODVERSIONS
3468 /* Generate the signature for all relevant module structures here.
3469  * If these change, we don't want to try to parse the module. */
3470 void module_layout(struct module *mod,
3471                    struct modversion_info *ver,
3472                    struct kernel_param *kp,
3473                    struct kernel_symbol *ks,
3474                    struct tracepoint * const *tp)
3475 {
3476 }
3477 EXPORT_SYMBOL(module_layout);
3478 #endif