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>
53 #include <linux/async.h>
54 #include <linux/percpu.h>
59 #define DEBUGP(fmt , a...)
62 #ifndef ARCH_SHF_SMALL
63 #define ARCH_SHF_SMALL 0
66 /* If this is set, the section belongs in the init part of the module */
67 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
69 /* List of modules, protected by module_mutex or preempt_disable
70 * (delete uses stop_machine/add uses RCU list operations). */
71 static DEFINE_MUTEX(module_mutex
);
72 static LIST_HEAD(modules
);
74 /* Waiting for a module to finish initializing? */
75 static DECLARE_WAIT_QUEUE_HEAD(module_wq
);
77 static BLOCKING_NOTIFIER_HEAD(module_notify_list
);
79 /* Bounds of module allocation, for speeding __module_text_address */
80 static unsigned long module_addr_min
= -1UL, module_addr_max
= 0;
82 int register_module_notifier(struct notifier_block
* nb
)
84 return blocking_notifier_chain_register(&module_notify_list
, nb
);
86 EXPORT_SYMBOL(register_module_notifier
);
88 int unregister_module_notifier(struct notifier_block
* nb
)
90 return blocking_notifier_chain_unregister(&module_notify_list
, nb
);
92 EXPORT_SYMBOL(unregister_module_notifier
);
94 /* We require a truly strong try_module_get(): 0 means failure due to
95 ongoing or failed initialization etc. */
96 static inline int strong_try_module_get(struct module
*mod
)
98 if (mod
&& mod
->state
== MODULE_STATE_COMING
)
100 if (try_module_get(mod
))
106 static inline void add_taint_module(struct module
*mod
, unsigned flag
)
109 mod
->taints
|= (1U << flag
);
113 * A thread that wants to hold a reference to a module only while it
114 * is running can call this to safely exit. nfsd and lockd use this.
116 void __module_put_and_exit(struct module
*mod
, long code
)
121 EXPORT_SYMBOL(__module_put_and_exit
);
123 /* Find a module section: 0 means not found. */
124 static unsigned int find_sec(Elf_Ehdr
*hdr
,
126 const char *secstrings
,
131 for (i
= 1; i
< hdr
->e_shnum
; i
++)
132 /* Alloc bit cleared means "ignore it." */
133 if ((sechdrs
[i
].sh_flags
& SHF_ALLOC
)
134 && strcmp(secstrings
+sechdrs
[i
].sh_name
, name
) == 0)
139 /* Find a module section, or NULL. */
140 static void *section_addr(Elf_Ehdr
*hdr
, Elf_Shdr
*shdrs
,
141 const char *secstrings
, const char *name
)
143 /* Section 0 has sh_addr 0. */
144 return (void *)shdrs
[find_sec(hdr
, shdrs
, secstrings
, name
)].sh_addr
;
147 /* Find a module section, or NULL. Fill in number of "objects" in section. */
148 static void *section_objs(Elf_Ehdr
*hdr
,
150 const char *secstrings
,
155 unsigned int sec
= find_sec(hdr
, sechdrs
, secstrings
, name
);
157 /* Section 0 has sh_addr 0 and sh_size 0. */
158 *num
= sechdrs
[sec
].sh_size
/ object_size
;
159 return (void *)sechdrs
[sec
].sh_addr
;
162 /* Provided by the linker */
163 extern const struct kernel_symbol __start___ksymtab
[];
164 extern const struct kernel_symbol __stop___ksymtab
[];
165 extern const struct kernel_symbol __start___ksymtab_gpl
[];
166 extern const struct kernel_symbol __stop___ksymtab_gpl
[];
167 extern const struct kernel_symbol __start___ksymtab_gpl_future
[];
168 extern const struct kernel_symbol __stop___ksymtab_gpl_future
[];
169 extern const struct kernel_symbol __start___ksymtab_gpl_future
[];
170 extern const struct kernel_symbol __stop___ksymtab_gpl_future
[];
171 extern const unsigned long __start___kcrctab
[];
172 extern const unsigned long __start___kcrctab_gpl
[];
173 extern const unsigned long __start___kcrctab_gpl_future
[];
174 #ifdef CONFIG_UNUSED_SYMBOLS
175 extern const struct kernel_symbol __start___ksymtab_unused
[];
176 extern const struct kernel_symbol __stop___ksymtab_unused
[];
177 extern const struct kernel_symbol __start___ksymtab_unused_gpl
[];
178 extern const struct kernel_symbol __stop___ksymtab_unused_gpl
[];
179 extern const unsigned long __start___kcrctab_unused
[];
180 extern const unsigned long __start___kcrctab_unused_gpl
[];
183 #ifndef CONFIG_MODVERSIONS
184 #define symversion(base, idx) NULL
186 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
190 const struct kernel_symbol
*start
, *stop
;
191 const unsigned long *crcs
;
200 static bool each_symbol_in_section(const struct symsearch
*arr
,
201 unsigned int arrsize
,
202 struct module
*owner
,
203 bool (*fn
)(const struct symsearch
*syms
,
204 struct module
*owner
,
205 unsigned int symnum
, void *data
),
210 for (j
= 0; j
< arrsize
; j
++) {
211 for (i
= 0; i
< arr
[j
].stop
- arr
[j
].start
; i
++)
212 if (fn(&arr
[j
], owner
, i
, data
))
219 /* Returns true as soon as fn returns true, otherwise false. */
220 static bool each_symbol(bool (*fn
)(const struct symsearch
*arr
,
221 struct module
*owner
,
222 unsigned int symnum
, void *data
),
226 const struct symsearch arr
[] = {
227 { __start___ksymtab
, __stop___ksymtab
, __start___kcrctab
,
228 NOT_GPL_ONLY
, false },
229 { __start___ksymtab_gpl
, __stop___ksymtab_gpl
,
230 __start___kcrctab_gpl
,
232 { __start___ksymtab_gpl_future
, __stop___ksymtab_gpl_future
,
233 __start___kcrctab_gpl_future
,
234 WILL_BE_GPL_ONLY
, false },
235 #ifdef CONFIG_UNUSED_SYMBOLS
236 { __start___ksymtab_unused
, __stop___ksymtab_unused
,
237 __start___kcrctab_unused
,
238 NOT_GPL_ONLY
, true },
239 { __start___ksymtab_unused_gpl
, __stop___ksymtab_unused_gpl
,
240 __start___kcrctab_unused_gpl
,
245 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), NULL
, fn
, data
))
248 list_for_each_entry_rcu(mod
, &modules
, list
) {
249 struct symsearch arr
[] = {
250 { mod
->syms
, mod
->syms
+ mod
->num_syms
, mod
->crcs
,
251 NOT_GPL_ONLY
, false },
252 { mod
->gpl_syms
, mod
->gpl_syms
+ mod
->num_gpl_syms
,
255 { mod
->gpl_future_syms
,
256 mod
->gpl_future_syms
+ mod
->num_gpl_future_syms
,
257 mod
->gpl_future_crcs
,
258 WILL_BE_GPL_ONLY
, false },
259 #ifdef CONFIG_UNUSED_SYMBOLS
261 mod
->unused_syms
+ mod
->num_unused_syms
,
263 NOT_GPL_ONLY
, true },
264 { mod
->unused_gpl_syms
,
265 mod
->unused_gpl_syms
+ mod
->num_unused_gpl_syms
,
266 mod
->unused_gpl_crcs
,
271 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), mod
, fn
, data
))
277 struct find_symbol_arg
{
284 struct module
*owner
;
285 const unsigned long *crc
;
289 static bool find_symbol_in_section(const struct symsearch
*syms
,
290 struct module
*owner
,
291 unsigned int symnum
, void *data
)
293 struct find_symbol_arg
*fsa
= data
;
295 if (strcmp(syms
->start
[symnum
].name
, fsa
->name
) != 0)
299 if (syms
->licence
== GPL_ONLY
)
301 if (syms
->licence
== WILL_BE_GPL_ONLY
&& fsa
->warn
) {
302 printk(KERN_WARNING
"Symbol %s is being used "
303 "by a non-GPL module, which will not "
304 "be allowed in the future\n", fsa
->name
);
305 printk(KERN_WARNING
"Please see the file "
306 "Documentation/feature-removal-schedule.txt "
307 "in the kernel source tree for more details.\n");
311 #ifdef CONFIG_UNUSED_SYMBOLS
312 if (syms
->unused
&& fsa
->warn
) {
313 printk(KERN_WARNING
"Symbol %s is marked as UNUSED, "
314 "however this module is using it.\n", fsa
->name
);
316 "This symbol will go away in the future.\n");
318 "Please evalute if this is the right api to use and if "
319 "it really is, submit a report the linux kernel "
320 "mailinglist together with submitting your code for "
326 fsa
->crc
= symversion(syms
->crcs
, symnum
);
327 fsa
->value
= syms
->start
[symnum
].value
;
331 /* Find a symbol, return value, (optional) crc and (optional) module
333 static unsigned long find_symbol(const char *name
,
334 struct module
**owner
,
335 const unsigned long **crc
,
339 struct find_symbol_arg fsa
;
345 if (each_symbol(find_symbol_in_section
, &fsa
)) {
353 DEBUGP("Failed to find symbol %s\n", name
);
357 /* Search for module by name: must hold module_mutex. */
358 static struct module
*find_module(const char *name
)
362 list_for_each_entry(mod
, &modules
, list
) {
363 if (strcmp(mod
->name
, name
) == 0)
371 #ifdef CONFIG_HAVE_DYNAMIC_PER_CPU_AREA
373 static void *percpu_modalloc(unsigned long size
, unsigned long align
,
378 if (align
> PAGE_SIZE
) {
379 printk(KERN_WARNING
"%s: per-cpu alignment %li > %li\n",
380 name
, align
, PAGE_SIZE
);
384 ptr
= __alloc_reserved_percpu(size
, align
);
387 "Could not allocate %lu bytes percpu data\n", size
);
391 static void percpu_modfree(void *freeme
)
396 #else /* ... !CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
398 /* Number of blocks used and allocated. */
399 static unsigned int pcpu_num_used
, pcpu_num_allocated
;
400 /* Size of each block. -ve means used. */
401 static int *pcpu_size
;
403 static int split_block(unsigned int i
, unsigned short size
)
405 /* Reallocation required? */
406 if (pcpu_num_used
+ 1 > pcpu_num_allocated
) {
409 new = krealloc(pcpu_size
, sizeof(new[0])*pcpu_num_allocated
*2,
414 pcpu_num_allocated
*= 2;
418 /* Insert a new subblock */
419 memmove(&pcpu_size
[i
+1], &pcpu_size
[i
],
420 sizeof(pcpu_size
[0]) * (pcpu_num_used
- i
));
423 pcpu_size
[i
+1] -= size
;
428 static inline unsigned int block_size(int val
)
435 static void *percpu_modalloc(unsigned long size
, unsigned long align
,
442 if (align
> PAGE_SIZE
) {
443 printk(KERN_WARNING
"%s: per-cpu alignment %li > %li\n",
444 name
, align
, PAGE_SIZE
);
448 ptr
= __per_cpu_start
;
449 for (i
= 0; i
< pcpu_num_used
; ptr
+= block_size(pcpu_size
[i
]), i
++) {
450 /* Extra for alignment requirement. */
451 extra
= ALIGN((unsigned long)ptr
, align
) - (unsigned long)ptr
;
452 BUG_ON(i
== 0 && extra
!= 0);
454 if (pcpu_size
[i
] < 0 || pcpu_size
[i
] < extra
+ size
)
457 /* Transfer extra to previous block. */
458 if (pcpu_size
[i
-1] < 0)
459 pcpu_size
[i
-1] -= extra
;
461 pcpu_size
[i
-1] += extra
;
462 pcpu_size
[i
] -= extra
;
465 /* Split block if warranted */
466 if (pcpu_size
[i
] - size
> sizeof(unsigned long))
467 if (!split_block(i
, size
))
471 pcpu_size
[i
] = -pcpu_size
[i
];
475 printk(KERN_WARNING
"Could not allocate %lu bytes percpu data\n",
480 static void percpu_modfree(void *freeme
)
483 void *ptr
= __per_cpu_start
+ block_size(pcpu_size
[0]);
485 /* First entry is core kernel percpu data. */
486 for (i
= 1; i
< pcpu_num_used
; ptr
+= block_size(pcpu_size
[i
]), i
++) {
488 pcpu_size
[i
] = -pcpu_size
[i
];
495 /* Merge with previous? */
496 if (pcpu_size
[i
-1] >= 0) {
497 pcpu_size
[i
-1] += pcpu_size
[i
];
499 memmove(&pcpu_size
[i
], &pcpu_size
[i
+1],
500 (pcpu_num_used
- i
) * sizeof(pcpu_size
[0]));
503 /* Merge with next? */
504 if (i
+1 < pcpu_num_used
&& pcpu_size
[i
+1] >= 0) {
505 pcpu_size
[i
] += pcpu_size
[i
+1];
507 memmove(&pcpu_size
[i
+1], &pcpu_size
[i
+2],
508 (pcpu_num_used
- (i
+1)) * sizeof(pcpu_size
[0]));
512 static int percpu_modinit(void)
515 pcpu_num_allocated
= 2;
516 pcpu_size
= kmalloc(sizeof(pcpu_size
[0]) * pcpu_num_allocated
,
518 /* Static in-kernel percpu data (used). */
519 pcpu_size
[0] = -(__per_cpu_end
-__per_cpu_start
);
521 pcpu_size
[1] = PERCPU_ENOUGH_ROOM
+ pcpu_size
[0];
522 if (pcpu_size
[1] < 0) {
523 printk(KERN_ERR
"No per-cpu room for modules.\n");
529 __initcall(percpu_modinit
);
531 #endif /* CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
533 static unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
535 const char *secstrings
)
537 return find_sec(hdr
, sechdrs
, secstrings
, ".data.percpu");
540 static void percpu_modcopy(void *pcpudest
, const void *from
, unsigned long size
)
544 for_each_possible_cpu(cpu
)
545 memcpy(pcpudest
+ per_cpu_offset(cpu
), from
, size
);
548 #else /* ... !CONFIG_SMP */
550 static inline void *percpu_modalloc(unsigned long size
, unsigned long align
,
555 static inline void percpu_modfree(void *pcpuptr
)
559 static inline unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
561 const char *secstrings
)
565 static inline void percpu_modcopy(void *pcpudst
, const void *src
,
568 /* pcpusec should be 0, and size of that section should be 0. */
572 #endif /* CONFIG_SMP */
574 #define MODINFO_ATTR(field) \
575 static void setup_modinfo_##field(struct module *mod, const char *s) \
577 mod->field = kstrdup(s, GFP_KERNEL); \
579 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
580 struct module *mod, char *buffer) \
582 return sprintf(buffer, "%s\n", mod->field); \
584 static int modinfo_##field##_exists(struct module *mod) \
586 return mod->field != NULL; \
588 static void free_modinfo_##field(struct module *mod) \
593 static struct module_attribute modinfo_##field = { \
594 .attr = { .name = __stringify(field), .mode = 0444 }, \
595 .show = show_modinfo_##field, \
596 .setup = setup_modinfo_##field, \
597 .test = modinfo_##field##_exists, \
598 .free = free_modinfo_##field, \
601 MODINFO_ATTR(version
);
602 MODINFO_ATTR(srcversion
);
604 static char last_unloaded_module
[MODULE_NAME_LEN
+1];
606 #ifdef CONFIG_MODULE_UNLOAD
607 /* Init the unload section of the module. */
608 static void module_unload_init(struct module
*mod
)
612 INIT_LIST_HEAD(&mod
->modules_which_use_me
);
613 for_each_possible_cpu(cpu
)
614 local_set(__module_ref_addr(mod
, cpu
), 0);
615 /* Hold reference count during initialization. */
616 local_set(__module_ref_addr(mod
, raw_smp_processor_id()), 1);
617 /* Backwards compatibility macros put refcount during init. */
618 mod
->waiter
= current
;
621 /* modules using other modules */
624 struct list_head list
;
625 struct module
*module_which_uses
;
628 /* Does a already use b? */
629 static int already_uses(struct module
*a
, struct module
*b
)
631 struct module_use
*use
;
633 list_for_each_entry(use
, &b
->modules_which_use_me
, list
) {
634 if (use
->module_which_uses
== a
) {
635 DEBUGP("%s uses %s!\n", a
->name
, b
->name
);
639 DEBUGP("%s does not use %s!\n", a
->name
, b
->name
);
643 /* Module a uses b */
644 static int use_module(struct module
*a
, struct module
*b
)
646 struct module_use
*use
;
649 if (b
== NULL
|| already_uses(a
, b
)) return 1;
651 /* If we're interrupted or time out, we fail. */
652 if (wait_event_interruptible_timeout(
653 module_wq
, (err
= strong_try_module_get(b
)) != -EBUSY
,
655 printk("%s: gave up waiting for init of module %s.\n",
660 /* If strong_try_module_get() returned a different error, we fail. */
664 DEBUGP("Allocating new usage for %s.\n", a
->name
);
665 use
= kmalloc(sizeof(*use
), GFP_ATOMIC
);
667 printk("%s: out of memory loading\n", a
->name
);
672 use
->module_which_uses
= a
;
673 list_add(&use
->list
, &b
->modules_which_use_me
);
674 no_warn
= sysfs_create_link(b
->holders_dir
, &a
->mkobj
.kobj
, a
->name
);
678 /* Clear the unload stuff of the module. */
679 static void module_unload_free(struct module
*mod
)
683 list_for_each_entry(i
, &modules
, list
) {
684 struct module_use
*use
;
686 list_for_each_entry(use
, &i
->modules_which_use_me
, list
) {
687 if (use
->module_which_uses
== mod
) {
688 DEBUGP("%s unusing %s\n", mod
->name
, i
->name
);
690 list_del(&use
->list
);
692 sysfs_remove_link(i
->holders_dir
, mod
->name
);
693 /* There can be at most one match. */
700 #ifdef CONFIG_MODULE_FORCE_UNLOAD
701 static inline int try_force_unload(unsigned int flags
)
703 int ret
= (flags
& O_TRUNC
);
705 add_taint(TAINT_FORCED_RMMOD
);
709 static inline int try_force_unload(unsigned int flags
)
713 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
722 /* Whole machine is stopped with interrupts off when this runs. */
723 static int __try_stop_module(void *_sref
)
725 struct stopref
*sref
= _sref
;
727 /* If it's not unused, quit unless we're forcing. */
728 if (module_refcount(sref
->mod
) != 0) {
729 if (!(*sref
->forced
= try_force_unload(sref
->flags
)))
733 /* Mark it as dying. */
734 sref
->mod
->state
= MODULE_STATE_GOING
;
738 static int try_stop_module(struct module
*mod
, int flags
, int *forced
)
740 if (flags
& O_NONBLOCK
) {
741 struct stopref sref
= { mod
, flags
, forced
};
743 return stop_machine(__try_stop_module
, &sref
, NULL
);
745 /* We don't need to stop the machine for this. */
746 mod
->state
= MODULE_STATE_GOING
;
752 unsigned int module_refcount(struct module
*mod
)
754 unsigned int total
= 0;
757 for_each_possible_cpu(cpu
)
758 total
+= local_read(__module_ref_addr(mod
, cpu
));
761 EXPORT_SYMBOL(module_refcount
);
763 /* This exists whether we can unload or not */
764 static void free_module(struct module
*mod
);
766 static void wait_for_zero_refcount(struct module
*mod
)
768 /* Since we might sleep for some time, release the mutex first */
769 mutex_unlock(&module_mutex
);
771 DEBUGP("Looking at refcount...\n");
772 set_current_state(TASK_UNINTERRUPTIBLE
);
773 if (module_refcount(mod
) == 0)
777 current
->state
= TASK_RUNNING
;
778 mutex_lock(&module_mutex
);
781 /* Block module loading/unloading? */
782 int modules_disabled
= 0;
784 SYSCALL_DEFINE2(delete_module
, const char __user
*, name_user
,
788 char name
[MODULE_NAME_LEN
];
791 if (!capable(CAP_SYS_MODULE
) || modules_disabled
)
794 if (strncpy_from_user(name
, name_user
, MODULE_NAME_LEN
-1) < 0)
796 name
[MODULE_NAME_LEN
-1] = '\0';
798 /* Create stop_machine threads since free_module relies on
799 * a non-failing stop_machine call. */
800 ret
= stop_machine_create();
804 if (mutex_lock_interruptible(&module_mutex
) != 0) {
809 mod
= find_module(name
);
815 if (!list_empty(&mod
->modules_which_use_me
)) {
816 /* Other modules depend on us: get rid of them first. */
821 /* Doing init or already dying? */
822 if (mod
->state
!= MODULE_STATE_LIVE
) {
823 /* FIXME: if (force), slam module count and wake up
825 DEBUGP("%s already dying\n", mod
->name
);
830 /* If it has an init func, it must have an exit func to unload */
831 if (mod
->init
&& !mod
->exit
) {
832 forced
= try_force_unload(flags
);
834 /* This module can't be removed */
840 /* Set this up before setting mod->state */
841 mod
->waiter
= current
;
843 /* Stop the machine so refcounts can't move and disable module. */
844 ret
= try_stop_module(mod
, flags
, &forced
);
848 /* Never wait if forced. */
849 if (!forced
&& module_refcount(mod
) != 0)
850 wait_for_zero_refcount(mod
);
852 mutex_unlock(&module_mutex
);
853 /* Final destruction now noone is using it. */
854 if (mod
->exit
!= NULL
)
856 blocking_notifier_call_chain(&module_notify_list
,
857 MODULE_STATE_GOING
, mod
);
858 async_synchronize_full();
859 mutex_lock(&module_mutex
);
860 /* Store the name of the last unloaded module for diagnostic purposes */
861 strlcpy(last_unloaded_module
, mod
->name
, sizeof(last_unloaded_module
));
862 ddebug_remove_module(mod
->name
);
866 mutex_unlock(&module_mutex
);
868 stop_machine_destroy();
872 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
874 struct module_use
*use
;
875 int printed_something
= 0;
877 seq_printf(m
, " %u ", module_refcount(mod
));
879 /* Always include a trailing , so userspace can differentiate
880 between this and the old multi-field proc format. */
881 list_for_each_entry(use
, &mod
->modules_which_use_me
, list
) {
882 printed_something
= 1;
883 seq_printf(m
, "%s,", use
->module_which_uses
->name
);
886 if (mod
->init
!= NULL
&& mod
->exit
== NULL
) {
887 printed_something
= 1;
888 seq_printf(m
, "[permanent],");
891 if (!printed_something
)
895 void __symbol_put(const char *symbol
)
897 struct module
*owner
;
900 if (IS_ERR_VALUE(find_symbol(symbol
, &owner
, NULL
, true, false)))
905 EXPORT_SYMBOL(__symbol_put
);
907 void symbol_put_addr(void *addr
)
909 struct module
*modaddr
;
911 if (core_kernel_text((unsigned long)addr
))
914 if (!(modaddr
= module_text_address((unsigned long)addr
)))
918 EXPORT_SYMBOL_GPL(symbol_put_addr
);
920 static ssize_t
show_refcnt(struct module_attribute
*mattr
,
921 struct module
*mod
, char *buffer
)
923 return sprintf(buffer
, "%u\n", module_refcount(mod
));
926 static struct module_attribute refcnt
= {
927 .attr
= { .name
= "refcnt", .mode
= 0444 },
931 void module_put(struct module
*module
)
934 unsigned int cpu
= get_cpu();
935 local_dec(__module_ref_addr(module
, cpu
));
936 /* Maybe they're waiting for us to drop reference? */
937 if (unlikely(!module_is_live(module
)))
938 wake_up_process(module
->waiter
);
942 EXPORT_SYMBOL(module_put
);
944 #else /* !CONFIG_MODULE_UNLOAD */
945 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
947 /* We don't know the usage count, or what modules are using. */
948 seq_printf(m
, " - -");
951 static inline void module_unload_free(struct module
*mod
)
955 static inline int use_module(struct module
*a
, struct module
*b
)
957 return strong_try_module_get(b
) == 0;
960 static inline void module_unload_init(struct module
*mod
)
963 #endif /* CONFIG_MODULE_UNLOAD */
965 static ssize_t
show_initstate(struct module_attribute
*mattr
,
966 struct module
*mod
, char *buffer
)
968 const char *state
= "unknown";
970 switch (mod
->state
) {
971 case MODULE_STATE_LIVE
:
974 case MODULE_STATE_COMING
:
977 case MODULE_STATE_GOING
:
981 return sprintf(buffer
, "%s\n", state
);
984 static struct module_attribute initstate
= {
985 .attr
= { .name
= "initstate", .mode
= 0444 },
986 .show
= show_initstate
,
989 static struct module_attribute
*modinfo_attrs
[] = {
993 #ifdef CONFIG_MODULE_UNLOAD
999 static const char vermagic
[] = VERMAGIC_STRING
;
1001 static int try_to_force_load(struct module
*mod
, const char *symname
)
1003 #ifdef CONFIG_MODULE_FORCE_LOAD
1004 if (!test_taint(TAINT_FORCED_MODULE
))
1005 printk("%s: no version for \"%s\" found: kernel tainted.\n",
1006 mod
->name
, symname
);
1007 add_taint_module(mod
, TAINT_FORCED_MODULE
);
1014 #ifdef CONFIG_MODVERSIONS
1015 static int check_version(Elf_Shdr
*sechdrs
,
1016 unsigned int versindex
,
1017 const char *symname
,
1019 const unsigned long *crc
)
1021 unsigned int i
, num_versions
;
1022 struct modversion_info
*versions
;
1024 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1028 /* No versions at all? modprobe --force does this. */
1030 return try_to_force_load(mod
, symname
) == 0;
1032 versions
= (void *) sechdrs
[versindex
].sh_addr
;
1033 num_versions
= sechdrs
[versindex
].sh_size
1034 / sizeof(struct modversion_info
);
1036 for (i
= 0; i
< num_versions
; i
++) {
1037 if (strcmp(versions
[i
].name
, symname
) != 0)
1040 if (versions
[i
].crc
== *crc
)
1042 DEBUGP("Found checksum %lX vs module %lX\n",
1043 *crc
, versions
[i
].crc
);
1047 printk(KERN_WARNING
"%s: no symbol version for %s\n",
1048 mod
->name
, symname
);
1052 printk("%s: disagrees about version of symbol %s\n",
1053 mod
->name
, symname
);
1057 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1058 unsigned int versindex
,
1061 const unsigned long *crc
;
1063 if (IS_ERR_VALUE(find_symbol("struct_module", NULL
, &crc
, true, false)))
1065 return check_version(sechdrs
, versindex
, "struct_module", mod
, crc
);
1068 /* First part is kernel version, which we ignore if module has crcs. */
1069 static inline int same_magic(const char *amagic
, const char *bmagic
,
1073 amagic
+= strcspn(amagic
, " ");
1074 bmagic
+= strcspn(bmagic
, " ");
1076 return strcmp(amagic
, bmagic
) == 0;
1079 static inline int check_version(Elf_Shdr
*sechdrs
,
1080 unsigned int versindex
,
1081 const char *symname
,
1083 const unsigned long *crc
)
1088 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1089 unsigned int versindex
,
1095 static inline int same_magic(const char *amagic
, const char *bmagic
,
1098 return strcmp(amagic
, bmagic
) == 0;
1100 #endif /* CONFIG_MODVERSIONS */
1102 /* Resolve a symbol for this module. I.e. if we find one, record usage.
1103 Must be holding module_mutex. */
1104 static unsigned long resolve_symbol(Elf_Shdr
*sechdrs
,
1105 unsigned int versindex
,
1109 struct module
*owner
;
1111 const unsigned long *crc
;
1113 ret
= find_symbol(name
, &owner
, &crc
,
1114 !(mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
)), true);
1115 if (!IS_ERR_VALUE(ret
)) {
1116 /* use_module can fail due to OOM,
1117 or module initialization or unloading */
1118 if (!check_version(sechdrs
, versindex
, name
, mod
, crc
) ||
1119 !use_module(mod
, owner
))
1126 * /sys/module/foo/sections stuff
1127 * J. Corbet <corbet@lwn.net>
1129 #if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
1130 struct module_sect_attr
1132 struct module_attribute mattr
;
1134 unsigned long address
;
1137 struct module_sect_attrs
1139 struct attribute_group grp
;
1140 unsigned int nsections
;
1141 struct module_sect_attr attrs
[0];
1144 static ssize_t
module_sect_show(struct module_attribute
*mattr
,
1145 struct module
*mod
, char *buf
)
1147 struct module_sect_attr
*sattr
=
1148 container_of(mattr
, struct module_sect_attr
, mattr
);
1149 return sprintf(buf
, "0x%lx\n", sattr
->address
);
1152 static void free_sect_attrs(struct module_sect_attrs
*sect_attrs
)
1154 unsigned int section
;
1156 for (section
= 0; section
< sect_attrs
->nsections
; section
++)
1157 kfree(sect_attrs
->attrs
[section
].name
);
1161 static void add_sect_attrs(struct module
*mod
, unsigned int nsect
,
1162 char *secstrings
, Elf_Shdr
*sechdrs
)
1164 unsigned int nloaded
= 0, i
, size
[2];
1165 struct module_sect_attrs
*sect_attrs
;
1166 struct module_sect_attr
*sattr
;
1167 struct attribute
**gattr
;
1169 /* Count loaded sections and allocate structures */
1170 for (i
= 0; i
< nsect
; i
++)
1171 if (sechdrs
[i
].sh_flags
& SHF_ALLOC
)
1173 size
[0] = ALIGN(sizeof(*sect_attrs
)
1174 + nloaded
* sizeof(sect_attrs
->attrs
[0]),
1175 sizeof(sect_attrs
->grp
.attrs
[0]));
1176 size
[1] = (nloaded
+ 1) * sizeof(sect_attrs
->grp
.attrs
[0]);
1177 sect_attrs
= kzalloc(size
[0] + size
[1], GFP_KERNEL
);
1178 if (sect_attrs
== NULL
)
1181 /* Setup section attributes. */
1182 sect_attrs
->grp
.name
= "sections";
1183 sect_attrs
->grp
.attrs
= (void *)sect_attrs
+ size
[0];
1185 sect_attrs
->nsections
= 0;
1186 sattr
= §_attrs
->attrs
[0];
1187 gattr
= §_attrs
->grp
.attrs
[0];
1188 for (i
= 0; i
< nsect
; i
++) {
1189 if (! (sechdrs
[i
].sh_flags
& SHF_ALLOC
))
1191 sattr
->address
= sechdrs
[i
].sh_addr
;
1192 sattr
->name
= kstrdup(secstrings
+ sechdrs
[i
].sh_name
,
1194 if (sattr
->name
== NULL
)
1196 sect_attrs
->nsections
++;
1197 sattr
->mattr
.show
= module_sect_show
;
1198 sattr
->mattr
.store
= NULL
;
1199 sattr
->mattr
.attr
.name
= sattr
->name
;
1200 sattr
->mattr
.attr
.mode
= S_IRUGO
;
1201 *(gattr
++) = &(sattr
++)->mattr
.attr
;
1205 if (sysfs_create_group(&mod
->mkobj
.kobj
, §_attrs
->grp
))
1208 mod
->sect_attrs
= sect_attrs
;
1211 free_sect_attrs(sect_attrs
);
1214 static void remove_sect_attrs(struct module
*mod
)
1216 if (mod
->sect_attrs
) {
1217 sysfs_remove_group(&mod
->mkobj
.kobj
,
1218 &mod
->sect_attrs
->grp
);
1219 /* We are positive that no one is using any sect attrs
1220 * at this point. Deallocate immediately. */
1221 free_sect_attrs(mod
->sect_attrs
);
1222 mod
->sect_attrs
= NULL
;
1227 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1230 struct module_notes_attrs
{
1231 struct kobject
*dir
;
1233 struct bin_attribute attrs
[0];
1236 static ssize_t
module_notes_read(struct kobject
*kobj
,
1237 struct bin_attribute
*bin_attr
,
1238 char *buf
, loff_t pos
, size_t count
)
1241 * The caller checked the pos and count against our size.
1243 memcpy(buf
, bin_attr
->private + pos
, count
);
1247 static void free_notes_attrs(struct module_notes_attrs
*notes_attrs
,
1250 if (notes_attrs
->dir
) {
1252 sysfs_remove_bin_file(notes_attrs
->dir
,
1253 ¬es_attrs
->attrs
[i
]);
1254 kobject_put(notes_attrs
->dir
);
1259 static void add_notes_attrs(struct module
*mod
, unsigned int nsect
,
1260 char *secstrings
, Elf_Shdr
*sechdrs
)
1262 unsigned int notes
, loaded
, i
;
1263 struct module_notes_attrs
*notes_attrs
;
1264 struct bin_attribute
*nattr
;
1266 /* Count notes sections and allocate structures. */
1268 for (i
= 0; i
< nsect
; i
++)
1269 if ((sechdrs
[i
].sh_flags
& SHF_ALLOC
) &&
1270 (sechdrs
[i
].sh_type
== SHT_NOTE
))
1276 notes_attrs
= kzalloc(sizeof(*notes_attrs
)
1277 + notes
* sizeof(notes_attrs
->attrs
[0]),
1279 if (notes_attrs
== NULL
)
1282 notes_attrs
->notes
= notes
;
1283 nattr
= ¬es_attrs
->attrs
[0];
1284 for (loaded
= i
= 0; i
< nsect
; ++i
) {
1285 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
1287 if (sechdrs
[i
].sh_type
== SHT_NOTE
) {
1288 nattr
->attr
.name
= mod
->sect_attrs
->attrs
[loaded
].name
;
1289 nattr
->attr
.mode
= S_IRUGO
;
1290 nattr
->size
= sechdrs
[i
].sh_size
;
1291 nattr
->private = (void *) sechdrs
[i
].sh_addr
;
1292 nattr
->read
= module_notes_read
;
1298 notes_attrs
->dir
= kobject_create_and_add("notes", &mod
->mkobj
.kobj
);
1299 if (!notes_attrs
->dir
)
1302 for (i
= 0; i
< notes
; ++i
)
1303 if (sysfs_create_bin_file(notes_attrs
->dir
,
1304 ¬es_attrs
->attrs
[i
]))
1307 mod
->notes_attrs
= notes_attrs
;
1311 free_notes_attrs(notes_attrs
, i
);
1314 static void remove_notes_attrs(struct module
*mod
)
1316 if (mod
->notes_attrs
)
1317 free_notes_attrs(mod
->notes_attrs
, mod
->notes_attrs
->notes
);
1322 static inline void add_sect_attrs(struct module
*mod
, unsigned int nsect
,
1323 char *sectstrings
, Elf_Shdr
*sechdrs
)
1327 static inline void remove_sect_attrs(struct module
*mod
)
1331 static inline void add_notes_attrs(struct module
*mod
, unsigned int nsect
,
1332 char *sectstrings
, Elf_Shdr
*sechdrs
)
1336 static inline void remove_notes_attrs(struct module
*mod
)
1342 int module_add_modinfo_attrs(struct module
*mod
)
1344 struct module_attribute
*attr
;
1345 struct module_attribute
*temp_attr
;
1349 mod
->modinfo_attrs
= kzalloc((sizeof(struct module_attribute
) *
1350 (ARRAY_SIZE(modinfo_attrs
) + 1)),
1352 if (!mod
->modinfo_attrs
)
1355 temp_attr
= mod
->modinfo_attrs
;
1356 for (i
= 0; (attr
= modinfo_attrs
[i
]) && !error
; i
++) {
1358 (attr
->test
&& attr
->test(mod
))) {
1359 memcpy(temp_attr
, attr
, sizeof(*temp_attr
));
1360 error
= sysfs_create_file(&mod
->mkobj
.kobj
,&temp_attr
->attr
);
1367 void module_remove_modinfo_attrs(struct module
*mod
)
1369 struct module_attribute
*attr
;
1372 for (i
= 0; (attr
= &mod
->modinfo_attrs
[i
]); i
++) {
1373 /* pick a field to test for end of list */
1374 if (!attr
->attr
.name
)
1376 sysfs_remove_file(&mod
->mkobj
.kobj
,&attr
->attr
);
1380 kfree(mod
->modinfo_attrs
);
1383 int mod_sysfs_init(struct module
*mod
)
1386 struct kobject
*kobj
;
1388 if (!module_sysfs_initialized
) {
1389 printk(KERN_ERR
"%s: module sysfs not initialized\n",
1395 kobj
= kset_find_obj(module_kset
, mod
->name
);
1397 printk(KERN_ERR
"%s: module is already loaded\n", mod
->name
);
1403 mod
->mkobj
.mod
= mod
;
1405 memset(&mod
->mkobj
.kobj
, 0, sizeof(mod
->mkobj
.kobj
));
1406 mod
->mkobj
.kobj
.kset
= module_kset
;
1407 err
= kobject_init_and_add(&mod
->mkobj
.kobj
, &module_ktype
, NULL
,
1410 kobject_put(&mod
->mkobj
.kobj
);
1412 /* delay uevent until full sysfs population */
1417 int mod_sysfs_setup(struct module
*mod
,
1418 struct kernel_param
*kparam
,
1419 unsigned int num_params
)
1423 mod
->holders_dir
= kobject_create_and_add("holders", &mod
->mkobj
.kobj
);
1424 if (!mod
->holders_dir
) {
1429 err
= module_param_sysfs_setup(mod
, kparam
, num_params
);
1431 goto out_unreg_holders
;
1433 err
= module_add_modinfo_attrs(mod
);
1435 goto out_unreg_param
;
1437 kobject_uevent(&mod
->mkobj
.kobj
, KOBJ_ADD
);
1441 module_param_sysfs_remove(mod
);
1443 kobject_put(mod
->holders_dir
);
1445 kobject_put(&mod
->mkobj
.kobj
);
1449 static void mod_sysfs_fini(struct module
*mod
)
1451 kobject_put(&mod
->mkobj
.kobj
);
1454 #else /* CONFIG_SYSFS */
1456 static void mod_sysfs_fini(struct module
*mod
)
1460 #endif /* CONFIG_SYSFS */
1462 static void mod_kobject_remove(struct module
*mod
)
1464 module_remove_modinfo_attrs(mod
);
1465 module_param_sysfs_remove(mod
);
1466 kobject_put(mod
->mkobj
.drivers_dir
);
1467 kobject_put(mod
->holders_dir
);
1468 mod_sysfs_fini(mod
);
1472 * unlink the module with the whole machine is stopped with interrupts off
1473 * - this defends against kallsyms not taking locks
1475 static int __unlink_module(void *_mod
)
1477 struct module
*mod
= _mod
;
1478 list_del(&mod
->list
);
1482 /* Free a module, remove from lists, etc (must hold module_mutex). */
1483 static void free_module(struct module
*mod
)
1485 /* Delete from various lists */
1486 stop_machine(__unlink_module
, mod
, NULL
);
1487 remove_notes_attrs(mod
);
1488 remove_sect_attrs(mod
);
1489 mod_kobject_remove(mod
);
1491 /* Arch-specific cleanup. */
1492 module_arch_cleanup(mod
);
1494 /* Module unload stuff */
1495 module_unload_free(mod
);
1497 /* release any pointers to mcount in this module */
1498 ftrace_release(mod
->module_core
, mod
->core_size
);
1500 /* This may be NULL, but that's OK */
1501 module_free(mod
, mod
->module_init
);
1504 percpu_modfree(mod
->percpu
);
1505 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
1507 percpu_modfree(mod
->refptr
);
1509 /* Free lock-classes: */
1510 lockdep_free_key_range(mod
->module_core
, mod
->core_size
);
1512 /* Finally, free the core (containing the module structure) */
1513 module_free(mod
, mod
->module_core
);
1516 void *__symbol_get(const char *symbol
)
1518 struct module
*owner
;
1519 unsigned long value
;
1522 value
= find_symbol(symbol
, &owner
, NULL
, true, true);
1523 if (IS_ERR_VALUE(value
))
1525 else if (strong_try_module_get(owner
))
1529 return (void *)value
;
1531 EXPORT_SYMBOL_GPL(__symbol_get
);
1534 * Ensure that an exported symbol [global namespace] does not already exist
1535 * in the kernel or in some other module's exported symbol table.
1537 static int verify_export_symbols(struct module
*mod
)
1540 struct module
*owner
;
1541 const struct kernel_symbol
*s
;
1543 const struct kernel_symbol
*sym
;
1546 { mod
->syms
, mod
->num_syms
},
1547 { mod
->gpl_syms
, mod
->num_gpl_syms
},
1548 { mod
->gpl_future_syms
, mod
->num_gpl_future_syms
},
1549 #ifdef CONFIG_UNUSED_SYMBOLS
1550 { mod
->unused_syms
, mod
->num_unused_syms
},
1551 { mod
->unused_gpl_syms
, mod
->num_unused_gpl_syms
},
1555 for (i
= 0; i
< ARRAY_SIZE(arr
); i
++) {
1556 for (s
= arr
[i
].sym
; s
< arr
[i
].sym
+ arr
[i
].num
; s
++) {
1557 if (!IS_ERR_VALUE(find_symbol(s
->name
, &owner
,
1558 NULL
, true, false))) {
1560 "%s: exports duplicate symbol %s"
1562 mod
->name
, s
->name
, module_name(owner
));
1570 /* Change all symbols so that st_value encodes the pointer directly. */
1571 static int simplify_symbols(Elf_Shdr
*sechdrs
,
1572 unsigned int symindex
,
1574 unsigned int versindex
,
1575 unsigned int pcpuindex
,
1578 Elf_Sym
*sym
= (void *)sechdrs
[symindex
].sh_addr
;
1579 unsigned long secbase
;
1580 unsigned int i
, n
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1583 for (i
= 1; i
< n
; i
++) {
1584 switch (sym
[i
].st_shndx
) {
1586 /* We compiled with -fno-common. These are not
1587 supposed to happen. */
1588 DEBUGP("Common symbol: %s\n", strtab
+ sym
[i
].st_name
);
1589 printk("%s: please compile with -fno-common\n",
1595 /* Don't need to do anything */
1596 DEBUGP("Absolute symbol: 0x%08lx\n",
1597 (long)sym
[i
].st_value
);
1602 = resolve_symbol(sechdrs
, versindex
,
1603 strtab
+ sym
[i
].st_name
, mod
);
1605 /* Ok if resolved. */
1606 if (!IS_ERR_VALUE(sym
[i
].st_value
))
1609 if (ELF_ST_BIND(sym
[i
].st_info
) == STB_WEAK
)
1612 printk(KERN_WARNING
"%s: Unknown symbol %s\n",
1613 mod
->name
, strtab
+ sym
[i
].st_name
);
1618 /* Divert to percpu allocation if a percpu var. */
1619 if (sym
[i
].st_shndx
== pcpuindex
)
1620 secbase
= (unsigned long)mod
->percpu
;
1622 secbase
= sechdrs
[sym
[i
].st_shndx
].sh_addr
;
1623 sym
[i
].st_value
+= secbase
;
1631 /* Additional bytes needed by arch in front of individual sections */
1632 unsigned int __weak
arch_mod_section_prepend(struct module
*mod
,
1633 unsigned int section
)
1635 /* default implementation just returns zero */
1639 /* Update size with this section: return offset. */
1640 static long get_offset(struct module
*mod
, unsigned int *size
,
1641 Elf_Shdr
*sechdr
, unsigned int section
)
1645 *size
+= arch_mod_section_prepend(mod
, section
);
1646 ret
= ALIGN(*size
, sechdr
->sh_addralign
?: 1);
1647 *size
= ret
+ sechdr
->sh_size
;
1651 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1652 might -- code, read-only data, read-write data, small data. Tally
1653 sizes, and place the offsets into sh_entsize fields: high bit means it
1655 static void layout_sections(struct module
*mod
,
1656 const Elf_Ehdr
*hdr
,
1658 const char *secstrings
)
1660 static unsigned long const masks
[][2] = {
1661 /* NOTE: all executable code must be the first section
1662 * in this array; otherwise modify the text_size
1663 * finder in the two loops below */
1664 { SHF_EXECINSTR
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1665 { SHF_ALLOC
, SHF_WRITE
| ARCH_SHF_SMALL
},
1666 { SHF_WRITE
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1667 { ARCH_SHF_SMALL
| SHF_ALLOC
, 0 }
1671 for (i
= 0; i
< hdr
->e_shnum
; i
++)
1672 sechdrs
[i
].sh_entsize
= ~0UL;
1674 DEBUGP("Core section allocation order:\n");
1675 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1676 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1677 Elf_Shdr
*s
= &sechdrs
[i
];
1679 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1680 || (s
->sh_flags
& masks
[m
][1])
1681 || s
->sh_entsize
!= ~0UL
1682 || strncmp(secstrings
+ s
->sh_name
,
1685 s
->sh_entsize
= get_offset(mod
, &mod
->core_size
, s
, i
);
1686 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1689 mod
->core_text_size
= mod
->core_size
;
1692 DEBUGP("Init section allocation order:\n");
1693 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1694 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1695 Elf_Shdr
*s
= &sechdrs
[i
];
1697 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1698 || (s
->sh_flags
& masks
[m
][1])
1699 || s
->sh_entsize
!= ~0UL
1700 || strncmp(secstrings
+ s
->sh_name
,
1703 s
->sh_entsize
= (get_offset(mod
, &mod
->init_size
, s
, i
)
1704 | INIT_OFFSET_MASK
);
1705 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1708 mod
->init_text_size
= mod
->init_size
;
1712 static void set_license(struct module
*mod
, const char *license
)
1715 license
= "unspecified";
1717 if (!license_is_gpl_compatible(license
)) {
1718 if (!test_taint(TAINT_PROPRIETARY_MODULE
))
1719 printk(KERN_WARNING
"%s: module license '%s' taints "
1720 "kernel.\n", mod
->name
, license
);
1721 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
1725 /* Parse tag=value strings from .modinfo section */
1726 static char *next_string(char *string
, unsigned long *secsize
)
1728 /* Skip non-zero chars */
1731 if ((*secsize
)-- <= 1)
1735 /* Skip any zero padding. */
1736 while (!string
[0]) {
1738 if ((*secsize
)-- <= 1)
1744 static char *get_modinfo(Elf_Shdr
*sechdrs
,
1749 unsigned int taglen
= strlen(tag
);
1750 unsigned long size
= sechdrs
[info
].sh_size
;
1752 for (p
= (char *)sechdrs
[info
].sh_addr
; p
; p
= next_string(p
, &size
)) {
1753 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
1754 return p
+ taglen
+ 1;
1759 static void setup_modinfo(struct module
*mod
, Elf_Shdr
*sechdrs
,
1760 unsigned int infoindex
)
1762 struct module_attribute
*attr
;
1765 for (i
= 0; (attr
= modinfo_attrs
[i
]); i
++) {
1768 get_modinfo(sechdrs
,
1774 #ifdef CONFIG_KALLSYMS
1776 /* lookup symbol in given range of kernel_symbols */
1777 static const struct kernel_symbol
*lookup_symbol(const char *name
,
1778 const struct kernel_symbol
*start
,
1779 const struct kernel_symbol
*stop
)
1781 const struct kernel_symbol
*ks
= start
;
1782 for (; ks
< stop
; ks
++)
1783 if (strcmp(ks
->name
, name
) == 0)
1788 static int is_exported(const char *name
, unsigned long value
,
1789 const struct module
*mod
)
1791 const struct kernel_symbol
*ks
;
1793 ks
= lookup_symbol(name
, __start___ksymtab
, __stop___ksymtab
);
1795 ks
= lookup_symbol(name
, mod
->syms
, mod
->syms
+ mod
->num_syms
);
1796 return ks
!= NULL
&& ks
->value
== value
;
1800 static char elf_type(const Elf_Sym
*sym
,
1802 const char *secstrings
,
1805 if (ELF_ST_BIND(sym
->st_info
) == STB_WEAK
) {
1806 if (ELF_ST_TYPE(sym
->st_info
) == STT_OBJECT
)
1811 if (sym
->st_shndx
== SHN_UNDEF
)
1813 if (sym
->st_shndx
== SHN_ABS
)
1815 if (sym
->st_shndx
>= SHN_LORESERVE
)
1817 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_EXECINSTR
)
1819 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_ALLOC
1820 && sechdrs
[sym
->st_shndx
].sh_type
!= SHT_NOBITS
) {
1821 if (!(sechdrs
[sym
->st_shndx
].sh_flags
& SHF_WRITE
))
1823 else if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1828 if (sechdrs
[sym
->st_shndx
].sh_type
== SHT_NOBITS
) {
1829 if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1834 if (strncmp(secstrings
+ sechdrs
[sym
->st_shndx
].sh_name
,
1835 ".debug", strlen(".debug")) == 0)
1840 static void add_kallsyms(struct module
*mod
,
1842 unsigned int symindex
,
1843 unsigned int strindex
,
1844 const char *secstrings
)
1848 mod
->symtab
= (void *)sechdrs
[symindex
].sh_addr
;
1849 mod
->num_symtab
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1850 mod
->strtab
= (void *)sechdrs
[strindex
].sh_addr
;
1852 /* Set types up while we still have access to sections. */
1853 for (i
= 0; i
< mod
->num_symtab
; i
++)
1854 mod
->symtab
[i
].st_info
1855 = elf_type(&mod
->symtab
[i
], sechdrs
, secstrings
, mod
);
1858 static inline void add_kallsyms(struct module
*mod
,
1860 unsigned int symindex
,
1861 unsigned int strindex
,
1862 const char *secstrings
)
1865 #endif /* CONFIG_KALLSYMS */
1867 static void dynamic_debug_setup(struct _ddebug
*debug
, unsigned int num
)
1869 #ifdef CONFIG_DYNAMIC_DEBUG
1870 if (ddebug_add_module(debug
, num
, debug
->modname
))
1871 printk(KERN_ERR
"dynamic debug error adding module: %s\n",
1876 static void *module_alloc_update_bounds(unsigned long size
)
1878 void *ret
= module_alloc(size
);
1881 /* Update module bounds. */
1882 if ((unsigned long)ret
< module_addr_min
)
1883 module_addr_min
= (unsigned long)ret
;
1884 if ((unsigned long)ret
+ size
> module_addr_max
)
1885 module_addr_max
= (unsigned long)ret
+ size
;
1890 /* Allocate and load the module: note that size of section 0 is always
1891 zero, and we rely on this for optional sections. */
1892 static noinline
struct module
*load_module(void __user
*umod
,
1894 const char __user
*uargs
)
1898 char *secstrings
, *args
, *modmagic
, *strtab
= NULL
;
1901 unsigned int symindex
= 0;
1902 unsigned int strindex
= 0;
1903 unsigned int modindex
, versindex
, infoindex
, pcpuindex
;
1904 unsigned int num_kp
, num_mcount
;
1905 struct kernel_param
*kp
;
1908 void *percpu
= NULL
, *ptr
= NULL
; /* Stops spurious gcc warning */
1909 unsigned long *mseg
;
1910 mm_segment_t old_fs
;
1912 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1914 if (len
< sizeof(*hdr
))
1915 return ERR_PTR(-ENOEXEC
);
1917 /* Suck in entire file: we'll want most of it. */
1918 /* vmalloc barfs on "unusual" numbers. Check here */
1919 if (len
> 64 * 1024 * 1024 || (hdr
= vmalloc(len
)) == NULL
)
1920 return ERR_PTR(-ENOMEM
);
1922 /* Create stop_machine threads since the error path relies on
1923 * a non-failing stop_machine call. */
1924 err
= stop_machine_create();
1928 if (copy_from_user(hdr
, umod
, len
) != 0) {
1933 /* Sanity checks against insmoding binaries or wrong arch,
1934 weird elf version */
1935 if (memcmp(hdr
->e_ident
, ELFMAG
, SELFMAG
) != 0
1936 || hdr
->e_type
!= ET_REL
1937 || !elf_check_arch(hdr
)
1938 || hdr
->e_shentsize
!= sizeof(*sechdrs
)) {
1943 if (len
< hdr
->e_shoff
+ hdr
->e_shnum
* sizeof(Elf_Shdr
))
1946 /* Convenience variables */
1947 sechdrs
= (void *)hdr
+ hdr
->e_shoff
;
1948 secstrings
= (void *)hdr
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
1949 sechdrs
[0].sh_addr
= 0;
1951 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
1952 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
1953 && len
< sechdrs
[i
].sh_offset
+ sechdrs
[i
].sh_size
)
1956 /* Mark all sections sh_addr with their address in the
1958 sechdrs
[i
].sh_addr
= (size_t)hdr
+ sechdrs
[i
].sh_offset
;
1960 /* Internal symbols and strings. */
1961 if (sechdrs
[i
].sh_type
== SHT_SYMTAB
) {
1963 strindex
= sechdrs
[i
].sh_link
;
1964 strtab
= (char *)hdr
+ sechdrs
[strindex
].sh_offset
;
1966 #ifndef CONFIG_MODULE_UNLOAD
1967 /* Don't load .exit sections */
1968 if (strncmp(secstrings
+sechdrs
[i
].sh_name
, ".exit", 5) == 0)
1969 sechdrs
[i
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1973 modindex
= find_sec(hdr
, sechdrs
, secstrings
,
1974 ".gnu.linkonce.this_module");
1976 printk(KERN_WARNING
"No module found in object\n");
1980 /* This is temporary: point mod into copy of data. */
1981 mod
= (void *)sechdrs
[modindex
].sh_addr
;
1983 if (symindex
== 0) {
1984 printk(KERN_WARNING
"%s: module has no symbols (stripped?)\n",
1990 versindex
= find_sec(hdr
, sechdrs
, secstrings
, "__versions");
1991 infoindex
= find_sec(hdr
, sechdrs
, secstrings
, ".modinfo");
1992 pcpuindex
= find_pcpusec(hdr
, sechdrs
, secstrings
);
1994 /* Don't keep modinfo and version sections. */
1995 sechdrs
[infoindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1996 sechdrs
[versindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1997 #ifdef CONFIG_KALLSYMS
1998 /* Keep symbol and string tables for decoding later. */
1999 sechdrs
[symindex
].sh_flags
|= SHF_ALLOC
;
2000 sechdrs
[strindex
].sh_flags
|= SHF_ALLOC
;
2003 /* Check module struct version now, before we try to use module. */
2004 if (!check_modstruct_version(sechdrs
, versindex
, mod
)) {
2009 modmagic
= get_modinfo(sechdrs
, infoindex
, "vermagic");
2010 /* This is allowed: modprobe --force will invalidate it. */
2012 err
= try_to_force_load(mod
, "magic");
2015 } else if (!same_magic(modmagic
, vermagic
, versindex
)) {
2016 printk(KERN_ERR
"%s: version magic '%s' should be '%s'\n",
2017 mod
->name
, modmagic
, vermagic
);
2022 staging
= get_modinfo(sechdrs
, infoindex
, "staging");
2024 add_taint_module(mod
, TAINT_CRAP
);
2025 printk(KERN_WARNING
"%s: module is from the staging directory,"
2026 " the quality is unknown, you have been warned.\n",
2030 /* Now copy in args */
2031 args
= strndup_user(uargs
, ~0UL >> 1);
2033 err
= PTR_ERR(args
);
2037 if (find_module(mod
->name
)) {
2042 mod
->state
= MODULE_STATE_COMING
;
2044 /* Allow arches to frob section contents and sizes. */
2045 err
= module_frob_arch_sections(hdr
, sechdrs
, secstrings
, mod
);
2050 /* We have a special allocation for this section. */
2051 percpu
= percpu_modalloc(sechdrs
[pcpuindex
].sh_size
,
2052 sechdrs
[pcpuindex
].sh_addralign
,
2058 sechdrs
[pcpuindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2059 mod
->percpu
= percpu
;
2062 /* Determine total sizes, and put offsets in sh_entsize. For now
2063 this is done generically; there doesn't appear to be any
2064 special cases for the architectures. */
2065 layout_sections(mod
, hdr
, sechdrs
, secstrings
);
2067 /* Do the allocs. */
2068 ptr
= module_alloc_update_bounds(mod
->core_size
);
2073 memset(ptr
, 0, mod
->core_size
);
2074 mod
->module_core
= ptr
;
2076 ptr
= module_alloc_update_bounds(mod
->init_size
);
2077 if (!ptr
&& mod
->init_size
) {
2081 memset(ptr
, 0, mod
->init_size
);
2082 mod
->module_init
= ptr
;
2084 /* Transfer each section which specifies SHF_ALLOC */
2085 DEBUGP("final section addresses:\n");
2086 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
2089 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
2092 if (sechdrs
[i
].sh_entsize
& INIT_OFFSET_MASK
)
2093 dest
= mod
->module_init
2094 + (sechdrs
[i
].sh_entsize
& ~INIT_OFFSET_MASK
);
2096 dest
= mod
->module_core
+ sechdrs
[i
].sh_entsize
;
2098 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
)
2099 memcpy(dest
, (void *)sechdrs
[i
].sh_addr
,
2100 sechdrs
[i
].sh_size
);
2101 /* Update sh_addr to point to copy in image. */
2102 sechdrs
[i
].sh_addr
= (unsigned long)dest
;
2103 DEBUGP("\t0x%lx %s\n", sechdrs
[i
].sh_addr
, secstrings
+ sechdrs
[i
].sh_name
);
2105 /* Module has been moved. */
2106 mod
= (void *)sechdrs
[modindex
].sh_addr
;
2108 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2109 mod
->refptr
= percpu_modalloc(sizeof(local_t
), __alignof__(local_t
),
2116 /* Now we've moved module, initialize linked lists, etc. */
2117 module_unload_init(mod
);
2119 /* add kobject, so we can reference it. */
2120 err
= mod_sysfs_init(mod
);
2124 /* Set up license info based on the info section */
2125 set_license(mod
, get_modinfo(sechdrs
, infoindex
, "license"));
2128 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2129 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2130 * using GPL-only symbols it needs.
2132 if (strcmp(mod
->name
, "ndiswrapper") == 0)
2133 add_taint(TAINT_PROPRIETARY_MODULE
);
2135 /* driverloader was caught wrongly pretending to be under GPL */
2136 if (strcmp(mod
->name
, "driverloader") == 0)
2137 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
2139 /* Set up MODINFO_ATTR fields */
2140 setup_modinfo(mod
, sechdrs
, infoindex
);
2142 /* Fix up syms, so that st_value is a pointer to location. */
2143 err
= simplify_symbols(sechdrs
, symindex
, strtab
, versindex
, pcpuindex
,
2148 /* Now we've got everything in the final locations, we can
2149 * find optional sections. */
2150 kp
= section_objs(hdr
, sechdrs
, secstrings
, "__param", sizeof(*kp
),
2152 mod
->syms
= section_objs(hdr
, sechdrs
, secstrings
, "__ksymtab",
2153 sizeof(*mod
->syms
), &mod
->num_syms
);
2154 mod
->crcs
= section_addr(hdr
, sechdrs
, secstrings
, "__kcrctab");
2155 mod
->gpl_syms
= section_objs(hdr
, sechdrs
, secstrings
, "__ksymtab_gpl",
2156 sizeof(*mod
->gpl_syms
),
2157 &mod
->num_gpl_syms
);
2158 mod
->gpl_crcs
= section_addr(hdr
, sechdrs
, secstrings
, "__kcrctab_gpl");
2159 mod
->gpl_future_syms
= section_objs(hdr
, sechdrs
, secstrings
,
2160 "__ksymtab_gpl_future",
2161 sizeof(*mod
->gpl_future_syms
),
2162 &mod
->num_gpl_future_syms
);
2163 mod
->gpl_future_crcs
= section_addr(hdr
, sechdrs
, secstrings
,
2164 "__kcrctab_gpl_future");
2166 #ifdef CONFIG_UNUSED_SYMBOLS
2167 mod
->unused_syms
= section_objs(hdr
, sechdrs
, secstrings
,
2169 sizeof(*mod
->unused_syms
),
2170 &mod
->num_unused_syms
);
2171 mod
->unused_crcs
= section_addr(hdr
, sechdrs
, secstrings
,
2172 "__kcrctab_unused");
2173 mod
->unused_gpl_syms
= section_objs(hdr
, sechdrs
, secstrings
,
2174 "__ksymtab_unused_gpl",
2175 sizeof(*mod
->unused_gpl_syms
),
2176 &mod
->num_unused_gpl_syms
);
2177 mod
->unused_gpl_crcs
= section_addr(hdr
, sechdrs
, secstrings
,
2178 "__kcrctab_unused_gpl");
2181 #ifdef CONFIG_MARKERS
2182 mod
->markers
= section_objs(hdr
, sechdrs
, secstrings
, "__markers",
2183 sizeof(*mod
->markers
), &mod
->num_markers
);
2185 #ifdef CONFIG_TRACEPOINTS
2186 mod
->tracepoints
= section_objs(hdr
, sechdrs
, secstrings
,
2188 sizeof(*mod
->tracepoints
),
2189 &mod
->num_tracepoints
);
2192 #ifdef CONFIG_MODVERSIONS
2193 if ((mod
->num_syms
&& !mod
->crcs
)
2194 || (mod
->num_gpl_syms
&& !mod
->gpl_crcs
)
2195 || (mod
->num_gpl_future_syms
&& !mod
->gpl_future_crcs
)
2196 #ifdef CONFIG_UNUSED_SYMBOLS
2197 || (mod
->num_unused_syms
&& !mod
->unused_crcs
)
2198 || (mod
->num_unused_gpl_syms
&& !mod
->unused_gpl_crcs
)
2201 printk(KERN_WARNING
"%s: No versions for exported symbols.\n", mod
->name
);
2202 err
= try_to_force_load(mod
, "nocrc");
2208 /* Now do relocations. */
2209 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
2210 const char *strtab
= (char *)sechdrs
[strindex
].sh_addr
;
2211 unsigned int info
= sechdrs
[i
].sh_info
;
2213 /* Not a valid relocation section? */
2214 if (info
>= hdr
->e_shnum
)
2217 /* Don't bother with non-allocated sections */
2218 if (!(sechdrs
[info
].sh_flags
& SHF_ALLOC
))
2221 if (sechdrs
[i
].sh_type
== SHT_REL
)
2222 err
= apply_relocate(sechdrs
, strtab
, symindex
, i
,mod
);
2223 else if (sechdrs
[i
].sh_type
== SHT_RELA
)
2224 err
= apply_relocate_add(sechdrs
, strtab
, symindex
, i
,
2230 /* Find duplicate symbols */
2231 err
= verify_export_symbols(mod
);
2235 /* Set up and sort exception table */
2236 mod
->extable
= section_objs(hdr
, sechdrs
, secstrings
, "__ex_table",
2237 sizeof(*mod
->extable
), &mod
->num_exentries
);
2238 sort_extable(mod
->extable
, mod
->extable
+ mod
->num_exentries
);
2240 /* Finally, copy percpu area over. */
2241 percpu_modcopy(mod
->percpu
, (void *)sechdrs
[pcpuindex
].sh_addr
,
2242 sechdrs
[pcpuindex
].sh_size
);
2244 add_kallsyms(mod
, sechdrs
, symindex
, strindex
, secstrings
);
2247 struct _ddebug
*debug
;
2248 unsigned int num_debug
;
2250 debug
= section_objs(hdr
, sechdrs
, secstrings
, "__verbose",
2251 sizeof(*debug
), &num_debug
);
2253 dynamic_debug_setup(debug
, num_debug
);
2256 /* sechdrs[0].sh_size is always zero */
2257 mseg
= section_objs(hdr
, sechdrs
, secstrings
, "__mcount_loc",
2258 sizeof(*mseg
), &num_mcount
);
2259 ftrace_init_module(mod
, mseg
, mseg
+ num_mcount
);
2261 err
= module_finalize(hdr
, sechdrs
, mod
);
2265 /* flush the icache in correct context */
2270 * Flush the instruction cache, since we've played with text.
2271 * Do it before processing of module parameters, so the module
2272 * can provide parameter accessor functions of its own.
2274 if (mod
->module_init
)
2275 flush_icache_range((unsigned long)mod
->module_init
,
2276 (unsigned long)mod
->module_init
2278 flush_icache_range((unsigned long)mod
->module_core
,
2279 (unsigned long)mod
->module_core
+ mod
->core_size
);
2284 if (section_addr(hdr
, sechdrs
, secstrings
, "__obsparm"))
2285 printk(KERN_WARNING
"%s: Ignoring obsolete parameters\n",
2288 /* Now sew it into the lists so we can get lockdep and oops
2289 * info during argument parsing. Noone should access us, since
2290 * strong_try_module_get() will fail.
2291 * lockdep/oops can run asynchronous, so use the RCU list insertion
2292 * function to insert in a way safe to concurrent readers.
2293 * The mutex protects against concurrent writers.
2295 list_add_rcu(&mod
->list
, &modules
);
2297 err
= parse_args(mod
->name
, mod
->args
, kp
, num_kp
, NULL
);
2301 err
= mod_sysfs_setup(mod
, kp
, num_kp
);
2304 add_sect_attrs(mod
, hdr
->e_shnum
, secstrings
, sechdrs
);
2305 add_notes_attrs(mod
, hdr
->e_shnum
, secstrings
, sechdrs
);
2307 /* Get rid of temporary copy */
2310 stop_machine_destroy();
2315 stop_machine(__unlink_module
, mod
, NULL
);
2316 module_arch_cleanup(mod
);
2318 kobject_del(&mod
->mkobj
.kobj
);
2319 kobject_put(&mod
->mkobj
.kobj
);
2320 ftrace_release(mod
->module_core
, mod
->core_size
);
2322 module_unload_free(mod
);
2324 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2325 percpu_modfree(mod
->refptr
);
2327 module_free(mod
, mod
->module_init
);
2329 module_free(mod
, mod
->module_core
);
2330 /* mod will be freed with core. Don't access it beyond this line! */
2333 percpu_modfree(percpu
);
2338 stop_machine_destroy();
2339 return ERR_PTR(err
);
2342 printk(KERN_ERR
"Module len %lu truncated\n", len
);
2347 /* This is where the real work happens */
2348 SYSCALL_DEFINE3(init_module
, void __user
*, umod
,
2349 unsigned long, len
, const char __user
*, uargs
)
2354 /* Must have permission */
2355 if (!capable(CAP_SYS_MODULE
) || modules_disabled
)
2358 /* Only one module load at a time, please */
2359 if (mutex_lock_interruptible(&module_mutex
) != 0)
2362 /* Do all the hard work */
2363 mod
= load_module(umod
, len
, uargs
);
2365 mutex_unlock(&module_mutex
);
2366 return PTR_ERR(mod
);
2369 /* Drop lock so they can recurse */
2370 mutex_unlock(&module_mutex
);
2372 blocking_notifier_call_chain(&module_notify_list
,
2373 MODULE_STATE_COMING
, mod
);
2375 /* Start the module */
2376 if (mod
->init
!= NULL
)
2377 ret
= do_one_initcall(mod
->init
);
2379 /* Init routine failed: abort. Try to protect us from
2380 buggy refcounters. */
2381 mod
->state
= MODULE_STATE_GOING
;
2382 synchronize_sched();
2384 blocking_notifier_call_chain(&module_notify_list
,
2385 MODULE_STATE_GOING
, mod
);
2386 mutex_lock(&module_mutex
);
2388 mutex_unlock(&module_mutex
);
2389 wake_up(&module_wq
);
2393 printk(KERN_WARNING
"%s: '%s'->init suspiciously returned %d, "
2394 "it should follow 0/-E convention\n"
2395 KERN_WARNING
"%s: loading module anyway...\n",
2396 __func__
, mod
->name
, ret
,
2401 /* Now it's a first class citizen! Wake up anyone waiting for it. */
2402 mod
->state
= MODULE_STATE_LIVE
;
2403 wake_up(&module_wq
);
2404 blocking_notifier_call_chain(&module_notify_list
,
2405 MODULE_STATE_LIVE
, mod
);
2407 mutex_lock(&module_mutex
);
2408 /* Drop initial reference. */
2410 module_free(mod
, mod
->module_init
);
2411 mod
->module_init
= NULL
;
2413 mod
->init_text_size
= 0;
2414 mutex_unlock(&module_mutex
);
2419 static inline int within(unsigned long addr
, void *start
, unsigned long size
)
2421 return ((void *)addr
>= start
&& (void *)addr
< start
+ size
);
2424 #ifdef CONFIG_KALLSYMS
2426 * This ignores the intensely annoying "mapping symbols" found
2427 * in ARM ELF files: $a, $t and $d.
2429 static inline int is_arm_mapping_symbol(const char *str
)
2431 return str
[0] == '$' && strchr("atd", str
[1])
2432 && (str
[2] == '\0' || str
[2] == '.');
2435 static const char *get_ksymbol(struct module
*mod
,
2437 unsigned long *size
,
2438 unsigned long *offset
)
2440 unsigned int i
, best
= 0;
2441 unsigned long nextval
;
2443 /* At worse, next value is at end of module */
2444 if (within_module_init(addr
, mod
))
2445 nextval
= (unsigned long)mod
->module_init
+mod
->init_text_size
;
2447 nextval
= (unsigned long)mod
->module_core
+mod
->core_text_size
;
2449 /* Scan for closest preceeding symbol, and next symbol. (ELF
2450 starts real symbols at 1). */
2451 for (i
= 1; i
< mod
->num_symtab
; i
++) {
2452 if (mod
->symtab
[i
].st_shndx
== SHN_UNDEF
)
2455 /* We ignore unnamed symbols: they're uninformative
2456 * and inserted at a whim. */
2457 if (mod
->symtab
[i
].st_value
<= addr
2458 && mod
->symtab
[i
].st_value
> mod
->symtab
[best
].st_value
2459 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
2460 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
2462 if (mod
->symtab
[i
].st_value
> addr
2463 && mod
->symtab
[i
].st_value
< nextval
2464 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
2465 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
2466 nextval
= mod
->symtab
[i
].st_value
;
2473 *size
= nextval
- mod
->symtab
[best
].st_value
;
2475 *offset
= addr
- mod
->symtab
[best
].st_value
;
2476 return mod
->strtab
+ mod
->symtab
[best
].st_name
;
2479 /* For kallsyms to ask for address resolution. NULL means not found. Careful
2480 * not to lock to avoid deadlock on oopses, simply disable preemption. */
2481 const char *module_address_lookup(unsigned long addr
,
2482 unsigned long *size
,
2483 unsigned long *offset
,
2488 const char *ret
= NULL
;
2491 list_for_each_entry_rcu(mod
, &modules
, list
) {
2492 if (within_module_init(addr
, mod
) ||
2493 within_module_core(addr
, mod
)) {
2495 *modname
= mod
->name
;
2496 ret
= get_ksymbol(mod
, addr
, size
, offset
);
2500 /* Make a copy in here where it's safe */
2502 strncpy(namebuf
, ret
, KSYM_NAME_LEN
- 1);
2509 int lookup_module_symbol_name(unsigned long addr
, char *symname
)
2514 list_for_each_entry_rcu(mod
, &modules
, list
) {
2515 if (within_module_init(addr
, mod
) ||
2516 within_module_core(addr
, mod
)) {
2519 sym
= get_ksymbol(mod
, addr
, NULL
, NULL
);
2522 strlcpy(symname
, sym
, KSYM_NAME_LEN
);
2532 int lookup_module_symbol_attrs(unsigned long addr
, unsigned long *size
,
2533 unsigned long *offset
, char *modname
, char *name
)
2538 list_for_each_entry_rcu(mod
, &modules
, list
) {
2539 if (within_module_init(addr
, mod
) ||
2540 within_module_core(addr
, mod
)) {
2543 sym
= get_ksymbol(mod
, addr
, size
, offset
);
2547 strlcpy(modname
, mod
->name
, MODULE_NAME_LEN
);
2549 strlcpy(name
, sym
, KSYM_NAME_LEN
);
2559 int module_get_kallsym(unsigned int symnum
, unsigned long *value
, char *type
,
2560 char *name
, char *module_name
, int *exported
)
2565 list_for_each_entry_rcu(mod
, &modules
, list
) {
2566 if (symnum
< mod
->num_symtab
) {
2567 *value
= mod
->symtab
[symnum
].st_value
;
2568 *type
= mod
->symtab
[symnum
].st_info
;
2569 strlcpy(name
, mod
->strtab
+ mod
->symtab
[symnum
].st_name
,
2571 strlcpy(module_name
, mod
->name
, MODULE_NAME_LEN
);
2572 *exported
= is_exported(name
, *value
, mod
);
2576 symnum
-= mod
->num_symtab
;
2582 static unsigned long mod_find_symname(struct module
*mod
, const char *name
)
2586 for (i
= 0; i
< mod
->num_symtab
; i
++)
2587 if (strcmp(name
, mod
->strtab
+mod
->symtab
[i
].st_name
) == 0 &&
2588 mod
->symtab
[i
].st_info
!= 'U')
2589 return mod
->symtab
[i
].st_value
;
2593 /* Look for this name: can be of form module:name. */
2594 unsigned long module_kallsyms_lookup_name(const char *name
)
2598 unsigned long ret
= 0;
2600 /* Don't lock: we're in enough trouble already. */
2602 if ((colon
= strchr(name
, ':')) != NULL
) {
2604 if ((mod
= find_module(name
)) != NULL
)
2605 ret
= mod_find_symname(mod
, colon
+1);
2608 list_for_each_entry_rcu(mod
, &modules
, list
)
2609 if ((ret
= mod_find_symname(mod
, name
)) != 0)
2615 #endif /* CONFIG_KALLSYMS */
2617 static char *module_flags(struct module
*mod
, char *buf
)
2622 mod
->state
== MODULE_STATE_GOING
||
2623 mod
->state
== MODULE_STATE_COMING
) {
2625 if (mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
))
2627 if (mod
->taints
& (1 << TAINT_FORCED_MODULE
))
2629 if (mod
->taints
& (1 << TAINT_CRAP
))
2632 * TAINT_FORCED_RMMOD: could be added.
2633 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2637 /* Show a - for module-is-being-unloaded */
2638 if (mod
->state
== MODULE_STATE_GOING
)
2640 /* Show a + for module-is-being-loaded */
2641 if (mod
->state
== MODULE_STATE_COMING
)
2650 #ifdef CONFIG_PROC_FS
2651 /* Called by the /proc file system to return a list of modules. */
2652 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
2654 mutex_lock(&module_mutex
);
2655 return seq_list_start(&modules
, *pos
);
2658 static void *m_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
2660 return seq_list_next(p
, &modules
, pos
);
2663 static void m_stop(struct seq_file
*m
, void *p
)
2665 mutex_unlock(&module_mutex
);
2668 static int m_show(struct seq_file
*m
, void *p
)
2670 struct module
*mod
= list_entry(p
, struct module
, list
);
2673 seq_printf(m
, "%s %u",
2674 mod
->name
, mod
->init_size
+ mod
->core_size
);
2675 print_unload_info(m
, mod
);
2677 /* Informative for users. */
2678 seq_printf(m
, " %s",
2679 mod
->state
== MODULE_STATE_GOING
? "Unloading":
2680 mod
->state
== MODULE_STATE_COMING
? "Loading":
2682 /* Used by oprofile and other similar tools. */
2683 seq_printf(m
, " 0x%p", mod
->module_core
);
2687 seq_printf(m
, " %s", module_flags(mod
, buf
));
2689 seq_printf(m
, "\n");
2693 /* Format: modulename size refcount deps address
2695 Where refcount is a number or -, and deps is a comma-separated list
2698 static const struct seq_operations modules_op
= {
2705 static int modules_open(struct inode
*inode
, struct file
*file
)
2707 return seq_open(file
, &modules_op
);
2710 static const struct file_operations proc_modules_operations
= {
2711 .open
= modules_open
,
2713 .llseek
= seq_lseek
,
2714 .release
= seq_release
,
2717 static int __init
proc_modules_init(void)
2719 proc_create("modules", 0, NULL
, &proc_modules_operations
);
2722 module_init(proc_modules_init
);
2725 /* Given an address, look for it in the module exception tables. */
2726 const struct exception_table_entry
*search_module_extables(unsigned long addr
)
2728 const struct exception_table_entry
*e
= NULL
;
2732 list_for_each_entry_rcu(mod
, &modules
, list
) {
2733 if (mod
->num_exentries
== 0)
2736 e
= search_extable(mod
->extable
,
2737 mod
->extable
+ mod
->num_exentries
- 1,
2744 /* Now, if we found one, we are running inside it now, hence
2745 we cannot unload the module, hence no refcnt needed. */
2750 * Is this a valid module address?
2752 int is_module_address(unsigned long addr
)
2758 list_for_each_entry_rcu(mod
, &modules
, list
) {
2759 if (within_module_core(addr
, mod
)) {
2771 /* Is this a valid kernel address? */
2772 __notrace_funcgraph
struct module
*__module_text_address(unsigned long addr
)
2776 if (addr
< module_addr_min
|| addr
> module_addr_max
)
2779 list_for_each_entry_rcu(mod
, &modules
, list
)
2780 if (within(addr
, mod
->module_init
, mod
->init_text_size
)
2781 || within(addr
, mod
->module_core
, mod
->core_text_size
))
2786 struct module
*module_text_address(unsigned long addr
)
2791 mod
= __module_text_address(addr
);
2797 /* Don't grab lock, we're oopsing. */
2798 void print_modules(void)
2803 printk("Modules linked in:");
2804 /* Most callers should already have preempt disabled, but make sure */
2806 list_for_each_entry_rcu(mod
, &modules
, list
)
2807 printk(" %s%s", mod
->name
, module_flags(mod
, buf
));
2809 if (last_unloaded_module
[0])
2810 printk(" [last unloaded: %s]", last_unloaded_module
);
2814 #ifdef CONFIG_MODVERSIONS
2815 /* Generate the signature for struct module here, too, for modversions. */
2816 void struct_module(struct module
*mod
) { return; }
2817 EXPORT_SYMBOL(struct_module
);
2820 #ifdef CONFIG_MARKERS
2821 void module_update_markers(void)
2825 mutex_lock(&module_mutex
);
2826 list_for_each_entry(mod
, &modules
, list
)
2828 marker_update_probe_range(mod
->markers
,
2829 mod
->markers
+ mod
->num_markers
);
2830 mutex_unlock(&module_mutex
);
2834 #ifdef CONFIG_TRACEPOINTS
2835 void module_update_tracepoints(void)
2839 mutex_lock(&module_mutex
);
2840 list_for_each_entry(mod
, &modules
, list
)
2842 tracepoint_update_probe_range(mod
->tracepoints
,
2843 mod
->tracepoints
+ mod
->num_tracepoints
);
2844 mutex_unlock(&module_mutex
);
2848 * Returns 0 if current not found.
2849 * Returns 1 if current found.
2851 int module_get_iter_tracepoints(struct tracepoint_iter
*iter
)
2853 struct module
*iter_mod
;
2856 mutex_lock(&module_mutex
);
2857 list_for_each_entry(iter_mod
, &modules
, list
) {
2858 if (!iter_mod
->taints
) {
2860 * Sorted module list
2862 if (iter_mod
< iter
->module
)
2864 else if (iter_mod
> iter
->module
)
2865 iter
->tracepoint
= NULL
;
2866 found
= tracepoint_get_iter_range(&iter
->tracepoint
,
2867 iter_mod
->tracepoints
,
2868 iter_mod
->tracepoints
2869 + iter_mod
->num_tracepoints
);
2871 iter
->module
= iter_mod
;
2876 mutex_unlock(&module_mutex
);