Skip to content

Commit e5da06b

Browse files
Yang Yinglianggregkh
authored andcommitted
drivers: base: transport_class: fix resource leak when transport_add_device() fails
The normal call sequence of using transport class is: Add path: transport_setup_device() transport_setup_classdev() // call sas_host_setup() here transport_add_device() // if fails, need call transport_destroy_device() transport_configure_device() Remove path: transport_remove_device() transport_remove_classdev // call sas_host_remove() here transport_destroy_device() If transport_add_device() fails, need call transport_destroy_device() to free memory, but in this case, ->remove() is not called, and the resources allocated in ->setup() are leaked. So fix these leaks by calling ->remove() in transport_add_class_device() if it returns error. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Yang Yingliang <yangyingliang@huawei.com> Link: https://lore.kernel.org/r/20221115031638.3816551-1-yangyingliang@huawei.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent a863678 commit e5da06b

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

drivers/base/transport_class.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,27 @@ static int transport_add_class_device(struct attribute_container *cont,
155155
struct device *dev,
156156
struct device *classdev)
157157
{
158+
struct transport_class *tclass = class_to_transport_class(cont->class);
158159
int error = attribute_container_add_class_device(classdev);
159160
struct transport_container *tcont =
160161
attribute_container_to_transport_container(cont);
161162

162-
if (!error && tcont->statistics)
163+
if (error)
164+
goto err_remove;
165+
166+
if (tcont->statistics) {
163167
error = sysfs_create_group(&classdev->kobj, tcont->statistics);
168+
if (error)
169+
goto err_del;
170+
}
171+
172+
return 0;
173+
174+
err_del:
175+
attribute_container_class_device_del(classdev);
176+
err_remove:
177+
if (tclass->remove)
178+
tclass->remove(tcont, dev, classdev);
164179

165180
return error;
166181
}

0 commit comments

Comments
 (0)