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