[PATCH] sd: remove extra put_device() for extra scsi device

From: Luis Chamberlain
Date: Mon Jan 29 2024 - 12:25:18 EST


The sd driver first device_add() its own device, and later use
device_add_disk() with another device. When we added error handling
for device_add_disk() we now call put_disk() and that will trigger
disk_release() when the refcount is 0. That will end up calling
the block driver's disk->fops->free_disk() if one is defined. The
sd driver has scsi_disk_free_disk() as its free_disk() and that
does the proper put_device(&sdkp->disk_dev) for us so we should not
need to call it, however we are left still missing the device_del()
for it.

While at it, unwind with scsi_autopm_put_device(sdp) *prior* to
putting to device as we do in sd_remove().

Reported-by: Li Nan <linan122@xxxxxxxxxx>
Reported-by: Yu Kuai <yukuai1@xxxxxxxxxxxxxxx>
Fixes: 2a7a891f4c40 ("scsi: sd: Add error handling support for add_disk()")
Signed-off-by: Luis Chamberlain <mcgrof@xxxxxxxxxx>
---
drivers/scsi/sd.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index 7f949adbadfd..6475a3c947f8 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -3693,8 +3693,9 @@ static int sd_probe(struct device *dev)

error = device_add(&sdkp->disk_dev);
if (error) {
+ scsi_autopm_put_device(sdp);
put_device(&sdkp->disk_dev);
- goto out;
+ return error;
}

dev_set_drvdata(dev, sdkp);
@@ -3734,9 +3735,10 @@ static int sd_probe(struct device *dev)

error = device_add_disk(dev, gd, NULL);
if (error) {
- put_device(&sdkp->disk_dev);
+ scsi_autopm_put_device(sdp);
+ device_del(&sdkp->disk_dev);
put_disk(gd);
- goto out;
+ return error;
}

if (sdkp->security) {
--
2.42.0