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 DEFINE_MUTEX(module_mutex
);
72 EXPORT_SYMBOL_GPL(module_mutex
);
73 static LIST_HEAD(modules
);
75 /* Waiting for a module to finish initializing? */
76 static DECLARE_WAIT_QUEUE_HEAD(module_wq
);
78 static BLOCKING_NOTIFIER_HEAD(module_notify_list
);
80 /* Bounds of module allocation, for speeding __module_address */
81 static unsigned long module_addr_min
= -1UL, module_addr_max
= 0;
83 int register_module_notifier(struct notifier_block
* nb
)
85 return blocking_notifier_chain_register(&module_notify_list
, nb
);
87 EXPORT_SYMBOL(register_module_notifier
);
89 int unregister_module_notifier(struct notifier_block
* nb
)
91 return blocking_notifier_chain_unregister(&module_notify_list
, nb
);
93 EXPORT_SYMBOL(unregister_module_notifier
);
95 /* We require a truly strong try_module_get(): 0 means failure due to
96 ongoing or failed initialization etc. */
97 static inline int strong_try_module_get(struct module
*mod
)
99 if (mod
&& mod
->state
== MODULE_STATE_COMING
)
101 if (try_module_get(mod
))
107 static inline void add_taint_module(struct module
*mod
, unsigned flag
)
110 mod
->taints
|= (1U << flag
);
114 * A thread that wants to hold a reference to a module only while it
115 * is running can call this to safely exit. nfsd and lockd use this.
117 void __module_put_and_exit(struct module
*mod
, long code
)
122 EXPORT_SYMBOL(__module_put_and_exit
);
124 /* Find a module section: 0 means not found. */
125 static unsigned int find_sec(Elf_Ehdr
*hdr
,
127 const char *secstrings
,
132 for (i
= 1; i
< hdr
->e_shnum
; i
++)
133 /* Alloc bit cleared means "ignore it." */
134 if ((sechdrs
[i
].sh_flags
& SHF_ALLOC
)
135 && strcmp(secstrings
+sechdrs
[i
].sh_name
, name
) == 0)
140 /* Find a module section, or NULL. */
141 static void *section_addr(Elf_Ehdr
*hdr
, Elf_Shdr
*shdrs
,
142 const char *secstrings
, const char *name
)
144 /* Section 0 has sh_addr 0. */
145 return (void *)shdrs
[find_sec(hdr
, shdrs
, secstrings
, name
)].sh_addr
;
148 /* Find a module section, or NULL. Fill in number of "objects" in section. */
149 static void *section_objs(Elf_Ehdr
*hdr
,
151 const char *secstrings
,
156 unsigned int sec
= find_sec(hdr
, sechdrs
, secstrings
, name
);
158 /* Section 0 has sh_addr 0 and sh_size 0. */
159 *num
= sechdrs
[sec
].sh_size
/ object_size
;
160 return (void *)sechdrs
[sec
].sh_addr
;
163 /* Provided by the linker */
164 extern const struct kernel_symbol __start___ksymtab
[];
165 extern const struct kernel_symbol __stop___ksymtab
[];
166 extern const struct kernel_symbol __start___ksymtab_gpl
[];
167 extern const struct kernel_symbol __stop___ksymtab_gpl
[];
168 extern const struct kernel_symbol __start___ksymtab_gpl_future
[];
169 extern const struct kernel_symbol __stop___ksymtab_gpl_future
[];
170 extern const struct kernel_symbol __start___ksymtab_gpl_future
[];
171 extern const struct kernel_symbol __stop___ksymtab_gpl_future
[];
172 extern const unsigned long __start___kcrctab
[];
173 extern const unsigned long __start___kcrctab_gpl
[];
174 extern const unsigned long __start___kcrctab_gpl_future
[];
175 #ifdef CONFIG_UNUSED_SYMBOLS
176 extern const struct kernel_symbol __start___ksymtab_unused
[];
177 extern const struct kernel_symbol __stop___ksymtab_unused
[];
178 extern const struct kernel_symbol __start___ksymtab_unused_gpl
[];
179 extern const struct kernel_symbol __stop___ksymtab_unused_gpl
[];
180 extern const unsigned long __start___kcrctab_unused
[];
181 extern const unsigned long __start___kcrctab_unused_gpl
[];
184 #ifndef CONFIG_MODVERSIONS
185 #define symversion(base, idx) NULL
187 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
190 static bool each_symbol_in_section(const struct symsearch
*arr
,
191 unsigned int arrsize
,
192 struct module
*owner
,
193 bool (*fn
)(const struct symsearch
*syms
,
194 struct module
*owner
,
195 unsigned int symnum
, void *data
),
200 for (j
= 0; j
< arrsize
; j
++) {
201 for (i
= 0; i
< arr
[j
].stop
- arr
[j
].start
; i
++)
202 if (fn(&arr
[j
], owner
, i
, data
))
209 /* Returns true as soon as fn returns true, otherwise false. */
210 bool each_symbol(bool (*fn
)(const struct symsearch
*arr
, struct module
*owner
,
211 unsigned int symnum
, void *data
), void *data
)
214 const struct symsearch arr
[] = {
215 { __start___ksymtab
, __stop___ksymtab
, __start___kcrctab
,
216 NOT_GPL_ONLY
, false },
217 { __start___ksymtab_gpl
, __stop___ksymtab_gpl
,
218 __start___kcrctab_gpl
,
220 { __start___ksymtab_gpl_future
, __stop___ksymtab_gpl_future
,
221 __start___kcrctab_gpl_future
,
222 WILL_BE_GPL_ONLY
, false },
223 #ifdef CONFIG_UNUSED_SYMBOLS
224 { __start___ksymtab_unused
, __stop___ksymtab_unused
,
225 __start___kcrctab_unused
,
226 NOT_GPL_ONLY
, true },
227 { __start___ksymtab_unused_gpl
, __stop___ksymtab_unused_gpl
,
228 __start___kcrctab_unused_gpl
,
233 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), NULL
, fn
, data
))
236 list_for_each_entry_rcu(mod
, &modules
, list
) {
237 struct symsearch arr
[] = {
238 { mod
->syms
, mod
->syms
+ mod
->num_syms
, mod
->crcs
,
239 NOT_GPL_ONLY
, false },
240 { mod
->gpl_syms
, mod
->gpl_syms
+ mod
->num_gpl_syms
,
243 { mod
->gpl_future_syms
,
244 mod
->gpl_future_syms
+ mod
->num_gpl_future_syms
,
245 mod
->gpl_future_crcs
,
246 WILL_BE_GPL_ONLY
, false },
247 #ifdef CONFIG_UNUSED_SYMBOLS
249 mod
->unused_syms
+ mod
->num_unused_syms
,
251 NOT_GPL_ONLY
, true },
252 { mod
->unused_gpl_syms
,
253 mod
->unused_gpl_syms
+ mod
->num_unused_gpl_syms
,
254 mod
->unused_gpl_crcs
,
259 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), mod
, fn
, data
))
264 EXPORT_SYMBOL_GPL(each_symbol
);
266 struct find_symbol_arg
{
273 struct module
*owner
;
274 const unsigned long *crc
;
275 const struct kernel_symbol
*sym
;
278 static bool find_symbol_in_section(const struct symsearch
*syms
,
279 struct module
*owner
,
280 unsigned int symnum
, void *data
)
282 struct find_symbol_arg
*fsa
= data
;
284 if (strcmp(syms
->start
[symnum
].name
, fsa
->name
) != 0)
288 if (syms
->licence
== GPL_ONLY
)
290 if (syms
->licence
== WILL_BE_GPL_ONLY
&& fsa
->warn
) {
291 printk(KERN_WARNING
"Symbol %s is being used "
292 "by a non-GPL module, which will not "
293 "be allowed in the future\n", fsa
->name
);
294 printk(KERN_WARNING
"Please see the file "
295 "Documentation/feature-removal-schedule.txt "
296 "in the kernel source tree for more details.\n");
300 #ifdef CONFIG_UNUSED_SYMBOLS
301 if (syms
->unused
&& fsa
->warn
) {
302 printk(KERN_WARNING
"Symbol %s is marked as UNUSED, "
303 "however this module is using it.\n", fsa
->name
);
305 "This symbol will go away in the future.\n");
307 "Please evalute if this is the right api to use and if "
308 "it really is, submit a report the linux kernel "
309 "mailinglist together with submitting your code for "
315 fsa
->crc
= symversion(syms
->crcs
, symnum
);
316 fsa
->sym
= &syms
->start
[symnum
];
320 /* Find a symbol and return it, along with, (optional) crc and
321 * (optional) module which owns it */
322 const struct kernel_symbol
*find_symbol(const char *name
,
323 struct module
**owner
,
324 const unsigned long **crc
,
328 struct find_symbol_arg fsa
;
334 if (each_symbol(find_symbol_in_section
, &fsa
)) {
342 DEBUGP("Failed to find symbol %s\n", name
);
345 EXPORT_SYMBOL_GPL(find_symbol
);
347 /* Search for module by name: must hold module_mutex. */
348 struct module
*find_module(const char *name
)
352 list_for_each_entry(mod
, &modules
, list
) {
353 if (strcmp(mod
->name
, name
) == 0)
358 EXPORT_SYMBOL_GPL(find_module
);
362 #ifdef CONFIG_HAVE_DYNAMIC_PER_CPU_AREA
364 static void *percpu_modalloc(unsigned long size
, unsigned long align
,
369 if (align
> PAGE_SIZE
) {
370 printk(KERN_WARNING
"%s: per-cpu alignment %li > %li\n",
371 name
, align
, PAGE_SIZE
);
375 ptr
= __alloc_reserved_percpu(size
, align
);
378 "Could not allocate %lu bytes percpu data\n", size
);
382 static void percpu_modfree(void *freeme
)
387 #else /* ... !CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
389 /* Number of blocks used and allocated. */
390 static unsigned int pcpu_num_used
, pcpu_num_allocated
;
391 /* Size of each block. -ve means used. */
392 static int *pcpu_size
;
394 static int split_block(unsigned int i
, unsigned short size
)
396 /* Reallocation required? */
397 if (pcpu_num_used
+ 1 > pcpu_num_allocated
) {
400 new = krealloc(pcpu_size
, sizeof(new[0])*pcpu_num_allocated
*2,
405 pcpu_num_allocated
*= 2;
409 /* Insert a new subblock */
410 memmove(&pcpu_size
[i
+1], &pcpu_size
[i
],
411 sizeof(pcpu_size
[0]) * (pcpu_num_used
- i
));
414 pcpu_size
[i
+1] -= size
;
419 static inline unsigned int block_size(int val
)
426 static void *percpu_modalloc(unsigned long size
, unsigned long align
,
433 if (align
> PAGE_SIZE
) {
434 printk(KERN_WARNING
"%s: per-cpu alignment %li > %li\n",
435 name
, align
, PAGE_SIZE
);
439 ptr
= __per_cpu_start
;
440 for (i
= 0; i
< pcpu_num_used
; ptr
+= block_size(pcpu_size
[i
]), i
++) {
441 /* Extra for alignment requirement. */
442 extra
= ALIGN((unsigned long)ptr
, align
) - (unsigned long)ptr
;
443 BUG_ON(i
== 0 && extra
!= 0);
445 if (pcpu_size
[i
] < 0 || pcpu_size
[i
] < extra
+ size
)
448 /* Transfer extra to previous block. */
449 if (pcpu_size
[i
-1] < 0)
450 pcpu_size
[i
-1] -= extra
;
452 pcpu_size
[i
-1] += extra
;
453 pcpu_size
[i
] -= extra
;
456 /* Split block if warranted */
457 if (pcpu_size
[i
] - size
> sizeof(unsigned long))
458 if (!split_block(i
, size
))
462 pcpu_size
[i
] = -pcpu_size
[i
];
466 printk(KERN_WARNING
"Could not allocate %lu bytes percpu data\n",
471 static void percpu_modfree(void *freeme
)
474 void *ptr
= __per_cpu_start
+ block_size(pcpu_size
[0]);
476 /* First entry is core kernel percpu data. */
477 for (i
= 1; i
< pcpu_num_used
; ptr
+= block_size(pcpu_size
[i
]), i
++) {
479 pcpu_size
[i
] = -pcpu_size
[i
];
486 /* Merge with previous? */
487 if (pcpu_size
[i
-1] >= 0) {
488 pcpu_size
[i
-1] += pcpu_size
[i
];
490 memmove(&pcpu_size
[i
], &pcpu_size
[i
+1],
491 (pcpu_num_used
- i
) * sizeof(pcpu_size
[0]));
494 /* Merge with next? */
495 if (i
+1 < pcpu_num_used
&& pcpu_size
[i
+1] >= 0) {
496 pcpu_size
[i
] += pcpu_size
[i
+1];
498 memmove(&pcpu_size
[i
+1], &pcpu_size
[i
+2],
499 (pcpu_num_used
- (i
+1)) * sizeof(pcpu_size
[0]));
503 static int percpu_modinit(void)
506 pcpu_num_allocated
= 2;
507 pcpu_size
= kmalloc(sizeof(pcpu_size
[0]) * pcpu_num_allocated
,
509 /* Static in-kernel percpu data (used). */
510 pcpu_size
[0] = -(__per_cpu_end
-__per_cpu_start
);
512 pcpu_size
[1] = PERCPU_ENOUGH_ROOM
+ pcpu_size
[0];
513 if (pcpu_size
[1] < 0) {
514 printk(KERN_ERR
"No per-cpu room for modules.\n");
520 __initcall(percpu_modinit
);
522 #endif /* CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
524 static unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
526 const char *secstrings
)
528 return find_sec(hdr
, sechdrs
, secstrings
, ".data.percpu");
531 static void percpu_modcopy(void *pcpudest
, const void *from
, unsigned long size
)
535 for_each_possible_cpu(cpu
)
536 memcpy(pcpudest
+ per_cpu_offset(cpu
), from
, size
);
539 #else /* ... !CONFIG_SMP */
541 static inline void *percpu_modalloc(unsigned long size
, unsigned long align
,
546 static inline void percpu_modfree(void *pcpuptr
)
550 static inline unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
552 const char *secstrings
)
556 static inline void percpu_modcopy(void *pcpudst
, const void *src
,
559 /* pcpusec should be 0, and size of that section should be 0. */
563 #endif /* CONFIG_SMP */
565 #define MODINFO_ATTR(field) \
566 static void setup_modinfo_##field(struct module *mod, const char *s) \
568 mod->field = kstrdup(s, GFP_KERNEL); \
570 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
571 struct module *mod, char *buffer) \
573 return sprintf(buffer, "%s\n", mod->field); \
575 static int modinfo_##field##_exists(struct module *mod) \
577 return mod->field != NULL; \
579 static void free_modinfo_##field(struct module *mod) \
584 static struct module_attribute modinfo_##field = { \
585 .attr = { .name = __stringify(field), .mode = 0444 }, \
586 .show = show_modinfo_##field, \
587 .setup = setup_modinfo_##field, \
588 .test = modinfo_##field##_exists, \
589 .free = free_modinfo_##field, \
592 MODINFO_ATTR(version
);
593 MODINFO_ATTR(srcversion
);
595 static char last_unloaded_module
[MODULE_NAME_LEN
+1];
597 #ifdef CONFIG_MODULE_UNLOAD
598 /* Init the unload section of the module. */
599 static void module_unload_init(struct module
*mod
)
603 INIT_LIST_HEAD(&mod
->modules_which_use_me
);
604 for_each_possible_cpu(cpu
)
605 local_set(__module_ref_addr(mod
, cpu
), 0);
606 /* Hold reference count during initialization. */
607 local_set(__module_ref_addr(mod
, raw_smp_processor_id()), 1);
608 /* Backwards compatibility macros put refcount during init. */
609 mod
->waiter
= current
;
612 /* modules using other modules */
615 struct list_head list
;
616 struct module
*module_which_uses
;
619 /* Does a already use b? */
620 static int already_uses(struct module
*a
, struct module
*b
)
622 struct module_use
*use
;
624 list_for_each_entry(use
, &b
->modules_which_use_me
, list
) {
625 if (use
->module_which_uses
== a
) {
626 DEBUGP("%s uses %s!\n", a
->name
, b
->name
);
630 DEBUGP("%s does not use %s!\n", a
->name
, b
->name
);
634 /* Module a uses b */
635 int use_module(struct module
*a
, struct module
*b
)
637 struct module_use
*use
;
640 if (b
== NULL
|| already_uses(a
, b
)) return 1;
642 /* If we're interrupted or time out, we fail. */
643 if (wait_event_interruptible_timeout(
644 module_wq
, (err
= strong_try_module_get(b
)) != -EBUSY
,
646 printk("%s: gave up waiting for init of module %s.\n",
651 /* If strong_try_module_get() returned a different error, we fail. */
655 DEBUGP("Allocating new usage for %s.\n", a
->name
);
656 use
= kmalloc(sizeof(*use
), GFP_ATOMIC
);
658 printk("%s: out of memory loading\n", a
->name
);
663 use
->module_which_uses
= a
;
664 list_add(&use
->list
, &b
->modules_which_use_me
);
665 no_warn
= sysfs_create_link(b
->holders_dir
, &a
->mkobj
.kobj
, a
->name
);
668 EXPORT_SYMBOL_GPL(use_module
);
670 /* Clear the unload stuff of the module. */
671 static void module_unload_free(struct module
*mod
)
675 list_for_each_entry(i
, &modules
, list
) {
676 struct module_use
*use
;
678 list_for_each_entry(use
, &i
->modules_which_use_me
, list
) {
679 if (use
->module_which_uses
== mod
) {
680 DEBUGP("%s unusing %s\n", mod
->name
, i
->name
);
682 list_del(&use
->list
);
684 sysfs_remove_link(i
->holders_dir
, mod
->name
);
685 /* There can be at most one match. */
692 #ifdef CONFIG_MODULE_FORCE_UNLOAD
693 static inline int try_force_unload(unsigned int flags
)
695 int ret
= (flags
& O_TRUNC
);
697 add_taint(TAINT_FORCED_RMMOD
);
701 static inline int try_force_unload(unsigned int flags
)
705 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
714 /* Whole machine is stopped with interrupts off when this runs. */
715 static int __try_stop_module(void *_sref
)
717 struct stopref
*sref
= _sref
;
719 /* If it's not unused, quit unless we're forcing. */
720 if (module_refcount(sref
->mod
) != 0) {
721 if (!(*sref
->forced
= try_force_unload(sref
->flags
)))
725 /* Mark it as dying. */
726 sref
->mod
->state
= MODULE_STATE_GOING
;
730 static int try_stop_module(struct module
*mod
, int flags
, int *forced
)
732 if (flags
& O_NONBLOCK
) {
733 struct stopref sref
= { mod
, flags
, forced
};
735 return stop_machine(__try_stop_module
, &sref
, NULL
);
737 /* We don't need to stop the machine for this. */
738 mod
->state
= MODULE_STATE_GOING
;
744 unsigned int module_refcount(struct module
*mod
)
746 unsigned int total
= 0;
749 for_each_possible_cpu(cpu
)
750 total
+= local_read(__module_ref_addr(mod
, cpu
));
753 EXPORT_SYMBOL(module_refcount
);
755 /* This exists whether we can unload or not */
756 static void free_module(struct module
*mod
);
758 static void wait_for_zero_refcount(struct module
*mod
)
760 /* Since we might sleep for some time, release the mutex first */
761 mutex_unlock(&module_mutex
);
763 DEBUGP("Looking at refcount...\n");
764 set_current_state(TASK_UNINTERRUPTIBLE
);
765 if (module_refcount(mod
) == 0)
769 current
->state
= TASK_RUNNING
;
770 mutex_lock(&module_mutex
);
773 SYSCALL_DEFINE2(delete_module
, const char __user
*, name_user
,
777 char name
[MODULE_NAME_LEN
];
780 if (!capable(CAP_SYS_MODULE
))
783 if (strncpy_from_user(name
, name_user
, MODULE_NAME_LEN
-1) < 0)
785 name
[MODULE_NAME_LEN
-1] = '\0';
787 /* Create stop_machine threads since free_module relies on
788 * a non-failing stop_machine call. */
789 ret
= stop_machine_create();
793 if (mutex_lock_interruptible(&module_mutex
) != 0) {
798 mod
= find_module(name
);
804 if (!list_empty(&mod
->modules_which_use_me
)) {
805 /* Other modules depend on us: get rid of them first. */
810 /* Doing init or already dying? */
811 if (mod
->state
!= MODULE_STATE_LIVE
) {
812 /* FIXME: if (force), slam module count and wake up
814 DEBUGP("%s already dying\n", mod
->name
);
819 /* If it has an init func, it must have an exit func to unload */
820 if (mod
->init
&& !mod
->exit
) {
821 forced
= try_force_unload(flags
);
823 /* This module can't be removed */
829 /* Set this up before setting mod->state */
830 mod
->waiter
= current
;
832 /* Stop the machine so refcounts can't move and disable module. */
833 ret
= try_stop_module(mod
, flags
, &forced
);
837 /* Never wait if forced. */
838 if (!forced
&& module_refcount(mod
) != 0)
839 wait_for_zero_refcount(mod
);
841 mutex_unlock(&module_mutex
);
842 /* Final destruction now noone is using it. */
843 if (mod
->exit
!= NULL
)
845 blocking_notifier_call_chain(&module_notify_list
,
846 MODULE_STATE_GOING
, mod
);
847 async_synchronize_full();
848 mutex_lock(&module_mutex
);
849 /* Store the name of the last unloaded module for diagnostic purposes */
850 strlcpy(last_unloaded_module
, mod
->name
, sizeof(last_unloaded_module
));
851 ddebug_remove_module(mod
->name
);
855 mutex_unlock(&module_mutex
);
857 stop_machine_destroy();
861 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
863 struct module_use
*use
;
864 int printed_something
= 0;
866 seq_printf(m
, " %u ", module_refcount(mod
));
868 /* Always include a trailing , so userspace can differentiate
869 between this and the old multi-field proc format. */
870 list_for_each_entry(use
, &mod
->modules_which_use_me
, list
) {
871 printed_something
= 1;
872 seq_printf(m
, "%s,", use
->module_which_uses
->name
);
875 if (mod
->init
!= NULL
&& mod
->exit
== NULL
) {
876 printed_something
= 1;
877 seq_printf(m
, "[permanent],");
880 if (!printed_something
)
884 void __symbol_put(const char *symbol
)
886 struct module
*owner
;
889 if (!find_symbol(symbol
, &owner
, NULL
, true, false))
894 EXPORT_SYMBOL(__symbol_put
);
896 void symbol_put_addr(void *addr
)
898 struct module
*modaddr
;
900 if (core_kernel_text((unsigned long)addr
))
903 /* module_text_address is safe here: we're supposed to have reference
904 * to module from symbol_get, so it can't go away. */
905 modaddr
= __module_text_address((unsigned long)addr
);
909 EXPORT_SYMBOL_GPL(symbol_put_addr
);
911 static ssize_t
show_refcnt(struct module_attribute
*mattr
,
912 struct module
*mod
, char *buffer
)
914 return sprintf(buffer
, "%u\n", module_refcount(mod
));
917 static struct module_attribute refcnt
= {
918 .attr
= { .name
= "refcnt", .mode
= 0444 },
922 void module_put(struct module
*module
)
925 unsigned int cpu
= get_cpu();
926 local_dec(__module_ref_addr(module
, cpu
));
927 /* Maybe they're waiting for us to drop reference? */
928 if (unlikely(!module_is_live(module
)))
929 wake_up_process(module
->waiter
);
933 EXPORT_SYMBOL(module_put
);
935 #else /* !CONFIG_MODULE_UNLOAD */
936 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
938 /* We don't know the usage count, or what modules are using. */
939 seq_printf(m
, " - -");
942 static inline void module_unload_free(struct module
*mod
)
946 int use_module(struct module
*a
, struct module
*b
)
948 return strong_try_module_get(b
) == 0;
950 EXPORT_SYMBOL_GPL(use_module
);
952 static inline void module_unload_init(struct module
*mod
)
955 #endif /* CONFIG_MODULE_UNLOAD */
957 static ssize_t
show_initstate(struct module_attribute
*mattr
,
958 struct module
*mod
, char *buffer
)
960 const char *state
= "unknown";
962 switch (mod
->state
) {
963 case MODULE_STATE_LIVE
:
966 case MODULE_STATE_COMING
:
969 case MODULE_STATE_GOING
:
973 return sprintf(buffer
, "%s\n", state
);
976 static struct module_attribute initstate
= {
977 .attr
= { .name
= "initstate", .mode
= 0444 },
978 .show
= show_initstate
,
981 static struct module_attribute
*modinfo_attrs
[] = {
985 #ifdef CONFIG_MODULE_UNLOAD
991 static const char vermagic
[] = VERMAGIC_STRING
;
993 static int try_to_force_load(struct module
*mod
, const char *reason
)
995 #ifdef CONFIG_MODULE_FORCE_LOAD
996 if (!test_taint(TAINT_FORCED_MODULE
))
997 printk(KERN_WARNING
"%s: %s: kernel tainted.\n",
999 add_taint_module(mod
, TAINT_FORCED_MODULE
);
1006 #ifdef CONFIG_MODVERSIONS
1007 static int check_version(Elf_Shdr
*sechdrs
,
1008 unsigned int versindex
,
1009 const char *symname
,
1011 const unsigned long *crc
)
1013 unsigned int i
, num_versions
;
1014 struct modversion_info
*versions
;
1016 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1020 /* No versions at all? modprobe --force does this. */
1022 return try_to_force_load(mod
, symname
) == 0;
1024 versions
= (void *) sechdrs
[versindex
].sh_addr
;
1025 num_versions
= sechdrs
[versindex
].sh_size
1026 / sizeof(struct modversion_info
);
1028 for (i
= 0; i
< num_versions
; i
++) {
1029 if (strcmp(versions
[i
].name
, symname
) != 0)
1032 if (versions
[i
].crc
== *crc
)
1034 DEBUGP("Found checksum %lX vs module %lX\n",
1035 *crc
, versions
[i
].crc
);
1039 printk(KERN_WARNING
"%s: no symbol version for %s\n",
1040 mod
->name
, symname
);
1044 printk("%s: disagrees about version of symbol %s\n",
1045 mod
->name
, symname
);
1049 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1050 unsigned int versindex
,
1053 const unsigned long *crc
;
1055 if (!find_symbol("struct_module", NULL
, &crc
, true, false))
1057 return check_version(sechdrs
, versindex
, "struct_module", mod
, crc
);
1060 /* First part is kernel version, which we ignore if module has crcs. */
1061 static inline int same_magic(const char *amagic
, const char *bmagic
,
1065 amagic
+= strcspn(amagic
, " ");
1066 bmagic
+= strcspn(bmagic
, " ");
1068 return strcmp(amagic
, bmagic
) == 0;
1071 static inline int check_version(Elf_Shdr
*sechdrs
,
1072 unsigned int versindex
,
1073 const char *symname
,
1075 const unsigned long *crc
)
1080 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1081 unsigned int versindex
,
1087 static inline int same_magic(const char *amagic
, const char *bmagic
,
1090 return strcmp(amagic
, bmagic
) == 0;
1092 #endif /* CONFIG_MODVERSIONS */
1094 /* Resolve a symbol for this module. I.e. if we find one, record usage.
1095 Must be holding module_mutex. */
1096 static const struct kernel_symbol
*resolve_symbol(Elf_Shdr
*sechdrs
,
1097 unsigned int versindex
,
1101 struct module
*owner
;
1102 const struct kernel_symbol
*sym
;
1103 const unsigned long *crc
;
1105 sym
= find_symbol(name
, &owner
, &crc
,
1106 !(mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
)), true);
1107 /* use_module can fail due to OOM,
1108 or module initialization or unloading */
1110 if (!check_version(sechdrs
, versindex
, name
, mod
, crc
) ||
1111 !use_module(mod
, owner
))
1118 * /sys/module/foo/sections stuff
1119 * J. Corbet <corbet@lwn.net>
1121 #if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
1122 struct module_sect_attr
1124 struct module_attribute mattr
;
1126 unsigned long address
;
1129 struct module_sect_attrs
1131 struct attribute_group grp
;
1132 unsigned int nsections
;
1133 struct module_sect_attr attrs
[0];
1136 static ssize_t
module_sect_show(struct module_attribute
*mattr
,
1137 struct module
*mod
, char *buf
)
1139 struct module_sect_attr
*sattr
=
1140 container_of(mattr
, struct module_sect_attr
, mattr
);
1141 return sprintf(buf
, "0x%lx\n", sattr
->address
);
1144 static void free_sect_attrs(struct module_sect_attrs
*sect_attrs
)
1146 unsigned int section
;
1148 for (section
= 0; section
< sect_attrs
->nsections
; section
++)
1149 kfree(sect_attrs
->attrs
[section
].name
);
1153 static void add_sect_attrs(struct module
*mod
, unsigned int nsect
,
1154 char *secstrings
, Elf_Shdr
*sechdrs
)
1156 unsigned int nloaded
= 0, i
, size
[2];
1157 struct module_sect_attrs
*sect_attrs
;
1158 struct module_sect_attr
*sattr
;
1159 struct attribute
**gattr
;
1161 /* Count loaded sections and allocate structures */
1162 for (i
= 0; i
< nsect
; i
++)
1163 if (sechdrs
[i
].sh_flags
& SHF_ALLOC
)
1165 size
[0] = ALIGN(sizeof(*sect_attrs
)
1166 + nloaded
* sizeof(sect_attrs
->attrs
[0]),
1167 sizeof(sect_attrs
->grp
.attrs
[0]));
1168 size
[1] = (nloaded
+ 1) * sizeof(sect_attrs
->grp
.attrs
[0]);
1169 sect_attrs
= kzalloc(size
[0] + size
[1], GFP_KERNEL
);
1170 if (sect_attrs
== NULL
)
1173 /* Setup section attributes. */
1174 sect_attrs
->grp
.name
= "sections";
1175 sect_attrs
->grp
.attrs
= (void *)sect_attrs
+ size
[0];
1177 sect_attrs
->nsections
= 0;
1178 sattr
= §_attrs
->attrs
[0];
1179 gattr
= §_attrs
->grp
.attrs
[0];
1180 for (i
= 0; i
< nsect
; i
++) {
1181 if (! (sechdrs
[i
].sh_flags
& SHF_ALLOC
))
1183 sattr
->address
= sechdrs
[i
].sh_addr
;
1184 sattr
->name
= kstrdup(secstrings
+ sechdrs
[i
].sh_name
,
1186 if (sattr
->name
== NULL
)
1188 sect_attrs
->nsections
++;
1189 sattr
->mattr
.show
= module_sect_show
;
1190 sattr
->mattr
.store
= NULL
;
1191 sattr
->mattr
.attr
.name
= sattr
->name
;
1192 sattr
->mattr
.attr
.mode
= S_IRUGO
;
1193 *(gattr
++) = &(sattr
++)->mattr
.attr
;
1197 if (sysfs_create_group(&mod
->mkobj
.kobj
, §_attrs
->grp
))
1200 mod
->sect_attrs
= sect_attrs
;
1203 free_sect_attrs(sect_attrs
);
1206 static void remove_sect_attrs(struct module
*mod
)
1208 if (mod
->sect_attrs
) {
1209 sysfs_remove_group(&mod
->mkobj
.kobj
,
1210 &mod
->sect_attrs
->grp
);
1211 /* We are positive that no one is using any sect attrs
1212 * at this point. Deallocate immediately. */
1213 free_sect_attrs(mod
->sect_attrs
);
1214 mod
->sect_attrs
= NULL
;
1219 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1222 struct module_notes_attrs
{
1223 struct kobject
*dir
;
1225 struct bin_attribute attrs
[0];
1228 static ssize_t
module_notes_read(struct kobject
*kobj
,
1229 struct bin_attribute
*bin_attr
,
1230 char *buf
, loff_t pos
, size_t count
)
1233 * The caller checked the pos and count against our size.
1235 memcpy(buf
, bin_attr
->private + pos
, count
);
1239 static void free_notes_attrs(struct module_notes_attrs
*notes_attrs
,
1242 if (notes_attrs
->dir
) {
1244 sysfs_remove_bin_file(notes_attrs
->dir
,
1245 ¬es_attrs
->attrs
[i
]);
1246 kobject_put(notes_attrs
->dir
);
1251 static void add_notes_attrs(struct module
*mod
, unsigned int nsect
,
1252 char *secstrings
, Elf_Shdr
*sechdrs
)
1254 unsigned int notes
, loaded
, i
;
1255 struct module_notes_attrs
*notes_attrs
;
1256 struct bin_attribute
*nattr
;
1258 /* Count notes sections and allocate structures. */
1260 for (i
= 0; i
< nsect
; i
++)
1261 if ((sechdrs
[i
].sh_flags
& SHF_ALLOC
) &&
1262 (sechdrs
[i
].sh_type
== SHT_NOTE
))
1268 notes_attrs
= kzalloc(sizeof(*notes_attrs
)
1269 + notes
* sizeof(notes_attrs
->attrs
[0]),
1271 if (notes_attrs
== NULL
)
1274 notes_attrs
->notes
= notes
;
1275 nattr
= ¬es_attrs
->attrs
[0];
1276 for (loaded
= i
= 0; i
< nsect
; ++i
) {
1277 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
1279 if (sechdrs
[i
].sh_type
== SHT_NOTE
) {
1280 nattr
->attr
.name
= mod
->sect_attrs
->attrs
[loaded
].name
;
1281 nattr
->attr
.mode
= S_IRUGO
;
1282 nattr
->size
= sechdrs
[i
].sh_size
;
1283 nattr
->private = (void *) sechdrs
[i
].sh_addr
;
1284 nattr
->read
= module_notes_read
;
1290 notes_attrs
->dir
= kobject_create_and_add("notes", &mod
->mkobj
.kobj
);
1291 if (!notes_attrs
->dir
)
1294 for (i
= 0; i
< notes
; ++i
)
1295 if (sysfs_create_bin_file(notes_attrs
->dir
,
1296 ¬es_attrs
->attrs
[i
]))
1299 mod
->notes_attrs
= notes_attrs
;
1303 free_notes_attrs(notes_attrs
, i
);
1306 static void remove_notes_attrs(struct module
*mod
)
1308 if (mod
->notes_attrs
)
1309 free_notes_attrs(mod
->notes_attrs
, mod
->notes_attrs
->notes
);
1314 static inline void add_sect_attrs(struct module
*mod
, unsigned int nsect
,
1315 char *sectstrings
, Elf_Shdr
*sechdrs
)
1319 static inline void remove_sect_attrs(struct module
*mod
)
1323 static inline void add_notes_attrs(struct module
*mod
, unsigned int nsect
,
1324 char *sectstrings
, Elf_Shdr
*sechdrs
)
1328 static inline void remove_notes_attrs(struct module
*mod
)
1334 int module_add_modinfo_attrs(struct module
*mod
)
1336 struct module_attribute
*attr
;
1337 struct module_attribute
*temp_attr
;
1341 mod
->modinfo_attrs
= kzalloc((sizeof(struct module_attribute
) *
1342 (ARRAY_SIZE(modinfo_attrs
) + 1)),
1344 if (!mod
->modinfo_attrs
)
1347 temp_attr
= mod
->modinfo_attrs
;
1348 for (i
= 0; (attr
= modinfo_attrs
[i
]) && !error
; i
++) {
1350 (attr
->test
&& attr
->test(mod
))) {
1351 memcpy(temp_attr
, attr
, sizeof(*temp_attr
));
1352 error
= sysfs_create_file(&mod
->mkobj
.kobj
,&temp_attr
->attr
);
1359 void module_remove_modinfo_attrs(struct module
*mod
)
1361 struct module_attribute
*attr
;
1364 for (i
= 0; (attr
= &mod
->modinfo_attrs
[i
]); i
++) {
1365 /* pick a field to test for end of list */
1366 if (!attr
->attr
.name
)
1368 sysfs_remove_file(&mod
->mkobj
.kobj
,&attr
->attr
);
1372 kfree(mod
->modinfo_attrs
);
1375 int mod_sysfs_init(struct module
*mod
)
1378 struct kobject
*kobj
;
1380 if (!module_sysfs_initialized
) {
1381 printk(KERN_ERR
"%s: module sysfs not initialized\n",
1387 kobj
= kset_find_obj(module_kset
, mod
->name
);
1389 printk(KERN_ERR
"%s: module is already loaded\n", mod
->name
);
1395 mod
->mkobj
.mod
= mod
;
1397 memset(&mod
->mkobj
.kobj
, 0, sizeof(mod
->mkobj
.kobj
));
1398 mod
->mkobj
.kobj
.kset
= module_kset
;
1399 err
= kobject_init_and_add(&mod
->mkobj
.kobj
, &module_ktype
, NULL
,
1402 kobject_put(&mod
->mkobj
.kobj
);
1404 /* delay uevent until full sysfs population */
1409 int mod_sysfs_setup(struct module
*mod
,
1410 struct kernel_param
*kparam
,
1411 unsigned int num_params
)
1415 mod
->holders_dir
= kobject_create_and_add("holders", &mod
->mkobj
.kobj
);
1416 if (!mod
->holders_dir
) {
1421 err
= module_param_sysfs_setup(mod
, kparam
, num_params
);
1423 goto out_unreg_holders
;
1425 err
= module_add_modinfo_attrs(mod
);
1427 goto out_unreg_param
;
1429 kobject_uevent(&mod
->mkobj
.kobj
, KOBJ_ADD
);
1433 module_param_sysfs_remove(mod
);
1435 kobject_put(mod
->holders_dir
);
1437 kobject_put(&mod
->mkobj
.kobj
);
1441 static void mod_sysfs_fini(struct module
*mod
)
1443 kobject_put(&mod
->mkobj
.kobj
);
1446 #else /* CONFIG_SYSFS */
1448 static void mod_sysfs_fini(struct module
*mod
)
1452 #endif /* CONFIG_SYSFS */
1454 static void mod_kobject_remove(struct module
*mod
)
1456 module_remove_modinfo_attrs(mod
);
1457 module_param_sysfs_remove(mod
);
1458 kobject_put(mod
->mkobj
.drivers_dir
);
1459 kobject_put(mod
->holders_dir
);
1460 mod_sysfs_fini(mod
);
1464 * unlink the module with the whole machine is stopped with interrupts off
1465 * - this defends against kallsyms not taking locks
1467 static int __unlink_module(void *_mod
)
1469 struct module
*mod
= _mod
;
1470 list_del(&mod
->list
);
1474 /* Free a module, remove from lists, etc (must hold module_mutex). */
1475 static void free_module(struct module
*mod
)
1477 /* Delete from various lists */
1478 stop_machine(__unlink_module
, mod
, NULL
);
1479 remove_notes_attrs(mod
);
1480 remove_sect_attrs(mod
);
1481 mod_kobject_remove(mod
);
1483 /* Arch-specific cleanup. */
1484 module_arch_cleanup(mod
);
1486 /* Module unload stuff */
1487 module_unload_free(mod
);
1489 /* Free any allocated parameters. */
1490 destroy_params(mod
->kp
, mod
->num_kp
);
1492 /* release any pointers to mcount in this module */
1493 ftrace_release(mod
->module_core
, mod
->core_size
);
1495 /* This may be NULL, but that's OK */
1496 module_free(mod
, mod
->module_init
);
1499 percpu_modfree(mod
->percpu
);
1500 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
1502 percpu_modfree(mod
->refptr
);
1504 /* Free lock-classes: */
1505 lockdep_free_key_range(mod
->module_core
, mod
->core_size
);
1507 /* Finally, free the core (containing the module structure) */
1508 module_free(mod
, mod
->module_core
);
1511 void *__symbol_get(const char *symbol
)
1513 struct module
*owner
;
1514 const struct kernel_symbol
*sym
;
1517 sym
= find_symbol(symbol
, &owner
, NULL
, true, true);
1518 if (sym
&& strong_try_module_get(owner
))
1522 return sym
? (void *)sym
->value
: NULL
;
1524 EXPORT_SYMBOL_GPL(__symbol_get
);
1527 * Ensure that an exported symbol [global namespace] does not already exist
1528 * in the kernel or in some other module's exported symbol table.
1530 static int verify_export_symbols(struct module
*mod
)
1533 struct module
*owner
;
1534 const struct kernel_symbol
*s
;
1536 const struct kernel_symbol
*sym
;
1539 { mod
->syms
, mod
->num_syms
},
1540 { mod
->gpl_syms
, mod
->num_gpl_syms
},
1541 { mod
->gpl_future_syms
, mod
->num_gpl_future_syms
},
1542 #ifdef CONFIG_UNUSED_SYMBOLS
1543 { mod
->unused_syms
, mod
->num_unused_syms
},
1544 { mod
->unused_gpl_syms
, mod
->num_unused_gpl_syms
},
1548 for (i
= 0; i
< ARRAY_SIZE(arr
); i
++) {
1549 for (s
= arr
[i
].sym
; s
< arr
[i
].sym
+ arr
[i
].num
; s
++) {
1550 if (find_symbol(s
->name
, &owner
, NULL
, true, false)) {
1552 "%s: exports duplicate symbol %s"
1554 mod
->name
, s
->name
, module_name(owner
));
1562 /* Change all symbols so that st_value encodes the pointer directly. */
1563 static int simplify_symbols(Elf_Shdr
*sechdrs
,
1564 unsigned int symindex
,
1566 unsigned int versindex
,
1567 unsigned int pcpuindex
,
1570 Elf_Sym
*sym
= (void *)sechdrs
[symindex
].sh_addr
;
1571 unsigned long secbase
;
1572 unsigned int i
, n
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1574 const struct kernel_symbol
*ksym
;
1576 for (i
= 1; i
< n
; i
++) {
1577 switch (sym
[i
].st_shndx
) {
1579 /* We compiled with -fno-common. These are not
1580 supposed to happen. */
1581 DEBUGP("Common symbol: %s\n", strtab
+ sym
[i
].st_name
);
1582 printk("%s: please compile with -fno-common\n",
1588 /* Don't need to do anything */
1589 DEBUGP("Absolute symbol: 0x%08lx\n",
1590 (long)sym
[i
].st_value
);
1594 ksym
= resolve_symbol(sechdrs
, versindex
,
1595 strtab
+ sym
[i
].st_name
, mod
);
1596 /* Ok if resolved. */
1598 sym
[i
].st_value
= ksym
->value
;
1603 if (ELF_ST_BIND(sym
[i
].st_info
) == STB_WEAK
)
1606 printk(KERN_WARNING
"%s: Unknown symbol %s\n",
1607 mod
->name
, strtab
+ sym
[i
].st_name
);
1612 /* Divert to percpu allocation if a percpu var. */
1613 if (sym
[i
].st_shndx
== pcpuindex
)
1614 secbase
= (unsigned long)mod
->percpu
;
1616 secbase
= sechdrs
[sym
[i
].st_shndx
].sh_addr
;
1617 sym
[i
].st_value
+= secbase
;
1625 /* Additional bytes needed by arch in front of individual sections */
1626 unsigned int __weak
arch_mod_section_prepend(struct module
*mod
,
1627 unsigned int section
)
1629 /* default implementation just returns zero */
1633 /* Update size with this section: return offset. */
1634 static long get_offset(struct module
*mod
, unsigned int *size
,
1635 Elf_Shdr
*sechdr
, unsigned int section
)
1639 *size
+= arch_mod_section_prepend(mod
, section
);
1640 ret
= ALIGN(*size
, sechdr
->sh_addralign
?: 1);
1641 *size
= ret
+ sechdr
->sh_size
;
1645 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1646 might -- code, read-only data, read-write data, small data. Tally
1647 sizes, and place the offsets into sh_entsize fields: high bit means it
1649 static void layout_sections(struct module
*mod
,
1650 const Elf_Ehdr
*hdr
,
1652 const char *secstrings
)
1654 static unsigned long const masks
[][2] = {
1655 /* NOTE: all executable code must be the first section
1656 * in this array; otherwise modify the text_size
1657 * finder in the two loops below */
1658 { SHF_EXECINSTR
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1659 { SHF_ALLOC
, SHF_WRITE
| ARCH_SHF_SMALL
},
1660 { SHF_WRITE
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1661 { ARCH_SHF_SMALL
| SHF_ALLOC
, 0 }
1665 for (i
= 0; i
< hdr
->e_shnum
; i
++)
1666 sechdrs
[i
].sh_entsize
= ~0UL;
1668 DEBUGP("Core section allocation order:\n");
1669 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1670 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1671 Elf_Shdr
*s
= &sechdrs
[i
];
1673 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1674 || (s
->sh_flags
& masks
[m
][1])
1675 || s
->sh_entsize
!= ~0UL
1676 || strncmp(secstrings
+ s
->sh_name
,
1679 s
->sh_entsize
= get_offset(mod
, &mod
->core_size
, s
, i
);
1680 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1683 mod
->core_text_size
= mod
->core_size
;
1686 DEBUGP("Init section allocation order:\n");
1687 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1688 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1689 Elf_Shdr
*s
= &sechdrs
[i
];
1691 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1692 || (s
->sh_flags
& masks
[m
][1])
1693 || s
->sh_entsize
!= ~0UL
1694 || strncmp(secstrings
+ s
->sh_name
,
1697 s
->sh_entsize
= (get_offset(mod
, &mod
->init_size
, s
, i
)
1698 | INIT_OFFSET_MASK
);
1699 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1702 mod
->init_text_size
= mod
->init_size
;
1706 static void set_license(struct module
*mod
, const char *license
)
1709 license
= "unspecified";
1711 if (!license_is_gpl_compatible(license
)) {
1712 if (!test_taint(TAINT_PROPRIETARY_MODULE
))
1713 printk(KERN_WARNING
"%s: module license '%s' taints "
1714 "kernel.\n", mod
->name
, license
);
1715 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
1719 /* Parse tag=value strings from .modinfo section */
1720 static char *next_string(char *string
, unsigned long *secsize
)
1722 /* Skip non-zero chars */
1725 if ((*secsize
)-- <= 1)
1729 /* Skip any zero padding. */
1730 while (!string
[0]) {
1732 if ((*secsize
)-- <= 1)
1738 static char *get_modinfo(Elf_Shdr
*sechdrs
,
1743 unsigned int taglen
= strlen(tag
);
1744 unsigned long size
= sechdrs
[info
].sh_size
;
1746 for (p
= (char *)sechdrs
[info
].sh_addr
; p
; p
= next_string(p
, &size
)) {
1747 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
1748 return p
+ taglen
+ 1;
1753 static void setup_modinfo(struct module
*mod
, Elf_Shdr
*sechdrs
,
1754 unsigned int infoindex
)
1756 struct module_attribute
*attr
;
1759 for (i
= 0; (attr
= modinfo_attrs
[i
]); i
++) {
1762 get_modinfo(sechdrs
,
1768 #ifdef CONFIG_KALLSYMS
1770 /* lookup symbol in given range of kernel_symbols */
1771 static const struct kernel_symbol
*lookup_symbol(const char *name
,
1772 const struct kernel_symbol
*start
,
1773 const struct kernel_symbol
*stop
)
1775 const struct kernel_symbol
*ks
= start
;
1776 for (; ks
< stop
; ks
++)
1777 if (strcmp(ks
->name
, name
) == 0)
1782 static int is_exported(const char *name
, unsigned long value
,
1783 const struct module
*mod
)
1785 const struct kernel_symbol
*ks
;
1787 ks
= lookup_symbol(name
, __start___ksymtab
, __stop___ksymtab
);
1789 ks
= lookup_symbol(name
, mod
->syms
, mod
->syms
+ mod
->num_syms
);
1790 return ks
!= NULL
&& ks
->value
== value
;
1794 static char elf_type(const Elf_Sym
*sym
,
1796 const char *secstrings
,
1799 if (ELF_ST_BIND(sym
->st_info
) == STB_WEAK
) {
1800 if (ELF_ST_TYPE(sym
->st_info
) == STT_OBJECT
)
1805 if (sym
->st_shndx
== SHN_UNDEF
)
1807 if (sym
->st_shndx
== SHN_ABS
)
1809 if (sym
->st_shndx
>= SHN_LORESERVE
)
1811 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_EXECINSTR
)
1813 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_ALLOC
1814 && sechdrs
[sym
->st_shndx
].sh_type
!= SHT_NOBITS
) {
1815 if (!(sechdrs
[sym
->st_shndx
].sh_flags
& SHF_WRITE
))
1817 else if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1822 if (sechdrs
[sym
->st_shndx
].sh_type
== SHT_NOBITS
) {
1823 if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1828 if (strncmp(secstrings
+ sechdrs
[sym
->st_shndx
].sh_name
,
1829 ".debug", strlen(".debug")) == 0)
1834 static void add_kallsyms(struct module
*mod
,
1836 unsigned int symindex
,
1837 unsigned int strindex
,
1838 const char *secstrings
)
1842 mod
->symtab
= (void *)sechdrs
[symindex
].sh_addr
;
1843 mod
->num_symtab
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1844 mod
->strtab
= (void *)sechdrs
[strindex
].sh_addr
;
1846 /* Set types up while we still have access to sections. */
1847 for (i
= 0; i
< mod
->num_symtab
; i
++)
1848 mod
->symtab
[i
].st_info
1849 = elf_type(&mod
->symtab
[i
], sechdrs
, secstrings
, mod
);
1852 static inline void add_kallsyms(struct module
*mod
,
1854 unsigned int symindex
,
1855 unsigned int strindex
,
1856 const char *secstrings
)
1859 #endif /* CONFIG_KALLSYMS */
1861 static void dynamic_debug_setup(struct _ddebug
*debug
, unsigned int num
)
1863 #ifdef CONFIG_DYNAMIC_DEBUG
1864 if (ddebug_add_module(debug
, num
, debug
->modname
))
1865 printk(KERN_ERR
"dynamic debug error adding module: %s\n",
1870 static void *module_alloc_update_bounds(unsigned long size
)
1872 void *ret
= module_alloc(size
);
1875 /* Update module bounds. */
1876 if ((unsigned long)ret
< module_addr_min
)
1877 module_addr_min
= (unsigned long)ret
;
1878 if ((unsigned long)ret
+ size
> module_addr_max
)
1879 module_addr_max
= (unsigned long)ret
+ size
;
1884 /* Allocate and load the module: note that size of section 0 is always
1885 zero, and we rely on this for optional sections. */
1886 static noinline
struct module
*load_module(void __user
*umod
,
1888 const char __user
*uargs
)
1892 char *secstrings
, *args
, *modmagic
, *strtab
= NULL
;
1895 unsigned int symindex
= 0;
1896 unsigned int strindex
= 0;
1897 unsigned int modindex
, versindex
, infoindex
, pcpuindex
;
1898 unsigned int num_mcount
;
1901 void *percpu
= NULL
, *ptr
= NULL
; /* Stops spurious gcc warning */
1902 unsigned long *mseg
;
1903 mm_segment_t old_fs
;
1905 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1907 if (len
< sizeof(*hdr
))
1908 return ERR_PTR(-ENOEXEC
);
1910 /* Suck in entire file: we'll want most of it. */
1911 /* vmalloc barfs on "unusual" numbers. Check here */
1912 if (len
> 64 * 1024 * 1024 || (hdr
= vmalloc(len
)) == NULL
)
1913 return ERR_PTR(-ENOMEM
);
1915 /* Create stop_machine threads since the error path relies on
1916 * a non-failing stop_machine call. */
1917 err
= stop_machine_create();
1921 if (copy_from_user(hdr
, umod
, len
) != 0) {
1926 /* Sanity checks against insmoding binaries or wrong arch,
1927 weird elf version */
1928 if (memcmp(hdr
->e_ident
, ELFMAG
, SELFMAG
) != 0
1929 || hdr
->e_type
!= ET_REL
1930 || !elf_check_arch(hdr
)
1931 || hdr
->e_shentsize
!= sizeof(*sechdrs
)) {
1936 if (len
< hdr
->e_shoff
+ hdr
->e_shnum
* sizeof(Elf_Shdr
))
1939 /* Convenience variables */
1940 sechdrs
= (void *)hdr
+ hdr
->e_shoff
;
1941 secstrings
= (void *)hdr
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
1942 sechdrs
[0].sh_addr
= 0;
1944 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
1945 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
1946 && len
< sechdrs
[i
].sh_offset
+ sechdrs
[i
].sh_size
)
1949 /* Mark all sections sh_addr with their address in the
1951 sechdrs
[i
].sh_addr
= (size_t)hdr
+ sechdrs
[i
].sh_offset
;
1953 /* Internal symbols and strings. */
1954 if (sechdrs
[i
].sh_type
== SHT_SYMTAB
) {
1956 strindex
= sechdrs
[i
].sh_link
;
1957 strtab
= (char *)hdr
+ sechdrs
[strindex
].sh_offset
;
1959 #ifndef CONFIG_MODULE_UNLOAD
1960 /* Don't load .exit sections */
1961 if (strncmp(secstrings
+sechdrs
[i
].sh_name
, ".exit", 5) == 0)
1962 sechdrs
[i
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1964 /* Don't keep __versions around; it's just for loading. */
1965 if (strcmp(secstrings
+ sechdrs
[i
].sh_name
, "__versions") == 0)
1966 sechdrs
[i
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1969 modindex
= find_sec(hdr
, sechdrs
, secstrings
,
1970 ".gnu.linkonce.this_module");
1972 printk(KERN_WARNING
"No module found in object\n");
1976 /* This is temporary: point mod into copy of data. */
1977 mod
= (void *)sechdrs
[modindex
].sh_addr
;
1979 if (symindex
== 0) {
1980 printk(KERN_WARNING
"%s: module has no symbols (stripped?)\n",
1986 versindex
= find_sec(hdr
, sechdrs
, secstrings
, "__versions");
1987 infoindex
= find_sec(hdr
, sechdrs
, secstrings
, ".modinfo");
1988 pcpuindex
= find_pcpusec(hdr
, sechdrs
, secstrings
);
1990 /* Don't keep modinfo and version sections. */
1991 sechdrs
[infoindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1992 sechdrs
[versindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1993 #ifdef CONFIG_KALLSYMS
1994 /* Keep symbol and string tables for decoding later. */
1995 sechdrs
[symindex
].sh_flags
|= SHF_ALLOC
;
1996 sechdrs
[strindex
].sh_flags
|= SHF_ALLOC
;
1999 /* Check module struct version now, before we try to use module. */
2000 if (!check_modstruct_version(sechdrs
, versindex
, mod
)) {
2005 modmagic
= get_modinfo(sechdrs
, infoindex
, "vermagic");
2006 /* This is allowed: modprobe --force will invalidate it. */
2008 err
= try_to_force_load(mod
, "bad vermagic");
2011 } else if (!same_magic(modmagic
, vermagic
, versindex
)) {
2012 printk(KERN_ERR
"%s: version magic '%s' should be '%s'\n",
2013 mod
->name
, modmagic
, vermagic
);
2018 staging
= get_modinfo(sechdrs
, infoindex
, "staging");
2020 add_taint_module(mod
, TAINT_CRAP
);
2021 printk(KERN_WARNING
"%s: module is from the staging directory,"
2022 " the quality is unknown, you have been warned.\n",
2026 /* Now copy in args */
2027 args
= strndup_user(uargs
, ~0UL >> 1);
2029 err
= PTR_ERR(args
);
2033 if (find_module(mod
->name
)) {
2038 mod
->state
= MODULE_STATE_COMING
;
2040 /* Allow arches to frob section contents and sizes. */
2041 err
= module_frob_arch_sections(hdr
, sechdrs
, secstrings
, mod
);
2046 /* We have a special allocation for this section. */
2047 percpu
= percpu_modalloc(sechdrs
[pcpuindex
].sh_size
,
2048 sechdrs
[pcpuindex
].sh_addralign
,
2054 sechdrs
[pcpuindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2055 mod
->percpu
= percpu
;
2058 /* Determine total sizes, and put offsets in sh_entsize. For now
2059 this is done generically; there doesn't appear to be any
2060 special cases for the architectures. */
2061 layout_sections(mod
, hdr
, sechdrs
, secstrings
);
2063 /* Do the allocs. */
2064 ptr
= module_alloc_update_bounds(mod
->core_size
);
2069 memset(ptr
, 0, mod
->core_size
);
2070 mod
->module_core
= ptr
;
2072 ptr
= module_alloc_update_bounds(mod
->init_size
);
2073 if (!ptr
&& mod
->init_size
) {
2077 memset(ptr
, 0, mod
->init_size
);
2078 mod
->module_init
= ptr
;
2080 /* Transfer each section which specifies SHF_ALLOC */
2081 DEBUGP("final section addresses:\n");
2082 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
2085 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
2088 if (sechdrs
[i
].sh_entsize
& INIT_OFFSET_MASK
)
2089 dest
= mod
->module_init
2090 + (sechdrs
[i
].sh_entsize
& ~INIT_OFFSET_MASK
);
2092 dest
= mod
->module_core
+ sechdrs
[i
].sh_entsize
;
2094 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
)
2095 memcpy(dest
, (void *)sechdrs
[i
].sh_addr
,
2096 sechdrs
[i
].sh_size
);
2097 /* Update sh_addr to point to copy in image. */
2098 sechdrs
[i
].sh_addr
= (unsigned long)dest
;
2099 DEBUGP("\t0x%lx %s\n", sechdrs
[i
].sh_addr
, secstrings
+ sechdrs
[i
].sh_name
);
2101 /* Module has been moved. */
2102 mod
= (void *)sechdrs
[modindex
].sh_addr
;
2104 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2105 mod
->refptr
= percpu_modalloc(sizeof(local_t
), __alignof__(local_t
),
2112 /* Now we've moved module, initialize linked lists, etc. */
2113 module_unload_init(mod
);
2115 /* add kobject, so we can reference it. */
2116 err
= mod_sysfs_init(mod
);
2120 /* Set up license info based on the info section */
2121 set_license(mod
, get_modinfo(sechdrs
, infoindex
, "license"));
2124 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2125 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2126 * using GPL-only symbols it needs.
2128 if (strcmp(mod
->name
, "ndiswrapper") == 0)
2129 add_taint(TAINT_PROPRIETARY_MODULE
);
2131 /* driverloader was caught wrongly pretending to be under GPL */
2132 if (strcmp(mod
->name
, "driverloader") == 0)
2133 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
2135 /* Set up MODINFO_ATTR fields */
2136 setup_modinfo(mod
, sechdrs
, infoindex
);
2138 /* Fix up syms, so that st_value is a pointer to location. */
2139 err
= simplify_symbols(sechdrs
, symindex
, strtab
, versindex
, pcpuindex
,
2144 /* Now we've got everything in the final locations, we can
2145 * find optional sections. */
2146 mod
->kp
= section_objs(hdr
, sechdrs
, secstrings
, "__param",
2147 sizeof(*mod
->kp
), &mod
->num_kp
);
2148 mod
->syms
= section_objs(hdr
, sechdrs
, secstrings
, "__ksymtab",
2149 sizeof(*mod
->syms
), &mod
->num_syms
);
2150 mod
->crcs
= section_addr(hdr
, sechdrs
, secstrings
, "__kcrctab");
2151 mod
->gpl_syms
= section_objs(hdr
, sechdrs
, secstrings
, "__ksymtab_gpl",
2152 sizeof(*mod
->gpl_syms
),
2153 &mod
->num_gpl_syms
);
2154 mod
->gpl_crcs
= section_addr(hdr
, sechdrs
, secstrings
, "__kcrctab_gpl");
2155 mod
->gpl_future_syms
= section_objs(hdr
, sechdrs
, secstrings
,
2156 "__ksymtab_gpl_future",
2157 sizeof(*mod
->gpl_future_syms
),
2158 &mod
->num_gpl_future_syms
);
2159 mod
->gpl_future_crcs
= section_addr(hdr
, sechdrs
, secstrings
,
2160 "__kcrctab_gpl_future");
2162 #ifdef CONFIG_UNUSED_SYMBOLS
2163 mod
->unused_syms
= section_objs(hdr
, sechdrs
, secstrings
,
2165 sizeof(*mod
->unused_syms
),
2166 &mod
->num_unused_syms
);
2167 mod
->unused_crcs
= section_addr(hdr
, sechdrs
, secstrings
,
2168 "__kcrctab_unused");
2169 mod
->unused_gpl_syms
= section_objs(hdr
, sechdrs
, secstrings
,
2170 "__ksymtab_unused_gpl",
2171 sizeof(*mod
->unused_gpl_syms
),
2172 &mod
->num_unused_gpl_syms
);
2173 mod
->unused_gpl_crcs
= section_addr(hdr
, sechdrs
, secstrings
,
2174 "__kcrctab_unused_gpl");
2177 #ifdef CONFIG_MARKERS
2178 mod
->markers
= section_objs(hdr
, sechdrs
, secstrings
, "__markers",
2179 sizeof(*mod
->markers
), &mod
->num_markers
);
2181 #ifdef CONFIG_TRACEPOINTS
2182 mod
->tracepoints
= section_objs(hdr
, sechdrs
, secstrings
,
2184 sizeof(*mod
->tracepoints
),
2185 &mod
->num_tracepoints
);
2188 #ifdef CONFIG_MODVERSIONS
2189 if ((mod
->num_syms
&& !mod
->crcs
)
2190 || (mod
->num_gpl_syms
&& !mod
->gpl_crcs
)
2191 || (mod
->num_gpl_future_syms
&& !mod
->gpl_future_crcs
)
2192 #ifdef CONFIG_UNUSED_SYMBOLS
2193 || (mod
->num_unused_syms
&& !mod
->unused_crcs
)
2194 || (mod
->num_unused_gpl_syms
&& !mod
->unused_gpl_crcs
)
2197 err
= try_to_force_load(mod
,
2198 "no versions for exported symbols");
2204 /* Now do relocations. */
2205 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
2206 const char *strtab
= (char *)sechdrs
[strindex
].sh_addr
;
2207 unsigned int info
= sechdrs
[i
].sh_info
;
2209 /* Not a valid relocation section? */
2210 if (info
>= hdr
->e_shnum
)
2213 /* Don't bother with non-allocated sections */
2214 if (!(sechdrs
[info
].sh_flags
& SHF_ALLOC
))
2217 if (sechdrs
[i
].sh_type
== SHT_REL
)
2218 err
= apply_relocate(sechdrs
, strtab
, symindex
, i
,mod
);
2219 else if (sechdrs
[i
].sh_type
== SHT_RELA
)
2220 err
= apply_relocate_add(sechdrs
, strtab
, symindex
, i
,
2226 /* Find duplicate symbols */
2227 err
= verify_export_symbols(mod
);
2231 /* Set up and sort exception table */
2232 mod
->extable
= section_objs(hdr
, sechdrs
, secstrings
, "__ex_table",
2233 sizeof(*mod
->extable
), &mod
->num_exentries
);
2234 sort_extable(mod
->extable
, mod
->extable
+ mod
->num_exentries
);
2236 /* Finally, copy percpu area over. */
2237 percpu_modcopy(mod
->percpu
, (void *)sechdrs
[pcpuindex
].sh_addr
,
2238 sechdrs
[pcpuindex
].sh_size
);
2240 add_kallsyms(mod
, sechdrs
, symindex
, strindex
, secstrings
);
2243 struct _ddebug
*debug
;
2244 unsigned int num_debug
;
2246 debug
= section_objs(hdr
, sechdrs
, secstrings
, "__verbose",
2247 sizeof(*debug
), &num_debug
);
2249 dynamic_debug_setup(debug
, num_debug
);
2252 /* sechdrs[0].sh_size is always zero */
2253 mseg
= section_objs(hdr
, sechdrs
, secstrings
, "__mcount_loc",
2254 sizeof(*mseg
), &num_mcount
);
2255 ftrace_init_module(mod
, mseg
, mseg
+ num_mcount
);
2257 err
= module_finalize(hdr
, sechdrs
, mod
);
2261 /* flush the icache in correct context */
2266 * Flush the instruction cache, since we've played with text.
2267 * Do it before processing of module parameters, so the module
2268 * can provide parameter accessor functions of its own.
2270 if (mod
->module_init
)
2271 flush_icache_range((unsigned long)mod
->module_init
,
2272 (unsigned long)mod
->module_init
2274 flush_icache_range((unsigned long)mod
->module_core
,
2275 (unsigned long)mod
->module_core
+ mod
->core_size
);
2280 if (section_addr(hdr
, sechdrs
, secstrings
, "__obsparm"))
2281 printk(KERN_WARNING
"%s: Ignoring obsolete parameters\n",
2284 /* Now sew it into the lists so we can get lockdep and oops
2285 * info during argument parsing. Noone should access us, since
2286 * strong_try_module_get() will fail.
2287 * lockdep/oops can run asynchronous, so use the RCU list insertion
2288 * function to insert in a way safe to concurrent readers.
2289 * The mutex protects against concurrent writers.
2291 list_add_rcu(&mod
->list
, &modules
);
2293 err
= parse_args(mod
->name
, mod
->args
, mod
->kp
, mod
->num_kp
, NULL
);
2297 err
= mod_sysfs_setup(mod
, mod
->kp
, mod
->num_kp
);
2300 add_sect_attrs(mod
, hdr
->e_shnum
, secstrings
, sechdrs
);
2301 add_notes_attrs(mod
, hdr
->e_shnum
, secstrings
, sechdrs
);
2303 /* Get rid of temporary copy */
2306 stop_machine_destroy();
2311 stop_machine(__unlink_module
, mod
, NULL
);
2312 module_arch_cleanup(mod
);
2314 kobject_del(&mod
->mkobj
.kobj
);
2315 kobject_put(&mod
->mkobj
.kobj
);
2316 ftrace_release(mod
->module_core
, mod
->core_size
);
2318 module_unload_free(mod
);
2319 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2321 percpu_modfree(mod
->refptr
);
2323 module_free(mod
, mod
->module_init
);
2325 module_free(mod
, mod
->module_core
);
2326 /* mod will be freed with core. Don't access it beyond this line! */
2329 percpu_modfree(percpu
);
2334 stop_machine_destroy();
2335 return ERR_PTR(err
);
2338 printk(KERN_ERR
"Module len %lu truncated\n", len
);
2343 /* This is where the real work happens */
2344 SYSCALL_DEFINE3(init_module
, void __user
*, umod
,
2345 unsigned long, len
, const char __user
*, uargs
)
2350 /* Must have permission */
2351 if (!capable(CAP_SYS_MODULE
))
2354 /* Only one module load at a time, please */
2355 if (mutex_lock_interruptible(&module_mutex
) != 0)
2358 /* Do all the hard work */
2359 mod
= load_module(umod
, len
, uargs
);
2361 mutex_unlock(&module_mutex
);
2362 return PTR_ERR(mod
);
2365 /* Drop lock so they can recurse */
2366 mutex_unlock(&module_mutex
);
2368 blocking_notifier_call_chain(&module_notify_list
,
2369 MODULE_STATE_COMING
, mod
);
2371 /* Start the module */
2372 if (mod
->init
!= NULL
)
2373 ret
= do_one_initcall(mod
->init
);
2375 /* Init routine failed: abort. Try to protect us from
2376 buggy refcounters. */
2377 mod
->state
= MODULE_STATE_GOING
;
2378 synchronize_sched();
2380 blocking_notifier_call_chain(&module_notify_list
,
2381 MODULE_STATE_GOING
, mod
);
2382 mutex_lock(&module_mutex
);
2384 mutex_unlock(&module_mutex
);
2385 wake_up(&module_wq
);
2389 printk(KERN_WARNING
"%s: '%s'->init suspiciously returned %d, "
2390 "it should follow 0/-E convention\n"
2391 KERN_WARNING
"%s: loading module anyway...\n",
2392 __func__
, mod
->name
, ret
,
2397 /* Now it's a first class citizen! Wake up anyone waiting for it. */
2398 mod
->state
= MODULE_STATE_LIVE
;
2399 wake_up(&module_wq
);
2400 blocking_notifier_call_chain(&module_notify_list
,
2401 MODULE_STATE_LIVE
, mod
);
2403 mutex_lock(&module_mutex
);
2404 /* Drop initial reference. */
2406 module_free(mod
, mod
->module_init
);
2407 mod
->module_init
= NULL
;
2409 mod
->init_text_size
= 0;
2410 mutex_unlock(&module_mutex
);
2415 static inline int within(unsigned long addr
, void *start
, unsigned long size
)
2417 return ((void *)addr
>= start
&& (void *)addr
< start
+ size
);
2420 #ifdef CONFIG_KALLSYMS
2422 * This ignores the intensely annoying "mapping symbols" found
2423 * in ARM ELF files: $a, $t and $d.
2425 static inline int is_arm_mapping_symbol(const char *str
)
2427 return str
[0] == '$' && strchr("atd", str
[1])
2428 && (str
[2] == '\0' || str
[2] == '.');
2431 static const char *get_ksymbol(struct module
*mod
,
2433 unsigned long *size
,
2434 unsigned long *offset
)
2436 unsigned int i
, best
= 0;
2437 unsigned long nextval
;
2439 /* At worse, next value is at end of module */
2440 if (within_module_init(addr
, mod
))
2441 nextval
= (unsigned long)mod
->module_init
+mod
->init_text_size
;
2443 nextval
= (unsigned long)mod
->module_core
+mod
->core_text_size
;
2445 /* Scan for closest preceeding symbol, and next symbol. (ELF
2446 starts real symbols at 1). */
2447 for (i
= 1; i
< mod
->num_symtab
; i
++) {
2448 if (mod
->symtab
[i
].st_shndx
== SHN_UNDEF
)
2451 /* We ignore unnamed symbols: they're uninformative
2452 * and inserted at a whim. */
2453 if (mod
->symtab
[i
].st_value
<= addr
2454 && mod
->symtab
[i
].st_value
> mod
->symtab
[best
].st_value
2455 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
2456 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
2458 if (mod
->symtab
[i
].st_value
> addr
2459 && mod
->symtab
[i
].st_value
< nextval
2460 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
2461 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
2462 nextval
= mod
->symtab
[i
].st_value
;
2469 *size
= nextval
- mod
->symtab
[best
].st_value
;
2471 *offset
= addr
- mod
->symtab
[best
].st_value
;
2472 return mod
->strtab
+ mod
->symtab
[best
].st_name
;
2475 /* For kallsyms to ask for address resolution. NULL means not found. Careful
2476 * not to lock to avoid deadlock on oopses, simply disable preemption. */
2477 const char *module_address_lookup(unsigned long addr
,
2478 unsigned long *size
,
2479 unsigned long *offset
,
2484 const char *ret
= NULL
;
2487 list_for_each_entry_rcu(mod
, &modules
, list
) {
2488 if (within_module_init(addr
, mod
) ||
2489 within_module_core(addr
, mod
)) {
2491 *modname
= mod
->name
;
2492 ret
= get_ksymbol(mod
, addr
, size
, offset
);
2496 /* Make a copy in here where it's safe */
2498 strncpy(namebuf
, ret
, KSYM_NAME_LEN
- 1);
2505 int lookup_module_symbol_name(unsigned long addr
, char *symname
)
2510 list_for_each_entry_rcu(mod
, &modules
, list
) {
2511 if (within_module_init(addr
, mod
) ||
2512 within_module_core(addr
, mod
)) {
2515 sym
= get_ksymbol(mod
, addr
, NULL
, NULL
);
2518 strlcpy(symname
, sym
, KSYM_NAME_LEN
);
2528 int lookup_module_symbol_attrs(unsigned long addr
, unsigned long *size
,
2529 unsigned long *offset
, char *modname
, char *name
)
2534 list_for_each_entry_rcu(mod
, &modules
, list
) {
2535 if (within_module_init(addr
, mod
) ||
2536 within_module_core(addr
, mod
)) {
2539 sym
= get_ksymbol(mod
, addr
, size
, offset
);
2543 strlcpy(modname
, mod
->name
, MODULE_NAME_LEN
);
2545 strlcpy(name
, sym
, KSYM_NAME_LEN
);
2555 int module_get_kallsym(unsigned int symnum
, unsigned long *value
, char *type
,
2556 char *name
, char *module_name
, int *exported
)
2561 list_for_each_entry_rcu(mod
, &modules
, list
) {
2562 if (symnum
< mod
->num_symtab
) {
2563 *value
= mod
->symtab
[symnum
].st_value
;
2564 *type
= mod
->symtab
[symnum
].st_info
;
2565 strlcpy(name
, mod
->strtab
+ mod
->symtab
[symnum
].st_name
,
2567 strlcpy(module_name
, mod
->name
, MODULE_NAME_LEN
);
2568 *exported
= is_exported(name
, *value
, mod
);
2572 symnum
-= mod
->num_symtab
;
2578 static unsigned long mod_find_symname(struct module
*mod
, const char *name
)
2582 for (i
= 0; i
< mod
->num_symtab
; i
++)
2583 if (strcmp(name
, mod
->strtab
+mod
->symtab
[i
].st_name
) == 0 &&
2584 mod
->symtab
[i
].st_info
!= 'U')
2585 return mod
->symtab
[i
].st_value
;
2589 /* Look for this name: can be of form module:name. */
2590 unsigned long module_kallsyms_lookup_name(const char *name
)
2594 unsigned long ret
= 0;
2596 /* Don't lock: we're in enough trouble already. */
2598 if ((colon
= strchr(name
, ':')) != NULL
) {
2600 if ((mod
= find_module(name
)) != NULL
)
2601 ret
= mod_find_symname(mod
, colon
+1);
2604 list_for_each_entry_rcu(mod
, &modules
, list
)
2605 if ((ret
= mod_find_symname(mod
, name
)) != 0)
2612 int module_kallsyms_on_each_symbol(int (*fn
)(void *, const char *,
2613 struct module
*, unsigned long),
2620 list_for_each_entry(mod
, &modules
, list
) {
2621 for (i
= 0; i
< mod
->num_symtab
; i
++) {
2622 ret
= fn(data
, mod
->strtab
+ mod
->symtab
[i
].st_name
,
2623 mod
, mod
->symtab
[i
].st_value
);
2630 #endif /* CONFIG_KALLSYMS */
2632 static char *module_flags(struct module
*mod
, char *buf
)
2637 mod
->state
== MODULE_STATE_GOING
||
2638 mod
->state
== MODULE_STATE_COMING
) {
2640 if (mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
))
2642 if (mod
->taints
& (1 << TAINT_FORCED_MODULE
))
2644 if (mod
->taints
& (1 << TAINT_CRAP
))
2647 * TAINT_FORCED_RMMOD: could be added.
2648 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2652 /* Show a - for module-is-being-unloaded */
2653 if (mod
->state
== MODULE_STATE_GOING
)
2655 /* Show a + for module-is-being-loaded */
2656 if (mod
->state
== MODULE_STATE_COMING
)
2665 #ifdef CONFIG_PROC_FS
2666 /* Called by the /proc file system to return a list of modules. */
2667 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
2669 mutex_lock(&module_mutex
);
2670 return seq_list_start(&modules
, *pos
);
2673 static void *m_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
2675 return seq_list_next(p
, &modules
, pos
);
2678 static void m_stop(struct seq_file
*m
, void *p
)
2680 mutex_unlock(&module_mutex
);
2683 static int m_show(struct seq_file
*m
, void *p
)
2685 struct module
*mod
= list_entry(p
, struct module
, list
);
2688 seq_printf(m
, "%s %u",
2689 mod
->name
, mod
->init_size
+ mod
->core_size
);
2690 print_unload_info(m
, mod
);
2692 /* Informative for users. */
2693 seq_printf(m
, " %s",
2694 mod
->state
== MODULE_STATE_GOING
? "Unloading":
2695 mod
->state
== MODULE_STATE_COMING
? "Loading":
2697 /* Used by oprofile and other similar tools. */
2698 seq_printf(m
, " 0x%p", mod
->module_core
);
2702 seq_printf(m
, " %s", module_flags(mod
, buf
));
2704 seq_printf(m
, "\n");
2708 /* Format: modulename size refcount deps address
2710 Where refcount is a number or -, and deps is a comma-separated list
2713 static const struct seq_operations modules_op
= {
2720 static int modules_open(struct inode
*inode
, struct file
*file
)
2722 return seq_open(file
, &modules_op
);
2725 static const struct file_operations proc_modules_operations
= {
2726 .open
= modules_open
,
2728 .llseek
= seq_lseek
,
2729 .release
= seq_release
,
2732 static int __init
proc_modules_init(void)
2734 proc_create("modules", 0, NULL
, &proc_modules_operations
);
2737 module_init(proc_modules_init
);
2740 /* Given an address, look for it in the module exception tables. */
2741 const struct exception_table_entry
*search_module_extables(unsigned long addr
)
2743 const struct exception_table_entry
*e
= NULL
;
2747 list_for_each_entry_rcu(mod
, &modules
, list
) {
2748 if (mod
->num_exentries
== 0)
2751 e
= search_extable(mod
->extable
,
2752 mod
->extable
+ mod
->num_exentries
- 1,
2759 /* Now, if we found one, we are running inside it now, hence
2760 we cannot unload the module, hence no refcnt needed. */
2765 * is_module_address - is this address inside a module?
2766 * @addr: the address to check.
2768 * See is_module_text_address() if you simply want to see if the address
2769 * is code (not data).
2771 bool is_module_address(unsigned long addr
)
2776 ret
= __module_address(addr
) != NULL
;
2783 * __module_address - get the module which contains an address.
2784 * @addr: the address.
2786 * Must be called with preempt disabled or module mutex held so that
2787 * module doesn't get freed during this.
2789 __notrace_funcgraph
struct module
*__module_address(unsigned long addr
)
2793 if (addr
< module_addr_min
|| addr
> module_addr_max
)
2796 list_for_each_entry_rcu(mod
, &modules
, list
)
2797 if (within_module_core(addr
, mod
)
2798 || within_module_init(addr
, mod
))
2802 EXPORT_SYMBOL_GPL(__module_address
);
2805 * is_module_text_address - is this address inside module code?
2806 * @addr: the address to check.
2808 * See is_module_address() if you simply want to see if the address is
2809 * anywhere in a module. See kernel_text_address() for testing if an
2810 * address corresponds to kernel or module code.
2812 bool is_module_text_address(unsigned long addr
)
2817 ret
= __module_text_address(addr
) != NULL
;
2824 * __module_text_address - get the module whose code contains an address.
2825 * @addr: the address.
2827 * Must be called with preempt disabled or module mutex held so that
2828 * module doesn't get freed during this.
2830 struct module
*__module_text_address(unsigned long addr
)
2832 struct module
*mod
= __module_address(addr
);
2834 /* Make sure it's within the text section. */
2835 if (!within(addr
, mod
->module_init
, mod
->init_text_size
)
2836 && !within(addr
, mod
->module_core
, mod
->core_text_size
))
2841 EXPORT_SYMBOL_GPL(__module_text_address
);
2843 /* Don't grab lock, we're oopsing. */
2844 void print_modules(void)
2849 printk("Modules linked in:");
2850 /* Most callers should already have preempt disabled, but make sure */
2852 list_for_each_entry_rcu(mod
, &modules
, list
)
2853 printk(" %s%s", mod
->name
, module_flags(mod
, buf
));
2855 if (last_unloaded_module
[0])
2856 printk(" [last unloaded: %s]", last_unloaded_module
);
2860 #ifdef CONFIG_MODVERSIONS
2861 /* Generate the signature for struct module here, too, for modversions. */
2862 void struct_module(struct module
*mod
) { return; }
2863 EXPORT_SYMBOL(struct_module
);
2866 #ifdef CONFIG_MARKERS
2867 void module_update_markers(void)
2871 mutex_lock(&module_mutex
);
2872 list_for_each_entry(mod
, &modules
, list
)
2874 marker_update_probe_range(mod
->markers
,
2875 mod
->markers
+ mod
->num_markers
);
2876 mutex_unlock(&module_mutex
);
2880 #ifdef CONFIG_TRACEPOINTS
2881 void module_update_tracepoints(void)
2885 mutex_lock(&module_mutex
);
2886 list_for_each_entry(mod
, &modules
, list
)
2888 tracepoint_update_probe_range(mod
->tracepoints
,
2889 mod
->tracepoints
+ mod
->num_tracepoints
);
2890 mutex_unlock(&module_mutex
);
2894 * Returns 0 if current not found.
2895 * Returns 1 if current found.
2897 int module_get_iter_tracepoints(struct tracepoint_iter
*iter
)
2899 struct module
*iter_mod
;
2902 mutex_lock(&module_mutex
);
2903 list_for_each_entry(iter_mod
, &modules
, list
) {
2904 if (!iter_mod
->taints
) {
2906 * Sorted module list
2908 if (iter_mod
< iter
->module
)
2910 else if (iter_mod
> iter
->module
)
2911 iter
->tracepoint
= NULL
;
2912 found
= tracepoint_get_iter_range(&iter
->tracepoint
,
2913 iter_mod
->tracepoints
,
2914 iter_mod
->tracepoints
2915 + iter_mod
->num_tracepoints
);
2917 iter
->module
= iter_mod
;
2922 mutex_unlock(&module_mutex
);