blob: eba134157ef64c738c24c7eeeafbd3547989633d [file] [log] [blame]
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 Copyright (C) 2002 Richard Henderson
Rusty Russell51f3d0f2010-08-05 12:59:13 -06003 Copyright (C) 2001 Rusty Russell, 2002, 2010 Rusty Russell IBM.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
20#include <linux/moduleloader.h>
Steven Rostedt6d723732009-04-10 14:53:50 -040021#include <linux/ftrace_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
Alexey Dobriyanae84e322007-05-08 00:28:38 -070023#include <linux/kallsyms.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040024#include <linux/fs.h>
Roland McGrath6d760132007-10-16 23:26:40 -070025#include <linux/sysfs.h>
Randy Dunlap9f158332005-09-13 01:25:16 -070026#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/slab.h>
28#include <linux/vmalloc.h>
29#include <linux/elf.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040030#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/seq_file.h>
32#include <linux/syscalls.h>
33#include <linux/fcntl.h>
34#include <linux/rcupdate.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080035#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/cpu.h>
37#include <linux/moduleparam.h>
38#include <linux/errno.h>
39#include <linux/err.h>
40#include <linux/vermagic.h>
41#include <linux/notifier.h>
Al Virof6a57032006-10-18 01:47:25 -040042#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/stop_machine.h>
44#include <linux/device.h>
Matt Domschc988d2b2005-06-23 22:05:15 -070045#include <linux/string.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080046#include <linux/mutex.h>
Andi Kleend72b3752008-08-30 10:09:00 +020047#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/cacheflush.h>
Bernd Schmidteb8cdec2009-09-21 17:03:57 -070050#include <asm/mmu_context.h>
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020051#include <linux/license.h>
Christoph Lameter6d762392008-02-08 04:18:42 -080052#include <asm/sections.h>
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040053#include <linux/tracepoint.h>
Steven Rostedt90d595f2008-08-14 15:45:09 -040054#include <linux/ftrace.h>
Arjan van de Ven22a9d642009-01-07 08:45:46 -080055#include <linux/async.h>
Tejun Heofbf59bc2009-02-20 16:29:08 +090056#include <linux/percpu.h>
Catalin Marinas4f2294b2009-06-11 13:23:20 +010057#include <linux/kmemleak.h>
Jason Baronbf5438fc2010-09-17 11:09:00 -040058#include <linux/jump_label.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Li Zefan7ead8b82009-08-17 16:56:28 +080060#define CREATE_TRACE_POINTS
61#include <trace/events/module.h>
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#if 0
64#define DEBUGP printk
65#else
66#define DEBUGP(fmt , a...)
67#endif
68
69#ifndef ARCH_SHF_SMALL
70#define ARCH_SHF_SMALL 0
71#endif
72
73/* If this is set, the section belongs in the init part of the module */
74#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
75
Rusty Russell75676502010-06-05 11:17:36 -060076/*
77 * Mutex protects:
78 * 1) List of modules (also safely readable with preempt_disable),
79 * 2) module_use links,
80 * 3) module_addr_min/module_addr_max.
Andi Kleend72b3752008-08-30 10:09:00 +020081 * (delete uses stop_machine/add uses RCU list operations). */
Tim Abbottc6b37802008-12-05 19:03:59 -050082DEFINE_MUTEX(module_mutex);
83EXPORT_SYMBOL_GPL(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static LIST_HEAD(modules);
Jason Wessel67fc4e02010-05-20 21:04:21 -050085#ifdef CONFIG_KGDB_KDB
86struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
87#endif /* CONFIG_KGDB_KDB */
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Stephen Rothwell19e45292009-04-14 17:27:18 +100090/* Block module loading/unloading? */
91int modules_disabled = 0;
92
Rusty Russellc9a3ba52008-01-29 17:13:18 -050093/* Waiting for a module to finish initializing? */
94static DECLARE_WAIT_QUEUE_HEAD(module_wq);
95
Alan Sterne041c682006-03-27 01:16:30 -080096static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Rusty Russell75676502010-06-05 11:17:36 -060098/* Bounds of module allocation, for speeding __module_address.
99 * Protected by module_mutex. */
Rusty Russell3a642e92008-07-22 19:24:28 -0500100static unsigned long module_addr_min = -1UL, module_addr_max = 0;
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102int register_module_notifier(struct notifier_block * nb)
103{
Alan Sterne041c682006-03-27 01:16:30 -0800104 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106EXPORT_SYMBOL(register_module_notifier);
107
108int unregister_module_notifier(struct notifier_block * nb)
109{
Alan Sterne041c682006-03-27 01:16:30 -0800110 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112EXPORT_SYMBOL(unregister_module_notifier);
113
Rusty Russelleded41c2010-08-05 12:59:07 -0600114struct load_info {
115 Elf_Ehdr *hdr;
116 unsigned long len;
117 Elf_Shdr *sechdrs;
Rusty Russell6526c532010-08-05 12:59:10 -0600118 char *secstrings, *strtab;
Rusty Russelld9131882010-08-05 12:59:08 -0600119 unsigned long *strmap;
120 unsigned long symoffs, stroffs;
Rusty Russell811d66a2010-08-05 12:59:12 -0600121 struct _ddebug *debug;
122 unsigned int num_debug;
Rusty Russelleded41c2010-08-05 12:59:07 -0600123 struct {
124 unsigned int sym, str, mod, vers, info, pcpu;
125 } index;
126};
127
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -0800128/* We require a truly strong try_module_get(): 0 means failure due to
129 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static inline int strong_try_module_get(struct module *mod)
131{
132 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500133 return -EBUSY;
134 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500136 else
137 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700140static inline void add_taint_module(struct module *mod, unsigned flag)
141{
142 add_taint(flag);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700143 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700144}
145
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200146/*
147 * A thread that wants to hold a reference to a module only while it
148 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 */
150void __module_put_and_exit(struct module *mod, long code)
151{
152 module_put(mod);
153 do_exit(code);
154}
155EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/* Find a module section: 0 means not found. */
Rusty Russell49668682010-08-05 12:59:10 -0600158static unsigned int find_sec(const struct load_info *info, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 unsigned int i;
161
Rusty Russell49668682010-08-05 12:59:10 -0600162 for (i = 1; i < info->hdr->e_shnum; i++) {
163 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 /* Alloc bit cleared means "ignore it." */
Rusty Russell49668682010-08-05 12:59:10 -0600165 if ((shdr->sh_flags & SHF_ALLOC)
166 && strcmp(info->secstrings + shdr->sh_name, name) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return i;
Rusty Russell49668682010-08-05 12:59:10 -0600168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return 0;
170}
171
Rusty Russell5e458cc2008-10-22 10:00:13 -0500172/* Find a module section, or NULL. */
Rusty Russell49668682010-08-05 12:59:10 -0600173static void *section_addr(const struct load_info *info, const char *name)
Rusty Russell5e458cc2008-10-22 10:00:13 -0500174{
175 /* Section 0 has sh_addr 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600176 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500177}
178
179/* Find a module section, or NULL. Fill in number of "objects" in section. */
Rusty Russell49668682010-08-05 12:59:10 -0600180static void *section_objs(const struct load_info *info,
Rusty Russell5e458cc2008-10-22 10:00:13 -0500181 const char *name,
182 size_t object_size,
183 unsigned int *num)
184{
Rusty Russell49668682010-08-05 12:59:10 -0600185 unsigned int sec = find_sec(info, name);
Rusty Russell5e458cc2008-10-22 10:00:13 -0500186
187 /* Section 0 has sh_addr 0 and sh_size 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600188 *num = info->sechdrs[sec].sh_size / object_size;
189 return (void *)info->sechdrs[sec].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500190}
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/* Provided by the linker */
193extern const struct kernel_symbol __start___ksymtab[];
194extern const struct kernel_symbol __stop___ksymtab[];
195extern const struct kernel_symbol __start___ksymtab_gpl[];
196extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800197extern const struct kernel_symbol __start___ksymtab_gpl_future[];
198extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199extern const unsigned long __start___kcrctab[];
200extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800201extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500202#ifdef CONFIG_UNUSED_SYMBOLS
203extern const struct kernel_symbol __start___ksymtab_unused[];
204extern const struct kernel_symbol __stop___ksymtab_unused[];
205extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
206extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700207extern const unsigned long __start___kcrctab_unused[];
208extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500209#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211#ifndef CONFIG_MODVERSIONS
212#define symversion(base, idx) NULL
213#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800214#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215#endif
216
Rusty Russelldafd0942008-07-22 19:24:25 -0500217static bool each_symbol_in_section(const struct symsearch *arr,
218 unsigned int arrsize,
219 struct module *owner,
220 bool (*fn)(const struct symsearch *syms,
221 struct module *owner,
222 unsigned int symnum, void *data),
223 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100224{
Rusty Russelldafd0942008-07-22 19:24:25 -0500225 unsigned int i, j;
226
227 for (j = 0; j < arrsize; j++) {
228 for (i = 0; i < arr[j].stop - arr[j].start; i++)
229 if (fn(&arr[j], owner, i, data))
230 return true;
231 }
232
233 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100234}
235
Rusty Russelldafd0942008-07-22 19:24:25 -0500236/* Returns true as soon as fn returns true, otherwise false. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500237bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
238 unsigned int symnum, void *data), void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700239{
Rusty Russelldafd0942008-07-22 19:24:25 -0500240 struct module *mod;
Linus Torvalds44032e62010-08-05 12:59:05 -0600241 static const struct symsearch arr[] = {
Rusty Russelldafd0942008-07-22 19:24:25 -0500242 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
243 NOT_GPL_ONLY, false },
244 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
245 __start___kcrctab_gpl,
246 GPL_ONLY, false },
247 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
248 __start___kcrctab_gpl_future,
249 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500250#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500251 { __start___ksymtab_unused, __stop___ksymtab_unused,
252 __start___kcrctab_unused,
253 NOT_GPL_ONLY, true },
254 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
255 __start___kcrctab_unused_gpl,
256 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500257#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500258 };
259
260 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
261 return true;
262
Andi Kleend72b3752008-08-30 10:09:00 +0200263 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500264 struct symsearch arr[] = {
265 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
266 NOT_GPL_ONLY, false },
267 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
268 mod->gpl_crcs,
269 GPL_ONLY, false },
270 { mod->gpl_future_syms,
271 mod->gpl_future_syms + mod->num_gpl_future_syms,
272 mod->gpl_future_crcs,
273 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500274#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500275 { mod->unused_syms,
276 mod->unused_syms + mod->num_unused_syms,
277 mod->unused_crcs,
278 NOT_GPL_ONLY, true },
279 { mod->unused_gpl_syms,
280 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
281 mod->unused_gpl_crcs,
282 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500283#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500284 };
285
286 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
287 return true;
288 }
289 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700290}
Tim Abbottc6b37802008-12-05 19:03:59 -0500291EXPORT_SYMBOL_GPL(each_symbol);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700292
Rusty Russelldafd0942008-07-22 19:24:25 -0500293struct find_symbol_arg {
294 /* Input */
295 const char *name;
296 bool gplok;
297 bool warn;
298
299 /* Output */
300 struct module *owner;
301 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500302 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500303};
304
305static bool find_symbol_in_section(const struct symsearch *syms,
306 struct module *owner,
307 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500308{
Rusty Russelldafd0942008-07-22 19:24:25 -0500309 struct find_symbol_arg *fsa = data;
310
311 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
312 return false;
313
314 if (!fsa->gplok) {
315 if (syms->licence == GPL_ONLY)
316 return false;
317 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
318 printk(KERN_WARNING "Symbol %s is being used "
319 "by a non-GPL module, which will not "
320 "be allowed in the future\n", fsa->name);
321 printk(KERN_WARNING "Please see the file "
322 "Documentation/feature-removal-schedule.txt "
323 "in the kernel source tree for more details.\n");
324 }
325 }
326
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500327#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500328 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500329 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500330 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500331 printk(KERN_WARNING
332 "This symbol will go away in the future.\n");
333 printk(KERN_WARNING
334 "Please evalute if this is the right api to use and if "
335 "it really is, submit a report the linux kernel "
336 "mailinglist together with submitting your code for "
337 "inclusion.\n");
338 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500339#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500340
341 fsa->owner = owner;
342 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500343 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500344 return true;
345}
346
Tim Abbott414fd312008-12-05 19:03:56 -0500347/* Find a symbol and return it, along with, (optional) crc and
Rusty Russell75676502010-06-05 11:17:36 -0600348 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500349const struct kernel_symbol *find_symbol(const char *name,
350 struct module **owner,
351 const unsigned long **crc,
352 bool gplok,
353 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Rusty Russelldafd0942008-07-22 19:24:25 -0500355 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Rusty Russelldafd0942008-07-22 19:24:25 -0500357 fsa.name = name;
358 fsa.gplok = gplok;
359 fsa.warn = warn;
360
361 if (each_symbol(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500362 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500363 *owner = fsa.owner;
364 if (crc)
365 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500366 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 DEBUGP("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500370 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
Tim Abbottc6b37802008-12-05 19:03:59 -0500372EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374/* Search for module by name: must hold module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500375struct module *find_module(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 struct module *mod;
378
379 list_for_each_entry(mod, &modules, list) {
380 if (strcmp(mod->name, name) == 0)
381 return mod;
382 }
383 return NULL;
384}
Tim Abbottc6b37802008-12-05 19:03:59 -0500385EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900388
Tejun Heo259354d2010-03-10 18:56:10 +0900389static inline void __percpu *mod_percpu(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900390{
Tejun Heo259354d2010-03-10 18:56:10 +0900391 return mod->percpu;
392}
Tejun Heofbf59bc2009-02-20 16:29:08 +0900393
Tejun Heo259354d2010-03-10 18:56:10 +0900394static int percpu_modalloc(struct module *mod,
395 unsigned long size, unsigned long align)
396{
Tejun Heofbf59bc2009-02-20 16:29:08 +0900397 if (align > PAGE_SIZE) {
398 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
Tejun Heo259354d2010-03-10 18:56:10 +0900399 mod->name, align, PAGE_SIZE);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900400 align = PAGE_SIZE;
401 }
402
Tejun Heo259354d2010-03-10 18:56:10 +0900403 mod->percpu = __alloc_reserved_percpu(size, align);
404 if (!mod->percpu) {
Tejun Heofbf59bc2009-02-20 16:29:08 +0900405 printk(KERN_WARNING
Rusty Russelld9131882010-08-05 12:59:08 -0600406 "%s: Could not allocate %lu bytes percpu data\n",
407 mod->name, size);
Tejun Heo259354d2010-03-10 18:56:10 +0900408 return -ENOMEM;
409 }
410 mod->percpu_size = size;
411 return 0;
Tejun Heofbf59bc2009-02-20 16:29:08 +0900412}
413
Tejun Heo259354d2010-03-10 18:56:10 +0900414static void percpu_modfree(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900415{
Tejun Heo259354d2010-03-10 18:56:10 +0900416 free_percpu(mod->percpu);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900417}
418
Rusty Russell49668682010-08-05 12:59:10 -0600419static unsigned int find_pcpusec(struct load_info *info)
Tejun Heo6b588c12009-02-20 16:29:07 +0900420{
Rusty Russell49668682010-08-05 12:59:10 -0600421 return find_sec(info, ".data..percpu");
Tejun Heo6b588c12009-02-20 16:29:07 +0900422}
423
Tejun Heo259354d2010-03-10 18:56:10 +0900424static void percpu_modcopy(struct module *mod,
425 const void *from, unsigned long size)
Tejun Heo6b588c12009-02-20 16:29:07 +0900426{
427 int cpu;
428
429 for_each_possible_cpu(cpu)
Tejun Heo259354d2010-03-10 18:56:10 +0900430 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
Tejun Heo6b588c12009-02-20 16:29:07 +0900431}
432
Tejun Heo10fad5e2010-03-10 18:57:54 +0900433/**
434 * is_module_percpu_address - test whether address is from module static percpu
435 * @addr: address to test
436 *
437 * Test whether @addr belongs to module static percpu area.
438 *
439 * RETURNS:
440 * %true if @addr is from module static percpu area
441 */
442bool is_module_percpu_address(unsigned long addr)
443{
444 struct module *mod;
445 unsigned int cpu;
446
447 preempt_disable();
448
449 list_for_each_entry_rcu(mod, &modules, list) {
450 if (!mod->percpu_size)
451 continue;
452 for_each_possible_cpu(cpu) {
453 void *start = per_cpu_ptr(mod->percpu, cpu);
454
455 if ((void *)addr >= start &&
456 (void *)addr < start + mod->percpu_size) {
457 preempt_enable();
458 return true;
459 }
460 }
461 }
462
463 preempt_enable();
464 return false;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700465}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900468
Tejun Heo259354d2010-03-10 18:56:10 +0900469static inline void __percpu *mod_percpu(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 return NULL;
472}
Tejun Heo259354d2010-03-10 18:56:10 +0900473static inline int percpu_modalloc(struct module *mod,
474 unsigned long size, unsigned long align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Tejun Heo259354d2010-03-10 18:56:10 +0900476 return -ENOMEM;
477}
478static inline void percpu_modfree(struct module *mod)
479{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
Rusty Russell49668682010-08-05 12:59:10 -0600481static unsigned int find_pcpusec(struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 return 0;
484}
Tejun Heo259354d2010-03-10 18:56:10 +0900485static inline void percpu_modcopy(struct module *mod,
486 const void *from, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 /* pcpusec should be 0, and size of that section should be 0. */
489 BUG_ON(size != 0);
490}
Tejun Heo10fad5e2010-03-10 18:57:54 +0900491bool is_module_percpu_address(unsigned long addr)
492{
493 return false;
494}
Tejun Heo6b588c12009-02-20 16:29:07 +0900495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496#endif /* CONFIG_SMP */
497
Matt Domschc988d2b2005-06-23 22:05:15 -0700498#define MODINFO_ATTR(field) \
499static void setup_modinfo_##field(struct module *mod, const char *s) \
500{ \
501 mod->field = kstrdup(s, GFP_KERNEL); \
502} \
503static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
504 struct module *mod, char *buffer) \
505{ \
506 return sprintf(buffer, "%s\n", mod->field); \
507} \
508static int modinfo_##field##_exists(struct module *mod) \
509{ \
510 return mod->field != NULL; \
511} \
512static void free_modinfo_##field(struct module *mod) \
513{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700514 kfree(mod->field); \
515 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700516} \
517static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900518 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700519 .show = show_modinfo_##field, \
520 .setup = setup_modinfo_##field, \
521 .test = modinfo_##field##_exists, \
522 .free = free_modinfo_##field, \
523};
524
525MODINFO_ATTR(version);
526MODINFO_ATTR(srcversion);
527
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100528static char last_unloaded_module[MODULE_NAME_LEN+1];
529
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800530#ifdef CONFIG_MODULE_UNLOAD
Steven Rostedteb0c5372010-03-29 14:25:18 -0400531
532EXPORT_TRACEPOINT_SYMBOL(module_get);
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534/* Init the unload section of the module. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600535static int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600537 mod->refptr = alloc_percpu(struct module_ref);
538 if (!mod->refptr)
539 return -ENOMEM;
540
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700541 INIT_LIST_HEAD(&mod->source_list);
542 INIT_LIST_HEAD(&mod->target_list);
Christoph Lametere1783a22010-01-05 15:34:50 +0900543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 /* Hold reference count during initialization. */
Nick Piggin5fbfb182010-04-01 19:09:40 +1100545 __this_cpu_write(mod->refptr->incs, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /* Backwards compatibility macros put refcount during init. */
547 mod->waiter = current;
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600548
549 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552/* Does a already use b? */
553static int already_uses(struct module *a, struct module *b)
554{
555 struct module_use *use;
556
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700557 list_for_each_entry(use, &b->source_list, source_list) {
558 if (use->source == a) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 DEBUGP("%s uses %s!\n", a->name, b->name);
560 return 1;
561 }
562 }
563 DEBUGP("%s does not use %s!\n", a->name, b->name);
564 return 0;
565}
566
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700567/*
568 * Module a uses b
569 * - we add 'a' as a "source", 'b' as a "target" of module use
570 * - the module_use is added to the list of 'b' sources (so
571 * 'b' can walk the list to see who sourced them), and of 'a'
572 * targets (so 'a' can see what modules it targets).
573 */
574static int add_module_usage(struct module *a, struct module *b)
575{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700576 struct module_use *use;
577
578 DEBUGP("Allocating new usage for %s.\n", a->name);
579 use = kmalloc(sizeof(*use), GFP_ATOMIC);
580 if (!use) {
581 printk(KERN_WARNING "%s: out of memory loading\n", a->name);
582 return -ENOMEM;
583 }
584
585 use->source = a;
586 use->target = b;
587 list_add(&use->source_list, &b->source_list);
588 list_add(&use->target_list, &a->target_list);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700589 return 0;
590}
591
Rusty Russell75676502010-06-05 11:17:36 -0600592/* Module a uses b: caller needs module_mutex() */
Rusty Russell9bea7f22010-06-05 11:17:37 -0600593int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Rusty Russellc8e21ce2010-06-05 11:17:35 -0600595 int err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100596
Rusty Russell9bea7f22010-06-05 11:17:37 -0600597 if (b == NULL || already_uses(a, b))
Linus Torvalds218ce732010-05-25 16:48:30 -0700598 return 0;
Linus Torvalds218ce732010-05-25 16:48:30 -0700599
Rusty Russell9bea7f22010-06-05 11:17:37 -0600600 /* If module isn't available, we fail. */
601 err = strong_try_module_get(b);
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500602 if (err)
Rusty Russell9bea7f22010-06-05 11:17:37 -0600603 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700605 err = add_module_usage(a, b);
606 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 module_put(b);
Rusty Russell9bea7f22010-06-05 11:17:37 -0600608 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
Rusty Russell9bea7f22010-06-05 11:17:37 -0600610 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600612EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614/* Clear the unload stuff of the module. */
615static void module_unload_free(struct module *mod)
616{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700617 struct module_use *use, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Rusty Russell75676502010-06-05 11:17:36 -0600619 mutex_lock(&module_mutex);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700620 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
621 struct module *i = use->target;
622 DEBUGP("%s unusing %s\n", mod->name, i->name);
623 module_put(i);
624 list_del(&use->source_list);
625 list_del(&use->target_list);
626 kfree(use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
Rusty Russell75676502010-06-05 11:17:36 -0600628 mutex_unlock(&module_mutex);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600629
630 free_percpu(mod->refptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
633#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800634static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 int ret = (flags & O_TRUNC);
637 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800638 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return ret;
640}
641#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800642static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 return 0;
645}
646#endif /* CONFIG_MODULE_FORCE_UNLOAD */
647
648struct stopref
649{
650 struct module *mod;
651 int flags;
652 int *forced;
653};
654
655/* Whole machine is stopped with interrupts off when this runs. */
656static int __try_stop_module(void *_sref)
657{
658 struct stopref *sref = _sref;
659
Rusty Russellda39ba52008-07-22 19:24:25 -0500660 /* If it's not unused, quit unless we're forcing. */
661 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800662 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return -EWOULDBLOCK;
664 }
665
666 /* Mark it as dying. */
667 sref->mod->state = MODULE_STATE_GOING;
668 return 0;
669}
670
671static int try_stop_module(struct module *mod, int flags, int *forced)
672{
Rusty Russellda39ba52008-07-22 19:24:25 -0500673 if (flags & O_NONBLOCK) {
674 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500676 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500677 } else {
678 /* We don't need to stop the machine for this. */
679 mod->state = MODULE_STATE_GOING;
680 synchronize_sched();
681 return 0;
682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
685unsigned int module_refcount(struct module *mod)
686{
Nick Piggin5fbfb182010-04-01 19:09:40 +1100687 unsigned int incs = 0, decs = 0;
Eric Dumazet720eba32009-02-03 13:31:36 +1030688 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Eric Dumazet720eba32009-02-03 13:31:36 +1030690 for_each_possible_cpu(cpu)
Nick Piggin5fbfb182010-04-01 19:09:40 +1100691 decs += per_cpu_ptr(mod->refptr, cpu)->decs;
692 /*
693 * ensure the incs are added up after the decs.
694 * module_put ensures incs are visible before decs with smp_wmb.
695 *
696 * This 2-count scheme avoids the situation where the refcount
697 * for CPU0 is read, then CPU0 increments the module refcount,
698 * then CPU1 drops that refcount, then the refcount for CPU1 is
699 * read. We would record a decrement but not its corresponding
700 * increment so we would see a low count (disaster).
701 *
702 * Rare situation? But module_refcount can be preempted, and we
703 * might be tallying up 4096+ CPUs. So it is not impossible.
704 */
705 smp_rmb();
706 for_each_possible_cpu(cpu)
707 incs += per_cpu_ptr(mod->refptr, cpu)->incs;
708 return incs - decs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710EXPORT_SYMBOL(module_refcount);
711
712/* This exists whether we can unload or not */
713static void free_module(struct module *mod);
714
715static void wait_for_zero_refcount(struct module *mod)
716{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500717 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800718 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 for (;;) {
720 DEBUGP("Looking at refcount...\n");
721 set_current_state(TASK_UNINTERRUPTIBLE);
722 if (module_refcount(mod) == 0)
723 break;
724 schedule();
725 }
726 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800727 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100730SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
731 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
733 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800734 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 int ret, forced = 0;
736
Kees Cook3d433212009-04-02 15:49:29 -0700737 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800738 return -EPERM;
739
740 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
741 return -EFAULT;
742 name[MODULE_NAME_LEN-1] = '\0';
743
Tejun Heo3fc1f1e2010-05-06 18:49:20 +0200744 if (mutex_lock_interruptible(&module_mutex) != 0)
745 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 mod = find_module(name);
748 if (!mod) {
749 ret = -ENOENT;
750 goto out;
751 }
752
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700753 if (!list_empty(&mod->source_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 /* Other modules depend on us: get rid of them first. */
755 ret = -EWOULDBLOCK;
756 goto out;
757 }
758
759 /* Doing init or already dying? */
760 if (mod->state != MODULE_STATE_LIVE) {
761 /* FIXME: if (force), slam module count and wake up
762 waiter --RR */
763 DEBUGP("%s already dying\n", mod->name);
764 ret = -EBUSY;
765 goto out;
766 }
767
768 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700769 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800770 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (!forced) {
772 /* This module can't be removed */
773 ret = -EBUSY;
774 goto out;
775 }
776 }
777
778 /* Set this up before setting mod->state */
779 mod->waiter = current;
780
781 /* Stop the machine so refcounts can't move and disable module. */
782 ret = try_stop_module(mod, flags, &forced);
783 if (ret != 0)
784 goto out;
785
786 /* Never wait if forced. */
787 if (!forced && module_refcount(mod) != 0)
788 wait_for_zero_refcount(mod);
789
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200790 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 /* Final destruction now noone is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200792 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200794 blocking_notifier_call_chain(&module_notify_list,
795 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800796 async_synchronize_full();
Rusty Russell75676502010-06-05 11:17:36 -0600797
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100798 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500799 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Rusty Russell75676502010-06-05 11:17:36 -0600801 free_module(mod);
802 return 0;
803out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800804 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return ret;
806}
807
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800808static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 struct module_use *use;
811 int printed_something = 0;
812
813 seq_printf(m, " %u ", module_refcount(mod));
814
815 /* Always include a trailing , so userspace can differentiate
816 between this and the old multi-field proc format. */
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700817 list_for_each_entry(use, &mod->source_list, source_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 printed_something = 1;
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700819 seq_printf(m, "%s,", use->source->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 if (mod->init != NULL && mod->exit == NULL) {
823 printed_something = 1;
824 seq_printf(m, "[permanent],");
825 }
826
827 if (!printed_something)
828 seq_printf(m, "-");
829}
830
831void __symbol_put(const char *symbol)
832{
833 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Rusty Russell24da1cb2007-07-15 23:41:46 -0700835 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -0500836 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 BUG();
838 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700839 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841EXPORT_SYMBOL(__symbol_put);
842
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930843/* Note this assumes addr is a function, which it currently always is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844void symbol_put_addr(void *addr)
845{
Trent Piepho5e376612006-05-15 09:44:06 -0700846 struct module *modaddr;
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930847 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930849 if (core_kernel_text(a))
Trent Piepho5e376612006-05-15 09:44:06 -0700850 return;
851
Rusty Russella6e6abd2009-03-31 13:05:31 -0600852 /* module_text_address is safe here: we're supposed to have reference
853 * to module from symbol_get, so it can't go away. */
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930854 modaddr = __module_text_address(a);
Rusty Russella6e6abd2009-03-31 13:05:31 -0600855 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -0700856 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858EXPORT_SYMBOL_GPL(symbol_put_addr);
859
860static ssize_t show_refcnt(struct module_attribute *mattr,
861 struct module *mod, char *buffer)
862{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400863 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
866static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900867 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 .show = show_refcnt,
869};
870
Al Virof6a57032006-10-18 01:47:25 -0400871void module_put(struct module *module)
872{
873 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +0900874 preempt_disable();
Nick Piggin5fbfb182010-04-01 19:09:40 +1100875 smp_wmb(); /* see comment in module_refcount */
876 __this_cpu_inc(module->refptr->decs);
Christoph Lametere1783a22010-01-05 15:34:50 +0900877
Li Zefanae832d12010-03-24 10:57:43 +0800878 trace_module_put(module, _RET_IP_);
Al Virof6a57032006-10-18 01:47:25 -0400879 /* Maybe they're waiting for us to drop reference? */
880 if (unlikely(!module_is_live(module)))
881 wake_up_process(module->waiter);
Christoph Lametere1783a22010-01-05 15:34:50 +0900882 preempt_enable();
Al Virof6a57032006-10-18 01:47:25 -0400883 }
884}
885EXPORT_SYMBOL(module_put);
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800888static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
890 /* We don't know the usage count, or what modules are using. */
891 seq_printf(m, " - -");
892}
893
894static inline void module_unload_free(struct module *mod)
895{
896}
897
Rusty Russell9bea7f22010-06-05 11:17:37 -0600898int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Rusty Russell9bea7f22010-06-05 11:17:37 -0600900 return strong_try_module_get(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600902EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600904static inline int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600906 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908#endif /* CONFIG_MODULE_UNLOAD */
909
Kay Sievers1f717402006-11-24 12:15:25 +0100910static ssize_t show_initstate(struct module_attribute *mattr,
911 struct module *mod, char *buffer)
912{
913 const char *state = "unknown";
914
915 switch (mod->state) {
916 case MODULE_STATE_LIVE:
917 state = "live";
918 break;
919 case MODULE_STATE_COMING:
920 state = "coming";
921 break;
922 case MODULE_STATE_GOING:
923 state = "going";
924 break;
925 }
926 return sprintf(buffer, "%s\n", state);
927}
928
929static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900930 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100931 .show = show_initstate,
932};
933
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800934static struct module_attribute *modinfo_attrs[] = {
935 &modinfo_version,
936 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100937 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800938#ifdef CONFIG_MODULE_UNLOAD
939 &refcnt,
940#endif
941 NULL,
942};
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944static const char vermagic[] = VERMAGIC_STRING;
945
Rusty Russellc6e665c2009-03-31 13:05:33 -0600946static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -0700947{
948#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -0700949 if (!test_taint(TAINT_FORCED_MODULE))
Rusty Russellc6e665c2009-03-31 13:05:33 -0600950 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
951 mod->name, reason);
Linus Torvalds826e4502008-05-04 17:04:16 -0700952 add_taint_module(mod, TAINT_FORCED_MODULE);
953 return 0;
954#else
955 return -ENOEXEC;
956#endif
957}
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959#ifdef CONFIG_MODVERSIONS
Rusty Russelld4703ae2009-12-15 16:28:32 -0600960/* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
961static unsigned long maybe_relocated(unsigned long crc,
962 const struct module *crc_owner)
963{
964#ifdef ARCH_RELOCATES_KCRCTAB
965 if (crc_owner == NULL)
966 return crc - (unsigned long)reloc_start;
967#endif
968 return crc;
969}
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971static int check_version(Elf_Shdr *sechdrs,
972 unsigned int versindex,
973 const char *symname,
974 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -0600975 const unsigned long *crc,
976 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
978 unsigned int i, num_versions;
979 struct modversion_info *versions;
980
981 /* Exporting module didn't supply crcs? OK, we're already tainted. */
982 if (!crc)
983 return 1;
984
Rusty Russella5dd6972008-05-09 16:24:21 +1000985 /* No versions at all? modprobe --force does this. */
986 if (versindex == 0)
987 return try_to_force_load(mod, symname) == 0;
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 versions = (void *) sechdrs[versindex].sh_addr;
990 num_versions = sechdrs[versindex].sh_size
991 / sizeof(struct modversion_info);
992
993 for (i = 0; i < num_versions; i++) {
994 if (strcmp(versions[i].name, symname) != 0)
995 continue;
996
Rusty Russelld4703ae2009-12-15 16:28:32 -0600997 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 DEBUGP("Found checksum %lX vs module %lX\n",
Rusty Russelld4703ae2009-12-15 16:28:32 -06001000 maybe_relocated(*crc, crc_owner), versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001001 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001003
Rusty Russella5dd6972008-05-09 16:24:21 +10001004 printk(KERN_WARNING "%s: no symbol version for %s\n",
1005 mod->name, symname);
1006 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001007
1008bad_version:
1009 printk("%s: disagrees about version of symbol %s\n",
1010 mod->name, symname);
1011 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013
1014static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1015 unsigned int versindex,
1016 struct module *mod)
1017{
1018 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Rusty Russell75676502010-06-05 11:17:36 -06001020 /* Since this should be found in kernel (which can't be removed),
1021 * no locking is necessary. */
Mike Frysinger6560dc12009-07-23 23:42:08 +09301022 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1023 &crc, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 BUG();
Rusty Russelld4703ae2009-12-15 16:28:32 -06001025 return check_version(sechdrs, versindex, "module_layout", mod, crc,
1026 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
Rusty Russell91e37a72008-05-09 16:25:28 +10001029/* First part is kernel version, which we ignore if module has crcs. */
1030static inline int same_magic(const char *amagic, const char *bmagic,
1031 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Rusty Russell91e37a72008-05-09 16:25:28 +10001033 if (has_crcs) {
1034 amagic += strcspn(amagic, " ");
1035 bmagic += strcspn(bmagic, " ");
1036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 return strcmp(amagic, bmagic) == 0;
1038}
1039#else
1040static inline int check_version(Elf_Shdr *sechdrs,
1041 unsigned int versindex,
1042 const char *symname,
1043 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001044 const unsigned long *crc,
1045 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 return 1;
1048}
1049
1050static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1051 unsigned int versindex,
1052 struct module *mod)
1053{
1054 return 1;
1055}
1056
Rusty Russell91e37a72008-05-09 16:25:28 +10001057static inline int same_magic(const char *amagic, const char *bmagic,
1058 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
1060 return strcmp(amagic, bmagic) == 0;
1061}
1062#endif /* CONFIG_MODVERSIONS */
1063
Rusty Russell75676502010-06-05 11:17:36 -06001064/* Resolve a symbol for this module. I.e. if we find one, record usage. */
Rusty Russell49668682010-08-05 12:59:10 -06001065static const struct kernel_symbol *resolve_symbol(struct module *mod,
1066 const struct load_info *info,
Tim Abbott414fd312008-12-05 19:03:56 -05001067 const char *name,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001068 char ownername[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
1070 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001071 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 const unsigned long *crc;
Rusty Russell9bea7f22010-06-05 11:17:37 -06001073 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Rusty Russell75676502010-06-05 11:17:36 -06001075 mutex_lock(&module_mutex);
Tim Abbott414fd312008-12-05 19:03:56 -05001076 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001077 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001078 if (!sym)
1079 goto unlock;
1080
Rusty Russell49668682010-08-05 12:59:10 -06001081 if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
1082 owner)) {
Rusty Russell9bea7f22010-06-05 11:17:37 -06001083 sym = ERR_PTR(-EINVAL);
1084 goto getname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
Rusty Russell9bea7f22010-06-05 11:17:37 -06001086
1087 err = ref_module(mod, owner);
1088 if (err) {
1089 sym = ERR_PTR(err);
1090 goto getname;
1091 }
1092
1093getname:
1094 /* We must make copy under the lock if we failed to get ref. */
1095 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1096unlock:
Rusty Russell75676502010-06-05 11:17:36 -06001097 mutex_unlock(&module_mutex);
Linus Torvalds218ce732010-05-25 16:48:30 -07001098 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
Rusty Russell49668682010-08-05 12:59:10 -06001101static const struct kernel_symbol *
1102resolve_symbol_wait(struct module *mod,
1103 const struct load_info *info,
1104 const char *name)
Rusty Russell9bea7f22010-06-05 11:17:37 -06001105{
1106 const struct kernel_symbol *ksym;
Rusty Russell49668682010-08-05 12:59:10 -06001107 char owner[MODULE_NAME_LEN];
Rusty Russell9bea7f22010-06-05 11:17:37 -06001108
1109 if (wait_event_interruptible_timeout(module_wq,
Rusty Russell49668682010-08-05 12:59:10 -06001110 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1111 || PTR_ERR(ksym) != -EBUSY,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001112 30 * HZ) <= 0) {
1113 printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
Rusty Russell49668682010-08-05 12:59:10 -06001114 mod->name, owner);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001115 }
1116 return ksym;
1117}
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119/*
1120 * /sys/module/foo/sections stuff
1121 * J. Corbet <corbet@lwn.net>
1122 */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001123#ifdef CONFIG_SYSFS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001124
Rusty Russell8f6d0372010-08-05 12:59:09 -06001125#ifdef CONFIG_KALLSYMS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001126static inline bool sect_empty(const Elf_Shdr *sect)
1127{
1128 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1129}
1130
Rusty Russella58730c2008-03-13 09:03:44 +00001131struct module_sect_attr
1132{
1133 struct module_attribute mattr;
1134 char *name;
1135 unsigned long address;
1136};
1137
1138struct module_sect_attrs
1139{
1140 struct attribute_group grp;
1141 unsigned int nsections;
1142 struct module_sect_attr attrs[0];
1143};
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145static ssize_t module_sect_show(struct module_attribute *mattr,
1146 struct module *mod, char *buf)
1147{
1148 struct module_sect_attr *sattr =
1149 container_of(mattr, struct module_sect_attr, mattr);
1150 return sprintf(buf, "0x%lx\n", sattr->address);
1151}
1152
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001153static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1154{
Rusty Russella58730c2008-03-13 09:03:44 +00001155 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001156
1157 for (section = 0; section < sect_attrs->nsections; section++)
1158 kfree(sect_attrs->attrs[section].name);
1159 kfree(sect_attrs);
1160}
1161
Rusty Russell8f6d0372010-08-05 12:59:09 -06001162static void add_sect_attrs(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164 unsigned int nloaded = 0, i, size[2];
1165 struct module_sect_attrs *sect_attrs;
1166 struct module_sect_attr *sattr;
1167 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 /* Count loaded sections and allocate structures */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001170 for (i = 0; i < info->hdr->e_shnum; i++)
1171 if (!sect_empty(&info->sechdrs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 nloaded++;
1173 size[0] = ALIGN(sizeof(*sect_attrs)
1174 + nloaded * sizeof(sect_attrs->attrs[0]),
1175 sizeof(sect_attrs->grp.attrs[0]));
1176 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001177 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1178 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return;
1180
1181 /* Setup section attributes. */
1182 sect_attrs->grp.name = "sections";
1183 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1184
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001185 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 sattr = &sect_attrs->attrs[0];
1187 gattr = &sect_attrs->grp.attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001188 for (i = 0; i < info->hdr->e_shnum; i++) {
1189 Elf_Shdr *sec = &info->sechdrs[i];
1190 if (sect_empty(sec))
Helge Deller35dead42009-12-03 00:29:15 +01001191 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001192 sattr->address = sec->sh_addr;
1193 sattr->name = kstrdup(info->secstrings + sec->sh_name,
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001194 GFP_KERNEL);
1195 if (sattr->name == NULL)
1196 goto out;
1197 sect_attrs->nsections++;
Eric W. Biederman361795b2010-02-12 13:41:56 -08001198 sysfs_attr_init(&sattr->mattr.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 sattr->mattr.show = module_sect_show;
1200 sattr->mattr.store = NULL;
1201 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 sattr->mattr.attr.mode = S_IRUGO;
1203 *(gattr++) = &(sattr++)->mattr.attr;
1204 }
1205 *gattr = NULL;
1206
1207 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1208 goto out;
1209
1210 mod->sect_attrs = sect_attrs;
1211 return;
1212 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001213 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
1216static void remove_sect_attrs(struct module *mod)
1217{
1218 if (mod->sect_attrs) {
1219 sysfs_remove_group(&mod->mkobj.kobj,
1220 &mod->sect_attrs->grp);
1221 /* We are positive that no one is using any sect attrs
1222 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001223 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 mod->sect_attrs = NULL;
1225 }
1226}
1227
Roland McGrath6d760132007-10-16 23:26:40 -07001228/*
1229 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1230 */
1231
1232struct module_notes_attrs {
1233 struct kobject *dir;
1234 unsigned int notes;
1235 struct bin_attribute attrs[0];
1236};
1237
Chris Wright2c3c8be2010-05-12 18:28:57 -07001238static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
Roland McGrath6d760132007-10-16 23:26:40 -07001239 struct bin_attribute *bin_attr,
1240 char *buf, loff_t pos, size_t count)
1241{
1242 /*
1243 * The caller checked the pos and count against our size.
1244 */
1245 memcpy(buf, bin_attr->private + pos, count);
1246 return count;
1247}
1248
1249static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1250 unsigned int i)
1251{
1252 if (notes_attrs->dir) {
1253 while (i-- > 0)
1254 sysfs_remove_bin_file(notes_attrs->dir,
1255 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001256 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001257 }
1258 kfree(notes_attrs);
1259}
1260
Rusty Russell8f6d0372010-08-05 12:59:09 -06001261static void add_notes_attrs(struct module *mod, const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001262{
1263 unsigned int notes, loaded, i;
1264 struct module_notes_attrs *notes_attrs;
1265 struct bin_attribute *nattr;
1266
Ingo Molnarea6bff32009-08-28 10:44:56 +02001267 /* failed to create section attributes, so can't create notes */
1268 if (!mod->sect_attrs)
1269 return;
1270
Roland McGrath6d760132007-10-16 23:26:40 -07001271 /* Count notes sections and allocate structures. */
1272 notes = 0;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001273 for (i = 0; i < info->hdr->e_shnum; i++)
1274 if (!sect_empty(&info->sechdrs[i]) &&
1275 (info->sechdrs[i].sh_type == SHT_NOTE))
Roland McGrath6d760132007-10-16 23:26:40 -07001276 ++notes;
1277
1278 if (notes == 0)
1279 return;
1280
1281 notes_attrs = kzalloc(sizeof(*notes_attrs)
1282 + notes * sizeof(notes_attrs->attrs[0]),
1283 GFP_KERNEL);
1284 if (notes_attrs == NULL)
1285 return;
1286
1287 notes_attrs->notes = notes;
1288 nattr = &notes_attrs->attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001289 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1290 if (sect_empty(&info->sechdrs[i]))
Roland McGrath6d760132007-10-16 23:26:40 -07001291 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001292 if (info->sechdrs[i].sh_type == SHT_NOTE) {
Eric W. Biederman361795b2010-02-12 13:41:56 -08001293 sysfs_bin_attr_init(nattr);
Roland McGrath6d760132007-10-16 23:26:40 -07001294 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1295 nattr->attr.mode = S_IRUGO;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001296 nattr->size = info->sechdrs[i].sh_size;
1297 nattr->private = (void *) info->sechdrs[i].sh_addr;
Roland McGrath6d760132007-10-16 23:26:40 -07001298 nattr->read = module_notes_read;
1299 ++nattr;
1300 }
1301 ++loaded;
1302 }
1303
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001304 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001305 if (!notes_attrs->dir)
1306 goto out;
1307
1308 for (i = 0; i < notes; ++i)
1309 if (sysfs_create_bin_file(notes_attrs->dir,
1310 &notes_attrs->attrs[i]))
1311 goto out;
1312
1313 mod->notes_attrs = notes_attrs;
1314 return;
1315
1316 out:
1317 free_notes_attrs(notes_attrs, i);
1318}
1319
1320static void remove_notes_attrs(struct module *mod)
1321{
1322 if (mod->notes_attrs)
1323 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1324}
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001327
Rusty Russell8f6d0372010-08-05 12:59:09 -06001328static inline void add_sect_attrs(struct module *mod,
1329 const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
1331}
1332
1333static inline void remove_sect_attrs(struct module *mod)
1334{
1335}
Roland McGrath6d760132007-10-16 23:26:40 -07001336
Rusty Russell8f6d0372010-08-05 12:59:09 -06001337static inline void add_notes_attrs(struct module *mod,
1338 const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001339{
1340}
1341
1342static inline void remove_notes_attrs(struct module *mod)
1343{
1344}
Rusty Russell8f6d0372010-08-05 12:59:09 -06001345#endif /* CONFIG_KALLSYMS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001347static void add_usage_links(struct module *mod)
1348{
1349#ifdef CONFIG_MODULE_UNLOAD
1350 struct module_use *use;
1351 int nowarn;
1352
Rusty Russell75676502010-06-05 11:17:36 -06001353 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001354 list_for_each_entry(use, &mod->target_list, target_list) {
1355 nowarn = sysfs_create_link(use->target->holders_dir,
1356 &mod->mkobj.kobj, mod->name);
1357 }
Rusty Russell75676502010-06-05 11:17:36 -06001358 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001359#endif
1360}
1361
1362static void del_usage_links(struct module *mod)
1363{
1364#ifdef CONFIG_MODULE_UNLOAD
1365 struct module_use *use;
1366
Rusty Russell75676502010-06-05 11:17:36 -06001367 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001368 list_for_each_entry(use, &mod->target_list, target_list)
1369 sysfs_remove_link(use->target->holders_dir, mod->name);
Rusty Russell75676502010-06-05 11:17:36 -06001370 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001371#endif
1372}
1373
Rusty Russell6407ebb22010-06-05 11:17:36 -06001374static int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001375{
1376 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001377 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001378 int error = 0;
1379 int i;
1380
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001381 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1382 (ARRAY_SIZE(modinfo_attrs) + 1)),
1383 GFP_KERNEL);
1384 if (!mod->modinfo_attrs)
1385 return -ENOMEM;
1386
1387 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001388 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1389 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001390 (attr->test && attr->test(mod))) {
1391 memcpy(temp_attr, attr, sizeof(*temp_attr));
Eric W. Biederman361795b2010-02-12 13:41:56 -08001392 sysfs_attr_init(&temp_attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001393 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1394 ++temp_attr;
1395 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001396 }
1397 return error;
1398}
1399
Rusty Russell6407ebb22010-06-05 11:17:36 -06001400static void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001401{
1402 struct module_attribute *attr;
1403 int i;
1404
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001405 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1406 /* pick a field to test for end of list */
1407 if (!attr->attr.name)
1408 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001409 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001410 if (attr->free)
1411 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001412 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001413 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001414}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Rusty Russell6407ebb22010-06-05 11:17:36 -06001416static int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
1418 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001419 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001421 if (!module_sysfs_initialized) {
1422 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001423 mod->name);
1424 err = -EINVAL;
1425 goto out;
1426 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001427
1428 kobj = kset_find_obj(module_kset, mod->name);
1429 if (kobj) {
1430 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1431 kobject_put(kobj);
1432 err = -EINVAL;
1433 goto out;
1434 }
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001437
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001438 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1439 mod->mkobj.kobj.kset = module_kset;
1440 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1441 "%s", mod->name);
1442 if (err)
1443 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001444
Kay Sievers97c146e2007-11-29 23:46:11 +01001445 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001446out:
1447 return err;
1448}
1449
Rusty Russell6407ebb22010-06-05 11:17:36 -06001450static int mod_sysfs_setup(struct module *mod,
Rusty Russell8f6d0372010-08-05 12:59:09 -06001451 const struct load_info *info,
Kay Sievers270a6c42007-01-18 13:26:15 +01001452 struct kernel_param *kparam,
1453 unsigned int num_params)
1454{
1455 int err;
1456
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001457 err = mod_sysfs_init(mod);
1458 if (err)
1459 goto out;
1460
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001461 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001462 if (!mod->holders_dir) {
1463 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001464 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001465 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 err = module_param_sysfs_setup(mod, kparam, num_params);
1468 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001469 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Matt Domschc988d2b2005-06-23 22:05:15 -07001471 err = module_add_modinfo_attrs(mod);
1472 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001473 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001474
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001475 add_usage_links(mod);
Rusty Russell8f6d0372010-08-05 12:59:09 -06001476 add_sect_attrs(mod, info);
1477 add_notes_attrs(mod, info);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001478
Kay Sieverse17e0f52006-11-24 12:15:25 +01001479 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return 0;
1481
Kay Sieverse17e0f52006-11-24 12:15:25 +01001482out_unreg_param:
1483 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001484out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001485 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001486out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001487 kobject_put(&mod->mkobj.kobj);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001488out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return err;
1490}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001491
1492static void mod_sysfs_fini(struct module *mod)
1493{
Rusty Russell8f6d0372010-08-05 12:59:09 -06001494 remove_notes_attrs(mod);
1495 remove_sect_attrs(mod);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001496 kobject_put(&mod->mkobj.kobj);
1497}
1498
Rusty Russell8f6d0372010-08-05 12:59:09 -06001499#else /* !CONFIG_SYSFS */
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001500
Rusty Russell8f6d0372010-08-05 12:59:09 -06001501static int mod_sysfs_setup(struct module *mod,
1502 const struct load_info *info,
Rusty Russell6407ebb22010-06-05 11:17:36 -06001503 struct kernel_param *kparam,
1504 unsigned int num_params)
1505{
1506 return 0;
1507}
1508
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001509static void mod_sysfs_fini(struct module *mod)
1510{
1511}
1512
Rusty Russell36b03602010-08-05 12:59:09 -06001513static void module_remove_modinfo_attrs(struct module *mod)
1514{
1515}
1516
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001517static void del_usage_links(struct module *mod)
1518{
1519}
1520
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001521#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Rusty Russell36b03602010-08-05 12:59:09 -06001523static void mod_sysfs_teardown(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001525 del_usage_links(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001526 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001528 kobject_put(mod->mkobj.drivers_dir);
1529 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001530 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531}
1532
1533/*
1534 * unlink the module with the whole machine is stopped with interrupts off
1535 * - this defends against kallsyms not taking locks
1536 */
1537static int __unlink_module(void *_mod)
1538{
1539 struct module *mod = _mod;
1540 list_del(&mod->list);
1541 return 0;
1542}
1543
Rusty Russell75676502010-06-05 11:17:36 -06001544/* Free a module, remove from lists, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545static void free_module(struct module *mod)
1546{
Li Zefan7ead8b82009-08-17 16:56:28 +08001547 trace_module_free(mod);
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 /* Delete from various lists */
Rusty Russell75676502010-06-05 11:17:36 -06001550 mutex_lock(&module_mutex);
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001551 stop_machine(__unlink_module, mod, NULL);
Rusty Russell75676502010-06-05 11:17:36 -06001552 mutex_unlock(&module_mutex);
Rusty Russell36b03602010-08-05 12:59:09 -06001553 mod_sysfs_teardown(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Jason Baronb82bab4b2010-07-27 13:18:01 -07001555 /* Remove dynamic debug info */
1556 ddebug_remove_module(mod->name);
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 /* Arch-specific cleanup. */
1559 module_arch_cleanup(mod);
1560
1561 /* Module unload stuff */
1562 module_unload_free(mod);
1563
Rusty Russelle180a6b2009-03-31 13:05:29 -06001564 /* Free any allocated parameters. */
1565 destroy_params(mod->kp, mod->num_kp);
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 /* This may be NULL, but that's OK */
1568 module_free(mod, mod->module_init);
1569 kfree(mod->args);
Tejun Heo259354d2010-03-10 18:56:10 +09001570 percpu_modfree(mod);
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001571
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001572 /* Free lock-classes: */
1573 lockdep_free_key_range(mod->module_core, mod->core_size);
1574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 /* Finally, free the core (containing the module structure) */
1576 module_free(mod, mod->module_core);
Bernd Schmidteb8cdec2009-09-21 17:03:57 -07001577
1578#ifdef CONFIG_MPU
1579 update_protections(current->mm);
1580#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
1582
1583void *__symbol_get(const char *symbol)
1584{
1585 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001586 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Rusty Russell24da1cb2007-07-15 23:41:46 -07001588 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001589 sym = find_symbol(symbol, &owner, NULL, true, true);
1590 if (sym && strong_try_module_get(owner))
1591 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001592 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Tim Abbott414fd312008-12-05 19:03:56 -05001594 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595}
1596EXPORT_SYMBOL_GPL(__symbol_get);
1597
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001598/*
1599 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001600 * in the kernel or in some other module's exported symbol table.
Rusty Russellbe593f42010-06-05 11:17:37 -06001601 *
1602 * You must hold the module_mutex.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001603 */
1604static int verify_export_symbols(struct module *mod)
1605{
Rusty Russellb2111042008-05-01 21:15:00 -05001606 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001607 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001608 const struct kernel_symbol *s;
1609 struct {
1610 const struct kernel_symbol *sym;
1611 unsigned int num;
1612 } arr[] = {
1613 { mod->syms, mod->num_syms },
1614 { mod->gpl_syms, mod->num_gpl_syms },
1615 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001616#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001617 { mod->unused_syms, mod->num_unused_syms },
1618 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001619#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001620 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001621
Rusty Russellb2111042008-05-01 21:15:00 -05001622 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1623 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Rusty Russellbe593f42010-06-05 11:17:37 -06001624 if (find_symbol(s->name, &owner, NULL, true, false)) {
Rusty Russellb2111042008-05-01 21:15:00 -05001625 printk(KERN_ERR
1626 "%s: exports duplicate symbol %s"
1627 " (owned by %s)\n",
1628 mod->name, s->name, module_name(owner));
1629 return -ENOEXEC;
1630 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001631 }
Rusty Russellb2111042008-05-01 21:15:00 -05001632 }
1633 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001634}
1635
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001636/* Change all symbols so that st_value encodes the pointer directly. */
Rusty Russell49668682010-08-05 12:59:10 -06001637static int simplify_symbols(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
Rusty Russell49668682010-08-05 12:59:10 -06001639 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
1640 Elf_Sym *sym = (void *)symsec->sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 unsigned long secbase;
Rusty Russell49668682010-08-05 12:59:10 -06001642 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05001644 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Rusty Russell49668682010-08-05 12:59:10 -06001646 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
1647 const char *name = info->strtab + sym[i].st_name;
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 switch (sym[i].st_shndx) {
1650 case SHN_COMMON:
1651 /* We compiled with -fno-common. These are not
1652 supposed to happen. */
Rusty Russell49668682010-08-05 12:59:10 -06001653 DEBUGP("Common symbol: %s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 printk("%s: please compile with -fno-common\n",
1655 mod->name);
1656 ret = -ENOEXEC;
1657 break;
1658
1659 case SHN_ABS:
1660 /* Don't need to do anything */
1661 DEBUGP("Absolute symbol: 0x%08lx\n",
1662 (long)sym[i].st_value);
1663 break;
1664
1665 case SHN_UNDEF:
Rusty Russell49668682010-08-05 12:59:10 -06001666 ksym = resolve_symbol_wait(mod, info, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 /* Ok if resolved. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001668 if (ksym && !IS_ERR(ksym)) {
Tim Abbott414fd312008-12-05 19:03:56 -05001669 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 break;
Tim Abbott414fd312008-12-05 19:03:56 -05001671 }
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 /* Ok if weak. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001674 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 break;
1676
Rusty Russell9bea7f22010-06-05 11:17:37 -06001677 printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
Rusty Russell49668682010-08-05 12:59:10 -06001678 mod->name, name, PTR_ERR(ksym));
Rusty Russell9bea7f22010-06-05 11:17:37 -06001679 ret = PTR_ERR(ksym) ?: -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 break;
1681
1682 default:
1683 /* Divert to percpu allocation if a percpu var. */
Rusty Russell49668682010-08-05 12:59:10 -06001684 if (sym[i].st_shndx == info->index.pcpu)
Tejun Heo259354d2010-03-10 18:56:10 +09001685 secbase = (unsigned long)mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 else
Rusty Russell49668682010-08-05 12:59:10 -06001687 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 sym[i].st_value += secbase;
1689 break;
1690 }
1691 }
1692
1693 return ret;
1694}
1695
Rusty Russell49668682010-08-05 12:59:10 -06001696static int apply_relocations(struct module *mod, const struct load_info *info)
Rusty Russell22e268e2010-08-05 12:59:05 -06001697{
1698 unsigned int i;
1699 int err = 0;
1700
1701 /* Now do relocations. */
Rusty Russell49668682010-08-05 12:59:10 -06001702 for (i = 1; i < info->hdr->e_shnum; i++) {
1703 unsigned int infosec = info->sechdrs[i].sh_info;
Rusty Russell22e268e2010-08-05 12:59:05 -06001704
1705 /* Not a valid relocation section? */
Rusty Russell49668682010-08-05 12:59:10 -06001706 if (infosec >= info->hdr->e_shnum)
Rusty Russell22e268e2010-08-05 12:59:05 -06001707 continue;
1708
1709 /* Don't bother with non-allocated sections */
Rusty Russell49668682010-08-05 12:59:10 -06001710 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
Rusty Russell22e268e2010-08-05 12:59:05 -06001711 continue;
1712
Rusty Russell49668682010-08-05 12:59:10 -06001713 if (info->sechdrs[i].sh_type == SHT_REL)
1714 err = apply_relocate(info->sechdrs, info->strtab,
1715 info->index.sym, i, mod);
1716 else if (info->sechdrs[i].sh_type == SHT_RELA)
1717 err = apply_relocate_add(info->sechdrs, info->strtab,
1718 info->index.sym, i, mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06001719 if (err < 0)
1720 break;
1721 }
1722 return err;
1723}
1724
Helge Deller088af9a2008-12-31 12:31:18 +01001725/* Additional bytes needed by arch in front of individual sections */
1726unsigned int __weak arch_mod_section_prepend(struct module *mod,
1727 unsigned int section)
1728{
1729 /* default implementation just returns zero */
1730 return 0;
1731}
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01001734static long get_offset(struct module *mod, unsigned int *size,
1735 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
1737 long ret;
1738
Helge Deller088af9a2008-12-31 12:31:18 +01001739 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1741 *size = ret + sechdr->sh_size;
1742 return ret;
1743}
1744
1745/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1746 might -- code, read-only data, read-write data, small data. Tally
1747 sizes, and place the offsets into sh_entsize fields: high bit means it
1748 belongs in init. */
Rusty Russell49668682010-08-05 12:59:10 -06001749static void layout_sections(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
1751 static unsigned long const masks[][2] = {
1752 /* NOTE: all executable code must be the first section
1753 * in this array; otherwise modify the text_size
1754 * finder in the two loops below */
1755 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1756 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1757 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1758 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1759 };
1760 unsigned int m, i;
1761
Rusty Russell49668682010-08-05 12:59:10 -06001762 for (i = 0; i < info->hdr->e_shnum; i++)
1763 info->sechdrs[i].sh_entsize = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
1765 DEBUGP("Core section allocation order:\n");
1766 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06001767 for (i = 0; i < info->hdr->e_shnum; ++i) {
1768 Elf_Shdr *s = &info->sechdrs[i];
1769 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1772 || (s->sh_flags & masks[m][1])
1773 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06001774 || strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001776 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Rusty Russell49668682010-08-05 12:59:10 -06001777 DEBUGP("\t%s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 }
1779 if (m == 0)
1780 mod->core_text_size = mod->core_size;
1781 }
1782
1783 DEBUGP("Init section allocation order:\n");
1784 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06001785 for (i = 0; i < info->hdr->e_shnum; ++i) {
1786 Elf_Shdr *s = &info->sechdrs[i];
1787 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1790 || (s->sh_flags & masks[m][1])
1791 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06001792 || !strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001794 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 | INIT_OFFSET_MASK);
Rusty Russell49668682010-08-05 12:59:10 -06001796 DEBUGP("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 }
1798 if (m == 0)
1799 mod->init_text_size = mod->init_size;
1800 }
1801}
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803static void set_license(struct module *mod, const char *license)
1804{
1805 if (!license)
1806 license = "unspecified";
1807
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001808 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07001809 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001810 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001811 "kernel.\n", mod->name, license);
1812 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 }
1814}
1815
1816/* Parse tag=value strings from .modinfo section */
1817static char *next_string(char *string, unsigned long *secsize)
1818{
1819 /* Skip non-zero chars */
1820 while (string[0]) {
1821 string++;
1822 if ((*secsize)-- <= 1)
1823 return NULL;
1824 }
1825
1826 /* Skip any zero padding. */
1827 while (!string[0]) {
1828 string++;
1829 if ((*secsize)-- <= 1)
1830 return NULL;
1831 }
1832 return string;
1833}
1834
Rusty Russell49668682010-08-05 12:59:10 -06001835static char *get_modinfo(struct load_info *info, const char *tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
1837 char *p;
1838 unsigned int taglen = strlen(tag);
Rusty Russell49668682010-08-05 12:59:10 -06001839 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
1840 unsigned long size = infosec->sh_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Rusty Russell49668682010-08-05 12:59:10 -06001842 for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1844 return p + taglen + 1;
1845 }
1846 return NULL;
1847}
1848
Rusty Russell49668682010-08-05 12:59:10 -06001849static void setup_modinfo(struct module *mod, struct load_info *info)
Matt Domschc988d2b2005-06-23 22:05:15 -07001850{
1851 struct module_attribute *attr;
1852 int i;
1853
1854 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1855 if (attr->setup)
Rusty Russell49668682010-08-05 12:59:10 -06001856 attr->setup(mod, get_modinfo(info, attr->attr.name));
Matt Domschc988d2b2005-06-23 22:05:15 -07001857 }
1858}
Matt Domschc988d2b2005-06-23 22:05:15 -07001859
Rusty Russella263f772009-09-25 00:32:58 -06001860static void free_modinfo(struct module *mod)
1861{
1862 struct module_attribute *attr;
1863 int i;
1864
1865 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1866 if (attr->free)
1867 attr->free(mod);
1868 }
1869}
1870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01001872
1873/* lookup symbol in given range of kernel_symbols */
1874static const struct kernel_symbol *lookup_symbol(const char *name,
1875 const struct kernel_symbol *start,
1876 const struct kernel_symbol *stop)
1877{
1878 const struct kernel_symbol *ks = start;
1879 for (; ks < stop; ks++)
1880 if (strcmp(ks->name, name) == 0)
1881 return ks;
1882 return NULL;
1883}
1884
Tim Abbottca4787b2009-01-05 08:40:10 -06001885static int is_exported(const char *name, unsigned long value,
1886 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
Tim Abbottca4787b2009-01-05 08:40:10 -06001888 const struct kernel_symbol *ks;
1889 if (!mod)
1890 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001891 else
Tim Abbottca4787b2009-01-05 08:40:10 -06001892 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
1893 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894}
1895
1896/* As per nm */
Rusty Russelleded41c2010-08-05 12:59:07 -06001897static char elf_type(const Elf_Sym *sym, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898{
Rusty Russelleded41c2010-08-05 12:59:07 -06001899 const Elf_Shdr *sechdrs = info->sechdrs;
1900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1902 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1903 return 'v';
1904 else
1905 return 'w';
1906 }
1907 if (sym->st_shndx == SHN_UNDEF)
1908 return 'U';
1909 if (sym->st_shndx == SHN_ABS)
1910 return 'a';
1911 if (sym->st_shndx >= SHN_LORESERVE)
1912 return '?';
1913 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1914 return 't';
1915 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1916 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1917 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1918 return 'r';
1919 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1920 return 'g';
1921 else
1922 return 'd';
1923 }
1924 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1925 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1926 return 's';
1927 else
1928 return 'b';
1929 }
Rusty Russelleded41c2010-08-05 12:59:07 -06001930 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
1931 ".debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 return 'n';
Rusty Russelleded41c2010-08-05 12:59:07 -06001933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 return '?';
1935}
1936
Jan Beulich4a496222009-07-06 14:50:42 +01001937static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
1938 unsigned int shnum)
1939{
1940 const Elf_Shdr *sec;
1941
1942 if (src->st_shndx == SHN_UNDEF
1943 || src->st_shndx >= shnum
1944 || !src->st_name)
1945 return false;
1946
1947 sec = sechdrs + src->st_shndx;
1948 if (!(sec->sh_flags & SHF_ALLOC)
1949#ifndef CONFIG_KALLSYMS_ALL
1950 || !(sec->sh_flags & SHF_EXECINSTR)
1951#endif
1952 || (sec->sh_entsize & INIT_OFFSET_MASK))
1953 return false;
1954
1955 return true;
1956}
1957
Rusty Russell49668682010-08-05 12:59:10 -06001958static void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01001959{
Rusty Russell49668682010-08-05 12:59:10 -06001960 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
1961 Elf_Shdr *strsect = info->sechdrs + info->index.str;
Jan Beulich4a496222009-07-06 14:50:42 +01001962 const Elf_Sym *src;
1963 unsigned int i, nsrc, ndst;
1964
1965 /* Put symbol section at end of init part of module. */
1966 symsect->sh_flags |= SHF_ALLOC;
1967 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
Rusty Russell49668682010-08-05 12:59:10 -06001968 info->index.sym) | INIT_OFFSET_MASK;
1969 DEBUGP("\t%s\n", info->secstrings + symsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01001970
Rusty Russell49668682010-08-05 12:59:10 -06001971 src = (void *)info->hdr + symsect->sh_offset;
Jan Beulich4a496222009-07-06 14:50:42 +01001972 nsrc = symsect->sh_size / sizeof(*src);
1973 for (ndst = i = 1; i < nsrc; ++i, ++src)
Rusty Russell49668682010-08-05 12:59:10 -06001974 if (is_core_symbol(src, info->sechdrs, info->hdr->e_shnum)) {
Jan Beulich554bdfe2009-07-06 14:51:44 +01001975 unsigned int j = src->st_name;
1976
Rusty Russell49668682010-08-05 12:59:10 -06001977 while (!__test_and_set_bit(j, info->strmap)
1978 && info->strtab[j])
Jan Beulich554bdfe2009-07-06 14:51:44 +01001979 ++j;
Jan Beulich4a496222009-07-06 14:50:42 +01001980 ++ndst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01001981 }
Jan Beulich4a496222009-07-06 14:50:42 +01001982
1983 /* Append room for core symbols at end of core part. */
Rusty Russell49668682010-08-05 12:59:10 -06001984 info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
1985 mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
Jan Beulich4a496222009-07-06 14:50:42 +01001986
Jan Beulich554bdfe2009-07-06 14:51:44 +01001987 /* Put string table section at end of init part of module. */
1988 strsect->sh_flags |= SHF_ALLOC;
1989 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
Rusty Russell49668682010-08-05 12:59:10 -06001990 info->index.str) | INIT_OFFSET_MASK;
1991 DEBUGP("\t%s\n", info->secstrings + strsect->sh_name);
Jan Beulich554bdfe2009-07-06 14:51:44 +01001992
1993 /* Append room for core symbols' strings at end of core part. */
Rusty Russell49668682010-08-05 12:59:10 -06001994 info->stroffs = mod->core_size;
1995 __set_bit(0, info->strmap);
1996 mod->core_size += bitmap_weight(info->strmap, strsect->sh_size);
Jan Beulich4a496222009-07-06 14:50:42 +01001997}
1998
Rusty Russell811d66a2010-08-05 12:59:12 -06001999static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
Jan Beulich4a496222009-07-06 14:50:42 +01002001 unsigned int i, ndst;
2002 const Elf_Sym *src;
2003 Elf_Sym *dst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002004 char *s;
Rusty Russelleded41c2010-08-05 12:59:07 -06002005 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Rusty Russelleded41c2010-08-05 12:59:07 -06002007 mod->symtab = (void *)symsec->sh_addr;
2008 mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
Rusty Russell511ca6a2010-08-05 12:59:08 -06002009 /* Make sure we get permanent strtab: don't use info->strtab. */
2010 mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
2012 /* Set types up while we still have access to sections. */
2013 for (i = 0; i < mod->num_symtab; i++)
Rusty Russelleded41c2010-08-05 12:59:07 -06002014 mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
Jan Beulich4a496222009-07-06 14:50:42 +01002015
Rusty Russelld9131882010-08-05 12:59:08 -06002016 mod->core_symtab = dst = mod->module_core + info->symoffs;
Jan Beulich4a496222009-07-06 14:50:42 +01002017 src = mod->symtab;
2018 *dst = *src;
2019 for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
Rusty Russelleded41c2010-08-05 12:59:07 -06002020 if (!is_core_symbol(src, info->sechdrs, info->hdr->e_shnum))
Jan Beulich4a496222009-07-06 14:50:42 +01002021 continue;
2022 dst[ndst] = *src;
Rusty Russelld9131882010-08-05 12:59:08 -06002023 dst[ndst].st_name = bitmap_weight(info->strmap,
2024 dst[ndst].st_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002025 ++ndst;
2026 }
2027 mod->core_num_syms = ndst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002028
Rusty Russelld9131882010-08-05 12:59:08 -06002029 mod->core_strtab = s = mod->module_core + info->stroffs;
Rusty Russelleded41c2010-08-05 12:59:07 -06002030 for (*s = 0, i = 1; i < info->sechdrs[info->index.str].sh_size; ++i)
Rusty Russelld9131882010-08-05 12:59:08 -06002031 if (test_bit(i, info->strmap))
Jan Beulich554bdfe2009-07-06 14:51:44 +01002032 *++s = mod->strtab[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033}
2034#else
Rusty Russell49668682010-08-05 12:59:10 -06002035static inline void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002036{
2037}
Paul Mundt3ae91c22009-10-01 15:43:54 -07002038
Rusty Russelld9131882010-08-05 12:59:08 -06002039static void add_kallsyms(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
2041}
2042#endif /* CONFIG_KALLSYMS */
2043
Jason Barone9d376f2009-02-05 11:51:38 -05002044static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05002045{
Rusty Russell811d66a2010-08-05 12:59:12 -06002046 if (!debug)
2047 return;
Jason Barone9d376f2009-02-05 11:51:38 -05002048#ifdef CONFIG_DYNAMIC_DEBUG
2049 if (ddebug_add_module(debug, num, debug->modname))
2050 printk(KERN_ERR "dynamic debug error adding module: %s\n",
2051 debug->modname);
2052#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002053}
Jason Baron346e15b2008-08-12 16:46:19 -04002054
Yehuda Sadehff49d742010-07-03 13:07:35 +10002055static void dynamic_debug_remove(struct _ddebug *debug)
2056{
2057 if (debug)
2058 ddebug_remove_module(debug->modname);
2059}
2060
Rusty Russell3a642e92008-07-22 19:24:28 -05002061static void *module_alloc_update_bounds(unsigned long size)
2062{
2063 void *ret = module_alloc(size);
2064
2065 if (ret) {
Rusty Russell75676502010-06-05 11:17:36 -06002066 mutex_lock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002067 /* Update module bounds. */
2068 if ((unsigned long)ret < module_addr_min)
2069 module_addr_min = (unsigned long)ret;
2070 if ((unsigned long)ret + size > module_addr_max)
2071 module_addr_max = (unsigned long)ret + size;
Rusty Russell75676502010-06-05 11:17:36 -06002072 mutex_unlock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002073 }
2074 return ret;
2075}
2076
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002077#ifdef CONFIG_DEBUG_KMEMLEAK
Rusty Russell49668682010-08-05 12:59:10 -06002078static void kmemleak_load_module(const struct module *mod,
2079 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002080{
2081 unsigned int i;
2082
2083 /* only scan the sections containing data */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002084 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002085
Rusty Russell49668682010-08-05 12:59:10 -06002086 for (i = 1; i < info->hdr->e_shnum; i++) {
2087 const char *name = info->secstrings + info->sechdrs[i].sh_name;
2088 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002089 continue;
Rusty Russell49668682010-08-05 12:59:10 -06002090 if (!strstarts(name, ".data") && !strstarts(name, ".bss"))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002091 continue;
2092
Rusty Russell49668682010-08-05 12:59:10 -06002093 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2094 info->sechdrs[i].sh_size, GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002095 }
2096}
2097#else
Rusty Russell49668682010-08-05 12:59:10 -06002098static inline void kmemleak_load_module(const struct module *mod,
2099 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002100{
2101}
2102#endif
2103
Rusty Russell6526c532010-08-05 12:59:10 -06002104/* Sets info->hdr and info->len. */
Rusty Russelld9131882010-08-05 12:59:08 -06002105static int copy_and_check(struct load_info *info,
2106 const void __user *umod, unsigned long len,
2107 const char __user *uargs)
Rusty Russell40dd2562010-08-05 12:59:03 -06002108{
2109 int err;
2110 Elf_Ehdr *hdr;
2111
2112 if (len < sizeof(*hdr))
2113 return -ENOEXEC;
2114
2115 /* Suck in entire file: we'll want most of it. */
2116 /* vmalloc barfs on "unusual" numbers. Check here */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002117 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
Rusty Russell40dd2562010-08-05 12:59:03 -06002118 return -ENOMEM;
2119
2120 if (copy_from_user(hdr, umod, len) != 0) {
2121 err = -EFAULT;
2122 goto free_hdr;
2123 }
2124
2125 /* Sanity checks against insmoding binaries or wrong arch,
2126 weird elf version */
2127 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
2128 || hdr->e_type != ET_REL
2129 || !elf_check_arch(hdr)
2130 || hdr->e_shentsize != sizeof(Elf_Shdr)) {
2131 err = -ENOEXEC;
2132 goto free_hdr;
2133 }
2134
2135 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) {
2136 err = -ENOEXEC;
2137 goto free_hdr;
2138 }
Rusty Russelld9131882010-08-05 12:59:08 -06002139
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002140 info->hdr = hdr;
2141 info->len = len;
Rusty Russell40dd2562010-08-05 12:59:03 -06002142 return 0;
2143
2144free_hdr:
2145 vfree(hdr);
2146 return err;
2147}
2148
Rusty Russelld9131882010-08-05 12:59:08 -06002149static void free_copy(struct load_info *info)
2150{
Rusty Russelld9131882010-08-05 12:59:08 -06002151 vfree(info->hdr);
2152}
2153
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002154static int rewrite_section_headers(struct load_info *info)
2155{
2156 unsigned int i;
2157
2158 /* This should always be true, but let's be sure. */
2159 info->sechdrs[0].sh_addr = 0;
2160
2161 for (i = 1; i < info->hdr->e_shnum; i++) {
2162 Elf_Shdr *shdr = &info->sechdrs[i];
2163 if (shdr->sh_type != SHT_NOBITS
2164 && info->len < shdr->sh_offset + shdr->sh_size) {
2165 printk(KERN_ERR "Module len %lu truncated\n",
2166 info->len);
2167 return -ENOEXEC;
2168 }
2169
2170 /* Mark all sections sh_addr with their address in the
2171 temporary image. */
2172 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2173
2174#ifndef CONFIG_MODULE_UNLOAD
2175 /* Don't load .exit sections */
2176 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2177 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2178#endif
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002179 }
Rusty Russelld6df72a2010-08-05 12:59:07 -06002180
2181 /* Track but don't keep modinfo and version sections. */
Rusty Russell49668682010-08-05 12:59:10 -06002182 info->index.vers = find_sec(info, "__versions");
2183 info->index.info = find_sec(info, ".modinfo");
Rusty Russelld6df72a2010-08-05 12:59:07 -06002184 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2185 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002186 return 0;
2187}
2188
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002189/*
2190 * Set up our basic convenience variables (pointers to section headers,
2191 * search for module section index etc), and do some basic section
2192 * verification.
2193 *
2194 * Return the temporary module pointer (we'll replace it with the final
2195 * one when we move the module sections around).
2196 */
2197static struct module *setup_load_info(struct load_info *info)
2198{
2199 unsigned int i;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002200 int err;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002201 struct module *mod;
2202
2203 /* Set up the convenience variables */
2204 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002205 info->secstrings = (void *)info->hdr
2206 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002207
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002208 err = rewrite_section_headers(info);
2209 if (err)
2210 return ERR_PTR(err);
2211
2212 /* Find internal symbols and strings. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002213 for (i = 1; i < info->hdr->e_shnum; i++) {
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002214 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2215 info->index.sym = i;
2216 info->index.str = info->sechdrs[i].sh_link;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002217 info->strtab = (char *)info->hdr
2218 + info->sechdrs[info->index.str].sh_offset;
2219 break;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002220 }
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002221 }
2222
Rusty Russell49668682010-08-05 12:59:10 -06002223 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002224 if (!info->index.mod) {
2225 printk(KERN_WARNING "No module found in object\n");
2226 return ERR_PTR(-ENOEXEC);
2227 }
2228 /* This is temporary: point mod into copy of data. */
2229 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2230
2231 if (info->index.sym == 0) {
2232 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2233 mod->name);
2234 return ERR_PTR(-ENOEXEC);
2235 }
2236
Rusty Russell49668682010-08-05 12:59:10 -06002237 info->index.pcpu = find_pcpusec(info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002238
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002239 /* Check module struct version now, before we try to use module. */
2240 if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2241 return ERR_PTR(-ENOEXEC);
2242
2243 return mod;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002244}
2245
Rusty Russell49668682010-08-05 12:59:10 -06002246static int check_modinfo(struct module *mod, struct load_info *info)
Rusty Russell40dd2562010-08-05 12:59:03 -06002247{
Rusty Russell49668682010-08-05 12:59:10 -06002248 const char *modmagic = get_modinfo(info, "vermagic");
Rusty Russell40dd2562010-08-05 12:59:03 -06002249 int err;
2250
2251 /* This is allowed: modprobe --force will invalidate it. */
2252 if (!modmagic) {
2253 err = try_to_force_load(mod, "bad vermagic");
2254 if (err)
2255 return err;
Rusty Russell49668682010-08-05 12:59:10 -06002256 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002257 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2258 mod->name, modmagic, vermagic);
2259 return -ENOEXEC;
2260 }
2261
Rusty Russell49668682010-08-05 12:59:10 -06002262 if (get_modinfo(info, "staging")) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002263 add_taint_module(mod, TAINT_CRAP);
2264 printk(KERN_WARNING "%s: module is from the staging directory,"
2265 " the quality is unknown, you have been warned.\n",
2266 mod->name);
2267 }
Rusty Russell22e268e2010-08-05 12:59:05 -06002268
2269 /* Set up license info based on the info section */
Rusty Russell49668682010-08-05 12:59:10 -06002270 set_license(mod, get_modinfo(info, "license"));
Rusty Russell22e268e2010-08-05 12:59:05 -06002271
Rusty Russell40dd2562010-08-05 12:59:03 -06002272 return 0;
2273}
2274
Rusty Russell811d66a2010-08-05 12:59:12 -06002275static void find_module_sections(struct module *mod, struct load_info *info)
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002276{
Rusty Russell49668682010-08-05 12:59:10 -06002277 mod->kp = section_objs(info, "__param",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002278 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell49668682010-08-05 12:59:10 -06002279 mod->syms = section_objs(info, "__ksymtab",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002280 sizeof(*mod->syms), &mod->num_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002281 mod->crcs = section_addr(info, "__kcrctab");
2282 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002283 sizeof(*mod->gpl_syms),
2284 &mod->num_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002285 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
2286 mod->gpl_future_syms = section_objs(info,
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002287 "__ksymtab_gpl_future",
2288 sizeof(*mod->gpl_future_syms),
2289 &mod->num_gpl_future_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002290 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002291
2292#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell49668682010-08-05 12:59:10 -06002293 mod->unused_syms = section_objs(info, "__ksymtab_unused",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002294 sizeof(*mod->unused_syms),
2295 &mod->num_unused_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002296 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
2297 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002298 sizeof(*mod->unused_gpl_syms),
2299 &mod->num_unused_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002300 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002301#endif
2302#ifdef CONFIG_CONSTRUCTORS
Rusty Russell49668682010-08-05 12:59:10 -06002303 mod->ctors = section_objs(info, ".ctors",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002304 sizeof(*mod->ctors), &mod->num_ctors);
2305#endif
2306
2307#ifdef CONFIG_TRACEPOINTS
Rusty Russell49668682010-08-05 12:59:10 -06002308 mod->tracepoints = section_objs(info, "__tracepoints",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002309 sizeof(*mod->tracepoints),
2310 &mod->num_tracepoints);
2311#endif
Jason Baronbf5438fc2010-09-17 11:09:00 -04002312#ifdef HAVE_JUMP_LABEL
2313 mod->jump_entries = section_objs(info, "__jump_table",
2314 sizeof(*mod->jump_entries),
2315 &mod->num_jump_entries);
2316#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002317#ifdef CONFIG_EVENT_TRACING
Rusty Russell49668682010-08-05 12:59:10 -06002318 mod->trace_events = section_objs(info, "_ftrace_events",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002319 sizeof(*mod->trace_events),
2320 &mod->num_trace_events);
2321 /*
2322 * This section contains pointers to allocated objects in the trace
2323 * code and not scanning it leads to false positives.
2324 */
2325 kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2326 mod->num_trace_events, GFP_KERNEL);
2327#endif
2328#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2329 /* sechdrs[0].sh_size is always zero */
Rusty Russell49668682010-08-05 12:59:10 -06002330 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002331 sizeof(*mod->ftrace_callsites),
2332 &mod->num_ftrace_callsites);
2333#endif
Rusty Russell22e268e2010-08-05 12:59:05 -06002334
Rusty Russell811d66a2010-08-05 12:59:12 -06002335 mod->extable = section_objs(info, "__ex_table",
2336 sizeof(*mod->extable), &mod->num_exentries);
2337
Rusty Russell49668682010-08-05 12:59:10 -06002338 if (section_addr(info, "__obsparm"))
Rusty Russell22e268e2010-08-05 12:59:05 -06002339 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2340 mod->name);
Rusty Russell811d66a2010-08-05 12:59:12 -06002341
2342 info->debug = section_objs(info, "__verbose",
2343 sizeof(*info->debug), &info->num_debug);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002344}
2345
Rusty Russell49668682010-08-05 12:59:10 -06002346static int move_module(struct module *mod, struct load_info *info)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002347{
2348 int i;
2349 void *ptr;
2350
2351 /* Do the allocs. */
2352 ptr = module_alloc_update_bounds(mod->core_size);
2353 /*
2354 * The pointer to this block is stored in the module structure
2355 * which is inside the block. Just mark it as not being a
2356 * leak.
2357 */
2358 kmemleak_not_leak(ptr);
2359 if (!ptr)
Rusty Russelld9131882010-08-05 12:59:08 -06002360 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002361
2362 memset(ptr, 0, mod->core_size);
2363 mod->module_core = ptr;
2364
2365 ptr = module_alloc_update_bounds(mod->init_size);
2366 /*
2367 * The pointer to this block is stored in the module structure
2368 * which is inside the block. This block doesn't need to be
2369 * scanned as it contains data and code that will be freed
2370 * after the module is initialized.
2371 */
2372 kmemleak_ignore(ptr);
2373 if (!ptr && mod->init_size) {
2374 module_free(mod, mod->module_core);
Rusty Russelld9131882010-08-05 12:59:08 -06002375 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002376 }
2377 memset(ptr, 0, mod->init_size);
2378 mod->module_init = ptr;
2379
2380 /* Transfer each section which specifies SHF_ALLOC */
2381 DEBUGP("final section addresses:\n");
Rusty Russell49668682010-08-05 12:59:10 -06002382 for (i = 0; i < info->hdr->e_shnum; i++) {
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002383 void *dest;
Rusty Russell49668682010-08-05 12:59:10 -06002384 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002385
Rusty Russell49668682010-08-05 12:59:10 -06002386 if (!(shdr->sh_flags & SHF_ALLOC))
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002387 continue;
2388
Rusty Russell49668682010-08-05 12:59:10 -06002389 if (shdr->sh_entsize & INIT_OFFSET_MASK)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002390 dest = mod->module_init
Rusty Russell49668682010-08-05 12:59:10 -06002391 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002392 else
Rusty Russell49668682010-08-05 12:59:10 -06002393 dest = mod->module_core + shdr->sh_entsize;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002394
Rusty Russell49668682010-08-05 12:59:10 -06002395 if (shdr->sh_type != SHT_NOBITS)
2396 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002397 /* Update sh_addr to point to copy in image. */
Rusty Russell49668682010-08-05 12:59:10 -06002398 shdr->sh_addr = (unsigned long)dest;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002399 DEBUGP("\t0x%lx %s\n",
Rusty Russell49668682010-08-05 12:59:10 -06002400 shdr->sh_addr, info->secstrings + shdr->sh_name);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002401 }
Rusty Russelld9131882010-08-05 12:59:08 -06002402
2403 return 0;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002404}
2405
Rusty Russell49668682010-08-05 12:59:10 -06002406static int check_module_license_and_versions(struct module *mod)
Rusty Russell22e268e2010-08-05 12:59:05 -06002407{
2408 /*
2409 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2410 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2411 * using GPL-only symbols it needs.
2412 */
2413 if (strcmp(mod->name, "ndiswrapper") == 0)
2414 add_taint(TAINT_PROPRIETARY_MODULE);
2415
2416 /* driverloader was caught wrongly pretending to be under GPL */
2417 if (strcmp(mod->name, "driverloader") == 0)
2418 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2419
2420#ifdef CONFIG_MODVERSIONS
2421 if ((mod->num_syms && !mod->crcs)
2422 || (mod->num_gpl_syms && !mod->gpl_crcs)
2423 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2424#ifdef CONFIG_UNUSED_SYMBOLS
2425 || (mod->num_unused_syms && !mod->unused_crcs)
2426 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2427#endif
2428 ) {
2429 return try_to_force_load(mod,
2430 "no versions for exported symbols");
2431 }
2432#endif
2433 return 0;
2434}
2435
2436static void flush_module_icache(const struct module *mod)
2437{
2438 mm_segment_t old_fs;
2439
2440 /* flush the icache in correct context */
2441 old_fs = get_fs();
2442 set_fs(KERNEL_DS);
2443
2444 /*
2445 * Flush the instruction cache, since we've played with text.
2446 * Do it before processing of module parameters, so the module
2447 * can provide parameter accessor functions of its own.
2448 */
2449 if (mod->module_init)
2450 flush_icache_range((unsigned long)mod->module_init,
2451 (unsigned long)mod->module_init
2452 + mod->init_size);
2453 flush_icache_range((unsigned long)mod->module_core,
2454 (unsigned long)mod->module_core + mod->core_size);
2455
2456 set_fs(old_fs);
2457}
2458
Rusty Russelld9131882010-08-05 12:59:08 -06002459static struct module *layout_and_allocate(struct load_info *info)
2460{
2461 /* Module within temporary copy. */
2462 struct module *mod;
Rusty Russell49668682010-08-05 12:59:10 -06002463 Elf_Shdr *pcpusec;
Rusty Russelld9131882010-08-05 12:59:08 -06002464 int err;
2465
2466 mod = setup_load_info(info);
2467 if (IS_ERR(mod))
2468 return mod;
2469
Rusty Russell49668682010-08-05 12:59:10 -06002470 err = check_modinfo(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002471 if (err)
2472 return ERR_PTR(err);
2473
2474 /* Allow arches to frob section contents and sizes. */
Rusty Russell49668682010-08-05 12:59:10 -06002475 err = module_frob_arch_sections(info->hdr, info->sechdrs,
2476 info->secstrings, mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002477 if (err < 0)
Rusty Russell6526c532010-08-05 12:59:10 -06002478 goto out;
Rusty Russelld9131882010-08-05 12:59:08 -06002479
Rusty Russell49668682010-08-05 12:59:10 -06002480 pcpusec = &info->sechdrs[info->index.pcpu];
2481 if (pcpusec->sh_size) {
Rusty Russelld9131882010-08-05 12:59:08 -06002482 /* We have a special allocation for this section. */
Rusty Russell49668682010-08-05 12:59:10 -06002483 err = percpu_modalloc(mod,
2484 pcpusec->sh_size, pcpusec->sh_addralign);
Rusty Russelld9131882010-08-05 12:59:08 -06002485 if (err)
Rusty Russell6526c532010-08-05 12:59:10 -06002486 goto out;
Rusty Russell49668682010-08-05 12:59:10 -06002487 pcpusec->sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russelld9131882010-08-05 12:59:08 -06002488 }
2489
2490 /* Determine total sizes, and put offsets in sh_entsize. For now
2491 this is done generically; there doesn't appear to be any
2492 special cases for the architectures. */
Rusty Russell49668682010-08-05 12:59:10 -06002493 layout_sections(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002494
2495 info->strmap = kzalloc(BITS_TO_LONGS(info->sechdrs[info->index.str].sh_size)
2496 * sizeof(long), GFP_KERNEL);
2497 if (!info->strmap) {
2498 err = -ENOMEM;
2499 goto free_percpu;
2500 }
Rusty Russell49668682010-08-05 12:59:10 -06002501 layout_symtab(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002502
2503 /* Allocate and move to the final place */
Rusty Russell49668682010-08-05 12:59:10 -06002504 err = move_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002505 if (err)
2506 goto free_strmap;
2507
2508 /* Module has been copied to its final place now: return it. */
2509 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
Rusty Russell49668682010-08-05 12:59:10 -06002510 kmemleak_load_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002511 return mod;
2512
2513free_strmap:
2514 kfree(info->strmap);
2515free_percpu:
2516 percpu_modfree(mod);
Rusty Russell6526c532010-08-05 12:59:10 -06002517out:
Rusty Russelld9131882010-08-05 12:59:08 -06002518 return ERR_PTR(err);
2519}
2520
2521/* mod is no longer valid after this! */
2522static void module_deallocate(struct module *mod, struct load_info *info)
2523{
2524 kfree(info->strmap);
2525 percpu_modfree(mod);
2526 module_free(mod, mod->module_init);
2527 module_free(mod, mod->module_core);
2528}
2529
Rusty Russell811d66a2010-08-05 12:59:12 -06002530static int post_relocation(struct module *mod, const struct load_info *info)
2531{
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002532 /* Sort exception table now relocations are done. */
Rusty Russell811d66a2010-08-05 12:59:12 -06002533 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2534
2535 /* Copy relocated percpu area over. */
2536 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
2537 info->sechdrs[info->index.pcpu].sh_size);
2538
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002539 /* Setup kallsyms-specific fields. */
Rusty Russell811d66a2010-08-05 12:59:12 -06002540 add_kallsyms(mod, info);
2541
2542 /* Arch-specific module finalizing. */
2543 return module_finalize(info->hdr, info->sechdrs, mod);
2544}
2545
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546/* Allocate and load the module: note that size of section 0 is always
2547 zero, and we rely on this for optional sections. */
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002548static struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 unsigned long len,
2550 const char __user *uargs)
2551{
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002552 struct load_info info = { NULL, };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 struct module *mod;
Rusty Russell40dd2562010-08-05 12:59:03 -06002554 long err;
Paul Mundt3ae91c22009-10-01 15:43:54 -07002555
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
2557 umod, len, uargs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Rusty Russelld9131882010-08-05 12:59:08 -06002559 /* Copy in the blobs from userspace, check they are vaguely sane. */
2560 err = copy_and_check(&info, umod, len, uargs);
Rusty Russell40dd2562010-08-05 12:59:03 -06002561 if (err)
2562 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
Rusty Russelld9131882010-08-05 12:59:08 -06002564 /* Figure out module layout, and allocate all the memory. */
2565 mod = layout_and_allocate(&info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002566 if (IS_ERR(mod)) {
2567 err = PTR_ERR(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002568 goto free_copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Rusty Russell49668682010-08-05 12:59:10 -06002571 /* Now module is in final location, initialize linked lists, etc. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -06002572 err = module_unload_init(mod);
2573 if (err)
Rusty Russelld9131882010-08-05 12:59:08 -06002574 goto free_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Rusty Russell22e268e2010-08-05 12:59:05 -06002576 /* Now we've got everything in the final locations, we can
2577 * find optional sections. */
Rusty Russell49668682010-08-05 12:59:10 -06002578 find_module_sections(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
Rusty Russell49668682010-08-05 12:59:10 -06002580 err = check_module_license_and_versions(mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002581 if (err)
2582 goto free_unload;
Dave Jones9841d61d2006-01-08 01:03:41 -08002583
Matt Domschc988d2b2005-06-23 22:05:15 -07002584 /* Set up MODINFO_ATTR fields */
Rusty Russell49668682010-08-05 12:59:10 -06002585 setup_modinfo(mod, &info);
Matt Domschc988d2b2005-06-23 22:05:15 -07002586
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 /* Fix up syms, so that st_value is a pointer to location. */
Rusty Russell49668682010-08-05 12:59:10 -06002588 err = simplify_symbols(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06002590 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Rusty Russell49668682010-08-05 12:59:10 -06002592 err = apply_relocations(mod, &info);
Rusty Russell22e268e2010-08-05 12:59:05 -06002593 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06002594 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Rusty Russell811d66a2010-08-05 12:59:12 -06002596 err = post_relocation(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06002598 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Rusty Russell22e268e2010-08-05 12:59:05 -06002600 flush_module_icache(mod);
Thomas Koeller378bac82005-09-06 15:17:11 -07002601
Rusty Russell6526c532010-08-05 12:59:10 -06002602 /* Now copy in args */
2603 mod->args = strndup_user(uargs, ~0UL >> 1);
2604 if (IS_ERR(mod->args)) {
2605 err = PTR_ERR(mod->args);
2606 goto free_arch_cleanup;
2607 }
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002608
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002609 /* Mark state as coming so strong_try_module_get() ignores us. */
Rusty Russelld9131882010-08-05 12:59:08 -06002610 mod->state = MODULE_STATE_COMING;
2611
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002612 /* Now sew it into the lists so we can get lockdep and oops
Andi Kleend72b3752008-08-30 10:09:00 +02002613 * info during argument parsing. Noone should access us, since
2614 * strong_try_module_get() will fail.
2615 * lockdep/oops can run asynchronous, so use the RCU list insertion
2616 * function to insert in a way safe to concurrent readers.
2617 * The mutex protects against concurrent writers.
2618 */
Rusty Russell75676502010-06-05 11:17:36 -06002619 mutex_lock(&module_mutex);
Linus Torvalds3bafeb62010-06-05 11:17:36 -06002620 if (find_module(mod->name)) {
2621 err = -EEXIST;
Rusty Russellbe593f42010-06-05 11:17:37 -06002622 goto unlock;
Linus Torvalds3bafeb62010-06-05 11:17:36 -06002623 }
2624
Rusty Russell811d66a2010-08-05 12:59:12 -06002625 /* This has to be done once we're sure module name is unique. */
2626 if (!mod->taints)
2627 dynamic_debug_setup(info.debug, info.num_debug);
Yehuda Sadehff49d742010-07-03 13:07:35 +10002628
Rusty Russellbe593f42010-06-05 11:17:37 -06002629 /* Find duplicate symbols */
2630 err = verify_export_symbols(mod);
2631 if (err < 0)
Yehuda Sadehff49d742010-07-03 13:07:35 +10002632 goto ddebug;
Rusty Russellbe593f42010-06-05 11:17:37 -06002633
Andi Kleend72b3752008-08-30 10:09:00 +02002634 list_add_rcu(&mod->list, &modules);
Rusty Russell75676502010-06-05 11:17:36 -06002635 mutex_unlock(&module_mutex);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002636
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002637 /* Module is ready to execute: parsing args may do that. */
Rusty Russelle180a6b2009-03-31 13:05:29 -06002638 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002640 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002642 /* Link in to syfs. */
Rusty Russell8f6d0372010-08-05 12:59:09 -06002643 err = mod_sysfs_setup(mod, &info, mod->kp, mod->num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002645 goto unlink;
Rusty Russell80a3d1b2010-06-05 11:17:36 -06002646
Rusty Russelld9131882010-08-05 12:59:08 -06002647 /* Get rid of temporary copy and strmap. */
2648 kfree(info.strmap);
2649 free_copy(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
2651 /* Done! */
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002652 trace_module_load(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 return mod;
2654
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002655 unlink:
Rusty Russell75676502010-06-05 11:17:36 -06002656 mutex_lock(&module_mutex);
Rusty Russelle91defa2009-03-31 13:05:35 -06002657 /* Unlink carefully: kallsyms could be walking list. */
2658 list_del_rcu(&mod->list);
Yehuda Sadehff49d742010-07-03 13:07:35 +10002659 ddebug:
Rusty Russell811d66a2010-08-05 12:59:12 -06002660 if (!mod->taints)
2661 dynamic_debug_remove(info.debug);
Rusty Russellbe593f42010-06-05 11:17:37 -06002662 unlock:
Rusty Russell75676502010-06-05 11:17:36 -06002663 mutex_unlock(&module_mutex);
Rusty Russelle91defa2009-03-31 13:05:35 -06002664 synchronize_sched();
Rusty Russell6526c532010-08-05 12:59:10 -06002665 kfree(mod->args);
2666 free_arch_cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 module_arch_cleanup(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002668 free_modinfo:
Rusty Russella263f772009-09-25 00:32:58 -06002669 free_modinfo(mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002670 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 module_unload_free(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002672 free_module:
2673 module_deallocate(mod, &info);
2674 free_copy:
2675 free_copy(&info);
Jayachandran C6fe2e702006-01-06 00:19:54 -08002676 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677}
2678
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002679/* Call module constructors. */
2680static void do_mod_ctors(struct module *mod)
2681{
2682#ifdef CONFIG_CONSTRUCTORS
2683 unsigned long i;
2684
2685 for (i = 0; i < mod->num_ctors; i++)
2686 mod->ctors[i]();
2687#endif
2688}
2689
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690/* This is where the real work happens */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002691SYSCALL_DEFINE3(init_module, void __user *, umod,
2692 unsigned long, len, const char __user *, uargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693{
2694 struct module *mod;
2695 int ret = 0;
2696
2697 /* Must have permission */
Kees Cook3d433212009-04-02 15:49:29 -07002698 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 return -EPERM;
2700
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 /* Do all the hard work */
2702 mod = load_module(umod, len, uargs);
Rusty Russell75676502010-06-05 11:17:36 -06002703 if (IS_ERR(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 return PTR_ERR(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
Alan Sterne041c682006-03-27 01:16:30 -08002706 blocking_notifier_call_chain(&module_notify_list,
2707 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002709 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 /* Start the module */
2711 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002712 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 if (ret < 0) {
2714 /* Init routine failed: abort. Try to protect us from
2715 buggy refcounters. */
2716 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002717 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002718 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002719 blocking_notifier_call_chain(&module_notify_list,
2720 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002721 free_module(mod);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002722 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 return ret;
2724 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002725 if (ret > 0) {
Joe Perchesad361c92009-07-06 13:05:40 -07002726 printk(KERN_WARNING
2727"%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
2728"%s: loading module anyway...\n",
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002729 __func__, mod->name, ret,
2730 __func__);
2731 dump_stack();
2732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
Rusty Russell6c5db222008-03-10 11:43:52 -07002734 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002736 wake_up(&module_wq);
Masami Hiramatsu0deddf432009-01-06 14:41:54 -08002737 blocking_notifier_call_chain(&module_notify_list,
2738 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07002739
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07002740 /* We need to finish all async code before the module init sequence is done */
2741 async_synchronize_full();
2742
Rusty Russell6c5db222008-03-10 11:43:52 -07002743 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 /* Drop initial reference. */
2745 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06002746 trim_init_extable(mod);
Jan Beulich4a496222009-07-06 14:50:42 +01002747#ifdef CONFIG_KALLSYMS
2748 mod->num_symtab = mod->core_num_syms;
2749 mod->symtab = mod->core_symtab;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002750 mod->strtab = mod->core_strtab;
Jan Beulich4a496222009-07-06 14:50:42 +01002751#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 module_free(mod, mod->module_init);
2753 mod->module_init = NULL;
2754 mod->init_size = 0;
2755 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002756 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
2758 return 0;
2759}
2760
2761static inline int within(unsigned long addr, void *start, unsigned long size)
2762{
2763 return ((void *)addr >= start && (void *)addr < start + size);
2764}
2765
2766#ifdef CONFIG_KALLSYMS
2767/*
2768 * This ignores the intensely annoying "mapping symbols" found
2769 * in ARM ELF files: $a, $t and $d.
2770 */
2771static inline int is_arm_mapping_symbol(const char *str)
2772{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002773 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 && (str[2] == '\0' || str[2] == '.');
2775}
2776
2777static const char *get_ksymbol(struct module *mod,
2778 unsigned long addr,
2779 unsigned long *size,
2780 unsigned long *offset)
2781{
2782 unsigned int i, best = 0;
2783 unsigned long nextval;
2784
2785 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002786 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002788 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2790
2791 /* Scan for closest preceeding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002792 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 for (i = 1; i < mod->num_symtab; i++) {
2794 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2795 continue;
2796
2797 /* We ignore unnamed symbols: they're uninformative
2798 * and inserted at a whim. */
2799 if (mod->symtab[i].st_value <= addr
2800 && mod->symtab[i].st_value > mod->symtab[best].st_value
2801 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2802 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2803 best = i;
2804 if (mod->symtab[i].st_value > addr
2805 && mod->symtab[i].st_value < nextval
2806 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2807 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2808 nextval = mod->symtab[i].st_value;
2809 }
2810
2811 if (!best)
2812 return NULL;
2813
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002814 if (size)
2815 *size = nextval - mod->symtab[best].st_value;
2816 if (offset)
2817 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 return mod->strtab + mod->symtab[best].st_name;
2819}
2820
Rusty Russell6dd06c92008-01-29 17:13:22 -05002821/* For kallsyms to ask for address resolution. NULL means not found. Careful
2822 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002823const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05002824 unsigned long *size,
2825 unsigned long *offset,
2826 char **modname,
2827 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
2829 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002830 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
Rusty Russellcb2a5202008-01-14 00:55:03 -08002832 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002833 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002834 if (within_module_init(addr, mod) ||
2835 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002836 if (modname)
2837 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002838 ret = get_ksymbol(mod, addr, size, offset);
2839 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 }
2841 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05002842 /* Make a copy in here where it's safe */
2843 if (ret) {
2844 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2845 ret = namebuf;
2846 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002847 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002848 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849}
2850
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002851int lookup_module_symbol_name(unsigned long addr, char *symname)
2852{
2853 struct module *mod;
2854
Rusty Russellcb2a5202008-01-14 00:55:03 -08002855 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002856 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002857 if (within_module_init(addr, mod) ||
2858 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002859 const char *sym;
2860
2861 sym = get_ksymbol(mod, addr, NULL, NULL);
2862 if (!sym)
2863 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07002864 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002865 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002866 return 0;
2867 }
2868 }
2869out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002870 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002871 return -ERANGE;
2872}
2873
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002874int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2875 unsigned long *offset, char *modname, char *name)
2876{
2877 struct module *mod;
2878
Rusty Russellcb2a5202008-01-14 00:55:03 -08002879 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002880 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002881 if (within_module_init(addr, mod) ||
2882 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002883 const char *sym;
2884
2885 sym = get_ksymbol(mod, addr, size, offset);
2886 if (!sym)
2887 goto out;
2888 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07002889 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002890 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07002891 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002892 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002893 return 0;
2894 }
2895 }
2896out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002897 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002898 return -ERANGE;
2899}
2900
Alexey Dobriyanea078902007-05-08 00:28:39 -07002901int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2902 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903{
2904 struct module *mod;
2905
Rusty Russellcb2a5202008-01-14 00:55:03 -08002906 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002907 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 if (symnum < mod->num_symtab) {
2909 *value = mod->symtab[symnum].st_value;
2910 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002911 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07002912 KSYM_NAME_LEN);
2913 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06002914 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002915 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002916 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 }
2918 symnum -= mod->num_symtab;
2919 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002920 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002921 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922}
2923
2924static unsigned long mod_find_symname(struct module *mod, const char *name)
2925{
2926 unsigned int i;
2927
2928 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002929 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2930 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 return mod->symtab[i].st_value;
2932 return 0;
2933}
2934
2935/* Look for this name: can be of form module:name. */
2936unsigned long module_kallsyms_lookup_name(const char *name)
2937{
2938 struct module *mod;
2939 char *colon;
2940 unsigned long ret = 0;
2941
2942 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08002943 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 if ((colon = strchr(name, ':')) != NULL) {
2945 *colon = '\0';
2946 if ((mod = find_module(name)) != NULL)
2947 ret = mod_find_symname(mod, colon+1);
2948 *colon = ':';
2949 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02002950 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 if ((ret = mod_find_symname(mod, name)) != 0)
2952 break;
2953 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002954 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 return ret;
2956}
Anders Kaseorg75a66612008-12-05 19:03:58 -05002957
2958int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
2959 struct module *, unsigned long),
2960 void *data)
2961{
2962 struct module *mod;
2963 unsigned int i;
2964 int ret;
2965
2966 list_for_each_entry(mod, &modules, list) {
2967 for (i = 0; i < mod->num_symtab; i++) {
2968 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
2969 mod, mod->symtab[i].st_value);
2970 if (ret != 0)
2971 return ret;
2972 }
2973 }
2974 return 0;
2975}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976#endif /* CONFIG_KALLSYMS */
2977
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002978static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002979{
2980 int bx = 0;
2981
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002982 if (mod->taints ||
2983 mod->state == MODULE_STATE_GOING ||
2984 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002985 buf[bx++] = '(';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002986 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002987 buf[bx++] = 'P';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002988 if (mod->taints & (1 << TAINT_FORCED_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002989 buf[bx++] = 'F';
Linus Torvalds26e9a392008-10-17 09:50:12 -07002990 if (mod->taints & (1 << TAINT_CRAP))
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002991 buf[bx++] = 'C';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002992 /*
2993 * TAINT_FORCED_RMMOD: could be added.
2994 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2995 * apply to modules.
2996 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002997
2998 /* Show a - for module-is-being-unloaded */
2999 if (mod->state == MODULE_STATE_GOING)
3000 buf[bx++] = '-';
3001 /* Show a + for module-is-being-loaded */
3002 if (mod->state == MODULE_STATE_COMING)
3003 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003004 buf[bx++] = ')';
3005 }
3006 buf[bx] = '\0';
3007
3008 return buf;
3009}
3010
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003011#ifdef CONFIG_PROC_FS
3012/* Called by the /proc file system to return a list of modules. */
3013static void *m_start(struct seq_file *m, loff_t *pos)
3014{
3015 mutex_lock(&module_mutex);
3016 return seq_list_start(&modules, *pos);
3017}
3018
3019static void *m_next(struct seq_file *m, void *p, loff_t *pos)
3020{
3021 return seq_list_next(p, &modules, pos);
3022}
3023
3024static void m_stop(struct seq_file *m, void *p)
3025{
3026 mutex_unlock(&module_mutex);
3027}
3028
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029static int m_show(struct seq_file *m, void *p)
3030{
3031 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003032 char buf[8];
3033
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05003034 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 mod->name, mod->init_size + mod->core_size);
3036 print_unload_info(m, mod);
3037
3038 /* Informative for users. */
3039 seq_printf(m, " %s",
3040 mod->state == MODULE_STATE_GOING ? "Unloading":
3041 mod->state == MODULE_STATE_COMING ? "Loading":
3042 "Live");
3043 /* Used by oprofile and other similar tools. */
3044 seq_printf(m, " 0x%p", mod->module_core);
3045
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003046 /* Taints info */
3047 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003048 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003049
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 seq_printf(m, "\n");
3051 return 0;
3052}
3053
3054/* Format: modulename size refcount deps address
3055
3056 Where refcount is a number or -, and deps is a comma-separated list
3057 of depends or -.
3058*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003059static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 .start = m_start,
3061 .next = m_next,
3062 .stop = m_stop,
3063 .show = m_show
3064};
3065
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003066static int modules_open(struct inode *inode, struct file *file)
3067{
3068 return seq_open(file, &modules_op);
3069}
3070
3071static const struct file_operations proc_modules_operations = {
3072 .open = modules_open,
3073 .read = seq_read,
3074 .llseek = seq_lseek,
3075 .release = seq_release,
3076};
3077
3078static int __init proc_modules_init(void)
3079{
3080 proc_create("modules", 0, NULL, &proc_modules_operations);
3081 return 0;
3082}
3083module_init(proc_modules_init);
3084#endif
3085
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086/* Given an address, look for it in the module exception tables. */
3087const struct exception_table_entry *search_module_extables(unsigned long addr)
3088{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 const struct exception_table_entry *e = NULL;
3090 struct module *mod;
3091
Rusty Russell24da1cb2007-07-15 23:41:46 -07003092 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003093 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 if (mod->num_exentries == 0)
3095 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003096
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 e = search_extable(mod->extable,
3098 mod->extable + mod->num_exentries - 1,
3099 addr);
3100 if (e)
3101 break;
3102 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07003103 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
3105 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07003106 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 return e;
3108}
3109
Ingo Molnar4d435f92006-07-03 00:24:24 -07003110/*
Rusty Russelle6104992009-03-31 13:05:31 -06003111 * is_module_address - is this address inside a module?
3112 * @addr: the address to check.
3113 *
3114 * See is_module_text_address() if you simply want to see if the address
3115 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07003116 */
Rusty Russelle6104992009-03-31 13:05:31 -06003117bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07003118{
Rusty Russelle6104992009-03-31 13:05:31 -06003119 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003120
Rusty Russell24da1cb2007-07-15 23:41:46 -07003121 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06003122 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07003123 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07003124
Rusty Russelle6104992009-03-31 13:05:31 -06003125 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003126}
3127
Rusty Russelle6104992009-03-31 13:05:31 -06003128/*
3129 * __module_address - get the module which contains an address.
3130 * @addr: the address.
3131 *
3132 * Must be called with preempt disabled or module mutex held so that
3133 * module doesn't get freed during this.
3134 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07003135struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136{
3137 struct module *mod;
3138
Rusty Russell3a642e92008-07-22 19:24:28 -05003139 if (addr < module_addr_min || addr > module_addr_max)
3140 return NULL;
3141
Andi Kleend72b3752008-08-30 10:09:00 +02003142 list_for_each_entry_rcu(mod, &modules, list)
Rusty Russelle6104992009-03-31 13:05:31 -06003143 if (within_module_core(addr, mod)
3144 || within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 return mod;
3146 return NULL;
3147}
Tim Abbottc6b37802008-12-05 19:03:59 -05003148EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
Rusty Russelle6104992009-03-31 13:05:31 -06003150/*
3151 * is_module_text_address - is this address inside module code?
3152 * @addr: the address to check.
3153 *
3154 * See is_module_address() if you simply want to see if the address is
3155 * anywhere in a module. See kernel_text_address() for testing if an
3156 * address corresponds to kernel or module code.
3157 */
3158bool is_module_text_address(unsigned long addr)
3159{
3160 bool ret;
3161
3162 preempt_disable();
3163 ret = __module_text_address(addr) != NULL;
3164 preempt_enable();
3165
3166 return ret;
3167}
3168
3169/*
3170 * __module_text_address - get the module whose code contains an address.
3171 * @addr: the address.
3172 *
3173 * Must be called with preempt disabled or module mutex held so that
3174 * module doesn't get freed during this.
3175 */
3176struct module *__module_text_address(unsigned long addr)
3177{
3178 struct module *mod = __module_address(addr);
3179 if (mod) {
3180 /* Make sure it's within the text section. */
3181 if (!within(addr, mod->module_init, mod->init_text_size)
3182 && !within(addr, mod->module_core, mod->core_text_size))
3183 mod = NULL;
3184 }
3185 return mod;
3186}
Tim Abbottc6b37802008-12-05 19:03:59 -05003187EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06003188
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189/* Don't grab lock, we're oopsing. */
3190void print_modules(void)
3191{
3192 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07003193 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
Linus Torvaldsb2311252009-06-16 11:07:14 -07003195 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02003196 /* Most callers should already have preempt disabled, but make sure */
3197 preempt_disable();
3198 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003199 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02003200 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01003201 if (last_unloaded_module[0])
3202 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 printk("\n");
3204}
3205
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06003207/* Generate the signature for all relevant module structures here.
3208 * If these change, we don't want to try to parse the module. */
3209void module_layout(struct module *mod,
3210 struct modversion_info *ver,
3211 struct kernel_param *kp,
3212 struct kernel_symbol *ks,
Rusty Russell8c8ef422009-03-31 13:05:34 -06003213 struct tracepoint *tp)
3214{
3215}
3216EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07003218
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04003219#ifdef CONFIG_TRACEPOINTS
3220void module_update_tracepoints(void)
3221{
3222 struct module *mod;
3223
3224 mutex_lock(&module_mutex);
3225 list_for_each_entry(mod, &modules, list)
3226 if (!mod->taints)
3227 tracepoint_update_probe_range(mod->tracepoints,
3228 mod->tracepoints + mod->num_tracepoints);
3229 mutex_unlock(&module_mutex);
3230}
3231
3232/*
3233 * Returns 0 if current not found.
3234 * Returns 1 if current found.
3235 */
3236int module_get_iter_tracepoints(struct tracepoint_iter *iter)
3237{
3238 struct module *iter_mod;
3239 int found = 0;
3240
3241 mutex_lock(&module_mutex);
3242 list_for_each_entry(iter_mod, &modules, list) {
3243 if (!iter_mod->taints) {
3244 /*
3245 * Sorted module list
3246 */
3247 if (iter_mod < iter->module)
3248 continue;
3249 else if (iter_mod > iter->module)
3250 iter->tracepoint = NULL;
3251 found = tracepoint_get_iter_range(&iter->tracepoint,
3252 iter_mod->tracepoints,
3253 iter_mod->tracepoints
3254 + iter_mod->num_tracepoints);
3255 if (found) {
3256 iter->module = iter_mod;
3257 break;
3258 }
3259 }
3260 }
3261 mutex_unlock(&module_mutex);
3262 return found;
3263}
3264#endif