blob: 22879725678dfcd53324698504500dc97decdea6 [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>
matthieu castet84e1c6b2010-11-16 22:35:16 +010059#include <linux/pfn.h>
Alessio Igor Bogani403ed272011-04-20 11:10:52 +020060#include <linux/bsearch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Li Zefan7ead8b82009-08-17 16:56:28 +080062#define CREATE_TRACE_POINTS
63#include <trace/events/module.h>
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#if 0
66#define DEBUGP printk
67#else
68#define DEBUGP(fmt , a...)
69#endif
70
71#ifndef ARCH_SHF_SMALL
72#define ARCH_SHF_SMALL 0
73#endif
74
matthieu castet84e1c6b2010-11-16 22:35:16 +010075/*
76 * Modules' sections will be aligned on page boundaries
77 * to ensure complete separation of code and data, but
78 * only when CONFIG_DEBUG_SET_MODULE_RONX=y
79 */
80#ifdef CONFIG_DEBUG_SET_MODULE_RONX
81# define debug_align(X) ALIGN(X, PAGE_SIZE)
82#else
83# define debug_align(X) (X)
84#endif
85
86/*
87 * Given BASE and SIZE this macro calculates the number of pages the
88 * memory regions occupies
89 */
90#define MOD_NUMBER_OF_PAGES(BASE, SIZE) (((SIZE) > 0) ? \
91 (PFN_DOWN((unsigned long)(BASE) + (SIZE) - 1) - \
92 PFN_DOWN((unsigned long)BASE) + 1) \
93 : (0UL))
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/* If this is set, the section belongs in the init part of the module */
96#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
97
Rusty Russell75676502010-06-05 11:17:36 -060098/*
99 * Mutex protects:
100 * 1) List of modules (also safely readable with preempt_disable),
101 * 2) module_use links,
102 * 3) module_addr_min/module_addr_max.
Andi Kleend72b3752008-08-30 10:09:00 +0200103 * (delete uses stop_machine/add uses RCU list operations). */
Tim Abbottc6b37802008-12-05 19:03:59 -0500104DEFINE_MUTEX(module_mutex);
105EXPORT_SYMBOL_GPL(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static LIST_HEAD(modules);
Jason Wessel67fc4e02010-05-20 21:04:21 -0500107#ifdef CONFIG_KGDB_KDB
108struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
109#endif /* CONFIG_KGDB_KDB */
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Stephen Rothwell19e45292009-04-14 17:27:18 +1000112/* Block module loading/unloading? */
113int modules_disabled = 0;
114
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500115/* Waiting for a module to finish initializing? */
116static DECLARE_WAIT_QUEUE_HEAD(module_wq);
117
Alan Sterne041c682006-03-27 01:16:30 -0800118static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Rusty Russell75676502010-06-05 11:17:36 -0600120/* Bounds of module allocation, for speeding __module_address.
121 * Protected by module_mutex. */
Rusty Russell3a642e92008-07-22 19:24:28 -0500122static unsigned long module_addr_min = -1UL, module_addr_max = 0;
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124int register_module_notifier(struct notifier_block * nb)
125{
Alan Sterne041c682006-03-27 01:16:30 -0800126 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128EXPORT_SYMBOL(register_module_notifier);
129
130int unregister_module_notifier(struct notifier_block * nb)
131{
Alan Sterne041c682006-03-27 01:16:30 -0800132 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134EXPORT_SYMBOL(unregister_module_notifier);
135
Rusty Russelleded41c2010-08-05 12:59:07 -0600136struct load_info {
137 Elf_Ehdr *hdr;
138 unsigned long len;
139 Elf_Shdr *sechdrs;
Rusty Russell6526c532010-08-05 12:59:10 -0600140 char *secstrings, *strtab;
Rusty Russelld9131882010-08-05 12:59:08 -0600141 unsigned long *strmap;
142 unsigned long symoffs, stroffs;
Rusty Russell811d66a2010-08-05 12:59:12 -0600143 struct _ddebug *debug;
144 unsigned int num_debug;
Rusty Russelleded41c2010-08-05 12:59:07 -0600145 struct {
146 unsigned int sym, str, mod, vers, info, pcpu;
147 } index;
148};
149
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -0800150/* We require a truly strong try_module_get(): 0 means failure due to
151 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static inline int strong_try_module_get(struct module *mod)
153{
154 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500155 return -EBUSY;
156 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500158 else
159 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700162static inline void add_taint_module(struct module *mod, unsigned flag)
163{
164 add_taint(flag);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700165 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700166}
167
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200168/*
169 * A thread that wants to hold a reference to a module only while it
170 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 */
172void __module_put_and_exit(struct module *mod, long code)
173{
174 module_put(mod);
175 do_exit(code);
176}
177EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/* Find a module section: 0 means not found. */
Rusty Russell49668682010-08-05 12:59:10 -0600180static unsigned int find_sec(const struct load_info *info, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 unsigned int i;
183
Rusty Russell49668682010-08-05 12:59:10 -0600184 for (i = 1; i < info->hdr->e_shnum; i++) {
185 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 /* Alloc bit cleared means "ignore it." */
Rusty Russell49668682010-08-05 12:59:10 -0600187 if ((shdr->sh_flags & SHF_ALLOC)
188 && strcmp(info->secstrings + shdr->sh_name, name) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return i;
Rusty Russell49668682010-08-05 12:59:10 -0600190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return 0;
192}
193
Rusty Russell5e458cc2008-10-22 10:00:13 -0500194/* Find a module section, or NULL. */
Rusty Russell49668682010-08-05 12:59:10 -0600195static void *section_addr(const struct load_info *info, const char *name)
Rusty Russell5e458cc2008-10-22 10:00:13 -0500196{
197 /* Section 0 has sh_addr 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600198 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500199}
200
201/* Find a module section, or NULL. Fill in number of "objects" in section. */
Rusty Russell49668682010-08-05 12:59:10 -0600202static void *section_objs(const struct load_info *info,
Rusty Russell5e458cc2008-10-22 10:00:13 -0500203 const char *name,
204 size_t object_size,
205 unsigned int *num)
206{
Rusty Russell49668682010-08-05 12:59:10 -0600207 unsigned int sec = find_sec(info, name);
Rusty Russell5e458cc2008-10-22 10:00:13 -0500208
209 /* Section 0 has sh_addr 0 and sh_size 0. */
Rusty Russell49668682010-08-05 12:59:10 -0600210 *num = info->sechdrs[sec].sh_size / object_size;
211 return (void *)info->sechdrs[sec].sh_addr;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500212}
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214/* Provided by the linker */
215extern const struct kernel_symbol __start___ksymtab[];
216extern const struct kernel_symbol __stop___ksymtab[];
217extern const struct kernel_symbol __start___ksymtab_gpl[];
218extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800219extern const struct kernel_symbol __start___ksymtab_gpl_future[];
220extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221extern const unsigned long __start___kcrctab[];
222extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800223extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500224#ifdef CONFIG_UNUSED_SYMBOLS
225extern const struct kernel_symbol __start___ksymtab_unused[];
226extern const struct kernel_symbol __stop___ksymtab_unused[];
227extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
228extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700229extern const unsigned long __start___kcrctab_unused[];
230extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500231#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233#ifndef CONFIG_MODVERSIONS
234#define symversion(base, idx) NULL
235#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800236#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237#endif
238
Rusty Russelldafd0942008-07-22 19:24:25 -0500239static bool each_symbol_in_section(const struct symsearch *arr,
240 unsigned int arrsize,
241 struct module *owner,
242 bool (*fn)(const struct symsearch *syms,
243 struct module *owner,
Rusty Russellde4d8d52011-04-19 21:49:58 +0200244 void *data),
Rusty Russelldafd0942008-07-22 19:24:25 -0500245 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100246{
Rusty Russellde4d8d52011-04-19 21:49:58 +0200247 unsigned int j;
Rusty Russelldafd0942008-07-22 19:24:25 -0500248
249 for (j = 0; j < arrsize; j++) {
Rusty Russellde4d8d52011-04-19 21:49:58 +0200250 if (fn(&arr[j], owner, data))
251 return true;
Rusty Russelldafd0942008-07-22 19:24:25 -0500252 }
253
254 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100255}
256
Rusty Russelldafd0942008-07-22 19:24:25 -0500257/* Returns true as soon as fn returns true, otherwise false. */
Rusty Russellde4d8d52011-04-19 21:49:58 +0200258bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
259 struct module *owner,
260 void *data),
261 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700262{
Rusty Russelldafd0942008-07-22 19:24:25 -0500263 struct module *mod;
Linus Torvalds44032e62010-08-05 12:59:05 -0600264 static const struct symsearch arr[] = {
Rusty Russelldafd0942008-07-22 19:24:25 -0500265 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
266 NOT_GPL_ONLY, false },
267 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
268 __start___kcrctab_gpl,
269 GPL_ONLY, false },
270 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
271 __start___kcrctab_gpl_future,
272 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500273#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500274 { __start___ksymtab_unused, __stop___ksymtab_unused,
275 __start___kcrctab_unused,
276 NOT_GPL_ONLY, true },
277 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
278 __start___kcrctab_unused_gpl,
279 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500280#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500281 };
282
283 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
284 return true;
285
Andi Kleend72b3752008-08-30 10:09:00 +0200286 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500287 struct symsearch arr[] = {
288 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
289 NOT_GPL_ONLY, false },
290 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
291 mod->gpl_crcs,
292 GPL_ONLY, false },
293 { mod->gpl_future_syms,
294 mod->gpl_future_syms + mod->num_gpl_future_syms,
295 mod->gpl_future_crcs,
296 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500297#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500298 { mod->unused_syms,
299 mod->unused_syms + mod->num_unused_syms,
300 mod->unused_crcs,
301 NOT_GPL_ONLY, true },
302 { mod->unused_gpl_syms,
303 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
304 mod->unused_gpl_crcs,
305 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500306#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500307 };
308
309 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
310 return true;
311 }
312 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700313}
Rusty Russellde4d8d52011-04-19 21:49:58 +0200314EXPORT_SYMBOL_GPL(each_symbol_section);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700315
Rusty Russelldafd0942008-07-22 19:24:25 -0500316struct find_symbol_arg {
317 /* Input */
318 const char *name;
319 bool gplok;
320 bool warn;
321
322 /* Output */
323 struct module *owner;
324 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500325 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500326};
327
Rusty Russellde4d8d52011-04-19 21:49:58 +0200328static bool check_symbol(const struct symsearch *syms,
329 struct module *owner,
330 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500331{
Rusty Russelldafd0942008-07-22 19:24:25 -0500332 struct find_symbol_arg *fsa = data;
333
Rusty Russelldafd0942008-07-22 19:24:25 -0500334 if (!fsa->gplok) {
335 if (syms->licence == GPL_ONLY)
336 return false;
337 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
338 printk(KERN_WARNING "Symbol %s is being used "
339 "by a non-GPL module, which will not "
340 "be allowed in the future\n", fsa->name);
341 printk(KERN_WARNING "Please see the file "
342 "Documentation/feature-removal-schedule.txt "
343 "in the kernel source tree for more details.\n");
344 }
345 }
346
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500347#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500348 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500349 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500350 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500351 printk(KERN_WARNING
352 "This symbol will go away in the future.\n");
353 printk(KERN_WARNING
354 "Please evalute if this is the right api to use and if "
355 "it really is, submit a report the linux kernel "
356 "mailinglist together with submitting your code for "
357 "inclusion.\n");
358 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500359#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500360
361 fsa->owner = owner;
362 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500363 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500364 return true;
365}
366
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200367static int cmp_name(const void *va, const void *vb)
368{
369 const char *a;
370 const struct kernel_symbol *b;
371 a = va; b = vb;
372 return strcmp(a, b->name);
373}
374
Rusty Russellde4d8d52011-04-19 21:49:58 +0200375static bool find_symbol_in_section(const struct symsearch *syms,
376 struct module *owner,
377 void *data)
378{
379 struct find_symbol_arg *fsa = data;
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200380 struct kernel_symbol *sym;
Rusty Russellde4d8d52011-04-19 21:49:58 +0200381
Alessio Igor Bogani403ed272011-04-20 11:10:52 +0200382 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
383 sizeof(struct kernel_symbol), cmp_name);
384
385 if (sym != NULL && check_symbol(syms, owner, sym - syms->start, data))
386 return true;
387
Rusty Russellde4d8d52011-04-19 21:49:58 +0200388 return false;
389}
390
Tim Abbott414fd312008-12-05 19:03:56 -0500391/* Find a symbol and return it, along with, (optional) crc and
Rusty Russell75676502010-06-05 11:17:36 -0600392 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500393const struct kernel_symbol *find_symbol(const char *name,
394 struct module **owner,
395 const unsigned long **crc,
396 bool gplok,
397 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Rusty Russelldafd0942008-07-22 19:24:25 -0500399 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Rusty Russelldafd0942008-07-22 19:24:25 -0500401 fsa.name = name;
402 fsa.gplok = gplok;
403 fsa.warn = warn;
404
Rusty Russellde4d8d52011-04-19 21:49:58 +0200405 if (each_symbol_section(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500406 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500407 *owner = fsa.owner;
408 if (crc)
409 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500410 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 DEBUGP("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500414 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
Tim Abbottc6b37802008-12-05 19:03:59 -0500416EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418/* Search for module by name: must hold module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500419struct module *find_module(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 struct module *mod;
422
423 list_for_each_entry(mod, &modules, list) {
424 if (strcmp(mod->name, name) == 0)
425 return mod;
426 }
427 return NULL;
428}
Tim Abbottc6b37802008-12-05 19:03:59 -0500429EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900432
Tejun Heo259354d2010-03-10 18:56:10 +0900433static inline void __percpu *mod_percpu(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900434{
Tejun Heo259354d2010-03-10 18:56:10 +0900435 return mod->percpu;
436}
Tejun Heofbf59bc2009-02-20 16:29:08 +0900437
Tejun Heo259354d2010-03-10 18:56:10 +0900438static int percpu_modalloc(struct module *mod,
439 unsigned long size, unsigned long align)
440{
Tejun Heofbf59bc2009-02-20 16:29:08 +0900441 if (align > PAGE_SIZE) {
442 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
Tejun Heo259354d2010-03-10 18:56:10 +0900443 mod->name, align, PAGE_SIZE);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900444 align = PAGE_SIZE;
445 }
446
Tejun Heo259354d2010-03-10 18:56:10 +0900447 mod->percpu = __alloc_reserved_percpu(size, align);
448 if (!mod->percpu) {
Tejun Heofbf59bc2009-02-20 16:29:08 +0900449 printk(KERN_WARNING
Rusty Russelld9131882010-08-05 12:59:08 -0600450 "%s: Could not allocate %lu bytes percpu data\n",
451 mod->name, size);
Tejun Heo259354d2010-03-10 18:56:10 +0900452 return -ENOMEM;
453 }
454 mod->percpu_size = size;
455 return 0;
Tejun Heofbf59bc2009-02-20 16:29:08 +0900456}
457
Tejun Heo259354d2010-03-10 18:56:10 +0900458static void percpu_modfree(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900459{
Tejun Heo259354d2010-03-10 18:56:10 +0900460 free_percpu(mod->percpu);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900461}
462
Rusty Russell49668682010-08-05 12:59:10 -0600463static unsigned int find_pcpusec(struct load_info *info)
Tejun Heo6b588c12009-02-20 16:29:07 +0900464{
Rusty Russell49668682010-08-05 12:59:10 -0600465 return find_sec(info, ".data..percpu");
Tejun Heo6b588c12009-02-20 16:29:07 +0900466}
467
Tejun Heo259354d2010-03-10 18:56:10 +0900468static void percpu_modcopy(struct module *mod,
469 const void *from, unsigned long size)
Tejun Heo6b588c12009-02-20 16:29:07 +0900470{
471 int cpu;
472
473 for_each_possible_cpu(cpu)
Tejun Heo259354d2010-03-10 18:56:10 +0900474 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
Tejun Heo6b588c12009-02-20 16:29:07 +0900475}
476
Tejun Heo10fad5e2010-03-10 18:57:54 +0900477/**
478 * is_module_percpu_address - test whether address is from module static percpu
479 * @addr: address to test
480 *
481 * Test whether @addr belongs to module static percpu area.
482 *
483 * RETURNS:
484 * %true if @addr is from module static percpu area
485 */
486bool is_module_percpu_address(unsigned long addr)
487{
488 struct module *mod;
489 unsigned int cpu;
490
491 preempt_disable();
492
493 list_for_each_entry_rcu(mod, &modules, list) {
494 if (!mod->percpu_size)
495 continue;
496 for_each_possible_cpu(cpu) {
497 void *start = per_cpu_ptr(mod->percpu, cpu);
498
499 if ((void *)addr >= start &&
500 (void *)addr < start + mod->percpu_size) {
501 preempt_enable();
502 return true;
503 }
504 }
505 }
506
507 preempt_enable();
508 return false;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700509}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900512
Tejun Heo259354d2010-03-10 18:56:10 +0900513static inline void __percpu *mod_percpu(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 return NULL;
516}
Tejun Heo259354d2010-03-10 18:56:10 +0900517static inline int percpu_modalloc(struct module *mod,
518 unsigned long size, unsigned long align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Tejun Heo259354d2010-03-10 18:56:10 +0900520 return -ENOMEM;
521}
522static inline void percpu_modfree(struct module *mod)
523{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
Rusty Russell49668682010-08-05 12:59:10 -0600525static unsigned int find_pcpusec(struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 return 0;
528}
Tejun Heo259354d2010-03-10 18:56:10 +0900529static inline void percpu_modcopy(struct module *mod,
530 const void *from, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
532 /* pcpusec should be 0, and size of that section should be 0. */
533 BUG_ON(size != 0);
534}
Tejun Heo10fad5e2010-03-10 18:57:54 +0900535bool is_module_percpu_address(unsigned long addr)
536{
537 return false;
538}
Tejun Heo6b588c12009-02-20 16:29:07 +0900539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540#endif /* CONFIG_SMP */
541
Matt Domschc988d2b2005-06-23 22:05:15 -0700542#define MODINFO_ATTR(field) \
543static void setup_modinfo_##field(struct module *mod, const char *s) \
544{ \
545 mod->field = kstrdup(s, GFP_KERNEL); \
546} \
547static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
548 struct module *mod, char *buffer) \
549{ \
550 return sprintf(buffer, "%s\n", mod->field); \
551} \
552static int modinfo_##field##_exists(struct module *mod) \
553{ \
554 return mod->field != NULL; \
555} \
556static void free_modinfo_##field(struct module *mod) \
557{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700558 kfree(mod->field); \
559 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700560} \
561static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900562 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700563 .show = show_modinfo_##field, \
564 .setup = setup_modinfo_##field, \
565 .test = modinfo_##field##_exists, \
566 .free = free_modinfo_##field, \
567};
568
569MODINFO_ATTR(version);
570MODINFO_ATTR(srcversion);
571
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100572static char last_unloaded_module[MODULE_NAME_LEN+1];
573
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800574#ifdef CONFIG_MODULE_UNLOAD
Steven Rostedteb0c5372010-03-29 14:25:18 -0400575
576EXPORT_TRACEPOINT_SYMBOL(module_get);
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578/* Init the unload section of the module. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600579static int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600581 mod->refptr = alloc_percpu(struct module_ref);
582 if (!mod->refptr)
583 return -ENOMEM;
584
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700585 INIT_LIST_HEAD(&mod->source_list);
586 INIT_LIST_HEAD(&mod->target_list);
Christoph Lametere1783a22010-01-05 15:34:50 +0900587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 /* Hold reference count during initialization. */
Nick Piggin5fbfb182010-04-01 19:09:40 +1100589 __this_cpu_write(mod->refptr->incs, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 /* Backwards compatibility macros put refcount during init. */
591 mod->waiter = current;
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600592
593 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596/* Does a already use b? */
597static int already_uses(struct module *a, struct module *b)
598{
599 struct module_use *use;
600
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700601 list_for_each_entry(use, &b->source_list, source_list) {
602 if (use->source == a) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 DEBUGP("%s uses %s!\n", a->name, b->name);
604 return 1;
605 }
606 }
607 DEBUGP("%s does not use %s!\n", a->name, b->name);
608 return 0;
609}
610
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700611/*
612 * Module a uses b
613 * - we add 'a' as a "source", 'b' as a "target" of module use
614 * - the module_use is added to the list of 'b' sources (so
615 * 'b' can walk the list to see who sourced them), and of 'a'
616 * targets (so 'a' can see what modules it targets).
617 */
618static int add_module_usage(struct module *a, struct module *b)
619{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700620 struct module_use *use;
621
622 DEBUGP("Allocating new usage for %s.\n", a->name);
623 use = kmalloc(sizeof(*use), GFP_ATOMIC);
624 if (!use) {
625 printk(KERN_WARNING "%s: out of memory loading\n", a->name);
626 return -ENOMEM;
627 }
628
629 use->source = a;
630 use->target = b;
631 list_add(&use->source_list, &b->source_list);
632 list_add(&use->target_list, &a->target_list);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700633 return 0;
634}
635
Rusty Russell75676502010-06-05 11:17:36 -0600636/* Module a uses b: caller needs module_mutex() */
Rusty Russell9bea7f22010-06-05 11:17:37 -0600637int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Rusty Russellc8e21ce2010-06-05 11:17:35 -0600639 int err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100640
Rusty Russell9bea7f22010-06-05 11:17:37 -0600641 if (b == NULL || already_uses(a, b))
Linus Torvalds218ce732010-05-25 16:48:30 -0700642 return 0;
Linus Torvalds218ce732010-05-25 16:48:30 -0700643
Rusty Russell9bea7f22010-06-05 11:17:37 -0600644 /* If module isn't available, we fail. */
645 err = strong_try_module_get(b);
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500646 if (err)
Rusty Russell9bea7f22010-06-05 11:17:37 -0600647 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700649 err = add_module_usage(a, b);
650 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 module_put(b);
Rusty Russell9bea7f22010-06-05 11:17:37 -0600652 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
Rusty Russell9bea7f22010-06-05 11:17:37 -0600654 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600656EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658/* Clear the unload stuff of the module. */
659static void module_unload_free(struct module *mod)
660{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700661 struct module_use *use, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Rusty Russell75676502010-06-05 11:17:36 -0600663 mutex_lock(&module_mutex);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700664 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
665 struct module *i = use->target;
666 DEBUGP("%s unusing %s\n", mod->name, i->name);
667 module_put(i);
668 list_del(&use->source_list);
669 list_del(&use->target_list);
670 kfree(use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
Rusty Russell75676502010-06-05 11:17:36 -0600672 mutex_unlock(&module_mutex);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600673
674 free_percpu(mod->refptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
677#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800678static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 int ret = (flags & O_TRUNC);
681 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800682 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return ret;
684}
685#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800686static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 return 0;
689}
690#endif /* CONFIG_MODULE_FORCE_UNLOAD */
691
692struct stopref
693{
694 struct module *mod;
695 int flags;
696 int *forced;
697};
698
699/* Whole machine is stopped with interrupts off when this runs. */
700static int __try_stop_module(void *_sref)
701{
702 struct stopref *sref = _sref;
703
Rusty Russellda39ba52008-07-22 19:24:25 -0500704 /* If it's not unused, quit unless we're forcing. */
705 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800706 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return -EWOULDBLOCK;
708 }
709
710 /* Mark it as dying. */
711 sref->mod->state = MODULE_STATE_GOING;
712 return 0;
713}
714
715static int try_stop_module(struct module *mod, int flags, int *forced)
716{
Rusty Russellda39ba52008-07-22 19:24:25 -0500717 if (flags & O_NONBLOCK) {
718 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500720 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500721 } else {
722 /* We don't need to stop the machine for this. */
723 mod->state = MODULE_STATE_GOING;
724 synchronize_sched();
725 return 0;
726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
729unsigned int module_refcount(struct module *mod)
730{
Nick Piggin5fbfb182010-04-01 19:09:40 +1100731 unsigned int incs = 0, decs = 0;
Eric Dumazet720eba32009-02-03 13:31:36 +1030732 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Eric Dumazet720eba32009-02-03 13:31:36 +1030734 for_each_possible_cpu(cpu)
Nick Piggin5fbfb182010-04-01 19:09:40 +1100735 decs += per_cpu_ptr(mod->refptr, cpu)->decs;
736 /*
737 * ensure the incs are added up after the decs.
738 * module_put ensures incs are visible before decs with smp_wmb.
739 *
740 * This 2-count scheme avoids the situation where the refcount
741 * for CPU0 is read, then CPU0 increments the module refcount,
742 * then CPU1 drops that refcount, then the refcount for CPU1 is
743 * read. We would record a decrement but not its corresponding
744 * increment so we would see a low count (disaster).
745 *
746 * Rare situation? But module_refcount can be preempted, and we
747 * might be tallying up 4096+ CPUs. So it is not impossible.
748 */
749 smp_rmb();
750 for_each_possible_cpu(cpu)
751 incs += per_cpu_ptr(mod->refptr, cpu)->incs;
752 return incs - decs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754EXPORT_SYMBOL(module_refcount);
755
756/* This exists whether we can unload or not */
757static void free_module(struct module *mod);
758
759static void wait_for_zero_refcount(struct module *mod)
760{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500761 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800762 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 for (;;) {
764 DEBUGP("Looking at refcount...\n");
765 set_current_state(TASK_UNINTERRUPTIBLE);
766 if (module_refcount(mod) == 0)
767 break;
768 schedule();
769 }
770 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800771 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100774SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
775 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
777 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800778 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 int ret, forced = 0;
780
Kees Cook3d433212009-04-02 15:49:29 -0700781 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800782 return -EPERM;
783
784 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
785 return -EFAULT;
786 name[MODULE_NAME_LEN-1] = '\0';
787
Tejun Heo3fc1f1e2010-05-06 18:49:20 +0200788 if (mutex_lock_interruptible(&module_mutex) != 0)
789 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 mod = find_module(name);
792 if (!mod) {
793 ret = -ENOENT;
794 goto out;
795 }
796
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700797 if (!list_empty(&mod->source_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 /* Other modules depend on us: get rid of them first. */
799 ret = -EWOULDBLOCK;
800 goto out;
801 }
802
803 /* Doing init or already dying? */
804 if (mod->state != MODULE_STATE_LIVE) {
805 /* FIXME: if (force), slam module count and wake up
806 waiter --RR */
807 DEBUGP("%s already dying\n", mod->name);
808 ret = -EBUSY;
809 goto out;
810 }
811
812 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700813 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800814 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (!forced) {
816 /* This module can't be removed */
817 ret = -EBUSY;
818 goto out;
819 }
820 }
821
822 /* Set this up before setting mod->state */
823 mod->waiter = current;
824
825 /* Stop the machine so refcounts can't move and disable module. */
826 ret = try_stop_module(mod, flags, &forced);
827 if (ret != 0)
828 goto out;
829
830 /* Never wait if forced. */
831 if (!forced && module_refcount(mod) != 0)
832 wait_for_zero_refcount(mod);
833
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200834 mutex_unlock(&module_mutex);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300835 /* Final destruction now no one is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200836 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200838 blocking_notifier_call_chain(&module_notify_list,
839 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800840 async_synchronize_full();
Rusty Russell75676502010-06-05 11:17:36 -0600841
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100842 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500843 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Rusty Russell75676502010-06-05 11:17:36 -0600845 free_module(mod);
846 return 0;
847out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800848 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return ret;
850}
851
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800852static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
854 struct module_use *use;
855 int printed_something = 0;
856
857 seq_printf(m, " %u ", module_refcount(mod));
858
859 /* Always include a trailing , so userspace can differentiate
860 between this and the old multi-field proc format. */
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700861 list_for_each_entry(use, &mod->source_list, source_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 printed_something = 1;
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700863 seq_printf(m, "%s,", use->source->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (mod->init != NULL && mod->exit == NULL) {
867 printed_something = 1;
868 seq_printf(m, "[permanent],");
869 }
870
871 if (!printed_something)
872 seq_printf(m, "-");
873}
874
875void __symbol_put(const char *symbol)
876{
877 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Rusty Russell24da1cb2007-07-15 23:41:46 -0700879 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -0500880 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 BUG();
882 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700883 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885EXPORT_SYMBOL(__symbol_put);
886
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930887/* Note this assumes addr is a function, which it currently always is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888void symbol_put_addr(void *addr)
889{
Trent Piepho5e376612006-05-15 09:44:06 -0700890 struct module *modaddr;
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930891 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930893 if (core_kernel_text(a))
Trent Piepho5e376612006-05-15 09:44:06 -0700894 return;
895
Rusty Russella6e6abd2009-03-31 13:05:31 -0600896 /* module_text_address is safe here: we're supposed to have reference
897 * to module from symbol_get, so it can't go away. */
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930898 modaddr = __module_text_address(a);
Rusty Russella6e6abd2009-03-31 13:05:31 -0600899 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -0700900 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902EXPORT_SYMBOL_GPL(symbol_put_addr);
903
904static ssize_t show_refcnt(struct module_attribute *mattr,
905 struct module *mod, char *buffer)
906{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400907 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
910static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900911 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 .show = show_refcnt,
913};
914
Al Virof6a57032006-10-18 01:47:25 -0400915void module_put(struct module *module)
916{
917 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +0900918 preempt_disable();
Nick Piggin5fbfb182010-04-01 19:09:40 +1100919 smp_wmb(); /* see comment in module_refcount */
920 __this_cpu_inc(module->refptr->decs);
Christoph Lametere1783a22010-01-05 15:34:50 +0900921
Li Zefanae832d12010-03-24 10:57:43 +0800922 trace_module_put(module, _RET_IP_);
Al Virof6a57032006-10-18 01:47:25 -0400923 /* Maybe they're waiting for us to drop reference? */
924 if (unlikely(!module_is_live(module)))
925 wake_up_process(module->waiter);
Christoph Lametere1783a22010-01-05 15:34:50 +0900926 preempt_enable();
Al Virof6a57032006-10-18 01:47:25 -0400927 }
928}
929EXPORT_SYMBOL(module_put);
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800932static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
934 /* We don't know the usage count, or what modules are using. */
935 seq_printf(m, " - -");
936}
937
938static inline void module_unload_free(struct module *mod)
939{
940}
941
Rusty Russell9bea7f22010-06-05 11:17:37 -0600942int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Rusty Russell9bea7f22010-06-05 11:17:37 -0600944 return strong_try_module_get(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600946EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600948static inline int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600950 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952#endif /* CONFIG_MODULE_UNLOAD */
953
Kay Sievers1f717402006-11-24 12:15:25 +0100954static ssize_t show_initstate(struct module_attribute *mattr,
955 struct module *mod, char *buffer)
956{
957 const char *state = "unknown";
958
959 switch (mod->state) {
960 case MODULE_STATE_LIVE:
961 state = "live";
962 break;
963 case MODULE_STATE_COMING:
964 state = "coming";
965 break;
966 case MODULE_STATE_GOING:
967 state = "going";
968 break;
969 }
970 return sprintf(buffer, "%s\n", state);
971}
972
973static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900974 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100975 .show = show_initstate,
976};
977
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800978static struct module_attribute *modinfo_attrs[] = {
979 &modinfo_version,
980 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100981 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800982#ifdef CONFIG_MODULE_UNLOAD
983 &refcnt,
984#endif
985 NULL,
986};
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988static const char vermagic[] = VERMAGIC_STRING;
989
Rusty Russellc6e665c2009-03-31 13:05:33 -0600990static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -0700991{
992#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -0700993 if (!test_taint(TAINT_FORCED_MODULE))
Rusty Russellc6e665c2009-03-31 13:05:33 -0600994 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
995 mod->name, reason);
Linus Torvalds826e4502008-05-04 17:04:16 -0700996 add_taint_module(mod, TAINT_FORCED_MODULE);
997 return 0;
998#else
999 return -ENOEXEC;
1000#endif
1001}
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003#ifdef CONFIG_MODVERSIONS
Rusty Russelld4703ae2009-12-15 16:28:32 -06001004/* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
1005static unsigned long maybe_relocated(unsigned long crc,
1006 const struct module *crc_owner)
1007{
1008#ifdef ARCH_RELOCATES_KCRCTAB
1009 if (crc_owner == NULL)
1010 return crc - (unsigned long)reloc_start;
1011#endif
1012 return crc;
1013}
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015static int check_version(Elf_Shdr *sechdrs,
1016 unsigned int versindex,
1017 const char *symname,
1018 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001019 const unsigned long *crc,
1020 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
1022 unsigned int i, num_versions;
1023 struct modversion_info *versions;
1024
1025 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1026 if (!crc)
1027 return 1;
1028
Rusty Russella5dd6972008-05-09 16:24:21 +10001029 /* No versions at all? modprobe --force does this. */
1030 if (versindex == 0)
1031 return try_to_force_load(mod, symname) == 0;
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 versions = (void *) sechdrs[versindex].sh_addr;
1034 num_versions = sechdrs[versindex].sh_size
1035 / sizeof(struct modversion_info);
1036
1037 for (i = 0; i < num_versions; i++) {
1038 if (strcmp(versions[i].name, symname) != 0)
1039 continue;
1040
Rusty Russelld4703ae2009-12-15 16:28:32 -06001041 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 DEBUGP("Found checksum %lX vs module %lX\n",
Rusty Russelld4703ae2009-12-15 16:28:32 -06001044 maybe_relocated(*crc, crc_owner), versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001045 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001047
Rusty Russella5dd6972008-05-09 16:24:21 +10001048 printk(KERN_WARNING "%s: no symbol version for %s\n",
1049 mod->name, symname);
1050 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001051
1052bad_version:
1053 printk("%s: disagrees about version of symbol %s\n",
1054 mod->name, symname);
1055 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
1058static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1059 unsigned int versindex,
1060 struct module *mod)
1061{
1062 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Rusty Russell75676502010-06-05 11:17:36 -06001064 /* Since this should be found in kernel (which can't be removed),
1065 * no locking is necessary. */
Mike Frysinger6560dc12009-07-23 23:42:08 +09301066 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1067 &crc, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 BUG();
Rusty Russelld4703ae2009-12-15 16:28:32 -06001069 return check_version(sechdrs, versindex, "module_layout", mod, crc,
1070 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
Rusty Russell91e37a72008-05-09 16:25:28 +10001073/* First part is kernel version, which we ignore if module has crcs. */
1074static inline int same_magic(const char *amagic, const char *bmagic,
1075 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Rusty Russell91e37a72008-05-09 16:25:28 +10001077 if (has_crcs) {
1078 amagic += strcspn(amagic, " ");
1079 bmagic += strcspn(bmagic, " ");
1080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return strcmp(amagic, bmagic) == 0;
1082}
1083#else
1084static inline int check_version(Elf_Shdr *sechdrs,
1085 unsigned int versindex,
1086 const char *symname,
1087 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001088 const unsigned long *crc,
1089 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 return 1;
1092}
1093
1094static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1095 unsigned int versindex,
1096 struct module *mod)
1097{
1098 return 1;
1099}
1100
Rusty Russell91e37a72008-05-09 16:25:28 +10001101static inline int same_magic(const char *amagic, const char *bmagic,
1102 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
1104 return strcmp(amagic, bmagic) == 0;
1105}
1106#endif /* CONFIG_MODVERSIONS */
1107
Rusty Russell75676502010-06-05 11:17:36 -06001108/* Resolve a symbol for this module. I.e. if we find one, record usage. */
Rusty Russell49668682010-08-05 12:59:10 -06001109static const struct kernel_symbol *resolve_symbol(struct module *mod,
1110 const struct load_info *info,
Tim Abbott414fd312008-12-05 19:03:56 -05001111 const char *name,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001112 char ownername[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
1114 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001115 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 const unsigned long *crc;
Rusty Russell9bea7f22010-06-05 11:17:37 -06001117 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Rusty Russell75676502010-06-05 11:17:36 -06001119 mutex_lock(&module_mutex);
Tim Abbott414fd312008-12-05 19:03:56 -05001120 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001121 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001122 if (!sym)
1123 goto unlock;
1124
Rusty Russell49668682010-08-05 12:59:10 -06001125 if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
1126 owner)) {
Rusty Russell9bea7f22010-06-05 11:17:37 -06001127 sym = ERR_PTR(-EINVAL);
1128 goto getname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
Rusty Russell9bea7f22010-06-05 11:17:37 -06001130
1131 err = ref_module(mod, owner);
1132 if (err) {
1133 sym = ERR_PTR(err);
1134 goto getname;
1135 }
1136
1137getname:
1138 /* We must make copy under the lock if we failed to get ref. */
1139 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1140unlock:
Rusty Russell75676502010-06-05 11:17:36 -06001141 mutex_unlock(&module_mutex);
Linus Torvalds218ce732010-05-25 16:48:30 -07001142 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
Rusty Russell49668682010-08-05 12:59:10 -06001145static const struct kernel_symbol *
1146resolve_symbol_wait(struct module *mod,
1147 const struct load_info *info,
1148 const char *name)
Rusty Russell9bea7f22010-06-05 11:17:37 -06001149{
1150 const struct kernel_symbol *ksym;
Rusty Russell49668682010-08-05 12:59:10 -06001151 char owner[MODULE_NAME_LEN];
Rusty Russell9bea7f22010-06-05 11:17:37 -06001152
1153 if (wait_event_interruptible_timeout(module_wq,
Rusty Russell49668682010-08-05 12:59:10 -06001154 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1155 || PTR_ERR(ksym) != -EBUSY,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001156 30 * HZ) <= 0) {
1157 printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
Rusty Russell49668682010-08-05 12:59:10 -06001158 mod->name, owner);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001159 }
1160 return ksym;
1161}
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163/*
1164 * /sys/module/foo/sections stuff
1165 * J. Corbet <corbet@lwn.net>
1166 */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001167#ifdef CONFIG_SYSFS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001168
Rusty Russell8f6d0372010-08-05 12:59:09 -06001169#ifdef CONFIG_KALLSYMS
Ben Hutchings10b465a2009-12-19 14:43:01 +00001170static inline bool sect_empty(const Elf_Shdr *sect)
1171{
1172 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1173}
1174
Rusty Russella58730c2008-03-13 09:03:44 +00001175struct module_sect_attr
1176{
1177 struct module_attribute mattr;
1178 char *name;
1179 unsigned long address;
1180};
1181
1182struct module_sect_attrs
1183{
1184 struct attribute_group grp;
1185 unsigned int nsections;
1186 struct module_sect_attr attrs[0];
1187};
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189static ssize_t module_sect_show(struct module_attribute *mattr,
1190 struct module *mod, char *buf)
1191{
1192 struct module_sect_attr *sattr =
1193 container_of(mattr, struct module_sect_attr, mattr);
Kees Cook9f36e2c2011-03-22 16:34:22 -07001194 return sprintf(buf, "0x%pK\n", (void *)sattr->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001197static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1198{
Rusty Russella58730c2008-03-13 09:03:44 +00001199 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001200
1201 for (section = 0; section < sect_attrs->nsections; section++)
1202 kfree(sect_attrs->attrs[section].name);
1203 kfree(sect_attrs);
1204}
1205
Rusty Russell8f6d0372010-08-05 12:59:09 -06001206static void add_sect_attrs(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
1208 unsigned int nloaded = 0, i, size[2];
1209 struct module_sect_attrs *sect_attrs;
1210 struct module_sect_attr *sattr;
1211 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 /* Count loaded sections and allocate structures */
Rusty Russell8f6d0372010-08-05 12:59:09 -06001214 for (i = 0; i < info->hdr->e_shnum; i++)
1215 if (!sect_empty(&info->sechdrs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 nloaded++;
1217 size[0] = ALIGN(sizeof(*sect_attrs)
1218 + nloaded * sizeof(sect_attrs->attrs[0]),
1219 sizeof(sect_attrs->grp.attrs[0]));
1220 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001221 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1222 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return;
1224
1225 /* Setup section attributes. */
1226 sect_attrs->grp.name = "sections";
1227 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1228
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001229 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 sattr = &sect_attrs->attrs[0];
1231 gattr = &sect_attrs->grp.attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001232 for (i = 0; i < info->hdr->e_shnum; i++) {
1233 Elf_Shdr *sec = &info->sechdrs[i];
1234 if (sect_empty(sec))
Helge Deller35dead42009-12-03 00:29:15 +01001235 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001236 sattr->address = sec->sh_addr;
1237 sattr->name = kstrdup(info->secstrings + sec->sh_name,
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001238 GFP_KERNEL);
1239 if (sattr->name == NULL)
1240 goto out;
1241 sect_attrs->nsections++;
Eric W. Biederman361795b2010-02-12 13:41:56 -08001242 sysfs_attr_init(&sattr->mattr.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 sattr->mattr.show = module_sect_show;
1244 sattr->mattr.store = NULL;
1245 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 sattr->mattr.attr.mode = S_IRUGO;
1247 *(gattr++) = &(sattr++)->mattr.attr;
1248 }
1249 *gattr = NULL;
1250
1251 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1252 goto out;
1253
1254 mod->sect_attrs = sect_attrs;
1255 return;
1256 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001257 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
1260static void remove_sect_attrs(struct module *mod)
1261{
1262 if (mod->sect_attrs) {
1263 sysfs_remove_group(&mod->mkobj.kobj,
1264 &mod->sect_attrs->grp);
1265 /* We are positive that no one is using any sect attrs
1266 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001267 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 mod->sect_attrs = NULL;
1269 }
1270}
1271
Roland McGrath6d760132007-10-16 23:26:40 -07001272/*
1273 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1274 */
1275
1276struct module_notes_attrs {
1277 struct kobject *dir;
1278 unsigned int notes;
1279 struct bin_attribute attrs[0];
1280};
1281
Chris Wright2c3c8be2010-05-12 18:28:57 -07001282static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
Roland McGrath6d760132007-10-16 23:26:40 -07001283 struct bin_attribute *bin_attr,
1284 char *buf, loff_t pos, size_t count)
1285{
1286 /*
1287 * The caller checked the pos and count against our size.
1288 */
1289 memcpy(buf, bin_attr->private + pos, count);
1290 return count;
1291}
1292
1293static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1294 unsigned int i)
1295{
1296 if (notes_attrs->dir) {
1297 while (i-- > 0)
1298 sysfs_remove_bin_file(notes_attrs->dir,
1299 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001300 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001301 }
1302 kfree(notes_attrs);
1303}
1304
Rusty Russell8f6d0372010-08-05 12:59:09 -06001305static void add_notes_attrs(struct module *mod, const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001306{
1307 unsigned int notes, loaded, i;
1308 struct module_notes_attrs *notes_attrs;
1309 struct bin_attribute *nattr;
1310
Ingo Molnarea6bff32009-08-28 10:44:56 +02001311 /* failed to create section attributes, so can't create notes */
1312 if (!mod->sect_attrs)
1313 return;
1314
Roland McGrath6d760132007-10-16 23:26:40 -07001315 /* Count notes sections and allocate structures. */
1316 notes = 0;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001317 for (i = 0; i < info->hdr->e_shnum; i++)
1318 if (!sect_empty(&info->sechdrs[i]) &&
1319 (info->sechdrs[i].sh_type == SHT_NOTE))
Roland McGrath6d760132007-10-16 23:26:40 -07001320 ++notes;
1321
1322 if (notes == 0)
1323 return;
1324
1325 notes_attrs = kzalloc(sizeof(*notes_attrs)
1326 + notes * sizeof(notes_attrs->attrs[0]),
1327 GFP_KERNEL);
1328 if (notes_attrs == NULL)
1329 return;
1330
1331 notes_attrs->notes = notes;
1332 nattr = &notes_attrs->attrs[0];
Rusty Russell8f6d0372010-08-05 12:59:09 -06001333 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1334 if (sect_empty(&info->sechdrs[i]))
Roland McGrath6d760132007-10-16 23:26:40 -07001335 continue;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001336 if (info->sechdrs[i].sh_type == SHT_NOTE) {
Eric W. Biederman361795b2010-02-12 13:41:56 -08001337 sysfs_bin_attr_init(nattr);
Roland McGrath6d760132007-10-16 23:26:40 -07001338 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1339 nattr->attr.mode = S_IRUGO;
Rusty Russell8f6d0372010-08-05 12:59:09 -06001340 nattr->size = info->sechdrs[i].sh_size;
1341 nattr->private = (void *) info->sechdrs[i].sh_addr;
Roland McGrath6d760132007-10-16 23:26:40 -07001342 nattr->read = module_notes_read;
1343 ++nattr;
1344 }
1345 ++loaded;
1346 }
1347
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001348 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001349 if (!notes_attrs->dir)
1350 goto out;
1351
1352 for (i = 0; i < notes; ++i)
1353 if (sysfs_create_bin_file(notes_attrs->dir,
1354 &notes_attrs->attrs[i]))
1355 goto out;
1356
1357 mod->notes_attrs = notes_attrs;
1358 return;
1359
1360 out:
1361 free_notes_attrs(notes_attrs, i);
1362}
1363
1364static void remove_notes_attrs(struct module *mod)
1365{
1366 if (mod->notes_attrs)
1367 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1368}
1369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001371
Rusty Russell8f6d0372010-08-05 12:59:09 -06001372static inline void add_sect_attrs(struct module *mod,
1373 const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375}
1376
1377static inline void remove_sect_attrs(struct module *mod)
1378{
1379}
Roland McGrath6d760132007-10-16 23:26:40 -07001380
Rusty Russell8f6d0372010-08-05 12:59:09 -06001381static inline void add_notes_attrs(struct module *mod,
1382 const struct load_info *info)
Roland McGrath6d760132007-10-16 23:26:40 -07001383{
1384}
1385
1386static inline void remove_notes_attrs(struct module *mod)
1387{
1388}
Rusty Russell8f6d0372010-08-05 12:59:09 -06001389#endif /* CONFIG_KALLSYMS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001391static void add_usage_links(struct module *mod)
1392{
1393#ifdef CONFIG_MODULE_UNLOAD
1394 struct module_use *use;
1395 int nowarn;
1396
Rusty Russell75676502010-06-05 11:17:36 -06001397 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001398 list_for_each_entry(use, &mod->target_list, target_list) {
1399 nowarn = sysfs_create_link(use->target->holders_dir,
1400 &mod->mkobj.kobj, mod->name);
1401 }
Rusty Russell75676502010-06-05 11:17:36 -06001402 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001403#endif
1404}
1405
1406static void del_usage_links(struct module *mod)
1407{
1408#ifdef CONFIG_MODULE_UNLOAD
1409 struct module_use *use;
1410
Rusty Russell75676502010-06-05 11:17:36 -06001411 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001412 list_for_each_entry(use, &mod->target_list, target_list)
1413 sysfs_remove_link(use->target->holders_dir, mod->name);
Rusty Russell75676502010-06-05 11:17:36 -06001414 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001415#endif
1416}
1417
Rusty Russell6407ebb22010-06-05 11:17:36 -06001418static int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001419{
1420 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001421 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001422 int error = 0;
1423 int i;
1424
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001425 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1426 (ARRAY_SIZE(modinfo_attrs) + 1)),
1427 GFP_KERNEL);
1428 if (!mod->modinfo_attrs)
1429 return -ENOMEM;
1430
1431 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001432 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1433 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001434 (attr->test && attr->test(mod))) {
1435 memcpy(temp_attr, attr, sizeof(*temp_attr));
Eric W. Biederman361795b2010-02-12 13:41:56 -08001436 sysfs_attr_init(&temp_attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001437 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1438 ++temp_attr;
1439 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001440 }
1441 return error;
1442}
1443
Rusty Russell6407ebb22010-06-05 11:17:36 -06001444static void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001445{
1446 struct module_attribute *attr;
1447 int i;
1448
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001449 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1450 /* pick a field to test for end of list */
1451 if (!attr->attr.name)
1452 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001453 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001454 if (attr->free)
1455 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001456 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001457 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001458}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Rusty Russell6407ebb22010-06-05 11:17:36 -06001460static int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
1462 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001463 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001465 if (!module_sysfs_initialized) {
1466 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001467 mod->name);
1468 err = -EINVAL;
1469 goto out;
1470 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001471
1472 kobj = kset_find_obj(module_kset, mod->name);
1473 if (kobj) {
1474 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1475 kobject_put(kobj);
1476 err = -EINVAL;
1477 goto out;
1478 }
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001481
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001482 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1483 mod->mkobj.kobj.kset = module_kset;
1484 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1485 "%s", mod->name);
1486 if (err)
1487 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001488
Kay Sievers97c146e2007-11-29 23:46:11 +01001489 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001490out:
1491 return err;
1492}
1493
Rusty Russell6407ebb22010-06-05 11:17:36 -06001494static int mod_sysfs_setup(struct module *mod,
Rusty Russell8f6d0372010-08-05 12:59:09 -06001495 const struct load_info *info,
Kay Sievers270a6c42007-01-18 13:26:15 +01001496 struct kernel_param *kparam,
1497 unsigned int num_params)
1498{
1499 int err;
1500
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001501 err = mod_sysfs_init(mod);
1502 if (err)
1503 goto out;
1504
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001505 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001506 if (!mod->holders_dir) {
1507 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001508 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001509 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 err = module_param_sysfs_setup(mod, kparam, num_params);
1512 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001513 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Matt Domschc988d2b2005-06-23 22:05:15 -07001515 err = module_add_modinfo_attrs(mod);
1516 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001517 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001518
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001519 add_usage_links(mod);
Rusty Russell8f6d0372010-08-05 12:59:09 -06001520 add_sect_attrs(mod, info);
1521 add_notes_attrs(mod, info);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001522
Kay Sieverse17e0f52006-11-24 12:15:25 +01001523 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return 0;
1525
Kay Sieverse17e0f52006-11-24 12:15:25 +01001526out_unreg_param:
1527 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001528out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001529 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001530out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001531 kobject_put(&mod->mkobj.kobj);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001532out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return err;
1534}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001535
1536static void mod_sysfs_fini(struct module *mod)
1537{
Rusty Russell8f6d0372010-08-05 12:59:09 -06001538 remove_notes_attrs(mod);
1539 remove_sect_attrs(mod);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001540 kobject_put(&mod->mkobj.kobj);
1541}
1542
Rusty Russell8f6d0372010-08-05 12:59:09 -06001543#else /* !CONFIG_SYSFS */
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001544
Rusty Russell8f6d0372010-08-05 12:59:09 -06001545static int mod_sysfs_setup(struct module *mod,
1546 const struct load_info *info,
Rusty Russell6407ebb22010-06-05 11:17:36 -06001547 struct kernel_param *kparam,
1548 unsigned int num_params)
1549{
1550 return 0;
1551}
1552
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001553static void mod_sysfs_fini(struct module *mod)
1554{
1555}
1556
Rusty Russell36b03602010-08-05 12:59:09 -06001557static void module_remove_modinfo_attrs(struct module *mod)
1558{
1559}
1560
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001561static void del_usage_links(struct module *mod)
1562{
1563}
1564
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001565#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Rusty Russell36b03602010-08-05 12:59:09 -06001567static void mod_sysfs_teardown(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001569 del_usage_links(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001570 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001572 kobject_put(mod->mkobj.drivers_dir);
1573 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001574 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
1577/*
1578 * unlink the module with the whole machine is stopped with interrupts off
1579 * - this defends against kallsyms not taking locks
1580 */
1581static int __unlink_module(void *_mod)
1582{
1583 struct module *mod = _mod;
1584 list_del(&mod->list);
Linus Torvalds53363772010-10-05 11:29:27 -07001585 module_bug_cleanup(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 return 0;
1587}
1588
matthieu castet84e1c6b2010-11-16 22:35:16 +01001589#ifdef CONFIG_DEBUG_SET_MODULE_RONX
1590/*
1591 * LKM RO/NX protection: protect module's text/ro-data
1592 * from modification and any data from execution.
1593 */
1594void set_page_attributes(void *start, void *end, int (*set)(unsigned long start, int num_pages))
1595{
1596 unsigned long begin_pfn = PFN_DOWN((unsigned long)start);
1597 unsigned long end_pfn = PFN_DOWN((unsigned long)end);
1598
1599 if (end_pfn > begin_pfn)
1600 set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1601}
1602
1603static void set_section_ro_nx(void *base,
1604 unsigned long text_size,
1605 unsigned long ro_size,
1606 unsigned long total_size)
1607{
1608 /* begin and end PFNs of the current subsection */
1609 unsigned long begin_pfn;
1610 unsigned long end_pfn;
1611
1612 /*
1613 * Set RO for module text and RO-data:
1614 * - Always protect first page.
1615 * - Do not protect last partial page.
1616 */
1617 if (ro_size > 0)
1618 set_page_attributes(base, base + ro_size, set_memory_ro);
1619
1620 /*
1621 * Set NX permissions for module data:
1622 * - Do not protect first partial page.
1623 * - Always protect last page.
1624 */
1625 if (total_size > text_size) {
1626 begin_pfn = PFN_UP((unsigned long)base + text_size);
1627 end_pfn = PFN_UP((unsigned long)base + total_size);
1628 if (end_pfn > begin_pfn)
1629 set_memory_nx(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1630 }
1631}
1632
Jan Glauber01526ed2011-05-19 16:55:26 -06001633static void unset_module_core_ro_nx(struct module *mod)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001634{
Jan Glauber01526ed2011-05-19 16:55:26 -06001635 set_page_attributes(mod->module_core + mod->core_text_size,
1636 mod->module_core + mod->core_size,
1637 set_memory_x);
1638 set_page_attributes(mod->module_core,
1639 mod->module_core + mod->core_ro_size,
1640 set_memory_rw);
1641}
1642
1643static void unset_module_init_ro_nx(struct module *mod)
1644{
1645 set_page_attributes(mod->module_init + mod->init_text_size,
1646 mod->module_init + mod->init_size,
1647 set_memory_x);
1648 set_page_attributes(mod->module_init,
1649 mod->module_init + mod->init_ro_size,
1650 set_memory_rw);
matthieu castet84e1c6b2010-11-16 22:35:16 +01001651}
1652
1653/* Iterate through all modules and set each module's text as RW */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001654void set_all_modules_text_rw(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001655{
1656 struct module *mod;
1657
1658 mutex_lock(&module_mutex);
1659 list_for_each_entry_rcu(mod, &modules, list) {
1660 if ((mod->module_core) && (mod->core_text_size)) {
1661 set_page_attributes(mod->module_core,
1662 mod->module_core + mod->core_text_size,
1663 set_memory_rw);
1664 }
1665 if ((mod->module_init) && (mod->init_text_size)) {
1666 set_page_attributes(mod->module_init,
1667 mod->module_init + mod->init_text_size,
1668 set_memory_rw);
1669 }
1670 }
1671 mutex_unlock(&module_mutex);
1672}
1673
1674/* Iterate through all modules and set each module's text as RO */
Daniel J Blueman5d05c702011-03-08 22:01:47 +08001675void set_all_modules_text_ro(void)
matthieu castet84e1c6b2010-11-16 22:35:16 +01001676{
1677 struct module *mod;
1678
1679 mutex_lock(&module_mutex);
1680 list_for_each_entry_rcu(mod, &modules, list) {
1681 if ((mod->module_core) && (mod->core_text_size)) {
1682 set_page_attributes(mod->module_core,
1683 mod->module_core + mod->core_text_size,
1684 set_memory_ro);
1685 }
1686 if ((mod->module_init) && (mod->init_text_size)) {
1687 set_page_attributes(mod->module_init,
1688 mod->module_init + mod->init_text_size,
1689 set_memory_ro);
1690 }
1691 }
1692 mutex_unlock(&module_mutex);
1693}
1694#else
1695static inline void set_section_ro_nx(void *base, unsigned long text_size, unsigned long ro_size, unsigned long total_size) { }
Jan Glauber01526ed2011-05-19 16:55:26 -06001696static void unset_module_core_ro_nx(struct module *mod) { }
1697static void unset_module_init_ro_nx(struct module *mod) { }
matthieu castet84e1c6b2010-11-16 22:35:16 +01001698#endif
1699
Rusty Russell75676502010-06-05 11:17:36 -06001700/* Free a module, remove from lists, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701static void free_module(struct module *mod)
1702{
Li Zefan7ead8b82009-08-17 16:56:28 +08001703 trace_module_free(mod);
1704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 /* Delete from various lists */
Rusty Russell75676502010-06-05 11:17:36 -06001706 mutex_lock(&module_mutex);
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001707 stop_machine(__unlink_module, mod, NULL);
Rusty Russell75676502010-06-05 11:17:36 -06001708 mutex_unlock(&module_mutex);
Rusty Russell36b03602010-08-05 12:59:09 -06001709 mod_sysfs_teardown(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Jason Baronb82bab4b2010-07-27 13:18:01 -07001711 /* Remove dynamic debug info */
1712 ddebug_remove_module(mod->name);
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 /* Arch-specific cleanup. */
1715 module_arch_cleanup(mod);
1716
1717 /* Module unload stuff */
1718 module_unload_free(mod);
1719
Rusty Russelle180a6b2009-03-31 13:05:29 -06001720 /* Free any allocated parameters. */
1721 destroy_params(mod->kp, mod->num_kp);
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 /* This may be NULL, but that's OK */
Jan Glauber01526ed2011-05-19 16:55:26 -06001724 unset_module_init_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 module_free(mod, mod->module_init);
1726 kfree(mod->args);
Tejun Heo259354d2010-03-10 18:56:10 +09001727 percpu_modfree(mod);
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001728
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001729 /* Free lock-classes: */
1730 lockdep_free_key_range(mod->module_core, mod->core_size);
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 /* Finally, free the core (containing the module structure) */
Jan Glauber01526ed2011-05-19 16:55:26 -06001733 unset_module_core_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 module_free(mod, mod->module_core);
Bernd Schmidteb8cdec2009-09-21 17:03:57 -07001735
1736#ifdef CONFIG_MPU
1737 update_protections(current->mm);
1738#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739}
1740
1741void *__symbol_get(const char *symbol)
1742{
1743 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001744 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Rusty Russell24da1cb2007-07-15 23:41:46 -07001746 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001747 sym = find_symbol(symbol, &owner, NULL, true, true);
1748 if (sym && strong_try_module_get(owner))
1749 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001750 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Tim Abbott414fd312008-12-05 19:03:56 -05001752 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753}
1754EXPORT_SYMBOL_GPL(__symbol_get);
1755
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001756/*
1757 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001758 * in the kernel or in some other module's exported symbol table.
Rusty Russellbe593f42010-06-05 11:17:37 -06001759 *
1760 * You must hold the module_mutex.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001761 */
1762static int verify_export_symbols(struct module *mod)
1763{
Rusty Russellb2111042008-05-01 21:15:00 -05001764 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001765 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001766 const struct kernel_symbol *s;
1767 struct {
1768 const struct kernel_symbol *sym;
1769 unsigned int num;
1770 } arr[] = {
1771 { mod->syms, mod->num_syms },
1772 { mod->gpl_syms, mod->num_gpl_syms },
1773 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001774#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001775 { mod->unused_syms, mod->num_unused_syms },
1776 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001777#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001778 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001779
Rusty Russellb2111042008-05-01 21:15:00 -05001780 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1781 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Rusty Russellbe593f42010-06-05 11:17:37 -06001782 if (find_symbol(s->name, &owner, NULL, true, false)) {
Rusty Russellb2111042008-05-01 21:15:00 -05001783 printk(KERN_ERR
1784 "%s: exports duplicate symbol %s"
1785 " (owned by %s)\n",
1786 mod->name, s->name, module_name(owner));
1787 return -ENOEXEC;
1788 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001789 }
Rusty Russellb2111042008-05-01 21:15:00 -05001790 }
1791 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001792}
1793
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001794/* Change all symbols so that st_value encodes the pointer directly. */
Rusty Russell49668682010-08-05 12:59:10 -06001795static int simplify_symbols(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
Rusty Russell49668682010-08-05 12:59:10 -06001797 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
1798 Elf_Sym *sym = (void *)symsec->sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 unsigned long secbase;
Rusty Russell49668682010-08-05 12:59:10 -06001800 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05001802 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Rusty Russell49668682010-08-05 12:59:10 -06001804 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
1805 const char *name = info->strtab + sym[i].st_name;
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 switch (sym[i].st_shndx) {
1808 case SHN_COMMON:
1809 /* We compiled with -fno-common. These are not
1810 supposed to happen. */
Rusty Russell49668682010-08-05 12:59:10 -06001811 DEBUGP("Common symbol: %s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 printk("%s: please compile with -fno-common\n",
1813 mod->name);
1814 ret = -ENOEXEC;
1815 break;
1816
1817 case SHN_ABS:
1818 /* Don't need to do anything */
1819 DEBUGP("Absolute symbol: 0x%08lx\n",
1820 (long)sym[i].st_value);
1821 break;
1822
1823 case SHN_UNDEF:
Rusty Russell49668682010-08-05 12:59:10 -06001824 ksym = resolve_symbol_wait(mod, info, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 /* Ok if resolved. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001826 if (ksym && !IS_ERR(ksym)) {
Tim Abbott414fd312008-12-05 19:03:56 -05001827 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 break;
Tim Abbott414fd312008-12-05 19:03:56 -05001829 }
1830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 /* Ok if weak. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001832 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 break;
1834
Rusty Russell9bea7f22010-06-05 11:17:37 -06001835 printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
Rusty Russell49668682010-08-05 12:59:10 -06001836 mod->name, name, PTR_ERR(ksym));
Rusty Russell9bea7f22010-06-05 11:17:37 -06001837 ret = PTR_ERR(ksym) ?: -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 break;
1839
1840 default:
1841 /* Divert to percpu allocation if a percpu var. */
Rusty Russell49668682010-08-05 12:59:10 -06001842 if (sym[i].st_shndx == info->index.pcpu)
Tejun Heo259354d2010-03-10 18:56:10 +09001843 secbase = (unsigned long)mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 else
Rusty Russell49668682010-08-05 12:59:10 -06001845 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 sym[i].st_value += secbase;
1847 break;
1848 }
1849 }
1850
1851 return ret;
1852}
1853
Rusty Russell49668682010-08-05 12:59:10 -06001854static int apply_relocations(struct module *mod, const struct load_info *info)
Rusty Russell22e268e2010-08-05 12:59:05 -06001855{
1856 unsigned int i;
1857 int err = 0;
1858
1859 /* Now do relocations. */
Rusty Russell49668682010-08-05 12:59:10 -06001860 for (i = 1; i < info->hdr->e_shnum; i++) {
1861 unsigned int infosec = info->sechdrs[i].sh_info;
Rusty Russell22e268e2010-08-05 12:59:05 -06001862
1863 /* Not a valid relocation section? */
Rusty Russell49668682010-08-05 12:59:10 -06001864 if (infosec >= info->hdr->e_shnum)
Rusty Russell22e268e2010-08-05 12:59:05 -06001865 continue;
1866
1867 /* Don't bother with non-allocated sections */
Rusty Russell49668682010-08-05 12:59:10 -06001868 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
Rusty Russell22e268e2010-08-05 12:59:05 -06001869 continue;
1870
Rusty Russell49668682010-08-05 12:59:10 -06001871 if (info->sechdrs[i].sh_type == SHT_REL)
1872 err = apply_relocate(info->sechdrs, info->strtab,
1873 info->index.sym, i, mod);
1874 else if (info->sechdrs[i].sh_type == SHT_RELA)
1875 err = apply_relocate_add(info->sechdrs, info->strtab,
1876 info->index.sym, i, mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06001877 if (err < 0)
1878 break;
1879 }
1880 return err;
1881}
1882
Helge Deller088af9a2008-12-31 12:31:18 +01001883/* Additional bytes needed by arch in front of individual sections */
1884unsigned int __weak arch_mod_section_prepend(struct module *mod,
1885 unsigned int section)
1886{
1887 /* default implementation just returns zero */
1888 return 0;
1889}
1890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01001892static long get_offset(struct module *mod, unsigned int *size,
1893 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
1895 long ret;
1896
Helge Deller088af9a2008-12-31 12:31:18 +01001897 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1899 *size = ret + sechdr->sh_size;
1900 return ret;
1901}
1902
1903/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1904 might -- code, read-only data, read-write data, small data. Tally
1905 sizes, and place the offsets into sh_entsize fields: high bit means it
1906 belongs in init. */
Rusty Russell49668682010-08-05 12:59:10 -06001907static void layout_sections(struct module *mod, struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
1909 static unsigned long const masks[][2] = {
1910 /* NOTE: all executable code must be the first section
1911 * in this array; otherwise modify the text_size
1912 * finder in the two loops below */
1913 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1914 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1915 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1916 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1917 };
1918 unsigned int m, i;
1919
Rusty Russell49668682010-08-05 12:59:10 -06001920 for (i = 0; i < info->hdr->e_shnum; i++)
1921 info->sechdrs[i].sh_entsize = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
1923 DEBUGP("Core section allocation order:\n");
1924 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06001925 for (i = 0; i < info->hdr->e_shnum; ++i) {
1926 Elf_Shdr *s = &info->sechdrs[i];
1927 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
1929 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1930 || (s->sh_flags & masks[m][1])
1931 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06001932 || strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001934 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Rusty Russell49668682010-08-05 12:59:10 -06001935 DEBUGP("\t%s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01001937 switch (m) {
1938 case 0: /* executable */
1939 mod->core_size = debug_align(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 mod->core_text_size = mod->core_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01001941 break;
1942 case 1: /* RO: text and ro-data */
1943 mod->core_size = debug_align(mod->core_size);
1944 mod->core_ro_size = mod->core_size;
1945 break;
1946 case 3: /* whole core */
1947 mod->core_size = debug_align(mod->core_size);
1948 break;
1949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
1951
1952 DEBUGP("Init section allocation order:\n");
1953 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
Rusty Russell49668682010-08-05 12:59:10 -06001954 for (i = 0; i < info->hdr->e_shnum; ++i) {
1955 Elf_Shdr *s = &info->sechdrs[i];
1956 const char *sname = info->secstrings + s->sh_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1959 || (s->sh_flags & masks[m][1])
1960 || s->sh_entsize != ~0UL
Rusty Russell49668682010-08-05 12:59:10 -06001961 || !strstarts(sname, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001963 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 | INIT_OFFSET_MASK);
Rusty Russell49668682010-08-05 12:59:10 -06001965 DEBUGP("\t%s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 }
matthieu castet84e1c6b2010-11-16 22:35:16 +01001967 switch (m) {
1968 case 0: /* executable */
1969 mod->init_size = debug_align(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 mod->init_text_size = mod->init_size;
matthieu castet84e1c6b2010-11-16 22:35:16 +01001971 break;
1972 case 1: /* RO: text and ro-data */
1973 mod->init_size = debug_align(mod->init_size);
1974 mod->init_ro_size = mod->init_size;
1975 break;
1976 case 3: /* whole init */
1977 mod->init_size = debug_align(mod->init_size);
1978 break;
1979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 }
1981}
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983static void set_license(struct module *mod, const char *license)
1984{
1985 if (!license)
1986 license = "unspecified";
1987
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001988 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07001989 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001990 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001991 "kernel.\n", mod->name, license);
1992 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 }
1994}
1995
1996/* Parse tag=value strings from .modinfo section */
1997static char *next_string(char *string, unsigned long *secsize)
1998{
1999 /* Skip non-zero chars */
2000 while (string[0]) {
2001 string++;
2002 if ((*secsize)-- <= 1)
2003 return NULL;
2004 }
2005
2006 /* Skip any zero padding. */
2007 while (!string[0]) {
2008 string++;
2009 if ((*secsize)-- <= 1)
2010 return NULL;
2011 }
2012 return string;
2013}
2014
Rusty Russell49668682010-08-05 12:59:10 -06002015static char *get_modinfo(struct load_info *info, const char *tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
2017 char *p;
2018 unsigned int taglen = strlen(tag);
Rusty Russell49668682010-08-05 12:59:10 -06002019 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2020 unsigned long size = infosec->sh_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Rusty Russell49668682010-08-05 12:59:10 -06002022 for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2024 return p + taglen + 1;
2025 }
2026 return NULL;
2027}
2028
Rusty Russell49668682010-08-05 12:59:10 -06002029static void setup_modinfo(struct module *mod, struct load_info *info)
Matt Domschc988d2b2005-06-23 22:05:15 -07002030{
2031 struct module_attribute *attr;
2032 int i;
2033
2034 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2035 if (attr->setup)
Rusty Russell49668682010-08-05 12:59:10 -06002036 attr->setup(mod, get_modinfo(info, attr->attr.name));
Matt Domschc988d2b2005-06-23 22:05:15 -07002037 }
2038}
Matt Domschc988d2b2005-06-23 22:05:15 -07002039
Rusty Russella263f772009-09-25 00:32:58 -06002040static void free_modinfo(struct module *mod)
2041{
2042 struct module_attribute *attr;
2043 int i;
2044
2045 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2046 if (attr->free)
2047 attr->free(mod);
2048 }
2049}
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01002052
2053/* lookup symbol in given range of kernel_symbols */
2054static const struct kernel_symbol *lookup_symbol(const char *name,
2055 const struct kernel_symbol *start,
2056 const struct kernel_symbol *stop)
2057{
Alessio Igor Bogani9d634872011-05-18 22:35:59 +02002058 return bsearch(name, start, stop - start,
2059 sizeof(struct kernel_symbol), cmp_name);
WANG Cong15bba372008-07-24 15:41:48 +01002060}
2061
Tim Abbottca4787b2009-01-05 08:40:10 -06002062static int is_exported(const char *name, unsigned long value,
2063 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
Tim Abbottca4787b2009-01-05 08:40:10 -06002065 const struct kernel_symbol *ks;
2066 if (!mod)
2067 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01002068 else
Tim Abbottca4787b2009-01-05 08:40:10 -06002069 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
2070 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071}
2072
2073/* As per nm */
Rusty Russelleded41c2010-08-05 12:59:07 -06002074static char elf_type(const Elf_Sym *sym, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
Rusty Russelleded41c2010-08-05 12:59:07 -06002076 const Elf_Shdr *sechdrs = info->sechdrs;
2077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2079 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2080 return 'v';
2081 else
2082 return 'w';
2083 }
2084 if (sym->st_shndx == SHN_UNDEF)
2085 return 'U';
2086 if (sym->st_shndx == SHN_ABS)
2087 return 'a';
2088 if (sym->st_shndx >= SHN_LORESERVE)
2089 return '?';
2090 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2091 return 't';
2092 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2093 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2094 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2095 return 'r';
2096 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2097 return 'g';
2098 else
2099 return 'd';
2100 }
2101 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2102 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2103 return 's';
2104 else
2105 return 'b';
2106 }
Rusty Russelleded41c2010-08-05 12:59:07 -06002107 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2108 ".debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 return 'n';
Rusty Russelleded41c2010-08-05 12:59:07 -06002110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 return '?';
2112}
2113
Jan Beulich4a496222009-07-06 14:50:42 +01002114static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
2115 unsigned int shnum)
2116{
2117 const Elf_Shdr *sec;
2118
2119 if (src->st_shndx == SHN_UNDEF
2120 || src->st_shndx >= shnum
2121 || !src->st_name)
2122 return false;
2123
2124 sec = sechdrs + src->st_shndx;
2125 if (!(sec->sh_flags & SHF_ALLOC)
2126#ifndef CONFIG_KALLSYMS_ALL
2127 || !(sec->sh_flags & SHF_EXECINSTR)
2128#endif
2129 || (sec->sh_entsize & INIT_OFFSET_MASK))
2130 return false;
2131
2132 return true;
2133}
2134
Rusty Russell49668682010-08-05 12:59:10 -06002135static void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002136{
Rusty Russell49668682010-08-05 12:59:10 -06002137 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2138 Elf_Shdr *strsect = info->sechdrs + info->index.str;
Jan Beulich4a496222009-07-06 14:50:42 +01002139 const Elf_Sym *src;
2140 unsigned int i, nsrc, ndst;
2141
2142 /* Put symbol section at end of init part of module. */
2143 symsect->sh_flags |= SHF_ALLOC;
2144 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
Rusty Russell49668682010-08-05 12:59:10 -06002145 info->index.sym) | INIT_OFFSET_MASK;
2146 DEBUGP("\t%s\n", info->secstrings + symsect->sh_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002147
Rusty Russell49668682010-08-05 12:59:10 -06002148 src = (void *)info->hdr + symsect->sh_offset;
Jan Beulich4a496222009-07-06 14:50:42 +01002149 nsrc = symsect->sh_size / sizeof(*src);
2150 for (ndst = i = 1; i < nsrc; ++i, ++src)
Rusty Russell49668682010-08-05 12:59:10 -06002151 if (is_core_symbol(src, info->sechdrs, info->hdr->e_shnum)) {
Jan Beulich554bdfe2009-07-06 14:51:44 +01002152 unsigned int j = src->st_name;
2153
Rusty Russell49668682010-08-05 12:59:10 -06002154 while (!__test_and_set_bit(j, info->strmap)
2155 && info->strtab[j])
Jan Beulich554bdfe2009-07-06 14:51:44 +01002156 ++j;
Jan Beulich4a496222009-07-06 14:50:42 +01002157 ++ndst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002158 }
Jan Beulich4a496222009-07-06 14:50:42 +01002159
2160 /* Append room for core symbols at end of core part. */
Rusty Russell49668682010-08-05 12:59:10 -06002161 info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
2162 mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
Jan Beulich4a496222009-07-06 14:50:42 +01002163
Jan Beulich554bdfe2009-07-06 14:51:44 +01002164 /* Put string table section at end of init part of module. */
2165 strsect->sh_flags |= SHF_ALLOC;
2166 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
Rusty Russell49668682010-08-05 12:59:10 -06002167 info->index.str) | INIT_OFFSET_MASK;
2168 DEBUGP("\t%s\n", info->secstrings + strsect->sh_name);
Jan Beulich554bdfe2009-07-06 14:51:44 +01002169
2170 /* Append room for core symbols' strings at end of core part. */
Rusty Russell49668682010-08-05 12:59:10 -06002171 info->stroffs = mod->core_size;
2172 __set_bit(0, info->strmap);
2173 mod->core_size += bitmap_weight(info->strmap, strsect->sh_size);
Jan Beulich4a496222009-07-06 14:50:42 +01002174}
2175
Rusty Russell811d66a2010-08-05 12:59:12 -06002176static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
Jan Beulich4a496222009-07-06 14:50:42 +01002178 unsigned int i, ndst;
2179 const Elf_Sym *src;
2180 Elf_Sym *dst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002181 char *s;
Rusty Russelleded41c2010-08-05 12:59:07 -06002182 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Rusty Russelleded41c2010-08-05 12:59:07 -06002184 mod->symtab = (void *)symsec->sh_addr;
2185 mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
Rusty Russell511ca6a2010-08-05 12:59:08 -06002186 /* Make sure we get permanent strtab: don't use info->strtab. */
2187 mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 /* Set types up while we still have access to sections. */
2190 for (i = 0; i < mod->num_symtab; i++)
Rusty Russelleded41c2010-08-05 12:59:07 -06002191 mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
Jan Beulich4a496222009-07-06 14:50:42 +01002192
Rusty Russelld9131882010-08-05 12:59:08 -06002193 mod->core_symtab = dst = mod->module_core + info->symoffs;
Jan Beulich4a496222009-07-06 14:50:42 +01002194 src = mod->symtab;
2195 *dst = *src;
2196 for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
Rusty Russelleded41c2010-08-05 12:59:07 -06002197 if (!is_core_symbol(src, info->sechdrs, info->hdr->e_shnum))
Jan Beulich4a496222009-07-06 14:50:42 +01002198 continue;
2199 dst[ndst] = *src;
Rusty Russelld9131882010-08-05 12:59:08 -06002200 dst[ndst].st_name = bitmap_weight(info->strmap,
2201 dst[ndst].st_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002202 ++ndst;
2203 }
2204 mod->core_num_syms = ndst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002205
Rusty Russelld9131882010-08-05 12:59:08 -06002206 mod->core_strtab = s = mod->module_core + info->stroffs;
Rusty Russelleded41c2010-08-05 12:59:07 -06002207 for (*s = 0, i = 1; i < info->sechdrs[info->index.str].sh_size; ++i)
Rusty Russelld9131882010-08-05 12:59:08 -06002208 if (test_bit(i, info->strmap))
Jan Beulich554bdfe2009-07-06 14:51:44 +01002209 *++s = mod->strtab[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210}
2211#else
Rusty Russell49668682010-08-05 12:59:10 -06002212static inline void layout_symtab(struct module *mod, struct load_info *info)
Jan Beulich4a496222009-07-06 14:50:42 +01002213{
2214}
Paul Mundt3ae91c22009-10-01 15:43:54 -07002215
Michał Mirosławabbce902010-09-20 01:58:08 +02002216static void add_kallsyms(struct module *mod, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
2218}
2219#endif /* CONFIG_KALLSYMS */
2220
Jason Barone9d376f2009-02-05 11:51:38 -05002221static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05002222{
Rusty Russell811d66a2010-08-05 12:59:12 -06002223 if (!debug)
2224 return;
Jason Barone9d376f2009-02-05 11:51:38 -05002225#ifdef CONFIG_DYNAMIC_DEBUG
2226 if (ddebug_add_module(debug, num, debug->modname))
2227 printk(KERN_ERR "dynamic debug error adding module: %s\n",
2228 debug->modname);
2229#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002230}
Jason Baron346e15b2008-08-12 16:46:19 -04002231
Yehuda Sadehff49d742010-07-03 13:07:35 +10002232static void dynamic_debug_remove(struct _ddebug *debug)
2233{
2234 if (debug)
2235 ddebug_remove_module(debug->modname);
2236}
2237
Rusty Russell3a642e92008-07-22 19:24:28 -05002238static void *module_alloc_update_bounds(unsigned long size)
2239{
2240 void *ret = module_alloc(size);
2241
2242 if (ret) {
Rusty Russell75676502010-06-05 11:17:36 -06002243 mutex_lock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002244 /* Update module bounds. */
2245 if ((unsigned long)ret < module_addr_min)
2246 module_addr_min = (unsigned long)ret;
2247 if ((unsigned long)ret + size > module_addr_max)
2248 module_addr_max = (unsigned long)ret + size;
Rusty Russell75676502010-06-05 11:17:36 -06002249 mutex_unlock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002250 }
2251 return ret;
2252}
2253
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002254#ifdef CONFIG_DEBUG_KMEMLEAK
Rusty Russell49668682010-08-05 12:59:10 -06002255static void kmemleak_load_module(const struct module *mod,
2256 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002257{
2258 unsigned int i;
2259
2260 /* only scan the sections containing data */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002261 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002262
Rusty Russell49668682010-08-05 12:59:10 -06002263 for (i = 1; i < info->hdr->e_shnum; i++) {
2264 const char *name = info->secstrings + info->sechdrs[i].sh_name;
2265 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002266 continue;
Rusty Russell49668682010-08-05 12:59:10 -06002267 if (!strstarts(name, ".data") && !strstarts(name, ".bss"))
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002268 continue;
2269
Rusty Russell49668682010-08-05 12:59:10 -06002270 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2271 info->sechdrs[i].sh_size, GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002272 }
2273}
2274#else
Rusty Russell49668682010-08-05 12:59:10 -06002275static inline void kmemleak_load_module(const struct module *mod,
2276 const struct load_info *info)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002277{
2278}
2279#endif
2280
Rusty Russell6526c532010-08-05 12:59:10 -06002281/* Sets info->hdr and info->len. */
Rusty Russelld9131882010-08-05 12:59:08 -06002282static int copy_and_check(struct load_info *info,
2283 const void __user *umod, unsigned long len,
2284 const char __user *uargs)
Rusty Russell40dd2562010-08-05 12:59:03 -06002285{
2286 int err;
2287 Elf_Ehdr *hdr;
2288
2289 if (len < sizeof(*hdr))
2290 return -ENOEXEC;
2291
2292 /* Suck in entire file: we'll want most of it. */
2293 /* vmalloc barfs on "unusual" numbers. Check here */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002294 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
Rusty Russell40dd2562010-08-05 12:59:03 -06002295 return -ENOMEM;
2296
2297 if (copy_from_user(hdr, umod, len) != 0) {
2298 err = -EFAULT;
2299 goto free_hdr;
2300 }
2301
2302 /* Sanity checks against insmoding binaries or wrong arch,
2303 weird elf version */
2304 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
2305 || hdr->e_type != ET_REL
2306 || !elf_check_arch(hdr)
2307 || hdr->e_shentsize != sizeof(Elf_Shdr)) {
2308 err = -ENOEXEC;
2309 goto free_hdr;
2310 }
2311
2312 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) {
2313 err = -ENOEXEC;
2314 goto free_hdr;
2315 }
Rusty Russelld9131882010-08-05 12:59:08 -06002316
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002317 info->hdr = hdr;
2318 info->len = len;
Rusty Russell40dd2562010-08-05 12:59:03 -06002319 return 0;
2320
2321free_hdr:
2322 vfree(hdr);
2323 return err;
2324}
2325
Rusty Russelld9131882010-08-05 12:59:08 -06002326static void free_copy(struct load_info *info)
2327{
Rusty Russelld9131882010-08-05 12:59:08 -06002328 vfree(info->hdr);
2329}
2330
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002331static int rewrite_section_headers(struct load_info *info)
2332{
2333 unsigned int i;
2334
2335 /* This should always be true, but let's be sure. */
2336 info->sechdrs[0].sh_addr = 0;
2337
2338 for (i = 1; i < info->hdr->e_shnum; i++) {
2339 Elf_Shdr *shdr = &info->sechdrs[i];
2340 if (shdr->sh_type != SHT_NOBITS
2341 && info->len < shdr->sh_offset + shdr->sh_size) {
2342 printk(KERN_ERR "Module len %lu truncated\n",
2343 info->len);
2344 return -ENOEXEC;
2345 }
2346
2347 /* Mark all sections sh_addr with their address in the
2348 temporary image. */
2349 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2350
2351#ifndef CONFIG_MODULE_UNLOAD
2352 /* Don't load .exit sections */
2353 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2354 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2355#endif
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002356 }
Rusty Russelld6df72a2010-08-05 12:59:07 -06002357
2358 /* Track but don't keep modinfo and version sections. */
Rusty Russell49668682010-08-05 12:59:10 -06002359 info->index.vers = find_sec(info, "__versions");
2360 info->index.info = find_sec(info, ".modinfo");
Rusty Russelld6df72a2010-08-05 12:59:07 -06002361 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2362 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002363 return 0;
2364}
2365
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002366/*
2367 * Set up our basic convenience variables (pointers to section headers,
2368 * search for module section index etc), and do some basic section
2369 * verification.
2370 *
2371 * Return the temporary module pointer (we'll replace it with the final
2372 * one when we move the module sections around).
2373 */
2374static struct module *setup_load_info(struct load_info *info)
2375{
2376 unsigned int i;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002377 int err;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002378 struct module *mod;
2379
2380 /* Set up the convenience variables */
2381 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002382 info->secstrings = (void *)info->hdr
2383 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002384
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002385 err = rewrite_section_headers(info);
2386 if (err)
2387 return ERR_PTR(err);
2388
2389 /* Find internal symbols and strings. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002390 for (i = 1; i < info->hdr->e_shnum; i++) {
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002391 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2392 info->index.sym = i;
2393 info->index.str = info->sechdrs[i].sh_link;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002394 info->strtab = (char *)info->hdr
2395 + info->sechdrs[info->index.str].sh_offset;
2396 break;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002397 }
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002398 }
2399
Rusty Russell49668682010-08-05 12:59:10 -06002400 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002401 if (!info->index.mod) {
2402 printk(KERN_WARNING "No module found in object\n");
2403 return ERR_PTR(-ENOEXEC);
2404 }
2405 /* This is temporary: point mod into copy of data. */
2406 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2407
2408 if (info->index.sym == 0) {
2409 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2410 mod->name);
2411 return ERR_PTR(-ENOEXEC);
2412 }
2413
Rusty Russell49668682010-08-05 12:59:10 -06002414 info->index.pcpu = find_pcpusec(info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002415
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002416 /* Check module struct version now, before we try to use module. */
2417 if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2418 return ERR_PTR(-ENOEXEC);
2419
2420 return mod;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002421}
2422
Rusty Russell49668682010-08-05 12:59:10 -06002423static int check_modinfo(struct module *mod, struct load_info *info)
Rusty Russell40dd2562010-08-05 12:59:03 -06002424{
Rusty Russell49668682010-08-05 12:59:10 -06002425 const char *modmagic = get_modinfo(info, "vermagic");
Rusty Russell40dd2562010-08-05 12:59:03 -06002426 int err;
2427
2428 /* This is allowed: modprobe --force will invalidate it. */
2429 if (!modmagic) {
2430 err = try_to_force_load(mod, "bad vermagic");
2431 if (err)
2432 return err;
Rusty Russell49668682010-08-05 12:59:10 -06002433 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002434 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2435 mod->name, modmagic, vermagic);
2436 return -ENOEXEC;
2437 }
2438
Rusty Russell49668682010-08-05 12:59:10 -06002439 if (get_modinfo(info, "staging")) {
Rusty Russell40dd2562010-08-05 12:59:03 -06002440 add_taint_module(mod, TAINT_CRAP);
2441 printk(KERN_WARNING "%s: module is from the staging directory,"
2442 " the quality is unknown, you have been warned.\n",
2443 mod->name);
2444 }
Rusty Russell22e268e2010-08-05 12:59:05 -06002445
2446 /* Set up license info based on the info section */
Rusty Russell49668682010-08-05 12:59:10 -06002447 set_license(mod, get_modinfo(info, "license"));
Rusty Russell22e268e2010-08-05 12:59:05 -06002448
Rusty Russell40dd2562010-08-05 12:59:03 -06002449 return 0;
2450}
2451
Rusty Russell811d66a2010-08-05 12:59:12 -06002452static void find_module_sections(struct module *mod, struct load_info *info)
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002453{
Rusty Russell49668682010-08-05 12:59:10 -06002454 mod->kp = section_objs(info, "__param",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002455 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell49668682010-08-05 12:59:10 -06002456 mod->syms = section_objs(info, "__ksymtab",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002457 sizeof(*mod->syms), &mod->num_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002458 mod->crcs = section_addr(info, "__kcrctab");
2459 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002460 sizeof(*mod->gpl_syms),
2461 &mod->num_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002462 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
2463 mod->gpl_future_syms = section_objs(info,
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002464 "__ksymtab_gpl_future",
2465 sizeof(*mod->gpl_future_syms),
2466 &mod->num_gpl_future_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002467 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002468
2469#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell49668682010-08-05 12:59:10 -06002470 mod->unused_syms = section_objs(info, "__ksymtab_unused",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002471 sizeof(*mod->unused_syms),
2472 &mod->num_unused_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002473 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
2474 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002475 sizeof(*mod->unused_gpl_syms),
2476 &mod->num_unused_gpl_syms);
Rusty Russell49668682010-08-05 12:59:10 -06002477 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002478#endif
2479#ifdef CONFIG_CONSTRUCTORS
Rusty Russell49668682010-08-05 12:59:10 -06002480 mod->ctors = section_objs(info, ".ctors",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002481 sizeof(*mod->ctors), &mod->num_ctors);
2482#endif
2483
2484#ifdef CONFIG_TRACEPOINTS
Mathieu Desnoyers65498642011-01-26 17:26:22 -05002485 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
2486 sizeof(*mod->tracepoints_ptrs),
2487 &mod->num_tracepoints);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002488#endif
Jason Baronbf5438fc2010-09-17 11:09:00 -04002489#ifdef HAVE_JUMP_LABEL
2490 mod->jump_entries = section_objs(info, "__jump_table",
2491 sizeof(*mod->jump_entries),
2492 &mod->num_jump_entries);
2493#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002494#ifdef CONFIG_EVENT_TRACING
Rusty Russell49668682010-08-05 12:59:10 -06002495 mod->trace_events = section_objs(info, "_ftrace_events",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002496 sizeof(*mod->trace_events),
2497 &mod->num_trace_events);
2498 /*
2499 * This section contains pointers to allocated objects in the trace
2500 * code and not scanning it leads to false positives.
2501 */
2502 kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2503 mod->num_trace_events, GFP_KERNEL);
2504#endif
Steven Rostedt13b9b6e2010-11-10 22:19:24 -05002505#ifdef CONFIG_TRACING
2506 mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
2507 sizeof(*mod->trace_bprintk_fmt_start),
2508 &mod->num_trace_bprintk_fmt);
2509 /*
2510 * This section contains pointers to allocated objects in the trace
2511 * code and not scanning it leads to false positives.
2512 */
2513 kmemleak_scan_area(mod->trace_bprintk_fmt_start,
2514 sizeof(*mod->trace_bprintk_fmt_start) *
2515 mod->num_trace_bprintk_fmt, GFP_KERNEL);
2516#endif
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002517#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2518 /* sechdrs[0].sh_size is always zero */
Rusty Russell49668682010-08-05 12:59:10 -06002519 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002520 sizeof(*mod->ftrace_callsites),
2521 &mod->num_ftrace_callsites);
2522#endif
Rusty Russell22e268e2010-08-05 12:59:05 -06002523
Rusty Russell811d66a2010-08-05 12:59:12 -06002524 mod->extable = section_objs(info, "__ex_table",
2525 sizeof(*mod->extable), &mod->num_exentries);
2526
Rusty Russell49668682010-08-05 12:59:10 -06002527 if (section_addr(info, "__obsparm"))
Rusty Russell22e268e2010-08-05 12:59:05 -06002528 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2529 mod->name);
Rusty Russell811d66a2010-08-05 12:59:12 -06002530
2531 info->debug = section_objs(info, "__verbose",
2532 sizeof(*info->debug), &info->num_debug);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002533}
2534
Rusty Russell49668682010-08-05 12:59:10 -06002535static int move_module(struct module *mod, struct load_info *info)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002536{
2537 int i;
2538 void *ptr;
2539
2540 /* Do the allocs. */
2541 ptr = module_alloc_update_bounds(mod->core_size);
2542 /*
2543 * The pointer to this block is stored in the module structure
2544 * which is inside the block. Just mark it as not being a
2545 * leak.
2546 */
2547 kmemleak_not_leak(ptr);
2548 if (!ptr)
Rusty Russelld9131882010-08-05 12:59:08 -06002549 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002550
2551 memset(ptr, 0, mod->core_size);
2552 mod->module_core = ptr;
2553
2554 ptr = module_alloc_update_bounds(mod->init_size);
2555 /*
2556 * The pointer to this block is stored in the module structure
2557 * which is inside the block. This block doesn't need to be
2558 * scanned as it contains data and code that will be freed
2559 * after the module is initialized.
2560 */
2561 kmemleak_ignore(ptr);
2562 if (!ptr && mod->init_size) {
2563 module_free(mod, mod->module_core);
Rusty Russelld9131882010-08-05 12:59:08 -06002564 return -ENOMEM;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002565 }
2566 memset(ptr, 0, mod->init_size);
2567 mod->module_init = ptr;
2568
2569 /* Transfer each section which specifies SHF_ALLOC */
2570 DEBUGP("final section addresses:\n");
Rusty Russell49668682010-08-05 12:59:10 -06002571 for (i = 0; i < info->hdr->e_shnum; i++) {
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002572 void *dest;
Rusty Russell49668682010-08-05 12:59:10 -06002573 Elf_Shdr *shdr = &info->sechdrs[i];
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002574
Rusty Russell49668682010-08-05 12:59:10 -06002575 if (!(shdr->sh_flags & SHF_ALLOC))
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002576 continue;
2577
Rusty Russell49668682010-08-05 12:59:10 -06002578 if (shdr->sh_entsize & INIT_OFFSET_MASK)
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002579 dest = mod->module_init
Rusty Russell49668682010-08-05 12:59:10 -06002580 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002581 else
Rusty Russell49668682010-08-05 12:59:10 -06002582 dest = mod->module_core + shdr->sh_entsize;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002583
Rusty Russell49668682010-08-05 12:59:10 -06002584 if (shdr->sh_type != SHT_NOBITS)
2585 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002586 /* Update sh_addr to point to copy in image. */
Rusty Russell49668682010-08-05 12:59:10 -06002587 shdr->sh_addr = (unsigned long)dest;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002588 DEBUGP("\t0x%lx %s\n",
Rusty Russell49668682010-08-05 12:59:10 -06002589 shdr->sh_addr, info->secstrings + shdr->sh_name);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002590 }
Rusty Russelld9131882010-08-05 12:59:08 -06002591
2592 return 0;
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002593}
2594
Rusty Russell49668682010-08-05 12:59:10 -06002595static int check_module_license_and_versions(struct module *mod)
Rusty Russell22e268e2010-08-05 12:59:05 -06002596{
2597 /*
2598 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2599 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2600 * using GPL-only symbols it needs.
2601 */
2602 if (strcmp(mod->name, "ndiswrapper") == 0)
2603 add_taint(TAINT_PROPRIETARY_MODULE);
2604
2605 /* driverloader was caught wrongly pretending to be under GPL */
2606 if (strcmp(mod->name, "driverloader") == 0)
2607 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2608
2609#ifdef CONFIG_MODVERSIONS
2610 if ((mod->num_syms && !mod->crcs)
2611 || (mod->num_gpl_syms && !mod->gpl_crcs)
2612 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2613#ifdef CONFIG_UNUSED_SYMBOLS
2614 || (mod->num_unused_syms && !mod->unused_crcs)
2615 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2616#endif
2617 ) {
2618 return try_to_force_load(mod,
2619 "no versions for exported symbols");
2620 }
2621#endif
2622 return 0;
2623}
2624
2625static void flush_module_icache(const struct module *mod)
2626{
2627 mm_segment_t old_fs;
2628
2629 /* flush the icache in correct context */
2630 old_fs = get_fs();
2631 set_fs(KERNEL_DS);
2632
2633 /*
2634 * Flush the instruction cache, since we've played with text.
2635 * Do it before processing of module parameters, so the module
2636 * can provide parameter accessor functions of its own.
2637 */
2638 if (mod->module_init)
2639 flush_icache_range((unsigned long)mod->module_init,
2640 (unsigned long)mod->module_init
2641 + mod->init_size);
2642 flush_icache_range((unsigned long)mod->module_core,
2643 (unsigned long)mod->module_core + mod->core_size);
2644
2645 set_fs(old_fs);
2646}
2647
Rusty Russelld9131882010-08-05 12:59:08 -06002648static struct module *layout_and_allocate(struct load_info *info)
2649{
2650 /* Module within temporary copy. */
2651 struct module *mod;
Rusty Russell49668682010-08-05 12:59:10 -06002652 Elf_Shdr *pcpusec;
Rusty Russelld9131882010-08-05 12:59:08 -06002653 int err;
2654
2655 mod = setup_load_info(info);
2656 if (IS_ERR(mod))
2657 return mod;
2658
Rusty Russell49668682010-08-05 12:59:10 -06002659 err = check_modinfo(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002660 if (err)
2661 return ERR_PTR(err);
2662
2663 /* Allow arches to frob section contents and sizes. */
Rusty Russell49668682010-08-05 12:59:10 -06002664 err = module_frob_arch_sections(info->hdr, info->sechdrs,
2665 info->secstrings, mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002666 if (err < 0)
Rusty Russell6526c532010-08-05 12:59:10 -06002667 goto out;
Rusty Russelld9131882010-08-05 12:59:08 -06002668
Rusty Russell49668682010-08-05 12:59:10 -06002669 pcpusec = &info->sechdrs[info->index.pcpu];
2670 if (pcpusec->sh_size) {
Rusty Russelld9131882010-08-05 12:59:08 -06002671 /* We have a special allocation for this section. */
Rusty Russell49668682010-08-05 12:59:10 -06002672 err = percpu_modalloc(mod,
2673 pcpusec->sh_size, pcpusec->sh_addralign);
Rusty Russelld9131882010-08-05 12:59:08 -06002674 if (err)
Rusty Russell6526c532010-08-05 12:59:10 -06002675 goto out;
Rusty Russell49668682010-08-05 12:59:10 -06002676 pcpusec->sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russelld9131882010-08-05 12:59:08 -06002677 }
2678
2679 /* Determine total sizes, and put offsets in sh_entsize. For now
2680 this is done generically; there doesn't appear to be any
2681 special cases for the architectures. */
Rusty Russell49668682010-08-05 12:59:10 -06002682 layout_sections(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002683
2684 info->strmap = kzalloc(BITS_TO_LONGS(info->sechdrs[info->index.str].sh_size)
2685 * sizeof(long), GFP_KERNEL);
2686 if (!info->strmap) {
2687 err = -ENOMEM;
2688 goto free_percpu;
2689 }
Rusty Russell49668682010-08-05 12:59:10 -06002690 layout_symtab(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002691
2692 /* Allocate and move to the final place */
Rusty Russell49668682010-08-05 12:59:10 -06002693 err = move_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002694 if (err)
2695 goto free_strmap;
2696
2697 /* Module has been copied to its final place now: return it. */
2698 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
Rusty Russell49668682010-08-05 12:59:10 -06002699 kmemleak_load_module(mod, info);
Rusty Russelld9131882010-08-05 12:59:08 -06002700 return mod;
2701
2702free_strmap:
2703 kfree(info->strmap);
2704free_percpu:
2705 percpu_modfree(mod);
Rusty Russell6526c532010-08-05 12:59:10 -06002706out:
Rusty Russelld9131882010-08-05 12:59:08 -06002707 return ERR_PTR(err);
2708}
2709
2710/* mod is no longer valid after this! */
2711static void module_deallocate(struct module *mod, struct load_info *info)
2712{
2713 kfree(info->strmap);
2714 percpu_modfree(mod);
2715 module_free(mod, mod->module_init);
2716 module_free(mod, mod->module_core);
2717}
2718
Rusty Russell811d66a2010-08-05 12:59:12 -06002719static int post_relocation(struct module *mod, const struct load_info *info)
2720{
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002721 /* Sort exception table now relocations are done. */
Rusty Russell811d66a2010-08-05 12:59:12 -06002722 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2723
2724 /* Copy relocated percpu area over. */
2725 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
2726 info->sechdrs[info->index.pcpu].sh_size);
2727
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002728 /* Setup kallsyms-specific fields. */
Rusty Russell811d66a2010-08-05 12:59:12 -06002729 add_kallsyms(mod, info);
2730
2731 /* Arch-specific module finalizing. */
2732 return module_finalize(info->hdr, info->sechdrs, mod);
2733}
2734
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735/* Allocate and load the module: note that size of section 0 is always
2736 zero, and we rely on this for optional sections. */
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002737static struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 unsigned long len,
2739 const char __user *uargs)
2740{
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002741 struct load_info info = { NULL, };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 struct module *mod;
Rusty Russell40dd2562010-08-05 12:59:03 -06002743 long err;
Paul Mundt3ae91c22009-10-01 15:43:54 -07002744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
2746 umod, len, uargs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
Rusty Russelld9131882010-08-05 12:59:08 -06002748 /* Copy in the blobs from userspace, check they are vaguely sane. */
2749 err = copy_and_check(&info, umod, len, uargs);
Rusty Russell40dd2562010-08-05 12:59:03 -06002750 if (err)
2751 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
Rusty Russelld9131882010-08-05 12:59:08 -06002753 /* Figure out module layout, and allocate all the memory. */
2754 mod = layout_and_allocate(&info);
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002755 if (IS_ERR(mod)) {
2756 err = PTR_ERR(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002757 goto free_copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
Rusty Russell49668682010-08-05 12:59:10 -06002760 /* Now module is in final location, initialize linked lists, etc. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -06002761 err = module_unload_init(mod);
2762 if (err)
Rusty Russelld9131882010-08-05 12:59:08 -06002763 goto free_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
Rusty Russell22e268e2010-08-05 12:59:05 -06002765 /* Now we've got everything in the final locations, we can
2766 * find optional sections. */
Rusty Russell49668682010-08-05 12:59:10 -06002767 find_module_sections(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
Rusty Russell49668682010-08-05 12:59:10 -06002769 err = check_module_license_and_versions(mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002770 if (err)
2771 goto free_unload;
Dave Jones9841d61d2006-01-08 01:03:41 -08002772
Matt Domschc988d2b2005-06-23 22:05:15 -07002773 /* Set up MODINFO_ATTR fields */
Rusty Russell49668682010-08-05 12:59:10 -06002774 setup_modinfo(mod, &info);
Matt Domschc988d2b2005-06-23 22:05:15 -07002775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 /* Fix up syms, so that st_value is a pointer to location. */
Rusty Russell49668682010-08-05 12:59:10 -06002777 err = simplify_symbols(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06002779 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
Rusty Russell49668682010-08-05 12:59:10 -06002781 err = apply_relocations(mod, &info);
Rusty Russell22e268e2010-08-05 12:59:05 -06002782 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06002783 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
Rusty Russell811d66a2010-08-05 12:59:12 -06002785 err = post_relocation(mod, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 if (err < 0)
Rusty Russelld9131882010-08-05 12:59:08 -06002787 goto free_modinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
Rusty Russell22e268e2010-08-05 12:59:05 -06002789 flush_module_icache(mod);
Thomas Koeller378bac82005-09-06 15:17:11 -07002790
Rusty Russell6526c532010-08-05 12:59:10 -06002791 /* Now copy in args */
2792 mod->args = strndup_user(uargs, ~0UL >> 1);
2793 if (IS_ERR(mod->args)) {
2794 err = PTR_ERR(mod->args);
2795 goto free_arch_cleanup;
2796 }
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002797
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002798 /* Mark state as coming so strong_try_module_get() ignores us. */
Rusty Russelld9131882010-08-05 12:59:08 -06002799 mod->state = MODULE_STATE_COMING;
2800
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002801 /* Now sew it into the lists so we can get lockdep and oops
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002802 * info during argument parsing. No one should access us, since
Andi Kleend72b3752008-08-30 10:09:00 +02002803 * strong_try_module_get() will fail.
2804 * lockdep/oops can run asynchronous, so use the RCU list insertion
2805 * function to insert in a way safe to concurrent readers.
2806 * The mutex protects against concurrent writers.
2807 */
Rusty Russell75676502010-06-05 11:17:36 -06002808 mutex_lock(&module_mutex);
Linus Torvalds3bafeb62010-06-05 11:17:36 -06002809 if (find_module(mod->name)) {
2810 err = -EEXIST;
Rusty Russellbe593f42010-06-05 11:17:37 -06002811 goto unlock;
Linus Torvalds3bafeb62010-06-05 11:17:36 -06002812 }
2813
Rusty Russell811d66a2010-08-05 12:59:12 -06002814 /* This has to be done once we're sure module name is unique. */
2815 if (!mod->taints)
2816 dynamic_debug_setup(info.debug, info.num_debug);
Yehuda Sadehff49d742010-07-03 13:07:35 +10002817
Rusty Russellbe593f42010-06-05 11:17:37 -06002818 /* Find duplicate symbols */
2819 err = verify_export_symbols(mod);
2820 if (err < 0)
Yehuda Sadehff49d742010-07-03 13:07:35 +10002821 goto ddebug;
Rusty Russellbe593f42010-06-05 11:17:37 -06002822
Linus Torvalds53363772010-10-05 11:29:27 -07002823 module_bug_finalize(info.hdr, info.sechdrs, mod);
Andi Kleend72b3752008-08-30 10:09:00 +02002824 list_add_rcu(&mod->list, &modules);
Rusty Russell75676502010-06-05 11:17:36 -06002825 mutex_unlock(&module_mutex);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002826
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002827 /* Module is ready to execute: parsing args may do that. */
Rusty Russelle180a6b2009-03-31 13:05:29 -06002828 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002830 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002832 /* Link in to syfs. */
Rusty Russell8f6d0372010-08-05 12:59:09 -06002833 err = mod_sysfs_setup(mod, &info, mod->kp, mod->num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002835 goto unlink;
Rusty Russell80a3d1b2010-06-05 11:17:36 -06002836
Rusty Russelld9131882010-08-05 12:59:08 -06002837 /* Get rid of temporary copy and strmap. */
2838 kfree(info.strmap);
2839 free_copy(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
2841 /* Done! */
Rusty Russell51f3d0f2010-08-05 12:59:13 -06002842 trace_module_load(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 return mod;
2844
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002845 unlink:
Rusty Russell75676502010-06-05 11:17:36 -06002846 mutex_lock(&module_mutex);
Rusty Russelle91defa2009-03-31 13:05:35 -06002847 /* Unlink carefully: kallsyms could be walking list. */
2848 list_del_rcu(&mod->list);
Linus Torvalds53363772010-10-05 11:29:27 -07002849 module_bug_cleanup(mod);
2850
Yehuda Sadehff49d742010-07-03 13:07:35 +10002851 ddebug:
Rusty Russell811d66a2010-08-05 12:59:12 -06002852 if (!mod->taints)
2853 dynamic_debug_remove(info.debug);
Rusty Russellbe593f42010-06-05 11:17:37 -06002854 unlock:
Rusty Russell75676502010-06-05 11:17:36 -06002855 mutex_unlock(&module_mutex);
Rusty Russelle91defa2009-03-31 13:05:35 -06002856 synchronize_sched();
Rusty Russell6526c532010-08-05 12:59:10 -06002857 kfree(mod->args);
2858 free_arch_cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 module_arch_cleanup(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002860 free_modinfo:
Rusty Russella263f772009-09-25 00:32:58 -06002861 free_modinfo(mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002862 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 module_unload_free(mod);
Rusty Russelld9131882010-08-05 12:59:08 -06002864 free_module:
2865 module_deallocate(mod, &info);
2866 free_copy:
2867 free_copy(&info);
Jayachandran C6fe2e702006-01-06 00:19:54 -08002868 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869}
2870
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002871/* Call module constructors. */
2872static void do_mod_ctors(struct module *mod)
2873{
2874#ifdef CONFIG_CONSTRUCTORS
2875 unsigned long i;
2876
2877 for (i = 0; i < mod->num_ctors; i++)
2878 mod->ctors[i]();
2879#endif
2880}
2881
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882/* This is where the real work happens */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002883SYSCALL_DEFINE3(init_module, void __user *, umod,
2884 unsigned long, len, const char __user *, uargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885{
2886 struct module *mod;
2887 int ret = 0;
2888
2889 /* Must have permission */
Kees Cook3d433212009-04-02 15:49:29 -07002890 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 return -EPERM;
2892
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 /* Do all the hard work */
2894 mod = load_module(umod, len, uargs);
Rusty Russell75676502010-06-05 11:17:36 -06002895 if (IS_ERR(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 return PTR_ERR(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
Alan Sterne041c682006-03-27 01:16:30 -08002898 blocking_notifier_call_chain(&module_notify_list,
2899 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
Steven Rostedt94462ad2010-11-29 13:15:42 -05002901 /* Set RO and NX regions for core */
2902 set_section_ro_nx(mod->module_core,
2903 mod->core_text_size,
2904 mod->core_ro_size,
2905 mod->core_size);
2906
2907 /* Set RO and NX regions for init */
2908 set_section_ro_nx(mod->module_init,
2909 mod->init_text_size,
2910 mod->init_ro_size,
2911 mod->init_size);
2912
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002913 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 /* Start the module */
2915 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002916 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 if (ret < 0) {
2918 /* Init routine failed: abort. Try to protect us from
2919 buggy refcounters. */
2920 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002921 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002922 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002923 blocking_notifier_call_chain(&module_notify_list,
2924 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002925 free_module(mod);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002926 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 return ret;
2928 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002929 if (ret > 0) {
Joe Perchesad361c92009-07-06 13:05:40 -07002930 printk(KERN_WARNING
2931"%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
2932"%s: loading module anyway...\n",
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002933 __func__, mod->name, ret,
2934 __func__);
2935 dump_stack();
2936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
Rusty Russell6c5db222008-03-10 11:43:52 -07002938 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002940 wake_up(&module_wq);
Masami Hiramatsu0deddf432009-01-06 14:41:54 -08002941 blocking_notifier_call_chain(&module_notify_list,
2942 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07002943
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07002944 /* We need to finish all async code before the module init sequence is done */
2945 async_synchronize_full();
2946
Rusty Russell6c5db222008-03-10 11:43:52 -07002947 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 /* Drop initial reference. */
2949 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06002950 trim_init_extable(mod);
Jan Beulich4a496222009-07-06 14:50:42 +01002951#ifdef CONFIG_KALLSYMS
2952 mod->num_symtab = mod->core_num_syms;
2953 mod->symtab = mod->core_symtab;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002954 mod->strtab = mod->core_strtab;
Jan Beulich4a496222009-07-06 14:50:42 +01002955#endif
Jan Glauber01526ed2011-05-19 16:55:26 -06002956 unset_module_init_ro_nx(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 module_free(mod, mod->module_init);
2958 mod->module_init = NULL;
2959 mod->init_size = 0;
Jan Glauber4d103802011-05-19 16:55:25 -06002960 mod->init_ro_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002962 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
2964 return 0;
2965}
2966
2967static inline int within(unsigned long addr, void *start, unsigned long size)
2968{
2969 return ((void *)addr >= start && (void *)addr < start + size);
2970}
2971
2972#ifdef CONFIG_KALLSYMS
2973/*
2974 * This ignores the intensely annoying "mapping symbols" found
2975 * in ARM ELF files: $a, $t and $d.
2976 */
2977static inline int is_arm_mapping_symbol(const char *str)
2978{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002979 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 && (str[2] == '\0' || str[2] == '.');
2981}
2982
2983static const char *get_ksymbol(struct module *mod,
2984 unsigned long addr,
2985 unsigned long *size,
2986 unsigned long *offset)
2987{
2988 unsigned int i, best = 0;
2989 unsigned long nextval;
2990
2991 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002992 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002994 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2996
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002997 /* Scan for closest preceding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002998 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 for (i = 1; i < mod->num_symtab; i++) {
3000 if (mod->symtab[i].st_shndx == SHN_UNDEF)
3001 continue;
3002
3003 /* We ignore unnamed symbols: they're uninformative
3004 * and inserted at a whim. */
3005 if (mod->symtab[i].st_value <= addr
3006 && mod->symtab[i].st_value > mod->symtab[best].st_value
3007 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3008 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3009 best = i;
3010 if (mod->symtab[i].st_value > addr
3011 && mod->symtab[i].st_value < nextval
3012 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3013 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3014 nextval = mod->symtab[i].st_value;
3015 }
3016
3017 if (!best)
3018 return NULL;
3019
Alexey Dobriyanffb45122007-05-08 00:28:41 -07003020 if (size)
3021 *size = nextval - mod->symtab[best].st_value;
3022 if (offset)
3023 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 return mod->strtab + mod->symtab[best].st_name;
3025}
3026
Rusty Russell6dd06c92008-01-29 17:13:22 -05003027/* For kallsyms to ask for address resolution. NULL means not found. Careful
3028 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003029const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05003030 unsigned long *size,
3031 unsigned long *offset,
3032 char **modname,
3033 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034{
3035 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08003036 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
Rusty Russellcb2a5202008-01-14 00:55:03 -08003038 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003039 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003040 if (within_module_init(addr, mod) ||
3041 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07003042 if (modname)
3043 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08003044 ret = get_ksymbol(mod, addr, size, offset);
3045 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 }
3047 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05003048 /* Make a copy in here where it's safe */
3049 if (ret) {
3050 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
3051 ret = namebuf;
3052 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003053 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08003054 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055}
3056
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003057int lookup_module_symbol_name(unsigned long addr, char *symname)
3058{
3059 struct module *mod;
3060
Rusty Russellcb2a5202008-01-14 00:55:03 -08003061 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003062 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003063 if (within_module_init(addr, mod) ||
3064 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003065 const char *sym;
3066
3067 sym = get_ksymbol(mod, addr, NULL, NULL);
3068 if (!sym)
3069 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07003070 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003071 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003072 return 0;
3073 }
3074 }
3075out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003076 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07003077 return -ERANGE;
3078}
3079
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003080int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
3081 unsigned long *offset, char *modname, char *name)
3082{
3083 struct module *mod;
3084
Rusty Russellcb2a5202008-01-14 00:55:03 -08003085 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003086 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08003087 if (within_module_init(addr, mod) ||
3088 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003089 const char *sym;
3090
3091 sym = get_ksymbol(mod, addr, size, offset);
3092 if (!sym)
3093 goto out;
3094 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07003095 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003096 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07003097 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003098 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003099 return 0;
3100 }
3101 }
3102out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08003103 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07003104 return -ERANGE;
3105}
3106
Alexey Dobriyanea078902007-05-08 00:28:39 -07003107int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
3108 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109{
3110 struct module *mod;
3111
Rusty Russellcb2a5202008-01-14 00:55:03 -08003112 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003113 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 if (symnum < mod->num_symtab) {
3115 *value = mod->symtab[symnum].st_value;
3116 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07003117 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07003118 KSYM_NAME_LEN);
3119 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06003120 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08003121 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003122 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 }
3124 symnum -= mod->num_symtab;
3125 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003126 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07003127 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128}
3129
3130static unsigned long mod_find_symname(struct module *mod, const char *name)
3131{
3132 unsigned int i;
3133
3134 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08003135 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
3136 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 return mod->symtab[i].st_value;
3138 return 0;
3139}
3140
3141/* Look for this name: can be of form module:name. */
3142unsigned long module_kallsyms_lookup_name(const char *name)
3143{
3144 struct module *mod;
3145 char *colon;
3146 unsigned long ret = 0;
3147
3148 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08003149 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 if ((colon = strchr(name, ':')) != NULL) {
3151 *colon = '\0';
3152 if ((mod = find_module(name)) != NULL)
3153 ret = mod_find_symname(mod, colon+1);
3154 *colon = ':';
3155 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02003156 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 if ((ret = mod_find_symname(mod, name)) != 0)
3158 break;
3159 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08003160 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 return ret;
3162}
Anders Kaseorg75a66612008-12-05 19:03:58 -05003163
3164int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
3165 struct module *, unsigned long),
3166 void *data)
3167{
3168 struct module *mod;
3169 unsigned int i;
3170 int ret;
3171
3172 list_for_each_entry(mod, &modules, list) {
3173 for (i = 0; i < mod->num_symtab; i++) {
3174 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
3175 mod, mod->symtab[i].st_value);
3176 if (ret != 0)
3177 return ret;
3178 }
3179 }
3180 return 0;
3181}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182#endif /* CONFIG_KALLSYMS */
3183
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003184static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003185{
3186 int bx = 0;
3187
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003188 if (mod->taints ||
3189 mod->state == MODULE_STATE_GOING ||
3190 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003191 buf[bx++] = '(';
Andi Kleen25ddbb12008-10-15 22:01:41 -07003192 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003193 buf[bx++] = 'P';
Andi Kleen25ddbb12008-10-15 22:01:41 -07003194 if (mod->taints & (1 << TAINT_FORCED_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003195 buf[bx++] = 'F';
Linus Torvalds26e9a392008-10-17 09:50:12 -07003196 if (mod->taints & (1 << TAINT_CRAP))
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07003197 buf[bx++] = 'C';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003198 /*
3199 * TAINT_FORCED_RMMOD: could be added.
3200 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
3201 * apply to modules.
3202 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003203
3204 /* Show a - for module-is-being-unloaded */
3205 if (mod->state == MODULE_STATE_GOING)
3206 buf[bx++] = '-';
3207 /* Show a + for module-is-being-loaded */
3208 if (mod->state == MODULE_STATE_COMING)
3209 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003210 buf[bx++] = ')';
3211 }
3212 buf[bx] = '\0';
3213
3214 return buf;
3215}
3216
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003217#ifdef CONFIG_PROC_FS
3218/* Called by the /proc file system to return a list of modules. */
3219static void *m_start(struct seq_file *m, loff_t *pos)
3220{
3221 mutex_lock(&module_mutex);
3222 return seq_list_start(&modules, *pos);
3223}
3224
3225static void *m_next(struct seq_file *m, void *p, loff_t *pos)
3226{
3227 return seq_list_next(p, &modules, pos);
3228}
3229
3230static void m_stop(struct seq_file *m, void *p)
3231{
3232 mutex_unlock(&module_mutex);
3233}
3234
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235static int m_show(struct seq_file *m, void *p)
3236{
3237 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003238 char buf[8];
3239
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05003240 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 mod->name, mod->init_size + mod->core_size);
3242 print_unload_info(m, mod);
3243
3244 /* Informative for users. */
3245 seq_printf(m, " %s",
3246 mod->state == MODULE_STATE_GOING ? "Unloading":
3247 mod->state == MODULE_STATE_COMING ? "Loading":
3248 "Live");
3249 /* Used by oprofile and other similar tools. */
Kees Cook9f36e2c2011-03-22 16:34:22 -07003250 seq_printf(m, " 0x%pK", mod->module_core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003252 /* Taints info */
3253 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003254 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003255
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 seq_printf(m, "\n");
3257 return 0;
3258}
3259
3260/* Format: modulename size refcount deps address
3261
3262 Where refcount is a number or -, and deps is a comma-separated list
3263 of depends or -.
3264*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003265static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 .start = m_start,
3267 .next = m_next,
3268 .stop = m_stop,
3269 .show = m_show
3270};
3271
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003272static int modules_open(struct inode *inode, struct file *file)
3273{
3274 return seq_open(file, &modules_op);
3275}
3276
3277static const struct file_operations proc_modules_operations = {
3278 .open = modules_open,
3279 .read = seq_read,
3280 .llseek = seq_lseek,
3281 .release = seq_release,
3282};
3283
3284static int __init proc_modules_init(void)
3285{
3286 proc_create("modules", 0, NULL, &proc_modules_operations);
3287 return 0;
3288}
3289module_init(proc_modules_init);
3290#endif
3291
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292/* Given an address, look for it in the module exception tables. */
3293const struct exception_table_entry *search_module_extables(unsigned long addr)
3294{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 const struct exception_table_entry *e = NULL;
3296 struct module *mod;
3297
Rusty Russell24da1cb2007-07-15 23:41:46 -07003298 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003299 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 if (mod->num_exentries == 0)
3301 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003302
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 e = search_extable(mod->extable,
3304 mod->extable + mod->num_exentries - 1,
3305 addr);
3306 if (e)
3307 break;
3308 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07003309 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
3311 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07003312 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 return e;
3314}
3315
Ingo Molnar4d435f92006-07-03 00:24:24 -07003316/*
Rusty Russelle6104992009-03-31 13:05:31 -06003317 * is_module_address - is this address inside a module?
3318 * @addr: the address to check.
3319 *
3320 * See is_module_text_address() if you simply want to see if the address
3321 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07003322 */
Rusty Russelle6104992009-03-31 13:05:31 -06003323bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07003324{
Rusty Russelle6104992009-03-31 13:05:31 -06003325 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003326
Rusty Russell24da1cb2007-07-15 23:41:46 -07003327 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06003328 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07003329 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07003330
Rusty Russelle6104992009-03-31 13:05:31 -06003331 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003332}
3333
Rusty Russelle6104992009-03-31 13:05:31 -06003334/*
3335 * __module_address - get the module which contains an address.
3336 * @addr: the address.
3337 *
3338 * Must be called with preempt disabled or module mutex held so that
3339 * module doesn't get freed during this.
3340 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07003341struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342{
3343 struct module *mod;
3344
Rusty Russell3a642e92008-07-22 19:24:28 -05003345 if (addr < module_addr_min || addr > module_addr_max)
3346 return NULL;
3347
Andi Kleend72b3752008-08-30 10:09:00 +02003348 list_for_each_entry_rcu(mod, &modules, list)
Rusty Russelle6104992009-03-31 13:05:31 -06003349 if (within_module_core(addr, mod)
3350 || within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 return mod;
3352 return NULL;
3353}
Tim Abbottc6b37802008-12-05 19:03:59 -05003354EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
Rusty Russelle6104992009-03-31 13:05:31 -06003356/*
3357 * is_module_text_address - is this address inside module code?
3358 * @addr: the address to check.
3359 *
3360 * See is_module_address() if you simply want to see if the address is
3361 * anywhere in a module. See kernel_text_address() for testing if an
3362 * address corresponds to kernel or module code.
3363 */
3364bool is_module_text_address(unsigned long addr)
3365{
3366 bool ret;
3367
3368 preempt_disable();
3369 ret = __module_text_address(addr) != NULL;
3370 preempt_enable();
3371
3372 return ret;
3373}
3374
3375/*
3376 * __module_text_address - get the module whose code contains an address.
3377 * @addr: the address.
3378 *
3379 * Must be called with preempt disabled or module mutex held so that
3380 * module doesn't get freed during this.
3381 */
3382struct module *__module_text_address(unsigned long addr)
3383{
3384 struct module *mod = __module_address(addr);
3385 if (mod) {
3386 /* Make sure it's within the text section. */
3387 if (!within(addr, mod->module_init, mod->init_text_size)
3388 && !within(addr, mod->module_core, mod->core_text_size))
3389 mod = NULL;
3390 }
3391 return mod;
3392}
Tim Abbottc6b37802008-12-05 19:03:59 -05003393EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06003394
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395/* Don't grab lock, we're oopsing. */
3396void print_modules(void)
3397{
3398 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07003399 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400
Linus Torvaldsb2311252009-06-16 11:07:14 -07003401 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02003402 /* Most callers should already have preempt disabled, but make sure */
3403 preempt_disable();
3404 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003405 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02003406 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01003407 if (last_unloaded_module[0])
3408 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 printk("\n");
3410}
3411
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06003413/* Generate the signature for all relevant module structures here.
3414 * If these change, we don't want to try to parse the module. */
3415void module_layout(struct module *mod,
3416 struct modversion_info *ver,
3417 struct kernel_param *kp,
3418 struct kernel_symbol *ks,
Mathieu Desnoyers65498642011-01-26 17:26:22 -05003419 struct tracepoint * const *tp)
Rusty Russell8c8ef422009-03-31 13:05:34 -06003420{
3421}
3422EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07003424
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04003425#ifdef CONFIG_TRACEPOINTS
3426void module_update_tracepoints(void)
3427{
3428 struct module *mod;
3429
3430 mutex_lock(&module_mutex);
3431 list_for_each_entry(mod, &modules, list)
3432 if (!mod->taints)
Mathieu Desnoyers65498642011-01-26 17:26:22 -05003433 tracepoint_update_probe_range(mod->tracepoints_ptrs,
3434 mod->tracepoints_ptrs + mod->num_tracepoints);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04003435 mutex_unlock(&module_mutex);
3436}
3437
3438/*
3439 * Returns 0 if current not found.
3440 * Returns 1 if current found.
3441 */
3442int module_get_iter_tracepoints(struct tracepoint_iter *iter)
3443{
3444 struct module *iter_mod;
3445 int found = 0;
3446
3447 mutex_lock(&module_mutex);
3448 list_for_each_entry(iter_mod, &modules, list) {
3449 if (!iter_mod->taints) {
3450 /*
3451 * Sorted module list
3452 */
3453 if (iter_mod < iter->module)
3454 continue;
3455 else if (iter_mod > iter->module)
3456 iter->tracepoint = NULL;
3457 found = tracepoint_get_iter_range(&iter->tracepoint,
Mathieu Desnoyers65498642011-01-26 17:26:22 -05003458 iter_mod->tracepoints_ptrs,
3459 iter_mod->tracepoints_ptrs
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04003460 + iter_mod->num_tracepoints);
3461 if (found) {
3462 iter->module = iter_mod;
3463 break;
3464 }
3465 }
3466 }
3467 mutex_unlock(&module_mutex);
3468 return found;
3469}
3470#endif