Skip to contentSkip to conversation

scsi: core: fix error handling for dev_set_name

PATCH Superseded Zhu Wang 2 participants 2 messages Aug 1, 2023 SCSI

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Zhu Wang

author
#0 lore raw

PATCH 1/1 scsi: core: fix error handling for dev_set_name

The driver do not handle the possible returning error of dev_set_name, if it returned fail, some operations should be rollback or there may be possible memory leak. We use put_device to free the device and use kfree to free the memory in the error handle path.

Fixes 71610f55fa4d ("[SCSI] struct device - replace bus_id with dev_name(), dev_set_name()")
---
 drivers/scsi/scsi_scan.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/scsi/scsi_scan.c +7 -1
index aa13feb17c62..36808a52ab09 100644
@@ -509,7 +509,13 @@ static struct scsi_target *scsi_alloc_target(struct device *parent,
 	device_initialize(dev);
 	kref_init(&starget->reap_ref);
 	dev->parent = get_device(parent);
-	dev_set_name(dev, "target%d:%d:%d", shost->host_no, channel, id);
+	error = dev_set_name(dev, "target%d:%d:%d", shost->host_no, channel, id);
+	if (error) {
+		dev_err(dev, "%s: dev_set_name failed, error %d\n", __func__, error);
+		put_device(dev);
+		kfree(starget);
+		return NULL;
+	}
 	dev->bus = &scsi_bus_type;
 	dev->type = &scsi_target_type;
 	scsi_enable_async_suspend(dev);
Signature · 1 line
2.17.1
#1 lore raw
Quoted 20 lines · Zhu Wang

The driver do not handle the possible returning error of dev_set_name, if it returned fail, some operations should be rollback or there may be possible memory leak. We use put_device to free the device and use kfree to free the memory in the error handle path.

Fixes 71610f55fa4d ("[SCSI] struct device - replace bus_id with dev_name(), dev_set_name()")
---
  drivers/scsi/scsi_scan.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
drivers/scsi/scsi_scan.c +0 -1
index aa13feb17c62..36808a52ab09 100644
@@ -509,7 +509,13 @@ static struct scsi_target *scsi_alloc_target(struct device *parent,
  	device_initialize(dev);
  	kref_init(&starget->reap_ref);
  	dev->parent = get_device(parent);
-	dev_set_name(dev, "target%d:%d:%d", shost->host_no, channel, id);

drivers/scsi/scsi_scan.c static struct scsi_target *scsi_alloc_target(struct device *parent, · line 521

Quoted 10 lines
drivers/scsi/scsi_scan.c +7 -0
+	error = dev_set_name(dev, "target%d:%d:%d", shost->host_no, channel, id);
+	if (error) {
+		dev_err(dev, "%s: dev_set_name failed, error %d\n", __func__, error);
+		put_device(dev);
+		kfree(starget);
+		return NULL;
+	}
  	dev->bus = &scsi_bus_type;
  	dev->type = &scsi_target_type;
  	scsi_enable_async_suspend(dev);

I think that a put_device(parent) call is missing from the error path.

Thanks,

Bart.