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/module.h>
20 #include <linux/moduleloader.h>
21 #include <linux/ftrace_event.h>
22 #include <linux/init.h>
23 #include <linux/kallsyms.h>
25 #include <linux/sysfs.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/vmalloc.h>
29 #include <linux/elf.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <linux/syscalls.h>
33 #include <linux/fcntl.h>
34 #include <linux/rcupdate.h>
35 #include <linux/capability.h>
36 #include <linux/cpu.h>
37 #include <linux/moduleparam.h>
38 #include <linux/errno.h>
39 #include <linux/err.h>
40 #include <linux/vermagic.h>
41 #include <linux/notifier.h>
42 #include <linux/sched.h>
43 #include <linux/stop_machine.h>
44 #include <linux/device.h>
45 #include <linux/string.h>
46 #include <linux/mutex.h>
47 #include <linux/rculist.h>
48 #include <asm/uaccess.h>
49 #include <asm/cacheflush.h>
50 #include <linux/license.h>
51 #include <asm/sections.h>
52 #include <linux/tracepoint.h>
53 #include <linux/ftrace.h>
54 #include <linux/async.h>
55 #include <linux/percpu.h>
60 #define DEBUGP(fmt , a...)
63 #ifndef ARCH_SHF_SMALL
64 #define ARCH_SHF_SMALL 0
67 /* If this is set, the section belongs in the init part of the module */
68 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
70 /* List of modules, protected by module_mutex or preempt_disable
71 * (delete uses stop_machine/add uses RCU list operations). */
72 DEFINE_MUTEX(module_mutex
);
73 EXPORT_SYMBOL_GPL(module_mutex
);
74 static LIST_HEAD(modules
);
76 /* Block module loading/unloading? */
77 int modules_disabled
= 0;
79 /* Waiting for a module to finish initializing? */
80 static DECLARE_WAIT_QUEUE_HEAD(module_wq
);
82 static BLOCKING_NOTIFIER_HEAD(module_notify_list
);
84 /* Bounds of module allocation, for speeding __module_address */
85 static unsigned long module_addr_min
= -1UL, module_addr_max
= 0;
87 int register_module_notifier(struct notifier_block
* nb
)
89 return blocking_notifier_chain_register(&module_notify_list
, nb
);
91 EXPORT_SYMBOL(register_module_notifier
);
93 int unregister_module_notifier(struct notifier_block
* nb
)
95 return blocking_notifier_chain_unregister(&module_notify_list
, nb
);
97 EXPORT_SYMBOL(unregister_module_notifier
);
99 /* We require a truly strong try_module_get(): 0 means failure due to
100 ongoing or failed initialization etc. */
101 static inline int strong_try_module_get(struct module
*mod
)
103 if (mod
&& mod
->state
== MODULE_STATE_COMING
)
105 if (try_module_get(mod
))
111 static inline void add_taint_module(struct module
*mod
, unsigned flag
)
114 mod
->taints
|= (1U << flag
);
118 * A thread that wants to hold a reference to a module only while it
119 * is running can call this to safely exit. nfsd and lockd use this.
121 void __module_put_and_exit(struct module
*mod
, long code
)
126 EXPORT_SYMBOL(__module_put_and_exit
);
128 /* Find a module section: 0 means not found. */
129 static unsigned int find_sec(Elf_Ehdr
*hdr
,
131 const char *secstrings
,
136 for (i
= 1; i
< hdr
->e_shnum
; i
++)
137 /* Alloc bit cleared means "ignore it." */
138 if ((sechdrs
[i
].sh_flags
& SHF_ALLOC
)
139 && strcmp(secstrings
+sechdrs
[i
].sh_name
, name
) == 0)
144 /* Find a module section, or NULL. */
145 static void *section_addr(Elf_Ehdr
*hdr
, Elf_Shdr
*shdrs
,
146 const char *secstrings
, const char *name
)
148 /* Section 0 has sh_addr 0. */
149 return (void *)shdrs
[find_sec(hdr
, shdrs
, secstrings
, name
)].sh_addr
;
152 /* Find a module section, or NULL. Fill in number of "objects" in section. */
153 static void *section_objs(Elf_Ehdr
*hdr
,
155 const char *secstrings
,
160 unsigned int sec
= find_sec(hdr
, sechdrs
, secstrings
, name
);
162 /* Section 0 has sh_addr 0 and sh_size 0. */
163 *num
= sechdrs
[sec
].sh_size
/ object_size
;
164 return (void *)sechdrs
[sec
].sh_addr
;
167 /* Provided by the linker */
168 extern const struct kernel_symbol __start___ksymtab
[];
169 extern const struct kernel_symbol __stop___ksymtab
[];
170 extern const struct kernel_symbol __start___ksymtab_gpl
[];
171 extern const struct kernel_symbol __stop___ksymtab_gpl
[];
172 extern const struct kernel_symbol __start___ksymtab_gpl_future
[];
173 extern const struct kernel_symbol __stop___ksymtab_gpl_future
[];
174 extern const struct kernel_symbol __start___ksymtab_gpl_future
[];
175 extern const struct kernel_symbol __stop___ksymtab_gpl_future
[];
176 extern const unsigned long __start___kcrctab
[];
177 extern const unsigned long __start___kcrctab_gpl
[];
178 extern const unsigned long __start___kcrctab_gpl_future
[];
179 #ifdef CONFIG_UNUSED_SYMBOLS
180 extern const struct kernel_symbol __start___ksymtab_unused
[];
181 extern const struct kernel_symbol __stop___ksymtab_unused
[];
182 extern const struct kernel_symbol __start___ksymtab_unused_gpl
[];
183 extern const struct kernel_symbol __stop___ksymtab_unused_gpl
[];
184 extern const unsigned long __start___kcrctab_unused
[];
185 extern const unsigned long __start___kcrctab_unused_gpl
[];
188 #ifndef CONFIG_MODVERSIONS
189 #define symversion(base, idx) NULL
191 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
194 static bool each_symbol_in_section(const struct symsearch
*arr
,
195 unsigned int arrsize
,
196 struct module
*owner
,
197 bool (*fn
)(const struct symsearch
*syms
,
198 struct module
*owner
,
199 unsigned int symnum
, void *data
),
204 for (j
= 0; j
< arrsize
; j
++) {
205 for (i
= 0; i
< arr
[j
].stop
- arr
[j
].start
; i
++)
206 if (fn(&arr
[j
], owner
, i
, data
))
213 /* Returns true as soon as fn returns true, otherwise false. */
214 bool each_symbol(bool (*fn
)(const struct symsearch
*arr
, struct module
*owner
,
215 unsigned int symnum
, void *data
), void *data
)
218 const struct symsearch arr
[] = {
219 { __start___ksymtab
, __stop___ksymtab
, __start___kcrctab
,
220 NOT_GPL_ONLY
, false },
221 { __start___ksymtab_gpl
, __stop___ksymtab_gpl
,
222 __start___kcrctab_gpl
,
224 { __start___ksymtab_gpl_future
, __stop___ksymtab_gpl_future
,
225 __start___kcrctab_gpl_future
,
226 WILL_BE_GPL_ONLY
, false },
227 #ifdef CONFIG_UNUSED_SYMBOLS
228 { __start___ksymtab_unused
, __stop___ksymtab_unused
,
229 __start___kcrctab_unused
,
230 NOT_GPL_ONLY
, true },
231 { __start___ksymtab_unused_gpl
, __stop___ksymtab_unused_gpl
,
232 __start___kcrctab_unused_gpl
,
237 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), NULL
, fn
, data
))
240 list_for_each_entry_rcu(mod
, &modules
, list
) {
241 struct symsearch arr
[] = {
242 { mod
->syms
, mod
->syms
+ mod
->num_syms
, mod
->crcs
,
243 NOT_GPL_ONLY
, false },
244 { mod
->gpl_syms
, mod
->gpl_syms
+ mod
->num_gpl_syms
,
247 { mod
->gpl_future_syms
,
248 mod
->gpl_future_syms
+ mod
->num_gpl_future_syms
,
249 mod
->gpl_future_crcs
,
250 WILL_BE_GPL_ONLY
, false },
251 #ifdef CONFIG_UNUSED_SYMBOLS
253 mod
->unused_syms
+ mod
->num_unused_syms
,
255 NOT_GPL_ONLY
, true },
256 { mod
->unused_gpl_syms
,
257 mod
->unused_gpl_syms
+ mod
->num_unused_gpl_syms
,
258 mod
->unused_gpl_crcs
,
263 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), mod
, fn
, data
))
268 EXPORT_SYMBOL_GPL(each_symbol
);
270 struct find_symbol_arg
{
277 struct module
*owner
;
278 const unsigned long *crc
;
279 const struct kernel_symbol
*sym
;
282 static bool find_symbol_in_section(const struct symsearch
*syms
,
283 struct module
*owner
,
284 unsigned int symnum
, void *data
)
286 struct find_symbol_arg
*fsa
= data
;
288 if (strcmp(syms
->start
[symnum
].name
, fsa
->name
) != 0)
292 if (syms
->licence
== GPL_ONLY
)
294 if (syms
->licence
== WILL_BE_GPL_ONLY
&& fsa
->warn
) {
295 printk(KERN_WARNING
"Symbol %s is being used "
296 "by a non-GPL module, which will not "
297 "be allowed in the future\n", fsa
->name
);
298 printk(KERN_WARNING
"Please see the file "
299 "Documentation/feature-removal-schedule.txt "
300 "in the kernel source tree for more details.\n");
304 #ifdef CONFIG_UNUSED_SYMBOLS
305 if (syms
->unused
&& fsa
->warn
) {
306 printk(KERN_WARNING
"Symbol %s is marked as UNUSED, "
307 "however this module is using it.\n", fsa
->name
);
309 "This symbol will go away in the future.\n");
311 "Please evalute if this is the right api to use and if "
312 "it really is, submit a report the linux kernel "
313 "mailinglist together with submitting your code for "
319 fsa
->crc
= symversion(syms
->crcs
, symnum
);
320 fsa
->sym
= &syms
->start
[symnum
];
324 /* Find a symbol and return it, along with, (optional) crc and
325 * (optional) module which owns it */
326 const struct kernel_symbol
*find_symbol(const char *name
,
327 struct module
**owner
,
328 const unsigned long **crc
,
332 struct find_symbol_arg fsa
;
338 if (each_symbol(find_symbol_in_section
, &fsa
)) {
346 DEBUGP("Failed to find symbol %s\n", name
);
349 EXPORT_SYMBOL_GPL(find_symbol
);
351 /* Search for module by name: must hold module_mutex. */
352 struct module
*find_module(const char *name
)
356 list_for_each_entry(mod
, &modules
, list
) {
357 if (strcmp(mod
->name
, name
) == 0)
362 EXPORT_SYMBOL_GPL(find_module
);
366 #ifdef CONFIG_HAVE_DYNAMIC_PER_CPU_AREA
368 static void *percpu_modalloc(unsigned long size
, unsigned long align
,
373 if (align
> PAGE_SIZE
) {
374 printk(KERN_WARNING
"%s: per-cpu alignment %li > %li\n",
375 name
, align
, PAGE_SIZE
);
379 ptr
= __alloc_reserved_percpu(size
, align
);
382 "Could not allocate %lu bytes percpu data\n", size
);
386 static void percpu_modfree(void *freeme
)
391 #else /* ... !CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
393 /* Number of blocks used and allocated. */
394 static unsigned int pcpu_num_used
, pcpu_num_allocated
;
395 /* Size of each block. -ve means used. */
396 static int *pcpu_size
;
398 static int split_block(unsigned int i
, unsigned short size
)
400 /* Reallocation required? */
401 if (pcpu_num_used
+ 1 > pcpu_num_allocated
) {
404 new = krealloc(pcpu_size
, sizeof(new[0])*pcpu_num_allocated
*2,
409 pcpu_num_allocated
*= 2;
413 /* Insert a new subblock */
414 memmove(&pcpu_size
[i
+1], &pcpu_size
[i
],
415 sizeof(pcpu_size
[0]) * (pcpu_num_used
- i
));
418 pcpu_size
[i
+1] -= size
;
423 static inline unsigned int block_size(int val
)
430 static void *percpu_modalloc(unsigned long size
, unsigned long align
,
437 if (align
> PAGE_SIZE
) {
438 printk(KERN_WARNING
"%s: per-cpu alignment %li > %li\n",
439 name
, align
, PAGE_SIZE
);
443 ptr
= __per_cpu_start
;
444 for (i
= 0; i
< pcpu_num_used
; ptr
+= block_size(pcpu_size
[i
]), i
++) {
445 /* Extra for alignment requirement. */
446 extra
= ALIGN((unsigned long)ptr
, align
) - (unsigned long)ptr
;
447 BUG_ON(i
== 0 && extra
!= 0);
449 if (pcpu_size
[i
] < 0 || pcpu_size
[i
] < extra
+ size
)
452 /* Transfer extra to previous block. */
453 if (pcpu_size
[i
-1] < 0)
454 pcpu_size
[i
-1] -= extra
;
456 pcpu_size
[i
-1] += extra
;
457 pcpu_size
[i
] -= extra
;
460 /* Split block if warranted */
461 if (pcpu_size
[i
] - size
> sizeof(unsigned long))
462 if (!split_block(i
, size
))
466 pcpu_size
[i
] = -pcpu_size
[i
];
470 printk(KERN_WARNING
"Could not allocate %lu bytes percpu data\n",
475 static void percpu_modfree(void *freeme
)
478 void *ptr
= __per_cpu_start
+ block_size(pcpu_size
[0]);
480 /* First entry is core kernel percpu data. */
481 for (i
= 1; i
< pcpu_num_used
; ptr
+= block_size(pcpu_size
[i
]), i
++) {
483 pcpu_size
[i
] = -pcpu_size
[i
];
490 /* Merge with previous? */
491 if (pcpu_size
[i
-1] >= 0) {
492 pcpu_size
[i
-1] += pcpu_size
[i
];
494 memmove(&pcpu_size
[i
], &pcpu_size
[i
+1],
495 (pcpu_num_used
- i
) * sizeof(pcpu_size
[0]));
498 /* Merge with next? */
499 if (i
+1 < pcpu_num_used
&& pcpu_size
[i
+1] >= 0) {
500 pcpu_size
[i
] += pcpu_size
[i
+1];
502 memmove(&pcpu_size
[i
+1], &pcpu_size
[i
+2],
503 (pcpu_num_used
- (i
+1)) * sizeof(pcpu_size
[0]));
507 static int percpu_modinit(void)
510 pcpu_num_allocated
= 2;
511 pcpu_size
= kmalloc(sizeof(pcpu_size
[0]) * pcpu_num_allocated
,
513 /* Static in-kernel percpu data (used). */
514 pcpu_size
[0] = -(__per_cpu_end
-__per_cpu_start
);
516 pcpu_size
[1] = PERCPU_ENOUGH_ROOM
+ pcpu_size
[0];
517 if (pcpu_size
[1] < 0) {
518 printk(KERN_ERR
"No per-cpu room for modules.\n");
524 __initcall(percpu_modinit
);
526 #endif /* CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
528 static unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
530 const char *secstrings
)
532 return find_sec(hdr
, sechdrs
, secstrings
, ".data.percpu");
535 static void percpu_modcopy(void *pcpudest
, const void *from
, unsigned long size
)
539 for_each_possible_cpu(cpu
)
540 memcpy(pcpudest
+ per_cpu_offset(cpu
), from
, size
);
543 #else /* ... !CONFIG_SMP */
545 static inline void *percpu_modalloc(unsigned long size
, unsigned long align
,
550 static inline void percpu_modfree(void *pcpuptr
)
554 static inline unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
556 const char *secstrings
)
560 static inline void percpu_modcopy(void *pcpudst
, const void *src
,
563 /* pcpusec should be 0, and size of that section should be 0. */
567 #endif /* CONFIG_SMP */
569 #define MODINFO_ATTR(field) \
570 static void setup_modinfo_##field(struct module *mod, const char *s) \
572 mod->field = kstrdup(s, GFP_KERNEL); \
574 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
575 struct module *mod, char *buffer) \
577 return sprintf(buffer, "%s\n", mod->field); \
579 static int modinfo_##field##_exists(struct module *mod) \
581 return mod->field != NULL; \
583 static void free_modinfo_##field(struct module *mod) \
588 static struct module_attribute modinfo_##field = { \
589 .attr = { .name = __stringify(field), .mode = 0444 }, \
590 .show = show_modinfo_##field, \
591 .setup = setup_modinfo_##field, \
592 .test = modinfo_##field##_exists, \
593 .free = free_modinfo_##field, \
596 MODINFO_ATTR(version
);
597 MODINFO_ATTR(srcversion
);
599 static char last_unloaded_module
[MODULE_NAME_LEN
+1];
601 #ifdef CONFIG_MODULE_UNLOAD
602 /* Init the unload section of the module. */
603 static void module_unload_init(struct module
*mod
)
607 INIT_LIST_HEAD(&mod
->modules_which_use_me
);
608 for_each_possible_cpu(cpu
)
609 local_set(__module_ref_addr(mod
, cpu
), 0);
610 /* Hold reference count during initialization. */
611 local_set(__module_ref_addr(mod
, raw_smp_processor_id()), 1);
612 /* Backwards compatibility macros put refcount during init. */
613 mod
->waiter
= current
;
616 /* modules using other modules */
619 struct list_head list
;
620 struct module
*module_which_uses
;
623 /* Does a already use b? */
624 static int already_uses(struct module
*a
, struct module
*b
)
626 struct module_use
*use
;
628 list_for_each_entry(use
, &b
->modules_which_use_me
, list
) {
629 if (use
->module_which_uses
== a
) {
630 DEBUGP("%s uses %s!\n", a
->name
, b
->name
);
634 DEBUGP("%s does not use %s!\n", a
->name
, b
->name
);
638 /* Module a uses b */
639 int use_module(struct module
*a
, struct module
*b
)
641 struct module_use
*use
;
644 if (b
== NULL
|| already_uses(a
, b
)) return 1;
646 /* If we're interrupted or time out, we fail. */
647 if (wait_event_interruptible_timeout(
648 module_wq
, (err
= strong_try_module_get(b
)) != -EBUSY
,
650 printk("%s: gave up waiting for init of module %s.\n",
655 /* If strong_try_module_get() returned a different error, we fail. */
659 DEBUGP("Allocating new usage for %s.\n", a
->name
);
660 use
= kmalloc(sizeof(*use
), GFP_ATOMIC
);
662 printk("%s: out of memory loading\n", a
->name
);
667 use
->module_which_uses
= a
;
668 list_add(&use
->list
, &b
->modules_which_use_me
);
669 no_warn
= sysfs_create_link(b
->holders_dir
, &a
->mkobj
.kobj
, a
->name
);
672 EXPORT_SYMBOL_GPL(use_module
);
674 /* Clear the unload stuff of the module. */
675 static void module_unload_free(struct module
*mod
)
679 list_for_each_entry(i
, &modules
, list
) {
680 struct module_use
*use
;
682 list_for_each_entry(use
, &i
->modules_which_use_me
, list
) {
683 if (use
->module_which_uses
== mod
) {
684 DEBUGP("%s unusing %s\n", mod
->name
, i
->name
);
686 list_del(&use
->list
);
688 sysfs_remove_link(i
->holders_dir
, mod
->name
);
689 /* There can be at most one match. */
696 #ifdef CONFIG_MODULE_FORCE_UNLOAD
697 static inline int try_force_unload(unsigned int flags
)
699 int ret
= (flags
& O_TRUNC
);
701 add_taint(TAINT_FORCED_RMMOD
);
705 static inline int try_force_unload(unsigned int flags
)
709 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
718 /* Whole machine is stopped with interrupts off when this runs. */
719 static int __try_stop_module(void *_sref
)
721 struct stopref
*sref
= _sref
;
723 /* If it's not unused, quit unless we're forcing. */
724 if (module_refcount(sref
->mod
) != 0) {
725 if (!(*sref
->forced
= try_force_unload(sref
->flags
)))
729 /* Mark it as dying. */
730 sref
->mod
->state
= MODULE_STATE_GOING
;
734 static int try_stop_module(struct module
*mod
, int flags
, int *forced
)
736 if (flags
& O_NONBLOCK
) {
737 struct stopref sref
= { mod
, flags
, forced
};
739 return stop_machine(__try_stop_module
, &sref
, NULL
);
741 /* We don't need to stop the machine for this. */
742 mod
->state
= MODULE_STATE_GOING
;
748 unsigned int module_refcount(struct module
*mod
)
750 unsigned int total
= 0;
753 for_each_possible_cpu(cpu
)
754 total
+= local_read(__module_ref_addr(mod
, cpu
));
757 EXPORT_SYMBOL(module_refcount
);
759 /* This exists whether we can unload or not */
760 static void free_module(struct module
*mod
);
762 static void wait_for_zero_refcount(struct module
*mod
)
764 /* Since we might sleep for some time, release the mutex first */
765 mutex_unlock(&module_mutex
);
767 DEBUGP("Looking at refcount...\n");
768 set_current_state(TASK_UNINTERRUPTIBLE
);
769 if (module_refcount(mod
) == 0)
773 current
->state
= TASK_RUNNING
;
774 mutex_lock(&module_mutex
);
777 SYSCALL_DEFINE2(delete_module
, const char __user
*, name_user
,
781 char name
[MODULE_NAME_LEN
];
784 if (!capable(CAP_SYS_MODULE
) || modules_disabled
)
787 if (strncpy_from_user(name
, name_user
, MODULE_NAME_LEN
-1) < 0)
789 name
[MODULE_NAME_LEN
-1] = '\0';
791 /* Create stop_machine threads since free_module relies on
792 * a non-failing stop_machine call. */
793 ret
= stop_machine_create();
797 if (mutex_lock_interruptible(&module_mutex
) != 0) {
802 mod
= find_module(name
);
808 if (!list_empty(&mod
->modules_which_use_me
)) {
809 /* Other modules depend on us: get rid of them first. */
814 /* Doing init or already dying? */
815 if (mod
->state
!= MODULE_STATE_LIVE
) {
816 /* FIXME: if (force), slam module count and wake up
818 DEBUGP("%s already dying\n", mod
->name
);
823 /* If it has an init func, it must have an exit func to unload */
824 if (mod
->init
&& !mod
->exit
) {
825 forced
= try_force_unload(flags
);
827 /* This module can't be removed */
833 /* Set this up before setting mod->state */
834 mod
->waiter
= current
;
836 /* Stop the machine so refcounts can't move and disable module. */
837 ret
= try_stop_module(mod
, flags
, &forced
);
841 /* Never wait if forced. */
842 if (!forced
&& module_refcount(mod
) != 0)
843 wait_for_zero_refcount(mod
);
845 mutex_unlock(&module_mutex
);
846 /* Final destruction now noone is using it. */
847 if (mod
->exit
!= NULL
)
849 blocking_notifier_call_chain(&module_notify_list
,
850 MODULE_STATE_GOING
, mod
);
851 async_synchronize_full();
852 mutex_lock(&module_mutex
);
853 /* Store the name of the last unloaded module for diagnostic purposes */
854 strlcpy(last_unloaded_module
, mod
->name
, sizeof(last_unloaded_module
));
855 ddebug_remove_module(mod
->name
);
859 mutex_unlock(&module_mutex
);
861 stop_machine_destroy();
865 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
867 struct module_use
*use
;
868 int printed_something
= 0;
870 seq_printf(m
, " %u ", module_refcount(mod
));
872 /* Always include a trailing , so userspace can differentiate
873 between this and the old multi-field proc format. */
874 list_for_each_entry(use
, &mod
->modules_which_use_me
, list
) {
875 printed_something
= 1;
876 seq_printf(m
, "%s,", use
->module_which_uses
->name
);
879 if (mod
->init
!= NULL
&& mod
->exit
== NULL
) {
880 printed_something
= 1;
881 seq_printf(m
, "[permanent],");
884 if (!printed_something
)
888 void __symbol_put(const char *symbol
)
890 struct module
*owner
;
893 if (!find_symbol(symbol
, &owner
, NULL
, true, false))
898 EXPORT_SYMBOL(__symbol_put
);
900 void symbol_put_addr(void *addr
)
902 struct module
*modaddr
;
904 if (core_kernel_text((unsigned long)addr
))
907 /* module_text_address is safe here: we're supposed to have reference
908 * to module from symbol_get, so it can't go away. */
909 modaddr
= __module_text_address((unsigned long)addr
);
913 EXPORT_SYMBOL_GPL(symbol_put_addr
);
915 static ssize_t
show_refcnt(struct module_attribute
*mattr
,
916 struct module
*mod
, char *buffer
)
918 return sprintf(buffer
, "%u\n", module_refcount(mod
));
921 static struct module_attribute refcnt
= {
922 .attr
= { .name
= "refcnt", .mode
= 0444 },
926 void module_put(struct module
*module
)
929 unsigned int cpu
= get_cpu();
930 local_dec(__module_ref_addr(module
, cpu
));
931 /* Maybe they're waiting for us to drop reference? */
932 if (unlikely(!module_is_live(module
)))
933 wake_up_process(module
->waiter
);
937 EXPORT_SYMBOL(module_put
);
939 #else /* !CONFIG_MODULE_UNLOAD */
940 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
942 /* We don't know the usage count, or what modules are using. */
943 seq_printf(m
, " - -");
946 static inline void module_unload_free(struct module
*mod
)
950 int use_module(struct module
*a
, struct module
*b
)
952 return strong_try_module_get(b
) == 0;
954 EXPORT_SYMBOL_GPL(use_module
);
956 static inline void module_unload_init(struct module
*mod
)
959 #endif /* CONFIG_MODULE_UNLOAD */
961 static ssize_t
show_initstate(struct module_attribute
*mattr
,
962 struct module
*mod
, char *buffer
)
964 const char *state
= "unknown";
966 switch (mod
->state
) {
967 case MODULE_STATE_LIVE
:
970 case MODULE_STATE_COMING
:
973 case MODULE_STATE_GOING
:
977 return sprintf(buffer
, "%s\n", state
);
980 static struct module_attribute initstate
= {
981 .attr
= { .name
= "initstate", .mode
= 0444 },
982 .show
= show_initstate
,
985 static struct module_attribute
*modinfo_attrs
[] = {
989 #ifdef CONFIG_MODULE_UNLOAD
995 static const char vermagic
[] = VERMAGIC_STRING
;
997 static int try_to_force_load(struct module
*mod
, const char *reason
)
999 #ifdef CONFIG_MODULE_FORCE_LOAD
1000 if (!test_taint(TAINT_FORCED_MODULE
))
1001 printk(KERN_WARNING
"%s: %s: kernel tainted.\n",
1003 add_taint_module(mod
, TAINT_FORCED_MODULE
);
1010 #ifdef CONFIG_MODVERSIONS
1011 static int check_version(Elf_Shdr
*sechdrs
,
1012 unsigned int versindex
,
1013 const char *symname
,
1015 const unsigned long *crc
)
1017 unsigned int i
, num_versions
;
1018 struct modversion_info
*versions
;
1020 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1024 /* No versions at all? modprobe --force does this. */
1026 return try_to_force_load(mod
, symname
) == 0;
1028 versions
= (void *) sechdrs
[versindex
].sh_addr
;
1029 num_versions
= sechdrs
[versindex
].sh_size
1030 / sizeof(struct modversion_info
);
1032 for (i
= 0; i
< num_versions
; i
++) {
1033 if (strcmp(versions
[i
].name
, symname
) != 0)
1036 if (versions
[i
].crc
== *crc
)
1038 DEBUGP("Found checksum %lX vs module %lX\n",
1039 *crc
, versions
[i
].crc
);
1043 printk(KERN_WARNING
"%s: no symbol version for %s\n",
1044 mod
->name
, symname
);
1048 printk("%s: disagrees about version of symbol %s\n",
1049 mod
->name
, symname
);
1053 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1054 unsigned int versindex
,
1057 const unsigned long *crc
;
1059 if (!find_symbol("module_layout", NULL
, &crc
, true, false))
1061 return check_version(sechdrs
, versindex
, "module_layout", mod
, crc
);
1064 /* First part is kernel version, which we ignore if module has crcs. */
1065 static inline int same_magic(const char *amagic
, const char *bmagic
,
1069 amagic
+= strcspn(amagic
, " ");
1070 bmagic
+= strcspn(bmagic
, " ");
1072 return strcmp(amagic
, bmagic
) == 0;
1075 static inline int check_version(Elf_Shdr
*sechdrs
,
1076 unsigned int versindex
,
1077 const char *symname
,
1079 const unsigned long *crc
)
1084 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1085 unsigned int versindex
,
1091 static inline int same_magic(const char *amagic
, const char *bmagic
,
1094 return strcmp(amagic
, bmagic
) == 0;
1096 #endif /* CONFIG_MODVERSIONS */
1098 /* Resolve a symbol for this module. I.e. if we find one, record usage.
1099 Must be holding module_mutex. */
1100 static const struct kernel_symbol
*resolve_symbol(Elf_Shdr
*sechdrs
,
1101 unsigned int versindex
,
1105 struct module
*owner
;
1106 const struct kernel_symbol
*sym
;
1107 const unsigned long *crc
;
1109 sym
= find_symbol(name
, &owner
, &crc
,
1110 !(mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
)), true);
1111 /* use_module can fail due to OOM,
1112 or module initialization or unloading */
1114 if (!check_version(sechdrs
, versindex
, name
, mod
, crc
) ||
1115 !use_module(mod
, owner
))
1122 * /sys/module/foo/sections stuff
1123 * J. Corbet <corbet@lwn.net>
1125 #if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
1126 struct module_sect_attr
1128 struct module_attribute mattr
;
1130 unsigned long address
;
1133 struct module_sect_attrs
1135 struct attribute_group grp
;
1136 unsigned int nsections
;
1137 struct module_sect_attr attrs
[0];
1140 static ssize_t
module_sect_show(struct module_attribute
*mattr
,
1141 struct module
*mod
, char *buf
)
1143 struct module_sect_attr
*sattr
=
1144 container_of(mattr
, struct module_sect_attr
, mattr
);
1145 return sprintf(buf
, "0x%lx\n", sattr
->address
);
1148 static void free_sect_attrs(struct module_sect_attrs
*sect_attrs
)
1150 unsigned int section
;
1152 for (section
= 0; section
< sect_attrs
->nsections
; section
++)
1153 kfree(sect_attrs
->attrs
[section
].name
);
1157 static void add_sect_attrs(struct module
*mod
, unsigned int nsect
,
1158 char *secstrings
, Elf_Shdr
*sechdrs
)
1160 unsigned int nloaded
= 0, i
, size
[2];
1161 struct module_sect_attrs
*sect_attrs
;
1162 struct module_sect_attr
*sattr
;
1163 struct attribute
**gattr
;
1165 /* Count loaded sections and allocate structures */
1166 for (i
= 0; i
< nsect
; i
++)
1167 if (sechdrs
[i
].sh_flags
& SHF_ALLOC
)
1169 size
[0] = ALIGN(sizeof(*sect_attrs
)
1170 + nloaded
* sizeof(sect_attrs
->attrs
[0]),
1171 sizeof(sect_attrs
->grp
.attrs
[0]));
1172 size
[1] = (nloaded
+ 1) * sizeof(sect_attrs
->grp
.attrs
[0]);
1173 sect_attrs
= kzalloc(size
[0] + size
[1], GFP_KERNEL
);
1174 if (sect_attrs
== NULL
)
1177 /* Setup section attributes. */
1178 sect_attrs
->grp
.name
= "sections";
1179 sect_attrs
->grp
.attrs
= (void *)sect_attrs
+ size
[0];
1181 sect_attrs
->nsections
= 0;
1182 sattr
= §_attrs
->attrs
[0];
1183 gattr
= §_attrs
->grp
.attrs
[0];
1184 for (i
= 0; i
< nsect
; i
++) {
1185 if (! (sechdrs
[i
].sh_flags
& SHF_ALLOC
))
1187 sattr
->address
= sechdrs
[i
].sh_addr
;
1188 sattr
->name
= kstrdup(secstrings
+ sechdrs
[i
].sh_name
,
1190 if (sattr
->name
== NULL
)
1192 sect_attrs
->nsections
++;
1193 sattr
->mattr
.show
= module_sect_show
;
1194 sattr
->mattr
.store
= NULL
;
1195 sattr
->mattr
.attr
.name
= sattr
->name
;
1196 sattr
->mattr
.attr
.mode
= S_IRUGO
;
1197 *(gattr
++) = &(sattr
++)->mattr
.attr
;
1201 if (sysfs_create_group(&mod
->mkobj
.kobj
, §_attrs
->grp
))
1204 mod
->sect_attrs
= sect_attrs
;
1207 free_sect_attrs(sect_attrs
);
1210 static void remove_sect_attrs(struct module
*mod
)
1212 if (mod
->sect_attrs
) {
1213 sysfs_remove_group(&mod
->mkobj
.kobj
,
1214 &mod
->sect_attrs
->grp
);
1215 /* We are positive that no one is using any sect attrs
1216 * at this point. Deallocate immediately. */
1217 free_sect_attrs(mod
->sect_attrs
);
1218 mod
->sect_attrs
= NULL
;
1223 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1226 struct module_notes_attrs
{
1227 struct kobject
*dir
;
1229 struct bin_attribute attrs
[0];
1232 static ssize_t
module_notes_read(struct kobject
*kobj
,
1233 struct bin_attribute
*bin_attr
,
1234 char *buf
, loff_t pos
, size_t count
)
1237 * The caller checked the pos and count against our size.
1239 memcpy(buf
, bin_attr
->private + pos
, count
);
1243 static void free_notes_attrs(struct module_notes_attrs
*notes_attrs
,
1246 if (notes_attrs
->dir
) {
1248 sysfs_remove_bin_file(notes_attrs
->dir
,
1249 ¬es_attrs
->attrs
[i
]);
1250 kobject_put(notes_attrs
->dir
);
1255 static void add_notes_attrs(struct module
*mod
, unsigned int nsect
,
1256 char *secstrings
, Elf_Shdr
*sechdrs
)
1258 unsigned int notes
, loaded
, i
;
1259 struct module_notes_attrs
*notes_attrs
;
1260 struct bin_attribute
*nattr
;
1262 /* Count notes sections and allocate structures. */
1264 for (i
= 0; i
< nsect
; i
++)
1265 if ((sechdrs
[i
].sh_flags
& SHF_ALLOC
) &&
1266 (sechdrs
[i
].sh_type
== SHT_NOTE
))
1272 notes_attrs
= kzalloc(sizeof(*notes_attrs
)
1273 + notes
* sizeof(notes_attrs
->attrs
[0]),
1275 if (notes_attrs
== NULL
)
1278 notes_attrs
->notes
= notes
;
1279 nattr
= ¬es_attrs
->attrs
[0];
1280 for (loaded
= i
= 0; i
< nsect
; ++i
) {
1281 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
1283 if (sechdrs
[i
].sh_type
== SHT_NOTE
) {
1284 nattr
->attr
.name
= mod
->sect_attrs
->attrs
[loaded
].name
;
1285 nattr
->attr
.mode
= S_IRUGO
;
1286 nattr
->size
= sechdrs
[i
].sh_size
;
1287 nattr
->private = (void *) sechdrs
[i
].sh_addr
;
1288 nattr
->read
= module_notes_read
;
1294 notes_attrs
->dir
= kobject_create_and_add("notes", &mod
->mkobj
.kobj
);
1295 if (!notes_attrs
->dir
)
1298 for (i
= 0; i
< notes
; ++i
)
1299 if (sysfs_create_bin_file(notes_attrs
->dir
,
1300 ¬es_attrs
->attrs
[i
]))
1303 mod
->notes_attrs
= notes_attrs
;
1307 free_notes_attrs(notes_attrs
, i
);
1310 static void remove_notes_attrs(struct module
*mod
)
1312 if (mod
->notes_attrs
)
1313 free_notes_attrs(mod
->notes_attrs
, mod
->notes_attrs
->notes
);
1318 static inline void add_sect_attrs(struct module
*mod
, unsigned int nsect
,
1319 char *sectstrings
, Elf_Shdr
*sechdrs
)
1323 static inline void remove_sect_attrs(struct module
*mod
)
1327 static inline void add_notes_attrs(struct module
*mod
, unsigned int nsect
,
1328 char *sectstrings
, Elf_Shdr
*sechdrs
)
1332 static inline void remove_notes_attrs(struct module
*mod
)
1338 int module_add_modinfo_attrs(struct module
*mod
)
1340 struct module_attribute
*attr
;
1341 struct module_attribute
*temp_attr
;
1345 mod
->modinfo_attrs
= kzalloc((sizeof(struct module_attribute
) *
1346 (ARRAY_SIZE(modinfo_attrs
) + 1)),
1348 if (!mod
->modinfo_attrs
)
1351 temp_attr
= mod
->modinfo_attrs
;
1352 for (i
= 0; (attr
= modinfo_attrs
[i
]) && !error
; i
++) {
1354 (attr
->test
&& attr
->test(mod
))) {
1355 memcpy(temp_attr
, attr
, sizeof(*temp_attr
));
1356 error
= sysfs_create_file(&mod
->mkobj
.kobj
,&temp_attr
->attr
);
1363 void module_remove_modinfo_attrs(struct module
*mod
)
1365 struct module_attribute
*attr
;
1368 for (i
= 0; (attr
= &mod
->modinfo_attrs
[i
]); i
++) {
1369 /* pick a field to test for end of list */
1370 if (!attr
->attr
.name
)
1372 sysfs_remove_file(&mod
->mkobj
.kobj
,&attr
->attr
);
1376 kfree(mod
->modinfo_attrs
);
1379 int mod_sysfs_init(struct module
*mod
)
1382 struct kobject
*kobj
;
1384 if (!module_sysfs_initialized
) {
1385 printk(KERN_ERR
"%s: module sysfs not initialized\n",
1391 kobj
= kset_find_obj(module_kset
, mod
->name
);
1393 printk(KERN_ERR
"%s: module is already loaded\n", mod
->name
);
1399 mod
->mkobj
.mod
= mod
;
1401 memset(&mod
->mkobj
.kobj
, 0, sizeof(mod
->mkobj
.kobj
));
1402 mod
->mkobj
.kobj
.kset
= module_kset
;
1403 err
= kobject_init_and_add(&mod
->mkobj
.kobj
, &module_ktype
, NULL
,
1406 kobject_put(&mod
->mkobj
.kobj
);
1408 /* delay uevent until full sysfs population */
1413 int mod_sysfs_setup(struct module
*mod
,
1414 struct kernel_param
*kparam
,
1415 unsigned int num_params
)
1419 mod
->holders_dir
= kobject_create_and_add("holders", &mod
->mkobj
.kobj
);
1420 if (!mod
->holders_dir
) {
1425 err
= module_param_sysfs_setup(mod
, kparam
, num_params
);
1427 goto out_unreg_holders
;
1429 err
= module_add_modinfo_attrs(mod
);
1431 goto out_unreg_param
;
1433 kobject_uevent(&mod
->mkobj
.kobj
, KOBJ_ADD
);
1437 module_param_sysfs_remove(mod
);
1439 kobject_put(mod
->holders_dir
);
1441 kobject_put(&mod
->mkobj
.kobj
);
1445 static void mod_sysfs_fini(struct module
*mod
)
1447 kobject_put(&mod
->mkobj
.kobj
);
1450 #else /* CONFIG_SYSFS */
1452 static void mod_sysfs_fini(struct module
*mod
)
1456 #endif /* CONFIG_SYSFS */
1458 static void mod_kobject_remove(struct module
*mod
)
1460 module_remove_modinfo_attrs(mod
);
1461 module_param_sysfs_remove(mod
);
1462 kobject_put(mod
->mkobj
.drivers_dir
);
1463 kobject_put(mod
->holders_dir
);
1464 mod_sysfs_fini(mod
);
1468 * unlink the module with the whole machine is stopped with interrupts off
1469 * - this defends against kallsyms not taking locks
1471 static int __unlink_module(void *_mod
)
1473 struct module
*mod
= _mod
;
1474 list_del(&mod
->list
);
1478 /* Free a module, remove from lists, etc (must hold module_mutex). */
1479 static void free_module(struct module
*mod
)
1481 /* Delete from various lists */
1482 stop_machine(__unlink_module
, mod
, NULL
);
1483 remove_notes_attrs(mod
);
1484 remove_sect_attrs(mod
);
1485 mod_kobject_remove(mod
);
1487 /* Arch-specific cleanup. */
1488 module_arch_cleanup(mod
);
1490 /* Module unload stuff */
1491 module_unload_free(mod
);
1493 /* Free any allocated parameters. */
1494 destroy_params(mod
->kp
, mod
->num_kp
);
1496 /* This may be NULL, but that's OK */
1497 module_free(mod
, mod
->module_init
);
1500 percpu_modfree(mod
->percpu
);
1501 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
1503 percpu_modfree(mod
->refptr
);
1505 /* Free lock-classes: */
1506 lockdep_free_key_range(mod
->module_core
, mod
->core_size
);
1508 /* Finally, free the core (containing the module structure) */
1509 module_free(mod
, mod
->module_core
);
1512 void *__symbol_get(const char *symbol
)
1514 struct module
*owner
;
1515 const struct kernel_symbol
*sym
;
1518 sym
= find_symbol(symbol
, &owner
, NULL
, true, true);
1519 if (sym
&& strong_try_module_get(owner
))
1523 return sym
? (void *)sym
->value
: NULL
;
1525 EXPORT_SYMBOL_GPL(__symbol_get
);
1528 * Ensure that an exported symbol [global namespace] does not already exist
1529 * in the kernel or in some other module's exported symbol table.
1531 static int verify_export_symbols(struct module
*mod
)
1534 struct module
*owner
;
1535 const struct kernel_symbol
*s
;
1537 const struct kernel_symbol
*sym
;
1540 { mod
->syms
, mod
->num_syms
},
1541 { mod
->gpl_syms
, mod
->num_gpl_syms
},
1542 { mod
->gpl_future_syms
, mod
->num_gpl_future_syms
},
1543 #ifdef CONFIG_UNUSED_SYMBOLS
1544 { mod
->unused_syms
, mod
->num_unused_syms
},
1545 { mod
->unused_gpl_syms
, mod
->num_unused_gpl_syms
},
1549 for (i
= 0; i
< ARRAY_SIZE(arr
); i
++) {
1550 for (s
= arr
[i
].sym
; s
< arr
[i
].sym
+ arr
[i
].num
; s
++) {
1551 if (find_symbol(s
->name
, &owner
, NULL
, true, false)) {
1553 "%s: exports duplicate symbol %s"
1555 mod
->name
, s
->name
, module_name(owner
));
1563 /* Change all symbols so that st_value encodes the pointer directly. */
1564 static int simplify_symbols(Elf_Shdr
*sechdrs
,
1565 unsigned int symindex
,
1567 unsigned int versindex
,
1568 unsigned int pcpuindex
,
1571 Elf_Sym
*sym
= (void *)sechdrs
[symindex
].sh_addr
;
1572 unsigned long secbase
;
1573 unsigned int i
, n
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1575 const struct kernel_symbol
*ksym
;
1577 for (i
= 1; i
< n
; i
++) {
1578 switch (sym
[i
].st_shndx
) {
1580 /* We compiled with -fno-common. These are not
1581 supposed to happen. */
1582 DEBUGP("Common symbol: %s\n", strtab
+ sym
[i
].st_name
);
1583 printk("%s: please compile with -fno-common\n",
1589 /* Don't need to do anything */
1590 DEBUGP("Absolute symbol: 0x%08lx\n",
1591 (long)sym
[i
].st_value
);
1595 ksym
= resolve_symbol(sechdrs
, versindex
,
1596 strtab
+ sym
[i
].st_name
, mod
);
1597 /* Ok if resolved. */
1599 sym
[i
].st_value
= ksym
->value
;
1604 if (ELF_ST_BIND(sym
[i
].st_info
) == STB_WEAK
)
1607 printk(KERN_WARNING
"%s: Unknown symbol %s\n",
1608 mod
->name
, strtab
+ sym
[i
].st_name
);
1613 /* Divert to percpu allocation if a percpu var. */
1614 if (sym
[i
].st_shndx
== pcpuindex
)
1615 secbase
= (unsigned long)mod
->percpu
;
1617 secbase
= sechdrs
[sym
[i
].st_shndx
].sh_addr
;
1618 sym
[i
].st_value
+= secbase
;
1626 /* Additional bytes needed by arch in front of individual sections */
1627 unsigned int __weak
arch_mod_section_prepend(struct module
*mod
,
1628 unsigned int section
)
1630 /* default implementation just returns zero */
1634 /* Update size with this section: return offset. */
1635 static long get_offset(struct module
*mod
, unsigned int *size
,
1636 Elf_Shdr
*sechdr
, unsigned int section
)
1640 *size
+= arch_mod_section_prepend(mod
, section
);
1641 ret
= ALIGN(*size
, sechdr
->sh_addralign
?: 1);
1642 *size
= ret
+ sechdr
->sh_size
;
1646 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1647 might -- code, read-only data, read-write data, small data. Tally
1648 sizes, and place the offsets into sh_entsize fields: high bit means it
1650 static void layout_sections(struct module
*mod
,
1651 const Elf_Ehdr
*hdr
,
1653 const char *secstrings
)
1655 static unsigned long const masks
[][2] = {
1656 /* NOTE: all executable code must be the first section
1657 * in this array; otherwise modify the text_size
1658 * finder in the two loops below */
1659 { SHF_EXECINSTR
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1660 { SHF_ALLOC
, SHF_WRITE
| ARCH_SHF_SMALL
},
1661 { SHF_WRITE
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1662 { ARCH_SHF_SMALL
| SHF_ALLOC
, 0 }
1666 for (i
= 0; i
< hdr
->e_shnum
; i
++)
1667 sechdrs
[i
].sh_entsize
= ~0UL;
1669 DEBUGP("Core section allocation order:\n");
1670 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1671 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1672 Elf_Shdr
*s
= &sechdrs
[i
];
1674 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1675 || (s
->sh_flags
& masks
[m
][1])
1676 || s
->sh_entsize
!= ~0UL
1677 || strstarts(secstrings
+ s
->sh_name
, ".init"))
1679 s
->sh_entsize
= get_offset(mod
, &mod
->core_size
, s
, i
);
1680 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1683 mod
->core_text_size
= mod
->core_size
;
1686 DEBUGP("Init section allocation order:\n");
1687 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1688 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1689 Elf_Shdr
*s
= &sechdrs
[i
];
1691 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1692 || (s
->sh_flags
& masks
[m
][1])
1693 || s
->sh_entsize
!= ~0UL
1694 || !strstarts(secstrings
+ s
->sh_name
, ".init"))
1696 s
->sh_entsize
= (get_offset(mod
, &mod
->init_size
, s
, i
)
1697 | INIT_OFFSET_MASK
);
1698 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1701 mod
->init_text_size
= mod
->init_size
;
1705 static void set_license(struct module
*mod
, const char *license
)
1708 license
= "unspecified";
1710 if (!license_is_gpl_compatible(license
)) {
1711 if (!test_taint(TAINT_PROPRIETARY_MODULE
))
1712 printk(KERN_WARNING
"%s: module license '%s' taints "
1713 "kernel.\n", mod
->name
, license
);
1714 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
1718 /* Parse tag=value strings from .modinfo section */
1719 static char *next_string(char *string
, unsigned long *secsize
)
1721 /* Skip non-zero chars */
1724 if ((*secsize
)-- <= 1)
1728 /* Skip any zero padding. */
1729 while (!string
[0]) {
1731 if ((*secsize
)-- <= 1)
1737 static char *get_modinfo(Elf_Shdr
*sechdrs
,
1742 unsigned int taglen
= strlen(tag
);
1743 unsigned long size
= sechdrs
[info
].sh_size
;
1745 for (p
= (char *)sechdrs
[info
].sh_addr
; p
; p
= next_string(p
, &size
)) {
1746 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
1747 return p
+ taglen
+ 1;
1752 static void setup_modinfo(struct module
*mod
, Elf_Shdr
*sechdrs
,
1753 unsigned int infoindex
)
1755 struct module_attribute
*attr
;
1758 for (i
= 0; (attr
= modinfo_attrs
[i
]); i
++) {
1761 get_modinfo(sechdrs
,
1767 #ifdef CONFIG_KALLSYMS
1769 /* lookup symbol in given range of kernel_symbols */
1770 static const struct kernel_symbol
*lookup_symbol(const char *name
,
1771 const struct kernel_symbol
*start
,
1772 const struct kernel_symbol
*stop
)
1774 const struct kernel_symbol
*ks
= start
;
1775 for (; ks
< stop
; ks
++)
1776 if (strcmp(ks
->name
, name
) == 0)
1781 static int is_exported(const char *name
, unsigned long value
,
1782 const struct module
*mod
)
1784 const struct kernel_symbol
*ks
;
1786 ks
= lookup_symbol(name
, __start___ksymtab
, __stop___ksymtab
);
1788 ks
= lookup_symbol(name
, mod
->syms
, mod
->syms
+ mod
->num_syms
);
1789 return ks
!= NULL
&& ks
->value
== value
;
1793 static char elf_type(const Elf_Sym
*sym
,
1795 const char *secstrings
,
1798 if (ELF_ST_BIND(sym
->st_info
) == STB_WEAK
) {
1799 if (ELF_ST_TYPE(sym
->st_info
) == STT_OBJECT
)
1804 if (sym
->st_shndx
== SHN_UNDEF
)
1806 if (sym
->st_shndx
== SHN_ABS
)
1808 if (sym
->st_shndx
>= SHN_LORESERVE
)
1810 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_EXECINSTR
)
1812 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_ALLOC
1813 && sechdrs
[sym
->st_shndx
].sh_type
!= SHT_NOBITS
) {
1814 if (!(sechdrs
[sym
->st_shndx
].sh_flags
& SHF_WRITE
))
1816 else if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1821 if (sechdrs
[sym
->st_shndx
].sh_type
== SHT_NOBITS
) {
1822 if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1827 if (strstarts(secstrings
+ sechdrs
[sym
->st_shndx
].sh_name
, ".debug"))
1832 static void add_kallsyms(struct module
*mod
,
1834 unsigned int symindex
,
1835 unsigned int strindex
,
1836 const char *secstrings
)
1840 mod
->symtab
= (void *)sechdrs
[symindex
].sh_addr
;
1841 mod
->num_symtab
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1842 mod
->strtab
= (void *)sechdrs
[strindex
].sh_addr
;
1844 /* Set types up while we still have access to sections. */
1845 for (i
= 0; i
< mod
->num_symtab
; i
++)
1846 mod
->symtab
[i
].st_info
1847 = elf_type(&mod
->symtab
[i
], sechdrs
, secstrings
, mod
);
1850 static inline void add_kallsyms(struct module
*mod
,
1852 unsigned int symindex
,
1853 unsigned int strindex
,
1854 const char *secstrings
)
1857 #endif /* CONFIG_KALLSYMS */
1859 static void dynamic_debug_setup(struct _ddebug
*debug
, unsigned int num
)
1861 #ifdef CONFIG_DYNAMIC_DEBUG
1862 if (ddebug_add_module(debug
, num
, debug
->modname
))
1863 printk(KERN_ERR
"dynamic debug error adding module: %s\n",
1868 static void *module_alloc_update_bounds(unsigned long size
)
1870 void *ret
= module_alloc(size
);
1873 /* Update module bounds. */
1874 if ((unsigned long)ret
< module_addr_min
)
1875 module_addr_min
= (unsigned long)ret
;
1876 if ((unsigned long)ret
+ size
> module_addr_max
)
1877 module_addr_max
= (unsigned long)ret
+ size
;
1882 /* Allocate and load the module: note that size of section 0 is always
1883 zero, and we rely on this for optional sections. */
1884 static noinline
struct module
*load_module(void __user
*umod
,
1886 const char __user
*uargs
)
1890 char *secstrings
, *args
, *modmagic
, *strtab
= NULL
;
1893 unsigned int symindex
= 0;
1894 unsigned int strindex
= 0;
1895 unsigned int modindex
, versindex
, infoindex
, pcpuindex
;
1898 void *percpu
= NULL
, *ptr
= NULL
; /* Stops spurious gcc warning */
1899 mm_segment_t old_fs
;
1901 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1903 if (len
< sizeof(*hdr
))
1904 return ERR_PTR(-ENOEXEC
);
1906 /* Suck in entire file: we'll want most of it. */
1907 /* vmalloc barfs on "unusual" numbers. Check here */
1908 if (len
> 64 * 1024 * 1024 || (hdr
= vmalloc(len
)) == NULL
)
1909 return ERR_PTR(-ENOMEM
);
1911 if (copy_from_user(hdr
, umod
, len
) != 0) {
1916 /* Sanity checks against insmoding binaries or wrong arch,
1917 weird elf version */
1918 if (memcmp(hdr
->e_ident
, ELFMAG
, SELFMAG
) != 0
1919 || hdr
->e_type
!= ET_REL
1920 || !elf_check_arch(hdr
)
1921 || hdr
->e_shentsize
!= sizeof(*sechdrs
)) {
1926 if (len
< hdr
->e_shoff
+ hdr
->e_shnum
* sizeof(Elf_Shdr
))
1929 /* Convenience variables */
1930 sechdrs
= (void *)hdr
+ hdr
->e_shoff
;
1931 secstrings
= (void *)hdr
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
1932 sechdrs
[0].sh_addr
= 0;
1934 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
1935 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
1936 && len
< sechdrs
[i
].sh_offset
+ sechdrs
[i
].sh_size
)
1939 /* Mark all sections sh_addr with their address in the
1941 sechdrs
[i
].sh_addr
= (size_t)hdr
+ sechdrs
[i
].sh_offset
;
1943 /* Internal symbols and strings. */
1944 if (sechdrs
[i
].sh_type
== SHT_SYMTAB
) {
1946 strindex
= sechdrs
[i
].sh_link
;
1947 strtab
= (char *)hdr
+ sechdrs
[strindex
].sh_offset
;
1949 #ifndef CONFIG_MODULE_UNLOAD
1950 /* Don't load .exit sections */
1951 if (strstarts(secstrings
+sechdrs
[i
].sh_name
, ".exit"))
1952 sechdrs
[i
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1956 modindex
= find_sec(hdr
, sechdrs
, secstrings
,
1957 ".gnu.linkonce.this_module");
1959 printk(KERN_WARNING
"No module found in object\n");
1963 /* This is temporary: point mod into copy of data. */
1964 mod
= (void *)sechdrs
[modindex
].sh_addr
;
1966 if (symindex
== 0) {
1967 printk(KERN_WARNING
"%s: module has no symbols (stripped?)\n",
1973 versindex
= find_sec(hdr
, sechdrs
, secstrings
, "__versions");
1974 infoindex
= find_sec(hdr
, sechdrs
, secstrings
, ".modinfo");
1975 pcpuindex
= find_pcpusec(hdr
, sechdrs
, secstrings
);
1977 /* Don't keep modinfo and version sections. */
1978 sechdrs
[infoindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1979 sechdrs
[versindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1980 #ifdef CONFIG_KALLSYMS
1981 /* Keep symbol and string tables for decoding later. */
1982 sechdrs
[symindex
].sh_flags
|= SHF_ALLOC
;
1983 sechdrs
[strindex
].sh_flags
|= SHF_ALLOC
;
1986 /* Check module struct version now, before we try to use module. */
1987 if (!check_modstruct_version(sechdrs
, versindex
, mod
)) {
1992 modmagic
= get_modinfo(sechdrs
, infoindex
, "vermagic");
1993 /* This is allowed: modprobe --force will invalidate it. */
1995 err
= try_to_force_load(mod
, "bad vermagic");
1998 } else if (!same_magic(modmagic
, vermagic
, versindex
)) {
1999 printk(KERN_ERR
"%s: version magic '%s' should be '%s'\n",
2000 mod
->name
, modmagic
, vermagic
);
2005 staging
= get_modinfo(sechdrs
, infoindex
, "staging");
2007 add_taint_module(mod
, TAINT_CRAP
);
2008 printk(KERN_WARNING
"%s: module is from the staging directory,"
2009 " the quality is unknown, you have been warned.\n",
2013 /* Now copy in args */
2014 args
= strndup_user(uargs
, ~0UL >> 1);
2016 err
= PTR_ERR(args
);
2020 if (find_module(mod
->name
)) {
2025 mod
->state
= MODULE_STATE_COMING
;
2027 /* Allow arches to frob section contents and sizes. */
2028 err
= module_frob_arch_sections(hdr
, sechdrs
, secstrings
, mod
);
2033 /* We have a special allocation for this section. */
2034 percpu
= percpu_modalloc(sechdrs
[pcpuindex
].sh_size
,
2035 sechdrs
[pcpuindex
].sh_addralign
,
2041 sechdrs
[pcpuindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2042 mod
->percpu
= percpu
;
2045 /* Determine total sizes, and put offsets in sh_entsize. For now
2046 this is done generically; there doesn't appear to be any
2047 special cases for the architectures. */
2048 layout_sections(mod
, hdr
, sechdrs
, secstrings
);
2050 /* Do the allocs. */
2051 ptr
= module_alloc_update_bounds(mod
->core_size
);
2056 memset(ptr
, 0, mod
->core_size
);
2057 mod
->module_core
= ptr
;
2059 ptr
= module_alloc_update_bounds(mod
->init_size
);
2060 if (!ptr
&& mod
->init_size
) {
2064 memset(ptr
, 0, mod
->init_size
);
2065 mod
->module_init
= ptr
;
2067 /* Transfer each section which specifies SHF_ALLOC */
2068 DEBUGP("final section addresses:\n");
2069 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
2072 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
2075 if (sechdrs
[i
].sh_entsize
& INIT_OFFSET_MASK
)
2076 dest
= mod
->module_init
2077 + (sechdrs
[i
].sh_entsize
& ~INIT_OFFSET_MASK
);
2079 dest
= mod
->module_core
+ sechdrs
[i
].sh_entsize
;
2081 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
)
2082 memcpy(dest
, (void *)sechdrs
[i
].sh_addr
,
2083 sechdrs
[i
].sh_size
);
2084 /* Update sh_addr to point to copy in image. */
2085 sechdrs
[i
].sh_addr
= (unsigned long)dest
;
2086 DEBUGP("\t0x%lx %s\n", sechdrs
[i
].sh_addr
, secstrings
+ sechdrs
[i
].sh_name
);
2088 /* Module has been moved. */
2089 mod
= (void *)sechdrs
[modindex
].sh_addr
;
2091 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2092 mod
->refptr
= percpu_modalloc(sizeof(local_t
), __alignof__(local_t
),
2099 /* Now we've moved module, initialize linked lists, etc. */
2100 module_unload_init(mod
);
2102 /* add kobject, so we can reference it. */
2103 err
= mod_sysfs_init(mod
);
2107 /* Set up license info based on the info section */
2108 set_license(mod
, get_modinfo(sechdrs
, infoindex
, "license"));
2111 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2112 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2113 * using GPL-only symbols it needs.
2115 if (strcmp(mod
->name
, "ndiswrapper") == 0)
2116 add_taint(TAINT_PROPRIETARY_MODULE
);
2118 /* driverloader was caught wrongly pretending to be under GPL */
2119 if (strcmp(mod
->name
, "driverloader") == 0)
2120 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
2122 /* Set up MODINFO_ATTR fields */
2123 setup_modinfo(mod
, sechdrs
, infoindex
);
2125 /* Fix up syms, so that st_value is a pointer to location. */
2126 err
= simplify_symbols(sechdrs
, symindex
, strtab
, versindex
, pcpuindex
,
2131 /* Now we've got everything in the final locations, we can
2132 * find optional sections. */
2133 mod
->kp
= section_objs(hdr
, sechdrs
, secstrings
, "__param",
2134 sizeof(*mod
->kp
), &mod
->num_kp
);
2135 mod
->syms
= section_objs(hdr
, sechdrs
, secstrings
, "__ksymtab",
2136 sizeof(*mod
->syms
), &mod
->num_syms
);
2137 mod
->crcs
= section_addr(hdr
, sechdrs
, secstrings
, "__kcrctab");
2138 mod
->gpl_syms
= section_objs(hdr
, sechdrs
, secstrings
, "__ksymtab_gpl",
2139 sizeof(*mod
->gpl_syms
),
2140 &mod
->num_gpl_syms
);
2141 mod
->gpl_crcs
= section_addr(hdr
, sechdrs
, secstrings
, "__kcrctab_gpl");
2142 mod
->gpl_future_syms
= section_objs(hdr
, sechdrs
, secstrings
,
2143 "__ksymtab_gpl_future",
2144 sizeof(*mod
->gpl_future_syms
),
2145 &mod
->num_gpl_future_syms
);
2146 mod
->gpl_future_crcs
= section_addr(hdr
, sechdrs
, secstrings
,
2147 "__kcrctab_gpl_future");
2149 #ifdef CONFIG_UNUSED_SYMBOLS
2150 mod
->unused_syms
= section_objs(hdr
, sechdrs
, secstrings
,
2152 sizeof(*mod
->unused_syms
),
2153 &mod
->num_unused_syms
);
2154 mod
->unused_crcs
= section_addr(hdr
, sechdrs
, secstrings
,
2155 "__kcrctab_unused");
2156 mod
->unused_gpl_syms
= section_objs(hdr
, sechdrs
, secstrings
,
2157 "__ksymtab_unused_gpl",
2158 sizeof(*mod
->unused_gpl_syms
),
2159 &mod
->num_unused_gpl_syms
);
2160 mod
->unused_gpl_crcs
= section_addr(hdr
, sechdrs
, secstrings
,
2161 "__kcrctab_unused_gpl");
2164 #ifdef CONFIG_MARKERS
2165 mod
->markers
= section_objs(hdr
, sechdrs
, secstrings
, "__markers",
2166 sizeof(*mod
->markers
), &mod
->num_markers
);
2168 #ifdef CONFIG_TRACEPOINTS
2169 mod
->tracepoints
= section_objs(hdr
, sechdrs
, secstrings
,
2171 sizeof(*mod
->tracepoints
),
2172 &mod
->num_tracepoints
);
2174 #ifdef CONFIG_EVENT_TRACING
2175 mod
->trace_events
= section_objs(hdr
, sechdrs
, secstrings
,
2177 sizeof(*mod
->trace_events
),
2178 &mod
->num_trace_events
);
2180 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
2181 /* sechdrs[0].sh_size is always zero */
2182 mod
->ftrace_callsites
= section_objs(hdr
, sechdrs
, secstrings
,
2184 sizeof(*mod
->ftrace_callsites
),
2185 &mod
->num_ftrace_callsites
);
2187 #ifdef CONFIG_MODVERSIONS
2188 if ((mod
->num_syms
&& !mod
->crcs
)
2189 || (mod
->num_gpl_syms
&& !mod
->gpl_crcs
)
2190 || (mod
->num_gpl_future_syms
&& !mod
->gpl_future_crcs
)
2191 #ifdef CONFIG_UNUSED_SYMBOLS
2192 || (mod
->num_unused_syms
&& !mod
->unused_crcs
)
2193 || (mod
->num_unused_gpl_syms
&& !mod
->unused_gpl_crcs
)
2196 err
= try_to_force_load(mod
,
2197 "no versions for exported symbols");
2203 /* Now do relocations. */
2204 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
2205 const char *strtab
= (char *)sechdrs
[strindex
].sh_addr
;
2206 unsigned int info
= sechdrs
[i
].sh_info
;
2208 /* Not a valid relocation section? */
2209 if (info
>= hdr
->e_shnum
)
2212 /* Don't bother with non-allocated sections */
2213 if (!(sechdrs
[info
].sh_flags
& SHF_ALLOC
))
2216 if (sechdrs
[i
].sh_type
== SHT_REL
)
2217 err
= apply_relocate(sechdrs
, strtab
, symindex
, i
,mod
);
2218 else if (sechdrs
[i
].sh_type
== SHT_RELA
)
2219 err
= apply_relocate_add(sechdrs
, strtab
, symindex
, i
,
2225 /* Find duplicate symbols */
2226 err
= verify_export_symbols(mod
);
2230 /* Set up and sort exception table */
2231 mod
->extable
= section_objs(hdr
, sechdrs
, secstrings
, "__ex_table",
2232 sizeof(*mod
->extable
), &mod
->num_exentries
);
2233 sort_extable(mod
->extable
, mod
->extable
+ mod
->num_exentries
);
2235 /* Finally, copy percpu area over. */
2236 percpu_modcopy(mod
->percpu
, (void *)sechdrs
[pcpuindex
].sh_addr
,
2237 sechdrs
[pcpuindex
].sh_size
);
2239 add_kallsyms(mod
, sechdrs
, symindex
, strindex
, secstrings
);
2242 struct _ddebug
*debug
;
2243 unsigned int num_debug
;
2245 debug
= section_objs(hdr
, sechdrs
, secstrings
, "__verbose",
2246 sizeof(*debug
), &num_debug
);
2248 dynamic_debug_setup(debug
, num_debug
);
2251 err
= module_finalize(hdr
, sechdrs
, mod
);
2255 /* flush the icache in correct context */
2260 * Flush the instruction cache, since we've played with text.
2261 * Do it before processing of module parameters, so the module
2262 * can provide parameter accessor functions of its own.
2264 if (mod
->module_init
)
2265 flush_icache_range((unsigned long)mod
->module_init
,
2266 (unsigned long)mod
->module_init
2268 flush_icache_range((unsigned long)mod
->module_core
,
2269 (unsigned long)mod
->module_core
+ mod
->core_size
);
2274 if (section_addr(hdr
, sechdrs
, secstrings
, "__obsparm"))
2275 printk(KERN_WARNING
"%s: Ignoring obsolete parameters\n",
2278 /* Now sew it into the lists so we can get lockdep and oops
2279 * info during argument parsing. Noone should access us, since
2280 * strong_try_module_get() will fail.
2281 * lockdep/oops can run asynchronous, so use the RCU list insertion
2282 * function to insert in a way safe to concurrent readers.
2283 * The mutex protects against concurrent writers.
2285 list_add_rcu(&mod
->list
, &modules
);
2287 err
= parse_args(mod
->name
, mod
->args
, mod
->kp
, mod
->num_kp
, NULL
);
2291 err
= mod_sysfs_setup(mod
, mod
->kp
, mod
->num_kp
);
2294 add_sect_attrs(mod
, hdr
->e_shnum
, secstrings
, sechdrs
);
2295 add_notes_attrs(mod
, hdr
->e_shnum
, secstrings
, sechdrs
);
2297 /* Get rid of temporary copy */
2304 /* Unlink carefully: kallsyms could be walking list. */
2305 list_del_rcu(&mod
->list
);
2306 synchronize_sched();
2307 module_arch_cleanup(mod
);
2309 kobject_del(&mod
->mkobj
.kobj
);
2310 kobject_put(&mod
->mkobj
.kobj
);
2312 module_unload_free(mod
);
2313 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2315 percpu_modfree(mod
->refptr
);
2317 module_free(mod
, mod
->module_init
);
2319 module_free(mod
, mod
->module_core
);
2320 /* mod will be freed with core. Don't access it beyond this line! */
2323 percpu_modfree(percpu
);
2328 return ERR_PTR(err
);
2331 printk(KERN_ERR
"Module len %lu truncated\n", len
);
2336 /* This is where the real work happens */
2337 SYSCALL_DEFINE3(init_module
, void __user
*, umod
,
2338 unsigned long, len
, const char __user
*, uargs
)
2343 /* Must have permission */
2344 if (!capable(CAP_SYS_MODULE
) || modules_disabled
)
2347 /* Only one module load at a time, please */
2348 if (mutex_lock_interruptible(&module_mutex
) != 0)
2351 /* Do all the hard work */
2352 mod
= load_module(umod
, len
, uargs
);
2354 mutex_unlock(&module_mutex
);
2355 return PTR_ERR(mod
);
2358 /* Drop lock so they can recurse */
2359 mutex_unlock(&module_mutex
);
2361 blocking_notifier_call_chain(&module_notify_list
,
2362 MODULE_STATE_COMING
, mod
);
2364 /* Start the module */
2365 if (mod
->init
!= NULL
)
2366 ret
= do_one_initcall(mod
->init
);
2368 /* Init routine failed: abort. Try to protect us from
2369 buggy refcounters. */
2370 mod
->state
= MODULE_STATE_GOING
;
2371 synchronize_sched();
2373 blocking_notifier_call_chain(&module_notify_list
,
2374 MODULE_STATE_GOING
, mod
);
2375 mutex_lock(&module_mutex
);
2377 mutex_unlock(&module_mutex
);
2378 wake_up(&module_wq
);
2382 printk(KERN_WARNING
"%s: '%s'->init suspiciously returned %d, "
2383 "it should follow 0/-E convention\n"
2384 KERN_WARNING
"%s: loading module anyway...\n",
2385 __func__
, mod
->name
, ret
,
2390 /* Now it's a first class citizen! Wake up anyone waiting for it. */
2391 mod
->state
= MODULE_STATE_LIVE
;
2392 wake_up(&module_wq
);
2393 blocking_notifier_call_chain(&module_notify_list
,
2394 MODULE_STATE_LIVE
, mod
);
2396 /* We need to finish all async code before the module init sequence is done */
2397 async_synchronize_full();
2399 mutex_lock(&module_mutex
);
2400 /* Drop initial reference. */
2402 module_free(mod
, mod
->module_init
);
2403 mod
->module_init
= NULL
;
2405 mod
->init_text_size
= 0;
2406 mutex_unlock(&module_mutex
);
2411 static inline int within(unsigned long addr
, void *start
, unsigned long size
)
2413 return ((void *)addr
>= start
&& (void *)addr
< start
+ size
);
2416 #ifdef CONFIG_KALLSYMS
2418 * This ignores the intensely annoying "mapping symbols" found
2419 * in ARM ELF files: $a, $t and $d.
2421 static inline int is_arm_mapping_symbol(const char *str
)
2423 return str
[0] == '$' && strchr("atd", str
[1])
2424 && (str
[2] == '\0' || str
[2] == '.');
2427 static const char *get_ksymbol(struct module
*mod
,
2429 unsigned long *size
,
2430 unsigned long *offset
)
2432 unsigned int i
, best
= 0;
2433 unsigned long nextval
;
2435 /* At worse, next value is at end of module */
2436 if (within_module_init(addr
, mod
))
2437 nextval
= (unsigned long)mod
->module_init
+mod
->init_text_size
;
2439 nextval
= (unsigned long)mod
->module_core
+mod
->core_text_size
;
2441 /* Scan for closest preceeding symbol, and next symbol. (ELF
2442 starts real symbols at 1). */
2443 for (i
= 1; i
< mod
->num_symtab
; i
++) {
2444 if (mod
->symtab
[i
].st_shndx
== SHN_UNDEF
)
2447 /* We ignore unnamed symbols: they're uninformative
2448 * and inserted at a whim. */
2449 if (mod
->symtab
[i
].st_value
<= addr
2450 && mod
->symtab
[i
].st_value
> mod
->symtab
[best
].st_value
2451 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
2452 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
2454 if (mod
->symtab
[i
].st_value
> addr
2455 && mod
->symtab
[i
].st_value
< nextval
2456 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
2457 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
2458 nextval
= mod
->symtab
[i
].st_value
;
2465 *size
= nextval
- mod
->symtab
[best
].st_value
;
2467 *offset
= addr
- mod
->symtab
[best
].st_value
;
2468 return mod
->strtab
+ mod
->symtab
[best
].st_name
;
2471 /* For kallsyms to ask for address resolution. NULL means not found. Careful
2472 * not to lock to avoid deadlock on oopses, simply disable preemption. */
2473 const char *module_address_lookup(unsigned long addr
,
2474 unsigned long *size
,
2475 unsigned long *offset
,
2480 const char *ret
= NULL
;
2483 list_for_each_entry_rcu(mod
, &modules
, list
) {
2484 if (within_module_init(addr
, mod
) ||
2485 within_module_core(addr
, mod
)) {
2487 *modname
= mod
->name
;
2488 ret
= get_ksymbol(mod
, addr
, size
, offset
);
2492 /* Make a copy in here where it's safe */
2494 strncpy(namebuf
, ret
, KSYM_NAME_LEN
- 1);
2501 int lookup_module_symbol_name(unsigned long addr
, char *symname
)
2506 list_for_each_entry_rcu(mod
, &modules
, list
) {
2507 if (within_module_init(addr
, mod
) ||
2508 within_module_core(addr
, mod
)) {
2511 sym
= get_ksymbol(mod
, addr
, NULL
, NULL
);
2514 strlcpy(symname
, sym
, KSYM_NAME_LEN
);
2524 int lookup_module_symbol_attrs(unsigned long addr
, unsigned long *size
,
2525 unsigned long *offset
, char *modname
, char *name
)
2530 list_for_each_entry_rcu(mod
, &modules
, list
) {
2531 if (within_module_init(addr
, mod
) ||
2532 within_module_core(addr
, mod
)) {
2535 sym
= get_ksymbol(mod
, addr
, size
, offset
);
2539 strlcpy(modname
, mod
->name
, MODULE_NAME_LEN
);
2541 strlcpy(name
, sym
, KSYM_NAME_LEN
);
2551 int module_get_kallsym(unsigned int symnum
, unsigned long *value
, char *type
,
2552 char *name
, char *module_name
, int *exported
)
2557 list_for_each_entry_rcu(mod
, &modules
, list
) {
2558 if (symnum
< mod
->num_symtab
) {
2559 *value
= mod
->symtab
[symnum
].st_value
;
2560 *type
= mod
->symtab
[symnum
].st_info
;
2561 strlcpy(name
, mod
->strtab
+ mod
->symtab
[symnum
].st_name
,
2563 strlcpy(module_name
, mod
->name
, MODULE_NAME_LEN
);
2564 *exported
= is_exported(name
, *value
, mod
);
2568 symnum
-= mod
->num_symtab
;
2574 static unsigned long mod_find_symname(struct module
*mod
, const char *name
)
2578 for (i
= 0; i
< mod
->num_symtab
; i
++)
2579 if (strcmp(name
, mod
->strtab
+mod
->symtab
[i
].st_name
) == 0 &&
2580 mod
->symtab
[i
].st_info
!= 'U')
2581 return mod
->symtab
[i
].st_value
;
2585 /* Look for this name: can be of form module:name. */
2586 unsigned long module_kallsyms_lookup_name(const char *name
)
2590 unsigned long ret
= 0;
2592 /* Don't lock: we're in enough trouble already. */
2594 if ((colon
= strchr(name
, ':')) != NULL
) {
2596 if ((mod
= find_module(name
)) != NULL
)
2597 ret
= mod_find_symname(mod
, colon
+1);
2600 list_for_each_entry_rcu(mod
, &modules
, list
)
2601 if ((ret
= mod_find_symname(mod
, name
)) != 0)
2608 int module_kallsyms_on_each_symbol(int (*fn
)(void *, const char *,
2609 struct module
*, unsigned long),
2616 list_for_each_entry(mod
, &modules
, list
) {
2617 for (i
= 0; i
< mod
->num_symtab
; i
++) {
2618 ret
= fn(data
, mod
->strtab
+ mod
->symtab
[i
].st_name
,
2619 mod
, mod
->symtab
[i
].st_value
);
2626 #endif /* CONFIG_KALLSYMS */
2628 static char *module_flags(struct module
*mod
, char *buf
)
2633 mod
->state
== MODULE_STATE_GOING
||
2634 mod
->state
== MODULE_STATE_COMING
) {
2636 if (mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
))
2638 if (mod
->taints
& (1 << TAINT_FORCED_MODULE
))
2640 if (mod
->taints
& (1 << TAINT_CRAP
))
2643 * TAINT_FORCED_RMMOD: could be added.
2644 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2648 /* Show a - for module-is-being-unloaded */
2649 if (mod
->state
== MODULE_STATE_GOING
)
2651 /* Show a + for module-is-being-loaded */
2652 if (mod
->state
== MODULE_STATE_COMING
)
2661 #ifdef CONFIG_PROC_FS
2662 /* Called by the /proc file system to return a list of modules. */
2663 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
2665 mutex_lock(&module_mutex
);
2666 return seq_list_start(&modules
, *pos
);
2669 static void *m_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
2671 return seq_list_next(p
, &modules
, pos
);
2674 static void m_stop(struct seq_file
*m
, void *p
)
2676 mutex_unlock(&module_mutex
);
2679 static int m_show(struct seq_file
*m
, void *p
)
2681 struct module
*mod
= list_entry(p
, struct module
, list
);
2684 seq_printf(m
, "%s %u",
2685 mod
->name
, mod
->init_size
+ mod
->core_size
);
2686 print_unload_info(m
, mod
);
2688 /* Informative for users. */
2689 seq_printf(m
, " %s",
2690 mod
->state
== MODULE_STATE_GOING
? "Unloading":
2691 mod
->state
== MODULE_STATE_COMING
? "Loading":
2693 /* Used by oprofile and other similar tools. */
2694 seq_printf(m
, " 0x%p", mod
->module_core
);
2698 seq_printf(m
, " %s", module_flags(mod
, buf
));
2700 seq_printf(m
, "\n");
2704 /* Format: modulename size refcount deps address
2706 Where refcount is a number or -, and deps is a comma-separated list
2709 static const struct seq_operations modules_op
= {
2716 static int modules_open(struct inode
*inode
, struct file
*file
)
2718 return seq_open(file
, &modules_op
);
2721 static const struct file_operations proc_modules_operations
= {
2722 .open
= modules_open
,
2724 .llseek
= seq_lseek
,
2725 .release
= seq_release
,
2728 static int __init
proc_modules_init(void)
2730 proc_create("modules", 0, NULL
, &proc_modules_operations
);
2733 module_init(proc_modules_init
);
2736 /* Given an address, look for it in the module exception tables. */
2737 const struct exception_table_entry
*search_module_extables(unsigned long addr
)
2739 const struct exception_table_entry
*e
= NULL
;
2743 list_for_each_entry_rcu(mod
, &modules
, list
) {
2744 if (mod
->num_exentries
== 0)
2747 e
= search_extable(mod
->extable
,
2748 mod
->extable
+ mod
->num_exentries
- 1,
2755 /* Now, if we found one, we are running inside it now, hence
2756 we cannot unload the module, hence no refcnt needed. */
2761 * is_module_address - is this address inside a module?
2762 * @addr: the address to check.
2764 * See is_module_text_address() if you simply want to see if the address
2765 * is code (not data).
2767 bool is_module_address(unsigned long addr
)
2772 ret
= __module_address(addr
) != NULL
;
2779 * __module_address - get the module which contains an address.
2780 * @addr: the address.
2782 * Must be called with preempt disabled or module mutex held so that
2783 * module doesn't get freed during this.
2785 struct module
*__module_address(unsigned long addr
)
2789 if (addr
< module_addr_min
|| addr
> module_addr_max
)
2792 list_for_each_entry_rcu(mod
, &modules
, list
)
2793 if (within_module_core(addr
, mod
)
2794 || within_module_init(addr
, mod
))
2798 EXPORT_SYMBOL_GPL(__module_address
);
2801 * is_module_text_address - is this address inside module code?
2802 * @addr: the address to check.
2804 * See is_module_address() if you simply want to see if the address is
2805 * anywhere in a module. See kernel_text_address() for testing if an
2806 * address corresponds to kernel or module code.
2808 bool is_module_text_address(unsigned long addr
)
2813 ret
= __module_text_address(addr
) != NULL
;
2820 * __module_text_address - get the module whose code contains an address.
2821 * @addr: the address.
2823 * Must be called with preempt disabled or module mutex held so that
2824 * module doesn't get freed during this.
2826 struct module
*__module_text_address(unsigned long addr
)
2828 struct module
*mod
= __module_address(addr
);
2830 /* Make sure it's within the text section. */
2831 if (!within(addr
, mod
->module_init
, mod
->init_text_size
)
2832 && !within(addr
, mod
->module_core
, mod
->core_text_size
))
2837 EXPORT_SYMBOL_GPL(__module_text_address
);
2839 /* Don't grab lock, we're oopsing. */
2840 void print_modules(void)
2845 printk("Modules linked in:");
2846 /* Most callers should already have preempt disabled, but make sure */
2848 list_for_each_entry_rcu(mod
, &modules
, list
)
2849 printk(" %s%s", mod
->name
, module_flags(mod
, buf
));
2851 if (last_unloaded_module
[0])
2852 printk(" [last unloaded: %s]", last_unloaded_module
);
2856 #ifdef CONFIG_MODVERSIONS
2857 /* Generate the signature for all relevant module structures here.
2858 * If these change, we don't want to try to parse the module. */
2859 void module_layout(struct module
*mod
,
2860 struct modversion_info
*ver
,
2861 struct kernel_param
*kp
,
2862 struct kernel_symbol
*ks
,
2863 struct marker
*marker
,
2864 struct tracepoint
*tp
)
2867 EXPORT_SYMBOL(module_layout
);
2870 #ifdef CONFIG_MARKERS
2871 void module_update_markers(void)
2875 mutex_lock(&module_mutex
);
2876 list_for_each_entry(mod
, &modules
, list
)
2878 marker_update_probe_range(mod
->markers
,
2879 mod
->markers
+ mod
->num_markers
);
2880 mutex_unlock(&module_mutex
);
2884 #ifdef CONFIG_TRACEPOINTS
2885 void module_update_tracepoints(void)
2889 mutex_lock(&module_mutex
);
2890 list_for_each_entry(mod
, &modules
, list
)
2892 tracepoint_update_probe_range(mod
->tracepoints
,
2893 mod
->tracepoints
+ mod
->num_tracepoints
);
2894 mutex_unlock(&module_mutex
);
2898 * Returns 0 if current not found.
2899 * Returns 1 if current found.
2901 int module_get_iter_tracepoints(struct tracepoint_iter
*iter
)
2903 struct module
*iter_mod
;
2906 mutex_lock(&module_mutex
);
2907 list_for_each_entry(iter_mod
, &modules
, list
) {
2908 if (!iter_mod
->taints
) {
2910 * Sorted module list
2912 if (iter_mod
< iter
->module
)
2914 else if (iter_mod
> iter
->module
)
2915 iter
->tracepoint
= NULL
;
2916 found
= tracepoint_get_iter_range(&iter
->tracepoint
,
2917 iter_mod
->tracepoints
,
2918 iter_mod
->tracepoints
2919 + iter_mod
->num_tracepoints
);
2921 iter
->module
= iter_mod
;
2926 mutex_unlock(&module_mutex
);