From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796044897; b=DNf4ymiQBgUQJVDi8/zCWVOrGA1J/dTyb9fZFPjOByr8+fWO6A4zOBSvQVKZTOg/zGON B0OnH6cWZemnfzR+Tmk+ZNebHLX0gLVz3OQv60rGEDLAVh/0veTrE8YBYjVEPerbbYdcS ka+smUe0DlxLqQSoiuc5zr+vkkedcEUdIJTvy6TvZFvoi2qQ4cj862rSkLbFWXOSmasBK q82jUAteL+93Jn+9/6bjGeF0C0lyWOgUT2ywEj1ASfKKEl9ndUliLhfg+5ru9ZS3UDycF CllSjmPwbzzr3h6lgRj5pg0pB8mvKIwNaake11WnfjWSAqK0YVvTC/noeHrU/k9c9dQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796044897; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=E5/pucnNMI/c2ojblg/G0Oa6lamaWazaHZpn915p/WM=; b=Xq2XvxV+wmdKrf7C7ydv/p2+y3cLNe5gq3z+bB8yPoqnisadoaxPIb6dTZGxQ1jLRUi3 MbwmNBPmLTF3Ds5PTjYpWeDWx9y6gOhDA42Lia5k9GY0igr/o+z1RU2rB6MyQiIWOje0n FeKtfagi6kH+pFi17MUd+osGPu/adoL9rsvCggm9PSdVXIXwhLF/im9pXoTCovvhzvSZ8 HJYX7J7/F8faPn4ZT+EqLNbl/TgLDx17rSWptrxNd3Fg3Zw8k0WdSTZipGpRklPGT+G5F RSss+o6tDPmumiGZoTVgCBaBHH1OEHy54283GvaQj54hfGMhnxjF3Duy9ZL1ihWdNFw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=C7Sph6rR; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1047A606A68C; Mon, 30 Nov 2026 13:21:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=C7Sph6rR Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9CD661F0089F; Mon, 30 Nov 2026 13:21:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796044896; bh=E5/pucnNMI/c2ojblg/G0Oa6lamaWazaHZpn915p/WM=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=C7Sph6rRRyM6Rf/lHquPUMsbq4kGI0kXqSdb+aAzZhK/JqUAbvJ1pJEIO7lZFq8cF dUJf7EpClFgQ5MZhXgWccQYWGbXYfSKQEG3ze6ZPfIuBV6pdNvjtGOQce9J+5qAisW bFWjPWQg6ShpiFCKq2F4Td3tnomJvVygZWZ9M6leARJbA5nmkCXphOZ7qjdpRipK1T 7EmeD347h+hwveZlRSm6onwE8bzL9hYB6n4iBTW/xIvzs4+gRnIwcoe1THA8Can2ar IfNyPToGFLQBO5kakOiujAgExWO4EWZKyxpvlMGNCqgC8F6/Sdlz8X5uhylFrMYpT/ ckgAmUHztTiEA== From: Jeff Layton Date: Mon, 30 Nov 2026 08:21:03 -0500 Subject: [PATCH v5 01/12] scsi: mpt3sas: Don't leave requeued firmware events pending at teardown 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 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261130-sas-abba-v5-1-487c39d1114f@kernel.org> References: <20261130-sas-abba-v5-0-487c39d1114f@kernel.org> In-Reply-To: <20261130-sas-abba-v5-0-487c39d1114f@kernel.org> To: Sathya Prakash , Sreekanth Reddy , Suganath Prabu Subramani , Ranjan Kumar , "James E.J. Bottomley" , "Martin K. Petersen" , Nagalakshmi Nandigama , James Bottomley Cc: Keith Busch , Sreekanth Reddy , MPT-FusionLinux.pdl@broadcom.com, linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org, Jeff Layton X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=5227; i=jlayton@kernel.org; h=from:subject:message-id; bh=13z5+kTDnv8IEmsjpCIo3UEfpOKkYSck2pI/NA8WCjI=; b=owEBbQKS/ZANAwAKAQAOaEEZVoIVAcsmYgBrDXhas5fpOM0Y/Ck1fQ3eH6mtv2gKQuQ0HYz35 2fHk5v/KSuJAjMEAAEKAB0WIQRLwNeyRHGyoYTq9dMADmhBGVaCFQUCaw14WgAKCRAADmhBGVaC FUtkD/9n0e4qHs9z+3jcuJRErh6ZEb/3bmzOxhcRQjOIXBEMVAsuUnmKXuwzOHfSJcX6L33SnUd AO4ZbvnSjW02JoF08K2b14iyMN5nJ/uXAZSbI6lySaPre741UuKAqw5R+M1qbUavouMAniKzSeS E+gf+uU0+EiRxnfdtXGSuqLd0VcUIvrDVt5axTGCc7XeO30GDZ/uPMxPG13+iEiqaHIvq/NAr3i Pudw5c+Yxf9e4OPhrzqtzU9vZt5LvOWOlFV6x2C4yX/F8mOHgYWEW9Fa+jv8LBmw+mu433rd7T4 CqT/efFcF6DNqf8k6AskNFN9N3peLGkGUbDYQN611nhhkaGD4D2otLfWFX/1fVsUpMTryppT/Iu KN0USAg2ckS/TqNAT/KY4nILqOe1u9mUDeUNPbRtP3GH7cq0/SQGl0/b0ODqK+kXtTm+JYYvMFA 5RL4/53TnfDuSvMJSDBO4rIEVYxEJxkLp2a6XrPTcMpRA1wUdvti1KpCLGzpB6lL5UKA6LejCQb RM9K8/bJuvTVe5AiYIzeMoOeQEORWTW4vJKToVV3mnX0feW0I71igAXWYRWmepPBEIvyC9/QmPC w01MjHkW5BztimlQAI5pFH7X2UypQuYL7MnFejxLEtuzVnqIml2WNFl54+aRf5Uh6/Afm3Xx4/9 VIXZyb61l2OIYCA== X-Developer-Key: i=jlayton@kernel.org; a=openpgp; fpr=4BC0D7B24471B2A184EAF5D3000E684119568215 When a topology change event finds a busy device, _scsih_fw_event_requeue() retries the event. It queues the event's delayed_work on firmware_event_thread, with a one-second delay. _scsih_fw_event_cleanup_queue() cancels only the event's work, not its delayed_work. Thus the timer stays armed. Then scsih_remove() destroys the workqueue. After the workqueue is freed, the timer queues _firmware_event_work_delayed() on it: BUG: KASAN: slab-use-after-free in __queue_work+0xa12/0xb50 Read of size 4 at addr ffff88814fefd1c0 by task swapper/10/0 __queue_work call_timer_fn __run_timer_base ... Allocated by task 11: __alloc_workqueue alloc_workqueue_noprof _scsih_probe [mpt3sas] Freed by task 0: kfree rcu_core ... workqueue: cannot queue _firmware_event_work_delayed [mpt3sas] on wq fw_event_mpt3sas0 The cleanup does not wait for a running topology change event. If such an event finds a busy device, it requeues itself. This can occur after the cleanup passes the event. Fix this: - In the cleanup, cancel the delayed_work of a requeued event. - Do not requeue an event while the cleanup runs or while the driver removes the host. Do this check under fw_event_lock, which the cleanup also takes. - Instead of a requeue, drop the reference that the work held. Previously, this reference leaked when firmware_event_thread was NULL. - Also drop the reference if the delayed_work cannot be queued. Cleanups can overlap. Thus one cleanup can clear fw_events_cleanup while another cleanup cancels the delayed_work. The cancel disables the delayed_work. - Initialize the delayed_work when the driver allocates the event, not at the first requeue. Thus a cleanup never cancels the delayed_work before it is initialized. Fixes: ad5957193107 ("scsi: mpt3sas: Add firmware event requeue support for busy devices") Signed-off-by: Jeff Layton --- drivers/scsi/mpt3sas/mpt3sas_scsih.c | 49 ++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/drivers/scsi/mpt3sas/mpt3sas_scsih.c b/drivers/scsi/mpt3sas/mpt3sas_scsih.c index 04255286a2a6..545913123d01 100644 --- a/drivers/scsi/mpt3sas/mpt3sas_scsih.c +++ b/drivers/scsi/mpt3sas/mpt3sas_scsih.c @@ -320,6 +320,11 @@ static struct fw_event_work *alloc_fw_event_work(int len) return NULL; kref_init(&fw_event->refcount); + /* + * Initialize the delayed_work up front: a cleanup may cancel it + * while the event is being requeued. + */ + INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work_delayed); return fw_event; } @@ -3696,6 +3701,9 @@ _scsih_fw_event_del_from_list(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work * * Context: This function will acquire ioc->fw_event_lock. * + * The reference held by the work that ran @fw_event passes to the delayed + * work. If the event can't be requeued, that reference is dropped. + * * Return nothing. */ static void @@ -3704,20 +3712,31 @@ _scsih_fw_event_requeue(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work { unsigned long flags; - if (ioc->firmware_event_thread == NULL) - return; - spin_lock_irqsave(&ioc->fw_event_lock, flags); + /* + * Don't rearm the event while the queue is being flushed or the + * host is going away. _scsih_fw_event_cleanup_queue() may already + * be past it, and the timer could then fire after the workqueue + * has been destroyed. + */ + if (!ioc->firmware_event_thread || ioc->remove_host || + ioc->fw_events_cleanup) + goto drop; + fw_event->delayed_work_active = 1; + /* + * Cleanups can overlap, and one can clear fw_events_cleanup while + * another is cancelling, and so disabling, the delayed work. + */ + if (!queue_delayed_work(ioc->firmware_event_thread, + &fw_event->delayed_work, msecs_to_jiffies(delay))) + goto drop; fw_event_work_get(fw_event); list_add_tail(&fw_event->list, &ioc->fw_event_list); - if (!fw_event->delayed_work_active) { - fw_event->delayed_work_active = 1; - INIT_DELAYED_WORK(&fw_event->delayed_work, - _firmware_event_work_delayed); - } - queue_delayed_work(ioc->firmware_event_thread, &fw_event->delayed_work, - msecs_to_jiffies(delay)); spin_unlock_irqrestore(&ioc->fw_event_lock, flags); + return; +drop: + spin_unlock_irqrestore(&ioc->fw_event_lock, flags); + fw_event_work_put(fw_event); } /** @@ -3869,9 +3888,17 @@ _scsih_fw_event_cleanup_queue(struct MPT3SAS_ADAPTER *ioc) * * If it did execute, we wait for it to finish, and the put will * happen from _firmware_event_work() + * + * An event requeued by _scsih_fw_event_requeue() is pending + * on its delayed_work instead, and its timer must not fire + * after the workqueue is destroyed. */ - if (cancel_work_sync(&fw_event->work)) + if (fw_event->delayed_work_active) { + if (cancel_delayed_work_sync(&fw_event->delayed_work)) + fw_event_work_put(fw_event); + } else if (cancel_work_sync(&fw_event->work)) { fw_event_work_put(fw_event); + } } ioc->fw_events_cleanup = 0; -- 2.56.1