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