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