blob: 43a529a1fa487b62fc570f2a08a2585c8274068b [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>
Randy Dunlap9f158332005-09-13 01:25:16 -070023#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/slab.h>
25#include <linux/vmalloc.h>
26#include <linux/elf.h>
27#include <linux/seq_file.h>
28#include <linux/syscalls.h>
29#include <linux/fcntl.h>
30#include <linux/rcupdate.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080031#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/cpu.h>
33#include <linux/moduleparam.h>
34#include <linux/errno.h>
35#include <linux/err.h>
36#include <linux/vermagic.h>
37#include <linux/notifier.h>
Al Virof6a57032006-10-18 01:47:25 -040038#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/stop_machine.h>
40#include <linux/device.h>
Matt Domschc988d2b2005-06-23 22:05:15 -070041#include <linux/string.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080042#include <linux/mutex.h>
Jan Beulich4552d5d2006-06-26 13:57:28 +020043#include <linux/unwind.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/uaccess.h>
45#include <asm/semaphore.h>
46#include <asm/cacheflush.h>
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020047#include <linux/license.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070049extern int module_sysfs_initialized;
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#if 0
52#define DEBUGP printk
53#else
54#define DEBUGP(fmt , a...)
55#endif
56
57#ifndef ARCH_SHF_SMALL
58#define ARCH_SHF_SMALL 0
59#endif
60
61/* If this is set, the section belongs in the init part of the module */
62#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
63
64/* Protects module list */
65static DEFINE_SPINLOCK(modlist_lock);
66
67/* List of modules, protected by module_mutex AND modlist_lock */
Ashutosh Naik6389a382006-03-23 03:00:46 -080068static DEFINE_MUTEX(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static LIST_HEAD(modules);
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
85/* We require a truly strong try_module_get() */
86static inline int strong_try_module_get(struct module *mod)
87{
88 if (mod && mod->state == MODULE_STATE_COMING)
89 return 0;
90 return try_module_get(mod);
91}
92
Florin Malitafa3ba2e82006-10-11 01:21:48 -070093static inline void add_taint_module(struct module *mod, unsigned flag)
94{
95 add_taint(flag);
96 mod->taints |= flag;
97}
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/* A thread that wants to hold a reference to a module only while it
100 * is running can call ths to safely exit.
101 * nfsd and lockd use this.
102 */
103void __module_put_and_exit(struct module *mod, long code)
104{
105 module_put(mod);
106 do_exit(code);
107}
108EXPORT_SYMBOL(__module_put_and_exit);
109
110/* Find a module section: 0 means not found. */
111static unsigned int find_sec(Elf_Ehdr *hdr,
112 Elf_Shdr *sechdrs,
113 const char *secstrings,
114 const char *name)
115{
116 unsigned int i;
117
118 for (i = 1; i < hdr->e_shnum; i++)
119 /* Alloc bit cleared means "ignore it." */
120 if ((sechdrs[i].sh_flags & SHF_ALLOC)
121 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
122 return i;
123 return 0;
124}
125
126/* Provided by the linker */
127extern const struct kernel_symbol __start___ksymtab[];
128extern const struct kernel_symbol __stop___ksymtab[];
129extern const struct kernel_symbol __start___ksymtab_gpl[];
130extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800131extern const struct kernel_symbol __start___ksymtab_gpl_future[];
132extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700133extern const struct kernel_symbol __start___ksymtab_unused[];
134extern const struct kernel_symbol __stop___ksymtab_unused[];
135extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
136extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
137extern 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[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700142extern const unsigned long __start___kcrctab_unused[];
143extern const unsigned long __start___kcrctab_unused_gpl[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145#ifndef CONFIG_MODVERSIONS
146#define symversion(base, idx) NULL
147#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800148#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#endif
150
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100151/* lookup symbol in given range of kernel_symbols */
152static const struct kernel_symbol *lookup_symbol(const char *name,
153 const struct kernel_symbol *start,
154 const struct kernel_symbol *stop)
155{
156 const struct kernel_symbol *ks = start;
157 for (; ks < stop; ks++)
158 if (strcmp(ks->name, name) == 0)
159 return ks;
160 return NULL;
161}
162
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700163static void printk_unused_warning(const char *name)
164{
165 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
166 "however this module is using it.\n", name);
167 printk(KERN_WARNING "This symbol will go away in the future.\n");
168 printk(KERN_WARNING "Please evalute if this is the right api to use, "
169 "and if it really is, submit a report the linux kernel "
170 "mailinglist together with submitting your code for "
171 "inclusion.\n");
172}
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/* Find a symbol, return value, crc and module which owns it */
175static unsigned long __find_symbol(const char *name,
176 struct module **owner,
177 const unsigned long **crc,
178 int gplok)
179{
180 struct module *mod;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100181 const struct kernel_symbol *ks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 /* Core kernel first. */
184 *owner = NULL;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100185 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
186 if (ks) {
187 *crc = symversion(__start___kcrctab, (ks - __start___ksymtab));
188 return ks->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190 if (gplok) {
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100191 ks = lookup_symbol(name, __start___ksymtab_gpl,
192 __stop___ksymtab_gpl);
193 if (ks) {
194 *crc = symversion(__start___kcrctab_gpl,
195 (ks - __start___ksymtab_gpl));
196 return ks->value;
197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800199 ks = lookup_symbol(name, __start___ksymtab_gpl_future,
200 __stop___ksymtab_gpl_future);
201 if (ks) {
202 if (!gplok) {
203 printk(KERN_WARNING "Symbol %s is being used "
204 "by a non-GPL module, which will not "
205 "be allowed in the future\n", name);
206 printk(KERN_WARNING "Please see the file "
207 "Documentation/feature-removal-schedule.txt "
208 "in the kernel source tree for more "
209 "details.\n");
210 }
211 *crc = symversion(__start___kcrctab_gpl_future,
212 (ks - __start___ksymtab_gpl_future));
213 return ks->value;
214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700216 ks = lookup_symbol(name, __start___ksymtab_unused,
217 __stop___ksymtab_unused);
218 if (ks) {
219 printk_unused_warning(name);
220 *crc = symversion(__start___kcrctab_unused,
221 (ks - __start___ksymtab_unused));
222 return ks->value;
223 }
224
225 if (gplok)
226 ks = lookup_symbol(name, __start___ksymtab_unused_gpl,
227 __stop___ksymtab_unused_gpl);
228 if (ks) {
229 printk_unused_warning(name);
230 *crc = symversion(__start___kcrctab_unused_gpl,
231 (ks - __start___ksymtab_unused_gpl));
232 return ks->value;
233 }
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /* Now try modules. */
236 list_for_each_entry(mod, &modules, list) {
237 *owner = mod;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100238 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
239 if (ks) {
240 *crc = symversion(mod->crcs, (ks - mod->syms));
241 return ks->value;
242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 if (gplok) {
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100245 ks = lookup_symbol(name, mod->gpl_syms,
246 mod->gpl_syms + mod->num_gpl_syms);
247 if (ks) {
248 *crc = symversion(mod->gpl_crcs,
249 (ks - mod->gpl_syms));
250 return ks->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252 }
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700253 ks = lookup_symbol(name, mod->unused_syms, mod->unused_syms + mod->num_unused_syms);
254 if (ks) {
255 printk_unused_warning(name);
256 *crc = symversion(mod->unused_crcs, (ks - mod->unused_syms));
257 return ks->value;
258 }
259
260 if (gplok) {
261 ks = lookup_symbol(name, mod->unused_gpl_syms,
262 mod->unused_gpl_syms + mod->num_unused_gpl_syms);
263 if (ks) {
264 printk_unused_warning(name);
265 *crc = symversion(mod->unused_gpl_crcs,
266 (ks - mod->unused_gpl_syms));
267 return ks->value;
268 }
269 }
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800270 ks = lookup_symbol(name, mod->gpl_future_syms,
271 (mod->gpl_future_syms +
272 mod->num_gpl_future_syms));
273 if (ks) {
274 if (!gplok) {
275 printk(KERN_WARNING "Symbol %s is being used "
276 "by a non-GPL module, which will not "
277 "be allowed in the future\n", name);
278 printk(KERN_WARNING "Please see the file "
279 "Documentation/feature-removal-schedule.txt "
280 "in the kernel source tree for more "
281 "details.\n");
282 }
283 *crc = symversion(mod->gpl_future_crcs,
284 (ks - mod->gpl_future_syms));
285 return ks->value;
286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288 DEBUGP("Failed to find symbol %s\n", name);
289 return 0;
290}
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/* Search for module by name: must hold module_mutex. */
293static struct module *find_module(const char *name)
294{
295 struct module *mod;
296
297 list_for_each_entry(mod, &modules, list) {
298 if (strcmp(mod->name, name) == 0)
299 return mod;
300 }
301 return NULL;
302}
303
304#ifdef CONFIG_SMP
305/* Number of blocks used and allocated. */
306static unsigned int pcpu_num_used, pcpu_num_allocated;
307/* Size of each block. -ve means used. */
308static int *pcpu_size;
309
310static int split_block(unsigned int i, unsigned short size)
311{
312 /* Reallocation required? */
313 if (pcpu_num_used + 1 > pcpu_num_allocated) {
Pekka Enberg6d4f9c52007-05-08 00:24:58 -0700314 int *new;
315
316 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
317 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (!new)
319 return 0;
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 pcpu_num_allocated *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 pcpu_size = new;
323 }
324
325 /* Insert a new subblock */
326 memmove(&pcpu_size[i+1], &pcpu_size[i],
327 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
328 pcpu_num_used++;
329
330 pcpu_size[i+1] -= size;
331 pcpu_size[i] = size;
332 return 1;
333}
334
335static inline unsigned int block_size(int val)
336{
337 if (val < 0)
338 return -val;
339 return val;
340}
341
342/* Created by linker magic */
343extern char __per_cpu_start[], __per_cpu_end[];
344
Rusty Russell842bbaa2005-08-01 21:11:47 -0700345static void *percpu_modalloc(unsigned long size, unsigned long align,
346 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 unsigned long extra;
349 unsigned int i;
350 void *ptr;
351
Jeremy Fitzhardingeb6e35902007-05-02 19:27:12 +0200352 if (align > PAGE_SIZE) {
353 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
354 name, align, PAGE_SIZE);
355 align = PAGE_SIZE;
Rusty Russell842bbaa2005-08-01 21:11:47 -0700356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 ptr = __per_cpu_start;
359 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
360 /* Extra for alignment requirement. */
361 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
362 BUG_ON(i == 0 && extra != 0);
363
364 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
365 continue;
366
367 /* Transfer extra to previous block. */
368 if (pcpu_size[i-1] < 0)
369 pcpu_size[i-1] -= extra;
370 else
371 pcpu_size[i-1] += extra;
372 pcpu_size[i] -= extra;
373 ptr += extra;
374
375 /* Split block if warranted */
376 if (pcpu_size[i] - size > sizeof(unsigned long))
377 if (!split_block(i, size))
378 return NULL;
379
380 /* Mark allocated */
381 pcpu_size[i] = -pcpu_size[i];
382 return ptr;
383 }
384
385 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
386 size);
387 return NULL;
388}
389
390static void percpu_modfree(void *freeme)
391{
392 unsigned int i;
393 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
394
395 /* First entry is core kernel percpu data. */
396 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
397 if (ptr == freeme) {
398 pcpu_size[i] = -pcpu_size[i];
399 goto free;
400 }
401 }
402 BUG();
403
404 free:
405 /* Merge with previous? */
406 if (pcpu_size[i-1] >= 0) {
407 pcpu_size[i-1] += pcpu_size[i];
408 pcpu_num_used--;
409 memmove(&pcpu_size[i], &pcpu_size[i+1],
410 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
411 i--;
412 }
413 /* Merge with next? */
414 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
415 pcpu_size[i] += pcpu_size[i+1];
416 pcpu_num_used--;
417 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
418 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
419 }
420}
421
422static unsigned int find_pcpusec(Elf_Ehdr *hdr,
423 Elf_Shdr *sechdrs,
424 const char *secstrings)
425{
426 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
427}
428
429static int percpu_modinit(void)
430{
431 pcpu_num_used = 2;
432 pcpu_num_allocated = 2;
433 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
434 GFP_KERNEL);
435 /* Static in-kernel percpu data (used). */
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +0200436 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 /* Free room. */
438 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
439 if (pcpu_size[1] < 0) {
440 printk(KERN_ERR "No per-cpu room for modules.\n");
441 pcpu_num_used = 1;
442 }
443
444 return 0;
445}
446__initcall(percpu_modinit);
447#else /* ... !CONFIG_SMP */
Rusty Russell842bbaa2005-08-01 21:11:47 -0700448static inline void *percpu_modalloc(unsigned long size, unsigned long align,
449 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 return NULL;
452}
453static inline void percpu_modfree(void *pcpuptr)
454{
455 BUG();
456}
457static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
458 Elf_Shdr *sechdrs,
459 const char *secstrings)
460{
461 return 0;
462}
463static inline void percpu_modcopy(void *pcpudst, const void *src,
464 unsigned long size)
465{
466 /* pcpusec should be 0, and size of that section should be 0. */
467 BUG_ON(size != 0);
468}
469#endif /* CONFIG_SMP */
470
Matt Domschc988d2b2005-06-23 22:05:15 -0700471#define MODINFO_ATTR(field) \
472static void setup_modinfo_##field(struct module *mod, const char *s) \
473{ \
474 mod->field = kstrdup(s, GFP_KERNEL); \
475} \
476static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
477 struct module *mod, char *buffer) \
478{ \
479 return sprintf(buffer, "%s\n", mod->field); \
480} \
481static int modinfo_##field##_exists(struct module *mod) \
482{ \
483 return mod->field != NULL; \
484} \
485static void free_modinfo_##field(struct module *mod) \
486{ \
487 kfree(mod->field); \
488 mod->field = NULL; \
489} \
490static struct module_attribute modinfo_##field = { \
491 .attr = { .name = __stringify(field), .mode = 0444, \
492 .owner = THIS_MODULE }, \
493 .show = show_modinfo_##field, \
494 .setup = setup_modinfo_##field, \
495 .test = modinfo_##field##_exists, \
496 .free = free_modinfo_##field, \
497};
498
499MODINFO_ATTR(version);
500MODINFO_ATTR(srcversion);
501
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800502#ifdef CONFIG_MODULE_UNLOAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503/* Init the unload section of the module. */
504static void module_unload_init(struct module *mod)
505{
506 unsigned int i;
507
508 INIT_LIST_HEAD(&mod->modules_which_use_me);
509 for (i = 0; i < NR_CPUS; i++)
510 local_set(&mod->ref[i].count, 0);
511 /* Hold reference count during initialization. */
Ingo Molnar39c715b2005-06-21 17:14:34 -0700512 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 /* Backwards compatibility macros put refcount during init. */
514 mod->waiter = current;
515}
516
517/* modules using other modules */
518struct module_use
519{
520 struct list_head list;
521 struct module *module_which_uses;
522};
523
524/* Does a already use b? */
525static int already_uses(struct module *a, struct module *b)
526{
527 struct module_use *use;
528
529 list_for_each_entry(use, &b->modules_which_use_me, list) {
530 if (use->module_which_uses == a) {
531 DEBUGP("%s uses %s!\n", a->name, b->name);
532 return 1;
533 }
534 }
535 DEBUGP("%s does not use %s!\n", a->name, b->name);
536 return 0;
537}
538
539/* Module a uses b */
540static int use_module(struct module *a, struct module *b)
541{
542 struct module_use *use;
Kay Sievers270a6c42007-01-18 13:26:15 +0100543 int no_warn;
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (b == NULL || already_uses(a, b)) return 1;
546
547 if (!strong_try_module_get(b))
548 return 0;
549
550 DEBUGP("Allocating new usage for %s.\n", a->name);
551 use = kmalloc(sizeof(*use), GFP_ATOMIC);
552 if (!use) {
553 printk("%s: out of memory loading\n", a->name);
554 module_put(b);
555 return 0;
556 }
557
558 use->module_which_uses = a;
559 list_add(&use->list, &b->modules_which_use_me);
Kay Sievers270a6c42007-01-18 13:26:15 +0100560 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return 1;
562}
563
564/* Clear the unload stuff of the module. */
565static void module_unload_free(struct module *mod)
566{
567 struct module *i;
568
569 list_for_each_entry(i, &modules, list) {
570 struct module_use *use;
571
572 list_for_each_entry(use, &i->modules_which_use_me, list) {
573 if (use->module_which_uses == mod) {
574 DEBUGP("%s unusing %s\n", mod->name, i->name);
575 module_put(i);
576 list_del(&use->list);
577 kfree(use);
Kay Sievers270a6c42007-01-18 13:26:15 +0100578 sysfs_remove_link(i->holders_dir, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 /* There can be at most one match. */
580 break;
581 }
582 }
583 }
584}
585
586#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800587static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 int ret = (flags & O_TRUNC);
590 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800591 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return ret;
593}
594#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800595static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
597 return 0;
598}
599#endif /* CONFIG_MODULE_FORCE_UNLOAD */
600
601struct stopref
602{
603 struct module *mod;
604 int flags;
605 int *forced;
606};
607
608/* Whole machine is stopped with interrupts off when this runs. */
609static int __try_stop_module(void *_sref)
610{
611 struct stopref *sref = _sref;
612
613 /* If it's not unused, quit unless we are told to block. */
614 if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800615 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return -EWOULDBLOCK;
617 }
618
619 /* Mark it as dying. */
620 sref->mod->state = MODULE_STATE_GOING;
621 return 0;
622}
623
624static int try_stop_module(struct module *mod, int flags, int *forced)
625{
626 struct stopref sref = { mod, flags, forced };
627
628 return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
629}
630
631unsigned int module_refcount(struct module *mod)
632{
633 unsigned int i, total = 0;
634
635 for (i = 0; i < NR_CPUS; i++)
636 total += local_read(&mod->ref[i].count);
637 return total;
638}
639EXPORT_SYMBOL(module_refcount);
640
641/* This exists whether we can unload or not */
642static void free_module(struct module *mod);
643
644static void wait_for_zero_refcount(struct module *mod)
645{
646 /* Since we might sleep for some time, drop the semaphore first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800647 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 for (;;) {
649 DEBUGP("Looking at refcount...\n");
650 set_current_state(TASK_UNINTERRUPTIBLE);
651 if (module_refcount(mod) == 0)
652 break;
653 schedule();
654 }
655 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800656 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800659asmlinkage long
660sys_delete_module(const char __user *name_user, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800663 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 int ret, forced = 0;
665
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800666 if (!capable(CAP_SYS_MODULE))
667 return -EPERM;
668
669 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
670 return -EFAULT;
671 name[MODULE_NAME_LEN-1] = '\0';
672
Ashutosh Naik6389a382006-03-23 03:00:46 -0800673 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return -EINTR;
675
676 mod = find_module(name);
677 if (!mod) {
678 ret = -ENOENT;
679 goto out;
680 }
681
682 if (!list_empty(&mod->modules_which_use_me)) {
683 /* Other modules depend on us: get rid of them first. */
684 ret = -EWOULDBLOCK;
685 goto out;
686 }
687
688 /* Doing init or already dying? */
689 if (mod->state != MODULE_STATE_LIVE) {
690 /* FIXME: if (force), slam module count and wake up
691 waiter --RR */
692 DEBUGP("%s already dying\n", mod->name);
693 ret = -EBUSY;
694 goto out;
695 }
696
697 /* If it has an init func, it must have an exit func to unload */
698 if ((mod->init != NULL && mod->exit == NULL)
699 || mod->unsafe) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800700 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (!forced) {
702 /* This module can't be removed */
703 ret = -EBUSY;
704 goto out;
705 }
706 }
707
708 /* Set this up before setting mod->state */
709 mod->waiter = current;
710
711 /* Stop the machine so refcounts can't move and disable module. */
712 ret = try_stop_module(mod, flags, &forced);
713 if (ret != 0)
714 goto out;
715
716 /* Never wait if forced. */
717 if (!forced && module_refcount(mod) != 0)
718 wait_for_zero_refcount(mod);
719
720 /* Final destruction now noone is using it. */
721 if (mod->exit != NULL) {
Ashutosh Naik6389a382006-03-23 03:00:46 -0800722 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 mod->exit();
Ashutosh Naik6389a382006-03-23 03:00:46 -0800724 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726 free_module(mod);
727
728 out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800729 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return ret;
731}
732
733static void print_unload_info(struct seq_file *m, struct module *mod)
734{
735 struct module_use *use;
736 int printed_something = 0;
737
738 seq_printf(m, " %u ", module_refcount(mod));
739
740 /* Always include a trailing , so userspace can differentiate
741 between this and the old multi-field proc format. */
742 list_for_each_entry(use, &mod->modules_which_use_me, list) {
743 printed_something = 1;
744 seq_printf(m, "%s,", use->module_which_uses->name);
745 }
746
747 if (mod->unsafe) {
748 printed_something = 1;
749 seq_printf(m, "[unsafe],");
750 }
751
752 if (mod->init != NULL && mod->exit == NULL) {
753 printed_something = 1;
754 seq_printf(m, "[permanent],");
755 }
756
757 if (!printed_something)
758 seq_printf(m, "-");
759}
760
761void __symbol_put(const char *symbol)
762{
763 struct module *owner;
764 unsigned long flags;
765 const unsigned long *crc;
766
767 spin_lock_irqsave(&modlist_lock, flags);
768 if (!__find_symbol(symbol, &owner, &crc, 1))
769 BUG();
770 module_put(owner);
771 spin_unlock_irqrestore(&modlist_lock, flags);
772}
773EXPORT_SYMBOL(__symbol_put);
774
775void symbol_put_addr(void *addr)
776{
Trent Piepho5e376612006-05-15 09:44:06 -0700777 struct module *modaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Trent Piepho5e376612006-05-15 09:44:06 -0700779 if (core_kernel_text((unsigned long)addr))
780 return;
781
782 if (!(modaddr = module_text_address((unsigned long)addr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 BUG();
Trent Piepho5e376612006-05-15 09:44:06 -0700784 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786EXPORT_SYMBOL_GPL(symbol_put_addr);
787
788static ssize_t show_refcnt(struct module_attribute *mattr,
789 struct module *mod, char *buffer)
790{
791 /* sysfs holds a reference */
792 return sprintf(buffer, "%u\n", module_refcount(mod)-1);
793}
794
795static struct module_attribute refcnt = {
796 .attr = { .name = "refcnt", .mode = 0444, .owner = THIS_MODULE },
797 .show = show_refcnt,
798};
799
Al Virof6a57032006-10-18 01:47:25 -0400800void module_put(struct module *module)
801{
802 if (module) {
803 unsigned int cpu = get_cpu();
804 local_dec(&module->ref[cpu].count);
805 /* Maybe they're waiting for us to drop reference? */
806 if (unlikely(!module_is_live(module)))
807 wake_up_process(module->waiter);
808 put_cpu();
809 }
810}
811EXPORT_SYMBOL(module_put);
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813#else /* !CONFIG_MODULE_UNLOAD */
814static void print_unload_info(struct seq_file *m, struct module *mod)
815{
816 /* We don't know the usage count, or what modules are using. */
817 seq_printf(m, " - -");
818}
819
820static inline void module_unload_free(struct module *mod)
821{
822}
823
824static inline int use_module(struct module *a, struct module *b)
825{
826 return strong_try_module_get(b);
827}
828
829static inline void module_unload_init(struct module *mod)
830{
831}
832#endif /* CONFIG_MODULE_UNLOAD */
833
Kay Sievers1f717402006-11-24 12:15:25 +0100834static ssize_t show_initstate(struct module_attribute *mattr,
835 struct module *mod, char *buffer)
836{
837 const char *state = "unknown";
838
839 switch (mod->state) {
840 case MODULE_STATE_LIVE:
841 state = "live";
842 break;
843 case MODULE_STATE_COMING:
844 state = "coming";
845 break;
846 case MODULE_STATE_GOING:
847 state = "going";
848 break;
849 }
850 return sprintf(buffer, "%s\n", state);
851}
852
853static struct module_attribute initstate = {
854 .attr = { .name = "initstate", .mode = 0444, .owner = THIS_MODULE },
855 .show = show_initstate,
856};
857
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800858static struct module_attribute *modinfo_attrs[] = {
859 &modinfo_version,
860 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100861 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800862#ifdef CONFIG_MODULE_UNLOAD
863 &refcnt,
864#endif
865 NULL,
866};
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868static const char vermagic[] = VERMAGIC_STRING;
869
870#ifdef CONFIG_MODVERSIONS
871static int check_version(Elf_Shdr *sechdrs,
872 unsigned int versindex,
873 const char *symname,
874 struct module *mod,
875 const unsigned long *crc)
876{
877 unsigned int i, num_versions;
878 struct modversion_info *versions;
879
880 /* Exporting module didn't supply crcs? OK, we're already tainted. */
881 if (!crc)
882 return 1;
883
884 versions = (void *) sechdrs[versindex].sh_addr;
885 num_versions = sechdrs[versindex].sh_size
886 / sizeof(struct modversion_info);
887
888 for (i = 0; i < num_versions; i++) {
889 if (strcmp(versions[i].name, symname) != 0)
890 continue;
891
892 if (versions[i].crc == *crc)
893 return 1;
894 printk("%s: disagrees about version of symbol %s\n",
895 mod->name, symname);
896 DEBUGP("Found checksum %lX vs module %lX\n",
897 *crc, versions[i].crc);
898 return 0;
899 }
900 /* Not in module's version table. OK, but that taints the kernel. */
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700901 if (!(tainted & TAINT_FORCED_MODULE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 printk("%s: no version for \"%s\" found: kernel tainted.\n",
903 mod->name, symname);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700904 add_taint_module(mod, TAINT_FORCED_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return 1;
906}
907
908static inline int check_modstruct_version(Elf_Shdr *sechdrs,
909 unsigned int versindex,
910 struct module *mod)
911{
912 const unsigned long *crc;
913 struct module *owner;
914
915 if (!__find_symbol("struct_module", &owner, &crc, 1))
916 BUG();
917 return check_version(sechdrs, versindex, "struct_module", mod,
918 crc);
919}
920
921/* First part is kernel version, which we ignore. */
922static inline int same_magic(const char *amagic, const char *bmagic)
923{
924 amagic += strcspn(amagic, " ");
925 bmagic += strcspn(bmagic, " ");
926 return strcmp(amagic, bmagic) == 0;
927}
928#else
929static inline int check_version(Elf_Shdr *sechdrs,
930 unsigned int versindex,
931 const char *symname,
932 struct module *mod,
933 const unsigned long *crc)
934{
935 return 1;
936}
937
938static inline int check_modstruct_version(Elf_Shdr *sechdrs,
939 unsigned int versindex,
940 struct module *mod)
941{
942 return 1;
943}
944
945static inline int same_magic(const char *amagic, const char *bmagic)
946{
947 return strcmp(amagic, bmagic) == 0;
948}
949#endif /* CONFIG_MODVERSIONS */
950
951/* Resolve a symbol for this module. I.e. if we find one, record usage.
952 Must be holding module_mutex. */
953static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
954 unsigned int versindex,
955 const char *name,
956 struct module *mod)
957{
958 struct module *owner;
959 unsigned long ret;
960 const unsigned long *crc;
961
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700962 ret = __find_symbol(name, &owner, &crc,
963 !(mod->taints & TAINT_PROPRIETARY_MODULE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (ret) {
965 /* use_module can fail due to OOM, or module unloading */
966 if (!check_version(sechdrs, versindex, name, mod, crc) ||
967 !use_module(mod, owner))
968 ret = 0;
969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return ret;
971}
972
973
974/*
975 * /sys/module/foo/sections stuff
976 * J. Corbet <corbet@lwn.net>
977 */
978#ifdef CONFIG_KALLSYMS
979static ssize_t module_sect_show(struct module_attribute *mattr,
980 struct module *mod, char *buf)
981{
982 struct module_sect_attr *sattr =
983 container_of(mattr, struct module_sect_attr, mattr);
984 return sprintf(buf, "0x%lx\n", sattr->address);
985}
986
Ian S. Nelson04b1db92006-09-29 02:01:31 -0700987static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
988{
989 int section;
990
991 for (section = 0; section < sect_attrs->nsections; section++)
992 kfree(sect_attrs->attrs[section].name);
993 kfree(sect_attrs);
994}
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996static void add_sect_attrs(struct module *mod, unsigned int nsect,
997 char *secstrings, Elf_Shdr *sechdrs)
998{
999 unsigned int nloaded = 0, i, size[2];
1000 struct module_sect_attrs *sect_attrs;
1001 struct module_sect_attr *sattr;
1002 struct attribute **gattr;
1003
1004 /* Count loaded sections and allocate structures */
1005 for (i = 0; i < nsect; i++)
1006 if (sechdrs[i].sh_flags & SHF_ALLOC)
1007 nloaded++;
1008 size[0] = ALIGN(sizeof(*sect_attrs)
1009 + nloaded * sizeof(sect_attrs->attrs[0]),
1010 sizeof(sect_attrs->grp.attrs[0]));
1011 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001012 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1013 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 return;
1015
1016 /* Setup section attributes. */
1017 sect_attrs->grp.name = "sections";
1018 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1019
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001020 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 sattr = &sect_attrs->attrs[0];
1022 gattr = &sect_attrs->grp.attrs[0];
1023 for (i = 0; i < nsect; i++) {
1024 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1025 continue;
1026 sattr->address = sechdrs[i].sh_addr;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001027 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1028 GFP_KERNEL);
1029 if (sattr->name == NULL)
1030 goto out;
1031 sect_attrs->nsections++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 sattr->mattr.show = module_sect_show;
1033 sattr->mattr.store = NULL;
1034 sattr->mattr.attr.name = sattr->name;
1035 sattr->mattr.attr.owner = mod;
1036 sattr->mattr.attr.mode = S_IRUGO;
1037 *(gattr++) = &(sattr++)->mattr.attr;
1038 }
1039 *gattr = NULL;
1040
1041 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1042 goto out;
1043
1044 mod->sect_attrs = sect_attrs;
1045 return;
1046 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001047 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
1050static void remove_sect_attrs(struct module *mod)
1051{
1052 if (mod->sect_attrs) {
1053 sysfs_remove_group(&mod->mkobj.kobj,
1054 &mod->sect_attrs->grp);
1055 /* We are positive that no one is using any sect attrs
1056 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001057 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 mod->sect_attrs = NULL;
1059 }
1060}
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1065 char *sectstrings, Elf_Shdr *sechdrs)
1066{
1067}
1068
1069static inline void remove_sect_attrs(struct module *mod)
1070{
1071}
1072#endif /* CONFIG_KALLSYMS */
1073
Randy Dunlapef665c12007-02-13 15:19:06 -08001074#ifdef CONFIG_SYSFS
1075int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001076{
1077 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001078 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001079 int error = 0;
1080 int i;
1081
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001082 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1083 (ARRAY_SIZE(modinfo_attrs) + 1)),
1084 GFP_KERNEL);
1085 if (!mod->modinfo_attrs)
1086 return -ENOMEM;
1087
1088 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001089 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1090 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001091 (attr->test && attr->test(mod))) {
1092 memcpy(temp_attr, attr, sizeof(*temp_attr));
1093 temp_attr->attr.owner = mod;
1094 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1095 ++temp_attr;
1096 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001097 }
1098 return error;
1099}
1100
Randy Dunlapef665c12007-02-13 15:19:06 -08001101void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001102{
1103 struct module_attribute *attr;
1104 int i;
1105
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001106 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1107 /* pick a field to test for end of list */
1108 if (!attr->attr.name)
1109 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001110 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001111 if (attr->free)
1112 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001113 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001114 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001115}
Randy Dunlapef665c12007-02-13 15:19:06 -08001116#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Randy Dunlapef665c12007-02-13 15:19:06 -08001118#ifdef CONFIG_SYSFS
1119int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
1121 int err;
1122
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001123 if (!module_sysfs_initialized) {
1124 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001125 mod->name);
1126 err = -EINVAL;
1127 goto out;
1128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1130 err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name);
1131 if (err)
1132 goto out;
1133 kobj_set_kset_s(&mod->mkobj, module_subsys);
1134 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001135
Kay Sieverse17e0f52006-11-24 12:15:25 +01001136 kobject_init(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001137
1138out:
1139 return err;
1140}
1141
Randy Dunlapef665c12007-02-13 15:19:06 -08001142int mod_sysfs_setup(struct module *mod,
Kay Sievers270a6c42007-01-18 13:26:15 +01001143 struct kernel_param *kparam,
1144 unsigned int num_params)
1145{
1146 int err;
1147
1148 /* delay uevent until full sysfs population */
Kay Sieverse17e0f52006-11-24 12:15:25 +01001149 err = kobject_add(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (err)
1151 goto out;
1152
Kay Sievers270a6c42007-01-18 13:26:15 +01001153 mod->holders_dir = kobject_add_dir(&mod->mkobj.kobj, "holders");
Akinobu Mita240936e2007-04-26 00:12:09 -07001154 if (!mod->holders_dir) {
1155 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001156 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001157 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 err = module_param_sysfs_setup(mod, kparam, num_params);
1160 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001161 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Matt Domschc988d2b2005-06-23 22:05:15 -07001163 err = module_add_modinfo_attrs(mod);
1164 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001165 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001166
Kay Sieverse17e0f52006-11-24 12:15:25 +01001167 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 return 0;
1169
Kay Sieverse17e0f52006-11-24 12:15:25 +01001170out_unreg_param:
1171 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001172out_unreg_holders:
1173 kobject_unregister(mod->holders_dir);
1174out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001175 kobject_del(&mod->mkobj.kobj);
1176 kobject_put(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177out:
1178 return err;
1179}
Randy Dunlapef665c12007-02-13 15:19:06 -08001180#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182static void mod_kobject_remove(struct module *mod)
1183{
Matt Domschc988d2b2005-06-23 22:05:15 -07001184 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 module_param_sysfs_remove(mod);
Mariusz Kozlowskib92be9f2007-02-14 21:03:39 +01001186 kobject_unregister(mod->mkobj.drivers_dir);
1187 kobject_unregister(mod->holders_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 kobject_unregister(&mod->mkobj.kobj);
1189}
1190
1191/*
1192 * unlink the module with the whole machine is stopped with interrupts off
1193 * - this defends against kallsyms not taking locks
1194 */
1195static int __unlink_module(void *_mod)
1196{
1197 struct module *mod = _mod;
1198 list_del(&mod->list);
1199 return 0;
1200}
1201
1202/* Free a module, remove from lists, etc (must hold module mutex). */
1203static void free_module(struct module *mod)
1204{
1205 /* Delete from various lists */
1206 stop_machine_run(__unlink_module, mod, NR_CPUS);
1207 remove_sect_attrs(mod);
1208 mod_kobject_remove(mod);
1209
Jan Beulich4552d5d2006-06-26 13:57:28 +02001210 unwind_remove_table(mod->unwind_info, 0);
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 /* Arch-specific cleanup. */
1213 module_arch_cleanup(mod);
1214
1215 /* Module unload stuff */
1216 module_unload_free(mod);
1217
1218 /* This may be NULL, but that's OK */
1219 module_free(mod, mod->module_init);
1220 kfree(mod->args);
1221 if (mod->percpu)
1222 percpu_modfree(mod->percpu);
1223
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001224 /* Free lock-classes: */
1225 lockdep_free_key_range(mod->module_core, mod->core_size);
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 /* Finally, free the core (containing the module structure) */
1228 module_free(mod, mod->module_core);
1229}
1230
1231void *__symbol_get(const char *symbol)
1232{
1233 struct module *owner;
1234 unsigned long value, flags;
1235 const unsigned long *crc;
1236
1237 spin_lock_irqsave(&modlist_lock, flags);
1238 value = __find_symbol(symbol, &owner, &crc, 1);
1239 if (value && !strong_try_module_get(owner))
1240 value = 0;
1241 spin_unlock_irqrestore(&modlist_lock, flags);
1242
1243 return (void *)value;
1244}
1245EXPORT_SYMBOL_GPL(__symbol_get);
1246
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001247/*
1248 * Ensure that an exported symbol [global namespace] does not already exist
1249 * in the Kernel or in some other modules exported symbol table.
1250 */
1251static int verify_export_symbols(struct module *mod)
1252{
1253 const char *name = NULL;
1254 unsigned long i, ret = 0;
1255 struct module *owner;
1256 const unsigned long *crc;
1257
1258 for (i = 0; i < mod->num_syms; i++)
1259 if (__find_symbol(mod->syms[i].name, &owner, &crc, 1)) {
1260 name = mod->syms[i].name;
1261 ret = -ENOEXEC;
1262 goto dup;
1263 }
1264
1265 for (i = 0; i < mod->num_gpl_syms; i++)
1266 if (__find_symbol(mod->gpl_syms[i].name, &owner, &crc, 1)) {
1267 name = mod->gpl_syms[i].name;
1268 ret = -ENOEXEC;
1269 goto dup;
1270 }
1271
1272dup:
1273 if (ret)
1274 printk(KERN_ERR "%s: exports duplicate symbol %s (owned by %s)\n",
1275 mod->name, name, module_name(owner));
1276
1277 return ret;
1278}
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280/* Change all symbols so that sh_value encodes the pointer directly. */
1281static int simplify_symbols(Elf_Shdr *sechdrs,
1282 unsigned int symindex,
1283 const char *strtab,
1284 unsigned int versindex,
1285 unsigned int pcpuindex,
1286 struct module *mod)
1287{
1288 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1289 unsigned long secbase;
1290 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1291 int ret = 0;
1292
1293 for (i = 1; i < n; i++) {
1294 switch (sym[i].st_shndx) {
1295 case SHN_COMMON:
1296 /* We compiled with -fno-common. These are not
1297 supposed to happen. */
1298 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1299 printk("%s: please compile with -fno-common\n",
1300 mod->name);
1301 ret = -ENOEXEC;
1302 break;
1303
1304 case SHN_ABS:
1305 /* Don't need to do anything */
1306 DEBUGP("Absolute symbol: 0x%08lx\n",
1307 (long)sym[i].st_value);
1308 break;
1309
1310 case SHN_UNDEF:
1311 sym[i].st_value
1312 = resolve_symbol(sechdrs, versindex,
1313 strtab + sym[i].st_name, mod);
1314
1315 /* Ok if resolved. */
1316 if (sym[i].st_value != 0)
1317 break;
1318 /* Ok if weak. */
1319 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1320 break;
1321
1322 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1323 mod->name, strtab + sym[i].st_name);
1324 ret = -ENOENT;
1325 break;
1326
1327 default:
1328 /* Divert to percpu allocation if a percpu var. */
1329 if (sym[i].st_shndx == pcpuindex)
1330 secbase = (unsigned long)mod->percpu;
1331 else
1332 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1333 sym[i].st_value += secbase;
1334 break;
1335 }
1336 }
1337
1338 return ret;
1339}
1340
1341/* Update size with this section: return offset. */
1342static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1343{
1344 long ret;
1345
1346 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1347 *size = ret + sechdr->sh_size;
1348 return ret;
1349}
1350
1351/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1352 might -- code, read-only data, read-write data, small data. Tally
1353 sizes, and place the offsets into sh_entsize fields: high bit means it
1354 belongs in init. */
1355static void layout_sections(struct module *mod,
1356 const Elf_Ehdr *hdr,
1357 Elf_Shdr *sechdrs,
1358 const char *secstrings)
1359{
1360 static unsigned long const masks[][2] = {
1361 /* NOTE: all executable code must be the first section
1362 * in this array; otherwise modify the text_size
1363 * finder in the two loops below */
1364 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1365 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1366 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1367 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1368 };
1369 unsigned int m, i;
1370
1371 for (i = 0; i < hdr->e_shnum; i++)
1372 sechdrs[i].sh_entsize = ~0UL;
1373
1374 DEBUGP("Core section allocation order:\n");
1375 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1376 for (i = 0; i < hdr->e_shnum; ++i) {
1377 Elf_Shdr *s = &sechdrs[i];
1378
1379 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1380 || (s->sh_flags & masks[m][1])
1381 || s->sh_entsize != ~0UL
1382 || strncmp(secstrings + s->sh_name,
1383 ".init", 5) == 0)
1384 continue;
1385 s->sh_entsize = get_offset(&mod->core_size, s);
1386 DEBUGP("\t%s\n", secstrings + s->sh_name);
1387 }
1388 if (m == 0)
1389 mod->core_text_size = mod->core_size;
1390 }
1391
1392 DEBUGP("Init section allocation order:\n");
1393 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1394 for (i = 0; i < hdr->e_shnum; ++i) {
1395 Elf_Shdr *s = &sechdrs[i];
1396
1397 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1398 || (s->sh_flags & masks[m][1])
1399 || s->sh_entsize != ~0UL
1400 || strncmp(secstrings + s->sh_name,
1401 ".init", 5) != 0)
1402 continue;
1403 s->sh_entsize = (get_offset(&mod->init_size, s)
1404 | INIT_OFFSET_MASK);
1405 DEBUGP("\t%s\n", secstrings + s->sh_name);
1406 }
1407 if (m == 0)
1408 mod->init_text_size = mod->init_size;
1409 }
1410}
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412static void set_license(struct module *mod, const char *license)
1413{
1414 if (!license)
1415 license = "unspecified";
1416
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001417 if (!license_is_gpl_compatible(license)) {
1418 if (!(tainted & TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001419 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001420 "kernel.\n", mod->name, license);
1421 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
1423}
1424
1425/* Parse tag=value strings from .modinfo section */
1426static char *next_string(char *string, unsigned long *secsize)
1427{
1428 /* Skip non-zero chars */
1429 while (string[0]) {
1430 string++;
1431 if ((*secsize)-- <= 1)
1432 return NULL;
1433 }
1434
1435 /* Skip any zero padding. */
1436 while (!string[0]) {
1437 string++;
1438 if ((*secsize)-- <= 1)
1439 return NULL;
1440 }
1441 return string;
1442}
1443
1444static char *get_modinfo(Elf_Shdr *sechdrs,
1445 unsigned int info,
1446 const char *tag)
1447{
1448 char *p;
1449 unsigned int taglen = strlen(tag);
1450 unsigned long size = sechdrs[info].sh_size;
1451
1452 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1453 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1454 return p + taglen + 1;
1455 }
1456 return NULL;
1457}
1458
Matt Domschc988d2b2005-06-23 22:05:15 -07001459static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1460 unsigned int infoindex)
1461{
1462 struct module_attribute *attr;
1463 int i;
1464
1465 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1466 if (attr->setup)
1467 attr->setup(mod,
1468 get_modinfo(sechdrs,
1469 infoindex,
1470 attr->attr.name));
1471 }
1472}
Matt Domschc988d2b2005-06-23 22:05:15 -07001473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474#ifdef CONFIG_KALLSYMS
1475int is_exported(const char *name, const struct module *mod)
1476{
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001477 if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
1478 return 1;
1479 else
Jesper Juhlf867d2a2006-06-25 05:47:09 -07001480 if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return 1;
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001482 else
1483 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484}
1485
1486/* As per nm */
1487static char elf_type(const Elf_Sym *sym,
1488 Elf_Shdr *sechdrs,
1489 const char *secstrings,
1490 struct module *mod)
1491{
1492 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1493 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1494 return 'v';
1495 else
1496 return 'w';
1497 }
1498 if (sym->st_shndx == SHN_UNDEF)
1499 return 'U';
1500 if (sym->st_shndx == SHN_ABS)
1501 return 'a';
1502 if (sym->st_shndx >= SHN_LORESERVE)
1503 return '?';
1504 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1505 return 't';
1506 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1507 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1508 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1509 return 'r';
1510 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1511 return 'g';
1512 else
1513 return 'd';
1514 }
1515 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1516 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1517 return 's';
1518 else
1519 return 'b';
1520 }
1521 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1522 ".debug", strlen(".debug")) == 0)
1523 return 'n';
1524 return '?';
1525}
1526
1527static void add_kallsyms(struct module *mod,
1528 Elf_Shdr *sechdrs,
1529 unsigned int symindex,
1530 unsigned int strindex,
1531 const char *secstrings)
1532{
1533 unsigned int i;
1534
1535 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1536 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1537 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1538
1539 /* Set types up while we still have access to sections. */
1540 for (i = 0; i < mod->num_symtab; i++)
1541 mod->symtab[i].st_info
1542 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1543}
1544#else
1545static inline void add_kallsyms(struct module *mod,
1546 Elf_Shdr *sechdrs,
1547 unsigned int symindex,
1548 unsigned int strindex,
1549 const char *secstrings)
1550{
1551}
1552#endif /* CONFIG_KALLSYMS */
1553
1554/* Allocate and load the module: note that size of section 0 is always
1555 zero, and we rely on this for optional sections. */
1556static struct module *load_module(void __user *umod,
1557 unsigned long len,
1558 const char __user *uargs)
1559{
1560 Elf_Ehdr *hdr;
1561 Elf_Shdr *sechdrs;
1562 char *secstrings, *args, *modmagic, *strtab = NULL;
Andrew Morton84860f92006-06-28 04:26:46 -07001563 unsigned int i;
1564 unsigned int symindex = 0;
1565 unsigned int strindex = 0;
1566 unsigned int setupindex;
1567 unsigned int exindex;
1568 unsigned int exportindex;
1569 unsigned int modindex;
1570 unsigned int obsparmindex;
1571 unsigned int infoindex;
1572 unsigned int gplindex;
1573 unsigned int crcindex;
1574 unsigned int gplcrcindex;
1575 unsigned int versindex;
1576 unsigned int pcpuindex;
1577 unsigned int gplfutureindex;
1578 unsigned int gplfuturecrcindex;
1579 unsigned int unwindex = 0;
1580 unsigned int unusedindex;
1581 unsigned int unusedcrcindex;
1582 unsigned int unusedgplindex;
1583 unsigned int unusedgplcrcindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 struct module *mod;
1585 long err = 0;
1586 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1587 struct exception_table_entry *extable;
Thomas Koeller378bac82005-09-06 15:17:11 -07001588 mm_segment_t old_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1591 umod, len, uargs);
1592 if (len < sizeof(*hdr))
1593 return ERR_PTR(-ENOEXEC);
1594
1595 /* Suck in entire file: we'll want most of it. */
1596 /* vmalloc barfs on "unusual" numbers. Check here */
1597 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1598 return ERR_PTR(-ENOMEM);
1599 if (copy_from_user(hdr, umod, len) != 0) {
1600 err = -EFAULT;
1601 goto free_hdr;
1602 }
1603
1604 /* Sanity checks against insmoding binaries or wrong arch,
1605 weird elf version */
1606 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1607 || hdr->e_type != ET_REL
1608 || !elf_check_arch(hdr)
1609 || hdr->e_shentsize != sizeof(*sechdrs)) {
1610 err = -ENOEXEC;
1611 goto free_hdr;
1612 }
1613
1614 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1615 goto truncated;
1616
1617 /* Convenience variables */
1618 sechdrs = (void *)hdr + hdr->e_shoff;
1619 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1620 sechdrs[0].sh_addr = 0;
1621
1622 for (i = 1; i < hdr->e_shnum; i++) {
1623 if (sechdrs[i].sh_type != SHT_NOBITS
1624 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1625 goto truncated;
1626
1627 /* Mark all sections sh_addr with their address in the
1628 temporary image. */
1629 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1630
1631 /* Internal symbols and strings. */
1632 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1633 symindex = i;
1634 strindex = sechdrs[i].sh_link;
1635 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1636 }
1637#ifndef CONFIG_MODULE_UNLOAD
1638 /* Don't load .exit sections */
1639 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1640 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1641#endif
1642 }
1643
1644 modindex = find_sec(hdr, sechdrs, secstrings,
1645 ".gnu.linkonce.this_module");
1646 if (!modindex) {
1647 printk(KERN_WARNING "No module found in object\n");
1648 err = -ENOEXEC;
1649 goto free_hdr;
1650 }
1651 mod = (void *)sechdrs[modindex].sh_addr;
1652
1653 if (symindex == 0) {
1654 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1655 mod->name);
1656 err = -ENOEXEC;
1657 goto free_hdr;
1658 }
1659
1660 /* Optional sections */
1661 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1662 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001663 gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future");
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001664 unusedindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused");
1665 unusedgplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused_gpl");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1667 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001668 gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future");
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001669 unusedcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused");
1670 unusedgplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused_gpl");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1672 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1673 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1674 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1675 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1676 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
Jan Beulich4552d5d2006-06-26 13:57:28 +02001677#ifdef ARCH_UNWIND_SECTION_NAME
1678 unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
1679#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 /* Don't keep modinfo section */
1682 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1683#ifdef CONFIG_KALLSYMS
1684 /* Keep symbol and string tables for decoding later. */
1685 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1686 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1687#endif
Jan Beulich4552d5d2006-06-26 13:57:28 +02001688 if (unwindex)
1689 sechdrs[unwindex].sh_flags |= SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 /* Check module struct version now, before we try to use module. */
1692 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1693 err = -ENOEXEC;
1694 goto free_hdr;
1695 }
1696
1697 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1698 /* This is allowed: modprobe --force will invalidate it. */
1699 if (!modmagic) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001700 add_taint_module(mod, TAINT_FORCED_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1702 mod->name);
1703 } else if (!same_magic(modmagic, vermagic)) {
1704 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1705 mod->name, modmagic, vermagic);
1706 err = -ENOEXEC;
1707 goto free_hdr;
1708 }
1709
1710 /* Now copy in args */
Davi Arnaut24277dd2006-03-24 03:18:43 -08001711 args = strndup_user(uargs, ~0UL >> 1);
1712 if (IS_ERR(args)) {
1713 err = PTR_ERR(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 goto free_hdr;
1715 }
Andrew Morton8e08b752006-02-07 12:58:45 -08001716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 if (find_module(mod->name)) {
1718 err = -EEXIST;
1719 goto free_mod;
1720 }
1721
1722 mod->state = MODULE_STATE_COMING;
1723
1724 /* Allow arches to frob section contents and sizes. */
1725 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1726 if (err < 0)
1727 goto free_mod;
1728
1729 if (pcpuindex) {
1730 /* We have a special allocation for this section. */
1731 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
Rusty Russell842bbaa2005-08-01 21:11:47 -07001732 sechdrs[pcpuindex].sh_addralign,
1733 mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 if (!percpu) {
1735 err = -ENOMEM;
1736 goto free_mod;
1737 }
1738 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1739 mod->percpu = percpu;
1740 }
1741
1742 /* Determine total sizes, and put offsets in sh_entsize. For now
1743 this is done generically; there doesn't appear to be any
1744 special cases for the architectures. */
1745 layout_sections(mod, hdr, sechdrs, secstrings);
1746
1747 /* Do the allocs. */
1748 ptr = module_alloc(mod->core_size);
1749 if (!ptr) {
1750 err = -ENOMEM;
1751 goto free_percpu;
1752 }
1753 memset(ptr, 0, mod->core_size);
1754 mod->module_core = ptr;
1755
1756 ptr = module_alloc(mod->init_size);
1757 if (!ptr && mod->init_size) {
1758 err = -ENOMEM;
1759 goto free_core;
1760 }
1761 memset(ptr, 0, mod->init_size);
1762 mod->module_init = ptr;
1763
1764 /* Transfer each section which specifies SHF_ALLOC */
1765 DEBUGP("final section addresses:\n");
1766 for (i = 0; i < hdr->e_shnum; i++) {
1767 void *dest;
1768
1769 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1770 continue;
1771
1772 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1773 dest = mod->module_init
1774 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1775 else
1776 dest = mod->module_core + sechdrs[i].sh_entsize;
1777
1778 if (sechdrs[i].sh_type != SHT_NOBITS)
1779 memcpy(dest, (void *)sechdrs[i].sh_addr,
1780 sechdrs[i].sh_size);
1781 /* Update sh_addr to point to copy in image. */
1782 sechdrs[i].sh_addr = (unsigned long)dest;
1783 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1784 }
1785 /* Module has been moved. */
1786 mod = (void *)sechdrs[modindex].sh_addr;
1787
1788 /* Now we've moved module, initialize linked lists, etc. */
1789 module_unload_init(mod);
1790
Kay Sievers270a6c42007-01-18 13:26:15 +01001791 /* Initialize kobject, so we can reference it. */
1792 if (mod_sysfs_init(mod) != 0)
1793 goto cleanup;
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 /* Set up license info based on the info section */
1796 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1797
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001798 if (strcmp(mod->name, "ndiswrapper") == 0)
Randy Dunlap0e2d57f2006-10-29 22:46:34 -08001799 add_taint(TAINT_PROPRIETARY_MODULE);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001800 if (strcmp(mod->name, "driverloader") == 0)
1801 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Dave Jones9841d61d2006-01-08 01:03:41 -08001802
Matt Domschc988d2b2005-06-23 22:05:15 -07001803 /* Set up MODINFO_ATTR fields */
1804 setup_modinfo(mod, sechdrs, infoindex);
Matt Domschc988d2b2005-06-23 22:05:15 -07001805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 /* Fix up syms, so that st_value is a pointer to location. */
1807 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
1808 mod);
1809 if (err < 0)
1810 goto cleanup;
1811
1812 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1813 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1814 mod->syms = (void *)sechdrs[exportindex].sh_addr;
1815 if (crcindex)
1816 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1817 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1818 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1819 if (gplcrcindex)
1820 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001821 mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size /
1822 sizeof(*mod->gpl_future_syms);
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001823 mod->num_unused_syms = sechdrs[unusedindex].sh_size /
1824 sizeof(*mod->unused_syms);
1825 mod->num_unused_gpl_syms = sechdrs[unusedgplindex].sh_size /
1826 sizeof(*mod->unused_gpl_syms);
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001827 mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr;
1828 if (gplfuturecrcindex)
1829 mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001831 mod->unused_syms = (void *)sechdrs[unusedindex].sh_addr;
1832 if (unusedcrcindex)
1833 mod->unused_crcs = (void *)sechdrs[unusedcrcindex].sh_addr;
1834 mod->unused_gpl_syms = (void *)sechdrs[unusedgplindex].sh_addr;
1835 if (unusedgplcrcindex)
1836 mod->unused_crcs = (void *)sechdrs[unusedgplcrcindex].sh_addr;
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838#ifdef CONFIG_MODVERSIONS
1839 if ((mod->num_syms && !crcindex) ||
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001840 (mod->num_gpl_syms && !gplcrcindex) ||
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001841 (mod->num_gpl_future_syms && !gplfuturecrcindex) ||
1842 (mod->num_unused_syms && !unusedcrcindex) ||
1843 (mod->num_unused_gpl_syms && !unusedgplcrcindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 printk(KERN_WARNING "%s: No versions for exported symbols."
1845 " Tainting kernel.\n", mod->name);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001846 add_taint_module(mod, TAINT_FORCED_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 }
1848#endif
1849
1850 /* Now do relocations. */
1851 for (i = 1; i < hdr->e_shnum; i++) {
1852 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1853 unsigned int info = sechdrs[i].sh_info;
1854
1855 /* Not a valid relocation section? */
1856 if (info >= hdr->e_shnum)
1857 continue;
1858
1859 /* Don't bother with non-allocated sections */
1860 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
1861 continue;
1862
1863 if (sechdrs[i].sh_type == SHT_REL)
1864 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1865 else if (sechdrs[i].sh_type == SHT_RELA)
1866 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1867 mod);
1868 if (err < 0)
1869 goto cleanup;
1870 }
1871
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001872 /* Find duplicate symbols */
1873 err = verify_export_symbols(mod);
1874
1875 if (err < 0)
1876 goto cleanup;
1877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 /* Set up and sort exception table */
1879 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1880 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
1881 sort_extable(extable, extable + mod->num_exentries);
1882
1883 /* Finally, copy percpu area over. */
1884 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
1885 sechdrs[pcpuindex].sh_size);
1886
1887 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
1888
1889 err = module_finalize(hdr, sechdrs, mod);
1890 if (err < 0)
1891 goto cleanup;
1892
Thomas Koeller378bac82005-09-06 15:17:11 -07001893 /* flush the icache in correct context */
1894 old_fs = get_fs();
1895 set_fs(KERNEL_DS);
1896
1897 /*
1898 * Flush the instruction cache, since we've played with text.
1899 * Do it before processing of module parameters, so the module
1900 * can provide parameter accessor functions of its own.
1901 */
1902 if (mod->module_init)
1903 flush_icache_range((unsigned long)mod->module_init,
1904 (unsigned long)mod->module_init
1905 + mod->init_size);
1906 flush_icache_range((unsigned long)mod->module_core,
1907 (unsigned long)mod->module_core + mod->core_size);
1908
1909 set_fs(old_fs);
1910
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 mod->args = args;
Rusty Russell8d3b33f2006-03-25 03:07:05 -08001912 if (obsparmindex)
1913 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
1914 mod->name);
1915
1916 /* Size of section 0 is 0, so this works well if no params */
1917 err = parse_args(mod->name, mod->args,
1918 (struct kernel_param *)
1919 sechdrs[setupindex].sh_addr,
1920 sechdrs[setupindex].sh_size
1921 / sizeof(struct kernel_param),
1922 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 if (err < 0)
1924 goto arch_cleanup;
1925
1926 err = mod_sysfs_setup(mod,
1927 (struct kernel_param *)
1928 sechdrs[setupindex].sh_addr,
1929 sechdrs[setupindex].sh_size
1930 / sizeof(struct kernel_param));
1931 if (err < 0)
1932 goto arch_cleanup;
1933 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
1934
Jan Beulich4552d5d2006-06-26 13:57:28 +02001935 /* Size of section 0 is 0, so this works well if no unwind info. */
1936 mod->unwind_info = unwind_add_table(mod,
1937 (void *)sechdrs[unwindex].sh_addr,
1938 sechdrs[unwindex].sh_size);
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 /* Get rid of temporary copy */
1941 vfree(hdr);
1942
1943 /* Done! */
1944 return mod;
1945
1946 arch_cleanup:
1947 module_arch_cleanup(mod);
1948 cleanup:
1949 module_unload_free(mod);
1950 module_free(mod, mod->module_init);
1951 free_core:
1952 module_free(mod, mod->module_core);
1953 free_percpu:
1954 if (percpu)
1955 percpu_modfree(percpu);
1956 free_mod:
1957 kfree(args);
1958 free_hdr:
1959 vfree(hdr);
Jayachandran C6fe2e702006-01-06 00:19:54 -08001960 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
1962 truncated:
1963 printk(KERN_ERR "Module len %lu truncated\n", len);
1964 err = -ENOEXEC;
1965 goto free_hdr;
1966}
1967
1968/*
1969 * link the module with the whole machine is stopped with interrupts off
1970 * - this defends against kallsyms not taking locks
1971 */
1972static int __link_module(void *_mod)
1973{
1974 struct module *mod = _mod;
1975 list_add(&mod->list, &modules);
1976 return 0;
1977}
1978
1979/* This is where the real work happens */
1980asmlinkage long
1981sys_init_module(void __user *umod,
1982 unsigned long len,
1983 const char __user *uargs)
1984{
1985 struct module *mod;
1986 int ret = 0;
1987
1988 /* Must have permission */
1989 if (!capable(CAP_SYS_MODULE))
1990 return -EPERM;
1991
1992 /* Only one module load at a time, please */
Ashutosh Naik6389a382006-03-23 03:00:46 -08001993 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 return -EINTR;
1995
1996 /* Do all the hard work */
1997 mod = load_module(umod, len, uargs);
1998 if (IS_ERR(mod)) {
Ashutosh Naik6389a382006-03-23 03:00:46 -08001999 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 return PTR_ERR(mod);
2001 }
2002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 /* Now sew it into the lists. They won't access us, since
2004 strong_try_module_get() will fail. */
2005 stop_machine_run(__link_module, mod, NR_CPUS);
2006
2007 /* Drop lock so they can recurse */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002008 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Alan Sterne041c682006-03-27 01:16:30 -08002010 blocking_notifier_call_chain(&module_notify_list,
2011 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 /* Start the module */
2014 if (mod->init != NULL)
2015 ret = mod->init();
2016 if (ret < 0) {
2017 /* Init routine failed: abort. Try to protect us from
2018 buggy refcounters. */
2019 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002020 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 if (mod->unsafe)
2022 printk(KERN_ERR "%s: module is now stuck!\n",
2023 mod->name);
2024 else {
2025 module_put(mod);
Ashutosh Naik6389a382006-03-23 03:00:46 -08002026 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 free_module(mod);
Ashutosh Naik6389a382006-03-23 03:00:46 -08002028 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 }
2030 return ret;
2031 }
2032
2033 /* Now it's a first class citizen! */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002034 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 mod->state = MODULE_STATE_LIVE;
2036 /* Drop initial reference. */
2037 module_put(mod);
Jan Beulich4552d5d2006-06-26 13:57:28 +02002038 unwind_remove_table(mod->unwind_info, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 module_free(mod, mod->module_init);
2040 mod->module_init = NULL;
2041 mod->init_size = 0;
2042 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002043 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045 return 0;
2046}
2047
2048static inline int within(unsigned long addr, void *start, unsigned long size)
2049{
2050 return ((void *)addr >= start && (void *)addr < start + size);
2051}
2052
2053#ifdef CONFIG_KALLSYMS
2054/*
2055 * This ignores the intensely annoying "mapping symbols" found
2056 * in ARM ELF files: $a, $t and $d.
2057 */
2058static inline int is_arm_mapping_symbol(const char *str)
2059{
2060 return str[0] == '$' && strchr("atd", str[1])
2061 && (str[2] == '\0' || str[2] == '.');
2062}
2063
2064static const char *get_ksymbol(struct module *mod,
2065 unsigned long addr,
2066 unsigned long *size,
2067 unsigned long *offset)
2068{
2069 unsigned int i, best = 0;
2070 unsigned long nextval;
2071
2072 /* At worse, next value is at end of module */
2073 if (within(addr, mod->module_init, mod->init_size))
2074 nextval = (unsigned long)mod->module_init+mod->init_text_size;
2075 else
2076 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2077
2078 /* Scan for closest preceeding symbol, and next symbol. (ELF
2079 starts real symbols at 1). */
2080 for (i = 1; i < mod->num_symtab; i++) {
2081 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2082 continue;
2083
2084 /* We ignore unnamed symbols: they're uninformative
2085 * and inserted at a whim. */
2086 if (mod->symtab[i].st_value <= addr
2087 && mod->symtab[i].st_value > mod->symtab[best].st_value
2088 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2089 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2090 best = i;
2091 if (mod->symtab[i].st_value > addr
2092 && mod->symtab[i].st_value < nextval
2093 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2094 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2095 nextval = mod->symtab[i].st_value;
2096 }
2097
2098 if (!best)
2099 return NULL;
2100
2101 *size = nextval - mod->symtab[best].st_value;
2102 *offset = addr - mod->symtab[best].st_value;
2103 return mod->strtab + mod->symtab[best].st_name;
2104}
2105
2106/* For kallsyms to ask for address resolution. NULL means not found.
2107 We don't lock, as this is used for oops resolution and races are a
2108 lesser concern. */
2109const char *module_address_lookup(unsigned long addr,
2110 unsigned long *size,
2111 unsigned long *offset,
2112 char **modname)
2113{
2114 struct module *mod;
2115
2116 list_for_each_entry(mod, &modules, list) {
2117 if (within(addr, mod->module_init, mod->init_size)
2118 || within(addr, mod->module_core, mod->core_size)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002119 if (modname)
2120 *modname = mod->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 return get_ksymbol(mod, addr, size, offset);
2122 }
2123 }
2124 return NULL;
2125}
2126
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002127struct module *module_get_kallsym(unsigned int symnum, unsigned long *value,
Alexey Dobriyanae84e322007-05-08 00:28:38 -07002128 char *type, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
2130 struct module *mod;
2131
Ashutosh Naik6389a382006-03-23 03:00:46 -08002132 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 list_for_each_entry(mod, &modules, list) {
2134 if (symnum < mod->num_symtab) {
2135 *value = mod->symtab[symnum].st_value;
2136 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002137 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Alexey Dobriyanae84e322007-05-08 00:28:38 -07002138 KSYM_NAME_LEN + 1);
Ashutosh Naik6389a382006-03-23 03:00:46 -08002139 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 return mod;
2141 }
2142 symnum -= mod->num_symtab;
2143 }
Ashutosh Naik6389a382006-03-23 03:00:46 -08002144 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 return NULL;
2146}
2147
2148static unsigned long mod_find_symname(struct module *mod, const char *name)
2149{
2150 unsigned int i;
2151
2152 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002153 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2154 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 return mod->symtab[i].st_value;
2156 return 0;
2157}
2158
2159/* Look for this name: can be of form module:name. */
2160unsigned long module_kallsyms_lookup_name(const char *name)
2161{
2162 struct module *mod;
2163 char *colon;
2164 unsigned long ret = 0;
2165
2166 /* Don't lock: we're in enough trouble already. */
2167 if ((colon = strchr(name, ':')) != NULL) {
2168 *colon = '\0';
2169 if ((mod = find_module(name)) != NULL)
2170 ret = mod_find_symname(mod, colon+1);
2171 *colon = ':';
2172 } else {
2173 list_for_each_entry(mod, &modules, list)
2174 if ((ret = mod_find_symname(mod, name)) != 0)
2175 break;
2176 }
2177 return ret;
2178}
2179#endif /* CONFIG_KALLSYMS */
2180
2181/* Called by the /proc file system to return a list of modules. */
2182static void *m_start(struct seq_file *m, loff_t *pos)
2183{
2184 struct list_head *i;
2185 loff_t n = 0;
2186
Ashutosh Naik6389a382006-03-23 03:00:46 -08002187 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 list_for_each(i, &modules) {
2189 if (n++ == *pos)
2190 break;
2191 }
2192 if (i == &modules)
2193 return NULL;
2194 return i;
2195}
2196
2197static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2198{
2199 struct list_head *i = p;
2200 (*pos)++;
2201 if (i->next == &modules)
2202 return NULL;
2203 return i->next;
2204}
2205
2206static void m_stop(struct seq_file *m, void *p)
2207{
Ashutosh Naik6389a382006-03-23 03:00:46 -08002208 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209}
2210
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002211static char *taint_flags(unsigned int taints, char *buf)
2212{
2213 int bx = 0;
2214
2215 if (taints) {
2216 buf[bx++] = '(';
2217 if (taints & TAINT_PROPRIETARY_MODULE)
2218 buf[bx++] = 'P';
2219 if (taints & TAINT_FORCED_MODULE)
2220 buf[bx++] = 'F';
2221 /*
2222 * TAINT_FORCED_RMMOD: could be added.
2223 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2224 * apply to modules.
2225 */
2226 buf[bx++] = ')';
2227 }
2228 buf[bx] = '\0';
2229
2230 return buf;
2231}
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233static int m_show(struct seq_file *m, void *p)
2234{
2235 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002236 char buf[8];
2237
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 seq_printf(m, "%s %lu",
2239 mod->name, mod->init_size + mod->core_size);
2240 print_unload_info(m, mod);
2241
2242 /* Informative for users. */
2243 seq_printf(m, " %s",
2244 mod->state == MODULE_STATE_GOING ? "Unloading":
2245 mod->state == MODULE_STATE_COMING ? "Loading":
2246 "Live");
2247 /* Used by oprofile and other similar tools. */
2248 seq_printf(m, " 0x%p", mod->module_core);
2249
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002250 /* Taints info */
2251 if (mod->taints)
2252 seq_printf(m, " %s", taint_flags(mod->taints, buf));
2253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 seq_printf(m, "\n");
2255 return 0;
2256}
2257
2258/* Format: modulename size refcount deps address
2259
2260 Where refcount is a number or -, and deps is a comma-separated list
2261 of depends or -.
2262*/
Helge Deller15ad7cd2006-12-06 20:40:36 -08002263const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 .start = m_start,
2265 .next = m_next,
2266 .stop = m_stop,
2267 .show = m_show
2268};
2269
2270/* Given an address, look for it in the module exception tables. */
2271const struct exception_table_entry *search_module_extables(unsigned long addr)
2272{
2273 unsigned long flags;
2274 const struct exception_table_entry *e = NULL;
2275 struct module *mod;
2276
2277 spin_lock_irqsave(&modlist_lock, flags);
2278 list_for_each_entry(mod, &modules, list) {
2279 if (mod->num_exentries == 0)
2280 continue;
2281
2282 e = search_extable(mod->extable,
2283 mod->extable + mod->num_exentries - 1,
2284 addr);
2285 if (e)
2286 break;
2287 }
2288 spin_unlock_irqrestore(&modlist_lock, flags);
2289
2290 /* Now, if we found one, we are running inside it now, hence
2291 we cannot unload the module, hence no refcnt needed. */
2292 return e;
2293}
2294
Ingo Molnar4d435f92006-07-03 00:24:24 -07002295/*
2296 * Is this a valid module address?
2297 */
2298int is_module_address(unsigned long addr)
2299{
2300 unsigned long flags;
2301 struct module *mod;
2302
2303 spin_lock_irqsave(&modlist_lock, flags);
2304
2305 list_for_each_entry(mod, &modules, list) {
2306 if (within(addr, mod->module_core, mod->core_size)) {
2307 spin_unlock_irqrestore(&modlist_lock, flags);
2308 return 1;
2309 }
2310 }
2311
2312 spin_unlock_irqrestore(&modlist_lock, flags);
2313
2314 return 0;
2315}
2316
2317
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318/* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
2319struct module *__module_text_address(unsigned long addr)
2320{
2321 struct module *mod;
2322
2323 list_for_each_entry(mod, &modules, list)
2324 if (within(addr, mod->module_init, mod->init_text_size)
2325 || within(addr, mod->module_core, mod->core_text_size))
2326 return mod;
2327 return NULL;
2328}
2329
2330struct module *module_text_address(unsigned long addr)
2331{
2332 struct module *mod;
2333 unsigned long flags;
2334
2335 spin_lock_irqsave(&modlist_lock, flags);
2336 mod = __module_text_address(addr);
2337 spin_unlock_irqrestore(&modlist_lock, flags);
2338
2339 return mod;
2340}
2341
2342/* Don't grab lock, we're oopsing. */
2343void print_modules(void)
2344{
2345 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07002346 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
2348 printk("Modules linked in:");
2349 list_for_each_entry(mod, &modules, list)
Randy Dunlap2bc2d612006-10-02 02:17:02 -07002350 printk(" %s%s", mod->name, taint_flags(mod->taints, buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 printk("\n");
2352}
2353
Randy Dunlapef665c12007-02-13 15:19:06 -08002354#ifdef CONFIG_SYSFS
Kay Sievers7f422e22006-12-20 10:39:33 +01002355static char *make_driver_name(struct device_driver *drv)
2356{
2357 char *driver_name;
2358
2359 driver_name = kmalloc(strlen(drv->name) + strlen(drv->bus->name) + 2,
2360 GFP_KERNEL);
2361 if (!driver_name)
2362 return NULL;
2363
2364 sprintf(driver_name, "%s:%s", drv->bus->name, drv->name);
2365 return driver_name;
2366}
2367
Greg Kroah-Hartmanfe480a22007-01-15 11:50:02 -08002368static void module_create_drivers_dir(struct module_kobject *mk)
2369{
2370 if (!mk || mk->drivers_dir)
2371 return;
2372
2373 mk->drivers_dir = kobject_add_dir(&mk->kobj, "drivers");
2374}
2375
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376void module_add_driver(struct module *mod, struct device_driver *drv)
2377{
Kay Sievers7f422e22006-12-20 10:39:33 +01002378 char *driver_name;
Kay Sieverse17e0f52006-11-24 12:15:25 +01002379 int no_warn;
Kay Sieversf30c53a2007-01-15 20:22:02 +01002380 struct module_kobject *mk = NULL;
Kay Sieverse17e0f52006-11-24 12:15:25 +01002381
Kay Sieversf30c53a2007-01-15 20:22:02 +01002382 if (!drv)
2383 return;
2384
2385 if (mod)
2386 mk = &mod->mkobj;
2387 else if (drv->mod_name) {
2388 struct kobject *mkobj;
2389
2390 /* Lookup built-in module entry in /sys/modules */
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07002391 mkobj = kset_find_obj(&module_subsys, drv->mod_name);
Kay Sievers0c84ce22007-04-01 23:49:48 -07002392 if (mkobj) {
Kay Sieversf30c53a2007-01-15 20:22:02 +01002393 mk = container_of(mkobj, struct module_kobject, kobj);
Kay Sievers0c84ce22007-04-01 23:49:48 -07002394 /* remember our module structure */
2395 drv->mkobj = mk;
2396 /* kset_find_obj took a reference */
2397 kobject_put(mkobj);
2398 }
Kay Sieversf30c53a2007-01-15 20:22:02 +01002399 }
2400
2401 if (!mk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 return;
2403
Kay Sieverse17e0f52006-11-24 12:15:25 +01002404 /* Don't check return codes; these calls are idempotent */
Kay Sieversf30c53a2007-01-15 20:22:02 +01002405 no_warn = sysfs_create_link(&drv->kobj, &mk->kobj, "module");
Kay Sievers7f422e22006-12-20 10:39:33 +01002406 driver_name = make_driver_name(drv);
2407 if (driver_name) {
Greg Kroah-Hartmanfe480a22007-01-15 11:50:02 -08002408 module_create_drivers_dir(mk);
Kay Sieversf30c53a2007-01-15 20:22:02 +01002409 no_warn = sysfs_create_link(mk->drivers_dir, &drv->kobj,
Kay Sievers7f422e22006-12-20 10:39:33 +01002410 driver_name);
2411 kfree(driver_name);
2412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413}
2414EXPORT_SYMBOL(module_add_driver);
2415
2416void module_remove_driver(struct device_driver *drv)
2417{
Kay Sievers0c84ce22007-04-01 23:49:48 -07002418 struct module_kobject *mk = NULL;
Kay Sievers7f422e22006-12-20 10:39:33 +01002419 char *driver_name;
2420
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 if (!drv)
2422 return;
Kay Sievers7f422e22006-12-20 10:39:33 +01002423
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 sysfs_remove_link(&drv->kobj, "module");
Kay Sievers0c84ce22007-04-01 23:49:48 -07002425
2426 if (drv->owner)
2427 mk = &drv->owner->mkobj;
2428 else if (drv->mkobj)
2429 mk = drv->mkobj;
2430 if (mk && mk->drivers_dir) {
Kay Sievers7f422e22006-12-20 10:39:33 +01002431 driver_name = make_driver_name(drv);
2432 if (driver_name) {
Kay Sievers0c84ce22007-04-01 23:49:48 -07002433 sysfs_remove_link(mk->drivers_dir, driver_name);
Kay Sievers7f422e22006-12-20 10:39:33 +01002434 kfree(driver_name);
2435 }
2436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437}
2438EXPORT_SYMBOL(module_remove_driver);
Randy Dunlapef665c12007-02-13 15:19:06 -08002439#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
2441#ifdef CONFIG_MODVERSIONS
2442/* Generate the signature for struct module here, too, for modversions. */
2443void struct_module(struct module *mod) { return; }
2444EXPORT_SYMBOL(struct_module);
2445#endif