mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Bart Van Assche <bvanassche@acm.org>
To: Shuo Qin <23159100078@stu.xidian.edu.cn>
Cc: "James E.J. Bottomley" <James.Bottomley@HansenPartnership.com>,
	"Martin K. Petersen" <mkp@kernel.org>,
	linux-scsi@vger.kernel.org, Ming Lei <tom.leiming@gmail.com>,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org,
	Bin Yu <byu@xidian.edu.cn>, Cheng Wen <wencheng@xidian.edu.cn>,
	XinCheng Wang <26031212453@stu.xidian.edu.cn>
Subject: Re: [PATCH] scsi: core: Protect host_busy reads against tag set teardown
Date: Thu, 3 Dec 2026 16:07:34 -0800	[thread overview]
Message-ID: <9388782a-ac9b-48ab-ae76-095e4cb888cc@acm.org> (raw)
In-Reply-To: <20261203024144.22289-1-23159100078@stu.xidian.edu.cn>

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);

  reply	other threads:[~2026-12-04  0:07 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 12:04 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 [this message]
2026-12-04  2:15       ` [PATCH v2] scsi: core: Free the tag set after unregistering shost_dev Shuo Qin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9388782a-ac9b-48ab-ae76-095e4cb888cc@acm.org \
    --to=bvanassche@acm.org \
    --cc=23159100078@stu.xidian.edu.cn \
    --cc=26031212453@stu.xidian.edu.cn \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=byu@xidian.edu.cn \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=mkp@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=tom.leiming@gmail.com \
    --cc=wencheng@xidian.edu.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®