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