* [PATCH 27.03 1/6] eal: add macros for local variable-length structure buffers
2026-11-16 16:09 [PATCH 27.03 0/6] eliminate unnecessary dynamic allocation Bruce Richardson
@ 2026-11-16 16:09 ` Bruce Richardson
2026-11-16 16:09 ` [PATCH 27.03 2/6] net/iavf: use stack storage for virtchnl messages Bruce Richardson
` (4 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Bruce Richardson @ 2026-11-16 16:09 UTC (permalink / raw)
To: dev; +Cc: Bruce Richardson
Many drivers allocate small control-path messages, which consist of a
header structure followed by a variable-length array, using malloc and
free, which adds error handling and the risk of leaks on error paths.
Add RTE_DEFINE_FLEX and RTE_DEFINE_FLEX_BYTES macros to define such
buffers on the stack instead, (similar to equivalent macros in Linux
kernel). The buffer is a union of a byte array and the structure type,
giving correct alignment and type-safe access via a pointer, with the
byte array first so that zero-initialization covers the whole buffer.
Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
---
app/test/test_common.c | 54 ++++++++++++++++++++++++++++++++++++
lib/eal/include/rte_common.h | 46 ++++++++++++++++++++++++++++++
2 files changed, 100 insertions(+)
diff --git a/app/test/test_common.c b/app/test/test_common.c
index 290bf763ee..148d31aee5 100644
--- a/app/test/test_common.c
+++ b/app/test/test_common.c
@@ -796,6 +796,59 @@ test_fls(void)
return 0;
}
+struct flex_test_elem {
+ uint64_t a;
+ uint16_t b;
+};
+
+struct flex_test_hdr {
+ uint16_t num;
+ struct flex_test_elem elems[];
+};
+
+struct flex_test_hdr_one {
+ uint8_t num;
+ struct {
+ uint8_t len;
+ struct flex_test_elem elems[1];
+ } list;
+};
+
+static int
+test_define_flex(void)
+{
+ RTE_DEFINE_FLEX(struct flex_test_hdr, hdr, elems, 4);
+ RTE_DEFINE_FLEX(struct flex_test_hdr_one, hdr_one, list.elems, 3);
+ RTE_DEFINE_FLEX_BYTES(struct flex_test_hdr, hdr_bytes, 100);
+
+ RTE_TEST_ASSERT(sizeof(hdr_u) >= sizeof(*hdr) + 4 * sizeof(hdr->elems[0]),
+ "RTE_DEFINE_FLEX size, flexible array");
+ RTE_TEST_ASSERT(sizeof(hdr_one_u) >= sizeof(*hdr_one) + 3 * sizeof(hdr_one->list.elems[0]),
+ "RTE_DEFINE_FLEX size, nested one-element array");
+ RTE_TEST_ASSERT(sizeof(hdr_bytes_u) >= 100, "RTE_DEFINE_FLEX_BYTES size");
+
+ RTE_TEST_ASSERT((uintptr_t)hdr % alignof(struct flex_test_hdr) == 0,
+ "RTE_DEFINE_FLEX alignment");
+ RTE_TEST_ASSERT((uintptr_t)hdr_one % alignof(struct flex_test_hdr_one) == 0,
+ "RTE_DEFINE_FLEX alignment, nested array");
+ RTE_TEST_ASSERT((uintptr_t)hdr_bytes % alignof(struct flex_test_hdr) == 0,
+ "RTE_DEFINE_FLEX_BYTES alignment");
+
+ for (size_t i = 0; i < sizeof(hdr_u.raw); i++)
+ RTE_TEST_ASSERT(hdr_u.raw[i] == 0, "RTE_DEFINE_FLEX not zeroed");
+ for (size_t i = 0; i < sizeof(hdr_one_u.raw); i++)
+ RTE_TEST_ASSERT(hdr_one_u.raw[i] == 0, "RTE_DEFINE_FLEX not zeroed, nested array");
+ for (size_t i = 0; i < sizeof(hdr_bytes_u.raw); i++)
+ RTE_TEST_ASSERT(hdr_bytes_u.raw[i] == 0, "RTE_DEFINE_FLEX_BYTES not zeroed");
+
+ hdr->num = 4;
+ for (uint16_t i = 0; i < hdr->num; i++)
+ hdr->elems[i].b = i;
+ RTE_TEST_ASSERT_EQUAL(hdr->elems[3].b, 3, "RTE_DEFINE_FLEX element access");
+
+ return 0;
+}
+
static struct unit_test_suite common_test_suite = {
.suite_name = "common autotest",
.setup = NULL,
@@ -805,6 +858,7 @@ static struct unit_test_suite common_test_suite = {
TEST_CASE(test_ptr_align_edge_cases),
TEST_CASE(test_align),
TEST_CASE(test_macros),
+ TEST_CASE(test_define_flex),
TEST_CASE(test_misc),
TEST_CASE(test_bsf),
TEST_CASE(test_log2),
diff --git a/lib/eal/include/rte_common.h b/lib/eal/include/rte_common.h
index fed373f412..4f55b776f8 100644
--- a/lib/eal/include/rte_common.h
+++ b/lib/eal/include/rte_common.h
@@ -1114,6 +1114,52 @@ __extension__ typedef uint64_t RTE_MARKER64[0];
*/
#define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
+/**
+ * Define a zero-initialized, local buffer of a given size in bytes,
+ * accessed via a pointer to a structure type.
+ *
+ * This is intended for structures ending in a variable-length array,
+ * e.g. messages with a header followed by a list of elements,
+ * allowing them to be placed on the stack rather than dynamically allocated.
+ * The buffer is suitably aligned for the structure type.
+ *
+ * @param type
+ * The type of the structure at the start of the buffer.
+ * @param name
+ * The name of the pointer variable to define, of type "type * const".
+ * @param size
+ * The size of the buffer in bytes, a compile-time constant
+ * which must be no smaller than the size of the structure type.
+ */
+#define RTE_DEFINE_FLEX_BYTES(type, name, size) \
+ static_assert((size) >= sizeof(type), "buffer smaller than " RTE_STR(type)); \
+ union { \
+ uint8_t raw[size]; /* first member, so "{0}" zeroes the whole buffer */ \
+ type obj; \
+ } name ## _u = {0}; \
+ type * const name = &name ## _u.obj
+
+/**
+ * Define a zero-initialized, local buffer for a structure ending in an array,
+ * with space for a given number of array elements beyond the size of the structure.
+ *
+ * The buffer is sized as "sizeof(type) + count * sizeof(element)", so for structures
+ * whose array is declared with a single element, e.g. "elem[1]",
+ * "count" elements are available in addition to that one.
+ *
+ * @param type
+ * The type of the structure.
+ * @param name
+ * The name of the pointer variable to define, of type "type * const".
+ * @param member
+ * The array member at the end of the structure.
+ * @param count
+ * The number of extra array elements to allow for, a compile-time constant.
+ */
+#define RTE_DEFINE_FLEX(type, name, member, count) \
+ RTE_DEFINE_FLEX_BYTES(type, name, \
+ sizeof(type) + RTE_SIZEOF_FIELD(type, member[0]) * (count))
+
#define _RTE_STR(x) #x
/** Take a macro value and get a string version of it */
#define RTE_STR(x) _RTE_STR(x)
--
2.55.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH 27.03 2/6] net/iavf: use stack storage for virtchnl messages
2026-11-16 16:09 [PATCH 27.03 0/6] eliminate unnecessary dynamic allocation Bruce Richardson
2026-11-16 16:09 ` [PATCH 27.03 1/6] eal: add macros for local variable-length structure buffers Bruce Richardson
@ 2026-11-16 16:09 ` Bruce Richardson
2026-11-16 16:09 ` [PATCH 27.03 3/6] net/ice: use stack storage for function-local buffers Bruce Richardson
` (3 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Bruce Richardson @ 2026-11-16 16:09 UTC (permalink / raw)
To: dev; +Cc: Bruce Richardson
Rather than using malloc and free for the request and response buffers
of IPsec and traffic management virtchnl messages, define them on the
stack using RTE_DEFINE_FLEX. This removes the allocation failure and
cleanup paths, avoiding potential leaks on error.
For the traffic management messages, add checks that the number of TCs
and queue pairs do not exceed the maximum sizes of the buffers.
Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
---
drivers/net/intel/iavf/iavf_ipsec_crypto.c | 270 ++++++---------------
drivers/net/intel/iavf/iavf_tm.c | 26 +-
2 files changed, 79 insertions(+), 217 deletions(-)
diff --git a/drivers/net/intel/iavf/iavf_ipsec_crypto.c b/drivers/net/intel/iavf/iavf_ipsec_crypto.c
index be0a0d0df6..d5a6a48092 100644
--- a/drivers/net/intel/iavf/iavf_ipsec_crypto.c
+++ b/drivers/net/intel/iavf/iavf_ipsec_crypto.c
@@ -460,28 +460,14 @@ static uint32_t
iavf_ipsec_crypto_security_association_add(struct iavf_adapter *adapter,
struct rte_security_session_conf *conf)
{
- struct inline_ipsec_msg *request = NULL, *response = NULL;
- struct virtchnl_ipsec_sa_cfg *sa_cfg;
- size_t request_len, response_len;
-
- int rc;
-
- request_len = sizeof(struct inline_ipsec_msg) +
+ RTE_DEFINE_FLEX(struct inline_ipsec_msg, request, ipsec_data.sa_cfg, 1);
+ RTE_DEFINE_FLEX(struct inline_ipsec_msg, response, ipsec_data.sa_cfg_resp, 1);
+ const size_t request_len = sizeof(struct inline_ipsec_msg) +
sizeof(struct virtchnl_ipsec_sa_cfg);
-
- request = calloc(1, request_len);
- if (request == NULL) {
- rc = -ENOMEM;
- goto update_cleanup;
- }
-
- response_len = sizeof(struct inline_ipsec_msg) +
+ const size_t response_len = sizeof(struct inline_ipsec_msg) +
sizeof(struct virtchnl_ipsec_sa_cfg_resp);
- response = calloc(1, response_len);
- if (response == NULL) {
- rc = -ENOMEM;
- goto update_cleanup;
- }
+ struct virtchnl_ipsec_sa_cfg *sa_cfg;
+ int rc;
/* set msg header params */
request->ipsec_opcode = INLINE_IPSEC_OP_SA_CREATE;
@@ -546,19 +532,14 @@ iavf_ipsec_crypto_security_association_add(struct iavf_adapter *adapter,
(uint8_t *)request, request_len,
(uint8_t *)response, response_len);
if (rc)
- goto update_cleanup;
+ return rc;
/* verify response id */
if (response->ipsec_opcode != request->ipsec_opcode ||
response->req_id != request->req_id)
- rc = -EFAULT;
- else
- rc = response->ipsec_data.sa_cfg_resp->sa_handle;
-update_cleanup:
- free(response);
- free(request);
+ return -EFAULT;
- return rc;
+ return response->ipsec_data.sa_cfg_resp->sa_handle;
}
static void
@@ -722,17 +703,13 @@ iavf_ipsec_crypto_inbound_security_policy_add(struct iavf_adapter *adapter,
bool is_udp,
uint16_t udp_port)
{
- struct inline_ipsec_msg *request = NULL, *response = NULL;
- size_t request_len, response_len;
- int rc = 0;
-
- request_len = sizeof(struct inline_ipsec_msg) +
+ RTE_DEFINE_FLEX(struct inline_ipsec_msg, request, ipsec_data.sp_cfg, 1);
+ RTE_DEFINE_FLEX(struct inline_ipsec_msg, response, ipsec_data.sp_cfg_resp, 1);
+ const size_t request_len = sizeof(struct inline_ipsec_msg) +
sizeof(struct virtchnl_ipsec_sp_cfg);
- request = calloc(1, request_len);
- if (request == NULL) {
- rc = -ENOMEM;
- goto update_cleanup;
- }
+ const size_t response_len = sizeof(struct inline_ipsec_msg) +
+ sizeof(struct virtchnl_ipsec_sp_cfg_resp);
+ int rc = 0;
/* set msg header params */
request->ipsec_opcode = INLINE_IPSEC_OP_SP_CREATE;
@@ -767,58 +744,32 @@ iavf_ipsec_crypto_inbound_security_policy_add(struct iavf_adapter *adapter,
request->ipsec_data.sp_cfg->is_udp = is_udp;
request->ipsec_data.sp_cfg->udp_port = htons(udp_port);
- response_len = sizeof(struct inline_ipsec_msg) +
- sizeof(struct virtchnl_ipsec_sp_cfg_resp);
- response = calloc(1, response_len);
- if (response == NULL) {
- rc = -ENOMEM;
- goto update_cleanup;
- }
-
/* send virtual channel request to add SA to hardware database */
rc = iavf_ipsec_crypto_request(adapter,
(uint8_t *)request, request_len,
(uint8_t *)response, response_len);
if (rc)
- goto update_cleanup;
+ return rc;
/* verify response */
if (response->ipsec_opcode != request->ipsec_opcode ||
response->req_id != request->req_id)
- rc = -EFAULT;
- else
- rc = response->ipsec_data.sp_cfg_resp->rule_id;
-
-update_cleanup:
- free(request);
- free(response);
+ return -EFAULT;
- return rc;
+ return response->ipsec_data.sp_cfg_resp->rule_id;
}
static uint32_t
iavf_ipsec_crypto_sa_update_esn(struct iavf_adapter *adapter,
struct iavf_security_session *sess)
{
- struct inline_ipsec_msg *request = NULL, *response = NULL;
- size_t request_len, response_len;
- int rc = 0;
-
- request_len = sizeof(struct inline_ipsec_msg) +
+ RTE_DEFINE_FLEX(struct inline_ipsec_msg, request, ipsec_data.sa_update, 1);
+ RTE_DEFINE_FLEX(struct inline_ipsec_msg, response, ipsec_data.ipsec_resp, 1);
+ const size_t request_len = sizeof(struct inline_ipsec_msg) +
sizeof(struct virtchnl_ipsec_sa_update);
- request = calloc(1, request_len);
- if (request == NULL) {
- rc = -ENOMEM;
- goto update_cleanup;
- }
-
- response_len = sizeof(struct inline_ipsec_msg) +
+ const size_t response_len = sizeof(struct inline_ipsec_msg) +
sizeof(struct virtchnl_ipsec_resp);
- response = calloc(1, response_len);
- if (response == NULL) {
- rc = -ENOMEM;
- goto update_cleanup;
- }
+ int rc = 0;
/* set msg header params */
request->ipsec_opcode = INLINE_IPSEC_OP_SA_UPDATE;
@@ -834,20 +785,14 @@ iavf_ipsec_crypto_sa_update_esn(struct iavf_adapter *adapter,
(uint8_t *)request, request_len,
(uint8_t *)response, response_len);
if (rc)
- goto update_cleanup;
+ return rc;
/* verify response */
if (response->ipsec_opcode != request->ipsec_opcode ||
response->req_id != request->req_id)
- rc = -EFAULT;
- else
- rc = response->ipsec_data.ipsec_resp->resp;
-
-update_cleanup:
- free(request);
- free(response);
+ return -EFAULT;
- return rc;
+ return response->ipsec_data.ipsec_resp->resp;
}
static int
@@ -897,25 +842,13 @@ int
iavf_ipsec_crypto_security_policy_delete(struct iavf_adapter *adapter,
uint8_t is_v4, uint32_t flow_id)
{
- struct inline_ipsec_msg *request = NULL, *response = NULL;
- size_t request_len, response_len;
- int rc = 0;
-
- request_len = sizeof(struct inline_ipsec_msg) +
+ RTE_DEFINE_FLEX(struct inline_ipsec_msg, request, ipsec_data.sp_destroy, 1);
+ RTE_DEFINE_FLEX(struct inline_ipsec_msg, response, ipsec_data.ipsec_resp, 1);
+ const size_t request_len = sizeof(struct inline_ipsec_msg) +
sizeof(struct virtchnl_ipsec_sp_destroy);
- request = calloc(1, request_len);
- if (request == NULL) {
- rc = -ENOMEM;
- goto update_cleanup;
- }
-
- response_len = sizeof(struct inline_ipsec_msg) +
+ const size_t response_len = sizeof(struct inline_ipsec_msg) +
sizeof(struct virtchnl_ipsec_resp);
- response = calloc(1, response_len);
- if (response == NULL) {
- rc = -ENOMEM;
- goto update_cleanup;
- }
+ int rc = 0;
/* set msg header params */
request->ipsec_opcode = INLINE_IPSEC_OP_SP_DESTROY;
@@ -932,48 +865,27 @@ iavf_ipsec_crypto_security_policy_delete(struct iavf_adapter *adapter,
(uint8_t *)request, request_len,
(uint8_t *)response, response_len);
if (rc)
- goto update_cleanup;
+ return rc;
/* verify response */
if (response->ipsec_opcode != request->ipsec_opcode ||
response->req_id != request->req_id)
- rc = -EFAULT;
- else
- rc = response->ipsec_data.ipsec_status->status;
-
-update_cleanup:
- free(request);
- free(response);
+ return -EFAULT;
- return rc;
+ return response->ipsec_data.ipsec_status->status;
}
static uint32_t
iavf_ipsec_crypto_sa_del(struct iavf_adapter *adapter,
struct iavf_security_session *sess)
{
- struct inline_ipsec_msg *request = NULL, *response = NULL;
- size_t request_len, response_len;
-
- int rc = 0;
-
- request_len = sizeof(struct inline_ipsec_msg) +
+ RTE_DEFINE_FLEX(struct inline_ipsec_msg, request, ipsec_data.sa_destroy, 1);
+ RTE_DEFINE_FLEX(struct inline_ipsec_msg, response, ipsec_data.ipsec_resp, 1);
+ const size_t request_len = sizeof(struct inline_ipsec_msg) +
sizeof(struct virtchnl_ipsec_sa_destroy);
-
- request = calloc(1, request_len);
- if (request == NULL) {
- rc = -ENOMEM;
- goto update_cleanup;
- }
-
- response_len = sizeof(struct inline_ipsec_msg) +
+ const size_t response_len = sizeof(struct inline_ipsec_msg) +
sizeof(struct virtchnl_ipsec_resp);
-
- response = calloc(1, response_len);
- if (response == NULL) {
- rc = -ENOMEM;
- goto update_cleanup;
- }
+ int rc = 0;
/* set msg header params */
request->ipsec_opcode = INLINE_IPSEC_OP_SA_DESTROY;
@@ -995,12 +907,12 @@ iavf_ipsec_crypto_sa_del(struct iavf_adapter *adapter,
(uint8_t *)request, request_len,
(uint8_t *)response, response_len);
if (rc)
- goto update_cleanup;
+ return rc;
/* verify response */
if (response->ipsec_opcode != request->ipsec_opcode ||
response->req_id != request->req_id)
- rc = -EFAULT;
+ return -EFAULT;
/**
* Delete status will be the same bitmask as sa_destroy request flag if
@@ -1008,13 +920,9 @@ iavf_ipsec_crypto_sa_del(struct iavf_adapter *adapter,
*/
if (request->ipsec_data.sa_destroy->flag !=
response->ipsec_data.ipsec_status->status)
- rc = -EFAULT;
-
-update_cleanup:
- free(response);
- free(request);
+ return -EFAULT;
- return rc;
+ return 0;
}
static int
@@ -1160,50 +1068,30 @@ iavf_ipsec_crypto_device_capabilities_get(struct iavf_adapter *adapter,
struct virtchnl_ipsec_cap *capability)
{
/* Perform pf-vf comms */
- struct inline_ipsec_msg *request = NULL, *response = NULL;
- size_t request_len, response_len;
- int rc;
-
- request_len = sizeof(struct inline_ipsec_msg);
-
- request = calloc(1, request_len);
- if (request == NULL) {
- rc = -ENOMEM;
- goto update_cleanup;
- }
-
- response_len = sizeof(struct inline_ipsec_msg) +
+ struct inline_ipsec_msg request = {0};
+ RTE_DEFINE_FLEX(struct inline_ipsec_msg, response, ipsec_data.ipsec_cap, 1);
+ const size_t response_len = sizeof(struct inline_ipsec_msg) +
sizeof(struct virtchnl_ipsec_cap);
- response = calloc(1, response_len);
- if (response == NULL) {
- rc = -ENOMEM;
- goto update_cleanup;
- }
+ int rc;
/* set msg header params */
- request->ipsec_opcode = INLINE_IPSEC_OP_GET_CAP;
- request->req_id = (uint16_t)0xDEADBEEF;
+ request.ipsec_opcode = INLINE_IPSEC_OP_GET_CAP;
+ request.req_id = (uint16_t)0xDEADBEEF;
/* send virtual channel request to add SA to hardware database */
rc = iavf_ipsec_crypto_request(adapter,
- (uint8_t *)request, request_len,
+ (uint8_t *)&request, sizeof(request),
(uint8_t *)response, response_len);
if (rc)
- goto update_cleanup;
+ return rc;
/* verify response id */
- if (response->ipsec_opcode != request->ipsec_opcode ||
- response->req_id != request->req_id){
- rc = -EFAULT;
- goto update_cleanup;
- }
- memcpy(capability, response->ipsec_data.ipsec_cap, sizeof(*capability));
-
-update_cleanup:
- free(response);
- free(request);
+ if (response->ipsec_opcode != request.ipsec_opcode ||
+ response->req_id != request.req_id)
+ return -EFAULT;
- return rc;
+ memcpy(capability, response->ipsec_data.ipsec_cap, sizeof(*capability));
+ return 0;
}
enum rte_crypto_auth_algorithm auth_maptbl[] = {
@@ -1588,50 +1476,30 @@ iavf_ipsec_crypto_status_get(struct iavf_adapter *adapter,
struct virtchnl_ipsec_status *status)
{
/* Perform pf-vf comms */
- struct inline_ipsec_msg *request = NULL, *response = NULL;
- size_t request_len, response_len;
- int rc;
-
- request_len = sizeof(struct inline_ipsec_msg);
-
- request = calloc(1, request_len);
- if (request == NULL) {
- rc = -ENOMEM;
- goto update_cleanup;
- }
-
- response_len = sizeof(struct inline_ipsec_msg) +
+ struct inline_ipsec_msg request = {0};
+ RTE_DEFINE_FLEX(struct inline_ipsec_msg, response, ipsec_data.ipsec_status, 1);
+ const size_t response_len = sizeof(struct inline_ipsec_msg) +
sizeof(struct virtchnl_ipsec_status);
- response = calloc(1, response_len);
- if (response == NULL) {
- rc = -ENOMEM;
- goto update_cleanup;
- }
+ int rc;
/* set msg header params */
- request->ipsec_opcode = INLINE_IPSEC_OP_GET_STATUS;
- request->req_id = (uint16_t)0xDEADBEEF;
+ request.ipsec_opcode = INLINE_IPSEC_OP_GET_STATUS;
+ request.req_id = (uint16_t)0xDEADBEEF;
/* send virtual channel request to add SA to hardware database */
rc = iavf_ipsec_crypto_request(adapter,
- (uint8_t *)request, request_len,
+ (uint8_t *)&request, sizeof(request),
(uint8_t *)response, response_len);
if (rc)
- goto update_cleanup;
+ return rc;
/* verify response id */
- if (response->ipsec_opcode != request->ipsec_opcode ||
- response->req_id != request->req_id){
- rc = -EFAULT;
- goto update_cleanup;
- }
- memcpy(status, response->ipsec_data.ipsec_status, sizeof(*status));
-
-update_cleanup:
- free(response);
- free(request);
+ if (response->ipsec_opcode != request.ipsec_opcode ||
+ response->req_id != request.req_id)
+ return -EFAULT;
- return rc;
+ memcpy(status, response->ipsec_data.ipsec_status, sizeof(*status));
+ return 0;
}
diff --git a/drivers/net/intel/iavf/iavf_tm.c b/drivers/net/intel/iavf/iavf_tm.c
index 07ba58f6f5..c7b904e9d7 100644
--- a/drivers/net/intel/iavf/iavf_tm.c
+++ b/drivers/net/intel/iavf/iavf_tm.c
@@ -833,8 +833,9 @@ static int iavf_hierarchy_commit(struct rte_eth_dev *dev,
struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
struct iavf_adapter *adapter =
IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
- struct virtchnl_queue_tc_mapping *q_tc_mapping = NULL;
- struct virtchnl_queues_bw_cfg *q_bw = NULL;
+ RTE_DEFINE_FLEX(struct virtchnl_queue_tc_mapping, q_tc_mapping, tc,
+ IAVF_MAX_TRAFFIC_CLASS - 1);
+ RTE_DEFINE_FLEX(struct virtchnl_queues_bw_cfg, q_bw, cfg, IAVF_MAX_NUM_QUEUES_LV - 1);
struct iavf_tm_node_list *queue_list = &vf->tm_conf.queue_list;
struct iavf_tm_node *tm_node;
struct iavf_qtc_map *qtc_map = NULL;
@@ -869,21 +870,18 @@ static int iavf_hierarchy_commit(struct rte_eth_dev *dev,
goto fail_clear;
}
- size = sizeof(*q_tc_mapping) + sizeof(q_tc_mapping->tc[0]) *
- (vf->qos_cap->num_elem - 1);
- q_tc_mapping = calloc(1, size);
- if (!q_tc_mapping) {
- ret_val = IAVF_ERR_NO_MEMORY;
+ if (vf->qos_cap->num_elem > IAVF_MAX_TRAFFIC_CLASS ||
+ vf->num_queue_pairs > IAVF_MAX_NUM_QUEUES_LV) {
+ PMD_DRV_LOG(ERR, "Too many TCs (%u) or queues (%u) for TM configuration",
+ vf->qos_cap->num_elem, vf->num_queue_pairs);
+ ret_val = IAVF_ERR_PARAM;
goto fail_clear;
}
+ size = sizeof(*q_tc_mapping) + sizeof(q_tc_mapping->tc[0]) *
+ (vf->qos_cap->num_elem - 1);
size_q = sizeof(*q_bw) + sizeof(q_bw->cfg[0]) *
(vf->num_queue_pairs - 1);
- q_bw = calloc(1, size_q);
- if (!q_bw) {
- ret_val = IAVF_ERR_NO_MEMORY;
- goto fail_clear;
- }
q_tc_mapping->vsi_id = vf->vsi.vsi_id;
q_tc_mapping->num_tc = vf->qos_cap->num_elem;
@@ -950,8 +948,6 @@ static int iavf_hierarchy_commit(struct rte_eth_dev *dev,
free(old_qtc_map);
if (adapter->stopped == 1)
vf->tm_conf.committed = true;
- free(q_bw);
- free(q_tc_mapping);
return ret_val;
fail_clear:
@@ -961,8 +957,6 @@ static int iavf_hierarchy_commit(struct rte_eth_dev *dev,
iavf_tm_conf_init(dev);
}
err:
- free(q_bw);
- free(q_tc_mapping);
free(qtc_map);
return ret_val;
}
--
2.55.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH 27.03 3/6] net/ice: use stack storage for function-local buffers
2026-11-16 16:09 [PATCH 27.03 0/6] eliminate unnecessary dynamic allocation Bruce Richardson
2026-11-16 16:09 ` [PATCH 27.03 1/6] eal: add macros for local variable-length structure buffers Bruce Richardson
2026-11-16 16:09 ` [PATCH 27.03 2/6] net/iavf: use stack storage for virtchnl messages Bruce Richardson
@ 2026-11-16 16:09 ` Bruce Richardson
2026-11-16 16:09 ` [PATCH 27.03 4/6] net/idpf: use stack storage for virtchnl messages Bruce Richardson
` (2 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Bruce Richardson @ 2026-11-16 16:09 UTC (permalink / raw)
To: dev; +Cc: Bruce Richardson
Rather than using malloc and free for small or medium-sized buffers used
only within a single function, such as adminq and virtchnl messages,
define them on the stack, using RTE_DEFINE_FLEX for those structures
ending in a variable-length array. This removes the allocation failure
and cleanup paths, avoiding potential leaks on error.
Where the number of elements used depends on runtime values, add checks
that those values do not exceed the buffer sizes.
Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
---
drivers/net/intel/ice/ice_dcf.c | 21 ++---
drivers/net/intel/ice/ice_dcf_parent.c | 12 +--
drivers/net/intel/ice/ice_dcf_sched.c | 25 ++----
drivers/net/intel/ice/ice_diagnose.c | 12 +--
drivers/net/intel/ice/ice_ethdev.c | 104 +++++++------------------
drivers/net/intel/ice/ice_rxtx.c | 26 +------
drivers/net/intel/ice/ice_tm.c | 7 +-
7 files changed, 52 insertions(+), 155 deletions(-)
diff --git a/drivers/net/intel/ice/ice_dcf.c b/drivers/net/intel/ice/ice_dcf.c
index 12eb58dc2a..3e9654244d 100644
--- a/drivers/net/intel/ice/ice_dcf.c
+++ b/drivers/net/intel/ice/ice_dcf.c
@@ -1275,18 +1275,19 @@ ice_dcf_configure_queues(struct ice_dcf_hw *hw)
int
ice_dcf_config_irq_map(struct ice_dcf_hw *hw)
{
- struct virtchnl_irq_map_info *map_info;
+ RTE_DEFINE_FLEX(struct virtchnl_irq_map_info, map_info, vecmap, RTE_DIM(hw->rxq_map));
struct virtchnl_vector_map *vecmap;
struct dcf_virtchnl_cmd args;
int len, i, err;
+ if (hw->nb_msix > RTE_DIM(hw->rxq_map)) {
+ PMD_DRV_LOG(ERR, "Too many interrupt vectors: %u", hw->nb_msix);
+ return -EINVAL;
+ }
+
len = sizeof(struct virtchnl_irq_map_info) +
sizeof(struct virtchnl_vector_map) * hw->nb_msix;
- map_info = rte_zmalloc("map_info", len, 0);
- if (!map_info)
- return -ENOMEM;
-
map_info->num_vectors = hw->nb_msix;
for (i = 0; i < hw->nb_msix; i++) {
vecmap = &map_info->vecmap[i];
@@ -1306,7 +1307,6 @@ ice_dcf_config_irq_map(struct ice_dcf_hw *hw)
if (err)
PMD_DRV_LOG(ERR, "fail to execute command OP_CONFIG_IRQ_MAP");
- rte_free(map_info);
return err;
}
@@ -1402,7 +1402,7 @@ ice_dcf_add_del_all_mac_addr(struct ice_dcf_hw *hw,
struct rte_ether_addr *addr,
bool add, uint8_t type)
{
- struct virtchnl_ether_addr_list *list;
+ RTE_DEFINE_FLEX(struct virtchnl_ether_addr_list, list, list, 1);
struct dcf_virtchnl_cmd args;
int len, err = 0;
@@ -1417,12 +1417,6 @@ ice_dcf_add_del_all_mac_addr(struct ice_dcf_hw *hw,
len = sizeof(struct virtchnl_ether_addr_list);
len += sizeof(struct virtchnl_ether_addr);
- list = rte_zmalloc(NULL, len, 0);
- if (!list) {
- PMD_DRV_LOG(ERR, "fail to allocate memory");
- return -ENOMEM;
- }
-
memcpy(list->list[0].addr, addr->addr_bytes,
sizeof(addr->addr_bytes));
@@ -1442,6 +1436,5 @@ ice_dcf_add_del_all_mac_addr(struct ice_dcf_hw *hw,
PMD_DRV_LOG(ERR, "fail to execute command %s",
add ? "OP_ADD_ETHER_ADDRESS" :
"OP_DEL_ETHER_ADDRESS");
- rte_free(list);
return err;
}
diff --git a/drivers/net/intel/ice/ice_dcf_parent.c b/drivers/net/intel/ice/ice_dcf_parent.c
index 4d620fe8f5..3dfdb5d9ce 100644
--- a/drivers/net/intel/ice/ice_dcf_parent.c
+++ b/drivers/net/intel/ice/ice_dcf_parent.c
@@ -321,7 +321,7 @@ ice_dcf_query_port_ets(struct ice_hw *parent_hw, struct ice_dcf_hw *real_hw)
static int
ice_dcf_init_parent_hw(struct ice_hw *hw)
{
- struct ice_aqc_get_phy_caps_data *pcaps;
+ struct ice_aqc_get_phy_caps_data pcaps = {0};
int status;
status = ice_aq_get_fw_ver(hw, NULL);
@@ -345,17 +345,9 @@ ice_dcf_init_parent_hw(struct ice_hw *hw)
if (status)
goto err_unroll_alloc;
- pcaps = (struct ice_aqc_get_phy_caps_data *)
- ice_malloc(hw, sizeof(*pcaps));
- if (!pcaps) {
- status = ICE_ERR_NO_MEMORY;
- goto err_unroll_alloc;
- }
-
/* Initialize port_info struct with PHY capabilities */
status = ice_aq_get_phy_caps(hw->port_info, false,
- ICE_AQC_REPORT_TOPO_CAP_MEDIA, pcaps, NULL);
- ice_free(hw, pcaps);
+ ICE_AQC_REPORT_TOPO_CAP_MEDIA, &pcaps, NULL);
if (status)
goto err_unroll_alloc;
diff --git a/drivers/net/intel/ice/ice_dcf_sched.c b/drivers/net/intel/ice/ice_dcf_sched.c
index a524d9c777..75752f4071 100644
--- a/drivers/net/intel/ice/ice_dcf_sched.c
+++ b/drivers/net/intel/ice/ice_dcf_sched.c
@@ -723,8 +723,8 @@ static int ice_dcf_hierarchy_commit(struct rte_eth_dev *dev,
{
struct ice_dcf_adapter *adapter = dev->data->dev_private;
struct ice_dcf_hw *hw = &adapter->real_hw;
- struct virtchnl_dcf_bw_cfg_list *vf_bw = NULL;
- struct virtchnl_dcf_bw_cfg_list *tc_bw = NULL;
+ RTE_DEFINE_FLEX(struct virtchnl_dcf_bw_cfg_list, vf_bw, cfg, ICE_MAX_TRAFFIC_CLASS - 1);
+ RTE_DEFINE_FLEX(struct virtchnl_dcf_bw_cfg_list, tc_bw, cfg, ICE_MAX_TRAFFIC_CLASS - 1);
struct virtchnl_dcf_bw_cfg_list *bw_cfg;
struct ice_dcf_tm_node_list *vsi_list = &hw->tm_conf.vsi_list;
struct rte_tm_shaper_params *profile;
@@ -745,19 +745,15 @@ static int ice_dcf_hierarchy_commit(struct rte_eth_dev *dev,
if (ret_val)
goto fail_clear;
+ if (hw->tm_conf.nb_tc_node > ICE_MAX_TRAFFIC_CLASS) {
+ PMD_DRV_LOG(ERR, "Too many TC nodes: %u", hw->tm_conf.nb_tc_node);
+ ret_val = ICE_ERR_PARAM;
+ goto fail_clear;
+ }
+
size = sizeof(struct virtchnl_dcf_bw_cfg_list) +
sizeof(struct virtchnl_dcf_bw_cfg) *
(hw->tm_conf.nb_tc_node - 1);
- vf_bw = calloc(1, size);
- if (!vf_bw) {
- ret_val = ICE_ERR_NO_MEMORY;
- goto fail_clear;
- }
- tc_bw = calloc(1, size);
- if (!tc_bw) {
- ret_val = ICE_ERR_NO_MEMORY;
- goto fail_clear;
- }
/* port bandwidth (Kbps) */
port_bw = hw->link_speed * 1000;
@@ -850,14 +846,9 @@ static int ice_dcf_hierarchy_commit(struct rte_eth_dev *dev,
ice_dcf_replace_qos_bw_cfg(hw, ICE_DCF_VFID, bw_cfg);
hw->tm_conf.committed = true;
- free(vf_bw);
- free(tc_bw);
return ret_val;
fail_clear:
- free(vf_bw);
- free(tc_bw);
-
/* clear all the traffic manager configuration */
if (clear_on_fail) {
ice_dcf_tm_conf_uninit(dev);
diff --git a/drivers/net/intel/ice/ice_diagnose.c b/drivers/net/intel/ice/ice_diagnose.c
index ecfcdf5559..320b42c5eb 100644
--- a/drivers/net/intel/ice/ice_diagnose.c
+++ b/drivers/net/intel/ice/ice_diagnose.c
@@ -453,13 +453,9 @@ ice_dump_switch(struct rte_eth_dev *dev, uint8_t **buff2, uint32_t *size)
for (i = 0; i < ICE_BLK_MAX_COUNT; i++) {
int res;
uint16_t buff_size;
- uint8_t *buff;
+ uint8_t buff[ICE_PKG_BUF_SIZE];
uint32_t offset = 0;
- buff = malloc(ICE_PKG_BUF_SIZE);
- if (!buff)
- return ICE_ERR_NO_MEMORY;
-
if (tbl_idx == 0) {
char tbl_idx_str[TBL_IDX_STR_SIZE];
memset(tbl_idx_str, 0, sizeof(tbl_idx_str));
@@ -475,16 +471,12 @@ ice_dump_switch(struct rte_eth_dev *dev, uint8_t **buff2, uint32_t *size)
ICE_PKG_BUF_SIZE,
&buff_size, &tbl_id, &tbl_idx, NULL, cd);
- if (res) {
- free(buff);
+ if (res)
return res;
- }
offset = covert_byte_to_hex(&buffer, buff, buff_size);
buffer += offset;
- free(buff);
-
if (tbl_idx == 0xffff) {
tbl_idx = 0;
memset(buffer, '\n', sizeof(char));
diff --git a/drivers/net/intel/ice/ice_ethdev.c b/drivers/net/intel/ice/ice_ethdev.c
index 457c955b2e..7835f6a905 100644
--- a/drivers/net/intel/ice/ice_ethdev.c
+++ b/drivers/net/intel/ice/ice_ethdev.c
@@ -1134,7 +1134,8 @@ ice_find_mac_filter(struct ice_vsi *vsi, struct rte_ether_addr *macaddr)
static int
ice_add_mac_filter(struct ice_vsi *vsi, struct rte_ether_addr *mac_addr)
{
- struct ice_fltr_list_entry *m_list_itr = NULL;
+ struct ice_fltr_list_entry m_list_entry = {0};
+ struct ice_fltr_list_entry *m_list_itr = &m_list_entry;
struct ice_mac_filter *f;
struct LIST_HEAD_TYPE list_head;
struct ice_hw *hw = ICE_VSI_TO_HW(vsi);
@@ -1155,12 +1156,6 @@ ice_add_mac_filter(struct ice_vsi *vsi, struct rte_ether_addr *mac_addr)
INIT_LIST_HEAD(&list_head);
- m_list_itr = (struct ice_fltr_list_entry *)
- ice_malloc(hw, sizeof(*m_list_itr));
- if (!m_list_itr) {
- ret = -ENOMEM;
- goto DONE;
- }
ice_memcpy(m_list_itr->fltr_info.l_data.mac.mac_addr,
mac_addr, ETH_ALEN, ICE_NONDMA_TO_NONDMA);
m_list_itr->fltr_info.src_id = ICE_SRC_ID_VSI;
@@ -1175,31 +1170,26 @@ ice_add_mac_filter(struct ice_vsi *vsi, struct rte_ether_addr *mac_addr)
ret = ice_add_mac(hw, &list_head);
if (ret != ICE_SUCCESS) {
PMD_DRV_LOG(ERR, "Failed to add MAC filter");
- ret = -EINVAL;
- goto DONE;
+ return -EINVAL;
}
/* Add the mac addr into mac list */
f = rte_zmalloc(NULL, sizeof(*f), 0);
if (!f) {
PMD_DRV_LOG(ERR, "failed to allocate memory");
- ret = -ENOMEM;
- goto DONE;
+ return -ENOMEM;
}
rte_ether_addr_copy(mac_addr, &f->mac_info.mac_addr);
TAILQ_INSERT_TAIL(&vsi->mac_list, f, next);
vsi->mac_num++;
- ret = 0;
-
-DONE:
- ice_free(hw, m_list_itr);
- return ret;
+ return 0;
}
static int
ice_remove_mac_filter(struct ice_vsi *vsi, struct rte_ether_addr *mac_addr)
{
- struct ice_fltr_list_entry *m_list_itr = NULL;
+ struct ice_fltr_list_entry m_list_entry = {0};
+ struct ice_fltr_list_entry *m_list_itr = &m_list_entry;
struct ice_mac_filter *f;
struct LIST_HEAD_TYPE list_head;
struct ice_hw *hw = ICE_VSI_TO_HW(vsi);
@@ -1212,12 +1202,6 @@ ice_remove_mac_filter(struct ice_vsi *vsi, struct rte_ether_addr *mac_addr)
INIT_LIST_HEAD(&list_head);
- m_list_itr = (struct ice_fltr_list_entry *)
- ice_malloc(hw, sizeof(*m_list_itr));
- if (!m_list_itr) {
- ret = -ENOMEM;
- goto DONE;
- }
ice_memcpy(m_list_itr->fltr_info.l_data.mac.mac_addr,
mac_addr, ETH_ALEN, ICE_NONDMA_TO_NONDMA);
m_list_itr->fltr_info.src_id = ICE_SRC_ID_VSI;
@@ -1232,8 +1216,7 @@ ice_remove_mac_filter(struct ice_vsi *vsi, struct rte_ether_addr *mac_addr)
ret = ice_remove_mac(hw, &list_head);
if (ret != ICE_SUCCESS) {
PMD_DRV_LOG(ERR, "Failed to remove MAC filter");
- ret = -EINVAL;
- goto DONE;
+ return -EINVAL;
}
/* Remove the mac addr from mac list */
@@ -1241,10 +1224,7 @@ ice_remove_mac_filter(struct ice_vsi *vsi, struct rte_ether_addr *mac_addr)
rte_free(f);
vsi->mac_num--;
- ret = 0;
-DONE:
- ice_free(hw, m_list_itr);
- return ret;
+ return 0;
}
/* Find out specific VLAN filter */
@@ -1265,7 +1245,8 @@ ice_find_vlan_filter(struct ice_vsi *vsi, struct ice_vlan *vlan)
static int
ice_add_vlan_filter(struct ice_vsi *vsi, struct ice_vlan *vlan)
{
- struct ice_fltr_list_entry *v_list_itr = NULL;
+ struct ice_fltr_list_entry v_list_entry = {0};
+ struct ice_fltr_list_entry *v_list_itr = &v_list_entry;
struct ice_vlan_filter *f;
struct LIST_HEAD_TYPE list_head;
struct ice_hw *hw;
@@ -1288,12 +1269,6 @@ ice_add_vlan_filter(struct ice_vsi *vsi, struct ice_vlan *vlan)
INIT_LIST_HEAD(&list_head);
- v_list_itr = (struct ice_fltr_list_entry *)
- ice_malloc(hw, sizeof(*v_list_itr));
- if (!v_list_itr) {
- ret = -ENOMEM;
- goto DONE;
- }
v_list_itr->fltr_info.l_data.vlan.vlan_id = vlan->vid;
v_list_itr->fltr_info.l_data.vlan.tpid = vlan->tpid;
v_list_itr->fltr_info.l_data.vlan.tpid_valid = true;
@@ -1309,33 +1284,28 @@ ice_add_vlan_filter(struct ice_vsi *vsi, struct ice_vlan *vlan)
ret = ice_add_vlan(hw, &list_head);
if (ret != ICE_SUCCESS) {
PMD_DRV_LOG(ERR, "Failed to add VLAN filter");
- ret = -EINVAL;
- goto DONE;
+ return -EINVAL;
}
/* Add vlan into vlan list */
f = rte_zmalloc(NULL, sizeof(*f), 0);
if (!f) {
PMD_DRV_LOG(ERR, "failed to allocate memory");
- ret = -ENOMEM;
- goto DONE;
+ return -ENOMEM;
}
f->vlan_info.vlan.tpid = vlan->tpid;
f->vlan_info.vlan.vid = vlan->vid;
TAILQ_INSERT_TAIL(&vsi->vlan_list, f, next);
vsi->vlan_num++;
- ret = 0;
-
-DONE:
- ice_free(hw, v_list_itr);
- return ret;
+ return 0;
}
static int
ice_remove_vlan_filter(struct ice_vsi *vsi, struct ice_vlan *vlan)
{
- struct ice_fltr_list_entry *v_list_itr = NULL;
+ struct ice_fltr_list_entry v_list_entry = {0};
+ struct ice_fltr_list_entry *v_list_itr = &v_list_entry;
struct ice_vlan_filter *f;
struct LIST_HEAD_TYPE list_head;
struct ice_hw *hw;
@@ -1353,13 +1323,6 @@ ice_remove_vlan_filter(struct ice_vsi *vsi, struct ice_vlan *vlan)
INIT_LIST_HEAD(&list_head);
- v_list_itr = (struct ice_fltr_list_entry *)
- ice_malloc(hw, sizeof(*v_list_itr));
- if (!v_list_itr) {
- ret = -ENOMEM;
- goto DONE;
- }
-
v_list_itr->fltr_info.l_data.vlan.vlan_id = vlan->vid;
v_list_itr->fltr_info.l_data.vlan.tpid = vlan->tpid;
v_list_itr->fltr_info.l_data.vlan.tpid_valid = true;
@@ -1375,8 +1338,7 @@ ice_remove_vlan_filter(struct ice_vsi *vsi, struct ice_vlan *vlan)
ret = ice_remove_vlan(hw, &list_head);
if (ret != ICE_SUCCESS) {
PMD_DRV_LOG(ERR, "Failed to remove VLAN filter");
- ret = -EINVAL;
- goto DONE;
+ return -EINVAL;
}
/* Remove the vlan id from vlan list */
@@ -1384,10 +1346,7 @@ ice_remove_vlan_filter(struct ice_vsi *vsi, struct ice_vlan *vlan)
rte_free(f);
vsi->vlan_num--;
- ret = 0;
-DONE:
- ice_free(hw, v_list_itr);
- return ret;
+ return 0;
}
static int
@@ -2554,7 +2513,7 @@ ice_get_hw_res(struct ice_hw *hw, uint16_t res_type,
uint16_t num, uint16_t desc_id,
uint16_t *prof_buf, uint16_t *num_prof)
{
- struct ice_aqc_res_elem *resp_buf;
+ struct ice_aqc_res_elem resp_buf[ICE_MAX_RES_DESC_NUM] = {0};
int ret;
uint16_t buf_len;
bool res_shared = 1;
@@ -2563,10 +2522,10 @@ ice_get_hw_res(struct ice_hw *hw, uint16_t res_type,
struct ice_aqc_get_allocd_res_desc *cmd =
&aq_desc.params.get_res_desc;
+ if (num > RTE_DIM(resp_buf))
+ return -EINVAL;
+
buf_len = sizeof(*resp_buf) * num;
- resp_buf = ice_malloc(hw, buf_len);
- if (!resp_buf)
- return -ENOMEM;
ice_fill_dflt_direct_cmd_desc(&aq_desc,
ice_aqc_opc_get_allocd_res_desc);
@@ -2580,13 +2539,14 @@ ice_get_hw_res(struct ice_hw *hw, uint16_t res_type,
if (!ret)
*num_prof = LE16_TO_CPU(cmd->ops.resp.num_desc);
else
- goto exit;
+ return ret;
+
+ if (*num_prof > num)
+ return -EIO;
ice_memcpy(prof_buf, resp_buf, sizeof(*resp_buf) *
(*num_prof), ICE_NONDMA_TO_NONDMA);
-exit:
- ice_free(hw, resp_buf);
return ret;
}
static int
@@ -5276,7 +5236,8 @@ ice_phy_conf_link(struct ice_hw *hw,
struct ice_aqc_set_phy_cfg_data cfg = { 0 };
struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(hw->back);
struct ice_port_info *pi = hw->port_info;
- struct ice_aqc_get_phy_caps_data *phy_caps;
+ struct ice_aqc_get_phy_caps_data phy_caps_data = {0};
+ struct ice_aqc_get_phy_caps_data *phy_caps = &phy_caps_data;
int err;
u64 phy_type_low = 0;
u64 phy_type_high = 0;
@@ -5284,11 +5245,6 @@ ice_phy_conf_link(struct ice_hw *hw,
if (!pi)
return -EIO;
- phy_caps = (struct ice_aqc_get_phy_caps_data *)
- ice_malloc(hw, sizeof(*phy_caps));
- if (!phy_caps)
- return ICE_ERR_NO_MEMORY;
-
if (ice_fw_supports_report_dflt_cfg(pi->hw))
err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_DFLT_CFG,
@@ -5297,7 +5253,7 @@ ice_phy_conf_link(struct ice_hw *hw,
err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
phy_caps, NULL);
if (err)
- goto done;
+ return err;
ice_update_phy_type(&phy_type_low, &phy_type_high, link_speeds_bitmap);
@@ -5333,8 +5289,6 @@ ice_phy_conf_link(struct ice_hw *hw,
err = ice_aq_set_phy_cfg(hw, pi, &cfg, NULL);
-done:
- ice_free(hw, phy_caps);
return err;
}
diff --git a/drivers/net/intel/ice/ice_rxtx.c b/drivers/net/intel/ice/ice_rxtx.c
index 6f19e070ee..a865541375 100644
--- a/drivers/net/intel/ice/ice_rxtx.c
+++ b/drivers/net/intel/ice/ice_rxtx.c
@@ -790,7 +790,7 @@ ice_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
struct ice_vsi *vsi;
struct ice_hw *hw;
struct ice_pf *pf;
- struct ice_aqc_add_tx_qgrp *txq_elem;
+ RTE_DEFINE_FLEX(struct ice_aqc_add_tx_qgrp, txq_elem, txqs, 1);
struct ice_tlan_ctx tx_ctx;
int buf_len;
struct ice_adapter *ad = ICE_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
@@ -816,9 +816,6 @@ ice_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
return 0;
buf_len = ice_struct_size(txq_elem, txqs, 1);
- txq_elem = ice_malloc(hw, buf_len);
- if (!txq_elem)
- return -ENOMEM;
vsi = txq->ice_vsi;
hw = ICE_VSI_TO_HW(vsi);
@@ -854,13 +851,11 @@ ice_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
}
if (cgd_idx >= ICE_MAX_TRAFFIC_CLASS) {
PMD_DRV_LOG(ERR, "Bad queue mapping configuration");
- ice_free(hw, txq_elem);
return -EINVAL;
}
} else if (pf->dcb_num_tcs > 1) {
/* TM only manages the TC0 scheduler subtree. */
PMD_DRV_LOG(ERR, "TM hierarchy is not supported together with multi-TC DCB");
- ice_free(hw, txq_elem);
return -EINVAL;
}
@@ -874,7 +869,6 @@ ice_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
txq_elem, buf_len, NULL);
if (err) {
PMD_DRV_LOG(ERR, "Failed to add lan txq");
- ice_free(hw, txq_elem);
return -EIO;
}
/* store the schedule node id */
@@ -884,7 +878,6 @@ ice_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
if (pf->tm_conf.committed)
if (ice_tm_setup_txq_node(pf, hw, tx_queue_id, txq->q_teid) != 0) {
PMD_DRV_LOG(ERR, "Failed to set up txq traffic management node");
- ice_free(hw, txq_elem);
return -EIO;
}
@@ -893,15 +886,10 @@ ice_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
txq->use_ctx = ad->use_ctx;
if (txq->tsq != NULL && txq->tsq->ts_flag > 0) {
- struct ice_aqc_set_txtime_qgrp *ts_elem;
+ RTE_DEFINE_FLEX(struct ice_aqc_set_txtime_qgrp, ts_elem, txtimeqs, 1);
struct ice_txtime_ctx txtime_ctx = { 0 };
u8 ts_buf_len = ice_struct_size(ts_elem, txtimeqs, 1);
- ts_elem = ice_malloc(hw, ts_buf_len);
- if (!ts_elem) {
- ice_free(hw, txq_elem);
- return -ENOMEM;
- }
ice_setup_txtime_ctx(txq, &txtime_ctx, true);
ice_set_ctx(hw, (u8 *)&txtime_ctx,
ts_elem->txtimeqs[0].txtime_ctx,
@@ -913,10 +901,8 @@ ice_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
ICE_PCI_REG_WRITE(txq->qtx_tail, 0);
err = ice_aq_set_txtimeq(hw, txq->reg_idx, 1, ts_elem, ts_buf_len, NULL);
- ice_free(hw, ts_elem);
if (err) {
PMD_DRV_LOG(ERR, "Failed to set Tx Time queue context, error: %d", err);
- ice_free(hw, txq_elem);
return err;
}
} else {
@@ -928,7 +914,6 @@ ice_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
- ice_free(hw, txq_elem);
return 0;
}
@@ -1052,7 +1037,7 @@ ice_fdir_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
int err;
struct ice_vsi *vsi;
struct ice_hw *hw;
- struct ice_aqc_add_tx_qgrp *txq_elem;
+ RTE_DEFINE_FLEX(struct ice_aqc_add_tx_qgrp, txq_elem, txqs, 1);
struct ice_tlan_ctx tx_ctx;
int buf_len;
@@ -1066,9 +1051,6 @@ ice_fdir_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
}
buf_len = ice_struct_size(txq_elem, txqs, 1);
- txq_elem = ice_malloc(hw, buf_len);
- if (!txq_elem)
- return -ENOMEM;
vsi = txq->ice_vsi;
hw = ICE_VSI_TO_HW(vsi);
@@ -1100,13 +1082,11 @@ ice_fdir_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
txq_elem, buf_len, NULL);
if (err) {
PMD_DRV_LOG(ERR, "Failed to add FDIR txq");
- ice_free(hw, txq_elem);
return -EIO;
}
/* store the schedule node id */
txq->q_teid = txq_elem->txqs[0].q_teid;
- ice_free(hw, txq_elem);
return 0;
}
diff --git a/drivers/net/intel/ice/ice_tm.c b/drivers/net/intel/ice/ice_tm.c
index 9566a36098..82b0bc5f4a 100644
--- a/drivers/net/intel/ice/ice_tm.c
+++ b/drivers/net/intel/ice/ice_tm.c
@@ -712,14 +712,10 @@ ice_tm_setup_txq_node(struct ice_pf *pf, struct ice_hw *hw, uint16_t qid, uint32
/* if the queue node has been put in the wrong place in hierarchy */
if (hw_node->parent != sw_node->parent->sched_node) {
- struct ice_aqc_move_txqs_data *buf;
+ RTE_DEFINE_FLEX(struct ice_aqc_move_txqs_data, buf, txqs, 1);
uint8_t txqs_moved = 0;
uint16_t buf_size = ice_struct_size(buf, txqs, 1);
- buf = calloc(1, buf_size);
- if (buf == NULL)
- return -ENOMEM;
-
struct ice_sched_node *parent = hw_node->parent;
struct ice_sched_node *new_parent = sw_node->parent->sched_node;
buf->src_teid = parent->info.node_teid;
@@ -729,7 +725,6 @@ ice_tm_setup_txq_node(struct ice_pf *pf, struct ice_hw *hw, uint16_t qid, uint32
int ret = ice_aq_move_recfg_lan_txq(hw, 1, true, false, false, false, 50,
NULL, buf, buf_size, &txqs_moved, NULL);
- free(buf);
if (ret || txqs_moved == 0) {
PMD_DRV_LOG(ERR, "move lan queue %u failed", qid);
return ICE_ERR_PARAM;
--
2.55.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH 27.03 4/6] net/idpf: use stack storage for virtchnl messages
2026-11-16 16:09 [PATCH 27.03 0/6] eliminate unnecessary dynamic allocation Bruce Richardson
` (2 preceding siblings ...)
2026-11-16 16:09 ` [PATCH 27.03 3/6] net/ice: use stack storage for function-local buffers Bruce Richardson
@ 2026-11-16 16:09 ` Bruce Richardson
2026-11-16 16:09 ` [PATCH 27.03 5/6] net/cpfl: use stack storage for function-local buffers Bruce Richardson
2026-11-16 16:09 ` [PATCH 27.03 6/6] net/i40e: " Bruce Richardson
5 siblings, 0 replies; 7+ messages in thread
From: Bruce Richardson @ 2026-11-16 16:09 UTC (permalink / raw)
To: dev; +Cc: Bruce Richardson
Rather than using malloc and free for virtchnl message buffers whose
maximum size is known at compile time, define them on the stack using
RTE_DEFINE_FLEX. This removes the allocation failure and cleanup paths.
As part of this, the queue chunks in idpf_vc_queues_ena_dis() are now
filled sequentially rather than indexed by queue type.
Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
---
drivers/net/intel/idpf/idpf_common_device.c | 27 ++-----
drivers/net/intel/idpf/idpf_common_virtchnl.c | 77 +++++++------------
2 files changed, 36 insertions(+), 68 deletions(-)
diff --git a/drivers/net/intel/idpf/idpf_common_device.c b/drivers/net/intel/idpf/idpf_common_device.c
index 1586dae96c..d3cb74f4a8 100644
--- a/drivers/net/intel/idpf/idpf_common_device.c
+++ b/drivers/net/intel/idpf/idpf_common_device.c
@@ -157,23 +157,15 @@ idpf_init_mbx(struct idpf_hw *hw)
static int
idpf_get_pkt_type(struct idpf_adapter *adapter)
{
- struct virtchnl2_get_ptype_info *req_ptype_info;
- struct virtchnl2_get_ptype_info *recv_ptype_info;
+ struct virtchnl2_get_ptype_info req_ptype_msg = {0};
+ struct virtchnl2_get_ptype_info *req_ptype_info = &req_ptype_msg;
+ RTE_DEFINE_FLEX_BYTES(struct virtchnl2_get_ptype_info, recv_ptype_info,
+ IDPF_DFLT_MBX_BUF_SIZE);
uint16_t recv_num_ptypes = 0;
uint16_t ptype_offset, i, j;
uint16_t start_ptype_id = 0;
int ret;
- req_ptype_info = rte_zmalloc("req_ptype_info", IDPF_DFLT_MBX_BUF_SIZE, 0);
- if (req_ptype_info == NULL)
- return -ENOMEM;
-
- recv_ptype_info = rte_zmalloc("recv_ptype_info", IDPF_DFLT_MBX_BUF_SIZE, 0);
- if (recv_ptype_info == NULL) {
- ret = -ENOMEM;
- goto free_req_ptype_info;
- }
-
while (start_ptype_id < IDPF_MAX_PKT_TYPE) {
memset(req_ptype_info, 0, sizeof(*req_ptype_info));
memset(recv_ptype_info, 0, sizeof(*recv_ptype_info));
@@ -188,13 +180,13 @@ idpf_get_pkt_type(struct idpf_adapter *adapter)
ret = idpf_vc_ptype_info_query(adapter, req_ptype_info, recv_ptype_info);
if (ret != 0) {
DRV_LOG(ERR, "Fail to query packet type information");
- goto free_recv_ptype_info;
+ goto out;
}
recv_num_ptypes += rte_le_to_cpu_16(recv_ptype_info->num_ptypes);
if (recv_num_ptypes > IDPF_MAX_PKT_TYPE) {
ret = -EINVAL;
- goto free_recv_ptype_info;
+ goto out;
}
start_ptype_id = rte_le_to_cpu_16(req_ptype_info->start_ptype_id) +
@@ -213,7 +205,7 @@ idpf_get_pkt_type(struct idpf_adapter *adapter)
ptype_offset += IDPF_GET_PTYPE_SIZE(ptype);
if (ptype_offset > IDPF_DFLT_MBX_BUF_SIZE) {
ret = -EINVAL;
- goto free_recv_ptype_info;
+ goto out;
}
for (j = 0; j < ptype->proto_id_count; j++) {
@@ -374,10 +366,7 @@ idpf_get_pkt_type(struct idpf_adapter *adapter)
}
}
-free_recv_ptype_info:
- rte_free(recv_ptype_info);
-free_req_ptype_info:
- rte_free(req_ptype_info);
+out:
clear_cmd(adapter);
return ret;
}
diff --git a/drivers/net/intel/idpf/idpf_common_virtchnl.c b/drivers/net/intel/idpf/idpf_common_virtchnl.c
index 4befd5fc83..2159dd9ad1 100644
--- a/drivers/net/intel/idpf/idpf_common_virtchnl.c
+++ b/drivers/net/intel/idpf/idpf_common_virtchnl.c
@@ -463,15 +463,15 @@ int
idpf_vc_rss_key_set(struct idpf_vport *vport)
{
struct idpf_adapter *adapter = vport->adapter;
- struct virtchnl2_rss_key *rss_key;
+ RTE_DEFINE_FLEX(struct virtchnl2_rss_key, rss_key, key, IDPF_RSS_KEY_LEN - 1);
struct idpf_cmd_info args;
int len, err;
+ if (vport->rss_key_size > IDPF_RSS_KEY_LEN)
+ return -EINVAL;
+
len = sizeof(*rss_key) + sizeof(rss_key->key[0]) *
(vport->rss_key_size - 1);
- rss_key = rte_zmalloc("rss_key", len, 0);
- if (rss_key == NULL)
- return -ENOMEM;
rss_key->vport_id = vport->vport_id;
rss_key->key_len = vport->rss_key_size;
@@ -489,7 +489,6 @@ idpf_vc_rss_key_set(struct idpf_vport *vport)
if (err != 0)
DRV_LOG(ERR, "Failed to execute command of VIRTCHNL2_OP_SET_RSS_KEY");
- rte_free(rss_key);
return err;
}
@@ -781,15 +780,13 @@ idpf_vc_ena_dis_one_queue(struct idpf_vport *vport, uint16_t qid,
uint32_t type, bool on)
{
struct idpf_adapter *adapter = vport->adapter;
- struct virtchnl2_del_ena_dis_queues *queue_select;
+ struct virtchnl2_del_ena_dis_queues queue_select_msg = {0};
+ struct virtchnl2_del_ena_dis_queues *queue_select = &queue_select_msg;
struct virtchnl2_queue_chunk *queue_chunk;
struct idpf_cmd_info args;
int err, len;
len = sizeof(struct virtchnl2_del_ena_dis_queues);
- queue_select = rte_zmalloc("queue_select", len, 0);
- if (queue_select == NULL)
- return -ENOMEM;
queue_chunk = queue_select->chunks.chunks;
queue_select->chunks.num_chunks = 1;
@@ -810,7 +807,6 @@ idpf_vc_ena_dis_one_queue(struct idpf_vport *vport, uint16_t qid,
DRV_LOG(ERR, "Failed to execute command of VIRTCHNL2_OP_%s_QUEUES",
on ? "ENABLE" : "DISABLE");
- rte_free(queue_select);
return err;
}
@@ -819,15 +815,13 @@ int
idpf_vc_ena_dis_one_queue_vcpf(struct idpf_adapter *adapter, uint16_t qid,
uint32_t type, bool on)
{
- struct virtchnl2_del_ena_dis_queues *queue_select;
+ struct virtchnl2_del_ena_dis_queues queue_select_msg = {0};
+ struct virtchnl2_del_ena_dis_queues *queue_select = &queue_select_msg;
struct virtchnl2_queue_chunk *queue_chunk;
struct idpf_cmd_info args;
int err, len;
len = sizeof(struct virtchnl2_del_ena_dis_queues);
- queue_select = rte_zmalloc("queue_select", len, 0);
- if (queue_select == NULL)
- return -ENOMEM;
queue_chunk = queue_select->chunks.chunks;
queue_select->chunks.num_chunks = 1;
@@ -848,7 +842,6 @@ idpf_vc_ena_dis_one_queue_vcpf(struct idpf_adapter *adapter, uint16_t qid,
DRV_LOG(ERR, "Failed to execute command of VIRTCHNL2_OP_%s_QUEUES",
on ? "ENABLE" : "DISABLE");
- rte_free(queue_select);
return err;
}
@@ -893,16 +886,18 @@ idpf_vc_queue_switch(struct idpf_vport *vport, uint16_t qid,
}
#define IDPF_RXTX_QUEUE_CHUNKS_NUM 2
+#define IDPF_RXTX_QUEUE_CHUNKS_MAX (IDPF_RXTX_QUEUE_CHUNKS_NUM + 2)
RTE_EXPORT_INTERNAL_SYMBOL(idpf_vc_queues_ena_dis)
int
idpf_vc_queues_ena_dis(struct idpf_vport *vport, bool enable)
{
struct idpf_adapter *adapter = vport->adapter;
- struct virtchnl2_del_ena_dis_queues *queue_select;
+ RTE_DEFINE_FLEX(struct virtchnl2_del_ena_dis_queues, queue_select, chunks.chunks,
+ IDPF_RXTX_QUEUE_CHUNKS_MAX - 1);
struct virtchnl2_queue_chunk *queue_chunk;
uint32_t type;
struct idpf_cmd_info args;
- uint16_t num_chunks;
+ uint16_t num_chunks, idx = 0;
int err, len;
num_chunks = IDPF_RXTX_QUEUE_CHUNKS_NUM;
@@ -913,38 +908,35 @@ idpf_vc_queues_ena_dis(struct idpf_vport *vport, bool enable)
len = sizeof(struct virtchnl2_del_ena_dis_queues) +
sizeof(struct virtchnl2_queue_chunk) * (num_chunks - 1);
- queue_select = rte_zmalloc("queue_select", len, 0);
- if (queue_select == NULL)
- return -ENOMEM;
queue_chunk = queue_select->chunks.chunks;
queue_select->chunks.num_chunks = num_chunks;
queue_select->vport_id = vport->vport_id;
type = VIRTCHNL_QUEUE_TYPE_RX;
- queue_chunk[type].type = type;
- queue_chunk[type].start_queue_id = vport->chunks_info.rx_start_qid;
- queue_chunk[type].num_queues = vport->num_rx_q;
+ queue_chunk[idx].type = type;
+ queue_chunk[idx].start_queue_id = vport->chunks_info.rx_start_qid;
+ queue_chunk[idx++].num_queues = vport->num_rx_q;
type = VIRTCHNL2_QUEUE_TYPE_TX;
- queue_chunk[type].type = type;
- queue_chunk[type].start_queue_id = vport->chunks_info.tx_start_qid;
- queue_chunk[type].num_queues = vport->num_tx_q;
+ queue_chunk[idx].type = type;
+ queue_chunk[idx].start_queue_id = vport->chunks_info.tx_start_qid;
+ queue_chunk[idx++].num_queues = vport->num_tx_q;
if (vport->rxq_model == VIRTCHNL2_QUEUE_MODEL_SPLIT) {
type = VIRTCHNL2_QUEUE_TYPE_RX_BUFFER;
- queue_chunk[type].type = type;
- queue_chunk[type].start_queue_id =
+ queue_chunk[idx].type = type;
+ queue_chunk[idx].start_queue_id =
vport->chunks_info.rx_buf_start_qid;
- queue_chunk[type].num_queues = vport->num_rx_bufq;
+ queue_chunk[idx++].num_queues = vport->num_rx_bufq;
}
if (vport->txq_model == VIRTCHNL2_QUEUE_MODEL_SPLIT) {
type = VIRTCHNL2_QUEUE_TYPE_TX_COMPLETION;
- queue_chunk[type].type = type;
- queue_chunk[type].start_queue_id =
+ queue_chunk[idx].type = type;
+ queue_chunk[idx].start_queue_id =
vport->chunks_info.tx_compl_start_qid;
- queue_chunk[type].num_queues = vport->num_tx_complq;
+ queue_chunk[idx++].num_queues = vport->num_tx_complq;
}
args.ops = enable ? VIRTCHNL2_OP_ENABLE_QUEUES :
@@ -958,7 +950,6 @@ idpf_vc_queues_ena_dis(struct idpf_vport *vport, bool enable)
DRV_LOG(ERR, "Failed to execute command of VIRTCHNL2_OP_%s_QUEUES",
enable ? "ENABLE" : "DISABLE");
- rte_free(queue_select);
return err;
}
@@ -1044,7 +1035,8 @@ int
idpf_vc_rxq_config(struct idpf_vport *vport, struct idpf_rx_queue *rxq)
{
struct idpf_adapter *adapter = vport->adapter;
- struct virtchnl2_config_rx_queues *vc_rxqs = NULL;
+ RTE_DEFINE_FLEX(struct virtchnl2_config_rx_queues, vc_rxqs, qinfo,
+ IDPF_RXQ_PER_GRP + IDPF_RX_BUFQ_PER_GRP - 1);
struct virtchnl2_rxq_info *rxq_info;
struct idpf_cmd_info args;
uint16_t num_qs;
@@ -1057,12 +1049,6 @@ idpf_vc_rxq_config(struct idpf_vport *vport, struct idpf_rx_queue *rxq)
size = sizeof(*vc_rxqs) + (num_qs - 1) *
sizeof(struct virtchnl2_rxq_info);
- vc_rxqs = rte_zmalloc("cfg_rxqs", size, 0);
- if (vc_rxqs == NULL) {
- DRV_LOG(ERR, "Failed to allocate virtchnl2_config_rx_queues");
- err = -ENOMEM;
- return err;
- }
vc_rxqs->vport_id = vport->vport_id;
vc_rxqs->num_qinfo = num_qs;
if (vport->rxq_model == VIRTCHNL2_QUEUE_MODEL_SINGLE) {
@@ -1122,7 +1108,6 @@ idpf_vc_rxq_config(struct idpf_vport *vport, struct idpf_rx_queue *rxq)
args.out_size = IDPF_DFLT_MBX_BUF_SIZE;
err = idpf_vc_cmd_execute(adapter, &args);
- rte_free(vc_rxqs);
if (err != 0)
DRV_LOG(ERR, "Failed to execute command of VIRTCHNL2_OP_CONFIG_RX_QUEUES");
@@ -1170,7 +1155,8 @@ int
idpf_vc_txq_config(struct idpf_vport *vport, struct ci_tx_queue *txq)
{
struct idpf_adapter *adapter = vport->adapter;
- struct virtchnl2_config_tx_queues *vc_txqs = NULL;
+ RTE_DEFINE_FLEX(struct virtchnl2_config_tx_queues, vc_txqs, qinfo,
+ IDPF_TXQ_PER_GRP + IDPF_TX_COMPLQ_PER_GRP - 1);
struct virtchnl2_txq_info *txq_info;
struct idpf_cmd_info args;
uint16_t num_qs;
@@ -1183,12 +1169,6 @@ idpf_vc_txq_config(struct idpf_vport *vport, struct ci_tx_queue *txq)
size = sizeof(*vc_txqs) + (num_qs - 1) *
sizeof(struct virtchnl2_txq_info);
- vc_txqs = rte_zmalloc("cfg_txqs", size, 0);
- if (vc_txqs == NULL) {
- DRV_LOG(ERR, "Failed to allocate virtchnl2_config_tx_queues");
- err = -ENOMEM;
- return err;
- }
vc_txqs->vport_id = vport->vport_id;
vc_txqs->num_qinfo = num_qs;
@@ -1230,7 +1210,6 @@ idpf_vc_txq_config(struct idpf_vport *vport, struct ci_tx_queue *txq)
args.out_size = IDPF_DFLT_MBX_BUF_SIZE;
err = idpf_vc_cmd_execute(adapter, &args);
- rte_free(vc_txqs);
if (err != 0)
DRV_LOG(ERR, "Failed to execute command of VIRTCHNL2_OP_CONFIG_TX_QUEUES");
--
2.55.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH 27.03 5/6] net/cpfl: use stack storage for function-local buffers
2026-11-16 16:09 [PATCH 27.03 0/6] eliminate unnecessary dynamic allocation Bruce Richardson
` (3 preceding siblings ...)
2026-11-16 16:09 ` [PATCH 27.03 4/6] net/idpf: use stack storage for virtchnl messages Bruce Richardson
@ 2026-11-16 16:09 ` Bruce Richardson
2026-11-16 16:09 ` [PATCH 27.03 6/6] net/i40e: " Bruce Richardson
5 siblings, 0 replies; 7+ messages in thread
From: Bruce Richardson @ 2026-11-16 16:09 UTC (permalink / raw)
To: dev; +Cc: Bruce Richardson
Rather than using malloc and free for virtchnl and control queue buffers
used only within a single function, define them on the stack, using
RTE_DEFINE_FLEX for those structures ending in a variable-length array.
This removes the allocation failure and cleanup paths.
Where the number of elements used depends on runtime values, add checks
that those values do not exceed the buffer sizes.
Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
---
drivers/net/intel/cpfl/cpfl_fxp_rule.c | 20 ++++-------
drivers/net/intel/cpfl/cpfl_representor.c | 15 +++++----
drivers/net/intel/cpfl/cpfl_vchnl.c | 41 +++++++++--------------
3 files changed, 32 insertions(+), 44 deletions(-)
diff --git a/drivers/net/intel/cpfl/cpfl_fxp_rule.c b/drivers/net/intel/cpfl/cpfl_fxp_rule.c
index d1c992dfe4..c522dde6d8 100644
--- a/drivers/net/intel/cpfl/cpfl_fxp_rule.c
+++ b/drivers/net/intel/cpfl/cpfl_fxp_rule.c
@@ -13,23 +13,21 @@ int
cpfl_send_ctlq_msg(struct idpf_hw *hw, struct idpf_ctlq_info *cq, u16 num_q_msg,
struct idpf_ctlq_msg q_msg[])
{
- struct idpf_ctlq_msg **msg_ptr_list;
+ struct idpf_ctlq_msg *msg_ptr_list[CPFL_FLOW_BATCH_SIZE] = {0};
u16 clean_count = 0;
int num_cleaned = 0;
int retries = 0;
int ret = 0;
- msg_ptr_list = calloc(num_q_msg, sizeof(struct idpf_ctlq_msg *));
- if (!msg_ptr_list) {
- PMD_INIT_LOG(ERR, "no memory for cleaning ctlq");
- ret = -ENOMEM;
- goto err;
+ if (num_q_msg > RTE_DIM(msg_ptr_list)) {
+ PMD_INIT_LOG(ERR, "too many messages for ctlq: %u", num_q_msg);
+ return -EINVAL;
}
ret = cpfl_vport_ctlq_send(hw, cq, num_q_msg, q_msg);
if (ret) {
PMD_INIT_LOG(ERR, "cpfl_vport_ctlq_send() failed with error: 0x%4x", ret);
- goto send_err;
+ return ret;
}
while (retries <= CTLQ_SEND_RETRIES) {
@@ -38,7 +36,7 @@ cpfl_send_ctlq_msg(struct idpf_hw *hw, struct idpf_ctlq_info *cq, u16 num_q_msg,
&msg_ptr_list[num_cleaned]);
if (ret) {
PMD_INIT_LOG(ERR, "clean ctlq failed: 0x%4x", ret);
- goto send_err;
+ return ret;
}
num_cleaned += clean_count;
@@ -50,13 +48,9 @@ cpfl_send_ctlq_msg(struct idpf_hw *hw, struct idpf_ctlq_info *cq, u16 num_q_msg,
if (retries > CTLQ_SEND_RETRIES) {
PMD_INIT_LOG(ERR, "timed out while polling for completions");
- ret = -1;
- goto send_err;
+ return -1;
}
-send_err:
- free(msg_ptr_list);
-err:
return ret;
}
diff --git a/drivers/net/intel/cpfl/cpfl_representor.c b/drivers/net/intel/cpfl/cpfl_representor.c
index bb28c0b989..91bbb3af48 100644
--- a/drivers/net/intel/cpfl/cpfl_representor.c
+++ b/drivers/net/intel/cpfl/cpfl_representor.c
@@ -558,14 +558,11 @@ cpfl_repr_create(struct rte_pci_device *pci_dev, struct cpfl_adapter_ext *adapte
uint32_t iter = 0;
const struct cpfl_repr_id *repr_id;
const struct cpfl_vport_id *vp_id;
- struct cpchnl2_get_vport_list_response *vlist_resp;
+ RTE_DEFINE_FLEX_BYTES(struct cpchnl2_get_vport_list_response, vlist_resp,
+ IDPF_DFLT_MBX_BUF_SIZE);
struct cpchnl2_get_vport_info_response vinfo_resp;
int ret;
- vlist_resp = rte_zmalloc(NULL, IDPF_DFLT_MBX_BUF_SIZE, 0);
- if (vlist_resp == NULL)
- return -ENOMEM;
-
rte_spinlock_lock(&adapter->repr_lock);
while (rte_hash_iterate(adapter->repr_allowlist_hash,
@@ -603,6 +600,13 @@ cpfl_repr_create(struct rte_pci_device *pci_dev, struct cpfl_adapter_ext *adapte
PMD_INIT_LOG(WARNING, "No matched vport for representor %s", name);
continue;
}
+ if (vlist_resp->nof_vports > (IDPF_DFLT_MBX_BUF_SIZE - sizeof(*vlist_resp)) /
+ sizeof(vlist_resp->vports[0])) {
+ PMD_INIT_LOG(ERR, "Invalid vport count %u for representor %s",
+ vlist_resp->nof_vports, name);
+ ret = -EINVAL;
+ goto err;
+ }
/* get all vport info for the port representor */
for (i = 0; i < vlist_resp->nof_vports; i++) {
@@ -657,6 +661,5 @@ cpfl_repr_create(struct rte_pci_device *pci_dev, struct cpfl_adapter_ext *adapte
err:
rte_spinlock_unlock(&adapter->repr_lock);
- rte_free(vlist_resp);
return ret;
}
diff --git a/drivers/net/intel/cpfl/cpfl_vchnl.c b/drivers/net/intel/cpfl/cpfl_vchnl.c
index 593516f700..6a4e4dac1b 100644
--- a/drivers/net/intel/cpfl/cpfl_vchnl.c
+++ b/drivers/net/intel/cpfl/cpfl_vchnl.c
@@ -147,18 +147,16 @@ vcpf_add_queues(struct cpfl_adapter_ext *adapter)
int
vcpf_del_queues(struct cpfl_adapter_ext *adapter)
{
- struct virtchnl2_del_ena_dis_queues *del_cfgq;
+ RTE_DEFINE_FLEX(struct virtchnl2_del_ena_dis_queues, del_cfgq, chunks.chunks,
+ VCPF_CFGQ_NUM - 1);
u16 num_chunks;
struct idpf_cmd_info args;
- int i, err, size;
+ int i, err;
num_chunks = adapter->cfgq_in.cfgq_add->chunks.num_chunks;
- size = idpf_struct_size(del_cfgq, chunks.chunks, (num_chunks - 1));
- del_cfgq = rte_zmalloc("del_cfgq", size, 0);
- if (!del_cfgq) {
- PMD_DRV_LOG(ERR, "Failed to allocate virtchnl2_del_ena_dis_queues");
- err = -ENOMEM;
- return err;
+ if (num_chunks > VCPF_CFGQ_NUM) {
+ PMD_DRV_LOG(ERR, "Too many config queue chunks: %u", num_chunks);
+ return -EINVAL;
}
del_cfgq->vport_id = rte_cpu_to_le_32(VCPF_CFGQ_VPORT_ID);
@@ -183,7 +181,6 @@ vcpf_del_queues(struct cpfl_adapter_ext *adapter)
args.out_size = IDPF_DFLT_MBX_BUF_SIZE;
err = idpf_vc_cmd_execute(&adapter->base, &args);
- rte_free(del_cfgq);
if (err) {
PMD_DRV_LOG(ERR,
"Failed to execute command VIRTCHNL2_OP_DEL_QUEUES");
@@ -210,7 +207,7 @@ int
cpfl_config_ctlq_rx(struct cpfl_adapter_ext *adapter)
{
struct cpfl_vport *vport = &adapter->ctrl_vport;
- struct virtchnl2_config_rx_queues *vc_rxqs = NULL;
+ RTE_DEFINE_FLEX(struct virtchnl2_config_rx_queues, vc_rxqs, qinfo, CPFL_RX_CFGQ_NUM - 1);
struct virtchnl2_rxq_info *rxq_info;
struct idpf_cmd_info args;
uint16_t num_qs;
@@ -225,15 +222,13 @@ cpfl_config_ctlq_rx(struct cpfl_adapter_ext *adapter)
}
num_qs = adapter->num_rx_cfgq;
+ if (num_qs > CPFL_RX_CFGQ_NUM) {
+ PMD_DRV_LOG(ERR, "Too many Rx config queues: %u", num_qs);
+ return -EINVAL;
+ }
size = sizeof(*vc_rxqs) + (num_qs - 1) *
sizeof(struct virtchnl2_rxq_info);
- vc_rxqs = rte_zmalloc("cfg_rxqs", size, 0);
- if (!vc_rxqs) {
- PMD_DRV_LOG(ERR, "Failed to allocate virtchnl2_config_rx_queues");
- err = -ENOMEM;
- return err;
- }
if (idpf_is_vf_device(&adapter->base.hw))
vc_rxqs->vport_id = rte_cpu_to_le_32(VCPF_CFGQ_VPORT_ID);
@@ -264,7 +259,6 @@ cpfl_config_ctlq_rx(struct cpfl_adapter_ext *adapter)
args.out_size = IDPF_DFLT_MBX_BUF_SIZE;
err = idpf_vc_cmd_execute(&adapter->base, &args);
- rte_free(vc_rxqs);
if (err)
PMD_DRV_LOG(ERR, "Failed to execute command of VIRTCHNL2_OP_CONFIG_RX_QUEUES");
@@ -275,7 +269,7 @@ int
cpfl_config_ctlq_tx(struct cpfl_adapter_ext *adapter)
{
struct cpfl_vport *vport = &adapter->ctrl_vport;
- struct virtchnl2_config_tx_queues *vc_txqs = NULL;
+ RTE_DEFINE_FLEX(struct virtchnl2_config_tx_queues, vc_txqs, qinfo, CPFL_TX_CFGQ_NUM - 1);
struct virtchnl2_txq_info *txq_info;
struct idpf_cmd_info args;
uint16_t num_qs;
@@ -290,15 +284,13 @@ cpfl_config_ctlq_tx(struct cpfl_adapter_ext *adapter)
}
num_qs = adapter->num_tx_cfgq;
+ if (num_qs > CPFL_TX_CFGQ_NUM) {
+ PMD_DRV_LOG(ERR, "Too many Tx config queues: %u", num_qs);
+ return -EINVAL;
+ }
size = sizeof(*vc_txqs) + (num_qs - 1) *
sizeof(struct virtchnl2_txq_info);
- vc_txqs = rte_zmalloc("cfg_txqs", size, 0);
- if (!vc_txqs) {
- PMD_DRV_LOG(ERR, "Failed to allocate virtchnl2_config_tx_queues");
- err = -ENOMEM;
- return err;
- }
if (idpf_is_vf_device(&adapter->base.hw))
vc_txqs->vport_id = rte_cpu_to_le_32(VCPF_CFGQ_VPORT_ID);
@@ -325,7 +317,6 @@ cpfl_config_ctlq_tx(struct cpfl_adapter_ext *adapter)
args.out_size = IDPF_DFLT_MBX_BUF_SIZE;
err = idpf_vc_cmd_execute(&adapter->base, &args);
- rte_free(vc_txqs);
if (err)
PMD_DRV_LOG(ERR, "Failed to execute command of VIRTCHNL2_OP_CONFIG_TX_QUEUES");
--
2.55.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH 27.03 6/6] net/i40e: use stack storage for function-local buffers
2026-11-16 16:09 [PATCH 27.03 0/6] eliminate unnecessary dynamic allocation Bruce Richardson
` (4 preceding siblings ...)
2026-11-16 16:09 ` [PATCH 27.03 5/6] net/cpfl: use stack storage for function-local buffers Bruce Richardson
@ 2026-11-16 16:09 ` Bruce Richardson
5 siblings, 0 replies; 7+ messages in thread
From: Bruce Richardson @ 2026-11-16 16:09 UTC (permalink / raw)
To: dev; +Cc: Bruce Richardson
Rather than using malloc and free for adminq response buffers, and for
the DDP profile list buffer in testpmd commands, define them on the
stack, using RTE_DEFINE_FLEX_BYTES for those structures ending in a
variable-length array. This removes the allocation failure and cleanup
paths.
Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
---
drivers/net/intel/i40e/i40e_ethdev.c | 27 +++++----------------------
drivers/net/intel/i40e/i40e_testpmd.c | 10 ++--------
2 files changed, 7 insertions(+), 30 deletions(-)
diff --git a/drivers/net/intel/i40e/i40e_ethdev.c b/drivers/net/intel/i40e/i40e_ethdev.c
index 498c5c7ec4..dde1867345 100644
--- a/drivers/net/intel/i40e/i40e_ethdev.c
+++ b/drivers/net/intel/i40e/i40e_ethdev.c
@@ -4549,18 +4549,13 @@ i40e_free_virt_mem_d(__rte_unused struct i40e_hw *hw,
static int
i40e_get_cap(struct i40e_hw *hw)
{
- struct i40e_aqc_list_capabilities_element_resp *buf;
+ struct i40e_aqc_list_capabilities_element_resp buf[I40E_MAX_CAP_ELE_NUM] = {0};
uint16_t len, size = 0;
int ret;
/* Calculate a huge enough buff for saving response data temporarily */
len = sizeof(struct i40e_aqc_list_capabilities_element_resp) *
I40E_MAX_CAP_ELE_NUM;
- buf = rte_zmalloc("i40e", len, 0);
- if (!buf) {
- PMD_DRV_LOG(ERR, "Failed to allocate memory");
- return I40E_ERR_NO_MEMORY;
- }
/* Get, parse the capabilities and save it to hw */
ret = i40e_aq_discover_capabilities(hw, buf, len, &size,
@@ -4568,9 +4563,6 @@ i40e_get_cap(struct i40e_hw *hw)
if (ret != I40E_SUCCESS)
PMD_DRV_LOG(ERR, "Failed to discover capabilities");
- /* Free the temporary buffer after being used */
- rte_free(buf);
-
return ret;
}
@@ -4775,29 +4767,23 @@ static int
i40e_pf_get_switch_config(struct i40e_pf *pf)
{
struct i40e_hw *hw = I40E_PF_TO_HW(pf);
- struct i40e_aqc_get_switch_config_resp *switch_config;
+ RTE_DEFINE_FLEX_BYTES(struct i40e_aqc_get_switch_config_resp, switch_config,
+ I40E_AQ_LARGE_BUF);
struct i40e_aqc_switch_config_element_resp *element;
uint16_t start_seid = 0, num_reported;
int ret;
- switch_config = (struct i40e_aqc_get_switch_config_resp *)\
- rte_zmalloc("i40e", I40E_AQ_LARGE_BUF, 0);
- if (!switch_config) {
- PMD_DRV_LOG(ERR, "Failed to allocated memory");
- return -ENOMEM;
- }
-
/* Get the switch configurations */
ret = i40e_aq_get_switch_config(hw, switch_config,
I40E_AQ_LARGE_BUF, &start_seid, NULL);
if (ret != I40E_SUCCESS) {
PMD_DRV_LOG(ERR, "Failed to get switch configurations");
- goto fail;
+ return ret;
}
num_reported = rte_le_to_cpu_16(switch_config->header.num_reported);
if (num_reported != 1) { /* The number should be 1 */
PMD_DRV_LOG(ERR, "Wrong number of switch config reported");
- goto fail;
+ return ret;
}
/* Parse the switch configuration elements */
@@ -4808,9 +4794,6 @@ i40e_pf_get_switch_config(struct i40e_pf *pf)
} else
PMD_DRV_LOG(INFO, "Unknown element type");
-fail:
- rte_free(switch_config);
-
return ret;
}
diff --git a/drivers/net/intel/i40e/i40e_testpmd.c b/drivers/net/intel/i40e/i40e_testpmd.c
index 21f596297b..30ae57336e 100644
--- a/drivers/net/intel/i40e/i40e_testpmd.c
+++ b/drivers/net/intel/i40e/i40e_testpmd.c
@@ -1616,7 +1616,8 @@ cmd_ddp_get_list_parsed(__rte_unused void *parsed_result,
__rte_unused void *data)
{
struct cmd_ddp_get_list_result *res = parsed_result;
- struct rte_pmd_i40e_profile_list *p_list;
+ RTE_DEFINE_FLEX_BYTES(struct rte_pmd_i40e_profile_list, p_list,
+ PROFILE_INFO_SIZE * MAX_PROFILE_NUM + 4);
struct rte_pmd_i40e_profile_info *p_info;
uint32_t p_num;
uint32_t size;
@@ -1624,11 +1625,6 @@ cmd_ddp_get_list_parsed(__rte_unused void *parsed_result,
int ret = -ENOTSUP;
size = PROFILE_INFO_SIZE * MAX_PROFILE_NUM + 4;
- p_list = (struct rte_pmd_i40e_profile_list *)malloc(size);
- if (!p_list) {
- fprintf(stderr, "%s: Failed to malloc buffer\n", __func__);
- return;
- }
if (ret == -ENOTSUP)
ret = rte_pmd_i40e_get_ddp_list(res->port_id,
@@ -1651,8 +1647,6 @@ cmd_ddp_get_list_parsed(__rte_unused void *parsed_result,
}
}
- free(p_list);
-
if (ret < 0)
fprintf(stderr, "Failed to get ddp list\n");
}
--
2.55.0
^ permalink raw reply related [flat|nested] 7+ messages in thread