From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=199.89.1.16 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796342860; b=yMPHbuQFENq+Y3FQrZuGODUJGB8eXvc5lScPR9kQM8ItkaJml2JqduiERDJlEXvKf5oA HJSs9InNlEv9k2dYou+M3CAhgenAXblk3FMkUgctGyrpWnTFS0E4oAxLJ95OTEq8LO8Ox 10CSVh313B+njlRA9s7knpB3wwKNzjHws1t2jw2WRP+DuPSTIEpMaL8eHpdjMJHU6elrJ fnhsgoQx9StCPE+0i2EwTWwotyIGR3MRD0JpIgS3SokI2wgiBySlgkpzX4gfr/QM8I8AC DXxWNFVjQBY/9h5YZiNO8at9QJLiiMPFA9uslIoLhk+XCEor2oUdRX+vxPQEVh+g+Eg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796342860; h=DMARC-Filter:Received:DKIM-Signature:X-Virus-Scanned:Received: Received:Message-ID:Date:MIME-Version:User-Agent:Subject:To:Cc: References:Content-Language:From:In-Reply-To:Content-Type: Content-Transfer-Encoding; bh=28ilEJ6EiwvZfl/Dn7LM1SblTqajCePEcfmpxLV0Ou8=; b=yHkGuQDhubyK/K/QzmuwVfctHwa2vbSCmwLsg1RNMCmd73jc7/X/wpUJdFgqE4mlv6SA pK1zrPGmFCR7SA7ZpUHk1OWMMFejit/8UWGL6eo8C/K5tPVUvamFMqB7xiWDGbT882w9D pRaZATPC5jHbz3DjLcVRmRSlMEhMD0yukaZHfTCkGmBKE+SUbioMB/vQwGkkqQ4fwDQL1 /2M2DMthTH9CfUFqI2eESevq5yNi61AEQ3nTzc7Z6VUkAJhBuEO1M3VxMzEuQIUACz0Vl xveZLZOSTDRyeb+uXQ8vkZhP/q871TgzSnU5Ek3jDsUgj4YlDe7gl4+7p9vATrjFb7w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=acm.org; spf=pass smtp.mailfrom=acm.org; dkim=pass header.d=acm.org header.i=@acm.org header.a=rsa-sha256 header.s=mr01 header.b=AMfnWGZR; arc=none smtp.remote-ip=199.89.1.16 Received: from 013.lax.mailroute.net (013.lax.mailroute.net [199.89.1.16]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9DA8A606A322; Fri, 4 Dec 2026 00:07:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9DA8A606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=acm.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=acm.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=acm.org header.i=@acm.org header.a=rsa-sha256 header.s=mr01 header.b=AMfnWGZR Received: from localhost (localhost [127.0.0.1]) by 013.lax.mailroute.net (Postfix) with ESMTP id 4jgWyz0tMjzlfvqM; Fri, 4 Dec 2026 00:07:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=acm.org; h= content-transfer-encoding:content-type:content-type:in-reply-to :from:from:content-language:references:subject:subject :user-agent:mime-version:date:date:message-id:received:received; s=mr01; t=1796342855; x=1798934856; bh=28ilEJ6EiwvZfl/Dn7LM1Sbl TqajCePEcfmpxLV0Ou8=; b=AMfnWGZR7zaSu6MxuJVJ9flfo0UsbPhLeTzsnrwC SsxqkiIJxa9xH8VwGD6EFfjza349VzJYfr1fH25yjzzz7y8mwyvl5djCo4VylMkP DFFJkPhThskr1hbmNf1/3wd3tA5I9kGZtgqrCQ6lbNzz1TtdO8sm2nsVMocZa5qN bxb8HIJyqqgczKekTF+IsTpYS8kBRJ9oOVdYxj3qKrqqVKa5PKsKydVGT32p2DNt P1HTKaOBKPpX++mrAqjRg09/HKozulXt/2V7Ex6gE7oQgQ/XrEF4SWf9Civy2A1N VpNHkimQUMmbbtB/3A3qQ3DhDX6R6/PTbuEpMJVquZ2JJQ== X-Virus-Scanned: by MailRoute Received: from 013.lax.mailroute.net ([127.0.0.1]) by localhost (013.lax [127.0.0.1]) (mroute_mailscanner, port 10029) with LMTP id QZUfaOByW50w; Fri, 4 Dec 2026 00:07:35 +0000 (UTC) Received: from [100.80.227.187] (unknown [104.135.182.43]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: bvanassche@acm.org) by 013.lax.mailroute.net (Postfix) with ESMTPSA id 4jgWyt6twKzlfvqK; Fri, 4 Dec 2026 00:07:34 +0000 (UTC) Message-ID: <9388782a-ac9b-48ab-ae76-095e4cb888cc@acm.org> Date: Thu, 3 Dec 2026 16:07:34 -0800 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] scsi: core: Protect host_busy reads against tag set teardown To: Shuo Qin <23159100078@stu.xidian.edu.cn> Cc: "James E.J. Bottomley" , "Martin K. Petersen" , linux-scsi@vger.kernel.org, Ming Lei , linux-kernel@vger.kernel.org, stable@vger.kernel.org, Bin Yu , Cheng Wen , XinCheng Wang <26031212453@stu.xidian.edu.cn> References: <20261202120443.15341-1-23159100078@stu.xidian.edu.cn> <20261203024144.22289-1-23159100078@stu.xidian.edu.cn> Content-Language: en-US From: Bart Van Assche In-Reply-To: <20261203024144.22289-1-23159100078@stu.xidian.edu.cn> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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);