mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [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; 2+ 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] 2+ 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
  0 siblings, 0 replies; 2+ 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] 2+ messages in thread

end of thread, other threads:[~2026-12-02 15:07 UTC | newest]

Thread overview: 2+ 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

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®