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

From: Chengfeng Ye
Date: Thu Sep 28 2023 - 10:17:40 EST


&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@xxxxxxxxx>
---
drivers/scsi/mvsas/mv_sas.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/scsi/mvsas/mv_sas.c b/drivers/scsi/mvsas/mv_sas.c
index 1444b1f1c4c8..ddd283ae1b92 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 flags;
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, flags);
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, flags);

memset(tstat, 0, sizeof(*tstat));
tstat->resp = SAS_TASK_COMPLETE;
--
2.17.1