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