aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/module.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/module.c')
-rw-r--r--kernel/module.c192
1 files changed, 126 insertions, 66 deletions
diff --git a/kernel/module.c b/kernel/module.c
index 5daf0abd63c..0129769301e 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;
@@ -178,8 +180,6 @@ extern const struct kernel_symbol __start___ksymtab_gpl[];
178extern const struct kernel_symbol __stop___ksymtab_gpl[]; 180extern const struct kernel_symbol __stop___ksymtab_gpl[];
179extern const struct kernel_symbol __start___ksymtab_gpl_future[]; 181extern const struct kernel_symbol __start___ksymtab_gpl_future[];
180extern const struct kernel_symbol __stop___ksymtab_gpl_future[]; 182extern 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[]; 183extern const unsigned long __start___kcrctab[];
184extern const unsigned long __start___kcrctab_gpl[]; 184extern const unsigned long __start___kcrctab_gpl[];
185extern const unsigned long __start___kcrctab_gpl_future[]; 185extern const unsigned long __start___kcrctab_gpl_future[];
@@ -370,27 +370,33 @@ EXPORT_SYMBOL_GPL(find_module);
370 370
371#ifdef CONFIG_SMP 371#ifdef CONFIG_SMP
372 372
373static void *percpu_modalloc(unsigned long size, unsigned long align, 373static inline void __percpu *mod_percpu(struct module *mod)
374 const char *name)
375{ 374{
376 void *ptr; 375 return mod->percpu;
376}
377 377
378static int percpu_modalloc(struct module *mod,
379 unsigned long size, unsigned long align)
380{
378 if (align > PAGE_SIZE) { 381 if (align > PAGE_SIZE) {
379 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n", 382 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
380 name, align, PAGE_SIZE); 383 mod->name, align, PAGE_SIZE);
381 align = PAGE_SIZE; 384 align = PAGE_SIZE;
382 } 385 }
383 386
384 ptr = __alloc_reserved_percpu(size, align); 387 mod->percpu = __alloc_reserved_percpu(size, align);
385 if (!ptr) 388 if (!mod->percpu) {
386 printk(KERN_WARNING 389 printk(KERN_WARNING
387 "Could not allocate %lu bytes percpu data\n", size); 390 "Could not allocate %lu bytes percpu data\n", size);
388 return ptr; 391 return -ENOMEM;
392 }
393 mod->percpu_size = size;
394 return 0;
389} 395}
390 396
391static void percpu_modfree(void *freeme) 397static void percpu_modfree(struct module *mod)
392{ 398{
393 free_percpu(freeme); 399 free_percpu(mod->percpu);
394} 400}
395 401
396static unsigned int find_pcpusec(Elf_Ehdr *hdr, 402static unsigned int find_pcpusec(Elf_Ehdr *hdr,
@@ -400,24 +406,62 @@ static unsigned int find_pcpusec(Elf_Ehdr *hdr,
400 return find_sec(hdr, sechdrs, secstrings, ".data..percpu"); 406 return find_sec(hdr, sechdrs, secstrings, ".data..percpu");
401} 407}
402 408
403static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size) 409static void percpu_modcopy(struct module *mod,
410 const void *from, unsigned long size)
404{ 411{
405 int cpu; 412 int cpu;
406 413
407 for_each_possible_cpu(cpu) 414 for_each_possible_cpu(cpu)
408 memcpy(pcpudest + per_cpu_offset(cpu), from, size); 415 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
416}
417
418/**
419 * is_module_percpu_address - test whether address is from module static percpu
420 * @addr: address to test
421 *
422 * Test whether @addr belongs to module static percpu area.
423 *
424 * RETURNS:
425 * %true if @addr is from module static percpu area
426 */
427bool is_module_percpu_address(unsigned long addr)
428{
429 struct module *mod;
430 unsigned int cpu;
431
432 preempt_disable();
433
434 list_for_each_entry_rcu(mod, &modules, list) {
435 if (!mod->percpu_size)
436 continue;
437 for_each_possible_cpu(cpu) {
438 void *start = per_cpu_ptr(mod->percpu, cpu);
439
440 if ((void *)addr >= start &&
441 (void *)addr < start + mod->percpu_size) {
442 preempt_enable();
443 return true;
444 }
445 }
446 }
447
448 preempt_enable();
449 return false;
409} 450}
410 451
411#else /* ... !CONFIG_SMP */ 452#else /* ... !CONFIG_SMP */
412 453
413static inline void *percpu_modalloc(unsigned long size, unsigned long align, 454static inline void __percpu *mod_percpu(struct module *mod)
414 const char *name)
415{ 455{
416 return NULL; 456 return NULL;
417} 457}
418static inline void percpu_modfree(void *pcpuptr) 458static inline int percpu_modalloc(struct module *mod,
459 unsigned long size, unsigned long align)
460{
461 return -ENOMEM;
462}
463static inline void percpu_modfree(struct module *mod)
419{ 464{
420 BUG();
421} 465}
422static inline unsigned int find_pcpusec(Elf_Ehdr *hdr, 466static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
423 Elf_Shdr *sechdrs, 467 Elf_Shdr *sechdrs,
@@ -425,12 +469,16 @@ static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
425{ 469{
426 return 0; 470 return 0;
427} 471}
428static inline void percpu_modcopy(void *pcpudst, const void *src, 472static inline void percpu_modcopy(struct module *mod,
429 unsigned long size) 473 const void *from, unsigned long size)
430{ 474{
431 /* pcpusec should be 0, and size of that section should be 0. */ 475 /* pcpusec should be 0, and size of that section should be 0. */
432 BUG_ON(size != 0); 476 BUG_ON(size != 0);
433} 477}
478bool is_module_percpu_address(unsigned long addr)
479{
480 return false;
481}
434 482
435#endif /* CONFIG_SMP */ 483#endif /* CONFIG_SMP */
436 484
@@ -467,16 +515,22 @@ MODINFO_ATTR(srcversion);
467static char last_unloaded_module[MODULE_NAME_LEN+1]; 515static char last_unloaded_module[MODULE_NAME_LEN+1];
468 516
469#ifdef CONFIG_MODULE_UNLOAD 517#ifdef CONFIG_MODULE_UNLOAD
518
519EXPORT_TRACEPOINT_SYMBOL(module_get);
520
470/* Init the unload section of the module. */ 521/* Init the unload section of the module. */
471static void module_unload_init(struct module *mod) 522static void module_unload_init(struct module *mod)
472{ 523{
473 int cpu; 524 int cpu;
474 525
475 INIT_LIST_HEAD(&mod->modules_which_use_me); 526 INIT_LIST_HEAD(&mod->modules_which_use_me);
476 for_each_possible_cpu(cpu) 527 for_each_possible_cpu(cpu) {
477 local_set(__module_ref_addr(mod, cpu), 0); 528 per_cpu_ptr(mod->refptr, cpu)->incs = 0;
529 per_cpu_ptr(mod->refptr, cpu)->decs = 0;
530 }
531
478 /* Hold reference count during initialization. */ 532 /* Hold reference count during initialization. */
479 local_set(__module_ref_addr(mod, raw_smp_processor_id()), 1); 533 __this_cpu_write(mod->refptr->incs, 1);
480 /* Backwards compatibility macros put refcount during init. */ 534 /* Backwards compatibility macros put refcount during init. */
481 mod->waiter = current; 535 mod->waiter = current;
482} 536}
@@ -615,12 +669,28 @@ static int try_stop_module(struct module *mod, int flags, int *forced)
615 669
616unsigned int module_refcount(struct module *mod) 670unsigned int module_refcount(struct module *mod)
617{ 671{
618 unsigned int total = 0; 672 unsigned int incs = 0, decs = 0;
619 int cpu; 673 int cpu;
620 674
621 for_each_possible_cpu(cpu) 675 for_each_possible_cpu(cpu)
622 total += local_read(__module_ref_addr(mod, cpu)); 676 decs += per_cpu_ptr(mod->refptr, cpu)->decs;
623 return total; 677 /*
678 * ensure the incs are added up after the decs.
679 * module_put ensures incs are visible before decs with smp_wmb.
680 *
681 * This 2-count scheme avoids the situation where the refcount
682 * for CPU0 is read, then CPU0 increments the module refcount,
683 * then CPU1 drops that refcount, then the refcount for CPU1 is
684 * read. We would record a decrement but not its corresponding
685 * increment so we would see a low count (disaster).
686 *
687 * Rare situation? But module_refcount can be preempted, and we
688 * might be tallying up 4096+ CPUs. So it is not impossible.
689 */
690 smp_rmb();
691 for_each_possible_cpu(cpu)
692 incs += per_cpu_ptr(mod->refptr, cpu)->incs;
693 return incs - decs;
624} 694}
625EXPORT_SYMBOL(module_refcount); 695EXPORT_SYMBOL(module_refcount);
626 696
@@ -656,16 +726,8 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
656 return -EFAULT; 726 return -EFAULT;
657 name[MODULE_NAME_LEN-1] = '\0'; 727 name[MODULE_NAME_LEN-1] = '\0';
658 728
659 /* Create stop_machine threads since free_module relies on 729 if (mutex_lock_interruptible(&module_mutex) != 0)
660 * a non-failing stop_machine call. */ 730 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 731
670 mod = find_module(name); 732 mod = find_module(name);
671 if (!mod) { 733 if (!mod) {
@@ -725,8 +787,6 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
725 787
726 out: 788 out:
727 mutex_unlock(&module_mutex); 789 mutex_unlock(&module_mutex);
728out_stop:
729 stop_machine_destroy();
730 return ret; 790 return ret;
731} 791}
732 792
@@ -796,14 +856,15 @@ static struct module_attribute refcnt = {
796void module_put(struct module *module) 856void module_put(struct module *module)
797{ 857{
798 if (module) { 858 if (module) {
799 unsigned int cpu = get_cpu(); 859 preempt_disable();
800 local_dec(__module_ref_addr(module, cpu)); 860 smp_wmb(); /* see comment in module_refcount */
801 trace_module_put(module, _RET_IP_, 861 __this_cpu_inc(module->refptr->decs);
802 local_read(__module_ref_addr(module, cpu))); 862
863 trace_module_put(module, _RET_IP_);
803 /* Maybe they're waiting for us to drop reference? */ 864 /* Maybe they're waiting for us to drop reference? */
804 if (unlikely(!module_is_live(module))) 865 if (unlikely(!module_is_live(module)))
805 wake_up_process(module->waiter); 866 wake_up_process(module->waiter);
806 put_cpu(); 867 preempt_enable();
807 } 868 }
808} 869}
809EXPORT_SYMBOL(module_put); 870EXPORT_SYMBOL(module_put);
@@ -1083,6 +1144,7 @@ static void add_sect_attrs(struct module *mod, unsigned int nsect,
1083 if (sattr->name == NULL) 1144 if (sattr->name == NULL)
1084 goto out; 1145 goto out;
1085 sect_attrs->nsections++; 1146 sect_attrs->nsections++;
1147 sysfs_attr_init(&sattr->mattr.attr);
1086 sattr->mattr.show = module_sect_show; 1148 sattr->mattr.show = module_sect_show;
1087 sattr->mattr.store = NULL; 1149 sattr->mattr.store = NULL;
1088 sattr->mattr.attr.name = sattr->name; 1150 sattr->mattr.attr.name = sattr->name;
@@ -1122,7 +1184,7 @@ struct module_notes_attrs {
1122 struct bin_attribute attrs[0]; 1184 struct bin_attribute attrs[0];
1123}; 1185};
1124 1186
1125static ssize_t module_notes_read(struct kobject *kobj, 1187static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
1126 struct bin_attribute *bin_attr, 1188 struct bin_attribute *bin_attr,
1127 char *buf, loff_t pos, size_t count) 1189 char *buf, loff_t pos, size_t count)
1128{ 1190{
@@ -1178,6 +1240,7 @@ static void add_notes_attrs(struct module *mod, unsigned int nsect,
1178 if (sect_empty(&sechdrs[i])) 1240 if (sect_empty(&sechdrs[i]))
1179 continue; 1241 continue;
1180 if (sechdrs[i].sh_type == SHT_NOTE) { 1242 if (sechdrs[i].sh_type == SHT_NOTE) {
1243 sysfs_bin_attr_init(nattr);
1181 nattr->attr.name = mod->sect_attrs->attrs[loaded].name; 1244 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1182 nattr->attr.mode = S_IRUGO; 1245 nattr->attr.mode = S_IRUGO;
1183 nattr->size = sechdrs[i].sh_size; 1246 nattr->size = sechdrs[i].sh_size;
@@ -1250,6 +1313,7 @@ int module_add_modinfo_attrs(struct module *mod)
1250 if (!attr->test || 1313 if (!attr->test ||
1251 (attr->test && attr->test(mod))) { 1314 (attr->test && attr->test(mod))) {
1252 memcpy(temp_attr, attr, sizeof(*temp_attr)); 1315 memcpy(temp_attr, attr, sizeof(*temp_attr));
1316 sysfs_attr_init(&temp_attr->attr);
1253 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr); 1317 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1254 ++temp_attr; 1318 ++temp_attr;
1255 } 1319 }
@@ -1395,11 +1459,10 @@ static void free_module(struct module *mod)
1395 /* This may be NULL, but that's OK */ 1459 /* This may be NULL, but that's OK */
1396 module_free(mod, mod->module_init); 1460 module_free(mod, mod->module_init);
1397 kfree(mod->args); 1461 kfree(mod->args);
1398 if (mod->percpu) 1462 percpu_modfree(mod);
1399 percpu_modfree(mod->percpu); 1463#if defined(CONFIG_MODULE_UNLOAD)
1400#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
1401 if (mod->refptr) 1464 if (mod->refptr)
1402 percpu_modfree(mod->refptr); 1465 free_percpu(mod->refptr);
1403#endif 1466#endif
1404 /* Free lock-classes: */ 1467 /* Free lock-classes: */
1405 lockdep_free_key_range(mod->module_core, mod->core_size); 1468 lockdep_free_key_range(mod->module_core, mod->core_size);
@@ -1515,7 +1578,7 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
1515 default: 1578 default:
1516 /* Divert to percpu allocation if a percpu var. */ 1579 /* Divert to percpu allocation if a percpu var. */
1517 if (sym[i].st_shndx == pcpuindex) 1580 if (sym[i].st_shndx == pcpuindex)
1518 secbase = (unsigned long)mod->percpu; 1581 secbase = (unsigned long)mod_percpu(mod);
1519 else 1582 else
1520 secbase = sechdrs[sym[i].st_shndx].sh_addr; 1583 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1521 sym[i].st_value += secbase; 1584 sym[i].st_value += secbase;
@@ -1949,8 +2012,9 @@ static noinline struct module *load_module(void __user *umod,
1949 unsigned int modindex, versindex, infoindex, pcpuindex; 2012 unsigned int modindex, versindex, infoindex, pcpuindex;
1950 struct module *mod; 2013 struct module *mod;
1951 long err = 0; 2014 long err = 0;
1952 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */ 2015 void *ptr = NULL; /* Stops spurious gcc warning */
1953 unsigned long symoffs, stroffs, *strmap; 2016 unsigned long symoffs, stroffs, *strmap;
2017 void __percpu *percpu;
1954 2018
1955 mm_segment_t old_fs; 2019 mm_segment_t old_fs;
1956 2020
@@ -2089,16 +2153,14 @@ static noinline struct module *load_module(void __user *umod,
2089 2153
2090 if (pcpuindex) { 2154 if (pcpuindex) {
2091 /* We have a special allocation for this section. */ 2155 /* We have a special allocation for this section. */
2092 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size, 2156 err = percpu_modalloc(mod, sechdrs[pcpuindex].sh_size,
2093 sechdrs[pcpuindex].sh_addralign, 2157 sechdrs[pcpuindex].sh_addralign);
2094 mod->name); 2158 if (err)
2095 if (!percpu) {
2096 err = -ENOMEM;
2097 goto free_mod; 2159 goto free_mod;
2098 }
2099 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC; 2160 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2100 mod->percpu = percpu;
2101 } 2161 }
2162 /* Keep this around for failure path. */
2163 percpu = mod_percpu(mod);
2102 2164
2103 /* Determine total sizes, and put offsets in sh_entsize. For now 2165 /* Determine total sizes, and put offsets in sh_entsize. For now
2104 this is done generically; there doesn't appear to be any 2166 this is done generically; there doesn't appear to be any
@@ -2162,9 +2224,8 @@ static noinline struct module *load_module(void __user *umod,
2162 mod = (void *)sechdrs[modindex].sh_addr; 2224 mod = (void *)sechdrs[modindex].sh_addr;
2163 kmemleak_load_module(mod, hdr, sechdrs, secstrings); 2225 kmemleak_load_module(mod, hdr, sechdrs, secstrings);
2164 2226
2165#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP) 2227#if defined(CONFIG_MODULE_UNLOAD)
2166 mod->refptr = percpu_modalloc(sizeof(local_t), __alignof__(local_t), 2228 mod->refptr = alloc_percpu(struct module_ref);
2167 mod->name);
2168 if (!mod->refptr) { 2229 if (!mod->refptr) {
2169 err = -ENOMEM; 2230 err = -ENOMEM;
2170 goto free_init; 2231 goto free_init;
@@ -2313,7 +2374,7 @@ static noinline struct module *load_module(void __user *umod,
2313 sort_extable(mod->extable, mod->extable + mod->num_exentries); 2374 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2314 2375
2315 /* Finally, copy percpu area over. */ 2376 /* Finally, copy percpu area over. */
2316 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr, 2377 percpu_modcopy(mod, (void *)sechdrs[pcpuindex].sh_addr,
2317 sechdrs[pcpuindex].sh_size); 2378 sechdrs[pcpuindex].sh_size);
2318 2379
2319 add_kallsyms(mod, sechdrs, hdr->e_shnum, symindex, strindex, 2380 add_kallsyms(mod, sechdrs, hdr->e_shnum, symindex, strindex,
@@ -2396,8 +2457,8 @@ static noinline struct module *load_module(void __user *umod,
2396 kobject_put(&mod->mkobj.kobj); 2457 kobject_put(&mod->mkobj.kobj);
2397 free_unload: 2458 free_unload:
2398 module_unload_free(mod); 2459 module_unload_free(mod);
2399#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP) 2460#if defined(CONFIG_MODULE_UNLOAD)
2400 percpu_modfree(mod->refptr); 2461 free_percpu(mod->refptr);
2401 free_init: 2462 free_init:
2402#endif 2463#endif
2403 module_free(mod, mod->module_init); 2464 module_free(mod, mod->module_init);
@@ -2405,8 +2466,7 @@ static noinline struct module *load_module(void __user *umod,
2405 module_free(mod, mod->module_core); 2466 module_free(mod, mod->module_core);
2406 /* mod will be freed with core. Don't access it beyond this line! */ 2467 /* mod will be freed with core. Don't access it beyond this line! */
2407 free_percpu: 2468 free_percpu:
2408 if (percpu) 2469 free_percpu(percpu);
2409 percpu_modfree(percpu);
2410 free_mod: 2470 free_mod:
2411 kfree(args); 2471 kfree(args);
2412 kfree(strmap); 2472 kfree(strmap);