diff mbox series

[-next,v3] SCSI: fix possible memory leak while device_add() fails

Message ID 20230803020230.226903-1-wangzhu9@huawei.com (mailing list archive)
State Accepted
Headers show
Series [-next,v3] SCSI: fix possible memory leak while device_add() fails | expand

Commit Message

wangzhu Aug. 3, 2023, 2:02 a.m. UTC
If device_add() returns error, the name allocated by dev_set_name() need
be freed. As comment of device_add() says, it should use put_device() to
decrease the reference count in the error path. So fix this by calling
put_device, then the name can be freed in kobject_cleanp().

Fixes: ee959b00c335 ("SCSI: convert struct class_device to struct device")
Signed-off-by: Zhu Wang <wangzhu9@huawei.com>

---
Changes in v2:
- Move the new put_device() call from under if to under err_out label.

---
Changes in v3:
- Swap put_device() and list_del(), perform the error recovery actions
in the opposite order of the corresponding actions.
---
 drivers/scsi/raid_class.c | 1 +
 1 file changed, 1 insertion(+)

Comments

Bart Van Assche Aug. 3, 2023, 3:01 p.m. UTC | #1
On 8/2/23 19:02, Zhu Wang wrote:
> If device_add() returns error, the name allocated by dev_set_name() need
> be freed. As comment of device_add() says, it should use put_device() to
> decrease the reference count in the error path. So fix this by calling
> put_device, then the name can be freed in kobject_cleanp().

Reviewed-by: Bart Van Assche <bvanassche@acm.org>
Martin K. Petersen Aug. 8, 2023, 2:43 a.m. UTC | #2
On Thu, 03 Aug 2023 10:02:30 +0800, Zhu Wang wrote:

> If device_add() returns error, the name allocated by dev_set_name() need
> be freed. As comment of device_add() says, it should use put_device() to
> decrease the reference count in the error path. So fix this by calling
> put_device, then the name can be freed in kobject_cleanp().
> 
> 

Applied to 6.5/scsi-fixes, thanks!

[1/1] SCSI: fix possible memory leak while device_add() fails
      https://git.kernel.org/mkp/scsi/c/04b5b5cb0136
diff mbox series

Patch

diff --git a/drivers/scsi/raid_class.c b/drivers/scsi/raid_class.c
index 898a0bdf8df6..711252e52d8e 100644
--- a/drivers/scsi/raid_class.c
+++ b/drivers/scsi/raid_class.c
@@ -248,6 +248,7 @@  int raid_component_add(struct raid_template *r,struct device *raid_dev,
 	return 0;
 
 err_out:
+	put_device(&rc->dev);
 	list_del(&rc->node);
 	rd->component_count--;
 	put_device(component_dev);