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