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

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