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