DPDK-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Junlong Wang <wang.junlong1@zte.com.cn>
To: stephen@networkplumber.org
Cc: dev@dpdk.org, Junlong Wang <wang.junlong1@zte.com.cn>, stable@dpdk.org
Subject: [PATCH v1 2/2] net/zxdh: avoid leaking flow entry on validation
Date: Sat, 28 Nov 2026 12:04:58 +0800	[thread overview]
Message-ID: <20261128040458.3967016-3-wang.junlong1@zte.com.cn> (raw)
In-Reply-To: <20261128040458.3967016-1-wang.junlong1@zte.com.cn>


[-- Attachment #1.1.1: Type: text/plain, Size: 7873 bytes --]

zxdh_flow_validate() reuses the pattern and action parsers of
zxdh_flow_create(), but passes dh_flow as NULL; both parsers then
rte_zmalloc() a struct zxdh_flow_info to parse into.

Neither parser releases that allocation on the validation path.
fd_flow_parse_pattern() has no cleanup at all, so the memory leaks on
its success return and on all ten of its early returns.
fd_flow_parse_action() only frees through its free_flow label, so its
success return and the four checks made after the action loop leak as
well. Each validate therefore leaks 368 bytes of the rte_malloc heap,
and rte_flow_validate() is a side-effect free API that an application
may call as often as it wants.

Keep the parse code shared, but make it fall back to a zeroed local
struct zxdh_flow_info when dh_flow is NULL instead of allocating one.
The memory then goes away with the stack frame, no return path needs
cleanup, and the free_flow label of fd_flow_parse_action() has nothing
left to do.

The local struct is zeroed exactly like rte_zmalloc() was, it never
escapes the parser, and the create path keeps parsing into dh_flow
unchanged. Stack use grows by 184 bytes per parser.

Fixes: d0af48c1b952 ("net/zxdh: support flow API")
Cc: stable@dpdk.org

Signed-off-by: Junlong Wang <wang.junlong1@zte.com.cn>
---
 drivers/net/zxdh/zxdh_flow.c | 58 ++++++++++--------------------------
 1 file changed, 16 insertions(+), 42 deletions(-)

diff --git a/drivers/net/zxdh/zxdh_flow.c b/drivers/net/zxdh/zxdh_flow.c
index a51ce9ef77..776c02117f 100644
--- a/drivers/net/zxdh/zxdh_flow.c
+++ b/drivers/net/zxdh/zxdh_flow.c
@@ -1238,7 +1238,8 @@ static int fd_flow_parse_pattern(struct rte_eth_dev *dev, const struct rte_flow_
 			 struct rte_flow_error *error, struct zxdh_flow *dh_flow)
 {
 	struct zxdh_hw *priv = dev->data->dev_private;
-	struct zxdh_flow_info *flow = NULL;
+	struct zxdh_flow_info local_flow = {0};
+	struct zxdh_flow_info *flow = dh_flow ? &dh_flow->flowentry : &local_flow;
 	const struct rte_flow_item *item;
 	const struct rte_flow_item_eth *eth_spec, *eth_mask;
 	const struct rte_flow_item_vlan *vlan_spec, *vlan_mask;
@@ -1250,18 +1251,6 @@ static int fd_flow_parse_pattern(struct rte_eth_dev *dev, const struct rte_flow_
 	const struct rte_flow_item_vxlan *vxlan_spec, *vxlan_mask;
 	struct fd_flow_key *key, *key_mask;
 
-	if (dh_flow) {
-		flow = &dh_flow->flowentry;
-	} else {
-		flow = rte_zmalloc("dh_flow", sizeof(*flow), 0);
-		if (flow == NULL) {
-			rte_flow_error_set(error, EINVAL,
-						 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
-						 "Failed to allocate memory ");
-			return -rte_errno;
-		}
-	}
-
 	key = &flow->fd_flow.key;
 	key_mask = &flow->fd_flow.key_mask;
 	key->vfid = rte_cpu_to_be_16(priv->vfid);
@@ -1648,7 +1637,8 @@ static int
 fd_flow_parse_action(struct rte_eth_dev *dev, const struct rte_flow_action *actions,
 			 struct rte_flow_error *error, struct zxdh_flow *dh_flow)
 {
-	struct zxdh_flow_info *flow = NULL;
+	struct zxdh_flow_info local_flow = {0};
+	struct zxdh_flow_info *flow = dh_flow ? &dh_flow->flowentry : &local_flow;
 	struct fd_flow_result *result = NULL;
 	const struct rte_flow_item *enc_item = NULL;
 	uint8_t action_bitmap = 0;
@@ -1658,17 +1648,6 @@ fd_flow_parse_action(struct rte_eth_dev *dev, const struct rte_flow_action *acti
 	int ret;
 
 	rte_errno = 0;
-	if (dh_flow) {
-		flow = &dh_flow->flowentry;
-	} else {
-		flow = rte_zmalloc("dh_flow", sizeof(*flow), 0);
-		if (flow == NULL) {
-			rte_flow_error_set(error, EINVAL,
-					 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
-					 "Failed to allocate memory ");
-			return -rte_errno;
-		}
-	}
 	result = &flow->fd_flow.result;
 	action_bitmap = result->action_idx;
 
@@ -1681,11 +1660,11 @@ fd_flow_parse_action(struct rte_eth_dev *dev, const struct rte_flow_action *acti
 				rte_flow_error_set(error, EINVAL,
 						RTE_FLOW_ERROR_TYPE_ACTION, actions,
 						"rss action does no support.");
-				goto free_flow;
+				return -rte_errno;
 			}
 			ret = validate_action_rss(dev, actions, error);
 			if (ret)
-				goto free_flow;
+				return -rte_errno;
 			action_bitmap |= (1 << FD_ACTION_RSS_BIT);
 			break;
 		}
@@ -1696,7 +1675,7 @@ fd_flow_parse_action(struct rte_eth_dev *dev, const struct rte_flow_action *acti
 				rte_flow_error_set(error, EINVAL,
 						RTE_FLOW_ERROR_TYPE_ACTION, actions,
 						"multi mark action no support.");
-				goto free_flow;
+				return -rte_errno;
 			}
 			const struct rte_flow_action_mark *act_mark = actions->conf;
 			result->mark_fd_id = rte_cpu_to_le_32(act_mark->id);
@@ -1710,14 +1689,14 @@ fd_flow_parse_action(struct rte_eth_dev *dev, const struct rte_flow_action *acti
 				rte_flow_error_set(error, EINVAL,
 						RTE_FLOW_ERROR_TYPE_ACTION, actions,
 						"multi count action no support.");
-				goto free_flow;
+				return -rte_errno;
 			}
 			const struct rte_flow_action_count *act_count = actions->conf;
 			if (act_count->id > MAX_FLOW_COUNT_NUM) {
 				rte_flow_error_set(error, EINVAL,
 							RTE_FLOW_ERROR_TYPE_ACTION, actions,
 							"count action id no support.");
-				goto free_flow;
+				return -rte_errno;
 			};
 			result->countid = act_count->id;
 			action_bitmap |= (1 << FD_ACTION_COUNT_BIT);
@@ -1730,7 +1709,7 @@ fd_flow_parse_action(struct rte_eth_dev *dev, const struct rte_flow_action *acti
 				rte_flow_error_set(error, EINVAL,
 						RTE_FLOW_ERROR_TYPE_ACTION, actions,
 						"multi queue action no support.");
-				goto free_flow;
+				return -rte_errno;
 			}
 			const struct rte_flow_action_queue *act_q;
 			act_q = actions->conf;
@@ -1738,14 +1717,14 @@ fd_flow_parse_action(struct rte_eth_dev *dev, const struct rte_flow_action *acti
 				rte_flow_error_set(error, EINVAL,
 						RTE_FLOW_ERROR_TYPE_ACTION, actions,
 						"Invalid queue ID");
-				goto free_flow;
+				return -rte_errno;
 			}
 			ret = zxdh_hw_qid_to_logic_qid(dev, act_q->index << 1);
 			if (ret < 0) {
 				rte_flow_error_set(error, EINVAL,
 						RTE_FLOW_ERROR_TYPE_ACTION, actions,
 						"Invalid phy queue ID .");
-				goto free_flow;
+				return -rte_errno;
 			}
 			result->qid = rte_cpu_to_le_16(ret);
 			action_bitmap |= (1 << FD_ACTION_QUEUE_BIT);
@@ -1760,7 +1739,7 @@ fd_flow_parse_action(struct rte_eth_dev *dev, const struct rte_flow_action *acti
 				rte_flow_error_set(error, EINVAL,
 						RTE_FLOW_ERROR_TYPE_ACTION, actions,
 						"multi drop action no support.");
-				goto free_flow;
+				return -rte_errno;
 			}
 			action_bitmap |= (1 << FD_ACTION_DROP_BIT);
 			break;
@@ -1772,7 +1751,7 @@ fd_flow_parse_action(struct rte_eth_dev *dev, const struct rte_flow_action *acti
 				rte_flow_error_set(error, EINVAL,
 						RTE_FLOW_ERROR_TYPE_ACTION, actions,
 						"multi drop action no support.");
-				goto free_flow;
+				return -rte_errno;
 			}
 			action_bitmap |= (1 << FD_ACTION_VXLAN_DECAP);
 			break;
@@ -1787,7 +1766,7 @@ fd_flow_parse_action(struct rte_eth_dev *dev, const struct rte_flow_action *acti
 				rte_flow_error_set(error, EINVAL,
 						RTE_FLOW_ERROR_TYPE_ACTION, actions,
 						"multi drop action no support.");
-				goto free_flow;
+				return -rte_errno;
 			}
 			action_bitmap |= (1 << FD_ACTION_VXLAN_ENCAP);
 			break;
@@ -1795,7 +1774,7 @@ fd_flow_parse_action(struct rte_eth_dev *dev, const struct rte_flow_action *acti
 			rte_flow_error_set(error, EINVAL,
 				RTE_FLOW_ERROR_TYPE_ACTION, actions,
 				"Invalid action.");
-			goto free_flow;
+			return -rte_errno;
 		}
 	}
 
@@ -1829,11 +1808,6 @@ fd_flow_parse_action(struct rte_eth_dev *dev, const struct rte_flow_action *acti
 
 	result->action_idx = action_bitmap;
 	return 0;
-
-free_flow:
-	if (!dh_flow)
-		rte_free(flow);
-	return -rte_errno;
 }
 
 static int
-- 
2.27.0

[-- Attachment #1.1.2: Type: text/html , Size: 23209 bytes --]

  parent reply	other threads:[~2026-11-28  4:18 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-28  4:04 [PATCH v1 0/2] net/zxdh: fix issues reported by GCC analyzer Junlong Wang
2026-11-28  4:04 ` [PATCH v1 1/2] net/zxdh: bound resource table reply length before copying Junlong Wang
2026-12-01  0:26   ` Stephen Hemminger
2026-11-28  4:04 ` Junlong Wang [this message]
2026-12-01  0:33 ` [PATCH v1 0/2] net/zxdh: fix issues reported by GCC analyzer Stephen Hemminger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261128040458.3967016-3-wang.junlong1@zte.com.cn \
    --to=wang.junlong1@zte.com.cn \
    --cc=dev@dpdk.org \
    --cc=stable@dpdk.org \
    --cc=stephen@networkplumber.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox