blob: b99dcebc980d035d97e354f967500cbe2f905107 [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,
243 unsigned int symnum, void *data),
244 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100245{
Rusty Russelldafd0942008-07-22 19:24:25 -0500246 unsigned int i, j;
247
248 for (j = 0; j < arrsize; j++) {
249 for (i = 0; i < arr[j].stop - arr[j].start; i++)
250 if (fn(&arr[j], owner, i, data))
251 return true;
252 }
253
254 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100255}
256
Rusty Russelldafd0942008-07-22 19:24:25 -0500257/* Returns true as soon as fn returns true, otherwise false. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500258bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
259 unsigned int symnum, void *data), void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700260{
Rusty Russelldafd0942008-07-22 19:24:25 -0500261 struct module *mod;
Linus Torvalds44032e62010-08-05 12:59:05 -0600262 static const struct symsearch arr[] = {
Rusty Russelldafd0942008-07-22 19:24:25 -0500263 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
264 NOT_GPL_ONLY, false },
265 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
266 __start___kcrctab_gpl,
267 GPL_ONLY, false },
268 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
269 __start___kcrctab_gpl_future,
270 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500271#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500272 { __start___ksymtab_unused, __stop___ksymtab_unused,
273 __start___kcrctab_unused,
274 NOT_GPL_ONLY, true },
275 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
276 __start___kcrctab_unused_gpl,
277 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500278#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500279 };
280
281 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
282 return true;
283
Andi Kleend72b3752008-08-30 10:09:00 +0200284 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500285 struct symsearch arr[] = {
286 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
287 NOT_GPL_ONLY, false },
288 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
289 mod->gpl_crcs,
290 GPL_ONLY, false },
291 { mod->gpl_future_syms,
292 mod->gpl_future_syms + mod->num_gpl_future_syms,
293 mod->gpl_future_crcs,
294 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500295#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500296 { mod->unused_syms,
297 mod->unused_syms + mod->num_unused_syms,
298 mod->unused_crcs,
299 NOT_GPL_ONLY, true },
300 { mod->unused_gpl_syms,
301 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
302 mod->unused_gpl_crcs,
303 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500304#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500305 };
306
307 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
308 return true;
309 }
310 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700311}
Tim Abbottc6b37802008-12-05 19:03:59 -0500312EXPORT_SYMBOL_GPL(each_symbol);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700313
Rusty Russelldafd0942008-07-22 19:24:25 -0500314struct find_symbol_arg {
315 /* Input */
316 const char *name;
317 bool gplok;
318 bool warn;
319
320 /* Output */
321 struct module *owner;
322 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500323 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500324};
325
326static bool find_symbol_in_section(const struct symsearch *syms,
327 struct module *owner,
328 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500329{
Rusty Russelldafd0942008-07-22 19:24:25 -0500330 struct find_symbol_arg *fsa = data;
331
332 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
333 return false;
334
335 if (!fsa->gplok) {
336 if (syms->licence == GPL_ONLY)
337 return false;
338 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
339 printk(KERN_WARNING "Symbol %s is being used "
340 "by a non-GPL module, which will not "
341 "be allowed in the future\n", fsa->name);
342 printk(KERN_WARNING "Please see the file "
343 "Documentation/feature-removal-schedule.txt "
344 "in the kernel source tree for more details.\n");
345 }
346 }
347
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500348#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500349 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500350 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500351 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500352 printk(KERN_WARNING
353 "This symbol will go away in the future.\n");
354 printk(KERN_WARNING
355 "Please evalute if this is the right api to use and if "
356 "it really is, submit a report the linux kernel "
357 "mailinglist together with submitting your code for "
358 "inclusion.\n");
359 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500360#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500361
362 fsa->owner = owner;
363 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500364 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500365 return true;
366}
367
Tim Abbott414fd312008-12-05 19:03:56 -0500368/* Find a symbol and return it, along with, (optional) crc and
Rusty Russell75676502010-06-05 11:17:36 -0600369 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500370const struct kernel_symbol *find_symbol(const char *name,
371 struct module **owner,
372 const unsigned long **crc,
373 bool gplok,
374 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Rusty Russelldafd0942008-07-22 19:24:25 -0500376 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Rusty Russelldafd0942008-07-22 19:24:25 -0500378 fsa.name = name;
379 fsa.gplok = gplok;
380 fsa.warn = warn;
381
382 if (each_symbol(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500383 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500384 *owner = fsa.owner;
385 if (crc)
386 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500387 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 DEBUGP("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500391 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
Tim Abbottc6b37802008-12-05 19:03:59 -0500393EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395/* Search for module by name: must hold module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500396struct module *find_module(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 struct module *mod;
399
400 list_for_each_entry(mod, &modules, list) {
401 if (strcmp(mod->name, name) == 0)
402 return mod;
403 }
404 return NULL;
405}
Tim Abbottc6b37802008-12-05 19:03:59 -0500406EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900409
Tejun Heo259354d2010-03-10 18:56:10 +0900410static inline void __percpu *mod_percpu(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900411{
Tejun Heo259354d2010-03-10 18:56:10 +0900412 return mod->percpu;
413}
Tejun Heofbf59bc2009-02-20 16:29:08 +0900414
Tejun Heo259354d2010-03-10 18:56:10 +0900415static int percpu_modalloc(struct module *mod,
416 unsigned long size, unsigned long align)
417{
Tejun Heofbf59bc2009-02-20 16:29:08 +0900418 if (align > PAGE_SIZE) {
419 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
Tejun Heo259354d2010-03-10 18:56:10 +0900420 mod->name, align, PAGE_SIZE);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900421 align = PAGE_SIZE;
422 }
423
Tejun Heo259354d2010-03-10 18:56:10 +0900424 mod->percpu = __alloc_reserved_percpu(size, align);
425 if (!mod->percpu) {
Tejun Heofbf59bc2009-02-20 16:29:08 +0900426 printk(KERN_WARNING
Rusty Russelld9131882010-08-05 12:59:08 -0600427 "%s: Could not allocate %lu bytes percpu data\n",
428 mod->name, size);
Tejun Heo259354d2010-03-10 18:56:10 +0900429 return -ENOMEM;
430 }
431 mod->percpu_size = size;
432 return 0;
Tejun Heofbf59bc2009-02-20 16:29:08 +0900433}
434
Tejun Heo259354d2010-03-10 18:56:10 +0900435static void percpu_modfree(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900436{
Tejun Heo259354d2010-03-10 18:56:10 +0900437 free_percpu(mod->percpu);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900438}
439
Rusty Russell49668682010-08-05 12:59:10 -0600440static unsigned int find_pcpusec(struct load_info *info)
Tejun Heo6b588c12009-02-20 16:29:07 +0900441{
Rusty Russell49668682010-08-05 12:59:10 -0600442 return find_sec(info, ".data..percpu");
Tejun Heo6b588c12009-02-20 16:29:07 +0900443}
444
Tejun Heo259354d2010-03-10 18:56:10 +0900445static void percpu_modcopy(struct module *mod,
446 const void *from, unsigned long size)
Tejun Heo6b588c12009-02-20 16:29:07 +0900447{
448 int cpu;
449
450 for_each_possible_cpu(cpu)
Tejun Heo259354d2010-03-10 18:56:10 +0900451 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
Tejun Heo6b588c12009-02-20 16:29:07 +0900452}
453
Tejun Heo10fad5e2010-03-10 18:57:54 +0900454/**
455 * is_module_percpu_address - test whether address is from module static percpu
456 * @addr: address to test
457 *
458 * Test whether @addr belongs to module static percpu area.
459 *
460 * RETURNS:
461 * %true if @addr is from module static percpu area
462 */
463bool is_module_percpu_address(unsigned long addr)
464{
465 struct module *mod;
466 unsigned int cpu;
467
468 preempt_disable();
469
470 list_for_each_entry_rcu(mod, &modules, list) {
471 if (!mod->percpu_size)
472 continue;
473 for_each_possible_cpu(cpu) {
474 void *start = per_cpu_ptr(mod->percpu, cpu);
475
476 if ((void *)addr >= start &&
477 (void *)addr < start + mod->percpu_size) {
478 preempt_enable();
479 return true;
480 }
481 }
482 }
483
484 preempt_enable();
485 return false;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700486}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900489
Tejun Heo259354d2010-03-10 18:56:10 +0900490static inline void __percpu *mod_percpu(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 return NULL;
493}
Tejun Heo259354d2010-03-10 18:56:10 +0900494static inline int percpu_modalloc(struct module *mod,
495 unsigned long size, unsigned long align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Tejun Heo259354d2010-03-10 18:56:10 +0900497 return -ENOMEM;
498}
499static inline void percpu_modfree(struct module *mod)
500{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
Rusty Russell49668682010-08-05 12:59:10 -0600502static unsigned int find_pcpusec(struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 return 0;
505}
Tejun Heo259354d2010-03-10 18:56:10 +0900506static inline void percpu_modcopy(struct module *mod,
507 const void *from, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 /* pcpusec should be 0, and size of that section should be 0. */
510 BUG_ON(size != 0);
511}
Tejun Heo10fad5e2010-03-10 18:57:54 +0900512bool is_module_percpu_address(unsigned long addr)
513{
514 return false;
515}
Tejun Heo6b588c12009-02-20 16:29:07 +0900516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517#endif /* CONFIG_SMP */
518
Matt Domschc988d2b2005-06-23 22:05:15 -0700519#define MODINFO_ATTR(field) \
520static void setup_modinfo_##field(struct module *mod, const char *s) \
521{ \
522 mod->field = kstrdup(s, GFP_KERNEL); \
523} \
524static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
525 struct module *mod, char *buffer) \
526{ \
527 return sprintf(buffer, "%s\n", mod->field); \
528} \
529static int modinfo_##field##_exists(struct module *mod) \
530{ \
531 return mod->field != NULL; \
532} \
533static void free_modinfo_##field(struct module *mod) \
534{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700535 kfree(mod->field); \
536 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700537} \
538static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900539 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700540 .show = show_modinfo_##field, \
541 .setup = setup_modinfo_##field, \
542 .test = modinfo_##field##_exists, \
543 .free = free_modinfo_##field, \
544};
545
546MODINFO_ATTR(version);
547MODINFO_ATTR(srcversion);
548
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100549static char last_unloaded_module[MODULE_NAME_LEN+1];
550
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -0800551#ifdef CONFIG_MODULE_UNLOAD
Steven Rostedteb0c5372010-03-29 14:25:18 -0400552
553EXPORT_TRACEPOINT_SYMBOL(module_get);
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555/* Init the unload section of the module. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600556static int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600558 mod->refptr = alloc_percpu(struct module_ref);
559 if (!mod->refptr)
560 return -ENOMEM;
561
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700562 INIT_LIST_HEAD(&mod->source_list);
563 INIT_LIST_HEAD(&mod->target_list);
Christoph Lametere1783a22010-01-05 15:34:50 +0900564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 /* Hold reference count during initialization. */
Nick Piggin5fbfb182010-04-01 19:09:40 +1100566 __this_cpu_write(mod->refptr->incs, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 /* Backwards compatibility macros put refcount during init. */
568 mod->waiter = current;
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600569
570 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573/* Does a already use b? */
574static int already_uses(struct module *a, struct module *b)
575{
576 struct module_use *use;
577
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700578 list_for_each_entry(use, &b->source_list, source_list) {
579 if (use->source == a) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 DEBUGP("%s uses %s!\n", a->name, b->name);
581 return 1;
582 }
583 }
584 DEBUGP("%s does not use %s!\n", a->name, b->name);
585 return 0;
586}
587
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700588/*
589 * Module a uses b
590 * - we add 'a' as a "source", 'b' as a "target" of module use
591 * - the module_use is added to the list of 'b' sources (so
592 * 'b' can walk the list to see who sourced them), and of 'a'
593 * targets (so 'a' can see what modules it targets).
594 */
595static int add_module_usage(struct module *a, struct module *b)
596{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700597 struct module_use *use;
598
599 DEBUGP("Allocating new usage for %s.\n", a->name);
600 use = kmalloc(sizeof(*use), GFP_ATOMIC);
601 if (!use) {
602 printk(KERN_WARNING "%s: out of memory loading\n", a->name);
603 return -ENOMEM;
604 }
605
606 use->source = a;
607 use->target = b;
608 list_add(&use->source_list, &b->source_list);
609 list_add(&use->target_list, &a->target_list);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700610 return 0;
611}
612
Rusty Russell75676502010-06-05 11:17:36 -0600613/* Module a uses b: caller needs module_mutex() */
Rusty Russell9bea7f22010-06-05 11:17:37 -0600614int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Rusty Russellc8e21ce2010-06-05 11:17:35 -0600616 int err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100617
Rusty Russell9bea7f22010-06-05 11:17:37 -0600618 if (b == NULL || already_uses(a, b))
Linus Torvalds218ce732010-05-25 16:48:30 -0700619 return 0;
Linus Torvalds218ce732010-05-25 16:48:30 -0700620
Rusty Russell9bea7f22010-06-05 11:17:37 -0600621 /* If module isn't available, we fail. */
622 err = strong_try_module_get(b);
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500623 if (err)
Rusty Russell9bea7f22010-06-05 11:17:37 -0600624 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700626 err = add_module_usage(a, b);
627 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 module_put(b);
Rusty Russell9bea7f22010-06-05 11:17:37 -0600629 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
Rusty Russell9bea7f22010-06-05 11:17:37 -0600631 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600633EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635/* Clear the unload stuff of the module. */
636static void module_unload_free(struct module *mod)
637{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700638 struct module_use *use, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Rusty Russell75676502010-06-05 11:17:36 -0600640 mutex_lock(&module_mutex);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700641 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
642 struct module *i = use->target;
643 DEBUGP("%s unusing %s\n", mod->name, i->name);
644 module_put(i);
645 list_del(&use->source_list);
646 list_del(&use->target_list);
647 kfree(use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
Rusty Russell75676502010-06-05 11:17:36 -0600649 mutex_unlock(&module_mutex);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600650
651 free_percpu(mod->refptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
654#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800655static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 int ret = (flags & O_TRUNC);
658 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800659 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return ret;
661}
662#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800663static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 return 0;
666}
667#endif /* CONFIG_MODULE_FORCE_UNLOAD */
668
669struct stopref
670{
671 struct module *mod;
672 int flags;
673 int *forced;
674};
675
676/* Whole machine is stopped with interrupts off when this runs. */
677static int __try_stop_module(void *_sref)
678{
679 struct stopref *sref = _sref;
680
Rusty Russellda39ba52008-07-22 19:24:25 -0500681 /* If it's not unused, quit unless we're forcing. */
682 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800683 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return -EWOULDBLOCK;
685 }
686
687 /* Mark it as dying. */
688 sref->mod->state = MODULE_STATE_GOING;
689 return 0;
690}
691
692static int try_stop_module(struct module *mod, int flags, int *forced)
693{
Rusty Russellda39ba52008-07-22 19:24:25 -0500694 if (flags & O_NONBLOCK) {
695 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500697 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500698 } else {
699 /* We don't need to stop the machine for this. */
700 mod->state = MODULE_STATE_GOING;
701 synchronize_sched();
702 return 0;
703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
706unsigned int module_refcount(struct module *mod)
707{
Nick Piggin5fbfb182010-04-01 19:09:40 +1100708 unsigned int incs = 0, decs = 0;
Eric Dumazet720eba32009-02-03 13:31:36 +1030709 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Eric Dumazet720eba32009-02-03 13:31:36 +1030711 for_each_possible_cpu(cpu)
Nick Piggin5fbfb182010-04-01 19:09:40 +1100712 decs += per_cpu_ptr(mod->refptr, cpu)->decs;
713 /*
714 * ensure the incs are added up after the decs.
715 * module_put ensures incs are visible before decs with smp_wmb.
716 *
717 * This 2-count scheme avoids the situation where the refcount
718 * for CPU0 is read, then CPU0 increments the module refcount,
719 * then CPU1 drops that refcount, then the refcount for CPU1 is
720 * read. We would record a decrement but not its corresponding
721 * increment so we would see a low count (disaster).
722 *
723 * Rare situation? But module_refcount can be preempted, and we
724 * might be tallying up 4096+ CPUs. So it is not impossible.
725 */
726 smp_rmb();
727 for_each_possible_cpu(cpu)
728 incs += per_cpu_ptr(mod->refptr, cpu)->incs;
729 return incs - decs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731EXPORT_SYMBOL(module_refcount);
732
733/* This exists whether we can unload or not */
734static void free_module(struct module *mod);
735
736static void wait_for_zero_refcount(struct module *mod)
737{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500738 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800739 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 for (;;) {
741 DEBUGP("Looking at refcount...\n");
742 set_current_state(TASK_UNINTERRUPTIBLE);
743 if (module_refcount(mod) == 0)
744 break;
745 schedule();
746 }
747 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800748 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100751SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
752 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800755 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 int ret, forced = 0;
757
Kees Cook3d433212009-04-02 15:49:29 -0700758 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800759 return -EPERM;
760
761 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
762 return -EFAULT;
763 name[MODULE_NAME_LEN-1] = '\0';
764
Tejun Heo3fc1f1e2010-05-06 18:49:20 +0200765 if (mutex_lock_interruptible(&module_mutex) != 0)
766 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 mod = find_module(name);
769 if (!mod) {
770 ret = -ENOENT;
771 goto out;
772 }
773
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700774 if (!list_empty(&mod->source_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 /* Other modules depend on us: get rid of them first. */
776 ret = -EWOULDBLOCK;
777 goto out;
778 }
779
780 /* Doing init or already dying? */
781 if (mod->state != MODULE_STATE_LIVE) {
782 /* FIXME: if (force), slam module count and wake up
783 waiter --RR */
784 DEBUGP("%s already dying\n", mod->name);
785 ret = -EBUSY;
786 goto out;
787 }
788
789 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700790 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800791 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (!forced) {
793 /* This module can't be removed */
794 ret = -EBUSY;
795 goto out;
796 }
797 }
798
799 /* Set this up before setting mod->state */
800 mod->waiter = current;
801
802 /* Stop the machine so refcounts can't move and disable module. */
803 ret = try_stop_module(mod, flags, &forced);
804 if (ret != 0)
805 goto out;
806
807 /* Never wait if forced. */
808 if (!forced && module_refcount(mod) != 0)
809 wait_for_zero_refcount(mod);
810
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200811 mutex_unlock(&module_mutex);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300812 /* Final destruction now no one is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200813 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200815 blocking_notifier_call_chain(&module_notify_list,
816 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800817 async_synchronize_full();
Rusty Russell75676502010-06-05 11:17:36 -0600818
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100819 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500820 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Rusty Russell75676502010-06-05 11:17:36 -0600822 free_module(mod);
823 return 0;
824out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800825 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return ret;
827}
828
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800829static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
831 struct module_use *use;
832 int printed_something = 0;
833
834 seq_printf(m, " %u ", module_refcount(mod));
835
836 /* Always include a trailing , so userspace can differentiate
837 between this and the old multi-field proc format. */
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700838 list_for_each_entry(use, &mod->source_list, source_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 printed_something = 1;
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700840 seq_printf(m, "%s,", use->source->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (mod->init != NULL && mod->exit == NULL) {
844 printed_something = 1;
845 seq_printf(m, "[permanent],");
846 }
847
848 if (!printed_something)
849 seq_printf(m, "-");
850}
851
852void __symbol_put(const char *symbol)
853{
854 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Rusty Russell24da1cb2007-07-15 23:41:46 -0700856 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -0500857 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 BUG();
859 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700860 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
862EXPORT_SYMBOL(__symbol_put);
863
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930864/* Note this assumes addr is a function, which it currently always is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865void symbol_put_addr(void *addr)
866{
Trent Piepho5e376612006-05-15 09:44:06 -0700867 struct module *modaddr;
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930868 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930870 if (core_kernel_text(a))
Trent Piepho5e376612006-05-15 09:44:06 -0700871 return;
872
Rusty Russella6e6abd2009-03-31 13:05:31 -0600873 /* module_text_address is safe here: we're supposed to have reference
874 * to module from symbol_get, so it can't go away. */
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930875 modaddr = __module_text_address(a);
Rusty Russella6e6abd2009-03-31 13:05:31 -0600876 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -0700877 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879EXPORT_SYMBOL_GPL(symbol_put_addr);
880
881static ssize_t show_refcnt(struct module_attribute *mattr,
882 struct module *mod, char *buffer)
883{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400884 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886
887static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900888 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 .show = show_refcnt,
890};
891
Al Virof6a57032006-10-18 01:47:25 -0400892void module_put(struct module *module)
893{
894 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +0900895 preempt_disable();
Nick Piggin5fbfb182010-04-01 19:09:40 +1100896 smp_wmb(); /* see comment in module_refcount */
897 __this_cpu_inc(module->refptr->decs);
Christoph Lametere1783a22010-01-05 15:34:50 +0900898
Li Zefanae832d12010-03-24 10:57:43 +0800899 trace_module_put(module, _RET_IP_);
Al Virof6a57032006-10-18 01:47:25 -0400900 /* Maybe they're waiting for us to drop reference? */
901 if (unlikely(!module_is_live(module)))
902 wake_up_process(module->waiter);
Christoph Lametere1783a22010-01-05 15:34:50 +0900903 preempt_enable();
Al Virof6a57032006-10-18 01:47:25 -0400904 }
905}
906EXPORT_SYMBOL(module_put);
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800909static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
911 /* We don't know the usage count, or what modules are using. */
912 seq_printf(m, " - -");
913}
914
915static inline void module_unload_free(struct module *mod)
916{
917}
918
Rusty Russell9bea7f22010-06-05 11:17:37 -0600919int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Rusty Russell9bea7f22010-06-05 11:17:37 -0600921 return strong_try_module_get(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600923EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600925static inline int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600927 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929#endif /* CONFIG_MODULE_UNLOAD */
930
Kay Sievers1f717402006-11-24 12:15:25 +0100931static ssize_t show_initstate(struct module_attribute *mattr,
932 struct module *mod, char *buffer)
933{
934 const char *state = "unknown";
935
936 switch (mod->state) {
937 case MODULE_STATE_LIVE:
938 state = "live";
939 break;
940 case MODULE_STATE_COMING:
941 state = "coming";
942 break;
943 case MODULE_STATE_GOING:
944 state = "going";
945 break;
946 }
947 return sprintf(buffer, "%s\n", state);
948}
949
950static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900951 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100952 .show = show_initstate,
953};
954
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -0800955static struct module_attribute *modinfo_attrs[] = {
956 &modinfo_version,
957 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100958 &initstate,
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -0800959#ifdef CONFIG_MODULE_UNLOAD
960 &refcnt,
961#endif
962 NULL,
963};
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965static const char vermagic[] = VERMAGIC_STRING;
966
Rusty Russellc6e665c2009-03-31 13:05:33 -0600967static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -0700968{
969#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -0700970 if (!test_taint(TAINT_FORCED_MODULE))
Rusty Russellc6e665c2009-03-31 13:05:33 -0600971 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
972 mod->name, reason);
Linus Torvalds826e4502008-05-04 17:04:16 -0700973 add_taint_module(mod, TAINT_FORCED_MODULE);
974 return 0;
975#else
976 return -ENOEXEC;
977#endif
978}
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980#ifdef CONFIG_MODVERSIONS
Rusty Russelld4703ae2009-12-15 16:28:32 -0600981/* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
982static unsigned long maybe_relocated(unsigned long crc,
983 const struct module *crc_owner)
984{
985#ifdef ARCH_RELOCATES_KCRCTAB
986 if (crc_owner == NULL)
987 return crc - (unsigned long)reloc_start;
988#endif
989 return crc;
990}
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992static int check_version(Elf_Shdr *sechdrs,
993 unsigned int versindex,
994 const char *symname,
995 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -0600996 const unsigned long *crc,
997 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 unsigned int i, num_versions;
1000 struct modversion_info *versions;
1001
1002 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1003 if (!crc)
1004 return 1;
1005
Rusty Russella5dd6972008-05-09 16:24:21 +10001006 /* No versions at all? modprobe --force does this. */
1007 if (versindex == 0)
1008 return try_to_force_load(mod, symname) == 0;
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 versions = (void *) sechdrs[versindex].sh_addr;
1011 num_versions = sechdrs[versindex].sh_size
1012 / sizeof(struct modversion_info);
1013
1014 for (i = 0; i < num_versions; i++) {
1015 if (strcmp(versions[i].name, symname) != 0)
1016 continue;
1017
Rusty Russelld4703ae2009-12-15 16:28:32 -06001018 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 DEBUGP("Found checksum %lX vs module %lX\n",
Rusty Russelld4703ae2009-12-15 16:28:32 -06001021 maybe_relocated(*crc, crc_owner), versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001022 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001024
Rusty Russella5dd6972008-05-09 16:24:21 +10001025 printk(KERN_WARNING "%s: no symbol version for %s\n",
1026 mod->name, symname);
1027 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001028
1029bad_version:
1030 printk("%s: disagrees about version of symbol %s\n",
1031 mod->name, symname);
1032 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
1035static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1036 unsigned int versindex,
1037 struct module *mod)
1038{
1039 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Rusty Russell75676502010-06-05 11:17:36 -06001041 /* Since this should be found in kernel (which can't be removed),
1042 * no locking is necessary. */
Mike Frysinger6560dc12009-07-23 23:42:08 +09301043 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1044 &crc, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 BUG();
Rusty Russelld4703ae2009-12-15 16:28:32 -06001046 return check_version(sechdrs, versindex, "module_layout", mod, crc,
1047 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
Rusty Russell91e37a72008-05-09 16:25:28 +10001050/* First part is kernel version, which we ignore if module has crcs. */
1051static inline int same_magic(const char *amagic, const char *bmagic,
1052 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
Rusty Russell91e37a72008-05-09 16:25:28 +10001054 if (has_crcs) {
1055 amagic += strcspn(amagic, " ");
1056 bmagic += strcspn(bmagic, " ");
1057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return strcmp(amagic, bmagic) == 0;
1059}
1060#else
1061static inline int check_version(Elf_Shdr *sechdrs,
1062 unsigned int versindex,
1063 const char *symname,
1064 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001065 const unsigned long *crc,
1066 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
1068 return 1;
1069}
1070
1071static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1072 unsigned int versindex,
1073 struct module *mod)
1074{
1075 return 1;
1076}
1077
Rusty Russell91e37a72008-05-09 16:25:28 +10001078static inline int same_magic(const char *amagic, const char *bmagic,
1079 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
1081 return strcmp(amagic, bmagic) == 0;
1082}
1083#endif /* CONFIG_MODVERSIONS */
1084
Rusty Russell75676502010-06-05 11:17:36 -06001085/* Resolve a symbol for this module. I.e. if we find one, record usage. */
Rusty Russell49668682010-08-05 12:59:10 -06001086static const struct kernel_symbol *resolve_symbol(struct module *mod,
1087 const struct load_info *info,
Tim Abbott414fd312008-12-05 19:03:56 -05001088 const char *name,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001089 char ownername[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001092 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 const unsigned long *crc;
Rusty Russell9bea7f22010-06-05 11:17:37 -06001094 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Rusty Russell75676502010-06-05 11:17:36 -06001096 mutex_lock(&module_mutex);
Tim Abbott414fd312008-12-05 19:03:56 -05001097 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001098 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001099 if (!sym)
1100 goto unlock;
1101
Rusty Russell49668682010-08-05 12:59:10 -06001102 if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
1103 owner)) {
Rusty Russell9bea7f22010-06-05 11:17:37 -06001104 sym = ERR_PTR(-EINVAL);
1105 goto getname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
Rusty Russell9bea7f22010-06-05 11:17:37 -06001107
1108 err = ref_module(mod, owner);
1109 if (err) {
1110 sym = ERR_PTR(err);
1111 goto getname;
1112 }
1113
1114getname:
1115 /* We must make copy under the lock if we failed to get ref. */
1116 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1117unlock:
Rusty Russell75676502010-06-05 11:17:36 -06001118 mutex_unlock(&module_mutex);
Linus Torvalds218ce732010-05-25 16:48:30 -07001119 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
Rusty Russell49668682010-08-05 12:59:10 -06001122static const struct kernel_symbol *
1123resolve_symbol_wait(struct module *mod,
1124 const struct load_info *info,
1125 const char *name)
Rusty Russell9bea7f22010-06-05 11:17:37 -06001126{
1127 const struct kernel_symbol *ksym;
Rusty Russell49668682010-08-05 12:59:10 -06001128 char owner[MODULE_NAME_LEN];
Rusty Russell9bea7f22010-06-05 11:17:37 -06001129
1130 if (wait_event_interruptible_timeout(module_wq,
Rusty Russell49668682010-08-05 12:59:10 -06001131 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1132 || PTR_ERR(ksym) != -EBUSY,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001133 30 * HZ) <= 0) {
1134 printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
Rusty Russell49668682010-08-05 12:59:10 -06001135 mod->name, owner);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001136 }
1137 return ksym;
1138}
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140/*
1141 * /sys/module/foo/sections stuff
1142 * J. Corbet <corbet@lwn.net>
1143 */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001144#ifdef CONFIG_SYSFS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001145
Rusty Russell8f6d0372010-08-05 12:59:09 -06001146#ifdef CONFIG_KALLSYMS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001147static inline bool sect_empty(const Elf_Shdr *sect)
1148{
1149 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1150}
1151
Rusty Russella58730c2008-03-13 09:03:44 +00001152struct module_sect_attr
1153{
1154 struct module_attribute mattr;
1155 char *name;
1156 unsigned long address;
1157};
1158
1159struct module_sect_attrs
1160{
1161 struct attribute_group grp;
1162 unsigned int nsections;
1163 struct module_sect_attr attrs[0];
1164};
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166static ssize_t module_sect_show(struct module_attribute *mattr,
1167 struct module *mod, char *buf)
1168{
1169 struct module_sect_attr *sattr =
1170 container_of(mattr, struct module_sect_attr, mattr);
Kees Cook9f36e2c2011-03-22 16:34:22 -07001171 return sprintf(buf, "0x%pK\n", (void *)sattr->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
1173
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001174static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1175{
Rusty Russella58730c2008-03-13 09:03:44 +00001176 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001177
1178 for (section = 0; section < sect_attrs->nsections; section++)
1179 kfree(sect_attrs->attrs[section].name);
1180 kfree(sect_attrs);
1181}
1182
Rusty Russell8f6d0372010-08-05 12:59:09 -06001183static void add_sect_attrs(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
1185 unsigned int nloaded = 0, i, size[2];
1186 struct module_sect_attrs *sect_attrs;
1187 struct module_sect_attr *sattr;
1188 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 /* Count loaded sections and allocate structures */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001191 for (i = 0; i < info->hdr->e_shnum; i++)
1192 if (!sect_empty(&info->sechdrs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 nloaded++;
1194 size[0] = ALIGN(sizeof(*sect_attrs)
1195 + nloaded * sizeof(sect_attrs->attrs[0]),
1196 sizeof(sect_attrs->grp.attrs[0]));
1197 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001198 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1199 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return;
1201
1202 /* Setup section attributes. */
1203 sect_attrs->grp.name = "sections";
1204 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1205
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001206 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 sattr = &sect_attrs->attrs[0];
1208 gattr = &sect_attrs->grp.attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001209 for (i = 0; i < info->hdr->e_shnum; i++) {
1210 Elf_Shdr *sec = &info->sechdrs[i];
1211 if (sect_empty(sec))
Helge Deller35dead42009-12-03 00:29:15 +01001212 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001213 sattr->address = sec->sh_addr;
1214 sattr->name = kstrdup(info->secstrings + sec->sh_name,
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001215 GFP_KERNEL);
1216 if (sattr->name == NULL)
1217 goto out;
1218 sect_attrs->nsections++;
Eric W. Biederman361795b2010-02-12 13:41:56 -08001219 sysfs_attr_init(&sattr->mattr.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 sattr->mattr.show = module_sect_show;
1221 sattr->mattr.store = NULL;
1222 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 sattr->mattr.attr.mode = S_IRUGO;
1224 *(gattr++) = &(sattr++)->mattr.attr;
1225 }
1226 *gattr = NULL;
1227
1228 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1229 goto out;
1230
1231 mod->sect_attrs = sect_attrs;
1232 return;
1233 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001234 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
1237static void remove_sect_attrs(struct module *mod)
1238{
1239 if (mod->sect_attrs) {
1240 sysfs_remove_group(&mod->mkobj.kobj,
1241 &mod->sect_attrs->grp);
1242 /* We are positive that no one is using any sect attrs
1243 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001244 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 mod->sect_attrs = NULL;
1246 }
1247}
1248
Roland McGrath6d760132007-10-16 23:26:40 -07001249/*
1250 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1251 */
1252
1253struct module_notes_attrs {
1254 struct kobject *dir;
1255 unsigned int notes;
1256 struct bin_attribute attrs[0];
1257};
1258
Chris Wright2c3c8be2010-05-12 18:28:57 -07001259static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
Roland McGrath6d760132007-10-16 23:26:40 -07001260 struct bin_attribute *bin_attr,
1261 char *buf, loff_t pos, size_t count)
1262{
1263 /*
1264 * The caller checked the pos and count against our size.
1265 */
1266 memcpy(buf, bin_attr->private + pos, count);
1267 return count;
1268}
1269
1270static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1271 unsigned int i)
1272{
1273 if (notes_attrs->dir) {
1274 while (i-- > 0)
1275 sysfs_remove_bin_file(notes_attrs->dir,
1276 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001277 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001278 }
1279 kfree(notes_attrs);
1280}
1281
Rusty Russell8f6d0372010-08-05 12:59:09 -06001282static void add_notes_attrs(struct module *mod, const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001283{
1284 unsigned int notes, loaded, i;
1285 struct module_notes_attrs *notes_attrs;
1286 struct bin_attribute *nattr;
1287
Ingo Molnarea6bff32009-08-28 10:44:56 +02001288 /* failed to create section attributes, so can't create notes */
1289 if (!mod->sect_attrs)
1290 return;
1291
Roland McGrath6d760132007-10-16 23:26:40 -07001292 /* Count notes sections and allocate structures. */
1293 notes = 0;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001294 for (i = 0; i < info->hdr->e_shnum; i++)
1295 if (!sect_empty(&info->sechdrs[i]) &&
1296 (info->sechdrs[i].sh_type == SHT_NOTE))
Roland McGrath6d760132007-10-16 23:26:40 -07001297 ++notes;
1298
1299 if (notes == 0)
1300 return;
1301
1302 notes_attrs = kzalloc(sizeof(*notes_attrs)
1303 + notes * sizeof(notes_attrs->attrs[0]),
1304 GFP_KERNEL);
1305 if (notes_attrs == NULL)
1306 return;
1307
1308 notes_attrs->notes = notes;
1309 nattr = &notes_attrs->attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001310 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1311 if (sect_empty(&info->sechdrs[i]))
Roland McGrath6d760132007-10-16 23:26:40 -07001312 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001313 if (info->sechdrs[i].sh_type == SHT_NOTE) {
Eric W. Biederman361795b2010-02-12 13:41:56 -08001314 sysfs_bin_attr_init(nattr);
Roland McGrath6d760132007-10-16 23:26:40 -07001315 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1316 nattr->attr.mode = S_IRUGO;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001317 nattr->size = info->sechdrs[i].sh_size;
1318 nattr->private = (void *) info->sechdrs[i].sh_addr;
Roland McGrath6d760132007-10-16 23:26:40 -07001319 nattr->read = module_notes_read;
1320 ++nattr;
1321 }
1322 ++loaded;
1323 }
1324
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001325 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001326 if (!notes_attrs->dir)
1327 goto out;
1328
1329 for (i = 0; i < notes; ++i)
1330 if (sysfs_create_bin_file(notes_attrs->dir,
1331 &notes_attrs->attrs[i]))
1332 goto out;
1333
1334 mod->notes_attrs = notes_attrs;
1335 return;
1336
1337 out:
1338 free_notes_attrs(notes_attrs, i);
1339}
1340
1341static void remove_notes_attrs(struct module *mod)
1342{
1343 if (mod->notes_attrs)
1344 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1345}
1346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001348
Rusty Russell8f6d0372010-08-05 12:59:09 -06001349static inline void add_sect_attrs(struct module *mod,
1350 const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352}
1353
1354static inline void remove_sect_attrs(struct module *mod)
1355{
1356}
Roland McGrath6d760132007-10-16 23:26:40 -07001357
Rusty Russell8f6d0372010-08-05 12:59:09 -06001358static inline void add_notes_attrs(struct module *mod,
1359 const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001360{
1361}
1362
1363static inline void remove_notes_attrs(struct module *mod)
1364{
1365}
Rusty Russell8f6d0372010-08-05 12:59:09 -06001366#endif /* CONFIG_KALLSYMS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001368static void add_usage_links(struct module *mod)
1369{
1370#ifdef CONFIG_MODULE_UNLOAD
1371 struct module_use *use;
1372 int nowarn;
1373
Rusty Russell75676502010-06-05 11:17:36 -06001374 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001375 list_for_each_entry(use, &mod->target_list, target_list) {
1376 nowarn = sysfs_create_link(use->target->holders_dir,
1377 &mod->mkobj.kobj, mod->name);
1378 }
Rusty Russell75676502010-06-05 11:17:36 -06001379 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001380#endif
1381}
1382
1383static void del_usage_links(struct module *mod)
1384{
1385#ifdef CONFIG_MODULE_UNLOAD
1386 struct module_use *use;
1387
Rusty Russell75676502010-06-05 11:17:36 -06001388 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001389 list_for_each_entry(use, &mod->target_list, target_list)
1390 sysfs_remove_link(use->target->holders_dir, mod->name);
Rusty Russell75676502010-06-05 11:17:36 -06001391 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001392#endif
1393}
1394
Rusty Russell6407ebb22010-06-05 11:17:36 -06001395static int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001396{
1397 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001398 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001399 int error = 0;
1400 int i;
1401
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001402 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1403 (ARRAY_SIZE(modinfo_attrs) + 1)),
1404 GFP_KERNEL);
1405 if (!mod->modinfo_attrs)
1406 return -ENOMEM;
1407
1408 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001409 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1410 if (!attr->test ||
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001411 (attr->test && attr->test(mod))) {
1412 memcpy(temp_attr, attr, sizeof(*temp_attr));
Eric W. Biederman361795b2010-02-12 13:41:56 -08001413 sysfs_attr_init(&temp_attr->attr);
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001414 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1415 ++temp_attr;
1416 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001417 }
1418 return error;
1419}
1420
Rusty Russell6407ebb22010-06-05 11:17:36 -06001421static void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001422{
1423 struct module_attribute *attr;
1424 int i;
1425
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001426 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1427 /* pick a field to test for end of list */
1428 if (!attr->attr.name)
1429 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001430 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001431 if (attr->free)
1432 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001433 }
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001434 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001435}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Rusty Russell6407ebb22010-06-05 11:17:36 -06001437static int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
1439 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001440 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001442 if (!module_sysfs_initialized) {
1443 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001444 mod->name);
1445 err = -EINVAL;
1446 goto out;
1447 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001448
1449 kobj = kset_find_obj(module_kset, mod->name);
1450 if (kobj) {
1451 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1452 kobject_put(kobj);
1453 err = -EINVAL;
1454 goto out;
1455 }
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001458
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001459 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1460 mod->mkobj.kobj.kset = module_kset;
1461 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1462 "%s", mod->name);
1463 if (err)
1464 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001465
Kay Sievers97c146e2007-11-29 23:46:11 +01001466 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001467out:
1468 return err;
1469}
1470
Rusty Russell6407ebb22010-06-05 11:17:36 -06001471static int mod_sysfs_setup(struct module *mod,
Rusty Russell8f6d0372010-08-05 12:59:09 -06001472 const struct load_info *info,
Kay Sievers270a6c42007-01-18 13:26:15 +01001473 struct kernel_param *kparam,
1474 unsigned int num_params)
1475{
1476 int err;
1477
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001478 err = mod_sysfs_init(mod);
1479 if (err)
1480 goto out;
1481
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001482 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001483 if (!mod->holders_dir) {
1484 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001485 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001486 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 err = module_param_sysfs_setup(mod, kparam, num_params);
1489 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001490 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Matt Domschc988d2b2005-06-23 22:05:15 -07001492 err = module_add_modinfo_attrs(mod);
1493 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001494 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001495
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001496 add_usage_links(mod);
Rusty Russell8f6d0372010-08-05 12:59:09 -06001497 add_sect_attrs(mod, info);
1498 add_notes_attrs(mod, info);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001499
Kay Sieverse17e0f52006-11-24 12:15:25 +01001500 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return 0;
1502
Kay Sieverse17e0f52006-11-24 12:15:25 +01001503out_unreg_param:
1504 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001505out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001506 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001507out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001508 kobject_put(&mod->mkobj.kobj);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001509out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 return err;
1511}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001512
1513static void mod_sysfs_fini(struct module *mod)
1514{
Rusty Russell8f6d0372010-08-05 12:59:09 -06001515 remove_notes_attrs(mod);
1516 remove_sect_attrs(mod);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001517 kobject_put(&mod->mkobj.kobj);
1518}
1519
Rusty Russell8f6d0372010-08-05 12:59:09 -06001520#else /* !CONFIG_SYSFS */
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001521
Rusty Russell8f6d0372010-08-05 12:59:09 -06001522static int mod_sysfs_setup(struct module *mod,
1523 const struct load_info *info,
Rusty Russell6407ebb22010-06-05 11:17:36 -06001524 struct kernel_param *kparam,
1525 unsigned int num_params)
1526{
1527 return 0;
1528}
1529
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001530static void mod_sysfs_fini(struct module *mod)
1531{
1532}
1533
Rusty Russell36b03602010-08-05 12:59:09 -06001534static void module_remove_modinfo_attrs(struct module *mod)
1535{
1536}
1537
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001538static void del_usage_links(struct module *mod)
1539{
1540}
1541
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001542#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Rusty Russell36b03602010-08-05 12:59:09 -06001544static void mod_sysfs_teardown(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001546 del_usage_links(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001547 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001549 kobject_put(mod->mkobj.drivers_dir);
1550 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001551 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552}
1553
1554/*
1555 * unlink the module with the whole machine is stopped with interrupts off
1556 * - this defends against kallsyms not taking locks
1557 */
1558static int __unlink_module(void *_mod)
1559{
1560 struct module *mod = _mod;
1561 list_del(&mod->list);
Linus Torvalds53363772010-10-05 11:29:27 -07001562 module_bug_cleanup(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 return 0;
1564}
1565
matthieu castet84e1c6b2010-11-16 22:35:16 +01001566#ifdef CONFIG_DEBUG_SET_MODULE_RONX
1567/*
1568 * LKM RO/NX protection: protect module's text/ro-data
1569 * from modification and any data from execution.
1570 */
1571void set_page_attributes(void *start, void *end, int (*set)(unsigned long start, int num_pages))
1572{
1573 unsigned long begin_pfn = PFN_DOWN((unsigned long)start);
1574 unsigned long end_pfn = PFN_DOWN((unsigned long)end);
1575
1576 if (end_pfn > begin_pfn)
1577 set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1578}
1579
1580static void set_section_ro_nx(void *base,
1581 unsigned long text_size,
1582 unsigned long ro_size,
1583 unsigned long total_size)
1584{
1585 /* begin and end PFNs of the current subsection */
1586 unsigned long begin_pfn;
1587 unsigned long end_pfn;
1588
1589 /*
1590 * Set RO for module text and RO-data:
1591 * - Always protect first page.
1592 * - Do not protect last partial page.
1593 */
1594 if (ro_size > 0)
1595 set_page_attributes(base, base + ro_size, set_memory_ro);
1596
1597 /*
1598 * Set NX permissions for module data:
1599 * - Do not protect first partial page.
1600 * - Always protect last page.
1601 */
1602 if (total_size > text_size) {
1603 begin_pfn = PFN_UP((unsigned long)base + text_size);
1604 end_pfn = PFN_UP((unsigned long)base + total_size);
1605 if (end_pfn > begin_pfn)
1606 set_memory_nx(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1607 }
1608}
1609
Jan Glauber448694a2011-05-19 16:55:26 -06001610/* Setting memory back to W+X before releasing it */
matthieu castet84e1c6b2010-11-16 22:35:16 +01001611void unset_section_ro_nx(struct module *mod, void *module_region)
1612{
matthieu castet84e1c6b2010-11-16 22:35:16 +01001613 if (mod->module_core == module_region) {
Jan Glauber448694a2011-05-19 16:55:26 -06001614 set_page_attributes(mod->module_core + mod->core_text_size,
1615 mod->module_core + mod->core_size,
1616 set_memory_x);
1617 set_page_attributes(mod->module_core,
1618 mod->module_core + mod->core_ro_size,
1619 set_memory_rw);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001620 } else if (mod->module_init == module_region) {
Jan Glauber448694a2011-05-19 16:55:26 -06001621 set_page_attributes(mod->module_init + mod->init_text_size,
1622 mod->module_init + mod->init_size,
1623 set_memory_x);
1624 set_page_attributes(mod->module_init,
1625 mod->module_init + mod->init_ro_size,
1626 set_memory_rw);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001627 }
1628}
1629
1630/* Iterate through all modules and set each module's text as RW */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001631void set_all_modules_text_rw(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001632{
1633 struct module *mod;
1634
1635 mutex_lock(&module_mutex);
1636 list_for_each_entry_rcu(mod, &modules, list) {
1637 if ((mod->module_core) && (mod->core_text_size)) {
1638 set_page_attributes(mod->module_core,
1639 mod->module_core + mod->core_text_size,
1640 set_memory_rw);
1641 }
1642 if ((mod->module_init) && (mod->init_text_size)) {
1643 set_page_attributes(mod->module_init,
1644 mod->module_init + mod->init_text_size,
1645 set_memory_rw);
1646 }
1647 }
1648 mutex_unlock(&module_mutex);
1649}
1650
1651/* Iterate through all modules and set each module's text as RO */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001652void set_all_modules_text_ro(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001653{
1654 struct module *mod;
1655
1656 mutex_lock(&module_mutex);
1657 list_for_each_entry_rcu(mod, &modules, list) {
1658 if ((mod->module_core) && (mod->core_text_size)) {
1659 set_page_attributes(mod->module_core,
1660 mod->module_core + mod->core_text_size,
1661 set_memory_ro);
1662 }
1663 if ((mod->module_init) && (mod->init_text_size)) {
1664 set_page_attributes(mod->module_init,
1665 mod->module_init + mod->init_text_size,
1666 set_memory_ro);
1667 }
1668 }
1669 mutex_unlock(&module_mutex);
1670}
1671#else
1672static inline void set_section_ro_nx(void *base, unsigned long text_size, unsigned long ro_size, unsigned long total_size) { }
1673static inline void unset_section_ro_nx(struct module *mod, void *module_region) { }
1674#endif
1675
Rusty Russell75676502010-06-05 11:17:36 -06001676/* Free a module, remove from lists, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677static void free_module(struct module *mod)
1678{
Li Zefan7ead8b82009-08-17 16:56:28 +08001679 trace_module_free(mod);
1680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 /* Delete from various lists */
Rusty Russell75676502010-06-05 11:17:36 -06001682 mutex_lock(&module_mutex);
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001683 stop_machine(__unlink_module, mod, NULL);
Rusty Russell75676502010-06-05 11:17:36 -06001684 mutex_unlock(&module_mutex);
Rusty Russell36b03602010-08-05 12:59:09 -06001685 mod_sysfs_teardown(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Jason Baronb82bab42010-07-27 13:18:01 -07001687 /* Remove dynamic debug info */
1688 ddebug_remove_module(mod->name);
1689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 /* Arch-specific cleanup. */
1691 module_arch_cleanup(mod);
1692
1693 /* Module unload stuff */
1694 module_unload_free(mod);
1695
Rusty Russelle180a6b2009-03-31 13:05:29 -06001696 /* Free any allocated parameters. */
1697 destroy_params(mod->kp, mod->num_kp);
1698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 /* This may be NULL, but that's OK */
matthieu castet84e1c6b2010-11-16 22:35:16 +01001700 unset_section_ro_nx(mod, mod->module_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 module_free(mod, mod->module_init);
1702 kfree(mod->args);
Tejun Heo259354d2010-03-10 18:56:10 +09001703 percpu_modfree(mod);
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001704
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001705 /* Free lock-classes: */
1706 lockdep_free_key_range(mod->module_core, mod->core_size);
1707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 /* Finally, free the core (containing the module structure) */
matthieu castet84e1c6b2010-11-16 22:35:16 +01001709 unset_section_ro_nx(mod, mod->module_core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 module_free(mod, mod->module_core);
Bernd Schmidteb8cdec2009-09-21 17:03:57 -07001711
1712#ifdef CONFIG_MPU
1713 update_protections(current->mm);
1714#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715}
1716
1717void *__symbol_get(const char *symbol)
1718{
1719 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001720 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Rusty Russell24da1cb2007-07-15 23:41:46 -07001722 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001723 sym = find_symbol(symbol, &owner, NULL, true, true);
1724 if (sym && strong_try_module_get(owner))
1725 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001726 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Tim Abbott414fd312008-12-05 19:03:56 -05001728 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729}
1730EXPORT_SYMBOL_GPL(__symbol_get);
1731
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001732/*
1733 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001734 * in the kernel or in some other module's exported symbol table.
Rusty Russellbe593f42010-06-05 11:17:37 -06001735 *
1736 * You must hold the module_mutex.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001737 */
1738static int verify_export_symbols(struct module *mod)
1739{
Rusty Russellb2111042008-05-01 21:15:00 -05001740 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001741 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001742 const struct kernel_symbol *s;
1743 struct {
1744 const struct kernel_symbol *sym;
1745 unsigned int num;
1746 } arr[] = {
1747 { mod->syms, mod->num_syms },
1748 { mod->gpl_syms, mod->num_gpl_syms },
1749 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001750#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001751 { mod->unused_syms, mod->num_unused_syms },
1752 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001753#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001754 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001755
Rusty Russellb2111042008-05-01 21:15:00 -05001756 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1757 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Rusty Russellbe593f42010-06-05 11:17:37 -06001758 if (find_symbol(s->name, &owner, NULL, true, false)) {
Rusty Russellb2111042008-05-01 21:15:00 -05001759 printk(KERN_ERR
1760 "%s: exports duplicate symbol %s"
1761 " (owned by %s)\n",
1762 mod->name, s->name, module_name(owner));
1763 return -ENOEXEC;
1764 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001765 }
Rusty Russellb2111042008-05-01 21:15:00 -05001766 }
1767 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001768}
1769
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001770/* Change all symbols so that st_value encodes the pointer directly. */
Rusty Russell49668682010-08-05 12:59:10 -06001771static int simplify_symbols(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
Rusty Russell49668682010-08-05 12:59:10 -06001773 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
1774 Elf_Sym *sym = (void *)symsec->sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 unsigned long secbase;
Rusty Russell49668682010-08-05 12:59:10 -06001776 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05001778 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Rusty Russell49668682010-08-05 12:59:10 -06001780 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
1781 const char *name = info->strtab + sym[i].st_name;
1782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 switch (sym[i].st_shndx) {
1784 case SHN_COMMON:
1785 /* We compiled with -fno-common. These are not
1786 supposed to happen. */
Rusty Russell49668682010-08-05 12:59:10 -06001787 DEBUGP("Common symbol: %s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 printk("%s: please compile with -fno-common\n",
1789 mod->name);
1790 ret = -ENOEXEC;
1791 break;
1792
1793 case SHN_ABS:
1794 /* Don't need to do anything */
1795 DEBUGP("Absolute symbol: 0x%08lx\n",
1796 (long)sym[i].st_value);
1797 break;
1798
1799 case SHN_UNDEF:
Rusty Russell49668682010-08-05 12:59:10 -06001800 ksym = resolve_symbol_wait(mod, info, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 /* Ok if resolved. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001802 if (ksym && !IS_ERR(ksym)) {
Tim Abbott414fd312008-12-05 19:03:56 -05001803 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 break;
Tim Abbott414fd312008-12-05 19:03:56 -05001805 }
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 /* Ok if weak. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001808 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 break;
1810
Rusty Russell9bea7f22010-06-05 11:17:37 -06001811 printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
Rusty Russell49668682010-08-05 12:59:10 -06001812 mod->name, name, PTR_ERR(ksym));
Rusty Russell9bea7f22010-06-05 11:17:37 -06001813 ret = PTR_ERR(ksym) ?: -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 break;
1815
1816 default:
1817 /* Divert to percpu allocation if a percpu var. */
Rusty Russell49668682010-08-05 12:59:10 -06001818 if (sym[i].st_shndx == info->index.pcpu)
Tejun Heo259354d2010-03-10 18:56:10 +09001819 secbase = (unsigned long)mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 else
Rusty Russell49668682010-08-05 12:59:10 -06001821 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 sym[i].st_value += secbase;
1823 break;
1824 }
1825 }
1826
1827 return ret;
1828}
1829
Rusty Russell49668682010-08-05 12:59:10 -06001830static int apply_relocations(struct module *mod, const struct load_info *info)
Rusty Russell22e268e2010-08-05 12:59:05 -06001831{
1832 unsigned int i;
1833 int err = 0;
1834
1835 /* Now do relocations. */
Rusty Russell49668682010-08-05 12:59:10 -06001836 for (i = 1; i < info->hdr->e_shnum; i++) {
1837 unsigned int infosec = info->sechdrs[i].sh_info;
Rusty Russell22e268e2010-08-05 12:59:05 -06001838
1839 /* Not a valid relocation section? */
Rusty Russell49668682010-08-05 12:59:10 -06001840 if (infosec >= info->hdr->e_shnum)
Rusty Russell22e268e2010-08-05 12:59:05 -06001841 continue;
1842
1843 /* Don't bother with non-allocated sections */
Rusty Russell49668682010-08-05 12:59:10 -06001844 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
Rusty Russell22e268e2010-08-05 12:59:05 -06001845 continue;
1846
Rusty Russell49668682010-08-05 12:59:10 -06001847 if (info->sechdrs[i].sh_type == SHT_REL)
1848 err = apply_relocate(info->sechdrs, info->strtab,
1849 info->index.sym, i, mod);
1850 else if (info->sechdrs[i].sh_type == SHT_RELA)
1851 err = apply_relocate_add(info->sechdrs, info->strtab,
1852 info->index.sym, i, mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06001853 if (err < 0)
1854 break;
1855 }
1856 return err;
1857}
1858
Helge Deller088af9a2008-12-31 12:31:18 +01001859/* Additional bytes needed by arch in front of individual sections */
1860unsigned int __weak arch_mod_section_prepend(struct module *mod,
1861 unsigned int section)
1862{
1863 /* default implementation just returns zero */
1864 return 0;
1865}
1866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01001868static long get_offset(struct module *mod, unsigned int *size,
1869 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870{
1871 long ret;
1872
Helge Deller088af9a2008-12-31 12:31:18 +01001873 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1875 *size = ret + sechdr->sh_size;
1876 return ret;
1877}
1878
1879/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1880 might -- code, read-only data, read-write data, small data. Tally
1881 sizes, and place the offsets into sh_entsize fields: high bit means it
1882 belongs in init. */
Rusty Russell49668682010-08-05 12:59:10 -06001883static void layout_sections(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
1885 static unsigned long const masks[][2] = {
1886 /* NOTE: all executable code must be the first section
1887 * in this array; otherwise modify the text_size
1888 * finder in the two loops below */
1889 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1890 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1891 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1892 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1893 };
1894 unsigned int m, i;
1895
Rusty Russell49668682010-08-05 12:59:10 -06001896 for (i = 0; i < info->hdr->e_shnum; i++)
1897 info->sechdrs[i].sh_entsize = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899 DEBUGP("Core section allocation order:\n");
1900 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06001901 for (i = 0; i < info->hdr->e_shnum; ++i) {
1902 Elf_Shdr *s = &info->sechdrs[i];
1903 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
1905 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1906 || (s->sh_flags & masks[m][1])
1907 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06001908 || strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001910 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Rusty Russell49668682010-08-05 12:59:10 -06001911 DEBUGP("\t%s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01001913 switch (m) {
1914 case 0: /* executable */
1915 mod->core_size = debug_align(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 mod->core_text_size = mod->core_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01001917 break;
1918 case 1: /* RO: text and ro-data */
1919 mod->core_size = debug_align(mod->core_size);
1920 mod->core_ro_size = mod->core_size;
1921 break;
1922 case 3: /* whole core */
1923 mod->core_size = debug_align(mod->core_size);
1924 break;
1925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 }
1927
1928 DEBUGP("Init section allocation order:\n");
1929 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06001930 for (i = 0; i < info->hdr->e_shnum; ++i) {
1931 Elf_Shdr *s = &info->sechdrs[i];
1932 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1935 || (s->sh_flags & masks[m][1])
1936 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06001937 || !strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001939 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 | INIT_OFFSET_MASK);
Rusty Russell49668682010-08-05 12:59:10 -06001941 DEBUGP("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01001943 switch (m) {
1944 case 0: /* executable */
1945 mod->init_size = debug_align(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 mod->init_text_size = mod->init_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01001947 break;
1948 case 1: /* RO: text and ro-data */
1949 mod->init_size = debug_align(mod->init_size);
1950 mod->init_ro_size = mod->init_size;
1951 break;
1952 case 3: /* whole init */
1953 mod->init_size = debug_align(mod->init_size);
1954 break;
1955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 }
1957}
1958
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959static void set_license(struct module *mod, const char *license)
1960{
1961 if (!license)
1962 license = "unspecified";
1963
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001964 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07001965 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001966 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001967 "kernel.\n", mod->name, license);
1968 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 }
1970}
1971
1972/* Parse tag=value strings from .modinfo section */
1973static char *next_string(char *string, unsigned long *secsize)
1974{
1975 /* Skip non-zero chars */
1976 while (string[0]) {
1977 string++;
1978 if ((*secsize)-- <= 1)
1979 return NULL;
1980 }
1981
1982 /* Skip any zero padding. */
1983 while (!string[0]) {
1984 string++;
1985 if ((*secsize)-- <= 1)
1986 return NULL;
1987 }
1988 return string;
1989}
1990
Rusty Russell49668682010-08-05 12:59:10 -06001991static char *get_modinfo(struct load_info *info, const char *tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
1993 char *p;
1994 unsigned int taglen = strlen(tag);
Rusty Russell49668682010-08-05 12:59:10 -06001995 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
1996 unsigned long size = infosec->sh_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Rusty Russell49668682010-08-05 12:59:10 -06001998 for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2000 return p + taglen + 1;
2001 }
2002 return NULL;
2003}
2004
Rusty Russell49668682010-08-05 12:59:10 -06002005static void setup_modinfo(struct module *mod, struct load_info *info)
Matt Domschc988d2b2005-06-23 22:05:15 -07002006{
2007 struct module_attribute *attr;
2008 int i;
2009
2010 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2011 if (attr->setup)
Rusty Russell49668682010-08-05 12:59:10 -06002012 attr->setup(mod, get_modinfo(info, attr->attr.name));
Matt Domschc988d2b2005-06-23 22:05:15 -07002013 }
2014}
Matt Domschc988d2b2005-06-23 22:05:15 -07002015
Rusty Russella263f772009-09-25 00:32:58 -06002016static void free_modinfo(struct module *mod)
2017{
2018 struct module_attribute *attr;
2019 int i;
2020
2021 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2022 if (attr->free)
2023 attr->free(mod);
2024 }
2025}
2026
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01002028
2029/* lookup symbol in given range of kernel_symbols */
2030static const struct kernel_symbol *lookup_symbol(const char *name,
2031 const struct kernel_symbol *start,
2032 const struct kernel_symbol *stop)
2033{
2034 const struct kernel_symbol *ks = start;
2035 for (; ks < stop; ks++)
2036 if (strcmp(ks->name, name) == 0)
2037 return ks;
2038 return NULL;
2039}
2040
Tim Abbottca4787b2009-01-05 08:40:10 -06002041static int is_exported(const char *name, unsigned long value,
2042 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Tim Abbottca4787b2009-01-05 08:40:10 -06002044 const struct kernel_symbol *ks;
2045 if (!mod)
2046 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01002047 else
Tim Abbottca4787b2009-01-05 08:40:10 -06002048 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
2049 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050}
2051
2052/* As per nm */
Rusty Russelleded41c2010-08-05 12:59:07 -06002053static char elf_type(const Elf_Sym *sym, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
Rusty Russelleded41c2010-08-05 12:59:07 -06002055 const Elf_Shdr *sechdrs = info->sechdrs;
2056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2058 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2059 return 'v';
2060 else
2061 return 'w';
2062 }
2063 if (sym->st_shndx == SHN_UNDEF)
2064 return 'U';
2065 if (sym->st_shndx == SHN_ABS)
2066 return 'a';
2067 if (sym->st_shndx >= SHN_LORESERVE)
2068 return '?';
2069 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2070 return 't';
2071 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2072 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2073 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2074 return 'r';
2075 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2076 return 'g';
2077 else
2078 return 'd';
2079 }
2080 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2081 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2082 return 's';
2083 else
2084 return 'b';
2085 }
Rusty Russelleded41c2010-08-05 12:59:07 -06002086 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2087 ".debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 return 'n';
Rusty Russelleded41c2010-08-05 12:59:07 -06002089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 return '?';
2091}
2092
Jan Beulich4a496222009-07-06 14:50:42 +01002093static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
2094 unsigned int shnum)
2095{
2096 const Elf_Shdr *sec;
2097
2098 if (src->st_shndx == SHN_UNDEF
2099 || src->st_shndx >= shnum
2100 || !src->st_name)
2101 return false;
2102
2103 sec = sechdrs + src->st_shndx;
2104 if (!(sec->sh_flags & SHF_ALLOC)
2105#ifndef CONFIG_KALLSYMS_ALL
2106 || !(sec->sh_flags & SHF_EXECINSTR)
2107#endif
2108 || (sec->sh_entsize & INIT_OFFSET_MASK))
2109 return false;
2110
2111 return true;
2112}
2113
Rusty Russell49668682010-08-05 12:59:10 -06002114static void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002115{
Rusty Russell49668682010-08-05 12:59:10 -06002116 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2117 Elf_Shdr *strsect = info->sechdrs + info->index.str;
Jan Beulich4a496222009-07-06 14:50:42 +01002118 const Elf_Sym *src;
2119 unsigned int i, nsrc, ndst;
2120
2121 /* Put symbol section at end of init part of module. */
2122 symsect->sh_flags |= SHF_ALLOC;
2123 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
Rusty Russell49668682010-08-05 12:59:10 -06002124 info->index.sym) | INIT_OFFSET_MASK;
2125 DEBUGP("\t%s\n", info->secstrings + symsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002126
Rusty Russell49668682010-08-05 12:59:10 -06002127 src = (void *)info->hdr + symsect->sh_offset;
Jan Beulich4a496222009-07-06 14:50:42 +01002128 nsrc = symsect->sh_size / sizeof(*src);
2129 for (ndst = i = 1; i < nsrc; ++i, ++src)
Rusty Russell49668682010-08-05 12:59:10 -06002130 if (is_core_symbol(src, info->sechdrs, info->hdr->e_shnum)) {
Jan Beulich554bdfe2009-07-06 14:51:44 +01002131 unsigned int j = src->st_name;
2132
Rusty Russell49668682010-08-05 12:59:10 -06002133 while (!__test_and_set_bit(j, info->strmap)
2134 && info->strtab[j])
Jan Beulich554bdfe2009-07-06 14:51:44 +01002135 ++j;
Jan Beulich4a496222009-07-06 14:50:42 +01002136 ++ndst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002137 }
Jan Beulich4a496222009-07-06 14:50:42 +01002138
2139 /* Append room for core symbols at end of core part. */
Rusty Russell49668682010-08-05 12:59:10 -06002140 info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
2141 mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
Jan Beulich4a496222009-07-06 14:50:42 +01002142
Jan Beulich554bdfe2009-07-06 14:51:44 +01002143 /* Put string table section at end of init part of module. */
2144 strsect->sh_flags |= SHF_ALLOC;
2145 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
Rusty Russell49668682010-08-05 12:59:10 -06002146 info->index.str) | INIT_OFFSET_MASK;
2147 DEBUGP("\t%s\n", info->secstrings + strsect->sh_name);
Jan Beulich554bdfe2009-07-06 14:51:44 +01002148
2149 /* Append room for core symbols' strings at end of core part. */
Rusty Russell49668682010-08-05 12:59:10 -06002150 info->stroffs = mod->core_size;
2151 __set_bit(0, info->strmap);
2152 mod->core_size += bitmap_weight(info->strmap, strsect->sh_size);
Jan Beulich4a496222009-07-06 14:50:42 +01002153}
2154
Rusty Russell811d66a2010-08-05 12:59:12 -06002155static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
Jan Beulich4a496222009-07-06 14:50:42 +01002157 unsigned int i, ndst;
2158 const Elf_Sym *src;
2159 Elf_Sym *dst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002160 char *s;
Rusty Russelleded41c2010-08-05 12:59:07 -06002161 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Rusty Russelleded41c2010-08-05 12:59:07 -06002163 mod->symtab = (void *)symsec->sh_addr;
2164 mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
Rusty Russell511ca6a2010-08-05 12:59:08 -06002165 /* Make sure we get permanent strtab: don't use info->strtab. */
2166 mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
2168 /* Set types up while we still have access to sections. */
2169 for (i = 0; i < mod->num_symtab; i++)
Rusty Russelleded41c2010-08-05 12:59:07 -06002170 mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
Jan Beulich4a496222009-07-06 14:50:42 +01002171
Rusty Russelld9131882010-08-05 12:59:08 -06002172 mod->core_symtab = dst = mod->module_core + info->symoffs;
Jan Beulich4a496222009-07-06 14:50:42 +01002173 src = mod->symtab;
2174 *dst = *src;
2175 for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
Rusty Russelleded41c2010-08-05 12:59:07 -06002176 if (!is_core_symbol(src, info->sechdrs, info->hdr->e_shnum))
Jan Beulich4a496222009-07-06 14:50:42 +01002177 continue;
2178 dst[ndst] = *src;
Rusty Russelld9131882010-08-05 12:59:08 -06002179 dst[ndst].st_name = bitmap_weight(info->strmap,
2180 dst[ndst].st_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002181 ++ndst;
2182 }
2183 mod->core_num_syms = ndst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002184
Rusty Russelld9131882010-08-05 12:59:08 -06002185 mod->core_strtab = s = mod->module_core + info->stroffs;
Rusty Russelleded41c2010-08-05 12:59:07 -06002186 for (*s = 0, i = 1; i < info->sechdrs[info->index.str].sh_size; ++i)
Rusty Russelld9131882010-08-05 12:59:08 -06002187 if (test_bit(i, info->strmap))
Jan Beulich554bdfe2009-07-06 14:51:44 +01002188 *++s = mod->strtab[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189}
2190#else
Rusty Russell49668682010-08-05 12:59:10 -06002191static inline void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002192{
2193}
Paul Mundt3ae91c22009-10-01 15:43:54 -07002194
Michał Mirosławabbce902010-09-20 01:58:08 +02002195static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
2197}
2198#endif /* CONFIG_KALLSYMS */
2199
Jason Barone9d376f2009-02-05 11:51:38 -05002200static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05002201{
Rusty Russell811d66a2010-08-05 12:59:12 -06002202 if (!debug)
2203 return;
Jason Barone9d376f2009-02-05 11:51:38 -05002204#ifdef CONFIG_DYNAMIC_DEBUG
2205 if (ddebug_add_module(debug, num, debug->modname))
2206 printk(KERN_ERR "dynamic debug error adding module: %s\n",
2207 debug->modname);
2208#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002209}
Jason Baron346e15b2008-08-12 16:46:19 -04002210
Yehuda Sadehff49d742010-07-03 13:07:35 +10002211static void dynamic_debug_remove(struct _ddebug *debug)
2212{
2213 if (debug)
2214 ddebug_remove_module(debug->modname);
2215}
2216
Rusty Russell3a642e92008-07-22 19:24:28 -05002217static void *module_alloc_update_bounds(unsigned long size)
2218{
2219 void *ret = module_alloc(size);
2220
2221 if (ret) {
Rusty Russell75676502010-06-05 11:17:36 -06002222 mutex_lock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002223 /* Update module bounds. */
2224 if ((unsigned long)ret < module_addr_min)
2225 module_addr_min = (unsigned long)ret;
2226 if ((unsigned long)ret + size > module_addr_max)
2227 module_addr_max = (unsigned long)ret + size;
Rusty Russell75676502010-06-05 11:17:36 -06002228 mutex_unlock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002229 }
2230 return ret;
2231}
2232
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002233#ifdef CONFIG_DEBUG_KMEMLEAK
Rusty Russell49668682010-08-05 12:59:10 -06002234static void kmemleak_load_module(const struct module *mod,
2235 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002236{
2237 unsigned int i;
2238
2239 /* only scan the sections containing data */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002240 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002241
Rusty Russell49668682010-08-05 12:59:10 -06002242 for (i = 1; i < info->hdr->e_shnum; i++) {
2243 const char *name = info->secstrings + info->sechdrs[i].sh_name;
2244 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002245 continue;
Rusty Russell49668682010-08-05 12:59:10 -06002246 if (!strstarts(name, ".data") && !strstarts(name, ".bss"))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002247 continue;
2248
Rusty Russell49668682010-08-05 12:59:10 -06002249 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2250 info->sechdrs[i].sh_size, GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002251 }
2252}
2253#else
Rusty Russell49668682010-08-05 12:59:10 -06002254static inline void kmemleak_load_module(const struct module *mod,
2255 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002256{
2257}
2258#endif
2259
Rusty Russell6526c532010-08-05 12:59:10 -06002260/* Sets info->hdr and info->len. */
Rusty Russelld9131882010-08-05 12:59:08 -06002261static int copy_and_check(struct load_info *info,
2262 const void __user *umod, unsigned long len,
2263 const char __user *uargs)
Rusty Russell40dd2562010-08-05 12:59:03 -06002264{
2265 int err;
2266 Elf_Ehdr *hdr;
2267
2268 if (len < sizeof(*hdr))
2269 return -ENOEXEC;
2270
2271 /* Suck in entire file: we'll want most of it. */
2272 /* vmalloc barfs on "unusual" numbers. Check here */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002273 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
Rusty Russell40dd2562010-08-05 12:59:03 -06002274 return -ENOMEM;
2275
2276 if (copy_from_user(hdr, umod, len) != 0) {
2277 err = -EFAULT;
2278 goto free_hdr;
2279 }
2280
2281 /* Sanity checks against insmoding binaries or wrong arch,
2282 weird elf version */
2283 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
2284 || hdr->e_type != ET_REL
2285 || !elf_check_arch(hdr)
2286 || hdr->e_shentsize != sizeof(Elf_Shdr)) {
2287 err = -ENOEXEC;
2288 goto free_hdr;
2289 }
2290
2291 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) {
2292 err = -ENOEXEC;
2293 goto free_hdr;
2294 }
Rusty Russelld9131882010-08-05 12:59:08 -06002295
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002296 info->hdr = hdr;
2297 info->len = len;
Rusty Russell40dd2562010-08-05 12:59:03 -06002298 return 0;
2299
2300free_hdr:
2301 vfree(hdr);
2302 return err;
2303}
2304
Rusty Russelld9131882010-08-05 12:59:08 -06002305static void free_copy(struct load_info *info)
2306{
Rusty Russelld9131882010-08-05 12:59:08 -06002307 vfree(info->hdr);
2308}
2309
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002310static int rewrite_section_headers(struct load_info *info)
2311{
2312 unsigned int i;
2313
2314 /* This should always be true, but let's be sure. */
2315 info->sechdrs[0].sh_addr = 0;
2316
2317 for (i = 1; i < info->hdr->e_shnum; i++) {
2318 Elf_Shdr *shdr = &info->sechdrs[i];
2319 if (shdr->sh_type != SHT_NOBITS
2320 && info->len < shdr->sh_offset + shdr->sh_size) {
2321 printk(KERN_ERR "Module len %lu truncated\n",
2322 info->len);
2323 return -ENOEXEC;
2324 }
2325
2326 /* Mark all sections sh_addr with their address in the
2327 temporary image. */
2328 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2329
2330#ifndef CONFIG_MODULE_UNLOAD
2331 /* Don't load .exit sections */
2332 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2333 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2334#endif
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002335 }
Rusty Russelld6df72a2010-08-05 12:59:07 -06002336
2337 /* Track but don't keep modinfo and version sections. */
Rusty Russell49668682010-08-05 12:59:10 -06002338 info->index.vers = find_sec(info, "__versions");
2339 info->index.info = find_sec(info, ".modinfo");
Rusty Russelld6df72a2010-08-05 12:59:07 -06002340 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2341 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002342 return 0;
2343}
2344
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002345/*
2346 * Set up our basic convenience variables (pointers to section headers,
2347 * search for module section index etc), and do some basic section
2348 * verification.
2349 *
2350 * Return the temporary module pointer (we'll replace it with the final
2351 * one when we move the module sections around).
2352 */
2353static struct module *setup_load_info(struct load_info *info)
2354{
2355 unsigned int i;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002356 int err;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002357 struct module *mod;
2358
2359 /* Set up the convenience variables */
2360 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002361 info->secstrings = (void *)info->hdr
2362 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002363
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002364 err = rewrite_section_headers(info);
2365 if (err)
2366 return ERR_PTR(err);
2367
2368 /* Find internal symbols and strings. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002369 for (i = 1; i < info->hdr->e_shnum; i++) {
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002370 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2371 info->index.sym = i;
2372 info->index.str = info->sechdrs[i].sh_link;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002373 info->strtab = (char *)info->hdr
2374 + info->sechdrs[info->index.str].sh_offset;
2375 break;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002376 }
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002377 }
2378
Rusty Russell49668682010-08-05 12:59:10 -06002379 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002380 if (!info->index.mod) {
2381 printk(KERN_WARNING "No module found in object\n");
2382 return ERR_PTR(-ENOEXEC);
2383 }
2384 /* This is temporary: point mod into copy of data. */
2385 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2386
2387 if (info->index.sym == 0) {
2388 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2389 mod->name);
2390 return ERR_PTR(-ENOEXEC);
2391 }
2392
Rusty Russell49668682010-08-05 12:59:10 -06002393 info->index.pcpu = find_pcpusec(info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002394
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002395 /* Check module struct version now, before we try to use module. */
2396 if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2397 return ERR_PTR(-ENOEXEC);
2398
2399 return mod;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002400}
2401
Rusty Russell49668682010-08-05 12:59:10 -06002402static int check_modinfo(struct module *mod, struct load_info *info)
Rusty Russell40dd2562010-08-05 12:59:03 -06002403{
Rusty Russell49668682010-08-05 12:59:10 -06002404 const char *modmagic = get_modinfo(info, "vermagic");
Rusty Russell40dd2562010-08-05 12:59:03 -06002405 int err;
2406
2407 /* This is allowed: modprobe --force will invalidate it. */
2408 if (!modmagic) {
2409 err = try_to_force_load(mod, "bad vermagic");
2410 if (err)
2411 return err;
Rusty Russell49668682010-08-05 12:59:10 -06002412 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002413 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2414 mod->name, modmagic, vermagic);
2415 return -ENOEXEC;
2416 }
2417
Rusty Russell49668682010-08-05 12:59:10 -06002418 if (get_modinfo(info, "staging")) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002419 add_taint_module(mod, TAINT_CRAP);
2420 printk(KERN_WARNING "%s: module is from the staging directory,"
2421 " the quality is unknown, you have been warned.\n",
2422 mod->name);
2423 }
Rusty Russell22e268e2010-08-05 12:59:05 -06002424
2425 /* Set up license info based on the info section */
Rusty Russell49668682010-08-05 12:59:10 -06002426 set_license(mod, get_modinfo(info, "license"));
Rusty Russell22e268e2010-08-05 12:59:05 -06002427
Rusty Russell40dd2562010-08-05 12:59:03 -06002428 return 0;
2429}
2430
Rusty Russell811d66a2010-08-05 12:59:12 -06002431static void find_module_sections(struct module *mod, struct load_info *info)
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002432{
Rusty Russell49668682010-08-05 12:59:10 -06002433 mod->kp = section_objs(info, "__param",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002434 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell49668682010-08-05 12:59:10 -06002435 mod->syms = section_objs(info, "__ksymtab",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002436 sizeof(*mod->syms), &mod->num_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002437 mod->crcs = section_addr(info, "__kcrctab");
2438 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002439 sizeof(*mod->gpl_syms),
2440 &mod->num_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002441 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
2442 mod->gpl_future_syms = section_objs(info,
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002443 "__ksymtab_gpl_future",
2444 sizeof(*mod->gpl_future_syms),
2445 &mod->num_gpl_future_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002446 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002447
2448#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell49668682010-08-05 12:59:10 -06002449 mod->unused_syms = section_objs(info, "__ksymtab_unused",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002450 sizeof(*mod->unused_syms),
2451 &mod->num_unused_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002452 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
2453 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002454 sizeof(*mod->unused_gpl_syms),
2455 &mod->num_unused_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002456 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002457#endif
2458#ifdef CONFIG_CONSTRUCTORS
Rusty Russell49668682010-08-05 12:59:10 -06002459 mod->ctors = section_objs(info, ".ctors",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002460 sizeof(*mod->ctors), &mod->num_ctors);
2461#endif
2462
2463#ifdef CONFIG_TRACEPOINTS
Mathieu Desnoyers65498642011-01-26 17:26:22 -05002464 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
2465 sizeof(*mod->tracepoints_ptrs),
2466 &mod->num_tracepoints);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002467#endif
Jason Baronbf5438fc2010-09-17 11:09:00 -04002468#ifdef HAVE_JUMP_LABEL
2469 mod->jump_entries = section_objs(info, "__jump_table",
2470 sizeof(*mod->jump_entries),
2471 &mod->num_jump_entries);
2472#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002473#ifdef CONFIG_EVENT_TRACING
Rusty Russell49668682010-08-05 12:59:10 -06002474 mod->trace_events = section_objs(info, "_ftrace_events",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002475 sizeof(*mod->trace_events),
2476 &mod->num_trace_events);
2477 /*
2478 * This section contains pointers to allocated objects in the trace
2479 * code and not scanning it leads to false positives.
2480 */
2481 kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2482 mod->num_trace_events, GFP_KERNEL);
2483#endif
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05002484#ifdef CONFIG_TRACING
2485 mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
2486 sizeof(*mod->trace_bprintk_fmt_start),
2487 &mod->num_trace_bprintk_fmt);
2488 /*
2489 * This section contains pointers to allocated objects in the trace
2490 * code and not scanning it leads to false positives.
2491 */
2492 kmemleak_scan_area(mod->trace_bprintk_fmt_start,
2493 sizeof(*mod->trace_bprintk_fmt_start) *
2494 mod->num_trace_bprintk_fmt, GFP_KERNEL);
2495#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002496#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2497 /* sechdrs[0].sh_size is always zero */
Rusty Russell49668682010-08-05 12:59:10 -06002498 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002499 sizeof(*mod->ftrace_callsites),
2500 &mod->num_ftrace_callsites);
2501#endif
Rusty Russell22e268e2010-08-05 12:59:05 -06002502
Rusty Russell811d66a2010-08-05 12:59:12 -06002503 mod->extable = section_objs(info, "__ex_table",
2504 sizeof(*mod->extable), &mod->num_exentries);
2505
Rusty Russell49668682010-08-05 12:59:10 -06002506 if (section_addr(info, "__obsparm"))
Rusty Russell22e268e2010-08-05 12:59:05 -06002507 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2508 mod->name);
Rusty Russell811d66a2010-08-05 12:59:12 -06002509
2510 info->debug = section_objs(info, "__verbose",
2511 sizeof(*info->debug), &info->num_debug);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002512}
2513
Rusty Russell49668682010-08-05 12:59:10 -06002514static int move_module(struct module *mod, struct load_info *info)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002515{
2516 int i;
2517 void *ptr;
2518
2519 /* Do the allocs. */
2520 ptr = module_alloc_update_bounds(mod->core_size);
2521 /*
2522 * The pointer to this block is stored in the module structure
2523 * which is inside the block. Just mark it as not being a
2524 * leak.
2525 */
2526 kmemleak_not_leak(ptr);
2527 if (!ptr)
Rusty Russelld9131882010-08-05 12:59:08 -06002528 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002529
2530 memset(ptr, 0, mod->core_size);
2531 mod->module_core = ptr;
2532
2533 ptr = module_alloc_update_bounds(mod->init_size);
2534 /*
2535 * The pointer to this block is stored in the module structure
2536 * which is inside the block. This block doesn't need to be
2537 * scanned as it contains data and code that will be freed
2538 * after the module is initialized.
2539 */
2540 kmemleak_ignore(ptr);
2541 if (!ptr && mod->init_size) {
2542 module_free(mod, mod->module_core);
Rusty Russelld9131882010-08-05 12:59:08 -06002543 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002544 }
2545 memset(ptr, 0, mod->init_size);
2546 mod->module_init = ptr;
2547
2548 /* Transfer each section which specifies SHF_ALLOC */
2549 DEBUGP("final section addresses:\n");
Rusty Russell49668682010-08-05 12:59:10 -06002550 for (i = 0; i < info->hdr->e_shnum; i++) {
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002551 void *dest;
Rusty Russell49668682010-08-05 12:59:10 -06002552 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002553
Rusty Russell49668682010-08-05 12:59:10 -06002554 if (!(shdr->sh_flags & SHF_ALLOC))
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002555 continue;
2556
Rusty Russell49668682010-08-05 12:59:10 -06002557 if (shdr->sh_entsize & INIT_OFFSET_MASK)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002558 dest = mod->module_init
Rusty Russell49668682010-08-05 12:59:10 -06002559 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002560 else
Rusty Russell49668682010-08-05 12:59:10 -06002561 dest = mod->module_core + shdr->sh_entsize;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002562
Rusty Russell49668682010-08-05 12:59:10 -06002563 if (shdr->sh_type != SHT_NOBITS)
2564 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002565 /* Update sh_addr to point to copy in image. */
Rusty Russell49668682010-08-05 12:59:10 -06002566 shdr->sh_addr = (unsigned long)dest;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002567 DEBUGP("\t0x%lx %s\n",
Rusty Russell49668682010-08-05 12:59:10 -06002568 shdr->sh_addr, info->secstrings + shdr->sh_name);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002569 }
Rusty Russelld9131882010-08-05 12:59:08 -06002570
2571 return 0;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002572}
2573
Rusty Russell49668682010-08-05 12:59:10 -06002574static int check_module_license_and_versions(struct module *mod)
Rusty Russell22e268e2010-08-05 12:59:05 -06002575{
2576 /*
2577 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2578 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2579 * using GPL-only symbols it needs.
2580 */
2581 if (strcmp(mod->name, "ndiswrapper") == 0)
2582 add_taint(TAINT_PROPRIETARY_MODULE);
2583
2584 /* driverloader was caught wrongly pretending to be under GPL */
2585 if (strcmp(mod->name, "driverloader") == 0)
2586 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2587
2588#ifdef CONFIG_MODVERSIONS
2589 if ((mod->num_syms && !mod->crcs)
2590 || (mod->num_gpl_syms && !mod->gpl_crcs)
2591 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2592#ifdef CONFIG_UNUSED_SYMBOLS
2593 || (mod->num_unused_syms && !mod->unused_crcs)
2594 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2595#endif
2596 ) {
2597 return try_to_force_load(mod,
2598 "no versions for exported symbols");
2599 }
2600#endif
2601 return 0;
2602}
2603
2604static void flush_module_icache(const struct module *mod)
2605{
2606 mm_segment_t old_fs;
2607
2608 /* flush the icache in correct context */
2609 old_fs = get_fs();
2610 set_fs(KERNEL_DS);
2611
2612 /*
2613 * Flush the instruction cache, since we've played with text.
2614 * Do it before processing of module parameters, so the module
2615 * can provide parameter accessor functions of its own.
2616 */
2617 if (mod->module_init)
2618 flush_icache_range((unsigned long)mod->module_init,
2619 (unsigned long)mod->module_init
2620 + mod->init_size);
2621 flush_icache_range((unsigned long)mod->module_core,
2622 (unsigned long)mod->module_core + mod->core_size);
2623
2624 set_fs(old_fs);
2625}
2626
Rusty Russelld9131882010-08-05 12:59:08 -06002627static struct module *layout_and_allocate(struct load_info *info)
2628{
2629 /* Module within temporary copy. */
2630 struct module *mod;
Rusty Russell49668682010-08-05 12:59:10 -06002631 Elf_Shdr *pcpusec;
Rusty Russelld9131882010-08-05 12:59:08 -06002632 int err;
2633
2634 mod = setup_load_info(info);
2635 if (IS_ERR(mod))
2636 return mod;
2637
Rusty Russell49668682010-08-05 12:59:10 -06002638 err = check_modinfo(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002639 if (err)
2640 return ERR_PTR(err);
2641
2642 /* Allow arches to frob section contents and sizes. */
Rusty Russell49668682010-08-05 12:59:10 -06002643 err = module_frob_arch_sections(info->hdr, info->sechdrs,
2644 info->secstrings, mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002645 if (err < 0)
Rusty Russell6526c532010-08-05 12:59:10 -06002646 goto out;
Rusty Russelld9131882010-08-05 12:59:08 -06002647
Rusty Russell49668682010-08-05 12:59:10 -06002648 pcpusec = &info->sechdrs[info->index.pcpu];
2649 if (pcpusec->sh_size) {
Rusty Russelld9131882010-08-05 12:59:08 -06002650 /* We have a special allocation for this section. */
Rusty Russell49668682010-08-05 12:59:10 -06002651 err = percpu_modalloc(mod,
2652 pcpusec->sh_size, pcpusec->sh_addralign);
Rusty Russelld9131882010-08-05 12:59:08 -06002653 if (err)
Rusty Russell6526c532010-08-05 12:59:10 -06002654 goto out;
Rusty Russell49668682010-08-05 12:59:10 -06002655 pcpusec->sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russelld9131882010-08-05 12:59:08 -06002656 }
2657
2658 /* Determine total sizes, and put offsets in sh_entsize. For now
2659 this is done generically; there doesn't appear to be any
2660 special cases for the architectures. */
Rusty Russell49668682010-08-05 12:59:10 -06002661 layout_sections(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002662
2663 info->strmap = kzalloc(BITS_TO_LONGS(info->sechdrs[info->index.str].sh_size)
2664 * sizeof(long), GFP_KERNEL);
2665 if (!info->strmap) {
2666 err = -ENOMEM;
2667 goto free_percpu;
2668 }
Rusty Russell49668682010-08-05 12:59:10 -06002669 layout_symtab(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002670
2671 /* Allocate and move to the final place */
Rusty Russell49668682010-08-05 12:59:10 -06002672 err = move_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002673 if (err)
2674 goto free_strmap;
2675
2676 /* Module has been copied to its final place now: return it. */
2677 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
Rusty Russell49668682010-08-05 12:59:10 -06002678 kmemleak_load_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002679 return mod;
2680
2681free_strmap:
2682 kfree(info->strmap);
2683free_percpu:
2684 percpu_modfree(mod);
Rusty Russell6526c532010-08-05 12:59:10 -06002685out:
Rusty Russelld9131882010-08-05 12:59:08 -06002686 return ERR_PTR(err);
2687}
2688
2689/* mod is no longer valid after this! */
2690static void module_deallocate(struct module *mod, struct load_info *info)
2691{
2692 kfree(info->strmap);
2693 percpu_modfree(mod);
2694 module_free(mod, mod->module_init);
2695 module_free(mod, mod->module_core);
2696}
2697
Rusty Russell811d66a2010-08-05 12:59:12 -06002698static int post_relocation(struct module *mod, const struct load_info *info)
2699{
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002700 /* Sort exception table now relocations are done. */
Rusty Russell811d66a2010-08-05 12:59:12 -06002701 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2702
2703 /* Copy relocated percpu area over. */
2704 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
2705 info->sechdrs[info->index.pcpu].sh_size);
2706
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002707 /* Setup kallsyms-specific fields. */
Rusty Russell811d66a2010-08-05 12:59:12 -06002708 add_kallsyms(mod, info);
2709
2710 /* Arch-specific module finalizing. */
2711 return module_finalize(info->hdr, info->sechdrs, mod);
2712}
2713
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714/* Allocate and load the module: note that size of section 0 is always
2715 zero, and we rely on this for optional sections. */
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002716static struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 unsigned long len,
2718 const char __user *uargs)
2719{
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002720 struct load_info info = { NULL, };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 struct module *mod;
Rusty Russell40dd2562010-08-05 12:59:03 -06002722 long err;
Paul Mundt3ae91c22009-10-01 15:43:54 -07002723
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
2725 umod, len, uargs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
Rusty Russelld9131882010-08-05 12:59:08 -06002727 /* Copy in the blobs from userspace, check they are vaguely sane. */
2728 err = copy_and_check(&info, umod, len, uargs);
Rusty Russell40dd2562010-08-05 12:59:03 -06002729 if (err)
2730 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
Rusty Russelld9131882010-08-05 12:59:08 -06002732 /* Figure out module layout, and allocate all the memory. */
2733 mod = layout_and_allocate(&info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002734 if (IS_ERR(mod)) {
2735 err = PTR_ERR(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002736 goto free_copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Rusty Russell49668682010-08-05 12:59:10 -06002739 /* Now module is in final location, initialize linked lists, etc. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -06002740 err = module_unload_init(mod);
2741 if (err)
Rusty Russelld9131882010-08-05 12:59:08 -06002742 goto free_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Rusty Russell22e268e2010-08-05 12:59:05 -06002744 /* Now we've got everything in the final locations, we can
2745 * find optional sections. */
Rusty Russell49668682010-08-05 12:59:10 -06002746 find_module_sections(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
Rusty Russell49668682010-08-05 12:59:10 -06002748 err = check_module_license_and_versions(mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002749 if (err)
2750 goto free_unload;
Dave Jones9841d612006-01-08 01:03:41 -08002751
Matt Domschc988d2b2005-06-23 22:05:15 -07002752 /* Set up MODINFO_ATTR fields */
Rusty Russell49668682010-08-05 12:59:10 -06002753 setup_modinfo(mod, &info);
Matt Domschc988d2b2005-06-23 22:05:15 -07002754
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 /* Fix up syms, so that st_value is a pointer to location. */
Rusty Russell49668682010-08-05 12:59:10 -06002756 err = simplify_symbols(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06002758 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
Rusty Russell49668682010-08-05 12:59:10 -06002760 err = apply_relocations(mod, &info);
Rusty Russell22e268e2010-08-05 12:59:05 -06002761 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06002762 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
Rusty Russell811d66a2010-08-05 12:59:12 -06002764 err = post_relocation(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06002766 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
Rusty Russell22e268e2010-08-05 12:59:05 -06002768 flush_module_icache(mod);
Thomas Koeller378bac82005-09-06 15:17:11 -07002769
Rusty Russell6526c532010-08-05 12:59:10 -06002770 /* Now copy in args */
2771 mod->args = strndup_user(uargs, ~0UL >> 1);
2772 if (IS_ERR(mod->args)) {
2773 err = PTR_ERR(mod->args);
2774 goto free_arch_cleanup;
2775 }
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002776
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002777 /* Mark state as coming so strong_try_module_get() ignores us. */
Rusty Russelld9131882010-08-05 12:59:08 -06002778 mod->state = MODULE_STATE_COMING;
2779
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002780 /* Now sew it into the lists so we can get lockdep and oops
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002781 * info during argument parsing. No one should access us, since
Andi Kleend72b3752008-08-30 10:09:00 +02002782 * strong_try_module_get() will fail.
2783 * lockdep/oops can run asynchronous, so use the RCU list insertion
2784 * function to insert in a way safe to concurrent readers.
2785 * The mutex protects against concurrent writers.
2786 */
Rusty Russell75676502010-06-05 11:17:36 -06002787 mutex_lock(&module_mutex);
Linus Torvalds3bafeb62010-06-05 11:17:36 -06002788 if (find_module(mod->name)) {
2789 err = -EEXIST;
Rusty Russellbe593f42010-06-05 11:17:37 -06002790 goto unlock;
Linus Torvalds3bafeb62010-06-05 11:17:36 -06002791 }
2792
Rusty Russell811d66a2010-08-05 12:59:12 -06002793 /* This has to be done once we're sure module name is unique. */
2794 if (!mod->taints)
2795 dynamic_debug_setup(info.debug, info.num_debug);
Yehuda Sadehff49d742010-07-03 13:07:35 +10002796
Rusty Russellbe593f42010-06-05 11:17:37 -06002797 /* Find duplicate symbols */
2798 err = verify_export_symbols(mod);
2799 if (err < 0)
Yehuda Sadehff49d742010-07-03 13:07:35 +10002800 goto ddebug;
Rusty Russellbe593f42010-06-05 11:17:37 -06002801
Linus Torvalds53363772010-10-05 11:29:27 -07002802 module_bug_finalize(info.hdr, info.sechdrs, mod);
Andi Kleend72b3752008-08-30 10:09:00 +02002803 list_add_rcu(&mod->list, &modules);
Rusty Russell75676502010-06-05 11:17:36 -06002804 mutex_unlock(&module_mutex);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002805
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002806 /* Module is ready to execute: parsing args may do that. */
Rusty Russelle180a6b2009-03-31 13:05:29 -06002807 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002809 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002811 /* Link in to syfs. */
Rusty Russell8f6d0372010-08-05 12:59:09 -06002812 err = mod_sysfs_setup(mod, &info, mod->kp, mod->num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002814 goto unlink;
Rusty Russell80a3d1b2010-06-05 11:17:36 -06002815
Rusty Russelld9131882010-08-05 12:59:08 -06002816 /* Get rid of temporary copy and strmap. */
2817 kfree(info.strmap);
2818 free_copy(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819
2820 /* Done! */
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002821 trace_module_load(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 return mod;
2823
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002824 unlink:
Rusty Russell75676502010-06-05 11:17:36 -06002825 mutex_lock(&module_mutex);
Rusty Russelle91defa2009-03-31 13:05:35 -06002826 /* Unlink carefully: kallsyms could be walking list. */
2827 list_del_rcu(&mod->list);
Linus Torvalds53363772010-10-05 11:29:27 -07002828 module_bug_cleanup(mod);
2829
Yehuda Sadehff49d742010-07-03 13:07:35 +10002830 ddebug:
Rusty Russell811d66a2010-08-05 12:59:12 -06002831 if (!mod->taints)
2832 dynamic_debug_remove(info.debug);
Rusty Russellbe593f42010-06-05 11:17:37 -06002833 unlock:
Rusty Russell75676502010-06-05 11:17:36 -06002834 mutex_unlock(&module_mutex);
Rusty Russelle91defa2009-03-31 13:05:35 -06002835 synchronize_sched();
Rusty Russell6526c532010-08-05 12:59:10 -06002836 kfree(mod->args);
2837 free_arch_cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 module_arch_cleanup(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002839 free_modinfo:
Rusty Russella263f772009-09-25 00:32:58 -06002840 free_modinfo(mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002841 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 module_unload_free(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002843 free_module:
2844 module_deallocate(mod, &info);
2845 free_copy:
2846 free_copy(&info);
Jayachandran C6fe2e702006-01-06 00:19:54 -08002847 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848}
2849
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002850/* Call module constructors. */
2851static void do_mod_ctors(struct module *mod)
2852{
2853#ifdef CONFIG_CONSTRUCTORS
2854 unsigned long i;
2855
2856 for (i = 0; i < mod->num_ctors; i++)
2857 mod->ctors[i]();
2858#endif
2859}
2860
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861/* This is where the real work happens */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002862SYSCALL_DEFINE3(init_module, void __user *, umod,
2863 unsigned long, len, const char __user *, uargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864{
2865 struct module *mod;
2866 int ret = 0;
2867
2868 /* Must have permission */
Kees Cook3d433212009-04-02 15:49:29 -07002869 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 return -EPERM;
2871
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 /* Do all the hard work */
2873 mod = load_module(umod, len, uargs);
Rusty Russell75676502010-06-05 11:17:36 -06002874 if (IS_ERR(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 return PTR_ERR(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
Alan Sterne041c682006-03-27 01:16:30 -08002877 blocking_notifier_call_chain(&module_notify_list,
2878 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
Steven Rostedt94462ad2010-11-29 13:15:42 -05002880 /* Set RO and NX regions for core */
2881 set_section_ro_nx(mod->module_core,
2882 mod->core_text_size,
2883 mod->core_ro_size,
2884 mod->core_size);
2885
2886 /* Set RO and NX regions for init */
2887 set_section_ro_nx(mod->module_init,
2888 mod->init_text_size,
2889 mod->init_ro_size,
2890 mod->init_size);
2891
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002892 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 /* Start the module */
2894 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002895 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 if (ret < 0) {
2897 /* Init routine failed: abort. Try to protect us from
2898 buggy refcounters. */
2899 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002900 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002901 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002902 blocking_notifier_call_chain(&module_notify_list,
2903 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002904 free_module(mod);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002905 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 return ret;
2907 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002908 if (ret > 0) {
Joe Perchesad361c92009-07-06 13:05:40 -07002909 printk(KERN_WARNING
2910"%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
2911"%s: loading module anyway...\n",
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002912 __func__, mod->name, ret,
2913 __func__);
2914 dump_stack();
2915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
Rusty Russell6c5db222008-03-10 11:43:52 -07002917 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002919 wake_up(&module_wq);
Masami Hiramatsu0deddf42009-01-06 14:41:54 -08002920 blocking_notifier_call_chain(&module_notify_list,
2921 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07002922
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07002923 /* We need to finish all async code before the module init sequence is done */
2924 async_synchronize_full();
2925
Rusty Russell6c5db222008-03-10 11:43:52 -07002926 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 /* Drop initial reference. */
2928 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06002929 trim_init_extable(mod);
Jan Beulich4a496222009-07-06 14:50:42 +01002930#ifdef CONFIG_KALLSYMS
2931 mod->num_symtab = mod->core_num_syms;
2932 mod->symtab = mod->core_symtab;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002933 mod->strtab = mod->core_strtab;
Jan Beulich4a496222009-07-06 14:50:42 +01002934#endif
matthieu castet84e1c6b2010-11-16 22:35:16 +01002935 unset_section_ro_nx(mod, mod->module_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 module_free(mod, mod->module_init);
2937 mod->module_init = NULL;
2938 mod->init_size = 0;
Jan Glauber4d103802011-05-19 16:55:25 -06002939 mod->init_ro_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002941 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
2943 return 0;
2944}
2945
2946static inline int within(unsigned long addr, void *start, unsigned long size)
2947{
2948 return ((void *)addr >= start && (void *)addr < start + size);
2949}
2950
2951#ifdef CONFIG_KALLSYMS
2952/*
2953 * This ignores the intensely annoying "mapping symbols" found
2954 * in ARM ELF files: $a, $t and $d.
2955 */
2956static inline int is_arm_mapping_symbol(const char *str)
2957{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002958 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 && (str[2] == '\0' || str[2] == '.');
2960}
2961
2962static const char *get_ksymbol(struct module *mod,
2963 unsigned long addr,
2964 unsigned long *size,
2965 unsigned long *offset)
2966{
2967 unsigned int i, best = 0;
2968 unsigned long nextval;
2969
2970 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002971 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002973 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2975
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002976 /* Scan for closest preceding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002977 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 for (i = 1; i < mod->num_symtab; i++) {
2979 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2980 continue;
2981
2982 /* We ignore unnamed symbols: they're uninformative
2983 * and inserted at a whim. */
2984 if (mod->symtab[i].st_value <= addr
2985 && mod->symtab[i].st_value > mod->symtab[best].st_value
2986 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2987 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2988 best = i;
2989 if (mod->symtab[i].st_value > addr
2990 && mod->symtab[i].st_value < nextval
2991 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2992 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2993 nextval = mod->symtab[i].st_value;
2994 }
2995
2996 if (!best)
2997 return NULL;
2998
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002999 if (size)
3000 *size = nextval - mod->symtab[best].st_value;
3001 if (offset)
3002 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 return mod->strtab + mod->symtab[best].st_name;
3004}
3005
Rusty Russell6dd06c92008-01-29 17:13:22 -05003006/* For kallsyms to ask for address resolution. NULL means not found. Careful
3007 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003008const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05003009 unsigned long *size,
3010 unsigned long *offset,
3011 char **modname,
3012 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013{
3014 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08003015 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Rusty Russellcb2a5202008-01-14 00:55:03 -08003017 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003018 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003019 if (within_module_init(addr, mod) ||
3020 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07003021 if (modname)
3022 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08003023 ret = get_ksymbol(mod, addr, size, offset);
3024 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 }
3026 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05003027 /* Make a copy in here where it's safe */
3028 if (ret) {
3029 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
3030 ret = namebuf;
3031 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003032 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003033 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034}
3035
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003036int lookup_module_symbol_name(unsigned long addr, char *symname)
3037{
3038 struct module *mod;
3039
Rusty Russellcb2a5202008-01-14 00:55:03 -08003040 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003041 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003042 if (within_module_init(addr, mod) ||
3043 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003044 const char *sym;
3045
3046 sym = get_ksymbol(mod, addr, NULL, NULL);
3047 if (!sym)
3048 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07003049 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003050 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003051 return 0;
3052 }
3053 }
3054out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003055 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003056 return -ERANGE;
3057}
3058
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003059int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
3060 unsigned long *offset, char *modname, char *name)
3061{
3062 struct module *mod;
3063
Rusty Russellcb2a5202008-01-14 00:55:03 -08003064 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003065 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003066 if (within_module_init(addr, mod) ||
3067 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003068 const char *sym;
3069
3070 sym = get_ksymbol(mod, addr, size, offset);
3071 if (!sym)
3072 goto out;
3073 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07003074 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003075 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07003076 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003077 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003078 return 0;
3079 }
3080 }
3081out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003082 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003083 return -ERANGE;
3084}
3085
Alexey Dobriyanea078902007-05-08 00:28:39 -07003086int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
3087 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088{
3089 struct module *mod;
3090
Rusty Russellcb2a5202008-01-14 00:55:03 -08003091 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003092 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 if (symnum < mod->num_symtab) {
3094 *value = mod->symtab[symnum].st_value;
3095 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07003096 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07003097 KSYM_NAME_LEN);
3098 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06003099 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003100 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003101 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 }
3103 symnum -= mod->num_symtab;
3104 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003105 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003106 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107}
3108
3109static unsigned long mod_find_symname(struct module *mod, const char *name)
3110{
3111 unsigned int i;
3112
3113 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08003114 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
3115 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 return mod->symtab[i].st_value;
3117 return 0;
3118}
3119
3120/* Look for this name: can be of form module:name. */
3121unsigned long module_kallsyms_lookup_name(const char *name)
3122{
3123 struct module *mod;
3124 char *colon;
3125 unsigned long ret = 0;
3126
3127 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08003128 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 if ((colon = strchr(name, ':')) != NULL) {
3130 *colon = '\0';
3131 if ((mod = find_module(name)) != NULL)
3132 ret = mod_find_symname(mod, colon+1);
3133 *colon = ':';
3134 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02003135 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 if ((ret = mod_find_symname(mod, name)) != 0)
3137 break;
3138 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003139 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 return ret;
3141}
Anders Kaseorg75a66612008-12-05 19:03:58 -05003142
3143int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
3144 struct module *, unsigned long),
3145 void *data)
3146{
3147 struct module *mod;
3148 unsigned int i;
3149 int ret;
3150
3151 list_for_each_entry(mod, &modules, list) {
3152 for (i = 0; i < mod->num_symtab; i++) {
3153 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
3154 mod, mod->symtab[i].st_value);
3155 if (ret != 0)
3156 return ret;
3157 }
3158 }
3159 return 0;
3160}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161#endif /* CONFIG_KALLSYMS */
3162
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003163static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003164{
3165 int bx = 0;
3166
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003167 if (mod->taints ||
3168 mod->state == MODULE_STATE_GOING ||
3169 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003170 buf[bx++] = '(';
Andi Kleen25ddbb12008-10-15 22:01:41 -07003171 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003172 buf[bx++] = 'P';
Andi Kleen25ddbb12008-10-15 22:01:41 -07003173 if (mod->taints & (1 << TAINT_FORCED_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003174 buf[bx++] = 'F';
Linus Torvalds26e9a392008-10-17 09:50:12 -07003175 if (mod->taints & (1 << TAINT_CRAP))
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07003176 buf[bx++] = 'C';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003177 /*
3178 * TAINT_FORCED_RMMOD: could be added.
3179 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
3180 * apply to modules.
3181 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003182
3183 /* Show a - for module-is-being-unloaded */
3184 if (mod->state == MODULE_STATE_GOING)
3185 buf[bx++] = '-';
3186 /* Show a + for module-is-being-loaded */
3187 if (mod->state == MODULE_STATE_COMING)
3188 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003189 buf[bx++] = ')';
3190 }
3191 buf[bx] = '\0';
3192
3193 return buf;
3194}
3195
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003196#ifdef CONFIG_PROC_FS
3197/* Called by the /proc file system to return a list of modules. */
3198static void *m_start(struct seq_file *m, loff_t *pos)
3199{
3200 mutex_lock(&module_mutex);
3201 return seq_list_start(&modules, *pos);
3202}
3203
3204static void *m_next(struct seq_file *m, void *p, loff_t *pos)
3205{
3206 return seq_list_next(p, &modules, pos);
3207}
3208
3209static void m_stop(struct seq_file *m, void *p)
3210{
3211 mutex_unlock(&module_mutex);
3212}
3213
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214static int m_show(struct seq_file *m, void *p)
3215{
3216 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003217 char buf[8];
3218
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05003219 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 mod->name, mod->init_size + mod->core_size);
3221 print_unload_info(m, mod);
3222
3223 /* Informative for users. */
3224 seq_printf(m, " %s",
3225 mod->state == MODULE_STATE_GOING ? "Unloading":
3226 mod->state == MODULE_STATE_COMING ? "Loading":
3227 "Live");
3228 /* Used by oprofile and other similar tools. */
Kees Cook9f36e2c2011-03-22 16:34:22 -07003229 seq_printf(m, " 0x%pK", mod->module_core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003231 /* Taints info */
3232 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003233 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003234
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 seq_printf(m, "\n");
3236 return 0;
3237}
3238
3239/* Format: modulename size refcount deps address
3240
3241 Where refcount is a number or -, and deps is a comma-separated list
3242 of depends or -.
3243*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003244static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 .start = m_start,
3246 .next = m_next,
3247 .stop = m_stop,
3248 .show = m_show
3249};
3250
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003251static int modules_open(struct inode *inode, struct file *file)
3252{
3253 return seq_open(file, &modules_op);
3254}
3255
3256static const struct file_operations proc_modules_operations = {
3257 .open = modules_open,
3258 .read = seq_read,
3259 .llseek = seq_lseek,
3260 .release = seq_release,
3261};
3262
3263static int __init proc_modules_init(void)
3264{
3265 proc_create("modules", 0, NULL, &proc_modules_operations);
3266 return 0;
3267}
3268module_init(proc_modules_init);
3269#endif
3270
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271/* Given an address, look for it in the module exception tables. */
3272const struct exception_table_entry *search_module_extables(unsigned long addr)
3273{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 const struct exception_table_entry *e = NULL;
3275 struct module *mod;
3276
Rusty Russell24da1cb2007-07-15 23:41:46 -07003277 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003278 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 if (mod->num_exentries == 0)
3280 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003281
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 e = search_extable(mod->extable,
3283 mod->extable + mod->num_exentries - 1,
3284 addr);
3285 if (e)
3286 break;
3287 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07003288 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
3290 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07003291 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 return e;
3293}
3294
Ingo Molnar4d435f92006-07-03 00:24:24 -07003295/*
Rusty Russelle6104992009-03-31 13:05:31 -06003296 * is_module_address - is this address inside a module?
3297 * @addr: the address to check.
3298 *
3299 * See is_module_text_address() if you simply want to see if the address
3300 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07003301 */
Rusty Russelle6104992009-03-31 13:05:31 -06003302bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07003303{
Rusty Russelle6104992009-03-31 13:05:31 -06003304 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003305
Rusty Russell24da1cb2007-07-15 23:41:46 -07003306 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06003307 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07003308 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07003309
Rusty Russelle6104992009-03-31 13:05:31 -06003310 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003311}
3312
Rusty Russelle6104992009-03-31 13:05:31 -06003313/*
3314 * __module_address - get the module which contains an address.
3315 * @addr: the address.
3316 *
3317 * Must be called with preempt disabled or module mutex held so that
3318 * module doesn't get freed during this.
3319 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07003320struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321{
3322 struct module *mod;
3323
Rusty Russell3a642e92008-07-22 19:24:28 -05003324 if (addr < module_addr_min || addr > module_addr_max)
3325 return NULL;
3326
Andi Kleend72b3752008-08-30 10:09:00 +02003327 list_for_each_entry_rcu(mod, &modules, list)
Rusty Russelle6104992009-03-31 13:05:31 -06003328 if (within_module_core(addr, mod)
3329 || within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 return mod;
3331 return NULL;
3332}
Tim Abbottc6b37802008-12-05 19:03:59 -05003333EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334
Rusty Russelle6104992009-03-31 13:05:31 -06003335/*
3336 * is_module_text_address - is this address inside module code?
3337 * @addr: the address to check.
3338 *
3339 * See is_module_address() if you simply want to see if the address is
3340 * anywhere in a module. See kernel_text_address() for testing if an
3341 * address corresponds to kernel or module code.
3342 */
3343bool is_module_text_address(unsigned long addr)
3344{
3345 bool ret;
3346
3347 preempt_disable();
3348 ret = __module_text_address(addr) != NULL;
3349 preempt_enable();
3350
3351 return ret;
3352}
3353
3354/*
3355 * __module_text_address - get the module whose code contains an address.
3356 * @addr: the address.
3357 *
3358 * Must be called with preempt disabled or module mutex held so that
3359 * module doesn't get freed during this.
3360 */
3361struct module *__module_text_address(unsigned long addr)
3362{
3363 struct module *mod = __module_address(addr);
3364 if (mod) {
3365 /* Make sure it's within the text section. */
3366 if (!within(addr, mod->module_init, mod->init_text_size)
3367 && !within(addr, mod->module_core, mod->core_text_size))
3368 mod = NULL;
3369 }
3370 return mod;
3371}
Tim Abbottc6b37802008-12-05 19:03:59 -05003372EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06003373
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374/* Don't grab lock, we're oopsing. */
3375void print_modules(void)
3376{
3377 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07003378 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379
Linus Torvaldsb2311252009-06-16 11:07:14 -07003380 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02003381 /* Most callers should already have preempt disabled, but make sure */
3382 preempt_disable();
3383 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003384 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02003385 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01003386 if (last_unloaded_module[0])
3387 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 printk("\n");
3389}
3390
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06003392/* Generate the signature for all relevant module structures here.
3393 * If these change, we don't want to try to parse the module. */
3394void module_layout(struct module *mod,
3395 struct modversion_info *ver,
3396 struct kernel_param *kp,
3397 struct kernel_symbol *ks,
Mathieu Desnoyers65498642011-01-26 17:26:22 -05003398 struct tracepoint * const *tp)
Rusty Russell8c8ef422009-03-31 13:05:34 -06003399{
3400}
3401EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07003403
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04003404#ifdef CONFIG_TRACEPOINTS
3405void module_update_tracepoints(void)
3406{
3407 struct module *mod;
3408
3409 mutex_lock(&module_mutex);
3410 list_for_each_entry(mod, &modules, list)
3411 if (!mod->taints)
Mathieu Desnoyers65498642011-01-26 17:26:22 -05003412 tracepoint_update_probe_range(mod->tracepoints_ptrs,
3413 mod->tracepoints_ptrs + mod->num_tracepoints);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04003414 mutex_unlock(&module_mutex);
3415}
3416
3417/*
3418 * Returns 0 if current not found.
3419 * Returns 1 if current found.
3420 */
3421int module_get_iter_tracepoints(struct tracepoint_iter *iter)
3422{
3423 struct module *iter_mod;
3424 int found = 0;
3425
3426 mutex_lock(&module_mutex);
3427 list_for_each_entry(iter_mod, &modules, list) {
3428 if (!iter_mod->taints) {
3429 /*
3430 * Sorted module list
3431 */
3432 if (iter_mod < iter->module)
3433 continue;
3434 else if (iter_mod > iter->module)
3435 iter->tracepoint = NULL;
3436 found = tracepoint_get_iter_range(&iter->tracepoint,
Mathieu Desnoyers65498642011-01-26 17:26:22 -05003437 iter_mod->tracepoints_ptrs,
3438 iter_mod->tracepoints_ptrs
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04003439 + iter_mod->num_tracepoints);
3440 if (found) {
3441 iter->module = iter_mod;
3442 break;
3443 }
3444 }
3445 }
3446 mutex_unlock(&module_mutex);
3447 return found;
3448}
3449#endif