mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jeff Layton <jlayton@kernel.org>
To: Sathya Prakash <sathya.prakash@broadcom.com>,
	 Sreekanth Reddy <sreekanth.reddy@broadcom.com>,
	 Suganath Prabu Subramani <suganath-prabu.subramani@broadcom.com>,
	 Ranjan Kumar <ranjan.kumar@broadcom.com>,
	 "James E.J. Bottomley" <James.Bottomley@HansenPartnership.com>,
	 "Martin K. Petersen" <mkp@kernel.org>,
	 Nagalakshmi Nandigama <Nagalakshmi.Nandigama@lsi.com>,
	 James Bottomley <JBottomley@Parallels.com>
Cc: Keith Busch <kbusch@kernel.org>,
	 Sreekanth Reddy <Sreekanth.Reddy@lsi.com>,
	MPT-FusionLinux.pdl@broadcom.com,  linux-scsi@vger.kernel.org,
	linux-kernel@vger.kernel.org,  Jeff Layton <jlayton@kernel.org>
Subject: [PATCH v5 02/12] scsi: mpt3sas: Disarm a requeued event's timer when it leaves the event list
Date: Mon, 30 Nov 2026 08:21:04 -0500	[thread overview]
Message-ID: <20261130-sas-abba-v5-2-487c39d1114f@kernel.org> (raw)
In-Reply-To: <20261130-sas-abba-v5-0-487c39d1114f@kernel.org>

_scsih_fw_event_cleanup_queue() takes an event off fw_event_list in
dequeue_next_fw_event(), under fw_event_lock. It cancels the timer of a
requeued event later, outside the lock. Between these two steps, the
event is not on the list but its timer is still armed.

Nothing serializes scsih_remove() or scsih_shutdown() with a host reset.
Thus a reset cleanup can overlap with the cleanup in teardown, for
example when the driver is unbound during a SCSI EH host reset:

  CPU0 (reset cleanup)                CPU1 (scsih_remove)
  dequeue_next_fw_event()
    takes E off the list
                                      _scsih_fw_event_cleanup_queue()
                                        list is empty, returns
                                      destroy_workqueue(wq)
  E's timer fires
    __queue_work() on the freed wq
  cancel_delayed_work_sync()

Cancel the timer with cancel_delayed_work() in dequeue_next_fw_event(),
under fw_event_lock, together with list_del_init(). The timer path does
not take fw_event_lock, and the driver already queues work under it.
Thus a cleanup that finds the list empty knows that no requeued event
has an armed timer. Keep cancel_delayed_work_sync() for an instance
that is already running, and put the reference of the work if either
cancel took the pending work.

Sashiko found this race in a review of the previous patch.

Fixes: ad5957193107 ("scsi: mpt3sas: Add firmware event requeue support for busy devices")
Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
 drivers/scsi/mpt3sas/mpt3sas_scsih.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)

diff --git a/drivers/scsi/mpt3sas/mpt3sas_scsih.c b/drivers/scsi/mpt3sas/mpt3sas_scsih.c
index 545913123d01..f97606785eac 100644
--- a/drivers/scsi/mpt3sas/mpt3sas_scsih.c
+++ b/drivers/scsi/mpt3sas/mpt3sas_scsih.c
@@ -3800,16 +3800,32 @@ mpt3sas_port_enable_complete(struct MPT3SAS_ADAPTER *ioc)
 	fw_event_work_put(fw_event);
 }
 
-static struct fw_event_work *dequeue_next_fw_event(struct MPT3SAS_ADAPTER *ioc)
+/*
+ * Take the next event off fw_event_list.  *@disarmed is set if this
+ * cancelled the pending delayed_work of a requeued event, and the caller
+ * then puts the reference that the work held.
+ */
+static struct fw_event_work *dequeue_next_fw_event(struct MPT3SAS_ADAPTER *ioc,
+						   bool *disarmed)
 {
 	unsigned long flags;
 	struct fw_event_work *fw_event = NULL;
 
+	*disarmed = false;
 	spin_lock_irqsave(&ioc->fw_event_lock, flags);
 	if (!list_empty(&ioc->fw_event_list)) {
 		fw_event = list_first_entry(&ioc->fw_event_list,
 				struct fw_event_work, list);
 		list_del_init(&fw_event->list);
+		/*
+		 * Disarm the timer of a requeued event in the same step that
+		 * takes the event off the list.  An overlapping cleanup that
+		 * finds the list empty, such as the one in scsih_remove(),
+		 * can then destroy the workqueue.  The timer must not fire
+		 * after that.
+		 */
+		if (fw_event->delayed_work_active)
+			*disarmed = cancel_delayed_work(&fw_event->delayed_work);
 		fw_event_work_put(fw_event);
 	}
 	spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
@@ -3830,6 +3846,7 @@ static void
 _scsih_fw_event_cleanup_queue(struct MPT3SAS_ADAPTER *ioc)
 {
 	struct fw_event_work *fw_event;
+	bool disarmed;
 
 	if ((list_empty(&ioc->fw_event_list) && !ioc->current_event) ||
 	    !ioc->firmware_event_thread)
@@ -3844,7 +3861,7 @@ _scsih_fw_event_cleanup_queue(struct MPT3SAS_ADAPTER *ioc)
 		ioc->current_event->ignore = 1;
 
 	ioc->fw_events_cleanup = 1;
-	while ((fw_event = dequeue_next_fw_event(ioc)) ||
+	while ((fw_event = dequeue_next_fw_event(ioc, &disarmed)) ||
 	     (fw_event = ioc->current_event)) {
 
 		/*
@@ -3891,10 +3908,13 @@ _scsih_fw_event_cleanup_queue(struct MPT3SAS_ADAPTER *ioc)
 		 *
 		 * 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.
+		 * after the workqueue is destroyed.  dequeue_next_fw_event()
+		 * already disarmed a pending timer.  If either cancel took
+		 * the pending work, put the reference that the work held.
 		 */
 		if (fw_event->delayed_work_active) {
-			if (cancel_delayed_work_sync(&fw_event->delayed_work))
+			if (cancel_delayed_work_sync(&fw_event->delayed_work) ||
+			    disarmed)
 				fw_event_work_put(fw_event);
 		} else if (cancel_work_sync(&fw_event->work)) {
 			fw_event_work_put(fw_event);

-- 
2.56.1


  parent reply	other threads:[~2026-11-30 13:21 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 13:21 [PATCH v5 00/12] scsi: mpt3sas: Fix delayed not-responding block deadlock and related bugs Jeff Layton
2026-11-30 13:21 ` [PATCH v5 01/12] scsi: mpt3sas: Don't leave requeued firmware events pending at teardown Jeff Layton
2026-11-30 13:21 ` Jeff Layton [this message]
2026-11-30 13:21 ` [PATCH v5 03/12] scsi: mpt3sas: Hold event references while cleaning up the event queue Jeff Layton
2026-11-30 13:21 ` [PATCH v5 04/12] scsi: mpt3sas: Requeue topology events only for busy devices Jeff Layton
2026-11-30 13:21 ` [PATCH v5 05/12] scsi: mpt3sas: Fix pcie_device leak in _scsih_ublock_io_device_wait() Jeff Layton
2026-11-30 13:21 ` [PATCH v5 06/12] scsi: mpt3sas: Restore unblock retry " Jeff Layton
2026-11-30 13:21 ` [PATCH v5 07/12] scsi: mpt3sas: Stop polling devices when the unresponding scan is cancelled Jeff Layton
2026-11-30 13:21 ` [PATCH v5 08/12] scsi: mpt3sas: Reset the TUR retry count for each device Jeff Layton
2026-11-30 13:21 ` [PATCH v5 09/12] scsi: mpt3sas: Cache the missing delays the IOC actually uses Jeff Layton
2026-11-30 13:21 ` [PATCH v5 10/12] scsi: mpt3sas: Serialize interrupt-time sdev block with hostdata teardown Jeff Layton
2026-11-30 13:21 ` [PATCH v5 11/12] scsi: mpt3sas: Don't use freed hostdata while polling devices Jeff Layton
2026-11-30 13:21 ` [PATCH v5 12/12] scsi: mpt3sas: Time out delayed not-responding device blocks Jeff Layton

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=20261130-sas-abba-v5-2-487c39d1114f@kernel.org \
    --to=jlayton@kernel.org \
    --cc=JBottomley@Parallels.com \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=MPT-FusionLinux.pdl@broadcom.com \
    --cc=Nagalakshmi.Nandigama@lsi.com \
    --cc=Sreekanth.Reddy@lsi.com \
    --cc=kbusch@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=mkp@kernel.org \
    --cc=ranjan.kumar@broadcom.com \
    --cc=sathya.prakash@broadcom.com \
    --cc=sreekanth.reddy@broadcom.com \
    --cc=suganath-prabu.subramani@broadcom.com \
    /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®