[v5,12/25] eventdev: replace strtok with reentrant version

Message ID 20241108110404.18317-13-haijie1@huawei.com (mailing list archive)
State Changes Requested, archived
Delegated to: David Marchand
Headers
Series [v5,01/25] app/graph: replace strtok with reentrant version |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Jie Hai Nov. 8, 2024, 11:03 a.m. UTC
Multiple threads calling the same function may cause condition
race issues, which often leads to abnormal behavior and can cause
more serious vulnerabilities such as abnormal termination, denial
of service, and compromised data integrity.

The strtok() is non-reentrant, it is better to replace it with a
reentrant version.

Signed-off-by: Jie Hai <haijie1@huawei.com>
Acked-by: Chengwen Feng <fengchengwen@huawei.com>
Acked-by: Morten Brørup <mb@smartsharesystems.com>
---
 lib/eventdev/rte_event_eth_rx_adapter.c | 39 +++++++++++++------------
 lib/eventdev/rte_eventdev.c             | 18 ++++++------
 2 files changed, 29 insertions(+), 28 deletions(-)
  

Patch

diff --git a/lib/eventdev/rte_event_eth_rx_adapter.c b/lib/eventdev/rte_event_eth_rx_adapter.c
index 3ee20d95f372..96af9c0c8b65 100644
--- a/lib/eventdev/rte_event_eth_rx_adapter.c
+++ b/lib/eventdev/rte_event_eth_rx_adapter.c
@@ -23,6 +23,7 @@ 
 #include <rte_interrupts.h>
 #include <rte_mbuf_dyn.h>
 #include <rte_telemetry.h>
+#include <rte_os_shim.h>
 
 #include "rte_eventdev.h"
 #include "eventdev_pmd.h"
@@ -3651,7 +3652,7 @@  handle_rxa_get_queue_conf(const char *cmd __rte_unused,
 	uint8_t rx_adapter_id;
 	uint16_t rx_queue_id;
 	int eth_dev_id, ret = -1;
-	char *token, *l_params;
+	char *token, *l_params, *sp;
 	struct rte_event_eth_rx_adapter_queue_conf queue_conf;
 
 	if (params == NULL || strlen(params) == 0 || !isdigit(*params))
@@ -3661,19 +3662,19 @@  handle_rxa_get_queue_conf(const char *cmd __rte_unused,
 	l_params = strdup(params);
 	if (l_params == NULL)
 		return -ENOMEM;
-	token = strtok(l_params, ",");
+	token = strtok_r(l_params, ",", &sp);
 	RTE_EVENT_ETH_RX_ADAPTER_TOKEN_VALID_OR_GOTO_ERR_RET(token, -1);
 	rx_adapter_id = strtoul(token, NULL, 10);
 	RTE_EVENT_ETH_RX_ADAPTER_ID_VALID_OR_GOTO_ERR_RET(rx_adapter_id, -EINVAL);
 
-	token = strtok(NULL, ",");
+	token = strtok_r(NULL, ",", &sp);
 	RTE_EVENT_ETH_RX_ADAPTER_TOKEN_VALID_OR_GOTO_ERR_RET(token, -1);
 
 	/* Get device ID from parameter string */
 	eth_dev_id = strtoul(token, NULL, 10);
 	RTE_EVENT_ETH_RX_ADAPTER_PORTID_VALID_OR_GOTO_ERR_RET(eth_dev_id, -EINVAL);
 
-	token = strtok(NULL, ",");
+	token = strtok_r(NULL, ",", &sp);
 	RTE_EVENT_ETH_RX_ADAPTER_TOKEN_VALID_OR_GOTO_ERR_RET(token, -1);
 
 	/* Get Rx queue ID from parameter string */
@@ -3684,7 +3685,7 @@  handle_rxa_get_queue_conf(const char *cmd __rte_unused,
 		goto error;
 	}
 
-	token = strtok(NULL, "\0");
+	token = strtok_r(NULL, "\0", &sp);
 	if (token != NULL)
 		RTE_EDEV_LOG_ERR("Extra parameters passed to eventdev"
 				 " telemetry command, ignoring");
@@ -3723,7 +3724,7 @@  handle_rxa_get_queue_stats(const char *cmd __rte_unused,
 	uint8_t rx_adapter_id;
 	uint16_t rx_queue_id;
 	int eth_dev_id, ret = -1;
-	char *token, *l_params;
+	char *token, *l_params, *sp = NULL;
 	struct rte_event_eth_rx_adapter_queue_stats q_stats;
 
 	if (params == NULL || strlen(params) == 0 || !isdigit(*params))
@@ -3733,19 +3734,19 @@  handle_rxa_get_queue_stats(const char *cmd __rte_unused,
 	l_params = strdup(params);
 	if (l_params == NULL)
 		return -ENOMEM;
-	token = strtok(l_params, ",");
+	token = strtok_r(l_params, ",", &sp);
 	RTE_EVENT_ETH_RX_ADAPTER_TOKEN_VALID_OR_GOTO_ERR_RET(token, -1);
 	rx_adapter_id = strtoul(token, NULL, 10);
 	RTE_EVENT_ETH_RX_ADAPTER_ID_VALID_OR_GOTO_ERR_RET(rx_adapter_id, -EINVAL);
 
-	token = strtok(NULL, ",");
+	token = strtok_r(NULL, ",", &sp);
 	RTE_EVENT_ETH_RX_ADAPTER_TOKEN_VALID_OR_GOTO_ERR_RET(token, -1);
 
 	/* Get device ID from parameter string */
 	eth_dev_id = strtoul(token, NULL, 10);
 	RTE_EVENT_ETH_RX_ADAPTER_PORTID_VALID_OR_GOTO_ERR_RET(eth_dev_id, -EINVAL);
 
-	token = strtok(NULL, ",");
+	token = strtok_r(NULL, ",", &sp);
 	RTE_EVENT_ETH_RX_ADAPTER_TOKEN_VALID_OR_GOTO_ERR_RET(token, -1);
 
 	/* Get Rx queue ID from parameter string */
@@ -3756,7 +3757,7 @@  handle_rxa_get_queue_stats(const char *cmd __rte_unused,
 		goto error;
 	}
 
-	token = strtok(NULL, "\0");
+	token = strtok_r(NULL, "\0", &sp);
 	if (token != NULL)
 		RTE_EDEV_LOG_ERR("Extra parameters passed to eventdev"
 				 " telemetry command, ignoring");
@@ -3794,7 +3795,7 @@  handle_rxa_queue_stats_reset(const char *cmd __rte_unused,
 	uint8_t rx_adapter_id;
 	uint16_t rx_queue_id;
 	int eth_dev_id, ret = -1;
-	char *token, *l_params;
+	char *token, *l_params, *sp = NULL;
 
 	if (params == NULL || strlen(params) == 0 || !isdigit(*params))
 		return -1;
@@ -3803,19 +3804,19 @@  handle_rxa_queue_stats_reset(const char *cmd __rte_unused,
 	l_params = strdup(params);
 	if (l_params == NULL)
 		return -ENOMEM;
-	token = strtok(l_params, ",");
+	token = strtok_r(l_params, ",", &sp);
 	RTE_EVENT_ETH_RX_ADAPTER_TOKEN_VALID_OR_GOTO_ERR_RET(token, -1);
 	rx_adapter_id = strtoul(token, NULL, 10);
 	RTE_EVENT_ETH_RX_ADAPTER_ID_VALID_OR_GOTO_ERR_RET(rx_adapter_id, -EINVAL);
 
-	token = strtok(NULL, ",");
+	token = strtok_r(NULL, ",", &sp);
 	RTE_EVENT_ETH_RX_ADAPTER_TOKEN_VALID_OR_GOTO_ERR_RET(token, -1);
 
 	/* Get device ID from parameter string */
 	eth_dev_id = strtoul(token, NULL, 10);
 	RTE_EVENT_ETH_RX_ADAPTER_PORTID_VALID_OR_GOTO_ERR_RET(eth_dev_id, -EINVAL);
 
-	token = strtok(NULL, ",");
+	token = strtok_r(NULL, ",", &sp);
 	RTE_EVENT_ETH_RX_ADAPTER_TOKEN_VALID_OR_GOTO_ERR_RET(token, -1);
 
 	/* Get Rx queue ID from parameter string */
@@ -3826,7 +3827,7 @@  handle_rxa_queue_stats_reset(const char *cmd __rte_unused,
 		goto error;
 	}
 
-	token = strtok(NULL, "\0");
+	token = strtok_r(NULL, "\0", &sp);
 	if (token != NULL)
 		RTE_EDEV_LOG_ERR("Extra parameters passed to eventdev"
 				 " telemetry command, ignoring");
@@ -3855,7 +3856,7 @@  handle_rxa_instance_get(const char *cmd __rte_unused,
 	uint8_t instance_id;
 	uint16_t rx_queue_id;
 	int eth_dev_id, ret = -1;
-	char *token, *l_params;
+	char *token, *l_params, *sp = NULL;
 
 	if (params == NULL || strlen(params) == 0 || !isdigit(*params))
 		return -1;
@@ -3863,14 +3864,14 @@  handle_rxa_instance_get(const char *cmd __rte_unused,
 	l_params = strdup(params);
 	if (l_params == NULL)
 		return -ENOMEM;
-	token = strtok(l_params, ",");
+	token = strtok_r(l_params, ",", &sp);
 	RTE_EVENT_ETH_RX_ADAPTER_TOKEN_VALID_OR_GOTO_ERR_RET(token, -1);
 
 	/* Get device ID from parameter string */
 	eth_dev_id = strtoul(token, NULL, 10);
 	RTE_EVENT_ETH_RX_ADAPTER_PORTID_VALID_OR_GOTO_ERR_RET(eth_dev_id, -EINVAL);
 
-	token = strtok(NULL, ",");
+	token = strtok_r(NULL, ",", &sp);
 	RTE_EVENT_ETH_RX_ADAPTER_TOKEN_VALID_OR_GOTO_ERR_RET(token, -1);
 
 	/* Get Rx queue ID from parameter string */
@@ -3881,7 +3882,7 @@  handle_rxa_instance_get(const char *cmd __rte_unused,
 		goto error;
 	}
 
-	token = strtok(NULL, "\0");
+	token = strtok_r(NULL, "\0", &sp);
 	if (token != NULL)
 		RTE_EDEV_LOG_ERR("Extra parameters passed to eventdev"
 				 " telemetry command, ignoring");
diff --git a/lib/eventdev/rte_eventdev.c b/lib/eventdev/rte_eventdev.c
index ca295c87c47d..d3dacffdfab2 100644
--- a/lib/eventdev/rte_eventdev.c
+++ b/lib/eventdev/rte_eventdev.c
@@ -1787,7 +1787,7 @@  handle_queue_links(const char *cmd __rte_unused,
 		   struct rte_tel_data *d)
 {
 	int i, ret, port_id = 0;
-	char *end_param;
+	char *end_param, *sp = NULL;
 	uint8_t dev_id;
 	uint8_t queues[RTE_EVENT_MAX_QUEUES_PER_DEV];
 	uint8_t priorities[RTE_EVENT_MAX_QUEUES_PER_DEV];
@@ -1800,12 +1800,12 @@  handle_queue_links(const char *cmd __rte_unused,
 	dev_id = strtoul(params, &end_param, 10);
 	RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
 
-	p_param = strtok(end_param, ",");
+	p_param = strtok_r(end_param, ",", &sp);
 	if (p_param == NULL || strlen(p_param) == 0 || !isdigit(*p_param))
 		return -1;
 
 	port_id = strtoul(p_param, &end_param, 10);
-	p_param = strtok(NULL, "\0");
+	p_param = strtok_r(NULL, "\0", &sp);
 	if (p_param != NULL)
 		RTE_EDEV_LOG_DEBUG(
 			"Extra parameters passed to eventdev telemetry command, ignoring");
@@ -1925,7 +1925,7 @@  handle_port_xstats(const char *cmd __rte_unused,
 	int dev_id;
 	int port_queue_id = 0;
 	enum rte_event_dev_xstats_mode mode;
-	char *end_param;
+	char *end_param, *sp = NULL;
 	const char *p_param;
 
 	if (params == NULL || strlen(params) == 0 || !isdigit(*params))
@@ -1935,7 +1935,7 @@  handle_port_xstats(const char *cmd __rte_unused,
 	dev_id = strtoul(params, &end_param, 10);
 	RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
 
-	p_param = strtok(end_param, ",");
+	p_param = strtok_r(end_param, ",", &sp);
 	mode = RTE_EVENT_DEV_XSTATS_PORT;
 
 	if (p_param == NULL || strlen(p_param) == 0 || !isdigit(*p_param))
@@ -1943,7 +1943,7 @@  handle_port_xstats(const char *cmd __rte_unused,
 
 	port_queue_id = strtoul(p_param, &end_param, 10);
 
-	p_param = strtok(NULL, "\0");
+	p_param = strtok_r(NULL, "\0", &sp);
 	if (p_param != NULL)
 		RTE_EDEV_LOG_DEBUG(
 			"Extra parameters passed to eventdev telemetry command, ignoring");
@@ -1959,7 +1959,7 @@  handle_queue_xstats(const char *cmd __rte_unused,
 	int dev_id;
 	int port_queue_id = 0;
 	enum rte_event_dev_xstats_mode mode;
-	char *end_param;
+	char *end_param, *sp = NULL;
 	const char *p_param;
 
 	if (params == NULL || strlen(params) == 0 || !isdigit(*params))
@@ -1969,7 +1969,7 @@  handle_queue_xstats(const char *cmd __rte_unused,
 	dev_id = strtoul(params, &end_param, 10);
 	RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
 
-	p_param = strtok(end_param, ",");
+	p_param = strtok_r(end_param, ",", &sp);
 	mode = RTE_EVENT_DEV_XSTATS_QUEUE;
 
 	if (p_param == NULL || strlen(p_param) == 0 || !isdigit(*p_param))
@@ -1977,7 +1977,7 @@  handle_queue_xstats(const char *cmd __rte_unused,
 
 	port_queue_id = strtoul(p_param, &end_param, 10);
 
-	p_param = strtok(NULL, "\0");
+	p_param = strtok_r(NULL, "\0", &sp);
 	if (p_param != NULL)
 		RTE_EDEV_LOG_DEBUG(
 			"Extra parameters passed to eventdev telemetry command, ignoring");