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