Skip to contentSkip to conversation

drivers: fix memzone allocations for DMA memory

PATCH In review David Marchand 3 participants 5 messages Jul 6, 2021 Network drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drivers: fix memzone allocations for DMA memory

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")
---
 drivers/common/iavf/iavf_impl.c  | 5 +++--
 drivers/net/hns3/hns3_cmd.c      | 4 +++-
 drivers/net/i40e/i40e_ethdev.c   | 4 +++-
 drivers/net/ice/base/ice_osdep.h | 5 +++--
 4 files changed, 12 insertions(+), 6 deletions(-)
drivers/common/iavf/iavf_impl.c +3 -2
index 0c7d5c0dae..8919b0e7c3 100644
@@ -6,7 +6,6 @@
 #include <inttypes.h>
 
 #include <rte_common.h>
-#include <rte_random.h>
 #include <rte_malloc.h>
 #include <rte_memzone.h>
 
@@ -19,13 +18,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);
drivers/net/hns3/hns3_cmd.c +3 -1
index 44a4e2860d..175d48d14b 100644
@@ -44,10 +44,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);
drivers/net/i40e/i40e_ethdev.c +3 -1
index dd61258739..87d5053a24 100644
@@ -4548,13 +4548,15 @@ i40e_allocate_dma_mem_d(__rte_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)
drivers/net/ice/base/ice_osdep.h +3 -2
index 878c5597d4..154fe96e93 100644
@@ -21,7 +21,6 @@
 #include <rte_cycles.h>
 #include <rte_spinlock.h>
 #include <rte_log.h>
-#include <rte_random.h>
 #include <rte_io.h>
 
 #include "ice_alloc.h"
@@ -260,13 +259,15 @@ static inline void *
 ice_alloc_dma_mem(__rte_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)
Signature · 1 line
2.23.0
#1 lore raw

acked.

Quoted 118 lines · David Marchand

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")
---
  drivers/common/iavf/iavf_impl.c  | 5 +++--
  drivers/net/hns3/hns3_cmd.c      | 4 +++-
  drivers/net/i40e/i40e_ethdev.c   | 4 +++-
  drivers/net/ice/base/ice_osdep.h | 5 +++--
  4 files changed, 12 insertions(+), 6 deletions(-)
drivers/common/iavf/iavf_impl.c +3 -2
index 0c7d5c0dae..8919b0e7c3 100644
@@ -6,7 +6,6 @@
  #include <inttypes.h>
 
  #include <rte_common.h>
-#include <rte_random.h>
  #include <rte_malloc.h>
  #include <rte_memzone.h>
 
@@ -19,13 +18,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);
drivers/net/hns3/hns3_cmd.c +3 -1
index 44a4e2860d..175d48d14b 100644
@@ -44,10 +44,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);
drivers/net/i40e/i40e_ethdev.c +3 -1
index dd61258739..87d5053a24 100644
@@ -4548,13 +4548,15 @@ i40e_allocate_dma_mem_d(__rte_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)
drivers/net/ice/base/ice_osdep.h +3 -2
index 878c5597d4..154fe96e93 100644
@@ -21,7 +21,6 @@
  #include <rte_cycles.h>
  #include <rte_spinlock.h>
  #include <rte_log.h>
-#include <rte_random.h>
  #include <rte_io.h>
 
  #include "ice_alloc.h"
@@ -260,13 +259,15 @@ static inline void *
  ice_alloc_dma_mem(__rte_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)
#3 lore raw
Quoted 9 lines · David Marchand
Quoted 2 lines · Min Hu (Connor)

acked.

Thanks for the quick review, just translating this "acked" to:

For patchwork to catch it.

OK, I will do it as you suggest in future.

Quoted 1 line
Acked-by on v1 1/1
#4 lore raw
In reply to David Marchand
Quoted 24 lines

-----Original Message-----
From: stable <[email protected]> On Behalf Of David Marchand
Sent: Tuesday, July 6, 2021 16:58
To: [email protected]
Cc: [email protected]; Wu, Jingjing <[email protected]>; Xing, Beilei <[email protected]>; Min Hu (Connor) <[email protected]>; Yisen Zhuang <[email protected]>; Lijun Ou <[email protected]>; Yang, Qiming <[email protected]>; Zhang, Qi Z <[email protected]>; Zhang, Helin <[email protected]>; Yigit, Ferruh <[email protected]>; Lu, Wenzhuo <[email protected]>; Huisong Li <[email protected]>; Chunsong Feng <[email protected]>; Wei Hu (Xavier) <[email protected]>; Hao Chen <[email protected]>
Subject: [dpdk-stable] [PATCH] drivers: fix memzone allocations for DMA memory

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")
Quoted 8 lines
---
 drivers/common/iavf/iavf_impl.c  | 5 +++--
 drivers/net/hns3/hns3_cmd.c      | 4 +++-
 drivers/net/i40e/i40e_ethdev.c   | 4 +++-
 drivers/net/ice/base/ice_osdep.h | 5 +++--
 4 files changed, 12 insertions(+), 6 deletions(-)
Quoted 2 lines

--
2.23.0