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