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