[PATCH] scsi: megaraid: Fix potential deadlock on &adapter->lock

From: Chengfeng Ye
Date: Fri Jul 28 2023 - 14:28:30 EST


As &adapter->lock is acquired by megaraid_isr_iomapped() under irq context,
process context code acquiring the lock should disable irq, otherwise
deadlock could happen if the irq preempt the execution while the
lock is held in process context on the same CPU.

Lock acquisition of &adapter->lock in megaraid_reset_handler() does not
disable irq. Inside the function there is a calling sequence shows like
the below where irq is re-enabled by spin_unlock_irqrestore() on
PENDING_LIST_LOCK(adapter).

spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
...
spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);

spin_lock(&adapter->lock)
...
spin_unlock(&adapter->lock)

This flaw was found by an experimental static analysis tool I am developing
for irq-related deadlock.

The patch fix the potential deadlock by also use spin_lock_irqsave() on
&adapter->lock.

Signed-off-by: Chengfeng Ye <dg573847474@xxxxxxxxx>
---
drivers/scsi/megaraid/megaraid_mbox.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/scsi/megaraid/megaraid_mbox.c b/drivers/scsi/megaraid/megaraid_mbox.c
index ef2b6380e19a..7edddefcc9a3 100644
--- a/drivers/scsi/megaraid/megaraid_mbox.c
+++ b/drivers/scsi/megaraid/megaraid_mbox.c
@@ -2603,7 +2603,7 @@ megaraid_reset_handler(struct scsi_cmnd *scp)
msleep(1000);
}

- spin_lock(&adapter->lock);
+ spin_lock_irqsave(&adapter->lock, flags);

// If still outstanding commands, bail out
if (adapter->outstanding_cmds) {
@@ -2643,7 +2643,7 @@ megaraid_reset_handler(struct scsi_cmnd *scp)
}

out:
- spin_unlock(&adapter->lock);
+ spin_unlock_irqrestore(&adapter->lock, flags);
return rval;
}

--
2.17.1