aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/module.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/module.c')
-rw-r--r--kernel/module.c222
1 files changed, 146 insertions, 76 deletions
diff --git a/kernel/module.c b/kernel/module.c
index c968d3606dca..a8014bfb5a4e 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
@@ -79,6 +77,10 @@ EXPORT_TRACEPOINT_SYMBOL(module_get);
79DEFINE_MUTEX(module_mutex); 77DEFINE_MUTEX(module_mutex);
80EXPORT_SYMBOL_GPL(module_mutex); 78EXPORT_SYMBOL_GPL(module_mutex);
81static LIST_HEAD(modules); 79static LIST_HEAD(modules);
80#ifdef CONFIG_KGDB_KDB
81struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
82#endif /* CONFIG_KGDB_KDB */
83
82 84
83/* Block module loading/unloading? */ 85/* Block module loading/unloading? */
84int modules_disabled = 0; 86int modules_disabled = 0;
@@ -370,27 +372,33 @@ EXPORT_SYMBOL_GPL(find_module);
370 372
371#ifdef CONFIG_SMP 373#ifdef CONFIG_SMP
372 374
373static void *percpu_modalloc(unsigned long size, unsigned long align, 375static inline void __percpu *mod_percpu(struct module *mod)
374 const char *name)
375{ 376{
376 void *ptr; 377 return mod->percpu;
378}
377 379
380static int percpu_modalloc(struct module *mod,
381 unsigned long size, unsigned long align)
382{
378 if (align > PAGE_SIZE) { 383 if (align > PAGE_SIZE) {
379 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n", 384 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
380 name, align, PAGE_SIZE); 385 mod->name, align, PAGE_SIZE);
381 align = PAGE_SIZE; 386 align = PAGE_SIZE;
382 } 387 }
383 388
384 ptr = __alloc_reserved_percpu(size, align); 389 mod->percpu = __alloc_reserved_percpu(size, align);
385 if (!ptr) 390 if (!mod->percpu) {
386 printk(KERN_WARNING 391 printk(KERN_WARNING
387 "Could not allocate %lu bytes percpu data\n", size); 392 "Could not allocate %lu bytes percpu data\n", size);
388 return ptr; 393 return -ENOMEM;
394 }
395 mod->percpu_size = size;
396 return 0;
389} 397}
390 398
391static void percpu_modfree(void *freeme) 399static void percpu_modfree(struct module *mod)
392{ 400{
393 free_percpu(freeme); 401 free_percpu(mod->percpu);
394} 402}
395 403
396static unsigned int find_pcpusec(Elf_Ehdr *hdr, 404static unsigned int find_pcpusec(Elf_Ehdr *hdr,
@@ -400,24 +408,62 @@ static unsigned int find_pcpusec(Elf_Ehdr *hdr,
400 return find_sec(hdr, sechdrs, secstrings, ".data.percpu"); 408 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
401} 409}
402 410
403static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size) 411static void percpu_modcopy(struct module *mod,
412 const void *from, unsigned long size)
404{ 413{
405 int cpu; 414 int cpu;
406 415
407 for_each_possible_cpu(cpu) 416 for_each_possible_cpu(cpu)
408 memcpy(pcpudest + per_cpu_offset(cpu), from, size); 417 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
418}
419
420/**
421 * is_module_percpu_address - test whether address is from module static percpu
422 * @addr: address to test
423 *
424 * Test whether @addr belongs to module static percpu area.
425 *
426 * RETURNS:
427 * %true if @addr is from module static percpu area
428 */
429bool is_module_percpu_address(unsigned long addr)
430{
431 struct module *mod;
432 unsigned int cpu;
433
434 preempt_disable();
435
436 list_for_each_entry_rcu(mod, &modules, list) {
437 if (!mod->percpu_size)
438 continue;
439 for_each_possible_cpu(cpu) {
440 void *start = per_cpu_ptr(mod->percpu, cpu);
441
442 if ((void *)addr >= start &&
443 (void *)addr < start + mod->percpu_size) {
444 preempt_enable();
445 return true;
446 }
447 }
448 }
449
450 preempt_enable();
451 return false;
409} 452}
410 453
411#else /* ... !CONFIG_SMP */ 454#else /* ... !CONFIG_SMP */
412 455
413static inline void *percpu_modalloc(unsigned long size, unsigned long align, 456static inline void __percpu *mod_percpu(struct module *mod)
414 const char *name)
415{ 457{
416 return NULL; 458 return NULL;
417} 459}
418static inline void percpu_modfree(void *pcpuptr) 460static inline int percpu_modalloc(struct module *mod,
461 unsigned long size, unsigned long align)
462{
463 return -ENOMEM;
464}
465static inline void percpu_modfree(struct module *mod)
419{ 466{
420 BUG();
421} 467}
422static inline unsigned int find_pcpusec(Elf_Ehdr *hdr, 468static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
423 Elf_Shdr *sechdrs, 469 Elf_Shdr *sechdrs,
@@ -425,12 +471,16 @@ static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
425{ 471{
426 return 0; 472 return 0;
427} 473}
428static inline void percpu_modcopy(void *pcpudst, const void *src, 474static inline void percpu_modcopy(struct module *mod,
429 unsigned long size) 475 const void *from, unsigned long size)
430{ 476{
431 /* pcpusec should be 0, and size of that section should be 0. */ 477 /* pcpusec should be 0, and size of that section should be 0. */
432 BUG_ON(size != 0); 478 BUG_ON(size != 0);
433} 479}
480bool is_module_percpu_address(unsigned long addr)
481{
482 return false;
483}
434 484
435#endif /* CONFIG_SMP */ 485#endif /* CONFIG_SMP */
436 486
@@ -467,17 +517,22 @@ MODINFO_ATTR(srcversion);
467static char last_unloaded_module[MODULE_NAME_LEN+1]; 517static char last_unloaded_module[MODULE_NAME_LEN+1];
468 518
469#ifdef CONFIG_MODULE_UNLOAD 519#ifdef CONFIG_MODULE_UNLOAD
520
521EXPORT_TRACEPOINT_SYMBOL(module_get);
522
470/* Init the unload section of the module. */ 523/* Init the unload section of the module. */
471static void module_unload_init(struct module *mod) 524static void module_unload_init(struct module *mod)
472{ 525{
473 int cpu; 526 int cpu;
474 527
475 INIT_LIST_HEAD(&mod->modules_which_use_me); 528 INIT_LIST_HEAD(&mod->modules_which_use_me);
476 for_each_possible_cpu(cpu) 529 for_each_possible_cpu(cpu) {
477 per_cpu_ptr(mod->refptr, cpu)->count = 0; 530 per_cpu_ptr(mod->refptr, cpu)->incs = 0;
531 per_cpu_ptr(mod->refptr, cpu)->decs = 0;
532 }
478 533
479 /* Hold reference count during initialization. */ 534 /* Hold reference count during initialization. */
480 __this_cpu_write(mod->refptr->count, 1); 535 __this_cpu_write(mod->refptr->incs, 1);
481 /* Backwards compatibility macros put refcount during init. */ 536 /* Backwards compatibility macros put refcount during init. */
482 mod->waiter = current; 537 mod->waiter = current;
483} 538}
@@ -510,33 +565,26 @@ int use_module(struct module *a, struct module *b)
510 struct module_use *use; 565 struct module_use *use;
511 int no_warn, err; 566 int no_warn, err;
512 567
513 if (b == NULL || already_uses(a, b)) return 1; 568 if (b == NULL || already_uses(a, b))
514
515 /* If we're interrupted or time out, we fail. */
516 if (wait_event_interruptible_timeout(
517 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
518 30 * HZ) <= 0) {
519 printk("%s: gave up waiting for init of module %s.\n",
520 a->name, b->name);
521 return 0; 569 return 0;
522 }
523 570
524 /* If strong_try_module_get() returned a different error, we fail. */ 571 /* If we're interrupted or time out, we fail. */
572 err = strong_try_module_get(b);
525 if (err) 573 if (err)
526 return 0; 574 return err;
527 575
528 DEBUGP("Allocating new usage for %s.\n", a->name); 576 DEBUGP("Allocating new usage for %s.\n", a->name);
529 use = kmalloc(sizeof(*use), GFP_ATOMIC); 577 use = kmalloc(sizeof(*use), GFP_ATOMIC);
530 if (!use) { 578 if (!use) {
531 printk("%s: out of memory loading\n", a->name); 579 printk("%s: out of memory loading\n", a->name);
532 module_put(b); 580 module_put(b);
533 return 0; 581 return -ENOMEM;
534 } 582 }
535 583
536 use->module_which_uses = a; 584 use->module_which_uses = a;
537 list_add(&use->list, &b->modules_which_use_me); 585 list_add(&use->list, &b->modules_which_use_me);
538 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name); 586 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
539 return 1; 587 return 0;
540} 588}
541EXPORT_SYMBOL_GPL(use_module); 589EXPORT_SYMBOL_GPL(use_module);
542 590
@@ -616,12 +664,28 @@ static int try_stop_module(struct module *mod, int flags, int *forced)
616 664
617unsigned int module_refcount(struct module *mod) 665unsigned int module_refcount(struct module *mod)
618{ 666{
619 unsigned int total = 0; 667 unsigned int incs = 0, decs = 0;
620 int cpu; 668 int cpu;
621 669
622 for_each_possible_cpu(cpu) 670 for_each_possible_cpu(cpu)
623 total += per_cpu_ptr(mod->refptr, cpu)->count; 671 decs += per_cpu_ptr(mod->refptr, cpu)->decs;
624 return total; 672 /*
673 * ensure the incs are added up after the decs.
674 * module_put ensures incs are visible before decs with smp_wmb.
675 *
676 * This 2-count scheme avoids the situation where the refcount
677 * for CPU0 is read, then CPU0 increments the module refcount,
678 * then CPU1 drops that refcount, then the refcount for CPU1 is
679 * read. We would record a decrement but not its corresponding
680 * increment so we would see a low count (disaster).
681 *
682 * Rare situation? But module_refcount can be preempted, and we
683 * might be tallying up 4096+ CPUs. So it is not impossible.
684 */
685 smp_rmb();
686 for_each_possible_cpu(cpu)
687 incs += per_cpu_ptr(mod->refptr, cpu)->incs;
688 return incs - decs;
625} 689}
626EXPORT_SYMBOL(module_refcount); 690EXPORT_SYMBOL(module_refcount);
627 691
@@ -657,16 +721,8 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
657 return -EFAULT; 721 return -EFAULT;
658 name[MODULE_NAME_LEN-1] = '\0'; 722 name[MODULE_NAME_LEN-1] = '\0';
659 723
660 /* Create stop_machine threads since free_module relies on 724 if (mutex_lock_interruptible(&module_mutex) != 0)
661 * a non-failing stop_machine call. */ 725 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 726
671 mod = find_module(name); 727 mod = find_module(name);
672 if (!mod) { 728 if (!mod) {
@@ -726,8 +782,6 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
726 782
727 out: 783 out:
728 mutex_unlock(&module_mutex); 784 mutex_unlock(&module_mutex);
729out_stop:
730 stop_machine_destroy();
731 return ret; 785 return ret;
732} 786}
733 787
@@ -798,10 +852,10 @@ void module_put(struct module *module)
798{ 852{
799 if (module) { 853 if (module) {
800 preempt_disable(); 854 preempt_disable();
801 __this_cpu_dec(module->refptr->count); 855 smp_wmb(); /* see comment in module_refcount */
856 __this_cpu_inc(module->refptr->decs);
802 857
803 trace_module_put(module, _RET_IP_, 858 trace_module_put(module, _RET_IP_);
804 __this_cpu_read(module->refptr->count));
805 /* Maybe they're waiting for us to drop reference? */ 859 /* Maybe they're waiting for us to drop reference? */
806 if (unlikely(!module_is_live(module))) 860 if (unlikely(!module_is_live(module)))
807 wake_up_process(module->waiter); 861 wake_up_process(module->waiter);
@@ -823,7 +877,7 @@ static inline void module_unload_free(struct module *mod)
823 877
824int use_module(struct module *a, struct module *b) 878int use_module(struct module *a, struct module *b)
825{ 879{
826 return strong_try_module_get(b) == 0; 880 return strong_try_module_get(b);
827} 881}
828EXPORT_SYMBOL_GPL(use_module); 882EXPORT_SYMBOL_GPL(use_module);
829 883
@@ -994,17 +1048,39 @@ static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
994 struct module *owner; 1048 struct module *owner;
995 const struct kernel_symbol *sym; 1049 const struct kernel_symbol *sym;
996 const unsigned long *crc; 1050 const unsigned long *crc;
1051 DEFINE_WAIT(wait);
1052 int err;
1053 long timeleft = 30 * HZ;
997 1054
1055again:
998 sym = find_symbol(name, &owner, &crc, 1056 sym = find_symbol(name, &owner, &crc,
999 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true); 1057 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1000 /* use_module can fail due to OOM, 1058 if (!sym)
1001 or module initialization or unloading */ 1059 return NULL;
1002 if (sym) { 1060
1003 if (!check_version(sechdrs, versindex, name, mod, crc, owner) 1061 if (!check_version(sechdrs, versindex, name, mod, crc, owner))
1004 || !use_module(mod, owner)) 1062 return NULL;
1005 sym = NULL; 1063
1064 prepare_to_wait(&module_wq, &wait, TASK_INTERRUPTIBLE);
1065 err = use_module(mod, owner);
1066 if (likely(!err) || err != -EBUSY || signal_pending(current)) {
1067 finish_wait(&module_wq, &wait);
1068 return err ? NULL : sym;
1006 } 1069 }
1007 return sym; 1070
1071 /* Module is still loading. Drop lock and wait. */
1072 mutex_unlock(&module_mutex);
1073 timeleft = schedule_timeout(timeleft);
1074 mutex_lock(&module_mutex);
1075 finish_wait(&module_wq, &wait);
1076
1077 /* Module might be gone entirely, or replaced. Re-lookup. */
1078 if (timeleft)
1079 goto again;
1080
1081 printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
1082 mod->name, owner->name);
1083 return NULL;
1008} 1084}
1009 1085
1010/* 1086/*
@@ -1125,7 +1201,7 @@ struct module_notes_attrs {
1125 struct bin_attribute attrs[0]; 1201 struct bin_attribute attrs[0];
1126}; 1202};
1127 1203
1128static ssize_t module_notes_read(struct kobject *kobj, 1204static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
1129 struct bin_attribute *bin_attr, 1205 struct bin_attribute *bin_attr,
1130 char *buf, loff_t pos, size_t count) 1206 char *buf, loff_t pos, size_t count)
1131{ 1207{
@@ -1400,8 +1476,7 @@ static void free_module(struct module *mod)
1400 /* This may be NULL, but that's OK */ 1476 /* This may be NULL, but that's OK */
1401 module_free(mod, mod->module_init); 1477 module_free(mod, mod->module_init);
1402 kfree(mod->args); 1478 kfree(mod->args);
1403 if (mod->percpu) 1479 percpu_modfree(mod);
1404 percpu_modfree(mod->percpu);
1405#if defined(CONFIG_MODULE_UNLOAD) 1480#if defined(CONFIG_MODULE_UNLOAD)
1406 if (mod->refptr) 1481 if (mod->refptr)
1407 free_percpu(mod->refptr); 1482 free_percpu(mod->refptr);
@@ -1520,7 +1595,7 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
1520 default: 1595 default:
1521 /* Divert to percpu allocation if a percpu var. */ 1596 /* Divert to percpu allocation if a percpu var. */
1522 if (sym[i].st_shndx == pcpuindex) 1597 if (sym[i].st_shndx == pcpuindex)
1523 secbase = (unsigned long)mod->percpu; 1598 secbase = (unsigned long)mod_percpu(mod);
1524 else 1599 else
1525 secbase = sechdrs[sym[i].st_shndx].sh_addr; 1600 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1526 sym[i].st_value += secbase; 1601 sym[i].st_value += secbase;
@@ -1954,7 +2029,7 @@ static noinline struct module *load_module(void __user *umod,
1954 unsigned int modindex, versindex, infoindex, pcpuindex; 2029 unsigned int modindex, versindex, infoindex, pcpuindex;
1955 struct module *mod; 2030 struct module *mod;
1956 long err = 0; 2031 long err = 0;
1957 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */ 2032 void *ptr = NULL; /* Stops spurious gcc warning */
1958 unsigned long symoffs, stroffs, *strmap; 2033 unsigned long symoffs, stroffs, *strmap;
1959 2034
1960 mm_segment_t old_fs; 2035 mm_segment_t old_fs;
@@ -2094,15 +2169,11 @@ static noinline struct module *load_module(void __user *umod,
2094 2169
2095 if (pcpuindex) { 2170 if (pcpuindex) {
2096 /* We have a special allocation for this section. */ 2171 /* We have a special allocation for this section. */
2097 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size, 2172 err = percpu_modalloc(mod, sechdrs[pcpuindex].sh_size,
2098 sechdrs[pcpuindex].sh_addralign, 2173 sechdrs[pcpuindex].sh_addralign);
2099 mod->name); 2174 if (err)
2100 if (!percpu) {
2101 err = -ENOMEM;
2102 goto free_mod; 2175 goto free_mod;
2103 }
2104 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC; 2176 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2105 mod->percpu = percpu;
2106 } 2177 }
2107 2178
2108 /* Determine total sizes, and put offsets in sh_entsize. For now 2179 /* Determine total sizes, and put offsets in sh_entsize. For now
@@ -2317,7 +2388,7 @@ static noinline struct module *load_module(void __user *umod,
2317 sort_extable(mod->extable, mod->extable + mod->num_exentries); 2388 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2318 2389
2319 /* Finally, copy percpu area over. */ 2390 /* Finally, copy percpu area over. */
2320 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr, 2391 percpu_modcopy(mod, (void *)sechdrs[pcpuindex].sh_addr,
2321 sechdrs[pcpuindex].sh_size); 2392 sechdrs[pcpuindex].sh_size);
2322 2393
2323 add_kallsyms(mod, sechdrs, hdr->e_shnum, symindex, strindex, 2394 add_kallsyms(mod, sechdrs, hdr->e_shnum, symindex, strindex,
@@ -2409,8 +2480,7 @@ static noinline struct module *load_module(void __user *umod,
2409 module_free(mod, mod->module_core); 2480 module_free(mod, mod->module_core);
2410 /* mod will be freed with core. Don't access it beyond this line! */ 2481 /* mod will be freed with core. Don't access it beyond this line! */
2411 free_percpu: 2482 free_percpu:
2412 if (percpu) 2483 percpu_modfree(mod);
2413 percpu_modfree(percpu);
2414 free_mod: 2484 free_mod:
2415 kfree(args); 2485 kfree(args);
2416 kfree(strmap); 2486 kfree(strmap);