blob: 07c80e68a6c41a656e109d697597f1c01ab52897 [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>
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020050#include <linux/license.h>
Christoph Lameter6d762392008-02-08 04:18:42 -080051#include <asm/sections.h>
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040052#include <linux/tracepoint.h>
Steven Rostedt90d595f2008-08-14 15:45:09 -040053#include <linux/ftrace.h>
Arjan van de Ven22a9d642009-01-07 08:45:46 -080054#include <linux/async.h>
Tejun Heofbf59bc2009-02-20 16:29:08 +090055#include <linux/percpu.h>
Catalin Marinas4f2294b2009-06-11 13:23:20 +010056#include <linux/kmemleak.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#if 0
59#define DEBUGP printk
60#else
61#define DEBUGP(fmt , a...)
62#endif
63
64#ifndef ARCH_SHF_SMALL
65#define ARCH_SHF_SMALL 0
66#endif
67
68/* If this is set, the section belongs in the init part of the module */
69#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
70
Rusty Russell24da1cb2007-07-15 23:41:46 -070071/* List of modules, protected by module_mutex or preempt_disable
Andi Kleend72b3752008-08-30 10:09:00 +020072 * (delete uses stop_machine/add uses RCU list operations). */
Tim Abbottc6b37802008-12-05 19:03:59 -050073DEFINE_MUTEX(module_mutex);
74EXPORT_SYMBOL_GPL(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static LIST_HEAD(modules);
76
Stephen Rothwell19e45292009-04-14 17:27:18 +100077/* Block module loading/unloading? */
78int modules_disabled = 0;
79
Rusty Russellc9a3ba52008-01-29 17:13:18 -050080/* Waiting for a module to finish initializing? */
81static DECLARE_WAIT_QUEUE_HEAD(module_wq);
82
Alan Sterne041c682006-03-27 01:16:30 -080083static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Rusty Russelle6104992009-03-31 13:05:31 -060085/* Bounds of module allocation, for speeding __module_address */
Rusty Russell3a642e92008-07-22 19:24:28 -050086static unsigned long module_addr_min = -1UL, module_addr_max = 0;
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088int register_module_notifier(struct notifier_block * nb)
89{
Alan Sterne041c682006-03-27 01:16:30 -080090 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92EXPORT_SYMBOL(register_module_notifier);
93
94int unregister_module_notifier(struct notifier_block * nb)
95{
Alan Sterne041c682006-03-27 01:16:30 -080096 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98EXPORT_SYMBOL(unregister_module_notifier);
99
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -0800100/* We require a truly strong try_module_get(): 0 means failure due to
101 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static inline int strong_try_module_get(struct module *mod)
103{
104 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500105 return -EBUSY;
106 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500108 else
109 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700112static inline void add_taint_module(struct module *mod, unsigned flag)
113{
114 add_taint(flag);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700115 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700116}
117
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200118/*
119 * A thread that wants to hold a reference to a module only while it
120 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 */
122void __module_put_and_exit(struct module *mod, long code)
123{
124 module_put(mod);
125 do_exit(code);
126}
127EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129/* Find a module section: 0 means not found. */
130static unsigned int find_sec(Elf_Ehdr *hdr,
131 Elf_Shdr *sechdrs,
132 const char *secstrings,
133 const char *name)
134{
135 unsigned int i;
136
137 for (i = 1; i < hdr->e_shnum; i++)
138 /* Alloc bit cleared means "ignore it." */
139 if ((sechdrs[i].sh_flags & SHF_ALLOC)
140 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
141 return i;
142 return 0;
143}
144
Rusty Russell5e458cc2008-10-22 10:00:13 -0500145/* Find a module section, or NULL. */
146static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
147 const char *secstrings, const char *name)
148{
149 /* Section 0 has sh_addr 0. */
150 return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
151}
152
153/* Find a module section, or NULL. Fill in number of "objects" in section. */
154static void *section_objs(Elf_Ehdr *hdr,
155 Elf_Shdr *sechdrs,
156 const char *secstrings,
157 const char *name,
158 size_t object_size,
159 unsigned int *num)
160{
161 unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
162
163 /* Section 0 has sh_addr 0 and sh_size 0. */
164 *num = sechdrs[sec].sh_size / object_size;
165 return (void *)sechdrs[sec].sh_addr;
166}
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/* Provided by the linker */
169extern const struct kernel_symbol __start___ksymtab[];
170extern const struct kernel_symbol __stop___ksymtab[];
171extern const struct kernel_symbol __start___ksymtab_gpl[];
172extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800173extern const struct kernel_symbol __start___ksymtab_gpl_future[];
174extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700175extern const struct kernel_symbol __start___ksymtab_gpl_future[];
176extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177extern const unsigned long __start___kcrctab[];
178extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800179extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500180#ifdef CONFIG_UNUSED_SYMBOLS
181extern const struct kernel_symbol __start___ksymtab_unused[];
182extern const struct kernel_symbol __stop___ksymtab_unused[];
183extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
184extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700185extern const unsigned long __start___kcrctab_unused[];
186extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500187#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189#ifndef CONFIG_MODVERSIONS
190#define symversion(base, idx) NULL
191#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800192#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193#endif
194
Rusty Russelldafd0942008-07-22 19:24:25 -0500195static bool each_symbol_in_section(const struct symsearch *arr,
196 unsigned int arrsize,
197 struct module *owner,
198 bool (*fn)(const struct symsearch *syms,
199 struct module *owner,
200 unsigned int symnum, void *data),
201 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100202{
Rusty Russelldafd0942008-07-22 19:24:25 -0500203 unsigned int i, j;
204
205 for (j = 0; j < arrsize; j++) {
206 for (i = 0; i < arr[j].stop - arr[j].start; i++)
207 if (fn(&arr[j], owner, i, data))
208 return true;
209 }
210
211 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100212}
213
Rusty Russelldafd0942008-07-22 19:24:25 -0500214/* Returns true as soon as fn returns true, otherwise false. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500215bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
216 unsigned int symnum, void *data), void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700217{
Rusty Russelldafd0942008-07-22 19:24:25 -0500218 struct module *mod;
219 const struct symsearch arr[] = {
220 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
221 NOT_GPL_ONLY, false },
222 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
223 __start___kcrctab_gpl,
224 GPL_ONLY, false },
225 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
226 __start___kcrctab_gpl_future,
227 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500228#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500229 { __start___ksymtab_unused, __stop___ksymtab_unused,
230 __start___kcrctab_unused,
231 NOT_GPL_ONLY, true },
232 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
233 __start___kcrctab_unused_gpl,
234 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500235#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500236 };
237
238 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
239 return true;
240
Andi Kleend72b3752008-08-30 10:09:00 +0200241 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500242 struct symsearch arr[] = {
243 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
244 NOT_GPL_ONLY, false },
245 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
246 mod->gpl_crcs,
247 GPL_ONLY, false },
248 { mod->gpl_future_syms,
249 mod->gpl_future_syms + mod->num_gpl_future_syms,
250 mod->gpl_future_crcs,
251 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500252#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500253 { mod->unused_syms,
254 mod->unused_syms + mod->num_unused_syms,
255 mod->unused_crcs,
256 NOT_GPL_ONLY, true },
257 { mod->unused_gpl_syms,
258 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
259 mod->unused_gpl_crcs,
260 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500261#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500262 };
263
264 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
265 return true;
266 }
267 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700268}
Tim Abbottc6b37802008-12-05 19:03:59 -0500269EXPORT_SYMBOL_GPL(each_symbol);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700270
Rusty Russelldafd0942008-07-22 19:24:25 -0500271struct find_symbol_arg {
272 /* Input */
273 const char *name;
274 bool gplok;
275 bool warn;
276
277 /* Output */
278 struct module *owner;
279 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500280 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500281};
282
283static bool find_symbol_in_section(const struct symsearch *syms,
284 struct module *owner,
285 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500286{
Rusty Russelldafd0942008-07-22 19:24:25 -0500287 struct find_symbol_arg *fsa = data;
288
289 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
290 return false;
291
292 if (!fsa->gplok) {
293 if (syms->licence == GPL_ONLY)
294 return false;
295 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
296 printk(KERN_WARNING "Symbol %s is being used "
297 "by a non-GPL module, which will not "
298 "be allowed in the future\n", fsa->name);
299 printk(KERN_WARNING "Please see the file "
300 "Documentation/feature-removal-schedule.txt "
301 "in the kernel source tree for more details.\n");
302 }
303 }
304
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500305#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500306 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500307 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500308 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500309 printk(KERN_WARNING
310 "This symbol will go away in the future.\n");
311 printk(KERN_WARNING
312 "Please evalute if this is the right api to use and if "
313 "it really is, submit a report the linux kernel "
314 "mailinglist together with submitting your code for "
315 "inclusion.\n");
316 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500317#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500318
319 fsa->owner = owner;
320 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500321 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500322 return true;
323}
324
Tim Abbott414fd312008-12-05 19:03:56 -0500325/* Find a symbol and return it, along with, (optional) crc and
326 * (optional) module which owns it */
Tim Abbottc6b37802008-12-05 19:03:59 -0500327const struct kernel_symbol *find_symbol(const char *name,
328 struct module **owner,
329 const unsigned long **crc,
330 bool gplok,
331 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Rusty Russelldafd0942008-07-22 19:24:25 -0500333 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Rusty Russelldafd0942008-07-22 19:24:25 -0500335 fsa.name = name;
336 fsa.gplok = gplok;
337 fsa.warn = warn;
338
339 if (each_symbol(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500340 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500341 *owner = fsa.owner;
342 if (crc)
343 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500344 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 DEBUGP("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500348 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
Tim Abbottc6b37802008-12-05 19:03:59 -0500350EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352/* Search for module by name: must hold module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500353struct module *find_module(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 struct module *mod;
356
357 list_for_each_entry(mod, &modules, list) {
358 if (strcmp(mod->name, name) == 0)
359 return mod;
360 }
361 return NULL;
362}
Tim Abbottc6b37802008-12-05 19:03:59 -0500363EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900366
367#ifdef CONFIG_HAVE_DYNAMIC_PER_CPU_AREA
368
369static void *percpu_modalloc(unsigned long size, unsigned long align,
370 const char *name)
371{
372 void *ptr;
373
374 if (align > PAGE_SIZE) {
375 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
376 name, align, PAGE_SIZE);
377 align = PAGE_SIZE;
378 }
379
Tejun Heoedcb4632009-03-06 14:33:59 +0900380 ptr = __alloc_reserved_percpu(size, align);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900381 if (!ptr)
382 printk(KERN_WARNING
383 "Could not allocate %lu bytes percpu data\n", size);
384 return ptr;
385}
386
387static void percpu_modfree(void *freeme)
388{
389 free_percpu(freeme);
390}
391
392#else /* ... !CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394/* Number of blocks used and allocated. */
395static unsigned int pcpu_num_used, pcpu_num_allocated;
396/* Size of each block. -ve means used. */
397static int *pcpu_size;
398
399static int split_block(unsigned int i, unsigned short size)
400{
401 /* Reallocation required? */
402 if (pcpu_num_used + 1 > pcpu_num_allocated) {
Pekka Enberg6d4f9c52007-05-08 00:24:58 -0700403 int *new;
404
405 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
406 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (!new)
408 return 0;
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 pcpu_num_allocated *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 pcpu_size = new;
412 }
413
414 /* Insert a new subblock */
415 memmove(&pcpu_size[i+1], &pcpu_size[i],
416 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
417 pcpu_num_used++;
418
419 pcpu_size[i+1] -= size;
420 pcpu_size[i] = size;
421 return 1;
422}
423
424static inline unsigned int block_size(int val)
425{
426 if (val < 0)
427 return -val;
428 return val;
429}
430
Rusty Russell842bbaa2005-08-01 21:11:47 -0700431static void *percpu_modalloc(unsigned long size, unsigned long align,
432 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 unsigned long extra;
435 unsigned int i;
436 void *ptr;
Catalin Marinas4f2294b2009-06-11 13:23:20 +0100437 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Jeremy Fitzhardingeb6e35902007-05-02 19:27:12 +0200439 if (align > PAGE_SIZE) {
440 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
441 name, align, PAGE_SIZE);
442 align = PAGE_SIZE;
Rusty Russell842bbaa2005-08-01 21:11:47 -0700443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 ptr = __per_cpu_start;
446 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
447 /* Extra for alignment requirement. */
448 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
449 BUG_ON(i == 0 && extra != 0);
450
451 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
452 continue;
453
454 /* Transfer extra to previous block. */
455 if (pcpu_size[i-1] < 0)
456 pcpu_size[i-1] -= extra;
457 else
458 pcpu_size[i-1] += extra;
459 pcpu_size[i] -= extra;
460 ptr += extra;
461
462 /* Split block if warranted */
463 if (pcpu_size[i] - size > sizeof(unsigned long))
464 if (!split_block(i, size))
465 return NULL;
466
Catalin Marinas4f2294b2009-06-11 13:23:20 +0100467 /* add the per-cpu scanning areas */
468 for_each_possible_cpu(cpu)
469 kmemleak_alloc(ptr + per_cpu_offset(cpu), size, 0,
470 GFP_KERNEL);
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /* Mark allocated */
473 pcpu_size[i] = -pcpu_size[i];
474 return ptr;
475 }
476
477 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
478 size);
479 return NULL;
480}
481
482static void percpu_modfree(void *freeme)
483{
484 unsigned int i;
485 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
Catalin Marinas4f2294b2009-06-11 13:23:20 +0100486 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 /* First entry is core kernel percpu data. */
489 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
490 if (ptr == freeme) {
491 pcpu_size[i] = -pcpu_size[i];
492 goto free;
493 }
494 }
495 BUG();
496
497 free:
Catalin Marinas4f2294b2009-06-11 13:23:20 +0100498 /* remove the per-cpu scanning areas */
499 for_each_possible_cpu(cpu)
500 kmemleak_free(freeme + per_cpu_offset(cpu));
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 /* Merge with previous? */
503 if (pcpu_size[i-1] >= 0) {
504 pcpu_size[i-1] += pcpu_size[i];
505 pcpu_num_used--;
506 memmove(&pcpu_size[i], &pcpu_size[i+1],
507 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
508 i--;
509 }
510 /* Merge with next? */
511 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
512 pcpu_size[i] += pcpu_size[i+1];
513 pcpu_num_used--;
514 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
515 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
516 }
517}
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519static int percpu_modinit(void)
520{
521 pcpu_num_used = 2;
522 pcpu_num_allocated = 2;
523 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
524 GFP_KERNEL);
525 /* Static in-kernel percpu data (used). */
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +0200526 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 /* Free room. */
528 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
529 if (pcpu_size[1] < 0) {
530 printk(KERN_ERR "No per-cpu room for modules.\n");
531 pcpu_num_used = 1;
532 }
533
534 return 0;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700535}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536__initcall(percpu_modinit);
Tejun Heo6b588c12009-02-20 16:29:07 +0900537
Tejun Heofbf59bc2009-02-20 16:29:08 +0900538#endif /* CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
539
Tejun Heo6b588c12009-02-20 16:29:07 +0900540static unsigned int find_pcpusec(Elf_Ehdr *hdr,
541 Elf_Shdr *sechdrs,
542 const char *secstrings)
543{
544 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
545}
546
547static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
548{
549 int cpu;
550
551 for_each_possible_cpu(cpu)
552 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
553}
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900556
Rusty Russell842bbaa2005-08-01 21:11:47 -0700557static inline void *percpu_modalloc(unsigned long size, unsigned long align,
558 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 return NULL;
561}
562static inline void percpu_modfree(void *pcpuptr)
563{
564 BUG();
565}
566static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
567 Elf_Shdr *sechdrs,
568 const char *secstrings)
569{
570 return 0;
571}
572static inline void percpu_modcopy(void *pcpudst, const void *src,
573 unsigned long size)
574{
575 /* pcpusec should be 0, and size of that section should be 0. */
576 BUG_ON(size != 0);
577}
Tejun Heo6b588c12009-02-20 16:29:07 +0900578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579#endif /* CONFIG_SMP */
580
Matt Domschc988d2b2005-06-23 22:05:15 -0700581#define MODINFO_ATTR(field) \
582static void setup_modinfo_##field(struct module *mod, const char *s) \
583{ \
584 mod->field = kstrdup(s, GFP_KERNEL); \
585} \
586static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
587 struct module *mod, char *buffer) \
588{ \
589 return sprintf(buffer, "%s\n", mod->field); \
590} \
591static int modinfo_##field##_exists(struct module *mod) \
592{ \
593 return mod->field != NULL; \
594} \
595static void free_modinfo_##field(struct module *mod) \
596{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700597 kfree(mod->field); \
598 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700599} \
600static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900601 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700602 .show = show_modinfo_##field, \
603 .setup = setup_modinfo_##field, \
604 .test = modinfo_##field##_exists, \
605 .free = free_modinfo_##field, \
606};
607
608MODINFO_ATTR(version);
609MODINFO_ATTR(srcversion);
610
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100611static char last_unloaded_module[MODULE_NAME_LEN+1];
612
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800613#ifdef CONFIG_MODULE_UNLOAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614/* Init the unload section of the module. */
615static void module_unload_init(struct module *mod)
616{
Eric Dumazet720eba32009-02-03 13:31:36 +1030617 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 INIT_LIST_HEAD(&mod->modules_which_use_me);
Eric Dumazet720eba32009-02-03 13:31:36 +1030620 for_each_possible_cpu(cpu)
621 local_set(__module_ref_addr(mod, cpu), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 /* Hold reference count during initialization. */
Eric Dumazet720eba32009-02-03 13:31:36 +1030623 local_set(__module_ref_addr(mod, raw_smp_processor_id()), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 /* Backwards compatibility macros put refcount during init. */
625 mod->waiter = current;
626}
627
628/* modules using other modules */
629struct module_use
630{
631 struct list_head list;
632 struct module *module_which_uses;
633};
634
635/* Does a already use b? */
636static int already_uses(struct module *a, struct module *b)
637{
638 struct module_use *use;
639
640 list_for_each_entry(use, &b->modules_which_use_me, list) {
641 if (use->module_which_uses == a) {
642 DEBUGP("%s uses %s!\n", a->name, b->name);
643 return 1;
644 }
645 }
646 DEBUGP("%s does not use %s!\n", a->name, b->name);
647 return 0;
648}
649
650/* Module a uses b */
Tim Abbottc6b37802008-12-05 19:03:59 -0500651int use_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 struct module_use *use;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500654 int no_warn, err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (b == NULL || already_uses(a, b)) return 1;
657
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500658 /* If we're interrupted or time out, we fail. */
659 if (wait_event_interruptible_timeout(
660 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
661 30 * HZ) <= 0) {
662 printk("%s: gave up waiting for init of module %s.\n",
663 a->name, b->name);
664 return 0;
665 }
666
667 /* If strong_try_module_get() returned a different error, we fail. */
668 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return 0;
670
671 DEBUGP("Allocating new usage for %s.\n", a->name);
672 use = kmalloc(sizeof(*use), GFP_ATOMIC);
673 if (!use) {
674 printk("%s: out of memory loading\n", a->name);
675 module_put(b);
676 return 0;
677 }
678
679 use->module_which_uses = a;
680 list_add(&use->list, &b->modules_which_use_me);
Kay Sievers270a6c42007-01-18 13:26:15 +0100681 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return 1;
683}
Tim Abbottc6b37802008-12-05 19:03:59 -0500684EXPORT_SYMBOL_GPL(use_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686/* Clear the unload stuff of the module. */
687static void module_unload_free(struct module *mod)
688{
689 struct module *i;
690
691 list_for_each_entry(i, &modules, list) {
692 struct module_use *use;
693
694 list_for_each_entry(use, &i->modules_which_use_me, list) {
695 if (use->module_which_uses == mod) {
696 DEBUGP("%s unusing %s\n", mod->name, i->name);
697 module_put(i);
698 list_del(&use->list);
699 kfree(use);
Kay Sievers270a6c42007-01-18 13:26:15 +0100700 sysfs_remove_link(i->holders_dir, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 /* There can be at most one match. */
702 break;
703 }
704 }
705 }
706}
707
708#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800709static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 int ret = (flags & O_TRUNC);
712 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800713 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return ret;
715}
716#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800717static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
719 return 0;
720}
721#endif /* CONFIG_MODULE_FORCE_UNLOAD */
722
723struct stopref
724{
725 struct module *mod;
726 int flags;
727 int *forced;
728};
729
730/* Whole machine is stopped with interrupts off when this runs. */
731static int __try_stop_module(void *_sref)
732{
733 struct stopref *sref = _sref;
734
Rusty Russellda39ba52008-07-22 19:24:25 -0500735 /* If it's not unused, quit unless we're forcing. */
736 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800737 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return -EWOULDBLOCK;
739 }
740
741 /* Mark it as dying. */
742 sref->mod->state = MODULE_STATE_GOING;
743 return 0;
744}
745
746static int try_stop_module(struct module *mod, int flags, int *forced)
747{
Rusty Russellda39ba52008-07-22 19:24:25 -0500748 if (flags & O_NONBLOCK) {
749 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500751 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500752 } else {
753 /* We don't need to stop the machine for this. */
754 mod->state = MODULE_STATE_GOING;
755 synchronize_sched();
756 return 0;
757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
760unsigned int module_refcount(struct module *mod)
761{
Eric Dumazet720eba32009-02-03 13:31:36 +1030762 unsigned int total = 0;
763 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Eric Dumazet720eba32009-02-03 13:31:36 +1030765 for_each_possible_cpu(cpu)
766 total += local_read(__module_ref_addr(mod, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return total;
768}
769EXPORT_SYMBOL(module_refcount);
770
771/* This exists whether we can unload or not */
772static void free_module(struct module *mod);
773
774static void wait_for_zero_refcount(struct module *mod)
775{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500776 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800777 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 for (;;) {
779 DEBUGP("Looking at refcount...\n");
780 set_current_state(TASK_UNINTERRUPTIBLE);
781 if (module_refcount(mod) == 0)
782 break;
783 schedule();
784 }
785 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800786 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100789SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
790 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
792 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800793 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 int ret, forced = 0;
795
Kees Cook3d433212009-04-02 15:49:29 -0700796 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800797 return -EPERM;
798
799 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
800 return -EFAULT;
801 name[MODULE_NAME_LEN-1] = '\0';
802
Heiko Carstens9e018922008-12-22 12:36:31 +0100803 /* Create stop_machine threads since free_module relies on
804 * a non-failing stop_machine call. */
805 ret = stop_machine_create();
806 if (ret)
807 return ret;
808
809 if (mutex_lock_interruptible(&module_mutex) != 0) {
810 ret = -EINTR;
811 goto out_stop;
812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 mod = find_module(name);
815 if (!mod) {
816 ret = -ENOENT;
817 goto out;
818 }
819
820 if (!list_empty(&mod->modules_which_use_me)) {
821 /* Other modules depend on us: get rid of them first. */
822 ret = -EWOULDBLOCK;
823 goto out;
824 }
825
826 /* Doing init or already dying? */
827 if (mod->state != MODULE_STATE_LIVE) {
828 /* FIXME: if (force), slam module count and wake up
829 waiter --RR */
830 DEBUGP("%s already dying\n", mod->name);
831 ret = -EBUSY;
832 goto out;
833 }
834
835 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700836 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800837 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (!forced) {
839 /* This module can't be removed */
840 ret = -EBUSY;
841 goto out;
842 }
843 }
844
845 /* Set this up before setting mod->state */
846 mod->waiter = current;
847
848 /* Stop the machine so refcounts can't move and disable module. */
849 ret = try_stop_module(mod, flags, &forced);
850 if (ret != 0)
851 goto out;
852
853 /* Never wait if forced. */
854 if (!forced && module_refcount(mod) != 0)
855 wait_for_zero_refcount(mod);
856
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200857 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 /* Final destruction now noone is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200859 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200861 blocking_notifier_call_chain(&module_notify_list,
862 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800863 async_synchronize_full();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200864 mutex_lock(&module_mutex);
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100865 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500866 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Jason Barone9d376f2009-02-05 11:51:38 -0500867 ddebug_remove_module(mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 free_module(mod);
869
870 out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800871 mutex_unlock(&module_mutex);
Heiko Carstens9e018922008-12-22 12:36:31 +0100872out_stop:
873 stop_machine_destroy();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return ret;
875}
876
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800877static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
879 struct module_use *use;
880 int printed_something = 0;
881
882 seq_printf(m, " %u ", module_refcount(mod));
883
884 /* Always include a trailing , so userspace can differentiate
885 between this and the old multi-field proc format. */
886 list_for_each_entry(use, &mod->modules_which_use_me, list) {
887 printed_something = 1;
888 seq_printf(m, "%s,", use->module_which_uses->name);
889 }
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (mod->init != NULL && mod->exit == NULL) {
892 printed_something = 1;
893 seq_printf(m, "[permanent],");
894 }
895
896 if (!printed_something)
897 seq_printf(m, "-");
898}
899
900void __symbol_put(const char *symbol)
901{
902 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Rusty Russell24da1cb2007-07-15 23:41:46 -0700904 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -0500905 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 BUG();
907 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700908 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910EXPORT_SYMBOL(__symbol_put);
911
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930912/* Note this assumes addr is a function, which it currently always is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913void symbol_put_addr(void *addr)
914{
Trent Piepho5e376612006-05-15 09:44:06 -0700915 struct module *modaddr;
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930916 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930918 if (core_kernel_text(a))
Trent Piepho5e376612006-05-15 09:44:06 -0700919 return;
920
Rusty Russella6e6abd2009-03-31 13:05:31 -0600921 /* module_text_address is safe here: we're supposed to have reference
922 * to module from symbol_get, so it can't go away. */
Rusty Russell7d1d16e2009-08-26 22:02:54 +0930923 modaddr = __module_text_address(a);
Rusty Russella6e6abd2009-03-31 13:05:31 -0600924 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -0700925 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927EXPORT_SYMBOL_GPL(symbol_put_addr);
928
929static ssize_t show_refcnt(struct module_attribute *mattr,
930 struct module *mod, char *buffer)
931{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400932 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
935static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900936 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 .show = show_refcnt,
938};
939
Al Virof6a57032006-10-18 01:47:25 -0400940void module_put(struct module *module)
941{
942 if (module) {
943 unsigned int cpu = get_cpu();
Eric Dumazet720eba32009-02-03 13:31:36 +1030944 local_dec(__module_ref_addr(module, cpu));
Al Virof6a57032006-10-18 01:47:25 -0400945 /* Maybe they're waiting for us to drop reference? */
946 if (unlikely(!module_is_live(module)))
947 wake_up_process(module->waiter);
948 put_cpu();
949 }
950}
951EXPORT_SYMBOL(module_put);
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800954static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
956 /* We don't know the usage count, or what modules are using. */
957 seq_printf(m, " - -");
958}
959
960static inline void module_unload_free(struct module *mod)
961{
962}
963
Tim Abbottc6b37802008-12-05 19:03:59 -0500964int use_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500966 return strong_try_module_get(b) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
Tim Abbottc6b37802008-12-05 19:03:59 -0500968EXPORT_SYMBOL_GPL(use_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970static inline void module_unload_init(struct module *mod)
971{
972}
973#endif /* CONFIG_MODULE_UNLOAD */
974
Kay Sievers1f717402006-11-24 12:15:25 +0100975static ssize_t show_initstate(struct module_attribute *mattr,
976 struct module *mod, char *buffer)
977{
978 const char *state = "unknown";
979
980 switch (mod->state) {
981 case MODULE_STATE_LIVE:
982 state = "live";
983 break;
984 case MODULE_STATE_COMING:
985 state = "coming";
986 break;
987 case MODULE_STATE_GOING:
988 state = "going";
989 break;
990 }
991 return sprintf(buffer, "%s\n", state);
992}
993
994static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900995 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100996 .show = show_initstate,
997};
998
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800999static struct module_attribute *modinfo_attrs[] = {
1000 &modinfo_version,
1001 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +01001002 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001003#ifdef CONFIG_MODULE_UNLOAD
1004 &refcnt,
1005#endif
1006 NULL,
1007};
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009static const char vermagic[] = VERMAGIC_STRING;
1010
Rusty Russellc6e665c2009-03-31 13:05:33 -06001011static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -07001012{
1013#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -07001014 if (!test_taint(TAINT_FORCED_MODULE))
Rusty Russellc6e665c2009-03-31 13:05:33 -06001015 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
1016 mod->name, reason);
Linus Torvalds826e4502008-05-04 17:04:16 -07001017 add_taint_module(mod, TAINT_FORCED_MODULE);
1018 return 0;
1019#else
1020 return -ENOEXEC;
1021#endif
1022}
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024#ifdef CONFIG_MODVERSIONS
1025static int check_version(Elf_Shdr *sechdrs,
1026 unsigned int versindex,
1027 const char *symname,
1028 struct module *mod,
1029 const unsigned long *crc)
1030{
1031 unsigned int i, num_versions;
1032 struct modversion_info *versions;
1033
1034 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1035 if (!crc)
1036 return 1;
1037
Rusty Russella5dd6972008-05-09 16:24:21 +10001038 /* No versions at all? modprobe --force does this. */
1039 if (versindex == 0)
1040 return try_to_force_load(mod, symname) == 0;
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 versions = (void *) sechdrs[versindex].sh_addr;
1043 num_versions = sechdrs[versindex].sh_size
1044 / sizeof(struct modversion_info);
1045
1046 for (i = 0; i < num_versions; i++) {
1047 if (strcmp(versions[i].name, symname) != 0)
1048 continue;
1049
1050 if (versions[i].crc == *crc)
1051 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 DEBUGP("Found checksum %lX vs module %lX\n",
1053 *crc, versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001054 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001056
Rusty Russella5dd6972008-05-09 16:24:21 +10001057 printk(KERN_WARNING "%s: no symbol version for %s\n",
1058 mod->name, symname);
1059 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001060
1061bad_version:
1062 printk("%s: disagrees about version of symbol %s\n",
1063 mod->name, symname);
1064 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
1067static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1068 unsigned int versindex,
1069 struct module *mod)
1070{
1071 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Mike Frysinger6560dc12009-07-23 23:42:08 +09301073 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1074 &crc, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 BUG();
Rusty Russell8c8ef422009-03-31 13:05:34 -06001076 return check_version(sechdrs, versindex, "module_layout", mod, crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
Rusty Russell91e37a72008-05-09 16:25:28 +10001079/* First part is kernel version, which we ignore if module has crcs. */
1080static inline int same_magic(const char *amagic, const char *bmagic,
1081 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Rusty Russell91e37a72008-05-09 16:25:28 +10001083 if (has_crcs) {
1084 amagic += strcspn(amagic, " ");
1085 bmagic += strcspn(bmagic, " ");
1086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return strcmp(amagic, bmagic) == 0;
1088}
1089#else
1090static inline int check_version(Elf_Shdr *sechdrs,
1091 unsigned int versindex,
1092 const char *symname,
1093 struct module *mod,
1094 const unsigned long *crc)
1095{
1096 return 1;
1097}
1098
1099static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1100 unsigned int versindex,
1101 struct module *mod)
1102{
1103 return 1;
1104}
1105
Rusty Russell91e37a72008-05-09 16:25:28 +10001106static inline int same_magic(const char *amagic, const char *bmagic,
1107 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
1109 return strcmp(amagic, bmagic) == 0;
1110}
1111#endif /* CONFIG_MODVERSIONS */
1112
1113/* Resolve a symbol for this module. I.e. if we find one, record usage.
1114 Must be holding module_mutex. */
Tim Abbott414fd312008-12-05 19:03:56 -05001115static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
1116 unsigned int versindex,
1117 const char *name,
1118 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
1120 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001121 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 const unsigned long *crc;
1123
Tim Abbott414fd312008-12-05 19:03:56 -05001124 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001125 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Tim Abbott414fd312008-12-05 19:03:56 -05001126 /* use_module can fail due to OOM,
1127 or module initialization or unloading */
1128 if (sym) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1130 !use_module(mod, owner))
Tim Abbott414fd312008-12-05 19:03:56 -05001131 sym = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
Tim Abbott414fd312008-12-05 19:03:56 -05001133 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134}
1135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136/*
1137 * /sys/module/foo/sections stuff
1138 * J. Corbet <corbet@lwn.net>
1139 */
Kay Sievers120fc3d2008-02-21 00:33:20 +01001140#if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
Rusty Russella58730c2008-03-13 09:03:44 +00001141struct module_sect_attr
1142{
1143 struct module_attribute mattr;
1144 char *name;
1145 unsigned long address;
1146};
1147
1148struct module_sect_attrs
1149{
1150 struct attribute_group grp;
1151 unsigned int nsections;
1152 struct module_sect_attr attrs[0];
1153};
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155static ssize_t module_sect_show(struct module_attribute *mattr,
1156 struct module *mod, char *buf)
1157{
1158 struct module_sect_attr *sattr =
1159 container_of(mattr, struct module_sect_attr, mattr);
1160 return sprintf(buf, "0x%lx\n", sattr->address);
1161}
1162
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001163static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1164{
Rusty Russella58730c2008-03-13 09:03:44 +00001165 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001166
1167 for (section = 0; section < sect_attrs->nsections; section++)
1168 kfree(sect_attrs->attrs[section].name);
1169 kfree(sect_attrs);
1170}
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172static void add_sect_attrs(struct module *mod, unsigned int nsect,
1173 char *secstrings, Elf_Shdr *sechdrs)
1174{
1175 unsigned int nloaded = 0, i, size[2];
1176 struct module_sect_attrs *sect_attrs;
1177 struct module_sect_attr *sattr;
1178 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 /* Count loaded sections and allocate structures */
1181 for (i = 0; i < nsect; i++)
1182 if (sechdrs[i].sh_flags & SHF_ALLOC)
1183 nloaded++;
1184 size[0] = ALIGN(sizeof(*sect_attrs)
1185 + nloaded * sizeof(sect_attrs->attrs[0]),
1186 sizeof(sect_attrs->grp.attrs[0]));
1187 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001188 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1189 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return;
1191
1192 /* Setup section attributes. */
1193 sect_attrs->grp.name = "sections";
1194 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1195
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001196 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 sattr = &sect_attrs->attrs[0];
1198 gattr = &sect_attrs->grp.attrs[0];
1199 for (i = 0; i < nsect; i++) {
1200 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1201 continue;
1202 sattr->address = sechdrs[i].sh_addr;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001203 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1204 GFP_KERNEL);
1205 if (sattr->name == NULL)
1206 goto out;
1207 sect_attrs->nsections++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 sattr->mattr.show = module_sect_show;
1209 sattr->mattr.store = NULL;
1210 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 sattr->mattr.attr.mode = S_IRUGO;
1212 *(gattr++) = &(sattr++)->mattr.attr;
1213 }
1214 *gattr = NULL;
1215
1216 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1217 goto out;
1218
1219 mod->sect_attrs = sect_attrs;
1220 return;
1221 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001222 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
1225static void remove_sect_attrs(struct module *mod)
1226{
1227 if (mod->sect_attrs) {
1228 sysfs_remove_group(&mod->mkobj.kobj,
1229 &mod->sect_attrs->grp);
1230 /* We are positive that no one is using any sect attrs
1231 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001232 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 mod->sect_attrs = NULL;
1234 }
1235}
1236
Roland McGrath6d760132007-10-16 23:26:40 -07001237/*
1238 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1239 */
1240
1241struct module_notes_attrs {
1242 struct kobject *dir;
1243 unsigned int notes;
1244 struct bin_attribute attrs[0];
1245};
1246
1247static ssize_t module_notes_read(struct kobject *kobj,
1248 struct bin_attribute *bin_attr,
1249 char *buf, loff_t pos, size_t count)
1250{
1251 /*
1252 * The caller checked the pos and count against our size.
1253 */
1254 memcpy(buf, bin_attr->private + pos, count);
1255 return count;
1256}
1257
1258static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1259 unsigned int i)
1260{
1261 if (notes_attrs->dir) {
1262 while (i-- > 0)
1263 sysfs_remove_bin_file(notes_attrs->dir,
1264 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001265 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001266 }
1267 kfree(notes_attrs);
1268}
1269
1270static void add_notes_attrs(struct module *mod, unsigned int nsect,
1271 char *secstrings, Elf_Shdr *sechdrs)
1272{
1273 unsigned int notes, loaded, i;
1274 struct module_notes_attrs *notes_attrs;
1275 struct bin_attribute *nattr;
1276
1277 /* Count notes sections and allocate structures. */
1278 notes = 0;
1279 for (i = 0; i < nsect; i++)
1280 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1281 (sechdrs[i].sh_type == SHT_NOTE))
1282 ++notes;
1283
1284 if (notes == 0)
1285 return;
1286
1287 notes_attrs = kzalloc(sizeof(*notes_attrs)
1288 + notes * sizeof(notes_attrs->attrs[0]),
1289 GFP_KERNEL);
1290 if (notes_attrs == NULL)
1291 return;
1292
1293 notes_attrs->notes = notes;
1294 nattr = &notes_attrs->attrs[0];
1295 for (loaded = i = 0; i < nsect; ++i) {
1296 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1297 continue;
1298 if (sechdrs[i].sh_type == SHT_NOTE) {
1299 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1300 nattr->attr.mode = S_IRUGO;
1301 nattr->size = sechdrs[i].sh_size;
1302 nattr->private = (void *) sechdrs[i].sh_addr;
1303 nattr->read = module_notes_read;
1304 ++nattr;
1305 }
1306 ++loaded;
1307 }
1308
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001309 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001310 if (!notes_attrs->dir)
1311 goto out;
1312
1313 for (i = 0; i < notes; ++i)
1314 if (sysfs_create_bin_file(notes_attrs->dir,
1315 &notes_attrs->attrs[i]))
1316 goto out;
1317
1318 mod->notes_attrs = notes_attrs;
1319 return;
1320
1321 out:
1322 free_notes_attrs(notes_attrs, i);
1323}
1324
1325static void remove_notes_attrs(struct module *mod)
1326{
1327 if (mod->notes_attrs)
1328 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1329}
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1334 char *sectstrings, Elf_Shdr *sechdrs)
1335{
1336}
1337
1338static inline void remove_sect_attrs(struct module *mod)
1339{
1340}
Roland McGrath6d760132007-10-16 23:26:40 -07001341
1342static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1343 char *sectstrings, Elf_Shdr *sechdrs)
1344{
1345}
1346
1347static inline void remove_notes_attrs(struct module *mod)
1348{
1349}
Kay Sievers120fc3d2008-02-21 00:33:20 +01001350#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Randy Dunlapef665c12007-02-13 15:19:06 -08001352#ifdef CONFIG_SYSFS
1353int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001354{
1355 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001356 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001357 int error = 0;
1358 int i;
1359
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001360 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1361 (ARRAY_SIZE(modinfo_attrs) + 1)),
1362 GFP_KERNEL);
1363 if (!mod->modinfo_attrs)
1364 return -ENOMEM;
1365
1366 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001367 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1368 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001369 (attr->test && attr->test(mod))) {
1370 memcpy(temp_attr, attr, sizeof(*temp_attr));
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001371 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1372 ++temp_attr;
1373 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001374 }
1375 return error;
1376}
1377
Randy Dunlapef665c12007-02-13 15:19:06 -08001378void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001379{
1380 struct module_attribute *attr;
1381 int i;
1382
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001383 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1384 /* pick a field to test for end of list */
1385 if (!attr->attr.name)
1386 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001387 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001388 if (attr->free)
1389 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001390 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001391 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001392}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Randy Dunlapef665c12007-02-13 15:19:06 -08001394int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001397 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001399 if (!module_sysfs_initialized) {
1400 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001401 mod->name);
1402 err = -EINVAL;
1403 goto out;
1404 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001405
1406 kobj = kset_find_obj(module_kset, mod->name);
1407 if (kobj) {
1408 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1409 kobject_put(kobj);
1410 err = -EINVAL;
1411 goto out;
1412 }
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001415
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001416 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1417 mod->mkobj.kobj.kset = module_kset;
1418 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1419 "%s", mod->name);
1420 if (err)
1421 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001422
Kay Sievers97c146e2007-11-29 23:46:11 +01001423 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001424out:
1425 return err;
1426}
1427
Randy Dunlapef665c12007-02-13 15:19:06 -08001428int mod_sysfs_setup(struct module *mod,
Kay Sievers270a6c42007-01-18 13:26:15 +01001429 struct kernel_param *kparam,
1430 unsigned int num_params)
1431{
1432 int err;
1433
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001434 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001435 if (!mod->holders_dir) {
1436 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001437 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001438 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 err = module_param_sysfs_setup(mod, kparam, num_params);
1441 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001442 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Matt Domschc988d2b2005-06-23 22:05:15 -07001444 err = module_add_modinfo_attrs(mod);
1445 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001446 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001447
Kay Sieverse17e0f52006-11-24 12:15:25 +01001448 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return 0;
1450
Kay Sieverse17e0f52006-11-24 12:15:25 +01001451out_unreg_param:
1452 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001453out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001454 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001455out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001456 kobject_put(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return err;
1458}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001459
1460static void mod_sysfs_fini(struct module *mod)
1461{
1462 kobject_put(&mod->mkobj.kobj);
1463}
1464
1465#else /* CONFIG_SYSFS */
1466
1467static void mod_sysfs_fini(struct module *mod)
1468{
1469}
1470
1471#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473static void mod_kobject_remove(struct module *mod)
1474{
Matt Domschc988d2b2005-06-23 22:05:15 -07001475 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001477 kobject_put(mod->mkobj.drivers_dir);
1478 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001479 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
1481
1482/*
1483 * unlink the module with the whole machine is stopped with interrupts off
1484 * - this defends against kallsyms not taking locks
1485 */
1486static int __unlink_module(void *_mod)
1487{
1488 struct module *mod = _mod;
1489 list_del(&mod->list);
1490 return 0;
1491}
1492
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001493/* Free a module, remove from lists, etc (must hold module_mutex). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494static void free_module(struct module *mod)
1495{
1496 /* Delete from various lists */
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001497 stop_machine(__unlink_module, mod, NULL);
Roland McGrath6d760132007-10-16 23:26:40 -07001498 remove_notes_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 remove_sect_attrs(mod);
1500 mod_kobject_remove(mod);
1501
1502 /* Arch-specific cleanup. */
1503 module_arch_cleanup(mod);
1504
1505 /* Module unload stuff */
1506 module_unload_free(mod);
1507
Rusty Russelle180a6b2009-03-31 13:05:29 -06001508 /* Free any allocated parameters. */
1509 destroy_params(mod->kp, mod->num_kp);
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 /* This may be NULL, but that's OK */
1512 module_free(mod, mod->module_init);
1513 kfree(mod->args);
1514 if (mod->percpu)
1515 percpu_modfree(mod->percpu);
Eric Dumazet720eba32009-02-03 13:31:36 +10301516#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
1517 if (mod->refptr)
1518 percpu_modfree(mod->refptr);
1519#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001520 /* Free lock-classes: */
1521 lockdep_free_key_range(mod->module_core, mod->core_size);
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 /* Finally, free the core (containing the module structure) */
1524 module_free(mod, mod->module_core);
1525}
1526
1527void *__symbol_get(const char *symbol)
1528{
1529 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001530 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Rusty Russell24da1cb2007-07-15 23:41:46 -07001532 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001533 sym = find_symbol(symbol, &owner, NULL, true, true);
1534 if (sym && strong_try_module_get(owner))
1535 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001536 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Tim Abbott414fd312008-12-05 19:03:56 -05001538 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540EXPORT_SYMBOL_GPL(__symbol_get);
1541
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001542/*
1543 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001544 * in the kernel or in some other module's exported symbol table.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001545 */
1546static int verify_export_symbols(struct module *mod)
1547{
Rusty Russellb2111042008-05-01 21:15:00 -05001548 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001549 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001550 const struct kernel_symbol *s;
1551 struct {
1552 const struct kernel_symbol *sym;
1553 unsigned int num;
1554 } arr[] = {
1555 { mod->syms, mod->num_syms },
1556 { mod->gpl_syms, mod->num_gpl_syms },
1557 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001558#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001559 { mod->unused_syms, mod->num_unused_syms },
1560 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001561#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001562 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001563
Rusty Russellb2111042008-05-01 21:15:00 -05001564 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1565 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Tim Abbott414fd312008-12-05 19:03:56 -05001566 if (find_symbol(s->name, &owner, NULL, true, false)) {
Rusty Russellb2111042008-05-01 21:15:00 -05001567 printk(KERN_ERR
1568 "%s: exports duplicate symbol %s"
1569 " (owned by %s)\n",
1570 mod->name, s->name, module_name(owner));
1571 return -ENOEXEC;
1572 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001573 }
Rusty Russellb2111042008-05-01 21:15:00 -05001574 }
1575 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001576}
1577
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001578/* Change all symbols so that st_value encodes the pointer directly. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579static int simplify_symbols(Elf_Shdr *sechdrs,
1580 unsigned int symindex,
1581 const char *strtab,
1582 unsigned int versindex,
1583 unsigned int pcpuindex,
1584 struct module *mod)
1585{
1586 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1587 unsigned long secbase;
1588 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1589 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05001590 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 for (i = 1; i < n; i++) {
1593 switch (sym[i].st_shndx) {
1594 case SHN_COMMON:
1595 /* We compiled with -fno-common. These are not
1596 supposed to happen. */
1597 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1598 printk("%s: please compile with -fno-common\n",
1599 mod->name);
1600 ret = -ENOEXEC;
1601 break;
1602
1603 case SHN_ABS:
1604 /* Don't need to do anything */
1605 DEBUGP("Absolute symbol: 0x%08lx\n",
1606 (long)sym[i].st_value);
1607 break;
1608
1609 case SHN_UNDEF:
Tim Abbott414fd312008-12-05 19:03:56 -05001610 ksym = resolve_symbol(sechdrs, versindex,
1611 strtab + sym[i].st_name, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 /* Ok if resolved. */
Tim Abbott414fd312008-12-05 19:03:56 -05001613 if (ksym) {
1614 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 break;
Tim Abbott414fd312008-12-05 19:03:56 -05001616 }
1617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 /* Ok if weak. */
1619 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1620 break;
1621
1622 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1623 mod->name, strtab + sym[i].st_name);
1624 ret = -ENOENT;
1625 break;
1626
1627 default:
1628 /* Divert to percpu allocation if a percpu var. */
1629 if (sym[i].st_shndx == pcpuindex)
1630 secbase = (unsigned long)mod->percpu;
1631 else
1632 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1633 sym[i].st_value += secbase;
1634 break;
1635 }
1636 }
1637
1638 return ret;
1639}
1640
Helge Deller088af9a2008-12-31 12:31:18 +01001641/* Additional bytes needed by arch in front of individual sections */
1642unsigned int __weak arch_mod_section_prepend(struct module *mod,
1643 unsigned int section)
1644{
1645 /* default implementation just returns zero */
1646 return 0;
1647}
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01001650static long get_offset(struct module *mod, unsigned int *size,
1651 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
1653 long ret;
1654
Helge Deller088af9a2008-12-31 12:31:18 +01001655 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1657 *size = ret + sechdr->sh_size;
1658 return ret;
1659}
1660
1661/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1662 might -- code, read-only data, read-write data, small data. Tally
1663 sizes, and place the offsets into sh_entsize fields: high bit means it
1664 belongs in init. */
1665static void layout_sections(struct module *mod,
1666 const Elf_Ehdr *hdr,
1667 Elf_Shdr *sechdrs,
1668 const char *secstrings)
1669{
1670 static unsigned long const masks[][2] = {
1671 /* NOTE: all executable code must be the first section
1672 * in this array; otherwise modify the text_size
1673 * finder in the two loops below */
1674 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1675 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1676 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1677 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1678 };
1679 unsigned int m, i;
1680
1681 for (i = 0; i < hdr->e_shnum; i++)
1682 sechdrs[i].sh_entsize = ~0UL;
1683
1684 DEBUGP("Core section allocation order:\n");
1685 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1686 for (i = 0; i < hdr->e_shnum; ++i) {
1687 Elf_Shdr *s = &sechdrs[i];
1688
1689 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1690 || (s->sh_flags & masks[m][1])
1691 || s->sh_entsize != ~0UL
Rusty Russell49502672009-03-31 13:05:36 -06001692 || strstarts(secstrings + s->sh_name, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001694 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 DEBUGP("\t%s\n", secstrings + s->sh_name);
1696 }
1697 if (m == 0)
1698 mod->core_text_size = mod->core_size;
1699 }
1700
1701 DEBUGP("Init section allocation order:\n");
1702 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1703 for (i = 0; i < hdr->e_shnum; ++i) {
1704 Elf_Shdr *s = &sechdrs[i];
1705
1706 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1707 || (s->sh_flags & masks[m][1])
1708 || s->sh_entsize != ~0UL
Rusty Russell49502672009-03-31 13:05:36 -06001709 || !strstarts(secstrings + s->sh_name, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001711 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 | INIT_OFFSET_MASK);
1713 DEBUGP("\t%s\n", secstrings + s->sh_name);
1714 }
1715 if (m == 0)
1716 mod->init_text_size = mod->init_size;
1717 }
1718}
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720static void set_license(struct module *mod, const char *license)
1721{
1722 if (!license)
1723 license = "unspecified";
1724
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001725 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07001726 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001727 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001728 "kernel.\n", mod->name, license);
1729 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 }
1731}
1732
1733/* Parse tag=value strings from .modinfo section */
1734static char *next_string(char *string, unsigned long *secsize)
1735{
1736 /* Skip non-zero chars */
1737 while (string[0]) {
1738 string++;
1739 if ((*secsize)-- <= 1)
1740 return NULL;
1741 }
1742
1743 /* Skip any zero padding. */
1744 while (!string[0]) {
1745 string++;
1746 if ((*secsize)-- <= 1)
1747 return NULL;
1748 }
1749 return string;
1750}
1751
1752static char *get_modinfo(Elf_Shdr *sechdrs,
1753 unsigned int info,
1754 const char *tag)
1755{
1756 char *p;
1757 unsigned int taglen = strlen(tag);
1758 unsigned long size = sechdrs[info].sh_size;
1759
1760 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1761 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1762 return p + taglen + 1;
1763 }
1764 return NULL;
1765}
1766
Matt Domschc988d2b2005-06-23 22:05:15 -07001767static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1768 unsigned int infoindex)
1769{
1770 struct module_attribute *attr;
1771 int i;
1772
1773 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1774 if (attr->setup)
1775 attr->setup(mod,
1776 get_modinfo(sechdrs,
1777 infoindex,
1778 attr->attr.name));
1779 }
1780}
Matt Domschc988d2b2005-06-23 22:05:15 -07001781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01001783
1784/* lookup symbol in given range of kernel_symbols */
1785static const struct kernel_symbol *lookup_symbol(const char *name,
1786 const struct kernel_symbol *start,
1787 const struct kernel_symbol *stop)
1788{
1789 const struct kernel_symbol *ks = start;
1790 for (; ks < stop; ks++)
1791 if (strcmp(ks->name, name) == 0)
1792 return ks;
1793 return NULL;
1794}
1795
Tim Abbottca4787b2009-01-05 08:40:10 -06001796static int is_exported(const char *name, unsigned long value,
1797 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
Tim Abbottca4787b2009-01-05 08:40:10 -06001799 const struct kernel_symbol *ks;
1800 if (!mod)
1801 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001802 else
Tim Abbottca4787b2009-01-05 08:40:10 -06001803 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
1804 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
1806
1807/* As per nm */
1808static char elf_type(const Elf_Sym *sym,
1809 Elf_Shdr *sechdrs,
1810 const char *secstrings,
1811 struct module *mod)
1812{
1813 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1814 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1815 return 'v';
1816 else
1817 return 'w';
1818 }
1819 if (sym->st_shndx == SHN_UNDEF)
1820 return 'U';
1821 if (sym->st_shndx == SHN_ABS)
1822 return 'a';
1823 if (sym->st_shndx >= SHN_LORESERVE)
1824 return '?';
1825 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1826 return 't';
1827 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1828 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1829 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1830 return 'r';
1831 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1832 return 'g';
1833 else
1834 return 'd';
1835 }
1836 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1837 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1838 return 's';
1839 else
1840 return 'b';
1841 }
Rusty Russell49502672009-03-31 13:05:36 -06001842 if (strstarts(secstrings + sechdrs[sym->st_shndx].sh_name, ".debug"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 return 'n';
1844 return '?';
1845}
1846
1847static void add_kallsyms(struct module *mod,
1848 Elf_Shdr *sechdrs,
1849 unsigned int symindex,
1850 unsigned int strindex,
1851 const char *secstrings)
1852{
1853 unsigned int i;
1854
1855 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1856 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1857 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1858
1859 /* Set types up while we still have access to sections. */
1860 for (i = 0; i < mod->num_symtab; i++)
1861 mod->symtab[i].st_info
1862 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1863}
1864#else
1865static inline void add_kallsyms(struct module *mod,
1866 Elf_Shdr *sechdrs,
1867 unsigned int symindex,
1868 unsigned int strindex,
1869 const char *secstrings)
1870{
1871}
1872#endif /* CONFIG_KALLSYMS */
1873
Jason Barone9d376f2009-02-05 11:51:38 -05001874static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05001875{
Jason Barone9d376f2009-02-05 11:51:38 -05001876#ifdef CONFIG_DYNAMIC_DEBUG
1877 if (ddebug_add_module(debug, num, debug->modname))
1878 printk(KERN_ERR "dynamic debug error adding module: %s\n",
1879 debug->modname);
1880#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05001881}
Jason Baron346e15b2008-08-12 16:46:19 -04001882
Rusty Russell3a642e92008-07-22 19:24:28 -05001883static void *module_alloc_update_bounds(unsigned long size)
1884{
1885 void *ret = module_alloc(size);
1886
1887 if (ret) {
1888 /* Update module bounds. */
1889 if ((unsigned long)ret < module_addr_min)
1890 module_addr_min = (unsigned long)ret;
1891 if ((unsigned long)ret + size > module_addr_max)
1892 module_addr_max = (unsigned long)ret + size;
1893 }
1894 return ret;
1895}
1896
Catalin Marinas4f2294b2009-06-11 13:23:20 +01001897#ifdef CONFIG_DEBUG_KMEMLEAK
1898static void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
1899 Elf_Shdr *sechdrs, char *secstrings)
1900{
1901 unsigned int i;
1902
1903 /* only scan the sections containing data */
1904 kmemleak_scan_area(mod->module_core, (unsigned long)mod -
1905 (unsigned long)mod->module_core,
1906 sizeof(struct module), GFP_KERNEL);
1907
1908 for (i = 1; i < hdr->e_shnum; i++) {
1909 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1910 continue;
1911 if (strncmp(secstrings + sechdrs[i].sh_name, ".data", 5) != 0
1912 && strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) != 0)
1913 continue;
1914
1915 kmemleak_scan_area(mod->module_core, sechdrs[i].sh_addr -
1916 (unsigned long)mod->module_core,
1917 sechdrs[i].sh_size, GFP_KERNEL);
1918 }
1919}
1920#else
1921static inline void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
1922 Elf_Shdr *sechdrs, char *secstrings)
1923{
1924}
1925#endif
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927/* Allocate and load the module: note that size of section 0 is always
1928 zero, and we rely on this for optional sections. */
Linus Torvaldsffb4ba72008-08-25 11:10:26 -07001929static noinline struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 unsigned long len,
1931 const char __user *uargs)
1932{
1933 Elf_Ehdr *hdr;
1934 Elf_Shdr *sechdrs;
1935 char *secstrings, *args, *modmagic, *strtab = NULL;
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07001936 char *staging;
Andrew Morton84860f92006-06-28 04:26:46 -07001937 unsigned int i;
1938 unsigned int symindex = 0;
1939 unsigned int strindex = 0;
Rusty Russell5e458cc2008-10-22 10:00:13 -05001940 unsigned int modindex, versindex, infoindex, pcpuindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 struct module *mod;
1942 long err = 0;
1943 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
Thomas Koeller378bac82005-09-06 15:17:11 -07001944 mm_segment_t old_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1947 umod, len, uargs);
1948 if (len < sizeof(*hdr))
1949 return ERR_PTR(-ENOEXEC);
1950
1951 /* Suck in entire file: we'll want most of it. */
1952 /* vmalloc barfs on "unusual" numbers. Check here */
1953 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1954 return ERR_PTR(-ENOMEM);
Heiko Carstens9e018922008-12-22 12:36:31 +01001955
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 if (copy_from_user(hdr, umod, len) != 0) {
1957 err = -EFAULT;
1958 goto free_hdr;
1959 }
1960
1961 /* Sanity checks against insmoding binaries or wrong arch,
1962 weird elf version */
Cyrill Gorcunovc4ea6fc2008-05-14 16:27:29 -07001963 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 || hdr->e_type != ET_REL
1965 || !elf_check_arch(hdr)
1966 || hdr->e_shentsize != sizeof(*sechdrs)) {
1967 err = -ENOEXEC;
1968 goto free_hdr;
1969 }
1970
1971 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1972 goto truncated;
1973
1974 /* Convenience variables */
1975 sechdrs = (void *)hdr + hdr->e_shoff;
1976 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1977 sechdrs[0].sh_addr = 0;
1978
1979 for (i = 1; i < hdr->e_shnum; i++) {
1980 if (sechdrs[i].sh_type != SHT_NOBITS
1981 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1982 goto truncated;
1983
1984 /* Mark all sections sh_addr with their address in the
1985 temporary image. */
1986 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1987
1988 /* Internal symbols and strings. */
1989 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1990 symindex = i;
1991 strindex = sechdrs[i].sh_link;
1992 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1993 }
1994#ifndef CONFIG_MODULE_UNLOAD
1995 /* Don't load .exit sections */
Rusty Russell49502672009-03-31 13:05:36 -06001996 if (strstarts(secstrings+sechdrs[i].sh_name, ".exit"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1998#endif
1999 }
2000
2001 modindex = find_sec(hdr, sechdrs, secstrings,
2002 ".gnu.linkonce.this_module");
2003 if (!modindex) {
2004 printk(KERN_WARNING "No module found in object\n");
2005 err = -ENOEXEC;
2006 goto free_hdr;
2007 }
Rusty Russell5e458cc2008-10-22 10:00:13 -05002008 /* This is temporary: point mod into copy of data. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 mod = (void *)sechdrs[modindex].sh_addr;
2010
2011 if (symindex == 0) {
2012 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2013 mod->name);
2014 err = -ENOEXEC;
2015 goto free_hdr;
2016 }
2017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
2019 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
2020 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
2021
Rusty Russellea01e792008-03-13 09:02:17 +00002022 /* Don't keep modinfo and version sections. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russellea01e792008-03-13 09:02:17 +00002024 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025#ifdef CONFIG_KALLSYMS
2026 /* Keep symbol and string tables for decoding later. */
2027 sechdrs[symindex].sh_flags |= SHF_ALLOC;
2028 sechdrs[strindex].sh_flags |= SHF_ALLOC;
2029#endif
2030
2031 /* Check module struct version now, before we try to use module. */
2032 if (!check_modstruct_version(sechdrs, versindex, mod)) {
2033 err = -ENOEXEC;
2034 goto free_hdr;
2035 }
2036
2037 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
2038 /* This is allowed: modprobe --force will invalidate it. */
2039 if (!modmagic) {
Rusty Russellc6e665c2009-03-31 13:05:33 -06002040 err = try_to_force_load(mod, "bad vermagic");
Linus Torvalds826e4502008-05-04 17:04:16 -07002041 if (err)
2042 goto free_hdr;
Rusty Russell91e37a72008-05-09 16:25:28 +10002043 } else if (!same_magic(modmagic, vermagic, versindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2045 mod->name, modmagic, vermagic);
2046 err = -ENOEXEC;
2047 goto free_hdr;
2048 }
2049
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002050 staging = get_modinfo(sechdrs, infoindex, "staging");
2051 if (staging) {
2052 add_taint_module(mod, TAINT_CRAP);
2053 printk(KERN_WARNING "%s: module is from the staging directory,"
2054 " the quality is unknown, you have been warned.\n",
2055 mod->name);
2056 }
2057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 /* Now copy in args */
Davi Arnaut24277dd2006-03-24 03:18:43 -08002059 args = strndup_user(uargs, ~0UL >> 1);
2060 if (IS_ERR(args)) {
2061 err = PTR_ERR(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 goto free_hdr;
2063 }
Andrew Morton8e08b752006-02-07 12:58:45 -08002064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 if (find_module(mod->name)) {
2066 err = -EEXIST;
2067 goto free_mod;
2068 }
2069
2070 mod->state = MODULE_STATE_COMING;
2071
2072 /* Allow arches to frob section contents and sizes. */
2073 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
2074 if (err < 0)
2075 goto free_mod;
2076
2077 if (pcpuindex) {
2078 /* We have a special allocation for this section. */
2079 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
Rusty Russell842bbaa2005-08-01 21:11:47 -07002080 sechdrs[pcpuindex].sh_addralign,
2081 mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 if (!percpu) {
2083 err = -ENOMEM;
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002084 goto free_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 }
2086 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2087 mod->percpu = percpu;
2088 }
2089
2090 /* Determine total sizes, and put offsets in sh_entsize. For now
2091 this is done generically; there doesn't appear to be any
2092 special cases for the architectures. */
2093 layout_sections(mod, hdr, sechdrs, secstrings);
2094
2095 /* Do the allocs. */
Rusty Russell3a642e92008-07-22 19:24:28 -05002096 ptr = module_alloc_update_bounds(mod->core_size);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002097 /*
2098 * The pointer to this block is stored in the module structure
2099 * which is inside the block. Just mark it as not being a
2100 * leak.
2101 */
2102 kmemleak_not_leak(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 if (!ptr) {
2104 err = -ENOMEM;
2105 goto free_percpu;
2106 }
2107 memset(ptr, 0, mod->core_size);
2108 mod->module_core = ptr;
2109
Rusty Russell3a642e92008-07-22 19:24:28 -05002110 ptr = module_alloc_update_bounds(mod->init_size);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002111 /*
2112 * The pointer to this block is stored in the module structure
2113 * which is inside the block. This block doesn't need to be
2114 * scanned as it contains data and code that will be freed
2115 * after the module is initialized.
2116 */
2117 kmemleak_ignore(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 if (!ptr && mod->init_size) {
2119 err = -ENOMEM;
2120 goto free_core;
2121 }
2122 memset(ptr, 0, mod->init_size);
2123 mod->module_init = ptr;
2124
2125 /* Transfer each section which specifies SHF_ALLOC */
2126 DEBUGP("final section addresses:\n");
2127 for (i = 0; i < hdr->e_shnum; i++) {
2128 void *dest;
2129
2130 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2131 continue;
2132
2133 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2134 dest = mod->module_init
2135 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2136 else
2137 dest = mod->module_core + sechdrs[i].sh_entsize;
2138
2139 if (sechdrs[i].sh_type != SHT_NOBITS)
2140 memcpy(dest, (void *)sechdrs[i].sh_addr,
2141 sechdrs[i].sh_size);
2142 /* Update sh_addr to point to copy in image. */
2143 sechdrs[i].sh_addr = (unsigned long)dest;
2144 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2145 }
2146 /* Module has been moved. */
2147 mod = (void *)sechdrs[modindex].sh_addr;
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002148 kmemleak_load_module(mod, hdr, sechdrs, secstrings);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002150#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2151 mod->refptr = percpu_modalloc(sizeof(local_t), __alignof__(local_t),
2152 mod->name);
2153 if (!mod->refptr) {
2154 err = -ENOMEM;
2155 goto free_init;
2156 }
2157#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 /* Now we've moved module, initialize linked lists, etc. */
2159 module_unload_init(mod);
2160
Kay Sievers97c146e2007-11-29 23:46:11 +01002161 /* add kobject, so we can reference it. */
Akinobu Mitad58ae672007-10-16 23:30:27 -07002162 err = mod_sysfs_init(mod);
2163 if (err)
Kay Sievers97c146e2007-11-29 23:46:11 +01002164 goto free_unload;
Kay Sievers270a6c42007-01-18 13:26:15 +01002165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 /* Set up license info based on the info section */
2167 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2168
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002169 /*
2170 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2171 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2172 * using GPL-only symbols it needs.
2173 */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002174 if (strcmp(mod->name, "ndiswrapper") == 0)
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002175 add_taint(TAINT_PROPRIETARY_MODULE);
2176
2177 /* driverloader was caught wrongly pretending to be under GPL */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002178 if (strcmp(mod->name, "driverloader") == 0)
2179 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Dave Jones9841d61d2006-01-08 01:03:41 -08002180
Matt Domschc988d2b2005-06-23 22:05:15 -07002181 /* Set up MODINFO_ATTR fields */
2182 setup_modinfo(mod, sechdrs, infoindex);
Matt Domschc988d2b2005-06-23 22:05:15 -07002183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 /* Fix up syms, so that st_value is a pointer to location. */
2185 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2186 mod);
2187 if (err < 0)
2188 goto cleanup;
2189
Rusty Russell5e458cc2008-10-22 10:00:13 -05002190 /* Now we've got everything in the final locations, we can
2191 * find optional sections. */
Rusty Russelle180a6b2009-03-31 13:05:29 -06002192 mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
2193 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell5e458cc2008-10-22 10:00:13 -05002194 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2195 sizeof(*mod->syms), &mod->num_syms);
2196 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2197 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2198 sizeof(*mod->gpl_syms),
2199 &mod->num_gpl_syms);
2200 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2201 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2202 "__ksymtab_gpl_future",
2203 sizeof(*mod->gpl_future_syms),
2204 &mod->num_gpl_future_syms);
2205 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2206 "__kcrctab_gpl_future");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002208#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002209 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2210 "__ksymtab_unused",
2211 sizeof(*mod->unused_syms),
2212 &mod->num_unused_syms);
2213 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2214 "__kcrctab_unused");
2215 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2216 "__ksymtab_unused_gpl",
2217 sizeof(*mod->unused_gpl_syms),
2218 &mod->num_unused_gpl_syms);
2219 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2220 "__kcrctab_unused_gpl");
2221#endif
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002222#ifdef CONFIG_CONSTRUCTORS
2223 mod->ctors = section_objs(hdr, sechdrs, secstrings, ".ctors",
2224 sizeof(*mod->ctors), &mod->num_ctors);
2225#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002226
2227#ifdef CONFIG_MARKERS
2228 mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers",
2229 sizeof(*mod->markers), &mod->num_markers);
2230#endif
2231#ifdef CONFIG_TRACEPOINTS
2232 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2233 "__tracepoints",
2234 sizeof(*mod->tracepoints),
2235 &mod->num_tracepoints);
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002236#endif
Steven Rostedt6d723732009-04-10 14:53:50 -04002237#ifdef CONFIG_EVENT_TRACING
2238 mod->trace_events = section_objs(hdr, sechdrs, secstrings,
2239 "_ftrace_events",
2240 sizeof(*mod->trace_events),
2241 &mod->num_trace_events);
2242#endif
Steven Rostedt93eb6772009-04-15 13:24:06 -04002243#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2244 /* sechdrs[0].sh_size is always zero */
2245 mod->ftrace_callsites = section_objs(hdr, sechdrs, secstrings,
2246 "__mcount_loc",
2247 sizeof(*mod->ftrace_callsites),
2248 &mod->num_ftrace_callsites);
2249#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250#ifdef CONFIG_MODVERSIONS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002251 if ((mod->num_syms && !mod->crcs)
2252 || (mod->num_gpl_syms && !mod->gpl_crcs)
2253 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002254#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002255 || (mod->num_unused_syms && !mod->unused_crcs)
2256 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002257#endif
2258 ) {
Rusty Russellc6e665c2009-03-31 13:05:33 -06002259 err = try_to_force_load(mod,
2260 "no versions for exported symbols");
Linus Torvalds826e4502008-05-04 17:04:16 -07002261 if (err)
2262 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 }
2264#endif
2265
2266 /* Now do relocations. */
2267 for (i = 1; i < hdr->e_shnum; i++) {
2268 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2269 unsigned int info = sechdrs[i].sh_info;
2270
2271 /* Not a valid relocation section? */
2272 if (info >= hdr->e_shnum)
2273 continue;
2274
2275 /* Don't bother with non-allocated sections */
2276 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2277 continue;
2278
2279 if (sechdrs[i].sh_type == SHT_REL)
2280 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2281 else if (sechdrs[i].sh_type == SHT_RELA)
2282 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2283 mod);
2284 if (err < 0)
2285 goto cleanup;
2286 }
2287
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002288 /* Find duplicate symbols */
2289 err = verify_export_symbols(mod);
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002290 if (err < 0)
2291 goto cleanup;
2292
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 /* Set up and sort exception table */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002294 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2295 sizeof(*mod->extable), &mod->num_exentries);
2296 sort_extable(mod->extable, mod->extable + mod->num_exentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
2298 /* Finally, copy percpu area over. */
2299 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2300 sechdrs[pcpuindex].sh_size);
2301
2302 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2303
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002304 if (!mod->taints) {
Jason Barone9d376f2009-02-05 11:51:38 -05002305 struct _ddebug *debug;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002306 unsigned int num_debug;
2307
Rusty Russell5e458cc2008-10-22 10:00:13 -05002308 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2309 sizeof(*debug), &num_debug);
Jason Barone9d376f2009-02-05 11:51:38 -05002310 if (debug)
2311 dynamic_debug_setup(debug, num_debug);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002312 }
Steven Rostedt90d595f2008-08-14 15:45:09 -04002313
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 err = module_finalize(hdr, sechdrs, mod);
2315 if (err < 0)
2316 goto cleanup;
2317
Thomas Koeller378bac82005-09-06 15:17:11 -07002318 /* flush the icache in correct context */
2319 old_fs = get_fs();
2320 set_fs(KERNEL_DS);
2321
2322 /*
2323 * Flush the instruction cache, since we've played with text.
2324 * Do it before processing of module parameters, so the module
2325 * can provide parameter accessor functions of its own.
2326 */
2327 if (mod->module_init)
2328 flush_icache_range((unsigned long)mod->module_init,
2329 (unsigned long)mod->module_init
2330 + mod->init_size);
2331 flush_icache_range((unsigned long)mod->module_core,
2332 (unsigned long)mod->module_core + mod->core_size);
2333
2334 set_fs(old_fs);
2335
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 mod->args = args;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002337 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002338 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2339 mod->name);
2340
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002341 /* Now sew it into the lists so we can get lockdep and oops
Andi Kleend72b3752008-08-30 10:09:00 +02002342 * info during argument parsing. Noone should access us, since
2343 * strong_try_module_get() will fail.
2344 * lockdep/oops can run asynchronous, so use the RCU list insertion
2345 * function to insert in a way safe to concurrent readers.
2346 * The mutex protects against concurrent writers.
2347 */
2348 list_add_rcu(&mod->list, &modules);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002349
Rusty Russelle180a6b2009-03-31 13:05:29 -06002350 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002352 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
Rusty Russelle180a6b2009-03-31 13:05:29 -06002354 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002356 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Roland McGrath6d760132007-10-16 23:26:40 -07002358 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
2360 /* Get rid of temporary copy */
2361 vfree(hdr);
2362
2363 /* Done! */
2364 return mod;
2365
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002366 unlink:
Rusty Russelle91defa2009-03-31 13:05:35 -06002367 /* Unlink carefully: kallsyms could be walking list. */
2368 list_del_rcu(&mod->list);
2369 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 module_arch_cleanup(mod);
2371 cleanup:
Kay Sievers97c146e2007-11-29 23:46:11 +01002372 kobject_del(&mod->mkobj.kobj);
2373 kobject_put(&mod->mkobj.kobj);
2374 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 module_unload_free(mod);
Eric Dumazet720eba32009-02-03 13:31:36 +10302376#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
Américo Wangb10153f2009-03-25 00:07:19 +08002377 free_init:
Eric Dumazet720eba32009-02-03 13:31:36 +10302378 percpu_modfree(mod->refptr);
2379#endif
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002380 module_free(mod, mod->module_init);
2381 free_core:
2382 module_free(mod, mod->module_core);
2383 /* mod will be freed with core. Don't access it beyond this line! */
2384 free_percpu:
2385 if (percpu)
2386 percpu_modfree(percpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 free_mod:
2388 kfree(args);
2389 free_hdr:
2390 vfree(hdr);
Jayachandran C6fe2e702006-01-06 00:19:54 -08002391 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
2393 truncated:
2394 printk(KERN_ERR "Module len %lu truncated\n", len);
2395 err = -ENOEXEC;
2396 goto free_hdr;
2397}
2398
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002399/* Call module constructors. */
2400static void do_mod_ctors(struct module *mod)
2401{
2402#ifdef CONFIG_CONSTRUCTORS
2403 unsigned long i;
2404
2405 for (i = 0; i < mod->num_ctors; i++)
2406 mod->ctors[i]();
2407#endif
2408}
2409
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410/* This is where the real work happens */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002411SYSCALL_DEFINE3(init_module, void __user *, umod,
2412 unsigned long, len, const char __user *, uargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
2414 struct module *mod;
2415 int ret = 0;
2416
2417 /* Must have permission */
Kees Cook3d433212009-04-02 15:49:29 -07002418 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 return -EPERM;
2420
2421 /* Only one module load at a time, please */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002422 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 return -EINTR;
2424
2425 /* Do all the hard work */
2426 mod = load_module(umod, len, uargs);
2427 if (IS_ERR(mod)) {
Ashutosh Naik6389a382006-03-23 03:00:46 -08002428 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 return PTR_ERR(mod);
2430 }
2431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 /* Drop lock so they can recurse */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002433 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
Alan Sterne041c682006-03-27 01:16:30 -08002435 blocking_notifier_call_chain(&module_notify_list,
2436 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002438 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 /* Start the module */
2440 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002441 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 if (ret < 0) {
2443 /* Init routine failed: abort. Try to protect us from
2444 buggy refcounters. */
2445 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002446 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002447 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002448 blocking_notifier_call_chain(&module_notify_list,
2449 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002450 mutex_lock(&module_mutex);
2451 free_module(mod);
2452 mutex_unlock(&module_mutex);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002453 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 return ret;
2455 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002456 if (ret > 0) {
Joe Perchesad361c92009-07-06 13:05:40 -07002457 printk(KERN_WARNING
2458"%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
2459"%s: loading module anyway...\n",
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002460 __func__, mod->name, ret,
2461 __func__);
2462 dump_stack();
2463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Rusty Russell6c5db222008-03-10 11:43:52 -07002465 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002467 wake_up(&module_wq);
Masami Hiramatsu0deddf432009-01-06 14:41:54 -08002468 blocking_notifier_call_chain(&module_notify_list,
2469 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07002470
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07002471 /* We need to finish all async code before the module init sequence is done */
2472 async_synchronize_full();
2473
Rusty Russell6c5db222008-03-10 11:43:52 -07002474 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 /* Drop initial reference. */
2476 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06002477 trim_init_extable(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 module_free(mod, mod->module_init);
2479 mod->module_init = NULL;
2480 mod->init_size = 0;
2481 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002482 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
2484 return 0;
2485}
2486
2487static inline int within(unsigned long addr, void *start, unsigned long size)
2488{
2489 return ((void *)addr >= start && (void *)addr < start + size);
2490}
2491
2492#ifdef CONFIG_KALLSYMS
2493/*
2494 * This ignores the intensely annoying "mapping symbols" found
2495 * in ARM ELF files: $a, $t and $d.
2496 */
2497static inline int is_arm_mapping_symbol(const char *str)
2498{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002499 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 && (str[2] == '\0' || str[2] == '.');
2501}
2502
2503static const char *get_ksymbol(struct module *mod,
2504 unsigned long addr,
2505 unsigned long *size,
2506 unsigned long *offset)
2507{
2508 unsigned int i, best = 0;
2509 unsigned long nextval;
2510
2511 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002512 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002514 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2516
2517 /* Scan for closest preceeding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002518 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 for (i = 1; i < mod->num_symtab; i++) {
2520 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2521 continue;
2522
2523 /* We ignore unnamed symbols: they're uninformative
2524 * and inserted at a whim. */
2525 if (mod->symtab[i].st_value <= addr
2526 && mod->symtab[i].st_value > mod->symtab[best].st_value
2527 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2528 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2529 best = i;
2530 if (mod->symtab[i].st_value > addr
2531 && mod->symtab[i].st_value < nextval
2532 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2533 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2534 nextval = mod->symtab[i].st_value;
2535 }
2536
2537 if (!best)
2538 return NULL;
2539
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002540 if (size)
2541 *size = nextval - mod->symtab[best].st_value;
2542 if (offset)
2543 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 return mod->strtab + mod->symtab[best].st_name;
2545}
2546
Rusty Russell6dd06c92008-01-29 17:13:22 -05002547/* For kallsyms to ask for address resolution. NULL means not found. Careful
2548 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002549const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05002550 unsigned long *size,
2551 unsigned long *offset,
2552 char **modname,
2553 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554{
2555 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002556 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
Rusty Russellcb2a5202008-01-14 00:55:03 -08002558 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002559 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002560 if (within_module_init(addr, mod) ||
2561 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002562 if (modname)
2563 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002564 ret = get_ksymbol(mod, addr, size, offset);
2565 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 }
2567 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05002568 /* Make a copy in here where it's safe */
2569 if (ret) {
2570 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2571 ret = namebuf;
2572 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002573 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002574 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575}
2576
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002577int lookup_module_symbol_name(unsigned long addr, char *symname)
2578{
2579 struct module *mod;
2580
Rusty Russellcb2a5202008-01-14 00:55:03 -08002581 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002582 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002583 if (within_module_init(addr, mod) ||
2584 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002585 const char *sym;
2586
2587 sym = get_ksymbol(mod, addr, NULL, NULL);
2588 if (!sym)
2589 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07002590 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002591 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002592 return 0;
2593 }
2594 }
2595out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002596 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002597 return -ERANGE;
2598}
2599
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002600int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2601 unsigned long *offset, char *modname, char *name)
2602{
2603 struct module *mod;
2604
Rusty Russellcb2a5202008-01-14 00:55:03 -08002605 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002606 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002607 if (within_module_init(addr, mod) ||
2608 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002609 const char *sym;
2610
2611 sym = get_ksymbol(mod, addr, size, offset);
2612 if (!sym)
2613 goto out;
2614 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07002615 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002616 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07002617 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002618 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002619 return 0;
2620 }
2621 }
2622out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002623 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002624 return -ERANGE;
2625}
2626
Alexey Dobriyanea078902007-05-08 00:28:39 -07002627int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2628 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629{
2630 struct module *mod;
2631
Rusty Russellcb2a5202008-01-14 00:55:03 -08002632 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002633 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 if (symnum < mod->num_symtab) {
2635 *value = mod->symtab[symnum].st_value;
2636 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002637 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07002638 KSYM_NAME_LEN);
2639 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06002640 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002641 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002642 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 }
2644 symnum -= mod->num_symtab;
2645 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002646 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002647 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648}
2649
2650static unsigned long mod_find_symname(struct module *mod, const char *name)
2651{
2652 unsigned int i;
2653
2654 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002655 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2656 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 return mod->symtab[i].st_value;
2658 return 0;
2659}
2660
2661/* Look for this name: can be of form module:name. */
2662unsigned long module_kallsyms_lookup_name(const char *name)
2663{
2664 struct module *mod;
2665 char *colon;
2666 unsigned long ret = 0;
2667
2668 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08002669 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 if ((colon = strchr(name, ':')) != NULL) {
2671 *colon = '\0';
2672 if ((mod = find_module(name)) != NULL)
2673 ret = mod_find_symname(mod, colon+1);
2674 *colon = ':';
2675 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02002676 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 if ((ret = mod_find_symname(mod, name)) != 0)
2678 break;
2679 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002680 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 return ret;
2682}
Anders Kaseorg75a66612008-12-05 19:03:58 -05002683
2684int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
2685 struct module *, unsigned long),
2686 void *data)
2687{
2688 struct module *mod;
2689 unsigned int i;
2690 int ret;
2691
2692 list_for_each_entry(mod, &modules, list) {
2693 for (i = 0; i < mod->num_symtab; i++) {
2694 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
2695 mod, mod->symtab[i].st_value);
2696 if (ret != 0)
2697 return ret;
2698 }
2699 }
2700 return 0;
2701}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702#endif /* CONFIG_KALLSYMS */
2703
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002704static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002705{
2706 int bx = 0;
2707
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002708 if (mod->taints ||
2709 mod->state == MODULE_STATE_GOING ||
2710 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002711 buf[bx++] = '(';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002712 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002713 buf[bx++] = 'P';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002714 if (mod->taints & (1 << TAINT_FORCED_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002715 buf[bx++] = 'F';
Linus Torvalds26e9a392008-10-17 09:50:12 -07002716 if (mod->taints & (1 << TAINT_CRAP))
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002717 buf[bx++] = 'C';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002718 /*
2719 * TAINT_FORCED_RMMOD: could be added.
2720 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2721 * apply to modules.
2722 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002723
2724 /* Show a - for module-is-being-unloaded */
2725 if (mod->state == MODULE_STATE_GOING)
2726 buf[bx++] = '-';
2727 /* Show a + for module-is-being-loaded */
2728 if (mod->state == MODULE_STATE_COMING)
2729 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002730 buf[bx++] = ')';
2731 }
2732 buf[bx] = '\0';
2733
2734 return buf;
2735}
2736
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002737#ifdef CONFIG_PROC_FS
2738/* Called by the /proc file system to return a list of modules. */
2739static void *m_start(struct seq_file *m, loff_t *pos)
2740{
2741 mutex_lock(&module_mutex);
2742 return seq_list_start(&modules, *pos);
2743}
2744
2745static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2746{
2747 return seq_list_next(p, &modules, pos);
2748}
2749
2750static void m_stop(struct seq_file *m, void *p)
2751{
2752 mutex_unlock(&module_mutex);
2753}
2754
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755static int m_show(struct seq_file *m, void *p)
2756{
2757 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002758 char buf[8];
2759
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05002760 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 mod->name, mod->init_size + mod->core_size);
2762 print_unload_info(m, mod);
2763
2764 /* Informative for users. */
2765 seq_printf(m, " %s",
2766 mod->state == MODULE_STATE_GOING ? "Unloading":
2767 mod->state == MODULE_STATE_COMING ? "Loading":
2768 "Live");
2769 /* Used by oprofile and other similar tools. */
2770 seq_printf(m, " 0x%p", mod->module_core);
2771
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002772 /* Taints info */
2773 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002774 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 seq_printf(m, "\n");
2777 return 0;
2778}
2779
2780/* Format: modulename size refcount deps address
2781
2782 Where refcount is a number or -, and deps is a comma-separated list
2783 of depends or -.
2784*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002785static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 .start = m_start,
2787 .next = m_next,
2788 .stop = m_stop,
2789 .show = m_show
2790};
2791
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002792static int modules_open(struct inode *inode, struct file *file)
2793{
2794 return seq_open(file, &modules_op);
2795}
2796
2797static const struct file_operations proc_modules_operations = {
2798 .open = modules_open,
2799 .read = seq_read,
2800 .llseek = seq_lseek,
2801 .release = seq_release,
2802};
2803
2804static int __init proc_modules_init(void)
2805{
2806 proc_create("modules", 0, NULL, &proc_modules_operations);
2807 return 0;
2808}
2809module_init(proc_modules_init);
2810#endif
2811
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812/* Given an address, look for it in the module exception tables. */
2813const struct exception_table_entry *search_module_extables(unsigned long addr)
2814{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 const struct exception_table_entry *e = NULL;
2816 struct module *mod;
2817
Rusty Russell24da1cb2007-07-15 23:41:46 -07002818 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002819 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 if (mod->num_exentries == 0)
2821 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002822
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 e = search_extable(mod->extable,
2824 mod->extable + mod->num_exentries - 1,
2825 addr);
2826 if (e)
2827 break;
2828 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07002829 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
2831 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07002832 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 return e;
2834}
2835
Ingo Molnar4d435f92006-07-03 00:24:24 -07002836/*
Rusty Russelle6104992009-03-31 13:05:31 -06002837 * is_module_address - is this address inside a module?
2838 * @addr: the address to check.
2839 *
2840 * See is_module_text_address() if you simply want to see if the address
2841 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07002842 */
Rusty Russelle6104992009-03-31 13:05:31 -06002843bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07002844{
Rusty Russelle6104992009-03-31 13:05:31 -06002845 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07002846
Rusty Russell24da1cb2007-07-15 23:41:46 -07002847 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06002848 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07002849 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002850
Rusty Russelle6104992009-03-31 13:05:31 -06002851 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07002852}
2853
Rusty Russelle6104992009-03-31 13:05:31 -06002854/*
2855 * __module_address - get the module which contains an address.
2856 * @addr: the address.
2857 *
2858 * Must be called with preempt disabled or module mutex held so that
2859 * module doesn't get freed during this.
2860 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07002861struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862{
2863 struct module *mod;
2864
Rusty Russell3a642e92008-07-22 19:24:28 -05002865 if (addr < module_addr_min || addr > module_addr_max)
2866 return NULL;
2867
Andi Kleend72b3752008-08-30 10:09:00 +02002868 list_for_each_entry_rcu(mod, &modules, list)
Rusty Russelle6104992009-03-31 13:05:31 -06002869 if (within_module_core(addr, mod)
2870 || within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 return mod;
2872 return NULL;
2873}
Tim Abbottc6b37802008-12-05 19:03:59 -05002874EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
Rusty Russelle6104992009-03-31 13:05:31 -06002876/*
2877 * is_module_text_address - is this address inside module code?
2878 * @addr: the address to check.
2879 *
2880 * See is_module_address() if you simply want to see if the address is
2881 * anywhere in a module. See kernel_text_address() for testing if an
2882 * address corresponds to kernel or module code.
2883 */
2884bool is_module_text_address(unsigned long addr)
2885{
2886 bool ret;
2887
2888 preempt_disable();
2889 ret = __module_text_address(addr) != NULL;
2890 preempt_enable();
2891
2892 return ret;
2893}
2894
2895/*
2896 * __module_text_address - get the module whose code contains an address.
2897 * @addr: the address.
2898 *
2899 * Must be called with preempt disabled or module mutex held so that
2900 * module doesn't get freed during this.
2901 */
2902struct module *__module_text_address(unsigned long addr)
2903{
2904 struct module *mod = __module_address(addr);
2905 if (mod) {
2906 /* Make sure it's within the text section. */
2907 if (!within(addr, mod->module_init, mod->init_text_size)
2908 && !within(addr, mod->module_core, mod->core_text_size))
2909 mod = NULL;
2910 }
2911 return mod;
2912}
Tim Abbottc6b37802008-12-05 19:03:59 -05002913EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06002914
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915/* Don't grab lock, we're oopsing. */
2916void print_modules(void)
2917{
2918 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07002919 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
Linus Torvaldsb2311252009-06-16 11:07:14 -07002921 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02002922 /* Most callers should already have preempt disabled, but make sure */
2923 preempt_disable();
2924 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002925 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02002926 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01002927 if (last_unloaded_module[0])
2928 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 printk("\n");
2930}
2931
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06002933/* Generate the signature for all relevant module structures here.
2934 * If these change, we don't want to try to parse the module. */
2935void module_layout(struct module *mod,
2936 struct modversion_info *ver,
2937 struct kernel_param *kp,
2938 struct kernel_symbol *ks,
2939 struct marker *marker,
2940 struct tracepoint *tp)
2941{
2942}
2943EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002945
2946#ifdef CONFIG_MARKERS
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002947void module_update_markers(void)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002948{
2949 struct module *mod;
2950
2951 mutex_lock(&module_mutex);
2952 list_for_each_entry(mod, &modules, list)
2953 if (!mod->taints)
2954 marker_update_probe_range(mod->markers,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002955 mod->markers + mod->num_markers);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002956 mutex_unlock(&module_mutex);
2957}
2958#endif
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002959
2960#ifdef CONFIG_TRACEPOINTS
2961void module_update_tracepoints(void)
2962{
2963 struct module *mod;
2964
2965 mutex_lock(&module_mutex);
2966 list_for_each_entry(mod, &modules, list)
2967 if (!mod->taints)
2968 tracepoint_update_probe_range(mod->tracepoints,
2969 mod->tracepoints + mod->num_tracepoints);
2970 mutex_unlock(&module_mutex);
2971}
2972
2973/*
2974 * Returns 0 if current not found.
2975 * Returns 1 if current found.
2976 */
2977int module_get_iter_tracepoints(struct tracepoint_iter *iter)
2978{
2979 struct module *iter_mod;
2980 int found = 0;
2981
2982 mutex_lock(&module_mutex);
2983 list_for_each_entry(iter_mod, &modules, list) {
2984 if (!iter_mod->taints) {
2985 /*
2986 * Sorted module list
2987 */
2988 if (iter_mod < iter->module)
2989 continue;
2990 else if (iter_mod > iter->module)
2991 iter->tracepoint = NULL;
2992 found = tracepoint_get_iter_range(&iter->tracepoint,
2993 iter_mod->tracepoints,
2994 iter_mod->tracepoints
2995 + iter_mod->num_tracepoints);
2996 if (found) {
2997 iter->module = iter_mod;
2998 break;
2999 }
3000 }
3001 }
3002 mutex_unlock(&module_mutex);
3003 return found;
3004}
3005#endif