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