driver core: fix device_create() error path
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
@@ -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
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 casekobject_set_name_vargs()fails, we have dev!=NULL butdevice_initialized()wasn't called, yet.Fix this by splitting
device_register()into explicit calls todevice_add()and an early call todevice_initialize().Signed-off-by David Herrmann <[email protected]>--- drivers/base/core.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/base/core.c +2
-2@@ -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
Greg Kroah-Hartman
maintainerQuoted 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 casekobject_set_name_vargs()fails, we have dev!=NULL butdevice_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
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 casekobject_set_name_vargs()fails, we have dev!=NULL butdevice_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'tinitialized(), 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