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