blob: e8aa462301e792ef8de00b549ec2b8dddde02adc [file] [log] [blame]
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 Copyright (C) 2002 Richard Henderson
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003 Copyright (C) 2001 Rusty Russell, 2002, 2010 Rusty Russell IBM.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
20#include <linux/moduleloader.h>
Steven Rostedt6d723732009-04-10 14:53:50 -040021#include <linux/ftrace_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
Alexey Dobriyanae84e322007-05-08 00:28:38 -070023#include <linux/kallsyms.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040024#include <linux/fs.h>
Roland McGrath6d760132007-10-16 23:26:40 -070025#include <linux/sysfs.h>
Randy Dunlap9f158332005-09-13 01:25:16 -070026#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/slab.h>
28#include <linux/vmalloc.h>
29#include <linux/elf.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040030#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/seq_file.h>
32#include <linux/syscalls.h>
33#include <linux/fcntl.h>
34#include <linux/rcupdate.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080035#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/cpu.h>
37#include <linux/moduleparam.h>
38#include <linux/errno.h>
39#include <linux/err.h>
40#include <linux/vermagic.h>
41#include <linux/notifier.h>
Al Virof6a57032006-10-18 01:47:25 -040042#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/stop_machine.h>
44#include <linux/device.h>
Matt Domschc988d2b2005-06-23 22:05:15 -070045#include <linux/string.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080046#include <linux/mutex.h>
Andi Kleend72b3752008-08-30 10:09:00 +020047#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/cacheflush.h>
Bernd Schmidteb8cdec2009-09-21 17:03:57 -070050#include <asm/mmu_context.h>
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020051#include <linux/license.h>
Christoph Lameter6d762392008-02-08 04:18:42 -080052#include <asm/sections.h>
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040053#include <linux/tracepoint.h>
Steven Rostedt90d595f2008-08-14 15:45:09 -040054#include <linux/ftrace.h>
Arjan van de Ven22a9d642009-01-07 08:45:46 -080055#include <linux/async.h>
Tejun Heofbf59bc2009-02-20 16:29:08 +090056#include <linux/percpu.h>
Catalin Marinas4f2294b2009-06-11 13:23:20 +010057#include <linux/kmemleak.h>
Jason Baronbf5438fc2010-09-17 11:09:00 -040058#include <linux/jump_label.h>
matthieu castet84e1c6b2010-11-16 22:35:16 +010059#include <linux/pfn.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Li Zefan7ead8b82009-08-17 16:56:28 +080061#define CREATE_TRACE_POINTS
62#include <trace/events/module.h>
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#if 0
65#define DEBUGP printk
66#else
67#define DEBUGP(fmt , a...)
68#endif
69
70#ifndef ARCH_SHF_SMALL
71#define ARCH_SHF_SMALL 0
72#endif
73
matthieu castet84e1c6b2010-11-16 22:35:16 +010074/*
75 * Modules' sections will be aligned on page boundaries
76 * to ensure complete separation of code and data, but
77 * only when CONFIG_DEBUG_SET_MODULE_RONX=y
78 */
79#ifdef CONFIG_DEBUG_SET_MODULE_RONX
80# define debug_align(X) ALIGN(X, PAGE_SIZE)
81#else
82# define debug_align(X) (X)
83#endif
84
85/*
86 * Given BASE and SIZE this macro calculates the number of pages the
87 * memory regions occupies
88 */
89#define MOD_NUMBER_OF_PAGES(BASE, SIZE) (((SIZE) > 0) ? \
90 (PFN_DOWN((unsigned long)(BASE) + (SIZE) - 1) - \
91 PFN_DOWN((unsigned long)BASE) + 1) \
92 : (0UL))
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/* If this is set, the section belongs in the init part of the module */
95#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
96
Rusty Russell75676502010-06-05 11:17:36 -060097/*
98 * Mutex protects:
99 * 1) List of modules (also safely readable with preempt_disable),
100 * 2) module_use links,
101 * 3) module_addr_min/module_addr_max.
Andi Kleend72b3752008-08-30 10:09:00 +0200102 * (delete uses stop_machine/add uses RCU list operations). */
Tim Abbottc6b37802008-12-05 19:03:59 -0500103DEFINE_MUTEX(module_mutex);
104EXPORT_SYMBOL_GPL(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static LIST_HEAD(modules);
Jason Wessel67fc4e02010-05-20 21:04:21 -0500106#ifdef CONFIG_KGDB_KDB
107struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
108#endif /* CONFIG_KGDB_KDB */
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Stephen Rothwell19e45292009-04-14 17:27:18 +1000111/* Block module loading/unloading? */
112int modules_disabled = 0;
113
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500114/* Waiting for a module to finish initializing? */
115static DECLARE_WAIT_QUEUE_HEAD(module_wq);
116
Alan Sterne041c682006-03-27 01:16:30 -0800117static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Rusty Russell75676502010-06-05 11:17:36 -0600119/* Bounds of module allocation, for speeding __module_address.
120 * Protected by module_mutex. */
Rusty Russell3a642e92008-07-22 19:24:28 -0500121static unsigned long module_addr_min = -1UL, module_addr_max = 0;
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123int register_module_notifier(struct notifier_block * nb)
124{
Alan Sterne041c682006-03-27 01:16:30 -0800125 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127EXPORT_SYMBOL(register_module_notifier);
128
129int unregister_module_notifier(struct notifier_block * nb)
130{
Alan Sterne041c682006-03-27 01:16:30 -0800131 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133EXPORT_SYMBOL(unregister_module_notifier);
134
Rusty Russelleded41c2010-08-05 12:59:07 -0600135struct load_info {
136 Elf_Ehdr *hdr;
137 unsigned long len;
138 Elf_Shdr *sechdrs;
Rusty Russell6526c532010-08-05 12:59:10 -0600139 char *secstrings, *strtab;
Rusty Russelld9131882010-08-05 12:59:08 -0600140 unsigned long *strmap;
141 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
Rusty Russellde4d8d52011-04-19 21:49:58 +0200366static bool find_symbol_in_section(const struct symsearch *syms,
367 struct module *owner,
368 void *data)
369{
370 struct find_symbol_arg *fsa = data;
371 unsigned int i;
372
373 for (i = 0; i < syms->stop - syms->start; i++) {
374 if (strcmp(syms->start[i].name, fsa->name) == 0)
375 return check_symbol(syms, owner, i, data);
376 }
377 return false;
378}
379
Tim Abbott414fd312008-12-05 19:03:56 -0500380/* Find a symbol and return it, along with, (optional) crc and
Rusty Russell75676502010-06-05 11:17:36 -0600381 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500382const struct kernel_symbol *find_symbol(const char *name,
383 struct module **owner,
384 const unsigned long **crc,
385 bool gplok,
386 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Rusty Russelldafd0942008-07-22 19:24:25 -0500388 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Rusty Russelldafd0942008-07-22 19:24:25 -0500390 fsa.name = name;
391 fsa.gplok = gplok;
392 fsa.warn = warn;
393
Rusty Russellde4d8d52011-04-19 21:49:58 +0200394 if (each_symbol_section(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500395 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500396 *owner = fsa.owner;
397 if (crc)
398 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500399 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 DEBUGP("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500403 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
Tim Abbottc6b37802008-12-05 19:03:59 -0500405EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407/* Search for module by name: must hold module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500408struct module *find_module(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 struct module *mod;
411
412 list_for_each_entry(mod, &modules, list) {
413 if (strcmp(mod->name, name) == 0)
414 return mod;
415 }
416 return NULL;
417}
Tim Abbottc6b37802008-12-05 19:03:59 -0500418EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900421
Tejun Heo259354d2010-03-10 18:56:10 +0900422static inline void __percpu *mod_percpu(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900423{
Tejun Heo259354d2010-03-10 18:56:10 +0900424 return mod->percpu;
425}
Tejun Heofbf59bc2009-02-20 16:29:08 +0900426
Tejun Heo259354d2010-03-10 18:56:10 +0900427static int percpu_modalloc(struct module *mod,
428 unsigned long size, unsigned long align)
429{
Tejun Heofbf59bc2009-02-20 16:29:08 +0900430 if (align > PAGE_SIZE) {
431 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
Tejun Heo259354d2010-03-10 18:56:10 +0900432 mod->name, align, PAGE_SIZE);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900433 align = PAGE_SIZE;
434 }
435
Tejun Heo259354d2010-03-10 18:56:10 +0900436 mod->percpu = __alloc_reserved_percpu(size, align);
437 if (!mod->percpu) {
Tejun Heofbf59bc2009-02-20 16:29:08 +0900438 printk(KERN_WARNING
Rusty Russelld9131882010-08-05 12:59:08 -0600439 "%s: Could not allocate %lu bytes percpu data\n",
440 mod->name, size);
Tejun Heo259354d2010-03-10 18:56:10 +0900441 return -ENOMEM;
442 }
443 mod->percpu_size = size;
444 return 0;
Tejun Heofbf59bc2009-02-20 16:29:08 +0900445}
446
Tejun Heo259354d2010-03-10 18:56:10 +0900447static void percpu_modfree(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900448{
Tejun Heo259354d2010-03-10 18:56:10 +0900449 free_percpu(mod->percpu);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900450}
451
Rusty Russell49668682010-08-05 12:59:10 -0600452static unsigned int find_pcpusec(struct load_info *info)
Tejun Heo6b588c12009-02-20 16:29:07 +0900453{
Rusty Russell49668682010-08-05 12:59:10 -0600454 return find_sec(info, ".data..percpu");
Tejun Heo6b588c12009-02-20 16:29:07 +0900455}
456
Tejun Heo259354d2010-03-10 18:56:10 +0900457static void percpu_modcopy(struct module *mod,
458 const void *from, unsigned long size)
Tejun Heo6b588c12009-02-20 16:29:07 +0900459{
460 int cpu;
461
462 for_each_possible_cpu(cpu)
Tejun Heo259354d2010-03-10 18:56:10 +0900463 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
Tejun Heo6b588c12009-02-20 16:29:07 +0900464}
465
Tejun Heo10fad5e2010-03-10 18:57:54 +0900466/**
467 * is_module_percpu_address - test whether address is from module static percpu
468 * @addr: address to test
469 *
470 * Test whether @addr belongs to module static percpu area.
471 *
472 * RETURNS:
473 * %true if @addr is from module static percpu area
474 */
475bool is_module_percpu_address(unsigned long addr)
476{
477 struct module *mod;
478 unsigned int cpu;
479
480 preempt_disable();
481
482 list_for_each_entry_rcu(mod, &modules, list) {
483 if (!mod->percpu_size)
484 continue;
485 for_each_possible_cpu(cpu) {
486 void *start = per_cpu_ptr(mod->percpu, cpu);
487
488 if ((void *)addr >= start &&
489 (void *)addr < start + mod->percpu_size) {
490 preempt_enable();
491 return true;
492 }
493 }
494 }
495
496 preempt_enable();
497 return false;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700498}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900501
Tejun Heo259354d2010-03-10 18:56:10 +0900502static inline void __percpu *mod_percpu(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 return NULL;
505}
Tejun Heo259354d2010-03-10 18:56:10 +0900506static inline int percpu_modalloc(struct module *mod,
507 unsigned long size, unsigned long align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Tejun Heo259354d2010-03-10 18:56:10 +0900509 return -ENOMEM;
510}
511static inline void percpu_modfree(struct module *mod)
512{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
Rusty Russell49668682010-08-05 12:59:10 -0600514static unsigned int find_pcpusec(struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 return 0;
517}
Tejun Heo259354d2010-03-10 18:56:10 +0900518static inline void percpu_modcopy(struct module *mod,
519 const void *from, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 /* pcpusec should be 0, and size of that section should be 0. */
522 BUG_ON(size != 0);
523}
Tejun Heo10fad5e2010-03-10 18:57:54 +0900524bool is_module_percpu_address(unsigned long addr)
525{
526 return false;
527}
Tejun Heo6b588c12009-02-20 16:29:07 +0900528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529#endif /* CONFIG_SMP */
530
Matt Domschc988d2b2005-06-23 22:05:15 -0700531#define MODINFO_ATTR(field) \
532static void setup_modinfo_##field(struct module *mod, const char *s) \
533{ \
534 mod->field = kstrdup(s, GFP_KERNEL); \
535} \
536static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
537 struct module *mod, char *buffer) \
538{ \
539 return sprintf(buffer, "%s\n", mod->field); \
540} \
541static int modinfo_##field##_exists(struct module *mod) \
542{ \
543 return mod->field != NULL; \
544} \
545static void free_modinfo_##field(struct module *mod) \
546{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700547 kfree(mod->field); \
548 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700549} \
550static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900551 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700552 .show = show_modinfo_##field, \
553 .setup = setup_modinfo_##field, \
554 .test = modinfo_##field##_exists, \
555 .free = free_modinfo_##field, \
556};
557
558MODINFO_ATTR(version);
559MODINFO_ATTR(srcversion);
560
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100561static char last_unloaded_module[MODULE_NAME_LEN+1];
562
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800563#ifdef CONFIG_MODULE_UNLOAD
Steven Rostedteb0c5372010-03-29 14:25:18 -0400564
565EXPORT_TRACEPOINT_SYMBOL(module_get);
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567/* Init the unload section of the module. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600568static int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600570 mod->refptr = alloc_percpu(struct module_ref);
571 if (!mod->refptr)
572 return -ENOMEM;
573
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700574 INIT_LIST_HEAD(&mod->source_list);
575 INIT_LIST_HEAD(&mod->target_list);
Christoph Lametere1783a22010-01-05 15:34:50 +0900576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /* Hold reference count during initialization. */
Nick Piggin5fbfb182010-04-01 19:09:40 +1100578 __this_cpu_write(mod->refptr->incs, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 /* Backwards compatibility macros put refcount during init. */
580 mod->waiter = current;
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600581
582 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585/* Does a already use b? */
586static int already_uses(struct module *a, struct module *b)
587{
588 struct module_use *use;
589
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700590 list_for_each_entry(use, &b->source_list, source_list) {
591 if (use->source == a) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 DEBUGP("%s uses %s!\n", a->name, b->name);
593 return 1;
594 }
595 }
596 DEBUGP("%s does not use %s!\n", a->name, b->name);
597 return 0;
598}
599
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700600/*
601 * Module a uses b
602 * - we add 'a' as a "source", 'b' as a "target" of module use
603 * - the module_use is added to the list of 'b' sources (so
604 * 'b' can walk the list to see who sourced them), and of 'a'
605 * targets (so 'a' can see what modules it targets).
606 */
607static int add_module_usage(struct module *a, struct module *b)
608{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700609 struct module_use *use;
610
611 DEBUGP("Allocating new usage for %s.\n", a->name);
612 use = kmalloc(sizeof(*use), GFP_ATOMIC);
613 if (!use) {
614 printk(KERN_WARNING "%s: out of memory loading\n", a->name);
615 return -ENOMEM;
616 }
617
618 use->source = a;
619 use->target = b;
620 list_add(&use->source_list, &b->source_list);
621 list_add(&use->target_list, &a->target_list);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700622 return 0;
623}
624
Rusty Russell75676502010-06-05 11:17:36 -0600625/* Module a uses b: caller needs module_mutex() */
Rusty Russell9bea7f22010-06-05 11:17:37 -0600626int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Rusty Russellc8e21ce2010-06-05 11:17:35 -0600628 int err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100629
Rusty Russell9bea7f22010-06-05 11:17:37 -0600630 if (b == NULL || already_uses(a, b))
Linus Torvalds218ce732010-05-25 16:48:30 -0700631 return 0;
Linus Torvalds218ce732010-05-25 16:48:30 -0700632
Rusty Russell9bea7f22010-06-05 11:17:37 -0600633 /* If module isn't available, we fail. */
634 err = strong_try_module_get(b);
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500635 if (err)
Rusty Russell9bea7f22010-06-05 11:17:37 -0600636 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700638 err = add_module_usage(a, b);
639 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 module_put(b);
Rusty Russell9bea7f22010-06-05 11:17:37 -0600641 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
Rusty Russell9bea7f22010-06-05 11:17:37 -0600643 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600645EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647/* Clear the unload stuff of the module. */
648static void module_unload_free(struct module *mod)
649{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700650 struct module_use *use, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Rusty Russell75676502010-06-05 11:17:36 -0600652 mutex_lock(&module_mutex);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700653 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
654 struct module *i = use->target;
655 DEBUGP("%s unusing %s\n", mod->name, i->name);
656 module_put(i);
657 list_del(&use->source_list);
658 list_del(&use->target_list);
659 kfree(use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
Rusty Russell75676502010-06-05 11:17:36 -0600661 mutex_unlock(&module_mutex);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600662
663 free_percpu(mod->refptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
666#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800667static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 int ret = (flags & O_TRUNC);
670 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800671 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return ret;
673}
674#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800675static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
677 return 0;
678}
679#endif /* CONFIG_MODULE_FORCE_UNLOAD */
680
681struct stopref
682{
683 struct module *mod;
684 int flags;
685 int *forced;
686};
687
688/* Whole machine is stopped with interrupts off when this runs. */
689static int __try_stop_module(void *_sref)
690{
691 struct stopref *sref = _sref;
692
Rusty Russellda39ba52008-07-22 19:24:25 -0500693 /* If it's not unused, quit unless we're forcing. */
694 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800695 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return -EWOULDBLOCK;
697 }
698
699 /* Mark it as dying. */
700 sref->mod->state = MODULE_STATE_GOING;
701 return 0;
702}
703
704static int try_stop_module(struct module *mod, int flags, int *forced)
705{
Rusty Russellda39ba52008-07-22 19:24:25 -0500706 if (flags & O_NONBLOCK) {
707 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500709 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500710 } else {
711 /* We don't need to stop the machine for this. */
712 mod->state = MODULE_STATE_GOING;
713 synchronize_sched();
714 return 0;
715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
718unsigned int module_refcount(struct module *mod)
719{
Nick Piggin5fbfb182010-04-01 19:09:40 +1100720 unsigned int incs = 0, decs = 0;
Eric Dumazet720eba32009-02-03 13:31:36 +1030721 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Eric Dumazet720eba32009-02-03 13:31:36 +1030723 for_each_possible_cpu(cpu)
Nick Piggin5fbfb182010-04-01 19:09:40 +1100724 decs += per_cpu_ptr(mod->refptr, cpu)->decs;
725 /*
726 * ensure the incs are added up after the decs.
727 * module_put ensures incs are visible before decs with smp_wmb.
728 *
729 * This 2-count scheme avoids the situation where the refcount
730 * for CPU0 is read, then CPU0 increments the module refcount,
731 * then CPU1 drops that refcount, then the refcount for CPU1 is
732 * read. We would record a decrement but not its corresponding
733 * increment so we would see a low count (disaster).
734 *
735 * Rare situation? But module_refcount can be preempted, and we
736 * might be tallying up 4096+ CPUs. So it is not impossible.
737 */
738 smp_rmb();
739 for_each_possible_cpu(cpu)
740 incs += per_cpu_ptr(mod->refptr, cpu)->incs;
741 return incs - decs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743EXPORT_SYMBOL(module_refcount);
744
745/* This exists whether we can unload or not */
746static void free_module(struct module *mod);
747
748static void wait_for_zero_refcount(struct module *mod)
749{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500750 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800751 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 for (;;) {
753 DEBUGP("Looking at refcount...\n");
754 set_current_state(TASK_UNINTERRUPTIBLE);
755 if (module_refcount(mod) == 0)
756 break;
757 schedule();
758 }
759 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800760 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100763SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
764 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800767 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 int ret, forced = 0;
769
Kees Cook3d433212009-04-02 15:49:29 -0700770 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800771 return -EPERM;
772
773 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
774 return -EFAULT;
775 name[MODULE_NAME_LEN-1] = '\0';
776
Tejun Heo3fc1f1e2010-05-06 18:49:20 +0200777 if (mutex_lock_interruptible(&module_mutex) != 0)
778 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 mod = find_module(name);
781 if (!mod) {
782 ret = -ENOENT;
783 goto out;
784 }
785
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700786 if (!list_empty(&mod->source_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 /* Other modules depend on us: get rid of them first. */
788 ret = -EWOULDBLOCK;
789 goto out;
790 }
791
792 /* Doing init or already dying? */
793 if (mod->state != MODULE_STATE_LIVE) {
794 /* FIXME: if (force), slam module count and wake up
795 waiter --RR */
796 DEBUGP("%s already dying\n", mod->name);
797 ret = -EBUSY;
798 goto out;
799 }
800
801 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700802 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800803 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (!forced) {
805 /* This module can't be removed */
806 ret = -EBUSY;
807 goto out;
808 }
809 }
810
811 /* Set this up before setting mod->state */
812 mod->waiter = current;
813
814 /* Stop the machine so refcounts can't move and disable module. */
815 ret = try_stop_module(mod, flags, &forced);
816 if (ret != 0)
817 goto out;
818
819 /* Never wait if forced. */
820 if (!forced && module_refcount(mod) != 0)
821 wait_for_zero_refcount(mod);
822
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200823 mutex_unlock(&module_mutex);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300824 /* Final destruction now no one is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200825 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200827 blocking_notifier_call_chain(&module_notify_list,
828 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800829 async_synchronize_full();
Rusty Russell75676502010-06-05 11:17:36 -0600830
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100831 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500832 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Rusty Russell75676502010-06-05 11:17:36 -0600834 free_module(mod);
835 return 0;
836out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800837 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return ret;
839}
840
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800841static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 struct module_use *use;
844 int printed_something = 0;
845
846 seq_printf(m, " %u ", module_refcount(mod));
847
848 /* Always include a trailing , so userspace can differentiate
849 between this and the old multi-field proc format. */
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700850 list_for_each_entry(use, &mod->source_list, source_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 printed_something = 1;
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700852 seq_printf(m, "%s,", use->source->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (mod->init != NULL && mod->exit == NULL) {
856 printed_something = 1;
857 seq_printf(m, "[permanent],");
858 }
859
860 if (!printed_something)
861 seq_printf(m, "-");
862}
863
864void __symbol_put(const char *symbol)
865{
866 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Rusty Russell24da1cb2007-07-15 23:41:46 -0700868 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -0500869 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 BUG();
871 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700872 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874EXPORT_SYMBOL(__symbol_put);
875
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930876/* Note this assumes addr is a function, which it currently always is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877void symbol_put_addr(void *addr)
878{
Trent Piepho5e376612006-05-15 09:44:06 -0700879 struct module *modaddr;
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930880 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930882 if (core_kernel_text(a))
Trent Piepho5e376612006-05-15 09:44:06 -0700883 return;
884
Rusty Russella6e6abd2009-03-31 13:05:31 -0600885 /* module_text_address is safe here: we're supposed to have reference
886 * to module from symbol_get, so it can't go away. */
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930887 modaddr = __module_text_address(a);
Rusty Russella6e6abd2009-03-31 13:05:31 -0600888 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -0700889 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891EXPORT_SYMBOL_GPL(symbol_put_addr);
892
893static ssize_t show_refcnt(struct module_attribute *mattr,
894 struct module *mod, char *buffer)
895{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400896 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898
899static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900900 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 .show = show_refcnt,
902};
903
Al Virof6a57032006-10-18 01:47:25 -0400904void module_put(struct module *module)
905{
906 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +0900907 preempt_disable();
Nick Piggin5fbfb182010-04-01 19:09:40 +1100908 smp_wmb(); /* see comment in module_refcount */
909 __this_cpu_inc(module->refptr->decs);
Christoph Lametere1783a22010-01-05 15:34:50 +0900910
Li Zefanae832d12010-03-24 10:57:43 +0800911 trace_module_put(module, _RET_IP_);
Al Virof6a57032006-10-18 01:47:25 -0400912 /* Maybe they're waiting for us to drop reference? */
913 if (unlikely(!module_is_live(module)))
914 wake_up_process(module->waiter);
Christoph Lametere1783a22010-01-05 15:34:50 +0900915 preempt_enable();
Al Virof6a57032006-10-18 01:47:25 -0400916 }
917}
918EXPORT_SYMBOL(module_put);
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800921static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 /* We don't know the usage count, or what modules are using. */
924 seq_printf(m, " - -");
925}
926
927static inline void module_unload_free(struct module *mod)
928{
929}
930
Rusty Russell9bea7f22010-06-05 11:17:37 -0600931int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Rusty Russell9bea7f22010-06-05 11:17:37 -0600933 return strong_try_module_get(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600935EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600937static inline int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600939 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941#endif /* CONFIG_MODULE_UNLOAD */
942
Kay Sievers1f717402006-11-24 12:15:25 +0100943static ssize_t show_initstate(struct module_attribute *mattr,
944 struct module *mod, char *buffer)
945{
946 const char *state = "unknown";
947
948 switch (mod->state) {
949 case MODULE_STATE_LIVE:
950 state = "live";
951 break;
952 case MODULE_STATE_COMING:
953 state = "coming";
954 break;
955 case MODULE_STATE_GOING:
956 state = "going";
957 break;
958 }
959 return sprintf(buffer, "%s\n", state);
960}
961
962static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900963 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100964 .show = show_initstate,
965};
966
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800967static struct module_attribute *modinfo_attrs[] = {
968 &modinfo_version,
969 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100970 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800971#ifdef CONFIG_MODULE_UNLOAD
972 &refcnt,
973#endif
974 NULL,
975};
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977static const char vermagic[] = VERMAGIC_STRING;
978
Rusty Russellc6e665c2009-03-31 13:05:33 -0600979static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -0700980{
981#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -0700982 if (!test_taint(TAINT_FORCED_MODULE))
Rusty Russellc6e665c2009-03-31 13:05:33 -0600983 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
984 mod->name, reason);
Linus Torvalds826e4502008-05-04 17:04:16 -0700985 add_taint_module(mod, TAINT_FORCED_MODULE);
986 return 0;
987#else
988 return -ENOEXEC;
989#endif
990}
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992#ifdef CONFIG_MODVERSIONS
Rusty Russelld4703ae2009-12-15 16:28:32 -0600993/* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
994static unsigned long maybe_relocated(unsigned long crc,
995 const struct module *crc_owner)
996{
997#ifdef ARCH_RELOCATES_KCRCTAB
998 if (crc_owner == NULL)
999 return crc - (unsigned long)reloc_start;
1000#endif
1001 return crc;
1002}
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004static int check_version(Elf_Shdr *sechdrs,
1005 unsigned int versindex,
1006 const char *symname,
1007 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001008 const unsigned long *crc,
1009 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
1011 unsigned int i, num_versions;
1012 struct modversion_info *versions;
1013
1014 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1015 if (!crc)
1016 return 1;
1017
Rusty Russella5dd6972008-05-09 16:24:21 +10001018 /* No versions at all? modprobe --force does this. */
1019 if (versindex == 0)
1020 return try_to_force_load(mod, symname) == 0;
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 versions = (void *) sechdrs[versindex].sh_addr;
1023 num_versions = sechdrs[versindex].sh_size
1024 / sizeof(struct modversion_info);
1025
1026 for (i = 0; i < num_versions; i++) {
1027 if (strcmp(versions[i].name, symname) != 0)
1028 continue;
1029
Rusty Russelld4703ae2009-12-15 16:28:32 -06001030 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 DEBUGP("Found checksum %lX vs module %lX\n",
Rusty Russelld4703ae2009-12-15 16:28:32 -06001033 maybe_relocated(*crc, crc_owner), versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001034 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001036
Rusty Russella5dd6972008-05-09 16:24:21 +10001037 printk(KERN_WARNING "%s: no symbol version for %s\n",
1038 mod->name, symname);
1039 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001040
1041bad_version:
1042 printk("%s: disagrees about version of symbol %s\n",
1043 mod->name, symname);
1044 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
1047static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1048 unsigned int versindex,
1049 struct module *mod)
1050{
1051 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Rusty Russell75676502010-06-05 11:17:36 -06001053 /* Since this should be found in kernel (which can't be removed),
1054 * no locking is necessary. */
Mike Frysinger6560dc12009-07-23 23:42:08 +09301055 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1056 &crc, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 BUG();
Rusty Russelld4703ae2009-12-15 16:28:32 -06001058 return check_version(sechdrs, versindex, "module_layout", mod, crc,
1059 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
Rusty Russell91e37a72008-05-09 16:25:28 +10001062/* First part is kernel version, which we ignore if module has crcs. */
1063static inline int same_magic(const char *amagic, const char *bmagic,
1064 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
Rusty Russell91e37a72008-05-09 16:25:28 +10001066 if (has_crcs) {
1067 amagic += strcspn(amagic, " ");
1068 bmagic += strcspn(bmagic, " ");
1069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 return strcmp(amagic, bmagic) == 0;
1071}
1072#else
1073static inline int check_version(Elf_Shdr *sechdrs,
1074 unsigned int versindex,
1075 const char *symname,
1076 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001077 const unsigned long *crc,
1078 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
1080 return 1;
1081}
1082
1083static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1084 unsigned int versindex,
1085 struct module *mod)
1086{
1087 return 1;
1088}
1089
Rusty Russell91e37a72008-05-09 16:25:28 +10001090static inline int same_magic(const char *amagic, const char *bmagic,
1091 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
1093 return strcmp(amagic, bmagic) == 0;
1094}
1095#endif /* CONFIG_MODVERSIONS */
1096
Rusty Russell75676502010-06-05 11:17:36 -06001097/* Resolve a symbol for this module. I.e. if we find one, record usage. */
Rusty Russell49668682010-08-05 12:59:10 -06001098static const struct kernel_symbol *resolve_symbol(struct module *mod,
1099 const struct load_info *info,
Tim Abbott414fd312008-12-05 19:03:56 -05001100 const char *name,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001101 char ownername[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
1103 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001104 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 const unsigned long *crc;
Rusty Russell9bea7f22010-06-05 11:17:37 -06001106 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Rusty Russell75676502010-06-05 11:17:36 -06001108 mutex_lock(&module_mutex);
Tim Abbott414fd312008-12-05 19:03:56 -05001109 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001110 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001111 if (!sym)
1112 goto unlock;
1113
Rusty Russell49668682010-08-05 12:59:10 -06001114 if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
1115 owner)) {
Rusty Russell9bea7f22010-06-05 11:17:37 -06001116 sym = ERR_PTR(-EINVAL);
1117 goto getname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
Rusty Russell9bea7f22010-06-05 11:17:37 -06001119
1120 err = ref_module(mod, owner);
1121 if (err) {
1122 sym = ERR_PTR(err);
1123 goto getname;
1124 }
1125
1126getname:
1127 /* We must make copy under the lock if we failed to get ref. */
1128 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1129unlock:
Rusty Russell75676502010-06-05 11:17:36 -06001130 mutex_unlock(&module_mutex);
Linus Torvalds218ce732010-05-25 16:48:30 -07001131 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132}
1133
Rusty Russell49668682010-08-05 12:59:10 -06001134static const struct kernel_symbol *
1135resolve_symbol_wait(struct module *mod,
1136 const struct load_info *info,
1137 const char *name)
Rusty Russell9bea7f22010-06-05 11:17:37 -06001138{
1139 const struct kernel_symbol *ksym;
Rusty Russell49668682010-08-05 12:59:10 -06001140 char owner[MODULE_NAME_LEN];
Rusty Russell9bea7f22010-06-05 11:17:37 -06001141
1142 if (wait_event_interruptible_timeout(module_wq,
Rusty Russell49668682010-08-05 12:59:10 -06001143 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1144 || PTR_ERR(ksym) != -EBUSY,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001145 30 * HZ) <= 0) {
1146 printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
Rusty Russell49668682010-08-05 12:59:10 -06001147 mod->name, owner);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001148 }
1149 return ksym;
1150}
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152/*
1153 * /sys/module/foo/sections stuff
1154 * J. Corbet <corbet@lwn.net>
1155 */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001156#ifdef CONFIG_SYSFS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001157
Rusty Russell8f6d0372010-08-05 12:59:09 -06001158#ifdef CONFIG_KALLSYMS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001159static inline bool sect_empty(const Elf_Shdr *sect)
1160{
1161 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1162}
1163
Rusty Russella58730c2008-03-13 09:03:44 +00001164struct module_sect_attr
1165{
1166 struct module_attribute mattr;
1167 char *name;
1168 unsigned long address;
1169};
1170
1171struct module_sect_attrs
1172{
1173 struct attribute_group grp;
1174 unsigned int nsections;
1175 struct module_sect_attr attrs[0];
1176};
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178static ssize_t module_sect_show(struct module_attribute *mattr,
1179 struct module *mod, char *buf)
1180{
1181 struct module_sect_attr *sattr =
1182 container_of(mattr, struct module_sect_attr, mattr);
Kees Cook9f36e2c2011-03-22 16:34:22 -07001183 return sprintf(buf, "0x%pK\n", (void *)sattr->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184}
1185
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001186static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1187{
Rusty Russella58730c2008-03-13 09:03:44 +00001188 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001189
1190 for (section = 0; section < sect_attrs->nsections; section++)
1191 kfree(sect_attrs->attrs[section].name);
1192 kfree(sect_attrs);
1193}
1194
Rusty Russell8f6d0372010-08-05 12:59:09 -06001195static void add_sect_attrs(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
1197 unsigned int nloaded = 0, i, size[2];
1198 struct module_sect_attrs *sect_attrs;
1199 struct module_sect_attr *sattr;
1200 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 /* Count loaded sections and allocate structures */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001203 for (i = 0; i < info->hdr->e_shnum; i++)
1204 if (!sect_empty(&info->sechdrs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 nloaded++;
1206 size[0] = ALIGN(sizeof(*sect_attrs)
1207 + nloaded * sizeof(sect_attrs->attrs[0]),
1208 sizeof(sect_attrs->grp.attrs[0]));
1209 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001210 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1211 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return;
1213
1214 /* Setup section attributes. */
1215 sect_attrs->grp.name = "sections";
1216 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1217
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001218 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 sattr = &sect_attrs->attrs[0];
1220 gattr = &sect_attrs->grp.attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001221 for (i = 0; i < info->hdr->e_shnum; i++) {
1222 Elf_Shdr *sec = &info->sechdrs[i];
1223 if (sect_empty(sec))
Helge Deller35dead42009-12-03 00:29:15 +01001224 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001225 sattr->address = sec->sh_addr;
1226 sattr->name = kstrdup(info->secstrings + sec->sh_name,
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001227 GFP_KERNEL);
1228 if (sattr->name == NULL)
1229 goto out;
1230 sect_attrs->nsections++;
Eric W. Biederman361795b2010-02-12 13:41:56 -08001231 sysfs_attr_init(&sattr->mattr.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 sattr->mattr.show = module_sect_show;
1233 sattr->mattr.store = NULL;
1234 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 sattr->mattr.attr.mode = S_IRUGO;
1236 *(gattr++) = &(sattr++)->mattr.attr;
1237 }
1238 *gattr = NULL;
1239
1240 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1241 goto out;
1242
1243 mod->sect_attrs = sect_attrs;
1244 return;
1245 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001246 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
1248
1249static void remove_sect_attrs(struct module *mod)
1250{
1251 if (mod->sect_attrs) {
1252 sysfs_remove_group(&mod->mkobj.kobj,
1253 &mod->sect_attrs->grp);
1254 /* We are positive that no one is using any sect attrs
1255 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001256 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 mod->sect_attrs = NULL;
1258 }
1259}
1260
Roland McGrath6d760132007-10-16 23:26:40 -07001261/*
1262 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1263 */
1264
1265struct module_notes_attrs {
1266 struct kobject *dir;
1267 unsigned int notes;
1268 struct bin_attribute attrs[0];
1269};
1270
Chris Wright2c3c8be2010-05-12 18:28:57 -07001271static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
Roland McGrath6d760132007-10-16 23:26:40 -07001272 struct bin_attribute *bin_attr,
1273 char *buf, loff_t pos, size_t count)
1274{
1275 /*
1276 * The caller checked the pos and count against our size.
1277 */
1278 memcpy(buf, bin_attr->private + pos, count);
1279 return count;
1280}
1281
1282static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1283 unsigned int i)
1284{
1285 if (notes_attrs->dir) {
1286 while (i-- > 0)
1287 sysfs_remove_bin_file(notes_attrs->dir,
1288 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001289 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001290 }
1291 kfree(notes_attrs);
1292}
1293
Rusty Russell8f6d0372010-08-05 12:59:09 -06001294static void add_notes_attrs(struct module *mod, const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001295{
1296 unsigned int notes, loaded, i;
1297 struct module_notes_attrs *notes_attrs;
1298 struct bin_attribute *nattr;
1299
Ingo Molnarea6bff32009-08-28 10:44:56 +02001300 /* failed to create section attributes, so can't create notes */
1301 if (!mod->sect_attrs)
1302 return;
1303
Roland McGrath6d760132007-10-16 23:26:40 -07001304 /* Count notes sections and allocate structures. */
1305 notes = 0;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001306 for (i = 0; i < info->hdr->e_shnum; i++)
1307 if (!sect_empty(&info->sechdrs[i]) &&
1308 (info->sechdrs[i].sh_type == SHT_NOTE))
Roland McGrath6d760132007-10-16 23:26:40 -07001309 ++notes;
1310
1311 if (notes == 0)
1312 return;
1313
1314 notes_attrs = kzalloc(sizeof(*notes_attrs)
1315 + notes * sizeof(notes_attrs->attrs[0]),
1316 GFP_KERNEL);
1317 if (notes_attrs == NULL)
1318 return;
1319
1320 notes_attrs->notes = notes;
1321 nattr = &notes_attrs->attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001322 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1323 if (sect_empty(&info->sechdrs[i]))
Roland McGrath6d760132007-10-16 23:26:40 -07001324 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001325 if (info->sechdrs[i].sh_type == SHT_NOTE) {
Eric W. Biederman361795b2010-02-12 13:41:56 -08001326 sysfs_bin_attr_init(nattr);
Roland McGrath6d760132007-10-16 23:26:40 -07001327 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1328 nattr->attr.mode = S_IRUGO;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001329 nattr->size = info->sechdrs[i].sh_size;
1330 nattr->private = (void *) info->sechdrs[i].sh_addr;
Roland McGrath6d760132007-10-16 23:26:40 -07001331 nattr->read = module_notes_read;
1332 ++nattr;
1333 }
1334 ++loaded;
1335 }
1336
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001337 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001338 if (!notes_attrs->dir)
1339 goto out;
1340
1341 for (i = 0; i < notes; ++i)
1342 if (sysfs_create_bin_file(notes_attrs->dir,
1343 &notes_attrs->attrs[i]))
1344 goto out;
1345
1346 mod->notes_attrs = notes_attrs;
1347 return;
1348
1349 out:
1350 free_notes_attrs(notes_attrs, i);
1351}
1352
1353static void remove_notes_attrs(struct module *mod)
1354{
1355 if (mod->notes_attrs)
1356 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1357}
1358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001360
Rusty Russell8f6d0372010-08-05 12:59:09 -06001361static inline void add_sect_attrs(struct module *mod,
1362 const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
1364}
1365
1366static inline void remove_sect_attrs(struct module *mod)
1367{
1368}
Roland McGrath6d760132007-10-16 23:26:40 -07001369
Rusty Russell8f6d0372010-08-05 12:59:09 -06001370static inline void add_notes_attrs(struct module *mod,
1371 const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001372{
1373}
1374
1375static inline void remove_notes_attrs(struct module *mod)
1376{
1377}
Rusty Russell8f6d0372010-08-05 12:59:09 -06001378#endif /* CONFIG_KALLSYMS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001380static void add_usage_links(struct module *mod)
1381{
1382#ifdef CONFIG_MODULE_UNLOAD
1383 struct module_use *use;
1384 int nowarn;
1385
Rusty Russell75676502010-06-05 11:17:36 -06001386 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001387 list_for_each_entry(use, &mod->target_list, target_list) {
1388 nowarn = sysfs_create_link(use->target->holders_dir,
1389 &mod->mkobj.kobj, mod->name);
1390 }
Rusty Russell75676502010-06-05 11:17:36 -06001391 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001392#endif
1393}
1394
1395static void del_usage_links(struct module *mod)
1396{
1397#ifdef CONFIG_MODULE_UNLOAD
1398 struct module_use *use;
1399
Rusty Russell75676502010-06-05 11:17:36 -06001400 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001401 list_for_each_entry(use, &mod->target_list, target_list)
1402 sysfs_remove_link(use->target->holders_dir, mod->name);
Rusty Russell75676502010-06-05 11:17:36 -06001403 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001404#endif
1405}
1406
Rusty Russell6407ebb22010-06-05 11:17:36 -06001407static int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001408{
1409 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001410 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001411 int error = 0;
1412 int i;
1413
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001414 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1415 (ARRAY_SIZE(modinfo_attrs) + 1)),
1416 GFP_KERNEL);
1417 if (!mod->modinfo_attrs)
1418 return -ENOMEM;
1419
1420 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001421 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1422 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001423 (attr->test && attr->test(mod))) {
1424 memcpy(temp_attr, attr, sizeof(*temp_attr));
Eric W. Biederman361795b2010-02-12 13:41:56 -08001425 sysfs_attr_init(&temp_attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001426 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1427 ++temp_attr;
1428 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001429 }
1430 return error;
1431}
1432
Rusty Russell6407ebb22010-06-05 11:17:36 -06001433static void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001434{
1435 struct module_attribute *attr;
1436 int i;
1437
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001438 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1439 /* pick a field to test for end of list */
1440 if (!attr->attr.name)
1441 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001442 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001443 if (attr->free)
1444 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001445 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001446 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001447}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Rusty Russell6407ebb22010-06-05 11:17:36 -06001449static int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
1451 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001452 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001454 if (!module_sysfs_initialized) {
1455 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001456 mod->name);
1457 err = -EINVAL;
1458 goto out;
1459 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001460
1461 kobj = kset_find_obj(module_kset, mod->name);
1462 if (kobj) {
1463 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1464 kobject_put(kobj);
1465 err = -EINVAL;
1466 goto out;
1467 }
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001470
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001471 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1472 mod->mkobj.kobj.kset = module_kset;
1473 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1474 "%s", mod->name);
1475 if (err)
1476 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001477
Kay Sievers97c146e2007-11-29 23:46:11 +01001478 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001479out:
1480 return err;
1481}
1482
Rusty Russell6407ebb22010-06-05 11:17:36 -06001483static int mod_sysfs_setup(struct module *mod,
Rusty Russell8f6d0372010-08-05 12:59:09 -06001484 const struct load_info *info,
Kay Sievers270a6c42007-01-18 13:26:15 +01001485 struct kernel_param *kparam,
1486 unsigned int num_params)
1487{
1488 int err;
1489
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001490 err = mod_sysfs_init(mod);
1491 if (err)
1492 goto out;
1493
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001494 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001495 if (!mod->holders_dir) {
1496 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001497 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001498 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 err = module_param_sysfs_setup(mod, kparam, num_params);
1501 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001502 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Matt Domschc988d2b2005-06-23 22:05:15 -07001504 err = module_add_modinfo_attrs(mod);
1505 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001506 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001507
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001508 add_usage_links(mod);
Rusty Russell8f6d0372010-08-05 12:59:09 -06001509 add_sect_attrs(mod, info);
1510 add_notes_attrs(mod, info);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001511
Kay Sieverse17e0f52006-11-24 12:15:25 +01001512 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return 0;
1514
Kay Sieverse17e0f52006-11-24 12:15:25 +01001515out_unreg_param:
1516 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001517out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001518 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001519out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001520 kobject_put(&mod->mkobj.kobj);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001521out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return err;
1523}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001524
1525static void mod_sysfs_fini(struct module *mod)
1526{
Rusty Russell8f6d0372010-08-05 12:59:09 -06001527 remove_notes_attrs(mod);
1528 remove_sect_attrs(mod);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001529 kobject_put(&mod->mkobj.kobj);
1530}
1531
Rusty Russell8f6d0372010-08-05 12:59:09 -06001532#else /* !CONFIG_SYSFS */
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001533
Rusty Russell8f6d0372010-08-05 12:59:09 -06001534static int mod_sysfs_setup(struct module *mod,
1535 const struct load_info *info,
Rusty Russell6407ebb22010-06-05 11:17:36 -06001536 struct kernel_param *kparam,
1537 unsigned int num_params)
1538{
1539 return 0;
1540}
1541
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001542static void mod_sysfs_fini(struct module *mod)
1543{
1544}
1545
Rusty Russell36b03602010-08-05 12:59:09 -06001546static void module_remove_modinfo_attrs(struct module *mod)
1547{
1548}
1549
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001550static void del_usage_links(struct module *mod)
1551{
1552}
1553
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001554#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Rusty Russell36b03602010-08-05 12:59:09 -06001556static void mod_sysfs_teardown(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001558 del_usage_links(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001559 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001561 kobject_put(mod->mkobj.drivers_dir);
1562 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001563 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
1566/*
1567 * unlink the module with the whole machine is stopped with interrupts off
1568 * - this defends against kallsyms not taking locks
1569 */
1570static int __unlink_module(void *_mod)
1571{
1572 struct module *mod = _mod;
1573 list_del(&mod->list);
Linus Torvalds53363772010-10-05 11:29:27 -07001574 module_bug_cleanup(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 return 0;
1576}
1577
matthieu castet84e1c6b2010-11-16 22:35:16 +01001578#ifdef CONFIG_DEBUG_SET_MODULE_RONX
1579/*
1580 * LKM RO/NX protection: protect module's text/ro-data
1581 * from modification and any data from execution.
1582 */
1583void set_page_attributes(void *start, void *end, int (*set)(unsigned long start, int num_pages))
1584{
1585 unsigned long begin_pfn = PFN_DOWN((unsigned long)start);
1586 unsigned long end_pfn = PFN_DOWN((unsigned long)end);
1587
1588 if (end_pfn > begin_pfn)
1589 set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1590}
1591
1592static void set_section_ro_nx(void *base,
1593 unsigned long text_size,
1594 unsigned long ro_size,
1595 unsigned long total_size)
1596{
1597 /* begin and end PFNs of the current subsection */
1598 unsigned long begin_pfn;
1599 unsigned long end_pfn;
1600
1601 /*
1602 * Set RO for module text and RO-data:
1603 * - Always protect first page.
1604 * - Do not protect last partial page.
1605 */
1606 if (ro_size > 0)
1607 set_page_attributes(base, base + ro_size, set_memory_ro);
1608
1609 /*
1610 * Set NX permissions for module data:
1611 * - Do not protect first partial page.
1612 * - Always protect last page.
1613 */
1614 if (total_size > text_size) {
1615 begin_pfn = PFN_UP((unsigned long)base + text_size);
1616 end_pfn = PFN_UP((unsigned long)base + total_size);
1617 if (end_pfn > begin_pfn)
1618 set_memory_nx(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1619 }
1620}
1621
Jan Glauber01526ed2011-05-19 16:55:26 -06001622static void unset_module_core_ro_nx(struct module *mod)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001623{
Jan Glauber01526ed2011-05-19 16:55:26 -06001624 set_page_attributes(mod->module_core + mod->core_text_size,
1625 mod->module_core + mod->core_size,
1626 set_memory_x);
1627 set_page_attributes(mod->module_core,
1628 mod->module_core + mod->core_ro_size,
1629 set_memory_rw);
1630}
1631
1632static void unset_module_init_ro_nx(struct module *mod)
1633{
1634 set_page_attributes(mod->module_init + mod->init_text_size,
1635 mod->module_init + mod->init_size,
1636 set_memory_x);
1637 set_page_attributes(mod->module_init,
1638 mod->module_init + mod->init_ro_size,
1639 set_memory_rw);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001640}
1641
1642/* Iterate through all modules and set each module's text as RW */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001643void set_all_modules_text_rw(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001644{
1645 struct module *mod;
1646
1647 mutex_lock(&module_mutex);
1648 list_for_each_entry_rcu(mod, &modules, list) {
1649 if ((mod->module_core) && (mod->core_text_size)) {
1650 set_page_attributes(mod->module_core,
1651 mod->module_core + mod->core_text_size,
1652 set_memory_rw);
1653 }
1654 if ((mod->module_init) && (mod->init_text_size)) {
1655 set_page_attributes(mod->module_init,
1656 mod->module_init + mod->init_text_size,
1657 set_memory_rw);
1658 }
1659 }
1660 mutex_unlock(&module_mutex);
1661}
1662
1663/* Iterate through all modules and set each module's text as RO */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001664void set_all_modules_text_ro(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001665{
1666 struct module *mod;
1667
1668 mutex_lock(&module_mutex);
1669 list_for_each_entry_rcu(mod, &modules, list) {
1670 if ((mod->module_core) && (mod->core_text_size)) {
1671 set_page_attributes(mod->module_core,
1672 mod->module_core + mod->core_text_size,
1673 set_memory_ro);
1674 }
1675 if ((mod->module_init) && (mod->init_text_size)) {
1676 set_page_attributes(mod->module_init,
1677 mod->module_init + mod->init_text_size,
1678 set_memory_ro);
1679 }
1680 }
1681 mutex_unlock(&module_mutex);
1682}
1683#else
1684static 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 -06001685static void unset_module_core_ro_nx(struct module *mod) { }
1686static void unset_module_init_ro_nx(struct module *mod) { }
matthieu castet84e1c6b2010-11-16 22:35:16 +01001687#endif
1688
Rusty Russell75676502010-06-05 11:17:36 -06001689/* Free a module, remove from lists, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690static void free_module(struct module *mod)
1691{
Li Zefan7ead8b82009-08-17 16:56:28 +08001692 trace_module_free(mod);
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 /* Delete from various lists */
Rusty Russell75676502010-06-05 11:17:36 -06001695 mutex_lock(&module_mutex);
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001696 stop_machine(__unlink_module, mod, NULL);
Rusty Russell75676502010-06-05 11:17:36 -06001697 mutex_unlock(&module_mutex);
Rusty Russell36b03602010-08-05 12:59:09 -06001698 mod_sysfs_teardown(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Jason Baronb82bab4b2010-07-27 13:18:01 -07001700 /* Remove dynamic debug info */
1701 ddebug_remove_module(mod->name);
1702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 /* Arch-specific cleanup. */
1704 module_arch_cleanup(mod);
1705
1706 /* Module unload stuff */
1707 module_unload_free(mod);
1708
Rusty Russelle180a6b2009-03-31 13:05:29 -06001709 /* Free any allocated parameters. */
1710 destroy_params(mod->kp, mod->num_kp);
1711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 /* This may be NULL, but that's OK */
Jan Glauber01526ed2011-05-19 16:55:26 -06001713 unset_module_init_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 module_free(mod, mod->module_init);
1715 kfree(mod->args);
Tejun Heo259354d2010-03-10 18:56:10 +09001716 percpu_modfree(mod);
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001717
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001718 /* Free lock-classes: */
1719 lockdep_free_key_range(mod->module_core, mod->core_size);
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 /* Finally, free the core (containing the module structure) */
Jan Glauber01526ed2011-05-19 16:55:26 -06001722 unset_module_core_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 module_free(mod, mod->module_core);
Bernd Schmidteb8cdec2009-09-21 17:03:57 -07001724
1725#ifdef CONFIG_MPU
1726 update_protections(current->mm);
1727#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
1729
1730void *__symbol_get(const char *symbol)
1731{
1732 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001733 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Rusty Russell24da1cb2007-07-15 23:41:46 -07001735 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001736 sym = find_symbol(symbol, &owner, NULL, true, true);
1737 if (sym && strong_try_module_get(owner))
1738 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001739 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Tim Abbott414fd312008-12-05 19:03:56 -05001741 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742}
1743EXPORT_SYMBOL_GPL(__symbol_get);
1744
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001745/*
1746 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001747 * in the kernel or in some other module's exported symbol table.
Rusty Russellbe593f42010-06-05 11:17:37 -06001748 *
1749 * You must hold the module_mutex.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001750 */
1751static int verify_export_symbols(struct module *mod)
1752{
Rusty Russellb2111042008-05-01 21:15:00 -05001753 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001754 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001755 const struct kernel_symbol *s;
1756 struct {
1757 const struct kernel_symbol *sym;
1758 unsigned int num;
1759 } arr[] = {
1760 { mod->syms, mod->num_syms },
1761 { mod->gpl_syms, mod->num_gpl_syms },
1762 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001763#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001764 { mod->unused_syms, mod->num_unused_syms },
1765 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001766#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001767 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001768
Rusty Russellb2111042008-05-01 21:15:00 -05001769 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1770 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Rusty Russellbe593f42010-06-05 11:17:37 -06001771 if (find_symbol(s->name, &owner, NULL, true, false)) {
Rusty Russellb2111042008-05-01 21:15:00 -05001772 printk(KERN_ERR
1773 "%s: exports duplicate symbol %s"
1774 " (owned by %s)\n",
1775 mod->name, s->name, module_name(owner));
1776 return -ENOEXEC;
1777 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001778 }
Rusty Russellb2111042008-05-01 21:15:00 -05001779 }
1780 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001781}
1782
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001783/* Change all symbols so that st_value encodes the pointer directly. */
Rusty Russell49668682010-08-05 12:59:10 -06001784static int simplify_symbols(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
Rusty Russell49668682010-08-05 12:59:10 -06001786 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
1787 Elf_Sym *sym = (void *)symsec->sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 unsigned long secbase;
Rusty Russell49668682010-08-05 12:59:10 -06001789 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05001791 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Rusty Russell49668682010-08-05 12:59:10 -06001793 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
1794 const char *name = info->strtab + sym[i].st_name;
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 switch (sym[i].st_shndx) {
1797 case SHN_COMMON:
1798 /* We compiled with -fno-common. These are not
1799 supposed to happen. */
Rusty Russell49668682010-08-05 12:59:10 -06001800 DEBUGP("Common symbol: %s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 printk("%s: please compile with -fno-common\n",
1802 mod->name);
1803 ret = -ENOEXEC;
1804 break;
1805
1806 case SHN_ABS:
1807 /* Don't need to do anything */
1808 DEBUGP("Absolute symbol: 0x%08lx\n",
1809 (long)sym[i].st_value);
1810 break;
1811
1812 case SHN_UNDEF:
Rusty Russell49668682010-08-05 12:59:10 -06001813 ksym = resolve_symbol_wait(mod, info, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 /* Ok if resolved. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001815 if (ksym && !IS_ERR(ksym)) {
Tim Abbott414fd312008-12-05 19:03:56 -05001816 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 break;
Tim Abbott414fd312008-12-05 19:03:56 -05001818 }
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 /* Ok if weak. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001821 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 break;
1823
Rusty Russell9bea7f22010-06-05 11:17:37 -06001824 printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
Rusty Russell49668682010-08-05 12:59:10 -06001825 mod->name, name, PTR_ERR(ksym));
Rusty Russell9bea7f22010-06-05 11:17:37 -06001826 ret = PTR_ERR(ksym) ?: -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 break;
1828
1829 default:
1830 /* Divert to percpu allocation if a percpu var. */
Rusty Russell49668682010-08-05 12:59:10 -06001831 if (sym[i].st_shndx == info->index.pcpu)
Tejun Heo259354d2010-03-10 18:56:10 +09001832 secbase = (unsigned long)mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 else
Rusty Russell49668682010-08-05 12:59:10 -06001834 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 sym[i].st_value += secbase;
1836 break;
1837 }
1838 }
1839
1840 return ret;
1841}
1842
Rusty Russell49668682010-08-05 12:59:10 -06001843static int apply_relocations(struct module *mod, const struct load_info *info)
Rusty Russell22e268e2010-08-05 12:59:05 -06001844{
1845 unsigned int i;
1846 int err = 0;
1847
1848 /* Now do relocations. */
Rusty Russell49668682010-08-05 12:59:10 -06001849 for (i = 1; i < info->hdr->e_shnum; i++) {
1850 unsigned int infosec = info->sechdrs[i].sh_info;
Rusty Russell22e268e2010-08-05 12:59:05 -06001851
1852 /* Not a valid relocation section? */
Rusty Russell49668682010-08-05 12:59:10 -06001853 if (infosec >= info->hdr->e_shnum)
Rusty Russell22e268e2010-08-05 12:59:05 -06001854 continue;
1855
1856 /* Don't bother with non-allocated sections */
Rusty Russell49668682010-08-05 12:59:10 -06001857 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
Rusty Russell22e268e2010-08-05 12:59:05 -06001858 continue;
1859
Rusty Russell49668682010-08-05 12:59:10 -06001860 if (info->sechdrs[i].sh_type == SHT_REL)
1861 err = apply_relocate(info->sechdrs, info->strtab,
1862 info->index.sym, i, mod);
1863 else if (info->sechdrs[i].sh_type == SHT_RELA)
1864 err = apply_relocate_add(info->sechdrs, info->strtab,
1865 info->index.sym, i, mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06001866 if (err < 0)
1867 break;
1868 }
1869 return err;
1870}
1871
Helge Deller088af9a2008-12-31 12:31:18 +01001872/* Additional bytes needed by arch in front of individual sections */
1873unsigned int __weak arch_mod_section_prepend(struct module *mod,
1874 unsigned int section)
1875{
1876 /* default implementation just returns zero */
1877 return 0;
1878}
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01001881static long get_offset(struct module *mod, unsigned int *size,
1882 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
1884 long ret;
1885
Helge Deller088af9a2008-12-31 12:31:18 +01001886 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1888 *size = ret + sechdr->sh_size;
1889 return ret;
1890}
1891
1892/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1893 might -- code, read-only data, read-write data, small data. Tally
1894 sizes, and place the offsets into sh_entsize fields: high bit means it
1895 belongs in init. */
Rusty Russell49668682010-08-05 12:59:10 -06001896static void layout_sections(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
1898 static unsigned long const masks[][2] = {
1899 /* NOTE: all executable code must be the first section
1900 * in this array; otherwise modify the text_size
1901 * finder in the two loops below */
1902 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1903 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1904 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1905 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1906 };
1907 unsigned int m, i;
1908
Rusty Russell49668682010-08-05 12:59:10 -06001909 for (i = 0; i < info->hdr->e_shnum; i++)
1910 info->sechdrs[i].sh_entsize = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
1912 DEBUGP("Core section allocation order:\n");
1913 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06001914 for (i = 0; i < info->hdr->e_shnum; ++i) {
1915 Elf_Shdr *s = &info->sechdrs[i];
1916 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1919 || (s->sh_flags & masks[m][1])
1920 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06001921 || strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001923 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Rusty Russell49668682010-08-05 12:59:10 -06001924 DEBUGP("\t%s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01001926 switch (m) {
1927 case 0: /* executable */
1928 mod->core_size = debug_align(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 mod->core_text_size = mod->core_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01001930 break;
1931 case 1: /* RO: text and ro-data */
1932 mod->core_size = debug_align(mod->core_size);
1933 mod->core_ro_size = mod->core_size;
1934 break;
1935 case 3: /* whole core */
1936 mod->core_size = debug_align(mod->core_size);
1937 break;
1938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 }
1940
1941 DEBUGP("Init section allocation order:\n");
1942 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06001943 for (i = 0; i < info->hdr->e_shnum; ++i) {
1944 Elf_Shdr *s = &info->sechdrs[i];
1945 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1948 || (s->sh_flags & masks[m][1])
1949 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06001950 || !strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001952 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 | INIT_OFFSET_MASK);
Rusty Russell49668682010-08-05 12:59:10 -06001954 DEBUGP("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01001956 switch (m) {
1957 case 0: /* executable */
1958 mod->init_size = debug_align(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 mod->init_text_size = mod->init_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01001960 break;
1961 case 1: /* RO: text and ro-data */
1962 mod->init_size = debug_align(mod->init_size);
1963 mod->init_ro_size = mod->init_size;
1964 break;
1965 case 3: /* whole init */
1966 mod->init_size = debug_align(mod->init_size);
1967 break;
1968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 }
1970}
1971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972static void set_license(struct module *mod, const char *license)
1973{
1974 if (!license)
1975 license = "unspecified";
1976
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001977 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07001978 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001979 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001980 "kernel.\n", mod->name, license);
1981 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 }
1983}
1984
1985/* Parse tag=value strings from .modinfo section */
1986static char *next_string(char *string, unsigned long *secsize)
1987{
1988 /* Skip non-zero chars */
1989 while (string[0]) {
1990 string++;
1991 if ((*secsize)-- <= 1)
1992 return NULL;
1993 }
1994
1995 /* Skip any zero padding. */
1996 while (!string[0]) {
1997 string++;
1998 if ((*secsize)-- <= 1)
1999 return NULL;
2000 }
2001 return string;
2002}
2003
Rusty Russell49668682010-08-05 12:59:10 -06002004static char *get_modinfo(struct load_info *info, const char *tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
2006 char *p;
2007 unsigned int taglen = strlen(tag);
Rusty Russell49668682010-08-05 12:59:10 -06002008 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2009 unsigned long size = infosec->sh_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Rusty Russell49668682010-08-05 12:59:10 -06002011 for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2013 return p + taglen + 1;
2014 }
2015 return NULL;
2016}
2017
Rusty Russell49668682010-08-05 12:59:10 -06002018static void setup_modinfo(struct module *mod, struct load_info *info)
Matt Domschc988d2b2005-06-23 22:05:15 -07002019{
2020 struct module_attribute *attr;
2021 int i;
2022
2023 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2024 if (attr->setup)
Rusty Russell49668682010-08-05 12:59:10 -06002025 attr->setup(mod, get_modinfo(info, attr->attr.name));
Matt Domschc988d2b2005-06-23 22:05:15 -07002026 }
2027}
Matt Domschc988d2b2005-06-23 22:05:15 -07002028
Rusty Russella263f772009-09-25 00:32:58 -06002029static void free_modinfo(struct module *mod)
2030{
2031 struct module_attribute *attr;
2032 int i;
2033
2034 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2035 if (attr->free)
2036 attr->free(mod);
2037 }
2038}
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01002041
2042/* lookup symbol in given range of kernel_symbols */
2043static const struct kernel_symbol *lookup_symbol(const char *name,
2044 const struct kernel_symbol *start,
2045 const struct kernel_symbol *stop)
2046{
2047 const struct kernel_symbol *ks = start;
2048 for (; ks < stop; ks++)
2049 if (strcmp(ks->name, name) == 0)
2050 return ks;
2051 return NULL;
2052}
2053
Tim Abbottca4787b2009-01-05 08:40:10 -06002054static int is_exported(const char *name, unsigned long value,
2055 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
Tim Abbottca4787b2009-01-05 08:40:10 -06002057 const struct kernel_symbol *ks;
2058 if (!mod)
2059 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01002060 else
Tim Abbottca4787b2009-01-05 08:40:10 -06002061 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
2062 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063}
2064
2065/* As per nm */
Rusty Russelleded41c2010-08-05 12:59:07 -06002066static char elf_type(const Elf_Sym *sym, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
Rusty Russelleded41c2010-08-05 12:59:07 -06002068 const Elf_Shdr *sechdrs = info->sechdrs;
2069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2071 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2072 return 'v';
2073 else
2074 return 'w';
2075 }
2076 if (sym->st_shndx == SHN_UNDEF)
2077 return 'U';
2078 if (sym->st_shndx == SHN_ABS)
2079 return 'a';
2080 if (sym->st_shndx >= SHN_LORESERVE)
2081 return '?';
2082 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2083 return 't';
2084 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2085 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2086 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2087 return 'r';
2088 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2089 return 'g';
2090 else
2091 return 'd';
2092 }
2093 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2094 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2095 return 's';
2096 else
2097 return 'b';
2098 }
Rusty Russelleded41c2010-08-05 12:59:07 -06002099 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2100 ".debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 return 'n';
Rusty Russelleded41c2010-08-05 12:59:07 -06002102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 return '?';
2104}
2105
Jan Beulich4a496222009-07-06 14:50:42 +01002106static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
2107 unsigned int shnum)
2108{
2109 const Elf_Shdr *sec;
2110
2111 if (src->st_shndx == SHN_UNDEF
2112 || src->st_shndx >= shnum
2113 || !src->st_name)
2114 return false;
2115
2116 sec = sechdrs + src->st_shndx;
2117 if (!(sec->sh_flags & SHF_ALLOC)
2118#ifndef CONFIG_KALLSYMS_ALL
2119 || !(sec->sh_flags & SHF_EXECINSTR)
2120#endif
2121 || (sec->sh_entsize & INIT_OFFSET_MASK))
2122 return false;
2123
2124 return true;
2125}
2126
Rusty Russell49668682010-08-05 12:59:10 -06002127static void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002128{
Rusty Russell49668682010-08-05 12:59:10 -06002129 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2130 Elf_Shdr *strsect = info->sechdrs + info->index.str;
Jan Beulich4a496222009-07-06 14:50:42 +01002131 const Elf_Sym *src;
2132 unsigned int i, nsrc, ndst;
2133
2134 /* Put symbol section at end of init part of module. */
2135 symsect->sh_flags |= SHF_ALLOC;
2136 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
Rusty Russell49668682010-08-05 12:59:10 -06002137 info->index.sym) | INIT_OFFSET_MASK;
2138 DEBUGP("\t%s\n", info->secstrings + symsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002139
Rusty Russell49668682010-08-05 12:59:10 -06002140 src = (void *)info->hdr + symsect->sh_offset;
Jan Beulich4a496222009-07-06 14:50:42 +01002141 nsrc = symsect->sh_size / sizeof(*src);
2142 for (ndst = i = 1; i < nsrc; ++i, ++src)
Rusty Russell49668682010-08-05 12:59:10 -06002143 if (is_core_symbol(src, info->sechdrs, info->hdr->e_shnum)) {
Jan Beulich554bdfe2009-07-06 14:51:44 +01002144 unsigned int j = src->st_name;
2145
Rusty Russell49668682010-08-05 12:59:10 -06002146 while (!__test_and_set_bit(j, info->strmap)
2147 && info->strtab[j])
Jan Beulich554bdfe2009-07-06 14:51:44 +01002148 ++j;
Jan Beulich4a496222009-07-06 14:50:42 +01002149 ++ndst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002150 }
Jan Beulich4a496222009-07-06 14:50:42 +01002151
2152 /* Append room for core symbols at end of core part. */
Rusty Russell49668682010-08-05 12:59:10 -06002153 info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
2154 mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
Jan Beulich4a496222009-07-06 14:50:42 +01002155
Jan Beulich554bdfe2009-07-06 14:51:44 +01002156 /* Put string table section at end of init part of module. */
2157 strsect->sh_flags |= SHF_ALLOC;
2158 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
Rusty Russell49668682010-08-05 12:59:10 -06002159 info->index.str) | INIT_OFFSET_MASK;
2160 DEBUGP("\t%s\n", info->secstrings + strsect->sh_name);
Jan Beulich554bdfe2009-07-06 14:51:44 +01002161
2162 /* Append room for core symbols' strings at end of core part. */
Rusty Russell49668682010-08-05 12:59:10 -06002163 info->stroffs = mod->core_size;
2164 __set_bit(0, info->strmap);
2165 mod->core_size += bitmap_weight(info->strmap, strsect->sh_size);
Jan Beulich4a496222009-07-06 14:50:42 +01002166}
2167
Rusty Russell811d66a2010-08-05 12:59:12 -06002168static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
Jan Beulich4a496222009-07-06 14:50:42 +01002170 unsigned int i, ndst;
2171 const Elf_Sym *src;
2172 Elf_Sym *dst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002173 char *s;
Rusty Russelleded41c2010-08-05 12:59:07 -06002174 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Rusty Russelleded41c2010-08-05 12:59:07 -06002176 mod->symtab = (void *)symsec->sh_addr;
2177 mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
Rusty Russell511ca6a2010-08-05 12:59:08 -06002178 /* Make sure we get permanent strtab: don't use info->strtab. */
2179 mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181 /* Set types up while we still have access to sections. */
2182 for (i = 0; i < mod->num_symtab; i++)
Rusty Russelleded41c2010-08-05 12:59:07 -06002183 mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
Jan Beulich4a496222009-07-06 14:50:42 +01002184
Rusty Russelld9131882010-08-05 12:59:08 -06002185 mod->core_symtab = dst = mod->module_core + info->symoffs;
Jan Beulich4a496222009-07-06 14:50:42 +01002186 src = mod->symtab;
2187 *dst = *src;
2188 for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
Rusty Russelleded41c2010-08-05 12:59:07 -06002189 if (!is_core_symbol(src, info->sechdrs, info->hdr->e_shnum))
Jan Beulich4a496222009-07-06 14:50:42 +01002190 continue;
2191 dst[ndst] = *src;
Rusty Russelld9131882010-08-05 12:59:08 -06002192 dst[ndst].st_name = bitmap_weight(info->strmap,
2193 dst[ndst].st_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002194 ++ndst;
2195 }
2196 mod->core_num_syms = ndst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002197
Rusty Russelld9131882010-08-05 12:59:08 -06002198 mod->core_strtab = s = mod->module_core + info->stroffs;
Rusty Russelleded41c2010-08-05 12:59:07 -06002199 for (*s = 0, i = 1; i < info->sechdrs[info->index.str].sh_size; ++i)
Rusty Russelld9131882010-08-05 12:59:08 -06002200 if (test_bit(i, info->strmap))
Jan Beulich554bdfe2009-07-06 14:51:44 +01002201 *++s = mod->strtab[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202}
2203#else
Rusty Russell49668682010-08-05 12:59:10 -06002204static inline void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002205{
2206}
Paul Mundt3ae91c22009-10-01 15:43:54 -07002207
Michał Mirosławabbce902010-09-20 01:58:08 +02002208static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
2210}
2211#endif /* CONFIG_KALLSYMS */
2212
Jason Barone9d376f2009-02-05 11:51:38 -05002213static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05002214{
Rusty Russell811d66a2010-08-05 12:59:12 -06002215 if (!debug)
2216 return;
Jason Barone9d376f2009-02-05 11:51:38 -05002217#ifdef CONFIG_DYNAMIC_DEBUG
2218 if (ddebug_add_module(debug, num, debug->modname))
2219 printk(KERN_ERR "dynamic debug error adding module: %s\n",
2220 debug->modname);
2221#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002222}
Jason Baron346e15b2008-08-12 16:46:19 -04002223
Yehuda Sadehff49d742010-07-03 13:07:35 +10002224static void dynamic_debug_remove(struct _ddebug *debug)
2225{
2226 if (debug)
2227 ddebug_remove_module(debug->modname);
2228}
2229
Rusty Russell3a642e92008-07-22 19:24:28 -05002230static void *module_alloc_update_bounds(unsigned long size)
2231{
2232 void *ret = module_alloc(size);
2233
2234 if (ret) {
Rusty Russell75676502010-06-05 11:17:36 -06002235 mutex_lock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002236 /* Update module bounds. */
2237 if ((unsigned long)ret < module_addr_min)
2238 module_addr_min = (unsigned long)ret;
2239 if ((unsigned long)ret + size > module_addr_max)
2240 module_addr_max = (unsigned long)ret + size;
Rusty Russell75676502010-06-05 11:17:36 -06002241 mutex_unlock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002242 }
2243 return ret;
2244}
2245
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002246#ifdef CONFIG_DEBUG_KMEMLEAK
Rusty Russell49668682010-08-05 12:59:10 -06002247static void kmemleak_load_module(const struct module *mod,
2248 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002249{
2250 unsigned int i;
2251
2252 /* only scan the sections containing data */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002253 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002254
Rusty Russell49668682010-08-05 12:59:10 -06002255 for (i = 1; i < info->hdr->e_shnum; i++) {
2256 const char *name = info->secstrings + info->sechdrs[i].sh_name;
2257 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002258 continue;
Rusty Russell49668682010-08-05 12:59:10 -06002259 if (!strstarts(name, ".data") && !strstarts(name, ".bss"))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002260 continue;
2261
Rusty Russell49668682010-08-05 12:59:10 -06002262 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2263 info->sechdrs[i].sh_size, GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002264 }
2265}
2266#else
Rusty Russell49668682010-08-05 12:59:10 -06002267static inline void kmemleak_load_module(const struct module *mod,
2268 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002269{
2270}
2271#endif
2272
Rusty Russell6526c532010-08-05 12:59:10 -06002273/* Sets info->hdr and info->len. */
Rusty Russelld9131882010-08-05 12:59:08 -06002274static int copy_and_check(struct load_info *info,
2275 const void __user *umod, unsigned long len,
2276 const char __user *uargs)
Rusty Russell40dd2562010-08-05 12:59:03 -06002277{
2278 int err;
2279 Elf_Ehdr *hdr;
2280
2281 if (len < sizeof(*hdr))
2282 return -ENOEXEC;
2283
2284 /* Suck in entire file: we'll want most of it. */
2285 /* vmalloc barfs on "unusual" numbers. Check here */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002286 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
Rusty Russell40dd2562010-08-05 12:59:03 -06002287 return -ENOMEM;
2288
2289 if (copy_from_user(hdr, umod, len) != 0) {
2290 err = -EFAULT;
2291 goto free_hdr;
2292 }
2293
2294 /* Sanity checks against insmoding binaries or wrong arch,
2295 weird elf version */
2296 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
2297 || hdr->e_type != ET_REL
2298 || !elf_check_arch(hdr)
2299 || hdr->e_shentsize != sizeof(Elf_Shdr)) {
2300 err = -ENOEXEC;
2301 goto free_hdr;
2302 }
2303
2304 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) {
2305 err = -ENOEXEC;
2306 goto free_hdr;
2307 }
Rusty Russelld9131882010-08-05 12:59:08 -06002308
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002309 info->hdr = hdr;
2310 info->len = len;
Rusty Russell40dd2562010-08-05 12:59:03 -06002311 return 0;
2312
2313free_hdr:
2314 vfree(hdr);
2315 return err;
2316}
2317
Rusty Russelld9131882010-08-05 12:59:08 -06002318static void free_copy(struct load_info *info)
2319{
Rusty Russelld9131882010-08-05 12:59:08 -06002320 vfree(info->hdr);
2321}
2322
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002323static int rewrite_section_headers(struct load_info *info)
2324{
2325 unsigned int i;
2326
2327 /* This should always be true, but let's be sure. */
2328 info->sechdrs[0].sh_addr = 0;
2329
2330 for (i = 1; i < info->hdr->e_shnum; i++) {
2331 Elf_Shdr *shdr = &info->sechdrs[i];
2332 if (shdr->sh_type != SHT_NOBITS
2333 && info->len < shdr->sh_offset + shdr->sh_size) {
2334 printk(KERN_ERR "Module len %lu truncated\n",
2335 info->len);
2336 return -ENOEXEC;
2337 }
2338
2339 /* Mark all sections sh_addr with their address in the
2340 temporary image. */
2341 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2342
2343#ifndef CONFIG_MODULE_UNLOAD
2344 /* Don't load .exit sections */
2345 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2346 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2347#endif
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002348 }
Rusty Russelld6df72a2010-08-05 12:59:07 -06002349
2350 /* Track but don't keep modinfo and version sections. */
Rusty Russell49668682010-08-05 12:59:10 -06002351 info->index.vers = find_sec(info, "__versions");
2352 info->index.info = find_sec(info, ".modinfo");
Rusty Russelld6df72a2010-08-05 12:59:07 -06002353 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2354 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002355 return 0;
2356}
2357
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002358/*
2359 * Set up our basic convenience variables (pointers to section headers,
2360 * search for module section index etc), and do some basic section
2361 * verification.
2362 *
2363 * Return the temporary module pointer (we'll replace it with the final
2364 * one when we move the module sections around).
2365 */
2366static struct module *setup_load_info(struct load_info *info)
2367{
2368 unsigned int i;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002369 int err;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002370 struct module *mod;
2371
2372 /* Set up the convenience variables */
2373 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002374 info->secstrings = (void *)info->hdr
2375 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002376
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002377 err = rewrite_section_headers(info);
2378 if (err)
2379 return ERR_PTR(err);
2380
2381 /* Find internal symbols and strings. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002382 for (i = 1; i < info->hdr->e_shnum; i++) {
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002383 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2384 info->index.sym = i;
2385 info->index.str = info->sechdrs[i].sh_link;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002386 info->strtab = (char *)info->hdr
2387 + info->sechdrs[info->index.str].sh_offset;
2388 break;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002389 }
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002390 }
2391
Rusty Russell49668682010-08-05 12:59:10 -06002392 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002393 if (!info->index.mod) {
2394 printk(KERN_WARNING "No module found in object\n");
2395 return ERR_PTR(-ENOEXEC);
2396 }
2397 /* This is temporary: point mod into copy of data. */
2398 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2399
2400 if (info->index.sym == 0) {
2401 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2402 mod->name);
2403 return ERR_PTR(-ENOEXEC);
2404 }
2405
Rusty Russell49668682010-08-05 12:59:10 -06002406 info->index.pcpu = find_pcpusec(info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002407
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002408 /* Check module struct version now, before we try to use module. */
2409 if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2410 return ERR_PTR(-ENOEXEC);
2411
2412 return mod;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002413}
2414
Rusty Russell49668682010-08-05 12:59:10 -06002415static int check_modinfo(struct module *mod, struct load_info *info)
Rusty Russell40dd2562010-08-05 12:59:03 -06002416{
Rusty Russell49668682010-08-05 12:59:10 -06002417 const char *modmagic = get_modinfo(info, "vermagic");
Rusty Russell40dd2562010-08-05 12:59:03 -06002418 int err;
2419
2420 /* This is allowed: modprobe --force will invalidate it. */
2421 if (!modmagic) {
2422 err = try_to_force_load(mod, "bad vermagic");
2423 if (err)
2424 return err;
Rusty Russell49668682010-08-05 12:59:10 -06002425 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002426 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2427 mod->name, modmagic, vermagic);
2428 return -ENOEXEC;
2429 }
2430
Rusty Russell49668682010-08-05 12:59:10 -06002431 if (get_modinfo(info, "staging")) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002432 add_taint_module(mod, TAINT_CRAP);
2433 printk(KERN_WARNING "%s: module is from the staging directory,"
2434 " the quality is unknown, you have been warned.\n",
2435 mod->name);
2436 }
Rusty Russell22e268e2010-08-05 12:59:05 -06002437
2438 /* Set up license info based on the info section */
Rusty Russell49668682010-08-05 12:59:10 -06002439 set_license(mod, get_modinfo(info, "license"));
Rusty Russell22e268e2010-08-05 12:59:05 -06002440
Rusty Russell40dd2562010-08-05 12:59:03 -06002441 return 0;
2442}
2443
Rusty Russell811d66a2010-08-05 12:59:12 -06002444static void find_module_sections(struct module *mod, struct load_info *info)
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002445{
Rusty Russell49668682010-08-05 12:59:10 -06002446 mod->kp = section_objs(info, "__param",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002447 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell49668682010-08-05 12:59:10 -06002448 mod->syms = section_objs(info, "__ksymtab",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002449 sizeof(*mod->syms), &mod->num_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002450 mod->crcs = section_addr(info, "__kcrctab");
2451 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002452 sizeof(*mod->gpl_syms),
2453 &mod->num_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002454 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
2455 mod->gpl_future_syms = section_objs(info,
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002456 "__ksymtab_gpl_future",
2457 sizeof(*mod->gpl_future_syms),
2458 &mod->num_gpl_future_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002459 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002460
2461#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell49668682010-08-05 12:59:10 -06002462 mod->unused_syms = section_objs(info, "__ksymtab_unused",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002463 sizeof(*mod->unused_syms),
2464 &mod->num_unused_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002465 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
2466 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002467 sizeof(*mod->unused_gpl_syms),
2468 &mod->num_unused_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002469 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002470#endif
2471#ifdef CONFIG_CONSTRUCTORS
Rusty Russell49668682010-08-05 12:59:10 -06002472 mod->ctors = section_objs(info, ".ctors",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002473 sizeof(*mod->ctors), &mod->num_ctors);
2474#endif
2475
2476#ifdef CONFIG_TRACEPOINTS
Mathieu Desnoyers65498642011-01-26 17:26:22 -05002477 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
2478 sizeof(*mod->tracepoints_ptrs),
2479 &mod->num_tracepoints);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002480#endif
Jason Baronbf5438fc2010-09-17 11:09:00 -04002481#ifdef HAVE_JUMP_LABEL
2482 mod->jump_entries = section_objs(info, "__jump_table",
2483 sizeof(*mod->jump_entries),
2484 &mod->num_jump_entries);
2485#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002486#ifdef CONFIG_EVENT_TRACING
Rusty Russell49668682010-08-05 12:59:10 -06002487 mod->trace_events = section_objs(info, "_ftrace_events",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002488 sizeof(*mod->trace_events),
2489 &mod->num_trace_events);
2490 /*
2491 * This section contains pointers to allocated objects in the trace
2492 * code and not scanning it leads to false positives.
2493 */
2494 kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2495 mod->num_trace_events, GFP_KERNEL);
2496#endif
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05002497#ifdef CONFIG_TRACING
2498 mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
2499 sizeof(*mod->trace_bprintk_fmt_start),
2500 &mod->num_trace_bprintk_fmt);
2501 /*
2502 * This section contains pointers to allocated objects in the trace
2503 * code and not scanning it leads to false positives.
2504 */
2505 kmemleak_scan_area(mod->trace_bprintk_fmt_start,
2506 sizeof(*mod->trace_bprintk_fmt_start) *
2507 mod->num_trace_bprintk_fmt, GFP_KERNEL);
2508#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002509#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2510 /* sechdrs[0].sh_size is always zero */
Rusty Russell49668682010-08-05 12:59:10 -06002511 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002512 sizeof(*mod->ftrace_callsites),
2513 &mod->num_ftrace_callsites);
2514#endif
Rusty Russell22e268e2010-08-05 12:59:05 -06002515
Rusty Russell811d66a2010-08-05 12:59:12 -06002516 mod->extable = section_objs(info, "__ex_table",
2517 sizeof(*mod->extable), &mod->num_exentries);
2518
Rusty Russell49668682010-08-05 12:59:10 -06002519 if (section_addr(info, "__obsparm"))
Rusty Russell22e268e2010-08-05 12:59:05 -06002520 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2521 mod->name);
Rusty Russell811d66a2010-08-05 12:59:12 -06002522
2523 info->debug = section_objs(info, "__verbose",
2524 sizeof(*info->debug), &info->num_debug);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002525}
2526
Rusty Russell49668682010-08-05 12:59:10 -06002527static int move_module(struct module *mod, struct load_info *info)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002528{
2529 int i;
2530 void *ptr;
2531
2532 /* Do the allocs. */
2533 ptr = module_alloc_update_bounds(mod->core_size);
2534 /*
2535 * The pointer to this block is stored in the module structure
2536 * which is inside the block. Just mark it as not being a
2537 * leak.
2538 */
2539 kmemleak_not_leak(ptr);
2540 if (!ptr)
Rusty Russelld9131882010-08-05 12:59:08 -06002541 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002542
2543 memset(ptr, 0, mod->core_size);
2544 mod->module_core = ptr;
2545
2546 ptr = module_alloc_update_bounds(mod->init_size);
2547 /*
2548 * The pointer to this block is stored in the module structure
2549 * which is inside the block. This block doesn't need to be
2550 * scanned as it contains data and code that will be freed
2551 * after the module is initialized.
2552 */
2553 kmemleak_ignore(ptr);
2554 if (!ptr && mod->init_size) {
2555 module_free(mod, mod->module_core);
Rusty Russelld9131882010-08-05 12:59:08 -06002556 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002557 }
2558 memset(ptr, 0, mod->init_size);
2559 mod->module_init = ptr;
2560
2561 /* Transfer each section which specifies SHF_ALLOC */
2562 DEBUGP("final section addresses:\n");
Rusty Russell49668682010-08-05 12:59:10 -06002563 for (i = 0; i < info->hdr->e_shnum; i++) {
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002564 void *dest;
Rusty Russell49668682010-08-05 12:59:10 -06002565 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002566
Rusty Russell49668682010-08-05 12:59:10 -06002567 if (!(shdr->sh_flags & SHF_ALLOC))
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002568 continue;
2569
Rusty Russell49668682010-08-05 12:59:10 -06002570 if (shdr->sh_entsize & INIT_OFFSET_MASK)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002571 dest = mod->module_init
Rusty Russell49668682010-08-05 12:59:10 -06002572 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002573 else
Rusty Russell49668682010-08-05 12:59:10 -06002574 dest = mod->module_core + shdr->sh_entsize;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002575
Rusty Russell49668682010-08-05 12:59:10 -06002576 if (shdr->sh_type != SHT_NOBITS)
2577 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002578 /* Update sh_addr to point to copy in image. */
Rusty Russell49668682010-08-05 12:59:10 -06002579 shdr->sh_addr = (unsigned long)dest;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002580 DEBUGP("\t0x%lx %s\n",
Rusty Russell49668682010-08-05 12:59:10 -06002581 shdr->sh_addr, info->secstrings + shdr->sh_name);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002582 }
Rusty Russelld9131882010-08-05 12:59:08 -06002583
2584 return 0;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002585}
2586
Rusty Russell49668682010-08-05 12:59:10 -06002587static int check_module_license_and_versions(struct module *mod)
Rusty Russell22e268e2010-08-05 12:59:05 -06002588{
2589 /*
2590 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2591 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2592 * using GPL-only symbols it needs.
2593 */
2594 if (strcmp(mod->name, "ndiswrapper") == 0)
2595 add_taint(TAINT_PROPRIETARY_MODULE);
2596
2597 /* driverloader was caught wrongly pretending to be under GPL */
2598 if (strcmp(mod->name, "driverloader") == 0)
2599 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2600
2601#ifdef CONFIG_MODVERSIONS
2602 if ((mod->num_syms && !mod->crcs)
2603 || (mod->num_gpl_syms && !mod->gpl_crcs)
2604 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2605#ifdef CONFIG_UNUSED_SYMBOLS
2606 || (mod->num_unused_syms && !mod->unused_crcs)
2607 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2608#endif
2609 ) {
2610 return try_to_force_load(mod,
2611 "no versions for exported symbols");
2612 }
2613#endif
2614 return 0;
2615}
2616
2617static void flush_module_icache(const struct module *mod)
2618{
2619 mm_segment_t old_fs;
2620
2621 /* flush the icache in correct context */
2622 old_fs = get_fs();
2623 set_fs(KERNEL_DS);
2624
2625 /*
2626 * Flush the instruction cache, since we've played with text.
2627 * Do it before processing of module parameters, so the module
2628 * can provide parameter accessor functions of its own.
2629 */
2630 if (mod->module_init)
2631 flush_icache_range((unsigned long)mod->module_init,
2632 (unsigned long)mod->module_init
2633 + mod->init_size);
2634 flush_icache_range((unsigned long)mod->module_core,
2635 (unsigned long)mod->module_core + mod->core_size);
2636
2637 set_fs(old_fs);
2638}
2639
Rusty Russelld9131882010-08-05 12:59:08 -06002640static struct module *layout_and_allocate(struct load_info *info)
2641{
2642 /* Module within temporary copy. */
2643 struct module *mod;
Rusty Russell49668682010-08-05 12:59:10 -06002644 Elf_Shdr *pcpusec;
Rusty Russelld9131882010-08-05 12:59:08 -06002645 int err;
2646
2647 mod = setup_load_info(info);
2648 if (IS_ERR(mod))
2649 return mod;
2650
Rusty Russell49668682010-08-05 12:59:10 -06002651 err = check_modinfo(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002652 if (err)
2653 return ERR_PTR(err);
2654
2655 /* Allow arches to frob section contents and sizes. */
Rusty Russell49668682010-08-05 12:59:10 -06002656 err = module_frob_arch_sections(info->hdr, info->sechdrs,
2657 info->secstrings, mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002658 if (err < 0)
Rusty Russell6526c532010-08-05 12:59:10 -06002659 goto out;
Rusty Russelld9131882010-08-05 12:59:08 -06002660
Rusty Russell49668682010-08-05 12:59:10 -06002661 pcpusec = &info->sechdrs[info->index.pcpu];
2662 if (pcpusec->sh_size) {
Rusty Russelld9131882010-08-05 12:59:08 -06002663 /* We have a special allocation for this section. */
Rusty Russell49668682010-08-05 12:59:10 -06002664 err = percpu_modalloc(mod,
2665 pcpusec->sh_size, pcpusec->sh_addralign);
Rusty Russelld9131882010-08-05 12:59:08 -06002666 if (err)
Rusty Russell6526c532010-08-05 12:59:10 -06002667 goto out;
Rusty Russell49668682010-08-05 12:59:10 -06002668 pcpusec->sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russelld9131882010-08-05 12:59:08 -06002669 }
2670
2671 /* Determine total sizes, and put offsets in sh_entsize. For now
2672 this is done generically; there doesn't appear to be any
2673 special cases for the architectures. */
Rusty Russell49668682010-08-05 12:59:10 -06002674 layout_sections(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002675
2676 info->strmap = kzalloc(BITS_TO_LONGS(info->sechdrs[info->index.str].sh_size)
2677 * sizeof(long), GFP_KERNEL);
2678 if (!info->strmap) {
2679 err = -ENOMEM;
2680 goto free_percpu;
2681 }
Rusty Russell49668682010-08-05 12:59:10 -06002682 layout_symtab(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002683
2684 /* Allocate and move to the final place */
Rusty Russell49668682010-08-05 12:59:10 -06002685 err = move_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002686 if (err)
2687 goto free_strmap;
2688
2689 /* Module has been copied to its final place now: return it. */
2690 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
Rusty Russell49668682010-08-05 12:59:10 -06002691 kmemleak_load_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002692 return mod;
2693
2694free_strmap:
2695 kfree(info->strmap);
2696free_percpu:
2697 percpu_modfree(mod);
Rusty Russell6526c532010-08-05 12:59:10 -06002698out:
Rusty Russelld9131882010-08-05 12:59:08 -06002699 return ERR_PTR(err);
2700}
2701
2702/* mod is no longer valid after this! */
2703static void module_deallocate(struct module *mod, struct load_info *info)
2704{
2705 kfree(info->strmap);
2706 percpu_modfree(mod);
2707 module_free(mod, mod->module_init);
2708 module_free(mod, mod->module_core);
2709}
2710
Rusty Russell811d66a2010-08-05 12:59:12 -06002711static int post_relocation(struct module *mod, const struct load_info *info)
2712{
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002713 /* Sort exception table now relocations are done. */
Rusty Russell811d66a2010-08-05 12:59:12 -06002714 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2715
2716 /* Copy relocated percpu area over. */
2717 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
2718 info->sechdrs[info->index.pcpu].sh_size);
2719
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002720 /* Setup kallsyms-specific fields. */
Rusty Russell811d66a2010-08-05 12:59:12 -06002721 add_kallsyms(mod, info);
2722
2723 /* Arch-specific module finalizing. */
2724 return module_finalize(info->hdr, info->sechdrs, mod);
2725}
2726
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727/* Allocate and load the module: note that size of section 0 is always
2728 zero, and we rely on this for optional sections. */
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002729static struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 unsigned long len,
2731 const char __user *uargs)
2732{
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002733 struct load_info info = { NULL, };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 struct module *mod;
Rusty Russell40dd2562010-08-05 12:59:03 -06002735 long err;
Paul Mundt3ae91c22009-10-01 15:43:54 -07002736
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
2738 umod, len, uargs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
Rusty Russelld9131882010-08-05 12:59:08 -06002740 /* Copy in the blobs from userspace, check they are vaguely sane. */
2741 err = copy_and_check(&info, umod, len, uargs);
Rusty Russell40dd2562010-08-05 12:59:03 -06002742 if (err)
2743 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
Rusty Russelld9131882010-08-05 12:59:08 -06002745 /* Figure out module layout, and allocate all the memory. */
2746 mod = layout_and_allocate(&info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002747 if (IS_ERR(mod)) {
2748 err = PTR_ERR(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002749 goto free_copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
Rusty Russell49668682010-08-05 12:59:10 -06002752 /* Now module is in final location, initialize linked lists, etc. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -06002753 err = module_unload_init(mod);
2754 if (err)
Rusty Russelld9131882010-08-05 12:59:08 -06002755 goto free_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
Rusty Russell22e268e2010-08-05 12:59:05 -06002757 /* Now we've got everything in the final locations, we can
2758 * find optional sections. */
Rusty Russell49668682010-08-05 12:59:10 -06002759 find_module_sections(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
Rusty Russell49668682010-08-05 12:59:10 -06002761 err = check_module_license_and_versions(mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002762 if (err)
2763 goto free_unload;
Dave Jones9841d61d2006-01-08 01:03:41 -08002764
Matt Domschc988d2b2005-06-23 22:05:15 -07002765 /* Set up MODINFO_ATTR fields */
Rusty Russell49668682010-08-05 12:59:10 -06002766 setup_modinfo(mod, &info);
Matt Domschc988d2b2005-06-23 22:05:15 -07002767
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 /* Fix up syms, so that st_value is a pointer to location. */
Rusty Russell49668682010-08-05 12:59:10 -06002769 err = simplify_symbols(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06002771 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
Rusty Russell49668682010-08-05 12:59:10 -06002773 err = apply_relocations(mod, &info);
Rusty Russell22e268e2010-08-05 12:59:05 -06002774 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06002775 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
Rusty Russell811d66a2010-08-05 12:59:12 -06002777 err = post_relocation(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06002779 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
Rusty Russell22e268e2010-08-05 12:59:05 -06002781 flush_module_icache(mod);
Thomas Koeller378bac82005-09-06 15:17:11 -07002782
Rusty Russell6526c532010-08-05 12:59:10 -06002783 /* Now copy in args */
2784 mod->args = strndup_user(uargs, ~0UL >> 1);
2785 if (IS_ERR(mod->args)) {
2786 err = PTR_ERR(mod->args);
2787 goto free_arch_cleanup;
2788 }
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002789
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002790 /* Mark state as coming so strong_try_module_get() ignores us. */
Rusty Russelld9131882010-08-05 12:59:08 -06002791 mod->state = MODULE_STATE_COMING;
2792
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002793 /* Now sew it into the lists so we can get lockdep and oops
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002794 * info during argument parsing. No one should access us, since
Andi Kleend72b3752008-08-30 10:09:00 +02002795 * strong_try_module_get() will fail.
2796 * lockdep/oops can run asynchronous, so use the RCU list insertion
2797 * function to insert in a way safe to concurrent readers.
2798 * The mutex protects against concurrent writers.
2799 */
Rusty Russell75676502010-06-05 11:17:36 -06002800 mutex_lock(&module_mutex);
Linus Torvalds3bafeb62010-06-05 11:17:36 -06002801 if (find_module(mod->name)) {
2802 err = -EEXIST;
Rusty Russellbe593f42010-06-05 11:17:37 -06002803 goto unlock;
Linus Torvalds3bafeb62010-06-05 11:17:36 -06002804 }
2805
Rusty Russell811d66a2010-08-05 12:59:12 -06002806 /* This has to be done once we're sure module name is unique. */
2807 if (!mod->taints)
2808 dynamic_debug_setup(info.debug, info.num_debug);
Yehuda Sadehff49d742010-07-03 13:07:35 +10002809
Rusty Russellbe593f42010-06-05 11:17:37 -06002810 /* Find duplicate symbols */
2811 err = verify_export_symbols(mod);
2812 if (err < 0)
Yehuda Sadehff49d742010-07-03 13:07:35 +10002813 goto ddebug;
Rusty Russellbe593f42010-06-05 11:17:37 -06002814
Linus Torvalds53363772010-10-05 11:29:27 -07002815 module_bug_finalize(info.hdr, info.sechdrs, mod);
Andi Kleend72b3752008-08-30 10:09:00 +02002816 list_add_rcu(&mod->list, &modules);
Rusty Russell75676502010-06-05 11:17:36 -06002817 mutex_unlock(&module_mutex);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002818
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002819 /* Module is ready to execute: parsing args may do that. */
Rusty Russelle180a6b2009-03-31 13:05:29 -06002820 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002822 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002824 /* Link in to syfs. */
Rusty Russell8f6d0372010-08-05 12:59:09 -06002825 err = mod_sysfs_setup(mod, &info, mod->kp, mod->num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002827 goto unlink;
Rusty Russell80a3d1b2010-06-05 11:17:36 -06002828
Rusty Russelld9131882010-08-05 12:59:08 -06002829 /* Get rid of temporary copy and strmap. */
2830 kfree(info.strmap);
2831 free_copy(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
2833 /* Done! */
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002834 trace_module_load(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 return mod;
2836
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002837 unlink:
Rusty Russell75676502010-06-05 11:17:36 -06002838 mutex_lock(&module_mutex);
Rusty Russelle91defa2009-03-31 13:05:35 -06002839 /* Unlink carefully: kallsyms could be walking list. */
2840 list_del_rcu(&mod->list);
Linus Torvalds53363772010-10-05 11:29:27 -07002841 module_bug_cleanup(mod);
2842
Yehuda Sadehff49d742010-07-03 13:07:35 +10002843 ddebug:
Rusty Russell811d66a2010-08-05 12:59:12 -06002844 if (!mod->taints)
2845 dynamic_debug_remove(info.debug);
Rusty Russellbe593f42010-06-05 11:17:37 -06002846 unlock:
Rusty Russell75676502010-06-05 11:17:36 -06002847 mutex_unlock(&module_mutex);
Rusty Russelle91defa2009-03-31 13:05:35 -06002848 synchronize_sched();
Rusty Russell6526c532010-08-05 12:59:10 -06002849 kfree(mod->args);
2850 free_arch_cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 module_arch_cleanup(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002852 free_modinfo:
Rusty Russella263f772009-09-25 00:32:58 -06002853 free_modinfo(mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002854 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 module_unload_free(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002856 free_module:
2857 module_deallocate(mod, &info);
2858 free_copy:
2859 free_copy(&info);
Jayachandran C6fe2e702006-01-06 00:19:54 -08002860 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861}
2862
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002863/* Call module constructors. */
2864static void do_mod_ctors(struct module *mod)
2865{
2866#ifdef CONFIG_CONSTRUCTORS
2867 unsigned long i;
2868
2869 for (i = 0; i < mod->num_ctors; i++)
2870 mod->ctors[i]();
2871#endif
2872}
2873
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874/* This is where the real work happens */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002875SYSCALL_DEFINE3(init_module, void __user *, umod,
2876 unsigned long, len, const char __user *, uargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877{
2878 struct module *mod;
2879 int ret = 0;
2880
2881 /* Must have permission */
Kees Cook3d433212009-04-02 15:49:29 -07002882 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 return -EPERM;
2884
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 /* Do all the hard work */
2886 mod = load_module(umod, len, uargs);
Rusty Russell75676502010-06-05 11:17:36 -06002887 if (IS_ERR(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 return PTR_ERR(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Alan Sterne041c682006-03-27 01:16:30 -08002890 blocking_notifier_call_chain(&module_notify_list,
2891 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
Steven Rostedt94462ad2010-11-29 13:15:42 -05002893 /* Set RO and NX regions for core */
2894 set_section_ro_nx(mod->module_core,
2895 mod->core_text_size,
2896 mod->core_ro_size,
2897 mod->core_size);
2898
2899 /* Set RO and NX regions for init */
2900 set_section_ro_nx(mod->module_init,
2901 mod->init_text_size,
2902 mod->init_ro_size,
2903 mod->init_size);
2904
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002905 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 /* Start the module */
2907 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002908 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 if (ret < 0) {
2910 /* Init routine failed: abort. Try to protect us from
2911 buggy refcounters. */
2912 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002913 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002914 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002915 blocking_notifier_call_chain(&module_notify_list,
2916 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002917 free_module(mod);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002918 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 return ret;
2920 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002921 if (ret > 0) {
Joe Perchesad361c92009-07-06 13:05:40 -07002922 printk(KERN_WARNING
2923"%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
2924"%s: loading module anyway...\n",
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002925 __func__, mod->name, ret,
2926 __func__);
2927 dump_stack();
2928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Rusty Russell6c5db222008-03-10 11:43:52 -07002930 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002932 wake_up(&module_wq);
Masami Hiramatsu0deddf432009-01-06 14:41:54 -08002933 blocking_notifier_call_chain(&module_notify_list,
2934 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07002935
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07002936 /* We need to finish all async code before the module init sequence is done */
2937 async_synchronize_full();
2938
Rusty Russell6c5db222008-03-10 11:43:52 -07002939 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 /* Drop initial reference. */
2941 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06002942 trim_init_extable(mod);
Jan Beulich4a496222009-07-06 14:50:42 +01002943#ifdef CONFIG_KALLSYMS
2944 mod->num_symtab = mod->core_num_syms;
2945 mod->symtab = mod->core_symtab;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002946 mod->strtab = mod->core_strtab;
Jan Beulich4a496222009-07-06 14:50:42 +01002947#endif
Jan Glauber01526ed2011-05-19 16:55:26 -06002948 unset_module_init_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 module_free(mod, mod->module_init);
2950 mod->module_init = NULL;
2951 mod->init_size = 0;
Jan Glauber4d103802011-05-19 16:55:25 -06002952 mod->init_ro_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002954 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
2956 return 0;
2957}
2958
2959static inline int within(unsigned long addr, void *start, unsigned long size)
2960{
2961 return ((void *)addr >= start && (void *)addr < start + size);
2962}
2963
2964#ifdef CONFIG_KALLSYMS
2965/*
2966 * This ignores the intensely annoying "mapping symbols" found
2967 * in ARM ELF files: $a, $t and $d.
2968 */
2969static inline int is_arm_mapping_symbol(const char *str)
2970{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002971 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 && (str[2] == '\0' || str[2] == '.');
2973}
2974
2975static const char *get_ksymbol(struct module *mod,
2976 unsigned long addr,
2977 unsigned long *size,
2978 unsigned long *offset)
2979{
2980 unsigned int i, best = 0;
2981 unsigned long nextval;
2982
2983 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002984 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002986 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2988
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002989 /* Scan for closest preceding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002990 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 for (i = 1; i < mod->num_symtab; i++) {
2992 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2993 continue;
2994
2995 /* We ignore unnamed symbols: they're uninformative
2996 * and inserted at a whim. */
2997 if (mod->symtab[i].st_value <= addr
2998 && mod->symtab[i].st_value > mod->symtab[best].st_value
2999 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3000 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3001 best = i;
3002 if (mod->symtab[i].st_value > addr
3003 && mod->symtab[i].st_value < nextval
3004 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3005 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3006 nextval = mod->symtab[i].st_value;
3007 }
3008
3009 if (!best)
3010 return NULL;
3011
Alexey Dobriyanffb45122007-05-08 00:28:41 -07003012 if (size)
3013 *size = nextval - mod->symtab[best].st_value;
3014 if (offset)
3015 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 return mod->strtab + mod->symtab[best].st_name;
3017}
3018
Rusty Russell6dd06c92008-01-29 17:13:22 -05003019/* For kallsyms to ask for address resolution. NULL means not found. Careful
3020 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003021const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05003022 unsigned long *size,
3023 unsigned long *offset,
3024 char **modname,
3025 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026{
3027 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08003028 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
Rusty Russellcb2a5202008-01-14 00:55:03 -08003030 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003031 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003032 if (within_module_init(addr, mod) ||
3033 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07003034 if (modname)
3035 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08003036 ret = get_ksymbol(mod, addr, size, offset);
3037 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 }
3039 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05003040 /* Make a copy in here where it's safe */
3041 if (ret) {
3042 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
3043 ret = namebuf;
3044 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003045 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003046 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047}
3048
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003049int lookup_module_symbol_name(unsigned long addr, char *symname)
3050{
3051 struct module *mod;
3052
Rusty Russellcb2a5202008-01-14 00:55:03 -08003053 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003054 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003055 if (within_module_init(addr, mod) ||
3056 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003057 const char *sym;
3058
3059 sym = get_ksymbol(mod, addr, NULL, NULL);
3060 if (!sym)
3061 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07003062 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003063 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003064 return 0;
3065 }
3066 }
3067out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003068 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003069 return -ERANGE;
3070}
3071
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003072int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
3073 unsigned long *offset, char *modname, char *name)
3074{
3075 struct module *mod;
3076
Rusty Russellcb2a5202008-01-14 00:55:03 -08003077 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003078 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003079 if (within_module_init(addr, mod) ||
3080 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003081 const char *sym;
3082
3083 sym = get_ksymbol(mod, addr, size, offset);
3084 if (!sym)
3085 goto out;
3086 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07003087 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003088 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07003089 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003090 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003091 return 0;
3092 }
3093 }
3094out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003095 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003096 return -ERANGE;
3097}
3098
Alexey Dobriyanea078902007-05-08 00:28:39 -07003099int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
3100 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101{
3102 struct module *mod;
3103
Rusty Russellcb2a5202008-01-14 00:55:03 -08003104 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003105 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 if (symnum < mod->num_symtab) {
3107 *value = mod->symtab[symnum].st_value;
3108 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07003109 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07003110 KSYM_NAME_LEN);
3111 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06003112 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003113 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003114 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 }
3116 symnum -= mod->num_symtab;
3117 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003118 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003119 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120}
3121
3122static unsigned long mod_find_symname(struct module *mod, const char *name)
3123{
3124 unsigned int i;
3125
3126 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08003127 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
3128 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 return mod->symtab[i].st_value;
3130 return 0;
3131}
3132
3133/* Look for this name: can be of form module:name. */
3134unsigned long module_kallsyms_lookup_name(const char *name)
3135{
3136 struct module *mod;
3137 char *colon;
3138 unsigned long ret = 0;
3139
3140 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08003141 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 if ((colon = strchr(name, ':')) != NULL) {
3143 *colon = '\0';
3144 if ((mod = find_module(name)) != NULL)
3145 ret = mod_find_symname(mod, colon+1);
3146 *colon = ':';
3147 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02003148 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 if ((ret = mod_find_symname(mod, name)) != 0)
3150 break;
3151 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003152 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 return ret;
3154}
Anders Kaseorg75a66612008-12-05 19:03:58 -05003155
3156int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
3157 struct module *, unsigned long),
3158 void *data)
3159{
3160 struct module *mod;
3161 unsigned int i;
3162 int ret;
3163
3164 list_for_each_entry(mod, &modules, list) {
3165 for (i = 0; i < mod->num_symtab; i++) {
3166 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
3167 mod, mod->symtab[i].st_value);
3168 if (ret != 0)
3169 return ret;
3170 }
3171 }
3172 return 0;
3173}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174#endif /* CONFIG_KALLSYMS */
3175
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003176static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003177{
3178 int bx = 0;
3179
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003180 if (mod->taints ||
3181 mod->state == MODULE_STATE_GOING ||
3182 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003183 buf[bx++] = '(';
Andi Kleen25ddbb12008-10-15 22:01:41 -07003184 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003185 buf[bx++] = 'P';
Andi Kleen25ddbb12008-10-15 22:01:41 -07003186 if (mod->taints & (1 << TAINT_FORCED_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003187 buf[bx++] = 'F';
Linus Torvalds26e9a392008-10-17 09:50:12 -07003188 if (mod->taints & (1 << TAINT_CRAP))
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07003189 buf[bx++] = 'C';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003190 /*
3191 * TAINT_FORCED_RMMOD: could be added.
3192 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
3193 * apply to modules.
3194 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003195
3196 /* Show a - for module-is-being-unloaded */
3197 if (mod->state == MODULE_STATE_GOING)
3198 buf[bx++] = '-';
3199 /* Show a + for module-is-being-loaded */
3200 if (mod->state == MODULE_STATE_COMING)
3201 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003202 buf[bx++] = ')';
3203 }
3204 buf[bx] = '\0';
3205
3206 return buf;
3207}
3208
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003209#ifdef CONFIG_PROC_FS
3210/* Called by the /proc file system to return a list of modules. */
3211static void *m_start(struct seq_file *m, loff_t *pos)
3212{
3213 mutex_lock(&module_mutex);
3214 return seq_list_start(&modules, *pos);
3215}
3216
3217static void *m_next(struct seq_file *m, void *p, loff_t *pos)
3218{
3219 return seq_list_next(p, &modules, pos);
3220}
3221
3222static void m_stop(struct seq_file *m, void *p)
3223{
3224 mutex_unlock(&module_mutex);
3225}
3226
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227static int m_show(struct seq_file *m, void *p)
3228{
3229 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003230 char buf[8];
3231
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05003232 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 mod->name, mod->init_size + mod->core_size);
3234 print_unload_info(m, mod);
3235
3236 /* Informative for users. */
3237 seq_printf(m, " %s",
3238 mod->state == MODULE_STATE_GOING ? "Unloading":
3239 mod->state == MODULE_STATE_COMING ? "Loading":
3240 "Live");
3241 /* Used by oprofile and other similar tools. */
Kees Cook9f36e2c2011-03-22 16:34:22 -07003242 seq_printf(m, " 0x%pK", mod->module_core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003244 /* Taints info */
3245 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003246 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003247
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 seq_printf(m, "\n");
3249 return 0;
3250}
3251
3252/* Format: modulename size refcount deps address
3253
3254 Where refcount is a number or -, and deps is a comma-separated list
3255 of depends or -.
3256*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003257static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 .start = m_start,
3259 .next = m_next,
3260 .stop = m_stop,
3261 .show = m_show
3262};
3263
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003264static int modules_open(struct inode *inode, struct file *file)
3265{
3266 return seq_open(file, &modules_op);
3267}
3268
3269static const struct file_operations proc_modules_operations = {
3270 .open = modules_open,
3271 .read = seq_read,
3272 .llseek = seq_lseek,
3273 .release = seq_release,
3274};
3275
3276static int __init proc_modules_init(void)
3277{
3278 proc_create("modules", 0, NULL, &proc_modules_operations);
3279 return 0;
3280}
3281module_init(proc_modules_init);
3282#endif
3283
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284/* Given an address, look for it in the module exception tables. */
3285const struct exception_table_entry *search_module_extables(unsigned long addr)
3286{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 const struct exception_table_entry *e = NULL;
3288 struct module *mod;
3289
Rusty Russell24da1cb2007-07-15 23:41:46 -07003290 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003291 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 if (mod->num_exentries == 0)
3293 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003294
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 e = search_extable(mod->extable,
3296 mod->extable + mod->num_exentries - 1,
3297 addr);
3298 if (e)
3299 break;
3300 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07003301 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
3303 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07003304 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 return e;
3306}
3307
Ingo Molnar4d435f92006-07-03 00:24:24 -07003308/*
Rusty Russelle6104992009-03-31 13:05:31 -06003309 * is_module_address - is this address inside a module?
3310 * @addr: the address to check.
3311 *
3312 * See is_module_text_address() if you simply want to see if the address
3313 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07003314 */
Rusty Russelle6104992009-03-31 13:05:31 -06003315bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07003316{
Rusty Russelle6104992009-03-31 13:05:31 -06003317 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003318
Rusty Russell24da1cb2007-07-15 23:41:46 -07003319 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06003320 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07003321 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07003322
Rusty Russelle6104992009-03-31 13:05:31 -06003323 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003324}
3325
Rusty Russelle6104992009-03-31 13:05:31 -06003326/*
3327 * __module_address - get the module which contains an address.
3328 * @addr: the address.
3329 *
3330 * Must be called with preempt disabled or module mutex held so that
3331 * module doesn't get freed during this.
3332 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07003333struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334{
3335 struct module *mod;
3336
Rusty Russell3a642e92008-07-22 19:24:28 -05003337 if (addr < module_addr_min || addr > module_addr_max)
3338 return NULL;
3339
Andi Kleend72b3752008-08-30 10:09:00 +02003340 list_for_each_entry_rcu(mod, &modules, list)
Rusty Russelle6104992009-03-31 13:05:31 -06003341 if (within_module_core(addr, mod)
3342 || within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 return mod;
3344 return NULL;
3345}
Tim Abbottc6b37802008-12-05 19:03:59 -05003346EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347
Rusty Russelle6104992009-03-31 13:05:31 -06003348/*
3349 * is_module_text_address - is this address inside module code?
3350 * @addr: the address to check.
3351 *
3352 * See is_module_address() if you simply want to see if the address is
3353 * anywhere in a module. See kernel_text_address() for testing if an
3354 * address corresponds to kernel or module code.
3355 */
3356bool is_module_text_address(unsigned long addr)
3357{
3358 bool ret;
3359
3360 preempt_disable();
3361 ret = __module_text_address(addr) != NULL;
3362 preempt_enable();
3363
3364 return ret;
3365}
3366
3367/*
3368 * __module_text_address - get the module whose code contains an address.
3369 * @addr: the address.
3370 *
3371 * Must be called with preempt disabled or module mutex held so that
3372 * module doesn't get freed during this.
3373 */
3374struct module *__module_text_address(unsigned long addr)
3375{
3376 struct module *mod = __module_address(addr);
3377 if (mod) {
3378 /* Make sure it's within the text section. */
3379 if (!within(addr, mod->module_init, mod->init_text_size)
3380 && !within(addr, mod->module_core, mod->core_text_size))
3381 mod = NULL;
3382 }
3383 return mod;
3384}
Tim Abbottc6b37802008-12-05 19:03:59 -05003385EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06003386
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387/* Don't grab lock, we're oopsing. */
3388void print_modules(void)
3389{
3390 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07003391 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
Linus Torvaldsb2311252009-06-16 11:07:14 -07003393 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02003394 /* Most callers should already have preempt disabled, but make sure */
3395 preempt_disable();
3396 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003397 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02003398 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01003399 if (last_unloaded_module[0])
3400 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 printk("\n");
3402}
3403
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06003405/* Generate the signature for all relevant module structures here.
3406 * If these change, we don't want to try to parse the module. */
3407void module_layout(struct module *mod,
3408 struct modversion_info *ver,
3409 struct kernel_param *kp,
3410 struct kernel_symbol *ks,
Mathieu Desnoyers65498642011-01-26 17:26:22 -05003411 struct tracepoint * const *tp)
Rusty Russell8c8ef422009-03-31 13:05:34 -06003412{
3413}
3414EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07003416
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04003417#ifdef CONFIG_TRACEPOINTS
3418void module_update_tracepoints(void)
3419{
3420 struct module *mod;
3421
3422 mutex_lock(&module_mutex);
3423 list_for_each_entry(mod, &modules, list)
3424 if (!mod->taints)
Mathieu Desnoyers65498642011-01-26 17:26:22 -05003425 tracepoint_update_probe_range(mod->tracepoints_ptrs,
3426 mod->tracepoints_ptrs + mod->num_tracepoints);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04003427 mutex_unlock(&module_mutex);
3428}
3429
3430/*
3431 * Returns 0 if current not found.
3432 * Returns 1 if current found.
3433 */
3434int module_get_iter_tracepoints(struct tracepoint_iter *iter)
3435{
3436 struct module *iter_mod;
3437 int found = 0;
3438
3439 mutex_lock(&module_mutex);
3440 list_for_each_entry(iter_mod, &modules, list) {
3441 if (!iter_mod->taints) {
3442 /*
3443 * Sorted module list
3444 */
3445 if (iter_mod < iter->module)
3446 continue;
3447 else if (iter_mod > iter->module)
3448 iter->tracepoint = NULL;
3449 found = tracepoint_get_iter_range(&iter->tracepoint,
Mathieu Desnoyers65498642011-01-26 17:26:22 -05003450 iter_mod->tracepoints_ptrs,
3451 iter_mod->tracepoints_ptrs
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04003452 + iter_mod->num_tracepoints);
3453 if (found) {
3454 iter->module = iter_mod;
3455 break;
3456 }
3457 }
3458 }
3459 mutex_unlock(&module_mutex);
3460 return found;
3461}
3462#endif