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