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