All of lore.kernel.org
 help / color / mirror / Atom feed
From: Zhu Wang <wangzhu9@huawei.com>
To: <jejb@linux.ibm.com>, <martin.petersen@oracle.com>,
	<kay.sievers@vrfy.org>, <gregkh@suse.de>,
	<James.Bottomley@HansenPartnership.com>,
	<linux-scsi@vger.kernel.org>, <bvanassche@acm.org>
Cc: <wangzhu9@huawei.com>
Subject: [PATCH -next v2] scsi: core: fix error handling for dev_set_name
Date: Wed, 2 Aug 2023 11:10:10 +0800	[thread overview]
Message-ID: <20230802031010.14340-1-wangzhu9@huawei.com> (raw)

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>
Reviewed-by: Bart Van Assche <bvanassche@acm.org>

---
Changes in v2:
- Add put_device(parent) in the error path.
---
 drivers/scsi/scsi_scan.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/drivers/scsi/scsi_scan.c b/drivers/scsi/scsi_scan.c
index aa13feb17c62..de7e503bfcab 100644
--- a/drivers/scsi/scsi_scan.c
+++ b/drivers/scsi/scsi_scan.c
@@ -509,7 +509,14 @@ 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(parent);
+		put_device(dev);
+		kfree(starget);
+		return NULL;
+	}
 	dev->bus = &scsi_bus_type;
 	dev->type = &scsi_target_type;
 	scsi_enable_async_suspend(dev);
-- 
2.17.1


             reply	other threads:[~2023-08-02  3:10 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-02  3:10 Zhu Wang [this message]
2023-08-02 15:59 ` [PATCH -next v2] scsi: core: fix error handling for dev_set_name Bart Van Assche
2023-08-02 16:34 ` John Garry
2023-08-10 11:27   ` wangzhu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230802031010.14340-1-wangzhu9@huawei.com \
    --to=wangzhu9@huawei.com \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=bvanassche@acm.org \
    --cc=gregkh@suse.de \
    --cc=jejb@linux.ibm.com \
    --cc=kay.sievers@vrfy.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.