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 <asm/mmu_context.h>
51 #include <linux/license.h>
52 #include <asm/sections.h>
53 #include <linux/tracepoint.h>
54 #include <linux/ftrace.h>
55 #include <linux/async.h>
56 #include <linux/percpu.h>
57 #include <linux/kmemleak.h>
59 #define CREATE_TRACE_POINTS
60 #include <trace/events/module.h>
65 #define DEBUGP(fmt , a...)
68 #ifndef ARCH_SHF_SMALL
69 #define ARCH_SHF_SMALL 0
72 /* If this is set, the section belongs in the init part of the module */
73 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
75 /* List of modules, protected by module_mutex or preempt_disable
76 * (delete uses stop_machine/add uses RCU list operations). */
77 DEFINE_MUTEX(module_mutex
);
78 EXPORT_SYMBOL_GPL(module_mutex
);
79 static LIST_HEAD(modules
);
81 /* Block module loading/unloading? */
82 int modules_disabled
= 0;
84 /* Waiting for a module to finish initializing? */
85 static DECLARE_WAIT_QUEUE_HEAD(module_wq
);
87 static BLOCKING_NOTIFIER_HEAD(module_notify_list
);
89 /* Bounds of module allocation, for speeding __module_address */
90 static unsigned long module_addr_min
= -1UL, module_addr_max
= 0;
92 int register_module_notifier(struct notifier_block
* nb
)
94 return blocking_notifier_chain_register(&module_notify_list
, nb
);
96 EXPORT_SYMBOL(register_module_notifier
);
98 int unregister_module_notifier(struct notifier_block
* nb
)
100 return blocking_notifier_chain_unregister(&module_notify_list
, nb
);
102 EXPORT_SYMBOL(unregister_module_notifier
);
104 /* We require a truly strong try_module_get(): 0 means failure due to
105 ongoing or failed initialization etc. */
106 static inline int strong_try_module_get(struct module
*mod
)
108 if (mod
&& mod
->state
== MODULE_STATE_COMING
)
110 if (try_module_get(mod
))
116 static inline void add_taint_module(struct module
*mod
, unsigned flag
)
119 mod
->taints
|= (1U << flag
);
123 * A thread that wants to hold a reference to a module only while it
124 * is running can call this to safely exit. nfsd and lockd use this.
126 void __module_put_and_exit(struct module
*mod
, long code
)
131 EXPORT_SYMBOL(__module_put_and_exit
);
133 /* Find a module section: 0 means not found. */
134 static unsigned int find_sec(Elf_Ehdr
*hdr
,
136 const char *secstrings
,
141 for (i
= 1; i
< hdr
->e_shnum
; i
++)
142 /* Alloc bit cleared means "ignore it." */
143 if ((sechdrs
[i
].sh_flags
& SHF_ALLOC
)
144 && strcmp(secstrings
+sechdrs
[i
].sh_name
, name
) == 0)
149 /* Find a module section, or NULL. */
150 static void *section_addr(Elf_Ehdr
*hdr
, Elf_Shdr
*shdrs
,
151 const char *secstrings
, const char *name
)
153 /* Section 0 has sh_addr 0. */
154 return (void *)shdrs
[find_sec(hdr
, shdrs
, secstrings
, name
)].sh_addr
;
157 /* Find a module section, or NULL. Fill in number of "objects" in section. */
158 static void *section_objs(Elf_Ehdr
*hdr
,
160 const char *secstrings
,
165 unsigned int sec
= find_sec(hdr
, sechdrs
, secstrings
, name
);
167 /* Section 0 has sh_addr 0 and sh_size 0. */
168 *num
= sechdrs
[sec
].sh_size
/ object_size
;
169 return (void *)sechdrs
[sec
].sh_addr
;
172 /* Provided by the linker */
173 extern const struct kernel_symbol __start___ksymtab
[];
174 extern const struct kernel_symbol __stop___ksymtab
[];
175 extern const struct kernel_symbol __start___ksymtab_gpl
[];
176 extern const struct kernel_symbol __stop___ksymtab_gpl
[];
177 extern const struct kernel_symbol __start___ksymtab_gpl_future
[];
178 extern const struct kernel_symbol __stop___ksymtab_gpl_future
[];
179 extern const struct kernel_symbol __start___ksymtab_gpl_future
[];
180 extern const struct kernel_symbol __stop___ksymtab_gpl_future
[];
181 extern const unsigned long __start___kcrctab
[];
182 extern const unsigned long __start___kcrctab_gpl
[];
183 extern const unsigned long __start___kcrctab_gpl_future
[];
184 #ifdef CONFIG_UNUSED_SYMBOLS
185 extern const struct kernel_symbol __start___ksymtab_unused
[];
186 extern const struct kernel_symbol __stop___ksymtab_unused
[];
187 extern const struct kernel_symbol __start___ksymtab_unused_gpl
[];
188 extern const struct kernel_symbol __stop___ksymtab_unused_gpl
[];
189 extern const unsigned long __start___kcrctab_unused
[];
190 extern const unsigned long __start___kcrctab_unused_gpl
[];
193 #ifndef CONFIG_MODVERSIONS
194 #define symversion(base, idx) NULL
196 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
199 static bool each_symbol_in_section(const struct symsearch
*arr
,
200 unsigned int arrsize
,
201 struct module
*owner
,
202 bool (*fn
)(const struct symsearch
*syms
,
203 struct module
*owner
,
204 unsigned int symnum
, void *data
),
209 for (j
= 0; j
< arrsize
; j
++) {
210 for (i
= 0; i
< arr
[j
].stop
- arr
[j
].start
; i
++)
211 if (fn(&arr
[j
], owner
, i
, data
))
218 /* Returns true as soon as fn returns true, otherwise false. */
219 bool each_symbol(bool (*fn
)(const struct symsearch
*arr
, struct module
*owner
,
220 unsigned int symnum
, void *data
), void *data
)
223 const struct symsearch arr
[] = {
224 { __start___ksymtab
, __stop___ksymtab
, __start___kcrctab
,
225 NOT_GPL_ONLY
, false },
226 { __start___ksymtab_gpl
, __stop___ksymtab_gpl
,
227 __start___kcrctab_gpl
,
229 { __start___ksymtab_gpl_future
, __stop___ksymtab_gpl_future
,
230 __start___kcrctab_gpl_future
,
231 WILL_BE_GPL_ONLY
, false },
232 #ifdef CONFIG_UNUSED_SYMBOLS
233 { __start___ksymtab_unused
, __stop___ksymtab_unused
,
234 __start___kcrctab_unused
,
235 NOT_GPL_ONLY
, true },
236 { __start___ksymtab_unused_gpl
, __stop___ksymtab_unused_gpl
,
237 __start___kcrctab_unused_gpl
,
242 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), NULL
, fn
, data
))
245 list_for_each_entry_rcu(mod
, &modules
, list
) {
246 struct symsearch arr
[] = {
247 { mod
->syms
, mod
->syms
+ mod
->num_syms
, mod
->crcs
,
248 NOT_GPL_ONLY
, false },
249 { mod
->gpl_syms
, mod
->gpl_syms
+ mod
->num_gpl_syms
,
252 { mod
->gpl_future_syms
,
253 mod
->gpl_future_syms
+ mod
->num_gpl_future_syms
,
254 mod
->gpl_future_crcs
,
255 WILL_BE_GPL_ONLY
, false },
256 #ifdef CONFIG_UNUSED_SYMBOLS
258 mod
->unused_syms
+ mod
->num_unused_syms
,
260 NOT_GPL_ONLY
, true },
261 { mod
->unused_gpl_syms
,
262 mod
->unused_gpl_syms
+ mod
->num_unused_gpl_syms
,
263 mod
->unused_gpl_crcs
,
268 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), mod
, fn
, data
))
273 EXPORT_SYMBOL_GPL(each_symbol
);
275 struct find_symbol_arg
{
282 struct module
*owner
;
283 const unsigned long *crc
;
284 const struct kernel_symbol
*sym
;
287 static bool find_symbol_in_section(const struct symsearch
*syms
,
288 struct module
*owner
,
289 unsigned int symnum
, void *data
)
291 struct find_symbol_arg
*fsa
= data
;
293 if (strcmp(syms
->start
[symnum
].name
, fsa
->name
) != 0)
297 if (syms
->licence
== GPL_ONLY
)
299 if (syms
->licence
== WILL_BE_GPL_ONLY
&& fsa
->warn
) {
300 printk(KERN_WARNING
"Symbol %s is being used "
301 "by a non-GPL module, which will not "
302 "be allowed in the future\n", fsa
->name
);
303 printk(KERN_WARNING
"Please see the file "
304 "Documentation/feature-removal-schedule.txt "
305 "in the kernel source tree for more details.\n");
309 #ifdef CONFIG_UNUSED_SYMBOLS
310 if (syms
->unused
&& fsa
->warn
) {
311 printk(KERN_WARNING
"Symbol %s is marked as UNUSED, "
312 "however this module is using it.\n", fsa
->name
);
314 "This symbol will go away in the future.\n");
316 "Please evalute if this is the right api to use and if "
317 "it really is, submit a report the linux kernel "
318 "mailinglist together with submitting your code for "
324 fsa
->crc
= symversion(syms
->crcs
, symnum
);
325 fsa
->sym
= &syms
->start
[symnum
];
329 /* Find a symbol and return it, along with, (optional) crc and
330 * (optional) module which owns it */
331 const struct kernel_symbol
*find_symbol(const char *name
,
332 struct module
**owner
,
333 const unsigned long **crc
,
337 struct find_symbol_arg fsa
;
343 if (each_symbol(find_symbol_in_section
, &fsa
)) {
351 DEBUGP("Failed to find symbol %s\n", name
);
354 EXPORT_SYMBOL_GPL(find_symbol
);
356 /* Search for module by name: must hold module_mutex. */
357 struct module
*find_module(const char *name
)
361 list_for_each_entry(mod
, &modules
, list
) {
362 if (strcmp(mod
->name
, name
) == 0)
367 EXPORT_SYMBOL_GPL(find_module
);
371 static inline void __percpu
*mod_percpu(struct module
*mod
)
376 static int percpu_modalloc(struct module
*mod
,
377 unsigned long size
, unsigned long align
)
379 if (align
> PAGE_SIZE
) {
380 printk(KERN_WARNING
"%s: per-cpu alignment %li > %li\n",
381 mod
->name
, align
, PAGE_SIZE
);
385 mod
->percpu
= __alloc_reserved_percpu(size
, align
);
388 "Could not allocate %lu bytes percpu data\n", size
);
391 mod
->percpu_size
= size
;
395 static void percpu_modfree(struct module
*mod
)
397 free_percpu(mod
->percpu
);
400 static unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
402 const char *secstrings
)
404 return find_sec(hdr
, sechdrs
, secstrings
, ".data.percpu");
407 static void percpu_modcopy(struct module
*mod
,
408 const void *from
, unsigned long size
)
412 for_each_possible_cpu(cpu
)
413 memcpy(per_cpu_ptr(mod
->percpu
, cpu
), from
, size
);
417 * is_module_percpu_address - test whether address is from module static percpu
418 * @addr: address to test
420 * Test whether @addr belongs to module static percpu area.
423 * %true if @addr is from module static percpu area
425 bool is_module_percpu_address(unsigned long addr
)
432 list_for_each_entry_rcu(mod
, &modules
, list
) {
433 if (!mod
->percpu_size
)
435 for_each_possible_cpu(cpu
) {
436 void *start
= per_cpu_ptr(mod
->percpu
, cpu
);
438 if ((void *)addr
>= start
&&
439 (void *)addr
< start
+ mod
->percpu_size
) {
450 #else /* ... !CONFIG_SMP */
452 static inline void __percpu
*mod_percpu(struct module
*mod
)
456 static inline int percpu_modalloc(struct module
*mod
,
457 unsigned long size
, unsigned long align
)
461 static inline void percpu_modfree(struct module
*mod
)
464 static inline unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
466 const char *secstrings
)
470 static inline void percpu_modcopy(struct module
*mod
,
471 const void *from
, unsigned long size
)
473 /* pcpusec should be 0, and size of that section should be 0. */
476 bool is_module_percpu_address(unsigned long addr
)
481 #endif /* CONFIG_SMP */
483 #define MODINFO_ATTR(field) \
484 static void setup_modinfo_##field(struct module *mod, const char *s) \
486 mod->field = kstrdup(s, GFP_KERNEL); \
488 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
489 struct module *mod, char *buffer) \
491 return sprintf(buffer, "%s\n", mod->field); \
493 static int modinfo_##field##_exists(struct module *mod) \
495 return mod->field != NULL; \
497 static void free_modinfo_##field(struct module *mod) \
502 static struct module_attribute modinfo_##field = { \
503 .attr = { .name = __stringify(field), .mode = 0444 }, \
504 .show = show_modinfo_##field, \
505 .setup = setup_modinfo_##field, \
506 .test = modinfo_##field##_exists, \
507 .free = free_modinfo_##field, \
510 MODINFO_ATTR(version
);
511 MODINFO_ATTR(srcversion
);
513 static char last_unloaded_module
[MODULE_NAME_LEN
+1];
515 #ifdef CONFIG_MODULE_UNLOAD
517 EXPORT_TRACEPOINT_SYMBOL(module_get
);
519 /* Init the unload section of the module. */
520 static void module_unload_init(struct module
*mod
)
524 INIT_LIST_HEAD(&mod
->modules_which_use_me
);
525 for_each_possible_cpu(cpu
) {
526 per_cpu_ptr(mod
->refptr
, cpu
)->incs
= 0;
527 per_cpu_ptr(mod
->refptr
, cpu
)->decs
= 0;
530 /* Hold reference count during initialization. */
531 __this_cpu_write(mod
->refptr
->incs
, 1);
532 /* Backwards compatibility macros put refcount during init. */
533 mod
->waiter
= current
;
536 /* modules using other modules */
539 struct list_head list
;
540 struct module
*module_which_uses
;
543 /* Does a already use b? */
544 static int already_uses(struct module
*a
, struct module
*b
)
546 struct module_use
*use
;
548 list_for_each_entry(use
, &b
->modules_which_use_me
, list
) {
549 if (use
->module_which_uses
== a
) {
550 DEBUGP("%s uses %s!\n", a
->name
, b
->name
);
554 DEBUGP("%s does not use %s!\n", a
->name
, b
->name
);
558 /* Module a uses b */
559 int use_module(struct module
*a
, struct module
*b
)
561 struct module_use
*use
;
564 if (b
== NULL
|| already_uses(a
, b
)) return 1;
566 /* If we're interrupted or time out, we fail. */
567 if (wait_event_interruptible_timeout(
568 module_wq
, (err
= strong_try_module_get(b
)) != -EBUSY
,
570 printk("%s: gave up waiting for init of module %s.\n",
575 /* If strong_try_module_get() returned a different error, we fail. */
579 DEBUGP("Allocating new usage for %s.\n", a
->name
);
580 use
= kmalloc(sizeof(*use
), GFP_ATOMIC
);
582 printk("%s: out of memory loading\n", a
->name
);
587 use
->module_which_uses
= a
;
588 list_add(&use
->list
, &b
->modules_which_use_me
);
589 no_warn
= sysfs_create_link(b
->holders_dir
, &a
->mkobj
.kobj
, a
->name
);
592 EXPORT_SYMBOL_GPL(use_module
);
594 /* Clear the unload stuff of the module. */
595 static void module_unload_free(struct module
*mod
)
599 list_for_each_entry(i
, &modules
, list
) {
600 struct module_use
*use
;
602 list_for_each_entry(use
, &i
->modules_which_use_me
, list
) {
603 if (use
->module_which_uses
== mod
) {
604 DEBUGP("%s unusing %s\n", mod
->name
, i
->name
);
606 list_del(&use
->list
);
608 sysfs_remove_link(i
->holders_dir
, mod
->name
);
609 /* There can be at most one match. */
616 #ifdef CONFIG_MODULE_FORCE_UNLOAD
617 static inline int try_force_unload(unsigned int flags
)
619 int ret
= (flags
& O_TRUNC
);
621 add_taint(TAINT_FORCED_RMMOD
);
625 static inline int try_force_unload(unsigned int flags
)
629 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
638 /* Whole machine is stopped with interrupts off when this runs. */
639 static int __try_stop_module(void *_sref
)
641 struct stopref
*sref
= _sref
;
643 /* If it's not unused, quit unless we're forcing. */
644 if (module_refcount(sref
->mod
) != 0) {
645 if (!(*sref
->forced
= try_force_unload(sref
->flags
)))
649 /* Mark it as dying. */
650 sref
->mod
->state
= MODULE_STATE_GOING
;
654 static int try_stop_module(struct module
*mod
, int flags
, int *forced
)
656 if (flags
& O_NONBLOCK
) {
657 struct stopref sref
= { mod
, flags
, forced
};
659 return stop_machine(__try_stop_module
, &sref
, NULL
);
661 /* We don't need to stop the machine for this. */
662 mod
->state
= MODULE_STATE_GOING
;
668 unsigned int module_refcount(struct module
*mod
)
670 unsigned int incs
= 0, decs
= 0;
673 for_each_possible_cpu(cpu
)
674 decs
+= per_cpu_ptr(mod
->refptr
, cpu
)->decs
;
676 * ensure the incs are added up after the decs.
677 * module_put ensures incs are visible before decs with smp_wmb.
679 * This 2-count scheme avoids the situation where the refcount
680 * for CPU0 is read, then CPU0 increments the module refcount,
681 * then CPU1 drops that refcount, then the refcount for CPU1 is
682 * read. We would record a decrement but not its corresponding
683 * increment so we would see a low count (disaster).
685 * Rare situation? But module_refcount can be preempted, and we
686 * might be tallying up 4096+ CPUs. So it is not impossible.
689 for_each_possible_cpu(cpu
)
690 incs
+= per_cpu_ptr(mod
->refptr
, cpu
)->incs
;
693 EXPORT_SYMBOL(module_refcount
);
695 /* This exists whether we can unload or not */
696 static void free_module(struct module
*mod
);
698 static void wait_for_zero_refcount(struct module
*mod
)
700 /* Since we might sleep for some time, release the mutex first */
701 mutex_unlock(&module_mutex
);
703 DEBUGP("Looking at refcount...\n");
704 set_current_state(TASK_UNINTERRUPTIBLE
);
705 if (module_refcount(mod
) == 0)
709 current
->state
= TASK_RUNNING
;
710 mutex_lock(&module_mutex
);
713 SYSCALL_DEFINE2(delete_module
, const char __user
*, name_user
,
717 char name
[MODULE_NAME_LEN
];
720 if (!capable(CAP_SYS_MODULE
) || modules_disabled
)
723 if (strncpy_from_user(name
, name_user
, MODULE_NAME_LEN
-1) < 0)
725 name
[MODULE_NAME_LEN
-1] = '\0';
727 if (mutex_lock_interruptible(&module_mutex
) != 0)
730 mod
= find_module(name
);
736 if (!list_empty(&mod
->modules_which_use_me
)) {
737 /* Other modules depend on us: get rid of them first. */
742 /* Doing init or already dying? */
743 if (mod
->state
!= MODULE_STATE_LIVE
) {
744 /* FIXME: if (force), slam module count and wake up
746 DEBUGP("%s already dying\n", mod
->name
);
751 /* If it has an init func, it must have an exit func to unload */
752 if (mod
->init
&& !mod
->exit
) {
753 forced
= try_force_unload(flags
);
755 /* This module can't be removed */
761 /* Set this up before setting mod->state */
762 mod
->waiter
= current
;
764 /* Stop the machine so refcounts can't move and disable module. */
765 ret
= try_stop_module(mod
, flags
, &forced
);
769 /* Never wait if forced. */
770 if (!forced
&& module_refcount(mod
) != 0)
771 wait_for_zero_refcount(mod
);
773 mutex_unlock(&module_mutex
);
774 /* Final destruction now noone is using it. */
775 if (mod
->exit
!= NULL
)
777 blocking_notifier_call_chain(&module_notify_list
,
778 MODULE_STATE_GOING
, mod
);
779 async_synchronize_full();
780 mutex_lock(&module_mutex
);
781 /* Store the name of the last unloaded module for diagnostic purposes */
782 strlcpy(last_unloaded_module
, mod
->name
, sizeof(last_unloaded_module
));
783 ddebug_remove_module(mod
->name
);
787 mutex_unlock(&module_mutex
);
791 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
793 struct module_use
*use
;
794 int printed_something
= 0;
796 seq_printf(m
, " %u ", module_refcount(mod
));
798 /* Always include a trailing , so userspace can differentiate
799 between this and the old multi-field proc format. */
800 list_for_each_entry(use
, &mod
->modules_which_use_me
, list
) {
801 printed_something
= 1;
802 seq_printf(m
, "%s,", use
->module_which_uses
->name
);
805 if (mod
->init
!= NULL
&& mod
->exit
== NULL
) {
806 printed_something
= 1;
807 seq_printf(m
, "[permanent],");
810 if (!printed_something
)
814 void __symbol_put(const char *symbol
)
816 struct module
*owner
;
819 if (!find_symbol(symbol
, &owner
, NULL
, true, false))
824 EXPORT_SYMBOL(__symbol_put
);
826 /* Note this assumes addr is a function, which it currently always is. */
827 void symbol_put_addr(void *addr
)
829 struct module
*modaddr
;
830 unsigned long a
= (unsigned long)dereference_function_descriptor(addr
);
832 if (core_kernel_text(a
))
835 /* module_text_address is safe here: we're supposed to have reference
836 * to module from symbol_get, so it can't go away. */
837 modaddr
= __module_text_address(a
);
841 EXPORT_SYMBOL_GPL(symbol_put_addr
);
843 static ssize_t
show_refcnt(struct module_attribute
*mattr
,
844 struct module
*mod
, char *buffer
)
846 return sprintf(buffer
, "%u\n", module_refcount(mod
));
849 static struct module_attribute refcnt
= {
850 .attr
= { .name
= "refcnt", .mode
= 0444 },
854 void module_put(struct module
*module
)
858 smp_wmb(); /* see comment in module_refcount */
859 __this_cpu_inc(module
->refptr
->decs
);
861 trace_module_put(module
, _RET_IP_
);
862 /* Maybe they're waiting for us to drop reference? */
863 if (unlikely(!module_is_live(module
)))
864 wake_up_process(module
->waiter
);
868 EXPORT_SYMBOL(module_put
);
870 #else /* !CONFIG_MODULE_UNLOAD */
871 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
873 /* We don't know the usage count, or what modules are using. */
874 seq_printf(m
, " - -");
877 static inline void module_unload_free(struct module
*mod
)
881 int use_module(struct module
*a
, struct module
*b
)
883 return strong_try_module_get(b
) == 0;
885 EXPORT_SYMBOL_GPL(use_module
);
887 static inline void module_unload_init(struct module
*mod
)
890 #endif /* CONFIG_MODULE_UNLOAD */
892 static ssize_t
show_initstate(struct module_attribute
*mattr
,
893 struct module
*mod
, char *buffer
)
895 const char *state
= "unknown";
897 switch (mod
->state
) {
898 case MODULE_STATE_LIVE
:
901 case MODULE_STATE_COMING
:
904 case MODULE_STATE_GOING
:
908 return sprintf(buffer
, "%s\n", state
);
911 static struct module_attribute initstate
= {
912 .attr
= { .name
= "initstate", .mode
= 0444 },
913 .show
= show_initstate
,
916 static struct module_attribute
*modinfo_attrs
[] = {
920 #ifdef CONFIG_MODULE_UNLOAD
926 static const char vermagic
[] = VERMAGIC_STRING
;
928 static int try_to_force_load(struct module
*mod
, const char *reason
)
930 #ifdef CONFIG_MODULE_FORCE_LOAD
931 if (!test_taint(TAINT_FORCED_MODULE
))
932 printk(KERN_WARNING
"%s: %s: kernel tainted.\n",
934 add_taint_module(mod
, TAINT_FORCED_MODULE
);
941 #ifdef CONFIG_MODVERSIONS
942 /* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
943 static unsigned long maybe_relocated(unsigned long crc
,
944 const struct module
*crc_owner
)
946 #ifdef ARCH_RELOCATES_KCRCTAB
947 if (crc_owner
== NULL
)
948 return crc
- (unsigned long)reloc_start
;
953 static int check_version(Elf_Shdr
*sechdrs
,
954 unsigned int versindex
,
957 const unsigned long *crc
,
958 const struct module
*crc_owner
)
960 unsigned int i
, num_versions
;
961 struct modversion_info
*versions
;
963 /* Exporting module didn't supply crcs? OK, we're already tainted. */
967 /* No versions at all? modprobe --force does this. */
969 return try_to_force_load(mod
, symname
) == 0;
971 versions
= (void *) sechdrs
[versindex
].sh_addr
;
972 num_versions
= sechdrs
[versindex
].sh_size
973 / sizeof(struct modversion_info
);
975 for (i
= 0; i
< num_versions
; i
++) {
976 if (strcmp(versions
[i
].name
, symname
) != 0)
979 if (versions
[i
].crc
== maybe_relocated(*crc
, crc_owner
))
981 DEBUGP("Found checksum %lX vs module %lX\n",
982 maybe_relocated(*crc
, crc_owner
), versions
[i
].crc
);
986 printk(KERN_WARNING
"%s: no symbol version for %s\n",
991 printk("%s: disagrees about version of symbol %s\n",
996 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
997 unsigned int versindex
,
1000 const unsigned long *crc
;
1002 if (!find_symbol(MODULE_SYMBOL_PREFIX
"module_layout", NULL
,
1005 return check_version(sechdrs
, versindex
, "module_layout", mod
, crc
,
1009 /* First part is kernel version, which we ignore if module has crcs. */
1010 static inline int same_magic(const char *amagic
, const char *bmagic
,
1014 amagic
+= strcspn(amagic
, " ");
1015 bmagic
+= strcspn(bmagic
, " ");
1017 return strcmp(amagic
, bmagic
) == 0;
1020 static inline int check_version(Elf_Shdr
*sechdrs
,
1021 unsigned int versindex
,
1022 const char *symname
,
1024 const unsigned long *crc
,
1025 const struct module
*crc_owner
)
1030 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1031 unsigned int versindex
,
1037 static inline int same_magic(const char *amagic
, const char *bmagic
,
1040 return strcmp(amagic
, bmagic
) == 0;
1042 #endif /* CONFIG_MODVERSIONS */
1044 /* Resolve a symbol for this module. I.e. if we find one, record usage.
1045 Must be holding module_mutex. */
1046 static const struct kernel_symbol
*resolve_symbol(Elf_Shdr
*sechdrs
,
1047 unsigned int versindex
,
1051 struct module
*owner
;
1052 const struct kernel_symbol
*sym
;
1053 const unsigned long *crc
;
1055 sym
= find_symbol(name
, &owner
, &crc
,
1056 !(mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
)), true);
1057 /* use_module can fail due to OOM,
1058 or module initialization or unloading */
1060 if (!check_version(sechdrs
, versindex
, name
, mod
, crc
, owner
)
1061 || !use_module(mod
, owner
))
1068 * /sys/module/foo/sections stuff
1069 * J. Corbet <corbet@lwn.net>
1071 #if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
1073 static inline bool sect_empty(const Elf_Shdr
*sect
)
1075 return !(sect
->sh_flags
& SHF_ALLOC
) || sect
->sh_size
== 0;
1078 struct module_sect_attr
1080 struct module_attribute mattr
;
1082 unsigned long address
;
1085 struct module_sect_attrs
1087 struct attribute_group grp
;
1088 unsigned int nsections
;
1089 struct module_sect_attr attrs
[0];
1092 static ssize_t
module_sect_show(struct module_attribute
*mattr
,
1093 struct module
*mod
, char *buf
)
1095 struct module_sect_attr
*sattr
=
1096 container_of(mattr
, struct module_sect_attr
, mattr
);
1097 return sprintf(buf
, "0x%lx\n", sattr
->address
);
1100 static void free_sect_attrs(struct module_sect_attrs
*sect_attrs
)
1102 unsigned int section
;
1104 for (section
= 0; section
< sect_attrs
->nsections
; section
++)
1105 kfree(sect_attrs
->attrs
[section
].name
);
1109 static void add_sect_attrs(struct module
*mod
, unsigned int nsect
,
1110 char *secstrings
, Elf_Shdr
*sechdrs
)
1112 unsigned int nloaded
= 0, i
, size
[2];
1113 struct module_sect_attrs
*sect_attrs
;
1114 struct module_sect_attr
*sattr
;
1115 struct attribute
**gattr
;
1117 /* Count loaded sections and allocate structures */
1118 for (i
= 0; i
< nsect
; i
++)
1119 if (!sect_empty(&sechdrs
[i
]))
1121 size
[0] = ALIGN(sizeof(*sect_attrs
)
1122 + nloaded
* sizeof(sect_attrs
->attrs
[0]),
1123 sizeof(sect_attrs
->grp
.attrs
[0]));
1124 size
[1] = (nloaded
+ 1) * sizeof(sect_attrs
->grp
.attrs
[0]);
1125 sect_attrs
= kzalloc(size
[0] + size
[1], GFP_KERNEL
);
1126 if (sect_attrs
== NULL
)
1129 /* Setup section attributes. */
1130 sect_attrs
->grp
.name
= "sections";
1131 sect_attrs
->grp
.attrs
= (void *)sect_attrs
+ size
[0];
1133 sect_attrs
->nsections
= 0;
1134 sattr
= §_attrs
->attrs
[0];
1135 gattr
= §_attrs
->grp
.attrs
[0];
1136 for (i
= 0; i
< nsect
; i
++) {
1137 if (sect_empty(&sechdrs
[i
]))
1139 sattr
->address
= sechdrs
[i
].sh_addr
;
1140 sattr
->name
= kstrdup(secstrings
+ sechdrs
[i
].sh_name
,
1142 if (sattr
->name
== NULL
)
1144 sect_attrs
->nsections
++;
1145 sysfs_attr_init(&sattr
->mattr
.attr
);
1146 sattr
->mattr
.show
= module_sect_show
;
1147 sattr
->mattr
.store
= NULL
;
1148 sattr
->mattr
.attr
.name
= sattr
->name
;
1149 sattr
->mattr
.attr
.mode
= S_IRUGO
;
1150 *(gattr
++) = &(sattr
++)->mattr
.attr
;
1154 if (sysfs_create_group(&mod
->mkobj
.kobj
, §_attrs
->grp
))
1157 mod
->sect_attrs
= sect_attrs
;
1160 free_sect_attrs(sect_attrs
);
1163 static void remove_sect_attrs(struct module
*mod
)
1165 if (mod
->sect_attrs
) {
1166 sysfs_remove_group(&mod
->mkobj
.kobj
,
1167 &mod
->sect_attrs
->grp
);
1168 /* We are positive that no one is using any sect attrs
1169 * at this point. Deallocate immediately. */
1170 free_sect_attrs(mod
->sect_attrs
);
1171 mod
->sect_attrs
= NULL
;
1176 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1179 struct module_notes_attrs
{
1180 struct kobject
*dir
;
1182 struct bin_attribute attrs
[0];
1185 static ssize_t
module_notes_read(struct kobject
*kobj
,
1186 struct bin_attribute
*bin_attr
,
1187 char *buf
, loff_t pos
, size_t count
)
1190 * The caller checked the pos and count against our size.
1192 memcpy(buf
, bin_attr
->private + pos
, count
);
1196 static void free_notes_attrs(struct module_notes_attrs
*notes_attrs
,
1199 if (notes_attrs
->dir
) {
1201 sysfs_remove_bin_file(notes_attrs
->dir
,
1202 ¬es_attrs
->attrs
[i
]);
1203 kobject_put(notes_attrs
->dir
);
1208 static void add_notes_attrs(struct module
*mod
, unsigned int nsect
,
1209 char *secstrings
, Elf_Shdr
*sechdrs
)
1211 unsigned int notes
, loaded
, i
;
1212 struct module_notes_attrs
*notes_attrs
;
1213 struct bin_attribute
*nattr
;
1215 /* failed to create section attributes, so can't create notes */
1216 if (!mod
->sect_attrs
)
1219 /* Count notes sections and allocate structures. */
1221 for (i
= 0; i
< nsect
; i
++)
1222 if (!sect_empty(&sechdrs
[i
]) &&
1223 (sechdrs
[i
].sh_type
== SHT_NOTE
))
1229 notes_attrs
= kzalloc(sizeof(*notes_attrs
)
1230 + notes
* sizeof(notes_attrs
->attrs
[0]),
1232 if (notes_attrs
== NULL
)
1235 notes_attrs
->notes
= notes
;
1236 nattr
= ¬es_attrs
->attrs
[0];
1237 for (loaded
= i
= 0; i
< nsect
; ++i
) {
1238 if (sect_empty(&sechdrs
[i
]))
1240 if (sechdrs
[i
].sh_type
== SHT_NOTE
) {
1241 sysfs_bin_attr_init(nattr
);
1242 nattr
->attr
.name
= mod
->sect_attrs
->attrs
[loaded
].name
;
1243 nattr
->attr
.mode
= S_IRUGO
;
1244 nattr
->size
= sechdrs
[i
].sh_size
;
1245 nattr
->private = (void *) sechdrs
[i
].sh_addr
;
1246 nattr
->read
= module_notes_read
;
1252 notes_attrs
->dir
= kobject_create_and_add("notes", &mod
->mkobj
.kobj
);
1253 if (!notes_attrs
->dir
)
1256 for (i
= 0; i
< notes
; ++i
)
1257 if (sysfs_create_bin_file(notes_attrs
->dir
,
1258 ¬es_attrs
->attrs
[i
]))
1261 mod
->notes_attrs
= notes_attrs
;
1265 free_notes_attrs(notes_attrs
, i
);
1268 static void remove_notes_attrs(struct module
*mod
)
1270 if (mod
->notes_attrs
)
1271 free_notes_attrs(mod
->notes_attrs
, mod
->notes_attrs
->notes
);
1276 static inline void add_sect_attrs(struct module
*mod
, unsigned int nsect
,
1277 char *sectstrings
, Elf_Shdr
*sechdrs
)
1281 static inline void remove_sect_attrs(struct module
*mod
)
1285 static inline void add_notes_attrs(struct module
*mod
, unsigned int nsect
,
1286 char *sectstrings
, Elf_Shdr
*sechdrs
)
1290 static inline void remove_notes_attrs(struct module
*mod
)
1296 int module_add_modinfo_attrs(struct module
*mod
)
1298 struct module_attribute
*attr
;
1299 struct module_attribute
*temp_attr
;
1303 mod
->modinfo_attrs
= kzalloc((sizeof(struct module_attribute
) *
1304 (ARRAY_SIZE(modinfo_attrs
) + 1)),
1306 if (!mod
->modinfo_attrs
)
1309 temp_attr
= mod
->modinfo_attrs
;
1310 for (i
= 0; (attr
= modinfo_attrs
[i
]) && !error
; i
++) {
1312 (attr
->test
&& attr
->test(mod
))) {
1313 memcpy(temp_attr
, attr
, sizeof(*temp_attr
));
1314 sysfs_attr_init(&temp_attr
->attr
);
1315 error
= sysfs_create_file(&mod
->mkobj
.kobj
,&temp_attr
->attr
);
1322 void module_remove_modinfo_attrs(struct module
*mod
)
1324 struct module_attribute
*attr
;
1327 for (i
= 0; (attr
= &mod
->modinfo_attrs
[i
]); i
++) {
1328 /* pick a field to test for end of list */
1329 if (!attr
->attr
.name
)
1331 sysfs_remove_file(&mod
->mkobj
.kobj
,&attr
->attr
);
1335 kfree(mod
->modinfo_attrs
);
1338 int mod_sysfs_init(struct module
*mod
)
1341 struct kobject
*kobj
;
1343 if (!module_sysfs_initialized
) {
1344 printk(KERN_ERR
"%s: module sysfs not initialized\n",
1350 kobj
= kset_find_obj(module_kset
, mod
->name
);
1352 printk(KERN_ERR
"%s: module is already loaded\n", mod
->name
);
1358 mod
->mkobj
.mod
= mod
;
1360 memset(&mod
->mkobj
.kobj
, 0, sizeof(mod
->mkobj
.kobj
));
1361 mod
->mkobj
.kobj
.kset
= module_kset
;
1362 err
= kobject_init_and_add(&mod
->mkobj
.kobj
, &module_ktype
, NULL
,
1365 kobject_put(&mod
->mkobj
.kobj
);
1367 /* delay uevent until full sysfs population */
1372 int mod_sysfs_setup(struct module
*mod
,
1373 struct kernel_param
*kparam
,
1374 unsigned int num_params
)
1378 mod
->holders_dir
= kobject_create_and_add("holders", &mod
->mkobj
.kobj
);
1379 if (!mod
->holders_dir
) {
1384 err
= module_param_sysfs_setup(mod
, kparam
, num_params
);
1386 goto out_unreg_holders
;
1388 err
= module_add_modinfo_attrs(mod
);
1390 goto out_unreg_param
;
1392 kobject_uevent(&mod
->mkobj
.kobj
, KOBJ_ADD
);
1396 module_param_sysfs_remove(mod
);
1398 kobject_put(mod
->holders_dir
);
1400 kobject_put(&mod
->mkobj
.kobj
);
1404 static void mod_sysfs_fini(struct module
*mod
)
1406 kobject_put(&mod
->mkobj
.kobj
);
1409 #else /* CONFIG_SYSFS */
1411 static void mod_sysfs_fini(struct module
*mod
)
1415 #endif /* CONFIG_SYSFS */
1417 static void mod_kobject_remove(struct module
*mod
)
1419 module_remove_modinfo_attrs(mod
);
1420 module_param_sysfs_remove(mod
);
1421 kobject_put(mod
->mkobj
.drivers_dir
);
1422 kobject_put(mod
->holders_dir
);
1423 mod_sysfs_fini(mod
);
1427 * unlink the module with the whole machine is stopped with interrupts off
1428 * - this defends against kallsyms not taking locks
1430 static int __unlink_module(void *_mod
)
1432 struct module
*mod
= _mod
;
1433 list_del(&mod
->list
);
1437 /* Free a module, remove from lists, etc (must hold module_mutex). */
1438 static void free_module(struct module
*mod
)
1440 trace_module_free(mod
);
1442 /* Delete from various lists */
1443 stop_machine(__unlink_module
, mod
, NULL
);
1444 remove_notes_attrs(mod
);
1445 remove_sect_attrs(mod
);
1446 mod_kobject_remove(mod
);
1448 /* Arch-specific cleanup. */
1449 module_arch_cleanup(mod
);
1451 /* Module unload stuff */
1452 module_unload_free(mod
);
1454 /* Free any allocated parameters. */
1455 destroy_params(mod
->kp
, mod
->num_kp
);
1457 /* This may be NULL, but that's OK */
1458 module_free(mod
, mod
->module_init
);
1460 percpu_modfree(mod
);
1461 #if defined(CONFIG_MODULE_UNLOAD)
1463 free_percpu(mod
->refptr
);
1465 /* Free lock-classes: */
1466 lockdep_free_key_range(mod
->module_core
, mod
->core_size
);
1468 /* Finally, free the core (containing the module structure) */
1469 module_free(mod
, mod
->module_core
);
1472 update_protections(current
->mm
);
1476 void *__symbol_get(const char *symbol
)
1478 struct module
*owner
;
1479 const struct kernel_symbol
*sym
;
1482 sym
= find_symbol(symbol
, &owner
, NULL
, true, true);
1483 if (sym
&& strong_try_module_get(owner
))
1487 return sym
? (void *)sym
->value
: NULL
;
1489 EXPORT_SYMBOL_GPL(__symbol_get
);
1492 * Ensure that an exported symbol [global namespace] does not already exist
1493 * in the kernel or in some other module's exported symbol table.
1495 static int verify_export_symbols(struct module
*mod
)
1498 struct module
*owner
;
1499 const struct kernel_symbol
*s
;
1501 const struct kernel_symbol
*sym
;
1504 { mod
->syms
, mod
->num_syms
},
1505 { mod
->gpl_syms
, mod
->num_gpl_syms
},
1506 { mod
->gpl_future_syms
, mod
->num_gpl_future_syms
},
1507 #ifdef CONFIG_UNUSED_SYMBOLS
1508 { mod
->unused_syms
, mod
->num_unused_syms
},
1509 { mod
->unused_gpl_syms
, mod
->num_unused_gpl_syms
},
1513 for (i
= 0; i
< ARRAY_SIZE(arr
); i
++) {
1514 for (s
= arr
[i
].sym
; s
< arr
[i
].sym
+ arr
[i
].num
; s
++) {
1515 if (find_symbol(s
->name
, &owner
, NULL
, true, false)) {
1517 "%s: exports duplicate symbol %s"
1519 mod
->name
, s
->name
, module_name(owner
));
1527 /* Change all symbols so that st_value encodes the pointer directly. */
1528 static int simplify_symbols(Elf_Shdr
*sechdrs
,
1529 unsigned int symindex
,
1531 unsigned int versindex
,
1532 unsigned int pcpuindex
,
1535 Elf_Sym
*sym
= (void *)sechdrs
[symindex
].sh_addr
;
1536 unsigned long secbase
;
1537 unsigned int i
, n
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1539 const struct kernel_symbol
*ksym
;
1541 for (i
= 1; i
< n
; i
++) {
1542 switch (sym
[i
].st_shndx
) {
1544 /* We compiled with -fno-common. These are not
1545 supposed to happen. */
1546 DEBUGP("Common symbol: %s\n", strtab
+ sym
[i
].st_name
);
1547 printk("%s: please compile with -fno-common\n",
1553 /* Don't need to do anything */
1554 DEBUGP("Absolute symbol: 0x%08lx\n",
1555 (long)sym
[i
].st_value
);
1559 ksym
= resolve_symbol(sechdrs
, versindex
,
1560 strtab
+ sym
[i
].st_name
, mod
);
1561 /* Ok if resolved. */
1563 sym
[i
].st_value
= ksym
->value
;
1568 if (ELF_ST_BIND(sym
[i
].st_info
) == STB_WEAK
)
1571 printk(KERN_WARNING
"%s: Unknown symbol %s\n",
1572 mod
->name
, strtab
+ sym
[i
].st_name
);
1577 /* Divert to percpu allocation if a percpu var. */
1578 if (sym
[i
].st_shndx
== pcpuindex
)
1579 secbase
= (unsigned long)mod_percpu(mod
);
1581 secbase
= sechdrs
[sym
[i
].st_shndx
].sh_addr
;
1582 sym
[i
].st_value
+= secbase
;
1590 /* Additional bytes needed by arch in front of individual sections */
1591 unsigned int __weak
arch_mod_section_prepend(struct module
*mod
,
1592 unsigned int section
)
1594 /* default implementation just returns zero */
1598 /* Update size with this section: return offset. */
1599 static long get_offset(struct module
*mod
, unsigned int *size
,
1600 Elf_Shdr
*sechdr
, unsigned int section
)
1604 *size
+= arch_mod_section_prepend(mod
, section
);
1605 ret
= ALIGN(*size
, sechdr
->sh_addralign
?: 1);
1606 *size
= ret
+ sechdr
->sh_size
;
1610 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1611 might -- code, read-only data, read-write data, small data. Tally
1612 sizes, and place the offsets into sh_entsize fields: high bit means it
1614 static void layout_sections(struct module
*mod
,
1615 const Elf_Ehdr
*hdr
,
1617 const char *secstrings
)
1619 static unsigned long const masks
[][2] = {
1620 /* NOTE: all executable code must be the first section
1621 * in this array; otherwise modify the text_size
1622 * finder in the two loops below */
1623 { SHF_EXECINSTR
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1624 { SHF_ALLOC
, SHF_WRITE
| ARCH_SHF_SMALL
},
1625 { SHF_WRITE
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1626 { ARCH_SHF_SMALL
| SHF_ALLOC
, 0 }
1630 for (i
= 0; i
< hdr
->e_shnum
; i
++)
1631 sechdrs
[i
].sh_entsize
= ~0UL;
1633 DEBUGP("Core section allocation order:\n");
1634 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1635 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1636 Elf_Shdr
*s
= &sechdrs
[i
];
1638 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1639 || (s
->sh_flags
& masks
[m
][1])
1640 || s
->sh_entsize
!= ~0UL
1641 || strstarts(secstrings
+ s
->sh_name
, ".init"))
1643 s
->sh_entsize
= get_offset(mod
, &mod
->core_size
, s
, i
);
1644 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1647 mod
->core_text_size
= mod
->core_size
;
1650 DEBUGP("Init section allocation order:\n");
1651 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1652 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1653 Elf_Shdr
*s
= &sechdrs
[i
];
1655 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1656 || (s
->sh_flags
& masks
[m
][1])
1657 || s
->sh_entsize
!= ~0UL
1658 || !strstarts(secstrings
+ s
->sh_name
, ".init"))
1660 s
->sh_entsize
= (get_offset(mod
, &mod
->init_size
, s
, i
)
1661 | INIT_OFFSET_MASK
);
1662 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1665 mod
->init_text_size
= mod
->init_size
;
1669 static void set_license(struct module
*mod
, const char *license
)
1672 license
= "unspecified";
1674 if (!license_is_gpl_compatible(license
)) {
1675 if (!test_taint(TAINT_PROPRIETARY_MODULE
))
1676 printk(KERN_WARNING
"%s: module license '%s' taints "
1677 "kernel.\n", mod
->name
, license
);
1678 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
1682 /* Parse tag=value strings from .modinfo section */
1683 static char *next_string(char *string
, unsigned long *secsize
)
1685 /* Skip non-zero chars */
1688 if ((*secsize
)-- <= 1)
1692 /* Skip any zero padding. */
1693 while (!string
[0]) {
1695 if ((*secsize
)-- <= 1)
1701 static char *get_modinfo(Elf_Shdr
*sechdrs
,
1706 unsigned int taglen
= strlen(tag
);
1707 unsigned long size
= sechdrs
[info
].sh_size
;
1709 for (p
= (char *)sechdrs
[info
].sh_addr
; p
; p
= next_string(p
, &size
)) {
1710 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
1711 return p
+ taglen
+ 1;
1716 static void setup_modinfo(struct module
*mod
, Elf_Shdr
*sechdrs
,
1717 unsigned int infoindex
)
1719 struct module_attribute
*attr
;
1722 for (i
= 0; (attr
= modinfo_attrs
[i
]); i
++) {
1725 get_modinfo(sechdrs
,
1731 static void free_modinfo(struct module
*mod
)
1733 struct module_attribute
*attr
;
1736 for (i
= 0; (attr
= modinfo_attrs
[i
]); i
++) {
1742 #ifdef CONFIG_KALLSYMS
1744 /* lookup symbol in given range of kernel_symbols */
1745 static const struct kernel_symbol
*lookup_symbol(const char *name
,
1746 const struct kernel_symbol
*start
,
1747 const struct kernel_symbol
*stop
)
1749 const struct kernel_symbol
*ks
= start
;
1750 for (; ks
< stop
; ks
++)
1751 if (strcmp(ks
->name
, name
) == 0)
1756 static int is_exported(const char *name
, unsigned long value
,
1757 const struct module
*mod
)
1759 const struct kernel_symbol
*ks
;
1761 ks
= lookup_symbol(name
, __start___ksymtab
, __stop___ksymtab
);
1763 ks
= lookup_symbol(name
, mod
->syms
, mod
->syms
+ mod
->num_syms
);
1764 return ks
!= NULL
&& ks
->value
== value
;
1768 static char elf_type(const Elf_Sym
*sym
,
1770 const char *secstrings
,
1773 if (ELF_ST_BIND(sym
->st_info
) == STB_WEAK
) {
1774 if (ELF_ST_TYPE(sym
->st_info
) == STT_OBJECT
)
1779 if (sym
->st_shndx
== SHN_UNDEF
)
1781 if (sym
->st_shndx
== SHN_ABS
)
1783 if (sym
->st_shndx
>= SHN_LORESERVE
)
1785 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_EXECINSTR
)
1787 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_ALLOC
1788 && sechdrs
[sym
->st_shndx
].sh_type
!= SHT_NOBITS
) {
1789 if (!(sechdrs
[sym
->st_shndx
].sh_flags
& SHF_WRITE
))
1791 else if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1796 if (sechdrs
[sym
->st_shndx
].sh_type
== SHT_NOBITS
) {
1797 if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1802 if (strstarts(secstrings
+ sechdrs
[sym
->st_shndx
].sh_name
, ".debug"))
1807 static bool is_core_symbol(const Elf_Sym
*src
, const Elf_Shdr
*sechdrs
,
1810 const Elf_Shdr
*sec
;
1812 if (src
->st_shndx
== SHN_UNDEF
1813 || src
->st_shndx
>= shnum
1817 sec
= sechdrs
+ src
->st_shndx
;
1818 if (!(sec
->sh_flags
& SHF_ALLOC
)
1819 #ifndef CONFIG_KALLSYMS_ALL
1820 || !(sec
->sh_flags
& SHF_EXECINSTR
)
1822 || (sec
->sh_entsize
& INIT_OFFSET_MASK
))
1828 static unsigned long layout_symtab(struct module
*mod
,
1830 unsigned int symindex
,
1831 unsigned int strindex
,
1832 const Elf_Ehdr
*hdr
,
1833 const char *secstrings
,
1834 unsigned long *pstroffs
,
1835 unsigned long *strmap
)
1837 unsigned long symoffs
;
1838 Elf_Shdr
*symsect
= sechdrs
+ symindex
;
1839 Elf_Shdr
*strsect
= sechdrs
+ strindex
;
1842 unsigned int i
, nsrc
, ndst
;
1844 /* Put symbol section at end of init part of module. */
1845 symsect
->sh_flags
|= SHF_ALLOC
;
1846 symsect
->sh_entsize
= get_offset(mod
, &mod
->init_size
, symsect
,
1847 symindex
) | INIT_OFFSET_MASK
;
1848 DEBUGP("\t%s\n", secstrings
+ symsect
->sh_name
);
1850 src
= (void *)hdr
+ symsect
->sh_offset
;
1851 nsrc
= symsect
->sh_size
/ sizeof(*src
);
1852 strtab
= (void *)hdr
+ strsect
->sh_offset
;
1853 for (ndst
= i
= 1; i
< nsrc
; ++i
, ++src
)
1854 if (is_core_symbol(src
, sechdrs
, hdr
->e_shnum
)) {
1855 unsigned int j
= src
->st_name
;
1857 while(!__test_and_set_bit(j
, strmap
) && strtab
[j
])
1862 /* Append room for core symbols at end of core part. */
1863 symoffs
= ALIGN(mod
->core_size
, symsect
->sh_addralign
?: 1);
1864 mod
->core_size
= symoffs
+ ndst
* sizeof(Elf_Sym
);
1866 /* Put string table section at end of init part of module. */
1867 strsect
->sh_flags
|= SHF_ALLOC
;
1868 strsect
->sh_entsize
= get_offset(mod
, &mod
->init_size
, strsect
,
1869 strindex
) | INIT_OFFSET_MASK
;
1870 DEBUGP("\t%s\n", secstrings
+ strsect
->sh_name
);
1872 /* Append room for core symbols' strings at end of core part. */
1873 *pstroffs
= mod
->core_size
;
1874 __set_bit(0, strmap
);
1875 mod
->core_size
+= bitmap_weight(strmap
, strsect
->sh_size
);
1880 static void add_kallsyms(struct module
*mod
,
1883 unsigned int symindex
,
1884 unsigned int strindex
,
1885 unsigned long symoffs
,
1886 unsigned long stroffs
,
1887 const char *secstrings
,
1888 unsigned long *strmap
)
1890 unsigned int i
, ndst
;
1895 mod
->symtab
= (void *)sechdrs
[symindex
].sh_addr
;
1896 mod
->num_symtab
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1897 mod
->strtab
= (void *)sechdrs
[strindex
].sh_addr
;
1899 /* Set types up while we still have access to sections. */
1900 for (i
= 0; i
< mod
->num_symtab
; i
++)
1901 mod
->symtab
[i
].st_info
1902 = elf_type(&mod
->symtab
[i
], sechdrs
, secstrings
, mod
);
1904 mod
->core_symtab
= dst
= mod
->module_core
+ symoffs
;
1907 for (ndst
= i
= 1; i
< mod
->num_symtab
; ++i
, ++src
) {
1908 if (!is_core_symbol(src
, sechdrs
, shnum
))
1911 dst
[ndst
].st_name
= bitmap_weight(strmap
, dst
[ndst
].st_name
);
1914 mod
->core_num_syms
= ndst
;
1916 mod
->core_strtab
= s
= mod
->module_core
+ stroffs
;
1917 for (*s
= 0, i
= 1; i
< sechdrs
[strindex
].sh_size
; ++i
)
1918 if (test_bit(i
, strmap
))
1919 *++s
= mod
->strtab
[i
];
1922 static inline unsigned long layout_symtab(struct module
*mod
,
1924 unsigned int symindex
,
1925 unsigned int strindex
,
1926 const Elf_Ehdr
*hdr
,
1927 const char *secstrings
,
1928 unsigned long *pstroffs
,
1929 unsigned long *strmap
)
1934 static inline void add_kallsyms(struct module
*mod
,
1937 unsigned int symindex
,
1938 unsigned int strindex
,
1939 unsigned long symoffs
,
1940 unsigned long stroffs
,
1941 const char *secstrings
,
1942 const unsigned long *strmap
)
1945 #endif /* CONFIG_KALLSYMS */
1947 static void dynamic_debug_setup(struct _ddebug
*debug
, unsigned int num
)
1949 #ifdef CONFIG_DYNAMIC_DEBUG
1950 if (ddebug_add_module(debug
, num
, debug
->modname
))
1951 printk(KERN_ERR
"dynamic debug error adding module: %s\n",
1956 static void *module_alloc_update_bounds(unsigned long size
)
1958 void *ret
= module_alloc(size
);
1961 /* Update module bounds. */
1962 if ((unsigned long)ret
< module_addr_min
)
1963 module_addr_min
= (unsigned long)ret
;
1964 if ((unsigned long)ret
+ size
> module_addr_max
)
1965 module_addr_max
= (unsigned long)ret
+ size
;
1970 #ifdef CONFIG_DEBUG_KMEMLEAK
1971 static void kmemleak_load_module(struct module
*mod
, Elf_Ehdr
*hdr
,
1972 Elf_Shdr
*sechdrs
, char *secstrings
)
1976 /* only scan the sections containing data */
1977 kmemleak_scan_area(mod
, sizeof(struct module
), GFP_KERNEL
);
1979 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
1980 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
1982 if (strncmp(secstrings
+ sechdrs
[i
].sh_name
, ".data", 5) != 0
1983 && strncmp(secstrings
+ sechdrs
[i
].sh_name
, ".bss", 4) != 0)
1986 kmemleak_scan_area((void *)sechdrs
[i
].sh_addr
,
1987 sechdrs
[i
].sh_size
, GFP_KERNEL
);
1991 static inline void kmemleak_load_module(struct module
*mod
, Elf_Ehdr
*hdr
,
1992 Elf_Shdr
*sechdrs
, char *secstrings
)
1997 /* Allocate and load the module: note that size of section 0 is always
1998 zero, and we rely on this for optional sections. */
1999 static noinline
struct module
*load_module(void __user
*umod
,
2001 const char __user
*uargs
)
2005 char *secstrings
, *args
, *modmagic
, *strtab
= NULL
;
2008 unsigned int symindex
= 0;
2009 unsigned int strindex
= 0;
2010 unsigned int modindex
, versindex
, infoindex
, pcpuindex
;
2013 void *ptr
= NULL
; /* Stops spurious gcc warning */
2014 unsigned long symoffs
, stroffs
, *strmap
;
2016 mm_segment_t old_fs
;
2018 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
2020 if (len
< sizeof(*hdr
))
2021 return ERR_PTR(-ENOEXEC
);
2023 /* Suck in entire file: we'll want most of it. */
2024 /* vmalloc barfs on "unusual" numbers. Check here */
2025 if (len
> 64 * 1024 * 1024 || (hdr
= vmalloc(len
)) == NULL
)
2026 return ERR_PTR(-ENOMEM
);
2028 if (copy_from_user(hdr
, umod
, len
) != 0) {
2033 /* Sanity checks against insmoding binaries or wrong arch,
2034 weird elf version */
2035 if (memcmp(hdr
->e_ident
, ELFMAG
, SELFMAG
) != 0
2036 || hdr
->e_type
!= ET_REL
2037 || !elf_check_arch(hdr
)
2038 || hdr
->e_shentsize
!= sizeof(*sechdrs
)) {
2043 if (len
< hdr
->e_shoff
+ hdr
->e_shnum
* sizeof(Elf_Shdr
))
2046 /* Convenience variables */
2047 sechdrs
= (void *)hdr
+ hdr
->e_shoff
;
2048 secstrings
= (void *)hdr
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
2049 sechdrs
[0].sh_addr
= 0;
2051 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
2052 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
2053 && len
< sechdrs
[i
].sh_offset
+ sechdrs
[i
].sh_size
)
2056 /* Mark all sections sh_addr with their address in the
2058 sechdrs
[i
].sh_addr
= (size_t)hdr
+ sechdrs
[i
].sh_offset
;
2060 /* Internal symbols and strings. */
2061 if (sechdrs
[i
].sh_type
== SHT_SYMTAB
) {
2063 strindex
= sechdrs
[i
].sh_link
;
2064 strtab
= (char *)hdr
+ sechdrs
[strindex
].sh_offset
;
2066 #ifndef CONFIG_MODULE_UNLOAD
2067 /* Don't load .exit sections */
2068 if (strstarts(secstrings
+sechdrs
[i
].sh_name
, ".exit"))
2069 sechdrs
[i
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2073 modindex
= find_sec(hdr
, sechdrs
, secstrings
,
2074 ".gnu.linkonce.this_module");
2076 printk(KERN_WARNING
"No module found in object\n");
2080 /* This is temporary: point mod into copy of data. */
2081 mod
= (void *)sechdrs
[modindex
].sh_addr
;
2083 if (symindex
== 0) {
2084 printk(KERN_WARNING
"%s: module has no symbols (stripped?)\n",
2090 versindex
= find_sec(hdr
, sechdrs
, secstrings
, "__versions");
2091 infoindex
= find_sec(hdr
, sechdrs
, secstrings
, ".modinfo");
2092 pcpuindex
= find_pcpusec(hdr
, sechdrs
, secstrings
);
2094 /* Don't keep modinfo and version sections. */
2095 sechdrs
[infoindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2096 sechdrs
[versindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2098 /* Check module struct version now, before we try to use module. */
2099 if (!check_modstruct_version(sechdrs
, versindex
, mod
)) {
2104 modmagic
= get_modinfo(sechdrs
, infoindex
, "vermagic");
2105 /* This is allowed: modprobe --force will invalidate it. */
2107 err
= try_to_force_load(mod
, "bad vermagic");
2110 } else if (!same_magic(modmagic
, vermagic
, versindex
)) {
2111 printk(KERN_ERR
"%s: version magic '%s' should be '%s'\n",
2112 mod
->name
, modmagic
, vermagic
);
2117 staging
= get_modinfo(sechdrs
, infoindex
, "staging");
2119 add_taint_module(mod
, TAINT_CRAP
);
2120 printk(KERN_WARNING
"%s: module is from the staging directory,"
2121 " the quality is unknown, you have been warned.\n",
2125 /* Now copy in args */
2126 args
= strndup_user(uargs
, ~0UL >> 1);
2128 err
= PTR_ERR(args
);
2132 strmap
= kzalloc(BITS_TO_LONGS(sechdrs
[strindex
].sh_size
)
2133 * sizeof(long), GFP_KERNEL
);
2139 if (find_module(mod
->name
)) {
2144 mod
->state
= MODULE_STATE_COMING
;
2146 /* Allow arches to frob section contents and sizes. */
2147 err
= module_frob_arch_sections(hdr
, sechdrs
, secstrings
, mod
);
2152 /* We have a special allocation for this section. */
2153 err
= percpu_modalloc(mod
, sechdrs
[pcpuindex
].sh_size
,
2154 sechdrs
[pcpuindex
].sh_addralign
);
2157 sechdrs
[pcpuindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2160 /* Determine total sizes, and put offsets in sh_entsize. For now
2161 this is done generically; there doesn't appear to be any
2162 special cases for the architectures. */
2163 layout_sections(mod
, hdr
, sechdrs
, secstrings
);
2164 symoffs
= layout_symtab(mod
, sechdrs
, symindex
, strindex
, hdr
,
2165 secstrings
, &stroffs
, strmap
);
2167 /* Do the allocs. */
2168 ptr
= module_alloc_update_bounds(mod
->core_size
);
2170 * The pointer to this block is stored in the module structure
2171 * which is inside the block. Just mark it as not being a
2174 kmemleak_not_leak(ptr
);
2179 memset(ptr
, 0, mod
->core_size
);
2180 mod
->module_core
= ptr
;
2182 ptr
= module_alloc_update_bounds(mod
->init_size
);
2184 * The pointer to this block is stored in the module structure
2185 * which is inside the block. This block doesn't need to be
2186 * scanned as it contains data and code that will be freed
2187 * after the module is initialized.
2189 kmemleak_ignore(ptr
);
2190 if (!ptr
&& mod
->init_size
) {
2194 memset(ptr
, 0, mod
->init_size
);
2195 mod
->module_init
= ptr
;
2197 /* Transfer each section which specifies SHF_ALLOC */
2198 DEBUGP("final section addresses:\n");
2199 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
2202 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
2205 if (sechdrs
[i
].sh_entsize
& INIT_OFFSET_MASK
)
2206 dest
= mod
->module_init
2207 + (sechdrs
[i
].sh_entsize
& ~INIT_OFFSET_MASK
);
2209 dest
= mod
->module_core
+ sechdrs
[i
].sh_entsize
;
2211 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
)
2212 memcpy(dest
, (void *)sechdrs
[i
].sh_addr
,
2213 sechdrs
[i
].sh_size
);
2214 /* Update sh_addr to point to copy in image. */
2215 sechdrs
[i
].sh_addr
= (unsigned long)dest
;
2216 DEBUGP("\t0x%lx %s\n", sechdrs
[i
].sh_addr
, secstrings
+ sechdrs
[i
].sh_name
);
2218 /* Module has been moved. */
2219 mod
= (void *)sechdrs
[modindex
].sh_addr
;
2220 kmemleak_load_module(mod
, hdr
, sechdrs
, secstrings
);
2222 #if defined(CONFIG_MODULE_UNLOAD)
2223 mod
->refptr
= alloc_percpu(struct module_ref
);
2229 /* Now we've moved module, initialize linked lists, etc. */
2230 module_unload_init(mod
);
2232 /* add kobject, so we can reference it. */
2233 err
= mod_sysfs_init(mod
);
2237 /* Set up license info based on the info section */
2238 set_license(mod
, get_modinfo(sechdrs
, infoindex
, "license"));
2241 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2242 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2243 * using GPL-only symbols it needs.
2245 if (strcmp(mod
->name
, "ndiswrapper") == 0)
2246 add_taint(TAINT_PROPRIETARY_MODULE
);
2248 /* driverloader was caught wrongly pretending to be under GPL */
2249 if (strcmp(mod
->name
, "driverloader") == 0)
2250 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
2252 /* Set up MODINFO_ATTR fields */
2253 setup_modinfo(mod
, sechdrs
, infoindex
);
2255 /* Fix up syms, so that st_value is a pointer to location. */
2256 err
= simplify_symbols(sechdrs
, symindex
, strtab
, versindex
, pcpuindex
,
2261 /* Now we've got everything in the final locations, we can
2262 * find optional sections. */
2263 mod
->kp
= section_objs(hdr
, sechdrs
, secstrings
, "__param",
2264 sizeof(*mod
->kp
), &mod
->num_kp
);
2265 mod
->syms
= section_objs(hdr
, sechdrs
, secstrings
, "__ksymtab",
2266 sizeof(*mod
->syms
), &mod
->num_syms
);
2267 mod
->crcs
= section_addr(hdr
, sechdrs
, secstrings
, "__kcrctab");
2268 mod
->gpl_syms
= section_objs(hdr
, sechdrs
, secstrings
, "__ksymtab_gpl",
2269 sizeof(*mod
->gpl_syms
),
2270 &mod
->num_gpl_syms
);
2271 mod
->gpl_crcs
= section_addr(hdr
, sechdrs
, secstrings
, "__kcrctab_gpl");
2272 mod
->gpl_future_syms
= section_objs(hdr
, sechdrs
, secstrings
,
2273 "__ksymtab_gpl_future",
2274 sizeof(*mod
->gpl_future_syms
),
2275 &mod
->num_gpl_future_syms
);
2276 mod
->gpl_future_crcs
= section_addr(hdr
, sechdrs
, secstrings
,
2277 "__kcrctab_gpl_future");
2279 #ifdef CONFIG_UNUSED_SYMBOLS
2280 mod
->unused_syms
= section_objs(hdr
, sechdrs
, secstrings
,
2282 sizeof(*mod
->unused_syms
),
2283 &mod
->num_unused_syms
);
2284 mod
->unused_crcs
= section_addr(hdr
, sechdrs
, secstrings
,
2285 "__kcrctab_unused");
2286 mod
->unused_gpl_syms
= section_objs(hdr
, sechdrs
, secstrings
,
2287 "__ksymtab_unused_gpl",
2288 sizeof(*mod
->unused_gpl_syms
),
2289 &mod
->num_unused_gpl_syms
);
2290 mod
->unused_gpl_crcs
= section_addr(hdr
, sechdrs
, secstrings
,
2291 "__kcrctab_unused_gpl");
2293 #ifdef CONFIG_CONSTRUCTORS
2294 mod
->ctors
= section_objs(hdr
, sechdrs
, secstrings
, ".ctors",
2295 sizeof(*mod
->ctors
), &mod
->num_ctors
);
2298 #ifdef CONFIG_TRACEPOINTS
2299 mod
->tracepoints
= section_objs(hdr
, sechdrs
, secstrings
,
2301 sizeof(*mod
->tracepoints
),
2302 &mod
->num_tracepoints
);
2304 #ifdef CONFIG_EVENT_TRACING
2305 mod
->trace_events
= section_objs(hdr
, sechdrs
, secstrings
,
2307 sizeof(*mod
->trace_events
),
2308 &mod
->num_trace_events
);
2310 * This section contains pointers to allocated objects in the trace
2311 * code and not scanning it leads to false positives.
2313 kmemleak_scan_area(mod
->trace_events
, sizeof(*mod
->trace_events
) *
2314 mod
->num_trace_events
, GFP_KERNEL
);
2316 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
2317 /* sechdrs[0].sh_size is always zero */
2318 mod
->ftrace_callsites
= section_objs(hdr
, sechdrs
, secstrings
,
2320 sizeof(*mod
->ftrace_callsites
),
2321 &mod
->num_ftrace_callsites
);
2323 #ifdef CONFIG_MODVERSIONS
2324 if ((mod
->num_syms
&& !mod
->crcs
)
2325 || (mod
->num_gpl_syms
&& !mod
->gpl_crcs
)
2326 || (mod
->num_gpl_future_syms
&& !mod
->gpl_future_crcs
)
2327 #ifdef CONFIG_UNUSED_SYMBOLS
2328 || (mod
->num_unused_syms
&& !mod
->unused_crcs
)
2329 || (mod
->num_unused_gpl_syms
&& !mod
->unused_gpl_crcs
)
2332 err
= try_to_force_load(mod
,
2333 "no versions for exported symbols");
2339 /* Now do relocations. */
2340 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
2341 const char *strtab
= (char *)sechdrs
[strindex
].sh_addr
;
2342 unsigned int info
= sechdrs
[i
].sh_info
;
2344 /* Not a valid relocation section? */
2345 if (info
>= hdr
->e_shnum
)
2348 /* Don't bother with non-allocated sections */
2349 if (!(sechdrs
[info
].sh_flags
& SHF_ALLOC
))
2352 if (sechdrs
[i
].sh_type
== SHT_REL
)
2353 err
= apply_relocate(sechdrs
, strtab
, symindex
, i
,mod
);
2354 else if (sechdrs
[i
].sh_type
== SHT_RELA
)
2355 err
= apply_relocate_add(sechdrs
, strtab
, symindex
, i
,
2361 /* Find duplicate symbols */
2362 err
= verify_export_symbols(mod
);
2366 /* Set up and sort exception table */
2367 mod
->extable
= section_objs(hdr
, sechdrs
, secstrings
, "__ex_table",
2368 sizeof(*mod
->extable
), &mod
->num_exentries
);
2369 sort_extable(mod
->extable
, mod
->extable
+ mod
->num_exentries
);
2371 /* Finally, copy percpu area over. */
2372 percpu_modcopy(mod
, (void *)sechdrs
[pcpuindex
].sh_addr
,
2373 sechdrs
[pcpuindex
].sh_size
);
2375 add_kallsyms(mod
, sechdrs
, hdr
->e_shnum
, symindex
, strindex
,
2376 symoffs
, stroffs
, secstrings
, strmap
);
2381 struct _ddebug
*debug
;
2382 unsigned int num_debug
;
2384 debug
= section_objs(hdr
, sechdrs
, secstrings
, "__verbose",
2385 sizeof(*debug
), &num_debug
);
2387 dynamic_debug_setup(debug
, num_debug
);
2390 err
= module_finalize(hdr
, sechdrs
, mod
);
2394 /* flush the icache in correct context */
2399 * Flush the instruction cache, since we've played with text.
2400 * Do it before processing of module parameters, so the module
2401 * can provide parameter accessor functions of its own.
2403 if (mod
->module_init
)
2404 flush_icache_range((unsigned long)mod
->module_init
,
2405 (unsigned long)mod
->module_init
2407 flush_icache_range((unsigned long)mod
->module_core
,
2408 (unsigned long)mod
->module_core
+ mod
->core_size
);
2413 if (section_addr(hdr
, sechdrs
, secstrings
, "__obsparm"))
2414 printk(KERN_WARNING
"%s: Ignoring obsolete parameters\n",
2417 /* Now sew it into the lists so we can get lockdep and oops
2418 * info during argument parsing. Noone should access us, since
2419 * strong_try_module_get() will fail.
2420 * lockdep/oops can run asynchronous, so use the RCU list insertion
2421 * function to insert in a way safe to concurrent readers.
2422 * The mutex protects against concurrent writers.
2424 list_add_rcu(&mod
->list
, &modules
);
2426 err
= parse_args(mod
->name
, mod
->args
, mod
->kp
, mod
->num_kp
, NULL
);
2430 err
= mod_sysfs_setup(mod
, mod
->kp
, mod
->num_kp
);
2433 add_sect_attrs(mod
, hdr
->e_shnum
, secstrings
, sechdrs
);
2434 add_notes_attrs(mod
, hdr
->e_shnum
, secstrings
, sechdrs
);
2436 /* Get rid of temporary copy */
2439 trace_module_load(mod
);
2445 /* Unlink carefully: kallsyms could be walking list. */
2446 list_del_rcu(&mod
->list
);
2447 synchronize_sched();
2448 module_arch_cleanup(mod
);
2451 kobject_del(&mod
->mkobj
.kobj
);
2452 kobject_put(&mod
->mkobj
.kobj
);
2454 module_unload_free(mod
);
2455 #if defined(CONFIG_MODULE_UNLOAD)
2456 free_percpu(mod
->refptr
);
2459 module_free(mod
, mod
->module_init
);
2461 module_free(mod
, mod
->module_core
);
2462 /* mod will be freed with core. Don't access it beyond this line! */
2464 percpu_modfree(mod
);
2470 return ERR_PTR(err
);
2473 printk(KERN_ERR
"Module len %lu truncated\n", len
);
2478 /* Call module constructors. */
2479 static void do_mod_ctors(struct module
*mod
)
2481 #ifdef CONFIG_CONSTRUCTORS
2484 for (i
= 0; i
< mod
->num_ctors
; i
++)
2489 /* This is where the real work happens */
2490 SYSCALL_DEFINE3(init_module
, void __user
*, umod
,
2491 unsigned long, len
, const char __user
*, uargs
)
2496 /* Must have permission */
2497 if (!capable(CAP_SYS_MODULE
) || modules_disabled
)
2500 /* Only one module load at a time, please */
2501 if (mutex_lock_interruptible(&module_mutex
) != 0)
2504 /* Do all the hard work */
2505 mod
= load_module(umod
, len
, uargs
);
2507 mutex_unlock(&module_mutex
);
2508 return PTR_ERR(mod
);
2511 /* Drop lock so they can recurse */
2512 mutex_unlock(&module_mutex
);
2514 blocking_notifier_call_chain(&module_notify_list
,
2515 MODULE_STATE_COMING
, mod
);
2518 /* Start the module */
2519 if (mod
->init
!= NULL
)
2520 ret
= do_one_initcall(mod
->init
);
2522 /* Init routine failed: abort. Try to protect us from
2523 buggy refcounters. */
2524 mod
->state
= MODULE_STATE_GOING
;
2525 synchronize_sched();
2527 blocking_notifier_call_chain(&module_notify_list
,
2528 MODULE_STATE_GOING
, mod
);
2529 mutex_lock(&module_mutex
);
2531 mutex_unlock(&module_mutex
);
2532 wake_up(&module_wq
);
2537 "%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
2538 "%s: loading module anyway...\n",
2539 __func__
, mod
->name
, ret
,
2544 /* Now it's a first class citizen! Wake up anyone waiting for it. */
2545 mod
->state
= MODULE_STATE_LIVE
;
2546 wake_up(&module_wq
);
2547 blocking_notifier_call_chain(&module_notify_list
,
2548 MODULE_STATE_LIVE
, mod
);
2550 /* We need to finish all async code before the module init sequence is done */
2551 async_synchronize_full();
2553 mutex_lock(&module_mutex
);
2554 /* Drop initial reference. */
2556 trim_init_extable(mod
);
2557 #ifdef CONFIG_KALLSYMS
2558 mod
->num_symtab
= mod
->core_num_syms
;
2559 mod
->symtab
= mod
->core_symtab
;
2560 mod
->strtab
= mod
->core_strtab
;
2562 module_free(mod
, mod
->module_init
);
2563 mod
->module_init
= NULL
;
2565 mod
->init_text_size
= 0;
2566 mutex_unlock(&module_mutex
);
2571 static inline int within(unsigned long addr
, void *start
, unsigned long size
)
2573 return ((void *)addr
>= start
&& (void *)addr
< start
+ size
);
2576 #ifdef CONFIG_KALLSYMS
2578 * This ignores the intensely annoying "mapping symbols" found
2579 * in ARM ELF files: $a, $t and $d.
2581 static inline int is_arm_mapping_symbol(const char *str
)
2583 return str
[0] == '$' && strchr("atd", str
[1])
2584 && (str
[2] == '\0' || str
[2] == '.');
2587 static const char *get_ksymbol(struct module
*mod
,
2589 unsigned long *size
,
2590 unsigned long *offset
)
2592 unsigned int i
, best
= 0;
2593 unsigned long nextval
;
2595 /* At worse, next value is at end of module */
2596 if (within_module_init(addr
, mod
))
2597 nextval
= (unsigned long)mod
->module_init
+mod
->init_text_size
;
2599 nextval
= (unsigned long)mod
->module_core
+mod
->core_text_size
;
2601 /* Scan for closest preceeding symbol, and next symbol. (ELF
2602 starts real symbols at 1). */
2603 for (i
= 1; i
< mod
->num_symtab
; i
++) {
2604 if (mod
->symtab
[i
].st_shndx
== SHN_UNDEF
)
2607 /* We ignore unnamed symbols: they're uninformative
2608 * and inserted at a whim. */
2609 if (mod
->symtab
[i
].st_value
<= addr
2610 && mod
->symtab
[i
].st_value
> mod
->symtab
[best
].st_value
2611 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
2612 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
2614 if (mod
->symtab
[i
].st_value
> addr
2615 && mod
->symtab
[i
].st_value
< nextval
2616 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
2617 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
2618 nextval
= mod
->symtab
[i
].st_value
;
2625 *size
= nextval
- mod
->symtab
[best
].st_value
;
2627 *offset
= addr
- mod
->symtab
[best
].st_value
;
2628 return mod
->strtab
+ mod
->symtab
[best
].st_name
;
2631 /* For kallsyms to ask for address resolution. NULL means not found. Careful
2632 * not to lock to avoid deadlock on oopses, simply disable preemption. */
2633 const char *module_address_lookup(unsigned long addr
,
2634 unsigned long *size
,
2635 unsigned long *offset
,
2640 const char *ret
= NULL
;
2643 list_for_each_entry_rcu(mod
, &modules
, list
) {
2644 if (within_module_init(addr
, mod
) ||
2645 within_module_core(addr
, mod
)) {
2647 *modname
= mod
->name
;
2648 ret
= get_ksymbol(mod
, addr
, size
, offset
);
2652 /* Make a copy in here where it's safe */
2654 strncpy(namebuf
, ret
, KSYM_NAME_LEN
- 1);
2661 int lookup_module_symbol_name(unsigned long addr
, char *symname
)
2666 list_for_each_entry_rcu(mod
, &modules
, list
) {
2667 if (within_module_init(addr
, mod
) ||
2668 within_module_core(addr
, mod
)) {
2671 sym
= get_ksymbol(mod
, addr
, NULL
, NULL
);
2674 strlcpy(symname
, sym
, KSYM_NAME_LEN
);
2684 int lookup_module_symbol_attrs(unsigned long addr
, unsigned long *size
,
2685 unsigned long *offset
, char *modname
, char *name
)
2690 list_for_each_entry_rcu(mod
, &modules
, list
) {
2691 if (within_module_init(addr
, mod
) ||
2692 within_module_core(addr
, mod
)) {
2695 sym
= get_ksymbol(mod
, addr
, size
, offset
);
2699 strlcpy(modname
, mod
->name
, MODULE_NAME_LEN
);
2701 strlcpy(name
, sym
, KSYM_NAME_LEN
);
2711 int module_get_kallsym(unsigned int symnum
, unsigned long *value
, char *type
,
2712 char *name
, char *module_name
, int *exported
)
2717 list_for_each_entry_rcu(mod
, &modules
, list
) {
2718 if (symnum
< mod
->num_symtab
) {
2719 *value
= mod
->symtab
[symnum
].st_value
;
2720 *type
= mod
->symtab
[symnum
].st_info
;
2721 strlcpy(name
, mod
->strtab
+ mod
->symtab
[symnum
].st_name
,
2723 strlcpy(module_name
, mod
->name
, MODULE_NAME_LEN
);
2724 *exported
= is_exported(name
, *value
, mod
);
2728 symnum
-= mod
->num_symtab
;
2734 static unsigned long mod_find_symname(struct module
*mod
, const char *name
)
2738 for (i
= 0; i
< mod
->num_symtab
; i
++)
2739 if (strcmp(name
, mod
->strtab
+mod
->symtab
[i
].st_name
) == 0 &&
2740 mod
->symtab
[i
].st_info
!= 'U')
2741 return mod
->symtab
[i
].st_value
;
2745 /* Look for this name: can be of form module:name. */
2746 unsigned long module_kallsyms_lookup_name(const char *name
)
2750 unsigned long ret
= 0;
2752 /* Don't lock: we're in enough trouble already. */
2754 if ((colon
= strchr(name
, ':')) != NULL
) {
2756 if ((mod
= find_module(name
)) != NULL
)
2757 ret
= mod_find_symname(mod
, colon
+1);
2760 list_for_each_entry_rcu(mod
, &modules
, list
)
2761 if ((ret
= mod_find_symname(mod
, name
)) != 0)
2768 int module_kallsyms_on_each_symbol(int (*fn
)(void *, const char *,
2769 struct module
*, unsigned long),
2776 list_for_each_entry(mod
, &modules
, list
) {
2777 for (i
= 0; i
< mod
->num_symtab
; i
++) {
2778 ret
= fn(data
, mod
->strtab
+ mod
->symtab
[i
].st_name
,
2779 mod
, mod
->symtab
[i
].st_value
);
2786 #endif /* CONFIG_KALLSYMS */
2788 static char *module_flags(struct module
*mod
, char *buf
)
2793 mod
->state
== MODULE_STATE_GOING
||
2794 mod
->state
== MODULE_STATE_COMING
) {
2796 if (mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
))
2798 if (mod
->taints
& (1 << TAINT_FORCED_MODULE
))
2800 if (mod
->taints
& (1 << TAINT_CRAP
))
2803 * TAINT_FORCED_RMMOD: could be added.
2804 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2808 /* Show a - for module-is-being-unloaded */
2809 if (mod
->state
== MODULE_STATE_GOING
)
2811 /* Show a + for module-is-being-loaded */
2812 if (mod
->state
== MODULE_STATE_COMING
)
2821 #ifdef CONFIG_PROC_FS
2822 /* Called by the /proc file system to return a list of modules. */
2823 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
2825 mutex_lock(&module_mutex
);
2826 return seq_list_start(&modules
, *pos
);
2829 static void *m_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
2831 return seq_list_next(p
, &modules
, pos
);
2834 static void m_stop(struct seq_file
*m
, void *p
)
2836 mutex_unlock(&module_mutex
);
2839 static int m_show(struct seq_file
*m
, void *p
)
2841 struct module
*mod
= list_entry(p
, struct module
, list
);
2844 seq_printf(m
, "%s %u",
2845 mod
->name
, mod
->init_size
+ mod
->core_size
);
2846 print_unload_info(m
, mod
);
2848 /* Informative for users. */
2849 seq_printf(m
, " %s",
2850 mod
->state
== MODULE_STATE_GOING
? "Unloading":
2851 mod
->state
== MODULE_STATE_COMING
? "Loading":
2853 /* Used by oprofile and other similar tools. */
2854 seq_printf(m
, " 0x%p", mod
->module_core
);
2858 seq_printf(m
, " %s", module_flags(mod
, buf
));
2860 seq_printf(m
, "\n");
2864 /* Format: modulename size refcount deps address
2866 Where refcount is a number or -, and deps is a comma-separated list
2869 static const struct seq_operations modules_op
= {
2876 static int modules_open(struct inode
*inode
, struct file
*file
)
2878 return seq_open(file
, &modules_op
);
2881 static const struct file_operations proc_modules_operations
= {
2882 .open
= modules_open
,
2884 .llseek
= seq_lseek
,
2885 .release
= seq_release
,
2888 static int __init
proc_modules_init(void)
2890 proc_create("modules", 0, NULL
, &proc_modules_operations
);
2893 module_init(proc_modules_init
);
2896 /* Given an address, look for it in the module exception tables. */
2897 const struct exception_table_entry
*search_module_extables(unsigned long addr
)
2899 const struct exception_table_entry
*e
= NULL
;
2903 list_for_each_entry_rcu(mod
, &modules
, list
) {
2904 if (mod
->num_exentries
== 0)
2907 e
= search_extable(mod
->extable
,
2908 mod
->extable
+ mod
->num_exentries
- 1,
2915 /* Now, if we found one, we are running inside it now, hence
2916 we cannot unload the module, hence no refcnt needed. */
2921 * is_module_address - is this address inside a module?
2922 * @addr: the address to check.
2924 * See is_module_text_address() if you simply want to see if the address
2925 * is code (not data).
2927 bool is_module_address(unsigned long addr
)
2932 ret
= __module_address(addr
) != NULL
;
2939 * __module_address - get the module which contains an address.
2940 * @addr: the address.
2942 * Must be called with preempt disabled or module mutex held so that
2943 * module doesn't get freed during this.
2945 struct module
*__module_address(unsigned long addr
)
2949 if (addr
< module_addr_min
|| addr
> module_addr_max
)
2952 list_for_each_entry_rcu(mod
, &modules
, list
)
2953 if (within_module_core(addr
, mod
)
2954 || within_module_init(addr
, mod
))
2958 EXPORT_SYMBOL_GPL(__module_address
);
2961 * is_module_text_address - is this address inside module code?
2962 * @addr: the address to check.
2964 * See is_module_address() if you simply want to see if the address is
2965 * anywhere in a module. See kernel_text_address() for testing if an
2966 * address corresponds to kernel or module code.
2968 bool is_module_text_address(unsigned long addr
)
2973 ret
= __module_text_address(addr
) != NULL
;
2980 * __module_text_address - get the module whose code contains an address.
2981 * @addr: the address.
2983 * Must be called with preempt disabled or module mutex held so that
2984 * module doesn't get freed during this.
2986 struct module
*__module_text_address(unsigned long addr
)
2988 struct module
*mod
= __module_address(addr
);
2990 /* Make sure it's within the text section. */
2991 if (!within(addr
, mod
->module_init
, mod
->init_text_size
)
2992 && !within(addr
, mod
->module_core
, mod
->core_text_size
))
2997 EXPORT_SYMBOL_GPL(__module_text_address
);
2999 /* Don't grab lock, we're oopsing. */
3000 void print_modules(void)
3005 printk(KERN_DEFAULT
"Modules linked in:");
3006 /* Most callers should already have preempt disabled, but make sure */
3008 list_for_each_entry_rcu(mod
, &modules
, list
)
3009 printk(" %s%s", mod
->name
, module_flags(mod
, buf
));
3011 if (last_unloaded_module
[0])
3012 printk(" [last unloaded: %s]", last_unloaded_module
);
3016 #ifdef CONFIG_MODVERSIONS
3017 /* Generate the signature for all relevant module structures here.
3018 * If these change, we don't want to try to parse the module. */
3019 void module_layout(struct module
*mod
,
3020 struct modversion_info
*ver
,
3021 struct kernel_param
*kp
,
3022 struct kernel_symbol
*ks
,
3023 struct tracepoint
*tp
)
3026 EXPORT_SYMBOL(module_layout
);
3029 #ifdef CONFIG_TRACEPOINTS
3030 void module_update_tracepoints(void)
3034 mutex_lock(&module_mutex
);
3035 list_for_each_entry(mod
, &modules
, list
)
3037 tracepoint_update_probe_range(mod
->tracepoints
,
3038 mod
->tracepoints
+ mod
->num_tracepoints
);
3039 mutex_unlock(&module_mutex
);
3043 * Returns 0 if current not found.
3044 * Returns 1 if current found.
3046 int module_get_iter_tracepoints(struct tracepoint_iter
*iter
)
3048 struct module
*iter_mod
;
3051 mutex_lock(&module_mutex
);
3052 list_for_each_entry(iter_mod
, &modules
, list
) {
3053 if (!iter_mod
->taints
) {
3055 * Sorted module list
3057 if (iter_mod
< iter
->module
)
3059 else if (iter_mod
> iter
->module
)
3060 iter
->tracepoint
= NULL
;
3061 found
= tracepoint_get_iter_range(&iter
->tracepoint
,
3062 iter_mod
->tracepoints
,
3063 iter_mod
->tracepoints
3064 + iter_mod
->num_tracepoints
);
3066 iter
->module
= iter_mod
;
3071 mutex_unlock(&module_mutex
);