blob: 970d773aec625bf42dfc90909a176c6b85c59484 [file] [log] [blame]
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 Copyright (C) 2002 Richard Henderson
3 Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
20#include <linux/moduleloader.h>
Steven Rostedt6d723732009-04-10 14:53:50 -040021#include <linux/ftrace_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
Alexey Dobriyanae84e322007-05-08 00:28:38 -070023#include <linux/kallsyms.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040024#include <linux/fs.h>
Roland McGrath6d760132007-10-16 23:26:40 -070025#include <linux/sysfs.h>
Randy Dunlap9f158332005-09-13 01:25:16 -070026#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/slab.h>
28#include <linux/vmalloc.h>
29#include <linux/elf.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040030#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/seq_file.h>
32#include <linux/syscalls.h>
33#include <linux/fcntl.h>
34#include <linux/rcupdate.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080035#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/cpu.h>
37#include <linux/moduleparam.h>
38#include <linux/errno.h>
39#include <linux/err.h>
40#include <linux/vermagic.h>
41#include <linux/notifier.h>
Al Virof6a57032006-10-18 01:47:25 -040042#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/stop_machine.h>
44#include <linux/device.h>
Matt Domschc988d2b2005-06-23 22:05:15 -070045#include <linux/string.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080046#include <linux/mutex.h>
Andi Kleend72b3752008-08-30 10:09:00 +020047#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/cacheflush.h>
Bernd Schmidteb8cdec2009-09-21 17:03:57 -070050#include <asm/mmu_context.h>
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020051#include <linux/license.h>
Christoph Lameter6d762392008-02-08 04:18:42 -080052#include <asm/sections.h>
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040053#include <linux/tracepoint.h>
Steven Rostedt90d595f2008-08-14 15:45:09 -040054#include <linux/ftrace.h>
Arjan van de Ven22a9d642009-01-07 08:45:46 -080055#include <linux/async.h>
Tejun Heofbf59bc2009-02-20 16:29:08 +090056#include <linux/percpu.h>
Catalin Marinas4f2294b2009-06-11 13:23:20 +010057#include <linux/kmemleak.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Li Zefan7ead8b82009-08-17 16:56:28 +080059#define CREATE_TRACE_POINTS
60#include <trace/events/module.h>
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#if 0
63#define DEBUGP printk
64#else
65#define DEBUGP(fmt , a...)
66#endif
67
68#ifndef ARCH_SHF_SMALL
69#define ARCH_SHF_SMALL 0
70#endif
71
72/* If this is set, the section belongs in the init part of the module */
73#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
74
Rusty Russell24da1cb2007-07-15 23:41:46 -070075/* List of modules, protected by module_mutex or preempt_disable
Andi Kleend72b3752008-08-30 10:09:00 +020076 * (delete uses stop_machine/add uses RCU list operations). */
Tim Abbottc6b37802008-12-05 19:03:59 -050077DEFINE_MUTEX(module_mutex);
78EXPORT_SYMBOL_GPL(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static LIST_HEAD(modules);
80
Stephen Rothwell19e45292009-04-14 17:27:18 +100081/* Block module loading/unloading? */
82int modules_disabled = 0;
83
Rusty Russellc9a3ba52008-01-29 17:13:18 -050084/* Waiting for a module to finish initializing? */
85static DECLARE_WAIT_QUEUE_HEAD(module_wq);
86
Alan Sterne041c682006-03-27 01:16:30 -080087static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Rusty Russelle6104992009-03-31 13:05:31 -060089/* Bounds of module allocation, for speeding __module_address */
Rusty Russell3a642e92008-07-22 19:24:28 -050090static unsigned long module_addr_min = -1UL, module_addr_max = 0;
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092int register_module_notifier(struct notifier_block * nb)
93{
Alan Sterne041c682006-03-27 01:16:30 -080094 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96EXPORT_SYMBOL(register_module_notifier);
97
98int unregister_module_notifier(struct notifier_block * nb)
99{
Alan Sterne041c682006-03-27 01:16:30 -0800100 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102EXPORT_SYMBOL(unregister_module_notifier);
103
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -0800104/* We require a truly strong try_module_get(): 0 means failure due to
105 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static inline int strong_try_module_get(struct module *mod)
107{
108 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500109 return -EBUSY;
110 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500112 else
113 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700116static inline void add_taint_module(struct module *mod, unsigned flag)
117{
118 add_taint(flag);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700119 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700120}
121
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200122/*
123 * A thread that wants to hold a reference to a module only while it
124 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 */
126void __module_put_and_exit(struct module *mod, long code)
127{
128 module_put(mod);
129 do_exit(code);
130}
131EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133/* Find a module section: 0 means not found. */
134static unsigned int find_sec(Elf_Ehdr *hdr,
135 Elf_Shdr *sechdrs,
136 const char *secstrings,
137 const char *name)
138{
139 unsigned int i;
140
141 for (i = 1; i < hdr->e_shnum; i++)
142 /* Alloc bit cleared means "ignore it." */
143 if ((sechdrs[i].sh_flags & SHF_ALLOC)
144 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
145 return i;
146 return 0;
147}
148
Rusty Russell5e458cc2008-10-22 10:00:13 -0500149/* Find a module section, or NULL. */
150static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
151 const char *secstrings, const char *name)
152{
153 /* Section 0 has sh_addr 0. */
154 return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
155}
156
157/* Find a module section, or NULL. Fill in number of "objects" in section. */
158static void *section_objs(Elf_Ehdr *hdr,
159 Elf_Shdr *sechdrs,
160 const char *secstrings,
161 const char *name,
162 size_t object_size,
163 unsigned int *num)
164{
165 unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
166
167 /* Section 0 has sh_addr 0 and sh_size 0. */
168 *num = sechdrs[sec].sh_size / object_size;
169 return (void *)sechdrs[sec].sh_addr;
170}
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/* Provided by the linker */
173extern const struct kernel_symbol __start___ksymtab[];
174extern const struct kernel_symbol __stop___ksymtab[];
175extern const struct kernel_symbol __start___ksymtab_gpl[];
176extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800177extern const struct kernel_symbol __start___ksymtab_gpl_future[];
178extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700179extern const struct kernel_symbol __start___ksymtab_gpl_future[];
180extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181extern const unsigned long __start___kcrctab[];
182extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800183extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500184#ifdef CONFIG_UNUSED_SYMBOLS
185extern const struct kernel_symbol __start___ksymtab_unused[];
186extern const struct kernel_symbol __stop___ksymtab_unused[];
187extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
188extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700189extern const unsigned long __start___kcrctab_unused[];
190extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500191#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193#ifndef CONFIG_MODVERSIONS
194#define symversion(base, idx) NULL
195#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800196#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197#endif
198
Rusty Russelldafd0942008-07-22 19:24:25 -0500199static bool each_symbol_in_section(const struct symsearch *arr,
200 unsigned int arrsize,
201 struct module *owner,
202 bool (*fn)(const struct symsearch *syms,
203 struct module *owner,
204 unsigned int symnum, void *data),
205 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100206{
Rusty Russelldafd0942008-07-22 19:24:25 -0500207 unsigned int i, j;
208
209 for (j = 0; j < arrsize; j++) {
210 for (i = 0; i < arr[j].stop - arr[j].start; i++)
211 if (fn(&arr[j], owner, i, data))
212 return true;
213 }
214
215 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100216}
217
Rusty Russelldafd0942008-07-22 19:24:25 -0500218/* Returns true as soon as fn returns true, otherwise false. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500219bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
220 unsigned int symnum, void *data), void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700221{
Rusty Russelldafd0942008-07-22 19:24:25 -0500222 struct module *mod;
223 const struct symsearch arr[] = {
224 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
225 NOT_GPL_ONLY, false },
226 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
227 __start___kcrctab_gpl,
228 GPL_ONLY, false },
229 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
230 __start___kcrctab_gpl_future,
231 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500232#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500233 { __start___ksymtab_unused, __stop___ksymtab_unused,
234 __start___kcrctab_unused,
235 NOT_GPL_ONLY, true },
236 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
237 __start___kcrctab_unused_gpl,
238 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500239#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500240 };
241
242 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
243 return true;
244
Andi Kleend72b3752008-08-30 10:09:00 +0200245 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500246 struct symsearch arr[] = {
247 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
248 NOT_GPL_ONLY, false },
249 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
250 mod->gpl_crcs,
251 GPL_ONLY, false },
252 { mod->gpl_future_syms,
253 mod->gpl_future_syms + mod->num_gpl_future_syms,
254 mod->gpl_future_crcs,
255 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500256#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500257 { mod->unused_syms,
258 mod->unused_syms + mod->num_unused_syms,
259 mod->unused_crcs,
260 NOT_GPL_ONLY, true },
261 { mod->unused_gpl_syms,
262 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
263 mod->unused_gpl_crcs,
264 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500265#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500266 };
267
268 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
269 return true;
270 }
271 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700272}
Tim Abbottc6b37802008-12-05 19:03:59 -0500273EXPORT_SYMBOL_GPL(each_symbol);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700274
Rusty Russelldafd0942008-07-22 19:24:25 -0500275struct find_symbol_arg {
276 /* Input */
277 const char *name;
278 bool gplok;
279 bool warn;
280
281 /* Output */
282 struct module *owner;
283 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500284 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500285};
286
287static bool find_symbol_in_section(const struct symsearch *syms,
288 struct module *owner,
289 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500290{
Rusty Russelldafd0942008-07-22 19:24:25 -0500291 struct find_symbol_arg *fsa = data;
292
293 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
294 return false;
295
296 if (!fsa->gplok) {
297 if (syms->licence == GPL_ONLY)
298 return false;
299 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
300 printk(KERN_WARNING "Symbol %s is being used "
301 "by a non-GPL module, which will not "
302 "be allowed in the future\n", fsa->name);
303 printk(KERN_WARNING "Please see the file "
304 "Documentation/feature-removal-schedule.txt "
305 "in the kernel source tree for more details.\n");
306 }
307 }
308
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500309#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500310 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500311 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500312 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500313 printk(KERN_WARNING
314 "This symbol will go away in the future.\n");
315 printk(KERN_WARNING
316 "Please evalute if this is the right api to use and if "
317 "it really is, submit a report the linux kernel "
318 "mailinglist together with submitting your code for "
319 "inclusion.\n");
320 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500321#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500322
323 fsa->owner = owner;
324 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500325 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500326 return true;
327}
328
Tim Abbott414fd312008-12-05 19:03:56 -0500329/* Find a symbol and return it, along with, (optional) crc and
330 * (optional) module which owns it */
Tim Abbottc6b37802008-12-05 19:03:59 -0500331const struct kernel_symbol *find_symbol(const char *name,
332 struct module **owner,
333 const unsigned long **crc,
334 bool gplok,
335 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Rusty Russelldafd0942008-07-22 19:24:25 -0500337 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Rusty Russelldafd0942008-07-22 19:24:25 -0500339 fsa.name = name;
340 fsa.gplok = gplok;
341 fsa.warn = warn;
342
343 if (each_symbol(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500344 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500345 *owner = fsa.owner;
346 if (crc)
347 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500348 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 DEBUGP("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500352 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
Tim Abbottc6b37802008-12-05 19:03:59 -0500354EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356/* Search for module by name: must hold module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500357struct module *find_module(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 struct module *mod;
360
361 list_for_each_entry(mod, &modules, list) {
362 if (strcmp(mod->name, name) == 0)
363 return mod;
364 }
365 return NULL;
366}
Tim Abbottc6b37802008-12-05 19:03:59 -0500367EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900370
Tejun Heo259354d2010-03-10 18:56:10 +0900371static inline void __percpu *mod_percpu(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900372{
Tejun Heo259354d2010-03-10 18:56:10 +0900373 return mod->percpu;
374}
Tejun Heofbf59bc2009-02-20 16:29:08 +0900375
Tejun Heo259354d2010-03-10 18:56:10 +0900376static int percpu_modalloc(struct module *mod,
377 unsigned long size, unsigned long align)
378{
Tejun Heofbf59bc2009-02-20 16:29:08 +0900379 if (align > PAGE_SIZE) {
380 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
Tejun Heo259354d2010-03-10 18:56:10 +0900381 mod->name, align, PAGE_SIZE);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900382 align = PAGE_SIZE;
383 }
384
Tejun Heo259354d2010-03-10 18:56:10 +0900385 mod->percpu = __alloc_reserved_percpu(size, align);
386 if (!mod->percpu) {
Tejun Heofbf59bc2009-02-20 16:29:08 +0900387 printk(KERN_WARNING
388 "Could not allocate %lu bytes percpu data\n", size);
Tejun Heo259354d2010-03-10 18:56:10 +0900389 return -ENOMEM;
390 }
391 mod->percpu_size = size;
392 return 0;
Tejun Heofbf59bc2009-02-20 16:29:08 +0900393}
394
Tejun Heo259354d2010-03-10 18:56:10 +0900395static void percpu_modfree(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900396{
Tejun Heo259354d2010-03-10 18:56:10 +0900397 free_percpu(mod->percpu);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900398}
399
Tejun Heo6b588c12009-02-20 16:29:07 +0900400static unsigned int find_pcpusec(Elf_Ehdr *hdr,
401 Elf_Shdr *sechdrs,
402 const char *secstrings)
403{
404 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
405}
406
Tejun Heo259354d2010-03-10 18:56:10 +0900407static void percpu_modcopy(struct module *mod,
408 const void *from, unsigned long size)
Tejun Heo6b588c12009-02-20 16:29:07 +0900409{
410 int cpu;
411
412 for_each_possible_cpu(cpu)
Tejun Heo259354d2010-03-10 18:56:10 +0900413 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
Tejun Heo6b588c12009-02-20 16:29:07 +0900414}
415
Tejun Heo10fad5e2010-03-10 18:57:54 +0900416/**
417 * is_module_percpu_address - test whether address is from module static percpu
418 * @addr: address to test
419 *
420 * Test whether @addr belongs to module static percpu area.
421 *
422 * RETURNS:
423 * %true if @addr is from module static percpu area
424 */
425bool is_module_percpu_address(unsigned long addr)
426{
427 struct module *mod;
428 unsigned int cpu;
429
430 preempt_disable();
431
432 list_for_each_entry_rcu(mod, &modules, list) {
433 if (!mod->percpu_size)
434 continue;
435 for_each_possible_cpu(cpu) {
436 void *start = per_cpu_ptr(mod->percpu, cpu);
437
438 if ((void *)addr >= start &&
439 (void *)addr < start + mod->percpu_size) {
440 preempt_enable();
441 return true;
442 }
443 }
444 }
445
446 preempt_enable();
447 return false;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700448}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900451
Tejun Heo259354d2010-03-10 18:56:10 +0900452static inline void __percpu *mod_percpu(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 return NULL;
455}
Tejun Heo259354d2010-03-10 18:56:10 +0900456static inline int percpu_modalloc(struct module *mod,
457 unsigned long size, unsigned long align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Tejun Heo259354d2010-03-10 18:56:10 +0900459 return -ENOMEM;
460}
461static inline void percpu_modfree(struct module *mod)
462{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
465 Elf_Shdr *sechdrs,
466 const char *secstrings)
467{
468 return 0;
469}
Tejun Heo259354d2010-03-10 18:56:10 +0900470static inline void percpu_modcopy(struct module *mod,
471 const void *from, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 /* pcpusec should be 0, and size of that section should be 0. */
474 BUG_ON(size != 0);
475}
Tejun Heo10fad5e2010-03-10 18:57:54 +0900476bool is_module_percpu_address(unsigned long addr)
477{
478 return false;
479}
Tejun Heo6b588c12009-02-20 16:29:07 +0900480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481#endif /* CONFIG_SMP */
482
Matt Domschc988d2b2005-06-23 22:05:15 -0700483#define MODINFO_ATTR(field) \
484static void setup_modinfo_##field(struct module *mod, const char *s) \
485{ \
486 mod->field = kstrdup(s, GFP_KERNEL); \
487} \
488static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
489 struct module *mod, char *buffer) \
490{ \
491 return sprintf(buffer, "%s\n", mod->field); \
492} \
493static int modinfo_##field##_exists(struct module *mod) \
494{ \
495 return mod->field != NULL; \
496} \
497static void free_modinfo_##field(struct module *mod) \
498{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700499 kfree(mod->field); \
500 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700501} \
502static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900503 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700504 .show = show_modinfo_##field, \
505 .setup = setup_modinfo_##field, \
506 .test = modinfo_##field##_exists, \
507 .free = free_modinfo_##field, \
508};
509
510MODINFO_ATTR(version);
511MODINFO_ATTR(srcversion);
512
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100513static char last_unloaded_module[MODULE_NAME_LEN+1];
514
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800515#ifdef CONFIG_MODULE_UNLOAD
Steven Rostedteb0c5372010-03-29 14:25:18 -0400516
517EXPORT_TRACEPOINT_SYMBOL(module_get);
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519/* Init the unload section of the module. */
520static void module_unload_init(struct module *mod)
521{
Eric Dumazet720eba32009-02-03 13:31:36 +1030522 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 INIT_LIST_HEAD(&mod->modules_which_use_me);
Nick Piggin5fbfb182010-04-01 19:09:40 +1100525 for_each_possible_cpu(cpu) {
526 per_cpu_ptr(mod->refptr, cpu)->incs = 0;
527 per_cpu_ptr(mod->refptr, cpu)->decs = 0;
528 }
Christoph Lametere1783a22010-01-05 15:34:50 +0900529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 /* Hold reference count during initialization. */
Nick Piggin5fbfb182010-04-01 19:09:40 +1100531 __this_cpu_write(mod->refptr->incs, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /* Backwards compatibility macros put refcount during init. */
533 mod->waiter = current;
534}
535
536/* modules using other modules */
537struct module_use
538{
539 struct list_head list;
540 struct module *module_which_uses;
541};
542
543/* Does a already use b? */
544static int already_uses(struct module *a, struct module *b)
545{
546 struct module_use *use;
547
548 list_for_each_entry(use, &b->modules_which_use_me, list) {
549 if (use->module_which_uses == a) {
550 DEBUGP("%s uses %s!\n", a->name, b->name);
551 return 1;
552 }
553 }
554 DEBUGP("%s does not use %s!\n", a->name, b->name);
555 return 0;
556}
557
558/* Module a uses b */
Tim Abbottc6b37802008-12-05 19:03:59 -0500559int use_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 struct module_use *use;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500562 int no_warn, err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100563
Rusty Russell480b02d2010-05-19 17:33:39 -0600564 if (b == NULL || already_uses(a, b))
565 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500567 /* If we're interrupted or time out, we fail. */
Rusty Russell480b02d2010-05-19 17:33:39 -0600568 err = strong_try_module_get(b);
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500569 if (err)
Rusty Russell480b02d2010-05-19 17:33:39 -0600570 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 DEBUGP("Allocating new usage for %s.\n", a->name);
573 use = kmalloc(sizeof(*use), GFP_ATOMIC);
574 if (!use) {
575 printk("%s: out of memory loading\n", a->name);
576 module_put(b);
Rusty Russell480b02d2010-05-19 17:33:39 -0600577 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579
580 use->module_which_uses = a;
581 list_add(&use->list, &b->modules_which_use_me);
Kay Sievers270a6c42007-01-18 13:26:15 +0100582 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
Rusty Russell480b02d2010-05-19 17:33:39 -0600583 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
Tim Abbottc6b37802008-12-05 19:03:59 -0500585EXPORT_SYMBOL_GPL(use_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587/* Clear the unload stuff of the module. */
588static void module_unload_free(struct module *mod)
589{
590 struct module *i;
591
592 list_for_each_entry(i, &modules, list) {
593 struct module_use *use;
594
595 list_for_each_entry(use, &i->modules_which_use_me, list) {
596 if (use->module_which_uses == mod) {
597 DEBUGP("%s unusing %s\n", mod->name, i->name);
598 module_put(i);
599 list_del(&use->list);
600 kfree(use);
Kay Sievers270a6c42007-01-18 13:26:15 +0100601 sysfs_remove_link(i->holders_dir, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 /* There can be at most one match. */
603 break;
604 }
605 }
606 }
607}
608
609#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800610static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612 int ret = (flags & O_TRUNC);
613 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800614 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return ret;
616}
617#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800618static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
620 return 0;
621}
622#endif /* CONFIG_MODULE_FORCE_UNLOAD */
623
624struct stopref
625{
626 struct module *mod;
627 int flags;
628 int *forced;
629};
630
631/* Whole machine is stopped with interrupts off when this runs. */
632static int __try_stop_module(void *_sref)
633{
634 struct stopref *sref = _sref;
635
Rusty Russellda39ba52008-07-22 19:24:25 -0500636 /* If it's not unused, quit unless we're forcing. */
637 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800638 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return -EWOULDBLOCK;
640 }
641
642 /* Mark it as dying. */
643 sref->mod->state = MODULE_STATE_GOING;
644 return 0;
645}
646
647static int try_stop_module(struct module *mod, int flags, int *forced)
648{
Rusty Russellda39ba52008-07-22 19:24:25 -0500649 if (flags & O_NONBLOCK) {
650 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500652 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500653 } else {
654 /* We don't need to stop the machine for this. */
655 mod->state = MODULE_STATE_GOING;
656 synchronize_sched();
657 return 0;
658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
661unsigned int module_refcount(struct module *mod)
662{
Nick Piggin5fbfb182010-04-01 19:09:40 +1100663 unsigned int incs = 0, decs = 0;
Eric Dumazet720eba32009-02-03 13:31:36 +1030664 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Eric Dumazet720eba32009-02-03 13:31:36 +1030666 for_each_possible_cpu(cpu)
Nick Piggin5fbfb182010-04-01 19:09:40 +1100667 decs += per_cpu_ptr(mod->refptr, cpu)->decs;
668 /*
669 * ensure the incs are added up after the decs.
670 * module_put ensures incs are visible before decs with smp_wmb.
671 *
672 * This 2-count scheme avoids the situation where the refcount
673 * for CPU0 is read, then CPU0 increments the module refcount,
674 * then CPU1 drops that refcount, then the refcount for CPU1 is
675 * read. We would record a decrement but not its corresponding
676 * increment so we would see a low count (disaster).
677 *
678 * Rare situation? But module_refcount can be preempted, and we
679 * might be tallying up 4096+ CPUs. So it is not impossible.
680 */
681 smp_rmb();
682 for_each_possible_cpu(cpu)
683 incs += per_cpu_ptr(mod->refptr, cpu)->incs;
684 return incs - decs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686EXPORT_SYMBOL(module_refcount);
687
688/* This exists whether we can unload or not */
689static void free_module(struct module *mod);
690
691static void wait_for_zero_refcount(struct module *mod)
692{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500693 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800694 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 for (;;) {
696 DEBUGP("Looking at refcount...\n");
697 set_current_state(TASK_UNINTERRUPTIBLE);
698 if (module_refcount(mod) == 0)
699 break;
700 schedule();
701 }
702 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800703 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100706SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
707 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800710 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 int ret, forced = 0;
712
Kees Cook3d433212009-04-02 15:49:29 -0700713 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800714 return -EPERM;
715
716 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
717 return -EFAULT;
718 name[MODULE_NAME_LEN-1] = '\0';
719
Tejun Heo3fc1f1e2010-05-06 18:49:20 +0200720 if (mutex_lock_interruptible(&module_mutex) != 0)
721 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 mod = find_module(name);
724 if (!mod) {
725 ret = -ENOENT;
726 goto out;
727 }
728
729 if (!list_empty(&mod->modules_which_use_me)) {
730 /* Other modules depend on us: get rid of them first. */
731 ret = -EWOULDBLOCK;
732 goto out;
733 }
734
735 /* Doing init or already dying? */
736 if (mod->state != MODULE_STATE_LIVE) {
737 /* FIXME: if (force), slam module count and wake up
738 waiter --RR */
739 DEBUGP("%s already dying\n", mod->name);
740 ret = -EBUSY;
741 goto out;
742 }
743
744 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700745 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800746 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (!forced) {
748 /* This module can't be removed */
749 ret = -EBUSY;
750 goto out;
751 }
752 }
753
754 /* Set this up before setting mod->state */
755 mod->waiter = current;
756
757 /* Stop the machine so refcounts can't move and disable module. */
758 ret = try_stop_module(mod, flags, &forced);
759 if (ret != 0)
760 goto out;
761
762 /* Never wait if forced. */
763 if (!forced && module_refcount(mod) != 0)
764 wait_for_zero_refcount(mod);
765
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200766 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 /* Final destruction now noone is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200768 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200770 blocking_notifier_call_chain(&module_notify_list,
771 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800772 async_synchronize_full();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200773 mutex_lock(&module_mutex);
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100774 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500775 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Jason Barone9d376f2009-02-05 11:51:38 -0500776 ddebug_remove_module(mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 free_module(mod);
778
779 out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800780 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return ret;
782}
783
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800784static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 struct module_use *use;
787 int printed_something = 0;
788
789 seq_printf(m, " %u ", module_refcount(mod));
790
791 /* Always include a trailing , so userspace can differentiate
792 between this and the old multi-field proc format. */
793 list_for_each_entry(use, &mod->modules_which_use_me, list) {
794 printed_something = 1;
795 seq_printf(m, "%s,", use->module_which_uses->name);
796 }
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (mod->init != NULL && mod->exit == NULL) {
799 printed_something = 1;
800 seq_printf(m, "[permanent],");
801 }
802
803 if (!printed_something)
804 seq_printf(m, "-");
805}
806
807void __symbol_put(const char *symbol)
808{
809 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Rusty Russell24da1cb2007-07-15 23:41:46 -0700811 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -0500812 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 BUG();
814 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700815 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817EXPORT_SYMBOL(__symbol_put);
818
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930819/* Note this assumes addr is a function, which it currently always is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820void symbol_put_addr(void *addr)
821{
Trent Piepho5e376612006-05-15 09:44:06 -0700822 struct module *modaddr;
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930823 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930825 if (core_kernel_text(a))
Trent Piepho5e376612006-05-15 09:44:06 -0700826 return;
827
Rusty Russella6e6abd2009-03-31 13:05:31 -0600828 /* module_text_address is safe here: we're supposed to have reference
829 * to module from symbol_get, so it can't go away. */
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930830 modaddr = __module_text_address(a);
Rusty Russella6e6abd2009-03-31 13:05:31 -0600831 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -0700832 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834EXPORT_SYMBOL_GPL(symbol_put_addr);
835
836static ssize_t show_refcnt(struct module_attribute *mattr,
837 struct module *mod, char *buffer)
838{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400839 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841
842static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900843 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 .show = show_refcnt,
845};
846
Al Virof6a57032006-10-18 01:47:25 -0400847void module_put(struct module *module)
848{
849 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +0900850 preempt_disable();
Nick Piggin5fbfb182010-04-01 19:09:40 +1100851 smp_wmb(); /* see comment in module_refcount */
852 __this_cpu_inc(module->refptr->decs);
Christoph Lametere1783a22010-01-05 15:34:50 +0900853
Li Zefanae832d12010-03-24 10:57:43 +0800854 trace_module_put(module, _RET_IP_);
Al Virof6a57032006-10-18 01:47:25 -0400855 /* Maybe they're waiting for us to drop reference? */
856 if (unlikely(!module_is_live(module)))
857 wake_up_process(module->waiter);
Christoph Lametere1783a22010-01-05 15:34:50 +0900858 preempt_enable();
Al Virof6a57032006-10-18 01:47:25 -0400859 }
860}
861EXPORT_SYMBOL(module_put);
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800864static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
866 /* We don't know the usage count, or what modules are using. */
867 seq_printf(m, " - -");
868}
869
870static inline void module_unload_free(struct module *mod)
871{
872}
873
Tim Abbottc6b37802008-12-05 19:03:59 -0500874int use_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Rusty Russell480b02d2010-05-19 17:33:39 -0600876 return strong_try_module_get(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
Tim Abbottc6b37802008-12-05 19:03:59 -0500878EXPORT_SYMBOL_GPL(use_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880static inline void module_unload_init(struct module *mod)
881{
882}
883#endif /* CONFIG_MODULE_UNLOAD */
884
Kay Sievers1f717402006-11-24 12:15:25 +0100885static ssize_t show_initstate(struct module_attribute *mattr,
886 struct module *mod, char *buffer)
887{
888 const char *state = "unknown";
889
890 switch (mod->state) {
891 case MODULE_STATE_LIVE:
892 state = "live";
893 break;
894 case MODULE_STATE_COMING:
895 state = "coming";
896 break;
897 case MODULE_STATE_GOING:
898 state = "going";
899 break;
900 }
901 return sprintf(buffer, "%s\n", state);
902}
903
904static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900905 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100906 .show = show_initstate,
907};
908
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800909static struct module_attribute *modinfo_attrs[] = {
910 &modinfo_version,
911 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100912 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800913#ifdef CONFIG_MODULE_UNLOAD
914 &refcnt,
915#endif
916 NULL,
917};
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919static const char vermagic[] = VERMAGIC_STRING;
920
Rusty Russellc6e665c2009-03-31 13:05:33 -0600921static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -0700922{
923#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -0700924 if (!test_taint(TAINT_FORCED_MODULE))
Rusty Russellc6e665c2009-03-31 13:05:33 -0600925 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
926 mod->name, reason);
Linus Torvalds826e4502008-05-04 17:04:16 -0700927 add_taint_module(mod, TAINT_FORCED_MODULE);
928 return 0;
929#else
930 return -ENOEXEC;
931#endif
932}
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934#ifdef CONFIG_MODVERSIONS
Rusty Russelld4703ae2009-12-15 16:28:32 -0600935/* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
936static unsigned long maybe_relocated(unsigned long crc,
937 const struct module *crc_owner)
938{
939#ifdef ARCH_RELOCATES_KCRCTAB
940 if (crc_owner == NULL)
941 return crc - (unsigned long)reloc_start;
942#endif
943 return crc;
944}
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946static int check_version(Elf_Shdr *sechdrs,
947 unsigned int versindex,
948 const char *symname,
949 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -0600950 const unsigned long *crc,
951 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
953 unsigned int i, num_versions;
954 struct modversion_info *versions;
955
956 /* Exporting module didn't supply crcs? OK, we're already tainted. */
957 if (!crc)
958 return 1;
959
Rusty Russella5dd6972008-05-09 16:24:21 +1000960 /* No versions at all? modprobe --force does this. */
961 if (versindex == 0)
962 return try_to_force_load(mod, symname) == 0;
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 versions = (void *) sechdrs[versindex].sh_addr;
965 num_versions = sechdrs[versindex].sh_size
966 / sizeof(struct modversion_info);
967
968 for (i = 0; i < num_versions; i++) {
969 if (strcmp(versions[i].name, symname) != 0)
970 continue;
971
Rusty Russelld4703ae2009-12-15 16:28:32 -0600972 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 DEBUGP("Found checksum %lX vs module %lX\n",
Rusty Russelld4703ae2009-12-15 16:28:32 -0600975 maybe_relocated(*crc, crc_owner), versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -0700976 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
Linus Torvalds826e4502008-05-04 17:04:16 -0700978
Rusty Russella5dd6972008-05-09 16:24:21 +1000979 printk(KERN_WARNING "%s: no symbol version for %s\n",
980 mod->name, symname);
981 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -0700982
983bad_version:
984 printk("%s: disagrees about version of symbol %s\n",
985 mod->name, symname);
986 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
989static inline int check_modstruct_version(Elf_Shdr *sechdrs,
990 unsigned int versindex,
991 struct module *mod)
992{
993 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Mike Frysinger6560dc12009-07-23 23:42:08 +0930995 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
996 &crc, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 BUG();
Rusty Russelld4703ae2009-12-15 16:28:32 -0600998 return check_version(sechdrs, versindex, "module_layout", mod, crc,
999 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
Rusty Russell91e37a72008-05-09 16:25:28 +10001002/* First part is kernel version, which we ignore if module has crcs. */
1003static inline int same_magic(const char *amagic, const char *bmagic,
1004 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Rusty Russell91e37a72008-05-09 16:25:28 +10001006 if (has_crcs) {
1007 amagic += strcspn(amagic, " ");
1008 bmagic += strcspn(bmagic, " ");
1009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return strcmp(amagic, bmagic) == 0;
1011}
1012#else
1013static inline int check_version(Elf_Shdr *sechdrs,
1014 unsigned int versindex,
1015 const char *symname,
1016 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001017 const unsigned long *crc,
1018 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
1020 return 1;
1021}
1022
1023static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1024 unsigned int versindex,
1025 struct module *mod)
1026{
1027 return 1;
1028}
1029
Rusty Russell91e37a72008-05-09 16:25:28 +10001030static inline int same_magic(const char *amagic, const char *bmagic,
1031 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
1033 return strcmp(amagic, bmagic) == 0;
1034}
1035#endif /* CONFIG_MODVERSIONS */
1036
1037/* Resolve a symbol for this module. I.e. if we find one, record usage.
1038 Must be holding module_mutex. */
Tim Abbott414fd312008-12-05 19:03:56 -05001039static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
1040 unsigned int versindex,
1041 const char *name,
1042 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
1044 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001045 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 const unsigned long *crc;
Rusty Russell480b02d2010-05-19 17:33:39 -06001047 DEFINE_WAIT(wait);
1048 int err;
1049 long timeleft = 30 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Rusty Russell480b02d2010-05-19 17:33:39 -06001051again:
Tim Abbott414fd312008-12-05 19:03:56 -05001052 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001053 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Rusty Russell480b02d2010-05-19 17:33:39 -06001054 if (!sym)
1055 return NULL;
1056
1057 if (!check_version(sechdrs, versindex, name, mod, crc, owner))
1058 return NULL;
1059
1060 prepare_to_wait(&module_wq, &wait, TASK_INTERRUPTIBLE);
1061 err = use_module(mod, owner);
1062 if (likely(!err) || err != -EBUSY || signal_pending(current)) {
1063 finish_wait(&module_wq, &wait);
1064 return err ? NULL : sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
Rusty Russell480b02d2010-05-19 17:33:39 -06001066
1067 /* Module is still loading. Drop lock and wait. */
1068 mutex_unlock(&module_mutex);
1069 timeleft = schedule_timeout(timeleft);
1070 mutex_lock(&module_mutex);
1071 finish_wait(&module_wq, &wait);
1072
1073 /* Module might be gone entirely, or replaced. Re-lookup. */
1074 if (timeleft)
1075 goto again;
1076
1077 printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
1078 mod->name, owner->name);
1079 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082/*
1083 * /sys/module/foo/sections stuff
1084 * J. Corbet <corbet@lwn.net>
1085 */
Kay Sievers120fc3d2008-02-21 00:33:20 +01001086#if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
Ben Hutchings10b465a2009-12-19 14:43:01 +00001087
1088static inline bool sect_empty(const Elf_Shdr *sect)
1089{
1090 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1091}
1092
Rusty Russella58730c2008-03-13 09:03:44 +00001093struct module_sect_attr
1094{
1095 struct module_attribute mattr;
1096 char *name;
1097 unsigned long address;
1098};
1099
1100struct module_sect_attrs
1101{
1102 struct attribute_group grp;
1103 unsigned int nsections;
1104 struct module_sect_attr attrs[0];
1105};
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107static ssize_t module_sect_show(struct module_attribute *mattr,
1108 struct module *mod, char *buf)
1109{
1110 struct module_sect_attr *sattr =
1111 container_of(mattr, struct module_sect_attr, mattr);
1112 return sprintf(buf, "0x%lx\n", sattr->address);
1113}
1114
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001115static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1116{
Rusty Russella58730c2008-03-13 09:03:44 +00001117 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001118
1119 for (section = 0; section < sect_attrs->nsections; section++)
1120 kfree(sect_attrs->attrs[section].name);
1121 kfree(sect_attrs);
1122}
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124static void add_sect_attrs(struct module *mod, unsigned int nsect,
1125 char *secstrings, Elf_Shdr *sechdrs)
1126{
1127 unsigned int nloaded = 0, i, size[2];
1128 struct module_sect_attrs *sect_attrs;
1129 struct module_sect_attr *sattr;
1130 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 /* Count loaded sections and allocate structures */
1133 for (i = 0; i < nsect; i++)
Ben Hutchings10b465a2009-12-19 14:43:01 +00001134 if (!sect_empty(&sechdrs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 nloaded++;
1136 size[0] = ALIGN(sizeof(*sect_attrs)
1137 + nloaded * sizeof(sect_attrs->attrs[0]),
1138 sizeof(sect_attrs->grp.attrs[0]));
1139 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001140 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1141 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return;
1143
1144 /* Setup section attributes. */
1145 sect_attrs->grp.name = "sections";
1146 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1147
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001148 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 sattr = &sect_attrs->attrs[0];
1150 gattr = &sect_attrs->grp.attrs[0];
1151 for (i = 0; i < nsect; i++) {
Ben Hutchings10b465a2009-12-19 14:43:01 +00001152 if (sect_empty(&sechdrs[i]))
Helge Deller35dead42009-12-03 00:29:15 +01001153 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 sattr->address = sechdrs[i].sh_addr;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001155 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1156 GFP_KERNEL);
1157 if (sattr->name == NULL)
1158 goto out;
1159 sect_attrs->nsections++;
Eric W. Biederman361795b2010-02-12 13:41:56 -08001160 sysfs_attr_init(&sattr->mattr.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 sattr->mattr.show = module_sect_show;
1162 sattr->mattr.store = NULL;
1163 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 sattr->mattr.attr.mode = S_IRUGO;
1165 *(gattr++) = &(sattr++)->mattr.attr;
1166 }
1167 *gattr = NULL;
1168
1169 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1170 goto out;
1171
1172 mod->sect_attrs = sect_attrs;
1173 return;
1174 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001175 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177
1178static void remove_sect_attrs(struct module *mod)
1179{
1180 if (mod->sect_attrs) {
1181 sysfs_remove_group(&mod->mkobj.kobj,
1182 &mod->sect_attrs->grp);
1183 /* We are positive that no one is using any sect attrs
1184 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001185 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 mod->sect_attrs = NULL;
1187 }
1188}
1189
Roland McGrath6d760132007-10-16 23:26:40 -07001190/*
1191 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1192 */
1193
1194struct module_notes_attrs {
1195 struct kobject *dir;
1196 unsigned int notes;
1197 struct bin_attribute attrs[0];
1198};
1199
1200static ssize_t module_notes_read(struct kobject *kobj,
1201 struct bin_attribute *bin_attr,
1202 char *buf, loff_t pos, size_t count)
1203{
1204 /*
1205 * The caller checked the pos and count against our size.
1206 */
1207 memcpy(buf, bin_attr->private + pos, count);
1208 return count;
1209}
1210
1211static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1212 unsigned int i)
1213{
1214 if (notes_attrs->dir) {
1215 while (i-- > 0)
1216 sysfs_remove_bin_file(notes_attrs->dir,
1217 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001218 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001219 }
1220 kfree(notes_attrs);
1221}
1222
1223static void add_notes_attrs(struct module *mod, unsigned int nsect,
1224 char *secstrings, Elf_Shdr *sechdrs)
1225{
1226 unsigned int notes, loaded, i;
1227 struct module_notes_attrs *notes_attrs;
1228 struct bin_attribute *nattr;
1229
Ingo Molnarea6bff32009-08-28 10:44:56 +02001230 /* failed to create section attributes, so can't create notes */
1231 if (!mod->sect_attrs)
1232 return;
1233
Roland McGrath6d760132007-10-16 23:26:40 -07001234 /* Count notes sections and allocate structures. */
1235 notes = 0;
1236 for (i = 0; i < nsect; i++)
Ben Hutchings10b465a2009-12-19 14:43:01 +00001237 if (!sect_empty(&sechdrs[i]) &&
Roland McGrath6d760132007-10-16 23:26:40 -07001238 (sechdrs[i].sh_type == SHT_NOTE))
1239 ++notes;
1240
1241 if (notes == 0)
1242 return;
1243
1244 notes_attrs = kzalloc(sizeof(*notes_attrs)
1245 + notes * sizeof(notes_attrs->attrs[0]),
1246 GFP_KERNEL);
1247 if (notes_attrs == NULL)
1248 return;
1249
1250 notes_attrs->notes = notes;
1251 nattr = &notes_attrs->attrs[0];
1252 for (loaded = i = 0; i < nsect; ++i) {
Ben Hutchings10b465a2009-12-19 14:43:01 +00001253 if (sect_empty(&sechdrs[i]))
Roland McGrath6d760132007-10-16 23:26:40 -07001254 continue;
1255 if (sechdrs[i].sh_type == SHT_NOTE) {
Eric W. Biederman361795b2010-02-12 13:41:56 -08001256 sysfs_bin_attr_init(nattr);
Roland McGrath6d760132007-10-16 23:26:40 -07001257 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1258 nattr->attr.mode = S_IRUGO;
1259 nattr->size = sechdrs[i].sh_size;
1260 nattr->private = (void *) sechdrs[i].sh_addr;
1261 nattr->read = module_notes_read;
1262 ++nattr;
1263 }
1264 ++loaded;
1265 }
1266
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001267 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001268 if (!notes_attrs->dir)
1269 goto out;
1270
1271 for (i = 0; i < notes; ++i)
1272 if (sysfs_create_bin_file(notes_attrs->dir,
1273 &notes_attrs->attrs[i]))
1274 goto out;
1275
1276 mod->notes_attrs = notes_attrs;
1277 return;
1278
1279 out:
1280 free_notes_attrs(notes_attrs, i);
1281}
1282
1283static void remove_notes_attrs(struct module *mod)
1284{
1285 if (mod->notes_attrs)
1286 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1287}
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1292 char *sectstrings, Elf_Shdr *sechdrs)
1293{
1294}
1295
1296static inline void remove_sect_attrs(struct module *mod)
1297{
1298}
Roland McGrath6d760132007-10-16 23:26:40 -07001299
1300static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1301 char *sectstrings, Elf_Shdr *sechdrs)
1302{
1303}
1304
1305static inline void remove_notes_attrs(struct module *mod)
1306{
1307}
Kay Sievers120fc3d2008-02-21 00:33:20 +01001308#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Randy Dunlapef665c12007-02-13 15:19:06 -08001310#ifdef CONFIG_SYSFS
1311int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001312{
1313 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001314 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001315 int error = 0;
1316 int i;
1317
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001318 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1319 (ARRAY_SIZE(modinfo_attrs) + 1)),
1320 GFP_KERNEL);
1321 if (!mod->modinfo_attrs)
1322 return -ENOMEM;
1323
1324 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001325 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1326 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001327 (attr->test && attr->test(mod))) {
1328 memcpy(temp_attr, attr, sizeof(*temp_attr));
Eric W. Biederman361795b2010-02-12 13:41:56 -08001329 sysfs_attr_init(&temp_attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001330 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1331 ++temp_attr;
1332 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001333 }
1334 return error;
1335}
1336
Randy Dunlapef665c12007-02-13 15:19:06 -08001337void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001338{
1339 struct module_attribute *attr;
1340 int i;
1341
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001342 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1343 /* pick a field to test for end of list */
1344 if (!attr->attr.name)
1345 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001346 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001347 if (attr->free)
1348 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001349 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001350 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001351}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Randy Dunlapef665c12007-02-13 15:19:06 -08001353int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
1355 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001356 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001358 if (!module_sysfs_initialized) {
1359 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001360 mod->name);
1361 err = -EINVAL;
1362 goto out;
1363 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001364
1365 kobj = kset_find_obj(module_kset, mod->name);
1366 if (kobj) {
1367 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1368 kobject_put(kobj);
1369 err = -EINVAL;
1370 goto out;
1371 }
1372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001374
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001375 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1376 mod->mkobj.kobj.kset = module_kset;
1377 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1378 "%s", mod->name);
1379 if (err)
1380 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001381
Kay Sievers97c146ef2007-11-29 23:46:11 +01001382 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001383out:
1384 return err;
1385}
1386
Randy Dunlapef665c12007-02-13 15:19:06 -08001387int mod_sysfs_setup(struct module *mod,
Kay Sievers270a6c42007-01-18 13:26:15 +01001388 struct kernel_param *kparam,
1389 unsigned int num_params)
1390{
1391 int err;
1392
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001393 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001394 if (!mod->holders_dir) {
1395 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001396 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001397 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 err = module_param_sysfs_setup(mod, kparam, num_params);
1400 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001401 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Matt Domschc988d2b2005-06-23 22:05:15 -07001403 err = module_add_modinfo_attrs(mod);
1404 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001405 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001406
Kay Sieverse17e0f52006-11-24 12:15:25 +01001407 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return 0;
1409
Kay Sieverse17e0f52006-11-24 12:15:25 +01001410out_unreg_param:
1411 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001412out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001413 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001414out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001415 kobject_put(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return err;
1417}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001418
1419static void mod_sysfs_fini(struct module *mod)
1420{
1421 kobject_put(&mod->mkobj.kobj);
1422}
1423
1424#else /* CONFIG_SYSFS */
1425
1426static void mod_sysfs_fini(struct module *mod)
1427{
1428}
1429
1430#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432static void mod_kobject_remove(struct module *mod)
1433{
Matt Domschc988d2b2005-06-23 22:05:15 -07001434 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001436 kobject_put(mod->mkobj.drivers_dir);
1437 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001438 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439}
1440
1441/*
1442 * unlink the module with the whole machine is stopped with interrupts off
1443 * - this defends against kallsyms not taking locks
1444 */
1445static int __unlink_module(void *_mod)
1446{
1447 struct module *mod = _mod;
1448 list_del(&mod->list);
1449 return 0;
1450}
1451
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001452/* Free a module, remove from lists, etc (must hold module_mutex). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453static void free_module(struct module *mod)
1454{
Li Zefan7ead8b82009-08-17 16:56:28 +08001455 trace_module_free(mod);
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 /* Delete from various lists */
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001458 stop_machine(__unlink_module, mod, NULL);
Roland McGrath6d760132007-10-16 23:26:40 -07001459 remove_notes_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 remove_sect_attrs(mod);
1461 mod_kobject_remove(mod);
1462
1463 /* Arch-specific cleanup. */
1464 module_arch_cleanup(mod);
1465
1466 /* Module unload stuff */
1467 module_unload_free(mod);
1468
Rusty Russelle180a6b2009-03-31 13:05:29 -06001469 /* Free any allocated parameters. */
1470 destroy_params(mod->kp, mod->num_kp);
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 /* This may be NULL, but that's OK */
1473 module_free(mod, mod->module_init);
1474 kfree(mod->args);
Tejun Heo259354d2010-03-10 18:56:10 +09001475 percpu_modfree(mod);
Christoph Lametere1783a22010-01-05 15:34:50 +09001476#if defined(CONFIG_MODULE_UNLOAD)
Eric Dumazet720eba32009-02-03 13:31:36 +10301477 if (mod->refptr)
Christoph Lametere1783a22010-01-05 15:34:50 +09001478 free_percpu(mod->refptr);
Eric Dumazet720eba32009-02-03 13:31:36 +10301479#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001480 /* Free lock-classes: */
1481 lockdep_free_key_range(mod->module_core, mod->core_size);
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 /* Finally, free the core (containing the module structure) */
1484 module_free(mod, mod->module_core);
Bernd Schmidteb8cdec2009-09-21 17:03:57 -07001485
1486#ifdef CONFIG_MPU
1487 update_protections(current->mm);
1488#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489}
1490
1491void *__symbol_get(const char *symbol)
1492{
1493 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001494 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Rusty Russell24da1cb2007-07-15 23:41:46 -07001496 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001497 sym = find_symbol(symbol, &owner, NULL, true, true);
1498 if (sym && strong_try_module_get(owner))
1499 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001500 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Tim Abbott414fd312008-12-05 19:03:56 -05001502 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503}
1504EXPORT_SYMBOL_GPL(__symbol_get);
1505
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001506/*
1507 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001508 * in the kernel or in some other module's exported symbol table.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001509 */
1510static int verify_export_symbols(struct module *mod)
1511{
Rusty Russellb2111042008-05-01 21:15:00 -05001512 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001513 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001514 const struct kernel_symbol *s;
1515 struct {
1516 const struct kernel_symbol *sym;
1517 unsigned int num;
1518 } arr[] = {
1519 { mod->syms, mod->num_syms },
1520 { mod->gpl_syms, mod->num_gpl_syms },
1521 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001522#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001523 { mod->unused_syms, mod->num_unused_syms },
1524 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001525#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001526 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001527
Rusty Russellb2111042008-05-01 21:15:00 -05001528 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1529 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Tim Abbott414fd312008-12-05 19:03:56 -05001530 if (find_symbol(s->name, &owner, NULL, true, false)) {
Rusty Russellb2111042008-05-01 21:15:00 -05001531 printk(KERN_ERR
1532 "%s: exports duplicate symbol %s"
1533 " (owned by %s)\n",
1534 mod->name, s->name, module_name(owner));
1535 return -ENOEXEC;
1536 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001537 }
Rusty Russellb2111042008-05-01 21:15:00 -05001538 }
1539 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001540}
1541
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001542/* Change all symbols so that st_value encodes the pointer directly. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543static int simplify_symbols(Elf_Shdr *sechdrs,
1544 unsigned int symindex,
1545 const char *strtab,
1546 unsigned int versindex,
1547 unsigned int pcpuindex,
1548 struct module *mod)
1549{
1550 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1551 unsigned long secbase;
1552 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1553 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05001554 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
1556 for (i = 1; i < n; i++) {
1557 switch (sym[i].st_shndx) {
1558 case SHN_COMMON:
1559 /* We compiled with -fno-common. These are not
1560 supposed to happen. */
1561 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1562 printk("%s: please compile with -fno-common\n",
1563 mod->name);
1564 ret = -ENOEXEC;
1565 break;
1566
1567 case SHN_ABS:
1568 /* Don't need to do anything */
1569 DEBUGP("Absolute symbol: 0x%08lx\n",
1570 (long)sym[i].st_value);
1571 break;
1572
1573 case SHN_UNDEF:
Tim Abbott414fd312008-12-05 19:03:56 -05001574 ksym = resolve_symbol(sechdrs, versindex,
1575 strtab + sym[i].st_name, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 /* Ok if resolved. */
Tim Abbott414fd312008-12-05 19:03:56 -05001577 if (ksym) {
1578 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 break;
Tim Abbott414fd312008-12-05 19:03:56 -05001580 }
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 /* Ok if weak. */
1583 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1584 break;
1585
1586 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1587 mod->name, strtab + sym[i].st_name);
1588 ret = -ENOENT;
1589 break;
1590
1591 default:
1592 /* Divert to percpu allocation if a percpu var. */
1593 if (sym[i].st_shndx == pcpuindex)
Tejun Heo259354d2010-03-10 18:56:10 +09001594 secbase = (unsigned long)mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 else
1596 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1597 sym[i].st_value += secbase;
1598 break;
1599 }
1600 }
1601
1602 return ret;
1603}
1604
Helge Deller088af9a2008-12-31 12:31:18 +01001605/* Additional bytes needed by arch in front of individual sections */
1606unsigned int __weak arch_mod_section_prepend(struct module *mod,
1607 unsigned int section)
1608{
1609 /* default implementation just returns zero */
1610 return 0;
1611}
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01001614static long get_offset(struct module *mod, unsigned int *size,
1615 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
1617 long ret;
1618
Helge Deller088af9a2008-12-31 12:31:18 +01001619 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1621 *size = ret + sechdr->sh_size;
1622 return ret;
1623}
1624
1625/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1626 might -- code, read-only data, read-write data, small data. Tally
1627 sizes, and place the offsets into sh_entsize fields: high bit means it
1628 belongs in init. */
1629static void layout_sections(struct module *mod,
1630 const Elf_Ehdr *hdr,
1631 Elf_Shdr *sechdrs,
1632 const char *secstrings)
1633{
1634 static unsigned long const masks[][2] = {
1635 /* NOTE: all executable code must be the first section
1636 * in this array; otherwise modify the text_size
1637 * finder in the two loops below */
1638 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1639 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1640 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1641 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1642 };
1643 unsigned int m, i;
1644
1645 for (i = 0; i < hdr->e_shnum; i++)
1646 sechdrs[i].sh_entsize = ~0UL;
1647
1648 DEBUGP("Core section allocation order:\n");
1649 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1650 for (i = 0; i < hdr->e_shnum; ++i) {
1651 Elf_Shdr *s = &sechdrs[i];
1652
1653 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1654 || (s->sh_flags & masks[m][1])
1655 || s->sh_entsize != ~0UL
Rusty Russell49502672009-03-31 13:05:36 -06001656 || strstarts(secstrings + s->sh_name, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001658 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 DEBUGP("\t%s\n", secstrings + s->sh_name);
1660 }
1661 if (m == 0)
1662 mod->core_text_size = mod->core_size;
1663 }
1664
1665 DEBUGP("Init section allocation order:\n");
1666 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1667 for (i = 0; i < hdr->e_shnum; ++i) {
1668 Elf_Shdr *s = &sechdrs[i];
1669
1670 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1671 || (s->sh_flags & masks[m][1])
1672 || s->sh_entsize != ~0UL
Rusty Russell49502672009-03-31 13:05:36 -06001673 || !strstarts(secstrings + s->sh_name, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001675 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 | INIT_OFFSET_MASK);
1677 DEBUGP("\t%s\n", secstrings + s->sh_name);
1678 }
1679 if (m == 0)
1680 mod->init_text_size = mod->init_size;
1681 }
1682}
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684static void set_license(struct module *mod, const char *license)
1685{
1686 if (!license)
1687 license = "unspecified";
1688
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001689 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07001690 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001691 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001692 "kernel.\n", mod->name, license);
1693 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
1695}
1696
1697/* Parse tag=value strings from .modinfo section */
1698static char *next_string(char *string, unsigned long *secsize)
1699{
1700 /* Skip non-zero chars */
1701 while (string[0]) {
1702 string++;
1703 if ((*secsize)-- <= 1)
1704 return NULL;
1705 }
1706
1707 /* Skip any zero padding. */
1708 while (!string[0]) {
1709 string++;
1710 if ((*secsize)-- <= 1)
1711 return NULL;
1712 }
1713 return string;
1714}
1715
1716static char *get_modinfo(Elf_Shdr *sechdrs,
1717 unsigned int info,
1718 const char *tag)
1719{
1720 char *p;
1721 unsigned int taglen = strlen(tag);
1722 unsigned long size = sechdrs[info].sh_size;
1723
1724 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1725 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1726 return p + taglen + 1;
1727 }
1728 return NULL;
1729}
1730
Matt Domschc988d2b2005-06-23 22:05:15 -07001731static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1732 unsigned int infoindex)
1733{
1734 struct module_attribute *attr;
1735 int i;
1736
1737 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1738 if (attr->setup)
1739 attr->setup(mod,
1740 get_modinfo(sechdrs,
1741 infoindex,
1742 attr->attr.name));
1743 }
1744}
Matt Domschc988d2b2005-06-23 22:05:15 -07001745
Rusty Russella263f7762009-09-25 00:32:58 -06001746static void free_modinfo(struct module *mod)
1747{
1748 struct module_attribute *attr;
1749 int i;
1750
1751 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1752 if (attr->free)
1753 attr->free(mod);
1754 }
1755}
1756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01001758
1759/* lookup symbol in given range of kernel_symbols */
1760static const struct kernel_symbol *lookup_symbol(const char *name,
1761 const struct kernel_symbol *start,
1762 const struct kernel_symbol *stop)
1763{
1764 const struct kernel_symbol *ks = start;
1765 for (; ks < stop; ks++)
1766 if (strcmp(ks->name, name) == 0)
1767 return ks;
1768 return NULL;
1769}
1770
Tim Abbottca4787b2009-01-05 08:40:10 -06001771static int is_exported(const char *name, unsigned long value,
1772 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
Tim Abbottca4787b2009-01-05 08:40:10 -06001774 const struct kernel_symbol *ks;
1775 if (!mod)
1776 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001777 else
Tim Abbottca4787b2009-01-05 08:40:10 -06001778 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
1779 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
1781
1782/* As per nm */
1783static char elf_type(const Elf_Sym *sym,
1784 Elf_Shdr *sechdrs,
1785 const char *secstrings,
1786 struct module *mod)
1787{
1788 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1789 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1790 return 'v';
1791 else
1792 return 'w';
1793 }
1794 if (sym->st_shndx == SHN_UNDEF)
1795 return 'U';
1796 if (sym->st_shndx == SHN_ABS)
1797 return 'a';
1798 if (sym->st_shndx >= SHN_LORESERVE)
1799 return '?';
1800 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1801 return 't';
1802 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1803 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1804 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1805 return 'r';
1806 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1807 return 'g';
1808 else
1809 return 'd';
1810 }
1811 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1812 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1813 return 's';
1814 else
1815 return 'b';
1816 }
Rusty Russell49502672009-03-31 13:05:36 -06001817 if (strstarts(secstrings + sechdrs[sym->st_shndx].sh_name, ".debug"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 return 'n';
1819 return '?';
1820}
1821
Jan Beulich4a496222009-07-06 14:50:42 +01001822static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
1823 unsigned int shnum)
1824{
1825 const Elf_Shdr *sec;
1826
1827 if (src->st_shndx == SHN_UNDEF
1828 || src->st_shndx >= shnum
1829 || !src->st_name)
1830 return false;
1831
1832 sec = sechdrs + src->st_shndx;
1833 if (!(sec->sh_flags & SHF_ALLOC)
1834#ifndef CONFIG_KALLSYMS_ALL
1835 || !(sec->sh_flags & SHF_EXECINSTR)
1836#endif
1837 || (sec->sh_entsize & INIT_OFFSET_MASK))
1838 return false;
1839
1840 return true;
1841}
1842
1843static unsigned long layout_symtab(struct module *mod,
1844 Elf_Shdr *sechdrs,
1845 unsigned int symindex,
Jan Beulich554bdfe2009-07-06 14:51:44 +01001846 unsigned int strindex,
Jan Beulich4a496222009-07-06 14:50:42 +01001847 const Elf_Ehdr *hdr,
Jan Beulich554bdfe2009-07-06 14:51:44 +01001848 const char *secstrings,
1849 unsigned long *pstroffs,
1850 unsigned long *strmap)
Jan Beulich4a496222009-07-06 14:50:42 +01001851{
1852 unsigned long symoffs;
1853 Elf_Shdr *symsect = sechdrs + symindex;
Jan Beulich554bdfe2009-07-06 14:51:44 +01001854 Elf_Shdr *strsect = sechdrs + strindex;
Jan Beulich4a496222009-07-06 14:50:42 +01001855 const Elf_Sym *src;
Jan Beulich554bdfe2009-07-06 14:51:44 +01001856 const char *strtab;
Jan Beulich4a496222009-07-06 14:50:42 +01001857 unsigned int i, nsrc, ndst;
1858
1859 /* Put symbol section at end of init part of module. */
1860 symsect->sh_flags |= SHF_ALLOC;
1861 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
1862 symindex) | INIT_OFFSET_MASK;
1863 DEBUGP("\t%s\n", secstrings + symsect->sh_name);
1864
1865 src = (void *)hdr + symsect->sh_offset;
1866 nsrc = symsect->sh_size / sizeof(*src);
Jan Beulich554bdfe2009-07-06 14:51:44 +01001867 strtab = (void *)hdr + strsect->sh_offset;
Jan Beulich4a496222009-07-06 14:50:42 +01001868 for (ndst = i = 1; i < nsrc; ++i, ++src)
Jan Beulich554bdfe2009-07-06 14:51:44 +01001869 if (is_core_symbol(src, sechdrs, hdr->e_shnum)) {
1870 unsigned int j = src->st_name;
1871
1872 while(!__test_and_set_bit(j, strmap) && strtab[j])
1873 ++j;
Jan Beulich4a496222009-07-06 14:50:42 +01001874 ++ndst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01001875 }
Jan Beulich4a496222009-07-06 14:50:42 +01001876
1877 /* Append room for core symbols at end of core part. */
1878 symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
1879 mod->core_size = symoffs + ndst * sizeof(Elf_Sym);
1880
Jan Beulich554bdfe2009-07-06 14:51:44 +01001881 /* Put string table section at end of init part of module. */
1882 strsect->sh_flags |= SHF_ALLOC;
1883 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
1884 strindex) | INIT_OFFSET_MASK;
1885 DEBUGP("\t%s\n", secstrings + strsect->sh_name);
1886
1887 /* Append room for core symbols' strings at end of core part. */
1888 *pstroffs = mod->core_size;
1889 __set_bit(0, strmap);
1890 mod->core_size += bitmap_weight(strmap, strsect->sh_size);
1891
Jan Beulich4a496222009-07-06 14:50:42 +01001892 return symoffs;
1893}
1894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895static void add_kallsyms(struct module *mod,
1896 Elf_Shdr *sechdrs,
Jan Beulich4a496222009-07-06 14:50:42 +01001897 unsigned int shnum,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 unsigned int symindex,
1899 unsigned int strindex,
Jan Beulich4a496222009-07-06 14:50:42 +01001900 unsigned long symoffs,
Jan Beulich554bdfe2009-07-06 14:51:44 +01001901 unsigned long stroffs,
1902 const char *secstrings,
1903 unsigned long *strmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
Jan Beulich4a496222009-07-06 14:50:42 +01001905 unsigned int i, ndst;
1906 const Elf_Sym *src;
1907 Elf_Sym *dst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01001908 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1911 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1912 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1913
1914 /* Set types up while we still have access to sections. */
1915 for (i = 0; i < mod->num_symtab; i++)
1916 mod->symtab[i].st_info
1917 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
Jan Beulich4a496222009-07-06 14:50:42 +01001918
1919 mod->core_symtab = dst = mod->module_core + symoffs;
1920 src = mod->symtab;
1921 *dst = *src;
1922 for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
1923 if (!is_core_symbol(src, sechdrs, shnum))
1924 continue;
1925 dst[ndst] = *src;
Jan Beulich554bdfe2009-07-06 14:51:44 +01001926 dst[ndst].st_name = bitmap_weight(strmap, dst[ndst].st_name);
Jan Beulich4a496222009-07-06 14:50:42 +01001927 ++ndst;
1928 }
1929 mod->core_num_syms = ndst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01001930
1931 mod->core_strtab = s = mod->module_core + stroffs;
1932 for (*s = 0, i = 1; i < sechdrs[strindex].sh_size; ++i)
1933 if (test_bit(i, strmap))
1934 *++s = mod->strtab[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935}
1936#else
Jan Beulich4a496222009-07-06 14:50:42 +01001937static inline unsigned long layout_symtab(struct module *mod,
1938 Elf_Shdr *sechdrs,
1939 unsigned int symindex,
Jan Beulich554bdfe2009-07-06 14:51:44 +01001940 unsigned int strindex,
Paul Mundt3ae91c22009-10-01 15:43:54 -07001941 const Elf_Ehdr *hdr,
Jan Beulich554bdfe2009-07-06 14:51:44 +01001942 const char *secstrings,
1943 unsigned long *pstroffs,
1944 unsigned long *strmap)
Jan Beulich4a496222009-07-06 14:50:42 +01001945{
Paul Mundt3ae91c22009-10-01 15:43:54 -07001946 return 0;
Jan Beulich4a496222009-07-06 14:50:42 +01001947}
Paul Mundt3ae91c22009-10-01 15:43:54 -07001948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949static inline void add_kallsyms(struct module *mod,
1950 Elf_Shdr *sechdrs,
Jan Beulich4a496222009-07-06 14:50:42 +01001951 unsigned int shnum,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 unsigned int symindex,
1953 unsigned int strindex,
Jan Beulich4a496222009-07-06 14:50:42 +01001954 unsigned long symoffs,
Jan Beulich554bdfe2009-07-06 14:51:44 +01001955 unsigned long stroffs,
1956 const char *secstrings,
1957 const unsigned long *strmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
1959}
1960#endif /* CONFIG_KALLSYMS */
1961
Jason Barone9d376f2009-02-05 11:51:38 -05001962static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05001963{
Jason Barone9d376f2009-02-05 11:51:38 -05001964#ifdef CONFIG_DYNAMIC_DEBUG
1965 if (ddebug_add_module(debug, num, debug->modname))
1966 printk(KERN_ERR "dynamic debug error adding module: %s\n",
1967 debug->modname);
1968#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05001969}
Jason Baron346e15b2008-08-12 16:46:19 -04001970
Rusty Russell3a642e92008-07-22 19:24:28 -05001971static void *module_alloc_update_bounds(unsigned long size)
1972{
1973 void *ret = module_alloc(size);
1974
1975 if (ret) {
1976 /* Update module bounds. */
1977 if ((unsigned long)ret < module_addr_min)
1978 module_addr_min = (unsigned long)ret;
1979 if ((unsigned long)ret + size > module_addr_max)
1980 module_addr_max = (unsigned long)ret + size;
1981 }
1982 return ret;
1983}
1984
Catalin Marinas4f2294b2009-06-11 13:23:20 +01001985#ifdef CONFIG_DEBUG_KMEMLEAK
1986static void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
1987 Elf_Shdr *sechdrs, char *secstrings)
1988{
1989 unsigned int i;
1990
1991 /* only scan the sections containing data */
Catalin Marinasc017b4be32009-10-28 13:33:09 +00001992 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01001993
1994 for (i = 1; i < hdr->e_shnum; i++) {
1995 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1996 continue;
1997 if (strncmp(secstrings + sechdrs[i].sh_name, ".data", 5) != 0
1998 && strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) != 0)
1999 continue;
2000
Catalin Marinasc017b4be32009-10-28 13:33:09 +00002001 kmemleak_scan_area((void *)sechdrs[i].sh_addr,
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002002 sechdrs[i].sh_size, GFP_KERNEL);
2003 }
2004}
2005#else
2006static inline void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
2007 Elf_Shdr *sechdrs, char *secstrings)
2008{
2009}
2010#endif
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012/* Allocate and load the module: note that size of section 0 is always
2013 zero, and we rely on this for optional sections. */
Linus Torvaldsffb4ba72008-08-25 11:10:26 -07002014static noinline struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 unsigned long len,
2016 const char __user *uargs)
2017{
2018 Elf_Ehdr *hdr;
2019 Elf_Shdr *sechdrs;
2020 char *secstrings, *args, *modmagic, *strtab = NULL;
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002021 char *staging;
Andrew Morton84860f92006-06-28 04:26:46 -07002022 unsigned int i;
2023 unsigned int symindex = 0;
2024 unsigned int strindex = 0;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002025 unsigned int modindex, versindex, infoindex, pcpuindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 struct module *mod;
2027 long err = 0;
Tejun Heo259354d2010-03-10 18:56:10 +09002028 void *ptr = NULL; /* Stops spurious gcc warning */
Jan Beulich554bdfe2009-07-06 14:51:44 +01002029 unsigned long symoffs, stroffs, *strmap;
Paul Mundt3ae91c22009-10-01 15:43:54 -07002030
Thomas Koeller378bac82005-09-06 15:17:11 -07002031 mm_segment_t old_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
2033 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
2034 umod, len, uargs);
2035 if (len < sizeof(*hdr))
2036 return ERR_PTR(-ENOEXEC);
2037
2038 /* Suck in entire file: we'll want most of it. */
2039 /* vmalloc barfs on "unusual" numbers. Check here */
2040 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
2041 return ERR_PTR(-ENOMEM);
Heiko Carstens9e01892c2008-12-22 12:36:31 +01002042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 if (copy_from_user(hdr, umod, len) != 0) {
2044 err = -EFAULT;
2045 goto free_hdr;
2046 }
2047
2048 /* Sanity checks against insmoding binaries or wrong arch,
2049 weird elf version */
Cyrill Gorcunovc4ea6fc2008-05-14 16:27:29 -07002050 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 || hdr->e_type != ET_REL
2052 || !elf_check_arch(hdr)
2053 || hdr->e_shentsize != sizeof(*sechdrs)) {
2054 err = -ENOEXEC;
2055 goto free_hdr;
2056 }
2057
2058 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
2059 goto truncated;
2060
2061 /* Convenience variables */
2062 sechdrs = (void *)hdr + hdr->e_shoff;
2063 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
2064 sechdrs[0].sh_addr = 0;
2065
2066 for (i = 1; i < hdr->e_shnum; i++) {
2067 if (sechdrs[i].sh_type != SHT_NOBITS
2068 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
2069 goto truncated;
2070
2071 /* Mark all sections sh_addr with their address in the
2072 temporary image. */
2073 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
2074
2075 /* Internal symbols and strings. */
2076 if (sechdrs[i].sh_type == SHT_SYMTAB) {
2077 symindex = i;
2078 strindex = sechdrs[i].sh_link;
2079 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
2080 }
2081#ifndef CONFIG_MODULE_UNLOAD
2082 /* Don't load .exit sections */
Rusty Russell49502672009-03-31 13:05:36 -06002083 if (strstarts(secstrings+sechdrs[i].sh_name, ".exit"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
2085#endif
2086 }
2087
2088 modindex = find_sec(hdr, sechdrs, secstrings,
2089 ".gnu.linkonce.this_module");
2090 if (!modindex) {
2091 printk(KERN_WARNING "No module found in object\n");
2092 err = -ENOEXEC;
2093 goto free_hdr;
2094 }
Rusty Russell5e458cc2008-10-22 10:00:13 -05002095 /* This is temporary: point mod into copy of data. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 mod = (void *)sechdrs[modindex].sh_addr;
2097
2098 if (symindex == 0) {
2099 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2100 mod->name);
2101 err = -ENOEXEC;
2102 goto free_hdr;
2103 }
2104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
2106 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
2107 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
2108
Rusty Russellea01e792008-03-13 09:02:17 +00002109 /* Don't keep modinfo and version sections. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russellea01e792008-03-13 09:02:17 +00002111 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
2113 /* Check module struct version now, before we try to use module. */
2114 if (!check_modstruct_version(sechdrs, versindex, mod)) {
2115 err = -ENOEXEC;
2116 goto free_hdr;
2117 }
2118
2119 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
2120 /* This is allowed: modprobe --force will invalidate it. */
2121 if (!modmagic) {
Rusty Russellc6e665c2009-03-31 13:05:33 -06002122 err = try_to_force_load(mod, "bad vermagic");
Linus Torvalds826e4502008-05-04 17:04:16 -07002123 if (err)
2124 goto free_hdr;
Rusty Russell91e37a72008-05-09 16:25:28 +10002125 } else if (!same_magic(modmagic, vermagic, versindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2127 mod->name, modmagic, vermagic);
2128 err = -ENOEXEC;
2129 goto free_hdr;
2130 }
2131
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002132 staging = get_modinfo(sechdrs, infoindex, "staging");
2133 if (staging) {
2134 add_taint_module(mod, TAINT_CRAP);
2135 printk(KERN_WARNING "%s: module is from the staging directory,"
2136 " the quality is unknown, you have been warned.\n",
2137 mod->name);
2138 }
2139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 /* Now copy in args */
Davi Arnaut24277dd2006-03-24 03:18:43 -08002141 args = strndup_user(uargs, ~0UL >> 1);
2142 if (IS_ERR(args)) {
2143 err = PTR_ERR(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 goto free_hdr;
2145 }
Andrew Morton8e08b752006-02-07 12:58:45 -08002146
Jan Beulich554bdfe2009-07-06 14:51:44 +01002147 strmap = kzalloc(BITS_TO_LONGS(sechdrs[strindex].sh_size)
2148 * sizeof(long), GFP_KERNEL);
2149 if (!strmap) {
2150 err = -ENOMEM;
2151 goto free_mod;
2152 }
2153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 if (find_module(mod->name)) {
2155 err = -EEXIST;
2156 goto free_mod;
2157 }
2158
2159 mod->state = MODULE_STATE_COMING;
2160
2161 /* Allow arches to frob section contents and sizes. */
2162 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
2163 if (err < 0)
2164 goto free_mod;
2165
2166 if (pcpuindex) {
2167 /* We have a special allocation for this section. */
Tejun Heo259354d2010-03-10 18:56:10 +09002168 err = percpu_modalloc(mod, sechdrs[pcpuindex].sh_size,
2169 sechdrs[pcpuindex].sh_addralign);
2170 if (err)
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002171 goto free_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 }
2174
2175 /* Determine total sizes, and put offsets in sh_entsize. For now
2176 this is done generically; there doesn't appear to be any
2177 special cases for the architectures. */
2178 layout_sections(mod, hdr, sechdrs, secstrings);
Jan Beulich554bdfe2009-07-06 14:51:44 +01002179 symoffs = layout_symtab(mod, sechdrs, symindex, strindex, hdr,
2180 secstrings, &stroffs, strmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 /* Do the allocs. */
Rusty Russell3a642e92008-07-22 19:24:28 -05002183 ptr = module_alloc_update_bounds(mod->core_size);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002184 /*
2185 * The pointer to this block is stored in the module structure
2186 * which is inside the block. Just mark it as not being a
2187 * leak.
2188 */
2189 kmemleak_not_leak(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 if (!ptr) {
2191 err = -ENOMEM;
2192 goto free_percpu;
2193 }
2194 memset(ptr, 0, mod->core_size);
2195 mod->module_core = ptr;
2196
Rusty Russell3a642e92008-07-22 19:24:28 -05002197 ptr = module_alloc_update_bounds(mod->init_size);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002198 /*
2199 * The pointer to this block is stored in the module structure
2200 * which is inside the block. This block doesn't need to be
2201 * scanned as it contains data and code that will be freed
2202 * after the module is initialized.
2203 */
2204 kmemleak_ignore(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 if (!ptr && mod->init_size) {
2206 err = -ENOMEM;
2207 goto free_core;
2208 }
2209 memset(ptr, 0, mod->init_size);
2210 mod->module_init = ptr;
2211
2212 /* Transfer each section which specifies SHF_ALLOC */
2213 DEBUGP("final section addresses:\n");
2214 for (i = 0; i < hdr->e_shnum; i++) {
2215 void *dest;
2216
2217 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2218 continue;
2219
2220 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2221 dest = mod->module_init
2222 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2223 else
2224 dest = mod->module_core + sechdrs[i].sh_entsize;
2225
2226 if (sechdrs[i].sh_type != SHT_NOBITS)
2227 memcpy(dest, (void *)sechdrs[i].sh_addr,
2228 sechdrs[i].sh_size);
2229 /* Update sh_addr to point to copy in image. */
2230 sechdrs[i].sh_addr = (unsigned long)dest;
2231 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2232 }
2233 /* Module has been moved. */
2234 mod = (void *)sechdrs[modindex].sh_addr;
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002235 kmemleak_load_module(mod, hdr, sechdrs, secstrings);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Christoph Lametere1783a22010-01-05 15:34:50 +09002237#if defined(CONFIG_MODULE_UNLOAD)
2238 mod->refptr = alloc_percpu(struct module_ref);
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002239 if (!mod->refptr) {
2240 err = -ENOMEM;
2241 goto free_init;
2242 }
2243#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 /* Now we've moved module, initialize linked lists, etc. */
2245 module_unload_init(mod);
2246
Kay Sievers97c146ef2007-11-29 23:46:11 +01002247 /* add kobject, so we can reference it. */
Akinobu Mitad58ae672007-10-16 23:30:27 -07002248 err = mod_sysfs_init(mod);
2249 if (err)
Kay Sievers97c146ef2007-11-29 23:46:11 +01002250 goto free_unload;
Kay Sievers270a6c42007-01-18 13:26:15 +01002251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 /* Set up license info based on the info section */
2253 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2254
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002255 /*
2256 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2257 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2258 * using GPL-only symbols it needs.
2259 */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002260 if (strcmp(mod->name, "ndiswrapper") == 0)
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002261 add_taint(TAINT_PROPRIETARY_MODULE);
2262
2263 /* driverloader was caught wrongly pretending to be under GPL */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002264 if (strcmp(mod->name, "driverloader") == 0)
2265 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Dave Jones9841d612006-01-08 01:03:41 -08002266
Matt Domschc988d2b2005-06-23 22:05:15 -07002267 /* Set up MODINFO_ATTR fields */
2268 setup_modinfo(mod, sechdrs, infoindex);
Matt Domschc988d2b2005-06-23 22:05:15 -07002269
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 /* Fix up syms, so that st_value is a pointer to location. */
2271 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2272 mod);
2273 if (err < 0)
2274 goto cleanup;
2275
Rusty Russell5e458cc2008-10-22 10:00:13 -05002276 /* Now we've got everything in the final locations, we can
2277 * find optional sections. */
Rusty Russelle180a6b2009-03-31 13:05:29 -06002278 mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
2279 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell5e458cc2008-10-22 10:00:13 -05002280 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2281 sizeof(*mod->syms), &mod->num_syms);
2282 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2283 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2284 sizeof(*mod->gpl_syms),
2285 &mod->num_gpl_syms);
2286 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2287 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2288 "__ksymtab_gpl_future",
2289 sizeof(*mod->gpl_future_syms),
2290 &mod->num_gpl_future_syms);
2291 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2292 "__kcrctab_gpl_future");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002294#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002295 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2296 "__ksymtab_unused",
2297 sizeof(*mod->unused_syms),
2298 &mod->num_unused_syms);
2299 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2300 "__kcrctab_unused");
2301 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2302 "__ksymtab_unused_gpl",
2303 sizeof(*mod->unused_gpl_syms),
2304 &mod->num_unused_gpl_syms);
2305 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2306 "__kcrctab_unused_gpl");
2307#endif
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002308#ifdef CONFIG_CONSTRUCTORS
2309 mod->ctors = section_objs(hdr, sechdrs, secstrings, ".ctors",
2310 sizeof(*mod->ctors), &mod->num_ctors);
2311#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002312
Rusty Russell5e458cc2008-10-22 10:00:13 -05002313#ifdef CONFIG_TRACEPOINTS
2314 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2315 "__tracepoints",
2316 sizeof(*mod->tracepoints),
2317 &mod->num_tracepoints);
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002318#endif
Steven Rostedt6d723732009-04-10 14:53:50 -04002319#ifdef CONFIG_EVENT_TRACING
2320 mod->trace_events = section_objs(hdr, sechdrs, secstrings,
2321 "_ftrace_events",
2322 sizeof(*mod->trace_events),
2323 &mod->num_trace_events);
Catalin Marinasa6f5aa1e2009-10-28 13:33:10 +00002324 /*
2325 * This section contains pointers to allocated objects in the trace
2326 * code and not scanning it leads to false positives.
2327 */
2328 kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2329 mod->num_trace_events, GFP_KERNEL);
Steven Rostedt6d723732009-04-10 14:53:50 -04002330#endif
Steven Rostedt93eb6772009-04-15 13:24:06 -04002331#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2332 /* sechdrs[0].sh_size is always zero */
2333 mod->ftrace_callsites = section_objs(hdr, sechdrs, secstrings,
2334 "__mcount_loc",
2335 sizeof(*mod->ftrace_callsites),
2336 &mod->num_ftrace_callsites);
2337#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338#ifdef CONFIG_MODVERSIONS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002339 if ((mod->num_syms && !mod->crcs)
2340 || (mod->num_gpl_syms && !mod->gpl_crcs)
2341 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002342#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002343 || (mod->num_unused_syms && !mod->unused_crcs)
2344 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002345#endif
2346 ) {
Rusty Russellc6e665c2009-03-31 13:05:33 -06002347 err = try_to_force_load(mod,
2348 "no versions for exported symbols");
Linus Torvalds826e4502008-05-04 17:04:16 -07002349 if (err)
2350 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 }
2352#endif
2353
2354 /* Now do relocations. */
2355 for (i = 1; i < hdr->e_shnum; i++) {
2356 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2357 unsigned int info = sechdrs[i].sh_info;
2358
2359 /* Not a valid relocation section? */
2360 if (info >= hdr->e_shnum)
2361 continue;
2362
2363 /* Don't bother with non-allocated sections */
2364 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2365 continue;
2366
2367 if (sechdrs[i].sh_type == SHT_REL)
2368 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2369 else if (sechdrs[i].sh_type == SHT_RELA)
2370 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2371 mod);
2372 if (err < 0)
2373 goto cleanup;
2374 }
2375
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002376 /* Find duplicate symbols */
2377 err = verify_export_symbols(mod);
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002378 if (err < 0)
2379 goto cleanup;
2380
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 /* Set up and sort exception table */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002382 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2383 sizeof(*mod->extable), &mod->num_exentries);
2384 sort_extable(mod->extable, mod->extable + mod->num_exentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
2386 /* Finally, copy percpu area over. */
Tejun Heo259354d2010-03-10 18:56:10 +09002387 percpu_modcopy(mod, (void *)sechdrs[pcpuindex].sh_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 sechdrs[pcpuindex].sh_size);
2389
Jan Beulich4a496222009-07-06 14:50:42 +01002390 add_kallsyms(mod, sechdrs, hdr->e_shnum, symindex, strindex,
Jan Beulich554bdfe2009-07-06 14:51:44 +01002391 symoffs, stroffs, secstrings, strmap);
2392 kfree(strmap);
2393 strmap = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002395 if (!mod->taints) {
Jason Barone9d376f2009-02-05 11:51:38 -05002396 struct _ddebug *debug;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002397 unsigned int num_debug;
2398
Rusty Russell5e458cc2008-10-22 10:00:13 -05002399 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2400 sizeof(*debug), &num_debug);
Jason Barone9d376f2009-02-05 11:51:38 -05002401 if (debug)
2402 dynamic_debug_setup(debug, num_debug);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002403 }
Steven Rostedt90d595f2008-08-14 15:45:09 -04002404
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 err = module_finalize(hdr, sechdrs, mod);
2406 if (err < 0)
2407 goto cleanup;
2408
Thomas Koeller378bac82005-09-06 15:17:11 -07002409 /* flush the icache in correct context */
2410 old_fs = get_fs();
2411 set_fs(KERNEL_DS);
2412
2413 /*
2414 * Flush the instruction cache, since we've played with text.
2415 * Do it before processing of module parameters, so the module
2416 * can provide parameter accessor functions of its own.
2417 */
2418 if (mod->module_init)
2419 flush_icache_range((unsigned long)mod->module_init,
2420 (unsigned long)mod->module_init
2421 + mod->init_size);
2422 flush_icache_range((unsigned long)mod->module_core,
2423 (unsigned long)mod->module_core + mod->core_size);
2424
2425 set_fs(old_fs);
2426
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 mod->args = args;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002428 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002429 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2430 mod->name);
2431
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002432 /* Now sew it into the lists so we can get lockdep and oops
Andi Kleend72b3752008-08-30 10:09:00 +02002433 * info during argument parsing. Noone should access us, since
2434 * strong_try_module_get() will fail.
2435 * lockdep/oops can run asynchronous, so use the RCU list insertion
2436 * function to insert in a way safe to concurrent readers.
2437 * The mutex protects against concurrent writers.
2438 */
2439 list_add_rcu(&mod->list, &modules);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002440
Rusty Russelle180a6b2009-03-31 13:05:29 -06002441 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002443 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Rusty Russelle180a6b2009-03-31 13:05:29 -06002445 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002447 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Roland McGrath6d760132007-10-16 23:26:40 -07002449 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
2451 /* Get rid of temporary copy */
2452 vfree(hdr);
2453
Li Zefan7ead8b82009-08-17 16:56:28 +08002454 trace_module_load(mod);
2455
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 /* Done! */
2457 return mod;
2458
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002459 unlink:
Rusty Russelle91defa2009-03-31 13:05:35 -06002460 /* Unlink carefully: kallsyms could be walking list. */
2461 list_del_rcu(&mod->list);
2462 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 module_arch_cleanup(mod);
2464 cleanup:
Rusty Russella263f7762009-09-25 00:32:58 -06002465 free_modinfo(mod);
Kay Sievers97c146ef2007-11-29 23:46:11 +01002466 kobject_del(&mod->mkobj.kobj);
2467 kobject_put(&mod->mkobj.kobj);
2468 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 module_unload_free(mod);
Christoph Lametere1783a22010-01-05 15:34:50 +09002470#if defined(CONFIG_MODULE_UNLOAD)
2471 free_percpu(mod->refptr);
Rusty Russellffa9f122009-09-25 00:32:59 -06002472 free_init:
Eric Dumazet720eba32009-02-03 13:31:36 +10302473#endif
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002474 module_free(mod, mod->module_init);
2475 free_core:
2476 module_free(mod, mod->module_core);
2477 /* mod will be freed with core. Don't access it beyond this line! */
2478 free_percpu:
Tejun Heo259354d2010-03-10 18:56:10 +09002479 percpu_modfree(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 free_mod:
2481 kfree(args);
Jan Beulich554bdfe2009-07-06 14:51:44 +01002482 kfree(strmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 free_hdr:
2484 vfree(hdr);
Jayachandran C6fe2e702006-01-06 00:19:54 -08002485 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
2487 truncated:
2488 printk(KERN_ERR "Module len %lu truncated\n", len);
2489 err = -ENOEXEC;
2490 goto free_hdr;
2491}
2492
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002493/* Call module constructors. */
2494static void do_mod_ctors(struct module *mod)
2495{
2496#ifdef CONFIG_CONSTRUCTORS
2497 unsigned long i;
2498
2499 for (i = 0; i < mod->num_ctors; i++)
2500 mod->ctors[i]();
2501#endif
2502}
2503
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504/* This is where the real work happens */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002505SYSCALL_DEFINE3(init_module, void __user *, umod,
2506 unsigned long, len, const char __user *, uargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
2508 struct module *mod;
2509 int ret = 0;
2510
2511 /* Must have permission */
Kees Cook3d433212009-04-02 15:49:29 -07002512 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 return -EPERM;
2514
2515 /* Only one module load at a time, please */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002516 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 return -EINTR;
2518
2519 /* Do all the hard work */
2520 mod = load_module(umod, len, uargs);
2521 if (IS_ERR(mod)) {
Ashutosh Naik6389a382006-03-23 03:00:46 -08002522 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 return PTR_ERR(mod);
2524 }
2525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 /* Drop lock so they can recurse */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002527 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Alan Sterne041c682006-03-27 01:16:30 -08002529 blocking_notifier_call_chain(&module_notify_list,
2530 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002532 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 /* Start the module */
2534 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002535 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 if (ret < 0) {
2537 /* Init routine failed: abort. Try to protect us from
2538 buggy refcounters. */
2539 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002540 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002541 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002542 blocking_notifier_call_chain(&module_notify_list,
2543 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002544 mutex_lock(&module_mutex);
2545 free_module(mod);
2546 mutex_unlock(&module_mutex);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002547 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 return ret;
2549 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002550 if (ret > 0) {
Joe Perchesad361c92009-07-06 13:05:40 -07002551 printk(KERN_WARNING
2552"%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
2553"%s: loading module anyway...\n",
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002554 __func__, mod->name, ret,
2555 __func__);
2556 dump_stack();
2557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Rusty Russell6c5db222008-03-10 11:43:52 -07002559 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002561 wake_up(&module_wq);
Masami Hiramatsu0deddf42009-01-06 14:41:54 -08002562 blocking_notifier_call_chain(&module_notify_list,
2563 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07002564
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07002565 /* We need to finish all async code before the module init sequence is done */
2566 async_synchronize_full();
2567
Rusty Russell6c5db222008-03-10 11:43:52 -07002568 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 /* Drop initial reference. */
2570 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06002571 trim_init_extable(mod);
Jan Beulich4a496222009-07-06 14:50:42 +01002572#ifdef CONFIG_KALLSYMS
2573 mod->num_symtab = mod->core_num_syms;
2574 mod->symtab = mod->core_symtab;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002575 mod->strtab = mod->core_strtab;
Jan Beulich4a496222009-07-06 14:50:42 +01002576#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 module_free(mod, mod->module_init);
2578 mod->module_init = NULL;
2579 mod->init_size = 0;
2580 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002581 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
2583 return 0;
2584}
2585
2586static inline int within(unsigned long addr, void *start, unsigned long size)
2587{
2588 return ((void *)addr >= start && (void *)addr < start + size);
2589}
2590
2591#ifdef CONFIG_KALLSYMS
2592/*
2593 * This ignores the intensely annoying "mapping symbols" found
2594 * in ARM ELF files: $a, $t and $d.
2595 */
2596static inline int is_arm_mapping_symbol(const char *str)
2597{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002598 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 && (str[2] == '\0' || str[2] == '.');
2600}
2601
2602static const char *get_ksymbol(struct module *mod,
2603 unsigned long addr,
2604 unsigned long *size,
2605 unsigned long *offset)
2606{
2607 unsigned int i, best = 0;
2608 unsigned long nextval;
2609
2610 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002611 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002613 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2615
2616 /* Scan for closest preceeding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002617 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 for (i = 1; i < mod->num_symtab; i++) {
2619 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2620 continue;
2621
2622 /* We ignore unnamed symbols: they're uninformative
2623 * and inserted at a whim. */
2624 if (mod->symtab[i].st_value <= addr
2625 && mod->symtab[i].st_value > mod->symtab[best].st_value
2626 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2627 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2628 best = i;
2629 if (mod->symtab[i].st_value > addr
2630 && mod->symtab[i].st_value < nextval
2631 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2632 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2633 nextval = mod->symtab[i].st_value;
2634 }
2635
2636 if (!best)
2637 return NULL;
2638
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002639 if (size)
2640 *size = nextval - mod->symtab[best].st_value;
2641 if (offset)
2642 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 return mod->strtab + mod->symtab[best].st_name;
2644}
2645
Rusty Russell6dd06c92008-01-29 17:13:22 -05002646/* For kallsyms to ask for address resolution. NULL means not found. Careful
2647 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002648const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05002649 unsigned long *size,
2650 unsigned long *offset,
2651 char **modname,
2652 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653{
2654 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002655 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
Rusty Russellcb2a5202008-01-14 00:55:03 -08002657 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002658 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002659 if (within_module_init(addr, mod) ||
2660 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002661 if (modname)
2662 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002663 ret = get_ksymbol(mod, addr, size, offset);
2664 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 }
2666 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05002667 /* Make a copy in here where it's safe */
2668 if (ret) {
2669 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2670 ret = namebuf;
2671 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002672 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002673 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674}
2675
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002676int lookup_module_symbol_name(unsigned long addr, char *symname)
2677{
2678 struct module *mod;
2679
Rusty Russellcb2a5202008-01-14 00:55:03 -08002680 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002681 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002682 if (within_module_init(addr, mod) ||
2683 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002684 const char *sym;
2685
2686 sym = get_ksymbol(mod, addr, NULL, NULL);
2687 if (!sym)
2688 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07002689 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002690 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002691 return 0;
2692 }
2693 }
2694out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002695 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002696 return -ERANGE;
2697}
2698
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002699int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2700 unsigned long *offset, char *modname, char *name)
2701{
2702 struct module *mod;
2703
Rusty Russellcb2a5202008-01-14 00:55:03 -08002704 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002705 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002706 if (within_module_init(addr, mod) ||
2707 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002708 const char *sym;
2709
2710 sym = get_ksymbol(mod, addr, size, offset);
2711 if (!sym)
2712 goto out;
2713 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07002714 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002715 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07002716 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002717 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002718 return 0;
2719 }
2720 }
2721out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002722 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002723 return -ERANGE;
2724}
2725
Alexey Dobriyanea078902007-05-08 00:28:39 -07002726int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2727 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728{
2729 struct module *mod;
2730
Rusty Russellcb2a5202008-01-14 00:55:03 -08002731 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002732 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 if (symnum < mod->num_symtab) {
2734 *value = mod->symtab[symnum].st_value;
2735 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002736 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07002737 KSYM_NAME_LEN);
2738 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06002739 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002740 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002741 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 }
2743 symnum -= mod->num_symtab;
2744 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002745 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002746 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747}
2748
2749static unsigned long mod_find_symname(struct module *mod, const char *name)
2750{
2751 unsigned int i;
2752
2753 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002754 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2755 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 return mod->symtab[i].st_value;
2757 return 0;
2758}
2759
2760/* Look for this name: can be of form module:name. */
2761unsigned long module_kallsyms_lookup_name(const char *name)
2762{
2763 struct module *mod;
2764 char *colon;
2765 unsigned long ret = 0;
2766
2767 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08002768 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 if ((colon = strchr(name, ':')) != NULL) {
2770 *colon = '\0';
2771 if ((mod = find_module(name)) != NULL)
2772 ret = mod_find_symname(mod, colon+1);
2773 *colon = ':';
2774 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02002775 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 if ((ret = mod_find_symname(mod, name)) != 0)
2777 break;
2778 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002779 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 return ret;
2781}
Anders Kaseorg75a66612008-12-05 19:03:58 -05002782
2783int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
2784 struct module *, unsigned long),
2785 void *data)
2786{
2787 struct module *mod;
2788 unsigned int i;
2789 int ret;
2790
2791 list_for_each_entry(mod, &modules, list) {
2792 for (i = 0; i < mod->num_symtab; i++) {
2793 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
2794 mod, mod->symtab[i].st_value);
2795 if (ret != 0)
2796 return ret;
2797 }
2798 }
2799 return 0;
2800}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801#endif /* CONFIG_KALLSYMS */
2802
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002803static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002804{
2805 int bx = 0;
2806
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002807 if (mod->taints ||
2808 mod->state == MODULE_STATE_GOING ||
2809 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002810 buf[bx++] = '(';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002811 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002812 buf[bx++] = 'P';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002813 if (mod->taints & (1 << TAINT_FORCED_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002814 buf[bx++] = 'F';
Linus Torvalds26e9a392008-10-17 09:50:12 -07002815 if (mod->taints & (1 << TAINT_CRAP))
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002816 buf[bx++] = 'C';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002817 /*
2818 * TAINT_FORCED_RMMOD: could be added.
2819 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2820 * apply to modules.
2821 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002822
2823 /* Show a - for module-is-being-unloaded */
2824 if (mod->state == MODULE_STATE_GOING)
2825 buf[bx++] = '-';
2826 /* Show a + for module-is-being-loaded */
2827 if (mod->state == MODULE_STATE_COMING)
2828 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002829 buf[bx++] = ')';
2830 }
2831 buf[bx] = '\0';
2832
2833 return buf;
2834}
2835
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002836#ifdef CONFIG_PROC_FS
2837/* Called by the /proc file system to return a list of modules. */
2838static void *m_start(struct seq_file *m, loff_t *pos)
2839{
2840 mutex_lock(&module_mutex);
2841 return seq_list_start(&modules, *pos);
2842}
2843
2844static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2845{
2846 return seq_list_next(p, &modules, pos);
2847}
2848
2849static void m_stop(struct seq_file *m, void *p)
2850{
2851 mutex_unlock(&module_mutex);
2852}
2853
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854static int m_show(struct seq_file *m, void *p)
2855{
2856 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002857 char buf[8];
2858
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05002859 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 mod->name, mod->init_size + mod->core_size);
2861 print_unload_info(m, mod);
2862
2863 /* Informative for users. */
2864 seq_printf(m, " %s",
2865 mod->state == MODULE_STATE_GOING ? "Unloading":
2866 mod->state == MODULE_STATE_COMING ? "Loading":
2867 "Live");
2868 /* Used by oprofile and other similar tools. */
2869 seq_printf(m, " 0x%p", mod->module_core);
2870
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002871 /* Taints info */
2872 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002873 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002874
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 seq_printf(m, "\n");
2876 return 0;
2877}
2878
2879/* Format: modulename size refcount deps address
2880
2881 Where refcount is a number or -, and deps is a comma-separated list
2882 of depends or -.
2883*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002884static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 .start = m_start,
2886 .next = m_next,
2887 .stop = m_stop,
2888 .show = m_show
2889};
2890
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002891static int modules_open(struct inode *inode, struct file *file)
2892{
2893 return seq_open(file, &modules_op);
2894}
2895
2896static const struct file_operations proc_modules_operations = {
2897 .open = modules_open,
2898 .read = seq_read,
2899 .llseek = seq_lseek,
2900 .release = seq_release,
2901};
2902
2903static int __init proc_modules_init(void)
2904{
2905 proc_create("modules", 0, NULL, &proc_modules_operations);
2906 return 0;
2907}
2908module_init(proc_modules_init);
2909#endif
2910
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911/* Given an address, look for it in the module exception tables. */
2912const struct exception_table_entry *search_module_extables(unsigned long addr)
2913{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 const struct exception_table_entry *e = NULL;
2915 struct module *mod;
2916
Rusty Russell24da1cb2007-07-15 23:41:46 -07002917 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002918 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 if (mod->num_exentries == 0)
2920 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002921
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 e = search_extable(mod->extable,
2923 mod->extable + mod->num_exentries - 1,
2924 addr);
2925 if (e)
2926 break;
2927 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07002928 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
2930 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07002931 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 return e;
2933}
2934
Ingo Molnar4d435f92006-07-03 00:24:24 -07002935/*
Rusty Russelle6104992009-03-31 13:05:31 -06002936 * is_module_address - is this address inside a module?
2937 * @addr: the address to check.
2938 *
2939 * See is_module_text_address() if you simply want to see if the address
2940 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07002941 */
Rusty Russelle6104992009-03-31 13:05:31 -06002942bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07002943{
Rusty Russelle6104992009-03-31 13:05:31 -06002944 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07002945
Rusty Russell24da1cb2007-07-15 23:41:46 -07002946 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06002947 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07002948 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002949
Rusty Russelle6104992009-03-31 13:05:31 -06002950 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07002951}
2952
Rusty Russelle6104992009-03-31 13:05:31 -06002953/*
2954 * __module_address - get the module which contains an address.
2955 * @addr: the address.
2956 *
2957 * Must be called with preempt disabled or module mutex held so that
2958 * module doesn't get freed during this.
2959 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07002960struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961{
2962 struct module *mod;
2963
Rusty Russell3a642e92008-07-22 19:24:28 -05002964 if (addr < module_addr_min || addr > module_addr_max)
2965 return NULL;
2966
Andi Kleend72b3752008-08-30 10:09:00 +02002967 list_for_each_entry_rcu(mod, &modules, list)
Rusty Russelle6104992009-03-31 13:05:31 -06002968 if (within_module_core(addr, mod)
2969 || within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 return mod;
2971 return NULL;
2972}
Tim Abbottc6b37802008-12-05 19:03:59 -05002973EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Rusty Russelle6104992009-03-31 13:05:31 -06002975/*
2976 * is_module_text_address - is this address inside module code?
2977 * @addr: the address to check.
2978 *
2979 * See is_module_address() if you simply want to see if the address is
2980 * anywhere in a module. See kernel_text_address() for testing if an
2981 * address corresponds to kernel or module code.
2982 */
2983bool is_module_text_address(unsigned long addr)
2984{
2985 bool ret;
2986
2987 preempt_disable();
2988 ret = __module_text_address(addr) != NULL;
2989 preempt_enable();
2990
2991 return ret;
2992}
2993
2994/*
2995 * __module_text_address - get the module whose code contains an address.
2996 * @addr: the address.
2997 *
2998 * Must be called with preempt disabled or module mutex held so that
2999 * module doesn't get freed during this.
3000 */
3001struct module *__module_text_address(unsigned long addr)
3002{
3003 struct module *mod = __module_address(addr);
3004 if (mod) {
3005 /* Make sure it's within the text section. */
3006 if (!within(addr, mod->module_init, mod->init_text_size)
3007 && !within(addr, mod->module_core, mod->core_text_size))
3008 mod = NULL;
3009 }
3010 return mod;
3011}
Tim Abbottc6b37802008-12-05 19:03:59 -05003012EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06003013
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014/* Don't grab lock, we're oopsing. */
3015void print_modules(void)
3016{
3017 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07003018 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
Linus Torvaldsb2311252009-06-16 11:07:14 -07003020 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02003021 /* Most callers should already have preempt disabled, but make sure */
3022 preempt_disable();
3023 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003024 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02003025 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01003026 if (last_unloaded_module[0])
3027 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 printk("\n");
3029}
3030
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06003032/* Generate the signature for all relevant module structures here.
3033 * If these change, we don't want to try to parse the module. */
3034void module_layout(struct module *mod,
3035 struct modversion_info *ver,
3036 struct kernel_param *kp,
3037 struct kernel_symbol *ks,
Rusty Russell8c8ef422009-03-31 13:05:34 -06003038 struct tracepoint *tp)
3039{
3040}
3041EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07003043
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04003044#ifdef CONFIG_TRACEPOINTS
3045void module_update_tracepoints(void)
3046{
3047 struct module *mod;
3048
3049 mutex_lock(&module_mutex);
3050 list_for_each_entry(mod, &modules, list)
3051 if (!mod->taints)
3052 tracepoint_update_probe_range(mod->tracepoints,
3053 mod->tracepoints + mod->num_tracepoints);
3054 mutex_unlock(&module_mutex);
3055}
3056
3057/*
3058 * Returns 0 if current not found.
3059 * Returns 1 if current found.
3060 */
3061int module_get_iter_tracepoints(struct tracepoint_iter *iter)
3062{
3063 struct module *iter_mod;
3064 int found = 0;
3065
3066 mutex_lock(&module_mutex);
3067 list_for_each_entry(iter_mod, &modules, list) {
3068 if (!iter_mod->taints) {
3069 /*
3070 * Sorted module list
3071 */
3072 if (iter_mod < iter->module)
3073 continue;
3074 else if (iter_mod > iter->module)
3075 iter->tracepoint = NULL;
3076 found = tracepoint_get_iter_range(&iter->tracepoint,
3077 iter_mod->tracepoints,
3078 iter_mod->tracepoints
3079 + iter_mod->num_tracepoints);
3080 if (found) {
3081 iter->module = iter_mod;
3082 break;
3083 }
3084 }
3085 }
3086 mutex_unlock(&module_mutex);
3087 return found;
3088}
3089#endif