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