blob: 60cdd0459eacb2a86fb0e9cc18d35713409f4791 [file] [log] [blame]
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 Copyright (C) 2002 Richard Henderson
3 Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
20#include <linux/moduleloader.h>
Steven Rostedt6d723732009-04-10 14:53:50 -040021#include <linux/ftrace_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
Alexey Dobriyanae84e322007-05-08 00:28:38 -070023#include <linux/kallsyms.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040024#include <linux/fs.h>
Roland McGrath6d760132007-10-16 23:26:40 -070025#include <linux/sysfs.h>
Randy Dunlap9f158332005-09-13 01:25:16 -070026#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/slab.h>
28#include <linux/vmalloc.h>
29#include <linux/elf.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040030#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/seq_file.h>
32#include <linux/syscalls.h>
33#include <linux/fcntl.h>
34#include <linux/rcupdate.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080035#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/cpu.h>
37#include <linux/moduleparam.h>
38#include <linux/errno.h>
39#include <linux/err.h>
40#include <linux/vermagic.h>
41#include <linux/notifier.h>
Al Virof6a57032006-10-18 01:47:25 -040042#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/stop_machine.h>
44#include <linux/device.h>
Matt Domschc988d2b2005-06-23 22:05:15 -070045#include <linux/string.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080046#include <linux/mutex.h>
Andi Kleend72b3752008-08-30 10:09:00 +020047#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/cacheflush.h>
Bernd Schmidteb8cdec2009-09-21 17:03:57 -070050#include <asm/mmu_context.h>
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020051#include <linux/license.h>
Christoph Lameter6d762392008-02-08 04:18:42 -080052#include <asm/sections.h>
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040053#include <linux/tracepoint.h>
Steven Rostedt90d595f2008-08-14 15:45:09 -040054#include <linux/ftrace.h>
Arjan van de Ven22a9d642009-01-07 08:45:46 -080055#include <linux/async.h>
Tejun Heofbf59bc2009-02-20 16:29:08 +090056#include <linux/percpu.h>
Catalin Marinas4f2294b2009-06-11 13:23:20 +010057#include <linux/kmemleak.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Li Zefan7ead8b82009-08-17 16:56:28 +080059#define CREATE_TRACE_POINTS
60#include <trace/events/module.h>
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#if 0
63#define DEBUGP printk
64#else
65#define DEBUGP(fmt , a...)
66#endif
67
68#ifndef ARCH_SHF_SMALL
69#define ARCH_SHF_SMALL 0
70#endif
71
72/* If this is set, the section belongs in the init part of the module */
73#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
74
Rusty Russell75676502010-06-05 11:17:36 -060075/*
76 * Mutex protects:
77 * 1) List of modules (also safely readable with preempt_disable),
78 * 2) module_use links,
79 * 3) module_addr_min/module_addr_max.
Andi Kleend72b3752008-08-30 10:09:00 +020080 * (delete uses stop_machine/add uses RCU list operations). */
Tim Abbottc6b37802008-12-05 19:03:59 -050081DEFINE_MUTEX(module_mutex);
82EXPORT_SYMBOL_GPL(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static LIST_HEAD(modules);
Jason Wessel67fc4e02010-05-20 21:04:21 -050084#ifdef CONFIG_KGDB_KDB
85struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
86#endif /* CONFIG_KGDB_KDB */
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Stephen Rothwell19e45292009-04-14 17:27:18 +100089/* Block module loading/unloading? */
90int modules_disabled = 0;
91
Rusty Russellc9a3ba52008-01-29 17:13:18 -050092/* Waiting for a module to finish initializing? */
93static DECLARE_WAIT_QUEUE_HEAD(module_wq);
94
Alan Sterne041c682006-03-27 01:16:30 -080095static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Rusty Russell75676502010-06-05 11:17:36 -060097/* Bounds of module allocation, for speeding __module_address.
98 * Protected by module_mutex. */
Rusty Russell3a642e92008-07-22 19:24:28 -050099static unsigned long module_addr_min = -1UL, module_addr_max = 0;
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101int register_module_notifier(struct notifier_block * nb)
102{
Alan Sterne041c682006-03-27 01:16:30 -0800103 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105EXPORT_SYMBOL(register_module_notifier);
106
107int unregister_module_notifier(struct notifier_block * nb)
108{
Alan Sterne041c682006-03-27 01:16:30 -0800109 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111EXPORT_SYMBOL(unregister_module_notifier);
112
Rusty Russelleded41c2010-08-05 12:59:07 -0600113struct load_info {
114 Elf_Ehdr *hdr;
115 unsigned long len;
116 Elf_Shdr *sechdrs;
117 char *secstrings, *args, *strtab;
118 struct {
119 unsigned int sym, str, mod, vers, info, pcpu;
120 } index;
121};
122
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -0800123/* We require a truly strong try_module_get(): 0 means failure due to
124 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static inline int strong_try_module_get(struct module *mod)
126{
127 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500128 return -EBUSY;
129 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500131 else
132 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700135static inline void add_taint_module(struct module *mod, unsigned flag)
136{
137 add_taint(flag);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700138 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700139}
140
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200141/*
142 * A thread that wants to hold a reference to a module only while it
143 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 */
145void __module_put_and_exit(struct module *mod, long code)
146{
147 module_put(mod);
148 do_exit(code);
149}
150EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* Find a module section: 0 means not found. */
153static unsigned int find_sec(Elf_Ehdr *hdr,
154 Elf_Shdr *sechdrs,
155 const char *secstrings,
156 const char *name)
157{
158 unsigned int i;
159
160 for (i = 1; i < hdr->e_shnum; i++)
161 /* Alloc bit cleared means "ignore it." */
162 if ((sechdrs[i].sh_flags & SHF_ALLOC)
163 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
164 return i;
165 return 0;
166}
167
Rusty Russell5e458cc2008-10-22 10:00:13 -0500168/* Find a module section, or NULL. */
169static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
170 const char *secstrings, const char *name)
171{
172 /* Section 0 has sh_addr 0. */
173 return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
174}
175
176/* Find a module section, or NULL. Fill in number of "objects" in section. */
177static void *section_objs(Elf_Ehdr *hdr,
178 Elf_Shdr *sechdrs,
179 const char *secstrings,
180 const char *name,
181 size_t object_size,
182 unsigned int *num)
183{
184 unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
185
186 /* Section 0 has sh_addr 0 and sh_size 0. */
187 *num = sechdrs[sec].sh_size / object_size;
188 return (void *)sechdrs[sec].sh_addr;
189}
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/* Provided by the linker */
192extern const struct kernel_symbol __start___ksymtab[];
193extern const struct kernel_symbol __stop___ksymtab[];
194extern const struct kernel_symbol __start___ksymtab_gpl[];
195extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800196extern const struct kernel_symbol __start___ksymtab_gpl_future[];
197extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198extern const unsigned long __start___kcrctab[];
199extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800200extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500201#ifdef CONFIG_UNUSED_SYMBOLS
202extern const struct kernel_symbol __start___ksymtab_unused[];
203extern const struct kernel_symbol __stop___ksymtab_unused[];
204extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
205extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700206extern const unsigned long __start___kcrctab_unused[];
207extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500208#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210#ifndef CONFIG_MODVERSIONS
211#define symversion(base, idx) NULL
212#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800213#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214#endif
215
Rusty Russelldafd0942008-07-22 19:24:25 -0500216static bool each_symbol_in_section(const struct symsearch *arr,
217 unsigned int arrsize,
218 struct module *owner,
219 bool (*fn)(const struct symsearch *syms,
220 struct module *owner,
221 unsigned int symnum, void *data),
222 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100223{
Rusty Russelldafd0942008-07-22 19:24:25 -0500224 unsigned int i, j;
225
226 for (j = 0; j < arrsize; j++) {
227 for (i = 0; i < arr[j].stop - arr[j].start; i++)
228 if (fn(&arr[j], owner, i, data))
229 return true;
230 }
231
232 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100233}
234
Rusty Russelldafd0942008-07-22 19:24:25 -0500235/* Returns true as soon as fn returns true, otherwise false. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500236bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
237 unsigned int symnum, void *data), void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700238{
Rusty Russelldafd0942008-07-22 19:24:25 -0500239 struct module *mod;
Linus Torvalds44032e62010-08-05 12:59:05 -0600240 static const struct symsearch arr[] = {
Rusty Russelldafd0942008-07-22 19:24:25 -0500241 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
242 NOT_GPL_ONLY, false },
243 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
244 __start___kcrctab_gpl,
245 GPL_ONLY, false },
246 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
247 __start___kcrctab_gpl_future,
248 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500249#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500250 { __start___ksymtab_unused, __stop___ksymtab_unused,
251 __start___kcrctab_unused,
252 NOT_GPL_ONLY, true },
253 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
254 __start___kcrctab_unused_gpl,
255 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500256#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500257 };
258
259 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
260 return true;
261
Andi Kleend72b3752008-08-30 10:09:00 +0200262 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500263 struct symsearch arr[] = {
264 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
265 NOT_GPL_ONLY, false },
266 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
267 mod->gpl_crcs,
268 GPL_ONLY, false },
269 { mod->gpl_future_syms,
270 mod->gpl_future_syms + mod->num_gpl_future_syms,
271 mod->gpl_future_crcs,
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 { mod->unused_syms,
275 mod->unused_syms + mod->num_unused_syms,
276 mod->unused_crcs,
277 NOT_GPL_ONLY, true },
278 { mod->unused_gpl_syms,
279 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
280 mod->unused_gpl_crcs,
281 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500282#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500283 };
284
285 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
286 return true;
287 }
288 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700289}
Tim Abbottc6b37802008-12-05 19:03:59 -0500290EXPORT_SYMBOL_GPL(each_symbol);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700291
Rusty Russelldafd0942008-07-22 19:24:25 -0500292struct find_symbol_arg {
293 /* Input */
294 const char *name;
295 bool gplok;
296 bool warn;
297
298 /* Output */
299 struct module *owner;
300 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500301 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500302};
303
304static bool find_symbol_in_section(const struct symsearch *syms,
305 struct module *owner,
306 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500307{
Rusty Russelldafd0942008-07-22 19:24:25 -0500308 struct find_symbol_arg *fsa = data;
309
310 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
311 return false;
312
313 if (!fsa->gplok) {
314 if (syms->licence == GPL_ONLY)
315 return false;
316 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
317 printk(KERN_WARNING "Symbol %s is being used "
318 "by a non-GPL module, which will not "
319 "be allowed in the future\n", fsa->name);
320 printk(KERN_WARNING "Please see the file "
321 "Documentation/feature-removal-schedule.txt "
322 "in the kernel source tree for more details.\n");
323 }
324 }
325
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500326#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500327 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500328 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500329 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500330 printk(KERN_WARNING
331 "This symbol will go away in the future.\n");
332 printk(KERN_WARNING
333 "Please evalute if this is the right api to use and if "
334 "it really is, submit a report the linux kernel "
335 "mailinglist together with submitting your code for "
336 "inclusion.\n");
337 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500338#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500339
340 fsa->owner = owner;
341 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500342 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500343 return true;
344}
345
Tim Abbott414fd312008-12-05 19:03:56 -0500346/* Find a symbol and return it, along with, (optional) crc and
Rusty Russell75676502010-06-05 11:17:36 -0600347 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500348const struct kernel_symbol *find_symbol(const char *name,
349 struct module **owner,
350 const unsigned long **crc,
351 bool gplok,
352 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Rusty Russelldafd0942008-07-22 19:24:25 -0500354 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Rusty Russelldafd0942008-07-22 19:24:25 -0500356 fsa.name = name;
357 fsa.gplok = gplok;
358 fsa.warn = warn;
359
360 if (each_symbol(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500361 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500362 *owner = fsa.owner;
363 if (crc)
364 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500365 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 DEBUGP("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500369 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
Tim Abbottc6b37802008-12-05 19:03:59 -0500371EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373/* Search for module by name: must hold module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500374struct module *find_module(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 struct module *mod;
377
378 list_for_each_entry(mod, &modules, list) {
379 if (strcmp(mod->name, name) == 0)
380 return mod;
381 }
382 return NULL;
383}
Tim Abbottc6b37802008-12-05 19:03:59 -0500384EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900387
Tejun Heo259354d2010-03-10 18:56:10 +0900388static inline void __percpu *mod_percpu(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900389{
Tejun Heo259354d2010-03-10 18:56:10 +0900390 return mod->percpu;
391}
Tejun Heofbf59bc2009-02-20 16:29:08 +0900392
Tejun Heo259354d2010-03-10 18:56:10 +0900393static int percpu_modalloc(struct module *mod,
394 unsigned long size, unsigned long align)
395{
Tejun Heofbf59bc2009-02-20 16:29:08 +0900396 if (align > PAGE_SIZE) {
397 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
Tejun Heo259354d2010-03-10 18:56:10 +0900398 mod->name, align, PAGE_SIZE);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900399 align = PAGE_SIZE;
400 }
401
Tejun Heo259354d2010-03-10 18:56:10 +0900402 mod->percpu = __alloc_reserved_percpu(size, align);
403 if (!mod->percpu) {
Tejun Heofbf59bc2009-02-20 16:29:08 +0900404 printk(KERN_WARNING
405 "Could not allocate %lu bytes percpu data\n", size);
Tejun Heo259354d2010-03-10 18:56:10 +0900406 return -ENOMEM;
407 }
408 mod->percpu_size = size;
409 return 0;
Tejun Heofbf59bc2009-02-20 16:29:08 +0900410}
411
Tejun Heo259354d2010-03-10 18:56:10 +0900412static void percpu_modfree(struct module *mod)
Tejun Heofbf59bc2009-02-20 16:29:08 +0900413{
Tejun Heo259354d2010-03-10 18:56:10 +0900414 free_percpu(mod->percpu);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900415}
416
Tejun Heo6b588c12009-02-20 16:29:07 +0900417static unsigned int find_pcpusec(Elf_Ehdr *hdr,
418 Elf_Shdr *sechdrs,
419 const char *secstrings)
420{
Denys Vlasenko3d9a8542010-02-20 01:03:43 +0100421 return find_sec(hdr, sechdrs, secstrings, ".data..percpu");
Tejun Heo6b588c12009-02-20 16:29:07 +0900422}
423
Tejun Heo259354d2010-03-10 18:56:10 +0900424static void percpu_modcopy(struct module *mod,
425 const void *from, unsigned long size)
Tejun Heo6b588c12009-02-20 16:29:07 +0900426{
427 int cpu;
428
429 for_each_possible_cpu(cpu)
Tejun Heo259354d2010-03-10 18:56:10 +0900430 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
Tejun Heo6b588c12009-02-20 16:29:07 +0900431}
432
Tejun Heo10fad5e2010-03-10 18:57:54 +0900433/**
434 * is_module_percpu_address - test whether address is from module static percpu
435 * @addr: address to test
436 *
437 * Test whether @addr belongs to module static percpu area.
438 *
439 * RETURNS:
440 * %true if @addr is from module static percpu area
441 */
442bool is_module_percpu_address(unsigned long addr)
443{
444 struct module *mod;
445 unsigned int cpu;
446
447 preempt_disable();
448
449 list_for_each_entry_rcu(mod, &modules, list) {
450 if (!mod->percpu_size)
451 continue;
452 for_each_possible_cpu(cpu) {
453 void *start = per_cpu_ptr(mod->percpu, cpu);
454
455 if ((void *)addr >= start &&
456 (void *)addr < start + mod->percpu_size) {
457 preempt_enable();
458 return true;
459 }
460 }
461 }
462
463 preempt_enable();
464 return false;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700465}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900468
Tejun Heo259354d2010-03-10 18:56:10 +0900469static inline void __percpu *mod_percpu(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 return NULL;
472}
Tejun Heo259354d2010-03-10 18:56:10 +0900473static inline int percpu_modalloc(struct module *mod,
474 unsigned long size, unsigned long align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Tejun Heo259354d2010-03-10 18:56:10 +0900476 return -ENOMEM;
477}
478static inline void percpu_modfree(struct module *mod)
479{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
482 Elf_Shdr *sechdrs,
483 const char *secstrings)
484{
485 return 0;
486}
Tejun Heo259354d2010-03-10 18:56:10 +0900487static inline void percpu_modcopy(struct module *mod,
488 const void *from, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 /* pcpusec should be 0, and size of that section should be 0. */
491 BUG_ON(size != 0);
492}
Tejun Heo10fad5e2010-03-10 18:57:54 +0900493bool is_module_percpu_address(unsigned long addr)
494{
495 return false;
496}
Tejun Heo6b588c12009-02-20 16:29:07 +0900497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498#endif /* CONFIG_SMP */
499
Matt Domschc988d2b2005-06-23 22:05:15 -0700500#define MODINFO_ATTR(field) \
501static void setup_modinfo_##field(struct module *mod, const char *s) \
502{ \
503 mod->field = kstrdup(s, GFP_KERNEL); \
504} \
505static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
506 struct module *mod, char *buffer) \
507{ \
508 return sprintf(buffer, "%s\n", mod->field); \
509} \
510static int modinfo_##field##_exists(struct module *mod) \
511{ \
512 return mod->field != NULL; \
513} \
514static void free_modinfo_##field(struct module *mod) \
515{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700516 kfree(mod->field); \
517 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700518} \
519static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900520 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700521 .show = show_modinfo_##field, \
522 .setup = setup_modinfo_##field, \
523 .test = modinfo_##field##_exists, \
524 .free = free_modinfo_##field, \
525};
526
527MODINFO_ATTR(version);
528MODINFO_ATTR(srcversion);
529
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100530static char last_unloaded_module[MODULE_NAME_LEN+1];
531
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800532#ifdef CONFIG_MODULE_UNLOAD
Steven Rostedteb0c5372010-03-29 14:25:18 -0400533
534EXPORT_TRACEPOINT_SYMBOL(module_get);
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536/* Init the unload section of the module. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600537static int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600539 mod->refptr = alloc_percpu(struct module_ref);
540 if (!mod->refptr)
541 return -ENOMEM;
542
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700543 INIT_LIST_HEAD(&mod->source_list);
544 INIT_LIST_HEAD(&mod->target_list);
Christoph Lametere1783a22010-01-05 15:34:50 +0900545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /* Hold reference count during initialization. */
Nick Piggin5fbfb182010-04-01 19:09:40 +1100547 __this_cpu_write(mod->refptr->incs, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* Backwards compatibility macros put refcount during init. */
549 mod->waiter = current;
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600550
551 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554/* Does a already use b? */
555static int already_uses(struct module *a, struct module *b)
556{
557 struct module_use *use;
558
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700559 list_for_each_entry(use, &b->source_list, source_list) {
560 if (use->source == a) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 DEBUGP("%s uses %s!\n", a->name, b->name);
562 return 1;
563 }
564 }
565 DEBUGP("%s does not use %s!\n", a->name, b->name);
566 return 0;
567}
568
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700569/*
570 * Module a uses b
571 * - we add 'a' as a "source", 'b' as a "target" of module use
572 * - the module_use is added to the list of 'b' sources (so
573 * 'b' can walk the list to see who sourced them), and of 'a'
574 * targets (so 'a' can see what modules it targets).
575 */
576static int add_module_usage(struct module *a, struct module *b)
577{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700578 struct module_use *use;
579
580 DEBUGP("Allocating new usage for %s.\n", a->name);
581 use = kmalloc(sizeof(*use), GFP_ATOMIC);
582 if (!use) {
583 printk(KERN_WARNING "%s: out of memory loading\n", a->name);
584 return -ENOMEM;
585 }
586
587 use->source = a;
588 use->target = b;
589 list_add(&use->source_list, &b->source_list);
590 list_add(&use->target_list, &a->target_list);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700591 return 0;
592}
593
Rusty Russell75676502010-06-05 11:17:36 -0600594/* Module a uses b: caller needs module_mutex() */
Rusty Russell9bea7f22010-06-05 11:17:37 -0600595int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Rusty Russellc8e21ce2010-06-05 11:17:35 -0600597 int err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100598
Rusty Russell9bea7f22010-06-05 11:17:37 -0600599 if (b == NULL || already_uses(a, b))
Linus Torvalds218ce732010-05-25 16:48:30 -0700600 return 0;
Linus Torvalds218ce732010-05-25 16:48:30 -0700601
Rusty Russell9bea7f22010-06-05 11:17:37 -0600602 /* If module isn't available, we fail. */
603 err = strong_try_module_get(b);
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500604 if (err)
Rusty Russell9bea7f22010-06-05 11:17:37 -0600605 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700607 err = add_module_usage(a, b);
608 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 module_put(b);
Rusty Russell9bea7f22010-06-05 11:17:37 -0600610 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
Rusty Russell9bea7f22010-06-05 11:17:37 -0600612 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600614EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616/* Clear the unload stuff of the module. */
617static void module_unload_free(struct module *mod)
618{
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700619 struct module_use *use, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Rusty Russell75676502010-06-05 11:17:36 -0600621 mutex_lock(&module_mutex);
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700622 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
623 struct module *i = use->target;
624 DEBUGP("%s unusing %s\n", mod->name, i->name);
625 module_put(i);
626 list_del(&use->source_list);
627 list_del(&use->target_list);
628 kfree(use);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
Rusty Russell75676502010-06-05 11:17:36 -0600630 mutex_unlock(&module_mutex);
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600631
632 free_percpu(mod->refptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
635#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800636static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 int ret = (flags & O_TRUNC);
639 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800640 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return ret;
642}
643#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800644static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
646 return 0;
647}
648#endif /* CONFIG_MODULE_FORCE_UNLOAD */
649
650struct stopref
651{
652 struct module *mod;
653 int flags;
654 int *forced;
655};
656
657/* Whole machine is stopped with interrupts off when this runs. */
658static int __try_stop_module(void *_sref)
659{
660 struct stopref *sref = _sref;
661
Rusty Russellda39ba52008-07-22 19:24:25 -0500662 /* If it's not unused, quit unless we're forcing. */
663 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800664 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return -EWOULDBLOCK;
666 }
667
668 /* Mark it as dying. */
669 sref->mod->state = MODULE_STATE_GOING;
670 return 0;
671}
672
673static int try_stop_module(struct module *mod, int flags, int *forced)
674{
Rusty Russellda39ba52008-07-22 19:24:25 -0500675 if (flags & O_NONBLOCK) {
676 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500678 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500679 } else {
680 /* We don't need to stop the machine for this. */
681 mod->state = MODULE_STATE_GOING;
682 synchronize_sched();
683 return 0;
684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
687unsigned int module_refcount(struct module *mod)
688{
Nick Piggin5fbfb182010-04-01 19:09:40 +1100689 unsigned int incs = 0, decs = 0;
Eric Dumazet720eba32009-02-03 13:31:36 +1030690 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Eric Dumazet720eba32009-02-03 13:31:36 +1030692 for_each_possible_cpu(cpu)
Nick Piggin5fbfb182010-04-01 19:09:40 +1100693 decs += per_cpu_ptr(mod->refptr, cpu)->decs;
694 /*
695 * ensure the incs are added up after the decs.
696 * module_put ensures incs are visible before decs with smp_wmb.
697 *
698 * This 2-count scheme avoids the situation where the refcount
699 * for CPU0 is read, then CPU0 increments the module refcount,
700 * then CPU1 drops that refcount, then the refcount for CPU1 is
701 * read. We would record a decrement but not its corresponding
702 * increment so we would see a low count (disaster).
703 *
704 * Rare situation? But module_refcount can be preempted, and we
705 * might be tallying up 4096+ CPUs. So it is not impossible.
706 */
707 smp_rmb();
708 for_each_possible_cpu(cpu)
709 incs += per_cpu_ptr(mod->refptr, cpu)->incs;
710 return incs - decs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712EXPORT_SYMBOL(module_refcount);
713
714/* This exists whether we can unload or not */
715static void free_module(struct module *mod);
716
717static void wait_for_zero_refcount(struct module *mod)
718{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500719 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800720 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 for (;;) {
722 DEBUGP("Looking at refcount...\n");
723 set_current_state(TASK_UNINTERRUPTIBLE);
724 if (module_refcount(mod) == 0)
725 break;
726 schedule();
727 }
728 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800729 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100732SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
733 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
735 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800736 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 int ret, forced = 0;
738
Kees Cook3d433212009-04-02 15:49:29 -0700739 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800740 return -EPERM;
741
742 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
743 return -EFAULT;
744 name[MODULE_NAME_LEN-1] = '\0';
745
Tejun Heo3fc1f1e2010-05-06 18:49:20 +0200746 if (mutex_lock_interruptible(&module_mutex) != 0)
747 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 mod = find_module(name);
750 if (!mod) {
751 ret = -ENOENT;
752 goto out;
753 }
754
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700755 if (!list_empty(&mod->source_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 /* Other modules depend on us: get rid of them first. */
757 ret = -EWOULDBLOCK;
758 goto out;
759 }
760
761 /* Doing init or already dying? */
762 if (mod->state != MODULE_STATE_LIVE) {
763 /* FIXME: if (force), slam module count and wake up
764 waiter --RR */
765 DEBUGP("%s already dying\n", mod->name);
766 ret = -EBUSY;
767 goto out;
768 }
769
770 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700771 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800772 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (!forced) {
774 /* This module can't be removed */
775 ret = -EBUSY;
776 goto out;
777 }
778 }
779
780 /* Set this up before setting mod->state */
781 mod->waiter = current;
782
783 /* Stop the machine so refcounts can't move and disable module. */
784 ret = try_stop_module(mod, flags, &forced);
785 if (ret != 0)
786 goto out;
787
788 /* Never wait if forced. */
789 if (!forced && module_refcount(mod) != 0)
790 wait_for_zero_refcount(mod);
791
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200792 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 /* Final destruction now noone is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200794 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200796 blocking_notifier_call_chain(&module_notify_list,
797 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800798 async_synchronize_full();
Rusty Russell75676502010-06-05 11:17:36 -0600799
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100800 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500801 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Rusty Russell75676502010-06-05 11:17:36 -0600803 free_module(mod);
804 return 0;
805out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800806 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return ret;
808}
809
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800810static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812 struct module_use *use;
813 int printed_something = 0;
814
815 seq_printf(m, " %u ", module_refcount(mod));
816
817 /* Always include a trailing , so userspace can differentiate
818 between this and the old multi-field proc format. */
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700819 list_for_each_entry(use, &mod->source_list, source_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 printed_something = 1;
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700821 seq_printf(m, "%s,", use->source->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (mod->init != NULL && mod->exit == NULL) {
825 printed_something = 1;
826 seq_printf(m, "[permanent],");
827 }
828
829 if (!printed_something)
830 seq_printf(m, "-");
831}
832
833void __symbol_put(const char *symbol)
834{
835 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Rusty Russell24da1cb2007-07-15 23:41:46 -0700837 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -0500838 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 BUG();
840 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700841 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843EXPORT_SYMBOL(__symbol_put);
844
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930845/* Note this assumes addr is a function, which it currently always is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846void symbol_put_addr(void *addr)
847{
Trent Piepho5e376612006-05-15 09:44:06 -0700848 struct module *modaddr;
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930849 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930851 if (core_kernel_text(a))
Trent Piepho5e376612006-05-15 09:44:06 -0700852 return;
853
Rusty Russella6e6abd2009-03-31 13:05:31 -0600854 /* module_text_address is safe here: we're supposed to have reference
855 * to module from symbol_get, so it can't go away. */
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930856 modaddr = __module_text_address(a);
Rusty Russella6e6abd2009-03-31 13:05:31 -0600857 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -0700858 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860EXPORT_SYMBOL_GPL(symbol_put_addr);
861
862static ssize_t show_refcnt(struct module_attribute *mattr,
863 struct module *mod, char *buffer)
864{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400865 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
868static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900869 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 .show = show_refcnt,
871};
872
Al Virof6a57032006-10-18 01:47:25 -0400873void module_put(struct module *module)
874{
875 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +0900876 preempt_disable();
Nick Piggin5fbfb182010-04-01 19:09:40 +1100877 smp_wmb(); /* see comment in module_refcount */
878 __this_cpu_inc(module->refptr->decs);
Christoph Lametere1783a22010-01-05 15:34:50 +0900879
Li Zefanae832d12010-03-24 10:57:43 +0800880 trace_module_put(module, _RET_IP_);
Al Virof6a57032006-10-18 01:47:25 -0400881 /* Maybe they're waiting for us to drop reference? */
882 if (unlikely(!module_is_live(module)))
883 wake_up_process(module->waiter);
Christoph Lametere1783a22010-01-05 15:34:50 +0900884 preempt_enable();
Al Virof6a57032006-10-18 01:47:25 -0400885 }
886}
887EXPORT_SYMBOL(module_put);
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800890static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
892 /* We don't know the usage count, or what modules are using. */
893 seq_printf(m, " - -");
894}
895
896static inline void module_unload_free(struct module *mod)
897{
898}
899
Rusty Russell9bea7f22010-06-05 11:17:37 -0600900int ref_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Rusty Russell9bea7f22010-06-05 11:17:37 -0600902 return strong_try_module_get(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
Rusty Russell9bea7f22010-06-05 11:17:37 -0600904EXPORT_SYMBOL_GPL(ref_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600906static inline int module_unload_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Rusty Russell9f85a4b2010-08-05 12:59:04 -0600908 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910#endif /* CONFIG_MODULE_UNLOAD */
911
Kay Sievers1f717402006-11-24 12:15:25 +0100912static ssize_t show_initstate(struct module_attribute *mattr,
913 struct module *mod, char *buffer)
914{
915 const char *state = "unknown";
916
917 switch (mod->state) {
918 case MODULE_STATE_LIVE:
919 state = "live";
920 break;
921 case MODULE_STATE_COMING:
922 state = "coming";
923 break;
924 case MODULE_STATE_GOING:
925 state = "going";
926 break;
927 }
928 return sprintf(buffer, "%s\n", state);
929}
930
931static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900932 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100933 .show = show_initstate,
934};
935
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800936static struct module_attribute *modinfo_attrs[] = {
937 &modinfo_version,
938 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100939 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800940#ifdef CONFIG_MODULE_UNLOAD
941 &refcnt,
942#endif
943 NULL,
944};
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946static const char vermagic[] = VERMAGIC_STRING;
947
Rusty Russellc6e665c2009-03-31 13:05:33 -0600948static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -0700949{
950#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -0700951 if (!test_taint(TAINT_FORCED_MODULE))
Rusty Russellc6e665c2009-03-31 13:05:33 -0600952 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
953 mod->name, reason);
Linus Torvalds826e4502008-05-04 17:04:16 -0700954 add_taint_module(mod, TAINT_FORCED_MODULE);
955 return 0;
956#else
957 return -ENOEXEC;
958#endif
959}
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961#ifdef CONFIG_MODVERSIONS
Rusty Russelld4703ae2009-12-15 16:28:32 -0600962/* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
963static unsigned long maybe_relocated(unsigned long crc,
964 const struct module *crc_owner)
965{
966#ifdef ARCH_RELOCATES_KCRCTAB
967 if (crc_owner == NULL)
968 return crc - (unsigned long)reloc_start;
969#endif
970 return crc;
971}
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973static int check_version(Elf_Shdr *sechdrs,
974 unsigned int versindex,
975 const char *symname,
976 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -0600977 const unsigned long *crc,
978 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 unsigned int i, num_versions;
981 struct modversion_info *versions;
982
983 /* Exporting module didn't supply crcs? OK, we're already tainted. */
984 if (!crc)
985 return 1;
986
Rusty Russella5dd6972008-05-09 16:24:21 +1000987 /* No versions at all? modprobe --force does this. */
988 if (versindex == 0)
989 return try_to_force_load(mod, symname) == 0;
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 versions = (void *) sechdrs[versindex].sh_addr;
992 num_versions = sechdrs[versindex].sh_size
993 / sizeof(struct modversion_info);
994
995 for (i = 0; i < num_versions; i++) {
996 if (strcmp(versions[i].name, symname) != 0)
997 continue;
998
Rusty Russelld4703ae2009-12-15 16:28:32 -0600999 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 DEBUGP("Found checksum %lX vs module %lX\n",
Rusty Russelld4703ae2009-12-15 16:28:32 -06001002 maybe_relocated(*crc, crc_owner), versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001003 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001005
Rusty Russella5dd6972008-05-09 16:24:21 +10001006 printk(KERN_WARNING "%s: no symbol version for %s\n",
1007 mod->name, symname);
1008 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001009
1010bad_version:
1011 printk("%s: disagrees about version of symbol %s\n",
1012 mod->name, symname);
1013 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
1016static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1017 unsigned int versindex,
1018 struct module *mod)
1019{
1020 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Rusty Russell75676502010-06-05 11:17:36 -06001022 /* Since this should be found in kernel (which can't be removed),
1023 * no locking is necessary. */
Mike Frysinger6560dc12009-07-23 23:42:08 +09301024 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1025 &crc, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 BUG();
Rusty Russelld4703ae2009-12-15 16:28:32 -06001027 return check_version(sechdrs, versindex, "module_layout", mod, crc,
1028 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
Rusty Russell91e37a72008-05-09 16:25:28 +10001031/* First part is kernel version, which we ignore if module has crcs. */
1032static inline int same_magic(const char *amagic, const char *bmagic,
1033 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Rusty Russell91e37a72008-05-09 16:25:28 +10001035 if (has_crcs) {
1036 amagic += strcspn(amagic, " ");
1037 bmagic += strcspn(bmagic, " ");
1038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return strcmp(amagic, bmagic) == 0;
1040}
1041#else
1042static inline int check_version(Elf_Shdr *sechdrs,
1043 unsigned int versindex,
1044 const char *symname,
1045 struct module *mod,
Rusty Russelld4703ae2009-12-15 16:28:32 -06001046 const unsigned long *crc,
1047 const struct module *crc_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
1049 return 1;
1050}
1051
1052static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1053 unsigned int versindex,
1054 struct module *mod)
1055{
1056 return 1;
1057}
1058
Rusty Russell91e37a72008-05-09 16:25:28 +10001059static inline int same_magic(const char *amagic, const char *bmagic,
1060 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
1062 return strcmp(amagic, bmagic) == 0;
1063}
1064#endif /* CONFIG_MODVERSIONS */
1065
Rusty Russell75676502010-06-05 11:17:36 -06001066/* Resolve a symbol for this module. I.e. if we find one, record usage. */
Tim Abbott414fd312008-12-05 19:03:56 -05001067static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
1068 unsigned int versindex,
1069 const char *name,
Rusty Russell9bea7f22010-06-05 11:17:37 -06001070 struct module *mod,
1071 char ownername[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
1073 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001074 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 const unsigned long *crc;
Rusty Russell9bea7f22010-06-05 11:17:37 -06001076 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Rusty Russell75676502010-06-05 11:17:36 -06001078 mutex_lock(&module_mutex);
Tim Abbott414fd312008-12-05 19:03:56 -05001079 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001080 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Rusty Russell9bea7f22010-06-05 11:17:37 -06001081 if (!sym)
1082 goto unlock;
1083
1084 if (!check_version(sechdrs, versindex, name, mod, crc, owner)) {
1085 sym = ERR_PTR(-EINVAL);
1086 goto getname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
Rusty Russell9bea7f22010-06-05 11:17:37 -06001088
1089 err = ref_module(mod, owner);
1090 if (err) {
1091 sym = ERR_PTR(err);
1092 goto getname;
1093 }
1094
1095getname:
1096 /* We must make copy under the lock if we failed to get ref. */
1097 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1098unlock:
Rusty Russell75676502010-06-05 11:17:36 -06001099 mutex_unlock(&module_mutex);
Linus Torvalds218ce732010-05-25 16:48:30 -07001100 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
1102
Rusty Russell9bea7f22010-06-05 11:17:37 -06001103static const struct kernel_symbol *resolve_symbol_wait(Elf_Shdr *sechdrs,
1104 unsigned int versindex,
1105 const char *name,
1106 struct module *mod)
1107{
1108 const struct kernel_symbol *ksym;
1109 char ownername[MODULE_NAME_LEN];
1110
1111 if (wait_event_interruptible_timeout(module_wq,
1112 !IS_ERR(ksym = resolve_symbol(sechdrs, versindex, name,
1113 mod, ownername)) ||
1114 PTR_ERR(ksym) != -EBUSY,
1115 30 * HZ) <= 0) {
1116 printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
1117 mod->name, ownername);
1118 }
1119 return ksym;
1120}
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122/*
1123 * /sys/module/foo/sections stuff
1124 * J. Corbet <corbet@lwn.net>
1125 */
Kay Sievers120fc3d2008-02-21 00:33:20 +01001126#if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
Ben Hutchings10b465a2009-12-19 14:43:01 +00001127
1128static inline bool sect_empty(const Elf_Shdr *sect)
1129{
1130 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1131}
1132
Rusty Russella58730c2008-03-13 09:03:44 +00001133struct module_sect_attr
1134{
1135 struct module_attribute mattr;
1136 char *name;
1137 unsigned long address;
1138};
1139
1140struct module_sect_attrs
1141{
1142 struct attribute_group grp;
1143 unsigned int nsections;
1144 struct module_sect_attr attrs[0];
1145};
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147static ssize_t module_sect_show(struct module_attribute *mattr,
1148 struct module *mod, char *buf)
1149{
1150 struct module_sect_attr *sattr =
1151 container_of(mattr, struct module_sect_attr, mattr);
1152 return sprintf(buf, "0x%lx\n", sattr->address);
1153}
1154
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001155static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1156{
Rusty Russella58730c2008-03-13 09:03:44 +00001157 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001158
1159 for (section = 0; section < sect_attrs->nsections; section++)
1160 kfree(sect_attrs->attrs[section].name);
1161 kfree(sect_attrs);
1162}
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164static void add_sect_attrs(struct module *mod, unsigned int nsect,
1165 char *secstrings, Elf_Shdr *sechdrs)
1166{
1167 unsigned int nloaded = 0, i, size[2];
1168 struct module_sect_attrs *sect_attrs;
1169 struct module_sect_attr *sattr;
1170 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 /* Count loaded sections and allocate structures */
1173 for (i = 0; i < nsect; i++)
Ben Hutchings10b465a2009-12-19 14:43:01 +00001174 if (!sect_empty(&sechdrs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 nloaded++;
1176 size[0] = ALIGN(sizeof(*sect_attrs)
1177 + nloaded * sizeof(sect_attrs->attrs[0]),
1178 sizeof(sect_attrs->grp.attrs[0]));
1179 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001180 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1181 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return;
1183
1184 /* Setup section attributes. */
1185 sect_attrs->grp.name = "sections";
1186 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1187
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001188 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 sattr = &sect_attrs->attrs[0];
1190 gattr = &sect_attrs->grp.attrs[0];
1191 for (i = 0; i < nsect; i++) {
Ben Hutchings10b465a2009-12-19 14:43:01 +00001192 if (sect_empty(&sechdrs[i]))
Helge Deller35dead42009-12-03 00:29:15 +01001193 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 sattr->address = sechdrs[i].sh_addr;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001195 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1196 GFP_KERNEL);
1197 if (sattr->name == NULL)
1198 goto out;
1199 sect_attrs->nsections++;
Eric W. Biederman361795b2010-02-12 13:41:56 -08001200 sysfs_attr_init(&sattr->mattr.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 sattr->mattr.show = module_sect_show;
1202 sattr->mattr.store = NULL;
1203 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 sattr->mattr.attr.mode = S_IRUGO;
1205 *(gattr++) = &(sattr++)->mattr.attr;
1206 }
1207 *gattr = NULL;
1208
1209 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1210 goto out;
1211
1212 mod->sect_attrs = sect_attrs;
1213 return;
1214 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001215 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
1218static void remove_sect_attrs(struct module *mod)
1219{
1220 if (mod->sect_attrs) {
1221 sysfs_remove_group(&mod->mkobj.kobj,
1222 &mod->sect_attrs->grp);
1223 /* We are positive that no one is using any sect attrs
1224 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001225 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 mod->sect_attrs = NULL;
1227 }
1228}
1229
Roland McGrath6d760132007-10-16 23:26:40 -07001230/*
1231 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1232 */
1233
1234struct module_notes_attrs {
1235 struct kobject *dir;
1236 unsigned int notes;
1237 struct bin_attribute attrs[0];
1238};
1239
Chris Wright2c3c8be2010-05-12 18:28:57 -07001240static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
Roland McGrath6d760132007-10-16 23:26:40 -07001241 struct bin_attribute *bin_attr,
1242 char *buf, loff_t pos, size_t count)
1243{
1244 /*
1245 * The caller checked the pos and count against our size.
1246 */
1247 memcpy(buf, bin_attr->private + pos, count);
1248 return count;
1249}
1250
1251static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1252 unsigned int i)
1253{
1254 if (notes_attrs->dir) {
1255 while (i-- > 0)
1256 sysfs_remove_bin_file(notes_attrs->dir,
1257 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001258 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001259 }
1260 kfree(notes_attrs);
1261}
1262
1263static void add_notes_attrs(struct module *mod, unsigned int nsect,
1264 char *secstrings, Elf_Shdr *sechdrs)
1265{
1266 unsigned int notes, loaded, i;
1267 struct module_notes_attrs *notes_attrs;
1268 struct bin_attribute *nattr;
1269
Ingo Molnarea6bff32009-08-28 10:44:56 +02001270 /* failed to create section attributes, so can't create notes */
1271 if (!mod->sect_attrs)
1272 return;
1273
Roland McGrath6d760132007-10-16 23:26:40 -07001274 /* Count notes sections and allocate structures. */
1275 notes = 0;
1276 for (i = 0; i < nsect; i++)
Ben Hutchings10b465a2009-12-19 14:43:01 +00001277 if (!sect_empty(&sechdrs[i]) &&
Roland McGrath6d760132007-10-16 23:26:40 -07001278 (sechdrs[i].sh_type == SHT_NOTE))
1279 ++notes;
1280
1281 if (notes == 0)
1282 return;
1283
1284 notes_attrs = kzalloc(sizeof(*notes_attrs)
1285 + notes * sizeof(notes_attrs->attrs[0]),
1286 GFP_KERNEL);
1287 if (notes_attrs == NULL)
1288 return;
1289
1290 notes_attrs->notes = notes;
1291 nattr = &notes_attrs->attrs[0];
1292 for (loaded = i = 0; i < nsect; ++i) {
Ben Hutchings10b465a2009-12-19 14:43:01 +00001293 if (sect_empty(&sechdrs[i]))
Roland McGrath6d760132007-10-16 23:26:40 -07001294 continue;
1295 if (sechdrs[i].sh_type == SHT_NOTE) {
Eric W. Biederman361795b2010-02-12 13:41:56 -08001296 sysfs_bin_attr_init(nattr);
Roland McGrath6d760132007-10-16 23:26:40 -07001297 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1298 nattr->attr.mode = S_IRUGO;
1299 nattr->size = sechdrs[i].sh_size;
1300 nattr->private = (void *) sechdrs[i].sh_addr;
1301 nattr->read = module_notes_read;
1302 ++nattr;
1303 }
1304 ++loaded;
1305 }
1306
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001307 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001308 if (!notes_attrs->dir)
1309 goto out;
1310
1311 for (i = 0; i < notes; ++i)
1312 if (sysfs_create_bin_file(notes_attrs->dir,
1313 &notes_attrs->attrs[i]))
1314 goto out;
1315
1316 mod->notes_attrs = notes_attrs;
1317 return;
1318
1319 out:
1320 free_notes_attrs(notes_attrs, i);
1321}
1322
1323static void remove_notes_attrs(struct module *mod)
1324{
1325 if (mod->notes_attrs)
1326 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1327}
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1332 char *sectstrings, Elf_Shdr *sechdrs)
1333{
1334}
1335
1336static inline void remove_sect_attrs(struct module *mod)
1337{
1338}
Roland McGrath6d760132007-10-16 23:26:40 -07001339
1340static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1341 char *sectstrings, Elf_Shdr *sechdrs)
1342{
1343}
1344
1345static inline void remove_notes_attrs(struct module *mod)
1346{
1347}
Kay Sievers120fc3d2008-02-21 00:33:20 +01001348#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Randy Dunlapef665c12007-02-13 15:19:06 -08001350#ifdef CONFIG_SYSFS
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001351static void add_usage_links(struct module *mod)
1352{
1353#ifdef CONFIG_MODULE_UNLOAD
1354 struct module_use *use;
1355 int nowarn;
1356
Rusty Russell75676502010-06-05 11:17:36 -06001357 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001358 list_for_each_entry(use, &mod->target_list, target_list) {
1359 nowarn = sysfs_create_link(use->target->holders_dir,
1360 &mod->mkobj.kobj, mod->name);
1361 }
Rusty Russell75676502010-06-05 11:17:36 -06001362 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001363#endif
1364}
1365
1366static void del_usage_links(struct module *mod)
1367{
1368#ifdef CONFIG_MODULE_UNLOAD
1369 struct module_use *use;
1370
Rusty Russell75676502010-06-05 11:17:36 -06001371 mutex_lock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001372 list_for_each_entry(use, &mod->target_list, target_list)
1373 sysfs_remove_link(use->target->holders_dir, mod->name);
Rusty Russell75676502010-06-05 11:17:36 -06001374 mutex_unlock(&module_mutex);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001375#endif
1376}
1377
Rusty Russell6407ebb22010-06-05 11:17:36 -06001378static int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001379{
1380 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001381 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001382 int error = 0;
1383 int i;
1384
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001385 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1386 (ARRAY_SIZE(modinfo_attrs) + 1)),
1387 GFP_KERNEL);
1388 if (!mod->modinfo_attrs)
1389 return -ENOMEM;
1390
1391 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001392 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1393 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001394 (attr->test && attr->test(mod))) {
1395 memcpy(temp_attr, attr, sizeof(*temp_attr));
Eric W. Biederman361795b2010-02-12 13:41:56 -08001396 sysfs_attr_init(&temp_attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001397 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1398 ++temp_attr;
1399 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001400 }
1401 return error;
1402}
1403
Rusty Russell6407ebb22010-06-05 11:17:36 -06001404static void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001405{
1406 struct module_attribute *attr;
1407 int i;
1408
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001409 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1410 /* pick a field to test for end of list */
1411 if (!attr->attr.name)
1412 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001413 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001414 if (attr->free)
1415 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001416 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001417 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001418}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Rusty Russell6407ebb22010-06-05 11:17:36 -06001420static int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
1422 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001423 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001425 if (!module_sysfs_initialized) {
1426 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001427 mod->name);
1428 err = -EINVAL;
1429 goto out;
1430 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001431
1432 kobj = kset_find_obj(module_kset, mod->name);
1433 if (kobj) {
1434 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1435 kobject_put(kobj);
1436 err = -EINVAL;
1437 goto out;
1438 }
1439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001441
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001442 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1443 mod->mkobj.kobj.kset = module_kset;
1444 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1445 "%s", mod->name);
1446 if (err)
1447 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001448
Kay Sievers97c146e2007-11-29 23:46:11 +01001449 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001450out:
1451 return err;
1452}
1453
Rusty Russell6407ebb22010-06-05 11:17:36 -06001454static int mod_sysfs_setup(struct module *mod,
Kay Sievers270a6c42007-01-18 13:26:15 +01001455 struct kernel_param *kparam,
1456 unsigned int num_params)
1457{
1458 int err;
1459
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001460 err = mod_sysfs_init(mod);
1461 if (err)
1462 goto out;
1463
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001464 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001465 if (!mod->holders_dir) {
1466 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001467 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001468 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 err = module_param_sysfs_setup(mod, kparam, num_params);
1471 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001472 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Matt Domschc988d2b2005-06-23 22:05:15 -07001474 err = module_add_modinfo_attrs(mod);
1475 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001476 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001477
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001478 add_usage_links(mod);
1479
Kay Sieverse17e0f52006-11-24 12:15:25 +01001480 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return 0;
1482
Kay Sieverse17e0f52006-11-24 12:15:25 +01001483out_unreg_param:
1484 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001485out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001486 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001487out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001488 kobject_put(&mod->mkobj.kobj);
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001489out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 return err;
1491}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001492
1493static void mod_sysfs_fini(struct module *mod)
1494{
1495 kobject_put(&mod->mkobj.kobj);
1496}
1497
1498#else /* CONFIG_SYSFS */
1499
Rusty Russell6407ebb22010-06-05 11:17:36 -06001500static inline int mod_sysfs_init(struct module *mod)
1501{
1502 return 0;
1503}
1504
1505static inline int mod_sysfs_setup(struct module *mod,
1506 struct kernel_param *kparam,
1507 unsigned int num_params)
1508{
1509 return 0;
1510}
1511
1512static inline int module_add_modinfo_attrs(struct module *mod)
1513{
1514 return 0;
1515}
1516
1517static inline void module_remove_modinfo_attrs(struct module *mod)
1518{
1519}
1520
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001521static void mod_sysfs_fini(struct module *mod)
1522{
1523}
1524
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001525static void del_usage_links(struct module *mod)
1526{
1527}
1528
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001529#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531static void mod_kobject_remove(struct module *mod)
1532{
Rusty Russell80a3d1b2010-06-05 11:17:36 -06001533 del_usage_links(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001534 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001536 kobject_put(mod->mkobj.drivers_dir);
1537 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001538 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
1541/*
1542 * unlink the module with the whole machine is stopped with interrupts off
1543 * - this defends against kallsyms not taking locks
1544 */
1545static int __unlink_module(void *_mod)
1546{
1547 struct module *mod = _mod;
1548 list_del(&mod->list);
1549 return 0;
1550}
1551
Rusty Russell75676502010-06-05 11:17:36 -06001552/* Free a module, remove from lists, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553static void free_module(struct module *mod)
1554{
Li Zefan7ead8b82009-08-17 16:56:28 +08001555 trace_module_free(mod);
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 /* Delete from various lists */
Rusty Russell75676502010-06-05 11:17:36 -06001558 mutex_lock(&module_mutex);
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001559 stop_machine(__unlink_module, mod, NULL);
Rusty Russell75676502010-06-05 11:17:36 -06001560 mutex_unlock(&module_mutex);
Roland McGrath6d760132007-10-16 23:26:40 -07001561 remove_notes_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 remove_sect_attrs(mod);
1563 mod_kobject_remove(mod);
1564
Jason Baronb82bab4b2010-07-27 13:18:01 -07001565 /* Remove dynamic debug info */
1566 ddebug_remove_module(mod->name);
1567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 /* Arch-specific cleanup. */
1569 module_arch_cleanup(mod);
1570
1571 /* Module unload stuff */
1572 module_unload_free(mod);
1573
Rusty Russelle180a6b2009-03-31 13:05:29 -06001574 /* Free any allocated parameters. */
1575 destroy_params(mod->kp, mod->num_kp);
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 /* This may be NULL, but that's OK */
1578 module_free(mod, mod->module_init);
1579 kfree(mod->args);
Tejun Heo259354d2010-03-10 18:56:10 +09001580 percpu_modfree(mod);
Rusty Russell9f85a4b2010-08-05 12:59:04 -06001581
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001582 /* Free lock-classes: */
1583 lockdep_free_key_range(mod->module_core, mod->core_size);
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 /* Finally, free the core (containing the module structure) */
1586 module_free(mod, mod->module_core);
Bernd Schmidteb8cdec2009-09-21 17:03:57 -07001587
1588#ifdef CONFIG_MPU
1589 update_protections(current->mm);
1590#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
1593void *__symbol_get(const char *symbol)
1594{
1595 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001596 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Rusty Russell24da1cb2007-07-15 23:41:46 -07001598 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001599 sym = find_symbol(symbol, &owner, NULL, true, true);
1600 if (sym && strong_try_module_get(owner))
1601 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001602 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Tim Abbott414fd312008-12-05 19:03:56 -05001604 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605}
1606EXPORT_SYMBOL_GPL(__symbol_get);
1607
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001608/*
1609 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001610 * in the kernel or in some other module's exported symbol table.
Rusty Russellbe593f42010-06-05 11:17:37 -06001611 *
1612 * You must hold the module_mutex.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001613 */
1614static int verify_export_symbols(struct module *mod)
1615{
Rusty Russellb2111042008-05-01 21:15:00 -05001616 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001617 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001618 const struct kernel_symbol *s;
1619 struct {
1620 const struct kernel_symbol *sym;
1621 unsigned int num;
1622 } arr[] = {
1623 { mod->syms, mod->num_syms },
1624 { mod->gpl_syms, mod->num_gpl_syms },
1625 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001626#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001627 { mod->unused_syms, mod->num_unused_syms },
1628 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001629#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001630 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001631
Rusty Russellb2111042008-05-01 21:15:00 -05001632 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1633 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Rusty Russellbe593f42010-06-05 11:17:37 -06001634 if (find_symbol(s->name, &owner, NULL, true, false)) {
Rusty Russellb2111042008-05-01 21:15:00 -05001635 printk(KERN_ERR
1636 "%s: exports duplicate symbol %s"
1637 " (owned by %s)\n",
1638 mod->name, s->name, module_name(owner));
1639 return -ENOEXEC;
1640 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001641 }
Rusty Russellb2111042008-05-01 21:15:00 -05001642 }
1643 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001644}
1645
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001646/* Change all symbols so that st_value encodes the pointer directly. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647static int simplify_symbols(Elf_Shdr *sechdrs,
1648 unsigned int symindex,
1649 const char *strtab,
1650 unsigned int versindex,
1651 unsigned int pcpuindex,
1652 struct module *mod)
1653{
1654 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1655 unsigned long secbase;
1656 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1657 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05001658 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 for (i = 1; i < n; i++) {
1661 switch (sym[i].st_shndx) {
1662 case SHN_COMMON:
1663 /* We compiled with -fno-common. These are not
1664 supposed to happen. */
1665 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1666 printk("%s: please compile with -fno-common\n",
1667 mod->name);
1668 ret = -ENOEXEC;
1669 break;
1670
1671 case SHN_ABS:
1672 /* Don't need to do anything */
1673 DEBUGP("Absolute symbol: 0x%08lx\n",
1674 (long)sym[i].st_value);
1675 break;
1676
1677 case SHN_UNDEF:
Rusty Russell9bea7f22010-06-05 11:17:37 -06001678 ksym = resolve_symbol_wait(sechdrs, versindex,
1679 strtab + sym[i].st_name,
1680 mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 /* Ok if resolved. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001682 if (ksym && !IS_ERR(ksym)) {
Tim Abbott414fd312008-12-05 19:03:56 -05001683 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 break;
Tim Abbott414fd312008-12-05 19:03:56 -05001685 }
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 /* Ok if weak. */
Rusty Russell9bea7f22010-06-05 11:17:37 -06001688 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 break;
1690
Rusty Russell9bea7f22010-06-05 11:17:37 -06001691 printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
1692 mod->name, strtab + sym[i].st_name,
1693 PTR_ERR(ksym));
1694 ret = PTR_ERR(ksym) ?: -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 break;
1696
1697 default:
1698 /* Divert to percpu allocation if a percpu var. */
1699 if (sym[i].st_shndx == pcpuindex)
Tejun Heo259354d2010-03-10 18:56:10 +09001700 secbase = (unsigned long)mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 else
1702 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1703 sym[i].st_value += secbase;
1704 break;
1705 }
1706 }
1707
1708 return ret;
1709}
1710
Rusty Russell22e268e2010-08-05 12:59:05 -06001711static int apply_relocations(struct module *mod,
1712 Elf_Ehdr *hdr,
1713 Elf_Shdr *sechdrs,
1714 unsigned int symindex,
1715 unsigned int strindex)
1716{
1717 unsigned int i;
1718 int err = 0;
1719
1720 /* Now do relocations. */
1721 for (i = 1; i < hdr->e_shnum; i++) {
1722 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1723 unsigned int info = sechdrs[i].sh_info;
1724
1725 /* Not a valid relocation section? */
1726 if (info >= hdr->e_shnum)
1727 continue;
1728
1729 /* Don't bother with non-allocated sections */
1730 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
1731 continue;
1732
1733 if (sechdrs[i].sh_type == SHT_REL)
1734 err = apply_relocate(sechdrs, strtab, symindex, i, mod);
1735 else if (sechdrs[i].sh_type == SHT_RELA)
1736 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1737 mod);
1738 if (err < 0)
1739 break;
1740 }
1741 return err;
1742}
1743
Helge Deller088af9a2008-12-31 12:31:18 +01001744/* Additional bytes needed by arch in front of individual sections */
1745unsigned int __weak arch_mod_section_prepend(struct module *mod,
1746 unsigned int section)
1747{
1748 /* default implementation just returns zero */
1749 return 0;
1750}
1751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01001753static long get_offset(struct module *mod, unsigned int *size,
1754 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
1756 long ret;
1757
Helge Deller088af9a2008-12-31 12:31:18 +01001758 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1760 *size = ret + sechdr->sh_size;
1761 return ret;
1762}
1763
1764/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1765 might -- code, read-only data, read-write data, small data. Tally
1766 sizes, and place the offsets into sh_entsize fields: high bit means it
1767 belongs in init. */
1768static void layout_sections(struct module *mod,
1769 const Elf_Ehdr *hdr,
1770 Elf_Shdr *sechdrs,
1771 const char *secstrings)
1772{
1773 static unsigned long const masks[][2] = {
1774 /* NOTE: all executable code must be the first section
1775 * in this array; otherwise modify the text_size
1776 * finder in the two loops below */
1777 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1778 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1779 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1780 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1781 };
1782 unsigned int m, i;
1783
1784 for (i = 0; i < hdr->e_shnum; i++)
1785 sechdrs[i].sh_entsize = ~0UL;
1786
1787 DEBUGP("Core section allocation order:\n");
1788 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1789 for (i = 0; i < hdr->e_shnum; ++i) {
1790 Elf_Shdr *s = &sechdrs[i];
1791
1792 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1793 || (s->sh_flags & masks[m][1])
1794 || s->sh_entsize != ~0UL
Rusty Russell49502672009-03-31 13:05:36 -06001795 || strstarts(secstrings + s->sh_name, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001797 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 DEBUGP("\t%s\n", secstrings + s->sh_name);
1799 }
1800 if (m == 0)
1801 mod->core_text_size = mod->core_size;
1802 }
1803
1804 DEBUGP("Init section allocation order:\n");
1805 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1806 for (i = 0; i < hdr->e_shnum; ++i) {
1807 Elf_Shdr *s = &sechdrs[i];
1808
1809 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1810 || (s->sh_flags & masks[m][1])
1811 || s->sh_entsize != ~0UL
Rusty Russell49502672009-03-31 13:05:36 -06001812 || !strstarts(secstrings + s->sh_name, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001814 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 | INIT_OFFSET_MASK);
1816 DEBUGP("\t%s\n", secstrings + s->sh_name);
1817 }
1818 if (m == 0)
1819 mod->init_text_size = mod->init_size;
1820 }
1821}
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823static void set_license(struct module *mod, const char *license)
1824{
1825 if (!license)
1826 license = "unspecified";
1827
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001828 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07001829 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001830 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001831 "kernel.\n", mod->name, license);
1832 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 }
1834}
1835
1836/* Parse tag=value strings from .modinfo section */
1837static char *next_string(char *string, unsigned long *secsize)
1838{
1839 /* Skip non-zero chars */
1840 while (string[0]) {
1841 string++;
1842 if ((*secsize)-- <= 1)
1843 return NULL;
1844 }
1845
1846 /* Skip any zero padding. */
1847 while (!string[0]) {
1848 string++;
1849 if ((*secsize)-- <= 1)
1850 return NULL;
1851 }
1852 return string;
1853}
1854
Rusty Russell40dd2562010-08-05 12:59:03 -06001855static char *get_modinfo(const Elf_Shdr *sechdrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 unsigned int info,
1857 const char *tag)
1858{
1859 char *p;
1860 unsigned int taglen = strlen(tag);
1861 unsigned long size = sechdrs[info].sh_size;
1862
1863 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1864 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1865 return p + taglen + 1;
1866 }
1867 return NULL;
1868}
1869
Matt Domschc988d2b2005-06-23 22:05:15 -07001870static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1871 unsigned int infoindex)
1872{
1873 struct module_attribute *attr;
1874 int i;
1875
1876 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1877 if (attr->setup)
1878 attr->setup(mod,
1879 get_modinfo(sechdrs,
1880 infoindex,
1881 attr->attr.name));
1882 }
1883}
Matt Domschc988d2b2005-06-23 22:05:15 -07001884
Rusty Russella263f772009-09-25 00:32:58 -06001885static void free_modinfo(struct module *mod)
1886{
1887 struct module_attribute *attr;
1888 int i;
1889
1890 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1891 if (attr->free)
1892 attr->free(mod);
1893 }
1894}
1895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01001897
1898/* lookup symbol in given range of kernel_symbols */
1899static const struct kernel_symbol *lookup_symbol(const char *name,
1900 const struct kernel_symbol *start,
1901 const struct kernel_symbol *stop)
1902{
1903 const struct kernel_symbol *ks = start;
1904 for (; ks < stop; ks++)
1905 if (strcmp(ks->name, name) == 0)
1906 return ks;
1907 return NULL;
1908}
1909
Tim Abbottca4787b2009-01-05 08:40:10 -06001910static int is_exported(const char *name, unsigned long value,
1911 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
Tim Abbottca4787b2009-01-05 08:40:10 -06001913 const struct kernel_symbol *ks;
1914 if (!mod)
1915 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001916 else
Tim Abbottca4787b2009-01-05 08:40:10 -06001917 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
1918 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919}
1920
1921/* As per nm */
Rusty Russelleded41c2010-08-05 12:59:07 -06001922static char elf_type(const Elf_Sym *sym, const struct load_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
Rusty Russelleded41c2010-08-05 12:59:07 -06001924 const Elf_Shdr *sechdrs = info->sechdrs;
1925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1927 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1928 return 'v';
1929 else
1930 return 'w';
1931 }
1932 if (sym->st_shndx == SHN_UNDEF)
1933 return 'U';
1934 if (sym->st_shndx == SHN_ABS)
1935 return 'a';
1936 if (sym->st_shndx >= SHN_LORESERVE)
1937 return '?';
1938 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1939 return 't';
1940 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1941 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1942 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1943 return 'r';
1944 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1945 return 'g';
1946 else
1947 return 'd';
1948 }
1949 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1950 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1951 return 's';
1952 else
1953 return 'b';
1954 }
Rusty Russelleded41c2010-08-05 12:59:07 -06001955 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
1956 ".debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 return 'n';
Rusty Russelleded41c2010-08-05 12:59:07 -06001958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 return '?';
1960}
1961
Jan Beulich4a496222009-07-06 14:50:42 +01001962static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
1963 unsigned int shnum)
1964{
1965 const Elf_Shdr *sec;
1966
1967 if (src->st_shndx == SHN_UNDEF
1968 || src->st_shndx >= shnum
1969 || !src->st_name)
1970 return false;
1971
1972 sec = sechdrs + src->st_shndx;
1973 if (!(sec->sh_flags & SHF_ALLOC)
1974#ifndef CONFIG_KALLSYMS_ALL
1975 || !(sec->sh_flags & SHF_EXECINSTR)
1976#endif
1977 || (sec->sh_entsize & INIT_OFFSET_MASK))
1978 return false;
1979
1980 return true;
1981}
1982
1983static unsigned long layout_symtab(struct module *mod,
1984 Elf_Shdr *sechdrs,
1985 unsigned int symindex,
Jan Beulich554bdfe2009-07-06 14:51:44 +01001986 unsigned int strindex,
Jan Beulich4a496222009-07-06 14:50:42 +01001987 const Elf_Ehdr *hdr,
Jan Beulich554bdfe2009-07-06 14:51:44 +01001988 const char *secstrings,
1989 unsigned long *pstroffs,
1990 unsigned long *strmap)
Jan Beulich4a496222009-07-06 14:50:42 +01001991{
1992 unsigned long symoffs;
1993 Elf_Shdr *symsect = sechdrs + symindex;
Jan Beulich554bdfe2009-07-06 14:51:44 +01001994 Elf_Shdr *strsect = sechdrs + strindex;
Jan Beulich4a496222009-07-06 14:50:42 +01001995 const Elf_Sym *src;
Jan Beulich554bdfe2009-07-06 14:51:44 +01001996 const char *strtab;
Jan Beulich4a496222009-07-06 14:50:42 +01001997 unsigned int i, nsrc, ndst;
1998
1999 /* Put symbol section at end of init part of module. */
2000 symsect->sh_flags |= SHF_ALLOC;
2001 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
2002 symindex) | INIT_OFFSET_MASK;
2003 DEBUGP("\t%s\n", secstrings + symsect->sh_name);
2004
2005 src = (void *)hdr + symsect->sh_offset;
2006 nsrc = symsect->sh_size / sizeof(*src);
Jan Beulich554bdfe2009-07-06 14:51:44 +01002007 strtab = (void *)hdr + strsect->sh_offset;
Jan Beulich4a496222009-07-06 14:50:42 +01002008 for (ndst = i = 1; i < nsrc; ++i, ++src)
Jan Beulich554bdfe2009-07-06 14:51:44 +01002009 if (is_core_symbol(src, sechdrs, hdr->e_shnum)) {
2010 unsigned int j = src->st_name;
2011
2012 while(!__test_and_set_bit(j, strmap) && strtab[j])
2013 ++j;
Jan Beulich4a496222009-07-06 14:50:42 +01002014 ++ndst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002015 }
Jan Beulich4a496222009-07-06 14:50:42 +01002016
2017 /* Append room for core symbols at end of core part. */
2018 symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
2019 mod->core_size = symoffs + ndst * sizeof(Elf_Sym);
2020
Jan Beulich554bdfe2009-07-06 14:51:44 +01002021 /* Put string table section at end of init part of module. */
2022 strsect->sh_flags |= SHF_ALLOC;
2023 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
2024 strindex) | INIT_OFFSET_MASK;
2025 DEBUGP("\t%s\n", secstrings + strsect->sh_name);
2026
2027 /* Append room for core symbols' strings at end of core part. */
2028 *pstroffs = mod->core_size;
2029 __set_bit(0, strmap);
2030 mod->core_size += bitmap_weight(strmap, strsect->sh_size);
2031
Jan Beulich4a496222009-07-06 14:50:42 +01002032 return symoffs;
2033}
2034
Rusty Russelleded41c2010-08-05 12:59:07 -06002035static void add_kallsyms(struct module *mod, struct load_info *info,
Jan Beulich4a496222009-07-06 14:50:42 +01002036 unsigned long symoffs,
Jan Beulich554bdfe2009-07-06 14:51:44 +01002037 unsigned long stroffs,
Jan Beulich554bdfe2009-07-06 14:51:44 +01002038 unsigned long *strmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
Jan Beulich4a496222009-07-06 14:50:42 +01002040 unsigned int i, ndst;
2041 const Elf_Sym *src;
2042 Elf_Sym *dst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002043 char *s;
Rusty Russelleded41c2010-08-05 12:59:07 -06002044 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Rusty Russelleded41c2010-08-05 12:59:07 -06002046 mod->symtab = (void *)symsec->sh_addr;
2047 mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
Rusty Russell511ca6a2010-08-05 12:59:08 -06002048 /* Make sure we get permanent strtab: don't use info->strtab. */
2049 mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
2051 /* Set types up while we still have access to sections. */
2052 for (i = 0; i < mod->num_symtab; i++)
Rusty Russelleded41c2010-08-05 12:59:07 -06002053 mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
Jan Beulich4a496222009-07-06 14:50:42 +01002054
2055 mod->core_symtab = dst = mod->module_core + symoffs;
2056 src = mod->symtab;
2057 *dst = *src;
2058 for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
Rusty Russelleded41c2010-08-05 12:59:07 -06002059 if (!is_core_symbol(src, info->sechdrs, info->hdr->e_shnum))
Jan Beulich4a496222009-07-06 14:50:42 +01002060 continue;
2061 dst[ndst] = *src;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002062 dst[ndst].st_name = bitmap_weight(strmap, dst[ndst].st_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002063 ++ndst;
2064 }
2065 mod->core_num_syms = ndst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002066
2067 mod->core_strtab = s = mod->module_core + stroffs;
Rusty Russelleded41c2010-08-05 12:59:07 -06002068 for (*s = 0, i = 1; i < info->sechdrs[info->index.str].sh_size; ++i)
Jan Beulich554bdfe2009-07-06 14:51:44 +01002069 if (test_bit(i, strmap))
2070 *++s = mod->strtab[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071}
2072#else
Jan Beulich4a496222009-07-06 14:50:42 +01002073static inline unsigned long layout_symtab(struct module *mod,
2074 Elf_Shdr *sechdrs,
2075 unsigned int symindex,
Jan Beulich554bdfe2009-07-06 14:51:44 +01002076 unsigned int strindex,
Paul Mundt3ae91c22009-10-01 15:43:54 -07002077 const Elf_Ehdr *hdr,
Jan Beulich554bdfe2009-07-06 14:51:44 +01002078 const char *secstrings,
2079 unsigned long *pstroffs,
2080 unsigned long *strmap)
Jan Beulich4a496222009-07-06 14:50:42 +01002081{
Paul Mundt3ae91c22009-10-01 15:43:54 -07002082 return 0;
Jan Beulich4a496222009-07-06 14:50:42 +01002083}
Paul Mundt3ae91c22009-10-01 15:43:54 -07002084
Rusty Russelleded41c2010-08-05 12:59:07 -06002085static void add_kallsyms(struct module *mod, struct load_info *info,
2086 unsigned long symoffs,
2087 unsigned long stroffs,
2088 unsigned long *strmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
2090}
2091#endif /* CONFIG_KALLSYMS */
2092
Jason Barone9d376f2009-02-05 11:51:38 -05002093static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05002094{
Jason Barone9d376f2009-02-05 11:51:38 -05002095#ifdef CONFIG_DYNAMIC_DEBUG
2096 if (ddebug_add_module(debug, num, debug->modname))
2097 printk(KERN_ERR "dynamic debug error adding module: %s\n",
2098 debug->modname);
2099#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002100}
Jason Baron346e15b2008-08-12 16:46:19 -04002101
Yehuda Sadehff49d742010-07-03 13:07:35 +10002102static void dynamic_debug_remove(struct _ddebug *debug)
2103{
2104 if (debug)
2105 ddebug_remove_module(debug->modname);
2106}
2107
Rusty Russell3a642e92008-07-22 19:24:28 -05002108static void *module_alloc_update_bounds(unsigned long size)
2109{
2110 void *ret = module_alloc(size);
2111
2112 if (ret) {
Rusty Russell75676502010-06-05 11:17:36 -06002113 mutex_lock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002114 /* Update module bounds. */
2115 if ((unsigned long)ret < module_addr_min)
2116 module_addr_min = (unsigned long)ret;
2117 if ((unsigned long)ret + size > module_addr_max)
2118 module_addr_max = (unsigned long)ret + size;
Rusty Russell75676502010-06-05 11:17:36 -06002119 mutex_unlock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002120 }
2121 return ret;
2122}
2123
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002124#ifdef CONFIG_DEBUG_KMEMLEAK
2125static void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002126 const Elf_Shdr *sechdrs,
2127 const char *secstrings)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002128{
2129 unsigned int i;
2130
2131 /* only scan the sections containing data */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002132 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002133
2134 for (i = 1; i < hdr->e_shnum; i++) {
2135 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2136 continue;
2137 if (strncmp(secstrings + sechdrs[i].sh_name, ".data", 5) != 0
2138 && strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) != 0)
2139 continue;
2140
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002141 kmemleak_scan_area((void *)sechdrs[i].sh_addr,
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002142 sechdrs[i].sh_size, GFP_KERNEL);
2143 }
2144}
2145#else
2146static inline void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002147 Elf_Shdr *sechdrs,
2148 const char *secstrings)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002149{
2150}
2151#endif
2152
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002153/* Sets info->hdr and info->len. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002154static int copy_and_check(struct load_info *info, const void __user *umod, unsigned long len)
Rusty Russell40dd2562010-08-05 12:59:03 -06002155{
2156 int err;
2157 Elf_Ehdr *hdr;
2158
2159 if (len < sizeof(*hdr))
2160 return -ENOEXEC;
2161
2162 /* Suck in entire file: we'll want most of it. */
2163 /* vmalloc barfs on "unusual" numbers. Check here */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002164 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
Rusty Russell40dd2562010-08-05 12:59:03 -06002165 return -ENOMEM;
2166
2167 if (copy_from_user(hdr, umod, len) != 0) {
2168 err = -EFAULT;
2169 goto free_hdr;
2170 }
2171
2172 /* Sanity checks against insmoding binaries or wrong arch,
2173 weird elf version */
2174 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
2175 || hdr->e_type != ET_REL
2176 || !elf_check_arch(hdr)
2177 || hdr->e_shentsize != sizeof(Elf_Shdr)) {
2178 err = -ENOEXEC;
2179 goto free_hdr;
2180 }
2181
2182 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) {
2183 err = -ENOEXEC;
2184 goto free_hdr;
2185 }
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002186 info->hdr = hdr;
2187 info->len = len;
Rusty Russell40dd2562010-08-05 12:59:03 -06002188 return 0;
2189
2190free_hdr:
2191 vfree(hdr);
2192 return err;
2193}
2194
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002195static int rewrite_section_headers(struct load_info *info)
2196{
2197 unsigned int i;
2198
2199 /* This should always be true, but let's be sure. */
2200 info->sechdrs[0].sh_addr = 0;
2201
2202 for (i = 1; i < info->hdr->e_shnum; i++) {
2203 Elf_Shdr *shdr = &info->sechdrs[i];
2204 if (shdr->sh_type != SHT_NOBITS
2205 && info->len < shdr->sh_offset + shdr->sh_size) {
2206 printk(KERN_ERR "Module len %lu truncated\n",
2207 info->len);
2208 return -ENOEXEC;
2209 }
2210
2211 /* Mark all sections sh_addr with their address in the
2212 temporary image. */
2213 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2214
2215#ifndef CONFIG_MODULE_UNLOAD
2216 /* Don't load .exit sections */
2217 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2218 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2219#endif
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002220 }
Rusty Russelld6df72a2010-08-05 12:59:07 -06002221
2222 /* Track but don't keep modinfo and version sections. */
2223 info->index.vers = find_sec(info->hdr, info->sechdrs, info->secstrings, "__versions");
2224 info->index.info = find_sec(info->hdr, info->sechdrs, info->secstrings, ".modinfo");
2225 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2226 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002227 return 0;
2228}
2229
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002230/*
2231 * Set up our basic convenience variables (pointers to section headers,
2232 * search for module section index etc), and do some basic section
2233 * verification.
2234 *
2235 * Return the temporary module pointer (we'll replace it with the final
2236 * one when we move the module sections around).
2237 */
2238static struct module *setup_load_info(struct load_info *info)
2239{
2240 unsigned int i;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002241 int err;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002242 struct module *mod;
2243
2244 /* Set up the convenience variables */
2245 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002246 info->secstrings = (void *)info->hdr
2247 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002248
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002249 err = rewrite_section_headers(info);
2250 if (err)
2251 return ERR_PTR(err);
2252
2253 /* Find internal symbols and strings. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002254 for (i = 1; i < info->hdr->e_shnum; i++) {
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002255 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2256 info->index.sym = i;
2257 info->index.str = info->sechdrs[i].sh_link;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002258 info->strtab = (char *)info->hdr
2259 + info->sechdrs[info->index.str].sh_offset;
2260 break;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002261 }
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002262 }
2263
2264 info->index.mod = find_sec(info->hdr, info->sechdrs, info->secstrings,
2265 ".gnu.linkonce.this_module");
2266 if (!info->index.mod) {
2267 printk(KERN_WARNING "No module found in object\n");
2268 return ERR_PTR(-ENOEXEC);
2269 }
2270 /* This is temporary: point mod into copy of data. */
2271 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2272
2273 if (info->index.sym == 0) {
2274 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2275 mod->name);
2276 return ERR_PTR(-ENOEXEC);
2277 }
2278
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002279 info->index.pcpu = find_pcpusec(info->hdr, info->sechdrs, info->secstrings);
2280
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002281 /* Check module struct version now, before we try to use module. */
2282 if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2283 return ERR_PTR(-ENOEXEC);
2284
2285 return mod;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002286}
2287
Rusty Russell40dd2562010-08-05 12:59:03 -06002288static int check_modinfo(struct module *mod,
2289 const Elf_Shdr *sechdrs,
2290 unsigned int infoindex, unsigned int versindex)
2291{
2292 const char *modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
2293 int err;
2294
2295 /* This is allowed: modprobe --force will invalidate it. */
2296 if (!modmagic) {
2297 err = try_to_force_load(mod, "bad vermagic");
2298 if (err)
2299 return err;
2300 } else if (!same_magic(modmagic, vermagic, versindex)) {
2301 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2302 mod->name, modmagic, vermagic);
2303 return -ENOEXEC;
2304 }
2305
2306 if (get_modinfo(sechdrs, infoindex, "staging")) {
2307 add_taint_module(mod, TAINT_CRAP);
2308 printk(KERN_WARNING "%s: module is from the staging directory,"
2309 " the quality is unknown, you have been warned.\n",
2310 mod->name);
2311 }
Rusty Russell22e268e2010-08-05 12:59:05 -06002312
2313 /* Set up license info based on the info section */
2314 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2315
Rusty Russell40dd2562010-08-05 12:59:03 -06002316 return 0;
2317}
2318
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002319static void find_module_sections(struct module *mod, Elf_Ehdr *hdr,
2320 Elf_Shdr *sechdrs, const char *secstrings)
2321{
2322 mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
2323 sizeof(*mod->kp), &mod->num_kp);
2324 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2325 sizeof(*mod->syms), &mod->num_syms);
2326 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2327 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2328 sizeof(*mod->gpl_syms),
2329 &mod->num_gpl_syms);
2330 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2331 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2332 "__ksymtab_gpl_future",
2333 sizeof(*mod->gpl_future_syms),
2334 &mod->num_gpl_future_syms);
2335 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2336 "__kcrctab_gpl_future");
2337
2338#ifdef CONFIG_UNUSED_SYMBOLS
2339 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2340 "__ksymtab_unused",
2341 sizeof(*mod->unused_syms),
2342 &mod->num_unused_syms);
2343 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2344 "__kcrctab_unused");
2345 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2346 "__ksymtab_unused_gpl",
2347 sizeof(*mod->unused_gpl_syms),
2348 &mod->num_unused_gpl_syms);
2349 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2350 "__kcrctab_unused_gpl");
2351#endif
2352#ifdef CONFIG_CONSTRUCTORS
2353 mod->ctors = section_objs(hdr, sechdrs, secstrings, ".ctors",
2354 sizeof(*mod->ctors), &mod->num_ctors);
2355#endif
2356
2357#ifdef CONFIG_TRACEPOINTS
2358 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2359 "__tracepoints",
2360 sizeof(*mod->tracepoints),
2361 &mod->num_tracepoints);
2362#endif
2363#ifdef CONFIG_EVENT_TRACING
2364 mod->trace_events = section_objs(hdr, sechdrs, secstrings,
2365 "_ftrace_events",
2366 sizeof(*mod->trace_events),
2367 &mod->num_trace_events);
2368 /*
2369 * This section contains pointers to allocated objects in the trace
2370 * code and not scanning it leads to false positives.
2371 */
2372 kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2373 mod->num_trace_events, GFP_KERNEL);
2374#endif
2375#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2376 /* sechdrs[0].sh_size is always zero */
2377 mod->ftrace_callsites = section_objs(hdr, sechdrs, secstrings,
2378 "__mcount_loc",
2379 sizeof(*mod->ftrace_callsites),
2380 &mod->num_ftrace_callsites);
2381#endif
Rusty Russell22e268e2010-08-05 12:59:05 -06002382
2383 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
2384 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2385 mod->name);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002386}
2387
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002388static struct module *move_module(struct module *mod,
2389 Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
2390 const char *secstrings, unsigned modindex)
2391{
2392 int i;
2393 void *ptr;
2394
2395 /* Do the allocs. */
2396 ptr = module_alloc_update_bounds(mod->core_size);
2397 /*
2398 * The pointer to this block is stored in the module structure
2399 * which is inside the block. Just mark it as not being a
2400 * leak.
2401 */
2402 kmemleak_not_leak(ptr);
2403 if (!ptr)
2404 return ERR_PTR(-ENOMEM);
2405
2406 memset(ptr, 0, mod->core_size);
2407 mod->module_core = ptr;
2408
2409 ptr = module_alloc_update_bounds(mod->init_size);
2410 /*
2411 * The pointer to this block is stored in the module structure
2412 * which is inside the block. This block doesn't need to be
2413 * scanned as it contains data and code that will be freed
2414 * after the module is initialized.
2415 */
2416 kmemleak_ignore(ptr);
2417 if (!ptr && mod->init_size) {
2418 module_free(mod, mod->module_core);
2419 return ERR_PTR(-ENOMEM);
2420 }
2421 memset(ptr, 0, mod->init_size);
2422 mod->module_init = ptr;
2423
2424 /* Transfer each section which specifies SHF_ALLOC */
2425 DEBUGP("final section addresses:\n");
2426 for (i = 0; i < hdr->e_shnum; i++) {
2427 void *dest;
2428
2429 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2430 continue;
2431
2432 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2433 dest = mod->module_init
2434 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2435 else
2436 dest = mod->module_core + sechdrs[i].sh_entsize;
2437
2438 if (sechdrs[i].sh_type != SHT_NOBITS)
2439 memcpy(dest, (void *)sechdrs[i].sh_addr,
2440 sechdrs[i].sh_size);
2441 /* Update sh_addr to point to copy in image. */
2442 sechdrs[i].sh_addr = (unsigned long)dest;
2443 DEBUGP("\t0x%lx %s\n",
2444 sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2445 }
2446 /* Module has been moved. */
2447 mod = (void *)sechdrs[modindex].sh_addr;
2448 kmemleak_load_module(mod, hdr, sechdrs, secstrings);
2449 return mod;
2450}
2451
Rusty Russell22e268e2010-08-05 12:59:05 -06002452static int check_module_license_and_versions(struct module *mod,
2453 Elf_Shdr *sechdrs)
2454{
2455 /*
2456 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2457 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2458 * using GPL-only symbols it needs.
2459 */
2460 if (strcmp(mod->name, "ndiswrapper") == 0)
2461 add_taint(TAINT_PROPRIETARY_MODULE);
2462
2463 /* driverloader was caught wrongly pretending to be under GPL */
2464 if (strcmp(mod->name, "driverloader") == 0)
2465 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2466
2467#ifdef CONFIG_MODVERSIONS
2468 if ((mod->num_syms && !mod->crcs)
2469 || (mod->num_gpl_syms && !mod->gpl_crcs)
2470 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2471#ifdef CONFIG_UNUSED_SYMBOLS
2472 || (mod->num_unused_syms && !mod->unused_crcs)
2473 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2474#endif
2475 ) {
2476 return try_to_force_load(mod,
2477 "no versions for exported symbols");
2478 }
2479#endif
2480 return 0;
2481}
2482
2483static void flush_module_icache(const struct module *mod)
2484{
2485 mm_segment_t old_fs;
2486
2487 /* flush the icache in correct context */
2488 old_fs = get_fs();
2489 set_fs(KERNEL_DS);
2490
2491 /*
2492 * Flush the instruction cache, since we've played with text.
2493 * Do it before processing of module parameters, so the module
2494 * can provide parameter accessor functions of its own.
2495 */
2496 if (mod->module_init)
2497 flush_icache_range((unsigned long)mod->module_init,
2498 (unsigned long)mod->module_init
2499 + mod->init_size);
2500 flush_icache_range((unsigned long)mod->module_core,
2501 (unsigned long)mod->module_core + mod->core_size);
2502
2503 set_fs(old_fs);
2504}
2505
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506/* Allocate and load the module: note that size of section 0 is always
2507 zero, and we rely on this for optional sections. */
Linus Torvaldsffb4ba72008-08-25 11:10:26 -07002508static noinline struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 unsigned long len,
2510 const char __user *uargs)
2511{
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002512 struct load_info info = { NULL, };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 struct module *mod;
Rusty Russell40dd2562010-08-05 12:59:03 -06002514 long err;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002515 unsigned long symoffs, stroffs, *strmap;
Rusty Russell293a7cf2010-05-31 19:53:50 +09302516 void __percpu *percpu;
Yehuda Sadehff49d742010-07-03 13:07:35 +10002517 struct _ddebug *debug = NULL;
2518 unsigned int num_debug = 0;
Paul Mundt3ae91c22009-10-01 15:43:54 -07002519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
2521 umod, len, uargs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002523 err = copy_and_check(&info, umod, len);
Rusty Russell40dd2562010-08-05 12:59:03 -06002524 if (err)
2525 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002527 mod = setup_load_info(&info);
2528 if (IS_ERR(mod)) {
2529 err = PTR_ERR(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 goto free_hdr;
2531 }
2532
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002533 err = check_modinfo(mod, info.sechdrs, info.index.info, info.index.vers);
Rusty Russell40dd2562010-08-05 12:59:03 -06002534 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 goto free_hdr;
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002536
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 /* Now copy in args */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002538 info.args = strndup_user(uargs, ~0UL >> 1);
2539 if (IS_ERR(info.args)) {
2540 err = PTR_ERR(info.args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 goto free_hdr;
2542 }
Andrew Morton8e08b752006-02-07 12:58:45 -08002543
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002544 strmap = kzalloc(BITS_TO_LONGS(info.sechdrs[info.index.str].sh_size)
Jan Beulich554bdfe2009-07-06 14:51:44 +01002545 * sizeof(long), GFP_KERNEL);
2546 if (!strmap) {
2547 err = -ENOMEM;
2548 goto free_mod;
2549 }
2550
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 mod->state = MODULE_STATE_COMING;
2552
2553 /* Allow arches to frob section contents and sizes. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002554 err = module_frob_arch_sections(info.hdr, info.sechdrs, info.secstrings, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 if (err < 0)
2556 goto free_mod;
2557
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002558 if (info.index.pcpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 /* We have a special allocation for this section. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002560 err = percpu_modalloc(mod, info.sechdrs[info.index.pcpu].sh_size,
2561 info.sechdrs[info.index.pcpu].sh_addralign);
Tejun Heo259354d2010-03-10 18:56:10 +09002562 if (err)
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002563 goto free_mod;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002564 info.sechdrs[info.index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 }
Rusty Russell293a7cf2010-05-31 19:53:50 +09302566 /* Keep this around for failure path. */
2567 percpu = mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
2569 /* Determine total sizes, and put offsets in sh_entsize. For now
2570 this is done generically; there doesn't appear to be any
2571 special cases for the architectures. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002572 layout_sections(mod, info.hdr, info.sechdrs, info.secstrings);
2573 symoffs = layout_symtab(mod, info.sechdrs, info.index.sym, info.index.str, info.hdr,
2574 info.secstrings, &stroffs, strmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002576 /* Allocate and move to the final place */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002577 mod = move_module(mod, info.hdr, info.sechdrs, info.secstrings, info.index.mod);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002578 if (IS_ERR(mod)) {
2579 err = PTR_ERR(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 goto free_percpu;
2581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
2583 /* Now we've moved module, initialize linked lists, etc. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -06002584 err = module_unload_init(mod);
2585 if (err)
2586 goto free_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
Rusty Russell22e268e2010-08-05 12:59:05 -06002588 /* Now we've got everything in the final locations, we can
2589 * find optional sections. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002590 find_module_sections(mod, info.hdr, info.sechdrs, info.secstrings);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002592 err = check_module_license_and_versions(mod, info.sechdrs);
Rusty Russell22e268e2010-08-05 12:59:05 -06002593 if (err)
2594 goto free_unload;
Dave Jones9841d61d2006-01-08 01:03:41 -08002595
Matt Domschc988d2b2005-06-23 22:05:15 -07002596 /* Set up MODINFO_ATTR fields */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002597 setup_modinfo(mod, info.sechdrs, info.index.info);
Matt Domschc988d2b2005-06-23 22:05:15 -07002598
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 /* Fix up syms, so that st_value is a pointer to location. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002600 err = simplify_symbols(info.sechdrs, info.index.sym, info.strtab, info.index.vers, info.index.pcpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 mod);
2602 if (err < 0)
2603 goto cleanup;
2604
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002605 err = apply_relocations(mod, info.hdr, info.sechdrs, info.index.sym, info.index.str);
Rusty Russell22e268e2010-08-05 12:59:05 -06002606 if (err < 0)
2607 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
2609 /* Set up and sort exception table */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002610 mod->extable = section_objs(info.hdr, info.sechdrs, info.secstrings, "__ex_table",
Rusty Russell5e458cc2008-10-22 10:00:13 -05002611 sizeof(*mod->extable), &mod->num_exentries);
2612 sort_extable(mod->extable, mod->extable + mod->num_exentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
2614 /* Finally, copy percpu area over. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002615 percpu_modcopy(mod, (void *)info.sechdrs[info.index.pcpu].sh_addr,
2616 info.sechdrs[info.index.pcpu].sh_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
Rusty Russelleded41c2010-08-05 12:59:07 -06002618 add_kallsyms(mod, &info, symoffs, stroffs, strmap);
Jan Beulich554bdfe2009-07-06 14:51:44 +01002619 kfree(strmap);
2620 strmap = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Yehuda Sadehff49d742010-07-03 13:07:35 +10002622 if (!mod->taints)
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002623 debug = section_objs(info.hdr, info.sechdrs, info.secstrings, "__verbose",
Rusty Russell5e458cc2008-10-22 10:00:13 -05002624 sizeof(*debug), &num_debug);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002625
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002626 err = module_finalize(info.hdr, info.sechdrs, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 if (err < 0)
2628 goto cleanup;
2629
Rusty Russell22e268e2010-08-05 12:59:05 -06002630 flush_module_icache(mod);
Thomas Koeller378bac82005-09-06 15:17:11 -07002631
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002632 mod->args = info.args;
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002633
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002634 /* Now sew it into the lists so we can get lockdep and oops
Andi Kleend72b3752008-08-30 10:09:00 +02002635 * info during argument parsing. Noone should access us, since
2636 * strong_try_module_get() will fail.
2637 * lockdep/oops can run asynchronous, so use the RCU list insertion
2638 * function to insert in a way safe to concurrent readers.
2639 * The mutex protects against concurrent writers.
2640 */
Rusty Russell75676502010-06-05 11:17:36 -06002641 mutex_lock(&module_mutex);
Linus Torvalds3bafeb62010-06-05 11:17:36 -06002642 if (find_module(mod->name)) {
2643 err = -EEXIST;
Rusty Russellbe593f42010-06-05 11:17:37 -06002644 goto unlock;
Linus Torvalds3bafeb62010-06-05 11:17:36 -06002645 }
2646
Yehuda Sadehff49d742010-07-03 13:07:35 +10002647 if (debug)
2648 dynamic_debug_setup(debug, num_debug);
2649
Rusty Russellbe593f42010-06-05 11:17:37 -06002650 /* Find duplicate symbols */
2651 err = verify_export_symbols(mod);
2652 if (err < 0)
Yehuda Sadehff49d742010-07-03 13:07:35 +10002653 goto ddebug;
Rusty Russellbe593f42010-06-05 11:17:37 -06002654
Andi Kleend72b3752008-08-30 10:09:00 +02002655 list_add_rcu(&mod->list, &modules);
Rusty Russell75676502010-06-05 11:17:36 -06002656 mutex_unlock(&module_mutex);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002657
Rusty Russelle180a6b2009-03-31 13:05:29 -06002658 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002660 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
Rusty Russelle180a6b2009-03-31 13:05:29 -06002662 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002664 goto unlink;
Rusty Russell80a3d1b2010-06-05 11:17:36 -06002665
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002666 add_sect_attrs(mod, info.hdr->e_shnum, info.secstrings, info.sechdrs);
2667 add_notes_attrs(mod, info.hdr->e_shnum, info.secstrings, info.sechdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
2669 /* Get rid of temporary copy */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002670 vfree(info.hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
Li Zefan7ead8b82009-08-17 16:56:28 +08002672 trace_module_load(mod);
2673
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 /* Done! */
2675 return mod;
2676
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002677 unlink:
Rusty Russell75676502010-06-05 11:17:36 -06002678 mutex_lock(&module_mutex);
Rusty Russelle91defa2009-03-31 13:05:35 -06002679 /* Unlink carefully: kallsyms could be walking list. */
2680 list_del_rcu(&mod->list);
Yehuda Sadehff49d742010-07-03 13:07:35 +10002681 ddebug:
2682 dynamic_debug_remove(debug);
Rusty Russellbe593f42010-06-05 11:17:37 -06002683 unlock:
Rusty Russell75676502010-06-05 11:17:36 -06002684 mutex_unlock(&module_mutex);
Rusty Russelle91defa2009-03-31 13:05:35 -06002685 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 module_arch_cleanup(mod);
2687 cleanup:
Rusty Russella263f772009-09-25 00:32:58 -06002688 free_modinfo(mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002689 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 module_unload_free(mod);
Rusty Russellffa9f122009-09-25 00:32:59 -06002691 free_init:
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002692 module_free(mod, mod->module_init);
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002693 module_free(mod, mod->module_core);
2694 /* mod will be freed with core. Don't access it beyond this line! */
2695 free_percpu:
Rusty Russell293a7cf2010-05-31 19:53:50 +09302696 free_percpu(percpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 free_mod:
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002698 kfree(info.args);
Jan Beulich554bdfe2009-07-06 14:51:44 +01002699 kfree(strmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 free_hdr:
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002701 vfree(info.hdr);
Jayachandran C6fe2e702006-01-06 00:19:54 -08002702 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703}
2704
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002705/* Call module constructors. */
2706static void do_mod_ctors(struct module *mod)
2707{
2708#ifdef CONFIG_CONSTRUCTORS
2709 unsigned long i;
2710
2711 for (i = 0; i < mod->num_ctors; i++)
2712 mod->ctors[i]();
2713#endif
2714}
2715
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716/* This is where the real work happens */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002717SYSCALL_DEFINE3(init_module, void __user *, umod,
2718 unsigned long, len, const char __user *, uargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719{
2720 struct module *mod;
2721 int ret = 0;
2722
2723 /* Must have permission */
Kees Cook3d433212009-04-02 15:49:29 -07002724 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 return -EPERM;
2726
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 /* Do all the hard work */
2728 mod = load_module(umod, len, uargs);
Rusty Russell75676502010-06-05 11:17:36 -06002729 if (IS_ERR(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 return PTR_ERR(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
Alan Sterne041c682006-03-27 01:16:30 -08002732 blocking_notifier_call_chain(&module_notify_list,
2733 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002735 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 /* Start the module */
2737 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002738 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 if (ret < 0) {
2740 /* Init routine failed: abort. Try to protect us from
2741 buggy refcounters. */
2742 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002743 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002744 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002745 blocking_notifier_call_chain(&module_notify_list,
2746 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002747 free_module(mod);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002748 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 return ret;
2750 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002751 if (ret > 0) {
Joe Perchesad361c92009-07-06 13:05:40 -07002752 printk(KERN_WARNING
2753"%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
2754"%s: loading module anyway...\n",
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002755 __func__, mod->name, ret,
2756 __func__);
2757 dump_stack();
2758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
Rusty Russell6c5db222008-03-10 11:43:52 -07002760 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002762 wake_up(&module_wq);
Masami Hiramatsu0deddf432009-01-06 14:41:54 -08002763 blocking_notifier_call_chain(&module_notify_list,
2764 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07002765
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07002766 /* We need to finish all async code before the module init sequence is done */
2767 async_synchronize_full();
2768
Rusty Russell6c5db222008-03-10 11:43:52 -07002769 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 /* Drop initial reference. */
2771 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06002772 trim_init_extable(mod);
Jan Beulich4a496222009-07-06 14:50:42 +01002773#ifdef CONFIG_KALLSYMS
2774 mod->num_symtab = mod->core_num_syms;
2775 mod->symtab = mod->core_symtab;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002776 mod->strtab = mod->core_strtab;
Jan Beulich4a496222009-07-06 14:50:42 +01002777#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 module_free(mod, mod->module_init);
2779 mod->module_init = NULL;
2780 mod->init_size = 0;
2781 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002782 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
2784 return 0;
2785}
2786
2787static inline int within(unsigned long addr, void *start, unsigned long size)
2788{
2789 return ((void *)addr >= start && (void *)addr < start + size);
2790}
2791
2792#ifdef CONFIG_KALLSYMS
2793/*
2794 * This ignores the intensely annoying "mapping symbols" found
2795 * in ARM ELF files: $a, $t and $d.
2796 */
2797static inline int is_arm_mapping_symbol(const char *str)
2798{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002799 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 && (str[2] == '\0' || str[2] == '.');
2801}
2802
2803static const char *get_ksymbol(struct module *mod,
2804 unsigned long addr,
2805 unsigned long *size,
2806 unsigned long *offset)
2807{
2808 unsigned int i, best = 0;
2809 unsigned long nextval;
2810
2811 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002812 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002814 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2816
2817 /* Scan for closest preceeding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002818 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 for (i = 1; i < mod->num_symtab; i++) {
2820 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2821 continue;
2822
2823 /* We ignore unnamed symbols: they're uninformative
2824 * and inserted at a whim. */
2825 if (mod->symtab[i].st_value <= addr
2826 && mod->symtab[i].st_value > mod->symtab[best].st_value
2827 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2828 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2829 best = i;
2830 if (mod->symtab[i].st_value > addr
2831 && mod->symtab[i].st_value < nextval
2832 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2833 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2834 nextval = mod->symtab[i].st_value;
2835 }
2836
2837 if (!best)
2838 return NULL;
2839
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002840 if (size)
2841 *size = nextval - mod->symtab[best].st_value;
2842 if (offset)
2843 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 return mod->strtab + mod->symtab[best].st_name;
2845}
2846
Rusty Russell6dd06c92008-01-29 17:13:22 -05002847/* For kallsyms to ask for address resolution. NULL means not found. Careful
2848 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002849const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05002850 unsigned long *size,
2851 unsigned long *offset,
2852 char **modname,
2853 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854{
2855 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002856 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
Rusty Russellcb2a5202008-01-14 00:55:03 -08002858 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002859 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002860 if (within_module_init(addr, mod) ||
2861 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002862 if (modname)
2863 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002864 ret = get_ksymbol(mod, addr, size, offset);
2865 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 }
2867 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05002868 /* Make a copy in here where it's safe */
2869 if (ret) {
2870 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2871 ret = namebuf;
2872 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002873 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002874 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875}
2876
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002877int lookup_module_symbol_name(unsigned long addr, char *symname)
2878{
2879 struct module *mod;
2880
Rusty Russellcb2a5202008-01-14 00:55:03 -08002881 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002882 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002883 if (within_module_init(addr, mod) ||
2884 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002885 const char *sym;
2886
2887 sym = get_ksymbol(mod, addr, NULL, NULL);
2888 if (!sym)
2889 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07002890 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002891 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002892 return 0;
2893 }
2894 }
2895out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002896 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002897 return -ERANGE;
2898}
2899
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002900int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2901 unsigned long *offset, char *modname, char *name)
2902{
2903 struct module *mod;
2904
Rusty Russellcb2a5202008-01-14 00:55:03 -08002905 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002906 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002907 if (within_module_init(addr, mod) ||
2908 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002909 const char *sym;
2910
2911 sym = get_ksymbol(mod, addr, size, offset);
2912 if (!sym)
2913 goto out;
2914 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07002915 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002916 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07002917 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002918 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002919 return 0;
2920 }
2921 }
2922out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002923 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002924 return -ERANGE;
2925}
2926
Alexey Dobriyanea078902007-05-08 00:28:39 -07002927int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2928 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929{
2930 struct module *mod;
2931
Rusty Russellcb2a5202008-01-14 00:55:03 -08002932 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002933 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 if (symnum < mod->num_symtab) {
2935 *value = mod->symtab[symnum].st_value;
2936 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002937 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07002938 KSYM_NAME_LEN);
2939 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06002940 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002941 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002942 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 }
2944 symnum -= mod->num_symtab;
2945 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002946 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002947 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948}
2949
2950static unsigned long mod_find_symname(struct module *mod, const char *name)
2951{
2952 unsigned int i;
2953
2954 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002955 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2956 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 return mod->symtab[i].st_value;
2958 return 0;
2959}
2960
2961/* Look for this name: can be of form module:name. */
2962unsigned long module_kallsyms_lookup_name(const char *name)
2963{
2964 struct module *mod;
2965 char *colon;
2966 unsigned long ret = 0;
2967
2968 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08002969 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 if ((colon = strchr(name, ':')) != NULL) {
2971 *colon = '\0';
2972 if ((mod = find_module(name)) != NULL)
2973 ret = mod_find_symname(mod, colon+1);
2974 *colon = ':';
2975 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02002976 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 if ((ret = mod_find_symname(mod, name)) != 0)
2978 break;
2979 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002980 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 return ret;
2982}
Anders Kaseorg75a66612008-12-05 19:03:58 -05002983
2984int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
2985 struct module *, unsigned long),
2986 void *data)
2987{
2988 struct module *mod;
2989 unsigned int i;
2990 int ret;
2991
2992 list_for_each_entry(mod, &modules, list) {
2993 for (i = 0; i < mod->num_symtab; i++) {
2994 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
2995 mod, mod->symtab[i].st_value);
2996 if (ret != 0)
2997 return ret;
2998 }
2999 }
3000 return 0;
3001}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002#endif /* CONFIG_KALLSYMS */
3003
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003004static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003005{
3006 int bx = 0;
3007
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003008 if (mod->taints ||
3009 mod->state == MODULE_STATE_GOING ||
3010 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003011 buf[bx++] = '(';
Andi Kleen25ddbb12008-10-15 22:01:41 -07003012 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003013 buf[bx++] = 'P';
Andi Kleen25ddbb12008-10-15 22:01:41 -07003014 if (mod->taints & (1 << TAINT_FORCED_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003015 buf[bx++] = 'F';
Linus Torvalds26e9a392008-10-17 09:50:12 -07003016 if (mod->taints & (1 << TAINT_CRAP))
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07003017 buf[bx++] = 'C';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003018 /*
3019 * TAINT_FORCED_RMMOD: could be added.
3020 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
3021 * apply to modules.
3022 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003023
3024 /* Show a - for module-is-being-unloaded */
3025 if (mod->state == MODULE_STATE_GOING)
3026 buf[bx++] = '-';
3027 /* Show a + for module-is-being-loaded */
3028 if (mod->state == MODULE_STATE_COMING)
3029 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003030 buf[bx++] = ')';
3031 }
3032 buf[bx] = '\0';
3033
3034 return buf;
3035}
3036
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003037#ifdef CONFIG_PROC_FS
3038/* Called by the /proc file system to return a list of modules. */
3039static void *m_start(struct seq_file *m, loff_t *pos)
3040{
3041 mutex_lock(&module_mutex);
3042 return seq_list_start(&modules, *pos);
3043}
3044
3045static void *m_next(struct seq_file *m, void *p, loff_t *pos)
3046{
3047 return seq_list_next(p, &modules, pos);
3048}
3049
3050static void m_stop(struct seq_file *m, void *p)
3051{
3052 mutex_unlock(&module_mutex);
3053}
3054
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055static int m_show(struct seq_file *m, void *p)
3056{
3057 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003058 char buf[8];
3059
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05003060 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 mod->name, mod->init_size + mod->core_size);
3062 print_unload_info(m, mod);
3063
3064 /* Informative for users. */
3065 seq_printf(m, " %s",
3066 mod->state == MODULE_STATE_GOING ? "Unloading":
3067 mod->state == MODULE_STATE_COMING ? "Loading":
3068 "Live");
3069 /* Used by oprofile and other similar tools. */
3070 seq_printf(m, " 0x%p", mod->module_core);
3071
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003072 /* Taints info */
3073 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003074 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003075
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 seq_printf(m, "\n");
3077 return 0;
3078}
3079
3080/* Format: modulename size refcount deps address
3081
3082 Where refcount is a number or -, and deps is a comma-separated list
3083 of depends or -.
3084*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003085static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 .start = m_start,
3087 .next = m_next,
3088 .stop = m_stop,
3089 .show = m_show
3090};
3091
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003092static int modules_open(struct inode *inode, struct file *file)
3093{
3094 return seq_open(file, &modules_op);
3095}
3096
3097static const struct file_operations proc_modules_operations = {
3098 .open = modules_open,
3099 .read = seq_read,
3100 .llseek = seq_lseek,
3101 .release = seq_release,
3102};
3103
3104static int __init proc_modules_init(void)
3105{
3106 proc_create("modules", 0, NULL, &proc_modules_operations);
3107 return 0;
3108}
3109module_init(proc_modules_init);
3110#endif
3111
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112/* Given an address, look for it in the module exception tables. */
3113const struct exception_table_entry *search_module_extables(unsigned long addr)
3114{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 const struct exception_table_entry *e = NULL;
3116 struct module *mod;
3117
Rusty Russell24da1cb2007-07-15 23:41:46 -07003118 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003119 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 if (mod->num_exentries == 0)
3121 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003122
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 e = search_extable(mod->extable,
3124 mod->extable + mod->num_exentries - 1,
3125 addr);
3126 if (e)
3127 break;
3128 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07003129 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
3131 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07003132 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 return e;
3134}
3135
Ingo Molnar4d435f92006-07-03 00:24:24 -07003136/*
Rusty Russelle6104992009-03-31 13:05:31 -06003137 * is_module_address - is this address inside a module?
3138 * @addr: the address to check.
3139 *
3140 * See is_module_text_address() if you simply want to see if the address
3141 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07003142 */
Rusty Russelle6104992009-03-31 13:05:31 -06003143bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07003144{
Rusty Russelle6104992009-03-31 13:05:31 -06003145 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003146
Rusty Russell24da1cb2007-07-15 23:41:46 -07003147 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06003148 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07003149 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07003150
Rusty Russelle6104992009-03-31 13:05:31 -06003151 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003152}
3153
Rusty Russelle6104992009-03-31 13:05:31 -06003154/*
3155 * __module_address - get the module which contains an address.
3156 * @addr: the address.
3157 *
3158 * Must be called with preempt disabled or module mutex held so that
3159 * module doesn't get freed during this.
3160 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07003161struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162{
3163 struct module *mod;
3164
Rusty Russell3a642e92008-07-22 19:24:28 -05003165 if (addr < module_addr_min || addr > module_addr_max)
3166 return NULL;
3167
Andi Kleend72b3752008-08-30 10:09:00 +02003168 list_for_each_entry_rcu(mod, &modules, list)
Rusty Russelle6104992009-03-31 13:05:31 -06003169 if (within_module_core(addr, mod)
3170 || within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 return mod;
3172 return NULL;
3173}
Tim Abbottc6b37802008-12-05 19:03:59 -05003174EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
Rusty Russelle6104992009-03-31 13:05:31 -06003176/*
3177 * is_module_text_address - is this address inside module code?
3178 * @addr: the address to check.
3179 *
3180 * See is_module_address() if you simply want to see if the address is
3181 * anywhere in a module. See kernel_text_address() for testing if an
3182 * address corresponds to kernel or module code.
3183 */
3184bool is_module_text_address(unsigned long addr)
3185{
3186 bool ret;
3187
3188 preempt_disable();
3189 ret = __module_text_address(addr) != NULL;
3190 preempt_enable();
3191
3192 return ret;
3193}
3194
3195/*
3196 * __module_text_address - get the module whose code contains an address.
3197 * @addr: the address.
3198 *
3199 * Must be called with preempt disabled or module mutex held so that
3200 * module doesn't get freed during this.
3201 */
3202struct module *__module_text_address(unsigned long addr)
3203{
3204 struct module *mod = __module_address(addr);
3205 if (mod) {
3206 /* Make sure it's within the text section. */
3207 if (!within(addr, mod->module_init, mod->init_text_size)
3208 && !within(addr, mod->module_core, mod->core_text_size))
3209 mod = NULL;
3210 }
3211 return mod;
3212}
Tim Abbottc6b37802008-12-05 19:03:59 -05003213EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06003214
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215/* Don't grab lock, we're oopsing. */
3216void print_modules(void)
3217{
3218 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07003219 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
Linus Torvaldsb2311252009-06-16 11:07:14 -07003221 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02003222 /* Most callers should already have preempt disabled, but make sure */
3223 preempt_disable();
3224 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003225 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02003226 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01003227 if (last_unloaded_module[0])
3228 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 printk("\n");
3230}
3231
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06003233/* Generate the signature for all relevant module structures here.
3234 * If these change, we don't want to try to parse the module. */
3235void module_layout(struct module *mod,
3236 struct modversion_info *ver,
3237 struct kernel_param *kp,
3238 struct kernel_symbol *ks,
Rusty Russell8c8ef422009-03-31 13:05:34 -06003239 struct tracepoint *tp)
3240{
3241}
3242EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07003244
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04003245#ifdef CONFIG_TRACEPOINTS
3246void module_update_tracepoints(void)
3247{
3248 struct module *mod;
3249
3250 mutex_lock(&module_mutex);
3251 list_for_each_entry(mod, &modules, list)
3252 if (!mod->taints)
3253 tracepoint_update_probe_range(mod->tracepoints,
3254 mod->tracepoints + mod->num_tracepoints);
3255 mutex_unlock(&module_mutex);
3256}
3257
3258/*
3259 * Returns 0 if current not found.
3260 * Returns 1 if current found.
3261 */
3262int module_get_iter_tracepoints(struct tracepoint_iter *iter)
3263{
3264 struct module *iter_mod;
3265 int found = 0;
3266
3267 mutex_lock(&module_mutex);
3268 list_for_each_entry(iter_mod, &modules, list) {
3269 if (!iter_mod->taints) {
3270 /*
3271 * Sorted module list
3272 */
3273 if (iter_mod < iter->module)
3274 continue;
3275 else if (iter_mod > iter->module)
3276 iter->tracepoint = NULL;
3277 found = tracepoint_get_iter_range(&iter->tracepoint,
3278 iter_mod->tracepoints,
3279 iter_mod->tracepoints
3280 + iter_mod->num_tracepoints);
3281 if (found) {
3282 iter->module = iter_mod;
3283 break;
3284 }
3285 }
3286 }
3287 mutex_unlock(&module_mutex);
3288 return found;
3289}
3290#endif