Skip to contentSkip to conversation

staging: vt6655: Implement allocation failure handling

RFC PATCH In review Nam Cao 2 participants 6 messages Sep 28, 2022 – Sep 29, 2022 Staging

lore
Share & tools
lore

Nam Cao

author
#0 lore raw

v2 cover staging: vt6655: Implement allocation failure handling

This driver does not handle allocation failure when receiving data very well. This patchset implements better handling in the case of allocation failure.

Also do some necessary clean-up to implement this.

v2:
- squash 3 commits that were doing a single thing
- add new commit which removes a redundant assignment
- take device_init_rx_desc() out of unnecessary else condition.
- remove return statement at the end of void function
- add a missing rd = rd->next statement in device_rx_srv(): because we already drop the current buffer, we should move on to the next buffer in the ring where new data will be written to.

Nam Cao (4):
staging: vt6655: remove redundant if condition
staging: vt6655: change vnt_receive_frame return type to void
staging: vt6655: remove redundant assignment
staging: vt6655: implement allocation failure handling

 drivers/staging/vt6655/device_main.c | 40 +++++++++++++++++-----------
 drivers/staging/vt6655/dpc.c         |  8 +++---
 drivers/staging/vt6655/dpc.h         |  2 +-
 3 files changed, 28 insertions(+), 22 deletions(-)
Signature · 1 line
2.25.1
author
#1 lore raw

RFC PATCH v2 1/4 staging: vt6655: remove redundant if condition

The function vnt_receive_frame always returns true, so checking its return value is redundant. Remove it.

---
 drivers/staging/vt6655/device_main.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/staging/vt6655/device_main.c +6 -6
index 56c3cf3ba53d..21d10d9fde47 100644
@@ -832,12 +832,12 @@ static int device_rx_srv(struct vnt_private *priv, unsigned int idx)
 		if (!rd->rd_info->skb)
 			break;
 
-		if (vnt_receive_frame(priv, rd)) {
-			if (!device_alloc_rx_buf(priv, rd)) {
-				dev_err(&priv->pcid->dev,
-					"can not allocate rx buf\n");
-				break;
-			}
+		vnt_receive_frame(priv, rd);
+
+		if (!device_alloc_rx_buf(priv, rd)) {
+			dev_err(&priv->pcid->dev,
+				"can not allocate rx buf\n");
+			break;
 		}
 		rd->rd0.owner = OWNED_BY_NIC;
 	}
Signature · 1 line
2.25.1
author
#2 lore raw

RFC PATCH v2 2/4 staging: vt6655: change vnt_receive_frame return type to void

In reply to Nam Cao

The function vnt_receive_frame's returned value is not used anywhere. Furthermore, it always return true. Change its return type to void.

---
 drivers/staging/vt6655/dpc.c | 8 +++-----
 drivers/staging/vt6655/dpc.h | 2 +-
 2 files changed, 4 insertions(+), 6 deletions(-)
drivers/staging/vt6655/dpc.c +3 -5
index c6ed3537f439..3bf60039fa9a 100644
@@ -115,7 +115,7 @@ static bool vnt_rx_data(struct vnt_private *priv, struct sk_buff *skb,
 	return true;
 }
 
-bool vnt_receive_frame(struct vnt_private *priv, struct vnt_rx_desc *curr_rd)
+void vnt_receive_frame(struct vnt_private *priv, struct vnt_rx_desc *curr_rd)
 {
 	struct vnt_rd_info *rd_info = curr_rd->rd_info;
 	struct sk_buff *skb;
@@ -133,13 +133,11 @@ bool vnt_receive_frame(struct vnt_private *priv, struct vnt_rx_desc *curr_rd)
 		/* Frame Size error drop this packet.*/
 		dev_dbg(&priv->pcid->dev, "Wrong frame size %d\n", frame_size);
 		dev_kfree_skb_irq(skb);
-		return true;
+		return;
 	}
 
 	if (vnt_rx_data(priv, skb, frame_size))
-		return true;
+		return;
 
 	dev_kfree_skb_irq(skb);
-
-	return true;
 }
drivers/staging/vt6655/dpc.h +1 -1
index 40364c0ab7f6..f67c1ef23171 100644
@@ -16,6 +16,6 @@
 
 #include "device.h"
 
-bool vnt_receive_frame(struct vnt_private *priv, struct vnt_rx_desc *curr_rd);
+void vnt_receive_frame(struct vnt_private *priv, struct vnt_rx_desc *curr_rd);
 
 #endif /* __RXTX_H__ */
Signature · 1 line
2.25.1
author
#3 lore raw

RFC PATCH v2 3/4 staging: vt6655: remove redundant assignment

In reply to Nam Cao

In function device_rx_srv, the assignment rd->rd0.owner = OWNED_BY_NIC is redundant because this is already done in device_alloc_rx_buf() which is called right above. Remove it.

---
 drivers/staging/vt6655/device_main.c | 1 -
 1 file changed, 1 deletion(-)
drivers/staging/vt6655/device_main.c +0 -1
index 21d10d9fde47..c8cae6df7f51 100644
@@ -839,7 +839,6 @@ static int device_rx_srv(struct vnt_private *priv, unsigned int idx)
 				"can not allocate rx buf\n");
 			break;
 		}
-		rd->rd0.owner = OWNED_BY_NIC;
 	}
 
 	priv->pCurrRD[idx] = rd;
Signature · 1 line
2.25.1
author
#4 lore raw

RFC PATCH v2 4/4 staging: vt6655: implement allocation failure handling

In reply to Nam Cao

The function device_rx_srv does not handle allocation failure very well. Currently, it performs these steps:
- Unmap DMA buffer and hand over the buffer to mac80211
- Allocate and dma-map new buffer
- If allocation fails, abort

The problem is that, it aborts while still marking the buffer as OWNED_BY_HOST. So when this function is called again in the future, it incorrectly perceives the same buffer as valid and dma-unmap and hand over this buffer to mac80211 again.

Re-implement this function to do things in a different order:
- Allocate and dma-map new buffer
- If allocation fails, abort and give up the ownership of the buffer (so that the device can re-use this buffer)
- If allocation does not fail: unmap dma buffer and hand over the buffer to mac80211

Thus, when the driver cannot allocate new buffer, it simply discards the received data and re-use the current buffer.

Also split device_alloc_rx_buf() into 2 parts: allocating new buffer and initializing the buffer, so that we can read the old buffer in between.

---
 drivers/staging/vt6655/device_main.c | 33 ++++++++++++++++++----------
 1 file changed, 21 insertions(+), 12 deletions(-)
drivers/staging/vt6655/device_main.c +21 -12
index c8cae6df7f51..cc952acd9825 100644
@@ -132,7 +132,8 @@ static int device_init_td1_ring(struct vnt_private *priv);
 
 static int  device_rx_srv(struct vnt_private *priv, unsigned int idx);
 static int  device_tx_srv(struct vnt_private *priv, unsigned int idx);
-static bool device_alloc_rx_buf(struct vnt_private *, struct vnt_rx_desc *);
+static bool device_alloc_rx_buf(struct vnt_private *, struct vnt_rd_info *);
+static void device_init_rx_desc(struct vnt_private *priv, struct vnt_rx_desc *rd);
 static void device_free_rx_buf(struct vnt_private *priv,
 			       struct vnt_rx_desc *rd);
 static void device_init_registers(struct vnt_private *priv);
@@ -611,12 +612,13 @@ static int device_init_rd0_ring(struct vnt_private *priv)
 			goto err_free_desc;
 		}
 
-		if (!device_alloc_rx_buf(priv, desc)) {
+		if (!device_alloc_rx_buf(priv, desc->rd_info)) {
 			dev_err(&priv->pcid->dev, "can not alloc rx bufs\n");
 			ret = -ENOMEM;
 			goto err_free_rd;
 		}
 
+		device_init_rx_desc(priv, desc);
 		desc->next = &priv->aRD0Ring[(i + 1) % priv->opts.rx_descs0];
 		desc->next_desc = cpu_to_le32(curr + sizeof(struct vnt_rx_desc));
 	}
@@ -657,12 +659,13 @@ static int device_init_rd1_ring(struct vnt_private *priv)
 			goto err_free_desc;
 		}
 
-		if (!device_alloc_rx_buf(priv, desc)) {
+		if (!device_alloc_rx_buf(priv, desc->rd_info)) {
 			dev_err(&priv->pcid->dev, "can not alloc rx bufs\n");
 			ret = -ENOMEM;
 			goto err_free_rd;
 		}
 
+		device_init_rx_desc(priv, desc);
 		desc->next = &priv->aRD1Ring[(i + 1) % priv->opts.rx_descs1];
 		desc->next_desc = cpu_to_le32(curr + sizeof(struct vnt_rx_desc));
 	}
@@ -821,6 +824,7 @@ static void device_free_td1_ring(struct vnt_private *priv)
 static int device_rx_srv(struct vnt_private *priv, unsigned int idx)
 {
 	struct vnt_rx_desc *rd;
+	struct vnt_rd_info new_info;
 	int works = 0;
 
 	for (rd = priv->pCurrRD[idx];
@@ -832,13 +836,18 @@ static int device_rx_srv(struct vnt_private *priv, unsigned int idx)
 		if (!rd->rd_info->skb)
 			break;
 
-		vnt_receive_frame(priv, rd);
-
-		if (!device_alloc_rx_buf(priv, rd)) {
+		if (!device_alloc_rx_buf(priv, &new_info)) {
 			dev_err(&priv->pcid->dev,
 				"can not allocate rx buf\n");
+			rd->rd0.owner = OWNED_BY_NIC;
+			rd = rd->next;
 			break;
 		}
+
+		vnt_receive_frame(priv, rd);
+
+		memcpy(rd->rd_info, &new_info, sizeof(new_info));
+		device_init_rx_desc(priv, rd);
 	}
 
 	priv->pCurrRD[idx] = rd;
@@ -847,10 +856,8 @@ static int device_rx_srv(struct vnt_private *priv, unsigned int idx)
 }
 
 static bool device_alloc_rx_buf(struct vnt_private *priv,
-				struct vnt_rx_desc *rd)
+				struct vnt_rd_info *rd_info)
 {
-	struct vnt_rd_info *rd_info = rd->rd_info;
-
 	rd_info->skb = dev_alloc_skb((int)priv->rx_buf_sz);
 	if (!rd_info->skb)
 		return false;
@@ -864,15 +871,17 @@ static bool device_alloc_rx_buf(struct vnt_private *priv,
 		rd_info->skb = NULL;
 		return false;
 	}
+	return true;
+}
 
+static void device_init_rx_desc(struct vnt_private *priv, struct vnt_rx_desc *rd)
+{
 	*((unsigned int *)&rd->rd0) = 0; /* FIX cast */
 
 	rd->rd0.res_count = cpu_to_le16(priv->rx_buf_sz);
 	rd->rd0.owner = OWNED_BY_NIC;
 	rd->rd1.req_count = cpu_to_le16(priv->rx_buf_sz);
-	rd->buff_addr = cpu_to_le32(rd_info->skb_dma);
-
-	return true;
+	rd->buff_addr = cpu_to_le32(rd->rd_info->skb_dma);
 }
 
 static void device_free_rx_buf(struct vnt_private *priv,
Signature · 1 line
2.25.1
Tested-by on the v2 series
#5 lore raw
In reply to Nam Cao
Quoted 21 lines · Nam Cao

This driver does not handle allocation failure when receiving data very well. This patchset implements better handling in the case of allocation failure.

Also do some necessary clean-up to implement this.

v2:
- squash 3 commits that were doing a single thing
- add new commit which removes a redundant assignment
- take device_init_rx_desc() out of unnecessary else condition.
- remove return statement at the end of void function
- add a missing rd = rd->next statement in device_rx_srv(): because we already drop the current buffer, we should move on to the next buffer in the ring where new data will be written to.

Nam Cao (4):
staging: vt6655: remove redundant if condition
staging: vt6655: change vnt_receive_frame return type to void
staging: vt6655: remove redundant assignment
staging: vt6655: implement allocation failure handling

Quoted 5 lines
  drivers/staging/vt6655/device_main.c | 40 +++++++++++++++++-----------
  drivers/staging/vt6655/dpc.c         |  8 +++---
  drivers/staging/vt6655/dpc.h         |  2 +-
  3 files changed, 28 insertions(+), 22 deletions(-)