aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/module.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/module.c')
-rw-r--r--kernel/module.c517
1 files changed, 339 insertions, 178 deletions
diff --git a/kernel/module.c b/kernel/module.c
index c968d3606dca..6c562828c85c 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -59,8 +59,6 @@
59#define CREATE_TRACE_POINTS 59#define CREATE_TRACE_POINTS
60#include <trace/events/module.h> 60#include <trace/events/module.h>
61 61
62EXPORT_TRACEPOINT_SYMBOL(module_get);
63
64#if 0 62#if 0
65#define DEBUGP printk 63#define DEBUGP printk
66#else 64#else
@@ -74,11 +72,19 @@ EXPORT_TRACEPOINT_SYMBOL(module_get);
74/* If this is set, the section belongs in the init part of the module */ 72/* If this is set, the section belongs in the init part of the module */
75#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1)) 73#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
76 74
77/* List of modules, protected by module_mutex or preempt_disable 75/*
76 * Mutex protects:
77 * 1) List of modules (also safely readable with preempt_disable),
78 * 2) module_use links,
79 * 3) module_addr_min/module_addr_max.
78 * (delete uses stop_machine/add uses RCU list operations). */ 80 * (delete uses stop_machine/add uses RCU list operations). */
79DEFINE_MUTEX(module_mutex); 81DEFINE_MUTEX(module_mutex);
80EXPORT_SYMBOL_GPL(module_mutex); 82EXPORT_SYMBOL_GPL(module_mutex);
81static LIST_HEAD(modules); 83static LIST_HEAD(modules);
84#ifdef CONFIG_KGDB_KDB
85struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
86#endif /* CONFIG_KGDB_KDB */
87
82 88
83/* Block module loading/unloading? */ 89/* Block module loading/unloading? */
84int modules_disabled = 0; 90int modules_disabled = 0;
@@ -88,7 +94,8 @@ static DECLARE_WAIT_QUEUE_HEAD(module_wq);
88 94
89static BLOCKING_NOTIFIER_HEAD(module_notify_list); 95static BLOCKING_NOTIFIER_HEAD(module_notify_list);
90 96
91/* Bounds of module allocation, for speeding __module_address */ 97/* Bounds of module allocation, for speeding __module_address.
98 * Protected by module_mutex. */
92static unsigned long module_addr_min = -1UL, module_addr_max = 0; 99static unsigned long module_addr_min = -1UL, module_addr_max = 0;
93 100
94int register_module_notifier(struct notifier_block * nb) 101int register_module_notifier(struct notifier_block * nb)
@@ -178,8 +185,6 @@ extern const struct kernel_symbol __start___ksymtab_gpl[];
178extern const struct kernel_symbol __stop___ksymtab_gpl[]; 185extern const struct kernel_symbol __stop___ksymtab_gpl[];
179extern const struct kernel_symbol __start___ksymtab_gpl_future[]; 186extern const struct kernel_symbol __start___ksymtab_gpl_future[];
180extern const struct kernel_symbol __stop___ksymtab_gpl_future[]; 187extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
181extern const struct kernel_symbol __start___ksymtab_gpl_future[];
182extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
183extern const unsigned long __start___kcrctab[]; 188extern const unsigned long __start___kcrctab[];
184extern const unsigned long __start___kcrctab_gpl[]; 189extern const unsigned long __start___kcrctab_gpl[];
185extern const unsigned long __start___kcrctab_gpl_future[]; 190extern const unsigned long __start___kcrctab_gpl_future[];
@@ -329,7 +334,7 @@ static bool find_symbol_in_section(const struct symsearch *syms,
329} 334}
330 335
331/* Find a symbol and return it, along with, (optional) crc and 336/* Find a symbol and return it, along with, (optional) crc and
332 * (optional) module which owns it */ 337 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
333const struct kernel_symbol *find_symbol(const char *name, 338const struct kernel_symbol *find_symbol(const char *name,
334 struct module **owner, 339 struct module **owner,
335 const unsigned long **crc, 340 const unsigned long **crc,
@@ -370,54 +375,98 @@ EXPORT_SYMBOL_GPL(find_module);
370 375
371#ifdef CONFIG_SMP 376#ifdef CONFIG_SMP
372 377
373static void *percpu_modalloc(unsigned long size, unsigned long align, 378static inline void __percpu *mod_percpu(struct module *mod)
374 const char *name)
375{ 379{
376 void *ptr; 380 return mod->percpu;
381}
377 382
383static int percpu_modalloc(struct module *mod,
384 unsigned long size, unsigned long align)
385{
378 if (align > PAGE_SIZE) { 386 if (align > PAGE_SIZE) {
379 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n", 387 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
380 name, align, PAGE_SIZE); 388 mod->name, align, PAGE_SIZE);
381 align = PAGE_SIZE; 389 align = PAGE_SIZE;
382 } 390 }
383 391
384 ptr = __alloc_reserved_percpu(size, align); 392 mod->percpu = __alloc_reserved_percpu(size, align);
385 if (!ptr) 393 if (!mod->percpu) {
386 printk(KERN_WARNING 394 printk(KERN_WARNING
387 "Could not allocate %lu bytes percpu data\n", size); 395 "Could not allocate %lu bytes percpu data\n", size);
388 return ptr; 396 return -ENOMEM;
397 }
398 mod->percpu_size = size;
399 return 0;
389} 400}
390 401
391static void percpu_modfree(void *freeme) 402static void percpu_modfree(struct module *mod)
392{ 403{
393 free_percpu(freeme); 404 free_percpu(mod->percpu);
394} 405}
395 406
396static unsigned int find_pcpusec(Elf_Ehdr *hdr, 407static unsigned int find_pcpusec(Elf_Ehdr *hdr,
397 Elf_Shdr *sechdrs, 408 Elf_Shdr *sechdrs,
398 const char *secstrings) 409 const char *secstrings)
399{ 410{
400 return find_sec(hdr, sechdrs, secstrings, ".data.percpu"); 411 return find_sec(hdr, sechdrs, secstrings, ".data..percpu");
401} 412}
402 413
403static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size) 414static void percpu_modcopy(struct module *mod,
415 const void *from, unsigned long size)
404{ 416{
405 int cpu; 417 int cpu;
406 418
407 for_each_possible_cpu(cpu) 419 for_each_possible_cpu(cpu)
408 memcpy(pcpudest + per_cpu_offset(cpu), from, size); 420 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
421}
422
423/**
424 * is_module_percpu_address - test whether address is from module static percpu
425 * @addr: address to test
426 *
427 * Test whether @addr belongs to module static percpu area.
428 *
429 * RETURNS:
430 * %true if @addr is from module static percpu area
431 */
432bool is_module_percpu_address(unsigned long addr)
433{
434 struct module *mod;
435 unsigned int cpu;
436
437 preempt_disable();
438
439 list_for_each_entry_rcu(mod, &modules, list) {
440 if (!mod->percpu_size)
441 continue;
442 for_each_possible_cpu(cpu) {
443 void *start = per_cpu_ptr(mod->percpu, cpu);
444
445 if ((void *)addr >= start &&
446 (void *)addr < start + mod->percpu_size) {
447 preempt_enable();
448 return true;
449 }
450 }
451 }
452
453 preempt_enable();
454 return false;
409} 455}
410 456
411#else /* ... !CONFIG_SMP */ 457#else /* ... !CONFIG_SMP */
412 458
413static inline void *percpu_modalloc(unsigned long size, unsigned long align, 459static inline void __percpu *mod_percpu(struct module *mod)
414 const char *name)
415{ 460{
416 return NULL; 461 return NULL;
417} 462}
418static inline void percpu_modfree(void *pcpuptr) 463static inline int percpu_modalloc(struct module *mod,
464 unsigned long size, unsigned long align)
465{
466 return -ENOMEM;
467}
468static inline void percpu_modfree(struct module *mod)
419{ 469{
420 BUG();
421} 470}
422static inline unsigned int find_pcpusec(Elf_Ehdr *hdr, 471static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
423 Elf_Shdr *sechdrs, 472 Elf_Shdr *sechdrs,
@@ -425,12 +474,16 @@ static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
425{ 474{
426 return 0; 475 return 0;
427} 476}
428static inline void percpu_modcopy(void *pcpudst, const void *src, 477static inline void percpu_modcopy(struct module *mod,
429 unsigned long size) 478 const void *from, unsigned long size)
430{ 479{
431 /* pcpusec should be 0, and size of that section should be 0. */ 480 /* pcpusec should be 0, and size of that section should be 0. */
432 BUG_ON(size != 0); 481 BUG_ON(size != 0);
433} 482}
483bool is_module_percpu_address(unsigned long addr)
484{
485 return false;
486}
434 487
435#endif /* CONFIG_SMP */ 488#endif /* CONFIG_SMP */
436 489
@@ -467,35 +520,34 @@ MODINFO_ATTR(srcversion);
467static char last_unloaded_module[MODULE_NAME_LEN+1]; 520static char last_unloaded_module[MODULE_NAME_LEN+1];
468 521
469#ifdef CONFIG_MODULE_UNLOAD 522#ifdef CONFIG_MODULE_UNLOAD
523
524EXPORT_TRACEPOINT_SYMBOL(module_get);
525
470/* Init the unload section of the module. */ 526/* Init the unload section of the module. */
471static void module_unload_init(struct module *mod) 527static void module_unload_init(struct module *mod)
472{ 528{
473 int cpu; 529 int cpu;
474 530
475 INIT_LIST_HEAD(&mod->modules_which_use_me); 531 INIT_LIST_HEAD(&mod->source_list);
476 for_each_possible_cpu(cpu) 532 INIT_LIST_HEAD(&mod->target_list);
477 per_cpu_ptr(mod->refptr, cpu)->count = 0; 533 for_each_possible_cpu(cpu) {
534 per_cpu_ptr(mod->refptr, cpu)->incs = 0;
535 per_cpu_ptr(mod->refptr, cpu)->decs = 0;
536 }
478 537
479 /* Hold reference count during initialization. */ 538 /* Hold reference count during initialization. */
480 __this_cpu_write(mod->refptr->count, 1); 539 __this_cpu_write(mod->refptr->incs, 1);
481 /* Backwards compatibility macros put refcount during init. */ 540 /* Backwards compatibility macros put refcount during init. */
482 mod->waiter = current; 541 mod->waiter = current;
483} 542}
484 543
485/* modules using other modules */
486struct module_use
487{
488 struct list_head list;
489 struct module *module_which_uses;
490};
491
492/* Does a already use b? */ 544/* Does a already use b? */
493static int already_uses(struct module *a, struct module *b) 545static int already_uses(struct module *a, struct module *b)
494{ 546{
495 struct module_use *use; 547 struct module_use *use;
496 548
497 list_for_each_entry(use, &b->modules_which_use_me, list) { 549 list_for_each_entry(use, &b->source_list, source_list) {
498 if (use->module_which_uses == a) { 550 if (use->source == a) {
499 DEBUGP("%s uses %s!\n", a->name, b->name); 551 DEBUGP("%s uses %s!\n", a->name, b->name);
500 return 1; 552 return 1;
501 } 553 }
@@ -504,62 +556,68 @@ static int already_uses(struct module *a, struct module *b)
504 return 0; 556 return 0;
505} 557}
506 558
507/* Module a uses b */ 559/*
508int use_module(struct module *a, struct module *b) 560 * Module a uses b
561 * - we add 'a' as a "source", 'b' as a "target" of module use
562 * - the module_use is added to the list of 'b' sources (so
563 * 'b' can walk the list to see who sourced them), and of 'a'
564 * targets (so 'a' can see what modules it targets).
565 */
566static int add_module_usage(struct module *a, struct module *b)
509{ 567{
510 struct module_use *use; 568 struct module_use *use;
511 int no_warn, err;
512 569
513 if (b == NULL || already_uses(a, b)) return 1; 570 DEBUGP("Allocating new usage for %s.\n", a->name);
571 use = kmalloc(sizeof(*use), GFP_ATOMIC);
572 if (!use) {
573 printk(KERN_WARNING "%s: out of memory loading\n", a->name);
574 return -ENOMEM;
575 }
576
577 use->source = a;
578 use->target = b;
579 list_add(&use->source_list, &b->source_list);
580 list_add(&use->target_list, &a->target_list);
581 return 0;
582}
514 583
515 /* If we're interrupted or time out, we fail. */ 584/* Module a uses b: caller needs module_mutex() */
516 if (wait_event_interruptible_timeout( 585int ref_module(struct module *a, struct module *b)
517 module_wq, (err = strong_try_module_get(b)) != -EBUSY, 586{
518 30 * HZ) <= 0) { 587 int err;
519 printk("%s: gave up waiting for init of module %s.\n", 588
520 a->name, b->name); 589 if (b == NULL || already_uses(a, b))
521 return 0; 590 return 0;
522 }
523 591
524 /* If strong_try_module_get() returned a different error, we fail. */ 592 /* If module isn't available, we fail. */
593 err = strong_try_module_get(b);
525 if (err) 594 if (err)
526 return 0; 595 return err;
527 596
528 DEBUGP("Allocating new usage for %s.\n", a->name); 597 err = add_module_usage(a, b);
529 use = kmalloc(sizeof(*use), GFP_ATOMIC); 598 if (err) {
530 if (!use) {
531 printk("%s: out of memory loading\n", a->name);
532 module_put(b); 599 module_put(b);
533 return 0; 600 return err;
534 } 601 }
535 602 return 0;
536 use->module_which_uses = a;
537 list_add(&use->list, &b->modules_which_use_me);
538 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
539 return 1;
540} 603}
541EXPORT_SYMBOL_GPL(use_module); 604EXPORT_SYMBOL_GPL(ref_module);
542 605
543/* Clear the unload stuff of the module. */ 606/* Clear the unload stuff of the module. */
544static void module_unload_free(struct module *mod) 607static void module_unload_free(struct module *mod)
545{ 608{
546 struct module *i; 609 struct module_use *use, *tmp;
547
548 list_for_each_entry(i, &modules, list) {
549 struct module_use *use;
550 610
551 list_for_each_entry(use, &i->modules_which_use_me, list) { 611 mutex_lock(&module_mutex);
552 if (use->module_which_uses == mod) { 612 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
553 DEBUGP("%s unusing %s\n", mod->name, i->name); 613 struct module *i = use->target;
554 module_put(i); 614 DEBUGP("%s unusing %s\n", mod->name, i->name);
555 list_del(&use->list); 615 module_put(i);
556 kfree(use); 616 list_del(&use->source_list);
557 sysfs_remove_link(i->holders_dir, mod->name); 617 list_del(&use->target_list);
558 /* There can be at most one match. */ 618 kfree(use);
559 break;
560 }
561 }
562 } 619 }
620 mutex_unlock(&module_mutex);
563} 621}
564 622
565#ifdef CONFIG_MODULE_FORCE_UNLOAD 623#ifdef CONFIG_MODULE_FORCE_UNLOAD
@@ -616,12 +674,28 @@ static int try_stop_module(struct module *mod, int flags, int *forced)
616 674
617unsigned int module_refcount(struct module *mod) 675unsigned int module_refcount(struct module *mod)
618{ 676{
619 unsigned int total = 0; 677 unsigned int incs = 0, decs = 0;
620 int cpu; 678 int cpu;
621 679
622 for_each_possible_cpu(cpu) 680 for_each_possible_cpu(cpu)
623 total += per_cpu_ptr(mod->refptr, cpu)->count; 681 decs += per_cpu_ptr(mod->refptr, cpu)->decs;
624 return total; 682 /*
683 * ensure the incs are added up after the decs.
684 * module_put ensures incs are visible before decs with smp_wmb.
685 *
686 * This 2-count scheme avoids the situation where the refcount
687 * for CPU0 is read, then CPU0 increments the module refcount,
688 * then CPU1 drops that refcount, then the refcount for CPU1 is
689 * read. We would record a decrement but not its corresponding
690 * increment so we would see a low count (disaster).
691 *
692 * Rare situation? But module_refcount can be preempted, and we
693 * might be tallying up 4096+ CPUs. So it is not impossible.
694 */
695 smp_rmb();
696 for_each_possible_cpu(cpu)
697 incs += per_cpu_ptr(mod->refptr, cpu)->incs;
698 return incs - decs;
625} 699}
626EXPORT_SYMBOL(module_refcount); 700EXPORT_SYMBOL(module_refcount);
627 701
@@ -657,16 +731,8 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
657 return -EFAULT; 731 return -EFAULT;
658 name[MODULE_NAME_LEN-1] = '\0'; 732 name[MODULE_NAME_LEN-1] = '\0';
659 733
660 /* Create stop_machine threads since free_module relies on 734 if (mutex_lock_interruptible(&module_mutex) != 0)
661 * a non-failing stop_machine call. */ 735 return -EINTR;
662 ret = stop_machine_create();
663 if (ret)
664 return ret;
665
666 if (mutex_lock_interruptible(&module_mutex) != 0) {
667 ret = -EINTR;
668 goto out_stop;
669 }
670 736
671 mod = find_module(name); 737 mod = find_module(name);
672 if (!mod) { 738 if (!mod) {
@@ -674,7 +740,7 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
674 goto out; 740 goto out;
675 } 741 }
676 742
677 if (!list_empty(&mod->modules_which_use_me)) { 743 if (!list_empty(&mod->source_list)) {
678 /* Other modules depend on us: get rid of them first. */ 744 /* Other modules depend on us: get rid of them first. */
679 ret = -EWOULDBLOCK; 745 ret = -EWOULDBLOCK;
680 goto out; 746 goto out;
@@ -718,16 +784,14 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
718 blocking_notifier_call_chain(&module_notify_list, 784 blocking_notifier_call_chain(&module_notify_list,
719 MODULE_STATE_GOING, mod); 785 MODULE_STATE_GOING, mod);
720 async_synchronize_full(); 786 async_synchronize_full();
721 mutex_lock(&module_mutex); 787
722 /* Store the name of the last unloaded module for diagnostic purposes */ 788 /* Store the name of the last unloaded module for diagnostic purposes */
723 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module)); 789 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
724 ddebug_remove_module(mod->name);
725 free_module(mod);
726 790
727 out: 791 free_module(mod);
792 return 0;
793out:
728 mutex_unlock(&module_mutex); 794 mutex_unlock(&module_mutex);
729out_stop:
730 stop_machine_destroy();
731 return ret; 795 return ret;
732} 796}
733 797
@@ -740,9 +804,9 @@ static inline void print_unload_info(struct seq_file *m, struct module *mod)
740 804
741 /* Always include a trailing , so userspace can differentiate 805 /* Always include a trailing , so userspace can differentiate
742 between this and the old multi-field proc format. */ 806 between this and the old multi-field proc format. */
743 list_for_each_entry(use, &mod->modules_which_use_me, list) { 807 list_for_each_entry(use, &mod->source_list, source_list) {
744 printed_something = 1; 808 printed_something = 1;
745 seq_printf(m, "%s,", use->module_which_uses->name); 809 seq_printf(m, "%s,", use->source->name);
746 } 810 }
747 811
748 if (mod->init != NULL && mod->exit == NULL) { 812 if (mod->init != NULL && mod->exit == NULL) {
@@ -798,10 +862,10 @@ void module_put(struct module *module)
798{ 862{
799 if (module) { 863 if (module) {
800 preempt_disable(); 864 preempt_disable();
801 __this_cpu_dec(module->refptr->count); 865 smp_wmb(); /* see comment in module_refcount */
866 __this_cpu_inc(module->refptr->decs);
802 867
803 trace_module_put(module, _RET_IP_, 868 trace_module_put(module, _RET_IP_);
804 __this_cpu_read(module->refptr->count));
805 /* Maybe they're waiting for us to drop reference? */ 869 /* Maybe they're waiting for us to drop reference? */
806 if (unlikely(!module_is_live(module))) 870 if (unlikely(!module_is_live(module)))
807 wake_up_process(module->waiter); 871 wake_up_process(module->waiter);
@@ -821,11 +885,11 @@ static inline void module_unload_free(struct module *mod)
821{ 885{
822} 886}
823 887
824int use_module(struct module *a, struct module *b) 888int ref_module(struct module *a, struct module *b)
825{ 889{
826 return strong_try_module_get(b) == 0; 890 return strong_try_module_get(b);
827} 891}
828EXPORT_SYMBOL_GPL(use_module); 892EXPORT_SYMBOL_GPL(ref_module);
829 893
830static inline void module_unload_init(struct module *mod) 894static inline void module_unload_init(struct module *mod)
831{ 895{
@@ -942,6 +1006,8 @@ static inline int check_modstruct_version(Elf_Shdr *sechdrs,
942{ 1006{
943 const unsigned long *crc; 1007 const unsigned long *crc;
944 1008
1009 /* Since this should be found in kernel (which can't be removed),
1010 * no locking is necessary. */
945 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL, 1011 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
946 &crc, true, false)) 1012 &crc, true, false))
947 BUG(); 1013 BUG();
@@ -984,29 +1050,62 @@ static inline int same_magic(const char *amagic, const char *bmagic,
984} 1050}
985#endif /* CONFIG_MODVERSIONS */ 1051#endif /* CONFIG_MODVERSIONS */
986 1052
987/* Resolve a symbol for this module. I.e. if we find one, record usage. 1053/* Resolve a symbol for this module. I.e. if we find one, record usage. */
988 Must be holding module_mutex. */
989static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs, 1054static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
990 unsigned int versindex, 1055 unsigned int versindex,
991 const char *name, 1056 const char *name,
992 struct module *mod) 1057 struct module *mod,
1058 char ownername[])
993{ 1059{
994 struct module *owner; 1060 struct module *owner;
995 const struct kernel_symbol *sym; 1061 const struct kernel_symbol *sym;
996 const unsigned long *crc; 1062 const unsigned long *crc;
1063 int err;
997 1064
1065 mutex_lock(&module_mutex);
998 sym = find_symbol(name, &owner, &crc, 1066 sym = find_symbol(name, &owner, &crc,
999 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true); 1067 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1000 /* use_module can fail due to OOM, 1068 if (!sym)
1001 or module initialization or unloading */ 1069 goto unlock;
1002 if (sym) { 1070
1003 if (!check_version(sechdrs, versindex, name, mod, crc, owner) 1071 if (!check_version(sechdrs, versindex, name, mod, crc, owner)) {
1004 || !use_module(mod, owner)) 1072 sym = ERR_PTR(-EINVAL);
1005 sym = NULL; 1073 goto getname;
1006 } 1074 }
1075
1076 err = ref_module(mod, owner);
1077 if (err) {
1078 sym = ERR_PTR(err);
1079 goto getname;
1080 }
1081
1082getname:
1083 /* We must make copy under the lock if we failed to get ref. */
1084 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1085unlock:
1086 mutex_unlock(&module_mutex);
1007 return sym; 1087 return sym;
1008} 1088}
1009 1089
1090static const struct kernel_symbol *resolve_symbol_wait(Elf_Shdr *sechdrs,
1091 unsigned int versindex,
1092 const char *name,
1093 struct module *mod)
1094{
1095 const struct kernel_symbol *ksym;
1096 char ownername[MODULE_NAME_LEN];
1097
1098 if (wait_event_interruptible_timeout(module_wq,
1099 !IS_ERR(ksym = resolve_symbol(sechdrs, versindex, name,
1100 mod, ownername)) ||
1101 PTR_ERR(ksym) != -EBUSY,
1102 30 * HZ) <= 0) {
1103 printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
1104 mod->name, ownername);
1105 }
1106 return ksym;
1107}
1108
1010/* 1109/*
1011 * /sys/module/foo/sections stuff 1110 * /sys/module/foo/sections stuff
1012 * J. Corbet <corbet@lwn.net> 1111 * J. Corbet <corbet@lwn.net>
@@ -1125,7 +1224,7 @@ struct module_notes_attrs {
1125 struct bin_attribute attrs[0]; 1224 struct bin_attribute attrs[0];
1126}; 1225};
1127 1226
1128static ssize_t module_notes_read(struct kobject *kobj, 1227static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
1129 struct bin_attribute *bin_attr, 1228 struct bin_attribute *bin_attr,
1130 char *buf, loff_t pos, size_t count) 1229 char *buf, loff_t pos, size_t count)
1131{ 1230{
@@ -1236,7 +1335,34 @@ static inline void remove_notes_attrs(struct module *mod)
1236#endif 1335#endif
1237 1336
1238#ifdef CONFIG_SYSFS 1337#ifdef CONFIG_SYSFS
1239int module_add_modinfo_attrs(struct module *mod) 1338static void add_usage_links(struct module *mod)
1339{
1340#ifdef CONFIG_MODULE_UNLOAD
1341 struct module_use *use;
1342 int nowarn;
1343
1344 mutex_lock(&module_mutex);
1345 list_for_each_entry(use, &mod->target_list, target_list) {
1346 nowarn = sysfs_create_link(use->target->holders_dir,
1347 &mod->mkobj.kobj, mod->name);
1348 }
1349 mutex_unlock(&module_mutex);
1350#endif
1351}
1352
1353static void del_usage_links(struct module *mod)
1354{
1355#ifdef CONFIG_MODULE_UNLOAD
1356 struct module_use *use;
1357
1358 mutex_lock(&module_mutex);
1359 list_for_each_entry(use, &mod->target_list, target_list)
1360 sysfs_remove_link(use->target->holders_dir, mod->name);
1361 mutex_unlock(&module_mutex);
1362#endif
1363}
1364
1365static int module_add_modinfo_attrs(struct module *mod)
1240{ 1366{
1241 struct module_attribute *attr; 1367 struct module_attribute *attr;
1242 struct module_attribute *temp_attr; 1368 struct module_attribute *temp_attr;
@@ -1262,7 +1388,7 @@ int module_add_modinfo_attrs(struct module *mod)
1262 return error; 1388 return error;
1263} 1389}
1264 1390
1265void module_remove_modinfo_attrs(struct module *mod) 1391static void module_remove_modinfo_attrs(struct module *mod)
1266{ 1392{
1267 struct module_attribute *attr; 1393 struct module_attribute *attr;
1268 int i; 1394 int i;
@@ -1278,7 +1404,7 @@ void module_remove_modinfo_attrs(struct module *mod)
1278 kfree(mod->modinfo_attrs); 1404 kfree(mod->modinfo_attrs);
1279} 1405}
1280 1406
1281int mod_sysfs_init(struct module *mod) 1407static int mod_sysfs_init(struct module *mod)
1282{ 1408{
1283 int err; 1409 int err;
1284 struct kobject *kobj; 1410 struct kobject *kobj;
@@ -1312,12 +1438,16 @@ out:
1312 return err; 1438 return err;
1313} 1439}
1314 1440
1315int mod_sysfs_setup(struct module *mod, 1441static int mod_sysfs_setup(struct module *mod,
1316 struct kernel_param *kparam, 1442 struct kernel_param *kparam,
1317 unsigned int num_params) 1443 unsigned int num_params)
1318{ 1444{
1319 int err; 1445 int err;
1320 1446
1447 err = mod_sysfs_init(mod);
1448 if (err)
1449 goto out;
1450
1321 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj); 1451 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1322 if (!mod->holders_dir) { 1452 if (!mod->holders_dir) {
1323 err = -ENOMEM; 1453 err = -ENOMEM;
@@ -1332,6 +1462,8 @@ int mod_sysfs_setup(struct module *mod,
1332 if (err) 1462 if (err)
1333 goto out_unreg_param; 1463 goto out_unreg_param;
1334 1464
1465 add_usage_links(mod);
1466
1335 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD); 1467 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1336 return 0; 1468 return 0;
1337 1469
@@ -1341,6 +1473,7 @@ out_unreg_holders:
1341 kobject_put(mod->holders_dir); 1473 kobject_put(mod->holders_dir);
1342out_unreg: 1474out_unreg:
1343 kobject_put(&mod->mkobj.kobj); 1475 kobject_put(&mod->mkobj.kobj);
1476out:
1344 return err; 1477 return err;
1345} 1478}
1346 1479
@@ -1351,14 +1484,40 @@ static void mod_sysfs_fini(struct module *mod)
1351 1484
1352#else /* CONFIG_SYSFS */ 1485#else /* CONFIG_SYSFS */
1353 1486
1487static inline int mod_sysfs_init(struct module *mod)
1488{
1489 return 0;
1490}
1491
1492static inline int mod_sysfs_setup(struct module *mod,
1493 struct kernel_param *kparam,
1494 unsigned int num_params)
1495{
1496 return 0;
1497}
1498
1499static inline int module_add_modinfo_attrs(struct module *mod)
1500{
1501 return 0;
1502}
1503
1504static inline void module_remove_modinfo_attrs(struct module *mod)
1505{
1506}
1507
1354static void mod_sysfs_fini(struct module *mod) 1508static void mod_sysfs_fini(struct module *mod)
1355{ 1509{
1356} 1510}
1357 1511
1512static void del_usage_links(struct module *mod)
1513{
1514}
1515
1358#endif /* CONFIG_SYSFS */ 1516#endif /* CONFIG_SYSFS */
1359 1517
1360static void mod_kobject_remove(struct module *mod) 1518static void mod_kobject_remove(struct module *mod)
1361{ 1519{
1520 del_usage_links(mod);
1362 module_remove_modinfo_attrs(mod); 1521 module_remove_modinfo_attrs(mod);
1363 module_param_sysfs_remove(mod); 1522 module_param_sysfs_remove(mod);
1364 kobject_put(mod->mkobj.drivers_dir); 1523 kobject_put(mod->mkobj.drivers_dir);
@@ -1377,17 +1536,22 @@ static int __unlink_module(void *_mod)
1377 return 0; 1536 return 0;
1378} 1537}
1379 1538
1380/* Free a module, remove from lists, etc (must hold module_mutex). */ 1539/* Free a module, remove from lists, etc. */
1381static void free_module(struct module *mod) 1540static void free_module(struct module *mod)
1382{ 1541{
1383 trace_module_free(mod); 1542 trace_module_free(mod);
1384 1543
1385 /* Delete from various lists */ 1544 /* Delete from various lists */
1545 mutex_lock(&module_mutex);
1386 stop_machine(__unlink_module, mod, NULL); 1546 stop_machine(__unlink_module, mod, NULL);
1547 mutex_unlock(&module_mutex);
1387 remove_notes_attrs(mod); 1548 remove_notes_attrs(mod);
1388 remove_sect_attrs(mod); 1549 remove_sect_attrs(mod);
1389 mod_kobject_remove(mod); 1550 mod_kobject_remove(mod);
1390 1551
1552 /* Remove dynamic debug info */
1553 ddebug_remove_module(mod->name);
1554
1391 /* Arch-specific cleanup. */ 1555 /* Arch-specific cleanup. */
1392 module_arch_cleanup(mod); 1556 module_arch_cleanup(mod);
1393 1557
@@ -1400,8 +1564,7 @@ static void free_module(struct module *mod)
1400 /* This may be NULL, but that's OK */ 1564 /* This may be NULL, but that's OK */
1401 module_free(mod, mod->module_init); 1565 module_free(mod, mod->module_init);
1402 kfree(mod->args); 1566 kfree(mod->args);
1403 if (mod->percpu) 1567 percpu_modfree(mod);
1404 percpu_modfree(mod->percpu);
1405#if defined(CONFIG_MODULE_UNLOAD) 1568#if defined(CONFIG_MODULE_UNLOAD)
1406 if (mod->refptr) 1569 if (mod->refptr)
1407 free_percpu(mod->refptr); 1570 free_percpu(mod->refptr);
@@ -1435,6 +1598,8 @@ EXPORT_SYMBOL_GPL(__symbol_get);
1435/* 1598/*
1436 * Ensure that an exported symbol [global namespace] does not already exist 1599 * Ensure that an exported symbol [global namespace] does not already exist
1437 * in the kernel or in some other module's exported symbol table. 1600 * in the kernel or in some other module's exported symbol table.
1601 *
1602 * You must hold the module_mutex.
1438 */ 1603 */
1439static int verify_export_symbols(struct module *mod) 1604static int verify_export_symbols(struct module *mod)
1440{ 1605{
@@ -1500,27 +1665,29 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
1500 break; 1665 break;
1501 1666
1502 case SHN_UNDEF: 1667 case SHN_UNDEF:
1503 ksym = resolve_symbol(sechdrs, versindex, 1668 ksym = resolve_symbol_wait(sechdrs, versindex,
1504 strtab + sym[i].st_name, mod); 1669 strtab + sym[i].st_name,
1670 mod);
1505 /* Ok if resolved. */ 1671 /* Ok if resolved. */
1506 if (ksym) { 1672 if (ksym && !IS_ERR(ksym)) {
1507 sym[i].st_value = ksym->value; 1673 sym[i].st_value = ksym->value;
1508 break; 1674 break;
1509 } 1675 }
1510 1676
1511 /* Ok if weak. */ 1677 /* Ok if weak. */
1512 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK) 1678 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1513 break; 1679 break;
1514 1680
1515 printk(KERN_WARNING "%s: Unknown symbol %s\n", 1681 printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
1516 mod->name, strtab + sym[i].st_name); 1682 mod->name, strtab + sym[i].st_name,
1517 ret = -ENOENT; 1683 PTR_ERR(ksym));
1684 ret = PTR_ERR(ksym) ?: -ENOENT;
1518 break; 1685 break;
1519 1686
1520 default: 1687 default:
1521 /* Divert to percpu allocation if a percpu var. */ 1688 /* Divert to percpu allocation if a percpu var. */
1522 if (sym[i].st_shndx == pcpuindex) 1689 if (sym[i].st_shndx == pcpuindex)
1523 secbase = (unsigned long)mod->percpu; 1690 secbase = (unsigned long)mod_percpu(mod);
1524 else 1691 else
1525 secbase = sechdrs[sym[i].st_shndx].sh_addr; 1692 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1526 sym[i].st_value += secbase; 1693 sym[i].st_value += secbase;
@@ -1897,16 +2064,24 @@ static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
1897#endif 2064#endif
1898} 2065}
1899 2066
2067static void dynamic_debug_remove(struct _ddebug *debug)
2068{
2069 if (debug)
2070 ddebug_remove_module(debug->modname);
2071}
2072
1900static void *module_alloc_update_bounds(unsigned long size) 2073static void *module_alloc_update_bounds(unsigned long size)
1901{ 2074{
1902 void *ret = module_alloc(size); 2075 void *ret = module_alloc(size);
1903 2076
1904 if (ret) { 2077 if (ret) {
2078 mutex_lock(&module_mutex);
1905 /* Update module bounds. */ 2079 /* Update module bounds. */
1906 if ((unsigned long)ret < module_addr_min) 2080 if ((unsigned long)ret < module_addr_min)
1907 module_addr_min = (unsigned long)ret; 2081 module_addr_min = (unsigned long)ret;
1908 if ((unsigned long)ret + size > module_addr_max) 2082 if ((unsigned long)ret + size > module_addr_max)
1909 module_addr_max = (unsigned long)ret + size; 2083 module_addr_max = (unsigned long)ret + size;
2084 mutex_unlock(&module_mutex);
1910 } 2085 }
1911 return ret; 2086 return ret;
1912} 2087}
@@ -1954,8 +2129,11 @@ static noinline struct module *load_module(void __user *umod,
1954 unsigned int modindex, versindex, infoindex, pcpuindex; 2129 unsigned int modindex, versindex, infoindex, pcpuindex;
1955 struct module *mod; 2130 struct module *mod;
1956 long err = 0; 2131 long err = 0;
1957 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */ 2132 void *ptr = NULL; /* Stops spurious gcc warning */
1958 unsigned long symoffs, stroffs, *strmap; 2133 unsigned long symoffs, stroffs, *strmap;
2134 void __percpu *percpu;
2135 struct _ddebug *debug = NULL;
2136 unsigned int num_debug = 0;
1959 2137
1960 mm_segment_t old_fs; 2138 mm_segment_t old_fs;
1961 2139
@@ -2080,11 +2258,6 @@ static noinline struct module *load_module(void __user *umod,
2080 goto free_mod; 2258 goto free_mod;
2081 } 2259 }
2082 2260
2083 if (find_module(mod->name)) {
2084 err = -EEXIST;
2085 goto free_mod;
2086 }
2087
2088 mod->state = MODULE_STATE_COMING; 2261 mod->state = MODULE_STATE_COMING;
2089 2262
2090 /* Allow arches to frob section contents and sizes. */ 2263 /* Allow arches to frob section contents and sizes. */
@@ -2094,16 +2267,14 @@ static noinline struct module *load_module(void __user *umod,
2094 2267
2095 if (pcpuindex) { 2268 if (pcpuindex) {
2096 /* We have a special allocation for this section. */ 2269 /* We have a special allocation for this section. */
2097 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size, 2270 err = percpu_modalloc(mod, sechdrs[pcpuindex].sh_size,
2098 sechdrs[pcpuindex].sh_addralign, 2271 sechdrs[pcpuindex].sh_addralign);
2099 mod->name); 2272 if (err)
2100 if (!percpu) {
2101 err = -ENOMEM;
2102 goto free_mod; 2273 goto free_mod;
2103 }
2104 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC; 2274 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2105 mod->percpu = percpu;
2106 } 2275 }
2276 /* Keep this around for failure path. */
2277 percpu = mod_percpu(mod);
2107 2278
2108 /* Determine total sizes, and put offsets in sh_entsize. For now 2279 /* Determine total sizes, and put offsets in sh_entsize. For now
2109 this is done generically; there doesn't appear to be any 2280 this is done generically; there doesn't appear to be any
@@ -2177,11 +2348,6 @@ static noinline struct module *load_module(void __user *umod,
2177 /* Now we've moved module, initialize linked lists, etc. */ 2348 /* Now we've moved module, initialize linked lists, etc. */
2178 module_unload_init(mod); 2349 module_unload_init(mod);
2179 2350
2180 /* add kobject, so we can reference it. */
2181 err = mod_sysfs_init(mod);
2182 if (err)
2183 goto free_unload;
2184
2185 /* Set up license info based on the info section */ 2351 /* Set up license info based on the info section */
2186 set_license(mod, get_modinfo(sechdrs, infoindex, "license")); 2352 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2187 2353
@@ -2306,18 +2472,13 @@ static noinline struct module *load_module(void __user *umod,
2306 goto cleanup; 2472 goto cleanup;
2307 } 2473 }
2308 2474
2309 /* Find duplicate symbols */
2310 err = verify_export_symbols(mod);
2311 if (err < 0)
2312 goto cleanup;
2313
2314 /* Set up and sort exception table */ 2475 /* Set up and sort exception table */
2315 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table", 2476 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2316 sizeof(*mod->extable), &mod->num_exentries); 2477 sizeof(*mod->extable), &mod->num_exentries);
2317 sort_extable(mod->extable, mod->extable + mod->num_exentries); 2478 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2318 2479
2319 /* Finally, copy percpu area over. */ 2480 /* Finally, copy percpu area over. */
2320 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr, 2481 percpu_modcopy(mod, (void *)sechdrs[pcpuindex].sh_addr,
2321 sechdrs[pcpuindex].sh_size); 2482 sechdrs[pcpuindex].sh_size);
2322 2483
2323 add_kallsyms(mod, sechdrs, hdr->e_shnum, symindex, strindex, 2484 add_kallsyms(mod, sechdrs, hdr->e_shnum, symindex, strindex,
@@ -2325,15 +2486,9 @@ static noinline struct module *load_module(void __user *umod,
2325 kfree(strmap); 2486 kfree(strmap);
2326 strmap = NULL; 2487 strmap = NULL;
2327 2488
2328 if (!mod->taints) { 2489 if (!mod->taints)
2329 struct _ddebug *debug;
2330 unsigned int num_debug;
2331
2332 debug = section_objs(hdr, sechdrs, secstrings, "__verbose", 2490 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2333 sizeof(*debug), &num_debug); 2491 sizeof(*debug), &num_debug);
2334 if (debug)
2335 dynamic_debug_setup(debug, num_debug);
2336 }
2337 2492
2338 err = module_finalize(hdr, sechdrs, mod); 2493 err = module_finalize(hdr, sechdrs, mod);
2339 if (err < 0) 2494 if (err < 0)
@@ -2369,7 +2524,22 @@ static noinline struct module *load_module(void __user *umod,
2369 * function to insert in a way safe to concurrent readers. 2524 * function to insert in a way safe to concurrent readers.
2370 * The mutex protects against concurrent writers. 2525 * The mutex protects against concurrent writers.
2371 */ 2526 */
2527 mutex_lock(&module_mutex);
2528 if (find_module(mod->name)) {
2529 err = -EEXIST;
2530 goto unlock;
2531 }
2532
2533 if (debug)
2534 dynamic_debug_setup(debug, num_debug);
2535
2536 /* Find duplicate symbols */
2537 err = verify_export_symbols(mod);
2538 if (err < 0)
2539 goto ddebug;
2540
2372 list_add_rcu(&mod->list, &modules); 2541 list_add_rcu(&mod->list, &modules);
2542 mutex_unlock(&module_mutex);
2373 2543
2374 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL); 2544 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
2375 if (err < 0) 2545 if (err < 0)
@@ -2378,6 +2548,7 @@ static noinline struct module *load_module(void __user *umod,
2378 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp); 2548 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
2379 if (err < 0) 2549 if (err < 0)
2380 goto unlink; 2550 goto unlink;
2551
2381 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs); 2552 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2382 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs); 2553 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2383 2554
@@ -2390,15 +2561,17 @@ static noinline struct module *load_module(void __user *umod,
2390 return mod; 2561 return mod;
2391 2562
2392 unlink: 2563 unlink:
2564 mutex_lock(&module_mutex);
2393 /* Unlink carefully: kallsyms could be walking list. */ 2565 /* Unlink carefully: kallsyms could be walking list. */
2394 list_del_rcu(&mod->list); 2566 list_del_rcu(&mod->list);
2567 ddebug:
2568 dynamic_debug_remove(debug);
2569 unlock:
2570 mutex_unlock(&module_mutex);
2395 synchronize_sched(); 2571 synchronize_sched();
2396 module_arch_cleanup(mod); 2572 module_arch_cleanup(mod);
2397 cleanup: 2573 cleanup:
2398 free_modinfo(mod); 2574 free_modinfo(mod);
2399 kobject_del(&mod->mkobj.kobj);
2400 kobject_put(&mod->mkobj.kobj);
2401 free_unload:
2402 module_unload_free(mod); 2575 module_unload_free(mod);
2403#if defined(CONFIG_MODULE_UNLOAD) 2576#if defined(CONFIG_MODULE_UNLOAD)
2404 free_percpu(mod->refptr); 2577 free_percpu(mod->refptr);
@@ -2409,8 +2582,7 @@ static noinline struct module *load_module(void __user *umod,
2409 module_free(mod, mod->module_core); 2582 module_free(mod, mod->module_core);
2410 /* mod will be freed with core. Don't access it beyond this line! */ 2583 /* mod will be freed with core. Don't access it beyond this line! */
2411 free_percpu: 2584 free_percpu:
2412 if (percpu) 2585 free_percpu(percpu);
2413 percpu_modfree(percpu);
2414 free_mod: 2586 free_mod:
2415 kfree(args); 2587 kfree(args);
2416 kfree(strmap); 2588 kfree(strmap);
@@ -2446,19 +2618,10 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
2446 if (!capable(CAP_SYS_MODULE) || modules_disabled) 2618 if (!capable(CAP_SYS_MODULE) || modules_disabled)
2447 return -EPERM; 2619 return -EPERM;
2448 2620
2449 /* Only one module load at a time, please */
2450 if (mutex_lock_interruptible(&module_mutex) != 0)
2451 return -EINTR;
2452
2453 /* Do all the hard work */ 2621 /* Do all the hard work */
2454 mod = load_module(umod, len, uargs); 2622 mod = load_module(umod, len, uargs);
2455 if (IS_ERR(mod)) { 2623 if (IS_ERR(mod))
2456 mutex_unlock(&module_mutex);
2457 return PTR_ERR(mod); 2624 return PTR_ERR(mod);
2458 }
2459
2460 /* Drop lock so they can recurse */
2461 mutex_unlock(&module_mutex);
2462 2625
2463 blocking_notifier_call_chain(&module_notify_list, 2626 blocking_notifier_call_chain(&module_notify_list,
2464 MODULE_STATE_COMING, mod); 2627 MODULE_STATE_COMING, mod);
@@ -2475,9 +2638,7 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
2475 module_put(mod); 2638 module_put(mod);
2476 blocking_notifier_call_chain(&module_notify_list, 2639 blocking_notifier_call_chain(&module_notify_list,
2477 MODULE_STATE_GOING, mod); 2640 MODULE_STATE_GOING, mod);
2478 mutex_lock(&module_mutex);
2479 free_module(mod); 2641 free_module(mod);
2480 mutex_unlock(&module_mutex);
2481 wake_up(&module_wq); 2642 wake_up(&module_wq);
2482 return ret; 2643 return ret;
2483 } 2644 }