Skip to contentSkip to conversation

fix race condition between device_del and device_add

PATCH EunTaik Lee 2 participants 2 messages Mar 30, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw
Quoted 4 lines

The bus that the device is on should prevent this, why isn't that working for you? What type of device/bus do you see this problem on?

The device is a firmware class device on a virtual bus.

Quoted 4 lines
Quoted 1 line
Signed-off-by eun.taik.lee

We need a "real" name here, I don't think your name has '.' in it, right?

Right. It should've been Eun Taik Lee.

Quoted 2 lines

Your email client turned all tabs to spaces and made the patch unable to be applied.

I think I pasted the patch on HTML mode and then switched to text mode. Below patch should be okay.

From: "Eun Taik Lee" <[email protected]>

There is a possible race condition when a device is added while another device with the same parent , with ref count of one, is deleted.

	CPU0				CPU1
device_add()			device_del()
get_device_parent()		put_device(parent);
kobj = kobject_get(k)		kobject_put()
				kref_put()
				refcount=0

refcount is 0
WARNS it was 0 but
return kobj frees kobj
uses the freed parent kobj

The race condition exists because kref_put() and the release function is not atomic.
Using kobject_get_unless_zero() instead of kobject_get() in get_device_parent() will prevent this race condition.

---
 drivers/base/core.c     |    2 +-
 include/linux/kobject.h |    2 ++
 lib/kobject.c           |    2 +-
 3 files changed, 4 insertions(+), 2 deletions(-)
drivers/base/core.c +1 -1
index 07304a3..ec2f211 100644
@@ -761,7 +761,7 @@ static struct kobject *get_device_parent(struct device *dev,
 		spin_lock(&dev->class->p->glue_dirs.list_lock);
 		list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
 			if (k->parent == parent_kobj) {
-				kobj = kobject_get(k);
+				kobj = kobject_get_unless_zero(k);
 				break;
 			}
 		spin_unlock(&dev->class->p->glue_dirs.list_lock);
include/linux/kobject.h +2 -0
index 2d61b90..3032744 100644
@@ -107,6 +107,8 @@ extern int __must_check kobject_rename(struct kobject *, const char *new_name);
 extern int __must_check kobject_move(struct kobject *, struct kobject *);
 
 extern struct kobject *kobject_get(struct kobject *kobj);
+extern struct kobject * __must_check kobject_get_unless_zero(
+		struct kobject *kobj);
 extern void kobject_put(struct kobject *kobj);
 
 extern const void *kobject_namespace(struct kobject *kobj);
lib/kobject.c +1 -1
index 03d4ab3..ff0ce681 100644
@@ -581,7 +581,7 @@ struct kobject *kobject_get(struct kobject *kobj)
 	return kobj;
 }
 
-static struct kobject * __must_check kobject_get_unless_zero(struct kobject *kobj)
+struct kobject * __must_check kobject_get_unless_zero(struct kobject *kobj)
 {
 	if (!kref_get_unless_zero(&kobj->kref))
 		kobj = NULL;
Signature · 5 lines
1.7.9.5


thanks
Eun Taik Lee���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m����������?�I�
#1 lore raw
Quoted 6 lines · EunTaik Lee
Quoted 4 lines

The bus that the device is on should prevent this, why isn't that working for you? What type of device/bus do you see this problem on?

The device is a firmware class device on a virtual bus.

What kernel code is causing this? Again, the bus should be preventing this from happening, is the virtual bus code incorrect somehow?

Quoted 5 lines
Quoted 4 lines
Quoted 1 line
Signed-off-by eun.taik.lee

We need a "real" name here, I don't think your name has '.' in it, right?

Quoted 7 lines

Right. It should've been Eun Taik Lee.

Quoted 2 lines

Your email client turned all tabs to spaces and made the patch unable to be applied.

I think I pasted the patch on HTML mode and then switched to text mode. Below patch should be okay.

I can't take a patch like this, please resend it properly.

thanks,

greg k-h