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