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