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

From: Bart Van Assche

Date: Thu Dec 03 2026 - 19:07:47 EST


On 12/2/26 6:41 PM, Shuo Qin wrote:
... adding a kref_get() call before scsi_remove_host() and a kref_put()
call after shost_busy has been removed?

I scoped the tag-set reference to show_host_busy() so that each read
pins the tag set only while it walks the busy requests.

If you mean taking an extra tag-set reference before
scsi_remove_host() and dropping it after host_busy is removed,
scsi_remove_host() would wait indefinitely with the current ordering.
It waits for tagset_freed before unregistering shost_dev, so it
cannot reach the point where host_busy is removed while that reference
is held.

Did you have in mind removing host_busy and waiting for any active
callbacks to finish before dropping the host's tag-set reference?
I can rework the patch that way if that's what you had in mind.
My concern is about the maintenance burden that would be introduced by
introducing per-sysfs attribute kref_get() and kref_put() calls.

Has something like the untested patch below been considered?

Thanks,

Bart.


Subject: [PATCH] scsi: core: Free the tag set after unregistering shost_dev

scsi_remove_host() drops shost->tagset_refcnt and waits for
shost->tagset_freed before unregistering shost->shost_dev. While
shost_dev is still registered, userspace can read the host_busy sysfs
attribute and invoke show_host_busy() -> scsi_host_busy() ->
blk_mq_tagset_busy_iter() concurrently with blk_mq_free_tag_set().
Because blk_mq_free_tag_set() frees set->tags before clearing it (and
without SRCU deferral for the pointer array), a concurrent host_busy
read can trigger a use-after-free.

Move the kref_put(&shost->tagset_refcnt, scsi_mq_free_tags) and
wait_for_completion(&shost->tagset_freed) calls to the end of
scsi_remove_host(), after transport_unregister_device(),
device_unregister(&shost->shost_dev), and
device_del(&shost->shost_gendev). This matches the reverse order of
scsi_add_host_with_dma() (and its error unwind path), guarantees that
kernfs has drained all active host sysfs callbacks before the tag set
is freed, and keeps tag-set teardown inside scsi_remove_host() before
it returns so .exit_cmd_priv implementations can still access host
resources.

Fixes: 8fe4ce5836e9 ("scsi: core: Fix a use-after-free")
Cc: stable@xxxxxxxxxxxxxxx
---
drivers/scsi/hosts.c | 19 ++++++++++---------
1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/drivers/scsi/hosts.c b/drivers/scsi/hosts.c
--- 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,16 @@ void scsi_remove_host(struct Scsi_Host *shost)
transport_unregister_device(&shost->shost_gendev);
device_unregister(&shost->shost_dev);
device_del(&shost->shost_gendev);
+
+ /*
+ * New SCSI devices cannot be attached anymore because of the SCSI host
+ * state and host sysfs attributes (including host_busy) have been
+ * removed, 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);
}
EXPORT_SYMBOL(scsi_remove_host);