blob: ee7ab612dafa045aaba38459543c5de5b6bc7de5 [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>
21#include <linux/init.h>
Alexey Dobriyanae84e322007-05-08 00:28:38 -070022#include <linux/kallsyms.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040023#include <linux/fs.h>
Roland McGrath6d760132007-10-16 23:26:40 -070024#include <linux/sysfs.h>
Randy Dunlap9f158332005-09-13 01:25:16 -070025#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/slab.h>
27#include <linux/vmalloc.h>
28#include <linux/elf.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040029#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/seq_file.h>
31#include <linux/syscalls.h>
32#include <linux/fcntl.h>
33#include <linux/rcupdate.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080034#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/cpu.h>
36#include <linux/moduleparam.h>
37#include <linux/errno.h>
38#include <linux/err.h>
39#include <linux/vermagic.h>
40#include <linux/notifier.h>
Al Virof6a57032006-10-18 01:47:25 -040041#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/stop_machine.h>
43#include <linux/device.h>
Matt Domschc988d2b2005-06-23 22:05:15 -070044#include <linux/string.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080045#include <linux/mutex.h>
Andi Kleend72b3752008-08-30 10:09:00 +020046#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/cacheflush.h>
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020049#include <linux/license.h>
Christoph Lameter6d762392008-02-08 04:18:42 -080050#include <asm/sections.h>
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040051#include <linux/tracepoint.h>
Steven Rostedt90d595f2008-08-14 15:45:09 -040052#include <linux/ftrace.h>
Arjan van de Ven22a9d642009-01-07 08:45:46 -080053#include <linux/async.h>
Tejun Heofbf59bc2009-02-20 16:29:08 +090054#include <linux/percpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#if 0
57#define DEBUGP printk
58#else
59#define DEBUGP(fmt , a...)
60#endif
61
62#ifndef ARCH_SHF_SMALL
63#define ARCH_SHF_SMALL 0
64#endif
65
66/* If this is set, the section belongs in the init part of the module */
67#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
68
Rusty Russell24da1cb2007-07-15 23:41:46 -070069/* List of modules, protected by module_mutex or preempt_disable
Andi Kleend72b3752008-08-30 10:09:00 +020070 * (delete uses stop_machine/add uses RCU list operations). */
Ashutosh Naik6389a382006-03-23 03:00:46 -080071static DEFINE_MUTEX(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static LIST_HEAD(modules);
73
Stephen Rothwell19e45292009-04-14 17:27:18 +100074/* Block module loading/unloading? */
75int modules_disabled = 0;
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 Russell3a642e92008-07-22 19:24:28 -050082/* Bounds of module allocation, for speeding __module_text_address */
83static 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 -0500192struct symsearch {
193 const struct kernel_symbol *start, *stop;
194 const unsigned long *crcs;
195 enum {
196 NOT_GPL_ONLY,
197 GPL_ONLY,
198 WILL_BE_GPL_ONLY,
199 } licence;
200 bool unused;
201};
202
203static bool each_symbol_in_section(const struct symsearch *arr,
204 unsigned int arrsize,
205 struct module *owner,
206 bool (*fn)(const struct symsearch *syms,
207 struct module *owner,
208 unsigned int symnum, void *data),
209 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100210{
Rusty Russelldafd0942008-07-22 19:24:25 -0500211 unsigned int i, j;
212
213 for (j = 0; j < arrsize; j++) {
214 for (i = 0; i < arr[j].stop - arr[j].start; i++)
215 if (fn(&arr[j], owner, i, data))
216 return true;
217 }
218
219 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100220}
221
Rusty Russelldafd0942008-07-22 19:24:25 -0500222/* Returns true as soon as fn returns true, otherwise false. */
223static bool each_symbol(bool (*fn)(const struct symsearch *arr,
224 struct module *owner,
225 unsigned int symnum, void *data),
226 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700227{
Rusty Russelldafd0942008-07-22 19:24:25 -0500228 struct module *mod;
229 const struct symsearch arr[] = {
230 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
231 NOT_GPL_ONLY, false },
232 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
233 __start___kcrctab_gpl,
234 GPL_ONLY, false },
235 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
236 __start___kcrctab_gpl_future,
237 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500238#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500239 { __start___ksymtab_unused, __stop___ksymtab_unused,
240 __start___kcrctab_unused,
241 NOT_GPL_ONLY, true },
242 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
243 __start___kcrctab_unused_gpl,
244 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500245#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500246 };
247
248 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
249 return true;
250
Andi Kleend72b3752008-08-30 10:09:00 +0200251 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500252 struct symsearch arr[] = {
253 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
254 NOT_GPL_ONLY, false },
255 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
256 mod->gpl_crcs,
257 GPL_ONLY, false },
258 { mod->gpl_future_syms,
259 mod->gpl_future_syms + mod->num_gpl_future_syms,
260 mod->gpl_future_crcs,
261 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500262#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500263 { mod->unused_syms,
264 mod->unused_syms + mod->num_unused_syms,
265 mod->unused_crcs,
266 NOT_GPL_ONLY, true },
267 { mod->unused_gpl_syms,
268 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
269 mod->unused_gpl_crcs,
270 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500271#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500272 };
273
274 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
275 return true;
276 }
277 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700278}
279
Rusty Russelldafd0942008-07-22 19:24:25 -0500280struct find_symbol_arg {
281 /* Input */
282 const char *name;
283 bool gplok;
284 bool warn;
285
286 /* Output */
287 struct module *owner;
288 const unsigned long *crc;
289 unsigned long value;
290};
291
292static bool find_symbol_in_section(const struct symsearch *syms,
293 struct module *owner,
294 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500295{
Rusty Russelldafd0942008-07-22 19:24:25 -0500296 struct find_symbol_arg *fsa = data;
297
298 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
299 return false;
300
301 if (!fsa->gplok) {
302 if (syms->licence == GPL_ONLY)
303 return false;
304 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
305 printk(KERN_WARNING "Symbol %s is being used "
306 "by a non-GPL module, which will not "
307 "be allowed in the future\n", fsa->name);
308 printk(KERN_WARNING "Please see the file "
309 "Documentation/feature-removal-schedule.txt "
310 "in the kernel source tree for more details.\n");
311 }
312 }
313
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500314#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500315 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500316 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500317 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500318 printk(KERN_WARNING
319 "This symbol will go away in the future.\n");
320 printk(KERN_WARNING
321 "Please evalute if this is the right api to use and if "
322 "it really is, submit a report the linux kernel "
323 "mailinglist together with submitting your code for "
324 "inclusion.\n");
325 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500326#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500327
328 fsa->owner = owner;
329 fsa->crc = symversion(syms->crcs, symnum);
330 fsa->value = syms->start[symnum].value;
Rusty Russellad9546c2008-05-01 21:14:59 -0500331 return true;
332}
333
Rusty Russellad9546c2008-05-01 21:14:59 -0500334/* Find a symbol, return value, (optional) crc and (optional) module
335 * which owns it */
336static unsigned long find_symbol(const char *name,
337 struct module **owner,
338 const unsigned long **crc,
339 bool gplok,
340 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Rusty Russelldafd0942008-07-22 19:24:25 -0500342 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Rusty Russelldafd0942008-07-22 19:24:25 -0500344 fsa.name = name;
345 fsa.gplok = gplok;
346 fsa.warn = warn;
347
348 if (each_symbol(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500349 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500350 *owner = fsa.owner;
351 if (crc)
352 *crc = fsa.crc;
353 return fsa.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 DEBUGP("Failed to find symbol %s\n", name);
Christoph Lameter88173502008-02-08 04:18:41 -0800357 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360/* Search for module by name: must hold module_mutex. */
361static struct module *find_module(const char *name)
362{
363 struct module *mod;
364
365 list_for_each_entry(mod, &modules, list) {
366 if (strcmp(mod->name, name) == 0)
367 return mod;
368 }
369 return NULL;
370}
371
372#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900373
374#ifdef CONFIG_HAVE_DYNAMIC_PER_CPU_AREA
375
376static void *percpu_modalloc(unsigned long size, unsigned long align,
377 const char *name)
378{
379 void *ptr;
380
381 if (align > PAGE_SIZE) {
382 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
383 name, align, PAGE_SIZE);
384 align = PAGE_SIZE;
385 }
386
Tejun Heoedcb4632009-03-06 14:33:59 +0900387 ptr = __alloc_reserved_percpu(size, align);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900388 if (!ptr)
389 printk(KERN_WARNING
390 "Could not allocate %lu bytes percpu data\n", size);
391 return ptr;
392}
393
394static void percpu_modfree(void *freeme)
395{
396 free_percpu(freeme);
397}
398
399#else /* ... !CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401/* Number of blocks used and allocated. */
402static unsigned int pcpu_num_used, pcpu_num_allocated;
403/* Size of each block. -ve means used. */
404static int *pcpu_size;
405
406static int split_block(unsigned int i, unsigned short size)
407{
408 /* Reallocation required? */
409 if (pcpu_num_used + 1 > pcpu_num_allocated) {
Pekka Enberg6d4f9c52007-05-08 00:24:58 -0700410 int *new;
411
412 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
413 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (!new)
415 return 0;
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 pcpu_num_allocated *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 pcpu_size = new;
419 }
420
421 /* Insert a new subblock */
422 memmove(&pcpu_size[i+1], &pcpu_size[i],
423 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
424 pcpu_num_used++;
425
426 pcpu_size[i+1] -= size;
427 pcpu_size[i] = size;
428 return 1;
429}
430
431static inline unsigned int block_size(int val)
432{
433 if (val < 0)
434 return -val;
435 return val;
436}
437
Rusty Russell842bbaa2005-08-01 21:11:47 -0700438static void *percpu_modalloc(unsigned long size, unsigned long align,
439 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 unsigned long extra;
442 unsigned int i;
443 void *ptr;
444
Jeremy Fitzhardingeb6e35902007-05-02 19:27:12 +0200445 if (align > PAGE_SIZE) {
446 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
447 name, align, PAGE_SIZE);
448 align = PAGE_SIZE;
Rusty Russell842bbaa2005-08-01 21:11:47 -0700449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 ptr = __per_cpu_start;
452 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
453 /* Extra for alignment requirement. */
454 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
455 BUG_ON(i == 0 && extra != 0);
456
457 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
458 continue;
459
460 /* Transfer extra to previous block. */
461 if (pcpu_size[i-1] < 0)
462 pcpu_size[i-1] -= extra;
463 else
464 pcpu_size[i-1] += extra;
465 pcpu_size[i] -= extra;
466 ptr += extra;
467
468 /* Split block if warranted */
469 if (pcpu_size[i] - size > sizeof(unsigned long))
470 if (!split_block(i, size))
471 return NULL;
472
473 /* Mark allocated */
474 pcpu_size[i] = -pcpu_size[i];
475 return ptr;
476 }
477
478 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
479 size);
480 return NULL;
481}
482
483static void percpu_modfree(void *freeme)
484{
485 unsigned int i;
486 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
487
488 /* First entry is core kernel percpu data. */
489 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
490 if (ptr == freeme) {
491 pcpu_size[i] = -pcpu_size[i];
492 goto free;
493 }
494 }
495 BUG();
496
497 free:
498 /* Merge with previous? */
499 if (pcpu_size[i-1] >= 0) {
500 pcpu_size[i-1] += pcpu_size[i];
501 pcpu_num_used--;
502 memmove(&pcpu_size[i], &pcpu_size[i+1],
503 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
504 i--;
505 }
506 /* Merge with next? */
507 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
508 pcpu_size[i] += pcpu_size[i+1];
509 pcpu_num_used--;
510 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
511 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
512 }
513}
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515static int percpu_modinit(void)
516{
517 pcpu_num_used = 2;
518 pcpu_num_allocated = 2;
519 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
520 GFP_KERNEL);
521 /* Static in-kernel percpu data (used). */
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +0200522 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 /* Free room. */
524 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
525 if (pcpu_size[1] < 0) {
526 printk(KERN_ERR "No per-cpu room for modules.\n");
527 pcpu_num_used = 1;
528 }
529
530 return 0;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700531}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532__initcall(percpu_modinit);
Tejun Heo6b588c12009-02-20 16:29:07 +0900533
Tejun Heofbf59bc2009-02-20 16:29:08 +0900534#endif /* CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
535
Tejun Heo6b588c12009-02-20 16:29:07 +0900536static unsigned int find_pcpusec(Elf_Ehdr *hdr,
537 Elf_Shdr *sechdrs,
538 const char *secstrings)
539{
540 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
541}
542
543static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
544{
545 int cpu;
546
547 for_each_possible_cpu(cpu)
548 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
549}
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900552
Rusty Russell842bbaa2005-08-01 21:11:47 -0700553static inline void *percpu_modalloc(unsigned long size, unsigned long align,
554 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 return NULL;
557}
558static inline void percpu_modfree(void *pcpuptr)
559{
560 BUG();
561}
562static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
563 Elf_Shdr *sechdrs,
564 const char *secstrings)
565{
566 return 0;
567}
568static inline void percpu_modcopy(void *pcpudst, const void *src,
569 unsigned long size)
570{
571 /* pcpusec should be 0, and size of that section should be 0. */
572 BUG_ON(size != 0);
573}
Tejun Heo6b588c12009-02-20 16:29:07 +0900574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575#endif /* CONFIG_SMP */
576
Matt Domschc988d2b2005-06-23 22:05:15 -0700577#define MODINFO_ATTR(field) \
578static void setup_modinfo_##field(struct module *mod, const char *s) \
579{ \
580 mod->field = kstrdup(s, GFP_KERNEL); \
581} \
582static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
583 struct module *mod, char *buffer) \
584{ \
585 return sprintf(buffer, "%s\n", mod->field); \
586} \
587static int modinfo_##field##_exists(struct module *mod) \
588{ \
589 return mod->field != NULL; \
590} \
591static void free_modinfo_##field(struct module *mod) \
592{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700593 kfree(mod->field); \
594 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700595} \
596static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900597 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700598 .show = show_modinfo_##field, \
599 .setup = setup_modinfo_##field, \
600 .test = modinfo_##field##_exists, \
601 .free = free_modinfo_##field, \
602};
603
604MODINFO_ATTR(version);
605MODINFO_ATTR(srcversion);
606
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100607static char last_unloaded_module[MODULE_NAME_LEN+1];
608
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800609#ifdef CONFIG_MODULE_UNLOAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610/* Init the unload section of the module. */
611static void module_unload_init(struct module *mod)
612{
Eric Dumazet720eba32009-02-03 13:31:36 +1030613 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 INIT_LIST_HEAD(&mod->modules_which_use_me);
Eric Dumazet720eba32009-02-03 13:31:36 +1030616 for_each_possible_cpu(cpu)
617 local_set(__module_ref_addr(mod, cpu), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 /* Hold reference count during initialization. */
Eric Dumazet720eba32009-02-03 13:31:36 +1030619 local_set(__module_ref_addr(mod, raw_smp_processor_id()), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 /* Backwards compatibility macros put refcount during init. */
621 mod->waiter = current;
622}
623
624/* modules using other modules */
625struct module_use
626{
627 struct list_head list;
628 struct module *module_which_uses;
629};
630
631/* Does a already use b? */
632static int already_uses(struct module *a, struct module *b)
633{
634 struct module_use *use;
635
636 list_for_each_entry(use, &b->modules_which_use_me, list) {
637 if (use->module_which_uses == a) {
638 DEBUGP("%s uses %s!\n", a->name, b->name);
639 return 1;
640 }
641 }
642 DEBUGP("%s does not use %s!\n", a->name, b->name);
643 return 0;
644}
645
646/* Module a uses b */
647static int use_module(struct module *a, struct module *b)
648{
649 struct module_use *use;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500650 int no_warn, err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (b == NULL || already_uses(a, b)) return 1;
653
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500654 /* If we're interrupted or time out, we fail. */
655 if (wait_event_interruptible_timeout(
656 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
657 30 * HZ) <= 0) {
658 printk("%s: gave up waiting for init of module %s.\n",
659 a->name, b->name);
660 return 0;
661 }
662
663 /* If strong_try_module_get() returned a different error, we fail. */
664 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return 0;
666
667 DEBUGP("Allocating new usage for %s.\n", a->name);
668 use = kmalloc(sizeof(*use), GFP_ATOMIC);
669 if (!use) {
670 printk("%s: out of memory loading\n", a->name);
671 module_put(b);
672 return 0;
673 }
674
675 use->module_which_uses = a;
676 list_add(&use->list, &b->modules_which_use_me);
Kay Sievers270a6c42007-01-18 13:26:15 +0100677 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return 1;
679}
680
681/* Clear the unload stuff of the module. */
682static void module_unload_free(struct module *mod)
683{
684 struct module *i;
685
686 list_for_each_entry(i, &modules, list) {
687 struct module_use *use;
688
689 list_for_each_entry(use, &i->modules_which_use_me, list) {
690 if (use->module_which_uses == mod) {
691 DEBUGP("%s unusing %s\n", mod->name, i->name);
692 module_put(i);
693 list_del(&use->list);
694 kfree(use);
Kay Sievers270a6c42007-01-18 13:26:15 +0100695 sysfs_remove_link(i->holders_dir, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 /* There can be at most one match. */
697 break;
698 }
699 }
700 }
701}
702
703#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800704static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706 int ret = (flags & O_TRUNC);
707 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800708 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return ret;
710}
711#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800712static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 return 0;
715}
716#endif /* CONFIG_MODULE_FORCE_UNLOAD */
717
718struct stopref
719{
720 struct module *mod;
721 int flags;
722 int *forced;
723};
724
725/* Whole machine is stopped with interrupts off when this runs. */
726static int __try_stop_module(void *_sref)
727{
728 struct stopref *sref = _sref;
729
Rusty Russellda39ba52008-07-22 19:24:25 -0500730 /* If it's not unused, quit unless we're forcing. */
731 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800732 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return -EWOULDBLOCK;
734 }
735
736 /* Mark it as dying. */
737 sref->mod->state = MODULE_STATE_GOING;
738 return 0;
739}
740
741static int try_stop_module(struct module *mod, int flags, int *forced)
742{
Rusty Russellda39ba52008-07-22 19:24:25 -0500743 if (flags & O_NONBLOCK) {
744 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500746 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500747 } else {
748 /* We don't need to stop the machine for this. */
749 mod->state = MODULE_STATE_GOING;
750 synchronize_sched();
751 return 0;
752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
755unsigned int module_refcount(struct module *mod)
756{
Eric Dumazet720eba32009-02-03 13:31:36 +1030757 unsigned int total = 0;
758 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Eric Dumazet720eba32009-02-03 13:31:36 +1030760 for_each_possible_cpu(cpu)
761 total += local_read(__module_ref_addr(mod, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return total;
763}
764EXPORT_SYMBOL(module_refcount);
765
766/* This exists whether we can unload or not */
767static void free_module(struct module *mod);
768
769static void wait_for_zero_refcount(struct module *mod)
770{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500771 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800772 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 for (;;) {
774 DEBUGP("Looking at refcount...\n");
775 set_current_state(TASK_UNINTERRUPTIBLE);
776 if (module_refcount(mod) == 0)
777 break;
778 schedule();
779 }
780 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800781 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100784SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
785 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800788 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 int ret, forced = 0;
790
Kees Cook3d433212009-04-02 15:49:29 -0700791 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800792 return -EPERM;
793
794 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
795 return -EFAULT;
796 name[MODULE_NAME_LEN-1] = '\0';
797
Heiko Carstens9e018922008-12-22 12:36:31 +0100798 /* Create stop_machine threads since free_module relies on
799 * a non-failing stop_machine call. */
800 ret = stop_machine_create();
801 if (ret)
802 return ret;
803
804 if (mutex_lock_interruptible(&module_mutex) != 0) {
805 ret = -EINTR;
806 goto out_stop;
807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 mod = find_module(name);
810 if (!mod) {
811 ret = -ENOENT;
812 goto out;
813 }
814
815 if (!list_empty(&mod->modules_which_use_me)) {
816 /* Other modules depend on us: get rid of them first. */
817 ret = -EWOULDBLOCK;
818 goto out;
819 }
820
821 /* Doing init or already dying? */
822 if (mod->state != MODULE_STATE_LIVE) {
823 /* FIXME: if (force), slam module count and wake up
824 waiter --RR */
825 DEBUGP("%s already dying\n", mod->name);
826 ret = -EBUSY;
827 goto out;
828 }
829
830 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700831 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800832 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (!forced) {
834 /* This module can't be removed */
835 ret = -EBUSY;
836 goto out;
837 }
838 }
839
840 /* Set this up before setting mod->state */
841 mod->waiter = current;
842
843 /* Stop the machine so refcounts can't move and disable module. */
844 ret = try_stop_module(mod, flags, &forced);
845 if (ret != 0)
846 goto out;
847
848 /* Never wait if forced. */
849 if (!forced && module_refcount(mod) != 0)
850 wait_for_zero_refcount(mod);
851
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200852 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 /* Final destruction now noone is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200854 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200856 blocking_notifier_call_chain(&module_notify_list,
857 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800858 async_synchronize_full();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200859 mutex_lock(&module_mutex);
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100860 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500861 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Jason Barone9d376f2009-02-05 11:51:38 -0500862 ddebug_remove_module(mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 free_module(mod);
864
865 out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800866 mutex_unlock(&module_mutex);
Heiko Carstens9e018922008-12-22 12:36:31 +0100867out_stop:
868 stop_machine_destroy();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return ret;
870}
871
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800872static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
874 struct module_use *use;
875 int printed_something = 0;
876
877 seq_printf(m, " %u ", module_refcount(mod));
878
879 /* Always include a trailing , so userspace can differentiate
880 between this and the old multi-field proc format. */
881 list_for_each_entry(use, &mod->modules_which_use_me, list) {
882 printed_something = 1;
883 seq_printf(m, "%s,", use->module_which_uses->name);
884 }
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (mod->init != NULL && mod->exit == NULL) {
887 printed_something = 1;
888 seq_printf(m, "[permanent],");
889 }
890
891 if (!printed_something)
892 seq_printf(m, "-");
893}
894
895void __symbol_put(const char *symbol)
896{
897 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Rusty Russell24da1cb2007-07-15 23:41:46 -0700899 preempt_disable();
Rusty Russellad9546c2008-05-01 21:14:59 -0500900 if (IS_ERR_VALUE(find_symbol(symbol, &owner, NULL, true, false)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 BUG();
902 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700903 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905EXPORT_SYMBOL(__symbol_put);
906
907void symbol_put_addr(void *addr)
908{
Trent Piepho5e376612006-05-15 09:44:06 -0700909 struct module *modaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Trent Piepho5e376612006-05-15 09:44:06 -0700911 if (core_kernel_text((unsigned long)addr))
912 return;
913
914 if (!(modaddr = module_text_address((unsigned long)addr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 BUG();
Trent Piepho5e376612006-05-15 09:44:06 -0700916 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918EXPORT_SYMBOL_GPL(symbol_put_addr);
919
920static ssize_t show_refcnt(struct module_attribute *mattr,
921 struct module *mod, char *buffer)
922{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400923 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
926static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900927 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 .show = show_refcnt,
929};
930
Al Virof6a57032006-10-18 01:47:25 -0400931void module_put(struct module *module)
932{
933 if (module) {
934 unsigned int cpu = get_cpu();
Eric Dumazet720eba32009-02-03 13:31:36 +1030935 local_dec(__module_ref_addr(module, cpu));
Al Virof6a57032006-10-18 01:47:25 -0400936 /* Maybe they're waiting for us to drop reference? */
937 if (unlikely(!module_is_live(module)))
938 wake_up_process(module->waiter);
939 put_cpu();
940 }
941}
942EXPORT_SYMBOL(module_put);
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800945static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
947 /* We don't know the usage count, or what modules are using. */
948 seq_printf(m, " - -");
949}
950
951static inline void module_unload_free(struct module *mod)
952{
953}
954
955static inline int use_module(struct module *a, struct module *b)
956{
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500957 return strong_try_module_get(b) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
960static inline void module_unload_init(struct module *mod)
961{
962}
963#endif /* CONFIG_MODULE_UNLOAD */
964
Kay Sievers1f717402006-11-24 12:15:25 +0100965static ssize_t show_initstate(struct module_attribute *mattr,
966 struct module *mod, char *buffer)
967{
968 const char *state = "unknown";
969
970 switch (mod->state) {
971 case MODULE_STATE_LIVE:
972 state = "live";
973 break;
974 case MODULE_STATE_COMING:
975 state = "coming";
976 break;
977 case MODULE_STATE_GOING:
978 state = "going";
979 break;
980 }
981 return sprintf(buffer, "%s\n", state);
982}
983
984static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900985 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100986 .show = show_initstate,
987};
988
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800989static struct module_attribute *modinfo_attrs[] = {
990 &modinfo_version,
991 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100992 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800993#ifdef CONFIG_MODULE_UNLOAD
994 &refcnt,
995#endif
996 NULL,
997};
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999static const char vermagic[] = VERMAGIC_STRING;
1000
Linus Torvalds826e4502008-05-04 17:04:16 -07001001static int try_to_force_load(struct module *mod, const char *symname)
1002{
1003#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -07001004 if (!test_taint(TAINT_FORCED_MODULE))
Linus Torvalds826e4502008-05-04 17:04:16 -07001005 printk("%s: no version for \"%s\" found: kernel tainted.\n",
1006 mod->name, symname);
1007 add_taint_module(mod, TAINT_FORCED_MODULE);
1008 return 0;
1009#else
1010 return -ENOEXEC;
1011#endif
1012}
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014#ifdef CONFIG_MODVERSIONS
1015static int check_version(Elf_Shdr *sechdrs,
1016 unsigned int versindex,
1017 const char *symname,
1018 struct module *mod,
1019 const unsigned long *crc)
1020{
1021 unsigned int i, num_versions;
1022 struct modversion_info *versions;
1023
1024 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1025 if (!crc)
1026 return 1;
1027
Rusty Russella5dd6972008-05-09 16:24:21 +10001028 /* No versions at all? modprobe --force does this. */
1029 if (versindex == 0)
1030 return try_to_force_load(mod, symname) == 0;
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 versions = (void *) sechdrs[versindex].sh_addr;
1033 num_versions = sechdrs[versindex].sh_size
1034 / sizeof(struct modversion_info);
1035
1036 for (i = 0; i < num_versions; i++) {
1037 if (strcmp(versions[i].name, symname) != 0)
1038 continue;
1039
1040 if (versions[i].crc == *crc)
1041 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 DEBUGP("Found checksum %lX vs module %lX\n",
1043 *crc, versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001044 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001046
Rusty Russella5dd6972008-05-09 16:24:21 +10001047 printk(KERN_WARNING "%s: no symbol version for %s\n",
1048 mod->name, symname);
1049 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001050
1051bad_version:
1052 printk("%s: disagrees about version of symbol %s\n",
1053 mod->name, symname);
1054 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056
1057static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1058 unsigned int versindex,
1059 struct module *mod)
1060{
1061 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Rusty Russellad9546c2008-05-01 21:14:59 -05001063 if (IS_ERR_VALUE(find_symbol("struct_module", NULL, &crc, true, false)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 BUG();
Rusty Russellad9546c2008-05-01 21:14:59 -05001065 return check_version(sechdrs, versindex, "struct_module", mod, crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
Rusty Russell91e37a72008-05-09 16:25:28 +10001068/* First part is kernel version, which we ignore if module has crcs. */
1069static inline int same_magic(const char *amagic, const char *bmagic,
1070 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Rusty Russell91e37a72008-05-09 16:25:28 +10001072 if (has_crcs) {
1073 amagic += strcspn(amagic, " ");
1074 bmagic += strcspn(bmagic, " ");
1075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return strcmp(amagic, bmagic) == 0;
1077}
1078#else
1079static inline int check_version(Elf_Shdr *sechdrs,
1080 unsigned int versindex,
1081 const char *symname,
1082 struct module *mod,
1083 const unsigned long *crc)
1084{
1085 return 1;
1086}
1087
1088static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1089 unsigned int versindex,
1090 struct module *mod)
1091{
1092 return 1;
1093}
1094
Rusty Russell91e37a72008-05-09 16:25:28 +10001095static inline int same_magic(const char *amagic, const char *bmagic,
1096 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
1098 return strcmp(amagic, bmagic) == 0;
1099}
1100#endif /* CONFIG_MODVERSIONS */
1101
1102/* Resolve a symbol for this module. I.e. if we find one, record usage.
1103 Must be holding module_mutex. */
1104static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
1105 unsigned int versindex,
1106 const char *name,
1107 struct module *mod)
1108{
1109 struct module *owner;
1110 unsigned long ret;
1111 const unsigned long *crc;
1112
Rusty Russellad9546c2008-05-01 21:14:59 -05001113 ret = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001114 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Christoph Lameter88173502008-02-08 04:18:41 -08001115 if (!IS_ERR_VALUE(ret)) {
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001116 /* use_module can fail due to OOM,
1117 or module initialization or unloading */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1119 !use_module(mod, owner))
Christoph Lameter88173502008-02-08 04:18:41 -08001120 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return ret;
1123}
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125/*
1126 * /sys/module/foo/sections stuff
1127 * J. Corbet <corbet@lwn.net>
1128 */
Kay Sievers120fc3d2008-02-21 00:33:20 +01001129#if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
Rusty Russella58730c2008-03-13 09:03:44 +00001130struct module_sect_attr
1131{
1132 struct module_attribute mattr;
1133 char *name;
1134 unsigned long address;
1135};
1136
1137struct module_sect_attrs
1138{
1139 struct attribute_group grp;
1140 unsigned int nsections;
1141 struct module_sect_attr attrs[0];
1142};
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144static ssize_t module_sect_show(struct module_attribute *mattr,
1145 struct module *mod, char *buf)
1146{
1147 struct module_sect_attr *sattr =
1148 container_of(mattr, struct module_sect_attr, mattr);
1149 return sprintf(buf, "0x%lx\n", sattr->address);
1150}
1151
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001152static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1153{
Rusty Russella58730c2008-03-13 09:03:44 +00001154 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001155
1156 for (section = 0; section < sect_attrs->nsections; section++)
1157 kfree(sect_attrs->attrs[section].name);
1158 kfree(sect_attrs);
1159}
1160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161static void add_sect_attrs(struct module *mod, unsigned int nsect,
1162 char *secstrings, Elf_Shdr *sechdrs)
1163{
1164 unsigned int nloaded = 0, i, size[2];
1165 struct module_sect_attrs *sect_attrs;
1166 struct module_sect_attr *sattr;
1167 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 /* Count loaded sections and allocate structures */
1170 for (i = 0; i < nsect; i++)
1171 if (sechdrs[i].sh_flags & SHF_ALLOC)
1172 nloaded++;
1173 size[0] = ALIGN(sizeof(*sect_attrs)
1174 + nloaded * sizeof(sect_attrs->attrs[0]),
1175 sizeof(sect_attrs->grp.attrs[0]));
1176 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001177 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1178 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return;
1180
1181 /* Setup section attributes. */
1182 sect_attrs->grp.name = "sections";
1183 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1184
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001185 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 sattr = &sect_attrs->attrs[0];
1187 gattr = &sect_attrs->grp.attrs[0];
1188 for (i = 0; i < nsect; i++) {
1189 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1190 continue;
1191 sattr->address = sechdrs[i].sh_addr;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001192 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1193 GFP_KERNEL);
1194 if (sattr->name == NULL)
1195 goto out;
1196 sect_attrs->nsections++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 sattr->mattr.show = module_sect_show;
1198 sattr->mattr.store = NULL;
1199 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 sattr->mattr.attr.mode = S_IRUGO;
1201 *(gattr++) = &(sattr++)->mattr.attr;
1202 }
1203 *gattr = NULL;
1204
1205 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1206 goto out;
1207
1208 mod->sect_attrs = sect_attrs;
1209 return;
1210 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001211 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
1214static void remove_sect_attrs(struct module *mod)
1215{
1216 if (mod->sect_attrs) {
1217 sysfs_remove_group(&mod->mkobj.kobj,
1218 &mod->sect_attrs->grp);
1219 /* We are positive that no one is using any sect attrs
1220 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001221 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 mod->sect_attrs = NULL;
1223 }
1224}
1225
Roland McGrath6d760132007-10-16 23:26:40 -07001226/*
1227 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1228 */
1229
1230struct module_notes_attrs {
1231 struct kobject *dir;
1232 unsigned int notes;
1233 struct bin_attribute attrs[0];
1234};
1235
1236static ssize_t module_notes_read(struct kobject *kobj,
1237 struct bin_attribute *bin_attr,
1238 char *buf, loff_t pos, size_t count)
1239{
1240 /*
1241 * The caller checked the pos and count against our size.
1242 */
1243 memcpy(buf, bin_attr->private + pos, count);
1244 return count;
1245}
1246
1247static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1248 unsigned int i)
1249{
1250 if (notes_attrs->dir) {
1251 while (i-- > 0)
1252 sysfs_remove_bin_file(notes_attrs->dir,
1253 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001254 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001255 }
1256 kfree(notes_attrs);
1257}
1258
1259static void add_notes_attrs(struct module *mod, unsigned int nsect,
1260 char *secstrings, Elf_Shdr *sechdrs)
1261{
1262 unsigned int notes, loaded, i;
1263 struct module_notes_attrs *notes_attrs;
1264 struct bin_attribute *nattr;
1265
1266 /* Count notes sections and allocate structures. */
1267 notes = 0;
1268 for (i = 0; i < nsect; i++)
1269 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1270 (sechdrs[i].sh_type == SHT_NOTE))
1271 ++notes;
1272
1273 if (notes == 0)
1274 return;
1275
1276 notes_attrs = kzalloc(sizeof(*notes_attrs)
1277 + notes * sizeof(notes_attrs->attrs[0]),
1278 GFP_KERNEL);
1279 if (notes_attrs == NULL)
1280 return;
1281
1282 notes_attrs->notes = notes;
1283 nattr = &notes_attrs->attrs[0];
1284 for (loaded = i = 0; i < nsect; ++i) {
1285 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1286 continue;
1287 if (sechdrs[i].sh_type == SHT_NOTE) {
1288 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1289 nattr->attr.mode = S_IRUGO;
1290 nattr->size = sechdrs[i].sh_size;
1291 nattr->private = (void *) sechdrs[i].sh_addr;
1292 nattr->read = module_notes_read;
1293 ++nattr;
1294 }
1295 ++loaded;
1296 }
1297
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001298 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001299 if (!notes_attrs->dir)
1300 goto out;
1301
1302 for (i = 0; i < notes; ++i)
1303 if (sysfs_create_bin_file(notes_attrs->dir,
1304 &notes_attrs->attrs[i]))
1305 goto out;
1306
1307 mod->notes_attrs = notes_attrs;
1308 return;
1309
1310 out:
1311 free_notes_attrs(notes_attrs, i);
1312}
1313
1314static void remove_notes_attrs(struct module *mod)
1315{
1316 if (mod->notes_attrs)
1317 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1318}
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1323 char *sectstrings, Elf_Shdr *sechdrs)
1324{
1325}
1326
1327static inline void remove_sect_attrs(struct module *mod)
1328{
1329}
Roland McGrath6d760132007-10-16 23:26:40 -07001330
1331static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1332 char *sectstrings, Elf_Shdr *sechdrs)
1333{
1334}
1335
1336static inline void remove_notes_attrs(struct module *mod)
1337{
1338}
Kay Sievers120fc3d2008-02-21 00:33:20 +01001339#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Randy Dunlapef665c12007-02-13 15:19:06 -08001341#ifdef CONFIG_SYSFS
1342int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001343{
1344 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001345 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001346 int error = 0;
1347 int i;
1348
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001349 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1350 (ARRAY_SIZE(modinfo_attrs) + 1)),
1351 GFP_KERNEL);
1352 if (!mod->modinfo_attrs)
1353 return -ENOMEM;
1354
1355 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001356 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1357 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001358 (attr->test && attr->test(mod))) {
1359 memcpy(temp_attr, attr, sizeof(*temp_attr));
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001360 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1361 ++temp_attr;
1362 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001363 }
1364 return error;
1365}
1366
Randy Dunlapef665c12007-02-13 15:19:06 -08001367void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001368{
1369 struct module_attribute *attr;
1370 int i;
1371
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001372 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1373 /* pick a field to test for end of list */
1374 if (!attr->attr.name)
1375 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001376 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001377 if (attr->free)
1378 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001379 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001380 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001381}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Randy Dunlapef665c12007-02-13 15:19:06 -08001383int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
1385 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001386 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001388 if (!module_sysfs_initialized) {
1389 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001390 mod->name);
1391 err = -EINVAL;
1392 goto out;
1393 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001394
1395 kobj = kset_find_obj(module_kset, mod->name);
1396 if (kobj) {
1397 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1398 kobject_put(kobj);
1399 err = -EINVAL;
1400 goto out;
1401 }
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001404
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001405 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1406 mod->mkobj.kobj.kset = module_kset;
1407 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1408 "%s", mod->name);
1409 if (err)
1410 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001411
Kay Sievers97c146e2007-11-29 23:46:11 +01001412 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001413out:
1414 return err;
1415}
1416
Randy Dunlapef665c12007-02-13 15:19:06 -08001417int mod_sysfs_setup(struct module *mod,
Kay Sievers270a6c42007-01-18 13:26:15 +01001418 struct kernel_param *kparam,
1419 unsigned int num_params)
1420{
1421 int err;
1422
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001423 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001424 if (!mod->holders_dir) {
1425 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001426 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001427 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 err = module_param_sysfs_setup(mod, kparam, num_params);
1430 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001431 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Matt Domschc988d2b2005-06-23 22:05:15 -07001433 err = module_add_modinfo_attrs(mod);
1434 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001435 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001436
Kay Sieverse17e0f52006-11-24 12:15:25 +01001437 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 return 0;
1439
Kay Sieverse17e0f52006-11-24 12:15:25 +01001440out_unreg_param:
1441 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001442out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001443 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001444out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001445 kobject_put(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return err;
1447}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001448
1449static void mod_sysfs_fini(struct module *mod)
1450{
1451 kobject_put(&mod->mkobj.kobj);
1452}
1453
1454#else /* CONFIG_SYSFS */
1455
1456static void mod_sysfs_fini(struct module *mod)
1457{
1458}
1459
1460#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462static void mod_kobject_remove(struct module *mod)
1463{
Matt Domschc988d2b2005-06-23 22:05:15 -07001464 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001466 kobject_put(mod->mkobj.drivers_dir);
1467 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001468 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
1471/*
1472 * unlink the module with the whole machine is stopped with interrupts off
1473 * - this defends against kallsyms not taking locks
1474 */
1475static int __unlink_module(void *_mod)
1476{
1477 struct module *mod = _mod;
1478 list_del(&mod->list);
1479 return 0;
1480}
1481
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001482/* Free a module, remove from lists, etc (must hold module_mutex). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483static void free_module(struct module *mod)
1484{
1485 /* Delete from various lists */
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001486 stop_machine(__unlink_module, mod, NULL);
Roland McGrath6d760132007-10-16 23:26:40 -07001487 remove_notes_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 remove_sect_attrs(mod);
1489 mod_kobject_remove(mod);
1490
1491 /* Arch-specific cleanup. */
1492 module_arch_cleanup(mod);
1493
1494 /* Module unload stuff */
1495 module_unload_free(mod);
1496
Steven Rostedtfed19392008-08-14 22:47:19 -04001497 /* release any pointers to mcount in this module */
1498 ftrace_release(mod->module_core, mod->core_size);
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 /* This may be NULL, but that's OK */
1501 module_free(mod, mod->module_init);
1502 kfree(mod->args);
1503 if (mod->percpu)
1504 percpu_modfree(mod->percpu);
Eric Dumazet720eba32009-02-03 13:31:36 +10301505#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
1506 if (mod->refptr)
1507 percpu_modfree(mod->refptr);
1508#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001509 /* Free lock-classes: */
1510 lockdep_free_key_range(mod->module_core, mod->core_size);
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 /* Finally, free the core (containing the module structure) */
1513 module_free(mod, mod->module_core);
1514}
1515
1516void *__symbol_get(const char *symbol)
1517{
1518 struct module *owner;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001519 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Rusty Russell24da1cb2007-07-15 23:41:46 -07001521 preempt_disable();
Rusty Russellad9546c2008-05-01 21:14:59 -05001522 value = find_symbol(symbol, &owner, NULL, true, true);
Christoph Lameter88173502008-02-08 04:18:41 -08001523 if (IS_ERR_VALUE(value))
1524 value = 0;
1525 else if (strong_try_module_get(owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 value = 0;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001527 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 return (void *)value;
1530}
1531EXPORT_SYMBOL_GPL(__symbol_get);
1532
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001533/*
1534 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001535 * in the kernel or in some other module's exported symbol table.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001536 */
1537static int verify_export_symbols(struct module *mod)
1538{
Rusty Russellb2111042008-05-01 21:15:00 -05001539 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001540 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001541 const struct kernel_symbol *s;
1542 struct {
1543 const struct kernel_symbol *sym;
1544 unsigned int num;
1545 } arr[] = {
1546 { mod->syms, mod->num_syms },
1547 { mod->gpl_syms, mod->num_gpl_syms },
1548 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001549#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001550 { mod->unused_syms, mod->num_unused_syms },
1551 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001552#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001553 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001554
Rusty Russellb2111042008-05-01 21:15:00 -05001555 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1556 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1557 if (!IS_ERR_VALUE(find_symbol(s->name, &owner,
1558 NULL, true, false))) {
1559 printk(KERN_ERR
1560 "%s: exports duplicate symbol %s"
1561 " (owned by %s)\n",
1562 mod->name, s->name, module_name(owner));
1563 return -ENOEXEC;
1564 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001565 }
Rusty Russellb2111042008-05-01 21:15:00 -05001566 }
1567 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001568}
1569
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001570/* Change all symbols so that st_value encodes the pointer directly. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571static int simplify_symbols(Elf_Shdr *sechdrs,
1572 unsigned int symindex,
1573 const char *strtab,
1574 unsigned int versindex,
1575 unsigned int pcpuindex,
1576 struct module *mod)
1577{
1578 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1579 unsigned long secbase;
1580 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1581 int ret = 0;
1582
1583 for (i = 1; i < n; i++) {
1584 switch (sym[i].st_shndx) {
1585 case SHN_COMMON:
1586 /* We compiled with -fno-common. These are not
1587 supposed to happen. */
1588 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1589 printk("%s: please compile with -fno-common\n",
1590 mod->name);
1591 ret = -ENOEXEC;
1592 break;
1593
1594 case SHN_ABS:
1595 /* Don't need to do anything */
1596 DEBUGP("Absolute symbol: 0x%08lx\n",
1597 (long)sym[i].st_value);
1598 break;
1599
1600 case SHN_UNDEF:
1601 sym[i].st_value
1602 = resolve_symbol(sechdrs, versindex,
1603 strtab + sym[i].st_name, mod);
1604
1605 /* Ok if resolved. */
Christoph Lameter88173502008-02-08 04:18:41 -08001606 if (!IS_ERR_VALUE(sym[i].st_value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 break;
1608 /* Ok if weak. */
1609 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1610 break;
1611
1612 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1613 mod->name, strtab + sym[i].st_name);
1614 ret = -ENOENT;
1615 break;
1616
1617 default:
1618 /* Divert to percpu allocation if a percpu var. */
1619 if (sym[i].st_shndx == pcpuindex)
1620 secbase = (unsigned long)mod->percpu;
1621 else
1622 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1623 sym[i].st_value += secbase;
1624 break;
1625 }
1626 }
1627
1628 return ret;
1629}
1630
Helge Deller088af9a2008-12-31 12:31:18 +01001631/* Additional bytes needed by arch in front of individual sections */
1632unsigned int __weak arch_mod_section_prepend(struct module *mod,
1633 unsigned int section)
1634{
1635 /* default implementation just returns zero */
1636 return 0;
1637}
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01001640static long get_offset(struct module *mod, unsigned int *size,
1641 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
1643 long ret;
1644
Helge Deller088af9a2008-12-31 12:31:18 +01001645 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1647 *size = ret + sechdr->sh_size;
1648 return ret;
1649}
1650
1651/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1652 might -- code, read-only data, read-write data, small data. Tally
1653 sizes, and place the offsets into sh_entsize fields: high bit means it
1654 belongs in init. */
1655static void layout_sections(struct module *mod,
1656 const Elf_Ehdr *hdr,
1657 Elf_Shdr *sechdrs,
1658 const char *secstrings)
1659{
1660 static unsigned long const masks[][2] = {
1661 /* NOTE: all executable code must be the first section
1662 * in this array; otherwise modify the text_size
1663 * finder in the two loops below */
1664 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1665 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1666 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1667 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1668 };
1669 unsigned int m, i;
1670
1671 for (i = 0; i < hdr->e_shnum; i++)
1672 sechdrs[i].sh_entsize = ~0UL;
1673
1674 DEBUGP("Core section allocation order:\n");
1675 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1676 for (i = 0; i < hdr->e_shnum; ++i) {
1677 Elf_Shdr *s = &sechdrs[i];
1678
1679 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1680 || (s->sh_flags & masks[m][1])
1681 || s->sh_entsize != ~0UL
1682 || strncmp(secstrings + s->sh_name,
1683 ".init", 5) == 0)
1684 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001685 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 DEBUGP("\t%s\n", secstrings + s->sh_name);
1687 }
1688 if (m == 0)
1689 mod->core_text_size = mod->core_size;
1690 }
1691
1692 DEBUGP("Init section allocation order:\n");
1693 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1694 for (i = 0; i < hdr->e_shnum; ++i) {
1695 Elf_Shdr *s = &sechdrs[i];
1696
1697 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1698 || (s->sh_flags & masks[m][1])
1699 || s->sh_entsize != ~0UL
1700 || strncmp(secstrings + s->sh_name,
1701 ".init", 5) != 0)
1702 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001703 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 | INIT_OFFSET_MASK);
1705 DEBUGP("\t%s\n", secstrings + s->sh_name);
1706 }
1707 if (m == 0)
1708 mod->init_text_size = mod->init_size;
1709 }
1710}
1711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712static void set_license(struct module *mod, const char *license)
1713{
1714 if (!license)
1715 license = "unspecified";
1716
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001717 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07001718 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001719 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001720 "kernel.\n", mod->name, license);
1721 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
1723}
1724
1725/* Parse tag=value strings from .modinfo section */
1726static char *next_string(char *string, unsigned long *secsize)
1727{
1728 /* Skip non-zero chars */
1729 while (string[0]) {
1730 string++;
1731 if ((*secsize)-- <= 1)
1732 return NULL;
1733 }
1734
1735 /* Skip any zero padding. */
1736 while (!string[0]) {
1737 string++;
1738 if ((*secsize)-- <= 1)
1739 return NULL;
1740 }
1741 return string;
1742}
1743
1744static char *get_modinfo(Elf_Shdr *sechdrs,
1745 unsigned int info,
1746 const char *tag)
1747{
1748 char *p;
1749 unsigned int taglen = strlen(tag);
1750 unsigned long size = sechdrs[info].sh_size;
1751
1752 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1753 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1754 return p + taglen + 1;
1755 }
1756 return NULL;
1757}
1758
Matt Domschc988d2b2005-06-23 22:05:15 -07001759static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1760 unsigned int infoindex)
1761{
1762 struct module_attribute *attr;
1763 int i;
1764
1765 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1766 if (attr->setup)
1767 attr->setup(mod,
1768 get_modinfo(sechdrs,
1769 infoindex,
1770 attr->attr.name));
1771 }
1772}
Matt Domschc988d2b2005-06-23 22:05:15 -07001773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01001775
1776/* lookup symbol in given range of kernel_symbols */
1777static const struct kernel_symbol *lookup_symbol(const char *name,
1778 const struct kernel_symbol *start,
1779 const struct kernel_symbol *stop)
1780{
1781 const struct kernel_symbol *ks = start;
1782 for (; ks < stop; ks++)
1783 if (strcmp(ks->name, name) == 0)
1784 return ks;
1785 return NULL;
1786}
1787
Tim Abbottca4787b2009-01-05 08:40:10 -06001788static int is_exported(const char *name, unsigned long value,
1789 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
Tim Abbottca4787b2009-01-05 08:40:10 -06001791 const struct kernel_symbol *ks;
1792 if (!mod)
1793 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001794 else
Tim Abbottca4787b2009-01-05 08:40:10 -06001795 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
1796 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797}
1798
1799/* As per nm */
1800static char elf_type(const Elf_Sym *sym,
1801 Elf_Shdr *sechdrs,
1802 const char *secstrings,
1803 struct module *mod)
1804{
1805 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1806 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1807 return 'v';
1808 else
1809 return 'w';
1810 }
1811 if (sym->st_shndx == SHN_UNDEF)
1812 return 'U';
1813 if (sym->st_shndx == SHN_ABS)
1814 return 'a';
1815 if (sym->st_shndx >= SHN_LORESERVE)
1816 return '?';
1817 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1818 return 't';
1819 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1820 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1821 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1822 return 'r';
1823 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1824 return 'g';
1825 else
1826 return 'd';
1827 }
1828 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1829 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1830 return 's';
1831 else
1832 return 'b';
1833 }
1834 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1835 ".debug", strlen(".debug")) == 0)
1836 return 'n';
1837 return '?';
1838}
1839
1840static void add_kallsyms(struct module *mod,
1841 Elf_Shdr *sechdrs,
1842 unsigned int symindex,
1843 unsigned int strindex,
1844 const char *secstrings)
1845{
1846 unsigned int i;
1847
1848 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1849 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1850 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1851
1852 /* Set types up while we still have access to sections. */
1853 for (i = 0; i < mod->num_symtab; i++)
1854 mod->symtab[i].st_info
1855 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1856}
1857#else
1858static inline void add_kallsyms(struct module *mod,
1859 Elf_Shdr *sechdrs,
1860 unsigned int symindex,
1861 unsigned int strindex,
1862 const char *secstrings)
1863{
1864}
1865#endif /* CONFIG_KALLSYMS */
1866
Jason Barone9d376f2009-02-05 11:51:38 -05001867static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05001868{
Jason Barone9d376f2009-02-05 11:51:38 -05001869#ifdef CONFIG_DYNAMIC_DEBUG
1870 if (ddebug_add_module(debug, num, debug->modname))
1871 printk(KERN_ERR "dynamic debug error adding module: %s\n",
1872 debug->modname);
1873#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05001874}
Jason Baron346e15b2008-08-12 16:46:19 -04001875
Rusty Russell3a642e92008-07-22 19:24:28 -05001876static void *module_alloc_update_bounds(unsigned long size)
1877{
1878 void *ret = module_alloc(size);
1879
1880 if (ret) {
1881 /* Update module bounds. */
1882 if ((unsigned long)ret < module_addr_min)
1883 module_addr_min = (unsigned long)ret;
1884 if ((unsigned long)ret + size > module_addr_max)
1885 module_addr_max = (unsigned long)ret + size;
1886 }
1887 return ret;
1888}
1889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890/* Allocate and load the module: note that size of section 0 is always
1891 zero, and we rely on this for optional sections. */
Linus Torvaldsffb4ba72008-08-25 11:10:26 -07001892static noinline struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 unsigned long len,
1894 const char __user *uargs)
1895{
1896 Elf_Ehdr *hdr;
1897 Elf_Shdr *sechdrs;
1898 char *secstrings, *args, *modmagic, *strtab = NULL;
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07001899 char *staging;
Andrew Morton84860f92006-06-28 04:26:46 -07001900 unsigned int i;
1901 unsigned int symindex = 0;
1902 unsigned int strindex = 0;
Rusty Russell5e458cc2008-10-22 10:00:13 -05001903 unsigned int modindex, versindex, infoindex, pcpuindex;
Rusty Russell5e458cc2008-10-22 10:00:13 -05001904 unsigned int num_kp, num_mcount;
1905 struct kernel_param *kp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 struct module *mod;
1907 long err = 0;
1908 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
Rusty Russell5e458cc2008-10-22 10:00:13 -05001909 unsigned long *mseg;
Thomas Koeller378bac82005-09-06 15:17:11 -07001910 mm_segment_t old_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
1912 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1913 umod, len, uargs);
1914 if (len < sizeof(*hdr))
1915 return ERR_PTR(-ENOEXEC);
1916
1917 /* Suck in entire file: we'll want most of it. */
1918 /* vmalloc barfs on "unusual" numbers. Check here */
1919 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1920 return ERR_PTR(-ENOMEM);
Heiko Carstens9e018922008-12-22 12:36:31 +01001921
1922 /* Create stop_machine threads since the error path relies on
1923 * a non-failing stop_machine call. */
1924 err = stop_machine_create();
1925 if (err)
1926 goto free_hdr;
1927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 if (copy_from_user(hdr, umod, len) != 0) {
1929 err = -EFAULT;
1930 goto free_hdr;
1931 }
1932
1933 /* Sanity checks against insmoding binaries or wrong arch,
1934 weird elf version */
Cyrill Gorcunovc4ea6fc2008-05-14 16:27:29 -07001935 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 || hdr->e_type != ET_REL
1937 || !elf_check_arch(hdr)
1938 || hdr->e_shentsize != sizeof(*sechdrs)) {
1939 err = -ENOEXEC;
1940 goto free_hdr;
1941 }
1942
1943 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1944 goto truncated;
1945
1946 /* Convenience variables */
1947 sechdrs = (void *)hdr + hdr->e_shoff;
1948 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1949 sechdrs[0].sh_addr = 0;
1950
1951 for (i = 1; i < hdr->e_shnum; i++) {
1952 if (sechdrs[i].sh_type != SHT_NOBITS
1953 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1954 goto truncated;
1955
1956 /* Mark all sections sh_addr with their address in the
1957 temporary image. */
1958 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1959
1960 /* Internal symbols and strings. */
1961 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1962 symindex = i;
1963 strindex = sechdrs[i].sh_link;
1964 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1965 }
1966#ifndef CONFIG_MODULE_UNLOAD
1967 /* Don't load .exit sections */
1968 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1969 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1970#endif
1971 }
1972
1973 modindex = find_sec(hdr, sechdrs, secstrings,
1974 ".gnu.linkonce.this_module");
1975 if (!modindex) {
1976 printk(KERN_WARNING "No module found in object\n");
1977 err = -ENOEXEC;
1978 goto free_hdr;
1979 }
Rusty Russell5e458cc2008-10-22 10:00:13 -05001980 /* This is temporary: point mod into copy of data. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 mod = (void *)sechdrs[modindex].sh_addr;
1982
1983 if (symindex == 0) {
1984 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1985 mod->name);
1986 err = -ENOEXEC;
1987 goto free_hdr;
1988 }
1989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1991 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1992 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1993
Rusty Russellea01e792008-03-13 09:02:17 +00001994 /* Don't keep modinfo and version sections. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russellea01e792008-03-13 09:02:17 +00001996 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997#ifdef CONFIG_KALLSYMS
1998 /* Keep symbol and string tables for decoding later. */
1999 sechdrs[symindex].sh_flags |= SHF_ALLOC;
2000 sechdrs[strindex].sh_flags |= SHF_ALLOC;
2001#endif
2002
2003 /* Check module struct version now, before we try to use module. */
2004 if (!check_modstruct_version(sechdrs, versindex, mod)) {
2005 err = -ENOEXEC;
2006 goto free_hdr;
2007 }
2008
2009 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
2010 /* This is allowed: modprobe --force will invalidate it. */
2011 if (!modmagic) {
Linus Torvalds826e4502008-05-04 17:04:16 -07002012 err = try_to_force_load(mod, "magic");
2013 if (err)
2014 goto free_hdr;
Rusty Russell91e37a72008-05-09 16:25:28 +10002015 } else if (!same_magic(modmagic, vermagic, versindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2017 mod->name, modmagic, vermagic);
2018 err = -ENOEXEC;
2019 goto free_hdr;
2020 }
2021
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002022 staging = get_modinfo(sechdrs, infoindex, "staging");
2023 if (staging) {
2024 add_taint_module(mod, TAINT_CRAP);
2025 printk(KERN_WARNING "%s: module is from the staging directory,"
2026 " the quality is unknown, you have been warned.\n",
2027 mod->name);
2028 }
2029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 /* Now copy in args */
Davi Arnaut24277dd2006-03-24 03:18:43 -08002031 args = strndup_user(uargs, ~0UL >> 1);
2032 if (IS_ERR(args)) {
2033 err = PTR_ERR(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 goto free_hdr;
2035 }
Andrew Morton8e08b752006-02-07 12:58:45 -08002036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 if (find_module(mod->name)) {
2038 err = -EEXIST;
2039 goto free_mod;
2040 }
2041
2042 mod->state = MODULE_STATE_COMING;
2043
2044 /* Allow arches to frob section contents and sizes. */
2045 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
2046 if (err < 0)
2047 goto free_mod;
2048
2049 if (pcpuindex) {
2050 /* We have a special allocation for this section. */
2051 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
Rusty Russell842bbaa2005-08-01 21:11:47 -07002052 sechdrs[pcpuindex].sh_addralign,
2053 mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 if (!percpu) {
2055 err = -ENOMEM;
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002056 goto free_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 }
2058 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2059 mod->percpu = percpu;
2060 }
2061
2062 /* Determine total sizes, and put offsets in sh_entsize. For now
2063 this is done generically; there doesn't appear to be any
2064 special cases for the architectures. */
2065 layout_sections(mod, hdr, sechdrs, secstrings);
2066
2067 /* Do the allocs. */
Rusty Russell3a642e92008-07-22 19:24:28 -05002068 ptr = module_alloc_update_bounds(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (!ptr) {
2070 err = -ENOMEM;
2071 goto free_percpu;
2072 }
2073 memset(ptr, 0, mod->core_size);
2074 mod->module_core = ptr;
2075
Rusty Russell3a642e92008-07-22 19:24:28 -05002076 ptr = module_alloc_update_bounds(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 if (!ptr && mod->init_size) {
2078 err = -ENOMEM;
2079 goto free_core;
2080 }
2081 memset(ptr, 0, mod->init_size);
2082 mod->module_init = ptr;
2083
2084 /* Transfer each section which specifies SHF_ALLOC */
2085 DEBUGP("final section addresses:\n");
2086 for (i = 0; i < hdr->e_shnum; i++) {
2087 void *dest;
2088
2089 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2090 continue;
2091
2092 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2093 dest = mod->module_init
2094 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2095 else
2096 dest = mod->module_core + sechdrs[i].sh_entsize;
2097
2098 if (sechdrs[i].sh_type != SHT_NOBITS)
2099 memcpy(dest, (void *)sechdrs[i].sh_addr,
2100 sechdrs[i].sh_size);
2101 /* Update sh_addr to point to copy in image. */
2102 sechdrs[i].sh_addr = (unsigned long)dest;
2103 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2104 }
2105 /* Module has been moved. */
2106 mod = (void *)sechdrs[modindex].sh_addr;
2107
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002108#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2109 mod->refptr = percpu_modalloc(sizeof(local_t), __alignof__(local_t),
2110 mod->name);
2111 if (!mod->refptr) {
2112 err = -ENOMEM;
2113 goto free_init;
2114 }
2115#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 /* Now we've moved module, initialize linked lists, etc. */
2117 module_unload_init(mod);
2118
Kay Sievers97c146e2007-11-29 23:46:11 +01002119 /* add kobject, so we can reference it. */
Akinobu Mitad58ae672007-10-16 23:30:27 -07002120 err = mod_sysfs_init(mod);
2121 if (err)
Kay Sievers97c146e2007-11-29 23:46:11 +01002122 goto free_unload;
Kay Sievers270a6c42007-01-18 13:26:15 +01002123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 /* Set up license info based on the info section */
2125 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2126
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002127 /*
2128 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2129 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2130 * using GPL-only symbols it needs.
2131 */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002132 if (strcmp(mod->name, "ndiswrapper") == 0)
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002133 add_taint(TAINT_PROPRIETARY_MODULE);
2134
2135 /* driverloader was caught wrongly pretending to be under GPL */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002136 if (strcmp(mod->name, "driverloader") == 0)
2137 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Dave Jones9841d61d2006-01-08 01:03:41 -08002138
Matt Domschc988d2b2005-06-23 22:05:15 -07002139 /* Set up MODINFO_ATTR fields */
2140 setup_modinfo(mod, sechdrs, infoindex);
Matt Domschc988d2b2005-06-23 22:05:15 -07002141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 /* Fix up syms, so that st_value is a pointer to location. */
2143 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2144 mod);
2145 if (err < 0)
2146 goto cleanup;
2147
Rusty Russell5e458cc2008-10-22 10:00:13 -05002148 /* Now we've got everything in the final locations, we can
2149 * find optional sections. */
2150 kp = section_objs(hdr, sechdrs, secstrings, "__param", sizeof(*kp),
2151 &num_kp);
2152 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2153 sizeof(*mod->syms), &mod->num_syms);
2154 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2155 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2156 sizeof(*mod->gpl_syms),
2157 &mod->num_gpl_syms);
2158 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2159 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2160 "__ksymtab_gpl_future",
2161 sizeof(*mod->gpl_future_syms),
2162 &mod->num_gpl_future_syms);
2163 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2164 "__kcrctab_gpl_future");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002166#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002167 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2168 "__ksymtab_unused",
2169 sizeof(*mod->unused_syms),
2170 &mod->num_unused_syms);
2171 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2172 "__kcrctab_unused");
2173 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2174 "__ksymtab_unused_gpl",
2175 sizeof(*mod->unused_gpl_syms),
2176 &mod->num_unused_gpl_syms);
2177 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2178 "__kcrctab_unused_gpl");
2179#endif
2180
2181#ifdef CONFIG_MARKERS
2182 mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers",
2183 sizeof(*mod->markers), &mod->num_markers);
2184#endif
2185#ifdef CONFIG_TRACEPOINTS
2186 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2187 "__tracepoints",
2188 sizeof(*mod->tracepoints),
2189 &mod->num_tracepoints);
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002190#endif
Arjan van de Venf71d20e2006-06-28 04:26:45 -07002191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192#ifdef CONFIG_MODVERSIONS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002193 if ((mod->num_syms && !mod->crcs)
2194 || (mod->num_gpl_syms && !mod->gpl_crcs)
2195 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002196#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002197 || (mod->num_unused_syms && !mod->unused_crcs)
2198 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002199#endif
2200 ) {
Linus Torvalds826e4502008-05-04 17:04:16 -07002201 printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name);
2202 err = try_to_force_load(mod, "nocrc");
2203 if (err)
2204 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 }
2206#endif
2207
2208 /* Now do relocations. */
2209 for (i = 1; i < hdr->e_shnum; i++) {
2210 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2211 unsigned int info = sechdrs[i].sh_info;
2212
2213 /* Not a valid relocation section? */
2214 if (info >= hdr->e_shnum)
2215 continue;
2216
2217 /* Don't bother with non-allocated sections */
2218 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2219 continue;
2220
2221 if (sechdrs[i].sh_type == SHT_REL)
2222 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2223 else if (sechdrs[i].sh_type == SHT_RELA)
2224 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2225 mod);
2226 if (err < 0)
2227 goto cleanup;
2228 }
2229
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002230 /* Find duplicate symbols */
2231 err = verify_export_symbols(mod);
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002232 if (err < 0)
2233 goto cleanup;
2234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 /* Set up and sort exception table */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002236 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2237 sizeof(*mod->extable), &mod->num_exentries);
2238 sort_extable(mod->extable, mod->extable + mod->num_exentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
2240 /* Finally, copy percpu area over. */
2241 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2242 sechdrs[pcpuindex].sh_size);
2243
2244 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2245
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002246 if (!mod->taints) {
Jason Barone9d376f2009-02-05 11:51:38 -05002247 struct _ddebug *debug;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002248 unsigned int num_debug;
2249
Rusty Russell5e458cc2008-10-22 10:00:13 -05002250 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2251 sizeof(*debug), &num_debug);
Jason Barone9d376f2009-02-05 11:51:38 -05002252 if (debug)
2253 dynamic_debug_setup(debug, num_debug);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002254 }
Steven Rostedt90d595f2008-08-14 15:45:09 -04002255
Steven Rostedtfed19392008-08-14 22:47:19 -04002256 /* sechdrs[0].sh_size is always zero */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002257 mseg = section_objs(hdr, sechdrs, secstrings, "__mcount_loc",
2258 sizeof(*mseg), &num_mcount);
Steven Rostedt31e88902008-11-14 16:21:19 -08002259 ftrace_init_module(mod, mseg, mseg + num_mcount);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002260
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 err = module_finalize(hdr, sechdrs, mod);
2262 if (err < 0)
2263 goto cleanup;
2264
Thomas Koeller378bac82005-09-06 15:17:11 -07002265 /* flush the icache in correct context */
2266 old_fs = get_fs();
2267 set_fs(KERNEL_DS);
2268
2269 /*
2270 * Flush the instruction cache, since we've played with text.
2271 * Do it before processing of module parameters, so the module
2272 * can provide parameter accessor functions of its own.
2273 */
2274 if (mod->module_init)
2275 flush_icache_range((unsigned long)mod->module_init,
2276 (unsigned long)mod->module_init
2277 + mod->init_size);
2278 flush_icache_range((unsigned long)mod->module_core,
2279 (unsigned long)mod->module_core + mod->core_size);
2280
2281 set_fs(old_fs);
2282
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 mod->args = args;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002284 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002285 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2286 mod->name);
2287
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002288 /* Now sew it into the lists so we can get lockdep and oops
Andi Kleend72b3752008-08-30 10:09:00 +02002289 * info during argument parsing. Noone should access us, since
2290 * strong_try_module_get() will fail.
2291 * lockdep/oops can run asynchronous, so use the RCU list insertion
2292 * function to insert in a way safe to concurrent readers.
2293 * The mutex protects against concurrent writers.
2294 */
2295 list_add_rcu(&mod->list, &modules);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002296
Rusty Russell5e458cc2008-10-22 10:00:13 -05002297 err = parse_args(mod->name, mod->args, kp, num_kp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002299 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Rusty Russell5e458cc2008-10-22 10:00:13 -05002301 err = mod_sysfs_setup(mod, kp, num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002303 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Roland McGrath6d760132007-10-16 23:26:40 -07002305 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
2307 /* Get rid of temporary copy */
2308 vfree(hdr);
2309
Heiko Carstens9e018922008-12-22 12:36:31 +01002310 stop_machine_destroy();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 /* Done! */
2312 return mod;
2313
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002314 unlink:
Rusty Russell9b1a4d32008-07-28 12:16:30 -05002315 stop_machine(__unlink_module, mod, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 module_arch_cleanup(mod);
2317 cleanup:
Kay Sievers97c146e2007-11-29 23:46:11 +01002318 kobject_del(&mod->mkobj.kobj);
2319 kobject_put(&mod->mkobj.kobj);
Steven Rostedtfed19392008-08-14 22:47:19 -04002320 ftrace_release(mod->module_core, mod->core_size);
Kay Sievers97c146e2007-11-29 23:46:11 +01002321 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 module_unload_free(mod);
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002323 free_init:
Eric Dumazet720eba32009-02-03 13:31:36 +10302324#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2325 percpu_modfree(mod->refptr);
2326#endif
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002327 module_free(mod, mod->module_init);
2328 free_core:
2329 module_free(mod, mod->module_core);
2330 /* mod will be freed with core. Don't access it beyond this line! */
2331 free_percpu:
2332 if (percpu)
2333 percpu_modfree(percpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 free_mod:
2335 kfree(args);
2336 free_hdr:
2337 vfree(hdr);
Heiko Carstens9e018922008-12-22 12:36:31 +01002338 stop_machine_destroy();
Jayachandran C6fe2e702006-01-06 00:19:54 -08002339 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
2341 truncated:
2342 printk(KERN_ERR "Module len %lu truncated\n", len);
2343 err = -ENOEXEC;
2344 goto free_hdr;
2345}
2346
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347/* This is where the real work happens */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002348SYSCALL_DEFINE3(init_module, void __user *, umod,
2349 unsigned long, len, const char __user *, uargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
2351 struct module *mod;
2352 int ret = 0;
2353
2354 /* Must have permission */
Kees Cook3d433212009-04-02 15:49:29 -07002355 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 return -EPERM;
2357
2358 /* Only one module load at a time, please */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002359 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 return -EINTR;
2361
2362 /* Do all the hard work */
2363 mod = load_module(umod, len, uargs);
2364 if (IS_ERR(mod)) {
Ashutosh Naik6389a382006-03-23 03:00:46 -08002365 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 return PTR_ERR(mod);
2367 }
2368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 /* Drop lock so they can recurse */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002370 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Alan Sterne041c682006-03-27 01:16:30 -08002372 blocking_notifier_call_chain(&module_notify_list,
2373 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
2375 /* Start the module */
2376 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002377 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 if (ret < 0) {
2379 /* Init routine failed: abort. Try to protect us from
2380 buggy refcounters. */
2381 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002382 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002383 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002384 blocking_notifier_call_chain(&module_notify_list,
2385 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002386 mutex_lock(&module_mutex);
2387 free_module(mod);
2388 mutex_unlock(&module_mutex);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002389 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 return ret;
2391 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002392 if (ret > 0) {
2393 printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
2394 "it should follow 0/-E convention\n"
2395 KERN_WARNING "%s: loading module anyway...\n",
2396 __func__, mod->name, ret,
2397 __func__);
2398 dump_stack();
2399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Rusty Russell6c5db222008-03-10 11:43:52 -07002401 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002403 wake_up(&module_wq);
Masami Hiramatsu0deddf432009-01-06 14:41:54 -08002404 blocking_notifier_call_chain(&module_notify_list,
2405 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07002406
2407 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 /* Drop initial reference. */
2409 module_put(mod);
2410 module_free(mod, mod->module_init);
2411 mod->module_init = NULL;
2412 mod->init_size = 0;
2413 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002414 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
2416 return 0;
2417}
2418
2419static inline int within(unsigned long addr, void *start, unsigned long size)
2420{
2421 return ((void *)addr >= start && (void *)addr < start + size);
2422}
2423
2424#ifdef CONFIG_KALLSYMS
2425/*
2426 * This ignores the intensely annoying "mapping symbols" found
2427 * in ARM ELF files: $a, $t and $d.
2428 */
2429static inline int is_arm_mapping_symbol(const char *str)
2430{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002431 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 && (str[2] == '\0' || str[2] == '.');
2433}
2434
2435static const char *get_ksymbol(struct module *mod,
2436 unsigned long addr,
2437 unsigned long *size,
2438 unsigned long *offset)
2439{
2440 unsigned int i, best = 0;
2441 unsigned long nextval;
2442
2443 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002444 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002446 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2448
2449 /* Scan for closest preceeding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002450 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 for (i = 1; i < mod->num_symtab; i++) {
2452 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2453 continue;
2454
2455 /* We ignore unnamed symbols: they're uninformative
2456 * and inserted at a whim. */
2457 if (mod->symtab[i].st_value <= addr
2458 && mod->symtab[i].st_value > mod->symtab[best].st_value
2459 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2460 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2461 best = i;
2462 if (mod->symtab[i].st_value > addr
2463 && mod->symtab[i].st_value < nextval
2464 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2465 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2466 nextval = mod->symtab[i].st_value;
2467 }
2468
2469 if (!best)
2470 return NULL;
2471
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002472 if (size)
2473 *size = nextval - mod->symtab[best].st_value;
2474 if (offset)
2475 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 return mod->strtab + mod->symtab[best].st_name;
2477}
2478
Rusty Russell6dd06c92008-01-29 17:13:22 -05002479/* For kallsyms to ask for address resolution. NULL means not found. Careful
2480 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002481const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05002482 unsigned long *size,
2483 unsigned long *offset,
2484 char **modname,
2485 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486{
2487 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002488 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Rusty Russellcb2a5202008-01-14 00:55:03 -08002490 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002491 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002492 if (within_module_init(addr, mod) ||
2493 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002494 if (modname)
2495 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002496 ret = get_ksymbol(mod, addr, size, offset);
2497 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 }
2499 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05002500 /* Make a copy in here where it's safe */
2501 if (ret) {
2502 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2503 ret = namebuf;
2504 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002505 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002506 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507}
2508
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002509int lookup_module_symbol_name(unsigned long addr, char *symname)
2510{
2511 struct module *mod;
2512
Rusty Russellcb2a5202008-01-14 00:55:03 -08002513 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002514 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002515 if (within_module_init(addr, mod) ||
2516 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002517 const char *sym;
2518
2519 sym = get_ksymbol(mod, addr, NULL, NULL);
2520 if (!sym)
2521 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07002522 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002523 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002524 return 0;
2525 }
2526 }
2527out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002528 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002529 return -ERANGE;
2530}
2531
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002532int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2533 unsigned long *offset, char *modname, char *name)
2534{
2535 struct module *mod;
2536
Rusty Russellcb2a5202008-01-14 00:55:03 -08002537 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002538 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002539 if (within_module_init(addr, mod) ||
2540 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002541 const char *sym;
2542
2543 sym = get_ksymbol(mod, addr, size, offset);
2544 if (!sym)
2545 goto out;
2546 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07002547 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002548 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07002549 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002550 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002551 return 0;
2552 }
2553 }
2554out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002555 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002556 return -ERANGE;
2557}
2558
Alexey Dobriyanea078902007-05-08 00:28:39 -07002559int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2560 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561{
2562 struct module *mod;
2563
Rusty Russellcb2a5202008-01-14 00:55:03 -08002564 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002565 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 if (symnum < mod->num_symtab) {
2567 *value = mod->symtab[symnum].st_value;
2568 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002569 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07002570 KSYM_NAME_LEN);
2571 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06002572 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002573 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002574 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 }
2576 symnum -= mod->num_symtab;
2577 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002578 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002579 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580}
2581
2582static unsigned long mod_find_symname(struct module *mod, const char *name)
2583{
2584 unsigned int i;
2585
2586 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002587 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2588 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 return mod->symtab[i].st_value;
2590 return 0;
2591}
2592
2593/* Look for this name: can be of form module:name. */
2594unsigned long module_kallsyms_lookup_name(const char *name)
2595{
2596 struct module *mod;
2597 char *colon;
2598 unsigned long ret = 0;
2599
2600 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08002601 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 if ((colon = strchr(name, ':')) != NULL) {
2603 *colon = '\0';
2604 if ((mod = find_module(name)) != NULL)
2605 ret = mod_find_symname(mod, colon+1);
2606 *colon = ':';
2607 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02002608 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 if ((ret = mod_find_symname(mod, name)) != 0)
2610 break;
2611 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002612 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 return ret;
2614}
2615#endif /* CONFIG_KALLSYMS */
2616
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002617static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002618{
2619 int bx = 0;
2620
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002621 if (mod->taints ||
2622 mod->state == MODULE_STATE_GOING ||
2623 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002624 buf[bx++] = '(';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002625 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002626 buf[bx++] = 'P';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002627 if (mod->taints & (1 << TAINT_FORCED_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002628 buf[bx++] = 'F';
Linus Torvalds26e9a392008-10-17 09:50:12 -07002629 if (mod->taints & (1 << TAINT_CRAP))
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002630 buf[bx++] = 'C';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002631 /*
2632 * TAINT_FORCED_RMMOD: could be added.
2633 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2634 * apply to modules.
2635 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002636
2637 /* Show a - for module-is-being-unloaded */
2638 if (mod->state == MODULE_STATE_GOING)
2639 buf[bx++] = '-';
2640 /* Show a + for module-is-being-loaded */
2641 if (mod->state == MODULE_STATE_COMING)
2642 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002643 buf[bx++] = ')';
2644 }
2645 buf[bx] = '\0';
2646
2647 return buf;
2648}
2649
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002650#ifdef CONFIG_PROC_FS
2651/* Called by the /proc file system to return a list of modules. */
2652static void *m_start(struct seq_file *m, loff_t *pos)
2653{
2654 mutex_lock(&module_mutex);
2655 return seq_list_start(&modules, *pos);
2656}
2657
2658static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2659{
2660 return seq_list_next(p, &modules, pos);
2661}
2662
2663static void m_stop(struct seq_file *m, void *p)
2664{
2665 mutex_unlock(&module_mutex);
2666}
2667
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668static int m_show(struct seq_file *m, void *p)
2669{
2670 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002671 char buf[8];
2672
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05002673 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 mod->name, mod->init_size + mod->core_size);
2675 print_unload_info(m, mod);
2676
2677 /* Informative for users. */
2678 seq_printf(m, " %s",
2679 mod->state == MODULE_STATE_GOING ? "Unloading":
2680 mod->state == MODULE_STATE_COMING ? "Loading":
2681 "Live");
2682 /* Used by oprofile and other similar tools. */
2683 seq_printf(m, " 0x%p", mod->module_core);
2684
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002685 /* Taints info */
2686 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002687 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002688
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 seq_printf(m, "\n");
2690 return 0;
2691}
2692
2693/* Format: modulename size refcount deps address
2694
2695 Where refcount is a number or -, and deps is a comma-separated list
2696 of depends or -.
2697*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002698static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 .start = m_start,
2700 .next = m_next,
2701 .stop = m_stop,
2702 .show = m_show
2703};
2704
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002705static int modules_open(struct inode *inode, struct file *file)
2706{
2707 return seq_open(file, &modules_op);
2708}
2709
2710static const struct file_operations proc_modules_operations = {
2711 .open = modules_open,
2712 .read = seq_read,
2713 .llseek = seq_lseek,
2714 .release = seq_release,
2715};
2716
2717static int __init proc_modules_init(void)
2718{
2719 proc_create("modules", 0, NULL, &proc_modules_operations);
2720 return 0;
2721}
2722module_init(proc_modules_init);
2723#endif
2724
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725/* Given an address, look for it in the module exception tables. */
2726const struct exception_table_entry *search_module_extables(unsigned long addr)
2727{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 const struct exception_table_entry *e = NULL;
2729 struct module *mod;
2730
Rusty Russell24da1cb2007-07-15 23:41:46 -07002731 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002732 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 if (mod->num_exentries == 0)
2734 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002735
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 e = search_extable(mod->extable,
2737 mod->extable + mod->num_exentries - 1,
2738 addr);
2739 if (e)
2740 break;
2741 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07002742 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
2744 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07002745 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 return e;
2747}
2748
Ingo Molnar4d435f92006-07-03 00:24:24 -07002749/*
2750 * Is this a valid module address?
2751 */
2752int is_module_address(unsigned long addr)
2753{
Ingo Molnar4d435f92006-07-03 00:24:24 -07002754 struct module *mod;
2755
Rusty Russell24da1cb2007-07-15 23:41:46 -07002756 preempt_disable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002757
Andi Kleend72b3752008-08-30 10:09:00 +02002758 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002759 if (within_module_core(addr, mod)) {
Rusty Russell24da1cb2007-07-15 23:41:46 -07002760 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002761 return 1;
2762 }
2763 }
2764
Rusty Russell24da1cb2007-07-15 23:41:46 -07002765 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002766
2767 return 0;
2768}
2769
2770
Rusty Russell24da1cb2007-07-15 23:41:46 -07002771/* Is this a valid kernel address? */
Frederic Weisbecker8b96f012008-12-06 03:40:00 +01002772__notrace_funcgraph struct module *__module_text_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
2774 struct module *mod;
2775
Rusty Russell3a642e92008-07-22 19:24:28 -05002776 if (addr < module_addr_min || addr > module_addr_max)
2777 return NULL;
2778
Andi Kleend72b3752008-08-30 10:09:00 +02002779 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 if (within(addr, mod->module_init, mod->init_text_size)
2781 || within(addr, mod->module_core, mod->core_text_size))
2782 return mod;
2783 return NULL;
2784}
2785
2786struct module *module_text_address(unsigned long addr)
2787{
2788 struct module *mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Rusty Russell24da1cb2007-07-15 23:41:46 -07002790 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 mod = __module_text_address(addr);
Rusty Russell24da1cb2007-07-15 23:41:46 -07002792 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
2794 return mod;
2795}
2796
2797/* Don't grab lock, we're oopsing. */
2798void print_modules(void)
2799{
2800 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07002801 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
2803 printk("Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02002804 /* Most callers should already have preempt disabled, but make sure */
2805 preempt_disable();
2806 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002807 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02002808 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01002809 if (last_unloaded_module[0])
2810 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 printk("\n");
2812}
2813
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814#ifdef CONFIG_MODVERSIONS
2815/* Generate the signature for struct module here, too, for modversions. */
2816void struct_module(struct module *mod) { return; }
2817EXPORT_SYMBOL(struct_module);
2818#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002819
2820#ifdef CONFIG_MARKERS
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002821void module_update_markers(void)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002822{
2823 struct module *mod;
2824
2825 mutex_lock(&module_mutex);
2826 list_for_each_entry(mod, &modules, list)
2827 if (!mod->taints)
2828 marker_update_probe_range(mod->markers,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002829 mod->markers + mod->num_markers);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002830 mutex_unlock(&module_mutex);
2831}
2832#endif
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002833
2834#ifdef CONFIG_TRACEPOINTS
2835void module_update_tracepoints(void)
2836{
2837 struct module *mod;
2838
2839 mutex_lock(&module_mutex);
2840 list_for_each_entry(mod, &modules, list)
2841 if (!mod->taints)
2842 tracepoint_update_probe_range(mod->tracepoints,
2843 mod->tracepoints + mod->num_tracepoints);
2844 mutex_unlock(&module_mutex);
2845}
2846
2847/*
2848 * Returns 0 if current not found.
2849 * Returns 1 if current found.
2850 */
2851int module_get_iter_tracepoints(struct tracepoint_iter *iter)
2852{
2853 struct module *iter_mod;
2854 int found = 0;
2855
2856 mutex_lock(&module_mutex);
2857 list_for_each_entry(iter_mod, &modules, list) {
2858 if (!iter_mod->taints) {
2859 /*
2860 * Sorted module list
2861 */
2862 if (iter_mod < iter->module)
2863 continue;
2864 else if (iter_mod > iter->module)
2865 iter->tracepoint = NULL;
2866 found = tracepoint_get_iter_range(&iter->tracepoint,
2867 iter_mod->tracepoints,
2868 iter_mod->tracepoints
2869 + iter_mod->num_tracepoints);
2870 if (found) {
2871 iter->module = iter_mod;
2872 break;
2873 }
2874 }
2875 }
2876 mutex_unlock(&module_mutex);
2877 return found;
2878}
2879#endif