blob: 79c4d6f69dd7edab4ad026416354d85093bcc618 [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-Hartman03e88ae2006-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-Hartman03e88ae2006-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-Hartman03e88ae2006-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-Hartman03e88ae2006-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-Hartman03e88ae2006-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-Hartman03e88ae2006-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-Hartman03e88ae2006-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-Hartman03e88ae2006-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-Hartman03e88ae2006-02-16 13:50:23 -08001414 if (attr->free)
1415 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001416 }
Greg Kroah-Hartman03e88ae2006-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 Baronb82bab42010-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 Molnarfbb9ce92006-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);
2048 mod->strtab = info->strtab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
2050 /* Set types up while we still have access to sections. */
2051 for (i = 0; i < mod->num_symtab; i++)
Rusty Russelleded41c2010-08-05 12:59:07 -06002052 mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
Jan Beulich4a496222009-07-06 14:50:42 +01002053
2054 mod->core_symtab = dst = mod->module_core + symoffs;
2055 src = mod->symtab;
2056 *dst = *src;
2057 for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
Rusty Russelleded41c2010-08-05 12:59:07 -06002058 if (!is_core_symbol(src, info->sechdrs, info->hdr->e_shnum))
Jan Beulich4a496222009-07-06 14:50:42 +01002059 continue;
2060 dst[ndst] = *src;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002061 dst[ndst].st_name = bitmap_weight(strmap, dst[ndst].st_name);
Jan Beulich4a496222009-07-06 14:50:42 +01002062 ++ndst;
2063 }
2064 mod->core_num_syms = ndst;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002065
2066 mod->core_strtab = s = mod->module_core + stroffs;
Rusty Russelleded41c2010-08-05 12:59:07 -06002067 for (*s = 0, i = 1; i < info->sechdrs[info->index.str].sh_size; ++i)
Jan Beulich554bdfe2009-07-06 14:51:44 +01002068 if (test_bit(i, strmap))
2069 *++s = mod->strtab[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070}
2071#else
Jan Beulich4a496222009-07-06 14:50:42 +01002072static inline unsigned long layout_symtab(struct module *mod,
2073 Elf_Shdr *sechdrs,
2074 unsigned int symindex,
Jan Beulich554bdfe2009-07-06 14:51:44 +01002075 unsigned int strindex,
Paul Mundt3ae91c22009-10-01 15:43:54 -07002076 const Elf_Ehdr *hdr,
Jan Beulich554bdfe2009-07-06 14:51:44 +01002077 const char *secstrings,
2078 unsigned long *pstroffs,
2079 unsigned long *strmap)
Jan Beulich4a496222009-07-06 14:50:42 +01002080{
Paul Mundt3ae91c22009-10-01 15:43:54 -07002081 return 0;
Jan Beulich4a496222009-07-06 14:50:42 +01002082}
Paul Mundt3ae91c22009-10-01 15:43:54 -07002083
Rusty Russelleded41c2010-08-05 12:59:07 -06002084static void add_kallsyms(struct module *mod, struct load_info *info,
2085 unsigned long symoffs,
2086 unsigned long stroffs,
2087 unsigned long *strmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
2089}
2090#endif /* CONFIG_KALLSYMS */
2091
Jason Barone9d376f2009-02-05 11:51:38 -05002092static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05002093{
Jason Barone9d376f2009-02-05 11:51:38 -05002094#ifdef CONFIG_DYNAMIC_DEBUG
2095 if (ddebug_add_module(debug, num, debug->modname))
2096 printk(KERN_ERR "dynamic debug error adding module: %s\n",
2097 debug->modname);
2098#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002099}
Jason Baron346e15b2008-08-12 16:46:19 -04002100
Yehuda Sadehff49d742010-07-03 13:07:35 +10002101static void dynamic_debug_remove(struct _ddebug *debug)
2102{
2103 if (debug)
2104 ddebug_remove_module(debug->modname);
2105}
2106
Rusty Russell3a642e92008-07-22 19:24:28 -05002107static void *module_alloc_update_bounds(unsigned long size)
2108{
2109 void *ret = module_alloc(size);
2110
2111 if (ret) {
Rusty Russell75676502010-06-05 11:17:36 -06002112 mutex_lock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002113 /* Update module bounds. */
2114 if ((unsigned long)ret < module_addr_min)
2115 module_addr_min = (unsigned long)ret;
2116 if ((unsigned long)ret + size > module_addr_max)
2117 module_addr_max = (unsigned long)ret + size;
Rusty Russell75676502010-06-05 11:17:36 -06002118 mutex_unlock(&module_mutex);
Rusty Russell3a642e92008-07-22 19:24:28 -05002119 }
2120 return ret;
2121}
2122
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002123#ifdef CONFIG_DEBUG_KMEMLEAK
2124static void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002125 const Elf_Shdr *sechdrs,
2126 const char *secstrings)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002127{
2128 unsigned int i;
2129
2130 /* only scan the sections containing data */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002131 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002132
2133 for (i = 1; i < hdr->e_shnum; i++) {
2134 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2135 continue;
2136 if (strncmp(secstrings + sechdrs[i].sh_name, ".data", 5) != 0
2137 && strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) != 0)
2138 continue;
2139
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002140 kmemleak_scan_area((void *)sechdrs[i].sh_addr,
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002141 sechdrs[i].sh_size, GFP_KERNEL);
2142 }
2143}
2144#else
2145static inline void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002146 Elf_Shdr *sechdrs,
2147 const char *secstrings)
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002148{
2149}
2150#endif
2151
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002152/* Sets info->hdr and info->len. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002153static int copy_and_check(struct load_info *info, const void __user *umod, unsigned long len)
Rusty Russell40dd2562010-08-05 12:59:03 -06002154{
2155 int err;
2156 Elf_Ehdr *hdr;
2157
2158 if (len < sizeof(*hdr))
2159 return -ENOEXEC;
2160
2161 /* Suck in entire file: we'll want most of it. */
2162 /* vmalloc barfs on "unusual" numbers. Check here */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002163 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
Rusty Russell40dd2562010-08-05 12:59:03 -06002164 return -ENOMEM;
2165
2166 if (copy_from_user(hdr, umod, len) != 0) {
2167 err = -EFAULT;
2168 goto free_hdr;
2169 }
2170
2171 /* Sanity checks against insmoding binaries or wrong arch,
2172 weird elf version */
2173 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
2174 || hdr->e_type != ET_REL
2175 || !elf_check_arch(hdr)
2176 || hdr->e_shentsize != sizeof(Elf_Shdr)) {
2177 err = -ENOEXEC;
2178 goto free_hdr;
2179 }
2180
2181 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) {
2182 err = -ENOEXEC;
2183 goto free_hdr;
2184 }
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002185 info->hdr = hdr;
2186 info->len = len;
Rusty Russell40dd2562010-08-05 12:59:03 -06002187 return 0;
2188
2189free_hdr:
2190 vfree(hdr);
2191 return err;
2192}
2193
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002194static int rewrite_section_headers(struct load_info *info)
2195{
2196 unsigned int i;
2197
2198 /* This should always be true, but let's be sure. */
2199 info->sechdrs[0].sh_addr = 0;
2200
2201 for (i = 1; i < info->hdr->e_shnum; i++) {
2202 Elf_Shdr *shdr = &info->sechdrs[i];
2203 if (shdr->sh_type != SHT_NOBITS
2204 && info->len < shdr->sh_offset + shdr->sh_size) {
2205 printk(KERN_ERR "Module len %lu truncated\n",
2206 info->len);
2207 return -ENOEXEC;
2208 }
2209
2210 /* Mark all sections sh_addr with their address in the
2211 temporary image. */
2212 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2213
2214#ifndef CONFIG_MODULE_UNLOAD
2215 /* Don't load .exit sections */
2216 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2217 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2218#endif
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002219 }
Rusty Russelld6df72a2010-08-05 12:59:07 -06002220
2221 /* Track but don't keep modinfo and version sections. */
2222 info->index.vers = find_sec(info->hdr, info->sechdrs, info->secstrings, "__versions");
2223 info->index.info = find_sec(info->hdr, info->sechdrs, info->secstrings, ".modinfo");
2224 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2225 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002226 return 0;
2227}
2228
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002229/*
2230 * Set up our basic convenience variables (pointers to section headers,
2231 * search for module section index etc), and do some basic section
2232 * verification.
2233 *
2234 * Return the temporary module pointer (we'll replace it with the final
2235 * one when we move the module sections around).
2236 */
2237static struct module *setup_load_info(struct load_info *info)
2238{
2239 unsigned int i;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002240 int err;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002241 struct module *mod;
2242
2243 /* Set up the convenience variables */
2244 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002245 info->secstrings = (void *)info->hdr
2246 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002247
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002248 err = rewrite_section_headers(info);
2249 if (err)
2250 return ERR_PTR(err);
2251
2252 /* Find internal symbols and strings. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002253 for (i = 1; i < info->hdr->e_shnum; i++) {
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002254 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2255 info->index.sym = i;
2256 info->index.str = info->sechdrs[i].sh_link;
Rusty Russell8b5f61a2010-08-05 12:59:06 -06002257 info->strtab = (char *)info->hdr
2258 + info->sechdrs[info->index.str].sh_offset;
2259 break;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002260 }
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002261 }
2262
2263 info->index.mod = find_sec(info->hdr, info->sechdrs, info->secstrings,
2264 ".gnu.linkonce.this_module");
2265 if (!info->index.mod) {
2266 printk(KERN_WARNING "No module found in object\n");
2267 return ERR_PTR(-ENOEXEC);
2268 }
2269 /* This is temporary: point mod into copy of data. */
2270 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2271
2272 if (info->index.sym == 0) {
2273 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2274 mod->name);
2275 return ERR_PTR(-ENOEXEC);
2276 }
2277
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002278 info->index.pcpu = find_pcpusec(info->hdr, info->sechdrs, info->secstrings);
2279
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002280 /* Check module struct version now, before we try to use module. */
2281 if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2282 return ERR_PTR(-ENOEXEC);
2283
2284 return mod;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002285}
2286
Rusty Russell40dd2562010-08-05 12:59:03 -06002287static int check_modinfo(struct module *mod,
2288 const Elf_Shdr *sechdrs,
2289 unsigned int infoindex, unsigned int versindex)
2290{
2291 const char *modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
2292 int err;
2293
2294 /* This is allowed: modprobe --force will invalidate it. */
2295 if (!modmagic) {
2296 err = try_to_force_load(mod, "bad vermagic");
2297 if (err)
2298 return err;
2299 } else if (!same_magic(modmagic, vermagic, versindex)) {
2300 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2301 mod->name, modmagic, vermagic);
2302 return -ENOEXEC;
2303 }
2304
2305 if (get_modinfo(sechdrs, infoindex, "staging")) {
2306 add_taint_module(mod, TAINT_CRAP);
2307 printk(KERN_WARNING "%s: module is from the staging directory,"
2308 " the quality is unknown, you have been warned.\n",
2309 mod->name);
2310 }
Rusty Russell22e268e2010-08-05 12:59:05 -06002311
2312 /* Set up license info based on the info section */
2313 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2314
Rusty Russell40dd2562010-08-05 12:59:03 -06002315 return 0;
2316}
2317
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002318static void find_module_sections(struct module *mod, Elf_Ehdr *hdr,
2319 Elf_Shdr *sechdrs, const char *secstrings)
2320{
2321 mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
2322 sizeof(*mod->kp), &mod->num_kp);
2323 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2324 sizeof(*mod->syms), &mod->num_syms);
2325 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2326 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2327 sizeof(*mod->gpl_syms),
2328 &mod->num_gpl_syms);
2329 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2330 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2331 "__ksymtab_gpl_future",
2332 sizeof(*mod->gpl_future_syms),
2333 &mod->num_gpl_future_syms);
2334 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2335 "__kcrctab_gpl_future");
2336
2337#ifdef CONFIG_UNUSED_SYMBOLS
2338 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2339 "__ksymtab_unused",
2340 sizeof(*mod->unused_syms),
2341 &mod->num_unused_syms);
2342 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2343 "__kcrctab_unused");
2344 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2345 "__ksymtab_unused_gpl",
2346 sizeof(*mod->unused_gpl_syms),
2347 &mod->num_unused_gpl_syms);
2348 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2349 "__kcrctab_unused_gpl");
2350#endif
2351#ifdef CONFIG_CONSTRUCTORS
2352 mod->ctors = section_objs(hdr, sechdrs, secstrings, ".ctors",
2353 sizeof(*mod->ctors), &mod->num_ctors);
2354#endif
2355
2356#ifdef CONFIG_TRACEPOINTS
2357 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2358 "__tracepoints",
2359 sizeof(*mod->tracepoints),
2360 &mod->num_tracepoints);
2361#endif
2362#ifdef CONFIG_EVENT_TRACING
2363 mod->trace_events = section_objs(hdr, sechdrs, secstrings,
2364 "_ftrace_events",
2365 sizeof(*mod->trace_events),
2366 &mod->num_trace_events);
2367 /*
2368 * This section contains pointers to allocated objects in the trace
2369 * code and not scanning it leads to false positives.
2370 */
2371 kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2372 mod->num_trace_events, GFP_KERNEL);
2373#endif
2374#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2375 /* sechdrs[0].sh_size is always zero */
2376 mod->ftrace_callsites = section_objs(hdr, sechdrs, secstrings,
2377 "__mcount_loc",
2378 sizeof(*mod->ftrace_callsites),
2379 &mod->num_ftrace_callsites);
2380#endif
Rusty Russell22e268e2010-08-05 12:59:05 -06002381
2382 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
2383 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2384 mod->name);
Linus Torvaldsf91a13b2010-08-05 12:59:02 -06002385}
2386
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002387static struct module *move_module(struct module *mod,
2388 Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
2389 const char *secstrings, unsigned modindex)
2390{
2391 int i;
2392 void *ptr;
2393
2394 /* Do the allocs. */
2395 ptr = module_alloc_update_bounds(mod->core_size);
2396 /*
2397 * The pointer to this block is stored in the module structure
2398 * which is inside the block. Just mark it as not being a
2399 * leak.
2400 */
2401 kmemleak_not_leak(ptr);
2402 if (!ptr)
2403 return ERR_PTR(-ENOMEM);
2404
2405 memset(ptr, 0, mod->core_size);
2406 mod->module_core = ptr;
2407
2408 ptr = module_alloc_update_bounds(mod->init_size);
2409 /*
2410 * The pointer to this block is stored in the module structure
2411 * which is inside the block. This block doesn't need to be
2412 * scanned as it contains data and code that will be freed
2413 * after the module is initialized.
2414 */
2415 kmemleak_ignore(ptr);
2416 if (!ptr && mod->init_size) {
2417 module_free(mod, mod->module_core);
2418 return ERR_PTR(-ENOMEM);
2419 }
2420 memset(ptr, 0, mod->init_size);
2421 mod->module_init = ptr;
2422
2423 /* Transfer each section which specifies SHF_ALLOC */
2424 DEBUGP("final section addresses:\n");
2425 for (i = 0; i < hdr->e_shnum; i++) {
2426 void *dest;
2427
2428 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2429 continue;
2430
2431 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2432 dest = mod->module_init
2433 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2434 else
2435 dest = mod->module_core + sechdrs[i].sh_entsize;
2436
2437 if (sechdrs[i].sh_type != SHT_NOBITS)
2438 memcpy(dest, (void *)sechdrs[i].sh_addr,
2439 sechdrs[i].sh_size);
2440 /* Update sh_addr to point to copy in image. */
2441 sechdrs[i].sh_addr = (unsigned long)dest;
2442 DEBUGP("\t0x%lx %s\n",
2443 sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2444 }
2445 /* Module has been moved. */
2446 mod = (void *)sechdrs[modindex].sh_addr;
2447 kmemleak_load_module(mod, hdr, sechdrs, secstrings);
2448 return mod;
2449}
2450
Rusty Russell22e268e2010-08-05 12:59:05 -06002451static int check_module_license_and_versions(struct module *mod,
2452 Elf_Shdr *sechdrs)
2453{
2454 /*
2455 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2456 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2457 * using GPL-only symbols it needs.
2458 */
2459 if (strcmp(mod->name, "ndiswrapper") == 0)
2460 add_taint(TAINT_PROPRIETARY_MODULE);
2461
2462 /* driverloader was caught wrongly pretending to be under GPL */
2463 if (strcmp(mod->name, "driverloader") == 0)
2464 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2465
2466#ifdef CONFIG_MODVERSIONS
2467 if ((mod->num_syms && !mod->crcs)
2468 || (mod->num_gpl_syms && !mod->gpl_crcs)
2469 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2470#ifdef CONFIG_UNUSED_SYMBOLS
2471 || (mod->num_unused_syms && !mod->unused_crcs)
2472 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2473#endif
2474 ) {
2475 return try_to_force_load(mod,
2476 "no versions for exported symbols");
2477 }
2478#endif
2479 return 0;
2480}
2481
2482static void flush_module_icache(const struct module *mod)
2483{
2484 mm_segment_t old_fs;
2485
2486 /* flush the icache in correct context */
2487 old_fs = get_fs();
2488 set_fs(KERNEL_DS);
2489
2490 /*
2491 * Flush the instruction cache, since we've played with text.
2492 * Do it before processing of module parameters, so the module
2493 * can provide parameter accessor functions of its own.
2494 */
2495 if (mod->module_init)
2496 flush_icache_range((unsigned long)mod->module_init,
2497 (unsigned long)mod->module_init
2498 + mod->init_size);
2499 flush_icache_range((unsigned long)mod->module_core,
2500 (unsigned long)mod->module_core + mod->core_size);
2501
2502 set_fs(old_fs);
2503}
2504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505/* Allocate and load the module: note that size of section 0 is always
2506 zero, and we rely on this for optional sections. */
Linus Torvaldsffb4ba72008-08-25 11:10:26 -07002507static noinline struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 unsigned long len,
2509 const char __user *uargs)
2510{
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002511 struct load_info info = { NULL, };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 struct module *mod;
Rusty Russell40dd2562010-08-05 12:59:03 -06002513 long err;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002514 unsigned long symoffs, stroffs, *strmap;
Rusty Russell293a7cf2010-05-31 19:53:50 +09302515 void __percpu *percpu;
Yehuda Sadehff49d742010-07-03 13:07:35 +10002516 struct _ddebug *debug = NULL;
2517 unsigned int num_debug = 0;
Paul Mundt3ae91c22009-10-01 15:43:54 -07002518
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
2520 umod, len, uargs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002522 err = copy_and_check(&info, umod, len);
Rusty Russell40dd2562010-08-05 12:59:03 -06002523 if (err)
2524 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002526 mod = setup_load_info(&info);
2527 if (IS_ERR(mod)) {
2528 err = PTR_ERR(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 goto free_hdr;
2530 }
2531
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002532 err = check_modinfo(mod, info.sechdrs, info.index.info, info.index.vers);
Rusty Russell40dd2562010-08-05 12:59:03 -06002533 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 goto free_hdr;
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 /* Now copy in args */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002537 info.args = strndup_user(uargs, ~0UL >> 1);
2538 if (IS_ERR(info.args)) {
2539 err = PTR_ERR(info.args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 goto free_hdr;
2541 }
Andrew Morton8e08b752006-02-07 12:58:45 -08002542
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002543 strmap = kzalloc(BITS_TO_LONGS(info.sechdrs[info.index.str].sh_size)
Jan Beulich554bdfe2009-07-06 14:51:44 +01002544 * sizeof(long), GFP_KERNEL);
2545 if (!strmap) {
2546 err = -ENOMEM;
2547 goto free_mod;
2548 }
2549
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 mod->state = MODULE_STATE_COMING;
2551
2552 /* Allow arches to frob section contents and sizes. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002553 err = module_frob_arch_sections(info.hdr, info.sechdrs, info.secstrings, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 if (err < 0)
2555 goto free_mod;
2556
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002557 if (info.index.pcpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 /* We have a special allocation for this section. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002559 err = percpu_modalloc(mod, info.sechdrs[info.index.pcpu].sh_size,
2560 info.sechdrs[info.index.pcpu].sh_addralign);
Tejun Heo259354d2010-03-10 18:56:10 +09002561 if (err)
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002562 goto free_mod;
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002563 info.sechdrs[info.index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 }
Rusty Russell293a7cf2010-05-31 19:53:50 +09302565 /* Keep this around for failure path. */
2566 percpu = mod_percpu(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
2568 /* Determine total sizes, and put offsets in sh_entsize. For now
2569 this is done generically; there doesn't appear to be any
2570 special cases for the architectures. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002571 layout_sections(mod, info.hdr, info.sechdrs, info.secstrings);
2572 symoffs = layout_symtab(mod, info.sechdrs, info.index.sym, info.index.str, info.hdr,
2573 info.secstrings, &stroffs, strmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002575 /* Allocate and move to the final place */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002576 mod = move_module(mod, info.hdr, info.sechdrs, info.secstrings, info.index.mod);
Linus Torvalds65b8a9b2010-08-05 12:59:02 -06002577 if (IS_ERR(mod)) {
2578 err = PTR_ERR(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 goto free_percpu;
2580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
2582 /* Now we've moved module, initialize linked lists, etc. */
Rusty Russell9f85a4b2010-08-05 12:59:04 -06002583 err = module_unload_init(mod);
2584 if (err)
2585 goto free_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
Rusty Russell22e268e2010-08-05 12:59:05 -06002587 /* Now we've got everything in the final locations, we can
2588 * find optional sections. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002589 find_module_sections(mod, info.hdr, info.sechdrs, info.secstrings);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002591 err = check_module_license_and_versions(mod, info.sechdrs);
Rusty Russell22e268e2010-08-05 12:59:05 -06002592 if (err)
2593 goto free_unload;
Dave Jones9841d612006-01-08 01:03:41 -08002594
Matt Domschc988d2b2005-06-23 22:05:15 -07002595 /* Set up MODINFO_ATTR fields */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002596 setup_modinfo(mod, info.sechdrs, info.index.info);
Matt Domschc988d2b2005-06-23 22:05:15 -07002597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 /* Fix up syms, so that st_value is a pointer to location. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002599 err = simplify_symbols(info.sechdrs, info.index.sym, info.strtab, info.index.vers, info.index.pcpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 mod);
2601 if (err < 0)
2602 goto cleanup;
2603
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002604 err = apply_relocations(mod, info.hdr, info.sechdrs, info.index.sym, info.index.str);
Rusty Russell22e268e2010-08-05 12:59:05 -06002605 if (err < 0)
2606 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
2608 /* Set up and sort exception table */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002609 mod->extable = section_objs(info.hdr, info.sechdrs, info.secstrings, "__ex_table",
Rusty Russell5e458cc2008-10-22 10:00:13 -05002610 sizeof(*mod->extable), &mod->num_exentries);
2611 sort_extable(mod->extable, mod->extable + mod->num_exentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
2613 /* Finally, copy percpu area over. */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002614 percpu_modcopy(mod, (void *)info.sechdrs[info.index.pcpu].sh_addr,
2615 info.sechdrs[info.index.pcpu].sh_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Rusty Russelleded41c2010-08-05 12:59:07 -06002617 add_kallsyms(mod, &info, symoffs, stroffs, strmap);
Jan Beulich554bdfe2009-07-06 14:51:44 +01002618 kfree(strmap);
2619 strmap = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
Yehuda Sadehff49d742010-07-03 13:07:35 +10002621 if (!mod->taints)
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002622 debug = section_objs(info.hdr, info.sechdrs, info.secstrings, "__verbose",
Rusty Russell5e458cc2008-10-22 10:00:13 -05002623 sizeof(*debug), &num_debug);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002624
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002625 err = module_finalize(info.hdr, info.sechdrs, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 if (err < 0)
2627 goto cleanup;
2628
Rusty Russell22e268e2010-08-05 12:59:05 -06002629 flush_module_icache(mod);
Thomas Koeller378bac82005-09-06 15:17:11 -07002630
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002631 mod->args = info.args;
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002632
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002633 /* Now sew it into the lists so we can get lockdep and oops
Andi Kleend72b3752008-08-30 10:09:00 +02002634 * info during argument parsing. Noone should access us, since
2635 * strong_try_module_get() will fail.
2636 * lockdep/oops can run asynchronous, so use the RCU list insertion
2637 * function to insert in a way safe to concurrent readers.
2638 * The mutex protects against concurrent writers.
2639 */
Rusty Russell75676502010-06-05 11:17:36 -06002640 mutex_lock(&module_mutex);
Linus Torvalds3bafeb62010-06-05 11:17:36 -06002641 if (find_module(mod->name)) {
2642 err = -EEXIST;
Rusty Russellbe593f42010-06-05 11:17:37 -06002643 goto unlock;
Linus Torvalds3bafeb62010-06-05 11:17:36 -06002644 }
2645
Yehuda Sadehff49d742010-07-03 13:07:35 +10002646 if (debug)
2647 dynamic_debug_setup(debug, num_debug);
2648
Rusty Russellbe593f42010-06-05 11:17:37 -06002649 /* Find duplicate symbols */
2650 err = verify_export_symbols(mod);
2651 if (err < 0)
Yehuda Sadehff49d742010-07-03 13:07:35 +10002652 goto ddebug;
Rusty Russellbe593f42010-06-05 11:17:37 -06002653
Andi Kleend72b3752008-08-30 10:09:00 +02002654 list_add_rcu(&mod->list, &modules);
Rusty Russell75676502010-06-05 11:17:36 -06002655 mutex_unlock(&module_mutex);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002656
Rusty Russelle180a6b2009-03-31 13:05:29 -06002657 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002659 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
Rusty Russelle180a6b2009-03-31 13:05:29 -06002661 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002663 goto unlink;
Rusty Russell80a3d1b2010-06-05 11:17:36 -06002664
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002665 add_sect_attrs(mod, info.hdr->e_shnum, info.secstrings, info.sechdrs);
2666 add_notes_attrs(mod, info.hdr->e_shnum, info.secstrings, info.sechdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
2668 /* Get rid of temporary copy */
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002669 vfree(info.hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
Li Zefan7ead8b82009-08-17 16:56:28 +08002671 trace_module_load(mod);
2672
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 /* Done! */
2674 return mod;
2675
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002676 unlink:
Rusty Russell75676502010-06-05 11:17:36 -06002677 mutex_lock(&module_mutex);
Rusty Russelle91defa2009-03-31 13:05:35 -06002678 /* Unlink carefully: kallsyms could be walking list. */
2679 list_del_rcu(&mod->list);
Yehuda Sadehff49d742010-07-03 13:07:35 +10002680 ddebug:
2681 dynamic_debug_remove(debug);
Rusty Russellbe593f42010-06-05 11:17:37 -06002682 unlock:
Rusty Russell75676502010-06-05 11:17:36 -06002683 mutex_unlock(&module_mutex);
Rusty Russelle91defa2009-03-31 13:05:35 -06002684 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 module_arch_cleanup(mod);
2686 cleanup:
Rusty Russella263f772009-09-25 00:32:58 -06002687 free_modinfo(mod);
Rusty Russell22e268e2010-08-05 12:59:05 -06002688 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 module_unload_free(mod);
Rusty Russellffa9f122009-09-25 00:32:59 -06002690 free_init:
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002691 module_free(mod, mod->module_init);
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002692 module_free(mod, mod->module_core);
2693 /* mod will be freed with core. Don't access it beyond this line! */
2694 free_percpu:
Rusty Russell293a7cf2010-05-31 19:53:50 +09302695 free_percpu(percpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 free_mod:
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002697 kfree(info.args);
Jan Beulich554bdfe2009-07-06 14:51:44 +01002698 kfree(strmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 free_hdr:
Linus Torvalds3264d3f2010-06-02 11:01:06 -07002700 vfree(info.hdr);
Jayachandran C6fe2e702006-01-06 00:19:54 -08002701 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702}
2703
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002704/* Call module constructors. */
2705static void do_mod_ctors(struct module *mod)
2706{
2707#ifdef CONFIG_CONSTRUCTORS
2708 unsigned long i;
2709
2710 for (i = 0; i < mod->num_ctors; i++)
2711 mod->ctors[i]();
2712#endif
2713}
2714
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715/* This is where the real work happens */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002716SYSCALL_DEFINE3(init_module, void __user *, umod,
2717 unsigned long, len, const char __user *, uargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718{
2719 struct module *mod;
2720 int ret = 0;
2721
2722 /* Must have permission */
Kees Cook3d433212009-04-02 15:49:29 -07002723 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 return -EPERM;
2725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 /* Do all the hard work */
2727 mod = load_module(umod, len, uargs);
Rusty Russell75676502010-06-05 11:17:36 -06002728 if (IS_ERR(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 return PTR_ERR(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
Alan Sterne041c682006-03-27 01:16:30 -08002731 blocking_notifier_call_chain(&module_notify_list,
2732 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002734 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 /* Start the module */
2736 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002737 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 if (ret < 0) {
2739 /* Init routine failed: abort. Try to protect us from
2740 buggy refcounters. */
2741 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002742 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002743 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002744 blocking_notifier_call_chain(&module_notify_list,
2745 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002746 free_module(mod);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002747 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 return ret;
2749 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002750 if (ret > 0) {
Joe Perchesad361c92009-07-06 13:05:40 -07002751 printk(KERN_WARNING
2752"%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
2753"%s: loading module anyway...\n",
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002754 __func__, mod->name, ret,
2755 __func__);
2756 dump_stack();
2757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
Rusty Russell6c5db222008-03-10 11:43:52 -07002759 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002761 wake_up(&module_wq);
Masami Hiramatsu0deddf42009-01-06 14:41:54 -08002762 blocking_notifier_call_chain(&module_notify_list,
2763 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07002764
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07002765 /* We need to finish all async code before the module init sequence is done */
2766 async_synchronize_full();
2767
Rusty Russell6c5db222008-03-10 11:43:52 -07002768 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 /* Drop initial reference. */
2770 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06002771 trim_init_extable(mod);
Jan Beulich4a496222009-07-06 14:50:42 +01002772#ifdef CONFIG_KALLSYMS
2773 mod->num_symtab = mod->core_num_syms;
2774 mod->symtab = mod->core_symtab;
Jan Beulich554bdfe2009-07-06 14:51:44 +01002775 mod->strtab = mod->core_strtab;
Jan Beulich4a496222009-07-06 14:50:42 +01002776#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 module_free(mod, mod->module_init);
2778 mod->module_init = NULL;
2779 mod->init_size = 0;
2780 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002781 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
2783 return 0;
2784}
2785
2786static inline int within(unsigned long addr, void *start, unsigned long size)
2787{
2788 return ((void *)addr >= start && (void *)addr < start + size);
2789}
2790
2791#ifdef CONFIG_KALLSYMS
2792/*
2793 * This ignores the intensely annoying "mapping symbols" found
2794 * in ARM ELF files: $a, $t and $d.
2795 */
2796static inline int is_arm_mapping_symbol(const char *str)
2797{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002798 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 && (str[2] == '\0' || str[2] == '.');
2800}
2801
2802static const char *get_ksymbol(struct module *mod,
2803 unsigned long addr,
2804 unsigned long *size,
2805 unsigned long *offset)
2806{
2807 unsigned int i, best = 0;
2808 unsigned long nextval;
2809
2810 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002811 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002813 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2815
2816 /* Scan for closest preceeding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002817 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 for (i = 1; i < mod->num_symtab; i++) {
2819 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2820 continue;
2821
2822 /* We ignore unnamed symbols: they're uninformative
2823 * and inserted at a whim. */
2824 if (mod->symtab[i].st_value <= addr
2825 && mod->symtab[i].st_value > mod->symtab[best].st_value
2826 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2827 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2828 best = i;
2829 if (mod->symtab[i].st_value > addr
2830 && mod->symtab[i].st_value < nextval
2831 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2832 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2833 nextval = mod->symtab[i].st_value;
2834 }
2835
2836 if (!best)
2837 return NULL;
2838
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002839 if (size)
2840 *size = nextval - mod->symtab[best].st_value;
2841 if (offset)
2842 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 return mod->strtab + mod->symtab[best].st_name;
2844}
2845
Rusty Russell6dd06c92008-01-29 17:13:22 -05002846/* For kallsyms to ask for address resolution. NULL means not found. Careful
2847 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002848const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05002849 unsigned long *size,
2850 unsigned long *offset,
2851 char **modname,
2852 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853{
2854 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002855 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
Rusty Russellcb2a5202008-01-14 00:55:03 -08002857 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002858 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002859 if (within_module_init(addr, mod) ||
2860 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002861 if (modname)
2862 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002863 ret = get_ksymbol(mod, addr, size, offset);
2864 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 }
2866 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05002867 /* Make a copy in here where it's safe */
2868 if (ret) {
2869 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2870 ret = namebuf;
2871 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002872 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002873 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874}
2875
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002876int lookup_module_symbol_name(unsigned long addr, char *symname)
2877{
2878 struct module *mod;
2879
Rusty Russellcb2a5202008-01-14 00:55:03 -08002880 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002881 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002882 if (within_module_init(addr, mod) ||
2883 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002884 const char *sym;
2885
2886 sym = get_ksymbol(mod, addr, NULL, NULL);
2887 if (!sym)
2888 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07002889 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002890 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002891 return 0;
2892 }
2893 }
2894out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002895 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002896 return -ERANGE;
2897}
2898
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002899int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2900 unsigned long *offset, char *modname, char *name)
2901{
2902 struct module *mod;
2903
Rusty Russellcb2a5202008-01-14 00:55:03 -08002904 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002905 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002906 if (within_module_init(addr, mod) ||
2907 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002908 const char *sym;
2909
2910 sym = get_ksymbol(mod, addr, size, offset);
2911 if (!sym)
2912 goto out;
2913 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07002914 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002915 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07002916 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002917 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002918 return 0;
2919 }
2920 }
2921out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002922 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002923 return -ERANGE;
2924}
2925
Alexey Dobriyanea078902007-05-08 00:28:39 -07002926int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2927 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928{
2929 struct module *mod;
2930
Rusty Russellcb2a5202008-01-14 00:55:03 -08002931 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002932 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 if (symnum < mod->num_symtab) {
2934 *value = mod->symtab[symnum].st_value;
2935 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002936 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07002937 KSYM_NAME_LEN);
2938 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06002939 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002940 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002941 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 }
2943 symnum -= mod->num_symtab;
2944 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002945 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002946 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947}
2948
2949static unsigned long mod_find_symname(struct module *mod, const char *name)
2950{
2951 unsigned int i;
2952
2953 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002954 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2955 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 return mod->symtab[i].st_value;
2957 return 0;
2958}
2959
2960/* Look for this name: can be of form module:name. */
2961unsigned long module_kallsyms_lookup_name(const char *name)
2962{
2963 struct module *mod;
2964 char *colon;
2965 unsigned long ret = 0;
2966
2967 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08002968 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 if ((colon = strchr(name, ':')) != NULL) {
2970 *colon = '\0';
2971 if ((mod = find_module(name)) != NULL)
2972 ret = mod_find_symname(mod, colon+1);
2973 *colon = ':';
2974 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02002975 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 if ((ret = mod_find_symname(mod, name)) != 0)
2977 break;
2978 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002979 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 return ret;
2981}
Anders Kaseorg75a66612008-12-05 19:03:58 -05002982
2983int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
2984 struct module *, unsigned long),
2985 void *data)
2986{
2987 struct module *mod;
2988 unsigned int i;
2989 int ret;
2990
2991 list_for_each_entry(mod, &modules, list) {
2992 for (i = 0; i < mod->num_symtab; i++) {
2993 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
2994 mod, mod->symtab[i].st_value);
2995 if (ret != 0)
2996 return ret;
2997 }
2998 }
2999 return 0;
3000}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001#endif /* CONFIG_KALLSYMS */
3002
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003003static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003004{
3005 int bx = 0;
3006
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003007 if (mod->taints ||
3008 mod->state == MODULE_STATE_GOING ||
3009 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003010 buf[bx++] = '(';
Andi Kleen25ddbb12008-10-15 22:01:41 -07003011 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003012 buf[bx++] = 'P';
Andi Kleen25ddbb12008-10-15 22:01:41 -07003013 if (mod->taints & (1 << TAINT_FORCED_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003014 buf[bx++] = 'F';
Linus Torvalds26e9a392008-10-17 09:50:12 -07003015 if (mod->taints & (1 << TAINT_CRAP))
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07003016 buf[bx++] = 'C';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003017 /*
3018 * TAINT_FORCED_RMMOD: could be added.
3019 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
3020 * apply to modules.
3021 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003022
3023 /* Show a - for module-is-being-unloaded */
3024 if (mod->state == MODULE_STATE_GOING)
3025 buf[bx++] = '-';
3026 /* Show a + for module-is-being-loaded */
3027 if (mod->state == MODULE_STATE_COMING)
3028 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003029 buf[bx++] = ')';
3030 }
3031 buf[bx] = '\0';
3032
3033 return buf;
3034}
3035
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003036#ifdef CONFIG_PROC_FS
3037/* Called by the /proc file system to return a list of modules. */
3038static void *m_start(struct seq_file *m, loff_t *pos)
3039{
3040 mutex_lock(&module_mutex);
3041 return seq_list_start(&modules, *pos);
3042}
3043
3044static void *m_next(struct seq_file *m, void *p, loff_t *pos)
3045{
3046 return seq_list_next(p, &modules, pos);
3047}
3048
3049static void m_stop(struct seq_file *m, void *p)
3050{
3051 mutex_unlock(&module_mutex);
3052}
3053
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054static int m_show(struct seq_file *m, void *p)
3055{
3056 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003057 char buf[8];
3058
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05003059 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 mod->name, mod->init_size + mod->core_size);
3061 print_unload_info(m, mod);
3062
3063 /* Informative for users. */
3064 seq_printf(m, " %s",
3065 mod->state == MODULE_STATE_GOING ? "Unloading":
3066 mod->state == MODULE_STATE_COMING ? "Loading":
3067 "Live");
3068 /* Used by oprofile and other similar tools. */
3069 seq_printf(m, " 0x%p", mod->module_core);
3070
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003071 /* Taints info */
3072 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003073 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07003074
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 seq_printf(m, "\n");
3076 return 0;
3077}
3078
3079/* Format: modulename size refcount deps address
3080
3081 Where refcount is a number or -, and deps is a comma-separated list
3082 of depends or -.
3083*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003084static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 .start = m_start,
3086 .next = m_next,
3087 .stop = m_stop,
3088 .show = m_show
3089};
3090
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04003091static int modules_open(struct inode *inode, struct file *file)
3092{
3093 return seq_open(file, &modules_op);
3094}
3095
3096static const struct file_operations proc_modules_operations = {
3097 .open = modules_open,
3098 .read = seq_read,
3099 .llseek = seq_lseek,
3100 .release = seq_release,
3101};
3102
3103static int __init proc_modules_init(void)
3104{
3105 proc_create("modules", 0, NULL, &proc_modules_operations);
3106 return 0;
3107}
3108module_init(proc_modules_init);
3109#endif
3110
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111/* Given an address, look for it in the module exception tables. */
3112const struct exception_table_entry *search_module_extables(unsigned long addr)
3113{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 const struct exception_table_entry *e = NULL;
3115 struct module *mod;
3116
Rusty Russell24da1cb2007-07-15 23:41:46 -07003117 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02003118 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 if (mod->num_exentries == 0)
3120 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07003121
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 e = search_extable(mod->extable,
3123 mod->extable + mod->num_exentries - 1,
3124 addr);
3125 if (e)
3126 break;
3127 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07003128 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
3130 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07003131 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 return e;
3133}
3134
Ingo Molnar4d435f92006-07-03 00:24:24 -07003135/*
Rusty Russelle6104992009-03-31 13:05:31 -06003136 * is_module_address - is this address inside a module?
3137 * @addr: the address to check.
3138 *
3139 * See is_module_text_address() if you simply want to see if the address
3140 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07003141 */
Rusty Russelle6104992009-03-31 13:05:31 -06003142bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07003143{
Rusty Russelle6104992009-03-31 13:05:31 -06003144 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003145
Rusty Russell24da1cb2007-07-15 23:41:46 -07003146 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06003147 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07003148 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07003149
Rusty Russelle6104992009-03-31 13:05:31 -06003150 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07003151}
3152
Rusty Russelle6104992009-03-31 13:05:31 -06003153/*
3154 * __module_address - get the module which contains an address.
3155 * @addr: the address.
3156 *
3157 * Must be called with preempt disabled or module mutex held so that
3158 * module doesn't get freed during this.
3159 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07003160struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161{
3162 struct module *mod;
3163
Rusty Russell3a642e92008-07-22 19:24:28 -05003164 if (addr < module_addr_min || addr > module_addr_max)
3165 return NULL;
3166
Andi Kleend72b3752008-08-30 10:09:00 +02003167 list_for_each_entry_rcu(mod, &modules, list)
Rusty Russelle6104992009-03-31 13:05:31 -06003168 if (within_module_core(addr, mod)
3169 || within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 return mod;
3171 return NULL;
3172}
Tim Abbottc6b37802008-12-05 19:03:59 -05003173EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174
Rusty Russelle6104992009-03-31 13:05:31 -06003175/*
3176 * is_module_text_address - is this address inside module code?
3177 * @addr: the address to check.
3178 *
3179 * See is_module_address() if you simply want to see if the address is
3180 * anywhere in a module. See kernel_text_address() for testing if an
3181 * address corresponds to kernel or module code.
3182 */
3183bool is_module_text_address(unsigned long addr)
3184{
3185 bool ret;
3186
3187 preempt_disable();
3188 ret = __module_text_address(addr) != NULL;
3189 preempt_enable();
3190
3191 return ret;
3192}
3193
3194/*
3195 * __module_text_address - get the module whose code contains an address.
3196 * @addr: the address.
3197 *
3198 * Must be called with preempt disabled or module mutex held so that
3199 * module doesn't get freed during this.
3200 */
3201struct module *__module_text_address(unsigned long addr)
3202{
3203 struct module *mod = __module_address(addr);
3204 if (mod) {
3205 /* Make sure it's within the text section. */
3206 if (!within(addr, mod->module_init, mod->init_text_size)
3207 && !within(addr, mod->module_core, mod->core_text_size))
3208 mod = NULL;
3209 }
3210 return mod;
3211}
Tim Abbottc6b37802008-12-05 19:03:59 -05003212EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06003213
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214/* Don't grab lock, we're oopsing. */
3215void print_modules(void)
3216{
3217 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07003218 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
Linus Torvaldsb2311252009-06-16 11:07:14 -07003220 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02003221 /* Most callers should already have preempt disabled, but make sure */
3222 preempt_disable();
3223 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01003224 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02003225 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01003226 if (last_unloaded_module[0])
3227 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 printk("\n");
3229}
3230
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06003232/* Generate the signature for all relevant module structures here.
3233 * If these change, we don't want to try to parse the module. */
3234void module_layout(struct module *mod,
3235 struct modversion_info *ver,
3236 struct kernel_param *kp,
3237 struct kernel_symbol *ks,
Rusty Russell8c8ef422009-03-31 13:05:34 -06003238 struct tracepoint *tp)
3239{
3240}
3241EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07003243
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04003244#ifdef CONFIG_TRACEPOINTS
3245void module_update_tracepoints(void)
3246{
3247 struct module *mod;
3248
3249 mutex_lock(&module_mutex);
3250 list_for_each_entry(mod, &modules, list)
3251 if (!mod->taints)
3252 tracepoint_update_probe_range(mod->tracepoints,
3253 mod->tracepoints + mod->num_tracepoints);
3254 mutex_unlock(&module_mutex);
3255}
3256
3257/*
3258 * Returns 0 if current not found.
3259 * Returns 1 if current found.
3260 */
3261int module_get_iter_tracepoints(struct tracepoint_iter *iter)
3262{
3263 struct module *iter_mod;
3264 int found = 0;
3265
3266 mutex_lock(&module_mutex);
3267 list_for_each_entry(iter_mod, &modules, list) {
3268 if (!iter_mod->taints) {
3269 /*
3270 * Sorted module list
3271 */
3272 if (iter_mod < iter->module)
3273 continue;
3274 else if (iter_mod > iter->module)
3275 iter->tracepoint = NULL;
3276 found = tracepoint_get_iter_range(&iter->tracepoint,
3277 iter_mod->tracepoints,
3278 iter_mod->tracepoints
3279 + iter_mod->num_tracepoints);
3280 if (found) {
3281 iter->module = iter_mod;
3282 break;
3283 }
3284 }
3285 }
3286 mutex_unlock(&module_mutex);
3287 return found;
3288}
3289#endif