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