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