sysfs-related oops during module unload (2.6.16-rc2)
If the refcnt attribute of a module is open when the module is unloaded, we get an oops when the file is closed. I used ide_cd for this report but I don't think the oops is caused by the driver itself. This bug seems to be restricted to the /sys/module hierarchy; it doesn't happen with /sys/class etc.
I suspect it's an extra put or a missing get somewhere, but the fix isn't obvious to me after looking at it for a little while, so I'm punting.
I'm pretty sure this happens with 2.6.15; I can double-check if needed.
Testcase:
#!/bin/sh
tail -f /sys/module/ide_cd/refcnt > /dev/null &
sleep 1
modprobe -r ide_cd
sleep 2
kill $!
Messages and oops with CONFIG_DEBUG_KOBJECT=y, starting from the time of modprobe -r:
kobject iosched: unregistering
kobject_uevent
kobject iosched: cleaning up
kobject queue: unregistering
kobject_uevent
kobject queue: cleaning up
kobject_uevent
fill_kobj_path: path = '/block/hdc'
fill_kobj_path: path = '/devices/pci0000:00/0000:00:1f.1/ide1/1.0' kobject hdc: cleaning up
kobject ide-cdrom: unregistering
kobject_uevent
fill_kobj_path: path = '/bus/ide/drivers/ide-cdrom' kobject ide-cdrom: cleaning up
kobject ide_cd: unregistering
kobject_uevent
fill_kobj_path: path = '/module/ide_cd'
Uniform CD-ROM driver unloaded
kobject cdrom: unregistering
kobject_uevent
fill_kobj_path: path = '/module/cdrom'
kobject cdrom: cleaning up
Unable to handle kernel paging request at virtual address c0f62b60
printing eip:
781af1f8
*pde = 45214067
Oops: 0002 [#1]
SMP
Modules linked in: speedstep_lib cpufreq_userspace cpufreq_stats freq_table cpufreq_powersave cpufreq_ondemand cpufreq_conservative nfsd exportfs lockd sunrpc autofs4 video container button battery ac floppy rtc pcspkr tsdev 3c59x snd_cs46xx gameport snd_rawmidi snd_seq_device snd_ac97_codec snd_ac97_bus snd_pcm_oss snd_mixer_oss snd_pcm snd_timer snd soundcore snd_page_alloc 8139cp usbhid 8139too mii uhci_hcd usbcore hw_random intel_agp agpgart psmouse parport_pc lp parport jfs ext3 jbd unix thermal processor fan
CPU: 1
EIP: 0060:[<781af1f8>] Not tainted VLI
EFLAGS: 00010216 (2.6.16-rc2 #2)
EIP is at kref_put+0x37/0x57
eax: c0f62b60 ebx: c0f62b60 ecx: b839fc18 edx: 781aeb38
esi: 781aeb38 edi: b13c744c ebp: aff82e0c esp: aff82e04
ds: 007b es: 007b ss: 0068
Process tail (pid: 5536, threadinfo=aff82000 task=af6b8bd0)
Stack: <0>00000000 c0f62b48 aff82e14 781aeb59 aff82e28 7818c41d 00000010 bb8bb840
af7a16e8 aff82e48 781575b1 bb8bb840 bff90e38 b839fc18 af7a16e8 bd8cea1c
00000000 aff82e50 781574fb aff82e64 7815616a 00000001 0000000c b13c757c
Call Trace:
[<78103a68>] show_stack_log_lvl+0xa8/0xb0
[<78103ba0>] show_registers+0x10a/0x174
[<78103d7a>] die+0xfb/0x16f
[<78295744>] do_page_fault+0x370/0x522
[<78103713>] error_code+0x4f/0x54
[<781aeb59>] kobject_put+0x14/0x16
[<7818c41d>] sysfs_release+0x2c/0x76
[<781575b1>] __fput+0xb4/0x151
[<781574fb>] fput+0x17/0x19
[<7815616a>] filp_close+0x4e/0x58
[<7811fb88>] close_files+0x57/0x67
[<7811fbde>] put_files_struct+0x18/0x3e
[<7812059f>] do_exit+0x1bc/0x35b
[<78120803>] sys_exit_group+0x0/0x11
[<7812847c>] get_signal_to_deliver+0x253/0x27b
[<78102a2c>] do_signal+0x5f/0x10a
[<78102b04>] do_notify_resume+0x2d/0x3d
[<78102ca2>] work_notifysig+0x13/0x19
Code: 04 6a 34 eb 0a 81 fa ed 41 15 78 75 1e 6a 35 68 1c 17 2b 78 68 34 cd 29 78 68 53 47 2a 78 e8 6e f0 f6 ff e8 8a 48 f5 ff 83 c4 10 <f0> ff 0b 0f 94 c0 31 d2 84 c0 74 09 89 d8 ff d6 ba 01 00 00 00
<1>Fixing recursive fault but reboot is needed!Quoted 6 lines · Nathan Lynch
If the refcnt attribute of a module is open when the module is unloaded, we get an oops when the file is closed. I used
ide_cdfor this report but I don't think the oops is caused by the driver itself. This bug seems to be restricted to the/sys/modulehierarchy; it doesn't happen with/sys/classetc.
Quoted 6 lines
I suspect it's an extra put or a missing get somewhere, but the fix isn't obvious to me after looking at it for a little while, so I'm punting.
I'm pretty sure this happens with 2.6.15; I can double-check if needed.
Ugh, we aren't setting the owner of these fields properly, good catch.
Does the patch below (built tested only), solve this for you?
thanks,
greg k-h
-----------------
kernel/module.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/module.c +3 -1
@@ -1085,8 +1085,10 @@for (i =0 ; (attr = modinfo_attrs[i]) && !error; i++) {if (!attr->test || - (attr->test && attr->test (mod))) + (attr->test && attr->test (mod))) { + attr->attr.owner = mod; error =sysfs_create_file (&mod->mkobj.kobj,&attr->attr); + } }return error; }
Quoted 11 lines · Greg KH
Quoted 10 lines · Nathan Lynch
If the refcnt attribute of a module is open when the module is unloaded, we get an oops when the file is closed. I used
ide_cdfor this report but I don't think the oops is caused by the driver itself. This bug seems to be restricted to the/sys/modulehierarchy; it doesn't happen with/sys/classetc.I suspect it's an extra put or a missing get somewhere, but the fix isn't obvious to me after looking at it for a little while, so I'm punting.
Quoted 6 lines
Quoted 2 lines
I'm pretty sure this happens with 2.6.15; I can double-check if needed.
Ugh, we aren't setting the owner of these fields properly, good catch.
Does the patch below (built tested only), solve this for you?
Thanks, but no, I get the same oops. The refcnt attribute isn't part of the modinfo_attrs array.
Quoted 17 lines
kernel/module.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)kernel/module.c +3
-1@@ -1085,8 +1085,10 @@for (i =0 ; (attr = modinfo_attrs[i]) && !error; i++) {if (!attr->test || - (attr->test && attr->test (mod))) + (attr->test && attr->test (mod))) { + attr->attr.owner = mod; error =sysfs_create_file (&mod->mkobj.kobj,&attr->attr); + } }return error; }
Quoted 15 lines · Nathan Lynch
Quoted 14 lines · Greg KH
Quoted 12 lines · Nathan Lynch
If the refcnt attribute of a module is open when the module is unloaded, we get an oops when the file is closed. I used
ide_cdfor this report but I don't think the oops is caused by the driver itself. This bug seems to be restricted to the/sys/modulehierarchy; it doesn't happen with/sys/classetc.I suspect it's an extra put or a missing get somewhere, but the fix isn't obvious to me after looking at it for a little while, so I'm punting.
I'm pretty sure this happens with 2.6.15; I can double-check if needed.
Quoted 6 lines
Quoted 3 lines
Ugh, we aren't setting the owner of these fields properly, good catch.
Does the patch below (built tested only), solve this for you?
Thanks, but no, I get the same oops. The refcnt attribute isn't part of the
modinfo_attrsarray.
Ah, crap, you're right. We really need to dynamically create these attributes for every module to get the owner right. That will be a bigger patch that I'll work on on Monday...
thanks for testing,
greg k-h
Quoted 18 lines · Greg KH
Quoted 17 lines · Nathan Lynch
Quoted 16 lines · Greg KH
Quoted 6 lines · Nathan Lynch
If the refcnt attribute of a module is open when the module is unloaded, we get an oops when the file is closed. I used
ide_cdfor this report but I don't think the oops is caused by the driver itself. This bug seems to be restricted to the/sys/modulehierarchy; it doesn't happen with/sys/classetc.Quoted 6 lines
I suspect it's an extra put or a missing get somewhere, but the fix isn't obvious to me after looking at it for a little while, so I'm punting.
I'm pretty sure this happens with 2.6.15; I can double-check if needed.
Ugh, we aren't setting the owner of these fields properly, good catch.
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Does the patch below (built tested only), solve this for you?
Thanks, but no, I get the same oops. The refcnt attribute isn't part of the
modinfo_attrsarray.Ah, crap, you're right. We really need to dynamically create these attributes for every module to get the owner right. That will be a bigger patch that I'll work on on Monday...
Ok, turns out the code was trying to increment the module reference count correctly, but it wasn't working right at all. And we were not showing a few things in sysfs if module unload was not selected, which isn't right.
So here's a patch that fixes all of this, and your original problem. Bonus is that it actually removes more code than it adds :)
Can you test it out to verify that it works for you?
thanks,
greg k-h
--- include/linux/module.h | 1 kernel/module.c | 77 +++++++++++++++++++------------------------------ kernel/params.c | 10 ------ 3 files changed, 32 insertions(+), 56 deletions(-)
include/linux/module.h +1 -0
@@ -242,6 +242,7 @@ struct module/* Sysfs stuff. */ struct module_kobject mkobj;struct module_param_attrs *param_attrs; +struct module_attribute *modinfo_attrs;const char *version;const char *srcversion;
kernel/module.c +31 -46
158 lines not on this page · raw message
kernel/params.c +0 -10
@@ -638,13 +638,8 @@ static ssize_t module_attr_show(struct kif (!attribute->show)return -EIO ; -if (!try_module_get (mk->mod)) -return -ENODEV ; - ret = attribute->show (attribute, mk->mod, buf); -module_put (mk->mod); -return ret; }
@@ -662,13 +657,8 @@ static ssize_t module_attr_store(structif (!attribute->store)return -EIO ; -if (!try_module_get (mk->mod)) -return -ENODEV ; - ret = attribute->store (attribute, mk->mod, buf, len); -module_put (mk->mod); -return ret; }
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 7 lines · Dmitry Torokhov
Quoted 4 lines · Greg KH
+???????mod->modinfo_attrs = kzalloc((sizeof(struct
module_attribute) * +???????????????????????????????????????(ARRAY_SIZE(modinfo_attrs) + 1)), +???????????????????????????????????????GFP_KERNEL);
kcalloc()perhaps? Here you explecitely create n elements of a given size...
Heh, sure, the one time I actually are creating n elements :)
I'll go change that.
thanks for pointing it out.
greg k-h
Quoted 9 lines · Greg KH
Quoted 8 lines · Greg KH
Quoted 7 lines · Nathan Lynch
Quoted 6 lines · Greg KH
Quoted 5 lines · Nathan Lynch
If the refcnt attribute of a module is open when the module is unloaded, we get an oops when the file is closed. I used
ide_cdfor this report but I don't think the oops is caused by the driver itself. This bug seems to be restricted to the/sys/modulehierarchy; it doesn't happen with/sys/classetc.
<snip>
Quoted 10 lines
Ok, turns out the code was trying to increment the module reference count correctly, but it wasn't working right at all. And we were not showing a few things in sysfs if module unload was not selected, which isn't right.
So here's a patch that fixes all of this, and your original problem. Bonus is that it actually removes more code than it adds :)
Can you test it out to verify that it works for you?
Sorry for the delay.
Tested against 2.6.16-rc4-ish, and it seems to do the right thing -- modprobe -r says the module is busy while the refcnt attribute is open. The module is allowed to unload once the file is closed.
I didn't verify the other stuff your patch changes, though.
Thanks.
Nathan
Quoted 5 lines · Nathan Lynch
Sorry for the delay.
Tested against 2.6.16-rc4-ish, and it seems to do the right thing -- modprobe -r says the module is busy while the refcnt attribute is open. The module is allowed to unload once the file is closed.
Great, thanks for trying it out and letting me know.
Quoted 1 line
I didn't verify the other stuff your patch changes, though.
Heh, well if it all still works, that's pretty much proof that the other stuff was correct too :)
thanks,
greg k-h
Quoted 8 lines · Greg KH
Quoted 5 lines · Nathan Lynch
Sorry for the delay.
Tested against 2.6.16-rc4-ish, and it seems to do the right thing -- modprobe -r says the module is busy while the refcnt attribute is open. The module is allowed to unload once the file is closed.
Great, thanks for trying it out and letting me know.
Sure -- do you plan to push this for 2.6.16?
The reason I ask is that the refcnt attribute is world-readable, so a malicious or silly user can keep the file open until an unwitting superuser unloads a module...
Far-fetched, I suppose, but I just wanted to make this scenario clear.
Nathan
Quoted 11 lines · Nathan Lynch
Quoted 8 lines · Greg KH
Quoted 5 lines · Nathan Lynch
Sorry for the delay.
Tested against 2.6.16-rc4-ish, and it seems to do the right thing -- modprobe -r says the module is busy while the refcnt attribute is open. The module is allowed to unload once the file is closed.
Great, thanks for trying it out and letting me know.
Sure -- do you plan to push this for 2.6.16?
No.
Quoted 5 lines
The reason I ask is that the refcnt attribute is world-readable, so a malicious or silly user can keep the file open until an unwitting superuser unloads a module...
Far-fetched, I suppose, but I just wanted to make this scenario clear.
It's been like this for a number of kernel versions now, and module unloading is a rare thing to happen (no tools do it automatically.) So I don't think it's worth 2.6.16 material. Let it sit in -mm for a bit to verify that I didn't break anything else, and I'll send it in for 2.6.17.
thanks,
greg k-h