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