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