blob: b751f1902476171bdab66af590d6f1d3ee1b7065 [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
3 Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
Linus 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Li Zefan7ead8b82009-08-17 16:56:28 +080059#define CREATE_TRACE_POINTS
60#include <trace/events/module.h>
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#if 0
63#define DEBUGP printk
64#else
65#define DEBUGP(fmt , a...)
66#endif
67
68#ifndef ARCH_SHF_SMALL
69#define ARCH_SHF_SMALL 0
70#endif
71
72/* If this is set, the section belongs in the init part of the module */
73#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
74
Rusty Russell24da1cb2007-07-15 23:41:46 -070075/* List of modules, protected by module_mutex or preempt_disable
Andi Kleend72b3752008-08-30 10:09:00 +020076 * (delete uses stop_machine/add uses RCU list operations). */
Tim Abbottc6b37802008-12-05 19:03:59 -050077DEFINE_MUTEX(module_mutex);
78EXPORT_SYMBOL_GPL(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static LIST_HEAD(modules);
Jason Wessel67fc4e02010-05-20 21:04:21 -050080#ifdef CONFIG_KGDB_KDB
81struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
82#endif /* CONFIG_KGDB_KDB */
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Stephen Rothwell19e45292009-04-14 17:27:18 +100085/* Block module loading/unloading? */
86int modules_disabled = 0;
87
Rusty Russellc9a3ba52008-01-29 17:13:18 -050088/* Waiting for a module to finish initializing? */
89static DECLARE_WAIT_QUEUE_HEAD(module_wq);
90
Alan Sterne041c682006-03-27 01:16:30 -080091static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Rusty Russelle6104992009-03-31 13:05:31 -060093/* Bounds of module allocation, for speeding __module_address */
Rusty Russell3a642e92008-07-22 19:24:28 -050094static unsigned long module_addr_min = -1UL, module_addr_max = 0;
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096int register_module_notifier(struct notifier_block * nb)
97{
Alan Sterne041c682006-03-27 01:16:30 -080098 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100EXPORT_SYMBOL(register_module_notifier);
101
102int unregister_module_notifier(struct notifier_block * nb)
103{
Alan Sterne041c682006-03-27 01:16:30 -0800104 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106EXPORT_SYMBOL(unregister_module_notifier);
107
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -0800108/* We require a truly strong try_module_get(): 0 means failure due to
109 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static inline int strong_try_module_get(struct module *mod)
111{
112 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500113 return -EBUSY;
114 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500116 else
117 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700120static inline void add_taint_module(struct module *mod, unsigned flag)
121{
122 add_taint(flag);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700123 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700124}
125
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200126/*
127 * A thread that wants to hold a reference to a module only while it
128 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 */
130void __module_put_and_exit(struct module *mod, long code)
131{
132 module_put(mod);
133 do_exit(code);
134}
135EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/* Find a module section: 0 means not found. */
138static unsigned int find_sec(Elf_Ehdr *hdr,
139 Elf_Shdr *sechdrs,
140 const char *secstrings,
141 const char *name)
142{
143 unsigned int i;
144
145 for (i = 1; i < hdr->e_shnum; i++)
146 /* Alloc bit cleared means "ignore it." */
147 if ((sechdrs[i].sh_flags & SHF_ALLOC)
148 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
149 return i;
150 return 0;
151}
152
Rusty Russell5e458cc2008-10-22 10:00:13 -0500153/* Find a module section, or NULL. */
154static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
155 const char *secstrings, const char *name)
156{
157 /* Section 0 has sh_addr 0. */
158 return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
159}
160
161/* Find a module section, or NULL. Fill in number of "objects" in section. */
162static void *section_objs(Elf_Ehdr *hdr,
163 Elf_Shdr *sechdrs,
164 const char *secstrings,
165 const char *name,
166 size_t object_size,
167 unsigned int *num)
168{
169 unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
170
171 /* Section 0 has sh_addr 0 and sh_size 0. */
172 *num = sechdrs[sec].sh_size / object_size;
173 return (void *)sechdrs[sec].sh_addr;
174}
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176/* Provided by the linker */
177extern const struct kernel_symbol __start___ksymtab[];
178extern const struct kernel_symbol __stop___ksymtab[];
179extern const struct kernel_symbol __start___ksymtab_gpl[];
180extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800181extern const struct kernel_symbol __start___ksymtab_gpl_future[];
182extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700183extern const struct kernel_symbol __start___ksymtab_gpl_future[];
184extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185extern const unsigned long __start___kcrctab[];
186extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800187extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500188#ifdef CONFIG_UNUSED_SYMBOLS
189extern const struct kernel_symbol __start___ksymtab_unused[];
190extern const struct kernel_symbol __stop___ksymtab_unused[];
191extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
192extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700193extern const unsigned long __start___kcrctab_unused[];
194extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500195#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197#ifndef CONFIG_MODVERSIONS
198#define symversion(base, idx) NULL
199#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800200#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201#endif
202
Rusty Russelldafd0942008-07-22 19:24:25 -0500203static bool each_symbol_in_section(const struct symsearch *arr,
204 unsigned int arrsize,
205 struct module *owner,
206 bool (*fn)(const struct symsearch *syms,
207 struct module *owner,
208 unsigned int symnum, void *data),
209 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100210{
Rusty Russelldafd0942008-07-22 19:24:25 -0500211 unsigned int i, j;
212
213 for (j = 0; j < arrsize; j++) {
214 for (i = 0; i < arr[j].stop - arr[j].start; i++)
215 if (fn(&arr[j], owner, i, data))
216 return true;
217 }
218
219 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100220}
221
Rusty Russelldafd0942008-07-22 19:24:25 -0500222/* Returns true as soon as fn returns true, otherwise false. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500223bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
224 unsigned int symnum, void *data), void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700225{
Rusty Russelldafd0942008-07-22 19:24:25 -0500226 struct module *mod;
227 const struct symsearch arr[] = {
228 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
229 NOT_GPL_ONLY, false },
230 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
231 __start___kcrctab_gpl,
232 GPL_ONLY, false },
233 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
234 __start___kcrctab_gpl_future,
235 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500236#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500237 { __start___ksymtab_unused, __stop___ksymtab_unused,
238 __start___kcrctab_unused,
239 NOT_GPL_ONLY, true },
240 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
241 __start___kcrctab_unused_gpl,
242 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500243#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500244 };
245
246 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
247 return true;
248
Andi Kleend72b3752008-08-30 10:09:00 +0200249 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500250 struct symsearch arr[] = {
251 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
252 NOT_GPL_ONLY, false },
253 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
254 mod->gpl_crcs,
255 GPL_ONLY, false },
256 { mod->gpl_future_syms,
257 mod->gpl_future_syms + mod->num_gpl_future_syms,
258 mod->gpl_future_crcs,
259 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500260#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500261 { mod->unused_syms,
262 mod->unused_syms + mod->num_unused_syms,
263 mod->unused_crcs,
264 NOT_GPL_ONLY, true },
265 { mod->unused_gpl_syms,
266 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
267 mod->unused_gpl_crcs,
268 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500269#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500270 };
271
272 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
273 return true;
274 }
275 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700276}
Tim Abbottc6b37802008-12-05 19:03:59 -0500277EXPORT_SYMBOL_GPL(each_symbol);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700278
Rusty Russelldafd0942008-07-22 19:24:25 -0500279struct find_symbol_arg {
280 /* Input */
281 const char *name;
282 bool gplok;
283 bool warn;
284
285 /* Output */
286 struct module *owner;
287 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500288 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500289};
290
291static bool find_symbol_in_section(const struct symsearch *syms,
292 struct module *owner,
293 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500294{
Rusty Russelldafd0942008-07-22 19:24:25 -0500295 struct find_symbol_arg *fsa = data;
296
297 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
298 return false;
299
300 if (!fsa->gplok) {
301 if (syms->licence == GPL_ONLY)
302 return false;
303 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
304 printk(KERN_WARNING "Symbol %s is being used "
305 "by a non-GPL module, which will not "
306 "be allowed in the future\n", fsa->name);
307 printk(KERN_WARNING "Please see the file "
308 "Documentation/feature-removal-schedule.txt "
309 "in the kernel source tree for more details.\n");
310 }
311 }
312
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500313#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500314 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500315 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500316 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500317 printk(KERN_WARNING
318 "This symbol will go away in the future.\n");
319 printk(KERN_WARNING
320 "Please evalute if this is the right api to use and if "
321 "it really is, submit a report the linux kernel "
322 "mailinglist together with submitting your code for "
323 "inclusion.\n");
324 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500325#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500326
327 fsa->owner = owner;
328 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500329 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500330 return true;
331}
332
Tim Abbott414fd312008-12-05 19:03:56 -0500333/* Find a symbol and return it, along with, (optional) crc and
334 * (optional) module which owns it */
Tim Abbottc6b37802008-12-05 19:03:59 -0500335const struct kernel_symbol *find_symbol(const char *name,
336 struct module **owner,
337 const unsigned long **crc,
338 bool gplok,
339 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Rusty Russelldafd0942008-07-22 19:24:25 -0500341 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Rusty Russelldafd0942008-07-22 19:24:25 -0500343 fsa.name = name;
344 fsa.gplok = gplok;
345 fsa.warn = warn;
346
347 if (each_symbol(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500348 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500349 *owner = fsa.owner;
350 if (crc)
351 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500352 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 DEBUGP("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500356 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
Tim Abbottc6b37802008-12-05 19:03:59 -0500358EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360/* Search for module by name: must hold module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500361struct module *find_module(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 struct module *mod;
364
365 list_for_each_entry(mod, &modules, list) {
366 if (strcmp(mod->name, name) == 0)
367 return mod;
368 }
369 return NULL;
370}
Tim Abbottc6b37802008-12-05 19:03:59 -0500371EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900374
Tejun Heo259354d2010-03-10 18:56:10 +0900375static inline void __percpu *mod_percpu(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900376{
Tejun Heo259354d2010-03-10 18:56:10 +0900377 return mod->percpu;
378}
Tejun Heofbf59bc2009-02-20 16:29:08 +0900379
Tejun Heo259354d2010-03-10 18:56:10 +0900380static int percpu_modalloc(struct module *mod,
381 unsigned long size, unsigned long align)
382{
Tejun Heofbf59bc2009-02-20 16:29:08 +0900383 if (align > PAGE_SIZE) {
384 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
Tejun Heo259354d2010-03-10 18:56:10 +0900385 mod->name, align, PAGE_SIZE);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900386 align = PAGE_SIZE;
387 }
388
Tejun Heo259354d2010-03-10 18:56:10 +0900389 mod->percpu = __alloc_reserved_percpu(size, align);
390 if (!mod->percpu) {
Tejun Heofbf59bc2009-02-20 16:29:08 +0900391 printk(KERN_WARNING
392 "Could not allocate %lu bytes percpu data\n", size);
Tejun Heo259354d2010-03-10 18:56:10 +0900393 return -ENOMEM;
394 }
395 mod->percpu_size = size;
396 return 0;
Tejun Heofbf59bc2009-02-20 16:29:08 +0900397}
398
Tejun Heo259354d2010-03-10 18:56:10 +0900399static void percpu_modfree(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900400{
Tejun Heo259354d2010-03-10 18:56:10 +0900401 free_percpu(mod->percpu);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900402}
403
Tejun Heo6b588c12009-02-20 16:29:07 +0900404static unsigned int find_pcpusec(Elf_Ehdr *hdr,
405 Elf_Shdr *sechdrs,
406 const char *secstrings)
407{
408 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
409}
410
Tejun Heo259354d2010-03-10 18:56:10 +0900411static void percpu_modcopy(struct module *mod,
412 const void *from, unsigned long size)
Tejun Heo6b588c12009-02-20 16:29:07 +0900413{
414 int cpu;
415
416 for_each_possible_cpu(cpu)
Tejun Heo259354d2010-03-10 18:56:10 +0900417 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
Tejun Heo6b588c12009-02-20 16:29:07 +0900418}
419
Tejun Heo10fad5e2010-03-10 18:57:54 +0900420/**
421 * is_module_percpu_address - test whether address is from module static percpu
422 * @addr: address to test
423 *
424 * Test whether @addr belongs to module static percpu area.
425 *
426 * RETURNS:
427 * %true if @addr is from module static percpu area
428 */
429bool is_module_percpu_address(unsigned long addr)
430{
431 struct module *mod;
432 unsigned int cpu;
433
434 preempt_disable();
435
436 list_for_each_entry_rcu(mod, &modules, list) {
437 if (!mod->percpu_size)
438 continue;
439 for_each_possible_cpu(cpu) {
440 void *start = per_cpu_ptr(mod->percpu, cpu);
441
442 if ((void *)addr >= start &&
443 (void *)addr < start + mod->percpu_size) {
444 preempt_enable();
445 return true;
446 }
447 }
448 }
449
450 preempt_enable();
451 return false;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700452}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900455
Tejun Heo259354d2010-03-10 18:56:10 +0900456static inline void __percpu *mod_percpu(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 return NULL;
459}
Tejun Heo259354d2010-03-10 18:56:10 +0900460static inline int percpu_modalloc(struct module *mod,
461 unsigned long size, unsigned long align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Tejun Heo259354d2010-03-10 18:56:10 +0900463 return -ENOMEM;
464}
465static inline void percpu_modfree(struct module *mod)
466{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
469 Elf_Shdr *sechdrs,
470 const char *secstrings)
471{
472 return 0;
473}
Tejun Heo259354d2010-03-10 18:56:10 +0900474static inline void percpu_modcopy(struct module *mod,
475 const void *from, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 /* pcpusec should be 0, and size of that section should be 0. */
478 BUG_ON(size != 0);
479}
Tejun Heo10fad5e2010-03-10 18:57:54 +0900480bool is_module_percpu_address(unsigned long addr)
481{
482 return false;
483}
Tejun Heo6b588c12009-02-20 16:29:07 +0900484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485#endif /* CONFIG_SMP */
486
Matt Domschc988d2b2005-06-23 22:05:15 -0700487#define MODINFO_ATTR(field) \
488static void setup_modinfo_##field(struct module *mod, const char *s) \
489{ \
490 mod->field = kstrdup(s, GFP_KERNEL); \
491} \
492static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
493 struct module *mod, char *buffer) \
494{ \
495 return sprintf(buffer, "%s\n", mod->field); \
496} \
497static int modinfo_##field##_exists(struct module *mod) \
498{ \
499 return mod->field != NULL; \
500} \
501static void free_modinfo_##field(struct module *mod) \
502{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700503 kfree(mod->field); \
504 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700505} \
506static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900507 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700508 .show = show_modinfo_##field, \
509 .setup = setup_modinfo_##field, \
510 .test = modinfo_##field##_exists, \
511 .free = free_modinfo_##field, \
512};
513
514MODINFO_ATTR(version);
515MODINFO_ATTR(srcversion);
516
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100517static char last_unloaded_module[MODULE_NAME_LEN+1];
518
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800519#ifdef CONFIG_MODULE_UNLOAD
Steven Rostedteb0c5372010-03-29 14:25:18 -0400520
521EXPORT_TRACEPOINT_SYMBOL(module_get);
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523/* Init the unload section of the module. */
524static void module_unload_init(struct module *mod)
525{
Eric Dumazet720eba32009-02-03 13:31:36 +1030526 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 INIT_LIST_HEAD(&mod->modules_which_use_me);
Nick Piggin5fbfb182010-04-01 19:09:40 +1100529 for_each_possible_cpu(cpu) {
530 per_cpu_ptr(mod->refptr, cpu)->incs = 0;
531 per_cpu_ptr(mod->refptr, cpu)->decs = 0;
532 }
Christoph Lametere1783a22010-01-05 15:34:50 +0900533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /* Hold reference count during initialization. */
Nick Piggin5fbfb182010-04-01 19:09:40 +1100535 __this_cpu_write(mod->refptr->incs, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 /* Backwards compatibility macros put refcount during init. */
537 mod->waiter = current;
538}
539
540/* modules using other modules */
541struct module_use
542{
543 struct list_head list;
544 struct module *module_which_uses;
545};
546
547/* Does a already use b? */
548static int already_uses(struct module *a, struct module *b)
549{
550 struct module_use *use;
551
552 list_for_each_entry(use, &b->modules_which_use_me, list) {
553 if (use->module_which_uses == a) {
554 DEBUGP("%s uses %s!\n", a->name, b->name);
555 return 1;
556 }
557 }
558 DEBUGP("%s does not use %s!\n", a->name, b->name);
559 return 0;
560}
561
562/* Module a uses b */
Tim Abbottc6b37802008-12-05 19:03:59 -0500563int use_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 struct module_use *use;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500566 int no_warn, err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (b == NULL || already_uses(a, b)) return 1;
569
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500570 /* If we're interrupted or time out, we fail. */
571 if (wait_event_interruptible_timeout(
572 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
573 30 * HZ) <= 0) {
574 printk("%s: gave up waiting for init of module %s.\n",
575 a->name, b->name);
576 return 0;
577 }
578
579 /* If strong_try_module_get() returned a different error, we fail. */
580 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return 0;
582
583 DEBUGP("Allocating new usage for %s.\n", a->name);
584 use = kmalloc(sizeof(*use), GFP_ATOMIC);
585 if (!use) {
586 printk("%s: out of memory loading\n", a->name);
587 module_put(b);
588 return 0;
589 }
590
591 use->module_which_uses = a;
592 list_add(&use->list, &b->modules_which_use_me);
Kay Sievers270a6c42007-01-18 13:26:15 +0100593 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return 1;
595}
Tim Abbottc6b37802008-12-05 19:03:59 -0500596EXPORT_SYMBOL_GPL(use_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598/* Clear the unload stuff of the module. */
599static void module_unload_free(struct module *mod)
600{
601 struct module *i;
602
603 list_for_each_entry(i, &modules, list) {
604 struct module_use *use;
605
606 list_for_each_entry(use, &i->modules_which_use_me, list) {
607 if (use->module_which_uses == mod) {
608 DEBUGP("%s unusing %s\n", mod->name, i->name);
609 module_put(i);
610 list_del(&use->list);
611 kfree(use);
Kay Sievers270a6c42007-01-18 13:26:15 +0100612 sysfs_remove_link(i->holders_dir, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /* There can be at most one match. */
614 break;
615 }
616 }
617 }
618}
619
620#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800621static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 int ret = (flags & O_TRUNC);
624 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800625 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return ret;
627}
628#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800629static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 return 0;
632}
633#endif /* CONFIG_MODULE_FORCE_UNLOAD */
634
635struct stopref
636{
637 struct module *mod;
638 int flags;
639 int *forced;
640};
641
642/* Whole machine is stopped with interrupts off when this runs. */
643static int __try_stop_module(void *_sref)
644{
645 struct stopref *sref = _sref;
646
Rusty Russellda39ba52008-07-22 19:24:25 -0500647 /* If it's not unused, quit unless we're forcing. */
648 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800649 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return -EWOULDBLOCK;
651 }
652
653 /* Mark it as dying. */
654 sref->mod->state = MODULE_STATE_GOING;
655 return 0;
656}
657
658static int try_stop_module(struct module *mod, int flags, int *forced)
659{
Rusty Russellda39ba52008-07-22 19:24:25 -0500660 if (flags & O_NONBLOCK) {
661 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500663 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500664 } else {
665 /* We don't need to stop the machine for this. */
666 mod->state = MODULE_STATE_GOING;
667 synchronize_sched();
668 return 0;
669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
672unsigned int module_refcount(struct module *mod)
673{
Nick Piggin5fbfb182010-04-01 19:09:40 +1100674 unsigned int incs = 0, decs = 0;
Eric Dumazet720eba32009-02-03 13:31:36 +1030675 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Eric Dumazet720eba32009-02-03 13:31:36 +1030677 for_each_possible_cpu(cpu)
Nick Piggin5fbfb182010-04-01 19:09:40 +1100678 decs += per_cpu_ptr(mod->refptr, cpu)->decs;
679 /*
680 * ensure the incs are added up after the decs.
681 * module_put ensures incs are visible before decs with smp_wmb.
682 *
683 * This 2-count scheme avoids the situation where the refcount
684 * for CPU0 is read, then CPU0 increments the module refcount,
685 * then CPU1 drops that refcount, then the refcount for CPU1 is
686 * read. We would record a decrement but not its corresponding
687 * increment so we would see a low count (disaster).
688 *
689 * Rare situation? But module_refcount can be preempted, and we
690 * might be tallying up 4096+ CPUs. So it is not impossible.
691 */
692 smp_rmb();
693 for_each_possible_cpu(cpu)
694 incs += per_cpu_ptr(mod->refptr, cpu)->incs;
695 return incs - decs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697EXPORT_SYMBOL(module_refcount);
698
699/* This exists whether we can unload or not */
700static void free_module(struct module *mod);
701
702static void wait_for_zero_refcount(struct module *mod)
703{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500704 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800705 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 for (;;) {
707 DEBUGP("Looking at refcount...\n");
708 set_current_state(TASK_UNINTERRUPTIBLE);
709 if (module_refcount(mod) == 0)
710 break;
711 schedule();
712 }
713 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800714 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100717SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
718 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800721 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 int ret, forced = 0;
723
Kees Cook3d433212009-04-02 15:49:29 -0700724 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800725 return -EPERM;
726
727 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
728 return -EFAULT;
729 name[MODULE_NAME_LEN-1] = '\0';
730
Tejun Heo3fc1f1e2010-05-06 18:49:20 +0200731 if (mutex_lock_interruptible(&module_mutex) != 0)
732 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 mod = find_module(name);
735 if (!mod) {
736 ret = -ENOENT;
737 goto out;
738 }
739
740 if (!list_empty(&mod->modules_which_use_me)) {
741 /* Other modules depend on us: get rid of them first. */
742 ret = -EWOULDBLOCK;
743 goto out;
744 }
745
746 /* Doing init or already dying? */
747 if (mod->state != MODULE_STATE_LIVE) {
748 /* FIXME: if (force), slam module count and wake up
749 waiter --RR */
750 DEBUGP("%s already dying\n", mod->name);
751 ret = -EBUSY;
752 goto out;
753 }
754
755 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700756 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800757 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (!forced) {
759 /* This module can't be removed */
760 ret = -EBUSY;
761 goto out;
762 }
763 }
764
765 /* Set this up before setting mod->state */
766 mod->waiter = current;
767
768 /* Stop the machine so refcounts can't move and disable module. */
769 ret = try_stop_module(mod, flags, &forced);
770 if (ret != 0)
771 goto out;
772
773 /* Never wait if forced. */
774 if (!forced && module_refcount(mod) != 0)
775 wait_for_zero_refcount(mod);
776
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200777 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 /* Final destruction now noone is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200779 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200781 blocking_notifier_call_chain(&module_notify_list,
782 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800783 async_synchronize_full();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200784 mutex_lock(&module_mutex);
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100785 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500786 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Jason Barone9d376f2009-02-05 11:51:38 -0500787 ddebug_remove_module(mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 free_module(mod);
789
790 out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800791 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return ret;
793}
794
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800795static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 struct module_use *use;
798 int printed_something = 0;
799
800 seq_printf(m, " %u ", module_refcount(mod));
801
802 /* Always include a trailing , so userspace can differentiate
803 between this and the old multi-field proc format. */
804 list_for_each_entry(use, &mod->modules_which_use_me, list) {
805 printed_something = 1;
806 seq_printf(m, "%s,", use->module_which_uses->name);
807 }
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (mod->init != NULL && mod->exit == NULL) {
810 printed_something = 1;
811 seq_printf(m, "[permanent],");
812 }
813
814 if (!printed_something)
815 seq_printf(m, "-");
816}
817
818void __symbol_put(const char *symbol)
819{
820 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Rusty Russell24da1cb2007-07-15 23:41:46 -0700822 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -0500823 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 BUG();
825 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700826 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
828EXPORT_SYMBOL(__symbol_put);
829
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930830/* Note this assumes addr is a function, which it currently always is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831void symbol_put_addr(void *addr)
832{
Trent Piepho5e376612006-05-15 09:44:06 -0700833 struct module *modaddr;
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930834 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930836 if (core_kernel_text(a))
Trent Piepho5e376612006-05-15 09:44:06 -0700837 return;
838
Rusty Russella6e6abd2009-03-31 13:05:31 -0600839 /* module_text_address is safe here: we're supposed to have reference
840 * to module from symbol_get, so it can't go away. */
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930841 modaddr = __module_text_address(a);
Rusty Russella6e6abd2009-03-31 13:05:31 -0600842 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -0700843 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845EXPORT_SYMBOL_GPL(symbol_put_addr);
846
847static ssize_t show_refcnt(struct module_attribute *mattr,
848 struct module *mod, char *buffer)
849{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400850 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
853static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900854 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 .show = show_refcnt,
856};
857
Al Virof6a57032006-10-18 01:47:25 -0400858void module_put(struct module *module)
859{
860 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +0900861 preempt_disable();
Nick Piggin5fbfb182010-04-01 19:09:40 +1100862 smp_wmb(); /* see comment in module_refcount */
863 __this_cpu_inc(module->refptr->decs);
Christoph Lametere1783a22010-01-05 15:34:50 +0900864
Li Zefanae832d12010-03-24 10:57:43 +0800865 trace_module_put(module, _RET_IP_);
Al Virof6a57032006-10-18 01:47:25 -0400866 /* Maybe they're waiting for us to drop reference? */
867 if (unlikely(!module_is_live(module)))
868 wake_up_process(module->waiter);
Christoph Lametere1783a22010-01-05 15:34:50 +0900869 preempt_enable();
Al Virof6a57032006-10-18 01:47:25 -0400870 }
871}
872EXPORT_SYMBOL(module_put);
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800875static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
877 /* We don't know the usage count, or what modules are using. */
878 seq_printf(m, " - -");
879}
880
881static inline void module_unload_free(struct module *mod)
882{
883}
884
Tim Abbottc6b37802008-12-05 19:03:59 -0500885int use_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500887 return strong_try_module_get(b) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
Tim Abbottc6b37802008-12-05 19:03:59 -0500889EXPORT_SYMBOL_GPL(use_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891static inline void module_unload_init(struct module *mod)
892{
893}
894#endif /* CONFIG_MODULE_UNLOAD */
895
Kay Sievers1f717402006-11-24 12:15:25 +0100896static ssize_t show_initstate(struct module_attribute *mattr,
897 struct module *mod, char *buffer)
898{
899 const char *state = "unknown";
900
901 switch (mod->state) {
902 case MODULE_STATE_LIVE:
903 state = "live";
904 break;
905 case MODULE_STATE_COMING:
906 state = "coming";
907 break;
908 case MODULE_STATE_GOING:
909 state = "going";
910 break;
911 }
912 return sprintf(buffer, "%s\n", state);
913}
914
915static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900916 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100917 .show = show_initstate,
918};
919
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800920static struct module_attribute *modinfo_attrs[] = {
921 &modinfo_version,
922 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100923 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800924#ifdef CONFIG_MODULE_UNLOAD
925 &refcnt,
926#endif
927 NULL,
928};
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930static const char vermagic[] = VERMAGIC_STRING;
931
Rusty Russellc6e665c2009-03-31 13:05:33 -0600932static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -0700933{
934#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -0700935 if (!test_taint(TAINT_FORCED_MODULE))
Rusty Russellc6e665c2009-03-31 13:05:33 -0600936 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
937 mod->name, reason);
Linus Torvalds826e4502008-05-04 17:04:16 -0700938 add_taint_module(mod, TAINT_FORCED_MODULE);
939 return 0;
940#else
941 return -ENOEXEC;
942#endif
943}
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945#ifdef CONFIG_MODVERSIONS
Rusty Russelld4703ae2009-12-15 16:28:32 -0600946/* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
947static unsigned long maybe_relocated(unsigned long crc,
948 const struct module *crc_owner)
949{
950#ifdef ARCH_RELOCATES_KCRCTAB
951 if (crc_owner == NULL)
952 return crc - (unsigned long)reloc_start;
953#endif
954 return crc;
955}
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957static int check_version(Elf_Shdr *sechdrs,
958 unsigned int versindex,
959 const char *symname,
960 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -0600961 const unsigned long *crc,
962 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
964 unsigned int i, num_versions;
965 struct modversion_info *versions;
966
967 /* Exporting module didn't supply crcs? OK, we're already tainted. */
968 if (!crc)
969 return 1;
970
Rusty Russella5dd6972008-05-09 16:24:21 +1000971 /* No versions at all? modprobe --force does this. */
972 if (versindex == 0)
973 return try_to_force_load(mod, symname) == 0;
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 versions = (void *) sechdrs[versindex].sh_addr;
976 num_versions = sechdrs[versindex].sh_size
977 / sizeof(struct modversion_info);
978
979 for (i = 0; i < num_versions; i++) {
980 if (strcmp(versions[i].name, symname) != 0)
981 continue;
982
Rusty Russelld4703ae2009-12-15 16:28:32 -0600983 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 DEBUGP("Found checksum %lX vs module %lX\n",
Rusty Russelld4703ae2009-12-15 16:28:32 -0600986 maybe_relocated(*crc, crc_owner), versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -0700987 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
Linus Torvalds826e4502008-05-04 17:04:16 -0700989
Rusty Russella5dd6972008-05-09 16:24:21 +1000990 printk(KERN_WARNING "%s: no symbol version for %s\n",
991 mod->name, symname);
992 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -0700993
994bad_version:
995 printk("%s: disagrees about version of symbol %s\n",
996 mod->name, symname);
997 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
999
1000static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1001 unsigned int versindex,
1002 struct module *mod)
1003{
1004 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Mike Frysinger6560dc12009-07-23 23:42:08 +09301006 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1007 &crc, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 BUG();
Rusty Russelld4703ae2009-12-15 16:28:32 -06001009 return check_version(sechdrs, versindex, "module_layout", mod, crc,
1010 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
Rusty Russell91e37a72008-05-09 16:25:28 +10001013/* First part is kernel version, which we ignore if module has crcs. */
1014static inline int same_magic(const char *amagic, const char *bmagic,
1015 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Rusty Russell91e37a72008-05-09 16:25:28 +10001017 if (has_crcs) {
1018 amagic += strcspn(amagic, " ");
1019 bmagic += strcspn(bmagic, " ");
1020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return strcmp(amagic, bmagic) == 0;
1022}
1023#else
1024static inline int check_version(Elf_Shdr *sechdrs,
1025 unsigned int versindex,
1026 const char *symname,
1027 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001028 const unsigned long *crc,
1029 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
1031 return 1;
1032}
1033
1034static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1035 unsigned int versindex,
1036 struct module *mod)
1037{
1038 return 1;
1039}
1040
Rusty Russell91e37a72008-05-09 16:25:28 +10001041static inline int same_magic(const char *amagic, const char *bmagic,
1042 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
1044 return strcmp(amagic, bmagic) == 0;
1045}
1046#endif /* CONFIG_MODVERSIONS */
1047
1048/* Resolve a symbol for this module. I.e. if we find one, record usage.
1049 Must be holding module_mutex. */
Tim Abbott414fd312008-12-05 19:03:56 -05001050static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
1051 unsigned int versindex,
1052 const char *name,
1053 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
1055 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001056 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 const unsigned long *crc;
1058
Tim Abbott414fd312008-12-05 19:03:56 -05001059 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001060 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Tim Abbott414fd312008-12-05 19:03:56 -05001061 /* use_module can fail due to OOM,
1062 or module initialization or unloading */
1063 if (sym) {
Rusty Russelld4703ae2009-12-15 16:28:32 -06001064 if (!check_version(sechdrs, versindex, name, mod, crc, owner)
1065 || !use_module(mod, owner))
Tim Abbott414fd312008-12-05 19:03:56 -05001066 sym = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
Tim Abbott414fd312008-12-05 19:03:56 -05001068 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071/*
1072 * /sys/module/foo/sections stuff
1073 * J. Corbet <corbet@lwn.net>
1074 */
Kay Sievers120fc3d2008-02-21 00:33:20 +01001075#if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
Ben Hutchings10b465a2009-12-19 14:43:01 +00001076
1077static inline bool sect_empty(const Elf_Shdr *sect)
1078{
1079 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1080}
1081
Rusty Russella58730c2008-03-13 09:03:44 +00001082struct module_sect_attr
1083{
1084 struct module_attribute mattr;
1085 char *name;
1086 unsigned long address;
1087};
1088
1089struct module_sect_attrs
1090{
1091 struct attribute_group grp;
1092 unsigned int nsections;
1093 struct module_sect_attr attrs[0];
1094};
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096static ssize_t module_sect_show(struct module_attribute *mattr,
1097 struct module *mod, char *buf)
1098{
1099 struct module_sect_attr *sattr =
1100 container_of(mattr, struct module_sect_attr, mattr);
1101 return sprintf(buf, "0x%lx\n", sattr->address);
1102}
1103
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001104static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1105{
Rusty Russella58730c2008-03-13 09:03:44 +00001106 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001107
1108 for (section = 0; section < sect_attrs->nsections; section++)
1109 kfree(sect_attrs->attrs[section].name);
1110 kfree(sect_attrs);
1111}
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113static void add_sect_attrs(struct module *mod, unsigned int nsect,
1114 char *secstrings, Elf_Shdr *sechdrs)
1115{
1116 unsigned int nloaded = 0, i, size[2];
1117 struct module_sect_attrs *sect_attrs;
1118 struct module_sect_attr *sattr;
1119 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 /* Count loaded sections and allocate structures */
1122 for (i = 0; i < nsect; i++)
Ben Hutchings10b465a2009-12-19 14:43:01 +00001123 if (!sect_empty(&sechdrs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 nloaded++;
1125 size[0] = ALIGN(sizeof(*sect_attrs)
1126 + nloaded * sizeof(sect_attrs->attrs[0]),
1127 sizeof(sect_attrs->grp.attrs[0]));
1128 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001129 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1130 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return;
1132
1133 /* Setup section attributes. */
1134 sect_attrs->grp.name = "sections";
1135 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1136
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001137 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 sattr = &sect_attrs->attrs[0];
1139 gattr = &sect_attrs->grp.attrs[0];
1140 for (i = 0; i < nsect; i++) {
Ben Hutchings10b465a2009-12-19 14:43:01 +00001141 if (sect_empty(&sechdrs[i]))
Helge Deller35dead42009-12-03 00:29:15 +01001142 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 sattr->address = sechdrs[i].sh_addr;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001144 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1145 GFP_KERNEL);
1146 if (sattr->name == NULL)
1147 goto out;
1148 sect_attrs->nsections++;
Eric W. Biederman361795b2010-02-12 13:41:56 -08001149 sysfs_attr_init(&sattr->mattr.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 sattr->mattr.show = module_sect_show;
1151 sattr->mattr.store = NULL;
1152 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 sattr->mattr.attr.mode = S_IRUGO;
1154 *(gattr++) = &(sattr++)->mattr.attr;
1155 }
1156 *gattr = NULL;
1157
1158 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1159 goto out;
1160
1161 mod->sect_attrs = sect_attrs;
1162 return;
1163 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001164 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
1167static void remove_sect_attrs(struct module *mod)
1168{
1169 if (mod->sect_attrs) {
1170 sysfs_remove_group(&mod->mkobj.kobj,
1171 &mod->sect_attrs->grp);
1172 /* We are positive that no one is using any sect attrs
1173 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001174 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 mod->sect_attrs = NULL;
1176 }
1177}
1178
Roland McGrath6d760132007-10-16 23:26:40 -07001179/*
1180 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1181 */
1182
1183struct module_notes_attrs {
1184 struct kobject *dir;
1185 unsigned int notes;
1186 struct bin_attribute attrs[0];
1187};
1188
1189static ssize_t module_notes_read(struct kobject *kobj,
1190 struct bin_attribute *bin_attr,
1191 char *buf, loff_t pos, size_t count)
1192{
1193 /*
1194 * The caller checked the pos and count against our size.
1195 */
1196 memcpy(buf, bin_attr->private + pos, count);
1197 return count;
1198}
1199
1200static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1201 unsigned int i)
1202{
1203 if (notes_attrs->dir) {
1204 while (i-- > 0)
1205 sysfs_remove_bin_file(notes_attrs->dir,
1206 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001207 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001208 }
1209 kfree(notes_attrs);
1210}
1211
1212static void add_notes_attrs(struct module *mod, unsigned int nsect,
1213 char *secstrings, Elf_Shdr *sechdrs)
1214{
1215 unsigned int notes, loaded, i;
1216 struct module_notes_attrs *notes_attrs;
1217 struct bin_attribute *nattr;
1218
Ingo Molnarea6bff32009-08-28 10:44:56 +02001219 /* failed to create section attributes, so can't create notes */
1220 if (!mod->sect_attrs)
1221 return;
1222
Roland McGrath6d760132007-10-16 23:26:40 -07001223 /* Count notes sections and allocate structures. */
1224 notes = 0;
1225 for (i = 0; i < nsect; i++)
Ben Hutchings10b465a2009-12-19 14:43:01 +00001226 if (!sect_empty(&sechdrs[i]) &&
Roland McGrath6d760132007-10-16 23:26:40 -07001227 (sechdrs[i].sh_type == SHT_NOTE))
1228 ++notes;
1229
1230 if (notes == 0)
1231 return;
1232
1233 notes_attrs = kzalloc(sizeof(*notes_attrs)
1234 + notes * sizeof(notes_attrs->attrs[0]),
1235 GFP_KERNEL);
1236 if (notes_attrs == NULL)
1237 return;
1238
1239 notes_attrs->notes = notes;
1240 nattr = &notes_attrs->attrs[0];
1241 for (loaded = i = 0; i < nsect; ++i) {
Ben Hutchings10b465a2009-12-19 14:43:01 +00001242 if (sect_empty(&sechdrs[i]))
Roland McGrath6d760132007-10-16 23:26:40 -07001243 continue;
1244 if (sechdrs[i].sh_type == SHT_NOTE) {
Eric W. Biederman361795b2010-02-12 13:41:56 -08001245 sysfs_bin_attr_init(nattr);
Roland McGrath6d760132007-10-16 23:26:40 -07001246 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1247 nattr->attr.mode = S_IRUGO;
1248 nattr->size = sechdrs[i].sh_size;
1249 nattr->private = (void *) sechdrs[i].sh_addr;
1250 nattr->read = module_notes_read;
1251 ++nattr;
1252 }
1253 ++loaded;
1254 }
1255
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001256 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001257 if (!notes_attrs->dir)
1258 goto out;
1259
1260 for (i = 0; i < notes; ++i)
1261 if (sysfs_create_bin_file(notes_attrs->dir,
1262 &notes_attrs->attrs[i]))
1263 goto out;
1264
1265 mod->notes_attrs = notes_attrs;
1266 return;
1267
1268 out:
1269 free_notes_attrs(notes_attrs, i);
1270}
1271
1272static void remove_notes_attrs(struct module *mod)
1273{
1274 if (mod->notes_attrs)
1275 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1276}
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1281 char *sectstrings, Elf_Shdr *sechdrs)
1282{
1283}
1284
1285static inline void remove_sect_attrs(struct module *mod)
1286{
1287}
Roland McGrath6d760132007-10-16 23:26:40 -07001288
1289static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1290 char *sectstrings, Elf_Shdr *sechdrs)
1291{
1292}
1293
1294static inline void remove_notes_attrs(struct module *mod)
1295{
1296}
Kay Sievers120fc3d2008-02-21 00:33:20 +01001297#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Randy Dunlapef665c12007-02-13 15:19:06 -08001299#ifdef CONFIG_SYSFS
1300int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001301{
1302 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001303 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001304 int error = 0;
1305 int i;
1306
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001307 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1308 (ARRAY_SIZE(modinfo_attrs) + 1)),
1309 GFP_KERNEL);
1310 if (!mod->modinfo_attrs)
1311 return -ENOMEM;
1312
1313 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001314 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1315 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001316 (attr->test && attr->test(mod))) {
1317 memcpy(temp_attr, attr, sizeof(*temp_attr));
Eric W. Biederman361795b2010-02-12 13:41:56 -08001318 sysfs_attr_init(&temp_attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001319 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1320 ++temp_attr;
1321 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001322 }
1323 return error;
1324}
1325
Randy Dunlapef665c12007-02-13 15:19:06 -08001326void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001327{
1328 struct module_attribute *attr;
1329 int i;
1330
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001331 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1332 /* pick a field to test for end of list */
1333 if (!attr->attr.name)
1334 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001335 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001336 if (attr->free)
1337 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001338 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001339 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001340}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Randy Dunlapef665c12007-02-13 15:19:06 -08001342int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
1344 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001345 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001347 if (!module_sysfs_initialized) {
1348 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001349 mod->name);
1350 err = -EINVAL;
1351 goto out;
1352 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001353
1354 kobj = kset_find_obj(module_kset, mod->name);
1355 if (kobj) {
1356 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1357 kobject_put(kobj);
1358 err = -EINVAL;
1359 goto out;
1360 }
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001363
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001364 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1365 mod->mkobj.kobj.kset = module_kset;
1366 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1367 "%s", mod->name);
1368 if (err)
1369 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001370
Kay Sievers97c146e2007-11-29 23:46:11 +01001371 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001372out:
1373 return err;
1374}
1375
Randy Dunlapef665c12007-02-13 15:19:06 -08001376int mod_sysfs_setup(struct module *mod,
Kay Sievers270a6c42007-01-18 13:26:15 +01001377 struct kernel_param *kparam,
1378 unsigned int num_params)
1379{
1380 int err;
1381
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001382 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001383 if (!mod->holders_dir) {
1384 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001385 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001386 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 err = module_param_sysfs_setup(mod, kparam, num_params);
1389 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001390 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Matt Domschc988d2b2005-06-23 22:05:15 -07001392 err = module_add_modinfo_attrs(mod);
1393 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001394 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001395
Kay Sieverse17e0f52006-11-24 12:15:25 +01001396 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return 0;
1398
Kay Sieverse17e0f52006-11-24 12:15:25 +01001399out_unreg_param:
1400 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001401out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001402 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001403out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001404 kobject_put(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 return err;
1406}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001407
1408static void mod_sysfs_fini(struct module *mod)
1409{
1410 kobject_put(&mod->mkobj.kobj);
1411}
1412
1413#else /* CONFIG_SYSFS */
1414
1415static void mod_sysfs_fini(struct module *mod)
1416{
1417}
1418
1419#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421static void mod_kobject_remove(struct module *mod)
1422{
Matt Domschc988d2b2005-06-23 22:05:15 -07001423 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001425 kobject_put(mod->mkobj.drivers_dir);
1426 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001427 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428}
1429
1430/*
1431 * unlink the module with the whole machine is stopped with interrupts off
1432 * - this defends against kallsyms not taking locks
1433 */
1434static int __unlink_module(void *_mod)
1435{
1436 struct module *mod = _mod;
1437 list_del(&mod->list);
1438 return 0;
1439}
1440
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001441/* Free a module, remove from lists, etc (must hold module_mutex). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442static void free_module(struct module *mod)
1443{
Li Zefan7ead8b82009-08-17 16:56:28 +08001444 trace_module_free(mod);
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 /* Delete from various lists */
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001447 stop_machine(__unlink_module, mod, NULL);
Roland McGrath6d760132007-10-16 23:26:40 -07001448 remove_notes_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 remove_sect_attrs(mod);
1450 mod_kobject_remove(mod);
1451
1452 /* Arch-specific cleanup. */
1453 module_arch_cleanup(mod);
1454
1455 /* Module unload stuff */
1456 module_unload_free(mod);
1457
Rusty Russelle180a6b2009-03-31 13:05:29 -06001458 /* Free any allocated parameters. */
1459 destroy_params(mod->kp, mod->num_kp);
1460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 /* This may be NULL, but that's OK */
1462 module_free(mod, mod->module_init);
1463 kfree(mod->args);
Tejun Heo259354d2010-03-10 18:56:10 +09001464 percpu_modfree(mod);
Christoph Lametere1783a22010-01-05 15:34:50 +09001465#if defined(CONFIG_MODULE_UNLOAD)
Eric Dumazet720eba32009-02-03 13:31:36 +10301466 if (mod->refptr)
Christoph Lametere1783a22010-01-05 15:34:50 +09001467 free_percpu(mod->refptr);
Eric Dumazet720eba32009-02-03 13:31:36 +10301468#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001469 /* Free lock-classes: */
1470 lockdep_free_key_range(mod->module_core, mod->core_size);
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 /* Finally, free the core (containing the module structure) */
1473 module_free(mod, mod->module_core);
Bernd Schmidteb8cdec2009-09-21 17:03:57 -07001474
1475#ifdef CONFIG_MPU
1476 update_protections(current->mm);
1477#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
1480void *__symbol_get(const char *symbol)
1481{
1482 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001483 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Rusty Russell24da1cb2007-07-15 23:41:46 -07001485 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001486 sym = find_symbol(symbol, &owner, NULL, true, true);
1487 if (sym && strong_try_module_get(owner))
1488 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001489 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Tim Abbott414fd312008-12-05 19:03:56 -05001491 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492}
1493EXPORT_SYMBOL_GPL(__symbol_get);
1494
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001495/*
1496 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001497 * in the kernel or in some other module's exported symbol table.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001498 */
1499static int verify_export_symbols(struct module *mod)
1500{
Rusty Russellb2111042008-05-01 21:15:00 -05001501 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001502 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001503 const struct kernel_symbol *s;
1504 struct {
1505 const struct kernel_symbol *sym;
1506 unsigned int num;
1507 } arr[] = {
1508 { mod->syms, mod->num_syms },
1509 { mod->gpl_syms, mod->num_gpl_syms },
1510 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001511#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001512 { mod->unused_syms, mod->num_unused_syms },
1513 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001514#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001515 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001516
Rusty Russellb2111042008-05-01 21:15:00 -05001517 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1518 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Tim Abbott414fd312008-12-05 19:03:56 -05001519 if (find_symbol(s->name, &owner, NULL, true, false)) {
Rusty Russellb2111042008-05-01 21:15:00 -05001520 printk(KERN_ERR
1521 "%s: exports duplicate symbol %s"
1522 " (owned by %s)\n",
1523 mod->name, s->name, module_name(owner));
1524 return -ENOEXEC;
1525 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001526 }
Rusty Russellb2111042008-05-01 21:15:00 -05001527 }
1528 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001529}
1530
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001531/* Change all symbols so that st_value encodes the pointer directly. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532static int simplify_symbols(Elf_Shdr *sechdrs,
1533 unsigned int symindex,
1534 const char *strtab,
1535 unsigned int versindex,
1536 unsigned int pcpuindex,
1537 struct module *mod)
1538{
1539 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1540 unsigned long secbase;
1541 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1542 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05001543 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545 for (i = 1; i < n; i++) {
1546 switch (sym[i].st_shndx) {
1547 case SHN_COMMON:
1548 /* We compiled with -fno-common. These are not
1549 supposed to happen. */
1550 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1551 printk("%s: please compile with -fno-common\n",
1552 mod->name);
1553 ret = -ENOEXEC;
1554 break;
1555
1556 case SHN_ABS:
1557 /* Don't need to do anything */
1558 DEBUGP("Absolute symbol: 0x%08lx\n",
1559 (long)sym[i].st_value);
1560 break;
1561
1562 case SHN_UNDEF:
Tim Abbott414fd312008-12-05 19:03:56 -05001563 ksym = resolve_symbol(sechdrs, versindex,
1564 strtab + sym[i].st_name, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 /* Ok if resolved. */
Tim Abbott414fd312008-12-05 19:03:56 -05001566 if (ksym) {
1567 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 break;
Tim Abbott414fd312008-12-05 19:03:56 -05001569 }
1570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 /* Ok if weak. */
1572 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1573 break;
1574
1575 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1576 mod->name, strtab + sym[i].st_name);
1577 ret = -ENOENT;
1578 break;
1579
1580 default:
1581 /* Divert to percpu allocation if a percpu var. */
1582 if (sym[i].st_shndx == pcpuindex)
Tejun Heo259354d2010-03-10 18:56:10 +09001583 secbase = (unsigned long)mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 else
1585 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1586 sym[i].st_value += secbase;
1587 break;
1588 }
1589 }
1590
1591 return ret;
1592}
1593
Helge Deller088af9a2008-12-31 12:31:18 +01001594/* Additional bytes needed by arch in front of individual sections */
1595unsigned int __weak arch_mod_section_prepend(struct module *mod,
1596 unsigned int section)
1597{
1598 /* default implementation just returns zero */
1599 return 0;
1600}
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01001603static long get_offset(struct module *mod, unsigned int *size,
1604 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
1606 long ret;
1607
Helge Deller088af9a2008-12-31 12:31:18 +01001608 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1610 *size = ret + sechdr->sh_size;
1611 return ret;
1612}
1613
1614/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1615 might -- code, read-only data, read-write data, small data. Tally
1616 sizes, and place the offsets into sh_entsize fields: high bit means it
1617 belongs in init. */
1618static void layout_sections(struct module *mod,
1619 const Elf_Ehdr *hdr,
1620 Elf_Shdr *sechdrs,
1621 const char *secstrings)
1622{
1623 static unsigned long const masks[][2] = {
1624 /* NOTE: all executable code must be the first section
1625 * in this array; otherwise modify the text_size
1626 * finder in the two loops below */
1627 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1628 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1629 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1630 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1631 };
1632 unsigned int m, i;
1633
1634 for (i = 0; i < hdr->e_shnum; i++)
1635 sechdrs[i].sh_entsize = ~0UL;
1636
1637 DEBUGP("Core section allocation order:\n");
1638 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1639 for (i = 0; i < hdr->e_shnum; ++i) {
1640 Elf_Shdr *s = &sechdrs[i];
1641
1642 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1643 || (s->sh_flags & masks[m][1])
1644 || s->sh_entsize != ~0UL
Rusty Russell49502672009-03-31 13:05:36 -06001645 || strstarts(secstrings + s->sh_name, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001647 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 DEBUGP("\t%s\n", secstrings + s->sh_name);
1649 }
1650 if (m == 0)
1651 mod->core_text_size = mod->core_size;
1652 }
1653
1654 DEBUGP("Init section allocation order:\n");
1655 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1656 for (i = 0; i < hdr->e_shnum; ++i) {
1657 Elf_Shdr *s = &sechdrs[i];
1658
1659 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1660 || (s->sh_flags & masks[m][1])
1661 || s->sh_entsize != ~0UL
Rusty Russell49502672009-03-31 13:05:36 -06001662 || !strstarts(secstrings + s->sh_name, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001664 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 | INIT_OFFSET_MASK);
1666 DEBUGP("\t%s\n", secstrings + s->sh_name);
1667 }
1668 if (m == 0)
1669 mod->init_text_size = mod->init_size;
1670 }
1671}
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673static void set_license(struct module *mod, const char *license)
1674{
1675 if (!license)
1676 license = "unspecified";
1677
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001678 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07001679 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001680 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001681 "kernel.\n", mod->name, license);
1682 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 }
1684}
1685
1686/* Parse tag=value strings from .modinfo section */
1687static char *next_string(char *string, unsigned long *secsize)
1688{
1689 /* Skip non-zero chars */
1690 while (string[0]) {
1691 string++;
1692 if ((*secsize)-- <= 1)
1693 return NULL;
1694 }
1695
1696 /* Skip any zero padding. */
1697 while (!string[0]) {
1698 string++;
1699 if ((*secsize)-- <= 1)
1700 return NULL;
1701 }
1702 return string;
1703}
1704
1705static char *get_modinfo(Elf_Shdr *sechdrs,
1706 unsigned int info,
1707 const char *tag)
1708{
1709 char *p;
1710 unsigned int taglen = strlen(tag);
1711 unsigned long size = sechdrs[info].sh_size;
1712
1713 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1714 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1715 return p + taglen + 1;
1716 }
1717 return NULL;
1718}
1719
Matt Domschc988d2b2005-06-23 22:05:15 -07001720static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1721 unsigned int infoindex)
1722{
1723 struct module_attribute *attr;
1724 int i;
1725
1726 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1727 if (attr->setup)
1728 attr->setup(mod,
1729 get_modinfo(sechdrs,
1730 infoindex,
1731 attr->attr.name));
1732 }
1733}
Matt Domschc988d2b2005-06-23 22:05:15 -07001734
Rusty Russella263f772009-09-25 00:32:58 -06001735static void free_modinfo(struct module *mod)
1736{
1737 struct module_attribute *attr;
1738 int i;
1739
1740 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1741 if (attr->free)
1742 attr->free(mod);
1743 }
1744}
1745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01001747
1748/* lookup symbol in given range of kernel_symbols */
1749static const struct kernel_symbol *lookup_symbol(const char *name,
1750 const struct kernel_symbol *start,
1751 const struct kernel_symbol *stop)
1752{
1753 const struct kernel_symbol *ks = start;
1754 for (; ks < stop; ks++)
1755 if (strcmp(ks->name, name) == 0)
1756 return ks;
1757 return NULL;
1758}
1759
Tim Abbottca4787b2009-01-05 08:40:10 -06001760static int is_exported(const char *name, unsigned long value,
1761 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762{
Tim Abbottca4787b2009-01-05 08:40:10 -06001763 const struct kernel_symbol *ks;
1764 if (!mod)
1765 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001766 else
Tim Abbottca4787b2009-01-05 08:40:10 -06001767 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
1768 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769}
1770
1771/* As per nm */
1772static char elf_type(const Elf_Sym *sym,
1773 Elf_Shdr *sechdrs,
1774 const char *secstrings,
1775 struct module *mod)
1776{
1777 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1778 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1779 return 'v';
1780 else
1781 return 'w';
1782 }
1783 if (sym->st_shndx == SHN_UNDEF)
1784 return 'U';
1785 if (sym->st_shndx == SHN_ABS)
1786 return 'a';
1787 if (sym->st_shndx >= SHN_LORESERVE)
1788 return '?';
1789 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1790 return 't';
1791 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1792 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1793 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1794 return 'r';
1795 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1796 return 'g';
1797 else
1798 return 'd';
1799 }
1800 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1801 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1802 return 's';
1803 else
1804 return 'b';
1805 }
Rusty Russell49502672009-03-31 13:05:36 -06001806 if (strstarts(secstrings + sechdrs[sym->st_shndx].sh_name, ".debug"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return 'n';
1808 return '?';
1809}
1810
Jan Beulich4a496222009-07-06 14:50:42 +01001811static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
1812 unsigned int shnum)
1813{
1814 const Elf_Shdr *sec;
1815
1816 if (src->st_shndx == SHN_UNDEF
1817 || src->st_shndx >= shnum
1818 || !src->st_name)
1819 return false;
1820
1821 sec = sechdrs + src->st_shndx;
1822 if (!(sec->sh_flags & SHF_ALLOC)
1823#ifndef CONFIG_KALLSYMS_ALL
1824 || !(sec->sh_flags & SHF_EXECINSTR)
1825#endif
1826 || (sec->sh_entsize & INIT_OFFSET_MASK))
1827 return false;
1828
1829 return true;
1830}
1831
1832static unsigned long layout_symtab(struct module *mod,
1833 Elf_Shdr *sechdrs,
1834 unsigned int symindex,
Jan Beulich554bdfe2009-07-06 14:51:44 +01001835 unsigned int strindex,
Jan Beulich4a496222009-07-06 14:50:42 +01001836 const Elf_Ehdr *hdr,
Jan Beulich554bdfe2009-07-06 14:51:44 +01001837 const char *secstrings,
1838 unsigned long *pstroffs,
1839 unsigned long *strmap)
Jan Beulich4a496222009-07-06 14:50:42 +01001840{
1841 unsigned long symoffs;
1842 Elf_Shdr *symsect = sechdrs + symindex;
Jan Beulich554bdfe2009-07-06 14:51:44 +01001843 Elf_Shdr *strsect = sechdrs + strindex;
Jan Beulich4a496222009-07-06 14:50:42 +01001844 const Elf_Sym *src;
Jan Beulich554bdfe2009-07-06 14:51:44 +01001845 const char *strtab;
Jan Beulich4a496222009-07-06 14:50:42 +01001846 unsigned int i, nsrc, ndst;
1847
1848 /* Put symbol section at end of init part of module. */
1849 symsect->sh_flags |= SHF_ALLOC;
1850 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
1851 symindex) | INIT_OFFSET_MASK;
1852 DEBUGP("\t%s\n", secstrings + symsect->sh_name);
1853
1854 src = (void *)hdr + symsect->sh_offset;
1855 nsrc = symsect->sh_size / sizeof(*src);
Jan Beulich554bdfe2009-07-06 14:51:44 +01001856 strtab = (void *)hdr + strsect->sh_offset;
Jan Beulich4a496222009-07-06 14:50:42 +01001857 for (ndst = i = 1; i < nsrc; ++i, ++src)
Jan Beulich554bdfe2009-07-06 14:51:44 +01001858 if (is_core_symbol(src, sechdrs, hdr->e_shnum)) {
1859 unsigned int j = src->st_name;
1860
1861 while(!__test_and_set_bit(j, strmap) && strtab[j])
1862 ++j;
Jan Beulich4a496222009-07-06 14:50:42 +01001863 ++ndst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01001864 }
Jan Beulich4a496222009-07-06 14:50:42 +01001865
1866 /* Append room for core symbols at end of core part. */
1867 symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
1868 mod->core_size = symoffs + ndst * sizeof(Elf_Sym);
1869
Jan Beulich554bdfe2009-07-06 14:51:44 +01001870 /* Put string table section at end of init part of module. */
1871 strsect->sh_flags |= SHF_ALLOC;
1872 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
1873 strindex) | INIT_OFFSET_MASK;
1874 DEBUGP("\t%s\n", secstrings + strsect->sh_name);
1875
1876 /* Append room for core symbols' strings at end of core part. */
1877 *pstroffs = mod->core_size;
1878 __set_bit(0, strmap);
1879 mod->core_size += bitmap_weight(strmap, strsect->sh_size);
1880
Jan Beulich4a496222009-07-06 14:50:42 +01001881 return symoffs;
1882}
1883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884static void add_kallsyms(struct module *mod,
1885 Elf_Shdr *sechdrs,
Jan Beulich4a496222009-07-06 14:50:42 +01001886 unsigned int shnum,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 unsigned int symindex,
1888 unsigned int strindex,
Jan Beulich4a496222009-07-06 14:50:42 +01001889 unsigned long symoffs,
Jan Beulich554bdfe2009-07-06 14:51:44 +01001890 unsigned long stroffs,
1891 const char *secstrings,
1892 unsigned long *strmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
Jan Beulich4a496222009-07-06 14:50:42 +01001894 unsigned int i, ndst;
1895 const Elf_Sym *src;
1896 Elf_Sym *dst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01001897 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1900 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1901 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1902
1903 /* Set types up while we still have access to sections. */
1904 for (i = 0; i < mod->num_symtab; i++)
1905 mod->symtab[i].st_info
1906 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
Jan Beulich4a496222009-07-06 14:50:42 +01001907
1908 mod->core_symtab = dst = mod->module_core + symoffs;
1909 src = mod->symtab;
1910 *dst = *src;
1911 for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
1912 if (!is_core_symbol(src, sechdrs, shnum))
1913 continue;
1914 dst[ndst] = *src;
Jan Beulich554bdfe2009-07-06 14:51:44 +01001915 dst[ndst].st_name = bitmap_weight(strmap, dst[ndst].st_name);
Jan Beulich4a496222009-07-06 14:50:42 +01001916 ++ndst;
1917 }
1918 mod->core_num_syms = ndst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01001919
1920 mod->core_strtab = s = mod->module_core + stroffs;
1921 for (*s = 0, i = 1; i < sechdrs[strindex].sh_size; ++i)
1922 if (test_bit(i, strmap))
1923 *++s = mod->strtab[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924}
1925#else
Jan Beulich4a496222009-07-06 14:50:42 +01001926static inline unsigned long layout_symtab(struct module *mod,
1927 Elf_Shdr *sechdrs,
1928 unsigned int symindex,
Jan Beulich554bdfe2009-07-06 14:51:44 +01001929 unsigned int strindex,
Paul Mundt3ae91c22009-10-01 15:43:54 -07001930 const Elf_Ehdr *hdr,
Jan Beulich554bdfe2009-07-06 14:51:44 +01001931 const char *secstrings,
1932 unsigned long *pstroffs,
1933 unsigned long *strmap)
Jan Beulich4a496222009-07-06 14:50:42 +01001934{
Paul Mundt3ae91c22009-10-01 15:43:54 -07001935 return 0;
Jan Beulich4a496222009-07-06 14:50:42 +01001936}
Paul Mundt3ae91c22009-10-01 15:43:54 -07001937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938static inline void add_kallsyms(struct module *mod,
1939 Elf_Shdr *sechdrs,
Jan Beulich4a496222009-07-06 14:50:42 +01001940 unsigned int shnum,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 unsigned int symindex,
1942 unsigned int strindex,
Jan Beulich4a496222009-07-06 14:50:42 +01001943 unsigned long symoffs,
Jan Beulich554bdfe2009-07-06 14:51:44 +01001944 unsigned long stroffs,
1945 const char *secstrings,
1946 const unsigned long *strmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
1948}
1949#endif /* CONFIG_KALLSYMS */
1950
Jason Barone9d376f2009-02-05 11:51:38 -05001951static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05001952{
Jason Barone9d376f2009-02-05 11:51:38 -05001953#ifdef CONFIG_DYNAMIC_DEBUG
1954 if (ddebug_add_module(debug, num, debug->modname))
1955 printk(KERN_ERR "dynamic debug error adding module: %s\n",
1956 debug->modname);
1957#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05001958}
Jason Baron346e15b2008-08-12 16:46:19 -04001959
Rusty Russell3a642e92008-07-22 19:24:28 -05001960static void *module_alloc_update_bounds(unsigned long size)
1961{
1962 void *ret = module_alloc(size);
1963
1964 if (ret) {
1965 /* Update module bounds. */
1966 if ((unsigned long)ret < module_addr_min)
1967 module_addr_min = (unsigned long)ret;
1968 if ((unsigned long)ret + size > module_addr_max)
1969 module_addr_max = (unsigned long)ret + size;
1970 }
1971 return ret;
1972}
1973
Catalin Marinas4f2294b2009-06-11 13:23:20 +01001974#ifdef CONFIG_DEBUG_KMEMLEAK
1975static void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
1976 Elf_Shdr *sechdrs, char *secstrings)
1977{
1978 unsigned int i;
1979
1980 /* only scan the sections containing data */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001981 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01001982
1983 for (i = 1; i < hdr->e_shnum; i++) {
1984 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1985 continue;
1986 if (strncmp(secstrings + sechdrs[i].sh_name, ".data", 5) != 0
1987 && strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) != 0)
1988 continue;
1989
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001990 kmemleak_scan_area((void *)sechdrs[i].sh_addr,
Catalin Marinas4f2294b2009-06-11 13:23:20 +01001991 sechdrs[i].sh_size, GFP_KERNEL);
1992 }
1993}
1994#else
1995static inline void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
1996 Elf_Shdr *sechdrs, char *secstrings)
1997{
1998}
1999#endif
2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001/* Allocate and load the module: note that size of section 0 is always
2002 zero, and we rely on this for optional sections. */
Linus Torvaldsffb4ba72008-08-25 11:10:26 -07002003static noinline struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 unsigned long len,
2005 const char __user *uargs)
2006{
2007 Elf_Ehdr *hdr;
2008 Elf_Shdr *sechdrs;
2009 char *secstrings, *args, *modmagic, *strtab = NULL;
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002010 char *staging;
Andrew Morton84860f92006-06-28 04:26:46 -07002011 unsigned int i;
2012 unsigned int symindex = 0;
2013 unsigned int strindex = 0;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002014 unsigned int modindex, versindex, infoindex, pcpuindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 struct module *mod;
2016 long err = 0;
Tejun Heo259354d2010-03-10 18:56:10 +09002017 void *ptr = NULL; /* Stops spurious gcc warning */
Jan Beulich554bdfe2009-07-06 14:51:44 +01002018 unsigned long symoffs, stroffs, *strmap;
Paul Mundt3ae91c22009-10-01 15:43:54 -07002019
Thomas Koeller378bac82005-09-06 15:17:11 -07002020 mm_segment_t old_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
2023 umod, len, uargs);
2024 if (len < sizeof(*hdr))
2025 return ERR_PTR(-ENOEXEC);
2026
2027 /* Suck in entire file: we'll want most of it. */
2028 /* vmalloc barfs on "unusual" numbers. Check here */
2029 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
2030 return ERR_PTR(-ENOMEM);
Heiko Carstens9e018922008-12-22 12:36:31 +01002031
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 if (copy_from_user(hdr, umod, len) != 0) {
2033 err = -EFAULT;
2034 goto free_hdr;
2035 }
2036
2037 /* Sanity checks against insmoding binaries or wrong arch,
2038 weird elf version */
Cyrill Gorcunovc4ea6fc2008-05-14 16:27:29 -07002039 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 || hdr->e_type != ET_REL
2041 || !elf_check_arch(hdr)
2042 || hdr->e_shentsize != sizeof(*sechdrs)) {
2043 err = -ENOEXEC;
2044 goto free_hdr;
2045 }
2046
2047 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
2048 goto truncated;
2049
2050 /* Convenience variables */
2051 sechdrs = (void *)hdr + hdr->e_shoff;
2052 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
2053 sechdrs[0].sh_addr = 0;
2054
2055 for (i = 1; i < hdr->e_shnum; i++) {
2056 if (sechdrs[i].sh_type != SHT_NOBITS
2057 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
2058 goto truncated;
2059
2060 /* Mark all sections sh_addr with their address in the
2061 temporary image. */
2062 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
2063
2064 /* Internal symbols and strings. */
2065 if (sechdrs[i].sh_type == SHT_SYMTAB) {
2066 symindex = i;
2067 strindex = sechdrs[i].sh_link;
2068 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
2069 }
2070#ifndef CONFIG_MODULE_UNLOAD
2071 /* Don't load .exit sections */
Rusty Russell49502672009-03-31 13:05:36 -06002072 if (strstarts(secstrings+sechdrs[i].sh_name, ".exit"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
2074#endif
2075 }
2076
2077 modindex = find_sec(hdr, sechdrs, secstrings,
2078 ".gnu.linkonce.this_module");
2079 if (!modindex) {
2080 printk(KERN_WARNING "No module found in object\n");
2081 err = -ENOEXEC;
2082 goto free_hdr;
2083 }
Rusty Russell5e458cc2008-10-22 10:00:13 -05002084 /* This is temporary: point mod into copy of data. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 mod = (void *)sechdrs[modindex].sh_addr;
2086
2087 if (symindex == 0) {
2088 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2089 mod->name);
2090 err = -ENOEXEC;
2091 goto free_hdr;
2092 }
2093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
2095 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
2096 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
2097
Rusty Russellea01e792008-03-13 09:02:17 +00002098 /* Don't keep modinfo and version sections. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russellea01e792008-03-13 09:02:17 +00002100 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
2102 /* Check module struct version now, before we try to use module. */
2103 if (!check_modstruct_version(sechdrs, versindex, mod)) {
2104 err = -ENOEXEC;
2105 goto free_hdr;
2106 }
2107
2108 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
2109 /* This is allowed: modprobe --force will invalidate it. */
2110 if (!modmagic) {
Rusty Russellc6e665c2009-03-31 13:05:33 -06002111 err = try_to_force_load(mod, "bad vermagic");
Linus Torvalds826e4502008-05-04 17:04:16 -07002112 if (err)
2113 goto free_hdr;
Rusty Russell91e37a72008-05-09 16:25:28 +10002114 } else if (!same_magic(modmagic, vermagic, versindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2116 mod->name, modmagic, vermagic);
2117 err = -ENOEXEC;
2118 goto free_hdr;
2119 }
2120
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002121 staging = get_modinfo(sechdrs, infoindex, "staging");
2122 if (staging) {
2123 add_taint_module(mod, TAINT_CRAP);
2124 printk(KERN_WARNING "%s: module is from the staging directory,"
2125 " the quality is unknown, you have been warned.\n",
2126 mod->name);
2127 }
2128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 /* Now copy in args */
Davi Arnaut24277dd2006-03-24 03:18:43 -08002130 args = strndup_user(uargs, ~0UL >> 1);
2131 if (IS_ERR(args)) {
2132 err = PTR_ERR(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 goto free_hdr;
2134 }
Andrew Morton8e08b752006-02-07 12:58:45 -08002135
Jan Beulich554bdfe2009-07-06 14:51:44 +01002136 strmap = kzalloc(BITS_TO_LONGS(sechdrs[strindex].sh_size)
2137 * sizeof(long), GFP_KERNEL);
2138 if (!strmap) {
2139 err = -ENOMEM;
2140 goto free_mod;
2141 }
2142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if (find_module(mod->name)) {
2144 err = -EEXIST;
2145 goto free_mod;
2146 }
2147
2148 mod->state = MODULE_STATE_COMING;
2149
2150 /* Allow arches to frob section contents and sizes. */
2151 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
2152 if (err < 0)
2153 goto free_mod;
2154
2155 if (pcpuindex) {
2156 /* We have a special allocation for this section. */
Tejun Heo259354d2010-03-10 18:56:10 +09002157 err = percpu_modalloc(mod, sechdrs[pcpuindex].sh_size,
2158 sechdrs[pcpuindex].sh_addralign);
2159 if (err)
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002160 goto free_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
2163
2164 /* Determine total sizes, and put offsets in sh_entsize. For now
2165 this is done generically; there doesn't appear to be any
2166 special cases for the architectures. */
2167 layout_sections(mod, hdr, sechdrs, secstrings);
Jan Beulich554bdfe2009-07-06 14:51:44 +01002168 symoffs = layout_symtab(mod, sechdrs, symindex, strindex, hdr,
2169 secstrings, &stroffs, strmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
2171 /* Do the allocs. */
Rusty Russell3a642e92008-07-22 19:24:28 -05002172 ptr = module_alloc_update_bounds(mod->core_size);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002173 /*
2174 * The pointer to this block is stored in the module structure
2175 * which is inside the block. Just mark it as not being a
2176 * leak.
2177 */
2178 kmemleak_not_leak(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 if (!ptr) {
2180 err = -ENOMEM;
2181 goto free_percpu;
2182 }
2183 memset(ptr, 0, mod->core_size);
2184 mod->module_core = ptr;
2185
Rusty Russell3a642e92008-07-22 19:24:28 -05002186 ptr = module_alloc_update_bounds(mod->init_size);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002187 /*
2188 * The pointer to this block is stored in the module structure
2189 * which is inside the block. This block doesn't need to be
2190 * scanned as it contains data and code that will be freed
2191 * after the module is initialized.
2192 */
2193 kmemleak_ignore(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if (!ptr && mod->init_size) {
2195 err = -ENOMEM;
2196 goto free_core;
2197 }
2198 memset(ptr, 0, mod->init_size);
2199 mod->module_init = ptr;
2200
2201 /* Transfer each section which specifies SHF_ALLOC */
2202 DEBUGP("final section addresses:\n");
2203 for (i = 0; i < hdr->e_shnum; i++) {
2204 void *dest;
2205
2206 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2207 continue;
2208
2209 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2210 dest = mod->module_init
2211 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2212 else
2213 dest = mod->module_core + sechdrs[i].sh_entsize;
2214
2215 if (sechdrs[i].sh_type != SHT_NOBITS)
2216 memcpy(dest, (void *)sechdrs[i].sh_addr,
2217 sechdrs[i].sh_size);
2218 /* Update sh_addr to point to copy in image. */
2219 sechdrs[i].sh_addr = (unsigned long)dest;
2220 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2221 }
2222 /* Module has been moved. */
2223 mod = (void *)sechdrs[modindex].sh_addr;
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002224 kmemleak_load_module(mod, hdr, sechdrs, secstrings);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Christoph Lametere1783a22010-01-05 15:34:50 +09002226#if defined(CONFIG_MODULE_UNLOAD)
2227 mod->refptr = alloc_percpu(struct module_ref);
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002228 if (!mod->refptr) {
2229 err = -ENOMEM;
2230 goto free_init;
2231 }
2232#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 /* Now we've moved module, initialize linked lists, etc. */
2234 module_unload_init(mod);
2235
Kay Sievers97c146e2007-11-29 23:46:11 +01002236 /* add kobject, so we can reference it. */
Akinobu Mitad58ae672007-10-16 23:30:27 -07002237 err = mod_sysfs_init(mod);
2238 if (err)
Kay Sievers97c146e2007-11-29 23:46:11 +01002239 goto free_unload;
Kay Sievers270a6c42007-01-18 13:26:15 +01002240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 /* Set up license info based on the info section */
2242 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2243
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002244 /*
2245 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2246 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2247 * using GPL-only symbols it needs.
2248 */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002249 if (strcmp(mod->name, "ndiswrapper") == 0)
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002250 add_taint(TAINT_PROPRIETARY_MODULE);
2251
2252 /* driverloader was caught wrongly pretending to be under GPL */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002253 if (strcmp(mod->name, "driverloader") == 0)
2254 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Dave Jones9841d61d2006-01-08 01:03:41 -08002255
Matt Domschc988d2b2005-06-23 22:05:15 -07002256 /* Set up MODINFO_ATTR fields */
2257 setup_modinfo(mod, sechdrs, infoindex);
Matt Domschc988d2b2005-06-23 22:05:15 -07002258
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 /* Fix up syms, so that st_value is a pointer to location. */
2260 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2261 mod);
2262 if (err < 0)
2263 goto cleanup;
2264
Rusty Russell5e458cc2008-10-22 10:00:13 -05002265 /* Now we've got everything in the final locations, we can
2266 * find optional sections. */
Rusty Russelle180a6b2009-03-31 13:05:29 -06002267 mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
2268 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell5e458cc2008-10-22 10:00:13 -05002269 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2270 sizeof(*mod->syms), &mod->num_syms);
2271 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2272 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2273 sizeof(*mod->gpl_syms),
2274 &mod->num_gpl_syms);
2275 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2276 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2277 "__ksymtab_gpl_future",
2278 sizeof(*mod->gpl_future_syms),
2279 &mod->num_gpl_future_syms);
2280 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2281 "__kcrctab_gpl_future");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002283#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002284 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2285 "__ksymtab_unused",
2286 sizeof(*mod->unused_syms),
2287 &mod->num_unused_syms);
2288 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2289 "__kcrctab_unused");
2290 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2291 "__ksymtab_unused_gpl",
2292 sizeof(*mod->unused_gpl_syms),
2293 &mod->num_unused_gpl_syms);
2294 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2295 "__kcrctab_unused_gpl");
2296#endif
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002297#ifdef CONFIG_CONSTRUCTORS
2298 mod->ctors = section_objs(hdr, sechdrs, secstrings, ".ctors",
2299 sizeof(*mod->ctors), &mod->num_ctors);
2300#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002301
Rusty Russell5e458cc2008-10-22 10:00:13 -05002302#ifdef CONFIG_TRACEPOINTS
2303 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2304 "__tracepoints",
2305 sizeof(*mod->tracepoints),
2306 &mod->num_tracepoints);
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002307#endif
Steven Rostedt6d723732009-04-10 14:53:50 -04002308#ifdef CONFIG_EVENT_TRACING
2309 mod->trace_events = section_objs(hdr, sechdrs, secstrings,
2310 "_ftrace_events",
2311 sizeof(*mod->trace_events),
2312 &mod->num_trace_events);
Catalin Marinasa6f5aa12009-10-28 13:33:10 +00002313 /*
2314 * This section contains pointers to allocated objects in the trace
2315 * code and not scanning it leads to false positives.
2316 */
2317 kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2318 mod->num_trace_events, GFP_KERNEL);
Steven Rostedt6d723732009-04-10 14:53:50 -04002319#endif
Steven Rostedt93eb6772009-04-15 13:24:06 -04002320#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2321 /* sechdrs[0].sh_size is always zero */
2322 mod->ftrace_callsites = section_objs(hdr, sechdrs, secstrings,
2323 "__mcount_loc",
2324 sizeof(*mod->ftrace_callsites),
2325 &mod->num_ftrace_callsites);
2326#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327#ifdef CONFIG_MODVERSIONS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002328 if ((mod->num_syms && !mod->crcs)
2329 || (mod->num_gpl_syms && !mod->gpl_crcs)
2330 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002331#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002332 || (mod->num_unused_syms && !mod->unused_crcs)
2333 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002334#endif
2335 ) {
Rusty Russellc6e665c2009-03-31 13:05:33 -06002336 err = try_to_force_load(mod,
2337 "no versions for exported symbols");
Linus Torvalds826e4502008-05-04 17:04:16 -07002338 if (err)
2339 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 }
2341#endif
2342
2343 /* Now do relocations. */
2344 for (i = 1; i < hdr->e_shnum; i++) {
2345 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2346 unsigned int info = sechdrs[i].sh_info;
2347
2348 /* Not a valid relocation section? */
2349 if (info >= hdr->e_shnum)
2350 continue;
2351
2352 /* Don't bother with non-allocated sections */
2353 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2354 continue;
2355
2356 if (sechdrs[i].sh_type == SHT_REL)
2357 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2358 else if (sechdrs[i].sh_type == SHT_RELA)
2359 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2360 mod);
2361 if (err < 0)
2362 goto cleanup;
2363 }
2364
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002365 /* Find duplicate symbols */
2366 err = verify_export_symbols(mod);
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002367 if (err < 0)
2368 goto cleanup;
2369
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 /* Set up and sort exception table */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002371 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2372 sizeof(*mod->extable), &mod->num_exentries);
2373 sort_extable(mod->extable, mod->extable + mod->num_exentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
2375 /* Finally, copy percpu area over. */
Tejun Heo259354d2010-03-10 18:56:10 +09002376 percpu_modcopy(mod, (void *)sechdrs[pcpuindex].sh_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 sechdrs[pcpuindex].sh_size);
2378
Jan Beulich4a496222009-07-06 14:50:42 +01002379 add_kallsyms(mod, sechdrs, hdr->e_shnum, symindex, strindex,
Jan Beulich554bdfe2009-07-06 14:51:44 +01002380 symoffs, stroffs, secstrings, strmap);
2381 kfree(strmap);
2382 strmap = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002384 if (!mod->taints) {
Jason Barone9d376f2009-02-05 11:51:38 -05002385 struct _ddebug *debug;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002386 unsigned int num_debug;
2387
Rusty Russell5e458cc2008-10-22 10:00:13 -05002388 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2389 sizeof(*debug), &num_debug);
Jason Barone9d376f2009-02-05 11:51:38 -05002390 if (debug)
2391 dynamic_debug_setup(debug, num_debug);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002392 }
Steven Rostedt90d595f2008-08-14 15:45:09 -04002393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 err = module_finalize(hdr, sechdrs, mod);
2395 if (err < 0)
2396 goto cleanup;
2397
Thomas Koeller378bac82005-09-06 15:17:11 -07002398 /* flush the icache in correct context */
2399 old_fs = get_fs();
2400 set_fs(KERNEL_DS);
2401
2402 /*
2403 * Flush the instruction cache, since we've played with text.
2404 * Do it before processing of module parameters, so the module
2405 * can provide parameter accessor functions of its own.
2406 */
2407 if (mod->module_init)
2408 flush_icache_range((unsigned long)mod->module_init,
2409 (unsigned long)mod->module_init
2410 + mod->init_size);
2411 flush_icache_range((unsigned long)mod->module_core,
2412 (unsigned long)mod->module_core + mod->core_size);
2413
2414 set_fs(old_fs);
2415
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 mod->args = args;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002417 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002418 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2419 mod->name);
2420
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002421 /* Now sew it into the lists so we can get lockdep and oops
Andi Kleend72b3752008-08-30 10:09:00 +02002422 * info during argument parsing. Noone should access us, since
2423 * strong_try_module_get() will fail.
2424 * lockdep/oops can run asynchronous, so use the RCU list insertion
2425 * function to insert in a way safe to concurrent readers.
2426 * The mutex protects against concurrent writers.
2427 */
2428 list_add_rcu(&mod->list, &modules);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002429
Rusty Russelle180a6b2009-03-31 13:05:29 -06002430 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002432 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Rusty Russelle180a6b2009-03-31 13:05:29 -06002434 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002436 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Roland McGrath6d760132007-10-16 23:26:40 -07002438 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440 /* Get rid of temporary copy */
2441 vfree(hdr);
2442
Li Zefan7ead8b82009-08-17 16:56:28 +08002443 trace_module_load(mod);
2444
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 /* Done! */
2446 return mod;
2447
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002448 unlink:
Rusty Russelle91defa2009-03-31 13:05:35 -06002449 /* Unlink carefully: kallsyms could be walking list. */
2450 list_del_rcu(&mod->list);
2451 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 module_arch_cleanup(mod);
2453 cleanup:
Rusty Russella263f772009-09-25 00:32:58 -06002454 free_modinfo(mod);
Kay Sievers97c146e2007-11-29 23:46:11 +01002455 kobject_del(&mod->mkobj.kobj);
2456 kobject_put(&mod->mkobj.kobj);
2457 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 module_unload_free(mod);
Christoph Lametere1783a22010-01-05 15:34:50 +09002459#if defined(CONFIG_MODULE_UNLOAD)
2460 free_percpu(mod->refptr);
Rusty Russellffa9f122009-09-25 00:32:59 -06002461 free_init:
Eric Dumazet720eba32009-02-03 13:31:36 +10302462#endif
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002463 module_free(mod, mod->module_init);
2464 free_core:
2465 module_free(mod, mod->module_core);
2466 /* mod will be freed with core. Don't access it beyond this line! */
2467 free_percpu:
Tejun Heo259354d2010-03-10 18:56:10 +09002468 percpu_modfree(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 free_mod:
2470 kfree(args);
Jan Beulich554bdfe2009-07-06 14:51:44 +01002471 kfree(strmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 free_hdr:
2473 vfree(hdr);
Jayachandran C6fe2e702006-01-06 00:19:54 -08002474 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
2476 truncated:
2477 printk(KERN_ERR "Module len %lu truncated\n", len);
2478 err = -ENOEXEC;
2479 goto free_hdr;
2480}
2481
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002482/* Call module constructors. */
2483static void do_mod_ctors(struct module *mod)
2484{
2485#ifdef CONFIG_CONSTRUCTORS
2486 unsigned long i;
2487
2488 for (i = 0; i < mod->num_ctors; i++)
2489 mod->ctors[i]();
2490#endif
2491}
2492
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493/* This is where the real work happens */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002494SYSCALL_DEFINE3(init_module, void __user *, umod,
2495 unsigned long, len, const char __user *, uargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496{
2497 struct module *mod;
2498 int ret = 0;
2499
2500 /* Must have permission */
Kees Cook3d433212009-04-02 15:49:29 -07002501 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 return -EPERM;
2503
2504 /* Only one module load at a time, please */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002505 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 return -EINTR;
2507
2508 /* Do all the hard work */
2509 mod = load_module(umod, len, uargs);
2510 if (IS_ERR(mod)) {
Ashutosh Naik6389a382006-03-23 03:00:46 -08002511 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 return PTR_ERR(mod);
2513 }
2514
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 /* Drop lock so they can recurse */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002516 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
Alan Sterne041c682006-03-27 01:16:30 -08002518 blocking_notifier_call_chain(&module_notify_list,
2519 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002521 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 /* Start the module */
2523 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002524 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 if (ret < 0) {
2526 /* Init routine failed: abort. Try to protect us from
2527 buggy refcounters. */
2528 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002529 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002530 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002531 blocking_notifier_call_chain(&module_notify_list,
2532 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002533 mutex_lock(&module_mutex);
2534 free_module(mod);
2535 mutex_unlock(&module_mutex);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002536 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 return ret;
2538 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002539 if (ret > 0) {
Joe Perchesad361c92009-07-06 13:05:40 -07002540 printk(KERN_WARNING
2541"%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
2542"%s: loading module anyway...\n",
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002543 __func__, mod->name, ret,
2544 __func__);
2545 dump_stack();
2546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
Rusty Russell6c5db222008-03-10 11:43:52 -07002548 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002550 wake_up(&module_wq);
Masami Hiramatsu0deddf432009-01-06 14:41:54 -08002551 blocking_notifier_call_chain(&module_notify_list,
2552 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07002553
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07002554 /* We need to finish all async code before the module init sequence is done */
2555 async_synchronize_full();
2556
Rusty Russell6c5db222008-03-10 11:43:52 -07002557 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 /* Drop initial reference. */
2559 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06002560 trim_init_extable(mod);
Jan Beulich4a496222009-07-06 14:50:42 +01002561#ifdef CONFIG_KALLSYMS
2562 mod->num_symtab = mod->core_num_syms;
2563 mod->symtab = mod->core_symtab;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002564 mod->strtab = mod->core_strtab;
Jan Beulich4a496222009-07-06 14:50:42 +01002565#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 module_free(mod, mod->module_init);
2567 mod->module_init = NULL;
2568 mod->init_size = 0;
2569 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002570 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
2572 return 0;
2573}
2574
2575static inline int within(unsigned long addr, void *start, unsigned long size)
2576{
2577 return ((void *)addr >= start && (void *)addr < start + size);
2578}
2579
2580#ifdef CONFIG_KALLSYMS
2581/*
2582 * This ignores the intensely annoying "mapping symbols" found
2583 * in ARM ELF files: $a, $t and $d.
2584 */
2585static inline int is_arm_mapping_symbol(const char *str)
2586{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002587 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 && (str[2] == '\0' || str[2] == '.');
2589}
2590
2591static const char *get_ksymbol(struct module *mod,
2592 unsigned long addr,
2593 unsigned long *size,
2594 unsigned long *offset)
2595{
2596 unsigned int i, best = 0;
2597 unsigned long nextval;
2598
2599 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002600 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002602 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2604
2605 /* Scan for closest preceeding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002606 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 for (i = 1; i < mod->num_symtab; i++) {
2608 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2609 continue;
2610
2611 /* We ignore unnamed symbols: they're uninformative
2612 * and inserted at a whim. */
2613 if (mod->symtab[i].st_value <= addr
2614 && mod->symtab[i].st_value > mod->symtab[best].st_value
2615 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2616 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2617 best = i;
2618 if (mod->symtab[i].st_value > addr
2619 && mod->symtab[i].st_value < nextval
2620 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2621 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2622 nextval = mod->symtab[i].st_value;
2623 }
2624
2625 if (!best)
2626 return NULL;
2627
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002628 if (size)
2629 *size = nextval - mod->symtab[best].st_value;
2630 if (offset)
2631 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 return mod->strtab + mod->symtab[best].st_name;
2633}
2634
Rusty Russell6dd06c92008-01-29 17:13:22 -05002635/* For kallsyms to ask for address resolution. NULL means not found. Careful
2636 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002637const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05002638 unsigned long *size,
2639 unsigned long *offset,
2640 char **modname,
2641 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642{
2643 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002644 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
Rusty Russellcb2a5202008-01-14 00:55:03 -08002646 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002647 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002648 if (within_module_init(addr, mod) ||
2649 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002650 if (modname)
2651 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002652 ret = get_ksymbol(mod, addr, size, offset);
2653 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 }
2655 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05002656 /* Make a copy in here where it's safe */
2657 if (ret) {
2658 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2659 ret = namebuf;
2660 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002661 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002662 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663}
2664
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002665int lookup_module_symbol_name(unsigned long addr, char *symname)
2666{
2667 struct module *mod;
2668
Rusty Russellcb2a5202008-01-14 00:55:03 -08002669 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002670 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002671 if (within_module_init(addr, mod) ||
2672 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002673 const char *sym;
2674
2675 sym = get_ksymbol(mod, addr, NULL, NULL);
2676 if (!sym)
2677 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07002678 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002679 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002680 return 0;
2681 }
2682 }
2683out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002684 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002685 return -ERANGE;
2686}
2687
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002688int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2689 unsigned long *offset, char *modname, char *name)
2690{
2691 struct module *mod;
2692
Rusty Russellcb2a5202008-01-14 00:55:03 -08002693 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002694 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002695 if (within_module_init(addr, mod) ||
2696 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002697 const char *sym;
2698
2699 sym = get_ksymbol(mod, addr, size, offset);
2700 if (!sym)
2701 goto out;
2702 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07002703 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002704 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07002705 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002706 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002707 return 0;
2708 }
2709 }
2710out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002711 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002712 return -ERANGE;
2713}
2714
Alexey Dobriyanea078902007-05-08 00:28:39 -07002715int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2716 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717{
2718 struct module *mod;
2719
Rusty Russellcb2a5202008-01-14 00:55:03 -08002720 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002721 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 if (symnum < mod->num_symtab) {
2723 *value = mod->symtab[symnum].st_value;
2724 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002725 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07002726 KSYM_NAME_LEN);
2727 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06002728 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002729 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002730 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 }
2732 symnum -= mod->num_symtab;
2733 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002734 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002735 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736}
2737
2738static unsigned long mod_find_symname(struct module *mod, const char *name)
2739{
2740 unsigned int i;
2741
2742 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002743 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2744 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 return mod->symtab[i].st_value;
2746 return 0;
2747}
2748
2749/* Look for this name: can be of form module:name. */
2750unsigned long module_kallsyms_lookup_name(const char *name)
2751{
2752 struct module *mod;
2753 char *colon;
2754 unsigned long ret = 0;
2755
2756 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08002757 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 if ((colon = strchr(name, ':')) != NULL) {
2759 *colon = '\0';
2760 if ((mod = find_module(name)) != NULL)
2761 ret = mod_find_symname(mod, colon+1);
2762 *colon = ':';
2763 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02002764 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 if ((ret = mod_find_symname(mod, name)) != 0)
2766 break;
2767 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002768 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 return ret;
2770}
Anders Kaseorg75a66612008-12-05 19:03:58 -05002771
2772int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
2773 struct module *, unsigned long),
2774 void *data)
2775{
2776 struct module *mod;
2777 unsigned int i;
2778 int ret;
2779
2780 list_for_each_entry(mod, &modules, list) {
2781 for (i = 0; i < mod->num_symtab; i++) {
2782 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
2783 mod, mod->symtab[i].st_value);
2784 if (ret != 0)
2785 return ret;
2786 }
2787 }
2788 return 0;
2789}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790#endif /* CONFIG_KALLSYMS */
2791
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002792static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002793{
2794 int bx = 0;
2795
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002796 if (mod->taints ||
2797 mod->state == MODULE_STATE_GOING ||
2798 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002799 buf[bx++] = '(';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002800 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002801 buf[bx++] = 'P';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002802 if (mod->taints & (1 << TAINT_FORCED_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002803 buf[bx++] = 'F';
Linus Torvalds26e9a392008-10-17 09:50:12 -07002804 if (mod->taints & (1 << TAINT_CRAP))
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002805 buf[bx++] = 'C';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002806 /*
2807 * TAINT_FORCED_RMMOD: could be added.
2808 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2809 * apply to modules.
2810 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002811
2812 /* Show a - for module-is-being-unloaded */
2813 if (mod->state == MODULE_STATE_GOING)
2814 buf[bx++] = '-';
2815 /* Show a + for module-is-being-loaded */
2816 if (mod->state == MODULE_STATE_COMING)
2817 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002818 buf[bx++] = ')';
2819 }
2820 buf[bx] = '\0';
2821
2822 return buf;
2823}
2824
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002825#ifdef CONFIG_PROC_FS
2826/* Called by the /proc file system to return a list of modules. */
2827static void *m_start(struct seq_file *m, loff_t *pos)
2828{
2829 mutex_lock(&module_mutex);
2830 return seq_list_start(&modules, *pos);
2831}
2832
2833static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2834{
2835 return seq_list_next(p, &modules, pos);
2836}
2837
2838static void m_stop(struct seq_file *m, void *p)
2839{
2840 mutex_unlock(&module_mutex);
2841}
2842
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843static int m_show(struct seq_file *m, void *p)
2844{
2845 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002846 char buf[8];
2847
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05002848 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 mod->name, mod->init_size + mod->core_size);
2850 print_unload_info(m, mod);
2851
2852 /* Informative for users. */
2853 seq_printf(m, " %s",
2854 mod->state == MODULE_STATE_GOING ? "Unloading":
2855 mod->state == MODULE_STATE_COMING ? "Loading":
2856 "Live");
2857 /* Used by oprofile and other similar tools. */
2858 seq_printf(m, " 0x%p", mod->module_core);
2859
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002860 /* Taints info */
2861 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002862 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002863
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 seq_printf(m, "\n");
2865 return 0;
2866}
2867
2868/* Format: modulename size refcount deps address
2869
2870 Where refcount is a number or -, and deps is a comma-separated list
2871 of depends or -.
2872*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002873static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 .start = m_start,
2875 .next = m_next,
2876 .stop = m_stop,
2877 .show = m_show
2878};
2879
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002880static int modules_open(struct inode *inode, struct file *file)
2881{
2882 return seq_open(file, &modules_op);
2883}
2884
2885static const struct file_operations proc_modules_operations = {
2886 .open = modules_open,
2887 .read = seq_read,
2888 .llseek = seq_lseek,
2889 .release = seq_release,
2890};
2891
2892static int __init proc_modules_init(void)
2893{
2894 proc_create("modules", 0, NULL, &proc_modules_operations);
2895 return 0;
2896}
2897module_init(proc_modules_init);
2898#endif
2899
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900/* Given an address, look for it in the module exception tables. */
2901const struct exception_table_entry *search_module_extables(unsigned long addr)
2902{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 const struct exception_table_entry *e = NULL;
2904 struct module *mod;
2905
Rusty Russell24da1cb2007-07-15 23:41:46 -07002906 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002907 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 if (mod->num_exentries == 0)
2909 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002910
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 e = search_extable(mod->extable,
2912 mod->extable + mod->num_exentries - 1,
2913 addr);
2914 if (e)
2915 break;
2916 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07002917 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
2919 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07002920 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 return e;
2922}
2923
Ingo Molnar4d435f92006-07-03 00:24:24 -07002924/*
Rusty Russelle6104992009-03-31 13:05:31 -06002925 * is_module_address - is this address inside a module?
2926 * @addr: the address to check.
2927 *
2928 * See is_module_text_address() if you simply want to see if the address
2929 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07002930 */
Rusty Russelle6104992009-03-31 13:05:31 -06002931bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07002932{
Rusty Russelle6104992009-03-31 13:05:31 -06002933 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07002934
Rusty Russell24da1cb2007-07-15 23:41:46 -07002935 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06002936 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07002937 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002938
Rusty Russelle6104992009-03-31 13:05:31 -06002939 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07002940}
2941
Rusty Russelle6104992009-03-31 13:05:31 -06002942/*
2943 * __module_address - get the module which contains an address.
2944 * @addr: the address.
2945 *
2946 * Must be called with preempt disabled or module mutex held so that
2947 * module doesn't get freed during this.
2948 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07002949struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950{
2951 struct module *mod;
2952
Rusty Russell3a642e92008-07-22 19:24:28 -05002953 if (addr < module_addr_min || addr > module_addr_max)
2954 return NULL;
2955
Andi Kleend72b3752008-08-30 10:09:00 +02002956 list_for_each_entry_rcu(mod, &modules, list)
Rusty Russelle6104992009-03-31 13:05:31 -06002957 if (within_module_core(addr, mod)
2958 || within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 return mod;
2960 return NULL;
2961}
Tim Abbottc6b37802008-12-05 19:03:59 -05002962EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Rusty Russelle6104992009-03-31 13:05:31 -06002964/*
2965 * is_module_text_address - is this address inside module code?
2966 * @addr: the address to check.
2967 *
2968 * See is_module_address() if you simply want to see if the address is
2969 * anywhere in a module. See kernel_text_address() for testing if an
2970 * address corresponds to kernel or module code.
2971 */
2972bool is_module_text_address(unsigned long addr)
2973{
2974 bool ret;
2975
2976 preempt_disable();
2977 ret = __module_text_address(addr) != NULL;
2978 preempt_enable();
2979
2980 return ret;
2981}
2982
2983/*
2984 * __module_text_address - get the module whose code contains an address.
2985 * @addr: the address.
2986 *
2987 * Must be called with preempt disabled or module mutex held so that
2988 * module doesn't get freed during this.
2989 */
2990struct module *__module_text_address(unsigned long addr)
2991{
2992 struct module *mod = __module_address(addr);
2993 if (mod) {
2994 /* Make sure it's within the text section. */
2995 if (!within(addr, mod->module_init, mod->init_text_size)
2996 && !within(addr, mod->module_core, mod->core_text_size))
2997 mod = NULL;
2998 }
2999 return mod;
3000}
Tim Abbottc6b37802008-12-05 19:03:59 -05003001EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06003002
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003/* Don't grab lock, we're oopsing. */
3004void print_modules(void)
3005{
3006 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07003007 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
Linus Torvaldsb2311252009-06-16 11:07:14 -07003009 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02003010 /* Most callers should already have preempt disabled, but make sure */
3011 preempt_disable();
3012 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003013 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02003014 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01003015 if (last_unloaded_module[0])
3016 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 printk("\n");
3018}
3019
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06003021/* Generate the signature for all relevant module structures here.
3022 * If these change, we don't want to try to parse the module. */
3023void module_layout(struct module *mod,
3024 struct modversion_info *ver,
3025 struct kernel_param *kp,
3026 struct kernel_symbol *ks,
Rusty Russell8c8ef422009-03-31 13:05:34 -06003027 struct tracepoint *tp)
3028{
3029}
3030EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07003032
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04003033#ifdef CONFIG_TRACEPOINTS
3034void module_update_tracepoints(void)
3035{
3036 struct module *mod;
3037
3038 mutex_lock(&module_mutex);
3039 list_for_each_entry(mod, &modules, list)
3040 if (!mod->taints)
3041 tracepoint_update_probe_range(mod->tracepoints,
3042 mod->tracepoints + mod->num_tracepoints);
3043 mutex_unlock(&module_mutex);
3044}
3045
3046/*
3047 * Returns 0 if current not found.
3048 * Returns 1 if current found.
3049 */
3050int module_get_iter_tracepoints(struct tracepoint_iter *iter)
3051{
3052 struct module *iter_mod;
3053 int found = 0;
3054
3055 mutex_lock(&module_mutex);
3056 list_for_each_entry(iter_mod, &modules, list) {
3057 if (!iter_mod->taints) {
3058 /*
3059 * Sorted module list
3060 */
3061 if (iter_mod < iter->module)
3062 continue;
3063 else if (iter_mod > iter->module)
3064 iter->tracepoint = NULL;
3065 found = tracepoint_get_iter_range(&iter->tracepoint,
3066 iter_mod->tracepoints,
3067 iter_mod->tracepoints
3068 + iter_mod->num_tracepoints);
3069 if (found) {
3070 iter->module = iter_mod;
3071 break;
3072 }
3073 }
3074 }
3075 mutex_unlock(&module_mutex);
3076 return found;
3077}
3078#endif