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