[v5,50/52] vdpa/ifc: replace strerror with reentrant version

Message ID 20241104111037.3632161-51-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/vdpa/ifc/ifcvf_vdpa.c | 57 ++++++++++++++++++++++++++---------
 1 file changed, 42 insertions(+), 15 deletions(-)
  

Patch

diff --git a/drivers/vdpa/ifc/ifcvf_vdpa.c b/drivers/vdpa/ifc/ifcvf_vdpa.c
index 65de383b95..170614d8ce 100644
--- a/drivers/vdpa/ifc/ifcvf_vdpa.c
+++ b/drivers/vdpa/ifc/ifcvf_vdpa.c
@@ -23,6 +23,7 @@ 
 #include <rte_log.h>
 #include <rte_kvargs.h>
 #include <rte_devargs.h>
+#include <rte_errno.h>
 
 #include "base/ifcvf.h"
 
@@ -407,6 +408,7 @@  vdpa_enable_vfio_intr(struct ifcvf_internal *internal, bool m_rx)
 	int ret;
 	uint32_t i, nr_vring;
 	char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
+	char errmsg[RTE_STRERR_BUFSIZE];
 	struct vfio_irq_set *irq_set;
 	int *fd_ptr;
 	struct rte_vhost_vring vring;
@@ -445,8 +447,9 @@  vdpa_enable_vfio_intr(struct ifcvf_internal *internal, bool m_rx)
 			 */
 			fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
 			if (fd < 0) {
-				DRV_LOG(ERR, "can't setup eventfd: %s",
-					strerror(errno));
+				if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+					snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+				DRV_LOG(ERR, "can't setup eventfd: %s", errmsg);
 				return -1;
 			}
 			internal->intr_fd[i] = fd;
@@ -456,8 +459,9 @@  vdpa_enable_vfio_intr(struct ifcvf_internal *internal, bool m_rx)
 
 	ret = ioctl(internal->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
 	if (ret) {
-		DRV_LOG(ERR, "Error enabling MSI-X interrupts: %s",
-				strerror(errno));
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+		DRV_LOG(ERR, "Error enabling MSI-X interrupts: %s", errmsg);
 		return -1;
 	}
 
@@ -470,6 +474,7 @@  vdpa_disable_vfio_intr(struct ifcvf_internal *internal)
 	int ret;
 	uint32_t i, nr_vring;
 	char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
+	char errmsg[RTE_STRERR_BUFSIZE];
 	struct vfio_irq_set *irq_set;
 
 	irq_set = (struct vfio_irq_set *)irq_set_buf;
@@ -488,8 +493,9 @@  vdpa_disable_vfio_intr(struct ifcvf_internal *internal)
 
 	ret = ioctl(internal->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
 	if (ret) {
-		DRV_LOG(ERR, "Error disabling MSI-X interrupts: %s",
-				strerror(errno));
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+		DRV_LOG(ERR, "Error disabling MSI-X interrupts: %s", errmsg);
 		return -1;
 	}
 
@@ -502,6 +508,7 @@  notify_relay(void *arg)
 	int i, kickfd, epfd, nfds = 0;
 	uint32_t qid, q_num;
 	struct epoll_event events[IFCVF_MAX_QUEUES * 2];
+	char errmsg[RTE_STRERR_BUFSIZE];
 	struct epoll_event ev;
 	uint64_t buf;
 	int nbytes;
@@ -526,7 +533,9 @@  notify_relay(void *arg)
 		rte_vhost_get_vhost_vring(internal->vid, qid, &vring);
 		ev.data.u64 = qid | (uint64_t)vring.kickfd << 32;
 		if (epoll_ctl(epfd, EPOLL_CTL_ADD, vring.kickfd, &ev) < 0) {
-			DRV_LOG(ERR, "epoll add error: %s", strerror(errno));
+			if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+				snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+			DRV_LOG(ERR, "epoll add error: %s", errmsg);
 			return 1;
 		}
 	}
@@ -550,9 +559,12 @@  notify_relay(void *arg)
 					    errno == EWOULDBLOCK ||
 					    errno == EAGAIN)
 						continue;
+					if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+						snprintf(errmsg, sizeof(errmsg),
+							 "Unknown error %d", errno);
 					DRV_LOG(INFO, "Error reading "
 						"kickfd: %s",
-						strerror(errno));
+						errmsg);
 				}
 				break;
 			} while (1);
@@ -612,6 +624,7 @@  static uint32_t
 intr_relay(void *arg)
 {
 	struct ifcvf_internal *internal = (struct ifcvf_internal *)arg;
+	char errmsg[RTE_STRERR_BUFSIZE];
 	struct epoll_event csc_event;
 	struct epoll_event ev;
 	uint64_t buf;
@@ -628,7 +641,9 @@  intr_relay(void *arg)
 	ev.data.fd = rte_intr_fd_get(internal->pdev->intr_handle);
 	if (epoll_ctl(csc_epfd, EPOLL_CTL_ADD,
 		rte_intr_fd_get(internal->pdev->intr_handle), &ev) < 0) {
-		DRV_LOG(ERR, "epoll add error: %s", strerror(errno));
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+		DRV_LOG(ERR, "epoll add error: %s", errmsg);
 		goto out;
 	}
 
@@ -651,9 +666,11 @@  intr_relay(void *arg)
 				    errno == EWOULDBLOCK ||
 				    errno == EAGAIN)
 					continue;
+				if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+					snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 				DRV_LOG(ERR, "Error reading from file descriptor %d: %s",
 					csc_event.data.fd,
-					strerror(errno));
+					errmsg);
 				goto out;
 			} else if (nbytes == 0) {
 				DRV_LOG(ERR, "Read nothing from file descriptor %d",
@@ -926,6 +943,7 @@  vring_relay(void *arg)
 	struct rte_vhost_vring vring;
 	uint16_t qid, q_num;
 	struct epoll_event events[IFCVF_MAX_QUEUES * 4];
+	char errmsg[RTE_STRERR_BUFSIZE];
 	struct epoll_event ev;
 	int nbytes;
 	uint64_t buf;
@@ -947,7 +965,9 @@  vring_relay(void *arg)
 		rte_vhost_get_vhost_vring(vid, qid, &vring);
 		ev.data.u64 = qid << 1 | (uint64_t)vring.kickfd << 32;
 		if (epoll_ctl(epfd, EPOLL_CTL_ADD, vring.kickfd, &ev) < 0) {
-			DRV_LOG(ERR, "epoll add error: %s", strerror(errno));
+			if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+				snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+			DRV_LOG(ERR, "epoll add error: %s", errmsg);
 			return 1;
 		}
 	}
@@ -961,7 +981,9 @@  vring_relay(void *arg)
 			(uint64_t)internal->intr_fd[qid] << 32;
 		if (epoll_ctl(epfd, EPOLL_CTL_ADD, internal->intr_fd[qid], &ev)
 				< 0) {
-			DRV_LOG(ERR, "epoll add error: %s", strerror(errno));
+			if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+				snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+			DRV_LOG(ERR, "epoll add error: %s", errmsg);
 			return 1;
 		}
 		update_used_ring(internal, qid);
@@ -990,9 +1012,12 @@  vring_relay(void *arg)
 					    errno == EWOULDBLOCK ||
 					    errno == EAGAIN)
 						continue;
+					if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+						snprintf(errmsg, sizeof(errmsg),
+							 "Unknown error %d", errno);
 					DRV_LOG(INFO, "Error reading "
 						"kickfd: %s",
-						strerror(errno));
+						errmsg);
 				}
 				break;
 			} while (1);
@@ -1250,6 +1275,7 @@  ifcvf_get_notify_area(int vid, int qid, uint64_t *offset, uint64_t *size)
 	struct internal_list *list;
 	struct ifcvf_internal *internal;
 	struct vfio_region_info reg = { .argsz = sizeof(reg) };
+	char errmsg[RTE_STRERR_BUFSIZE];
 	int ret;
 
 	vdev = rte_vhost_get_vdpa_device(vid);
@@ -1264,8 +1290,9 @@  ifcvf_get_notify_area(int vid, int qid, uint64_t *offset, uint64_t *size)
 	reg.index = ifcvf_get_notify_region(&internal->hw);
 	ret = ioctl(internal->vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, &reg);
 	if (ret) {
-		DRV_LOG(ERR, "Get not get device region info: %s",
-				strerror(errno));
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
+		DRV_LOG(ERR, "Get not get device region info: %s", errmsg);
 		return -1;
 	}