blob: 0838246d8c94991ec8e8788b22a86e91e8552349 [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
62EXPORT_TRACEPOINT_SYMBOL(module_get);
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#if 0
65#define DEBUGP printk
66#else
67#define DEBUGP(fmt , a...)
68#endif
69
70#ifndef ARCH_SHF_SMALL
71#define ARCH_SHF_SMALL 0
72#endif
73
74/* If this is set, the section belongs in the init part of the module */
75#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
76
Rusty Russell24da1cb2007-07-15 23:41:46 -070077/* List of modules, protected by module_mutex or preempt_disable
Andi Kleend72b3752008-08-30 10:09:00 +020078 * (delete uses stop_machine/add uses RCU list operations). */
Tim Abbottc6b37802008-12-05 19:03:59 -050079DEFINE_MUTEX(module_mutex);
80EXPORT_SYMBOL_GPL(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static LIST_HEAD(modules);
82
Stephen Rothwell19e45292009-04-14 17:27:18 +100083/* Block module loading/unloading? */
84int modules_disabled = 0;
85
Rusty Russellc9a3ba52008-01-29 17:13:18 -050086/* Waiting for a module to finish initializing? */
87static DECLARE_WAIT_QUEUE_HEAD(module_wq);
88
Alan Sterne041c682006-03-27 01:16:30 -080089static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Rusty Russelle6104992009-03-31 13:05:31 -060091/* Bounds of module allocation, for speeding __module_address */
Rusty Russell3a642e92008-07-22 19:24:28 -050092static unsigned long module_addr_min = -1UL, module_addr_max = 0;
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094int register_module_notifier(struct notifier_block * nb)
95{
Alan Sterne041c682006-03-27 01:16:30 -080096 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98EXPORT_SYMBOL(register_module_notifier);
99
100int unregister_module_notifier(struct notifier_block * nb)
101{
Alan Sterne041c682006-03-27 01:16:30 -0800102 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104EXPORT_SYMBOL(unregister_module_notifier);
105
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -0800106/* We require a truly strong try_module_get(): 0 means failure due to
107 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static inline int strong_try_module_get(struct module *mod)
109{
110 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500111 return -EBUSY;
112 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500114 else
115 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700118static inline void add_taint_module(struct module *mod, unsigned flag)
119{
120 add_taint(flag);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700121 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700122}
123
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200124/*
125 * A thread that wants to hold a reference to a module only while it
126 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 */
128void __module_put_and_exit(struct module *mod, long code)
129{
130 module_put(mod);
131 do_exit(code);
132}
133EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/* Find a module section: 0 means not found. */
136static unsigned int find_sec(Elf_Ehdr *hdr,
137 Elf_Shdr *sechdrs,
138 const char *secstrings,
139 const char *name)
140{
141 unsigned int i;
142
143 for (i = 1; i < hdr->e_shnum; i++)
144 /* Alloc bit cleared means "ignore it." */
145 if ((sechdrs[i].sh_flags & SHF_ALLOC)
146 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
147 return i;
148 return 0;
149}
150
Rusty Russell5e458cc2008-10-22 10:00:13 -0500151/* Find a module section, or NULL. */
152static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
153 const char *secstrings, const char *name)
154{
155 /* Section 0 has sh_addr 0. */
156 return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
157}
158
159/* Find a module section, or NULL. Fill in number of "objects" in section. */
160static void *section_objs(Elf_Ehdr *hdr,
161 Elf_Shdr *sechdrs,
162 const char *secstrings,
163 const char *name,
164 size_t object_size,
165 unsigned int *num)
166{
167 unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
168
169 /* Section 0 has sh_addr 0 and sh_size 0. */
170 *num = sechdrs[sec].sh_size / object_size;
171 return (void *)sechdrs[sec].sh_addr;
172}
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/* Provided by the linker */
175extern const struct kernel_symbol __start___ksymtab[];
176extern const struct kernel_symbol __stop___ksymtab[];
177extern const struct kernel_symbol __start___ksymtab_gpl[];
178extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800179extern const struct kernel_symbol __start___ksymtab_gpl_future[];
180extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700181extern const struct kernel_symbol __start___ksymtab_gpl_future[];
182extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183extern const unsigned long __start___kcrctab[];
184extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800185extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500186#ifdef CONFIG_UNUSED_SYMBOLS
187extern const struct kernel_symbol __start___ksymtab_unused[];
188extern const struct kernel_symbol __stop___ksymtab_unused[];
189extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
190extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700191extern const unsigned long __start___kcrctab_unused[];
192extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500193#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195#ifndef CONFIG_MODVERSIONS
196#define symversion(base, idx) NULL
197#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800198#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199#endif
200
Rusty Russelldafd0942008-07-22 19:24:25 -0500201static bool each_symbol_in_section(const struct symsearch *arr,
202 unsigned int arrsize,
203 struct module *owner,
204 bool (*fn)(const struct symsearch *syms,
205 struct module *owner,
206 unsigned int symnum, void *data),
207 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100208{
Rusty Russelldafd0942008-07-22 19:24:25 -0500209 unsigned int i, j;
210
211 for (j = 0; j < arrsize; j++) {
212 for (i = 0; i < arr[j].stop - arr[j].start; i++)
213 if (fn(&arr[j], owner, i, data))
214 return true;
215 }
216
217 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100218}
219
Rusty Russelldafd0942008-07-22 19:24:25 -0500220/* Returns true as soon as fn returns true, otherwise false. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500221bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
222 unsigned int symnum, void *data), void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700223{
Rusty Russelldafd0942008-07-22 19:24:25 -0500224 struct module *mod;
225 const struct symsearch arr[] = {
226 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
227 NOT_GPL_ONLY, false },
228 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
229 __start___kcrctab_gpl,
230 GPL_ONLY, false },
231 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
232 __start___kcrctab_gpl_future,
233 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500234#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500235 { __start___ksymtab_unused, __stop___ksymtab_unused,
236 __start___kcrctab_unused,
237 NOT_GPL_ONLY, true },
238 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
239 __start___kcrctab_unused_gpl,
240 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500241#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500242 };
243
244 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
245 return true;
246
Andi Kleend72b3752008-08-30 10:09:00 +0200247 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500248 struct symsearch arr[] = {
249 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
250 NOT_GPL_ONLY, false },
251 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
252 mod->gpl_crcs,
253 GPL_ONLY, false },
254 { mod->gpl_future_syms,
255 mod->gpl_future_syms + mod->num_gpl_future_syms,
256 mod->gpl_future_crcs,
257 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500258#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500259 { mod->unused_syms,
260 mod->unused_syms + mod->num_unused_syms,
261 mod->unused_crcs,
262 NOT_GPL_ONLY, true },
263 { mod->unused_gpl_syms,
264 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
265 mod->unused_gpl_crcs,
266 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500267#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500268 };
269
270 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
271 return true;
272 }
273 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700274}
Tim Abbottc6b37802008-12-05 19:03:59 -0500275EXPORT_SYMBOL_GPL(each_symbol);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700276
Rusty Russelldafd0942008-07-22 19:24:25 -0500277struct find_symbol_arg {
278 /* Input */
279 const char *name;
280 bool gplok;
281 bool warn;
282
283 /* Output */
284 struct module *owner;
285 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500286 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500287};
288
289static bool find_symbol_in_section(const struct symsearch *syms,
290 struct module *owner,
291 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500292{
Rusty Russelldafd0942008-07-22 19:24:25 -0500293 struct find_symbol_arg *fsa = data;
294
295 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
296 return false;
297
298 if (!fsa->gplok) {
299 if (syms->licence == GPL_ONLY)
300 return false;
301 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
302 printk(KERN_WARNING "Symbol %s is being used "
303 "by a non-GPL module, which will not "
304 "be allowed in the future\n", fsa->name);
305 printk(KERN_WARNING "Please see the file "
306 "Documentation/feature-removal-schedule.txt "
307 "in the kernel source tree for more details.\n");
308 }
309 }
310
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500311#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500312 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500313 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500314 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500315 printk(KERN_WARNING
316 "This symbol will go away in the future.\n");
317 printk(KERN_WARNING
318 "Please evalute if this is the right api to use and if "
319 "it really is, submit a report the linux kernel "
320 "mailinglist together with submitting your code for "
321 "inclusion.\n");
322 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500323#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500324
325 fsa->owner = owner;
326 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500327 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500328 return true;
329}
330
Tim Abbott414fd312008-12-05 19:03:56 -0500331/* Find a symbol and return it, along with, (optional) crc and
332 * (optional) module which owns it */
Tim Abbottc6b37802008-12-05 19:03:59 -0500333const struct kernel_symbol *find_symbol(const char *name,
334 struct module **owner,
335 const unsigned long **crc,
336 bool gplok,
337 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Rusty Russelldafd0942008-07-22 19:24:25 -0500339 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Rusty Russelldafd0942008-07-22 19:24:25 -0500341 fsa.name = name;
342 fsa.gplok = gplok;
343 fsa.warn = warn;
344
345 if (each_symbol(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500346 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500347 *owner = fsa.owner;
348 if (crc)
349 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500350 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 DEBUGP("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500354 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
Tim Abbottc6b37802008-12-05 19:03:59 -0500356EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358/* Search for module by name: must hold module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500359struct module *find_module(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 struct module *mod;
362
363 list_for_each_entry(mod, &modules, list) {
364 if (strcmp(mod->name, name) == 0)
365 return mod;
366 }
367 return NULL;
368}
Tim Abbottc6b37802008-12-05 19:03:59 -0500369EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900372
Tejun Heo259354d2010-03-10 18:56:10 +0900373static inline void __percpu *mod_percpu(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900374{
Tejun Heo259354d2010-03-10 18:56:10 +0900375 return mod->percpu;
376}
Tejun Heofbf59bc2009-02-20 16:29:08 +0900377
Tejun Heo259354d2010-03-10 18:56:10 +0900378static int percpu_modalloc(struct module *mod,
379 unsigned long size, unsigned long align)
380{
Tejun Heofbf59bc2009-02-20 16:29:08 +0900381 if (align > PAGE_SIZE) {
382 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
Tejun Heo259354d2010-03-10 18:56:10 +0900383 mod->name, align, PAGE_SIZE);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900384 align = PAGE_SIZE;
385 }
386
Tejun Heo259354d2010-03-10 18:56:10 +0900387 mod->percpu = __alloc_reserved_percpu(size, align);
388 if (!mod->percpu) {
Tejun Heofbf59bc2009-02-20 16:29:08 +0900389 printk(KERN_WARNING
390 "Could not allocate %lu bytes percpu data\n", size);
Tejun Heo259354d2010-03-10 18:56:10 +0900391 return -ENOMEM;
392 }
393 mod->percpu_size = size;
394 return 0;
Tejun Heofbf59bc2009-02-20 16:29:08 +0900395}
396
Tejun Heo259354d2010-03-10 18:56:10 +0900397static void percpu_modfree(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900398{
Tejun Heo259354d2010-03-10 18:56:10 +0900399 free_percpu(mod->percpu);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900400}
401
Tejun Heo6b588c12009-02-20 16:29:07 +0900402static unsigned int find_pcpusec(Elf_Ehdr *hdr,
403 Elf_Shdr *sechdrs,
404 const char *secstrings)
405{
406 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
407}
408
Tejun Heo259354d2010-03-10 18:56:10 +0900409static void percpu_modcopy(struct module *mod,
410 const void *from, unsigned long size)
Tejun Heo6b588c12009-02-20 16:29:07 +0900411{
412 int cpu;
413
414 for_each_possible_cpu(cpu)
Tejun Heo259354d2010-03-10 18:56:10 +0900415 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
Tejun Heo6b588c12009-02-20 16:29:07 +0900416}
417
Tejun Heo10fad5e2010-03-10 18:57:54 +0900418/**
419 * is_module_percpu_address - test whether address is from module static percpu
420 * @addr: address to test
421 *
422 * Test whether @addr belongs to module static percpu area.
423 *
424 * RETURNS:
425 * %true if @addr is from module static percpu area
426 */
427bool is_module_percpu_address(unsigned long addr)
428{
429 struct module *mod;
430 unsigned int cpu;
431
432 preempt_disable();
433
434 list_for_each_entry_rcu(mod, &modules, list) {
435 if (!mod->percpu_size)
436 continue;
437 for_each_possible_cpu(cpu) {
438 void *start = per_cpu_ptr(mod->percpu, cpu);
439
440 if ((void *)addr >= start &&
441 (void *)addr < start + mod->percpu_size) {
442 preempt_enable();
443 return true;
444 }
445 }
446 }
447
448 preempt_enable();
449 return false;
450}
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900453
Tejun Heo259354d2010-03-10 18:56:10 +0900454static inline void __percpu *mod_percpu(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 return NULL;
457}
Tejun Heo259354d2010-03-10 18:56:10 +0900458static inline int percpu_modalloc(struct module *mod,
459 unsigned long size, unsigned long align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Tejun Heo259354d2010-03-10 18:56:10 +0900461 return -ENOMEM;
462}
463static inline void percpu_modfree(struct module *mod)
464{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
467 Elf_Shdr *sechdrs,
468 const char *secstrings)
469{
470 return 0;
471}
Tejun Heo259354d2010-03-10 18:56:10 +0900472static inline void percpu_modcopy(struct module *mod,
473 const void *from, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 /* pcpusec should be 0, and size of that section should be 0. */
476 BUG_ON(size != 0);
477}
Tejun Heo10fad5e2010-03-10 18:57:54 +0900478bool is_module_percpu_address(unsigned long addr)
479{
480 return false;
481}
Tejun Heo6b588c12009-02-20 16:29:07 +0900482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483#endif /* CONFIG_SMP */
484
Matt Domschc988d2b2005-06-23 22:05:15 -0700485#define MODINFO_ATTR(field) \
486static void setup_modinfo_##field(struct module *mod, const char *s) \
487{ \
488 mod->field = kstrdup(s, GFP_KERNEL); \
489} \
490static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
491 struct module *mod, char *buffer) \
492{ \
493 return sprintf(buffer, "%s\n", mod->field); \
494} \
495static int modinfo_##field##_exists(struct module *mod) \
496{ \
497 return mod->field != NULL; \
498} \
499static void free_modinfo_##field(struct module *mod) \
500{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700501 kfree(mod->field); \
502 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700503} \
504static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900505 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700506 .show = show_modinfo_##field, \
507 .setup = setup_modinfo_##field, \
508 .test = modinfo_##field##_exists, \
509 .free = free_modinfo_##field, \
510};
511
512MODINFO_ATTR(version);
513MODINFO_ATTR(srcversion);
514
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100515static char last_unloaded_module[MODULE_NAME_LEN+1];
516
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800517#ifdef CONFIG_MODULE_UNLOAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518/* Init the unload section of the module. */
519static void module_unload_init(struct module *mod)
520{
Eric Dumazet720eba32009-02-03 13:31:36 +1030521 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 INIT_LIST_HEAD(&mod->modules_which_use_me);
Nick Piggin5fbfb182010-04-01 19:09:40 +1100524 for_each_possible_cpu(cpu) {
525 per_cpu_ptr(mod->refptr, cpu)->incs = 0;
526 per_cpu_ptr(mod->refptr, cpu)->decs = 0;
527 }
Christoph Lametere1783a22010-01-05 15:34:50 +0900528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /* Hold reference count during initialization. */
Nick Piggin5fbfb182010-04-01 19:09:40 +1100530 __this_cpu_write(mod->refptr->incs, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 /* Backwards compatibility macros put refcount during init. */
532 mod->waiter = current;
533}
534
535/* modules using other modules */
536struct module_use
537{
538 struct list_head list;
539 struct module *module_which_uses;
540};
541
542/* Does a already use b? */
543static int already_uses(struct module *a, struct module *b)
544{
545 struct module_use *use;
546
547 list_for_each_entry(use, &b->modules_which_use_me, list) {
548 if (use->module_which_uses == a) {
549 DEBUGP("%s uses %s!\n", a->name, b->name);
550 return 1;
551 }
552 }
553 DEBUGP("%s does not use %s!\n", a->name, b->name);
554 return 0;
555}
556
557/* Module a uses b */
Tim Abbottc6b37802008-12-05 19:03:59 -0500558int use_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 struct module_use *use;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500561 int no_warn, err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (b == NULL || already_uses(a, b)) return 1;
564
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500565 /* If we're interrupted or time out, we fail. */
566 if (wait_event_interruptible_timeout(
567 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
568 30 * HZ) <= 0) {
569 printk("%s: gave up waiting for init of module %s.\n",
570 a->name, b->name);
571 return 0;
572 }
573
574 /* If strong_try_module_get() returned a different error, we fail. */
575 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return 0;
577
578 DEBUGP("Allocating new usage for %s.\n", a->name);
579 use = kmalloc(sizeof(*use), GFP_ATOMIC);
580 if (!use) {
581 printk("%s: out of memory loading\n", a->name);
582 module_put(b);
583 return 0;
584 }
585
586 use->module_which_uses = a;
587 list_add(&use->list, &b->modules_which_use_me);
Kay Sievers270a6c42007-01-18 13:26:15 +0100588 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return 1;
590}
Tim Abbottc6b37802008-12-05 19:03:59 -0500591EXPORT_SYMBOL_GPL(use_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593/* Clear the unload stuff of the module. */
594static void module_unload_free(struct module *mod)
595{
596 struct module *i;
597
598 list_for_each_entry(i, &modules, list) {
599 struct module_use *use;
600
601 list_for_each_entry(use, &i->modules_which_use_me, list) {
602 if (use->module_which_uses == mod) {
603 DEBUGP("%s unusing %s\n", mod->name, i->name);
604 module_put(i);
605 list_del(&use->list);
606 kfree(use);
Kay Sievers270a6c42007-01-18 13:26:15 +0100607 sysfs_remove_link(i->holders_dir, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /* There can be at most one match. */
609 break;
610 }
611 }
612 }
613}
614
615#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800616static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 int ret = (flags & O_TRUNC);
619 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800620 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return ret;
622}
623#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800624static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 return 0;
627}
628#endif /* CONFIG_MODULE_FORCE_UNLOAD */
629
630struct stopref
631{
632 struct module *mod;
633 int flags;
634 int *forced;
635};
636
637/* Whole machine is stopped with interrupts off when this runs. */
638static int __try_stop_module(void *_sref)
639{
640 struct stopref *sref = _sref;
641
Rusty Russellda39ba52008-07-22 19:24:25 -0500642 /* If it's not unused, quit unless we're forcing. */
643 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800644 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return -EWOULDBLOCK;
646 }
647
648 /* Mark it as dying. */
649 sref->mod->state = MODULE_STATE_GOING;
650 return 0;
651}
652
653static int try_stop_module(struct module *mod, int flags, int *forced)
654{
Rusty Russellda39ba52008-07-22 19:24:25 -0500655 if (flags & O_NONBLOCK) {
656 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500658 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500659 } else {
660 /* We don't need to stop the machine for this. */
661 mod->state = MODULE_STATE_GOING;
662 synchronize_sched();
663 return 0;
664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
667unsigned int module_refcount(struct module *mod)
668{
Nick Piggin5fbfb182010-04-01 19:09:40 +1100669 unsigned int incs = 0, decs = 0;
Eric Dumazet720eba32009-02-03 13:31:36 +1030670 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Eric Dumazet720eba32009-02-03 13:31:36 +1030672 for_each_possible_cpu(cpu)
Nick Piggin5fbfb182010-04-01 19:09:40 +1100673 decs += per_cpu_ptr(mod->refptr, cpu)->decs;
674 /*
675 * ensure the incs are added up after the decs.
676 * module_put ensures incs are visible before decs with smp_wmb.
677 *
678 * This 2-count scheme avoids the situation where the refcount
679 * for CPU0 is read, then CPU0 increments the module refcount,
680 * then CPU1 drops that refcount, then the refcount for CPU1 is
681 * read. We would record a decrement but not its corresponding
682 * increment so we would see a low count (disaster).
683 *
684 * Rare situation? But module_refcount can be preempted, and we
685 * might be tallying up 4096+ CPUs. So it is not impossible.
686 */
687 smp_rmb();
688 for_each_possible_cpu(cpu)
689 incs += per_cpu_ptr(mod->refptr, cpu)->incs;
690 return incs - decs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692EXPORT_SYMBOL(module_refcount);
693
694/* This exists whether we can unload or not */
695static void free_module(struct module *mod);
696
697static void wait_for_zero_refcount(struct module *mod)
698{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500699 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800700 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 for (;;) {
702 DEBUGP("Looking at refcount...\n");
703 set_current_state(TASK_UNINTERRUPTIBLE);
704 if (module_refcount(mod) == 0)
705 break;
706 schedule();
707 }
708 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800709 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100712SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
713 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800716 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 int ret, forced = 0;
718
Kees Cook3d433212009-04-02 15:49:29 -0700719 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800720 return -EPERM;
721
722 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
723 return -EFAULT;
724 name[MODULE_NAME_LEN-1] = '\0';
725
Tejun Heo3fc1f1e2010-05-06 18:49:20 +0200726 if (mutex_lock_interruptible(&module_mutex) != 0)
727 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 mod = find_module(name);
730 if (!mod) {
731 ret = -ENOENT;
732 goto out;
733 }
734
735 if (!list_empty(&mod->modules_which_use_me)) {
736 /* Other modules depend on us: get rid of them first. */
737 ret = -EWOULDBLOCK;
738 goto out;
739 }
740
741 /* Doing init or already dying? */
742 if (mod->state != MODULE_STATE_LIVE) {
743 /* FIXME: if (force), slam module count and wake up
744 waiter --RR */
745 DEBUGP("%s already dying\n", mod->name);
746 ret = -EBUSY;
747 goto out;
748 }
749
750 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700751 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800752 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (!forced) {
754 /* This module can't be removed */
755 ret = -EBUSY;
756 goto out;
757 }
758 }
759
760 /* Set this up before setting mod->state */
761 mod->waiter = current;
762
763 /* Stop the machine so refcounts can't move and disable module. */
764 ret = try_stop_module(mod, flags, &forced);
765 if (ret != 0)
766 goto out;
767
768 /* Never wait if forced. */
769 if (!forced && module_refcount(mod) != 0)
770 wait_for_zero_refcount(mod);
771
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200772 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 /* Final destruction now noone is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200774 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200776 blocking_notifier_call_chain(&module_notify_list,
777 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800778 async_synchronize_full();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200779 mutex_lock(&module_mutex);
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100780 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500781 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Jason Barone9d376f2009-02-05 11:51:38 -0500782 ddebug_remove_module(mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 free_module(mod);
784
785 out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800786 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return ret;
788}
789
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800790static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
792 struct module_use *use;
793 int printed_something = 0;
794
795 seq_printf(m, " %u ", module_refcount(mod));
796
797 /* Always include a trailing , so userspace can differentiate
798 between this and the old multi-field proc format. */
799 list_for_each_entry(use, &mod->modules_which_use_me, list) {
800 printed_something = 1;
801 seq_printf(m, "%s,", use->module_which_uses->name);
802 }
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (mod->init != NULL && mod->exit == NULL) {
805 printed_something = 1;
806 seq_printf(m, "[permanent],");
807 }
808
809 if (!printed_something)
810 seq_printf(m, "-");
811}
812
813void __symbol_put(const char *symbol)
814{
815 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Rusty Russell24da1cb2007-07-15 23:41:46 -0700817 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -0500818 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 BUG();
820 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700821 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823EXPORT_SYMBOL(__symbol_put);
824
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930825/* Note this assumes addr is a function, which it currently always is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826void symbol_put_addr(void *addr)
827{
Trent Piepho5e376612006-05-15 09:44:06 -0700828 struct module *modaddr;
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930829 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930831 if (core_kernel_text(a))
Trent Piepho5e376612006-05-15 09:44:06 -0700832 return;
833
Rusty Russella6e6abd2009-03-31 13:05:31 -0600834 /* module_text_address is safe here: we're supposed to have reference
835 * to module from symbol_get, so it can't go away. */
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930836 modaddr = __module_text_address(a);
Rusty Russella6e6abd2009-03-31 13:05:31 -0600837 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -0700838 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840EXPORT_SYMBOL_GPL(symbol_put_addr);
841
842static ssize_t show_refcnt(struct module_attribute *mattr,
843 struct module *mod, char *buffer)
844{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400845 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
848static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900849 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 .show = show_refcnt,
851};
852
Al Virof6a57032006-10-18 01:47:25 -0400853void module_put(struct module *module)
854{
855 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +0900856 preempt_disable();
Nick Piggin5fbfb182010-04-01 19:09:40 +1100857 smp_wmb(); /* see comment in module_refcount */
858 __this_cpu_inc(module->refptr->decs);
Christoph Lametere1783a22010-01-05 15:34:50 +0900859
Li Zefan7ead8b82009-08-17 16:56:28 +0800860 trace_module_put(module, _RET_IP_,
Nick Piggin5fbfb182010-04-01 19:09:40 +1100861 __this_cpu_read(module->refptr->decs));
Al Virof6a57032006-10-18 01:47:25 -0400862 /* Maybe they're waiting for us to drop reference? */
863 if (unlikely(!module_is_live(module)))
864 wake_up_process(module->waiter);
Christoph Lametere1783a22010-01-05 15:34:50 +0900865 preempt_enable();
Al Virof6a57032006-10-18 01:47:25 -0400866 }
867}
868EXPORT_SYMBOL(module_put);
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800871static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
873 /* We don't know the usage count, or what modules are using. */
874 seq_printf(m, " - -");
875}
876
877static inline void module_unload_free(struct module *mod)
878{
879}
880
Tim Abbottc6b37802008-12-05 19:03:59 -0500881int use_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500883 return strong_try_module_get(b) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
Tim Abbottc6b37802008-12-05 19:03:59 -0500885EXPORT_SYMBOL_GPL(use_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887static inline void module_unload_init(struct module *mod)
888{
889}
890#endif /* CONFIG_MODULE_UNLOAD */
891
Kay Sievers1f717402006-11-24 12:15:25 +0100892static ssize_t show_initstate(struct module_attribute *mattr,
893 struct module *mod, char *buffer)
894{
895 const char *state = "unknown";
896
897 switch (mod->state) {
898 case MODULE_STATE_LIVE:
899 state = "live";
900 break;
901 case MODULE_STATE_COMING:
902 state = "coming";
903 break;
904 case MODULE_STATE_GOING:
905 state = "going";
906 break;
907 }
908 return sprintf(buffer, "%s\n", state);
909}
910
911static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900912 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100913 .show = show_initstate,
914};
915
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800916static struct module_attribute *modinfo_attrs[] = {
917 &modinfo_version,
918 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100919 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800920#ifdef CONFIG_MODULE_UNLOAD
921 &refcnt,
922#endif
923 NULL,
924};
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926static const char vermagic[] = VERMAGIC_STRING;
927
Rusty Russellc6e665c2009-03-31 13:05:33 -0600928static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -0700929{
930#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -0700931 if (!test_taint(TAINT_FORCED_MODULE))
Rusty Russellc6e665c2009-03-31 13:05:33 -0600932 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
933 mod->name, reason);
Linus Torvalds826e4502008-05-04 17:04:16 -0700934 add_taint_module(mod, TAINT_FORCED_MODULE);
935 return 0;
936#else
937 return -ENOEXEC;
938#endif
939}
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941#ifdef CONFIG_MODVERSIONS
Rusty Russelld4703ae2009-12-15 16:28:32 -0600942/* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
943static unsigned long maybe_relocated(unsigned long crc,
944 const struct module *crc_owner)
945{
946#ifdef ARCH_RELOCATES_KCRCTAB
947 if (crc_owner == NULL)
948 return crc - (unsigned long)reloc_start;
949#endif
950 return crc;
951}
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953static int check_version(Elf_Shdr *sechdrs,
954 unsigned int versindex,
955 const char *symname,
956 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -0600957 const unsigned long *crc,
958 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 unsigned int i, num_versions;
961 struct modversion_info *versions;
962
963 /* Exporting module didn't supply crcs? OK, we're already tainted. */
964 if (!crc)
965 return 1;
966
Rusty Russella5dd6972008-05-09 16:24:21 +1000967 /* No versions at all? modprobe --force does this. */
968 if (versindex == 0)
969 return try_to_force_load(mod, symname) == 0;
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 versions = (void *) sechdrs[versindex].sh_addr;
972 num_versions = sechdrs[versindex].sh_size
973 / sizeof(struct modversion_info);
974
975 for (i = 0; i < num_versions; i++) {
976 if (strcmp(versions[i].name, symname) != 0)
977 continue;
978
Rusty Russelld4703ae2009-12-15 16:28:32 -0600979 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 DEBUGP("Found checksum %lX vs module %lX\n",
Rusty Russelld4703ae2009-12-15 16:28:32 -0600982 maybe_relocated(*crc, crc_owner), versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -0700983 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
Linus Torvalds826e4502008-05-04 17:04:16 -0700985
Rusty Russella5dd6972008-05-09 16:24:21 +1000986 printk(KERN_WARNING "%s: no symbol version for %s\n",
987 mod->name, symname);
988 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -0700989
990bad_version:
991 printk("%s: disagrees about version of symbol %s\n",
992 mod->name, symname);
993 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
996static inline int check_modstruct_version(Elf_Shdr *sechdrs,
997 unsigned int versindex,
998 struct module *mod)
999{
1000 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Mike Frysinger6560dc12009-07-23 23:42:08 +09301002 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1003 &crc, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 BUG();
Rusty Russelld4703ae2009-12-15 16:28:32 -06001005 return check_version(sechdrs, versindex, "module_layout", mod, crc,
1006 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
Rusty Russell91e37a72008-05-09 16:25:28 +10001009/* First part is kernel version, which we ignore if module has crcs. */
1010static inline int same_magic(const char *amagic, const char *bmagic,
1011 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Rusty Russell91e37a72008-05-09 16:25:28 +10001013 if (has_crcs) {
1014 amagic += strcspn(amagic, " ");
1015 bmagic += strcspn(bmagic, " ");
1016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return strcmp(amagic, bmagic) == 0;
1018}
1019#else
1020static inline int check_version(Elf_Shdr *sechdrs,
1021 unsigned int versindex,
1022 const char *symname,
1023 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001024 const unsigned long *crc,
1025 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
1027 return 1;
1028}
1029
1030static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1031 unsigned int versindex,
1032 struct module *mod)
1033{
1034 return 1;
1035}
1036
Rusty Russell91e37a72008-05-09 16:25:28 +10001037static inline int same_magic(const char *amagic, const char *bmagic,
1038 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 return strcmp(amagic, bmagic) == 0;
1041}
1042#endif /* CONFIG_MODVERSIONS */
1043
1044/* Resolve a symbol for this module. I.e. if we find one, record usage.
1045 Must be holding module_mutex. */
Tim Abbott414fd312008-12-05 19:03:56 -05001046static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
1047 unsigned int versindex,
1048 const char *name,
1049 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
1051 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001052 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 const unsigned long *crc;
1054
Tim Abbott414fd312008-12-05 19:03:56 -05001055 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001056 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Tim Abbott414fd312008-12-05 19:03:56 -05001057 /* use_module can fail due to OOM,
1058 or module initialization or unloading */
1059 if (sym) {
Rusty Russelld4703ae2009-12-15 16:28:32 -06001060 if (!check_version(sechdrs, versindex, name, mod, crc, owner)
1061 || !use_module(mod, owner))
Tim Abbott414fd312008-12-05 19:03:56 -05001062 sym = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 }
Tim Abbott414fd312008-12-05 19:03:56 -05001064 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067/*
1068 * /sys/module/foo/sections stuff
1069 * J. Corbet <corbet@lwn.net>
1070 */
Kay Sievers120fc3d2008-02-21 00:33:20 +01001071#if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
Ben Hutchings10b465a2009-12-19 14:43:01 +00001072
1073static inline bool sect_empty(const Elf_Shdr *sect)
1074{
1075 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1076}
1077
Rusty Russella58730c2008-03-13 09:03:44 +00001078struct module_sect_attr
1079{
1080 struct module_attribute mattr;
1081 char *name;
1082 unsigned long address;
1083};
1084
1085struct module_sect_attrs
1086{
1087 struct attribute_group grp;
1088 unsigned int nsections;
1089 struct module_sect_attr attrs[0];
1090};
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092static ssize_t module_sect_show(struct module_attribute *mattr,
1093 struct module *mod, char *buf)
1094{
1095 struct module_sect_attr *sattr =
1096 container_of(mattr, struct module_sect_attr, mattr);
1097 return sprintf(buf, "0x%lx\n", sattr->address);
1098}
1099
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001100static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1101{
Rusty Russella58730c2008-03-13 09:03:44 +00001102 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001103
1104 for (section = 0; section < sect_attrs->nsections; section++)
1105 kfree(sect_attrs->attrs[section].name);
1106 kfree(sect_attrs);
1107}
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109static void add_sect_attrs(struct module *mod, unsigned int nsect,
1110 char *secstrings, Elf_Shdr *sechdrs)
1111{
1112 unsigned int nloaded = 0, i, size[2];
1113 struct module_sect_attrs *sect_attrs;
1114 struct module_sect_attr *sattr;
1115 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 /* Count loaded sections and allocate structures */
1118 for (i = 0; i < nsect; i++)
Ben Hutchings10b465a2009-12-19 14:43:01 +00001119 if (!sect_empty(&sechdrs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 nloaded++;
1121 size[0] = ALIGN(sizeof(*sect_attrs)
1122 + nloaded * sizeof(sect_attrs->attrs[0]),
1123 sizeof(sect_attrs->grp.attrs[0]));
1124 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001125 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1126 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return;
1128
1129 /* Setup section attributes. */
1130 sect_attrs->grp.name = "sections";
1131 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1132
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001133 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 sattr = &sect_attrs->attrs[0];
1135 gattr = &sect_attrs->grp.attrs[0];
1136 for (i = 0; i < nsect; i++) {
Ben Hutchings10b465a2009-12-19 14:43:01 +00001137 if (sect_empty(&sechdrs[i]))
Helge Deller35dead42009-12-03 00:29:15 +01001138 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 sattr->address = sechdrs[i].sh_addr;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001140 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1141 GFP_KERNEL);
1142 if (sattr->name == NULL)
1143 goto out;
1144 sect_attrs->nsections++;
Eric W. Biederman361795b2010-02-12 13:41:56 -08001145 sysfs_attr_init(&sattr->mattr.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 sattr->mattr.show = module_sect_show;
1147 sattr->mattr.store = NULL;
1148 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 sattr->mattr.attr.mode = S_IRUGO;
1150 *(gattr++) = &(sattr++)->mattr.attr;
1151 }
1152 *gattr = NULL;
1153
1154 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1155 goto out;
1156
1157 mod->sect_attrs = sect_attrs;
1158 return;
1159 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001160 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
1163static void remove_sect_attrs(struct module *mod)
1164{
1165 if (mod->sect_attrs) {
1166 sysfs_remove_group(&mod->mkobj.kobj,
1167 &mod->sect_attrs->grp);
1168 /* We are positive that no one is using any sect attrs
1169 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001170 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 mod->sect_attrs = NULL;
1172 }
1173}
1174
Roland McGrath6d760132007-10-16 23:26:40 -07001175/*
1176 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1177 */
1178
1179struct module_notes_attrs {
1180 struct kobject *dir;
1181 unsigned int notes;
1182 struct bin_attribute attrs[0];
1183};
1184
1185static ssize_t module_notes_read(struct kobject *kobj,
1186 struct bin_attribute *bin_attr,
1187 char *buf, loff_t pos, size_t count)
1188{
1189 /*
1190 * The caller checked the pos and count against our size.
1191 */
1192 memcpy(buf, bin_attr->private + pos, count);
1193 return count;
1194}
1195
1196static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1197 unsigned int i)
1198{
1199 if (notes_attrs->dir) {
1200 while (i-- > 0)
1201 sysfs_remove_bin_file(notes_attrs->dir,
1202 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001203 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001204 }
1205 kfree(notes_attrs);
1206}
1207
1208static void add_notes_attrs(struct module *mod, unsigned int nsect,
1209 char *secstrings, Elf_Shdr *sechdrs)
1210{
1211 unsigned int notes, loaded, i;
1212 struct module_notes_attrs *notes_attrs;
1213 struct bin_attribute *nattr;
1214
Ingo Molnarea6bff32009-08-28 10:44:56 +02001215 /* failed to create section attributes, so can't create notes */
1216 if (!mod->sect_attrs)
1217 return;
1218
Roland McGrath6d760132007-10-16 23:26:40 -07001219 /* Count notes sections and allocate structures. */
1220 notes = 0;
1221 for (i = 0; i < nsect; i++)
Ben Hutchings10b465a2009-12-19 14:43:01 +00001222 if (!sect_empty(&sechdrs[i]) &&
Roland McGrath6d760132007-10-16 23:26:40 -07001223 (sechdrs[i].sh_type == SHT_NOTE))
1224 ++notes;
1225
1226 if (notes == 0)
1227 return;
1228
1229 notes_attrs = kzalloc(sizeof(*notes_attrs)
1230 + notes * sizeof(notes_attrs->attrs[0]),
1231 GFP_KERNEL);
1232 if (notes_attrs == NULL)
1233 return;
1234
1235 notes_attrs->notes = notes;
1236 nattr = &notes_attrs->attrs[0];
1237 for (loaded = i = 0; i < nsect; ++i) {
Ben Hutchings10b465a2009-12-19 14:43:01 +00001238 if (sect_empty(&sechdrs[i]))
Roland McGrath6d760132007-10-16 23:26:40 -07001239 continue;
1240 if (sechdrs[i].sh_type == SHT_NOTE) {
Eric W. Biederman361795b2010-02-12 13:41:56 -08001241 sysfs_bin_attr_init(nattr);
Roland McGrath6d760132007-10-16 23:26:40 -07001242 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1243 nattr->attr.mode = S_IRUGO;
1244 nattr->size = sechdrs[i].sh_size;
1245 nattr->private = (void *) sechdrs[i].sh_addr;
1246 nattr->read = module_notes_read;
1247 ++nattr;
1248 }
1249 ++loaded;
1250 }
1251
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001252 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001253 if (!notes_attrs->dir)
1254 goto out;
1255
1256 for (i = 0; i < notes; ++i)
1257 if (sysfs_create_bin_file(notes_attrs->dir,
1258 &notes_attrs->attrs[i]))
1259 goto out;
1260
1261 mod->notes_attrs = notes_attrs;
1262 return;
1263
1264 out:
1265 free_notes_attrs(notes_attrs, i);
1266}
1267
1268static void remove_notes_attrs(struct module *mod)
1269{
1270 if (mod->notes_attrs)
1271 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1272}
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1277 char *sectstrings, Elf_Shdr *sechdrs)
1278{
1279}
1280
1281static inline void remove_sect_attrs(struct module *mod)
1282{
1283}
Roland McGrath6d760132007-10-16 23:26:40 -07001284
1285static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1286 char *sectstrings, Elf_Shdr *sechdrs)
1287{
1288}
1289
1290static inline void remove_notes_attrs(struct module *mod)
1291{
1292}
Kay Sievers120fc3d2008-02-21 00:33:20 +01001293#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Randy Dunlapef665c12007-02-13 15:19:06 -08001295#ifdef CONFIG_SYSFS
1296int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001297{
1298 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001299 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001300 int error = 0;
1301 int i;
1302
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001303 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1304 (ARRAY_SIZE(modinfo_attrs) + 1)),
1305 GFP_KERNEL);
1306 if (!mod->modinfo_attrs)
1307 return -ENOMEM;
1308
1309 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001310 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1311 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001312 (attr->test && attr->test(mod))) {
1313 memcpy(temp_attr, attr, sizeof(*temp_attr));
Eric W. Biederman361795b2010-02-12 13:41:56 -08001314 sysfs_attr_init(&temp_attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001315 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1316 ++temp_attr;
1317 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001318 }
1319 return error;
1320}
1321
Randy Dunlapef665c12007-02-13 15:19:06 -08001322void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001323{
1324 struct module_attribute *attr;
1325 int i;
1326
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001327 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1328 /* pick a field to test for end of list */
1329 if (!attr->attr.name)
1330 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001331 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001332 if (attr->free)
1333 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001334 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001335 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001336}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Randy Dunlapef665c12007-02-13 15:19:06 -08001338int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001341 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001343 if (!module_sysfs_initialized) {
1344 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001345 mod->name);
1346 err = -EINVAL;
1347 goto out;
1348 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001349
1350 kobj = kset_find_obj(module_kset, mod->name);
1351 if (kobj) {
1352 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1353 kobject_put(kobj);
1354 err = -EINVAL;
1355 goto out;
1356 }
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001359
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001360 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1361 mod->mkobj.kobj.kset = module_kset;
1362 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1363 "%s", mod->name);
1364 if (err)
1365 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001366
Kay Sievers97c146e2007-11-29 23:46:11 +01001367 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001368out:
1369 return err;
1370}
1371
Randy Dunlapef665c12007-02-13 15:19:06 -08001372int mod_sysfs_setup(struct module *mod,
Kay Sievers270a6c42007-01-18 13:26:15 +01001373 struct kernel_param *kparam,
1374 unsigned int num_params)
1375{
1376 int err;
1377
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001378 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001379 if (!mod->holders_dir) {
1380 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001381 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001382 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 err = module_param_sysfs_setup(mod, kparam, num_params);
1385 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001386 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Matt Domschc988d2b2005-06-23 22:05:15 -07001388 err = module_add_modinfo_attrs(mod);
1389 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001390 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001391
Kay Sieverse17e0f52006-11-24 12:15:25 +01001392 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return 0;
1394
Kay Sieverse17e0f52006-11-24 12:15:25 +01001395out_unreg_param:
1396 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001397out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001398 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001399out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001400 kobject_put(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 return err;
1402}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001403
1404static void mod_sysfs_fini(struct module *mod)
1405{
1406 kobject_put(&mod->mkobj.kobj);
1407}
1408
1409#else /* CONFIG_SYSFS */
1410
1411static void mod_sysfs_fini(struct module *mod)
1412{
1413}
1414
1415#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417static void mod_kobject_remove(struct module *mod)
1418{
Matt Domschc988d2b2005-06-23 22:05:15 -07001419 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001421 kobject_put(mod->mkobj.drivers_dir);
1422 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001423 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
1425
1426/*
1427 * unlink the module with the whole machine is stopped with interrupts off
1428 * - this defends against kallsyms not taking locks
1429 */
1430static int __unlink_module(void *_mod)
1431{
1432 struct module *mod = _mod;
1433 list_del(&mod->list);
1434 return 0;
1435}
1436
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001437/* Free a module, remove from lists, etc (must hold module_mutex). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438static void free_module(struct module *mod)
1439{
Li Zefan7ead8b82009-08-17 16:56:28 +08001440 trace_module_free(mod);
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 /* Delete from various lists */
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001443 stop_machine(__unlink_module, mod, NULL);
Roland McGrath6d760132007-10-16 23:26:40 -07001444 remove_notes_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 remove_sect_attrs(mod);
1446 mod_kobject_remove(mod);
1447
1448 /* Arch-specific cleanup. */
1449 module_arch_cleanup(mod);
1450
1451 /* Module unload stuff */
1452 module_unload_free(mod);
1453
Rusty Russelle180a6b2009-03-31 13:05:29 -06001454 /* Free any allocated parameters. */
1455 destroy_params(mod->kp, mod->num_kp);
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 /* This may be NULL, but that's OK */
1458 module_free(mod, mod->module_init);
1459 kfree(mod->args);
Tejun Heo259354d2010-03-10 18:56:10 +09001460 percpu_modfree(mod);
Christoph Lametere1783a22010-01-05 15:34:50 +09001461#if defined(CONFIG_MODULE_UNLOAD)
Eric Dumazet720eba32009-02-03 13:31:36 +10301462 if (mod->refptr)
Christoph Lametere1783a22010-01-05 15:34:50 +09001463 free_percpu(mod->refptr);
Eric Dumazet720eba32009-02-03 13:31:36 +10301464#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001465 /* Free lock-classes: */
1466 lockdep_free_key_range(mod->module_core, mod->core_size);
1467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 /* Finally, free the core (containing the module structure) */
1469 module_free(mod, mod->module_core);
Bernd Schmidteb8cdec2009-09-21 17:03:57 -07001470
1471#ifdef CONFIG_MPU
1472 update_protections(current->mm);
1473#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
1475
1476void *__symbol_get(const char *symbol)
1477{
1478 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001479 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Rusty Russell24da1cb2007-07-15 23:41:46 -07001481 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001482 sym = find_symbol(symbol, &owner, NULL, true, true);
1483 if (sym && strong_try_module_get(owner))
1484 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001485 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Tim Abbott414fd312008-12-05 19:03:56 -05001487 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
1489EXPORT_SYMBOL_GPL(__symbol_get);
1490
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001491/*
1492 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001493 * in the kernel or in some other module's exported symbol table.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001494 */
1495static int verify_export_symbols(struct module *mod)
1496{
Rusty Russellb2111042008-05-01 21:15:00 -05001497 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001498 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001499 const struct kernel_symbol *s;
1500 struct {
1501 const struct kernel_symbol *sym;
1502 unsigned int num;
1503 } arr[] = {
1504 { mod->syms, mod->num_syms },
1505 { mod->gpl_syms, mod->num_gpl_syms },
1506 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001507#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001508 { mod->unused_syms, mod->num_unused_syms },
1509 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001510#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001511 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001512
Rusty Russellb2111042008-05-01 21:15:00 -05001513 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1514 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Tim Abbott414fd312008-12-05 19:03:56 -05001515 if (find_symbol(s->name, &owner, NULL, true, false)) {
Rusty Russellb2111042008-05-01 21:15:00 -05001516 printk(KERN_ERR
1517 "%s: exports duplicate symbol %s"
1518 " (owned by %s)\n",
1519 mod->name, s->name, module_name(owner));
1520 return -ENOEXEC;
1521 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001522 }
Rusty Russellb2111042008-05-01 21:15:00 -05001523 }
1524 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001525}
1526
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001527/* Change all symbols so that st_value encodes the pointer directly. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528static int simplify_symbols(Elf_Shdr *sechdrs,
1529 unsigned int symindex,
1530 const char *strtab,
1531 unsigned int versindex,
1532 unsigned int pcpuindex,
1533 struct module *mod)
1534{
1535 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1536 unsigned long secbase;
1537 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1538 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05001539 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 for (i = 1; i < n; i++) {
1542 switch (sym[i].st_shndx) {
1543 case SHN_COMMON:
1544 /* We compiled with -fno-common. These are not
1545 supposed to happen. */
1546 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1547 printk("%s: please compile with -fno-common\n",
1548 mod->name);
1549 ret = -ENOEXEC;
1550 break;
1551
1552 case SHN_ABS:
1553 /* Don't need to do anything */
1554 DEBUGP("Absolute symbol: 0x%08lx\n",
1555 (long)sym[i].st_value);
1556 break;
1557
1558 case SHN_UNDEF:
Tim Abbott414fd312008-12-05 19:03:56 -05001559 ksym = resolve_symbol(sechdrs, versindex,
1560 strtab + sym[i].st_name, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 /* Ok if resolved. */
Tim Abbott414fd312008-12-05 19:03:56 -05001562 if (ksym) {
1563 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 break;
Tim Abbott414fd312008-12-05 19:03:56 -05001565 }
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 /* Ok if weak. */
1568 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1569 break;
1570
1571 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1572 mod->name, strtab + sym[i].st_name);
1573 ret = -ENOENT;
1574 break;
1575
1576 default:
1577 /* Divert to percpu allocation if a percpu var. */
1578 if (sym[i].st_shndx == pcpuindex)
Tejun Heo259354d2010-03-10 18:56:10 +09001579 secbase = (unsigned long)mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 else
1581 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1582 sym[i].st_value += secbase;
1583 break;
1584 }
1585 }
1586
1587 return ret;
1588}
1589
Helge Deller088af9a2008-12-31 12:31:18 +01001590/* Additional bytes needed by arch in front of individual sections */
1591unsigned int __weak arch_mod_section_prepend(struct module *mod,
1592 unsigned int section)
1593{
1594 /* default implementation just returns zero */
1595 return 0;
1596}
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01001599static long get_offset(struct module *mod, unsigned int *size,
1600 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
1602 long ret;
1603
Helge Deller088af9a2008-12-31 12:31:18 +01001604 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1606 *size = ret + sechdr->sh_size;
1607 return ret;
1608}
1609
1610/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1611 might -- code, read-only data, read-write data, small data. Tally
1612 sizes, and place the offsets into sh_entsize fields: high bit means it
1613 belongs in init. */
1614static void layout_sections(struct module *mod,
1615 const Elf_Ehdr *hdr,
1616 Elf_Shdr *sechdrs,
1617 const char *secstrings)
1618{
1619 static unsigned long const masks[][2] = {
1620 /* NOTE: all executable code must be the first section
1621 * in this array; otherwise modify the text_size
1622 * finder in the two loops below */
1623 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1624 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1625 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1626 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1627 };
1628 unsigned int m, i;
1629
1630 for (i = 0; i < hdr->e_shnum; i++)
1631 sechdrs[i].sh_entsize = ~0UL;
1632
1633 DEBUGP("Core section allocation order:\n");
1634 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1635 for (i = 0; i < hdr->e_shnum; ++i) {
1636 Elf_Shdr *s = &sechdrs[i];
1637
1638 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1639 || (s->sh_flags & masks[m][1])
1640 || s->sh_entsize != ~0UL
Rusty Russell49502672009-03-31 13:05:36 -06001641 || strstarts(secstrings + s->sh_name, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001643 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 DEBUGP("\t%s\n", secstrings + s->sh_name);
1645 }
1646 if (m == 0)
1647 mod->core_text_size = mod->core_size;
1648 }
1649
1650 DEBUGP("Init section allocation order:\n");
1651 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1652 for (i = 0; i < hdr->e_shnum; ++i) {
1653 Elf_Shdr *s = &sechdrs[i];
1654
1655 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1656 || (s->sh_flags & masks[m][1])
1657 || s->sh_entsize != ~0UL
Rusty Russell49502672009-03-31 13:05:36 -06001658 || !strstarts(secstrings + s->sh_name, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001660 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 | INIT_OFFSET_MASK);
1662 DEBUGP("\t%s\n", secstrings + s->sh_name);
1663 }
1664 if (m == 0)
1665 mod->init_text_size = mod->init_size;
1666 }
1667}
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669static void set_license(struct module *mod, const char *license)
1670{
1671 if (!license)
1672 license = "unspecified";
1673
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001674 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07001675 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001676 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001677 "kernel.\n", mod->name, license);
1678 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
1680}
1681
1682/* Parse tag=value strings from .modinfo section */
1683static char *next_string(char *string, unsigned long *secsize)
1684{
1685 /* Skip non-zero chars */
1686 while (string[0]) {
1687 string++;
1688 if ((*secsize)-- <= 1)
1689 return NULL;
1690 }
1691
1692 /* Skip any zero padding. */
1693 while (!string[0]) {
1694 string++;
1695 if ((*secsize)-- <= 1)
1696 return NULL;
1697 }
1698 return string;
1699}
1700
1701static char *get_modinfo(Elf_Shdr *sechdrs,
1702 unsigned int info,
1703 const char *tag)
1704{
1705 char *p;
1706 unsigned int taglen = strlen(tag);
1707 unsigned long size = sechdrs[info].sh_size;
1708
1709 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1710 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1711 return p + taglen + 1;
1712 }
1713 return NULL;
1714}
1715
Matt Domschc988d2b2005-06-23 22:05:15 -07001716static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1717 unsigned int infoindex)
1718{
1719 struct module_attribute *attr;
1720 int i;
1721
1722 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1723 if (attr->setup)
1724 attr->setup(mod,
1725 get_modinfo(sechdrs,
1726 infoindex,
1727 attr->attr.name));
1728 }
1729}
Matt Domschc988d2b2005-06-23 22:05:15 -07001730
Rusty Russella263f772009-09-25 00:32:58 -06001731static void free_modinfo(struct module *mod)
1732{
1733 struct module_attribute *attr;
1734 int i;
1735
1736 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1737 if (attr->free)
1738 attr->free(mod);
1739 }
1740}
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01001743
1744/* lookup symbol in given range of kernel_symbols */
1745static const struct kernel_symbol *lookup_symbol(const char *name,
1746 const struct kernel_symbol *start,
1747 const struct kernel_symbol *stop)
1748{
1749 const struct kernel_symbol *ks = start;
1750 for (; ks < stop; ks++)
1751 if (strcmp(ks->name, name) == 0)
1752 return ks;
1753 return NULL;
1754}
1755
Tim Abbottca4787b2009-01-05 08:40:10 -06001756static int is_exported(const char *name, unsigned long value,
1757 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
Tim Abbottca4787b2009-01-05 08:40:10 -06001759 const struct kernel_symbol *ks;
1760 if (!mod)
1761 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001762 else
Tim Abbottca4787b2009-01-05 08:40:10 -06001763 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
1764 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765}
1766
1767/* As per nm */
1768static char elf_type(const Elf_Sym *sym,
1769 Elf_Shdr *sechdrs,
1770 const char *secstrings,
1771 struct module *mod)
1772{
1773 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1774 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1775 return 'v';
1776 else
1777 return 'w';
1778 }
1779 if (sym->st_shndx == SHN_UNDEF)
1780 return 'U';
1781 if (sym->st_shndx == SHN_ABS)
1782 return 'a';
1783 if (sym->st_shndx >= SHN_LORESERVE)
1784 return '?';
1785 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1786 return 't';
1787 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1788 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1789 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1790 return 'r';
1791 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1792 return 'g';
1793 else
1794 return 'd';
1795 }
1796 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1797 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1798 return 's';
1799 else
1800 return 'b';
1801 }
Rusty Russell49502672009-03-31 13:05:36 -06001802 if (strstarts(secstrings + sechdrs[sym->st_shndx].sh_name, ".debug"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 return 'n';
1804 return '?';
1805}
1806
Jan Beulich4a496222009-07-06 14:50:42 +01001807static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
1808 unsigned int shnum)
1809{
1810 const Elf_Shdr *sec;
1811
1812 if (src->st_shndx == SHN_UNDEF
1813 || src->st_shndx >= shnum
1814 || !src->st_name)
1815 return false;
1816
1817 sec = sechdrs + src->st_shndx;
1818 if (!(sec->sh_flags & SHF_ALLOC)
1819#ifndef CONFIG_KALLSYMS_ALL
1820 || !(sec->sh_flags & SHF_EXECINSTR)
1821#endif
1822 || (sec->sh_entsize & INIT_OFFSET_MASK))
1823 return false;
1824
1825 return true;
1826}
1827
1828static unsigned long layout_symtab(struct module *mod,
1829 Elf_Shdr *sechdrs,
1830 unsigned int symindex,
Jan Beulich554bdfe2009-07-06 14:51:44 +01001831 unsigned int strindex,
Jan Beulich4a496222009-07-06 14:50:42 +01001832 const Elf_Ehdr *hdr,
Jan Beulich554bdfe2009-07-06 14:51:44 +01001833 const char *secstrings,
1834 unsigned long *pstroffs,
1835 unsigned long *strmap)
Jan Beulich4a496222009-07-06 14:50:42 +01001836{
1837 unsigned long symoffs;
1838 Elf_Shdr *symsect = sechdrs + symindex;
Jan Beulich554bdfe2009-07-06 14:51:44 +01001839 Elf_Shdr *strsect = sechdrs + strindex;
Jan Beulich4a496222009-07-06 14:50:42 +01001840 const Elf_Sym *src;
Jan Beulich554bdfe2009-07-06 14:51:44 +01001841 const char *strtab;
Jan Beulich4a496222009-07-06 14:50:42 +01001842 unsigned int i, nsrc, ndst;
1843
1844 /* Put symbol section at end of init part of module. */
1845 symsect->sh_flags |= SHF_ALLOC;
1846 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
1847 symindex) | INIT_OFFSET_MASK;
1848 DEBUGP("\t%s\n", secstrings + symsect->sh_name);
1849
1850 src = (void *)hdr + symsect->sh_offset;
1851 nsrc = symsect->sh_size / sizeof(*src);
Jan Beulich554bdfe2009-07-06 14:51:44 +01001852 strtab = (void *)hdr + strsect->sh_offset;
Jan Beulich4a496222009-07-06 14:50:42 +01001853 for (ndst = i = 1; i < nsrc; ++i, ++src)
Jan Beulich554bdfe2009-07-06 14:51:44 +01001854 if (is_core_symbol(src, sechdrs, hdr->e_shnum)) {
1855 unsigned int j = src->st_name;
1856
1857 while(!__test_and_set_bit(j, strmap) && strtab[j])
1858 ++j;
Jan Beulich4a496222009-07-06 14:50:42 +01001859 ++ndst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01001860 }
Jan Beulich4a496222009-07-06 14:50:42 +01001861
1862 /* Append room for core symbols at end of core part. */
1863 symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
1864 mod->core_size = symoffs + ndst * sizeof(Elf_Sym);
1865
Jan Beulich554bdfe2009-07-06 14:51:44 +01001866 /* Put string table section at end of init part of module. */
1867 strsect->sh_flags |= SHF_ALLOC;
1868 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
1869 strindex) | INIT_OFFSET_MASK;
1870 DEBUGP("\t%s\n", secstrings + strsect->sh_name);
1871
1872 /* Append room for core symbols' strings at end of core part. */
1873 *pstroffs = mod->core_size;
1874 __set_bit(0, strmap);
1875 mod->core_size += bitmap_weight(strmap, strsect->sh_size);
1876
Jan Beulich4a496222009-07-06 14:50:42 +01001877 return symoffs;
1878}
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880static void add_kallsyms(struct module *mod,
1881 Elf_Shdr *sechdrs,
Jan Beulich4a496222009-07-06 14:50:42 +01001882 unsigned int shnum,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 unsigned int symindex,
1884 unsigned int strindex,
Jan Beulich4a496222009-07-06 14:50:42 +01001885 unsigned long symoffs,
Jan Beulich554bdfe2009-07-06 14:51:44 +01001886 unsigned long stroffs,
1887 const char *secstrings,
1888 unsigned long *strmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Jan Beulich4a496222009-07-06 14:50:42 +01001890 unsigned int i, ndst;
1891 const Elf_Sym *src;
1892 Elf_Sym *dst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01001893 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
1895 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1896 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1897 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1898
1899 /* Set types up while we still have access to sections. */
1900 for (i = 0; i < mod->num_symtab; i++)
1901 mod->symtab[i].st_info
1902 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
Jan Beulich4a496222009-07-06 14:50:42 +01001903
1904 mod->core_symtab = dst = mod->module_core + symoffs;
1905 src = mod->symtab;
1906 *dst = *src;
1907 for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
1908 if (!is_core_symbol(src, sechdrs, shnum))
1909 continue;
1910 dst[ndst] = *src;
Jan Beulich554bdfe2009-07-06 14:51:44 +01001911 dst[ndst].st_name = bitmap_weight(strmap, dst[ndst].st_name);
Jan Beulich4a496222009-07-06 14:50:42 +01001912 ++ndst;
1913 }
1914 mod->core_num_syms = ndst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01001915
1916 mod->core_strtab = s = mod->module_core + stroffs;
1917 for (*s = 0, i = 1; i < sechdrs[strindex].sh_size; ++i)
1918 if (test_bit(i, strmap))
1919 *++s = mod->strtab[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920}
1921#else
Jan Beulich4a496222009-07-06 14:50:42 +01001922static inline unsigned long layout_symtab(struct module *mod,
1923 Elf_Shdr *sechdrs,
1924 unsigned int symindex,
Jan Beulich554bdfe2009-07-06 14:51:44 +01001925 unsigned int strindex,
Paul Mundt3ae91c22009-10-01 15:43:54 -07001926 const Elf_Ehdr *hdr,
Jan Beulich554bdfe2009-07-06 14:51:44 +01001927 const char *secstrings,
1928 unsigned long *pstroffs,
1929 unsigned long *strmap)
Jan Beulich4a496222009-07-06 14:50:42 +01001930{
Paul Mundt3ae91c22009-10-01 15:43:54 -07001931 return 0;
Jan Beulich4a496222009-07-06 14:50:42 +01001932}
Paul Mundt3ae91c22009-10-01 15:43:54 -07001933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934static inline void add_kallsyms(struct module *mod,
1935 Elf_Shdr *sechdrs,
Jan Beulich4a496222009-07-06 14:50:42 +01001936 unsigned int shnum,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 unsigned int symindex,
1938 unsigned int strindex,
Jan Beulich4a496222009-07-06 14:50:42 +01001939 unsigned long symoffs,
Jan Beulich554bdfe2009-07-06 14:51:44 +01001940 unsigned long stroffs,
1941 const char *secstrings,
1942 const unsigned long *strmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
1944}
1945#endif /* CONFIG_KALLSYMS */
1946
Jason Barone9d376f2009-02-05 11:51:38 -05001947static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05001948{
Jason Barone9d376f2009-02-05 11:51:38 -05001949#ifdef CONFIG_DYNAMIC_DEBUG
1950 if (ddebug_add_module(debug, num, debug->modname))
1951 printk(KERN_ERR "dynamic debug error adding module: %s\n",
1952 debug->modname);
1953#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05001954}
Jason Baron346e15b2008-08-12 16:46:19 -04001955
Rusty Russell3a642e92008-07-22 19:24:28 -05001956static void *module_alloc_update_bounds(unsigned long size)
1957{
1958 void *ret = module_alloc(size);
1959
1960 if (ret) {
1961 /* Update module bounds. */
1962 if ((unsigned long)ret < module_addr_min)
1963 module_addr_min = (unsigned long)ret;
1964 if ((unsigned long)ret + size > module_addr_max)
1965 module_addr_max = (unsigned long)ret + size;
1966 }
1967 return ret;
1968}
1969
Catalin Marinas4f2294b2009-06-11 13:23:20 +01001970#ifdef CONFIG_DEBUG_KMEMLEAK
1971static void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
1972 Elf_Shdr *sechdrs, char *secstrings)
1973{
1974 unsigned int i;
1975
1976 /* only scan the sections containing data */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001977 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01001978
1979 for (i = 1; i < hdr->e_shnum; i++) {
1980 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1981 continue;
1982 if (strncmp(secstrings + sechdrs[i].sh_name, ".data", 5) != 0
1983 && strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) != 0)
1984 continue;
1985
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001986 kmemleak_scan_area((void *)sechdrs[i].sh_addr,
Catalin Marinas4f2294b2009-06-11 13:23:20 +01001987 sechdrs[i].sh_size, GFP_KERNEL);
1988 }
1989}
1990#else
1991static inline void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
1992 Elf_Shdr *sechdrs, char *secstrings)
1993{
1994}
1995#endif
1996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997/* Allocate and load the module: note that size of section 0 is always
1998 zero, and we rely on this for optional sections. */
Linus Torvaldsffb4ba72008-08-25 11:10:26 -07001999static noinline struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 unsigned long len,
2001 const char __user *uargs)
2002{
2003 Elf_Ehdr *hdr;
2004 Elf_Shdr *sechdrs;
2005 char *secstrings, *args, *modmagic, *strtab = NULL;
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002006 char *staging;
Andrew Morton84860f92006-06-28 04:26:46 -07002007 unsigned int i;
2008 unsigned int symindex = 0;
2009 unsigned int strindex = 0;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002010 unsigned int modindex, versindex, infoindex, pcpuindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 struct module *mod;
2012 long err = 0;
Tejun Heo259354d2010-03-10 18:56:10 +09002013 void *ptr = NULL; /* Stops spurious gcc warning */
Jan Beulich554bdfe2009-07-06 14:51:44 +01002014 unsigned long symoffs, stroffs, *strmap;
Paul Mundt3ae91c22009-10-01 15:43:54 -07002015
Thomas Koeller378bac82005-09-06 15:17:11 -07002016 mm_segment_t old_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
2018 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
2019 umod, len, uargs);
2020 if (len < sizeof(*hdr))
2021 return ERR_PTR(-ENOEXEC);
2022
2023 /* Suck in entire file: we'll want most of it. */
2024 /* vmalloc barfs on "unusual" numbers. Check here */
2025 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
2026 return ERR_PTR(-ENOMEM);
Heiko Carstens9e018922008-12-22 12:36:31 +01002027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 if (copy_from_user(hdr, umod, len) != 0) {
2029 err = -EFAULT;
2030 goto free_hdr;
2031 }
2032
2033 /* Sanity checks against insmoding binaries or wrong arch,
2034 weird elf version */
Cyrill Gorcunovc4ea6fc2008-05-14 16:27:29 -07002035 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 || hdr->e_type != ET_REL
2037 || !elf_check_arch(hdr)
2038 || hdr->e_shentsize != sizeof(*sechdrs)) {
2039 err = -ENOEXEC;
2040 goto free_hdr;
2041 }
2042
2043 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
2044 goto truncated;
2045
2046 /* Convenience variables */
2047 sechdrs = (void *)hdr + hdr->e_shoff;
2048 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
2049 sechdrs[0].sh_addr = 0;
2050
2051 for (i = 1; i < hdr->e_shnum; i++) {
2052 if (sechdrs[i].sh_type != SHT_NOBITS
2053 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
2054 goto truncated;
2055
2056 /* Mark all sections sh_addr with their address in the
2057 temporary image. */
2058 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
2059
2060 /* Internal symbols and strings. */
2061 if (sechdrs[i].sh_type == SHT_SYMTAB) {
2062 symindex = i;
2063 strindex = sechdrs[i].sh_link;
2064 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
2065 }
2066#ifndef CONFIG_MODULE_UNLOAD
2067 /* Don't load .exit sections */
Rusty Russell49502672009-03-31 13:05:36 -06002068 if (strstarts(secstrings+sechdrs[i].sh_name, ".exit"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
2070#endif
2071 }
2072
2073 modindex = find_sec(hdr, sechdrs, secstrings,
2074 ".gnu.linkonce.this_module");
2075 if (!modindex) {
2076 printk(KERN_WARNING "No module found in object\n");
2077 err = -ENOEXEC;
2078 goto free_hdr;
2079 }
Rusty Russell5e458cc2008-10-22 10:00:13 -05002080 /* This is temporary: point mod into copy of data. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 mod = (void *)sechdrs[modindex].sh_addr;
2082
2083 if (symindex == 0) {
2084 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2085 mod->name);
2086 err = -ENOEXEC;
2087 goto free_hdr;
2088 }
2089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
2091 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
2092 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
2093
Rusty Russellea01e792008-03-13 09:02:17 +00002094 /* Don't keep modinfo and version sections. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russellea01e792008-03-13 09:02:17 +00002096 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098 /* Check module struct version now, before we try to use module. */
2099 if (!check_modstruct_version(sechdrs, versindex, mod)) {
2100 err = -ENOEXEC;
2101 goto free_hdr;
2102 }
2103
2104 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
2105 /* This is allowed: modprobe --force will invalidate it. */
2106 if (!modmagic) {
Rusty Russellc6e665c2009-03-31 13:05:33 -06002107 err = try_to_force_load(mod, "bad vermagic");
Linus Torvalds826e4502008-05-04 17:04:16 -07002108 if (err)
2109 goto free_hdr;
Rusty Russell91e37a72008-05-09 16:25:28 +10002110 } else if (!same_magic(modmagic, vermagic, versindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2112 mod->name, modmagic, vermagic);
2113 err = -ENOEXEC;
2114 goto free_hdr;
2115 }
2116
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002117 staging = get_modinfo(sechdrs, infoindex, "staging");
2118 if (staging) {
2119 add_taint_module(mod, TAINT_CRAP);
2120 printk(KERN_WARNING "%s: module is from the staging directory,"
2121 " the quality is unknown, you have been warned.\n",
2122 mod->name);
2123 }
2124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 /* Now copy in args */
Davi Arnaut24277dd2006-03-24 03:18:43 -08002126 args = strndup_user(uargs, ~0UL >> 1);
2127 if (IS_ERR(args)) {
2128 err = PTR_ERR(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 goto free_hdr;
2130 }
Andrew Morton8e08b752006-02-07 12:58:45 -08002131
Jan Beulich554bdfe2009-07-06 14:51:44 +01002132 strmap = kzalloc(BITS_TO_LONGS(sechdrs[strindex].sh_size)
2133 * sizeof(long), GFP_KERNEL);
2134 if (!strmap) {
2135 err = -ENOMEM;
2136 goto free_mod;
2137 }
2138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 if (find_module(mod->name)) {
2140 err = -EEXIST;
2141 goto free_mod;
2142 }
2143
2144 mod->state = MODULE_STATE_COMING;
2145
2146 /* Allow arches to frob section contents and sizes. */
2147 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
2148 if (err < 0)
2149 goto free_mod;
2150
2151 if (pcpuindex) {
2152 /* We have a special allocation for this section. */
Tejun Heo259354d2010-03-10 18:56:10 +09002153 err = percpu_modalloc(mod, sechdrs[pcpuindex].sh_size,
2154 sechdrs[pcpuindex].sh_addralign);
2155 if (err)
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002156 goto free_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 }
2159
2160 /* Determine total sizes, and put offsets in sh_entsize. For now
2161 this is done generically; there doesn't appear to be any
2162 special cases for the architectures. */
2163 layout_sections(mod, hdr, sechdrs, secstrings);
Jan Beulich554bdfe2009-07-06 14:51:44 +01002164 symoffs = layout_symtab(mod, sechdrs, symindex, strindex, hdr,
2165 secstrings, &stroffs, strmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
2167 /* Do the allocs. */
Rusty Russell3a642e92008-07-22 19:24:28 -05002168 ptr = module_alloc_update_bounds(mod->core_size);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002169 /*
2170 * The pointer to this block is stored in the module structure
2171 * which is inside the block. Just mark it as not being a
2172 * leak.
2173 */
2174 kmemleak_not_leak(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 if (!ptr) {
2176 err = -ENOMEM;
2177 goto free_percpu;
2178 }
2179 memset(ptr, 0, mod->core_size);
2180 mod->module_core = ptr;
2181
Rusty Russell3a642e92008-07-22 19:24:28 -05002182 ptr = module_alloc_update_bounds(mod->init_size);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002183 /*
2184 * The pointer to this block is stored in the module structure
2185 * which is inside the block. This block doesn't need to be
2186 * scanned as it contains data and code that will be freed
2187 * after the module is initialized.
2188 */
2189 kmemleak_ignore(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 if (!ptr && mod->init_size) {
2191 err = -ENOMEM;
2192 goto free_core;
2193 }
2194 memset(ptr, 0, mod->init_size);
2195 mod->module_init = ptr;
2196
2197 /* Transfer each section which specifies SHF_ALLOC */
2198 DEBUGP("final section addresses:\n");
2199 for (i = 0; i < hdr->e_shnum; i++) {
2200 void *dest;
2201
2202 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2203 continue;
2204
2205 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2206 dest = mod->module_init
2207 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2208 else
2209 dest = mod->module_core + sechdrs[i].sh_entsize;
2210
2211 if (sechdrs[i].sh_type != SHT_NOBITS)
2212 memcpy(dest, (void *)sechdrs[i].sh_addr,
2213 sechdrs[i].sh_size);
2214 /* Update sh_addr to point to copy in image. */
2215 sechdrs[i].sh_addr = (unsigned long)dest;
2216 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2217 }
2218 /* Module has been moved. */
2219 mod = (void *)sechdrs[modindex].sh_addr;
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002220 kmemleak_load_module(mod, hdr, sechdrs, secstrings);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Christoph Lametere1783a22010-01-05 15:34:50 +09002222#if defined(CONFIG_MODULE_UNLOAD)
2223 mod->refptr = alloc_percpu(struct module_ref);
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002224 if (!mod->refptr) {
2225 err = -ENOMEM;
2226 goto free_init;
2227 }
2228#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 /* Now we've moved module, initialize linked lists, etc. */
2230 module_unload_init(mod);
2231
Kay Sievers97c146e2007-11-29 23:46:11 +01002232 /* add kobject, so we can reference it. */
Akinobu Mitad58ae672007-10-16 23:30:27 -07002233 err = mod_sysfs_init(mod);
2234 if (err)
Kay Sievers97c146e2007-11-29 23:46:11 +01002235 goto free_unload;
Kay Sievers270a6c42007-01-18 13:26:15 +01002236
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 /* Set up license info based on the info section */
2238 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2239
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002240 /*
2241 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2242 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2243 * using GPL-only symbols it needs.
2244 */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002245 if (strcmp(mod->name, "ndiswrapper") == 0)
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002246 add_taint(TAINT_PROPRIETARY_MODULE);
2247
2248 /* driverloader was caught wrongly pretending to be under GPL */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002249 if (strcmp(mod->name, "driverloader") == 0)
2250 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Dave Jones9841d61d2006-01-08 01:03:41 -08002251
Matt Domschc988d2b2005-06-23 22:05:15 -07002252 /* Set up MODINFO_ATTR fields */
2253 setup_modinfo(mod, sechdrs, infoindex);
Matt Domschc988d2b2005-06-23 22:05:15 -07002254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 /* Fix up syms, so that st_value is a pointer to location. */
2256 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2257 mod);
2258 if (err < 0)
2259 goto cleanup;
2260
Rusty Russell5e458cc2008-10-22 10:00:13 -05002261 /* Now we've got everything in the final locations, we can
2262 * find optional sections. */
Rusty Russelle180a6b2009-03-31 13:05:29 -06002263 mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
2264 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell5e458cc2008-10-22 10:00:13 -05002265 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2266 sizeof(*mod->syms), &mod->num_syms);
2267 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2268 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2269 sizeof(*mod->gpl_syms),
2270 &mod->num_gpl_syms);
2271 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2272 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2273 "__ksymtab_gpl_future",
2274 sizeof(*mod->gpl_future_syms),
2275 &mod->num_gpl_future_syms);
2276 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2277 "__kcrctab_gpl_future");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002279#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002280 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2281 "__ksymtab_unused",
2282 sizeof(*mod->unused_syms),
2283 &mod->num_unused_syms);
2284 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2285 "__kcrctab_unused");
2286 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2287 "__ksymtab_unused_gpl",
2288 sizeof(*mod->unused_gpl_syms),
2289 &mod->num_unused_gpl_syms);
2290 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2291 "__kcrctab_unused_gpl");
2292#endif
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002293#ifdef CONFIG_CONSTRUCTORS
2294 mod->ctors = section_objs(hdr, sechdrs, secstrings, ".ctors",
2295 sizeof(*mod->ctors), &mod->num_ctors);
2296#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002297
Rusty Russell5e458cc2008-10-22 10:00:13 -05002298#ifdef CONFIG_TRACEPOINTS
2299 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2300 "__tracepoints",
2301 sizeof(*mod->tracepoints),
2302 &mod->num_tracepoints);
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002303#endif
Steven Rostedt6d723732009-04-10 14:53:50 -04002304#ifdef CONFIG_EVENT_TRACING
2305 mod->trace_events = section_objs(hdr, sechdrs, secstrings,
2306 "_ftrace_events",
2307 sizeof(*mod->trace_events),
2308 &mod->num_trace_events);
Catalin Marinasa6f5aa12009-10-28 13:33:10 +00002309 /*
2310 * This section contains pointers to allocated objects in the trace
2311 * code and not scanning it leads to false positives.
2312 */
2313 kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2314 mod->num_trace_events, GFP_KERNEL);
Steven Rostedt6d723732009-04-10 14:53:50 -04002315#endif
Steven Rostedt93eb6772009-04-15 13:24:06 -04002316#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2317 /* sechdrs[0].sh_size is always zero */
2318 mod->ftrace_callsites = section_objs(hdr, sechdrs, secstrings,
2319 "__mcount_loc",
2320 sizeof(*mod->ftrace_callsites),
2321 &mod->num_ftrace_callsites);
2322#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323#ifdef CONFIG_MODVERSIONS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002324 if ((mod->num_syms && !mod->crcs)
2325 || (mod->num_gpl_syms && !mod->gpl_crcs)
2326 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002327#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002328 || (mod->num_unused_syms && !mod->unused_crcs)
2329 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002330#endif
2331 ) {
Rusty Russellc6e665c2009-03-31 13:05:33 -06002332 err = try_to_force_load(mod,
2333 "no versions for exported symbols");
Linus Torvalds826e4502008-05-04 17:04:16 -07002334 if (err)
2335 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 }
2337#endif
2338
2339 /* Now do relocations. */
2340 for (i = 1; i < hdr->e_shnum; i++) {
2341 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2342 unsigned int info = sechdrs[i].sh_info;
2343
2344 /* Not a valid relocation section? */
2345 if (info >= hdr->e_shnum)
2346 continue;
2347
2348 /* Don't bother with non-allocated sections */
2349 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2350 continue;
2351
2352 if (sechdrs[i].sh_type == SHT_REL)
2353 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2354 else if (sechdrs[i].sh_type == SHT_RELA)
2355 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2356 mod);
2357 if (err < 0)
2358 goto cleanup;
2359 }
2360
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002361 /* Find duplicate symbols */
2362 err = verify_export_symbols(mod);
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002363 if (err < 0)
2364 goto cleanup;
2365
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 /* Set up and sort exception table */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002367 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2368 sizeof(*mod->extable), &mod->num_exentries);
2369 sort_extable(mod->extable, mod->extable + mod->num_exentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
2371 /* Finally, copy percpu area over. */
Tejun Heo259354d2010-03-10 18:56:10 +09002372 percpu_modcopy(mod, (void *)sechdrs[pcpuindex].sh_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 sechdrs[pcpuindex].sh_size);
2374
Jan Beulich4a496222009-07-06 14:50:42 +01002375 add_kallsyms(mod, sechdrs, hdr->e_shnum, symindex, strindex,
Jan Beulich554bdfe2009-07-06 14:51:44 +01002376 symoffs, stroffs, secstrings, strmap);
2377 kfree(strmap);
2378 strmap = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002380 if (!mod->taints) {
Jason Barone9d376f2009-02-05 11:51:38 -05002381 struct _ddebug *debug;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002382 unsigned int num_debug;
2383
Rusty Russell5e458cc2008-10-22 10:00:13 -05002384 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2385 sizeof(*debug), &num_debug);
Jason Barone9d376f2009-02-05 11:51:38 -05002386 if (debug)
2387 dynamic_debug_setup(debug, num_debug);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002388 }
Steven Rostedt90d595f2008-08-14 15:45:09 -04002389
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 err = module_finalize(hdr, sechdrs, mod);
2391 if (err < 0)
2392 goto cleanup;
2393
Thomas Koeller378bac82005-09-06 15:17:11 -07002394 /* flush the icache in correct context */
2395 old_fs = get_fs();
2396 set_fs(KERNEL_DS);
2397
2398 /*
2399 * Flush the instruction cache, since we've played with text.
2400 * Do it before processing of module parameters, so the module
2401 * can provide parameter accessor functions of its own.
2402 */
2403 if (mod->module_init)
2404 flush_icache_range((unsigned long)mod->module_init,
2405 (unsigned long)mod->module_init
2406 + mod->init_size);
2407 flush_icache_range((unsigned long)mod->module_core,
2408 (unsigned long)mod->module_core + mod->core_size);
2409
2410 set_fs(old_fs);
2411
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 mod->args = args;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002413 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002414 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2415 mod->name);
2416
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002417 /* Now sew it into the lists so we can get lockdep and oops
Andi Kleend72b3752008-08-30 10:09:00 +02002418 * info during argument parsing. Noone should access us, since
2419 * strong_try_module_get() will fail.
2420 * lockdep/oops can run asynchronous, so use the RCU list insertion
2421 * function to insert in a way safe to concurrent readers.
2422 * The mutex protects against concurrent writers.
2423 */
2424 list_add_rcu(&mod->list, &modules);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002425
Rusty Russelle180a6b2009-03-31 13:05:29 -06002426 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002428 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
Rusty Russelle180a6b2009-03-31 13:05:29 -06002430 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
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 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Roland McGrath6d760132007-10-16 23:26:40 -07002434 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
2436 /* Get rid of temporary copy */
2437 vfree(hdr);
2438
Li Zefan7ead8b82009-08-17 16:56:28 +08002439 trace_module_load(mod);
2440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 /* Done! */
2442 return mod;
2443
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002444 unlink:
Rusty Russelle91defa2009-03-31 13:05:35 -06002445 /* Unlink carefully: kallsyms could be walking list. */
2446 list_del_rcu(&mod->list);
2447 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 module_arch_cleanup(mod);
2449 cleanup:
Rusty Russella263f772009-09-25 00:32:58 -06002450 free_modinfo(mod);
Kay Sievers97c146e2007-11-29 23:46:11 +01002451 kobject_del(&mod->mkobj.kobj);
2452 kobject_put(&mod->mkobj.kobj);
2453 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 module_unload_free(mod);
Christoph Lametere1783a22010-01-05 15:34:50 +09002455#if defined(CONFIG_MODULE_UNLOAD)
2456 free_percpu(mod->refptr);
Rusty Russellffa9f122009-09-25 00:32:59 -06002457 free_init:
Eric Dumazet720eba32009-02-03 13:31:36 +10302458#endif
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002459 module_free(mod, mod->module_init);
2460 free_core:
2461 module_free(mod, mod->module_core);
2462 /* mod will be freed with core. Don't access it beyond this line! */
2463 free_percpu:
Tejun Heo259354d2010-03-10 18:56:10 +09002464 percpu_modfree(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 free_mod:
2466 kfree(args);
Jan Beulich554bdfe2009-07-06 14:51:44 +01002467 kfree(strmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 free_hdr:
2469 vfree(hdr);
Jayachandran C6fe2e702006-01-06 00:19:54 -08002470 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
2472 truncated:
2473 printk(KERN_ERR "Module len %lu truncated\n", len);
2474 err = -ENOEXEC;
2475 goto free_hdr;
2476}
2477
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002478/* Call module constructors. */
2479static void do_mod_ctors(struct module *mod)
2480{
2481#ifdef CONFIG_CONSTRUCTORS
2482 unsigned long i;
2483
2484 for (i = 0; i < mod->num_ctors; i++)
2485 mod->ctors[i]();
2486#endif
2487}
2488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489/* This is where the real work happens */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002490SYSCALL_DEFINE3(init_module, void __user *, umod,
2491 unsigned long, len, const char __user *, uargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492{
2493 struct module *mod;
2494 int ret = 0;
2495
2496 /* Must have permission */
Kees Cook3d433212009-04-02 15:49:29 -07002497 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 return -EPERM;
2499
2500 /* Only one module load at a time, please */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002501 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 return -EINTR;
2503
2504 /* Do all the hard work */
2505 mod = load_module(umod, len, uargs);
2506 if (IS_ERR(mod)) {
Ashutosh Naik6389a382006-03-23 03:00:46 -08002507 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 return PTR_ERR(mod);
2509 }
2510
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 /* Drop lock so they can recurse */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002512 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Alan Sterne041c682006-03-27 01:16:30 -08002514 blocking_notifier_call_chain(&module_notify_list,
2515 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002517 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 /* Start the module */
2519 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002520 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 if (ret < 0) {
2522 /* Init routine failed: abort. Try to protect us from
2523 buggy refcounters. */
2524 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002525 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002526 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002527 blocking_notifier_call_chain(&module_notify_list,
2528 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002529 mutex_lock(&module_mutex);
2530 free_module(mod);
2531 mutex_unlock(&module_mutex);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002532 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 return ret;
2534 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002535 if (ret > 0) {
Joe Perchesad361c92009-07-06 13:05:40 -07002536 printk(KERN_WARNING
2537"%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
2538"%s: loading module anyway...\n",
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002539 __func__, mod->name, ret,
2540 __func__);
2541 dump_stack();
2542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
Rusty Russell6c5db222008-03-10 11:43:52 -07002544 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002546 wake_up(&module_wq);
Masami Hiramatsu0deddf432009-01-06 14:41:54 -08002547 blocking_notifier_call_chain(&module_notify_list,
2548 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07002549
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07002550 /* We need to finish all async code before the module init sequence is done */
2551 async_synchronize_full();
2552
Rusty Russell6c5db222008-03-10 11:43:52 -07002553 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 /* Drop initial reference. */
2555 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06002556 trim_init_extable(mod);
Jan Beulich4a496222009-07-06 14:50:42 +01002557#ifdef CONFIG_KALLSYMS
2558 mod->num_symtab = mod->core_num_syms;
2559 mod->symtab = mod->core_symtab;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002560 mod->strtab = mod->core_strtab;
Jan Beulich4a496222009-07-06 14:50:42 +01002561#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 module_free(mod, mod->module_init);
2563 mod->module_init = NULL;
2564 mod->init_size = 0;
2565 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002566 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
2568 return 0;
2569}
2570
2571static inline int within(unsigned long addr, void *start, unsigned long size)
2572{
2573 return ((void *)addr >= start && (void *)addr < start + size);
2574}
2575
2576#ifdef CONFIG_KALLSYMS
2577/*
2578 * This ignores the intensely annoying "mapping symbols" found
2579 * in ARM ELF files: $a, $t and $d.
2580 */
2581static inline int is_arm_mapping_symbol(const char *str)
2582{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002583 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 && (str[2] == '\0' || str[2] == '.');
2585}
2586
2587static const char *get_ksymbol(struct module *mod,
2588 unsigned long addr,
2589 unsigned long *size,
2590 unsigned long *offset)
2591{
2592 unsigned int i, best = 0;
2593 unsigned long nextval;
2594
2595 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002596 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002598 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2600
2601 /* Scan for closest preceeding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002602 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 for (i = 1; i < mod->num_symtab; i++) {
2604 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2605 continue;
2606
2607 /* We ignore unnamed symbols: they're uninformative
2608 * and inserted at a whim. */
2609 if (mod->symtab[i].st_value <= addr
2610 && mod->symtab[i].st_value > mod->symtab[best].st_value
2611 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2612 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2613 best = i;
2614 if (mod->symtab[i].st_value > addr
2615 && mod->symtab[i].st_value < nextval
2616 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2617 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2618 nextval = mod->symtab[i].st_value;
2619 }
2620
2621 if (!best)
2622 return NULL;
2623
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002624 if (size)
2625 *size = nextval - mod->symtab[best].st_value;
2626 if (offset)
2627 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 return mod->strtab + mod->symtab[best].st_name;
2629}
2630
Rusty Russell6dd06c92008-01-29 17:13:22 -05002631/* For kallsyms to ask for address resolution. NULL means not found. Careful
2632 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002633const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05002634 unsigned long *size,
2635 unsigned long *offset,
2636 char **modname,
2637 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638{
2639 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002640 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Rusty Russellcb2a5202008-01-14 00:55:03 -08002642 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002643 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002644 if (within_module_init(addr, mod) ||
2645 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002646 if (modname)
2647 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002648 ret = get_ksymbol(mod, addr, size, offset);
2649 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 }
2651 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05002652 /* Make a copy in here where it's safe */
2653 if (ret) {
2654 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2655 ret = namebuf;
2656 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002657 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002658 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659}
2660
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002661int lookup_module_symbol_name(unsigned long addr, char *symname)
2662{
2663 struct module *mod;
2664
Rusty Russellcb2a5202008-01-14 00:55:03 -08002665 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002666 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002667 if (within_module_init(addr, mod) ||
2668 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002669 const char *sym;
2670
2671 sym = get_ksymbol(mod, addr, NULL, NULL);
2672 if (!sym)
2673 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07002674 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002675 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002676 return 0;
2677 }
2678 }
2679out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002680 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002681 return -ERANGE;
2682}
2683
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002684int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2685 unsigned long *offset, char *modname, char *name)
2686{
2687 struct module *mod;
2688
Rusty Russellcb2a5202008-01-14 00:55:03 -08002689 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002690 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002691 if (within_module_init(addr, mod) ||
2692 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002693 const char *sym;
2694
2695 sym = get_ksymbol(mod, addr, size, offset);
2696 if (!sym)
2697 goto out;
2698 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07002699 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002700 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07002701 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002702 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002703 return 0;
2704 }
2705 }
2706out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002707 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002708 return -ERANGE;
2709}
2710
Alexey Dobriyanea078902007-05-08 00:28:39 -07002711int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2712 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713{
2714 struct module *mod;
2715
Rusty Russellcb2a5202008-01-14 00:55:03 -08002716 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002717 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 if (symnum < mod->num_symtab) {
2719 *value = mod->symtab[symnum].st_value;
2720 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002721 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07002722 KSYM_NAME_LEN);
2723 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06002724 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002725 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002726 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 }
2728 symnum -= mod->num_symtab;
2729 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002730 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002731 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732}
2733
2734static unsigned long mod_find_symname(struct module *mod, const char *name)
2735{
2736 unsigned int i;
2737
2738 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002739 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2740 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 return mod->symtab[i].st_value;
2742 return 0;
2743}
2744
2745/* Look for this name: can be of form module:name. */
2746unsigned long module_kallsyms_lookup_name(const char *name)
2747{
2748 struct module *mod;
2749 char *colon;
2750 unsigned long ret = 0;
2751
2752 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08002753 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 if ((colon = strchr(name, ':')) != NULL) {
2755 *colon = '\0';
2756 if ((mod = find_module(name)) != NULL)
2757 ret = mod_find_symname(mod, colon+1);
2758 *colon = ':';
2759 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02002760 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 if ((ret = mod_find_symname(mod, name)) != 0)
2762 break;
2763 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002764 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 return ret;
2766}
Anders Kaseorg75a66612008-12-05 19:03:58 -05002767
2768int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
2769 struct module *, unsigned long),
2770 void *data)
2771{
2772 struct module *mod;
2773 unsigned int i;
2774 int ret;
2775
2776 list_for_each_entry(mod, &modules, list) {
2777 for (i = 0; i < mod->num_symtab; i++) {
2778 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
2779 mod, mod->symtab[i].st_value);
2780 if (ret != 0)
2781 return ret;
2782 }
2783 }
2784 return 0;
2785}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786#endif /* CONFIG_KALLSYMS */
2787
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002788static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002789{
2790 int bx = 0;
2791
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002792 if (mod->taints ||
2793 mod->state == MODULE_STATE_GOING ||
2794 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002795 buf[bx++] = '(';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002796 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002797 buf[bx++] = 'P';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002798 if (mod->taints & (1 << TAINT_FORCED_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002799 buf[bx++] = 'F';
Linus Torvalds26e9a392008-10-17 09:50:12 -07002800 if (mod->taints & (1 << TAINT_CRAP))
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002801 buf[bx++] = 'C';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002802 /*
2803 * TAINT_FORCED_RMMOD: could be added.
2804 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2805 * apply to modules.
2806 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002807
2808 /* Show a - for module-is-being-unloaded */
2809 if (mod->state == MODULE_STATE_GOING)
2810 buf[bx++] = '-';
2811 /* Show a + for module-is-being-loaded */
2812 if (mod->state == MODULE_STATE_COMING)
2813 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002814 buf[bx++] = ')';
2815 }
2816 buf[bx] = '\0';
2817
2818 return buf;
2819}
2820
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002821#ifdef CONFIG_PROC_FS
2822/* Called by the /proc file system to return a list of modules. */
2823static void *m_start(struct seq_file *m, loff_t *pos)
2824{
2825 mutex_lock(&module_mutex);
2826 return seq_list_start(&modules, *pos);
2827}
2828
2829static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2830{
2831 return seq_list_next(p, &modules, pos);
2832}
2833
2834static void m_stop(struct seq_file *m, void *p)
2835{
2836 mutex_unlock(&module_mutex);
2837}
2838
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839static int m_show(struct seq_file *m, void *p)
2840{
2841 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002842 char buf[8];
2843
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05002844 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 mod->name, mod->init_size + mod->core_size);
2846 print_unload_info(m, mod);
2847
2848 /* Informative for users. */
2849 seq_printf(m, " %s",
2850 mod->state == MODULE_STATE_GOING ? "Unloading":
2851 mod->state == MODULE_STATE_COMING ? "Loading":
2852 "Live");
2853 /* Used by oprofile and other similar tools. */
2854 seq_printf(m, " 0x%p", mod->module_core);
2855
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002856 /* Taints info */
2857 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002858 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002859
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 seq_printf(m, "\n");
2861 return 0;
2862}
2863
2864/* Format: modulename size refcount deps address
2865
2866 Where refcount is a number or -, and deps is a comma-separated list
2867 of depends or -.
2868*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002869static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 .start = m_start,
2871 .next = m_next,
2872 .stop = m_stop,
2873 .show = m_show
2874};
2875
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002876static int modules_open(struct inode *inode, struct file *file)
2877{
2878 return seq_open(file, &modules_op);
2879}
2880
2881static const struct file_operations proc_modules_operations = {
2882 .open = modules_open,
2883 .read = seq_read,
2884 .llseek = seq_lseek,
2885 .release = seq_release,
2886};
2887
2888static int __init proc_modules_init(void)
2889{
2890 proc_create("modules", 0, NULL, &proc_modules_operations);
2891 return 0;
2892}
2893module_init(proc_modules_init);
2894#endif
2895
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896/* Given an address, look for it in the module exception tables. */
2897const struct exception_table_entry *search_module_extables(unsigned long addr)
2898{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 const struct exception_table_entry *e = NULL;
2900 struct module *mod;
2901
Rusty Russell24da1cb2007-07-15 23:41:46 -07002902 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002903 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 if (mod->num_exentries == 0)
2905 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 e = search_extable(mod->extable,
2908 mod->extable + mod->num_exentries - 1,
2909 addr);
2910 if (e)
2911 break;
2912 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07002913 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
2915 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07002916 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 return e;
2918}
2919
Ingo Molnar4d435f92006-07-03 00:24:24 -07002920/*
Rusty Russelle6104992009-03-31 13:05:31 -06002921 * is_module_address - is this address inside a module?
2922 * @addr: the address to check.
2923 *
2924 * See is_module_text_address() if you simply want to see if the address
2925 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07002926 */
Rusty Russelle6104992009-03-31 13:05:31 -06002927bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07002928{
Rusty Russelle6104992009-03-31 13:05:31 -06002929 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07002930
Rusty Russell24da1cb2007-07-15 23:41:46 -07002931 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06002932 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07002933 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002934
Rusty Russelle6104992009-03-31 13:05:31 -06002935 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07002936}
2937
Rusty Russelle6104992009-03-31 13:05:31 -06002938/*
2939 * __module_address - get the module which contains an address.
2940 * @addr: the address.
2941 *
2942 * Must be called with preempt disabled or module mutex held so that
2943 * module doesn't get freed during this.
2944 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07002945struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946{
2947 struct module *mod;
2948
Rusty Russell3a642e92008-07-22 19:24:28 -05002949 if (addr < module_addr_min || addr > module_addr_max)
2950 return NULL;
2951
Andi Kleend72b3752008-08-30 10:09:00 +02002952 list_for_each_entry_rcu(mod, &modules, list)
Rusty Russelle6104992009-03-31 13:05:31 -06002953 if (within_module_core(addr, mod)
2954 || within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 return mod;
2956 return NULL;
2957}
Tim Abbottc6b37802008-12-05 19:03:59 -05002958EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
Rusty Russelle6104992009-03-31 13:05:31 -06002960/*
2961 * is_module_text_address - is this address inside module code?
2962 * @addr: the address to check.
2963 *
2964 * See is_module_address() if you simply want to see if the address is
2965 * anywhere in a module. See kernel_text_address() for testing if an
2966 * address corresponds to kernel or module code.
2967 */
2968bool is_module_text_address(unsigned long addr)
2969{
2970 bool ret;
2971
2972 preempt_disable();
2973 ret = __module_text_address(addr) != NULL;
2974 preempt_enable();
2975
2976 return ret;
2977}
2978
2979/*
2980 * __module_text_address - get the module whose code contains an address.
2981 * @addr: the address.
2982 *
2983 * Must be called with preempt disabled or module mutex held so that
2984 * module doesn't get freed during this.
2985 */
2986struct module *__module_text_address(unsigned long addr)
2987{
2988 struct module *mod = __module_address(addr);
2989 if (mod) {
2990 /* Make sure it's within the text section. */
2991 if (!within(addr, mod->module_init, mod->init_text_size)
2992 && !within(addr, mod->module_core, mod->core_text_size))
2993 mod = NULL;
2994 }
2995 return mod;
2996}
Tim Abbottc6b37802008-12-05 19:03:59 -05002997EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06002998
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999/* Don't grab lock, we're oopsing. */
3000void print_modules(void)
3001{
3002 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07003003 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Linus Torvaldsb2311252009-06-16 11:07:14 -07003005 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02003006 /* Most callers should already have preempt disabled, but make sure */
3007 preempt_disable();
3008 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003009 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02003010 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01003011 if (last_unloaded_module[0])
3012 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 printk("\n");
3014}
3015
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06003017/* Generate the signature for all relevant module structures here.
3018 * If these change, we don't want to try to parse the module. */
3019void module_layout(struct module *mod,
3020 struct modversion_info *ver,
3021 struct kernel_param *kp,
3022 struct kernel_symbol *ks,
Rusty Russell8c8ef422009-03-31 13:05:34 -06003023 struct tracepoint *tp)
3024{
3025}
3026EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07003028
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04003029#ifdef CONFIG_TRACEPOINTS
3030void module_update_tracepoints(void)
3031{
3032 struct module *mod;
3033
3034 mutex_lock(&module_mutex);
3035 list_for_each_entry(mod, &modules, list)
3036 if (!mod->taints)
3037 tracepoint_update_probe_range(mod->tracepoints,
3038 mod->tracepoints + mod->num_tracepoints);
3039 mutex_unlock(&module_mutex);
3040}
3041
3042/*
3043 * Returns 0 if current not found.
3044 * Returns 1 if current found.
3045 */
3046int module_get_iter_tracepoints(struct tracepoint_iter *iter)
3047{
3048 struct module *iter_mod;
3049 int found = 0;
3050
3051 mutex_lock(&module_mutex);
3052 list_for_each_entry(iter_mod, &modules, list) {
3053 if (!iter_mod->taints) {
3054 /*
3055 * Sorted module list
3056 */
3057 if (iter_mod < iter->module)
3058 continue;
3059 else if (iter_mod > iter->module)
3060 iter->tracepoint = NULL;
3061 found = tracepoint_get_iter_range(&iter->tracepoint,
3062 iter_mod->tracepoints,
3063 iter_mod->tracepoints
3064 + iter_mod->num_tracepoints);
3065 if (found) {
3066 iter->module = iter_mod;
3067 break;
3068 }
3069 }
3070 }
3071 mutex_unlock(&module_mutex);
3072 return found;
3073}
3074#endif