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