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