[v5,39/52] net/mlx4: replace strerror with reentrant version

Message ID 20241104111037.3632161-40-huangdengdui@huawei.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series replace strerror |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

huangdengdui Nov. 4, 2024, 11:10 a.m. UTC
The function strerror() is insecure in a multi-thread environment.
This patch uses strerror_r() to replace it.

Signed-off-by: Dengdui Huang <huangdengdui@huawei.com>
Acked-by: Chengwen Feng <fengchengwen@huawei.com>
Acked-by: Morten Brørup <mb@smartsharesystems.com>
Acked-by: Huisong Li <lihuisong@huawei.com>
---
 drivers/net/mlx4/mlx4.c     | 27 +++++++++++++++++++++------
 drivers/net/mlx4/mlx4_rxq.c | 14 +++++++++++---
 2 files changed, 32 insertions(+), 9 deletions(-)
  

Patch

diff --git a/drivers/net/mlx4/mlx4.c b/drivers/net/mlx4/mlx4.c
index 9c2872429f..e8872e105c 100644
--- a/drivers/net/mlx4/mlx4.c
+++ b/drivers/net/mlx4/mlx4.c
@@ -247,15 +247,18 @@  static int
 mlx4_dev_configure(struct rte_eth_dev *dev)
 {
 	struct mlx4_priv *priv = dev->data->dev_private;
+	char errmsg[RTE_STRERR_BUFSIZE];
 	struct rte_flow_error error;
 	int ret;
 
 	/* Prepare internal flow rules. */
 	ret = mlx4_flow_sync(priv, &error);
 	if (ret) {
+		if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
 		ERROR("cannot set up internal flow rules (code %d, \"%s\"),"
 		      " flow error type %d, cause %p, message: %s",
-		      -ret, strerror(-ret), error.type, error.cause,
+		      -ret, errmsg, error.type, error.cause,
 		      error.message ? error.message : "(unspecified)");
 		goto exit;
 	}
@@ -291,6 +294,7 @@  static int
 mlx4_dev_start(struct rte_eth_dev *dev)
 {
 	struct mlx4_priv *priv = dev->data->dev_private;
+	char errmsg[RTE_STRERR_BUFSIZE];
 	struct rte_flow_error error;
 	uint16_t i;
 	int ret;
@@ -301,8 +305,10 @@  mlx4_dev_start(struct rte_eth_dev *dev)
 	priv->started = 1;
 	ret = mlx4_rss_init(priv);
 	if (ret) {
+		if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
 		ERROR("%p: cannot initialize RSS resources: %s",
-		      (void *)dev, strerror(-ret));
+		      (void *)dev, errmsg);
 		goto err;
 	}
 #ifdef RTE_LIBRTE_MLX4_DEBUG
@@ -316,10 +322,12 @@  mlx4_dev_start(struct rte_eth_dev *dev)
 	}
 	ret = mlx4_flow_sync(priv, &error);
 	if (ret) {
+		if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
 		ERROR("%p: cannot attach flow rules (code %d, \"%s\"),"
 		      " flow error type %d, cause %p, message: %s",
 		      (void *)dev,
-		      -ret, strerror(-ret), error.type, error.cause,
+		      -ret, errmsg, error.type, error.cause,
 		      error.message ? error.message : "(unspecified)");
 		goto err;
 	}
@@ -788,6 +796,7 @@  mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
 	unsigned int vf;
 	int i;
 	char ifname[IF_NAMESIZE];
+	char errmsg[RTE_STRERR_BUFSIZE];
 
 	(void)pci_drv;
 	err = mlx4_init_once();
@@ -946,7 +955,9 @@  mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
 		err = mlx4_glue->query_port(ctx, port, &port_attr);
 		if (err) {
 			err = ENODEV;
-			ERROR("port query failed: %s", strerror(err));
+			if (strerror_r(err, errmsg, sizeof(errmsg)) != 0)
+				snprintf(errmsg, sizeof(errmsg), "Unknown error %d", err);
+			ERROR("port query failed: %s", errmsg);
 			goto port_error;
 		}
 		if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
@@ -962,8 +973,10 @@  mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
 		/* Make asynchronous FD non-blocking to handle interrupts. */
 		err = mlx4_fd_set_non_blocking(ctx->async_fd);
 		if (err) {
+			if (strerror_r(err, errmsg, sizeof(errmsg)) != 0)
+				snprintf(errmsg, sizeof(errmsg), "Unknown error %d", err);
 			ERROR("cannot make asynchronous FD non-blocking: %s",
-			      strerror(err));
+			      errmsg);
 			goto port_error;
 		}
 		/* Allocate protection domain. */
@@ -1023,8 +1036,10 @@  mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
 		/* Configure the first MAC address by default. */
 		err = mlx4_get_mac(priv, &mac.addr_bytes);
 		if (err) {
+			if (strerror_r(err, errmsg, sizeof(errmsg)) != 0)
+				snprintf(errmsg, sizeof(errmsg), "Unknown error %d", err);
 			ERROR("cannot get MAC address, is mlx4_en loaded?"
-			      " (error: %s)", strerror(err));
+			      " (error: %s)", errmsg);
 			goto port_error;
 		}
 		INFO("port %u MAC address is " RTE_ETHER_ADDR_PRT_FMT,
diff --git a/drivers/net/mlx4/mlx4_rxq.c b/drivers/net/mlx4/mlx4_rxq.c
index 781ee256df..d147bcad26 100644
--- a/drivers/net/mlx4/mlx4_rxq.c
+++ b/drivers/net/mlx4/mlx4_rxq.c
@@ -332,6 +332,7 @@  mlx4_rss_init(struct mlx4_priv *priv)
 {
 	struct rte_eth_dev *dev = ETH_DEV(priv);
 	uint8_t log2_range = rte_log2_u32(dev->data->nb_rx_queues);
+	char errmsg[RTE_STRERR_BUFSIZE];
 	uint32_t wq_num_prev = 0;
 	const char *msg;
 	unsigned int i;
@@ -351,9 +352,11 @@  mlx4_rss_init(struct mlx4_priv *priv)
 		 MLX4DV_SET_CTX_ATTR_LOG_WQS_RANGE_SZ,
 		 &log2_range);
 	if (ret) {
+		if (strerror_r(ret, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", ret);
 		ERROR("cannot set up range size for RSS context to %u"
 		      " (for %u Rx queues), error: %s",
-		      1 << log2_range, dev->data->nb_rx_queues, strerror(ret));
+		      1 << log2_range, dev->data->nb_rx_queues, errmsg);
 		rte_errno = ret;
 		return -ret;
 	}
@@ -430,8 +433,10 @@  mlx4_rss_init(struct mlx4_priv *priv)
 	priv->rss_init = 1;
 	return 0;
 error:
+	if (strerror_r(ret, errmsg, sizeof(errmsg)) != 0)
+		snprintf(errmsg, sizeof(errmsg), "Unknown error %d", ret);
 	ERROR("cannot initialize common RSS resources (queue %u): %s: %s",
-	      i, msg, strerror(ret));
+	      i, msg, errmsg);
 	while (i--) {
 		struct rxq *rxq = ETH_DEV(priv)->data->rx_queues[i];
 
@@ -508,6 +513,7 @@  mlx4_rxq_attach(struct rxq *rxq)
 	uint32_t create_flags = 0;
 	uint32_t comp_mask = 0;
 	volatile struct mlx4_wqe_data_seg (*wqes)[];
+	char errmsg[RTE_STRERR_BUFSIZE];
 	unsigned int i;
 	int ret;
 
@@ -630,8 +636,10 @@  mlx4_rxq_attach(struct rxq *rxq)
 		claim_zero(mlx4_glue->destroy_cq(cq));
 	--rxq->usecnt;
 	rte_errno = ret;
+	if (strerror_r(ret, errmsg, sizeof(errmsg)) != 0)
+		snprintf(errmsg, sizeof(errmsg), "Unknown error %d", ret);
 	ERROR("error while attaching Rx queue %p: %s: %s",
-	      (void *)rxq, msg, strerror(ret));
+	      (void *)rxq, msg, errmsg);
 	priv->verbs_alloc_ctx.type = MLX4_VERBS_ALLOC_TYPE_NONE;
 	return -ret;
 }