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