blob: 4928cffc3dcc3baf1bd1bb3ca91f28fcc34bfcb4 [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*/
Paul Gortmaker9984de12011-05-23 14:51:41 -040019#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#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 symoffs, stroffs;
Rusty Russell811d66a2010-08-05 12:59:12 -0600142 struct _ddebug *debug;
143 unsigned int num_debug;
Rusty Russelleded41c2010-08-05 12:59:07 -0600144 struct {
145 unsigned int sym, str, mod, vers, info, pcpu;
146 } index;
147};
148
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -0800149/* We require a truly strong try_module_get(): 0 means failure due to
150 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static inline int strong_try_module_get(struct module *mod)
152{
153 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500154 return -EBUSY;
155 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500157 else
158 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700161static inline void add_taint_module(struct module *mod, unsigned flag)
162{
163 add_taint(flag);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700164 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700165}
166
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200167/*
168 * A thread that wants to hold a reference to a module only while it
169 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 */
171void __module_put_and_exit(struct module *mod, long code)
172{
173 module_put(mod);
174 do_exit(code);
175}
176EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/* Find a module section: 0 means not found. */
Rusty Russell49668682010-08-05 12:59:10 -0600179static unsigned int find_sec(const struct load_info *info, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 unsigned int i;
182
Rusty Russell49668682010-08-05 12:59:10 -0600183 for (i = 1; i < info->hdr->e_shnum; i++) {
184 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 /* Alloc bit cleared means "ignore it." */
Rusty Russell49668682010-08-05 12:59:10 -0600186 if ((shdr->sh_flags & SHF_ALLOC)
187 && strcmp(info->secstrings + shdr->sh_name, name) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return i;
Rusty Russell49668682010-08-05 12:59:10 -0600189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return 0;
191}
192
Rusty Russell5e458cc2008-10-22 10:00:13 -0500193/* Find a module section, or NULL. */
Rusty Russell49668682010-08-05 12:59:10 -0600194static void *section_addr(const struct load_info *info, const char *name)
Rusty Russell5e458cc2008-10-22 10:00:13 -0500195{
196 /* Section 0 has sh_addr 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600197 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500198}
199
200/* Find a module section, or NULL. Fill in number of "objects" in section. */
Rusty Russell49668682010-08-05 12:59:10 -0600201static void *section_objs(const struct load_info *info,
Rusty Russell5e458cc2008-10-22 10:00:13 -0500202 const char *name,
203 size_t object_size,
204 unsigned int *num)
205{
Rusty Russell49668682010-08-05 12:59:10 -0600206 unsigned int sec = find_sec(info, name);
Rusty Russell5e458cc2008-10-22 10:00:13 -0500207
208 /* Section 0 has sh_addr 0 and sh_size 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600209 *num = info->sechdrs[sec].sh_size / object_size;
210 return (void *)info->sechdrs[sec].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500211}
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/* Provided by the linker */
214extern const struct kernel_symbol __start___ksymtab[];
215extern const struct kernel_symbol __stop___ksymtab[];
216extern const struct kernel_symbol __start___ksymtab_gpl[];
217extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800218extern const struct kernel_symbol __start___ksymtab_gpl_future[];
219extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220extern const unsigned long __start___kcrctab[];
221extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800222extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500223#ifdef CONFIG_UNUSED_SYMBOLS
224extern const struct kernel_symbol __start___ksymtab_unused[];
225extern const struct kernel_symbol __stop___ksymtab_unused[];
226extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
227extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700228extern const unsigned long __start___kcrctab_unused[];
229extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500230#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232#ifndef CONFIG_MODVERSIONS
233#define symversion(base, idx) NULL
234#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800235#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236#endif
237
Rusty Russelldafd0942008-07-22 19:24:25 -0500238static bool each_symbol_in_section(const struct symsearch *arr,
239 unsigned int arrsize,
240 struct module *owner,
241 bool (*fn)(const struct symsearch *syms,
242 struct module *owner,
Rusty Russellde4d8d52011-04-19 21:49:58 +0200243 void *data),
Rusty Russelldafd0942008-07-22 19:24:25 -0500244 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100245{
Rusty Russellde4d8d52011-04-19 21:49:58 +0200246 unsigned int j;
Rusty Russelldafd0942008-07-22 19:24:25 -0500247
248 for (j = 0; j < arrsize; j++) {
Rusty Russellde4d8d52011-04-19 21:49:58 +0200249 if (fn(&arr[j], owner, data))
250 return true;
Rusty Russelldafd0942008-07-22 19:24:25 -0500251 }
252
253 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100254}
255
Rusty Russelldafd0942008-07-22 19:24:25 -0500256/* Returns true as soon as fn returns true, otherwise false. */
Rusty Russellde4d8d52011-04-19 21:49:58 +0200257bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
258 struct module *owner,
259 void *data),
260 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700261{
Rusty Russelldafd0942008-07-22 19:24:25 -0500262 struct module *mod;
Linus Torvalds44032e62010-08-05 12:59:05 -0600263 static const struct symsearch arr[] = {
Rusty Russelldafd0942008-07-22 19:24:25 -0500264 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
265 NOT_GPL_ONLY, false },
266 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
267 __start___kcrctab_gpl,
268 GPL_ONLY, false },
269 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
270 __start___kcrctab_gpl_future,
271 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500272#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500273 { __start___ksymtab_unused, __stop___ksymtab_unused,
274 __start___kcrctab_unused,
275 NOT_GPL_ONLY, true },
276 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
277 __start___kcrctab_unused_gpl,
278 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500279#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500280 };
281
282 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
283 return true;
284
Andi Kleend72b3752008-08-30 10:09:00 +0200285 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500286 struct symsearch arr[] = {
287 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
288 NOT_GPL_ONLY, false },
289 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
290 mod->gpl_crcs,
291 GPL_ONLY, false },
292 { mod->gpl_future_syms,
293 mod->gpl_future_syms + mod->num_gpl_future_syms,
294 mod->gpl_future_crcs,
295 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500296#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500297 { mod->unused_syms,
298 mod->unused_syms + mod->num_unused_syms,
299 mod->unused_crcs,
300 NOT_GPL_ONLY, true },
301 { mod->unused_gpl_syms,
302 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
303 mod->unused_gpl_crcs,
304 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500305#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500306 };
307
308 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
309 return true;
310 }
311 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700312}
Rusty Russellde4d8d52011-04-19 21:49:58 +0200313EXPORT_SYMBOL_GPL(each_symbol_section);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700314
Rusty Russelldafd0942008-07-22 19:24:25 -0500315struct find_symbol_arg {
316 /* Input */
317 const char *name;
318 bool gplok;
319 bool warn;
320
321 /* Output */
322 struct module *owner;
323 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500324 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500325};
326
Rusty Russellde4d8d52011-04-19 21:49:58 +0200327static bool check_symbol(const struct symsearch *syms,
328 struct module *owner,
329 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500330{
Rusty Russelldafd0942008-07-22 19:24:25 -0500331 struct find_symbol_arg *fsa = data;
332
Rusty Russelldafd0942008-07-22 19:24:25 -0500333 if (!fsa->gplok) {
334 if (syms->licence == GPL_ONLY)
335 return false;
336 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
337 printk(KERN_WARNING "Symbol %s is being used "
338 "by a non-GPL module, which will not "
339 "be allowed in the future\n", fsa->name);
340 printk(KERN_WARNING "Please see the file "
341 "Documentation/feature-removal-schedule.txt "
342 "in the kernel source tree for more details.\n");
343 }
344 }
345
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500346#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500347 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500348 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500349 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500350 printk(KERN_WARNING
351 "This symbol will go away in the future.\n");
352 printk(KERN_WARNING
353 "Please evalute if this is the right api to use and if "
354 "it really is, submit a report the linux kernel "
355 "mailinglist together with submitting your code for "
356 "inclusion.\n");
357 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500358#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500359
360 fsa->owner = owner;
361 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500362 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500363 return true;
364}
365
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200366static int cmp_name(const void *va, const void *vb)
367{
368 const char *a;
369 const struct kernel_symbol *b;
370 a = va; b = vb;
371 return strcmp(a, b->name);
372}
373
Rusty Russellde4d8d52011-04-19 21:49:58 +0200374static bool find_symbol_in_section(const struct symsearch *syms,
375 struct module *owner,
376 void *data)
377{
378 struct find_symbol_arg *fsa = data;
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200379 struct kernel_symbol *sym;
Rusty Russellde4d8d52011-04-19 21:49:58 +0200380
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200381 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
382 sizeof(struct kernel_symbol), cmp_name);
383
384 if (sym != NULL && check_symbol(syms, owner, sym - syms->start, data))
385 return true;
386
Rusty Russellde4d8d52011-04-19 21:49:58 +0200387 return false;
388}
389
Tim Abbott414fd312008-12-05 19:03:56 -0500390/* Find a symbol and return it, along with, (optional) crc and
Rusty Russell75676502010-06-05 11:17:36 -0600391 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500392const struct kernel_symbol *find_symbol(const char *name,
393 struct module **owner,
394 const unsigned long **crc,
395 bool gplok,
396 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Rusty Russelldafd0942008-07-22 19:24:25 -0500398 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Rusty Russelldafd0942008-07-22 19:24:25 -0500400 fsa.name = name;
401 fsa.gplok = gplok;
402 fsa.warn = warn;
403
Rusty Russellde4d8d52011-04-19 21:49:58 +0200404 if (each_symbol_section(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500405 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500406 *owner = fsa.owner;
407 if (crc)
408 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500409 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 DEBUGP("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500413 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
Tim Abbottc6b37802008-12-05 19:03:59 -0500415EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417/* Search for module by name: must hold module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500418struct module *find_module(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 struct module *mod;
421
422 list_for_each_entry(mod, &modules, list) {
423 if (strcmp(mod->name, name) == 0)
424 return mod;
425 }
426 return NULL;
427}
Tim Abbottc6b37802008-12-05 19:03:59 -0500428EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900431
Tejun Heo259354d2010-03-10 18:56:10 +0900432static inline void __percpu *mod_percpu(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900433{
Tejun Heo259354d2010-03-10 18:56:10 +0900434 return mod->percpu;
435}
Tejun Heofbf59bc2009-02-20 16:29:08 +0900436
Tejun Heo259354d2010-03-10 18:56:10 +0900437static int percpu_modalloc(struct module *mod,
438 unsigned long size, unsigned long align)
439{
Tejun Heofbf59bc2009-02-20 16:29:08 +0900440 if (align > PAGE_SIZE) {
441 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
Tejun Heo259354d2010-03-10 18:56:10 +0900442 mod->name, align, PAGE_SIZE);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900443 align = PAGE_SIZE;
444 }
445
Tejun Heo259354d2010-03-10 18:56:10 +0900446 mod->percpu = __alloc_reserved_percpu(size, align);
447 if (!mod->percpu) {
Tejun Heofbf59bc2009-02-20 16:29:08 +0900448 printk(KERN_WARNING
Rusty Russelld9131882010-08-05 12:59:08 -0600449 "%s: Could not allocate %lu bytes percpu data\n",
450 mod->name, size);
Tejun Heo259354d2010-03-10 18:56:10 +0900451 return -ENOMEM;
452 }
453 mod->percpu_size = size;
454 return 0;
Tejun Heofbf59bc2009-02-20 16:29:08 +0900455}
456
Tejun Heo259354d2010-03-10 18:56:10 +0900457static void percpu_modfree(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900458{
Tejun Heo259354d2010-03-10 18:56:10 +0900459 free_percpu(mod->percpu);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900460}
461
Rusty Russell49668682010-08-05 12:59:10 -0600462static unsigned int find_pcpusec(struct load_info *info)
Tejun Heo6b588c12009-02-20 16:29:07 +0900463{
Rusty Russell49668682010-08-05 12:59:10 -0600464 return find_sec(info, ".data..percpu");
Tejun Heo6b588c12009-02-20 16:29:07 +0900465}
466
Tejun Heo259354d2010-03-10 18:56:10 +0900467static void percpu_modcopy(struct module *mod,
468 const void *from, unsigned long size)
Tejun Heo6b588c12009-02-20 16:29:07 +0900469{
470 int cpu;
471
472 for_each_possible_cpu(cpu)
Tejun Heo259354d2010-03-10 18:56:10 +0900473 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
Tejun Heo6b588c12009-02-20 16:29:07 +0900474}
475
Tejun Heo10fad5e2010-03-10 18:57:54 +0900476/**
477 * is_module_percpu_address - test whether address is from module static percpu
478 * @addr: address to test
479 *
480 * Test whether @addr belongs to module static percpu area.
481 *
482 * RETURNS:
483 * %true if @addr is from module static percpu area
484 */
485bool is_module_percpu_address(unsigned long addr)
486{
487 struct module *mod;
488 unsigned int cpu;
489
490 preempt_disable();
491
492 list_for_each_entry_rcu(mod, &modules, list) {
493 if (!mod->percpu_size)
494 continue;
495 for_each_possible_cpu(cpu) {
496 void *start = per_cpu_ptr(mod->percpu, cpu);
497
498 if ((void *)addr >= start &&
499 (void *)addr < start + mod->percpu_size) {
500 preempt_enable();
501 return true;
502 }
503 }
504 }
505
506 preempt_enable();
507 return false;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700508}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900511
Tejun Heo259354d2010-03-10 18:56:10 +0900512static inline void __percpu *mod_percpu(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 return NULL;
515}
Tejun Heo259354d2010-03-10 18:56:10 +0900516static inline int percpu_modalloc(struct module *mod,
517 unsigned long size, unsigned long align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Tejun Heo259354d2010-03-10 18:56:10 +0900519 return -ENOMEM;
520}
521static inline void percpu_modfree(struct module *mod)
522{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
Rusty Russell49668682010-08-05 12:59:10 -0600524static unsigned int find_pcpusec(struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 return 0;
527}
Tejun Heo259354d2010-03-10 18:56:10 +0900528static inline void percpu_modcopy(struct module *mod,
529 const void *from, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
531 /* pcpusec should be 0, and size of that section should be 0. */
532 BUG_ON(size != 0);
533}
Tejun Heo10fad5e2010-03-10 18:57:54 +0900534bool is_module_percpu_address(unsigned long addr)
535{
536 return false;
537}
Tejun Heo6b588c12009-02-20 16:29:07 +0900538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539#endif /* CONFIG_SMP */
540
Matt Domschc988d2b2005-06-23 22:05:15 -0700541#define MODINFO_ATTR(field) \
542static void setup_modinfo_##field(struct module *mod, const char *s) \
543{ \
544 mod->field = kstrdup(s, GFP_KERNEL); \
545} \
546static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
Kay Sievers4befb022011-07-24 22:06:04 +0930547 struct module_kobject *mk, char *buffer) \
Matt Domschc988d2b2005-06-23 22:05:15 -0700548{ \
Kay Sievers4befb022011-07-24 22:06:04 +0930549 return sprintf(buffer, "%s\n", mk->mod->field); \
Matt Domschc988d2b2005-06-23 22:05:15 -0700550} \
551static int modinfo_##field##_exists(struct module *mod) \
552{ \
553 return mod->field != NULL; \
554} \
555static void free_modinfo_##field(struct module *mod) \
556{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700557 kfree(mod->field); \
558 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700559} \
560static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900561 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700562 .show = show_modinfo_##field, \
563 .setup = setup_modinfo_##field, \
564 .test = modinfo_##field##_exists, \
565 .free = free_modinfo_##field, \
566};
567
568MODINFO_ATTR(version);
569MODINFO_ATTR(srcversion);
570
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100571static char last_unloaded_module[MODULE_NAME_LEN+1];
572
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800573#ifdef CONFIG_MODULE_UNLOAD
Steven Rostedteb0c5372010-03-29 14:25:18 -0400574
575EXPORT_TRACEPOINT_SYMBOL(module_get);
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577/* Init the unload section of the module. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600578static int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600580 mod->refptr = alloc_percpu(struct module_ref);
581 if (!mod->refptr)
582 return -ENOMEM;
583
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700584 INIT_LIST_HEAD(&mod->source_list);
585 INIT_LIST_HEAD(&mod->target_list);
Christoph Lametere1783a22010-01-05 15:34:50 +0900586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /* Hold reference count during initialization. */
Nick Piggin5fbfb182010-04-01 19:09:40 +1100588 __this_cpu_write(mod->refptr->incs, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /* Backwards compatibility macros put refcount during init. */
590 mod->waiter = current;
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600591
592 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595/* Does a already use b? */
596static int already_uses(struct module *a, struct module *b)
597{
598 struct module_use *use;
599
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700600 list_for_each_entry(use, &b->source_list, source_list) {
601 if (use->source == a) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 DEBUGP("%s uses %s!\n", a->name, b->name);
603 return 1;
604 }
605 }
606 DEBUGP("%s does not use %s!\n", a->name, b->name);
607 return 0;
608}
609
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700610/*
611 * Module a uses b
612 * - we add 'a' as a "source", 'b' as a "target" of module use
613 * - the module_use is added to the list of 'b' sources (so
614 * 'b' can walk the list to see who sourced them), and of 'a'
615 * targets (so 'a' can see what modules it targets).
616 */
617static int add_module_usage(struct module *a, struct module *b)
618{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700619 struct module_use *use;
620
621 DEBUGP("Allocating new usage for %s.\n", a->name);
622 use = kmalloc(sizeof(*use), GFP_ATOMIC);
623 if (!use) {
624 printk(KERN_WARNING "%s: out of memory loading\n", a->name);
625 return -ENOMEM;
626 }
627
628 use->source = a;
629 use->target = b;
630 list_add(&use->source_list, &b->source_list);
631 list_add(&use->target_list, &a->target_list);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700632 return 0;
633}
634
Rusty Russell75676502010-06-05 11:17:36 -0600635/* Module a uses b: caller needs module_mutex() */
Rusty Russell9bea7f22010-06-05 11:17:37 -0600636int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Rusty Russellc8e21ce2010-06-05 11:17:35 -0600638 int err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100639
Rusty Russell9bea7f22010-06-05 11:17:37 -0600640 if (b == NULL || already_uses(a, b))
Linus Torvalds218ce732010-05-25 16:48:30 -0700641 return 0;
Linus Torvalds218ce732010-05-25 16:48:30 -0700642
Rusty Russell9bea7f22010-06-05 11:17:37 -0600643 /* If module isn't available, we fail. */
644 err = strong_try_module_get(b);
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500645 if (err)
Rusty Russell9bea7f22010-06-05 11:17:37 -0600646 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700648 err = add_module_usage(a, b);
649 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 module_put(b);
Rusty Russell9bea7f22010-06-05 11:17:37 -0600651 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
Rusty Russell9bea7f22010-06-05 11:17:37 -0600653 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600655EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657/* Clear the unload stuff of the module. */
658static void module_unload_free(struct module *mod)
659{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700660 struct module_use *use, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Rusty Russell75676502010-06-05 11:17:36 -0600662 mutex_lock(&module_mutex);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700663 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
664 struct module *i = use->target;
665 DEBUGP("%s unusing %s\n", mod->name, i->name);
666 module_put(i);
667 list_del(&use->source_list);
668 list_del(&use->target_list);
669 kfree(use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
Rusty Russell75676502010-06-05 11:17:36 -0600671 mutex_unlock(&module_mutex);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600672
673 free_percpu(mod->refptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
676#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800677static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
679 int ret = (flags & O_TRUNC);
680 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800681 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return ret;
683}
684#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800685static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 return 0;
688}
689#endif /* CONFIG_MODULE_FORCE_UNLOAD */
690
691struct stopref
692{
693 struct module *mod;
694 int flags;
695 int *forced;
696};
697
698/* Whole machine is stopped with interrupts off when this runs. */
699static int __try_stop_module(void *_sref)
700{
701 struct stopref *sref = _sref;
702
Rusty Russellda39ba52008-07-22 19:24:25 -0500703 /* If it's not unused, quit unless we're forcing. */
704 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800705 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return -EWOULDBLOCK;
707 }
708
709 /* Mark it as dying. */
710 sref->mod->state = MODULE_STATE_GOING;
711 return 0;
712}
713
714static int try_stop_module(struct module *mod, int flags, int *forced)
715{
Rusty Russellda39ba52008-07-22 19:24:25 -0500716 if (flags & O_NONBLOCK) {
717 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500719 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500720 } else {
721 /* We don't need to stop the machine for this. */
722 mod->state = MODULE_STATE_GOING;
723 synchronize_sched();
724 return 0;
725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
728unsigned int module_refcount(struct module *mod)
729{
Nick Piggin5fbfb182010-04-01 19:09:40 +1100730 unsigned int incs = 0, decs = 0;
Eric Dumazet720eba32009-02-03 13:31:36 +1030731 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Eric Dumazet720eba32009-02-03 13:31:36 +1030733 for_each_possible_cpu(cpu)
Nick Piggin5fbfb182010-04-01 19:09:40 +1100734 decs += per_cpu_ptr(mod->refptr, cpu)->decs;
735 /*
736 * ensure the incs are added up after the decs.
737 * module_put ensures incs are visible before decs with smp_wmb.
738 *
739 * This 2-count scheme avoids the situation where the refcount
740 * for CPU0 is read, then CPU0 increments the module refcount,
741 * then CPU1 drops that refcount, then the refcount for CPU1 is
742 * read. We would record a decrement but not its corresponding
743 * increment so we would see a low count (disaster).
744 *
745 * Rare situation? But module_refcount can be preempted, and we
746 * might be tallying up 4096+ CPUs. So it is not impossible.
747 */
748 smp_rmb();
749 for_each_possible_cpu(cpu)
750 incs += per_cpu_ptr(mod->refptr, cpu)->incs;
751 return incs - decs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753EXPORT_SYMBOL(module_refcount);
754
755/* This exists whether we can unload or not */
756static void free_module(struct module *mod);
757
758static void wait_for_zero_refcount(struct module *mod)
759{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500760 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800761 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 for (;;) {
763 DEBUGP("Looking at refcount...\n");
764 set_current_state(TASK_UNINTERRUPTIBLE);
765 if (module_refcount(mod) == 0)
766 break;
767 schedule();
768 }
769 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800770 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100773SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
774 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
776 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800777 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 int ret, forced = 0;
779
Kees Cook3d433212009-04-02 15:49:29 -0700780 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800781 return -EPERM;
782
783 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
784 return -EFAULT;
785 name[MODULE_NAME_LEN-1] = '\0';
786
Tejun Heo3fc1f1e2010-05-06 18:49:20 +0200787 if (mutex_lock_interruptible(&module_mutex) != 0)
788 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 mod = find_module(name);
791 if (!mod) {
792 ret = -ENOENT;
793 goto out;
794 }
795
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700796 if (!list_empty(&mod->source_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 /* Other modules depend on us: get rid of them first. */
798 ret = -EWOULDBLOCK;
799 goto out;
800 }
801
802 /* Doing init or already dying? */
803 if (mod->state != MODULE_STATE_LIVE) {
804 /* FIXME: if (force), slam module count and wake up
805 waiter --RR */
806 DEBUGP("%s already dying\n", mod->name);
807 ret = -EBUSY;
808 goto out;
809 }
810
811 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700812 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800813 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (!forced) {
815 /* This module can't be removed */
816 ret = -EBUSY;
817 goto out;
818 }
819 }
820
821 /* Set this up before setting mod->state */
822 mod->waiter = current;
823
824 /* Stop the machine so refcounts can't move and disable module. */
825 ret = try_stop_module(mod, flags, &forced);
826 if (ret != 0)
827 goto out;
828
829 /* Never wait if forced. */
830 if (!forced && module_refcount(mod) != 0)
831 wait_for_zero_refcount(mod);
832
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200833 mutex_unlock(&module_mutex);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300834 /* Final destruction now no one is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200835 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200837 blocking_notifier_call_chain(&module_notify_list,
838 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800839 async_synchronize_full();
Rusty Russell75676502010-06-05 11:17:36 -0600840
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100841 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500842 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Rusty Russell75676502010-06-05 11:17:36 -0600844 free_module(mod);
845 return 0;
846out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800847 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return ret;
849}
850
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800851static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 struct module_use *use;
854 int printed_something = 0;
855
856 seq_printf(m, " %u ", module_refcount(mod));
857
858 /* Always include a trailing , so userspace can differentiate
859 between this and the old multi-field proc format. */
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700860 list_for_each_entry(use, &mod->source_list, source_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 printed_something = 1;
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700862 seq_printf(m, "%s,", use->source->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (mod->init != NULL && mod->exit == NULL) {
866 printed_something = 1;
867 seq_printf(m, "[permanent],");
868 }
869
870 if (!printed_something)
871 seq_printf(m, "-");
872}
873
874void __symbol_put(const char *symbol)
875{
876 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Rusty Russell24da1cb2007-07-15 23:41:46 -0700878 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -0500879 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 BUG();
881 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700882 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884EXPORT_SYMBOL(__symbol_put);
885
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930886/* Note this assumes addr is a function, which it currently always is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887void symbol_put_addr(void *addr)
888{
Trent Piepho5e376612006-05-15 09:44:06 -0700889 struct module *modaddr;
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930890 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930892 if (core_kernel_text(a))
Trent Piepho5e376612006-05-15 09:44:06 -0700893 return;
894
Rusty Russella6e6abd2009-03-31 13:05:31 -0600895 /* module_text_address is safe here: we're supposed to have reference
896 * to module from symbol_get, so it can't go away. */
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930897 modaddr = __module_text_address(a);
Rusty Russella6e6abd2009-03-31 13:05:31 -0600898 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -0700899 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901EXPORT_SYMBOL_GPL(symbol_put_addr);
902
903static ssize_t show_refcnt(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +0930904 struct module_kobject *mk, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Kay Sievers4befb022011-07-24 22:06:04 +0930906 return sprintf(buffer, "%u\n", module_refcount(mk->mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
909static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900910 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 .show = show_refcnt,
912};
913
Al Virof6a57032006-10-18 01:47:25 -0400914void module_put(struct module *module)
915{
916 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +0900917 preempt_disable();
Nick Piggin5fbfb182010-04-01 19:09:40 +1100918 smp_wmb(); /* see comment in module_refcount */
919 __this_cpu_inc(module->refptr->decs);
Christoph Lametere1783a22010-01-05 15:34:50 +0900920
Li Zefanae832d12010-03-24 10:57:43 +0800921 trace_module_put(module, _RET_IP_);
Al Virof6a57032006-10-18 01:47:25 -0400922 /* Maybe they're waiting for us to drop reference? */
923 if (unlikely(!module_is_live(module)))
924 wake_up_process(module->waiter);
Christoph Lametere1783a22010-01-05 15:34:50 +0900925 preempt_enable();
Al Virof6a57032006-10-18 01:47:25 -0400926 }
927}
928EXPORT_SYMBOL(module_put);
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800931static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
933 /* We don't know the usage count, or what modules are using. */
934 seq_printf(m, " - -");
935}
936
937static inline void module_unload_free(struct module *mod)
938{
939}
940
Rusty Russell9bea7f22010-06-05 11:17:37 -0600941int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Rusty Russell9bea7f22010-06-05 11:17:37 -0600943 return strong_try_module_get(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600945EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600947static inline int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600949 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
951#endif /* CONFIG_MODULE_UNLOAD */
952
Kay Sievers1f717402006-11-24 12:15:25 +0100953static ssize_t show_initstate(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +0930954 struct module_kobject *mk, char *buffer)
Kay Sievers1f717402006-11-24 12:15:25 +0100955{
956 const char *state = "unknown";
957
Kay Sievers4befb022011-07-24 22:06:04 +0930958 switch (mk->mod->state) {
Kay Sievers1f717402006-11-24 12:15:25 +0100959 case MODULE_STATE_LIVE:
960 state = "live";
961 break;
962 case MODULE_STATE_COMING:
963 state = "coming";
964 break;
965 case MODULE_STATE_GOING:
966 state = "going";
967 break;
968 }
969 return sprintf(buffer, "%s\n", state);
970}
971
972static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900973 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100974 .show = show_initstate,
975};
976
Kay Sievers88bfa322011-07-24 22:06:04 +0930977static ssize_t store_uevent(struct module_attribute *mattr,
978 struct module_kobject *mk,
979 const char *buffer, size_t count)
980{
981 enum kobject_action action;
982
983 if (kobject_action_type(buffer, count, &action) == 0)
984 kobject_uevent(&mk->kobj, action);
985 return count;
986}
987
988struct module_attribute module_uevent = {
989 .attr = { .name = "uevent", .mode = 0200 },
990 .store = store_uevent,
991};
992
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800993static struct module_attribute *modinfo_attrs[] = {
994 &modinfo_version,
995 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100996 &initstate,
Kay Sievers88bfa322011-07-24 22:06:04 +0930997 &module_uevent,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800998#ifdef CONFIG_MODULE_UNLOAD
999 &refcnt,
1000#endif
1001 NULL,
1002};
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004static const char vermagic[] = VERMAGIC_STRING;
1005
Rusty Russellc6e665c2009-03-31 13:05:33 -06001006static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -07001007{
1008#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -07001009 if (!test_taint(TAINT_FORCED_MODULE))
Rusty Russellc6e665c2009-03-31 13:05:33 -06001010 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
1011 mod->name, reason);
Linus Torvalds826e4502008-05-04 17:04:16 -07001012 add_taint_module(mod, TAINT_FORCED_MODULE);
1013 return 0;
1014#else
1015 return -ENOEXEC;
1016#endif
1017}
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019#ifdef CONFIG_MODVERSIONS
Rusty Russelld4703ae2009-12-15 16:28:32 -06001020/* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
1021static unsigned long maybe_relocated(unsigned long crc,
1022 const struct module *crc_owner)
1023{
1024#ifdef ARCH_RELOCATES_KCRCTAB
1025 if (crc_owner == NULL)
1026 return crc - (unsigned long)reloc_start;
1027#endif
1028 return crc;
1029}
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031static int check_version(Elf_Shdr *sechdrs,
1032 unsigned int versindex,
1033 const char *symname,
1034 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001035 const unsigned long *crc,
1036 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
1038 unsigned int i, num_versions;
1039 struct modversion_info *versions;
1040
1041 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1042 if (!crc)
1043 return 1;
1044
Rusty Russella5dd6972008-05-09 16:24:21 +10001045 /* No versions at all? modprobe --force does this. */
1046 if (versindex == 0)
1047 return try_to_force_load(mod, symname) == 0;
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 versions = (void *) sechdrs[versindex].sh_addr;
1050 num_versions = sechdrs[versindex].sh_size
1051 / sizeof(struct modversion_info);
1052
1053 for (i = 0; i < num_versions; i++) {
1054 if (strcmp(versions[i].name, symname) != 0)
1055 continue;
1056
Rusty Russelld4703ae2009-12-15 16:28:32 -06001057 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 DEBUGP("Found checksum %lX vs module %lX\n",
Rusty Russelld4703ae2009-12-15 16:28:32 -06001060 maybe_relocated(*crc, crc_owner), versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001061 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001063
Rusty Russella5dd6972008-05-09 16:24:21 +10001064 printk(KERN_WARNING "%s: no symbol version for %s\n",
1065 mod->name, symname);
1066 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001067
1068bad_version:
1069 printk("%s: disagrees about version of symbol %s\n",
1070 mod->name, symname);
1071 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
1074static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1075 unsigned int versindex,
1076 struct module *mod)
1077{
1078 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Rusty Russell75676502010-06-05 11:17:36 -06001080 /* Since this should be found in kernel (which can't be removed),
1081 * no locking is necessary. */
Mike Frysinger6560dc12009-07-23 23:42:08 +09301082 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1083 &crc, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 BUG();
Rusty Russelld4703ae2009-12-15 16:28:32 -06001085 return check_version(sechdrs, versindex, "module_layout", mod, crc,
1086 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
Rusty Russell91e37a72008-05-09 16:25:28 +10001089/* First part is kernel version, which we ignore if module has crcs. */
1090static inline int same_magic(const char *amagic, const char *bmagic,
1091 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Rusty Russell91e37a72008-05-09 16:25:28 +10001093 if (has_crcs) {
1094 amagic += strcspn(amagic, " ");
1095 bmagic += strcspn(bmagic, " ");
1096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return strcmp(amagic, bmagic) == 0;
1098}
1099#else
1100static inline int check_version(Elf_Shdr *sechdrs,
1101 unsigned int versindex,
1102 const char *symname,
1103 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001104 const unsigned long *crc,
1105 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
1107 return 1;
1108}
1109
1110static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1111 unsigned int versindex,
1112 struct module *mod)
1113{
1114 return 1;
1115}
1116
Rusty Russell91e37a72008-05-09 16:25:28 +10001117static inline int same_magic(const char *amagic, const char *bmagic,
1118 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
1120 return strcmp(amagic, bmagic) == 0;
1121}
1122#endif /* CONFIG_MODVERSIONS */
1123
Rusty Russell75676502010-06-05 11:17:36 -06001124/* Resolve a symbol for this module. I.e. if we find one, record usage. */
Rusty Russell49668682010-08-05 12:59:10 -06001125static const struct kernel_symbol *resolve_symbol(struct module *mod,
1126 const struct load_info *info,
Tim Abbott414fd312008-12-05 19:03:56 -05001127 const char *name,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001128 char ownername[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
1130 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001131 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 const unsigned long *crc;
Rusty Russell9bea7f22010-06-05 11:17:37 -06001133 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Rusty Russell75676502010-06-05 11:17:36 -06001135 mutex_lock(&module_mutex);
Tim Abbott414fd312008-12-05 19:03:56 -05001136 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001137 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001138 if (!sym)
1139 goto unlock;
1140
Rusty Russell49668682010-08-05 12:59:10 -06001141 if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
1142 owner)) {
Rusty Russell9bea7f22010-06-05 11:17:37 -06001143 sym = ERR_PTR(-EINVAL);
1144 goto getname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
Rusty Russell9bea7f22010-06-05 11:17:37 -06001146
1147 err = ref_module(mod, owner);
1148 if (err) {
1149 sym = ERR_PTR(err);
1150 goto getname;
1151 }
1152
1153getname:
1154 /* We must make copy under the lock if we failed to get ref. */
1155 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1156unlock:
Rusty Russell75676502010-06-05 11:17:36 -06001157 mutex_unlock(&module_mutex);
Linus Torvalds218ce732010-05-25 16:48:30 -07001158 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160
Rusty Russell49668682010-08-05 12:59:10 -06001161static const struct kernel_symbol *
1162resolve_symbol_wait(struct module *mod,
1163 const struct load_info *info,
1164 const char *name)
Rusty Russell9bea7f22010-06-05 11:17:37 -06001165{
1166 const struct kernel_symbol *ksym;
Rusty Russell49668682010-08-05 12:59:10 -06001167 char owner[MODULE_NAME_LEN];
Rusty Russell9bea7f22010-06-05 11:17:37 -06001168
1169 if (wait_event_interruptible_timeout(module_wq,
Rusty Russell49668682010-08-05 12:59:10 -06001170 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1171 || PTR_ERR(ksym) != -EBUSY,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001172 30 * HZ) <= 0) {
1173 printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
Rusty Russell49668682010-08-05 12:59:10 -06001174 mod->name, owner);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001175 }
1176 return ksym;
1177}
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179/*
1180 * /sys/module/foo/sections stuff
1181 * J. Corbet <corbet@lwn.net>
1182 */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001183#ifdef CONFIG_SYSFS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001184
Rusty Russell8f6d0372010-08-05 12:59:09 -06001185#ifdef CONFIG_KALLSYMS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001186static inline bool sect_empty(const Elf_Shdr *sect)
1187{
1188 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1189}
1190
Rusty Russella58730c2008-03-13 09:03:44 +00001191struct module_sect_attr
1192{
1193 struct module_attribute mattr;
1194 char *name;
1195 unsigned long address;
1196};
1197
1198struct module_sect_attrs
1199{
1200 struct attribute_group grp;
1201 unsigned int nsections;
1202 struct module_sect_attr attrs[0];
1203};
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205static ssize_t module_sect_show(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +09301206 struct module_kobject *mk, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
1208 struct module_sect_attr *sattr =
1209 container_of(mattr, struct module_sect_attr, mattr);
Kees Cook9f36e2c2011-03-22 16:34:22 -07001210 return sprintf(buf, "0x%pK\n", (void *)sattr->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
1212
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001213static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1214{
Rusty Russella58730c2008-03-13 09:03:44 +00001215 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001216
1217 for (section = 0; section < sect_attrs->nsections; section++)
1218 kfree(sect_attrs->attrs[section].name);
1219 kfree(sect_attrs);
1220}
1221
Rusty Russell8f6d0372010-08-05 12:59:09 -06001222static void add_sect_attrs(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
1224 unsigned int nloaded = 0, i, size[2];
1225 struct module_sect_attrs *sect_attrs;
1226 struct module_sect_attr *sattr;
1227 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 /* Count loaded sections and allocate structures */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001230 for (i = 0; i < info->hdr->e_shnum; i++)
1231 if (!sect_empty(&info->sechdrs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 nloaded++;
1233 size[0] = ALIGN(sizeof(*sect_attrs)
1234 + nloaded * sizeof(sect_attrs->attrs[0]),
1235 sizeof(sect_attrs->grp.attrs[0]));
1236 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001237 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1238 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return;
1240
1241 /* Setup section attributes. */
1242 sect_attrs->grp.name = "sections";
1243 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1244
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001245 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 sattr = &sect_attrs->attrs[0];
1247 gattr = &sect_attrs->grp.attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001248 for (i = 0; i < info->hdr->e_shnum; i++) {
1249 Elf_Shdr *sec = &info->sechdrs[i];
1250 if (sect_empty(sec))
Helge Deller35dead42009-12-03 00:29:15 +01001251 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001252 sattr->address = sec->sh_addr;
1253 sattr->name = kstrdup(info->secstrings + sec->sh_name,
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001254 GFP_KERNEL);
1255 if (sattr->name == NULL)
1256 goto out;
1257 sect_attrs->nsections++;
Eric W. Biederman361795b2010-02-12 13:41:56 -08001258 sysfs_attr_init(&sattr->mattr.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 sattr->mattr.show = module_sect_show;
1260 sattr->mattr.store = NULL;
1261 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 sattr->mattr.attr.mode = S_IRUGO;
1263 *(gattr++) = &(sattr++)->mattr.attr;
1264 }
1265 *gattr = NULL;
1266
1267 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1268 goto out;
1269
1270 mod->sect_attrs = sect_attrs;
1271 return;
1272 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001273 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
1276static void remove_sect_attrs(struct module *mod)
1277{
1278 if (mod->sect_attrs) {
1279 sysfs_remove_group(&mod->mkobj.kobj,
1280 &mod->sect_attrs->grp);
1281 /* We are positive that no one is using any sect attrs
1282 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001283 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 mod->sect_attrs = NULL;
1285 }
1286}
1287
Roland McGrath6d760132007-10-16 23:26:40 -07001288/*
1289 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1290 */
1291
1292struct module_notes_attrs {
1293 struct kobject *dir;
1294 unsigned int notes;
1295 struct bin_attribute attrs[0];
1296};
1297
Chris Wright2c3c8be2010-05-12 18:28:57 -07001298static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
Roland McGrath6d760132007-10-16 23:26:40 -07001299 struct bin_attribute *bin_attr,
1300 char *buf, loff_t pos, size_t count)
1301{
1302 /*
1303 * The caller checked the pos and count against our size.
1304 */
1305 memcpy(buf, bin_attr->private + pos, count);
1306 return count;
1307}
1308
1309static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1310 unsigned int i)
1311{
1312 if (notes_attrs->dir) {
1313 while (i-- > 0)
1314 sysfs_remove_bin_file(notes_attrs->dir,
1315 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001316 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001317 }
1318 kfree(notes_attrs);
1319}
1320
Rusty Russell8f6d0372010-08-05 12:59:09 -06001321static void add_notes_attrs(struct module *mod, const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001322{
1323 unsigned int notes, loaded, i;
1324 struct module_notes_attrs *notes_attrs;
1325 struct bin_attribute *nattr;
1326
Ingo Molnarea6bff32009-08-28 10:44:56 +02001327 /* failed to create section attributes, so can't create notes */
1328 if (!mod->sect_attrs)
1329 return;
1330
Roland McGrath6d760132007-10-16 23:26:40 -07001331 /* Count notes sections and allocate structures. */
1332 notes = 0;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001333 for (i = 0; i < info->hdr->e_shnum; i++)
1334 if (!sect_empty(&info->sechdrs[i]) &&
1335 (info->sechdrs[i].sh_type == SHT_NOTE))
Roland McGrath6d760132007-10-16 23:26:40 -07001336 ++notes;
1337
1338 if (notes == 0)
1339 return;
1340
1341 notes_attrs = kzalloc(sizeof(*notes_attrs)
1342 + notes * sizeof(notes_attrs->attrs[0]),
1343 GFP_KERNEL);
1344 if (notes_attrs == NULL)
1345 return;
1346
1347 notes_attrs->notes = notes;
1348 nattr = &notes_attrs->attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001349 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1350 if (sect_empty(&info->sechdrs[i]))
Roland McGrath6d760132007-10-16 23:26:40 -07001351 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001352 if (info->sechdrs[i].sh_type == SHT_NOTE) {
Eric W. Biederman361795b2010-02-12 13:41:56 -08001353 sysfs_bin_attr_init(nattr);
Roland McGrath6d760132007-10-16 23:26:40 -07001354 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1355 nattr->attr.mode = S_IRUGO;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001356 nattr->size = info->sechdrs[i].sh_size;
1357 nattr->private = (void *) info->sechdrs[i].sh_addr;
Roland McGrath6d760132007-10-16 23:26:40 -07001358 nattr->read = module_notes_read;
1359 ++nattr;
1360 }
1361 ++loaded;
1362 }
1363
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001364 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001365 if (!notes_attrs->dir)
1366 goto out;
1367
1368 for (i = 0; i < notes; ++i)
1369 if (sysfs_create_bin_file(notes_attrs->dir,
1370 &notes_attrs->attrs[i]))
1371 goto out;
1372
1373 mod->notes_attrs = notes_attrs;
1374 return;
1375
1376 out:
1377 free_notes_attrs(notes_attrs, i);
1378}
1379
1380static void remove_notes_attrs(struct module *mod)
1381{
1382 if (mod->notes_attrs)
1383 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1384}
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001387
Rusty Russell8f6d0372010-08-05 12:59:09 -06001388static inline void add_sect_attrs(struct module *mod,
1389 const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391}
1392
1393static inline void remove_sect_attrs(struct module *mod)
1394{
1395}
Roland McGrath6d760132007-10-16 23:26:40 -07001396
Rusty Russell8f6d0372010-08-05 12:59:09 -06001397static inline void add_notes_attrs(struct module *mod,
1398 const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001399{
1400}
1401
1402static inline void remove_notes_attrs(struct module *mod)
1403{
1404}
Rusty Russell8f6d0372010-08-05 12:59:09 -06001405#endif /* CONFIG_KALLSYMS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001407static void add_usage_links(struct module *mod)
1408{
1409#ifdef CONFIG_MODULE_UNLOAD
1410 struct module_use *use;
1411 int nowarn;
1412
Rusty Russell75676502010-06-05 11:17:36 -06001413 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001414 list_for_each_entry(use, &mod->target_list, target_list) {
1415 nowarn = sysfs_create_link(use->target->holders_dir,
1416 &mod->mkobj.kobj, mod->name);
1417 }
Rusty Russell75676502010-06-05 11:17:36 -06001418 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001419#endif
1420}
1421
1422static void del_usage_links(struct module *mod)
1423{
1424#ifdef CONFIG_MODULE_UNLOAD
1425 struct module_use *use;
1426
Rusty Russell75676502010-06-05 11:17:36 -06001427 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001428 list_for_each_entry(use, &mod->target_list, target_list)
1429 sysfs_remove_link(use->target->holders_dir, mod->name);
Rusty Russell75676502010-06-05 11:17:36 -06001430 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001431#endif
1432}
1433
Rusty Russell6407ebb22010-06-05 11:17:36 -06001434static int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001435{
1436 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001437 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001438 int error = 0;
1439 int i;
1440
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001441 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1442 (ARRAY_SIZE(modinfo_attrs) + 1)),
1443 GFP_KERNEL);
1444 if (!mod->modinfo_attrs)
1445 return -ENOMEM;
1446
1447 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001448 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1449 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001450 (attr->test && attr->test(mod))) {
1451 memcpy(temp_attr, attr, sizeof(*temp_attr));
Eric W. Biederman361795b2010-02-12 13:41:56 -08001452 sysfs_attr_init(&temp_attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001453 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1454 ++temp_attr;
1455 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001456 }
1457 return error;
1458}
1459
Rusty Russell6407ebb22010-06-05 11:17:36 -06001460static void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001461{
1462 struct module_attribute *attr;
1463 int i;
1464
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001465 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1466 /* pick a field to test for end of list */
1467 if (!attr->attr.name)
1468 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001469 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001470 if (attr->free)
1471 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001472 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001473 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001474}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Rusty Russell6407ebb22010-06-05 11:17:36 -06001476static int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
1478 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001479 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001481 if (!module_sysfs_initialized) {
1482 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001483 mod->name);
1484 err = -EINVAL;
1485 goto out;
1486 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001487
1488 kobj = kset_find_obj(module_kset, mod->name);
1489 if (kobj) {
1490 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1491 kobject_put(kobj);
1492 err = -EINVAL;
1493 goto out;
1494 }
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001497
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001498 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1499 mod->mkobj.kobj.kset = module_kset;
1500 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1501 "%s", mod->name);
1502 if (err)
1503 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001504
Kay Sievers97c146e2007-11-29 23:46:11 +01001505 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001506out:
1507 return err;
1508}
1509
Rusty Russell6407ebb22010-06-05 11:17:36 -06001510static int mod_sysfs_setup(struct module *mod,
Rusty Russell8f6d0372010-08-05 12:59:09 -06001511 const struct load_info *info,
Kay Sievers270a6c42007-01-18 13:26:15 +01001512 struct kernel_param *kparam,
1513 unsigned int num_params)
1514{
1515 int err;
1516
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001517 err = mod_sysfs_init(mod);
1518 if (err)
1519 goto out;
1520
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001521 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001522 if (!mod->holders_dir) {
1523 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001524 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001525 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 err = module_param_sysfs_setup(mod, kparam, num_params);
1528 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001529 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Matt Domschc988d2b2005-06-23 22:05:15 -07001531 err = module_add_modinfo_attrs(mod);
1532 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001533 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001534
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001535 add_usage_links(mod);
Rusty Russell8f6d0372010-08-05 12:59:09 -06001536 add_sect_attrs(mod, info);
1537 add_notes_attrs(mod, info);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001538
Kay Sieverse17e0f52006-11-24 12:15:25 +01001539 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 return 0;
1541
Kay Sieverse17e0f52006-11-24 12:15:25 +01001542out_unreg_param:
1543 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001544out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001545 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001546out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001547 kobject_put(&mod->mkobj.kobj);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001548out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return err;
1550}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001551
1552static void mod_sysfs_fini(struct module *mod)
1553{
Rusty Russell8f6d0372010-08-05 12:59:09 -06001554 remove_notes_attrs(mod);
1555 remove_sect_attrs(mod);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001556 kobject_put(&mod->mkobj.kobj);
1557}
1558
Rusty Russell8f6d0372010-08-05 12:59:09 -06001559#else /* !CONFIG_SYSFS */
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001560
Rusty Russell8f6d0372010-08-05 12:59:09 -06001561static int mod_sysfs_setup(struct module *mod,
1562 const struct load_info *info,
Rusty Russell6407ebb22010-06-05 11:17:36 -06001563 struct kernel_param *kparam,
1564 unsigned int num_params)
1565{
1566 return 0;
1567}
1568
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001569static void mod_sysfs_fini(struct module *mod)
1570{
1571}
1572
Rusty Russell36b03602010-08-05 12:59:09 -06001573static void module_remove_modinfo_attrs(struct module *mod)
1574{
1575}
1576
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001577static void del_usage_links(struct module *mod)
1578{
1579}
1580
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001581#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Rusty Russell36b03602010-08-05 12:59:09 -06001583static void mod_sysfs_teardown(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001585 del_usage_links(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001586 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001588 kobject_put(mod->mkobj.drivers_dir);
1589 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001590 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
1593/*
1594 * unlink the module with the whole machine is stopped with interrupts off
1595 * - this defends against kallsyms not taking locks
1596 */
1597static int __unlink_module(void *_mod)
1598{
1599 struct module *mod = _mod;
1600 list_del(&mod->list);
Linus Torvalds53363772010-10-05 11:29:27 -07001601 module_bug_cleanup(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 return 0;
1603}
1604
matthieu castet84e1c6b2010-11-16 22:35:16 +01001605#ifdef CONFIG_DEBUG_SET_MODULE_RONX
1606/*
1607 * LKM RO/NX protection: protect module's text/ro-data
1608 * from modification and any data from execution.
1609 */
1610void set_page_attributes(void *start, void *end, int (*set)(unsigned long start, int num_pages))
1611{
1612 unsigned long begin_pfn = PFN_DOWN((unsigned long)start);
1613 unsigned long end_pfn = PFN_DOWN((unsigned long)end);
1614
1615 if (end_pfn > begin_pfn)
1616 set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1617}
1618
1619static void set_section_ro_nx(void *base,
1620 unsigned long text_size,
1621 unsigned long ro_size,
1622 unsigned long total_size)
1623{
1624 /* begin and end PFNs of the current subsection */
1625 unsigned long begin_pfn;
1626 unsigned long end_pfn;
1627
1628 /*
1629 * Set RO for module text and RO-data:
1630 * - Always protect first page.
1631 * - Do not protect last partial page.
1632 */
1633 if (ro_size > 0)
1634 set_page_attributes(base, base + ro_size, set_memory_ro);
1635
1636 /*
1637 * Set NX permissions for module data:
1638 * - Do not protect first partial page.
1639 * - Always protect last page.
1640 */
1641 if (total_size > text_size) {
1642 begin_pfn = PFN_UP((unsigned long)base + text_size);
1643 end_pfn = PFN_UP((unsigned long)base + total_size);
1644 if (end_pfn > begin_pfn)
1645 set_memory_nx(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1646 }
1647}
1648
Jan Glauber01526ed2011-05-19 16:55:26 -06001649static void unset_module_core_ro_nx(struct module *mod)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001650{
Jan Glauber01526ed2011-05-19 16:55:26 -06001651 set_page_attributes(mod->module_core + mod->core_text_size,
1652 mod->module_core + mod->core_size,
1653 set_memory_x);
1654 set_page_attributes(mod->module_core,
1655 mod->module_core + mod->core_ro_size,
1656 set_memory_rw);
1657}
1658
1659static void unset_module_init_ro_nx(struct module *mod)
1660{
1661 set_page_attributes(mod->module_init + mod->init_text_size,
1662 mod->module_init + mod->init_size,
1663 set_memory_x);
1664 set_page_attributes(mod->module_init,
1665 mod->module_init + mod->init_ro_size,
1666 set_memory_rw);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001667}
1668
1669/* Iterate through all modules and set each module's text as RW */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001670void set_all_modules_text_rw(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001671{
1672 struct module *mod;
1673
1674 mutex_lock(&module_mutex);
1675 list_for_each_entry_rcu(mod, &modules, list) {
1676 if ((mod->module_core) && (mod->core_text_size)) {
1677 set_page_attributes(mod->module_core,
1678 mod->module_core + mod->core_text_size,
1679 set_memory_rw);
1680 }
1681 if ((mod->module_init) && (mod->init_text_size)) {
1682 set_page_attributes(mod->module_init,
1683 mod->module_init + mod->init_text_size,
1684 set_memory_rw);
1685 }
1686 }
1687 mutex_unlock(&module_mutex);
1688}
1689
1690/* Iterate through all modules and set each module's text as RO */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001691void set_all_modules_text_ro(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001692{
1693 struct module *mod;
1694
1695 mutex_lock(&module_mutex);
1696 list_for_each_entry_rcu(mod, &modules, list) {
1697 if ((mod->module_core) && (mod->core_text_size)) {
1698 set_page_attributes(mod->module_core,
1699 mod->module_core + mod->core_text_size,
1700 set_memory_ro);
1701 }
1702 if ((mod->module_init) && (mod->init_text_size)) {
1703 set_page_attributes(mod->module_init,
1704 mod->module_init + mod->init_text_size,
1705 set_memory_ro);
1706 }
1707 }
1708 mutex_unlock(&module_mutex);
1709}
1710#else
1711static 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 -06001712static void unset_module_core_ro_nx(struct module *mod) { }
1713static void unset_module_init_ro_nx(struct module *mod) { }
matthieu castet84e1c6b2010-11-16 22:35:16 +01001714#endif
1715
Jonas Bonn74e08fc2011-06-30 21:22:11 +02001716void __weak module_free(struct module *mod, void *module_region)
1717{
1718 vfree(module_region);
1719}
1720
1721void __weak module_arch_cleanup(struct module *mod)
1722{
1723}
1724
Rusty Russell75676502010-06-05 11:17:36 -06001725/* Free a module, remove from lists, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726static void free_module(struct module *mod)
1727{
Li Zefan7ead8b82009-08-17 16:56:28 +08001728 trace_module_free(mod);
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 /* Delete from various lists */
Rusty Russell75676502010-06-05 11:17:36 -06001731 mutex_lock(&module_mutex);
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001732 stop_machine(__unlink_module, mod, NULL);
Rusty Russell75676502010-06-05 11:17:36 -06001733 mutex_unlock(&module_mutex);
Rusty Russell36b03602010-08-05 12:59:09 -06001734 mod_sysfs_teardown(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Jason Baronb82bab4b2010-07-27 13:18:01 -07001736 /* Remove dynamic debug info */
1737 ddebug_remove_module(mod->name);
1738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 /* Arch-specific cleanup. */
1740 module_arch_cleanup(mod);
1741
1742 /* Module unload stuff */
1743 module_unload_free(mod);
1744
Rusty Russelle180a6b2009-03-31 13:05:29 -06001745 /* Free any allocated parameters. */
1746 destroy_params(mod->kp, mod->num_kp);
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 /* This may be NULL, but that's OK */
Jan Glauber01526ed2011-05-19 16:55:26 -06001749 unset_module_init_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 module_free(mod, mod->module_init);
1751 kfree(mod->args);
Tejun Heo259354d2010-03-10 18:56:10 +09001752 percpu_modfree(mod);
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001753
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001754 /* Free lock-classes: */
1755 lockdep_free_key_range(mod->module_core, mod->core_size);
1756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 /* Finally, free the core (containing the module structure) */
Jan Glauber01526ed2011-05-19 16:55:26 -06001758 unset_module_core_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 module_free(mod, mod->module_core);
Bernd Schmidteb8cdec2009-09-21 17:03:57 -07001760
1761#ifdef CONFIG_MPU
1762 update_protections(current->mm);
1763#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764}
1765
1766void *__symbol_get(const char *symbol)
1767{
1768 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001769 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Rusty Russell24da1cb2007-07-15 23:41:46 -07001771 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001772 sym = find_symbol(symbol, &owner, NULL, true, true);
1773 if (sym && strong_try_module_get(owner))
1774 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001775 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Tim Abbott414fd312008-12-05 19:03:56 -05001777 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778}
1779EXPORT_SYMBOL_GPL(__symbol_get);
1780
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001781/*
1782 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001783 * in the kernel or in some other module's exported symbol table.
Rusty Russellbe593f42010-06-05 11:17:37 -06001784 *
1785 * You must hold the module_mutex.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001786 */
1787static int verify_export_symbols(struct module *mod)
1788{
Rusty Russellb2111042008-05-01 21:15:00 -05001789 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001790 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001791 const struct kernel_symbol *s;
1792 struct {
1793 const struct kernel_symbol *sym;
1794 unsigned int num;
1795 } arr[] = {
1796 { mod->syms, mod->num_syms },
1797 { mod->gpl_syms, mod->num_gpl_syms },
1798 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001799#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001800 { mod->unused_syms, mod->num_unused_syms },
1801 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001802#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001803 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001804
Rusty Russellb2111042008-05-01 21:15:00 -05001805 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1806 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Rusty Russellbe593f42010-06-05 11:17:37 -06001807 if (find_symbol(s->name, &owner, NULL, true, false)) {
Rusty Russellb2111042008-05-01 21:15:00 -05001808 printk(KERN_ERR
1809 "%s: exports duplicate symbol %s"
1810 " (owned by %s)\n",
1811 mod->name, s->name, module_name(owner));
1812 return -ENOEXEC;
1813 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001814 }
Rusty Russellb2111042008-05-01 21:15:00 -05001815 }
1816 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001817}
1818
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001819/* Change all symbols so that st_value encodes the pointer directly. */
Rusty Russell49668682010-08-05 12:59:10 -06001820static int simplify_symbols(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
Rusty Russell49668682010-08-05 12:59:10 -06001822 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
1823 Elf_Sym *sym = (void *)symsec->sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 unsigned long secbase;
Rusty Russell49668682010-08-05 12:59:10 -06001825 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05001827 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Rusty Russell49668682010-08-05 12:59:10 -06001829 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
1830 const char *name = info->strtab + sym[i].st_name;
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 switch (sym[i].st_shndx) {
1833 case SHN_COMMON:
1834 /* We compiled with -fno-common. These are not
1835 supposed to happen. */
Rusty Russell49668682010-08-05 12:59:10 -06001836 DEBUGP("Common symbol: %s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 printk("%s: please compile with -fno-common\n",
1838 mod->name);
1839 ret = -ENOEXEC;
1840 break;
1841
1842 case SHN_ABS:
1843 /* Don't need to do anything */
1844 DEBUGP("Absolute symbol: 0x%08lx\n",
1845 (long)sym[i].st_value);
1846 break;
1847
1848 case SHN_UNDEF:
Rusty Russell49668682010-08-05 12:59:10 -06001849 ksym = resolve_symbol_wait(mod, info, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 /* Ok if resolved. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001851 if (ksym && !IS_ERR(ksym)) {
Tim Abbott414fd312008-12-05 19:03:56 -05001852 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 break;
Tim Abbott414fd312008-12-05 19:03:56 -05001854 }
1855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 /* Ok if weak. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001857 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 break;
1859
Rusty Russell9bea7f22010-06-05 11:17:37 -06001860 printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
Rusty Russell49668682010-08-05 12:59:10 -06001861 mod->name, name, PTR_ERR(ksym));
Rusty Russell9bea7f22010-06-05 11:17:37 -06001862 ret = PTR_ERR(ksym) ?: -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 break;
1864
1865 default:
1866 /* Divert to percpu allocation if a percpu var. */
Rusty Russell49668682010-08-05 12:59:10 -06001867 if (sym[i].st_shndx == info->index.pcpu)
Tejun Heo259354d2010-03-10 18:56:10 +09001868 secbase = (unsigned long)mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 else
Rusty Russell49668682010-08-05 12:59:10 -06001870 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 sym[i].st_value += secbase;
1872 break;
1873 }
1874 }
1875
1876 return ret;
1877}
1878
Jonas Bonn74e08fc2011-06-30 21:22:11 +02001879int __weak apply_relocate(Elf_Shdr *sechdrs,
1880 const char *strtab,
1881 unsigned int symindex,
1882 unsigned int relsec,
1883 struct module *me)
1884{
1885 pr_err("module %s: REL relocation unsupported\n", me->name);
1886 return -ENOEXEC;
1887}
1888
1889int __weak apply_relocate_add(Elf_Shdr *sechdrs,
1890 const char *strtab,
1891 unsigned int symindex,
1892 unsigned int relsec,
1893 struct module *me)
1894{
1895 pr_err("module %s: RELA relocation unsupported\n", me->name);
1896 return -ENOEXEC;
1897}
1898
Rusty Russell49668682010-08-05 12:59:10 -06001899static int apply_relocations(struct module *mod, const struct load_info *info)
Rusty Russell22e268e2010-08-05 12:59:05 -06001900{
1901 unsigned int i;
1902 int err = 0;
1903
1904 /* Now do relocations. */
Rusty Russell49668682010-08-05 12:59:10 -06001905 for (i = 1; i < info->hdr->e_shnum; i++) {
1906 unsigned int infosec = info->sechdrs[i].sh_info;
Rusty Russell22e268e2010-08-05 12:59:05 -06001907
1908 /* Not a valid relocation section? */
Rusty Russell49668682010-08-05 12:59:10 -06001909 if (infosec >= info->hdr->e_shnum)
Rusty Russell22e268e2010-08-05 12:59:05 -06001910 continue;
1911
1912 /* Don't bother with non-allocated sections */
Rusty Russell49668682010-08-05 12:59:10 -06001913 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
Rusty Russell22e268e2010-08-05 12:59:05 -06001914 continue;
1915
Rusty Russell49668682010-08-05 12:59:10 -06001916 if (info->sechdrs[i].sh_type == SHT_REL)
1917 err = apply_relocate(info->sechdrs, info->strtab,
1918 info->index.sym, i, mod);
1919 else if (info->sechdrs[i].sh_type == SHT_RELA)
1920 err = apply_relocate_add(info->sechdrs, info->strtab,
1921 info->index.sym, i, mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06001922 if (err < 0)
1923 break;
1924 }
1925 return err;
1926}
1927
Helge Deller088af9a2008-12-31 12:31:18 +01001928/* Additional bytes needed by arch in front of individual sections */
1929unsigned int __weak arch_mod_section_prepend(struct module *mod,
1930 unsigned int section)
1931{
1932 /* default implementation just returns zero */
1933 return 0;
1934}
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01001937static long get_offset(struct module *mod, unsigned int *size,
1938 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
1940 long ret;
1941
Helge Deller088af9a2008-12-31 12:31:18 +01001942 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1944 *size = ret + sechdr->sh_size;
1945 return ret;
1946}
1947
1948/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1949 might -- code, read-only data, read-write data, small data. Tally
1950 sizes, and place the offsets into sh_entsize fields: high bit means it
1951 belongs in init. */
Rusty Russell49668682010-08-05 12:59:10 -06001952static void layout_sections(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
1954 static unsigned long const masks[][2] = {
1955 /* NOTE: all executable code must be the first section
1956 * in this array; otherwise modify the text_size
1957 * finder in the two loops below */
1958 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1959 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1960 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1961 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1962 };
1963 unsigned int m, i;
1964
Rusty Russell49668682010-08-05 12:59:10 -06001965 for (i = 0; i < info->hdr->e_shnum; i++)
1966 info->sechdrs[i].sh_entsize = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 DEBUGP("Core section allocation order:\n");
1969 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06001970 for (i = 0; i < info->hdr->e_shnum; ++i) {
1971 Elf_Shdr *s = &info->sechdrs[i];
1972 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
1974 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1975 || (s->sh_flags & masks[m][1])
1976 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06001977 || strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001979 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Rusty Russell49668682010-08-05 12:59:10 -06001980 DEBUGP("\t%s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01001982 switch (m) {
1983 case 0: /* executable */
1984 mod->core_size = debug_align(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 mod->core_text_size = mod->core_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01001986 break;
1987 case 1: /* RO: text and ro-data */
1988 mod->core_size = debug_align(mod->core_size);
1989 mod->core_ro_size = mod->core_size;
1990 break;
1991 case 3: /* whole core */
1992 mod->core_size = debug_align(mod->core_size);
1993 break;
1994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 }
1996
1997 DEBUGP("Init section allocation order:\n");
1998 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06001999 for (i = 0; i < info->hdr->e_shnum; ++i) {
2000 Elf_Shdr *s = &info->sechdrs[i];
2001 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
2003 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2004 || (s->sh_flags & masks[m][1])
2005 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06002006 || !strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01002008 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 | INIT_OFFSET_MASK);
Rusty Russell49668682010-08-05 12:59:10 -06002010 DEBUGP("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01002012 switch (m) {
2013 case 0: /* executable */
2014 mod->init_size = debug_align(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 mod->init_text_size = mod->init_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01002016 break;
2017 case 1: /* RO: text and ro-data */
2018 mod->init_size = debug_align(mod->init_size);
2019 mod->init_ro_size = mod->init_size;
2020 break;
2021 case 3: /* whole init */
2022 mod->init_size = debug_align(mod->init_size);
2023 break;
2024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 }
2026}
2027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028static void set_license(struct module *mod, const char *license)
2029{
2030 if (!license)
2031 license = "unspecified";
2032
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002033 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07002034 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07002035 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002036 "kernel.\n", mod->name, license);
2037 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 }
2039}
2040
2041/* Parse tag=value strings from .modinfo section */
2042static char *next_string(char *string, unsigned long *secsize)
2043{
2044 /* Skip non-zero chars */
2045 while (string[0]) {
2046 string++;
2047 if ((*secsize)-- <= 1)
2048 return NULL;
2049 }
2050
2051 /* Skip any zero padding. */
2052 while (!string[0]) {
2053 string++;
2054 if ((*secsize)-- <= 1)
2055 return NULL;
2056 }
2057 return string;
2058}
2059
Rusty Russell49668682010-08-05 12:59:10 -06002060static char *get_modinfo(struct load_info *info, const char *tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
2062 char *p;
2063 unsigned int taglen = strlen(tag);
Rusty Russell49668682010-08-05 12:59:10 -06002064 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2065 unsigned long size = infosec->sh_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Rusty Russell49668682010-08-05 12:59:10 -06002067 for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2069 return p + taglen + 1;
2070 }
2071 return NULL;
2072}
2073
Rusty Russell49668682010-08-05 12:59:10 -06002074static void setup_modinfo(struct module *mod, struct load_info *info)
Matt Domschc988d2b2005-06-23 22:05:15 -07002075{
2076 struct module_attribute *attr;
2077 int i;
2078
2079 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2080 if (attr->setup)
Rusty Russell49668682010-08-05 12:59:10 -06002081 attr->setup(mod, get_modinfo(info, attr->attr.name));
Matt Domschc988d2b2005-06-23 22:05:15 -07002082 }
2083}
Matt Domschc988d2b2005-06-23 22:05:15 -07002084
Rusty Russella263f772009-09-25 00:32:58 -06002085static void free_modinfo(struct module *mod)
2086{
2087 struct module_attribute *attr;
2088 int i;
2089
2090 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2091 if (attr->free)
2092 attr->free(mod);
2093 }
2094}
2095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01002097
2098/* lookup symbol in given range of kernel_symbols */
2099static const struct kernel_symbol *lookup_symbol(const char *name,
2100 const struct kernel_symbol *start,
2101 const struct kernel_symbol *stop)
2102{
Alessio Igor Bogani9d634872011-05-18 22:35:59 +02002103 return bsearch(name, start, stop - start,
2104 sizeof(struct kernel_symbol), cmp_name);
WANG Cong15bba372008-07-24 15:41:48 +01002105}
2106
Tim Abbottca4787b2009-01-05 08:40:10 -06002107static int is_exported(const char *name, unsigned long value,
2108 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
Tim Abbottca4787b2009-01-05 08:40:10 -06002110 const struct kernel_symbol *ks;
2111 if (!mod)
2112 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01002113 else
Tim Abbottca4787b2009-01-05 08:40:10 -06002114 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
2115 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116}
2117
2118/* As per nm */
Rusty Russelleded41c2010-08-05 12:59:07 -06002119static char elf_type(const Elf_Sym *sym, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
Rusty Russelleded41c2010-08-05 12:59:07 -06002121 const Elf_Shdr *sechdrs = info->sechdrs;
2122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2124 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2125 return 'v';
2126 else
2127 return 'w';
2128 }
2129 if (sym->st_shndx == SHN_UNDEF)
2130 return 'U';
2131 if (sym->st_shndx == SHN_ABS)
2132 return 'a';
2133 if (sym->st_shndx >= SHN_LORESERVE)
2134 return '?';
2135 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2136 return 't';
2137 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2138 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2139 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2140 return 'r';
2141 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2142 return 'g';
2143 else
2144 return 'd';
2145 }
2146 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2147 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2148 return 's';
2149 else
2150 return 'b';
2151 }
Rusty Russelleded41c2010-08-05 12:59:07 -06002152 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2153 ".debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 return 'n';
Rusty Russelleded41c2010-08-05 12:59:07 -06002155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 return '?';
2157}
2158
Jan Beulich4a496222009-07-06 14:50:42 +01002159static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
2160 unsigned int shnum)
2161{
2162 const Elf_Shdr *sec;
2163
2164 if (src->st_shndx == SHN_UNDEF
2165 || src->st_shndx >= shnum
2166 || !src->st_name)
2167 return false;
2168
2169 sec = sechdrs + src->st_shndx;
2170 if (!(sec->sh_flags & SHF_ALLOC)
2171#ifndef CONFIG_KALLSYMS_ALL
2172 || !(sec->sh_flags & SHF_EXECINSTR)
2173#endif
2174 || (sec->sh_entsize & INIT_OFFSET_MASK))
2175 return false;
2176
2177 return true;
2178}
2179
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302180/*
2181 * We only allocate and copy the strings needed by the parts of symtab
2182 * we keep. This is simple, but has the effect of making multiple
2183 * copies of duplicates. We could be more sophisticated, see
2184 * linux-kernel thread starting with
2185 * <73defb5e4bca04a6431392cc341112b1@localhost>.
2186 */
Rusty Russell49668682010-08-05 12:59:10 -06002187static void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002188{
Rusty Russell49668682010-08-05 12:59:10 -06002189 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2190 Elf_Shdr *strsect = info->sechdrs + info->index.str;
Jan Beulich4a496222009-07-06 14:50:42 +01002191 const Elf_Sym *src;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302192 unsigned int i, nsrc, ndst, strtab_size;
Jan Beulich4a496222009-07-06 14:50:42 +01002193
2194 /* Put symbol section at end of init part of module. */
2195 symsect->sh_flags |= SHF_ALLOC;
2196 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
Rusty Russell49668682010-08-05 12:59:10 -06002197 info->index.sym) | INIT_OFFSET_MASK;
2198 DEBUGP("\t%s\n", info->secstrings + symsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002199
Rusty Russell49668682010-08-05 12:59:10 -06002200 src = (void *)info->hdr + symsect->sh_offset;
Jan Beulich4a496222009-07-06 14:50:42 +01002201 nsrc = symsect->sh_size / sizeof(*src);
Kevin Cernekee70b1e9162011-11-12 19:08:55 -08002202
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302203 /* Compute total space required for the core symbols' strtab. */
2204 for (ndst = i = strtab_size = 1; i < nsrc; ++i, ++src)
Rusty Russell49668682010-08-05 12:59:10 -06002205 if (is_core_symbol(src, info->sechdrs, info->hdr->e_shnum)) {
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302206 strtab_size += strlen(&info->strtab[src->st_name]) + 1;
2207 ndst++;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002208 }
Jan Beulich4a496222009-07-06 14:50:42 +01002209
2210 /* Append room for core symbols at end of core part. */
Rusty Russell49668682010-08-05 12:59:10 -06002211 info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302212 info->stroffs = mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
2213 mod->core_size += strtab_size;
Jan Beulich4a496222009-07-06 14:50:42 +01002214
Jan Beulich554bdfe2009-07-06 14:51:44 +01002215 /* Put string table section at end of init part of module. */
2216 strsect->sh_flags |= SHF_ALLOC;
2217 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
Rusty Russell49668682010-08-05 12:59:10 -06002218 info->index.str) | INIT_OFFSET_MASK;
2219 DEBUGP("\t%s\n", info->secstrings + strsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002220}
2221
Rusty Russell811d66a2010-08-05 12:59:12 -06002222static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
Jan Beulich4a496222009-07-06 14:50:42 +01002224 unsigned int i, ndst;
2225 const Elf_Sym *src;
2226 Elf_Sym *dst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002227 char *s;
Rusty Russelleded41c2010-08-05 12:59:07 -06002228 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Rusty Russelleded41c2010-08-05 12:59:07 -06002230 mod->symtab = (void *)symsec->sh_addr;
2231 mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
Rusty Russell511ca6a2010-08-05 12:59:08 -06002232 /* Make sure we get permanent strtab: don't use info->strtab. */
2233 mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
2235 /* Set types up while we still have access to sections. */
2236 for (i = 0; i < mod->num_symtab; i++)
Rusty Russelleded41c2010-08-05 12:59:07 -06002237 mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
Jan Beulich4a496222009-07-06 14:50:42 +01002238
Rusty Russelld9131882010-08-05 12:59:08 -06002239 mod->core_symtab = dst = mod->module_core + info->symoffs;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302240 mod->core_strtab = s = mod->module_core + info->stroffs;
Jan Beulich4a496222009-07-06 14:50:42 +01002241 src = mod->symtab;
2242 *dst = *src;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302243 *s++ = 0;
Jan Beulich4a496222009-07-06 14:50:42 +01002244 for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
Rusty Russelleded41c2010-08-05 12:59:07 -06002245 if (!is_core_symbol(src, info->sechdrs, info->hdr->e_shnum))
Jan Beulich4a496222009-07-06 14:50:42 +01002246 continue;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302247
Jan Beulich4a496222009-07-06 14:50:42 +01002248 dst[ndst] = *src;
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302249 dst[ndst++].st_name = s - mod->core_strtab;
2250 s += strlcpy(s, &mod->strtab[src->st_name], KSYM_NAME_LEN) + 1;
Jan Beulich4a496222009-07-06 14:50:42 +01002251 }
2252 mod->core_num_syms = ndst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253}
2254#else
Rusty Russell49668682010-08-05 12:59:10 -06002255static inline void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002256{
2257}
Paul Mundt3ae91c22009-10-01 15:43:54 -07002258
Michał Mirosławabbce902010-09-20 01:58:08 +02002259static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260{
2261}
2262#endif /* CONFIG_KALLSYMS */
2263
Jason Barone9d376f2009-02-05 11:51:38 -05002264static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05002265{
Rusty Russell811d66a2010-08-05 12:59:12 -06002266 if (!debug)
2267 return;
Jason Barone9d376f2009-02-05 11:51:38 -05002268#ifdef CONFIG_DYNAMIC_DEBUG
2269 if (ddebug_add_module(debug, num, debug->modname))
2270 printk(KERN_ERR "dynamic debug error adding module: %s\n",
2271 debug->modname);
2272#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002273}
Jason Baron346e15b2008-08-12 16:46:19 -04002274
Yehuda Sadehff49d742010-07-03 13:07:35 +10002275static void dynamic_debug_remove(struct _ddebug *debug)
2276{
2277 if (debug)
2278 ddebug_remove_module(debug->modname);
2279}
2280
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002281void * __weak module_alloc(unsigned long size)
2282{
2283 return size == 0 ? NULL : vmalloc_exec(size);
2284}
2285
Rusty Russell3a642e92008-07-22 19:24:28 -05002286static void *module_alloc_update_bounds(unsigned long size)
2287{
2288 void *ret = module_alloc(size);
2289
2290 if (ret) {
Rusty Russell75676502010-06-05 11:17:36 -06002291 mutex_lock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002292 /* Update module bounds. */
2293 if ((unsigned long)ret < module_addr_min)
2294 module_addr_min = (unsigned long)ret;
2295 if ((unsigned long)ret + size > module_addr_max)
2296 module_addr_max = (unsigned long)ret + size;
Rusty Russell75676502010-06-05 11:17:36 -06002297 mutex_unlock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002298 }
2299 return ret;
2300}
2301
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002302#ifdef CONFIG_DEBUG_KMEMLEAK
Rusty Russell49668682010-08-05 12:59:10 -06002303static void kmemleak_load_module(const struct module *mod,
2304 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002305{
2306 unsigned int i;
2307
2308 /* only scan the sections containing data */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002309 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002310
Rusty Russell49668682010-08-05 12:59:10 -06002311 for (i = 1; i < info->hdr->e_shnum; i++) {
2312 const char *name = info->secstrings + info->sechdrs[i].sh_name;
2313 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002314 continue;
Rusty Russell49668682010-08-05 12:59:10 -06002315 if (!strstarts(name, ".data") && !strstarts(name, ".bss"))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002316 continue;
2317
Rusty Russell49668682010-08-05 12:59:10 -06002318 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2319 info->sechdrs[i].sh_size, GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002320 }
2321}
2322#else
Rusty Russell49668682010-08-05 12:59:10 -06002323static inline void kmemleak_load_module(const struct module *mod,
2324 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002325{
2326}
2327#endif
2328
Rusty Russell6526c532010-08-05 12:59:10 -06002329/* Sets info->hdr and info->len. */
Rusty Russelld9131882010-08-05 12:59:08 -06002330static int copy_and_check(struct load_info *info,
2331 const void __user *umod, unsigned long len,
2332 const char __user *uargs)
Rusty Russell40dd2562010-08-05 12:59:03 -06002333{
2334 int err;
2335 Elf_Ehdr *hdr;
2336
2337 if (len < sizeof(*hdr))
2338 return -ENOEXEC;
2339
2340 /* Suck in entire file: we'll want most of it. */
2341 /* vmalloc barfs on "unusual" numbers. Check here */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002342 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
Rusty Russell40dd2562010-08-05 12:59:03 -06002343 return -ENOMEM;
2344
2345 if (copy_from_user(hdr, umod, len) != 0) {
2346 err = -EFAULT;
2347 goto free_hdr;
2348 }
2349
2350 /* Sanity checks against insmoding binaries or wrong arch,
2351 weird elf version */
2352 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
2353 || hdr->e_type != ET_REL
2354 || !elf_check_arch(hdr)
2355 || hdr->e_shentsize != sizeof(Elf_Shdr)) {
2356 err = -ENOEXEC;
2357 goto free_hdr;
2358 }
2359
2360 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) {
2361 err = -ENOEXEC;
2362 goto free_hdr;
2363 }
Rusty Russelld9131882010-08-05 12:59:08 -06002364
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002365 info->hdr = hdr;
2366 info->len = len;
Rusty Russell40dd2562010-08-05 12:59:03 -06002367 return 0;
2368
2369free_hdr:
2370 vfree(hdr);
2371 return err;
2372}
2373
Rusty Russelld9131882010-08-05 12:59:08 -06002374static void free_copy(struct load_info *info)
2375{
Rusty Russelld9131882010-08-05 12:59:08 -06002376 vfree(info->hdr);
2377}
2378
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002379static int rewrite_section_headers(struct load_info *info)
2380{
2381 unsigned int i;
2382
2383 /* This should always be true, but let's be sure. */
2384 info->sechdrs[0].sh_addr = 0;
2385
2386 for (i = 1; i < info->hdr->e_shnum; i++) {
2387 Elf_Shdr *shdr = &info->sechdrs[i];
2388 if (shdr->sh_type != SHT_NOBITS
2389 && info->len < shdr->sh_offset + shdr->sh_size) {
2390 printk(KERN_ERR "Module len %lu truncated\n",
2391 info->len);
2392 return -ENOEXEC;
2393 }
2394
2395 /* Mark all sections sh_addr with their address in the
2396 temporary image. */
2397 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2398
2399#ifndef CONFIG_MODULE_UNLOAD
2400 /* Don't load .exit sections */
2401 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2402 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2403#endif
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002404 }
Rusty Russelld6df72a2010-08-05 12:59:07 -06002405
2406 /* Track but don't keep modinfo and version sections. */
Rusty Russell49668682010-08-05 12:59:10 -06002407 info->index.vers = find_sec(info, "__versions");
2408 info->index.info = find_sec(info, ".modinfo");
Rusty Russelld6df72a2010-08-05 12:59:07 -06002409 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2410 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002411 return 0;
2412}
2413
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002414/*
2415 * Set up our basic convenience variables (pointers to section headers,
2416 * search for module section index etc), and do some basic section
2417 * verification.
2418 *
2419 * Return the temporary module pointer (we'll replace it with the final
2420 * one when we move the module sections around).
2421 */
2422static struct module *setup_load_info(struct load_info *info)
2423{
2424 unsigned int i;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002425 int err;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002426 struct module *mod;
2427
2428 /* Set up the convenience variables */
2429 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002430 info->secstrings = (void *)info->hdr
2431 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002432
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002433 err = rewrite_section_headers(info);
2434 if (err)
2435 return ERR_PTR(err);
2436
2437 /* Find internal symbols and strings. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002438 for (i = 1; i < info->hdr->e_shnum; i++) {
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002439 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2440 info->index.sym = i;
2441 info->index.str = info->sechdrs[i].sh_link;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002442 info->strtab = (char *)info->hdr
2443 + info->sechdrs[info->index.str].sh_offset;
2444 break;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002445 }
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002446 }
2447
Rusty Russell49668682010-08-05 12:59:10 -06002448 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002449 if (!info->index.mod) {
2450 printk(KERN_WARNING "No module found in object\n");
2451 return ERR_PTR(-ENOEXEC);
2452 }
2453 /* This is temporary: point mod into copy of data. */
2454 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2455
2456 if (info->index.sym == 0) {
2457 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2458 mod->name);
2459 return ERR_PTR(-ENOEXEC);
2460 }
2461
Rusty Russell49668682010-08-05 12:59:10 -06002462 info->index.pcpu = find_pcpusec(info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002463
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002464 /* Check module struct version now, before we try to use module. */
2465 if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2466 return ERR_PTR(-ENOEXEC);
2467
2468 return mod;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002469}
2470
Rusty Russell49668682010-08-05 12:59:10 -06002471static int check_modinfo(struct module *mod, struct load_info *info)
Rusty Russell40dd2562010-08-05 12:59:03 -06002472{
Rusty Russell49668682010-08-05 12:59:10 -06002473 const char *modmagic = get_modinfo(info, "vermagic");
Rusty Russell40dd2562010-08-05 12:59:03 -06002474 int err;
2475
2476 /* This is allowed: modprobe --force will invalidate it. */
2477 if (!modmagic) {
2478 err = try_to_force_load(mod, "bad vermagic");
2479 if (err)
2480 return err;
Rusty Russell49668682010-08-05 12:59:10 -06002481 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002482 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2483 mod->name, modmagic, vermagic);
2484 return -ENOEXEC;
2485 }
2486
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002487 if (!get_modinfo(info, "intree"))
2488 add_taint_module(mod, TAINT_OOT_MODULE);
2489
Rusty Russell49668682010-08-05 12:59:10 -06002490 if (get_modinfo(info, "staging")) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002491 add_taint_module(mod, TAINT_CRAP);
2492 printk(KERN_WARNING "%s: module is from the staging directory,"
2493 " the quality is unknown, you have been warned.\n",
2494 mod->name);
2495 }
Rusty Russell22e268e2010-08-05 12:59:05 -06002496
2497 /* Set up license info based on the info section */
Rusty Russell49668682010-08-05 12:59:10 -06002498 set_license(mod, get_modinfo(info, "license"));
Rusty Russell22e268e2010-08-05 12:59:05 -06002499
Rusty Russell40dd2562010-08-05 12:59:03 -06002500 return 0;
2501}
2502
Rusty Russell811d66a2010-08-05 12:59:12 -06002503static void find_module_sections(struct module *mod, struct load_info *info)
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002504{
Rusty Russell49668682010-08-05 12:59:10 -06002505 mod->kp = section_objs(info, "__param",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002506 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell49668682010-08-05 12:59:10 -06002507 mod->syms = section_objs(info, "__ksymtab",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002508 sizeof(*mod->syms), &mod->num_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002509 mod->crcs = section_addr(info, "__kcrctab");
2510 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002511 sizeof(*mod->gpl_syms),
2512 &mod->num_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002513 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
2514 mod->gpl_future_syms = section_objs(info,
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002515 "__ksymtab_gpl_future",
2516 sizeof(*mod->gpl_future_syms),
2517 &mod->num_gpl_future_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002518 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002519
2520#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell49668682010-08-05 12:59:10 -06002521 mod->unused_syms = section_objs(info, "__ksymtab_unused",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002522 sizeof(*mod->unused_syms),
2523 &mod->num_unused_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002524 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
2525 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002526 sizeof(*mod->unused_gpl_syms),
2527 &mod->num_unused_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002528 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002529#endif
2530#ifdef CONFIG_CONSTRUCTORS
Rusty Russell49668682010-08-05 12:59:10 -06002531 mod->ctors = section_objs(info, ".ctors",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002532 sizeof(*mod->ctors), &mod->num_ctors);
2533#endif
2534
2535#ifdef CONFIG_TRACEPOINTS
Mathieu Desnoyers65498642011-01-26 17:26:22 -05002536 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
2537 sizeof(*mod->tracepoints_ptrs),
2538 &mod->num_tracepoints);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002539#endif
Jason Baronbf5438fc2010-09-17 11:09:00 -04002540#ifdef HAVE_JUMP_LABEL
2541 mod->jump_entries = section_objs(info, "__jump_table",
2542 sizeof(*mod->jump_entries),
2543 &mod->num_jump_entries);
2544#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002545#ifdef CONFIG_EVENT_TRACING
Rusty Russell49668682010-08-05 12:59:10 -06002546 mod->trace_events = section_objs(info, "_ftrace_events",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002547 sizeof(*mod->trace_events),
2548 &mod->num_trace_events);
2549 /*
2550 * This section contains pointers to allocated objects in the trace
2551 * code and not scanning it leads to false positives.
2552 */
2553 kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2554 mod->num_trace_events, GFP_KERNEL);
2555#endif
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05002556#ifdef CONFIG_TRACING
2557 mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
2558 sizeof(*mod->trace_bprintk_fmt_start),
2559 &mod->num_trace_bprintk_fmt);
2560 /*
2561 * This section contains pointers to allocated objects in the trace
2562 * code and not scanning it leads to false positives.
2563 */
2564 kmemleak_scan_area(mod->trace_bprintk_fmt_start,
2565 sizeof(*mod->trace_bprintk_fmt_start) *
2566 mod->num_trace_bprintk_fmt, GFP_KERNEL);
2567#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002568#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2569 /* sechdrs[0].sh_size is always zero */
Rusty Russell49668682010-08-05 12:59:10 -06002570 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002571 sizeof(*mod->ftrace_callsites),
2572 &mod->num_ftrace_callsites);
2573#endif
Rusty Russell22e268e2010-08-05 12:59:05 -06002574
Rusty Russell811d66a2010-08-05 12:59:12 -06002575 mod->extable = section_objs(info, "__ex_table",
2576 sizeof(*mod->extable), &mod->num_exentries);
2577
Rusty Russell49668682010-08-05 12:59:10 -06002578 if (section_addr(info, "__obsparm"))
Rusty Russell22e268e2010-08-05 12:59:05 -06002579 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2580 mod->name);
Rusty Russell811d66a2010-08-05 12:59:12 -06002581
2582 info->debug = section_objs(info, "__verbose",
2583 sizeof(*info->debug), &info->num_debug);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002584}
2585
Rusty Russell49668682010-08-05 12:59:10 -06002586static int move_module(struct module *mod, struct load_info *info)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002587{
2588 int i;
2589 void *ptr;
2590
2591 /* Do the allocs. */
2592 ptr = module_alloc_update_bounds(mod->core_size);
2593 /*
2594 * The pointer to this block is stored in the module structure
2595 * which is inside the block. Just mark it as not being a
2596 * leak.
2597 */
2598 kmemleak_not_leak(ptr);
2599 if (!ptr)
Rusty Russelld9131882010-08-05 12:59:08 -06002600 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002601
2602 memset(ptr, 0, mod->core_size);
2603 mod->module_core = ptr;
2604
2605 ptr = module_alloc_update_bounds(mod->init_size);
2606 /*
2607 * The pointer to this block is stored in the module structure
2608 * which is inside the block. This block doesn't need to be
2609 * scanned as it contains data and code that will be freed
2610 * after the module is initialized.
2611 */
2612 kmemleak_ignore(ptr);
2613 if (!ptr && mod->init_size) {
2614 module_free(mod, mod->module_core);
Rusty Russelld9131882010-08-05 12:59:08 -06002615 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002616 }
2617 memset(ptr, 0, mod->init_size);
2618 mod->module_init = ptr;
2619
2620 /* Transfer each section which specifies SHF_ALLOC */
2621 DEBUGP("final section addresses:\n");
Rusty Russell49668682010-08-05 12:59:10 -06002622 for (i = 0; i < info->hdr->e_shnum; i++) {
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002623 void *dest;
Rusty Russell49668682010-08-05 12:59:10 -06002624 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002625
Rusty Russell49668682010-08-05 12:59:10 -06002626 if (!(shdr->sh_flags & SHF_ALLOC))
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002627 continue;
2628
Rusty Russell49668682010-08-05 12:59:10 -06002629 if (shdr->sh_entsize & INIT_OFFSET_MASK)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002630 dest = mod->module_init
Rusty Russell49668682010-08-05 12:59:10 -06002631 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002632 else
Rusty Russell49668682010-08-05 12:59:10 -06002633 dest = mod->module_core + shdr->sh_entsize;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002634
Rusty Russell49668682010-08-05 12:59:10 -06002635 if (shdr->sh_type != SHT_NOBITS)
2636 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002637 /* Update sh_addr to point to copy in image. */
Rusty Russell49668682010-08-05 12:59:10 -06002638 shdr->sh_addr = (unsigned long)dest;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002639 DEBUGP("\t0x%lx %s\n",
Rusty Russell49668682010-08-05 12:59:10 -06002640 shdr->sh_addr, info->secstrings + shdr->sh_name);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002641 }
Rusty Russelld9131882010-08-05 12:59:08 -06002642
2643 return 0;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002644}
2645
Rusty Russell49668682010-08-05 12:59:10 -06002646static int check_module_license_and_versions(struct module *mod)
Rusty Russell22e268e2010-08-05 12:59:05 -06002647{
2648 /*
2649 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2650 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2651 * using GPL-only symbols it needs.
2652 */
2653 if (strcmp(mod->name, "ndiswrapper") == 0)
2654 add_taint(TAINT_PROPRIETARY_MODULE);
2655
2656 /* driverloader was caught wrongly pretending to be under GPL */
2657 if (strcmp(mod->name, "driverloader") == 0)
2658 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2659
2660#ifdef CONFIG_MODVERSIONS
2661 if ((mod->num_syms && !mod->crcs)
2662 || (mod->num_gpl_syms && !mod->gpl_crcs)
2663 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2664#ifdef CONFIG_UNUSED_SYMBOLS
2665 || (mod->num_unused_syms && !mod->unused_crcs)
2666 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2667#endif
2668 ) {
2669 return try_to_force_load(mod,
2670 "no versions for exported symbols");
2671 }
2672#endif
2673 return 0;
2674}
2675
2676static void flush_module_icache(const struct module *mod)
2677{
2678 mm_segment_t old_fs;
2679
2680 /* flush the icache in correct context */
2681 old_fs = get_fs();
2682 set_fs(KERNEL_DS);
2683
2684 /*
2685 * Flush the instruction cache, since we've played with text.
2686 * Do it before processing of module parameters, so the module
2687 * can provide parameter accessor functions of its own.
2688 */
2689 if (mod->module_init)
2690 flush_icache_range((unsigned long)mod->module_init,
2691 (unsigned long)mod->module_init
2692 + mod->init_size);
2693 flush_icache_range((unsigned long)mod->module_core,
2694 (unsigned long)mod->module_core + mod->core_size);
2695
2696 set_fs(old_fs);
2697}
2698
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002699int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
2700 Elf_Shdr *sechdrs,
2701 char *secstrings,
2702 struct module *mod)
2703{
2704 return 0;
2705}
2706
Rusty Russelld9131882010-08-05 12:59:08 -06002707static struct module *layout_and_allocate(struct load_info *info)
2708{
2709 /* Module within temporary copy. */
2710 struct module *mod;
Rusty Russell49668682010-08-05 12:59:10 -06002711 Elf_Shdr *pcpusec;
Rusty Russelld9131882010-08-05 12:59:08 -06002712 int err;
2713
2714 mod = setup_load_info(info);
2715 if (IS_ERR(mod))
2716 return mod;
2717
Rusty Russell49668682010-08-05 12:59:10 -06002718 err = check_modinfo(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002719 if (err)
2720 return ERR_PTR(err);
2721
2722 /* Allow arches to frob section contents and sizes. */
Rusty Russell49668682010-08-05 12:59:10 -06002723 err = module_frob_arch_sections(info->hdr, info->sechdrs,
2724 info->secstrings, mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002725 if (err < 0)
Rusty Russell6526c532010-08-05 12:59:10 -06002726 goto out;
Rusty Russelld9131882010-08-05 12:59:08 -06002727
Rusty Russell49668682010-08-05 12:59:10 -06002728 pcpusec = &info->sechdrs[info->index.pcpu];
2729 if (pcpusec->sh_size) {
Rusty Russelld9131882010-08-05 12:59:08 -06002730 /* We have a special allocation for this section. */
Rusty Russell49668682010-08-05 12:59:10 -06002731 err = percpu_modalloc(mod,
2732 pcpusec->sh_size, pcpusec->sh_addralign);
Rusty Russelld9131882010-08-05 12:59:08 -06002733 if (err)
Rusty Russell6526c532010-08-05 12:59:10 -06002734 goto out;
Rusty Russell49668682010-08-05 12:59:10 -06002735 pcpusec->sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russelld9131882010-08-05 12:59:08 -06002736 }
2737
2738 /* Determine total sizes, and put offsets in sh_entsize. For now
2739 this is done generically; there doesn't appear to be any
2740 special cases for the architectures. */
Rusty Russell49668682010-08-05 12:59:10 -06002741 layout_sections(mod, info);
Rusty Russell49668682010-08-05 12:59:10 -06002742 layout_symtab(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002743
2744 /* Allocate and move to the final place */
Rusty Russell49668682010-08-05 12:59:10 -06002745 err = move_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002746 if (err)
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302747 goto free_percpu;
Rusty Russelld9131882010-08-05 12:59:08 -06002748
2749 /* Module has been copied to its final place now: return it. */
2750 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
Rusty Russell49668682010-08-05 12:59:10 -06002751 kmemleak_load_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002752 return mod;
2753
Rusty Russelld9131882010-08-05 12:59:08 -06002754free_percpu:
2755 percpu_modfree(mod);
Rusty Russell6526c532010-08-05 12:59:10 -06002756out:
Rusty Russelld9131882010-08-05 12:59:08 -06002757 return ERR_PTR(err);
2758}
2759
2760/* mod is no longer valid after this! */
2761static void module_deallocate(struct module *mod, struct load_info *info)
2762{
Rusty Russelld9131882010-08-05 12:59:08 -06002763 percpu_modfree(mod);
2764 module_free(mod, mod->module_init);
2765 module_free(mod, mod->module_core);
2766}
2767
Jonas Bonn74e08fc2011-06-30 21:22:11 +02002768int __weak module_finalize(const Elf_Ehdr *hdr,
2769 const Elf_Shdr *sechdrs,
2770 struct module *me)
2771{
2772 return 0;
2773}
2774
Rusty Russell811d66a2010-08-05 12:59:12 -06002775static int post_relocation(struct module *mod, const struct load_info *info)
2776{
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002777 /* Sort exception table now relocations are done. */
Rusty Russell811d66a2010-08-05 12:59:12 -06002778 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2779
2780 /* Copy relocated percpu area over. */
2781 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
2782 info->sechdrs[info->index.pcpu].sh_size);
2783
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002784 /* Setup kallsyms-specific fields. */
Rusty Russell811d66a2010-08-05 12:59:12 -06002785 add_kallsyms(mod, info);
2786
2787 /* Arch-specific module finalizing. */
2788 return module_finalize(info->hdr, info->sechdrs, mod);
2789}
2790
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791/* Allocate and load the module: note that size of section 0 is always
2792 zero, and we rely on this for optional sections. */
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002793static struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 unsigned long len,
2795 const char __user *uargs)
2796{
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002797 struct load_info info = { NULL, };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 struct module *mod;
Rusty Russell40dd2562010-08-05 12:59:03 -06002799 long err;
Paul Mundt3ae91c22009-10-01 15:43:54 -07002800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
2802 umod, len, uargs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
Rusty Russelld9131882010-08-05 12:59:08 -06002804 /* Copy in the blobs from userspace, check they are vaguely sane. */
2805 err = copy_and_check(&info, umod, len, uargs);
Rusty Russell40dd2562010-08-05 12:59:03 -06002806 if (err)
2807 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
Rusty Russelld9131882010-08-05 12:59:08 -06002809 /* Figure out module layout, and allocate all the memory. */
2810 mod = layout_and_allocate(&info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002811 if (IS_ERR(mod)) {
2812 err = PTR_ERR(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002813 goto free_copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
Rusty Russell49668682010-08-05 12:59:10 -06002816 /* Now module is in final location, initialize linked lists, etc. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -06002817 err = module_unload_init(mod);
2818 if (err)
Rusty Russelld9131882010-08-05 12:59:08 -06002819 goto free_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
Rusty Russell22e268e2010-08-05 12:59:05 -06002821 /* Now we've got everything in the final locations, we can
2822 * find optional sections. */
Rusty Russell49668682010-08-05 12:59:10 -06002823 find_module_sections(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
Rusty Russell49668682010-08-05 12:59:10 -06002825 err = check_module_license_and_versions(mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002826 if (err)
2827 goto free_unload;
Dave Jones9841d61d2006-01-08 01:03:41 -08002828
Matt Domschc988d2b2005-06-23 22:05:15 -07002829 /* Set up MODINFO_ATTR fields */
Rusty Russell49668682010-08-05 12:59:10 -06002830 setup_modinfo(mod, &info);
Matt Domschc988d2b2005-06-23 22:05:15 -07002831
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 /* Fix up syms, so that st_value is a pointer to location. */
Rusty Russell49668682010-08-05 12:59:10 -06002833 err = simplify_symbols(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06002835 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
Rusty Russell49668682010-08-05 12:59:10 -06002837 err = apply_relocations(mod, &info);
Rusty Russell22e268e2010-08-05 12:59:05 -06002838 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06002839 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
Rusty Russell811d66a2010-08-05 12:59:12 -06002841 err = post_relocation(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06002843 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
Rusty Russell22e268e2010-08-05 12:59:05 -06002845 flush_module_icache(mod);
Thomas Koeller378bac82005-09-06 15:17:11 -07002846
Rusty Russell6526c532010-08-05 12:59:10 -06002847 /* Now copy in args */
2848 mod->args = strndup_user(uargs, ~0UL >> 1);
2849 if (IS_ERR(mod->args)) {
2850 err = PTR_ERR(mod->args);
2851 goto free_arch_cleanup;
2852 }
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002853
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002854 /* Mark state as coming so strong_try_module_get() ignores us. */
Rusty Russelld9131882010-08-05 12:59:08 -06002855 mod->state = MODULE_STATE_COMING;
2856
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002857 /* Now sew it into the lists so we can get lockdep and oops
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002858 * info during argument parsing. No one should access us, since
Andi Kleend72b3752008-08-30 10:09:00 +02002859 * strong_try_module_get() will fail.
2860 * lockdep/oops can run asynchronous, so use the RCU list insertion
2861 * function to insert in a way safe to concurrent readers.
2862 * The mutex protects against concurrent writers.
2863 */
Rusty Russell75676502010-06-05 11:17:36 -06002864 mutex_lock(&module_mutex);
Linus Torvalds3bafeb62010-06-05 11:17:36 -06002865 if (find_module(mod->name)) {
2866 err = -EEXIST;
Rusty Russellbe593f42010-06-05 11:17:37 -06002867 goto unlock;
Linus Torvalds3bafeb62010-06-05 11:17:36 -06002868 }
2869
Rusty Russell811d66a2010-08-05 12:59:12 -06002870 /* This has to be done once we're sure module name is unique. */
Ben Hutchings1cd0d6c2011-11-01 03:59:33 +00002871 dynamic_debug_setup(info.debug, info.num_debug);
Yehuda Sadehff49d742010-07-03 13:07:35 +10002872
Rusty Russellbe593f42010-06-05 11:17:37 -06002873 /* Find duplicate symbols */
2874 err = verify_export_symbols(mod);
2875 if (err < 0)
Yehuda Sadehff49d742010-07-03 13:07:35 +10002876 goto ddebug;
Rusty Russellbe593f42010-06-05 11:17:37 -06002877
Linus Torvalds53363772010-10-05 11:29:27 -07002878 module_bug_finalize(info.hdr, info.sechdrs, mod);
Andi Kleend72b3752008-08-30 10:09:00 +02002879 list_add_rcu(&mod->list, &modules);
Rusty Russell75676502010-06-05 11:17:36 -06002880 mutex_unlock(&module_mutex);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002881
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002882 /* Module is ready to execute: parsing args may do that. */
Rusty Russelle180a6b2009-03-31 13:05:29 -06002883 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002885 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002887 /* Link in to syfs. */
Rusty Russell8f6d0372010-08-05 12:59:09 -06002888 err = mod_sysfs_setup(mod, &info, mod->kp, mod->num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002890 goto unlink;
Rusty Russell80a3d1b2010-06-05 11:17:36 -06002891
Kevin Cernekee48fd1182012-01-13 09:32:14 +10302892 /* Get rid of temporary copy. */
Rusty Russelld9131882010-08-05 12:59:08 -06002893 free_copy(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
2895 /* Done! */
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002896 trace_module_load(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 return mod;
2898
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002899 unlink:
Rusty Russell75676502010-06-05 11:17:36 -06002900 mutex_lock(&module_mutex);
Rusty Russelle91defa2009-03-31 13:05:35 -06002901 /* Unlink carefully: kallsyms could be walking list. */
2902 list_del_rcu(&mod->list);
Linus Torvalds53363772010-10-05 11:29:27 -07002903 module_bug_cleanup(mod);
2904
Yehuda Sadehff49d742010-07-03 13:07:35 +10002905 ddebug:
Ben Hutchings1cd0d6c2011-11-01 03:59:33 +00002906 dynamic_debug_remove(info.debug);
Rusty Russellbe593f42010-06-05 11:17:37 -06002907 unlock:
Rusty Russell75676502010-06-05 11:17:36 -06002908 mutex_unlock(&module_mutex);
Rusty Russelle91defa2009-03-31 13:05:35 -06002909 synchronize_sched();
Rusty Russell6526c532010-08-05 12:59:10 -06002910 kfree(mod->args);
2911 free_arch_cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 module_arch_cleanup(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002913 free_modinfo:
Rusty Russella263f772009-09-25 00:32:58 -06002914 free_modinfo(mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002915 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 module_unload_free(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002917 free_module:
2918 module_deallocate(mod, &info);
2919 free_copy:
2920 free_copy(&info);
Jayachandran C6fe2e702006-01-06 00:19:54 -08002921 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922}
2923
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002924/* Call module constructors. */
2925static void do_mod_ctors(struct module *mod)
2926{
2927#ifdef CONFIG_CONSTRUCTORS
2928 unsigned long i;
2929
2930 for (i = 0; i < mod->num_ctors; i++)
2931 mod->ctors[i]();
2932#endif
2933}
2934
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935/* This is where the real work happens */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002936SYSCALL_DEFINE3(init_module, void __user *, umod,
2937 unsigned long, len, const char __user *, uargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938{
2939 struct module *mod;
2940 int ret = 0;
2941
2942 /* Must have permission */
Kees Cook3d433212009-04-02 15:49:29 -07002943 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 return -EPERM;
2945
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 /* Do all the hard work */
2947 mod = load_module(umod, len, uargs);
Rusty Russell75676502010-06-05 11:17:36 -06002948 if (IS_ERR(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 return PTR_ERR(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Alan Sterne041c682006-03-27 01:16:30 -08002951 blocking_notifier_call_chain(&module_notify_list,
2952 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Steven Rostedt94462ad2010-11-29 13:15:42 -05002954 /* Set RO and NX regions for core */
2955 set_section_ro_nx(mod->module_core,
2956 mod->core_text_size,
2957 mod->core_ro_size,
2958 mod->core_size);
2959
2960 /* Set RO and NX regions for init */
2961 set_section_ro_nx(mod->module_init,
2962 mod->init_text_size,
2963 mod->init_ro_size,
2964 mod->init_size);
2965
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002966 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 /* Start the module */
2968 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002969 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 if (ret < 0) {
2971 /* Init routine failed: abort. Try to protect us from
2972 buggy refcounters. */
2973 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002974 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002975 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002976 blocking_notifier_call_chain(&module_notify_list,
2977 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002978 free_module(mod);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002979 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 return ret;
2981 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002982 if (ret > 0) {
Joe Perchesad361c92009-07-06 13:05:40 -07002983 printk(KERN_WARNING
2984"%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
2985"%s: loading module anyway...\n",
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002986 __func__, mod->name, ret,
2987 __func__);
2988 dump_stack();
2989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
Rusty Russell6c5db222008-03-10 11:43:52 -07002991 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002993 wake_up(&module_wq);
Masami Hiramatsu0deddf432009-01-06 14:41:54 -08002994 blocking_notifier_call_chain(&module_notify_list,
2995 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07002996
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07002997 /* We need to finish all async code before the module init sequence is done */
2998 async_synchronize_full();
2999
Rusty Russell6c5db222008-03-10 11:43:52 -07003000 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 /* Drop initial reference. */
3002 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06003003 trim_init_extable(mod);
Jan Beulich4a496222009-07-06 14:50:42 +01003004#ifdef CONFIG_KALLSYMS
3005 mod->num_symtab = mod->core_num_syms;
3006 mod->symtab = mod->core_symtab;
Jan Beulich554bdfe2009-07-06 14:51:44 +01003007 mod->strtab = mod->core_strtab;
Jan Beulich4a496222009-07-06 14:50:42 +01003008#endif
Jan Glauber01526ed2011-05-19 16:55:26 -06003009 unset_module_init_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 module_free(mod, mod->module_init);
3011 mod->module_init = NULL;
3012 mod->init_size = 0;
Jan Glauber4d103802011-05-19 16:55:25 -06003013 mod->init_ro_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08003015 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
3017 return 0;
3018}
3019
3020static inline int within(unsigned long addr, void *start, unsigned long size)
3021{
3022 return ((void *)addr >= start && (void *)addr < start + size);
3023}
3024
3025#ifdef CONFIG_KALLSYMS
3026/*
3027 * This ignores the intensely annoying "mapping symbols" found
3028 * in ARM ELF files: $a, $t and $d.
3029 */
3030static inline int is_arm_mapping_symbol(const char *str)
3031{
Daniel Walker22a8bde2007-10-18 03:06:07 -07003032 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 && (str[2] == '\0' || str[2] == '.');
3034}
3035
3036static const char *get_ksymbol(struct module *mod,
3037 unsigned long addr,
3038 unsigned long *size,
3039 unsigned long *offset)
3040{
3041 unsigned int i, best = 0;
3042 unsigned long nextval;
3043
3044 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003045 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003047 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 nextval = (unsigned long)mod->module_core+mod->core_text_size;
3049
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003050 /* Scan for closest preceding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07003051 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 for (i = 1; i < mod->num_symtab; i++) {
3053 if (mod->symtab[i].st_shndx == SHN_UNDEF)
3054 continue;
3055
3056 /* We ignore unnamed symbols: they're uninformative
3057 * and inserted at a whim. */
3058 if (mod->symtab[i].st_value <= addr
3059 && mod->symtab[i].st_value > mod->symtab[best].st_value
3060 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3061 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3062 best = i;
3063 if (mod->symtab[i].st_value > addr
3064 && mod->symtab[i].st_value < nextval
3065 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3066 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3067 nextval = mod->symtab[i].st_value;
3068 }
3069
3070 if (!best)
3071 return NULL;
3072
Alexey Dobriyanffb45122007-05-08 00:28:41 -07003073 if (size)
3074 *size = nextval - mod->symtab[best].st_value;
3075 if (offset)
3076 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 return mod->strtab + mod->symtab[best].st_name;
3078}
3079
Rusty Russell6dd06c92008-01-29 17:13:22 -05003080/* For kallsyms to ask for address resolution. NULL means not found. Careful
3081 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003082const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05003083 unsigned long *size,
3084 unsigned long *offset,
3085 char **modname,
3086 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087{
3088 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08003089 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
Rusty Russellcb2a5202008-01-14 00:55:03 -08003091 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003092 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003093 if (within_module_init(addr, mod) ||
3094 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07003095 if (modname)
3096 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08003097 ret = get_ksymbol(mod, addr, size, offset);
3098 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 }
3100 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05003101 /* Make a copy in here where it's safe */
3102 if (ret) {
3103 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
3104 ret = namebuf;
3105 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003106 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003107 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108}
3109
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003110int lookup_module_symbol_name(unsigned long addr, char *symname)
3111{
3112 struct module *mod;
3113
Rusty Russellcb2a5202008-01-14 00:55:03 -08003114 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003115 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003116 if (within_module_init(addr, mod) ||
3117 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003118 const char *sym;
3119
3120 sym = get_ksymbol(mod, addr, NULL, NULL);
3121 if (!sym)
3122 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07003123 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003124 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003125 return 0;
3126 }
3127 }
3128out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003129 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003130 return -ERANGE;
3131}
3132
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003133int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
3134 unsigned long *offset, char *modname, char *name)
3135{
3136 struct module *mod;
3137
Rusty Russellcb2a5202008-01-14 00:55:03 -08003138 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003139 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003140 if (within_module_init(addr, mod) ||
3141 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003142 const char *sym;
3143
3144 sym = get_ksymbol(mod, addr, size, offset);
3145 if (!sym)
3146 goto out;
3147 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07003148 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003149 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07003150 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003151 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003152 return 0;
3153 }
3154 }
3155out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003156 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003157 return -ERANGE;
3158}
3159
Alexey Dobriyanea078902007-05-08 00:28:39 -07003160int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
3161 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162{
3163 struct module *mod;
3164
Rusty Russellcb2a5202008-01-14 00:55:03 -08003165 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003166 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 if (symnum < mod->num_symtab) {
3168 *value = mod->symtab[symnum].st_value;
3169 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07003170 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07003171 KSYM_NAME_LEN);
3172 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06003173 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003174 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003175 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 }
3177 symnum -= mod->num_symtab;
3178 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003179 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003180 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181}
3182
3183static unsigned long mod_find_symname(struct module *mod, const char *name)
3184{
3185 unsigned int i;
3186
3187 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08003188 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
3189 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 return mod->symtab[i].st_value;
3191 return 0;
3192}
3193
3194/* Look for this name: can be of form module:name. */
3195unsigned long module_kallsyms_lookup_name(const char *name)
3196{
3197 struct module *mod;
3198 char *colon;
3199 unsigned long ret = 0;
3200
3201 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08003202 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 if ((colon = strchr(name, ':')) != NULL) {
3204 *colon = '\0';
3205 if ((mod = find_module(name)) != NULL)
3206 ret = mod_find_symname(mod, colon+1);
3207 *colon = ':';
3208 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02003209 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 if ((ret = mod_find_symname(mod, name)) != 0)
3211 break;
3212 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003213 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 return ret;
3215}
Anders Kaseorg75a66612008-12-05 19:03:58 -05003216
3217int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
3218 struct module *, unsigned long),
3219 void *data)
3220{
3221 struct module *mod;
3222 unsigned int i;
3223 int ret;
3224
3225 list_for_each_entry(mod, &modules, list) {
3226 for (i = 0; i < mod->num_symtab; i++) {
3227 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
3228 mod, mod->symtab[i].st_value);
3229 if (ret != 0)
3230 return ret;
3231 }
3232 }
3233 return 0;
3234}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235#endif /* CONFIG_KALLSYMS */
3236
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003237static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003238{
3239 int bx = 0;
3240
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003241 if (mod->taints ||
3242 mod->state == MODULE_STATE_GOING ||
3243 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003244 buf[bx++] = '(';
Andi Kleen25ddbb12008-10-15 22:01:41 -07003245 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003246 buf[bx++] = 'P';
Ben Hutchings2449b8b2011-10-24 15:12:28 +02003247 else if (mod->taints & (1 << TAINT_OOT_MODULE))
3248 buf[bx++] = 'O';
Andi Kleen25ddbb12008-10-15 22:01:41 -07003249 if (mod->taints & (1 << TAINT_FORCED_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003250 buf[bx++] = 'F';
Linus Torvalds26e9a392008-10-17 09:50:12 -07003251 if (mod->taints & (1 << TAINT_CRAP))
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07003252 buf[bx++] = 'C';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003253 /*
3254 * TAINT_FORCED_RMMOD: could be added.
3255 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
3256 * apply to modules.
3257 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003258
3259 /* Show a - for module-is-being-unloaded */
3260 if (mod->state == MODULE_STATE_GOING)
3261 buf[bx++] = '-';
3262 /* Show a + for module-is-being-loaded */
3263 if (mod->state == MODULE_STATE_COMING)
3264 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003265 buf[bx++] = ')';
3266 }
3267 buf[bx] = '\0';
3268
3269 return buf;
3270}
3271
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003272#ifdef CONFIG_PROC_FS
3273/* Called by the /proc file system to return a list of modules. */
3274static void *m_start(struct seq_file *m, loff_t *pos)
3275{
3276 mutex_lock(&module_mutex);
3277 return seq_list_start(&modules, *pos);
3278}
3279
3280static void *m_next(struct seq_file *m, void *p, loff_t *pos)
3281{
3282 return seq_list_next(p, &modules, pos);
3283}
3284
3285static void m_stop(struct seq_file *m, void *p)
3286{
3287 mutex_unlock(&module_mutex);
3288}
3289
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290static int m_show(struct seq_file *m, void *p)
3291{
3292 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003293 char buf[8];
3294
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05003295 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 mod->name, mod->init_size + mod->core_size);
3297 print_unload_info(m, mod);
3298
3299 /* Informative for users. */
3300 seq_printf(m, " %s",
3301 mod->state == MODULE_STATE_GOING ? "Unloading":
3302 mod->state == MODULE_STATE_COMING ? "Loading":
3303 "Live");
3304 /* Used by oprofile and other similar tools. */
Kees Cook9f36e2c2011-03-22 16:34:22 -07003305 seq_printf(m, " 0x%pK", mod->module_core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003307 /* Taints info */
3308 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003309 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003310
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 seq_printf(m, "\n");
3312 return 0;
3313}
3314
3315/* Format: modulename size refcount deps address
3316
3317 Where refcount is a number or -, and deps is a comma-separated list
3318 of depends or -.
3319*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003320static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 .start = m_start,
3322 .next = m_next,
3323 .stop = m_stop,
3324 .show = m_show
3325};
3326
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003327static int modules_open(struct inode *inode, struct file *file)
3328{
3329 return seq_open(file, &modules_op);
3330}
3331
3332static const struct file_operations proc_modules_operations = {
3333 .open = modules_open,
3334 .read = seq_read,
3335 .llseek = seq_lseek,
3336 .release = seq_release,
3337};
3338
3339static int __init proc_modules_init(void)
3340{
3341 proc_create("modules", 0, NULL, &proc_modules_operations);
3342 return 0;
3343}
3344module_init(proc_modules_init);
3345#endif
3346
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347/* Given an address, look for it in the module exception tables. */
3348const struct exception_table_entry *search_module_extables(unsigned long addr)
3349{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 const struct exception_table_entry *e = NULL;
3351 struct module *mod;
3352
Rusty Russell24da1cb2007-07-15 23:41:46 -07003353 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003354 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 if (mod->num_exentries == 0)
3356 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003357
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 e = search_extable(mod->extable,
3359 mod->extable + mod->num_exentries - 1,
3360 addr);
3361 if (e)
3362 break;
3363 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07003364 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365
3366 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07003367 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 return e;
3369}
3370
Ingo Molnar4d435f92006-07-03 00:24:24 -07003371/*
Rusty Russelle6104992009-03-31 13:05:31 -06003372 * is_module_address - is this address inside a module?
3373 * @addr: the address to check.
3374 *
3375 * See is_module_text_address() if you simply want to see if the address
3376 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07003377 */
Rusty Russelle6104992009-03-31 13:05:31 -06003378bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07003379{
Rusty Russelle6104992009-03-31 13:05:31 -06003380 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003381
Rusty Russell24da1cb2007-07-15 23:41:46 -07003382 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06003383 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07003384 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07003385
Rusty Russelle6104992009-03-31 13:05:31 -06003386 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003387}
3388
Rusty Russelle6104992009-03-31 13:05:31 -06003389/*
3390 * __module_address - get the module which contains an address.
3391 * @addr: the address.
3392 *
3393 * Must be called with preempt disabled or module mutex held so that
3394 * module doesn't get freed during this.
3395 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07003396struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397{
3398 struct module *mod;
3399
Rusty Russell3a642e92008-07-22 19:24:28 -05003400 if (addr < module_addr_min || addr > module_addr_max)
3401 return NULL;
3402
Andi Kleend72b3752008-08-30 10:09:00 +02003403 list_for_each_entry_rcu(mod, &modules, list)
Rusty Russelle6104992009-03-31 13:05:31 -06003404 if (within_module_core(addr, mod)
3405 || within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 return mod;
3407 return NULL;
3408}
Tim Abbottc6b37802008-12-05 19:03:59 -05003409EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Rusty Russelle6104992009-03-31 13:05:31 -06003411/*
3412 * is_module_text_address - is this address inside module code?
3413 * @addr: the address to check.
3414 *
3415 * See is_module_address() if you simply want to see if the address is
3416 * anywhere in a module. See kernel_text_address() for testing if an
3417 * address corresponds to kernel or module code.
3418 */
3419bool is_module_text_address(unsigned long addr)
3420{
3421 bool ret;
3422
3423 preempt_disable();
3424 ret = __module_text_address(addr) != NULL;
3425 preempt_enable();
3426
3427 return ret;
3428}
3429
3430/*
3431 * __module_text_address - get the module whose code contains an address.
3432 * @addr: the address.
3433 *
3434 * Must be called with preempt disabled or module mutex held so that
3435 * module doesn't get freed during this.
3436 */
3437struct module *__module_text_address(unsigned long addr)
3438{
3439 struct module *mod = __module_address(addr);
3440 if (mod) {
3441 /* Make sure it's within the text section. */
3442 if (!within(addr, mod->module_init, mod->init_text_size)
3443 && !within(addr, mod->module_core, mod->core_text_size))
3444 mod = NULL;
3445 }
3446 return mod;
3447}
Tim Abbottc6b37802008-12-05 19:03:59 -05003448EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06003449
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450/* Don't grab lock, we're oopsing. */
3451void print_modules(void)
3452{
3453 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07003454 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455
Linus Torvaldsb2311252009-06-16 11:07:14 -07003456 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02003457 /* Most callers should already have preempt disabled, but make sure */
3458 preempt_disable();
3459 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003460 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02003461 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01003462 if (last_unloaded_module[0])
3463 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 printk("\n");
3465}
3466
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06003468/* Generate the signature for all relevant module structures here.
3469 * If these change, we don't want to try to parse the module. */
3470void module_layout(struct module *mod,
3471 struct modversion_info *ver,
3472 struct kernel_param *kp,
3473 struct kernel_symbol *ks,
Mathieu Desnoyers65498642011-01-26 17:26:22 -05003474 struct tracepoint * const *tp)
Rusty Russell8c8ef422009-03-31 13:05:34 -06003475{
3476}
3477EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478#endif