blob: 29dd232f818314fc3e278aecc86482a1536a95a5 [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 Russell75676502010-06-05 11:17:36 -060075/*
76 * Mutex protects:
77 * 1) List of modules (also safely readable with preempt_disable),
78 * 2) module_use links,
79 * 3) module_addr_min/module_addr_max.
Andi Kleend72b3752008-08-30 10:09:00 +020080 * (delete uses stop_machine/add uses RCU list operations). */
Tim Abbottc6b37802008-12-05 19:03:59 -050081DEFINE_MUTEX(module_mutex);
82EXPORT_SYMBOL_GPL(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static LIST_HEAD(modules);
Jason Wessel67fc4e02010-05-20 21:04:21 -050084#ifdef CONFIG_KGDB_KDB
85struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
86#endif /* CONFIG_KGDB_KDB */
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Stephen Rothwell19e45292009-04-14 17:27:18 +100089/* Block module loading/unloading? */
90int modules_disabled = 0;
91
Rusty Russellc9a3ba52008-01-29 17:13:18 -050092/* Waiting for a module to finish initializing? */
93static DECLARE_WAIT_QUEUE_HEAD(module_wq);
94
Alan Sterne041c682006-03-27 01:16:30 -080095static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Rusty Russell75676502010-06-05 11:17:36 -060097/* Bounds of module allocation, for speeding __module_address.
98 * Protected by module_mutex. */
Rusty Russell3a642e92008-07-22 19:24:28 -050099static unsigned long module_addr_min = -1UL, module_addr_max = 0;
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101int register_module_notifier(struct notifier_block * nb)
102{
Alan Sterne041c682006-03-27 01:16:30 -0800103 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105EXPORT_SYMBOL(register_module_notifier);
106
107int unregister_module_notifier(struct notifier_block * nb)
108{
Alan Sterne041c682006-03-27 01:16:30 -0800109 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111EXPORT_SYMBOL(unregister_module_notifier);
112
Rusty Russelleded41c2010-08-05 12:59:07 -0600113struct load_info {
114 Elf_Ehdr *hdr;
115 unsigned long len;
116 Elf_Shdr *sechdrs;
117 char *secstrings, *args, *strtab;
Rusty Russelld9131882010-08-05 12:59:08 -0600118 unsigned long *strmap;
119 unsigned long symoffs, stroffs;
Rusty Russelleded41c2010-08-05 12:59:07 -0600120 struct {
121 unsigned int sym, str, mod, vers, info, pcpu;
122 } index;
123};
124
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -0800125/* We require a truly strong try_module_get(): 0 means failure due to
126 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static inline int strong_try_module_get(struct module *mod)
128{
129 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500130 return -EBUSY;
131 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500133 else
134 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700137static inline void add_taint_module(struct module *mod, unsigned flag)
138{
139 add_taint(flag);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700140 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700141}
142
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200143/*
144 * A thread that wants to hold a reference to a module only while it
145 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 */
147void __module_put_and_exit(struct module *mod, long code)
148{
149 module_put(mod);
150 do_exit(code);
151}
152EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/* Find a module section: 0 means not found. */
Rusty Russell49668682010-08-05 12:59:10 -0600155static unsigned int find_sec(const struct load_info *info, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 unsigned int i;
158
Rusty Russell49668682010-08-05 12:59:10 -0600159 for (i = 1; i < info->hdr->e_shnum; i++) {
160 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 /* Alloc bit cleared means "ignore it." */
Rusty Russell49668682010-08-05 12:59:10 -0600162 if ((shdr->sh_flags & SHF_ALLOC)
163 && strcmp(info->secstrings + shdr->sh_name, name) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return i;
Rusty Russell49668682010-08-05 12:59:10 -0600165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return 0;
167}
168
Rusty Russell5e458cc2008-10-22 10:00:13 -0500169/* Find a module section, or NULL. */
Rusty Russell49668682010-08-05 12:59:10 -0600170static void *section_addr(const struct load_info *info, const char *name)
Rusty Russell5e458cc2008-10-22 10:00:13 -0500171{
172 /* Section 0 has sh_addr 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600173 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500174}
175
176/* Find a module section, or NULL. Fill in number of "objects" in section. */
Rusty Russell49668682010-08-05 12:59:10 -0600177static void *section_objs(const struct load_info *info,
Rusty Russell5e458cc2008-10-22 10:00:13 -0500178 const char *name,
179 size_t object_size,
180 unsigned int *num)
181{
Rusty Russell49668682010-08-05 12:59:10 -0600182 unsigned int sec = find_sec(info, name);
Rusty Russell5e458cc2008-10-22 10:00:13 -0500183
184 /* Section 0 has sh_addr 0 and sh_size 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600185 *num = info->sechdrs[sec].sh_size / object_size;
186 return (void *)info->sechdrs[sec].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500187}
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/* Provided by the linker */
190extern const struct kernel_symbol __start___ksymtab[];
191extern const struct kernel_symbol __stop___ksymtab[];
192extern const struct kernel_symbol __start___ksymtab_gpl[];
193extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800194extern const struct kernel_symbol __start___ksymtab_gpl_future[];
195extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196extern const unsigned long __start___kcrctab[];
197extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800198extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500199#ifdef CONFIG_UNUSED_SYMBOLS
200extern const struct kernel_symbol __start___ksymtab_unused[];
201extern const struct kernel_symbol __stop___ksymtab_unused[];
202extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
203extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700204extern const unsigned long __start___kcrctab_unused[];
205extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500206#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208#ifndef CONFIG_MODVERSIONS
209#define symversion(base, idx) NULL
210#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800211#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212#endif
213
Rusty Russelldafd0942008-07-22 19:24:25 -0500214static bool each_symbol_in_section(const struct symsearch *arr,
215 unsigned int arrsize,
216 struct module *owner,
217 bool (*fn)(const struct symsearch *syms,
218 struct module *owner,
219 unsigned int symnum, void *data),
220 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100221{
Rusty Russelldafd0942008-07-22 19:24:25 -0500222 unsigned int i, j;
223
224 for (j = 0; j < arrsize; j++) {
225 for (i = 0; i < arr[j].stop - arr[j].start; i++)
226 if (fn(&arr[j], owner, i, data))
227 return true;
228 }
229
230 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100231}
232
Rusty Russelldafd0942008-07-22 19:24:25 -0500233/* Returns true as soon as fn returns true, otherwise false. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500234bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
235 unsigned int symnum, void *data), void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700236{
Rusty Russelldafd0942008-07-22 19:24:25 -0500237 struct module *mod;
Linus Torvalds44032e62010-08-05 12:59:05 -0600238 static const struct symsearch arr[] = {
Rusty Russelldafd0942008-07-22 19:24:25 -0500239 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
240 NOT_GPL_ONLY, false },
241 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
242 __start___kcrctab_gpl,
243 GPL_ONLY, false },
244 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
245 __start___kcrctab_gpl_future,
246 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500247#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500248 { __start___ksymtab_unused, __stop___ksymtab_unused,
249 __start___kcrctab_unused,
250 NOT_GPL_ONLY, true },
251 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
252 __start___kcrctab_unused_gpl,
253 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500254#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500255 };
256
257 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
258 return true;
259
Andi Kleend72b3752008-08-30 10:09:00 +0200260 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500261 struct symsearch arr[] = {
262 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
263 NOT_GPL_ONLY, false },
264 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
265 mod->gpl_crcs,
266 GPL_ONLY, false },
267 { mod->gpl_future_syms,
268 mod->gpl_future_syms + mod->num_gpl_future_syms,
269 mod->gpl_future_crcs,
270 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500271#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500272 { mod->unused_syms,
273 mod->unused_syms + mod->num_unused_syms,
274 mod->unused_crcs,
275 NOT_GPL_ONLY, true },
276 { mod->unused_gpl_syms,
277 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
278 mod->unused_gpl_crcs,
279 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500280#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500281 };
282
283 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
284 return true;
285 }
286 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700287}
Tim Abbottc6b37802008-12-05 19:03:59 -0500288EXPORT_SYMBOL_GPL(each_symbol);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700289
Rusty Russelldafd0942008-07-22 19:24:25 -0500290struct find_symbol_arg {
291 /* Input */
292 const char *name;
293 bool gplok;
294 bool warn;
295
296 /* Output */
297 struct module *owner;
298 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500299 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500300};
301
302static bool find_symbol_in_section(const struct symsearch *syms,
303 struct module *owner,
304 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500305{
Rusty Russelldafd0942008-07-22 19:24:25 -0500306 struct find_symbol_arg *fsa = data;
307
308 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
309 return false;
310
311 if (!fsa->gplok) {
312 if (syms->licence == GPL_ONLY)
313 return false;
314 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
315 printk(KERN_WARNING "Symbol %s is being used "
316 "by a non-GPL module, which will not "
317 "be allowed in the future\n", fsa->name);
318 printk(KERN_WARNING "Please see the file "
319 "Documentation/feature-removal-schedule.txt "
320 "in the kernel source tree for more details.\n");
321 }
322 }
323
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500324#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500325 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500326 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500327 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500328 printk(KERN_WARNING
329 "This symbol will go away in the future.\n");
330 printk(KERN_WARNING
331 "Please evalute if this is the right api to use and if "
332 "it really is, submit a report the linux kernel "
333 "mailinglist together with submitting your code for "
334 "inclusion.\n");
335 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500336#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500337
338 fsa->owner = owner;
339 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500340 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500341 return true;
342}
343
Tim Abbott414fd312008-12-05 19:03:56 -0500344/* Find a symbol and return it, along with, (optional) crc and
Rusty Russell75676502010-06-05 11:17:36 -0600345 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500346const struct kernel_symbol *find_symbol(const char *name,
347 struct module **owner,
348 const unsigned long **crc,
349 bool gplok,
350 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Rusty Russelldafd0942008-07-22 19:24:25 -0500352 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Rusty Russelldafd0942008-07-22 19:24:25 -0500354 fsa.name = name;
355 fsa.gplok = gplok;
356 fsa.warn = warn;
357
358 if (each_symbol(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500359 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500360 *owner = fsa.owner;
361 if (crc)
362 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500363 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 DEBUGP("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500367 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
Tim Abbottc6b37802008-12-05 19:03:59 -0500369EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371/* Search for module by name: must hold module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500372struct module *find_module(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 struct module *mod;
375
376 list_for_each_entry(mod, &modules, list) {
377 if (strcmp(mod->name, name) == 0)
378 return mod;
379 }
380 return NULL;
381}
Tim Abbottc6b37802008-12-05 19:03:59 -0500382EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900385
Tejun Heo259354d2010-03-10 18:56:10 +0900386static inline void __percpu *mod_percpu(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900387{
Tejun Heo259354d2010-03-10 18:56:10 +0900388 return mod->percpu;
389}
Tejun Heofbf59bc2009-02-20 16:29:08 +0900390
Tejun Heo259354d2010-03-10 18:56:10 +0900391static int percpu_modalloc(struct module *mod,
392 unsigned long size, unsigned long align)
393{
Tejun Heofbf59bc2009-02-20 16:29:08 +0900394 if (align > PAGE_SIZE) {
395 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
Tejun Heo259354d2010-03-10 18:56:10 +0900396 mod->name, align, PAGE_SIZE);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900397 align = PAGE_SIZE;
398 }
399
Tejun Heo259354d2010-03-10 18:56:10 +0900400 mod->percpu = __alloc_reserved_percpu(size, align);
401 if (!mod->percpu) {
Tejun Heofbf59bc2009-02-20 16:29:08 +0900402 printk(KERN_WARNING
Rusty Russelld9131882010-08-05 12:59:08 -0600403 "%s: Could not allocate %lu bytes percpu data\n",
404 mod->name, size);
Tejun Heo259354d2010-03-10 18:56:10 +0900405 return -ENOMEM;
406 }
407 mod->percpu_size = size;
408 return 0;
Tejun Heofbf59bc2009-02-20 16:29:08 +0900409}
410
Tejun Heo259354d2010-03-10 18:56:10 +0900411static void percpu_modfree(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900412{
Tejun Heo259354d2010-03-10 18:56:10 +0900413 free_percpu(mod->percpu);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900414}
415
Rusty Russell49668682010-08-05 12:59:10 -0600416static unsigned int find_pcpusec(struct load_info *info)
Tejun Heo6b588c12009-02-20 16:29:07 +0900417{
Rusty Russell49668682010-08-05 12:59:10 -0600418 return find_sec(info, ".data..percpu");
Tejun Heo6b588c12009-02-20 16:29:07 +0900419}
420
Tejun Heo259354d2010-03-10 18:56:10 +0900421static void percpu_modcopy(struct module *mod,
422 const void *from, unsigned long size)
Tejun Heo6b588c12009-02-20 16:29:07 +0900423{
424 int cpu;
425
426 for_each_possible_cpu(cpu)
Tejun Heo259354d2010-03-10 18:56:10 +0900427 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
Tejun Heo6b588c12009-02-20 16:29:07 +0900428}
429
Tejun Heo10fad5e2010-03-10 18:57:54 +0900430/**
431 * is_module_percpu_address - test whether address is from module static percpu
432 * @addr: address to test
433 *
434 * Test whether @addr belongs to module static percpu area.
435 *
436 * RETURNS:
437 * %true if @addr is from module static percpu area
438 */
439bool is_module_percpu_address(unsigned long addr)
440{
441 struct module *mod;
442 unsigned int cpu;
443
444 preempt_disable();
445
446 list_for_each_entry_rcu(mod, &modules, list) {
447 if (!mod->percpu_size)
448 continue;
449 for_each_possible_cpu(cpu) {
450 void *start = per_cpu_ptr(mod->percpu, cpu);
451
452 if ((void *)addr >= start &&
453 (void *)addr < start + mod->percpu_size) {
454 preempt_enable();
455 return true;
456 }
457 }
458 }
459
460 preempt_enable();
461 return false;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700462}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900465
Tejun Heo259354d2010-03-10 18:56:10 +0900466static inline void __percpu *mod_percpu(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 return NULL;
469}
Tejun Heo259354d2010-03-10 18:56:10 +0900470static inline int percpu_modalloc(struct module *mod,
471 unsigned long size, unsigned long align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Tejun Heo259354d2010-03-10 18:56:10 +0900473 return -ENOMEM;
474}
475static inline void percpu_modfree(struct module *mod)
476{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
Rusty Russell49668682010-08-05 12:59:10 -0600478static unsigned int find_pcpusec(struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480 return 0;
481}
Tejun Heo259354d2010-03-10 18:56:10 +0900482static inline void percpu_modcopy(struct module *mod,
483 const void *from, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 /* pcpusec should be 0, and size of that section should be 0. */
486 BUG_ON(size != 0);
487}
Tejun Heo10fad5e2010-03-10 18:57:54 +0900488bool is_module_percpu_address(unsigned long addr)
489{
490 return false;
491}
Tejun Heo6b588c12009-02-20 16:29:07 +0900492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493#endif /* CONFIG_SMP */
494
Matt Domschc988d2b2005-06-23 22:05:15 -0700495#define MODINFO_ATTR(field) \
496static void setup_modinfo_##field(struct module *mod, const char *s) \
497{ \
498 mod->field = kstrdup(s, GFP_KERNEL); \
499} \
500static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
501 struct module *mod, char *buffer) \
502{ \
503 return sprintf(buffer, "%s\n", mod->field); \
504} \
505static int modinfo_##field##_exists(struct module *mod) \
506{ \
507 return mod->field != NULL; \
508} \
509static void free_modinfo_##field(struct module *mod) \
510{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700511 kfree(mod->field); \
512 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700513} \
514static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900515 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700516 .show = show_modinfo_##field, \
517 .setup = setup_modinfo_##field, \
518 .test = modinfo_##field##_exists, \
519 .free = free_modinfo_##field, \
520};
521
522MODINFO_ATTR(version);
523MODINFO_ATTR(srcversion);
524
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100525static char last_unloaded_module[MODULE_NAME_LEN+1];
526
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800527#ifdef CONFIG_MODULE_UNLOAD
Steven Rostedteb0c5372010-03-29 14:25:18 -0400528
529EXPORT_TRACEPOINT_SYMBOL(module_get);
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531/* Init the unload section of the module. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600532static int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600534 mod->refptr = alloc_percpu(struct module_ref);
535 if (!mod->refptr)
536 return -ENOMEM;
537
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700538 INIT_LIST_HEAD(&mod->source_list);
539 INIT_LIST_HEAD(&mod->target_list);
Christoph Lametere1783a22010-01-05 15:34:50 +0900540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 /* Hold reference count during initialization. */
Nick Piggin5fbfb182010-04-01 19:09:40 +1100542 __this_cpu_write(mod->refptr->incs, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 /* Backwards compatibility macros put refcount during init. */
544 mod->waiter = current;
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600545
546 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549/* Does a already use b? */
550static int already_uses(struct module *a, struct module *b)
551{
552 struct module_use *use;
553
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700554 list_for_each_entry(use, &b->source_list, source_list) {
555 if (use->source == a) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 DEBUGP("%s uses %s!\n", a->name, b->name);
557 return 1;
558 }
559 }
560 DEBUGP("%s does not use %s!\n", a->name, b->name);
561 return 0;
562}
563
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700564/*
565 * Module a uses b
566 * - we add 'a' as a "source", 'b' as a "target" of module use
567 * - the module_use is added to the list of 'b' sources (so
568 * 'b' can walk the list to see who sourced them), and of 'a'
569 * targets (so 'a' can see what modules it targets).
570 */
571static int add_module_usage(struct module *a, struct module *b)
572{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700573 struct module_use *use;
574
575 DEBUGP("Allocating new usage for %s.\n", a->name);
576 use = kmalloc(sizeof(*use), GFP_ATOMIC);
577 if (!use) {
578 printk(KERN_WARNING "%s: out of memory loading\n", a->name);
579 return -ENOMEM;
580 }
581
582 use->source = a;
583 use->target = b;
584 list_add(&use->source_list, &b->source_list);
585 list_add(&use->target_list, &a->target_list);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700586 return 0;
587}
588
Rusty Russell75676502010-06-05 11:17:36 -0600589/* Module a uses b: caller needs module_mutex() */
Rusty Russell9bea7f22010-06-05 11:17:37 -0600590int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Rusty Russellc8e21ce2010-06-05 11:17:35 -0600592 int err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100593
Rusty Russell9bea7f22010-06-05 11:17:37 -0600594 if (b == NULL || already_uses(a, b))
Linus Torvalds218ce732010-05-25 16:48:30 -0700595 return 0;
Linus Torvalds218ce732010-05-25 16:48:30 -0700596
Rusty Russell9bea7f22010-06-05 11:17:37 -0600597 /* If module isn't available, we fail. */
598 err = strong_try_module_get(b);
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500599 if (err)
Rusty Russell9bea7f22010-06-05 11:17:37 -0600600 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700602 err = add_module_usage(a, b);
603 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 module_put(b);
Rusty Russell9bea7f22010-06-05 11:17:37 -0600605 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
Rusty Russell9bea7f22010-06-05 11:17:37 -0600607 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600609EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611/* Clear the unload stuff of the module. */
612static void module_unload_free(struct module *mod)
613{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700614 struct module_use *use, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Rusty Russell75676502010-06-05 11:17:36 -0600616 mutex_lock(&module_mutex);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700617 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
618 struct module *i = use->target;
619 DEBUGP("%s unusing %s\n", mod->name, i->name);
620 module_put(i);
621 list_del(&use->source_list);
622 list_del(&use->target_list);
623 kfree(use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
Rusty Russell75676502010-06-05 11:17:36 -0600625 mutex_unlock(&module_mutex);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600626
627 free_percpu(mod->refptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
630#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800631static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 int ret = (flags & O_TRUNC);
634 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800635 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return ret;
637}
638#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800639static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 return 0;
642}
643#endif /* CONFIG_MODULE_FORCE_UNLOAD */
644
645struct stopref
646{
647 struct module *mod;
648 int flags;
649 int *forced;
650};
651
652/* Whole machine is stopped with interrupts off when this runs. */
653static int __try_stop_module(void *_sref)
654{
655 struct stopref *sref = _sref;
656
Rusty Russellda39ba52008-07-22 19:24:25 -0500657 /* If it's not unused, quit unless we're forcing. */
658 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800659 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return -EWOULDBLOCK;
661 }
662
663 /* Mark it as dying. */
664 sref->mod->state = MODULE_STATE_GOING;
665 return 0;
666}
667
668static int try_stop_module(struct module *mod, int flags, int *forced)
669{
Rusty Russellda39ba52008-07-22 19:24:25 -0500670 if (flags & O_NONBLOCK) {
671 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500673 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500674 } else {
675 /* We don't need to stop the machine for this. */
676 mod->state = MODULE_STATE_GOING;
677 synchronize_sched();
678 return 0;
679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
682unsigned int module_refcount(struct module *mod)
683{
Nick Piggin5fbfb182010-04-01 19:09:40 +1100684 unsigned int incs = 0, decs = 0;
Eric Dumazet720eba32009-02-03 13:31:36 +1030685 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Eric Dumazet720eba32009-02-03 13:31:36 +1030687 for_each_possible_cpu(cpu)
Nick Piggin5fbfb182010-04-01 19:09:40 +1100688 decs += per_cpu_ptr(mod->refptr, cpu)->decs;
689 /*
690 * ensure the incs are added up after the decs.
691 * module_put ensures incs are visible before decs with smp_wmb.
692 *
693 * This 2-count scheme avoids the situation where the refcount
694 * for CPU0 is read, then CPU0 increments the module refcount,
695 * then CPU1 drops that refcount, then the refcount for CPU1 is
696 * read. We would record a decrement but not its corresponding
697 * increment so we would see a low count (disaster).
698 *
699 * Rare situation? But module_refcount can be preempted, and we
700 * might be tallying up 4096+ CPUs. So it is not impossible.
701 */
702 smp_rmb();
703 for_each_possible_cpu(cpu)
704 incs += per_cpu_ptr(mod->refptr, cpu)->incs;
705 return incs - decs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707EXPORT_SYMBOL(module_refcount);
708
709/* This exists whether we can unload or not */
710static void free_module(struct module *mod);
711
712static void wait_for_zero_refcount(struct module *mod)
713{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500714 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800715 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 for (;;) {
717 DEBUGP("Looking at refcount...\n");
718 set_current_state(TASK_UNINTERRUPTIBLE);
719 if (module_refcount(mod) == 0)
720 break;
721 schedule();
722 }
723 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800724 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100727SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
728 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
730 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800731 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 int ret, forced = 0;
733
Kees Cook3d433212009-04-02 15:49:29 -0700734 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800735 return -EPERM;
736
737 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
738 return -EFAULT;
739 name[MODULE_NAME_LEN-1] = '\0';
740
Tejun Heo3fc1f1e2010-05-06 18:49:20 +0200741 if (mutex_lock_interruptible(&module_mutex) != 0)
742 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 mod = find_module(name);
745 if (!mod) {
746 ret = -ENOENT;
747 goto out;
748 }
749
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700750 if (!list_empty(&mod->source_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 /* Other modules depend on us: get rid of them first. */
752 ret = -EWOULDBLOCK;
753 goto out;
754 }
755
756 /* Doing init or already dying? */
757 if (mod->state != MODULE_STATE_LIVE) {
758 /* FIXME: if (force), slam module count and wake up
759 waiter --RR */
760 DEBUGP("%s already dying\n", mod->name);
761 ret = -EBUSY;
762 goto out;
763 }
764
765 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700766 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800767 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (!forced) {
769 /* This module can't be removed */
770 ret = -EBUSY;
771 goto out;
772 }
773 }
774
775 /* Set this up before setting mod->state */
776 mod->waiter = current;
777
778 /* Stop the machine so refcounts can't move and disable module. */
779 ret = try_stop_module(mod, flags, &forced);
780 if (ret != 0)
781 goto out;
782
783 /* Never wait if forced. */
784 if (!forced && module_refcount(mod) != 0)
785 wait_for_zero_refcount(mod);
786
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200787 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 /* Final destruction now noone is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200789 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200791 blocking_notifier_call_chain(&module_notify_list,
792 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800793 async_synchronize_full();
Rusty Russell75676502010-06-05 11:17:36 -0600794
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100795 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500796 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Rusty Russell75676502010-06-05 11:17:36 -0600798 free_module(mod);
799 return 0;
800out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800801 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return ret;
803}
804
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800805static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 struct module_use *use;
808 int printed_something = 0;
809
810 seq_printf(m, " %u ", module_refcount(mod));
811
812 /* Always include a trailing , so userspace can differentiate
813 between this and the old multi-field proc format. */
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700814 list_for_each_entry(use, &mod->source_list, source_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 printed_something = 1;
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700816 seq_printf(m, "%s,", use->source->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (mod->init != NULL && mod->exit == NULL) {
820 printed_something = 1;
821 seq_printf(m, "[permanent],");
822 }
823
824 if (!printed_something)
825 seq_printf(m, "-");
826}
827
828void __symbol_put(const char *symbol)
829{
830 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Rusty Russell24da1cb2007-07-15 23:41:46 -0700832 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -0500833 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 BUG();
835 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700836 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838EXPORT_SYMBOL(__symbol_put);
839
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930840/* Note this assumes addr is a function, which it currently always is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841void symbol_put_addr(void *addr)
842{
Trent Piepho5e376612006-05-15 09:44:06 -0700843 struct module *modaddr;
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930844 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930846 if (core_kernel_text(a))
Trent Piepho5e376612006-05-15 09:44:06 -0700847 return;
848
Rusty Russella6e6abd2009-03-31 13:05:31 -0600849 /* module_text_address is safe here: we're supposed to have reference
850 * to module from symbol_get, so it can't go away. */
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930851 modaddr = __module_text_address(a);
Rusty Russella6e6abd2009-03-31 13:05:31 -0600852 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -0700853 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
855EXPORT_SYMBOL_GPL(symbol_put_addr);
856
857static ssize_t show_refcnt(struct module_attribute *mattr,
858 struct module *mod, char *buffer)
859{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400860 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
862
863static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900864 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 .show = show_refcnt,
866};
867
Al Virof6a57032006-10-18 01:47:25 -0400868void module_put(struct module *module)
869{
870 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +0900871 preempt_disable();
Nick Piggin5fbfb182010-04-01 19:09:40 +1100872 smp_wmb(); /* see comment in module_refcount */
873 __this_cpu_inc(module->refptr->decs);
Christoph Lametere1783a22010-01-05 15:34:50 +0900874
Li Zefanae832d12010-03-24 10:57:43 +0800875 trace_module_put(module, _RET_IP_);
Al Virof6a57032006-10-18 01:47:25 -0400876 /* Maybe they're waiting for us to drop reference? */
877 if (unlikely(!module_is_live(module)))
878 wake_up_process(module->waiter);
Christoph Lametere1783a22010-01-05 15:34:50 +0900879 preempt_enable();
Al Virof6a57032006-10-18 01:47:25 -0400880 }
881}
882EXPORT_SYMBOL(module_put);
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800885static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
887 /* We don't know the usage count, or what modules are using. */
888 seq_printf(m, " - -");
889}
890
891static inline void module_unload_free(struct module *mod)
892{
893}
894
Rusty Russell9bea7f22010-06-05 11:17:37 -0600895int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Rusty Russell9bea7f22010-06-05 11:17:37 -0600897 return strong_try_module_get(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600899EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600901static inline int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600903 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905#endif /* CONFIG_MODULE_UNLOAD */
906
Kay Sievers1f717402006-11-24 12:15:25 +0100907static ssize_t show_initstate(struct module_attribute *mattr,
908 struct module *mod, char *buffer)
909{
910 const char *state = "unknown";
911
912 switch (mod->state) {
913 case MODULE_STATE_LIVE:
914 state = "live";
915 break;
916 case MODULE_STATE_COMING:
917 state = "coming";
918 break;
919 case MODULE_STATE_GOING:
920 state = "going";
921 break;
922 }
923 return sprintf(buffer, "%s\n", state);
924}
925
926static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900927 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100928 .show = show_initstate,
929};
930
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800931static struct module_attribute *modinfo_attrs[] = {
932 &modinfo_version,
933 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100934 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800935#ifdef CONFIG_MODULE_UNLOAD
936 &refcnt,
937#endif
938 NULL,
939};
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941static const char vermagic[] = VERMAGIC_STRING;
942
Rusty Russellc6e665c2009-03-31 13:05:33 -0600943static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -0700944{
945#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -0700946 if (!test_taint(TAINT_FORCED_MODULE))
Rusty Russellc6e665c2009-03-31 13:05:33 -0600947 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
948 mod->name, reason);
Linus Torvalds826e4502008-05-04 17:04:16 -0700949 add_taint_module(mod, TAINT_FORCED_MODULE);
950 return 0;
951#else
952 return -ENOEXEC;
953#endif
954}
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956#ifdef CONFIG_MODVERSIONS
Rusty Russelld4703ae2009-12-15 16:28:32 -0600957/* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
958static unsigned long maybe_relocated(unsigned long crc,
959 const struct module *crc_owner)
960{
961#ifdef ARCH_RELOCATES_KCRCTAB
962 if (crc_owner == NULL)
963 return crc - (unsigned long)reloc_start;
964#endif
965 return crc;
966}
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968static int check_version(Elf_Shdr *sechdrs,
969 unsigned int versindex,
970 const char *symname,
971 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -0600972 const unsigned long *crc,
973 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
975 unsigned int i, num_versions;
976 struct modversion_info *versions;
977
978 /* Exporting module didn't supply crcs? OK, we're already tainted. */
979 if (!crc)
980 return 1;
981
Rusty Russella5dd6972008-05-09 16:24:21 +1000982 /* No versions at all? modprobe --force does this. */
983 if (versindex == 0)
984 return try_to_force_load(mod, symname) == 0;
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 versions = (void *) sechdrs[versindex].sh_addr;
987 num_versions = sechdrs[versindex].sh_size
988 / sizeof(struct modversion_info);
989
990 for (i = 0; i < num_versions; i++) {
991 if (strcmp(versions[i].name, symname) != 0)
992 continue;
993
Rusty Russelld4703ae2009-12-15 16:28:32 -0600994 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 DEBUGP("Found checksum %lX vs module %lX\n",
Rusty Russelld4703ae2009-12-15 16:28:32 -0600997 maybe_relocated(*crc, crc_owner), versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -0700998 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001000
Rusty Russella5dd6972008-05-09 16:24:21 +10001001 printk(KERN_WARNING "%s: no symbol version for %s\n",
1002 mod->name, symname);
1003 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001004
1005bad_version:
1006 printk("%s: disagrees about version of symbol %s\n",
1007 mod->name, symname);
1008 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
1011static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1012 unsigned int versindex,
1013 struct module *mod)
1014{
1015 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Rusty Russell75676502010-06-05 11:17:36 -06001017 /* Since this should be found in kernel (which can't be removed),
1018 * no locking is necessary. */
Mike Frysinger6560dc12009-07-23 23:42:08 +09301019 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1020 &crc, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 BUG();
Rusty Russelld4703ae2009-12-15 16:28:32 -06001022 return check_version(sechdrs, versindex, "module_layout", mod, crc,
1023 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
Rusty Russell91e37a72008-05-09 16:25:28 +10001026/* First part is kernel version, which we ignore if module has crcs. */
1027static inline int same_magic(const char *amagic, const char *bmagic,
1028 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Rusty Russell91e37a72008-05-09 16:25:28 +10001030 if (has_crcs) {
1031 amagic += strcspn(amagic, " ");
1032 bmagic += strcspn(bmagic, " ");
1033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return strcmp(amagic, bmagic) == 0;
1035}
1036#else
1037static inline int check_version(Elf_Shdr *sechdrs,
1038 unsigned int versindex,
1039 const char *symname,
1040 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001041 const unsigned long *crc,
1042 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
1044 return 1;
1045}
1046
1047static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1048 unsigned int versindex,
1049 struct module *mod)
1050{
1051 return 1;
1052}
1053
Rusty Russell91e37a72008-05-09 16:25:28 +10001054static inline int same_magic(const char *amagic, const char *bmagic,
1055 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
1057 return strcmp(amagic, bmagic) == 0;
1058}
1059#endif /* CONFIG_MODVERSIONS */
1060
Rusty Russell75676502010-06-05 11:17:36 -06001061/* Resolve a symbol for this module. I.e. if we find one, record usage. */
Rusty Russell49668682010-08-05 12:59:10 -06001062static const struct kernel_symbol *resolve_symbol(struct module *mod,
1063 const struct load_info *info,
Tim Abbott414fd312008-12-05 19:03:56 -05001064 const char *name,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001065 char ownername[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
1067 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001068 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 const unsigned long *crc;
Rusty Russell9bea7f22010-06-05 11:17:37 -06001070 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Rusty Russell75676502010-06-05 11:17:36 -06001072 mutex_lock(&module_mutex);
Tim Abbott414fd312008-12-05 19:03:56 -05001073 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001074 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001075 if (!sym)
1076 goto unlock;
1077
Rusty Russell49668682010-08-05 12:59:10 -06001078 if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
1079 owner)) {
Rusty Russell9bea7f22010-06-05 11:17:37 -06001080 sym = ERR_PTR(-EINVAL);
1081 goto getname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
Rusty Russell9bea7f22010-06-05 11:17:37 -06001083
1084 err = ref_module(mod, owner);
1085 if (err) {
1086 sym = ERR_PTR(err);
1087 goto getname;
1088 }
1089
1090getname:
1091 /* We must make copy under the lock if we failed to get ref. */
1092 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1093unlock:
Rusty Russell75676502010-06-05 11:17:36 -06001094 mutex_unlock(&module_mutex);
Linus Torvalds218ce732010-05-25 16:48:30 -07001095 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
1097
Rusty Russell49668682010-08-05 12:59:10 -06001098static const struct kernel_symbol *
1099resolve_symbol_wait(struct module *mod,
1100 const struct load_info *info,
1101 const char *name)
Rusty Russell9bea7f22010-06-05 11:17:37 -06001102{
1103 const struct kernel_symbol *ksym;
Rusty Russell49668682010-08-05 12:59:10 -06001104 char owner[MODULE_NAME_LEN];
Rusty Russell9bea7f22010-06-05 11:17:37 -06001105
1106 if (wait_event_interruptible_timeout(module_wq,
Rusty Russell49668682010-08-05 12:59:10 -06001107 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1108 || PTR_ERR(ksym) != -EBUSY,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001109 30 * HZ) <= 0) {
1110 printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
Rusty Russell49668682010-08-05 12:59:10 -06001111 mod->name, owner);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001112 }
1113 return ksym;
1114}
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116/*
1117 * /sys/module/foo/sections stuff
1118 * J. Corbet <corbet@lwn.net>
1119 */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001120#ifdef CONFIG_SYSFS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001121
Rusty Russell8f6d0372010-08-05 12:59:09 -06001122#ifdef CONFIG_KALLSYMS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001123static inline bool sect_empty(const Elf_Shdr *sect)
1124{
1125 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1126}
1127
Rusty Russella58730c2008-03-13 09:03:44 +00001128struct module_sect_attr
1129{
1130 struct module_attribute mattr;
1131 char *name;
1132 unsigned long address;
1133};
1134
1135struct module_sect_attrs
1136{
1137 struct attribute_group grp;
1138 unsigned int nsections;
1139 struct module_sect_attr attrs[0];
1140};
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142static ssize_t module_sect_show(struct module_attribute *mattr,
1143 struct module *mod, char *buf)
1144{
1145 struct module_sect_attr *sattr =
1146 container_of(mattr, struct module_sect_attr, mattr);
1147 return sprintf(buf, "0x%lx\n", sattr->address);
1148}
1149
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001150static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1151{
Rusty Russella58730c2008-03-13 09:03:44 +00001152 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001153
1154 for (section = 0; section < sect_attrs->nsections; section++)
1155 kfree(sect_attrs->attrs[section].name);
1156 kfree(sect_attrs);
1157}
1158
Rusty Russell8f6d0372010-08-05 12:59:09 -06001159static void add_sect_attrs(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 unsigned int nloaded = 0, i, size[2];
1162 struct module_sect_attrs *sect_attrs;
1163 struct module_sect_attr *sattr;
1164 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 /* Count loaded sections and allocate structures */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001167 for (i = 0; i < info->hdr->e_shnum; i++)
1168 if (!sect_empty(&info->sechdrs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 nloaded++;
1170 size[0] = ALIGN(sizeof(*sect_attrs)
1171 + nloaded * sizeof(sect_attrs->attrs[0]),
1172 sizeof(sect_attrs->grp.attrs[0]));
1173 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001174 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1175 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return;
1177
1178 /* Setup section attributes. */
1179 sect_attrs->grp.name = "sections";
1180 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1181
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001182 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 sattr = &sect_attrs->attrs[0];
1184 gattr = &sect_attrs->grp.attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001185 for (i = 0; i < info->hdr->e_shnum; i++) {
1186 Elf_Shdr *sec = &info->sechdrs[i];
1187 if (sect_empty(sec))
Helge Deller35dead42009-12-03 00:29:15 +01001188 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001189 sattr->address = sec->sh_addr;
1190 sattr->name = kstrdup(info->secstrings + sec->sh_name,
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001191 GFP_KERNEL);
1192 if (sattr->name == NULL)
1193 goto out;
1194 sect_attrs->nsections++;
Eric W. Biederman361795b2010-02-12 13:41:56 -08001195 sysfs_attr_init(&sattr->mattr.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 sattr->mattr.show = module_sect_show;
1197 sattr->mattr.store = NULL;
1198 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 sattr->mattr.attr.mode = S_IRUGO;
1200 *(gattr++) = &(sattr++)->mattr.attr;
1201 }
1202 *gattr = NULL;
1203
1204 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1205 goto out;
1206
1207 mod->sect_attrs = sect_attrs;
1208 return;
1209 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001210 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
1212
1213static void remove_sect_attrs(struct module *mod)
1214{
1215 if (mod->sect_attrs) {
1216 sysfs_remove_group(&mod->mkobj.kobj,
1217 &mod->sect_attrs->grp);
1218 /* We are positive that no one is using any sect attrs
1219 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001220 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 mod->sect_attrs = NULL;
1222 }
1223}
1224
Roland McGrath6d760132007-10-16 23:26:40 -07001225/*
1226 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1227 */
1228
1229struct module_notes_attrs {
1230 struct kobject *dir;
1231 unsigned int notes;
1232 struct bin_attribute attrs[0];
1233};
1234
Chris Wright2c3c8be2010-05-12 18:28:57 -07001235static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
Roland McGrath6d760132007-10-16 23:26:40 -07001236 struct bin_attribute *bin_attr,
1237 char *buf, loff_t pos, size_t count)
1238{
1239 /*
1240 * The caller checked the pos and count against our size.
1241 */
1242 memcpy(buf, bin_attr->private + pos, count);
1243 return count;
1244}
1245
1246static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1247 unsigned int i)
1248{
1249 if (notes_attrs->dir) {
1250 while (i-- > 0)
1251 sysfs_remove_bin_file(notes_attrs->dir,
1252 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001253 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001254 }
1255 kfree(notes_attrs);
1256}
1257
Rusty Russell8f6d0372010-08-05 12:59:09 -06001258static void add_notes_attrs(struct module *mod, const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001259{
1260 unsigned int notes, loaded, i;
1261 struct module_notes_attrs *notes_attrs;
1262 struct bin_attribute *nattr;
1263
Ingo Molnarea6bff32009-08-28 10:44:56 +02001264 /* failed to create section attributes, so can't create notes */
1265 if (!mod->sect_attrs)
1266 return;
1267
Roland McGrath6d760132007-10-16 23:26:40 -07001268 /* Count notes sections and allocate structures. */
1269 notes = 0;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001270 for (i = 0; i < info->hdr->e_shnum; i++)
1271 if (!sect_empty(&info->sechdrs[i]) &&
1272 (info->sechdrs[i].sh_type == SHT_NOTE))
Roland McGrath6d760132007-10-16 23:26:40 -07001273 ++notes;
1274
1275 if (notes == 0)
1276 return;
1277
1278 notes_attrs = kzalloc(sizeof(*notes_attrs)
1279 + notes * sizeof(notes_attrs->attrs[0]),
1280 GFP_KERNEL);
1281 if (notes_attrs == NULL)
1282 return;
1283
1284 notes_attrs->notes = notes;
1285 nattr = &notes_attrs->attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001286 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1287 if (sect_empty(&info->sechdrs[i]))
Roland McGrath6d760132007-10-16 23:26:40 -07001288 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001289 if (info->sechdrs[i].sh_type == SHT_NOTE) {
Eric W. Biederman361795b2010-02-12 13:41:56 -08001290 sysfs_bin_attr_init(nattr);
Roland McGrath6d760132007-10-16 23:26:40 -07001291 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1292 nattr->attr.mode = S_IRUGO;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001293 nattr->size = info->sechdrs[i].sh_size;
1294 nattr->private = (void *) info->sechdrs[i].sh_addr;
Roland McGrath6d760132007-10-16 23:26:40 -07001295 nattr->read = module_notes_read;
1296 ++nattr;
1297 }
1298 ++loaded;
1299 }
1300
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001301 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001302 if (!notes_attrs->dir)
1303 goto out;
1304
1305 for (i = 0; i < notes; ++i)
1306 if (sysfs_create_bin_file(notes_attrs->dir,
1307 &notes_attrs->attrs[i]))
1308 goto out;
1309
1310 mod->notes_attrs = notes_attrs;
1311 return;
1312
1313 out:
1314 free_notes_attrs(notes_attrs, i);
1315}
1316
1317static void remove_notes_attrs(struct module *mod)
1318{
1319 if (mod->notes_attrs)
1320 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1321}
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001324
Rusty Russell8f6d0372010-08-05 12:59:09 -06001325static inline void add_sect_attrs(struct module *mod,
1326 const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
1328}
1329
1330static inline void remove_sect_attrs(struct module *mod)
1331{
1332}
Roland McGrath6d760132007-10-16 23:26:40 -07001333
Rusty Russell8f6d0372010-08-05 12:59:09 -06001334static inline void add_notes_attrs(struct module *mod,
1335 const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001336{
1337}
1338
1339static inline void remove_notes_attrs(struct module *mod)
1340{
1341}
Rusty Russell8f6d0372010-08-05 12:59:09 -06001342#endif /* CONFIG_KALLSYMS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001344static void add_usage_links(struct module *mod)
1345{
1346#ifdef CONFIG_MODULE_UNLOAD
1347 struct module_use *use;
1348 int nowarn;
1349
Rusty Russell75676502010-06-05 11:17:36 -06001350 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001351 list_for_each_entry(use, &mod->target_list, target_list) {
1352 nowarn = sysfs_create_link(use->target->holders_dir,
1353 &mod->mkobj.kobj, mod->name);
1354 }
Rusty Russell75676502010-06-05 11:17:36 -06001355 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001356#endif
1357}
1358
1359static void del_usage_links(struct module *mod)
1360{
1361#ifdef CONFIG_MODULE_UNLOAD
1362 struct module_use *use;
1363
Rusty Russell75676502010-06-05 11:17:36 -06001364 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001365 list_for_each_entry(use, &mod->target_list, target_list)
1366 sysfs_remove_link(use->target->holders_dir, mod->name);
Rusty Russell75676502010-06-05 11:17:36 -06001367 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001368#endif
1369}
1370
Rusty Russell6407ebb22010-06-05 11:17:36 -06001371static int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001372{
1373 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001374 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001375 int error = 0;
1376 int i;
1377
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001378 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1379 (ARRAY_SIZE(modinfo_attrs) + 1)),
1380 GFP_KERNEL);
1381 if (!mod->modinfo_attrs)
1382 return -ENOMEM;
1383
1384 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001385 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1386 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001387 (attr->test && attr->test(mod))) {
1388 memcpy(temp_attr, attr, sizeof(*temp_attr));
Eric W. Biederman361795b2010-02-12 13:41:56 -08001389 sysfs_attr_init(&temp_attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001390 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1391 ++temp_attr;
1392 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001393 }
1394 return error;
1395}
1396
Rusty Russell6407ebb22010-06-05 11:17:36 -06001397static void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001398{
1399 struct module_attribute *attr;
1400 int i;
1401
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001402 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1403 /* pick a field to test for end of list */
1404 if (!attr->attr.name)
1405 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001406 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001407 if (attr->free)
1408 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001409 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001410 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001411}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Rusty Russell6407ebb22010-06-05 11:17:36 -06001413static int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
1415 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001416 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001418 if (!module_sysfs_initialized) {
1419 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001420 mod->name);
1421 err = -EINVAL;
1422 goto out;
1423 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001424
1425 kobj = kset_find_obj(module_kset, mod->name);
1426 if (kobj) {
1427 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1428 kobject_put(kobj);
1429 err = -EINVAL;
1430 goto out;
1431 }
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001434
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001435 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1436 mod->mkobj.kobj.kset = module_kset;
1437 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1438 "%s", mod->name);
1439 if (err)
1440 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001441
Kay Sievers97c146e2007-11-29 23:46:11 +01001442 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001443out:
1444 return err;
1445}
1446
Rusty Russell6407ebb22010-06-05 11:17:36 -06001447static int mod_sysfs_setup(struct module *mod,
Rusty Russell8f6d0372010-08-05 12:59:09 -06001448 const struct load_info *info,
Kay Sievers270a6c42007-01-18 13:26:15 +01001449 struct kernel_param *kparam,
1450 unsigned int num_params)
1451{
1452 int err;
1453
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001454 err = mod_sysfs_init(mod);
1455 if (err)
1456 goto out;
1457
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001458 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001459 if (!mod->holders_dir) {
1460 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001461 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001462 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 err = module_param_sysfs_setup(mod, kparam, num_params);
1465 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001466 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Matt Domschc988d2b2005-06-23 22:05:15 -07001468 err = module_add_modinfo_attrs(mod);
1469 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001470 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001471
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001472 add_usage_links(mod);
Rusty Russell8f6d0372010-08-05 12:59:09 -06001473 add_sect_attrs(mod, info);
1474 add_notes_attrs(mod, info);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001475
Kay Sieverse17e0f52006-11-24 12:15:25 +01001476 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return 0;
1478
Kay Sieverse17e0f52006-11-24 12:15:25 +01001479out_unreg_param:
1480 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001481out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001482 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001483out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001484 kobject_put(&mod->mkobj.kobj);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001485out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return err;
1487}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001488
1489static void mod_sysfs_fini(struct module *mod)
1490{
Rusty Russell8f6d0372010-08-05 12:59:09 -06001491 remove_notes_attrs(mod);
1492 remove_sect_attrs(mod);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001493 kobject_put(&mod->mkobj.kobj);
1494}
1495
Rusty Russell8f6d0372010-08-05 12:59:09 -06001496#else /* !CONFIG_SYSFS */
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001497
Rusty Russell8f6d0372010-08-05 12:59:09 -06001498static int mod_sysfs_setup(struct module *mod,
1499 const struct load_info *info,
Rusty Russell6407ebb22010-06-05 11:17:36 -06001500 struct kernel_param *kparam,
1501 unsigned int num_params)
1502{
1503 return 0;
1504}
1505
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001506static void mod_sysfs_fini(struct module *mod)
1507{
1508}
1509
Rusty Russell36b03602010-08-05 12:59:09 -06001510static void module_remove_modinfo_attrs(struct module *mod)
1511{
1512}
1513
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001514static void del_usage_links(struct module *mod)
1515{
1516}
1517
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001518#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Rusty Russell36b03602010-08-05 12:59:09 -06001520static void mod_sysfs_teardown(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001522 del_usage_links(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001523 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001525 kobject_put(mod->mkobj.drivers_dir);
1526 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001527 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528}
1529
1530/*
1531 * unlink the module with the whole machine is stopped with interrupts off
1532 * - this defends against kallsyms not taking locks
1533 */
1534static int __unlink_module(void *_mod)
1535{
1536 struct module *mod = _mod;
1537 list_del(&mod->list);
1538 return 0;
1539}
1540
Rusty Russell75676502010-06-05 11:17:36 -06001541/* Free a module, remove from lists, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542static void free_module(struct module *mod)
1543{
Li Zefan7ead8b82009-08-17 16:56:28 +08001544 trace_module_free(mod);
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 /* Delete from various lists */
Rusty Russell75676502010-06-05 11:17:36 -06001547 mutex_lock(&module_mutex);
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001548 stop_machine(__unlink_module, mod, NULL);
Rusty Russell75676502010-06-05 11:17:36 -06001549 mutex_unlock(&module_mutex);
Rusty Russell36b03602010-08-05 12:59:09 -06001550 mod_sysfs_teardown(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Jason Baronb82bab4b2010-07-27 13:18:01 -07001552 /* Remove dynamic debug info */
1553 ddebug_remove_module(mod->name);
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 /* Arch-specific cleanup. */
1556 module_arch_cleanup(mod);
1557
1558 /* Module unload stuff */
1559 module_unload_free(mod);
1560
Rusty Russelle180a6b2009-03-31 13:05:29 -06001561 /* Free any allocated parameters. */
1562 destroy_params(mod->kp, mod->num_kp);
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 /* This may be NULL, but that's OK */
1565 module_free(mod, mod->module_init);
1566 kfree(mod->args);
Tejun Heo259354d2010-03-10 18:56:10 +09001567 percpu_modfree(mod);
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001568
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001569 /* Free lock-classes: */
1570 lockdep_free_key_range(mod->module_core, mod->core_size);
1571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 /* Finally, free the core (containing the module structure) */
1573 module_free(mod, mod->module_core);
Bernd Schmidteb8cdec2009-09-21 17:03:57 -07001574
1575#ifdef CONFIG_MPU
1576 update_protections(current->mm);
1577#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578}
1579
1580void *__symbol_get(const char *symbol)
1581{
1582 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001583 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Rusty Russell24da1cb2007-07-15 23:41:46 -07001585 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001586 sym = find_symbol(symbol, &owner, NULL, true, true);
1587 if (sym && strong_try_module_get(owner))
1588 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001589 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Tim Abbott414fd312008-12-05 19:03:56 -05001591 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592}
1593EXPORT_SYMBOL_GPL(__symbol_get);
1594
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001595/*
1596 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001597 * in the kernel or in some other module's exported symbol table.
Rusty Russellbe593f42010-06-05 11:17:37 -06001598 *
1599 * You must hold the module_mutex.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001600 */
1601static int verify_export_symbols(struct module *mod)
1602{
Rusty Russellb2111042008-05-01 21:15:00 -05001603 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001604 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001605 const struct kernel_symbol *s;
1606 struct {
1607 const struct kernel_symbol *sym;
1608 unsigned int num;
1609 } arr[] = {
1610 { mod->syms, mod->num_syms },
1611 { mod->gpl_syms, mod->num_gpl_syms },
1612 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001613#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001614 { mod->unused_syms, mod->num_unused_syms },
1615 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001616#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001617 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001618
Rusty Russellb2111042008-05-01 21:15:00 -05001619 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1620 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Rusty Russellbe593f42010-06-05 11:17:37 -06001621 if (find_symbol(s->name, &owner, NULL, true, false)) {
Rusty Russellb2111042008-05-01 21:15:00 -05001622 printk(KERN_ERR
1623 "%s: exports duplicate symbol %s"
1624 " (owned by %s)\n",
1625 mod->name, s->name, module_name(owner));
1626 return -ENOEXEC;
1627 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001628 }
Rusty Russellb2111042008-05-01 21:15:00 -05001629 }
1630 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001631}
1632
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001633/* Change all symbols so that st_value encodes the pointer directly. */
Rusty Russell49668682010-08-05 12:59:10 -06001634static int simplify_symbols(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
Rusty Russell49668682010-08-05 12:59:10 -06001636 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
1637 Elf_Sym *sym = (void *)symsec->sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 unsigned long secbase;
Rusty Russell49668682010-08-05 12:59:10 -06001639 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05001641 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Rusty Russell49668682010-08-05 12:59:10 -06001643 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
1644 const char *name = info->strtab + sym[i].st_name;
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 switch (sym[i].st_shndx) {
1647 case SHN_COMMON:
1648 /* We compiled with -fno-common. These are not
1649 supposed to happen. */
Rusty Russell49668682010-08-05 12:59:10 -06001650 DEBUGP("Common symbol: %s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 printk("%s: please compile with -fno-common\n",
1652 mod->name);
1653 ret = -ENOEXEC;
1654 break;
1655
1656 case SHN_ABS:
1657 /* Don't need to do anything */
1658 DEBUGP("Absolute symbol: 0x%08lx\n",
1659 (long)sym[i].st_value);
1660 break;
1661
1662 case SHN_UNDEF:
Rusty Russell49668682010-08-05 12:59:10 -06001663 ksym = resolve_symbol_wait(mod, info, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 /* Ok if resolved. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001665 if (ksym && !IS_ERR(ksym)) {
Tim Abbott414fd312008-12-05 19:03:56 -05001666 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 break;
Tim Abbott414fd312008-12-05 19:03:56 -05001668 }
1669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 /* Ok if weak. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001671 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 break;
1673
Rusty Russell9bea7f22010-06-05 11:17:37 -06001674 printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
Rusty Russell49668682010-08-05 12:59:10 -06001675 mod->name, name, PTR_ERR(ksym));
Rusty Russell9bea7f22010-06-05 11:17:37 -06001676 ret = PTR_ERR(ksym) ?: -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 break;
1678
1679 default:
1680 /* Divert to percpu allocation if a percpu var. */
Rusty Russell49668682010-08-05 12:59:10 -06001681 if (sym[i].st_shndx == info->index.pcpu)
Tejun Heo259354d2010-03-10 18:56:10 +09001682 secbase = (unsigned long)mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 else
Rusty Russell49668682010-08-05 12:59:10 -06001684 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 sym[i].st_value += secbase;
1686 break;
1687 }
1688 }
1689
1690 return ret;
1691}
1692
Rusty Russell49668682010-08-05 12:59:10 -06001693static int apply_relocations(struct module *mod, const struct load_info *info)
Rusty Russell22e268e2010-08-05 12:59:05 -06001694{
1695 unsigned int i;
1696 int err = 0;
1697
1698 /* Now do relocations. */
Rusty Russell49668682010-08-05 12:59:10 -06001699 for (i = 1; i < info->hdr->e_shnum; i++) {
1700 unsigned int infosec = info->sechdrs[i].sh_info;
Rusty Russell22e268e2010-08-05 12:59:05 -06001701
1702 /* Not a valid relocation section? */
Rusty Russell49668682010-08-05 12:59:10 -06001703 if (infosec >= info->hdr->e_shnum)
Rusty Russell22e268e2010-08-05 12:59:05 -06001704 continue;
1705
1706 /* Don't bother with non-allocated sections */
Rusty Russell49668682010-08-05 12:59:10 -06001707 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
Rusty Russell22e268e2010-08-05 12:59:05 -06001708 continue;
1709
Rusty Russell49668682010-08-05 12:59:10 -06001710 if (info->sechdrs[i].sh_type == SHT_REL)
1711 err = apply_relocate(info->sechdrs, info->strtab,
1712 info->index.sym, i, mod);
1713 else if (info->sechdrs[i].sh_type == SHT_RELA)
1714 err = apply_relocate_add(info->sechdrs, info->strtab,
1715 info->index.sym, i, mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06001716 if (err < 0)
1717 break;
1718 }
1719 return err;
1720}
1721
Helge Deller088af9a2008-12-31 12:31:18 +01001722/* Additional bytes needed by arch in front of individual sections */
1723unsigned int __weak arch_mod_section_prepend(struct module *mod,
1724 unsigned int section)
1725{
1726 /* default implementation just returns zero */
1727 return 0;
1728}
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01001731static long get_offset(struct module *mod, unsigned int *size,
1732 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
1734 long ret;
1735
Helge Deller088af9a2008-12-31 12:31:18 +01001736 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1738 *size = ret + sechdr->sh_size;
1739 return ret;
1740}
1741
1742/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1743 might -- code, read-only data, read-write data, small data. Tally
1744 sizes, and place the offsets into sh_entsize fields: high bit means it
1745 belongs in init. */
Rusty Russell49668682010-08-05 12:59:10 -06001746static void layout_sections(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
1748 static unsigned long const masks[][2] = {
1749 /* NOTE: all executable code must be the first section
1750 * in this array; otherwise modify the text_size
1751 * finder in the two loops below */
1752 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1753 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1754 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1755 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1756 };
1757 unsigned int m, i;
1758
Rusty Russell49668682010-08-05 12:59:10 -06001759 for (i = 0; i < info->hdr->e_shnum; i++)
1760 info->sechdrs[i].sh_entsize = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 DEBUGP("Core section allocation order:\n");
1763 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06001764 for (i = 0; i < info->hdr->e_shnum; ++i) {
1765 Elf_Shdr *s = &info->sechdrs[i];
1766 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1769 || (s->sh_flags & masks[m][1])
1770 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06001771 || strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001773 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Rusty Russell49668682010-08-05 12:59:10 -06001774 DEBUGP("\t%s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 }
1776 if (m == 0)
1777 mod->core_text_size = mod->core_size;
1778 }
1779
1780 DEBUGP("Init section allocation order:\n");
1781 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06001782 for (i = 0; i < info->hdr->e_shnum; ++i) {
1783 Elf_Shdr *s = &info->sechdrs[i];
1784 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1787 || (s->sh_flags & masks[m][1])
1788 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06001789 || !strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001791 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 | INIT_OFFSET_MASK);
Rusty Russell49668682010-08-05 12:59:10 -06001793 DEBUGP("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 }
1795 if (m == 0)
1796 mod->init_text_size = mod->init_size;
1797 }
1798}
1799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800static void set_license(struct module *mod, const char *license)
1801{
1802 if (!license)
1803 license = "unspecified";
1804
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001805 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07001806 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001807 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001808 "kernel.\n", mod->name, license);
1809 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 }
1811}
1812
1813/* Parse tag=value strings from .modinfo section */
1814static char *next_string(char *string, unsigned long *secsize)
1815{
1816 /* Skip non-zero chars */
1817 while (string[0]) {
1818 string++;
1819 if ((*secsize)-- <= 1)
1820 return NULL;
1821 }
1822
1823 /* Skip any zero padding. */
1824 while (!string[0]) {
1825 string++;
1826 if ((*secsize)-- <= 1)
1827 return NULL;
1828 }
1829 return string;
1830}
1831
Rusty Russell49668682010-08-05 12:59:10 -06001832static char *get_modinfo(struct load_info *info, const char *tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
1834 char *p;
1835 unsigned int taglen = strlen(tag);
Rusty Russell49668682010-08-05 12:59:10 -06001836 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
1837 unsigned long size = infosec->sh_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Rusty Russell49668682010-08-05 12:59:10 -06001839 for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1841 return p + taglen + 1;
1842 }
1843 return NULL;
1844}
1845
Rusty Russell49668682010-08-05 12:59:10 -06001846static void setup_modinfo(struct module *mod, struct load_info *info)
Matt Domschc988d2b2005-06-23 22:05:15 -07001847{
1848 struct module_attribute *attr;
1849 int i;
1850
1851 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1852 if (attr->setup)
Rusty Russell49668682010-08-05 12:59:10 -06001853 attr->setup(mod, get_modinfo(info, attr->attr.name));
Matt Domschc988d2b2005-06-23 22:05:15 -07001854 }
1855}
Matt Domschc988d2b2005-06-23 22:05:15 -07001856
Rusty Russella263f772009-09-25 00:32:58 -06001857static void free_modinfo(struct module *mod)
1858{
1859 struct module_attribute *attr;
1860 int i;
1861
1862 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1863 if (attr->free)
1864 attr->free(mod);
1865 }
1866}
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01001869
1870/* lookup symbol in given range of kernel_symbols */
1871static const struct kernel_symbol *lookup_symbol(const char *name,
1872 const struct kernel_symbol *start,
1873 const struct kernel_symbol *stop)
1874{
1875 const struct kernel_symbol *ks = start;
1876 for (; ks < stop; ks++)
1877 if (strcmp(ks->name, name) == 0)
1878 return ks;
1879 return NULL;
1880}
1881
Tim Abbottca4787b2009-01-05 08:40:10 -06001882static int is_exported(const char *name, unsigned long value,
1883 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
Tim Abbottca4787b2009-01-05 08:40:10 -06001885 const struct kernel_symbol *ks;
1886 if (!mod)
1887 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001888 else
Tim Abbottca4787b2009-01-05 08:40:10 -06001889 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
1890 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891}
1892
1893/* As per nm */
Rusty Russelleded41c2010-08-05 12:59:07 -06001894static char elf_type(const Elf_Sym *sym, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
Rusty Russelleded41c2010-08-05 12:59:07 -06001896 const Elf_Shdr *sechdrs = info->sechdrs;
1897
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1899 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1900 return 'v';
1901 else
1902 return 'w';
1903 }
1904 if (sym->st_shndx == SHN_UNDEF)
1905 return 'U';
1906 if (sym->st_shndx == SHN_ABS)
1907 return 'a';
1908 if (sym->st_shndx >= SHN_LORESERVE)
1909 return '?';
1910 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1911 return 't';
1912 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1913 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1914 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1915 return 'r';
1916 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1917 return 'g';
1918 else
1919 return 'd';
1920 }
1921 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1922 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1923 return 's';
1924 else
1925 return 'b';
1926 }
Rusty Russelleded41c2010-08-05 12:59:07 -06001927 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
1928 ".debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 return 'n';
Rusty Russelleded41c2010-08-05 12:59:07 -06001930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return '?';
1932}
1933
Jan Beulich4a496222009-07-06 14:50:42 +01001934static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
1935 unsigned int shnum)
1936{
1937 const Elf_Shdr *sec;
1938
1939 if (src->st_shndx == SHN_UNDEF
1940 || src->st_shndx >= shnum
1941 || !src->st_name)
1942 return false;
1943
1944 sec = sechdrs + src->st_shndx;
1945 if (!(sec->sh_flags & SHF_ALLOC)
1946#ifndef CONFIG_KALLSYMS_ALL
1947 || !(sec->sh_flags & SHF_EXECINSTR)
1948#endif
1949 || (sec->sh_entsize & INIT_OFFSET_MASK))
1950 return false;
1951
1952 return true;
1953}
1954
Rusty Russell49668682010-08-05 12:59:10 -06001955static void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01001956{
Rusty Russell49668682010-08-05 12:59:10 -06001957 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
1958 Elf_Shdr *strsect = info->sechdrs + info->index.str;
Jan Beulich4a496222009-07-06 14:50:42 +01001959 const Elf_Sym *src;
1960 unsigned int i, nsrc, ndst;
1961
1962 /* Put symbol section at end of init part of module. */
1963 symsect->sh_flags |= SHF_ALLOC;
1964 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
Rusty Russell49668682010-08-05 12:59:10 -06001965 info->index.sym) | INIT_OFFSET_MASK;
1966 DEBUGP("\t%s\n", info->secstrings + symsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01001967
Rusty Russell49668682010-08-05 12:59:10 -06001968 src = (void *)info->hdr + symsect->sh_offset;
Jan Beulich4a496222009-07-06 14:50:42 +01001969 nsrc = symsect->sh_size / sizeof(*src);
1970 for (ndst = i = 1; i < nsrc; ++i, ++src)
Rusty Russell49668682010-08-05 12:59:10 -06001971 if (is_core_symbol(src, info->sechdrs, info->hdr->e_shnum)) {
Jan Beulich554bdfe2009-07-06 14:51:44 +01001972 unsigned int j = src->st_name;
1973
Rusty Russell49668682010-08-05 12:59:10 -06001974 while (!__test_and_set_bit(j, info->strmap)
1975 && info->strtab[j])
Jan Beulich554bdfe2009-07-06 14:51:44 +01001976 ++j;
Jan Beulich4a496222009-07-06 14:50:42 +01001977 ++ndst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01001978 }
Jan Beulich4a496222009-07-06 14:50:42 +01001979
1980 /* Append room for core symbols at end of core part. */
Rusty Russell49668682010-08-05 12:59:10 -06001981 info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
1982 mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
Jan Beulich4a496222009-07-06 14:50:42 +01001983
Jan Beulich554bdfe2009-07-06 14:51:44 +01001984 /* Put string table section at end of init part of module. */
1985 strsect->sh_flags |= SHF_ALLOC;
1986 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
Rusty Russell49668682010-08-05 12:59:10 -06001987 info->index.str) | INIT_OFFSET_MASK;
1988 DEBUGP("\t%s\n", info->secstrings + strsect->sh_name);
Jan Beulich554bdfe2009-07-06 14:51:44 +01001989
1990 /* Append room for core symbols' strings at end of core part. */
Rusty Russell49668682010-08-05 12:59:10 -06001991 info->stroffs = mod->core_size;
1992 __set_bit(0, info->strmap);
1993 mod->core_size += bitmap_weight(info->strmap, strsect->sh_size);
Jan Beulich4a496222009-07-06 14:50:42 +01001994}
1995
Rusty Russelld9131882010-08-05 12:59:08 -06001996static void add_kallsyms(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
Jan Beulich4a496222009-07-06 14:50:42 +01001998 unsigned int i, ndst;
1999 const Elf_Sym *src;
2000 Elf_Sym *dst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002001 char *s;
Rusty Russelleded41c2010-08-05 12:59:07 -06002002 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Rusty Russelleded41c2010-08-05 12:59:07 -06002004 mod->symtab = (void *)symsec->sh_addr;
2005 mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
Rusty Russell511ca6a2010-08-05 12:59:08 -06002006 /* Make sure we get permanent strtab: don't use info->strtab. */
2007 mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
2009 /* Set types up while we still have access to sections. */
2010 for (i = 0; i < mod->num_symtab; i++)
Rusty Russelleded41c2010-08-05 12:59:07 -06002011 mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
Jan Beulich4a496222009-07-06 14:50:42 +01002012
Rusty Russelld9131882010-08-05 12:59:08 -06002013 mod->core_symtab = dst = mod->module_core + info->symoffs;
Jan Beulich4a496222009-07-06 14:50:42 +01002014 src = mod->symtab;
2015 *dst = *src;
2016 for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
Rusty Russelleded41c2010-08-05 12:59:07 -06002017 if (!is_core_symbol(src, info->sechdrs, info->hdr->e_shnum))
Jan Beulich4a496222009-07-06 14:50:42 +01002018 continue;
2019 dst[ndst] = *src;
Rusty Russelld9131882010-08-05 12:59:08 -06002020 dst[ndst].st_name = bitmap_weight(info->strmap,
2021 dst[ndst].st_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002022 ++ndst;
2023 }
2024 mod->core_num_syms = ndst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002025
Rusty Russelld9131882010-08-05 12:59:08 -06002026 mod->core_strtab = s = mod->module_core + info->stroffs;
Rusty Russelleded41c2010-08-05 12:59:07 -06002027 for (*s = 0, i = 1; i < info->sechdrs[info->index.str].sh_size; ++i)
Rusty Russelld9131882010-08-05 12:59:08 -06002028 if (test_bit(i, info->strmap))
Jan Beulich554bdfe2009-07-06 14:51:44 +01002029 *++s = mod->strtab[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031#else
Rusty Russell49668682010-08-05 12:59:10 -06002032static inline void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002033{
2034}
Paul Mundt3ae91c22009-10-01 15:43:54 -07002035
Rusty Russelld9131882010-08-05 12:59:08 -06002036static void add_kallsyms(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
2038}
2039#endif /* CONFIG_KALLSYMS */
2040
Jason Barone9d376f2009-02-05 11:51:38 -05002041static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05002042{
Jason Barone9d376f2009-02-05 11:51:38 -05002043#ifdef CONFIG_DYNAMIC_DEBUG
2044 if (ddebug_add_module(debug, num, debug->modname))
2045 printk(KERN_ERR "dynamic debug error adding module: %s\n",
2046 debug->modname);
2047#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002048}
Jason Baron346e15b2008-08-12 16:46:19 -04002049
Yehuda Sadehff49d742010-07-03 13:07:35 +10002050static void dynamic_debug_remove(struct _ddebug *debug)
2051{
2052 if (debug)
2053 ddebug_remove_module(debug->modname);
2054}
2055
Rusty Russell3a642e92008-07-22 19:24:28 -05002056static void *module_alloc_update_bounds(unsigned long size)
2057{
2058 void *ret = module_alloc(size);
2059
2060 if (ret) {
Rusty Russell75676502010-06-05 11:17:36 -06002061 mutex_lock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002062 /* Update module bounds. */
2063 if ((unsigned long)ret < module_addr_min)
2064 module_addr_min = (unsigned long)ret;
2065 if ((unsigned long)ret + size > module_addr_max)
2066 module_addr_max = (unsigned long)ret + size;
Rusty Russell75676502010-06-05 11:17:36 -06002067 mutex_unlock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002068 }
2069 return ret;
2070}
2071
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002072#ifdef CONFIG_DEBUG_KMEMLEAK
Rusty Russell49668682010-08-05 12:59:10 -06002073static void kmemleak_load_module(const struct module *mod,
2074 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002075{
2076 unsigned int i;
2077
2078 /* only scan the sections containing data */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002079 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002080
Rusty Russell49668682010-08-05 12:59:10 -06002081 for (i = 1; i < info->hdr->e_shnum; i++) {
2082 const char *name = info->secstrings + info->sechdrs[i].sh_name;
2083 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002084 continue;
Rusty Russell49668682010-08-05 12:59:10 -06002085 if (!strstarts(name, ".data") && !strstarts(name, ".bss"))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002086 continue;
2087
Rusty Russell49668682010-08-05 12:59:10 -06002088 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2089 info->sechdrs[i].sh_size, GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002090 }
2091}
2092#else
Rusty Russell49668682010-08-05 12:59:10 -06002093static inline void kmemleak_load_module(const struct module *mod,
2094 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002095{
2096}
2097#endif
2098
Rusty Russelld9131882010-08-05 12:59:08 -06002099/* Sets info->hdr, info->len and info->args. */
2100static int copy_and_check(struct load_info *info,
2101 const void __user *umod, unsigned long len,
2102 const char __user *uargs)
Rusty Russell40dd2562010-08-05 12:59:03 -06002103{
2104 int err;
2105 Elf_Ehdr *hdr;
2106
2107 if (len < sizeof(*hdr))
2108 return -ENOEXEC;
2109
2110 /* Suck in entire file: we'll want most of it. */
2111 /* vmalloc barfs on "unusual" numbers. Check here */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002112 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
Rusty Russell40dd2562010-08-05 12:59:03 -06002113 return -ENOMEM;
2114
2115 if (copy_from_user(hdr, umod, len) != 0) {
2116 err = -EFAULT;
2117 goto free_hdr;
2118 }
2119
2120 /* Sanity checks against insmoding binaries or wrong arch,
2121 weird elf version */
2122 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
2123 || hdr->e_type != ET_REL
2124 || !elf_check_arch(hdr)
2125 || hdr->e_shentsize != sizeof(Elf_Shdr)) {
2126 err = -ENOEXEC;
2127 goto free_hdr;
2128 }
2129
2130 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) {
2131 err = -ENOEXEC;
2132 goto free_hdr;
2133 }
Rusty Russelld9131882010-08-05 12:59:08 -06002134
2135 /* Now copy in args */
2136 info->args = strndup_user(uargs, ~0UL >> 1);
2137 if (IS_ERR(info->args)) {
2138 err = PTR_ERR(info->args);
2139 goto free_hdr;
2140 }
2141
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002142 info->hdr = hdr;
2143 info->len = len;
Rusty Russell40dd2562010-08-05 12:59:03 -06002144 return 0;
2145
2146free_hdr:
2147 vfree(hdr);
2148 return err;
2149}
2150
Rusty Russelld9131882010-08-05 12:59:08 -06002151static void free_copy(struct load_info *info)
2152{
2153 kfree(info->args);
2154 vfree(info->hdr);
2155}
2156
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002157static int rewrite_section_headers(struct load_info *info)
2158{
2159 unsigned int i;
2160
2161 /* This should always be true, but let's be sure. */
2162 info->sechdrs[0].sh_addr = 0;
2163
2164 for (i = 1; i < info->hdr->e_shnum; i++) {
2165 Elf_Shdr *shdr = &info->sechdrs[i];
2166 if (shdr->sh_type != SHT_NOBITS
2167 && info->len < shdr->sh_offset + shdr->sh_size) {
2168 printk(KERN_ERR "Module len %lu truncated\n",
2169 info->len);
2170 return -ENOEXEC;
2171 }
2172
2173 /* Mark all sections sh_addr with their address in the
2174 temporary image. */
2175 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2176
2177#ifndef CONFIG_MODULE_UNLOAD
2178 /* Don't load .exit sections */
2179 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2180 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2181#endif
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002182 }
Rusty Russelld6df72a2010-08-05 12:59:07 -06002183
2184 /* Track but don't keep modinfo and version sections. */
Rusty Russell49668682010-08-05 12:59:10 -06002185 info->index.vers = find_sec(info, "__versions");
2186 info->index.info = find_sec(info, ".modinfo");
Rusty Russelld6df72a2010-08-05 12:59:07 -06002187 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2188 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002189 return 0;
2190}
2191
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002192/*
2193 * Set up our basic convenience variables (pointers to section headers,
2194 * search for module section index etc), and do some basic section
2195 * verification.
2196 *
2197 * Return the temporary module pointer (we'll replace it with the final
2198 * one when we move the module sections around).
2199 */
2200static struct module *setup_load_info(struct load_info *info)
2201{
2202 unsigned int i;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002203 int err;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002204 struct module *mod;
2205
2206 /* Set up the convenience variables */
2207 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002208 info->secstrings = (void *)info->hdr
2209 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002210
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002211 err = rewrite_section_headers(info);
2212 if (err)
2213 return ERR_PTR(err);
2214
2215 /* Find internal symbols and strings. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002216 for (i = 1; i < info->hdr->e_shnum; i++) {
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002217 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2218 info->index.sym = i;
2219 info->index.str = info->sechdrs[i].sh_link;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002220 info->strtab = (char *)info->hdr
2221 + info->sechdrs[info->index.str].sh_offset;
2222 break;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002223 }
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002224 }
2225
Rusty Russell49668682010-08-05 12:59:10 -06002226 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002227 if (!info->index.mod) {
2228 printk(KERN_WARNING "No module found in object\n");
2229 return ERR_PTR(-ENOEXEC);
2230 }
2231 /* This is temporary: point mod into copy of data. */
2232 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2233
2234 if (info->index.sym == 0) {
2235 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2236 mod->name);
2237 return ERR_PTR(-ENOEXEC);
2238 }
2239
Rusty Russell49668682010-08-05 12:59:10 -06002240 info->index.pcpu = find_pcpusec(info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002241
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002242 /* Check module struct version now, before we try to use module. */
2243 if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2244 return ERR_PTR(-ENOEXEC);
2245
2246 return mod;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002247}
2248
Rusty Russell49668682010-08-05 12:59:10 -06002249static int check_modinfo(struct module *mod, struct load_info *info)
Rusty Russell40dd2562010-08-05 12:59:03 -06002250{
Rusty Russell49668682010-08-05 12:59:10 -06002251 const char *modmagic = get_modinfo(info, "vermagic");
Rusty Russell40dd2562010-08-05 12:59:03 -06002252 int err;
2253
2254 /* This is allowed: modprobe --force will invalidate it. */
2255 if (!modmagic) {
2256 err = try_to_force_load(mod, "bad vermagic");
2257 if (err)
2258 return err;
Rusty Russell49668682010-08-05 12:59:10 -06002259 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002260 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2261 mod->name, modmagic, vermagic);
2262 return -ENOEXEC;
2263 }
2264
Rusty Russell49668682010-08-05 12:59:10 -06002265 if (get_modinfo(info, "staging")) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002266 add_taint_module(mod, TAINT_CRAP);
2267 printk(KERN_WARNING "%s: module is from the staging directory,"
2268 " the quality is unknown, you have been warned.\n",
2269 mod->name);
2270 }
Rusty Russell22e268e2010-08-05 12:59:05 -06002271
2272 /* Set up license info based on the info section */
Rusty Russell49668682010-08-05 12:59:10 -06002273 set_license(mod, get_modinfo(info, "license"));
Rusty Russell22e268e2010-08-05 12:59:05 -06002274
Rusty Russell40dd2562010-08-05 12:59:03 -06002275 return 0;
2276}
2277
Rusty Russell49668682010-08-05 12:59:10 -06002278static void find_module_sections(struct module *mod,
2279 const struct load_info *info)
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002280{
Rusty Russell49668682010-08-05 12:59:10 -06002281 mod->kp = section_objs(info, "__param",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002282 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell49668682010-08-05 12:59:10 -06002283 mod->syms = section_objs(info, "__ksymtab",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002284 sizeof(*mod->syms), &mod->num_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002285 mod->crcs = section_addr(info, "__kcrctab");
2286 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002287 sizeof(*mod->gpl_syms),
2288 &mod->num_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002289 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
2290 mod->gpl_future_syms = section_objs(info,
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002291 "__ksymtab_gpl_future",
2292 sizeof(*mod->gpl_future_syms),
2293 &mod->num_gpl_future_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002294 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002295
2296#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell49668682010-08-05 12:59:10 -06002297 mod->unused_syms = section_objs(info, "__ksymtab_unused",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002298 sizeof(*mod->unused_syms),
2299 &mod->num_unused_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002300 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
2301 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002302 sizeof(*mod->unused_gpl_syms),
2303 &mod->num_unused_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002304 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002305#endif
2306#ifdef CONFIG_CONSTRUCTORS
Rusty Russell49668682010-08-05 12:59:10 -06002307 mod->ctors = section_objs(info, ".ctors",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002308 sizeof(*mod->ctors), &mod->num_ctors);
2309#endif
2310
2311#ifdef CONFIG_TRACEPOINTS
Rusty Russell49668682010-08-05 12:59:10 -06002312 mod->tracepoints = section_objs(info, "__tracepoints",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002313 sizeof(*mod->tracepoints),
2314 &mod->num_tracepoints);
2315#endif
2316#ifdef CONFIG_EVENT_TRACING
Rusty Russell49668682010-08-05 12:59:10 -06002317 mod->trace_events = section_objs(info, "_ftrace_events",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002318 sizeof(*mod->trace_events),
2319 &mod->num_trace_events);
2320 /*
2321 * This section contains pointers to allocated objects in the trace
2322 * code and not scanning it leads to false positives.
2323 */
2324 kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2325 mod->num_trace_events, GFP_KERNEL);
2326#endif
2327#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2328 /* sechdrs[0].sh_size is always zero */
Rusty Russell49668682010-08-05 12:59:10 -06002329 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002330 sizeof(*mod->ftrace_callsites),
2331 &mod->num_ftrace_callsites);
2332#endif
Rusty Russell22e268e2010-08-05 12:59:05 -06002333
Rusty Russell49668682010-08-05 12:59:10 -06002334 if (section_addr(info, "__obsparm"))
Rusty Russell22e268e2010-08-05 12:59:05 -06002335 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2336 mod->name);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002337}
2338
Rusty Russell49668682010-08-05 12:59:10 -06002339static int move_module(struct module *mod, struct load_info *info)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002340{
2341 int i;
2342 void *ptr;
2343
2344 /* Do the allocs. */
2345 ptr = module_alloc_update_bounds(mod->core_size);
2346 /*
2347 * The pointer to this block is stored in the module structure
2348 * which is inside the block. Just mark it as not being a
2349 * leak.
2350 */
2351 kmemleak_not_leak(ptr);
2352 if (!ptr)
Rusty Russelld9131882010-08-05 12:59:08 -06002353 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002354
2355 memset(ptr, 0, mod->core_size);
2356 mod->module_core = ptr;
2357
2358 ptr = module_alloc_update_bounds(mod->init_size);
2359 /*
2360 * The pointer to this block is stored in the module structure
2361 * which is inside the block. This block doesn't need to be
2362 * scanned as it contains data and code that will be freed
2363 * after the module is initialized.
2364 */
2365 kmemleak_ignore(ptr);
2366 if (!ptr && mod->init_size) {
2367 module_free(mod, mod->module_core);
Rusty Russelld9131882010-08-05 12:59:08 -06002368 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002369 }
2370 memset(ptr, 0, mod->init_size);
2371 mod->module_init = ptr;
2372
2373 /* Transfer each section which specifies SHF_ALLOC */
2374 DEBUGP("final section addresses:\n");
Rusty Russell49668682010-08-05 12:59:10 -06002375 for (i = 0; i < info->hdr->e_shnum; i++) {
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002376 void *dest;
Rusty Russell49668682010-08-05 12:59:10 -06002377 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002378
Rusty Russell49668682010-08-05 12:59:10 -06002379 if (!(shdr->sh_flags & SHF_ALLOC))
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002380 continue;
2381
Rusty Russell49668682010-08-05 12:59:10 -06002382 if (shdr->sh_entsize & INIT_OFFSET_MASK)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002383 dest = mod->module_init
Rusty Russell49668682010-08-05 12:59:10 -06002384 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002385 else
Rusty Russell49668682010-08-05 12:59:10 -06002386 dest = mod->module_core + shdr->sh_entsize;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002387
Rusty Russell49668682010-08-05 12:59:10 -06002388 if (shdr->sh_type != SHT_NOBITS)
2389 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002390 /* Update sh_addr to point to copy in image. */
Rusty Russell49668682010-08-05 12:59:10 -06002391 shdr->sh_addr = (unsigned long)dest;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002392 DEBUGP("\t0x%lx %s\n",
Rusty Russell49668682010-08-05 12:59:10 -06002393 shdr->sh_addr, info->secstrings + shdr->sh_name);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002394 }
Rusty Russelld9131882010-08-05 12:59:08 -06002395
2396 return 0;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002397}
2398
Rusty Russell49668682010-08-05 12:59:10 -06002399static int check_module_license_and_versions(struct module *mod)
Rusty Russell22e268e2010-08-05 12:59:05 -06002400{
2401 /*
2402 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2403 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2404 * using GPL-only symbols it needs.
2405 */
2406 if (strcmp(mod->name, "ndiswrapper") == 0)
2407 add_taint(TAINT_PROPRIETARY_MODULE);
2408
2409 /* driverloader was caught wrongly pretending to be under GPL */
2410 if (strcmp(mod->name, "driverloader") == 0)
2411 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2412
2413#ifdef CONFIG_MODVERSIONS
2414 if ((mod->num_syms && !mod->crcs)
2415 || (mod->num_gpl_syms && !mod->gpl_crcs)
2416 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2417#ifdef CONFIG_UNUSED_SYMBOLS
2418 || (mod->num_unused_syms && !mod->unused_crcs)
2419 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2420#endif
2421 ) {
2422 return try_to_force_load(mod,
2423 "no versions for exported symbols");
2424 }
2425#endif
2426 return 0;
2427}
2428
2429static void flush_module_icache(const struct module *mod)
2430{
2431 mm_segment_t old_fs;
2432
2433 /* flush the icache in correct context */
2434 old_fs = get_fs();
2435 set_fs(KERNEL_DS);
2436
2437 /*
2438 * Flush the instruction cache, since we've played with text.
2439 * Do it before processing of module parameters, so the module
2440 * can provide parameter accessor functions of its own.
2441 */
2442 if (mod->module_init)
2443 flush_icache_range((unsigned long)mod->module_init,
2444 (unsigned long)mod->module_init
2445 + mod->init_size);
2446 flush_icache_range((unsigned long)mod->module_core,
2447 (unsigned long)mod->module_core + mod->core_size);
2448
2449 set_fs(old_fs);
2450}
2451
Rusty Russelld9131882010-08-05 12:59:08 -06002452static struct module *layout_and_allocate(struct load_info *info)
2453{
2454 /* Module within temporary copy. */
2455 struct module *mod;
Rusty Russell49668682010-08-05 12:59:10 -06002456 Elf_Shdr *pcpusec;
Rusty Russelld9131882010-08-05 12:59:08 -06002457 int err;
2458
2459 mod = setup_load_info(info);
2460 if (IS_ERR(mod))
2461 return mod;
2462
Rusty Russell49668682010-08-05 12:59:10 -06002463 err = check_modinfo(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002464 if (err)
2465 return ERR_PTR(err);
2466
2467 /* Allow arches to frob section contents and sizes. */
Rusty Russell49668682010-08-05 12:59:10 -06002468 err = module_frob_arch_sections(info->hdr, info->sechdrs,
2469 info->secstrings, mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002470 if (err < 0)
2471 goto free_args;
2472
Rusty Russell49668682010-08-05 12:59:10 -06002473 pcpusec = &info->sechdrs[info->index.pcpu];
2474 if (pcpusec->sh_size) {
Rusty Russelld9131882010-08-05 12:59:08 -06002475 /* We have a special allocation for this section. */
Rusty Russell49668682010-08-05 12:59:10 -06002476 err = percpu_modalloc(mod,
2477 pcpusec->sh_size, pcpusec->sh_addralign);
Rusty Russelld9131882010-08-05 12:59:08 -06002478 if (err)
2479 goto free_args;
Rusty Russell49668682010-08-05 12:59:10 -06002480 pcpusec->sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russelld9131882010-08-05 12:59:08 -06002481 }
2482
2483 /* Determine total sizes, and put offsets in sh_entsize. For now
2484 this is done generically; there doesn't appear to be any
2485 special cases for the architectures. */
Rusty Russell49668682010-08-05 12:59:10 -06002486 layout_sections(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002487
2488 info->strmap = kzalloc(BITS_TO_LONGS(info->sechdrs[info->index.str].sh_size)
2489 * sizeof(long), GFP_KERNEL);
2490 if (!info->strmap) {
2491 err = -ENOMEM;
2492 goto free_percpu;
2493 }
Rusty Russell49668682010-08-05 12:59:10 -06002494 layout_symtab(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002495
2496 /* Allocate and move to the final place */
Rusty Russell49668682010-08-05 12:59:10 -06002497 err = move_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002498 if (err)
2499 goto free_strmap;
2500
2501 /* Module has been copied to its final place now: return it. */
2502 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
Rusty Russell49668682010-08-05 12:59:10 -06002503 kmemleak_load_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002504 return mod;
2505
2506free_strmap:
2507 kfree(info->strmap);
2508free_percpu:
2509 percpu_modfree(mod);
2510free_args:
2511 kfree(info->args);
2512 return ERR_PTR(err);
2513}
2514
2515/* mod is no longer valid after this! */
2516static void module_deallocate(struct module *mod, struct load_info *info)
2517{
2518 kfree(info->strmap);
2519 percpu_modfree(mod);
2520 module_free(mod, mod->module_init);
2521 module_free(mod, mod->module_core);
2522}
2523
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524/* Allocate and load the module: note that size of section 0 is always
2525 zero, and we rely on this for optional sections. */
Linus Torvaldsffb4ba72008-08-25 11:10:26 -07002526static noinline struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 unsigned long len,
2528 const char __user *uargs)
2529{
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002530 struct load_info info = { NULL, };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 struct module *mod;
Rusty Russell40dd2562010-08-05 12:59:03 -06002532 long err;
Yehuda Sadehff49d742010-07-03 13:07:35 +10002533 struct _ddebug *debug = NULL;
2534 unsigned int num_debug = 0;
Paul Mundt3ae91c22009-10-01 15:43:54 -07002535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
2537 umod, len, uargs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Rusty Russelld9131882010-08-05 12:59:08 -06002539 /* Copy in the blobs from userspace, check they are vaguely sane. */
2540 err = copy_and_check(&info, umod, len, uargs);
Rusty Russell40dd2562010-08-05 12:59:03 -06002541 if (err)
2542 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
Rusty Russelld9131882010-08-05 12:59:08 -06002544 /* Figure out module layout, and allocate all the memory. */
2545 mod = layout_and_allocate(&info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002546 if (IS_ERR(mod)) {
2547 err = PTR_ERR(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002548 goto free_copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Rusty Russell49668682010-08-05 12:59:10 -06002551 /* Now module is in final location, initialize linked lists, etc. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -06002552 err = module_unload_init(mod);
2553 if (err)
Rusty Russelld9131882010-08-05 12:59:08 -06002554 goto free_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
Rusty Russell22e268e2010-08-05 12:59:05 -06002556 /* Now we've got everything in the final locations, we can
2557 * find optional sections. */
Rusty Russell49668682010-08-05 12:59:10 -06002558 find_module_sections(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Rusty Russell49668682010-08-05 12:59:10 -06002560 err = check_module_license_and_versions(mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002561 if (err)
2562 goto free_unload;
Dave Jones9841d61d2006-01-08 01:03:41 -08002563
Matt Domschc988d2b2005-06-23 22:05:15 -07002564 /* Set up MODINFO_ATTR fields */
Rusty Russell49668682010-08-05 12:59:10 -06002565 setup_modinfo(mod, &info);
Matt Domschc988d2b2005-06-23 22:05:15 -07002566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 /* Fix up syms, so that st_value is a pointer to location. */
Rusty Russell49668682010-08-05 12:59:10 -06002568 err = simplify_symbols(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06002570 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Rusty Russell49668682010-08-05 12:59:10 -06002572 err = apply_relocations(mod, &info);
Rusty Russell22e268e2010-08-05 12:59:05 -06002573 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06002574 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
2576 /* Set up and sort exception table */
Rusty Russell49668682010-08-05 12:59:10 -06002577 mod->extable = section_objs(&info, "__ex_table",
Rusty Russell5e458cc2008-10-22 10:00:13 -05002578 sizeof(*mod->extable), &mod->num_exentries);
2579 sort_extable(mod->extable, mod->extable + mod->num_exentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
2581 /* Finally, copy percpu area over. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002582 percpu_modcopy(mod, (void *)info.sechdrs[info.index.pcpu].sh_addr,
2583 info.sechdrs[info.index.pcpu].sh_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
Rusty Russelld9131882010-08-05 12:59:08 -06002585 add_kallsyms(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
Yehuda Sadehff49d742010-07-03 13:07:35 +10002587 if (!mod->taints)
Rusty Russell49668682010-08-05 12:59:10 -06002588 debug = section_objs(&info, "__verbose",
Rusty Russell5e458cc2008-10-22 10:00:13 -05002589 sizeof(*debug), &num_debug);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002590
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002591 err = module_finalize(info.hdr, info.sechdrs, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06002593 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Rusty Russell22e268e2010-08-05 12:59:05 -06002595 flush_module_icache(mod);
Thomas Koeller378bac82005-09-06 15:17:11 -07002596
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002597 mod->args = info.args;
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002598
Rusty Russelld9131882010-08-05 12:59:08 -06002599 mod->state = MODULE_STATE_COMING;
2600
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002601 /* Now sew it into the lists so we can get lockdep and oops
Andi Kleend72b3752008-08-30 10:09:00 +02002602 * info during argument parsing. Noone should access us, since
2603 * strong_try_module_get() will fail.
2604 * lockdep/oops can run asynchronous, so use the RCU list insertion
2605 * function to insert in a way safe to concurrent readers.
2606 * The mutex protects against concurrent writers.
2607 */
Rusty Russell75676502010-06-05 11:17:36 -06002608 mutex_lock(&module_mutex);
Linus Torvalds3bafeb62010-06-05 11:17:36 -06002609 if (find_module(mod->name)) {
2610 err = -EEXIST;
Rusty Russellbe593f42010-06-05 11:17:37 -06002611 goto unlock;
Linus Torvalds3bafeb62010-06-05 11:17:36 -06002612 }
2613
Yehuda Sadehff49d742010-07-03 13:07:35 +10002614 if (debug)
2615 dynamic_debug_setup(debug, num_debug);
2616
Rusty Russellbe593f42010-06-05 11:17:37 -06002617 /* Find duplicate symbols */
2618 err = verify_export_symbols(mod);
2619 if (err < 0)
Yehuda Sadehff49d742010-07-03 13:07:35 +10002620 goto ddebug;
Rusty Russellbe593f42010-06-05 11:17:37 -06002621
Andi Kleend72b3752008-08-30 10:09:00 +02002622 list_add_rcu(&mod->list, &modules);
Rusty Russell75676502010-06-05 11:17:36 -06002623 mutex_unlock(&module_mutex);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002624
Rusty Russelle180a6b2009-03-31 13:05:29 -06002625 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002627 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
Rusty Russell8f6d0372010-08-05 12:59:09 -06002629 err = mod_sysfs_setup(mod, &info, mod->kp, mod->num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002631 goto unlink;
Rusty Russell80a3d1b2010-06-05 11:17:36 -06002632
Rusty Russelld9131882010-08-05 12:59:08 -06002633 /* Get rid of temporary copy and strmap. */
2634 kfree(info.strmap);
2635 free_copy(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
Li Zefan7ead8b82009-08-17 16:56:28 +08002637 trace_module_load(mod);
2638
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 /* Done! */
2640 return mod;
2641
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002642 unlink:
Rusty Russell75676502010-06-05 11:17:36 -06002643 mutex_lock(&module_mutex);
Rusty Russelle91defa2009-03-31 13:05:35 -06002644 /* Unlink carefully: kallsyms could be walking list. */
2645 list_del_rcu(&mod->list);
Yehuda Sadehff49d742010-07-03 13:07:35 +10002646 ddebug:
2647 dynamic_debug_remove(debug);
Rusty Russellbe593f42010-06-05 11:17:37 -06002648 unlock:
Rusty Russell75676502010-06-05 11:17:36 -06002649 mutex_unlock(&module_mutex);
Rusty Russelle91defa2009-03-31 13:05:35 -06002650 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 module_arch_cleanup(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002652 free_modinfo:
Rusty Russella263f772009-09-25 00:32:58 -06002653 free_modinfo(mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002654 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 module_unload_free(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002656 free_module:
2657 module_deallocate(mod, &info);
2658 free_copy:
2659 free_copy(&info);
Jayachandran C6fe2e702006-01-06 00:19:54 -08002660 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661}
2662
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002663/* Call module constructors. */
2664static void do_mod_ctors(struct module *mod)
2665{
2666#ifdef CONFIG_CONSTRUCTORS
2667 unsigned long i;
2668
2669 for (i = 0; i < mod->num_ctors; i++)
2670 mod->ctors[i]();
2671#endif
2672}
2673
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674/* This is where the real work happens */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002675SYSCALL_DEFINE3(init_module, void __user *, umod,
2676 unsigned long, len, const char __user *, uargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677{
2678 struct module *mod;
2679 int ret = 0;
2680
2681 /* Must have permission */
Kees Cook3d433212009-04-02 15:49:29 -07002682 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 return -EPERM;
2684
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 /* Do all the hard work */
2686 mod = load_module(umod, len, uargs);
Rusty Russell75676502010-06-05 11:17:36 -06002687 if (IS_ERR(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 return PTR_ERR(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
Alan Sterne041c682006-03-27 01:16:30 -08002690 blocking_notifier_call_chain(&module_notify_list,
2691 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002693 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 /* Start the module */
2695 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002696 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 if (ret < 0) {
2698 /* Init routine failed: abort. Try to protect us from
2699 buggy refcounters. */
2700 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002701 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002702 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002703 blocking_notifier_call_chain(&module_notify_list,
2704 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002705 free_module(mod);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002706 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 return ret;
2708 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002709 if (ret > 0) {
Joe Perchesad361c92009-07-06 13:05:40 -07002710 printk(KERN_WARNING
2711"%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
2712"%s: loading module anyway...\n",
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002713 __func__, mod->name, ret,
2714 __func__);
2715 dump_stack();
2716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
Rusty Russell6c5db222008-03-10 11:43:52 -07002718 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002720 wake_up(&module_wq);
Masami Hiramatsu0deddf432009-01-06 14:41:54 -08002721 blocking_notifier_call_chain(&module_notify_list,
2722 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07002723
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07002724 /* We need to finish all async code before the module init sequence is done */
2725 async_synchronize_full();
2726
Rusty Russell6c5db222008-03-10 11:43:52 -07002727 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 /* Drop initial reference. */
2729 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06002730 trim_init_extable(mod);
Jan Beulich4a496222009-07-06 14:50:42 +01002731#ifdef CONFIG_KALLSYMS
2732 mod->num_symtab = mod->core_num_syms;
2733 mod->symtab = mod->core_symtab;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002734 mod->strtab = mod->core_strtab;
Jan Beulich4a496222009-07-06 14:50:42 +01002735#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 module_free(mod, mod->module_init);
2737 mod->module_init = NULL;
2738 mod->init_size = 0;
2739 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002740 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
2742 return 0;
2743}
2744
2745static inline int within(unsigned long addr, void *start, unsigned long size)
2746{
2747 return ((void *)addr >= start && (void *)addr < start + size);
2748}
2749
2750#ifdef CONFIG_KALLSYMS
2751/*
2752 * This ignores the intensely annoying "mapping symbols" found
2753 * in ARM ELF files: $a, $t and $d.
2754 */
2755static inline int is_arm_mapping_symbol(const char *str)
2756{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002757 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 && (str[2] == '\0' || str[2] == '.');
2759}
2760
2761static const char *get_ksymbol(struct module *mod,
2762 unsigned long addr,
2763 unsigned long *size,
2764 unsigned long *offset)
2765{
2766 unsigned int i, best = 0;
2767 unsigned long nextval;
2768
2769 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002770 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002772 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2774
2775 /* Scan for closest preceeding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002776 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 for (i = 1; i < mod->num_symtab; i++) {
2778 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2779 continue;
2780
2781 /* We ignore unnamed symbols: they're uninformative
2782 * and inserted at a whim. */
2783 if (mod->symtab[i].st_value <= addr
2784 && mod->symtab[i].st_value > mod->symtab[best].st_value
2785 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2786 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2787 best = i;
2788 if (mod->symtab[i].st_value > addr
2789 && mod->symtab[i].st_value < nextval
2790 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2791 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2792 nextval = mod->symtab[i].st_value;
2793 }
2794
2795 if (!best)
2796 return NULL;
2797
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002798 if (size)
2799 *size = nextval - mod->symtab[best].st_value;
2800 if (offset)
2801 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 return mod->strtab + mod->symtab[best].st_name;
2803}
2804
Rusty Russell6dd06c92008-01-29 17:13:22 -05002805/* For kallsyms to ask for address resolution. NULL means not found. Careful
2806 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002807const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05002808 unsigned long *size,
2809 unsigned long *offset,
2810 char **modname,
2811 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812{
2813 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002814 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
Rusty Russellcb2a5202008-01-14 00:55:03 -08002816 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002817 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002818 if (within_module_init(addr, mod) ||
2819 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002820 if (modname)
2821 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002822 ret = get_ksymbol(mod, addr, size, offset);
2823 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 }
2825 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05002826 /* Make a copy in here where it's safe */
2827 if (ret) {
2828 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2829 ret = namebuf;
2830 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002831 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002832 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833}
2834
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002835int lookup_module_symbol_name(unsigned long addr, char *symname)
2836{
2837 struct module *mod;
2838
Rusty Russellcb2a5202008-01-14 00:55:03 -08002839 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002840 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002841 if (within_module_init(addr, mod) ||
2842 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002843 const char *sym;
2844
2845 sym = get_ksymbol(mod, addr, NULL, NULL);
2846 if (!sym)
2847 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07002848 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002849 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002850 return 0;
2851 }
2852 }
2853out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002854 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002855 return -ERANGE;
2856}
2857
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002858int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2859 unsigned long *offset, char *modname, char *name)
2860{
2861 struct module *mod;
2862
Rusty Russellcb2a5202008-01-14 00:55:03 -08002863 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002864 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002865 if (within_module_init(addr, mod) ||
2866 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002867 const char *sym;
2868
2869 sym = get_ksymbol(mod, addr, size, offset);
2870 if (!sym)
2871 goto out;
2872 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07002873 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002874 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07002875 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002876 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002877 return 0;
2878 }
2879 }
2880out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002881 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002882 return -ERANGE;
2883}
2884
Alexey Dobriyanea078902007-05-08 00:28:39 -07002885int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2886 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887{
2888 struct module *mod;
2889
Rusty Russellcb2a5202008-01-14 00:55:03 -08002890 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002891 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 if (symnum < mod->num_symtab) {
2893 *value = mod->symtab[symnum].st_value;
2894 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002895 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07002896 KSYM_NAME_LEN);
2897 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06002898 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002899 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002900 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 }
2902 symnum -= mod->num_symtab;
2903 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002904 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002905 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906}
2907
2908static unsigned long mod_find_symname(struct module *mod, const char *name)
2909{
2910 unsigned int i;
2911
2912 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002913 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2914 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 return mod->symtab[i].st_value;
2916 return 0;
2917}
2918
2919/* Look for this name: can be of form module:name. */
2920unsigned long module_kallsyms_lookup_name(const char *name)
2921{
2922 struct module *mod;
2923 char *colon;
2924 unsigned long ret = 0;
2925
2926 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08002927 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 if ((colon = strchr(name, ':')) != NULL) {
2929 *colon = '\0';
2930 if ((mod = find_module(name)) != NULL)
2931 ret = mod_find_symname(mod, colon+1);
2932 *colon = ':';
2933 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02002934 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 if ((ret = mod_find_symname(mod, name)) != 0)
2936 break;
2937 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002938 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 return ret;
2940}
Anders Kaseorg75a66612008-12-05 19:03:58 -05002941
2942int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
2943 struct module *, unsigned long),
2944 void *data)
2945{
2946 struct module *mod;
2947 unsigned int i;
2948 int ret;
2949
2950 list_for_each_entry(mod, &modules, list) {
2951 for (i = 0; i < mod->num_symtab; i++) {
2952 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
2953 mod, mod->symtab[i].st_value);
2954 if (ret != 0)
2955 return ret;
2956 }
2957 }
2958 return 0;
2959}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960#endif /* CONFIG_KALLSYMS */
2961
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002962static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002963{
2964 int bx = 0;
2965
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002966 if (mod->taints ||
2967 mod->state == MODULE_STATE_GOING ||
2968 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002969 buf[bx++] = '(';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002970 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002971 buf[bx++] = 'P';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002972 if (mod->taints & (1 << TAINT_FORCED_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002973 buf[bx++] = 'F';
Linus Torvalds26e9a392008-10-17 09:50:12 -07002974 if (mod->taints & (1 << TAINT_CRAP))
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002975 buf[bx++] = 'C';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002976 /*
2977 * TAINT_FORCED_RMMOD: could be added.
2978 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2979 * apply to modules.
2980 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002981
2982 /* Show a - for module-is-being-unloaded */
2983 if (mod->state == MODULE_STATE_GOING)
2984 buf[bx++] = '-';
2985 /* Show a + for module-is-being-loaded */
2986 if (mod->state == MODULE_STATE_COMING)
2987 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002988 buf[bx++] = ')';
2989 }
2990 buf[bx] = '\0';
2991
2992 return buf;
2993}
2994
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002995#ifdef CONFIG_PROC_FS
2996/* Called by the /proc file system to return a list of modules. */
2997static void *m_start(struct seq_file *m, loff_t *pos)
2998{
2999 mutex_lock(&module_mutex);
3000 return seq_list_start(&modules, *pos);
3001}
3002
3003static void *m_next(struct seq_file *m, void *p, loff_t *pos)
3004{
3005 return seq_list_next(p, &modules, pos);
3006}
3007
3008static void m_stop(struct seq_file *m, void *p)
3009{
3010 mutex_unlock(&module_mutex);
3011}
3012
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013static int m_show(struct seq_file *m, void *p)
3014{
3015 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003016 char buf[8];
3017
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05003018 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 mod->name, mod->init_size + mod->core_size);
3020 print_unload_info(m, mod);
3021
3022 /* Informative for users. */
3023 seq_printf(m, " %s",
3024 mod->state == MODULE_STATE_GOING ? "Unloading":
3025 mod->state == MODULE_STATE_COMING ? "Loading":
3026 "Live");
3027 /* Used by oprofile and other similar tools. */
3028 seq_printf(m, " 0x%p", mod->module_core);
3029
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003030 /* Taints info */
3031 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003032 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003033
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 seq_printf(m, "\n");
3035 return 0;
3036}
3037
3038/* Format: modulename size refcount deps address
3039
3040 Where refcount is a number or -, and deps is a comma-separated list
3041 of depends or -.
3042*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003043static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 .start = m_start,
3045 .next = m_next,
3046 .stop = m_stop,
3047 .show = m_show
3048};
3049
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003050static int modules_open(struct inode *inode, struct file *file)
3051{
3052 return seq_open(file, &modules_op);
3053}
3054
3055static const struct file_operations proc_modules_operations = {
3056 .open = modules_open,
3057 .read = seq_read,
3058 .llseek = seq_lseek,
3059 .release = seq_release,
3060};
3061
3062static int __init proc_modules_init(void)
3063{
3064 proc_create("modules", 0, NULL, &proc_modules_operations);
3065 return 0;
3066}
3067module_init(proc_modules_init);
3068#endif
3069
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070/* Given an address, look for it in the module exception tables. */
3071const struct exception_table_entry *search_module_extables(unsigned long addr)
3072{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 const struct exception_table_entry *e = NULL;
3074 struct module *mod;
3075
Rusty Russell24da1cb2007-07-15 23:41:46 -07003076 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003077 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 if (mod->num_exentries == 0)
3079 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003080
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 e = search_extable(mod->extable,
3082 mod->extable + mod->num_exentries - 1,
3083 addr);
3084 if (e)
3085 break;
3086 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07003087 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
3089 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07003090 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 return e;
3092}
3093
Ingo Molnar4d435f92006-07-03 00:24:24 -07003094/*
Rusty Russelle6104992009-03-31 13:05:31 -06003095 * is_module_address - is this address inside a module?
3096 * @addr: the address to check.
3097 *
3098 * See is_module_text_address() if you simply want to see if the address
3099 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07003100 */
Rusty Russelle6104992009-03-31 13:05:31 -06003101bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07003102{
Rusty Russelle6104992009-03-31 13:05:31 -06003103 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003104
Rusty Russell24da1cb2007-07-15 23:41:46 -07003105 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06003106 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07003107 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07003108
Rusty Russelle6104992009-03-31 13:05:31 -06003109 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003110}
3111
Rusty Russelle6104992009-03-31 13:05:31 -06003112/*
3113 * __module_address - get the module which contains an address.
3114 * @addr: the address.
3115 *
3116 * Must be called with preempt disabled or module mutex held so that
3117 * module doesn't get freed during this.
3118 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07003119struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120{
3121 struct module *mod;
3122
Rusty Russell3a642e92008-07-22 19:24:28 -05003123 if (addr < module_addr_min || addr > module_addr_max)
3124 return NULL;
3125
Andi Kleend72b3752008-08-30 10:09:00 +02003126 list_for_each_entry_rcu(mod, &modules, list)
Rusty Russelle6104992009-03-31 13:05:31 -06003127 if (within_module_core(addr, mod)
3128 || within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 return mod;
3130 return NULL;
3131}
Tim Abbottc6b37802008-12-05 19:03:59 -05003132EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
Rusty Russelle6104992009-03-31 13:05:31 -06003134/*
3135 * is_module_text_address - is this address inside module code?
3136 * @addr: the address to check.
3137 *
3138 * See is_module_address() if you simply want to see if the address is
3139 * anywhere in a module. See kernel_text_address() for testing if an
3140 * address corresponds to kernel or module code.
3141 */
3142bool is_module_text_address(unsigned long addr)
3143{
3144 bool ret;
3145
3146 preempt_disable();
3147 ret = __module_text_address(addr) != NULL;
3148 preempt_enable();
3149
3150 return ret;
3151}
3152
3153/*
3154 * __module_text_address - get the module whose code contains an address.
3155 * @addr: the address.
3156 *
3157 * Must be called with preempt disabled or module mutex held so that
3158 * module doesn't get freed during this.
3159 */
3160struct module *__module_text_address(unsigned long addr)
3161{
3162 struct module *mod = __module_address(addr);
3163 if (mod) {
3164 /* Make sure it's within the text section. */
3165 if (!within(addr, mod->module_init, mod->init_text_size)
3166 && !within(addr, mod->module_core, mod->core_text_size))
3167 mod = NULL;
3168 }
3169 return mod;
3170}
Tim Abbottc6b37802008-12-05 19:03:59 -05003171EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06003172
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173/* Don't grab lock, we're oopsing. */
3174void print_modules(void)
3175{
3176 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07003177 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
Linus Torvaldsb2311252009-06-16 11:07:14 -07003179 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02003180 /* Most callers should already have preempt disabled, but make sure */
3181 preempt_disable();
3182 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003183 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02003184 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01003185 if (last_unloaded_module[0])
3186 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 printk("\n");
3188}
3189
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06003191/* Generate the signature for all relevant module structures here.
3192 * If these change, we don't want to try to parse the module. */
3193void module_layout(struct module *mod,
3194 struct modversion_info *ver,
3195 struct kernel_param *kp,
3196 struct kernel_symbol *ks,
Rusty Russell8c8ef422009-03-31 13:05:34 -06003197 struct tracepoint *tp)
3198{
3199}
3200EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07003202
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04003203#ifdef CONFIG_TRACEPOINTS
3204void module_update_tracepoints(void)
3205{
3206 struct module *mod;
3207
3208 mutex_lock(&module_mutex);
3209 list_for_each_entry(mod, &modules, list)
3210 if (!mod->taints)
3211 tracepoint_update_probe_range(mod->tracepoints,
3212 mod->tracepoints + mod->num_tracepoints);
3213 mutex_unlock(&module_mutex);
3214}
3215
3216/*
3217 * Returns 0 if current not found.
3218 * Returns 1 if current found.
3219 */
3220int module_get_iter_tracepoints(struct tracepoint_iter *iter)
3221{
3222 struct module *iter_mod;
3223 int found = 0;
3224
3225 mutex_lock(&module_mutex);
3226 list_for_each_entry(iter_mod, &modules, list) {
3227 if (!iter_mod->taints) {
3228 /*
3229 * Sorted module list
3230 */
3231 if (iter_mod < iter->module)
3232 continue;
3233 else if (iter_mod > iter->module)
3234 iter->tracepoint = NULL;
3235 found = tracepoint_get_iter_range(&iter->tracepoint,
3236 iter_mod->tracepoints,
3237 iter_mod->tracepoints
3238 + iter_mod->num_tracepoints);
3239 if (found) {
3240 iter->module = iter_mod;
3241 break;
3242 }
3243 }
3244 }
3245 mutex_unlock(&module_mutex);
3246 return found;
3247}
3248#endif