[PATCH v2] scsi: core: Free the tag set after unregistering shost_dev
From: Shuo Qin
Date: Thu Dec 03 2026 - 21:16:23 EST
scsi_remove_host() can free the tag set while host_busy is still
readable through sysfs. show_host_busy() walks the tag set and can
access the tag pointer array concurrently with its release.
Drop the host's tag-set reference after unregistering the host
devices. Unregistering shost_dev removes host_busy and waits for
active callbacks, so the tag set remains valid throughout each read.
Keep the completion wait in scsi_remove_host() so that low-level
drivers can release resources used by .exit_cmd_priv() after it returns.
Fixes: 8fe4ce5836e9 ("scsi: core: Fix a use-after-free")
Cc: stable@xxxxxxxxxxxxxxx
Suggested-by: Bart Van Assche <bvanassche@xxxxxxx>
Co-developed-by: Bin Yu <byu@xxxxxxxxxxxxx>
Signed-off-by: Bin Yu <byu@xxxxxxxxxxxxx>
Co-developed-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
Signed-off-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
Signed-off-by: Shuo Qin <23159100078@xxxxxxxxxxxxxxxxx>
---
Changes in v2:
- Move tag-set teardown after host device removal, as proposed by Bart.
- Keep host_busy unchanged.
drivers/scsi/hosts.c | 17 ++++++++---------
1 file changed, 8 insertions(+), 9 deletions(-)
diff --git a/drivers/scsi/hosts.c b/drivers/scsi/hosts.c
index e8361ba655..20e92fb1b1 100644
--- a/drivers/scsi/hosts.c
+++ b/drivers/scsi/hosts.c
@@ -184,15 +184,6 @@ void scsi_remove_host(struct Scsi_Host *shost)
scsi_proc_host_rm(shost);
scsi_proc_hostdir_rm(shost->hostt);
- /*
- * New SCSI devices cannot be attached anymore because of the SCSI host
- * state so drop the tag set refcnt. Wait until the tag set refcnt drops
- * to zero because .exit_cmd_priv implementations may need the host
- * pointer.
- */
- kref_put(&shost->tagset_refcnt, scsi_mq_free_tags);
- wait_for_completion(&shost->tagset_freed);
-
spin_lock_irqsave(&shost->host_lock, flags);
if (scsi_host_set_state(shost, SHOST_DEL))
BUG_ON(scsi_host_set_state(shost, SHOST_DEL_RECOVERY));
@@ -201,6 +192,14 @@ void scsi_remove_host(struct Scsi_Host *shost)
transport_unregister_device(&shost->shost_gendev);
device_unregister(&shost->shost_dev);
device_del(&shost->shost_gendev);
+
+ /*
+ * Keep the tag set alive until the host sysfs callbacks have drained.
+ * Wait for tag set teardown before returning, since .exit_cmd_priv()
+ * implementations may still need host resources.
+ */
+ kref_put(&shost->tagset_refcnt, scsi_mq_free_tags);
+ wait_for_completion(&shost->tagset_freed);
}
EXPORT_SYMBOL(scsi_remove_host);