blob: a0394706f10c4499c3321a4badd3e0086be28957 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#if 0
58#define DEBUGP printk
59#else
60#define DEBUGP(fmt , a...)
61#endif
62
63#ifndef ARCH_SHF_SMALL
64#define ARCH_SHF_SMALL 0
65#endif
66
67/* If this is set, the section belongs in the init part of the module */
68#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
69
Rusty Russell24da1cb2007-07-15 23:41:46 -070070/* List of modules, protected by module_mutex or preempt_disable
Andi Kleend72b3752008-08-30 10:09:00 +020071 * (delete uses stop_machine/add uses RCU list operations). */
Tim Abbottc6b37802008-12-05 19:03:59 -050072DEFINE_MUTEX(module_mutex);
73EXPORT_SYMBOL_GPL(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static LIST_HEAD(modules);
75
Rusty Russellc9a3ba52008-01-29 17:13:18 -050076/* Waiting for a module to finish initializing? */
77static DECLARE_WAIT_QUEUE_HEAD(module_wq);
78
Alan Sterne041c682006-03-27 01:16:30 -080079static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Rusty Russelle6104992009-03-31 13:05:31 -060081/* Bounds of module allocation, for speeding __module_address */
Rusty Russell3a642e92008-07-22 19:24:28 -050082static unsigned long module_addr_min = -1UL, module_addr_max = 0;
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084int register_module_notifier(struct notifier_block * nb)
85{
Alan Sterne041c682006-03-27 01:16:30 -080086 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88EXPORT_SYMBOL(register_module_notifier);
89
90int unregister_module_notifier(struct notifier_block * nb)
91{
Alan Sterne041c682006-03-27 01:16:30 -080092 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94EXPORT_SYMBOL(unregister_module_notifier);
95
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -080096/* We require a truly strong try_module_get(): 0 means failure due to
97 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static inline int strong_try_module_get(struct module *mod)
99{
100 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500101 return -EBUSY;
102 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500104 else
105 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700108static inline void add_taint_module(struct module *mod, unsigned flag)
109{
110 add_taint(flag);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700111 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700112}
113
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200114/*
115 * A thread that wants to hold a reference to a module only while it
116 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
118void __module_put_and_exit(struct module *mod, long code)
119{
120 module_put(mod);
121 do_exit(code);
122}
123EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/* Find a module section: 0 means not found. */
126static unsigned int find_sec(Elf_Ehdr *hdr,
127 Elf_Shdr *sechdrs,
128 const char *secstrings,
129 const char *name)
130{
131 unsigned int i;
132
133 for (i = 1; i < hdr->e_shnum; i++)
134 /* Alloc bit cleared means "ignore it." */
135 if ((sechdrs[i].sh_flags & SHF_ALLOC)
136 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
137 return i;
138 return 0;
139}
140
Rusty Russell5e458cc2008-10-22 10:00:13 -0500141/* Find a module section, or NULL. */
142static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
143 const char *secstrings, const char *name)
144{
145 /* Section 0 has sh_addr 0. */
146 return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
147}
148
149/* Find a module section, or NULL. Fill in number of "objects" in section. */
150static void *section_objs(Elf_Ehdr *hdr,
151 Elf_Shdr *sechdrs,
152 const char *secstrings,
153 const char *name,
154 size_t object_size,
155 unsigned int *num)
156{
157 unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
158
159 /* Section 0 has sh_addr 0 and sh_size 0. */
160 *num = sechdrs[sec].sh_size / object_size;
161 return (void *)sechdrs[sec].sh_addr;
162}
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/* Provided by the linker */
165extern const struct kernel_symbol __start___ksymtab[];
166extern const struct kernel_symbol __stop___ksymtab[];
167extern const struct kernel_symbol __start___ksymtab_gpl[];
168extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800169extern const struct kernel_symbol __start___ksymtab_gpl_future[];
170extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700171extern const struct kernel_symbol __start___ksymtab_gpl_future[];
172extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173extern const unsigned long __start___kcrctab[];
174extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800175extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500176#ifdef CONFIG_UNUSED_SYMBOLS
177extern const struct kernel_symbol __start___ksymtab_unused[];
178extern const struct kernel_symbol __stop___ksymtab_unused[];
179extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
180extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700181extern const unsigned long __start___kcrctab_unused[];
182extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500183#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185#ifndef CONFIG_MODVERSIONS
186#define symversion(base, idx) NULL
187#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800188#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189#endif
190
Rusty Russelldafd0942008-07-22 19:24:25 -0500191static bool each_symbol_in_section(const struct symsearch *arr,
192 unsigned int arrsize,
193 struct module *owner,
194 bool (*fn)(const struct symsearch *syms,
195 struct module *owner,
196 unsigned int symnum, void *data),
197 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100198{
Rusty Russelldafd0942008-07-22 19:24:25 -0500199 unsigned int i, j;
200
201 for (j = 0; j < arrsize; j++) {
202 for (i = 0; i < arr[j].stop - arr[j].start; i++)
203 if (fn(&arr[j], owner, i, data))
204 return true;
205 }
206
207 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100208}
209
Rusty Russelldafd0942008-07-22 19:24:25 -0500210/* Returns true as soon as fn returns true, otherwise false. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500211bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
212 unsigned int symnum, void *data), void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700213{
Rusty Russelldafd0942008-07-22 19:24:25 -0500214 struct module *mod;
215 const struct symsearch arr[] = {
216 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
217 NOT_GPL_ONLY, false },
218 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
219 __start___kcrctab_gpl,
220 GPL_ONLY, false },
221 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
222 __start___kcrctab_gpl_future,
223 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500224#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500225 { __start___ksymtab_unused, __stop___ksymtab_unused,
226 __start___kcrctab_unused,
227 NOT_GPL_ONLY, true },
228 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
229 __start___kcrctab_unused_gpl,
230 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500231#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500232 };
233
234 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
235 return true;
236
Andi Kleend72b3752008-08-30 10:09:00 +0200237 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500238 struct symsearch arr[] = {
239 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
240 NOT_GPL_ONLY, false },
241 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
242 mod->gpl_crcs,
243 GPL_ONLY, false },
244 { mod->gpl_future_syms,
245 mod->gpl_future_syms + mod->num_gpl_future_syms,
246 mod->gpl_future_crcs,
247 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500248#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500249 { mod->unused_syms,
250 mod->unused_syms + mod->num_unused_syms,
251 mod->unused_crcs,
252 NOT_GPL_ONLY, true },
253 { mod->unused_gpl_syms,
254 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
255 mod->unused_gpl_crcs,
256 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500257#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500258 };
259
260 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
261 return true;
262 }
263 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700264}
Tim Abbottc6b37802008-12-05 19:03:59 -0500265EXPORT_SYMBOL_GPL(each_symbol);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700266
Rusty Russelldafd0942008-07-22 19:24:25 -0500267struct find_symbol_arg {
268 /* Input */
269 const char *name;
270 bool gplok;
271 bool warn;
272
273 /* Output */
274 struct module *owner;
275 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500276 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500277};
278
279static bool find_symbol_in_section(const struct symsearch *syms,
280 struct module *owner,
281 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500282{
Rusty Russelldafd0942008-07-22 19:24:25 -0500283 struct find_symbol_arg *fsa = data;
284
285 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
286 return false;
287
288 if (!fsa->gplok) {
289 if (syms->licence == GPL_ONLY)
290 return false;
291 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
292 printk(KERN_WARNING "Symbol %s is being used "
293 "by a non-GPL module, which will not "
294 "be allowed in the future\n", fsa->name);
295 printk(KERN_WARNING "Please see the file "
296 "Documentation/feature-removal-schedule.txt "
297 "in the kernel source tree for more details.\n");
298 }
299 }
300
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500301#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500302 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500303 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500304 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500305 printk(KERN_WARNING
306 "This symbol will go away in the future.\n");
307 printk(KERN_WARNING
308 "Please evalute if this is the right api to use and if "
309 "it really is, submit a report the linux kernel "
310 "mailinglist together with submitting your code for "
311 "inclusion.\n");
312 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500313#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500314
315 fsa->owner = owner;
316 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500317 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500318 return true;
319}
320
Tim Abbott414fd312008-12-05 19:03:56 -0500321/* Find a symbol and return it, along with, (optional) crc and
322 * (optional) module which owns it */
Tim Abbottc6b37802008-12-05 19:03:59 -0500323const struct kernel_symbol *find_symbol(const char *name,
324 struct module **owner,
325 const unsigned long **crc,
326 bool gplok,
327 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Rusty Russelldafd0942008-07-22 19:24:25 -0500329 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Rusty Russelldafd0942008-07-22 19:24:25 -0500331 fsa.name = name;
332 fsa.gplok = gplok;
333 fsa.warn = warn;
334
335 if (each_symbol(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500336 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500337 *owner = fsa.owner;
338 if (crc)
339 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500340 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 DEBUGP("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500344 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
Tim Abbottc6b37802008-12-05 19:03:59 -0500346EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348/* Search for module by name: must hold module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500349struct module *find_module(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 struct module *mod;
352
353 list_for_each_entry(mod, &modules, list) {
354 if (strcmp(mod->name, name) == 0)
355 return mod;
356 }
357 return NULL;
358}
Tim Abbottc6b37802008-12-05 19:03:59 -0500359EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900362
363#ifdef CONFIG_HAVE_DYNAMIC_PER_CPU_AREA
364
365static void *percpu_modalloc(unsigned long size, unsigned long align,
366 const char *name)
367{
368 void *ptr;
369
370 if (align > PAGE_SIZE) {
371 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
372 name, align, PAGE_SIZE);
373 align = PAGE_SIZE;
374 }
375
Tejun Heoedcb4632009-03-06 14:33:59 +0900376 ptr = __alloc_reserved_percpu(size, align);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900377 if (!ptr)
378 printk(KERN_WARNING
379 "Could not allocate %lu bytes percpu data\n", size);
380 return ptr;
381}
382
383static void percpu_modfree(void *freeme)
384{
385 free_percpu(freeme);
386}
387
388#else /* ... !CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390/* Number of blocks used and allocated. */
391static unsigned int pcpu_num_used, pcpu_num_allocated;
392/* Size of each block. -ve means used. */
393static int *pcpu_size;
394
395static int split_block(unsigned int i, unsigned short size)
396{
397 /* Reallocation required? */
398 if (pcpu_num_used + 1 > pcpu_num_allocated) {
Pekka Enberg6d4f9c52007-05-08 00:24:58 -0700399 int *new;
400
401 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
402 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (!new)
404 return 0;
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 pcpu_num_allocated *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 pcpu_size = new;
408 }
409
410 /* Insert a new subblock */
411 memmove(&pcpu_size[i+1], &pcpu_size[i],
412 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
413 pcpu_num_used++;
414
415 pcpu_size[i+1] -= size;
416 pcpu_size[i] = size;
417 return 1;
418}
419
420static inline unsigned int block_size(int val)
421{
422 if (val < 0)
423 return -val;
424 return val;
425}
426
Rusty Russell842bbaa2005-08-01 21:11:47 -0700427static void *percpu_modalloc(unsigned long size, unsigned long align,
428 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 unsigned long extra;
431 unsigned int i;
432 void *ptr;
433
Jeremy Fitzhardingeb6e35902007-05-02 19:27:12 +0200434 if (align > PAGE_SIZE) {
435 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
436 name, align, PAGE_SIZE);
437 align = PAGE_SIZE;
Rusty Russell842bbaa2005-08-01 21:11:47 -0700438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 ptr = __per_cpu_start;
441 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
442 /* Extra for alignment requirement. */
443 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
444 BUG_ON(i == 0 && extra != 0);
445
446 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
447 continue;
448
449 /* Transfer extra to previous block. */
450 if (pcpu_size[i-1] < 0)
451 pcpu_size[i-1] -= extra;
452 else
453 pcpu_size[i-1] += extra;
454 pcpu_size[i] -= extra;
455 ptr += extra;
456
457 /* Split block if warranted */
458 if (pcpu_size[i] - size > sizeof(unsigned long))
459 if (!split_block(i, size))
460 return NULL;
461
462 /* Mark allocated */
463 pcpu_size[i] = -pcpu_size[i];
464 return ptr;
465 }
466
467 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
468 size);
469 return NULL;
470}
471
472static void percpu_modfree(void *freeme)
473{
474 unsigned int i;
475 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
476
477 /* First entry is core kernel percpu data. */
478 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
479 if (ptr == freeme) {
480 pcpu_size[i] = -pcpu_size[i];
481 goto free;
482 }
483 }
484 BUG();
485
486 free:
487 /* Merge with previous? */
488 if (pcpu_size[i-1] >= 0) {
489 pcpu_size[i-1] += pcpu_size[i];
490 pcpu_num_used--;
491 memmove(&pcpu_size[i], &pcpu_size[i+1],
492 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
493 i--;
494 }
495 /* Merge with next? */
496 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
497 pcpu_size[i] += pcpu_size[i+1];
498 pcpu_num_used--;
499 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
500 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
501 }
502}
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504static int percpu_modinit(void)
505{
506 pcpu_num_used = 2;
507 pcpu_num_allocated = 2;
508 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
509 GFP_KERNEL);
510 /* Static in-kernel percpu data (used). */
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +0200511 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 /* Free room. */
513 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
514 if (pcpu_size[1] < 0) {
515 printk(KERN_ERR "No per-cpu room for modules.\n");
516 pcpu_num_used = 1;
517 }
518
519 return 0;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700520}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521__initcall(percpu_modinit);
Tejun Heo6b588c12009-02-20 16:29:07 +0900522
Tejun Heofbf59bc2009-02-20 16:29:08 +0900523#endif /* CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
524
Tejun Heo6b588c12009-02-20 16:29:07 +0900525static unsigned int find_pcpusec(Elf_Ehdr *hdr,
526 Elf_Shdr *sechdrs,
527 const char *secstrings)
528{
529 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
530}
531
532static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
533{
534 int cpu;
535
536 for_each_possible_cpu(cpu)
537 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
538}
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900541
Rusty Russell842bbaa2005-08-01 21:11:47 -0700542static inline void *percpu_modalloc(unsigned long size, unsigned long align,
543 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 return NULL;
546}
547static inline void percpu_modfree(void *pcpuptr)
548{
549 BUG();
550}
551static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
552 Elf_Shdr *sechdrs,
553 const char *secstrings)
554{
555 return 0;
556}
557static inline void percpu_modcopy(void *pcpudst, const void *src,
558 unsigned long size)
559{
560 /* pcpusec should be 0, and size of that section should be 0. */
561 BUG_ON(size != 0);
562}
Tejun Heo6b588c12009-02-20 16:29:07 +0900563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564#endif /* CONFIG_SMP */
565
Matt Domschc988d2b2005-06-23 22:05:15 -0700566#define MODINFO_ATTR(field) \
567static void setup_modinfo_##field(struct module *mod, const char *s) \
568{ \
569 mod->field = kstrdup(s, GFP_KERNEL); \
570} \
571static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
572 struct module *mod, char *buffer) \
573{ \
574 return sprintf(buffer, "%s\n", mod->field); \
575} \
576static int modinfo_##field##_exists(struct module *mod) \
577{ \
578 return mod->field != NULL; \
579} \
580static void free_modinfo_##field(struct module *mod) \
581{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700582 kfree(mod->field); \
583 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700584} \
585static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900586 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700587 .show = show_modinfo_##field, \
588 .setup = setup_modinfo_##field, \
589 .test = modinfo_##field##_exists, \
590 .free = free_modinfo_##field, \
591};
592
593MODINFO_ATTR(version);
594MODINFO_ATTR(srcversion);
595
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100596static char last_unloaded_module[MODULE_NAME_LEN+1];
597
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800598#ifdef CONFIG_MODULE_UNLOAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599/* Init the unload section of the module. */
600static void module_unload_init(struct module *mod)
601{
Eric Dumazet720eba32009-02-03 13:31:36 +1030602 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 INIT_LIST_HEAD(&mod->modules_which_use_me);
Eric Dumazet720eba32009-02-03 13:31:36 +1030605 for_each_possible_cpu(cpu)
606 local_set(__module_ref_addr(mod, cpu), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /* Hold reference count during initialization. */
Eric Dumazet720eba32009-02-03 13:31:36 +1030608 local_set(__module_ref_addr(mod, raw_smp_processor_id()), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /* Backwards compatibility macros put refcount during init. */
610 mod->waiter = current;
611}
612
613/* modules using other modules */
614struct module_use
615{
616 struct list_head list;
617 struct module *module_which_uses;
618};
619
620/* Does a already use b? */
621static int already_uses(struct module *a, struct module *b)
622{
623 struct module_use *use;
624
625 list_for_each_entry(use, &b->modules_which_use_me, list) {
626 if (use->module_which_uses == a) {
627 DEBUGP("%s uses %s!\n", a->name, b->name);
628 return 1;
629 }
630 }
631 DEBUGP("%s does not use %s!\n", a->name, b->name);
632 return 0;
633}
634
635/* Module a uses b */
Tim Abbottc6b37802008-12-05 19:03:59 -0500636int use_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 struct module_use *use;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500639 int no_warn, err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (b == NULL || already_uses(a, b)) return 1;
642
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500643 /* If we're interrupted or time out, we fail. */
644 if (wait_event_interruptible_timeout(
645 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
646 30 * HZ) <= 0) {
647 printk("%s: gave up waiting for init of module %s.\n",
648 a->name, b->name);
649 return 0;
650 }
651
652 /* If strong_try_module_get() returned a different error, we fail. */
653 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return 0;
655
656 DEBUGP("Allocating new usage for %s.\n", a->name);
657 use = kmalloc(sizeof(*use), GFP_ATOMIC);
658 if (!use) {
659 printk("%s: out of memory loading\n", a->name);
660 module_put(b);
661 return 0;
662 }
663
664 use->module_which_uses = a;
665 list_add(&use->list, &b->modules_which_use_me);
Kay Sievers270a6c42007-01-18 13:26:15 +0100666 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return 1;
668}
Tim Abbottc6b37802008-12-05 19:03:59 -0500669EXPORT_SYMBOL_GPL(use_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671/* Clear the unload stuff of the module. */
672static void module_unload_free(struct module *mod)
673{
674 struct module *i;
675
676 list_for_each_entry(i, &modules, list) {
677 struct module_use *use;
678
679 list_for_each_entry(use, &i->modules_which_use_me, list) {
680 if (use->module_which_uses == mod) {
681 DEBUGP("%s unusing %s\n", mod->name, i->name);
682 module_put(i);
683 list_del(&use->list);
684 kfree(use);
Kay Sievers270a6c42007-01-18 13:26:15 +0100685 sysfs_remove_link(i->holders_dir, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 /* There can be at most one match. */
687 break;
688 }
689 }
690 }
691}
692
693#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800694static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
696 int ret = (flags & O_TRUNC);
697 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800698 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return ret;
700}
701#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800702static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 return 0;
705}
706#endif /* CONFIG_MODULE_FORCE_UNLOAD */
707
708struct stopref
709{
710 struct module *mod;
711 int flags;
712 int *forced;
713};
714
715/* Whole machine is stopped with interrupts off when this runs. */
716static int __try_stop_module(void *_sref)
717{
718 struct stopref *sref = _sref;
719
Rusty Russellda39ba52008-07-22 19:24:25 -0500720 /* If it's not unused, quit unless we're forcing. */
721 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800722 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return -EWOULDBLOCK;
724 }
725
726 /* Mark it as dying. */
727 sref->mod->state = MODULE_STATE_GOING;
728 return 0;
729}
730
731static int try_stop_module(struct module *mod, int flags, int *forced)
732{
Rusty Russellda39ba52008-07-22 19:24:25 -0500733 if (flags & O_NONBLOCK) {
734 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500736 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500737 } else {
738 /* We don't need to stop the machine for this. */
739 mod->state = MODULE_STATE_GOING;
740 synchronize_sched();
741 return 0;
742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
745unsigned int module_refcount(struct module *mod)
746{
Eric Dumazet720eba32009-02-03 13:31:36 +1030747 unsigned int total = 0;
748 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Eric Dumazet720eba32009-02-03 13:31:36 +1030750 for_each_possible_cpu(cpu)
751 total += local_read(__module_ref_addr(mod, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return total;
753}
754EXPORT_SYMBOL(module_refcount);
755
756/* This exists whether we can unload or not */
757static void free_module(struct module *mod);
758
759static void wait_for_zero_refcount(struct module *mod)
760{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500761 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800762 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 for (;;) {
764 DEBUGP("Looking at refcount...\n");
765 set_current_state(TASK_UNINTERRUPTIBLE);
766 if (module_refcount(mod) == 0)
767 break;
768 schedule();
769 }
770 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800771 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100774SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
775 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
777 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800778 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 int ret, forced = 0;
780
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800781 if (!capable(CAP_SYS_MODULE))
782 return -EPERM;
783
784 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
785 return -EFAULT;
786 name[MODULE_NAME_LEN-1] = '\0';
787
Heiko Carstens9e018922008-12-22 12:36:31 +0100788 /* Create stop_machine threads since free_module relies on
789 * a non-failing stop_machine call. */
790 ret = stop_machine_create();
791 if (ret)
792 return ret;
793
794 if (mutex_lock_interruptible(&module_mutex) != 0) {
795 ret = -EINTR;
796 goto out_stop;
797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 mod = find_module(name);
800 if (!mod) {
801 ret = -ENOENT;
802 goto out;
803 }
804
805 if (!list_empty(&mod->modules_which_use_me)) {
806 /* Other modules depend on us: get rid of them first. */
807 ret = -EWOULDBLOCK;
808 goto out;
809 }
810
811 /* Doing init or already dying? */
812 if (mod->state != MODULE_STATE_LIVE) {
813 /* FIXME: if (force), slam module count and wake up
814 waiter --RR */
815 DEBUGP("%s already dying\n", mod->name);
816 ret = -EBUSY;
817 goto out;
818 }
819
820 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700821 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800822 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (!forced) {
824 /* This module can't be removed */
825 ret = -EBUSY;
826 goto out;
827 }
828 }
829
830 /* Set this up before setting mod->state */
831 mod->waiter = current;
832
833 /* Stop the machine so refcounts can't move and disable module. */
834 ret = try_stop_module(mod, flags, &forced);
835 if (ret != 0)
836 goto out;
837
838 /* Never wait if forced. */
839 if (!forced && module_refcount(mod) != 0)
840 wait_for_zero_refcount(mod);
841
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200842 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 /* Final destruction now noone is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200844 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200846 blocking_notifier_call_chain(&module_notify_list,
847 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800848 async_synchronize_full();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200849 mutex_lock(&module_mutex);
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100850 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500851 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Jason Barone9d376f2009-02-05 11:51:38 -0500852 ddebug_remove_module(mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 free_module(mod);
854
855 out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800856 mutex_unlock(&module_mutex);
Heiko Carstens9e018922008-12-22 12:36:31 +0100857out_stop:
858 stop_machine_destroy();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return ret;
860}
861
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800862static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
864 struct module_use *use;
865 int printed_something = 0;
866
867 seq_printf(m, " %u ", module_refcount(mod));
868
869 /* Always include a trailing , so userspace can differentiate
870 between this and the old multi-field proc format. */
871 list_for_each_entry(use, &mod->modules_which_use_me, list) {
872 printed_something = 1;
873 seq_printf(m, "%s,", use->module_which_uses->name);
874 }
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (mod->init != NULL && mod->exit == NULL) {
877 printed_something = 1;
878 seq_printf(m, "[permanent],");
879 }
880
881 if (!printed_something)
882 seq_printf(m, "-");
883}
884
885void __symbol_put(const char *symbol)
886{
887 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Rusty Russell24da1cb2007-07-15 23:41:46 -0700889 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -0500890 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 BUG();
892 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700893 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895EXPORT_SYMBOL(__symbol_put);
896
897void symbol_put_addr(void *addr)
898{
Trent Piepho5e376612006-05-15 09:44:06 -0700899 struct module *modaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Trent Piepho5e376612006-05-15 09:44:06 -0700901 if (core_kernel_text((unsigned long)addr))
902 return;
903
Rusty Russella6e6abd2009-03-31 13:05:31 -0600904 /* module_text_address is safe here: we're supposed to have reference
905 * to module from symbol_get, so it can't go away. */
906 modaddr = __module_text_address((unsigned long)addr);
907 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -0700908 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910EXPORT_SYMBOL_GPL(symbol_put_addr);
911
912static ssize_t show_refcnt(struct module_attribute *mattr,
913 struct module *mod, char *buffer)
914{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400915 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
918static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900919 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 .show = show_refcnt,
921};
922
Al Virof6a57032006-10-18 01:47:25 -0400923void module_put(struct module *module)
924{
925 if (module) {
926 unsigned int cpu = get_cpu();
Eric Dumazet720eba32009-02-03 13:31:36 +1030927 local_dec(__module_ref_addr(module, cpu));
Al Virof6a57032006-10-18 01:47:25 -0400928 /* Maybe they're waiting for us to drop reference? */
929 if (unlikely(!module_is_live(module)))
930 wake_up_process(module->waiter);
931 put_cpu();
932 }
933}
934EXPORT_SYMBOL(module_put);
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800937static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
939 /* We don't know the usage count, or what modules are using. */
940 seq_printf(m, " - -");
941}
942
943static inline void module_unload_free(struct module *mod)
944{
945}
946
Tim Abbottc6b37802008-12-05 19:03:59 -0500947int use_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500949 return strong_try_module_get(b) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
Tim Abbottc6b37802008-12-05 19:03:59 -0500951EXPORT_SYMBOL_GPL(use_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953static inline void module_unload_init(struct module *mod)
954{
955}
956#endif /* CONFIG_MODULE_UNLOAD */
957
Kay Sievers1f717402006-11-24 12:15:25 +0100958static ssize_t show_initstate(struct module_attribute *mattr,
959 struct module *mod, char *buffer)
960{
961 const char *state = "unknown";
962
963 switch (mod->state) {
964 case MODULE_STATE_LIVE:
965 state = "live";
966 break;
967 case MODULE_STATE_COMING:
968 state = "coming";
969 break;
970 case MODULE_STATE_GOING:
971 state = "going";
972 break;
973 }
974 return sprintf(buffer, "%s\n", state);
975}
976
977static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900978 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100979 .show = show_initstate,
980};
981
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800982static struct module_attribute *modinfo_attrs[] = {
983 &modinfo_version,
984 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100985 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800986#ifdef CONFIG_MODULE_UNLOAD
987 &refcnt,
988#endif
989 NULL,
990};
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992static const char vermagic[] = VERMAGIC_STRING;
993
Rusty Russellc6e665c2009-03-31 13:05:33 -0600994static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -0700995{
996#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -0700997 if (!test_taint(TAINT_FORCED_MODULE))
Rusty Russellc6e665c2009-03-31 13:05:33 -0600998 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
999 mod->name, reason);
Linus Torvalds826e4502008-05-04 17:04:16 -07001000 add_taint_module(mod, TAINT_FORCED_MODULE);
1001 return 0;
1002#else
1003 return -ENOEXEC;
1004#endif
1005}
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007#ifdef CONFIG_MODVERSIONS
1008static int check_version(Elf_Shdr *sechdrs,
1009 unsigned int versindex,
1010 const char *symname,
1011 struct module *mod,
1012 const unsigned long *crc)
1013{
1014 unsigned int i, num_versions;
1015 struct modversion_info *versions;
1016
1017 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1018 if (!crc)
1019 return 1;
1020
Rusty Russella5dd6972008-05-09 16:24:21 +10001021 /* No versions at all? modprobe --force does this. */
1022 if (versindex == 0)
1023 return try_to_force_load(mod, symname) == 0;
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 versions = (void *) sechdrs[versindex].sh_addr;
1026 num_versions = sechdrs[versindex].sh_size
1027 / sizeof(struct modversion_info);
1028
1029 for (i = 0; i < num_versions; i++) {
1030 if (strcmp(versions[i].name, symname) != 0)
1031 continue;
1032
1033 if (versions[i].crc == *crc)
1034 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 DEBUGP("Found checksum %lX vs module %lX\n",
1036 *crc, versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001037 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001039
Rusty Russella5dd6972008-05-09 16:24:21 +10001040 printk(KERN_WARNING "%s: no symbol version for %s\n",
1041 mod->name, symname);
1042 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001043
1044bad_version:
1045 printk("%s: disagrees about version of symbol %s\n",
1046 mod->name, symname);
1047 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
1050static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1051 unsigned int versindex,
1052 struct module *mod)
1053{
1054 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Rusty Russell8c8ef422009-03-31 13:05:34 -06001056 if (!find_symbol("module_layout", NULL, &crc, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 BUG();
Rusty Russell8c8ef422009-03-31 13:05:34 -06001058 return check_version(sechdrs, versindex, "module_layout", mod, crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
Rusty Russell91e37a72008-05-09 16:25:28 +10001061/* First part is kernel version, which we ignore if module has crcs. */
1062static inline int same_magic(const char *amagic, const char *bmagic,
1063 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Rusty Russell91e37a72008-05-09 16:25:28 +10001065 if (has_crcs) {
1066 amagic += strcspn(amagic, " ");
1067 bmagic += strcspn(bmagic, " ");
1068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return strcmp(amagic, bmagic) == 0;
1070}
1071#else
1072static inline int check_version(Elf_Shdr *sechdrs,
1073 unsigned int versindex,
1074 const char *symname,
1075 struct module *mod,
1076 const unsigned long *crc)
1077{
1078 return 1;
1079}
1080
1081static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1082 unsigned int versindex,
1083 struct module *mod)
1084{
1085 return 1;
1086}
1087
Rusty Russell91e37a72008-05-09 16:25:28 +10001088static inline int same_magic(const char *amagic, const char *bmagic,
1089 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 return strcmp(amagic, bmagic) == 0;
1092}
1093#endif /* CONFIG_MODVERSIONS */
1094
1095/* Resolve a symbol for this module. I.e. if we find one, record usage.
1096 Must be holding module_mutex. */
Tim Abbott414fd312008-12-05 19:03:56 -05001097static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
1098 unsigned int versindex,
1099 const char *name,
1100 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
1102 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001103 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 const unsigned long *crc;
1105
Tim Abbott414fd312008-12-05 19:03:56 -05001106 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001107 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Tim Abbott414fd312008-12-05 19:03:56 -05001108 /* use_module can fail due to OOM,
1109 or module initialization or unloading */
1110 if (sym) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1112 !use_module(mod, owner))
Tim Abbott414fd312008-12-05 19:03:56 -05001113 sym = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
Tim Abbott414fd312008-12-05 19:03:56 -05001115 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116}
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118/*
1119 * /sys/module/foo/sections stuff
1120 * J. Corbet <corbet@lwn.net>
1121 */
Kay Sievers120fc3d2008-02-21 00:33:20 +01001122#if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
Rusty Russella58730c2008-03-13 09:03:44 +00001123struct module_sect_attr
1124{
1125 struct module_attribute mattr;
1126 char *name;
1127 unsigned long address;
1128};
1129
1130struct module_sect_attrs
1131{
1132 struct attribute_group grp;
1133 unsigned int nsections;
1134 struct module_sect_attr attrs[0];
1135};
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137static ssize_t module_sect_show(struct module_attribute *mattr,
1138 struct module *mod, char *buf)
1139{
1140 struct module_sect_attr *sattr =
1141 container_of(mattr, struct module_sect_attr, mattr);
1142 return sprintf(buf, "0x%lx\n", sattr->address);
1143}
1144
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001145static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1146{
Rusty Russella58730c2008-03-13 09:03:44 +00001147 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001148
1149 for (section = 0; section < sect_attrs->nsections; section++)
1150 kfree(sect_attrs->attrs[section].name);
1151 kfree(sect_attrs);
1152}
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154static void add_sect_attrs(struct module *mod, unsigned int nsect,
1155 char *secstrings, Elf_Shdr *sechdrs)
1156{
1157 unsigned int nloaded = 0, i, size[2];
1158 struct module_sect_attrs *sect_attrs;
1159 struct module_sect_attr *sattr;
1160 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 /* Count loaded sections and allocate structures */
1163 for (i = 0; i < nsect; i++)
1164 if (sechdrs[i].sh_flags & SHF_ALLOC)
1165 nloaded++;
1166 size[0] = ALIGN(sizeof(*sect_attrs)
1167 + nloaded * sizeof(sect_attrs->attrs[0]),
1168 sizeof(sect_attrs->grp.attrs[0]));
1169 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001170 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1171 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return;
1173
1174 /* Setup section attributes. */
1175 sect_attrs->grp.name = "sections";
1176 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1177
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001178 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 sattr = &sect_attrs->attrs[0];
1180 gattr = &sect_attrs->grp.attrs[0];
1181 for (i = 0; i < nsect; i++) {
1182 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1183 continue;
1184 sattr->address = sechdrs[i].sh_addr;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001185 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1186 GFP_KERNEL);
1187 if (sattr->name == NULL)
1188 goto out;
1189 sect_attrs->nsections++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 sattr->mattr.show = module_sect_show;
1191 sattr->mattr.store = NULL;
1192 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 sattr->mattr.attr.mode = S_IRUGO;
1194 *(gattr++) = &(sattr++)->mattr.attr;
1195 }
1196 *gattr = NULL;
1197
1198 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1199 goto out;
1200
1201 mod->sect_attrs = sect_attrs;
1202 return;
1203 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001204 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
1207static void remove_sect_attrs(struct module *mod)
1208{
1209 if (mod->sect_attrs) {
1210 sysfs_remove_group(&mod->mkobj.kobj,
1211 &mod->sect_attrs->grp);
1212 /* We are positive that no one is using any sect attrs
1213 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001214 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 mod->sect_attrs = NULL;
1216 }
1217}
1218
Roland McGrath6d760132007-10-16 23:26:40 -07001219/*
1220 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1221 */
1222
1223struct module_notes_attrs {
1224 struct kobject *dir;
1225 unsigned int notes;
1226 struct bin_attribute attrs[0];
1227};
1228
1229static ssize_t module_notes_read(struct kobject *kobj,
1230 struct bin_attribute *bin_attr,
1231 char *buf, loff_t pos, size_t count)
1232{
1233 /*
1234 * The caller checked the pos and count against our size.
1235 */
1236 memcpy(buf, bin_attr->private + pos, count);
1237 return count;
1238}
1239
1240static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1241 unsigned int i)
1242{
1243 if (notes_attrs->dir) {
1244 while (i-- > 0)
1245 sysfs_remove_bin_file(notes_attrs->dir,
1246 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001247 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001248 }
1249 kfree(notes_attrs);
1250}
1251
1252static void add_notes_attrs(struct module *mod, unsigned int nsect,
1253 char *secstrings, Elf_Shdr *sechdrs)
1254{
1255 unsigned int notes, loaded, i;
1256 struct module_notes_attrs *notes_attrs;
1257 struct bin_attribute *nattr;
1258
1259 /* Count notes sections and allocate structures. */
1260 notes = 0;
1261 for (i = 0; i < nsect; i++)
1262 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1263 (sechdrs[i].sh_type == SHT_NOTE))
1264 ++notes;
1265
1266 if (notes == 0)
1267 return;
1268
1269 notes_attrs = kzalloc(sizeof(*notes_attrs)
1270 + notes * sizeof(notes_attrs->attrs[0]),
1271 GFP_KERNEL);
1272 if (notes_attrs == NULL)
1273 return;
1274
1275 notes_attrs->notes = notes;
1276 nattr = &notes_attrs->attrs[0];
1277 for (loaded = i = 0; i < nsect; ++i) {
1278 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1279 continue;
1280 if (sechdrs[i].sh_type == SHT_NOTE) {
1281 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1282 nattr->attr.mode = S_IRUGO;
1283 nattr->size = sechdrs[i].sh_size;
1284 nattr->private = (void *) sechdrs[i].sh_addr;
1285 nattr->read = module_notes_read;
1286 ++nattr;
1287 }
1288 ++loaded;
1289 }
1290
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001291 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001292 if (!notes_attrs->dir)
1293 goto out;
1294
1295 for (i = 0; i < notes; ++i)
1296 if (sysfs_create_bin_file(notes_attrs->dir,
1297 &notes_attrs->attrs[i]))
1298 goto out;
1299
1300 mod->notes_attrs = notes_attrs;
1301 return;
1302
1303 out:
1304 free_notes_attrs(notes_attrs, i);
1305}
1306
1307static void remove_notes_attrs(struct module *mod)
1308{
1309 if (mod->notes_attrs)
1310 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1311}
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1316 char *sectstrings, Elf_Shdr *sechdrs)
1317{
1318}
1319
1320static inline void remove_sect_attrs(struct module *mod)
1321{
1322}
Roland McGrath6d760132007-10-16 23:26:40 -07001323
1324static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1325 char *sectstrings, Elf_Shdr *sechdrs)
1326{
1327}
1328
1329static inline void remove_notes_attrs(struct module *mod)
1330{
1331}
Kay Sievers120fc3d2008-02-21 00:33:20 +01001332#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Randy Dunlapef665c12007-02-13 15:19:06 -08001334#ifdef CONFIG_SYSFS
1335int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001336{
1337 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001338 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001339 int error = 0;
1340 int i;
1341
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001342 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1343 (ARRAY_SIZE(modinfo_attrs) + 1)),
1344 GFP_KERNEL);
1345 if (!mod->modinfo_attrs)
1346 return -ENOMEM;
1347
1348 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001349 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1350 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001351 (attr->test && attr->test(mod))) {
1352 memcpy(temp_attr, attr, sizeof(*temp_attr));
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001353 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1354 ++temp_attr;
1355 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001356 }
1357 return error;
1358}
1359
Randy Dunlapef665c12007-02-13 15:19:06 -08001360void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001361{
1362 struct module_attribute *attr;
1363 int i;
1364
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001365 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1366 /* pick a field to test for end of list */
1367 if (!attr->attr.name)
1368 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001369 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001370 if (attr->free)
1371 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001372 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001373 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001374}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Randy Dunlapef665c12007-02-13 15:19:06 -08001376int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
1378 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001379 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001381 if (!module_sysfs_initialized) {
1382 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001383 mod->name);
1384 err = -EINVAL;
1385 goto out;
1386 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001387
1388 kobj = kset_find_obj(module_kset, mod->name);
1389 if (kobj) {
1390 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1391 kobject_put(kobj);
1392 err = -EINVAL;
1393 goto out;
1394 }
1395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001397
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001398 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1399 mod->mkobj.kobj.kset = module_kset;
1400 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1401 "%s", mod->name);
1402 if (err)
1403 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001404
Kay Sievers97c146e2007-11-29 23:46:11 +01001405 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001406out:
1407 return err;
1408}
1409
Randy Dunlapef665c12007-02-13 15:19:06 -08001410int mod_sysfs_setup(struct module *mod,
Kay Sievers270a6c42007-01-18 13:26:15 +01001411 struct kernel_param *kparam,
1412 unsigned int num_params)
1413{
1414 int err;
1415
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001416 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001417 if (!mod->holders_dir) {
1418 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001419 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001420 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 err = module_param_sysfs_setup(mod, kparam, num_params);
1423 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001424 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Matt Domschc988d2b2005-06-23 22:05:15 -07001426 err = module_add_modinfo_attrs(mod);
1427 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001428 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001429
Kay Sieverse17e0f52006-11-24 12:15:25 +01001430 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return 0;
1432
Kay Sieverse17e0f52006-11-24 12:15:25 +01001433out_unreg_param:
1434 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001435out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001436 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001437out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001438 kobject_put(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return err;
1440}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001441
1442static void mod_sysfs_fini(struct module *mod)
1443{
1444 kobject_put(&mod->mkobj.kobj);
1445}
1446
1447#else /* CONFIG_SYSFS */
1448
1449static void mod_sysfs_fini(struct module *mod)
1450{
1451}
1452
1453#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455static void mod_kobject_remove(struct module *mod)
1456{
Matt Domschc988d2b2005-06-23 22:05:15 -07001457 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001459 kobject_put(mod->mkobj.drivers_dir);
1460 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001461 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
1464/*
1465 * unlink the module with the whole machine is stopped with interrupts off
1466 * - this defends against kallsyms not taking locks
1467 */
1468static int __unlink_module(void *_mod)
1469{
1470 struct module *mod = _mod;
1471 list_del(&mod->list);
1472 return 0;
1473}
1474
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001475/* Free a module, remove from lists, etc (must hold module_mutex). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476static void free_module(struct module *mod)
1477{
1478 /* Delete from various lists */
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001479 stop_machine(__unlink_module, mod, NULL);
Roland McGrath6d760132007-10-16 23:26:40 -07001480 remove_notes_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 remove_sect_attrs(mod);
1482 mod_kobject_remove(mod);
1483
1484 /* Arch-specific cleanup. */
1485 module_arch_cleanup(mod);
1486
1487 /* Module unload stuff */
1488 module_unload_free(mod);
1489
Rusty Russelle180a6b2009-03-31 13:05:29 -06001490 /* Free any allocated parameters. */
1491 destroy_params(mod->kp, mod->num_kp);
1492
Steven Rostedtfed19392008-08-14 22:47:19 -04001493 /* release any pointers to mcount in this module */
1494 ftrace_release(mod->module_core, mod->core_size);
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 /* This may be NULL, but that's OK */
1497 module_free(mod, mod->module_init);
1498 kfree(mod->args);
1499 if (mod->percpu)
1500 percpu_modfree(mod->percpu);
Eric Dumazet720eba32009-02-03 13:31:36 +10301501#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
1502 if (mod->refptr)
1503 percpu_modfree(mod->refptr);
1504#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001505 /* Free lock-classes: */
1506 lockdep_free_key_range(mod->module_core, mod->core_size);
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 /* Finally, free the core (containing the module structure) */
1509 module_free(mod, mod->module_core);
1510}
1511
1512void *__symbol_get(const char *symbol)
1513{
1514 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001515 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Rusty Russell24da1cb2007-07-15 23:41:46 -07001517 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001518 sym = find_symbol(symbol, &owner, NULL, true, true);
1519 if (sym && strong_try_module_get(owner))
1520 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001521 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Tim Abbott414fd312008-12-05 19:03:56 -05001523 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525EXPORT_SYMBOL_GPL(__symbol_get);
1526
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001527/*
1528 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001529 * in the kernel or in some other module's exported symbol table.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001530 */
1531static int verify_export_symbols(struct module *mod)
1532{
Rusty Russellb2111042008-05-01 21:15:00 -05001533 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001534 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001535 const struct kernel_symbol *s;
1536 struct {
1537 const struct kernel_symbol *sym;
1538 unsigned int num;
1539 } arr[] = {
1540 { mod->syms, mod->num_syms },
1541 { mod->gpl_syms, mod->num_gpl_syms },
1542 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001543#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001544 { mod->unused_syms, mod->num_unused_syms },
1545 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001546#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001547 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001548
Rusty Russellb2111042008-05-01 21:15:00 -05001549 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1550 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Tim Abbott414fd312008-12-05 19:03:56 -05001551 if (find_symbol(s->name, &owner, NULL, true, false)) {
Rusty Russellb2111042008-05-01 21:15:00 -05001552 printk(KERN_ERR
1553 "%s: exports duplicate symbol %s"
1554 " (owned by %s)\n",
1555 mod->name, s->name, module_name(owner));
1556 return -ENOEXEC;
1557 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001558 }
Rusty Russellb2111042008-05-01 21:15:00 -05001559 }
1560 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001561}
1562
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001563/* Change all symbols so that st_value encodes the pointer directly. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564static int simplify_symbols(Elf_Shdr *sechdrs,
1565 unsigned int symindex,
1566 const char *strtab,
1567 unsigned int versindex,
1568 unsigned int pcpuindex,
1569 struct module *mod)
1570{
1571 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1572 unsigned long secbase;
1573 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1574 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05001575 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 for (i = 1; i < n; i++) {
1578 switch (sym[i].st_shndx) {
1579 case SHN_COMMON:
1580 /* We compiled with -fno-common. These are not
1581 supposed to happen. */
1582 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1583 printk("%s: please compile with -fno-common\n",
1584 mod->name);
1585 ret = -ENOEXEC;
1586 break;
1587
1588 case SHN_ABS:
1589 /* Don't need to do anything */
1590 DEBUGP("Absolute symbol: 0x%08lx\n",
1591 (long)sym[i].st_value);
1592 break;
1593
1594 case SHN_UNDEF:
Tim Abbott414fd312008-12-05 19:03:56 -05001595 ksym = resolve_symbol(sechdrs, versindex,
1596 strtab + sym[i].st_name, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 /* Ok if resolved. */
Tim Abbott414fd312008-12-05 19:03:56 -05001598 if (ksym) {
1599 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 break;
Tim Abbott414fd312008-12-05 19:03:56 -05001601 }
1602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 /* Ok if weak. */
1604 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1605 break;
1606
1607 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1608 mod->name, strtab + sym[i].st_name);
1609 ret = -ENOENT;
1610 break;
1611
1612 default:
1613 /* Divert to percpu allocation if a percpu var. */
1614 if (sym[i].st_shndx == pcpuindex)
1615 secbase = (unsigned long)mod->percpu;
1616 else
1617 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1618 sym[i].st_value += secbase;
1619 break;
1620 }
1621 }
1622
1623 return ret;
1624}
1625
Helge Deller088af9a2008-12-31 12:31:18 +01001626/* Additional bytes needed by arch in front of individual sections */
1627unsigned int __weak arch_mod_section_prepend(struct module *mod,
1628 unsigned int section)
1629{
1630 /* default implementation just returns zero */
1631 return 0;
1632}
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01001635static long get_offset(struct module *mod, unsigned int *size,
1636 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
1638 long ret;
1639
Helge Deller088af9a2008-12-31 12:31:18 +01001640 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1642 *size = ret + sechdr->sh_size;
1643 return ret;
1644}
1645
1646/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1647 might -- code, read-only data, read-write data, small data. Tally
1648 sizes, and place the offsets into sh_entsize fields: high bit means it
1649 belongs in init. */
1650static void layout_sections(struct module *mod,
1651 const Elf_Ehdr *hdr,
1652 Elf_Shdr *sechdrs,
1653 const char *secstrings)
1654{
1655 static unsigned long const masks[][2] = {
1656 /* NOTE: all executable code must be the first section
1657 * in this array; otherwise modify the text_size
1658 * finder in the two loops below */
1659 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1660 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1661 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1662 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1663 };
1664 unsigned int m, i;
1665
1666 for (i = 0; i < hdr->e_shnum; i++)
1667 sechdrs[i].sh_entsize = ~0UL;
1668
1669 DEBUGP("Core section allocation order:\n");
1670 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1671 for (i = 0; i < hdr->e_shnum; ++i) {
1672 Elf_Shdr *s = &sechdrs[i];
1673
1674 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1675 || (s->sh_flags & masks[m][1])
1676 || s->sh_entsize != ~0UL
Rusty Russell49502672009-03-31 13:05:36 -06001677 || strstarts(secstrings + s->sh_name, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001679 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 DEBUGP("\t%s\n", secstrings + s->sh_name);
1681 }
1682 if (m == 0)
1683 mod->core_text_size = mod->core_size;
1684 }
1685
1686 DEBUGP("Init section allocation order:\n");
1687 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1688 for (i = 0; i < hdr->e_shnum; ++i) {
1689 Elf_Shdr *s = &sechdrs[i];
1690
1691 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1692 || (s->sh_flags & masks[m][1])
1693 || s->sh_entsize != ~0UL
Rusty Russell49502672009-03-31 13:05:36 -06001694 || !strstarts(secstrings + s->sh_name, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001696 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 | INIT_OFFSET_MASK);
1698 DEBUGP("\t%s\n", secstrings + s->sh_name);
1699 }
1700 if (m == 0)
1701 mod->init_text_size = mod->init_size;
1702 }
1703}
1704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705static void set_license(struct module *mod, const char *license)
1706{
1707 if (!license)
1708 license = "unspecified";
1709
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001710 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07001711 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001712 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001713 "kernel.\n", mod->name, license);
1714 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
1716}
1717
1718/* Parse tag=value strings from .modinfo section */
1719static char *next_string(char *string, unsigned long *secsize)
1720{
1721 /* Skip non-zero chars */
1722 while (string[0]) {
1723 string++;
1724 if ((*secsize)-- <= 1)
1725 return NULL;
1726 }
1727
1728 /* Skip any zero padding. */
1729 while (!string[0]) {
1730 string++;
1731 if ((*secsize)-- <= 1)
1732 return NULL;
1733 }
1734 return string;
1735}
1736
1737static char *get_modinfo(Elf_Shdr *sechdrs,
1738 unsigned int info,
1739 const char *tag)
1740{
1741 char *p;
1742 unsigned int taglen = strlen(tag);
1743 unsigned long size = sechdrs[info].sh_size;
1744
1745 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1746 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1747 return p + taglen + 1;
1748 }
1749 return NULL;
1750}
1751
Matt Domschc988d2b2005-06-23 22:05:15 -07001752static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1753 unsigned int infoindex)
1754{
1755 struct module_attribute *attr;
1756 int i;
1757
1758 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1759 if (attr->setup)
1760 attr->setup(mod,
1761 get_modinfo(sechdrs,
1762 infoindex,
1763 attr->attr.name));
1764 }
1765}
Matt Domschc988d2b2005-06-23 22:05:15 -07001766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01001768
1769/* lookup symbol in given range of kernel_symbols */
1770static const struct kernel_symbol *lookup_symbol(const char *name,
1771 const struct kernel_symbol *start,
1772 const struct kernel_symbol *stop)
1773{
1774 const struct kernel_symbol *ks = start;
1775 for (; ks < stop; ks++)
1776 if (strcmp(ks->name, name) == 0)
1777 return ks;
1778 return NULL;
1779}
1780
Tim Abbottca4787b2009-01-05 08:40:10 -06001781static int is_exported(const char *name, unsigned long value,
1782 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
Tim Abbottca4787b2009-01-05 08:40:10 -06001784 const struct kernel_symbol *ks;
1785 if (!mod)
1786 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001787 else
Tim Abbottca4787b2009-01-05 08:40:10 -06001788 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
1789 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790}
1791
1792/* As per nm */
1793static char elf_type(const Elf_Sym *sym,
1794 Elf_Shdr *sechdrs,
1795 const char *secstrings,
1796 struct module *mod)
1797{
1798 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1799 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1800 return 'v';
1801 else
1802 return 'w';
1803 }
1804 if (sym->st_shndx == SHN_UNDEF)
1805 return 'U';
1806 if (sym->st_shndx == SHN_ABS)
1807 return 'a';
1808 if (sym->st_shndx >= SHN_LORESERVE)
1809 return '?';
1810 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1811 return 't';
1812 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1813 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1814 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1815 return 'r';
1816 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1817 return 'g';
1818 else
1819 return 'd';
1820 }
1821 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1822 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1823 return 's';
1824 else
1825 return 'b';
1826 }
Rusty Russell49502672009-03-31 13:05:36 -06001827 if (strstarts(secstrings + sechdrs[sym->st_shndx].sh_name, ".debug"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 return 'n';
1829 return '?';
1830}
1831
1832static void add_kallsyms(struct module *mod,
1833 Elf_Shdr *sechdrs,
1834 unsigned int symindex,
1835 unsigned int strindex,
1836 const char *secstrings)
1837{
1838 unsigned int i;
1839
1840 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1841 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1842 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1843
1844 /* Set types up while we still have access to sections. */
1845 for (i = 0; i < mod->num_symtab; i++)
1846 mod->symtab[i].st_info
1847 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1848}
1849#else
1850static inline void add_kallsyms(struct module *mod,
1851 Elf_Shdr *sechdrs,
1852 unsigned int symindex,
1853 unsigned int strindex,
1854 const char *secstrings)
1855{
1856}
1857#endif /* CONFIG_KALLSYMS */
1858
Jason Barone9d376f2009-02-05 11:51:38 -05001859static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05001860{
Jason Barone9d376f2009-02-05 11:51:38 -05001861#ifdef CONFIG_DYNAMIC_DEBUG
1862 if (ddebug_add_module(debug, num, debug->modname))
1863 printk(KERN_ERR "dynamic debug error adding module: %s\n",
1864 debug->modname);
1865#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05001866}
Jason Baron346e15b2008-08-12 16:46:19 -04001867
Rusty Russell3a642e92008-07-22 19:24:28 -05001868static void *module_alloc_update_bounds(unsigned long size)
1869{
1870 void *ret = module_alloc(size);
1871
1872 if (ret) {
1873 /* Update module bounds. */
1874 if ((unsigned long)ret < module_addr_min)
1875 module_addr_min = (unsigned long)ret;
1876 if ((unsigned long)ret + size > module_addr_max)
1877 module_addr_max = (unsigned long)ret + size;
1878 }
1879 return ret;
1880}
1881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882/* Allocate and load the module: note that size of section 0 is always
1883 zero, and we rely on this for optional sections. */
Linus Torvaldsffb4ba72008-08-25 11:10:26 -07001884static noinline struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 unsigned long len,
1886 const char __user *uargs)
1887{
1888 Elf_Ehdr *hdr;
1889 Elf_Shdr *sechdrs;
1890 char *secstrings, *args, *modmagic, *strtab = NULL;
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07001891 char *staging;
Andrew Morton84860f92006-06-28 04:26:46 -07001892 unsigned int i;
1893 unsigned int symindex = 0;
1894 unsigned int strindex = 0;
Rusty Russell5e458cc2008-10-22 10:00:13 -05001895 unsigned int modindex, versindex, infoindex, pcpuindex;
Rusty Russelle180a6b2009-03-31 13:05:29 -06001896 unsigned int num_mcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 struct module *mod;
1898 long err = 0;
1899 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
Rusty Russell5e458cc2008-10-22 10:00:13 -05001900 unsigned long *mseg;
Thomas Koeller378bac82005-09-06 15:17:11 -07001901 mm_segment_t old_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
1903 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1904 umod, len, uargs);
1905 if (len < sizeof(*hdr))
1906 return ERR_PTR(-ENOEXEC);
1907
1908 /* Suck in entire file: we'll want most of it. */
1909 /* vmalloc barfs on "unusual" numbers. Check here */
1910 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1911 return ERR_PTR(-ENOMEM);
Heiko Carstens9e018922008-12-22 12:36:31 +01001912
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 if (copy_from_user(hdr, umod, len) != 0) {
1914 err = -EFAULT;
1915 goto free_hdr;
1916 }
1917
1918 /* Sanity checks against insmoding binaries or wrong arch,
1919 weird elf version */
Cyrill Gorcunovc4ea6fc2008-05-14 16:27:29 -07001920 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 || hdr->e_type != ET_REL
1922 || !elf_check_arch(hdr)
1923 || hdr->e_shentsize != sizeof(*sechdrs)) {
1924 err = -ENOEXEC;
1925 goto free_hdr;
1926 }
1927
1928 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1929 goto truncated;
1930
1931 /* Convenience variables */
1932 sechdrs = (void *)hdr + hdr->e_shoff;
1933 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1934 sechdrs[0].sh_addr = 0;
1935
1936 for (i = 1; i < hdr->e_shnum; i++) {
1937 if (sechdrs[i].sh_type != SHT_NOBITS
1938 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1939 goto truncated;
1940
1941 /* Mark all sections sh_addr with their address in the
1942 temporary image. */
1943 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1944
1945 /* Internal symbols and strings. */
1946 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1947 symindex = i;
1948 strindex = sechdrs[i].sh_link;
1949 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1950 }
1951#ifndef CONFIG_MODULE_UNLOAD
1952 /* Don't load .exit sections */
Rusty Russell49502672009-03-31 13:05:36 -06001953 if (strstarts(secstrings+sechdrs[i].sh_name, ".exit"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1955#endif
1956 }
1957
1958 modindex = find_sec(hdr, sechdrs, secstrings,
1959 ".gnu.linkonce.this_module");
1960 if (!modindex) {
1961 printk(KERN_WARNING "No module found in object\n");
1962 err = -ENOEXEC;
1963 goto free_hdr;
1964 }
Rusty Russell5e458cc2008-10-22 10:00:13 -05001965 /* This is temporary: point mod into copy of data. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 mod = (void *)sechdrs[modindex].sh_addr;
1967
1968 if (symindex == 0) {
1969 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1970 mod->name);
1971 err = -ENOEXEC;
1972 goto free_hdr;
1973 }
1974
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1976 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1977 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1978
Rusty Russellea01e792008-03-13 09:02:17 +00001979 /* Don't keep modinfo and version sections. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russellea01e792008-03-13 09:02:17 +00001981 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982#ifdef CONFIG_KALLSYMS
1983 /* Keep symbol and string tables for decoding later. */
1984 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1985 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1986#endif
1987
1988 /* Check module struct version now, before we try to use module. */
1989 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1990 err = -ENOEXEC;
1991 goto free_hdr;
1992 }
1993
1994 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1995 /* This is allowed: modprobe --force will invalidate it. */
1996 if (!modmagic) {
Rusty Russellc6e665c2009-03-31 13:05:33 -06001997 err = try_to_force_load(mod, "bad vermagic");
Linus Torvalds826e4502008-05-04 17:04:16 -07001998 if (err)
1999 goto free_hdr;
Rusty Russell91e37a72008-05-09 16:25:28 +10002000 } else if (!same_magic(modmagic, vermagic, versindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2002 mod->name, modmagic, vermagic);
2003 err = -ENOEXEC;
2004 goto free_hdr;
2005 }
2006
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002007 staging = get_modinfo(sechdrs, infoindex, "staging");
2008 if (staging) {
2009 add_taint_module(mod, TAINT_CRAP);
2010 printk(KERN_WARNING "%s: module is from the staging directory,"
2011 " the quality is unknown, you have been warned.\n",
2012 mod->name);
2013 }
2014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 /* Now copy in args */
Davi Arnaut24277dd2006-03-24 03:18:43 -08002016 args = strndup_user(uargs, ~0UL >> 1);
2017 if (IS_ERR(args)) {
2018 err = PTR_ERR(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 goto free_hdr;
2020 }
Andrew Morton8e08b752006-02-07 12:58:45 -08002021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 if (find_module(mod->name)) {
2023 err = -EEXIST;
2024 goto free_mod;
2025 }
2026
2027 mod->state = MODULE_STATE_COMING;
2028
2029 /* Allow arches to frob section contents and sizes. */
2030 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
2031 if (err < 0)
2032 goto free_mod;
2033
2034 if (pcpuindex) {
2035 /* We have a special allocation for this section. */
2036 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
Rusty Russell842bbaa2005-08-01 21:11:47 -07002037 sechdrs[pcpuindex].sh_addralign,
2038 mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 if (!percpu) {
2040 err = -ENOMEM;
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002041 goto free_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 }
2043 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2044 mod->percpu = percpu;
2045 }
2046
2047 /* Determine total sizes, and put offsets in sh_entsize. For now
2048 this is done generically; there doesn't appear to be any
2049 special cases for the architectures. */
2050 layout_sections(mod, hdr, sechdrs, secstrings);
2051
2052 /* Do the allocs. */
Rusty Russell3a642e92008-07-22 19:24:28 -05002053 ptr = module_alloc_update_bounds(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 if (!ptr) {
2055 err = -ENOMEM;
2056 goto free_percpu;
2057 }
2058 memset(ptr, 0, mod->core_size);
2059 mod->module_core = ptr;
2060
Rusty Russell3a642e92008-07-22 19:24:28 -05002061 ptr = module_alloc_update_bounds(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 if (!ptr && mod->init_size) {
2063 err = -ENOMEM;
2064 goto free_core;
2065 }
2066 memset(ptr, 0, mod->init_size);
2067 mod->module_init = ptr;
2068
2069 /* Transfer each section which specifies SHF_ALLOC */
2070 DEBUGP("final section addresses:\n");
2071 for (i = 0; i < hdr->e_shnum; i++) {
2072 void *dest;
2073
2074 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2075 continue;
2076
2077 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2078 dest = mod->module_init
2079 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2080 else
2081 dest = mod->module_core + sechdrs[i].sh_entsize;
2082
2083 if (sechdrs[i].sh_type != SHT_NOBITS)
2084 memcpy(dest, (void *)sechdrs[i].sh_addr,
2085 sechdrs[i].sh_size);
2086 /* Update sh_addr to point to copy in image. */
2087 sechdrs[i].sh_addr = (unsigned long)dest;
2088 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2089 }
2090 /* Module has been moved. */
2091 mod = (void *)sechdrs[modindex].sh_addr;
2092
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002093#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2094 mod->refptr = percpu_modalloc(sizeof(local_t), __alignof__(local_t),
2095 mod->name);
2096 if (!mod->refptr) {
2097 err = -ENOMEM;
2098 goto free_init;
2099 }
2100#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 /* Now we've moved module, initialize linked lists, etc. */
2102 module_unload_init(mod);
2103
Kay Sievers97c146e2007-11-29 23:46:11 +01002104 /* add kobject, so we can reference it. */
Akinobu Mitad58ae672007-10-16 23:30:27 -07002105 err = mod_sysfs_init(mod);
2106 if (err)
Kay Sievers97c146e2007-11-29 23:46:11 +01002107 goto free_unload;
Kay Sievers270a6c42007-01-18 13:26:15 +01002108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 /* Set up license info based on the info section */
2110 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2111
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002112 /*
2113 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2114 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2115 * using GPL-only symbols it needs.
2116 */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002117 if (strcmp(mod->name, "ndiswrapper") == 0)
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002118 add_taint(TAINT_PROPRIETARY_MODULE);
2119
2120 /* driverloader was caught wrongly pretending to be under GPL */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002121 if (strcmp(mod->name, "driverloader") == 0)
2122 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Dave Jones9841d61d2006-01-08 01:03:41 -08002123
Matt Domschc988d2b2005-06-23 22:05:15 -07002124 /* Set up MODINFO_ATTR fields */
2125 setup_modinfo(mod, sechdrs, infoindex);
Matt Domschc988d2b2005-06-23 22:05:15 -07002126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 /* Fix up syms, so that st_value is a pointer to location. */
2128 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2129 mod);
2130 if (err < 0)
2131 goto cleanup;
2132
Rusty Russell5e458cc2008-10-22 10:00:13 -05002133 /* Now we've got everything in the final locations, we can
2134 * find optional sections. */
Rusty Russelle180a6b2009-03-31 13:05:29 -06002135 mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
2136 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell5e458cc2008-10-22 10:00:13 -05002137 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2138 sizeof(*mod->syms), &mod->num_syms);
2139 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2140 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2141 sizeof(*mod->gpl_syms),
2142 &mod->num_gpl_syms);
2143 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2144 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2145 "__ksymtab_gpl_future",
2146 sizeof(*mod->gpl_future_syms),
2147 &mod->num_gpl_future_syms);
2148 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2149 "__kcrctab_gpl_future");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002151#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002152 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2153 "__ksymtab_unused",
2154 sizeof(*mod->unused_syms),
2155 &mod->num_unused_syms);
2156 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2157 "__kcrctab_unused");
2158 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2159 "__ksymtab_unused_gpl",
2160 sizeof(*mod->unused_gpl_syms),
2161 &mod->num_unused_gpl_syms);
2162 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2163 "__kcrctab_unused_gpl");
2164#endif
2165
2166#ifdef CONFIG_MARKERS
2167 mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers",
2168 sizeof(*mod->markers), &mod->num_markers);
2169#endif
2170#ifdef CONFIG_TRACEPOINTS
2171 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2172 "__tracepoints",
2173 sizeof(*mod->tracepoints),
2174 &mod->num_tracepoints);
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002175#endif
Steven Rostedt6d723732009-04-10 14:53:50 -04002176#ifdef CONFIG_EVENT_TRACING
2177 mod->trace_events = section_objs(hdr, sechdrs, secstrings,
2178 "_ftrace_events",
2179 sizeof(*mod->trace_events),
2180 &mod->num_trace_events);
2181#endif
Arjan van de Venf71d20e2006-06-28 04:26:45 -07002182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183#ifdef CONFIG_MODVERSIONS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002184 if ((mod->num_syms && !mod->crcs)
2185 || (mod->num_gpl_syms && !mod->gpl_crcs)
2186 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002187#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002188 || (mod->num_unused_syms && !mod->unused_crcs)
2189 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002190#endif
2191 ) {
Rusty Russellc6e665c2009-03-31 13:05:33 -06002192 err = try_to_force_load(mod,
2193 "no versions for exported symbols");
Linus Torvalds826e4502008-05-04 17:04:16 -07002194 if (err)
2195 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 }
2197#endif
2198
2199 /* Now do relocations. */
2200 for (i = 1; i < hdr->e_shnum; i++) {
2201 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2202 unsigned int info = sechdrs[i].sh_info;
2203
2204 /* Not a valid relocation section? */
2205 if (info >= hdr->e_shnum)
2206 continue;
2207
2208 /* Don't bother with non-allocated sections */
2209 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2210 continue;
2211
2212 if (sechdrs[i].sh_type == SHT_REL)
2213 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2214 else if (sechdrs[i].sh_type == SHT_RELA)
2215 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2216 mod);
2217 if (err < 0)
2218 goto cleanup;
2219 }
2220
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002221 /* Find duplicate symbols */
2222 err = verify_export_symbols(mod);
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002223 if (err < 0)
2224 goto cleanup;
2225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 /* Set up and sort exception table */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002227 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2228 sizeof(*mod->extable), &mod->num_exentries);
2229 sort_extable(mod->extable, mod->extable + mod->num_exentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
2231 /* Finally, copy percpu area over. */
2232 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2233 sechdrs[pcpuindex].sh_size);
2234
2235 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2236
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002237 if (!mod->taints) {
Jason Barone9d376f2009-02-05 11:51:38 -05002238 struct _ddebug *debug;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002239 unsigned int num_debug;
2240
Rusty Russell5e458cc2008-10-22 10:00:13 -05002241 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2242 sizeof(*debug), &num_debug);
Jason Barone9d376f2009-02-05 11:51:38 -05002243 if (debug)
2244 dynamic_debug_setup(debug, num_debug);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002245 }
Steven Rostedt90d595f2008-08-14 15:45:09 -04002246
Steven Rostedtfed19392008-08-14 22:47:19 -04002247 /* sechdrs[0].sh_size is always zero */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002248 mseg = section_objs(hdr, sechdrs, secstrings, "__mcount_loc",
2249 sizeof(*mseg), &num_mcount);
Steven Rostedt31e88902008-11-14 16:21:19 -08002250 ftrace_init_module(mod, mseg, mseg + num_mcount);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 err = module_finalize(hdr, sechdrs, mod);
2253 if (err < 0)
2254 goto cleanup;
2255
Thomas Koeller378bac82005-09-06 15:17:11 -07002256 /* flush the icache in correct context */
2257 old_fs = get_fs();
2258 set_fs(KERNEL_DS);
2259
2260 /*
2261 * Flush the instruction cache, since we've played with text.
2262 * Do it before processing of module parameters, so the module
2263 * can provide parameter accessor functions of its own.
2264 */
2265 if (mod->module_init)
2266 flush_icache_range((unsigned long)mod->module_init,
2267 (unsigned long)mod->module_init
2268 + mod->init_size);
2269 flush_icache_range((unsigned long)mod->module_core,
2270 (unsigned long)mod->module_core + mod->core_size);
2271
2272 set_fs(old_fs);
2273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 mod->args = args;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002275 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002276 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2277 mod->name);
2278
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002279 /* Now sew it into the lists so we can get lockdep and oops
Andi Kleend72b3752008-08-30 10:09:00 +02002280 * info during argument parsing. Noone should access us, since
2281 * strong_try_module_get() will fail.
2282 * lockdep/oops can run asynchronous, so use the RCU list insertion
2283 * function to insert in a way safe to concurrent readers.
2284 * The mutex protects against concurrent writers.
2285 */
2286 list_add_rcu(&mod->list, &modules);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002287
Rusty Russelle180a6b2009-03-31 13:05:29 -06002288 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002290 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
Rusty Russelle180a6b2009-03-31 13:05:29 -06002292 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002294 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Roland McGrath6d760132007-10-16 23:26:40 -07002296 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
2298 /* Get rid of temporary copy */
2299 vfree(hdr);
2300
2301 /* Done! */
2302 return mod;
2303
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002304 unlink:
Rusty Russelle91defa2009-03-31 13:05:35 -06002305 /* Unlink carefully: kallsyms could be walking list. */
2306 list_del_rcu(&mod->list);
2307 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 module_arch_cleanup(mod);
2309 cleanup:
Kay Sievers97c146e2007-11-29 23:46:11 +01002310 kobject_del(&mod->mkobj.kobj);
2311 kobject_put(&mod->mkobj.kobj);
Steven Rostedtfed19392008-08-14 22:47:19 -04002312 ftrace_release(mod->module_core, mod->core_size);
Kay Sievers97c146e2007-11-29 23:46:11 +01002313 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 module_unload_free(mod);
Eric Dumazet720eba32009-02-03 13:31:36 +10302315#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
Américo Wangb10153f2009-03-25 00:07:19 +08002316 free_init:
Eric Dumazet720eba32009-02-03 13:31:36 +10302317 percpu_modfree(mod->refptr);
2318#endif
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002319 module_free(mod, mod->module_init);
2320 free_core:
2321 module_free(mod, mod->module_core);
2322 /* mod will be freed with core. Don't access it beyond this line! */
2323 free_percpu:
2324 if (percpu)
2325 percpu_modfree(percpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 free_mod:
2327 kfree(args);
2328 free_hdr:
2329 vfree(hdr);
Jayachandran C6fe2e702006-01-06 00:19:54 -08002330 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
2332 truncated:
2333 printk(KERN_ERR "Module len %lu truncated\n", len);
2334 err = -ENOEXEC;
2335 goto free_hdr;
2336}
2337
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338/* This is where the real work happens */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002339SYSCALL_DEFINE3(init_module, void __user *, umod,
2340 unsigned long, len, const char __user *, uargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341{
2342 struct module *mod;
2343 int ret = 0;
2344
2345 /* Must have permission */
2346 if (!capable(CAP_SYS_MODULE))
2347 return -EPERM;
2348
2349 /* Only one module load at a time, please */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002350 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 return -EINTR;
2352
2353 /* Do all the hard work */
2354 mod = load_module(umod, len, uargs);
2355 if (IS_ERR(mod)) {
Ashutosh Naik6389a382006-03-23 03:00:46 -08002356 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 return PTR_ERR(mod);
2358 }
2359
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 /* Drop lock so they can recurse */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002361 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Alan Sterne041c682006-03-27 01:16:30 -08002363 blocking_notifier_call_chain(&module_notify_list,
2364 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
2366 /* Start the module */
2367 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002368 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 if (ret < 0) {
2370 /* Init routine failed: abort. Try to protect us from
2371 buggy refcounters. */
2372 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002373 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002374 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002375 blocking_notifier_call_chain(&module_notify_list,
2376 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002377 mutex_lock(&module_mutex);
2378 free_module(mod);
2379 mutex_unlock(&module_mutex);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002380 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 return ret;
2382 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002383 if (ret > 0) {
2384 printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
2385 "it should follow 0/-E convention\n"
2386 KERN_WARNING "%s: loading module anyway...\n",
2387 __func__, mod->name, ret,
2388 __func__);
2389 dump_stack();
2390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Rusty Russell6c5db222008-03-10 11:43:52 -07002392 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002394 wake_up(&module_wq);
Masami Hiramatsu0deddf432009-01-06 14:41:54 -08002395 blocking_notifier_call_chain(&module_notify_list,
2396 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07002397
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07002398 /* We need to finish all async code before the module init sequence is done */
2399 async_synchronize_full();
2400
Rusty Russell6c5db222008-03-10 11:43:52 -07002401 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 /* Drop initial reference. */
2403 module_put(mod);
2404 module_free(mod, mod->module_init);
2405 mod->module_init = NULL;
2406 mod->init_size = 0;
2407 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002408 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
2410 return 0;
2411}
2412
2413static inline int within(unsigned long addr, void *start, unsigned long size)
2414{
2415 return ((void *)addr >= start && (void *)addr < start + size);
2416}
2417
2418#ifdef CONFIG_KALLSYMS
2419/*
2420 * This ignores the intensely annoying "mapping symbols" found
2421 * in ARM ELF files: $a, $t and $d.
2422 */
2423static inline int is_arm_mapping_symbol(const char *str)
2424{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002425 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 && (str[2] == '\0' || str[2] == '.');
2427}
2428
2429static const char *get_ksymbol(struct module *mod,
2430 unsigned long addr,
2431 unsigned long *size,
2432 unsigned long *offset)
2433{
2434 unsigned int i, best = 0;
2435 unsigned long nextval;
2436
2437 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002438 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002440 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2442
2443 /* Scan for closest preceeding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002444 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 for (i = 1; i < mod->num_symtab; i++) {
2446 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2447 continue;
2448
2449 /* We ignore unnamed symbols: they're uninformative
2450 * and inserted at a whim. */
2451 if (mod->symtab[i].st_value <= addr
2452 && mod->symtab[i].st_value > mod->symtab[best].st_value
2453 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2454 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2455 best = i;
2456 if (mod->symtab[i].st_value > addr
2457 && mod->symtab[i].st_value < nextval
2458 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2459 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2460 nextval = mod->symtab[i].st_value;
2461 }
2462
2463 if (!best)
2464 return NULL;
2465
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002466 if (size)
2467 *size = nextval - mod->symtab[best].st_value;
2468 if (offset)
2469 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 return mod->strtab + mod->symtab[best].st_name;
2471}
2472
Rusty Russell6dd06c92008-01-29 17:13:22 -05002473/* For kallsyms to ask for address resolution. NULL means not found. Careful
2474 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002475const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05002476 unsigned long *size,
2477 unsigned long *offset,
2478 char **modname,
2479 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480{
2481 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002482 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Rusty Russellcb2a5202008-01-14 00:55:03 -08002484 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002485 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002486 if (within_module_init(addr, mod) ||
2487 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002488 if (modname)
2489 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002490 ret = get_ksymbol(mod, addr, size, offset);
2491 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 }
2493 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05002494 /* Make a copy in here where it's safe */
2495 if (ret) {
2496 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2497 ret = namebuf;
2498 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002499 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002500 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501}
2502
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002503int lookup_module_symbol_name(unsigned long addr, char *symname)
2504{
2505 struct module *mod;
2506
Rusty Russellcb2a5202008-01-14 00:55:03 -08002507 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002508 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002509 if (within_module_init(addr, mod) ||
2510 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002511 const char *sym;
2512
2513 sym = get_ksymbol(mod, addr, NULL, NULL);
2514 if (!sym)
2515 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07002516 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002517 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002518 return 0;
2519 }
2520 }
2521out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002522 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002523 return -ERANGE;
2524}
2525
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002526int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2527 unsigned long *offset, char *modname, char *name)
2528{
2529 struct module *mod;
2530
Rusty Russellcb2a5202008-01-14 00:55:03 -08002531 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002532 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002533 if (within_module_init(addr, mod) ||
2534 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002535 const char *sym;
2536
2537 sym = get_ksymbol(mod, addr, size, offset);
2538 if (!sym)
2539 goto out;
2540 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07002541 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002542 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07002543 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002544 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002545 return 0;
2546 }
2547 }
2548out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002549 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002550 return -ERANGE;
2551}
2552
Alexey Dobriyanea078902007-05-08 00:28:39 -07002553int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2554 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555{
2556 struct module *mod;
2557
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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 if (symnum < mod->num_symtab) {
2561 *value = mod->symtab[symnum].st_value;
2562 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002563 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07002564 KSYM_NAME_LEN);
2565 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06002566 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002567 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002568 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 }
2570 symnum -= mod->num_symtab;
2571 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002572 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002573 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574}
2575
2576static unsigned long mod_find_symname(struct module *mod, const char *name)
2577{
2578 unsigned int i;
2579
2580 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002581 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2582 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 return mod->symtab[i].st_value;
2584 return 0;
2585}
2586
2587/* Look for this name: can be of form module:name. */
2588unsigned long module_kallsyms_lookup_name(const char *name)
2589{
2590 struct module *mod;
2591 char *colon;
2592 unsigned long ret = 0;
2593
2594 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08002595 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 if ((colon = strchr(name, ':')) != NULL) {
2597 *colon = '\0';
2598 if ((mod = find_module(name)) != NULL)
2599 ret = mod_find_symname(mod, colon+1);
2600 *colon = ':';
2601 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02002602 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 if ((ret = mod_find_symname(mod, name)) != 0)
2604 break;
2605 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002606 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 return ret;
2608}
Anders Kaseorg75a66612008-12-05 19:03:58 -05002609
2610int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
2611 struct module *, unsigned long),
2612 void *data)
2613{
2614 struct module *mod;
2615 unsigned int i;
2616 int ret;
2617
2618 list_for_each_entry(mod, &modules, list) {
2619 for (i = 0; i < mod->num_symtab; i++) {
2620 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
2621 mod, mod->symtab[i].st_value);
2622 if (ret != 0)
2623 return ret;
2624 }
2625 }
2626 return 0;
2627}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628#endif /* CONFIG_KALLSYMS */
2629
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002630static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002631{
2632 int bx = 0;
2633
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002634 if (mod->taints ||
2635 mod->state == MODULE_STATE_GOING ||
2636 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002637 buf[bx++] = '(';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002638 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002639 buf[bx++] = 'P';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002640 if (mod->taints & (1 << TAINT_FORCED_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002641 buf[bx++] = 'F';
Linus Torvalds26e9a392008-10-17 09:50:12 -07002642 if (mod->taints & (1 << TAINT_CRAP))
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002643 buf[bx++] = 'C';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002644 /*
2645 * TAINT_FORCED_RMMOD: could be added.
2646 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2647 * apply to modules.
2648 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002649
2650 /* Show a - for module-is-being-unloaded */
2651 if (mod->state == MODULE_STATE_GOING)
2652 buf[bx++] = '-';
2653 /* Show a + for module-is-being-loaded */
2654 if (mod->state == MODULE_STATE_COMING)
2655 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002656 buf[bx++] = ')';
2657 }
2658 buf[bx] = '\0';
2659
2660 return buf;
2661}
2662
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002663#ifdef CONFIG_PROC_FS
2664/* Called by the /proc file system to return a list of modules. */
2665static void *m_start(struct seq_file *m, loff_t *pos)
2666{
2667 mutex_lock(&module_mutex);
2668 return seq_list_start(&modules, *pos);
2669}
2670
2671static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2672{
2673 return seq_list_next(p, &modules, pos);
2674}
2675
2676static void m_stop(struct seq_file *m, void *p)
2677{
2678 mutex_unlock(&module_mutex);
2679}
2680
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681static int m_show(struct seq_file *m, void *p)
2682{
2683 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002684 char buf[8];
2685
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05002686 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 mod->name, mod->init_size + mod->core_size);
2688 print_unload_info(m, mod);
2689
2690 /* Informative for users. */
2691 seq_printf(m, " %s",
2692 mod->state == MODULE_STATE_GOING ? "Unloading":
2693 mod->state == MODULE_STATE_COMING ? "Loading":
2694 "Live");
2695 /* Used by oprofile and other similar tools. */
2696 seq_printf(m, " 0x%p", mod->module_core);
2697
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002698 /* Taints info */
2699 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002700 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002701
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 seq_printf(m, "\n");
2703 return 0;
2704}
2705
2706/* Format: modulename size refcount deps address
2707
2708 Where refcount is a number or -, and deps is a comma-separated list
2709 of depends or -.
2710*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002711static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 .start = m_start,
2713 .next = m_next,
2714 .stop = m_stop,
2715 .show = m_show
2716};
2717
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002718static int modules_open(struct inode *inode, struct file *file)
2719{
2720 return seq_open(file, &modules_op);
2721}
2722
2723static const struct file_operations proc_modules_operations = {
2724 .open = modules_open,
2725 .read = seq_read,
2726 .llseek = seq_lseek,
2727 .release = seq_release,
2728};
2729
2730static int __init proc_modules_init(void)
2731{
2732 proc_create("modules", 0, NULL, &proc_modules_operations);
2733 return 0;
2734}
2735module_init(proc_modules_init);
2736#endif
2737
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738/* Given an address, look for it in the module exception tables. */
2739const struct exception_table_entry *search_module_extables(unsigned long addr)
2740{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 const struct exception_table_entry *e = NULL;
2742 struct module *mod;
2743
Rusty Russell24da1cb2007-07-15 23:41:46 -07002744 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002745 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 if (mod->num_exentries == 0)
2747 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002748
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 e = search_extable(mod->extable,
2750 mod->extable + mod->num_exentries - 1,
2751 addr);
2752 if (e)
2753 break;
2754 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07002755 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
2757 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07002758 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 return e;
2760}
2761
Ingo Molnar4d435f92006-07-03 00:24:24 -07002762/*
Rusty Russelle6104992009-03-31 13:05:31 -06002763 * is_module_address - is this address inside a module?
2764 * @addr: the address to check.
2765 *
2766 * See is_module_text_address() if you simply want to see if the address
2767 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07002768 */
Rusty Russelle6104992009-03-31 13:05:31 -06002769bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07002770{
Rusty Russelle6104992009-03-31 13:05:31 -06002771 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07002772
Rusty Russell24da1cb2007-07-15 23:41:46 -07002773 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06002774 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07002775 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002776
Rusty Russelle6104992009-03-31 13:05:31 -06002777 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07002778}
2779
Rusty Russelle6104992009-03-31 13:05:31 -06002780/*
2781 * __module_address - get the module which contains an address.
2782 * @addr: the address.
2783 *
2784 * Must be called with preempt disabled or module mutex held so that
2785 * module doesn't get freed during this.
2786 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07002787struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788{
2789 struct module *mod;
2790
Rusty Russell3a642e92008-07-22 19:24:28 -05002791 if (addr < module_addr_min || addr > module_addr_max)
2792 return NULL;
2793
Andi Kleend72b3752008-08-30 10:09:00 +02002794 list_for_each_entry_rcu(mod, &modules, list)
Rusty Russelle6104992009-03-31 13:05:31 -06002795 if (within_module_core(addr, mod)
2796 || within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 return mod;
2798 return NULL;
2799}
Tim Abbottc6b37802008-12-05 19:03:59 -05002800EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
Rusty Russelle6104992009-03-31 13:05:31 -06002802/*
2803 * is_module_text_address - is this address inside module code?
2804 * @addr: the address to check.
2805 *
2806 * See is_module_address() if you simply want to see if the address is
2807 * anywhere in a module. See kernel_text_address() for testing if an
2808 * address corresponds to kernel or module code.
2809 */
2810bool is_module_text_address(unsigned long addr)
2811{
2812 bool ret;
2813
2814 preempt_disable();
2815 ret = __module_text_address(addr) != NULL;
2816 preempt_enable();
2817
2818 return ret;
2819}
2820
2821/*
2822 * __module_text_address - get the module whose code contains an address.
2823 * @addr: the address.
2824 *
2825 * Must be called with preempt disabled or module mutex held so that
2826 * module doesn't get freed during this.
2827 */
2828struct module *__module_text_address(unsigned long addr)
2829{
2830 struct module *mod = __module_address(addr);
2831 if (mod) {
2832 /* Make sure it's within the text section. */
2833 if (!within(addr, mod->module_init, mod->init_text_size)
2834 && !within(addr, mod->module_core, mod->core_text_size))
2835 mod = NULL;
2836 }
2837 return mod;
2838}
Tim Abbottc6b37802008-12-05 19:03:59 -05002839EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06002840
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841/* Don't grab lock, we're oopsing. */
2842void print_modules(void)
2843{
2844 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07002845 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
2847 printk("Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02002848 /* Most callers should already have preempt disabled, but make sure */
2849 preempt_disable();
2850 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002851 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02002852 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01002853 if (last_unloaded_module[0])
2854 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 printk("\n");
2856}
2857
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06002859/* Generate the signature for all relevant module structures here.
2860 * If these change, we don't want to try to parse the module. */
2861void module_layout(struct module *mod,
2862 struct modversion_info *ver,
2863 struct kernel_param *kp,
2864 struct kernel_symbol *ks,
2865 struct marker *marker,
2866 struct tracepoint *tp)
2867{
2868}
2869EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002871
2872#ifdef CONFIG_MARKERS
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002873void module_update_markers(void)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002874{
2875 struct module *mod;
2876
2877 mutex_lock(&module_mutex);
2878 list_for_each_entry(mod, &modules, list)
2879 if (!mod->taints)
2880 marker_update_probe_range(mod->markers,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002881 mod->markers + mod->num_markers);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002882 mutex_unlock(&module_mutex);
2883}
2884#endif
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002885
2886#ifdef CONFIG_TRACEPOINTS
2887void module_update_tracepoints(void)
2888{
2889 struct module *mod;
2890
2891 mutex_lock(&module_mutex);
2892 list_for_each_entry(mod, &modules, list)
2893 if (!mod->taints)
2894 tracepoint_update_probe_range(mod->tracepoints,
2895 mod->tracepoints + mod->num_tracepoints);
2896 mutex_unlock(&module_mutex);
2897}
2898
2899/*
2900 * Returns 0 if current not found.
2901 * Returns 1 if current found.
2902 */
2903int module_get_iter_tracepoints(struct tracepoint_iter *iter)
2904{
2905 struct module *iter_mod;
2906 int found = 0;
2907
2908 mutex_lock(&module_mutex);
2909 list_for_each_entry(iter_mod, &modules, list) {
2910 if (!iter_mod->taints) {
2911 /*
2912 * Sorted module list
2913 */
2914 if (iter_mod < iter->module)
2915 continue;
2916 else if (iter_mod > iter->module)
2917 iter->tracepoint = NULL;
2918 found = tracepoint_get_iter_range(&iter->tracepoint,
2919 iter_mod->tracepoints,
2920 iter_mod->tracepoints
2921 + iter_mod->num_tracepoints);
2922 if (found) {
2923 iter->module = iter_mod;
2924 break;
2925 }
2926 }
2927 }
2928 mutex_unlock(&module_mutex);
2929 return found;
2930}
2931#endif