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