* [PATCH] scsi: core: Protect host_busy reads against tag set teardown
@ 2026-12-02 12:04 Shuo Qin
2026-12-02 15:07 ` Bart Van Assche
0 siblings, 1 reply; 5+ messages in thread
From: Shuo Qin @ 2026-12-02 12:04 UTC (permalink / raw)
To: James E.J. Bottomley, Martin K. Petersen
Cc: linux-scsi, Ming Lei, Bart Van Assche, linux-kernel, stable,
Bin Yu, Cheng Wen, XinCheng Wang
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@vger.kernel.org
Co-developed-by: Bin Yu <byu@xidian.edu.cn>
Signed-off-by: Bin Yu <byu@xidian.edu.cn>
Co-developed-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: Shuo Qin <23159100078@stu.xidian.edu.cn>
---
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);
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH] scsi: core: Protect host_busy reads against tag set teardown
2026-12-02 12:04 [PATCH] scsi: core: Protect host_busy reads against tag set teardown Shuo Qin
@ 2026-12-02 15:07 ` Bart Van Assche
2026-12-03 2:41 ` Shuo Qin
0 siblings, 1 reply; 5+ messages in thread
From: Bart Van Assche @ 2026-12-02 15:07 UTC (permalink / raw)
To: Shuo Qin, James E.J. Bottomley, Martin K. Petersen
Cc: linux-scsi, Ming Lei, linux-kernel, stable, Bin Yu, Cheng Wen,
XinCheng Wang
On 12/2/26 4:04 AM, Shuo Qin wrote:
> 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().
Why has the approach of this patch been selected instead of adding a
kref_get() call before scsi_remove_host() and a kref_put() call after
shost_busy has been removed?
Thanks,
Bart.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] scsi: core: Protect host_busy reads against tag set teardown
2026-12-02 15:07 ` Bart Van Assche
@ 2026-12-03 2:41 ` Shuo Qin
2026-12-04 0:07 ` Bart Van Assche
0 siblings, 1 reply; 5+ messages in thread
From: Shuo Qin @ 2026-12-03 2:41 UTC (permalink / raw)
To: Bart Van Assche
Cc: James E.J. Bottomley, Martin K. Petersen, linux-scsi, Ming Lei,
linux-kernel, stable, Bin Yu, Cheng Wen, XinCheng Wang
Hi Bart,
> ... 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.
The original patch is available here:
https://lore.kernel.org/r/20261202120443.15341-1-23159100078@stu.xidian.edu.cn
Thanks,
Shuo
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] scsi: core: Protect host_busy reads against tag set teardown
2026-12-03 2:41 ` Shuo Qin
@ 2026-12-04 0:07 ` Bart Van Assche
2026-12-04 2:15 ` [PATCH v2] scsi: core: Free the tag set after unregistering shost_dev Shuo Qin
0 siblings, 1 reply; 5+ messages in thread
From: Bart Van Assche @ 2026-12-04 0:07 UTC (permalink / raw)
To: Shuo Qin
Cc: James E.J. Bottomley, Martin K. Petersen, linux-scsi, Ming Lei,
linux-kernel, stable, Bin Yu, Cheng Wen, XinCheng Wang
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@vger.kernel.org
---
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);
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH v2] scsi: core: Free the tag set after unregistering shost_dev
2026-12-04 0:07 ` Bart Van Assche
@ 2026-12-04 2:15 ` Shuo Qin
0 siblings, 0 replies; 5+ messages in thread
From: Shuo Qin @ 2026-12-04 2:15 UTC (permalink / raw)
To: Bart Van Assche
Cc: James E.J. Bottomley, Martin K. Petersen, linux-scsi, Ming Lei,
linux-kernel, stable, Bin Yu, Cheng Wen, XinCheng Wang
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@vger.kernel.org
Suggested-by: Bart Van Assche <bvanassche@acm.org>
Co-developed-by: Bin Yu <byu@xidian.edu.cn>
Signed-off-by: Bin Yu <byu@xidian.edu.cn>
Co-developed-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: Shuo Qin <23159100078@stu.xidian.edu.cn>
---
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);
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-04 2:16 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-02 12:04 [PATCH] scsi: core: Protect host_busy reads against tag set teardown Shuo Qin
2026-12-02 15:07 ` Bart Van Assche
2026-12-03 2:41 ` Shuo Qin
2026-12-04 0:07 ` Bart Van Assche
2026-12-04 2:15 ` [PATCH v2] scsi: core: Free the tag set after unregistering shost_dev Shuo Qin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®