1 module: Fix a few concurrent module loading races
3 Concurrently starting multiple OSTs on a single OSS frequently
4 triggers 30s deadlocks on module_mutex. This RHEL 6 kernel bug
5 applies to any module that results in additional request_module()
6 calls in its init callback. In Lustre, at least ptlrpc and libcfs are
7 affected. This patch adapts fixes from the following upstream
10 9bea7f2 module: fix bne2 "gave up waiting for init of module
12 be593f4 module: verify_export_symbols under the lock
13 3bafeb6 module: move find_module check to end
14 80a3d1b module: move sysfs exposure to end of load_module
16 diff -rNup linux-2.6.32-431.20.3.el6.orig/kernel/module.c linux-2.6.32-431.20.3.el6/kernel/module.c
17 --- linux-2.6.32-431.20.3.el6.orig/kernel/module.c 2014-06-07 05:42:39.000000000 +0800
18 +++ linux-2.6.32-431.20.3.el6/kernel/module.c 2014-07-28 00:04:43.000000000 +0800
19 @@ -658,6 +658,33 @@ static int already_uses(struct module *a
23 +static int ref_module(struct module *a, struct module *b)
25 + struct module_use *use;
28 + if (b == NULL || already_uses(a, b))
31 + /* If module isn't available, we fail. */
32 + err = strong_try_module_get(b);
36 + DEBUGP("Allocating new usage for %s.\n", a->name);
37 + use = kmalloc(sizeof(*use), GFP_ATOMIC);
39 + printk("%s: out of memory loading\n", a->name);
44 + use->module_which_uses = a;
45 + list_add(&use->list, &b->modules_which_use_me);
49 +/* Module a uses b */
50 int use_module(struct module *a, struct module *b)
52 struct module_use *use;
53 @@ -707,7 +734,6 @@ static void module_unload_free(struct mo
57 - sysfs_remove_link(i->holders_dir, mod->name);
58 /* There can be at most one match. */
61 @@ -962,6 +988,11 @@ static inline void module_unload_free(st
65 +static inline int ref_module(struct module *a, struct module *b)
67 + return strong_try_module_get(b);
70 int use_module(struct module *a, struct module *b)
72 return strong_try_module_get(b) == 0;
73 @@ -1130,24 +1161,60 @@ static inline int same_magic(const char
74 static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
75 unsigned int versindex,
82 const struct kernel_symbol *sym;
83 const unsigned long *crc;
86 + mutex_lock(&module_mutex);
87 sym = find_symbol(name, &owner, &crc,
88 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
89 - /* use_module can fail due to OOM,
90 - or module initialization or unloading */
92 - if (!check_version(sechdrs, versindex, name, mod, crc, owner)
93 - || !use_module(mod, owner))
98 + if (!check_version(sechdrs, versindex, name, mod, crc, owner)) {
99 + sym = ERR_PTR(-EINVAL);
103 + err = ref_module(mod, owner);
105 + sym = ERR_PTR(err);
110 + /* We must make copy under the lock if we failed to get ref. */
111 + strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
113 + mutex_unlock(&module_mutex);
117 +static const struct kernel_symbol *resolve_symbol_wait(Elf_Shdr *sechdrs,
118 + unsigned int versindex,
120 + struct module *mod)
122 + const struct kernel_symbol *ksym;
123 + char ownername[MODULE_NAME_LEN];
125 + mutex_unlock(&module_mutex);
126 + if (wait_event_interruptible_timeout(module_wq,
127 + !IS_ERR(ksym = resolve_symbol(sechdrs, versindex, name,
128 + mod, ownername)) ||
129 + PTR_ERR(ksym) != -EBUSY,
130 + msecs_to_jiffies(30 * MSEC_PER_SEC)) <= 0) {
131 + printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
132 + mod->name, ownername);
134 + mutex_lock(&module_mutex);
139 * /sys/module/foo/sections stuff
140 * J. Corbet <corbet@lwn.net>
141 @@ -1375,6 +1442,30 @@ static inline void remove_notes_attrs(st
145 +static void add_usage_links(struct module *mod)
147 +#ifdef CONFIG_MODULE_UNLOAD
148 + struct module_use *use;
151 + list_for_each_entry(use, &mod->modules_which_use_me, list) {
152 + nowarn = sysfs_create_link(use->module_which_uses->holders_dir,
153 + &mod->mkobj.kobj, mod->name);
158 +static void del_usage_links(struct module *mod)
160 +#ifdef CONFIG_MODULE_UNLOAD
161 + struct module_use *use;
163 + list_for_each_entry(use, &mod->modules_which_use_me, list)
164 + sysfs_remove_link(use->module_which_uses->holders_dir, mod->name);
169 int module_add_modinfo_attrs(struct module *mod)
171 struct module_attribute *attr;
172 @@ -1456,6 +1547,10 @@ int mod_sysfs_setup(struct module *mod,
176 + err = mod_sysfs_init(mod);
180 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
181 if (!mod->holders_dir) {
183 @@ -1470,6 +1565,8 @@ int mod_sysfs_setup(struct module *mod,
185 goto out_unreg_param;
187 + add_usage_links(mod);
189 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
192 @@ -1479,6 +1576,7 @@ out_unreg_holders:
193 kobject_put(mod->holders_dir);
195 kobject_put(&mod->mkobj.kobj);
200 @@ -1493,10 +1591,15 @@ static void mod_sysfs_fini(struct module
204 +static void del_usage_links(struct module *mod)
208 #endif /* CONFIG_SYSFS */
210 static void mod_kobject_remove(struct module *mod)
212 + del_usage_links(mod);
213 module_remove_modinfo_attrs(mod);
214 module_param_sysfs_remove(mod);
215 kobject_put(mod->mkobj.drivers_dir);
216 @@ -1576,6 +1679,8 @@ EXPORT_SYMBOL_GPL(__symbol_get);
218 * Ensure that an exported symbol [global namespace] does not already exist
219 * in the kernel or in some other module's exported symbol table.
221 + * You must hold the module_mutex.
223 static int verify_export_symbols(struct module *mod)
225 @@ -1641,21 +1746,23 @@ static int simplify_symbols(Elf_Shdr *se
229 - ksym = resolve_symbol(sechdrs, versindex,
230 - strtab + sym[i].st_name, mod);
231 + ksym = resolve_symbol_wait(sechdrs, versindex,
232 + strtab + sym[i].st_name,
234 /* Ok if resolved. */
236 + if (ksym && !IS_ERR(ksym)) {
237 sym[i].st_value = ksym->value;
242 - if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
243 + if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
246 - printk(KERN_WARNING "%s: Unknown symbol %s\n",
247 - mod->name, strtab + sym[i].st_name);
249 + printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
250 + mod->name, strtab + sym[i].st_name,
252 + ret = PTR_ERR(ksym) ?: -ENOENT;
256 @@ -2240,11 +2347,6 @@ static noinline struct module *load_modu
260 - if (find_module(mod->name)) {
265 mod->state = MODULE_STATE_COMING;
267 /* Allow arches to frob section contents and sizes. */
268 @@ -2338,11 +2440,6 @@ static noinline struct module *load_modu
269 /* Now we've moved module, initialize linked lists, etc. */
270 module_unload_init(mod);
272 - /* add kobject, so we can reference it. */
273 - err = mod_sysfs_init(mod);
277 /* Set up license info based on the info section */
278 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
280 @@ -2461,11 +2558,6 @@ static noinline struct module *load_modu
284 - /* Find duplicate symbols */
285 - err = verify_export_symbols(mod);
289 /* Set up and sort exception table */
290 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
291 sizeof(*mod->extable), &mod->num_exentries);
292 @@ -2521,6 +2613,16 @@ static noinline struct module *load_modu
293 * function to insert in a way safe to concurrent readers.
294 * The mutex protects against concurrent writers.
296 + if (find_module(mod->name)) {
298 + goto already_exists;
301 + /* Find duplicate symbols */
302 + err = verify_export_symbols(mod);
304 + goto already_exists;
306 list_add_rcu(&mod->list, &modules);
308 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
309 @@ -2530,6 +2632,7 @@ static noinline struct module *load_modu
310 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
314 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
315 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
317 @@ -2544,15 +2647,13 @@ static noinline struct module *load_modu
319 /* Unlink carefully: kallsyms could be walking list. */
320 list_del_rcu(&mod->list);
323 module_arch_cleanup(mod);
325 dynamic_debug_remove(debug);
328 - kobject_del(&mod->mkobj.kobj);
329 - kobject_put(&mod->mkobj.kobj);
331 module_unload_free(mod);
332 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
333 percpu_modfree(mod->refptr);