[IB] uverbs: have kernel return QP capabilities
[linux-block.git] / kernel / module.c
CommitLineData
1da177e4
LT
1/* Rewritten by Rusty Russell, on the backs of many others...
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*/
19#include <linux/config.h>
20#include <linux/module.h>
21#include <linux/moduleloader.h>
22#include <linux/init.h>
9f158333 23#include <linux/kernel.h>
1da177e4
LT
24#include <linux/slab.h>
25#include <linux/vmalloc.h>
26#include <linux/elf.h>
27#include <linux/seq_file.h>
28#include <linux/syscalls.h>
29#include <linux/fcntl.h>
30#include <linux/rcupdate.h>
31#include <linux/cpu.h>
32#include <linux/moduleparam.h>
33#include <linux/errno.h>
34#include <linux/err.h>
35#include <linux/vermagic.h>
36#include <linux/notifier.h>
37#include <linux/stop_machine.h>
38#include <linux/device.h>
c988d2b2 39#include <linux/string.h>
1da177e4
LT
40#include <asm/uaccess.h>
41#include <asm/semaphore.h>
42#include <asm/cacheflush.h>
43
44#if 0
45#define DEBUGP printk
46#else
47#define DEBUGP(fmt , a...)
48#endif
49
50#ifndef ARCH_SHF_SMALL
51#define ARCH_SHF_SMALL 0
52#endif
53
54/* If this is set, the section belongs in the init part of the module */
55#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
56
57/* Protects module list */
58static DEFINE_SPINLOCK(modlist_lock);
59
60/* List of modules, protected by module_mutex AND modlist_lock */
61static DECLARE_MUTEX(module_mutex);
62static LIST_HEAD(modules);
63
64static DECLARE_MUTEX(notify_mutex);
65static struct notifier_block * module_notify_list;
66
67int register_module_notifier(struct notifier_block * nb)
68{
69 int err;
70 down(&notify_mutex);
71 err = notifier_chain_register(&module_notify_list, nb);
72 up(&notify_mutex);
73 return err;
74}
75EXPORT_SYMBOL(register_module_notifier);
76
77int unregister_module_notifier(struct notifier_block * nb)
78{
79 int err;
80 down(&notify_mutex);
81 err = notifier_chain_unregister(&module_notify_list, nb);
82 up(&notify_mutex);
83 return err;
84}
85EXPORT_SYMBOL(unregister_module_notifier);
86
87/* We require a truly strong try_module_get() */
88static inline int strong_try_module_get(struct module *mod)
89{
90 if (mod && mod->state == MODULE_STATE_COMING)
91 return 0;
92 return try_module_get(mod);
93}
94
95/* A thread that wants to hold a reference to a module only while it
96 * is running can call ths to safely exit.
97 * nfsd and lockd use this.
98 */
99void __module_put_and_exit(struct module *mod, long code)
100{
101 module_put(mod);
102 do_exit(code);
103}
104EXPORT_SYMBOL(__module_put_and_exit);
105
106/* Find a module section: 0 means not found. */
107static unsigned int find_sec(Elf_Ehdr *hdr,
108 Elf_Shdr *sechdrs,
109 const char *secstrings,
110 const char *name)
111{
112 unsigned int i;
113
114 for (i = 1; i < hdr->e_shnum; i++)
115 /* Alloc bit cleared means "ignore it." */
116 if ((sechdrs[i].sh_flags & SHF_ALLOC)
117 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
118 return i;
119 return 0;
120}
121
122/* Provided by the linker */
123extern const struct kernel_symbol __start___ksymtab[];
124extern const struct kernel_symbol __stop___ksymtab[];
125extern const struct kernel_symbol __start___ksymtab_gpl[];
126extern const struct kernel_symbol __stop___ksymtab_gpl[];
127extern const unsigned long __start___kcrctab[];
128extern const unsigned long __start___kcrctab_gpl[];
129
130#ifndef CONFIG_MODVERSIONS
131#define symversion(base, idx) NULL
132#else
133#define symversion(base, idx) ((base) ? ((base) + (idx)) : NULL)
134#endif
135
136/* Find a symbol, return value, crc and module which owns it */
137static unsigned long __find_symbol(const char *name,
138 struct module **owner,
139 const unsigned long **crc,
140 int gplok)
141{
142 struct module *mod;
143 unsigned int i;
144
145 /* Core kernel first. */
146 *owner = NULL;
147 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++) {
148 if (strcmp(__start___ksymtab[i].name, name) == 0) {
149 *crc = symversion(__start___kcrctab, i);
150 return __start___ksymtab[i].value;
151 }
152 }
153 if (gplok) {
154 for (i = 0; __start___ksymtab_gpl+i<__stop___ksymtab_gpl; i++)
155 if (strcmp(__start___ksymtab_gpl[i].name, name) == 0) {
156 *crc = symversion(__start___kcrctab_gpl, i);
157 return __start___ksymtab_gpl[i].value;
158 }
159 }
160
161 /* Now try modules. */
162 list_for_each_entry(mod, &modules, list) {
163 *owner = mod;
164 for (i = 0; i < mod->num_syms; i++)
165 if (strcmp(mod->syms[i].name, name) == 0) {
166 *crc = symversion(mod->crcs, i);
167 return mod->syms[i].value;
168 }
169
170 if (gplok) {
171 for (i = 0; i < mod->num_gpl_syms; i++) {
172 if (strcmp(mod->gpl_syms[i].name, name) == 0) {
173 *crc = symversion(mod->gpl_crcs, i);
174 return mod->gpl_syms[i].value;
175 }
176 }
177 }
178 }
179 DEBUGP("Failed to find symbol %s\n", name);
180 return 0;
181}
182
183/* Find a symbol in this elf symbol table */
184static unsigned long find_local_symbol(Elf_Shdr *sechdrs,
185 unsigned int symindex,
186 const char *strtab,
187 const char *name)
188{
189 unsigned int i;
190 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
191
192 /* Search (defined) internal symbols first. */
193 for (i = 1; i < sechdrs[symindex].sh_size/sizeof(*sym); i++) {
194 if (sym[i].st_shndx != SHN_UNDEF
195 && strcmp(name, strtab + sym[i].st_name) == 0)
196 return sym[i].st_value;
197 }
198 return 0;
199}
200
201/* Search for module by name: must hold module_mutex. */
202static struct module *find_module(const char *name)
203{
204 struct module *mod;
205
206 list_for_each_entry(mod, &modules, list) {
207 if (strcmp(mod->name, name) == 0)
208 return mod;
209 }
210 return NULL;
211}
212
213#ifdef CONFIG_SMP
214/* Number of blocks used and allocated. */
215static unsigned int pcpu_num_used, pcpu_num_allocated;
216/* Size of each block. -ve means used. */
217static int *pcpu_size;
218
219static int split_block(unsigned int i, unsigned short size)
220{
221 /* Reallocation required? */
222 if (pcpu_num_used + 1 > pcpu_num_allocated) {
223 int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated*2,
224 GFP_KERNEL);
225 if (!new)
226 return 0;
227
228 memcpy(new, pcpu_size, sizeof(new[0])*pcpu_num_allocated);
229 pcpu_num_allocated *= 2;
230 kfree(pcpu_size);
231 pcpu_size = new;
232 }
233
234 /* Insert a new subblock */
235 memmove(&pcpu_size[i+1], &pcpu_size[i],
236 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
237 pcpu_num_used++;
238
239 pcpu_size[i+1] -= size;
240 pcpu_size[i] = size;
241 return 1;
242}
243
244static inline unsigned int block_size(int val)
245{
246 if (val < 0)
247 return -val;
248 return val;
249}
250
251/* Created by linker magic */
252extern char __per_cpu_start[], __per_cpu_end[];
253
842bbaaa
RR
254static void *percpu_modalloc(unsigned long size, unsigned long align,
255 const char *name)
1da177e4
LT
256{
257 unsigned long extra;
258 unsigned int i;
259 void *ptr;
260
842bbaaa
RR
261 if (align > SMP_CACHE_BYTES) {
262 printk(KERN_WARNING "%s: per-cpu alignment %li > %i\n",
263 name, align, SMP_CACHE_BYTES);
264 align = SMP_CACHE_BYTES;
265 }
1da177e4
LT
266
267 ptr = __per_cpu_start;
268 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
269 /* Extra for alignment requirement. */
270 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
271 BUG_ON(i == 0 && extra != 0);
272
273 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
274 continue;
275
276 /* Transfer extra to previous block. */
277 if (pcpu_size[i-1] < 0)
278 pcpu_size[i-1] -= extra;
279 else
280 pcpu_size[i-1] += extra;
281 pcpu_size[i] -= extra;
282 ptr += extra;
283
284 /* Split block if warranted */
285 if (pcpu_size[i] - size > sizeof(unsigned long))
286 if (!split_block(i, size))
287 return NULL;
288
289 /* Mark allocated */
290 pcpu_size[i] = -pcpu_size[i];
291 return ptr;
292 }
293
294 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
295 size);
296 return NULL;
297}
298
299static void percpu_modfree(void *freeme)
300{
301 unsigned int i;
302 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
303
304 /* First entry is core kernel percpu data. */
305 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
306 if (ptr == freeme) {
307 pcpu_size[i] = -pcpu_size[i];
308 goto free;
309 }
310 }
311 BUG();
312
313 free:
314 /* Merge with previous? */
315 if (pcpu_size[i-1] >= 0) {
316 pcpu_size[i-1] += pcpu_size[i];
317 pcpu_num_used--;
318 memmove(&pcpu_size[i], &pcpu_size[i+1],
319 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
320 i--;
321 }
322 /* Merge with next? */
323 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
324 pcpu_size[i] += pcpu_size[i+1];
325 pcpu_num_used--;
326 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
327 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
328 }
329}
330
331static unsigned int find_pcpusec(Elf_Ehdr *hdr,
332 Elf_Shdr *sechdrs,
333 const char *secstrings)
334{
335 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
336}
337
338static int percpu_modinit(void)
339{
340 pcpu_num_used = 2;
341 pcpu_num_allocated = 2;
342 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
343 GFP_KERNEL);
344 /* Static in-kernel percpu data (used). */
345 pcpu_size[0] = -ALIGN(__per_cpu_end-__per_cpu_start, SMP_CACHE_BYTES);
346 /* Free room. */
347 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
348 if (pcpu_size[1] < 0) {
349 printk(KERN_ERR "No per-cpu room for modules.\n");
350 pcpu_num_used = 1;
351 }
352
353 return 0;
354}
355__initcall(percpu_modinit);
356#else /* ... !CONFIG_SMP */
842bbaaa
RR
357static inline void *percpu_modalloc(unsigned long size, unsigned long align,
358 const char *name)
1da177e4
LT
359{
360 return NULL;
361}
362static inline void percpu_modfree(void *pcpuptr)
363{
364 BUG();
365}
366static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
367 Elf_Shdr *sechdrs,
368 const char *secstrings)
369{
370 return 0;
371}
372static inline void percpu_modcopy(void *pcpudst, const void *src,
373 unsigned long size)
374{
375 /* pcpusec should be 0, and size of that section should be 0. */
376 BUG_ON(size != 0);
377}
378#endif /* CONFIG_SMP */
379
380#ifdef CONFIG_MODULE_UNLOAD
c988d2b2
MD
381#define MODINFO_ATTR(field) \
382static void setup_modinfo_##field(struct module *mod, const char *s) \
383{ \
384 mod->field = kstrdup(s, GFP_KERNEL); \
385} \
386static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
387 struct module *mod, char *buffer) \
388{ \
389 return sprintf(buffer, "%s\n", mod->field); \
390} \
391static int modinfo_##field##_exists(struct module *mod) \
392{ \
393 return mod->field != NULL; \
394} \
395static void free_modinfo_##field(struct module *mod) \
396{ \
397 kfree(mod->field); \
398 mod->field = NULL; \
399} \
400static struct module_attribute modinfo_##field = { \
401 .attr = { .name = __stringify(field), .mode = 0444, \
402 .owner = THIS_MODULE }, \
403 .show = show_modinfo_##field, \
404 .setup = setup_modinfo_##field, \
405 .test = modinfo_##field##_exists, \
406 .free = free_modinfo_##field, \
407};
408
409MODINFO_ATTR(version);
410MODINFO_ATTR(srcversion);
411
412static struct module_attribute *modinfo_attrs[] = {
413 &modinfo_version,
414 &modinfo_srcversion,
415 NULL,
416};
417
1da177e4
LT
418/* Init the unload section of the module. */
419static void module_unload_init(struct module *mod)
420{
421 unsigned int i;
422
423 INIT_LIST_HEAD(&mod->modules_which_use_me);
424 for (i = 0; i < NR_CPUS; i++)
425 local_set(&mod->ref[i].count, 0);
426 /* Hold reference count during initialization. */
39c715b7 427 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
1da177e4
LT
428 /* Backwards compatibility macros put refcount during init. */
429 mod->waiter = current;
430}
431
432/* modules using other modules */
433struct module_use
434{
435 struct list_head list;
436 struct module *module_which_uses;
437};
438
439/* Does a already use b? */
440static int already_uses(struct module *a, struct module *b)
441{
442 struct module_use *use;
443
444 list_for_each_entry(use, &b->modules_which_use_me, list) {
445 if (use->module_which_uses == a) {
446 DEBUGP("%s uses %s!\n", a->name, b->name);
447 return 1;
448 }
449 }
450 DEBUGP("%s does not use %s!\n", a->name, b->name);
451 return 0;
452}
453
454/* Module a uses b */
455static int use_module(struct module *a, struct module *b)
456{
457 struct module_use *use;
458 if (b == NULL || already_uses(a, b)) return 1;
459
460 if (!strong_try_module_get(b))
461 return 0;
462
463 DEBUGP("Allocating new usage for %s.\n", a->name);
464 use = kmalloc(sizeof(*use), GFP_ATOMIC);
465 if (!use) {
466 printk("%s: out of memory loading\n", a->name);
467 module_put(b);
468 return 0;
469 }
470
471 use->module_which_uses = a;
472 list_add(&use->list, &b->modules_which_use_me);
473 return 1;
474}
475
476/* Clear the unload stuff of the module. */
477static void module_unload_free(struct module *mod)
478{
479 struct module *i;
480
481 list_for_each_entry(i, &modules, list) {
482 struct module_use *use;
483
484 list_for_each_entry(use, &i->modules_which_use_me, list) {
485 if (use->module_which_uses == mod) {
486 DEBUGP("%s unusing %s\n", mod->name, i->name);
487 module_put(i);
488 list_del(&use->list);
489 kfree(use);
490 /* There can be at most one match. */
491 break;
492 }
493 }
494 }
495}
496
497#ifdef CONFIG_MODULE_FORCE_UNLOAD
498static inline int try_force(unsigned int flags)
499{
500 int ret = (flags & O_TRUNC);
501 if (ret)
9f158333 502 add_taint(TAINT_FORCED_MODULE);
1da177e4
LT
503 return ret;
504}
505#else
506static inline int try_force(unsigned int flags)
507{
508 return 0;
509}
510#endif /* CONFIG_MODULE_FORCE_UNLOAD */
511
512struct stopref
513{
514 struct module *mod;
515 int flags;
516 int *forced;
517};
518
519/* Whole machine is stopped with interrupts off when this runs. */
520static int __try_stop_module(void *_sref)
521{
522 struct stopref *sref = _sref;
523
524 /* If it's not unused, quit unless we are told to block. */
525 if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
526 if (!(*sref->forced = try_force(sref->flags)))
527 return -EWOULDBLOCK;
528 }
529
530 /* Mark it as dying. */
531 sref->mod->state = MODULE_STATE_GOING;
532 return 0;
533}
534
535static int try_stop_module(struct module *mod, int flags, int *forced)
536{
537 struct stopref sref = { mod, flags, forced };
538
539 return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
540}
541
542unsigned int module_refcount(struct module *mod)
543{
544 unsigned int i, total = 0;
545
546 for (i = 0; i < NR_CPUS; i++)
547 total += local_read(&mod->ref[i].count);
548 return total;
549}
550EXPORT_SYMBOL(module_refcount);
551
552/* This exists whether we can unload or not */
553static void free_module(struct module *mod);
554
555static void wait_for_zero_refcount(struct module *mod)
556{
557 /* Since we might sleep for some time, drop the semaphore first */
558 up(&module_mutex);
559 for (;;) {
560 DEBUGP("Looking at refcount...\n");
561 set_current_state(TASK_UNINTERRUPTIBLE);
562 if (module_refcount(mod) == 0)
563 break;
564 schedule();
565 }
566 current->state = TASK_RUNNING;
567 down(&module_mutex);
568}
569
570asmlinkage long
571sys_delete_module(const char __user *name_user, unsigned int flags)
572{
573 struct module *mod;
574 char name[MODULE_NAME_LEN];
575 int ret, forced = 0;
576
577 if (!capable(CAP_SYS_MODULE))
578 return -EPERM;
579
580 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
581 return -EFAULT;
582 name[MODULE_NAME_LEN-1] = '\0';
583
584 if (down_interruptible(&module_mutex) != 0)
585 return -EINTR;
586
587 mod = find_module(name);
588 if (!mod) {
589 ret = -ENOENT;
590 goto out;
591 }
592
593 if (!list_empty(&mod->modules_which_use_me)) {
594 /* Other modules depend on us: get rid of them first. */
595 ret = -EWOULDBLOCK;
596 goto out;
597 }
598
599 /* Doing init or already dying? */
600 if (mod->state != MODULE_STATE_LIVE) {
601 /* FIXME: if (force), slam module count and wake up
602 waiter --RR */
603 DEBUGP("%s already dying\n", mod->name);
604 ret = -EBUSY;
605 goto out;
606 }
607
608 /* If it has an init func, it must have an exit func to unload */
609 if ((mod->init != NULL && mod->exit == NULL)
610 || mod->unsafe) {
611 forced = try_force(flags);
612 if (!forced) {
613 /* This module can't be removed */
614 ret = -EBUSY;
615 goto out;
616 }
617 }
618
619 /* Set this up before setting mod->state */
620 mod->waiter = current;
621
622 /* Stop the machine so refcounts can't move and disable module. */
623 ret = try_stop_module(mod, flags, &forced);
624 if (ret != 0)
625 goto out;
626
627 /* Never wait if forced. */
628 if (!forced && module_refcount(mod) != 0)
629 wait_for_zero_refcount(mod);
630
631 /* Final destruction now noone is using it. */
632 if (mod->exit != NULL) {
633 up(&module_mutex);
634 mod->exit();
635 down(&module_mutex);
636 }
637 free_module(mod);
638
639 out:
640 up(&module_mutex);
641 return ret;
642}
643
644static void print_unload_info(struct seq_file *m, struct module *mod)
645{
646 struct module_use *use;
647 int printed_something = 0;
648
649 seq_printf(m, " %u ", module_refcount(mod));
650
651 /* Always include a trailing , so userspace can differentiate
652 between this and the old multi-field proc format. */
653 list_for_each_entry(use, &mod->modules_which_use_me, list) {
654 printed_something = 1;
655 seq_printf(m, "%s,", use->module_which_uses->name);
656 }
657
658 if (mod->unsafe) {
659 printed_something = 1;
660 seq_printf(m, "[unsafe],");
661 }
662
663 if (mod->init != NULL && mod->exit == NULL) {
664 printed_something = 1;
665 seq_printf(m, "[permanent],");
666 }
667
668 if (!printed_something)
669 seq_printf(m, "-");
670}
671
672void __symbol_put(const char *symbol)
673{
674 struct module *owner;
675 unsigned long flags;
676 const unsigned long *crc;
677
678 spin_lock_irqsave(&modlist_lock, flags);
679 if (!__find_symbol(symbol, &owner, &crc, 1))
680 BUG();
681 module_put(owner);
682 spin_unlock_irqrestore(&modlist_lock, flags);
683}
684EXPORT_SYMBOL(__symbol_put);
685
686void symbol_put_addr(void *addr)
687{
688 unsigned long flags;
689
690 spin_lock_irqsave(&modlist_lock, flags);
691 if (!kernel_text_address((unsigned long)addr))
692 BUG();
693
694 module_put(module_text_address((unsigned long)addr));
695 spin_unlock_irqrestore(&modlist_lock, flags);
696}
697EXPORT_SYMBOL_GPL(symbol_put_addr);
698
699static ssize_t show_refcnt(struct module_attribute *mattr,
700 struct module *mod, char *buffer)
701{
702 /* sysfs holds a reference */
703 return sprintf(buffer, "%u\n", module_refcount(mod)-1);
704}
705
706static struct module_attribute refcnt = {
707 .attr = { .name = "refcnt", .mode = 0444, .owner = THIS_MODULE },
708 .show = show_refcnt,
709};
710
711#else /* !CONFIG_MODULE_UNLOAD */
712static void print_unload_info(struct seq_file *m, struct module *mod)
713{
714 /* We don't know the usage count, or what modules are using. */
715 seq_printf(m, " - -");
716}
717
718static inline void module_unload_free(struct module *mod)
719{
720}
721
722static inline int use_module(struct module *a, struct module *b)
723{
724 return strong_try_module_get(b);
725}
726
727static inline void module_unload_init(struct module *mod)
728{
729}
730#endif /* CONFIG_MODULE_UNLOAD */
731
732#ifdef CONFIG_OBSOLETE_MODPARM
733/* Bounds checking done below */
734static int obsparm_copy_string(const char *val, struct kernel_param *kp)
735{
736 strcpy(kp->arg, val);
737 return 0;
738}
739
52c1da39 740static int set_obsolete(const char *val, struct kernel_param *kp)
1da177e4
LT
741{
742 unsigned int min, max;
743 unsigned int size, maxsize;
744 int dummy;
745 char *endp;
746 const char *p;
747 struct obsolete_modparm *obsparm = kp->arg;
748
749 if (!val) {
750 printk(KERN_ERR "Parameter %s needs an argument\n", kp->name);
751 return -EINVAL;
752 }
753
754 /* type is: [min[-max]]{b,h,i,l,s} */
755 p = obsparm->type;
756 min = simple_strtol(p, &endp, 10);
757 if (endp == obsparm->type)
758 min = max = 1;
759 else if (*endp == '-') {
760 p = endp+1;
761 max = simple_strtol(p, &endp, 10);
762 } else
763 max = min;
764 switch (*endp) {
765 case 'b':
766 return param_array(kp->name, val, min, max, obsparm->addr,
767 1, param_set_byte, &dummy);
768 case 'h':
769 return param_array(kp->name, val, min, max, obsparm->addr,
770 sizeof(short), param_set_short, &dummy);
771 case 'i':
772 return param_array(kp->name, val, min, max, obsparm->addr,
773 sizeof(int), param_set_int, &dummy);
774 case 'l':
775 return param_array(kp->name, val, min, max, obsparm->addr,
776 sizeof(long), param_set_long, &dummy);
777 case 's':
778 return param_array(kp->name, val, min, max, obsparm->addr,
779 sizeof(char *), param_set_charp, &dummy);
780
781 case 'c':
782 /* Undocumented: 1-5c50 means 1-5 strings of up to 49 chars,
783 and the decl is "char xxx[5][50];" */
784 p = endp+1;
785 maxsize = simple_strtol(p, &endp, 10);
786 /* We check lengths here (yes, this is a hack). */
787 p = val;
788 while (p[size = strcspn(p, ",")]) {
789 if (size >= maxsize)
790 goto oversize;
791 p += size+1;
792 }
793 if (size >= maxsize)
794 goto oversize;
795 return param_array(kp->name, val, min, max, obsparm->addr,
796 maxsize, obsparm_copy_string, &dummy);
797 }
798 printk(KERN_ERR "Unknown obsolete parameter type %s\n", obsparm->type);
799 return -EINVAL;
800 oversize:
801 printk(KERN_ERR
802 "Parameter %s doesn't fit in %u chars.\n", kp->name, maxsize);
803 return -EINVAL;
804}
805
806static int obsolete_params(const char *name,
807 char *args,
808 struct obsolete_modparm obsparm[],
809 unsigned int num,
810 Elf_Shdr *sechdrs,
811 unsigned int symindex,
812 const char *strtab)
813{
814 struct kernel_param *kp;
815 unsigned int i;
816 int ret;
817
818 kp = kmalloc(sizeof(kp[0]) * num, GFP_KERNEL);
819 if (!kp)
820 return -ENOMEM;
821
822 for (i = 0; i < num; i++) {
823 char sym_name[128 + sizeof(MODULE_SYMBOL_PREFIX)];
824
825 snprintf(sym_name, sizeof(sym_name), "%s%s",
826 MODULE_SYMBOL_PREFIX, obsparm[i].name);
827
828 kp[i].name = obsparm[i].name;
829 kp[i].perm = 000;
830 kp[i].set = set_obsolete;
831 kp[i].get = NULL;
832 obsparm[i].addr
833 = (void *)find_local_symbol(sechdrs, symindex, strtab,
834 sym_name);
835 if (!obsparm[i].addr) {
836 printk("%s: falsely claims to have parameter %s\n",
837 name, obsparm[i].name);
838 ret = -EINVAL;
839 goto out;
840 }
841 kp[i].arg = &obsparm[i];
842 }
843
844 ret = parse_args(name, args, kp, num, NULL);
845 out:
846 kfree(kp);
847 return ret;
848}
849#else
850static int obsolete_params(const char *name,
851 char *args,
852 struct obsolete_modparm obsparm[],
853 unsigned int num,
854 Elf_Shdr *sechdrs,
855 unsigned int symindex,
856 const char *strtab)
857{
858 if (num != 0)
859 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
860 name);
861 return 0;
862}
863#endif /* CONFIG_OBSOLETE_MODPARM */
864
865static const char vermagic[] = VERMAGIC_STRING;
866
867#ifdef CONFIG_MODVERSIONS
868static int check_version(Elf_Shdr *sechdrs,
869 unsigned int versindex,
870 const char *symname,
871 struct module *mod,
872 const unsigned long *crc)
873{
874 unsigned int i, num_versions;
875 struct modversion_info *versions;
876
877 /* Exporting module didn't supply crcs? OK, we're already tainted. */
878 if (!crc)
879 return 1;
880
881 versions = (void *) sechdrs[versindex].sh_addr;
882 num_versions = sechdrs[versindex].sh_size
883 / sizeof(struct modversion_info);
884
885 for (i = 0; i < num_versions; i++) {
886 if (strcmp(versions[i].name, symname) != 0)
887 continue;
888
889 if (versions[i].crc == *crc)
890 return 1;
891 printk("%s: disagrees about version of symbol %s\n",
892 mod->name, symname);
893 DEBUGP("Found checksum %lX vs module %lX\n",
894 *crc, versions[i].crc);
895 return 0;
896 }
897 /* Not in module's version table. OK, but that taints the kernel. */
898 if (!(tainted & TAINT_FORCED_MODULE)) {
899 printk("%s: no version for \"%s\" found: kernel tainted.\n",
900 mod->name, symname);
9f158333 901 add_taint(TAINT_FORCED_MODULE);
1da177e4
LT
902 }
903 return 1;
904}
905
906static inline int check_modstruct_version(Elf_Shdr *sechdrs,
907 unsigned int versindex,
908 struct module *mod)
909{
910 const unsigned long *crc;
911 struct module *owner;
912
913 if (!__find_symbol("struct_module", &owner, &crc, 1))
914 BUG();
915 return check_version(sechdrs, versindex, "struct_module", mod,
916 crc);
917}
918
919/* First part is kernel version, which we ignore. */
920static inline int same_magic(const char *amagic, const char *bmagic)
921{
922 amagic += strcspn(amagic, " ");
923 bmagic += strcspn(bmagic, " ");
924 return strcmp(amagic, bmagic) == 0;
925}
926#else
927static inline int check_version(Elf_Shdr *sechdrs,
928 unsigned int versindex,
929 const char *symname,
930 struct module *mod,
931 const unsigned long *crc)
932{
933 return 1;
934}
935
936static inline int check_modstruct_version(Elf_Shdr *sechdrs,
937 unsigned int versindex,
938 struct module *mod)
939{
940 return 1;
941}
942
943static inline int same_magic(const char *amagic, const char *bmagic)
944{
945 return strcmp(amagic, bmagic) == 0;
946}
947#endif /* CONFIG_MODVERSIONS */
948
949/* Resolve a symbol for this module. I.e. if we find one, record usage.
950 Must be holding module_mutex. */
951static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
952 unsigned int versindex,
953 const char *name,
954 struct module *mod)
955{
956 struct module *owner;
957 unsigned long ret;
958 const unsigned long *crc;
959
960 spin_lock_irq(&modlist_lock);
961 ret = __find_symbol(name, &owner, &crc, mod->license_gplok);
962 if (ret) {
963 /* use_module can fail due to OOM, or module unloading */
964 if (!check_version(sechdrs, versindex, name, mod, crc) ||
965 !use_module(mod, owner))
966 ret = 0;
967 }
968 spin_unlock_irq(&modlist_lock);
969 return ret;
970}
971
972
973/*
974 * /sys/module/foo/sections stuff
975 * J. Corbet <corbet@lwn.net>
976 */
977#ifdef CONFIG_KALLSYMS
978static ssize_t module_sect_show(struct module_attribute *mattr,
979 struct module *mod, char *buf)
980{
981 struct module_sect_attr *sattr =
982 container_of(mattr, struct module_sect_attr, mattr);
983 return sprintf(buf, "0x%lx\n", sattr->address);
984}
985
986static void add_sect_attrs(struct module *mod, unsigned int nsect,
987 char *secstrings, Elf_Shdr *sechdrs)
988{
989 unsigned int nloaded = 0, i, size[2];
990 struct module_sect_attrs *sect_attrs;
991 struct module_sect_attr *sattr;
992 struct attribute **gattr;
993
994 /* Count loaded sections and allocate structures */
995 for (i = 0; i < nsect; i++)
996 if (sechdrs[i].sh_flags & SHF_ALLOC)
997 nloaded++;
998 size[0] = ALIGN(sizeof(*sect_attrs)
999 + nloaded * sizeof(sect_attrs->attrs[0]),
1000 sizeof(sect_attrs->grp.attrs[0]));
1001 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
1002 if (! (sect_attrs = kmalloc(size[0] + size[1], GFP_KERNEL)))
1003 return;
1004
1005 /* Setup section attributes. */
1006 sect_attrs->grp.name = "sections";
1007 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1008
1009 sattr = &sect_attrs->attrs[0];
1010 gattr = &sect_attrs->grp.attrs[0];
1011 for (i = 0; i < nsect; i++) {
1012 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1013 continue;
1014 sattr->address = sechdrs[i].sh_addr;
1015 strlcpy(sattr->name, secstrings + sechdrs[i].sh_name,
1016 MODULE_SECT_NAME_LEN);
1017 sattr->mattr.show = module_sect_show;
1018 sattr->mattr.store = NULL;
1019 sattr->mattr.attr.name = sattr->name;
1020 sattr->mattr.attr.owner = mod;
1021 sattr->mattr.attr.mode = S_IRUGO;
1022 *(gattr++) = &(sattr++)->mattr.attr;
1023 }
1024 *gattr = NULL;
1025
1026 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1027 goto out;
1028
1029 mod->sect_attrs = sect_attrs;
1030 return;
1031 out:
1032 kfree(sect_attrs);
1033}
1034
1035static void remove_sect_attrs(struct module *mod)
1036{
1037 if (mod->sect_attrs) {
1038 sysfs_remove_group(&mod->mkobj.kobj,
1039 &mod->sect_attrs->grp);
1040 /* We are positive that no one is using any sect attrs
1041 * at this point. Deallocate immediately. */
1042 kfree(mod->sect_attrs);
1043 mod->sect_attrs = NULL;
1044 }
1045}
1046
1047
1048#else
1049static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1050 char *sectstrings, Elf_Shdr *sechdrs)
1051{
1052}
1053
1054static inline void remove_sect_attrs(struct module *mod)
1055{
1056}
1057#endif /* CONFIG_KALLSYMS */
1058
1059
1060#ifdef CONFIG_MODULE_UNLOAD
1061static inline int module_add_refcnt_attr(struct module *mod)
1062{
1063 return sysfs_create_file(&mod->mkobj.kobj, &refcnt.attr);
1064}
1065static void module_remove_refcnt_attr(struct module *mod)
1066{
1067 return sysfs_remove_file(&mod->mkobj.kobj, &refcnt.attr);
1068}
1069#else
1070static inline int module_add_refcnt_attr(struct module *mod)
1071{
1072 return 0;
1073}
1074static void module_remove_refcnt_attr(struct module *mod)
1075{
1076}
1077#endif
1078
c988d2b2
MD
1079#ifdef CONFIG_MODULE_UNLOAD
1080static int module_add_modinfo_attrs(struct module *mod)
1081{
1082 struct module_attribute *attr;
1083 int error = 0;
1084 int i;
1085
1086 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1087 if (!attr->test ||
1088 (attr->test && attr->test(mod)))
1089 error = sysfs_create_file(&mod->mkobj.kobj,&attr->attr);
1090 }
1091 return error;
1092}
1093
1094static void module_remove_modinfo_attrs(struct module *mod)
1095{
1096 struct module_attribute *attr;
1097 int i;
1098
1099 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1100 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1101 attr->free(mod);
1102 }
1103}
1104#endif
1da177e4
LT
1105
1106static int mod_sysfs_setup(struct module *mod,
1107 struct kernel_param *kparam,
1108 unsigned int num_params)
1109{
1110 int err;
1111
1112 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1113 err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name);
1114 if (err)
1115 goto out;
1116 kobj_set_kset_s(&mod->mkobj, module_subsys);
1117 mod->mkobj.mod = mod;
1118 err = kobject_register(&mod->mkobj.kobj);
1119 if (err)
1120 goto out;
1121
1122 err = module_add_refcnt_attr(mod);
1123 if (err)
1124 goto out_unreg;
1125
1126 err = module_param_sysfs_setup(mod, kparam, num_params);
1127 if (err)
1128 goto out_unreg;
1129
c988d2b2
MD
1130#ifdef CONFIG_MODULE_UNLOAD
1131 err = module_add_modinfo_attrs(mod);
1132 if (err)
1133 goto out_unreg;
1134#endif
1135
1da177e4
LT
1136 return 0;
1137
1138out_unreg:
1139 kobject_unregister(&mod->mkobj.kobj);
1140out:
1141 return err;
1142}
1143
1144static void mod_kobject_remove(struct module *mod)
1145{
c988d2b2
MD
1146#ifdef CONFIG_MODULE_UNLOAD
1147 module_remove_modinfo_attrs(mod);
1148#endif
1da177e4
LT
1149 module_remove_refcnt_attr(mod);
1150 module_param_sysfs_remove(mod);
1151
1152 kobject_unregister(&mod->mkobj.kobj);
1153}
1154
1155/*
1156 * unlink the module with the whole machine is stopped with interrupts off
1157 * - this defends against kallsyms not taking locks
1158 */
1159static int __unlink_module(void *_mod)
1160{
1161 struct module *mod = _mod;
1162 list_del(&mod->list);
1163 return 0;
1164}
1165
1166/* Free a module, remove from lists, etc (must hold module mutex). */
1167static void free_module(struct module *mod)
1168{
1169 /* Delete from various lists */
1170 stop_machine_run(__unlink_module, mod, NR_CPUS);
1171 remove_sect_attrs(mod);
1172 mod_kobject_remove(mod);
1173
1174 /* Arch-specific cleanup. */
1175 module_arch_cleanup(mod);
1176
1177 /* Module unload stuff */
1178 module_unload_free(mod);
1179
1180 /* This may be NULL, but that's OK */
1181 module_free(mod, mod->module_init);
1182 kfree(mod->args);
1183 if (mod->percpu)
1184 percpu_modfree(mod->percpu);
1185
1186 /* Finally, free the core (containing the module structure) */
1187 module_free(mod, mod->module_core);
1188}
1189
1190void *__symbol_get(const char *symbol)
1191{
1192 struct module *owner;
1193 unsigned long value, flags;
1194 const unsigned long *crc;
1195
1196 spin_lock_irqsave(&modlist_lock, flags);
1197 value = __find_symbol(symbol, &owner, &crc, 1);
1198 if (value && !strong_try_module_get(owner))
1199 value = 0;
1200 spin_unlock_irqrestore(&modlist_lock, flags);
1201
1202 return (void *)value;
1203}
1204EXPORT_SYMBOL_GPL(__symbol_get);
1205
1206/* Change all symbols so that sh_value encodes the pointer directly. */
1207static int simplify_symbols(Elf_Shdr *sechdrs,
1208 unsigned int symindex,
1209 const char *strtab,
1210 unsigned int versindex,
1211 unsigned int pcpuindex,
1212 struct module *mod)
1213{
1214 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1215 unsigned long secbase;
1216 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1217 int ret = 0;
1218
1219 for (i = 1; i < n; i++) {
1220 switch (sym[i].st_shndx) {
1221 case SHN_COMMON:
1222 /* We compiled with -fno-common. These are not
1223 supposed to happen. */
1224 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1225 printk("%s: please compile with -fno-common\n",
1226 mod->name);
1227 ret = -ENOEXEC;
1228 break;
1229
1230 case SHN_ABS:
1231 /* Don't need to do anything */
1232 DEBUGP("Absolute symbol: 0x%08lx\n",
1233 (long)sym[i].st_value);
1234 break;
1235
1236 case SHN_UNDEF:
1237 sym[i].st_value
1238 = resolve_symbol(sechdrs, versindex,
1239 strtab + sym[i].st_name, mod);
1240
1241 /* Ok if resolved. */
1242 if (sym[i].st_value != 0)
1243 break;
1244 /* Ok if weak. */
1245 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1246 break;
1247
1248 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1249 mod->name, strtab + sym[i].st_name);
1250 ret = -ENOENT;
1251 break;
1252
1253 default:
1254 /* Divert to percpu allocation if a percpu var. */
1255 if (sym[i].st_shndx == pcpuindex)
1256 secbase = (unsigned long)mod->percpu;
1257 else
1258 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1259 sym[i].st_value += secbase;
1260 break;
1261 }
1262 }
1263
1264 return ret;
1265}
1266
1267/* Update size with this section: return offset. */
1268static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1269{
1270 long ret;
1271
1272 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1273 *size = ret + sechdr->sh_size;
1274 return ret;
1275}
1276
1277/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1278 might -- code, read-only data, read-write data, small data. Tally
1279 sizes, and place the offsets into sh_entsize fields: high bit means it
1280 belongs in init. */
1281static void layout_sections(struct module *mod,
1282 const Elf_Ehdr *hdr,
1283 Elf_Shdr *sechdrs,
1284 const char *secstrings)
1285{
1286 static unsigned long const masks[][2] = {
1287 /* NOTE: all executable code must be the first section
1288 * in this array; otherwise modify the text_size
1289 * finder in the two loops below */
1290 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1291 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1292 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1293 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1294 };
1295 unsigned int m, i;
1296
1297 for (i = 0; i < hdr->e_shnum; i++)
1298 sechdrs[i].sh_entsize = ~0UL;
1299
1300 DEBUGP("Core section allocation order:\n");
1301 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1302 for (i = 0; i < hdr->e_shnum; ++i) {
1303 Elf_Shdr *s = &sechdrs[i];
1304
1305 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1306 || (s->sh_flags & masks[m][1])
1307 || s->sh_entsize != ~0UL
1308 || strncmp(secstrings + s->sh_name,
1309 ".init", 5) == 0)
1310 continue;
1311 s->sh_entsize = get_offset(&mod->core_size, s);
1312 DEBUGP("\t%s\n", secstrings + s->sh_name);
1313 }
1314 if (m == 0)
1315 mod->core_text_size = mod->core_size;
1316 }
1317
1318 DEBUGP("Init section allocation order:\n");
1319 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1320 for (i = 0; i < hdr->e_shnum; ++i) {
1321 Elf_Shdr *s = &sechdrs[i];
1322
1323 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1324 || (s->sh_flags & masks[m][1])
1325 || s->sh_entsize != ~0UL
1326 || strncmp(secstrings + s->sh_name,
1327 ".init", 5) != 0)
1328 continue;
1329 s->sh_entsize = (get_offset(&mod->init_size, s)
1330 | INIT_OFFSET_MASK);
1331 DEBUGP("\t%s\n", secstrings + s->sh_name);
1332 }
1333 if (m == 0)
1334 mod->init_text_size = mod->init_size;
1335 }
1336}
1337
1338static inline int license_is_gpl_compatible(const char *license)
1339{
1340 return (strcmp(license, "GPL") == 0
1341 || strcmp(license, "GPL v2") == 0
1342 || strcmp(license, "GPL and additional rights") == 0
1343 || strcmp(license, "Dual BSD/GPL") == 0
1344 || strcmp(license, "Dual MPL/GPL") == 0);
1345}
1346
1347static void set_license(struct module *mod, const char *license)
1348{
1349 if (!license)
1350 license = "unspecified";
1351
1352 mod->license_gplok = license_is_gpl_compatible(license);
1353 if (!mod->license_gplok && !(tainted & TAINT_PROPRIETARY_MODULE)) {
1354 printk(KERN_WARNING "%s: module license '%s' taints kernel.\n",
1355 mod->name, license);
9f158333 1356 add_taint(TAINT_PROPRIETARY_MODULE);
1da177e4
LT
1357 }
1358}
1359
1360/* Parse tag=value strings from .modinfo section */
1361static char *next_string(char *string, unsigned long *secsize)
1362{
1363 /* Skip non-zero chars */
1364 while (string[0]) {
1365 string++;
1366 if ((*secsize)-- <= 1)
1367 return NULL;
1368 }
1369
1370 /* Skip any zero padding. */
1371 while (!string[0]) {
1372 string++;
1373 if ((*secsize)-- <= 1)
1374 return NULL;
1375 }
1376 return string;
1377}
1378
1379static char *get_modinfo(Elf_Shdr *sechdrs,
1380 unsigned int info,
1381 const char *tag)
1382{
1383 char *p;
1384 unsigned int taglen = strlen(tag);
1385 unsigned long size = sechdrs[info].sh_size;
1386
1387 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1388 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1389 return p + taglen + 1;
1390 }
1391 return NULL;
1392}
1393
c988d2b2
MD
1394#ifdef CONFIG_MODULE_UNLOAD
1395static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1396 unsigned int infoindex)
1397{
1398 struct module_attribute *attr;
1399 int i;
1400
1401 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1402 if (attr->setup)
1403 attr->setup(mod,
1404 get_modinfo(sechdrs,
1405 infoindex,
1406 attr->attr.name));
1407 }
1408}
1409#endif
1410
1da177e4
LT
1411#ifdef CONFIG_KALLSYMS
1412int is_exported(const char *name, const struct module *mod)
1413{
1414 unsigned int i;
1415
1416 if (!mod) {
1417 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++)
1418 if (strcmp(__start___ksymtab[i].name, name) == 0)
1419 return 1;
1420 return 0;
1421 }
1422 for (i = 0; i < mod->num_syms; i++)
1423 if (strcmp(mod->syms[i].name, name) == 0)
1424 return 1;
1425 return 0;
1426}
1427
1428/* As per nm */
1429static char elf_type(const Elf_Sym *sym,
1430 Elf_Shdr *sechdrs,
1431 const char *secstrings,
1432 struct module *mod)
1433{
1434 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1435 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1436 return 'v';
1437 else
1438 return 'w';
1439 }
1440 if (sym->st_shndx == SHN_UNDEF)
1441 return 'U';
1442 if (sym->st_shndx == SHN_ABS)
1443 return 'a';
1444 if (sym->st_shndx >= SHN_LORESERVE)
1445 return '?';
1446 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1447 return 't';
1448 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1449 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1450 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1451 return 'r';
1452 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1453 return 'g';
1454 else
1455 return 'd';
1456 }
1457 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1458 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1459 return 's';
1460 else
1461 return 'b';
1462 }
1463 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1464 ".debug", strlen(".debug")) == 0)
1465 return 'n';
1466 return '?';
1467}
1468
1469static void add_kallsyms(struct module *mod,
1470 Elf_Shdr *sechdrs,
1471 unsigned int symindex,
1472 unsigned int strindex,
1473 const char *secstrings)
1474{
1475 unsigned int i;
1476
1477 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1478 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1479 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1480
1481 /* Set types up while we still have access to sections. */
1482 for (i = 0; i < mod->num_symtab; i++)
1483 mod->symtab[i].st_info
1484 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1485}
1486#else
1487static inline void add_kallsyms(struct module *mod,
1488 Elf_Shdr *sechdrs,
1489 unsigned int symindex,
1490 unsigned int strindex,
1491 const char *secstrings)
1492{
1493}
1494#endif /* CONFIG_KALLSYMS */
1495
1496/* Allocate and load the module: note that size of section 0 is always
1497 zero, and we rely on this for optional sections. */
1498static struct module *load_module(void __user *umod,
1499 unsigned long len,
1500 const char __user *uargs)
1501{
1502 Elf_Ehdr *hdr;
1503 Elf_Shdr *sechdrs;
1504 char *secstrings, *args, *modmagic, *strtab = NULL;
1505 unsigned int i, symindex = 0, strindex = 0, setupindex, exindex,
1506 exportindex, modindex, obsparmindex, infoindex, gplindex,
1507 crcindex, gplcrcindex, versindex, pcpuindex;
1508 long arglen;
1509 struct module *mod;
1510 long err = 0;
1511 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1512 struct exception_table_entry *extable;
378bac82 1513 mm_segment_t old_fs;
1da177e4
LT
1514
1515 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1516 umod, len, uargs);
1517 if (len < sizeof(*hdr))
1518 return ERR_PTR(-ENOEXEC);
1519
1520 /* Suck in entire file: we'll want most of it. */
1521 /* vmalloc barfs on "unusual" numbers. Check here */
1522 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1523 return ERR_PTR(-ENOMEM);
1524 if (copy_from_user(hdr, umod, len) != 0) {
1525 err = -EFAULT;
1526 goto free_hdr;
1527 }
1528
1529 /* Sanity checks against insmoding binaries or wrong arch,
1530 weird elf version */
1531 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1532 || hdr->e_type != ET_REL
1533 || !elf_check_arch(hdr)
1534 || hdr->e_shentsize != sizeof(*sechdrs)) {
1535 err = -ENOEXEC;
1536 goto free_hdr;
1537 }
1538
1539 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1540 goto truncated;
1541
1542 /* Convenience variables */
1543 sechdrs = (void *)hdr + hdr->e_shoff;
1544 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1545 sechdrs[0].sh_addr = 0;
1546
1547 for (i = 1; i < hdr->e_shnum; i++) {
1548 if (sechdrs[i].sh_type != SHT_NOBITS
1549 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1550 goto truncated;
1551
1552 /* Mark all sections sh_addr with their address in the
1553 temporary image. */
1554 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1555
1556 /* Internal symbols and strings. */
1557 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1558 symindex = i;
1559 strindex = sechdrs[i].sh_link;
1560 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1561 }
1562#ifndef CONFIG_MODULE_UNLOAD
1563 /* Don't load .exit sections */
1564 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1565 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1566#endif
1567 }
1568
1569 modindex = find_sec(hdr, sechdrs, secstrings,
1570 ".gnu.linkonce.this_module");
1571 if (!modindex) {
1572 printk(KERN_WARNING "No module found in object\n");
1573 err = -ENOEXEC;
1574 goto free_hdr;
1575 }
1576 mod = (void *)sechdrs[modindex].sh_addr;
1577
1578 if (symindex == 0) {
1579 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1580 mod->name);
1581 err = -ENOEXEC;
1582 goto free_hdr;
1583 }
1584
1585 /* Optional sections */
1586 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1587 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
1588 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1589 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
1590 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1591 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1592 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1593 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1594 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1595 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1596
1597 /* Don't keep modinfo section */
1598 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1599#ifdef CONFIG_KALLSYMS
1600 /* Keep symbol and string tables for decoding later. */
1601 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1602 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1603#endif
1604
1605 /* Check module struct version now, before we try to use module. */
1606 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1607 err = -ENOEXEC;
1608 goto free_hdr;
1609 }
1610
1611 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1612 /* This is allowed: modprobe --force will invalidate it. */
1613 if (!modmagic) {
9f158333 1614 add_taint(TAINT_FORCED_MODULE);
1da177e4
LT
1615 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1616 mod->name);
1617 } else if (!same_magic(modmagic, vermagic)) {
1618 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1619 mod->name, modmagic, vermagic);
1620 err = -ENOEXEC;
1621 goto free_hdr;
1622 }
1623
1624 /* Now copy in args */
1625 arglen = strlen_user(uargs);
1626 if (!arglen) {
1627 err = -EFAULT;
1628 goto free_hdr;
1629 }
1630 args = kmalloc(arglen, GFP_KERNEL);
1631 if (!args) {
1632 err = -ENOMEM;
1633 goto free_hdr;
1634 }
1635 if (copy_from_user(args, uargs, arglen) != 0) {
1636 err = -EFAULT;
1637 goto free_mod;
1638 }
1639
1640 if (find_module(mod->name)) {
1641 err = -EEXIST;
1642 goto free_mod;
1643 }
1644
1645 mod->state = MODULE_STATE_COMING;
1646
1647 /* Allow arches to frob section contents and sizes. */
1648 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1649 if (err < 0)
1650 goto free_mod;
1651
1652 if (pcpuindex) {
1653 /* We have a special allocation for this section. */
1654 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
842bbaaa
RR
1655 sechdrs[pcpuindex].sh_addralign,
1656 mod->name);
1da177e4
LT
1657 if (!percpu) {
1658 err = -ENOMEM;
1659 goto free_mod;
1660 }
1661 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1662 mod->percpu = percpu;
1663 }
1664
1665 /* Determine total sizes, and put offsets in sh_entsize. For now
1666 this is done generically; there doesn't appear to be any
1667 special cases for the architectures. */
1668 layout_sections(mod, hdr, sechdrs, secstrings);
1669
1670 /* Do the allocs. */
1671 ptr = module_alloc(mod->core_size);
1672 if (!ptr) {
1673 err = -ENOMEM;
1674 goto free_percpu;
1675 }
1676 memset(ptr, 0, mod->core_size);
1677 mod->module_core = ptr;
1678
1679 ptr = module_alloc(mod->init_size);
1680 if (!ptr && mod->init_size) {
1681 err = -ENOMEM;
1682 goto free_core;
1683 }
1684 memset(ptr, 0, mod->init_size);
1685 mod->module_init = ptr;
1686
1687 /* Transfer each section which specifies SHF_ALLOC */
1688 DEBUGP("final section addresses:\n");
1689 for (i = 0; i < hdr->e_shnum; i++) {
1690 void *dest;
1691
1692 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1693 continue;
1694
1695 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1696 dest = mod->module_init
1697 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1698 else
1699 dest = mod->module_core + sechdrs[i].sh_entsize;
1700
1701 if (sechdrs[i].sh_type != SHT_NOBITS)
1702 memcpy(dest, (void *)sechdrs[i].sh_addr,
1703 sechdrs[i].sh_size);
1704 /* Update sh_addr to point to copy in image. */
1705 sechdrs[i].sh_addr = (unsigned long)dest;
1706 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1707 }
1708 /* Module has been moved. */
1709 mod = (void *)sechdrs[modindex].sh_addr;
1710
1711 /* Now we've moved module, initialize linked lists, etc. */
1712 module_unload_init(mod);
1713
1714 /* Set up license info based on the info section */
1715 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1716
c988d2b2
MD
1717#ifdef CONFIG_MODULE_UNLOAD
1718 /* Set up MODINFO_ATTR fields */
1719 setup_modinfo(mod, sechdrs, infoindex);
1720#endif
1721
1da177e4
LT
1722 /* Fix up syms, so that st_value is a pointer to location. */
1723 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
1724 mod);
1725 if (err < 0)
1726 goto cleanup;
1727
1728 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1729 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1730 mod->syms = (void *)sechdrs[exportindex].sh_addr;
1731 if (crcindex)
1732 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1733 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1734 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1735 if (gplcrcindex)
1736 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
1737
1738#ifdef CONFIG_MODVERSIONS
1739 if ((mod->num_syms && !crcindex) ||
1740 (mod->num_gpl_syms && !gplcrcindex)) {
1741 printk(KERN_WARNING "%s: No versions for exported symbols."
1742 " Tainting kernel.\n", mod->name);
9f158333 1743 add_taint(TAINT_FORCED_MODULE);
1da177e4
LT
1744 }
1745#endif
1746
1747 /* Now do relocations. */
1748 for (i = 1; i < hdr->e_shnum; i++) {
1749 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1750 unsigned int info = sechdrs[i].sh_info;
1751
1752 /* Not a valid relocation section? */
1753 if (info >= hdr->e_shnum)
1754 continue;
1755
1756 /* Don't bother with non-allocated sections */
1757 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
1758 continue;
1759
1760 if (sechdrs[i].sh_type == SHT_REL)
1761 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1762 else if (sechdrs[i].sh_type == SHT_RELA)
1763 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1764 mod);
1765 if (err < 0)
1766 goto cleanup;
1767 }
1768
1769 /* Set up and sort exception table */
1770 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1771 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
1772 sort_extable(extable, extable + mod->num_exentries);
1773
1774 /* Finally, copy percpu area over. */
1775 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
1776 sechdrs[pcpuindex].sh_size);
1777
1778 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
1779
1780 err = module_finalize(hdr, sechdrs, mod);
1781 if (err < 0)
1782 goto cleanup;
1783
378bac82
TK
1784 /* flush the icache in correct context */
1785 old_fs = get_fs();
1786 set_fs(KERNEL_DS);
1787
1788 /*
1789 * Flush the instruction cache, since we've played with text.
1790 * Do it before processing of module parameters, so the module
1791 * can provide parameter accessor functions of its own.
1792 */
1793 if (mod->module_init)
1794 flush_icache_range((unsigned long)mod->module_init,
1795 (unsigned long)mod->module_init
1796 + mod->init_size);
1797 flush_icache_range((unsigned long)mod->module_core,
1798 (unsigned long)mod->module_core + mod->core_size);
1799
1800 set_fs(old_fs);
1801
1da177e4
LT
1802 mod->args = args;
1803 if (obsparmindex) {
1804 err = obsolete_params(mod->name, mod->args,
1805 (struct obsolete_modparm *)
1806 sechdrs[obsparmindex].sh_addr,
1807 sechdrs[obsparmindex].sh_size
1808 / sizeof(struct obsolete_modparm),
1809 sechdrs, symindex,
1810 (char *)sechdrs[strindex].sh_addr);
1811 if (setupindex)
1812 printk(KERN_WARNING "%s: Ignoring new-style "
1813 "parameters in presence of obsolete ones\n",
1814 mod->name);
1815 } else {
1816 /* Size of section 0 is 0, so this works well if no params */
1817 err = parse_args(mod->name, mod->args,
1818 (struct kernel_param *)
1819 sechdrs[setupindex].sh_addr,
1820 sechdrs[setupindex].sh_size
1821 / sizeof(struct kernel_param),
1822 NULL);
1823 }
1824 if (err < 0)
1825 goto arch_cleanup;
1826
1827 err = mod_sysfs_setup(mod,
1828 (struct kernel_param *)
1829 sechdrs[setupindex].sh_addr,
1830 sechdrs[setupindex].sh_size
1831 / sizeof(struct kernel_param));
1832 if (err < 0)
1833 goto arch_cleanup;
1834 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
1835
1836 /* Get rid of temporary copy */
1837 vfree(hdr);
1838
1839 /* Done! */
1840 return mod;
1841
1842 arch_cleanup:
1843 module_arch_cleanup(mod);
1844 cleanup:
1845 module_unload_free(mod);
1846 module_free(mod, mod->module_init);
1847 free_core:
1848 module_free(mod, mod->module_core);
1849 free_percpu:
1850 if (percpu)
1851 percpu_modfree(percpu);
1852 free_mod:
1853 kfree(args);
1854 free_hdr:
1855 vfree(hdr);
1856 if (err < 0) return ERR_PTR(err);
1857 else return ptr;
1858
1859 truncated:
1860 printk(KERN_ERR "Module len %lu truncated\n", len);
1861 err = -ENOEXEC;
1862 goto free_hdr;
1863}
1864
1865/*
1866 * link the module with the whole machine is stopped with interrupts off
1867 * - this defends against kallsyms not taking locks
1868 */
1869static int __link_module(void *_mod)
1870{
1871 struct module *mod = _mod;
1872 list_add(&mod->list, &modules);
1873 return 0;
1874}
1875
1876/* This is where the real work happens */
1877asmlinkage long
1878sys_init_module(void __user *umod,
1879 unsigned long len,
1880 const char __user *uargs)
1881{
1882 struct module *mod;
1883 int ret = 0;
1884
1885 /* Must have permission */
1886 if (!capable(CAP_SYS_MODULE))
1887 return -EPERM;
1888
1889 /* Only one module load at a time, please */
1890 if (down_interruptible(&module_mutex) != 0)
1891 return -EINTR;
1892
1893 /* Do all the hard work */
1894 mod = load_module(umod, len, uargs);
1895 if (IS_ERR(mod)) {
1896 up(&module_mutex);
1897 return PTR_ERR(mod);
1898 }
1899
1da177e4
LT
1900 /* Now sew it into the lists. They won't access us, since
1901 strong_try_module_get() will fail. */
1902 stop_machine_run(__link_module, mod, NR_CPUS);
1903
1904 /* Drop lock so they can recurse */
1905 up(&module_mutex);
1906
1907 down(&notify_mutex);
1908 notifier_call_chain(&module_notify_list, MODULE_STATE_COMING, mod);
1909 up(&notify_mutex);
1910
1911 /* Start the module */
1912 if (mod->init != NULL)
1913 ret = mod->init();
1914 if (ret < 0) {
1915 /* Init routine failed: abort. Try to protect us from
1916 buggy refcounters. */
1917 mod->state = MODULE_STATE_GOING;
fbd568a3 1918 synchronize_sched();
1da177e4
LT
1919 if (mod->unsafe)
1920 printk(KERN_ERR "%s: module is now stuck!\n",
1921 mod->name);
1922 else {
1923 module_put(mod);
1924 down(&module_mutex);
1925 free_module(mod);
1926 up(&module_mutex);
1927 }
1928 return ret;
1929 }
1930
1931 /* Now it's a first class citizen! */
1932 down(&module_mutex);
1933 mod->state = MODULE_STATE_LIVE;
1934 /* Drop initial reference. */
1935 module_put(mod);
1936 module_free(mod, mod->module_init);
1937 mod->module_init = NULL;
1938 mod->init_size = 0;
1939 mod->init_text_size = 0;
1940 up(&module_mutex);
1941
1942 return 0;
1943}
1944
1945static inline int within(unsigned long addr, void *start, unsigned long size)
1946{
1947 return ((void *)addr >= start && (void *)addr < start + size);
1948}
1949
1950#ifdef CONFIG_KALLSYMS
1951/*
1952 * This ignores the intensely annoying "mapping symbols" found
1953 * in ARM ELF files: $a, $t and $d.
1954 */
1955static inline int is_arm_mapping_symbol(const char *str)
1956{
1957 return str[0] == '$' && strchr("atd", str[1])
1958 && (str[2] == '\0' || str[2] == '.');
1959}
1960
1961static const char *get_ksymbol(struct module *mod,
1962 unsigned long addr,
1963 unsigned long *size,
1964 unsigned long *offset)
1965{
1966 unsigned int i, best = 0;
1967 unsigned long nextval;
1968
1969 /* At worse, next value is at end of module */
1970 if (within(addr, mod->module_init, mod->init_size))
1971 nextval = (unsigned long)mod->module_init+mod->init_text_size;
1972 else
1973 nextval = (unsigned long)mod->module_core+mod->core_text_size;
1974
1975 /* Scan for closest preceeding symbol, and next symbol. (ELF
1976 starts real symbols at 1). */
1977 for (i = 1; i < mod->num_symtab; i++) {
1978 if (mod->symtab[i].st_shndx == SHN_UNDEF)
1979 continue;
1980
1981 /* We ignore unnamed symbols: they're uninformative
1982 * and inserted at a whim. */
1983 if (mod->symtab[i].st_value <= addr
1984 && mod->symtab[i].st_value > mod->symtab[best].st_value
1985 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
1986 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
1987 best = i;
1988 if (mod->symtab[i].st_value > addr
1989 && mod->symtab[i].st_value < nextval
1990 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
1991 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
1992 nextval = mod->symtab[i].st_value;
1993 }
1994
1995 if (!best)
1996 return NULL;
1997
1998 *size = nextval - mod->symtab[best].st_value;
1999 *offset = addr - mod->symtab[best].st_value;
2000 return mod->strtab + mod->symtab[best].st_name;
2001}
2002
2003/* For kallsyms to ask for address resolution. NULL means not found.
2004 We don't lock, as this is used for oops resolution and races are a
2005 lesser concern. */
2006const char *module_address_lookup(unsigned long addr,
2007 unsigned long *size,
2008 unsigned long *offset,
2009 char **modname)
2010{
2011 struct module *mod;
2012
2013 list_for_each_entry(mod, &modules, list) {
2014 if (within(addr, mod->module_init, mod->init_size)
2015 || within(addr, mod->module_core, mod->core_size)) {
2016 *modname = mod->name;
2017 return get_ksymbol(mod, addr, size, offset);
2018 }
2019 }
2020 return NULL;
2021}
2022
2023struct module *module_get_kallsym(unsigned int symnum,
2024 unsigned long *value,
2025 char *type,
2026 char namebuf[128])
2027{
2028 struct module *mod;
2029
2030 down(&module_mutex);
2031 list_for_each_entry(mod, &modules, list) {
2032 if (symnum < mod->num_symtab) {
2033 *value = mod->symtab[symnum].st_value;
2034 *type = mod->symtab[symnum].st_info;
2035 strncpy(namebuf,
2036 mod->strtab + mod->symtab[symnum].st_name,
2037 127);
2038 up(&module_mutex);
2039 return mod;
2040 }
2041 symnum -= mod->num_symtab;
2042 }
2043 up(&module_mutex);
2044 return NULL;
2045}
2046
2047static unsigned long mod_find_symname(struct module *mod, const char *name)
2048{
2049 unsigned int i;
2050
2051 for (i = 0; i < mod->num_symtab; i++)
2052 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0)
2053 return mod->symtab[i].st_value;
2054 return 0;
2055}
2056
2057/* Look for this name: can be of form module:name. */
2058unsigned long module_kallsyms_lookup_name(const char *name)
2059{
2060 struct module *mod;
2061 char *colon;
2062 unsigned long ret = 0;
2063
2064 /* Don't lock: we're in enough trouble already. */
2065 if ((colon = strchr(name, ':')) != NULL) {
2066 *colon = '\0';
2067 if ((mod = find_module(name)) != NULL)
2068 ret = mod_find_symname(mod, colon+1);
2069 *colon = ':';
2070 } else {
2071 list_for_each_entry(mod, &modules, list)
2072 if ((ret = mod_find_symname(mod, name)) != 0)
2073 break;
2074 }
2075 return ret;
2076}
2077#endif /* CONFIG_KALLSYMS */
2078
2079/* Called by the /proc file system to return a list of modules. */
2080static void *m_start(struct seq_file *m, loff_t *pos)
2081{
2082 struct list_head *i;
2083 loff_t n = 0;
2084
2085 down(&module_mutex);
2086 list_for_each(i, &modules) {
2087 if (n++ == *pos)
2088 break;
2089 }
2090 if (i == &modules)
2091 return NULL;
2092 return i;
2093}
2094
2095static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2096{
2097 struct list_head *i = p;
2098 (*pos)++;
2099 if (i->next == &modules)
2100 return NULL;
2101 return i->next;
2102}
2103
2104static void m_stop(struct seq_file *m, void *p)
2105{
2106 up(&module_mutex);
2107}
2108
2109static int m_show(struct seq_file *m, void *p)
2110{
2111 struct module *mod = list_entry(p, struct module, list);
2112 seq_printf(m, "%s %lu",
2113 mod->name, mod->init_size + mod->core_size);
2114 print_unload_info(m, mod);
2115
2116 /* Informative for users. */
2117 seq_printf(m, " %s",
2118 mod->state == MODULE_STATE_GOING ? "Unloading":
2119 mod->state == MODULE_STATE_COMING ? "Loading":
2120 "Live");
2121 /* Used by oprofile and other similar tools. */
2122 seq_printf(m, " 0x%p", mod->module_core);
2123
2124 seq_printf(m, "\n");
2125 return 0;
2126}
2127
2128/* Format: modulename size refcount deps address
2129
2130 Where refcount is a number or -, and deps is a comma-separated list
2131 of depends or -.
2132*/
2133struct seq_operations modules_op = {
2134 .start = m_start,
2135 .next = m_next,
2136 .stop = m_stop,
2137 .show = m_show
2138};
2139
2140/* Given an address, look for it in the module exception tables. */
2141const struct exception_table_entry *search_module_extables(unsigned long addr)
2142{
2143 unsigned long flags;
2144 const struct exception_table_entry *e = NULL;
2145 struct module *mod;
2146
2147 spin_lock_irqsave(&modlist_lock, flags);
2148 list_for_each_entry(mod, &modules, list) {
2149 if (mod->num_exentries == 0)
2150 continue;
2151
2152 e = search_extable(mod->extable,
2153 mod->extable + mod->num_exentries - 1,
2154 addr);
2155 if (e)
2156 break;
2157 }
2158 spin_unlock_irqrestore(&modlist_lock, flags);
2159
2160 /* Now, if we found one, we are running inside it now, hence
2161 we cannot unload the module, hence no refcnt needed. */
2162 return e;
2163}
2164
2165/* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
2166struct module *__module_text_address(unsigned long addr)
2167{
2168 struct module *mod;
2169
2170 list_for_each_entry(mod, &modules, list)
2171 if (within(addr, mod->module_init, mod->init_text_size)
2172 || within(addr, mod->module_core, mod->core_text_size))
2173 return mod;
2174 return NULL;
2175}
2176
2177struct module *module_text_address(unsigned long addr)
2178{
2179 struct module *mod;
2180 unsigned long flags;
2181
2182 spin_lock_irqsave(&modlist_lock, flags);
2183 mod = __module_text_address(addr);
2184 spin_unlock_irqrestore(&modlist_lock, flags);
2185
2186 return mod;
2187}
2188
2189/* Don't grab lock, we're oopsing. */
2190void print_modules(void)
2191{
2192 struct module *mod;
2193
2194 printk("Modules linked in:");
2195 list_for_each_entry(mod, &modules, list)
2196 printk(" %s", mod->name);
2197 printk("\n");
2198}
2199
2200void module_add_driver(struct module *mod, struct device_driver *drv)
2201{
2202 if (!mod || !drv)
2203 return;
2204
2205 /* Don't check return code; this call is idempotent */
2206 sysfs_create_link(&drv->kobj, &mod->mkobj.kobj, "module");
2207}
2208EXPORT_SYMBOL(module_add_driver);
2209
2210void module_remove_driver(struct device_driver *drv)
2211{
2212 if (!drv)
2213 return;
2214 sysfs_remove_link(&drv->kobj, "module");
2215}
2216EXPORT_SYMBOL(module_remove_driver);
2217
2218#ifdef CONFIG_MODVERSIONS
2219/* Generate the signature for struct module here, too, for modversions. */
2220void struct_module(struct module *mod) { return; }
2221EXPORT_SYMBOL(struct_module);
2222#endif