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>
56 #include <linux/kmemleak.h>
58 #define CREATE_TRACE_POINTS
59 #include <trace/events/module.h>
61 EXPORT_TRACEPOINT_SYMBOL(module_get
);
66 #define DEBUGP(fmt , a...)
69 #ifndef ARCH_SHF_SMALL
70 #define ARCH_SHF_SMALL 0
73 /* If this is set, the section belongs in the init part of the module */
74 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
76 /* List of modules, protected by module_mutex or preempt_disable
77 * (delete uses stop_machine/add uses RCU list operations). */
78 DEFINE_MUTEX(module_mutex
);
79 EXPORT_SYMBOL_GPL(module_mutex
);
80 static LIST_HEAD(modules
);
82 /* Block module loading/unloading? */
83 int modules_disabled
= 0;
85 /* Waiting for a module to finish initializing? */
86 static DECLARE_WAIT_QUEUE_HEAD(module_wq
);
88 static BLOCKING_NOTIFIER_HEAD(module_notify_list
);
90 /* Bounds of module allocation, for speeding __module_address */
91 static unsigned long module_addr_min
= -1UL, module_addr_max
= 0;
93 int register_module_notifier(struct notifier_block
* nb
)
95 return blocking_notifier_chain_register(&module_notify_list
, nb
);
97 EXPORT_SYMBOL(register_module_notifier
);
99 int unregister_module_notifier(struct notifier_block
* nb
)
101 return blocking_notifier_chain_unregister(&module_notify_list
, nb
);
103 EXPORT_SYMBOL(unregister_module_notifier
);
105 /* We require a truly strong try_module_get(): 0 means failure due to
106 ongoing or failed initialization etc. */
107 static inline int strong_try_module_get(struct module
*mod
)
109 if (mod
&& mod
->state
== MODULE_STATE_COMING
)
111 if (try_module_get(mod
))
117 static inline void add_taint_module(struct module
*mod
, unsigned flag
)
120 mod
->taints
|= (1U << flag
);
124 * A thread that wants to hold a reference to a module only while it
125 * is running can call this to safely exit. nfsd and lockd use this.
127 void __module_put_and_exit(struct module
*mod
, long code
)
132 EXPORT_SYMBOL(__module_put_and_exit
);
134 /* Find a module section: 0 means not found. */
135 static unsigned int find_sec(Elf_Ehdr
*hdr
,
137 const char *secstrings
,
142 for (i
= 1; i
< hdr
->e_shnum
; i
++)
143 /* Alloc bit cleared means "ignore it." */
144 if ((sechdrs
[i
].sh_flags
& SHF_ALLOC
)
145 && strcmp(secstrings
+sechdrs
[i
].sh_name
, name
) == 0)
150 /* Find a module section, or NULL. */
151 static void *section_addr(Elf_Ehdr
*hdr
, Elf_Shdr
*shdrs
,
152 const char *secstrings
, const char *name
)
154 /* Section 0 has sh_addr 0. */
155 return (void *)shdrs
[find_sec(hdr
, shdrs
, secstrings
, name
)].sh_addr
;
158 /* Find a module section, or NULL. Fill in number of "objects" in section. */
159 static void *section_objs(Elf_Ehdr
*hdr
,
161 const char *secstrings
,
166 unsigned int sec
= find_sec(hdr
, sechdrs
, secstrings
, name
);
168 /* Section 0 has sh_addr 0 and sh_size 0. */
169 *num
= sechdrs
[sec
].sh_size
/ object_size
;
170 return (void *)sechdrs
[sec
].sh_addr
;
173 /* Provided by the linker */
174 extern const struct kernel_symbol __start___ksymtab
[];
175 extern const struct kernel_symbol __stop___ksymtab
[];
176 extern const struct kernel_symbol __start___ksymtab_gpl
[];
177 extern const struct kernel_symbol __stop___ksymtab_gpl
[];
178 extern const struct kernel_symbol __start___ksymtab_gpl_future
[];
179 extern const struct kernel_symbol __stop___ksymtab_gpl_future
[];
180 extern const struct kernel_symbol __start___ksymtab_gpl_future
[];
181 extern const struct kernel_symbol __stop___ksymtab_gpl_future
[];
182 extern const unsigned long __start___kcrctab
[];
183 extern const unsigned long __start___kcrctab_gpl
[];
184 extern const unsigned long __start___kcrctab_gpl_future
[];
185 #ifdef CONFIG_UNUSED_SYMBOLS
186 extern const struct kernel_symbol __start___ksymtab_unused
[];
187 extern const struct kernel_symbol __stop___ksymtab_unused
[];
188 extern const struct kernel_symbol __start___ksymtab_unused_gpl
[];
189 extern const struct kernel_symbol __stop___ksymtab_unused_gpl
[];
190 extern const unsigned long __start___kcrctab_unused
[];
191 extern const unsigned long __start___kcrctab_unused_gpl
[];
194 #ifndef CONFIG_MODVERSIONS
195 #define symversion(base, idx) NULL
197 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
200 static bool each_symbol_in_section(const struct symsearch
*arr
,
201 unsigned int arrsize
,
202 struct module
*owner
,
203 bool (*fn
)(const struct symsearch
*syms
,
204 struct module
*owner
,
205 unsigned int symnum
, void *data
),
210 for (j
= 0; j
< arrsize
; j
++) {
211 for (i
= 0; i
< arr
[j
].stop
- arr
[j
].start
; i
++)
212 if (fn(&arr
[j
], owner
, i
, data
))
219 /* Returns true as soon as fn returns true, otherwise false. */
220 bool each_symbol(bool (*fn
)(const struct symsearch
*arr
, struct module
*owner
,
221 unsigned int symnum
, void *data
), void *data
)
224 const struct symsearch arr
[] = {
225 { __start___ksymtab
, __stop___ksymtab
, __start___kcrctab
,
226 NOT_GPL_ONLY
, false },
227 { __start___ksymtab_gpl
, __stop___ksymtab_gpl
,
228 __start___kcrctab_gpl
,
230 { __start___ksymtab_gpl_future
, __stop___ksymtab_gpl_future
,
231 __start___kcrctab_gpl_future
,
232 WILL_BE_GPL_ONLY
, false },
233 #ifdef CONFIG_UNUSED_SYMBOLS
234 { __start___ksymtab_unused
, __stop___ksymtab_unused
,
235 __start___kcrctab_unused
,
236 NOT_GPL_ONLY
, true },
237 { __start___ksymtab_unused_gpl
, __stop___ksymtab_unused_gpl
,
238 __start___kcrctab_unused_gpl
,
243 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), NULL
, fn
, data
))
246 list_for_each_entry_rcu(mod
, &modules
, list
) {
247 struct symsearch arr
[] = {
248 { mod
->syms
, mod
->syms
+ mod
->num_syms
, mod
->crcs
,
249 NOT_GPL_ONLY
, false },
250 { mod
->gpl_syms
, mod
->gpl_syms
+ mod
->num_gpl_syms
,
253 { mod
->gpl_future_syms
,
254 mod
->gpl_future_syms
+ mod
->num_gpl_future_syms
,
255 mod
->gpl_future_crcs
,
256 WILL_BE_GPL_ONLY
, false },
257 #ifdef CONFIG_UNUSED_SYMBOLS
259 mod
->unused_syms
+ mod
->num_unused_syms
,
261 NOT_GPL_ONLY
, true },
262 { mod
->unused_gpl_syms
,
263 mod
->unused_gpl_syms
+ mod
->num_unused_gpl_syms
,
264 mod
->unused_gpl_crcs
,
269 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), mod
, fn
, data
))
274 EXPORT_SYMBOL_GPL(each_symbol
);
276 struct find_symbol_arg
{
283 struct module
*owner
;
284 const unsigned long *crc
;
285 const struct kernel_symbol
*sym
;
288 static bool find_symbol_in_section(const struct symsearch
*syms
,
289 struct module
*owner
,
290 unsigned int symnum
, void *data
)
292 struct find_symbol_arg
*fsa
= data
;
294 if (strcmp(syms
->start
[symnum
].name
, fsa
->name
) != 0)
298 if (syms
->licence
== GPL_ONLY
)
300 if (syms
->licence
== WILL_BE_GPL_ONLY
&& fsa
->warn
) {
301 printk(KERN_WARNING
"Symbol %s is being used "
302 "by a non-GPL module, which will not "
303 "be allowed in the future\n", fsa
->name
);
304 printk(KERN_WARNING
"Please see the file "
305 "Documentation/feature-removal-schedule.txt "
306 "in the kernel source tree for more details.\n");
310 #ifdef CONFIG_UNUSED_SYMBOLS
311 if (syms
->unused
&& fsa
->warn
) {
312 printk(KERN_WARNING
"Symbol %s is marked as UNUSED, "
313 "however this module is using it.\n", fsa
->name
);
315 "This symbol will go away in the future.\n");
317 "Please evalute if this is the right api to use and if "
318 "it really is, submit a report the linux kernel "
319 "mailinglist together with submitting your code for "
325 fsa
->crc
= symversion(syms
->crcs
, symnum
);
326 fsa
->sym
= &syms
->start
[symnum
];
330 /* Find a symbol and return it, along with, (optional) crc and
331 * (optional) module which owns it */
332 const struct kernel_symbol
*find_symbol(const char *name
,
333 struct module
**owner
,
334 const unsigned long **crc
,
338 struct find_symbol_arg fsa
;
344 if (each_symbol(find_symbol_in_section
, &fsa
)) {
352 DEBUGP("Failed to find symbol %s\n", name
);
355 EXPORT_SYMBOL_GPL(find_symbol
);
357 /* Search for module by name: must hold module_mutex. */
358 struct module
*find_module(const char *name
)
362 list_for_each_entry(mod
, &modules
, list
) {
363 if (strcmp(mod
->name
, name
) == 0)
368 EXPORT_SYMBOL_GPL(find_module
);
372 #ifdef CONFIG_HAVE_DYNAMIC_PER_CPU_AREA
374 static void *percpu_modalloc(unsigned long size
, unsigned long align
,
379 if (align
> PAGE_SIZE
) {
380 printk(KERN_WARNING
"%s: per-cpu alignment %li > %li\n",
381 name
, align
, PAGE_SIZE
);
385 ptr
= __alloc_reserved_percpu(size
, align
);
388 "Could not allocate %lu bytes percpu data\n", size
);
392 static void percpu_modfree(void *freeme
)
397 #else /* ... !CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
399 /* Number of blocks used and allocated. */
400 static unsigned int pcpu_num_used
, pcpu_num_allocated
;
401 /* Size of each block. -ve means used. */
402 static int *pcpu_size
;
404 static int split_block(unsigned int i
, unsigned short size
)
406 /* Reallocation required? */
407 if (pcpu_num_used
+ 1 > pcpu_num_allocated
) {
410 new = krealloc(pcpu_size
, sizeof(new[0])*pcpu_num_allocated
*2,
415 pcpu_num_allocated
*= 2;
419 /* Insert a new subblock */
420 memmove(&pcpu_size
[i
+1], &pcpu_size
[i
],
421 sizeof(pcpu_size
[0]) * (pcpu_num_used
- i
));
424 pcpu_size
[i
+1] -= size
;
429 static inline unsigned int block_size(int val
)
436 static void *percpu_modalloc(unsigned long size
, unsigned long align
,
444 if (align
> PAGE_SIZE
) {
445 printk(KERN_WARNING
"%s: per-cpu alignment %li > %li\n",
446 name
, align
, PAGE_SIZE
);
450 ptr
= __per_cpu_start
;
451 for (i
= 0; i
< pcpu_num_used
; ptr
+= block_size(pcpu_size
[i
]), i
++) {
452 /* Extra for alignment requirement. */
453 extra
= ALIGN((unsigned long)ptr
, align
) - (unsigned long)ptr
;
454 BUG_ON(i
== 0 && extra
!= 0);
456 if (pcpu_size
[i
] < 0 || pcpu_size
[i
] < extra
+ size
)
459 /* Transfer extra to previous block. */
460 if (pcpu_size
[i
-1] < 0)
461 pcpu_size
[i
-1] -= extra
;
463 pcpu_size
[i
-1] += extra
;
464 pcpu_size
[i
] -= extra
;
467 /* Split block if warranted */
468 if (pcpu_size
[i
] - size
> sizeof(unsigned long))
469 if (!split_block(i
, size
))
472 /* add the per-cpu scanning areas */
473 for_each_possible_cpu(cpu
)
474 kmemleak_alloc(ptr
+ per_cpu_offset(cpu
), size
, 0,
478 pcpu_size
[i
] = -pcpu_size
[i
];
482 printk(KERN_WARNING
"Could not allocate %lu bytes percpu data\n",
487 static void percpu_modfree(void *freeme
)
490 void *ptr
= __per_cpu_start
+ block_size(pcpu_size
[0]);
493 /* First entry is core kernel percpu data. */
494 for (i
= 1; i
< pcpu_num_used
; ptr
+= block_size(pcpu_size
[i
]), i
++) {
496 pcpu_size
[i
] = -pcpu_size
[i
];
503 /* remove the per-cpu scanning areas */
504 for_each_possible_cpu(cpu
)
505 kmemleak_free(freeme
+ per_cpu_offset(cpu
));
507 /* Merge with previous? */
508 if (pcpu_size
[i
-1] >= 0) {
509 pcpu_size
[i
-1] += pcpu_size
[i
];
511 memmove(&pcpu_size
[i
], &pcpu_size
[i
+1],
512 (pcpu_num_used
- i
) * sizeof(pcpu_size
[0]));
515 /* Merge with next? */
516 if (i
+1 < pcpu_num_used
&& pcpu_size
[i
+1] >= 0) {
517 pcpu_size
[i
] += pcpu_size
[i
+1];
519 memmove(&pcpu_size
[i
+1], &pcpu_size
[i
+2],
520 (pcpu_num_used
- (i
+1)) * sizeof(pcpu_size
[0]));
524 static int percpu_modinit(void)
527 pcpu_num_allocated
= 2;
528 pcpu_size
= kmalloc(sizeof(pcpu_size
[0]) * pcpu_num_allocated
,
530 /* Static in-kernel percpu data (used). */
531 pcpu_size
[0] = -(__per_cpu_end
-__per_cpu_start
);
533 pcpu_size
[1] = PERCPU_ENOUGH_ROOM
+ pcpu_size
[0];
534 if (pcpu_size
[1] < 0) {
535 printk(KERN_ERR
"No per-cpu room for modules.\n");
541 __initcall(percpu_modinit
);
543 #endif /* CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
545 static unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
547 const char *secstrings
)
549 return find_sec(hdr
, sechdrs
, secstrings
, ".data.percpu");
552 static void percpu_modcopy(void *pcpudest
, const void *from
, unsigned long size
)
556 for_each_possible_cpu(cpu
)
557 memcpy(pcpudest
+ per_cpu_offset(cpu
), from
, size
);
560 #else /* ... !CONFIG_SMP */
562 static inline void *percpu_modalloc(unsigned long size
, unsigned long align
,
567 static inline void percpu_modfree(void *pcpuptr
)
571 static inline unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
573 const char *secstrings
)
577 static inline void percpu_modcopy(void *pcpudst
, const void *src
,
580 /* pcpusec should be 0, and size of that section should be 0. */
584 #endif /* CONFIG_SMP */
586 #define MODINFO_ATTR(field) \
587 static void setup_modinfo_##field(struct module *mod, const char *s) \
589 mod->field = kstrdup(s, GFP_KERNEL); \
591 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
592 struct module *mod, char *buffer) \
594 return sprintf(buffer, "%s\n", mod->field); \
596 static int modinfo_##field##_exists(struct module *mod) \
598 return mod->field != NULL; \
600 static void free_modinfo_##field(struct module *mod) \
605 static struct module_attribute modinfo_##field = { \
606 .attr = { .name = __stringify(field), .mode = 0444 }, \
607 .show = show_modinfo_##field, \
608 .setup = setup_modinfo_##field, \
609 .test = modinfo_##field##_exists, \
610 .free = free_modinfo_##field, \
613 MODINFO_ATTR(version
);
614 MODINFO_ATTR(srcversion
);
616 static char last_unloaded_module
[MODULE_NAME_LEN
+1];
618 #ifdef CONFIG_MODULE_UNLOAD
619 /* Init the unload section of the module. */
620 static void module_unload_init(struct module
*mod
)
624 INIT_LIST_HEAD(&mod
->modules_which_use_me
);
625 for_each_possible_cpu(cpu
)
626 local_set(__module_ref_addr(mod
, cpu
), 0);
627 /* Hold reference count during initialization. */
628 local_set(__module_ref_addr(mod
, raw_smp_processor_id()), 1);
629 /* Backwards compatibility macros put refcount during init. */
630 mod
->waiter
= current
;
633 /* modules using other modules */
636 struct list_head list
;
637 struct module
*module_which_uses
;
640 /* Does a already use b? */
641 static int already_uses(struct module
*a
, struct module
*b
)
643 struct module_use
*use
;
645 list_for_each_entry(use
, &b
->modules_which_use_me
, list
) {
646 if (use
->module_which_uses
== a
) {
647 DEBUGP("%s uses %s!\n", a
->name
, b
->name
);
651 DEBUGP("%s does not use %s!\n", a
->name
, b
->name
);
655 /* Module a uses b */
656 int use_module(struct module
*a
, struct module
*b
)
658 struct module_use
*use
;
661 if (b
== NULL
|| already_uses(a
, b
)) return 1;
663 /* If we're interrupted or time out, we fail. */
664 if (wait_event_interruptible_timeout(
665 module_wq
, (err
= strong_try_module_get(b
)) != -EBUSY
,
667 printk("%s: gave up waiting for init of module %s.\n",
672 /* If strong_try_module_get() returned a different error, we fail. */
676 DEBUGP("Allocating new usage for %s.\n", a
->name
);
677 use
= kmalloc(sizeof(*use
), GFP_ATOMIC
);
679 printk("%s: out of memory loading\n", a
->name
);
684 use
->module_which_uses
= a
;
685 list_add(&use
->list
, &b
->modules_which_use_me
);
686 no_warn
= sysfs_create_link(b
->holders_dir
, &a
->mkobj
.kobj
, a
->name
);
689 EXPORT_SYMBOL_GPL(use_module
);
691 /* Clear the unload stuff of the module. */
692 static void module_unload_free(struct module
*mod
)
696 list_for_each_entry(i
, &modules
, list
) {
697 struct module_use
*use
;
699 list_for_each_entry(use
, &i
->modules_which_use_me
, list
) {
700 if (use
->module_which_uses
== mod
) {
701 DEBUGP("%s unusing %s\n", mod
->name
, i
->name
);
703 list_del(&use
->list
);
705 sysfs_remove_link(i
->holders_dir
, mod
->name
);
706 /* There can be at most one match. */
713 #ifdef CONFIG_MODULE_FORCE_UNLOAD
714 static inline int try_force_unload(unsigned int flags
)
716 int ret
= (flags
& O_TRUNC
);
718 add_taint(TAINT_FORCED_RMMOD
);
722 static inline int try_force_unload(unsigned int flags
)
726 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
735 /* Whole machine is stopped with interrupts off when this runs. */
736 static int __try_stop_module(void *_sref
)
738 struct stopref
*sref
= _sref
;
740 /* If it's not unused, quit unless we're forcing. */
741 if (module_refcount(sref
->mod
) != 0) {
742 if (!(*sref
->forced
= try_force_unload(sref
->flags
)))
746 /* Mark it as dying. */
747 sref
->mod
->state
= MODULE_STATE_GOING
;
751 static int try_stop_module(struct module
*mod
, int flags
, int *forced
)
753 if (flags
& O_NONBLOCK
) {
754 struct stopref sref
= { mod
, flags
, forced
};
756 return stop_machine(__try_stop_module
, &sref
, NULL
);
758 /* We don't need to stop the machine for this. */
759 mod
->state
= MODULE_STATE_GOING
;
765 unsigned int module_refcount(struct module
*mod
)
767 unsigned int total
= 0;
770 for_each_possible_cpu(cpu
)
771 total
+= local_read(__module_ref_addr(mod
, cpu
));
774 EXPORT_SYMBOL(module_refcount
);
776 /* This exists whether we can unload or not */
777 static void free_module(struct module
*mod
);
779 static void wait_for_zero_refcount(struct module
*mod
)
781 /* Since we might sleep for some time, release the mutex first */
782 mutex_unlock(&module_mutex
);
784 DEBUGP("Looking at refcount...\n");
785 set_current_state(TASK_UNINTERRUPTIBLE
);
786 if (module_refcount(mod
) == 0)
790 current
->state
= TASK_RUNNING
;
791 mutex_lock(&module_mutex
);
794 SYSCALL_DEFINE2(delete_module
, const char __user
*, name_user
,
798 char name
[MODULE_NAME_LEN
];
801 if (!capable(CAP_SYS_MODULE
) || modules_disabled
)
804 if (strncpy_from_user(name
, name_user
, MODULE_NAME_LEN
-1) < 0)
806 name
[MODULE_NAME_LEN
-1] = '\0';
808 /* Create stop_machine threads since free_module relies on
809 * a non-failing stop_machine call. */
810 ret
= stop_machine_create();
814 if (mutex_lock_interruptible(&module_mutex
) != 0) {
819 mod
= find_module(name
);
825 if (!list_empty(&mod
->modules_which_use_me
)) {
826 /* Other modules depend on us: get rid of them first. */
831 /* Doing init or already dying? */
832 if (mod
->state
!= MODULE_STATE_LIVE
) {
833 /* FIXME: if (force), slam module count and wake up
835 DEBUGP("%s already dying\n", mod
->name
);
840 /* If it has an init func, it must have an exit func to unload */
841 if (mod
->init
&& !mod
->exit
) {
842 forced
= try_force_unload(flags
);
844 /* This module can't be removed */
850 /* Set this up before setting mod->state */
851 mod
->waiter
= current
;
853 /* Stop the machine so refcounts can't move and disable module. */
854 ret
= try_stop_module(mod
, flags
, &forced
);
858 /* Never wait if forced. */
859 if (!forced
&& module_refcount(mod
) != 0)
860 wait_for_zero_refcount(mod
);
862 mutex_unlock(&module_mutex
);
863 /* Final destruction now noone is using it. */
864 if (mod
->exit
!= NULL
)
866 blocking_notifier_call_chain(&module_notify_list
,
867 MODULE_STATE_GOING
, mod
);
868 async_synchronize_full();
869 mutex_lock(&module_mutex
);
870 /* Store the name of the last unloaded module for diagnostic purposes */
871 strlcpy(last_unloaded_module
, mod
->name
, sizeof(last_unloaded_module
));
872 ddebug_remove_module(mod
->name
);
876 mutex_unlock(&module_mutex
);
878 stop_machine_destroy();
882 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
884 struct module_use
*use
;
885 int printed_something
= 0;
887 seq_printf(m
, " %u ", module_refcount(mod
));
889 /* Always include a trailing , so userspace can differentiate
890 between this and the old multi-field proc format. */
891 list_for_each_entry(use
, &mod
->modules_which_use_me
, list
) {
892 printed_something
= 1;
893 seq_printf(m
, "%s,", use
->module_which_uses
->name
);
896 if (mod
->init
!= NULL
&& mod
->exit
== NULL
) {
897 printed_something
= 1;
898 seq_printf(m
, "[permanent],");
901 if (!printed_something
)
905 void __symbol_put(const char *symbol
)
907 struct module
*owner
;
910 if (!find_symbol(symbol
, &owner
, NULL
, true, false))
915 EXPORT_SYMBOL(__symbol_put
);
917 void symbol_put_addr(void *addr
)
919 struct module
*modaddr
;
921 if (core_kernel_text((unsigned long)addr
))
924 /* module_text_address is safe here: we're supposed to have reference
925 * to module from symbol_get, so it can't go away. */
926 modaddr
= __module_text_address((unsigned long)addr
);
930 EXPORT_SYMBOL_GPL(symbol_put_addr
);
932 static ssize_t
show_refcnt(struct module_attribute
*mattr
,
933 struct module
*mod
, char *buffer
)
935 return sprintf(buffer
, "%u\n", module_refcount(mod
));
938 static struct module_attribute refcnt
= {
939 .attr
= { .name
= "refcnt", .mode
= 0444 },
943 void module_put(struct module
*module
)
946 unsigned int cpu
= get_cpu();
947 local_dec(__module_ref_addr(module
, cpu
));
948 trace_module_put(module
, _RET_IP_
,
949 local_read(__module_ref_addr(module
, cpu
)));
950 /* Maybe they're waiting for us to drop reference? */
951 if (unlikely(!module_is_live(module
)))
952 wake_up_process(module
->waiter
);
956 EXPORT_SYMBOL(module_put
);
958 #else /* !CONFIG_MODULE_UNLOAD */
959 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
961 /* We don't know the usage count, or what modules are using. */
962 seq_printf(m
, " - -");
965 static inline void module_unload_free(struct module
*mod
)
969 int use_module(struct module
*a
, struct module
*b
)
971 return strong_try_module_get(b
) == 0;
973 EXPORT_SYMBOL_GPL(use_module
);
975 static inline void module_unload_init(struct module
*mod
)
978 #endif /* CONFIG_MODULE_UNLOAD */
980 static ssize_t
show_initstate(struct module_attribute
*mattr
,
981 struct module
*mod
, char *buffer
)
983 const char *state
= "unknown";
985 switch (mod
->state
) {
986 case MODULE_STATE_LIVE
:
989 case MODULE_STATE_COMING
:
992 case MODULE_STATE_GOING
:
996 return sprintf(buffer
, "%s\n", state
);
999 static struct module_attribute initstate
= {
1000 .attr
= { .name
= "initstate", .mode
= 0444 },
1001 .show
= show_initstate
,
1004 static struct module_attribute
*modinfo_attrs
[] = {
1006 &modinfo_srcversion
,
1008 #ifdef CONFIG_MODULE_UNLOAD
1014 static const char vermagic
[] = VERMAGIC_STRING
;
1016 static int try_to_force_load(struct module
*mod
, const char *reason
)
1018 #ifdef CONFIG_MODULE_FORCE_LOAD
1019 if (!test_taint(TAINT_FORCED_MODULE
))
1020 printk(KERN_WARNING
"%s: %s: kernel tainted.\n",
1022 add_taint_module(mod
, TAINT_FORCED_MODULE
);
1029 #ifdef CONFIG_MODVERSIONS
1030 static int check_version(Elf_Shdr
*sechdrs
,
1031 unsigned int versindex
,
1032 const char *symname
,
1034 const unsigned long *crc
)
1036 unsigned int i
, num_versions
;
1037 struct modversion_info
*versions
;
1039 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1043 /* No versions at all? modprobe --force does this. */
1045 return try_to_force_load(mod
, symname
) == 0;
1047 versions
= (void *) sechdrs
[versindex
].sh_addr
;
1048 num_versions
= sechdrs
[versindex
].sh_size
1049 / sizeof(struct modversion_info
);
1051 for (i
= 0; i
< num_versions
; i
++) {
1052 if (strcmp(versions
[i
].name
, symname
) != 0)
1055 if (versions
[i
].crc
== *crc
)
1057 DEBUGP("Found checksum %lX vs module %lX\n",
1058 *crc
, versions
[i
].crc
);
1062 printk(KERN_WARNING
"%s: no symbol version for %s\n",
1063 mod
->name
, symname
);
1067 printk("%s: disagrees about version of symbol %s\n",
1068 mod
->name
, symname
);
1072 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1073 unsigned int versindex
,
1076 const unsigned long *crc
;
1078 if (!find_symbol(MODULE_SYMBOL_PREFIX
"module_layout", NULL
,
1081 return check_version(sechdrs
, versindex
, "module_layout", mod
, crc
);
1084 /* First part is kernel version, which we ignore if module has crcs. */
1085 static inline int same_magic(const char *amagic
, const char *bmagic
,
1089 amagic
+= strcspn(amagic
, " ");
1090 bmagic
+= strcspn(bmagic
, " ");
1092 return strcmp(amagic
, bmagic
) == 0;
1095 static inline int check_version(Elf_Shdr
*sechdrs
,
1096 unsigned int versindex
,
1097 const char *symname
,
1099 const unsigned long *crc
)
1104 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1105 unsigned int versindex
,
1111 static inline int same_magic(const char *amagic
, const char *bmagic
,
1114 return strcmp(amagic
, bmagic
) == 0;
1116 #endif /* CONFIG_MODVERSIONS */
1118 /* Resolve a symbol for this module. I.e. if we find one, record usage.
1119 Must be holding module_mutex. */
1120 static const struct kernel_symbol
*resolve_symbol(Elf_Shdr
*sechdrs
,
1121 unsigned int versindex
,
1125 struct module
*owner
;
1126 const struct kernel_symbol
*sym
;
1127 const unsigned long *crc
;
1129 sym
= find_symbol(name
, &owner
, &crc
,
1130 !(mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
)), true);
1131 /* use_module can fail due to OOM,
1132 or module initialization or unloading */
1134 if (!check_version(sechdrs
, versindex
, name
, mod
, crc
) ||
1135 !use_module(mod
, owner
))
1142 * /sys/module/foo/sections stuff
1143 * J. Corbet <corbet@lwn.net>
1145 #if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
1146 struct module_sect_attr
1148 struct module_attribute mattr
;
1150 unsigned long address
;
1153 struct module_sect_attrs
1155 struct attribute_group grp
;
1156 unsigned int nsections
;
1157 struct module_sect_attr attrs
[0];
1160 static ssize_t
module_sect_show(struct module_attribute
*mattr
,
1161 struct module
*mod
, char *buf
)
1163 struct module_sect_attr
*sattr
=
1164 container_of(mattr
, struct module_sect_attr
, mattr
);
1165 return sprintf(buf
, "0x%lx\n", sattr
->address
);
1168 static void free_sect_attrs(struct module_sect_attrs
*sect_attrs
)
1170 unsigned int section
;
1172 for (section
= 0; section
< sect_attrs
->nsections
; section
++)
1173 kfree(sect_attrs
->attrs
[section
].name
);
1177 static void add_sect_attrs(struct module
*mod
, unsigned int nsect
,
1178 char *secstrings
, Elf_Shdr
*sechdrs
)
1180 unsigned int nloaded
= 0, i
, size
[2];
1181 struct module_sect_attrs
*sect_attrs
;
1182 struct module_sect_attr
*sattr
;
1183 struct attribute
**gattr
;
1185 /* Count loaded sections and allocate structures */
1186 for (i
= 0; i
< nsect
; i
++)
1187 if (sechdrs
[i
].sh_flags
& SHF_ALLOC
)
1189 size
[0] = ALIGN(sizeof(*sect_attrs
)
1190 + nloaded
* sizeof(sect_attrs
->attrs
[0]),
1191 sizeof(sect_attrs
->grp
.attrs
[0]));
1192 size
[1] = (nloaded
+ 1) * sizeof(sect_attrs
->grp
.attrs
[0]);
1193 sect_attrs
= kzalloc(size
[0] + size
[1], GFP_KERNEL
);
1194 if (sect_attrs
== NULL
)
1197 /* Setup section attributes. */
1198 sect_attrs
->grp
.name
= "sections";
1199 sect_attrs
->grp
.attrs
= (void *)sect_attrs
+ size
[0];
1201 sect_attrs
->nsections
= 0;
1202 sattr
= §_attrs
->attrs
[0];
1203 gattr
= §_attrs
->grp
.attrs
[0];
1204 for (i
= 0; i
< nsect
; i
++) {
1205 if (! (sechdrs
[i
].sh_flags
& SHF_ALLOC
))
1207 sattr
->address
= sechdrs
[i
].sh_addr
;
1208 sattr
->name
= kstrdup(secstrings
+ sechdrs
[i
].sh_name
,
1210 if (sattr
->name
== NULL
)
1212 sect_attrs
->nsections
++;
1213 sattr
->mattr
.show
= module_sect_show
;
1214 sattr
->mattr
.store
= NULL
;
1215 sattr
->mattr
.attr
.name
= sattr
->name
;
1216 sattr
->mattr
.attr
.mode
= S_IRUGO
;
1217 *(gattr
++) = &(sattr
++)->mattr
.attr
;
1221 if (sysfs_create_group(&mod
->mkobj
.kobj
, §_attrs
->grp
))
1224 mod
->sect_attrs
= sect_attrs
;
1227 free_sect_attrs(sect_attrs
);
1230 static void remove_sect_attrs(struct module
*mod
)
1232 if (mod
->sect_attrs
) {
1233 sysfs_remove_group(&mod
->mkobj
.kobj
,
1234 &mod
->sect_attrs
->grp
);
1235 /* We are positive that no one is using any sect attrs
1236 * at this point. Deallocate immediately. */
1237 free_sect_attrs(mod
->sect_attrs
);
1238 mod
->sect_attrs
= NULL
;
1243 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1246 struct module_notes_attrs
{
1247 struct kobject
*dir
;
1249 struct bin_attribute attrs
[0];
1252 static ssize_t
module_notes_read(struct kobject
*kobj
,
1253 struct bin_attribute
*bin_attr
,
1254 char *buf
, loff_t pos
, size_t count
)
1257 * The caller checked the pos and count against our size.
1259 memcpy(buf
, bin_attr
->private + pos
, count
);
1263 static void free_notes_attrs(struct module_notes_attrs
*notes_attrs
,
1266 if (notes_attrs
->dir
) {
1268 sysfs_remove_bin_file(notes_attrs
->dir
,
1269 ¬es_attrs
->attrs
[i
]);
1270 kobject_put(notes_attrs
->dir
);
1275 static void add_notes_attrs(struct module
*mod
, unsigned int nsect
,
1276 char *secstrings
, Elf_Shdr
*sechdrs
)
1278 unsigned int notes
, loaded
, i
;
1279 struct module_notes_attrs
*notes_attrs
;
1280 struct bin_attribute
*nattr
;
1282 /* Count notes sections and allocate structures. */
1284 for (i
= 0; i
< nsect
; i
++)
1285 if ((sechdrs
[i
].sh_flags
& SHF_ALLOC
) &&
1286 (sechdrs
[i
].sh_type
== SHT_NOTE
))
1292 notes_attrs
= kzalloc(sizeof(*notes_attrs
)
1293 + notes
* sizeof(notes_attrs
->attrs
[0]),
1295 if (notes_attrs
== NULL
)
1298 notes_attrs
->notes
= notes
;
1299 nattr
= ¬es_attrs
->attrs
[0];
1300 for (loaded
= i
= 0; i
< nsect
; ++i
) {
1301 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
1303 if (sechdrs
[i
].sh_type
== SHT_NOTE
) {
1304 nattr
->attr
.name
= mod
->sect_attrs
->attrs
[loaded
].name
;
1305 nattr
->attr
.mode
= S_IRUGO
;
1306 nattr
->size
= sechdrs
[i
].sh_size
;
1307 nattr
->private = (void *) sechdrs
[i
].sh_addr
;
1308 nattr
->read
= module_notes_read
;
1314 notes_attrs
->dir
= kobject_create_and_add("notes", &mod
->mkobj
.kobj
);
1315 if (!notes_attrs
->dir
)
1318 for (i
= 0; i
< notes
; ++i
)
1319 if (sysfs_create_bin_file(notes_attrs
->dir
,
1320 ¬es_attrs
->attrs
[i
]))
1323 mod
->notes_attrs
= notes_attrs
;
1327 free_notes_attrs(notes_attrs
, i
);
1330 static void remove_notes_attrs(struct module
*mod
)
1332 if (mod
->notes_attrs
)
1333 free_notes_attrs(mod
->notes_attrs
, mod
->notes_attrs
->notes
);
1338 static inline void add_sect_attrs(struct module
*mod
, unsigned int nsect
,
1339 char *sectstrings
, Elf_Shdr
*sechdrs
)
1343 static inline void remove_sect_attrs(struct module
*mod
)
1347 static inline void add_notes_attrs(struct module
*mod
, unsigned int nsect
,
1348 char *sectstrings
, Elf_Shdr
*sechdrs
)
1352 static inline void remove_notes_attrs(struct module
*mod
)
1358 int module_add_modinfo_attrs(struct module
*mod
)
1360 struct module_attribute
*attr
;
1361 struct module_attribute
*temp_attr
;
1365 mod
->modinfo_attrs
= kzalloc((sizeof(struct module_attribute
) *
1366 (ARRAY_SIZE(modinfo_attrs
) + 1)),
1368 if (!mod
->modinfo_attrs
)
1371 temp_attr
= mod
->modinfo_attrs
;
1372 for (i
= 0; (attr
= modinfo_attrs
[i
]) && !error
; i
++) {
1374 (attr
->test
&& attr
->test(mod
))) {
1375 memcpy(temp_attr
, attr
, sizeof(*temp_attr
));
1376 error
= sysfs_create_file(&mod
->mkobj
.kobj
,&temp_attr
->attr
);
1383 void module_remove_modinfo_attrs(struct module
*mod
)
1385 struct module_attribute
*attr
;
1388 for (i
= 0; (attr
= &mod
->modinfo_attrs
[i
]); i
++) {
1389 /* pick a field to test for end of list */
1390 if (!attr
->attr
.name
)
1392 sysfs_remove_file(&mod
->mkobj
.kobj
,&attr
->attr
);
1396 kfree(mod
->modinfo_attrs
);
1399 int mod_sysfs_init(struct module
*mod
)
1402 struct kobject
*kobj
;
1404 if (!module_sysfs_initialized
) {
1405 printk(KERN_ERR
"%s: module sysfs not initialized\n",
1411 kobj
= kset_find_obj(module_kset
, mod
->name
);
1413 printk(KERN_ERR
"%s: module is already loaded\n", mod
->name
);
1419 mod
->mkobj
.mod
= mod
;
1421 memset(&mod
->mkobj
.kobj
, 0, sizeof(mod
->mkobj
.kobj
));
1422 mod
->mkobj
.kobj
.kset
= module_kset
;
1423 err
= kobject_init_and_add(&mod
->mkobj
.kobj
, &module_ktype
, NULL
,
1426 kobject_put(&mod
->mkobj
.kobj
);
1428 /* delay uevent until full sysfs population */
1433 int mod_sysfs_setup(struct module
*mod
,
1434 struct kernel_param
*kparam
,
1435 unsigned int num_params
)
1439 mod
->holders_dir
= kobject_create_and_add("holders", &mod
->mkobj
.kobj
);
1440 if (!mod
->holders_dir
) {
1445 err
= module_param_sysfs_setup(mod
, kparam
, num_params
);
1447 goto out_unreg_holders
;
1449 err
= module_add_modinfo_attrs(mod
);
1451 goto out_unreg_param
;
1453 kobject_uevent(&mod
->mkobj
.kobj
, KOBJ_ADD
);
1457 module_param_sysfs_remove(mod
);
1459 kobject_put(mod
->holders_dir
);
1461 kobject_put(&mod
->mkobj
.kobj
);
1465 static void mod_sysfs_fini(struct module
*mod
)
1467 kobject_put(&mod
->mkobj
.kobj
);
1470 #else /* CONFIG_SYSFS */
1472 static void mod_sysfs_fini(struct module
*mod
)
1476 #endif /* CONFIG_SYSFS */
1478 static void mod_kobject_remove(struct module
*mod
)
1480 module_remove_modinfo_attrs(mod
);
1481 module_param_sysfs_remove(mod
);
1482 kobject_put(mod
->mkobj
.drivers_dir
);
1483 kobject_put(mod
->holders_dir
);
1484 mod_sysfs_fini(mod
);
1488 * unlink the module with the whole machine is stopped with interrupts off
1489 * - this defends against kallsyms not taking locks
1491 static int __unlink_module(void *_mod
)
1493 struct module
*mod
= _mod
;
1494 list_del(&mod
->list
);
1498 /* Free a module, remove from lists, etc (must hold module_mutex). */
1499 static void free_module(struct module
*mod
)
1501 trace_module_free(mod
);
1503 /* Delete from various lists */
1504 stop_machine(__unlink_module
, mod
, NULL
);
1505 remove_notes_attrs(mod
);
1506 remove_sect_attrs(mod
);
1507 mod_kobject_remove(mod
);
1509 /* Arch-specific cleanup. */
1510 module_arch_cleanup(mod
);
1512 /* Module unload stuff */
1513 module_unload_free(mod
);
1515 /* Free any allocated parameters. */
1516 destroy_params(mod
->kp
, mod
->num_kp
);
1518 /* This may be NULL, but that's OK */
1519 module_free(mod
, mod
->module_init
);
1522 percpu_modfree(mod
->percpu
);
1523 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
1525 percpu_modfree(mod
->refptr
);
1527 /* Free lock-classes: */
1528 lockdep_free_key_range(mod
->module_core
, mod
->core_size
);
1530 /* Finally, free the core (containing the module structure) */
1531 module_free(mod
, mod
->module_core
);
1534 void *__symbol_get(const char *symbol
)
1536 struct module
*owner
;
1537 const struct kernel_symbol
*sym
;
1540 sym
= find_symbol(symbol
, &owner
, NULL
, true, true);
1541 if (sym
&& strong_try_module_get(owner
))
1545 return sym
? (void *)sym
->value
: NULL
;
1547 EXPORT_SYMBOL_GPL(__symbol_get
);
1550 * Ensure that an exported symbol [global namespace] does not already exist
1551 * in the kernel or in some other module's exported symbol table.
1553 static int verify_export_symbols(struct module
*mod
)
1556 struct module
*owner
;
1557 const struct kernel_symbol
*s
;
1559 const struct kernel_symbol
*sym
;
1562 { mod
->syms
, mod
->num_syms
},
1563 { mod
->gpl_syms
, mod
->num_gpl_syms
},
1564 { mod
->gpl_future_syms
, mod
->num_gpl_future_syms
},
1565 #ifdef CONFIG_UNUSED_SYMBOLS
1566 { mod
->unused_syms
, mod
->num_unused_syms
},
1567 { mod
->unused_gpl_syms
, mod
->num_unused_gpl_syms
},
1571 for (i
= 0; i
< ARRAY_SIZE(arr
); i
++) {
1572 for (s
= arr
[i
].sym
; s
< arr
[i
].sym
+ arr
[i
].num
; s
++) {
1573 if (find_symbol(s
->name
, &owner
, NULL
, true, false)) {
1575 "%s: exports duplicate symbol %s"
1577 mod
->name
, s
->name
, module_name(owner
));
1585 /* Change all symbols so that st_value encodes the pointer directly. */
1586 static int simplify_symbols(Elf_Shdr
*sechdrs
,
1587 unsigned int symindex
,
1589 unsigned int versindex
,
1590 unsigned int pcpuindex
,
1593 Elf_Sym
*sym
= (void *)sechdrs
[symindex
].sh_addr
;
1594 unsigned long secbase
;
1595 unsigned int i
, n
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1597 const struct kernel_symbol
*ksym
;
1599 for (i
= 1; i
< n
; i
++) {
1600 switch (sym
[i
].st_shndx
) {
1602 /* We compiled with -fno-common. These are not
1603 supposed to happen. */
1604 DEBUGP("Common symbol: %s\n", strtab
+ sym
[i
].st_name
);
1605 printk("%s: please compile with -fno-common\n",
1611 /* Don't need to do anything */
1612 DEBUGP("Absolute symbol: 0x%08lx\n",
1613 (long)sym
[i
].st_value
);
1617 ksym
= resolve_symbol(sechdrs
, versindex
,
1618 strtab
+ sym
[i
].st_name
, mod
);
1619 /* Ok if resolved. */
1621 sym
[i
].st_value
= ksym
->value
;
1626 if (ELF_ST_BIND(sym
[i
].st_info
) == STB_WEAK
)
1629 printk(KERN_WARNING
"%s: Unknown symbol %s\n",
1630 mod
->name
, strtab
+ sym
[i
].st_name
);
1635 /* Divert to percpu allocation if a percpu var. */
1636 if (sym
[i
].st_shndx
== pcpuindex
)
1637 secbase
= (unsigned long)mod
->percpu
;
1639 secbase
= sechdrs
[sym
[i
].st_shndx
].sh_addr
;
1640 sym
[i
].st_value
+= secbase
;
1648 /* Additional bytes needed by arch in front of individual sections */
1649 unsigned int __weak
arch_mod_section_prepend(struct module
*mod
,
1650 unsigned int section
)
1652 /* default implementation just returns zero */
1656 /* Update size with this section: return offset. */
1657 static long get_offset(struct module
*mod
, unsigned int *size
,
1658 Elf_Shdr
*sechdr
, unsigned int section
)
1662 *size
+= arch_mod_section_prepend(mod
, section
);
1663 ret
= ALIGN(*size
, sechdr
->sh_addralign
?: 1);
1664 *size
= ret
+ sechdr
->sh_size
;
1668 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1669 might -- code, read-only data, read-write data, small data. Tally
1670 sizes, and place the offsets into sh_entsize fields: high bit means it
1672 static void layout_sections(struct module
*mod
,
1673 const Elf_Ehdr
*hdr
,
1675 const char *secstrings
)
1677 static unsigned long const masks
[][2] = {
1678 /* NOTE: all executable code must be the first section
1679 * in this array; otherwise modify the text_size
1680 * finder in the two loops below */
1681 { SHF_EXECINSTR
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1682 { SHF_ALLOC
, SHF_WRITE
| ARCH_SHF_SMALL
},
1683 { SHF_WRITE
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1684 { ARCH_SHF_SMALL
| SHF_ALLOC
, 0 }
1688 for (i
= 0; i
< hdr
->e_shnum
; i
++)
1689 sechdrs
[i
].sh_entsize
= ~0UL;
1691 DEBUGP("Core section allocation order:\n");
1692 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1693 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1694 Elf_Shdr
*s
= &sechdrs
[i
];
1696 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1697 || (s
->sh_flags
& masks
[m
][1])
1698 || s
->sh_entsize
!= ~0UL
1699 || strstarts(secstrings
+ s
->sh_name
, ".init"))
1701 s
->sh_entsize
= get_offset(mod
, &mod
->core_size
, s
, i
);
1702 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1705 mod
->core_text_size
= mod
->core_size
;
1708 DEBUGP("Init section allocation order:\n");
1709 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1710 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1711 Elf_Shdr
*s
= &sechdrs
[i
];
1713 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1714 || (s
->sh_flags
& masks
[m
][1])
1715 || s
->sh_entsize
!= ~0UL
1716 || !strstarts(secstrings
+ s
->sh_name
, ".init"))
1718 s
->sh_entsize
= (get_offset(mod
, &mod
->init_size
, s
, i
)
1719 | INIT_OFFSET_MASK
);
1720 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1723 mod
->init_text_size
= mod
->init_size
;
1727 static void set_license(struct module
*mod
, const char *license
)
1730 license
= "unspecified";
1732 if (!license_is_gpl_compatible(license
)) {
1733 if (!test_taint(TAINT_PROPRIETARY_MODULE
))
1734 printk(KERN_WARNING
"%s: module license '%s' taints "
1735 "kernel.\n", mod
->name
, license
);
1736 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
1740 /* Parse tag=value strings from .modinfo section */
1741 static char *next_string(char *string
, unsigned long *secsize
)
1743 /* Skip non-zero chars */
1746 if ((*secsize
)-- <= 1)
1750 /* Skip any zero padding. */
1751 while (!string
[0]) {
1753 if ((*secsize
)-- <= 1)
1759 static char *get_modinfo(Elf_Shdr
*sechdrs
,
1764 unsigned int taglen
= strlen(tag
);
1765 unsigned long size
= sechdrs
[info
].sh_size
;
1767 for (p
= (char *)sechdrs
[info
].sh_addr
; p
; p
= next_string(p
, &size
)) {
1768 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
1769 return p
+ taglen
+ 1;
1774 static void setup_modinfo(struct module
*mod
, Elf_Shdr
*sechdrs
,
1775 unsigned int infoindex
)
1777 struct module_attribute
*attr
;
1780 for (i
= 0; (attr
= modinfo_attrs
[i
]); i
++) {
1783 get_modinfo(sechdrs
,
1789 #ifdef CONFIG_KALLSYMS
1791 /* lookup symbol in given range of kernel_symbols */
1792 static const struct kernel_symbol
*lookup_symbol(const char *name
,
1793 const struct kernel_symbol
*start
,
1794 const struct kernel_symbol
*stop
)
1796 const struct kernel_symbol
*ks
= start
;
1797 for (; ks
< stop
; ks
++)
1798 if (strcmp(ks
->name
, name
) == 0)
1803 static int is_exported(const char *name
, unsigned long value
,
1804 const struct module
*mod
)
1806 const struct kernel_symbol
*ks
;
1808 ks
= lookup_symbol(name
, __start___ksymtab
, __stop___ksymtab
);
1810 ks
= lookup_symbol(name
, mod
->syms
, mod
->syms
+ mod
->num_syms
);
1811 return ks
!= NULL
&& ks
->value
== value
;
1815 static char elf_type(const Elf_Sym
*sym
,
1817 const char *secstrings
,
1820 if (ELF_ST_BIND(sym
->st_info
) == STB_WEAK
) {
1821 if (ELF_ST_TYPE(sym
->st_info
) == STT_OBJECT
)
1826 if (sym
->st_shndx
== SHN_UNDEF
)
1828 if (sym
->st_shndx
== SHN_ABS
)
1830 if (sym
->st_shndx
>= SHN_LORESERVE
)
1832 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_EXECINSTR
)
1834 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_ALLOC
1835 && sechdrs
[sym
->st_shndx
].sh_type
!= SHT_NOBITS
) {
1836 if (!(sechdrs
[sym
->st_shndx
].sh_flags
& SHF_WRITE
))
1838 else if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1843 if (sechdrs
[sym
->st_shndx
].sh_type
== SHT_NOBITS
) {
1844 if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1849 if (strstarts(secstrings
+ sechdrs
[sym
->st_shndx
].sh_name
, ".debug"))
1854 static void add_kallsyms(struct module
*mod
,
1856 unsigned int symindex
,
1857 unsigned int strindex
,
1858 const char *secstrings
)
1862 mod
->symtab
= (void *)sechdrs
[symindex
].sh_addr
;
1863 mod
->num_symtab
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1864 mod
->strtab
= (void *)sechdrs
[strindex
].sh_addr
;
1866 /* Set types up while we still have access to sections. */
1867 for (i
= 0; i
< mod
->num_symtab
; i
++)
1868 mod
->symtab
[i
].st_info
1869 = elf_type(&mod
->symtab
[i
], sechdrs
, secstrings
, mod
);
1872 static inline void add_kallsyms(struct module
*mod
,
1874 unsigned int symindex
,
1875 unsigned int strindex
,
1876 const char *secstrings
)
1879 #endif /* CONFIG_KALLSYMS */
1881 static void dynamic_debug_setup(struct _ddebug
*debug
, unsigned int num
)
1883 #ifdef CONFIG_DYNAMIC_DEBUG
1884 if (ddebug_add_module(debug
, num
, debug
->modname
))
1885 printk(KERN_ERR
"dynamic debug error adding module: %s\n",
1890 static void *module_alloc_update_bounds(unsigned long size
)
1892 void *ret
= module_alloc(size
);
1895 /* Update module bounds. */
1896 if ((unsigned long)ret
< module_addr_min
)
1897 module_addr_min
= (unsigned long)ret
;
1898 if ((unsigned long)ret
+ size
> module_addr_max
)
1899 module_addr_max
= (unsigned long)ret
+ size
;
1904 #ifdef CONFIG_DEBUG_KMEMLEAK
1905 static void kmemleak_load_module(struct module
*mod
, Elf_Ehdr
*hdr
,
1906 Elf_Shdr
*sechdrs
, char *secstrings
)
1910 /* only scan the sections containing data */
1911 kmemleak_scan_area(mod
->module_core
, (unsigned long)mod
-
1912 (unsigned long)mod
->module_core
,
1913 sizeof(struct module
), GFP_KERNEL
);
1915 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
1916 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
1918 if (strncmp(secstrings
+ sechdrs
[i
].sh_name
, ".data", 5) != 0
1919 && strncmp(secstrings
+ sechdrs
[i
].sh_name
, ".bss", 4) != 0)
1922 kmemleak_scan_area(mod
->module_core
, sechdrs
[i
].sh_addr
-
1923 (unsigned long)mod
->module_core
,
1924 sechdrs
[i
].sh_size
, GFP_KERNEL
);
1928 static inline void kmemleak_load_module(struct module
*mod
, Elf_Ehdr
*hdr
,
1929 Elf_Shdr
*sechdrs
, char *secstrings
)
1934 /* Allocate and load the module: note that size of section 0 is always
1935 zero, and we rely on this for optional sections. */
1936 static noinline
struct module
*load_module(void __user
*umod
,
1938 const char __user
*uargs
)
1942 char *secstrings
, *args
, *modmagic
, *strtab
= NULL
;
1945 unsigned int symindex
= 0;
1946 unsigned int strindex
= 0;
1947 unsigned int modindex
, versindex
, infoindex
, pcpuindex
;
1950 void *percpu
= NULL
, *ptr
= NULL
; /* Stops spurious gcc warning */
1951 mm_segment_t old_fs
;
1953 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1955 if (len
< sizeof(*hdr
))
1956 return ERR_PTR(-ENOEXEC
);
1958 /* Suck in entire file: we'll want most of it. */
1959 /* vmalloc barfs on "unusual" numbers. Check here */
1960 if (len
> 64 * 1024 * 1024 || (hdr
= vmalloc(len
)) == NULL
)
1961 return ERR_PTR(-ENOMEM
);
1963 if (copy_from_user(hdr
, umod
, len
) != 0) {
1968 /* Sanity checks against insmoding binaries or wrong arch,
1969 weird elf version */
1970 if (memcmp(hdr
->e_ident
, ELFMAG
, SELFMAG
) != 0
1971 || hdr
->e_type
!= ET_REL
1972 || !elf_check_arch(hdr
)
1973 || hdr
->e_shentsize
!= sizeof(*sechdrs
)) {
1978 if (len
< hdr
->e_shoff
+ hdr
->e_shnum
* sizeof(Elf_Shdr
))
1981 /* Convenience variables */
1982 sechdrs
= (void *)hdr
+ hdr
->e_shoff
;
1983 secstrings
= (void *)hdr
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
1984 sechdrs
[0].sh_addr
= 0;
1986 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
1987 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
1988 && len
< sechdrs
[i
].sh_offset
+ sechdrs
[i
].sh_size
)
1991 /* Mark all sections sh_addr with their address in the
1993 sechdrs
[i
].sh_addr
= (size_t)hdr
+ sechdrs
[i
].sh_offset
;
1995 /* Internal symbols and strings. */
1996 if (sechdrs
[i
].sh_type
== SHT_SYMTAB
) {
1998 strindex
= sechdrs
[i
].sh_link
;
1999 strtab
= (char *)hdr
+ sechdrs
[strindex
].sh_offset
;
2001 #ifndef CONFIG_MODULE_UNLOAD
2002 /* Don't load .exit sections */
2003 if (strstarts(secstrings
+sechdrs
[i
].sh_name
, ".exit"))
2004 sechdrs
[i
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2008 modindex
= find_sec(hdr
, sechdrs
, secstrings
,
2009 ".gnu.linkonce.this_module");
2011 printk(KERN_WARNING
"No module found in object\n");
2015 /* This is temporary: point mod into copy of data. */
2016 mod
= (void *)sechdrs
[modindex
].sh_addr
;
2018 if (symindex
== 0) {
2019 printk(KERN_WARNING
"%s: module has no symbols (stripped?)\n",
2025 versindex
= find_sec(hdr
, sechdrs
, secstrings
, "__versions");
2026 infoindex
= find_sec(hdr
, sechdrs
, secstrings
, ".modinfo");
2027 pcpuindex
= find_pcpusec(hdr
, sechdrs
, secstrings
);
2029 /* Don't keep modinfo and version sections. */
2030 sechdrs
[infoindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2031 sechdrs
[versindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2032 #ifdef CONFIG_KALLSYMS
2033 /* Keep symbol and string tables for decoding later. */
2034 sechdrs
[symindex
].sh_flags
|= SHF_ALLOC
;
2035 sechdrs
[strindex
].sh_flags
|= SHF_ALLOC
;
2038 /* Check module struct version now, before we try to use module. */
2039 if (!check_modstruct_version(sechdrs
, versindex
, mod
)) {
2044 modmagic
= get_modinfo(sechdrs
, infoindex
, "vermagic");
2045 /* This is allowed: modprobe --force will invalidate it. */
2047 err
= try_to_force_load(mod
, "bad vermagic");
2050 } else if (!same_magic(modmagic
, vermagic
, versindex
)) {
2051 printk(KERN_ERR
"%s: version magic '%s' should be '%s'\n",
2052 mod
->name
, modmagic
, vermagic
);
2057 staging
= get_modinfo(sechdrs
, infoindex
, "staging");
2059 add_taint_module(mod
, TAINT_CRAP
);
2060 printk(KERN_WARNING
"%s: module is from the staging directory,"
2061 " the quality is unknown, you have been warned.\n",
2065 /* Now copy in args */
2066 args
= strndup_user(uargs
, ~0UL >> 1);
2068 err
= PTR_ERR(args
);
2072 if (find_module(mod
->name
)) {
2077 mod
->state
= MODULE_STATE_COMING
;
2079 /* Allow arches to frob section contents and sizes. */
2080 err
= module_frob_arch_sections(hdr
, sechdrs
, secstrings
, mod
);
2085 /* We have a special allocation for this section. */
2086 percpu
= percpu_modalloc(sechdrs
[pcpuindex
].sh_size
,
2087 sechdrs
[pcpuindex
].sh_addralign
,
2093 sechdrs
[pcpuindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2094 mod
->percpu
= percpu
;
2097 /* Determine total sizes, and put offsets in sh_entsize. For now
2098 this is done generically; there doesn't appear to be any
2099 special cases for the architectures. */
2100 layout_sections(mod
, hdr
, sechdrs
, secstrings
);
2102 /* Do the allocs. */
2103 ptr
= module_alloc_update_bounds(mod
->core_size
);
2105 * The pointer to this block is stored in the module structure
2106 * which is inside the block. Just mark it as not being a
2109 kmemleak_not_leak(ptr
);
2114 memset(ptr
, 0, mod
->core_size
);
2115 mod
->module_core
= ptr
;
2117 ptr
= module_alloc_update_bounds(mod
->init_size
);
2119 * The pointer to this block is stored in the module structure
2120 * which is inside the block. This block doesn't need to be
2121 * scanned as it contains data and code that will be freed
2122 * after the module is initialized.
2124 kmemleak_ignore(ptr
);
2125 if (!ptr
&& mod
->init_size
) {
2129 memset(ptr
, 0, mod
->init_size
);
2130 mod
->module_init
= ptr
;
2132 /* Transfer each section which specifies SHF_ALLOC */
2133 DEBUGP("final section addresses:\n");
2134 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
2137 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
2140 if (sechdrs
[i
].sh_entsize
& INIT_OFFSET_MASK
)
2141 dest
= mod
->module_init
2142 + (sechdrs
[i
].sh_entsize
& ~INIT_OFFSET_MASK
);
2144 dest
= mod
->module_core
+ sechdrs
[i
].sh_entsize
;
2146 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
)
2147 memcpy(dest
, (void *)sechdrs
[i
].sh_addr
,
2148 sechdrs
[i
].sh_size
);
2149 /* Update sh_addr to point to copy in image. */
2150 sechdrs
[i
].sh_addr
= (unsigned long)dest
;
2151 DEBUGP("\t0x%lx %s\n", sechdrs
[i
].sh_addr
, secstrings
+ sechdrs
[i
].sh_name
);
2153 /* Module has been moved. */
2154 mod
= (void *)sechdrs
[modindex
].sh_addr
;
2155 kmemleak_load_module(mod
, hdr
, sechdrs
, secstrings
);
2157 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2158 mod
->refptr
= percpu_modalloc(sizeof(local_t
), __alignof__(local_t
),
2165 /* Now we've moved module, initialize linked lists, etc. */
2166 module_unload_init(mod
);
2168 /* add kobject, so we can reference it. */
2169 err
= mod_sysfs_init(mod
);
2173 /* Set up license info based on the info section */
2174 set_license(mod
, get_modinfo(sechdrs
, infoindex
, "license"));
2177 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2178 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2179 * using GPL-only symbols it needs.
2181 if (strcmp(mod
->name
, "ndiswrapper") == 0)
2182 add_taint(TAINT_PROPRIETARY_MODULE
);
2184 /* driverloader was caught wrongly pretending to be under GPL */
2185 if (strcmp(mod
->name
, "driverloader") == 0)
2186 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
2188 /* Set up MODINFO_ATTR fields */
2189 setup_modinfo(mod
, sechdrs
, infoindex
);
2191 /* Fix up syms, so that st_value is a pointer to location. */
2192 err
= simplify_symbols(sechdrs
, symindex
, strtab
, versindex
, pcpuindex
,
2197 /* Now we've got everything in the final locations, we can
2198 * find optional sections. */
2199 mod
->kp
= section_objs(hdr
, sechdrs
, secstrings
, "__param",
2200 sizeof(*mod
->kp
), &mod
->num_kp
);
2201 mod
->syms
= section_objs(hdr
, sechdrs
, secstrings
, "__ksymtab",
2202 sizeof(*mod
->syms
), &mod
->num_syms
);
2203 mod
->crcs
= section_addr(hdr
, sechdrs
, secstrings
, "__kcrctab");
2204 mod
->gpl_syms
= section_objs(hdr
, sechdrs
, secstrings
, "__ksymtab_gpl",
2205 sizeof(*mod
->gpl_syms
),
2206 &mod
->num_gpl_syms
);
2207 mod
->gpl_crcs
= section_addr(hdr
, sechdrs
, secstrings
, "__kcrctab_gpl");
2208 mod
->gpl_future_syms
= section_objs(hdr
, sechdrs
, secstrings
,
2209 "__ksymtab_gpl_future",
2210 sizeof(*mod
->gpl_future_syms
),
2211 &mod
->num_gpl_future_syms
);
2212 mod
->gpl_future_crcs
= section_addr(hdr
, sechdrs
, secstrings
,
2213 "__kcrctab_gpl_future");
2215 #ifdef CONFIG_UNUSED_SYMBOLS
2216 mod
->unused_syms
= section_objs(hdr
, sechdrs
, secstrings
,
2218 sizeof(*mod
->unused_syms
),
2219 &mod
->num_unused_syms
);
2220 mod
->unused_crcs
= section_addr(hdr
, sechdrs
, secstrings
,
2221 "__kcrctab_unused");
2222 mod
->unused_gpl_syms
= section_objs(hdr
, sechdrs
, secstrings
,
2223 "__ksymtab_unused_gpl",
2224 sizeof(*mod
->unused_gpl_syms
),
2225 &mod
->num_unused_gpl_syms
);
2226 mod
->unused_gpl_crcs
= section_addr(hdr
, sechdrs
, secstrings
,
2227 "__kcrctab_unused_gpl");
2229 #ifdef CONFIG_CONSTRUCTORS
2230 mod
->ctors
= section_objs(hdr
, sechdrs
, secstrings
, ".ctors",
2231 sizeof(*mod
->ctors
), &mod
->num_ctors
);
2234 #ifdef CONFIG_MARKERS
2235 mod
->markers
= section_objs(hdr
, sechdrs
, secstrings
, "__markers",
2236 sizeof(*mod
->markers
), &mod
->num_markers
);
2238 #ifdef CONFIG_TRACEPOINTS
2239 mod
->tracepoints
= section_objs(hdr
, sechdrs
, secstrings
,
2241 sizeof(*mod
->tracepoints
),
2242 &mod
->num_tracepoints
);
2244 #ifdef CONFIG_EVENT_TRACING
2245 mod
->trace_events
= section_objs(hdr
, sechdrs
, secstrings
,
2247 sizeof(*mod
->trace_events
),
2248 &mod
->num_trace_events
);
2250 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
2251 /* sechdrs[0].sh_size is always zero */
2252 mod
->ftrace_callsites
= section_objs(hdr
, sechdrs
, secstrings
,
2254 sizeof(*mod
->ftrace_callsites
),
2255 &mod
->num_ftrace_callsites
);
2257 #ifdef CONFIG_MODVERSIONS
2258 if ((mod
->num_syms
&& !mod
->crcs
)
2259 || (mod
->num_gpl_syms
&& !mod
->gpl_crcs
)
2260 || (mod
->num_gpl_future_syms
&& !mod
->gpl_future_crcs
)
2261 #ifdef CONFIG_UNUSED_SYMBOLS
2262 || (mod
->num_unused_syms
&& !mod
->unused_crcs
)
2263 || (mod
->num_unused_gpl_syms
&& !mod
->unused_gpl_crcs
)
2266 err
= try_to_force_load(mod
,
2267 "no versions for exported symbols");
2273 /* Now do relocations. */
2274 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
2275 const char *strtab
= (char *)sechdrs
[strindex
].sh_addr
;
2276 unsigned int info
= sechdrs
[i
].sh_info
;
2278 /* Not a valid relocation section? */
2279 if (info
>= hdr
->e_shnum
)
2282 /* Don't bother with non-allocated sections */
2283 if (!(sechdrs
[info
].sh_flags
& SHF_ALLOC
))
2286 if (sechdrs
[i
].sh_type
== SHT_REL
)
2287 err
= apply_relocate(sechdrs
, strtab
, symindex
, i
,mod
);
2288 else if (sechdrs
[i
].sh_type
== SHT_RELA
)
2289 err
= apply_relocate_add(sechdrs
, strtab
, symindex
, i
,
2295 /* Find duplicate symbols */
2296 err
= verify_export_symbols(mod
);
2300 /* Set up and sort exception table */
2301 mod
->extable
= section_objs(hdr
, sechdrs
, secstrings
, "__ex_table",
2302 sizeof(*mod
->extable
), &mod
->num_exentries
);
2303 sort_extable(mod
->extable
, mod
->extable
+ mod
->num_exentries
);
2305 /* Finally, copy percpu area over. */
2306 percpu_modcopy(mod
->percpu
, (void *)sechdrs
[pcpuindex
].sh_addr
,
2307 sechdrs
[pcpuindex
].sh_size
);
2309 add_kallsyms(mod
, sechdrs
, symindex
, strindex
, secstrings
);
2312 struct _ddebug
*debug
;
2313 unsigned int num_debug
;
2315 debug
= section_objs(hdr
, sechdrs
, secstrings
, "__verbose",
2316 sizeof(*debug
), &num_debug
);
2318 dynamic_debug_setup(debug
, num_debug
);
2321 err
= module_finalize(hdr
, sechdrs
, mod
);
2325 /* flush the icache in correct context */
2330 * Flush the instruction cache, since we've played with text.
2331 * Do it before processing of module parameters, so the module
2332 * can provide parameter accessor functions of its own.
2334 if (mod
->module_init
)
2335 flush_icache_range((unsigned long)mod
->module_init
,
2336 (unsigned long)mod
->module_init
2338 flush_icache_range((unsigned long)mod
->module_core
,
2339 (unsigned long)mod
->module_core
+ mod
->core_size
);
2344 if (section_addr(hdr
, sechdrs
, secstrings
, "__obsparm"))
2345 printk(KERN_WARNING
"%s: Ignoring obsolete parameters\n",
2348 /* Now sew it into the lists so we can get lockdep and oops
2349 * info during argument parsing. Noone should access us, since
2350 * strong_try_module_get() will fail.
2351 * lockdep/oops can run asynchronous, so use the RCU list insertion
2352 * function to insert in a way safe to concurrent readers.
2353 * The mutex protects against concurrent writers.
2355 list_add_rcu(&mod
->list
, &modules
);
2357 err
= parse_args(mod
->name
, mod
->args
, mod
->kp
, mod
->num_kp
, NULL
);
2361 err
= mod_sysfs_setup(mod
, mod
->kp
, mod
->num_kp
);
2364 add_sect_attrs(mod
, hdr
->e_shnum
, secstrings
, sechdrs
);
2365 add_notes_attrs(mod
, hdr
->e_shnum
, secstrings
, sechdrs
);
2367 /* Get rid of temporary copy */
2370 trace_module_load(mod
);
2376 /* Unlink carefully: kallsyms could be walking list. */
2377 list_del_rcu(&mod
->list
);
2378 synchronize_sched();
2379 module_arch_cleanup(mod
);
2381 kobject_del(&mod
->mkobj
.kobj
);
2382 kobject_put(&mod
->mkobj
.kobj
);
2384 module_unload_free(mod
);
2385 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2387 percpu_modfree(mod
->refptr
);
2389 module_free(mod
, mod
->module_init
);
2391 module_free(mod
, mod
->module_core
);
2392 /* mod will be freed with core. Don't access it beyond this line! */
2395 percpu_modfree(percpu
);
2400 return ERR_PTR(err
);
2403 printk(KERN_ERR
"Module len %lu truncated\n", len
);
2408 /* Call module constructors. */
2409 static void do_mod_ctors(struct module
*mod
)
2411 #ifdef CONFIG_CONSTRUCTORS
2414 for (i
= 0; i
< mod
->num_ctors
; i
++)
2419 /* This is where the real work happens */
2420 SYSCALL_DEFINE3(init_module
, void __user
*, umod
,
2421 unsigned long, len
, const char __user
*, uargs
)
2426 /* Must have permission */
2427 if (!capable(CAP_SYS_MODULE
) || modules_disabled
)
2430 /* Only one module load at a time, please */
2431 if (mutex_lock_interruptible(&module_mutex
) != 0)
2434 /* Do all the hard work */
2435 mod
= load_module(umod
, len
, uargs
);
2437 mutex_unlock(&module_mutex
);
2438 return PTR_ERR(mod
);
2441 /* Drop lock so they can recurse */
2442 mutex_unlock(&module_mutex
);
2444 blocking_notifier_call_chain(&module_notify_list
,
2445 MODULE_STATE_COMING
, mod
);
2448 /* Start the module */
2449 if (mod
->init
!= NULL
)
2450 ret
= do_one_initcall(mod
->init
);
2452 /* Init routine failed: abort. Try to protect us from
2453 buggy refcounters. */
2454 mod
->state
= MODULE_STATE_GOING
;
2455 synchronize_sched();
2457 blocking_notifier_call_chain(&module_notify_list
,
2458 MODULE_STATE_GOING
, mod
);
2459 mutex_lock(&module_mutex
);
2461 mutex_unlock(&module_mutex
);
2462 wake_up(&module_wq
);
2467 "%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
2468 "%s: loading module anyway...\n",
2469 __func__
, mod
->name
, ret
,
2474 /* Now it's a first class citizen! Wake up anyone waiting for it. */
2475 mod
->state
= MODULE_STATE_LIVE
;
2476 wake_up(&module_wq
);
2477 blocking_notifier_call_chain(&module_notify_list
,
2478 MODULE_STATE_LIVE
, mod
);
2480 /* We need to finish all async code before the module init sequence is done */
2481 async_synchronize_full();
2483 mutex_lock(&module_mutex
);
2484 /* Drop initial reference. */
2486 trim_init_extable(mod
);
2487 module_free(mod
, mod
->module_init
);
2488 mod
->module_init
= NULL
;
2490 mod
->init_text_size
= 0;
2491 mutex_unlock(&module_mutex
);
2496 static inline int within(unsigned long addr
, void *start
, unsigned long size
)
2498 return ((void *)addr
>= start
&& (void *)addr
< start
+ size
);
2501 #ifdef CONFIG_KALLSYMS
2503 * This ignores the intensely annoying "mapping symbols" found
2504 * in ARM ELF files: $a, $t and $d.
2506 static inline int is_arm_mapping_symbol(const char *str
)
2508 return str
[0] == '$' && strchr("atd", str
[1])
2509 && (str
[2] == '\0' || str
[2] == '.');
2512 static const char *get_ksymbol(struct module
*mod
,
2514 unsigned long *size
,
2515 unsigned long *offset
)
2517 unsigned int i
, best
= 0;
2518 unsigned long nextval
;
2520 /* At worse, next value is at end of module */
2521 if (within_module_init(addr
, mod
))
2522 nextval
= (unsigned long)mod
->module_init
+mod
->init_text_size
;
2524 nextval
= (unsigned long)mod
->module_core
+mod
->core_text_size
;
2526 /* Scan for closest preceeding symbol, and next symbol. (ELF
2527 starts real symbols at 1). */
2528 for (i
= 1; i
< mod
->num_symtab
; i
++) {
2529 if (mod
->symtab
[i
].st_shndx
== SHN_UNDEF
)
2532 /* We ignore unnamed symbols: they're uninformative
2533 * and inserted at a whim. */
2534 if (mod
->symtab
[i
].st_value
<= addr
2535 && mod
->symtab
[i
].st_value
> mod
->symtab
[best
].st_value
2536 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
2537 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
2539 if (mod
->symtab
[i
].st_value
> addr
2540 && mod
->symtab
[i
].st_value
< nextval
2541 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
2542 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
2543 nextval
= mod
->symtab
[i
].st_value
;
2550 *size
= nextval
- mod
->symtab
[best
].st_value
;
2552 *offset
= addr
- mod
->symtab
[best
].st_value
;
2553 return mod
->strtab
+ mod
->symtab
[best
].st_name
;
2556 /* For kallsyms to ask for address resolution. NULL means not found. Careful
2557 * not to lock to avoid deadlock on oopses, simply disable preemption. */
2558 const char *module_address_lookup(unsigned long addr
,
2559 unsigned long *size
,
2560 unsigned long *offset
,
2565 const char *ret
= NULL
;
2568 list_for_each_entry_rcu(mod
, &modules
, list
) {
2569 if (within_module_init(addr
, mod
) ||
2570 within_module_core(addr
, mod
)) {
2572 *modname
= mod
->name
;
2573 ret
= get_ksymbol(mod
, addr
, size
, offset
);
2577 /* Make a copy in here where it's safe */
2579 strncpy(namebuf
, ret
, KSYM_NAME_LEN
- 1);
2586 int lookup_module_symbol_name(unsigned long addr
, char *symname
)
2591 list_for_each_entry_rcu(mod
, &modules
, list
) {
2592 if (within_module_init(addr
, mod
) ||
2593 within_module_core(addr
, mod
)) {
2596 sym
= get_ksymbol(mod
, addr
, NULL
, NULL
);
2599 strlcpy(symname
, sym
, KSYM_NAME_LEN
);
2609 int lookup_module_symbol_attrs(unsigned long addr
, unsigned long *size
,
2610 unsigned long *offset
, char *modname
, char *name
)
2615 list_for_each_entry_rcu(mod
, &modules
, list
) {
2616 if (within_module_init(addr
, mod
) ||
2617 within_module_core(addr
, mod
)) {
2620 sym
= get_ksymbol(mod
, addr
, size
, offset
);
2624 strlcpy(modname
, mod
->name
, MODULE_NAME_LEN
);
2626 strlcpy(name
, sym
, KSYM_NAME_LEN
);
2636 int module_get_kallsym(unsigned int symnum
, unsigned long *value
, char *type
,
2637 char *name
, char *module_name
, int *exported
)
2642 list_for_each_entry_rcu(mod
, &modules
, list
) {
2643 if (symnum
< mod
->num_symtab
) {
2644 *value
= mod
->symtab
[symnum
].st_value
;
2645 *type
= mod
->symtab
[symnum
].st_info
;
2646 strlcpy(name
, mod
->strtab
+ mod
->symtab
[symnum
].st_name
,
2648 strlcpy(module_name
, mod
->name
, MODULE_NAME_LEN
);
2649 *exported
= is_exported(name
, *value
, mod
);
2653 symnum
-= mod
->num_symtab
;
2659 static unsigned long mod_find_symname(struct module
*mod
, const char *name
)
2663 for (i
= 0; i
< mod
->num_symtab
; i
++)
2664 if (strcmp(name
, mod
->strtab
+mod
->symtab
[i
].st_name
) == 0 &&
2665 mod
->symtab
[i
].st_info
!= 'U')
2666 return mod
->symtab
[i
].st_value
;
2670 /* Look for this name: can be of form module:name. */
2671 unsigned long module_kallsyms_lookup_name(const char *name
)
2675 unsigned long ret
= 0;
2677 /* Don't lock: we're in enough trouble already. */
2679 if ((colon
= strchr(name
, ':')) != NULL
) {
2681 if ((mod
= find_module(name
)) != NULL
)
2682 ret
= mod_find_symname(mod
, colon
+1);
2685 list_for_each_entry_rcu(mod
, &modules
, list
)
2686 if ((ret
= mod_find_symname(mod
, name
)) != 0)
2693 int module_kallsyms_on_each_symbol(int (*fn
)(void *, const char *,
2694 struct module
*, unsigned long),
2701 list_for_each_entry(mod
, &modules
, list
) {
2702 for (i
= 0; i
< mod
->num_symtab
; i
++) {
2703 ret
= fn(data
, mod
->strtab
+ mod
->symtab
[i
].st_name
,
2704 mod
, mod
->symtab
[i
].st_value
);
2711 #endif /* CONFIG_KALLSYMS */
2713 static char *module_flags(struct module
*mod
, char *buf
)
2718 mod
->state
== MODULE_STATE_GOING
||
2719 mod
->state
== MODULE_STATE_COMING
) {
2721 if (mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
))
2723 if (mod
->taints
& (1 << TAINT_FORCED_MODULE
))
2725 if (mod
->taints
& (1 << TAINT_CRAP
))
2728 * TAINT_FORCED_RMMOD: could be added.
2729 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2733 /* Show a - for module-is-being-unloaded */
2734 if (mod
->state
== MODULE_STATE_GOING
)
2736 /* Show a + for module-is-being-loaded */
2737 if (mod
->state
== MODULE_STATE_COMING
)
2746 #ifdef CONFIG_PROC_FS
2747 /* Called by the /proc file system to return a list of modules. */
2748 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
2750 mutex_lock(&module_mutex
);
2751 return seq_list_start(&modules
, *pos
);
2754 static void *m_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
2756 return seq_list_next(p
, &modules
, pos
);
2759 static void m_stop(struct seq_file
*m
, void *p
)
2761 mutex_unlock(&module_mutex
);
2764 static int m_show(struct seq_file
*m
, void *p
)
2766 struct module
*mod
= list_entry(p
, struct module
, list
);
2769 seq_printf(m
, "%s %u",
2770 mod
->name
, mod
->init_size
+ mod
->core_size
);
2771 print_unload_info(m
, mod
);
2773 /* Informative for users. */
2774 seq_printf(m
, " %s",
2775 mod
->state
== MODULE_STATE_GOING
? "Unloading":
2776 mod
->state
== MODULE_STATE_COMING
? "Loading":
2778 /* Used by oprofile and other similar tools. */
2779 seq_printf(m
, " 0x%p", mod
->module_core
);
2783 seq_printf(m
, " %s", module_flags(mod
, buf
));
2785 seq_printf(m
, "\n");
2789 /* Format: modulename size refcount deps address
2791 Where refcount is a number or -, and deps is a comma-separated list
2794 static const struct seq_operations modules_op
= {
2801 static int modules_open(struct inode
*inode
, struct file
*file
)
2803 return seq_open(file
, &modules_op
);
2806 static const struct file_operations proc_modules_operations
= {
2807 .open
= modules_open
,
2809 .llseek
= seq_lseek
,
2810 .release
= seq_release
,
2813 static int __init
proc_modules_init(void)
2815 proc_create("modules", 0, NULL
, &proc_modules_operations
);
2818 module_init(proc_modules_init
);
2821 /* Given an address, look for it in the module exception tables. */
2822 const struct exception_table_entry
*search_module_extables(unsigned long addr
)
2824 const struct exception_table_entry
*e
= NULL
;
2828 list_for_each_entry_rcu(mod
, &modules
, list
) {
2829 if (mod
->num_exentries
== 0)
2832 e
= search_extable(mod
->extable
,
2833 mod
->extable
+ mod
->num_exentries
- 1,
2840 /* Now, if we found one, we are running inside it now, hence
2841 we cannot unload the module, hence no refcnt needed. */
2846 * is_module_address - is this address inside a module?
2847 * @addr: the address to check.
2849 * See is_module_text_address() if you simply want to see if the address
2850 * is code (not data).
2852 bool is_module_address(unsigned long addr
)
2857 ret
= __module_address(addr
) != NULL
;
2864 * __module_address - get the module which contains an address.
2865 * @addr: the address.
2867 * Must be called with preempt disabled or module mutex held so that
2868 * module doesn't get freed during this.
2870 struct module
*__module_address(unsigned long addr
)
2874 if (addr
< module_addr_min
|| addr
> module_addr_max
)
2877 list_for_each_entry_rcu(mod
, &modules
, list
)
2878 if (within_module_core(addr
, mod
)
2879 || within_module_init(addr
, mod
))
2883 EXPORT_SYMBOL_GPL(__module_address
);
2886 * is_module_text_address - is this address inside module code?
2887 * @addr: the address to check.
2889 * See is_module_address() if you simply want to see if the address is
2890 * anywhere in a module. See kernel_text_address() for testing if an
2891 * address corresponds to kernel or module code.
2893 bool is_module_text_address(unsigned long addr
)
2898 ret
= __module_text_address(addr
) != NULL
;
2905 * __module_text_address - get the module whose code contains an address.
2906 * @addr: the address.
2908 * Must be called with preempt disabled or module mutex held so that
2909 * module doesn't get freed during this.
2911 struct module
*__module_text_address(unsigned long addr
)
2913 struct module
*mod
= __module_address(addr
);
2915 /* Make sure it's within the text section. */
2916 if (!within(addr
, mod
->module_init
, mod
->init_text_size
)
2917 && !within(addr
, mod
->module_core
, mod
->core_text_size
))
2922 EXPORT_SYMBOL_GPL(__module_text_address
);
2924 /* Don't grab lock, we're oopsing. */
2925 void print_modules(void)
2930 printk(KERN_DEFAULT
"Modules linked in:");
2931 /* Most callers should already have preempt disabled, but make sure */
2933 list_for_each_entry_rcu(mod
, &modules
, list
)
2934 printk(" %s%s", mod
->name
, module_flags(mod
, buf
));
2936 if (last_unloaded_module
[0])
2937 printk(" [last unloaded: %s]", last_unloaded_module
);
2941 #ifdef CONFIG_MODVERSIONS
2942 /* Generate the signature for all relevant module structures here.
2943 * If these change, we don't want to try to parse the module. */
2944 void module_layout(struct module
*mod
,
2945 struct modversion_info
*ver
,
2946 struct kernel_param
*kp
,
2947 struct kernel_symbol
*ks
,
2948 struct marker
*marker
,
2949 struct tracepoint
*tp
)
2952 EXPORT_SYMBOL(module_layout
);
2955 #ifdef CONFIG_MARKERS
2956 void module_update_markers(void)
2960 mutex_lock(&module_mutex
);
2961 list_for_each_entry(mod
, &modules
, list
)
2963 marker_update_probe_range(mod
->markers
,
2964 mod
->markers
+ mod
->num_markers
);
2965 mutex_unlock(&module_mutex
);
2969 #ifdef CONFIG_TRACEPOINTS
2970 void module_update_tracepoints(void)
2974 mutex_lock(&module_mutex
);
2975 list_for_each_entry(mod
, &modules
, list
)
2977 tracepoint_update_probe_range(mod
->tracepoints
,
2978 mod
->tracepoints
+ mod
->num_tracepoints
);
2979 mutex_unlock(&module_mutex
);
2983 * Returns 0 if current not found.
2984 * Returns 1 if current found.
2986 int module_get_iter_tracepoints(struct tracepoint_iter
*iter
)
2988 struct module
*iter_mod
;
2991 mutex_lock(&module_mutex
);
2992 list_for_each_entry(iter_mod
, &modules
, list
) {
2993 if (!iter_mod
->taints
) {
2995 * Sorted module list
2997 if (iter_mod
< iter
->module
)
2999 else if (iter_mod
> iter
->module
)
3000 iter
->tracepoint
= NULL
;
3001 found
= tracepoint_get_iter_range(&iter
->tracepoint
,
3002 iter_mod
->tracepoints
,
3003 iter_mod
->tracepoints
3004 + iter_mod
->num_tracepoints
);
3006 iter
->module
= iter_mod
;
3011 mutex_unlock(&module_mutex
);