diff options
author | Tejun Heo <tj@kernel.org> | 2010-02-05 03:57:02 -0500 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@suse.de> | 2010-03-07 20:04:46 -0500 |
commit | 77d3d7c1d561f49f755d7390f0764dff90765974 (patch) | |
tree | 4a510d5dedd5681fb76353bbaae6d622452fd749 /drivers/base/core.c | |
parent | 25cf84cf377c0aae5dbcf937ea89bc7893db5176 (diff) |
driver-core: fix race condition in get_device_parent()
sysfs is creating several devices in cuse class concurrently and with
CONFIG_SYSFS_DEPRECATED turned off, it triggers the following oops.
BUG: unable to handle kernel NULL pointer dereference at 0000000000000038
IP: [<ffffffff81158b0a>] sysfs_addrm_start+0x4a/0xf0
PGD 75bb067 PUD 75be067 PMD 0
Oops: 0000 [#1] PREEMPT SMP
last sysfs file: /sys/devices/system/cpu/cpu7/topology/core_siblings
CPU 1
Modules linked in: cuse fuse
Pid: 4737, comm: osspd Not tainted 2.6.31-work #77
RIP: 0010:[<ffffffff81158b0a>] [<ffffffff81158b0a>] sysfs_addrm_start+0x4a/0xf0
RSP: 0018:ffff88000042f8f8 EFLAGS: 00010296
RAX: ffff88000042ffd8 RBX: 0000000000000000 RCX: 0000000000000000
RDX: 0000000000000000 RSI: ffff880007eef660 RDI: 0000000000000001
RBP: ffff88000042f918 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000001 R11: ffffffff81158b0a R12: ffff88000042f928
R13: 00000000fffffff4 R14: 0000000000000000 R15: ffff88000042f9a0
FS: 00007fe93905a950(0000) GS:ffff880008600000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 000000008005003b
CR2: 0000000000000038 CR3: 00000000077c9000 CR4: 00000000000006e0
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000ffff0ff0 DR7: 0000000000000400
Process osspd (pid: 4737, threadinfo ffff88000042e000, task ffff880007eef040)
Stack:
ffff880005da10e8 0000000011cc8d6e ffff88000042f928 ffff880003d28a28
<0> ffff88000042f988 ffffffff811592d7 0000000000000000 0000000000000000
<0> 0000000000000000 0000000000000000 ffff88000042f958 0000000011cc8d6e
Call Trace:
[<ffffffff811592d7>] create_dir+0x67/0xe0
[<ffffffff811593a8>] sysfs_create_dir+0x58/0xb0
[<ffffffff8128ca7c>] ? kobject_add_internal+0xcc/0x220
[<ffffffff812942e1>] ? vsnprintf+0x3c1/0xb90
[<ffffffff8128cab7>] kobject_add_internal+0x107/0x220
[<ffffffff8128cd37>] kobject_add_varg+0x47/0x80
[<ffffffff8128ce53>] kobject_add+0x53/0x90
[<ffffffff81357d84>] device_add+0xd4/0x690
[<ffffffff81356c2b>] ? dev_set_name+0x4b/0x70
[<ffffffffa001a884>] cuse_process_init_reply+0x2b4/0x420 [cuse]
...
The problem is that kobject_add_internal() first adds a kobject to the
kset and then try to create sysfs directory for it. If the creation
fails, it remove the kobject from the kset. get_device_parent()
accesses class_dirs kset while only holding class_dirs.list_lock to
see whether the cuse class dir exists. But when it exists, it may not
have finished initialization yet or may fail and get removed soon. In
the above case, the former happened so the second one ends up trying
to create subdirectory under NULL sysfs_dirent.
Fix it by grabbing a mutex in get_device_parent().
Signed-off-by: Tejun Heo <tj@kernel.org>
Reported-by: Colin Guthrie <cguthrie@mandriva.org>
Cc: stable <stable@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers/base/core.c')
-rw-r--r-- | drivers/base/core.c | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/drivers/base/core.c b/drivers/base/core.c index 282025770429..fb4bc4f5151c 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c | |||
@@ -607,6 +607,7 @@ static struct kobject *get_device_parent(struct device *dev, | |||
607 | int retval; | 607 | int retval; |
608 | 608 | ||
609 | if (dev->class) { | 609 | if (dev->class) { |
610 | static DEFINE_MUTEX(gdp_mutex); | ||
610 | struct kobject *kobj = NULL; | 611 | struct kobject *kobj = NULL; |
611 | struct kobject *parent_kobj; | 612 | struct kobject *parent_kobj; |
612 | struct kobject *k; | 613 | struct kobject *k; |
@@ -623,6 +624,8 @@ static struct kobject *get_device_parent(struct device *dev, | |||
623 | else | 624 | else |
624 | parent_kobj = &parent->kobj; | 625 | parent_kobj = &parent->kobj; |
625 | 626 | ||
627 | mutex_lock(&gdp_mutex); | ||
628 | |||
626 | /* find our class-directory at the parent and reference it */ | 629 | /* find our class-directory at the parent and reference it */ |
627 | spin_lock(&dev->class->p->class_dirs.list_lock); | 630 | spin_lock(&dev->class->p->class_dirs.list_lock); |
628 | list_for_each_entry(k, &dev->class->p->class_dirs.list, entry) | 631 | list_for_each_entry(k, &dev->class->p->class_dirs.list, entry) |
@@ -631,20 +634,26 @@ static struct kobject *get_device_parent(struct device *dev, | |||
631 | break; | 634 | break; |
632 | } | 635 | } |
633 | spin_unlock(&dev->class->p->class_dirs.list_lock); | 636 | spin_unlock(&dev->class->p->class_dirs.list_lock); |
634 | if (kobj) | 637 | if (kobj) { |
638 | mutex_unlock(&gdp_mutex); | ||
635 | return kobj; | 639 | return kobj; |
640 | } | ||
636 | 641 | ||
637 | /* or create a new class-directory at the parent device */ | 642 | /* or create a new class-directory at the parent device */ |
638 | k = kobject_create(); | 643 | k = kobject_create(); |
639 | if (!k) | 644 | if (!k) { |
645 | mutex_unlock(&gdp_mutex); | ||
640 | return NULL; | 646 | return NULL; |
647 | } | ||
641 | k->kset = &dev->class->p->class_dirs; | 648 | k->kset = &dev->class->p->class_dirs; |
642 | retval = kobject_add(k, parent_kobj, "%s", dev->class->name); | 649 | retval = kobject_add(k, parent_kobj, "%s", dev->class->name); |
643 | if (retval < 0) { | 650 | if (retval < 0) { |
651 | mutex_unlock(&gdp_mutex); | ||
644 | kobject_put(k); | 652 | kobject_put(k); |
645 | return NULL; | 653 | return NULL; |
646 | } | 654 | } |
647 | /* do not emit an uevent for this simple "glue" directory */ | 655 | /* do not emit an uevent for this simple "glue" directory */ |
656 | mutex_unlock(&gdp_mutex); | ||
648 | return k; | 657 | return k; |
649 | } | 658 | } |
650 | 659 | ||