[PATCH v2] scsi_sysfs: Fix runtime PM usage count leak on device add failure

From: Haoxiang Li

Date: Wed Jun 24 2026 - 03:55:27 EST


Balance the scsi_autopm_get_device() call on the error paths of
scsi_sysfs_add_sdev(). Disable runtime PM before dropping the
reference when device_add() fails for sdev_gendev, since device_del()
is not run on that path.

Signed-off-by: Haoxiang Li <haoxiang_li2024@xxxxxxx>
---
drivers/scsi/scsi_sysfs.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/scsi/scsi_sysfs.c b/drivers/scsi/scsi_sysfs.c
index dfc3559e7e04..14dcf45b524a 100644
--- a/drivers/scsi/scsi_sysfs.c
+++ b/drivers/scsi/scsi_sysfs.c
@@ -1427,7 +1427,7 @@ int scsi_sysfs_add_sdev(struct scsi_device *sdev)
if (error) {
sdev_printk(KERN_INFO, sdev,
"failed to add device: %d\n", error);
- return error;
+ goto out_pm_runtime_disable;
}

device_enable_async_suspend(&sdev->sdev_dev);
@@ -1436,7 +1436,7 @@ int scsi_sysfs_add_sdev(struct scsi_device *sdev)
sdev_printk(KERN_INFO, sdev,
"failed to add class device: %d\n", error);
device_del(&sdev->sdev_gendev);
- return error;
+ goto out_autopm_put;
}
transport_add_device(&sdev->sdev_gendev);
sdev->is_visible = 1;
@@ -1452,8 +1452,14 @@ int scsi_sysfs_add_sdev(struct scsi_device *sdev)
}
}

+out_autopm_put:
scsi_autopm_put_device(sdev);
return error;
+
+out_pm_runtime_disable:
+ if (pm_runtime_enabled(&sdev->sdev_gendev))
+ pm_runtime_disable(&sdev->sdev_gendev);
+ goto out_autopm_put;
}

void __scsi_remove_device(struct scsi_device *sdev)
--
2.25.1