aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/module.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/module.c')
-rw-r--r--kernel/module.c149
1 files changed, 95 insertions, 54 deletions
diff --git a/kernel/module.c b/kernel/module.c
index 3965511ae133..b34813f725e9 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -56,6 +56,7 @@
56#include <linux/async.h> 56#include <linux/async.h>
57#include <linux/percpu.h> 57#include <linux/percpu.h>
58#include <linux/kmemleak.h> 58#include <linux/kmemleak.h>
59#include <linux/kasan.h>
59#include <linux/jump_label.h> 60#include <linux/jump_label.h>
60#include <linux/pfn.h> 61#include <linux/pfn.h>
61#include <linux/bsearch.h> 62#include <linux/bsearch.h>
@@ -772,9 +773,18 @@ static int try_stop_module(struct module *mod, int flags, int *forced)
772 return 0; 773 return 0;
773} 774}
774 775
775unsigned long module_refcount(struct module *mod) 776/**
777 * module_refcount - return the refcount or -1 if unloading
778 *
779 * @mod: the module we're checking
780 *
781 * Returns:
782 * -1 if the module is in the process of unloading
783 * otherwise the number of references in the kernel to the module
784 */
785int module_refcount(struct module *mod)
776{ 786{
777 return (unsigned long)atomic_read(&mod->refcnt) - MODULE_REF_BASE; 787 return atomic_read(&mod->refcnt) - MODULE_REF_BASE;
778} 788}
779EXPORT_SYMBOL(module_refcount); 789EXPORT_SYMBOL(module_refcount);
780 790
@@ -856,7 +866,7 @@ static inline void print_unload_info(struct seq_file *m, struct module *mod)
856 struct module_use *use; 866 struct module_use *use;
857 int printed_something = 0; 867 int printed_something = 0;
858 868
859 seq_printf(m, " %lu ", module_refcount(mod)); 869 seq_printf(m, " %i ", module_refcount(mod));
860 870
861 /* 871 /*
862 * Always include a trailing , so userspace can differentiate 872 * Always include a trailing , so userspace can differentiate
@@ -908,7 +918,7 @@ EXPORT_SYMBOL_GPL(symbol_put_addr);
908static ssize_t show_refcnt(struct module_attribute *mattr, 918static ssize_t show_refcnt(struct module_attribute *mattr,
909 struct module_kobject *mk, char *buffer) 919 struct module_kobject *mk, char *buffer)
910{ 920{
911 return sprintf(buffer, "%lu\n", module_refcount(mk->mod)); 921 return sprintf(buffer, "%i\n", module_refcount(mk->mod));
912} 922}
913 923
914static struct module_attribute modinfo_refcnt = 924static struct module_attribute modinfo_refcnt =
@@ -1216,6 +1226,12 @@ static const struct kernel_symbol *resolve_symbol(struct module *mod,
1216 const unsigned long *crc; 1226 const unsigned long *crc;
1217 int err; 1227 int err;
1218 1228
1229 /*
1230 * The module_mutex should not be a heavily contended lock;
1231 * if we get the occasional sleep here, we'll go an extra iteration
1232 * in the wait_event_interruptible(), which is harmless.
1233 */
1234 sched_annotate_sleep();
1219 mutex_lock(&module_mutex); 1235 mutex_lock(&module_mutex);
1220 sym = find_symbol(name, &owner, &crc, 1236 sym = find_symbol(name, &owner, &crc,
1221 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true); 1237 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
@@ -1795,15 +1811,20 @@ static void unset_module_core_ro_nx(struct module *mod) { }
1795static void unset_module_init_ro_nx(struct module *mod) { } 1811static void unset_module_init_ro_nx(struct module *mod) { }
1796#endif 1812#endif
1797 1813
1798void __weak module_free(struct module *mod, void *module_region) 1814void __weak module_memfree(void *module_region)
1799{ 1815{
1800 vfree(module_region); 1816 vfree(module_region);
1817 kasan_module_free(module_region);
1801} 1818}
1802 1819
1803void __weak module_arch_cleanup(struct module *mod) 1820void __weak module_arch_cleanup(struct module *mod)
1804{ 1821{
1805} 1822}
1806 1823
1824void __weak module_arch_freeing_init(struct module *mod)
1825{
1826}
1827
1807/* Free a module, remove from lists, etc. */ 1828/* Free a module, remove from lists, etc. */
1808static void free_module(struct module *mod) 1829static void free_module(struct module *mod)
1809{ 1830{
@@ -1841,7 +1862,8 @@ static void free_module(struct module *mod)
1841 1862
1842 /* This may be NULL, but that's OK */ 1863 /* This may be NULL, but that's OK */
1843 unset_module_init_ro_nx(mod); 1864 unset_module_init_ro_nx(mod);
1844 module_free(mod, mod->module_init); 1865 module_arch_freeing_init(mod);
1866 module_memfree(mod->module_init);
1845 kfree(mod->args); 1867 kfree(mod->args);
1846 percpu_modfree(mod); 1868 percpu_modfree(mod);
1847 1869
@@ -1850,7 +1872,7 @@ static void free_module(struct module *mod)
1850 1872
1851 /* Finally, free the core (containing the module structure) */ 1873 /* Finally, free the core (containing the module structure) */
1852 unset_module_core_ro_nx(mod); 1874 unset_module_core_ro_nx(mod);
1853 module_free(mod, mod->module_core); 1875 module_memfree(mod->module_core);
1854 1876
1855#ifdef CONFIG_MPU 1877#ifdef CONFIG_MPU
1856 update_protections(current->mm); 1878 update_protections(current->mm);
@@ -2785,7 +2807,7 @@ static int move_module(struct module *mod, struct load_info *info)
2785 */ 2807 */
2786 kmemleak_ignore(ptr); 2808 kmemleak_ignore(ptr);
2787 if (!ptr) { 2809 if (!ptr) {
2788 module_free(mod, mod->module_core); 2810 module_memfree(mod->module_core);
2789 return -ENOMEM; 2811 return -ENOMEM;
2790 } 2812 }
2791 memset(ptr, 0, mod->init_size); 2813 memset(ptr, 0, mod->init_size);
@@ -2930,8 +2952,9 @@ static struct module *layout_and_allocate(struct load_info *info, int flags)
2930static void module_deallocate(struct module *mod, struct load_info *info) 2952static void module_deallocate(struct module *mod, struct load_info *info)
2931{ 2953{
2932 percpu_modfree(mod); 2954 percpu_modfree(mod);
2933 module_free(mod, mod->module_init); 2955 module_arch_freeing_init(mod);
2934 module_free(mod, mod->module_core); 2956 module_memfree(mod->module_init);
2957 module_memfree(mod->module_core);
2935} 2958}
2936 2959
2937int __weak module_finalize(const Elf_Ehdr *hdr, 2960int __weak module_finalize(const Elf_Ehdr *hdr,
@@ -2963,6 +2986,12 @@ static bool finished_loading(const char *name)
2963 struct module *mod; 2986 struct module *mod;
2964 bool ret; 2987 bool ret;
2965 2988
2989 /*
2990 * The module_mutex should not be a heavily contended lock;
2991 * if we get the occasional sleep here, we'll go an extra iteration
2992 * in the wait_event_interruptible(), which is harmless.
2993 */
2994 sched_annotate_sleep();
2966 mutex_lock(&module_mutex); 2995 mutex_lock(&module_mutex);
2967 mod = find_module_all(name, strlen(name), true); 2996 mod = find_module_all(name, strlen(name), true);
2968 ret = !mod || mod->state == MODULE_STATE_LIVE 2997 ret = !mod || mod->state == MODULE_STATE_LIVE
@@ -2983,10 +3012,36 @@ static void do_mod_ctors(struct module *mod)
2983#endif 3012#endif
2984} 3013}
2985 3014
2986/* This is where the real work happens */ 3015/* For freeing module_init on success, in case kallsyms traversing */
2987static int do_init_module(struct module *mod) 3016struct mod_initfree {
3017 struct rcu_head rcu;
3018 void *module_init;
3019};
3020
3021static void do_free_init(struct rcu_head *head)
3022{
3023 struct mod_initfree *m = container_of(head, struct mod_initfree, rcu);
3024 module_memfree(m->module_init);
3025 kfree(m);
3026}
3027
3028/*
3029 * This is where the real work happens.
3030 *
3031 * Keep it uninlined to provide a reliable breakpoint target, e.g. for the gdb
3032 * helper command 'lx-symbols'.
3033 */
3034static noinline int do_init_module(struct module *mod)
2988{ 3035{
2989 int ret = 0; 3036 int ret = 0;
3037 struct mod_initfree *freeinit;
3038
3039 freeinit = kmalloc(sizeof(*freeinit), GFP_KERNEL);
3040 if (!freeinit) {
3041 ret = -ENOMEM;
3042 goto fail;
3043 }
3044 freeinit->module_init = mod->module_init;
2990 3045
2991 /* 3046 /*
2992 * We want to find out whether @mod uses async during init. Clear 3047 * We want to find out whether @mod uses async during init. Clear
@@ -2999,18 +3054,7 @@ static int do_init_module(struct module *mod)
2999 if (mod->init != NULL) 3054 if (mod->init != NULL)
3000 ret = do_one_initcall(mod->init); 3055 ret = do_one_initcall(mod->init);
3001 if (ret < 0) { 3056 if (ret < 0) {
3002 /* 3057 goto fail_free_freeinit;
3003 * Init routine failed: abort. Try to protect us from
3004 * buggy refcounters.
3005 */
3006 mod->state = MODULE_STATE_GOING;
3007 synchronize_sched();
3008 module_put(mod);
3009 blocking_notifier_call_chain(&module_notify_list,
3010 MODULE_STATE_GOING, mod);
3011 free_module(mod);
3012 wake_up_all(&module_wq);
3013 return ret;
3014 } 3058 }
3015 if (ret > 0) { 3059 if (ret > 0) {
3016 pr_warn("%s: '%s'->init suspiciously returned %d, it should " 3060 pr_warn("%s: '%s'->init suspiciously returned %d, it should "
@@ -3055,15 +3099,35 @@ static int do_init_module(struct module *mod)
3055 mod->strtab = mod->core_strtab; 3099 mod->strtab = mod->core_strtab;
3056#endif 3100#endif
3057 unset_module_init_ro_nx(mod); 3101 unset_module_init_ro_nx(mod);
3058 module_free(mod, mod->module_init); 3102 module_arch_freeing_init(mod);
3059 mod->module_init = NULL; 3103 mod->module_init = NULL;
3060 mod->init_size = 0; 3104 mod->init_size = 0;
3061 mod->init_ro_size = 0; 3105 mod->init_ro_size = 0;
3062 mod->init_text_size = 0; 3106 mod->init_text_size = 0;
3107 /*
3108 * We want to free module_init, but be aware that kallsyms may be
3109 * walking this with preempt disabled. In all the failure paths,
3110 * we call synchronize_rcu/synchronize_sched, but we don't want
3111 * to slow down the success path, so use actual RCU here.
3112 */
3113 call_rcu(&freeinit->rcu, do_free_init);
3063 mutex_unlock(&module_mutex); 3114 mutex_unlock(&module_mutex);
3064 wake_up_all(&module_wq); 3115 wake_up_all(&module_wq);
3065 3116
3066 return 0; 3117 return 0;
3118
3119fail_free_freeinit:
3120 kfree(freeinit);
3121fail:
3122 /* Try to protect us from buggy refcounters. */
3123 mod->state = MODULE_STATE_GOING;
3124 synchronize_sched();
3125 module_put(mod);
3126 blocking_notifier_call_chain(&module_notify_list,
3127 MODULE_STATE_GOING, mod);
3128 free_module(mod);
3129 wake_up_all(&module_wq);
3130 return ret;
3067} 3131}
3068 3132
3069static int may_init_module(void) 3133static int may_init_module(void)
@@ -3075,32 +3139,6 @@ static int may_init_module(void)
3075} 3139}
3076 3140
3077/* 3141/*
3078 * Can't use wait_event_interruptible() because our condition
3079 * 'finished_loading()' contains a blocking primitive itself (mutex_lock).
3080 */
3081static int wait_finished_loading(struct module *mod)
3082{
3083 DEFINE_WAIT_FUNC(wait, woken_wake_function);
3084 int ret = 0;
3085
3086 add_wait_queue(&module_wq, &wait);
3087 for (;;) {
3088 if (finished_loading(mod->name))
3089 break;
3090
3091 if (signal_pending(current)) {
3092 ret = -ERESTARTSYS;
3093 break;
3094 }
3095
3096 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
3097 }
3098 remove_wait_queue(&module_wq, &wait);
3099
3100 return ret;
3101}
3102
3103/*
3104 * We try to place it in the list now to make sure it's unique before 3142 * We try to place it in the list now to make sure it's unique before
3105 * we dedicate too many resources. In particular, temporary percpu 3143 * we dedicate too many resources. In particular, temporary percpu
3106 * memory exhaustion. 3144 * memory exhaustion.
@@ -3120,8 +3158,8 @@ again:
3120 || old->state == MODULE_STATE_UNFORMED) { 3158 || old->state == MODULE_STATE_UNFORMED) {
3121 /* Wait in case it fails to load. */ 3159 /* Wait in case it fails to load. */
3122 mutex_unlock(&module_mutex); 3160 mutex_unlock(&module_mutex);
3123 3161 err = wait_event_interruptible(module_wq,
3124 err = wait_finished_loading(mod); 3162 finished_loading(mod->name));
3125 if (err) 3163 if (err)
3126 goto out_unlocked; 3164 goto out_unlocked;
3127 goto again; 3165 goto again;
@@ -3220,7 +3258,7 @@ static int load_module(struct load_info *info, const char __user *uargs,
3220 mod->sig_ok = info->sig_ok; 3258 mod->sig_ok = info->sig_ok;
3221 if (!mod->sig_ok) { 3259 if (!mod->sig_ok) {
3222 pr_notice_once("%s: module verification failed: signature " 3260 pr_notice_once("%s: module verification failed: signature "
3223 "and/or required key missing - tainting " 3261 "and/or required key missing - tainting "
3224 "kernel\n", mod->name); 3262 "kernel\n", mod->name);
3225 add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK); 3263 add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK);
3226 } 3264 }
@@ -3311,6 +3349,9 @@ static int load_module(struct load_info *info, const char __user *uargs,
3311 module_bug_cleanup(mod); 3349 module_bug_cleanup(mod);
3312 mutex_unlock(&module_mutex); 3350 mutex_unlock(&module_mutex);
3313 3351
3352 /* Free lock-classes: */
3353 lockdep_free_key_range(mod->module_core, mod->core_size);
3354
3314 /* we can't deallocate the module until we clear memory protection */ 3355 /* we can't deallocate the module until we clear memory protection */
3315 unset_module_init_ro_nx(mod); 3356 unset_module_init_ro_nx(mod);
3316 unset_module_core_ro_nx(mod); 3357 unset_module_core_ro_nx(mod);