Skip to contentSkip to conversation

Fix: scsi: megaraid: reduce the scope of pending-list lock to avoid double lock

PATCH In review [email protected] 2 participants 3 messages Oct 17, 2016 – Nov 11, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix: scsi: megaraid: reduce the scope of pending-list lock to avoid double lock

From: Iago Abal <[email protected]>

The EBA code analyzer (https://github.com/models-team/eba) reported the following double lock:

1. In function `megaraid_reset_handler' at 2571;
2. take `&adapter->pend_list_lock' for the first time at 2602:

// FIRST spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);

3. enter the `list_for_each_entry_safe' loop at 2603;
4. call `megaraid_mbox_mm_done' at 2616;
5. call `megaraid_mbox_runpendq' at 3782;
6. take `&adapter->pend_list_lock' for the second time at 1892:

// SECOND: DOUBLE LOCK !!! spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);

Quoted 1 line

From my shallow understanding of the code (so please review carefully), I think

that it is not necessary to hold PENDING_LIST_LOCK(adapter)' while executing the body of the list_for_each_entry_safe' loop. I assume this because both megaraid_mbox_mm_done' and megaraid_dealloc_scb' are called from several places where, as far as I can tell, this lock is not hold. In fact, as reported by EBA, at some point `megaraid_mbox_mm_done' will acquire this lock again.

Fixes c005fb4fb2d2 ("[SCSI] megaraid_{mm,mbox}: fix a bug in reset handler")
---
 drivers/scsi/megaraid/megaraid_mbox.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/scsi/megaraid/megaraid_mbox.c +2 -0
index f0987f2..7f11898 100644
@@ -2603,6 +2603,7 @@ static DEF_SCSI_QCMD(megaraid_queue_command)
 	list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
 		list_del_init(&scb->list);	// from pending list
 
+		spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
 		if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
 			con_log(CL_ANN, (KERN_WARNING
 			"megaraid: IOCTL packet with %d[%d:%d] being reset\n",
@@ -2630,6 +2631,7 @@ static DEF_SCSI_QCMD(megaraid_queue_command)
 
 			megaraid_dealloc_scb(adapter, scb);
 		}
+		spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
 	}
 	spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
 
Signature · 1 line
1.9.1
maintainer
#1 lore raw
Quoted 5 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]]
Sent: Monday, October 17, 2016 1:00 PM
To: Jiri Kosina
Cc: Kashyap Desai; Sumit Saxena; Uday Lingala; James E.J. Bottomley;

Martin K.

Quoted 1 line

Petersen; [email protected]; [email protected];

Iago

Quoted 2 lines

Abal
Subject: [PATCH] Fix: scsi: megaraid: reduce the scope of pending-list

lock to

Quoted 18 lines

avoid double lock

From: Iago Abal <[email protected]>

The EBA code analyzer (https://github.com/models-team/eba) reported the following double lock:

1. In function `megaraid_reset_handler' at 2571;
2. take `&adapter->pend_list_lock' for the first time at 2602:

// FIRST spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);

3. enter the `list_for_each_entry_safe' loop at 2603;
4. call `megaraid_mbox_mm_done' at 2616;
5. call `megaraid_mbox_runpendq' at 3782;
6. take `&adapter->pend_list_lock' for the second time at 1892:

Quoted 4 lines

// SECOND: DOUBLE LOCK !!! spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);

From my shallow understanding of the code (so please review carefully),

I think

Quoted 1 line

that it is not necessary to hold `PENDING_LIST_LOCK(adapter)' while

executing

Quoted 1 line

the body of the `list_for_each_entry_safe' loop. I assume this because

both

Quoted 2 lines

megaraid_mbox_mm_done' and megaraid_dealloc_scb' are called from several places where, as far as I can tell, this lock is not hold. In

fact, as reported

Quoted 1 line

by EBA, at some point `megaraid_mbox_mm_done' will acquire this lock

again.

Quoted 2 lines
Fixes c005fb4fb2d2 ("[SCSI] megaraid_{mm,mbox}: fix a bug in reset

handler")

Quoted 21 lines
---
 drivers/scsi/megaraid/megaraid_mbox.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/scsi/megaraid/megaraid_mbox.c +1 -0
index f0987f2..7f11898 100644
@@ -2603,6 +2603,7 @@ static DEF_SCSI_QCMD(megaraid_queue_command)
 	list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
 		list_del_init(&scb->list);	// from pending list
 
+		spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
 		if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
 			con_log(CL_ANN, (KERN_WARNING
 			"megaraid: IOCTL packet with %d[%d:%d] being

reset\n", @@ -2630,6 +2631,7 @@ static
DEF_SCSI_QCMD(megaraid_queue_command)

drivers/scsi/megaraid/megaraid_mbox.c static DEF_SCSI_QCMD(megaraid_queue_command) · line 2636

Quoted 5 lines
drivers/scsi/megaraid/megaraid_mbox.c +1 -0
 			megaraid_dealloc_scb(adapter, scb);
 		}
+		spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
 	}
 	spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);

Looks correct, but please note that MEGARAID_MAILBOX and MEGARAID_MM is
not supported by LSI/ Broadcom. We will revert back to you shortly if we
can safely remove those two modules.

.

Quoted 3 lines

--
1.9.1

maintainer
#2 lore raw
In reply to [email protected]
Quoted 5 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]]
Sent: Monday, October 17, 2016 1:00 PM
To: Jiri Kosina
Cc: Kashyap Desai; Sumit Saxena; Uday Lingala; James E.J. Bottomley;

Martin K.

Quoted 1 line

Petersen; [email protected]; [email protected];

Iago

Quoted 2 lines

Abal
Subject: [PATCH] Fix: scsi: megaraid: reduce the scope of pending-list

lock to

Quoted 18 lines

avoid double lock

From: Iago Abal <[email protected]>

The EBA code analyzer (https://github.com/models-team/eba) reported the following double lock:

1. In function `megaraid_reset_handler' at 2571;
2. take `&adapter->pend_list_lock' for the first time at 2602:

// FIRST spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);

3. enter the `list_for_each_entry_safe' loop at 2603;
4. call `megaraid_mbox_mm_done' at 2616;
5. call `megaraid_mbox_runpendq' at 3782;
6. take `&adapter->pend_list_lock' for the second time at 1892:

Quoted 4 lines

// SECOND: DOUBLE LOCK !!! spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);

From my shallow understanding of the code (so please review carefully),

I think

Quoted 1 line

that it is not necessary to hold `PENDING_LIST_LOCK(adapter)' while

executing

Quoted 1 line

the body of the `list_for_each_entry_safe' loop. I assume this because

both

Quoted 2 lines

megaraid_mbox_mm_done' and megaraid_dealloc_scb' are called from several places where, as far as I can tell, this lock is not hold. In

fact, as reported

Quoted 1 line

by EBA, at some point `megaraid_mbox_mm_done' will acquire this lock

again.

Quoted 2 lines
Fixes c005fb4fb2d2 ("[SCSI] megaraid_{mm,mbox}: fix a bug in reset

handler")

Quoted 21 lines
---
 drivers/scsi/megaraid/megaraid_mbox.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/scsi/megaraid/megaraid_mbox.c +1 -0
index f0987f2..7f11898 100644
@@ -2603,6 +2603,7 @@ static DEF_SCSI_QCMD(megaraid_queue_command)
 	list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
 		list_del_init(&scb->list);	// from pending list
 
+		spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
 		if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
 			con_log(CL_ANN, (KERN_WARNING
 			"megaraid: IOCTL packet with %d[%d:%d] being

reset\n", @@ -2630,6 +2631,7 @@ static
DEF_SCSI_QCMD(megaraid_queue_command)

drivers/scsi/megaraid/megaraid_mbox.c static DEF_SCSI_QCMD(megaraid_queue_command) · line 2636

Quoted 5 lines
drivers/scsi/megaraid/megaraid_mbox.c +1 -0
 			megaraid_dealloc_scb(adapter, scb);
 		}
+		spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
 	}
 	spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);

Sorry for delay. We had internal discussion and confirm that it is safe to remove mbox driver from mainline as this product is discontinued and we are planning to post patch to remove megaraid mbox driver from mainline.

Quoted 3 lines

--
1.9.1