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