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