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