[PATCH 1/2] scsi: core: Fix target reap_ref leak in scsi_get_pseudo_sdev()

From: Laurence Oberman

Date: Tue Nov 03 2026 - 14:33:10 EST


scsi_alloc_target() returns a target holding the initial reap_ref from
kref_init(). The scan paths drop that reference with scsi_target_reap()
once they are done with the target; the device allocated under it holds
its own reap_ref, taken in scsi_sysfs_device_initialize() and dropped
by __scsi_remove_device().

scsi_get_pseudo_sdev() only drops the initial reference on the failure
path. On success the pseudo target is left with an extra reap_ref, so
when scsi_forget_host() removes the pseudo device the count only drops
to one and the target is never destroyed: ->target_destroy() is not
called, and the scsi_target, the LLD's per-target data and the
reference the target holds on the host's gendev are leaked.

With scsi_debug (nr_reserved_cmds = 1) this is visible as one target
per host, at id max_id, for which ->target_alloc() is called but
->target_destroy() never is.

Drop the initial reap_ref unconditionally, as the scan paths do. The
pseudo target is then destroyed when the pseudo device is removed.

Fixes: d630fbf6fc8c ("scsi: core: Support allocating a pseudo SCSI device")
Signed-off-by: Laurence Oberman <loberman@xxxxxxxxxx>
---
drivers/scsi/scsi_scan.c | 15 ++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/drivers/scsi/scsi_scan.c b/drivers/scsi/scsi_scan.c
index 8aae12fd38f3..9ed52724f97c 100644
--- a/drivers/scsi/scsi_scan.c
+++ b/drivers/scsi/scsi_scan.c
@@ -2154,14 +2154,15 @@ struct scsi_device *scsi_get_pseudo_sdev(struct Scsi_Host *shost)
goto out;

sdev = scsi_alloc_sdev(starget, U64_MAX, NULL);
- if (!sdev) {
- scsi_target_reap(starget);
- goto put_target;
- }
-
- sdev->borken = 0;
+ if (sdev)
+ sdev->borken = 0;

-put_target:
+ /*
+ * Drop the reap_ref taken by scsi_alloc_target(). If the pseudo device
+ * was allocated it holds its own reap_ref, which __scsi_remove_device()
+ * drops, so the target is destroyed when the pseudo device is removed.
+ */
+ scsi_target_reap(starget);
/* See also the get_device(dev) call in scsi_alloc_target(). */
put_device(&starget->dev);

--
2.56.0