[PATCH v5 02/12] scsi: mpt3sas: Disarm a requeued event's timer when it leaves the event list
From: Jeff Layton
Date: Mon Nov 30 2026 - 08:22:08 EST
_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@xxxxxxxxxx>
---
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