Skip to contentSkip to conversation

driver core: fix device_create() error path

PATCH In review David Herrmann 2 participants 4 messages Nov 21, 2013 – Dec 9, 2013 Driver core

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 driver core: fix device_create() error path

We call put_device() in the error path, which is fine for dev==NULL. However, in case kobject_set_name_vargs() fails, we have dev!=NULL but device_initialized() wasn't called, yet.

Fix this by splitting device_register() into explicit calls to device_add() and an early call to device_initialize().

---
 drivers/base/core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/base/core.c +2 -1
index 34abf4d..4be8911 100644
@@ -1676,6 +1676,7 @@ device_create_groups_vargs(struct class *class, struct device *parent,
 		goto error;
 	}
 
+	device_initialize(dev);
 	dev->devt = devt;
 	dev->class = class;
 	dev->parent = parent;
@@ -1687,7 +1688,7 @@ device_create_groups_vargs(struct class *class, struct device *parent,
 	if (retval)
 		goto error;
 
-	retval = device_register(dev);
+	retval = device_add(dev);
 	if (retval)
 		goto error;
 
Signature · 1 line
1.8.4.2
#1 lore raw

Hi Greg

Ping?

Thanks
David

Quoted 36 lines · David Herrmann

We call put_device() in the error path, which is fine for dev==NULL. However, in case kobject_set_name_vargs() fails, we have dev!=NULL but device_initialized() wasn't called, yet.

Fix this by splitting device_register() into explicit calls to device_add() and an early call to device_initialize().

---
 drivers/base/core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/base/core.c +2 -2
index 34abf4d..4be8911 100644
@@ -1676,6 +1676,7 @@ device_create_groups_vargs(struct class *class, struct device *parent,
                goto error;
        }
 
+       device_initialize(dev);
        dev->devt = devt;
        dev->class = class;
        dev->parent = parent;
@@ -1687,7 +1688,7 @@ device_create_groups_vargs(struct class *class, struct device *parent,
        if (retval)
                goto error;
 
-       retval = device_register(dev);
+       retval = device_add(dev);
        if (retval)
                goto error;
 
--

1.8.4.2

#2 lore raw
Quoted 3 lines · David Herrmann

Hi Greg

Ping?

It's in my "todo" queue, don't worry, it's not lost.

Quoted 4 lines · David Herrmann
Quoted 3 lines

We call put_device() in the error path, which is fine for dev==NULL. However, in case kobject_set_name_vargs() fails, we have dev!=NULL but device_initialized() wasn't called, yet.

If kobject_set_name_vargs() fails, we have bigger problems than this :)

And I think it's safe for a put_device() on a device that wasn't initialized(), we just get a warning message if that happens.

Not to say that your patch is wrong, I'll look at it soon, but things should still recover ok from what I can tell.

Oh, and there's the much-complained about memory leak in kobject_set_name_vargs() if something goes wrong there as well, I have a patch sitting in my queue to hopefully fix that up.

thanks,

greg k-h

#3 lore raw

Hi Greg

Quoted 8 lines · Greg Kroah-Hartman
Quoted 3 lines · David Herrmann

Hi Greg

Ping?

It's in my "todo" queue, don't worry, it's not lost.

Quoted 1 line · David Herrmann
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines

We call put_device() in the error path, which is fine for dev==NULL. However, in case kobject_set_name_vargs() fails, we have dev!=NULL but device_initialized() wasn't called, yet.

If kobject_set_name_vargs() fails, we have bigger problems than this :)

And I think it's safe for a put_device() on a device that wasn't initialized(), we just get a warning message if that happens.

I think it results in a call to kref_sub(), which will call atomic_sub_and_test() which will fail as the new value is -1. So the memory isn't freed.

I was just going through my pending patches to make sure they're not forgotten. Nothing important, but I saw you already applied it.

Thanks
David

Quoted 10 lines

Not to say that your patch is wrong, I'll look at it soon, but things should still recover ok from what I can tell.

Oh, and there's the much-complained about memory leak in kobject_set_name_vargs() if something goes wrong there as well, I have a patch sitting in my queue to hopefully fix that up.

thanks,

greg k-h