blob: 5c7eb0695b3ce82d2a41386c2df0970466089a3c [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>
Roland McGrath6d760132007-10-16 23:26:40 -070023#include <linux/sysfs.h>
Randy Dunlap9f158332005-09-13 01:25:16 -070024#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/slab.h>
26#include <linux/vmalloc.h>
27#include <linux/elf.h>
28#include <linux/seq_file.h>
29#include <linux/syscalls.h>
30#include <linux/fcntl.h>
31#include <linux/rcupdate.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080032#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/cpu.h>
34#include <linux/moduleparam.h>
35#include <linux/errno.h>
36#include <linux/err.h>
37#include <linux/vermagic.h>
38#include <linux/notifier.h>
Al Virof6a57032006-10-18 01:47:25 -040039#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/stop_machine.h>
41#include <linux/device.h>
Matt Domschc988d2b2005-06-23 22:05:15 -070042#include <linux/string.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080043#include <linux/mutex.h>
Jan Beulich4552d5d2006-06-26 13:57:28 +020044#include <linux/unwind.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/cacheflush.h>
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020047#include <linux/license.h>
Christoph Lameter6d762392008-02-08 04:18:42 -080048#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#if 0
51#define DEBUGP printk
52#else
53#define DEBUGP(fmt , a...)
54#endif
55
56#ifndef ARCH_SHF_SMALL
57#define ARCH_SHF_SMALL 0
58#endif
59
60/* If this is set, the section belongs in the init part of the module */
61#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
62
Rusty Russell24da1cb2007-07-15 23:41:46 -070063/* List of modules, protected by module_mutex or preempt_disable
64 * (add/delete uses stop_machine). */
Ashutosh Naik6389a382006-03-23 03:00:46 -080065static DEFINE_MUTEX(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static LIST_HEAD(modules);
67
Rusty Russellc9a3ba52008-01-29 17:13:18 -050068/* Waiting for a module to finish initializing? */
69static DECLARE_WAIT_QUEUE_HEAD(module_wq);
70
Alan Sterne041c682006-03-27 01:16:30 -080071static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73int register_module_notifier(struct notifier_block * nb)
74{
Alan Sterne041c682006-03-27 01:16:30 -080075 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77EXPORT_SYMBOL(register_module_notifier);
78
79int unregister_module_notifier(struct notifier_block * nb)
80{
Alan Sterne041c682006-03-27 01:16:30 -080081 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83EXPORT_SYMBOL(unregister_module_notifier);
84
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -080085/* We require a truly strong try_module_get(): 0 means failure due to
86 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static inline int strong_try_module_get(struct module *mod)
88{
89 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -050090 return -EBUSY;
91 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -050093 else
94 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Florin Malitafa3ba2e82006-10-11 01:21:48 -070097static inline void add_taint_module(struct module *mod, unsigned flag)
98{
99 add_taint(flag);
100 mod->taints |= flag;
101}
102
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200103/*
104 * A thread that wants to hold a reference to a module only while it
105 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 */
107void __module_put_and_exit(struct module *mod, long code)
108{
109 module_put(mod);
110 do_exit(code);
111}
112EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/* Find a module section: 0 means not found. */
115static unsigned int find_sec(Elf_Ehdr *hdr,
116 Elf_Shdr *sechdrs,
117 const char *secstrings,
118 const char *name)
119{
120 unsigned int i;
121
122 for (i = 1; i < hdr->e_shnum; i++)
123 /* Alloc bit cleared means "ignore it." */
124 if ((sechdrs[i].sh_flags & SHF_ALLOC)
125 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
126 return i;
127 return 0;
128}
129
130/* Provided by the linker */
131extern const struct kernel_symbol __start___ksymtab[];
132extern const struct kernel_symbol __stop___ksymtab[];
133extern const struct kernel_symbol __start___ksymtab_gpl[];
134extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800135extern const struct kernel_symbol __start___ksymtab_gpl_future[];
136extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700137extern const struct kernel_symbol __start___ksymtab_gpl_future[];
138extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139extern const unsigned long __start___kcrctab[];
140extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800141extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500142#ifdef CONFIG_UNUSED_SYMBOLS
143extern const struct kernel_symbol __start___ksymtab_unused[];
144extern const struct kernel_symbol __stop___ksymtab_unused[];
145extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
146extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700147extern const unsigned long __start___kcrctab_unused[];
148extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500149#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151#ifndef CONFIG_MODVERSIONS
152#define symversion(base, idx) NULL
153#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800154#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#endif
156
Rusty Russelldafd0942008-07-22 19:24:25 -0500157struct symsearch {
158 const struct kernel_symbol *start, *stop;
159 const unsigned long *crcs;
160 enum {
161 NOT_GPL_ONLY,
162 GPL_ONLY,
163 WILL_BE_GPL_ONLY,
164 } licence;
165 bool unused;
166};
167
168static bool each_symbol_in_section(const struct symsearch *arr,
169 unsigned int arrsize,
170 struct module *owner,
171 bool (*fn)(const struct symsearch *syms,
172 struct module *owner,
173 unsigned int symnum, void *data),
174 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100175{
Rusty Russelldafd0942008-07-22 19:24:25 -0500176 unsigned int i, j;
177
178 for (j = 0; j < arrsize; j++) {
179 for (i = 0; i < arr[j].stop - arr[j].start; i++)
180 if (fn(&arr[j], owner, i, data))
181 return true;
182 }
183
184 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100185}
186
Rusty Russelldafd0942008-07-22 19:24:25 -0500187/* Returns true as soon as fn returns true, otherwise false. */
188static bool each_symbol(bool (*fn)(const struct symsearch *arr,
189 struct module *owner,
190 unsigned int symnum, void *data),
191 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700192{
Rusty Russelldafd0942008-07-22 19:24:25 -0500193 struct module *mod;
194 const struct symsearch arr[] = {
195 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
196 NOT_GPL_ONLY, false },
197 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
198 __start___kcrctab_gpl,
199 GPL_ONLY, false },
200 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
201 __start___kcrctab_gpl_future,
202 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500203#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500204 { __start___ksymtab_unused, __stop___ksymtab_unused,
205 __start___kcrctab_unused,
206 NOT_GPL_ONLY, true },
207 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
208 __start___kcrctab_unused_gpl,
209 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500210#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500211 };
212
213 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
214 return true;
215
216 list_for_each_entry(mod, &modules, list) {
217 struct symsearch arr[] = {
218 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
219 NOT_GPL_ONLY, false },
220 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
221 mod->gpl_crcs,
222 GPL_ONLY, false },
223 { mod->gpl_future_syms,
224 mod->gpl_future_syms + mod->num_gpl_future_syms,
225 mod->gpl_future_crcs,
226 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500227#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500228 { mod->unused_syms,
229 mod->unused_syms + mod->num_unused_syms,
230 mod->unused_crcs,
231 NOT_GPL_ONLY, true },
232 { mod->unused_gpl_syms,
233 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
234 mod->unused_gpl_crcs,
235 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500236#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500237 };
238
239 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
240 return true;
241 }
242 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700243}
244
Rusty Russelldafd0942008-07-22 19:24:25 -0500245struct find_symbol_arg {
246 /* Input */
247 const char *name;
248 bool gplok;
249 bool warn;
250
251 /* Output */
252 struct module *owner;
253 const unsigned long *crc;
254 unsigned long value;
255};
256
257static bool find_symbol_in_section(const struct symsearch *syms,
258 struct module *owner,
259 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500260{
Rusty Russelldafd0942008-07-22 19:24:25 -0500261 struct find_symbol_arg *fsa = data;
262
263 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
264 return false;
265
266 if (!fsa->gplok) {
267 if (syms->licence == GPL_ONLY)
268 return false;
269 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
270 printk(KERN_WARNING "Symbol %s is being used "
271 "by a non-GPL module, which will not "
272 "be allowed in the future\n", fsa->name);
273 printk(KERN_WARNING "Please see the file "
274 "Documentation/feature-removal-schedule.txt "
275 "in the kernel source tree for more details.\n");
276 }
277 }
278
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500279#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500280 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500281 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500282 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500283 printk(KERN_WARNING
284 "This symbol will go away in the future.\n");
285 printk(KERN_WARNING
286 "Please evalute if this is the right api to use and if "
287 "it really is, submit a report the linux kernel "
288 "mailinglist together with submitting your code for "
289 "inclusion.\n");
290 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500291#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500292
293 fsa->owner = owner;
294 fsa->crc = symversion(syms->crcs, symnum);
295 fsa->value = syms->start[symnum].value;
Rusty Russellad9546c2008-05-01 21:14:59 -0500296 return true;
297}
298
Rusty Russellad9546c2008-05-01 21:14:59 -0500299/* Find a symbol, return value, (optional) crc and (optional) module
300 * which owns it */
301static unsigned long find_symbol(const char *name,
302 struct module **owner,
303 const unsigned long **crc,
304 bool gplok,
305 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Rusty Russelldafd0942008-07-22 19:24:25 -0500307 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Rusty Russelldafd0942008-07-22 19:24:25 -0500309 fsa.name = name;
310 fsa.gplok = gplok;
311 fsa.warn = warn;
312
313 if (each_symbol(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500314 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500315 *owner = fsa.owner;
316 if (crc)
317 *crc = fsa.crc;
318 return fsa.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 DEBUGP("Failed to find symbol %s\n", name);
Christoph Lameter88173502008-02-08 04:18:41 -0800322 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
Rusty Russelldafd0942008-07-22 19:24:25 -0500325/* lookup symbol in given range of kernel_symbols */
326static const struct kernel_symbol *lookup_symbol(const char *name,
327 const struct kernel_symbol *start,
328 const struct kernel_symbol *stop)
329{
330 const struct kernel_symbol *ks = start;
331 for (; ks < stop; ks++)
332 if (strcmp(ks->name, name) == 0)
333 return ks;
334 return NULL;
335}
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337/* Search for module by name: must hold module_mutex. */
338static struct module *find_module(const char *name)
339{
340 struct module *mod;
341
342 list_for_each_entry(mod, &modules, list) {
343 if (strcmp(mod->name, name) == 0)
344 return mod;
345 }
346 return NULL;
347}
348
349#ifdef CONFIG_SMP
350/* Number of blocks used and allocated. */
351static unsigned int pcpu_num_used, pcpu_num_allocated;
352/* Size of each block. -ve means used. */
353static int *pcpu_size;
354
355static int split_block(unsigned int i, unsigned short size)
356{
357 /* Reallocation required? */
358 if (pcpu_num_used + 1 > pcpu_num_allocated) {
Pekka Enberg6d4f9c52007-05-08 00:24:58 -0700359 int *new;
360
361 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
362 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (!new)
364 return 0;
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 pcpu_num_allocated *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 pcpu_size = new;
368 }
369
370 /* Insert a new subblock */
371 memmove(&pcpu_size[i+1], &pcpu_size[i],
372 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
373 pcpu_num_used++;
374
375 pcpu_size[i+1] -= size;
376 pcpu_size[i] = size;
377 return 1;
378}
379
380static inline unsigned int block_size(int val)
381{
382 if (val < 0)
383 return -val;
384 return val;
385}
386
Rusty Russell842bbaa2005-08-01 21:11:47 -0700387static void *percpu_modalloc(unsigned long size, unsigned long align,
388 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 unsigned long extra;
391 unsigned int i;
392 void *ptr;
393
Jeremy Fitzhardingeb6e35902007-05-02 19:27:12 +0200394 if (align > PAGE_SIZE) {
395 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
396 name, align, PAGE_SIZE);
397 align = PAGE_SIZE;
Rusty Russell842bbaa2005-08-01 21:11:47 -0700398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 ptr = __per_cpu_start;
401 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
402 /* Extra for alignment requirement. */
403 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
404 BUG_ON(i == 0 && extra != 0);
405
406 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
407 continue;
408
409 /* Transfer extra to previous block. */
410 if (pcpu_size[i-1] < 0)
411 pcpu_size[i-1] -= extra;
412 else
413 pcpu_size[i-1] += extra;
414 pcpu_size[i] -= extra;
415 ptr += extra;
416
417 /* Split block if warranted */
418 if (pcpu_size[i] - size > sizeof(unsigned long))
419 if (!split_block(i, size))
420 return NULL;
421
422 /* Mark allocated */
423 pcpu_size[i] = -pcpu_size[i];
424 return ptr;
425 }
426
427 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
428 size);
429 return NULL;
430}
431
432static void percpu_modfree(void *freeme)
433{
434 unsigned int i;
435 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
436
437 /* First entry is core kernel percpu data. */
438 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
439 if (ptr == freeme) {
440 pcpu_size[i] = -pcpu_size[i];
441 goto free;
442 }
443 }
444 BUG();
445
446 free:
447 /* Merge with previous? */
448 if (pcpu_size[i-1] >= 0) {
449 pcpu_size[i-1] += pcpu_size[i];
450 pcpu_num_used--;
451 memmove(&pcpu_size[i], &pcpu_size[i+1],
452 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
453 i--;
454 }
455 /* Merge with next? */
456 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
457 pcpu_size[i] += pcpu_size[i+1];
458 pcpu_num_used--;
459 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
460 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
461 }
462}
463
464static unsigned int find_pcpusec(Elf_Ehdr *hdr,
465 Elf_Shdr *sechdrs,
466 const char *secstrings)
467{
468 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
469}
470
Mike Travisdd5af902008-01-30 13:33:32 +0100471static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
472{
473 int cpu;
474
475 for_each_possible_cpu(cpu)
476 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
477}
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479static int percpu_modinit(void)
480{
481 pcpu_num_used = 2;
482 pcpu_num_allocated = 2;
483 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
484 GFP_KERNEL);
485 /* Static in-kernel percpu data (used). */
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +0200486 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 /* Free room. */
488 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
489 if (pcpu_size[1] < 0) {
490 printk(KERN_ERR "No per-cpu room for modules.\n");
491 pcpu_num_used = 1;
492 }
493
494 return 0;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700495}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496__initcall(percpu_modinit);
497#else /* ... !CONFIG_SMP */
Rusty Russell842bbaa2005-08-01 21:11:47 -0700498static inline void *percpu_modalloc(unsigned long size, unsigned long align,
499 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 return NULL;
502}
503static inline void percpu_modfree(void *pcpuptr)
504{
505 BUG();
506}
507static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
508 Elf_Shdr *sechdrs,
509 const char *secstrings)
510{
511 return 0;
512}
513static inline void percpu_modcopy(void *pcpudst, const void *src,
514 unsigned long size)
515{
516 /* pcpusec should be 0, and size of that section should be 0. */
517 BUG_ON(size != 0);
518}
519#endif /* CONFIG_SMP */
520
Matt Domschc988d2b2005-06-23 22:05:15 -0700521#define MODINFO_ATTR(field) \
522static void setup_modinfo_##field(struct module *mod, const char *s) \
523{ \
524 mod->field = kstrdup(s, GFP_KERNEL); \
525} \
526static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
527 struct module *mod, char *buffer) \
528{ \
529 return sprintf(buffer, "%s\n", mod->field); \
530} \
531static int modinfo_##field##_exists(struct module *mod) \
532{ \
533 return mod->field != NULL; \
534} \
535static void free_modinfo_##field(struct module *mod) \
536{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700537 kfree(mod->field); \
538 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700539} \
540static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900541 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700542 .show = show_modinfo_##field, \
543 .setup = setup_modinfo_##field, \
544 .test = modinfo_##field##_exists, \
545 .free = free_modinfo_##field, \
546};
547
548MODINFO_ATTR(version);
549MODINFO_ATTR(srcversion);
550
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100551static char last_unloaded_module[MODULE_NAME_LEN+1];
552
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800553#ifdef CONFIG_MODULE_UNLOAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554/* Init the unload section of the module. */
555static void module_unload_init(struct module *mod)
556{
557 unsigned int i;
558
559 INIT_LIST_HEAD(&mod->modules_which_use_me);
560 for (i = 0; i < NR_CPUS; i++)
561 local_set(&mod->ref[i].count, 0);
562 /* Hold reference count during initialization. */
Ingo Molnar39c715b2005-06-21 17:14:34 -0700563 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /* Backwards compatibility macros put refcount during init. */
565 mod->waiter = current;
566}
567
568/* modules using other modules */
569struct module_use
570{
571 struct list_head list;
572 struct module *module_which_uses;
573};
574
575/* Does a already use b? */
576static int already_uses(struct module *a, struct module *b)
577{
578 struct module_use *use;
579
580 list_for_each_entry(use, &b->modules_which_use_me, list) {
581 if (use->module_which_uses == a) {
582 DEBUGP("%s uses %s!\n", a->name, b->name);
583 return 1;
584 }
585 }
586 DEBUGP("%s does not use %s!\n", a->name, b->name);
587 return 0;
588}
589
590/* Module a uses b */
591static int use_module(struct module *a, struct module *b)
592{
593 struct module_use *use;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500594 int no_warn, err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (b == NULL || already_uses(a, b)) return 1;
597
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500598 /* If we're interrupted or time out, we fail. */
599 if (wait_event_interruptible_timeout(
600 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
601 30 * HZ) <= 0) {
602 printk("%s: gave up waiting for init of module %s.\n",
603 a->name, b->name);
604 return 0;
605 }
606
607 /* If strong_try_module_get() returned a different error, we fail. */
608 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return 0;
610
611 DEBUGP("Allocating new usage for %s.\n", a->name);
612 use = kmalloc(sizeof(*use), GFP_ATOMIC);
613 if (!use) {
614 printk("%s: out of memory loading\n", a->name);
615 module_put(b);
616 return 0;
617 }
618
619 use->module_which_uses = a;
620 list_add(&use->list, &b->modules_which_use_me);
Kay Sievers270a6c42007-01-18 13:26:15 +0100621 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return 1;
623}
624
625/* Clear the unload stuff of the module. */
626static void module_unload_free(struct module *mod)
627{
628 struct module *i;
629
630 list_for_each_entry(i, &modules, list) {
631 struct module_use *use;
632
633 list_for_each_entry(use, &i->modules_which_use_me, list) {
634 if (use->module_which_uses == mod) {
635 DEBUGP("%s unusing %s\n", mod->name, i->name);
636 module_put(i);
637 list_del(&use->list);
638 kfree(use);
Kay Sievers270a6c42007-01-18 13:26:15 +0100639 sysfs_remove_link(i->holders_dir, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 /* There can be at most one match. */
641 break;
642 }
643 }
644 }
645}
646
647#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800648static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
650 int ret = (flags & O_TRUNC);
651 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800652 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return ret;
654}
655#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800656static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 return 0;
659}
660#endif /* CONFIG_MODULE_FORCE_UNLOAD */
661
662struct stopref
663{
664 struct module *mod;
665 int flags;
666 int *forced;
667};
668
669/* Whole machine is stopped with interrupts off when this runs. */
670static int __try_stop_module(void *_sref)
671{
672 struct stopref *sref = _sref;
673
Rusty Russellda39ba52008-07-22 19:24:25 -0500674 /* If it's not unused, quit unless we're forcing. */
675 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800676 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return -EWOULDBLOCK;
678 }
679
680 /* Mark it as dying. */
681 sref->mod->state = MODULE_STATE_GOING;
682 return 0;
683}
684
685static int try_stop_module(struct module *mod, int flags, int *forced)
686{
Rusty Russellda39ba52008-07-22 19:24:25 -0500687 if (flags & O_NONBLOCK) {
688 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Rusty Russellda39ba52008-07-22 19:24:25 -0500690 return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
691 } else {
692 /* We don't need to stop the machine for this. */
693 mod->state = MODULE_STATE_GOING;
694 synchronize_sched();
695 return 0;
696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
699unsigned int module_refcount(struct module *mod)
700{
701 unsigned int i, total = 0;
702
703 for (i = 0; i < NR_CPUS; i++)
704 total += local_read(&mod->ref[i].count);
705 return total;
706}
707EXPORT_SYMBOL(module_refcount);
708
709/* This exists whether we can unload or not */
710static void free_module(struct module *mod);
711
712static void wait_for_zero_refcount(struct module *mod)
713{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500714 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800715 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 for (;;) {
717 DEBUGP("Looking at refcount...\n");
718 set_current_state(TASK_UNINTERRUPTIBLE);
719 if (module_refcount(mod) == 0)
720 break;
721 schedule();
722 }
723 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800724 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800727asmlinkage long
728sys_delete_module(const char __user *name_user, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
730 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800731 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 int ret, forced = 0;
733
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800734 if (!capable(CAP_SYS_MODULE))
735 return -EPERM;
736
737 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
738 return -EFAULT;
739 name[MODULE_NAME_LEN-1] = '\0';
740
Ashutosh Naik6389a382006-03-23 03:00:46 -0800741 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return -EINTR;
743
744 mod = find_module(name);
745 if (!mod) {
746 ret = -ENOENT;
747 goto out;
748 }
749
750 if (!list_empty(&mod->modules_which_use_me)) {
751 /* Other modules depend on us: get rid of them first. */
752 ret = -EWOULDBLOCK;
753 goto out;
754 }
755
756 /* Doing init or already dying? */
757 if (mod->state != MODULE_STATE_LIVE) {
758 /* FIXME: if (force), slam module count and wake up
759 waiter --RR */
760 DEBUGP("%s already dying\n", mod->name);
761 ret = -EBUSY;
762 goto out;
763 }
764
765 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700766 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800767 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (!forced) {
769 /* This module can't be removed */
770 ret = -EBUSY;
771 goto out;
772 }
773 }
774
775 /* Set this up before setting mod->state */
776 mod->waiter = current;
777
778 /* Stop the machine so refcounts can't move and disable module. */
779 ret = try_stop_module(mod, flags, &forced);
780 if (ret != 0)
781 goto out;
782
783 /* Never wait if forced. */
784 if (!forced && module_refcount(mod) != 0)
785 wait_for_zero_refcount(mod);
786
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200787 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 /* Final destruction now noone is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200789 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200791 blocking_notifier_call_chain(&module_notify_list,
792 MODULE_STATE_GOING, mod);
793 mutex_lock(&module_mutex);
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100794 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500795 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 free_module(mod);
797
798 out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800799 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return ret;
801}
802
803static void print_unload_info(struct seq_file *m, struct module *mod)
804{
805 struct module_use *use;
806 int printed_something = 0;
807
808 seq_printf(m, " %u ", module_refcount(mod));
809
810 /* Always include a trailing , so userspace can differentiate
811 between this and the old multi-field proc format. */
812 list_for_each_entry(use, &mod->modules_which_use_me, list) {
813 printed_something = 1;
814 seq_printf(m, "%s,", use->module_which_uses->name);
815 }
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (mod->init != NULL && mod->exit == NULL) {
818 printed_something = 1;
819 seq_printf(m, "[permanent],");
820 }
821
822 if (!printed_something)
823 seq_printf(m, "-");
824}
825
826void __symbol_put(const char *symbol)
827{
828 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Rusty Russell24da1cb2007-07-15 23:41:46 -0700830 preempt_disable();
Rusty Russellad9546c2008-05-01 21:14:59 -0500831 if (IS_ERR_VALUE(find_symbol(symbol, &owner, NULL, true, false)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 BUG();
833 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700834 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836EXPORT_SYMBOL(__symbol_put);
837
838void symbol_put_addr(void *addr)
839{
Trent Piepho5e376612006-05-15 09:44:06 -0700840 struct module *modaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Trent Piepho5e376612006-05-15 09:44:06 -0700842 if (core_kernel_text((unsigned long)addr))
843 return;
844
845 if (!(modaddr = module_text_address((unsigned long)addr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 BUG();
Trent Piepho5e376612006-05-15 09:44:06 -0700847 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849EXPORT_SYMBOL_GPL(symbol_put_addr);
850
851static ssize_t show_refcnt(struct module_attribute *mattr,
852 struct module *mod, char *buffer)
853{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400854 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
857static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900858 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 .show = show_refcnt,
860};
861
Al Virof6a57032006-10-18 01:47:25 -0400862void module_put(struct module *module)
863{
864 if (module) {
865 unsigned int cpu = get_cpu();
866 local_dec(&module->ref[cpu].count);
867 /* Maybe they're waiting for us to drop reference? */
868 if (unlikely(!module_is_live(module)))
869 wake_up_process(module->waiter);
870 put_cpu();
871 }
872}
873EXPORT_SYMBOL(module_put);
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875#else /* !CONFIG_MODULE_UNLOAD */
876static void print_unload_info(struct seq_file *m, struct module *mod)
877{
878 /* We don't know the usage count, or what modules are using. */
879 seq_printf(m, " - -");
880}
881
882static inline void module_unload_free(struct module *mod)
883{
884}
885
886static inline int use_module(struct module *a, struct module *b)
887{
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500888 return strong_try_module_get(b) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
891static inline void module_unload_init(struct module *mod)
892{
893}
894#endif /* CONFIG_MODULE_UNLOAD */
895
Kay Sievers1f717402006-11-24 12:15:25 +0100896static ssize_t show_initstate(struct module_attribute *mattr,
897 struct module *mod, char *buffer)
898{
899 const char *state = "unknown";
900
901 switch (mod->state) {
902 case MODULE_STATE_LIVE:
903 state = "live";
904 break;
905 case MODULE_STATE_COMING:
906 state = "coming";
907 break;
908 case MODULE_STATE_GOING:
909 state = "going";
910 break;
911 }
912 return sprintf(buffer, "%s\n", state);
913}
914
915static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900916 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100917 .show = show_initstate,
918};
919
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800920static struct module_attribute *modinfo_attrs[] = {
921 &modinfo_version,
922 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100923 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800924#ifdef CONFIG_MODULE_UNLOAD
925 &refcnt,
926#endif
927 NULL,
928};
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930static const char vermagic[] = VERMAGIC_STRING;
931
Linus Torvalds826e4502008-05-04 17:04:16 -0700932static int try_to_force_load(struct module *mod, const char *symname)
933{
934#ifdef CONFIG_MODULE_FORCE_LOAD
935 if (!(tainted & TAINT_FORCED_MODULE))
936 printk("%s: no version for \"%s\" found: kernel tainted.\n",
937 mod->name, symname);
938 add_taint_module(mod, TAINT_FORCED_MODULE);
939 return 0;
940#else
941 return -ENOEXEC;
942#endif
943}
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945#ifdef CONFIG_MODVERSIONS
946static int check_version(Elf_Shdr *sechdrs,
947 unsigned int versindex,
948 const char *symname,
949 struct module *mod,
950 const unsigned long *crc)
951{
952 unsigned int i, num_versions;
953 struct modversion_info *versions;
954
955 /* Exporting module didn't supply crcs? OK, we're already tainted. */
956 if (!crc)
957 return 1;
958
Rusty Russella5dd6972008-05-09 16:24:21 +1000959 /* No versions at all? modprobe --force does this. */
960 if (versindex == 0)
961 return try_to_force_load(mod, symname) == 0;
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 versions = (void *) sechdrs[versindex].sh_addr;
964 num_versions = sechdrs[versindex].sh_size
965 / sizeof(struct modversion_info);
966
967 for (i = 0; i < num_versions; i++) {
968 if (strcmp(versions[i].name, symname) != 0)
969 continue;
970
971 if (versions[i].crc == *crc)
972 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 DEBUGP("Found checksum %lX vs module %lX\n",
974 *crc, versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -0700975 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
Linus Torvalds826e4502008-05-04 17:04:16 -0700977
Rusty Russella5dd6972008-05-09 16:24:21 +1000978 printk(KERN_WARNING "%s: no symbol version for %s\n",
979 mod->name, symname);
980 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -0700981
982bad_version:
983 printk("%s: disagrees about version of symbol %s\n",
984 mod->name, symname);
985 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987
988static inline int check_modstruct_version(Elf_Shdr *sechdrs,
989 unsigned int versindex,
990 struct module *mod)
991{
992 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Rusty Russellad9546c2008-05-01 21:14:59 -0500994 if (IS_ERR_VALUE(find_symbol("struct_module", NULL, &crc, true, false)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 BUG();
Rusty Russellad9546c2008-05-01 21:14:59 -0500996 return check_version(sechdrs, versindex, "struct_module", mod, crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
Rusty Russell91e37a72008-05-09 16:25:28 +1000999/* First part is kernel version, which we ignore if module has crcs. */
1000static inline int same_magic(const char *amagic, const char *bmagic,
1001 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Rusty Russell91e37a72008-05-09 16:25:28 +10001003 if (has_crcs) {
1004 amagic += strcspn(amagic, " ");
1005 bmagic += strcspn(bmagic, " ");
1006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return strcmp(amagic, bmagic) == 0;
1008}
1009#else
1010static inline int check_version(Elf_Shdr *sechdrs,
1011 unsigned int versindex,
1012 const char *symname,
1013 struct module *mod,
1014 const unsigned long *crc)
1015{
1016 return 1;
1017}
1018
1019static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1020 unsigned int versindex,
1021 struct module *mod)
1022{
1023 return 1;
1024}
1025
Rusty Russell91e37a72008-05-09 16:25:28 +10001026static inline int same_magic(const char *amagic, const char *bmagic,
1027 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
1029 return strcmp(amagic, bmagic) == 0;
1030}
1031#endif /* CONFIG_MODVERSIONS */
1032
1033/* Resolve a symbol for this module. I.e. if we find one, record usage.
1034 Must be holding module_mutex. */
1035static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
1036 unsigned int versindex,
1037 const char *name,
1038 struct module *mod)
1039{
1040 struct module *owner;
1041 unsigned long ret;
1042 const unsigned long *crc;
1043
Rusty Russellad9546c2008-05-01 21:14:59 -05001044 ret = find_symbol(name, &owner, &crc,
1045 !(mod->taints & TAINT_PROPRIETARY_MODULE), true);
Christoph Lameter88173502008-02-08 04:18:41 -08001046 if (!IS_ERR_VALUE(ret)) {
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001047 /* use_module can fail due to OOM,
1048 or module initialization or unloading */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1050 !use_module(mod, owner))
Christoph Lameter88173502008-02-08 04:18:41 -08001051 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return ret;
1054}
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056/*
1057 * /sys/module/foo/sections stuff
1058 * J. Corbet <corbet@lwn.net>
1059 */
Kay Sievers120fc3d2008-02-21 00:33:20 +01001060#if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
Rusty Russella58730c2008-03-13 09:03:44 +00001061struct module_sect_attr
1062{
1063 struct module_attribute mattr;
1064 char *name;
1065 unsigned long address;
1066};
1067
1068struct module_sect_attrs
1069{
1070 struct attribute_group grp;
1071 unsigned int nsections;
1072 struct module_sect_attr attrs[0];
1073};
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075static ssize_t module_sect_show(struct module_attribute *mattr,
1076 struct module *mod, char *buf)
1077{
1078 struct module_sect_attr *sattr =
1079 container_of(mattr, struct module_sect_attr, mattr);
1080 return sprintf(buf, "0x%lx\n", sattr->address);
1081}
1082
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001083static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1084{
Rusty Russella58730c2008-03-13 09:03:44 +00001085 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001086
1087 for (section = 0; section < sect_attrs->nsections; section++)
1088 kfree(sect_attrs->attrs[section].name);
1089 kfree(sect_attrs);
1090}
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092static void add_sect_attrs(struct module *mod, unsigned int nsect,
1093 char *secstrings, Elf_Shdr *sechdrs)
1094{
1095 unsigned int nloaded = 0, i, size[2];
1096 struct module_sect_attrs *sect_attrs;
1097 struct module_sect_attr *sattr;
1098 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 /* Count loaded sections and allocate structures */
1101 for (i = 0; i < nsect; i++)
1102 if (sechdrs[i].sh_flags & SHF_ALLOC)
1103 nloaded++;
1104 size[0] = ALIGN(sizeof(*sect_attrs)
1105 + nloaded * sizeof(sect_attrs->attrs[0]),
1106 sizeof(sect_attrs->grp.attrs[0]));
1107 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001108 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1109 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return;
1111
1112 /* Setup section attributes. */
1113 sect_attrs->grp.name = "sections";
1114 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1115
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001116 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 sattr = &sect_attrs->attrs[0];
1118 gattr = &sect_attrs->grp.attrs[0];
1119 for (i = 0; i < nsect; i++) {
1120 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1121 continue;
1122 sattr->address = sechdrs[i].sh_addr;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001123 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1124 GFP_KERNEL);
1125 if (sattr->name == NULL)
1126 goto out;
1127 sect_attrs->nsections++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 sattr->mattr.show = module_sect_show;
1129 sattr->mattr.store = NULL;
1130 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 sattr->mattr.attr.mode = S_IRUGO;
1132 *(gattr++) = &(sattr++)->mattr.attr;
1133 }
1134 *gattr = NULL;
1135
1136 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1137 goto out;
1138
1139 mod->sect_attrs = sect_attrs;
1140 return;
1141 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001142 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
1145static void remove_sect_attrs(struct module *mod)
1146{
1147 if (mod->sect_attrs) {
1148 sysfs_remove_group(&mod->mkobj.kobj,
1149 &mod->sect_attrs->grp);
1150 /* We are positive that no one is using any sect attrs
1151 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001152 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 mod->sect_attrs = NULL;
1154 }
1155}
1156
Roland McGrath6d760132007-10-16 23:26:40 -07001157/*
1158 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1159 */
1160
1161struct module_notes_attrs {
1162 struct kobject *dir;
1163 unsigned int notes;
1164 struct bin_attribute attrs[0];
1165};
1166
1167static ssize_t module_notes_read(struct kobject *kobj,
1168 struct bin_attribute *bin_attr,
1169 char *buf, loff_t pos, size_t count)
1170{
1171 /*
1172 * The caller checked the pos and count against our size.
1173 */
1174 memcpy(buf, bin_attr->private + pos, count);
1175 return count;
1176}
1177
1178static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1179 unsigned int i)
1180{
1181 if (notes_attrs->dir) {
1182 while (i-- > 0)
1183 sysfs_remove_bin_file(notes_attrs->dir,
1184 &notes_attrs->attrs[i]);
1185 kobject_del(notes_attrs->dir);
1186 }
1187 kfree(notes_attrs);
1188}
1189
1190static void add_notes_attrs(struct module *mod, unsigned int nsect,
1191 char *secstrings, Elf_Shdr *sechdrs)
1192{
1193 unsigned int notes, loaded, i;
1194 struct module_notes_attrs *notes_attrs;
1195 struct bin_attribute *nattr;
1196
1197 /* Count notes sections and allocate structures. */
1198 notes = 0;
1199 for (i = 0; i < nsect; i++)
1200 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1201 (sechdrs[i].sh_type == SHT_NOTE))
1202 ++notes;
1203
1204 if (notes == 0)
1205 return;
1206
1207 notes_attrs = kzalloc(sizeof(*notes_attrs)
1208 + notes * sizeof(notes_attrs->attrs[0]),
1209 GFP_KERNEL);
1210 if (notes_attrs == NULL)
1211 return;
1212
1213 notes_attrs->notes = notes;
1214 nattr = &notes_attrs->attrs[0];
1215 for (loaded = i = 0; i < nsect; ++i) {
1216 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1217 continue;
1218 if (sechdrs[i].sh_type == SHT_NOTE) {
1219 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1220 nattr->attr.mode = S_IRUGO;
1221 nattr->size = sechdrs[i].sh_size;
1222 nattr->private = (void *) sechdrs[i].sh_addr;
1223 nattr->read = module_notes_read;
1224 ++nattr;
1225 }
1226 ++loaded;
1227 }
1228
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001229 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001230 if (!notes_attrs->dir)
1231 goto out;
1232
1233 for (i = 0; i < notes; ++i)
1234 if (sysfs_create_bin_file(notes_attrs->dir,
1235 &notes_attrs->attrs[i]))
1236 goto out;
1237
1238 mod->notes_attrs = notes_attrs;
1239 return;
1240
1241 out:
1242 free_notes_attrs(notes_attrs, i);
1243}
1244
1245static void remove_notes_attrs(struct module *mod)
1246{
1247 if (mod->notes_attrs)
1248 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1249}
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1254 char *sectstrings, Elf_Shdr *sechdrs)
1255{
1256}
1257
1258static inline void remove_sect_attrs(struct module *mod)
1259{
1260}
Roland McGrath6d760132007-10-16 23:26:40 -07001261
1262static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1263 char *sectstrings, Elf_Shdr *sechdrs)
1264{
1265}
1266
1267static inline void remove_notes_attrs(struct module *mod)
1268{
1269}
Kay Sievers120fc3d2008-02-21 00:33:20 +01001270#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Randy Dunlapef665c12007-02-13 15:19:06 -08001272#ifdef CONFIG_SYSFS
1273int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001274{
1275 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001276 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001277 int error = 0;
1278 int i;
1279
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001280 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1281 (ARRAY_SIZE(modinfo_attrs) + 1)),
1282 GFP_KERNEL);
1283 if (!mod->modinfo_attrs)
1284 return -ENOMEM;
1285
1286 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001287 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1288 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001289 (attr->test && attr->test(mod))) {
1290 memcpy(temp_attr, attr, sizeof(*temp_attr));
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001291 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1292 ++temp_attr;
1293 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001294 }
1295 return error;
1296}
1297
Randy Dunlapef665c12007-02-13 15:19:06 -08001298void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001299{
1300 struct module_attribute *attr;
1301 int i;
1302
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001303 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1304 /* pick a field to test for end of list */
1305 if (!attr->attr.name)
1306 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001307 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001308 if (attr->free)
1309 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001310 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001311 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001312}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Randy Dunlapef665c12007-02-13 15:19:06 -08001314int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
1316 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001317 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001319 if (!module_sysfs_initialized) {
1320 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001321 mod->name);
1322 err = -EINVAL;
1323 goto out;
1324 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001325
1326 kobj = kset_find_obj(module_kset, mod->name);
1327 if (kobj) {
1328 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1329 kobject_put(kobj);
1330 err = -EINVAL;
1331 goto out;
1332 }
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001335
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001336 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1337 mod->mkobj.kobj.kset = module_kset;
1338 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1339 "%s", mod->name);
1340 if (err)
1341 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001342
Kay Sievers97c146e2007-11-29 23:46:11 +01001343 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001344out:
1345 return err;
1346}
1347
Randy Dunlapef665c12007-02-13 15:19:06 -08001348int mod_sysfs_setup(struct module *mod,
Kay Sievers270a6c42007-01-18 13:26:15 +01001349 struct kernel_param *kparam,
1350 unsigned int num_params)
1351{
1352 int err;
1353
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001354 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001355 if (!mod->holders_dir) {
1356 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001357 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001358 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 err = module_param_sysfs_setup(mod, kparam, num_params);
1361 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001362 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Matt Domschc988d2b2005-06-23 22:05:15 -07001364 err = module_add_modinfo_attrs(mod);
1365 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001366 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001367
Kay Sieverse17e0f52006-11-24 12:15:25 +01001368 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return 0;
1370
Kay Sieverse17e0f52006-11-24 12:15:25 +01001371out_unreg_param:
1372 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001373out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001374 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001375out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001376 kobject_put(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 return err;
1378}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001379
1380static void mod_sysfs_fini(struct module *mod)
1381{
1382 kobject_put(&mod->mkobj.kobj);
1383}
1384
1385#else /* CONFIG_SYSFS */
1386
1387static void mod_sysfs_fini(struct module *mod)
1388{
1389}
1390
1391#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393static void mod_kobject_remove(struct module *mod)
1394{
Matt Domschc988d2b2005-06-23 22:05:15 -07001395 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001397 kobject_put(mod->mkobj.drivers_dir);
1398 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001399 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
1401
1402/*
Rusty Russellbb9d3d52008-01-29 17:13:21 -05001403 * link the module with the whole machine is stopped with interrupts off
1404 * - this defends against kallsyms not taking locks
1405 */
1406static int __link_module(void *_mod)
1407{
1408 struct module *mod = _mod;
1409 list_add(&mod->list, &modules);
1410 return 0;
1411}
1412
1413/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 * unlink the module with the whole machine is stopped with interrupts off
1415 * - this defends against kallsyms not taking locks
1416 */
1417static int __unlink_module(void *_mod)
1418{
1419 struct module *mod = _mod;
1420 list_del(&mod->list);
1421 return 0;
1422}
1423
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001424/* Free a module, remove from lists, etc (must hold module_mutex). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425static void free_module(struct module *mod)
1426{
1427 /* Delete from various lists */
1428 stop_machine_run(__unlink_module, mod, NR_CPUS);
Roland McGrath6d760132007-10-16 23:26:40 -07001429 remove_notes_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 remove_sect_attrs(mod);
1431 mod_kobject_remove(mod);
1432
Jan Beulich4552d5d2006-06-26 13:57:28 +02001433 unwind_remove_table(mod->unwind_info, 0);
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 /* Arch-specific cleanup. */
1436 module_arch_cleanup(mod);
1437
1438 /* Module unload stuff */
1439 module_unload_free(mod);
1440
1441 /* This may be NULL, but that's OK */
1442 module_free(mod, mod->module_init);
1443 kfree(mod->args);
1444 if (mod->percpu)
1445 percpu_modfree(mod->percpu);
1446
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001447 /* Free lock-classes: */
1448 lockdep_free_key_range(mod->module_core, mod->core_size);
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 /* Finally, free the core (containing the module structure) */
1451 module_free(mod, mod->module_core);
1452}
1453
1454void *__symbol_get(const char *symbol)
1455{
1456 struct module *owner;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001457 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Rusty Russell24da1cb2007-07-15 23:41:46 -07001459 preempt_disable();
Rusty Russellad9546c2008-05-01 21:14:59 -05001460 value = find_symbol(symbol, &owner, NULL, true, true);
Christoph Lameter88173502008-02-08 04:18:41 -08001461 if (IS_ERR_VALUE(value))
1462 value = 0;
1463 else if (strong_try_module_get(owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 value = 0;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001465 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 return (void *)value;
1468}
1469EXPORT_SYMBOL_GPL(__symbol_get);
1470
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001471/*
1472 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001473 * in the kernel or in some other module's exported symbol table.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001474 */
1475static int verify_export_symbols(struct module *mod)
1476{
Rusty Russellb2111042008-05-01 21:15:00 -05001477 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001478 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001479 const struct kernel_symbol *s;
1480 struct {
1481 const struct kernel_symbol *sym;
1482 unsigned int num;
1483 } arr[] = {
1484 { mod->syms, mod->num_syms },
1485 { mod->gpl_syms, mod->num_gpl_syms },
1486 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001487#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001488 { mod->unused_syms, mod->num_unused_syms },
1489 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001490#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001491 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001492
Rusty Russellb2111042008-05-01 21:15:00 -05001493 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1494 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1495 if (!IS_ERR_VALUE(find_symbol(s->name, &owner,
1496 NULL, true, false))) {
1497 printk(KERN_ERR
1498 "%s: exports duplicate symbol %s"
1499 " (owned by %s)\n",
1500 mod->name, s->name, module_name(owner));
1501 return -ENOEXEC;
1502 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001503 }
Rusty Russellb2111042008-05-01 21:15:00 -05001504 }
1505 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001506}
1507
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001508/* Change all symbols so that st_value encodes the pointer directly. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509static int simplify_symbols(Elf_Shdr *sechdrs,
1510 unsigned int symindex,
1511 const char *strtab,
1512 unsigned int versindex,
1513 unsigned int pcpuindex,
1514 struct module *mod)
1515{
1516 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1517 unsigned long secbase;
1518 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1519 int ret = 0;
1520
1521 for (i = 1; i < n; i++) {
1522 switch (sym[i].st_shndx) {
1523 case SHN_COMMON:
1524 /* We compiled with -fno-common. These are not
1525 supposed to happen. */
1526 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1527 printk("%s: please compile with -fno-common\n",
1528 mod->name);
1529 ret = -ENOEXEC;
1530 break;
1531
1532 case SHN_ABS:
1533 /* Don't need to do anything */
1534 DEBUGP("Absolute symbol: 0x%08lx\n",
1535 (long)sym[i].st_value);
1536 break;
1537
1538 case SHN_UNDEF:
1539 sym[i].st_value
1540 = resolve_symbol(sechdrs, versindex,
1541 strtab + sym[i].st_name, mod);
1542
1543 /* Ok if resolved. */
Christoph Lameter88173502008-02-08 04:18:41 -08001544 if (!IS_ERR_VALUE(sym[i].st_value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 break;
1546 /* Ok if weak. */
1547 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1548 break;
1549
1550 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1551 mod->name, strtab + sym[i].st_name);
1552 ret = -ENOENT;
1553 break;
1554
1555 default:
1556 /* Divert to percpu allocation if a percpu var. */
1557 if (sym[i].st_shndx == pcpuindex)
1558 secbase = (unsigned long)mod->percpu;
1559 else
1560 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1561 sym[i].st_value += secbase;
1562 break;
1563 }
1564 }
1565
1566 return ret;
1567}
1568
1569/* Update size with this section: return offset. */
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05001570static long get_offset(unsigned int *size, Elf_Shdr *sechdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
1572 long ret;
1573
1574 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1575 *size = ret + sechdr->sh_size;
1576 return ret;
1577}
1578
1579/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1580 might -- code, read-only data, read-write data, small data. Tally
1581 sizes, and place the offsets into sh_entsize fields: high bit means it
1582 belongs in init. */
1583static void layout_sections(struct module *mod,
1584 const Elf_Ehdr *hdr,
1585 Elf_Shdr *sechdrs,
1586 const char *secstrings)
1587{
1588 static unsigned long const masks[][2] = {
1589 /* NOTE: all executable code must be the first section
1590 * in this array; otherwise modify the text_size
1591 * finder in the two loops below */
1592 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1593 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1594 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1595 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1596 };
1597 unsigned int m, i;
1598
1599 for (i = 0; i < hdr->e_shnum; i++)
1600 sechdrs[i].sh_entsize = ~0UL;
1601
1602 DEBUGP("Core section allocation order:\n");
1603 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1604 for (i = 0; i < hdr->e_shnum; ++i) {
1605 Elf_Shdr *s = &sechdrs[i];
1606
1607 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1608 || (s->sh_flags & masks[m][1])
1609 || s->sh_entsize != ~0UL
1610 || strncmp(secstrings + s->sh_name,
1611 ".init", 5) == 0)
1612 continue;
1613 s->sh_entsize = get_offset(&mod->core_size, s);
1614 DEBUGP("\t%s\n", secstrings + s->sh_name);
1615 }
1616 if (m == 0)
1617 mod->core_text_size = mod->core_size;
1618 }
1619
1620 DEBUGP("Init section allocation order:\n");
1621 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1622 for (i = 0; i < hdr->e_shnum; ++i) {
1623 Elf_Shdr *s = &sechdrs[i];
1624
1625 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1626 || (s->sh_flags & masks[m][1])
1627 || s->sh_entsize != ~0UL
1628 || strncmp(secstrings + s->sh_name,
1629 ".init", 5) != 0)
1630 continue;
1631 s->sh_entsize = (get_offset(&mod->init_size, s)
1632 | INIT_OFFSET_MASK);
1633 DEBUGP("\t%s\n", secstrings + s->sh_name);
1634 }
1635 if (m == 0)
1636 mod->init_text_size = mod->init_size;
1637 }
1638}
1639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640static void set_license(struct module *mod, const char *license)
1641{
1642 if (!license)
1643 license = "unspecified";
1644
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001645 if (!license_is_gpl_compatible(license)) {
1646 if (!(tainted & TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001647 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001648 "kernel.\n", mod->name, license);
1649 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 }
1651}
1652
1653/* Parse tag=value strings from .modinfo section */
1654static char *next_string(char *string, unsigned long *secsize)
1655{
1656 /* Skip non-zero chars */
1657 while (string[0]) {
1658 string++;
1659 if ((*secsize)-- <= 1)
1660 return NULL;
1661 }
1662
1663 /* Skip any zero padding. */
1664 while (!string[0]) {
1665 string++;
1666 if ((*secsize)-- <= 1)
1667 return NULL;
1668 }
1669 return string;
1670}
1671
1672static char *get_modinfo(Elf_Shdr *sechdrs,
1673 unsigned int info,
1674 const char *tag)
1675{
1676 char *p;
1677 unsigned int taglen = strlen(tag);
1678 unsigned long size = sechdrs[info].sh_size;
1679
1680 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1681 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1682 return p + taglen + 1;
1683 }
1684 return NULL;
1685}
1686
Matt Domschc988d2b2005-06-23 22:05:15 -07001687static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1688 unsigned int infoindex)
1689{
1690 struct module_attribute *attr;
1691 int i;
1692
1693 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1694 if (attr->setup)
1695 attr->setup(mod,
1696 get_modinfo(sechdrs,
1697 infoindex,
1698 attr->attr.name));
1699 }
1700}
Matt Domschc988d2b2005-06-23 22:05:15 -07001701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702#ifdef CONFIG_KALLSYMS
Alexey Dobriyanea078902007-05-08 00:28:39 -07001703static int is_exported(const char *name, const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001705 if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
1706 return 1;
1707 else
Jesper Juhlf867d2a2006-06-25 05:47:09 -07001708 if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 return 1;
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001710 else
1711 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712}
1713
1714/* As per nm */
1715static char elf_type(const Elf_Sym *sym,
1716 Elf_Shdr *sechdrs,
1717 const char *secstrings,
1718 struct module *mod)
1719{
1720 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1721 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1722 return 'v';
1723 else
1724 return 'w';
1725 }
1726 if (sym->st_shndx == SHN_UNDEF)
1727 return 'U';
1728 if (sym->st_shndx == SHN_ABS)
1729 return 'a';
1730 if (sym->st_shndx >= SHN_LORESERVE)
1731 return '?';
1732 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1733 return 't';
1734 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1735 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1736 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1737 return 'r';
1738 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1739 return 'g';
1740 else
1741 return 'd';
1742 }
1743 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1744 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1745 return 's';
1746 else
1747 return 'b';
1748 }
1749 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1750 ".debug", strlen(".debug")) == 0)
1751 return 'n';
1752 return '?';
1753}
1754
1755static void add_kallsyms(struct module *mod,
1756 Elf_Shdr *sechdrs,
1757 unsigned int symindex,
1758 unsigned int strindex,
1759 const char *secstrings)
1760{
1761 unsigned int i;
1762
1763 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1764 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1765 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1766
1767 /* Set types up while we still have access to sections. */
1768 for (i = 0; i < mod->num_symtab; i++)
1769 mod->symtab[i].st_info
1770 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1771}
1772#else
1773static inline void add_kallsyms(struct module *mod,
1774 Elf_Shdr *sechdrs,
1775 unsigned int symindex,
1776 unsigned int strindex,
1777 const char *secstrings)
1778{
1779}
1780#endif /* CONFIG_KALLSYMS */
1781
1782/* Allocate and load the module: note that size of section 0 is always
1783 zero, and we rely on this for optional sections. */
1784static struct module *load_module(void __user *umod,
1785 unsigned long len,
1786 const char __user *uargs)
1787{
1788 Elf_Ehdr *hdr;
1789 Elf_Shdr *sechdrs;
1790 char *secstrings, *args, *modmagic, *strtab = NULL;
Andrew Morton84860f92006-06-28 04:26:46 -07001791 unsigned int i;
1792 unsigned int symindex = 0;
1793 unsigned int strindex = 0;
1794 unsigned int setupindex;
1795 unsigned int exindex;
1796 unsigned int exportindex;
1797 unsigned int modindex;
1798 unsigned int obsparmindex;
1799 unsigned int infoindex;
1800 unsigned int gplindex;
1801 unsigned int crcindex;
1802 unsigned int gplcrcindex;
1803 unsigned int versindex;
1804 unsigned int pcpuindex;
1805 unsigned int gplfutureindex;
1806 unsigned int gplfuturecrcindex;
1807 unsigned int unwindex = 0;
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001808#ifdef CONFIG_UNUSED_SYMBOLS
Andrew Morton84860f92006-06-28 04:26:46 -07001809 unsigned int unusedindex;
1810 unsigned int unusedcrcindex;
1811 unsigned int unusedgplindex;
1812 unsigned int unusedgplcrcindex;
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001813#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07001814 unsigned int markersindex;
1815 unsigned int markersstringsindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 struct module *mod;
1817 long err = 0;
1818 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1819 struct exception_table_entry *extable;
Thomas Koeller378bac82005-09-06 15:17:11 -07001820 mm_segment_t old_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
1822 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1823 umod, len, uargs);
1824 if (len < sizeof(*hdr))
1825 return ERR_PTR(-ENOEXEC);
1826
1827 /* Suck in entire file: we'll want most of it. */
1828 /* vmalloc barfs on "unusual" numbers. Check here */
1829 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1830 return ERR_PTR(-ENOMEM);
1831 if (copy_from_user(hdr, umod, len) != 0) {
1832 err = -EFAULT;
1833 goto free_hdr;
1834 }
1835
1836 /* Sanity checks against insmoding binaries or wrong arch,
1837 weird elf version */
Cyrill Gorcunovc4ea6fc2008-05-14 16:27:29 -07001838 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 || hdr->e_type != ET_REL
1840 || !elf_check_arch(hdr)
1841 || hdr->e_shentsize != sizeof(*sechdrs)) {
1842 err = -ENOEXEC;
1843 goto free_hdr;
1844 }
1845
1846 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1847 goto truncated;
1848
1849 /* Convenience variables */
1850 sechdrs = (void *)hdr + hdr->e_shoff;
1851 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1852 sechdrs[0].sh_addr = 0;
1853
1854 for (i = 1; i < hdr->e_shnum; i++) {
1855 if (sechdrs[i].sh_type != SHT_NOBITS
1856 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1857 goto truncated;
1858
1859 /* Mark all sections sh_addr with their address in the
1860 temporary image. */
1861 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1862
1863 /* Internal symbols and strings. */
1864 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1865 symindex = i;
1866 strindex = sechdrs[i].sh_link;
1867 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1868 }
1869#ifndef CONFIG_MODULE_UNLOAD
1870 /* Don't load .exit sections */
1871 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1872 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1873#endif
1874 }
1875
1876 modindex = find_sec(hdr, sechdrs, secstrings,
1877 ".gnu.linkonce.this_module");
1878 if (!modindex) {
1879 printk(KERN_WARNING "No module found in object\n");
1880 err = -ENOEXEC;
1881 goto free_hdr;
1882 }
1883 mod = (void *)sechdrs[modindex].sh_addr;
1884
1885 if (symindex == 0) {
1886 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1887 mod->name);
1888 err = -ENOEXEC;
1889 goto free_hdr;
1890 }
1891
1892 /* Optional sections */
1893 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1894 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001895 gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1897 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001898 gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future");
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001899#ifdef CONFIG_UNUSED_SYMBOLS
1900 unusedindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused");
1901 unusedgplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused_gpl");
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001902 unusedcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused");
1903 unusedgplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused_gpl");
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001904#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1906 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1907 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1908 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1909 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1910 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
Jan Beulich4552d5d2006-06-26 13:57:28 +02001911#ifdef ARCH_UNWIND_SECTION_NAME
1912 unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
1913#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Rusty Russellea01e792008-03-13 09:02:17 +00001915 /* Don't keep modinfo and version sections. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russellea01e792008-03-13 09:02:17 +00001917 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918#ifdef CONFIG_KALLSYMS
1919 /* Keep symbol and string tables for decoding later. */
1920 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1921 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1922#endif
Jan Beulich4552d5d2006-06-26 13:57:28 +02001923 if (unwindex)
1924 sechdrs[unwindex].sh_flags |= SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
1926 /* Check module struct version now, before we try to use module. */
1927 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1928 err = -ENOEXEC;
1929 goto free_hdr;
1930 }
1931
1932 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1933 /* This is allowed: modprobe --force will invalidate it. */
1934 if (!modmagic) {
Linus Torvalds826e4502008-05-04 17:04:16 -07001935 err = try_to_force_load(mod, "magic");
1936 if (err)
1937 goto free_hdr;
Rusty Russell91e37a72008-05-09 16:25:28 +10001938 } else if (!same_magic(modmagic, vermagic, versindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1940 mod->name, modmagic, vermagic);
1941 err = -ENOEXEC;
1942 goto free_hdr;
1943 }
1944
1945 /* Now copy in args */
Davi Arnaut24277dd2006-03-24 03:18:43 -08001946 args = strndup_user(uargs, ~0UL >> 1);
1947 if (IS_ERR(args)) {
1948 err = PTR_ERR(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 goto free_hdr;
1950 }
Andrew Morton8e08b752006-02-07 12:58:45 -08001951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 if (find_module(mod->name)) {
1953 err = -EEXIST;
1954 goto free_mod;
1955 }
1956
1957 mod->state = MODULE_STATE_COMING;
1958
1959 /* Allow arches to frob section contents and sizes. */
1960 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1961 if (err < 0)
1962 goto free_mod;
1963
1964 if (pcpuindex) {
1965 /* We have a special allocation for this section. */
1966 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
Rusty Russell842bbaa2005-08-01 21:11:47 -07001967 sechdrs[pcpuindex].sh_addralign,
1968 mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 if (!percpu) {
1970 err = -ENOMEM;
1971 goto free_mod;
1972 }
1973 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1974 mod->percpu = percpu;
1975 }
1976
1977 /* Determine total sizes, and put offsets in sh_entsize. For now
1978 this is done generically; there doesn't appear to be any
1979 special cases for the architectures. */
1980 layout_sections(mod, hdr, sechdrs, secstrings);
1981
1982 /* Do the allocs. */
1983 ptr = module_alloc(mod->core_size);
1984 if (!ptr) {
1985 err = -ENOMEM;
1986 goto free_percpu;
1987 }
1988 memset(ptr, 0, mod->core_size);
1989 mod->module_core = ptr;
1990
1991 ptr = module_alloc(mod->init_size);
1992 if (!ptr && mod->init_size) {
1993 err = -ENOMEM;
1994 goto free_core;
1995 }
1996 memset(ptr, 0, mod->init_size);
1997 mod->module_init = ptr;
1998
1999 /* Transfer each section which specifies SHF_ALLOC */
2000 DEBUGP("final section addresses:\n");
2001 for (i = 0; i < hdr->e_shnum; i++) {
2002 void *dest;
2003
2004 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2005 continue;
2006
2007 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2008 dest = mod->module_init
2009 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2010 else
2011 dest = mod->module_core + sechdrs[i].sh_entsize;
2012
2013 if (sechdrs[i].sh_type != SHT_NOBITS)
2014 memcpy(dest, (void *)sechdrs[i].sh_addr,
2015 sechdrs[i].sh_size);
2016 /* Update sh_addr to point to copy in image. */
2017 sechdrs[i].sh_addr = (unsigned long)dest;
2018 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2019 }
2020 /* Module has been moved. */
2021 mod = (void *)sechdrs[modindex].sh_addr;
2022
2023 /* Now we've moved module, initialize linked lists, etc. */
2024 module_unload_init(mod);
2025
Kay Sievers97c146e2007-11-29 23:46:11 +01002026 /* add kobject, so we can reference it. */
Akinobu Mitad58ae672007-10-16 23:30:27 -07002027 err = mod_sysfs_init(mod);
2028 if (err)
Kay Sievers97c146e2007-11-29 23:46:11 +01002029 goto free_unload;
Kay Sievers270a6c42007-01-18 13:26:15 +01002030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 /* Set up license info based on the info section */
2032 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2033
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002034 /*
2035 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2036 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2037 * using GPL-only symbols it needs.
2038 */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002039 if (strcmp(mod->name, "ndiswrapper") == 0)
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002040 add_taint(TAINT_PROPRIETARY_MODULE);
2041
2042 /* driverloader was caught wrongly pretending to be under GPL */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002043 if (strcmp(mod->name, "driverloader") == 0)
2044 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Dave Jones9841d61d2006-01-08 01:03:41 -08002045
Matt Domschc988d2b2005-06-23 22:05:15 -07002046 /* Set up MODINFO_ATTR fields */
2047 setup_modinfo(mod, sechdrs, infoindex);
Matt Domschc988d2b2005-06-23 22:05:15 -07002048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 /* Fix up syms, so that st_value is a pointer to location. */
2050 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2051 mod);
2052 if (err < 0)
2053 goto cleanup;
2054
2055 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
2056 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
2057 mod->syms = (void *)sechdrs[exportindex].sh_addr;
2058 if (crcindex)
2059 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
2060 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
2061 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
2062 if (gplcrcindex)
2063 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08002064 mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size /
2065 sizeof(*mod->gpl_future_syms);
2066 mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr;
2067 if (gplfuturecrcindex)
2068 mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002070#ifdef CONFIG_UNUSED_SYMBOLS
2071 mod->num_unused_syms = sechdrs[unusedindex].sh_size /
2072 sizeof(*mod->unused_syms);
2073 mod->num_unused_gpl_syms = sechdrs[unusedgplindex].sh_size /
2074 sizeof(*mod->unused_gpl_syms);
Arjan van de Venf71d20e2006-06-28 04:26:45 -07002075 mod->unused_syms = (void *)sechdrs[unusedindex].sh_addr;
2076 if (unusedcrcindex)
2077 mod->unused_crcs = (void *)sechdrs[unusedcrcindex].sh_addr;
2078 mod->unused_gpl_syms = (void *)sechdrs[unusedgplindex].sh_addr;
2079 if (unusedgplcrcindex)
Rusty Russell4e2d9242008-05-01 21:15:00 -05002080 mod->unused_gpl_crcs
2081 = (void *)sechdrs[unusedgplcrcindex].sh_addr;
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002082#endif
Arjan van de Venf71d20e2006-06-28 04:26:45 -07002083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084#ifdef CONFIG_MODVERSIONS
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002085 if ((mod->num_syms && !crcindex)
2086 || (mod->num_gpl_syms && !gplcrcindex)
2087 || (mod->num_gpl_future_syms && !gplfuturecrcindex)
2088#ifdef CONFIG_UNUSED_SYMBOLS
2089 || (mod->num_unused_syms && !unusedcrcindex)
2090 || (mod->num_unused_gpl_syms && !unusedgplcrcindex)
2091#endif
2092 ) {
Linus Torvalds826e4502008-05-04 17:04:16 -07002093 printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name);
2094 err = try_to_force_load(mod, "nocrc");
2095 if (err)
2096 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 }
2098#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002099 markersindex = find_sec(hdr, sechdrs, secstrings, "__markers");
2100 markersstringsindex = find_sec(hdr, sechdrs, secstrings,
2101 "__markers_strings");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 /* Now do relocations. */
2104 for (i = 1; i < hdr->e_shnum; i++) {
2105 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2106 unsigned int info = sechdrs[i].sh_info;
2107
2108 /* Not a valid relocation section? */
2109 if (info >= hdr->e_shnum)
2110 continue;
2111
2112 /* Don't bother with non-allocated sections */
2113 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2114 continue;
2115
2116 if (sechdrs[i].sh_type == SHT_REL)
2117 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2118 else if (sechdrs[i].sh_type == SHT_RELA)
2119 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2120 mod);
2121 if (err < 0)
2122 goto cleanup;
2123 }
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002124#ifdef CONFIG_MARKERS
2125 mod->markers = (void *)sechdrs[markersindex].sh_addr;
2126 mod->num_markers =
2127 sechdrs[markersindex].sh_size / sizeof(*mod->markers);
2128#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002130 /* Find duplicate symbols */
2131 err = verify_export_symbols(mod);
2132
2133 if (err < 0)
2134 goto cleanup;
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 /* Set up and sort exception table */
2137 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
2138 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
2139 sort_extable(extable, extable + mod->num_exentries);
2140
2141 /* Finally, copy percpu area over. */
2142 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2143 sechdrs[pcpuindex].sh_size);
2144
2145 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2146
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002147#ifdef CONFIG_MARKERS
2148 if (!mod->taints)
2149 marker_update_probe_range(mod->markers,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002150 mod->markers + mod->num_markers);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002151#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 err = module_finalize(hdr, sechdrs, mod);
2153 if (err < 0)
2154 goto cleanup;
2155
Thomas Koeller378bac82005-09-06 15:17:11 -07002156 /* flush the icache in correct context */
2157 old_fs = get_fs();
2158 set_fs(KERNEL_DS);
2159
2160 /*
2161 * Flush the instruction cache, since we've played with text.
2162 * Do it before processing of module parameters, so the module
2163 * can provide parameter accessor functions of its own.
2164 */
2165 if (mod->module_init)
2166 flush_icache_range((unsigned long)mod->module_init,
2167 (unsigned long)mod->module_init
2168 + mod->init_size);
2169 flush_icache_range((unsigned long)mod->module_core,
2170 (unsigned long)mod->module_core + mod->core_size);
2171
2172 set_fs(old_fs);
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 mod->args = args;
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002175 if (obsparmindex)
2176 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2177 mod->name);
2178
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002179 /* Now sew it into the lists so we can get lockdep and oops
2180 * info during argument parsing. Noone should access us, since
2181 * strong_try_module_get() will fail. */
2182 stop_machine_run(__link_module, mod, NR_CPUS);
2183
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002184 /* Size of section 0 is 0, so this works well if no params */
2185 err = parse_args(mod->name, mod->args,
2186 (struct kernel_param *)
2187 sechdrs[setupindex].sh_addr,
2188 sechdrs[setupindex].sh_size
2189 / sizeof(struct kernel_param),
2190 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002192 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
Daniel Walker22a8bde2007-10-18 03:06:07 -07002194 err = mod_sysfs_setup(mod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 (struct kernel_param *)
2196 sechdrs[setupindex].sh_addr,
2197 sechdrs[setupindex].sh_size
2198 / sizeof(struct kernel_param));
2199 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002200 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Roland McGrath6d760132007-10-16 23:26:40 -07002202 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Jan Beulich4552d5d2006-06-26 13:57:28 +02002204 /* Size of section 0 is 0, so this works well if no unwind info. */
2205 mod->unwind_info = unwind_add_table(mod,
Daniel Walker22a8bde2007-10-18 03:06:07 -07002206 (void *)sechdrs[unwindex].sh_addr,
2207 sechdrs[unwindex].sh_size);
Jan Beulich4552d5d2006-06-26 13:57:28 +02002208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 /* Get rid of temporary copy */
2210 vfree(hdr);
2211
2212 /* Done! */
2213 return mod;
2214
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002215 unlink:
2216 stop_machine_run(__unlink_module, mod, NR_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 module_arch_cleanup(mod);
2218 cleanup:
Kay Sievers97c146e2007-11-29 23:46:11 +01002219 kobject_del(&mod->mkobj.kobj);
2220 kobject_put(&mod->mkobj.kobj);
2221 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 module_unload_free(mod);
2223 module_free(mod, mod->module_init);
2224 free_core:
2225 module_free(mod, mod->module_core);
2226 free_percpu:
2227 if (percpu)
2228 percpu_modfree(percpu);
2229 free_mod:
2230 kfree(args);
2231 free_hdr:
2232 vfree(hdr);
Jayachandran C6fe2e702006-01-06 00:19:54 -08002233 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
2235 truncated:
2236 printk(KERN_ERR "Module len %lu truncated\n", len);
2237 err = -ENOEXEC;
2238 goto free_hdr;
2239}
2240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241/* This is where the real work happens */
2242asmlinkage long
2243sys_init_module(void __user *umod,
2244 unsigned long len,
2245 const char __user *uargs)
2246{
2247 struct module *mod;
2248 int ret = 0;
2249
2250 /* Must have permission */
2251 if (!capable(CAP_SYS_MODULE))
2252 return -EPERM;
2253
2254 /* Only one module load at a time, please */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002255 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 return -EINTR;
2257
2258 /* Do all the hard work */
2259 mod = load_module(umod, len, uargs);
2260 if (IS_ERR(mod)) {
Ashutosh Naik6389a382006-03-23 03:00:46 -08002261 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 return PTR_ERR(mod);
2263 }
2264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 /* Drop lock so they can recurse */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002266 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Alan Sterne041c682006-03-27 01:16:30 -08002268 blocking_notifier_call_chain(&module_notify_list,
2269 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
2271 /* Start the module */
2272 if (mod->init != NULL)
2273 ret = mod->init();
2274 if (ret < 0) {
2275 /* Init routine failed: abort. Try to protect us from
2276 buggy refcounters. */
2277 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002278 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002279 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002280 blocking_notifier_call_chain(&module_notify_list,
2281 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002282 mutex_lock(&module_mutex);
2283 free_module(mod);
2284 mutex_unlock(&module_mutex);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002285 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 return ret;
2287 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002288 if (ret > 0) {
2289 printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
2290 "it should follow 0/-E convention\n"
2291 KERN_WARNING "%s: loading module anyway...\n",
2292 __func__, mod->name, ret,
2293 __func__);
2294 dump_stack();
2295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
Rusty Russell6c5db222008-03-10 11:43:52 -07002297 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002299 wake_up(&module_wq);
2300
2301 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 /* Drop initial reference. */
2303 module_put(mod);
Jan Beulich4552d5d2006-06-26 13:57:28 +02002304 unwind_remove_table(mod->unwind_info, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 module_free(mod, mod->module_init);
2306 mod->module_init = NULL;
2307 mod->init_size = 0;
2308 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002309 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
2311 return 0;
2312}
2313
2314static inline int within(unsigned long addr, void *start, unsigned long size)
2315{
2316 return ((void *)addr >= start && (void *)addr < start + size);
2317}
2318
2319#ifdef CONFIG_KALLSYMS
2320/*
2321 * This ignores the intensely annoying "mapping symbols" found
2322 * in ARM ELF files: $a, $t and $d.
2323 */
2324static inline int is_arm_mapping_symbol(const char *str)
2325{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002326 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 && (str[2] == '\0' || str[2] == '.');
2328}
2329
2330static const char *get_ksymbol(struct module *mod,
2331 unsigned long addr,
2332 unsigned long *size,
2333 unsigned long *offset)
2334{
2335 unsigned int i, best = 0;
2336 unsigned long nextval;
2337
2338 /* At worse, next value is at end of module */
2339 if (within(addr, mod->module_init, mod->init_size))
2340 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002341 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2343
2344 /* Scan for closest preceeding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002345 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 for (i = 1; i < mod->num_symtab; i++) {
2347 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2348 continue;
2349
2350 /* We ignore unnamed symbols: they're uninformative
2351 * and inserted at a whim. */
2352 if (mod->symtab[i].st_value <= addr
2353 && mod->symtab[i].st_value > mod->symtab[best].st_value
2354 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2355 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2356 best = i;
2357 if (mod->symtab[i].st_value > addr
2358 && mod->symtab[i].st_value < nextval
2359 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2360 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2361 nextval = mod->symtab[i].st_value;
2362 }
2363
2364 if (!best)
2365 return NULL;
2366
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002367 if (size)
2368 *size = nextval - mod->symtab[best].st_value;
2369 if (offset)
2370 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 return mod->strtab + mod->symtab[best].st_name;
2372}
2373
Rusty Russell6dd06c92008-01-29 17:13:22 -05002374/* For kallsyms to ask for address resolution. NULL means not found. Careful
2375 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002376const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05002377 unsigned long *size,
2378 unsigned long *offset,
2379 char **modname,
2380 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381{
2382 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002383 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Rusty Russellcb2a5202008-01-14 00:55:03 -08002385 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 list_for_each_entry(mod, &modules, list) {
2387 if (within(addr, mod->module_init, mod->init_size)
2388 || within(addr, mod->module_core, mod->core_size)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002389 if (modname)
2390 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002391 ret = get_ksymbol(mod, addr, size, offset);
2392 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 }
2394 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05002395 /* Make a copy in here where it's safe */
2396 if (ret) {
2397 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2398 ret = namebuf;
2399 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002400 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002401 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402}
2403
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002404int lookup_module_symbol_name(unsigned long addr, char *symname)
2405{
2406 struct module *mod;
2407
Rusty Russellcb2a5202008-01-14 00:55:03 -08002408 preempt_disable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002409 list_for_each_entry(mod, &modules, list) {
2410 if (within(addr, mod->module_init, mod->init_size) ||
2411 within(addr, mod->module_core, mod->core_size)) {
2412 const char *sym;
2413
2414 sym = get_ksymbol(mod, addr, NULL, NULL);
2415 if (!sym)
2416 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07002417 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002418 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002419 return 0;
2420 }
2421 }
2422out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002423 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002424 return -ERANGE;
2425}
2426
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002427int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2428 unsigned long *offset, char *modname, char *name)
2429{
2430 struct module *mod;
2431
Rusty Russellcb2a5202008-01-14 00:55:03 -08002432 preempt_disable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002433 list_for_each_entry(mod, &modules, list) {
2434 if (within(addr, mod->module_init, mod->init_size) ||
2435 within(addr, mod->module_core, mod->core_size)) {
2436 const char *sym;
2437
2438 sym = get_ksymbol(mod, addr, size, offset);
2439 if (!sym)
2440 goto out;
2441 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07002442 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002443 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07002444 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002445 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002446 return 0;
2447 }
2448 }
2449out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002450 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002451 return -ERANGE;
2452}
2453
Alexey Dobriyanea078902007-05-08 00:28:39 -07002454int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2455 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
2457 struct module *mod;
2458
Rusty Russellcb2a5202008-01-14 00:55:03 -08002459 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 list_for_each_entry(mod, &modules, list) {
2461 if (symnum < mod->num_symtab) {
2462 *value = mod->symtab[symnum].st_value;
2463 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002464 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07002465 KSYM_NAME_LEN);
2466 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Alexey Dobriyanea078902007-05-08 00:28:39 -07002467 *exported = is_exported(name, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002468 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002469 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 }
2471 symnum -= mod->num_symtab;
2472 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002473 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002474 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
2476
2477static unsigned long mod_find_symname(struct module *mod, const char *name)
2478{
2479 unsigned int i;
2480
2481 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002482 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2483 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 return mod->symtab[i].st_value;
2485 return 0;
2486}
2487
2488/* Look for this name: can be of form module:name. */
2489unsigned long module_kallsyms_lookup_name(const char *name)
2490{
2491 struct module *mod;
2492 char *colon;
2493 unsigned long ret = 0;
2494
2495 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08002496 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 if ((colon = strchr(name, ':')) != NULL) {
2498 *colon = '\0';
2499 if ((mod = find_module(name)) != NULL)
2500 ret = mod_find_symname(mod, colon+1);
2501 *colon = ':';
2502 } else {
2503 list_for_each_entry(mod, &modules, list)
2504 if ((ret = mod_find_symname(mod, name)) != 0)
2505 break;
2506 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002507 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 return ret;
2509}
2510#endif /* CONFIG_KALLSYMS */
2511
2512/* Called by the /proc file system to return a list of modules. */
2513static void *m_start(struct seq_file *m, loff_t *pos)
2514{
Ashutosh Naik6389a382006-03-23 03:00:46 -08002515 mutex_lock(&module_mutex);
Pavel Emelianov708f4b52007-07-15 23:39:54 -07002516 return seq_list_start(&modules, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517}
2518
2519static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2520{
Pavel Emelianov708f4b52007-07-15 23:39:54 -07002521 return seq_list_next(p, &modules, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522}
2523
2524static void m_stop(struct seq_file *m, void *p)
2525{
Ashutosh Naik6389a382006-03-23 03:00:46 -08002526 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527}
2528
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002529static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002530{
2531 int bx = 0;
2532
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002533 if (mod->taints ||
2534 mod->state == MODULE_STATE_GOING ||
2535 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002536 buf[bx++] = '(';
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002537 if (mod->taints & TAINT_PROPRIETARY_MODULE)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002538 buf[bx++] = 'P';
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002539 if (mod->taints & TAINT_FORCED_MODULE)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002540 buf[bx++] = 'F';
2541 /*
2542 * TAINT_FORCED_RMMOD: could be added.
2543 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2544 * apply to modules.
2545 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002546
2547 /* Show a - for module-is-being-unloaded */
2548 if (mod->state == MODULE_STATE_GOING)
2549 buf[bx++] = '-';
2550 /* Show a + for module-is-being-loaded */
2551 if (mod->state == MODULE_STATE_COMING)
2552 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002553 buf[bx++] = ')';
2554 }
2555 buf[bx] = '\0';
2556
2557 return buf;
2558}
2559
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560static int m_show(struct seq_file *m, void *p)
2561{
2562 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002563 char buf[8];
2564
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05002565 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 mod->name, mod->init_size + mod->core_size);
2567 print_unload_info(m, mod);
2568
2569 /* Informative for users. */
2570 seq_printf(m, " %s",
2571 mod->state == MODULE_STATE_GOING ? "Unloading":
2572 mod->state == MODULE_STATE_COMING ? "Loading":
2573 "Live");
2574 /* Used by oprofile and other similar tools. */
2575 seq_printf(m, " 0x%p", mod->module_core);
2576
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002577 /* Taints info */
2578 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002579 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 seq_printf(m, "\n");
2582 return 0;
2583}
2584
2585/* Format: modulename size refcount deps address
2586
2587 Where refcount is a number or -, and deps is a comma-separated list
2588 of depends or -.
2589*/
Helge Deller15ad7cd2006-12-06 20:40:36 -08002590const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 .start = m_start,
2592 .next = m_next,
2593 .stop = m_stop,
2594 .show = m_show
2595};
2596
2597/* Given an address, look for it in the module exception tables. */
2598const struct exception_table_entry *search_module_extables(unsigned long addr)
2599{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 const struct exception_table_entry *e = NULL;
2601 struct module *mod;
2602
Rusty Russell24da1cb2007-07-15 23:41:46 -07002603 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 list_for_each_entry(mod, &modules, list) {
2605 if (mod->num_exentries == 0)
2606 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002607
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 e = search_extable(mod->extable,
2609 mod->extable + mod->num_exentries - 1,
2610 addr);
2611 if (e)
2612 break;
2613 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07002614 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
2616 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07002617 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 return e;
2619}
2620
Ingo Molnar4d435f92006-07-03 00:24:24 -07002621/*
2622 * Is this a valid module address?
2623 */
2624int is_module_address(unsigned long addr)
2625{
Ingo Molnar4d435f92006-07-03 00:24:24 -07002626 struct module *mod;
2627
Rusty Russell24da1cb2007-07-15 23:41:46 -07002628 preempt_disable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002629
2630 list_for_each_entry(mod, &modules, list) {
2631 if (within(addr, mod->module_core, mod->core_size)) {
Rusty Russell24da1cb2007-07-15 23:41:46 -07002632 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002633 return 1;
2634 }
2635 }
2636
Rusty Russell24da1cb2007-07-15 23:41:46 -07002637 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002638
2639 return 0;
2640}
2641
2642
Rusty Russell24da1cb2007-07-15 23:41:46 -07002643/* Is this a valid kernel address? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644struct module *__module_text_address(unsigned long addr)
2645{
2646 struct module *mod;
2647
2648 list_for_each_entry(mod, &modules, list)
2649 if (within(addr, mod->module_init, mod->init_text_size)
2650 || within(addr, mod->module_core, mod->core_text_size))
2651 return mod;
2652 return NULL;
2653}
2654
2655struct module *module_text_address(unsigned long addr)
2656{
2657 struct module *mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
Rusty Russell24da1cb2007-07-15 23:41:46 -07002659 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 mod = __module_text_address(addr);
Rusty Russell24da1cb2007-07-15 23:41:46 -07002661 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
2663 return mod;
2664}
2665
2666/* Don't grab lock, we're oopsing. */
2667void print_modules(void)
2668{
2669 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07002670 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
2672 printk("Modules linked in:");
2673 list_for_each_entry(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002674 printk(" %s%s", mod->name, module_flags(mod, buf));
Arjan van de Vene14af7e2008-01-25 21:08:33 +01002675 if (last_unloaded_module[0])
2676 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 printk("\n");
2678}
2679
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680#ifdef CONFIG_MODVERSIONS
2681/* Generate the signature for struct module here, too, for modversions. */
2682void struct_module(struct module *mod) { return; }
2683EXPORT_SYMBOL(struct_module);
2684#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002685
2686#ifdef CONFIG_MARKERS
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002687void module_update_markers(void)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002688{
2689 struct module *mod;
2690
2691 mutex_lock(&module_mutex);
2692 list_for_each_entry(mod, &modules, list)
2693 if (!mod->taints)
2694 marker_update_probe_range(mod->markers,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002695 mod->markers + mod->num_markers);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002696 mutex_unlock(&module_mutex);
2697}
2698#endif