Skip to contentSkip to conversation

raid class: Fix error handling in raid_component_add

PATCH New Ma Ke 1 participant 1 message Jan 7, 2025

Subsystems (2)
lore
Share & tools
lore

Ma Ke

author
#0 lore raw

PATCH 1/1 raid class: Fix error handling in raid_component_add

The reference count of the device incremented in device_initialize() is not decremented when device_add() fails. Add a put_device() and delete kfree() before returning from the function to decrement reference count for cleanup. Or it could cause memory leak.

As comment of device_add() says, if device_add() succeeds, you should call device_del() when you want to get rid of it. If device_add() has not succeeded, use only put_device() to drop the reference count.

Found by code review.

Fixes ed542bed126c ("[SCSI] raid class: handle component-add errors")
---
 drivers/scsi/raid_class.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/raid_class.c +1 -1
index 898a0bdf8df6..77c91dbf3b77 100644
@@ -251,7 +251,7 @@ int raid_component_add(struct raid_template *r,struct device *raid_dev,
 	list_del(&rc->node);
 	rd->component_count--;
 	put_device(component_dev);
-	kfree(rc);
+	put_device(&rc->dev);
 	return err;
 }
 EXPORT_SYMBOL(raid_component_add);
Signature · 1 line
2.25.1