Blame SOURCES/open-iscsi-2.0.874-31-iscsiuio-serialize-xmit_mutex-lock-to-prevent-iscsiuio-seg-fault.patch

e88930
From: Chris Leech <cleech@redhat.com>
e88930
Subject: iscsiuio: serialize xmit_mutex lock to prevent iscsiuio seg fault
e88930
e88930
Bugzilla: ZZZ
e88930
Upstream Status:
e88930
Build Info: XXX
e88930
Tested:
e88930
e88930
commit a1be9c4ec348f87923f63ce2dbc23893a3b9e45c
e88930
Author: Nilesh Javali <nilesh.javali@cavium.com>
e88930
Date:   Thu May 18 23:04:20 2017 +0530
e88930
e88930
    iscsiuio: serialize xmit_mutex lock to prevent iscsiuio seg fault
e88930
    
e88930
    Signed-off-by: Nilesh Javali <nilesh.javali@cavium.com>
e88930
---
e88930
 iscsiuio/src/unix/libs/bnx2x.c | 24 +++++++++++-------------
e88930
 iscsiuio/src/unix/libs/cnic.c  |  9 +++------
e88930
 iscsiuio/src/unix/libs/qedi.c  | 19 +++++++++++--------
e88930
 3 files changed, 25 insertions(+), 27 deletions(-)
e88930
e88930
diff --git a/iscsiuio/src/unix/libs/bnx2x.c b/iscsiuio/src/unix/libs/bnx2x.c
e88930
index 19cbcecaacca..1e8f532edcdf 100644
e88930
--- a/iscsiuio/src/unix/libs/bnx2x.c
e88930
+++ b/iscsiuio/src/unix/libs/bnx2x.c
e88930
@@ -1316,7 +1316,6 @@ void bnx2x_start_xmit(nic_t *nic, size_t len, u16_t vlan_id)
e88930
 	if ((rx_bd->addr_hi == 0) && (rx_bd->addr_lo == 0)) {
e88930
 		LOG_PACKET(PFX "%s: trying to transmit when device is closed",
e88930
 			   nic->log_name);
e88930
-		pthread_mutex_unlock(&nic->xmit_mutex);
e88930
 		return;
e88930
 	}
e88930
 
e88930
@@ -1343,12 +1342,9 @@ void bnx2x_start_xmit(nic_t *nic, size_t len, u16_t vlan_id)
e88930
 			       (bp->tx_bd_prod << 16));
e88930
 		bnx2x_flush_doorbell(bp, bp->tx_doorbell);
e88930
 	} else {
e88930
-		/* If the doorbell is not rung, the packet will not
e88930
-		   get sent.  Hence, the xmit_mutex lock will not
e88930
-		   get freed.
e88930
-		 */
e88930
-		pthread_mutex_unlock(&nic->xmit_mutex);
e88930
+		LOG_ERR(PFX "Pkt transmission failed.");
e88930
 	}
e88930
+
e88930
 	LOG_PACKET(PFX "%s: sent %d bytes using bp->tx_prod: %d",
e88930
 		   nic->log_name, len, bp->tx_prod);
e88930
 }
e88930
@@ -1412,6 +1408,8 @@ int bnx2x_write(nic_t *nic, nic_interface_t *nic_iface, packet_t *pkt)
e88930
 		   nic->log_name, pkt->buf_size,
e88930
 		   bp->tx_cons, bp->tx_prod, bp->tx_bd_prod);
e88930
 
e88930
+	pthread_mutex_unlock(&nic->xmit_mutex);
e88930
+
e88930
 	return 0;
e88930
 }
e88930
 
e88930
@@ -1560,17 +1558,16 @@ static int bnx2x_clear_tx_intr(nic_t *nic)
e88930
 	hw_cons = bp->get_tx_cons(bp);
e88930
 
e88930
 	if (bp->tx_cons == hw_cons) {
e88930
-		if (bp->tx_cons == bp->tx_prod) {
e88930
-			/* Make sure the xmit_mutex lock is unlock */
e88930
-			if (pthread_mutex_trylock(&nic->xmit_mutex))
e88930
-				LOG_ERR(PFX "bnx2x tx lock with prod == cons");
e88930
-
e88930
-			pthread_mutex_unlock(&nic->xmit_mutex);
e88930
+		if (bp->tx_cons == bp->tx_prod)
e88930
 			return 0;
e88930
-		}
e88930
 		return -EAGAIN;
e88930
 	}
e88930
 
e88930
+	if (pthread_mutex_trylock(&nic->xmit_mutex)) {
e88930
+		LOG_ERR(PFX "%s: unable to get xmit_mutex.", nic->log_name);
e88930
+		return -EINVAL;
e88930
+	}
e88930
+
e88930
 	LOG_PACKET(PFX "%s: clearing tx interrupt [%d %d]",
e88930
 		   nic->log_name, bp->tx_cons, hw_cons);
e88930
 	bp->tx_cons = hw_cons;
e88930
@@ -1600,6 +1597,7 @@ static int bnx2x_clear_tx_intr(nic_t *nic)
e88930
 				   nic->log_name, pkt->buf_size,
e88930
 				   bp->tx_cons, bp->tx_prod, bp->tx_bd_prod);
e88930
 
e88930
+			pthread_mutex_unlock(&nic->xmit_mutex);
e88930
 			return 0;
e88930
 		}
e88930
 
e88930
diff --git a/iscsiuio/src/unix/libs/cnic.c b/iscsiuio/src/unix/libs/cnic.c
e88930
index 5d60f898ad57..a009f25f0814 100644
e88930
--- a/iscsiuio/src/unix/libs/cnic.c
e88930
+++ b/iscsiuio/src/unix/libs/cnic.c
e88930
@@ -141,6 +141,7 @@ static int cnic_arp_send(nic_t *nic, nic_interface_t *nic_iface, int fd,
e88930
 	memcpy(&addr.s_addr, &dst_ip, sizeof(addr.s_addr));
e88930
 	LOG_DEBUG(PFX "%s: Sent cnic arp request for IP: %s",
e88930
 		  nic->log_name, addr_str);
e88930
+	pthread_mutex_unlock(&nic->xmit_mutex);
e88930
 
e88930
 	return 0;
e88930
 }
e88930
@@ -204,6 +205,8 @@ static int cnic_neigh_soliciation_send(nic_t *nic,
e88930
 	LOG_DEBUG(PFX "%s: Sent cnic ICMPv6 neighbor request %s",
e88930
 		  nic->log_name, addr_str);
e88930
 
e88930
+	pthread_mutex_unlock(&nic->xmit_mutex);
e88930
+
e88930
 	return 0;
e88930
 }
e88930
 
e88930
@@ -433,9 +436,6 @@ done:
e88930
 		rc = -EIO;
e88930
 	}
e88930
 
e88930
-	if (status != 0 || rc != 0)
e88930
-		pthread_mutex_unlock(&nic->xmit_mutex);
e88930
-
e88930
 	if (ev) {
e88930
 		cnic_nl_neigh_rsp(nic, fd, ev, path, mac_addr,
e88930
 				  nic_iface, status, AF_INET);
e88930
@@ -632,9 +632,6 @@ done:
e88930
 		rc = -EIO;
e88930
 	}
e88930
 
e88930
-	if (status != 0 || rc != 0)
e88930
-		pthread_mutex_unlock(&nic->xmit_mutex);
e88930
-
e88930
 	if (ev) {
e88930
 		cnic_nl_neigh_rsp(nic, fd, ev, path, mac_addr,
e88930
 				  nic_iface, status, AF_INET6);
e88930
diff --git a/iscsiuio/src/unix/libs/qedi.c b/iscsiuio/src/unix/libs/qedi.c
e88930
index c2096e59dad1..c6ff6e7724a3 100644
e88930
--- a/iscsiuio/src/unix/libs/qedi.c
e88930
+++ b/iscsiuio/src/unix/libs/qedi.c
e88930
@@ -887,7 +887,6 @@ void qedi_start_xmit(nic_t *nic, size_t len, u16_t vlan_id)
e88930
 			   nic->log_name, len, bp->tx_prod);
e88930
 	} else {
e88930
 		LOG_ERR(PFX "Pkt transmission failed: %d", rc);
e88930
-		pthread_mutex_unlock(&nic->xmit_mutex);
e88930
 	}
e88930
 
e88930
 	free(ubuf);
e88930
@@ -950,6 +949,10 @@ int qedi_write(nic_t *nic, nic_interface_t *nic_iface, packet_t *pkt)
e88930
 		   nic->log_name, pkt->buf_size,
e88930
 		   bp->tx_cons, bp->tx_prod, bp->tx_bd_prod);
e88930
 
e88930
+	LOG_DEBUG(PFX "%s: host:%d - releasing xmit mutex",
e88930
+		  nic->log_name, nic->host_no);
e88930
+	pthread_mutex_unlock(&nic->xmit_mutex);
e88930
+
e88930
 	return 0;
e88930
 }
e88930
 
e88930
@@ -1059,17 +1062,16 @@ static int qedi_clear_tx_intr(nic_t *nic)
e88930
 	hw_cons = uctrl->hw_tx_cons;
e88930
 
e88930
 	if (bp->tx_cons == hw_cons) {
e88930
-		if (bp->tx_cons == bp->tx_prod) {
e88930
-			/* Make sure the xmit_mutex lock is unlock */
e88930
-			if (pthread_mutex_trylock(&nic->xmit_mutex))
e88930
-				LOG_ERR(PFX "qedi tx lock with prod == cons");
e88930
-
e88930
-			pthread_mutex_unlock(&nic->xmit_mutex);
e88930
+		if (bp->tx_cons == bp->tx_prod)
e88930
 			return 0;
e88930
-		}
e88930
 		return -EAGAIN;
e88930
 	}
e88930
 
e88930
+	if (pthread_mutex_trylock(&nic->xmit_mutex)) {
e88930
+		LOG_ERR(PFX "%s: unable to get xmit_mutex.", nic->log_name);
e88930
+		return -EINVAL;
e88930
+	}
e88930
+
e88930
 	LOG_PACKET(PFX "%s: clearing tx interrupt [%d %d]",
e88930
 		   nic->log_name, bp->tx_cons, hw_cons);
e88930
 	bp->tx_cons = hw_cons;
e88930
@@ -1099,6 +1101,7 @@ static int qedi_clear_tx_intr(nic_t *nic)
e88930
 				   nic->log_name, pkt->buf_size,
e88930
 				   bp->tx_cons, bp->tx_prod, bp->tx_bd_prod);
e88930
 
e88930
+			pthread_mutex_unlock(&nic->xmit_mutex);
e88930
 			return 0;
e88930
 		}
e88930