blob: 6fded84d702936789f74443b7ac28caeaa827945 [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>
Jan Beulich4552d5d2006-06-26 13:57:28 +020046#include <linux/unwind.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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#if 0
55#define DEBUGP printk
56#else
57#define DEBUGP(fmt , a...)
58#endif
59
60#ifndef ARCH_SHF_SMALL
61#define ARCH_SHF_SMALL 0
62#endif
63
64/* If this is set, the section belongs in the init part of the module */
65#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
66
Rusty Russell24da1cb2007-07-15 23:41:46 -070067/* List of modules, protected by module_mutex or preempt_disable
68 * (add/delete uses stop_machine). */
Ashutosh Naik6389a382006-03-23 03:00:46 -080069static DEFINE_MUTEX(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static LIST_HEAD(modules);
71
Rusty Russellc9a3ba52008-01-29 17:13:18 -050072/* Waiting for a module to finish initializing? */
73static DECLARE_WAIT_QUEUE_HEAD(module_wq);
74
Alan Sterne041c682006-03-27 01:16:30 -080075static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Rusty Russell3a642e92008-07-22 19:24:28 -050077/* Bounds of module allocation, for speeding __module_text_address */
78static unsigned long module_addr_min = -1UL, module_addr_max = 0;
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080int register_module_notifier(struct notifier_block * nb)
81{
Alan Sterne041c682006-03-27 01:16:30 -080082 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84EXPORT_SYMBOL(register_module_notifier);
85
86int unregister_module_notifier(struct notifier_block * nb)
87{
Alan Sterne041c682006-03-27 01:16:30 -080088 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90EXPORT_SYMBOL(unregister_module_notifier);
91
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -080092/* We require a truly strong try_module_get(): 0 means failure due to
93 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static inline int strong_try_module_get(struct module *mod)
95{
96 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -050097 return -EBUSY;
98 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500100 else
101 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700104static inline void add_taint_module(struct module *mod, unsigned flag)
105{
106 add_taint(flag);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700107 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700108}
109
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200110/*
111 * A thread that wants to hold a reference to a module only while it
112 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 */
114void __module_put_and_exit(struct module *mod, long code)
115{
116 module_put(mod);
117 do_exit(code);
118}
119EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121/* Find a module section: 0 means not found. */
122static unsigned int find_sec(Elf_Ehdr *hdr,
123 Elf_Shdr *sechdrs,
124 const char *secstrings,
125 const char *name)
126{
127 unsigned int i;
128
129 for (i = 1; i < hdr->e_shnum; i++)
130 /* Alloc bit cleared means "ignore it." */
131 if ((sechdrs[i].sh_flags & SHF_ALLOC)
132 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
133 return i;
134 return 0;
135}
136
137/* Provided by the linker */
138extern const struct kernel_symbol __start___ksymtab[];
139extern const struct kernel_symbol __stop___ksymtab[];
140extern const struct kernel_symbol __start___ksymtab_gpl[];
141extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800142extern const struct kernel_symbol __start___ksymtab_gpl_future[];
143extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700144extern const struct kernel_symbol __start___ksymtab_gpl_future[];
145extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146extern const unsigned long __start___kcrctab[];
147extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800148extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500149#ifdef CONFIG_UNUSED_SYMBOLS
150extern const struct kernel_symbol __start___ksymtab_unused[];
151extern const struct kernel_symbol __stop___ksymtab_unused[];
152extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
153extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700154extern const unsigned long __start___kcrctab_unused[];
155extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500156#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158#ifndef CONFIG_MODVERSIONS
159#define symversion(base, idx) NULL
160#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800161#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#endif
163
Rusty Russelldafd0942008-07-22 19:24:25 -0500164struct symsearch {
165 const struct kernel_symbol *start, *stop;
166 const unsigned long *crcs;
167 enum {
168 NOT_GPL_ONLY,
169 GPL_ONLY,
170 WILL_BE_GPL_ONLY,
171 } licence;
172 bool unused;
173};
174
175static bool each_symbol_in_section(const struct symsearch *arr,
176 unsigned int arrsize,
177 struct module *owner,
178 bool (*fn)(const struct symsearch *syms,
179 struct module *owner,
180 unsigned int symnum, void *data),
181 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100182{
Rusty Russelldafd0942008-07-22 19:24:25 -0500183 unsigned int i, j;
184
185 for (j = 0; j < arrsize; j++) {
186 for (i = 0; i < arr[j].stop - arr[j].start; i++)
187 if (fn(&arr[j], owner, i, data))
188 return true;
189 }
190
191 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100192}
193
Rusty Russelldafd0942008-07-22 19:24:25 -0500194/* Returns true as soon as fn returns true, otherwise false. */
195static bool each_symbol(bool (*fn)(const struct symsearch *arr,
196 struct module *owner,
197 unsigned int symnum, void *data),
198 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700199{
Rusty Russelldafd0942008-07-22 19:24:25 -0500200 struct module *mod;
201 const struct symsearch arr[] = {
202 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
203 NOT_GPL_ONLY, false },
204 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
205 __start___kcrctab_gpl,
206 GPL_ONLY, false },
207 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
208 __start___kcrctab_gpl_future,
209 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500210#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500211 { __start___ksymtab_unused, __stop___ksymtab_unused,
212 __start___kcrctab_unused,
213 NOT_GPL_ONLY, true },
214 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
215 __start___kcrctab_unused_gpl,
216 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500217#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500218 };
219
220 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
221 return true;
222
223 list_for_each_entry(mod, &modules, list) {
224 struct symsearch arr[] = {
225 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
226 NOT_GPL_ONLY, false },
227 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
228 mod->gpl_crcs,
229 GPL_ONLY, false },
230 { mod->gpl_future_syms,
231 mod->gpl_future_syms + mod->num_gpl_future_syms,
232 mod->gpl_future_crcs,
233 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500234#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500235 { mod->unused_syms,
236 mod->unused_syms + mod->num_unused_syms,
237 mod->unused_crcs,
238 NOT_GPL_ONLY, true },
239 { mod->unused_gpl_syms,
240 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
241 mod->unused_gpl_crcs,
242 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500243#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500244 };
245
246 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
247 return true;
248 }
249 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700250}
251
Rusty Russelldafd0942008-07-22 19:24:25 -0500252struct find_symbol_arg {
253 /* Input */
254 const char *name;
255 bool gplok;
256 bool warn;
257
258 /* Output */
259 struct module *owner;
260 const unsigned long *crc;
261 unsigned long value;
262};
263
264static bool find_symbol_in_section(const struct symsearch *syms,
265 struct module *owner,
266 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500267{
Rusty Russelldafd0942008-07-22 19:24:25 -0500268 struct find_symbol_arg *fsa = data;
269
270 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
271 return false;
272
273 if (!fsa->gplok) {
274 if (syms->licence == GPL_ONLY)
275 return false;
276 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
277 printk(KERN_WARNING "Symbol %s is being used "
278 "by a non-GPL module, which will not "
279 "be allowed in the future\n", fsa->name);
280 printk(KERN_WARNING "Please see the file "
281 "Documentation/feature-removal-schedule.txt "
282 "in the kernel source tree for more details.\n");
283 }
284 }
285
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500286#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500287 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500288 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500289 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500290 printk(KERN_WARNING
291 "This symbol will go away in the future.\n");
292 printk(KERN_WARNING
293 "Please evalute if this is the right api to use and if "
294 "it really is, submit a report the linux kernel "
295 "mailinglist together with submitting your code for "
296 "inclusion.\n");
297 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500298#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500299
300 fsa->owner = owner;
301 fsa->crc = symversion(syms->crcs, symnum);
302 fsa->value = syms->start[symnum].value;
Rusty Russellad9546c2008-05-01 21:14:59 -0500303 return true;
304}
305
Rusty Russellad9546c2008-05-01 21:14:59 -0500306/* Find a symbol, return value, (optional) crc and (optional) module
307 * which owns it */
308static unsigned long find_symbol(const char *name,
309 struct module **owner,
310 const unsigned long **crc,
311 bool gplok,
312 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Rusty Russelldafd0942008-07-22 19:24:25 -0500314 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Rusty Russelldafd0942008-07-22 19:24:25 -0500316 fsa.name = name;
317 fsa.gplok = gplok;
318 fsa.warn = warn;
319
320 if (each_symbol(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500321 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500322 *owner = fsa.owner;
323 if (crc)
324 *crc = fsa.crc;
325 return fsa.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 DEBUGP("Failed to find symbol %s\n", name);
Christoph Lameter88173502008-02-08 04:18:41 -0800329 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332/* Search for module by name: must hold module_mutex. */
333static struct module *find_module(const char *name)
334{
335 struct module *mod;
336
337 list_for_each_entry(mod, &modules, list) {
338 if (strcmp(mod->name, name) == 0)
339 return mod;
340 }
341 return NULL;
342}
343
344#ifdef CONFIG_SMP
345/* Number of blocks used and allocated. */
346static unsigned int pcpu_num_used, pcpu_num_allocated;
347/* Size of each block. -ve means used. */
348static int *pcpu_size;
349
350static int split_block(unsigned int i, unsigned short size)
351{
352 /* Reallocation required? */
353 if (pcpu_num_used + 1 > pcpu_num_allocated) {
Pekka Enberg6d4f9c52007-05-08 00:24:58 -0700354 int *new;
355
356 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
357 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (!new)
359 return 0;
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 pcpu_num_allocated *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 pcpu_size = new;
363 }
364
365 /* Insert a new subblock */
366 memmove(&pcpu_size[i+1], &pcpu_size[i],
367 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
368 pcpu_num_used++;
369
370 pcpu_size[i+1] -= size;
371 pcpu_size[i] = size;
372 return 1;
373}
374
375static inline unsigned int block_size(int val)
376{
377 if (val < 0)
378 return -val;
379 return val;
380}
381
Rusty Russell842bbaa2005-08-01 21:11:47 -0700382static void *percpu_modalloc(unsigned long size, unsigned long align,
383 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 unsigned long extra;
386 unsigned int i;
387 void *ptr;
388
Jeremy Fitzhardingeb6e35902007-05-02 19:27:12 +0200389 if (align > PAGE_SIZE) {
390 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
391 name, align, PAGE_SIZE);
392 align = PAGE_SIZE;
Rusty Russell842bbaa2005-08-01 21:11:47 -0700393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 ptr = __per_cpu_start;
396 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
397 /* Extra for alignment requirement. */
398 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
399 BUG_ON(i == 0 && extra != 0);
400
401 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
402 continue;
403
404 /* Transfer extra to previous block. */
405 if (pcpu_size[i-1] < 0)
406 pcpu_size[i-1] -= extra;
407 else
408 pcpu_size[i-1] += extra;
409 pcpu_size[i] -= extra;
410 ptr += extra;
411
412 /* Split block if warranted */
413 if (pcpu_size[i] - size > sizeof(unsigned long))
414 if (!split_block(i, size))
415 return NULL;
416
417 /* Mark allocated */
418 pcpu_size[i] = -pcpu_size[i];
419 return ptr;
420 }
421
422 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
423 size);
424 return NULL;
425}
426
427static void percpu_modfree(void *freeme)
428{
429 unsigned int i;
430 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
431
432 /* First entry is core kernel percpu data. */
433 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
434 if (ptr == freeme) {
435 pcpu_size[i] = -pcpu_size[i];
436 goto free;
437 }
438 }
439 BUG();
440
441 free:
442 /* Merge with previous? */
443 if (pcpu_size[i-1] >= 0) {
444 pcpu_size[i-1] += pcpu_size[i];
445 pcpu_num_used--;
446 memmove(&pcpu_size[i], &pcpu_size[i+1],
447 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
448 i--;
449 }
450 /* Merge with next? */
451 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
452 pcpu_size[i] += pcpu_size[i+1];
453 pcpu_num_used--;
454 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
455 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
456 }
457}
458
459static unsigned int find_pcpusec(Elf_Ehdr *hdr,
460 Elf_Shdr *sechdrs,
461 const char *secstrings)
462{
463 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
464}
465
Mike Travisdd5af902008-01-30 13:33:32 +0100466static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
467{
468 int cpu;
469
470 for_each_possible_cpu(cpu)
471 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
472}
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474static int percpu_modinit(void)
475{
476 pcpu_num_used = 2;
477 pcpu_num_allocated = 2;
478 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
479 GFP_KERNEL);
480 /* Static in-kernel percpu data (used). */
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +0200481 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 /* Free room. */
483 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
484 if (pcpu_size[1] < 0) {
485 printk(KERN_ERR "No per-cpu room for modules.\n");
486 pcpu_num_used = 1;
487 }
488
489 return 0;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700490}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491__initcall(percpu_modinit);
492#else /* ... !CONFIG_SMP */
Rusty Russell842bbaa2005-08-01 21:11:47 -0700493static inline void *percpu_modalloc(unsigned long size, unsigned long align,
494 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 return NULL;
497}
498static inline void percpu_modfree(void *pcpuptr)
499{
500 BUG();
501}
502static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
503 Elf_Shdr *sechdrs,
504 const char *secstrings)
505{
506 return 0;
507}
508static inline void percpu_modcopy(void *pcpudst, const void *src,
509 unsigned long size)
510{
511 /* pcpusec should be 0, and size of that section should be 0. */
512 BUG_ON(size != 0);
513}
514#endif /* CONFIG_SMP */
515
Matt Domschc988d2b2005-06-23 22:05:15 -0700516#define MODINFO_ATTR(field) \
517static void setup_modinfo_##field(struct module *mod, const char *s) \
518{ \
519 mod->field = kstrdup(s, GFP_KERNEL); \
520} \
521static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
522 struct module *mod, char *buffer) \
523{ \
524 return sprintf(buffer, "%s\n", mod->field); \
525} \
526static int modinfo_##field##_exists(struct module *mod) \
527{ \
528 return mod->field != NULL; \
529} \
530static void free_modinfo_##field(struct module *mod) \
531{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700532 kfree(mod->field); \
533 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700534} \
535static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900536 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700537 .show = show_modinfo_##field, \
538 .setup = setup_modinfo_##field, \
539 .test = modinfo_##field##_exists, \
540 .free = free_modinfo_##field, \
541};
542
543MODINFO_ATTR(version);
544MODINFO_ATTR(srcversion);
545
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100546static char last_unloaded_module[MODULE_NAME_LEN+1];
547
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800548#ifdef CONFIG_MODULE_UNLOAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549/* Init the unload section of the module. */
550static void module_unload_init(struct module *mod)
551{
552 unsigned int i;
553
554 INIT_LIST_HEAD(&mod->modules_which_use_me);
555 for (i = 0; i < NR_CPUS; i++)
556 local_set(&mod->ref[i].count, 0);
557 /* Hold reference count during initialization. */
Ingo Molnar39c715b2005-06-21 17:14:34 -0700558 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 /* Backwards compatibility macros put refcount during init. */
560 mod->waiter = current;
561}
562
563/* modules using other modules */
564struct module_use
565{
566 struct list_head list;
567 struct module *module_which_uses;
568};
569
570/* Does a already use b? */
571static int already_uses(struct module *a, struct module *b)
572{
573 struct module_use *use;
574
575 list_for_each_entry(use, &b->modules_which_use_me, list) {
576 if (use->module_which_uses == a) {
577 DEBUGP("%s uses %s!\n", a->name, b->name);
578 return 1;
579 }
580 }
581 DEBUGP("%s does not use %s!\n", a->name, b->name);
582 return 0;
583}
584
585/* Module a uses b */
586static int use_module(struct module *a, struct module *b)
587{
588 struct module_use *use;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500589 int no_warn, err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (b == NULL || already_uses(a, b)) return 1;
592
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500593 /* If we're interrupted or time out, we fail. */
594 if (wait_event_interruptible_timeout(
595 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
596 30 * HZ) <= 0) {
597 printk("%s: gave up waiting for init of module %s.\n",
598 a->name, b->name);
599 return 0;
600 }
601
602 /* If strong_try_module_get() returned a different error, we fail. */
603 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return 0;
605
606 DEBUGP("Allocating new usage for %s.\n", a->name);
607 use = kmalloc(sizeof(*use), GFP_ATOMIC);
608 if (!use) {
609 printk("%s: out of memory loading\n", a->name);
610 module_put(b);
611 return 0;
612 }
613
614 use->module_which_uses = a;
615 list_add(&use->list, &b->modules_which_use_me);
Kay Sievers270a6c42007-01-18 13:26:15 +0100616 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return 1;
618}
619
620/* Clear the unload stuff of the module. */
621static void module_unload_free(struct module *mod)
622{
623 struct module *i;
624
625 list_for_each_entry(i, &modules, list) {
626 struct module_use *use;
627
628 list_for_each_entry(use, &i->modules_which_use_me, list) {
629 if (use->module_which_uses == mod) {
630 DEBUGP("%s unusing %s\n", mod->name, i->name);
631 module_put(i);
632 list_del(&use->list);
633 kfree(use);
Kay Sievers270a6c42007-01-18 13:26:15 +0100634 sysfs_remove_link(i->holders_dir, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /* There can be at most one match. */
636 break;
637 }
638 }
639 }
640}
641
642#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800643static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
645 int ret = (flags & O_TRUNC);
646 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800647 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return ret;
649}
650#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800651static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 return 0;
654}
655#endif /* CONFIG_MODULE_FORCE_UNLOAD */
656
657struct stopref
658{
659 struct module *mod;
660 int flags;
661 int *forced;
662};
663
664/* Whole machine is stopped with interrupts off when this runs. */
665static int __try_stop_module(void *_sref)
666{
667 struct stopref *sref = _sref;
668
Rusty Russellda39ba52008-07-22 19:24:25 -0500669 /* If it's not unused, quit unless we're forcing. */
670 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800671 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return -EWOULDBLOCK;
673 }
674
675 /* Mark it as dying. */
676 sref->mod->state = MODULE_STATE_GOING;
677 return 0;
678}
679
680static int try_stop_module(struct module *mod, int flags, int *forced)
681{
Rusty Russellda39ba52008-07-22 19:24:25 -0500682 if (flags & O_NONBLOCK) {
683 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500685 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500686 } else {
687 /* We don't need to stop the machine for this. */
688 mod->state = MODULE_STATE_GOING;
689 synchronize_sched();
690 return 0;
691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
694unsigned int module_refcount(struct module *mod)
695{
696 unsigned int i, total = 0;
697
698 for (i = 0; i < NR_CPUS; i++)
699 total += local_read(&mod->ref[i].count);
700 return total;
701}
702EXPORT_SYMBOL(module_refcount);
703
704/* This exists whether we can unload or not */
705static void free_module(struct module *mod);
706
707static void wait_for_zero_refcount(struct module *mod)
708{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500709 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800710 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 for (;;) {
712 DEBUGP("Looking at refcount...\n");
713 set_current_state(TASK_UNINTERRUPTIBLE);
714 if (module_refcount(mod) == 0)
715 break;
716 schedule();
717 }
718 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800719 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800722asmlinkage long
723sys_delete_module(const char __user *name_user, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
725 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800726 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 int ret, forced = 0;
728
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800729 if (!capable(CAP_SYS_MODULE))
730 return -EPERM;
731
732 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
733 return -EFAULT;
734 name[MODULE_NAME_LEN-1] = '\0';
735
Ashutosh Naik6389a382006-03-23 03:00:46 -0800736 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return -EINTR;
738
739 mod = find_module(name);
740 if (!mod) {
741 ret = -ENOENT;
742 goto out;
743 }
744
745 if (!list_empty(&mod->modules_which_use_me)) {
746 /* Other modules depend on us: get rid of them first. */
747 ret = -EWOULDBLOCK;
748 goto out;
749 }
750
751 /* Doing init or already dying? */
752 if (mod->state != MODULE_STATE_LIVE) {
753 /* FIXME: if (force), slam module count and wake up
754 waiter --RR */
755 DEBUGP("%s already dying\n", mod->name);
756 ret = -EBUSY;
757 goto out;
758 }
759
760 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700761 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800762 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (!forced) {
764 /* This module can't be removed */
765 ret = -EBUSY;
766 goto out;
767 }
768 }
769
770 /* Set this up before setting mod->state */
771 mod->waiter = current;
772
773 /* Stop the machine so refcounts can't move and disable module. */
774 ret = try_stop_module(mod, flags, &forced);
775 if (ret != 0)
776 goto out;
777
778 /* Never wait if forced. */
779 if (!forced && module_refcount(mod) != 0)
780 wait_for_zero_refcount(mod);
781
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200782 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 /* Final destruction now noone is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200784 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200786 blocking_notifier_call_chain(&module_notify_list,
787 MODULE_STATE_GOING, mod);
788 mutex_lock(&module_mutex);
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100789 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500790 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Jason Baron346e15b2008-08-12 16:46:19 -0400791 unregister_dynamic_debug_module(mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 free_module(mod);
793
794 out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800795 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return ret;
797}
798
799static void print_unload_info(struct seq_file *m, struct module *mod)
800{
801 struct module_use *use;
802 int printed_something = 0;
803
804 seq_printf(m, " %u ", module_refcount(mod));
805
806 /* Always include a trailing , so userspace can differentiate
807 between this and the old multi-field proc format. */
808 list_for_each_entry(use, &mod->modules_which_use_me, list) {
809 printed_something = 1;
810 seq_printf(m, "%s,", use->module_which_uses->name);
811 }
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (mod->init != NULL && mod->exit == NULL) {
814 printed_something = 1;
815 seq_printf(m, "[permanent],");
816 }
817
818 if (!printed_something)
819 seq_printf(m, "-");
820}
821
822void __symbol_put(const char *symbol)
823{
824 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Rusty Russell24da1cb2007-07-15 23:41:46 -0700826 preempt_disable();
Rusty Russellad9546c2008-05-01 21:14:59 -0500827 if (IS_ERR_VALUE(find_symbol(symbol, &owner, NULL, true, false)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 BUG();
829 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700830 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832EXPORT_SYMBOL(__symbol_put);
833
834void symbol_put_addr(void *addr)
835{
Trent Piepho5e376612006-05-15 09:44:06 -0700836 struct module *modaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Trent Piepho5e376612006-05-15 09:44:06 -0700838 if (core_kernel_text((unsigned long)addr))
839 return;
840
841 if (!(modaddr = module_text_address((unsigned long)addr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 BUG();
Trent Piepho5e376612006-05-15 09:44:06 -0700843 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845EXPORT_SYMBOL_GPL(symbol_put_addr);
846
847static ssize_t show_refcnt(struct module_attribute *mattr,
848 struct module *mod, char *buffer)
849{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400850 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
853static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900854 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 .show = show_refcnt,
856};
857
Al Virof6a57032006-10-18 01:47:25 -0400858void module_put(struct module *module)
859{
860 if (module) {
861 unsigned int cpu = get_cpu();
862 local_dec(&module->ref[cpu].count);
863 /* Maybe they're waiting for us to drop reference? */
864 if (unlikely(!module_is_live(module)))
865 wake_up_process(module->waiter);
866 put_cpu();
867 }
868}
869EXPORT_SYMBOL(module_put);
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871#else /* !CONFIG_MODULE_UNLOAD */
872static void print_unload_info(struct seq_file *m, struct module *mod)
873{
874 /* We don't know the usage count, or what modules are using. */
875 seq_printf(m, " - -");
876}
877
878static inline void module_unload_free(struct module *mod)
879{
880}
881
882static inline int use_module(struct module *a, struct module *b)
883{
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500884 return strong_try_module_get(b) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886
887static inline void module_unload_init(struct module *mod)
888{
889}
890#endif /* CONFIG_MODULE_UNLOAD */
891
Kay Sievers1f717402006-11-24 12:15:25 +0100892static ssize_t show_initstate(struct module_attribute *mattr,
893 struct module *mod, char *buffer)
894{
895 const char *state = "unknown";
896
897 switch (mod->state) {
898 case MODULE_STATE_LIVE:
899 state = "live";
900 break;
901 case MODULE_STATE_COMING:
902 state = "coming";
903 break;
904 case MODULE_STATE_GOING:
905 state = "going";
906 break;
907 }
908 return sprintf(buffer, "%s\n", state);
909}
910
911static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900912 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100913 .show = show_initstate,
914};
915
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800916static struct module_attribute *modinfo_attrs[] = {
917 &modinfo_version,
918 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100919 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800920#ifdef CONFIG_MODULE_UNLOAD
921 &refcnt,
922#endif
923 NULL,
924};
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926static const char vermagic[] = VERMAGIC_STRING;
927
Linus Torvalds826e4502008-05-04 17:04:16 -0700928static int try_to_force_load(struct module *mod, const char *symname)
929{
930#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -0700931 if (!test_taint(TAINT_FORCED_MODULE))
Linus Torvalds826e4502008-05-04 17:04:16 -0700932 printk("%s: no version for \"%s\" found: kernel tainted.\n",
933 mod->name, symname);
934 add_taint_module(mod, TAINT_FORCED_MODULE);
935 return 0;
936#else
937 return -ENOEXEC;
938#endif
939}
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941#ifdef CONFIG_MODVERSIONS
942static int check_version(Elf_Shdr *sechdrs,
943 unsigned int versindex,
944 const char *symname,
945 struct module *mod,
946 const unsigned long *crc)
947{
948 unsigned int i, num_versions;
949 struct modversion_info *versions;
950
951 /* Exporting module didn't supply crcs? OK, we're already tainted. */
952 if (!crc)
953 return 1;
954
Rusty Russella5dd6972008-05-09 16:24:21 +1000955 /* No versions at all? modprobe --force does this. */
956 if (versindex == 0)
957 return try_to_force_load(mod, symname) == 0;
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 versions = (void *) sechdrs[versindex].sh_addr;
960 num_versions = sechdrs[versindex].sh_size
961 / sizeof(struct modversion_info);
962
963 for (i = 0; i < num_versions; i++) {
964 if (strcmp(versions[i].name, symname) != 0)
965 continue;
966
967 if (versions[i].crc == *crc)
968 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 DEBUGP("Found checksum %lX vs module %lX\n",
970 *crc, versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -0700971 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
Linus Torvalds826e4502008-05-04 17:04:16 -0700973
Rusty Russella5dd6972008-05-09 16:24:21 +1000974 printk(KERN_WARNING "%s: no symbol version for %s\n",
975 mod->name, symname);
976 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -0700977
978bad_version:
979 printk("%s: disagrees about version of symbol %s\n",
980 mod->name, symname);
981 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
984static inline int check_modstruct_version(Elf_Shdr *sechdrs,
985 unsigned int versindex,
986 struct module *mod)
987{
988 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Rusty Russellad9546c2008-05-01 21:14:59 -0500990 if (IS_ERR_VALUE(find_symbol("struct_module", NULL, &crc, true, false)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 BUG();
Rusty Russellad9546c2008-05-01 21:14:59 -0500992 return check_version(sechdrs, versindex, "struct_module", mod, crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
994
Rusty Russell91e37a72008-05-09 16:25:28 +1000995/* First part is kernel version, which we ignore if module has crcs. */
996static inline int same_magic(const char *amagic, const char *bmagic,
997 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Rusty Russell91e37a72008-05-09 16:25:28 +1000999 if (has_crcs) {
1000 amagic += strcspn(amagic, " ");
1001 bmagic += strcspn(bmagic, " ");
1002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return strcmp(amagic, bmagic) == 0;
1004}
1005#else
1006static inline int check_version(Elf_Shdr *sechdrs,
1007 unsigned int versindex,
1008 const char *symname,
1009 struct module *mod,
1010 const unsigned long *crc)
1011{
1012 return 1;
1013}
1014
1015static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1016 unsigned int versindex,
1017 struct module *mod)
1018{
1019 return 1;
1020}
1021
Rusty Russell91e37a72008-05-09 16:25:28 +10001022static inline int same_magic(const char *amagic, const char *bmagic,
1023 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025 return strcmp(amagic, bmagic) == 0;
1026}
1027#endif /* CONFIG_MODVERSIONS */
1028
1029/* Resolve a symbol for this module. I.e. if we find one, record usage.
1030 Must be holding module_mutex. */
1031static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
1032 unsigned int versindex,
1033 const char *name,
1034 struct module *mod)
1035{
1036 struct module *owner;
1037 unsigned long ret;
1038 const unsigned long *crc;
1039
Rusty Russellad9546c2008-05-01 21:14:59 -05001040 ret = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001041 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Christoph Lameter88173502008-02-08 04:18:41 -08001042 if (!IS_ERR_VALUE(ret)) {
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001043 /* use_module can fail due to OOM,
1044 or module initialization or unloading */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1046 !use_module(mod, owner))
Christoph Lameter88173502008-02-08 04:18:41 -08001047 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return ret;
1050}
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052/*
1053 * /sys/module/foo/sections stuff
1054 * J. Corbet <corbet@lwn.net>
1055 */
Kay Sievers120fc3d2008-02-21 00:33:20 +01001056#if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
Rusty Russella58730c2008-03-13 09:03:44 +00001057struct module_sect_attr
1058{
1059 struct module_attribute mattr;
1060 char *name;
1061 unsigned long address;
1062};
1063
1064struct module_sect_attrs
1065{
1066 struct attribute_group grp;
1067 unsigned int nsections;
1068 struct module_sect_attr attrs[0];
1069};
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071static ssize_t module_sect_show(struct module_attribute *mattr,
1072 struct module *mod, char *buf)
1073{
1074 struct module_sect_attr *sattr =
1075 container_of(mattr, struct module_sect_attr, mattr);
1076 return sprintf(buf, "0x%lx\n", sattr->address);
1077}
1078
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001079static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1080{
Rusty Russella58730c2008-03-13 09:03:44 +00001081 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001082
1083 for (section = 0; section < sect_attrs->nsections; section++)
1084 kfree(sect_attrs->attrs[section].name);
1085 kfree(sect_attrs);
1086}
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088static void add_sect_attrs(struct module *mod, unsigned int nsect,
1089 char *secstrings, Elf_Shdr *sechdrs)
1090{
1091 unsigned int nloaded = 0, i, size[2];
1092 struct module_sect_attrs *sect_attrs;
1093 struct module_sect_attr *sattr;
1094 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 /* Count loaded sections and allocate structures */
1097 for (i = 0; i < nsect; i++)
1098 if (sechdrs[i].sh_flags & SHF_ALLOC)
1099 nloaded++;
1100 size[0] = ALIGN(sizeof(*sect_attrs)
1101 + nloaded * sizeof(sect_attrs->attrs[0]),
1102 sizeof(sect_attrs->grp.attrs[0]));
1103 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001104 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1105 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return;
1107
1108 /* Setup section attributes. */
1109 sect_attrs->grp.name = "sections";
1110 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1111
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001112 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 sattr = &sect_attrs->attrs[0];
1114 gattr = &sect_attrs->grp.attrs[0];
1115 for (i = 0; i < nsect; i++) {
1116 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1117 continue;
1118 sattr->address = sechdrs[i].sh_addr;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001119 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1120 GFP_KERNEL);
1121 if (sattr->name == NULL)
1122 goto out;
1123 sect_attrs->nsections++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 sattr->mattr.show = module_sect_show;
1125 sattr->mattr.store = NULL;
1126 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 sattr->mattr.attr.mode = S_IRUGO;
1128 *(gattr++) = &(sattr++)->mattr.attr;
1129 }
1130 *gattr = NULL;
1131
1132 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1133 goto out;
1134
1135 mod->sect_attrs = sect_attrs;
1136 return;
1137 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001138 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
1141static void remove_sect_attrs(struct module *mod)
1142{
1143 if (mod->sect_attrs) {
1144 sysfs_remove_group(&mod->mkobj.kobj,
1145 &mod->sect_attrs->grp);
1146 /* We are positive that no one is using any sect attrs
1147 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001148 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 mod->sect_attrs = NULL;
1150 }
1151}
1152
Roland McGrath6d760132007-10-16 23:26:40 -07001153/*
1154 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1155 */
1156
1157struct module_notes_attrs {
1158 struct kobject *dir;
1159 unsigned int notes;
1160 struct bin_attribute attrs[0];
1161};
1162
1163static ssize_t module_notes_read(struct kobject *kobj,
1164 struct bin_attribute *bin_attr,
1165 char *buf, loff_t pos, size_t count)
1166{
1167 /*
1168 * The caller checked the pos and count against our size.
1169 */
1170 memcpy(buf, bin_attr->private + pos, count);
1171 return count;
1172}
1173
1174static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1175 unsigned int i)
1176{
1177 if (notes_attrs->dir) {
1178 while (i-- > 0)
1179 sysfs_remove_bin_file(notes_attrs->dir,
1180 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001181 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001182 }
1183 kfree(notes_attrs);
1184}
1185
1186static void add_notes_attrs(struct module *mod, unsigned int nsect,
1187 char *secstrings, Elf_Shdr *sechdrs)
1188{
1189 unsigned int notes, loaded, i;
1190 struct module_notes_attrs *notes_attrs;
1191 struct bin_attribute *nattr;
1192
1193 /* Count notes sections and allocate structures. */
1194 notes = 0;
1195 for (i = 0; i < nsect; i++)
1196 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1197 (sechdrs[i].sh_type == SHT_NOTE))
1198 ++notes;
1199
1200 if (notes == 0)
1201 return;
1202
1203 notes_attrs = kzalloc(sizeof(*notes_attrs)
1204 + notes * sizeof(notes_attrs->attrs[0]),
1205 GFP_KERNEL);
1206 if (notes_attrs == NULL)
1207 return;
1208
1209 notes_attrs->notes = notes;
1210 nattr = &notes_attrs->attrs[0];
1211 for (loaded = i = 0; i < nsect; ++i) {
1212 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1213 continue;
1214 if (sechdrs[i].sh_type == SHT_NOTE) {
1215 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1216 nattr->attr.mode = S_IRUGO;
1217 nattr->size = sechdrs[i].sh_size;
1218 nattr->private = (void *) sechdrs[i].sh_addr;
1219 nattr->read = module_notes_read;
1220 ++nattr;
1221 }
1222 ++loaded;
1223 }
1224
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001225 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001226 if (!notes_attrs->dir)
1227 goto out;
1228
1229 for (i = 0; i < notes; ++i)
1230 if (sysfs_create_bin_file(notes_attrs->dir,
1231 &notes_attrs->attrs[i]))
1232 goto out;
1233
1234 mod->notes_attrs = notes_attrs;
1235 return;
1236
1237 out:
1238 free_notes_attrs(notes_attrs, i);
1239}
1240
1241static void remove_notes_attrs(struct module *mod)
1242{
1243 if (mod->notes_attrs)
1244 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1245}
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1250 char *sectstrings, Elf_Shdr *sechdrs)
1251{
1252}
1253
1254static inline void remove_sect_attrs(struct module *mod)
1255{
1256}
Roland McGrath6d760132007-10-16 23:26:40 -07001257
1258static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1259 char *sectstrings, Elf_Shdr *sechdrs)
1260{
1261}
1262
1263static inline void remove_notes_attrs(struct module *mod)
1264{
1265}
Kay Sievers120fc3d2008-02-21 00:33:20 +01001266#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Randy Dunlapef665c12007-02-13 15:19:06 -08001268#ifdef CONFIG_SYSFS
1269int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001270{
1271 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001272 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001273 int error = 0;
1274 int i;
1275
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001276 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1277 (ARRAY_SIZE(modinfo_attrs) + 1)),
1278 GFP_KERNEL);
1279 if (!mod->modinfo_attrs)
1280 return -ENOMEM;
1281
1282 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001283 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1284 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001285 (attr->test && attr->test(mod))) {
1286 memcpy(temp_attr, attr, sizeof(*temp_attr));
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001287 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1288 ++temp_attr;
1289 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001290 }
1291 return error;
1292}
1293
Randy Dunlapef665c12007-02-13 15:19:06 -08001294void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001295{
1296 struct module_attribute *attr;
1297 int i;
1298
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001299 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1300 /* pick a field to test for end of list */
1301 if (!attr->attr.name)
1302 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001303 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001304 if (attr->free)
1305 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001306 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001307 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001308}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Randy Dunlapef665c12007-02-13 15:19:06 -08001310int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001313 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001315 if (!module_sysfs_initialized) {
1316 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001317 mod->name);
1318 err = -EINVAL;
1319 goto out;
1320 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001321
1322 kobj = kset_find_obj(module_kset, mod->name);
1323 if (kobj) {
1324 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1325 kobject_put(kobj);
1326 err = -EINVAL;
1327 goto out;
1328 }
1329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001331
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001332 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1333 mod->mkobj.kobj.kset = module_kset;
1334 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1335 "%s", mod->name);
1336 if (err)
1337 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001338
Kay Sievers97c146e2007-11-29 23:46:11 +01001339 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001340out:
1341 return err;
1342}
1343
Randy Dunlapef665c12007-02-13 15:19:06 -08001344int mod_sysfs_setup(struct module *mod,
Kay Sievers270a6c42007-01-18 13:26:15 +01001345 struct kernel_param *kparam,
1346 unsigned int num_params)
1347{
1348 int err;
1349
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001350 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001351 if (!mod->holders_dir) {
1352 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001353 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001354 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 err = module_param_sysfs_setup(mod, kparam, num_params);
1357 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001358 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Matt Domschc988d2b2005-06-23 22:05:15 -07001360 err = module_add_modinfo_attrs(mod);
1361 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001362 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001363
Kay Sieverse17e0f52006-11-24 12:15:25 +01001364 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return 0;
1366
Kay Sieverse17e0f52006-11-24 12:15:25 +01001367out_unreg_param:
1368 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001369out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001370 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001371out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001372 kobject_put(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return err;
1374}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001375
1376static void mod_sysfs_fini(struct module *mod)
1377{
1378 kobject_put(&mod->mkobj.kobj);
1379}
1380
1381#else /* CONFIG_SYSFS */
1382
1383static void mod_sysfs_fini(struct module *mod)
1384{
1385}
1386
1387#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389static void mod_kobject_remove(struct module *mod)
1390{
Matt Domschc988d2b2005-06-23 22:05:15 -07001391 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001393 kobject_put(mod->mkobj.drivers_dir);
1394 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001395 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396}
1397
1398/*
Rusty Russellbb9d3d52008-01-29 17:13:21 -05001399 * link the module with the whole machine is stopped with interrupts off
1400 * - this defends against kallsyms not taking locks
1401 */
1402static int __link_module(void *_mod)
1403{
1404 struct module *mod = _mod;
1405 list_add(&mod->list, &modules);
1406 return 0;
1407}
1408
1409/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 * unlink the module with the whole machine is stopped with interrupts off
1411 * - this defends against kallsyms not taking locks
1412 */
1413static int __unlink_module(void *_mod)
1414{
1415 struct module *mod = _mod;
1416 list_del(&mod->list);
1417 return 0;
1418}
1419
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001420/* Free a module, remove from lists, etc (must hold module_mutex). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421static void free_module(struct module *mod)
1422{
1423 /* Delete from various lists */
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001424 stop_machine(__unlink_module, mod, NULL);
Roland McGrath6d760132007-10-16 23:26:40 -07001425 remove_notes_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 remove_sect_attrs(mod);
1427 mod_kobject_remove(mod);
1428
Jan Beulich4552d5d2006-06-26 13:57:28 +02001429 unwind_remove_table(mod->unwind_info, 0);
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 /* Arch-specific cleanup. */
1432 module_arch_cleanup(mod);
1433
1434 /* Module unload stuff */
1435 module_unload_free(mod);
1436
Steven Rostedtfed19392008-08-14 22:47:19 -04001437 /* release any pointers to mcount in this module */
1438 ftrace_release(mod->module_core, mod->core_size);
1439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 /* This may be NULL, but that's OK */
1441 module_free(mod, mod->module_init);
1442 kfree(mod->args);
1443 if (mod->percpu)
1444 percpu_modfree(mod->percpu);
1445
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001446 /* Free lock-classes: */
1447 lockdep_free_key_range(mod->module_core, mod->core_size);
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 /* Finally, free the core (containing the module structure) */
1450 module_free(mod, mod->module_core);
1451}
1452
1453void *__symbol_get(const char *symbol)
1454{
1455 struct module *owner;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001456 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Rusty Russell24da1cb2007-07-15 23:41:46 -07001458 preempt_disable();
Rusty Russellad9546c2008-05-01 21:14:59 -05001459 value = find_symbol(symbol, &owner, NULL, true, true);
Christoph Lameter88173502008-02-08 04:18:41 -08001460 if (IS_ERR_VALUE(value))
1461 value = 0;
1462 else if (strong_try_module_get(owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 value = 0;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001464 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 return (void *)value;
1467}
1468EXPORT_SYMBOL_GPL(__symbol_get);
1469
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001470/*
1471 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001472 * in the kernel or in some other module's exported symbol table.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001473 */
1474static int verify_export_symbols(struct module *mod)
1475{
Rusty Russellb2111042008-05-01 21:15:00 -05001476 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001477 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001478 const struct kernel_symbol *s;
1479 struct {
1480 const struct kernel_symbol *sym;
1481 unsigned int num;
1482 } arr[] = {
1483 { mod->syms, mod->num_syms },
1484 { mod->gpl_syms, mod->num_gpl_syms },
1485 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001486#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001487 { mod->unused_syms, mod->num_unused_syms },
1488 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001489#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001490 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001491
Rusty Russellb2111042008-05-01 21:15:00 -05001492 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1493 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1494 if (!IS_ERR_VALUE(find_symbol(s->name, &owner,
1495 NULL, true, false))) {
1496 printk(KERN_ERR
1497 "%s: exports duplicate symbol %s"
1498 " (owned by %s)\n",
1499 mod->name, s->name, module_name(owner));
1500 return -ENOEXEC;
1501 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001502 }
Rusty Russellb2111042008-05-01 21:15:00 -05001503 }
1504 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001505}
1506
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001507/* Change all symbols so that st_value encodes the pointer directly. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508static int simplify_symbols(Elf_Shdr *sechdrs,
1509 unsigned int symindex,
1510 const char *strtab,
1511 unsigned int versindex,
1512 unsigned int pcpuindex,
1513 struct module *mod)
1514{
1515 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1516 unsigned long secbase;
1517 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1518 int ret = 0;
1519
1520 for (i = 1; i < n; i++) {
1521 switch (sym[i].st_shndx) {
1522 case SHN_COMMON:
1523 /* We compiled with -fno-common. These are not
1524 supposed to happen. */
1525 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1526 printk("%s: please compile with -fno-common\n",
1527 mod->name);
1528 ret = -ENOEXEC;
1529 break;
1530
1531 case SHN_ABS:
1532 /* Don't need to do anything */
1533 DEBUGP("Absolute symbol: 0x%08lx\n",
1534 (long)sym[i].st_value);
1535 break;
1536
1537 case SHN_UNDEF:
1538 sym[i].st_value
1539 = resolve_symbol(sechdrs, versindex,
1540 strtab + sym[i].st_name, mod);
1541
1542 /* Ok if resolved. */
Christoph Lameter88173502008-02-08 04:18:41 -08001543 if (!IS_ERR_VALUE(sym[i].st_value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 break;
1545 /* Ok if weak. */
1546 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1547 break;
1548
1549 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1550 mod->name, strtab + sym[i].st_name);
1551 ret = -ENOENT;
1552 break;
1553
1554 default:
1555 /* Divert to percpu allocation if a percpu var. */
1556 if (sym[i].st_shndx == pcpuindex)
1557 secbase = (unsigned long)mod->percpu;
1558 else
1559 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1560 sym[i].st_value += secbase;
1561 break;
1562 }
1563 }
1564
1565 return ret;
1566}
1567
1568/* Update size with this section: return offset. */
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05001569static long get_offset(unsigned int *size, Elf_Shdr *sechdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
1571 long ret;
1572
1573 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1574 *size = ret + sechdr->sh_size;
1575 return ret;
1576}
1577
1578/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1579 might -- code, read-only data, read-write data, small data. Tally
1580 sizes, and place the offsets into sh_entsize fields: high bit means it
1581 belongs in init. */
1582static void layout_sections(struct module *mod,
1583 const Elf_Ehdr *hdr,
1584 Elf_Shdr *sechdrs,
1585 const char *secstrings)
1586{
1587 static unsigned long const masks[][2] = {
1588 /* NOTE: all executable code must be the first section
1589 * in this array; otherwise modify the text_size
1590 * finder in the two loops below */
1591 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1592 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1593 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1594 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1595 };
1596 unsigned int m, i;
1597
1598 for (i = 0; i < hdr->e_shnum; i++)
1599 sechdrs[i].sh_entsize = ~0UL;
1600
1601 DEBUGP("Core section allocation order:\n");
1602 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1603 for (i = 0; i < hdr->e_shnum; ++i) {
1604 Elf_Shdr *s = &sechdrs[i];
1605
1606 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1607 || (s->sh_flags & masks[m][1])
1608 || s->sh_entsize != ~0UL
1609 || strncmp(secstrings + s->sh_name,
1610 ".init", 5) == 0)
1611 continue;
1612 s->sh_entsize = get_offset(&mod->core_size, s);
1613 DEBUGP("\t%s\n", secstrings + s->sh_name);
1614 }
1615 if (m == 0)
1616 mod->core_text_size = mod->core_size;
1617 }
1618
1619 DEBUGP("Init section allocation order:\n");
1620 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1621 for (i = 0; i < hdr->e_shnum; ++i) {
1622 Elf_Shdr *s = &sechdrs[i];
1623
1624 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1625 || (s->sh_flags & masks[m][1])
1626 || s->sh_entsize != ~0UL
1627 || strncmp(secstrings + s->sh_name,
1628 ".init", 5) != 0)
1629 continue;
1630 s->sh_entsize = (get_offset(&mod->init_size, s)
1631 | INIT_OFFSET_MASK);
1632 DEBUGP("\t%s\n", secstrings + s->sh_name);
1633 }
1634 if (m == 0)
1635 mod->init_text_size = mod->init_size;
1636 }
1637}
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639static void set_license(struct module *mod, const char *license)
1640{
1641 if (!license)
1642 license = "unspecified";
1643
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001644 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07001645 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001646 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001647 "kernel.\n", mod->name, license);
1648 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
1650}
1651
1652/* Parse tag=value strings from .modinfo section */
1653static char *next_string(char *string, unsigned long *secsize)
1654{
1655 /* Skip non-zero chars */
1656 while (string[0]) {
1657 string++;
1658 if ((*secsize)-- <= 1)
1659 return NULL;
1660 }
1661
1662 /* Skip any zero padding. */
1663 while (!string[0]) {
1664 string++;
1665 if ((*secsize)-- <= 1)
1666 return NULL;
1667 }
1668 return string;
1669}
1670
1671static char *get_modinfo(Elf_Shdr *sechdrs,
1672 unsigned int info,
1673 const char *tag)
1674{
1675 char *p;
1676 unsigned int taglen = strlen(tag);
1677 unsigned long size = sechdrs[info].sh_size;
1678
1679 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1680 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1681 return p + taglen + 1;
1682 }
1683 return NULL;
1684}
1685
Matt Domschc988d2b2005-06-23 22:05:15 -07001686static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1687 unsigned int infoindex)
1688{
1689 struct module_attribute *attr;
1690 int i;
1691
1692 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1693 if (attr->setup)
1694 attr->setup(mod,
1695 get_modinfo(sechdrs,
1696 infoindex,
1697 attr->attr.name));
1698 }
1699}
Matt Domschc988d2b2005-06-23 22:05:15 -07001700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01001702
1703/* lookup symbol in given range of kernel_symbols */
1704static const struct kernel_symbol *lookup_symbol(const char *name,
1705 const struct kernel_symbol *start,
1706 const struct kernel_symbol *stop)
1707{
1708 const struct kernel_symbol *ks = start;
1709 for (; ks < stop; ks++)
1710 if (strcmp(ks->name, name) == 0)
1711 return ks;
1712 return NULL;
1713}
1714
Alexey Dobriyanea078902007-05-08 00:28:39 -07001715static int is_exported(const char *name, const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001717 if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
1718 return 1;
1719 else
Jesper Juhlf867d2a2006-06-25 05:47:09 -07001720 if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 return 1;
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001722 else
1723 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725
1726/* As per nm */
1727static char elf_type(const Elf_Sym *sym,
1728 Elf_Shdr *sechdrs,
1729 const char *secstrings,
1730 struct module *mod)
1731{
1732 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1733 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1734 return 'v';
1735 else
1736 return 'w';
1737 }
1738 if (sym->st_shndx == SHN_UNDEF)
1739 return 'U';
1740 if (sym->st_shndx == SHN_ABS)
1741 return 'a';
1742 if (sym->st_shndx >= SHN_LORESERVE)
1743 return '?';
1744 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1745 return 't';
1746 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1747 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1748 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1749 return 'r';
1750 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1751 return 'g';
1752 else
1753 return 'd';
1754 }
1755 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1756 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1757 return 's';
1758 else
1759 return 'b';
1760 }
1761 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1762 ".debug", strlen(".debug")) == 0)
1763 return 'n';
1764 return '?';
1765}
1766
1767static void add_kallsyms(struct module *mod,
1768 Elf_Shdr *sechdrs,
1769 unsigned int symindex,
1770 unsigned int strindex,
1771 const char *secstrings)
1772{
1773 unsigned int i;
1774
1775 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1776 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1777 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1778
1779 /* Set types up while we still have access to sections. */
1780 for (i = 0; i < mod->num_symtab; i++)
1781 mod->symtab[i].st_info
1782 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1783}
1784#else
1785static inline void add_kallsyms(struct module *mod,
1786 Elf_Shdr *sechdrs,
1787 unsigned int symindex,
1788 unsigned int strindex,
1789 const char *secstrings)
1790{
1791}
1792#endif /* CONFIG_KALLSYMS */
1793
Jason Baron346e15b2008-08-12 16:46:19 -04001794#ifdef CONFIG_DYNAMIC_PRINTK_DEBUG
1795static void dynamic_printk_setup(Elf_Shdr *sechdrs, unsigned int verboseindex)
1796{
1797 struct mod_debug *debug_info;
1798 unsigned long pos, end;
1799 unsigned int num_verbose;
1800
1801 pos = sechdrs[verboseindex].sh_addr;
1802 num_verbose = sechdrs[verboseindex].sh_size /
1803 sizeof(struct mod_debug);
1804 end = pos + (num_verbose * sizeof(struct mod_debug));
1805
1806 for (; pos < end; pos += sizeof(struct mod_debug)) {
1807 debug_info = (struct mod_debug *)pos;
1808 register_dynamic_debug_module(debug_info->modname,
1809 debug_info->type, debug_info->logical_modname,
1810 debug_info->flag_names, debug_info->hash,
1811 debug_info->hash2);
1812 }
1813}
1814#else
1815static inline void dynamic_printk_setup(Elf_Shdr *sechdrs,
1816 unsigned int verboseindex)
1817{
1818}
1819#endif /* CONFIG_DYNAMIC_PRINTK_DEBUG */
1820
Rusty Russell3a642e92008-07-22 19:24:28 -05001821static void *module_alloc_update_bounds(unsigned long size)
1822{
1823 void *ret = module_alloc(size);
1824
1825 if (ret) {
1826 /* Update module bounds. */
1827 if ((unsigned long)ret < module_addr_min)
1828 module_addr_min = (unsigned long)ret;
1829 if ((unsigned long)ret + size > module_addr_max)
1830 module_addr_max = (unsigned long)ret + size;
1831 }
1832 return ret;
1833}
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835/* Allocate and load the module: note that size of section 0 is always
1836 zero, and we rely on this for optional sections. */
Linus Torvaldsffb4ba72008-08-25 11:10:26 -07001837static noinline struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 unsigned long len,
1839 const char __user *uargs)
1840{
1841 Elf_Ehdr *hdr;
1842 Elf_Shdr *sechdrs;
1843 char *secstrings, *args, *modmagic, *strtab = NULL;
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07001844 char *staging;
Andrew Morton84860f92006-06-28 04:26:46 -07001845 unsigned int i;
1846 unsigned int symindex = 0;
1847 unsigned int strindex = 0;
1848 unsigned int setupindex;
1849 unsigned int exindex;
1850 unsigned int exportindex;
1851 unsigned int modindex;
1852 unsigned int obsparmindex;
1853 unsigned int infoindex;
1854 unsigned int gplindex;
1855 unsigned int crcindex;
1856 unsigned int gplcrcindex;
1857 unsigned int versindex;
1858 unsigned int pcpuindex;
1859 unsigned int gplfutureindex;
1860 unsigned int gplfuturecrcindex;
1861 unsigned int unwindex = 0;
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001862#ifdef CONFIG_UNUSED_SYMBOLS
Andrew Morton84860f92006-06-28 04:26:46 -07001863 unsigned int unusedindex;
1864 unsigned int unusedcrcindex;
1865 unsigned int unusedgplindex;
1866 unsigned int unusedgplcrcindex;
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001867#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07001868 unsigned int markersindex;
1869 unsigned int markersstringsindex;
Jason Baron346e15b2008-08-12 16:46:19 -04001870 unsigned int verboseindex;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04001871 unsigned int tracepointsindex;
1872 unsigned int tracepointsstringsindex;
Steven Rostedt90d595f2008-08-14 15:45:09 -04001873 unsigned int mcountindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 struct module *mod;
1875 long err = 0;
1876 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
Steven Rostedtfed19392008-08-14 22:47:19 -04001877 void *mseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 struct exception_table_entry *extable;
Thomas Koeller378bac82005-09-06 15:17:11 -07001879 mm_segment_t old_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1882 umod, len, uargs);
1883 if (len < sizeof(*hdr))
1884 return ERR_PTR(-ENOEXEC);
1885
1886 /* Suck in entire file: we'll want most of it. */
1887 /* vmalloc barfs on "unusual" numbers. Check here */
1888 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1889 return ERR_PTR(-ENOMEM);
1890 if (copy_from_user(hdr, umod, len) != 0) {
1891 err = -EFAULT;
1892 goto free_hdr;
1893 }
1894
1895 /* Sanity checks against insmoding binaries or wrong arch,
1896 weird elf version */
Cyrill Gorcunovc4ea6fc2008-05-14 16:27:29 -07001897 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 || hdr->e_type != ET_REL
1899 || !elf_check_arch(hdr)
1900 || hdr->e_shentsize != sizeof(*sechdrs)) {
1901 err = -ENOEXEC;
1902 goto free_hdr;
1903 }
1904
1905 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1906 goto truncated;
1907
1908 /* Convenience variables */
1909 sechdrs = (void *)hdr + hdr->e_shoff;
1910 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1911 sechdrs[0].sh_addr = 0;
1912
1913 for (i = 1; i < hdr->e_shnum; i++) {
1914 if (sechdrs[i].sh_type != SHT_NOBITS
1915 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1916 goto truncated;
1917
1918 /* Mark all sections sh_addr with their address in the
1919 temporary image. */
1920 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1921
1922 /* Internal symbols and strings. */
1923 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1924 symindex = i;
1925 strindex = sechdrs[i].sh_link;
1926 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1927 }
1928#ifndef CONFIG_MODULE_UNLOAD
1929 /* Don't load .exit sections */
1930 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1931 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1932#endif
1933 }
1934
1935 modindex = find_sec(hdr, sechdrs, secstrings,
1936 ".gnu.linkonce.this_module");
1937 if (!modindex) {
1938 printk(KERN_WARNING "No module found in object\n");
1939 err = -ENOEXEC;
1940 goto free_hdr;
1941 }
1942 mod = (void *)sechdrs[modindex].sh_addr;
1943
1944 if (symindex == 0) {
1945 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1946 mod->name);
1947 err = -ENOEXEC;
1948 goto free_hdr;
1949 }
1950
1951 /* Optional sections */
1952 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1953 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001954 gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1956 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001957 gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future");
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001958#ifdef CONFIG_UNUSED_SYMBOLS
1959 unusedindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused");
1960 unusedgplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused_gpl");
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001961 unusedcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused");
1962 unusedgplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused_gpl");
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001963#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1965 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1966 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1967 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1968 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1969 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
Jan Beulich4552d5d2006-06-26 13:57:28 +02001970#ifdef ARCH_UNWIND_SECTION_NAME
1971 unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
1972#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Rusty Russellea01e792008-03-13 09:02:17 +00001974 /* Don't keep modinfo and version sections. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russellea01e792008-03-13 09:02:17 +00001976 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977#ifdef CONFIG_KALLSYMS
1978 /* Keep symbol and string tables for decoding later. */
1979 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1980 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1981#endif
Jan Beulich4552d5d2006-06-26 13:57:28 +02001982 if (unwindex)
1983 sechdrs[unwindex].sh_flags |= SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
1985 /* Check module struct version now, before we try to use module. */
1986 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1987 err = -ENOEXEC;
1988 goto free_hdr;
1989 }
1990
1991 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1992 /* This is allowed: modprobe --force will invalidate it. */
1993 if (!modmagic) {
Linus Torvalds826e4502008-05-04 17:04:16 -07001994 err = try_to_force_load(mod, "magic");
1995 if (err)
1996 goto free_hdr;
Rusty Russell91e37a72008-05-09 16:25:28 +10001997 } else if (!same_magic(modmagic, vermagic, versindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1999 mod->name, modmagic, vermagic);
2000 err = -ENOEXEC;
2001 goto free_hdr;
2002 }
2003
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002004 staging = get_modinfo(sechdrs, infoindex, "staging");
2005 if (staging) {
2006 add_taint_module(mod, TAINT_CRAP);
2007 printk(KERN_WARNING "%s: module is from the staging directory,"
2008 " the quality is unknown, you have been warned.\n",
2009 mod->name);
2010 }
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 /* Now copy in args */
Davi Arnaut24277dd2006-03-24 03:18:43 -08002013 args = strndup_user(uargs, ~0UL >> 1);
2014 if (IS_ERR(args)) {
2015 err = PTR_ERR(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 goto free_hdr;
2017 }
Andrew Morton8e08b752006-02-07 12:58:45 -08002018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 if (find_module(mod->name)) {
2020 err = -EEXIST;
2021 goto free_mod;
2022 }
2023
2024 mod->state = MODULE_STATE_COMING;
2025
2026 /* Allow arches to frob section contents and sizes. */
2027 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
2028 if (err < 0)
2029 goto free_mod;
2030
2031 if (pcpuindex) {
2032 /* We have a special allocation for this section. */
2033 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
Rusty Russell842bbaa2005-08-01 21:11:47 -07002034 sechdrs[pcpuindex].sh_addralign,
2035 mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (!percpu) {
2037 err = -ENOMEM;
2038 goto free_mod;
2039 }
2040 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2041 mod->percpu = percpu;
2042 }
2043
2044 /* Determine total sizes, and put offsets in sh_entsize. For now
2045 this is done generically; there doesn't appear to be any
2046 special cases for the architectures. */
2047 layout_sections(mod, hdr, sechdrs, secstrings);
2048
2049 /* Do the allocs. */
Rusty Russell3a642e92008-07-22 19:24:28 -05002050 ptr = module_alloc_update_bounds(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 if (!ptr) {
2052 err = -ENOMEM;
2053 goto free_percpu;
2054 }
2055 memset(ptr, 0, mod->core_size);
2056 mod->module_core = ptr;
2057
Rusty Russell3a642e92008-07-22 19:24:28 -05002058 ptr = module_alloc_update_bounds(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 if (!ptr && mod->init_size) {
2060 err = -ENOMEM;
2061 goto free_core;
2062 }
2063 memset(ptr, 0, mod->init_size);
2064 mod->module_init = ptr;
2065
2066 /* Transfer each section which specifies SHF_ALLOC */
2067 DEBUGP("final section addresses:\n");
2068 for (i = 0; i < hdr->e_shnum; i++) {
2069 void *dest;
2070
2071 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2072 continue;
2073
2074 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2075 dest = mod->module_init
2076 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2077 else
2078 dest = mod->module_core + sechdrs[i].sh_entsize;
2079
2080 if (sechdrs[i].sh_type != SHT_NOBITS)
2081 memcpy(dest, (void *)sechdrs[i].sh_addr,
2082 sechdrs[i].sh_size);
2083 /* Update sh_addr to point to copy in image. */
2084 sechdrs[i].sh_addr = (unsigned long)dest;
2085 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2086 }
2087 /* Module has been moved. */
2088 mod = (void *)sechdrs[modindex].sh_addr;
2089
2090 /* Now we've moved module, initialize linked lists, etc. */
2091 module_unload_init(mod);
2092
Kay Sievers97c146e2007-11-29 23:46:11 +01002093 /* add kobject, so we can reference it. */
Akinobu Mitad58ae672007-10-16 23:30:27 -07002094 err = mod_sysfs_init(mod);
2095 if (err)
Kay Sievers97c146e2007-11-29 23:46:11 +01002096 goto free_unload;
Kay Sievers270a6c42007-01-18 13:26:15 +01002097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 /* Set up license info based on the info section */
2099 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2100
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002101 /*
2102 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2103 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2104 * using GPL-only symbols it needs.
2105 */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002106 if (strcmp(mod->name, "ndiswrapper") == 0)
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002107 add_taint(TAINT_PROPRIETARY_MODULE);
2108
2109 /* driverloader was caught wrongly pretending to be under GPL */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002110 if (strcmp(mod->name, "driverloader") == 0)
2111 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Dave Jones9841d61d2006-01-08 01:03:41 -08002112
Matt Domschc988d2b2005-06-23 22:05:15 -07002113 /* Set up MODINFO_ATTR fields */
2114 setup_modinfo(mod, sechdrs, infoindex);
Matt Domschc988d2b2005-06-23 22:05:15 -07002115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 /* Fix up syms, so that st_value is a pointer to location. */
2117 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2118 mod);
2119 if (err < 0)
2120 goto cleanup;
2121
2122 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
2123 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
2124 mod->syms = (void *)sechdrs[exportindex].sh_addr;
2125 if (crcindex)
2126 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
2127 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
2128 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
2129 if (gplcrcindex)
2130 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08002131 mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size /
2132 sizeof(*mod->gpl_future_syms);
2133 mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr;
2134 if (gplfuturecrcindex)
2135 mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002137#ifdef CONFIG_UNUSED_SYMBOLS
2138 mod->num_unused_syms = sechdrs[unusedindex].sh_size /
2139 sizeof(*mod->unused_syms);
2140 mod->num_unused_gpl_syms = sechdrs[unusedgplindex].sh_size /
2141 sizeof(*mod->unused_gpl_syms);
Arjan van de Venf71d20e2006-06-28 04:26:45 -07002142 mod->unused_syms = (void *)sechdrs[unusedindex].sh_addr;
2143 if (unusedcrcindex)
2144 mod->unused_crcs = (void *)sechdrs[unusedcrcindex].sh_addr;
2145 mod->unused_gpl_syms = (void *)sechdrs[unusedgplindex].sh_addr;
2146 if (unusedgplcrcindex)
Rusty Russell4e2d9242008-05-01 21:15:00 -05002147 mod->unused_gpl_crcs
2148 = (void *)sechdrs[unusedgplcrcindex].sh_addr;
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002149#endif
Arjan van de Venf71d20e2006-06-28 04:26:45 -07002150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151#ifdef CONFIG_MODVERSIONS
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002152 if ((mod->num_syms && !crcindex)
2153 || (mod->num_gpl_syms && !gplcrcindex)
2154 || (mod->num_gpl_future_syms && !gplfuturecrcindex)
2155#ifdef CONFIG_UNUSED_SYMBOLS
2156 || (mod->num_unused_syms && !unusedcrcindex)
2157 || (mod->num_unused_gpl_syms && !unusedgplcrcindex)
2158#endif
2159 ) {
Linus Torvalds826e4502008-05-04 17:04:16 -07002160 printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name);
2161 err = try_to_force_load(mod, "nocrc");
2162 if (err)
2163 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 }
2165#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002166 markersindex = find_sec(hdr, sechdrs, secstrings, "__markers");
2167 markersstringsindex = find_sec(hdr, sechdrs, secstrings,
2168 "__markers_strings");
Jason Baron346e15b2008-08-12 16:46:19 -04002169 verboseindex = find_sec(hdr, sechdrs, secstrings, "__verbose");
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002170 tracepointsindex = find_sec(hdr, sechdrs, secstrings, "__tracepoints");
2171 tracepointsstringsindex = find_sec(hdr, sechdrs, secstrings,
2172 "__tracepoints_strings");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Steven Rostedt90d595f2008-08-14 15:45:09 -04002174 mcountindex = find_sec(hdr, sechdrs, secstrings,
2175 "__mcount_loc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
2177 /* Now do relocations. */
2178 for (i = 1; i < hdr->e_shnum; i++) {
2179 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2180 unsigned int info = sechdrs[i].sh_info;
2181
2182 /* Not a valid relocation section? */
2183 if (info >= hdr->e_shnum)
2184 continue;
2185
2186 /* Don't bother with non-allocated sections */
2187 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2188 continue;
2189
2190 if (sechdrs[i].sh_type == SHT_REL)
2191 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2192 else if (sechdrs[i].sh_type == SHT_RELA)
2193 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2194 mod);
2195 if (err < 0)
2196 goto cleanup;
2197 }
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002198#ifdef CONFIG_MARKERS
2199 mod->markers = (void *)sechdrs[markersindex].sh_addr;
2200 mod->num_markers =
2201 sechdrs[markersindex].sh_size / sizeof(*mod->markers);
2202#endif
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002203#ifdef CONFIG_TRACEPOINTS
2204 mod->tracepoints = (void *)sechdrs[tracepointsindex].sh_addr;
2205 mod->num_tracepoints =
2206 sechdrs[tracepointsindex].sh_size / sizeof(*mod->tracepoints);
2207#endif
2208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002210 /* Find duplicate symbols */
2211 err = verify_export_symbols(mod);
2212
2213 if (err < 0)
2214 goto cleanup;
2215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 /* Set up and sort exception table */
2217 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
2218 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
2219 sort_extable(extable, extable + mod->num_exentries);
2220
2221 /* Finally, copy percpu area over. */
2222 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2223 sechdrs[pcpuindex].sh_size);
2224
2225 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2226
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002227 if (!mod->taints) {
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002228#ifdef CONFIG_MARKERS
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002229 marker_update_probe_range(mod->markers,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002230 mod->markers + mod->num_markers);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002231#endif
Jason Baron346e15b2008-08-12 16:46:19 -04002232 dynamic_printk_setup(sechdrs, verboseindex);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002233#ifdef CONFIG_TRACEPOINTS
2234 tracepoint_update_probe_range(mod->tracepoints,
2235 mod->tracepoints + mod->num_tracepoints);
2236#endif
2237 }
Steven Rostedt90d595f2008-08-14 15:45:09 -04002238
Steven Rostedtfed19392008-08-14 22:47:19 -04002239 /* sechdrs[0].sh_size is always zero */
2240 mseg = (void *)sechdrs[mcountindex].sh_addr;
2241 ftrace_init_module(mseg, mseg + sechdrs[mcountindex].sh_size);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 err = module_finalize(hdr, sechdrs, mod);
2244 if (err < 0)
2245 goto cleanup;
2246
Thomas Koeller378bac82005-09-06 15:17:11 -07002247 /* flush the icache in correct context */
2248 old_fs = get_fs();
2249 set_fs(KERNEL_DS);
2250
2251 /*
2252 * Flush the instruction cache, since we've played with text.
2253 * Do it before processing of module parameters, so the module
2254 * can provide parameter accessor functions of its own.
2255 */
2256 if (mod->module_init)
2257 flush_icache_range((unsigned long)mod->module_init,
2258 (unsigned long)mod->module_init
2259 + mod->init_size);
2260 flush_icache_range((unsigned long)mod->module_core,
2261 (unsigned long)mod->module_core + mod->core_size);
2262
2263 set_fs(old_fs);
2264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 mod->args = args;
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002266 if (obsparmindex)
2267 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2268 mod->name);
2269
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002270 /* Now sew it into the lists so we can get lockdep and oops
2271 * info during argument parsing. Noone should access us, since
2272 * strong_try_module_get() will fail. */
Rusty Russell9b1a4d32008-07-28 12:16:30 -05002273 stop_machine(__link_module, mod, NULL);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002274
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002275 /* Size of section 0 is 0, so this works well if no params */
2276 err = parse_args(mod->name, mod->args,
2277 (struct kernel_param *)
2278 sechdrs[setupindex].sh_addr,
2279 sechdrs[setupindex].sh_size
2280 / sizeof(struct kernel_param),
2281 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002283 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Daniel Walker22a8bde2007-10-18 03:06:07 -07002285 err = mod_sysfs_setup(mod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 (struct kernel_param *)
2287 sechdrs[setupindex].sh_addr,
2288 sechdrs[setupindex].sh_size
2289 / sizeof(struct kernel_param));
2290 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002291 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Roland McGrath6d760132007-10-16 23:26:40 -07002293 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Jan Beulich4552d5d2006-06-26 13:57:28 +02002295 /* Size of section 0 is 0, so this works well if no unwind info. */
2296 mod->unwind_info = unwind_add_table(mod,
Daniel Walker22a8bde2007-10-18 03:06:07 -07002297 (void *)sechdrs[unwindex].sh_addr,
2298 sechdrs[unwindex].sh_size);
Jan Beulich4552d5d2006-06-26 13:57:28 +02002299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 /* Get rid of temporary copy */
2301 vfree(hdr);
2302
2303 /* Done! */
2304 return mod;
2305
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002306 unlink:
Rusty Russell9b1a4d32008-07-28 12:16:30 -05002307 stop_machine(__unlink_module, mod, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 module_arch_cleanup(mod);
2309 cleanup:
Kay Sievers97c146e2007-11-29 23:46:11 +01002310 kobject_del(&mod->mkobj.kobj);
2311 kobject_put(&mod->mkobj.kobj);
Steven Rostedtfed19392008-08-14 22:47:19 -04002312 ftrace_release(mod->module_core, mod->core_size);
Kay Sievers97c146e2007-11-29 23:46:11 +01002313 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 module_unload_free(mod);
2315 module_free(mod, mod->module_init);
2316 free_core:
2317 module_free(mod, mod->module_core);
2318 free_percpu:
2319 if (percpu)
2320 percpu_modfree(percpu);
2321 free_mod:
2322 kfree(args);
2323 free_hdr:
2324 vfree(hdr);
Jayachandran C6fe2e702006-01-06 00:19:54 -08002325 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327 truncated:
2328 printk(KERN_ERR "Module len %lu truncated\n", len);
2329 err = -ENOEXEC;
2330 goto free_hdr;
2331}
2332
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333/* This is where the real work happens */
2334asmlinkage long
2335sys_init_module(void __user *umod,
2336 unsigned long len,
2337 const char __user *uargs)
2338{
2339 struct module *mod;
2340 int ret = 0;
2341
2342 /* Must have permission */
2343 if (!capable(CAP_SYS_MODULE))
2344 return -EPERM;
2345
2346 /* Only one module load at a time, please */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002347 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return -EINTR;
2349
2350 /* Do all the hard work */
2351 mod = load_module(umod, len, uargs);
2352 if (IS_ERR(mod)) {
Ashutosh Naik6389a382006-03-23 03:00:46 -08002353 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 return PTR_ERR(mod);
2355 }
2356
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 /* Drop lock so they can recurse */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002358 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Alan Sterne041c682006-03-27 01:16:30 -08002360 blocking_notifier_call_chain(&module_notify_list,
2361 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
2363 /* Start the module */
2364 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002365 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 if (ret < 0) {
2367 /* Init routine failed: abort. Try to protect us from
2368 buggy refcounters. */
2369 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002370 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002371 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002372 blocking_notifier_call_chain(&module_notify_list,
2373 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002374 mutex_lock(&module_mutex);
2375 free_module(mod);
2376 mutex_unlock(&module_mutex);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002377 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 return ret;
2379 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002380 if (ret > 0) {
2381 printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
2382 "it should follow 0/-E convention\n"
2383 KERN_WARNING "%s: loading module anyway...\n",
2384 __func__, mod->name, ret,
2385 __func__);
2386 dump_stack();
2387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Rusty Russell6c5db222008-03-10 11:43:52 -07002389 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002391 wake_up(&module_wq);
2392
2393 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 /* Drop initial reference. */
2395 module_put(mod);
Jan Beulich4552d5d2006-06-26 13:57:28 +02002396 unwind_remove_table(mod->unwind_info, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 module_free(mod, mod->module_init);
2398 mod->module_init = NULL;
2399 mod->init_size = 0;
2400 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002401 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
2403 return 0;
2404}
2405
2406static inline int within(unsigned long addr, void *start, unsigned long size)
2407{
2408 return ((void *)addr >= start && (void *)addr < start + size);
2409}
2410
2411#ifdef CONFIG_KALLSYMS
2412/*
2413 * This ignores the intensely annoying "mapping symbols" found
2414 * in ARM ELF files: $a, $t and $d.
2415 */
2416static inline int is_arm_mapping_symbol(const char *str)
2417{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002418 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 && (str[2] == '\0' || str[2] == '.');
2420}
2421
2422static const char *get_ksymbol(struct module *mod,
2423 unsigned long addr,
2424 unsigned long *size,
2425 unsigned long *offset)
2426{
2427 unsigned int i, best = 0;
2428 unsigned long nextval;
2429
2430 /* At worse, next value is at end of module */
2431 if (within(addr, mod->module_init, mod->init_size))
2432 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002433 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2435
2436 /* Scan for closest preceeding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002437 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 for (i = 1; i < mod->num_symtab; i++) {
2439 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2440 continue;
2441
2442 /* We ignore unnamed symbols: they're uninformative
2443 * and inserted at a whim. */
2444 if (mod->symtab[i].st_value <= addr
2445 && mod->symtab[i].st_value > mod->symtab[best].st_value
2446 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2447 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2448 best = i;
2449 if (mod->symtab[i].st_value > addr
2450 && mod->symtab[i].st_value < nextval
2451 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2452 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2453 nextval = mod->symtab[i].st_value;
2454 }
2455
2456 if (!best)
2457 return NULL;
2458
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002459 if (size)
2460 *size = nextval - mod->symtab[best].st_value;
2461 if (offset)
2462 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 return mod->strtab + mod->symtab[best].st_name;
2464}
2465
Rusty Russell6dd06c92008-01-29 17:13:22 -05002466/* For kallsyms to ask for address resolution. NULL means not found. Careful
2467 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002468const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05002469 unsigned long *size,
2470 unsigned long *offset,
2471 char **modname,
2472 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
2474 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002475 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Rusty Russellcb2a5202008-01-14 00:55:03 -08002477 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 list_for_each_entry(mod, &modules, list) {
2479 if (within(addr, mod->module_init, mod->init_size)
2480 || within(addr, mod->module_core, mod->core_size)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002481 if (modname)
2482 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002483 ret = get_ksymbol(mod, addr, size, offset);
2484 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 }
2486 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05002487 /* Make a copy in here where it's safe */
2488 if (ret) {
2489 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2490 ret = namebuf;
2491 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002492 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002493 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494}
2495
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002496int lookup_module_symbol_name(unsigned long addr, char *symname)
2497{
2498 struct module *mod;
2499
Rusty Russellcb2a5202008-01-14 00:55:03 -08002500 preempt_disable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002501 list_for_each_entry(mod, &modules, list) {
2502 if (within(addr, mod->module_init, mod->init_size) ||
2503 within(addr, mod->module_core, mod->core_size)) {
2504 const char *sym;
2505
2506 sym = get_ksymbol(mod, addr, NULL, NULL);
2507 if (!sym)
2508 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07002509 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002510 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002511 return 0;
2512 }
2513 }
2514out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002515 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002516 return -ERANGE;
2517}
2518
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002519int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2520 unsigned long *offset, char *modname, char *name)
2521{
2522 struct module *mod;
2523
Rusty Russellcb2a5202008-01-14 00:55:03 -08002524 preempt_disable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002525 list_for_each_entry(mod, &modules, list) {
2526 if (within(addr, mod->module_init, mod->init_size) ||
2527 within(addr, mod->module_core, mod->core_size)) {
2528 const char *sym;
2529
2530 sym = get_ksymbol(mod, addr, size, offset);
2531 if (!sym)
2532 goto out;
2533 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07002534 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002535 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07002536 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002537 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002538 return 0;
2539 }
2540 }
2541out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002542 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002543 return -ERANGE;
2544}
2545
Alexey Dobriyanea078902007-05-08 00:28:39 -07002546int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2547 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548{
2549 struct module *mod;
2550
Rusty Russellcb2a5202008-01-14 00:55:03 -08002551 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 list_for_each_entry(mod, &modules, list) {
2553 if (symnum < mod->num_symtab) {
2554 *value = mod->symtab[symnum].st_value;
2555 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002556 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07002557 KSYM_NAME_LEN);
2558 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Alexey Dobriyanea078902007-05-08 00:28:39 -07002559 *exported = is_exported(name, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002560 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002561 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 }
2563 symnum -= mod->num_symtab;
2564 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002565 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002566 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567}
2568
2569static unsigned long mod_find_symname(struct module *mod, const char *name)
2570{
2571 unsigned int i;
2572
2573 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002574 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2575 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 return mod->symtab[i].st_value;
2577 return 0;
2578}
2579
2580/* Look for this name: can be of form module:name. */
2581unsigned long module_kallsyms_lookup_name(const char *name)
2582{
2583 struct module *mod;
2584 char *colon;
2585 unsigned long ret = 0;
2586
2587 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08002588 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 if ((colon = strchr(name, ':')) != NULL) {
2590 *colon = '\0';
2591 if ((mod = find_module(name)) != NULL)
2592 ret = mod_find_symname(mod, colon+1);
2593 *colon = ':';
2594 } else {
2595 list_for_each_entry(mod, &modules, list)
2596 if ((ret = mod_find_symname(mod, name)) != 0)
2597 break;
2598 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002599 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 return ret;
2601}
2602#endif /* CONFIG_KALLSYMS */
2603
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002604static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002605{
2606 int bx = 0;
2607
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002608 if (mod->taints ||
2609 mod->state == MODULE_STATE_GOING ||
2610 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002611 buf[bx++] = '(';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002612 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002613 buf[bx++] = 'P';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002614 if (mod->taints & (1 << TAINT_FORCED_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002615 buf[bx++] = 'F';
Linus Torvalds26e9a392008-10-17 09:50:12 -07002616 if (mod->taints & (1 << TAINT_CRAP))
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002617 buf[bx++] = 'C';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002618 /*
2619 * TAINT_FORCED_RMMOD: could be added.
2620 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2621 * apply to modules.
2622 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002623
2624 /* Show a - for module-is-being-unloaded */
2625 if (mod->state == MODULE_STATE_GOING)
2626 buf[bx++] = '-';
2627 /* Show a + for module-is-being-loaded */
2628 if (mod->state == MODULE_STATE_COMING)
2629 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002630 buf[bx++] = ')';
2631 }
2632 buf[bx] = '\0';
2633
2634 return buf;
2635}
2636
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002637#ifdef CONFIG_PROC_FS
2638/* Called by the /proc file system to return a list of modules. */
2639static void *m_start(struct seq_file *m, loff_t *pos)
2640{
2641 mutex_lock(&module_mutex);
2642 return seq_list_start(&modules, *pos);
2643}
2644
2645static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2646{
2647 return seq_list_next(p, &modules, pos);
2648}
2649
2650static void m_stop(struct seq_file *m, void *p)
2651{
2652 mutex_unlock(&module_mutex);
2653}
2654
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655static int m_show(struct seq_file *m, void *p)
2656{
2657 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002658 char buf[8];
2659
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05002660 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 mod->name, mod->init_size + mod->core_size);
2662 print_unload_info(m, mod);
2663
2664 /* Informative for users. */
2665 seq_printf(m, " %s",
2666 mod->state == MODULE_STATE_GOING ? "Unloading":
2667 mod->state == MODULE_STATE_COMING ? "Loading":
2668 "Live");
2669 /* Used by oprofile and other similar tools. */
2670 seq_printf(m, " 0x%p", mod->module_core);
2671
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002672 /* Taints info */
2673 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002674 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002675
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 seq_printf(m, "\n");
2677 return 0;
2678}
2679
2680/* Format: modulename size refcount deps address
2681
2682 Where refcount is a number or -, and deps is a comma-separated list
2683 of depends or -.
2684*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002685static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 .start = m_start,
2687 .next = m_next,
2688 .stop = m_stop,
2689 .show = m_show
2690};
2691
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002692static int modules_open(struct inode *inode, struct file *file)
2693{
2694 return seq_open(file, &modules_op);
2695}
2696
2697static const struct file_operations proc_modules_operations = {
2698 .open = modules_open,
2699 .read = seq_read,
2700 .llseek = seq_lseek,
2701 .release = seq_release,
2702};
2703
2704static int __init proc_modules_init(void)
2705{
2706 proc_create("modules", 0, NULL, &proc_modules_operations);
2707 return 0;
2708}
2709module_init(proc_modules_init);
2710#endif
2711
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712/* Given an address, look for it in the module exception tables. */
2713const struct exception_table_entry *search_module_extables(unsigned long addr)
2714{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 const struct exception_table_entry *e = NULL;
2716 struct module *mod;
2717
Rusty Russell24da1cb2007-07-15 23:41:46 -07002718 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 list_for_each_entry(mod, &modules, list) {
2720 if (mod->num_exentries == 0)
2721 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002722
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 e = search_extable(mod->extable,
2724 mod->extable + mod->num_exentries - 1,
2725 addr);
2726 if (e)
2727 break;
2728 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07002729 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
2731 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07002732 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 return e;
2734}
2735
Ingo Molnar4d435f92006-07-03 00:24:24 -07002736/*
2737 * Is this a valid module address?
2738 */
2739int is_module_address(unsigned long addr)
2740{
Ingo Molnar4d435f92006-07-03 00:24:24 -07002741 struct module *mod;
2742
Rusty Russell24da1cb2007-07-15 23:41:46 -07002743 preempt_disable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002744
2745 list_for_each_entry(mod, &modules, list) {
2746 if (within(addr, mod->module_core, mod->core_size)) {
Rusty Russell24da1cb2007-07-15 23:41:46 -07002747 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002748 return 1;
2749 }
2750 }
2751
Rusty Russell24da1cb2007-07-15 23:41:46 -07002752 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002753
2754 return 0;
2755}
2756
2757
Rusty Russell24da1cb2007-07-15 23:41:46 -07002758/* Is this a valid kernel address? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759struct module *__module_text_address(unsigned long addr)
2760{
2761 struct module *mod;
2762
Rusty Russell3a642e92008-07-22 19:24:28 -05002763 if (addr < module_addr_min || addr > module_addr_max)
2764 return NULL;
2765
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 list_for_each_entry(mod, &modules, list)
2767 if (within(addr, mod->module_init, mod->init_text_size)
2768 || within(addr, mod->module_core, mod->core_text_size))
2769 return mod;
2770 return NULL;
2771}
2772
2773struct module *module_text_address(unsigned long addr)
2774{
2775 struct module *mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
Rusty Russell24da1cb2007-07-15 23:41:46 -07002777 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 mod = __module_text_address(addr);
Rusty Russell24da1cb2007-07-15 23:41:46 -07002779 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
2781 return mod;
2782}
2783
2784/* Don't grab lock, we're oopsing. */
2785void print_modules(void)
2786{
2787 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07002788 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
2790 printk("Modules linked in:");
2791 list_for_each_entry(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002792 printk(" %s%s", mod->name, module_flags(mod, buf));
Arjan van de Vene14af7e2008-01-25 21:08:33 +01002793 if (last_unloaded_module[0])
2794 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 printk("\n");
2796}
2797
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798#ifdef CONFIG_MODVERSIONS
2799/* Generate the signature for struct module here, too, for modversions. */
2800void struct_module(struct module *mod) { return; }
2801EXPORT_SYMBOL(struct_module);
2802#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002803
2804#ifdef CONFIG_MARKERS
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002805void module_update_markers(void)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002806{
2807 struct module *mod;
2808
2809 mutex_lock(&module_mutex);
2810 list_for_each_entry(mod, &modules, list)
2811 if (!mod->taints)
2812 marker_update_probe_range(mod->markers,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002813 mod->markers + mod->num_markers);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002814 mutex_unlock(&module_mutex);
2815}
2816#endif
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002817
2818#ifdef CONFIG_TRACEPOINTS
2819void module_update_tracepoints(void)
2820{
2821 struct module *mod;
2822
2823 mutex_lock(&module_mutex);
2824 list_for_each_entry(mod, &modules, list)
2825 if (!mod->taints)
2826 tracepoint_update_probe_range(mod->tracepoints,
2827 mod->tracepoints + mod->num_tracepoints);
2828 mutex_unlock(&module_mutex);
2829}
2830
2831/*
2832 * Returns 0 if current not found.
2833 * Returns 1 if current found.
2834 */
2835int module_get_iter_tracepoints(struct tracepoint_iter *iter)
2836{
2837 struct module *iter_mod;
2838 int found = 0;
2839
2840 mutex_lock(&module_mutex);
2841 list_for_each_entry(iter_mod, &modules, list) {
2842 if (!iter_mod->taints) {
2843 /*
2844 * Sorted module list
2845 */
2846 if (iter_mod < iter->module)
2847 continue;
2848 else if (iter_mod > iter->module)
2849 iter->tracepoint = NULL;
2850 found = tracepoint_get_iter_range(&iter->tracepoint,
2851 iter_mod->tracepoints,
2852 iter_mod->tracepoints
2853 + iter_mod->num_tracepoints);
2854 if (found) {
2855 iter->module = iter_mod;
2856 break;
2857 }
2858 }
2859 }
2860 mutex_unlock(&module_mutex);
2861 return found;
2862}
2863#endif