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.
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.
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.
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
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/fcntl.h>
28 #include <linux/rcupdate.h>
29 #include <linux/cpu.h>
30 #include <linux/moduleparam.h>
31 #include <linux/errno.h>
32 #include <linux/err.h>
33 #include <linux/vermagic.h>
34 #include <linux/notifier.h>
35 #include <asm/uaccess.h>
36 #include <asm/semaphore.h>
37 #include <asm/pgalloc.h>
38 #include <asm/cacheflush.h>
43 #define DEBUGP(fmt , a...)
46 #ifndef ARCH_SHF_SMALL
47 #define ARCH_SHF_SMALL 0
50 /* If this is set, the section belongs in the init part of the module */
51 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
53 #define symbol_is(literal, string) \
54 (strcmp(MODULE_SYMBOL_PREFIX literal, (string)) == 0)
56 /* Protects module list */
57 static spinlock_t modlist_lock
= SPIN_LOCK_UNLOCKED
;
59 /* List of modules, protected by module_mutex AND modlist_lock */
60 static DECLARE_MUTEX(module_mutex
);
61 static LIST_HEAD(modules
);
63 static DECLARE_MUTEX(notify_mutex
);
64 static struct notifier_block
* module_notify_list
;
66 int register_module_notifier(struct notifier_block
* nb
)
70 err
= notifier_chain_register(&module_notify_list
, nb
);
74 EXPORT_SYMBOL(register_module_notifier
);
76 int unregister_module_notifier(struct notifier_block
* nb
)
80 err
= notifier_chain_unregister(&module_notify_list
, nb
);
84 EXPORT_SYMBOL(unregister_module_notifier
);
86 /* We require a truly strong try_module_get() */
87 static inline int strong_try_module_get(struct module
*mod
)
89 if (mod
&& mod
->state
== MODULE_STATE_COMING
)
91 return try_module_get(mod
);
94 /* Stub function for modules which don't have an initfn */
99 EXPORT_SYMBOL(init_module
);
101 /* A thread that wants to hold a reference to a module only while it
102 * is running can call ths to safely exit.
103 * nfsd and lockd use this.
105 void __module_put_and_exit(struct module
*mod
, long code
)
110 EXPORT_SYMBOL(__module_put_and_exit
);
112 /* Find a module section: 0 means not found. */
113 static unsigned int find_sec(Elf_Ehdr
*hdr
,
115 const char *secstrings
,
120 for (i
= 1; i
< hdr
->e_shnum
; i
++)
121 /* Alloc bit cleared means "ignore it." */
122 if ((sechdrs
[i
].sh_flags
& SHF_ALLOC
)
123 && strcmp(secstrings
+sechdrs
[i
].sh_name
, name
) == 0)
128 /* Provided by the linker */
129 extern const struct kernel_symbol __start___ksymtab
[];
130 extern const struct kernel_symbol __stop___ksymtab
[];
131 extern const struct kernel_symbol __start___ksymtab_gpl
[];
132 extern const struct kernel_symbol __stop___ksymtab_gpl
[];
133 extern const unsigned long __start___kcrctab
[];
134 extern const unsigned long __start___kcrctab_gpl
[];
136 #ifndef CONFIG_MODVERSIONS
137 #define symversion(base, idx) NULL
139 #define symversion(base, idx) ((base) ? ((base) + (idx)) : NULL)
142 /* Find a symbol, return value, crc and module which owns it */
143 static unsigned long __find_symbol(const char *name
,
144 struct module
**owner
,
145 const unsigned long **crc
,
151 /* Core kernel first. */
153 for (i
= 0; __start___ksymtab
+i
< __stop___ksymtab
; i
++) {
154 if (strcmp(__start___ksymtab
[i
].name
, name
) == 0) {
155 *crc
= symversion(__start___kcrctab
, i
);
156 return __start___ksymtab
[i
].value
;
160 for (i
= 0; __start___ksymtab_gpl
+i
<__stop___ksymtab_gpl
; i
++)
161 if (strcmp(__start___ksymtab_gpl
[i
].name
, name
) == 0) {
162 *crc
= symversion(__start___kcrctab_gpl
, i
);
163 return __start___ksymtab_gpl
[i
].value
;
167 /* Now try modules. */
168 list_for_each_entry(mod
, &modules
, list
) {
170 for (i
= 0; i
< mod
->num_syms
; i
++)
171 if (strcmp(mod
->syms
[i
].name
, name
) == 0) {
172 *crc
= symversion(mod
->crcs
, i
);
173 return mod
->syms
[i
].value
;
177 for (i
= 0; i
< mod
->num_gpl_syms
; i
++) {
178 if (strcmp(mod
->gpl_syms
[i
].name
, name
) == 0) {
179 *crc
= symversion(mod
->gpl_crcs
, i
);
180 return mod
->gpl_syms
[i
].value
;
185 DEBUGP("Failed to find symbol %s\n", name
);
189 /* Find a symbol in this elf symbol table */
190 static unsigned long find_local_symbol(Elf_Shdr
*sechdrs
,
191 unsigned int symindex
,
196 Elf_Sym
*sym
= (void *)sechdrs
[symindex
].sh_addr
;
198 /* Search (defined) internal symbols first. */
199 for (i
= 1; i
< sechdrs
[symindex
].sh_size
/sizeof(*sym
); i
++) {
200 if (sym
[i
].st_shndx
!= SHN_UNDEF
201 && strcmp(name
, strtab
+ sym
[i
].st_name
) == 0)
202 return sym
[i
].st_value
;
207 /* Search for module by name: must hold module_mutex. */
208 static struct module
*find_module(const char *name
)
212 list_for_each_entry(mod
, &modules
, list
) {
213 if (strcmp(mod
->name
, name
) == 0)
220 /* Number of blocks used and allocated. */
221 static unsigned int pcpu_num_used
, pcpu_num_allocated
;
222 /* Size of each block. -ve means used. */
223 static int *pcpu_size
;
225 static int split_block(unsigned int i
, unsigned short size
)
227 /* Reallocation required? */
228 if (pcpu_num_used
+ 1 > pcpu_num_allocated
) {
229 int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated
*2,
234 memcpy(new, pcpu_size
, sizeof(new[0])*pcpu_num_allocated
);
235 pcpu_num_allocated
*= 2;
240 /* Insert a new subblock */
241 memmove(&pcpu_size
[i
+1], &pcpu_size
[i
],
242 sizeof(pcpu_size
[0]) * (pcpu_num_used
- i
));
245 pcpu_size
[i
+1] -= size
;
250 static inline unsigned int block_size(int val
)
257 /* Created by linker magic */
258 extern char __per_cpu_start
[], __per_cpu_end
[];
260 static void *percpu_modalloc(unsigned long size
, unsigned long align
)
266 BUG_ON(align
> SMP_CACHE_BYTES
);
268 ptr
= __per_cpu_start
;
269 for (i
= 0; i
< pcpu_num_used
; ptr
+= block_size(pcpu_size
[i
]), i
++) {
270 /* Extra for alignment requirement. */
271 extra
= ALIGN((unsigned long)ptr
, align
) - (unsigned long)ptr
;
272 BUG_ON(i
== 0 && extra
!= 0);
274 if (pcpu_size
[i
] < 0 || pcpu_size
[i
] < extra
+ size
)
277 /* Transfer extra to previous block. */
278 if (pcpu_size
[i
-1] < 0)
279 pcpu_size
[i
-1] -= extra
;
281 pcpu_size
[i
-1] += extra
;
282 pcpu_size
[i
] -= extra
;
285 /* Split block if warranted */
286 if (pcpu_size
[i
] - size
> sizeof(unsigned long))
287 if (!split_block(i
, size
))
291 pcpu_size
[i
] = -pcpu_size
[i
];
295 printk(KERN_WARNING
"Could not allocate %lu bytes percpu data\n",
300 static void percpu_modfree(void *freeme
)
303 void *ptr
= __per_cpu_start
+ block_size(pcpu_size
[0]);
305 /* First entry is core kernel percpu data. */
306 for (i
= 1; i
< pcpu_num_used
; ptr
+= block_size(pcpu_size
[i
]), i
++) {
308 pcpu_size
[i
] = -pcpu_size
[i
];
315 /* Merge with previous? */
316 if (pcpu_size
[i
-1] >= 0) {
317 pcpu_size
[i
-1] += pcpu_size
[i
];
319 memmove(&pcpu_size
[i
], &pcpu_size
[i
+1],
320 (pcpu_num_used
- i
) * sizeof(pcpu_size
[0]));
323 /* Merge with next? */
324 if (i
+1 < pcpu_num_used
&& pcpu_size
[i
+1] >= 0) {
325 pcpu_size
[i
] += pcpu_size
[i
+1];
327 memmove(&pcpu_size
[i
+1], &pcpu_size
[i
+2],
328 (pcpu_num_used
- (i
+1)) * sizeof(pcpu_size
[0]));
332 static unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
334 const char *secstrings
)
336 return find_sec(hdr
, sechdrs
, secstrings
, ".data.percpu");
339 static int percpu_modinit(void)
342 pcpu_num_allocated
= 2;
343 pcpu_size
= kmalloc(sizeof(pcpu_size
[0]) * pcpu_num_allocated
,
345 /* Static in-kernel percpu data (used). */
346 pcpu_size
[0] = -ALIGN(__per_cpu_end
-__per_cpu_start
, SMP_CACHE_BYTES
);
348 pcpu_size
[1] = PERCPU_ENOUGH_ROOM
+ pcpu_size
[0];
349 if (pcpu_size
[1] < 0) {
350 printk(KERN_ERR
"No per-cpu room for modules.\n");
356 __initcall(percpu_modinit
);
357 #else /* ... !CONFIG_SMP */
358 static inline void *percpu_modalloc(unsigned long size
, unsigned long align
)
362 static inline void percpu_modfree(void *pcpuptr
)
366 static inline unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
368 const char *secstrings
)
372 static inline void percpu_modcopy(void *pcpudst
, const void *src
,
375 /* pcpusec should be 0, and size of that section should be 0. */
378 #endif /* CONFIG_SMP */
380 #ifdef CONFIG_MODULE_UNLOAD
381 /* Init the unload section of the module. */
382 static void module_unload_init(struct module
*mod
)
386 INIT_LIST_HEAD(&mod
->modules_which_use_me
);
387 for (i
= 0; i
< NR_CPUS
; i
++)
388 local_set(&mod
->ref
[i
].count
, 0);
389 /* Hold reference count during initialization. */
390 local_set(&mod
->ref
[smp_processor_id()].count
, 1);
391 /* Backwards compatibility macros put refcount during init. */
392 mod
->waiter
= current
;
395 /* modules using other modules */
398 struct list_head list
;
399 struct module
*module_which_uses
;
402 /* Does a already use b? */
403 static int already_uses(struct module
*a
, struct module
*b
)
405 struct module_use
*use
;
407 list_for_each_entry(use
, &b
->modules_which_use_me
, list
) {
408 if (use
->module_which_uses
== a
) {
409 DEBUGP("%s uses %s!\n", a
->name
, b
->name
);
413 DEBUGP("%s does not use %s!\n", a
->name
, b
->name
);
417 /* Module a uses b */
418 static int use_module(struct module
*a
, struct module
*b
)
420 struct module_use
*use
;
421 if (b
== NULL
|| already_uses(a
, b
)) return 1;
423 if (!strong_try_module_get(b
))
426 DEBUGP("Allocating new usage for %s.\n", a
->name
);
427 use
= kmalloc(sizeof(*use
), GFP_ATOMIC
);
429 printk("%s: out of memory loading\n", a
->name
);
434 use
->module_which_uses
= a
;
435 list_add(&use
->list
, &b
->modules_which_use_me
);
439 /* Clear the unload stuff of the module. */
440 static void module_unload_free(struct module
*mod
)
444 list_for_each_entry(i
, &modules
, list
) {
445 struct module_use
*use
;
447 list_for_each_entry(use
, &i
->modules_which_use_me
, list
) {
448 if (use
->module_which_uses
== mod
) {
449 DEBUGP("%s unusing %s\n", mod
->name
, i
->name
);
451 list_del(&use
->list
);
453 /* There can be at most one match. */
461 /* Thread to stop each CPU in user context. */
469 static enum stopref_state stopref_state
;
470 static unsigned int stopref_num_threads
;
471 static atomic_t stopref_thread_ack
;
473 static int stopref(void *cpu
)
475 int irqs_disabled
= 0;
478 sprintf(current
->comm
, "kmodule%lu\n", (unsigned long)cpu
);
480 /* Highest priority we can manage, and move to right CPU. */
482 struct sched_param param
= { .sched_priority
= MAX_RT_PRIO
-1 };
483 setscheduler(current
->pid
, SCHED_FIFO
, ¶m
);
485 set_cpus_allowed(current
, 1UL << (unsigned long)cpu
);
487 /* Ack: we are alive */
488 atomic_inc(&stopref_thread_ack
);
490 /* Simple state machine */
491 while (stopref_state
!= STOPREF_EXIT
) {
492 if (stopref_state
== STOPREF_DISABLE_IRQ
&& !irqs_disabled
) {
495 /* Ack: irqs disabled. */
496 atomic_inc(&stopref_thread_ack
);
497 } else if (stopref_state
== STOPREF_PREPARE
&& !prepared
) {
498 /* Everyone is in place, hold CPU. */
501 atomic_inc(&stopref_thread_ack
);
503 if (irqs_disabled
|| prepared
)
509 /* Ack: we are exiting. */
510 atomic_inc(&stopref_thread_ack
);
520 /* Change the thread state */
521 static void stopref_set_state(enum stopref_state state
, int sleep
)
523 atomic_set(&stopref_thread_ack
, 0);
525 stopref_state
= state
;
526 while (atomic_read(&stopref_thread_ack
) != stopref_num_threads
) {
534 /* Stop the machine. Disables irqs. */
535 static int stop_refcounts(void)
538 unsigned long old_allowed
;
541 /* One thread per cpu. We'll do our own. */
542 cpu
= smp_processor_id();
544 /* FIXME: racy with set_cpus_allowed. */
545 old_allowed
= current
->cpus_allowed
;
546 set_cpus_allowed(current
, 1UL << (unsigned long)cpu
);
548 atomic_set(&stopref_thread_ack
, 0);
549 stopref_num_threads
= 0;
550 stopref_state
= STOPREF_WAIT
;
552 /* No CPUs can come up or down during this. */
555 for (i
= 0; i
< NR_CPUS
; i
++) {
556 if (i
== cpu
|| !cpu_online(i
))
558 ret
= kernel_thread(stopref
, (void *)(long)i
, CLONE_KERNEL
);
561 stopref_num_threads
++;
564 /* Wait for them all to come to life. */
565 while (atomic_read(&stopref_thread_ack
) != stopref_num_threads
)
568 /* If some failed, kill them all. */
570 stopref_set_state(STOPREF_EXIT
, 1);
575 /* Don't schedule us away at this point, please. */
578 /* Now they are all scheduled, make them hold the CPUs, ready. */
579 stopref_set_state(STOPREF_PREPARE
, 0);
581 /* Make them disable irqs. */
582 stopref_set_state(STOPREF_DISABLE_IRQ
, 0);
588 /* Restart the machine. Re-enables irqs. */
589 static void restart_refcounts(void)
591 stopref_set_state(STOPREF_EXIT
, 0);
597 static inline int stop_refcounts(void)
602 static inline void restart_refcounts(void)
608 unsigned int module_refcount(struct module
*mod
)
610 unsigned int i
, total
= 0;
612 for (i
= 0; i
< NR_CPUS
; i
++)
613 total
+= local_read(&mod
->ref
[i
].count
);
616 EXPORT_SYMBOL(module_refcount
);
618 /* This exists whether we can unload or not */
619 static void free_module(struct module
*mod
);
621 #ifdef CONFIG_MODULE_FORCE_UNLOAD
622 static inline int try_force(unsigned int flags
)
624 int ret
= (flags
& O_TRUNC
);
626 tainted
|= TAINT_FORCED_MODULE
;
630 static inline int try_force(unsigned int flags
)
634 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
636 /* Stub function for modules which don't have an exitfn */
637 void cleanup_module(void)
640 EXPORT_SYMBOL(cleanup_module
);
642 static void wait_for_zero_refcount(struct module
*mod
)
644 /* Since we might sleep for some time, drop the semaphore first */
647 DEBUGP("Looking at refcount...\n");
648 set_current_state(TASK_UNINTERRUPTIBLE
);
649 if (module_refcount(mod
) == 0)
653 current
->state
= TASK_RUNNING
;
658 sys_delete_module(const char __user
*name_user
, unsigned int flags
)
661 char name
[MODULE_NAME_LEN
];
664 if (!capable(CAP_SYS_MODULE
))
667 if (strncpy_from_user(name
, name_user
, MODULE_NAME_LEN
-1) < 0)
669 name
[MODULE_NAME_LEN
-1] = '\0';
671 if (down_interruptible(&module_mutex
) != 0)
674 mod
= find_module(name
);
680 if (!list_empty(&mod
->modules_which_use_me
)) {
681 /* Other modules depend on us: get rid of them first. */
687 if (mod
->state
== MODULE_STATE_GOING
) {
688 /* FIXME: if (force), slam module count and wake up
690 DEBUGP("%s already dying\n", mod
->name
);
695 /* If it has an init func, it must have an exit func to unload */
696 if ((mod
->init
!= init_module
&& mod
->exit
== cleanup_module
)
698 forced
= try_force(flags
);
700 /* This module can't be removed */
705 /* Stop the machine so refcounts can't move: irqs disabled. */
706 DEBUGP("Stopping refcounts...\n");
707 ret
= stop_refcounts();
711 /* If it's not unused, quit unless we are told to block. */
712 if ((flags
& O_NONBLOCK
) && module_refcount(mod
) != 0) {
713 forced
= try_force(flags
);
721 /* Mark it as dying. */
722 mod
->waiter
= current
;
723 mod
->state
= MODULE_STATE_GOING
;
726 /* Never wait if forced. */
727 if (!forced
&& module_refcount(mod
) != 0)
728 wait_for_zero_refcount(mod
);
730 /* Final destruction now noone is using it. */
739 static void print_unload_info(struct seq_file
*m
, struct module
*mod
)
741 struct module_use
*use
;
742 int printed_something
= 0;
744 seq_printf(m
, " %u ", module_refcount(mod
));
746 /* Always include a trailing , so userspace can differentiate
747 between this and the old multi-field proc format. */
748 list_for_each_entry(use
, &mod
->modules_which_use_me
, list
) {
749 printed_something
= 1;
750 seq_printf(m
, "%s,", use
->module_which_uses
->name
);
754 printed_something
= 1;
755 seq_printf(m
, "[unsafe],");
758 if (mod
->init
!= init_module
&& mod
->exit
== cleanup_module
) {
759 printed_something
= 1;
760 seq_printf(m
, "[permanent],");
763 if (!printed_something
)
767 void __symbol_put(const char *symbol
)
769 struct module
*owner
;
771 const unsigned long *crc
;
773 spin_lock_irqsave(&modlist_lock
, flags
);
774 if (!__find_symbol(symbol
, &owner
, &crc
, 1))
777 spin_unlock_irqrestore(&modlist_lock
, flags
);
779 EXPORT_SYMBOL(__symbol_put
);
781 void symbol_put_addr(void *addr
)
785 spin_lock_irqsave(&modlist_lock
, flags
);
786 if (!kernel_text_address((unsigned long)addr
))
789 module_put(module_text_address((unsigned long)addr
));
790 spin_unlock_irqrestore(&modlist_lock
, flags
);
792 EXPORT_SYMBOL_GPL(symbol_put_addr
);
794 #else /* !CONFIG_MODULE_UNLOAD */
795 static void print_unload_info(struct seq_file
*m
, struct module
*mod
)
797 /* We don't know the usage count, or what modules are using. */
798 seq_printf(m
, " - -");
801 static inline void module_unload_free(struct module
*mod
)
805 static inline int use_module(struct module
*a
, struct module
*b
)
807 return strong_try_module_get(b
);
810 static inline void module_unload_init(struct module
*mod
)
815 sys_delete_module(const char *name_user
, unsigned int flags
)
820 #endif /* CONFIG_MODULE_UNLOAD */
822 #ifdef CONFIG_OBSOLETE_MODPARM
823 static int param_set_byte(const char *val
, struct kernel_param
*kp
)
828 if (!val
) return -EINVAL
;
829 l
= simple_strtol(val
, &endp
, 0);
830 if (endp
== val
|| *endp
|| ((char)l
!= l
))
832 *((char *)kp
->arg
) = l
;
836 /* Bounds checking done below */
837 static int obsparm_copy_string(const char *val
, struct kernel_param
*kp
)
839 strcpy(kp
->arg
, val
);
843 int set_obsolete(const char *val
, struct kernel_param
*kp
)
845 unsigned int min
, max
;
846 unsigned int size
, maxsize
;
849 struct obsolete_modparm
*obsparm
= kp
->arg
;
852 printk(KERN_ERR
"Parameter %s needs an argument\n", kp
->name
);
856 /* type is: [min[-max]]{b,h,i,l,s} */
858 min
= simple_strtol(p
, &endp
, 10);
859 if (endp
== obsparm
->type
)
861 else if (*endp
== '-') {
863 max
= simple_strtol(p
, &endp
, 10);
868 return param_array(kp
->name
, val
, min
, max
, obsparm
->addr
,
871 return param_array(kp
->name
, val
, min
, max
, obsparm
->addr
,
872 sizeof(short), param_set_short
);
874 return param_array(kp
->name
, val
, min
, max
, obsparm
->addr
,
875 sizeof(int), param_set_int
);
877 return param_array(kp
->name
, val
, min
, max
, obsparm
->addr
,
878 sizeof(long), param_set_long
);
880 return param_array(kp
->name
, val
, min
, max
, obsparm
->addr
,
881 sizeof(char *), param_set_charp
);
884 /* Undocumented: 1-5c50 means 1-5 strings of up to 49 chars,
885 and the decl is "char xxx[5][50];" */
887 maxsize
= simple_strtol(p
, &endp
, 10);
888 /* We check lengths here (yes, this is a hack). */
890 while (p
[size
= strcspn(p
, ",")]) {
897 return param_array(kp
->name
, val
, min
, max
, obsparm
->addr
,
898 maxsize
, obsparm_copy_string
);
900 printk(KERN_ERR
"Unknown obsolete parameter type %s\n", obsparm
->type
);
904 "Parameter %s doesn't fit in %u chars.\n", kp
->name
, maxsize
);
908 static int obsolete_params(const char *name
,
910 struct obsolete_modparm obsparm
[],
913 unsigned int symindex
,
916 struct kernel_param
*kp
;
920 kp
= kmalloc(sizeof(kp
[0]) * num
, GFP_KERNEL
);
924 for (i
= 0; i
< num
; i
++) {
925 char sym_name
[128 + sizeof(MODULE_SYMBOL_PREFIX
)];
927 snprintf(sym_name
, sizeof(sym_name
), "%s%s",
928 MODULE_SYMBOL_PREFIX
, obsparm
[i
].name
);
930 kp
[i
].name
= obsparm
[i
].name
;
932 kp
[i
].set
= set_obsolete
;
935 = (void *)find_local_symbol(sechdrs
, symindex
, strtab
,
937 if (!obsparm
[i
].addr
) {
938 printk("%s: falsely claims to have parameter %s\n",
939 name
, obsparm
[i
].name
);
943 kp
[i
].arg
= &obsparm
[i
];
946 ret
= parse_args(name
, args
, kp
, num
, NULL
);
952 static int obsolete_params(const char *name
,
954 struct obsolete_modparm obsparm
[],
957 unsigned int symindex
,
961 printk(KERN_WARNING
"%s: Ignoring obsolete parameters\n",
965 #endif /* CONFIG_OBSOLETE_MODPARM */
967 static const char vermagic
[] = VERMAGIC_STRING
;
969 #ifdef CONFIG_MODVERSIONS
970 static int check_version(Elf_Shdr
*sechdrs
,
971 unsigned int versindex
,
974 const unsigned long *crc
)
976 unsigned int i
, num_versions
;
977 struct modversion_info
*versions
;
979 /* Exporting module didn't supply crcs? OK, we're already tainted. */
983 versions
= (void *) sechdrs
[versindex
].sh_addr
;
984 num_versions
= sechdrs
[versindex
].sh_size
985 / sizeof(struct modversion_info
);
987 for (i
= 0; i
< num_versions
; i
++) {
988 if (strcmp(versions
[i
].name
, symname
) != 0)
991 if (versions
[i
].crc
== *crc
)
993 printk("%s: disagrees about version of symbol %s\n",
995 DEBUGP("Found checksum %lX vs module %lX\n",
996 *crc
, versions
[i
].crc
);
999 /* Not in module's version table. OK, but that taints the kernel. */
1000 if (!(tainted
& TAINT_FORCED_MODULE
)) {
1001 printk("%s: no version for \"%s\" found: kernel tainted.\n",
1002 mod
->name
, symname
);
1003 tainted
|= TAINT_FORCED_MODULE
;
1008 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1009 unsigned int versindex
,
1012 const unsigned long *crc
;
1013 struct module
*owner
;
1015 if (!__find_symbol("struct_module", &owner
, &crc
, 1))
1017 return check_version(sechdrs
, versindex
, "struct_module", mod
,
1021 /* First part is kernel version, which we ignore. */
1022 static inline int same_magic(const char *amagic
, const char *bmagic
)
1024 amagic
+= strcspn(amagic
, " ");
1025 bmagic
+= strcspn(bmagic
, " ");
1026 return strcmp(amagic
, bmagic
) == 0;
1029 static inline int check_version(Elf_Shdr
*sechdrs
,
1030 unsigned int versindex
,
1031 const char *symname
,
1033 const unsigned long *crc
)
1038 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1039 unsigned int versindex
,
1045 static inline int same_magic(const char *amagic
, const char *bmagic
)
1047 return strcmp(amagic
, bmagic
) == 0;
1049 #endif /* CONFIG_MODVERSIONS */
1051 /* Resolve a symbol for this module. I.e. if we find one, record usage.
1052 Must be holding module_mutex. */
1053 static unsigned long resolve_symbol(Elf_Shdr
*sechdrs
,
1054 unsigned int versindex
,
1058 struct module
*owner
;
1060 const unsigned long *crc
;
1062 spin_lock_irq(&modlist_lock
);
1063 ret
= __find_symbol(name
, &owner
, &crc
, mod
->license_gplok
);
1065 /* use_module can fail due to OOM, or module unloading */
1066 if (!check_version(sechdrs
, versindex
, name
, mod
, crc
) ||
1067 !use_module(mod
, owner
))
1070 spin_unlock_irq(&modlist_lock
);
1074 /* Free a module, remove from lists, etc (must hold module mutex). */
1075 static void free_module(struct module
*mod
)
1077 /* Delete from various lists */
1078 spin_lock_irq(&modlist_lock
);
1079 list_del(&mod
->list
);
1080 spin_unlock_irq(&modlist_lock
);
1082 /* Arch-specific cleanup. */
1083 module_arch_cleanup(mod
);
1085 /* Module unload stuff */
1086 module_unload_free(mod
);
1088 /* This may be NULL, but that's OK */
1089 module_free(mod
, mod
->module_init
);
1092 percpu_modfree(mod
->percpu
);
1094 /* Finally, free the core (containing the module structure) */
1095 module_free(mod
, mod
->module_core
);
1098 void *__symbol_get(const char *symbol
)
1100 struct module
*owner
;
1101 unsigned long value
, flags
;
1102 const unsigned long *crc
;
1104 spin_lock_irqsave(&modlist_lock
, flags
);
1105 value
= __find_symbol(symbol
, &owner
, &crc
, 1);
1106 if (value
&& !strong_try_module_get(owner
))
1108 spin_unlock_irqrestore(&modlist_lock
, flags
);
1110 return (void *)value
;
1112 EXPORT_SYMBOL_GPL(__symbol_get
);
1114 /* Change all symbols so that sh_value encodes the pointer directly. */
1115 static int simplify_symbols(Elf_Shdr
*sechdrs
,
1116 unsigned int symindex
,
1118 unsigned int versindex
,
1119 unsigned int pcpuindex
,
1122 Elf_Sym
*sym
= (void *)sechdrs
[symindex
].sh_addr
;
1123 unsigned long secbase
;
1124 unsigned int i
, n
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1127 for (i
= 1; i
< n
; i
++) {
1128 switch (sym
[i
].st_shndx
) {
1130 /* We compiled with -fno-common. These are not
1131 supposed to happen. */
1132 DEBUGP("Common symbol: %s\n", strtab
+ sym
[i
].st_name
);
1137 /* Don't need to do anything */
1138 DEBUGP("Absolute symbol: 0x%08lx\n",
1139 (long)sym
[i
].st_value
);
1144 = resolve_symbol(sechdrs
, versindex
,
1145 strtab
+ sym
[i
].st_name
, mod
);
1147 /* Ok if resolved. */
1148 if (sym
[i
].st_value
!= 0)
1151 if (ELF_ST_BIND(sym
[i
].st_info
) == STB_WEAK
)
1154 printk(KERN_WARNING
"%s: Unknown symbol %s\n",
1155 mod
->name
, strtab
+ sym
[i
].st_name
);
1160 /* Divert to percpu allocation if a percpu var. */
1161 if (sym
[i
].st_shndx
== pcpuindex
)
1162 secbase
= (unsigned long)mod
->percpu
;
1164 secbase
= sechdrs
[sym
[i
].st_shndx
].sh_addr
;
1165 sym
[i
].st_value
+= secbase
;
1173 /* Update size with this section: return offset. */
1174 static long get_offset(unsigned long *size
, Elf_Shdr
*sechdr
)
1178 ret
= ALIGN(*size
, sechdr
->sh_addralign
?: 1);
1179 *size
= ret
+ sechdr
->sh_size
;
1183 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1184 might -- code, read-only data, read-write data, small data. Tally
1185 sizes, and place the offsets into sh_entsize fields: high bit means it
1187 static void layout_sections(struct module
*mod
,
1188 const Elf_Ehdr
*hdr
,
1190 const char *secstrings
)
1192 static unsigned long const masks
[][2] = {
1193 /* NOTE: all executable code must be the first section
1194 * in this array; otherwise modify the text_size
1195 * finder in the two loops below */
1196 { SHF_EXECINSTR
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1197 { SHF_ALLOC
, SHF_WRITE
| ARCH_SHF_SMALL
},
1198 { SHF_WRITE
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1199 { ARCH_SHF_SMALL
| SHF_ALLOC
, 0 }
1203 for (i
= 0; i
< hdr
->e_shnum
; i
++)
1204 sechdrs
[i
].sh_entsize
= ~0UL;
1206 DEBUGP("Core section allocation order:\n");
1207 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1208 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1209 Elf_Shdr
*s
= &sechdrs
[i
];
1211 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1212 || (s
->sh_flags
& masks
[m
][1])
1213 || s
->sh_entsize
!= ~0UL
1214 || strstr(secstrings
+ s
->sh_name
, ".init"))
1216 s
->sh_entsize
= get_offset(&mod
->core_size
, s
);
1217 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1220 mod
->core_text_size
= mod
->core_size
;
1223 DEBUGP("Init section allocation order:\n");
1224 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1225 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1226 Elf_Shdr
*s
= &sechdrs
[i
];
1228 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1229 || (s
->sh_flags
& masks
[m
][1])
1230 || s
->sh_entsize
!= ~0UL
1231 || !strstr(secstrings
+ s
->sh_name
, ".init"))
1233 s
->sh_entsize
= (get_offset(&mod
->init_size
, s
)
1234 | INIT_OFFSET_MASK
);
1235 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1238 mod
->init_text_size
= mod
->init_size
;
1242 static inline int license_is_gpl_compatible(const char *license
)
1244 return (strcmp(license
, "GPL") == 0
1245 || strcmp(license
, "GPL v2") == 0
1246 || strcmp(license
, "GPL and additional rights") == 0
1247 || strcmp(license
, "Dual BSD/GPL") == 0
1248 || strcmp(license
, "Dual MPL/GPL") == 0);
1251 static void set_license(struct module
*mod
, const char *license
)
1254 license
= "unspecified";
1256 mod
->license_gplok
= license_is_gpl_compatible(license
);
1257 if (!mod
->license_gplok
) {
1258 printk(KERN_WARNING
"%s: module license '%s' taints kernel.\n",
1259 mod
->name
, license
);
1260 tainted
|= TAINT_PROPRIETARY_MODULE
;
1264 /* Parse tag=value strings from .modinfo section */
1265 static char *next_string(char *string
, unsigned long *secsize
)
1267 /* Skip non-zero chars */
1270 if ((*secsize
)-- <= 1)
1274 /* Skip any zero padding. */
1275 while (!string
[0]) {
1277 if ((*secsize
)-- <= 1)
1283 static char *get_modinfo(Elf_Shdr
*sechdrs
,
1288 unsigned int taglen
= strlen(tag
);
1289 unsigned long size
= sechdrs
[info
].sh_size
;
1291 for (p
= (char *)sechdrs
[info
].sh_addr
; p
; p
= next_string(p
, &size
)) {
1292 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
1293 return p
+ taglen
+ 1;
1298 #ifdef CONFIG_KALLSYMS
1299 int is_exported(const char *name
, const struct module
*mod
)
1304 for (i
= 0; __start___ksymtab
+i
< __stop___ksymtab
; i
++)
1305 if (strcmp(__start___ksymtab
[i
].name
, name
) == 0)
1309 for (i
= 0; i
< mod
->num_syms
; i
++)
1310 if (strcmp(mod
->syms
[i
].name
, name
) == 0)
1316 static char elf_type(const Elf_Sym
*sym
,
1318 const char *secstrings
,
1321 if (ELF_ST_BIND(sym
->st_info
) == STB_WEAK
) {
1322 if (ELF_ST_TYPE(sym
->st_info
) == STT_OBJECT
)
1327 if (sym
->st_shndx
== SHN_UNDEF
)
1329 if (sym
->st_shndx
== SHN_ABS
)
1331 if (sym
->st_shndx
>= SHN_LORESERVE
)
1333 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_EXECINSTR
)
1335 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_ALLOC
1336 && sechdrs
[sym
->st_shndx
].sh_type
!= SHT_NOBITS
) {
1337 if (!(sechdrs
[sym
->st_shndx
].sh_flags
& SHF_WRITE
))
1339 else if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1344 if (sechdrs
[sym
->st_shndx
].sh_type
== SHT_NOBITS
) {
1345 if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1350 if (strncmp(secstrings
+ sechdrs
[sym
->st_shndx
].sh_name
,
1351 ".debug", strlen(".debug")) == 0)
1356 static void add_kallsyms(struct module
*mod
,
1358 unsigned int symindex
,
1359 unsigned int strindex
,
1360 const char *secstrings
)
1364 mod
->symtab
= (void *)sechdrs
[symindex
].sh_addr
;
1365 mod
->num_symtab
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1366 mod
->strtab
= (void *)sechdrs
[strindex
].sh_addr
;
1368 /* Set types up while we still have access to sections. */
1369 for (i
= 0; i
< mod
->num_symtab
; i
++)
1370 mod
->symtab
[i
].st_info
1371 = elf_type(&mod
->symtab
[i
], sechdrs
, secstrings
, mod
);
1375 /* Allocate and load the module: note that size of section 0 is always
1376 zero, and we rely on this for optional sections. */
1377 static struct module
*load_module(void __user
*umod
,
1379 const char __user
*uargs
)
1383 char *secstrings
, *args
, *modmagic
, *strtab
= NULL
;
1384 unsigned int i
, symindex
= 0, strindex
= 0, setupindex
, exindex
,
1385 exportindex
, modindex
, obsparmindex
, infoindex
, gplindex
,
1386 crcindex
, gplcrcindex
, versindex
, pcpuindex
;
1390 void *percpu
= NULL
, *ptr
= NULL
; /* Stops spurious gcc warning */
1392 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1394 if (len
< sizeof(*hdr
))
1395 return ERR_PTR(-ENOEXEC
);
1397 /* Suck in entire file: we'll want most of it. */
1398 /* vmalloc barfs on "unusual" numbers. Check here */
1399 if (len
> 64 * 1024 * 1024 || (hdr
= vmalloc(len
)) == NULL
)
1400 return ERR_PTR(-ENOMEM
);
1401 if (copy_from_user(hdr
, umod
, len
) != 0) {
1406 /* Sanity checks against insmoding binaries or wrong arch,
1407 weird elf version */
1408 if (memcmp(hdr
->e_ident
, ELFMAG
, 4) != 0
1409 || hdr
->e_type
!= ET_REL
1410 || !elf_check_arch(hdr
)
1411 || hdr
->e_shentsize
!= sizeof(*sechdrs
)) {
1416 /* Convenience variables */
1417 sechdrs
= (void *)hdr
+ hdr
->e_shoff
;
1418 secstrings
= (void *)hdr
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
1419 sechdrs
[0].sh_addr
= 0;
1421 /* And these should exist, but gcc whinges if we don't init them */
1422 symindex
= strindex
= 0;
1424 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
1425 /* Mark all sections sh_addr with their address in the
1427 sechdrs
[i
].sh_addr
= (size_t)hdr
+ sechdrs
[i
].sh_offset
;
1429 /* Internal symbols and strings. */
1430 if (sechdrs
[i
].sh_type
== SHT_SYMTAB
) {
1432 strindex
= sechdrs
[i
].sh_link
;
1433 strtab
= (char *)hdr
+ sechdrs
[strindex
].sh_offset
;
1435 #ifndef CONFIG_MODULE_UNLOAD
1436 /* Don't load .exit sections */
1437 if (strstr(secstrings
+sechdrs
[i
].sh_name
, ".exit"))
1438 sechdrs
[i
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1442 modindex
= find_sec(hdr
, sechdrs
, secstrings
,
1443 ".gnu.linkonce.this_module");
1445 printk(KERN_WARNING
"No module found in object\n");
1449 mod
= (void *)sechdrs
[modindex
].sh_addr
;
1451 /* Optional sections */
1452 exportindex
= find_sec(hdr
, sechdrs
, secstrings
, "__ksymtab");
1453 gplindex
= find_sec(hdr
, sechdrs
, secstrings
, "__ksymtab_gpl");
1454 crcindex
= find_sec(hdr
, sechdrs
, secstrings
, "__kcrctab");
1455 gplcrcindex
= find_sec(hdr
, sechdrs
, secstrings
, "__kcrctab_gpl");
1456 setupindex
= find_sec(hdr
, sechdrs
, secstrings
, "__param");
1457 exindex
= find_sec(hdr
, sechdrs
, secstrings
, "__ex_table");
1458 obsparmindex
= find_sec(hdr
, sechdrs
, secstrings
, "__obsparm");
1459 versindex
= find_sec(hdr
, sechdrs
, secstrings
, "__versions");
1460 infoindex
= find_sec(hdr
, sechdrs
, secstrings
, ".modinfo");
1461 pcpuindex
= find_pcpusec(hdr
, sechdrs
, secstrings
);
1463 /* Don't keep modinfo section */
1464 sechdrs
[infoindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1465 #ifdef CONFIG_KALLSYMS
1466 /* Keep symbol and string tables for decoding later. */
1467 sechdrs
[symindex
].sh_flags
|= SHF_ALLOC
;
1468 sechdrs
[strindex
].sh_flags
|= SHF_ALLOC
;
1471 /* Check module struct version now, before we try to use module. */
1472 if (!check_modstruct_version(sechdrs
, versindex
, mod
)) {
1477 modmagic
= get_modinfo(sechdrs
, infoindex
, "vermagic");
1478 /* This is allowed: modprobe --force will invalidate it. */
1480 tainted
|= TAINT_FORCED_MODULE
;
1481 printk(KERN_WARNING
"%s: no version magic, tainting kernel.\n",
1483 } else if (!same_magic(modmagic
, vermagic
)) {
1484 printk(KERN_ERR
"%s: version magic '%s' should be '%s'\n",
1485 mod
->name
, modmagic
, vermagic
);
1490 /* Now copy in args */
1491 arglen
= strlen_user(uargs
);
1496 args
= kmalloc(arglen
, GFP_KERNEL
);
1501 if (copy_from_user(args
, uargs
, arglen
) != 0) {
1506 if (find_module(mod
->name
)) {
1511 mod
->state
= MODULE_STATE_COMING
;
1513 /* Allow arches to frob section contents and sizes. */
1514 err
= module_frob_arch_sections(hdr
, sechdrs
, secstrings
, mod
);
1519 /* We have a special allocation for this section. */
1520 percpu
= percpu_modalloc(sechdrs
[pcpuindex
].sh_size
,
1521 sechdrs
[pcpuindex
].sh_addralign
);
1526 sechdrs
[pcpuindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1527 mod
->percpu
= percpu
;
1530 /* Determine total sizes, and put offsets in sh_entsize. For now
1531 this is done generically; there doesn't appear to be any
1532 special cases for the architectures. */
1533 layout_sections(mod
, hdr
, sechdrs
, secstrings
);
1535 /* Do the allocs. */
1536 ptr
= module_alloc(mod
->core_size
);
1541 memset(ptr
, 0, mod
->core_size
);
1542 mod
->module_core
= ptr
;
1544 ptr
= module_alloc(mod
->init_size
);
1545 if (!ptr
&& mod
->init_size
) {
1549 memset(ptr
, 0, mod
->init_size
);
1550 mod
->module_init
= ptr
;
1552 /* Transfer each section which specifies SHF_ALLOC */
1553 DEBUGP("final section addresses:\n");
1554 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
1557 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
1560 if (sechdrs
[i
].sh_entsize
& INIT_OFFSET_MASK
)
1561 dest
= mod
->module_init
1562 + (sechdrs
[i
].sh_entsize
& ~INIT_OFFSET_MASK
);
1564 dest
= mod
->module_core
+ sechdrs
[i
].sh_entsize
;
1566 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
)
1567 memcpy(dest
, (void *)sechdrs
[i
].sh_addr
,
1568 sechdrs
[i
].sh_size
);
1569 /* Update sh_addr to point to copy in image. */
1570 sechdrs
[i
].sh_addr
= (unsigned long)dest
;
1571 DEBUGP("\t0x%lx %s\n", sechdrs
[i
].sh_addr
, secstrings
+ sechdrs
[i
].sh_name
);
1573 /* Module has been moved. */
1574 mod
= (void *)sechdrs
[modindex
].sh_addr
;
1576 /* Now we've moved module, initialize linked lists, etc. */
1577 module_unload_init(mod
);
1579 /* Set up license info based on the info section */
1580 set_license(mod
, get_modinfo(sechdrs
, infoindex
, "license"));
1582 /* Fix up syms, so that st_value is a pointer to location. */
1583 err
= simplify_symbols(sechdrs
, symindex
, strtab
, versindex
, pcpuindex
,
1588 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1589 mod
->num_syms
= sechdrs
[exportindex
].sh_size
/ sizeof(*mod
->syms
);
1590 mod
->syms
= (void *)sechdrs
[exportindex
].sh_addr
;
1592 mod
->crcs
= (void *)sechdrs
[crcindex
].sh_addr
;
1593 mod
->num_gpl_syms
= sechdrs
[gplindex
].sh_size
/ sizeof(*mod
->gpl_syms
);
1594 mod
->gpl_syms
= (void *)sechdrs
[gplindex
].sh_addr
;
1596 mod
->gpl_crcs
= (void *)sechdrs
[gplcrcindex
].sh_addr
;
1598 #ifdef CONFIG_MODVERSIONS
1599 if ((mod
->num_syms
&& !crcindex
) ||
1600 (mod
->num_gpl_syms
&& !gplcrcindex
)) {
1601 printk(KERN_WARNING
"%s: No versions for exported symbols."
1602 " Tainting kernel.\n", mod
->name
);
1603 tainted
|= TAINT_FORCED_MODULE
;
1607 /* Set up exception table */
1608 mod
->num_exentries
= sechdrs
[exindex
].sh_size
/ sizeof(*mod
->extable
);
1609 mod
->extable
= (void *)sechdrs
[exindex
].sh_addr
;
1611 /* Now do relocations. */
1612 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
1613 const char *strtab
= (char *)sechdrs
[strindex
].sh_addr
;
1614 if (sechdrs
[i
].sh_type
== SHT_REL
)
1615 err
= apply_relocate(sechdrs
, strtab
, symindex
, i
,mod
);
1616 else if (sechdrs
[i
].sh_type
== SHT_RELA
)
1617 err
= apply_relocate_add(sechdrs
, strtab
, symindex
, i
,
1623 /* Finally, copy percpu area over. */
1624 percpu_modcopy(mod
->percpu
, (void *)sechdrs
[pcpuindex
].sh_addr
,
1625 sechdrs
[pcpuindex
].sh_size
);
1627 err
= module_finalize(hdr
, sechdrs
, mod
);
1631 #ifdef CONFIG_KALLSYMS
1632 add_kallsyms(mod
, sechdrs
, symindex
, strindex
, secstrings
);
1637 err
= obsolete_params(mod
->name
, mod
->args
,
1638 (struct obsolete_modparm
*)
1639 sechdrs
[obsparmindex
].sh_addr
,
1640 sechdrs
[obsparmindex
].sh_size
1641 / sizeof(struct obsolete_modparm
),
1643 (char *)sechdrs
[strindex
].sh_addr
);
1645 /* Size of section 0 is 0, so this works well if no params */
1646 err
= parse_args(mod
->name
, mod
->args
,
1647 (struct kernel_param
*)
1648 sechdrs
[setupindex
].sh_addr
,
1649 sechdrs
[setupindex
].sh_size
1650 / sizeof(struct kernel_param
),
1656 /* Get rid of temporary copy */
1663 module_unload_free(mod
);
1664 module_free(mod
, mod
->module_init
);
1666 module_free(mod
, mod
->module_core
);
1669 percpu_modfree(percpu
);
1674 if (err
< 0) return ERR_PTR(err
);
1678 /* This is where the real work happens */
1680 sys_init_module(void __user
*umod
,
1682 const char __user
*uargs
)
1687 /* Must have permission */
1688 if (!capable(CAP_SYS_MODULE
))
1691 /* Only one module load at a time, please */
1692 if (down_interruptible(&module_mutex
) != 0)
1695 /* Do all the hard work */
1696 mod
= load_module(umod
, len
, uargs
);
1699 return PTR_ERR(mod
);
1702 /* Flush the instruction cache, since we've played with text */
1703 if (mod
->module_init
)
1704 flush_icache_range((unsigned long)mod
->module_init
,
1705 (unsigned long)mod
->module_init
1707 flush_icache_range((unsigned long)mod
->module_core
,
1708 (unsigned long)mod
->module_core
+ mod
->core_size
);
1710 /* Now sew it into the lists. They won't access us, since
1711 strong_try_module_get() will fail. */
1712 spin_lock_irq(&modlist_lock
);
1713 list_add(&mod
->list
, &modules
);
1714 spin_unlock_irq(&modlist_lock
);
1716 /* Drop lock so they can recurse */
1719 down(¬ify_mutex
);
1720 notifier_call_chain(&module_notify_list
, MODULE_STATE_COMING
, mod
);
1723 /* Start the module */
1726 /* Init routine failed: abort. Try to protect us from
1727 buggy refcounters. */
1728 mod
->state
= MODULE_STATE_GOING
;
1729 synchronize_kernel();
1731 printk(KERN_ERR
"%s: module is now stuck!\n",
1735 down(&module_mutex
);
1742 /* Now it's a first class citizen! */
1743 down(&module_mutex
);
1744 mod
->state
= MODULE_STATE_LIVE
;
1745 /* Drop initial reference. */
1747 module_free(mod
, mod
->module_init
);
1748 mod
->module_init
= NULL
;
1750 mod
->init_text_size
= 0;
1756 static inline int within(unsigned long addr
, void *start
, unsigned long size
)
1758 return ((void *)addr
>= start
&& (void *)addr
< start
+ size
);
1761 #ifdef CONFIG_KALLSYMS
1762 static const char *get_ksymbol(struct module
*mod
,
1764 unsigned long *size
,
1765 unsigned long *offset
)
1767 unsigned int i
, best
= 0;
1768 unsigned long nextval
;
1770 /* At worse, next value is at end of module */
1771 if (within(addr
, mod
->module_init
, mod
->init_size
))
1772 nextval
= (unsigned long)mod
->module_init
+mod
->init_text_size
;
1774 nextval
= (unsigned long)mod
->module_core
+mod
->core_text_size
;
1776 /* Scan for closest preceeding symbol, and next symbol. (ELF
1777 starts real symbols at 1). */
1778 for (i
= 1; i
< mod
->num_symtab
; i
++) {
1779 if (mod
->symtab
[i
].st_shndx
== SHN_UNDEF
)
1782 /* We ignore unnamed symbols: they're uninformative
1783 * and inserted at a whim. */
1784 if (mod
->symtab
[i
].st_value
<= addr
1785 && mod
->symtab
[i
].st_value
> mod
->symtab
[best
].st_value
1786 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0' )
1788 if (mod
->symtab
[i
].st_value
> addr
1789 && mod
->symtab
[i
].st_value
< nextval
1790 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0')
1791 nextval
= mod
->symtab
[i
].st_value
;
1797 *size
= nextval
- mod
->symtab
[best
].st_value
;
1798 *offset
= addr
- mod
->symtab
[best
].st_value
;
1799 return mod
->strtab
+ mod
->symtab
[best
].st_name
;
1802 /* For kallsyms to ask for address resolution. NULL means not found.
1803 We don't lock, as this is used for oops resolution and races are a
1805 const char *module_address_lookup(unsigned long addr
,
1806 unsigned long *size
,
1807 unsigned long *offset
,
1812 list_for_each_entry(mod
, &modules
, list
) {
1813 if (within(addr
, mod
->module_init
, mod
->init_size
)
1814 || within(addr
, mod
->module_core
, mod
->core_size
)) {
1815 *modname
= mod
->name
;
1816 return get_ksymbol(mod
, addr
, size
, offset
);
1822 struct module
*module_get_kallsym(unsigned int symnum
,
1823 unsigned long *value
,
1829 down(&module_mutex
);
1830 list_for_each_entry(mod
, &modules
, list
) {
1831 if (symnum
< mod
->num_symtab
) {
1832 *value
= mod
->symtab
[symnum
].st_value
;
1833 *type
= mod
->symtab
[symnum
].st_info
;
1835 mod
->strtab
+ mod
->symtab
[symnum
].st_name
,
1840 symnum
-= mod
->num_symtab
;
1845 #endif /* CONFIG_KALLSYMS */
1847 /* Called by the /proc file system to return a list of modules. */
1848 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
1850 struct list_head
*i
;
1853 down(&module_mutex
);
1854 list_for_each(i
, &modules
) {
1863 static void *m_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
1865 struct list_head
*i
= p
;
1867 if (i
->next
== &modules
)
1872 static void m_stop(struct seq_file
*m
, void *p
)
1877 static int m_show(struct seq_file
*m
, void *p
)
1879 struct module
*mod
= list_entry(p
, struct module
, list
);
1880 seq_printf(m
, "%s %lu",
1881 mod
->name
, mod
->init_size
+ mod
->core_size
);
1882 print_unload_info(m
, mod
);
1884 /* Informative for users. */
1885 seq_printf(m
, " %s",
1886 mod
->state
== MODULE_STATE_GOING
? "Unloading":
1887 mod
->state
== MODULE_STATE_COMING
? "Loading":
1889 /* Used by oprofile and other similar tools. */
1890 seq_printf(m
, " 0x%p", mod
->module_core
);
1892 seq_printf(m
, "\n");
1896 /* Format: modulename size refcount deps address
1898 Where refcount is a number or -, and deps is a comma-separated list
1901 struct seq_operations modules_op
= {
1908 /* Given an address, look for it in the module exception tables. */
1909 const struct exception_table_entry
*search_module_extables(unsigned long addr
)
1911 unsigned long flags
;
1912 const struct exception_table_entry
*e
= NULL
;
1915 spin_lock_irqsave(&modlist_lock
, flags
);
1916 list_for_each_entry(mod
, &modules
, list
) {
1917 if (mod
->num_exentries
== 0)
1920 e
= search_extable(mod
->extable
,
1921 mod
->extable
+ mod
->num_exentries
- 1,
1926 spin_unlock_irqrestore(&modlist_lock
, flags
);
1928 /* Now, if we found one, we are running inside it now, hence
1929 we cannot unload the module, hence no refcnt needed. */
1933 /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
1934 struct module
*module_text_address(unsigned long addr
)
1938 list_for_each_entry(mod
, &modules
, list
)
1939 if (within(addr
, mod
->module_init
, mod
->init_text_size
)
1940 || within(addr
, mod
->module_core
, mod
->core_text_size
))
1945 #ifdef CONFIG_MODVERSIONS
1946 /* Generate the signature for struct module here, too, for modversions. */
1947 void struct_module(struct module
*mod
) { return; }
1948 EXPORT_SYMBOL(struct_module
);