aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/module.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/module.c')
-rw-r--r--kernel/module.c537
1 files changed, 351 insertions, 186 deletions
diff --git a/kernel/module.c b/kernel/module.c
index 5daf0abd63c1..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,27 +375,33 @@ 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,
@@ -400,24 +411,62 @@ static unsigned int find_pcpusec(Elf_Ehdr *hdr,
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,34 +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 local_set(__module_ref_addr(mod, cpu), 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 }
537
478 /* Hold reference count during initialization. */ 538 /* Hold reference count during initialization. */
479 local_set(__module_ref_addr(mod, raw_smp_processor_id()), 1); 539 __this_cpu_write(mod->refptr->incs, 1);
480 /* Backwards compatibility macros put refcount during init. */ 540 /* Backwards compatibility macros put refcount during init. */
481 mod->waiter = current; 541 mod->waiter = current;
482} 542}
483 543
484/* modules using other modules */
485struct module_use
486{
487 struct list_head list;
488 struct module *module_which_uses;
489};
490
491/* Does a already use b? */ 544/* Does a already use b? */
492static int already_uses(struct module *a, struct module *b) 545static int already_uses(struct module *a, struct module *b)
493{ 546{
494 struct module_use *use; 547 struct module_use *use;
495 548
496 list_for_each_entry(use, &b->modules_which_use_me, list) { 549 list_for_each_entry(use, &b->source_list, source_list) {
497 if (use->module_which_uses == a) { 550 if (use->source == a) {
498 DEBUGP("%s uses %s!\n", a->name, b->name); 551 DEBUGP("%s uses %s!\n", a->name, b->name);
499 return 1; 552 return 1;
500 } 553 }
@@ -503,62 +556,68 @@ static int already_uses(struct module *a, struct module *b)
503 return 0; 556 return 0;
504} 557}
505 558
506/* Module a uses b */ 559/*
507int 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)
508{ 567{
509 struct module_use *use; 568 struct module_use *use;
510 int no_warn, err;
511 569
512 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 }
513 576
514 /* If we're interrupted or time out, we fail. */ 577 use->source = a;
515 if (wait_event_interruptible_timeout( 578 use->target = b;
516 module_wq, (err = strong_try_module_get(b)) != -EBUSY, 579 list_add(&use->source_list, &b->source_list);
517 30 * HZ) <= 0) { 580 list_add(&use->target_list, &a->target_list);
518 printk("%s: gave up waiting for init of module %s.\n", 581 return 0;
519 a->name, b->name); 582}
583
584/* Module a uses b: caller needs module_mutex() */
585int ref_module(struct module *a, struct module *b)
586{
587 int err;
588
589 if (b == NULL || already_uses(a, b))
520 return 0; 590 return 0;
521 }
522 591
523 /* 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);
524 if (err) 594 if (err)
525 return 0; 595 return err;
526 596
527 DEBUGP("Allocating new usage for %s.\n", a->name); 597 err = add_module_usage(a, b);
528 use = kmalloc(sizeof(*use), GFP_ATOMIC); 598 if (err) {
529 if (!use) {
530 printk("%s: out of memory loading\n", a->name);
531 module_put(b); 599 module_put(b);
532 return 0; 600 return err;
533 } 601 }
534 602 return 0;
535 use->module_which_uses = a;
536 list_add(&use->list, &b->modules_which_use_me);
537 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
538 return 1;
539} 603}
540EXPORT_SYMBOL_GPL(use_module); 604EXPORT_SYMBOL_GPL(ref_module);
541 605
542/* Clear the unload stuff of the module. */ 606/* Clear the unload stuff of the module. */
543static void module_unload_free(struct module *mod) 607static void module_unload_free(struct module *mod)
544{ 608{
545 struct module *i; 609 struct module_use *use, *tmp;
546
547 list_for_each_entry(i, &modules, list) {
548 struct module_use *use;
549 610
550 list_for_each_entry(use, &i->modules_which_use_me, list) { 611 mutex_lock(&module_mutex);
551 if (use->module_which_uses == mod) { 612 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
552 DEBUGP("%s unusing %s\n", mod->name, i->name); 613 struct module *i = use->target;
553 module_put(i); 614 DEBUGP("%s unusing %s\n", mod->name, i->name);
554 list_del(&use->list); 615 module_put(i);
555 kfree(use); 616 list_del(&use->source_list);
556 sysfs_remove_link(i->holders_dir, mod->name); 617 list_del(&use->target_list);
557 /* There can be at most one match. */ 618 kfree(use);
558 break;
559 }
560 }
561 } 619 }
620 mutex_unlock(&module_mutex);
562} 621}
563 622
564#ifdef CONFIG_MODULE_FORCE_UNLOAD 623#ifdef CONFIG_MODULE_FORCE_UNLOAD
@@ -615,12 +674,28 @@ static int try_stop_module(struct module *mod, int flags, int *forced)
615 674
616unsigned int module_refcount(struct module *mod) 675unsigned int module_refcount(struct module *mod)
617{ 676{
618 unsigned int total = 0; 677 unsigned int incs = 0, decs = 0;
619 int cpu; 678 int cpu;
620 679
621 for_each_possible_cpu(cpu) 680 for_each_possible_cpu(cpu)
622 total += local_read(__module_ref_addr(mod, cpu)); 681 decs += per_cpu_ptr(mod->refptr, cpu)->decs;
623 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;
624} 699}
625EXPORT_SYMBOL(module_refcount); 700EXPORT_SYMBOL(module_refcount);
626 701
@@ -656,16 +731,8 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
656 return -EFAULT; 731 return -EFAULT;
657 name[MODULE_NAME_LEN-1] = '\0'; 732 name[MODULE_NAME_LEN-1] = '\0';
658 733
659 /* Create stop_machine threads since free_module relies on 734 if (mutex_lock_interruptible(&module_mutex) != 0)
660 * a non-failing stop_machine call. */ 735 return -EINTR;
661 ret = stop_machine_create();
662 if (ret)
663 return ret;
664
665 if (mutex_lock_interruptible(&module_mutex) != 0) {
666 ret = -EINTR;
667 goto out_stop;
668 }
669 736
670 mod = find_module(name); 737 mod = find_module(name);
671 if (!mod) { 738 if (!mod) {
@@ -673,7 +740,7 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
673 goto out; 740 goto out;
674 } 741 }
675 742
676 if (!list_empty(&mod->modules_which_use_me)) { 743 if (!list_empty(&mod->source_list)) {
677 /* Other modules depend on us: get rid of them first. */ 744 /* Other modules depend on us: get rid of them first. */
678 ret = -EWOULDBLOCK; 745 ret = -EWOULDBLOCK;
679 goto out; 746 goto out;
@@ -717,16 +784,14 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
717 blocking_notifier_call_chain(&module_notify_list, 784 blocking_notifier_call_chain(&module_notify_list,
718 MODULE_STATE_GOING, mod); 785 MODULE_STATE_GOING, mod);
719 async_synchronize_full(); 786 async_synchronize_full();
720 mutex_lock(&module_mutex); 787
721 /* Store the name of the last unloaded module for diagnostic purposes */ 788 /* Store the name of the last unloaded module for diagnostic purposes */
722 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module)); 789 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
723 ddebug_remove_module(mod->name);
724 free_module(mod);
725 790
726 out: 791 free_module(mod);
792 return 0;
793out:
727 mutex_unlock(&module_mutex); 794 mutex_unlock(&module_mutex);
728out_stop:
729 stop_machine_destroy();
730 return ret; 795 return ret;
731} 796}
732 797
@@ -739,9 +804,9 @@ static inline void print_unload_info(struct seq_file *m, struct module *mod)
739 804
740 /* Always include a trailing , so userspace can differentiate 805 /* Always include a trailing , so userspace can differentiate
741 between this and the old multi-field proc format. */ 806 between this and the old multi-field proc format. */
742 list_for_each_entry(use, &mod->modules_which_use_me, list) { 807 list_for_each_entry(use, &mod->source_list, source_list) {
743 printed_something = 1; 808 printed_something = 1;
744 seq_printf(m, "%s,", use->module_which_uses->name); 809 seq_printf(m, "%s,", use->source->name);
745 } 810 }
746 811
747 if (mod->init != NULL && mod->exit == NULL) { 812 if (mod->init != NULL && mod->exit == NULL) {
@@ -796,14 +861,15 @@ static struct module_attribute refcnt = {
796void module_put(struct module *module) 861void module_put(struct module *module)
797{ 862{
798 if (module) { 863 if (module) {
799 unsigned int cpu = get_cpu(); 864 preempt_disable();
800 local_dec(__module_ref_addr(module, cpu)); 865 smp_wmb(); /* see comment in module_refcount */
801 trace_module_put(module, _RET_IP_, 866 __this_cpu_inc(module->refptr->decs);
802 local_read(__module_ref_addr(module, cpu))); 867
868 trace_module_put(module, _RET_IP_);
803 /* Maybe they're waiting for us to drop reference? */ 869 /* Maybe they're waiting for us to drop reference? */
804 if (unlikely(!module_is_live(module))) 870 if (unlikely(!module_is_live(module)))
805 wake_up_process(module->waiter); 871 wake_up_process(module->waiter);
806 put_cpu(); 872 preempt_enable();
807 } 873 }
808} 874}
809EXPORT_SYMBOL(module_put); 875EXPORT_SYMBOL(module_put);
@@ -819,11 +885,11 @@ static inline void module_unload_free(struct module *mod)
819{ 885{
820} 886}
821 887
822int use_module(struct module *a, struct module *b) 888int ref_module(struct module *a, struct module *b)
823{ 889{
824 return strong_try_module_get(b) == 0; 890 return strong_try_module_get(b);
825} 891}
826EXPORT_SYMBOL_GPL(use_module); 892EXPORT_SYMBOL_GPL(ref_module);
827 893
828static inline void module_unload_init(struct module *mod) 894static inline void module_unload_init(struct module *mod)
829{ 895{
@@ -940,6 +1006,8 @@ static inline int check_modstruct_version(Elf_Shdr *sechdrs,
940{ 1006{
941 const unsigned long *crc; 1007 const unsigned long *crc;
942 1008
1009 /* Since this should be found in kernel (which can't be removed),
1010 * no locking is necessary. */
943 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL, 1011 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
944 &crc, true, false)) 1012 &crc, true, false))
945 BUG(); 1013 BUG();
@@ -982,29 +1050,62 @@ static inline int same_magic(const char *amagic, const char *bmagic,
982} 1050}
983#endif /* CONFIG_MODVERSIONS */ 1051#endif /* CONFIG_MODVERSIONS */
984 1052
985/* 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. */
986 Must be holding module_mutex. */
987static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs, 1054static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
988 unsigned int versindex, 1055 unsigned int versindex,
989 const char *name, 1056 const char *name,
990 struct module *mod) 1057 struct module *mod,
1058 char ownername[])
991{ 1059{
992 struct module *owner; 1060 struct module *owner;
993 const struct kernel_symbol *sym; 1061 const struct kernel_symbol *sym;
994 const unsigned long *crc; 1062 const unsigned long *crc;
1063 int err;
995 1064
1065 mutex_lock(&module_mutex);
996 sym = find_symbol(name, &owner, &crc, 1066 sym = find_symbol(name, &owner, &crc,
997 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true); 1067 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
998 /* use_module can fail due to OOM, 1068 if (!sym)
999 or module initialization or unloading */ 1069 goto unlock;
1000 if (sym) { 1070
1001 if (!check_version(sechdrs, versindex, name, mod, crc, owner) 1071 if (!check_version(sechdrs, versindex, name, mod, crc, owner)) {
1002 || !use_module(mod, owner)) 1072 sym = ERR_PTR(-EINVAL);
1003 sym = NULL; 1073 goto getname;
1074 }
1075
1076 err = ref_module(mod, owner);
1077 if (err) {
1078 sym = ERR_PTR(err);
1079 goto getname;
1004 } 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);
1005 return sym; 1087 return sym;
1006} 1088}
1007 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
1008/* 1109/*
1009 * /sys/module/foo/sections stuff 1110 * /sys/module/foo/sections stuff
1010 * J. Corbet <corbet@lwn.net> 1111 * J. Corbet <corbet@lwn.net>
@@ -1083,6 +1184,7 @@ static void add_sect_attrs(struct module *mod, unsigned int nsect,
1083 if (sattr->name == NULL) 1184 if (sattr->name == NULL)
1084 goto out; 1185 goto out;
1085 sect_attrs->nsections++; 1186 sect_attrs->nsections++;
1187 sysfs_attr_init(&sattr->mattr.attr);
1086 sattr->mattr.show = module_sect_show; 1188 sattr->mattr.show = module_sect_show;
1087 sattr->mattr.store = NULL; 1189 sattr->mattr.store = NULL;
1088 sattr->mattr.attr.name = sattr->name; 1190 sattr->mattr.attr.name = sattr->name;
@@ -1122,7 +1224,7 @@ struct module_notes_attrs {
1122 struct bin_attribute attrs[0]; 1224 struct bin_attribute attrs[0];
1123}; 1225};
1124 1226
1125static ssize_t module_notes_read(struct kobject *kobj, 1227static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
1126 struct bin_attribute *bin_attr, 1228 struct bin_attribute *bin_attr,
1127 char *buf, loff_t pos, size_t count) 1229 char *buf, loff_t pos, size_t count)
1128{ 1230{
@@ -1178,6 +1280,7 @@ static void add_notes_attrs(struct module *mod, unsigned int nsect,
1178 if (sect_empty(&sechdrs[i])) 1280 if (sect_empty(&sechdrs[i]))
1179 continue; 1281 continue;
1180 if (sechdrs[i].sh_type == SHT_NOTE) { 1282 if (sechdrs[i].sh_type == SHT_NOTE) {
1283 sysfs_bin_attr_init(nattr);
1181 nattr->attr.name = mod->sect_attrs->attrs[loaded].name; 1284 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1182 nattr->attr.mode = S_IRUGO; 1285 nattr->attr.mode = S_IRUGO;
1183 nattr->size = sechdrs[i].sh_size; 1286 nattr->size = sechdrs[i].sh_size;
@@ -1232,7 +1335,34 @@ static inline void remove_notes_attrs(struct module *mod)
1232#endif 1335#endif
1233 1336
1234#ifdef CONFIG_SYSFS 1337#ifdef CONFIG_SYSFS
1235int 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)
1236{ 1366{
1237 struct module_attribute *attr; 1367 struct module_attribute *attr;
1238 struct module_attribute *temp_attr; 1368 struct module_attribute *temp_attr;
@@ -1250,6 +1380,7 @@ int module_add_modinfo_attrs(struct module *mod)
1250 if (!attr->test || 1380 if (!attr->test ||
1251 (attr->test && attr->test(mod))) { 1381 (attr->test && attr->test(mod))) {
1252 memcpy(temp_attr, attr, sizeof(*temp_attr)); 1382 memcpy(temp_attr, attr, sizeof(*temp_attr));
1383 sysfs_attr_init(&temp_attr->attr);
1253 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr); 1384 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1254 ++temp_attr; 1385 ++temp_attr;
1255 } 1386 }
@@ -1257,7 +1388,7 @@ int module_add_modinfo_attrs(struct module *mod)
1257 return error; 1388 return error;
1258} 1389}
1259 1390
1260void module_remove_modinfo_attrs(struct module *mod) 1391static void module_remove_modinfo_attrs(struct module *mod)
1261{ 1392{
1262 struct module_attribute *attr; 1393 struct module_attribute *attr;
1263 int i; 1394 int i;
@@ -1273,7 +1404,7 @@ void module_remove_modinfo_attrs(struct module *mod)
1273 kfree(mod->modinfo_attrs); 1404 kfree(mod->modinfo_attrs);
1274} 1405}
1275 1406
1276int mod_sysfs_init(struct module *mod) 1407static int mod_sysfs_init(struct module *mod)
1277{ 1408{
1278 int err; 1409 int err;
1279 struct kobject *kobj; 1410 struct kobject *kobj;
@@ -1307,12 +1438,16 @@ out:
1307 return err; 1438 return err;
1308} 1439}
1309 1440
1310int mod_sysfs_setup(struct module *mod, 1441static int mod_sysfs_setup(struct module *mod,
1311 struct kernel_param *kparam, 1442 struct kernel_param *kparam,
1312 unsigned int num_params) 1443 unsigned int num_params)
1313{ 1444{
1314 int err; 1445 int err;
1315 1446
1447 err = mod_sysfs_init(mod);
1448 if (err)
1449 goto out;
1450
1316 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj); 1451 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1317 if (!mod->holders_dir) { 1452 if (!mod->holders_dir) {
1318 err = -ENOMEM; 1453 err = -ENOMEM;
@@ -1327,6 +1462,8 @@ int mod_sysfs_setup(struct module *mod,
1327 if (err) 1462 if (err)
1328 goto out_unreg_param; 1463 goto out_unreg_param;
1329 1464
1465 add_usage_links(mod);
1466
1330 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD); 1467 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1331 return 0; 1468 return 0;
1332 1469
@@ -1336,6 +1473,7 @@ out_unreg_holders:
1336 kobject_put(mod->holders_dir); 1473 kobject_put(mod->holders_dir);
1337out_unreg: 1474out_unreg:
1338 kobject_put(&mod->mkobj.kobj); 1475 kobject_put(&mod->mkobj.kobj);
1476out:
1339 return err; 1477 return err;
1340} 1478}
1341 1479
@@ -1346,14 +1484,40 @@ static void mod_sysfs_fini(struct module *mod)
1346 1484
1347#else /* CONFIG_SYSFS */ 1485#else /* CONFIG_SYSFS */
1348 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
1349static void mod_sysfs_fini(struct module *mod) 1508static void mod_sysfs_fini(struct module *mod)
1350{ 1509{
1351} 1510}
1352 1511
1512static void del_usage_links(struct module *mod)
1513{
1514}
1515
1353#endif /* CONFIG_SYSFS */ 1516#endif /* CONFIG_SYSFS */
1354 1517
1355static void mod_kobject_remove(struct module *mod) 1518static void mod_kobject_remove(struct module *mod)
1356{ 1519{
1520 del_usage_links(mod);
1357 module_remove_modinfo_attrs(mod); 1521 module_remove_modinfo_attrs(mod);
1358 module_param_sysfs_remove(mod); 1522 module_param_sysfs_remove(mod);
1359 kobject_put(mod->mkobj.drivers_dir); 1523 kobject_put(mod->mkobj.drivers_dir);
@@ -1372,17 +1536,22 @@ static int __unlink_module(void *_mod)
1372 return 0; 1536 return 0;
1373} 1537}
1374 1538
1375/* Free a module, remove from lists, etc (must hold module_mutex). */ 1539/* Free a module, remove from lists, etc. */
1376static void free_module(struct module *mod) 1540static void free_module(struct module *mod)
1377{ 1541{
1378 trace_module_free(mod); 1542 trace_module_free(mod);
1379 1543
1380 /* Delete from various lists */ 1544 /* Delete from various lists */
1545 mutex_lock(&module_mutex);
1381 stop_machine(__unlink_module, mod, NULL); 1546 stop_machine(__unlink_module, mod, NULL);
1547 mutex_unlock(&module_mutex);
1382 remove_notes_attrs(mod); 1548 remove_notes_attrs(mod);
1383 remove_sect_attrs(mod); 1549 remove_sect_attrs(mod);
1384 mod_kobject_remove(mod); 1550 mod_kobject_remove(mod);
1385 1551
1552 /* Remove dynamic debug info */
1553 ddebug_remove_module(mod->name);
1554
1386 /* Arch-specific cleanup. */ 1555 /* Arch-specific cleanup. */
1387 module_arch_cleanup(mod); 1556 module_arch_cleanup(mod);
1388 1557
@@ -1395,11 +1564,10 @@ static void free_module(struct module *mod)
1395 /* This may be NULL, but that's OK */ 1564 /* This may be NULL, but that's OK */
1396 module_free(mod, mod->module_init); 1565 module_free(mod, mod->module_init);
1397 kfree(mod->args); 1566 kfree(mod->args);
1398 if (mod->percpu) 1567 percpu_modfree(mod);
1399 percpu_modfree(mod->percpu); 1568#if defined(CONFIG_MODULE_UNLOAD)
1400#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
1401 if (mod->refptr) 1569 if (mod->refptr)
1402 percpu_modfree(mod->refptr); 1570 free_percpu(mod->refptr);
1403#endif 1571#endif
1404 /* Free lock-classes: */ 1572 /* Free lock-classes: */
1405 lockdep_free_key_range(mod->module_core, mod->core_size); 1573 lockdep_free_key_range(mod->module_core, mod->core_size);
@@ -1430,6 +1598,8 @@ EXPORT_SYMBOL_GPL(__symbol_get);
1430/* 1598/*
1431 * Ensure that an exported symbol [global namespace] does not already exist 1599 * Ensure that an exported symbol [global namespace] does not already exist
1432 * 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.
1433 */ 1603 */
1434static int verify_export_symbols(struct module *mod) 1604static int verify_export_symbols(struct module *mod)
1435{ 1605{
@@ -1495,27 +1665,29 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
1495 break; 1665 break;
1496 1666
1497 case SHN_UNDEF: 1667 case SHN_UNDEF:
1498 ksym = resolve_symbol(sechdrs, versindex, 1668 ksym = resolve_symbol_wait(sechdrs, versindex,
1499 strtab + sym[i].st_name, mod); 1669 strtab + sym[i].st_name,
1670 mod);
1500 /* Ok if resolved. */ 1671 /* Ok if resolved. */
1501 if (ksym) { 1672 if (ksym && !IS_ERR(ksym)) {
1502 sym[i].st_value = ksym->value; 1673 sym[i].st_value = ksym->value;
1503 break; 1674 break;
1504 } 1675 }
1505 1676
1506 /* Ok if weak. */ 1677 /* Ok if weak. */
1507 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK) 1678 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1508 break; 1679 break;
1509 1680
1510 printk(KERN_WARNING "%s: Unknown symbol %s\n", 1681 printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
1511 mod->name, strtab + sym[i].st_name); 1682 mod->name, strtab + sym[i].st_name,
1512 ret = -ENOENT; 1683 PTR_ERR(ksym));
1684 ret = PTR_ERR(ksym) ?: -ENOENT;
1513 break; 1685 break;
1514 1686
1515 default: 1687 default:
1516 /* Divert to percpu allocation if a percpu var. */ 1688 /* Divert to percpu allocation if a percpu var. */
1517 if (sym[i].st_shndx == pcpuindex) 1689 if (sym[i].st_shndx == pcpuindex)
1518 secbase = (unsigned long)mod->percpu; 1690 secbase = (unsigned long)mod_percpu(mod);
1519 else 1691 else
1520 secbase = sechdrs[sym[i].st_shndx].sh_addr; 1692 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1521 sym[i].st_value += secbase; 1693 sym[i].st_value += secbase;
@@ -1892,16 +2064,24 @@ static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
1892#endif 2064#endif
1893} 2065}
1894 2066
2067static void dynamic_debug_remove(struct _ddebug *debug)
2068{
2069 if (debug)
2070 ddebug_remove_module(debug->modname);
2071}
2072
1895static void *module_alloc_update_bounds(unsigned long size) 2073static void *module_alloc_update_bounds(unsigned long size)
1896{ 2074{
1897 void *ret = module_alloc(size); 2075 void *ret = module_alloc(size);
1898 2076
1899 if (ret) { 2077 if (ret) {
2078 mutex_lock(&module_mutex);
1900 /* Update module bounds. */ 2079 /* Update module bounds. */
1901 if ((unsigned long)ret < module_addr_min) 2080 if ((unsigned long)ret < module_addr_min)
1902 module_addr_min = (unsigned long)ret; 2081 module_addr_min = (unsigned long)ret;
1903 if ((unsigned long)ret + size > module_addr_max) 2082 if ((unsigned long)ret + size > module_addr_max)
1904 module_addr_max = (unsigned long)ret + size; 2083 module_addr_max = (unsigned long)ret + size;
2084 mutex_unlock(&module_mutex);
1905 } 2085 }
1906 return ret; 2086 return ret;
1907} 2087}
@@ -1949,8 +2129,11 @@ static noinline struct module *load_module(void __user *umod,
1949 unsigned int modindex, versindex, infoindex, pcpuindex; 2129 unsigned int modindex, versindex, infoindex, pcpuindex;
1950 struct module *mod; 2130 struct module *mod;
1951 long err = 0; 2131 long err = 0;
1952 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */ 2132 void *ptr = NULL; /* Stops spurious gcc warning */
1953 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;
1954 2137
1955 mm_segment_t old_fs; 2138 mm_segment_t old_fs;
1956 2139
@@ -2075,11 +2258,6 @@ static noinline struct module *load_module(void __user *umod,
2075 goto free_mod; 2258 goto free_mod;
2076 } 2259 }
2077 2260
2078 if (find_module(mod->name)) {
2079 err = -EEXIST;
2080 goto free_mod;
2081 }
2082
2083 mod->state = MODULE_STATE_COMING; 2261 mod->state = MODULE_STATE_COMING;
2084 2262
2085 /* Allow arches to frob section contents and sizes. */ 2263 /* Allow arches to frob section contents and sizes. */
@@ -2089,16 +2267,14 @@ static noinline struct module *load_module(void __user *umod,
2089 2267
2090 if (pcpuindex) { 2268 if (pcpuindex) {
2091 /* We have a special allocation for this section. */ 2269 /* We have a special allocation for this section. */
2092 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size, 2270 err = percpu_modalloc(mod, sechdrs[pcpuindex].sh_size,
2093 sechdrs[pcpuindex].sh_addralign, 2271 sechdrs[pcpuindex].sh_addralign);
2094 mod->name); 2272 if (err)
2095 if (!percpu) {
2096 err = -ENOMEM;
2097 goto free_mod; 2273 goto free_mod;
2098 }
2099 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC; 2274 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2100 mod->percpu = percpu;
2101 } 2275 }
2276 /* Keep this around for failure path. */
2277 percpu = mod_percpu(mod);
2102 2278
2103 /* Determine total sizes, and put offsets in sh_entsize. For now 2279 /* Determine total sizes, and put offsets in sh_entsize. For now
2104 this is done generically; there doesn't appear to be any 2280 this is done generically; there doesn't appear to be any
@@ -2162,9 +2338,8 @@ static noinline struct module *load_module(void __user *umod,
2162 mod = (void *)sechdrs[modindex].sh_addr; 2338 mod = (void *)sechdrs[modindex].sh_addr;
2163 kmemleak_load_module(mod, hdr, sechdrs, secstrings); 2339 kmemleak_load_module(mod, hdr, sechdrs, secstrings);
2164 2340
2165#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP) 2341#if defined(CONFIG_MODULE_UNLOAD)
2166 mod->refptr = percpu_modalloc(sizeof(local_t), __alignof__(local_t), 2342 mod->refptr = alloc_percpu(struct module_ref);
2167 mod->name);
2168 if (!mod->refptr) { 2343 if (!mod->refptr) {
2169 err = -ENOMEM; 2344 err = -ENOMEM;
2170 goto free_init; 2345 goto free_init;
@@ -2173,11 +2348,6 @@ static noinline struct module *load_module(void __user *umod,
2173 /* Now we've moved module, initialize linked lists, etc. */ 2348 /* Now we've moved module, initialize linked lists, etc. */
2174 module_unload_init(mod); 2349 module_unload_init(mod);
2175 2350
2176 /* add kobject, so we can reference it. */
2177 err = mod_sysfs_init(mod);
2178 if (err)
2179 goto free_unload;
2180
2181 /* Set up license info based on the info section */ 2351 /* Set up license info based on the info section */
2182 set_license(mod, get_modinfo(sechdrs, infoindex, "license")); 2352 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2183 2353
@@ -2302,18 +2472,13 @@ static noinline struct module *load_module(void __user *umod,
2302 goto cleanup; 2472 goto cleanup;
2303 } 2473 }
2304 2474
2305 /* Find duplicate symbols */
2306 err = verify_export_symbols(mod);
2307 if (err < 0)
2308 goto cleanup;
2309
2310 /* Set up and sort exception table */ 2475 /* Set up and sort exception table */
2311 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table", 2476 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2312 sizeof(*mod->extable), &mod->num_exentries); 2477 sizeof(*mod->extable), &mod->num_exentries);
2313 sort_extable(mod->extable, mod->extable + mod->num_exentries); 2478 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2314 2479
2315 /* Finally, copy percpu area over. */ 2480 /* Finally, copy percpu area over. */
2316 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr, 2481 percpu_modcopy(mod, (void *)sechdrs[pcpuindex].sh_addr,
2317 sechdrs[pcpuindex].sh_size); 2482 sechdrs[pcpuindex].sh_size);
2318 2483
2319 add_kallsyms(mod, sechdrs, hdr->e_shnum, symindex, strindex, 2484 add_kallsyms(mod, sechdrs, hdr->e_shnum, symindex, strindex,
@@ -2321,15 +2486,9 @@ static noinline struct module *load_module(void __user *umod,
2321 kfree(strmap); 2486 kfree(strmap);
2322 strmap = NULL; 2487 strmap = NULL;
2323 2488
2324 if (!mod->taints) { 2489 if (!mod->taints)
2325 struct _ddebug *debug;
2326 unsigned int num_debug;
2327
2328 debug = section_objs(hdr, sechdrs, secstrings, "__verbose", 2490 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2329 sizeof(*debug), &num_debug); 2491 sizeof(*debug), &num_debug);
2330 if (debug)
2331 dynamic_debug_setup(debug, num_debug);
2332 }
2333 2492
2334 err = module_finalize(hdr, sechdrs, mod); 2493 err = module_finalize(hdr, sechdrs, mod);
2335 if (err < 0) 2494 if (err < 0)
@@ -2365,7 +2524,22 @@ static noinline struct module *load_module(void __user *umod,
2365 * function to insert in a way safe to concurrent readers. 2524 * function to insert in a way safe to concurrent readers.
2366 * The mutex protects against concurrent writers. 2525 * The mutex protects against concurrent writers.
2367 */ 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
2368 list_add_rcu(&mod->list, &modules); 2541 list_add_rcu(&mod->list, &modules);
2542 mutex_unlock(&module_mutex);
2369 2543
2370 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);
2371 if (err < 0) 2545 if (err < 0)
@@ -2374,6 +2548,7 @@ static noinline struct module *load_module(void __user *umod,
2374 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp); 2548 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
2375 if (err < 0) 2549 if (err < 0)
2376 goto unlink; 2550 goto unlink;
2551
2377 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs); 2552 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2378 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs); 2553 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2379 2554
@@ -2386,18 +2561,20 @@ static noinline struct module *load_module(void __user *umod,
2386 return mod; 2561 return mod;
2387 2562
2388 unlink: 2563 unlink:
2564 mutex_lock(&module_mutex);
2389 /* Unlink carefully: kallsyms could be walking list. */ 2565 /* Unlink carefully: kallsyms could be walking list. */
2390 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);
2391 synchronize_sched(); 2571 synchronize_sched();
2392 module_arch_cleanup(mod); 2572 module_arch_cleanup(mod);
2393 cleanup: 2573 cleanup:
2394 free_modinfo(mod); 2574 free_modinfo(mod);
2395 kobject_del(&mod->mkobj.kobj);
2396 kobject_put(&mod->mkobj.kobj);
2397 free_unload:
2398 module_unload_free(mod); 2575 module_unload_free(mod);
2399#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP) 2576#if defined(CONFIG_MODULE_UNLOAD)
2400 percpu_modfree(mod->refptr); 2577 free_percpu(mod->refptr);
2401 free_init: 2578 free_init:
2402#endif 2579#endif
2403 module_free(mod, mod->module_init); 2580 module_free(mod, mod->module_init);
@@ -2405,8 +2582,7 @@ static noinline struct module *load_module(void __user *umod,
2405 module_free(mod, mod->module_core); 2582 module_free(mod, mod->module_core);
2406 /* 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! */
2407 free_percpu: 2584 free_percpu:
2408 if (percpu) 2585 free_percpu(percpu);
2409 percpu_modfree(percpu);
2410 free_mod: 2586 free_mod:
2411 kfree(args); 2587 kfree(args);
2412 kfree(strmap); 2588 kfree(strmap);
@@ -2442,19 +2618,10 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
2442 if (!capable(CAP_SYS_MODULE) || modules_disabled) 2618 if (!capable(CAP_SYS_MODULE) || modules_disabled)
2443 return -EPERM; 2619 return -EPERM;
2444 2620
2445 /* Only one module load at a time, please */
2446 if (mutex_lock_interruptible(&module_mutex) != 0)
2447 return -EINTR;
2448
2449 /* Do all the hard work */ 2621 /* Do all the hard work */
2450 mod = load_module(umod, len, uargs); 2622 mod = load_module(umod, len, uargs);
2451 if (IS_ERR(mod)) { 2623 if (IS_ERR(mod))
2452 mutex_unlock(&module_mutex);
2453 return PTR_ERR(mod); 2624 return PTR_ERR(mod);
2454 }
2455
2456 /* Drop lock so they can recurse */
2457 mutex_unlock(&module_mutex);
2458 2625
2459 blocking_notifier_call_chain(&module_notify_list, 2626 blocking_notifier_call_chain(&module_notify_list,
2460 MODULE_STATE_COMING, mod); 2627 MODULE_STATE_COMING, mod);
@@ -2471,9 +2638,7 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
2471 module_put(mod); 2638 module_put(mod);
2472 blocking_notifier_call_chain(&module_notify_list, 2639 blocking_notifier_call_chain(&module_notify_list,
2473 MODULE_STATE_GOING, mod); 2640 MODULE_STATE_GOING, mod);
2474 mutex_lock(&module_mutex);
2475 free_module(mod); 2641 free_module(mod);
2476 mutex_unlock(&module_mutex);
2477 wake_up(&module_wq); 2642 wake_up(&module_wq);
2478 return ret; 2643 return ret;
2479 } 2644 }