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/unwind.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>
58 #define DEBUGP(fmt , a...)
61 #ifndef ARCH_SHF_SMALL
62 #define ARCH_SHF_SMALL 0
65 /* If this is set, the section belongs in the init part of the module */
66 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
68 /* List of modules, protected by module_mutex or preempt_disable
69 * (delete uses stop_machine/add uses RCU list operations). */
70 static DEFINE_MUTEX(module_mutex
);
71 static LIST_HEAD(modules
);
73 /* Waiting for a module to finish initializing? */
74 static DECLARE_WAIT_QUEUE_HEAD(module_wq
);
76 static BLOCKING_NOTIFIER_HEAD(module_notify_list
);
78 /* Bounds of module allocation, for speeding __module_text_address */
79 static unsigned long module_addr_min
= -1UL, module_addr_max
= 0;
81 int register_module_notifier(struct notifier_block
* nb
)
83 return blocking_notifier_chain_register(&module_notify_list
, nb
);
85 EXPORT_SYMBOL(register_module_notifier
);
87 int unregister_module_notifier(struct notifier_block
* nb
)
89 return blocking_notifier_chain_unregister(&module_notify_list
, nb
);
91 EXPORT_SYMBOL(unregister_module_notifier
);
93 /* We require a truly strong try_module_get(): 0 means failure due to
94 ongoing or failed initialization etc. */
95 static inline int strong_try_module_get(struct module
*mod
)
97 if (mod
&& mod
->state
== MODULE_STATE_COMING
)
99 if (try_module_get(mod
))
105 static inline void add_taint_module(struct module
*mod
, unsigned flag
)
108 mod
->taints
|= (1U << flag
);
112 * A thread that wants to hold a reference to a module only while it
113 * is running can call this to safely exit. nfsd and lockd use this.
115 void __module_put_and_exit(struct module
*mod
, long code
)
120 EXPORT_SYMBOL(__module_put_and_exit
);
122 /* Find a module section: 0 means not found. */
123 static unsigned int find_sec(Elf_Ehdr
*hdr
,
125 const char *secstrings
,
130 for (i
= 1; i
< hdr
->e_shnum
; i
++)
131 /* Alloc bit cleared means "ignore it." */
132 if ((sechdrs
[i
].sh_flags
& SHF_ALLOC
)
133 && strcmp(secstrings
+sechdrs
[i
].sh_name
, name
) == 0)
138 /* Find a module section, or NULL. */
139 static void *section_addr(Elf_Ehdr
*hdr
, Elf_Shdr
*shdrs
,
140 const char *secstrings
, const char *name
)
142 /* Section 0 has sh_addr 0. */
143 return (void *)shdrs
[find_sec(hdr
, shdrs
, secstrings
, name
)].sh_addr
;
146 /* Find a module section, or NULL. Fill in number of "objects" in section. */
147 static void *section_objs(Elf_Ehdr
*hdr
,
149 const char *secstrings
,
154 unsigned int sec
= find_sec(hdr
, sechdrs
, secstrings
, name
);
156 /* Section 0 has sh_addr 0 and sh_size 0. */
157 *num
= sechdrs
[sec
].sh_size
/ object_size
;
158 return (void *)sechdrs
[sec
].sh_addr
;
161 /* Provided by the linker */
162 extern const struct kernel_symbol __start___ksymtab
[];
163 extern const struct kernel_symbol __stop___ksymtab
[];
164 extern const struct kernel_symbol __start___ksymtab_gpl
[];
165 extern const struct kernel_symbol __stop___ksymtab_gpl
[];
166 extern const struct kernel_symbol __start___ksymtab_gpl_future
[];
167 extern const struct kernel_symbol __stop___ksymtab_gpl_future
[];
168 extern const struct kernel_symbol __start___ksymtab_gpl_future
[];
169 extern const struct kernel_symbol __stop___ksymtab_gpl_future
[];
170 extern const unsigned long __start___kcrctab
[];
171 extern const unsigned long __start___kcrctab_gpl
[];
172 extern const unsigned long __start___kcrctab_gpl_future
[];
173 #ifdef CONFIG_UNUSED_SYMBOLS
174 extern const struct kernel_symbol __start___ksymtab_unused
[];
175 extern const struct kernel_symbol __stop___ksymtab_unused
[];
176 extern const struct kernel_symbol __start___ksymtab_unused_gpl
[];
177 extern const struct kernel_symbol __stop___ksymtab_unused_gpl
[];
178 extern const unsigned long __start___kcrctab_unused
[];
179 extern const unsigned long __start___kcrctab_unused_gpl
[];
182 #ifndef CONFIG_MODVERSIONS
183 #define symversion(base, idx) NULL
185 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
189 const struct kernel_symbol
*start
, *stop
;
190 const unsigned long *crcs
;
199 static bool each_symbol_in_section(const struct symsearch
*arr
,
200 unsigned int arrsize
,
201 struct module
*owner
,
202 bool (*fn
)(const struct symsearch
*syms
,
203 struct module
*owner
,
204 unsigned int symnum
, void *data
),
209 for (j
= 0; j
< arrsize
; j
++) {
210 for (i
= 0; i
< arr
[j
].stop
- arr
[j
].start
; i
++)
211 if (fn(&arr
[j
], owner
, i
, data
))
218 /* Returns true as soon as fn returns true, otherwise false. */
219 static bool each_symbol(bool (*fn
)(const struct symsearch
*arr
,
220 struct module
*owner
,
221 unsigned int symnum
, void *data
),
225 const struct symsearch arr
[] = {
226 { __start___ksymtab
, __stop___ksymtab
, __start___kcrctab
,
227 NOT_GPL_ONLY
, false },
228 { __start___ksymtab_gpl
, __stop___ksymtab_gpl
,
229 __start___kcrctab_gpl
,
231 { __start___ksymtab_gpl_future
, __stop___ksymtab_gpl_future
,
232 __start___kcrctab_gpl_future
,
233 WILL_BE_GPL_ONLY
, false },
234 #ifdef CONFIG_UNUSED_SYMBOLS
235 { __start___ksymtab_unused
, __stop___ksymtab_unused
,
236 __start___kcrctab_unused
,
237 NOT_GPL_ONLY
, true },
238 { __start___ksymtab_unused_gpl
, __stop___ksymtab_unused_gpl
,
239 __start___kcrctab_unused_gpl
,
244 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), NULL
, fn
, data
))
247 list_for_each_entry_rcu(mod
, &modules
, list
) {
248 struct symsearch arr
[] = {
249 { mod
->syms
, mod
->syms
+ mod
->num_syms
, mod
->crcs
,
250 NOT_GPL_ONLY
, false },
251 { mod
->gpl_syms
, mod
->gpl_syms
+ mod
->num_gpl_syms
,
254 { mod
->gpl_future_syms
,
255 mod
->gpl_future_syms
+ mod
->num_gpl_future_syms
,
256 mod
->gpl_future_crcs
,
257 WILL_BE_GPL_ONLY
, false },
258 #ifdef CONFIG_UNUSED_SYMBOLS
260 mod
->unused_syms
+ mod
->num_unused_syms
,
262 NOT_GPL_ONLY
, true },
263 { mod
->unused_gpl_syms
,
264 mod
->unused_gpl_syms
+ mod
->num_unused_gpl_syms
,
265 mod
->unused_gpl_crcs
,
270 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), mod
, fn
, data
))
276 struct find_symbol_arg
{
283 struct module
*owner
;
284 const unsigned long *crc
;
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
->value
= syms
->start
[symnum
].value
;
330 /* Find a symbol, return value, (optional) crc and (optional) module
332 static unsigned long 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
);
356 /* Search for module by name: must hold module_mutex. */
357 static struct module
*find_module(const char *name
)
361 list_for_each_entry(mod
, &modules
, list
) {
362 if (strcmp(mod
->name
, name
) == 0)
369 /* Number of blocks used and allocated. */
370 static unsigned int pcpu_num_used
, pcpu_num_allocated
;
371 /* Size of each block. -ve means used. */
372 static int *pcpu_size
;
374 static int split_block(unsigned int i
, unsigned short size
)
376 /* Reallocation required? */
377 if (pcpu_num_used
+ 1 > pcpu_num_allocated
) {
380 new = krealloc(pcpu_size
, sizeof(new[0])*pcpu_num_allocated
*2,
385 pcpu_num_allocated
*= 2;
389 /* Insert a new subblock */
390 memmove(&pcpu_size
[i
+1], &pcpu_size
[i
],
391 sizeof(pcpu_size
[0]) * (pcpu_num_used
- i
));
394 pcpu_size
[i
+1] -= size
;
399 static inline unsigned int block_size(int val
)
406 static void *percpu_modalloc(unsigned long size
, unsigned long align
,
413 if (align
> PAGE_SIZE
) {
414 printk(KERN_WARNING
"%s: per-cpu alignment %li > %li\n",
415 name
, align
, PAGE_SIZE
);
419 ptr
= __per_cpu_start
;
420 for (i
= 0; i
< pcpu_num_used
; ptr
+= block_size(pcpu_size
[i
]), i
++) {
421 /* Extra for alignment requirement. */
422 extra
= ALIGN((unsigned long)ptr
, align
) - (unsigned long)ptr
;
423 BUG_ON(i
== 0 && extra
!= 0);
425 if (pcpu_size
[i
] < 0 || pcpu_size
[i
] < extra
+ size
)
428 /* Transfer extra to previous block. */
429 if (pcpu_size
[i
-1] < 0)
430 pcpu_size
[i
-1] -= extra
;
432 pcpu_size
[i
-1] += extra
;
433 pcpu_size
[i
] -= extra
;
436 /* Split block if warranted */
437 if (pcpu_size
[i
] - size
> sizeof(unsigned long))
438 if (!split_block(i
, size
))
442 pcpu_size
[i
] = -pcpu_size
[i
];
446 printk(KERN_WARNING
"Could not allocate %lu bytes percpu data\n",
451 static void percpu_modfree(void *freeme
)
454 void *ptr
= __per_cpu_start
+ block_size(pcpu_size
[0]);
456 /* First entry is core kernel percpu data. */
457 for (i
= 1; i
< pcpu_num_used
; ptr
+= block_size(pcpu_size
[i
]), i
++) {
459 pcpu_size
[i
] = -pcpu_size
[i
];
466 /* Merge with previous? */
467 if (pcpu_size
[i
-1] >= 0) {
468 pcpu_size
[i
-1] += pcpu_size
[i
];
470 memmove(&pcpu_size
[i
], &pcpu_size
[i
+1],
471 (pcpu_num_used
- i
) * sizeof(pcpu_size
[0]));
474 /* Merge with next? */
475 if (i
+1 < pcpu_num_used
&& pcpu_size
[i
+1] >= 0) {
476 pcpu_size
[i
] += pcpu_size
[i
+1];
478 memmove(&pcpu_size
[i
+1], &pcpu_size
[i
+2],
479 (pcpu_num_used
- (i
+1)) * sizeof(pcpu_size
[0]));
483 static unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
485 const char *secstrings
)
487 return find_sec(hdr
, sechdrs
, secstrings
, ".data.percpu");
490 static void percpu_modcopy(void *pcpudest
, const void *from
, unsigned long size
)
494 for_each_possible_cpu(cpu
)
495 memcpy(pcpudest
+ per_cpu_offset(cpu
), from
, size
);
498 static int percpu_modinit(void)
501 pcpu_num_allocated
= 2;
502 pcpu_size
= kmalloc(sizeof(pcpu_size
[0]) * pcpu_num_allocated
,
504 /* Static in-kernel percpu data (used). */
505 pcpu_size
[0] = -(__per_cpu_end
-__per_cpu_start
);
507 pcpu_size
[1] = PERCPU_ENOUGH_ROOM
+ pcpu_size
[0];
508 if (pcpu_size
[1] < 0) {
509 printk(KERN_ERR
"No per-cpu room for modules.\n");
515 __initcall(percpu_modinit
);
516 #else /* ... !CONFIG_SMP */
517 static inline void *percpu_modalloc(unsigned long size
, unsigned long align
,
522 static inline void percpu_modfree(void *pcpuptr
)
526 static inline unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
528 const char *secstrings
)
532 static inline void percpu_modcopy(void *pcpudst
, const void *src
,
535 /* pcpusec should be 0, and size of that section should be 0. */
538 #endif /* CONFIG_SMP */
540 #define MODINFO_ATTR(field) \
541 static void setup_modinfo_##field(struct module *mod, const char *s) \
543 mod->field = kstrdup(s, GFP_KERNEL); \
545 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
546 struct module *mod, char *buffer) \
548 return sprintf(buffer, "%s\n", mod->field); \
550 static int modinfo_##field##_exists(struct module *mod) \
552 return mod->field != NULL; \
554 static void free_modinfo_##field(struct module *mod) \
559 static struct module_attribute modinfo_##field = { \
560 .attr = { .name = __stringify(field), .mode = 0444 }, \
561 .show = show_modinfo_##field, \
562 .setup = setup_modinfo_##field, \
563 .test = modinfo_##field##_exists, \
564 .free = free_modinfo_##field, \
567 MODINFO_ATTR(version
);
568 MODINFO_ATTR(srcversion
);
570 static char last_unloaded_module
[MODULE_NAME_LEN
+1];
572 #ifdef CONFIG_MODULE_UNLOAD
573 /* Init the unload section of the module. */
574 static void module_unload_init(struct module
*mod
)
578 INIT_LIST_HEAD(&mod
->modules_which_use_me
);
579 for (i
= 0; i
< NR_CPUS
; i
++)
580 local_set(&mod
->ref
[i
].count
, 0);
581 /* Hold reference count during initialization. */
582 local_set(&mod
->ref
[raw_smp_processor_id()].count
, 1);
583 /* Backwards compatibility macros put refcount during init. */
584 mod
->waiter
= current
;
587 /* modules using other modules */
590 struct list_head list
;
591 struct module
*module_which_uses
;
594 /* Does a already use b? */
595 static int already_uses(struct module
*a
, struct module
*b
)
597 struct module_use
*use
;
599 list_for_each_entry(use
, &b
->modules_which_use_me
, list
) {
600 if (use
->module_which_uses
== a
) {
601 DEBUGP("%s uses %s!\n", a
->name
, b
->name
);
605 DEBUGP("%s does not use %s!\n", a
->name
, b
->name
);
609 /* Module a uses b */
610 static int use_module(struct module
*a
, struct module
*b
)
612 struct module_use
*use
;
615 if (b
== NULL
|| already_uses(a
, b
)) return 1;
617 /* If we're interrupted or time out, we fail. */
618 if (wait_event_interruptible_timeout(
619 module_wq
, (err
= strong_try_module_get(b
)) != -EBUSY
,
621 printk("%s: gave up waiting for init of module %s.\n",
626 /* If strong_try_module_get() returned a different error, we fail. */
630 DEBUGP("Allocating new usage for %s.\n", a
->name
);
631 use
= kmalloc(sizeof(*use
), GFP_ATOMIC
);
633 printk("%s: out of memory loading\n", a
->name
);
638 use
->module_which_uses
= a
;
639 list_add(&use
->list
, &b
->modules_which_use_me
);
640 no_warn
= sysfs_create_link(b
->holders_dir
, &a
->mkobj
.kobj
, a
->name
);
644 /* Clear the unload stuff of the module. */
645 static void module_unload_free(struct module
*mod
)
649 list_for_each_entry(i
, &modules
, list
) {
650 struct module_use
*use
;
652 list_for_each_entry(use
, &i
->modules_which_use_me
, list
) {
653 if (use
->module_which_uses
== mod
) {
654 DEBUGP("%s unusing %s\n", mod
->name
, i
->name
);
656 list_del(&use
->list
);
658 sysfs_remove_link(i
->holders_dir
, mod
->name
);
659 /* There can be at most one match. */
666 #ifdef CONFIG_MODULE_FORCE_UNLOAD
667 static inline int try_force_unload(unsigned int flags
)
669 int ret
= (flags
& O_TRUNC
);
671 add_taint(TAINT_FORCED_RMMOD
);
675 static inline int try_force_unload(unsigned int flags
)
679 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
688 /* Whole machine is stopped with interrupts off when this runs. */
689 static int __try_stop_module(void *_sref
)
691 struct stopref
*sref
= _sref
;
693 /* If it's not unused, quit unless we're forcing. */
694 if (module_refcount(sref
->mod
) != 0) {
695 if (!(*sref
->forced
= try_force_unload(sref
->flags
)))
699 /* Mark it as dying. */
700 sref
->mod
->state
= MODULE_STATE_GOING
;
704 static int try_stop_module(struct module
*mod
, int flags
, int *forced
)
706 if (flags
& O_NONBLOCK
) {
707 struct stopref sref
= { mod
, flags
, forced
};
709 return stop_machine(__try_stop_module
, &sref
, NULL
);
711 /* We don't need to stop the machine for this. */
712 mod
->state
= MODULE_STATE_GOING
;
718 unsigned int module_refcount(struct module
*mod
)
720 unsigned int i
, total
= 0;
722 for (i
= 0; i
< NR_CPUS
; i
++)
723 total
+= local_read(&mod
->ref
[i
].count
);
726 EXPORT_SYMBOL(module_refcount
);
728 /* This exists whether we can unload or not */
729 static void free_module(struct module
*mod
);
731 static void wait_for_zero_refcount(struct module
*mod
)
733 /* Since we might sleep for some time, release the mutex first */
734 mutex_unlock(&module_mutex
);
736 DEBUGP("Looking at refcount...\n");
737 set_current_state(TASK_UNINTERRUPTIBLE
);
738 if (module_refcount(mod
) == 0)
742 current
->state
= TASK_RUNNING
;
743 mutex_lock(&module_mutex
);
747 sys_delete_module(const char __user
*name_user
, unsigned int flags
)
750 char name
[MODULE_NAME_LEN
];
753 if (!capable(CAP_SYS_MODULE
))
756 if (strncpy_from_user(name
, name_user
, MODULE_NAME_LEN
-1) < 0)
758 name
[MODULE_NAME_LEN
-1] = '\0';
760 if (mutex_lock_interruptible(&module_mutex
) != 0)
763 mod
= find_module(name
);
769 if (!list_empty(&mod
->modules_which_use_me
)) {
770 /* Other modules depend on us: get rid of them first. */
775 /* Doing init or already dying? */
776 if (mod
->state
!= MODULE_STATE_LIVE
) {
777 /* FIXME: if (force), slam module count and wake up
779 DEBUGP("%s already dying\n", mod
->name
);
784 /* If it has an init func, it must have an exit func to unload */
785 if (mod
->init
&& !mod
->exit
) {
786 forced
= try_force_unload(flags
);
788 /* This module can't be removed */
794 /* Set this up before setting mod->state */
795 mod
->waiter
= current
;
797 /* Stop the machine so refcounts can't move and disable module. */
798 ret
= try_stop_module(mod
, flags
, &forced
);
802 /* Never wait if forced. */
803 if (!forced
&& module_refcount(mod
) != 0)
804 wait_for_zero_refcount(mod
);
806 mutex_unlock(&module_mutex
);
807 /* Final destruction now noone is using it. */
808 if (mod
->exit
!= NULL
)
810 blocking_notifier_call_chain(&module_notify_list
,
811 MODULE_STATE_GOING
, mod
);
812 mutex_lock(&module_mutex
);
813 /* Store the name of the last unloaded module for diagnostic purposes */
814 strlcpy(last_unloaded_module
, mod
->name
, sizeof(last_unloaded_module
));
815 unregister_dynamic_debug_module(mod
->name
);
819 mutex_unlock(&module_mutex
);
823 static void print_unload_info(struct seq_file
*m
, struct module
*mod
)
825 struct module_use
*use
;
826 int printed_something
= 0;
828 seq_printf(m
, " %u ", module_refcount(mod
));
830 /* Always include a trailing , so userspace can differentiate
831 between this and the old multi-field proc format. */
832 list_for_each_entry(use
, &mod
->modules_which_use_me
, list
) {
833 printed_something
= 1;
834 seq_printf(m
, "%s,", use
->module_which_uses
->name
);
837 if (mod
->init
!= NULL
&& mod
->exit
== NULL
) {
838 printed_something
= 1;
839 seq_printf(m
, "[permanent],");
842 if (!printed_something
)
846 void __symbol_put(const char *symbol
)
848 struct module
*owner
;
851 if (IS_ERR_VALUE(find_symbol(symbol
, &owner
, NULL
, true, false)))
856 EXPORT_SYMBOL(__symbol_put
);
858 void symbol_put_addr(void *addr
)
860 struct module
*modaddr
;
862 if (core_kernel_text((unsigned long)addr
))
865 if (!(modaddr
= module_text_address((unsigned long)addr
)))
869 EXPORT_SYMBOL_GPL(symbol_put_addr
);
871 static ssize_t
show_refcnt(struct module_attribute
*mattr
,
872 struct module
*mod
, char *buffer
)
874 return sprintf(buffer
, "%u\n", module_refcount(mod
));
877 static struct module_attribute refcnt
= {
878 .attr
= { .name
= "refcnt", .mode
= 0444 },
882 void module_put(struct module
*module
)
885 unsigned int cpu
= get_cpu();
886 local_dec(&module
->ref
[cpu
].count
);
887 /* Maybe they're waiting for us to drop reference? */
888 if (unlikely(!module_is_live(module
)))
889 wake_up_process(module
->waiter
);
893 EXPORT_SYMBOL(module_put
);
895 #else /* !CONFIG_MODULE_UNLOAD */
896 static void print_unload_info(struct seq_file
*m
, struct module
*mod
)
898 /* We don't know the usage count, or what modules are using. */
899 seq_printf(m
, " - -");
902 static inline void module_unload_free(struct module
*mod
)
906 static inline int use_module(struct module
*a
, struct module
*b
)
908 return strong_try_module_get(b
) == 0;
911 static inline void module_unload_init(struct module
*mod
)
914 #endif /* CONFIG_MODULE_UNLOAD */
916 static ssize_t
show_initstate(struct module_attribute
*mattr
,
917 struct module
*mod
, char *buffer
)
919 const char *state
= "unknown";
921 switch (mod
->state
) {
922 case MODULE_STATE_LIVE
:
925 case MODULE_STATE_COMING
:
928 case MODULE_STATE_GOING
:
932 return sprintf(buffer
, "%s\n", state
);
935 static struct module_attribute initstate
= {
936 .attr
= { .name
= "initstate", .mode
= 0444 },
937 .show
= show_initstate
,
940 static struct module_attribute
*modinfo_attrs
[] = {
944 #ifdef CONFIG_MODULE_UNLOAD
950 static const char vermagic
[] = VERMAGIC_STRING
;
952 static int try_to_force_load(struct module
*mod
, const char *symname
)
954 #ifdef CONFIG_MODULE_FORCE_LOAD
955 if (!test_taint(TAINT_FORCED_MODULE
))
956 printk("%s: no version for \"%s\" found: kernel tainted.\n",
958 add_taint_module(mod
, TAINT_FORCED_MODULE
);
965 #ifdef CONFIG_MODVERSIONS
966 static int check_version(Elf_Shdr
*sechdrs
,
967 unsigned int versindex
,
970 const unsigned long *crc
)
972 unsigned int i
, num_versions
;
973 struct modversion_info
*versions
;
975 /* Exporting module didn't supply crcs? OK, we're already tainted. */
979 /* No versions at all? modprobe --force does this. */
981 return try_to_force_load(mod
, symname
) == 0;
983 versions
= (void *) sechdrs
[versindex
].sh_addr
;
984 num_versions
= sechdrs
[versindex
].sh_size
985 / sizeof(struct modversion_info
);
987 for (i
= 0; i
< num_versions
; i
++) {
988 if (strcmp(versions
[i
].name
, symname
) != 0)
991 if (versions
[i
].crc
== *crc
)
993 DEBUGP("Found checksum %lX vs module %lX\n",
994 *crc
, versions
[i
].crc
);
998 printk(KERN_WARNING
"%s: no symbol version for %s\n",
1003 printk("%s: disagrees about version of symbol %s\n",
1004 mod
->name
, symname
);
1008 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1009 unsigned int versindex
,
1012 const unsigned long *crc
;
1014 if (IS_ERR_VALUE(find_symbol("struct_module", NULL
, &crc
, true, false)))
1016 return check_version(sechdrs
, versindex
, "struct_module", mod
, crc
);
1019 /* First part is kernel version, which we ignore if module has crcs. */
1020 static inline int same_magic(const char *amagic
, const char *bmagic
,
1024 amagic
+= strcspn(amagic
, " ");
1025 bmagic
+= strcspn(bmagic
, " ");
1027 return strcmp(amagic
, bmagic
) == 0;
1030 static inline int check_version(Elf_Shdr
*sechdrs
,
1031 unsigned int versindex
,
1032 const char *symname
,
1034 const unsigned long *crc
)
1039 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1040 unsigned int versindex
,
1046 static inline int same_magic(const char *amagic
, const char *bmagic
,
1049 return strcmp(amagic
, bmagic
) == 0;
1051 #endif /* CONFIG_MODVERSIONS */
1053 /* Resolve a symbol for this module. I.e. if we find one, record usage.
1054 Must be holding module_mutex. */
1055 static unsigned long resolve_symbol(Elf_Shdr
*sechdrs
,
1056 unsigned int versindex
,
1060 struct module
*owner
;
1062 const unsigned long *crc
;
1064 ret
= find_symbol(name
, &owner
, &crc
,
1065 !(mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
)), true);
1066 if (!IS_ERR_VALUE(ret
)) {
1067 /* use_module can fail due to OOM,
1068 or module initialization or unloading */
1069 if (!check_version(sechdrs
, versindex
, name
, mod
, crc
) ||
1070 !use_module(mod
, owner
))
1077 * /sys/module/foo/sections stuff
1078 * J. Corbet <corbet@lwn.net>
1080 #if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
1081 struct module_sect_attr
1083 struct module_attribute mattr
;
1085 unsigned long address
;
1088 struct module_sect_attrs
1090 struct attribute_group grp
;
1091 unsigned int nsections
;
1092 struct module_sect_attr attrs
[0];
1095 static ssize_t
module_sect_show(struct module_attribute
*mattr
,
1096 struct module
*mod
, char *buf
)
1098 struct module_sect_attr
*sattr
=
1099 container_of(mattr
, struct module_sect_attr
, mattr
);
1100 return sprintf(buf
, "0x%lx\n", sattr
->address
);
1103 static void free_sect_attrs(struct module_sect_attrs
*sect_attrs
)
1105 unsigned int section
;
1107 for (section
= 0; section
< sect_attrs
->nsections
; section
++)
1108 kfree(sect_attrs
->attrs
[section
].name
);
1112 static void add_sect_attrs(struct module
*mod
, unsigned int nsect
,
1113 char *secstrings
, Elf_Shdr
*sechdrs
)
1115 unsigned int nloaded
= 0, i
, size
[2];
1116 struct module_sect_attrs
*sect_attrs
;
1117 struct module_sect_attr
*sattr
;
1118 struct attribute
**gattr
;
1120 /* Count loaded sections and allocate structures */
1121 for (i
= 0; i
< nsect
; i
++)
1122 if (sechdrs
[i
].sh_flags
& SHF_ALLOC
)
1124 size
[0] = ALIGN(sizeof(*sect_attrs
)
1125 + nloaded
* sizeof(sect_attrs
->attrs
[0]),
1126 sizeof(sect_attrs
->grp
.attrs
[0]));
1127 size
[1] = (nloaded
+ 1) * sizeof(sect_attrs
->grp
.attrs
[0]);
1128 sect_attrs
= kzalloc(size
[0] + size
[1], GFP_KERNEL
);
1129 if (sect_attrs
== NULL
)
1132 /* Setup section attributes. */
1133 sect_attrs
->grp
.name
= "sections";
1134 sect_attrs
->grp
.attrs
= (void *)sect_attrs
+ size
[0];
1136 sect_attrs
->nsections
= 0;
1137 sattr
= §_attrs
->attrs
[0];
1138 gattr
= §_attrs
->grp
.attrs
[0];
1139 for (i
= 0; i
< nsect
; i
++) {
1140 if (! (sechdrs
[i
].sh_flags
& SHF_ALLOC
))
1142 sattr
->address
= sechdrs
[i
].sh_addr
;
1143 sattr
->name
= kstrdup(secstrings
+ sechdrs
[i
].sh_name
,
1145 if (sattr
->name
== NULL
)
1147 sect_attrs
->nsections
++;
1148 sattr
->mattr
.show
= module_sect_show
;
1149 sattr
->mattr
.store
= NULL
;
1150 sattr
->mattr
.attr
.name
= sattr
->name
;
1151 sattr
->mattr
.attr
.mode
= S_IRUGO
;
1152 *(gattr
++) = &(sattr
++)->mattr
.attr
;
1156 if (sysfs_create_group(&mod
->mkobj
.kobj
, §_attrs
->grp
))
1159 mod
->sect_attrs
= sect_attrs
;
1162 free_sect_attrs(sect_attrs
);
1165 static void remove_sect_attrs(struct module
*mod
)
1167 if (mod
->sect_attrs
) {
1168 sysfs_remove_group(&mod
->mkobj
.kobj
,
1169 &mod
->sect_attrs
->grp
);
1170 /* We are positive that no one is using any sect attrs
1171 * at this point. Deallocate immediately. */
1172 free_sect_attrs(mod
->sect_attrs
);
1173 mod
->sect_attrs
= NULL
;
1178 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1181 struct module_notes_attrs
{
1182 struct kobject
*dir
;
1184 struct bin_attribute attrs
[0];
1187 static ssize_t
module_notes_read(struct kobject
*kobj
,
1188 struct bin_attribute
*bin_attr
,
1189 char *buf
, loff_t pos
, size_t count
)
1192 * The caller checked the pos and count against our size.
1194 memcpy(buf
, bin_attr
->private + pos
, count
);
1198 static void free_notes_attrs(struct module_notes_attrs
*notes_attrs
,
1201 if (notes_attrs
->dir
) {
1203 sysfs_remove_bin_file(notes_attrs
->dir
,
1204 ¬es_attrs
->attrs
[i
]);
1205 kobject_put(notes_attrs
->dir
);
1210 static void add_notes_attrs(struct module
*mod
, unsigned int nsect
,
1211 char *secstrings
, Elf_Shdr
*sechdrs
)
1213 unsigned int notes
, loaded
, i
;
1214 struct module_notes_attrs
*notes_attrs
;
1215 struct bin_attribute
*nattr
;
1217 /* Count notes sections and allocate structures. */
1219 for (i
= 0; i
< nsect
; i
++)
1220 if ((sechdrs
[i
].sh_flags
& SHF_ALLOC
) &&
1221 (sechdrs
[i
].sh_type
== SHT_NOTE
))
1227 notes_attrs
= kzalloc(sizeof(*notes_attrs
)
1228 + notes
* sizeof(notes_attrs
->attrs
[0]),
1230 if (notes_attrs
== NULL
)
1233 notes_attrs
->notes
= notes
;
1234 nattr
= ¬es_attrs
->attrs
[0];
1235 for (loaded
= i
= 0; i
< nsect
; ++i
) {
1236 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
1238 if (sechdrs
[i
].sh_type
== SHT_NOTE
) {
1239 nattr
->attr
.name
= mod
->sect_attrs
->attrs
[loaded
].name
;
1240 nattr
->attr
.mode
= S_IRUGO
;
1241 nattr
->size
= sechdrs
[i
].sh_size
;
1242 nattr
->private = (void *) sechdrs
[i
].sh_addr
;
1243 nattr
->read
= module_notes_read
;
1249 notes_attrs
->dir
= kobject_create_and_add("notes", &mod
->mkobj
.kobj
);
1250 if (!notes_attrs
->dir
)
1253 for (i
= 0; i
< notes
; ++i
)
1254 if (sysfs_create_bin_file(notes_attrs
->dir
,
1255 ¬es_attrs
->attrs
[i
]))
1258 mod
->notes_attrs
= notes_attrs
;
1262 free_notes_attrs(notes_attrs
, i
);
1265 static void remove_notes_attrs(struct module
*mod
)
1267 if (mod
->notes_attrs
)
1268 free_notes_attrs(mod
->notes_attrs
, mod
->notes_attrs
->notes
);
1273 static inline void add_sect_attrs(struct module
*mod
, unsigned int nsect
,
1274 char *sectstrings
, Elf_Shdr
*sechdrs
)
1278 static inline void remove_sect_attrs(struct module
*mod
)
1282 static inline void add_notes_attrs(struct module
*mod
, unsigned int nsect
,
1283 char *sectstrings
, Elf_Shdr
*sechdrs
)
1287 static inline void remove_notes_attrs(struct module
*mod
)
1293 int module_add_modinfo_attrs(struct module
*mod
)
1295 struct module_attribute
*attr
;
1296 struct module_attribute
*temp_attr
;
1300 mod
->modinfo_attrs
= kzalloc((sizeof(struct module_attribute
) *
1301 (ARRAY_SIZE(modinfo_attrs
) + 1)),
1303 if (!mod
->modinfo_attrs
)
1306 temp_attr
= mod
->modinfo_attrs
;
1307 for (i
= 0; (attr
= modinfo_attrs
[i
]) && !error
; i
++) {
1309 (attr
->test
&& attr
->test(mod
))) {
1310 memcpy(temp_attr
, attr
, sizeof(*temp_attr
));
1311 error
= sysfs_create_file(&mod
->mkobj
.kobj
,&temp_attr
->attr
);
1318 void module_remove_modinfo_attrs(struct module
*mod
)
1320 struct module_attribute
*attr
;
1323 for (i
= 0; (attr
= &mod
->modinfo_attrs
[i
]); i
++) {
1324 /* pick a field to test for end of list */
1325 if (!attr
->attr
.name
)
1327 sysfs_remove_file(&mod
->mkobj
.kobj
,&attr
->attr
);
1331 kfree(mod
->modinfo_attrs
);
1334 int mod_sysfs_init(struct module
*mod
)
1337 struct kobject
*kobj
;
1339 if (!module_sysfs_initialized
) {
1340 printk(KERN_ERR
"%s: module sysfs not initialized\n",
1346 kobj
= kset_find_obj(module_kset
, mod
->name
);
1348 printk(KERN_ERR
"%s: module is already loaded\n", mod
->name
);
1354 mod
->mkobj
.mod
= mod
;
1356 memset(&mod
->mkobj
.kobj
, 0, sizeof(mod
->mkobj
.kobj
));
1357 mod
->mkobj
.kobj
.kset
= module_kset
;
1358 err
= kobject_init_and_add(&mod
->mkobj
.kobj
, &module_ktype
, NULL
,
1361 kobject_put(&mod
->mkobj
.kobj
);
1363 /* delay uevent until full sysfs population */
1368 int mod_sysfs_setup(struct module
*mod
,
1369 struct kernel_param
*kparam
,
1370 unsigned int num_params
)
1374 mod
->holders_dir
= kobject_create_and_add("holders", &mod
->mkobj
.kobj
);
1375 if (!mod
->holders_dir
) {
1380 err
= module_param_sysfs_setup(mod
, kparam
, num_params
);
1382 goto out_unreg_holders
;
1384 err
= module_add_modinfo_attrs(mod
);
1386 goto out_unreg_param
;
1388 kobject_uevent(&mod
->mkobj
.kobj
, KOBJ_ADD
);
1392 module_param_sysfs_remove(mod
);
1394 kobject_put(mod
->holders_dir
);
1396 kobject_put(&mod
->mkobj
.kobj
);
1400 static void mod_sysfs_fini(struct module
*mod
)
1402 kobject_put(&mod
->mkobj
.kobj
);
1405 #else /* CONFIG_SYSFS */
1407 static void mod_sysfs_fini(struct module
*mod
)
1411 #endif /* CONFIG_SYSFS */
1413 static void mod_kobject_remove(struct module
*mod
)
1415 module_remove_modinfo_attrs(mod
);
1416 module_param_sysfs_remove(mod
);
1417 kobject_put(mod
->mkobj
.drivers_dir
);
1418 kobject_put(mod
->holders_dir
);
1419 mod_sysfs_fini(mod
);
1423 * unlink the module with the whole machine is stopped with interrupts off
1424 * - this defends against kallsyms not taking locks
1426 static int __unlink_module(void *_mod
)
1428 struct module
*mod
= _mod
;
1429 list_del(&mod
->list
);
1433 /* Free a module, remove from lists, etc (must hold module_mutex). */
1434 static void free_module(struct module
*mod
)
1436 /* Delete from various lists */
1437 stop_machine(__unlink_module
, mod
, NULL
);
1438 remove_notes_attrs(mod
);
1439 remove_sect_attrs(mod
);
1440 mod_kobject_remove(mod
);
1442 unwind_remove_table(mod
->unwind_info
, 0);
1444 /* Arch-specific cleanup. */
1445 module_arch_cleanup(mod
);
1447 /* Module unload stuff */
1448 module_unload_free(mod
);
1450 /* release any pointers to mcount in this module */
1451 ftrace_release(mod
->module_core
, mod
->core_size
);
1453 /* This may be NULL, but that's OK */
1454 module_free(mod
, mod
->module_init
);
1457 percpu_modfree(mod
->percpu
);
1459 /* Free lock-classes: */
1460 lockdep_free_key_range(mod
->module_core
, mod
->core_size
);
1462 /* Finally, free the core (containing the module structure) */
1463 module_free(mod
, mod
->module_core
);
1466 void *__symbol_get(const char *symbol
)
1468 struct module
*owner
;
1469 unsigned long value
;
1472 value
= find_symbol(symbol
, &owner
, NULL
, true, true);
1473 if (IS_ERR_VALUE(value
))
1475 else if (strong_try_module_get(owner
))
1479 return (void *)value
;
1481 EXPORT_SYMBOL_GPL(__symbol_get
);
1484 * Ensure that an exported symbol [global namespace] does not already exist
1485 * in the kernel or in some other module's exported symbol table.
1487 static int verify_export_symbols(struct module
*mod
)
1490 struct module
*owner
;
1491 const struct kernel_symbol
*s
;
1493 const struct kernel_symbol
*sym
;
1496 { mod
->syms
, mod
->num_syms
},
1497 { mod
->gpl_syms
, mod
->num_gpl_syms
},
1498 { mod
->gpl_future_syms
, mod
->num_gpl_future_syms
},
1499 #ifdef CONFIG_UNUSED_SYMBOLS
1500 { mod
->unused_syms
, mod
->num_unused_syms
},
1501 { mod
->unused_gpl_syms
, mod
->num_unused_gpl_syms
},
1505 for (i
= 0; i
< ARRAY_SIZE(arr
); i
++) {
1506 for (s
= arr
[i
].sym
; s
< arr
[i
].sym
+ arr
[i
].num
; s
++) {
1507 if (!IS_ERR_VALUE(find_symbol(s
->name
, &owner
,
1508 NULL
, true, false))) {
1510 "%s: exports duplicate symbol %s"
1512 mod
->name
, s
->name
, module_name(owner
));
1520 /* Change all symbols so that st_value encodes the pointer directly. */
1521 static int simplify_symbols(Elf_Shdr
*sechdrs
,
1522 unsigned int symindex
,
1524 unsigned int versindex
,
1525 unsigned int pcpuindex
,
1528 Elf_Sym
*sym
= (void *)sechdrs
[symindex
].sh_addr
;
1529 unsigned long secbase
;
1530 unsigned int i
, n
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1533 for (i
= 1; i
< n
; i
++) {
1534 switch (sym
[i
].st_shndx
) {
1536 /* We compiled with -fno-common. These are not
1537 supposed to happen. */
1538 DEBUGP("Common symbol: %s\n", strtab
+ sym
[i
].st_name
);
1539 printk("%s: please compile with -fno-common\n",
1545 /* Don't need to do anything */
1546 DEBUGP("Absolute symbol: 0x%08lx\n",
1547 (long)sym
[i
].st_value
);
1552 = resolve_symbol(sechdrs
, versindex
,
1553 strtab
+ sym
[i
].st_name
, mod
);
1555 /* Ok if resolved. */
1556 if (!IS_ERR_VALUE(sym
[i
].st_value
))
1559 if (ELF_ST_BIND(sym
[i
].st_info
) == STB_WEAK
)
1562 printk(KERN_WARNING
"%s: Unknown symbol %s\n",
1563 mod
->name
, strtab
+ sym
[i
].st_name
);
1568 /* Divert to percpu allocation if a percpu var. */
1569 if (sym
[i
].st_shndx
== pcpuindex
)
1570 secbase
= (unsigned long)mod
->percpu
;
1572 secbase
= sechdrs
[sym
[i
].st_shndx
].sh_addr
;
1573 sym
[i
].st_value
+= secbase
;
1581 /* Update size with this section: return offset. */
1582 static long get_offset(unsigned int *size
, Elf_Shdr
*sechdr
)
1586 ret
= ALIGN(*size
, sechdr
->sh_addralign
?: 1);
1587 *size
= ret
+ sechdr
->sh_size
;
1591 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1592 might -- code, read-only data, read-write data, small data. Tally
1593 sizes, and place the offsets into sh_entsize fields: high bit means it
1595 static void layout_sections(struct module
*mod
,
1596 const Elf_Ehdr
*hdr
,
1598 const char *secstrings
)
1600 static unsigned long const masks
[][2] = {
1601 /* NOTE: all executable code must be the first section
1602 * in this array; otherwise modify the text_size
1603 * finder in the two loops below */
1604 { SHF_EXECINSTR
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1605 { SHF_ALLOC
, SHF_WRITE
| ARCH_SHF_SMALL
},
1606 { SHF_WRITE
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1607 { ARCH_SHF_SMALL
| SHF_ALLOC
, 0 }
1611 for (i
= 0; i
< hdr
->e_shnum
; i
++)
1612 sechdrs
[i
].sh_entsize
= ~0UL;
1614 DEBUGP("Core section allocation order:\n");
1615 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1616 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1617 Elf_Shdr
*s
= &sechdrs
[i
];
1619 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1620 || (s
->sh_flags
& masks
[m
][1])
1621 || s
->sh_entsize
!= ~0UL
1622 || strncmp(secstrings
+ s
->sh_name
,
1625 s
->sh_entsize
= get_offset(&mod
->core_size
, s
);
1626 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1629 mod
->core_text_size
= mod
->core_size
;
1632 DEBUGP("Init section allocation order:\n");
1633 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1634 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1635 Elf_Shdr
*s
= &sechdrs
[i
];
1637 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1638 || (s
->sh_flags
& masks
[m
][1])
1639 || s
->sh_entsize
!= ~0UL
1640 || strncmp(secstrings
+ s
->sh_name
,
1643 s
->sh_entsize
= (get_offset(&mod
->init_size
, s
)
1644 | INIT_OFFSET_MASK
);
1645 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1648 mod
->init_text_size
= mod
->init_size
;
1652 static void set_license(struct module
*mod
, const char *license
)
1655 license
= "unspecified";
1657 if (!license_is_gpl_compatible(license
)) {
1658 if (!test_taint(TAINT_PROPRIETARY_MODULE
))
1659 printk(KERN_WARNING
"%s: module license '%s' taints "
1660 "kernel.\n", mod
->name
, license
);
1661 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
1665 /* Parse tag=value strings from .modinfo section */
1666 static char *next_string(char *string
, unsigned long *secsize
)
1668 /* Skip non-zero chars */
1671 if ((*secsize
)-- <= 1)
1675 /* Skip any zero padding. */
1676 while (!string
[0]) {
1678 if ((*secsize
)-- <= 1)
1684 static char *get_modinfo(Elf_Shdr
*sechdrs
,
1689 unsigned int taglen
= strlen(tag
);
1690 unsigned long size
= sechdrs
[info
].sh_size
;
1692 for (p
= (char *)sechdrs
[info
].sh_addr
; p
; p
= next_string(p
, &size
)) {
1693 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
1694 return p
+ taglen
+ 1;
1699 static void setup_modinfo(struct module
*mod
, Elf_Shdr
*sechdrs
,
1700 unsigned int infoindex
)
1702 struct module_attribute
*attr
;
1705 for (i
= 0; (attr
= modinfo_attrs
[i
]); i
++) {
1708 get_modinfo(sechdrs
,
1714 #ifdef CONFIG_KALLSYMS
1716 /* lookup symbol in given range of kernel_symbols */
1717 static const struct kernel_symbol
*lookup_symbol(const char *name
,
1718 const struct kernel_symbol
*start
,
1719 const struct kernel_symbol
*stop
)
1721 const struct kernel_symbol
*ks
= start
;
1722 for (; ks
< stop
; ks
++)
1723 if (strcmp(ks
->name
, name
) == 0)
1728 static int is_exported(const char *name
, const struct module
*mod
)
1730 if (!mod
&& lookup_symbol(name
, __start___ksymtab
, __stop___ksymtab
))
1733 if (mod
&& lookup_symbol(name
, mod
->syms
, mod
->syms
+ mod
->num_syms
))
1740 static char elf_type(const Elf_Sym
*sym
,
1742 const char *secstrings
,
1745 if (ELF_ST_BIND(sym
->st_info
) == STB_WEAK
) {
1746 if (ELF_ST_TYPE(sym
->st_info
) == STT_OBJECT
)
1751 if (sym
->st_shndx
== SHN_UNDEF
)
1753 if (sym
->st_shndx
== SHN_ABS
)
1755 if (sym
->st_shndx
>= SHN_LORESERVE
)
1757 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_EXECINSTR
)
1759 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_ALLOC
1760 && sechdrs
[sym
->st_shndx
].sh_type
!= SHT_NOBITS
) {
1761 if (!(sechdrs
[sym
->st_shndx
].sh_flags
& SHF_WRITE
))
1763 else if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1768 if (sechdrs
[sym
->st_shndx
].sh_type
== SHT_NOBITS
) {
1769 if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1774 if (strncmp(secstrings
+ sechdrs
[sym
->st_shndx
].sh_name
,
1775 ".debug", strlen(".debug")) == 0)
1780 static void add_kallsyms(struct module
*mod
,
1782 unsigned int symindex
,
1783 unsigned int strindex
,
1784 const char *secstrings
)
1788 mod
->symtab
= (void *)sechdrs
[symindex
].sh_addr
;
1789 mod
->num_symtab
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1790 mod
->strtab
= (void *)sechdrs
[strindex
].sh_addr
;
1792 /* Set types up while we still have access to sections. */
1793 for (i
= 0; i
< mod
->num_symtab
; i
++)
1794 mod
->symtab
[i
].st_info
1795 = elf_type(&mod
->symtab
[i
], sechdrs
, secstrings
, mod
);
1798 static inline void add_kallsyms(struct module
*mod
,
1800 unsigned int symindex
,
1801 unsigned int strindex
,
1802 const char *secstrings
)
1805 #endif /* CONFIG_KALLSYMS */
1807 static void dynamic_printk_setup(struct mod_debug
*debug
, unsigned int num
)
1809 #ifdef CONFIG_DYNAMIC_PRINTK_DEBUG
1812 for (i
= 0; i
< num
; i
++) {
1813 register_dynamic_debug_module(debug
[i
].modname
,
1815 debug
[i
].logical_modname
,
1816 debug
[i
].flag_names
,
1817 debug
[i
].hash
, debug
[i
].hash2
);
1819 #endif /* CONFIG_DYNAMIC_PRINTK_DEBUG */
1822 static void *module_alloc_update_bounds(unsigned long size
)
1824 void *ret
= module_alloc(size
);
1827 /* Update module bounds. */
1828 if ((unsigned long)ret
< module_addr_min
)
1829 module_addr_min
= (unsigned long)ret
;
1830 if ((unsigned long)ret
+ size
> module_addr_max
)
1831 module_addr_max
= (unsigned long)ret
+ size
;
1836 /* Allocate and load the module: note that size of section 0 is always
1837 zero, and we rely on this for optional sections. */
1838 static noinline
struct module
*load_module(void __user
*umod
,
1840 const char __user
*uargs
)
1844 char *secstrings
, *args
, *modmagic
, *strtab
= NULL
;
1847 unsigned int symindex
= 0;
1848 unsigned int strindex
= 0;
1849 unsigned int modindex
, versindex
, infoindex
, pcpuindex
;
1850 unsigned int unwindex
= 0;
1851 unsigned int num_kp
, num_mcount
;
1852 struct kernel_param
*kp
;
1855 void *percpu
= NULL
, *ptr
= NULL
; /* Stops spurious gcc warning */
1856 unsigned long *mseg
;
1857 mm_segment_t old_fs
;
1859 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1861 if (len
< sizeof(*hdr
))
1862 return ERR_PTR(-ENOEXEC
);
1864 /* Suck in entire file: we'll want most of it. */
1865 /* vmalloc barfs on "unusual" numbers. Check here */
1866 if (len
> 64 * 1024 * 1024 || (hdr
= vmalloc(len
)) == NULL
)
1867 return ERR_PTR(-ENOMEM
);
1868 if (copy_from_user(hdr
, umod
, len
) != 0) {
1873 /* Sanity checks against insmoding binaries or wrong arch,
1874 weird elf version */
1875 if (memcmp(hdr
->e_ident
, ELFMAG
, SELFMAG
) != 0
1876 || hdr
->e_type
!= ET_REL
1877 || !elf_check_arch(hdr
)
1878 || hdr
->e_shentsize
!= sizeof(*sechdrs
)) {
1883 if (len
< hdr
->e_shoff
+ hdr
->e_shnum
* sizeof(Elf_Shdr
))
1886 /* Convenience variables */
1887 sechdrs
= (void *)hdr
+ hdr
->e_shoff
;
1888 secstrings
= (void *)hdr
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
1889 sechdrs
[0].sh_addr
= 0;
1891 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
1892 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
1893 && len
< sechdrs
[i
].sh_offset
+ sechdrs
[i
].sh_size
)
1896 /* Mark all sections sh_addr with their address in the
1898 sechdrs
[i
].sh_addr
= (size_t)hdr
+ sechdrs
[i
].sh_offset
;
1900 /* Internal symbols and strings. */
1901 if (sechdrs
[i
].sh_type
== SHT_SYMTAB
) {
1903 strindex
= sechdrs
[i
].sh_link
;
1904 strtab
= (char *)hdr
+ sechdrs
[strindex
].sh_offset
;
1906 #ifndef CONFIG_MODULE_UNLOAD
1907 /* Don't load .exit sections */
1908 if (strncmp(secstrings
+sechdrs
[i
].sh_name
, ".exit", 5) == 0)
1909 sechdrs
[i
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1913 modindex
= find_sec(hdr
, sechdrs
, secstrings
,
1914 ".gnu.linkonce.this_module");
1916 printk(KERN_WARNING
"No module found in object\n");
1920 /* This is temporary: point mod into copy of data. */
1921 mod
= (void *)sechdrs
[modindex
].sh_addr
;
1923 if (symindex
== 0) {
1924 printk(KERN_WARNING
"%s: module has no symbols (stripped?)\n",
1930 versindex
= find_sec(hdr
, sechdrs
, secstrings
, "__versions");
1931 infoindex
= find_sec(hdr
, sechdrs
, secstrings
, ".modinfo");
1932 pcpuindex
= find_pcpusec(hdr
, sechdrs
, secstrings
);
1933 #ifdef ARCH_UNWIND_SECTION_NAME
1934 unwindex
= find_sec(hdr
, sechdrs
, secstrings
, ARCH_UNWIND_SECTION_NAME
);
1937 /* Don't keep modinfo and version sections. */
1938 sechdrs
[infoindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1939 sechdrs
[versindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1940 #ifdef CONFIG_KALLSYMS
1941 /* Keep symbol and string tables for decoding later. */
1942 sechdrs
[symindex
].sh_flags
|= SHF_ALLOC
;
1943 sechdrs
[strindex
].sh_flags
|= SHF_ALLOC
;
1946 sechdrs
[unwindex
].sh_flags
|= SHF_ALLOC
;
1948 /* Check module struct version now, before we try to use module. */
1949 if (!check_modstruct_version(sechdrs
, versindex
, mod
)) {
1954 modmagic
= get_modinfo(sechdrs
, infoindex
, "vermagic");
1955 /* This is allowed: modprobe --force will invalidate it. */
1957 err
= try_to_force_load(mod
, "magic");
1960 } else if (!same_magic(modmagic
, vermagic
, versindex
)) {
1961 printk(KERN_ERR
"%s: version magic '%s' should be '%s'\n",
1962 mod
->name
, modmagic
, vermagic
);
1967 staging
= get_modinfo(sechdrs
, infoindex
, "staging");
1969 add_taint_module(mod
, TAINT_CRAP
);
1970 printk(KERN_WARNING
"%s: module is from the staging directory,"
1971 " the quality is unknown, you have been warned.\n",
1975 /* Now copy in args */
1976 args
= strndup_user(uargs
, ~0UL >> 1);
1978 err
= PTR_ERR(args
);
1982 if (find_module(mod
->name
)) {
1987 mod
->state
= MODULE_STATE_COMING
;
1989 /* Allow arches to frob section contents and sizes. */
1990 err
= module_frob_arch_sections(hdr
, sechdrs
, secstrings
, mod
);
1995 /* We have a special allocation for this section. */
1996 percpu
= percpu_modalloc(sechdrs
[pcpuindex
].sh_size
,
1997 sechdrs
[pcpuindex
].sh_addralign
,
2003 sechdrs
[pcpuindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2004 mod
->percpu
= percpu
;
2007 /* Determine total sizes, and put offsets in sh_entsize. For now
2008 this is done generically; there doesn't appear to be any
2009 special cases for the architectures. */
2010 layout_sections(mod
, hdr
, sechdrs
, secstrings
);
2012 /* Do the allocs. */
2013 ptr
= module_alloc_update_bounds(mod
->core_size
);
2018 memset(ptr
, 0, mod
->core_size
);
2019 mod
->module_core
= ptr
;
2021 ptr
= module_alloc_update_bounds(mod
->init_size
);
2022 if (!ptr
&& mod
->init_size
) {
2026 memset(ptr
, 0, mod
->init_size
);
2027 mod
->module_init
= ptr
;
2029 /* Transfer each section which specifies SHF_ALLOC */
2030 DEBUGP("final section addresses:\n");
2031 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
2034 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
2037 if (sechdrs
[i
].sh_entsize
& INIT_OFFSET_MASK
)
2038 dest
= mod
->module_init
2039 + (sechdrs
[i
].sh_entsize
& ~INIT_OFFSET_MASK
);
2041 dest
= mod
->module_core
+ sechdrs
[i
].sh_entsize
;
2043 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
)
2044 memcpy(dest
, (void *)sechdrs
[i
].sh_addr
,
2045 sechdrs
[i
].sh_size
);
2046 /* Update sh_addr to point to copy in image. */
2047 sechdrs
[i
].sh_addr
= (unsigned long)dest
;
2048 DEBUGP("\t0x%lx %s\n", sechdrs
[i
].sh_addr
, secstrings
+ sechdrs
[i
].sh_name
);
2050 /* Module has been moved. */
2051 mod
= (void *)sechdrs
[modindex
].sh_addr
;
2053 /* Now we've moved module, initialize linked lists, etc. */
2054 module_unload_init(mod
);
2056 /* add kobject, so we can reference it. */
2057 err
= mod_sysfs_init(mod
);
2061 /* Set up license info based on the info section */
2062 set_license(mod
, get_modinfo(sechdrs
, infoindex
, "license"));
2065 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2066 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2067 * using GPL-only symbols it needs.
2069 if (strcmp(mod
->name
, "ndiswrapper") == 0)
2070 add_taint(TAINT_PROPRIETARY_MODULE
);
2072 /* driverloader was caught wrongly pretending to be under GPL */
2073 if (strcmp(mod
->name
, "driverloader") == 0)
2074 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
2076 /* Set up MODINFO_ATTR fields */
2077 setup_modinfo(mod
, sechdrs
, infoindex
);
2079 /* Fix up syms, so that st_value is a pointer to location. */
2080 err
= simplify_symbols(sechdrs
, symindex
, strtab
, versindex
, pcpuindex
,
2085 /* Now we've got everything in the final locations, we can
2086 * find optional sections. */
2087 kp
= section_objs(hdr
, sechdrs
, secstrings
, "__param", sizeof(*kp
),
2089 mod
->syms
= section_objs(hdr
, sechdrs
, secstrings
, "__ksymtab",
2090 sizeof(*mod
->syms
), &mod
->num_syms
);
2091 mod
->crcs
= section_addr(hdr
, sechdrs
, secstrings
, "__kcrctab");
2092 mod
->gpl_syms
= section_objs(hdr
, sechdrs
, secstrings
, "__ksymtab_gpl",
2093 sizeof(*mod
->gpl_syms
),
2094 &mod
->num_gpl_syms
);
2095 mod
->gpl_crcs
= section_addr(hdr
, sechdrs
, secstrings
, "__kcrctab_gpl");
2096 mod
->gpl_future_syms
= section_objs(hdr
, sechdrs
, secstrings
,
2097 "__ksymtab_gpl_future",
2098 sizeof(*mod
->gpl_future_syms
),
2099 &mod
->num_gpl_future_syms
);
2100 mod
->gpl_future_crcs
= section_addr(hdr
, sechdrs
, secstrings
,
2101 "__kcrctab_gpl_future");
2103 #ifdef CONFIG_UNUSED_SYMBOLS
2104 mod
->unused_syms
= section_objs(hdr
, sechdrs
, secstrings
,
2106 sizeof(*mod
->unused_syms
),
2107 &mod
->num_unused_syms
);
2108 mod
->unused_crcs
= section_addr(hdr
, sechdrs
, secstrings
,
2109 "__kcrctab_unused");
2110 mod
->unused_gpl_syms
= section_objs(hdr
, sechdrs
, secstrings
,
2111 "__ksymtab_unused_gpl",
2112 sizeof(*mod
->unused_gpl_syms
),
2113 &mod
->num_unused_gpl_syms
);
2114 mod
->unused_gpl_crcs
= section_addr(hdr
, sechdrs
, secstrings
,
2115 "__kcrctab_unused_gpl");
2118 #ifdef CONFIG_MARKERS
2119 mod
->markers
= section_objs(hdr
, sechdrs
, secstrings
, "__markers",
2120 sizeof(*mod
->markers
), &mod
->num_markers
);
2122 #ifdef CONFIG_TRACEPOINTS
2123 mod
->tracepoints
= section_objs(hdr
, sechdrs
, secstrings
,
2125 sizeof(*mod
->tracepoints
),
2126 &mod
->num_tracepoints
);
2129 #ifdef CONFIG_MODVERSIONS
2130 if ((mod
->num_syms
&& !mod
->crcs
)
2131 || (mod
->num_gpl_syms
&& !mod
->gpl_crcs
)
2132 || (mod
->num_gpl_future_syms
&& !mod
->gpl_future_crcs
)
2133 #ifdef CONFIG_UNUSED_SYMBOLS
2134 || (mod
->num_unused_syms
&& !mod
->unused_crcs
)
2135 || (mod
->num_unused_gpl_syms
&& !mod
->unused_gpl_crcs
)
2138 printk(KERN_WARNING
"%s: No versions for exported symbols.\n", mod
->name
);
2139 err
= try_to_force_load(mod
, "nocrc");
2145 /* Now do relocations. */
2146 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
2147 const char *strtab
= (char *)sechdrs
[strindex
].sh_addr
;
2148 unsigned int info
= sechdrs
[i
].sh_info
;
2150 /* Not a valid relocation section? */
2151 if (info
>= hdr
->e_shnum
)
2154 /* Don't bother with non-allocated sections */
2155 if (!(sechdrs
[info
].sh_flags
& SHF_ALLOC
))
2158 if (sechdrs
[i
].sh_type
== SHT_REL
)
2159 err
= apply_relocate(sechdrs
, strtab
, symindex
, i
,mod
);
2160 else if (sechdrs
[i
].sh_type
== SHT_RELA
)
2161 err
= apply_relocate_add(sechdrs
, strtab
, symindex
, i
,
2167 /* Find duplicate symbols */
2168 err
= verify_export_symbols(mod
);
2172 /* Set up and sort exception table */
2173 mod
->extable
= section_objs(hdr
, sechdrs
, secstrings
, "__ex_table",
2174 sizeof(*mod
->extable
), &mod
->num_exentries
);
2175 sort_extable(mod
->extable
, mod
->extable
+ mod
->num_exentries
);
2177 /* Finally, copy percpu area over. */
2178 percpu_modcopy(mod
->percpu
, (void *)sechdrs
[pcpuindex
].sh_addr
,
2179 sechdrs
[pcpuindex
].sh_size
);
2181 add_kallsyms(mod
, sechdrs
, symindex
, strindex
, secstrings
);
2184 struct mod_debug
*debug
;
2185 unsigned int num_debug
;
2187 #ifdef CONFIG_MARKERS
2188 marker_update_probe_range(mod
->markers
,
2189 mod
->markers
+ mod
->num_markers
);
2191 debug
= section_objs(hdr
, sechdrs
, secstrings
, "__verbose",
2192 sizeof(*debug
), &num_debug
);
2193 dynamic_printk_setup(debug
, num_debug
);
2195 #ifdef CONFIG_TRACEPOINTS
2196 tracepoint_update_probe_range(mod
->tracepoints
,
2197 mod
->tracepoints
+ mod
->num_tracepoints
);
2201 /* sechdrs[0].sh_size is always zero */
2202 mseg
= section_objs(hdr
, sechdrs
, secstrings
, "__mcount_loc",
2203 sizeof(*mseg
), &num_mcount
);
2204 ftrace_init_module(mseg
, mseg
+ num_mcount
);
2206 err
= module_finalize(hdr
, sechdrs
, mod
);
2210 /* flush the icache in correct context */
2215 * Flush the instruction cache, since we've played with text.
2216 * Do it before processing of module parameters, so the module
2217 * can provide parameter accessor functions of its own.
2219 if (mod
->module_init
)
2220 flush_icache_range((unsigned long)mod
->module_init
,
2221 (unsigned long)mod
->module_init
2223 flush_icache_range((unsigned long)mod
->module_core
,
2224 (unsigned long)mod
->module_core
+ mod
->core_size
);
2229 if (section_addr(hdr
, sechdrs
, secstrings
, "__obsparm"))
2230 printk(KERN_WARNING
"%s: Ignoring obsolete parameters\n",
2233 /* Now sew it into the lists so we can get lockdep and oops
2234 * info during argument parsing. Noone should access us, since
2235 * strong_try_module_get() will fail.
2236 * lockdep/oops can run asynchronous, so use the RCU list insertion
2237 * function to insert in a way safe to concurrent readers.
2238 * The mutex protects against concurrent writers.
2240 list_add_rcu(&mod
->list
, &modules
);
2242 err
= parse_args(mod
->name
, mod
->args
, kp
, num_kp
, NULL
);
2246 err
= mod_sysfs_setup(mod
, kp
, num_kp
);
2249 add_sect_attrs(mod
, hdr
->e_shnum
, secstrings
, sechdrs
);
2250 add_notes_attrs(mod
, hdr
->e_shnum
, secstrings
, sechdrs
);
2252 /* Size of section 0 is 0, so this works well if no unwind info. */
2253 mod
->unwind_info
= unwind_add_table(mod
,
2254 (void *)sechdrs
[unwindex
].sh_addr
,
2255 sechdrs
[unwindex
].sh_size
);
2257 /* Get rid of temporary copy */
2264 stop_machine(__unlink_module
, mod
, NULL
);
2265 module_arch_cleanup(mod
);
2267 kobject_del(&mod
->mkobj
.kobj
);
2268 kobject_put(&mod
->mkobj
.kobj
);
2269 ftrace_release(mod
->module_core
, mod
->core_size
);
2271 module_unload_free(mod
);
2272 module_free(mod
, mod
->module_init
);
2274 module_free(mod
, mod
->module_core
);
2277 percpu_modfree(percpu
);
2282 return ERR_PTR(err
);
2285 printk(KERN_ERR
"Module len %lu truncated\n", len
);
2290 /* This is where the real work happens */
2292 sys_init_module(void __user
*umod
,
2294 const char __user
*uargs
)
2299 /* Must have permission */
2300 if (!capable(CAP_SYS_MODULE
))
2303 /* Only one module load at a time, please */
2304 if (mutex_lock_interruptible(&module_mutex
) != 0)
2307 /* Do all the hard work */
2308 mod
= load_module(umod
, len
, uargs
);
2310 mutex_unlock(&module_mutex
);
2311 return PTR_ERR(mod
);
2314 /* Drop lock so they can recurse */
2315 mutex_unlock(&module_mutex
);
2317 blocking_notifier_call_chain(&module_notify_list
,
2318 MODULE_STATE_COMING
, mod
);
2320 /* Start the module */
2321 if (mod
->init
!= NULL
)
2322 ret
= do_one_initcall(mod
->init
);
2324 /* Init routine failed: abort. Try to protect us from
2325 buggy refcounters. */
2326 mod
->state
= MODULE_STATE_GOING
;
2327 synchronize_sched();
2329 blocking_notifier_call_chain(&module_notify_list
,
2330 MODULE_STATE_GOING
, mod
);
2331 mutex_lock(&module_mutex
);
2333 mutex_unlock(&module_mutex
);
2334 wake_up(&module_wq
);
2338 printk(KERN_WARNING
"%s: '%s'->init suspiciously returned %d, "
2339 "it should follow 0/-E convention\n"
2340 KERN_WARNING
"%s: loading module anyway...\n",
2341 __func__
, mod
->name
, ret
,
2346 /* Now it's a first class citizen! Wake up anyone waiting for it. */
2347 mod
->state
= MODULE_STATE_LIVE
;
2348 wake_up(&module_wq
);
2350 mutex_lock(&module_mutex
);
2351 /* Drop initial reference. */
2353 unwind_remove_table(mod
->unwind_info
, 1);
2354 module_free(mod
, mod
->module_init
);
2355 mod
->module_init
= NULL
;
2357 mod
->init_text_size
= 0;
2358 mutex_unlock(&module_mutex
);
2363 static inline int within(unsigned long addr
, void *start
, unsigned long size
)
2365 return ((void *)addr
>= start
&& (void *)addr
< start
+ size
);
2368 #ifdef CONFIG_KALLSYMS
2370 * This ignores the intensely annoying "mapping symbols" found
2371 * in ARM ELF files: $a, $t and $d.
2373 static inline int is_arm_mapping_symbol(const char *str
)
2375 return str
[0] == '$' && strchr("atd", str
[1])
2376 && (str
[2] == '\0' || str
[2] == '.');
2379 static const char *get_ksymbol(struct module
*mod
,
2381 unsigned long *size
,
2382 unsigned long *offset
)
2384 unsigned int i
, best
= 0;
2385 unsigned long nextval
;
2387 /* At worse, next value is at end of module */
2388 if (within(addr
, mod
->module_init
, mod
->init_size
))
2389 nextval
= (unsigned long)mod
->module_init
+mod
->init_text_size
;
2391 nextval
= (unsigned long)mod
->module_core
+mod
->core_text_size
;
2393 /* Scan for closest preceeding symbol, and next symbol. (ELF
2394 starts real symbols at 1). */
2395 for (i
= 1; i
< mod
->num_symtab
; i
++) {
2396 if (mod
->symtab
[i
].st_shndx
== SHN_UNDEF
)
2399 /* We ignore unnamed symbols: they're uninformative
2400 * and inserted at a whim. */
2401 if (mod
->symtab
[i
].st_value
<= addr
2402 && mod
->symtab
[i
].st_value
> mod
->symtab
[best
].st_value
2403 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
2404 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
2406 if (mod
->symtab
[i
].st_value
> addr
2407 && mod
->symtab
[i
].st_value
< nextval
2408 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
2409 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
2410 nextval
= mod
->symtab
[i
].st_value
;
2417 *size
= nextval
- mod
->symtab
[best
].st_value
;
2419 *offset
= addr
- mod
->symtab
[best
].st_value
;
2420 return mod
->strtab
+ mod
->symtab
[best
].st_name
;
2423 /* For kallsyms to ask for address resolution. NULL means not found. Careful
2424 * not to lock to avoid deadlock on oopses, simply disable preemption. */
2425 const char *module_address_lookup(unsigned long addr
,
2426 unsigned long *size
,
2427 unsigned long *offset
,
2432 const char *ret
= NULL
;
2435 list_for_each_entry_rcu(mod
, &modules
, list
) {
2436 if (within(addr
, mod
->module_init
, mod
->init_size
)
2437 || within(addr
, mod
->module_core
, mod
->core_size
)) {
2439 *modname
= mod
->name
;
2440 ret
= get_ksymbol(mod
, addr
, size
, offset
);
2444 /* Make a copy in here where it's safe */
2446 strncpy(namebuf
, ret
, KSYM_NAME_LEN
- 1);
2453 int lookup_module_symbol_name(unsigned long addr
, char *symname
)
2458 list_for_each_entry_rcu(mod
, &modules
, list
) {
2459 if (within(addr
, mod
->module_init
, mod
->init_size
) ||
2460 within(addr
, mod
->module_core
, mod
->core_size
)) {
2463 sym
= get_ksymbol(mod
, addr
, NULL
, NULL
);
2466 strlcpy(symname
, sym
, KSYM_NAME_LEN
);
2476 int lookup_module_symbol_attrs(unsigned long addr
, unsigned long *size
,
2477 unsigned long *offset
, char *modname
, char *name
)
2482 list_for_each_entry_rcu(mod
, &modules
, list
) {
2483 if (within(addr
, mod
->module_init
, mod
->init_size
) ||
2484 within(addr
, mod
->module_core
, mod
->core_size
)) {
2487 sym
= get_ksymbol(mod
, addr
, size
, offset
);
2491 strlcpy(modname
, mod
->name
, MODULE_NAME_LEN
);
2493 strlcpy(name
, sym
, KSYM_NAME_LEN
);
2503 int module_get_kallsym(unsigned int symnum
, unsigned long *value
, char *type
,
2504 char *name
, char *module_name
, int *exported
)
2509 list_for_each_entry_rcu(mod
, &modules
, list
) {
2510 if (symnum
< mod
->num_symtab
) {
2511 *value
= mod
->symtab
[symnum
].st_value
;
2512 *type
= mod
->symtab
[symnum
].st_info
;
2513 strlcpy(name
, mod
->strtab
+ mod
->symtab
[symnum
].st_name
,
2515 strlcpy(module_name
, mod
->name
, MODULE_NAME_LEN
);
2516 *exported
= is_exported(name
, mod
);
2520 symnum
-= mod
->num_symtab
;
2526 static unsigned long mod_find_symname(struct module
*mod
, const char *name
)
2530 for (i
= 0; i
< mod
->num_symtab
; i
++)
2531 if (strcmp(name
, mod
->strtab
+mod
->symtab
[i
].st_name
) == 0 &&
2532 mod
->symtab
[i
].st_info
!= 'U')
2533 return mod
->symtab
[i
].st_value
;
2537 /* Look for this name: can be of form module:name. */
2538 unsigned long module_kallsyms_lookup_name(const char *name
)
2542 unsigned long ret
= 0;
2544 /* Don't lock: we're in enough trouble already. */
2546 if ((colon
= strchr(name
, ':')) != NULL
) {
2548 if ((mod
= find_module(name
)) != NULL
)
2549 ret
= mod_find_symname(mod
, colon
+1);
2552 list_for_each_entry_rcu(mod
, &modules
, list
)
2553 if ((ret
= mod_find_symname(mod
, name
)) != 0)
2559 #endif /* CONFIG_KALLSYMS */
2561 static char *module_flags(struct module
*mod
, char *buf
)
2566 mod
->state
== MODULE_STATE_GOING
||
2567 mod
->state
== MODULE_STATE_COMING
) {
2569 if (mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
))
2571 if (mod
->taints
& (1 << TAINT_FORCED_MODULE
))
2573 if (mod
->taints
& (1 << TAINT_CRAP
))
2576 * TAINT_FORCED_RMMOD: could be added.
2577 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2581 /* Show a - for module-is-being-unloaded */
2582 if (mod
->state
== MODULE_STATE_GOING
)
2584 /* Show a + for module-is-being-loaded */
2585 if (mod
->state
== MODULE_STATE_COMING
)
2594 #ifdef CONFIG_PROC_FS
2595 /* Called by the /proc file system to return a list of modules. */
2596 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
2598 mutex_lock(&module_mutex
);
2599 return seq_list_start(&modules
, *pos
);
2602 static void *m_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
2604 return seq_list_next(p
, &modules
, pos
);
2607 static void m_stop(struct seq_file
*m
, void *p
)
2609 mutex_unlock(&module_mutex
);
2612 static int m_show(struct seq_file
*m
, void *p
)
2614 struct module
*mod
= list_entry(p
, struct module
, list
);
2617 seq_printf(m
, "%s %u",
2618 mod
->name
, mod
->init_size
+ mod
->core_size
);
2619 print_unload_info(m
, mod
);
2621 /* Informative for users. */
2622 seq_printf(m
, " %s",
2623 mod
->state
== MODULE_STATE_GOING
? "Unloading":
2624 mod
->state
== MODULE_STATE_COMING
? "Loading":
2626 /* Used by oprofile and other similar tools. */
2627 seq_printf(m
, " 0x%p", mod
->module_core
);
2631 seq_printf(m
, " %s", module_flags(mod
, buf
));
2633 seq_printf(m
, "\n");
2637 /* Format: modulename size refcount deps address
2639 Where refcount is a number or -, and deps is a comma-separated list
2642 static const struct seq_operations modules_op
= {
2649 static int modules_open(struct inode
*inode
, struct file
*file
)
2651 return seq_open(file
, &modules_op
);
2654 static const struct file_operations proc_modules_operations
= {
2655 .open
= modules_open
,
2657 .llseek
= seq_lseek
,
2658 .release
= seq_release
,
2661 static int __init
proc_modules_init(void)
2663 proc_create("modules", 0, NULL
, &proc_modules_operations
);
2666 module_init(proc_modules_init
);
2669 /* Given an address, look for it in the module exception tables. */
2670 const struct exception_table_entry
*search_module_extables(unsigned long addr
)
2672 const struct exception_table_entry
*e
= NULL
;
2676 list_for_each_entry_rcu(mod
, &modules
, list
) {
2677 if (mod
->num_exentries
== 0)
2680 e
= search_extable(mod
->extable
,
2681 mod
->extable
+ mod
->num_exentries
- 1,
2688 /* Now, if we found one, we are running inside it now, hence
2689 we cannot unload the module, hence no refcnt needed. */
2694 * Is this a valid module address?
2696 int is_module_address(unsigned long addr
)
2702 list_for_each_entry_rcu(mod
, &modules
, list
) {
2703 if (within(addr
, mod
->module_core
, mod
->core_size
)) {
2715 /* Is this a valid kernel address? */
2716 struct module
*__module_text_address(unsigned long addr
)
2720 if (addr
< module_addr_min
|| addr
> module_addr_max
)
2723 list_for_each_entry_rcu(mod
, &modules
, list
)
2724 if (within(addr
, mod
->module_init
, mod
->init_text_size
)
2725 || within(addr
, mod
->module_core
, mod
->core_text_size
))
2730 struct module
*module_text_address(unsigned long addr
)
2735 mod
= __module_text_address(addr
);
2741 /* Don't grab lock, we're oopsing. */
2742 void print_modules(void)
2747 printk("Modules linked in:");
2748 /* Most callers should already have preempt disabled, but make sure */
2750 list_for_each_entry_rcu(mod
, &modules
, list
)
2751 printk(" %s%s", mod
->name
, module_flags(mod
, buf
));
2753 if (last_unloaded_module
[0])
2754 printk(" [last unloaded: %s]", last_unloaded_module
);
2758 #ifdef CONFIG_MODVERSIONS
2759 /* Generate the signature for struct module here, too, for modversions. */
2760 void struct_module(struct module
*mod
) { return; }
2761 EXPORT_SYMBOL(struct_module
);
2764 #ifdef CONFIG_MARKERS
2765 void module_update_markers(void)
2769 mutex_lock(&module_mutex
);
2770 list_for_each_entry(mod
, &modules
, list
)
2772 marker_update_probe_range(mod
->markers
,
2773 mod
->markers
+ mod
->num_markers
);
2774 mutex_unlock(&module_mutex
);
2778 #ifdef CONFIG_TRACEPOINTS
2779 void module_update_tracepoints(void)
2783 mutex_lock(&module_mutex
);
2784 list_for_each_entry(mod
, &modules
, list
)
2786 tracepoint_update_probe_range(mod
->tracepoints
,
2787 mod
->tracepoints
+ mod
->num_tracepoints
);
2788 mutex_unlock(&module_mutex
);
2792 * Returns 0 if current not found.
2793 * Returns 1 if current found.
2795 int module_get_iter_tracepoints(struct tracepoint_iter
*iter
)
2797 struct module
*iter_mod
;
2800 mutex_lock(&module_mutex
);
2801 list_for_each_entry(iter_mod
, &modules
, list
) {
2802 if (!iter_mod
->taints
) {
2804 * Sorted module list
2806 if (iter_mod
< iter
->module
)
2808 else if (iter_mod
> iter
->module
)
2809 iter
->tracepoint
= NULL
;
2810 found
= tracepoint_get_iter_range(&iter
->tracepoint
,
2811 iter_mod
->tracepoints
,
2812 iter_mod
->tracepoints
2813 + iter_mod
->num_tracepoints
);
2815 iter
->module
= iter_mod
;
2820 mutex_unlock(&module_mutex
);