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