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