blob: 5cd55ab15daf90cd5dce3f7cfa1637bd4ff1e574 [file] [log] [blame]
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 Copyright (C) 2002 Richard Henderson
3 Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
20#include <linux/moduleloader.h>
Steven Rostedt6d723732009-04-10 14:53:50 -040021#include <linux/ftrace_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
Alexey Dobriyanae84e322007-05-08 00:28:38 -070023#include <linux/kallsyms.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040024#include <linux/fs.h>
Roland McGrath6d760132007-10-16 23:26:40 -070025#include <linux/sysfs.h>
Randy Dunlap9f158332005-09-13 01:25:16 -070026#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/slab.h>
28#include <linux/vmalloc.h>
29#include <linux/elf.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040030#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/seq_file.h>
32#include <linux/syscalls.h>
33#include <linux/fcntl.h>
34#include <linux/rcupdate.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080035#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/cpu.h>
37#include <linux/moduleparam.h>
38#include <linux/errno.h>
39#include <linux/err.h>
40#include <linux/vermagic.h>
41#include <linux/notifier.h>
Al Virof6a57032006-10-18 01:47:25 -040042#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/stop_machine.h>
44#include <linux/device.h>
Matt Domschc988d2b2005-06-23 22:05:15 -070045#include <linux/string.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080046#include <linux/mutex.h>
Andi Kleend72b3752008-08-30 10:09:00 +020047#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/cacheflush.h>
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020050#include <linux/license.h>
Christoph Lameter6d762392008-02-08 04:18:42 -080051#include <asm/sections.h>
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040052#include <linux/tracepoint.h>
Steven Rostedt90d595f2008-08-14 15:45:09 -040053#include <linux/ftrace.h>
Arjan van de Ven22a9d642009-01-07 08:45:46 -080054#include <linux/async.h>
Tejun Heofbf59bc2009-02-20 16:29:08 +090055#include <linux/percpu.h>
Catalin Marinas4f2294b2009-06-11 13:23:20 +010056#include <linux/kmemleak.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#if 0
59#define DEBUGP printk
60#else
61#define DEBUGP(fmt , a...)
62#endif
63
64#ifndef ARCH_SHF_SMALL
65#define ARCH_SHF_SMALL 0
66#endif
67
68/* If this is set, the section belongs in the init part of the module */
69#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
70
Rusty Russell24da1cb2007-07-15 23:41:46 -070071/* List of modules, protected by module_mutex or preempt_disable
Andi Kleend72b3752008-08-30 10:09:00 +020072 * (delete uses stop_machine/add uses RCU list operations). */
Tim Abbottc6b37802008-12-05 19:03:59 -050073DEFINE_MUTEX(module_mutex);
74EXPORT_SYMBOL_GPL(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static LIST_HEAD(modules);
76
Rusty Russellc9a3ba52008-01-29 17:13:18 -050077/* Waiting for a module to finish initializing? */
78static DECLARE_WAIT_QUEUE_HEAD(module_wq);
79
Alan Sterne041c682006-03-27 01:16:30 -080080static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Rusty Russelle6104992009-03-31 13:05:31 -060082/* Bounds of module allocation, for speeding __module_address */
Rusty Russell3a642e92008-07-22 19:24:28 -050083static unsigned long module_addr_min = -1UL, module_addr_max = 0;
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085int register_module_notifier(struct notifier_block * nb)
86{
Alan Sterne041c682006-03-27 01:16:30 -080087 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89EXPORT_SYMBOL(register_module_notifier);
90
91int unregister_module_notifier(struct notifier_block * nb)
92{
Alan Sterne041c682006-03-27 01:16:30 -080093 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95EXPORT_SYMBOL(unregister_module_notifier);
96
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -080097/* We require a truly strong try_module_get(): 0 means failure due to
98 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static inline int strong_try_module_get(struct module *mod)
100{
101 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500102 return -EBUSY;
103 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500105 else
106 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700109static inline void add_taint_module(struct module *mod, unsigned flag)
110{
111 add_taint(flag);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700112 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700113}
114
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200115/*
116 * A thread that wants to hold a reference to a module only while it
117 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 */
119void __module_put_and_exit(struct module *mod, long code)
120{
121 module_put(mod);
122 do_exit(code);
123}
124EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126/* Find a module section: 0 means not found. */
127static unsigned int find_sec(Elf_Ehdr *hdr,
128 Elf_Shdr *sechdrs,
129 const char *secstrings,
130 const char *name)
131{
132 unsigned int i;
133
134 for (i = 1; i < hdr->e_shnum; i++)
135 /* Alloc bit cleared means "ignore it." */
136 if ((sechdrs[i].sh_flags & SHF_ALLOC)
137 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
138 return i;
139 return 0;
140}
141
Rusty Russell5e458cc2008-10-22 10:00:13 -0500142/* Find a module section, or NULL. */
143static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
144 const char *secstrings, const char *name)
145{
146 /* Section 0 has sh_addr 0. */
147 return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
148}
149
150/* Find a module section, or NULL. Fill in number of "objects" in section. */
151static void *section_objs(Elf_Ehdr *hdr,
152 Elf_Shdr *sechdrs,
153 const char *secstrings,
154 const char *name,
155 size_t object_size,
156 unsigned int *num)
157{
158 unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
159
160 /* Section 0 has sh_addr 0 and sh_size 0. */
161 *num = sechdrs[sec].sh_size / object_size;
162 return (void *)sechdrs[sec].sh_addr;
163}
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/* Provided by the linker */
166extern const struct kernel_symbol __start___ksymtab[];
167extern const struct kernel_symbol __stop___ksymtab[];
168extern const struct kernel_symbol __start___ksymtab_gpl[];
169extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800170extern const struct kernel_symbol __start___ksymtab_gpl_future[];
171extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700172extern const struct kernel_symbol __start___ksymtab_gpl_future[];
173extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174extern const unsigned long __start___kcrctab[];
175extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800176extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500177#ifdef CONFIG_UNUSED_SYMBOLS
178extern const struct kernel_symbol __start___ksymtab_unused[];
179extern const struct kernel_symbol __stop___ksymtab_unused[];
180extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
181extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700182extern const unsigned long __start___kcrctab_unused[];
183extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500184#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186#ifndef CONFIG_MODVERSIONS
187#define symversion(base, idx) NULL
188#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800189#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190#endif
191
Rusty Russelldafd0942008-07-22 19:24:25 -0500192static bool each_symbol_in_section(const struct symsearch *arr,
193 unsigned int arrsize,
194 struct module *owner,
195 bool (*fn)(const struct symsearch *syms,
196 struct module *owner,
197 unsigned int symnum, void *data),
198 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100199{
Rusty Russelldafd0942008-07-22 19:24:25 -0500200 unsigned int i, j;
201
202 for (j = 0; j < arrsize; j++) {
203 for (i = 0; i < arr[j].stop - arr[j].start; i++)
204 if (fn(&arr[j], owner, i, data))
205 return true;
206 }
207
208 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100209}
210
Rusty Russelldafd0942008-07-22 19:24:25 -0500211/* Returns true as soon as fn returns true, otherwise false. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500212bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
213 unsigned int symnum, void *data), void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700214{
Rusty Russelldafd0942008-07-22 19:24:25 -0500215 struct module *mod;
216 const struct symsearch arr[] = {
217 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
218 NOT_GPL_ONLY, false },
219 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
220 __start___kcrctab_gpl,
221 GPL_ONLY, false },
222 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
223 __start___kcrctab_gpl_future,
224 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500225#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500226 { __start___ksymtab_unused, __stop___ksymtab_unused,
227 __start___kcrctab_unused,
228 NOT_GPL_ONLY, true },
229 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
230 __start___kcrctab_unused_gpl,
231 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500232#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500233 };
234
235 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
236 return true;
237
Andi Kleend72b3752008-08-30 10:09:00 +0200238 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500239 struct symsearch arr[] = {
240 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
241 NOT_GPL_ONLY, false },
242 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
243 mod->gpl_crcs,
244 GPL_ONLY, false },
245 { mod->gpl_future_syms,
246 mod->gpl_future_syms + mod->num_gpl_future_syms,
247 mod->gpl_future_crcs,
248 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500249#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500250 { mod->unused_syms,
251 mod->unused_syms + mod->num_unused_syms,
252 mod->unused_crcs,
253 NOT_GPL_ONLY, true },
254 { mod->unused_gpl_syms,
255 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
256 mod->unused_gpl_crcs,
257 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500258#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500259 };
260
261 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
262 return true;
263 }
264 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700265}
Tim Abbottc6b37802008-12-05 19:03:59 -0500266EXPORT_SYMBOL_GPL(each_symbol);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700267
Rusty Russelldafd0942008-07-22 19:24:25 -0500268struct find_symbol_arg {
269 /* Input */
270 const char *name;
271 bool gplok;
272 bool warn;
273
274 /* Output */
275 struct module *owner;
276 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500277 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500278};
279
280static bool find_symbol_in_section(const struct symsearch *syms,
281 struct module *owner,
282 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500283{
Rusty Russelldafd0942008-07-22 19:24:25 -0500284 struct find_symbol_arg *fsa = data;
285
286 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
287 return false;
288
289 if (!fsa->gplok) {
290 if (syms->licence == GPL_ONLY)
291 return false;
292 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
293 printk(KERN_WARNING "Symbol %s is being used "
294 "by a non-GPL module, which will not "
295 "be allowed in the future\n", fsa->name);
296 printk(KERN_WARNING "Please see the file "
297 "Documentation/feature-removal-schedule.txt "
298 "in the kernel source tree for more details.\n");
299 }
300 }
301
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500302#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500303 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500304 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500305 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500306 printk(KERN_WARNING
307 "This symbol will go away in the future.\n");
308 printk(KERN_WARNING
309 "Please evalute if this is the right api to use and if "
310 "it really is, submit a report the linux kernel "
311 "mailinglist together with submitting your code for "
312 "inclusion.\n");
313 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500314#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500315
316 fsa->owner = owner;
317 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500318 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500319 return true;
320}
321
Tim Abbott414fd312008-12-05 19:03:56 -0500322/* Find a symbol and return it, along with, (optional) crc and
323 * (optional) module which owns it */
Tim Abbottc6b37802008-12-05 19:03:59 -0500324const struct kernel_symbol *find_symbol(const char *name,
325 struct module **owner,
326 const unsigned long **crc,
327 bool gplok,
328 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Rusty Russelldafd0942008-07-22 19:24:25 -0500330 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Rusty Russelldafd0942008-07-22 19:24:25 -0500332 fsa.name = name;
333 fsa.gplok = gplok;
334 fsa.warn = warn;
335
336 if (each_symbol(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500337 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500338 *owner = fsa.owner;
339 if (crc)
340 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500341 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 DEBUGP("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500345 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
Tim Abbottc6b37802008-12-05 19:03:59 -0500347EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349/* Search for module by name: must hold module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500350struct module *find_module(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 struct module *mod;
353
354 list_for_each_entry(mod, &modules, list) {
355 if (strcmp(mod->name, name) == 0)
356 return mod;
357 }
358 return NULL;
359}
Tim Abbottc6b37802008-12-05 19:03:59 -0500360EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900363
364#ifdef CONFIG_HAVE_DYNAMIC_PER_CPU_AREA
365
366static void *percpu_modalloc(unsigned long size, unsigned long align,
367 const char *name)
368{
369 void *ptr;
370
371 if (align > PAGE_SIZE) {
372 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
373 name, align, PAGE_SIZE);
374 align = PAGE_SIZE;
375 }
376
Tejun Heoedcb4632009-03-06 14:33:59 +0900377 ptr = __alloc_reserved_percpu(size, align);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900378 if (!ptr)
379 printk(KERN_WARNING
380 "Could not allocate %lu bytes percpu data\n", size);
381 return ptr;
382}
383
384static void percpu_modfree(void *freeme)
385{
386 free_percpu(freeme);
387}
388
389#else /* ... !CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391/* Number of blocks used and allocated. */
392static unsigned int pcpu_num_used, pcpu_num_allocated;
393/* Size of each block. -ve means used. */
394static int *pcpu_size;
395
396static int split_block(unsigned int i, unsigned short size)
397{
398 /* Reallocation required? */
399 if (pcpu_num_used + 1 > pcpu_num_allocated) {
Pekka Enberg6d4f9c52007-05-08 00:24:58 -0700400 int *new;
401
402 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
403 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (!new)
405 return 0;
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 pcpu_num_allocated *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 pcpu_size = new;
409 }
410
411 /* Insert a new subblock */
412 memmove(&pcpu_size[i+1], &pcpu_size[i],
413 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
414 pcpu_num_used++;
415
416 pcpu_size[i+1] -= size;
417 pcpu_size[i] = size;
418 return 1;
419}
420
421static inline unsigned int block_size(int val)
422{
423 if (val < 0)
424 return -val;
425 return val;
426}
427
Rusty Russell842bbaa2005-08-01 21:11:47 -0700428static void *percpu_modalloc(unsigned long size, unsigned long align,
429 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 unsigned long extra;
432 unsigned int i;
433 void *ptr;
Catalin Marinas4f2294b2009-06-11 13:23:20 +0100434 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Jeremy Fitzhardingeb6e35902007-05-02 19:27:12 +0200436 if (align > PAGE_SIZE) {
437 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
438 name, align, PAGE_SIZE);
439 align = PAGE_SIZE;
Rusty Russell842bbaa2005-08-01 21:11:47 -0700440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 ptr = __per_cpu_start;
443 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
444 /* Extra for alignment requirement. */
445 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
446 BUG_ON(i == 0 && extra != 0);
447
448 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
449 continue;
450
451 /* Transfer extra to previous block. */
452 if (pcpu_size[i-1] < 0)
453 pcpu_size[i-1] -= extra;
454 else
455 pcpu_size[i-1] += extra;
456 pcpu_size[i] -= extra;
457 ptr += extra;
458
459 /* Split block if warranted */
460 if (pcpu_size[i] - size > sizeof(unsigned long))
461 if (!split_block(i, size))
462 return NULL;
463
Catalin Marinas4f2294b2009-06-11 13:23:20 +0100464 /* add the per-cpu scanning areas */
465 for_each_possible_cpu(cpu)
466 kmemleak_alloc(ptr + per_cpu_offset(cpu), size, 0,
467 GFP_KERNEL);
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /* Mark allocated */
470 pcpu_size[i] = -pcpu_size[i];
471 return ptr;
472 }
473
474 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
475 size);
476 return NULL;
477}
478
479static void percpu_modfree(void *freeme)
480{
481 unsigned int i;
482 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
Catalin Marinas4f2294b2009-06-11 13:23:20 +0100483 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 /* First entry is core kernel percpu data. */
486 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
487 if (ptr == freeme) {
488 pcpu_size[i] = -pcpu_size[i];
489 goto free;
490 }
491 }
492 BUG();
493
494 free:
Catalin Marinas4f2294b2009-06-11 13:23:20 +0100495 /* remove the per-cpu scanning areas */
496 for_each_possible_cpu(cpu)
497 kmemleak_free(freeme + per_cpu_offset(cpu));
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /* Merge with previous? */
500 if (pcpu_size[i-1] >= 0) {
501 pcpu_size[i-1] += pcpu_size[i];
502 pcpu_num_used--;
503 memmove(&pcpu_size[i], &pcpu_size[i+1],
504 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
505 i--;
506 }
507 /* Merge with next? */
508 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
509 pcpu_size[i] += pcpu_size[i+1];
510 pcpu_num_used--;
511 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
512 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
513 }
514}
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516static int percpu_modinit(void)
517{
518 pcpu_num_used = 2;
519 pcpu_num_allocated = 2;
520 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
521 GFP_KERNEL);
522 /* Static in-kernel percpu data (used). */
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +0200523 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 /* Free room. */
525 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
526 if (pcpu_size[1] < 0) {
527 printk(KERN_ERR "No per-cpu room for modules.\n");
528 pcpu_num_used = 1;
529 }
530
531 return 0;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700532}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533__initcall(percpu_modinit);
Tejun Heo6b588c12009-02-20 16:29:07 +0900534
Tejun Heofbf59bc2009-02-20 16:29:08 +0900535#endif /* CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
536
Tejun Heo6b588c12009-02-20 16:29:07 +0900537static unsigned int find_pcpusec(Elf_Ehdr *hdr,
538 Elf_Shdr *sechdrs,
539 const char *secstrings)
540{
541 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
542}
543
544static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
545{
546 int cpu;
547
548 for_each_possible_cpu(cpu)
549 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
550}
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900553
Rusty Russell842bbaa2005-08-01 21:11:47 -0700554static inline void *percpu_modalloc(unsigned long size, unsigned long align,
555 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 return NULL;
558}
559static inline void percpu_modfree(void *pcpuptr)
560{
561 BUG();
562}
563static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
564 Elf_Shdr *sechdrs,
565 const char *secstrings)
566{
567 return 0;
568}
569static inline void percpu_modcopy(void *pcpudst, const void *src,
570 unsigned long size)
571{
572 /* pcpusec should be 0, and size of that section should be 0. */
573 BUG_ON(size != 0);
574}
Tejun Heo6b588c12009-02-20 16:29:07 +0900575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576#endif /* CONFIG_SMP */
577
Matt Domschc988d2b2005-06-23 22:05:15 -0700578#define MODINFO_ATTR(field) \
579static void setup_modinfo_##field(struct module *mod, const char *s) \
580{ \
581 mod->field = kstrdup(s, GFP_KERNEL); \
582} \
583static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
584 struct module *mod, char *buffer) \
585{ \
586 return sprintf(buffer, "%s\n", mod->field); \
587} \
588static int modinfo_##field##_exists(struct module *mod) \
589{ \
590 return mod->field != NULL; \
591} \
592static void free_modinfo_##field(struct module *mod) \
593{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700594 kfree(mod->field); \
595 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700596} \
597static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900598 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700599 .show = show_modinfo_##field, \
600 .setup = setup_modinfo_##field, \
601 .test = modinfo_##field##_exists, \
602 .free = free_modinfo_##field, \
603};
604
605MODINFO_ATTR(version);
606MODINFO_ATTR(srcversion);
607
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100608static char last_unloaded_module[MODULE_NAME_LEN+1];
609
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800610#ifdef CONFIG_MODULE_UNLOAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611/* Init the unload section of the module. */
612static void module_unload_init(struct module *mod)
613{
Eric Dumazet720eba32009-02-03 13:31:36 +1030614 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 INIT_LIST_HEAD(&mod->modules_which_use_me);
Eric Dumazet720eba32009-02-03 13:31:36 +1030617 for_each_possible_cpu(cpu)
618 local_set(__module_ref_addr(mod, cpu), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 /* Hold reference count during initialization. */
Eric Dumazet720eba32009-02-03 13:31:36 +1030620 local_set(__module_ref_addr(mod, raw_smp_processor_id()), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 /* Backwards compatibility macros put refcount during init. */
622 mod->waiter = current;
623}
624
625/* modules using other modules */
626struct module_use
627{
628 struct list_head list;
629 struct module *module_which_uses;
630};
631
632/* Does a already use b? */
633static int already_uses(struct module *a, struct module *b)
634{
635 struct module_use *use;
636
637 list_for_each_entry(use, &b->modules_which_use_me, list) {
638 if (use->module_which_uses == a) {
639 DEBUGP("%s uses %s!\n", a->name, b->name);
640 return 1;
641 }
642 }
643 DEBUGP("%s does not use %s!\n", a->name, b->name);
644 return 0;
645}
646
647/* Module a uses b */
Tim Abbottc6b37802008-12-05 19:03:59 -0500648int use_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
650 struct module_use *use;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500651 int no_warn, err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (b == NULL || already_uses(a, b)) return 1;
654
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500655 /* If we're interrupted or time out, we fail. */
656 if (wait_event_interruptible_timeout(
657 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
658 30 * HZ) <= 0) {
659 printk("%s: gave up waiting for init of module %s.\n",
660 a->name, b->name);
661 return 0;
662 }
663
664 /* If strong_try_module_get() returned a different error, we fail. */
665 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return 0;
667
668 DEBUGP("Allocating new usage for %s.\n", a->name);
669 use = kmalloc(sizeof(*use), GFP_ATOMIC);
670 if (!use) {
671 printk("%s: out of memory loading\n", a->name);
672 module_put(b);
673 return 0;
674 }
675
676 use->module_which_uses = a;
677 list_add(&use->list, &b->modules_which_use_me);
Kay Sievers270a6c42007-01-18 13:26:15 +0100678 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return 1;
680}
Tim Abbottc6b37802008-12-05 19:03:59 -0500681EXPORT_SYMBOL_GPL(use_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683/* Clear the unload stuff of the module. */
684static void module_unload_free(struct module *mod)
685{
686 struct module *i;
687
688 list_for_each_entry(i, &modules, list) {
689 struct module_use *use;
690
691 list_for_each_entry(use, &i->modules_which_use_me, list) {
692 if (use->module_which_uses == mod) {
693 DEBUGP("%s unusing %s\n", mod->name, i->name);
694 module_put(i);
695 list_del(&use->list);
696 kfree(use);
Kay Sievers270a6c42007-01-18 13:26:15 +0100697 sysfs_remove_link(i->holders_dir, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 /* There can be at most one match. */
699 break;
700 }
701 }
702 }
703}
704
705#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800706static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
708 int ret = (flags & O_TRUNC);
709 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800710 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return ret;
712}
713#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800714static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 return 0;
717}
718#endif /* CONFIG_MODULE_FORCE_UNLOAD */
719
720struct stopref
721{
722 struct module *mod;
723 int flags;
724 int *forced;
725};
726
727/* Whole machine is stopped with interrupts off when this runs. */
728static int __try_stop_module(void *_sref)
729{
730 struct stopref *sref = _sref;
731
Rusty Russellda39ba52008-07-22 19:24:25 -0500732 /* If it's not unused, quit unless we're forcing. */
733 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800734 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return -EWOULDBLOCK;
736 }
737
738 /* Mark it as dying. */
739 sref->mod->state = MODULE_STATE_GOING;
740 return 0;
741}
742
743static int try_stop_module(struct module *mod, int flags, int *forced)
744{
Rusty Russellda39ba52008-07-22 19:24:25 -0500745 if (flags & O_NONBLOCK) {
746 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500748 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500749 } else {
750 /* We don't need to stop the machine for this. */
751 mod->state = MODULE_STATE_GOING;
752 synchronize_sched();
753 return 0;
754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
757unsigned int module_refcount(struct module *mod)
758{
Eric Dumazet720eba32009-02-03 13:31:36 +1030759 unsigned int total = 0;
760 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Eric Dumazet720eba32009-02-03 13:31:36 +1030762 for_each_possible_cpu(cpu)
763 total += local_read(__module_ref_addr(mod, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return total;
765}
766EXPORT_SYMBOL(module_refcount);
767
768/* This exists whether we can unload or not */
769static void free_module(struct module *mod);
770
771static void wait_for_zero_refcount(struct module *mod)
772{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500773 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800774 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 for (;;) {
776 DEBUGP("Looking at refcount...\n");
777 set_current_state(TASK_UNINTERRUPTIBLE);
778 if (module_refcount(mod) == 0)
779 break;
780 schedule();
781 }
782 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800783 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100786SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
787 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
789 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800790 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 int ret, forced = 0;
792
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800793 if (!capable(CAP_SYS_MODULE))
794 return -EPERM;
795
796 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
797 return -EFAULT;
798 name[MODULE_NAME_LEN-1] = '\0';
799
Heiko Carstens9e018922008-12-22 12:36:31 +0100800 /* Create stop_machine threads since free_module relies on
801 * a non-failing stop_machine call. */
802 ret = stop_machine_create();
803 if (ret)
804 return ret;
805
806 if (mutex_lock_interruptible(&module_mutex) != 0) {
807 ret = -EINTR;
808 goto out_stop;
809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 mod = find_module(name);
812 if (!mod) {
813 ret = -ENOENT;
814 goto out;
815 }
816
817 if (!list_empty(&mod->modules_which_use_me)) {
818 /* Other modules depend on us: get rid of them first. */
819 ret = -EWOULDBLOCK;
820 goto out;
821 }
822
823 /* Doing init or already dying? */
824 if (mod->state != MODULE_STATE_LIVE) {
825 /* FIXME: if (force), slam module count and wake up
826 waiter --RR */
827 DEBUGP("%s already dying\n", mod->name);
828 ret = -EBUSY;
829 goto out;
830 }
831
832 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700833 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800834 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (!forced) {
836 /* This module can't be removed */
837 ret = -EBUSY;
838 goto out;
839 }
840 }
841
842 /* Set this up before setting mod->state */
843 mod->waiter = current;
844
845 /* Stop the machine so refcounts can't move and disable module. */
846 ret = try_stop_module(mod, flags, &forced);
847 if (ret != 0)
848 goto out;
849
850 /* Never wait if forced. */
851 if (!forced && module_refcount(mod) != 0)
852 wait_for_zero_refcount(mod);
853
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200854 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 /* Final destruction now noone is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200856 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200858 blocking_notifier_call_chain(&module_notify_list,
859 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800860 async_synchronize_full();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200861 mutex_lock(&module_mutex);
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100862 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500863 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Jason Barone9d376f2009-02-05 11:51:38 -0500864 ddebug_remove_module(mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 free_module(mod);
866
867 out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800868 mutex_unlock(&module_mutex);
Heiko Carstens9e018922008-12-22 12:36:31 +0100869out_stop:
870 stop_machine_destroy();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return ret;
872}
873
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800874static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
876 struct module_use *use;
877 int printed_something = 0;
878
879 seq_printf(m, " %u ", module_refcount(mod));
880
881 /* Always include a trailing , so userspace can differentiate
882 between this and the old multi-field proc format. */
883 list_for_each_entry(use, &mod->modules_which_use_me, list) {
884 printed_something = 1;
885 seq_printf(m, "%s,", use->module_which_uses->name);
886 }
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (mod->init != NULL && mod->exit == NULL) {
889 printed_something = 1;
890 seq_printf(m, "[permanent],");
891 }
892
893 if (!printed_something)
894 seq_printf(m, "-");
895}
896
897void __symbol_put(const char *symbol)
898{
899 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Rusty Russell24da1cb2007-07-15 23:41:46 -0700901 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -0500902 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 BUG();
904 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700905 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907EXPORT_SYMBOL(__symbol_put);
908
909void symbol_put_addr(void *addr)
910{
Trent Piepho5e376612006-05-15 09:44:06 -0700911 struct module *modaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Trent Piepho5e376612006-05-15 09:44:06 -0700913 if (core_kernel_text((unsigned long)addr))
914 return;
915
Rusty Russella6e6abd2009-03-31 13:05:31 -0600916 /* module_text_address is safe here: we're supposed to have reference
917 * to module from symbol_get, so it can't go away. */
918 modaddr = __module_text_address((unsigned long)addr);
919 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -0700920 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922EXPORT_SYMBOL_GPL(symbol_put_addr);
923
924static ssize_t show_refcnt(struct module_attribute *mattr,
925 struct module *mod, char *buffer)
926{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400927 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
930static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900931 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 .show = show_refcnt,
933};
934
Al Virof6a57032006-10-18 01:47:25 -0400935void module_put(struct module *module)
936{
937 if (module) {
938 unsigned int cpu = get_cpu();
Eric Dumazet720eba32009-02-03 13:31:36 +1030939 local_dec(__module_ref_addr(module, cpu));
Al Virof6a57032006-10-18 01:47:25 -0400940 /* Maybe they're waiting for us to drop reference? */
941 if (unlikely(!module_is_live(module)))
942 wake_up_process(module->waiter);
943 put_cpu();
944 }
945}
946EXPORT_SYMBOL(module_put);
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800949static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
951 /* We don't know the usage count, or what modules are using. */
952 seq_printf(m, " - -");
953}
954
955static inline void module_unload_free(struct module *mod)
956{
957}
958
Tim Abbottc6b37802008-12-05 19:03:59 -0500959int use_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500961 return strong_try_module_get(b) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
Tim Abbottc6b37802008-12-05 19:03:59 -0500963EXPORT_SYMBOL_GPL(use_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965static inline void module_unload_init(struct module *mod)
966{
967}
968#endif /* CONFIG_MODULE_UNLOAD */
969
Kay Sievers1f717402006-11-24 12:15:25 +0100970static ssize_t show_initstate(struct module_attribute *mattr,
971 struct module *mod, char *buffer)
972{
973 const char *state = "unknown";
974
975 switch (mod->state) {
976 case MODULE_STATE_LIVE:
977 state = "live";
978 break;
979 case MODULE_STATE_COMING:
980 state = "coming";
981 break;
982 case MODULE_STATE_GOING:
983 state = "going";
984 break;
985 }
986 return sprintf(buffer, "%s\n", state);
987}
988
989static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900990 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100991 .show = show_initstate,
992};
993
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800994static struct module_attribute *modinfo_attrs[] = {
995 &modinfo_version,
996 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100997 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800998#ifdef CONFIG_MODULE_UNLOAD
999 &refcnt,
1000#endif
1001 NULL,
1002};
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004static const char vermagic[] = VERMAGIC_STRING;
1005
Rusty Russellc6e665c2009-03-31 13:05:33 -06001006static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -07001007{
1008#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -07001009 if (!test_taint(TAINT_FORCED_MODULE))
Rusty Russellc6e665c2009-03-31 13:05:33 -06001010 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
1011 mod->name, reason);
Linus Torvalds826e4502008-05-04 17:04:16 -07001012 add_taint_module(mod, TAINT_FORCED_MODULE);
1013 return 0;
1014#else
1015 return -ENOEXEC;
1016#endif
1017}
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019#ifdef CONFIG_MODVERSIONS
1020static int check_version(Elf_Shdr *sechdrs,
1021 unsigned int versindex,
1022 const char *symname,
1023 struct module *mod,
1024 const unsigned long *crc)
1025{
1026 unsigned int i, num_versions;
1027 struct modversion_info *versions;
1028
1029 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1030 if (!crc)
1031 return 1;
1032
Rusty Russella5dd6972008-05-09 16:24:21 +10001033 /* No versions at all? modprobe --force does this. */
1034 if (versindex == 0)
1035 return try_to_force_load(mod, symname) == 0;
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 versions = (void *) sechdrs[versindex].sh_addr;
1038 num_versions = sechdrs[versindex].sh_size
1039 / sizeof(struct modversion_info);
1040
1041 for (i = 0; i < num_versions; i++) {
1042 if (strcmp(versions[i].name, symname) != 0)
1043 continue;
1044
1045 if (versions[i].crc == *crc)
1046 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 DEBUGP("Found checksum %lX vs module %lX\n",
1048 *crc, versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001049 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001051
Rusty Russella5dd6972008-05-09 16:24:21 +10001052 printk(KERN_WARNING "%s: no symbol version for %s\n",
1053 mod->name, symname);
1054 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001055
1056bad_version:
1057 printk("%s: disagrees about version of symbol %s\n",
1058 mod->name, symname);
1059 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
1062static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1063 unsigned int versindex,
1064 struct module *mod)
1065{
1066 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Rusty Russell8c8ef422009-03-31 13:05:34 -06001068 if (!find_symbol("module_layout", NULL, &crc, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 BUG();
Rusty Russell8c8ef422009-03-31 13:05:34 -06001070 return check_version(sechdrs, versindex, "module_layout", mod, crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
Rusty Russell91e37a72008-05-09 16:25:28 +10001073/* First part is kernel version, which we ignore if module has crcs. */
1074static inline int same_magic(const char *amagic, const char *bmagic,
1075 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Rusty Russell91e37a72008-05-09 16:25:28 +10001077 if (has_crcs) {
1078 amagic += strcspn(amagic, " ");
1079 bmagic += strcspn(bmagic, " ");
1080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return strcmp(amagic, bmagic) == 0;
1082}
1083#else
1084static inline int check_version(Elf_Shdr *sechdrs,
1085 unsigned int versindex,
1086 const char *symname,
1087 struct module *mod,
1088 const unsigned long *crc)
1089{
1090 return 1;
1091}
1092
1093static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1094 unsigned int versindex,
1095 struct module *mod)
1096{
1097 return 1;
1098}
1099
Rusty Russell91e37a72008-05-09 16:25:28 +10001100static inline int same_magic(const char *amagic, const char *bmagic,
1101 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
1103 return strcmp(amagic, bmagic) == 0;
1104}
1105#endif /* CONFIG_MODVERSIONS */
1106
1107/* Resolve a symbol for this module. I.e. if we find one, record usage.
1108 Must be holding module_mutex. */
Tim Abbott414fd312008-12-05 19:03:56 -05001109static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
1110 unsigned int versindex,
1111 const char *name,
1112 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
1114 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001115 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 const unsigned long *crc;
1117
Tim Abbott414fd312008-12-05 19:03:56 -05001118 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001119 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Tim Abbott414fd312008-12-05 19:03:56 -05001120 /* use_module can fail due to OOM,
1121 or module initialization or unloading */
1122 if (sym) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1124 !use_module(mod, owner))
Tim Abbott414fd312008-12-05 19:03:56 -05001125 sym = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
Tim Abbott414fd312008-12-05 19:03:56 -05001127 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130/*
1131 * /sys/module/foo/sections stuff
1132 * J. Corbet <corbet@lwn.net>
1133 */
Kay Sievers120fc3d2008-02-21 00:33:20 +01001134#if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
Rusty Russella58730c2008-03-13 09:03:44 +00001135struct module_sect_attr
1136{
1137 struct module_attribute mattr;
1138 char *name;
1139 unsigned long address;
1140};
1141
1142struct module_sect_attrs
1143{
1144 struct attribute_group grp;
1145 unsigned int nsections;
1146 struct module_sect_attr attrs[0];
1147};
1148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149static ssize_t module_sect_show(struct module_attribute *mattr,
1150 struct module *mod, char *buf)
1151{
1152 struct module_sect_attr *sattr =
1153 container_of(mattr, struct module_sect_attr, mattr);
1154 return sprintf(buf, "0x%lx\n", sattr->address);
1155}
1156
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001157static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1158{
Rusty Russella58730c2008-03-13 09:03:44 +00001159 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001160
1161 for (section = 0; section < sect_attrs->nsections; section++)
1162 kfree(sect_attrs->attrs[section].name);
1163 kfree(sect_attrs);
1164}
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166static void add_sect_attrs(struct module *mod, unsigned int nsect,
1167 char *secstrings, Elf_Shdr *sechdrs)
1168{
1169 unsigned int nloaded = 0, i, size[2];
1170 struct module_sect_attrs *sect_attrs;
1171 struct module_sect_attr *sattr;
1172 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 /* Count loaded sections and allocate structures */
1175 for (i = 0; i < nsect; i++)
1176 if (sechdrs[i].sh_flags & SHF_ALLOC)
1177 nloaded++;
1178 size[0] = ALIGN(sizeof(*sect_attrs)
1179 + nloaded * sizeof(sect_attrs->attrs[0]),
1180 sizeof(sect_attrs->grp.attrs[0]));
1181 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001182 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1183 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 return;
1185
1186 /* Setup section attributes. */
1187 sect_attrs->grp.name = "sections";
1188 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1189
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001190 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 sattr = &sect_attrs->attrs[0];
1192 gattr = &sect_attrs->grp.attrs[0];
1193 for (i = 0; i < nsect; i++) {
1194 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1195 continue;
1196 sattr->address = sechdrs[i].sh_addr;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001197 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1198 GFP_KERNEL);
1199 if (sattr->name == NULL)
1200 goto out;
1201 sect_attrs->nsections++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 sattr->mattr.show = module_sect_show;
1203 sattr->mattr.store = NULL;
1204 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 sattr->mattr.attr.mode = S_IRUGO;
1206 *(gattr++) = &(sattr++)->mattr.attr;
1207 }
1208 *gattr = NULL;
1209
1210 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1211 goto out;
1212
1213 mod->sect_attrs = sect_attrs;
1214 return;
1215 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001216 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217}
1218
1219static void remove_sect_attrs(struct module *mod)
1220{
1221 if (mod->sect_attrs) {
1222 sysfs_remove_group(&mod->mkobj.kobj,
1223 &mod->sect_attrs->grp);
1224 /* We are positive that no one is using any sect attrs
1225 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001226 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 mod->sect_attrs = NULL;
1228 }
1229}
1230
Roland McGrath6d760132007-10-16 23:26:40 -07001231/*
1232 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1233 */
1234
1235struct module_notes_attrs {
1236 struct kobject *dir;
1237 unsigned int notes;
1238 struct bin_attribute attrs[0];
1239};
1240
1241static ssize_t module_notes_read(struct kobject *kobj,
1242 struct bin_attribute *bin_attr,
1243 char *buf, loff_t pos, size_t count)
1244{
1245 /*
1246 * The caller checked the pos and count against our size.
1247 */
1248 memcpy(buf, bin_attr->private + pos, count);
1249 return count;
1250}
1251
1252static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1253 unsigned int i)
1254{
1255 if (notes_attrs->dir) {
1256 while (i-- > 0)
1257 sysfs_remove_bin_file(notes_attrs->dir,
1258 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001259 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001260 }
1261 kfree(notes_attrs);
1262}
1263
1264static void add_notes_attrs(struct module *mod, unsigned int nsect,
1265 char *secstrings, Elf_Shdr *sechdrs)
1266{
1267 unsigned int notes, loaded, i;
1268 struct module_notes_attrs *notes_attrs;
1269 struct bin_attribute *nattr;
1270
1271 /* Count notes sections and allocate structures. */
1272 notes = 0;
1273 for (i = 0; i < nsect; i++)
1274 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1275 (sechdrs[i].sh_type == SHT_NOTE))
1276 ++notes;
1277
1278 if (notes == 0)
1279 return;
1280
1281 notes_attrs = kzalloc(sizeof(*notes_attrs)
1282 + notes * sizeof(notes_attrs->attrs[0]),
1283 GFP_KERNEL);
1284 if (notes_attrs == NULL)
1285 return;
1286
1287 notes_attrs->notes = notes;
1288 nattr = &notes_attrs->attrs[0];
1289 for (loaded = i = 0; i < nsect; ++i) {
1290 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1291 continue;
1292 if (sechdrs[i].sh_type == SHT_NOTE) {
1293 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1294 nattr->attr.mode = S_IRUGO;
1295 nattr->size = sechdrs[i].sh_size;
1296 nattr->private = (void *) sechdrs[i].sh_addr;
1297 nattr->read = module_notes_read;
1298 ++nattr;
1299 }
1300 ++loaded;
1301 }
1302
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001303 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001304 if (!notes_attrs->dir)
1305 goto out;
1306
1307 for (i = 0; i < notes; ++i)
1308 if (sysfs_create_bin_file(notes_attrs->dir,
1309 &notes_attrs->attrs[i]))
1310 goto out;
1311
1312 mod->notes_attrs = notes_attrs;
1313 return;
1314
1315 out:
1316 free_notes_attrs(notes_attrs, i);
1317}
1318
1319static void remove_notes_attrs(struct module *mod)
1320{
1321 if (mod->notes_attrs)
1322 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1323}
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1328 char *sectstrings, Elf_Shdr *sechdrs)
1329{
1330}
1331
1332static inline void remove_sect_attrs(struct module *mod)
1333{
1334}
Roland McGrath6d760132007-10-16 23:26:40 -07001335
1336static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1337 char *sectstrings, Elf_Shdr *sechdrs)
1338{
1339}
1340
1341static inline void remove_notes_attrs(struct module *mod)
1342{
1343}
Kay Sievers120fc3d2008-02-21 00:33:20 +01001344#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Randy Dunlapef665c12007-02-13 15:19:06 -08001346#ifdef CONFIG_SYSFS
1347int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001348{
1349 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001350 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001351 int error = 0;
1352 int i;
1353
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001354 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1355 (ARRAY_SIZE(modinfo_attrs) + 1)),
1356 GFP_KERNEL);
1357 if (!mod->modinfo_attrs)
1358 return -ENOMEM;
1359
1360 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001361 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1362 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001363 (attr->test && attr->test(mod))) {
1364 memcpy(temp_attr, attr, sizeof(*temp_attr));
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001365 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1366 ++temp_attr;
1367 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001368 }
1369 return error;
1370}
1371
Randy Dunlapef665c12007-02-13 15:19:06 -08001372void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001373{
1374 struct module_attribute *attr;
1375 int i;
1376
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001377 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1378 /* pick a field to test for end of list */
1379 if (!attr->attr.name)
1380 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001381 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001382 if (attr->free)
1383 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001384 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001385 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001386}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Randy Dunlapef665c12007-02-13 15:19:06 -08001388int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
1390 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001391 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001393 if (!module_sysfs_initialized) {
1394 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001395 mod->name);
1396 err = -EINVAL;
1397 goto out;
1398 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001399
1400 kobj = kset_find_obj(module_kset, mod->name);
1401 if (kobj) {
1402 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1403 kobject_put(kobj);
1404 err = -EINVAL;
1405 goto out;
1406 }
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001409
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001410 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1411 mod->mkobj.kobj.kset = module_kset;
1412 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1413 "%s", mod->name);
1414 if (err)
1415 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001416
Kay Sievers97c146e2007-11-29 23:46:11 +01001417 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001418out:
1419 return err;
1420}
1421
Randy Dunlapef665c12007-02-13 15:19:06 -08001422int mod_sysfs_setup(struct module *mod,
Kay Sievers270a6c42007-01-18 13:26:15 +01001423 struct kernel_param *kparam,
1424 unsigned int num_params)
1425{
1426 int err;
1427
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001428 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001429 if (!mod->holders_dir) {
1430 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001431 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001432 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 err = module_param_sysfs_setup(mod, kparam, num_params);
1435 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001436 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Matt Domschc988d2b2005-06-23 22:05:15 -07001438 err = module_add_modinfo_attrs(mod);
1439 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001440 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001441
Kay Sieverse17e0f52006-11-24 12:15:25 +01001442 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 return 0;
1444
Kay Sieverse17e0f52006-11-24 12:15:25 +01001445out_unreg_param:
1446 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001447out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001448 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001449out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001450 kobject_put(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 return err;
1452}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001453
1454static void mod_sysfs_fini(struct module *mod)
1455{
1456 kobject_put(&mod->mkobj.kobj);
1457}
1458
1459#else /* CONFIG_SYSFS */
1460
1461static void mod_sysfs_fini(struct module *mod)
1462{
1463}
1464
1465#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467static void mod_kobject_remove(struct module *mod)
1468{
Matt Domschc988d2b2005-06-23 22:05:15 -07001469 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001471 kobject_put(mod->mkobj.drivers_dir);
1472 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001473 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
1475
1476/*
1477 * unlink the module with the whole machine is stopped with interrupts off
1478 * - this defends against kallsyms not taking locks
1479 */
1480static int __unlink_module(void *_mod)
1481{
1482 struct module *mod = _mod;
1483 list_del(&mod->list);
1484 return 0;
1485}
1486
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001487/* Free a module, remove from lists, etc (must hold module_mutex). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488static void free_module(struct module *mod)
1489{
1490 /* Delete from various lists */
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001491 stop_machine(__unlink_module, mod, NULL);
Roland McGrath6d760132007-10-16 23:26:40 -07001492 remove_notes_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 remove_sect_attrs(mod);
1494 mod_kobject_remove(mod);
1495
1496 /* Arch-specific cleanup. */
1497 module_arch_cleanup(mod);
1498
1499 /* Module unload stuff */
1500 module_unload_free(mod);
1501
Rusty Russelle180a6b2009-03-31 13:05:29 -06001502 /* Free any allocated parameters. */
1503 destroy_params(mod->kp, mod->num_kp);
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 /* This may be NULL, but that's OK */
1506 module_free(mod, mod->module_init);
1507 kfree(mod->args);
1508 if (mod->percpu)
1509 percpu_modfree(mod->percpu);
Eric Dumazet720eba32009-02-03 13:31:36 +10301510#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
1511 if (mod->refptr)
1512 percpu_modfree(mod->refptr);
1513#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001514 /* Free lock-classes: */
1515 lockdep_free_key_range(mod->module_core, mod->core_size);
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 /* Finally, free the core (containing the module structure) */
1518 module_free(mod, mod->module_core);
1519}
1520
1521void *__symbol_get(const char *symbol)
1522{
1523 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001524 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Rusty Russell24da1cb2007-07-15 23:41:46 -07001526 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001527 sym = find_symbol(symbol, &owner, NULL, true, true);
1528 if (sym && strong_try_module_get(owner))
1529 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001530 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Tim Abbott414fd312008-12-05 19:03:56 -05001532 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
1534EXPORT_SYMBOL_GPL(__symbol_get);
1535
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001536/*
1537 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001538 * in the kernel or in some other module's exported symbol table.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001539 */
1540static int verify_export_symbols(struct module *mod)
1541{
Rusty Russellb2111042008-05-01 21:15:00 -05001542 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001543 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001544 const struct kernel_symbol *s;
1545 struct {
1546 const struct kernel_symbol *sym;
1547 unsigned int num;
1548 } arr[] = {
1549 { mod->syms, mod->num_syms },
1550 { mod->gpl_syms, mod->num_gpl_syms },
1551 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001552#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001553 { mod->unused_syms, mod->num_unused_syms },
1554 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001555#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001556 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001557
Rusty Russellb2111042008-05-01 21:15:00 -05001558 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1559 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Tim Abbott414fd312008-12-05 19:03:56 -05001560 if (find_symbol(s->name, &owner, NULL, true, false)) {
Rusty Russellb2111042008-05-01 21:15:00 -05001561 printk(KERN_ERR
1562 "%s: exports duplicate symbol %s"
1563 " (owned by %s)\n",
1564 mod->name, s->name, module_name(owner));
1565 return -ENOEXEC;
1566 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001567 }
Rusty Russellb2111042008-05-01 21:15:00 -05001568 }
1569 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001570}
1571
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001572/* Change all symbols so that st_value encodes the pointer directly. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573static int simplify_symbols(Elf_Shdr *sechdrs,
1574 unsigned int symindex,
1575 const char *strtab,
1576 unsigned int versindex,
1577 unsigned int pcpuindex,
1578 struct module *mod)
1579{
1580 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1581 unsigned long secbase;
1582 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1583 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05001584 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 for (i = 1; i < n; i++) {
1587 switch (sym[i].st_shndx) {
1588 case SHN_COMMON:
1589 /* We compiled with -fno-common. These are not
1590 supposed to happen. */
1591 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1592 printk("%s: please compile with -fno-common\n",
1593 mod->name);
1594 ret = -ENOEXEC;
1595 break;
1596
1597 case SHN_ABS:
1598 /* Don't need to do anything */
1599 DEBUGP("Absolute symbol: 0x%08lx\n",
1600 (long)sym[i].st_value);
1601 break;
1602
1603 case SHN_UNDEF:
Tim Abbott414fd312008-12-05 19:03:56 -05001604 ksym = resolve_symbol(sechdrs, versindex,
1605 strtab + sym[i].st_name, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 /* Ok if resolved. */
Tim Abbott414fd312008-12-05 19:03:56 -05001607 if (ksym) {
1608 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 break;
Tim Abbott414fd312008-12-05 19:03:56 -05001610 }
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 /* Ok if weak. */
1613 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1614 break;
1615
1616 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1617 mod->name, strtab + sym[i].st_name);
1618 ret = -ENOENT;
1619 break;
1620
1621 default:
1622 /* Divert to percpu allocation if a percpu var. */
1623 if (sym[i].st_shndx == pcpuindex)
1624 secbase = (unsigned long)mod->percpu;
1625 else
1626 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1627 sym[i].st_value += secbase;
1628 break;
1629 }
1630 }
1631
1632 return ret;
1633}
1634
Helge Deller088af9a2008-12-31 12:31:18 +01001635/* Additional bytes needed by arch in front of individual sections */
1636unsigned int __weak arch_mod_section_prepend(struct module *mod,
1637 unsigned int section)
1638{
1639 /* default implementation just returns zero */
1640 return 0;
1641}
1642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01001644static long get_offset(struct module *mod, unsigned int *size,
1645 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
1647 long ret;
1648
Helge Deller088af9a2008-12-31 12:31:18 +01001649 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1651 *size = ret + sechdr->sh_size;
1652 return ret;
1653}
1654
1655/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1656 might -- code, read-only data, read-write data, small data. Tally
1657 sizes, and place the offsets into sh_entsize fields: high bit means it
1658 belongs in init. */
1659static void layout_sections(struct module *mod,
1660 const Elf_Ehdr *hdr,
1661 Elf_Shdr *sechdrs,
1662 const char *secstrings)
1663{
1664 static unsigned long const masks[][2] = {
1665 /* NOTE: all executable code must be the first section
1666 * in this array; otherwise modify the text_size
1667 * finder in the two loops below */
1668 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1669 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1670 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1671 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1672 };
1673 unsigned int m, i;
1674
1675 for (i = 0; i < hdr->e_shnum; i++)
1676 sechdrs[i].sh_entsize = ~0UL;
1677
1678 DEBUGP("Core section allocation order:\n");
1679 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1680 for (i = 0; i < hdr->e_shnum; ++i) {
1681 Elf_Shdr *s = &sechdrs[i];
1682
1683 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1684 || (s->sh_flags & masks[m][1])
1685 || s->sh_entsize != ~0UL
Rusty Russell49502672009-03-31 13:05:36 -06001686 || strstarts(secstrings + s->sh_name, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001688 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 DEBUGP("\t%s\n", secstrings + s->sh_name);
1690 }
1691 if (m == 0)
1692 mod->core_text_size = mod->core_size;
1693 }
1694
1695 DEBUGP("Init section allocation order:\n");
1696 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1697 for (i = 0; i < hdr->e_shnum; ++i) {
1698 Elf_Shdr *s = &sechdrs[i];
1699
1700 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1701 || (s->sh_flags & masks[m][1])
1702 || s->sh_entsize != ~0UL
Rusty Russell49502672009-03-31 13:05:36 -06001703 || !strstarts(secstrings + s->sh_name, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001705 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 | INIT_OFFSET_MASK);
1707 DEBUGP("\t%s\n", secstrings + s->sh_name);
1708 }
1709 if (m == 0)
1710 mod->init_text_size = mod->init_size;
1711 }
1712}
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714static void set_license(struct module *mod, const char *license)
1715{
1716 if (!license)
1717 license = "unspecified";
1718
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001719 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07001720 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001721 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001722 "kernel.\n", mod->name, license);
1723 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 }
1725}
1726
1727/* Parse tag=value strings from .modinfo section */
1728static char *next_string(char *string, unsigned long *secsize)
1729{
1730 /* Skip non-zero chars */
1731 while (string[0]) {
1732 string++;
1733 if ((*secsize)-- <= 1)
1734 return NULL;
1735 }
1736
1737 /* Skip any zero padding. */
1738 while (!string[0]) {
1739 string++;
1740 if ((*secsize)-- <= 1)
1741 return NULL;
1742 }
1743 return string;
1744}
1745
1746static char *get_modinfo(Elf_Shdr *sechdrs,
1747 unsigned int info,
1748 const char *tag)
1749{
1750 char *p;
1751 unsigned int taglen = strlen(tag);
1752 unsigned long size = sechdrs[info].sh_size;
1753
1754 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1755 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1756 return p + taglen + 1;
1757 }
1758 return NULL;
1759}
1760
Matt Domschc988d2b2005-06-23 22:05:15 -07001761static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1762 unsigned int infoindex)
1763{
1764 struct module_attribute *attr;
1765 int i;
1766
1767 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1768 if (attr->setup)
1769 attr->setup(mod,
1770 get_modinfo(sechdrs,
1771 infoindex,
1772 attr->attr.name));
1773 }
1774}
Matt Domschc988d2b2005-06-23 22:05:15 -07001775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01001777
1778/* lookup symbol in given range of kernel_symbols */
1779static const struct kernel_symbol *lookup_symbol(const char *name,
1780 const struct kernel_symbol *start,
1781 const struct kernel_symbol *stop)
1782{
1783 const struct kernel_symbol *ks = start;
1784 for (; ks < stop; ks++)
1785 if (strcmp(ks->name, name) == 0)
1786 return ks;
1787 return NULL;
1788}
1789
Tim Abbottca4787b2009-01-05 08:40:10 -06001790static int is_exported(const char *name, unsigned long value,
1791 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
Tim Abbottca4787b2009-01-05 08:40:10 -06001793 const struct kernel_symbol *ks;
1794 if (!mod)
1795 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001796 else
Tim Abbottca4787b2009-01-05 08:40:10 -06001797 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
1798 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799}
1800
1801/* As per nm */
1802static char elf_type(const Elf_Sym *sym,
1803 Elf_Shdr *sechdrs,
1804 const char *secstrings,
1805 struct module *mod)
1806{
1807 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1808 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1809 return 'v';
1810 else
1811 return 'w';
1812 }
1813 if (sym->st_shndx == SHN_UNDEF)
1814 return 'U';
1815 if (sym->st_shndx == SHN_ABS)
1816 return 'a';
1817 if (sym->st_shndx >= SHN_LORESERVE)
1818 return '?';
1819 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1820 return 't';
1821 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1822 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1823 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1824 return 'r';
1825 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1826 return 'g';
1827 else
1828 return 'd';
1829 }
1830 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1831 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1832 return 's';
1833 else
1834 return 'b';
1835 }
Rusty Russell49502672009-03-31 13:05:36 -06001836 if (strstarts(secstrings + sechdrs[sym->st_shndx].sh_name, ".debug"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return 'n';
1838 return '?';
1839}
1840
1841static void add_kallsyms(struct module *mod,
1842 Elf_Shdr *sechdrs,
1843 unsigned int symindex,
1844 unsigned int strindex,
1845 const char *secstrings)
1846{
1847 unsigned int i;
1848
1849 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1850 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1851 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1852
1853 /* Set types up while we still have access to sections. */
1854 for (i = 0; i < mod->num_symtab; i++)
1855 mod->symtab[i].st_info
1856 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1857}
1858#else
1859static inline void add_kallsyms(struct module *mod,
1860 Elf_Shdr *sechdrs,
1861 unsigned int symindex,
1862 unsigned int strindex,
1863 const char *secstrings)
1864{
1865}
1866#endif /* CONFIG_KALLSYMS */
1867
Jason Barone9d376f2009-02-05 11:51:38 -05001868static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05001869{
Jason Barone9d376f2009-02-05 11:51:38 -05001870#ifdef CONFIG_DYNAMIC_DEBUG
1871 if (ddebug_add_module(debug, num, debug->modname))
1872 printk(KERN_ERR "dynamic debug error adding module: %s\n",
1873 debug->modname);
1874#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05001875}
Jason Baron346e15b2008-08-12 16:46:19 -04001876
Rusty Russell3a642e92008-07-22 19:24:28 -05001877static void *module_alloc_update_bounds(unsigned long size)
1878{
1879 void *ret = module_alloc(size);
1880
1881 if (ret) {
1882 /* Update module bounds. */
1883 if ((unsigned long)ret < module_addr_min)
1884 module_addr_min = (unsigned long)ret;
1885 if ((unsigned long)ret + size > module_addr_max)
1886 module_addr_max = (unsigned long)ret + size;
1887 }
1888 return ret;
1889}
1890
Catalin Marinas4f2294b2009-06-11 13:23:20 +01001891#ifdef CONFIG_DEBUG_KMEMLEAK
1892static void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
1893 Elf_Shdr *sechdrs, char *secstrings)
1894{
1895 unsigned int i;
1896
1897 /* only scan the sections containing data */
1898 kmemleak_scan_area(mod->module_core, (unsigned long)mod -
1899 (unsigned long)mod->module_core,
1900 sizeof(struct module), GFP_KERNEL);
1901
1902 for (i = 1; i < hdr->e_shnum; i++) {
1903 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1904 continue;
1905 if (strncmp(secstrings + sechdrs[i].sh_name, ".data", 5) != 0
1906 && strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) != 0)
1907 continue;
1908
1909 kmemleak_scan_area(mod->module_core, sechdrs[i].sh_addr -
1910 (unsigned long)mod->module_core,
1911 sechdrs[i].sh_size, GFP_KERNEL);
1912 }
1913}
1914#else
1915static inline void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
1916 Elf_Shdr *sechdrs, char *secstrings)
1917{
1918}
1919#endif
1920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921/* Allocate and load the module: note that size of section 0 is always
1922 zero, and we rely on this for optional sections. */
Linus Torvaldsffb4ba72008-08-25 11:10:26 -07001923static noinline struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 unsigned long len,
1925 const char __user *uargs)
1926{
1927 Elf_Ehdr *hdr;
1928 Elf_Shdr *sechdrs;
1929 char *secstrings, *args, *modmagic, *strtab = NULL;
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07001930 char *staging;
Andrew Morton84860f92006-06-28 04:26:46 -07001931 unsigned int i;
1932 unsigned int symindex = 0;
1933 unsigned int strindex = 0;
Rusty Russell5e458cc2008-10-22 10:00:13 -05001934 unsigned int modindex, versindex, infoindex, pcpuindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 struct module *mod;
1936 long err = 0;
1937 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
Thomas Koeller378bac82005-09-06 15:17:11 -07001938 mm_segment_t old_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1941 umod, len, uargs);
1942 if (len < sizeof(*hdr))
1943 return ERR_PTR(-ENOEXEC);
1944
1945 /* Suck in entire file: we'll want most of it. */
1946 /* vmalloc barfs on "unusual" numbers. Check here */
1947 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1948 return ERR_PTR(-ENOMEM);
Heiko Carstens9e018922008-12-22 12:36:31 +01001949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 if (copy_from_user(hdr, umod, len) != 0) {
1951 err = -EFAULT;
1952 goto free_hdr;
1953 }
1954
1955 /* Sanity checks against insmoding binaries or wrong arch,
1956 weird elf version */
Cyrill Gorcunovc4ea6fc2008-05-14 16:27:29 -07001957 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 || hdr->e_type != ET_REL
1959 || !elf_check_arch(hdr)
1960 || hdr->e_shentsize != sizeof(*sechdrs)) {
1961 err = -ENOEXEC;
1962 goto free_hdr;
1963 }
1964
1965 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1966 goto truncated;
1967
1968 /* Convenience variables */
1969 sechdrs = (void *)hdr + hdr->e_shoff;
1970 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1971 sechdrs[0].sh_addr = 0;
1972
1973 for (i = 1; i < hdr->e_shnum; i++) {
1974 if (sechdrs[i].sh_type != SHT_NOBITS
1975 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1976 goto truncated;
1977
1978 /* Mark all sections sh_addr with their address in the
1979 temporary image. */
1980 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1981
1982 /* Internal symbols and strings. */
1983 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1984 symindex = i;
1985 strindex = sechdrs[i].sh_link;
1986 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1987 }
1988#ifndef CONFIG_MODULE_UNLOAD
1989 /* Don't load .exit sections */
Rusty Russell49502672009-03-31 13:05:36 -06001990 if (strstarts(secstrings+sechdrs[i].sh_name, ".exit"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1992#endif
1993 }
1994
1995 modindex = find_sec(hdr, sechdrs, secstrings,
1996 ".gnu.linkonce.this_module");
1997 if (!modindex) {
1998 printk(KERN_WARNING "No module found in object\n");
1999 err = -ENOEXEC;
2000 goto free_hdr;
2001 }
Rusty Russell5e458cc2008-10-22 10:00:13 -05002002 /* This is temporary: point mod into copy of data. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 mod = (void *)sechdrs[modindex].sh_addr;
2004
2005 if (symindex == 0) {
2006 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2007 mod->name);
2008 err = -ENOEXEC;
2009 goto free_hdr;
2010 }
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
2013 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
2014 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
2015
Rusty Russellea01e792008-03-13 09:02:17 +00002016 /* Don't keep modinfo and version sections. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russellea01e792008-03-13 09:02:17 +00002018 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019#ifdef CONFIG_KALLSYMS
2020 /* Keep symbol and string tables for decoding later. */
2021 sechdrs[symindex].sh_flags |= SHF_ALLOC;
2022 sechdrs[strindex].sh_flags |= SHF_ALLOC;
2023#endif
2024
2025 /* Check module struct version now, before we try to use module. */
2026 if (!check_modstruct_version(sechdrs, versindex, mod)) {
2027 err = -ENOEXEC;
2028 goto free_hdr;
2029 }
2030
2031 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
2032 /* This is allowed: modprobe --force will invalidate it. */
2033 if (!modmagic) {
Rusty Russellc6e665c2009-03-31 13:05:33 -06002034 err = try_to_force_load(mod, "bad vermagic");
Linus Torvalds826e4502008-05-04 17:04:16 -07002035 if (err)
2036 goto free_hdr;
Rusty Russell91e37a72008-05-09 16:25:28 +10002037 } else if (!same_magic(modmagic, vermagic, versindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2039 mod->name, modmagic, vermagic);
2040 err = -ENOEXEC;
2041 goto free_hdr;
2042 }
2043
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002044 staging = get_modinfo(sechdrs, infoindex, "staging");
2045 if (staging) {
2046 add_taint_module(mod, TAINT_CRAP);
2047 printk(KERN_WARNING "%s: module is from the staging directory,"
2048 " the quality is unknown, you have been warned.\n",
2049 mod->name);
2050 }
2051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 /* Now copy in args */
Davi Arnaut24277dd2006-03-24 03:18:43 -08002053 args = strndup_user(uargs, ~0UL >> 1);
2054 if (IS_ERR(args)) {
2055 err = PTR_ERR(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 goto free_hdr;
2057 }
Andrew Morton8e08b752006-02-07 12:58:45 -08002058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 if (find_module(mod->name)) {
2060 err = -EEXIST;
2061 goto free_mod;
2062 }
2063
2064 mod->state = MODULE_STATE_COMING;
2065
2066 /* Allow arches to frob section contents and sizes. */
2067 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
2068 if (err < 0)
2069 goto free_mod;
2070
2071 if (pcpuindex) {
2072 /* We have a special allocation for this section. */
2073 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
Rusty Russell842bbaa2005-08-01 21:11:47 -07002074 sechdrs[pcpuindex].sh_addralign,
2075 mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 if (!percpu) {
2077 err = -ENOMEM;
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002078 goto free_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 }
2080 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2081 mod->percpu = percpu;
2082 }
2083
2084 /* Determine total sizes, and put offsets in sh_entsize. For now
2085 this is done generically; there doesn't appear to be any
2086 special cases for the architectures. */
2087 layout_sections(mod, hdr, sechdrs, secstrings);
2088
2089 /* Do the allocs. */
Rusty Russell3a642e92008-07-22 19:24:28 -05002090 ptr = module_alloc_update_bounds(mod->core_size);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002091 /*
2092 * The pointer to this block is stored in the module structure
2093 * which is inside the block. Just mark it as not being a
2094 * leak.
2095 */
2096 kmemleak_not_leak(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 if (!ptr) {
2098 err = -ENOMEM;
2099 goto free_percpu;
2100 }
2101 memset(ptr, 0, mod->core_size);
2102 mod->module_core = ptr;
2103
Rusty Russell3a642e92008-07-22 19:24:28 -05002104 ptr = module_alloc_update_bounds(mod->init_size);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002105 /*
2106 * The pointer to this block is stored in the module structure
2107 * which is inside the block. This block doesn't need to be
2108 * scanned as it contains data and code that will be freed
2109 * after the module is initialized.
2110 */
2111 kmemleak_ignore(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 if (!ptr && mod->init_size) {
2113 err = -ENOMEM;
2114 goto free_core;
2115 }
2116 memset(ptr, 0, mod->init_size);
2117 mod->module_init = ptr;
2118
2119 /* Transfer each section which specifies SHF_ALLOC */
2120 DEBUGP("final section addresses:\n");
2121 for (i = 0; i < hdr->e_shnum; i++) {
2122 void *dest;
2123
2124 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2125 continue;
2126
2127 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2128 dest = mod->module_init
2129 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2130 else
2131 dest = mod->module_core + sechdrs[i].sh_entsize;
2132
2133 if (sechdrs[i].sh_type != SHT_NOBITS)
2134 memcpy(dest, (void *)sechdrs[i].sh_addr,
2135 sechdrs[i].sh_size);
2136 /* Update sh_addr to point to copy in image. */
2137 sechdrs[i].sh_addr = (unsigned long)dest;
2138 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2139 }
2140 /* Module has been moved. */
2141 mod = (void *)sechdrs[modindex].sh_addr;
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002142 kmemleak_load_module(mod, hdr, sechdrs, secstrings);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002144#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2145 mod->refptr = percpu_modalloc(sizeof(local_t), __alignof__(local_t),
2146 mod->name);
2147 if (!mod->refptr) {
2148 err = -ENOMEM;
2149 goto free_init;
2150 }
2151#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 /* Now we've moved module, initialize linked lists, etc. */
2153 module_unload_init(mod);
2154
Kay Sievers97c146e2007-11-29 23:46:11 +01002155 /* add kobject, so we can reference it. */
Akinobu Mitad58ae672007-10-16 23:30:27 -07002156 err = mod_sysfs_init(mod);
2157 if (err)
Kay Sievers97c146e2007-11-29 23:46:11 +01002158 goto free_unload;
Kay Sievers270a6c42007-01-18 13:26:15 +01002159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 /* Set up license info based on the info section */
2161 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2162
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002163 /*
2164 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2165 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2166 * using GPL-only symbols it needs.
2167 */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002168 if (strcmp(mod->name, "ndiswrapper") == 0)
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002169 add_taint(TAINT_PROPRIETARY_MODULE);
2170
2171 /* driverloader was caught wrongly pretending to be under GPL */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002172 if (strcmp(mod->name, "driverloader") == 0)
2173 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Dave Jones9841d61d2006-01-08 01:03:41 -08002174
Matt Domschc988d2b2005-06-23 22:05:15 -07002175 /* Set up MODINFO_ATTR fields */
2176 setup_modinfo(mod, sechdrs, infoindex);
Matt Domschc988d2b2005-06-23 22:05:15 -07002177
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 /* Fix up syms, so that st_value is a pointer to location. */
2179 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2180 mod);
2181 if (err < 0)
2182 goto cleanup;
2183
Rusty Russell5e458cc2008-10-22 10:00:13 -05002184 /* Now we've got everything in the final locations, we can
2185 * find optional sections. */
Rusty Russelle180a6b2009-03-31 13:05:29 -06002186 mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
2187 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell5e458cc2008-10-22 10:00:13 -05002188 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2189 sizeof(*mod->syms), &mod->num_syms);
2190 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2191 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2192 sizeof(*mod->gpl_syms),
2193 &mod->num_gpl_syms);
2194 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2195 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2196 "__ksymtab_gpl_future",
2197 sizeof(*mod->gpl_future_syms),
2198 &mod->num_gpl_future_syms);
2199 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2200 "__kcrctab_gpl_future");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002202#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002203 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2204 "__ksymtab_unused",
2205 sizeof(*mod->unused_syms),
2206 &mod->num_unused_syms);
2207 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2208 "__kcrctab_unused");
2209 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2210 "__ksymtab_unused_gpl",
2211 sizeof(*mod->unused_gpl_syms),
2212 &mod->num_unused_gpl_syms);
2213 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2214 "__kcrctab_unused_gpl");
2215#endif
2216
2217#ifdef CONFIG_MARKERS
2218 mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers",
2219 sizeof(*mod->markers), &mod->num_markers);
2220#endif
2221#ifdef CONFIG_TRACEPOINTS
2222 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2223 "__tracepoints",
2224 sizeof(*mod->tracepoints),
2225 &mod->num_tracepoints);
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002226#endif
Steven Rostedt6d723732009-04-10 14:53:50 -04002227#ifdef CONFIG_EVENT_TRACING
2228 mod->trace_events = section_objs(hdr, sechdrs, secstrings,
2229 "_ftrace_events",
2230 sizeof(*mod->trace_events),
2231 &mod->num_trace_events);
2232#endif
Steven Rostedt93eb6772009-04-15 13:24:06 -04002233#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2234 /* sechdrs[0].sh_size is always zero */
2235 mod->ftrace_callsites = section_objs(hdr, sechdrs, secstrings,
2236 "__mcount_loc",
2237 sizeof(*mod->ftrace_callsites),
2238 &mod->num_ftrace_callsites);
2239#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240#ifdef CONFIG_MODVERSIONS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002241 if ((mod->num_syms && !mod->crcs)
2242 || (mod->num_gpl_syms && !mod->gpl_crcs)
2243 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002244#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002245 || (mod->num_unused_syms && !mod->unused_crcs)
2246 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002247#endif
2248 ) {
Rusty Russellc6e665c2009-03-31 13:05:33 -06002249 err = try_to_force_load(mod,
2250 "no versions for exported symbols");
Linus Torvalds826e4502008-05-04 17:04:16 -07002251 if (err)
2252 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 }
2254#endif
2255
2256 /* Now do relocations. */
2257 for (i = 1; i < hdr->e_shnum; i++) {
2258 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2259 unsigned int info = sechdrs[i].sh_info;
2260
2261 /* Not a valid relocation section? */
2262 if (info >= hdr->e_shnum)
2263 continue;
2264
2265 /* Don't bother with non-allocated sections */
2266 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2267 continue;
2268
2269 if (sechdrs[i].sh_type == SHT_REL)
2270 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2271 else if (sechdrs[i].sh_type == SHT_RELA)
2272 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2273 mod);
2274 if (err < 0)
2275 goto cleanup;
2276 }
2277
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002278 /* Find duplicate symbols */
2279 err = verify_export_symbols(mod);
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002280 if (err < 0)
2281 goto cleanup;
2282
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 /* Set up and sort exception table */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002284 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2285 sizeof(*mod->extable), &mod->num_exentries);
2286 sort_extable(mod->extable, mod->extable + mod->num_exentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288 /* Finally, copy percpu area over. */
2289 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2290 sechdrs[pcpuindex].sh_size);
2291
2292 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2293
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002294 if (!mod->taints) {
Jason Barone9d376f2009-02-05 11:51:38 -05002295 struct _ddebug *debug;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002296 unsigned int num_debug;
2297
Rusty Russell5e458cc2008-10-22 10:00:13 -05002298 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2299 sizeof(*debug), &num_debug);
Jason Barone9d376f2009-02-05 11:51:38 -05002300 if (debug)
2301 dynamic_debug_setup(debug, num_debug);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002302 }
Steven Rostedt90d595f2008-08-14 15:45:09 -04002303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 err = module_finalize(hdr, sechdrs, mod);
2305 if (err < 0)
2306 goto cleanup;
2307
Thomas Koeller378bac82005-09-06 15:17:11 -07002308 /* flush the icache in correct context */
2309 old_fs = get_fs();
2310 set_fs(KERNEL_DS);
2311
2312 /*
2313 * Flush the instruction cache, since we've played with text.
2314 * Do it before processing of module parameters, so the module
2315 * can provide parameter accessor functions of its own.
2316 */
2317 if (mod->module_init)
2318 flush_icache_range((unsigned long)mod->module_init,
2319 (unsigned long)mod->module_init
2320 + mod->init_size);
2321 flush_icache_range((unsigned long)mod->module_core,
2322 (unsigned long)mod->module_core + mod->core_size);
2323
2324 set_fs(old_fs);
2325
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 mod->args = args;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002327 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002328 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2329 mod->name);
2330
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002331 /* Now sew it into the lists so we can get lockdep and oops
Andi Kleend72b3752008-08-30 10:09:00 +02002332 * info during argument parsing. Noone should access us, since
2333 * strong_try_module_get() will fail.
2334 * lockdep/oops can run asynchronous, so use the RCU list insertion
2335 * function to insert in a way safe to concurrent readers.
2336 * The mutex protects against concurrent writers.
2337 */
2338 list_add_rcu(&mod->list, &modules);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002339
Rusty Russelle180a6b2009-03-31 13:05:29 -06002340 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002342 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Rusty Russelle180a6b2009-03-31 13:05:29 -06002344 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002346 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Roland McGrath6d760132007-10-16 23:26:40 -07002348 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
2350 /* Get rid of temporary copy */
2351 vfree(hdr);
2352
2353 /* Done! */
2354 return mod;
2355
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002356 unlink:
Rusty Russelle91defa2009-03-31 13:05:35 -06002357 /* Unlink carefully: kallsyms could be walking list. */
2358 list_del_rcu(&mod->list);
2359 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 module_arch_cleanup(mod);
2361 cleanup:
Kay Sievers97c146e2007-11-29 23:46:11 +01002362 kobject_del(&mod->mkobj.kobj);
2363 kobject_put(&mod->mkobj.kobj);
2364 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 module_unload_free(mod);
Eric Dumazet720eba32009-02-03 13:31:36 +10302366#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
Américo Wangb10153f2009-03-25 00:07:19 +08002367 free_init:
Eric Dumazet720eba32009-02-03 13:31:36 +10302368 percpu_modfree(mod->refptr);
2369#endif
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002370 module_free(mod, mod->module_init);
2371 free_core:
2372 module_free(mod, mod->module_core);
2373 /* mod will be freed with core. Don't access it beyond this line! */
2374 free_percpu:
2375 if (percpu)
2376 percpu_modfree(percpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 free_mod:
2378 kfree(args);
2379 free_hdr:
2380 vfree(hdr);
Jayachandran C6fe2e702006-01-06 00:19:54 -08002381 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
2383 truncated:
2384 printk(KERN_ERR "Module len %lu truncated\n", len);
2385 err = -ENOEXEC;
2386 goto free_hdr;
2387}
2388
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389/* This is where the real work happens */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002390SYSCALL_DEFINE3(init_module, void __user *, umod,
2391 unsigned long, len, const char __user *, uargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392{
2393 struct module *mod;
2394 int ret = 0;
2395
2396 /* Must have permission */
2397 if (!capable(CAP_SYS_MODULE))
2398 return -EPERM;
2399
2400 /* Only one module load at a time, please */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002401 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 return -EINTR;
2403
2404 /* Do all the hard work */
2405 mod = load_module(umod, len, uargs);
2406 if (IS_ERR(mod)) {
Ashutosh Naik6389a382006-03-23 03:00:46 -08002407 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 return PTR_ERR(mod);
2409 }
2410
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 /* Drop lock so they can recurse */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002412 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
Alan Sterne041c682006-03-27 01:16:30 -08002414 blocking_notifier_call_chain(&module_notify_list,
2415 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
2417 /* Start the module */
2418 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002419 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 if (ret < 0) {
2421 /* Init routine failed: abort. Try to protect us from
2422 buggy refcounters. */
2423 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002424 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002425 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002426 blocking_notifier_call_chain(&module_notify_list,
2427 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002428 mutex_lock(&module_mutex);
2429 free_module(mod);
2430 mutex_unlock(&module_mutex);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002431 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 return ret;
2433 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002434 if (ret > 0) {
2435 printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
2436 "it should follow 0/-E convention\n"
2437 KERN_WARNING "%s: loading module anyway...\n",
2438 __func__, mod->name, ret,
2439 __func__);
2440 dump_stack();
2441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Rusty Russell6c5db222008-03-10 11:43:52 -07002443 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002445 wake_up(&module_wq);
Masami Hiramatsu0deddf432009-01-06 14:41:54 -08002446 blocking_notifier_call_chain(&module_notify_list,
2447 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07002448
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07002449 /* We need to finish all async code before the module init sequence is done */
2450 async_synchronize_full();
2451
Rusty Russell6c5db222008-03-10 11:43:52 -07002452 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 /* Drop initial reference. */
2454 module_put(mod);
2455 module_free(mod, mod->module_init);
2456 mod->module_init = NULL;
2457 mod->init_size = 0;
2458 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002459 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
2461 return 0;
2462}
2463
2464static inline int within(unsigned long addr, void *start, unsigned long size)
2465{
2466 return ((void *)addr >= start && (void *)addr < start + size);
2467}
2468
2469#ifdef CONFIG_KALLSYMS
2470/*
2471 * This ignores the intensely annoying "mapping symbols" found
2472 * in ARM ELF files: $a, $t and $d.
2473 */
2474static inline int is_arm_mapping_symbol(const char *str)
2475{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002476 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 && (str[2] == '\0' || str[2] == '.');
2478}
2479
2480static const char *get_ksymbol(struct module *mod,
2481 unsigned long addr,
2482 unsigned long *size,
2483 unsigned long *offset)
2484{
2485 unsigned int i, best = 0;
2486 unsigned long nextval;
2487
2488 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002489 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002491 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2493
2494 /* Scan for closest preceeding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002495 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 for (i = 1; i < mod->num_symtab; i++) {
2497 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2498 continue;
2499
2500 /* We ignore unnamed symbols: they're uninformative
2501 * and inserted at a whim. */
2502 if (mod->symtab[i].st_value <= addr
2503 && mod->symtab[i].st_value > mod->symtab[best].st_value
2504 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2505 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2506 best = i;
2507 if (mod->symtab[i].st_value > addr
2508 && mod->symtab[i].st_value < nextval
2509 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2510 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2511 nextval = mod->symtab[i].st_value;
2512 }
2513
2514 if (!best)
2515 return NULL;
2516
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002517 if (size)
2518 *size = nextval - mod->symtab[best].st_value;
2519 if (offset)
2520 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 return mod->strtab + mod->symtab[best].st_name;
2522}
2523
Rusty Russell6dd06c92008-01-29 17:13:22 -05002524/* For kallsyms to ask for address resolution. NULL means not found. Careful
2525 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002526const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05002527 unsigned long *size,
2528 unsigned long *offset,
2529 char **modname,
2530 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531{
2532 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002533 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
Rusty Russellcb2a5202008-01-14 00:55:03 -08002535 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002536 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002537 if (within_module_init(addr, mod) ||
2538 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002539 if (modname)
2540 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002541 ret = get_ksymbol(mod, addr, size, offset);
2542 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 }
2544 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05002545 /* Make a copy in here where it's safe */
2546 if (ret) {
2547 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2548 ret = namebuf;
2549 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002550 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002551 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552}
2553
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002554int lookup_module_symbol_name(unsigned long addr, char *symname)
2555{
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) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002560 if (within_module_init(addr, mod) ||
2561 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002562 const char *sym;
2563
2564 sym = get_ksymbol(mod, addr, NULL, NULL);
2565 if (!sym)
2566 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07002567 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002568 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002569 return 0;
2570 }
2571 }
2572out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002573 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002574 return -ERANGE;
2575}
2576
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002577int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2578 unsigned long *offset, char *modname, char *name)
2579{
2580 struct module *mod;
2581
Rusty Russellcb2a5202008-01-14 00:55:03 -08002582 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002583 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002584 if (within_module_init(addr, mod) ||
2585 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002586 const char *sym;
2587
2588 sym = get_ksymbol(mod, addr, size, offset);
2589 if (!sym)
2590 goto out;
2591 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07002592 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002593 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07002594 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002595 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002596 return 0;
2597 }
2598 }
2599out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002600 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002601 return -ERANGE;
2602}
2603
Alexey Dobriyanea078902007-05-08 00:28:39 -07002604int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2605 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
2607 struct module *mod;
2608
Rusty Russellcb2a5202008-01-14 00:55:03 -08002609 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002610 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 if (symnum < mod->num_symtab) {
2612 *value = mod->symtab[symnum].st_value;
2613 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002614 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07002615 KSYM_NAME_LEN);
2616 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06002617 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002618 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002619 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 }
2621 symnum -= mod->num_symtab;
2622 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002623 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002624 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625}
2626
2627static unsigned long mod_find_symname(struct module *mod, const char *name)
2628{
2629 unsigned int i;
2630
2631 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002632 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2633 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 return mod->symtab[i].st_value;
2635 return 0;
2636}
2637
2638/* Look for this name: can be of form module:name. */
2639unsigned long module_kallsyms_lookup_name(const char *name)
2640{
2641 struct module *mod;
2642 char *colon;
2643 unsigned long ret = 0;
2644
2645 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08002646 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 if ((colon = strchr(name, ':')) != NULL) {
2648 *colon = '\0';
2649 if ((mod = find_module(name)) != NULL)
2650 ret = mod_find_symname(mod, colon+1);
2651 *colon = ':';
2652 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02002653 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 if ((ret = mod_find_symname(mod, name)) != 0)
2655 break;
2656 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002657 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 return ret;
2659}
Anders Kaseorg75a66612008-12-05 19:03:58 -05002660
2661int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
2662 struct module *, unsigned long),
2663 void *data)
2664{
2665 struct module *mod;
2666 unsigned int i;
2667 int ret;
2668
2669 list_for_each_entry(mod, &modules, list) {
2670 for (i = 0; i < mod->num_symtab; i++) {
2671 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
2672 mod, mod->symtab[i].st_value);
2673 if (ret != 0)
2674 return ret;
2675 }
2676 }
2677 return 0;
2678}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679#endif /* CONFIG_KALLSYMS */
2680
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002681static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002682{
2683 int bx = 0;
2684
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002685 if (mod->taints ||
2686 mod->state == MODULE_STATE_GOING ||
2687 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002688 buf[bx++] = '(';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002689 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002690 buf[bx++] = 'P';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002691 if (mod->taints & (1 << TAINT_FORCED_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002692 buf[bx++] = 'F';
Linus Torvalds26e9a392008-10-17 09:50:12 -07002693 if (mod->taints & (1 << TAINT_CRAP))
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002694 buf[bx++] = 'C';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002695 /*
2696 * TAINT_FORCED_RMMOD: could be added.
2697 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2698 * apply to modules.
2699 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002700
2701 /* Show a - for module-is-being-unloaded */
2702 if (mod->state == MODULE_STATE_GOING)
2703 buf[bx++] = '-';
2704 /* Show a + for module-is-being-loaded */
2705 if (mod->state == MODULE_STATE_COMING)
2706 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002707 buf[bx++] = ')';
2708 }
2709 buf[bx] = '\0';
2710
2711 return buf;
2712}
2713
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002714#ifdef CONFIG_PROC_FS
2715/* Called by the /proc file system to return a list of modules. */
2716static void *m_start(struct seq_file *m, loff_t *pos)
2717{
2718 mutex_lock(&module_mutex);
2719 return seq_list_start(&modules, *pos);
2720}
2721
2722static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2723{
2724 return seq_list_next(p, &modules, pos);
2725}
2726
2727static void m_stop(struct seq_file *m, void *p)
2728{
2729 mutex_unlock(&module_mutex);
2730}
2731
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732static int m_show(struct seq_file *m, void *p)
2733{
2734 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002735 char buf[8];
2736
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05002737 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 mod->name, mod->init_size + mod->core_size);
2739 print_unload_info(m, mod);
2740
2741 /* Informative for users. */
2742 seq_printf(m, " %s",
2743 mod->state == MODULE_STATE_GOING ? "Unloading":
2744 mod->state == MODULE_STATE_COMING ? "Loading":
2745 "Live");
2746 /* Used by oprofile and other similar tools. */
2747 seq_printf(m, " 0x%p", mod->module_core);
2748
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002749 /* Taints info */
2750 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002751 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002752
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 seq_printf(m, "\n");
2754 return 0;
2755}
2756
2757/* Format: modulename size refcount deps address
2758
2759 Where refcount is a number or -, and deps is a comma-separated list
2760 of depends or -.
2761*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002762static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 .start = m_start,
2764 .next = m_next,
2765 .stop = m_stop,
2766 .show = m_show
2767};
2768
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002769static int modules_open(struct inode *inode, struct file *file)
2770{
2771 return seq_open(file, &modules_op);
2772}
2773
2774static const struct file_operations proc_modules_operations = {
2775 .open = modules_open,
2776 .read = seq_read,
2777 .llseek = seq_lseek,
2778 .release = seq_release,
2779};
2780
2781static int __init proc_modules_init(void)
2782{
2783 proc_create("modules", 0, NULL, &proc_modules_operations);
2784 return 0;
2785}
2786module_init(proc_modules_init);
2787#endif
2788
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789/* Given an address, look for it in the module exception tables. */
2790const struct exception_table_entry *search_module_extables(unsigned long addr)
2791{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 const struct exception_table_entry *e = NULL;
2793 struct module *mod;
2794
Rusty Russell24da1cb2007-07-15 23:41:46 -07002795 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002796 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 if (mod->num_exentries == 0)
2798 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002799
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 e = search_extable(mod->extable,
2801 mod->extable + mod->num_exentries - 1,
2802 addr);
2803 if (e)
2804 break;
2805 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07002806 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
2808 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07002809 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 return e;
2811}
2812
Ingo Molnar4d435f92006-07-03 00:24:24 -07002813/*
Rusty Russelle6104992009-03-31 13:05:31 -06002814 * is_module_address - is this address inside a module?
2815 * @addr: the address to check.
2816 *
2817 * See is_module_text_address() if you simply want to see if the address
2818 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07002819 */
Rusty Russelle6104992009-03-31 13:05:31 -06002820bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07002821{
Rusty Russelle6104992009-03-31 13:05:31 -06002822 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07002823
Rusty Russell24da1cb2007-07-15 23:41:46 -07002824 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06002825 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07002826 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002827
Rusty Russelle6104992009-03-31 13:05:31 -06002828 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07002829}
2830
Rusty Russelle6104992009-03-31 13:05:31 -06002831/*
2832 * __module_address - get the module which contains an address.
2833 * @addr: the address.
2834 *
2835 * Must be called with preempt disabled or module mutex held so that
2836 * module doesn't get freed during this.
2837 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07002838struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839{
2840 struct module *mod;
2841
Rusty Russell3a642e92008-07-22 19:24:28 -05002842 if (addr < module_addr_min || addr > module_addr_max)
2843 return NULL;
2844
Andi Kleend72b3752008-08-30 10:09:00 +02002845 list_for_each_entry_rcu(mod, &modules, list)
Rusty Russelle6104992009-03-31 13:05:31 -06002846 if (within_module_core(addr, mod)
2847 || within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 return mod;
2849 return NULL;
2850}
Tim Abbottc6b37802008-12-05 19:03:59 -05002851EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
Rusty Russelle6104992009-03-31 13:05:31 -06002853/*
2854 * is_module_text_address - is this address inside module code?
2855 * @addr: the address to check.
2856 *
2857 * See is_module_address() if you simply want to see if the address is
2858 * anywhere in a module. See kernel_text_address() for testing if an
2859 * address corresponds to kernel or module code.
2860 */
2861bool is_module_text_address(unsigned long addr)
2862{
2863 bool ret;
2864
2865 preempt_disable();
2866 ret = __module_text_address(addr) != NULL;
2867 preempt_enable();
2868
2869 return ret;
2870}
2871
2872/*
2873 * __module_text_address - get the module whose code contains an address.
2874 * @addr: the address.
2875 *
2876 * Must be called with preempt disabled or module mutex held so that
2877 * module doesn't get freed during this.
2878 */
2879struct module *__module_text_address(unsigned long addr)
2880{
2881 struct module *mod = __module_address(addr);
2882 if (mod) {
2883 /* Make sure it's within the text section. */
2884 if (!within(addr, mod->module_init, mod->init_text_size)
2885 && !within(addr, mod->module_core, mod->core_text_size))
2886 mod = NULL;
2887 }
2888 return mod;
2889}
Tim Abbottc6b37802008-12-05 19:03:59 -05002890EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06002891
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892/* Don't grab lock, we're oopsing. */
2893void print_modules(void)
2894{
2895 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07002896 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
2898 printk("Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02002899 /* Most callers should already have preempt disabled, but make sure */
2900 preempt_disable();
2901 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002902 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02002903 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01002904 if (last_unloaded_module[0])
2905 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 printk("\n");
2907}
2908
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06002910/* Generate the signature for all relevant module structures here.
2911 * If these change, we don't want to try to parse the module. */
2912void module_layout(struct module *mod,
2913 struct modversion_info *ver,
2914 struct kernel_param *kp,
2915 struct kernel_symbol *ks,
2916 struct marker *marker,
2917 struct tracepoint *tp)
2918{
2919}
2920EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002922
2923#ifdef CONFIG_MARKERS
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002924void module_update_markers(void)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002925{
2926 struct module *mod;
2927
2928 mutex_lock(&module_mutex);
2929 list_for_each_entry(mod, &modules, list)
2930 if (!mod->taints)
2931 marker_update_probe_range(mod->markers,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002932 mod->markers + mod->num_markers);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002933 mutex_unlock(&module_mutex);
2934}
2935#endif
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002936
2937#ifdef CONFIG_TRACEPOINTS
2938void module_update_tracepoints(void)
2939{
2940 struct module *mod;
2941
2942 mutex_lock(&module_mutex);
2943 list_for_each_entry(mod, &modules, list)
2944 if (!mod->taints)
2945 tracepoint_update_probe_range(mod->tracepoints,
2946 mod->tracepoints + mod->num_tracepoints);
2947 mutex_unlock(&module_mutex);
2948}
2949
2950/*
2951 * Returns 0 if current not found.
2952 * Returns 1 if current found.
2953 */
2954int module_get_iter_tracepoints(struct tracepoint_iter *iter)
2955{
2956 struct module *iter_mod;
2957 int found = 0;
2958
2959 mutex_lock(&module_mutex);
2960 list_for_each_entry(iter_mod, &modules, list) {
2961 if (!iter_mod->taints) {
2962 /*
2963 * Sorted module list
2964 */
2965 if (iter_mod < iter->module)
2966 continue;
2967 else if (iter_mod > iter->module)
2968 iter->tracepoint = NULL;
2969 found = tracepoint_get_iter_range(&iter->tracepoint,
2970 iter_mod->tracepoints,
2971 iter_mod->tracepoints
2972 + iter_mod->num_tracepoints);
2973 if (found) {
2974 iter->module = iter_mod;
2975 break;
2976 }
2977 }
2978 }
2979 mutex_unlock(&module_mutex);
2980 return found;
2981}
2982#endif