Skip to contentSkip to conversation

drivers: base: transport_class: fix resource leak when transport_add_device() fails

PATCH Superseded Yang Yingliang 1 participant 1 message Nov 10, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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")
---
 drivers/base/transport_class.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/base/transport_class.c +4 -0
index ccc86206e508..d0237db6236f 100644
@@ -155,6 +155,7 @@ static int transport_add_class_device(struct attribute_container *cont,
 				      struct device *dev,
 				      struct device *classdev)
 {
+	struct transport_class *tclass = class_to_transport_class(cont->class);
 	int error = attribute_container_add_class_device(classdev);
 	struct transport_container *tcont = 
 		attribute_container_to_transport_container(cont);
@@ -162,6 +163,9 @@ static int transport_add_class_device(struct attribute_container *cont,
 	if (!error && tcont->statistics)
 		error = sysfs_create_group(&classdev->kobj, tcont->statistics);
 
+	if (error && tclass->remove)
+		tclass->remove(tcont, dev, classdev);
+
 	return error;
 }
 
Signature · 1 line
2.25.1