Arjan van de Ven | f71d20e | 2006-06-28 04:26:45 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | Copyright (C) 2002 Richard Henderson |
Rusty Russell | 51f3d0f | 2010-08-05 12:59:13 -0600 | [diff] [blame] | 3 | Copyright (C) 2001 Rusty Russell, 2002, 2010 Rusty Russell IBM. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | |
| 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. |
| 9 | |
| 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. |
| 14 | |
| 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 |
| 18 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/module.h> |
| 20 | #include <linux/moduleloader.h> |
Steven Rostedt | 6d72373 | 2009-04-10 14:53:50 -0400 | [diff] [blame] | 21 | #include <linux/ftrace_event.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/init.h> |
Alexey Dobriyan | ae84e32 | 2007-05-08 00:28:38 -0700 | [diff] [blame] | 23 | #include <linux/kallsyms.h> |
Alexey Dobriyan | 3b5d5c6 | 2008-10-06 13:19:27 +0400 | [diff] [blame] | 24 | #include <linux/fs.h> |
Roland McGrath | 6d76013 | 2007-10-16 23:26:40 -0700 | [diff] [blame] | 25 | #include <linux/sysfs.h> |
Randy Dunlap | 9f15833 | 2005-09-13 01:25:16 -0700 | [diff] [blame] | 26 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/slab.h> |
| 28 | #include <linux/vmalloc.h> |
| 29 | #include <linux/elf.h> |
Alexey Dobriyan | 3b5d5c6 | 2008-10-06 13:19:27 +0400 | [diff] [blame] | 30 | #include <linux/proc_fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/seq_file.h> |
| 32 | #include <linux/syscalls.h> |
| 33 | #include <linux/fcntl.h> |
| 34 | #include <linux/rcupdate.h> |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 35 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/cpu.h> |
| 37 | #include <linux/moduleparam.h> |
| 38 | #include <linux/errno.h> |
| 39 | #include <linux/err.h> |
| 40 | #include <linux/vermagic.h> |
| 41 | #include <linux/notifier.h> |
Al Viro | f6a5703 | 2006-10-18 01:47:25 -0400 | [diff] [blame] | 42 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <linux/stop_machine.h> |
| 44 | #include <linux/device.h> |
Matt Domsch | c988d2b | 2005-06-23 22:05:15 -0700 | [diff] [blame] | 45 | #include <linux/string.h> |
Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 46 | #include <linux/mutex.h> |
Andi Kleen | d72b375 | 2008-08-30 10:09:00 +0200 | [diff] [blame] | 47 | #include <linux/rculist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <asm/cacheflush.h> |
Bernd Schmidt | eb8cdec | 2009-09-21 17:03:57 -0700 | [diff] [blame] | 50 | #include <asm/mmu_context.h> |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 51 | #include <linux/license.h> |
Christoph Lameter | 6d76239 | 2008-02-08 04:18:42 -0800 | [diff] [blame] | 52 | #include <asm/sections.h> |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 53 | #include <linux/tracepoint.h> |
Steven Rostedt | 90d595f | 2008-08-14 15:45:09 -0400 | [diff] [blame] | 54 | #include <linux/ftrace.h> |
Arjan van de Ven | 22a9d64 | 2009-01-07 08:45:46 -0800 | [diff] [blame] | 55 | #include <linux/async.h> |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 56 | #include <linux/percpu.h> |
Catalin Marinas | 4f2294b | 2009-06-11 13:23:20 +0100 | [diff] [blame] | 57 | #include <linux/kmemleak.h> |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 58 | #include <linux/jump_label.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Li Zefan | 7ead8b8 | 2009-08-17 16:56:28 +0800 | [diff] [blame] | 60 | #define CREATE_TRACE_POINTS |
| 61 | #include <trace/events/module.h> |
| 62 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #if 0 |
| 64 | #define DEBUGP printk |
| 65 | #else |
| 66 | #define DEBUGP(fmt , a...) |
| 67 | #endif |
| 68 | |
| 69 | #ifndef ARCH_SHF_SMALL |
| 70 | #define ARCH_SHF_SMALL 0 |
| 71 | #endif |
| 72 | |
| 73 | /* If this is set, the section belongs in the init part of the module */ |
| 74 | #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1)) |
| 75 | |
Rusty Russell | 7567650 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 76 | /* |
| 77 | * Mutex protects: |
| 78 | * 1) List of modules (also safely readable with preempt_disable), |
| 79 | * 2) module_use links, |
| 80 | * 3) module_addr_min/module_addr_max. |
Andi Kleen | d72b375 | 2008-08-30 10:09:00 +0200 | [diff] [blame] | 81 | * (delete uses stop_machine/add uses RCU list operations). */ |
Tim Abbott | c6b3780 | 2008-12-05 19:03:59 -0500 | [diff] [blame] | 82 | DEFINE_MUTEX(module_mutex); |
| 83 | EXPORT_SYMBOL_GPL(module_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | static LIST_HEAD(modules); |
Jason Wessel | 67fc4e0 | 2010-05-20 21:04:21 -0500 | [diff] [blame] | 85 | #ifdef CONFIG_KGDB_KDB |
| 86 | struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */ |
| 87 | #endif /* CONFIG_KGDB_KDB */ |
| 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
Stephen Rothwell | 19e4529 | 2009-04-14 17:27:18 +1000 | [diff] [blame] | 90 | /* Block module loading/unloading? */ |
| 91 | int modules_disabled = 0; |
| 92 | |
Rusty Russell | c9a3ba5 | 2008-01-29 17:13:18 -0500 | [diff] [blame] | 93 | /* Waiting for a module to finish initializing? */ |
| 94 | static DECLARE_WAIT_QUEUE_HEAD(module_wq); |
| 95 | |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 96 | static BLOCKING_NOTIFIER_HEAD(module_notify_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Rusty Russell | 7567650 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 98 | /* Bounds of module allocation, for speeding __module_address. |
| 99 | * Protected by module_mutex. */ |
Rusty Russell | 3a642e9 | 2008-07-22 19:24:28 -0500 | [diff] [blame] | 100 | static unsigned long module_addr_min = -1UL, module_addr_max = 0; |
| 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | int register_module_notifier(struct notifier_block * nb) |
| 103 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 104 | return blocking_notifier_chain_register(&module_notify_list, nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | EXPORT_SYMBOL(register_module_notifier); |
| 107 | |
| 108 | int unregister_module_notifier(struct notifier_block * nb) |
| 109 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 110 | return blocking_notifier_chain_unregister(&module_notify_list, nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | EXPORT_SYMBOL(unregister_module_notifier); |
| 113 | |
Rusty Russell | eded41c | 2010-08-05 12:59:07 -0600 | [diff] [blame] | 114 | struct load_info { |
| 115 | Elf_Ehdr *hdr; |
| 116 | unsigned long len; |
| 117 | Elf_Shdr *sechdrs; |
Rusty Russell | 6526c53 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 118 | char *secstrings, *strtab; |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 119 | unsigned long *strmap; |
| 120 | unsigned long symoffs, stroffs; |
Rusty Russell | 811d66a | 2010-08-05 12:59:12 -0600 | [diff] [blame] | 121 | struct _ddebug *debug; |
| 122 | unsigned int num_debug; |
Rusty Russell | eded41c | 2010-08-05 12:59:07 -0600 | [diff] [blame] | 123 | struct { |
| 124 | unsigned int sym, str, mod, vers, info, pcpu; |
| 125 | } index; |
| 126 | }; |
| 127 | |
Matti Linnanvuori | 9a4b970 | 2007-11-08 08:37:38 -0800 | [diff] [blame] | 128 | /* We require a truly strong try_module_get(): 0 means failure due to |
| 129 | ongoing or failed initialization etc. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | static inline int strong_try_module_get(struct module *mod) |
| 131 | { |
| 132 | if (mod && mod->state == MODULE_STATE_COMING) |
Rusty Russell | c9a3ba5 | 2008-01-29 17:13:18 -0500 | [diff] [blame] | 133 | return -EBUSY; |
| 134 | if (try_module_get(mod)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | return 0; |
Rusty Russell | c9a3ba5 | 2008-01-29 17:13:18 -0500 | [diff] [blame] | 136 | else |
| 137 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Florin Malita | fa3ba2e8 | 2006-10-11 01:21:48 -0700 | [diff] [blame] | 140 | static inline void add_taint_module(struct module *mod, unsigned flag) |
| 141 | { |
| 142 | add_taint(flag); |
Andi Kleen | 25ddbb1 | 2008-10-15 22:01:41 -0700 | [diff] [blame] | 143 | mod->taints |= (1U << flag); |
Florin Malita | fa3ba2e8 | 2006-10-11 01:21:48 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Robert P. J. Day | 02a3e59 | 2007-05-09 07:26:28 +0200 | [diff] [blame] | 146 | /* |
| 147 | * A thread that wants to hold a reference to a module only while it |
| 148 | * is running can call this to safely exit. nfsd and lockd use this. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | */ |
| 150 | void __module_put_and_exit(struct module *mod, long code) |
| 151 | { |
| 152 | module_put(mod); |
| 153 | do_exit(code); |
| 154 | } |
| 155 | EXPORT_SYMBOL(__module_put_and_exit); |
Daniel Walker | 22a8bde | 2007-10-18 03:06:07 -0700 | [diff] [blame] | 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | /* Find a module section: 0 means not found. */ |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 158 | static unsigned int find_sec(const struct load_info *info, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | { |
| 160 | unsigned int i; |
| 161 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 162 | for (i = 1; i < info->hdr->e_shnum; i++) { |
| 163 | Elf_Shdr *shdr = &info->sechdrs[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | /* Alloc bit cleared means "ignore it." */ |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 165 | if ((shdr->sh_flags & SHF_ALLOC) |
| 166 | && strcmp(info->secstrings + shdr->sh_name, name) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | return i; |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 168 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | return 0; |
| 170 | } |
| 171 | |
Rusty Russell | 5e458cc | 2008-10-22 10:00:13 -0500 | [diff] [blame] | 172 | /* Find a module section, or NULL. */ |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 173 | static void *section_addr(const struct load_info *info, const char *name) |
Rusty Russell | 5e458cc | 2008-10-22 10:00:13 -0500 | [diff] [blame] | 174 | { |
| 175 | /* Section 0 has sh_addr 0. */ |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 176 | return (void *)info->sechdrs[find_sec(info, name)].sh_addr; |
Rusty Russell | 5e458cc | 2008-10-22 10:00:13 -0500 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /* Find a module section, or NULL. Fill in number of "objects" in section. */ |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 180 | static void *section_objs(const struct load_info *info, |
Rusty Russell | 5e458cc | 2008-10-22 10:00:13 -0500 | [diff] [blame] | 181 | const char *name, |
| 182 | size_t object_size, |
| 183 | unsigned int *num) |
| 184 | { |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 185 | unsigned int sec = find_sec(info, name); |
Rusty Russell | 5e458cc | 2008-10-22 10:00:13 -0500 | [diff] [blame] | 186 | |
| 187 | /* Section 0 has sh_addr 0 and sh_size 0. */ |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 188 | *num = info->sechdrs[sec].sh_size / object_size; |
| 189 | return (void *)info->sechdrs[sec].sh_addr; |
Rusty Russell | 5e458cc | 2008-10-22 10:00:13 -0500 | [diff] [blame] | 190 | } |
| 191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | /* Provided by the linker */ |
| 193 | extern const struct kernel_symbol __start___ksymtab[]; |
| 194 | extern const struct kernel_symbol __stop___ksymtab[]; |
| 195 | extern const struct kernel_symbol __start___ksymtab_gpl[]; |
| 196 | extern const struct kernel_symbol __stop___ksymtab_gpl[]; |
Greg Kroah-Hartman | 9f28bb7 | 2006-03-20 13:17:13 -0800 | [diff] [blame] | 197 | extern const struct kernel_symbol __start___ksymtab_gpl_future[]; |
| 198 | extern const struct kernel_symbol __stop___ksymtab_gpl_future[]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | extern const unsigned long __start___kcrctab[]; |
| 200 | extern const unsigned long __start___kcrctab_gpl[]; |
Greg Kroah-Hartman | 9f28bb7 | 2006-03-20 13:17:13 -0800 | [diff] [blame] | 201 | extern const unsigned long __start___kcrctab_gpl_future[]; |
Denys Vlasenko | f7f5b67 | 2008-07-22 19:24:26 -0500 | [diff] [blame] | 202 | #ifdef CONFIG_UNUSED_SYMBOLS |
| 203 | extern const struct kernel_symbol __start___ksymtab_unused[]; |
| 204 | extern const struct kernel_symbol __stop___ksymtab_unused[]; |
| 205 | extern const struct kernel_symbol __start___ksymtab_unused_gpl[]; |
| 206 | extern const struct kernel_symbol __stop___ksymtab_unused_gpl[]; |
Arjan van de Ven | f71d20e | 2006-06-28 04:26:45 -0700 | [diff] [blame] | 207 | extern const unsigned long __start___kcrctab_unused[]; |
| 208 | extern const unsigned long __start___kcrctab_unused_gpl[]; |
Denys Vlasenko | f7f5b67 | 2008-07-22 19:24:26 -0500 | [diff] [blame] | 209 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
| 211 | #ifndef CONFIG_MODVERSIONS |
| 212 | #define symversion(base, idx) NULL |
| 213 | #else |
Andrew Morton | f83ca9f | 2006-03-28 01:56:20 -0800 | [diff] [blame] | 214 | #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | #endif |
| 216 | |
Rusty Russell | dafd094 | 2008-07-22 19:24:25 -0500 | [diff] [blame] | 217 | static bool each_symbol_in_section(const struct symsearch *arr, |
| 218 | unsigned int arrsize, |
| 219 | struct module *owner, |
| 220 | bool (*fn)(const struct symsearch *syms, |
| 221 | struct module *owner, |
| 222 | unsigned int symnum, void *data), |
| 223 | void *data) |
Sam Ravnborg | 3fd6805 | 2006-02-08 21:16:45 +0100 | [diff] [blame] | 224 | { |
Rusty Russell | dafd094 | 2008-07-22 19:24:25 -0500 | [diff] [blame] | 225 | unsigned int i, j; |
| 226 | |
| 227 | for (j = 0; j < arrsize; j++) { |
| 228 | for (i = 0; i < arr[j].stop - arr[j].start; i++) |
| 229 | if (fn(&arr[j], owner, i, data)) |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | return false; |
Sam Ravnborg | 3fd6805 | 2006-02-08 21:16:45 +0100 | [diff] [blame] | 234 | } |
| 235 | |
Rusty Russell | dafd094 | 2008-07-22 19:24:25 -0500 | [diff] [blame] | 236 | /* Returns true as soon as fn returns true, otherwise false. */ |
Tim Abbott | c6b3780 | 2008-12-05 19:03:59 -0500 | [diff] [blame] | 237 | bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner, |
| 238 | unsigned int symnum, void *data), void *data) |
Arjan van de Ven | f71d20e | 2006-06-28 04:26:45 -0700 | [diff] [blame] | 239 | { |
Rusty Russell | dafd094 | 2008-07-22 19:24:25 -0500 | [diff] [blame] | 240 | struct module *mod; |
Linus Torvalds | 44032e6 | 2010-08-05 12:59:05 -0600 | [diff] [blame] | 241 | static const struct symsearch arr[] = { |
Rusty Russell | dafd094 | 2008-07-22 19:24:25 -0500 | [diff] [blame] | 242 | { __start___ksymtab, __stop___ksymtab, __start___kcrctab, |
| 243 | NOT_GPL_ONLY, false }, |
| 244 | { __start___ksymtab_gpl, __stop___ksymtab_gpl, |
| 245 | __start___kcrctab_gpl, |
| 246 | GPL_ONLY, false }, |
| 247 | { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future, |
| 248 | __start___kcrctab_gpl_future, |
| 249 | WILL_BE_GPL_ONLY, false }, |
Denys Vlasenko | f7f5b67 | 2008-07-22 19:24:26 -0500 | [diff] [blame] | 250 | #ifdef CONFIG_UNUSED_SYMBOLS |
Rusty Russell | dafd094 | 2008-07-22 19:24:25 -0500 | [diff] [blame] | 251 | { __start___ksymtab_unused, __stop___ksymtab_unused, |
| 252 | __start___kcrctab_unused, |
| 253 | NOT_GPL_ONLY, true }, |
| 254 | { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl, |
| 255 | __start___kcrctab_unused_gpl, |
| 256 | GPL_ONLY, true }, |
Denys Vlasenko | f7f5b67 | 2008-07-22 19:24:26 -0500 | [diff] [blame] | 257 | #endif |
Rusty Russell | dafd094 | 2008-07-22 19:24:25 -0500 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data)) |
| 261 | return true; |
| 262 | |
Andi Kleen | d72b375 | 2008-08-30 10:09:00 +0200 | [diff] [blame] | 263 | list_for_each_entry_rcu(mod, &modules, list) { |
Rusty Russell | dafd094 | 2008-07-22 19:24:25 -0500 | [diff] [blame] | 264 | struct symsearch arr[] = { |
| 265 | { mod->syms, mod->syms + mod->num_syms, mod->crcs, |
| 266 | NOT_GPL_ONLY, false }, |
| 267 | { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms, |
| 268 | mod->gpl_crcs, |
| 269 | GPL_ONLY, false }, |
| 270 | { mod->gpl_future_syms, |
| 271 | mod->gpl_future_syms + mod->num_gpl_future_syms, |
| 272 | mod->gpl_future_crcs, |
| 273 | WILL_BE_GPL_ONLY, false }, |
Denys Vlasenko | f7f5b67 | 2008-07-22 19:24:26 -0500 | [diff] [blame] | 274 | #ifdef CONFIG_UNUSED_SYMBOLS |
Rusty Russell | dafd094 | 2008-07-22 19:24:25 -0500 | [diff] [blame] | 275 | { mod->unused_syms, |
| 276 | mod->unused_syms + mod->num_unused_syms, |
| 277 | mod->unused_crcs, |
| 278 | NOT_GPL_ONLY, true }, |
| 279 | { mod->unused_gpl_syms, |
| 280 | mod->unused_gpl_syms + mod->num_unused_gpl_syms, |
| 281 | mod->unused_gpl_crcs, |
| 282 | GPL_ONLY, true }, |
Denys Vlasenko | f7f5b67 | 2008-07-22 19:24:26 -0500 | [diff] [blame] | 283 | #endif |
Rusty Russell | dafd094 | 2008-07-22 19:24:25 -0500 | [diff] [blame] | 284 | }; |
| 285 | |
| 286 | if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data)) |
| 287 | return true; |
| 288 | } |
| 289 | return false; |
Arjan van de Ven | f71d20e | 2006-06-28 04:26:45 -0700 | [diff] [blame] | 290 | } |
Tim Abbott | c6b3780 | 2008-12-05 19:03:59 -0500 | [diff] [blame] | 291 | EXPORT_SYMBOL_GPL(each_symbol); |
Arjan van de Ven | f71d20e | 2006-06-28 04:26:45 -0700 | [diff] [blame] | 292 | |
Rusty Russell | dafd094 | 2008-07-22 19:24:25 -0500 | [diff] [blame] | 293 | struct find_symbol_arg { |
| 294 | /* Input */ |
| 295 | const char *name; |
| 296 | bool gplok; |
| 297 | bool warn; |
| 298 | |
| 299 | /* Output */ |
| 300 | struct module *owner; |
| 301 | const unsigned long *crc; |
Tim Abbott | 414fd31 | 2008-12-05 19:03:56 -0500 | [diff] [blame] | 302 | const struct kernel_symbol *sym; |
Rusty Russell | dafd094 | 2008-07-22 19:24:25 -0500 | [diff] [blame] | 303 | }; |
| 304 | |
| 305 | static bool find_symbol_in_section(const struct symsearch *syms, |
| 306 | struct module *owner, |
| 307 | unsigned int symnum, void *data) |
Rusty Russell | ad9546c | 2008-05-01 21:14:59 -0500 | [diff] [blame] | 308 | { |
Rusty Russell | dafd094 | 2008-07-22 19:24:25 -0500 | [diff] [blame] | 309 | struct find_symbol_arg *fsa = data; |
| 310 | |
| 311 | if (strcmp(syms->start[symnum].name, fsa->name) != 0) |
| 312 | return false; |
| 313 | |
| 314 | if (!fsa->gplok) { |
| 315 | if (syms->licence == GPL_ONLY) |
| 316 | return false; |
| 317 | if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) { |
| 318 | printk(KERN_WARNING "Symbol %s is being used " |
| 319 | "by a non-GPL module, which will not " |
| 320 | "be allowed in the future\n", fsa->name); |
| 321 | printk(KERN_WARNING "Please see the file " |
| 322 | "Documentation/feature-removal-schedule.txt " |
| 323 | "in the kernel source tree for more details.\n"); |
| 324 | } |
| 325 | } |
| 326 | |
Denys Vlasenko | f7f5b67 | 2008-07-22 19:24:26 -0500 | [diff] [blame] | 327 | #ifdef CONFIG_UNUSED_SYMBOLS |
Rusty Russell | dafd094 | 2008-07-22 19:24:25 -0500 | [diff] [blame] | 328 | if (syms->unused && fsa->warn) { |
Rusty Russell | ad9546c | 2008-05-01 21:14:59 -0500 | [diff] [blame] | 329 | printk(KERN_WARNING "Symbol %s is marked as UNUSED, " |
Rusty Russell | dafd094 | 2008-07-22 19:24:25 -0500 | [diff] [blame] | 330 | "however this module is using it.\n", fsa->name); |
Rusty Russell | ad9546c | 2008-05-01 21:14:59 -0500 | [diff] [blame] | 331 | printk(KERN_WARNING |
| 332 | "This symbol will go away in the future.\n"); |
| 333 | printk(KERN_WARNING |
| 334 | "Please evalute if this is the right api to use and if " |
| 335 | "it really is, submit a report the linux kernel " |
| 336 | "mailinglist together with submitting your code for " |
| 337 | "inclusion.\n"); |
| 338 | } |
Denys Vlasenko | f7f5b67 | 2008-07-22 19:24:26 -0500 | [diff] [blame] | 339 | #endif |
Rusty Russell | dafd094 | 2008-07-22 19:24:25 -0500 | [diff] [blame] | 340 | |
| 341 | fsa->owner = owner; |
| 342 | fsa->crc = symversion(syms->crcs, symnum); |
Tim Abbott | 414fd31 | 2008-12-05 19:03:56 -0500 | [diff] [blame] | 343 | fsa->sym = &syms->start[symnum]; |
Rusty Russell | ad9546c | 2008-05-01 21:14:59 -0500 | [diff] [blame] | 344 | return true; |
| 345 | } |
| 346 | |
Tim Abbott | 414fd31 | 2008-12-05 19:03:56 -0500 | [diff] [blame] | 347 | /* Find a symbol and return it, along with, (optional) crc and |
Rusty Russell | 7567650 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 348 | * (optional) module which owns it. Needs preempt disabled or module_mutex. */ |
Tim Abbott | c6b3780 | 2008-12-05 19:03:59 -0500 | [diff] [blame] | 349 | const struct kernel_symbol *find_symbol(const char *name, |
| 350 | struct module **owner, |
| 351 | const unsigned long **crc, |
| 352 | bool gplok, |
| 353 | bool warn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | { |
Rusty Russell | dafd094 | 2008-07-22 19:24:25 -0500 | [diff] [blame] | 355 | struct find_symbol_arg fsa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | |
Rusty Russell | dafd094 | 2008-07-22 19:24:25 -0500 | [diff] [blame] | 357 | fsa.name = name; |
| 358 | fsa.gplok = gplok; |
| 359 | fsa.warn = warn; |
| 360 | |
| 361 | if (each_symbol(find_symbol_in_section, &fsa)) { |
Rusty Russell | ad9546c | 2008-05-01 21:14:59 -0500 | [diff] [blame] | 362 | if (owner) |
Rusty Russell | dafd094 | 2008-07-22 19:24:25 -0500 | [diff] [blame] | 363 | *owner = fsa.owner; |
| 364 | if (crc) |
| 365 | *crc = fsa.crc; |
Tim Abbott | 414fd31 | 2008-12-05 19:03:56 -0500 | [diff] [blame] | 366 | return fsa.sym; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | } |
Rusty Russell | ad9546c | 2008-05-01 21:14:59 -0500 | [diff] [blame] | 368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | DEBUGP("Failed to find symbol %s\n", name); |
Tim Abbott | 414fd31 | 2008-12-05 19:03:56 -0500 | [diff] [blame] | 370 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | } |
Tim Abbott | c6b3780 | 2008-12-05 19:03:59 -0500 | [diff] [blame] | 372 | EXPORT_SYMBOL_GPL(find_symbol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | /* Search for module by name: must hold module_mutex. */ |
Tim Abbott | c6b3780 | 2008-12-05 19:03:59 -0500 | [diff] [blame] | 375 | struct module *find_module(const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | { |
| 377 | struct module *mod; |
| 378 | |
| 379 | list_for_each_entry(mod, &modules, list) { |
| 380 | if (strcmp(mod->name, name) == 0) |
| 381 | return mod; |
| 382 | } |
| 383 | return NULL; |
| 384 | } |
Tim Abbott | c6b3780 | 2008-12-05 19:03:59 -0500 | [diff] [blame] | 385 | EXPORT_SYMBOL_GPL(find_module); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
| 387 | #ifdef CONFIG_SMP |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 388 | |
Tejun Heo | 259354d | 2010-03-10 18:56:10 +0900 | [diff] [blame] | 389 | static inline void __percpu *mod_percpu(struct module *mod) |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 390 | { |
Tejun Heo | 259354d | 2010-03-10 18:56:10 +0900 | [diff] [blame] | 391 | return mod->percpu; |
| 392 | } |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 393 | |
Tejun Heo | 259354d | 2010-03-10 18:56:10 +0900 | [diff] [blame] | 394 | static int percpu_modalloc(struct module *mod, |
| 395 | unsigned long size, unsigned long align) |
| 396 | { |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 397 | if (align > PAGE_SIZE) { |
| 398 | printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n", |
Tejun Heo | 259354d | 2010-03-10 18:56:10 +0900 | [diff] [blame] | 399 | mod->name, align, PAGE_SIZE); |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 400 | align = PAGE_SIZE; |
| 401 | } |
| 402 | |
Tejun Heo | 259354d | 2010-03-10 18:56:10 +0900 | [diff] [blame] | 403 | mod->percpu = __alloc_reserved_percpu(size, align); |
| 404 | if (!mod->percpu) { |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 405 | printk(KERN_WARNING |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 406 | "%s: Could not allocate %lu bytes percpu data\n", |
| 407 | mod->name, size); |
Tejun Heo | 259354d | 2010-03-10 18:56:10 +0900 | [diff] [blame] | 408 | return -ENOMEM; |
| 409 | } |
| 410 | mod->percpu_size = size; |
| 411 | return 0; |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 412 | } |
| 413 | |
Tejun Heo | 259354d | 2010-03-10 18:56:10 +0900 | [diff] [blame] | 414 | static void percpu_modfree(struct module *mod) |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 415 | { |
Tejun Heo | 259354d | 2010-03-10 18:56:10 +0900 | [diff] [blame] | 416 | free_percpu(mod->percpu); |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 417 | } |
| 418 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 419 | static unsigned int find_pcpusec(struct load_info *info) |
Tejun Heo | 6b588c1 | 2009-02-20 16:29:07 +0900 | [diff] [blame] | 420 | { |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 421 | return find_sec(info, ".data..percpu"); |
Tejun Heo | 6b588c1 | 2009-02-20 16:29:07 +0900 | [diff] [blame] | 422 | } |
| 423 | |
Tejun Heo | 259354d | 2010-03-10 18:56:10 +0900 | [diff] [blame] | 424 | static void percpu_modcopy(struct module *mod, |
| 425 | const void *from, unsigned long size) |
Tejun Heo | 6b588c1 | 2009-02-20 16:29:07 +0900 | [diff] [blame] | 426 | { |
| 427 | int cpu; |
| 428 | |
| 429 | for_each_possible_cpu(cpu) |
Tejun Heo | 259354d | 2010-03-10 18:56:10 +0900 | [diff] [blame] | 430 | memcpy(per_cpu_ptr(mod->percpu, cpu), from, size); |
Tejun Heo | 6b588c1 | 2009-02-20 16:29:07 +0900 | [diff] [blame] | 431 | } |
| 432 | |
Tejun Heo | 10fad5e | 2010-03-10 18:57:54 +0900 | [diff] [blame] | 433 | /** |
| 434 | * is_module_percpu_address - test whether address is from module static percpu |
| 435 | * @addr: address to test |
| 436 | * |
| 437 | * Test whether @addr belongs to module static percpu area. |
| 438 | * |
| 439 | * RETURNS: |
| 440 | * %true if @addr is from module static percpu area |
| 441 | */ |
| 442 | bool is_module_percpu_address(unsigned long addr) |
| 443 | { |
| 444 | struct module *mod; |
| 445 | unsigned int cpu; |
| 446 | |
| 447 | preempt_disable(); |
| 448 | |
| 449 | list_for_each_entry_rcu(mod, &modules, list) { |
| 450 | if (!mod->percpu_size) |
| 451 | continue; |
| 452 | for_each_possible_cpu(cpu) { |
| 453 | void *start = per_cpu_ptr(mod->percpu, cpu); |
| 454 | |
| 455 | if ((void *)addr >= start && |
| 456 | (void *)addr < start + mod->percpu_size) { |
| 457 | preempt_enable(); |
| 458 | return true; |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | preempt_enable(); |
| 464 | return false; |
Daniel Walker | 22a8bde | 2007-10-18 03:06:07 -0700 | [diff] [blame] | 465 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | |
| 467 | #else /* ... !CONFIG_SMP */ |
Tejun Heo | 6b588c1 | 2009-02-20 16:29:07 +0900 | [diff] [blame] | 468 | |
Tejun Heo | 259354d | 2010-03-10 18:56:10 +0900 | [diff] [blame] | 469 | static inline void __percpu *mod_percpu(struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | { |
| 471 | return NULL; |
| 472 | } |
Tejun Heo | 259354d | 2010-03-10 18:56:10 +0900 | [diff] [blame] | 473 | static inline int percpu_modalloc(struct module *mod, |
| 474 | unsigned long size, unsigned long align) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | { |
Tejun Heo | 259354d | 2010-03-10 18:56:10 +0900 | [diff] [blame] | 476 | return -ENOMEM; |
| 477 | } |
| 478 | static inline void percpu_modfree(struct module *mod) |
| 479 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | } |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 481 | static unsigned int find_pcpusec(struct load_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | { |
| 483 | return 0; |
| 484 | } |
Tejun Heo | 259354d | 2010-03-10 18:56:10 +0900 | [diff] [blame] | 485 | static inline void percpu_modcopy(struct module *mod, |
| 486 | const void *from, unsigned long size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | { |
| 488 | /* pcpusec should be 0, and size of that section should be 0. */ |
| 489 | BUG_ON(size != 0); |
| 490 | } |
Tejun Heo | 10fad5e | 2010-03-10 18:57:54 +0900 | [diff] [blame] | 491 | bool is_module_percpu_address(unsigned long addr) |
| 492 | { |
| 493 | return false; |
| 494 | } |
Tejun Heo | 6b588c1 | 2009-02-20 16:29:07 +0900 | [diff] [blame] | 495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | #endif /* CONFIG_SMP */ |
| 497 | |
Matt Domsch | c988d2b | 2005-06-23 22:05:15 -0700 | [diff] [blame] | 498 | #define MODINFO_ATTR(field) \ |
| 499 | static void setup_modinfo_##field(struct module *mod, const char *s) \ |
| 500 | { \ |
| 501 | mod->field = kstrdup(s, GFP_KERNEL); \ |
| 502 | } \ |
| 503 | static ssize_t show_modinfo_##field(struct module_attribute *mattr, \ |
| 504 | struct module *mod, char *buffer) \ |
| 505 | { \ |
| 506 | return sprintf(buffer, "%s\n", mod->field); \ |
| 507 | } \ |
| 508 | static int modinfo_##field##_exists(struct module *mod) \ |
| 509 | { \ |
| 510 | return mod->field != NULL; \ |
| 511 | } \ |
| 512 | static void free_modinfo_##field(struct module *mod) \ |
| 513 | { \ |
Daniel Walker | 22a8bde | 2007-10-18 03:06:07 -0700 | [diff] [blame] | 514 | kfree(mod->field); \ |
| 515 | mod->field = NULL; \ |
Matt Domsch | c988d2b | 2005-06-23 22:05:15 -0700 | [diff] [blame] | 516 | } \ |
| 517 | static struct module_attribute modinfo_##field = { \ |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 518 | .attr = { .name = __stringify(field), .mode = 0444 }, \ |
Matt Domsch | c988d2b | 2005-06-23 22:05:15 -0700 | [diff] [blame] | 519 | .show = show_modinfo_##field, \ |
| 520 | .setup = setup_modinfo_##field, \ |
| 521 | .test = modinfo_##field##_exists, \ |
| 522 | .free = free_modinfo_##field, \ |
| 523 | }; |
| 524 | |
| 525 | MODINFO_ATTR(version); |
| 526 | MODINFO_ATTR(srcversion); |
| 527 | |
Arjan van de Ven | e14af7e | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 528 | static char last_unloaded_module[MODULE_NAME_LEN+1]; |
| 529 | |
Greg Kroah-Hartman | 03e88ae1 | 2006-02-16 13:50:23 -0800 | [diff] [blame] | 530 | #ifdef CONFIG_MODULE_UNLOAD |
Steven Rostedt | eb0c537 | 2010-03-29 14:25:18 -0400 | [diff] [blame] | 531 | |
| 532 | EXPORT_TRACEPOINT_SYMBOL(module_get); |
| 533 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | /* Init the unload section of the module. */ |
Rusty Russell | 9f85a4b | 2010-08-05 12:59:04 -0600 | [diff] [blame] | 535 | static int module_unload_init(struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | { |
Rusty Russell | 9f85a4b | 2010-08-05 12:59:04 -0600 | [diff] [blame] | 537 | mod->refptr = alloc_percpu(struct module_ref); |
| 538 | if (!mod->refptr) |
| 539 | return -ENOMEM; |
| 540 | |
Linus Torvalds | 2c02dfe | 2010-05-31 12:19:37 -0700 | [diff] [blame] | 541 | INIT_LIST_HEAD(&mod->source_list); |
| 542 | INIT_LIST_HEAD(&mod->target_list); |
Christoph Lameter | e1783a2 | 2010-01-05 15:34:50 +0900 | [diff] [blame] | 543 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | /* Hold reference count during initialization. */ |
Nick Piggin | 5fbfb18 | 2010-04-01 19:09:40 +1100 | [diff] [blame] | 545 | __this_cpu_write(mod->refptr->incs, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | /* Backwards compatibility macros put refcount during init. */ |
| 547 | mod->waiter = current; |
Rusty Russell | 9f85a4b | 2010-08-05 12:59:04 -0600 | [diff] [blame] | 548 | |
| 549 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | /* Does a already use b? */ |
| 553 | static int already_uses(struct module *a, struct module *b) |
| 554 | { |
| 555 | struct module_use *use; |
| 556 | |
Linus Torvalds | 2c02dfe | 2010-05-31 12:19:37 -0700 | [diff] [blame] | 557 | list_for_each_entry(use, &b->source_list, source_list) { |
| 558 | if (use->source == a) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | DEBUGP("%s uses %s!\n", a->name, b->name); |
| 560 | return 1; |
| 561 | } |
| 562 | } |
| 563 | DEBUGP("%s does not use %s!\n", a->name, b->name); |
| 564 | return 0; |
| 565 | } |
| 566 | |
Linus Torvalds | 2c02dfe | 2010-05-31 12:19:37 -0700 | [diff] [blame] | 567 | /* |
| 568 | * Module a uses b |
| 569 | * - we add 'a' as a "source", 'b' as a "target" of module use |
| 570 | * - the module_use is added to the list of 'b' sources (so |
| 571 | * 'b' can walk the list to see who sourced them), and of 'a' |
| 572 | * targets (so 'a' can see what modules it targets). |
| 573 | */ |
| 574 | static int add_module_usage(struct module *a, struct module *b) |
| 575 | { |
Linus Torvalds | 2c02dfe | 2010-05-31 12:19:37 -0700 | [diff] [blame] | 576 | struct module_use *use; |
| 577 | |
| 578 | DEBUGP("Allocating new usage for %s.\n", a->name); |
| 579 | use = kmalloc(sizeof(*use), GFP_ATOMIC); |
| 580 | if (!use) { |
| 581 | printk(KERN_WARNING "%s: out of memory loading\n", a->name); |
| 582 | return -ENOMEM; |
| 583 | } |
| 584 | |
| 585 | use->source = a; |
| 586 | use->target = b; |
| 587 | list_add(&use->source_list, &b->source_list); |
| 588 | list_add(&use->target_list, &a->target_list); |
Linus Torvalds | 2c02dfe | 2010-05-31 12:19:37 -0700 | [diff] [blame] | 589 | return 0; |
| 590 | } |
| 591 | |
Rusty Russell | 7567650 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 592 | /* Module a uses b: caller needs module_mutex() */ |
Rusty Russell | 9bea7f2 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 593 | int ref_module(struct module *a, struct module *b) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | { |
Rusty Russell | c8e21ce | 2010-06-05 11:17:35 -0600 | [diff] [blame] | 595 | int err; |
Kay Sievers | 270a6c4 | 2007-01-18 13:26:15 +0100 | [diff] [blame] | 596 | |
Rusty Russell | 9bea7f2 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 597 | if (b == NULL || already_uses(a, b)) |
Linus Torvalds | 218ce73 | 2010-05-25 16:48:30 -0700 | [diff] [blame] | 598 | return 0; |
Linus Torvalds | 218ce73 | 2010-05-25 16:48:30 -0700 | [diff] [blame] | 599 | |
Rusty Russell | 9bea7f2 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 600 | /* If module isn't available, we fail. */ |
| 601 | err = strong_try_module_get(b); |
Rusty Russell | c9a3ba5 | 2008-01-29 17:13:18 -0500 | [diff] [blame] | 602 | if (err) |
Rusty Russell | 9bea7f2 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 603 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | |
Linus Torvalds | 2c02dfe | 2010-05-31 12:19:37 -0700 | [diff] [blame] | 605 | err = add_module_usage(a, b); |
| 606 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | module_put(b); |
Rusty Russell | 9bea7f2 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 608 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | } |
Rusty Russell | 9bea7f2 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 610 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | } |
Rusty Russell | 9bea7f2 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 612 | EXPORT_SYMBOL_GPL(ref_module); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | |
| 614 | /* Clear the unload stuff of the module. */ |
| 615 | static void module_unload_free(struct module *mod) |
| 616 | { |
Linus Torvalds | 2c02dfe | 2010-05-31 12:19:37 -0700 | [diff] [blame] | 617 | struct module_use *use, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | |
Rusty Russell | 7567650 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 619 | mutex_lock(&module_mutex); |
Linus Torvalds | 2c02dfe | 2010-05-31 12:19:37 -0700 | [diff] [blame] | 620 | list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) { |
| 621 | struct module *i = use->target; |
| 622 | DEBUGP("%s unusing %s\n", mod->name, i->name); |
| 623 | module_put(i); |
| 624 | list_del(&use->source_list); |
| 625 | list_del(&use->target_list); |
| 626 | kfree(use); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | } |
Rusty Russell | 7567650 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 628 | mutex_unlock(&module_mutex); |
Rusty Russell | 9f85a4b | 2010-08-05 12:59:04 -0600 | [diff] [blame] | 629 | |
| 630 | free_percpu(mod->refptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | #ifdef CONFIG_MODULE_FORCE_UNLOAD |
Akinobu Mita | fb16979 | 2006-01-08 01:04:29 -0800 | [diff] [blame] | 634 | static inline int try_force_unload(unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | { |
| 636 | int ret = (flags & O_TRUNC); |
| 637 | if (ret) |
Akinobu Mita | fb16979 | 2006-01-08 01:04:29 -0800 | [diff] [blame] | 638 | add_taint(TAINT_FORCED_RMMOD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | return ret; |
| 640 | } |
| 641 | #else |
Akinobu Mita | fb16979 | 2006-01-08 01:04:29 -0800 | [diff] [blame] | 642 | static inline int try_force_unload(unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | { |
| 644 | return 0; |
| 645 | } |
| 646 | #endif /* CONFIG_MODULE_FORCE_UNLOAD */ |
| 647 | |
| 648 | struct stopref |
| 649 | { |
| 650 | struct module *mod; |
| 651 | int flags; |
| 652 | int *forced; |
| 653 | }; |
| 654 | |
| 655 | /* Whole machine is stopped with interrupts off when this runs. */ |
| 656 | static int __try_stop_module(void *_sref) |
| 657 | { |
| 658 | struct stopref *sref = _sref; |
| 659 | |
Rusty Russell | da39ba5 | 2008-07-22 19:24:25 -0500 | [diff] [blame] | 660 | /* If it's not unused, quit unless we're forcing. */ |
| 661 | if (module_refcount(sref->mod) != 0) { |
Akinobu Mita | fb16979 | 2006-01-08 01:04:29 -0800 | [diff] [blame] | 662 | if (!(*sref->forced = try_force_unload(sref->flags))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | return -EWOULDBLOCK; |
| 664 | } |
| 665 | |
| 666 | /* Mark it as dying. */ |
| 667 | sref->mod->state = MODULE_STATE_GOING; |
| 668 | return 0; |
| 669 | } |
| 670 | |
| 671 | static int try_stop_module(struct module *mod, int flags, int *forced) |
| 672 | { |
Rusty Russell | da39ba5 | 2008-07-22 19:24:25 -0500 | [diff] [blame] | 673 | if (flags & O_NONBLOCK) { |
| 674 | struct stopref sref = { mod, flags, forced }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | |
Rusty Russell | 9b1a4d3 | 2008-07-28 12:16:30 -0500 | [diff] [blame] | 676 | return stop_machine(__try_stop_module, &sref, NULL); |
Rusty Russell | da39ba5 | 2008-07-22 19:24:25 -0500 | [diff] [blame] | 677 | } else { |
| 678 | /* We don't need to stop the machine for this. */ |
| 679 | mod->state = MODULE_STATE_GOING; |
| 680 | synchronize_sched(); |
| 681 | return 0; |
| 682 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | unsigned int module_refcount(struct module *mod) |
| 686 | { |
Nick Piggin | 5fbfb18 | 2010-04-01 19:09:40 +1100 | [diff] [blame] | 687 | unsigned int incs = 0, decs = 0; |
Eric Dumazet | 720eba3 | 2009-02-03 13:31:36 +1030 | [diff] [blame] | 688 | int cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | |
Eric Dumazet | 720eba3 | 2009-02-03 13:31:36 +1030 | [diff] [blame] | 690 | for_each_possible_cpu(cpu) |
Nick Piggin | 5fbfb18 | 2010-04-01 19:09:40 +1100 | [diff] [blame] | 691 | decs += per_cpu_ptr(mod->refptr, cpu)->decs; |
| 692 | /* |
| 693 | * ensure the incs are added up after the decs. |
| 694 | * module_put ensures incs are visible before decs with smp_wmb. |
| 695 | * |
| 696 | * This 2-count scheme avoids the situation where the refcount |
| 697 | * for CPU0 is read, then CPU0 increments the module refcount, |
| 698 | * then CPU1 drops that refcount, then the refcount for CPU1 is |
| 699 | * read. We would record a decrement but not its corresponding |
| 700 | * increment so we would see a low count (disaster). |
| 701 | * |
| 702 | * Rare situation? But module_refcount can be preempted, and we |
| 703 | * might be tallying up 4096+ CPUs. So it is not impossible. |
| 704 | */ |
| 705 | smp_rmb(); |
| 706 | for_each_possible_cpu(cpu) |
| 707 | incs += per_cpu_ptr(mod->refptr, cpu)->incs; |
| 708 | return incs - decs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | } |
| 710 | EXPORT_SYMBOL(module_refcount); |
| 711 | |
| 712 | /* This exists whether we can unload or not */ |
| 713 | static void free_module(struct module *mod); |
| 714 | |
| 715 | static void wait_for_zero_refcount(struct module *mod) |
| 716 | { |
Matthew Wilcox | a655020 | 2008-02-26 10:47:18 -0500 | [diff] [blame] | 717 | /* Since we might sleep for some time, release the mutex first */ |
Ashutosh Naik | 6389a38 | 2006-03-23 03:00:46 -0800 | [diff] [blame] | 718 | mutex_unlock(&module_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | for (;;) { |
| 720 | DEBUGP("Looking at refcount...\n"); |
| 721 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 722 | if (module_refcount(mod) == 0) |
| 723 | break; |
| 724 | schedule(); |
| 725 | } |
| 726 | current->state = TASK_RUNNING; |
Ashutosh Naik | 6389a38 | 2006-03-23 03:00:46 -0800 | [diff] [blame] | 727 | mutex_lock(&module_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | } |
| 729 | |
Heiko Carstens | 17da2bd | 2009-01-14 14:14:10 +0100 | [diff] [blame] | 730 | SYSCALL_DEFINE2(delete_module, const char __user *, name_user, |
| 731 | unsigned int, flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | { |
| 733 | struct module *mod; |
Greg Kroah-Hartman | dfff0a0 | 2007-02-23 14:54:57 -0800 | [diff] [blame] | 734 | char name[MODULE_NAME_LEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | int ret, forced = 0; |
| 736 | |
Kees Cook | 3d43321 | 2009-04-02 15:49:29 -0700 | [diff] [blame] | 737 | if (!capable(CAP_SYS_MODULE) || modules_disabled) |
Greg Kroah-Hartman | dfff0a0 | 2007-02-23 14:54:57 -0800 | [diff] [blame] | 738 | return -EPERM; |
| 739 | |
| 740 | if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0) |
| 741 | return -EFAULT; |
| 742 | name[MODULE_NAME_LEN-1] = '\0'; |
| 743 | |
Tejun Heo | 3fc1f1e | 2010-05-06 18:49:20 +0200 | [diff] [blame] | 744 | if (mutex_lock_interruptible(&module_mutex) != 0) |
| 745 | return -EINTR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | |
| 747 | mod = find_module(name); |
| 748 | if (!mod) { |
| 749 | ret = -ENOENT; |
| 750 | goto out; |
| 751 | } |
| 752 | |
Linus Torvalds | 2c02dfe | 2010-05-31 12:19:37 -0700 | [diff] [blame] | 753 | if (!list_empty(&mod->source_list)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | /* Other modules depend on us: get rid of them first. */ |
| 755 | ret = -EWOULDBLOCK; |
| 756 | goto out; |
| 757 | } |
| 758 | |
| 759 | /* Doing init or already dying? */ |
| 760 | if (mod->state != MODULE_STATE_LIVE) { |
| 761 | /* FIXME: if (force), slam module count and wake up |
| 762 | waiter --RR */ |
| 763 | DEBUGP("%s already dying\n", mod->name); |
| 764 | ret = -EBUSY; |
| 765 | goto out; |
| 766 | } |
| 767 | |
| 768 | /* If it has an init func, it must have an exit func to unload */ |
Rusty Russell | af49d92 | 2007-10-16 23:26:27 -0700 | [diff] [blame] | 769 | if (mod->init && !mod->exit) { |
Akinobu Mita | fb16979 | 2006-01-08 01:04:29 -0800 | [diff] [blame] | 770 | forced = try_force_unload(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | if (!forced) { |
| 772 | /* This module can't be removed */ |
| 773 | ret = -EBUSY; |
| 774 | goto out; |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | /* Set this up before setting mod->state */ |
| 779 | mod->waiter = current; |
| 780 | |
| 781 | /* Stop the machine so refcounts can't move and disable module. */ |
| 782 | ret = try_stop_module(mod, flags, &forced); |
| 783 | if (ret != 0) |
| 784 | goto out; |
| 785 | |
| 786 | /* Never wait if forced. */ |
| 787 | if (!forced && module_refcount(mod) != 0) |
| 788 | wait_for_zero_refcount(mod); |
| 789 | |
Peter Oberparleiter | df4b565 | 2008-04-21 14:34:31 +0200 | [diff] [blame] | 790 | mutex_unlock(&module_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | /* Final destruction now noone is using it. */ |
Peter Oberparleiter | df4b565 | 2008-04-21 14:34:31 +0200 | [diff] [blame] | 792 | if (mod->exit != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | mod->exit(); |
Peter Oberparleiter | df4b565 | 2008-04-21 14:34:31 +0200 | [diff] [blame] | 794 | blocking_notifier_call_chain(&module_notify_list, |
| 795 | MODULE_STATE_GOING, mod); |
Arjan van de Ven | 22a9d64 | 2009-01-07 08:45:46 -0800 | [diff] [blame] | 796 | async_synchronize_full(); |
Rusty Russell | 7567650 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 797 | |
Arjan van de Ven | e14af7e | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 798 | /* Store the name of the last unloaded module for diagnostic purposes */ |
Rusty Russell | efa5345 | 2008-01-29 17:13:20 -0500 | [diff] [blame] | 799 | strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | |
Rusty Russell | 7567650 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 801 | free_module(mod); |
| 802 | return 0; |
| 803 | out: |
Ashutosh Naik | 6389a38 | 2006-03-23 03:00:46 -0800 | [diff] [blame] | 804 | mutex_unlock(&module_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | return ret; |
| 806 | } |
| 807 | |
Jianjun Kong | d1e99d7 | 2008-12-08 14:26:29 +0800 | [diff] [blame] | 808 | static inline void print_unload_info(struct seq_file *m, struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | { |
| 810 | struct module_use *use; |
| 811 | int printed_something = 0; |
| 812 | |
| 813 | seq_printf(m, " %u ", module_refcount(mod)); |
| 814 | |
| 815 | /* Always include a trailing , so userspace can differentiate |
| 816 | between this and the old multi-field proc format. */ |
Linus Torvalds | 2c02dfe | 2010-05-31 12:19:37 -0700 | [diff] [blame] | 817 | list_for_each_entry(use, &mod->source_list, source_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | printed_something = 1; |
Linus Torvalds | 2c02dfe | 2010-05-31 12:19:37 -0700 | [diff] [blame] | 819 | seq_printf(m, "%s,", use->source->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | } |
| 821 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | if (mod->init != NULL && mod->exit == NULL) { |
| 823 | printed_something = 1; |
| 824 | seq_printf(m, "[permanent],"); |
| 825 | } |
| 826 | |
| 827 | if (!printed_something) |
| 828 | seq_printf(m, "-"); |
| 829 | } |
| 830 | |
| 831 | void __symbol_put(const char *symbol) |
| 832 | { |
| 833 | struct module *owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | |
Rusty Russell | 24da1cb | 2007-07-15 23:41:46 -0700 | [diff] [blame] | 835 | preempt_disable(); |
Tim Abbott | 414fd31 | 2008-12-05 19:03:56 -0500 | [diff] [blame] | 836 | if (!find_symbol(symbol, &owner, NULL, true, false)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | BUG(); |
| 838 | module_put(owner); |
Rusty Russell | 24da1cb | 2007-07-15 23:41:46 -0700 | [diff] [blame] | 839 | preempt_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | } |
| 841 | EXPORT_SYMBOL(__symbol_put); |
| 842 | |
Rusty Russell | 7d1d16e | 2009-08-26 22:02:54 +0930 | [diff] [blame] | 843 | /* Note this assumes addr is a function, which it currently always is. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | void symbol_put_addr(void *addr) |
| 845 | { |
Trent Piepho | 5e37661 | 2006-05-15 09:44:06 -0700 | [diff] [blame] | 846 | struct module *modaddr; |
Rusty Russell | 7d1d16e | 2009-08-26 22:02:54 +0930 | [diff] [blame] | 847 | unsigned long a = (unsigned long)dereference_function_descriptor(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | |
Rusty Russell | 7d1d16e | 2009-08-26 22:02:54 +0930 | [diff] [blame] | 849 | if (core_kernel_text(a)) |
Trent Piepho | 5e37661 | 2006-05-15 09:44:06 -0700 | [diff] [blame] | 850 | return; |
| 851 | |
Rusty Russell | a6e6abd | 2009-03-31 13:05:31 -0600 | [diff] [blame] | 852 | /* module_text_address is safe here: we're supposed to have reference |
| 853 | * to module from symbol_get, so it can't go away. */ |
Rusty Russell | 7d1d16e | 2009-08-26 22:02:54 +0930 | [diff] [blame] | 854 | modaddr = __module_text_address(a); |
Rusty Russell | a6e6abd | 2009-03-31 13:05:31 -0600 | [diff] [blame] | 855 | BUG_ON(!modaddr); |
Trent Piepho | 5e37661 | 2006-05-15 09:44:06 -0700 | [diff] [blame] | 856 | module_put(modaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | } |
| 858 | EXPORT_SYMBOL_GPL(symbol_put_addr); |
| 859 | |
| 860 | static ssize_t show_refcnt(struct module_attribute *mattr, |
| 861 | struct module *mod, char *buffer) |
| 862 | { |
Alexey Dobriyan | 256e2fd | 2007-08-06 23:47:45 +0400 | [diff] [blame] | 863 | return sprintf(buffer, "%u\n", module_refcount(mod)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | static struct module_attribute refcnt = { |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 867 | .attr = { .name = "refcnt", .mode = 0444 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | .show = show_refcnt, |
| 869 | }; |
| 870 | |
Al Viro | f6a5703 | 2006-10-18 01:47:25 -0400 | [diff] [blame] | 871 | void module_put(struct module *module) |
| 872 | { |
| 873 | if (module) { |
Christoph Lameter | e1783a2 | 2010-01-05 15:34:50 +0900 | [diff] [blame] | 874 | preempt_disable(); |
Nick Piggin | 5fbfb18 | 2010-04-01 19:09:40 +1100 | [diff] [blame] | 875 | smp_wmb(); /* see comment in module_refcount */ |
| 876 | __this_cpu_inc(module->refptr->decs); |
Christoph Lameter | e1783a2 | 2010-01-05 15:34:50 +0900 | [diff] [blame] | 877 | |
Li Zefan | ae832d1 | 2010-03-24 10:57:43 +0800 | [diff] [blame] | 878 | trace_module_put(module, _RET_IP_); |
Al Viro | f6a5703 | 2006-10-18 01:47:25 -0400 | [diff] [blame] | 879 | /* Maybe they're waiting for us to drop reference? */ |
| 880 | if (unlikely(!module_is_live(module))) |
| 881 | wake_up_process(module->waiter); |
Christoph Lameter | e1783a2 | 2010-01-05 15:34:50 +0900 | [diff] [blame] | 882 | preempt_enable(); |
Al Viro | f6a5703 | 2006-10-18 01:47:25 -0400 | [diff] [blame] | 883 | } |
| 884 | } |
| 885 | EXPORT_SYMBOL(module_put); |
| 886 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | #else /* !CONFIG_MODULE_UNLOAD */ |
Jianjun Kong | d1e99d7 | 2008-12-08 14:26:29 +0800 | [diff] [blame] | 888 | static inline void print_unload_info(struct seq_file *m, struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | { |
| 890 | /* We don't know the usage count, or what modules are using. */ |
| 891 | seq_printf(m, " - -"); |
| 892 | } |
| 893 | |
| 894 | static inline void module_unload_free(struct module *mod) |
| 895 | { |
| 896 | } |
| 897 | |
Rusty Russell | 9bea7f2 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 898 | int ref_module(struct module *a, struct module *b) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | { |
Rusty Russell | 9bea7f2 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 900 | return strong_try_module_get(b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | } |
Rusty Russell | 9bea7f2 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 902 | EXPORT_SYMBOL_GPL(ref_module); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | |
Rusty Russell | 9f85a4b | 2010-08-05 12:59:04 -0600 | [diff] [blame] | 904 | static inline int module_unload_init(struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | { |
Rusty Russell | 9f85a4b | 2010-08-05 12:59:04 -0600 | [diff] [blame] | 906 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | } |
| 908 | #endif /* CONFIG_MODULE_UNLOAD */ |
| 909 | |
Kay Sievers | 1f71740 | 2006-11-24 12:15:25 +0100 | [diff] [blame] | 910 | static ssize_t show_initstate(struct module_attribute *mattr, |
| 911 | struct module *mod, char *buffer) |
| 912 | { |
| 913 | const char *state = "unknown"; |
| 914 | |
| 915 | switch (mod->state) { |
| 916 | case MODULE_STATE_LIVE: |
| 917 | state = "live"; |
| 918 | break; |
| 919 | case MODULE_STATE_COMING: |
| 920 | state = "coming"; |
| 921 | break; |
| 922 | case MODULE_STATE_GOING: |
| 923 | state = "going"; |
| 924 | break; |
| 925 | } |
| 926 | return sprintf(buffer, "%s\n", state); |
| 927 | } |
| 928 | |
| 929 | static struct module_attribute initstate = { |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 930 | .attr = { .name = "initstate", .mode = 0444 }, |
Kay Sievers | 1f71740 | 2006-11-24 12:15:25 +0100 | [diff] [blame] | 931 | .show = show_initstate, |
| 932 | }; |
| 933 | |
Greg Kroah-Hartman | 03e88ae1 | 2006-02-16 13:50:23 -0800 | [diff] [blame] | 934 | static struct module_attribute *modinfo_attrs[] = { |
| 935 | &modinfo_version, |
| 936 | &modinfo_srcversion, |
Kay Sievers | 1f71740 | 2006-11-24 12:15:25 +0100 | [diff] [blame] | 937 | &initstate, |
Greg Kroah-Hartman | 03e88ae1 | 2006-02-16 13:50:23 -0800 | [diff] [blame] | 938 | #ifdef CONFIG_MODULE_UNLOAD |
| 939 | &refcnt, |
| 940 | #endif |
| 941 | NULL, |
| 942 | }; |
| 943 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | static const char vermagic[] = VERMAGIC_STRING; |
| 945 | |
Rusty Russell | c6e665c | 2009-03-31 13:05:33 -0600 | [diff] [blame] | 946 | static int try_to_force_load(struct module *mod, const char *reason) |
Linus Torvalds | 826e450 | 2008-05-04 17:04:16 -0700 | [diff] [blame] | 947 | { |
| 948 | #ifdef CONFIG_MODULE_FORCE_LOAD |
Andi Kleen | 25ddbb1 | 2008-10-15 22:01:41 -0700 | [diff] [blame] | 949 | if (!test_taint(TAINT_FORCED_MODULE)) |
Rusty Russell | c6e665c | 2009-03-31 13:05:33 -0600 | [diff] [blame] | 950 | printk(KERN_WARNING "%s: %s: kernel tainted.\n", |
| 951 | mod->name, reason); |
Linus Torvalds | 826e450 | 2008-05-04 17:04:16 -0700 | [diff] [blame] | 952 | add_taint_module(mod, TAINT_FORCED_MODULE); |
| 953 | return 0; |
| 954 | #else |
| 955 | return -ENOEXEC; |
| 956 | #endif |
| 957 | } |
| 958 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | #ifdef CONFIG_MODVERSIONS |
Rusty Russell | d4703ae | 2009-12-15 16:28:32 -0600 | [diff] [blame] | 960 | /* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */ |
| 961 | static unsigned long maybe_relocated(unsigned long crc, |
| 962 | const struct module *crc_owner) |
| 963 | { |
| 964 | #ifdef ARCH_RELOCATES_KCRCTAB |
| 965 | if (crc_owner == NULL) |
| 966 | return crc - (unsigned long)reloc_start; |
| 967 | #endif |
| 968 | return crc; |
| 969 | } |
| 970 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | static int check_version(Elf_Shdr *sechdrs, |
| 972 | unsigned int versindex, |
| 973 | const char *symname, |
| 974 | struct module *mod, |
Rusty Russell | d4703ae | 2009-12-15 16:28:32 -0600 | [diff] [blame] | 975 | const unsigned long *crc, |
| 976 | const struct module *crc_owner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | { |
| 978 | unsigned int i, num_versions; |
| 979 | struct modversion_info *versions; |
| 980 | |
| 981 | /* Exporting module didn't supply crcs? OK, we're already tainted. */ |
| 982 | if (!crc) |
| 983 | return 1; |
| 984 | |
Rusty Russell | a5dd697 | 2008-05-09 16:24:21 +1000 | [diff] [blame] | 985 | /* No versions at all? modprobe --force does this. */ |
| 986 | if (versindex == 0) |
| 987 | return try_to_force_load(mod, symname) == 0; |
| 988 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | versions = (void *) sechdrs[versindex].sh_addr; |
| 990 | num_versions = sechdrs[versindex].sh_size |
| 991 | / sizeof(struct modversion_info); |
| 992 | |
| 993 | for (i = 0; i < num_versions; i++) { |
| 994 | if (strcmp(versions[i].name, symname) != 0) |
| 995 | continue; |
| 996 | |
Rusty Russell | d4703ae | 2009-12-15 16:28:32 -0600 | [diff] [blame] | 997 | if (versions[i].crc == maybe_relocated(*crc, crc_owner)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | DEBUGP("Found checksum %lX vs module %lX\n", |
Rusty Russell | d4703ae | 2009-12-15 16:28:32 -0600 | [diff] [blame] | 1000 | maybe_relocated(*crc, crc_owner), versions[i].crc); |
Linus Torvalds | 826e450 | 2008-05-04 17:04:16 -0700 | [diff] [blame] | 1001 | goto bad_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | } |
Linus Torvalds | 826e450 | 2008-05-04 17:04:16 -0700 | [diff] [blame] | 1003 | |
Rusty Russell | a5dd697 | 2008-05-09 16:24:21 +1000 | [diff] [blame] | 1004 | printk(KERN_WARNING "%s: no symbol version for %s\n", |
| 1005 | mod->name, symname); |
| 1006 | return 0; |
Linus Torvalds | 826e450 | 2008-05-04 17:04:16 -0700 | [diff] [blame] | 1007 | |
| 1008 | bad_version: |
| 1009 | printk("%s: disagrees about version of symbol %s\n", |
| 1010 | mod->name, symname); |
| 1011 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | static inline int check_modstruct_version(Elf_Shdr *sechdrs, |
| 1015 | unsigned int versindex, |
| 1016 | struct module *mod) |
| 1017 | { |
| 1018 | const unsigned long *crc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | |
Rusty Russell | 7567650 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 1020 | /* Since this should be found in kernel (which can't be removed), |
| 1021 | * no locking is necessary. */ |
Mike Frysinger | 6560dc1 | 2009-07-23 23:42:08 +0930 | [diff] [blame] | 1022 | if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL, |
| 1023 | &crc, true, false)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | BUG(); |
Rusty Russell | d4703ae | 2009-12-15 16:28:32 -0600 | [diff] [blame] | 1025 | return check_version(sechdrs, versindex, "module_layout", mod, crc, |
| 1026 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
Rusty Russell | 91e37a7 | 2008-05-09 16:25:28 +1000 | [diff] [blame] | 1029 | /* First part is kernel version, which we ignore if module has crcs. */ |
| 1030 | static inline int same_magic(const char *amagic, const char *bmagic, |
| 1031 | bool has_crcs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | { |
Rusty Russell | 91e37a7 | 2008-05-09 16:25:28 +1000 | [diff] [blame] | 1033 | if (has_crcs) { |
| 1034 | amagic += strcspn(amagic, " "); |
| 1035 | bmagic += strcspn(bmagic, " "); |
| 1036 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | return strcmp(amagic, bmagic) == 0; |
| 1038 | } |
| 1039 | #else |
| 1040 | static inline int check_version(Elf_Shdr *sechdrs, |
| 1041 | unsigned int versindex, |
| 1042 | const char *symname, |
| 1043 | struct module *mod, |
Rusty Russell | d4703ae | 2009-12-15 16:28:32 -0600 | [diff] [blame] | 1044 | const unsigned long *crc, |
| 1045 | const struct module *crc_owner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | { |
| 1047 | return 1; |
| 1048 | } |
| 1049 | |
| 1050 | static inline int check_modstruct_version(Elf_Shdr *sechdrs, |
| 1051 | unsigned int versindex, |
| 1052 | struct module *mod) |
| 1053 | { |
| 1054 | return 1; |
| 1055 | } |
| 1056 | |
Rusty Russell | 91e37a7 | 2008-05-09 16:25:28 +1000 | [diff] [blame] | 1057 | static inline int same_magic(const char *amagic, const char *bmagic, |
| 1058 | bool has_crcs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | { |
| 1060 | return strcmp(amagic, bmagic) == 0; |
| 1061 | } |
| 1062 | #endif /* CONFIG_MODVERSIONS */ |
| 1063 | |
Rusty Russell | 7567650 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 1064 | /* Resolve a symbol for this module. I.e. if we find one, record usage. */ |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1065 | static const struct kernel_symbol *resolve_symbol(struct module *mod, |
| 1066 | const struct load_info *info, |
Tim Abbott | 414fd31 | 2008-12-05 19:03:56 -0500 | [diff] [blame] | 1067 | const char *name, |
Rusty Russell | 9bea7f2 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 1068 | char ownername[]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | { |
| 1070 | struct module *owner; |
Tim Abbott | 414fd31 | 2008-12-05 19:03:56 -0500 | [diff] [blame] | 1071 | const struct kernel_symbol *sym; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | const unsigned long *crc; |
Rusty Russell | 9bea7f2 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 1073 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | |
Rusty Russell | 7567650 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 1075 | mutex_lock(&module_mutex); |
Tim Abbott | 414fd31 | 2008-12-05 19:03:56 -0500 | [diff] [blame] | 1076 | sym = find_symbol(name, &owner, &crc, |
Andi Kleen | 25ddbb1 | 2008-10-15 22:01:41 -0700 | [diff] [blame] | 1077 | !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true); |
Rusty Russell | 9bea7f2 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 1078 | if (!sym) |
| 1079 | goto unlock; |
| 1080 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1081 | if (!check_version(info->sechdrs, info->index.vers, name, mod, crc, |
| 1082 | owner)) { |
Rusty Russell | 9bea7f2 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 1083 | sym = ERR_PTR(-EINVAL); |
| 1084 | goto getname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | } |
Rusty Russell | 9bea7f2 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 1086 | |
| 1087 | err = ref_module(mod, owner); |
| 1088 | if (err) { |
| 1089 | sym = ERR_PTR(err); |
| 1090 | goto getname; |
| 1091 | } |
| 1092 | |
| 1093 | getname: |
| 1094 | /* We must make copy under the lock if we failed to get ref. */ |
| 1095 | strncpy(ownername, module_name(owner), MODULE_NAME_LEN); |
| 1096 | unlock: |
Rusty Russell | 7567650 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 1097 | mutex_unlock(&module_mutex); |
Linus Torvalds | 218ce73 | 2010-05-25 16:48:30 -0700 | [diff] [blame] | 1098 | return sym; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1101 | static const struct kernel_symbol * |
| 1102 | resolve_symbol_wait(struct module *mod, |
| 1103 | const struct load_info *info, |
| 1104 | const char *name) |
Rusty Russell | 9bea7f2 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 1105 | { |
| 1106 | const struct kernel_symbol *ksym; |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1107 | char owner[MODULE_NAME_LEN]; |
Rusty Russell | 9bea7f2 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 1108 | |
| 1109 | if (wait_event_interruptible_timeout(module_wq, |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1110 | !IS_ERR(ksym = resolve_symbol(mod, info, name, owner)) |
| 1111 | || PTR_ERR(ksym) != -EBUSY, |
Rusty Russell | 9bea7f2 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 1112 | 30 * HZ) <= 0) { |
| 1113 | printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n", |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1114 | mod->name, owner); |
Rusty Russell | 9bea7f2 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 1115 | } |
| 1116 | return ksym; |
| 1117 | } |
| 1118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | /* |
| 1120 | * /sys/module/foo/sections stuff |
| 1121 | * J. Corbet <corbet@lwn.net> |
| 1122 | */ |
Rusty Russell | 8f6d037 | 2010-08-05 12:59:09 -0600 | [diff] [blame] | 1123 | #ifdef CONFIG_SYSFS |
Ben Hutchings | 10b465a | 2009-12-19 14:43:01 +0000 | [diff] [blame] | 1124 | |
Rusty Russell | 8f6d037 | 2010-08-05 12:59:09 -0600 | [diff] [blame] | 1125 | #ifdef CONFIG_KALLSYMS |
Ben Hutchings | 10b465a | 2009-12-19 14:43:01 +0000 | [diff] [blame] | 1126 | static inline bool sect_empty(const Elf_Shdr *sect) |
| 1127 | { |
| 1128 | return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0; |
| 1129 | } |
| 1130 | |
Rusty Russell | a58730c | 2008-03-13 09:03:44 +0000 | [diff] [blame] | 1131 | struct module_sect_attr |
| 1132 | { |
| 1133 | struct module_attribute mattr; |
| 1134 | char *name; |
| 1135 | unsigned long address; |
| 1136 | }; |
| 1137 | |
| 1138 | struct module_sect_attrs |
| 1139 | { |
| 1140 | struct attribute_group grp; |
| 1141 | unsigned int nsections; |
| 1142 | struct module_sect_attr attrs[0]; |
| 1143 | }; |
| 1144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | static ssize_t module_sect_show(struct module_attribute *mattr, |
| 1146 | struct module *mod, char *buf) |
| 1147 | { |
| 1148 | struct module_sect_attr *sattr = |
| 1149 | container_of(mattr, struct module_sect_attr, mattr); |
| 1150 | return sprintf(buf, "0x%lx\n", sattr->address); |
| 1151 | } |
| 1152 | |
Ian S. Nelson | 04b1db9 | 2006-09-29 02:01:31 -0700 | [diff] [blame] | 1153 | static void free_sect_attrs(struct module_sect_attrs *sect_attrs) |
| 1154 | { |
Rusty Russell | a58730c | 2008-03-13 09:03:44 +0000 | [diff] [blame] | 1155 | unsigned int section; |
Ian S. Nelson | 04b1db9 | 2006-09-29 02:01:31 -0700 | [diff] [blame] | 1156 | |
| 1157 | for (section = 0; section < sect_attrs->nsections; section++) |
| 1158 | kfree(sect_attrs->attrs[section].name); |
| 1159 | kfree(sect_attrs); |
| 1160 | } |
| 1161 | |
Rusty Russell | 8f6d037 | 2010-08-05 12:59:09 -0600 | [diff] [blame] | 1162 | static void add_sect_attrs(struct module *mod, const struct load_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | { |
| 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; |
Daniel Walker | 22a8bde | 2007-10-18 03:06:07 -0700 | [diff] [blame] | 1168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | /* Count loaded sections and allocate structures */ |
Rusty Russell | 8f6d037 | 2010-08-05 12:59:09 -0600 | [diff] [blame] | 1170 | for (i = 0; i < info->hdr->e_shnum; i++) |
| 1171 | if (!sect_empty(&info->sechdrs[i])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | nloaded++; |
| 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]); |
Ian S. Nelson | 04b1db9 | 2006-09-29 02:01:31 -0700 | [diff] [blame] | 1177 | sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL); |
| 1178 | if (sect_attrs == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | return; |
| 1180 | |
| 1181 | /* Setup section attributes. */ |
| 1182 | sect_attrs->grp.name = "sections"; |
| 1183 | sect_attrs->grp.attrs = (void *)sect_attrs + size[0]; |
| 1184 | |
Ian S. Nelson | 04b1db9 | 2006-09-29 02:01:31 -0700 | [diff] [blame] | 1185 | sect_attrs->nsections = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | sattr = §_attrs->attrs[0]; |
| 1187 | gattr = §_attrs->grp.attrs[0]; |
Rusty Russell | 8f6d037 | 2010-08-05 12:59:09 -0600 | [diff] [blame] | 1188 | for (i = 0; i < info->hdr->e_shnum; i++) { |
| 1189 | Elf_Shdr *sec = &info->sechdrs[i]; |
| 1190 | if (sect_empty(sec)) |
Helge Deller | 35dead4 | 2009-12-03 00:29:15 +0100 | [diff] [blame] | 1191 | continue; |
Rusty Russell | 8f6d037 | 2010-08-05 12:59:09 -0600 | [diff] [blame] | 1192 | sattr->address = sec->sh_addr; |
| 1193 | sattr->name = kstrdup(info->secstrings + sec->sh_name, |
Ian S. Nelson | 04b1db9 | 2006-09-29 02:01:31 -0700 | [diff] [blame] | 1194 | GFP_KERNEL); |
| 1195 | if (sattr->name == NULL) |
| 1196 | goto out; |
| 1197 | sect_attrs->nsections++; |
Eric W. Biederman | 361795b | 2010-02-12 13:41:56 -0800 | [diff] [blame] | 1198 | sysfs_attr_init(&sattr->mattr.attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | sattr->mattr.show = module_sect_show; |
| 1200 | sattr->mattr.store = NULL; |
| 1201 | sattr->mattr.attr.name = sattr->name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | sattr->mattr.attr.mode = S_IRUGO; |
| 1203 | *(gattr++) = &(sattr++)->mattr.attr; |
| 1204 | } |
| 1205 | *gattr = NULL; |
| 1206 | |
| 1207 | if (sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp)) |
| 1208 | goto out; |
| 1209 | |
| 1210 | mod->sect_attrs = sect_attrs; |
| 1211 | return; |
| 1212 | out: |
Ian S. Nelson | 04b1db9 | 2006-09-29 02:01:31 -0700 | [diff] [blame] | 1213 | free_sect_attrs(sect_attrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | static void remove_sect_attrs(struct module *mod) |
| 1217 | { |
| 1218 | if (mod->sect_attrs) { |
| 1219 | sysfs_remove_group(&mod->mkobj.kobj, |
| 1220 | &mod->sect_attrs->grp); |
| 1221 | /* We are positive that no one is using any sect attrs |
| 1222 | * at this point. Deallocate immediately. */ |
Ian S. Nelson | 04b1db9 | 2006-09-29 02:01:31 -0700 | [diff] [blame] | 1223 | free_sect_attrs(mod->sect_attrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | mod->sect_attrs = NULL; |
| 1225 | } |
| 1226 | } |
| 1227 | |
Roland McGrath | 6d76013 | 2007-10-16 23:26:40 -0700 | [diff] [blame] | 1228 | /* |
| 1229 | * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections. |
| 1230 | */ |
| 1231 | |
| 1232 | struct module_notes_attrs { |
| 1233 | struct kobject *dir; |
| 1234 | unsigned int notes; |
| 1235 | struct bin_attribute attrs[0]; |
| 1236 | }; |
| 1237 | |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 1238 | static ssize_t module_notes_read(struct file *filp, struct kobject *kobj, |
Roland McGrath | 6d76013 | 2007-10-16 23:26:40 -0700 | [diff] [blame] | 1239 | struct bin_attribute *bin_attr, |
| 1240 | char *buf, loff_t pos, size_t count) |
| 1241 | { |
| 1242 | /* |
| 1243 | * The caller checked the pos and count against our size. |
| 1244 | */ |
| 1245 | memcpy(buf, bin_attr->private + pos, count); |
| 1246 | return count; |
| 1247 | } |
| 1248 | |
| 1249 | static void free_notes_attrs(struct module_notes_attrs *notes_attrs, |
| 1250 | unsigned int i) |
| 1251 | { |
| 1252 | if (notes_attrs->dir) { |
| 1253 | while (i-- > 0) |
| 1254 | sysfs_remove_bin_file(notes_attrs->dir, |
| 1255 | ¬es_attrs->attrs[i]); |
Alexey Dobriyan | e943209 | 2008-09-23 23:51:11 +0400 | [diff] [blame] | 1256 | kobject_put(notes_attrs->dir); |
Roland McGrath | 6d76013 | 2007-10-16 23:26:40 -0700 | [diff] [blame] | 1257 | } |
| 1258 | kfree(notes_attrs); |
| 1259 | } |
| 1260 | |
Rusty Russell | 8f6d037 | 2010-08-05 12:59:09 -0600 | [diff] [blame] | 1261 | static void add_notes_attrs(struct module *mod, const struct load_info *info) |
Roland McGrath | 6d76013 | 2007-10-16 23:26:40 -0700 | [diff] [blame] | 1262 | { |
| 1263 | unsigned int notes, loaded, i; |
| 1264 | struct module_notes_attrs *notes_attrs; |
| 1265 | struct bin_attribute *nattr; |
| 1266 | |
Ingo Molnar | ea6bff3 | 2009-08-28 10:44:56 +0200 | [diff] [blame] | 1267 | /* failed to create section attributes, so can't create notes */ |
| 1268 | if (!mod->sect_attrs) |
| 1269 | return; |
| 1270 | |
Roland McGrath | 6d76013 | 2007-10-16 23:26:40 -0700 | [diff] [blame] | 1271 | /* Count notes sections and allocate structures. */ |
| 1272 | notes = 0; |
Rusty Russell | 8f6d037 | 2010-08-05 12:59:09 -0600 | [diff] [blame] | 1273 | for (i = 0; i < info->hdr->e_shnum; i++) |
| 1274 | if (!sect_empty(&info->sechdrs[i]) && |
| 1275 | (info->sechdrs[i].sh_type == SHT_NOTE)) |
Roland McGrath | 6d76013 | 2007-10-16 23:26:40 -0700 | [diff] [blame] | 1276 | ++notes; |
| 1277 | |
| 1278 | if (notes == 0) |
| 1279 | return; |
| 1280 | |
| 1281 | notes_attrs = kzalloc(sizeof(*notes_attrs) |
| 1282 | + notes * sizeof(notes_attrs->attrs[0]), |
| 1283 | GFP_KERNEL); |
| 1284 | if (notes_attrs == NULL) |
| 1285 | return; |
| 1286 | |
| 1287 | notes_attrs->notes = notes; |
| 1288 | nattr = ¬es_attrs->attrs[0]; |
Rusty Russell | 8f6d037 | 2010-08-05 12:59:09 -0600 | [diff] [blame] | 1289 | for (loaded = i = 0; i < info->hdr->e_shnum; ++i) { |
| 1290 | if (sect_empty(&info->sechdrs[i])) |
Roland McGrath | 6d76013 | 2007-10-16 23:26:40 -0700 | [diff] [blame] | 1291 | continue; |
Rusty Russell | 8f6d037 | 2010-08-05 12:59:09 -0600 | [diff] [blame] | 1292 | if (info->sechdrs[i].sh_type == SHT_NOTE) { |
Eric W. Biederman | 361795b | 2010-02-12 13:41:56 -0800 | [diff] [blame] | 1293 | sysfs_bin_attr_init(nattr); |
Roland McGrath | 6d76013 | 2007-10-16 23:26:40 -0700 | [diff] [blame] | 1294 | nattr->attr.name = mod->sect_attrs->attrs[loaded].name; |
| 1295 | nattr->attr.mode = S_IRUGO; |
Rusty Russell | 8f6d037 | 2010-08-05 12:59:09 -0600 | [diff] [blame] | 1296 | nattr->size = info->sechdrs[i].sh_size; |
| 1297 | nattr->private = (void *) info->sechdrs[i].sh_addr; |
Roland McGrath | 6d76013 | 2007-10-16 23:26:40 -0700 | [diff] [blame] | 1298 | nattr->read = module_notes_read; |
| 1299 | ++nattr; |
| 1300 | } |
| 1301 | ++loaded; |
| 1302 | } |
| 1303 | |
Greg Kroah-Hartman | 4ff6abf | 2007-11-05 22:24:43 -0800 | [diff] [blame] | 1304 | notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj); |
Roland McGrath | 6d76013 | 2007-10-16 23:26:40 -0700 | [diff] [blame] | 1305 | if (!notes_attrs->dir) |
| 1306 | goto out; |
| 1307 | |
| 1308 | for (i = 0; i < notes; ++i) |
| 1309 | if (sysfs_create_bin_file(notes_attrs->dir, |
| 1310 | ¬es_attrs->attrs[i])) |
| 1311 | goto out; |
| 1312 | |
| 1313 | mod->notes_attrs = notes_attrs; |
| 1314 | return; |
| 1315 | |
| 1316 | out: |
| 1317 | free_notes_attrs(notes_attrs, i); |
| 1318 | } |
| 1319 | |
| 1320 | static void remove_notes_attrs(struct module *mod) |
| 1321 | { |
| 1322 | if (mod->notes_attrs) |
| 1323 | free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes); |
| 1324 | } |
| 1325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | #else |
Ian S. Nelson | 04b1db9 | 2006-09-29 02:01:31 -0700 | [diff] [blame] | 1327 | |
Rusty Russell | 8f6d037 | 2010-08-05 12:59:09 -0600 | [diff] [blame] | 1328 | static inline void add_sect_attrs(struct module *mod, |
| 1329 | const struct load_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | { |
| 1331 | } |
| 1332 | |
| 1333 | static inline void remove_sect_attrs(struct module *mod) |
| 1334 | { |
| 1335 | } |
Roland McGrath | 6d76013 | 2007-10-16 23:26:40 -0700 | [diff] [blame] | 1336 | |
Rusty Russell | 8f6d037 | 2010-08-05 12:59:09 -0600 | [diff] [blame] | 1337 | static inline void add_notes_attrs(struct module *mod, |
| 1338 | const struct load_info *info) |
Roland McGrath | 6d76013 | 2007-10-16 23:26:40 -0700 | [diff] [blame] | 1339 | { |
| 1340 | } |
| 1341 | |
| 1342 | static inline void remove_notes_attrs(struct module *mod) |
| 1343 | { |
| 1344 | } |
Rusty Russell | 8f6d037 | 2010-08-05 12:59:09 -0600 | [diff] [blame] | 1345 | #endif /* CONFIG_KALLSYMS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | |
Rusty Russell | 80a3d1b | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 1347 | static void add_usage_links(struct module *mod) |
| 1348 | { |
| 1349 | #ifdef CONFIG_MODULE_UNLOAD |
| 1350 | struct module_use *use; |
| 1351 | int nowarn; |
| 1352 | |
Rusty Russell | 7567650 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 1353 | mutex_lock(&module_mutex); |
Rusty Russell | 80a3d1b | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 1354 | list_for_each_entry(use, &mod->target_list, target_list) { |
| 1355 | nowarn = sysfs_create_link(use->target->holders_dir, |
| 1356 | &mod->mkobj.kobj, mod->name); |
| 1357 | } |
Rusty Russell | 7567650 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 1358 | mutex_unlock(&module_mutex); |
Rusty Russell | 80a3d1b | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 1359 | #endif |
| 1360 | } |
| 1361 | |
| 1362 | static void del_usage_links(struct module *mod) |
| 1363 | { |
| 1364 | #ifdef CONFIG_MODULE_UNLOAD |
| 1365 | struct module_use *use; |
| 1366 | |
Rusty Russell | 7567650 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 1367 | mutex_lock(&module_mutex); |
Rusty Russell | 80a3d1b | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 1368 | list_for_each_entry(use, &mod->target_list, target_list) |
| 1369 | sysfs_remove_link(use->target->holders_dir, mod->name); |
Rusty Russell | 7567650 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 1370 | mutex_unlock(&module_mutex); |
Rusty Russell | 80a3d1b | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 1371 | #endif |
| 1372 | } |
| 1373 | |
Rusty Russell | 6407ebb2 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 1374 | static int module_add_modinfo_attrs(struct module *mod) |
Matt Domsch | c988d2b | 2005-06-23 22:05:15 -0700 | [diff] [blame] | 1375 | { |
| 1376 | struct module_attribute *attr; |
Greg Kroah-Hartman | 03e88ae1 | 2006-02-16 13:50:23 -0800 | [diff] [blame] | 1377 | struct module_attribute *temp_attr; |
Matt Domsch | c988d2b | 2005-06-23 22:05:15 -0700 | [diff] [blame] | 1378 | int error = 0; |
| 1379 | int i; |
| 1380 | |
Greg Kroah-Hartman | 03e88ae1 | 2006-02-16 13:50:23 -0800 | [diff] [blame] | 1381 | mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) * |
| 1382 | (ARRAY_SIZE(modinfo_attrs) + 1)), |
| 1383 | GFP_KERNEL); |
| 1384 | if (!mod->modinfo_attrs) |
| 1385 | return -ENOMEM; |
| 1386 | |
| 1387 | temp_attr = mod->modinfo_attrs; |
Matt Domsch | c988d2b | 2005-06-23 22:05:15 -0700 | [diff] [blame] | 1388 | for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) { |
| 1389 | if (!attr->test || |
Greg Kroah-Hartman | 03e88ae1 | 2006-02-16 13:50:23 -0800 | [diff] [blame] | 1390 | (attr->test && attr->test(mod))) { |
| 1391 | memcpy(temp_attr, attr, sizeof(*temp_attr)); |
Eric W. Biederman | 361795b | 2010-02-12 13:41:56 -0800 | [diff] [blame] | 1392 | sysfs_attr_init(&temp_attr->attr); |
Greg Kroah-Hartman | 03e88ae1 | 2006-02-16 13:50:23 -0800 | [diff] [blame] | 1393 | error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr); |
| 1394 | ++temp_attr; |
| 1395 | } |
Matt Domsch | c988d2b | 2005-06-23 22:05:15 -0700 | [diff] [blame] | 1396 | } |
| 1397 | return error; |
| 1398 | } |
| 1399 | |
Rusty Russell | 6407ebb2 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 1400 | static void module_remove_modinfo_attrs(struct module *mod) |
Matt Domsch | c988d2b | 2005-06-23 22:05:15 -0700 | [diff] [blame] | 1401 | { |
| 1402 | struct module_attribute *attr; |
| 1403 | int i; |
| 1404 | |
Greg Kroah-Hartman | 03e88ae1 | 2006-02-16 13:50:23 -0800 | [diff] [blame] | 1405 | for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) { |
| 1406 | /* pick a field to test for end of list */ |
| 1407 | if (!attr->attr.name) |
| 1408 | break; |
Matt Domsch | c988d2b | 2005-06-23 22:05:15 -0700 | [diff] [blame] | 1409 | sysfs_remove_file(&mod->mkobj.kobj,&attr->attr); |
Greg Kroah-Hartman | 03e88ae1 | 2006-02-16 13:50:23 -0800 | [diff] [blame] | 1410 | if (attr->free) |
| 1411 | attr->free(mod); |
Matt Domsch | c988d2b | 2005-06-23 22:05:15 -0700 | [diff] [blame] | 1412 | } |
Greg Kroah-Hartman | 03e88ae1 | 2006-02-16 13:50:23 -0800 | [diff] [blame] | 1413 | kfree(mod->modinfo_attrs); |
Matt Domsch | c988d2b | 2005-06-23 22:05:15 -0700 | [diff] [blame] | 1414 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | |
Rusty Russell | 6407ebb2 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 1416 | static int mod_sysfs_init(struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | { |
| 1418 | int err; |
Greg Kroah-Hartman | 6494a93 | 2008-01-27 15:38:40 -0800 | [diff] [blame] | 1419 | struct kobject *kobj; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 1421 | if (!module_sysfs_initialized) { |
| 1422 | printk(KERN_ERR "%s: module sysfs not initialized\n", |
Ed Swierk | 1cc5f71 | 2006-09-25 16:25:36 -0700 | [diff] [blame] | 1423 | mod->name); |
| 1424 | err = -EINVAL; |
| 1425 | goto out; |
| 1426 | } |
Greg Kroah-Hartman | 6494a93 | 2008-01-27 15:38:40 -0800 | [diff] [blame] | 1427 | |
| 1428 | kobj = kset_find_obj(module_kset, mod->name); |
| 1429 | if (kobj) { |
| 1430 | printk(KERN_ERR "%s: module is already loaded\n", mod->name); |
| 1431 | kobject_put(kobj); |
| 1432 | err = -EINVAL; |
| 1433 | goto out; |
| 1434 | } |
| 1435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | mod->mkobj.mod = mod; |
Kay Sievers | e17e0f5 | 2006-11-24 12:15:25 +0100 | [diff] [blame] | 1437 | |
Greg Kroah-Hartman | ac3c814 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1438 | memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj)); |
| 1439 | mod->mkobj.kobj.kset = module_kset; |
| 1440 | err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL, |
| 1441 | "%s", mod->name); |
| 1442 | if (err) |
| 1443 | kobject_put(&mod->mkobj.kobj); |
Kay Sievers | 270a6c4 | 2007-01-18 13:26:15 +0100 | [diff] [blame] | 1444 | |
Kay Sievers | 97c146ef | 2007-11-29 23:46:11 +0100 | [diff] [blame] | 1445 | /* delay uevent until full sysfs population */ |
Kay Sievers | 270a6c4 | 2007-01-18 13:26:15 +0100 | [diff] [blame] | 1446 | out: |
| 1447 | return err; |
| 1448 | } |
| 1449 | |
Rusty Russell | 6407ebb2 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 1450 | static int mod_sysfs_setup(struct module *mod, |
Rusty Russell | 8f6d037 | 2010-08-05 12:59:09 -0600 | [diff] [blame] | 1451 | const struct load_info *info, |
Kay Sievers | 270a6c4 | 2007-01-18 13:26:15 +0100 | [diff] [blame] | 1452 | struct kernel_param *kparam, |
| 1453 | unsigned int num_params) |
| 1454 | { |
| 1455 | int err; |
| 1456 | |
Rusty Russell | 80a3d1b | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 1457 | err = mod_sysfs_init(mod); |
| 1458 | if (err) |
| 1459 | goto out; |
| 1460 | |
Greg Kroah-Hartman | 4ff6abf | 2007-11-05 22:24:43 -0800 | [diff] [blame] | 1461 | mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj); |
Akinobu Mita | 240936e | 2007-04-26 00:12:09 -0700 | [diff] [blame] | 1462 | if (!mod->holders_dir) { |
| 1463 | err = -ENOMEM; |
Kay Sievers | 270a6c4 | 2007-01-18 13:26:15 +0100 | [diff] [blame] | 1464 | goto out_unreg; |
Akinobu Mita | 240936e | 2007-04-26 00:12:09 -0700 | [diff] [blame] | 1465 | } |
Kay Sievers | 270a6c4 | 2007-01-18 13:26:15 +0100 | [diff] [blame] | 1466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | err = module_param_sysfs_setup(mod, kparam, num_params); |
| 1468 | if (err) |
Kay Sievers | 270a6c4 | 2007-01-18 13:26:15 +0100 | [diff] [blame] | 1469 | goto out_unreg_holders; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | |
Matt Domsch | c988d2b | 2005-06-23 22:05:15 -0700 | [diff] [blame] | 1471 | err = module_add_modinfo_attrs(mod); |
| 1472 | if (err) |
Kay Sievers | e17e0f5 | 2006-11-24 12:15:25 +0100 | [diff] [blame] | 1473 | goto out_unreg_param; |
Matt Domsch | c988d2b | 2005-06-23 22:05:15 -0700 | [diff] [blame] | 1474 | |
Rusty Russell | 80a3d1b | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 1475 | add_usage_links(mod); |
Rusty Russell | 8f6d037 | 2010-08-05 12:59:09 -0600 | [diff] [blame] | 1476 | add_sect_attrs(mod, info); |
| 1477 | add_notes_attrs(mod, info); |
Rusty Russell | 80a3d1b | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 1478 | |
Kay Sievers | e17e0f5 | 2006-11-24 12:15:25 +0100 | [diff] [blame] | 1479 | kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | return 0; |
| 1481 | |
Kay Sievers | e17e0f5 | 2006-11-24 12:15:25 +0100 | [diff] [blame] | 1482 | out_unreg_param: |
| 1483 | module_param_sysfs_remove(mod); |
Kay Sievers | 270a6c4 | 2007-01-18 13:26:15 +0100 | [diff] [blame] | 1484 | out_unreg_holders: |
Greg Kroah-Hartman | 78a2d90 | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 1485 | kobject_put(mod->holders_dir); |
Kay Sievers | 270a6c4 | 2007-01-18 13:26:15 +0100 | [diff] [blame] | 1486 | out_unreg: |
Kay Sievers | e17e0f5 | 2006-11-24 12:15:25 +0100 | [diff] [blame] | 1487 | kobject_put(&mod->mkobj.kobj); |
Rusty Russell | 80a3d1b | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 1488 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | return err; |
| 1490 | } |
Denis V. Lunev | 34e4e2f | 2008-05-20 13:59:48 +0400 | [diff] [blame] | 1491 | |
| 1492 | static void mod_sysfs_fini(struct module *mod) |
| 1493 | { |
Rusty Russell | 8f6d037 | 2010-08-05 12:59:09 -0600 | [diff] [blame] | 1494 | remove_notes_attrs(mod); |
| 1495 | remove_sect_attrs(mod); |
Denis V. Lunev | 34e4e2f | 2008-05-20 13:59:48 +0400 | [diff] [blame] | 1496 | kobject_put(&mod->mkobj.kobj); |
| 1497 | } |
| 1498 | |
Rusty Russell | 8f6d037 | 2010-08-05 12:59:09 -0600 | [diff] [blame] | 1499 | #else /* !CONFIG_SYSFS */ |
Denis V. Lunev | 34e4e2f | 2008-05-20 13:59:48 +0400 | [diff] [blame] | 1500 | |
Rusty Russell | 8f6d037 | 2010-08-05 12:59:09 -0600 | [diff] [blame] | 1501 | static int mod_sysfs_setup(struct module *mod, |
| 1502 | const struct load_info *info, |
Rusty Russell | 6407ebb2 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 1503 | struct kernel_param *kparam, |
| 1504 | unsigned int num_params) |
| 1505 | { |
| 1506 | return 0; |
| 1507 | } |
| 1508 | |
Denis V. Lunev | 34e4e2f | 2008-05-20 13:59:48 +0400 | [diff] [blame] | 1509 | static void mod_sysfs_fini(struct module *mod) |
| 1510 | { |
| 1511 | } |
| 1512 | |
Rusty Russell | 36b0360 | 2010-08-05 12:59:09 -0600 | [diff] [blame] | 1513 | static void module_remove_modinfo_attrs(struct module *mod) |
| 1514 | { |
| 1515 | } |
| 1516 | |
Rusty Russell | 80a3d1b | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 1517 | static void del_usage_links(struct module *mod) |
| 1518 | { |
| 1519 | } |
| 1520 | |
Denis V. Lunev | 34e4e2f | 2008-05-20 13:59:48 +0400 | [diff] [blame] | 1521 | #endif /* CONFIG_SYSFS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | |
Rusty Russell | 36b0360 | 2010-08-05 12:59:09 -0600 | [diff] [blame] | 1523 | static void mod_sysfs_teardown(struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | { |
Rusty Russell | 80a3d1b | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 1525 | del_usage_links(mod); |
Matt Domsch | c988d2b | 2005-06-23 22:05:15 -0700 | [diff] [blame] | 1526 | module_remove_modinfo_attrs(mod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | module_param_sysfs_remove(mod); |
Greg Kroah-Hartman | 78a2d90 | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 1528 | kobject_put(mod->mkobj.drivers_dir); |
| 1529 | kobject_put(mod->holders_dir); |
Denis V. Lunev | 34e4e2f | 2008-05-20 13:59:48 +0400 | [diff] [blame] | 1530 | mod_sysfs_fini(mod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | /* |
| 1534 | * unlink the module with the whole machine is stopped with interrupts off |
| 1535 | * - this defends against kallsyms not taking locks |
| 1536 | */ |
| 1537 | static int __unlink_module(void *_mod) |
| 1538 | { |
| 1539 | struct module *mod = _mod; |
| 1540 | list_del(&mod->list); |
Linus Torvalds | 5336377 | 2010-10-05 11:29:27 -0700 | [diff] [blame] | 1541 | module_bug_cleanup(mod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | return 0; |
| 1543 | } |
| 1544 | |
Rusty Russell | 7567650 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 1545 | /* Free a module, remove from lists, etc. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | static void free_module(struct module *mod) |
| 1547 | { |
Li Zefan | 7ead8b8 | 2009-08-17 16:56:28 +0800 | [diff] [blame] | 1548 | trace_module_free(mod); |
| 1549 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | /* Delete from various lists */ |
Rusty Russell | 7567650 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 1551 | mutex_lock(&module_mutex); |
Rusty Russell | 9b1a4d3 | 2008-07-28 12:16:30 -0500 | [diff] [blame] | 1552 | stop_machine(__unlink_module, mod, NULL); |
Rusty Russell | 7567650 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 1553 | mutex_unlock(&module_mutex); |
Rusty Russell | 36b0360 | 2010-08-05 12:59:09 -0600 | [diff] [blame] | 1554 | mod_sysfs_teardown(mod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | |
Jason Baron | b82bab4b | 2010-07-27 13:18:01 -0700 | [diff] [blame] | 1556 | /* Remove dynamic debug info */ |
| 1557 | ddebug_remove_module(mod->name); |
| 1558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | /* Arch-specific cleanup. */ |
| 1560 | module_arch_cleanup(mod); |
| 1561 | |
| 1562 | /* Module unload stuff */ |
| 1563 | module_unload_free(mod); |
| 1564 | |
Rusty Russell | e180a6b | 2009-03-31 13:05:29 -0600 | [diff] [blame] | 1565 | /* Free any allocated parameters. */ |
| 1566 | destroy_params(mod->kp, mod->num_kp); |
| 1567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | /* This may be NULL, but that's OK */ |
| 1569 | module_free(mod, mod->module_init); |
| 1570 | kfree(mod->args); |
Tejun Heo | 259354d | 2010-03-10 18:56:10 +0900 | [diff] [blame] | 1571 | percpu_modfree(mod); |
Rusty Russell | 9f85a4b | 2010-08-05 12:59:04 -0600 | [diff] [blame] | 1572 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1573 | /* Free lock-classes: */ |
| 1574 | lockdep_free_key_range(mod->module_core, mod->core_size); |
| 1575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | /* Finally, free the core (containing the module structure) */ |
| 1577 | module_free(mod, mod->module_core); |
Bernd Schmidt | eb8cdec | 2009-09-21 17:03:57 -0700 | [diff] [blame] | 1578 | |
| 1579 | #ifdef CONFIG_MPU |
| 1580 | update_protections(current->mm); |
| 1581 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | void *__symbol_get(const char *symbol) |
| 1585 | { |
| 1586 | struct module *owner; |
Tim Abbott | 414fd31 | 2008-12-05 19:03:56 -0500 | [diff] [blame] | 1587 | const struct kernel_symbol *sym; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | |
Rusty Russell | 24da1cb | 2007-07-15 23:41:46 -0700 | [diff] [blame] | 1589 | preempt_disable(); |
Tim Abbott | 414fd31 | 2008-12-05 19:03:56 -0500 | [diff] [blame] | 1590 | sym = find_symbol(symbol, &owner, NULL, true, true); |
| 1591 | if (sym && strong_try_module_get(owner)) |
| 1592 | sym = NULL; |
Rusty Russell | 24da1cb | 2007-07-15 23:41:46 -0700 | [diff] [blame] | 1593 | preempt_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | |
Tim Abbott | 414fd31 | 2008-12-05 19:03:56 -0500 | [diff] [blame] | 1595 | return sym ? (void *)sym->value : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | } |
| 1597 | EXPORT_SYMBOL_GPL(__symbol_get); |
| 1598 | |
Ashutosh Naik | eea8b54 | 2006-01-08 01:04:25 -0800 | [diff] [blame] | 1599 | /* |
| 1600 | * Ensure that an exported symbol [global namespace] does not already exist |
Robert P. J. Day | 02a3e59 | 2007-05-09 07:26:28 +0200 | [diff] [blame] | 1601 | * in the kernel or in some other module's exported symbol table. |
Rusty Russell | be593f4 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 1602 | * |
| 1603 | * You must hold the module_mutex. |
Ashutosh Naik | eea8b54 | 2006-01-08 01:04:25 -0800 | [diff] [blame] | 1604 | */ |
| 1605 | static int verify_export_symbols(struct module *mod) |
| 1606 | { |
Rusty Russell | b211104 | 2008-05-01 21:15:00 -0500 | [diff] [blame] | 1607 | unsigned int i; |
Ashutosh Naik | eea8b54 | 2006-01-08 01:04:25 -0800 | [diff] [blame] | 1608 | struct module *owner; |
Rusty Russell | b211104 | 2008-05-01 21:15:00 -0500 | [diff] [blame] | 1609 | const struct kernel_symbol *s; |
| 1610 | struct { |
| 1611 | const struct kernel_symbol *sym; |
| 1612 | unsigned int num; |
| 1613 | } arr[] = { |
| 1614 | { mod->syms, mod->num_syms }, |
| 1615 | { mod->gpl_syms, mod->num_gpl_syms }, |
| 1616 | { mod->gpl_future_syms, mod->num_gpl_future_syms }, |
Denys Vlasenko | f7f5b67 | 2008-07-22 19:24:26 -0500 | [diff] [blame] | 1617 | #ifdef CONFIG_UNUSED_SYMBOLS |
Rusty Russell | b211104 | 2008-05-01 21:15:00 -0500 | [diff] [blame] | 1618 | { mod->unused_syms, mod->num_unused_syms }, |
| 1619 | { mod->unused_gpl_syms, mod->num_unused_gpl_syms }, |
Denys Vlasenko | f7f5b67 | 2008-07-22 19:24:26 -0500 | [diff] [blame] | 1620 | #endif |
Rusty Russell | b211104 | 2008-05-01 21:15:00 -0500 | [diff] [blame] | 1621 | }; |
Ashutosh Naik | eea8b54 | 2006-01-08 01:04:25 -0800 | [diff] [blame] | 1622 | |
Rusty Russell | b211104 | 2008-05-01 21:15:00 -0500 | [diff] [blame] | 1623 | for (i = 0; i < ARRAY_SIZE(arr); i++) { |
| 1624 | for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) { |
Rusty Russell | be593f4 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 1625 | if (find_symbol(s->name, &owner, NULL, true, false)) { |
Rusty Russell | b211104 | 2008-05-01 21:15:00 -0500 | [diff] [blame] | 1626 | printk(KERN_ERR |
| 1627 | "%s: exports duplicate symbol %s" |
| 1628 | " (owned by %s)\n", |
| 1629 | mod->name, s->name, module_name(owner)); |
| 1630 | return -ENOEXEC; |
| 1631 | } |
Ashutosh Naik | eea8b54 | 2006-01-08 01:04:25 -0800 | [diff] [blame] | 1632 | } |
Rusty Russell | b211104 | 2008-05-01 21:15:00 -0500 | [diff] [blame] | 1633 | } |
| 1634 | return 0; |
Ashutosh Naik | eea8b54 | 2006-01-08 01:04:25 -0800 | [diff] [blame] | 1635 | } |
| 1636 | |
Matti Linnanvuori | 9a4b970 | 2007-11-08 08:37:38 -0800 | [diff] [blame] | 1637 | /* Change all symbols so that st_value encodes the pointer directly. */ |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1638 | static int simplify_symbols(struct module *mod, const struct load_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | { |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1640 | Elf_Shdr *symsec = &info->sechdrs[info->index.sym]; |
| 1641 | Elf_Sym *sym = (void *)symsec->sh_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | unsigned long secbase; |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1643 | unsigned int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | int ret = 0; |
Tim Abbott | 414fd31 | 2008-12-05 19:03:56 -0500 | [diff] [blame] | 1645 | const struct kernel_symbol *ksym; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1647 | for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) { |
| 1648 | const char *name = info->strtab + sym[i].st_name; |
| 1649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | switch (sym[i].st_shndx) { |
| 1651 | case SHN_COMMON: |
| 1652 | /* We compiled with -fno-common. These are not |
| 1653 | supposed to happen. */ |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1654 | DEBUGP("Common symbol: %s\n", name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | printk("%s: please compile with -fno-common\n", |
| 1656 | mod->name); |
| 1657 | ret = -ENOEXEC; |
| 1658 | break; |
| 1659 | |
| 1660 | case SHN_ABS: |
| 1661 | /* Don't need to do anything */ |
| 1662 | DEBUGP("Absolute symbol: 0x%08lx\n", |
| 1663 | (long)sym[i].st_value); |
| 1664 | break; |
| 1665 | |
| 1666 | case SHN_UNDEF: |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1667 | ksym = resolve_symbol_wait(mod, info, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | /* Ok if resolved. */ |
Rusty Russell | 9bea7f2 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 1669 | if (ksym && !IS_ERR(ksym)) { |
Tim Abbott | 414fd31 | 2008-12-05 19:03:56 -0500 | [diff] [blame] | 1670 | sym[i].st_value = ksym->value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | break; |
Tim Abbott | 414fd31 | 2008-12-05 19:03:56 -0500 | [diff] [blame] | 1672 | } |
| 1673 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | /* Ok if weak. */ |
Rusty Russell | 9bea7f2 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 1675 | if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | break; |
| 1677 | |
Rusty Russell | 9bea7f2 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 1678 | printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n", |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1679 | mod->name, name, PTR_ERR(ksym)); |
Rusty Russell | 9bea7f2 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 1680 | ret = PTR_ERR(ksym) ?: -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | break; |
| 1682 | |
| 1683 | default: |
| 1684 | /* Divert to percpu allocation if a percpu var. */ |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1685 | if (sym[i].st_shndx == info->index.pcpu) |
Tejun Heo | 259354d | 2010-03-10 18:56:10 +0900 | [diff] [blame] | 1686 | secbase = (unsigned long)mod_percpu(mod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | else |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1688 | secbase = info->sechdrs[sym[i].st_shndx].sh_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | sym[i].st_value += secbase; |
| 1690 | break; |
| 1691 | } |
| 1692 | } |
| 1693 | |
| 1694 | return ret; |
| 1695 | } |
| 1696 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1697 | static int apply_relocations(struct module *mod, const struct load_info *info) |
Rusty Russell | 22e268e | 2010-08-05 12:59:05 -0600 | [diff] [blame] | 1698 | { |
| 1699 | unsigned int i; |
| 1700 | int err = 0; |
| 1701 | |
| 1702 | /* Now do relocations. */ |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1703 | for (i = 1; i < info->hdr->e_shnum; i++) { |
| 1704 | unsigned int infosec = info->sechdrs[i].sh_info; |
Rusty Russell | 22e268e | 2010-08-05 12:59:05 -0600 | [diff] [blame] | 1705 | |
| 1706 | /* Not a valid relocation section? */ |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1707 | if (infosec >= info->hdr->e_shnum) |
Rusty Russell | 22e268e | 2010-08-05 12:59:05 -0600 | [diff] [blame] | 1708 | continue; |
| 1709 | |
| 1710 | /* Don't bother with non-allocated sections */ |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1711 | if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC)) |
Rusty Russell | 22e268e | 2010-08-05 12:59:05 -0600 | [diff] [blame] | 1712 | continue; |
| 1713 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1714 | if (info->sechdrs[i].sh_type == SHT_REL) |
| 1715 | err = apply_relocate(info->sechdrs, info->strtab, |
| 1716 | info->index.sym, i, mod); |
| 1717 | else if (info->sechdrs[i].sh_type == SHT_RELA) |
| 1718 | err = apply_relocate_add(info->sechdrs, info->strtab, |
| 1719 | info->index.sym, i, mod); |
Rusty Russell | 22e268e | 2010-08-05 12:59:05 -0600 | [diff] [blame] | 1720 | if (err < 0) |
| 1721 | break; |
| 1722 | } |
| 1723 | return err; |
| 1724 | } |
| 1725 | |
Helge Deller | 088af9a | 2008-12-31 12:31:18 +0100 | [diff] [blame] | 1726 | /* Additional bytes needed by arch in front of individual sections */ |
| 1727 | unsigned int __weak arch_mod_section_prepend(struct module *mod, |
| 1728 | unsigned int section) |
| 1729 | { |
| 1730 | /* default implementation just returns zero */ |
| 1731 | return 0; |
| 1732 | } |
| 1733 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | /* Update size with this section: return offset. */ |
Helge Deller | 088af9a | 2008-12-31 12:31:18 +0100 | [diff] [blame] | 1735 | static long get_offset(struct module *mod, unsigned int *size, |
| 1736 | Elf_Shdr *sechdr, unsigned int section) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | { |
| 1738 | long ret; |
| 1739 | |
Helge Deller | 088af9a | 2008-12-31 12:31:18 +0100 | [diff] [blame] | 1740 | *size += arch_mod_section_prepend(mod, section); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | ret = ALIGN(*size, sechdr->sh_addralign ?: 1); |
| 1742 | *size = ret + sechdr->sh_size; |
| 1743 | return ret; |
| 1744 | } |
| 1745 | |
| 1746 | /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld |
| 1747 | might -- code, read-only data, read-write data, small data. Tally |
| 1748 | sizes, and place the offsets into sh_entsize fields: high bit means it |
| 1749 | belongs in init. */ |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1750 | static void layout_sections(struct module *mod, struct load_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | { |
| 1752 | static unsigned long const masks[][2] = { |
| 1753 | /* NOTE: all executable code must be the first section |
| 1754 | * in this array; otherwise modify the text_size |
| 1755 | * finder in the two loops below */ |
| 1756 | { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL }, |
| 1757 | { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL }, |
| 1758 | { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL }, |
| 1759 | { ARCH_SHF_SMALL | SHF_ALLOC, 0 } |
| 1760 | }; |
| 1761 | unsigned int m, i; |
| 1762 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1763 | for (i = 0; i < info->hdr->e_shnum; i++) |
| 1764 | info->sechdrs[i].sh_entsize = ~0UL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | |
| 1766 | DEBUGP("Core section allocation order:\n"); |
| 1767 | for (m = 0; m < ARRAY_SIZE(masks); ++m) { |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1768 | for (i = 0; i < info->hdr->e_shnum; ++i) { |
| 1769 | Elf_Shdr *s = &info->sechdrs[i]; |
| 1770 | const char *sname = info->secstrings + s->sh_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | |
| 1772 | if ((s->sh_flags & masks[m][0]) != masks[m][0] |
| 1773 | || (s->sh_flags & masks[m][1]) |
| 1774 | || s->sh_entsize != ~0UL |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1775 | || strstarts(sname, ".init")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | continue; |
Helge Deller | 088af9a | 2008-12-31 12:31:18 +0100 | [diff] [blame] | 1777 | s->sh_entsize = get_offset(mod, &mod->core_size, s, i); |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1778 | DEBUGP("\t%s\n", name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | } |
| 1780 | if (m == 0) |
| 1781 | mod->core_text_size = mod->core_size; |
| 1782 | } |
| 1783 | |
| 1784 | DEBUGP("Init section allocation order:\n"); |
| 1785 | for (m = 0; m < ARRAY_SIZE(masks); ++m) { |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1786 | for (i = 0; i < info->hdr->e_shnum; ++i) { |
| 1787 | Elf_Shdr *s = &info->sechdrs[i]; |
| 1788 | const char *sname = info->secstrings + s->sh_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | |
| 1790 | if ((s->sh_flags & masks[m][0]) != masks[m][0] |
| 1791 | || (s->sh_flags & masks[m][1]) |
| 1792 | || s->sh_entsize != ~0UL |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1793 | || !strstarts(sname, ".init")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | continue; |
Helge Deller | 088af9a | 2008-12-31 12:31:18 +0100 | [diff] [blame] | 1795 | s->sh_entsize = (get_offset(mod, &mod->init_size, s, i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | | INIT_OFFSET_MASK); |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1797 | DEBUGP("\t%s\n", sname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | } |
| 1799 | if (m == 0) |
| 1800 | mod->init_text_size = mod->init_size; |
| 1801 | } |
| 1802 | } |
| 1803 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | static void set_license(struct module *mod, const char *license) |
| 1805 | { |
| 1806 | if (!license) |
| 1807 | license = "unspecified"; |
| 1808 | |
Florin Malita | fa3ba2e8 | 2006-10-11 01:21:48 -0700 | [diff] [blame] | 1809 | if (!license_is_gpl_compatible(license)) { |
Andi Kleen | 25ddbb1 | 2008-10-15 22:01:41 -0700 | [diff] [blame] | 1810 | if (!test_taint(TAINT_PROPRIETARY_MODULE)) |
Jan Dittmer | 1d4d262 | 2006-10-28 10:38:38 -0700 | [diff] [blame] | 1811 | printk(KERN_WARNING "%s: module license '%s' taints " |
Florin Malita | fa3ba2e8 | 2006-10-11 01:21:48 -0700 | [diff] [blame] | 1812 | "kernel.\n", mod->name, license); |
| 1813 | add_taint_module(mod, TAINT_PROPRIETARY_MODULE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | } |
| 1815 | } |
| 1816 | |
| 1817 | /* Parse tag=value strings from .modinfo section */ |
| 1818 | static char *next_string(char *string, unsigned long *secsize) |
| 1819 | { |
| 1820 | /* Skip non-zero chars */ |
| 1821 | while (string[0]) { |
| 1822 | string++; |
| 1823 | if ((*secsize)-- <= 1) |
| 1824 | return NULL; |
| 1825 | } |
| 1826 | |
| 1827 | /* Skip any zero padding. */ |
| 1828 | while (!string[0]) { |
| 1829 | string++; |
| 1830 | if ((*secsize)-- <= 1) |
| 1831 | return NULL; |
| 1832 | } |
| 1833 | return string; |
| 1834 | } |
| 1835 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1836 | static char *get_modinfo(struct load_info *info, const char *tag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | { |
| 1838 | char *p; |
| 1839 | unsigned int taglen = strlen(tag); |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1840 | Elf_Shdr *infosec = &info->sechdrs[info->index.info]; |
| 1841 | unsigned long size = infosec->sh_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1843 | for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=') |
| 1845 | return p + taglen + 1; |
| 1846 | } |
| 1847 | return NULL; |
| 1848 | } |
| 1849 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1850 | static void setup_modinfo(struct module *mod, struct load_info *info) |
Matt Domsch | c988d2b | 2005-06-23 22:05:15 -0700 | [diff] [blame] | 1851 | { |
| 1852 | struct module_attribute *attr; |
| 1853 | int i; |
| 1854 | |
| 1855 | for (i = 0; (attr = modinfo_attrs[i]); i++) { |
| 1856 | if (attr->setup) |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1857 | attr->setup(mod, get_modinfo(info, attr->attr.name)); |
Matt Domsch | c988d2b | 2005-06-23 22:05:15 -0700 | [diff] [blame] | 1858 | } |
| 1859 | } |
Matt Domsch | c988d2b | 2005-06-23 22:05:15 -0700 | [diff] [blame] | 1860 | |
Rusty Russell | a263f776 | 2009-09-25 00:32:58 -0600 | [diff] [blame] | 1861 | static void free_modinfo(struct module *mod) |
| 1862 | { |
| 1863 | struct module_attribute *attr; |
| 1864 | int i; |
| 1865 | |
| 1866 | for (i = 0; (attr = modinfo_attrs[i]); i++) { |
| 1867 | if (attr->free) |
| 1868 | attr->free(mod); |
| 1869 | } |
| 1870 | } |
| 1871 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | #ifdef CONFIG_KALLSYMS |
WANG Cong | 15bba37 | 2008-07-24 15:41:48 +0100 | [diff] [blame] | 1873 | |
| 1874 | /* lookup symbol in given range of kernel_symbols */ |
| 1875 | static const struct kernel_symbol *lookup_symbol(const char *name, |
| 1876 | const struct kernel_symbol *start, |
| 1877 | const struct kernel_symbol *stop) |
| 1878 | { |
| 1879 | const struct kernel_symbol *ks = start; |
| 1880 | for (; ks < stop; ks++) |
| 1881 | if (strcmp(ks->name, name) == 0) |
| 1882 | return ks; |
| 1883 | return NULL; |
| 1884 | } |
| 1885 | |
Tim Abbott | ca4787b | 2009-01-05 08:40:10 -0600 | [diff] [blame] | 1886 | static int is_exported(const char *name, unsigned long value, |
| 1887 | const struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | { |
Tim Abbott | ca4787b | 2009-01-05 08:40:10 -0600 | [diff] [blame] | 1889 | const struct kernel_symbol *ks; |
| 1890 | if (!mod) |
| 1891 | ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab); |
Sam Ravnborg | 3fd6805 | 2006-02-08 21:16:45 +0100 | [diff] [blame] | 1892 | else |
Tim Abbott | ca4787b | 2009-01-05 08:40:10 -0600 | [diff] [blame] | 1893 | ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms); |
| 1894 | return ks != NULL && ks->value == value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | } |
| 1896 | |
| 1897 | /* As per nm */ |
Rusty Russell | eded41c | 2010-08-05 12:59:07 -0600 | [diff] [blame] | 1898 | static char elf_type(const Elf_Sym *sym, const struct load_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | { |
Rusty Russell | eded41c | 2010-08-05 12:59:07 -0600 | [diff] [blame] | 1900 | const Elf_Shdr *sechdrs = info->sechdrs; |
| 1901 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1902 | if (ELF_ST_BIND(sym->st_info) == STB_WEAK) { |
| 1903 | if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT) |
| 1904 | return 'v'; |
| 1905 | else |
| 1906 | return 'w'; |
| 1907 | } |
| 1908 | if (sym->st_shndx == SHN_UNDEF) |
| 1909 | return 'U'; |
| 1910 | if (sym->st_shndx == SHN_ABS) |
| 1911 | return 'a'; |
| 1912 | if (sym->st_shndx >= SHN_LORESERVE) |
| 1913 | return '?'; |
| 1914 | if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR) |
| 1915 | return 't'; |
| 1916 | if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC |
| 1917 | && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) { |
| 1918 | if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE)) |
| 1919 | return 'r'; |
| 1920 | else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL) |
| 1921 | return 'g'; |
| 1922 | else |
| 1923 | return 'd'; |
| 1924 | } |
| 1925 | if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) { |
| 1926 | if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL) |
| 1927 | return 's'; |
| 1928 | else |
| 1929 | return 'b'; |
| 1930 | } |
Rusty Russell | eded41c | 2010-08-05 12:59:07 -0600 | [diff] [blame] | 1931 | if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name, |
| 1932 | ".debug")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | return 'n'; |
Rusty Russell | eded41c | 2010-08-05 12:59:07 -0600 | [diff] [blame] | 1934 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | return '?'; |
| 1936 | } |
| 1937 | |
Jan Beulich | 4a49622 | 2009-07-06 14:50:42 +0100 | [diff] [blame] | 1938 | static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs, |
| 1939 | unsigned int shnum) |
| 1940 | { |
| 1941 | const Elf_Shdr *sec; |
| 1942 | |
| 1943 | if (src->st_shndx == SHN_UNDEF |
| 1944 | || src->st_shndx >= shnum |
| 1945 | || !src->st_name) |
| 1946 | return false; |
| 1947 | |
| 1948 | sec = sechdrs + src->st_shndx; |
| 1949 | if (!(sec->sh_flags & SHF_ALLOC) |
| 1950 | #ifndef CONFIG_KALLSYMS_ALL |
| 1951 | || !(sec->sh_flags & SHF_EXECINSTR) |
| 1952 | #endif |
| 1953 | || (sec->sh_entsize & INIT_OFFSET_MASK)) |
| 1954 | return false; |
| 1955 | |
| 1956 | return true; |
| 1957 | } |
| 1958 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1959 | static void layout_symtab(struct module *mod, struct load_info *info) |
Jan Beulich | 4a49622 | 2009-07-06 14:50:42 +0100 | [diff] [blame] | 1960 | { |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1961 | Elf_Shdr *symsect = info->sechdrs + info->index.sym; |
| 1962 | Elf_Shdr *strsect = info->sechdrs + info->index.str; |
Jan Beulich | 4a49622 | 2009-07-06 14:50:42 +0100 | [diff] [blame] | 1963 | const Elf_Sym *src; |
| 1964 | unsigned int i, nsrc, ndst; |
| 1965 | |
| 1966 | /* Put symbol section at end of init part of module. */ |
| 1967 | symsect->sh_flags |= SHF_ALLOC; |
| 1968 | symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect, |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1969 | info->index.sym) | INIT_OFFSET_MASK; |
| 1970 | DEBUGP("\t%s\n", info->secstrings + symsect->sh_name); |
Jan Beulich | 4a49622 | 2009-07-06 14:50:42 +0100 | [diff] [blame] | 1971 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1972 | src = (void *)info->hdr + symsect->sh_offset; |
Jan Beulich | 4a49622 | 2009-07-06 14:50:42 +0100 | [diff] [blame] | 1973 | nsrc = symsect->sh_size / sizeof(*src); |
| 1974 | for (ndst = i = 1; i < nsrc; ++i, ++src) |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1975 | if (is_core_symbol(src, info->sechdrs, info->hdr->e_shnum)) { |
Jan Beulich | 554bdfe | 2009-07-06 14:51:44 +0100 | [diff] [blame] | 1976 | unsigned int j = src->st_name; |
| 1977 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1978 | while (!__test_and_set_bit(j, info->strmap) |
| 1979 | && info->strtab[j]) |
Jan Beulich | 554bdfe | 2009-07-06 14:51:44 +0100 | [diff] [blame] | 1980 | ++j; |
Jan Beulich | 4a49622 | 2009-07-06 14:50:42 +0100 | [diff] [blame] | 1981 | ++ndst; |
Jan Beulich | 554bdfe | 2009-07-06 14:51:44 +0100 | [diff] [blame] | 1982 | } |
Jan Beulich | 4a49622 | 2009-07-06 14:50:42 +0100 | [diff] [blame] | 1983 | |
| 1984 | /* Append room for core symbols at end of core part. */ |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1985 | info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1); |
| 1986 | mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym); |
Jan Beulich | 4a49622 | 2009-07-06 14:50:42 +0100 | [diff] [blame] | 1987 | |
Jan Beulich | 554bdfe | 2009-07-06 14:51:44 +0100 | [diff] [blame] | 1988 | /* Put string table section at end of init part of module. */ |
| 1989 | strsect->sh_flags |= SHF_ALLOC; |
| 1990 | strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect, |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1991 | info->index.str) | INIT_OFFSET_MASK; |
| 1992 | DEBUGP("\t%s\n", info->secstrings + strsect->sh_name); |
Jan Beulich | 554bdfe | 2009-07-06 14:51:44 +0100 | [diff] [blame] | 1993 | |
| 1994 | /* Append room for core symbols' strings at end of core part. */ |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 1995 | info->stroffs = mod->core_size; |
| 1996 | __set_bit(0, info->strmap); |
| 1997 | mod->core_size += bitmap_weight(info->strmap, strsect->sh_size); |
Jan Beulich | 4a49622 | 2009-07-06 14:50:42 +0100 | [diff] [blame] | 1998 | } |
| 1999 | |
Rusty Russell | 811d66a | 2010-08-05 12:59:12 -0600 | [diff] [blame] | 2000 | static void add_kallsyms(struct module *mod, const struct load_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | { |
Jan Beulich | 4a49622 | 2009-07-06 14:50:42 +0100 | [diff] [blame] | 2002 | unsigned int i, ndst; |
| 2003 | const Elf_Sym *src; |
| 2004 | Elf_Sym *dst; |
Jan Beulich | 554bdfe | 2009-07-06 14:51:44 +0100 | [diff] [blame] | 2005 | char *s; |
Rusty Russell | eded41c | 2010-08-05 12:59:07 -0600 | [diff] [blame] | 2006 | Elf_Shdr *symsec = &info->sechdrs[info->index.sym]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2007 | |
Rusty Russell | eded41c | 2010-08-05 12:59:07 -0600 | [diff] [blame] | 2008 | mod->symtab = (void *)symsec->sh_addr; |
| 2009 | mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym); |
Rusty Russell | 511ca6a | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2010 | /* Make sure we get permanent strtab: don't use info->strtab. */ |
| 2011 | mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | |
| 2013 | /* Set types up while we still have access to sections. */ |
| 2014 | for (i = 0; i < mod->num_symtab; i++) |
Rusty Russell | eded41c | 2010-08-05 12:59:07 -0600 | [diff] [blame] | 2015 | mod->symtab[i].st_info = elf_type(&mod->symtab[i], info); |
Jan Beulich | 4a49622 | 2009-07-06 14:50:42 +0100 | [diff] [blame] | 2016 | |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2017 | mod->core_symtab = dst = mod->module_core + info->symoffs; |
Jan Beulich | 4a49622 | 2009-07-06 14:50:42 +0100 | [diff] [blame] | 2018 | src = mod->symtab; |
| 2019 | *dst = *src; |
| 2020 | for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) { |
Rusty Russell | eded41c | 2010-08-05 12:59:07 -0600 | [diff] [blame] | 2021 | if (!is_core_symbol(src, info->sechdrs, info->hdr->e_shnum)) |
Jan Beulich | 4a49622 | 2009-07-06 14:50:42 +0100 | [diff] [blame] | 2022 | continue; |
| 2023 | dst[ndst] = *src; |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2024 | dst[ndst].st_name = bitmap_weight(info->strmap, |
| 2025 | dst[ndst].st_name); |
Jan Beulich | 4a49622 | 2009-07-06 14:50:42 +0100 | [diff] [blame] | 2026 | ++ndst; |
| 2027 | } |
| 2028 | mod->core_num_syms = ndst; |
Jan Beulich | 554bdfe | 2009-07-06 14:51:44 +0100 | [diff] [blame] | 2029 | |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2030 | mod->core_strtab = s = mod->module_core + info->stroffs; |
Rusty Russell | eded41c | 2010-08-05 12:59:07 -0600 | [diff] [blame] | 2031 | for (*s = 0, i = 1; i < info->sechdrs[info->index.str].sh_size; ++i) |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2032 | if (test_bit(i, info->strmap)) |
Jan Beulich | 554bdfe | 2009-07-06 14:51:44 +0100 | [diff] [blame] | 2033 | *++s = mod->strtab[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | } |
| 2035 | #else |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2036 | static inline void layout_symtab(struct module *mod, struct load_info *info) |
Jan Beulich | 4a49622 | 2009-07-06 14:50:42 +0100 | [diff] [blame] | 2037 | { |
| 2038 | } |
Paul Mundt | 3ae91c2 | 2009-10-01 15:43:54 -0700 | [diff] [blame] | 2039 | |
Michał Mirosław | abbce90 | 2010-09-20 01:58:08 +0200 | [diff] [blame^] | 2040 | static void add_kallsyms(struct module *mod, const struct load_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | { |
| 2042 | } |
| 2043 | #endif /* CONFIG_KALLSYMS */ |
| 2044 | |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 2045 | static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num) |
Rusty Russell | 5e458cc | 2008-10-22 10:00:13 -0500 | [diff] [blame] | 2046 | { |
Rusty Russell | 811d66a | 2010-08-05 12:59:12 -0600 | [diff] [blame] | 2047 | if (!debug) |
| 2048 | return; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 2049 | #ifdef CONFIG_DYNAMIC_DEBUG |
| 2050 | if (ddebug_add_module(debug, num, debug->modname)) |
| 2051 | printk(KERN_ERR "dynamic debug error adding module: %s\n", |
| 2052 | debug->modname); |
| 2053 | #endif |
Rusty Russell | 5e458cc | 2008-10-22 10:00:13 -0500 | [diff] [blame] | 2054 | } |
Jason Baron | 346e15b | 2008-08-12 16:46:19 -0400 | [diff] [blame] | 2055 | |
Yehuda Sadeh | ff49d74 | 2010-07-03 13:07:35 +1000 | [diff] [blame] | 2056 | static void dynamic_debug_remove(struct _ddebug *debug) |
| 2057 | { |
| 2058 | if (debug) |
| 2059 | ddebug_remove_module(debug->modname); |
| 2060 | } |
| 2061 | |
Rusty Russell | 3a642e9 | 2008-07-22 19:24:28 -0500 | [diff] [blame] | 2062 | static void *module_alloc_update_bounds(unsigned long size) |
| 2063 | { |
| 2064 | void *ret = module_alloc(size); |
| 2065 | |
| 2066 | if (ret) { |
Rusty Russell | 7567650 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 2067 | mutex_lock(&module_mutex); |
Rusty Russell | 3a642e9 | 2008-07-22 19:24:28 -0500 | [diff] [blame] | 2068 | /* Update module bounds. */ |
| 2069 | if ((unsigned long)ret < module_addr_min) |
| 2070 | module_addr_min = (unsigned long)ret; |
| 2071 | if ((unsigned long)ret + size > module_addr_max) |
| 2072 | module_addr_max = (unsigned long)ret + size; |
Rusty Russell | 7567650 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 2073 | mutex_unlock(&module_mutex); |
Rusty Russell | 3a642e9 | 2008-07-22 19:24:28 -0500 | [diff] [blame] | 2074 | } |
| 2075 | return ret; |
| 2076 | } |
| 2077 | |
Catalin Marinas | 4f2294b | 2009-06-11 13:23:20 +0100 | [diff] [blame] | 2078 | #ifdef CONFIG_DEBUG_KMEMLEAK |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2079 | static void kmemleak_load_module(const struct module *mod, |
| 2080 | const struct load_info *info) |
Catalin Marinas | 4f2294b | 2009-06-11 13:23:20 +0100 | [diff] [blame] | 2081 | { |
| 2082 | unsigned int i; |
| 2083 | |
| 2084 | /* only scan the sections containing data */ |
Catalin Marinas | c017b4b | 2009-10-28 13:33:09 +0000 | [diff] [blame] | 2085 | kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL); |
Catalin Marinas | 4f2294b | 2009-06-11 13:23:20 +0100 | [diff] [blame] | 2086 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2087 | for (i = 1; i < info->hdr->e_shnum; i++) { |
| 2088 | const char *name = info->secstrings + info->sechdrs[i].sh_name; |
| 2089 | if (!(info->sechdrs[i].sh_flags & SHF_ALLOC)) |
Catalin Marinas | 4f2294b | 2009-06-11 13:23:20 +0100 | [diff] [blame] | 2090 | continue; |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2091 | if (!strstarts(name, ".data") && !strstarts(name, ".bss")) |
Catalin Marinas | 4f2294b | 2009-06-11 13:23:20 +0100 | [diff] [blame] | 2092 | continue; |
| 2093 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2094 | kmemleak_scan_area((void *)info->sechdrs[i].sh_addr, |
| 2095 | info->sechdrs[i].sh_size, GFP_KERNEL); |
Catalin Marinas | 4f2294b | 2009-06-11 13:23:20 +0100 | [diff] [blame] | 2096 | } |
| 2097 | } |
| 2098 | #else |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2099 | static inline void kmemleak_load_module(const struct module *mod, |
| 2100 | const struct load_info *info) |
Catalin Marinas | 4f2294b | 2009-06-11 13:23:20 +0100 | [diff] [blame] | 2101 | { |
| 2102 | } |
| 2103 | #endif |
| 2104 | |
Rusty Russell | 6526c53 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2105 | /* Sets info->hdr and info->len. */ |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2106 | static int copy_and_check(struct load_info *info, |
| 2107 | const void __user *umod, unsigned long len, |
| 2108 | const char __user *uargs) |
Rusty Russell | 40dd256 | 2010-08-05 12:59:03 -0600 | [diff] [blame] | 2109 | { |
| 2110 | int err; |
| 2111 | Elf_Ehdr *hdr; |
| 2112 | |
| 2113 | if (len < sizeof(*hdr)) |
| 2114 | return -ENOEXEC; |
| 2115 | |
| 2116 | /* Suck in entire file: we'll want most of it. */ |
| 2117 | /* vmalloc barfs on "unusual" numbers. Check here */ |
Linus Torvalds | 3264d3f | 2010-06-02 11:01:06 -0700 | [diff] [blame] | 2118 | if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL) |
Rusty Russell | 40dd256 | 2010-08-05 12:59:03 -0600 | [diff] [blame] | 2119 | return -ENOMEM; |
| 2120 | |
| 2121 | if (copy_from_user(hdr, umod, len) != 0) { |
| 2122 | err = -EFAULT; |
| 2123 | goto free_hdr; |
| 2124 | } |
| 2125 | |
| 2126 | /* Sanity checks against insmoding binaries or wrong arch, |
| 2127 | weird elf version */ |
| 2128 | if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0 |
| 2129 | || hdr->e_type != ET_REL |
| 2130 | || !elf_check_arch(hdr) |
| 2131 | || hdr->e_shentsize != sizeof(Elf_Shdr)) { |
| 2132 | err = -ENOEXEC; |
| 2133 | goto free_hdr; |
| 2134 | } |
| 2135 | |
| 2136 | if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) { |
| 2137 | err = -ENOEXEC; |
| 2138 | goto free_hdr; |
| 2139 | } |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2140 | |
Linus Torvalds | 3264d3f | 2010-06-02 11:01:06 -0700 | [diff] [blame] | 2141 | info->hdr = hdr; |
| 2142 | info->len = len; |
Rusty Russell | 40dd256 | 2010-08-05 12:59:03 -0600 | [diff] [blame] | 2143 | return 0; |
| 2144 | |
| 2145 | free_hdr: |
| 2146 | vfree(hdr); |
| 2147 | return err; |
| 2148 | } |
| 2149 | |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2150 | static void free_copy(struct load_info *info) |
| 2151 | { |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2152 | vfree(info->hdr); |
| 2153 | } |
| 2154 | |
Rusty Russell | 8b5f61a | 2010-08-05 12:59:06 -0600 | [diff] [blame] | 2155 | static int rewrite_section_headers(struct load_info *info) |
| 2156 | { |
| 2157 | unsigned int i; |
| 2158 | |
| 2159 | /* This should always be true, but let's be sure. */ |
| 2160 | info->sechdrs[0].sh_addr = 0; |
| 2161 | |
| 2162 | for (i = 1; i < info->hdr->e_shnum; i++) { |
| 2163 | Elf_Shdr *shdr = &info->sechdrs[i]; |
| 2164 | if (shdr->sh_type != SHT_NOBITS |
| 2165 | && info->len < shdr->sh_offset + shdr->sh_size) { |
| 2166 | printk(KERN_ERR "Module len %lu truncated\n", |
| 2167 | info->len); |
| 2168 | return -ENOEXEC; |
| 2169 | } |
| 2170 | |
| 2171 | /* Mark all sections sh_addr with their address in the |
| 2172 | temporary image. */ |
| 2173 | shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset; |
| 2174 | |
| 2175 | #ifndef CONFIG_MODULE_UNLOAD |
| 2176 | /* Don't load .exit sections */ |
| 2177 | if (strstarts(info->secstrings+shdr->sh_name, ".exit")) |
| 2178 | shdr->sh_flags &= ~(unsigned long)SHF_ALLOC; |
| 2179 | #endif |
Rusty Russell | 8b5f61a | 2010-08-05 12:59:06 -0600 | [diff] [blame] | 2180 | } |
Rusty Russell | d6df72a | 2010-08-05 12:59:07 -0600 | [diff] [blame] | 2181 | |
| 2182 | /* Track but don't keep modinfo and version sections. */ |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2183 | info->index.vers = find_sec(info, "__versions"); |
| 2184 | info->index.info = find_sec(info, ".modinfo"); |
Rusty Russell | d6df72a | 2010-08-05 12:59:07 -0600 | [diff] [blame] | 2185 | info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC; |
| 2186 | info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC; |
Rusty Russell | 8b5f61a | 2010-08-05 12:59:06 -0600 | [diff] [blame] | 2187 | return 0; |
| 2188 | } |
| 2189 | |
Linus Torvalds | 3264d3f | 2010-06-02 11:01:06 -0700 | [diff] [blame] | 2190 | /* |
| 2191 | * Set up our basic convenience variables (pointers to section headers, |
| 2192 | * search for module section index etc), and do some basic section |
| 2193 | * verification. |
| 2194 | * |
| 2195 | * Return the temporary module pointer (we'll replace it with the final |
| 2196 | * one when we move the module sections around). |
| 2197 | */ |
| 2198 | static struct module *setup_load_info(struct load_info *info) |
| 2199 | { |
| 2200 | unsigned int i; |
Rusty Russell | 8b5f61a | 2010-08-05 12:59:06 -0600 | [diff] [blame] | 2201 | int err; |
Linus Torvalds | 3264d3f | 2010-06-02 11:01:06 -0700 | [diff] [blame] | 2202 | struct module *mod; |
| 2203 | |
| 2204 | /* Set up the convenience variables */ |
| 2205 | info->sechdrs = (void *)info->hdr + info->hdr->e_shoff; |
Rusty Russell | 8b5f61a | 2010-08-05 12:59:06 -0600 | [diff] [blame] | 2206 | info->secstrings = (void *)info->hdr |
| 2207 | + info->sechdrs[info->hdr->e_shstrndx].sh_offset; |
Linus Torvalds | 3264d3f | 2010-06-02 11:01:06 -0700 | [diff] [blame] | 2208 | |
Rusty Russell | 8b5f61a | 2010-08-05 12:59:06 -0600 | [diff] [blame] | 2209 | err = rewrite_section_headers(info); |
| 2210 | if (err) |
| 2211 | return ERR_PTR(err); |
| 2212 | |
| 2213 | /* Find internal symbols and strings. */ |
Linus Torvalds | 3264d3f | 2010-06-02 11:01:06 -0700 | [diff] [blame] | 2214 | for (i = 1; i < info->hdr->e_shnum; i++) { |
Linus Torvalds | 3264d3f | 2010-06-02 11:01:06 -0700 | [diff] [blame] | 2215 | if (info->sechdrs[i].sh_type == SHT_SYMTAB) { |
| 2216 | info->index.sym = i; |
| 2217 | info->index.str = info->sechdrs[i].sh_link; |
Rusty Russell | 8b5f61a | 2010-08-05 12:59:06 -0600 | [diff] [blame] | 2218 | info->strtab = (char *)info->hdr |
| 2219 | + info->sechdrs[info->index.str].sh_offset; |
| 2220 | break; |
Linus Torvalds | 3264d3f | 2010-06-02 11:01:06 -0700 | [diff] [blame] | 2221 | } |
Linus Torvalds | 3264d3f | 2010-06-02 11:01:06 -0700 | [diff] [blame] | 2222 | } |
| 2223 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2224 | info->index.mod = find_sec(info, ".gnu.linkonce.this_module"); |
Linus Torvalds | 3264d3f | 2010-06-02 11:01:06 -0700 | [diff] [blame] | 2225 | if (!info->index.mod) { |
| 2226 | printk(KERN_WARNING "No module found in object\n"); |
| 2227 | return ERR_PTR(-ENOEXEC); |
| 2228 | } |
| 2229 | /* This is temporary: point mod into copy of data. */ |
| 2230 | mod = (void *)info->sechdrs[info->index.mod].sh_addr; |
| 2231 | |
| 2232 | if (info->index.sym == 0) { |
| 2233 | printk(KERN_WARNING "%s: module has no symbols (stripped?)\n", |
| 2234 | mod->name); |
| 2235 | return ERR_PTR(-ENOEXEC); |
| 2236 | } |
| 2237 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2238 | info->index.pcpu = find_pcpusec(info); |
Linus Torvalds | 3264d3f | 2010-06-02 11:01:06 -0700 | [diff] [blame] | 2239 | |
Linus Torvalds | 3264d3f | 2010-06-02 11:01:06 -0700 | [diff] [blame] | 2240 | /* Check module struct version now, before we try to use module. */ |
| 2241 | if (!check_modstruct_version(info->sechdrs, info->index.vers, mod)) |
| 2242 | return ERR_PTR(-ENOEXEC); |
| 2243 | |
| 2244 | return mod; |
Linus Torvalds | 3264d3f | 2010-06-02 11:01:06 -0700 | [diff] [blame] | 2245 | } |
| 2246 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2247 | static int check_modinfo(struct module *mod, struct load_info *info) |
Rusty Russell | 40dd256 | 2010-08-05 12:59:03 -0600 | [diff] [blame] | 2248 | { |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2249 | const char *modmagic = get_modinfo(info, "vermagic"); |
Rusty Russell | 40dd256 | 2010-08-05 12:59:03 -0600 | [diff] [blame] | 2250 | int err; |
| 2251 | |
| 2252 | /* This is allowed: modprobe --force will invalidate it. */ |
| 2253 | if (!modmagic) { |
| 2254 | err = try_to_force_load(mod, "bad vermagic"); |
| 2255 | if (err) |
| 2256 | return err; |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2257 | } else if (!same_magic(modmagic, vermagic, info->index.vers)) { |
Rusty Russell | 40dd256 | 2010-08-05 12:59:03 -0600 | [diff] [blame] | 2258 | printk(KERN_ERR "%s: version magic '%s' should be '%s'\n", |
| 2259 | mod->name, modmagic, vermagic); |
| 2260 | return -ENOEXEC; |
| 2261 | } |
| 2262 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2263 | if (get_modinfo(info, "staging")) { |
Rusty Russell | 40dd256 | 2010-08-05 12:59:03 -0600 | [diff] [blame] | 2264 | add_taint_module(mod, TAINT_CRAP); |
| 2265 | printk(KERN_WARNING "%s: module is from the staging directory," |
| 2266 | " the quality is unknown, you have been warned.\n", |
| 2267 | mod->name); |
| 2268 | } |
Rusty Russell | 22e268e | 2010-08-05 12:59:05 -0600 | [diff] [blame] | 2269 | |
| 2270 | /* Set up license info based on the info section */ |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2271 | set_license(mod, get_modinfo(info, "license")); |
Rusty Russell | 22e268e | 2010-08-05 12:59:05 -0600 | [diff] [blame] | 2272 | |
Rusty Russell | 40dd256 | 2010-08-05 12:59:03 -0600 | [diff] [blame] | 2273 | return 0; |
| 2274 | } |
| 2275 | |
Rusty Russell | 811d66a | 2010-08-05 12:59:12 -0600 | [diff] [blame] | 2276 | static void find_module_sections(struct module *mod, struct load_info *info) |
Linus Torvalds | f91a13b | 2010-08-05 12:59:02 -0600 | [diff] [blame] | 2277 | { |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2278 | mod->kp = section_objs(info, "__param", |
Linus Torvalds | f91a13b | 2010-08-05 12:59:02 -0600 | [diff] [blame] | 2279 | sizeof(*mod->kp), &mod->num_kp); |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2280 | mod->syms = section_objs(info, "__ksymtab", |
Linus Torvalds | f91a13b | 2010-08-05 12:59:02 -0600 | [diff] [blame] | 2281 | sizeof(*mod->syms), &mod->num_syms); |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2282 | mod->crcs = section_addr(info, "__kcrctab"); |
| 2283 | mod->gpl_syms = section_objs(info, "__ksymtab_gpl", |
Linus Torvalds | f91a13b | 2010-08-05 12:59:02 -0600 | [diff] [blame] | 2284 | sizeof(*mod->gpl_syms), |
| 2285 | &mod->num_gpl_syms); |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2286 | mod->gpl_crcs = section_addr(info, "__kcrctab_gpl"); |
| 2287 | mod->gpl_future_syms = section_objs(info, |
Linus Torvalds | f91a13b | 2010-08-05 12:59:02 -0600 | [diff] [blame] | 2288 | "__ksymtab_gpl_future", |
| 2289 | sizeof(*mod->gpl_future_syms), |
| 2290 | &mod->num_gpl_future_syms); |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2291 | mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future"); |
Linus Torvalds | f91a13b | 2010-08-05 12:59:02 -0600 | [diff] [blame] | 2292 | |
| 2293 | #ifdef CONFIG_UNUSED_SYMBOLS |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2294 | mod->unused_syms = section_objs(info, "__ksymtab_unused", |
Linus Torvalds | f91a13b | 2010-08-05 12:59:02 -0600 | [diff] [blame] | 2295 | sizeof(*mod->unused_syms), |
| 2296 | &mod->num_unused_syms); |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2297 | mod->unused_crcs = section_addr(info, "__kcrctab_unused"); |
| 2298 | mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl", |
Linus Torvalds | f91a13b | 2010-08-05 12:59:02 -0600 | [diff] [blame] | 2299 | sizeof(*mod->unused_gpl_syms), |
| 2300 | &mod->num_unused_gpl_syms); |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2301 | mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl"); |
Linus Torvalds | f91a13b | 2010-08-05 12:59:02 -0600 | [diff] [blame] | 2302 | #endif |
| 2303 | #ifdef CONFIG_CONSTRUCTORS |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2304 | mod->ctors = section_objs(info, ".ctors", |
Linus Torvalds | f91a13b | 2010-08-05 12:59:02 -0600 | [diff] [blame] | 2305 | sizeof(*mod->ctors), &mod->num_ctors); |
| 2306 | #endif |
| 2307 | |
| 2308 | #ifdef CONFIG_TRACEPOINTS |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2309 | mod->tracepoints = section_objs(info, "__tracepoints", |
Linus Torvalds | f91a13b | 2010-08-05 12:59:02 -0600 | [diff] [blame] | 2310 | sizeof(*mod->tracepoints), |
| 2311 | &mod->num_tracepoints); |
| 2312 | #endif |
Jason Baron | bf5438fc | 2010-09-17 11:09:00 -0400 | [diff] [blame] | 2313 | #ifdef HAVE_JUMP_LABEL |
| 2314 | mod->jump_entries = section_objs(info, "__jump_table", |
| 2315 | sizeof(*mod->jump_entries), |
| 2316 | &mod->num_jump_entries); |
| 2317 | #endif |
Linus Torvalds | f91a13b | 2010-08-05 12:59:02 -0600 | [diff] [blame] | 2318 | #ifdef CONFIG_EVENT_TRACING |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2319 | mod->trace_events = section_objs(info, "_ftrace_events", |
Linus Torvalds | f91a13b | 2010-08-05 12:59:02 -0600 | [diff] [blame] | 2320 | sizeof(*mod->trace_events), |
| 2321 | &mod->num_trace_events); |
| 2322 | /* |
| 2323 | * This section contains pointers to allocated objects in the trace |
| 2324 | * code and not scanning it leads to false positives. |
| 2325 | */ |
| 2326 | kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) * |
| 2327 | mod->num_trace_events, GFP_KERNEL); |
| 2328 | #endif |
| 2329 | #ifdef CONFIG_FTRACE_MCOUNT_RECORD |
| 2330 | /* sechdrs[0].sh_size is always zero */ |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2331 | mod->ftrace_callsites = section_objs(info, "__mcount_loc", |
Linus Torvalds | f91a13b | 2010-08-05 12:59:02 -0600 | [diff] [blame] | 2332 | sizeof(*mod->ftrace_callsites), |
| 2333 | &mod->num_ftrace_callsites); |
| 2334 | #endif |
Rusty Russell | 22e268e | 2010-08-05 12:59:05 -0600 | [diff] [blame] | 2335 | |
Rusty Russell | 811d66a | 2010-08-05 12:59:12 -0600 | [diff] [blame] | 2336 | mod->extable = section_objs(info, "__ex_table", |
| 2337 | sizeof(*mod->extable), &mod->num_exentries); |
| 2338 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2339 | if (section_addr(info, "__obsparm")) |
Rusty Russell | 22e268e | 2010-08-05 12:59:05 -0600 | [diff] [blame] | 2340 | printk(KERN_WARNING "%s: Ignoring obsolete parameters\n", |
| 2341 | mod->name); |
Rusty Russell | 811d66a | 2010-08-05 12:59:12 -0600 | [diff] [blame] | 2342 | |
| 2343 | info->debug = section_objs(info, "__verbose", |
| 2344 | sizeof(*info->debug), &info->num_debug); |
Linus Torvalds | f91a13b | 2010-08-05 12:59:02 -0600 | [diff] [blame] | 2345 | } |
| 2346 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2347 | static int move_module(struct module *mod, struct load_info *info) |
Linus Torvalds | 65b8a9b | 2010-08-05 12:59:02 -0600 | [diff] [blame] | 2348 | { |
| 2349 | int i; |
| 2350 | void *ptr; |
| 2351 | |
| 2352 | /* Do the allocs. */ |
| 2353 | ptr = module_alloc_update_bounds(mod->core_size); |
| 2354 | /* |
| 2355 | * The pointer to this block is stored in the module structure |
| 2356 | * which is inside the block. Just mark it as not being a |
| 2357 | * leak. |
| 2358 | */ |
| 2359 | kmemleak_not_leak(ptr); |
| 2360 | if (!ptr) |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2361 | return -ENOMEM; |
Linus Torvalds | 65b8a9b | 2010-08-05 12:59:02 -0600 | [diff] [blame] | 2362 | |
| 2363 | memset(ptr, 0, mod->core_size); |
| 2364 | mod->module_core = ptr; |
| 2365 | |
| 2366 | ptr = module_alloc_update_bounds(mod->init_size); |
| 2367 | /* |
| 2368 | * The pointer to this block is stored in the module structure |
| 2369 | * which is inside the block. This block doesn't need to be |
| 2370 | * scanned as it contains data and code that will be freed |
| 2371 | * after the module is initialized. |
| 2372 | */ |
| 2373 | kmemleak_ignore(ptr); |
| 2374 | if (!ptr && mod->init_size) { |
| 2375 | module_free(mod, mod->module_core); |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2376 | return -ENOMEM; |
Linus Torvalds | 65b8a9b | 2010-08-05 12:59:02 -0600 | [diff] [blame] | 2377 | } |
| 2378 | memset(ptr, 0, mod->init_size); |
| 2379 | mod->module_init = ptr; |
| 2380 | |
| 2381 | /* Transfer each section which specifies SHF_ALLOC */ |
| 2382 | DEBUGP("final section addresses:\n"); |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2383 | for (i = 0; i < info->hdr->e_shnum; i++) { |
Linus Torvalds | 65b8a9b | 2010-08-05 12:59:02 -0600 | [diff] [blame] | 2384 | void *dest; |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2385 | Elf_Shdr *shdr = &info->sechdrs[i]; |
Linus Torvalds | 65b8a9b | 2010-08-05 12:59:02 -0600 | [diff] [blame] | 2386 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2387 | if (!(shdr->sh_flags & SHF_ALLOC)) |
Linus Torvalds | 65b8a9b | 2010-08-05 12:59:02 -0600 | [diff] [blame] | 2388 | continue; |
| 2389 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2390 | if (shdr->sh_entsize & INIT_OFFSET_MASK) |
Linus Torvalds | 65b8a9b | 2010-08-05 12:59:02 -0600 | [diff] [blame] | 2391 | dest = mod->module_init |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2392 | + (shdr->sh_entsize & ~INIT_OFFSET_MASK); |
Linus Torvalds | 65b8a9b | 2010-08-05 12:59:02 -0600 | [diff] [blame] | 2393 | else |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2394 | dest = mod->module_core + shdr->sh_entsize; |
Linus Torvalds | 65b8a9b | 2010-08-05 12:59:02 -0600 | [diff] [blame] | 2395 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2396 | if (shdr->sh_type != SHT_NOBITS) |
| 2397 | memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size); |
Linus Torvalds | 65b8a9b | 2010-08-05 12:59:02 -0600 | [diff] [blame] | 2398 | /* Update sh_addr to point to copy in image. */ |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2399 | shdr->sh_addr = (unsigned long)dest; |
Linus Torvalds | 65b8a9b | 2010-08-05 12:59:02 -0600 | [diff] [blame] | 2400 | DEBUGP("\t0x%lx %s\n", |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2401 | shdr->sh_addr, info->secstrings + shdr->sh_name); |
Linus Torvalds | 65b8a9b | 2010-08-05 12:59:02 -0600 | [diff] [blame] | 2402 | } |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2403 | |
| 2404 | return 0; |
Linus Torvalds | 65b8a9b | 2010-08-05 12:59:02 -0600 | [diff] [blame] | 2405 | } |
| 2406 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2407 | static int check_module_license_and_versions(struct module *mod) |
Rusty Russell | 22e268e | 2010-08-05 12:59:05 -0600 | [diff] [blame] | 2408 | { |
| 2409 | /* |
| 2410 | * ndiswrapper is under GPL by itself, but loads proprietary modules. |
| 2411 | * Don't use add_taint_module(), as it would prevent ndiswrapper from |
| 2412 | * using GPL-only symbols it needs. |
| 2413 | */ |
| 2414 | if (strcmp(mod->name, "ndiswrapper") == 0) |
| 2415 | add_taint(TAINT_PROPRIETARY_MODULE); |
| 2416 | |
| 2417 | /* driverloader was caught wrongly pretending to be under GPL */ |
| 2418 | if (strcmp(mod->name, "driverloader") == 0) |
| 2419 | add_taint_module(mod, TAINT_PROPRIETARY_MODULE); |
| 2420 | |
| 2421 | #ifdef CONFIG_MODVERSIONS |
| 2422 | if ((mod->num_syms && !mod->crcs) |
| 2423 | || (mod->num_gpl_syms && !mod->gpl_crcs) |
| 2424 | || (mod->num_gpl_future_syms && !mod->gpl_future_crcs) |
| 2425 | #ifdef CONFIG_UNUSED_SYMBOLS |
| 2426 | || (mod->num_unused_syms && !mod->unused_crcs) |
| 2427 | || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs) |
| 2428 | #endif |
| 2429 | ) { |
| 2430 | return try_to_force_load(mod, |
| 2431 | "no versions for exported symbols"); |
| 2432 | } |
| 2433 | #endif |
| 2434 | return 0; |
| 2435 | } |
| 2436 | |
| 2437 | static void flush_module_icache(const struct module *mod) |
| 2438 | { |
| 2439 | mm_segment_t old_fs; |
| 2440 | |
| 2441 | /* flush the icache in correct context */ |
| 2442 | old_fs = get_fs(); |
| 2443 | set_fs(KERNEL_DS); |
| 2444 | |
| 2445 | /* |
| 2446 | * Flush the instruction cache, since we've played with text. |
| 2447 | * Do it before processing of module parameters, so the module |
| 2448 | * can provide parameter accessor functions of its own. |
| 2449 | */ |
| 2450 | if (mod->module_init) |
| 2451 | flush_icache_range((unsigned long)mod->module_init, |
| 2452 | (unsigned long)mod->module_init |
| 2453 | + mod->init_size); |
| 2454 | flush_icache_range((unsigned long)mod->module_core, |
| 2455 | (unsigned long)mod->module_core + mod->core_size); |
| 2456 | |
| 2457 | set_fs(old_fs); |
| 2458 | } |
| 2459 | |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2460 | static struct module *layout_and_allocate(struct load_info *info) |
| 2461 | { |
| 2462 | /* Module within temporary copy. */ |
| 2463 | struct module *mod; |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2464 | Elf_Shdr *pcpusec; |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2465 | int err; |
| 2466 | |
| 2467 | mod = setup_load_info(info); |
| 2468 | if (IS_ERR(mod)) |
| 2469 | return mod; |
| 2470 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2471 | err = check_modinfo(mod, info); |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2472 | if (err) |
| 2473 | return ERR_PTR(err); |
| 2474 | |
| 2475 | /* Allow arches to frob section contents and sizes. */ |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2476 | err = module_frob_arch_sections(info->hdr, info->sechdrs, |
| 2477 | info->secstrings, mod); |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2478 | if (err < 0) |
Rusty Russell | 6526c53 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2479 | goto out; |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2480 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2481 | pcpusec = &info->sechdrs[info->index.pcpu]; |
| 2482 | if (pcpusec->sh_size) { |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2483 | /* We have a special allocation for this section. */ |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2484 | err = percpu_modalloc(mod, |
| 2485 | pcpusec->sh_size, pcpusec->sh_addralign); |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2486 | if (err) |
Rusty Russell | 6526c53 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2487 | goto out; |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2488 | pcpusec->sh_flags &= ~(unsigned long)SHF_ALLOC; |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2489 | } |
| 2490 | |
| 2491 | /* Determine total sizes, and put offsets in sh_entsize. For now |
| 2492 | this is done generically; there doesn't appear to be any |
| 2493 | special cases for the architectures. */ |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2494 | layout_sections(mod, info); |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2495 | |
| 2496 | info->strmap = kzalloc(BITS_TO_LONGS(info->sechdrs[info->index.str].sh_size) |
| 2497 | * sizeof(long), GFP_KERNEL); |
| 2498 | if (!info->strmap) { |
| 2499 | err = -ENOMEM; |
| 2500 | goto free_percpu; |
| 2501 | } |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2502 | layout_symtab(mod, info); |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2503 | |
| 2504 | /* Allocate and move to the final place */ |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2505 | err = move_module(mod, info); |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2506 | if (err) |
| 2507 | goto free_strmap; |
| 2508 | |
| 2509 | /* Module has been copied to its final place now: return it. */ |
| 2510 | mod = (void *)info->sechdrs[info->index.mod].sh_addr; |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2511 | kmemleak_load_module(mod, info); |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2512 | return mod; |
| 2513 | |
| 2514 | free_strmap: |
| 2515 | kfree(info->strmap); |
| 2516 | free_percpu: |
| 2517 | percpu_modfree(mod); |
Rusty Russell | 6526c53 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2518 | out: |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2519 | return ERR_PTR(err); |
| 2520 | } |
| 2521 | |
| 2522 | /* mod is no longer valid after this! */ |
| 2523 | static void module_deallocate(struct module *mod, struct load_info *info) |
| 2524 | { |
| 2525 | kfree(info->strmap); |
| 2526 | percpu_modfree(mod); |
| 2527 | module_free(mod, mod->module_init); |
| 2528 | module_free(mod, mod->module_core); |
| 2529 | } |
| 2530 | |
Rusty Russell | 811d66a | 2010-08-05 12:59:12 -0600 | [diff] [blame] | 2531 | static int post_relocation(struct module *mod, const struct load_info *info) |
| 2532 | { |
Rusty Russell | 51f3d0f | 2010-08-05 12:59:13 -0600 | [diff] [blame] | 2533 | /* Sort exception table now relocations are done. */ |
Rusty Russell | 811d66a | 2010-08-05 12:59:12 -0600 | [diff] [blame] | 2534 | sort_extable(mod->extable, mod->extable + mod->num_exentries); |
| 2535 | |
| 2536 | /* Copy relocated percpu area over. */ |
| 2537 | percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr, |
| 2538 | info->sechdrs[info->index.pcpu].sh_size); |
| 2539 | |
Rusty Russell | 51f3d0f | 2010-08-05 12:59:13 -0600 | [diff] [blame] | 2540 | /* Setup kallsyms-specific fields. */ |
Rusty Russell | 811d66a | 2010-08-05 12:59:12 -0600 | [diff] [blame] | 2541 | add_kallsyms(mod, info); |
| 2542 | |
| 2543 | /* Arch-specific module finalizing. */ |
| 2544 | return module_finalize(info->hdr, info->sechdrs, mod); |
| 2545 | } |
| 2546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2547 | /* Allocate and load the module: note that size of section 0 is always |
| 2548 | zero, and we rely on this for optional sections. */ |
Rusty Russell | 51f3d0f | 2010-08-05 12:59:13 -0600 | [diff] [blame] | 2549 | static struct module *load_module(void __user *umod, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2550 | unsigned long len, |
| 2551 | const char __user *uargs) |
| 2552 | { |
Linus Torvalds | 3264d3f | 2010-06-02 11:01:06 -0700 | [diff] [blame] | 2553 | struct load_info info = { NULL, }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2554 | struct module *mod; |
Rusty Russell | 40dd256 | 2010-08-05 12:59:03 -0600 | [diff] [blame] | 2555 | long err; |
Paul Mundt | 3ae91c2 | 2009-10-01 15:43:54 -0700 | [diff] [blame] | 2556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2557 | DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n", |
| 2558 | umod, len, uargs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2559 | |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2560 | /* Copy in the blobs from userspace, check they are vaguely sane. */ |
| 2561 | err = copy_and_check(&info, umod, len, uargs); |
Rusty Russell | 40dd256 | 2010-08-05 12:59:03 -0600 | [diff] [blame] | 2562 | if (err) |
| 2563 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2564 | |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2565 | /* Figure out module layout, and allocate all the memory. */ |
| 2566 | mod = layout_and_allocate(&info); |
Linus Torvalds | 3264d3f | 2010-06-02 11:01:06 -0700 | [diff] [blame] | 2567 | if (IS_ERR(mod)) { |
| 2568 | err = PTR_ERR(mod); |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2569 | goto free_copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2570 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2571 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2572 | /* Now module is in final location, initialize linked lists, etc. */ |
Rusty Russell | 9f85a4b | 2010-08-05 12:59:04 -0600 | [diff] [blame] | 2573 | err = module_unload_init(mod); |
| 2574 | if (err) |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2575 | goto free_module; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2576 | |
Rusty Russell | 22e268e | 2010-08-05 12:59:05 -0600 | [diff] [blame] | 2577 | /* Now we've got everything in the final locations, we can |
| 2578 | * find optional sections. */ |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2579 | find_module_sections(mod, &info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2580 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2581 | err = check_module_license_and_versions(mod); |
Rusty Russell | 22e268e | 2010-08-05 12:59:05 -0600 | [diff] [blame] | 2582 | if (err) |
| 2583 | goto free_unload; |
Dave Jones | 9841d61 | 2006-01-08 01:03:41 -0800 | [diff] [blame] | 2584 | |
Matt Domsch | c988d2b | 2005-06-23 22:05:15 -0700 | [diff] [blame] | 2585 | /* Set up MODINFO_ATTR fields */ |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2586 | setup_modinfo(mod, &info); |
Matt Domsch | c988d2b | 2005-06-23 22:05:15 -0700 | [diff] [blame] | 2587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2588 | /* Fix up syms, so that st_value is a pointer to location. */ |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2589 | err = simplify_symbols(mod, &info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2590 | if (err < 0) |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2591 | goto free_modinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2592 | |
Rusty Russell | 4966868 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2593 | err = apply_relocations(mod, &info); |
Rusty Russell | 22e268e | 2010-08-05 12:59:05 -0600 | [diff] [blame] | 2594 | if (err < 0) |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2595 | goto free_modinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2596 | |
Rusty Russell | 811d66a | 2010-08-05 12:59:12 -0600 | [diff] [blame] | 2597 | err = post_relocation(mod, &info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | if (err < 0) |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2599 | goto free_modinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2600 | |
Rusty Russell | 22e268e | 2010-08-05 12:59:05 -0600 | [diff] [blame] | 2601 | flush_module_icache(mod); |
Thomas Koeller | 378bac8 | 2005-09-06 15:17:11 -0700 | [diff] [blame] | 2602 | |
Rusty Russell | 6526c53 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2603 | /* Now copy in args */ |
| 2604 | mod->args = strndup_user(uargs, ~0UL >> 1); |
| 2605 | if (IS_ERR(mod->args)) { |
| 2606 | err = PTR_ERR(mod->args); |
| 2607 | goto free_arch_cleanup; |
| 2608 | } |
Rusty Russell | 8d3b33f | 2006-03-25 03:07:05 -0800 | [diff] [blame] | 2609 | |
Rusty Russell | 51f3d0f | 2010-08-05 12:59:13 -0600 | [diff] [blame] | 2610 | /* Mark state as coming so strong_try_module_get() ignores us. */ |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2611 | mod->state = MODULE_STATE_COMING; |
| 2612 | |
Rusty Russell | bb9d3d5 | 2008-01-29 17:13:21 -0500 | [diff] [blame] | 2613 | /* Now sew it into the lists so we can get lockdep and oops |
Andi Kleen | d72b375 | 2008-08-30 10:09:00 +0200 | [diff] [blame] | 2614 | * info during argument parsing. Noone should access us, since |
| 2615 | * strong_try_module_get() will fail. |
| 2616 | * lockdep/oops can run asynchronous, so use the RCU list insertion |
| 2617 | * function to insert in a way safe to concurrent readers. |
| 2618 | * The mutex protects against concurrent writers. |
| 2619 | */ |
Rusty Russell | 7567650 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 2620 | mutex_lock(&module_mutex); |
Linus Torvalds | 3bafeb6 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 2621 | if (find_module(mod->name)) { |
| 2622 | err = -EEXIST; |
Rusty Russell | be593f4 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 2623 | goto unlock; |
Linus Torvalds | 3bafeb6 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 2624 | } |
| 2625 | |
Rusty Russell | 811d66a | 2010-08-05 12:59:12 -0600 | [diff] [blame] | 2626 | /* This has to be done once we're sure module name is unique. */ |
| 2627 | if (!mod->taints) |
| 2628 | dynamic_debug_setup(info.debug, info.num_debug); |
Yehuda Sadeh | ff49d74 | 2010-07-03 13:07:35 +1000 | [diff] [blame] | 2629 | |
Rusty Russell | be593f4 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 2630 | /* Find duplicate symbols */ |
| 2631 | err = verify_export_symbols(mod); |
| 2632 | if (err < 0) |
Yehuda Sadeh | ff49d74 | 2010-07-03 13:07:35 +1000 | [diff] [blame] | 2633 | goto ddebug; |
Rusty Russell | be593f4 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 2634 | |
Linus Torvalds | 5336377 | 2010-10-05 11:29:27 -0700 | [diff] [blame] | 2635 | module_bug_finalize(info.hdr, info.sechdrs, mod); |
Andi Kleen | d72b375 | 2008-08-30 10:09:00 +0200 | [diff] [blame] | 2636 | list_add_rcu(&mod->list, &modules); |
Rusty Russell | 7567650 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 2637 | mutex_unlock(&module_mutex); |
Rusty Russell | bb9d3d5 | 2008-01-29 17:13:21 -0500 | [diff] [blame] | 2638 | |
Rusty Russell | 51f3d0f | 2010-08-05 12:59:13 -0600 | [diff] [blame] | 2639 | /* Module is ready to execute: parsing args may do that. */ |
Rusty Russell | e180a6b | 2009-03-31 13:05:29 -0600 | [diff] [blame] | 2640 | err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2641 | if (err < 0) |
Rusty Russell | bb9d3d5 | 2008-01-29 17:13:21 -0500 | [diff] [blame] | 2642 | goto unlink; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2643 | |
Rusty Russell | 51f3d0f | 2010-08-05 12:59:13 -0600 | [diff] [blame] | 2644 | /* Link in to syfs. */ |
Rusty Russell | 8f6d037 | 2010-08-05 12:59:09 -0600 | [diff] [blame] | 2645 | err = mod_sysfs_setup(mod, &info, mod->kp, mod->num_kp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2646 | if (err < 0) |
Rusty Russell | bb9d3d5 | 2008-01-29 17:13:21 -0500 | [diff] [blame] | 2647 | goto unlink; |
Rusty Russell | 80a3d1b | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 2648 | |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2649 | /* Get rid of temporary copy and strmap. */ |
| 2650 | kfree(info.strmap); |
| 2651 | free_copy(&info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2652 | |
| 2653 | /* Done! */ |
Rusty Russell | 51f3d0f | 2010-08-05 12:59:13 -0600 | [diff] [blame] | 2654 | trace_module_load(mod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2655 | return mod; |
| 2656 | |
Rusty Russell | bb9d3d5 | 2008-01-29 17:13:21 -0500 | [diff] [blame] | 2657 | unlink: |
Rusty Russell | 7567650 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 2658 | mutex_lock(&module_mutex); |
Rusty Russell | e91defa | 2009-03-31 13:05:35 -0600 | [diff] [blame] | 2659 | /* Unlink carefully: kallsyms could be walking list. */ |
| 2660 | list_del_rcu(&mod->list); |
Linus Torvalds | 5336377 | 2010-10-05 11:29:27 -0700 | [diff] [blame] | 2661 | module_bug_cleanup(mod); |
| 2662 | |
Yehuda Sadeh | ff49d74 | 2010-07-03 13:07:35 +1000 | [diff] [blame] | 2663 | ddebug: |
Rusty Russell | 811d66a | 2010-08-05 12:59:12 -0600 | [diff] [blame] | 2664 | if (!mod->taints) |
| 2665 | dynamic_debug_remove(info.debug); |
Rusty Russell | be593f4 | 2010-06-05 11:17:37 -0600 | [diff] [blame] | 2666 | unlock: |
Rusty Russell | 7567650 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 2667 | mutex_unlock(&module_mutex); |
Rusty Russell | e91defa | 2009-03-31 13:05:35 -0600 | [diff] [blame] | 2668 | synchronize_sched(); |
Rusty Russell | 6526c53 | 2010-08-05 12:59:10 -0600 | [diff] [blame] | 2669 | kfree(mod->args); |
| 2670 | free_arch_cleanup: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2671 | module_arch_cleanup(mod); |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2672 | free_modinfo: |
Rusty Russell | a263f776 | 2009-09-25 00:32:58 -0600 | [diff] [blame] | 2673 | free_modinfo(mod); |
Rusty Russell | 22e268e | 2010-08-05 12:59:05 -0600 | [diff] [blame] | 2674 | free_unload: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2675 | module_unload_free(mod); |
Rusty Russell | d913188 | 2010-08-05 12:59:08 -0600 | [diff] [blame] | 2676 | free_module: |
| 2677 | module_deallocate(mod, &info); |
| 2678 | free_copy: |
| 2679 | free_copy(&info); |
Jayachandran C | 6fe2e70 | 2006-01-06 00:19:54 -0800 | [diff] [blame] | 2680 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2681 | } |
| 2682 | |
Peter Oberparleiter | b99b87f | 2009-06-17 16:28:03 -0700 | [diff] [blame] | 2683 | /* Call module constructors. */ |
| 2684 | static void do_mod_ctors(struct module *mod) |
| 2685 | { |
| 2686 | #ifdef CONFIG_CONSTRUCTORS |
| 2687 | unsigned long i; |
| 2688 | |
| 2689 | for (i = 0; i < mod->num_ctors; i++) |
| 2690 | mod->ctors[i](); |
| 2691 | #endif |
| 2692 | } |
| 2693 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2694 | /* This is where the real work happens */ |
Heiko Carstens | 17da2bd | 2009-01-14 14:14:10 +0100 | [diff] [blame] | 2695 | SYSCALL_DEFINE3(init_module, void __user *, umod, |
| 2696 | unsigned long, len, const char __user *, uargs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2697 | { |
| 2698 | struct module *mod; |
| 2699 | int ret = 0; |
| 2700 | |
| 2701 | /* Must have permission */ |
Kees Cook | 3d43321 | 2009-04-02 15:49:29 -0700 | [diff] [blame] | 2702 | if (!capable(CAP_SYS_MODULE) || modules_disabled) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2703 | return -EPERM; |
| 2704 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2705 | /* Do all the hard work */ |
| 2706 | mod = load_module(umod, len, uargs); |
Rusty Russell | 7567650 | 2010-06-05 11:17:36 -0600 | [diff] [blame] | 2707 | if (IS_ERR(mod)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2708 | return PTR_ERR(mod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2709 | |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 2710 | blocking_notifier_call_chain(&module_notify_list, |
| 2711 | MODULE_STATE_COMING, mod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2712 | |
Peter Oberparleiter | b99b87f | 2009-06-17 16:28:03 -0700 | [diff] [blame] | 2713 | do_mod_ctors(mod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2714 | /* Start the module */ |
| 2715 | if (mod->init != NULL) |
Arjan van de Ven | 59f9415 | 2008-07-30 12:49:02 -0700 | [diff] [blame] | 2716 | ret = do_one_initcall(mod->init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2717 | if (ret < 0) { |
| 2718 | /* Init routine failed: abort. Try to protect us from |
| 2719 | buggy refcounters. */ |
| 2720 | mod->state = MODULE_STATE_GOING; |
Paul E. McKenney | fbd568a3e | 2005-05-01 08:59:04 -0700 | [diff] [blame] | 2721 | synchronize_sched(); |
Rusty Russell | af49d92 | 2007-10-16 23:26:27 -0700 | [diff] [blame] | 2722 | module_put(mod); |
Peter Oberparleiter | df4b565 | 2008-04-21 14:34:31 +0200 | [diff] [blame] | 2723 | blocking_notifier_call_chain(&module_notify_list, |
| 2724 | MODULE_STATE_GOING, mod); |
Rusty Russell | af49d92 | 2007-10-16 23:26:27 -0700 | [diff] [blame] | 2725 | free_module(mod); |
Rusty Russell | c9a3ba5 | 2008-01-29 17:13:18 -0500 | [diff] [blame] | 2726 | wake_up(&module_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2727 | return ret; |
| 2728 | } |
Alexey Dobriyan | e24e2e6 | 2008-03-10 11:43:53 -0700 | [diff] [blame] | 2729 | if (ret > 0) { |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2730 | printk(KERN_WARNING |
| 2731 | "%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n" |
| 2732 | "%s: loading module anyway...\n", |
Alexey Dobriyan | e24e2e6 | 2008-03-10 11:43:53 -0700 | [diff] [blame] | 2733 | __func__, mod->name, ret, |
| 2734 | __func__); |
| 2735 | dump_stack(); |
| 2736 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2737 | |
Rusty Russell | 6c5db22 | 2008-03-10 11:43:52 -0700 | [diff] [blame] | 2738 | /* Now it's a first class citizen! Wake up anyone waiting for it. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2739 | mod->state = MODULE_STATE_LIVE; |
Rusty Russell | 6c5db22 | 2008-03-10 11:43:52 -0700 | [diff] [blame] | 2740 | wake_up(&module_wq); |
Masami Hiramatsu | 0deddf4 | 2009-01-06 14:41:54 -0800 | [diff] [blame] | 2741 | blocking_notifier_call_chain(&module_notify_list, |
| 2742 | MODULE_STATE_LIVE, mod); |
Rusty Russell | 6c5db22 | 2008-03-10 11:43:52 -0700 | [diff] [blame] | 2743 | |
Linus Torvalds | d6de2c8 | 2009-04-10 12:17:41 -0700 | [diff] [blame] | 2744 | /* We need to finish all async code before the module init sequence is done */ |
| 2745 | async_synchronize_full(); |
| 2746 | |
Rusty Russell | 6c5db22 | 2008-03-10 11:43:52 -0700 | [diff] [blame] | 2747 | mutex_lock(&module_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2748 | /* Drop initial reference. */ |
| 2749 | module_put(mod); |
Rusty Russell | ad6561d | 2009-06-12 21:47:03 -0600 | [diff] [blame] | 2750 | trim_init_extable(mod); |
Jan Beulich | 4a49622 | 2009-07-06 14:50:42 +0100 | [diff] [blame] | 2751 | #ifdef CONFIG_KALLSYMS |
| 2752 | mod->num_symtab = mod->core_num_syms; |
| 2753 | mod->symtab = mod->core_symtab; |
Jan Beulich | 554bdfe | 2009-07-06 14:51:44 +0100 | [diff] [blame] | 2754 | mod->strtab = mod->core_strtab; |
Jan Beulich | 4a49622 | 2009-07-06 14:50:42 +0100 | [diff] [blame] | 2755 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2756 | module_free(mod, mod->module_init); |
| 2757 | mod->module_init = NULL; |
| 2758 | mod->init_size = 0; |
| 2759 | mod->init_text_size = 0; |
Ashutosh Naik | 6389a38 | 2006-03-23 03:00:46 -0800 | [diff] [blame] | 2760 | mutex_unlock(&module_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2761 | |
| 2762 | return 0; |
| 2763 | } |
| 2764 | |
| 2765 | static inline int within(unsigned long addr, void *start, unsigned long size) |
| 2766 | { |
| 2767 | return ((void *)addr >= start && (void *)addr < start + size); |
| 2768 | } |
| 2769 | |
| 2770 | #ifdef CONFIG_KALLSYMS |
| 2771 | /* |
| 2772 | * This ignores the intensely annoying "mapping symbols" found |
| 2773 | * in ARM ELF files: $a, $t and $d. |
| 2774 | */ |
| 2775 | static inline int is_arm_mapping_symbol(const char *str) |
| 2776 | { |
Daniel Walker | 22a8bde | 2007-10-18 03:06:07 -0700 | [diff] [blame] | 2777 | return str[0] == '$' && strchr("atd", str[1]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2778 | && (str[2] == '\0' || str[2] == '.'); |
| 2779 | } |
| 2780 | |
| 2781 | static const char *get_ksymbol(struct module *mod, |
| 2782 | unsigned long addr, |
| 2783 | unsigned long *size, |
| 2784 | unsigned long *offset) |
| 2785 | { |
| 2786 | unsigned int i, best = 0; |
| 2787 | unsigned long nextval; |
| 2788 | |
| 2789 | /* At worse, next value is at end of module */ |
Masami Hiramatsu | a06f621 | 2009-01-06 14:41:49 -0800 | [diff] [blame] | 2790 | if (within_module_init(addr, mod)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2791 | nextval = (unsigned long)mod->module_init+mod->init_text_size; |
Daniel Walker | 22a8bde | 2007-10-18 03:06:07 -0700 | [diff] [blame] | 2792 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2793 | nextval = (unsigned long)mod->module_core+mod->core_text_size; |
| 2794 | |
| 2795 | /* Scan for closest preceeding symbol, and next symbol. (ELF |
Daniel Walker | 22a8bde | 2007-10-18 03:06:07 -0700 | [diff] [blame] | 2796 | starts real symbols at 1). */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2797 | for (i = 1; i < mod->num_symtab; i++) { |
| 2798 | if (mod->symtab[i].st_shndx == SHN_UNDEF) |
| 2799 | continue; |
| 2800 | |
| 2801 | /* We ignore unnamed symbols: they're uninformative |
| 2802 | * and inserted at a whim. */ |
| 2803 | if (mod->symtab[i].st_value <= addr |
| 2804 | && mod->symtab[i].st_value > mod->symtab[best].st_value |
| 2805 | && *(mod->strtab + mod->symtab[i].st_name) != '\0' |
| 2806 | && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name)) |
| 2807 | best = i; |
| 2808 | if (mod->symtab[i].st_value > addr |
| 2809 | && mod->symtab[i].st_value < nextval |
| 2810 | && *(mod->strtab + mod->symtab[i].st_name) != '\0' |
| 2811 | && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name)) |
| 2812 | nextval = mod->symtab[i].st_value; |
| 2813 | } |
| 2814 | |
| 2815 | if (!best) |
| 2816 | return NULL; |
| 2817 | |
Alexey Dobriyan | ffb4512 | 2007-05-08 00:28:41 -0700 | [diff] [blame] | 2818 | if (size) |
| 2819 | *size = nextval - mod->symtab[best].st_value; |
| 2820 | if (offset) |
| 2821 | *offset = addr - mod->symtab[best].st_value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2822 | return mod->strtab + mod->symtab[best].st_name; |
| 2823 | } |
| 2824 | |
Rusty Russell | 6dd06c9 | 2008-01-29 17:13:22 -0500 | [diff] [blame] | 2825 | /* For kallsyms to ask for address resolution. NULL means not found. Careful |
| 2826 | * not to lock to avoid deadlock on oopses, simply disable preemption. */ |
Andrew Morton | 92dfc9d | 2008-02-08 04:18:43 -0800 | [diff] [blame] | 2827 | const char *module_address_lookup(unsigned long addr, |
Rusty Russell | 6dd06c9 | 2008-01-29 17:13:22 -0500 | [diff] [blame] | 2828 | unsigned long *size, |
| 2829 | unsigned long *offset, |
| 2830 | char **modname, |
| 2831 | char *namebuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2832 | { |
| 2833 | struct module *mod; |
Rusty Russell | cb2a520 | 2008-01-14 00:55:03 -0800 | [diff] [blame] | 2834 | const char *ret = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2835 | |
Rusty Russell | cb2a520 | 2008-01-14 00:55:03 -0800 | [diff] [blame] | 2836 | preempt_disable(); |
Andi Kleen | d72b375 | 2008-08-30 10:09:00 +0200 | [diff] [blame] | 2837 | list_for_each_entry_rcu(mod, &modules, list) { |
Masami Hiramatsu | a06f621 | 2009-01-06 14:41:49 -0800 | [diff] [blame] | 2838 | if (within_module_init(addr, mod) || |
| 2839 | within_module_core(addr, mod)) { |
Franck Bui-Huu | ffc5089 | 2006-10-03 01:13:48 -0700 | [diff] [blame] | 2840 | if (modname) |
| 2841 | *modname = mod->name; |
Rusty Russell | cb2a520 | 2008-01-14 00:55:03 -0800 | [diff] [blame] | 2842 | ret = get_ksymbol(mod, addr, size, offset); |
| 2843 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2844 | } |
| 2845 | } |
Rusty Russell | 6dd06c9 | 2008-01-29 17:13:22 -0500 | [diff] [blame] | 2846 | /* Make a copy in here where it's safe */ |
| 2847 | if (ret) { |
| 2848 | strncpy(namebuf, ret, KSYM_NAME_LEN - 1); |
| 2849 | ret = namebuf; |
| 2850 | } |
Rusty Russell | cb2a520 | 2008-01-14 00:55:03 -0800 | [diff] [blame] | 2851 | preempt_enable(); |
Andrew Morton | 92dfc9d | 2008-02-08 04:18:43 -0800 | [diff] [blame] | 2852 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2853 | } |
| 2854 | |
Alexey Dobriyan | 9d65cb4 | 2007-05-08 00:28:43 -0700 | [diff] [blame] | 2855 | int lookup_module_symbol_name(unsigned long addr, char *symname) |
| 2856 | { |
| 2857 | struct module *mod; |
| 2858 | |
Rusty Russell | cb2a520 | 2008-01-14 00:55:03 -0800 | [diff] [blame] | 2859 | preempt_disable(); |
Andi Kleen | d72b375 | 2008-08-30 10:09:00 +0200 | [diff] [blame] | 2860 | list_for_each_entry_rcu(mod, &modules, list) { |
Masami Hiramatsu | a06f621 | 2009-01-06 14:41:49 -0800 | [diff] [blame] | 2861 | if (within_module_init(addr, mod) || |
| 2862 | within_module_core(addr, mod)) { |
Alexey Dobriyan | 9d65cb4 | 2007-05-08 00:28:43 -0700 | [diff] [blame] | 2863 | const char *sym; |
| 2864 | |
| 2865 | sym = get_ksymbol(mod, addr, NULL, NULL); |
| 2866 | if (!sym) |
| 2867 | goto out; |
Tejun Heo | 9281ace | 2007-07-17 04:03:51 -0700 | [diff] [blame] | 2868 | strlcpy(symname, sym, KSYM_NAME_LEN); |
Rusty Russell | cb2a520 | 2008-01-14 00:55:03 -0800 | [diff] [blame] | 2869 | preempt_enable(); |
Alexey Dobriyan | 9d65cb4 | 2007-05-08 00:28:43 -0700 | [diff] [blame] | 2870 | return 0; |
| 2871 | } |
| 2872 | } |
| 2873 | out: |
Rusty Russell | cb2a520 | 2008-01-14 00:55:03 -0800 | [diff] [blame] | 2874 | preempt_enable(); |
Alexey Dobriyan | 9d65cb4 | 2007-05-08 00:28:43 -0700 | [diff] [blame] | 2875 | return -ERANGE; |
| 2876 | } |
| 2877 | |
Alexey Dobriyan | a5c43da | 2007-05-08 00:28:47 -0700 | [diff] [blame] | 2878 | int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, |
| 2879 | unsigned long *offset, char *modname, char *name) |
| 2880 | { |
| 2881 | struct module *mod; |
| 2882 | |
Rusty Russell | cb2a520 | 2008-01-14 00:55:03 -0800 | [diff] [blame] | 2883 | preempt_disable(); |
Andi Kleen | d72b375 | 2008-08-30 10:09:00 +0200 | [diff] [blame] | 2884 | list_for_each_entry_rcu(mod, &modules, list) { |
Masami Hiramatsu | a06f621 | 2009-01-06 14:41:49 -0800 | [diff] [blame] | 2885 | if (within_module_init(addr, mod) || |
| 2886 | within_module_core(addr, mod)) { |
Alexey Dobriyan | a5c43da | 2007-05-08 00:28:47 -0700 | [diff] [blame] | 2887 | const char *sym; |
| 2888 | |
| 2889 | sym = get_ksymbol(mod, addr, size, offset); |
| 2890 | if (!sym) |
| 2891 | goto out; |
| 2892 | if (modname) |
Tejun Heo | 9281ace | 2007-07-17 04:03:51 -0700 | [diff] [blame] | 2893 | strlcpy(modname, mod->name, MODULE_NAME_LEN); |
Alexey Dobriyan | a5c43da | 2007-05-08 00:28:47 -0700 | [diff] [blame] | 2894 | if (name) |
Tejun Heo | 9281ace | 2007-07-17 04:03:51 -0700 | [diff] [blame] | 2895 | strlcpy(name, sym, KSYM_NAME_LEN); |
Rusty Russell | cb2a520 | 2008-01-14 00:55:03 -0800 | [diff] [blame] | 2896 | preempt_enable(); |
Alexey Dobriyan | a5c43da | 2007-05-08 00:28:47 -0700 | [diff] [blame] | 2897 | return 0; |
| 2898 | } |
| 2899 | } |
| 2900 | out: |
Rusty Russell | cb2a520 | 2008-01-14 00:55:03 -0800 | [diff] [blame] | 2901 | preempt_enable(); |
Alexey Dobriyan | a5c43da | 2007-05-08 00:28:47 -0700 | [diff] [blame] | 2902 | return -ERANGE; |
| 2903 | } |
| 2904 | |
Alexey Dobriyan | ea07890 | 2007-05-08 00:28:39 -0700 | [diff] [blame] | 2905 | int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type, |
| 2906 | char *name, char *module_name, int *exported) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2907 | { |
| 2908 | struct module *mod; |
| 2909 | |
Rusty Russell | cb2a520 | 2008-01-14 00:55:03 -0800 | [diff] [blame] | 2910 | preempt_disable(); |
Andi Kleen | d72b375 | 2008-08-30 10:09:00 +0200 | [diff] [blame] | 2911 | list_for_each_entry_rcu(mod, &modules, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2912 | if (symnum < mod->num_symtab) { |
| 2913 | *value = mod->symtab[symnum].st_value; |
| 2914 | *type = mod->symtab[symnum].st_info; |
Andreas Gruenbacher | 098c5ee | 2006-07-14 00:24:04 -0700 | [diff] [blame] | 2915 | strlcpy(name, mod->strtab + mod->symtab[symnum].st_name, |
Tejun Heo | 9281ace | 2007-07-17 04:03:51 -0700 | [diff] [blame] | 2916 | KSYM_NAME_LEN); |
| 2917 | strlcpy(module_name, mod->name, MODULE_NAME_LEN); |
Tim Abbott | ca4787b | 2009-01-05 08:40:10 -0600 | [diff] [blame] | 2918 | *exported = is_exported(name, *value, mod); |
Rusty Russell | cb2a520 | 2008-01-14 00:55:03 -0800 | [diff] [blame] | 2919 | preempt_enable(); |
Alexey Dobriyan | ea07890 | 2007-05-08 00:28:39 -0700 | [diff] [blame] | 2920 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2921 | } |
| 2922 | symnum -= mod->num_symtab; |
| 2923 | } |
Rusty Russell | cb2a520 | 2008-01-14 00:55:03 -0800 | [diff] [blame] | 2924 | preempt_enable(); |
Alexey Dobriyan | ea07890 | 2007-05-08 00:28:39 -0700 | [diff] [blame] | 2925 | return -ERANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2926 | } |
| 2927 | |
| 2928 | static unsigned long mod_find_symname(struct module *mod, const char *name) |
| 2929 | { |
| 2930 | unsigned int i; |
| 2931 | |
| 2932 | for (i = 0; i < mod->num_symtab; i++) |
Keith Owens | 54e8ce4 | 2006-02-03 03:03:53 -0800 | [diff] [blame] | 2933 | if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 && |
| 2934 | mod->symtab[i].st_info != 'U') |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2935 | return mod->symtab[i].st_value; |
| 2936 | return 0; |
| 2937 | } |
| 2938 | |
| 2939 | /* Look for this name: can be of form module:name. */ |
| 2940 | unsigned long module_kallsyms_lookup_name(const char *name) |
| 2941 | { |
| 2942 | struct module *mod; |
| 2943 | char *colon; |
| 2944 | unsigned long ret = 0; |
| 2945 | |
| 2946 | /* Don't lock: we're in enough trouble already. */ |
Rusty Russell | cb2a520 | 2008-01-14 00:55:03 -0800 | [diff] [blame] | 2947 | preempt_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2948 | if ((colon = strchr(name, ':')) != NULL) { |
| 2949 | *colon = '\0'; |
| 2950 | if ((mod = find_module(name)) != NULL) |
| 2951 | ret = mod_find_symname(mod, colon+1); |
| 2952 | *colon = ':'; |
| 2953 | } else { |
Andi Kleen | d72b375 | 2008-08-30 10:09:00 +0200 | [diff] [blame] | 2954 | list_for_each_entry_rcu(mod, &modules, list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2955 | if ((ret = mod_find_symname(mod, name)) != 0) |
| 2956 | break; |
| 2957 | } |
Rusty Russell | cb2a520 | 2008-01-14 00:55:03 -0800 | [diff] [blame] | 2958 | preempt_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2959 | return ret; |
| 2960 | } |
Anders Kaseorg | 75a6661 | 2008-12-05 19:03:58 -0500 | [diff] [blame] | 2961 | |
| 2962 | int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *, |
| 2963 | struct module *, unsigned long), |
| 2964 | void *data) |
| 2965 | { |
| 2966 | struct module *mod; |
| 2967 | unsigned int i; |
| 2968 | int ret; |
| 2969 | |
| 2970 | list_for_each_entry(mod, &modules, list) { |
| 2971 | for (i = 0; i < mod->num_symtab; i++) { |
| 2972 | ret = fn(data, mod->strtab + mod->symtab[i].st_name, |
| 2973 | mod, mod->symtab[i].st_value); |
| 2974 | if (ret != 0) |
| 2975 | return ret; |
| 2976 | } |
| 2977 | } |
| 2978 | return 0; |
| 2979 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2980 | #endif /* CONFIG_KALLSYMS */ |
| 2981 | |
Arjan van de Ven | 21aa928 | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 2982 | static char *module_flags(struct module *mod, char *buf) |
Florin Malita | fa3ba2e8 | 2006-10-11 01:21:48 -0700 | [diff] [blame] | 2983 | { |
| 2984 | int bx = 0; |
| 2985 | |
Arjan van de Ven | 21aa928 | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 2986 | if (mod->taints || |
| 2987 | mod->state == MODULE_STATE_GOING || |
| 2988 | mod->state == MODULE_STATE_COMING) { |
Florin Malita | fa3ba2e8 | 2006-10-11 01:21:48 -0700 | [diff] [blame] | 2989 | buf[bx++] = '('; |
Andi Kleen | 25ddbb1 | 2008-10-15 22:01:41 -0700 | [diff] [blame] | 2990 | if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE)) |
Florin Malita | fa3ba2e8 | 2006-10-11 01:21:48 -0700 | [diff] [blame] | 2991 | buf[bx++] = 'P'; |
Andi Kleen | 25ddbb1 | 2008-10-15 22:01:41 -0700 | [diff] [blame] | 2992 | if (mod->taints & (1 << TAINT_FORCED_MODULE)) |
Florin Malita | fa3ba2e8 | 2006-10-11 01:21:48 -0700 | [diff] [blame] | 2993 | buf[bx++] = 'F'; |
Linus Torvalds | 26e9a39 | 2008-10-17 09:50:12 -0700 | [diff] [blame] | 2994 | if (mod->taints & (1 << TAINT_CRAP)) |
Greg Kroah-Hartman | 061b1bd | 2008-09-24 14:46:44 -0700 | [diff] [blame] | 2995 | buf[bx++] = 'C'; |
Florin Malita | fa3ba2e8 | 2006-10-11 01:21:48 -0700 | [diff] [blame] | 2996 | /* |
| 2997 | * TAINT_FORCED_RMMOD: could be added. |
| 2998 | * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't |
| 2999 | * apply to modules. |
| 3000 | */ |
Arjan van de Ven | 21aa928 | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 3001 | |
| 3002 | /* Show a - for module-is-being-unloaded */ |
| 3003 | if (mod->state == MODULE_STATE_GOING) |
| 3004 | buf[bx++] = '-'; |
| 3005 | /* Show a + for module-is-being-loaded */ |
| 3006 | if (mod->state == MODULE_STATE_COMING) |
| 3007 | buf[bx++] = '+'; |
Florin Malita | fa3ba2e8 | 2006-10-11 01:21:48 -0700 | [diff] [blame] | 3008 | buf[bx++] = ')'; |
| 3009 | } |
| 3010 | buf[bx] = '\0'; |
| 3011 | |
| 3012 | return buf; |
| 3013 | } |
| 3014 | |
Alexey Dobriyan | 3b5d5c6 | 2008-10-06 13:19:27 +0400 | [diff] [blame] | 3015 | #ifdef CONFIG_PROC_FS |
| 3016 | /* Called by the /proc file system to return a list of modules. */ |
| 3017 | static void *m_start(struct seq_file *m, loff_t *pos) |
| 3018 | { |
| 3019 | mutex_lock(&module_mutex); |
| 3020 | return seq_list_start(&modules, *pos); |
| 3021 | } |
| 3022 | |
| 3023 | static void *m_next(struct seq_file *m, void *p, loff_t *pos) |
| 3024 | { |
| 3025 | return seq_list_next(p, &modules, pos); |
| 3026 | } |
| 3027 | |
| 3028 | static void m_stop(struct seq_file *m, void *p) |
| 3029 | { |
| 3030 | mutex_unlock(&module_mutex); |
| 3031 | } |
| 3032 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3033 | static int m_show(struct seq_file *m, void *p) |
| 3034 | { |
| 3035 | struct module *mod = list_entry(p, struct module, list); |
Florin Malita | fa3ba2e8 | 2006-10-11 01:21:48 -0700 | [diff] [blame] | 3036 | char buf[8]; |
| 3037 | |
Denys Vlasenko | 2f0f2a3 | 2008-07-22 19:24:27 -0500 | [diff] [blame] | 3038 | seq_printf(m, "%s %u", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3039 | mod->name, mod->init_size + mod->core_size); |
| 3040 | print_unload_info(m, mod); |
| 3041 | |
| 3042 | /* Informative for users. */ |
| 3043 | seq_printf(m, " %s", |
| 3044 | mod->state == MODULE_STATE_GOING ? "Unloading": |
| 3045 | mod->state == MODULE_STATE_COMING ? "Loading": |
| 3046 | "Live"); |
| 3047 | /* Used by oprofile and other similar tools. */ |
| 3048 | seq_printf(m, " 0x%p", mod->module_core); |
| 3049 | |
Florin Malita | fa3ba2e8 | 2006-10-11 01:21:48 -0700 | [diff] [blame] | 3050 | /* Taints info */ |
| 3051 | if (mod->taints) |
Arjan van de Ven | 21aa928 | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 3052 | seq_printf(m, " %s", module_flags(mod, buf)); |
Florin Malita | fa3ba2e8 | 2006-10-11 01:21:48 -0700 | [diff] [blame] | 3053 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3054 | seq_printf(m, "\n"); |
| 3055 | return 0; |
| 3056 | } |
| 3057 | |
| 3058 | /* Format: modulename size refcount deps address |
| 3059 | |
| 3060 | Where refcount is a number or -, and deps is a comma-separated list |
| 3061 | of depends or -. |
| 3062 | */ |
Alexey Dobriyan | 3b5d5c6 | 2008-10-06 13:19:27 +0400 | [diff] [blame] | 3063 | static const struct seq_operations modules_op = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3064 | .start = m_start, |
| 3065 | .next = m_next, |
| 3066 | .stop = m_stop, |
| 3067 | .show = m_show |
| 3068 | }; |
| 3069 | |
Alexey Dobriyan | 3b5d5c6 | 2008-10-06 13:19:27 +0400 | [diff] [blame] | 3070 | static int modules_open(struct inode *inode, struct file *file) |
| 3071 | { |
| 3072 | return seq_open(file, &modules_op); |
| 3073 | } |
| 3074 | |
| 3075 | static const struct file_operations proc_modules_operations = { |
| 3076 | .open = modules_open, |
| 3077 | .read = seq_read, |
| 3078 | .llseek = seq_lseek, |
| 3079 | .release = seq_release, |
| 3080 | }; |
| 3081 | |
| 3082 | static int __init proc_modules_init(void) |
| 3083 | { |
| 3084 | proc_create("modules", 0, NULL, &proc_modules_operations); |
| 3085 | return 0; |
| 3086 | } |
| 3087 | module_init(proc_modules_init); |
| 3088 | #endif |
| 3089 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3090 | /* Given an address, look for it in the module exception tables. */ |
| 3091 | const struct exception_table_entry *search_module_extables(unsigned long addr) |
| 3092 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3093 | const struct exception_table_entry *e = NULL; |
| 3094 | struct module *mod; |
| 3095 | |
Rusty Russell | 24da1cb | 2007-07-15 23:41:46 -0700 | [diff] [blame] | 3096 | preempt_disable(); |
Andi Kleen | d72b375 | 2008-08-30 10:09:00 +0200 | [diff] [blame] | 3097 | list_for_each_entry_rcu(mod, &modules, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3098 | if (mod->num_exentries == 0) |
| 3099 | continue; |
Daniel Walker | 22a8bde | 2007-10-18 03:06:07 -0700 | [diff] [blame] | 3100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3101 | e = search_extable(mod->extable, |
| 3102 | mod->extable + mod->num_exentries - 1, |
| 3103 | addr); |
| 3104 | if (e) |
| 3105 | break; |
| 3106 | } |
Rusty Russell | 24da1cb | 2007-07-15 23:41:46 -0700 | [diff] [blame] | 3107 | preempt_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3108 | |
| 3109 | /* Now, if we found one, we are running inside it now, hence |
Daniel Walker | 22a8bde | 2007-10-18 03:06:07 -0700 | [diff] [blame] | 3110 | we cannot unload the module, hence no refcnt needed. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3111 | return e; |
| 3112 | } |
| 3113 | |
Ingo Molnar | 4d435f9 | 2006-07-03 00:24:24 -0700 | [diff] [blame] | 3114 | /* |
Rusty Russell | e610499 | 2009-03-31 13:05:31 -0600 | [diff] [blame] | 3115 | * is_module_address - is this address inside a module? |
| 3116 | * @addr: the address to check. |
| 3117 | * |
| 3118 | * See is_module_text_address() if you simply want to see if the address |
| 3119 | * is code (not data). |
Ingo Molnar | 4d435f9 | 2006-07-03 00:24:24 -0700 | [diff] [blame] | 3120 | */ |
Rusty Russell | e610499 | 2009-03-31 13:05:31 -0600 | [diff] [blame] | 3121 | bool is_module_address(unsigned long addr) |
Ingo Molnar | 4d435f9 | 2006-07-03 00:24:24 -0700 | [diff] [blame] | 3122 | { |
Rusty Russell | e610499 | 2009-03-31 13:05:31 -0600 | [diff] [blame] | 3123 | bool ret; |
Ingo Molnar | 4d435f9 | 2006-07-03 00:24:24 -0700 | [diff] [blame] | 3124 | |
Rusty Russell | 24da1cb | 2007-07-15 23:41:46 -0700 | [diff] [blame] | 3125 | preempt_disable(); |
Rusty Russell | e610499 | 2009-03-31 13:05:31 -0600 | [diff] [blame] | 3126 | ret = __module_address(addr) != NULL; |
Rusty Russell | 24da1cb | 2007-07-15 23:41:46 -0700 | [diff] [blame] | 3127 | preempt_enable(); |
Ingo Molnar | 4d435f9 | 2006-07-03 00:24:24 -0700 | [diff] [blame] | 3128 | |
Rusty Russell | e610499 | 2009-03-31 13:05:31 -0600 | [diff] [blame] | 3129 | return ret; |
Ingo Molnar | 4d435f9 | 2006-07-03 00:24:24 -0700 | [diff] [blame] | 3130 | } |
| 3131 | |
Rusty Russell | e610499 | 2009-03-31 13:05:31 -0600 | [diff] [blame] | 3132 | /* |
| 3133 | * __module_address - get the module which contains an address. |
| 3134 | * @addr: the address. |
| 3135 | * |
| 3136 | * Must be called with preempt disabled or module mutex held so that |
| 3137 | * module doesn't get freed during this. |
| 3138 | */ |
Linus Torvalds | 714f83d | 2009-04-05 11:04:19 -0700 | [diff] [blame] | 3139 | struct module *__module_address(unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3140 | { |
| 3141 | struct module *mod; |
| 3142 | |
Rusty Russell | 3a642e9 | 2008-07-22 19:24:28 -0500 | [diff] [blame] | 3143 | if (addr < module_addr_min || addr > module_addr_max) |
| 3144 | return NULL; |
| 3145 | |
Andi Kleen | d72b375 | 2008-08-30 10:09:00 +0200 | [diff] [blame] | 3146 | list_for_each_entry_rcu(mod, &modules, list) |
Rusty Russell | e610499 | 2009-03-31 13:05:31 -0600 | [diff] [blame] | 3147 | if (within_module_core(addr, mod) |
| 3148 | || within_module_init(addr, mod)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3149 | return mod; |
| 3150 | return NULL; |
| 3151 | } |
Tim Abbott | c6b3780 | 2008-12-05 19:03:59 -0500 | [diff] [blame] | 3152 | EXPORT_SYMBOL_GPL(__module_address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3153 | |
Rusty Russell | e610499 | 2009-03-31 13:05:31 -0600 | [diff] [blame] | 3154 | /* |
| 3155 | * is_module_text_address - is this address inside module code? |
| 3156 | * @addr: the address to check. |
| 3157 | * |
| 3158 | * See is_module_address() if you simply want to see if the address is |
| 3159 | * anywhere in a module. See kernel_text_address() for testing if an |
| 3160 | * address corresponds to kernel or module code. |
| 3161 | */ |
| 3162 | bool is_module_text_address(unsigned long addr) |
| 3163 | { |
| 3164 | bool ret; |
| 3165 | |
| 3166 | preempt_disable(); |
| 3167 | ret = __module_text_address(addr) != NULL; |
| 3168 | preempt_enable(); |
| 3169 | |
| 3170 | return ret; |
| 3171 | } |
| 3172 | |
| 3173 | /* |
| 3174 | * __module_text_address - get the module whose code contains an address. |
| 3175 | * @addr: the address. |
| 3176 | * |
| 3177 | * Must be called with preempt disabled or module mutex held so that |
| 3178 | * module doesn't get freed during this. |
| 3179 | */ |
| 3180 | struct module *__module_text_address(unsigned long addr) |
| 3181 | { |
| 3182 | struct module *mod = __module_address(addr); |
| 3183 | if (mod) { |
| 3184 | /* Make sure it's within the text section. */ |
| 3185 | if (!within(addr, mod->module_init, mod->init_text_size) |
| 3186 | && !within(addr, mod->module_core, mod->core_text_size)) |
| 3187 | mod = NULL; |
| 3188 | } |
| 3189 | return mod; |
| 3190 | } |
Tim Abbott | c6b3780 | 2008-12-05 19:03:59 -0500 | [diff] [blame] | 3191 | EXPORT_SYMBOL_GPL(__module_text_address); |
Rusty Russell | e610499 | 2009-03-31 13:05:31 -0600 | [diff] [blame] | 3192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3193 | /* Don't grab lock, we're oopsing. */ |
| 3194 | void print_modules(void) |
| 3195 | { |
| 3196 | struct module *mod; |
Randy Dunlap | 2bc2d61 | 2006-10-02 02:17:02 -0700 | [diff] [blame] | 3197 | char buf[8]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3198 | |
Linus Torvalds | b231125 | 2009-06-16 11:07:14 -0700 | [diff] [blame] | 3199 | printk(KERN_DEFAULT "Modules linked in:"); |
Andi Kleen | d72b375 | 2008-08-30 10:09:00 +0200 | [diff] [blame] | 3200 | /* Most callers should already have preempt disabled, but make sure */ |
| 3201 | preempt_disable(); |
| 3202 | list_for_each_entry_rcu(mod, &modules, list) |
Arjan van de Ven | 21aa928 | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 3203 | printk(" %s%s", mod->name, module_flags(mod, buf)); |
Andi Kleen | d72b375 | 2008-08-30 10:09:00 +0200 | [diff] [blame] | 3204 | preempt_enable(); |
Arjan van de Ven | e14af7e | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 3205 | if (last_unloaded_module[0]) |
| 3206 | printk(" [last unloaded: %s]", last_unloaded_module); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3207 | printk("\n"); |
| 3208 | } |
| 3209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3210 | #ifdef CONFIG_MODVERSIONS |
Rusty Russell | 8c8ef42 | 2009-03-31 13:05:34 -0600 | [diff] [blame] | 3211 | /* Generate the signature for all relevant module structures here. |
| 3212 | * If these change, we don't want to try to parse the module. */ |
| 3213 | void module_layout(struct module *mod, |
| 3214 | struct modversion_info *ver, |
| 3215 | struct kernel_param *kp, |
| 3216 | struct kernel_symbol *ks, |
Rusty Russell | 8c8ef42 | 2009-03-31 13:05:34 -0600 | [diff] [blame] | 3217 | struct tracepoint *tp) |
| 3218 | { |
| 3219 | } |
| 3220 | EXPORT_SYMBOL(module_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3221 | #endif |
Mathieu Desnoyers | 8256e47 | 2007-10-18 23:41:06 -0700 | [diff] [blame] | 3222 | |
Mathieu Desnoyers | 97e1c18 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 3223 | #ifdef CONFIG_TRACEPOINTS |
| 3224 | void module_update_tracepoints(void) |
| 3225 | { |
| 3226 | struct module *mod; |
| 3227 | |
| 3228 | mutex_lock(&module_mutex); |
| 3229 | list_for_each_entry(mod, &modules, list) |
| 3230 | if (!mod->taints) |
| 3231 | tracepoint_update_probe_range(mod->tracepoints, |
| 3232 | mod->tracepoints + mod->num_tracepoints); |
| 3233 | mutex_unlock(&module_mutex); |
| 3234 | } |
| 3235 | |
| 3236 | /* |
| 3237 | * Returns 0 if current not found. |
| 3238 | * Returns 1 if current found. |
| 3239 | */ |
| 3240 | int module_get_iter_tracepoints(struct tracepoint_iter *iter) |
| 3241 | { |
| 3242 | struct module *iter_mod; |
| 3243 | int found = 0; |
| 3244 | |
| 3245 | mutex_lock(&module_mutex); |
| 3246 | list_for_each_entry(iter_mod, &modules, list) { |
| 3247 | if (!iter_mod->taints) { |
| 3248 | /* |
| 3249 | * Sorted module list |
| 3250 | */ |
| 3251 | if (iter_mod < iter->module) |
| 3252 | continue; |
| 3253 | else if (iter_mod > iter->module) |
| 3254 | iter->tracepoint = NULL; |
| 3255 | found = tracepoint_get_iter_range(&iter->tracepoint, |
| 3256 | iter_mod->tracepoints, |
| 3257 | iter_mod->tracepoints |
| 3258 | + iter_mod->num_tracepoints); |
| 3259 | if (found) { |
| 3260 | iter->module = iter_mod; |
| 3261 | break; |
| 3262 | } |
| 3263 | } |
| 3264 | } |
| 3265 | mutex_unlock(&module_mutex); |
| 3266 | return found; |
| 3267 | } |
| 3268 | #endif |