blob: 4fafd58038a03ad6a36ab9265bcbbd1093bdea83 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Rewritten by Rusty Russell, on the backs of many others...
2 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*/
19#include <linux/config.h>
20#include <linux/module.h>
21#include <linux/moduleloader.h>
22#include <linux/init.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>
38#include <linux/stop_machine.h>
39#include <linux/device.h>
Matt Domschc988d2b2005-06-23 22:05:15 -070040#include <linux/string.h>
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080041#include <linux/sched.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080042#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/uaccess.h>
44#include <asm/semaphore.h>
45#include <asm/cacheflush.h>
46
47#if 0
48#define DEBUGP printk
49#else
50#define DEBUGP(fmt , a...)
51#endif
52
53#ifndef ARCH_SHF_SMALL
54#define ARCH_SHF_SMALL 0
55#endif
56
57/* If this is set, the section belongs in the init part of the module */
58#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
59
60/* Protects module list */
61static DEFINE_SPINLOCK(modlist_lock);
62
63/* List of modules, protected by module_mutex AND modlist_lock */
Ashutosh Naik6389a382006-03-23 03:00:46 -080064static DEFINE_MUTEX(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static LIST_HEAD(modules);
66
Alan Sterne041c682006-03-27 01:16:30 -080067static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69int register_module_notifier(struct notifier_block * nb)
70{
Alan Sterne041c682006-03-27 01:16:30 -080071 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73EXPORT_SYMBOL(register_module_notifier);
74
75int unregister_module_notifier(struct notifier_block * nb)
76{
Alan Sterne041c682006-03-27 01:16:30 -080077 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79EXPORT_SYMBOL(unregister_module_notifier);
80
81/* We require a truly strong try_module_get() */
82static inline int strong_try_module_get(struct module *mod)
83{
84 if (mod && mod->state == MODULE_STATE_COMING)
85 return 0;
86 return try_module_get(mod);
87}
88
89/* A thread that wants to hold a reference to a module only while it
90 * is running can call ths to safely exit.
91 * nfsd and lockd use this.
92 */
93void __module_put_and_exit(struct module *mod, long code)
94{
95 module_put(mod);
96 do_exit(code);
97}
98EXPORT_SYMBOL(__module_put_and_exit);
99
100/* Find a module section: 0 means not found. */
101static unsigned int find_sec(Elf_Ehdr *hdr,
102 Elf_Shdr *sechdrs,
103 const char *secstrings,
104 const char *name)
105{
106 unsigned int i;
107
108 for (i = 1; i < hdr->e_shnum; i++)
109 /* Alloc bit cleared means "ignore it." */
110 if ((sechdrs[i].sh_flags & SHF_ALLOC)
111 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
112 return i;
113 return 0;
114}
115
116/* Provided by the linker */
117extern const struct kernel_symbol __start___ksymtab[];
118extern const struct kernel_symbol __stop___ksymtab[];
119extern const struct kernel_symbol __start___ksymtab_gpl[];
120extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800121extern const struct kernel_symbol __start___ksymtab_gpl_future[];
122extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123extern const unsigned long __start___kcrctab[];
124extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800125extern const unsigned long __start___kcrctab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127#ifndef CONFIG_MODVERSIONS
128#define symversion(base, idx) NULL
129#else
130#define symversion(base, idx) ((base) ? ((base) + (idx)) : NULL)
131#endif
132
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100133/* lookup symbol in given range of kernel_symbols */
134static const struct kernel_symbol *lookup_symbol(const char *name,
135 const struct kernel_symbol *start,
136 const struct kernel_symbol *stop)
137{
138 const struct kernel_symbol *ks = start;
139 for (; ks < stop; ks++)
140 if (strcmp(ks->name, name) == 0)
141 return ks;
142 return NULL;
143}
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/* Find a symbol, return value, crc and module which owns it */
146static unsigned long __find_symbol(const char *name,
147 struct module **owner,
148 const unsigned long **crc,
149 int gplok)
150{
151 struct module *mod;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100152 const struct kernel_symbol *ks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 /* Core kernel first. */
155 *owner = NULL;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100156 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
157 if (ks) {
158 *crc = symversion(__start___kcrctab, (ks - __start___ksymtab));
159 return ks->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161 if (gplok) {
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100162 ks = lookup_symbol(name, __start___ksymtab_gpl,
163 __stop___ksymtab_gpl);
164 if (ks) {
165 *crc = symversion(__start___kcrctab_gpl,
166 (ks - __start___ksymtab_gpl));
167 return ks->value;
168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800170 ks = lookup_symbol(name, __start___ksymtab_gpl_future,
171 __stop___ksymtab_gpl_future);
172 if (ks) {
173 if (!gplok) {
174 printk(KERN_WARNING "Symbol %s is being used "
175 "by a non-GPL module, which will not "
176 "be allowed in the future\n", name);
177 printk(KERN_WARNING "Please see the file "
178 "Documentation/feature-removal-schedule.txt "
179 "in the kernel source tree for more "
180 "details.\n");
181 }
182 *crc = symversion(__start___kcrctab_gpl_future,
183 (ks - __start___ksymtab_gpl_future));
184 return ks->value;
185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 /* Now try modules. */
188 list_for_each_entry(mod, &modules, list) {
189 *owner = mod;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100190 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
191 if (ks) {
192 *crc = symversion(mod->crcs, (ks - mod->syms));
193 return ks->value;
194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 if (gplok) {
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100197 ks = lookup_symbol(name, mod->gpl_syms,
198 mod->gpl_syms + mod->num_gpl_syms);
199 if (ks) {
200 *crc = symversion(mod->gpl_crcs,
201 (ks - mod->gpl_syms));
202 return ks->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
204 }
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800205 ks = lookup_symbol(name, mod->gpl_future_syms,
206 (mod->gpl_future_syms +
207 mod->num_gpl_future_syms));
208 if (ks) {
209 if (!gplok) {
210 printk(KERN_WARNING "Symbol %s is being used "
211 "by a non-GPL module, which will not "
212 "be allowed in the future\n", name);
213 printk(KERN_WARNING "Please see the file "
214 "Documentation/feature-removal-schedule.txt "
215 "in the kernel source tree for more "
216 "details.\n");
217 }
218 *crc = symversion(mod->gpl_future_crcs,
219 (ks - mod->gpl_future_syms));
220 return ks->value;
221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223 DEBUGP("Failed to find symbol %s\n", name);
224 return 0;
225}
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227/* Search for module by name: must hold module_mutex. */
228static struct module *find_module(const char *name)
229{
230 struct module *mod;
231
232 list_for_each_entry(mod, &modules, list) {
233 if (strcmp(mod->name, name) == 0)
234 return mod;
235 }
236 return NULL;
237}
238
239#ifdef CONFIG_SMP
240/* Number of blocks used and allocated. */
241static unsigned int pcpu_num_used, pcpu_num_allocated;
242/* Size of each block. -ve means used. */
243static int *pcpu_size;
244
245static int split_block(unsigned int i, unsigned short size)
246{
247 /* Reallocation required? */
248 if (pcpu_num_used + 1 > pcpu_num_allocated) {
249 int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated*2,
250 GFP_KERNEL);
251 if (!new)
252 return 0;
253
254 memcpy(new, pcpu_size, sizeof(new[0])*pcpu_num_allocated);
255 pcpu_num_allocated *= 2;
256 kfree(pcpu_size);
257 pcpu_size = new;
258 }
259
260 /* Insert a new subblock */
261 memmove(&pcpu_size[i+1], &pcpu_size[i],
262 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
263 pcpu_num_used++;
264
265 pcpu_size[i+1] -= size;
266 pcpu_size[i] = size;
267 return 1;
268}
269
270static inline unsigned int block_size(int val)
271{
272 if (val < 0)
273 return -val;
274 return val;
275}
276
277/* Created by linker magic */
278extern char __per_cpu_start[], __per_cpu_end[];
279
Rusty Russell842bbaa2005-08-01 21:11:47 -0700280static void *percpu_modalloc(unsigned long size, unsigned long align,
281 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 unsigned long extra;
284 unsigned int i;
285 void *ptr;
286
Rusty Russell842bbaa2005-08-01 21:11:47 -0700287 if (align > SMP_CACHE_BYTES) {
288 printk(KERN_WARNING "%s: per-cpu alignment %li > %i\n",
289 name, align, SMP_CACHE_BYTES);
290 align = SMP_CACHE_BYTES;
291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 ptr = __per_cpu_start;
294 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
295 /* Extra for alignment requirement. */
296 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
297 BUG_ON(i == 0 && extra != 0);
298
299 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
300 continue;
301
302 /* Transfer extra to previous block. */
303 if (pcpu_size[i-1] < 0)
304 pcpu_size[i-1] -= extra;
305 else
306 pcpu_size[i-1] += extra;
307 pcpu_size[i] -= extra;
308 ptr += extra;
309
310 /* Split block if warranted */
311 if (pcpu_size[i] - size > sizeof(unsigned long))
312 if (!split_block(i, size))
313 return NULL;
314
315 /* Mark allocated */
316 pcpu_size[i] = -pcpu_size[i];
317 return ptr;
318 }
319
320 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
321 size);
322 return NULL;
323}
324
325static void percpu_modfree(void *freeme)
326{
327 unsigned int i;
328 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
329
330 /* First entry is core kernel percpu data. */
331 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
332 if (ptr == freeme) {
333 pcpu_size[i] = -pcpu_size[i];
334 goto free;
335 }
336 }
337 BUG();
338
339 free:
340 /* Merge with previous? */
341 if (pcpu_size[i-1] >= 0) {
342 pcpu_size[i-1] += pcpu_size[i];
343 pcpu_num_used--;
344 memmove(&pcpu_size[i], &pcpu_size[i+1],
345 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
346 i--;
347 }
348 /* Merge with next? */
349 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
350 pcpu_size[i] += pcpu_size[i+1];
351 pcpu_num_used--;
352 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
353 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
354 }
355}
356
357static unsigned int find_pcpusec(Elf_Ehdr *hdr,
358 Elf_Shdr *sechdrs,
359 const char *secstrings)
360{
361 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
362}
363
364static int percpu_modinit(void)
365{
366 pcpu_num_used = 2;
367 pcpu_num_allocated = 2;
368 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
369 GFP_KERNEL);
370 /* Static in-kernel percpu data (used). */
371 pcpu_size[0] = -ALIGN(__per_cpu_end-__per_cpu_start, SMP_CACHE_BYTES);
372 /* Free room. */
373 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
374 if (pcpu_size[1] < 0) {
375 printk(KERN_ERR "No per-cpu room for modules.\n");
376 pcpu_num_used = 1;
377 }
378
379 return 0;
380}
381__initcall(percpu_modinit);
382#else /* ... !CONFIG_SMP */
Rusty Russell842bbaa2005-08-01 21:11:47 -0700383static inline void *percpu_modalloc(unsigned long size, unsigned long align,
384 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 return NULL;
387}
388static inline void percpu_modfree(void *pcpuptr)
389{
390 BUG();
391}
392static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
393 Elf_Shdr *sechdrs,
394 const char *secstrings)
395{
396 return 0;
397}
398static inline void percpu_modcopy(void *pcpudst, const void *src,
399 unsigned long size)
400{
401 /* pcpusec should be 0, and size of that section should be 0. */
402 BUG_ON(size != 0);
403}
404#endif /* CONFIG_SMP */
405
Matt Domschc988d2b2005-06-23 22:05:15 -0700406#define MODINFO_ATTR(field) \
407static void setup_modinfo_##field(struct module *mod, const char *s) \
408{ \
409 mod->field = kstrdup(s, GFP_KERNEL); \
410} \
411static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
412 struct module *mod, char *buffer) \
413{ \
414 return sprintf(buffer, "%s\n", mod->field); \
415} \
416static int modinfo_##field##_exists(struct module *mod) \
417{ \
418 return mod->field != NULL; \
419} \
420static void free_modinfo_##field(struct module *mod) \
421{ \
422 kfree(mod->field); \
423 mod->field = NULL; \
424} \
425static struct module_attribute modinfo_##field = { \
426 .attr = { .name = __stringify(field), .mode = 0444, \
427 .owner = THIS_MODULE }, \
428 .show = show_modinfo_##field, \
429 .setup = setup_modinfo_##field, \
430 .test = modinfo_##field##_exists, \
431 .free = free_modinfo_##field, \
432};
433
434MODINFO_ATTR(version);
435MODINFO_ATTR(srcversion);
436
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800437#ifdef CONFIG_MODULE_UNLOAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438/* Init the unload section of the module. */
439static void module_unload_init(struct module *mod)
440{
441 unsigned int i;
442
443 INIT_LIST_HEAD(&mod->modules_which_use_me);
444 for (i = 0; i < NR_CPUS; i++)
445 local_set(&mod->ref[i].count, 0);
446 /* Hold reference count during initialization. */
Ingo Molnar39c715b2005-06-21 17:14:34 -0700447 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 /* Backwards compatibility macros put refcount during init. */
449 mod->waiter = current;
450}
451
452/* modules using other modules */
453struct module_use
454{
455 struct list_head list;
456 struct module *module_which_uses;
457};
458
459/* Does a already use b? */
460static int already_uses(struct module *a, struct module *b)
461{
462 struct module_use *use;
463
464 list_for_each_entry(use, &b->modules_which_use_me, list) {
465 if (use->module_which_uses == a) {
466 DEBUGP("%s uses %s!\n", a->name, b->name);
467 return 1;
468 }
469 }
470 DEBUGP("%s does not use %s!\n", a->name, b->name);
471 return 0;
472}
473
474/* Module a uses b */
475static int use_module(struct module *a, struct module *b)
476{
477 struct module_use *use;
478 if (b == NULL || already_uses(a, b)) return 1;
479
480 if (!strong_try_module_get(b))
481 return 0;
482
483 DEBUGP("Allocating new usage for %s.\n", a->name);
484 use = kmalloc(sizeof(*use), GFP_ATOMIC);
485 if (!use) {
486 printk("%s: out of memory loading\n", a->name);
487 module_put(b);
488 return 0;
489 }
490
491 use->module_which_uses = a;
492 list_add(&use->list, &b->modules_which_use_me);
493 return 1;
494}
495
496/* Clear the unload stuff of the module. */
497static void module_unload_free(struct module *mod)
498{
499 struct module *i;
500
501 list_for_each_entry(i, &modules, list) {
502 struct module_use *use;
503
504 list_for_each_entry(use, &i->modules_which_use_me, list) {
505 if (use->module_which_uses == mod) {
506 DEBUGP("%s unusing %s\n", mod->name, i->name);
507 module_put(i);
508 list_del(&use->list);
509 kfree(use);
510 /* There can be at most one match. */
511 break;
512 }
513 }
514 }
515}
516
517#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800518static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 int ret = (flags & O_TRUNC);
521 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800522 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return ret;
524}
525#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800526static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 return 0;
529}
530#endif /* CONFIG_MODULE_FORCE_UNLOAD */
531
532struct stopref
533{
534 struct module *mod;
535 int flags;
536 int *forced;
537};
538
539/* Whole machine is stopped with interrupts off when this runs. */
540static int __try_stop_module(void *_sref)
541{
542 struct stopref *sref = _sref;
543
544 /* If it's not unused, quit unless we are told to block. */
545 if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800546 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return -EWOULDBLOCK;
548 }
549
550 /* Mark it as dying. */
551 sref->mod->state = MODULE_STATE_GOING;
552 return 0;
553}
554
555static int try_stop_module(struct module *mod, int flags, int *forced)
556{
557 struct stopref sref = { mod, flags, forced };
558
559 return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
560}
561
562unsigned int module_refcount(struct module *mod)
563{
564 unsigned int i, total = 0;
565
566 for (i = 0; i < NR_CPUS; i++)
567 total += local_read(&mod->ref[i].count);
568 return total;
569}
570EXPORT_SYMBOL(module_refcount);
571
572/* This exists whether we can unload or not */
573static void free_module(struct module *mod);
574
575static void wait_for_zero_refcount(struct module *mod)
576{
577 /* Since we might sleep for some time, drop the semaphore first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800578 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 for (;;) {
580 DEBUGP("Looking at refcount...\n");
581 set_current_state(TASK_UNINTERRUPTIBLE);
582 if (module_refcount(mod) == 0)
583 break;
584 schedule();
585 }
586 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800587 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
590asmlinkage long
591sys_delete_module(const char __user *name_user, unsigned int flags)
592{
593 struct module *mod;
594 char name[MODULE_NAME_LEN];
595 int ret, forced = 0;
596
597 if (!capable(CAP_SYS_MODULE))
598 return -EPERM;
599
600 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
601 return -EFAULT;
602 name[MODULE_NAME_LEN-1] = '\0';
603
Ashutosh Naik6389a382006-03-23 03:00:46 -0800604 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return -EINTR;
606
607 mod = find_module(name);
608 if (!mod) {
609 ret = -ENOENT;
610 goto out;
611 }
612
613 if (!list_empty(&mod->modules_which_use_me)) {
614 /* Other modules depend on us: get rid of them first. */
615 ret = -EWOULDBLOCK;
616 goto out;
617 }
618
619 /* Doing init or already dying? */
620 if (mod->state != MODULE_STATE_LIVE) {
621 /* FIXME: if (force), slam module count and wake up
622 waiter --RR */
623 DEBUGP("%s already dying\n", mod->name);
624 ret = -EBUSY;
625 goto out;
626 }
627
628 /* If it has an init func, it must have an exit func to unload */
629 if ((mod->init != NULL && mod->exit == NULL)
630 || mod->unsafe) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800631 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (!forced) {
633 /* This module can't be removed */
634 ret = -EBUSY;
635 goto out;
636 }
637 }
638
639 /* Set this up before setting mod->state */
640 mod->waiter = current;
641
642 /* Stop the machine so refcounts can't move and disable module. */
643 ret = try_stop_module(mod, flags, &forced);
644 if (ret != 0)
645 goto out;
646
647 /* Never wait if forced. */
648 if (!forced && module_refcount(mod) != 0)
649 wait_for_zero_refcount(mod);
650
651 /* Final destruction now noone is using it. */
652 if (mod->exit != NULL) {
Ashutosh Naik6389a382006-03-23 03:00:46 -0800653 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 mod->exit();
Ashutosh Naik6389a382006-03-23 03:00:46 -0800655 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657 free_module(mod);
658
659 out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800660 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return ret;
662}
663
664static void print_unload_info(struct seq_file *m, struct module *mod)
665{
666 struct module_use *use;
667 int printed_something = 0;
668
669 seq_printf(m, " %u ", module_refcount(mod));
670
671 /* Always include a trailing , so userspace can differentiate
672 between this and the old multi-field proc format. */
673 list_for_each_entry(use, &mod->modules_which_use_me, list) {
674 printed_something = 1;
675 seq_printf(m, "%s,", use->module_which_uses->name);
676 }
677
678 if (mod->unsafe) {
679 printed_something = 1;
680 seq_printf(m, "[unsafe],");
681 }
682
683 if (mod->init != NULL && mod->exit == NULL) {
684 printed_something = 1;
685 seq_printf(m, "[permanent],");
686 }
687
688 if (!printed_something)
689 seq_printf(m, "-");
690}
691
692void __symbol_put(const char *symbol)
693{
694 struct module *owner;
695 unsigned long flags;
696 const unsigned long *crc;
697
698 spin_lock_irqsave(&modlist_lock, flags);
699 if (!__find_symbol(symbol, &owner, &crc, 1))
700 BUG();
701 module_put(owner);
702 spin_unlock_irqrestore(&modlist_lock, flags);
703}
704EXPORT_SYMBOL(__symbol_put);
705
706void symbol_put_addr(void *addr)
707{
708 unsigned long flags;
709
710 spin_lock_irqsave(&modlist_lock, flags);
711 if (!kernel_text_address((unsigned long)addr))
712 BUG();
713
714 module_put(module_text_address((unsigned long)addr));
715 spin_unlock_irqrestore(&modlist_lock, flags);
716}
717EXPORT_SYMBOL_GPL(symbol_put_addr);
718
719static ssize_t show_refcnt(struct module_attribute *mattr,
720 struct module *mod, char *buffer)
721{
722 /* sysfs holds a reference */
723 return sprintf(buffer, "%u\n", module_refcount(mod)-1);
724}
725
726static struct module_attribute refcnt = {
727 .attr = { .name = "refcnt", .mode = 0444, .owner = THIS_MODULE },
728 .show = show_refcnt,
729};
730
731#else /* !CONFIG_MODULE_UNLOAD */
732static void print_unload_info(struct seq_file *m, struct module *mod)
733{
734 /* We don't know the usage count, or what modules are using. */
735 seq_printf(m, " - -");
736}
737
738static inline void module_unload_free(struct module *mod)
739{
740}
741
742static inline int use_module(struct module *a, struct module *b)
743{
744 return strong_try_module_get(b);
745}
746
747static inline void module_unload_init(struct module *mod)
748{
749}
750#endif /* CONFIG_MODULE_UNLOAD */
751
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800752static struct module_attribute *modinfo_attrs[] = {
753 &modinfo_version,
754 &modinfo_srcversion,
755#ifdef CONFIG_MODULE_UNLOAD
756 &refcnt,
757#endif
758 NULL,
759};
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761static const char vermagic[] = VERMAGIC_STRING;
762
763#ifdef CONFIG_MODVERSIONS
764static int check_version(Elf_Shdr *sechdrs,
765 unsigned int versindex,
766 const char *symname,
767 struct module *mod,
768 const unsigned long *crc)
769{
770 unsigned int i, num_versions;
771 struct modversion_info *versions;
772
773 /* Exporting module didn't supply crcs? OK, we're already tainted. */
774 if (!crc)
775 return 1;
776
777 versions = (void *) sechdrs[versindex].sh_addr;
778 num_versions = sechdrs[versindex].sh_size
779 / sizeof(struct modversion_info);
780
781 for (i = 0; i < num_versions; i++) {
782 if (strcmp(versions[i].name, symname) != 0)
783 continue;
784
785 if (versions[i].crc == *crc)
786 return 1;
787 printk("%s: disagrees about version of symbol %s\n",
788 mod->name, symname);
789 DEBUGP("Found checksum %lX vs module %lX\n",
790 *crc, versions[i].crc);
791 return 0;
792 }
793 /* Not in module's version table. OK, but that taints the kernel. */
794 if (!(tainted & TAINT_FORCED_MODULE)) {
795 printk("%s: no version for \"%s\" found: kernel tainted.\n",
796 mod->name, symname);
Randy Dunlap9f158332005-09-13 01:25:16 -0700797 add_taint(TAINT_FORCED_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799 return 1;
800}
801
802static inline int check_modstruct_version(Elf_Shdr *sechdrs,
803 unsigned int versindex,
804 struct module *mod)
805{
806 const unsigned long *crc;
807 struct module *owner;
808
809 if (!__find_symbol("struct_module", &owner, &crc, 1))
810 BUG();
811 return check_version(sechdrs, versindex, "struct_module", mod,
812 crc);
813}
814
815/* First part is kernel version, which we ignore. */
816static inline int same_magic(const char *amagic, const char *bmagic)
817{
818 amagic += strcspn(amagic, " ");
819 bmagic += strcspn(bmagic, " ");
820 return strcmp(amagic, bmagic) == 0;
821}
822#else
823static inline int check_version(Elf_Shdr *sechdrs,
824 unsigned int versindex,
825 const char *symname,
826 struct module *mod,
827 const unsigned long *crc)
828{
829 return 1;
830}
831
832static inline int check_modstruct_version(Elf_Shdr *sechdrs,
833 unsigned int versindex,
834 struct module *mod)
835{
836 return 1;
837}
838
839static inline int same_magic(const char *amagic, const char *bmagic)
840{
841 return strcmp(amagic, bmagic) == 0;
842}
843#endif /* CONFIG_MODVERSIONS */
844
845/* Resolve a symbol for this module. I.e. if we find one, record usage.
846 Must be holding module_mutex. */
847static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
848 unsigned int versindex,
849 const char *name,
850 struct module *mod)
851{
852 struct module *owner;
853 unsigned long ret;
854 const unsigned long *crc;
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 ret = __find_symbol(name, &owner, &crc, mod->license_gplok);
857 if (ret) {
858 /* use_module can fail due to OOM, or module unloading */
859 if (!check_version(sechdrs, versindex, name, mod, crc) ||
860 !use_module(mod, owner))
861 ret = 0;
862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return ret;
864}
865
866
867/*
868 * /sys/module/foo/sections stuff
869 * J. Corbet <corbet@lwn.net>
870 */
871#ifdef CONFIG_KALLSYMS
872static ssize_t module_sect_show(struct module_attribute *mattr,
873 struct module *mod, char *buf)
874{
875 struct module_sect_attr *sattr =
876 container_of(mattr, struct module_sect_attr, mattr);
877 return sprintf(buf, "0x%lx\n", sattr->address);
878}
879
880static void add_sect_attrs(struct module *mod, unsigned int nsect,
881 char *secstrings, Elf_Shdr *sechdrs)
882{
883 unsigned int nloaded = 0, i, size[2];
884 struct module_sect_attrs *sect_attrs;
885 struct module_sect_attr *sattr;
886 struct attribute **gattr;
887
888 /* Count loaded sections and allocate structures */
889 for (i = 0; i < nsect; i++)
890 if (sechdrs[i].sh_flags & SHF_ALLOC)
891 nloaded++;
892 size[0] = ALIGN(sizeof(*sect_attrs)
893 + nloaded * sizeof(sect_attrs->attrs[0]),
894 sizeof(sect_attrs->grp.attrs[0]));
895 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
896 if (! (sect_attrs = kmalloc(size[0] + size[1], GFP_KERNEL)))
897 return;
898
899 /* Setup section attributes. */
900 sect_attrs->grp.name = "sections";
901 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
902
903 sattr = &sect_attrs->attrs[0];
904 gattr = &sect_attrs->grp.attrs[0];
905 for (i = 0; i < nsect; i++) {
906 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
907 continue;
908 sattr->address = sechdrs[i].sh_addr;
909 strlcpy(sattr->name, secstrings + sechdrs[i].sh_name,
910 MODULE_SECT_NAME_LEN);
911 sattr->mattr.show = module_sect_show;
912 sattr->mattr.store = NULL;
913 sattr->mattr.attr.name = sattr->name;
914 sattr->mattr.attr.owner = mod;
915 sattr->mattr.attr.mode = S_IRUGO;
916 *(gattr++) = &(sattr++)->mattr.attr;
917 }
918 *gattr = NULL;
919
920 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
921 goto out;
922
923 mod->sect_attrs = sect_attrs;
924 return;
925 out:
926 kfree(sect_attrs);
927}
928
929static void remove_sect_attrs(struct module *mod)
930{
931 if (mod->sect_attrs) {
932 sysfs_remove_group(&mod->mkobj.kobj,
933 &mod->sect_attrs->grp);
934 /* We are positive that no one is using any sect attrs
935 * at this point. Deallocate immediately. */
936 kfree(mod->sect_attrs);
937 mod->sect_attrs = NULL;
938 }
939}
940
941
942#else
943static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
944 char *sectstrings, Elf_Shdr *sechdrs)
945{
946}
947
948static inline void remove_sect_attrs(struct module *mod)
949{
950}
951#endif /* CONFIG_KALLSYMS */
952
Matt Domschc988d2b2005-06-23 22:05:15 -0700953static int module_add_modinfo_attrs(struct module *mod)
954{
955 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800956 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -0700957 int error = 0;
958 int i;
959
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800960 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
961 (ARRAY_SIZE(modinfo_attrs) + 1)),
962 GFP_KERNEL);
963 if (!mod->modinfo_attrs)
964 return -ENOMEM;
965
966 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -0700967 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
968 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800969 (attr->test && attr->test(mod))) {
970 memcpy(temp_attr, attr, sizeof(*temp_attr));
971 temp_attr->attr.owner = mod;
972 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
973 ++temp_attr;
974 }
Matt Domschc988d2b2005-06-23 22:05:15 -0700975 }
976 return error;
977}
978
979static void module_remove_modinfo_attrs(struct module *mod)
980{
981 struct module_attribute *attr;
982 int i;
983
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800984 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
985 /* pick a field to test for end of list */
986 if (!attr->attr.name)
987 break;
Matt Domschc988d2b2005-06-23 22:05:15 -0700988 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800989 if (attr->free)
990 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -0700991 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800992 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -0700993}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995static int mod_sysfs_setup(struct module *mod,
996 struct kernel_param *kparam,
997 unsigned int num_params)
998{
999 int err;
1000
1001 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1002 err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name);
1003 if (err)
1004 goto out;
1005 kobj_set_kset_s(&mod->mkobj, module_subsys);
1006 mod->mkobj.mod = mod;
1007 err = kobject_register(&mod->mkobj.kobj);
1008 if (err)
1009 goto out;
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 err = module_param_sysfs_setup(mod, kparam, num_params);
1012 if (err)
1013 goto out_unreg;
1014
Matt Domschc988d2b2005-06-23 22:05:15 -07001015 err = module_add_modinfo_attrs(mod);
1016 if (err)
1017 goto out_unreg;
Matt Domschc988d2b2005-06-23 22:05:15 -07001018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return 0;
1020
1021out_unreg:
1022 kobject_unregister(&mod->mkobj.kobj);
1023out:
1024 return err;
1025}
1026
1027static void mod_kobject_remove(struct module *mod)
1028{
Matt Domschc988d2b2005-06-23 22:05:15 -07001029 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 module_param_sysfs_remove(mod);
1031
1032 kobject_unregister(&mod->mkobj.kobj);
1033}
1034
1035/*
1036 * unlink the module with the whole machine is stopped with interrupts off
1037 * - this defends against kallsyms not taking locks
1038 */
1039static int __unlink_module(void *_mod)
1040{
1041 struct module *mod = _mod;
1042 list_del(&mod->list);
1043 return 0;
1044}
1045
1046/* Free a module, remove from lists, etc (must hold module mutex). */
1047static void free_module(struct module *mod)
1048{
1049 /* Delete from various lists */
1050 stop_machine_run(__unlink_module, mod, NR_CPUS);
1051 remove_sect_attrs(mod);
1052 mod_kobject_remove(mod);
1053
1054 /* Arch-specific cleanup. */
1055 module_arch_cleanup(mod);
1056
1057 /* Module unload stuff */
1058 module_unload_free(mod);
1059
1060 /* This may be NULL, but that's OK */
1061 module_free(mod, mod->module_init);
1062 kfree(mod->args);
1063 if (mod->percpu)
1064 percpu_modfree(mod->percpu);
1065
1066 /* Finally, free the core (containing the module structure) */
1067 module_free(mod, mod->module_core);
1068}
1069
1070void *__symbol_get(const char *symbol)
1071{
1072 struct module *owner;
1073 unsigned long value, flags;
1074 const unsigned long *crc;
1075
1076 spin_lock_irqsave(&modlist_lock, flags);
1077 value = __find_symbol(symbol, &owner, &crc, 1);
1078 if (value && !strong_try_module_get(owner))
1079 value = 0;
1080 spin_unlock_irqrestore(&modlist_lock, flags);
1081
1082 return (void *)value;
1083}
1084EXPORT_SYMBOL_GPL(__symbol_get);
1085
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001086/*
1087 * Ensure that an exported symbol [global namespace] does not already exist
1088 * in the Kernel or in some other modules exported symbol table.
1089 */
1090static int verify_export_symbols(struct module *mod)
1091{
1092 const char *name = NULL;
1093 unsigned long i, ret = 0;
1094 struct module *owner;
1095 const unsigned long *crc;
1096
1097 for (i = 0; i < mod->num_syms; i++)
1098 if (__find_symbol(mod->syms[i].name, &owner, &crc, 1)) {
1099 name = mod->syms[i].name;
1100 ret = -ENOEXEC;
1101 goto dup;
1102 }
1103
1104 for (i = 0; i < mod->num_gpl_syms; i++)
1105 if (__find_symbol(mod->gpl_syms[i].name, &owner, &crc, 1)) {
1106 name = mod->gpl_syms[i].name;
1107 ret = -ENOEXEC;
1108 goto dup;
1109 }
1110
1111dup:
1112 if (ret)
1113 printk(KERN_ERR "%s: exports duplicate symbol %s (owned by %s)\n",
1114 mod->name, name, module_name(owner));
1115
1116 return ret;
1117}
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119/* Change all symbols so that sh_value encodes the pointer directly. */
1120static int simplify_symbols(Elf_Shdr *sechdrs,
1121 unsigned int symindex,
1122 const char *strtab,
1123 unsigned int versindex,
1124 unsigned int pcpuindex,
1125 struct module *mod)
1126{
1127 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1128 unsigned long secbase;
1129 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1130 int ret = 0;
1131
1132 for (i = 1; i < n; i++) {
1133 switch (sym[i].st_shndx) {
1134 case SHN_COMMON:
1135 /* We compiled with -fno-common. These are not
1136 supposed to happen. */
1137 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1138 printk("%s: please compile with -fno-common\n",
1139 mod->name);
1140 ret = -ENOEXEC;
1141 break;
1142
1143 case SHN_ABS:
1144 /* Don't need to do anything */
1145 DEBUGP("Absolute symbol: 0x%08lx\n",
1146 (long)sym[i].st_value);
1147 break;
1148
1149 case SHN_UNDEF:
1150 sym[i].st_value
1151 = resolve_symbol(sechdrs, versindex,
1152 strtab + sym[i].st_name, mod);
1153
1154 /* Ok if resolved. */
1155 if (sym[i].st_value != 0)
1156 break;
1157 /* Ok if weak. */
1158 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1159 break;
1160
1161 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1162 mod->name, strtab + sym[i].st_name);
1163 ret = -ENOENT;
1164 break;
1165
1166 default:
1167 /* Divert to percpu allocation if a percpu var. */
1168 if (sym[i].st_shndx == pcpuindex)
1169 secbase = (unsigned long)mod->percpu;
1170 else
1171 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1172 sym[i].st_value += secbase;
1173 break;
1174 }
1175 }
1176
1177 return ret;
1178}
1179
1180/* Update size with this section: return offset. */
1181static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1182{
1183 long ret;
1184
1185 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1186 *size = ret + sechdr->sh_size;
1187 return ret;
1188}
1189
1190/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1191 might -- code, read-only data, read-write data, small data. Tally
1192 sizes, and place the offsets into sh_entsize fields: high bit means it
1193 belongs in init. */
1194static void layout_sections(struct module *mod,
1195 const Elf_Ehdr *hdr,
1196 Elf_Shdr *sechdrs,
1197 const char *secstrings)
1198{
1199 static unsigned long const masks[][2] = {
1200 /* NOTE: all executable code must be the first section
1201 * in this array; otherwise modify the text_size
1202 * finder in the two loops below */
1203 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1204 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1205 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1206 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1207 };
1208 unsigned int m, i;
1209
1210 for (i = 0; i < hdr->e_shnum; i++)
1211 sechdrs[i].sh_entsize = ~0UL;
1212
1213 DEBUGP("Core section allocation order:\n");
1214 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1215 for (i = 0; i < hdr->e_shnum; ++i) {
1216 Elf_Shdr *s = &sechdrs[i];
1217
1218 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1219 || (s->sh_flags & masks[m][1])
1220 || s->sh_entsize != ~0UL
1221 || strncmp(secstrings + s->sh_name,
1222 ".init", 5) == 0)
1223 continue;
1224 s->sh_entsize = get_offset(&mod->core_size, s);
1225 DEBUGP("\t%s\n", secstrings + s->sh_name);
1226 }
1227 if (m == 0)
1228 mod->core_text_size = mod->core_size;
1229 }
1230
1231 DEBUGP("Init section allocation order:\n");
1232 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1233 for (i = 0; i < hdr->e_shnum; ++i) {
1234 Elf_Shdr *s = &sechdrs[i];
1235
1236 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1237 || (s->sh_flags & masks[m][1])
1238 || s->sh_entsize != ~0UL
1239 || strncmp(secstrings + s->sh_name,
1240 ".init", 5) != 0)
1241 continue;
1242 s->sh_entsize = (get_offset(&mod->init_size, s)
1243 | INIT_OFFSET_MASK);
1244 DEBUGP("\t%s\n", secstrings + s->sh_name);
1245 }
1246 if (m == 0)
1247 mod->init_text_size = mod->init_size;
1248 }
1249}
1250
1251static inline int license_is_gpl_compatible(const char *license)
1252{
1253 return (strcmp(license, "GPL") == 0
1254 || strcmp(license, "GPL v2") == 0
1255 || strcmp(license, "GPL and additional rights") == 0
1256 || strcmp(license, "Dual BSD/GPL") == 0
1257 || strcmp(license, "Dual MPL/GPL") == 0);
1258}
1259
1260static void set_license(struct module *mod, const char *license)
1261{
1262 if (!license)
1263 license = "unspecified";
1264
1265 mod->license_gplok = license_is_gpl_compatible(license);
1266 if (!mod->license_gplok && !(tainted & TAINT_PROPRIETARY_MODULE)) {
1267 printk(KERN_WARNING "%s: module license '%s' taints kernel.\n",
1268 mod->name, license);
Randy Dunlap9f158332005-09-13 01:25:16 -07001269 add_taint(TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
1271}
1272
1273/* Parse tag=value strings from .modinfo section */
1274static char *next_string(char *string, unsigned long *secsize)
1275{
1276 /* Skip non-zero chars */
1277 while (string[0]) {
1278 string++;
1279 if ((*secsize)-- <= 1)
1280 return NULL;
1281 }
1282
1283 /* Skip any zero padding. */
1284 while (!string[0]) {
1285 string++;
1286 if ((*secsize)-- <= 1)
1287 return NULL;
1288 }
1289 return string;
1290}
1291
1292static char *get_modinfo(Elf_Shdr *sechdrs,
1293 unsigned int info,
1294 const char *tag)
1295{
1296 char *p;
1297 unsigned int taglen = strlen(tag);
1298 unsigned long size = sechdrs[info].sh_size;
1299
1300 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1301 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1302 return p + taglen + 1;
1303 }
1304 return NULL;
1305}
1306
Matt Domschc988d2b2005-06-23 22:05:15 -07001307static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1308 unsigned int infoindex)
1309{
1310 struct module_attribute *attr;
1311 int i;
1312
1313 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1314 if (attr->setup)
1315 attr->setup(mod,
1316 get_modinfo(sechdrs,
1317 infoindex,
1318 attr->attr.name));
1319 }
1320}
Matt Domschc988d2b2005-06-23 22:05:15 -07001321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322#ifdef CONFIG_KALLSYMS
1323int is_exported(const char *name, const struct module *mod)
1324{
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001325 if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
1326 return 1;
1327 else
1328 if (lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return 1;
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001330 else
1331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
1334/* As per nm */
1335static char elf_type(const Elf_Sym *sym,
1336 Elf_Shdr *sechdrs,
1337 const char *secstrings,
1338 struct module *mod)
1339{
1340 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1341 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1342 return 'v';
1343 else
1344 return 'w';
1345 }
1346 if (sym->st_shndx == SHN_UNDEF)
1347 return 'U';
1348 if (sym->st_shndx == SHN_ABS)
1349 return 'a';
1350 if (sym->st_shndx >= SHN_LORESERVE)
1351 return '?';
1352 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1353 return 't';
1354 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1355 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1356 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1357 return 'r';
1358 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1359 return 'g';
1360 else
1361 return 'd';
1362 }
1363 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1364 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1365 return 's';
1366 else
1367 return 'b';
1368 }
1369 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1370 ".debug", strlen(".debug")) == 0)
1371 return 'n';
1372 return '?';
1373}
1374
1375static void add_kallsyms(struct module *mod,
1376 Elf_Shdr *sechdrs,
1377 unsigned int symindex,
1378 unsigned int strindex,
1379 const char *secstrings)
1380{
1381 unsigned int i;
1382
1383 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1384 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1385 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1386
1387 /* Set types up while we still have access to sections. */
1388 for (i = 0; i < mod->num_symtab; i++)
1389 mod->symtab[i].st_info
1390 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1391}
1392#else
1393static inline void add_kallsyms(struct module *mod,
1394 Elf_Shdr *sechdrs,
1395 unsigned int symindex,
1396 unsigned int strindex,
1397 const char *secstrings)
1398{
1399}
1400#endif /* CONFIG_KALLSYMS */
1401
1402/* Allocate and load the module: note that size of section 0 is always
1403 zero, and we rely on this for optional sections. */
1404static struct module *load_module(void __user *umod,
1405 unsigned long len,
1406 const char __user *uargs)
1407{
1408 Elf_Ehdr *hdr;
1409 Elf_Shdr *sechdrs;
1410 char *secstrings, *args, *modmagic, *strtab = NULL;
1411 unsigned int i, symindex = 0, strindex = 0, setupindex, exindex,
1412 exportindex, modindex, obsparmindex, infoindex, gplindex,
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001413 crcindex, gplcrcindex, versindex, pcpuindex, gplfutureindex,
1414 gplfuturecrcindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 struct module *mod;
1416 long err = 0;
1417 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1418 struct exception_table_entry *extable;
Thomas Koeller378bac82005-09-06 15:17:11 -07001419 mm_segment_t old_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1422 umod, len, uargs);
1423 if (len < sizeof(*hdr))
1424 return ERR_PTR(-ENOEXEC);
1425
1426 /* Suck in entire file: we'll want most of it. */
1427 /* vmalloc barfs on "unusual" numbers. Check here */
1428 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1429 return ERR_PTR(-ENOMEM);
1430 if (copy_from_user(hdr, umod, len) != 0) {
1431 err = -EFAULT;
1432 goto free_hdr;
1433 }
1434
1435 /* Sanity checks against insmoding binaries or wrong arch,
1436 weird elf version */
1437 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1438 || hdr->e_type != ET_REL
1439 || !elf_check_arch(hdr)
1440 || hdr->e_shentsize != sizeof(*sechdrs)) {
1441 err = -ENOEXEC;
1442 goto free_hdr;
1443 }
1444
1445 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1446 goto truncated;
1447
1448 /* Convenience variables */
1449 sechdrs = (void *)hdr + hdr->e_shoff;
1450 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1451 sechdrs[0].sh_addr = 0;
1452
1453 for (i = 1; i < hdr->e_shnum; i++) {
1454 if (sechdrs[i].sh_type != SHT_NOBITS
1455 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1456 goto truncated;
1457
1458 /* Mark all sections sh_addr with their address in the
1459 temporary image. */
1460 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1461
1462 /* Internal symbols and strings. */
1463 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1464 symindex = i;
1465 strindex = sechdrs[i].sh_link;
1466 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1467 }
1468#ifndef CONFIG_MODULE_UNLOAD
1469 /* Don't load .exit sections */
1470 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1471 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1472#endif
1473 }
1474
1475 modindex = find_sec(hdr, sechdrs, secstrings,
1476 ".gnu.linkonce.this_module");
1477 if (!modindex) {
1478 printk(KERN_WARNING "No module found in object\n");
1479 err = -ENOEXEC;
1480 goto free_hdr;
1481 }
1482 mod = (void *)sechdrs[modindex].sh_addr;
1483
1484 if (symindex == 0) {
1485 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1486 mod->name);
1487 err = -ENOEXEC;
1488 goto free_hdr;
1489 }
1490
1491 /* Optional sections */
1492 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1493 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001494 gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1496 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001497 gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1499 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1500 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1501 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1502 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1503 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1504
1505 /* Don't keep modinfo section */
1506 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1507#ifdef CONFIG_KALLSYMS
1508 /* Keep symbol and string tables for decoding later. */
1509 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1510 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1511#endif
1512
1513 /* Check module struct version now, before we try to use module. */
1514 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1515 err = -ENOEXEC;
1516 goto free_hdr;
1517 }
1518
1519 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1520 /* This is allowed: modprobe --force will invalidate it. */
1521 if (!modmagic) {
Randy Dunlap9f158332005-09-13 01:25:16 -07001522 add_taint(TAINT_FORCED_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1524 mod->name);
1525 } else if (!same_magic(modmagic, vermagic)) {
1526 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1527 mod->name, modmagic, vermagic);
1528 err = -ENOEXEC;
1529 goto free_hdr;
1530 }
1531
1532 /* Now copy in args */
Davi Arnaut24277dd2006-03-24 03:18:43 -08001533 args = strndup_user(uargs, ~0UL >> 1);
1534 if (IS_ERR(args)) {
1535 err = PTR_ERR(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 goto free_hdr;
1537 }
Andrew Morton8e08b752006-02-07 12:58:45 -08001538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 if (find_module(mod->name)) {
1540 err = -EEXIST;
1541 goto free_mod;
1542 }
1543
1544 mod->state = MODULE_STATE_COMING;
1545
1546 /* Allow arches to frob section contents and sizes. */
1547 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1548 if (err < 0)
1549 goto free_mod;
1550
1551 if (pcpuindex) {
1552 /* We have a special allocation for this section. */
1553 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
Rusty Russell842bbaa2005-08-01 21:11:47 -07001554 sechdrs[pcpuindex].sh_addralign,
1555 mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 if (!percpu) {
1557 err = -ENOMEM;
1558 goto free_mod;
1559 }
1560 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1561 mod->percpu = percpu;
1562 }
1563
1564 /* Determine total sizes, and put offsets in sh_entsize. For now
1565 this is done generically; there doesn't appear to be any
1566 special cases for the architectures. */
1567 layout_sections(mod, hdr, sechdrs, secstrings);
1568
1569 /* Do the allocs. */
1570 ptr = module_alloc(mod->core_size);
1571 if (!ptr) {
1572 err = -ENOMEM;
1573 goto free_percpu;
1574 }
1575 memset(ptr, 0, mod->core_size);
1576 mod->module_core = ptr;
1577
1578 ptr = module_alloc(mod->init_size);
1579 if (!ptr && mod->init_size) {
1580 err = -ENOMEM;
1581 goto free_core;
1582 }
1583 memset(ptr, 0, mod->init_size);
1584 mod->module_init = ptr;
1585
1586 /* Transfer each section which specifies SHF_ALLOC */
1587 DEBUGP("final section addresses:\n");
1588 for (i = 0; i < hdr->e_shnum; i++) {
1589 void *dest;
1590
1591 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1592 continue;
1593
1594 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1595 dest = mod->module_init
1596 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1597 else
1598 dest = mod->module_core + sechdrs[i].sh_entsize;
1599
1600 if (sechdrs[i].sh_type != SHT_NOBITS)
1601 memcpy(dest, (void *)sechdrs[i].sh_addr,
1602 sechdrs[i].sh_size);
1603 /* Update sh_addr to point to copy in image. */
1604 sechdrs[i].sh_addr = (unsigned long)dest;
1605 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1606 }
1607 /* Module has been moved. */
1608 mod = (void *)sechdrs[modindex].sh_addr;
1609
1610 /* Now we've moved module, initialize linked lists, etc. */
1611 module_unload_init(mod);
1612
1613 /* Set up license info based on the info section */
1614 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1615
Dave Jones9841d61d2006-01-08 01:03:41 -08001616 if (strcmp(mod->name, "ndiswrapper") == 0)
1617 add_taint(TAINT_PROPRIETARY_MODULE);
1618 if (strcmp(mod->name, "driverloader") == 0)
1619 add_taint(TAINT_PROPRIETARY_MODULE);
1620
Matt Domschc988d2b2005-06-23 22:05:15 -07001621 /* Set up MODINFO_ATTR fields */
1622 setup_modinfo(mod, sechdrs, infoindex);
Matt Domschc988d2b2005-06-23 22:05:15 -07001623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 /* Fix up syms, so that st_value is a pointer to location. */
1625 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
1626 mod);
1627 if (err < 0)
1628 goto cleanup;
1629
1630 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1631 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1632 mod->syms = (void *)sechdrs[exportindex].sh_addr;
1633 if (crcindex)
1634 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1635 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1636 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1637 if (gplcrcindex)
1638 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001639 mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size /
1640 sizeof(*mod->gpl_future_syms);
1641 mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr;
1642 if (gplfuturecrcindex)
1643 mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645#ifdef CONFIG_MODVERSIONS
1646 if ((mod->num_syms && !crcindex) ||
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001647 (mod->num_gpl_syms && !gplcrcindex) ||
1648 (mod->num_gpl_future_syms && !gplfuturecrcindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 printk(KERN_WARNING "%s: No versions for exported symbols."
1650 " Tainting kernel.\n", mod->name);
Randy Dunlap9f158332005-09-13 01:25:16 -07001651 add_taint(TAINT_FORCED_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 }
1653#endif
1654
1655 /* Now do relocations. */
1656 for (i = 1; i < hdr->e_shnum; i++) {
1657 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1658 unsigned int info = sechdrs[i].sh_info;
1659
1660 /* Not a valid relocation section? */
1661 if (info >= hdr->e_shnum)
1662 continue;
1663
1664 /* Don't bother with non-allocated sections */
1665 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
1666 continue;
1667
1668 if (sechdrs[i].sh_type == SHT_REL)
1669 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1670 else if (sechdrs[i].sh_type == SHT_RELA)
1671 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1672 mod);
1673 if (err < 0)
1674 goto cleanup;
1675 }
1676
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001677 /* Find duplicate symbols */
1678 err = verify_export_symbols(mod);
1679
1680 if (err < 0)
1681 goto cleanup;
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 /* Set up and sort exception table */
1684 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1685 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
1686 sort_extable(extable, extable + mod->num_exentries);
1687
1688 /* Finally, copy percpu area over. */
1689 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
1690 sechdrs[pcpuindex].sh_size);
1691
1692 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
1693
1694 err = module_finalize(hdr, sechdrs, mod);
1695 if (err < 0)
1696 goto cleanup;
1697
Thomas Koeller378bac82005-09-06 15:17:11 -07001698 /* flush the icache in correct context */
1699 old_fs = get_fs();
1700 set_fs(KERNEL_DS);
1701
1702 /*
1703 * Flush the instruction cache, since we've played with text.
1704 * Do it before processing of module parameters, so the module
1705 * can provide parameter accessor functions of its own.
1706 */
1707 if (mod->module_init)
1708 flush_icache_range((unsigned long)mod->module_init,
1709 (unsigned long)mod->module_init
1710 + mod->init_size);
1711 flush_icache_range((unsigned long)mod->module_core,
1712 (unsigned long)mod->module_core + mod->core_size);
1713
1714 set_fs(old_fs);
1715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 mod->args = args;
Rusty Russell8d3b33f2006-03-25 03:07:05 -08001717 if (obsparmindex)
1718 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
1719 mod->name);
1720
1721 /* Size of section 0 is 0, so this works well if no params */
1722 err = parse_args(mod->name, mod->args,
1723 (struct kernel_param *)
1724 sechdrs[setupindex].sh_addr,
1725 sechdrs[setupindex].sh_size
1726 / sizeof(struct kernel_param),
1727 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 if (err < 0)
1729 goto arch_cleanup;
1730
1731 err = mod_sysfs_setup(mod,
1732 (struct kernel_param *)
1733 sechdrs[setupindex].sh_addr,
1734 sechdrs[setupindex].sh_size
1735 / sizeof(struct kernel_param));
1736 if (err < 0)
1737 goto arch_cleanup;
1738 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
1739
1740 /* Get rid of temporary copy */
1741 vfree(hdr);
1742
1743 /* Done! */
1744 return mod;
1745
1746 arch_cleanup:
1747 module_arch_cleanup(mod);
1748 cleanup:
1749 module_unload_free(mod);
1750 module_free(mod, mod->module_init);
1751 free_core:
1752 module_free(mod, mod->module_core);
1753 free_percpu:
1754 if (percpu)
1755 percpu_modfree(percpu);
1756 free_mod:
1757 kfree(args);
1758 free_hdr:
1759 vfree(hdr);
Jayachandran C6fe2e702006-01-06 00:19:54 -08001760 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 truncated:
1763 printk(KERN_ERR "Module len %lu truncated\n", len);
1764 err = -ENOEXEC;
1765 goto free_hdr;
1766}
1767
1768/*
1769 * link the module with the whole machine is stopped with interrupts off
1770 * - this defends against kallsyms not taking locks
1771 */
1772static int __link_module(void *_mod)
1773{
1774 struct module *mod = _mod;
1775 list_add(&mod->list, &modules);
1776 return 0;
1777}
1778
1779/* This is where the real work happens */
1780asmlinkage long
1781sys_init_module(void __user *umod,
1782 unsigned long len,
1783 const char __user *uargs)
1784{
1785 struct module *mod;
1786 int ret = 0;
1787
1788 /* Must have permission */
1789 if (!capable(CAP_SYS_MODULE))
1790 return -EPERM;
1791
1792 /* Only one module load at a time, please */
Ashutosh Naik6389a382006-03-23 03:00:46 -08001793 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return -EINTR;
1795
1796 /* Do all the hard work */
1797 mod = load_module(umod, len, uargs);
1798 if (IS_ERR(mod)) {
Ashutosh Naik6389a382006-03-23 03:00:46 -08001799 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return PTR_ERR(mod);
1801 }
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 /* Now sew it into the lists. They won't access us, since
1804 strong_try_module_get() will fail. */
1805 stop_machine_run(__link_module, mod, NR_CPUS);
1806
1807 /* Drop lock so they can recurse */
Ashutosh Naik6389a382006-03-23 03:00:46 -08001808 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Alan Sterne041c682006-03-27 01:16:30 -08001810 blocking_notifier_call_chain(&module_notify_list,
1811 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
1813 /* Start the module */
1814 if (mod->init != NULL)
1815 ret = mod->init();
1816 if (ret < 0) {
1817 /* Init routine failed: abort. Try to protect us from
1818 buggy refcounters. */
1819 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07001820 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 if (mod->unsafe)
1822 printk(KERN_ERR "%s: module is now stuck!\n",
1823 mod->name);
1824 else {
1825 module_put(mod);
Ashutosh Naik6389a382006-03-23 03:00:46 -08001826 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 free_module(mod);
Ashutosh Naik6389a382006-03-23 03:00:46 -08001828 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 }
1830 return ret;
1831 }
1832
1833 /* Now it's a first class citizen! */
Ashutosh Naik6389a382006-03-23 03:00:46 -08001834 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 mod->state = MODULE_STATE_LIVE;
1836 /* Drop initial reference. */
1837 module_put(mod);
1838 module_free(mod, mod->module_init);
1839 mod->module_init = NULL;
1840 mod->init_size = 0;
1841 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08001842 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844 return 0;
1845}
1846
1847static inline int within(unsigned long addr, void *start, unsigned long size)
1848{
1849 return ((void *)addr >= start && (void *)addr < start + size);
1850}
1851
1852#ifdef CONFIG_KALLSYMS
1853/*
1854 * This ignores the intensely annoying "mapping symbols" found
1855 * in ARM ELF files: $a, $t and $d.
1856 */
1857static inline int is_arm_mapping_symbol(const char *str)
1858{
1859 return str[0] == '$' && strchr("atd", str[1])
1860 && (str[2] == '\0' || str[2] == '.');
1861}
1862
1863static const char *get_ksymbol(struct module *mod,
1864 unsigned long addr,
1865 unsigned long *size,
1866 unsigned long *offset)
1867{
1868 unsigned int i, best = 0;
1869 unsigned long nextval;
1870
1871 /* At worse, next value is at end of module */
1872 if (within(addr, mod->module_init, mod->init_size))
1873 nextval = (unsigned long)mod->module_init+mod->init_text_size;
1874 else
1875 nextval = (unsigned long)mod->module_core+mod->core_text_size;
1876
1877 /* Scan for closest preceeding symbol, and next symbol. (ELF
1878 starts real symbols at 1). */
1879 for (i = 1; i < mod->num_symtab; i++) {
1880 if (mod->symtab[i].st_shndx == SHN_UNDEF)
1881 continue;
1882
1883 /* We ignore unnamed symbols: they're uninformative
1884 * and inserted at a whim. */
1885 if (mod->symtab[i].st_value <= addr
1886 && mod->symtab[i].st_value > mod->symtab[best].st_value
1887 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
1888 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
1889 best = i;
1890 if (mod->symtab[i].st_value > addr
1891 && mod->symtab[i].st_value < nextval
1892 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
1893 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
1894 nextval = mod->symtab[i].st_value;
1895 }
1896
1897 if (!best)
1898 return NULL;
1899
1900 *size = nextval - mod->symtab[best].st_value;
1901 *offset = addr - mod->symtab[best].st_value;
1902 return mod->strtab + mod->symtab[best].st_name;
1903}
1904
1905/* For kallsyms to ask for address resolution. NULL means not found.
1906 We don't lock, as this is used for oops resolution and races are a
1907 lesser concern. */
1908const char *module_address_lookup(unsigned long addr,
1909 unsigned long *size,
1910 unsigned long *offset,
1911 char **modname)
1912{
1913 struct module *mod;
1914
1915 list_for_each_entry(mod, &modules, list) {
1916 if (within(addr, mod->module_init, mod->init_size)
1917 || within(addr, mod->module_core, mod->core_size)) {
1918 *modname = mod->name;
1919 return get_ksymbol(mod, addr, size, offset);
1920 }
1921 }
1922 return NULL;
1923}
1924
1925struct module *module_get_kallsym(unsigned int symnum,
1926 unsigned long *value,
1927 char *type,
1928 char namebuf[128])
1929{
1930 struct module *mod;
1931
Ashutosh Naik6389a382006-03-23 03:00:46 -08001932 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 list_for_each_entry(mod, &modules, list) {
1934 if (symnum < mod->num_symtab) {
1935 *value = mod->symtab[symnum].st_value;
1936 *type = mod->symtab[symnum].st_info;
1937 strncpy(namebuf,
1938 mod->strtab + mod->symtab[symnum].st_name,
1939 127);
Ashutosh Naik6389a382006-03-23 03:00:46 -08001940 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 return mod;
1942 }
1943 symnum -= mod->num_symtab;
1944 }
Ashutosh Naik6389a382006-03-23 03:00:46 -08001945 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 return NULL;
1947}
1948
1949static unsigned long mod_find_symname(struct module *mod, const char *name)
1950{
1951 unsigned int i;
1952
1953 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08001954 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
1955 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return mod->symtab[i].st_value;
1957 return 0;
1958}
1959
1960/* Look for this name: can be of form module:name. */
1961unsigned long module_kallsyms_lookup_name(const char *name)
1962{
1963 struct module *mod;
1964 char *colon;
1965 unsigned long ret = 0;
1966
1967 /* Don't lock: we're in enough trouble already. */
1968 if ((colon = strchr(name, ':')) != NULL) {
1969 *colon = '\0';
1970 if ((mod = find_module(name)) != NULL)
1971 ret = mod_find_symname(mod, colon+1);
1972 *colon = ':';
1973 } else {
1974 list_for_each_entry(mod, &modules, list)
1975 if ((ret = mod_find_symname(mod, name)) != 0)
1976 break;
1977 }
1978 return ret;
1979}
1980#endif /* CONFIG_KALLSYMS */
1981
1982/* Called by the /proc file system to return a list of modules. */
1983static void *m_start(struct seq_file *m, loff_t *pos)
1984{
1985 struct list_head *i;
1986 loff_t n = 0;
1987
Ashutosh Naik6389a382006-03-23 03:00:46 -08001988 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 list_for_each(i, &modules) {
1990 if (n++ == *pos)
1991 break;
1992 }
1993 if (i == &modules)
1994 return NULL;
1995 return i;
1996}
1997
1998static void *m_next(struct seq_file *m, void *p, loff_t *pos)
1999{
2000 struct list_head *i = p;
2001 (*pos)++;
2002 if (i->next == &modules)
2003 return NULL;
2004 return i->next;
2005}
2006
2007static void m_stop(struct seq_file *m, void *p)
2008{
Ashutosh Naik6389a382006-03-23 03:00:46 -08002009 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010}
2011
2012static int m_show(struct seq_file *m, void *p)
2013{
2014 struct module *mod = list_entry(p, struct module, list);
2015 seq_printf(m, "%s %lu",
2016 mod->name, mod->init_size + mod->core_size);
2017 print_unload_info(m, mod);
2018
2019 /* Informative for users. */
2020 seq_printf(m, " %s",
2021 mod->state == MODULE_STATE_GOING ? "Unloading":
2022 mod->state == MODULE_STATE_COMING ? "Loading":
2023 "Live");
2024 /* Used by oprofile and other similar tools. */
2025 seq_printf(m, " 0x%p", mod->module_core);
2026
2027 seq_printf(m, "\n");
2028 return 0;
2029}
2030
2031/* Format: modulename size refcount deps address
2032
2033 Where refcount is a number or -, and deps is a comma-separated list
2034 of depends or -.
2035*/
2036struct seq_operations modules_op = {
2037 .start = m_start,
2038 .next = m_next,
2039 .stop = m_stop,
2040 .show = m_show
2041};
2042
2043/* Given an address, look for it in the module exception tables. */
2044const struct exception_table_entry *search_module_extables(unsigned long addr)
2045{
2046 unsigned long flags;
2047 const struct exception_table_entry *e = NULL;
2048 struct module *mod;
2049
2050 spin_lock_irqsave(&modlist_lock, flags);
2051 list_for_each_entry(mod, &modules, list) {
2052 if (mod->num_exentries == 0)
2053 continue;
2054
2055 e = search_extable(mod->extable,
2056 mod->extable + mod->num_exentries - 1,
2057 addr);
2058 if (e)
2059 break;
2060 }
2061 spin_unlock_irqrestore(&modlist_lock, flags);
2062
2063 /* Now, if we found one, we are running inside it now, hence
2064 we cannot unload the module, hence no refcnt needed. */
2065 return e;
2066}
2067
2068/* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
2069struct module *__module_text_address(unsigned long addr)
2070{
2071 struct module *mod;
2072
2073 list_for_each_entry(mod, &modules, list)
2074 if (within(addr, mod->module_init, mod->init_text_size)
2075 || within(addr, mod->module_core, mod->core_text_size))
2076 return mod;
2077 return NULL;
2078}
2079
2080struct module *module_text_address(unsigned long addr)
2081{
2082 struct module *mod;
2083 unsigned long flags;
2084
2085 spin_lock_irqsave(&modlist_lock, flags);
2086 mod = __module_text_address(addr);
2087 spin_unlock_irqrestore(&modlist_lock, flags);
2088
2089 return mod;
2090}
2091
2092/* Don't grab lock, we're oopsing. */
2093void print_modules(void)
2094{
2095 struct module *mod;
2096
2097 printk("Modules linked in:");
2098 list_for_each_entry(mod, &modules, list)
2099 printk(" %s", mod->name);
2100 printk("\n");
2101}
2102
2103void module_add_driver(struct module *mod, struct device_driver *drv)
2104{
2105 if (!mod || !drv)
2106 return;
2107
2108 /* Don't check return code; this call is idempotent */
2109 sysfs_create_link(&drv->kobj, &mod->mkobj.kobj, "module");
2110}
2111EXPORT_SYMBOL(module_add_driver);
2112
2113void module_remove_driver(struct device_driver *drv)
2114{
2115 if (!drv)
2116 return;
2117 sysfs_remove_link(&drv->kobj, "module");
2118}
2119EXPORT_SYMBOL(module_remove_driver);
2120
2121#ifdef CONFIG_MODVERSIONS
2122/* Generate the signature for struct module here, too, for modversions. */
2123void struct_module(struct module *mod) { return; }
2124EXPORT_SYMBOL(struct_module);
2125#endif