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