Message ID | 20230801091933.31794-1-wangzhu9@huawei.com |
---|---|
State | Superseded |
Headers | show |
Series | [-next] scsi: core: fix error handling for dev_set_name | expand |
On 8/1/23 02:19, Zhu Wang wrote: > 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()") > Signed-off-by: Zhu Wang <wangzhu9@huawei.com> > --- > drivers/scsi/scsi_scan.c | 8 +++++++- > 1 file changed, 7 insertions(+), 1 deletion(-) > > diff --git a/drivers/scsi/scsi_scan.c b/drivers/scsi/scsi_scan.c > index aa13feb17c62..36808a52ab09 100644 > --- a/drivers/scsi/scsi_scan.c > +++ b/drivers/scsi/scsi_scan.c > @@ -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); I think that a put_device(parent) call is missing from the error path. Thanks, Bart.
diff --git a/drivers/scsi/scsi_scan.c b/drivers/scsi/scsi_scan.c index aa13feb17c62..36808a52ab09 100644 --- a/drivers/scsi/scsi_scan.c +++ b/drivers/scsi/scsi_scan.c @@ -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);
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()") Signed-off-by: Zhu Wang <wangzhu9@huawei.com> --- drivers/scsi/scsi_scan.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)