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/init.h>
22 #include <linux/kallsyms.h>
24 #include <linux/sysfs.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/vmalloc.h>
28 #include <linux/elf.h>
29 #include <linux/proc_fs.h>
30 #include <linux/seq_file.h>
31 #include <linux/syscalls.h>
32 #include <linux/fcntl.h>
33 #include <linux/rcupdate.h>
34 #include <linux/capability.h>
35 #include <linux/cpu.h>
36 #include <linux/moduleparam.h>
37 #include <linux/errno.h>
38 #include <linux/err.h>
39 #include <linux/vermagic.h>
40 #include <linux/notifier.h>
41 #include <linux/sched.h>
42 #include <linux/stop_machine.h>
43 #include <linux/device.h>
44 #include <linux/string.h>
45 #include <linux/mutex.h>
46 #include <linux/rculist.h>
47 #include <asm/uaccess.h>
48 #include <asm/cacheflush.h>
49 #include <linux/license.h>
50 #include <asm/sections.h>
51 #include <linux/tracepoint.h>
52 #include <linux/ftrace.h>
57 #define DEBUGP(fmt , a...)
60 #ifndef ARCH_SHF_SMALL
61 #define ARCH_SHF_SMALL 0
64 /* If this is set, the section belongs in the init part of the module */
65 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
67 /* List of modules, protected by module_mutex or preempt_disable
68 * (delete uses stop_machine/add uses RCU list operations). */
69 static DEFINE_MUTEX(module_mutex
);
70 static LIST_HEAD(modules
);
72 /* Waiting for a module to finish initializing? */
73 static DECLARE_WAIT_QUEUE_HEAD(module_wq
);
75 static BLOCKING_NOTIFIER_HEAD(module_notify_list
);
77 /* Bounds of module allocation, for speeding __module_text_address */
78 static unsigned long module_addr_min
= -1UL, module_addr_max
= 0;
80 int register_module_notifier(struct notifier_block
* nb
)
82 return blocking_notifier_chain_register(&module_notify_list
, nb
);
84 EXPORT_SYMBOL(register_module_notifier
);
86 int unregister_module_notifier(struct notifier_block
* nb
)
88 return blocking_notifier_chain_unregister(&module_notify_list
, nb
);
90 EXPORT_SYMBOL(unregister_module_notifier
);
92 /* We require a truly strong try_module_get(): 0 means failure due to
93 ongoing or failed initialization etc. */
94 static inline int strong_try_module_get(struct module
*mod
)
96 if (mod
&& mod
->state
== MODULE_STATE_COMING
)
98 if (try_module_get(mod
))
104 static inline void add_taint_module(struct module
*mod
, unsigned flag
)
107 mod
->taints
|= (1U << flag
);
111 * A thread that wants to hold a reference to a module only while it
112 * is running can call this to safely exit. nfsd and lockd use this.
114 void __module_put_and_exit(struct module
*mod
, long code
)
119 EXPORT_SYMBOL(__module_put_and_exit
);
121 /* Find a module section: 0 means not found. */
122 static unsigned int find_sec(Elf_Ehdr
*hdr
,
124 const char *secstrings
,
129 for (i
= 1; i
< hdr
->e_shnum
; i
++)
130 /* Alloc bit cleared means "ignore it." */
131 if ((sechdrs
[i
].sh_flags
& SHF_ALLOC
)
132 && strcmp(secstrings
+sechdrs
[i
].sh_name
, name
) == 0)
137 /* Find a module section, or NULL. */
138 static void *section_addr(Elf_Ehdr
*hdr
, Elf_Shdr
*shdrs
,
139 const char *secstrings
, const char *name
)
141 /* Section 0 has sh_addr 0. */
142 return (void *)shdrs
[find_sec(hdr
, shdrs
, secstrings
, name
)].sh_addr
;
145 /* Find a module section, or NULL. Fill in number of "objects" in section. */
146 static void *section_objs(Elf_Ehdr
*hdr
,
148 const char *secstrings
,
153 unsigned int sec
= find_sec(hdr
, sechdrs
, secstrings
, name
);
155 /* Section 0 has sh_addr 0 and sh_size 0. */
156 *num
= sechdrs
[sec
].sh_size
/ object_size
;
157 return (void *)sechdrs
[sec
].sh_addr
;
160 /* Provided by the linker */
161 extern const struct kernel_symbol __start___ksymtab
[];
162 extern const struct kernel_symbol __stop___ksymtab
[];
163 extern const struct kernel_symbol __start___ksymtab_gpl
[];
164 extern const struct kernel_symbol __stop___ksymtab_gpl
[];
165 extern const struct kernel_symbol __start___ksymtab_gpl_future
[];
166 extern const struct kernel_symbol __stop___ksymtab_gpl_future
[];
167 extern const struct kernel_symbol __start___ksymtab_gpl_future
[];
168 extern const struct kernel_symbol __stop___ksymtab_gpl_future
[];
169 extern const unsigned long __start___kcrctab
[];
170 extern const unsigned long __start___kcrctab_gpl
[];
171 extern const unsigned long __start___kcrctab_gpl_future
[];
172 #ifdef CONFIG_UNUSED_SYMBOLS
173 extern const struct kernel_symbol __start___ksymtab_unused
[];
174 extern const struct kernel_symbol __stop___ksymtab_unused
[];
175 extern const struct kernel_symbol __start___ksymtab_unused_gpl
[];
176 extern const struct kernel_symbol __stop___ksymtab_unused_gpl
[];
177 extern const unsigned long __start___kcrctab_unused
[];
178 extern const unsigned long __start___kcrctab_unused_gpl
[];
181 #ifndef CONFIG_MODVERSIONS
182 #define symversion(base, idx) NULL
184 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
188 const struct kernel_symbol
*start
, *stop
;
189 const unsigned long *crcs
;
198 static bool each_symbol_in_section(const struct symsearch
*arr
,
199 unsigned int arrsize
,
200 struct module
*owner
,
201 bool (*fn
)(const struct symsearch
*syms
,
202 struct module
*owner
,
203 unsigned int symnum
, void *data
),
208 for (j
= 0; j
< arrsize
; j
++) {
209 for (i
= 0; i
< arr
[j
].stop
- arr
[j
].start
; i
++)
210 if (fn(&arr
[j
], owner
, i
, data
))
217 /* Returns true as soon as fn returns true, otherwise false. */
218 static bool each_symbol(bool (*fn
)(const struct symsearch
*arr
,
219 struct module
*owner
,
220 unsigned int symnum
, 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
))
275 struct find_symbol_arg
{
282 struct module
*owner
;
283 const unsigned long *crc
;
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
->value
= syms
->start
[symnum
].value
;
329 /* Find a symbol, return value, (optional) crc and (optional) module
331 static unsigned long 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
);
355 /* Search for module by name: must hold module_mutex. */
356 static struct module
*find_module(const char *name
)
360 list_for_each_entry(mod
, &modules
, list
) {
361 if (strcmp(mod
->name
, name
) == 0)
368 /* Number of blocks used and allocated. */
369 static unsigned int pcpu_num_used
, pcpu_num_allocated
;
370 /* Size of each block. -ve means used. */
371 static int *pcpu_size
;
373 static int split_block(unsigned int i
, unsigned short size
)
375 /* Reallocation required? */
376 if (pcpu_num_used
+ 1 > pcpu_num_allocated
) {
379 new = krealloc(pcpu_size
, sizeof(new[0])*pcpu_num_allocated
*2,
384 pcpu_num_allocated
*= 2;
388 /* Insert a new subblock */
389 memmove(&pcpu_size
[i
+1], &pcpu_size
[i
],
390 sizeof(pcpu_size
[0]) * (pcpu_num_used
- i
));
393 pcpu_size
[i
+1] -= size
;
398 static inline unsigned int block_size(int val
)
405 static void *percpu_modalloc(unsigned long size
, unsigned long align
,
412 if (align
> PAGE_SIZE
) {
413 printk(KERN_WARNING
"%s: per-cpu alignment %li > %li\n",
414 name
, align
, PAGE_SIZE
);
418 ptr
= __per_cpu_start
;
419 for (i
= 0; i
< pcpu_num_used
; ptr
+= block_size(pcpu_size
[i
]), i
++) {
420 /* Extra for alignment requirement. */
421 extra
= ALIGN((unsigned long)ptr
, align
) - (unsigned long)ptr
;
422 BUG_ON(i
== 0 && extra
!= 0);
424 if (pcpu_size
[i
] < 0 || pcpu_size
[i
] < extra
+ size
)
427 /* Transfer extra to previous block. */
428 if (pcpu_size
[i
-1] < 0)
429 pcpu_size
[i
-1] -= extra
;
431 pcpu_size
[i
-1] += extra
;
432 pcpu_size
[i
] -= extra
;
435 /* Split block if warranted */
436 if (pcpu_size
[i
] - size
> sizeof(unsigned long))
437 if (!split_block(i
, size
))
441 pcpu_size
[i
] = -pcpu_size
[i
];
445 printk(KERN_WARNING
"Could not allocate %lu bytes percpu data\n",
450 static void percpu_modfree(void *freeme
)
453 void *ptr
= __per_cpu_start
+ block_size(pcpu_size
[0]);
455 /* First entry is core kernel percpu data. */
456 for (i
= 1; i
< pcpu_num_used
; ptr
+= block_size(pcpu_size
[i
]), i
++) {
458 pcpu_size
[i
] = -pcpu_size
[i
];
465 /* Merge with previous? */
466 if (pcpu_size
[i
-1] >= 0) {
467 pcpu_size
[i
-1] += pcpu_size
[i
];
469 memmove(&pcpu_size
[i
], &pcpu_size
[i
+1],
470 (pcpu_num_used
- i
) * sizeof(pcpu_size
[0]));
473 /* Merge with next? */
474 if (i
+1 < pcpu_num_used
&& pcpu_size
[i
+1] >= 0) {
475 pcpu_size
[i
] += pcpu_size
[i
+1];
477 memmove(&pcpu_size
[i
+1], &pcpu_size
[i
+2],
478 (pcpu_num_used
- (i
+1)) * sizeof(pcpu_size
[0]));
482 static unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
484 const char *secstrings
)
486 return find_sec(hdr
, sechdrs
, secstrings
, ".data.percpu");
489 static void percpu_modcopy(void *pcpudest
, const void *from
, unsigned long size
)
493 for_each_possible_cpu(cpu
)
494 memcpy(pcpudest
+ per_cpu_offset(cpu
), from
, size
);
497 static int percpu_modinit(void)
500 pcpu_num_allocated
= 2;
501 pcpu_size
= kmalloc(sizeof(pcpu_size
[0]) * pcpu_num_allocated
,
503 /* Static in-kernel percpu data (used). */
504 pcpu_size
[0] = -(__per_cpu_end
-__per_cpu_start
);
506 pcpu_size
[1] = PERCPU_ENOUGH_ROOM
+ pcpu_size
[0];
507 if (pcpu_size
[1] < 0) {
508 printk(KERN_ERR
"No per-cpu room for modules.\n");
514 __initcall(percpu_modinit
);
515 #else /* ... !CONFIG_SMP */
516 static inline void *percpu_modalloc(unsigned long size
, unsigned long align
,
521 static inline void percpu_modfree(void *pcpuptr
)
525 static inline unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
527 const char *secstrings
)
531 static inline void percpu_modcopy(void *pcpudst
, const void *src
,
534 /* pcpusec should be 0, and size of that section should be 0. */
537 #endif /* CONFIG_SMP */
539 #define MODINFO_ATTR(field) \
540 static void setup_modinfo_##field(struct module *mod, const char *s) \
542 mod->field = kstrdup(s, GFP_KERNEL); \
544 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
545 struct module *mod, char *buffer) \
547 return sprintf(buffer, "%s\n", mod->field); \
549 static int modinfo_##field##_exists(struct module *mod) \
551 return mod->field != NULL; \
553 static void free_modinfo_##field(struct module *mod) \
558 static struct module_attribute modinfo_##field = { \
559 .attr = { .name = __stringify(field), .mode = 0444 }, \
560 .show = show_modinfo_##field, \
561 .setup = setup_modinfo_##field, \
562 .test = modinfo_##field##_exists, \
563 .free = free_modinfo_##field, \
566 MODINFO_ATTR(version
);
567 MODINFO_ATTR(srcversion
);
569 static char last_unloaded_module
[MODULE_NAME_LEN
+1];
571 #ifdef CONFIG_MODULE_UNLOAD
572 /* Init the unload section of the module. */
573 static void module_unload_init(struct module
*mod
)
577 INIT_LIST_HEAD(&mod
->modules_which_use_me
);
578 for (i
= 0; i
< NR_CPUS
; i
++)
579 local_set(&mod
->ref
[i
].count
, 0);
580 /* Hold reference count during initialization. */
581 local_set(&mod
->ref
[raw_smp_processor_id()].count
, 1);
582 /* Backwards compatibility macros put refcount during init. */
583 mod
->waiter
= current
;
586 /* modules using other modules */
589 struct list_head list
;
590 struct module
*module_which_uses
;
593 /* Does a already use b? */
594 static int already_uses(struct module
*a
, struct module
*b
)
596 struct module_use
*use
;
598 list_for_each_entry(use
, &b
->modules_which_use_me
, list
) {
599 if (use
->module_which_uses
== a
) {
600 DEBUGP("%s uses %s!\n", a
->name
, b
->name
);
604 DEBUGP("%s does not use %s!\n", a
->name
, b
->name
);
608 /* Module a uses b */
609 static int use_module(struct module
*a
, struct module
*b
)
611 struct module_use
*use
;
614 if (b
== NULL
|| already_uses(a
, b
)) return 1;
616 /* If we're interrupted or time out, we fail. */
617 if (wait_event_interruptible_timeout(
618 module_wq
, (err
= strong_try_module_get(b
)) != -EBUSY
,
620 printk("%s: gave up waiting for init of module %s.\n",
625 /* If strong_try_module_get() returned a different error, we fail. */
629 DEBUGP("Allocating new usage for %s.\n", a
->name
);
630 use
= kmalloc(sizeof(*use
), GFP_ATOMIC
);
632 printk("%s: out of memory loading\n", a
->name
);
637 use
->module_which_uses
= a
;
638 list_add(&use
->list
, &b
->modules_which_use_me
);
639 no_warn
= sysfs_create_link(b
->holders_dir
, &a
->mkobj
.kobj
, a
->name
);
643 /* Clear the unload stuff of the module. */
644 static void module_unload_free(struct module
*mod
)
648 list_for_each_entry(i
, &modules
, list
) {
649 struct module_use
*use
;
651 list_for_each_entry(use
, &i
->modules_which_use_me
, list
) {
652 if (use
->module_which_uses
== mod
) {
653 DEBUGP("%s unusing %s\n", mod
->name
, i
->name
);
655 list_del(&use
->list
);
657 sysfs_remove_link(i
->holders_dir
, mod
->name
);
658 /* There can be at most one match. */
665 #ifdef CONFIG_MODULE_FORCE_UNLOAD
666 static inline int try_force_unload(unsigned int flags
)
668 int ret
= (flags
& O_TRUNC
);
670 add_taint(TAINT_FORCED_RMMOD
);
674 static inline int try_force_unload(unsigned int flags
)
678 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
687 /* Whole machine is stopped with interrupts off when this runs. */
688 static int __try_stop_module(void *_sref
)
690 struct stopref
*sref
= _sref
;
692 /* If it's not unused, quit unless we're forcing. */
693 if (module_refcount(sref
->mod
) != 0) {
694 if (!(*sref
->forced
= try_force_unload(sref
->flags
)))
698 /* Mark it as dying. */
699 sref
->mod
->state
= MODULE_STATE_GOING
;
703 static int try_stop_module(struct module
*mod
, int flags
, int *forced
)
705 if (flags
& O_NONBLOCK
) {
706 struct stopref sref
= { mod
, flags
, forced
};
708 return stop_machine(__try_stop_module
, &sref
, NULL
);
710 /* We don't need to stop the machine for this. */
711 mod
->state
= MODULE_STATE_GOING
;
717 unsigned int module_refcount(struct module
*mod
)
719 unsigned int i
, total
= 0;
721 for (i
= 0; i
< NR_CPUS
; i
++)
722 total
+= local_read(&mod
->ref
[i
].count
);
725 EXPORT_SYMBOL(module_refcount
);
727 /* This exists whether we can unload or not */
728 static void free_module(struct module
*mod
);
730 static void wait_for_zero_refcount(struct module
*mod
)
732 /* Since we might sleep for some time, release the mutex first */
733 mutex_unlock(&module_mutex
);
735 DEBUGP("Looking at refcount...\n");
736 set_current_state(TASK_UNINTERRUPTIBLE
);
737 if (module_refcount(mod
) == 0)
741 current
->state
= TASK_RUNNING
;
742 mutex_lock(&module_mutex
);
746 sys_delete_module(const char __user
*name_user
, unsigned int flags
)
749 char name
[MODULE_NAME_LEN
];
752 if (!capable(CAP_SYS_MODULE
))
755 if (strncpy_from_user(name
, name_user
, MODULE_NAME_LEN
-1) < 0)
757 name
[MODULE_NAME_LEN
-1] = '\0';
759 /* Create stop_machine threads since free_module relies on
760 * a non-failing stop_machine call. */
761 ret
= stop_machine_create();
765 if (mutex_lock_interruptible(&module_mutex
) != 0) {
770 mod
= find_module(name
);
776 if (!list_empty(&mod
->modules_which_use_me
)) {
777 /* Other modules depend on us: get rid of them first. */
782 /* Doing init or already dying? */
783 if (mod
->state
!= MODULE_STATE_LIVE
) {
784 /* FIXME: if (force), slam module count and wake up
786 DEBUGP("%s already dying\n", mod
->name
);
791 /* If it has an init func, it must have an exit func to unload */
792 if (mod
->init
&& !mod
->exit
) {
793 forced
= try_force_unload(flags
);
795 /* This module can't be removed */
801 /* Set this up before setting mod->state */
802 mod
->waiter
= current
;
804 /* Stop the machine so refcounts can't move and disable module. */
805 ret
= try_stop_module(mod
, flags
, &forced
);
809 /* Never wait if forced. */
810 if (!forced
&& module_refcount(mod
) != 0)
811 wait_for_zero_refcount(mod
);
813 mutex_unlock(&module_mutex
);
814 /* Final destruction now noone is using it. */
815 if (mod
->exit
!= NULL
)
817 blocking_notifier_call_chain(&module_notify_list
,
818 MODULE_STATE_GOING
, mod
);
819 mutex_lock(&module_mutex
);
820 /* Store the name of the last unloaded module for diagnostic purposes */
821 strlcpy(last_unloaded_module
, mod
->name
, sizeof(last_unloaded_module
));
822 unregister_dynamic_debug_module(mod
->name
);
826 mutex_unlock(&module_mutex
);
828 stop_machine_destroy();
832 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
834 struct module_use
*use
;
835 int printed_something
= 0;
837 seq_printf(m
, " %u ", module_refcount(mod
));
839 /* Always include a trailing , so userspace can differentiate
840 between this and the old multi-field proc format. */
841 list_for_each_entry(use
, &mod
->modules_which_use_me
, list
) {
842 printed_something
= 1;
843 seq_printf(m
, "%s,", use
->module_which_uses
->name
);
846 if (mod
->init
!= NULL
&& mod
->exit
== NULL
) {
847 printed_something
= 1;
848 seq_printf(m
, "[permanent],");
851 if (!printed_something
)
855 void __symbol_put(const char *symbol
)
857 struct module
*owner
;
860 if (IS_ERR_VALUE(find_symbol(symbol
, &owner
, NULL
, true, false)))
865 EXPORT_SYMBOL(__symbol_put
);
867 void symbol_put_addr(void *addr
)
869 struct module
*modaddr
;
871 if (core_kernel_text((unsigned long)addr
))
874 if (!(modaddr
= module_text_address((unsigned long)addr
)))
878 EXPORT_SYMBOL_GPL(symbol_put_addr
);
880 static ssize_t
show_refcnt(struct module_attribute
*mattr
,
881 struct module
*mod
, char *buffer
)
883 return sprintf(buffer
, "%u\n", module_refcount(mod
));
886 static struct module_attribute refcnt
= {
887 .attr
= { .name
= "refcnt", .mode
= 0444 },
891 void module_put(struct module
*module
)
894 unsigned int cpu
= get_cpu();
895 local_dec(&module
->ref
[cpu
].count
);
896 /* Maybe they're waiting for us to drop reference? */
897 if (unlikely(!module_is_live(module
)))
898 wake_up_process(module
->waiter
);
902 EXPORT_SYMBOL(module_put
);
904 #else /* !CONFIG_MODULE_UNLOAD */
905 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
907 /* We don't know the usage count, or what modules are using. */
908 seq_printf(m
, " - -");
911 static inline void module_unload_free(struct module
*mod
)
915 static inline int use_module(struct module
*a
, struct module
*b
)
917 return strong_try_module_get(b
) == 0;
920 static inline void module_unload_init(struct module
*mod
)
923 #endif /* CONFIG_MODULE_UNLOAD */
925 static ssize_t
show_initstate(struct module_attribute
*mattr
,
926 struct module
*mod
, char *buffer
)
928 const char *state
= "unknown";
930 switch (mod
->state
) {
931 case MODULE_STATE_LIVE
:
934 case MODULE_STATE_COMING
:
937 case MODULE_STATE_GOING
:
941 return sprintf(buffer
, "%s\n", state
);
944 static struct module_attribute initstate
= {
945 .attr
= { .name
= "initstate", .mode
= 0444 },
946 .show
= show_initstate
,
949 static struct module_attribute
*modinfo_attrs
[] = {
953 #ifdef CONFIG_MODULE_UNLOAD
959 static const char vermagic
[] = VERMAGIC_STRING
;
961 static int try_to_force_load(struct module
*mod
, const char *symname
)
963 #ifdef CONFIG_MODULE_FORCE_LOAD
964 if (!test_taint(TAINT_FORCED_MODULE
))
965 printk("%s: no version for \"%s\" found: kernel tainted.\n",
967 add_taint_module(mod
, TAINT_FORCED_MODULE
);
974 #ifdef CONFIG_MODVERSIONS
975 static int check_version(Elf_Shdr
*sechdrs
,
976 unsigned int versindex
,
979 const unsigned long *crc
)
981 unsigned int i
, num_versions
;
982 struct modversion_info
*versions
;
984 /* Exporting module didn't supply crcs? OK, we're already tainted. */
988 /* No versions at all? modprobe --force does this. */
990 return try_to_force_load(mod
, symname
) == 0;
992 versions
= (void *) sechdrs
[versindex
].sh_addr
;
993 num_versions
= sechdrs
[versindex
].sh_size
994 / sizeof(struct modversion_info
);
996 for (i
= 0; i
< num_versions
; i
++) {
997 if (strcmp(versions
[i
].name
, symname
) != 0)
1000 if (versions
[i
].crc
== *crc
)
1002 DEBUGP("Found checksum %lX vs module %lX\n",
1003 *crc
, versions
[i
].crc
);
1007 printk(KERN_WARNING
"%s: no symbol version for %s\n",
1008 mod
->name
, symname
);
1012 printk("%s: disagrees about version of symbol %s\n",
1013 mod
->name
, symname
);
1017 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1018 unsigned int versindex
,
1021 const unsigned long *crc
;
1023 if (IS_ERR_VALUE(find_symbol("struct_module", NULL
, &crc
, true, false)))
1025 return check_version(sechdrs
, versindex
, "struct_module", mod
, crc
);
1028 /* First part is kernel version, which we ignore if module has crcs. */
1029 static inline int same_magic(const char *amagic
, const char *bmagic
,
1033 amagic
+= strcspn(amagic
, " ");
1034 bmagic
+= strcspn(bmagic
, " ");
1036 return strcmp(amagic
, bmagic
) == 0;
1039 static inline int check_version(Elf_Shdr
*sechdrs
,
1040 unsigned int versindex
,
1041 const char *symname
,
1043 const unsigned long *crc
)
1048 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1049 unsigned int versindex
,
1055 static inline int same_magic(const char *amagic
, const char *bmagic
,
1058 return strcmp(amagic
, bmagic
) == 0;
1060 #endif /* CONFIG_MODVERSIONS */
1062 /* Resolve a symbol for this module. I.e. if we find one, record usage.
1063 Must be holding module_mutex. */
1064 static unsigned long resolve_symbol(Elf_Shdr
*sechdrs
,
1065 unsigned int versindex
,
1069 struct module
*owner
;
1071 const unsigned long *crc
;
1073 ret
= find_symbol(name
, &owner
, &crc
,
1074 !(mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
)), true);
1075 if (!IS_ERR_VALUE(ret
)) {
1076 /* use_module can fail due to OOM,
1077 or module initialization or unloading */
1078 if (!check_version(sechdrs
, versindex
, name
, mod
, crc
) ||
1079 !use_module(mod
, owner
))
1086 * /sys/module/foo/sections stuff
1087 * J. Corbet <corbet@lwn.net>
1089 #if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
1090 struct module_sect_attr
1092 struct module_attribute mattr
;
1094 unsigned long address
;
1097 struct module_sect_attrs
1099 struct attribute_group grp
;
1100 unsigned int nsections
;
1101 struct module_sect_attr attrs
[0];
1104 static ssize_t
module_sect_show(struct module_attribute
*mattr
,
1105 struct module
*mod
, char *buf
)
1107 struct module_sect_attr
*sattr
=
1108 container_of(mattr
, struct module_sect_attr
, mattr
);
1109 return sprintf(buf
, "0x%lx\n", sattr
->address
);
1112 static void free_sect_attrs(struct module_sect_attrs
*sect_attrs
)
1114 unsigned int section
;
1116 for (section
= 0; section
< sect_attrs
->nsections
; section
++)
1117 kfree(sect_attrs
->attrs
[section
].name
);
1121 static void add_sect_attrs(struct module
*mod
, unsigned int nsect
,
1122 char *secstrings
, Elf_Shdr
*sechdrs
)
1124 unsigned int nloaded
= 0, i
, size
[2];
1125 struct module_sect_attrs
*sect_attrs
;
1126 struct module_sect_attr
*sattr
;
1127 struct attribute
**gattr
;
1129 /* Count loaded sections and allocate structures */
1130 for (i
= 0; i
< nsect
; i
++)
1131 if (sechdrs
[i
].sh_flags
& SHF_ALLOC
)
1133 size
[0] = ALIGN(sizeof(*sect_attrs
)
1134 + nloaded
* sizeof(sect_attrs
->attrs
[0]),
1135 sizeof(sect_attrs
->grp
.attrs
[0]));
1136 size
[1] = (nloaded
+ 1) * sizeof(sect_attrs
->grp
.attrs
[0]);
1137 sect_attrs
= kzalloc(size
[0] + size
[1], GFP_KERNEL
);
1138 if (sect_attrs
== NULL
)
1141 /* Setup section attributes. */
1142 sect_attrs
->grp
.name
= "sections";
1143 sect_attrs
->grp
.attrs
= (void *)sect_attrs
+ size
[0];
1145 sect_attrs
->nsections
= 0;
1146 sattr
= §_attrs
->attrs
[0];
1147 gattr
= §_attrs
->grp
.attrs
[0];
1148 for (i
= 0; i
< nsect
; i
++) {
1149 if (! (sechdrs
[i
].sh_flags
& SHF_ALLOC
))
1151 sattr
->address
= sechdrs
[i
].sh_addr
;
1152 sattr
->name
= kstrdup(secstrings
+ sechdrs
[i
].sh_name
,
1154 if (sattr
->name
== NULL
)
1156 sect_attrs
->nsections
++;
1157 sattr
->mattr
.show
= module_sect_show
;
1158 sattr
->mattr
.store
= NULL
;
1159 sattr
->mattr
.attr
.name
= sattr
->name
;
1160 sattr
->mattr
.attr
.mode
= S_IRUGO
;
1161 *(gattr
++) = &(sattr
++)->mattr
.attr
;
1165 if (sysfs_create_group(&mod
->mkobj
.kobj
, §_attrs
->grp
))
1168 mod
->sect_attrs
= sect_attrs
;
1171 free_sect_attrs(sect_attrs
);
1174 static void remove_sect_attrs(struct module
*mod
)
1176 if (mod
->sect_attrs
) {
1177 sysfs_remove_group(&mod
->mkobj
.kobj
,
1178 &mod
->sect_attrs
->grp
);
1179 /* We are positive that no one is using any sect attrs
1180 * at this point. Deallocate immediately. */
1181 free_sect_attrs(mod
->sect_attrs
);
1182 mod
->sect_attrs
= NULL
;
1187 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1190 struct module_notes_attrs
{
1191 struct kobject
*dir
;
1193 struct bin_attribute attrs
[0];
1196 static ssize_t
module_notes_read(struct kobject
*kobj
,
1197 struct bin_attribute
*bin_attr
,
1198 char *buf
, loff_t pos
, size_t count
)
1201 * The caller checked the pos and count against our size.
1203 memcpy(buf
, bin_attr
->private + pos
, count
);
1207 static void free_notes_attrs(struct module_notes_attrs
*notes_attrs
,
1210 if (notes_attrs
->dir
) {
1212 sysfs_remove_bin_file(notes_attrs
->dir
,
1213 ¬es_attrs
->attrs
[i
]);
1214 kobject_put(notes_attrs
->dir
);
1219 static void add_notes_attrs(struct module
*mod
, unsigned int nsect
,
1220 char *secstrings
, Elf_Shdr
*sechdrs
)
1222 unsigned int notes
, loaded
, i
;
1223 struct module_notes_attrs
*notes_attrs
;
1224 struct bin_attribute
*nattr
;
1226 /* Count notes sections and allocate structures. */
1228 for (i
= 0; i
< nsect
; i
++)
1229 if ((sechdrs
[i
].sh_flags
& SHF_ALLOC
) &&
1230 (sechdrs
[i
].sh_type
== SHT_NOTE
))
1236 notes_attrs
= kzalloc(sizeof(*notes_attrs
)
1237 + notes
* sizeof(notes_attrs
->attrs
[0]),
1239 if (notes_attrs
== NULL
)
1242 notes_attrs
->notes
= notes
;
1243 nattr
= ¬es_attrs
->attrs
[0];
1244 for (loaded
= i
= 0; i
< nsect
; ++i
) {
1245 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
1247 if (sechdrs
[i
].sh_type
== SHT_NOTE
) {
1248 nattr
->attr
.name
= mod
->sect_attrs
->attrs
[loaded
].name
;
1249 nattr
->attr
.mode
= S_IRUGO
;
1250 nattr
->size
= sechdrs
[i
].sh_size
;
1251 nattr
->private = (void *) sechdrs
[i
].sh_addr
;
1252 nattr
->read
= module_notes_read
;
1258 notes_attrs
->dir
= kobject_create_and_add("notes", &mod
->mkobj
.kobj
);
1259 if (!notes_attrs
->dir
)
1262 for (i
= 0; i
< notes
; ++i
)
1263 if (sysfs_create_bin_file(notes_attrs
->dir
,
1264 ¬es_attrs
->attrs
[i
]))
1267 mod
->notes_attrs
= notes_attrs
;
1271 free_notes_attrs(notes_attrs
, i
);
1274 static void remove_notes_attrs(struct module
*mod
)
1276 if (mod
->notes_attrs
)
1277 free_notes_attrs(mod
->notes_attrs
, mod
->notes_attrs
->notes
);
1282 static inline void add_sect_attrs(struct module
*mod
, unsigned int nsect
,
1283 char *sectstrings
, Elf_Shdr
*sechdrs
)
1287 static inline void remove_sect_attrs(struct module
*mod
)
1291 static inline void add_notes_attrs(struct module
*mod
, unsigned int nsect
,
1292 char *sectstrings
, Elf_Shdr
*sechdrs
)
1296 static inline void remove_notes_attrs(struct module
*mod
)
1302 int module_add_modinfo_attrs(struct module
*mod
)
1304 struct module_attribute
*attr
;
1305 struct module_attribute
*temp_attr
;
1309 mod
->modinfo_attrs
= kzalloc((sizeof(struct module_attribute
) *
1310 (ARRAY_SIZE(modinfo_attrs
) + 1)),
1312 if (!mod
->modinfo_attrs
)
1315 temp_attr
= mod
->modinfo_attrs
;
1316 for (i
= 0; (attr
= modinfo_attrs
[i
]) && !error
; i
++) {
1318 (attr
->test
&& attr
->test(mod
))) {
1319 memcpy(temp_attr
, attr
, sizeof(*temp_attr
));
1320 error
= sysfs_create_file(&mod
->mkobj
.kobj
,&temp_attr
->attr
);
1327 void module_remove_modinfo_attrs(struct module
*mod
)
1329 struct module_attribute
*attr
;
1332 for (i
= 0; (attr
= &mod
->modinfo_attrs
[i
]); i
++) {
1333 /* pick a field to test for end of list */
1334 if (!attr
->attr
.name
)
1336 sysfs_remove_file(&mod
->mkobj
.kobj
,&attr
->attr
);
1340 kfree(mod
->modinfo_attrs
);
1343 int mod_sysfs_init(struct module
*mod
)
1346 struct kobject
*kobj
;
1348 if (!module_sysfs_initialized
) {
1349 printk(KERN_ERR
"%s: module sysfs not initialized\n",
1355 kobj
= kset_find_obj(module_kset
, mod
->name
);
1357 printk(KERN_ERR
"%s: module is already loaded\n", mod
->name
);
1363 mod
->mkobj
.mod
= mod
;
1365 memset(&mod
->mkobj
.kobj
, 0, sizeof(mod
->mkobj
.kobj
));
1366 mod
->mkobj
.kobj
.kset
= module_kset
;
1367 err
= kobject_init_and_add(&mod
->mkobj
.kobj
, &module_ktype
, NULL
,
1370 kobject_put(&mod
->mkobj
.kobj
);
1372 /* delay uevent until full sysfs population */
1377 int mod_sysfs_setup(struct module
*mod
,
1378 struct kernel_param
*kparam
,
1379 unsigned int num_params
)
1383 mod
->holders_dir
= kobject_create_and_add("holders", &mod
->mkobj
.kobj
);
1384 if (!mod
->holders_dir
) {
1389 err
= module_param_sysfs_setup(mod
, kparam
, num_params
);
1391 goto out_unreg_holders
;
1393 err
= module_add_modinfo_attrs(mod
);
1395 goto out_unreg_param
;
1397 kobject_uevent(&mod
->mkobj
.kobj
, KOBJ_ADD
);
1401 module_param_sysfs_remove(mod
);
1403 kobject_put(mod
->holders_dir
);
1405 kobject_put(&mod
->mkobj
.kobj
);
1409 static void mod_sysfs_fini(struct module
*mod
)
1411 kobject_put(&mod
->mkobj
.kobj
);
1414 #else /* CONFIG_SYSFS */
1416 static void mod_sysfs_fini(struct module
*mod
)
1420 #endif /* CONFIG_SYSFS */
1422 static void mod_kobject_remove(struct module
*mod
)
1424 module_remove_modinfo_attrs(mod
);
1425 module_param_sysfs_remove(mod
);
1426 kobject_put(mod
->mkobj
.drivers_dir
);
1427 kobject_put(mod
->holders_dir
);
1428 mod_sysfs_fini(mod
);
1432 * unlink the module with the whole machine is stopped with interrupts off
1433 * - this defends against kallsyms not taking locks
1435 static int __unlink_module(void *_mod
)
1437 struct module
*mod
= _mod
;
1438 list_del(&mod
->list
);
1442 /* Free a module, remove from lists, etc (must hold module_mutex). */
1443 static void free_module(struct module
*mod
)
1445 /* Delete from various lists */
1446 stop_machine(__unlink_module
, mod
, NULL
);
1447 remove_notes_attrs(mod
);
1448 remove_sect_attrs(mod
);
1449 mod_kobject_remove(mod
);
1451 /* Arch-specific cleanup. */
1452 module_arch_cleanup(mod
);
1454 /* Module unload stuff */
1455 module_unload_free(mod
);
1457 /* release any pointers to mcount in this module */
1458 ftrace_release(mod
->module_core
, mod
->core_size
);
1460 /* This may be NULL, but that's OK */
1461 module_free(mod
, mod
->module_init
);
1464 percpu_modfree(mod
->percpu
);
1466 /* Free lock-classes: */
1467 lockdep_free_key_range(mod
->module_core
, mod
->core_size
);
1469 /* Finally, free the core (containing the module structure) */
1470 module_free(mod
, mod
->module_core
);
1473 void *__symbol_get(const char *symbol
)
1475 struct module
*owner
;
1476 unsigned long value
;
1479 value
= find_symbol(symbol
, &owner
, NULL
, true, true);
1480 if (IS_ERR_VALUE(value
))
1482 else if (strong_try_module_get(owner
))
1486 return (void *)value
;
1488 EXPORT_SYMBOL_GPL(__symbol_get
);
1491 * Ensure that an exported symbol [global namespace] does not already exist
1492 * in the kernel or in some other module's exported symbol table.
1494 static int verify_export_symbols(struct module
*mod
)
1497 struct module
*owner
;
1498 const struct kernel_symbol
*s
;
1500 const struct kernel_symbol
*sym
;
1503 { mod
->syms
, mod
->num_syms
},
1504 { mod
->gpl_syms
, mod
->num_gpl_syms
},
1505 { mod
->gpl_future_syms
, mod
->num_gpl_future_syms
},
1506 #ifdef CONFIG_UNUSED_SYMBOLS
1507 { mod
->unused_syms
, mod
->num_unused_syms
},
1508 { mod
->unused_gpl_syms
, mod
->num_unused_gpl_syms
},
1512 for (i
= 0; i
< ARRAY_SIZE(arr
); i
++) {
1513 for (s
= arr
[i
].sym
; s
< arr
[i
].sym
+ arr
[i
].num
; s
++) {
1514 if (!IS_ERR_VALUE(find_symbol(s
->name
, &owner
,
1515 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
);
1540 for (i
= 1; i
< n
; i
++) {
1541 switch (sym
[i
].st_shndx
) {
1543 /* We compiled with -fno-common. These are not
1544 supposed to happen. */
1545 DEBUGP("Common symbol: %s\n", strtab
+ sym
[i
].st_name
);
1546 printk("%s: please compile with -fno-common\n",
1552 /* Don't need to do anything */
1553 DEBUGP("Absolute symbol: 0x%08lx\n",
1554 (long)sym
[i
].st_value
);
1559 = resolve_symbol(sechdrs
, versindex
,
1560 strtab
+ sym
[i
].st_name
, mod
);
1562 /* Ok if resolved. */
1563 if (!IS_ERR_VALUE(sym
[i
].st_value
))
1566 if (ELF_ST_BIND(sym
[i
].st_info
) == STB_WEAK
)
1569 printk(KERN_WARNING
"%s: Unknown symbol %s\n",
1570 mod
->name
, strtab
+ sym
[i
].st_name
);
1575 /* Divert to percpu allocation if a percpu var. */
1576 if (sym
[i
].st_shndx
== pcpuindex
)
1577 secbase
= (unsigned long)mod
->percpu
;
1579 secbase
= sechdrs
[sym
[i
].st_shndx
].sh_addr
;
1580 sym
[i
].st_value
+= secbase
;
1588 /* Additional bytes needed by arch in front of individual sections */
1589 unsigned int __weak
arch_mod_section_prepend(struct module
*mod
,
1590 unsigned int section
)
1592 /* default implementation just returns zero */
1596 /* Update size with this section: return offset. */
1597 static long get_offset(struct module
*mod
, unsigned int *size
,
1598 Elf_Shdr
*sechdr
, unsigned int section
)
1602 *size
+= arch_mod_section_prepend(mod
, section
);
1603 ret
= ALIGN(*size
, sechdr
->sh_addralign
?: 1);
1604 *size
= ret
+ sechdr
->sh_size
;
1608 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1609 might -- code, read-only data, read-write data, small data. Tally
1610 sizes, and place the offsets into sh_entsize fields: high bit means it
1612 static void layout_sections(struct module
*mod
,
1613 const Elf_Ehdr
*hdr
,
1615 const char *secstrings
)
1617 static unsigned long const masks
[][2] = {
1618 /* NOTE: all executable code must be the first section
1619 * in this array; otherwise modify the text_size
1620 * finder in the two loops below */
1621 { SHF_EXECINSTR
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1622 { SHF_ALLOC
, SHF_WRITE
| ARCH_SHF_SMALL
},
1623 { SHF_WRITE
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1624 { ARCH_SHF_SMALL
| SHF_ALLOC
, 0 }
1628 for (i
= 0; i
< hdr
->e_shnum
; i
++)
1629 sechdrs
[i
].sh_entsize
= ~0UL;
1631 DEBUGP("Core section allocation order:\n");
1632 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1633 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1634 Elf_Shdr
*s
= &sechdrs
[i
];
1636 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1637 || (s
->sh_flags
& masks
[m
][1])
1638 || s
->sh_entsize
!= ~0UL
1639 || strncmp(secstrings
+ s
->sh_name
,
1642 s
->sh_entsize
= get_offset(mod
, &mod
->core_size
, s
, i
);
1643 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1646 mod
->core_text_size
= mod
->core_size
;
1649 DEBUGP("Init section allocation order:\n");
1650 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1651 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1652 Elf_Shdr
*s
= &sechdrs
[i
];
1654 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1655 || (s
->sh_flags
& masks
[m
][1])
1656 || s
->sh_entsize
!= ~0UL
1657 || strncmp(secstrings
+ s
->sh_name
,
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 #ifdef CONFIG_KALLSYMS
1733 /* lookup symbol in given range of kernel_symbols */
1734 static const struct kernel_symbol
*lookup_symbol(const char *name
,
1735 const struct kernel_symbol
*start
,
1736 const struct kernel_symbol
*stop
)
1738 const struct kernel_symbol
*ks
= start
;
1739 for (; ks
< stop
; ks
++)
1740 if (strcmp(ks
->name
, name
) == 0)
1745 static int is_exported(const char *name
, unsigned long value
,
1746 const struct module
*mod
)
1748 const struct kernel_symbol
*ks
;
1750 ks
= lookup_symbol(name
, __start___ksymtab
, __stop___ksymtab
);
1752 ks
= lookup_symbol(name
, mod
->syms
, mod
->syms
+ mod
->num_syms
);
1753 return ks
!= NULL
&& ks
->value
== value
;
1757 static char elf_type(const Elf_Sym
*sym
,
1759 const char *secstrings
,
1762 if (ELF_ST_BIND(sym
->st_info
) == STB_WEAK
) {
1763 if (ELF_ST_TYPE(sym
->st_info
) == STT_OBJECT
)
1768 if (sym
->st_shndx
== SHN_UNDEF
)
1770 if (sym
->st_shndx
== SHN_ABS
)
1772 if (sym
->st_shndx
>= SHN_LORESERVE
)
1774 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_EXECINSTR
)
1776 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_ALLOC
1777 && sechdrs
[sym
->st_shndx
].sh_type
!= SHT_NOBITS
) {
1778 if (!(sechdrs
[sym
->st_shndx
].sh_flags
& SHF_WRITE
))
1780 else if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1785 if (sechdrs
[sym
->st_shndx
].sh_type
== SHT_NOBITS
) {
1786 if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1791 if (strncmp(secstrings
+ sechdrs
[sym
->st_shndx
].sh_name
,
1792 ".debug", strlen(".debug")) == 0)
1797 static void add_kallsyms(struct module
*mod
,
1799 unsigned int symindex
,
1800 unsigned int strindex
,
1801 const char *secstrings
)
1805 mod
->symtab
= (void *)sechdrs
[symindex
].sh_addr
;
1806 mod
->num_symtab
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1807 mod
->strtab
= (void *)sechdrs
[strindex
].sh_addr
;
1809 /* Set types up while we still have access to sections. */
1810 for (i
= 0; i
< mod
->num_symtab
; i
++)
1811 mod
->symtab
[i
].st_info
1812 = elf_type(&mod
->symtab
[i
], sechdrs
, secstrings
, mod
);
1815 static inline void add_kallsyms(struct module
*mod
,
1817 unsigned int symindex
,
1818 unsigned int strindex
,
1819 const char *secstrings
)
1822 #endif /* CONFIG_KALLSYMS */
1824 static void dynamic_printk_setup(struct mod_debug
*debug
, unsigned int num
)
1826 #ifdef CONFIG_DYNAMIC_PRINTK_DEBUG
1829 for (i
= 0; i
< num
; i
++) {
1830 register_dynamic_debug_module(debug
[i
].modname
,
1832 debug
[i
].logical_modname
,
1833 debug
[i
].flag_names
,
1834 debug
[i
].hash
, debug
[i
].hash2
);
1836 #endif /* CONFIG_DYNAMIC_PRINTK_DEBUG */
1839 static void *module_alloc_update_bounds(unsigned long size
)
1841 void *ret
= module_alloc(size
);
1844 /* Update module bounds. */
1845 if ((unsigned long)ret
< module_addr_min
)
1846 module_addr_min
= (unsigned long)ret
;
1847 if ((unsigned long)ret
+ size
> module_addr_max
)
1848 module_addr_max
= (unsigned long)ret
+ size
;
1853 /* Allocate and load the module: note that size of section 0 is always
1854 zero, and we rely on this for optional sections. */
1855 static noinline
struct module
*load_module(void __user
*umod
,
1857 const char __user
*uargs
)
1861 char *secstrings
, *args
, *modmagic
, *strtab
= NULL
;
1864 unsigned int symindex
= 0;
1865 unsigned int strindex
= 0;
1866 unsigned int modindex
, versindex
, infoindex
, pcpuindex
;
1867 unsigned int num_kp
, num_mcount
;
1868 struct kernel_param
*kp
;
1871 void *percpu
= NULL
, *ptr
= NULL
; /* Stops spurious gcc warning */
1872 unsigned long *mseg
;
1873 mm_segment_t old_fs
;
1875 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1877 if (len
< sizeof(*hdr
))
1878 return ERR_PTR(-ENOEXEC
);
1880 /* Suck in entire file: we'll want most of it. */
1881 /* vmalloc barfs on "unusual" numbers. Check here */
1882 if (len
> 64 * 1024 * 1024 || (hdr
= vmalloc(len
)) == NULL
)
1883 return ERR_PTR(-ENOMEM
);
1885 /* Create stop_machine threads since the error path relies on
1886 * a non-failing stop_machine call. */
1887 err
= stop_machine_create();
1891 if (copy_from_user(hdr
, umod
, len
) != 0) {
1896 /* Sanity checks against insmoding binaries or wrong arch,
1897 weird elf version */
1898 if (memcmp(hdr
->e_ident
, ELFMAG
, SELFMAG
) != 0
1899 || hdr
->e_type
!= ET_REL
1900 || !elf_check_arch(hdr
)
1901 || hdr
->e_shentsize
!= sizeof(*sechdrs
)) {
1906 if (len
< hdr
->e_shoff
+ hdr
->e_shnum
* sizeof(Elf_Shdr
))
1909 /* Convenience variables */
1910 sechdrs
= (void *)hdr
+ hdr
->e_shoff
;
1911 secstrings
= (void *)hdr
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
1912 sechdrs
[0].sh_addr
= 0;
1914 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
1915 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
1916 && len
< sechdrs
[i
].sh_offset
+ sechdrs
[i
].sh_size
)
1919 /* Mark all sections sh_addr with their address in the
1921 sechdrs
[i
].sh_addr
= (size_t)hdr
+ sechdrs
[i
].sh_offset
;
1923 /* Internal symbols and strings. */
1924 if (sechdrs
[i
].sh_type
== SHT_SYMTAB
) {
1926 strindex
= sechdrs
[i
].sh_link
;
1927 strtab
= (char *)hdr
+ sechdrs
[strindex
].sh_offset
;
1929 #ifndef CONFIG_MODULE_UNLOAD
1930 /* Don't load .exit sections */
1931 if (strncmp(secstrings
+sechdrs
[i
].sh_name
, ".exit", 5) == 0)
1932 sechdrs
[i
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1936 modindex
= find_sec(hdr
, sechdrs
, secstrings
,
1937 ".gnu.linkonce.this_module");
1939 printk(KERN_WARNING
"No module found in object\n");
1943 /* This is temporary: point mod into copy of data. */
1944 mod
= (void *)sechdrs
[modindex
].sh_addr
;
1946 if (symindex
== 0) {
1947 printk(KERN_WARNING
"%s: module has no symbols (stripped?)\n",
1953 versindex
= find_sec(hdr
, sechdrs
, secstrings
, "__versions");
1954 infoindex
= find_sec(hdr
, sechdrs
, secstrings
, ".modinfo");
1955 pcpuindex
= find_pcpusec(hdr
, sechdrs
, secstrings
);
1957 /* Don't keep modinfo and version sections. */
1958 sechdrs
[infoindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1959 sechdrs
[versindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1960 #ifdef CONFIG_KALLSYMS
1961 /* Keep symbol and string tables for decoding later. */
1962 sechdrs
[symindex
].sh_flags
|= SHF_ALLOC
;
1963 sechdrs
[strindex
].sh_flags
|= SHF_ALLOC
;
1966 /* Check module struct version now, before we try to use module. */
1967 if (!check_modstruct_version(sechdrs
, versindex
, mod
)) {
1972 modmagic
= get_modinfo(sechdrs
, infoindex
, "vermagic");
1973 /* This is allowed: modprobe --force will invalidate it. */
1975 err
= try_to_force_load(mod
, "magic");
1978 } else if (!same_magic(modmagic
, vermagic
, versindex
)) {
1979 printk(KERN_ERR
"%s: version magic '%s' should be '%s'\n",
1980 mod
->name
, modmagic
, vermagic
);
1985 staging
= get_modinfo(sechdrs
, infoindex
, "staging");
1987 add_taint_module(mod
, TAINT_CRAP
);
1988 printk(KERN_WARNING
"%s: module is from the staging directory,"
1989 " the quality is unknown, you have been warned.\n",
1993 /* Now copy in args */
1994 args
= strndup_user(uargs
, ~0UL >> 1);
1996 err
= PTR_ERR(args
);
2000 if (find_module(mod
->name
)) {
2005 mod
->state
= MODULE_STATE_COMING
;
2007 /* Allow arches to frob section contents and sizes. */
2008 err
= module_frob_arch_sections(hdr
, sechdrs
, secstrings
, mod
);
2013 /* We have a special allocation for this section. */
2014 percpu
= percpu_modalloc(sechdrs
[pcpuindex
].sh_size
,
2015 sechdrs
[pcpuindex
].sh_addralign
,
2021 sechdrs
[pcpuindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2022 mod
->percpu
= percpu
;
2025 /* Determine total sizes, and put offsets in sh_entsize. For now
2026 this is done generically; there doesn't appear to be any
2027 special cases for the architectures. */
2028 layout_sections(mod
, hdr
, sechdrs
, secstrings
);
2030 /* Do the allocs. */
2031 ptr
= module_alloc_update_bounds(mod
->core_size
);
2036 memset(ptr
, 0, mod
->core_size
);
2037 mod
->module_core
= ptr
;
2039 ptr
= module_alloc_update_bounds(mod
->init_size
);
2040 if (!ptr
&& mod
->init_size
) {
2044 memset(ptr
, 0, mod
->init_size
);
2045 mod
->module_init
= ptr
;
2047 /* Transfer each section which specifies SHF_ALLOC */
2048 DEBUGP("final section addresses:\n");
2049 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
2052 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
2055 if (sechdrs
[i
].sh_entsize
& INIT_OFFSET_MASK
)
2056 dest
= mod
->module_init
2057 + (sechdrs
[i
].sh_entsize
& ~INIT_OFFSET_MASK
);
2059 dest
= mod
->module_core
+ sechdrs
[i
].sh_entsize
;
2061 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
)
2062 memcpy(dest
, (void *)sechdrs
[i
].sh_addr
,
2063 sechdrs
[i
].sh_size
);
2064 /* Update sh_addr to point to copy in image. */
2065 sechdrs
[i
].sh_addr
= (unsigned long)dest
;
2066 DEBUGP("\t0x%lx %s\n", sechdrs
[i
].sh_addr
, secstrings
+ sechdrs
[i
].sh_name
);
2068 /* Module has been moved. */
2069 mod
= (void *)sechdrs
[modindex
].sh_addr
;
2071 /* Now we've moved module, initialize linked lists, etc. */
2072 module_unload_init(mod
);
2074 /* add kobject, so we can reference it. */
2075 err
= mod_sysfs_init(mod
);
2079 /* Set up license info based on the info section */
2080 set_license(mod
, get_modinfo(sechdrs
, infoindex
, "license"));
2083 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2084 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2085 * using GPL-only symbols it needs.
2087 if (strcmp(mod
->name
, "ndiswrapper") == 0)
2088 add_taint(TAINT_PROPRIETARY_MODULE
);
2090 /* driverloader was caught wrongly pretending to be under GPL */
2091 if (strcmp(mod
->name
, "driverloader") == 0)
2092 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
2094 /* Set up MODINFO_ATTR fields */
2095 setup_modinfo(mod
, sechdrs
, infoindex
);
2097 /* Fix up syms, so that st_value is a pointer to location. */
2098 err
= simplify_symbols(sechdrs
, symindex
, strtab
, versindex
, pcpuindex
,
2103 /* Now we've got everything in the final locations, we can
2104 * find optional sections. */
2105 kp
= section_objs(hdr
, sechdrs
, secstrings
, "__param", sizeof(*kp
),
2107 mod
->syms
= section_objs(hdr
, sechdrs
, secstrings
, "__ksymtab",
2108 sizeof(*mod
->syms
), &mod
->num_syms
);
2109 mod
->crcs
= section_addr(hdr
, sechdrs
, secstrings
, "__kcrctab");
2110 mod
->gpl_syms
= section_objs(hdr
, sechdrs
, secstrings
, "__ksymtab_gpl",
2111 sizeof(*mod
->gpl_syms
),
2112 &mod
->num_gpl_syms
);
2113 mod
->gpl_crcs
= section_addr(hdr
, sechdrs
, secstrings
, "__kcrctab_gpl");
2114 mod
->gpl_future_syms
= section_objs(hdr
, sechdrs
, secstrings
,
2115 "__ksymtab_gpl_future",
2116 sizeof(*mod
->gpl_future_syms
),
2117 &mod
->num_gpl_future_syms
);
2118 mod
->gpl_future_crcs
= section_addr(hdr
, sechdrs
, secstrings
,
2119 "__kcrctab_gpl_future");
2121 #ifdef CONFIG_UNUSED_SYMBOLS
2122 mod
->unused_syms
= section_objs(hdr
, sechdrs
, secstrings
,
2124 sizeof(*mod
->unused_syms
),
2125 &mod
->num_unused_syms
);
2126 mod
->unused_crcs
= section_addr(hdr
, sechdrs
, secstrings
,
2127 "__kcrctab_unused");
2128 mod
->unused_gpl_syms
= section_objs(hdr
, sechdrs
, secstrings
,
2129 "__ksymtab_unused_gpl",
2130 sizeof(*mod
->unused_gpl_syms
),
2131 &mod
->num_unused_gpl_syms
);
2132 mod
->unused_gpl_crcs
= section_addr(hdr
, sechdrs
, secstrings
,
2133 "__kcrctab_unused_gpl");
2136 #ifdef CONFIG_MARKERS
2137 mod
->markers
= section_objs(hdr
, sechdrs
, secstrings
, "__markers",
2138 sizeof(*mod
->markers
), &mod
->num_markers
);
2140 #ifdef CONFIG_TRACEPOINTS
2141 mod
->tracepoints
= section_objs(hdr
, sechdrs
, secstrings
,
2143 sizeof(*mod
->tracepoints
),
2144 &mod
->num_tracepoints
);
2147 #ifdef CONFIG_MODVERSIONS
2148 if ((mod
->num_syms
&& !mod
->crcs
)
2149 || (mod
->num_gpl_syms
&& !mod
->gpl_crcs
)
2150 || (mod
->num_gpl_future_syms
&& !mod
->gpl_future_crcs
)
2151 #ifdef CONFIG_UNUSED_SYMBOLS
2152 || (mod
->num_unused_syms
&& !mod
->unused_crcs
)
2153 || (mod
->num_unused_gpl_syms
&& !mod
->unused_gpl_crcs
)
2156 printk(KERN_WARNING
"%s: No versions for exported symbols.\n", mod
->name
);
2157 err
= try_to_force_load(mod
, "nocrc");
2163 /* Now do relocations. */
2164 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
2165 const char *strtab
= (char *)sechdrs
[strindex
].sh_addr
;
2166 unsigned int info
= sechdrs
[i
].sh_info
;
2168 /* Not a valid relocation section? */
2169 if (info
>= hdr
->e_shnum
)
2172 /* Don't bother with non-allocated sections */
2173 if (!(sechdrs
[info
].sh_flags
& SHF_ALLOC
))
2176 if (sechdrs
[i
].sh_type
== SHT_REL
)
2177 err
= apply_relocate(sechdrs
, strtab
, symindex
, i
,mod
);
2178 else if (sechdrs
[i
].sh_type
== SHT_RELA
)
2179 err
= apply_relocate_add(sechdrs
, strtab
, symindex
, i
,
2185 /* Find duplicate symbols */
2186 err
= verify_export_symbols(mod
);
2190 /* Set up and sort exception table */
2191 mod
->extable
= section_objs(hdr
, sechdrs
, secstrings
, "__ex_table",
2192 sizeof(*mod
->extable
), &mod
->num_exentries
);
2193 sort_extable(mod
->extable
, mod
->extable
+ mod
->num_exentries
);
2195 /* Finally, copy percpu area over. */
2196 percpu_modcopy(mod
->percpu
, (void *)sechdrs
[pcpuindex
].sh_addr
,
2197 sechdrs
[pcpuindex
].sh_size
);
2199 add_kallsyms(mod
, sechdrs
, symindex
, strindex
, secstrings
);
2202 struct mod_debug
*debug
;
2203 unsigned int num_debug
;
2205 debug
= section_objs(hdr
, sechdrs
, secstrings
, "__verbose",
2206 sizeof(*debug
), &num_debug
);
2207 dynamic_printk_setup(debug
, num_debug
);
2210 /* sechdrs[0].sh_size is always zero */
2211 mseg
= section_objs(hdr
, sechdrs
, secstrings
, "__mcount_loc",
2212 sizeof(*mseg
), &num_mcount
);
2213 ftrace_init_module(mod
, mseg
, mseg
+ num_mcount
);
2215 err
= module_finalize(hdr
, sechdrs
, mod
);
2219 /* flush the icache in correct context */
2224 * Flush the instruction cache, since we've played with text.
2225 * Do it before processing of module parameters, so the module
2226 * can provide parameter accessor functions of its own.
2228 if (mod
->module_init
)
2229 flush_icache_range((unsigned long)mod
->module_init
,
2230 (unsigned long)mod
->module_init
2232 flush_icache_range((unsigned long)mod
->module_core
,
2233 (unsigned long)mod
->module_core
+ mod
->core_size
);
2238 if (section_addr(hdr
, sechdrs
, secstrings
, "__obsparm"))
2239 printk(KERN_WARNING
"%s: Ignoring obsolete parameters\n",
2242 /* Now sew it into the lists so we can get lockdep and oops
2243 * info during argument parsing. Noone should access us, since
2244 * strong_try_module_get() will fail.
2245 * lockdep/oops can run asynchronous, so use the RCU list insertion
2246 * function to insert in a way safe to concurrent readers.
2247 * The mutex protects against concurrent writers.
2249 list_add_rcu(&mod
->list
, &modules
);
2251 err
= parse_args(mod
->name
, mod
->args
, kp
, num_kp
, NULL
);
2255 err
= mod_sysfs_setup(mod
, kp
, num_kp
);
2258 add_sect_attrs(mod
, hdr
->e_shnum
, secstrings
, sechdrs
);
2259 add_notes_attrs(mod
, hdr
->e_shnum
, secstrings
, sechdrs
);
2261 /* Get rid of temporary copy */
2264 stop_machine_destroy();
2269 stop_machine(__unlink_module
, mod
, NULL
);
2270 module_arch_cleanup(mod
);
2272 kobject_del(&mod
->mkobj
.kobj
);
2273 kobject_put(&mod
->mkobj
.kobj
);
2274 ftrace_release(mod
->module_core
, mod
->core_size
);
2276 module_unload_free(mod
);
2277 module_free(mod
, mod
->module_init
);
2279 module_free(mod
, mod
->module_core
);
2282 percpu_modfree(percpu
);
2287 stop_machine_destroy();
2288 return ERR_PTR(err
);
2291 printk(KERN_ERR
"Module len %lu truncated\n", len
);
2296 /* This is where the real work happens */
2298 sys_init_module(void __user
*umod
,
2300 const char __user
*uargs
)
2305 /* Must have permission */
2306 if (!capable(CAP_SYS_MODULE
))
2309 /* Only one module load at a time, please */
2310 if (mutex_lock_interruptible(&module_mutex
) != 0)
2313 /* Do all the hard work */
2314 mod
= load_module(umod
, len
, uargs
);
2316 mutex_unlock(&module_mutex
);
2317 return PTR_ERR(mod
);
2320 /* Drop lock so they can recurse */
2321 mutex_unlock(&module_mutex
);
2323 blocking_notifier_call_chain(&module_notify_list
,
2324 MODULE_STATE_COMING
, mod
);
2326 /* Start the module */
2327 if (mod
->init
!= NULL
)
2328 ret
= do_one_initcall(mod
->init
);
2330 /* Init routine failed: abort. Try to protect us from
2331 buggy refcounters. */
2332 mod
->state
= MODULE_STATE_GOING
;
2333 synchronize_sched();
2335 blocking_notifier_call_chain(&module_notify_list
,
2336 MODULE_STATE_GOING
, mod
);
2337 mutex_lock(&module_mutex
);
2339 mutex_unlock(&module_mutex
);
2340 wake_up(&module_wq
);
2344 printk(KERN_WARNING
"%s: '%s'->init suspiciously returned %d, "
2345 "it should follow 0/-E convention\n"
2346 KERN_WARNING
"%s: loading module anyway...\n",
2347 __func__
, mod
->name
, ret
,
2352 /* Now it's a first class citizen! Wake up anyone waiting for it. */
2353 mod
->state
= MODULE_STATE_LIVE
;
2354 wake_up(&module_wq
);
2356 mutex_lock(&module_mutex
);
2357 /* Drop initial reference. */
2359 module_free(mod
, mod
->module_init
);
2360 mod
->module_init
= NULL
;
2362 mod
->init_text_size
= 0;
2363 mutex_unlock(&module_mutex
);
2368 static inline int within(unsigned long addr
, void *start
, unsigned long size
)
2370 return ((void *)addr
>= start
&& (void *)addr
< start
+ size
);
2373 #ifdef CONFIG_KALLSYMS
2375 * This ignores the intensely annoying "mapping symbols" found
2376 * in ARM ELF files: $a, $t and $d.
2378 static inline int is_arm_mapping_symbol(const char *str
)
2380 return str
[0] == '$' && strchr("atd", str
[1])
2381 && (str
[2] == '\0' || str
[2] == '.');
2384 static const char *get_ksymbol(struct module
*mod
,
2386 unsigned long *size
,
2387 unsigned long *offset
)
2389 unsigned int i
, best
= 0;
2390 unsigned long nextval
;
2392 /* At worse, next value is at end of module */
2393 if (within(addr
, mod
->module_init
, mod
->init_size
))
2394 nextval
= (unsigned long)mod
->module_init
+mod
->init_text_size
;
2396 nextval
= (unsigned long)mod
->module_core
+mod
->core_text_size
;
2398 /* Scan for closest preceeding symbol, and next symbol. (ELF
2399 starts real symbols at 1). */
2400 for (i
= 1; i
< mod
->num_symtab
; i
++) {
2401 if (mod
->symtab
[i
].st_shndx
== SHN_UNDEF
)
2404 /* We ignore unnamed symbols: they're uninformative
2405 * and inserted at a whim. */
2406 if (mod
->symtab
[i
].st_value
<= addr
2407 && mod
->symtab
[i
].st_value
> mod
->symtab
[best
].st_value
2408 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
2409 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
2411 if (mod
->symtab
[i
].st_value
> addr
2412 && mod
->symtab
[i
].st_value
< nextval
2413 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
2414 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
2415 nextval
= mod
->symtab
[i
].st_value
;
2422 *size
= nextval
- mod
->symtab
[best
].st_value
;
2424 *offset
= addr
- mod
->symtab
[best
].st_value
;
2425 return mod
->strtab
+ mod
->symtab
[best
].st_name
;
2428 /* For kallsyms to ask for address resolution. NULL means not found. Careful
2429 * not to lock to avoid deadlock on oopses, simply disable preemption. */
2430 const char *module_address_lookup(unsigned long addr
,
2431 unsigned long *size
,
2432 unsigned long *offset
,
2437 const char *ret
= NULL
;
2440 list_for_each_entry_rcu(mod
, &modules
, list
) {
2441 if (within(addr
, mod
->module_init
, mod
->init_size
)
2442 || within(addr
, mod
->module_core
, mod
->core_size
)) {
2444 *modname
= mod
->name
;
2445 ret
= get_ksymbol(mod
, addr
, size
, offset
);
2449 /* Make a copy in here where it's safe */
2451 strncpy(namebuf
, ret
, KSYM_NAME_LEN
- 1);
2458 int lookup_module_symbol_name(unsigned long addr
, char *symname
)
2463 list_for_each_entry_rcu(mod
, &modules
, list
) {
2464 if (within(addr
, mod
->module_init
, mod
->init_size
) ||
2465 within(addr
, mod
->module_core
, mod
->core_size
)) {
2468 sym
= get_ksymbol(mod
, addr
, NULL
, NULL
);
2471 strlcpy(symname
, sym
, KSYM_NAME_LEN
);
2481 int lookup_module_symbol_attrs(unsigned long addr
, unsigned long *size
,
2482 unsigned long *offset
, char *modname
, char *name
)
2487 list_for_each_entry_rcu(mod
, &modules
, list
) {
2488 if (within(addr
, mod
->module_init
, mod
->init_size
) ||
2489 within(addr
, mod
->module_core
, mod
->core_size
)) {
2492 sym
= get_ksymbol(mod
, addr
, size
, offset
);
2496 strlcpy(modname
, mod
->name
, MODULE_NAME_LEN
);
2498 strlcpy(name
, sym
, KSYM_NAME_LEN
);
2508 int module_get_kallsym(unsigned int symnum
, unsigned long *value
, char *type
,
2509 char *name
, char *module_name
, int *exported
)
2514 list_for_each_entry_rcu(mod
, &modules
, list
) {
2515 if (symnum
< mod
->num_symtab
) {
2516 *value
= mod
->symtab
[symnum
].st_value
;
2517 *type
= mod
->symtab
[symnum
].st_info
;
2518 strlcpy(name
, mod
->strtab
+ mod
->symtab
[symnum
].st_name
,
2520 strlcpy(module_name
, mod
->name
, MODULE_NAME_LEN
);
2521 *exported
= is_exported(name
, *value
, mod
);
2525 symnum
-= mod
->num_symtab
;
2531 static unsigned long mod_find_symname(struct module
*mod
, const char *name
)
2535 for (i
= 0; i
< mod
->num_symtab
; i
++)
2536 if (strcmp(name
, mod
->strtab
+mod
->symtab
[i
].st_name
) == 0 &&
2537 mod
->symtab
[i
].st_info
!= 'U')
2538 return mod
->symtab
[i
].st_value
;
2542 /* Look for this name: can be of form module:name. */
2543 unsigned long module_kallsyms_lookup_name(const char *name
)
2547 unsigned long ret
= 0;
2549 /* Don't lock: we're in enough trouble already. */
2551 if ((colon
= strchr(name
, ':')) != NULL
) {
2553 if ((mod
= find_module(name
)) != NULL
)
2554 ret
= mod_find_symname(mod
, colon
+1);
2557 list_for_each_entry_rcu(mod
, &modules
, list
)
2558 if ((ret
= mod_find_symname(mod
, name
)) != 0)
2564 #endif /* CONFIG_KALLSYMS */
2566 static char *module_flags(struct module
*mod
, char *buf
)
2571 mod
->state
== MODULE_STATE_GOING
||
2572 mod
->state
== MODULE_STATE_COMING
) {
2574 if (mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
))
2576 if (mod
->taints
& (1 << TAINT_FORCED_MODULE
))
2578 if (mod
->taints
& (1 << TAINT_CRAP
))
2581 * TAINT_FORCED_RMMOD: could be added.
2582 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2586 /* Show a - for module-is-being-unloaded */
2587 if (mod
->state
== MODULE_STATE_GOING
)
2589 /* Show a + for module-is-being-loaded */
2590 if (mod
->state
== MODULE_STATE_COMING
)
2599 #ifdef CONFIG_PROC_FS
2600 /* Called by the /proc file system to return a list of modules. */
2601 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
2603 mutex_lock(&module_mutex
);
2604 return seq_list_start(&modules
, *pos
);
2607 static void *m_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
2609 return seq_list_next(p
, &modules
, pos
);
2612 static void m_stop(struct seq_file
*m
, void *p
)
2614 mutex_unlock(&module_mutex
);
2617 static int m_show(struct seq_file
*m
, void *p
)
2619 struct module
*mod
= list_entry(p
, struct module
, list
);
2622 seq_printf(m
, "%s %u",
2623 mod
->name
, mod
->init_size
+ mod
->core_size
);
2624 print_unload_info(m
, mod
);
2626 /* Informative for users. */
2627 seq_printf(m
, " %s",
2628 mod
->state
== MODULE_STATE_GOING
? "Unloading":
2629 mod
->state
== MODULE_STATE_COMING
? "Loading":
2631 /* Used by oprofile and other similar tools. */
2632 seq_printf(m
, " 0x%p", mod
->module_core
);
2636 seq_printf(m
, " %s", module_flags(mod
, buf
));
2638 seq_printf(m
, "\n");
2642 /* Format: modulename size refcount deps address
2644 Where refcount is a number or -, and deps is a comma-separated list
2647 static const struct seq_operations modules_op
= {
2654 static int modules_open(struct inode
*inode
, struct file
*file
)
2656 return seq_open(file
, &modules_op
);
2659 static const struct file_operations proc_modules_operations
= {
2660 .open
= modules_open
,
2662 .llseek
= seq_lseek
,
2663 .release
= seq_release
,
2666 static int __init
proc_modules_init(void)
2668 proc_create("modules", 0, NULL
, &proc_modules_operations
);
2671 module_init(proc_modules_init
);
2674 /* Given an address, look for it in the module exception tables. */
2675 const struct exception_table_entry
*search_module_extables(unsigned long addr
)
2677 const struct exception_table_entry
*e
= NULL
;
2681 list_for_each_entry_rcu(mod
, &modules
, list
) {
2682 if (mod
->num_exentries
== 0)
2685 e
= search_extable(mod
->extable
,
2686 mod
->extable
+ mod
->num_exentries
- 1,
2693 /* Now, if we found one, we are running inside it now, hence
2694 we cannot unload the module, hence no refcnt needed. */
2699 * Is this a valid module address?
2701 int is_module_address(unsigned long addr
)
2707 list_for_each_entry_rcu(mod
, &modules
, list
) {
2708 if (within(addr
, mod
->module_core
, mod
->core_size
)) {
2720 /* Is this a valid kernel address? */
2721 __notrace_funcgraph
struct module
*__module_text_address(unsigned long addr
)
2725 if (addr
< module_addr_min
|| addr
> module_addr_max
)
2728 list_for_each_entry_rcu(mod
, &modules
, list
)
2729 if (within(addr
, mod
->module_init
, mod
->init_text_size
)
2730 || within(addr
, mod
->module_core
, mod
->core_text_size
))
2735 struct module
*module_text_address(unsigned long addr
)
2740 mod
= __module_text_address(addr
);
2746 /* Don't grab lock, we're oopsing. */
2747 void print_modules(void)
2752 printk("Modules linked in:");
2753 /* Most callers should already have preempt disabled, but make sure */
2755 list_for_each_entry_rcu(mod
, &modules
, list
)
2756 printk(" %s%s", mod
->name
, module_flags(mod
, buf
));
2758 if (last_unloaded_module
[0])
2759 printk(" [last unloaded: %s]", last_unloaded_module
);
2763 #ifdef CONFIG_MODVERSIONS
2764 /* Generate the signature for struct module here, too, for modversions. */
2765 void struct_module(struct module
*mod
) { return; }
2766 EXPORT_SYMBOL(struct_module
);
2769 #ifdef CONFIG_MARKERS
2770 void module_update_markers(void)
2774 mutex_lock(&module_mutex
);
2775 list_for_each_entry(mod
, &modules
, list
)
2777 marker_update_probe_range(mod
->markers
,
2778 mod
->markers
+ mod
->num_markers
);
2779 mutex_unlock(&module_mutex
);
2783 #ifdef CONFIG_TRACEPOINTS
2784 void module_update_tracepoints(void)
2788 mutex_lock(&module_mutex
);
2789 list_for_each_entry(mod
, &modules
, list
)
2791 tracepoint_update_probe_range(mod
->tracepoints
,
2792 mod
->tracepoints
+ mod
->num_tracepoints
);
2793 mutex_unlock(&module_mutex
);
2797 * Returns 0 if current not found.
2798 * Returns 1 if current found.
2800 int module_get_iter_tracepoints(struct tracepoint_iter
*iter
)
2802 struct module
*iter_mod
;
2805 mutex_lock(&module_mutex
);
2806 list_for_each_entry(iter_mod
, &modules
, list
) {
2807 if (!iter_mod
->taints
) {
2809 * Sorted module list
2811 if (iter_mod
< iter
->module
)
2813 else if (iter_mod
> iter
->module
)
2814 iter
->tracepoint
= NULL
;
2815 found
= tracepoint_get_iter_range(&iter
->tracepoint
,
2816 iter_mod
->tracepoints
,
2817 iter_mod
->tracepoints
2818 + iter_mod
->num_tracepoints
);
2820 iter
->module
= iter_mod
;
2825 mutex_unlock(&module_mutex
);