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