diff mbox series

[v2] scsi: mvsas: fix potential deadlock on &task->task_state_lock

Message ID 20230928144721.31794-1-dg573847474@gmail.com
State Superseded
Headers show
Series [v2] scsi: mvsas: fix potential deadlock on &task->task_state_lock | expand

Commit Message

Chengfeng Ye Sept. 28, 2023, 2:47 p.m. UTC
&mvi->lock and &task->task_state_lock could be acquired under irq
context from mvs_64xx_isr(). As mvs_port_deformed() also acquire
the lock, called from workqueue sas_suspend_devices() under process
context, and without irq protection.

<deadlock #1>
mvs_port_deformed()
--> mvs_port_notify_deformed()
--> mvs_do_release_task()
--> mvs_int_rx()
--> mvs_slot_complete()
--> spin_lock(&mvi->lock)
<interrupt>
   --> mvs_64xx_isr()
   --> spin_lock(&mvi->lock)

<deadlock #2>
mvs_port_deformed()
--> mvs_port_notify_deformed()
--> mvs_do_release_task()
--> mvs_int_rx()
--> mvs_slot_complete()
--> spin_lock(&task->task_state_lock)
<interrupt>
   --> mvs_64xx_isr()
   --> mvs_int_rx()
   --> mvs_slot_complete()
   --> spin_lock(&task->task_state_lock)

Another strange thing is that while inspecting the code I find
mvs_do_release_task() should be called with phy->mvi locked held,
but the call chain from mvs_port_notify_deformed() seems do not
hold the lock.

The current patch just switch spin_lock(&task->task_state_lock)
to spin_lock_irqsave(). If I didn't miss anything, seems the better
way could be adding a spin_lock_irqsave() to protect mvi->lock at
mvs_port_notify_deformed()?

Signed-off-by: Chengfeng Ye <dg573847474@gmail.com>
---
Change in v2:
  Declare varible name as "irqflags", since "flags" is already used
---
 drivers/scsi/mvsas/mv_sas.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/drivers/scsi/mvsas/mv_sas.c b/drivers/scsi/mvsas/mv_sas.c
index 1444b1f1c4c8..ea48c1e9f185 100644
--- a/drivers/scsi/mvsas/mv_sas.c
+++ b/drivers/scsi/mvsas/mv_sas.c
@@ -1534,6 +1534,7 @@  int mvs_slot_complete(struct mvs_info *mvi, u32 rx_desc, u32 flags)
 	struct mvs_device *mvi_dev = NULL;
 	struct task_status_struct *tstat;
 	struct domain_device *dev;
+	unsigned long irqflags;
 	u32 aborted;
 
 	void *to;
@@ -1546,12 +1547,12 @@  int mvs_slot_complete(struct mvs_info *mvi, u32 rx_desc, u32 flags)
 	dev = task->dev;
 	mvi_dev = dev->lldd_dev;
 
-	spin_lock(&task->task_state_lock);
+	spin_lock_irqsave(&task->task_state_lock, irqflags);
 	task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
 	task->task_state_flags |= SAS_TASK_STATE_DONE;
 	/* race condition*/
 	aborted = task->task_state_flags & SAS_TASK_STATE_ABORTED;
-	spin_unlock(&task->task_state_lock);
+	spin_unlock_irqrestore(&task->task_state_lock, irqflags);
 
 	memset(tstat, 0, sizeof(*tstat));
 	tstat->resp = SAS_TASK_COMPLETE;