[PATCH] scsi: core: Protect host_busy reads against tag set teardown

From: Shuo Qin

Date: Wed Dec 02 2026 - 07:05:28 EST


show_host_busy() can race with SCSI host removal. scsi_remove_host()
drops the host's tagset_refcnt reference and waits for the tag set to
be freed before unregistering shost_dev. During this interval the
host_busy attribute is still accessible, but its callback does not
hold a tag set reference while calling scsi_host_busy().

The busy iterator dereferences tagset->tags. blk_mq_free_tag_set()
frees this pointer array before clearing set->tags, so a concurrent
host_busy read can observe a non-NULL pointer to the freed array.
The global tags SRCU domain defers freeing internal tag objects and
request storage, but does not defer freeing this array or pin the
tag set for the sysfs reader.

On older kernels with a per-tag-set tags_srcu, the same missing
lifetime protection can also allow the reader to enter an already
cleaned-up SRCU domain. Switching to a global SRCU domain removes
that failure mode, but leaves the pointer-array lifetime unprotected.

Take a tagset_refcnt reference before iterating and drop it afterwards.
Return -ENODEV if teardown has already dropped the last reference.
Otherwise the existing tagset_freed completion keeps removal waiting
until the reader has finished with the tag set.

Fixes: 8fe4ce5836e9 ("scsi: core: Fix a use-after-free")
Cc: stable@xxxxxxxxxxxxxxx
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>
---
drivers/scsi/scsi_sysfs.c | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/drivers/scsi/scsi_sysfs.c b/drivers/scsi/scsi_sysfs.c
index fe54792ed69e4f0e91b1203c2f106606ef865154..b435ad46558cd156b3c83bfeec97a9617d649245 100644
--- a/drivers/scsi/scsi_sysfs.c
+++ b/drivers/scsi/scsi_sysfs.c
@@ -383,7 +383,16 @@ static ssize_t
show_host_busy(struct device *dev, struct device_attribute *attr, char *buf)
{
struct Scsi_Host *shost = class_to_shost(dev);
- return snprintf(buf, 20, "%d\n", scsi_host_busy(shost));
+ int busy;
+
+ /* scsi_remove_host() frees the tag set before removing this node. */
+ if (!kref_get_unless_zero(&shost->tagset_refcnt))
+ return -ENODEV;
+
+ busy = scsi_host_busy(shost);
+ kref_put(&shost->tagset_refcnt, scsi_mq_free_tags);
+
+ return snprintf(buf, 20, "%d\n", busy);
}
static DEVICE_ATTR(host_busy, S_IRUGO, show_host_busy, NULL);