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