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