From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id C410045C76; Mon, 4 Nov 2024 12:12:41 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 538EA410D2; Mon, 4 Nov 2024 12:11:03 +0100 (CET) Received: from szxga06-in.huawei.com (szxga06-in.huawei.com [45.249.212.32]) by mails.dpdk.org (Postfix) with ESMTP id E887740E24 for ; Mon, 4 Nov 2024 12:10:41 +0100 (CET) Received: from mail.maildlp.com (unknown [172.19.88.163]) by szxga06-in.huawei.com (SkyGuard) with ESMTP id 4Xhpfy0Yjjz1ypCX; Mon, 4 Nov 2024 19:10:50 +0800 (CST) Received: from dggpeml500011.china.huawei.com (unknown [7.185.36.84]) by mail.maildlp.com (Postfix) with ESMTPS id 5FB01180019; Mon, 4 Nov 2024 19:10:40 +0800 (CST) Received: from localhost.huawei.com (10.50.165.33) by dggpeml500011.china.huawei.com (7.185.36.84) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.39; Mon, 4 Nov 2024 19:10:40 +0800 From: Dengdui Huang To: CC: , , , , , , , , , , Subject: [PATCH v5 12/52] bus/cdx: replace strerror with reentrant version Date: Mon, 4 Nov 2024 19:09:57 +0800 Message-ID: <20241104111037.3632161-13-huangdengdui@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20241104111037.3632161-1-huangdengdui@huawei.com> References: <20231114082539.1858594-1-huangdengdui@huawei.com> <20241104111037.3632161-1-huangdengdui@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-Originating-IP: [10.50.165.33] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpeml500011.china.huawei.com (7.185.36.84) X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org The function strerror() is insecure in a multi-thread environment. This patch uses strerror_r() to replace it. Signed-off-by: Dengdui Huang Acked-by: Chengwen Feng Acked-by: Morten Brørup Acked-by: Huisong Li --- drivers/bus/cdx/cdx.c | 6 ++-- drivers/bus/cdx/cdx_vfio.c | 58 +++++++++++++++++++++++++++++--------- 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/drivers/bus/cdx/cdx.c b/drivers/bus/cdx/cdx.c index 62b108e082..3e8ac81766 100644 --- a/drivers/bus/cdx/cdx.c +++ b/drivers/bus/cdx/cdx.c @@ -264,14 +264,16 @@ cdx_scan_one(const char *dirname, const char *dev_name) static int cdx_scan(void) { + char errmsg[RTE_STRERR_BUFSIZE]; struct dirent *e; DIR *dir; char dirname[PATH_MAX]; dir = opendir(RTE_CDX_BUS_DEVICES_PATH); if (dir == NULL) { - CDX_BUS_INFO("%s(): opendir failed: %s", __func__, - strerror(errno)); + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); + CDX_BUS_INFO("%s(): opendir failed: %s", __func__, errmsg); return 0; } diff --git a/drivers/bus/cdx/cdx_vfio.c b/drivers/bus/cdx/cdx_vfio.c index 664f267471..a17da30701 100644 --- a/drivers/bus/cdx/cdx_vfio.c +++ b/drivers/bus/cdx/cdx_vfio.c @@ -20,6 +20,7 @@ #include #include #include +#include #include "bus_cdx_driver.h" #include "cdx_logs.h" @@ -186,6 +187,7 @@ static int cdx_vfio_setup_interrupts(struct rte_cdx_device *dev, int vfio_dev_fd, int num_irqs) { + char errmsg[RTE_STRERR_BUFSIZE]; int i, ret; if (rte_intr_dev_fd_set(dev->intr_handle, vfio_dev_fd)) @@ -203,8 +205,10 @@ cdx_vfio_setup_interrupts(struct rte_cdx_device *dev, int vfio_dev_fd, ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_IRQ_INFO, &irq); if (ret < 0) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); CDX_BUS_ERR("Cannot get VFIO IRQ info, error %i (%s)", - errno, strerror(errno)); + errno, errmsg); return -1; } @@ -221,8 +225,10 @@ cdx_vfio_setup_interrupts(struct rte_cdx_device *dev, int vfio_dev_fd, /* set up an eventfd for interrupts */ fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); if (fd < 0) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); CDX_BUS_ERR("Cannot set up eventfd, error %i (%s)", - errno, strerror(errno)); + errno, errmsg); return -1; } @@ -244,6 +250,8 @@ static int cdx_vfio_setup_device(struct rte_cdx_device *dev, int vfio_dev_fd, int num_irqs) { + char errmsg[RTE_STRERR_BUFSIZE]; + if (cdx_vfio_setup_interrupts(dev, vfio_dev_fd, num_irqs) != 0) { CDX_BUS_ERR("Error setting up interrupts!"); return -1; @@ -254,8 +262,10 @@ cdx_vfio_setup_device(struct rte_cdx_device *dev, int vfio_dev_fd, * then it updates errno as EINVAL. */ if (ioctl(vfio_dev_fd, VFIO_DEVICE_RESET) && errno != EINVAL) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); CDX_BUS_ERR("Unable to reset device! Error: %d (%s)", errno, - strerror(errno)); + errmsg); return -1; } @@ -264,8 +274,10 @@ cdx_vfio_setup_device(struct rte_cdx_device *dev, int vfio_dev_fd, * device does not support configuring bus master. */ if (rte_cdx_vfio_bm_enable(dev) && (errno != -ENOTTY)) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); CDX_BUS_ERR("Bus master enable failure! Error: %d (%s)", errno, - strerror(errno)); + errmsg); return -1; } @@ -393,6 +405,7 @@ cdx_vfio_map_resource_primary(struct rte_cdx_device *dev) const char *dev_name = dev->device.name; struct cdx_map *maps; int vfio_dev_fd, i, ret; + char errmsg[RTE_STRERR_BUFSIZE]; if (rte_intr_fd_set(dev->intr_handle, -1)) return -1; @@ -422,8 +435,10 @@ cdx_vfio_map_resource_primary(struct rte_cdx_device *dev) ret = cdx_vfio_get_region_info(vfio_dev_fd, ®, i); if (ret < 0) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); CDX_BUS_ERR("%s cannot get device region info error %i (%s)", - dev_name, errno, strerror(errno)); + dev_name, errno, errmsg); goto err_vfio_res; } @@ -450,8 +465,10 @@ cdx_vfio_map_resource_primary(struct rte_cdx_device *dev) ret = cdx_vfio_mmap_resource(vfio_dev_fd, vfio_res, i, 0); if (ret < 0) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); CDX_BUS_ERR("%s mapping region %i failed: %s", - cdx_addr, i, strerror(errno)); + cdx_addr, i, errmsg); free(reg); goto err_vfio_res; } @@ -489,6 +506,7 @@ cdx_vfio_map_resource_secondary(struct rte_cdx_device *dev) struct mapped_cdx_res_list *vfio_res_list = RTE_TAILQ_CAST(cdx_vfio_tailq.head, mapped_cdx_res_list); const char *dev_name = dev->device.name; + char errmsg[RTE_STRERR_BUFSIZE]; struct cdx_map *maps; if (rte_intr_fd_set(dev->intr_handle, -1)) @@ -518,8 +536,10 @@ cdx_vfio_map_resource_secondary(struct rte_cdx_device *dev) for (i = 0; i < vfio_res->nb_maps; i++) { ret = cdx_vfio_mmap_resource(vfio_dev_fd, vfio_res, i, MAP_FIXED); if (ret < 0) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); CDX_BUS_ERR("%s mapping MMIO region %i failed: %s", - dev_name, i, strerror(errno)); + dev_name, i, errmsg); goto err_vfio_dev_fd; } @@ -617,6 +637,7 @@ rte_cdx_vfio_bm_enable(struct rte_cdx_device *dev) struct vfio_device_info device_info = { .argsz = sizeof(device_info) }; struct vfio_device_feature_bus_master *vfio_bm_feature; struct vfio_device_feature *feature; + char errmsg[RTE_STRERR_BUFSIZE]; int vfio_dev_fd, ret; size_t argsz; @@ -638,8 +659,10 @@ rte_cdx_vfio_bm_enable(struct rte_cdx_device *dev) feature->flags |= VFIO_DEVICE_FEATURE_SET; ret = ioctl(vfio_dev_fd, RTE_VFIO_DEVICE_FEATURE, feature); if (ret) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); CDX_BUS_ERR("Bus Master configuring not supported for device: %s, error: %d (%s)", - dev->name, errno, strerror(errno)); + dev->name, errno, errmsg); free(feature); return ret; } @@ -647,9 +670,12 @@ rte_cdx_vfio_bm_enable(struct rte_cdx_device *dev) feature->flags = RTE_VFIO_DEVICE_FEATURE_BUS_MASTER | VFIO_DEVICE_FEATURE_SET; vfio_bm_feature->op = VFIO_DEVICE_FEATURE_SET_MASTER; ret = ioctl(vfio_dev_fd, RTE_VFIO_DEVICE_FEATURE, feature); - if (ret < 0) + if (ret < 0) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); CDX_BUS_ERR("BM Enable Error for device: %s, Error: %d (%s)", - dev->name, errno, strerror(errno)); + dev->name, errno, errmsg); + } free(feature); return ret; @@ -661,6 +687,7 @@ rte_cdx_vfio_bm_disable(struct rte_cdx_device *dev) { struct vfio_device_feature_bus_master *vfio_bm_feature; struct vfio_device_feature *feature; + char errmsg[RTE_STRERR_BUFSIZE]; int vfio_dev_fd, ret; size_t argsz; @@ -682,8 +709,10 @@ rte_cdx_vfio_bm_disable(struct rte_cdx_device *dev) feature->flags |= VFIO_DEVICE_FEATURE_SET; ret = ioctl(vfio_dev_fd, RTE_VFIO_DEVICE_FEATURE, feature); if (ret) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); CDX_BUS_ERR("Bus Master configuring not supported for device: %s, Error: %d (%s)", - dev->name, errno, strerror(errno)); + dev->name, errno, errmsg); free(feature); return ret; } @@ -691,9 +720,12 @@ rte_cdx_vfio_bm_disable(struct rte_cdx_device *dev) feature->flags = RTE_VFIO_DEVICE_FEATURE_BUS_MASTER | VFIO_DEVICE_FEATURE_SET; vfio_bm_feature->op = VFIO_DEVICE_FEATURE_CLEAR_MASTER; ret = ioctl(vfio_dev_fd, RTE_VFIO_DEVICE_FEATURE, feature); - if (ret < 0) + if (ret < 0) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); CDX_BUS_ERR("BM Disable Error for device: %s, Error: %d (%s)", - dev->name, errno, strerror(errno)); + dev->name, errno, errmsg); + } free(feature); return ret; -- 2.33.0