aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/module.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/module.c')
-rw-r--r--kernel/module.c294
1 files changed, 134 insertions, 160 deletions
diff --git a/kernel/module.c b/kernel/module.c
index 3202c9950073..bd60278ee703 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -47,8 +47,6 @@
47#include <asm/cacheflush.h> 47#include <asm/cacheflush.h>
48#include <linux/license.h> 48#include <linux/license.h>
49 49
50extern int module_sysfs_initialized;
51
52#if 0 50#if 0
53#define DEBUGP printk 51#define DEBUGP printk
54#else 52#else
@@ -67,6 +65,9 @@ extern int module_sysfs_initialized;
67static DEFINE_MUTEX(module_mutex); 65static DEFINE_MUTEX(module_mutex);
68static LIST_HEAD(modules); 66static LIST_HEAD(modules);
69 67
68/* Waiting for a module to finish initializing? */
69static DECLARE_WAIT_QUEUE_HEAD(module_wq);
70
70static BLOCKING_NOTIFIER_HEAD(module_notify_list); 71static BLOCKING_NOTIFIER_HEAD(module_notify_list);
71 72
72int register_module_notifier(struct notifier_block * nb) 73int register_module_notifier(struct notifier_block * nb)
@@ -81,12 +82,16 @@ int unregister_module_notifier(struct notifier_block * nb)
81} 82}
82EXPORT_SYMBOL(unregister_module_notifier); 83EXPORT_SYMBOL(unregister_module_notifier);
83 84
84/* We require a truly strong try_module_get() */ 85/* We require a truly strong try_module_get(): 0 means failure due to
86 ongoing or failed initialization etc. */
85static inline int strong_try_module_get(struct module *mod) 87static inline int strong_try_module_get(struct module *mod)
86{ 88{
87 if (mod && mod->state == MODULE_STATE_COMING) 89 if (mod && mod->state == MODULE_STATE_COMING)
90 return -EBUSY;
91 if (try_module_get(mod))
88 return 0; 92 return 0;
89 return try_module_get(mod); 93 else
94 return -ENOENT;
90} 95}
91 96
92static inline void add_taint_module(struct module *mod, unsigned flag) 97static inline void add_taint_module(struct module *mod, unsigned flag)
@@ -425,6 +430,14 @@ static unsigned int find_pcpusec(Elf_Ehdr *hdr,
425 return find_sec(hdr, sechdrs, secstrings, ".data.percpu"); 430 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
426} 431}
427 432
433static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
434{
435 int cpu;
436
437 for_each_possible_cpu(cpu)
438 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
439}
440
428static int percpu_modinit(void) 441static int percpu_modinit(void)
429{ 442{
430 pcpu_num_used = 2; 443 pcpu_num_used = 2;
@@ -497,6 +510,8 @@ static struct module_attribute modinfo_##field = { \
497MODINFO_ATTR(version); 510MODINFO_ATTR(version);
498MODINFO_ATTR(srcversion); 511MODINFO_ATTR(srcversion);
499 512
513static char last_unloaded_module[MODULE_NAME_LEN+1];
514
500#ifdef CONFIG_MODULE_UNLOAD 515#ifdef CONFIG_MODULE_UNLOAD
501/* Init the unload section of the module. */ 516/* Init the unload section of the module. */
502static void module_unload_init(struct module *mod) 517static void module_unload_init(struct module *mod)
@@ -538,11 +553,21 @@ static int already_uses(struct module *a, struct module *b)
538static int use_module(struct module *a, struct module *b) 553static int use_module(struct module *a, struct module *b)
539{ 554{
540 struct module_use *use; 555 struct module_use *use;
541 int no_warn; 556 int no_warn, err;
542 557
543 if (b == NULL || already_uses(a, b)) return 1; 558 if (b == NULL || already_uses(a, b)) return 1;
544 559
545 if (!strong_try_module_get(b)) 560 /* If we're interrupted or time out, we fail. */
561 if (wait_event_interruptible_timeout(
562 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
563 30 * HZ) <= 0) {
564 printk("%s: gave up waiting for init of module %s.\n",
565 a->name, b->name);
566 return 0;
567 }
568
569 /* If strong_try_module_get() returned a different error, we fail. */
570 if (err)
546 return 0; 571 return 0;
547 572
548 DEBUGP("Allocating new usage for %s.\n", a->name); 573 DEBUGP("Allocating new usage for %s.\n", a->name);
@@ -720,6 +745,8 @@ sys_delete_module(const char __user *name_user, unsigned int flags)
720 mod->exit(); 745 mod->exit();
721 mutex_lock(&module_mutex); 746 mutex_lock(&module_mutex);
722 } 747 }
748 /* Store the name of the last unloaded module for diagnostic purposes */
749 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
723 free_module(mod); 750 free_module(mod);
724 751
725 out: 752 out:
@@ -813,7 +840,7 @@ static inline void module_unload_free(struct module *mod)
813 840
814static inline int use_module(struct module *a, struct module *b) 841static inline int use_module(struct module *a, struct module *b)
815{ 842{
816 return strong_try_module_get(b); 843 return strong_try_module_get(b) == 0;
817} 844}
818 845
819static inline void module_unload_init(struct module *mod) 846static inline void module_unload_init(struct module *mod)
@@ -952,7 +979,8 @@ static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
952 ret = __find_symbol(name, &owner, &crc, 979 ret = __find_symbol(name, &owner, &crc,
953 !(mod->taints & TAINT_PROPRIETARY_MODULE)); 980 !(mod->taints & TAINT_PROPRIETARY_MODULE));
954 if (ret) { 981 if (ret) {
955 /* use_module can fail due to OOM, or module unloading */ 982 /* use_module can fail due to OOM,
983 or module initialization or unloading */
956 if (!check_version(sechdrs, versindex, name, mod, crc) || 984 if (!check_version(sechdrs, versindex, name, mod, crc) ||
957 !use_module(mod, owner)) 985 !use_module(mod, owner))
958 ret = 0; 986 ret = 0;
@@ -1120,7 +1148,7 @@ static void add_notes_attrs(struct module *mod, unsigned int nsect,
1120 ++loaded; 1148 ++loaded;
1121 } 1149 }
1122 1150
1123 notes_attrs->dir = kobject_add_dir(&mod->mkobj.kobj, "notes"); 1151 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
1124 if (!notes_attrs->dir) 1152 if (!notes_attrs->dir)
1125 goto out; 1153 goto out;
1126 1154
@@ -1210,6 +1238,7 @@ void module_remove_modinfo_attrs(struct module *mod)
1210int mod_sysfs_init(struct module *mod) 1238int mod_sysfs_init(struct module *mod)
1211{ 1239{
1212 int err; 1240 int err;
1241 struct kobject *kobj;
1213 1242
1214 if (!module_sysfs_initialized) { 1243 if (!module_sysfs_initialized) {
1215 printk(KERN_ERR "%s: module sysfs not initialized\n", 1244 printk(KERN_ERR "%s: module sysfs not initialized\n",
@@ -1217,15 +1246,25 @@ int mod_sysfs_init(struct module *mod)
1217 err = -EINVAL; 1246 err = -EINVAL;
1218 goto out; 1247 goto out;
1219 } 1248 }
1220 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj)); 1249
1221 err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name); 1250 kobj = kset_find_obj(module_kset, mod->name);
1222 if (err) 1251 if (kobj) {
1252 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1253 kobject_put(kobj);
1254 err = -EINVAL;
1223 goto out; 1255 goto out;
1224 kobj_set_kset_s(&mod->mkobj, module_subsys); 1256 }
1257
1225 mod->mkobj.mod = mod; 1258 mod->mkobj.mod = mod;
1226 1259
1227 kobject_init(&mod->mkobj.kobj); 1260 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1261 mod->mkobj.kobj.kset = module_kset;
1262 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1263 "%s", mod->name);
1264 if (err)
1265 kobject_put(&mod->mkobj.kobj);
1228 1266
1267 /* delay uevent until full sysfs population */
1229out: 1268out:
1230 return err; 1269 return err;
1231} 1270}
@@ -1236,12 +1275,7 @@ int mod_sysfs_setup(struct module *mod,
1236{ 1275{
1237 int err; 1276 int err;
1238 1277
1239 /* delay uevent until full sysfs population */ 1278 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1240 err = kobject_add(&mod->mkobj.kobj);
1241 if (err)
1242 goto out;
1243
1244 mod->holders_dir = kobject_add_dir(&mod->mkobj.kobj, "holders");
1245 if (!mod->holders_dir) { 1279 if (!mod->holders_dir) {
1246 err = -ENOMEM; 1280 err = -ENOMEM;
1247 goto out_unreg; 1281 goto out_unreg;
@@ -1261,11 +1295,9 @@ int mod_sysfs_setup(struct module *mod,
1261out_unreg_param: 1295out_unreg_param:
1262 module_param_sysfs_remove(mod); 1296 module_param_sysfs_remove(mod);
1263out_unreg_holders: 1297out_unreg_holders:
1264 kobject_unregister(mod->holders_dir); 1298 kobject_put(mod->holders_dir);
1265out_unreg: 1299out_unreg:
1266 kobject_del(&mod->mkobj.kobj);
1267 kobject_put(&mod->mkobj.kobj); 1300 kobject_put(&mod->mkobj.kobj);
1268out:
1269 return err; 1301 return err;
1270} 1302}
1271#endif 1303#endif
@@ -1274,9 +1306,20 @@ static void mod_kobject_remove(struct module *mod)
1274{ 1306{
1275 module_remove_modinfo_attrs(mod); 1307 module_remove_modinfo_attrs(mod);
1276 module_param_sysfs_remove(mod); 1308 module_param_sysfs_remove(mod);
1277 kobject_unregister(mod->mkobj.drivers_dir); 1309 kobject_put(mod->mkobj.drivers_dir);
1278 kobject_unregister(mod->holders_dir); 1310 kobject_put(mod->holders_dir);
1279 kobject_unregister(&mod->mkobj.kobj); 1311 kobject_put(&mod->mkobj.kobj);
1312}
1313
1314/*
1315 * link the module with the whole machine is stopped with interrupts off
1316 * - this defends against kallsyms not taking locks
1317 */
1318static int __link_module(void *_mod)
1319{
1320 struct module *mod = _mod;
1321 list_add(&mod->list, &modules);
1322 return 0;
1280} 1323}
1281 1324
1282/* 1325/*
@@ -1328,7 +1371,7 @@ void *__symbol_get(const char *symbol)
1328 1371
1329 preempt_disable(); 1372 preempt_disable();
1330 value = __find_symbol(symbol, &owner, &crc, 1); 1373 value = __find_symbol(symbol, &owner, &crc, 1);
1331 if (value && !strong_try_module_get(owner)) 1374 if (value && strong_try_module_get(owner) != 0)
1332 value = 0; 1375 value = 0;
1333 preempt_enable(); 1376 preempt_enable();
1334 1377
@@ -1369,7 +1412,7 @@ dup:
1369 return ret; 1412 return ret;
1370} 1413}
1371 1414
1372/* Change all symbols so that sh_value encodes the pointer directly. */ 1415/* Change all symbols so that st_value encodes the pointer directly. */
1373static int simplify_symbols(Elf_Shdr *sechdrs, 1416static int simplify_symbols(Elf_Shdr *sechdrs,
1374 unsigned int symindex, 1417 unsigned int symindex,
1375 const char *strtab, 1418 const char *strtab,
@@ -1882,16 +1925,16 @@ static struct module *load_module(void __user *umod,
1882 /* Now we've moved module, initialize linked lists, etc. */ 1925 /* Now we've moved module, initialize linked lists, etc. */
1883 module_unload_init(mod); 1926 module_unload_init(mod);
1884 1927
1885 /* Initialize kobject, so we can reference it. */ 1928 /* add kobject, so we can reference it. */
1886 err = mod_sysfs_init(mod); 1929 err = mod_sysfs_init(mod);
1887 if (err) 1930 if (err)
1888 goto cleanup; 1931 goto free_unload;
1889 1932
1890 /* Set up license info based on the info section */ 1933 /* Set up license info based on the info section */
1891 set_license(mod, get_modinfo(sechdrs, infoindex, "license")); 1934 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1892 1935
1893 if (strcmp(mod->name, "ndiswrapper") == 0) 1936 if (strcmp(mod->name, "ndiswrapper") == 0)
1894 add_taint(TAINT_PROPRIETARY_MODULE); 1937 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
1895 if (strcmp(mod->name, "driverloader") == 0) 1938 if (strcmp(mod->name, "driverloader") == 0)
1896 add_taint_module(mod, TAINT_PROPRIETARY_MODULE); 1939 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
1897 1940
@@ -2021,6 +2064,11 @@ static struct module *load_module(void __user *umod,
2021 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n", 2064 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2022 mod->name); 2065 mod->name);
2023 2066
2067 /* Now sew it into the lists so we can get lockdep and oops
2068 * info during argument parsing. Noone should access us, since
2069 * strong_try_module_get() will fail. */
2070 stop_machine_run(__link_module, mod, NR_CPUS);
2071
2024 /* Size of section 0 is 0, so this works well if no params */ 2072 /* Size of section 0 is 0, so this works well if no params */
2025 err = parse_args(mod->name, mod->args, 2073 err = parse_args(mod->name, mod->args,
2026 (struct kernel_param *) 2074 (struct kernel_param *)
@@ -2029,7 +2077,7 @@ static struct module *load_module(void __user *umod,
2029 / sizeof(struct kernel_param), 2077 / sizeof(struct kernel_param),
2030 NULL); 2078 NULL);
2031 if (err < 0) 2079 if (err < 0)
2032 goto arch_cleanup; 2080 goto unlink;
2033 2081
2034 err = mod_sysfs_setup(mod, 2082 err = mod_sysfs_setup(mod,
2035 (struct kernel_param *) 2083 (struct kernel_param *)
@@ -2037,7 +2085,7 @@ static struct module *load_module(void __user *umod,
2037 sechdrs[setupindex].sh_size 2085 sechdrs[setupindex].sh_size
2038 / sizeof(struct kernel_param)); 2086 / sizeof(struct kernel_param));
2039 if (err < 0) 2087 if (err < 0)
2040 goto arch_cleanup; 2088 goto unlink;
2041 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs); 2089 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2042 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs); 2090 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2043 2091
@@ -2052,9 +2100,13 @@ static struct module *load_module(void __user *umod,
2052 /* Done! */ 2100 /* Done! */
2053 return mod; 2101 return mod;
2054 2102
2055 arch_cleanup: 2103 unlink:
2104 stop_machine_run(__unlink_module, mod, NR_CPUS);
2056 module_arch_cleanup(mod); 2105 module_arch_cleanup(mod);
2057 cleanup: 2106 cleanup:
2107 kobject_del(&mod->mkobj.kobj);
2108 kobject_put(&mod->mkobj.kobj);
2109 free_unload:
2058 module_unload_free(mod); 2110 module_unload_free(mod);
2059 module_free(mod, mod->module_init); 2111 module_free(mod, mod->module_init);
2060 free_core: 2112 free_core:
@@ -2074,17 +2126,6 @@ static struct module *load_module(void __user *umod,
2074 goto free_hdr; 2126 goto free_hdr;
2075} 2127}
2076 2128
2077/*
2078 * link the module with the whole machine is stopped with interrupts off
2079 * - this defends against kallsyms not taking locks
2080 */
2081static int __link_module(void *_mod)
2082{
2083 struct module *mod = _mod;
2084 list_add(&mod->list, &modules);
2085 return 0;
2086}
2087
2088/* This is where the real work happens */ 2129/* This is where the real work happens */
2089asmlinkage long 2130asmlinkage long
2090sys_init_module(void __user *umod, 2131sys_init_module(void __user *umod,
@@ -2109,10 +2150,6 @@ sys_init_module(void __user *umod,
2109 return PTR_ERR(mod); 2150 return PTR_ERR(mod);
2110 } 2151 }
2111 2152
2112 /* Now sew it into the lists. They won't access us, since
2113 strong_try_module_get() will fail. */
2114 stop_machine_run(__link_module, mod, NR_CPUS);
2115
2116 /* Drop lock so they can recurse */ 2153 /* Drop lock so they can recurse */
2117 mutex_unlock(&module_mutex); 2154 mutex_unlock(&module_mutex);
2118 2155
@@ -2131,6 +2168,7 @@ sys_init_module(void __user *umod,
2131 mutex_lock(&module_mutex); 2168 mutex_lock(&module_mutex);
2132 free_module(mod); 2169 free_module(mod);
2133 mutex_unlock(&module_mutex); 2170 mutex_unlock(&module_mutex);
2171 wake_up(&module_wq);
2134 return ret; 2172 return ret;
2135 } 2173 }
2136 2174
@@ -2145,6 +2183,7 @@ sys_init_module(void __user *umod,
2145 mod->init_size = 0; 2183 mod->init_size = 0;
2146 mod->init_text_size = 0; 2184 mod->init_text_size = 0;
2147 mutex_unlock(&module_mutex); 2185 mutex_unlock(&module_mutex);
2186 wake_up(&module_wq);
2148 2187
2149 return 0; 2188 return 0;
2150} 2189}
@@ -2209,32 +2248,41 @@ static const char *get_ksymbol(struct module *mod,
2209 return mod->strtab + mod->symtab[best].st_name; 2248 return mod->strtab + mod->symtab[best].st_name;
2210} 2249}
2211 2250
2212/* For kallsyms to ask for address resolution. NULL means not found. 2251/* For kallsyms to ask for address resolution. NULL means not found. Careful
2213 We don't lock, as this is used for oops resolution and races are a 2252 * not to lock to avoid deadlock on oopses, simply disable preemption. */
2214 lesser concern. */ 2253char *module_address_lookup(unsigned long addr,
2215const char *module_address_lookup(unsigned long addr, 2254 unsigned long *size,
2216 unsigned long *size, 2255 unsigned long *offset,
2217 unsigned long *offset, 2256 char **modname,
2218 char **modname) 2257 char *namebuf)
2219{ 2258{
2220 struct module *mod; 2259 struct module *mod;
2260 const char *ret = NULL;
2221 2261
2262 preempt_disable();
2222 list_for_each_entry(mod, &modules, list) { 2263 list_for_each_entry(mod, &modules, list) {
2223 if (within(addr, mod->module_init, mod->init_size) 2264 if (within(addr, mod->module_init, mod->init_size)
2224 || within(addr, mod->module_core, mod->core_size)) { 2265 || within(addr, mod->module_core, mod->core_size)) {
2225 if (modname) 2266 if (modname)
2226 *modname = mod->name; 2267 *modname = mod->name;
2227 return get_ksymbol(mod, addr, size, offset); 2268 ret = get_ksymbol(mod, addr, size, offset);
2269 break;
2228 } 2270 }
2229 } 2271 }
2230 return NULL; 2272 /* Make a copy in here where it's safe */
2273 if (ret) {
2274 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2275 ret = namebuf;
2276 }
2277 preempt_enable();
2278 return (char *)ret;
2231} 2279}
2232 2280
2233int lookup_module_symbol_name(unsigned long addr, char *symname) 2281int lookup_module_symbol_name(unsigned long addr, char *symname)
2234{ 2282{
2235 struct module *mod; 2283 struct module *mod;
2236 2284
2237 mutex_lock(&module_mutex); 2285 preempt_disable();
2238 list_for_each_entry(mod, &modules, list) { 2286 list_for_each_entry(mod, &modules, list) {
2239 if (within(addr, mod->module_init, mod->init_size) || 2287 if (within(addr, mod->module_init, mod->init_size) ||
2240 within(addr, mod->module_core, mod->core_size)) { 2288 within(addr, mod->module_core, mod->core_size)) {
@@ -2244,12 +2292,12 @@ int lookup_module_symbol_name(unsigned long addr, char *symname)
2244 if (!sym) 2292 if (!sym)
2245 goto out; 2293 goto out;
2246 strlcpy(symname, sym, KSYM_NAME_LEN); 2294 strlcpy(symname, sym, KSYM_NAME_LEN);
2247 mutex_unlock(&module_mutex); 2295 preempt_enable();
2248 return 0; 2296 return 0;
2249 } 2297 }
2250 } 2298 }
2251out: 2299out:
2252 mutex_unlock(&module_mutex); 2300 preempt_enable();
2253 return -ERANGE; 2301 return -ERANGE;
2254} 2302}
2255 2303
@@ -2258,7 +2306,7 @@ int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2258{ 2306{
2259 struct module *mod; 2307 struct module *mod;
2260 2308
2261 mutex_lock(&module_mutex); 2309 preempt_disable();
2262 list_for_each_entry(mod, &modules, list) { 2310 list_for_each_entry(mod, &modules, list) {
2263 if (within(addr, mod->module_init, mod->init_size) || 2311 if (within(addr, mod->module_init, mod->init_size) ||
2264 within(addr, mod->module_core, mod->core_size)) { 2312 within(addr, mod->module_core, mod->core_size)) {
@@ -2271,12 +2319,12 @@ int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2271 strlcpy(modname, mod->name, MODULE_NAME_LEN); 2319 strlcpy(modname, mod->name, MODULE_NAME_LEN);
2272 if (name) 2320 if (name)
2273 strlcpy(name, sym, KSYM_NAME_LEN); 2321 strlcpy(name, sym, KSYM_NAME_LEN);
2274 mutex_unlock(&module_mutex); 2322 preempt_enable();
2275 return 0; 2323 return 0;
2276 } 2324 }
2277 } 2325 }
2278out: 2326out:
2279 mutex_unlock(&module_mutex); 2327 preempt_enable();
2280 return -ERANGE; 2328 return -ERANGE;
2281} 2329}
2282 2330
@@ -2285,7 +2333,7 @@ int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2285{ 2333{
2286 struct module *mod; 2334 struct module *mod;
2287 2335
2288 mutex_lock(&module_mutex); 2336 preempt_disable();
2289 list_for_each_entry(mod, &modules, list) { 2337 list_for_each_entry(mod, &modules, list) {
2290 if (symnum < mod->num_symtab) { 2338 if (symnum < mod->num_symtab) {
2291 *value = mod->symtab[symnum].st_value; 2339 *value = mod->symtab[symnum].st_value;
@@ -2294,12 +2342,12 @@ int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2294 KSYM_NAME_LEN); 2342 KSYM_NAME_LEN);
2295 strlcpy(module_name, mod->name, MODULE_NAME_LEN); 2343 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
2296 *exported = is_exported(name, mod); 2344 *exported = is_exported(name, mod);
2297 mutex_unlock(&module_mutex); 2345 preempt_enable();
2298 return 0; 2346 return 0;
2299 } 2347 }
2300 symnum -= mod->num_symtab; 2348 symnum -= mod->num_symtab;
2301 } 2349 }
2302 mutex_unlock(&module_mutex); 2350 preempt_enable();
2303 return -ERANGE; 2351 return -ERANGE;
2304} 2352}
2305 2353
@@ -2322,6 +2370,7 @@ unsigned long module_kallsyms_lookup_name(const char *name)
2322 unsigned long ret = 0; 2370 unsigned long ret = 0;
2323 2371
2324 /* Don't lock: we're in enough trouble already. */ 2372 /* Don't lock: we're in enough trouble already. */
2373 preempt_disable();
2325 if ((colon = strchr(name, ':')) != NULL) { 2374 if ((colon = strchr(name, ':')) != NULL) {
2326 *colon = '\0'; 2375 *colon = '\0';
2327 if ((mod = find_module(name)) != NULL) 2376 if ((mod = find_module(name)) != NULL)
@@ -2332,6 +2381,7 @@ unsigned long module_kallsyms_lookup_name(const char *name)
2332 if ((ret = mod_find_symname(mod, name)) != 0) 2381 if ((ret = mod_find_symname(mod, name)) != 0)
2333 break; 2382 break;
2334 } 2383 }
2384 preempt_enable();
2335 return ret; 2385 return ret;
2336} 2386}
2337#endif /* CONFIG_KALLSYMS */ 2387#endif /* CONFIG_KALLSYMS */
@@ -2353,21 +2403,30 @@ static void m_stop(struct seq_file *m, void *p)
2353 mutex_unlock(&module_mutex); 2403 mutex_unlock(&module_mutex);
2354} 2404}
2355 2405
2356static char *taint_flags(unsigned int taints, char *buf) 2406static char *module_flags(struct module *mod, char *buf)
2357{ 2407{
2358 int bx = 0; 2408 int bx = 0;
2359 2409
2360 if (taints) { 2410 if (mod->taints ||
2411 mod->state == MODULE_STATE_GOING ||
2412 mod->state == MODULE_STATE_COMING) {
2361 buf[bx++] = '('; 2413 buf[bx++] = '(';
2362 if (taints & TAINT_PROPRIETARY_MODULE) 2414 if (mod->taints & TAINT_PROPRIETARY_MODULE)
2363 buf[bx++] = 'P'; 2415 buf[bx++] = 'P';
2364 if (taints & TAINT_FORCED_MODULE) 2416 if (mod->taints & TAINT_FORCED_MODULE)
2365 buf[bx++] = 'F'; 2417 buf[bx++] = 'F';
2366 /* 2418 /*
2367 * TAINT_FORCED_RMMOD: could be added. 2419 * TAINT_FORCED_RMMOD: could be added.
2368 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't 2420 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2369 * apply to modules. 2421 * apply to modules.
2370 */ 2422 */
2423
2424 /* Show a - for module-is-being-unloaded */
2425 if (mod->state == MODULE_STATE_GOING)
2426 buf[bx++] = '-';
2427 /* Show a + for module-is-being-loaded */
2428 if (mod->state == MODULE_STATE_COMING)
2429 buf[bx++] = '+';
2371 buf[bx++] = ')'; 2430 buf[bx++] = ')';
2372 } 2431 }
2373 buf[bx] = '\0'; 2432 buf[bx] = '\0';
@@ -2394,7 +2453,7 @@ static int m_show(struct seq_file *m, void *p)
2394 2453
2395 /* Taints info */ 2454 /* Taints info */
2396 if (mod->taints) 2455 if (mod->taints)
2397 seq_printf(m, " %s", taint_flags(mod->taints, buf)); 2456 seq_printf(m, " %s", module_flags(mod, buf));
2398 2457
2399 seq_printf(m, "\n"); 2458 seq_printf(m, "\n");
2400 return 0; 2459 return 0;
@@ -2489,97 +2548,12 @@ void print_modules(void)
2489 2548
2490 printk("Modules linked in:"); 2549 printk("Modules linked in:");
2491 list_for_each_entry(mod, &modules, list) 2550 list_for_each_entry(mod, &modules, list)
2492 printk(" %s%s", mod->name, taint_flags(mod->taints, buf)); 2551 printk(" %s%s", mod->name, module_flags(mod, buf));
2552 if (last_unloaded_module[0])
2553 printk(" [last unloaded: %s]", last_unloaded_module);
2493 printk("\n"); 2554 printk("\n");
2494} 2555}
2495 2556
2496#ifdef CONFIG_SYSFS
2497static char *make_driver_name(struct device_driver *drv)
2498{
2499 char *driver_name;
2500
2501 driver_name = kmalloc(strlen(drv->name) + strlen(drv->bus->name) + 2,
2502 GFP_KERNEL);
2503 if (!driver_name)
2504 return NULL;
2505
2506 sprintf(driver_name, "%s:%s", drv->bus->name, drv->name);
2507 return driver_name;
2508}
2509
2510static void module_create_drivers_dir(struct module_kobject *mk)
2511{
2512 if (!mk || mk->drivers_dir)
2513 return;
2514
2515 mk->drivers_dir = kobject_add_dir(&mk->kobj, "drivers");
2516}
2517
2518void module_add_driver(struct module *mod, struct device_driver *drv)
2519{
2520 char *driver_name;
2521 int no_warn;
2522 struct module_kobject *mk = NULL;
2523
2524 if (!drv)
2525 return;
2526
2527 if (mod)
2528 mk = &mod->mkobj;
2529 else if (drv->mod_name) {
2530 struct kobject *mkobj;
2531
2532 /* Lookup built-in module entry in /sys/modules */
2533 mkobj = kset_find_obj(&module_subsys, drv->mod_name);
2534 if (mkobj) {
2535 mk = container_of(mkobj, struct module_kobject, kobj);
2536 /* remember our module structure */
2537 drv->mkobj = mk;
2538 /* kset_find_obj took a reference */
2539 kobject_put(mkobj);
2540 }
2541 }
2542
2543 if (!mk)
2544 return;
2545
2546 /* Don't check return codes; these calls are idempotent */
2547 no_warn = sysfs_create_link(&drv->kobj, &mk->kobj, "module");
2548 driver_name = make_driver_name(drv);
2549 if (driver_name) {
2550 module_create_drivers_dir(mk);
2551 no_warn = sysfs_create_link(mk->drivers_dir, &drv->kobj,
2552 driver_name);
2553 kfree(driver_name);
2554 }
2555}
2556EXPORT_SYMBOL(module_add_driver);
2557
2558void module_remove_driver(struct device_driver *drv)
2559{
2560 struct module_kobject *mk = NULL;
2561 char *driver_name;
2562
2563 if (!drv)
2564 return;
2565
2566 sysfs_remove_link(&drv->kobj, "module");
2567
2568 if (drv->owner)
2569 mk = &drv->owner->mkobj;
2570 else if (drv->mkobj)
2571 mk = drv->mkobj;
2572 if (mk && mk->drivers_dir) {
2573 driver_name = make_driver_name(drv);
2574 if (driver_name) {
2575 sysfs_remove_link(mk->drivers_dir, driver_name);
2576 kfree(driver_name);
2577 }
2578 }
2579}
2580EXPORT_SYMBOL(module_remove_driver);
2581#endif
2582
2583#ifdef CONFIG_MODVERSIONS 2557#ifdef CONFIG_MODVERSIONS
2584/* Generate the signature for struct module here, too, for modversions. */ 2558/* Generate the signature for struct module here, too, for modversions. */
2585void struct_module(struct module *mod) { return; } 2559void struct_module(struct module *mod) { return; }