From 9e9fa80269c4149159457c04714a91d7ae1bcef2 Mon Sep 17 00:00:00 2001 From: David Marchand Date: Tue, 6 Jul 2021 10:57:50 +0200 Subject: [PATCH] drivers/net: fix memzone allocations for DMA memory [ upstream commit d07fc02fb4b38c0dded75d2d21b976d68b494f0e ] Caught by code review. Using a random name for memzone allocations can result in init failures in the unlikely case that a name collision occurs. Use a simple sequential generator on 64 bits. Fixes: 3f50f072ff06 ("i40e: fix memzone freeing") Fixes: 22b123a36d07 ("net/avf: initialize PMD") Fixes: 5f0978e96220 ("net/ice/base: add OS specific implementation") Fixes: 737f30e1c3ab ("net/hns3: support command interface with firmware") Signed-off-by: David Marchand Acked-by: Min Hu (Connor) Acked-by: Haiyue Wang --- drivers/net/hns3/hns3_cmd.c | 4 +++- drivers/net/i40e/i40e_ethdev.c | 4 +++- drivers/net/iavf/iavf_ethdev.c | 4 +++- drivers/net/ice/base/ice_osdep.h | 5 +++-- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/drivers/net/hns3/hns3_cmd.c b/drivers/net/hns3/hns3_cmd.c index ef03d31b22..07a3185f6b 100644 --- a/drivers/net/hns3/hns3_cmd.c +++ b/drivers/net/hns3/hns3_cmd.c @@ -59,10 +59,12 @@ static int hns3_allocate_dma_mem(struct hns3_hw *hw, struct hns3_cmq_ring *ring, uint64_t size, uint32_t alignment) { + static uint64_t hns3_dma_memzone_id; const struct rte_memzone *mz = NULL; char z_name[RTE_MEMZONE_NAMESIZE]; - snprintf(z_name, sizeof(z_name), "hns3_dma_%" PRIu64, rte_rand()); + snprintf(z_name, sizeof(z_name), "hns3_dma_%" PRIu64, + __atomic_fetch_add(&hns3_dma_memzone_id, 1, __ATOMIC_RELAXED)); mz = rte_memzone_reserve_bounded(z_name, size, SOCKET_ID_ANY, RTE_MEMZONE_IOVA_CONTIG, alignment, RTE_PGSIZE_2M); diff --git a/drivers/net/i40e/i40e_ethdev.c b/drivers/net/i40e/i40e_ethdev.c index d825ef7296..e057f16e46 100644 --- a/drivers/net/i40e/i40e_ethdev.c +++ b/drivers/net/i40e/i40e_ethdev.c @@ -4604,13 +4604,15 @@ i40e_allocate_dma_mem_d(__attribute__((unused)) struct i40e_hw *hw, u64 size, u32 alignment) { + static uint64_t i40e_dma_memzone_id; const struct rte_memzone *mz = NULL; char z_name[RTE_MEMZONE_NAMESIZE]; if (!mem) return I40E_ERR_PARAM; - snprintf(z_name, sizeof(z_name), "i40e_dma_%"PRIu64, rte_rand()); + snprintf(z_name, sizeof(z_name), "i40e_dma_%" PRIu64, + __atomic_fetch_add(&i40e_dma_memzone_id, 1, __ATOMIC_RELAXED)); mz = rte_memzone_reserve_bounded(z_name, size, SOCKET_ID_ANY, RTE_MEMZONE_IOVA_CONTIG, alignment, RTE_PGSIZE_2M); if (!mz) diff --git a/drivers/net/iavf/iavf_ethdev.c b/drivers/net/iavf/iavf_ethdev.c index d7d14ee11a..7d4b6b915c 100644 --- a/drivers/net/iavf/iavf_ethdev.c +++ b/drivers/net/iavf/iavf_ethdev.c @@ -1544,13 +1544,15 @@ iavf_allocate_dma_mem_d(__rte_unused struct iavf_hw *hw, u64 size, u32 alignment) { + static uint64_t iavf_dma_memzone_id; const struct rte_memzone *mz = NULL; char z_name[RTE_MEMZONE_NAMESIZE]; if (!mem) return IAVF_ERR_PARAM; - snprintf(z_name, sizeof(z_name), "iavf_dma_%"PRIu64, rte_rand()); + snprintf(z_name, sizeof(z_name), "iavf_dma_%" PRIu64, + __atomic_fetch_add(&iavf_dma_memzone_id, 1, __ATOMIC_RELAXED)); mz = rte_memzone_reserve_bounded(z_name, size, SOCKET_ID_ANY, RTE_MEMZONE_IOVA_CONTIG, alignment, RTE_PGSIZE_2M); if (!mz) diff --git a/drivers/net/ice/base/ice_osdep.h b/drivers/net/ice/base/ice_osdep.h index ba0c57e1a9..5970c918d1 100644 --- a/drivers/net/ice/base/ice_osdep.h +++ b/drivers/net/ice/base/ice_osdep.h @@ -21,7 +21,6 @@ #include #include #include -#include #include #include "ice_alloc.h" @@ -241,13 +240,15 @@ static inline void * ice_alloc_dma_mem(__attribute__((unused)) struct ice_hw *hw, struct ice_dma_mem *mem, u64 size) { + static uint64_t ice_dma_memzone_id; const struct rte_memzone *mz = NULL; char z_name[RTE_MEMZONE_NAMESIZE]; if (!mem) return NULL; - snprintf(z_name, sizeof(z_name), "ice_dma_%"PRIu64, rte_rand()); + snprintf(z_name, sizeof(z_name), "ice_dma_%" PRIu64, + __atomic_fetch_add(&ice_dma_memzone_id, 1, __ATOMIC_RELAXED)); mz = rte_memzone_reserve_bounded(z_name, size, SOCKET_ID_ANY, 0, 0, RTE_PGSIZE_2M); if (!mz)