blob: 6301d6e173caa6fbdf49da8c1e170b930989a9ef [file] [log] [blame]
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 Copyright (C) 2002 Richard Henderson
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003 Copyright (C) 2001 Rusty Russell, 2002, 2010 Rusty Russell IBM.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
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 Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
20#include <linux/moduleloader.h>
Steven Rostedt6d723732009-04-10 14:53:50 -040021#include <linux/ftrace_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
Alexey Dobriyanae84e322007-05-08 00:28:38 -070023#include <linux/kallsyms.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040024#include <linux/fs.h>
Roland McGrath6d760132007-10-16 23:26:40 -070025#include <linux/sysfs.h>
Randy Dunlap9f158332005-09-13 01:25:16 -070026#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/slab.h>
28#include <linux/vmalloc.h>
29#include <linux/elf.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040030#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/seq_file.h>
32#include <linux/syscalls.h>
33#include <linux/fcntl.h>
34#include <linux/rcupdate.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080035#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#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 Virof6a57032006-10-18 01:47:25 -040042#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/stop_machine.h>
44#include <linux/device.h>
Matt Domschc988d2b2005-06-23 22:05:15 -070045#include <linux/string.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080046#include <linux/mutex.h>
Andi Kleend72b3752008-08-30 10:09:00 +020047#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/cacheflush.h>
Bernd Schmidteb8cdec2009-09-21 17:03:57 -070050#include <asm/mmu_context.h>
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020051#include <linux/license.h>
Christoph Lameter6d762392008-02-08 04:18:42 -080052#include <asm/sections.h>
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040053#include <linux/tracepoint.h>
Steven Rostedt90d595f2008-08-14 15:45:09 -040054#include <linux/ftrace.h>
Arjan van de Ven22a9d642009-01-07 08:45:46 -080055#include <linux/async.h>
Tejun Heofbf59bc2009-02-20 16:29:08 +090056#include <linux/percpu.h>
Catalin Marinas4f2294b2009-06-11 13:23:20 +010057#include <linux/kmemleak.h>
Jason Baronbf5438fc2010-09-17 11:09:00 -040058#include <linux/jump_label.h>
matthieu castet84e1c6b2010-11-16 22:35:16 +010059#include <linux/pfn.h>
Alessio Igor Bogani403ed272011-04-20 11:10:52 +020060#include <linux/bsearch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Li Zefan7ead8b82009-08-17 16:56:28 +080062#define CREATE_TRACE_POINTS
63#include <trace/events/module.h>
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#if 0
66#define DEBUGP printk
67#else
68#define DEBUGP(fmt , a...)
69#endif
70
71#ifndef ARCH_SHF_SMALL
72#define ARCH_SHF_SMALL 0
73#endif
74
matthieu castet84e1c6b2010-11-16 22:35:16 +010075/*
76 * Modules' sections will be aligned on page boundaries
77 * to ensure complete separation of code and data, but
78 * only when CONFIG_DEBUG_SET_MODULE_RONX=y
79 */
80#ifdef CONFIG_DEBUG_SET_MODULE_RONX
81# define debug_align(X) ALIGN(X, PAGE_SIZE)
82#else
83# define debug_align(X) (X)
84#endif
85
86/*
87 * Given BASE and SIZE this macro calculates the number of pages the
88 * memory regions occupies
89 */
90#define MOD_NUMBER_OF_PAGES(BASE, SIZE) (((SIZE) > 0) ? \
91 (PFN_DOWN((unsigned long)(BASE) + (SIZE) - 1) - \
92 PFN_DOWN((unsigned long)BASE) + 1) \
93 : (0UL))
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/* If this is set, the section belongs in the init part of the module */
96#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
97
Rusty Russell75676502010-06-05 11:17:36 -060098/*
99 * Mutex protects:
100 * 1) List of modules (also safely readable with preempt_disable),
101 * 2) module_use links,
102 * 3) module_addr_min/module_addr_max.
Andi Kleend72b3752008-08-30 10:09:00 +0200103 * (delete uses stop_machine/add uses RCU list operations). */
Tim Abbottc6b37802008-12-05 19:03:59 -0500104DEFINE_MUTEX(module_mutex);
105EXPORT_SYMBOL_GPL(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static LIST_HEAD(modules);
Jason Wessel67fc4e02010-05-20 21:04:21 -0500107#ifdef CONFIG_KGDB_KDB
108struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
109#endif /* CONFIG_KGDB_KDB */
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Stephen Rothwell19e45292009-04-14 17:27:18 +1000112/* Block module loading/unloading? */
113int modules_disabled = 0;
114
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500115/* Waiting for a module to finish initializing? */
116static DECLARE_WAIT_QUEUE_HEAD(module_wq);
117
Alan Sterne041c682006-03-27 01:16:30 -0800118static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Rusty Russell75676502010-06-05 11:17:36 -0600120/* Bounds of module allocation, for speeding __module_address.
121 * Protected by module_mutex. */
Rusty Russell3a642e92008-07-22 19:24:28 -0500122static unsigned long module_addr_min = -1UL, module_addr_max = 0;
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124int register_module_notifier(struct notifier_block * nb)
125{
Alan Sterne041c682006-03-27 01:16:30 -0800126 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128EXPORT_SYMBOL(register_module_notifier);
129
130int unregister_module_notifier(struct notifier_block * nb)
131{
Alan Sterne041c682006-03-27 01:16:30 -0800132 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134EXPORT_SYMBOL(unregister_module_notifier);
135
Rusty Russelleded41c2010-08-05 12:59:07 -0600136struct load_info {
137 Elf_Ehdr *hdr;
138 unsigned long len;
139 Elf_Shdr *sechdrs;
Rusty Russell6526c532010-08-05 12:59:10 -0600140 char *secstrings, *strtab;
Rusty Russelld9131882010-08-05 12:59:08 -0600141 unsigned long *strmap;
142 unsigned long symoffs, stroffs;
Rusty Russell811d66a2010-08-05 12:59:12 -0600143 struct _ddebug *debug;
144 unsigned int num_debug;
Rusty Russelleded41c2010-08-05 12:59:07 -0600145 struct {
146 unsigned int sym, str, mod, vers, info, pcpu;
147 } index;
148};
149
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -0800150/* We require a truly strong try_module_get(): 0 means failure due to
151 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static inline int strong_try_module_get(struct module *mod)
153{
154 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500155 return -EBUSY;
156 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500158 else
159 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700162static inline void add_taint_module(struct module *mod, unsigned flag)
163{
164 add_taint(flag);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700165 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700166}
167
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200168/*
169 * A thread that wants to hold a reference to a module only while it
170 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 */
172void __module_put_and_exit(struct module *mod, long code)
173{
174 module_put(mod);
175 do_exit(code);
176}
177EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/* Find a module section: 0 means not found. */
Rusty Russell49668682010-08-05 12:59:10 -0600180static unsigned int find_sec(const struct load_info *info, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 unsigned int i;
183
Rusty Russell49668682010-08-05 12:59:10 -0600184 for (i = 1; i < info->hdr->e_shnum; i++) {
185 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 /* Alloc bit cleared means "ignore it." */
Rusty Russell49668682010-08-05 12:59:10 -0600187 if ((shdr->sh_flags & SHF_ALLOC)
188 && strcmp(info->secstrings + shdr->sh_name, name) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return i;
Rusty Russell49668682010-08-05 12:59:10 -0600190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return 0;
192}
193
Rusty Russell5e458cc2008-10-22 10:00:13 -0500194/* Find a module section, or NULL. */
Rusty Russell49668682010-08-05 12:59:10 -0600195static void *section_addr(const struct load_info *info, const char *name)
Rusty Russell5e458cc2008-10-22 10:00:13 -0500196{
197 /* Section 0 has sh_addr 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600198 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500199}
200
201/* Find a module section, or NULL. Fill in number of "objects" in section. */
Rusty Russell49668682010-08-05 12:59:10 -0600202static void *section_objs(const struct load_info *info,
Rusty Russell5e458cc2008-10-22 10:00:13 -0500203 const char *name,
204 size_t object_size,
205 unsigned int *num)
206{
Rusty Russell49668682010-08-05 12:59:10 -0600207 unsigned int sec = find_sec(info, name);
Rusty Russell5e458cc2008-10-22 10:00:13 -0500208
209 /* Section 0 has sh_addr 0 and sh_size 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600210 *num = info->sechdrs[sec].sh_size / object_size;
211 return (void *)info->sechdrs[sec].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500212}
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214/* Provided by the linker */
215extern const struct kernel_symbol __start___ksymtab[];
216extern const struct kernel_symbol __stop___ksymtab[];
217extern const struct kernel_symbol __start___ksymtab_gpl[];
218extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800219extern const struct kernel_symbol __start___ksymtab_gpl_future[];
220extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221extern const unsigned long __start___kcrctab[];
222extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800223extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500224#ifdef CONFIG_UNUSED_SYMBOLS
225extern const struct kernel_symbol __start___ksymtab_unused[];
226extern const struct kernel_symbol __stop___ksymtab_unused[];
227extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
228extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700229extern const unsigned long __start___kcrctab_unused[];
230extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500231#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233#ifndef CONFIG_MODVERSIONS
234#define symversion(base, idx) NULL
235#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800236#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237#endif
238
Rusty Russelldafd0942008-07-22 19:24:25 -0500239static bool each_symbol_in_section(const struct symsearch *arr,
240 unsigned int arrsize,
241 struct module *owner,
242 bool (*fn)(const struct symsearch *syms,
243 struct module *owner,
Rusty Russellde4d8d52011-04-19 21:49:58 +0200244 void *data),
Rusty Russelldafd0942008-07-22 19:24:25 -0500245 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100246{
Rusty Russellde4d8d52011-04-19 21:49:58 +0200247 unsigned int j;
Rusty Russelldafd0942008-07-22 19:24:25 -0500248
249 for (j = 0; j < arrsize; j++) {
Rusty Russellde4d8d52011-04-19 21:49:58 +0200250 if (fn(&arr[j], owner, data))
251 return true;
Rusty Russelldafd0942008-07-22 19:24:25 -0500252 }
253
254 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100255}
256
Rusty Russelldafd0942008-07-22 19:24:25 -0500257/* Returns true as soon as fn returns true, otherwise false. */
Rusty Russellde4d8d52011-04-19 21:49:58 +0200258bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
259 struct module *owner,
260 void *data),
261 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700262{
Rusty Russelldafd0942008-07-22 19:24:25 -0500263 struct module *mod;
Linus Torvalds44032e62010-08-05 12:59:05 -0600264 static const struct symsearch arr[] = {
Rusty Russelldafd0942008-07-22 19:24:25 -0500265 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
266 NOT_GPL_ONLY, false },
267 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
268 __start___kcrctab_gpl,
269 GPL_ONLY, false },
270 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
271 __start___kcrctab_gpl_future,
272 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500273#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500274 { __start___ksymtab_unused, __stop___ksymtab_unused,
275 __start___kcrctab_unused,
276 NOT_GPL_ONLY, true },
277 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
278 __start___kcrctab_unused_gpl,
279 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500280#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500281 };
282
283 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
284 return true;
285
Andi Kleend72b3752008-08-30 10:09:00 +0200286 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500287 struct symsearch arr[] = {
288 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
289 NOT_GPL_ONLY, false },
290 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
291 mod->gpl_crcs,
292 GPL_ONLY, false },
293 { mod->gpl_future_syms,
294 mod->gpl_future_syms + mod->num_gpl_future_syms,
295 mod->gpl_future_crcs,
296 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500297#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500298 { mod->unused_syms,
299 mod->unused_syms + mod->num_unused_syms,
300 mod->unused_crcs,
301 NOT_GPL_ONLY, true },
302 { mod->unused_gpl_syms,
303 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
304 mod->unused_gpl_crcs,
305 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500306#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500307 };
308
309 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
310 return true;
311 }
312 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700313}
Rusty Russellde4d8d52011-04-19 21:49:58 +0200314EXPORT_SYMBOL_GPL(each_symbol_section);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700315
Rusty Russelldafd0942008-07-22 19:24:25 -0500316struct find_symbol_arg {
317 /* Input */
318 const char *name;
319 bool gplok;
320 bool warn;
321
322 /* Output */
323 struct module *owner;
324 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500325 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500326};
327
Rusty Russellde4d8d52011-04-19 21:49:58 +0200328static bool check_symbol(const struct symsearch *syms,
329 struct module *owner,
330 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500331{
Rusty Russelldafd0942008-07-22 19:24:25 -0500332 struct find_symbol_arg *fsa = data;
333
Rusty Russelldafd0942008-07-22 19:24:25 -0500334 if (!fsa->gplok) {
335 if (syms->licence == GPL_ONLY)
336 return false;
337 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
338 printk(KERN_WARNING "Symbol %s is being used "
339 "by a non-GPL module, which will not "
340 "be allowed in the future\n", fsa->name);
341 printk(KERN_WARNING "Please see the file "
342 "Documentation/feature-removal-schedule.txt "
343 "in the kernel source tree for more details.\n");
344 }
345 }
346
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500347#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500348 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500349 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500350 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500351 printk(KERN_WARNING
352 "This symbol will go away in the future.\n");
353 printk(KERN_WARNING
354 "Please evalute if this is the right api to use and if "
355 "it really is, submit a report the linux kernel "
356 "mailinglist together with submitting your code for "
357 "inclusion.\n");
358 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500359#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500360
361 fsa->owner = owner;
362 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500363 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500364 return true;
365}
366
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200367static int cmp_name(const void *va, const void *vb)
368{
369 const char *a;
370 const struct kernel_symbol *b;
371 a = va; b = vb;
372 return strcmp(a, b->name);
373}
374
Rusty Russellde4d8d52011-04-19 21:49:58 +0200375static bool find_symbol_in_section(const struct symsearch *syms,
376 struct module *owner,
377 void *data)
378{
379 struct find_symbol_arg *fsa = data;
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200380 struct kernel_symbol *sym;
Rusty Russellde4d8d52011-04-19 21:49:58 +0200381
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200382 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
383 sizeof(struct kernel_symbol), cmp_name);
384
385 if (sym != NULL && check_symbol(syms, owner, sym - syms->start, data))
386 return true;
387
Rusty Russellde4d8d52011-04-19 21:49:58 +0200388 return false;
389}
390
Tim Abbott414fd312008-12-05 19:03:56 -0500391/* Find a symbol and return it, along with, (optional) crc and
Rusty Russell75676502010-06-05 11:17:36 -0600392 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500393const struct kernel_symbol *find_symbol(const char *name,
394 struct module **owner,
395 const unsigned long **crc,
396 bool gplok,
397 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Rusty Russelldafd0942008-07-22 19:24:25 -0500399 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Rusty Russelldafd0942008-07-22 19:24:25 -0500401 fsa.name = name;
402 fsa.gplok = gplok;
403 fsa.warn = warn;
404
Rusty Russellde4d8d52011-04-19 21:49:58 +0200405 if (each_symbol_section(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500406 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500407 *owner = fsa.owner;
408 if (crc)
409 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500410 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 DEBUGP("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500414 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
Tim Abbottc6b37802008-12-05 19:03:59 -0500416EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418/* Search for module by name: must hold module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500419struct module *find_module(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 struct module *mod;
422
423 list_for_each_entry(mod, &modules, list) {
424 if (strcmp(mod->name, name) == 0)
425 return mod;
426 }
427 return NULL;
428}
Tim Abbottc6b37802008-12-05 19:03:59 -0500429EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900432
Tejun Heo259354d2010-03-10 18:56:10 +0900433static inline void __percpu *mod_percpu(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900434{
Tejun Heo259354d2010-03-10 18:56:10 +0900435 return mod->percpu;
436}
Tejun Heofbf59bc2009-02-20 16:29:08 +0900437
Tejun Heo259354d2010-03-10 18:56:10 +0900438static int percpu_modalloc(struct module *mod,
439 unsigned long size, unsigned long align)
440{
Tejun Heofbf59bc2009-02-20 16:29:08 +0900441 if (align > PAGE_SIZE) {
442 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
Tejun Heo259354d2010-03-10 18:56:10 +0900443 mod->name, align, PAGE_SIZE);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900444 align = PAGE_SIZE;
445 }
446
Tejun Heo259354d2010-03-10 18:56:10 +0900447 mod->percpu = __alloc_reserved_percpu(size, align);
448 if (!mod->percpu) {
Tejun Heofbf59bc2009-02-20 16:29:08 +0900449 printk(KERN_WARNING
Rusty Russelld9131882010-08-05 12:59:08 -0600450 "%s: Could not allocate %lu bytes percpu data\n",
451 mod->name, size);
Tejun Heo259354d2010-03-10 18:56:10 +0900452 return -ENOMEM;
453 }
454 mod->percpu_size = size;
455 return 0;
Tejun Heofbf59bc2009-02-20 16:29:08 +0900456}
457
Tejun Heo259354d2010-03-10 18:56:10 +0900458static void percpu_modfree(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900459{
Tejun Heo259354d2010-03-10 18:56:10 +0900460 free_percpu(mod->percpu);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900461}
462
Rusty Russell49668682010-08-05 12:59:10 -0600463static unsigned int find_pcpusec(struct load_info *info)
Tejun Heo6b588c12009-02-20 16:29:07 +0900464{
Rusty Russell49668682010-08-05 12:59:10 -0600465 return find_sec(info, ".data..percpu");
Tejun Heo6b588c12009-02-20 16:29:07 +0900466}
467
Tejun Heo259354d2010-03-10 18:56:10 +0900468static void percpu_modcopy(struct module *mod,
469 const void *from, unsigned long size)
Tejun Heo6b588c12009-02-20 16:29:07 +0900470{
471 int cpu;
472
473 for_each_possible_cpu(cpu)
Tejun Heo259354d2010-03-10 18:56:10 +0900474 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
Tejun Heo6b588c12009-02-20 16:29:07 +0900475}
476
Tejun Heo10fad5e2010-03-10 18:57:54 +0900477/**
478 * is_module_percpu_address - test whether address is from module static percpu
479 * @addr: address to test
480 *
481 * Test whether @addr belongs to module static percpu area.
482 *
483 * RETURNS:
484 * %true if @addr is from module static percpu area
485 */
486bool is_module_percpu_address(unsigned long addr)
487{
488 struct module *mod;
489 unsigned int cpu;
490
491 preempt_disable();
492
493 list_for_each_entry_rcu(mod, &modules, list) {
494 if (!mod->percpu_size)
495 continue;
496 for_each_possible_cpu(cpu) {
497 void *start = per_cpu_ptr(mod->percpu, cpu);
498
499 if ((void *)addr >= start &&
500 (void *)addr < start + mod->percpu_size) {
501 preempt_enable();
502 return true;
503 }
504 }
505 }
506
507 preempt_enable();
508 return false;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700509}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900512
Tejun Heo259354d2010-03-10 18:56:10 +0900513static inline void __percpu *mod_percpu(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 return NULL;
516}
Tejun Heo259354d2010-03-10 18:56:10 +0900517static inline int percpu_modalloc(struct module *mod,
518 unsigned long size, unsigned long align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Tejun Heo259354d2010-03-10 18:56:10 +0900520 return -ENOMEM;
521}
522static inline void percpu_modfree(struct module *mod)
523{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
Rusty Russell49668682010-08-05 12:59:10 -0600525static unsigned int find_pcpusec(struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 return 0;
528}
Tejun Heo259354d2010-03-10 18:56:10 +0900529static inline void percpu_modcopy(struct module *mod,
530 const void *from, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
532 /* pcpusec should be 0, and size of that section should be 0. */
533 BUG_ON(size != 0);
534}
Tejun Heo10fad5e2010-03-10 18:57:54 +0900535bool is_module_percpu_address(unsigned long addr)
536{
537 return false;
538}
Tejun Heo6b588c12009-02-20 16:29:07 +0900539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540#endif /* CONFIG_SMP */
541
Matt Domschc988d2b2005-06-23 22:05:15 -0700542#define MODINFO_ATTR(field) \
543static void setup_modinfo_##field(struct module *mod, const char *s) \
544{ \
545 mod->field = kstrdup(s, GFP_KERNEL); \
546} \
547static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
548 struct module *mod, char *buffer) \
549{ \
550 return sprintf(buffer, "%s\n", mod->field); \
551} \
552static int modinfo_##field##_exists(struct module *mod) \
553{ \
554 return mod->field != NULL; \
555} \
556static void free_modinfo_##field(struct module *mod) \
557{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700558 kfree(mod->field); \
559 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700560} \
561static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900562 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700563 .show = show_modinfo_##field, \
564 .setup = setup_modinfo_##field, \
565 .test = modinfo_##field##_exists, \
566 .free = free_modinfo_##field, \
567};
568
569MODINFO_ATTR(version);
570MODINFO_ATTR(srcversion);
571
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100572static char last_unloaded_module[MODULE_NAME_LEN+1];
573
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800574#ifdef CONFIG_MODULE_UNLOAD
Steven Rostedteb0c5372010-03-29 14:25:18 -0400575
576EXPORT_TRACEPOINT_SYMBOL(module_get);
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578/* Init the unload section of the module. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600579static int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600581 mod->refptr = alloc_percpu(struct module_ref);
582 if (!mod->refptr)
583 return -ENOMEM;
584
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700585 INIT_LIST_HEAD(&mod->source_list);
586 INIT_LIST_HEAD(&mod->target_list);
Christoph Lametere1783a22010-01-05 15:34:50 +0900587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 /* Hold reference count during initialization. */
Nick Piggin5fbfb182010-04-01 19:09:40 +1100589 __this_cpu_write(mod->refptr->incs, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 /* Backwards compatibility macros put refcount during init. */
591 mod->waiter = current;
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600592
593 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596/* Does a already use b? */
597static int already_uses(struct module *a, struct module *b)
598{
599 struct module_use *use;
600
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700601 list_for_each_entry(use, &b->source_list, source_list) {
602 if (use->source == a) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 DEBUGP("%s uses %s!\n", a->name, b->name);
604 return 1;
605 }
606 }
607 DEBUGP("%s does not use %s!\n", a->name, b->name);
608 return 0;
609}
610
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700611/*
612 * Module a uses b
613 * - we add 'a' as a "source", 'b' as a "target" of module use
614 * - the module_use is added to the list of 'b' sources (so
615 * 'b' can walk the list to see who sourced them), and of 'a'
616 * targets (so 'a' can see what modules it targets).
617 */
618static int add_module_usage(struct module *a, struct module *b)
619{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700620 struct module_use *use;
621
622 DEBUGP("Allocating new usage for %s.\n", a->name);
623 use = kmalloc(sizeof(*use), GFP_ATOMIC);
624 if (!use) {
625 printk(KERN_WARNING "%s: out of memory loading\n", a->name);
626 return -ENOMEM;
627 }
628
629 use->source = a;
630 use->target = b;
631 list_add(&use->source_list, &b->source_list);
632 list_add(&use->target_list, &a->target_list);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700633 return 0;
634}
635
Rusty Russell75676502010-06-05 11:17:36 -0600636/* Module a uses b: caller needs module_mutex() */
Rusty Russell9bea7f22010-06-05 11:17:37 -0600637int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Rusty Russellc8e21ce2010-06-05 11:17:35 -0600639 int err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100640
Rusty Russell9bea7f22010-06-05 11:17:37 -0600641 if (b == NULL || already_uses(a, b))
Linus Torvalds218ce732010-05-25 16:48:30 -0700642 return 0;
Linus Torvalds218ce732010-05-25 16:48:30 -0700643
Rusty Russell9bea7f22010-06-05 11:17:37 -0600644 /* If module isn't available, we fail. */
645 err = strong_try_module_get(b);
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500646 if (err)
Rusty Russell9bea7f22010-06-05 11:17:37 -0600647 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700649 err = add_module_usage(a, b);
650 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 module_put(b);
Rusty Russell9bea7f22010-06-05 11:17:37 -0600652 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
Rusty Russell9bea7f22010-06-05 11:17:37 -0600654 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600656EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658/* Clear the unload stuff of the module. */
659static void module_unload_free(struct module *mod)
660{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700661 struct module_use *use, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Rusty Russell75676502010-06-05 11:17:36 -0600663 mutex_lock(&module_mutex);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700664 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
665 struct module *i = use->target;
666 DEBUGP("%s unusing %s\n", mod->name, i->name);
667 module_put(i);
668 list_del(&use->source_list);
669 list_del(&use->target_list);
670 kfree(use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
Rusty Russell75676502010-06-05 11:17:36 -0600672 mutex_unlock(&module_mutex);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600673
674 free_percpu(mod->refptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
677#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800678static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 int ret = (flags & O_TRUNC);
681 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800682 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return ret;
684}
685#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800686static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 return 0;
689}
690#endif /* CONFIG_MODULE_FORCE_UNLOAD */
691
692struct stopref
693{
694 struct module *mod;
695 int flags;
696 int *forced;
697};
698
699/* Whole machine is stopped with interrupts off when this runs. */
700static int __try_stop_module(void *_sref)
701{
702 struct stopref *sref = _sref;
703
Rusty Russellda39ba52008-07-22 19:24:25 -0500704 /* If it's not unused, quit unless we're forcing. */
705 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800706 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return -EWOULDBLOCK;
708 }
709
710 /* Mark it as dying. */
711 sref->mod->state = MODULE_STATE_GOING;
712 return 0;
713}
714
715static int try_stop_module(struct module *mod, int flags, int *forced)
716{
Rusty Russellda39ba52008-07-22 19:24:25 -0500717 if (flags & O_NONBLOCK) {
718 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500720 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500721 } else {
722 /* We don't need to stop the machine for this. */
723 mod->state = MODULE_STATE_GOING;
724 synchronize_sched();
725 return 0;
726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
729unsigned int module_refcount(struct module *mod)
730{
Nick Piggin5fbfb182010-04-01 19:09:40 +1100731 unsigned int incs = 0, decs = 0;
Eric Dumazet720eba32009-02-03 13:31:36 +1030732 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Eric Dumazet720eba32009-02-03 13:31:36 +1030734 for_each_possible_cpu(cpu)
Nick Piggin5fbfb182010-04-01 19:09:40 +1100735 decs += per_cpu_ptr(mod->refptr, cpu)->decs;
736 /*
737 * ensure the incs are added up after the decs.
738 * module_put ensures incs are visible before decs with smp_wmb.
739 *
740 * This 2-count scheme avoids the situation where the refcount
741 * for CPU0 is read, then CPU0 increments the module refcount,
742 * then CPU1 drops that refcount, then the refcount for CPU1 is
743 * read. We would record a decrement but not its corresponding
744 * increment so we would see a low count (disaster).
745 *
746 * Rare situation? But module_refcount can be preempted, and we
747 * might be tallying up 4096+ CPUs. So it is not impossible.
748 */
749 smp_rmb();
750 for_each_possible_cpu(cpu)
751 incs += per_cpu_ptr(mod->refptr, cpu)->incs;
752 return incs - decs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754EXPORT_SYMBOL(module_refcount);
755
756/* This exists whether we can unload or not */
757static void free_module(struct module *mod);
758
759static void wait_for_zero_refcount(struct module *mod)
760{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500761 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800762 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 for (;;) {
764 DEBUGP("Looking at refcount...\n");
765 set_current_state(TASK_UNINTERRUPTIBLE);
766 if (module_refcount(mod) == 0)
767 break;
768 schedule();
769 }
770 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800771 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100774SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
775 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
777 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800778 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 int ret, forced = 0;
780
Kees Cook3d433212009-04-02 15:49:29 -0700781 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800782 return -EPERM;
783
784 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
785 return -EFAULT;
786 name[MODULE_NAME_LEN-1] = '\0';
787
Tejun Heo3fc1f1e2010-05-06 18:49:20 +0200788 if (mutex_lock_interruptible(&module_mutex) != 0)
789 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 mod = find_module(name);
792 if (!mod) {
793 ret = -ENOENT;
794 goto out;
795 }
796
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700797 if (!list_empty(&mod->source_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 /* Other modules depend on us: get rid of them first. */
799 ret = -EWOULDBLOCK;
800 goto out;
801 }
802
803 /* Doing init or already dying? */
804 if (mod->state != MODULE_STATE_LIVE) {
805 /* FIXME: if (force), slam module count and wake up
806 waiter --RR */
807 DEBUGP("%s already dying\n", mod->name);
808 ret = -EBUSY;
809 goto out;
810 }
811
812 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700813 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800814 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (!forced) {
816 /* This module can't be removed */
817 ret = -EBUSY;
818 goto out;
819 }
820 }
821
822 /* Set this up before setting mod->state */
823 mod->waiter = current;
824
825 /* Stop the machine so refcounts can't move and disable module. */
826 ret = try_stop_module(mod, flags, &forced);
827 if (ret != 0)
828 goto out;
829
830 /* Never wait if forced. */
831 if (!forced && module_refcount(mod) != 0)
832 wait_for_zero_refcount(mod);
833
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200834 mutex_unlock(&module_mutex);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300835 /* Final destruction now no one is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200836 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200838 blocking_notifier_call_chain(&module_notify_list,
839 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800840 async_synchronize_full();
Rusty Russell75676502010-06-05 11:17:36 -0600841
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100842 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500843 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Rusty Russell75676502010-06-05 11:17:36 -0600845 free_module(mod);
846 return 0;
847out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800848 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return ret;
850}
851
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800852static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
854 struct module_use *use;
855 int printed_something = 0;
856
857 seq_printf(m, " %u ", module_refcount(mod));
858
859 /* Always include a trailing , so userspace can differentiate
860 between this and the old multi-field proc format. */
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700861 list_for_each_entry(use, &mod->source_list, source_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 printed_something = 1;
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700863 seq_printf(m, "%s,", use->source->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (mod->init != NULL && mod->exit == NULL) {
867 printed_something = 1;
868 seq_printf(m, "[permanent],");
869 }
870
871 if (!printed_something)
872 seq_printf(m, "-");
873}
874
875void __symbol_put(const char *symbol)
876{
877 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Rusty Russell24da1cb2007-07-15 23:41:46 -0700879 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -0500880 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 BUG();
882 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700883 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885EXPORT_SYMBOL(__symbol_put);
886
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930887/* Note this assumes addr is a function, which it currently always is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888void symbol_put_addr(void *addr)
889{
Trent Piepho5e376612006-05-15 09:44:06 -0700890 struct module *modaddr;
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930891 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930893 if (core_kernel_text(a))
Trent Piepho5e376612006-05-15 09:44:06 -0700894 return;
895
Rusty Russella6e6abd2009-03-31 13:05:31 -0600896 /* module_text_address is safe here: we're supposed to have reference
897 * to module from symbol_get, so it can't go away. */
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930898 modaddr = __module_text_address(a);
Rusty Russella6e6abd2009-03-31 13:05:31 -0600899 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -0700900 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902EXPORT_SYMBOL_GPL(symbol_put_addr);
903
904static ssize_t show_refcnt(struct module_attribute *mattr,
905 struct module *mod, char *buffer)
906{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400907 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
910static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900911 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 .show = show_refcnt,
913};
914
Al Virof6a57032006-10-18 01:47:25 -0400915void module_put(struct module *module)
916{
917 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +0900918 preempt_disable();
Nick Piggin5fbfb182010-04-01 19:09:40 +1100919 smp_wmb(); /* see comment in module_refcount */
920 __this_cpu_inc(module->refptr->decs);
Christoph Lametere1783a22010-01-05 15:34:50 +0900921
Li Zefanae832d12010-03-24 10:57:43 +0800922 trace_module_put(module, _RET_IP_);
Al Virof6a57032006-10-18 01:47:25 -0400923 /* Maybe they're waiting for us to drop reference? */
924 if (unlikely(!module_is_live(module)))
925 wake_up_process(module->waiter);
Christoph Lametere1783a22010-01-05 15:34:50 +0900926 preempt_enable();
Al Virof6a57032006-10-18 01:47:25 -0400927 }
928}
929EXPORT_SYMBOL(module_put);
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800932static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
934 /* We don't know the usage count, or what modules are using. */
935 seq_printf(m, " - -");
936}
937
938static inline void module_unload_free(struct module *mod)
939{
940}
941
Rusty Russell9bea7f22010-06-05 11:17:37 -0600942int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Rusty Russell9bea7f22010-06-05 11:17:37 -0600944 return strong_try_module_get(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600946EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600948static inline int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600950 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952#endif /* CONFIG_MODULE_UNLOAD */
953
Kay Sievers1f717402006-11-24 12:15:25 +0100954static ssize_t show_initstate(struct module_attribute *mattr,
955 struct module *mod, char *buffer)
956{
957 const char *state = "unknown";
958
959 switch (mod->state) {
960 case MODULE_STATE_LIVE:
961 state = "live";
962 break;
963 case MODULE_STATE_COMING:
964 state = "coming";
965 break;
966 case MODULE_STATE_GOING:
967 state = "going";
968 break;
969 }
970 return sprintf(buffer, "%s\n", state);
971}
972
973static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900974 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100975 .show = show_initstate,
976};
977
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800978static struct module_attribute *modinfo_attrs[] = {
979 &modinfo_version,
980 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100981 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800982#ifdef CONFIG_MODULE_UNLOAD
983 &refcnt,
984#endif
985 NULL,
986};
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988static const char vermagic[] = VERMAGIC_STRING;
989
Rusty Russellc6e665c2009-03-31 13:05:33 -0600990static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -0700991{
992#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -0700993 if (!test_taint(TAINT_FORCED_MODULE))
Rusty Russellc6e665c2009-03-31 13:05:33 -0600994 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
995 mod->name, reason);
Linus Torvalds826e4502008-05-04 17:04:16 -0700996 add_taint_module(mod, TAINT_FORCED_MODULE);
997 return 0;
998#else
999 return -ENOEXEC;
1000#endif
1001}
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003#ifdef CONFIG_MODVERSIONS
Rusty Russelld4703ae2009-12-15 16:28:32 -06001004/* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
1005static unsigned long maybe_relocated(unsigned long crc,
1006 const struct module *crc_owner)
1007{
1008#ifdef ARCH_RELOCATES_KCRCTAB
1009 if (crc_owner == NULL)
1010 return crc - (unsigned long)reloc_start;
1011#endif
1012 return crc;
1013}
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015static int check_version(Elf_Shdr *sechdrs,
1016 unsigned int versindex,
1017 const char *symname,
1018 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001019 const unsigned long *crc,
1020 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
1022 unsigned int i, num_versions;
1023 struct modversion_info *versions;
1024
1025 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1026 if (!crc)
1027 return 1;
1028
Rusty Russella5dd6972008-05-09 16:24:21 +10001029 /* No versions at all? modprobe --force does this. */
1030 if (versindex == 0)
1031 return try_to_force_load(mod, symname) == 0;
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 versions = (void *) sechdrs[versindex].sh_addr;
1034 num_versions = sechdrs[versindex].sh_size
1035 / sizeof(struct modversion_info);
1036
1037 for (i = 0; i < num_versions; i++) {
1038 if (strcmp(versions[i].name, symname) != 0)
1039 continue;
1040
Rusty Russelld4703ae2009-12-15 16:28:32 -06001041 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 DEBUGP("Found checksum %lX vs module %lX\n",
Rusty Russelld4703ae2009-12-15 16:28:32 -06001044 maybe_relocated(*crc, crc_owner), versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001045 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001047
Rusty Russella5dd6972008-05-09 16:24:21 +10001048 printk(KERN_WARNING "%s: no symbol version for %s\n",
1049 mod->name, symname);
1050 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001051
1052bad_version:
1053 printk("%s: disagrees about version of symbol %s\n",
1054 mod->name, symname);
1055 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
1058static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1059 unsigned int versindex,
1060 struct module *mod)
1061{
1062 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Rusty Russell75676502010-06-05 11:17:36 -06001064 /* Since this should be found in kernel (which can't be removed),
1065 * no locking is necessary. */
Mike Frysinger6560dc12009-07-23 23:42:08 +09301066 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1067 &crc, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 BUG();
Rusty Russelld4703ae2009-12-15 16:28:32 -06001069 return check_version(sechdrs, versindex, "module_layout", mod, crc,
1070 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
Rusty Russell91e37a72008-05-09 16:25:28 +10001073/* First part is kernel version, which we ignore if module has crcs. */
1074static inline int same_magic(const char *amagic, const char *bmagic,
1075 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Rusty Russell91e37a72008-05-09 16:25:28 +10001077 if (has_crcs) {
1078 amagic += strcspn(amagic, " ");
1079 bmagic += strcspn(bmagic, " ");
1080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return strcmp(amagic, bmagic) == 0;
1082}
1083#else
1084static inline int check_version(Elf_Shdr *sechdrs,
1085 unsigned int versindex,
1086 const char *symname,
1087 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001088 const unsigned long *crc,
1089 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 return 1;
1092}
1093
1094static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1095 unsigned int versindex,
1096 struct module *mod)
1097{
1098 return 1;
1099}
1100
Rusty Russell91e37a72008-05-09 16:25:28 +10001101static inline int same_magic(const char *amagic, const char *bmagic,
1102 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
1104 return strcmp(amagic, bmagic) == 0;
1105}
1106#endif /* CONFIG_MODVERSIONS */
1107
Rusty Russell75676502010-06-05 11:17:36 -06001108/* Resolve a symbol for this module. I.e. if we find one, record usage. */
Rusty Russell49668682010-08-05 12:59:10 -06001109static const struct kernel_symbol *resolve_symbol(struct module *mod,
1110 const struct load_info *info,
Tim Abbott414fd312008-12-05 19:03:56 -05001111 const char *name,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001112 char ownername[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
1114 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001115 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 const unsigned long *crc;
Rusty Russell9bea7f22010-06-05 11:17:37 -06001117 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Rusty Russell75676502010-06-05 11:17:36 -06001119 mutex_lock(&module_mutex);
Tim Abbott414fd312008-12-05 19:03:56 -05001120 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001121 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001122 if (!sym)
1123 goto unlock;
1124
Rusty Russell49668682010-08-05 12:59:10 -06001125 if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
1126 owner)) {
Rusty Russell9bea7f22010-06-05 11:17:37 -06001127 sym = ERR_PTR(-EINVAL);
1128 goto getname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
Rusty Russell9bea7f22010-06-05 11:17:37 -06001130
1131 err = ref_module(mod, owner);
1132 if (err) {
1133 sym = ERR_PTR(err);
1134 goto getname;
1135 }
1136
1137getname:
1138 /* We must make copy under the lock if we failed to get ref. */
1139 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1140unlock:
Rusty Russell75676502010-06-05 11:17:36 -06001141 mutex_unlock(&module_mutex);
Linus Torvalds218ce732010-05-25 16:48:30 -07001142 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
Rusty Russell49668682010-08-05 12:59:10 -06001145static const struct kernel_symbol *
1146resolve_symbol_wait(struct module *mod,
1147 const struct load_info *info,
1148 const char *name)
Rusty Russell9bea7f22010-06-05 11:17:37 -06001149{
1150 const struct kernel_symbol *ksym;
Rusty Russell49668682010-08-05 12:59:10 -06001151 char owner[MODULE_NAME_LEN];
Rusty Russell9bea7f22010-06-05 11:17:37 -06001152
1153 if (wait_event_interruptible_timeout(module_wq,
Rusty Russell49668682010-08-05 12:59:10 -06001154 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1155 || PTR_ERR(ksym) != -EBUSY,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001156 30 * HZ) <= 0) {
1157 printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
Rusty Russell49668682010-08-05 12:59:10 -06001158 mod->name, owner);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001159 }
1160 return ksym;
1161}
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163/*
1164 * /sys/module/foo/sections stuff
1165 * J. Corbet <corbet@lwn.net>
1166 */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001167#ifdef CONFIG_SYSFS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001168
Rusty Russell8f6d0372010-08-05 12:59:09 -06001169#ifdef CONFIG_KALLSYMS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001170static inline bool sect_empty(const Elf_Shdr *sect)
1171{
1172 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1173}
1174
Rusty Russella58730c2008-03-13 09:03:44 +00001175struct module_sect_attr
1176{
1177 struct module_attribute mattr;
1178 char *name;
1179 unsigned long address;
1180};
1181
1182struct module_sect_attrs
1183{
1184 struct attribute_group grp;
1185 unsigned int nsections;
1186 struct module_sect_attr attrs[0];
1187};
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189static ssize_t module_sect_show(struct module_attribute *mattr,
1190 struct module *mod, char *buf)
1191{
1192 struct module_sect_attr *sattr =
1193 container_of(mattr, struct module_sect_attr, mattr);
Kees Cook9f36e2c2011-03-22 16:34:22 -07001194 return sprintf(buf, "0x%pK\n", (void *)sattr->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001197static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1198{
Rusty Russella58730c2008-03-13 09:03:44 +00001199 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001200
1201 for (section = 0; section < sect_attrs->nsections; section++)
1202 kfree(sect_attrs->attrs[section].name);
1203 kfree(sect_attrs);
1204}
1205
Rusty Russell8f6d0372010-08-05 12:59:09 -06001206static void add_sect_attrs(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
1208 unsigned int nloaded = 0, i, size[2];
1209 struct module_sect_attrs *sect_attrs;
1210 struct module_sect_attr *sattr;
1211 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 /* Count loaded sections and allocate structures */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001214 for (i = 0; i < info->hdr->e_shnum; i++)
1215 if (!sect_empty(&info->sechdrs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 nloaded++;
1217 size[0] = ALIGN(sizeof(*sect_attrs)
1218 + nloaded * sizeof(sect_attrs->attrs[0]),
1219 sizeof(sect_attrs->grp.attrs[0]));
1220 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001221 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1222 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return;
1224
1225 /* Setup section attributes. */
1226 sect_attrs->grp.name = "sections";
1227 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1228
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001229 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 sattr = &sect_attrs->attrs[0];
1231 gattr = &sect_attrs->grp.attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001232 for (i = 0; i < info->hdr->e_shnum; i++) {
1233 Elf_Shdr *sec = &info->sechdrs[i];
1234 if (sect_empty(sec))
Helge Deller35dead42009-12-03 00:29:15 +01001235 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001236 sattr->address = sec->sh_addr;
1237 sattr->name = kstrdup(info->secstrings + sec->sh_name,
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001238 GFP_KERNEL);
1239 if (sattr->name == NULL)
1240 goto out;
1241 sect_attrs->nsections++;
Eric W. Biederman361795b2010-02-12 13:41:56 -08001242 sysfs_attr_init(&sattr->mattr.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 sattr->mattr.show = module_sect_show;
1244 sattr->mattr.store = NULL;
1245 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 sattr->mattr.attr.mode = S_IRUGO;
1247 *(gattr++) = &(sattr++)->mattr.attr;
1248 }
1249 *gattr = NULL;
1250
1251 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1252 goto out;
1253
1254 mod->sect_attrs = sect_attrs;
1255 return;
1256 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001257 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
1260static void remove_sect_attrs(struct module *mod)
1261{
1262 if (mod->sect_attrs) {
1263 sysfs_remove_group(&mod->mkobj.kobj,
1264 &mod->sect_attrs->grp);
1265 /* We are positive that no one is using any sect attrs
1266 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001267 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 mod->sect_attrs = NULL;
1269 }
1270}
1271
Roland McGrath6d760132007-10-16 23:26:40 -07001272/*
1273 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1274 */
1275
1276struct module_notes_attrs {
1277 struct kobject *dir;
1278 unsigned int notes;
1279 struct bin_attribute attrs[0];
1280};
1281
Chris Wright2c3c8be2010-05-12 18:28:57 -07001282static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
Roland McGrath6d760132007-10-16 23:26:40 -07001283 struct bin_attribute *bin_attr,
1284 char *buf, loff_t pos, size_t count)
1285{
1286 /*
1287 * The caller checked the pos and count against our size.
1288 */
1289 memcpy(buf, bin_attr->private + pos, count);
1290 return count;
1291}
1292
1293static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1294 unsigned int i)
1295{
1296 if (notes_attrs->dir) {
1297 while (i-- > 0)
1298 sysfs_remove_bin_file(notes_attrs->dir,
1299 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001300 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001301 }
1302 kfree(notes_attrs);
1303}
1304
Rusty Russell8f6d0372010-08-05 12:59:09 -06001305static void add_notes_attrs(struct module *mod, const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001306{
1307 unsigned int notes, loaded, i;
1308 struct module_notes_attrs *notes_attrs;
1309 struct bin_attribute *nattr;
1310
Ingo Molnarea6bff32009-08-28 10:44:56 +02001311 /* failed to create section attributes, so can't create notes */
1312 if (!mod->sect_attrs)
1313 return;
1314
Roland McGrath6d760132007-10-16 23:26:40 -07001315 /* Count notes sections and allocate structures. */
1316 notes = 0;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001317 for (i = 0; i < info->hdr->e_shnum; i++)
1318 if (!sect_empty(&info->sechdrs[i]) &&
1319 (info->sechdrs[i].sh_type == SHT_NOTE))
Roland McGrath6d760132007-10-16 23:26:40 -07001320 ++notes;
1321
1322 if (notes == 0)
1323 return;
1324
1325 notes_attrs = kzalloc(sizeof(*notes_attrs)
1326 + notes * sizeof(notes_attrs->attrs[0]),
1327 GFP_KERNEL);
1328 if (notes_attrs == NULL)
1329 return;
1330
1331 notes_attrs->notes = notes;
1332 nattr = &notes_attrs->attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001333 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1334 if (sect_empty(&info->sechdrs[i]))
Roland McGrath6d760132007-10-16 23:26:40 -07001335 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001336 if (info->sechdrs[i].sh_type == SHT_NOTE) {
Eric W. Biederman361795b2010-02-12 13:41:56 -08001337 sysfs_bin_attr_init(nattr);
Roland McGrath6d760132007-10-16 23:26:40 -07001338 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1339 nattr->attr.mode = S_IRUGO;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001340 nattr->size = info->sechdrs[i].sh_size;
1341 nattr->private = (void *) info->sechdrs[i].sh_addr;
Roland McGrath6d760132007-10-16 23:26:40 -07001342 nattr->read = module_notes_read;
1343 ++nattr;
1344 }
1345 ++loaded;
1346 }
1347
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001348 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001349 if (!notes_attrs->dir)
1350 goto out;
1351
1352 for (i = 0; i < notes; ++i)
1353 if (sysfs_create_bin_file(notes_attrs->dir,
1354 &notes_attrs->attrs[i]))
1355 goto out;
1356
1357 mod->notes_attrs = notes_attrs;
1358 return;
1359
1360 out:
1361 free_notes_attrs(notes_attrs, i);
1362}
1363
1364static void remove_notes_attrs(struct module *mod)
1365{
1366 if (mod->notes_attrs)
1367 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1368}
1369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001371
Rusty Russell8f6d0372010-08-05 12:59:09 -06001372static inline void add_sect_attrs(struct module *mod,
1373 const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375}
1376
1377static inline void remove_sect_attrs(struct module *mod)
1378{
1379}
Roland McGrath6d760132007-10-16 23:26:40 -07001380
Rusty Russell8f6d0372010-08-05 12:59:09 -06001381static inline void add_notes_attrs(struct module *mod,
1382 const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001383{
1384}
1385
1386static inline void remove_notes_attrs(struct module *mod)
1387{
1388}
Rusty Russell8f6d0372010-08-05 12:59:09 -06001389#endif /* CONFIG_KALLSYMS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001391static void add_usage_links(struct module *mod)
1392{
1393#ifdef CONFIG_MODULE_UNLOAD
1394 struct module_use *use;
1395 int nowarn;
1396
Rusty Russell75676502010-06-05 11:17:36 -06001397 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001398 list_for_each_entry(use, &mod->target_list, target_list) {
1399 nowarn = sysfs_create_link(use->target->holders_dir,
1400 &mod->mkobj.kobj, mod->name);
1401 }
Rusty Russell75676502010-06-05 11:17:36 -06001402 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001403#endif
1404}
1405
1406static void del_usage_links(struct module *mod)
1407{
1408#ifdef CONFIG_MODULE_UNLOAD
1409 struct module_use *use;
1410
Rusty Russell75676502010-06-05 11:17:36 -06001411 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001412 list_for_each_entry(use, &mod->target_list, target_list)
1413 sysfs_remove_link(use->target->holders_dir, mod->name);
Rusty Russell75676502010-06-05 11:17:36 -06001414 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001415#endif
1416}
1417
Rusty Russell6407ebb22010-06-05 11:17:36 -06001418static int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001419{
1420 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001421 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001422 int error = 0;
1423 int i;
1424
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001425 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1426 (ARRAY_SIZE(modinfo_attrs) + 1)),
1427 GFP_KERNEL);
1428 if (!mod->modinfo_attrs)
1429 return -ENOMEM;
1430
1431 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001432 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1433 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001434 (attr->test && attr->test(mod))) {
1435 memcpy(temp_attr, attr, sizeof(*temp_attr));
Eric W. Biederman361795b2010-02-12 13:41:56 -08001436 sysfs_attr_init(&temp_attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001437 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1438 ++temp_attr;
1439 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001440 }
1441 return error;
1442}
1443
Rusty Russell6407ebb22010-06-05 11:17:36 -06001444static void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001445{
1446 struct module_attribute *attr;
1447 int i;
1448
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001449 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1450 /* pick a field to test for end of list */
1451 if (!attr->attr.name)
1452 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001453 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001454 if (attr->free)
1455 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001456 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001457 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001458}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Rusty Russell6407ebb22010-06-05 11:17:36 -06001460static int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
1462 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001463 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001465 if (!module_sysfs_initialized) {
1466 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001467 mod->name);
1468 err = -EINVAL;
1469 goto out;
1470 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001471
1472 kobj = kset_find_obj(module_kset, mod->name);
1473 if (kobj) {
1474 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1475 kobject_put(kobj);
1476 err = -EINVAL;
1477 goto out;
1478 }
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001481
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001482 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1483 mod->mkobj.kobj.kset = module_kset;
1484 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1485 "%s", mod->name);
1486 if (err)
1487 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001488
Kay Sievers97c146e2007-11-29 23:46:11 +01001489 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001490out:
1491 return err;
1492}
1493
Rusty Russell6407ebb22010-06-05 11:17:36 -06001494static int mod_sysfs_setup(struct module *mod,
Rusty Russell8f6d0372010-08-05 12:59:09 -06001495 const struct load_info *info,
Kay Sievers270a6c42007-01-18 13:26:15 +01001496 struct kernel_param *kparam,
1497 unsigned int num_params)
1498{
1499 int err;
1500
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001501 err = mod_sysfs_init(mod);
1502 if (err)
1503 goto out;
1504
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001505 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001506 if (!mod->holders_dir) {
1507 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001508 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001509 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 err = module_param_sysfs_setup(mod, kparam, num_params);
1512 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001513 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Matt Domschc988d2b2005-06-23 22:05:15 -07001515 err = module_add_modinfo_attrs(mod);
1516 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001517 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001518
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001519 add_usage_links(mod);
Rusty Russell8f6d0372010-08-05 12:59:09 -06001520 add_sect_attrs(mod, info);
1521 add_notes_attrs(mod, info);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001522
Kay Sieverse17e0f52006-11-24 12:15:25 +01001523 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return 0;
1525
Kay Sieverse17e0f52006-11-24 12:15:25 +01001526out_unreg_param:
1527 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001528out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001529 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001530out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001531 kobject_put(&mod->mkobj.kobj);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001532out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return err;
1534}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001535
1536static void mod_sysfs_fini(struct module *mod)
1537{
Rusty Russell8f6d0372010-08-05 12:59:09 -06001538 remove_notes_attrs(mod);
1539 remove_sect_attrs(mod);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001540 kobject_put(&mod->mkobj.kobj);
1541}
1542
Rusty Russell8f6d0372010-08-05 12:59:09 -06001543#else /* !CONFIG_SYSFS */
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001544
Rusty Russell8f6d0372010-08-05 12:59:09 -06001545static int mod_sysfs_setup(struct module *mod,
1546 const struct load_info *info,
Rusty Russell6407ebb22010-06-05 11:17:36 -06001547 struct kernel_param *kparam,
1548 unsigned int num_params)
1549{
1550 return 0;
1551}
1552
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001553static void mod_sysfs_fini(struct module *mod)
1554{
1555}
1556
Rusty Russell36b03602010-08-05 12:59:09 -06001557static void module_remove_modinfo_attrs(struct module *mod)
1558{
1559}
1560
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001561static void del_usage_links(struct module *mod)
1562{
1563}
1564
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001565#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Rusty Russell36b03602010-08-05 12:59:09 -06001567static void mod_sysfs_teardown(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001569 del_usage_links(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001570 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001572 kobject_put(mod->mkobj.drivers_dir);
1573 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001574 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
1577/*
1578 * unlink the module with the whole machine is stopped with interrupts off
1579 * - this defends against kallsyms not taking locks
1580 */
1581static int __unlink_module(void *_mod)
1582{
1583 struct module *mod = _mod;
1584 list_del(&mod->list);
Linus Torvalds53363772010-10-05 11:29:27 -07001585 module_bug_cleanup(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 return 0;
1587}
1588
matthieu castet84e1c6b2010-11-16 22:35:16 +01001589#ifdef CONFIG_DEBUG_SET_MODULE_RONX
1590/*
1591 * LKM RO/NX protection: protect module's text/ro-data
1592 * from modification and any data from execution.
1593 */
1594void set_page_attributes(void *start, void *end, int (*set)(unsigned long start, int num_pages))
1595{
1596 unsigned long begin_pfn = PFN_DOWN((unsigned long)start);
1597 unsigned long end_pfn = PFN_DOWN((unsigned long)end);
1598
1599 if (end_pfn > begin_pfn)
1600 set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1601}
1602
1603static void set_section_ro_nx(void *base,
1604 unsigned long text_size,
1605 unsigned long ro_size,
1606 unsigned long total_size)
1607{
1608 /* begin and end PFNs of the current subsection */
1609 unsigned long begin_pfn;
1610 unsigned long end_pfn;
1611
1612 /*
1613 * Set RO for module text and RO-data:
1614 * - Always protect first page.
1615 * - Do not protect last partial page.
1616 */
1617 if (ro_size > 0)
1618 set_page_attributes(base, base + ro_size, set_memory_ro);
1619
1620 /*
1621 * Set NX permissions for module data:
1622 * - Do not protect first partial page.
1623 * - Always protect last page.
1624 */
1625 if (total_size > text_size) {
1626 begin_pfn = PFN_UP((unsigned long)base + text_size);
1627 end_pfn = PFN_UP((unsigned long)base + total_size);
1628 if (end_pfn > begin_pfn)
1629 set_memory_nx(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1630 }
1631}
1632
Jan Glauber01526ed2011-05-19 16:55:26 -06001633static void unset_module_core_ro_nx(struct module *mod)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001634{
Jan Glauber01526ed2011-05-19 16:55:26 -06001635 set_page_attributes(mod->module_core + mod->core_text_size,
1636 mod->module_core + mod->core_size,
1637 set_memory_x);
1638 set_page_attributes(mod->module_core,
1639 mod->module_core + mod->core_ro_size,
1640 set_memory_rw);
1641}
1642
1643static void unset_module_init_ro_nx(struct module *mod)
1644{
1645 set_page_attributes(mod->module_init + mod->init_text_size,
1646 mod->module_init + mod->init_size,
1647 set_memory_x);
1648 set_page_attributes(mod->module_init,
1649 mod->module_init + mod->init_ro_size,
1650 set_memory_rw);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001651}
1652
1653/* Iterate through all modules and set each module's text as RW */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001654void set_all_modules_text_rw(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001655{
1656 struct module *mod;
1657
1658 mutex_lock(&module_mutex);
1659 list_for_each_entry_rcu(mod, &modules, list) {
1660 if ((mod->module_core) && (mod->core_text_size)) {
1661 set_page_attributes(mod->module_core,
1662 mod->module_core + mod->core_text_size,
1663 set_memory_rw);
1664 }
1665 if ((mod->module_init) && (mod->init_text_size)) {
1666 set_page_attributes(mod->module_init,
1667 mod->module_init + mod->init_text_size,
1668 set_memory_rw);
1669 }
1670 }
1671 mutex_unlock(&module_mutex);
1672}
1673
1674/* Iterate through all modules and set each module's text as RO */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001675void set_all_modules_text_ro(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001676{
1677 struct module *mod;
1678
1679 mutex_lock(&module_mutex);
1680 list_for_each_entry_rcu(mod, &modules, list) {
1681 if ((mod->module_core) && (mod->core_text_size)) {
1682 set_page_attributes(mod->module_core,
1683 mod->module_core + mod->core_text_size,
1684 set_memory_ro);
1685 }
1686 if ((mod->module_init) && (mod->init_text_size)) {
1687 set_page_attributes(mod->module_init,
1688 mod->module_init + mod->init_text_size,
1689 set_memory_ro);
1690 }
1691 }
1692 mutex_unlock(&module_mutex);
1693}
1694#else
1695static inline void set_section_ro_nx(void *base, unsigned long text_size, unsigned long ro_size, unsigned long total_size) { }
Jan Glauber01526ed2011-05-19 16:55:26 -06001696static void unset_module_core_ro_nx(struct module *mod) { }
1697static void unset_module_init_ro_nx(struct module *mod) { }
matthieu castet84e1c6b2010-11-16 22:35:16 +01001698#endif
1699
Jonas Bonn74e08fc2011-06-30 21:22:11 +02001700void __weak module_free(struct module *mod, void *module_region)
1701{
1702 vfree(module_region);
1703}
1704
1705void __weak module_arch_cleanup(struct module *mod)
1706{
1707}
1708
Rusty Russell75676502010-06-05 11:17:36 -06001709/* Free a module, remove from lists, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710static void free_module(struct module *mod)
1711{
Li Zefan7ead8b82009-08-17 16:56:28 +08001712 trace_module_free(mod);
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 /* Delete from various lists */
Rusty Russell75676502010-06-05 11:17:36 -06001715 mutex_lock(&module_mutex);
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001716 stop_machine(__unlink_module, mod, NULL);
Rusty Russell75676502010-06-05 11:17:36 -06001717 mutex_unlock(&module_mutex);
Rusty Russell36b03602010-08-05 12:59:09 -06001718 mod_sysfs_teardown(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Jason Baronb82bab4b2010-07-27 13:18:01 -07001720 /* Remove dynamic debug info */
1721 ddebug_remove_module(mod->name);
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 /* Arch-specific cleanup. */
1724 module_arch_cleanup(mod);
1725
1726 /* Module unload stuff */
1727 module_unload_free(mod);
1728
Rusty Russelle180a6b2009-03-31 13:05:29 -06001729 /* Free any allocated parameters. */
1730 destroy_params(mod->kp, mod->num_kp);
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 /* This may be NULL, but that's OK */
Jan Glauber01526ed2011-05-19 16:55:26 -06001733 unset_module_init_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 module_free(mod, mod->module_init);
1735 kfree(mod->args);
Tejun Heo259354d2010-03-10 18:56:10 +09001736 percpu_modfree(mod);
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001737
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001738 /* Free lock-classes: */
1739 lockdep_free_key_range(mod->module_core, mod->core_size);
1740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 /* Finally, free the core (containing the module structure) */
Jan Glauber01526ed2011-05-19 16:55:26 -06001742 unset_module_core_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 module_free(mod, mod->module_core);
Bernd Schmidteb8cdec2009-09-21 17:03:57 -07001744
1745#ifdef CONFIG_MPU
1746 update_protections(current->mm);
1747#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748}
1749
1750void *__symbol_get(const char *symbol)
1751{
1752 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001753 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Rusty Russell24da1cb2007-07-15 23:41:46 -07001755 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001756 sym = find_symbol(symbol, &owner, NULL, true, true);
1757 if (sym && strong_try_module_get(owner))
1758 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001759 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Tim Abbott414fd312008-12-05 19:03:56 -05001761 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762}
1763EXPORT_SYMBOL_GPL(__symbol_get);
1764
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001765/*
1766 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001767 * in the kernel or in some other module's exported symbol table.
Rusty Russellbe593f42010-06-05 11:17:37 -06001768 *
1769 * You must hold the module_mutex.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001770 */
1771static int verify_export_symbols(struct module *mod)
1772{
Rusty Russellb2111042008-05-01 21:15:00 -05001773 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001774 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001775 const struct kernel_symbol *s;
1776 struct {
1777 const struct kernel_symbol *sym;
1778 unsigned int num;
1779 } arr[] = {
1780 { mod->syms, mod->num_syms },
1781 { mod->gpl_syms, mod->num_gpl_syms },
1782 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001783#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001784 { mod->unused_syms, mod->num_unused_syms },
1785 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001786#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001787 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001788
Rusty Russellb2111042008-05-01 21:15:00 -05001789 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1790 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Rusty Russellbe593f42010-06-05 11:17:37 -06001791 if (find_symbol(s->name, &owner, NULL, true, false)) {
Rusty Russellb2111042008-05-01 21:15:00 -05001792 printk(KERN_ERR
1793 "%s: exports duplicate symbol %s"
1794 " (owned by %s)\n",
1795 mod->name, s->name, module_name(owner));
1796 return -ENOEXEC;
1797 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001798 }
Rusty Russellb2111042008-05-01 21:15:00 -05001799 }
1800 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001801}
1802
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001803/* Change all symbols so that st_value encodes the pointer directly. */
Rusty Russell49668682010-08-05 12:59:10 -06001804static int simplify_symbols(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
Rusty Russell49668682010-08-05 12:59:10 -06001806 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
1807 Elf_Sym *sym = (void *)symsec->sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 unsigned long secbase;
Rusty Russell49668682010-08-05 12:59:10 -06001809 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05001811 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Rusty Russell49668682010-08-05 12:59:10 -06001813 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
1814 const char *name = info->strtab + sym[i].st_name;
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 switch (sym[i].st_shndx) {
1817 case SHN_COMMON:
1818 /* We compiled with -fno-common. These are not
1819 supposed to happen. */
Rusty Russell49668682010-08-05 12:59:10 -06001820 DEBUGP("Common symbol: %s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 printk("%s: please compile with -fno-common\n",
1822 mod->name);
1823 ret = -ENOEXEC;
1824 break;
1825
1826 case SHN_ABS:
1827 /* Don't need to do anything */
1828 DEBUGP("Absolute symbol: 0x%08lx\n",
1829 (long)sym[i].st_value);
1830 break;
1831
1832 case SHN_UNDEF:
Rusty Russell49668682010-08-05 12:59:10 -06001833 ksym = resolve_symbol_wait(mod, info, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 /* Ok if resolved. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001835 if (ksym && !IS_ERR(ksym)) {
Tim Abbott414fd312008-12-05 19:03:56 -05001836 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 break;
Tim Abbott414fd312008-12-05 19:03:56 -05001838 }
1839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 /* Ok if weak. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001841 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 break;
1843
Rusty Russell9bea7f22010-06-05 11:17:37 -06001844 printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
Rusty Russell49668682010-08-05 12:59:10 -06001845 mod->name, name, PTR_ERR(ksym));
Rusty Russell9bea7f22010-06-05 11:17:37 -06001846 ret = PTR_ERR(ksym) ?: -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 break;
1848
1849 default:
1850 /* Divert to percpu allocation if a percpu var. */
Rusty Russell49668682010-08-05 12:59:10 -06001851 if (sym[i].st_shndx == info->index.pcpu)
Tejun Heo259354d2010-03-10 18:56:10 +09001852 secbase = (unsigned long)mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 else
Rusty Russell49668682010-08-05 12:59:10 -06001854 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 sym[i].st_value += secbase;
1856 break;
1857 }
1858 }
1859
1860 return ret;
1861}
1862
Jonas Bonn74e08fc2011-06-30 21:22:11 +02001863int __weak apply_relocate(Elf_Shdr *sechdrs,
1864 const char *strtab,
1865 unsigned int symindex,
1866 unsigned int relsec,
1867 struct module *me)
1868{
1869 pr_err("module %s: REL relocation unsupported\n", me->name);
1870 return -ENOEXEC;
1871}
1872
1873int __weak apply_relocate_add(Elf_Shdr *sechdrs,
1874 const char *strtab,
1875 unsigned int symindex,
1876 unsigned int relsec,
1877 struct module *me)
1878{
1879 pr_err("module %s: RELA relocation unsupported\n", me->name);
1880 return -ENOEXEC;
1881}
1882
Rusty Russell49668682010-08-05 12:59:10 -06001883static int apply_relocations(struct module *mod, const struct load_info *info)
Rusty Russell22e268e2010-08-05 12:59:05 -06001884{
1885 unsigned int i;
1886 int err = 0;
1887
1888 /* Now do relocations. */
Rusty Russell49668682010-08-05 12:59:10 -06001889 for (i = 1; i < info->hdr->e_shnum; i++) {
1890 unsigned int infosec = info->sechdrs[i].sh_info;
Rusty Russell22e268e2010-08-05 12:59:05 -06001891
1892 /* Not a valid relocation section? */
Rusty Russell49668682010-08-05 12:59:10 -06001893 if (infosec >= info->hdr->e_shnum)
Rusty Russell22e268e2010-08-05 12:59:05 -06001894 continue;
1895
1896 /* Don't bother with non-allocated sections */
Rusty Russell49668682010-08-05 12:59:10 -06001897 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
Rusty Russell22e268e2010-08-05 12:59:05 -06001898 continue;
1899
Rusty Russell49668682010-08-05 12:59:10 -06001900 if (info->sechdrs[i].sh_type == SHT_REL)
1901 err = apply_relocate(info->sechdrs, info->strtab,
1902 info->index.sym, i, mod);
1903 else if (info->sechdrs[i].sh_type == SHT_RELA)
1904 err = apply_relocate_add(info->sechdrs, info->strtab,
1905 info->index.sym, i, mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06001906 if (err < 0)
1907 break;
1908 }
1909 return err;
1910}
1911
Helge Deller088af9a2008-12-31 12:31:18 +01001912/* Additional bytes needed by arch in front of individual sections */
1913unsigned int __weak arch_mod_section_prepend(struct module *mod,
1914 unsigned int section)
1915{
1916 /* default implementation just returns zero */
1917 return 0;
1918}
1919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01001921static long get_offset(struct module *mod, unsigned int *size,
1922 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
1924 long ret;
1925
Helge Deller088af9a2008-12-31 12:31:18 +01001926 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1928 *size = ret + sechdr->sh_size;
1929 return ret;
1930}
1931
1932/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1933 might -- code, read-only data, read-write data, small data. Tally
1934 sizes, and place the offsets into sh_entsize fields: high bit means it
1935 belongs in init. */
Rusty Russell49668682010-08-05 12:59:10 -06001936static void layout_sections(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
1938 static unsigned long const masks[][2] = {
1939 /* NOTE: all executable code must be the first section
1940 * in this array; otherwise modify the text_size
1941 * finder in the two loops below */
1942 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1943 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1944 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1945 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1946 };
1947 unsigned int m, i;
1948
Rusty Russell49668682010-08-05 12:59:10 -06001949 for (i = 0; i < info->hdr->e_shnum; i++)
1950 info->sechdrs[i].sh_entsize = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 DEBUGP("Core section allocation order:\n");
1953 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06001954 for (i = 0; i < info->hdr->e_shnum; ++i) {
1955 Elf_Shdr *s = &info->sechdrs[i];
1956 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1959 || (s->sh_flags & masks[m][1])
1960 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06001961 || strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001963 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Rusty Russell49668682010-08-05 12:59:10 -06001964 DEBUGP("\t%s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01001966 switch (m) {
1967 case 0: /* executable */
1968 mod->core_size = debug_align(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 mod->core_text_size = mod->core_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01001970 break;
1971 case 1: /* RO: text and ro-data */
1972 mod->core_size = debug_align(mod->core_size);
1973 mod->core_ro_size = mod->core_size;
1974 break;
1975 case 3: /* whole core */
1976 mod->core_size = debug_align(mod->core_size);
1977 break;
1978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 }
1980
1981 DEBUGP("Init section allocation order:\n");
1982 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06001983 for (i = 0; i < info->hdr->e_shnum; ++i) {
1984 Elf_Shdr *s = &info->sechdrs[i];
1985 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1988 || (s->sh_flags & masks[m][1])
1989 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06001990 || !strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001992 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 | INIT_OFFSET_MASK);
Rusty Russell49668682010-08-05 12:59:10 -06001994 DEBUGP("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01001996 switch (m) {
1997 case 0: /* executable */
1998 mod->init_size = debug_align(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 mod->init_text_size = mod->init_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002000 break;
2001 case 1: /* RO: text and ro-data */
2002 mod->init_size = debug_align(mod->init_size);
2003 mod->init_ro_size = mod->init_size;
2004 break;
2005 case 3: /* whole init */
2006 mod->init_size = debug_align(mod->init_size);
2007 break;
2008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
2010}
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012static void set_license(struct module *mod, const char *license)
2013{
2014 if (!license)
2015 license = "unspecified";
2016
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002017 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07002018 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07002019 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002020 "kernel.\n", mod->name, license);
2021 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 }
2023}
2024
2025/* Parse tag=value strings from .modinfo section */
2026static char *next_string(char *string, unsigned long *secsize)
2027{
2028 /* Skip non-zero chars */
2029 while (string[0]) {
2030 string++;
2031 if ((*secsize)-- <= 1)
2032 return NULL;
2033 }
2034
2035 /* Skip any zero padding. */
2036 while (!string[0]) {
2037 string++;
2038 if ((*secsize)-- <= 1)
2039 return NULL;
2040 }
2041 return string;
2042}
2043
Rusty Russell49668682010-08-05 12:59:10 -06002044static char *get_modinfo(struct load_info *info, const char *tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
2046 char *p;
2047 unsigned int taglen = strlen(tag);
Rusty Russell49668682010-08-05 12:59:10 -06002048 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2049 unsigned long size = infosec->sh_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Rusty Russell49668682010-08-05 12:59:10 -06002051 for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2053 return p + taglen + 1;
2054 }
2055 return NULL;
2056}
2057
Rusty Russell49668682010-08-05 12:59:10 -06002058static void setup_modinfo(struct module *mod, struct load_info *info)
Matt Domschc988d2b2005-06-23 22:05:15 -07002059{
2060 struct module_attribute *attr;
2061 int i;
2062
2063 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2064 if (attr->setup)
Rusty Russell49668682010-08-05 12:59:10 -06002065 attr->setup(mod, get_modinfo(info, attr->attr.name));
Matt Domschc988d2b2005-06-23 22:05:15 -07002066 }
2067}
Matt Domschc988d2b2005-06-23 22:05:15 -07002068
Rusty Russella263f772009-09-25 00:32:58 -06002069static void free_modinfo(struct module *mod)
2070{
2071 struct module_attribute *attr;
2072 int i;
2073
2074 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2075 if (attr->free)
2076 attr->free(mod);
2077 }
2078}
2079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01002081
2082/* lookup symbol in given range of kernel_symbols */
2083static const struct kernel_symbol *lookup_symbol(const char *name,
2084 const struct kernel_symbol *start,
2085 const struct kernel_symbol *stop)
2086{
Alessio Igor Bogani9d634872011-05-18 22:35:59 +02002087 return bsearch(name, start, stop - start,
2088 sizeof(struct kernel_symbol), cmp_name);
WANG Cong15bba372008-07-24 15:41:48 +01002089}
2090
Tim Abbottca4787b2009-01-05 08:40:10 -06002091static int is_exported(const char *name, unsigned long value,
2092 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
Tim Abbottca4787b2009-01-05 08:40:10 -06002094 const struct kernel_symbol *ks;
2095 if (!mod)
2096 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01002097 else
Tim Abbottca4787b2009-01-05 08:40:10 -06002098 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
2099 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
2101
2102/* As per nm */
Rusty Russelleded41c2010-08-05 12:59:07 -06002103static char elf_type(const Elf_Sym *sym, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
Rusty Russelleded41c2010-08-05 12:59:07 -06002105 const Elf_Shdr *sechdrs = info->sechdrs;
2106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2108 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2109 return 'v';
2110 else
2111 return 'w';
2112 }
2113 if (sym->st_shndx == SHN_UNDEF)
2114 return 'U';
2115 if (sym->st_shndx == SHN_ABS)
2116 return 'a';
2117 if (sym->st_shndx >= SHN_LORESERVE)
2118 return '?';
2119 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2120 return 't';
2121 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2122 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2123 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2124 return 'r';
2125 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2126 return 'g';
2127 else
2128 return 'd';
2129 }
2130 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2131 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2132 return 's';
2133 else
2134 return 'b';
2135 }
Rusty Russelleded41c2010-08-05 12:59:07 -06002136 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2137 ".debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 return 'n';
Rusty Russelleded41c2010-08-05 12:59:07 -06002139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 return '?';
2141}
2142
Jan Beulich4a496222009-07-06 14:50:42 +01002143static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
2144 unsigned int shnum)
2145{
2146 const Elf_Shdr *sec;
2147
2148 if (src->st_shndx == SHN_UNDEF
2149 || src->st_shndx >= shnum
2150 || !src->st_name)
2151 return false;
2152
2153 sec = sechdrs + src->st_shndx;
2154 if (!(sec->sh_flags & SHF_ALLOC)
2155#ifndef CONFIG_KALLSYMS_ALL
2156 || !(sec->sh_flags & SHF_EXECINSTR)
2157#endif
2158 || (sec->sh_entsize & INIT_OFFSET_MASK))
2159 return false;
2160
2161 return true;
2162}
2163
Rusty Russell49668682010-08-05 12:59:10 -06002164static void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002165{
Rusty Russell49668682010-08-05 12:59:10 -06002166 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2167 Elf_Shdr *strsect = info->sechdrs + info->index.str;
Jan Beulich4a496222009-07-06 14:50:42 +01002168 const Elf_Sym *src;
2169 unsigned int i, nsrc, ndst;
2170
2171 /* Put symbol section at end of init part of module. */
2172 symsect->sh_flags |= SHF_ALLOC;
2173 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
Rusty Russell49668682010-08-05 12:59:10 -06002174 info->index.sym) | INIT_OFFSET_MASK;
2175 DEBUGP("\t%s\n", info->secstrings + symsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002176
Rusty Russell49668682010-08-05 12:59:10 -06002177 src = (void *)info->hdr + symsect->sh_offset;
Jan Beulich4a496222009-07-06 14:50:42 +01002178 nsrc = symsect->sh_size / sizeof(*src);
2179 for (ndst = i = 1; i < nsrc; ++i, ++src)
Rusty Russell49668682010-08-05 12:59:10 -06002180 if (is_core_symbol(src, info->sechdrs, info->hdr->e_shnum)) {
Jan Beulich554bdfe2009-07-06 14:51:44 +01002181 unsigned int j = src->st_name;
2182
Rusty Russell49668682010-08-05 12:59:10 -06002183 while (!__test_and_set_bit(j, info->strmap)
2184 && info->strtab[j])
Jan Beulich554bdfe2009-07-06 14:51:44 +01002185 ++j;
Jan Beulich4a496222009-07-06 14:50:42 +01002186 ++ndst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002187 }
Jan Beulich4a496222009-07-06 14:50:42 +01002188
2189 /* Append room for core symbols at end of core part. */
Rusty Russell49668682010-08-05 12:59:10 -06002190 info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
2191 mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
Jan Beulich4a496222009-07-06 14:50:42 +01002192
Jan Beulich554bdfe2009-07-06 14:51:44 +01002193 /* Put string table section at end of init part of module. */
2194 strsect->sh_flags |= SHF_ALLOC;
2195 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
Rusty Russell49668682010-08-05 12:59:10 -06002196 info->index.str) | INIT_OFFSET_MASK;
2197 DEBUGP("\t%s\n", info->secstrings + strsect->sh_name);
Jan Beulich554bdfe2009-07-06 14:51:44 +01002198
2199 /* Append room for core symbols' strings at end of core part. */
Rusty Russell49668682010-08-05 12:59:10 -06002200 info->stroffs = mod->core_size;
2201 __set_bit(0, info->strmap);
2202 mod->core_size += bitmap_weight(info->strmap, strsect->sh_size);
Jan Beulich4a496222009-07-06 14:50:42 +01002203}
2204
Rusty Russell811d66a2010-08-05 12:59:12 -06002205static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206{
Jan Beulich4a496222009-07-06 14:50:42 +01002207 unsigned int i, ndst;
2208 const Elf_Sym *src;
2209 Elf_Sym *dst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002210 char *s;
Rusty Russelleded41c2010-08-05 12:59:07 -06002211 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Rusty Russelleded41c2010-08-05 12:59:07 -06002213 mod->symtab = (void *)symsec->sh_addr;
2214 mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
Rusty Russell511ca6a2010-08-05 12:59:08 -06002215 /* Make sure we get permanent strtab: don't use info->strtab. */
2216 mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
2218 /* Set types up while we still have access to sections. */
2219 for (i = 0; i < mod->num_symtab; i++)
Rusty Russelleded41c2010-08-05 12:59:07 -06002220 mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
Jan Beulich4a496222009-07-06 14:50:42 +01002221
Rusty Russelld9131882010-08-05 12:59:08 -06002222 mod->core_symtab = dst = mod->module_core + info->symoffs;
Jan Beulich4a496222009-07-06 14:50:42 +01002223 src = mod->symtab;
2224 *dst = *src;
2225 for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
Rusty Russelleded41c2010-08-05 12:59:07 -06002226 if (!is_core_symbol(src, info->sechdrs, info->hdr->e_shnum))
Jan Beulich4a496222009-07-06 14:50:42 +01002227 continue;
2228 dst[ndst] = *src;
Rusty Russelld9131882010-08-05 12:59:08 -06002229 dst[ndst].st_name = bitmap_weight(info->strmap,
2230 dst[ndst].st_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002231 ++ndst;
2232 }
2233 mod->core_num_syms = ndst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002234
Rusty Russelld9131882010-08-05 12:59:08 -06002235 mod->core_strtab = s = mod->module_core + info->stroffs;
Rusty Russelleded41c2010-08-05 12:59:07 -06002236 for (*s = 0, i = 1; i < info->sechdrs[info->index.str].sh_size; ++i)
Rusty Russelld9131882010-08-05 12:59:08 -06002237 if (test_bit(i, info->strmap))
Jan Beulich554bdfe2009-07-06 14:51:44 +01002238 *++s = mod->strtab[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239}
2240#else
Rusty Russell49668682010-08-05 12:59:10 -06002241static inline void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002242{
2243}
Paul Mundt3ae91c22009-10-01 15:43:54 -07002244
Michał Mirosławabbce902010-09-20 01:58:08 +02002245static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
2247}
2248#endif /* CONFIG_KALLSYMS */
2249
Jason Barone9d376f2009-02-05 11:51:38 -05002250static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05002251{
Rusty Russell811d66a2010-08-05 12:59:12 -06002252 if (!debug)
2253 return;
Jason Barone9d376f2009-02-05 11:51:38 -05002254#ifdef CONFIG_DYNAMIC_DEBUG
2255 if (ddebug_add_module(debug, num, debug->modname))
2256 printk(KERN_ERR "dynamic debug error adding module: %s\n",
2257 debug->modname);
2258#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002259}
Jason Baron346e15b2008-08-12 16:46:19 -04002260
Yehuda Sadehff49d742010-07-03 13:07:35 +10002261static void dynamic_debug_remove(struct _ddebug *debug)
2262{
2263 if (debug)
2264 ddebug_remove_module(debug->modname);
2265}
2266
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002267void * __weak module_alloc(unsigned long size)
2268{
2269 return size == 0 ? NULL : vmalloc_exec(size);
2270}
2271
Rusty Russell3a642e92008-07-22 19:24:28 -05002272static void *module_alloc_update_bounds(unsigned long size)
2273{
2274 void *ret = module_alloc(size);
2275
2276 if (ret) {
Rusty Russell75676502010-06-05 11:17:36 -06002277 mutex_lock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002278 /* Update module bounds. */
2279 if ((unsigned long)ret < module_addr_min)
2280 module_addr_min = (unsigned long)ret;
2281 if ((unsigned long)ret + size > module_addr_max)
2282 module_addr_max = (unsigned long)ret + size;
Rusty Russell75676502010-06-05 11:17:36 -06002283 mutex_unlock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002284 }
2285 return ret;
2286}
2287
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002288#ifdef CONFIG_DEBUG_KMEMLEAK
Rusty Russell49668682010-08-05 12:59:10 -06002289static void kmemleak_load_module(const struct module *mod,
2290 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002291{
2292 unsigned int i;
2293
2294 /* only scan the sections containing data */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002295 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002296
Rusty Russell49668682010-08-05 12:59:10 -06002297 for (i = 1; i < info->hdr->e_shnum; i++) {
2298 const char *name = info->secstrings + info->sechdrs[i].sh_name;
2299 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002300 continue;
Rusty Russell49668682010-08-05 12:59:10 -06002301 if (!strstarts(name, ".data") && !strstarts(name, ".bss"))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002302 continue;
2303
Rusty Russell49668682010-08-05 12:59:10 -06002304 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2305 info->sechdrs[i].sh_size, GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002306 }
2307}
2308#else
Rusty Russell49668682010-08-05 12:59:10 -06002309static inline void kmemleak_load_module(const struct module *mod,
2310 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002311{
2312}
2313#endif
2314
Rusty Russell6526c532010-08-05 12:59:10 -06002315/* Sets info->hdr and info->len. */
Rusty Russelld9131882010-08-05 12:59:08 -06002316static int copy_and_check(struct load_info *info,
2317 const void __user *umod, unsigned long len,
2318 const char __user *uargs)
Rusty Russell40dd2562010-08-05 12:59:03 -06002319{
2320 int err;
2321 Elf_Ehdr *hdr;
2322
2323 if (len < sizeof(*hdr))
2324 return -ENOEXEC;
2325
2326 /* Suck in entire file: we'll want most of it. */
2327 /* vmalloc barfs on "unusual" numbers. Check here */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002328 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
Rusty Russell40dd2562010-08-05 12:59:03 -06002329 return -ENOMEM;
2330
2331 if (copy_from_user(hdr, umod, len) != 0) {
2332 err = -EFAULT;
2333 goto free_hdr;
2334 }
2335
2336 /* Sanity checks against insmoding binaries or wrong arch,
2337 weird elf version */
2338 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
2339 || hdr->e_type != ET_REL
2340 || !elf_check_arch(hdr)
2341 || hdr->e_shentsize != sizeof(Elf_Shdr)) {
2342 err = -ENOEXEC;
2343 goto free_hdr;
2344 }
2345
2346 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) {
2347 err = -ENOEXEC;
2348 goto free_hdr;
2349 }
Rusty Russelld9131882010-08-05 12:59:08 -06002350
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002351 info->hdr = hdr;
2352 info->len = len;
Rusty Russell40dd2562010-08-05 12:59:03 -06002353 return 0;
2354
2355free_hdr:
2356 vfree(hdr);
2357 return err;
2358}
2359
Rusty Russelld9131882010-08-05 12:59:08 -06002360static void free_copy(struct load_info *info)
2361{
Rusty Russelld9131882010-08-05 12:59:08 -06002362 vfree(info->hdr);
2363}
2364
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002365static int rewrite_section_headers(struct load_info *info)
2366{
2367 unsigned int i;
2368
2369 /* This should always be true, but let's be sure. */
2370 info->sechdrs[0].sh_addr = 0;
2371
2372 for (i = 1; i < info->hdr->e_shnum; i++) {
2373 Elf_Shdr *shdr = &info->sechdrs[i];
2374 if (shdr->sh_type != SHT_NOBITS
2375 && info->len < shdr->sh_offset + shdr->sh_size) {
2376 printk(KERN_ERR "Module len %lu truncated\n",
2377 info->len);
2378 return -ENOEXEC;
2379 }
2380
2381 /* Mark all sections sh_addr with their address in the
2382 temporary image. */
2383 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2384
2385#ifndef CONFIG_MODULE_UNLOAD
2386 /* Don't load .exit sections */
2387 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2388 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2389#endif
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002390 }
Rusty Russelld6df72a2010-08-05 12:59:07 -06002391
2392 /* Track but don't keep modinfo and version sections. */
Rusty Russell49668682010-08-05 12:59:10 -06002393 info->index.vers = find_sec(info, "__versions");
2394 info->index.info = find_sec(info, ".modinfo");
Rusty Russelld6df72a2010-08-05 12:59:07 -06002395 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2396 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002397 return 0;
2398}
2399
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002400/*
2401 * Set up our basic convenience variables (pointers to section headers,
2402 * search for module section index etc), and do some basic section
2403 * verification.
2404 *
2405 * Return the temporary module pointer (we'll replace it with the final
2406 * one when we move the module sections around).
2407 */
2408static struct module *setup_load_info(struct load_info *info)
2409{
2410 unsigned int i;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002411 int err;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002412 struct module *mod;
2413
2414 /* Set up the convenience variables */
2415 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002416 info->secstrings = (void *)info->hdr
2417 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002418
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002419 err = rewrite_section_headers(info);
2420 if (err)
2421 return ERR_PTR(err);
2422
2423 /* Find internal symbols and strings. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002424 for (i = 1; i < info->hdr->e_shnum; i++) {
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002425 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2426 info->index.sym = i;
2427 info->index.str = info->sechdrs[i].sh_link;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002428 info->strtab = (char *)info->hdr
2429 + info->sechdrs[info->index.str].sh_offset;
2430 break;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002431 }
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002432 }
2433
Rusty Russell49668682010-08-05 12:59:10 -06002434 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002435 if (!info->index.mod) {
2436 printk(KERN_WARNING "No module found in object\n");
2437 return ERR_PTR(-ENOEXEC);
2438 }
2439 /* This is temporary: point mod into copy of data. */
2440 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2441
2442 if (info->index.sym == 0) {
2443 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2444 mod->name);
2445 return ERR_PTR(-ENOEXEC);
2446 }
2447
Rusty Russell49668682010-08-05 12:59:10 -06002448 info->index.pcpu = find_pcpusec(info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002449
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002450 /* Check module struct version now, before we try to use module. */
2451 if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2452 return ERR_PTR(-ENOEXEC);
2453
2454 return mod;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002455}
2456
Rusty Russell49668682010-08-05 12:59:10 -06002457static int check_modinfo(struct module *mod, struct load_info *info)
Rusty Russell40dd2562010-08-05 12:59:03 -06002458{
Rusty Russell49668682010-08-05 12:59:10 -06002459 const char *modmagic = get_modinfo(info, "vermagic");
Rusty Russell40dd2562010-08-05 12:59:03 -06002460 int err;
2461
2462 /* This is allowed: modprobe --force will invalidate it. */
2463 if (!modmagic) {
2464 err = try_to_force_load(mod, "bad vermagic");
2465 if (err)
2466 return err;
Rusty Russell49668682010-08-05 12:59:10 -06002467 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002468 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2469 mod->name, modmagic, vermagic);
2470 return -ENOEXEC;
2471 }
2472
Rusty Russell49668682010-08-05 12:59:10 -06002473 if (get_modinfo(info, "staging")) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002474 add_taint_module(mod, TAINT_CRAP);
2475 printk(KERN_WARNING "%s: module is from the staging directory,"
2476 " the quality is unknown, you have been warned.\n",
2477 mod->name);
2478 }
Rusty Russell22e268e2010-08-05 12:59:05 -06002479
2480 /* Set up license info based on the info section */
Rusty Russell49668682010-08-05 12:59:10 -06002481 set_license(mod, get_modinfo(info, "license"));
Rusty Russell22e268e2010-08-05 12:59:05 -06002482
Rusty Russell40dd2562010-08-05 12:59:03 -06002483 return 0;
2484}
2485
Rusty Russell811d66a2010-08-05 12:59:12 -06002486static void find_module_sections(struct module *mod, struct load_info *info)
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002487{
Rusty Russell49668682010-08-05 12:59:10 -06002488 mod->kp = section_objs(info, "__param",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002489 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell49668682010-08-05 12:59:10 -06002490 mod->syms = section_objs(info, "__ksymtab",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002491 sizeof(*mod->syms), &mod->num_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002492 mod->crcs = section_addr(info, "__kcrctab");
2493 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002494 sizeof(*mod->gpl_syms),
2495 &mod->num_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002496 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
2497 mod->gpl_future_syms = section_objs(info,
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002498 "__ksymtab_gpl_future",
2499 sizeof(*mod->gpl_future_syms),
2500 &mod->num_gpl_future_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002501 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002502
2503#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell49668682010-08-05 12:59:10 -06002504 mod->unused_syms = section_objs(info, "__ksymtab_unused",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002505 sizeof(*mod->unused_syms),
2506 &mod->num_unused_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002507 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
2508 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002509 sizeof(*mod->unused_gpl_syms),
2510 &mod->num_unused_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002511 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002512#endif
2513#ifdef CONFIG_CONSTRUCTORS
Rusty Russell49668682010-08-05 12:59:10 -06002514 mod->ctors = section_objs(info, ".ctors",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002515 sizeof(*mod->ctors), &mod->num_ctors);
2516#endif
2517
2518#ifdef CONFIG_TRACEPOINTS
Mathieu Desnoyers65498642011-01-26 17:26:22 -05002519 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
2520 sizeof(*mod->tracepoints_ptrs),
2521 &mod->num_tracepoints);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002522#endif
Jason Baronbf5438fc2010-09-17 11:09:00 -04002523#ifdef HAVE_JUMP_LABEL
2524 mod->jump_entries = section_objs(info, "__jump_table",
2525 sizeof(*mod->jump_entries),
2526 &mod->num_jump_entries);
2527#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002528#ifdef CONFIG_EVENT_TRACING
Rusty Russell49668682010-08-05 12:59:10 -06002529 mod->trace_events = section_objs(info, "_ftrace_events",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002530 sizeof(*mod->trace_events),
2531 &mod->num_trace_events);
2532 /*
2533 * This section contains pointers to allocated objects in the trace
2534 * code and not scanning it leads to false positives.
2535 */
2536 kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2537 mod->num_trace_events, GFP_KERNEL);
2538#endif
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05002539#ifdef CONFIG_TRACING
2540 mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
2541 sizeof(*mod->trace_bprintk_fmt_start),
2542 &mod->num_trace_bprintk_fmt);
2543 /*
2544 * This section contains pointers to allocated objects in the trace
2545 * code and not scanning it leads to false positives.
2546 */
2547 kmemleak_scan_area(mod->trace_bprintk_fmt_start,
2548 sizeof(*mod->trace_bprintk_fmt_start) *
2549 mod->num_trace_bprintk_fmt, GFP_KERNEL);
2550#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002551#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2552 /* sechdrs[0].sh_size is always zero */
Rusty Russell49668682010-08-05 12:59:10 -06002553 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002554 sizeof(*mod->ftrace_callsites),
2555 &mod->num_ftrace_callsites);
2556#endif
Rusty Russell22e268e2010-08-05 12:59:05 -06002557
Rusty Russell811d66a2010-08-05 12:59:12 -06002558 mod->extable = section_objs(info, "__ex_table",
2559 sizeof(*mod->extable), &mod->num_exentries);
2560
Rusty Russell49668682010-08-05 12:59:10 -06002561 if (section_addr(info, "__obsparm"))
Rusty Russell22e268e2010-08-05 12:59:05 -06002562 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2563 mod->name);
Rusty Russell811d66a2010-08-05 12:59:12 -06002564
2565 info->debug = section_objs(info, "__verbose",
2566 sizeof(*info->debug), &info->num_debug);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002567}
2568
Rusty Russell49668682010-08-05 12:59:10 -06002569static int move_module(struct module *mod, struct load_info *info)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002570{
2571 int i;
2572 void *ptr;
2573
2574 /* Do the allocs. */
2575 ptr = module_alloc_update_bounds(mod->core_size);
2576 /*
2577 * The pointer to this block is stored in the module structure
2578 * which is inside the block. Just mark it as not being a
2579 * leak.
2580 */
2581 kmemleak_not_leak(ptr);
2582 if (!ptr)
Rusty Russelld9131882010-08-05 12:59:08 -06002583 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002584
2585 memset(ptr, 0, mod->core_size);
2586 mod->module_core = ptr;
2587
2588 ptr = module_alloc_update_bounds(mod->init_size);
2589 /*
2590 * The pointer to this block is stored in the module structure
2591 * which is inside the block. This block doesn't need to be
2592 * scanned as it contains data and code that will be freed
2593 * after the module is initialized.
2594 */
2595 kmemleak_ignore(ptr);
2596 if (!ptr && mod->init_size) {
2597 module_free(mod, mod->module_core);
Rusty Russelld9131882010-08-05 12:59:08 -06002598 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002599 }
2600 memset(ptr, 0, mod->init_size);
2601 mod->module_init = ptr;
2602
2603 /* Transfer each section which specifies SHF_ALLOC */
2604 DEBUGP("final section addresses:\n");
Rusty Russell49668682010-08-05 12:59:10 -06002605 for (i = 0; i < info->hdr->e_shnum; i++) {
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002606 void *dest;
Rusty Russell49668682010-08-05 12:59:10 -06002607 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002608
Rusty Russell49668682010-08-05 12:59:10 -06002609 if (!(shdr->sh_flags & SHF_ALLOC))
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002610 continue;
2611
Rusty Russell49668682010-08-05 12:59:10 -06002612 if (shdr->sh_entsize & INIT_OFFSET_MASK)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002613 dest = mod->module_init
Rusty Russell49668682010-08-05 12:59:10 -06002614 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002615 else
Rusty Russell49668682010-08-05 12:59:10 -06002616 dest = mod->module_core + shdr->sh_entsize;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002617
Rusty Russell49668682010-08-05 12:59:10 -06002618 if (shdr->sh_type != SHT_NOBITS)
2619 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002620 /* Update sh_addr to point to copy in image. */
Rusty Russell49668682010-08-05 12:59:10 -06002621 shdr->sh_addr = (unsigned long)dest;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002622 DEBUGP("\t0x%lx %s\n",
Rusty Russell49668682010-08-05 12:59:10 -06002623 shdr->sh_addr, info->secstrings + shdr->sh_name);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002624 }
Rusty Russelld9131882010-08-05 12:59:08 -06002625
2626 return 0;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002627}
2628
Rusty Russell49668682010-08-05 12:59:10 -06002629static int check_module_license_and_versions(struct module *mod)
Rusty Russell22e268e2010-08-05 12:59:05 -06002630{
2631 /*
2632 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2633 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2634 * using GPL-only symbols it needs.
2635 */
2636 if (strcmp(mod->name, "ndiswrapper") == 0)
2637 add_taint(TAINT_PROPRIETARY_MODULE);
2638
2639 /* driverloader was caught wrongly pretending to be under GPL */
2640 if (strcmp(mod->name, "driverloader") == 0)
2641 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2642
2643#ifdef CONFIG_MODVERSIONS
2644 if ((mod->num_syms && !mod->crcs)
2645 || (mod->num_gpl_syms && !mod->gpl_crcs)
2646 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2647#ifdef CONFIG_UNUSED_SYMBOLS
2648 || (mod->num_unused_syms && !mod->unused_crcs)
2649 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2650#endif
2651 ) {
2652 return try_to_force_load(mod,
2653 "no versions for exported symbols");
2654 }
2655#endif
2656 return 0;
2657}
2658
2659static void flush_module_icache(const struct module *mod)
2660{
2661 mm_segment_t old_fs;
2662
2663 /* flush the icache in correct context */
2664 old_fs = get_fs();
2665 set_fs(KERNEL_DS);
2666
2667 /*
2668 * Flush the instruction cache, since we've played with text.
2669 * Do it before processing of module parameters, so the module
2670 * can provide parameter accessor functions of its own.
2671 */
2672 if (mod->module_init)
2673 flush_icache_range((unsigned long)mod->module_init,
2674 (unsigned long)mod->module_init
2675 + mod->init_size);
2676 flush_icache_range((unsigned long)mod->module_core,
2677 (unsigned long)mod->module_core + mod->core_size);
2678
2679 set_fs(old_fs);
2680}
2681
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002682int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
2683 Elf_Shdr *sechdrs,
2684 char *secstrings,
2685 struct module *mod)
2686{
2687 return 0;
2688}
2689
Rusty Russelld9131882010-08-05 12:59:08 -06002690static struct module *layout_and_allocate(struct load_info *info)
2691{
2692 /* Module within temporary copy. */
2693 struct module *mod;
Rusty Russell49668682010-08-05 12:59:10 -06002694 Elf_Shdr *pcpusec;
Rusty Russelld9131882010-08-05 12:59:08 -06002695 int err;
2696
2697 mod = setup_load_info(info);
2698 if (IS_ERR(mod))
2699 return mod;
2700
Rusty Russell49668682010-08-05 12:59:10 -06002701 err = check_modinfo(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002702 if (err)
2703 return ERR_PTR(err);
2704
2705 /* Allow arches to frob section contents and sizes. */
Rusty Russell49668682010-08-05 12:59:10 -06002706 err = module_frob_arch_sections(info->hdr, info->sechdrs,
2707 info->secstrings, mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002708 if (err < 0)
Rusty Russell6526c532010-08-05 12:59:10 -06002709 goto out;
Rusty Russelld9131882010-08-05 12:59:08 -06002710
Rusty Russell49668682010-08-05 12:59:10 -06002711 pcpusec = &info->sechdrs[info->index.pcpu];
2712 if (pcpusec->sh_size) {
Rusty Russelld9131882010-08-05 12:59:08 -06002713 /* We have a special allocation for this section. */
Rusty Russell49668682010-08-05 12:59:10 -06002714 err = percpu_modalloc(mod,
2715 pcpusec->sh_size, pcpusec->sh_addralign);
Rusty Russelld9131882010-08-05 12:59:08 -06002716 if (err)
Rusty Russell6526c532010-08-05 12:59:10 -06002717 goto out;
Rusty Russell49668682010-08-05 12:59:10 -06002718 pcpusec->sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russelld9131882010-08-05 12:59:08 -06002719 }
2720
2721 /* Determine total sizes, and put offsets in sh_entsize. For now
2722 this is done generically; there doesn't appear to be any
2723 special cases for the architectures. */
Rusty Russell49668682010-08-05 12:59:10 -06002724 layout_sections(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002725
2726 info->strmap = kzalloc(BITS_TO_LONGS(info->sechdrs[info->index.str].sh_size)
2727 * sizeof(long), GFP_KERNEL);
2728 if (!info->strmap) {
2729 err = -ENOMEM;
2730 goto free_percpu;
2731 }
Rusty Russell49668682010-08-05 12:59:10 -06002732 layout_symtab(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002733
2734 /* Allocate and move to the final place */
Rusty Russell49668682010-08-05 12:59:10 -06002735 err = move_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002736 if (err)
2737 goto free_strmap;
2738
2739 /* Module has been copied to its final place now: return it. */
2740 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
Rusty Russell49668682010-08-05 12:59:10 -06002741 kmemleak_load_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002742 return mod;
2743
2744free_strmap:
2745 kfree(info->strmap);
2746free_percpu:
2747 percpu_modfree(mod);
Rusty Russell6526c532010-08-05 12:59:10 -06002748out:
Rusty Russelld9131882010-08-05 12:59:08 -06002749 return ERR_PTR(err);
2750}
2751
2752/* mod is no longer valid after this! */
2753static void module_deallocate(struct module *mod, struct load_info *info)
2754{
2755 kfree(info->strmap);
2756 percpu_modfree(mod);
2757 module_free(mod, mod->module_init);
2758 module_free(mod, mod->module_core);
2759}
2760
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002761int __weak module_finalize(const Elf_Ehdr *hdr,
2762 const Elf_Shdr *sechdrs,
2763 struct module *me)
2764{
2765 return 0;
2766}
2767
Rusty Russell811d66a2010-08-05 12:59:12 -06002768static int post_relocation(struct module *mod, const struct load_info *info)
2769{
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002770 /* Sort exception table now relocations are done. */
Rusty Russell811d66a2010-08-05 12:59:12 -06002771 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2772
2773 /* Copy relocated percpu area over. */
2774 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
2775 info->sechdrs[info->index.pcpu].sh_size);
2776
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002777 /* Setup kallsyms-specific fields. */
Rusty Russell811d66a2010-08-05 12:59:12 -06002778 add_kallsyms(mod, info);
2779
2780 /* Arch-specific module finalizing. */
2781 return module_finalize(info->hdr, info->sechdrs, mod);
2782}
2783
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784/* Allocate and load the module: note that size of section 0 is always
2785 zero, and we rely on this for optional sections. */
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002786static struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 unsigned long len,
2788 const char __user *uargs)
2789{
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002790 struct load_info info = { NULL, };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 struct module *mod;
Rusty Russell40dd2562010-08-05 12:59:03 -06002792 long err;
Paul Mundt3ae91c22009-10-01 15:43:54 -07002793
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
2795 umod, len, uargs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
Rusty Russelld9131882010-08-05 12:59:08 -06002797 /* Copy in the blobs from userspace, check they are vaguely sane. */
2798 err = copy_and_check(&info, umod, len, uargs);
Rusty Russell40dd2562010-08-05 12:59:03 -06002799 if (err)
2800 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
Rusty Russelld9131882010-08-05 12:59:08 -06002802 /* Figure out module layout, and allocate all the memory. */
2803 mod = layout_and_allocate(&info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002804 if (IS_ERR(mod)) {
2805 err = PTR_ERR(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002806 goto free_copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
Rusty Russell49668682010-08-05 12:59:10 -06002809 /* Now module is in final location, initialize linked lists, etc. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -06002810 err = module_unload_init(mod);
2811 if (err)
Rusty Russelld9131882010-08-05 12:59:08 -06002812 goto free_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
Rusty Russell22e268e2010-08-05 12:59:05 -06002814 /* Now we've got everything in the final locations, we can
2815 * find optional sections. */
Rusty Russell49668682010-08-05 12:59:10 -06002816 find_module_sections(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
Rusty Russell49668682010-08-05 12:59:10 -06002818 err = check_module_license_and_versions(mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002819 if (err)
2820 goto free_unload;
Dave Jones9841d61d2006-01-08 01:03:41 -08002821
Matt Domschc988d2b2005-06-23 22:05:15 -07002822 /* Set up MODINFO_ATTR fields */
Rusty Russell49668682010-08-05 12:59:10 -06002823 setup_modinfo(mod, &info);
Matt Domschc988d2b2005-06-23 22:05:15 -07002824
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 /* Fix up syms, so that st_value is a pointer to location. */
Rusty Russell49668682010-08-05 12:59:10 -06002826 err = simplify_symbols(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06002828 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829
Rusty Russell49668682010-08-05 12:59:10 -06002830 err = apply_relocations(mod, &info);
Rusty Russell22e268e2010-08-05 12:59:05 -06002831 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06002832 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
Rusty Russell811d66a2010-08-05 12:59:12 -06002834 err = post_relocation(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06002836 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
Rusty Russell22e268e2010-08-05 12:59:05 -06002838 flush_module_icache(mod);
Thomas Koeller378bac82005-09-06 15:17:11 -07002839
Rusty Russell6526c532010-08-05 12:59:10 -06002840 /* Now copy in args */
2841 mod->args = strndup_user(uargs, ~0UL >> 1);
2842 if (IS_ERR(mod->args)) {
2843 err = PTR_ERR(mod->args);
2844 goto free_arch_cleanup;
2845 }
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002846
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002847 /* Mark state as coming so strong_try_module_get() ignores us. */
Rusty Russelld9131882010-08-05 12:59:08 -06002848 mod->state = MODULE_STATE_COMING;
2849
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002850 /* Now sew it into the lists so we can get lockdep and oops
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002851 * info during argument parsing. No one should access us, since
Andi Kleend72b3752008-08-30 10:09:00 +02002852 * strong_try_module_get() will fail.
2853 * lockdep/oops can run asynchronous, so use the RCU list insertion
2854 * function to insert in a way safe to concurrent readers.
2855 * The mutex protects against concurrent writers.
2856 */
Rusty Russell75676502010-06-05 11:17:36 -06002857 mutex_lock(&module_mutex);
Linus Torvalds3bafeb62010-06-05 11:17:36 -06002858 if (find_module(mod->name)) {
2859 err = -EEXIST;
Rusty Russellbe593f42010-06-05 11:17:37 -06002860 goto unlock;
Linus Torvalds3bafeb62010-06-05 11:17:36 -06002861 }
2862
Rusty Russell811d66a2010-08-05 12:59:12 -06002863 /* This has to be done once we're sure module name is unique. */
Roland Vossen7816c452011-04-07 11:20:58 +02002864 if (!mod->taints || mod->taints == (1U<<TAINT_CRAP))
Rusty Russell811d66a2010-08-05 12:59:12 -06002865 dynamic_debug_setup(info.debug, info.num_debug);
Yehuda Sadehff49d742010-07-03 13:07:35 +10002866
Rusty Russellbe593f42010-06-05 11:17:37 -06002867 /* Find duplicate symbols */
2868 err = verify_export_symbols(mod);
2869 if (err < 0)
Yehuda Sadehff49d742010-07-03 13:07:35 +10002870 goto ddebug;
Rusty Russellbe593f42010-06-05 11:17:37 -06002871
Linus Torvalds53363772010-10-05 11:29:27 -07002872 module_bug_finalize(info.hdr, info.sechdrs, mod);
Andi Kleend72b3752008-08-30 10:09:00 +02002873 list_add_rcu(&mod->list, &modules);
Rusty Russell75676502010-06-05 11:17:36 -06002874 mutex_unlock(&module_mutex);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002875
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002876 /* Module is ready to execute: parsing args may do that. */
Rusty Russelle180a6b2009-03-31 13:05:29 -06002877 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002879 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002881 /* Link in to syfs. */
Rusty Russell8f6d0372010-08-05 12:59:09 -06002882 err = mod_sysfs_setup(mod, &info, mod->kp, mod->num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002884 goto unlink;
Rusty Russell80a3d1b2010-06-05 11:17:36 -06002885
Rusty Russelld9131882010-08-05 12:59:08 -06002886 /* Get rid of temporary copy and strmap. */
2887 kfree(info.strmap);
2888 free_copy(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
2890 /* Done! */
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002891 trace_module_load(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 return mod;
2893
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002894 unlink:
Rusty Russell75676502010-06-05 11:17:36 -06002895 mutex_lock(&module_mutex);
Rusty Russelle91defa2009-03-31 13:05:35 -06002896 /* Unlink carefully: kallsyms could be walking list. */
2897 list_del_rcu(&mod->list);
Linus Torvalds53363772010-10-05 11:29:27 -07002898 module_bug_cleanup(mod);
2899
Yehuda Sadehff49d742010-07-03 13:07:35 +10002900 ddebug:
Roland Vossen7816c452011-04-07 11:20:58 +02002901 if (!mod->taints || mod->taints == (1U<<TAINT_CRAP))
Rusty Russell811d66a2010-08-05 12:59:12 -06002902 dynamic_debug_remove(info.debug);
Rusty Russellbe593f42010-06-05 11:17:37 -06002903 unlock:
Rusty Russell75676502010-06-05 11:17:36 -06002904 mutex_unlock(&module_mutex);
Rusty Russelle91defa2009-03-31 13:05:35 -06002905 synchronize_sched();
Rusty Russell6526c532010-08-05 12:59:10 -06002906 kfree(mod->args);
2907 free_arch_cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 module_arch_cleanup(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002909 free_modinfo:
Rusty Russella263f772009-09-25 00:32:58 -06002910 free_modinfo(mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002911 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 module_unload_free(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002913 free_module:
2914 module_deallocate(mod, &info);
2915 free_copy:
2916 free_copy(&info);
Jayachandran C6fe2e702006-01-06 00:19:54 -08002917 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918}
2919
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002920/* Call module constructors. */
2921static void do_mod_ctors(struct module *mod)
2922{
2923#ifdef CONFIG_CONSTRUCTORS
2924 unsigned long i;
2925
2926 for (i = 0; i < mod->num_ctors; i++)
2927 mod->ctors[i]();
2928#endif
2929}
2930
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931/* This is where the real work happens */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002932SYSCALL_DEFINE3(init_module, void __user *, umod,
2933 unsigned long, len, const char __user *, uargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934{
2935 struct module *mod;
2936 int ret = 0;
2937
2938 /* Must have permission */
Kees Cook3d433212009-04-02 15:49:29 -07002939 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 return -EPERM;
2941
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 /* Do all the hard work */
2943 mod = load_module(umod, len, uargs);
Rusty Russell75676502010-06-05 11:17:36 -06002944 if (IS_ERR(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 return PTR_ERR(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
Alan Sterne041c682006-03-27 01:16:30 -08002947 blocking_notifier_call_chain(&module_notify_list,
2948 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
Steven Rostedt94462ad2010-11-29 13:15:42 -05002950 /* Set RO and NX regions for core */
2951 set_section_ro_nx(mod->module_core,
2952 mod->core_text_size,
2953 mod->core_ro_size,
2954 mod->core_size);
2955
2956 /* Set RO and NX regions for init */
2957 set_section_ro_nx(mod->module_init,
2958 mod->init_text_size,
2959 mod->init_ro_size,
2960 mod->init_size);
2961
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002962 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 /* Start the module */
2964 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002965 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 if (ret < 0) {
2967 /* Init routine failed: abort. Try to protect us from
2968 buggy refcounters. */
2969 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002970 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002971 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002972 blocking_notifier_call_chain(&module_notify_list,
2973 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002974 free_module(mod);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002975 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 return ret;
2977 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002978 if (ret > 0) {
Joe Perchesad361c92009-07-06 13:05:40 -07002979 printk(KERN_WARNING
2980"%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
2981"%s: loading module anyway...\n",
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002982 __func__, mod->name, ret,
2983 __func__);
2984 dump_stack();
2985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
Rusty Russell6c5db222008-03-10 11:43:52 -07002987 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002989 wake_up(&module_wq);
Masami Hiramatsu0deddf432009-01-06 14:41:54 -08002990 blocking_notifier_call_chain(&module_notify_list,
2991 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07002992
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07002993 /* We need to finish all async code before the module init sequence is done */
2994 async_synchronize_full();
2995
Rusty Russell6c5db222008-03-10 11:43:52 -07002996 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 /* Drop initial reference. */
2998 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06002999 trim_init_extable(mod);
Jan Beulich4a496222009-07-06 14:50:42 +01003000#ifdef CONFIG_KALLSYMS
3001 mod->num_symtab = mod->core_num_syms;
3002 mod->symtab = mod->core_symtab;
Jan Beulich554bdfe2009-07-06 14:51:44 +01003003 mod->strtab = mod->core_strtab;
Jan Beulich4a496222009-07-06 14:50:42 +01003004#endif
Jan Glauber01526ed2011-05-19 16:55:26 -06003005 unset_module_init_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 module_free(mod, mod->module_init);
3007 mod->module_init = NULL;
3008 mod->init_size = 0;
Jan Glauber4d103802011-05-19 16:55:25 -06003009 mod->init_ro_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08003011 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
3013 return 0;
3014}
3015
3016static inline int within(unsigned long addr, void *start, unsigned long size)
3017{
3018 return ((void *)addr >= start && (void *)addr < start + size);
3019}
3020
3021#ifdef CONFIG_KALLSYMS
3022/*
3023 * This ignores the intensely annoying "mapping symbols" found
3024 * in ARM ELF files: $a, $t and $d.
3025 */
3026static inline int is_arm_mapping_symbol(const char *str)
3027{
Daniel Walker22a8bde2007-10-18 03:06:07 -07003028 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 && (str[2] == '\0' || str[2] == '.');
3030}
3031
3032static const char *get_ksymbol(struct module *mod,
3033 unsigned long addr,
3034 unsigned long *size,
3035 unsigned long *offset)
3036{
3037 unsigned int i, best = 0;
3038 unsigned long nextval;
3039
3040 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003041 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003043 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 nextval = (unsigned long)mod->module_core+mod->core_text_size;
3045
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003046 /* Scan for closest preceding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07003047 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 for (i = 1; i < mod->num_symtab; i++) {
3049 if (mod->symtab[i].st_shndx == SHN_UNDEF)
3050 continue;
3051
3052 /* We ignore unnamed symbols: they're uninformative
3053 * and inserted at a whim. */
3054 if (mod->symtab[i].st_value <= addr
3055 && mod->symtab[i].st_value > mod->symtab[best].st_value
3056 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3057 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3058 best = i;
3059 if (mod->symtab[i].st_value > addr
3060 && mod->symtab[i].st_value < nextval
3061 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3062 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3063 nextval = mod->symtab[i].st_value;
3064 }
3065
3066 if (!best)
3067 return NULL;
3068
Alexey Dobriyanffb45122007-05-08 00:28:41 -07003069 if (size)
3070 *size = nextval - mod->symtab[best].st_value;
3071 if (offset)
3072 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 return mod->strtab + mod->symtab[best].st_name;
3074}
3075
Rusty Russell6dd06c92008-01-29 17:13:22 -05003076/* For kallsyms to ask for address resolution. NULL means not found. Careful
3077 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003078const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05003079 unsigned long *size,
3080 unsigned long *offset,
3081 char **modname,
3082 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083{
3084 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08003085 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
Rusty Russellcb2a5202008-01-14 00:55:03 -08003087 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003088 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003089 if (within_module_init(addr, mod) ||
3090 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07003091 if (modname)
3092 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08003093 ret = get_ksymbol(mod, addr, size, offset);
3094 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 }
3096 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05003097 /* Make a copy in here where it's safe */
3098 if (ret) {
3099 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
3100 ret = namebuf;
3101 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003102 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003103 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104}
3105
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003106int lookup_module_symbol_name(unsigned long addr, char *symname)
3107{
3108 struct module *mod;
3109
Rusty Russellcb2a5202008-01-14 00:55:03 -08003110 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003111 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003112 if (within_module_init(addr, mod) ||
3113 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003114 const char *sym;
3115
3116 sym = get_ksymbol(mod, addr, NULL, NULL);
3117 if (!sym)
3118 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07003119 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003120 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003121 return 0;
3122 }
3123 }
3124out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003125 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003126 return -ERANGE;
3127}
3128
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003129int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
3130 unsigned long *offset, char *modname, char *name)
3131{
3132 struct module *mod;
3133
Rusty Russellcb2a5202008-01-14 00:55:03 -08003134 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003135 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003136 if (within_module_init(addr, mod) ||
3137 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003138 const char *sym;
3139
3140 sym = get_ksymbol(mod, addr, size, offset);
3141 if (!sym)
3142 goto out;
3143 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07003144 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003145 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07003146 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003147 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003148 return 0;
3149 }
3150 }
3151out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003152 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003153 return -ERANGE;
3154}
3155
Alexey Dobriyanea078902007-05-08 00:28:39 -07003156int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
3157 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158{
3159 struct module *mod;
3160
Rusty Russellcb2a5202008-01-14 00:55:03 -08003161 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003162 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 if (symnum < mod->num_symtab) {
3164 *value = mod->symtab[symnum].st_value;
3165 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07003166 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07003167 KSYM_NAME_LEN);
3168 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06003169 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003170 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003171 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 }
3173 symnum -= mod->num_symtab;
3174 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003175 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003176 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177}
3178
3179static unsigned long mod_find_symname(struct module *mod, const char *name)
3180{
3181 unsigned int i;
3182
3183 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08003184 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
3185 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 return mod->symtab[i].st_value;
3187 return 0;
3188}
3189
3190/* Look for this name: can be of form module:name. */
3191unsigned long module_kallsyms_lookup_name(const char *name)
3192{
3193 struct module *mod;
3194 char *colon;
3195 unsigned long ret = 0;
3196
3197 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08003198 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 if ((colon = strchr(name, ':')) != NULL) {
3200 *colon = '\0';
3201 if ((mod = find_module(name)) != NULL)
3202 ret = mod_find_symname(mod, colon+1);
3203 *colon = ':';
3204 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02003205 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 if ((ret = mod_find_symname(mod, name)) != 0)
3207 break;
3208 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003209 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 return ret;
3211}
Anders Kaseorg75a66612008-12-05 19:03:58 -05003212
3213int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
3214 struct module *, unsigned long),
3215 void *data)
3216{
3217 struct module *mod;
3218 unsigned int i;
3219 int ret;
3220
3221 list_for_each_entry(mod, &modules, list) {
3222 for (i = 0; i < mod->num_symtab; i++) {
3223 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
3224 mod, mod->symtab[i].st_value);
3225 if (ret != 0)
3226 return ret;
3227 }
3228 }
3229 return 0;
3230}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231#endif /* CONFIG_KALLSYMS */
3232
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003233static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003234{
3235 int bx = 0;
3236
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003237 if (mod->taints ||
3238 mod->state == MODULE_STATE_GOING ||
3239 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003240 buf[bx++] = '(';
Andi Kleen25ddbb12008-10-15 22:01:41 -07003241 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003242 buf[bx++] = 'P';
Andi Kleen25ddbb12008-10-15 22:01:41 -07003243 if (mod->taints & (1 << TAINT_FORCED_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003244 buf[bx++] = 'F';
Linus Torvalds26e9a392008-10-17 09:50:12 -07003245 if (mod->taints & (1 << TAINT_CRAP))
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07003246 buf[bx++] = 'C';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003247 /*
3248 * TAINT_FORCED_RMMOD: could be added.
3249 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
3250 * apply to modules.
3251 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003252
3253 /* Show a - for module-is-being-unloaded */
3254 if (mod->state == MODULE_STATE_GOING)
3255 buf[bx++] = '-';
3256 /* Show a + for module-is-being-loaded */
3257 if (mod->state == MODULE_STATE_COMING)
3258 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003259 buf[bx++] = ')';
3260 }
3261 buf[bx] = '\0';
3262
3263 return buf;
3264}
3265
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003266#ifdef CONFIG_PROC_FS
3267/* Called by the /proc file system to return a list of modules. */
3268static void *m_start(struct seq_file *m, loff_t *pos)
3269{
3270 mutex_lock(&module_mutex);
3271 return seq_list_start(&modules, *pos);
3272}
3273
3274static void *m_next(struct seq_file *m, void *p, loff_t *pos)
3275{
3276 return seq_list_next(p, &modules, pos);
3277}
3278
3279static void m_stop(struct seq_file *m, void *p)
3280{
3281 mutex_unlock(&module_mutex);
3282}
3283
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284static int m_show(struct seq_file *m, void *p)
3285{
3286 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003287 char buf[8];
3288
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05003289 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 mod->name, mod->init_size + mod->core_size);
3291 print_unload_info(m, mod);
3292
3293 /* Informative for users. */
3294 seq_printf(m, " %s",
3295 mod->state == MODULE_STATE_GOING ? "Unloading":
3296 mod->state == MODULE_STATE_COMING ? "Loading":
3297 "Live");
3298 /* Used by oprofile and other similar tools. */
Kees Cook9f36e2c2011-03-22 16:34:22 -07003299 seq_printf(m, " 0x%pK", mod->module_core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003301 /* Taints info */
3302 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003303 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003304
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 seq_printf(m, "\n");
3306 return 0;
3307}
3308
3309/* Format: modulename size refcount deps address
3310
3311 Where refcount is a number or -, and deps is a comma-separated list
3312 of depends or -.
3313*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003314static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 .start = m_start,
3316 .next = m_next,
3317 .stop = m_stop,
3318 .show = m_show
3319};
3320
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003321static int modules_open(struct inode *inode, struct file *file)
3322{
3323 return seq_open(file, &modules_op);
3324}
3325
3326static const struct file_operations proc_modules_operations = {
3327 .open = modules_open,
3328 .read = seq_read,
3329 .llseek = seq_lseek,
3330 .release = seq_release,
3331};
3332
3333static int __init proc_modules_init(void)
3334{
3335 proc_create("modules", 0, NULL, &proc_modules_operations);
3336 return 0;
3337}
3338module_init(proc_modules_init);
3339#endif
3340
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341/* Given an address, look for it in the module exception tables. */
3342const struct exception_table_entry *search_module_extables(unsigned long addr)
3343{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 const struct exception_table_entry *e = NULL;
3345 struct module *mod;
3346
Rusty Russell24da1cb2007-07-15 23:41:46 -07003347 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003348 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 if (mod->num_exentries == 0)
3350 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003351
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 e = search_extable(mod->extable,
3353 mod->extable + mod->num_exentries - 1,
3354 addr);
3355 if (e)
3356 break;
3357 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07003358 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
3360 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07003361 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 return e;
3363}
3364
Ingo Molnar4d435f92006-07-03 00:24:24 -07003365/*
Rusty Russelle6104992009-03-31 13:05:31 -06003366 * is_module_address - is this address inside a module?
3367 * @addr: the address to check.
3368 *
3369 * See is_module_text_address() if you simply want to see if the address
3370 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07003371 */
Rusty Russelle6104992009-03-31 13:05:31 -06003372bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07003373{
Rusty Russelle6104992009-03-31 13:05:31 -06003374 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003375
Rusty Russell24da1cb2007-07-15 23:41:46 -07003376 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06003377 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07003378 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07003379
Rusty Russelle6104992009-03-31 13:05:31 -06003380 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003381}
3382
Rusty Russelle6104992009-03-31 13:05:31 -06003383/*
3384 * __module_address - get the module which contains an address.
3385 * @addr: the address.
3386 *
3387 * Must be called with preempt disabled or module mutex held so that
3388 * module doesn't get freed during this.
3389 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07003390struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391{
3392 struct module *mod;
3393
Rusty Russell3a642e92008-07-22 19:24:28 -05003394 if (addr < module_addr_min || addr > module_addr_max)
3395 return NULL;
3396
Andi Kleend72b3752008-08-30 10:09:00 +02003397 list_for_each_entry_rcu(mod, &modules, list)
Rusty Russelle6104992009-03-31 13:05:31 -06003398 if (within_module_core(addr, mod)
3399 || within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 return mod;
3401 return NULL;
3402}
Tim Abbottc6b37802008-12-05 19:03:59 -05003403EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
Rusty Russelle6104992009-03-31 13:05:31 -06003405/*
3406 * is_module_text_address - is this address inside module code?
3407 * @addr: the address to check.
3408 *
3409 * See is_module_address() if you simply want to see if the address is
3410 * anywhere in a module. See kernel_text_address() for testing if an
3411 * address corresponds to kernel or module code.
3412 */
3413bool is_module_text_address(unsigned long addr)
3414{
3415 bool ret;
3416
3417 preempt_disable();
3418 ret = __module_text_address(addr) != NULL;
3419 preempt_enable();
3420
3421 return ret;
3422}
3423
3424/*
3425 * __module_text_address - get the module whose code contains an address.
3426 * @addr: the address.
3427 *
3428 * Must be called with preempt disabled or module mutex held so that
3429 * module doesn't get freed during this.
3430 */
3431struct module *__module_text_address(unsigned long addr)
3432{
3433 struct module *mod = __module_address(addr);
3434 if (mod) {
3435 /* Make sure it's within the text section. */
3436 if (!within(addr, mod->module_init, mod->init_text_size)
3437 && !within(addr, mod->module_core, mod->core_text_size))
3438 mod = NULL;
3439 }
3440 return mod;
3441}
Tim Abbottc6b37802008-12-05 19:03:59 -05003442EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06003443
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444/* Don't grab lock, we're oopsing. */
3445void print_modules(void)
3446{
3447 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07003448 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
Linus Torvaldsb2311252009-06-16 11:07:14 -07003450 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02003451 /* Most callers should already have preempt disabled, but make sure */
3452 preempt_disable();
3453 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003454 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02003455 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01003456 if (last_unloaded_module[0])
3457 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 printk("\n");
3459}
3460
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06003462/* Generate the signature for all relevant module structures here.
3463 * If these change, we don't want to try to parse the module. */
3464void module_layout(struct module *mod,
3465 struct modversion_info *ver,
3466 struct kernel_param *kp,
3467 struct kernel_symbol *ks,
Mathieu Desnoyers65498642011-01-26 17:26:22 -05003468 struct tracepoint * const *tp)
Rusty Russell8c8ef422009-03-31 13:05:34 -06003469{
3470}
3471EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07003473
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04003474#ifdef CONFIG_TRACEPOINTS
3475void module_update_tracepoints(void)
3476{
3477 struct module *mod;
3478
3479 mutex_lock(&module_mutex);
3480 list_for_each_entry(mod, &modules, list)
3481 if (!mod->taints)
Mathieu Desnoyers65498642011-01-26 17:26:22 -05003482 tracepoint_update_probe_range(mod->tracepoints_ptrs,
3483 mod->tracepoints_ptrs + mod->num_tracepoints);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04003484 mutex_unlock(&module_mutex);
3485}
3486
3487/*
3488 * Returns 0 if current not found.
3489 * Returns 1 if current found.
3490 */
3491int module_get_iter_tracepoints(struct tracepoint_iter *iter)
3492{
3493 struct module *iter_mod;
3494 int found = 0;
3495
3496 mutex_lock(&module_mutex);
3497 list_for_each_entry(iter_mod, &modules, list) {
3498 if (!iter_mod->taints) {
3499 /*
3500 * Sorted module list
3501 */
3502 if (iter_mod < iter->module)
3503 continue;
3504 else if (iter_mod > iter->module)
3505 iter->tracepoint = NULL;
3506 found = tracepoint_get_iter_range(&iter->tracepoint,
Mathieu Desnoyers65498642011-01-26 17:26:22 -05003507 iter_mod->tracepoints_ptrs,
3508 iter_mod->tracepoints_ptrs
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04003509 + iter_mod->num_tracepoints);
3510 if (found) {
3511 iter->module = iter_mod;
3512 break;
3513 }
3514 }
3515 }
3516 mutex_unlock(&module_mutex);
3517 return found;
3518}
3519#endif