Skip to contentSkip to conversation

net/af_xdp: fix umem map size for zero copy

PATCH Superseded Frank Du 4 participants 29 messages Apr 26, 2024 – Jul 6, 2024 Network drivers

lore
Share & tools
lore

Frank Du

author
#0 lore raw

PATCH 1/1 net/af_xdp: fix umem map size for zero copy

The current calculation assumes that the mbufs are contiguous. However, this assumption is incorrect when the memory spans across a huge page. Correct to directly read the size from the mempool memory chunks.

---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +5 -5
index 268a130c49..cb95d17d13 100644
@@ -1039,7 +1039,7 @@ eth_link_update(struct rte_eth_dev *dev __rte_unused,
 }
 
 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
-static inline uintptr_t get_base_addr(struct rte_mempool *mp, uint64_t *align)
+static inline uintptr_t get_memhdr_info(struct rte_mempool *mp, uint64_t *align, size_t *len)
 {
 	struct rte_mempool_memhdr *memhdr;
 	uintptr_t memhdr_addr, aligned_addr;
@@ -1048,6 +1048,7 @@ static inline uintptr_t get_base_addr(struct rte_mempool *mp, uint64_t *align)
 	memhdr_addr = (uintptr_t)memhdr->addr;
 	aligned_addr = memhdr_addr & ~(getpagesize() - 1);
 	*align = memhdr_addr - aligned_addr;
+	*len = memhdr->len;
 
 	return aligned_addr;
 }
@@ -1125,6 +1126,7 @@ xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
 	void *base_addr = NULL;
 	struct rte_mempool *mb_pool = rxq->mb_pool;
 	uint64_t umem_size, align = 0;
+	size_t len = 0;
 
 	if (internals->shared_umem) {
 		if (get_shared_umem(rxq, internals->if_name, &umem) < 0)
@@ -1156,10 +1158,8 @@ xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
 		}
 
 		umem->mb_pool = mb_pool;
-		base_addr = (void *)get_base_addr(mb_pool, &align);
-		umem_size = (uint64_t)mb_pool->populated_size *
-				(uint64_t)usr_config.frame_size +
-				align;
+		base_addr = (void *)get_memhdr_info(mb_pool, &align, &len);
+		umem_size = (uint64_t)len + align;
 
 		ret = xsk_umem__create(&umem->umem, base_addr, umem_size,
 				&rxq->fq, &rxq->cq, &usr_config);
Signature · 1 line
2.34.1
#1 lore raw
Quoted 48 lines

Subject: [PATCH] net/af_xdp: fix umem map size for zero copy

The current calculation assumes that the mbufs are contiguous. However, this assumption is incorrect when the memory spans across a huge page. Correct to directly read the size from the mempool memory chunks.

---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -1
index 268a130c49..cb95d17d13 100644
@@ -1039,7 +1039,7 @@ eth_link_update(struct rte_eth_dev *dev
 __rte_unused,
 }
 
 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
-static inline uintptr_t get_base_addr(struct rte_mempool *mp, uint64_t
 *align)
+static inline uintptr_t get_memhdr_info(struct rte_mempool *mp, uint64_t
 *align, size_t *len)

{

drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
 	struct rte_mempool_memhdr *memhdr;
 	uintptr_t memhdr_addr, aligned_addr;
@@ -1048,6 +1048,7 @@ static inline uintptr_t get_base_addr(struct
 rte_mempool *mp, uint64_t *align)
 	memhdr_addr = (uintptr_t)memhdr->addr;
 	aligned_addr = memhdr_addr & ~(getpagesize() - 1);
 	*align = memhdr_addr - aligned_addr;
+	*len = memhdr->len;
 
 	return aligned_addr;

}

drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
@@ -1125,6 +1126,7 @@ xsk_umem_info *xdp_umem_configure(struct
 pmd_internals *internals,
 	void *base_addr = NULL;
 	struct rte_mempool *mb_pool = rxq->mb_pool;
 	uint64_t umem_size, align = 0;
+	size_t len = 0;
 
 	if (internals->shared_umem) {
drivers/net/af_xdp/rte_eth_af_xdp.c +0 -0
 		if (get_shared_umem(rxq, internals->if_name, &umem) < 0)
@@ -1156,10 +1158,8 @@ xsk_umem_info *xdp_umem_configure(struct
 pmd_internals *internals,

drivers/net/af_xdp/rte_eth_af_xdp.c xsk_umem_info *xdp_umem_configure(struct · line 1164

Quoted 10 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +2 -4
 		}
 
 		umem->mb_pool = mb_pool;
-		base_addr = (void *)get_base_addr(mb_pool, &align);
-		umem_size = (uint64_t)mb_pool->populated_size *
-				(uint64_t)usr_config.frame_size +
-				align;
+		base_addr = (void *)get_memhdr_info(mb_pool, &align,
 &len);
+		umem_size = (uint64_t)len + align;

len is set to the length of the first memhdr of the mempool. There may be many other memhdrs in the mempool. So I don't think this is the correct value to use for calculating the entire umem size.

Quoted 6 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +0 -0
 		ret = xsk_umem__create(&umem->umem, base_addr,

umem_size,
&rxq->fq, &rxq->cq, &usr_config);
--
2.34.1

author
#2 lore raw
Quoted 59 lines

-----Original Message-----
From: Loftus, Ciara <[email protected]>
Sent: Friday, April 26, 2024 6:44 PM
To: Du, Frank <[email protected]>
Cc: [email protected]
Subject: RE: [PATCH] net/af_xdp: fix umem map size for zero copy

Quoted 4 lines

Subject: [PATCH] net/af_xdp: fix umem map size for zero copy

The current calculation assumes that the mbufs are contiguous. However, this assumption is incorrect when the memory spans across a huge

page.

Quoted 47 lines

Correct to directly read the size from the mempool memory chunks.

---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +0 -1
index 268a130c49..cb95d17d13 100644
@@ -1039,7 +1039,7 @@ eth_link_update(struct rte_eth_dev *dev
 __rte_unused,  }
 
 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
-static inline uintptr_t get_base_addr(struct rte_mempool *mp,

uint64_t
*align)

drivers/net/af_xdp/rte_eth_af_xdp.c +2 -0
+static inline uintptr_t get_memhdr_info(struct rte_mempool *mp,
+uint64_t

*align, size_t *len)
{

drivers/net/af_xdp/rte_eth_af_xdp.c +0 -0
 	struct rte_mempool_memhdr *memhdr;
 	uintptr_t memhdr_addr, aligned_addr; @@ -1048,6 +1048,7 @@ static

inline uintptr_t get_base_addr(struct rte_mempool *mp, uint64_t *align)

drivers/net/af_xdp/rte_eth_af_xdp.c +2 -0
 	memhdr_addr = (uintptr_t)memhdr->addr;
 	aligned_addr = memhdr_addr & ~(getpagesize() - 1);
 	*align = memhdr_addr - aligned_addr;
+	*len = memhdr->len;
 
 	return aligned_addr;
 }
@@ -1125,6 +1126,7 @@ xsk_umem_info *xdp_umem_configure(struct
 pmd_internals *internals,
 	void *base_addr = NULL;
 	struct rte_mempool *mb_pool = rxq->mb_pool;
 	uint64_t umem_size, align = 0;
+	size_t len = 0;
 
 	if (internals->shared_umem) {

if (get_shared_umem(rxq, internals->if_name, &umem) < 0) @@
-1156,10 +1158,8 @@ xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
}

Quoted 12 lines

drivers/net/af_xdp/rte_eth_af_xdp.c xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals, · line 1162

Quoted 8 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -4
 		umem->mb_pool = mb_pool;
-		base_addr = (void *)get_base_addr(mb_pool, &align);
-		umem_size = (uint64_t)mb_pool->populated_size *
-				(uint64_t)usr_config.frame_size +
-				align;
+		base_addr = (void *)get_memhdr_info(mb_pool, &align,

&len);

drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
+		umem_size = (uint64_t)len + align;

len is set to the length of the first memhdr of the mempool. There may be many other memhdrs in the mempool. So I don't think this is the correct value to use for calculating the entire umem size.

Current each xdp rx ring is bonded to one single umem region, it can't reuse the memory if there are multiple memhdrs in the mempool. How about adding a check on the number of the memory chunks to only allow one single memhdr mempool can be used here?

Quoted 7 lines
Quoted 3 lines

ret = xsk_umem__create(&umem->umem, base_addr,

umem_size,

Quoted 3 lines

&rxq->fq, &rxq->cq, &usr_config);
--
2.34.1

#3 lore raw
Quoted 69 lines
Quoted 5 lines
Quoted 5 lines

Subject: [PATCH] net/af_xdp: fix umem map size for zero copy

The current calculation assumes that the mbufs are contiguous. However, this assumption is incorrect when the memory spans across a

huge

Quoted 16 lines

page.

Quoted 15 lines

Correct to directly read the size from the mempool memory chunks.

---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +0 -0
index 268a130c49..cb95d17d13 100644
@@ -1039,7 +1039,7 @@ eth_link_update(struct rte_eth_dev *dev
 __rte_unused,  }
 
Quoted 10 lines

drivers/net/af_xdp/rte_eth_af_xdp.c

Quoted 10 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +0 -1
 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
-static inline uintptr_t get_base_addr(struct rte_mempool *mp,

uint64_t
*align)

drivers/net/af_xdp/rte_eth_af_xdp.c +2 -0
+static inline uintptr_t get_memhdr_info(struct rte_mempool *mp,
+uint64_t

*align, size_t *len)
{

drivers/net/af_xdp/rte_eth_af_xdp.c +0 -0
 	struct rte_mempool_memhdr *memhdr;
 	uintptr_t memhdr_addr, aligned_addr; @@ -1048,6 +1048,7 @@

static

Quoted 16 lines
Quoted 16 lines

inline uintptr_t get_base_addr(struct rte_mempool *mp, uint64_t *align)

drivers/net/af_xdp/rte_eth_af_xdp.c +2 -0
 	memhdr_addr = (uintptr_t)memhdr->addr;
 	aligned_addr = memhdr_addr & ~(getpagesize() - 1);
 	*align = memhdr_addr - aligned_addr;
+	*len = memhdr->len;
 
 	return aligned_addr;
 }
@@ -1125,6 +1126,7 @@ xsk_umem_info *xdp_umem_configure(struct
 pmd_internals *internals,
 	void *base_addr = NULL;
 	struct rte_mempool *mb_pool = rxq->mb_pool;
 	uint64_t umem_size, align = 0;
+	size_t len = 0;
 
Quoted 2 lines
Quoted 2 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +0 -0
 	if (internals->shared_umem) {

if (get_shared_umem(rxq, internals->if_name, &umem) < 0)

@@

Quoted 4 lines
Quoted 4 lines

-1156,10 +1158,8 @@ xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
}

Quoted 10 lines

drivers/net/af_xdp/rte_eth_af_xdp.c xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals, · line 1162

Quoted 8 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -4
 		umem->mb_pool = mb_pool;
-		base_addr = (void *)get_base_addr(mb_pool, &align);
-		umem_size = (uint64_t)mb_pool->populated_size *
-				(uint64_t)usr_config.frame_size +
-				align;
+		base_addr = (void *)get_memhdr_info(mb_pool, &align,

&len);

drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
+		umem_size = (uint64_t)len + align;

len is set to the length of the first memhdr of the mempool. There may be

many

Quoted 1 line

other memhdrs in the mempool. So I don't think this is the correct value to

use for

Quoted 8 lines
Quoted 1 line

calculating the entire umem size.

Current each xdp rx ring is bonded to one single umem region, it can't reuse the memory
if there are multiple memhdrs in the mempool. How about adding a check on the number
of the memory chunks to only allow one single memhdr mempool can be used here?

The UMEM needs to be a region of virtual contiguous memory. I think this can still be the case, even if the mempool has multiple memhdrs.
If we detect >1 memhdrs perhaps we need to verify that the RTE_MEMPOOL_F_NO_IOVA_CONTIG flag is not set which I think would mean that the mempool may not be virtually contiguous.

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

ret = xsk_umem__create(&umem->umem, base_addr,

umem_size,

Quoted 3 lines

&rxq->fq, &rxq->cq, &usr_config);
--
2.34.1

author
#4 lore raw

PATCH v2 1/1 net/af_xdp: fix umem map size for zero copy

In reply to Frank Du

The current calculation assumes that the mbufs are contiguous. However, this assumption is incorrect when the memory spans across a huge page. Correct to directly read the size from the mempool memory chunks.

---
v2:
* Add virtual contiguous detect for for multiple memhdrs.

---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 34 ++++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 6 deletions(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +28 -6
index 268a130c49..7456108d6d 100644
@@ -1039,16 +1039,35 @@ eth_link_update(struct rte_eth_dev *dev __rte_unused,
 }
 
 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
-static inline uintptr_t get_base_addr(struct rte_mempool *mp, uint64_t *align)
+static inline uintptr_t get_memhdr_info(struct rte_mempool *mp, uint64_t *align, size_t *len)
 {
-	struct rte_mempool_memhdr *memhdr;
+	struct rte_mempool_memhdr *memhdr, *next;
 	uintptr_t memhdr_addr, aligned_addr;
+	size_t memhdr_len = 0;
 
+	/* get the mempool base addr and align */
 	memhdr = STAILQ_FIRST(&mp->mem_list);
 	memhdr_addr = (uintptr_t)memhdr->addr;
 	aligned_addr = memhdr_addr & ~(getpagesize() - 1);
 	*align = memhdr_addr - aligned_addr;
+	memhdr_len += memhdr->len;
+
+	/* check if virtual contiguous memory for multiple memhdrs */
+	next = STAILQ_NEXT(memhdr, next);
+	while (next != NULL) {
+		if ((uintptr_t)next->addr != (uintptr_t)memhdr->addr + memhdr->len) {
+			AF_XDP_LOG(ERR, "memory chunks not virtual contiguous, "
+					"next: %p, cur: %p(len: %" PRId64 " )\n",
+					next->addr, memhdr->addr, memhdr->len);
+			return 0;
+		}
+		/* virtual contiguous */
+		memhdr = next;
+		memhdr_len += memhdr->len;
+		next = STAILQ_NEXT(memhdr, next);
+	}
 
+	*len = memhdr_len;
 	return aligned_addr;
 }
 
@@ -1125,6 +1144,7 @@ xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
 	void *base_addr = NULL;
 	struct rte_mempool *mb_pool = rxq->mb_pool;
 	uint64_t umem_size, align = 0;
+	size_t len = 0;
 
 	if (internals->shared_umem) {
 		if (get_shared_umem(rxq, internals->if_name, &umem) < 0)
@@ -1156,10 +1176,12 @@ xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
 		}
 
 		umem->mb_pool = mb_pool;
-		base_addr = (void *)get_base_addr(mb_pool, &align);
-		umem_size = (uint64_t)mb_pool->populated_size *
-				(uint64_t)usr_config.frame_size +
-				align;
+		base_addr = (void *)get_memhdr_info(mb_pool, &align, &len);
+		if (!base_addr) {
+			AF_XDP_LOG(ERR, "Failed to parse memhdr info from pool\n");
+			goto err;
+		}
+		umem_size = (uint64_t)len + align;
 
 		ret = xsk_umem__create(&umem->umem, base_addr, umem_size,
 				&rxq->fq, &rxq->cq, &usr_config);
Signature · 1 line
2.34.1
Acked-by on v2 1/1
#5 lore raw
Quoted 6 lines

The current calculation assumes that the mbufs are contiguous. However, this assumption is incorrect when the memory spans across a huge page. Correct to directly read the size from the mempool memory chunks.

Hi Frank,

Thanks for the patch.

Before your patch the umem_size was calculated using mb_pool->populated_size * rte_mempool_calc_obj_size(mb_pool->elt_size, ..) With your patch we sum up the lens of all memhdrs in the mempool.

When debugging I see the new calculation can yield a larger value, but the new logic looks good and more thorough to me so I'm happy to opt with the new approach.

Thanks,
Ciara

Quoted 91 lines

---
v2:
* Add virtual contiguous detect for for multiple memhdrs.

---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 34 ++++++++++++++++++++++++----
(quoted diff) +0 -1
-
 1 file changed, 28 insertions(+), 6 deletions(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +11 -2
index 268a130c49..7456108d6d 100644
@@ -1039,16 +1039,35 @@ eth_link_update(struct rte_eth_dev *dev
 __rte_unused,
 }
 
 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
-static inline uintptr_t get_base_addr(struct rte_mempool *mp, uint64_t
 *align)
+static inline uintptr_t get_memhdr_info(struct rte_mempool *mp, uint64_t
 *align, size_t *len)
 {
-	struct rte_mempool_memhdr *memhdr;
+	struct rte_mempool_memhdr *memhdr, *next;
 	uintptr_t memhdr_addr, aligned_addr;
+	size_t memhdr_len = 0;
 
+	/* get the mempool base addr and align */
 	memhdr = STAILQ_FIRST(&mp->mem_list);
 	memhdr_addr = (uintptr_t)memhdr->addr;
 	aligned_addr = memhdr_addr & ~(getpagesize() - 1);
 	*align = memhdr_addr - aligned_addr;
+	memhdr_len += memhdr->len;
+
+	/* check if virtual contiguous memory for multiple memhdrs */
+	next = STAILQ_NEXT(memhdr, next);
+	while (next != NULL) {
+		if ((uintptr_t)next->addr != (uintptr_t)memhdr->addr +
 memhdr->len) {
+			AF_XDP_LOG(ERR, "memory chunks not virtual

contiguous, "

drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
+					"next: %p, cur: %p(len: %" PRId64 "

)\n",

drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
+					next->addr, memhdr->addr, memhdr-
Quoted 1 line

len);

drivers/net/af_xdp/rte_eth_af_xdp.c +9 -0
+			return 0;
+		}
+		/* virtual contiguous */
+		memhdr = next;
+		memhdr_len += memhdr->len;
+		next = STAILQ_NEXT(memhdr, next);
+	}
 
+	*len = memhdr_len;
 	return aligned_addr;
 }
@@ -1125,6 +1144,7 @@ xsk_umem_info *xdp_umem_configure(struct
 pmd_internals *internals,
 	void *base_addr = NULL;
 	struct rte_mempool *mb_pool = rxq->mb_pool;
 	uint64_t umem_size, align = 0;
+	size_t len = 0;
 
 	if (internals->shared_umem) {
drivers/net/af_xdp/rte_eth_af_xdp.c +6 -4
 		if (get_shared_umem(rxq, internals->if_name, &umem) < 0)
@@ -1156,10 +1176,12 @@ xsk_umem_info *xdp_umem_configure(struct
 pmd_internals *internals,
 		}
 
 		umem->mb_pool = mb_pool;
-		base_addr = (void *)get_base_addr(mb_pool, &align);
-		umem_size = (uint64_t)mb_pool->populated_size *
-				(uint64_t)usr_config.frame_size +
-				align;
+		base_addr = (void *)get_memhdr_info(mb_pool, &align,
 &len);
+		if (!base_addr) {
+			AF_XDP_LOG(ERR, "Failed to parse memhdr info from
 pool\n");
+			goto err;
+		}
+		umem_size = (uint64_t)len + align;

ret = xsk_umem__create(&umem->umem, base_addr,
umem_size,
&rxq->fq, &rxq->cq, &usr_config);
--
2.34.1

author
#6 lore raw
Quoted 19 lines

-----Original Message-----
From: Loftus, Ciara <[email protected]>
Sent: Friday, May 17, 2024 9:20 PM
To: Du, Frank <[email protected]>
Cc: [email protected]
Subject: RE: [PATCH v2] net/af_xdp: fix umem map size for zero copy

Quoted 3 lines

The current calculation assumes that the mbufs are contiguous. However, this assumption is incorrect when the memory spans across a huge

page.

Quoted 3 lines

Correct to directly read the size from the mempool memory chunks.

Hi Frank,

Thanks for the patch.

Quoted 7 lines

Before your patch the umem_size was calculated using mb_pool->populated_size
* rte_mempool_calc_obj_size(mb_pool->elt_size, ..) With your patch we sum up the lens of all memhdrs in the mempool.

When debugging I see the new calculation can yield a larger value, but the new logic looks good and more thorough to me so I'm happy to opt with the new approach.

Hi Ciara,

Thanks for the review. The reason for a larger value is that, the pool contain some necessary space hole to ensure one single mbuf does not span two virtual huge page, so that the PA of each mbuf is secure for the hardware operation.

Quoted 98 lines

Thanks,
Ciara

Quoted 83 lines

---
v2:
* Add virtual contiguous detect for for multiple memhdrs.

---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 34 ++++++++++++++++++++++++----
(quoted diff) +0 -1
-
 1 file changed, 28 insertions(+), 6 deletions(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +0 -1
index 268a130c49..7456108d6d 100644
@@ -1039,16 +1039,35 @@ eth_link_update(struct rte_eth_dev *dev
 __rte_unused,  }
 
 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
-static inline uintptr_t get_base_addr(struct rte_mempool *mp,

uint64_t
*align)

drivers/net/af_xdp/rte_eth_af_xdp.c +2 -0
+static inline uintptr_t get_memhdr_info(struct rte_mempool *mp,
+uint64_t

*align, size_t *len)
{

drivers/net/af_xdp/rte_eth_af_xdp.c +9 -1
-	struct rte_mempool_memhdr *memhdr;
+	struct rte_mempool_memhdr *memhdr, *next;
 	uintptr_t memhdr_addr, aligned_addr;
+	size_t memhdr_len = 0;
 
+	/* get the mempool base addr and align */
 	memhdr = STAILQ_FIRST(&mp->mem_list);
 	memhdr_addr = (uintptr_t)memhdr->addr;
 	aligned_addr = memhdr_addr & ~(getpagesize() - 1);
 	*align = memhdr_addr - aligned_addr;
+	memhdr_len += memhdr->len;
+
+	/* check if virtual contiguous memory for multiple memhdrs */
+	next = STAILQ_NEXT(memhdr, next);
+	while (next != NULL) {
+		if ((uintptr_t)next->addr != (uintptr_t)memhdr->addr +

memhdr->len) {

drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
+			AF_XDP_LOG(ERR, "memory chunks not virtual

contiguous, "

drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
+					"next: %p, cur: %p(len: %" PRId64 "

)\n",

drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
+					next->addr, memhdr->addr, memhdr-
Quoted 1 line

len);

drivers/net/af_xdp/rte_eth_af_xdp.c +9 -0
+			return 0;
+		}
+		/* virtual contiguous */
+		memhdr = next;
+		memhdr_len += memhdr->len;
+		next = STAILQ_NEXT(memhdr, next);
+	}
 
+	*len = memhdr_len;
 	return aligned_addr;
 }
@@ -1125,6 +1144,7 @@ xsk_umem_info *xdp_umem_configure(struct
 pmd_internals *internals,
 	void *base_addr = NULL;
 	struct rte_mempool *mb_pool = rxq->mb_pool;
 	uint64_t umem_size, align = 0;
+	size_t len = 0;
 
 	if (internals->shared_umem) {

if (get_shared_umem(rxq, internals->if_name, &umem) < 0) @@
-1156,10 +1176,12 @@ xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,

drivers/net/af_xdp/rte_eth_af_xdp.c +1 -4
 		}
 
 		umem->mb_pool = mb_pool;
-		base_addr = (void *)get_base_addr(mb_pool, &align);
-		umem_size = (uint64_t)mb_pool->populated_size *
-				(uint64_t)usr_config.frame_size +
-				align;
+		base_addr = (void *)get_memhdr_info(mb_pool, &align,

&len);

drivers/net/af_xdp/rte_eth_af_xdp.c +2 -0
+		if (!base_addr) {
+			AF_XDP_LOG(ERR, "Failed to parse memhdr info from

pool\n");

drivers/net/af_xdp/rte_eth_af_xdp.c xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,

Quoted 5 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +3 -0
+			goto err;
+		}
+		umem_size = (uint64_t)len + align;
 
 		ret = xsk_umem__create(&umem->umem, base_addr,

umem_size,

Quoted 3 lines

&rxq->fq, &rxq->cq, &usr_config);
--
2.34.1

#7 lore raw
In reply to Frank Du
Quoted 6 lines · Frank Du

The current calculation assumes that the mbufs are contiguous. However, this assumption is incorrect when the memory spans across a huge page. Correct to directly read the size from the mempool memory chunks.

Hi Frank,

As this is a fix patch, can you please provide a fixes tag [1], this helps us understanding the logic in the original code and enables us figuring out which stable tree to backport this fix.

[1]
https://doc.dpdk.org/guides/contributing/patches.html#commit-messages-body

#8 lore raw
In reply to Frank Du
Quoted 25 lines · Frank Du

The current calculation assumes that the mbufs are contiguous. However, this assumption is incorrect when the memory spans across a huge page. Correct to directly read the size from the mempool memory chunks.

---
v2:
* Add virtual contiguous detect for for multiple memhdrs.

---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 34 ++++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 6 deletions(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -2
index 268a130c49..7456108d6d 100644
@@ -1039,16 +1039,35 @@ eth_link_update(struct rte_eth_dev *dev __rte_unused,
 }
 
 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
-static inline uintptr_t get_base_addr(struct rte_mempool *mp, uint64_t *align)
+static inline uintptr_t get_memhdr_info(struct rte_mempool *mp, uint64_t *align, size_t *len)
 {
-	struct rte_mempool_memhdr *memhdr;

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1053

Quoted 10 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +3 -0
+	struct rte_mempool_memhdr *memhdr, *next;
 	uintptr_t memhdr_addr, aligned_addr;
+	size_t memhdr_len = 0;
 
+	/* get the mempool base addr and align */
 	memhdr = STAILQ_FIRST(&mp->mem_list);
 	memhdr_addr = (uintptr_t)memhdr->addr;
 	aligned_addr = memhdr_addr & ~(getpagesize() - 1);
 	*align = memhdr_addr - aligned_addr;
 

I am aware this is not part of this patch, but as note, can't we use 'RTE_ALIGN_FLOOR' to calculate aligned address.

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1069

Quoted 12 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +11 -0
+	memhdr_len += memhdr->len;
+
+	/* check if virtual contiguous memory for multiple memhdrs */
+	next = STAILQ_NEXT(memhdr, next);
+	while (next != NULL) {
+		if ((uintptr_t)next->addr != (uintptr_t)memhdr->addr + memhdr->len) {
+			AF_XDP_LOG(ERR, "memory chunks not virtual contiguous, "
+					"next: %p, cur: %p(len: %" PRId64 " )\n",
+					next->addr, memhdr->addr, memhdr->len);
+			return 0;
+		}
 

Isn't there a mempool flag that can help us figure out mempool is not IOVA contiguous? Isn't it sufficient on its own?

Quoted 10 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +6 -0
+		/* virtual contiguous */
+		memhdr = next;
+		memhdr_len += memhdr->len;
+		next = STAILQ_NEXT(memhdr, next);
+	}
 
+	*len = memhdr_len;
 	return aligned_addr;

}

This function goes too much details of the mempool object, and any change in mempool details has potential to break this code.

@Andrew, @Morten, do you think does it make sense to have 'rte_mempool_info_get()' kind of function, that provides at least address and length of the mempool, and used here?

This helps to hide internal details and complexity of the mempool for users.

Quoted 10 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
@@ -1125,6 +1144,7 @@ xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
 	void *base_addr = NULL;
 	struct rte_mempool *mb_pool = rxq->mb_pool;
 	uint64_t umem_size, align = 0;
+	size_t len = 0;
 
 	if (internals->shared_umem) {
 		if (get_shared_umem(rxq, internals->if_name, &umem) < 0)

drivers/net/af_xdp/rte_eth_af_xdp.c xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals, · line 1180

Quoted 9 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -4
@@ -1156,10 +1176,12 @@ xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
 		}
 
 		umem->mb_pool = mb_pool;
-		base_addr = (void *)get_base_addr(mb_pool, &align);
-		umem_size = (uint64_t)mb_pool->populated_size *
-				(uint64_t)usr_config.frame_size +
-				align;
+		base_addr = (void *)get_memhdr_info(mb_pool, &align, &len);
 

Is this calculation correct if mempool is not already aligned to page size?

Like in an example page size is '0x1000', and "memhdr_addr = 0x000a1080" returned aligned address is '0x000a1000', "base_addr = 0x000a1000"

Any access between '0x000a1000' & '0x000a1080' is invalid. Is this expected?

drivers/net/af_xdp/rte_eth_af_xdp.c xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals, · line 1185

Quoted 3 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +2 -0
+		if (!base_addr) {
+			AF_XDP_LOG(ERR, "Failed to parse memhdr info from pool\n");
 

Log message is not accurate, it is not parsing memhdr info failed, but mempool was not satisfying expectation.

Quoted 6 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +3 -0
+			goto err;
+		}
+		umem_size = (uint64_t)len + align;
 
drivers/net/af_xdp/rte_eth_af_xdp.c +0 -0
 		ret = xsk_umem__create(&umem->umem, base_addr, umem_size,
 				&rxq->fq, &rxq->cq, &usr_config);
author
#9 lore raw
Quoted 45 lines

-----Original Message-----
From: Ferruh Yigit <[email protected]>
Sent: Wednesday, May 22, 2024 1:58 AM
To: Du, Frank <[email protected]>; [email protected]; Andrew Rybchenko <[email protected]>; Morten Brørup <[email protected]>
Cc: Loftus, Ciara <[email protected]>; Burakov, Anatoly <[email protected]>
Subject: Re: [PATCH v2] net/af_xdp: fix umem map size for zero copy

Quoted 2 lines · Frank Du

The current calculation assumes that the mbufs are contiguous. However, this assumption is incorrect when the memory spans across a huge

page.

Quoted 31 lines

Correct to directly read the size from the mempool memory chunks.

---
v2:
* Add virtual contiguous detect for for multiple memhdrs.

---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 34
++++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 6 deletions(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +4 -2
index 268a130c49..7456108d6d 100644
@@ -1039,16 +1039,35 @@ eth_link_update(struct rte_eth_dev *dev
 __rte_unused,  }
 
 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
-static inline uintptr_t get_base_addr(struct rte_mempool *mp,
 uint64_t *align)
+static inline uintptr_t get_memhdr_info(struct rte_mempool *mp,
+uint64_t *align, size_t *len)
 {
-	struct rte_mempool_memhdr *memhdr;
+	struct rte_mempool_memhdr *memhdr, *next;
 	uintptr_t memhdr_addr, aligned_addr;
+	size_t memhdr_len = 0;
 
Quoted 9 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1069

Quoted 6 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
+	/* get the mempool base addr and align */
 	memhdr = STAILQ_FIRST(&mp->mem_list);
 	memhdr_addr = (uintptr_t)memhdr->addr;
 	aligned_addr = memhdr_addr & ~(getpagesize() - 1);
 	*align = memhdr_addr - aligned_addr;
 

I am aware this is not part of this patch, but as note, can't we use 'RTE_ALIGN_FLOOR' to calculate aligned address.

Sure, will use RTE_ALIGN_FLOOR in next version.

Quoted 13 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused,

Quoted 10 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +7 -0
+	memhdr_len += memhdr->len;
+
+	/* check if virtual contiguous memory for multiple memhdrs */
+	next = STAILQ_NEXT(memhdr, next);
+	while (next != NULL) {
+		if ((uintptr_t)next->addr != (uintptr_t)memhdr->addr + memhdr-
 len) {
+			AF_XDP_LOG(ERR, "memory chunks not virtual

contiguous, "

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev · line 1064

Quoted 1 line
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
+					"next: %p, cur: %p(len: %" PRId64

" )\n",

Quoted 8 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1063

Quoted 5 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +3 -0
+					next->addr, memhdr->addr, memhdr-
 len);
+			return 0;
+		}

Isn't there a mempool flag that can help us figure out mempool is not IOVA contiguous? Isn't it sufficient on its own?

Indeed, what we need to ascertain is whether it's contiguous in CPU virtual space, not IOVA. I haven't come across a flag specifically for CPU virtual contiguity. The major limitation in XDP is XSK UMEM only supports registering a single contiguous virtual memory area.

Quoted 47 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1072

Quoted 12 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +6 -0
+		/* virtual contiguous */
+		memhdr = next;
+		memhdr_len += memhdr->len;
+		next = STAILQ_NEXT(memhdr, next);
+	}
 
+	*len = memhdr_len;
 	return aligned_addr;
 }

This function goes too much details of the mempool object, and any change in mempool details has potential to break this code.

@Andrew, @Morten, do you think does it make sense to have 'rte_mempool_info_get()' kind of function, that provides at least address and length of the mempool, and used here?

This helps to hide internal details and complexity of the mempool for users.

drivers/net/af_xdp/rte_eth_af_xdp.c xsk_umem_info *xdp_umem_configure(struct

Quoted 2 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +0 -0
@@ -1125,6 +1144,7 @@ xsk_umem_info *xdp_umem_configure(struct

pmd_internals *internals,

drivers/net/af_xdp/rte_eth_af_xdp.c xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,

Quoted 8 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -1
 	void *base_addr = NULL;
 	struct rte_mempool *mb_pool = rxq->mb_pool;
 	uint64_t umem_size, align = 0;
+	size_t len = 0;
 
 	if (internals->shared_umem) {
 		if (get_shared_umem(rxq, internals->if_name, &umem) < 0) @@
-1156,10 +1176,12 @@ xsk_umem_info *xdp_umem_configure(struct

pmd_internals *internals,

drivers/net/af_xdp/rte_eth_af_xdp.c xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals, · line 1179

Quoted 9 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -4
 		}
 
 		umem->mb_pool = mb_pool;
-		base_addr = (void *)get_base_addr(mb_pool, &align);
-		umem_size = (uint64_t)mb_pool->populated_size *
-				(uint64_t)usr_config.frame_size +
-				align;
+		base_addr = (void *)get_memhdr_info(mb_pool, &align, &len);

Is this calculation correct if mempool is not already aligned to page size?

Quoted 4 lines

Like in an example page size is '0x1000', and "memhdr_addr = 0x000a1080" returned aligned address is '0x000a1000', "base_addr = 0x000a1000"

Any access between '0x000a1000' & '0x000a1080' is invalid. Is this expected?

Yes, since the XSK UMEM memory area requires page alignment. However, no need to worry; the memory pointer in the XSK TX/RX descriptor is obtained from the mbuf data area. We don’t have any chance to access the invalid range [0x000a1000: 0x000a1080] here.

Quoted 9 lines

drivers/net/af_xdp/rte_eth_af_xdp.c

Quoted 4 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +2 -0
+		if (!base_addr) {
+			AF_XDP_LOG(ERR, "Failed to parse memhdr info from

pool\n");

Quoted 1 line

Log message is not accurate, it is not parsing memhdr info failed, but mempool was not satisfying expectation.

Thanks, will correct it in next version.

Quoted 8 lines

drivers/net/af_xdp/rte_eth_af_xdp.c xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,

Quoted 6 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +3 -0
+			goto err;
+		}
+		umem_size = (uint64_t)len + align;
 
 		ret = xsk_umem__create(&umem->umem, base_addr,

umem_size,

Quoted 1 line

&rxq->fq, &rxq->cq, &usr_config);

#10 lore raw
Quoted 41 lines

From: Du, Frank [mailto:[email protected]]
Sent: Wednesday, 22 May 2024 03.25

Quoted 38 lines

From: Ferruh Yigit <[email protected]>
Sent: Wednesday, May 22, 2024 1:58 AM

Quoted 2 lines · Frank Du

The current calculation assumes that the mbufs are contiguous. However, this assumption is incorrect when the memory spans across a huge

page.

Quoted 31 lines

Correct to directly read the size from the mempool memory chunks.

---
v2:
* Add virtual contiguous detect for for multiple memhdrs.

---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 34
++++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 6 deletions(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +4 -2
index 268a130c49..7456108d6d 100644
@@ -1039,16 +1039,35 @@ eth_link_update(struct rte_eth_dev *dev
 __rte_unused,  }
 
 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
-static inline uintptr_t get_base_addr(struct rte_mempool *mp,
 uint64_t *align)
+static inline uintptr_t get_memhdr_info(struct rte_mempool *mp,
+uint64_t *align, size_t *len)
 {
-	struct rte_mempool_memhdr *memhdr;
+	struct rte_mempool_memhdr *memhdr, *next;
 	uintptr_t memhdr_addr, aligned_addr;
+	size_t memhdr_len = 0;
 
Quoted 3 lines
Quoted 3 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1050

Quoted 3 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
+	/* get the mempool base addr and align */
 	memhdr = STAILQ_FIRST(&mp->mem_list);
 	memhdr_addr = (uintptr_t)memhdr->addr;

This is not a new bug; but if the mempool is not populated, memhdr is NULL here.

Quoted 31 lines
Quoted 6 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1069

Quoted 3 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +0 -0
 	aligned_addr = memhdr_addr & ~(getpagesize() - 1);
 	*align = memhdr_addr - aligned_addr;
 

I am aware this is not part of this patch, but as note, can't we use 'RTE_ALIGN_FLOOR' to calculate aligned address.

Sure, will use RTE_ALIGN_FLOOR in next version.

Quoted 13 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused,

Quoted 10 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +7 -0
+	memhdr_len += memhdr->len;
+
+	/* check if virtual contiguous memory for multiple memhdrs */
+	next = STAILQ_NEXT(memhdr, next);
+	while (next != NULL) {
+		if ((uintptr_t)next->addr != (uintptr_t)memhdr->addr + memhdr-
 len) {
+			AF_XDP_LOG(ERR, "memory chunks not virtual

contiguous, "

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev · line 1064

Quoted 1 line
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
+					"next: %p, cur: %p(len: %" PRId64

" )\n",

Quoted 8 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1063

Quoted 5 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +3 -0
+					next->addr, memhdr->addr, memhdr-
 len);
+			return 0;
+		}

Isn't there a mempool flag that can help us figure out mempool is not IOVA contiguous? Isn't it sufficient on its own?

Quoted 4 lines

Indeed, what we need to ascertain is whether it's contiguous in CPU virtual space, not IOVA. I haven't come across a flag specifically for CPU virtual contiguity. The major limitation in XDP is XSK UMEM only supports registering a single contiguous virtual memory area.

I would assume that the EAL memory manager merges free memory into contiguous chunks whenever possible. @Anatoly, please confirm?

If my assumption is correct, it means that if mp->nb_mem_chunks != 1, then the mempool is not virtual contiguous. And if mp->nb_mem_chunks == 1, then it is; there is no need to iterate through the memhdr list.

Quoted 17 lines
Quoted 17 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1072

Quoted 13 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +6 -0
+		/* virtual contiguous */
+		memhdr = next;
+		memhdr_len += memhdr->len;
+		next = STAILQ_NEXT(memhdr, next);
+	}
 
+	*len = memhdr_len;
 	return aligned_addr;
 }

This function goes too much details of the mempool object, and any change in mempool details has potential to break this code.

Quoted 6 lines
Quoted 2 lines

@Andrew, @Morten, do you think does it make sense to have 'rte_mempool_info_get()' kind of function, that provides at least address

and

Quoted 3 lines

length of the mempool, and used here?

This helps to hide internal details and complexity of the mempool for users.

I think all the relevant information is available as (public) fields directly in the rte_mempool.
I agree about hiding internal details. For discriminating between private and public information, I would prefer marking the "private" fields in the rte_mempool structure as such.

Optimally we need an rte_mempool_create() flag, specifying that the mempool objects must be allocated as one chunk of memory with contiguous virtual addresses when populating the mempool. As discussed in another thread [1], the proposed pointer compression library would also benefit from such a mempool flag.

[1] https://inbox.dpdk.org/dev/[email protected]/

Quoted 16 lines
Quoted 16 lines

drivers/net/af_xdp/rte_eth_af_xdp.c xsk_umem_info *xdp_umem_configure(struct

Quoted 4 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +0 -0
@@ -1125,6 +1144,7 @@ xsk_umem_info *xdp_umem_configure(struct

pmd_internals *internals,

drivers/net/af_xdp/rte_eth_af_xdp.c xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,

Quoted 8 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -1
 	void *base_addr = NULL;
 	struct rte_mempool *mb_pool = rxq->mb_pool;
 	uint64_t umem_size, align = 0;
+	size_t len = 0;
 
 	if (internals->shared_umem) {
 		if (get_shared_umem(rxq, internals->if_name, &umem) < 0) @@
-1156,10 +1176,12 @@ xsk_umem_info *xdp_umem_configure(struct

pmd_internals *internals,

Quoted 2 lines

}

Quoted 9 lines
Quoted 9 lines

drivers/net/af_xdp/rte_eth_af_xdp.c xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals, · line 1179

Quoted 7 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -4
 		umem->mb_pool = mb_pool;
-		base_addr = (void *)get_base_addr(mb_pool, &align);
-		umem_size = (uint64_t)mb_pool->populated_size *
-				(uint64_t)usr_config.frame_size +
-				align;
+		base_addr = (void *)get_memhdr_info(mb_pool, &align, &len);

Is this calculation correct if mempool is not already aligned to page size?

Please note: The mempool uses one memzone for the mempool structure itself. The objects in the mempool are stored in another memzone (or multiple other memzones, if necessary). I think you are talking about the alignment of the mempool object chunk, not of the mempool structure itself.

Quoted 13 lines
Quoted 5 lines

Like in an example page size is '0x1000', and "memhdr_addr = 0x000a1080" returned aligned address is '0x000a1000', "base_addr = 0x000a1000"

Any access between '0x000a1000' & '0x000a1080' is invalid. Is this expected?

Yes, since the XSK UMEM memory area requires page alignment. However, no need to worry; the memory pointer in the XSK TX/RX descriptor is obtained from the mbuf data area. We don’t have any chance to access the invalid range [0x000a1000: 0x000a1080] here.

Quoted 2 lines
Quoted 8 lines
Quoted 6 lines

drivers/net/af_xdp/rte_eth_af_xdp.c

Quoted 2 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +2 -0
+		if (!base_addr) {
+			AF_XDP_LOG(ERR, "Failed to parse memhdr info from

pool\n");

Quoted 1 line

Log message is not accurate, it is not parsing memhdr info failed, but

mempool

Quoted 1 line

was not satisfying expectation.

Looking at get_memhdr_info() above, it could be either mempool or memhdr failing to parse.

Quoted 11 lines

Thanks, will correct it in next version.

Quoted 8 lines

drivers/net/af_xdp/rte_eth_af_xdp.c xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,

Quoted 6 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +3 -0
+			goto err;
+		}
+		umem_size = (uint64_t)len + align;
 
 		ret = xsk_umem__create(&umem->umem, base_addr,

umem_size,

Quoted 1 line

&rxq->fq, &rxq->cq, &usr_config);

#11 lore raw
Quoted 107 lines · Morten Brørup
Quoted 41 lines

From: Du, Frank [mailto:[email protected]]
Sent: Wednesday, 22 May 2024 03.25

Quoted 38 lines

From: Ferruh Yigit <[email protected]>
Sent: Wednesday, May 22, 2024 1:58 AM

Quoted 2 lines · Frank Du

The current calculation assumes that the mbufs are contiguous. However, this assumption is incorrect when the memory spans across a huge

page.

Quoted 31 lines

Correct to directly read the size from the mempool memory chunks.

---
v2:
* Add virtual contiguous detect for for multiple memhdrs.

---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 34
++++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 6 deletions(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +4 -2
index 268a130c49..7456108d6d 100644
@@ -1039,16 +1039,35 @@ eth_link_update(struct rte_eth_dev *dev
 __rte_unused,  }
 
 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
-static inline uintptr_t get_base_addr(struct rte_mempool *mp,
 uint64_t *align)
+static inline uintptr_t get_memhdr_info(struct rte_mempool *mp,
+uint64_t *align, size_t *len)
 {
-	struct rte_mempool_memhdr *memhdr;
+	struct rte_mempool_memhdr *memhdr, *next;
 	uintptr_t memhdr_addr, aligned_addr;
+	size_t memhdr_len = 0;
 
Quoted 3 lines
Quoted 3 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1050

Quoted 3 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
+	/* get the mempool base addr and align */
 	memhdr = STAILQ_FIRST(&mp->mem_list);
 	memhdr_addr = (uintptr_t)memhdr->addr;

This is not a new bug; but if the mempool is not populated, memhdr is NULL here.

Quoted 31 lines
Quoted 6 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1069

Quoted 3 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +0 -0
 	aligned_addr = memhdr_addr & ~(getpagesize() - 1);
 	*align = memhdr_addr - aligned_addr;
 

I am aware this is not part of this patch, but as note, can't we use 'RTE_ALIGN_FLOOR' to calculate aligned address.

Sure, will use RTE_ALIGN_FLOOR in next version.

Quoted 13 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused,

Quoted 10 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +7 -0
+	memhdr_len += memhdr->len;
+
+	/* check if virtual contiguous memory for multiple memhdrs */
+	next = STAILQ_NEXT(memhdr, next);
+	while (next != NULL) {
+		if ((uintptr_t)next->addr != (uintptr_t)memhdr->addr + memhdr-
 len) {
+			AF_XDP_LOG(ERR, "memory chunks not virtual

contiguous, "

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev · line 1064

Quoted 1 line
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
+					"next: %p, cur: %p(len: %" PRId64

" )\n",

Quoted 8 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1063

Quoted 5 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +3 -0
+					next->addr, memhdr->addr, memhdr-
 len);
+			return 0;
+		}

Isn't there a mempool flag that can help us figure out mempool is not IOVA contiguous? Isn't it sufficient on its own?

Quoted 4 lines

Indeed, what we need to ascertain is whether it's contiguous in CPU virtual space, not IOVA. I haven't come across a flag specifically for CPU virtual contiguity. The major limitation in XDP is XSK UMEM only supports registering a single contiguous virtual memory area.

I would assume that the EAL memory manager merges free memory into contiguous chunks whenever possible. @Anatoly, please confirm?

If my assumption is correct, it means that if mp->nb_mem_chunks != 1, then the mempool is not virtual contiguous. And if mp->nb_mem_chunks == 1, then it is; there is no need to iterate through the memhdr list.

Quoted 19 lines
Quoted 19 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1072

Quoted 13 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +6 -0
+		/* virtual contiguous */
+		memhdr = next;
+		memhdr_len += memhdr->len;
+		next = STAILQ_NEXT(memhdr, next);
+	}
 
+	*len = memhdr_len;
 	return aligned_addr;
 }

This function goes too much details of the mempool object, and any change in mempool details has potential to break this code.

@Andrew, @Morten, do you think does it make sense to have 'rte_mempool_info_get()' kind of function, that provides at least address

Quoted 8 lines
Quoted 4 lines

and

Quoted 3 lines

length of the mempool, and used here?

This helps to hide internal details and complexity of the mempool for users.

I think all the relevant information is available as (public) fields directly in the rte_mempool.
I agree about hiding internal details. For discriminating between private and public information, I would prefer marking the "private" fields in the rte_mempool structure as such.

Marking fields 'private' may break some user code, so I don't know about this, although I agree better thing to do programmatically.

But even required fields are public, having an _info() API may help abstracting the implementation details and kept it withing the library (instead of leaking to drivers like this).
But not sure if there are other potential users of such an API.

Quoted 2 lines

Optimally we need an rte_mempool_create() flag, specifying that the mempool objects must be allocated as one chunk of memory with contiguous virtual addresses when populating the mempool. As discussed in another thread [1], the proposed pointer compression library would also benefit from such a mempool flag.

Ack, this also can be useful for this usecase.

Quoted 28 lines

[1] https://inbox.dpdk.org/dev/[email protected]/

Quoted 16 lines
Quoted 16 lines

drivers/net/af_xdp/rte_eth_af_xdp.c xsk_umem_info *xdp_umem_configure(struct

Quoted 4 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +0 -0
@@ -1125,6 +1144,7 @@ xsk_umem_info *xdp_umem_configure(struct

pmd_internals *internals,

drivers/net/af_xdp/rte_eth_af_xdp.c xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,

Quoted 8 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -1
 	void *base_addr = NULL;
 	struct rte_mempool *mb_pool = rxq->mb_pool;
 	uint64_t umem_size, align = 0;
+	size_t len = 0;
 
 	if (internals->shared_umem) {
 		if (get_shared_umem(rxq, internals->if_name, &umem) < 0) @@
-1156,10 +1176,12 @@ xsk_umem_info *xdp_umem_configure(struct

pmd_internals *internals,

Quoted 2 lines

}

Quoted 9 lines
Quoted 9 lines

drivers/net/af_xdp/rte_eth_af_xdp.c xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals, · line 1179

Quoted 7 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -4
 		umem->mb_pool = mb_pool;
-		base_addr = (void *)get_base_addr(mb_pool, &align);
-		umem_size = (uint64_t)mb_pool->populated_size *
-				(uint64_t)usr_config.frame_size +
-				align;
+		base_addr = (void *)get_memhdr_info(mb_pool, &align, &len);

Is this calculation correct if mempool is not already aligned to page size?

Quoted 2 lines

Please note: The mempool uses one memzone for the mempool structure itself. The objects in the mempool are stored in another memzone (or multiple other memzones, if necessary). I think you are talking about the alignment of the mempool object chunk, not of the mempool structure itself.

Yes, about the mempool object chunk.

author
#12 lore raw
In reply to Morten Brørup
Quoted 51 lines

-----Original Message-----
From: Morten Brørup <[email protected]>
Sent: Wednesday, May 22, 2024 3:27 PM
To: Du, Frank <[email protected]>; Ferruh Yigit <[email protected]>; [email protected]; Andrew Rybchenko <[email protected]>; Burakov, Anatoly <[email protected]>
Cc: Loftus, Ciara <[email protected]>
Subject: RE: [PATCH v2] net/af_xdp: fix umem map size for zero copy

Quoted 42 lines

From: Du, Frank [mailto:[email protected]]
Sent: Wednesday, 22 May 2024 03.25

Quoted 39 lines

From: Ferruh Yigit <[email protected]>
Sent: Wednesday, May 22, 2024 1:58 AM

Quoted 3 lines · Frank Du

The current calculation assumes that the mbufs are contiguous. However, this assumption is incorrect when the memory spans across a huge

page.

Quoted 31 lines

Correct to directly read the size from the mempool memory chunks.

---
v2:
* Add virtual contiguous detect for for multiple memhdrs.

---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 34
++++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 6 deletions(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +4 -2
index 268a130c49..7456108d6d 100644
@@ -1039,16 +1039,35 @@ eth_link_update(struct rte_eth_dev *dev
 __rte_unused,  }
 
 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
-static inline uintptr_t get_base_addr(struct rte_mempool *mp,
 uint64_t *align)
+static inline uintptr_t get_memhdr_info(struct rte_mempool *mp,
+uint64_t *align, size_t *len)
 {
-	struct rte_mempool_memhdr *memhdr;
+	struct rte_mempool_memhdr *memhdr, *next;
 	uintptr_t memhdr_addr, aligned_addr;
+	size_t memhdr_len = 0;
 
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1050

Quoted 3 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
+	/* get the mempool base addr and align */
 	memhdr = STAILQ_FIRST(&mp->mem_list);
 	memhdr_addr = (uintptr_t)memhdr->addr;

This is not a new bug; but if the mempool is not populated, memhdr is NULL here.

Thanks, will add a check later.

Quoted 37 lines
Quoted 32 lines
Quoted 7 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1069

Quoted 4 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +0 -0
 	aligned_addr = memhdr_addr & ~(getpagesize() - 1);
 	*align = memhdr_addr - aligned_addr;
 

I am aware this is not part of this patch, but as note, can't we use 'RTE_ALIGN_FLOOR' to calculate aligned address.

Sure, will use RTE_ALIGN_FLOOR in next version.

Quoted 13 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused,

Quoted 10 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +7 -0
+	memhdr_len += memhdr->len;
+
+	/* check if virtual contiguous memory for multiple memhdrs */
+	next = STAILQ_NEXT(memhdr, next);
+	while (next != NULL) {
+		if ((uintptr_t)next->addr != (uintptr_t)memhdr->addr + memhdr-
 len) {
+			AF_XDP_LOG(ERR, "memory chunks not virtual

contiguous, "

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev · line 1064

Quoted 1 line
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
+					"next: %p, cur: %p(len: %" PRId64

" )\n",

Quoted 8 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1063

Quoted 5 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +3 -0
+					next->addr, memhdr->addr, memhdr-
 len);
+			return 0;
+		}

Isn't there a mempool flag that can help us figure out mempool is not IOVA contiguous? Isn't it sufficient on its own?

Quoted 4 lines

Indeed, what we need to ascertain is whether it's contiguous in CPU virtual space, not IOVA. I haven't come across a flag specifically for CPU virtual contiguity. The major limitation in XDP is XSK UMEM only supports registering a single contiguous virtual memory area.

Quoted 7 lines

I would assume that the EAL memory manager merges free memory into contiguous chunks whenever possible.
@Anatoly, please confirm?

If my assumption is correct, it means that if mp->nb_mem_chunks != 1, then the mempool is not virtual contiguous. And if mp->nb_mem_chunks == 1, then it is; there is no need to iterate through the memhdr list.

If this's true now, however, this assumption may not hold true in the future code change, iterating through the list may is a safer way as it carefully checks the virtual address without relying on any condition.

#13 lore raw
Quoted 17 lines

From: Du, Frank [mailto:[email protected]]
Sent: Thursday, 23 May 2024 08.56

Quoted 14 lines

From: Morten Brørup <[email protected]>
Sent: Wednesday, May 22, 2024 3:27 PM

Quoted 11 lines

From: Du, Frank [mailto:[email protected]]
Sent: Wednesday, 22 May 2024 03.25

Quoted 8 lines

From: Ferruh Yigit <[email protected]>
Sent: Wednesday, May 22, 2024 1:58 AM

Quoted 3 lines · Frank Du

The current calculation assumes that the mbufs are contiguous. However, this assumption is incorrect when the memory spans across a huge

page.

What does "the memory spans across a huge page" mean?

Should it be "the memory spans across multiple memory chunks"?

Quoted 83 lines
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines

Correct to directly read the size from the mempool memory chunks.

---
v2:
* Add virtual contiguous detect for for multiple memhdrs.

---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 34
++++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 6 deletions(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +4 -2
index 268a130c49..7456108d6d 100644
@@ -1039,16 +1039,35 @@ eth_link_update(struct rte_eth_dev *dev
 __rte_unused,  }
 
 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
-static inline uintptr_t get_base_addr(struct rte_mempool *mp,
 uint64_t *align)
+static inline uintptr_t get_memhdr_info(struct rte_mempool *mp,
+uint64_t *align, size_t *len)
 {
-	struct rte_mempool_memhdr *memhdr;
+	struct rte_mempool_memhdr *memhdr, *next;
 	uintptr_t memhdr_addr, aligned_addr;
+	size_t memhdr_len = 0;
 
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1050

Quoted 3 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
+	/* get the mempool base addr and align */
 	memhdr = STAILQ_FIRST(&mp->mem_list);
 	memhdr_addr = (uintptr_t)memhdr->addr;

This is not a new bug; but if the mempool is not populated, memhdr is NULL

here.

Thanks, will add a check later.

Quoted 12 lines
Quoted 12 lines
Quoted 7 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1069

Quoted 4 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +0 -0
 	aligned_addr = memhdr_addr & ~(getpagesize() - 1);
 	*align = memhdr_addr - aligned_addr;
 

I am aware this is not part of this patch, but as note, can't we use 'RTE_ALIGN_FLOOR' to calculate aligned address.

Sure, will use RTE_ALIGN_FLOOR in next version.

Quoted 2 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused,

Quoted 6 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +6 -0
+	memhdr_len += memhdr->len;
+
+	/* check if virtual contiguous memory for multiple memhdrs */
+	next = STAILQ_NEXT(memhdr, next);
+	while (next != NULL) {
+		if ((uintptr_t)next->addr != (uintptr_t)memhdr->addr +

memhdr-

Quoted 19 lines
Quoted 14 lines
Quoted 5 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev · line 1063

Quoted 2 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
 len) {
+			AF_XDP_LOG(ERR, "memory chunks not virtual

contiguous, "

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev · line 1064

Quoted 1 line
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
+					"next: %p, cur: %p(len: %" PRId64

" )\n",

Quoted 8 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1063

Quoted 5 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +3 -0
+					next->addr, memhdr->addr, memhdr-
 len);
+			return 0;
+		}

Isn't there a mempool flag that can help us figure out mempool is not IOVA contiguous? Isn't it sufficient on its own?

Quoted 4 lines

Indeed, what we need to ascertain is whether it's contiguous in CPU virtual space, not IOVA. I haven't come across a flag specifically for CPU virtual contiguity. The major limitation in XDP is XSK UMEM only supports registering a single contiguous virtual memory area.

Quoted 5 lines

I would assume that the EAL memory manager merges free memory into contiguous chunks whenever possible.
@Anatoly, please confirm?

If my assumption is correct, it means that if mp->nb_mem_chunks != 1, then

Quoted 8 lines

the

Quoted 1 line

mempool is not virtual contiguous. And if mp->nb_mem_chunks == 1, then it

is;

Quoted 1 line

there is no need to iterate through the memhdr list.

If this's true now, however, this assumption may not hold true in the future code change, iterating through the list may is a safer way as it carefully checks the virtual address without relying on any condition.

If there is exactly one memory chunk, it is virtual contiguous. It has one address and one length, so it must be.

If there are more than one memory chunk, I consider it unlikely that they are contiguous. Have you ever observed the opposite, i.e. a mempool with multiple memory chunks being virtual contiguous?

Iterating through the list does not seem safer to me, quite the opposite. Which future change are you trying to prepare for?

Keeping it simple is more likely to not break with future changes.

author
#14 lore raw
Quoted 22 lines

-----Original Message-----
From: Morten Brørup <[email protected]>
Sent: Thursday, May 23, 2024 3:41 PM
To: Du, Frank <[email protected]>; Ferruh Yigit <[email protected]>; [email protected]; Andrew Rybchenko <[email protected]>; Burakov, Anatoly <[email protected]>
Cc: Loftus, Ciara <[email protected]>
Subject: RE: [PATCH v2] net/af_xdp: fix umem map size for zero copy

Quoted 13 lines

From: Du, Frank [mailto:[email protected]]
Sent: Thursday, 23 May 2024 08.56

Quoted 10 lines

From: Morten Brørup <[email protected]>
Sent: Wednesday, May 22, 2024 3:27 PM

Quoted 7 lines

From: Du, Frank [mailto:[email protected]]
Sent: Wednesday, 22 May 2024 03.25

Quoted 4 lines

From: Ferruh Yigit <[email protected]>
Sent: Wednesday, May 22, 2024 1:58 AM

On 5/11/2024 6:26 AM, Frank Du wrote:

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

The current calculation assumes that the mbufs are contiguous. However, this assumption is incorrect when the memory spans across a huge

page.

What does "the memory spans across a huge page" mean?

Should it be "the memory spans across multiple memory chunks"?

This does not pertain to multiple memory chunks but rather to mbuf memory. The scenario involves a single memory chunk utilizing multiple 2M pages. To ensure that each mbuf resides exclusively within a single page, there are deliberate spacing gaps when allocating mbufs across the 2M page boundaries.

Quoted 107 lines
Quoted 61 lines
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines

Correct to directly read the size from the mempool memory chunks.

---
v2:
* Add virtual contiguous detect for for multiple memhdrs.

---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 34
++++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 6 deletions(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +4 -2
index 268a130c49..7456108d6d 100644
@@ -1039,16 +1039,35 @@ eth_link_update(struct rte_eth_dev
 *dev __rte_unused,  }
 
 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
-static inline uintptr_t get_base_addr(struct rte_mempool *mp,
 uint64_t *align)
+static inline uintptr_t get_memhdr_info(struct rte_mempool
+*mp, uint64_t *align, size_t *len)
 {
-	struct rte_mempool_memhdr *memhdr;
+	struct rte_mempool_memhdr *memhdr, *next;
 	uintptr_t memhdr_addr, aligned_addr;
+	size_t memhdr_len = 0;
 
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1050

Quoted 3 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
+	/* get the mempool base addr and align */
 	memhdr = STAILQ_FIRST(&mp->mem_list);
 	memhdr_addr = (uintptr_t)memhdr->addr;

This is not a new bug; but if the mempool is not populated, memhdr is NULL

here.

Thanks, will add a check later.

Quoted 12 lines
Quoted 12 lines
Quoted 7 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1069

Quoted 4 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +0 -0
 	aligned_addr = memhdr_addr & ~(getpagesize() - 1);
 	*align = memhdr_addr - aligned_addr;
 

I am aware this is not part of this patch, but as note, can't we use 'RTE_ALIGN_FLOOR' to calculate aligned address.

Sure, will use RTE_ALIGN_FLOOR in next version.

Quoted 2 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused,

Quoted 6 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +6 -0
+	memhdr_len += memhdr->len;
+
+	/* check if virtual contiguous memory for multiple memhdrs */
+	next = STAILQ_NEXT(memhdr, next);
+	while (next != NULL) {
+		if ((uintptr_t)next->addr != (uintptr_t)memhdr->addr +

memhdr-

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev · line 1063

Quoted 2 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
 len) {
+			AF_XDP_LOG(ERR, "memory chunks not virtual

contiguous, "

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev · line 1064

Quoted 1 line
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
+					"next: %p, cur: %p(len: %"

PRId64

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

" )\n",

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev · line 1065

Quoted 1 line
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
+					next->addr, memhdr->addr,

memhdr-

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1063

Quoted 4 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +2 -0
 len);
+			return 0;
+		}

Isn't there a mempool flag that can help us figure out mempool is not IOVA contiguous? Isn't it sufficient on its own?

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Indeed, what we need to ascertain is whether it's contiguous in CPU virtual space, not IOVA. I haven't come across a flag specifically for CPU virtual contiguity. The major limitation in XDP is XSK UMEM only supports registering a single contiguous virtual

memory area.

Quoted 8 lines
Quoted 7 lines

I would assume that the EAL memory manager merges free memory into contiguous chunks whenever possible.
@Anatoly, please confirm?

If my assumption is correct, it means that if mp->nb_mem_chunks != 1, then

the

Quoted 8 lines
Quoted 2 lines

mempool is not virtual contiguous. And if mp->nb_mem_chunks == 1, then it

is;

Quoted 1 line

there is no need to iterate through the memhdr list.

If this's true now, however, this assumption may not hold true in the future code change, iterating through the list may is a safer way as it carefully checks the virtual address without relying on any condition.

If there is exactly one memory chunk, it is virtual contiguous. It has one address and one length, so it must be.

If there are more than one memory chunk, I consider it unlikely that they are contiguous.
Have you ever observed the opposite, i.e. a mempool with multiple memory chunks being virtual contiguous?

Quoted 4 lines

Iterating through the list does not seem safer to me, quite the opposite. Which future change are you trying to prepare for?

Keeping it simple is more likely to not break with future changes.

No, I haven't encountered a mempool with multiple memory chunks actually, not know how to construct such mempool. The initial approach was to return an error if multiple chunks were detected, and the iteration method was introduced later. I can revert to the original, simpler way.

#15 lore raw
Quoted 164 lines

-----Original Message-----
From: Du, Frank <[email protected]>
Sent: Thursday, May 23, 2024 8:56 AM
To: Morten Brørup <[email protected]>; Ferruh Yigit <[email protected]>; [email protected]; Andrew Rybchenko <[email protected]>; Burakov, Anatoly <[email protected]>
Cc: Loftus, Ciara <[email protected]>
Subject: RE: [PATCH v2] net/af_xdp: fix umem map size for zero copy

Quoted 5 lines

-----Original Message-----
From: Morten Brørup <[email protected]>
Sent: Thursday, May 23, 2024 3:41 PM
To: Du, Frank <[email protected]>; Ferruh Yigit <[email protected]>; [email protected]; Andrew Rybchenko <[email protected]>;

Burakov,

Quoted 17 lines

Anatoly <[email protected]>
Cc: Loftus, Ciara <[email protected]>
Subject: RE: [PATCH v2] net/af_xdp: fix umem map size for zero copy

Quoted 13 lines

From: Du, Frank [mailto:[email protected]]
Sent: Thursday, 23 May 2024 08.56

Quoted 10 lines

From: Morten Brørup <[email protected]>
Sent: Wednesday, May 22, 2024 3:27 PM

Quoted 7 lines

From: Du, Frank [mailto:[email protected]]
Sent: Wednesday, 22 May 2024 03.25

Quoted 4 lines

From: Ferruh Yigit <[email protected]>
Sent: Wednesday, May 22, 2024 1:58 AM

On 5/11/2024 6:26 AM, Frank Du wrote:

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

The current calculation assumes that the mbufs are contiguous. However, this assumption is incorrect when the memory spans across a huge

page.

What does "the memory spans across a huge page" mean?

Should it be "the memory spans across multiple memory chunks"?

This does not pertain to multiple memory chunks but rather to mbuf memory. The scenario involves a single memory chunk utilizing multiple 2M pages. To ensure that each mbuf resides exclusively within a single page, there are deliberate spacing gaps when allocating mbufs across the 2M page boundaries.

Quoted 50 lines
Quoted 50 lines
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines

Correct to directly read the size from the mempool memory chunks.

---
v2:
* Add virtual contiguous detect for for multiple memhdrs.

---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 34
++++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 6 deletions(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +4 -2
index 268a130c49..7456108d6d 100644
@@ -1039,16 +1039,35 @@ eth_link_update(struct rte_eth_dev
 *dev __rte_unused,  }
 
 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
-static inline uintptr_t get_base_addr(struct rte_mempool *mp,
 uint64_t *align)
+static inline uintptr_t get_memhdr_info(struct rte_mempool
+*mp, uint64_t *align, size_t *len)
 {
-	struct rte_mempool_memhdr *memhdr;
+	struct rte_mempool_memhdr *memhdr, *next;
 	uintptr_t memhdr_addr, aligned_addr;
+	size_t memhdr_len = 0;
 
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev · line 1052

Quoted 3 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
+	/* get the mempool base addr and align */
 	memhdr = STAILQ_FIRST(&mp->mem_list);
 	memhdr_addr = (uintptr_t)memhdr->addr;

This is not a new bug; but if the mempool is not populated, memhdr is NULL

here.

Thanks, will add a check later.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev · line 1055

Quoted 4 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +0 -0
 	aligned_addr = memhdr_addr & ~(getpagesize() - 1);
 	*align = memhdr_addr - aligned_addr;
 

I am aware this is not part of this patch, but as note, can't we use 'RTE_ALIGN_FLOOR' to calculate aligned address.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Sure, will use RTE_ALIGN_FLOOR in next version.

Quoted 5 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev · line 1058

Quoted 5 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +3 -0
+	memhdr_len += memhdr->len;
+
+	/* check if virtual contiguous memory for multiple memhdrs

*/

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev · line 1061

Quoted 3 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +3 -0
+	next = STAILQ_NEXT(memhdr, next);
+	while (next != NULL) {
+		if ((uintptr_t)next->addr != (uintptr_t)memhdr->addr

+

Quoted 3 lines
Quoted 3 lines

memhdr-

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev · line 1063

Quoted 2 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
 len) {
+			AF_XDP_LOG(ERR, "memory chunks not

virtual

Quoted 29 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

contiguous, "

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev · line 1064

Quoted 1 line
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
+					"next: %p, cur: %p(len: %"

PRId64

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

" )\n",

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev · line 1065

Quoted 1 line
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
+					next->addr, memhdr->addr,

memhdr-

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev · line 1068

Quoted 4 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +2 -0
 len);
+			return 0;
+		}

Isn't there a mempool flag that can help us figure out mempool is not IOVA contiguous? Isn't it sufficient on its own?

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Indeed, what we need to ascertain is whether it's contiguous in CPU virtual space, not IOVA. I haven't come across a flag specifically for CPU virtual contiguity. The major limitation in XDP is XSK UMEM only supports registering a single contiguous virtual

memory area.

Quoted 10 lines
Quoted 7 lines

I would assume that the EAL memory manager merges free memory into contiguous chunks whenever possible.
@Anatoly, please confirm?

If my assumption is correct, it means that if mp->nb_mem_chunks != 1, then

the

Quoted 2 lines

mempool is not virtual contiguous. And if mp->nb_mem_chunks == 1, then it

Quoted 8 lines
Quoted 6 lines

is;

Quoted 1 line

there is no need to iterate through the memhdr list.

If this's true now, however, this assumption may not hold true in the future code change, iterating through the list may is a safer way as it carefully checks the virtual address without relying on any condition.

If there is exactly one memory chunk, it is virtual contiguous. It has one

address

Quoted 10 lines

and one length, so it must be.

If there are more than one memory chunk, I consider it unlikely that they are contiguous.
Have you ever observed the opposite, i.e. a mempool with multiple memory chunks being virtual contiguous?

Iterating through the list does not seem safer to me, quite the opposite. Which future change are you trying to prepare for?

Quoted 6 lines
Quoted 1 line

Keeping it simple is more likely to not break with future changes.

No, I haven't encountered a mempool with multiple memory chunks actually, not know how to construct such mempool. The initial approach was to return an error if multiple chunks were detected, and the iteration method was introduced later. I can revert to the original, simpler way.

The mempool created in my (virtualized) test environment always has multiple memory chunks and the iterative check for virtual contiguity in v2 of this patch succeeds for me. However in v4, since mp->nb_mem_chunks != 1, it will fail for me.
So it appears that virtual contiguity is possible even if mp->nb_mem_chunks != 1 so I don't think we can rely on that value for determining virtual contiguity.

#16 lore raw
Quoted 20 lines

From: Loftus, Ciara [mailto:[email protected]]
Sent: Wednesday, 29 May 2024 14.58

Quoted 17 lines

From: Du, Frank <[email protected]>
Sent: Thursday, May 23, 2024 8:56 AM

Quoted 14 lines

From: Morten Brørup <[email protected]>
Sent: Thursday, May 23, 2024 3:41 PM

Quoted 11 lines

From: Du, Frank [mailto:[email protected]]
Sent: Thursday, 23 May 2024 08.56

Quoted 8 lines

From: Morten Brørup <[email protected]>
Sent: Wednesday, May 22, 2024 3:27 PM

Quoted 5 lines

From: Du, Frank [mailto:[email protected]]
Sent: Wednesday, 22 May 2024 03.25

Quoted 2 lines

From: Ferruh Yigit <[email protected]>
Sent: Wednesday, May 22, 2024 1:58 AM

[...]

Quoted 43 lines
Quoted 36 lines
Quoted 36 lines
Quoted 20 lines
Quoted 14 lines
Quoted 9 lines
Quoted 3 lines

Isn't there a mempool flag that can help us figure out mempool is not IOVA contiguous? Isn't it sufficient on its own?

Indeed, what we need to ascertain is whether it's contiguous in CPU virtual space, not IOVA. I haven't come across a flag specifically for CPU virtual contiguity. The major limitation in XDP is XSK UMEM only supports registering a single contiguous virtual memory area.

I would assume that the EAL memory manager merges free memory into contiguous chunks whenever possible.
@Anatoly, please confirm?

Quoted 5 lines

If my assumption is correct, it means that if mp->nb_mem_chunks != 1, then the
mempool is not virtual contiguous. And if mp->nb_mem_chunks == 1, then it is;
there is no need to iterate through the memhdr list.

Quoted 4 lines

If this's true now, however, this assumption may not hold true in the future code change, iterating through the list may is a safer way as it carefully checks the virtual address without relying on any condition.

If there is exactly one memory chunk, it is virtual contiguous. It has one address and one length, so it must be.

If there are more than one memory chunk, I consider it unlikely that they are contiguous.
Have you ever observed the opposite, i.e. a mempool with multiple memory chunks being virtual contiguous?

Iterating through the list does not seem safer to me, quite the opposite. Which future change are you trying to prepare for?

Quoted 6 lines
Quoted 1 line

Keeping it simple is more likely to not break with future changes.

No, I haven't encountered a mempool with multiple memory chunks actually, not know how to construct such mempool. The initial approach was to return an error if multiple chunks were detected, and the iteration method was introduced later. I can revert to the original, simpler way.

Quoted 7 lines

The mempool created in my (virtualized) test environment always has multiple memory chunks and the iterative check for virtual contiguity in v2 of this patch succeeds for me.
However in v4, since mp->nb_mem_chunks != 1, it will fail for me. So it appears that virtual contiguity is possible even if mp->nb_mem_chunks != 1 so I don't think we can rely on that value for determining virtual contiguity.

Excellent feedback, Ciara!
Once again, reality beats assumptions. Memory management is not easy. :-)

In another thread [1], I have asked Paul Szczepanek to pick up on this, and coordinate directly with Frank Du.
Paul is working on a closely related function in the mempool library, and it makes sense to merge this feature into the function he is providing. Alternatively provide another function in the mempool library.

[1]: https://inbox.dpdk.org/dev/[email protected]/T/#m8bf62c45d12f34659becf965cf101f8456723c94

#17 lore raw
In reply to Du, Frank
Quoted 79 lines · Frank
Quoted 45 lines

-----Original Message-----
From: Ferruh Yigit <[email protected]>
Sent: Wednesday, May 22, 2024 1:58 AM
To: Du, Frank <[email protected]>; [email protected]; Andrew Rybchenko <[email protected]>; Morten Brørup <[email protected]>
Cc: Loftus, Ciara <[email protected]>; Burakov, Anatoly <[email protected]>
Subject: Re: [PATCH v2] net/af_xdp: fix umem map size for zero copy

Quoted 2 lines · Frank Du

The current calculation assumes that the mbufs are contiguous. However, this assumption is incorrect when the memory spans across a huge

page.

Quoted 31 lines

Correct to directly read the size from the mempool memory chunks.

---
v2:
* Add virtual contiguous detect for for multiple memhdrs.

---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 34
++++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 6 deletions(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +4 -2
index 268a130c49..7456108d6d 100644
@@ -1039,16 +1039,35 @@ eth_link_update(struct rte_eth_dev *dev
 __rte_unused,  }
 
 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
-static inline uintptr_t get_base_addr(struct rte_mempool *mp,
 uint64_t *align)
+static inline uintptr_t get_memhdr_info(struct rte_mempool *mp,
+uint64_t *align, size_t *len)
 {
-	struct rte_mempool_memhdr *memhdr;
+	struct rte_mempool_memhdr *memhdr, *next;
 	uintptr_t memhdr_addr, aligned_addr;
+	size_t memhdr_len = 0;
 
Quoted 9 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1069

Quoted 6 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
+	/* get the mempool base addr and align */
 	memhdr = STAILQ_FIRST(&mp->mem_list);
 	memhdr_addr = (uintptr_t)memhdr->addr;
 	aligned_addr = memhdr_addr & ~(getpagesize() - 1);
 	*align = memhdr_addr - aligned_addr;
 

I am aware this is not part of this patch, but as note, can't we use 'RTE_ALIGN_FLOOR' to calculate aligned address.

Sure, will use RTE_ALIGN_FLOOR in next version.

Quoted 13 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused,

Quoted 10 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +7 -0
+	memhdr_len += memhdr->len;
+
+	/* check if virtual contiguous memory for multiple memhdrs */
+	next = STAILQ_NEXT(memhdr, next);
+	while (next != NULL) {
+		if ((uintptr_t)next->addr != (uintptr_t)memhdr->addr + memhdr-
 len) {
+			AF_XDP_LOG(ERR, "memory chunks not virtual

contiguous, "

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev · line 1064

Quoted 1 line
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
+					"next: %p, cur: %p(len: %" PRId64

" )\n",

Quoted 8 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1063

Quoted 5 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +3 -0
+					next->addr, memhdr->addr, memhdr-
 len);
+			return 0;
+		}

Isn't there a mempool flag that can help us figure out mempool is not IOVA contiguous? Isn't it sufficient on its own?

Quoted 2 lines

Indeed, what we need to ascertain is whether it's contiguous in CPU virtual space, not IOVA. I haven't come across a flag specifically for CPU virtual contiguity. The major limitation in XDP is XSK UMEM only supports registering a single contiguous virtual memory area.

'RTE_MEMPOOL_F_NO_IOVA_CONTIG' is the flag I was looking for. This flag being *cleared* implies IOVA contiguous but not sure if it is guaranteed, need to check.

And I may be wrong here, but as far as I remember in IOVA as VA mode, process virtual address and IOVA address are same, so IOVA contiguous is same as contiguous CPU virtual address.

Quoted 47 lines
Quoted 47 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1072

Quoted 12 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +6 -0
+		/* virtual contiguous */
+		memhdr = next;
+		memhdr_len += memhdr->len;
+		next = STAILQ_NEXT(memhdr, next);
+	}
 
+	*len = memhdr_len;
 	return aligned_addr;
 }

This function goes too much details of the mempool object, and any change in mempool details has potential to break this code.

@Andrew, @Morten, do you think does it make sense to have 'rte_mempool_info_get()' kind of function, that provides at least address and length of the mempool, and used here?

This helps to hide internal details and complexity of the mempool for users.

drivers/net/af_xdp/rte_eth_af_xdp.c xsk_umem_info *xdp_umem_configure(struct

Quoted 2 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +0 -0
@@ -1125,6 +1144,7 @@ xsk_umem_info *xdp_umem_configure(struct

pmd_internals *internals,

drivers/net/af_xdp/rte_eth_af_xdp.c xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,

Quoted 8 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -1
 	void *base_addr = NULL;
 	struct rte_mempool *mb_pool = rxq->mb_pool;
 	uint64_t umem_size, align = 0;
+	size_t len = 0;
 
 	if (internals->shared_umem) {
 		if (get_shared_umem(rxq, internals->if_name, &umem) < 0) @@
-1156,10 +1176,12 @@ xsk_umem_info *xdp_umem_configure(struct

pmd_internals *internals,

drivers/net/af_xdp/rte_eth_af_xdp.c xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals, · line 1179

Quoted 9 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -4
 		}
 
 		umem->mb_pool = mb_pool;
-		base_addr = (void *)get_base_addr(mb_pool, &align);
-		umem_size = (uint64_t)mb_pool->populated_size *
-				(uint64_t)usr_config.frame_size +
-				align;
+		base_addr = (void *)get_memhdr_info(mb_pool, &align, &len);

Is this calculation correct if mempool is not already aligned to page size?

Quoted 7 lines
Quoted 4 lines

Like in an example page size is '0x1000', and "memhdr_addr = 0x000a1080" returned aligned address is '0x000a1000', "base_addr = 0x000a1000"

Any access between '0x000a1000' & '0x000a1080' is invalid. Is this expected?

Yes, since the XSK UMEM memory area requires page alignment. However, no need to worry; the memory pointer in the XSK TX/RX descriptor is obtained from the mbuf data area. We don’t have any chance to access the invalid range [0x000a1000: 0x000a1080] here.

Thanks for clarification.

#18 lore raw
Quoted 11 lines

From: Ferruh Yigit [mailto:[email protected]]
Sent: Wednesday, 22 May 2024 12.01

Quoted 7 lines · Frank
Quoted 5 lines

From: Ferruh Yigit <[email protected]>
Sent: Wednesday, May 22, 2024 1:58 AM

Isn't there a mempool flag that can help us figure out mempool is not IOVA contiguous? Isn't it sufficient on its own?

Indeed, what we need to ascertain is whether it's contiguous in CPU virtual

Quoted 8 lines

space, not IOVA. I haven't come across a flag specifically for CPU virtual contiguity. The major limitation in XDP is XSK UMEM only supports registering a single contiguous virtual memory area.

Quoted 1 line

'RTE_MEMPOOL_F_NO_IOVA_CONTIG' is the flag I was looking for. This flag being *cleared* implies IOVA contiguous but not sure if it is guaranteed, need to check.

Wrong.
RTE_MEMPOOL_F_NO_IOVA_CONTIG only relates to individual objects in the pool. This flag being cleared only means that each individual object in the mempool is IOVA contiguous. It does not imply that the entire pool of objects is IOVA contiguous.

Quoted 4 lines

And I may be wrong here, but as far as I remember in IOVA as VA mode, process virtual address and IOVA address are same, so IOVA contiguous is same as contiguous CPU virtual address.

#19 lore raw
Quoted 16 lines · Morten Brørup
Quoted 16 lines

From: Ferruh Yigit [mailto:[email protected]]
Sent: Wednesday, 22 May 2024 12.01

Quoted 7 lines · Frank
Quoted 5 lines

From: Ferruh Yigit <[email protected]>
Sent: Wednesday, May 22, 2024 1:58 AM

Isn't there a mempool flag that can help us figure out mempool is not IOVA contiguous? Isn't it sufficient on its own?

Indeed, what we need to ascertain is whether it's contiguous in CPU virtual

space, not IOVA. I haven't come across a flag specifically for CPU virtual contiguity. The major limitation in XDP is XSK UMEM only supports registering a single contiguous virtual memory area.

Quoted 1 line
Quoted 8 lines
Quoted 3 lines

'RTE_MEMPOOL_F_NO_IOVA_CONTIG' is the flag I was looking for. This flag being *cleared* implies IOVA contiguous but not sure if it is guaranteed, need to check.

Wrong.
RTE_MEMPOOL_F_NO_IOVA_CONTIG only relates to individual objects in the pool. This flag being cleared only means that each individual object in the mempool is IOVA contiguous. It does not imply that the entire pool of objects is IOVA contiguous.

Ah, thanks for clarification.

author
#20 lore raw

PATCH v3 1/1 net/af_xdp: fix umem map size for zero copy

In reply to Frank Du

The current calculation assumes that the mbufs are contiguous. However, this assumption is incorrect when the memory spans across a huge page. Correct to directly read the size from the mempool memory chunks.

Fixes d8a210774e1d ("net/af_xdp: support unaligned umem chunks")

---
v2:
* Add virtual contiguous detect for for multiple memhdrs. v3:
* Use RTE_ALIGN_FLOOR to get the aligned addr
* Add check on the first memhdr of memory chunks

---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 40 ++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 7 deletions(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +33 -7
index 6ba455bb9b..986665d1d4 100644
@@ -1040,16 +1040,39 @@ eth_link_update(struct rte_eth_dev *dev __rte_unused,
 }
 
 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
-static inline uintptr_t get_base_addr(struct rte_mempool *mp, uint64_t *align)
+static inline uintptr_t get_memhdr_info(struct rte_mempool *mp, uint64_t *align, size_t *len)
 {
-	struct rte_mempool_memhdr *memhdr;
+	struct rte_mempool_memhdr *memhdr, *next;
 	uintptr_t memhdr_addr, aligned_addr;
+	size_t memhdr_len = 0;
 
+	/* get the mempool base addr and align */
 	memhdr = STAILQ_FIRST(&mp->mem_list);
+	if (!memhdr) {
+		AF_XDP_LOG(ERR, "The mempool is not populated\n");
+		return 0;
+	}
 	memhdr_addr = (uintptr_t)memhdr->addr;
-	aligned_addr = memhdr_addr & ~(getpagesize() - 1);
+	aligned_addr = RTE_ALIGN_FLOOR(memhdr_addr, getpagesize());
 	*align = memhdr_addr - aligned_addr;
+	memhdr_len += memhdr->len;
+
+	/* check if virtual contiguous memory for multiple memhdrs */
+	next = STAILQ_NEXT(memhdr, next);
+	while (next) {
+		if ((uintptr_t)next->addr != (uintptr_t)memhdr->addr + memhdr->len) {
+			AF_XDP_LOG(ERR, "Memory chunks not virtual contiguous, "
+					"next: %p, cur: %p(len: %" PRId64 " )\n",
+					next->addr, memhdr->addr, memhdr->len);
+			return 0;
+		}
+		/* virtual contiguous */
+		memhdr = next;
+		memhdr_len += memhdr->len;
+		next = STAILQ_NEXT(memhdr, next);
+	}
 
+	*len = memhdr_len;
 	return aligned_addr;
 }
 
@@ -1126,6 +1149,7 @@ xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
 	void *base_addr = NULL;
 	struct rte_mempool *mb_pool = rxq->mb_pool;
 	uint64_t umem_size, align = 0;
+	size_t len = 0;
 
 	if (internals->shared_umem) {
 		if (get_shared_umem(rxq, internals->if_name, &umem) < 0)
@@ -1157,10 +1181,12 @@ xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
 		}
 
 		umem->mb_pool = mb_pool;
-		base_addr = (void *)get_base_addr(mb_pool, &align);
-		umem_size = (uint64_t)mb_pool->populated_size *
-				(uint64_t)usr_config.frame_size +
-				align;
+		base_addr = (void *)get_memhdr_info(mb_pool, &align, &len);
+		if (!base_addr) {
+			AF_XDP_LOG(ERR, "The memory pool can't be mapped into umem\n");
+			goto err;
+		}
+		umem_size = (uint64_t)len + align;
 
 		ret = xsk_umem__create(&umem->umem, base_addr, umem_size,
 				&rxq->fq, &rxq->cq, &usr_config);
Signature · 1 line
2.34.1
author
#21 lore raw

PATCH v4 1/1 net/af_xdp: fix umem map size for zero copy

In reply to Frank Du

The current calculation assumes that the mbufs are contiguous. However, this assumption is incorrect when the mbuf memory spans across huge page. To ensure that each mbuf resides exclusively within a single page, there are deliberate spacing gaps when allocating mbufs across the boundaries.

Correct to directly read the size from the mempool memory chunk.

Fixes d8a210774e1d ("net/af_xdp: support unaligned umem chunks")

---
v2:
* Add virtual contiguous detect for for multiple memhdrs v3:
* Use RTE_ALIGN_FLOOR to get the aligned addr
* Add check on the first memhdr of memory chunks v4:
* Replace the iterating with simple nb_mem_chunks check

---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 33 +++++++++++++++++++++++------
 1 file changed, 26 insertions(+), 7 deletions(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +26 -7
index 6ba455bb9b..d0431ec089 100644
@@ -1040,16 +1040,32 @@ eth_link_update(struct rte_eth_dev *dev __rte_unused,
 }
 
 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
-static inline uintptr_t get_base_addr(struct rte_mempool *mp, uint64_t *align)
+static inline uintptr_t
+get_memhdr_info(const struct rte_mempool *mp, uint64_t *align, size_t *len)
 {
 	struct rte_mempool_memhdr *memhdr;
 	uintptr_t memhdr_addr, aligned_addr;
 
+	if (mp->nb_mem_chunks != 1) {
+		/*
+		 * The mempool with multiple chunks is not virtual contiguous but
+		 * xsk umem only support single virtual region mapping.
+		 */
+		AF_XDP_LOG(ERR, "The mempool contain multiple %u memory chunks\n",
+				   mp->nb_mem_chunks);
+		return 0;
+	}
+
+	/* Get the mempool base addr and align from the header now */
 	memhdr = STAILQ_FIRST(&mp->mem_list);
+	if (!memhdr) {
+		AF_XDP_LOG(ERR, "The mempool is not populated\n");
+		return 0;
+	}
 	memhdr_addr = (uintptr_t)memhdr->addr;
-	aligned_addr = memhdr_addr & ~(getpagesize() - 1);
+	aligned_addr = RTE_ALIGN_FLOOR(memhdr_addr, getpagesize());
 	*align = memhdr_addr - aligned_addr;
-
+	*len = memhdr->len;
 	return aligned_addr;
 }
 
@@ -1126,6 +1142,7 @@ xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
 	void *base_addr = NULL;
 	struct rte_mempool *mb_pool = rxq->mb_pool;
 	uint64_t umem_size, align = 0;
+	size_t len = 0;
 
 	if (internals->shared_umem) {
 		if (get_shared_umem(rxq, internals->if_name, &umem) < 0)
@@ -1157,10 +1174,12 @@ xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
 		}
 
 		umem->mb_pool = mb_pool;
-		base_addr = (void *)get_base_addr(mb_pool, &align);
-		umem_size = (uint64_t)mb_pool->populated_size *
-				(uint64_t)usr_config.frame_size +
-				align;
+		base_addr = (void *)get_memhdr_info(mb_pool, &align, &len);
+		if (!base_addr) {
+			AF_XDP_LOG(ERR, "The memory pool can't be mapped as umem\n");
+			goto err;
+		}
+		umem_size = (uint64_t)len + align;
 
 		ret = xsk_umem__create(&umem->umem, base_addr, umem_size,
 				&rxq->fq, &rxq->cq, &usr_config);
Signature · 1 line
2.34.1
#22 lore raw
Quoted 7 lines

From: Frank Du [mailto:[email protected]]
Sent: Thursday, 23 May 2024 10.08

The current calculation assumes that the mbufs are contiguous. However, this assumption is incorrect when the mbuf memory spans across huge page. To ensure that each mbuf resides exclusively within a single page, there are deliberate spacing gaps when allocating mbufs across the boundaries.

A agree that this patch is an improvement of what existed previously.
But I still don't understand the patch description. To me, it looks like the patch adds a missing check for contiguous memory, and the patch itself has nothing to do with huge pages. Anyway, if the maintainer agrees with the description, I don't mind not grasping it. ;-)

However, while trying to understand what is happening, I think I found one more (already existing) bug. I will show through an example inline below.

Quoted 51 lines

Correct to directly read the size from the mempool memory chunk.

Fixes d8a210774e1d ("net/af_xdp: support unaligned umem chunks")

---
v2:
* Add virtual contiguous detect for for multiple memhdrs v3:
* Use RTE_ALIGN_FLOOR to get the aligned addr
* Add check on the first memhdr of memory chunks v4:
* Replace the iterating with simple nb_mem_chunks check

---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 33 +++++++++++++++++++++++------
 1 file changed, 26 insertions(+), 7 deletions(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +14 -1
index 6ba455bb9b..d0431ec089 100644
@@ -1040,16 +1040,32 @@ eth_link_update(struct rte_eth_dev *dev __rte_unused,
 }
 
 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
-static inline uintptr_t get_base_addr(struct rte_mempool *mp, uint64_t
 *align)
+static inline uintptr_t
+get_memhdr_info(const struct rte_mempool *mp, uint64_t *align, size_t *len)
 {
 	struct rte_mempool_memhdr *memhdr;
 	uintptr_t memhdr_addr, aligned_addr;
 
+	if (mp->nb_mem_chunks != 1) {
+		/*
+		 * The mempool with multiple chunks is not virtual contiguous but
+		 * xsk umem only support single virtual region mapping.
+		 */
+		AF_XDP_LOG(ERR, "The mempool contain multiple %u memory
 chunks\n",
+				   mp->nb_mem_chunks);
+		return 0;
+	}
+
+	/* Get the mempool base addr and align from the header now */
 	memhdr = STAILQ_FIRST(&mp->mem_list);
+	if (!memhdr) {

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1071

Quoted 10 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +5 -2
+		AF_XDP_LOG(ERR, "The mempool is not populated\n");
+		return 0;
+	}
 	memhdr_addr = (uintptr_t)memhdr->addr;
-	aligned_addr = memhdr_addr & ~(getpagesize() - 1);
+	aligned_addr = RTE_ALIGN_FLOOR(memhdr_addr, getpagesize());
 	*align = memhdr_addr - aligned_addr;
-
+	*len = memhdr->len;
 	return aligned_addr;

On x86_64, the page size is 4 KB = 0x1000.

Let's look at an example where memhdr->addr is not aligned to the page size:

In the example,
memhdr->addr is 0x700100, and
memhdr->len is 0x20000.

Then
aligned_addr becomes 0x700000,
*align becomes 0x100, and
*len becomes 0x20000.

Quoted 17 lines

}

drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
@@ -1126,6 +1142,7 @@ xsk_umem_info *xdp_umem_configure(struct pmd_internals
 *internals,
 	void *base_addr = NULL;
 	struct rte_mempool *mb_pool = rxq->mb_pool;
 	uint64_t umem_size, align = 0;
+	size_t len = 0;
 
 	if (internals->shared_umem) {
drivers/net/af_xdp/rte_eth_af_xdp.c +0 -1
 		if (get_shared_umem(rxq, internals->if_name, &umem) < 0)
@@ -1157,10 +1174,12 @@ xsk_umem_info *xdp_umem_configure(struct pmd_internals
 *internals,
 		}
 
 		umem->mb_pool = mb_pool;
-		base_addr = (void *)get_base_addr(mb_pool, &align);

drivers/net/af_xdp/rte_eth_af_xdp.c xsk_umem_info *xdp_umem_configure(struct pmd_internals · line 1184

Quoted 10 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +6 -3
-		umem_size = (uint64_t)mb_pool->populated_size *
-				(uint64_t)usr_config.frame_size +
-				align;
+		base_addr = (void *)get_memhdr_info(mb_pool, &align, &len);
+		if (!base_addr) {
+			AF_XDP_LOG(ERR, "The memory pool can't be mapped as
 umem\n");
+			goto err;
+		}
+		umem_size = (uint64_t)len + align;

Here, umem_size becomes 0x20100.

Quoted 3 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +0 -0
 		ret = xsk_umem__create(&umem->umem, base_addr, umem_size,

&rxq->fq, &rxq->cq, &usr_config);

Here, xsk_umem__create() is called with the base_address (0x700000) preceding the address of the memory chunk (0x700100). It looks like a bug, causing a buffer underrun. I.e. will it access memory starting at base_address?

If I'm correct, the code should probably do this for alignment instead:

aligned_addr = RTE_ALIGN_CEIL(memhdr_addr, getpagesize()); *align = aligned_addr - memhdr_addr;
umem_size = (uint64_t)len - align;

Disclaimer: I don't know much about the AF_XDP implementation, so maybe I just don't understand what is going on.

Quoted 2 lines

--
2.34.1

#23 lore raw
Quoted 120 lines · Morten Brørup
Quoted 7 lines

From: Frank Du [mailto:[email protected]]
Sent: Thursday, 23 May 2024 10.08

The current calculation assumes that the mbufs are contiguous. However, this assumption is incorrect when the mbuf memory spans across huge page. To ensure that each mbuf resides exclusively within a single page, there are deliberate spacing gaps when allocating mbufs across the boundaries.

A agree that this patch is an improvement of what existed previously.
But I still don't understand the patch description. To me, it looks like the patch adds a missing check for contiguous memory, and the patch itself has nothing to do with huge pages. Anyway, if the maintainer agrees with the description, I don't mind not grasping it. ;-)

However, while trying to understand what is happening, I think I found one more (already existing) bug. I will show through an example inline below.

Quoted 51 lines

Correct to directly read the size from the mempool memory chunk.

Fixes d8a210774e1d ("net/af_xdp: support unaligned umem chunks")

---
v2:
* Add virtual contiguous detect for for multiple memhdrs v3:
* Use RTE_ALIGN_FLOOR to get the aligned addr
* Add check on the first memhdr of memory chunks v4:
* Replace the iterating with simple nb_mem_chunks check

---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 33 +++++++++++++++++++++++------
 1 file changed, 26 insertions(+), 7 deletions(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +14 -1
index 6ba455bb9b..d0431ec089 100644
@@ -1040,16 +1040,32 @@ eth_link_update(struct rte_eth_dev *dev __rte_unused,
 }
 
 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
-static inline uintptr_t get_base_addr(struct rte_mempool *mp, uint64_t
 *align)
+static inline uintptr_t
+get_memhdr_info(const struct rte_mempool *mp, uint64_t *align, size_t *len)
 {
 	struct rte_mempool_memhdr *memhdr;
 	uintptr_t memhdr_addr, aligned_addr;
 
+	if (mp->nb_mem_chunks != 1) {
+		/*
+		 * The mempool with multiple chunks is not virtual contiguous but
+		 * xsk umem only support single virtual region mapping.
+		 */
+		AF_XDP_LOG(ERR, "The mempool contain multiple %u memory
 chunks\n",
+				   mp->nb_mem_chunks);
+		return 0;
+	}
+
+	/* Get the mempool base addr and align from the header now */
 	memhdr = STAILQ_FIRST(&mp->mem_list);
+	if (!memhdr) {

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1071

Quoted 10 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +5 -2
+		AF_XDP_LOG(ERR, "The mempool is not populated\n");
+		return 0;
+	}
 	memhdr_addr = (uintptr_t)memhdr->addr;
-	aligned_addr = memhdr_addr & ~(getpagesize() - 1);
+	aligned_addr = RTE_ALIGN_FLOOR(memhdr_addr, getpagesize());
 	*align = memhdr_addr - aligned_addr;
-
+	*len = memhdr->len;
 	return aligned_addr;

On x86_64, the page size is 4 KB = 0x1000.

Let's look at an example where memhdr->addr is not aligned to the page size:

In the example,
memhdr->addr is 0x700100, and
memhdr->len is 0x20000.

Then
aligned_addr becomes 0x700000,
*align becomes 0x100, and
*len becomes 0x20000.

Quoted 17 lines

}

drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
@@ -1126,6 +1142,7 @@ xsk_umem_info *xdp_umem_configure(struct pmd_internals
 *internals,
 	void *base_addr = NULL;
 	struct rte_mempool *mb_pool = rxq->mb_pool;
 	uint64_t umem_size, align = 0;
+	size_t len = 0;
 
 	if (internals->shared_umem) {
drivers/net/af_xdp/rte_eth_af_xdp.c +0 -1
 		if (get_shared_umem(rxq, internals->if_name, &umem) < 0)
@@ -1157,10 +1174,12 @@ xsk_umem_info *xdp_umem_configure(struct pmd_internals
 *internals,
 		}
 
 		umem->mb_pool = mb_pool;
-		base_addr = (void *)get_base_addr(mb_pool, &align);

drivers/net/af_xdp/rte_eth_af_xdp.c xsk_umem_info *xdp_umem_configure(struct pmd_internals · line 1184

Quoted 10 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +6 -3
-		umem_size = (uint64_t)mb_pool->populated_size *
-				(uint64_t)usr_config.frame_size +
-				align;
+		base_addr = (void *)get_memhdr_info(mb_pool, &align, &len);
+		if (!base_addr) {
+			AF_XDP_LOG(ERR, "The memory pool can't be mapped as
 umem\n");
+			goto err;
+		}
+		umem_size = (uint64_t)len + align;

Here, umem_size becomes 0x20100.

Quoted 1 line
Quoted 6 lines

drivers/net/af_xdp/rte_eth_af_xdp.c xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals, · line 1185

Quoted 2 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +0 -0
 		ret = xsk_umem__create(&umem->umem, base_addr, umem_size,
 				&rxq->fq, &rxq->cq, &usr_config);

Here, xsk_umem__create() is called with the base_address (0x700000) preceding the address of the memory chunk (0x700100). It looks like a bug, causing a buffer underrun. I.e. will it access memory starting at base_address?

I already asked for this on v2, Frank mentioned that area is not accessed and having gap is safe.

Quoted 12 lines

If I'm correct, the code should probably do this for alignment instead:

aligned_addr = RTE_ALIGN_CEIL(memhdr_addr, getpagesize()); *align = aligned_addr - memhdr_addr;
umem_size = (uint64_t)len - align;

Disclaimer: I don't know much about the AF_XDP implementation, so maybe I just don't understand what is going on.

Quoted 2 lines

--
2.34.1

author
#24 lore raw
Quoted 145 lines

-----Original Message-----
From: Ferruh Yigit <[email protected]>
Sent: Thursday, May 23, 2024 9:32 PM
To: Morten Brørup <[email protected]>; Du, Frank <[email protected]>; [email protected]
Cc: Loftus, Ciara <[email protected]>
Subject: Re: [PATCH v4] net/af_xdp: fix umem map size for zero copy

Quoted 5 lines · Morten Brørup
Quoted 5 lines

From: Frank Du [mailto:[email protected]]
Sent: Thursday, 23 May 2024 10.08

The current calculation assumes that the mbufs are contiguous. However, this assumption is incorrect when the mbuf memory spans across

huge page.

Quoted 2 lines
Quoted 2 lines

To ensure that each mbuf resides exclusively within a single page, there are deliberate spacing gaps when allocating mbufs across the

boundaries.

Quoted 9 lines

A agree that this patch is an improvement of what existed previously. But I still don't understand the patch description. To me, it looks like the patch adds a missing check for contiguous memory, and the patch itself has nothing to do with huge pages. Anyway, if the maintainer agrees with the description, I don't mind not grasping it. ;-)

However, while trying to understand what is happening, I think I found one

more (already existing) bug.

Quoted 42 lines

I will show through an example inline below.

Quoted 40 lines

Correct to directly read the size from the mempool memory chunk.

Fixes d8a210774e1d ("net/af_xdp: support unaligned umem chunks")

---
v2:
* Add virtual contiguous detect for for multiple memhdrs v3:
* Use RTE_ALIGN_FLOOR to get the aligned addr
* Add check on the first memhdr of memory chunks v4:
* Replace the iterating with simple nb_mem_chunks check

---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 33
+++++++++++++++++++++++------
 1 file changed, 26 insertions(+), 7 deletions(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +0 -1
index 6ba455bb9b..d0431ec089 100644
@@ -1040,16 +1040,32 @@ eth_link_update(struct rte_eth_dev *dev
 __rte_unused,  }
 
 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
-static inline uintptr_t get_base_addr(struct rte_mempool *mp,

uint64_t
*align)

drivers/net/af_xdp/rte_eth_af_xdp.c +3 -0
+static inline uintptr_t
+get_memhdr_info(const struct rte_mempool *mp, uint64_t *align,
+size_t *len)
 {
 	struct rte_mempool_memhdr *memhdr;
 	uintptr_t memhdr_addr, aligned_addr;
Quoted 3 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused,

Quoted 3 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +3 -0
+	if (mp->nb_mem_chunks != 1) {
+		/*
+		 * The mempool with multiple chunks is not virtual contiguous

but

Quoted 50 lines
Quoted 11 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +3 -0
+		 * xsk umem only support single virtual region mapping.
+		 */
+		AF_XDP_LOG(ERR, "The mempool contain multiple %u memory

chunks\n",

drivers/net/af_xdp/rte_eth_af_xdp.c +6 -0
+				   mp->nb_mem_chunks);
+		return 0;
+	}
+
+	/* Get the mempool base addr and align from the header now */
 	memhdr = STAILQ_FIRST(&mp->mem_list);
+	if (!memhdr) {

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1069

Quoted 10 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +5 -2
+		AF_XDP_LOG(ERR, "The mempool is not populated\n");
+		return 0;
+	}
 	memhdr_addr = (uintptr_t)memhdr->addr;
-	aligned_addr = memhdr_addr & ~(getpagesize() - 1);
+	aligned_addr = RTE_ALIGN_FLOOR(memhdr_addr, getpagesize());
 	*align = memhdr_addr - aligned_addr;
-
+	*len = memhdr->len;
 	return aligned_addr;

On x86_64, the page size is 4 KB = 0x1000.

Let's look at an example where memhdr->addr is not aligned to the page size:

In the example,
memhdr->addr is 0x700100, and
memhdr->len is 0x20000.

Then
aligned_addr becomes 0x700000,
*align becomes 0x100, and
*len becomes 0x20000.

Quoted 15 lines

}

drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
@@ -1126,6 +1142,7 @@ xsk_umem_info *xdp_umem_configure(struct
 pmd_internals *internals,
 	void *base_addr = NULL;
 	struct rte_mempool *mb_pool = rxq->mb_pool;
 	uint64_t umem_size, align = 0;
+	size_t len = 0;
 
 	if (internals->shared_umem) {

if (get_shared_umem(rxq, internals->if_name, &umem) < 0) @@
-1157,10 +1174,12 @@ xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
}

Quoted 8 lines

drivers/net/af_xdp/rte_eth_af_xdp.c xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,

Quoted 8 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +3 -4
 		umem->mb_pool = mb_pool;
-		base_addr = (void *)get_base_addr(mb_pool, &align);
-		umem_size = (uint64_t)mb_pool->populated_size *
-				(uint64_t)usr_config.frame_size +
-				align;
+		base_addr = (void *)get_memhdr_info(mb_pool, &align, &len);
+		if (!base_addr) {
+			AF_XDP_LOG(ERR, "The memory pool can't be mapped

as

Quoted 9 lines

drivers/net/af_xdp/rte_eth_af_xdp.c xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals, · line 1182

Quoted 4 lines

umem\n");

drivers/net/af_xdp/rte_eth_af_xdp.c +3 -0
+			goto err;
+		}
+		umem_size = (uint64_t)len + align;

Here, umem_size becomes 0x20100.

Quoted 2 lines

ret = xsk_umem__create(&umem->umem, base_addr,

umem_size,

Quoted 2 lines
Quoted 1 line

&rxq->fq, &rxq->cq, &usr_config);

Quoted 8 lines
Quoted 1 line

Here, xsk_umem__create() is called with the base_address (0x700000)

preceding the address of the memory chunk (0x700100).

Quoted 1 line

It looks like a bug, causing a buffer underrun. I.e. will it access memory starting

at base_address?

Quoted 1 line

I already asked for this on v2, Frank mentioned that area is not accessed and having gap is safe.

xsk_umem__create() requires a base address that is aligned to a page boundary. And, there is no chance to access the area between 0x700000 and 0x700100, because the memory pointer for each XSK TX/RX descriptor is derived from the mbuf data area.

Quoted 13 lines
Quoted 8 lines

If I'm correct, the code should probably do this for alignment instead:

aligned_addr = RTE_ALIGN_CEIL(memhdr_addr, getpagesize()); *align = aligned_addr - memhdr_addr; umem_size = (uint64_t)len - align;

Disclaimer: I don't know much about the AF_XDP implementation, so maybe I

just don't understand what is going on.

Quoted 4 lines
Quoted 3 lines

--
2.34.1

#25 lore raw
Quoted 151 lines

From: Du, Frank [mailto:[email protected]]
Sent: Friday, 24 May 2024 03.05

Quoted 65 lines

From: Ferruh Yigit <[email protected]>
Sent: Thursday, May 23, 2024 9:32 PM

Quoted 5 lines · Morten Brørup
Quoted 5 lines

From: Frank Du [mailto:[email protected]]
Sent: Thursday, 23 May 2024 10.08

The current calculation assumes that the mbufs are contiguous. However, this assumption is incorrect when the mbuf memory spans across

huge page.

Quoted 2 lines
Quoted 2 lines

To ensure that each mbuf resides exclusively within a single page, there are deliberate spacing gaps when allocating mbufs across the

boundaries.

Quoted 9 lines

A agree that this patch is an improvement of what existed previously. But I still don't understand the patch description. To me, it looks like the patch adds a missing check for contiguous memory, and the patch itself has nothing to do with huge pages. Anyway, if the maintainer agrees with the description, I don't mind not grasping it. ;-)

However, while trying to understand what is happening, I think I found one

more (already existing) bug.

Quoted 42 lines

I will show through an example inline below.

Quoted 40 lines

Correct to directly read the size from the mempool memory chunk.

Fixes d8a210774e1d ("net/af_xdp: support unaligned umem chunks")

---
v2:
* Add virtual contiguous detect for for multiple memhdrs v3:
* Use RTE_ALIGN_FLOOR to get the aligned addr
* Add check on the first memhdr of memory chunks v4:
* Replace the iterating with simple nb_mem_chunks check

---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 33
+++++++++++++++++++++++------
 1 file changed, 26 insertions(+), 7 deletions(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +0 -1
index 6ba455bb9b..d0431ec089 100644
@@ -1040,16 +1040,32 @@ eth_link_update(struct rte_eth_dev *dev
 __rte_unused,  }
 
 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
-static inline uintptr_t get_base_addr(struct rte_mempool *mp,

uint64_t
*align)

drivers/net/af_xdp/rte_eth_af_xdp.c +3 -0
+static inline uintptr_t
+get_memhdr_info(const struct rte_mempool *mp, uint64_t *align,
+size_t *len)
 {
 	struct rte_mempool_memhdr *memhdr;
 	uintptr_t memhdr_addr, aligned_addr;
Quoted 29 lines
Quoted 3 lines

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused,

Quoted 3 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +3 -0
+	if (mp->nb_mem_chunks != 1) {
+		/*
+		 * The mempool with multiple chunks is not virtual contiguous

but

Quoted 25 lines
Quoted 11 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +3 -0
+		 * xsk umem only support single virtual region mapping.
+		 */
+		AF_XDP_LOG(ERR, "The mempool contain multiple %u memory

chunks\n",

drivers/net/af_xdp/rte_eth_af_xdp.c +6 -0
+				   mp->nb_mem_chunks);
+		return 0;
+	}
+
+	/* Get the mempool base addr and align from the header now */
 	memhdr = STAILQ_FIRST(&mp->mem_list);
+	if (!memhdr) {

drivers/net/af_xdp/rte_eth_af_xdp.c eth_link_update(struct rte_eth_dev *dev __rte_unused, · line 1069

Quoted 10 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +5 -2
+		AF_XDP_LOG(ERR, "The mempool is not populated\n");
+		return 0;
+	}
 	memhdr_addr = (uintptr_t)memhdr->addr;
-	aligned_addr = memhdr_addr & ~(getpagesize() - 1);
+	aligned_addr = RTE_ALIGN_FLOOR(memhdr_addr, getpagesize());
 	*align = memhdr_addr - aligned_addr;
-
+	*len = memhdr->len;
 	return aligned_addr;

On x86_64, the page size is 4 KB = 0x1000.

Let's look at an example where memhdr->addr is not aligned to the page

size:

Quoted 42 lines
Quoted 25 lines

In the example,
memhdr->addr is 0x700100, and
memhdr->len is 0x20000.

Then
aligned_addr becomes 0x700000,
*align becomes 0x100, and
*len becomes 0x20000.

Quoted 15 lines

}

drivers/net/af_xdp/rte_eth_af_xdp.c +1 -0
@@ -1126,6 +1142,7 @@ xsk_umem_info *xdp_umem_configure(struct
 pmd_internals *internals,
 	void *base_addr = NULL;
 	struct rte_mempool *mb_pool = rxq->mb_pool;
 	uint64_t umem_size, align = 0;
+	size_t len = 0;
 
 	if (internals->shared_umem) {

if (get_shared_umem(rxq, internals->if_name, &umem) < 0) @@
-1157,10 +1174,12 @@ xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
}

Quoted 8 lines

drivers/net/af_xdp/rte_eth_af_xdp.c xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,

Quoted 8 lines
drivers/net/af_xdp/rte_eth_af_xdp.c +3 -4
 		umem->mb_pool = mb_pool;
-		base_addr = (void *)get_base_addr(mb_pool, &align);
-		umem_size = (uint64_t)mb_pool->populated_size *
-				(uint64_t)usr_config.frame_size +
-				align;
+		base_addr = (void *)get_memhdr_info(mb_pool, &align, &len);
+		if (!base_addr) {
+			AF_XDP_LOG(ERR, "The memory pool can't be mapped

as

Quoted 8 lines

drivers/net/af_xdp/rte_eth_af_xdp.c xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals, · line 1182

Quoted 4 lines

umem\n");

drivers/net/af_xdp/rte_eth_af_xdp.c +3 -0
+			goto err;
+		}
+		umem_size = (uint64_t)len + align;

Here, umem_size becomes 0x20100.

Quoted 1 line
Quoted 7 lines
Quoted 1 line
Quoted 1 line

ret = xsk_umem__create(&umem->umem, base_addr,

umem_size,

Quoted 3 lines
Quoted 1 line

&rxq->fq, &rxq->cq, &usr_config);

Here, xsk_umem__create() is called with the base_address (0x700000)

preceding the address of the memory chunk (0x700100).

Quoted 1 line

It looks like a bug, causing a buffer underrun. I.e. will it access memory

starting

Quoted 3 lines

at base_address?

Quoted 1 line
Quoted 8 lines
Quoted 1 line

I already asked for this on v2, Frank mentioned that area is not accessed

and

Quoted 1 line

having gap is safe.

xsk_umem__create() requires a base address that is aligned to a page boundary. And, there is no chance to access the area between 0x700000 and 0x700100, because the memory pointer for each XSK TX/RX descriptor is derived from the mbuf data area.

OK, thanks for explaining.

Quoted 14 lines
Quoted 14 lines
Quoted 9 lines

If I'm correct, the code should probably do this for alignment instead:

aligned_addr = RTE_ALIGN_CEIL(memhdr_addr, getpagesize()); *align = aligned_addr - memhdr_addr; umem_size = (uint64_t)len - align;

Disclaimer: I don't know much about the AF_XDP implementation, so maybe I

just don't understand what is going on.

Quoted 4 lines
Quoted 3 lines

--
2.34.1

author
#26 lore raw

PATCH v5 1/1 net/af_xdp: parse umem map info from mempool range api

In reply to Frank Du

The current calculation assumes that the mbufs are contiguous. However, this assumption is incorrect when the mbuf memory spans across huge page.

Correct to directly read with mempool get range API.

Fixes d8a210774e1d ("net/af_xdp: support unaligned umem chunks")

---
v2:
* Add virtual contiguous detect for for multiple memhdrs v3:
* Use RTE_ALIGN_FLOOR to get the aligned addr
* Add check on the first memhdr of memory chunks v4:
* Replace the iterating with simple nb_mem_chunks check v5:
* Use rte_mempool_get_mem_range to query the mempool range

---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 42 ++++++++++++++---------------
 1 file changed, 20 insertions(+), 22 deletions(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +20 -22
index 4b282adb03..0bc0d9a55a 100644
@@ -1067,19 +1067,6 @@ eth_link_update(struct rte_eth_dev *dev __rte_unused,
 }
 
 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
-static inline uintptr_t get_base_addr(struct rte_mempool *mp, uint64_t *align)
-{
-	struct rte_mempool_memhdr *memhdr;
-	uintptr_t memhdr_addr, aligned_addr;
-
-	memhdr = STAILQ_FIRST(&mp->mem_list);
-	memhdr_addr = (uintptr_t)memhdr->addr;
-	aligned_addr = memhdr_addr & ~(getpagesize() - 1);
-	*align = memhdr_addr - aligned_addr;
-
-	return aligned_addr;
-}
-
 /* Check if the netdev,qid context already exists */
 static inline bool
 ctx_exists(struct pkt_rx_queue *rxq, const char *ifname,
@@ -1150,9 +1137,10 @@ xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
 		.fill_size = ETH_AF_XDP_DFLT_NUM_DESCS * 2,
 		.comp_size = ETH_AF_XDP_DFLT_NUM_DESCS,
 		.flags = XDP_UMEM_UNALIGNED_CHUNK_FLAG};
-	void *base_addr = NULL;
 	struct rte_mempool *mb_pool = rxq->mb_pool;
-	uint64_t umem_size, align = 0;
+	void *aligned_addr;
+	uint64_t umem_size;
+	struct rte_mempool_mem_range_info range;
 
 	if (internals->shared_umem) {
 		if (get_shared_umem(rxq, internals->if_name, &umem) < 0)
@@ -1184,19 +1172,29 @@ xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
 		}
 
 		umem->mb_pool = mb_pool;
-		base_addr = (void *)get_base_addr(mb_pool, &align);
-		umem_size = (uint64_t)mb_pool->populated_size *
-				(uint64_t)usr_config.frame_size +
-				align;
-
-		ret = xsk_umem__create(&umem->umem, base_addr, umem_size,
+		ret = rte_mempool_get_mem_range(mb_pool, &range);
+		if (ret < 0) {
+			AF_XDP_LOG(ERR, "Failed(%d) to get range from mempool\n", ret);
+			goto err;
+		}
+		if (!range.is_contiguous) {
+			AF_XDP_LOG(ERR, "Can't mapped to umem as mempool is not contiguous\n");
+			goto err;
+		}
+		/*
+		 * umem requires the memory area be page aligned, safe to map with a large area as
+		 * the memory pointer for each XSK TX/RX descriptor is derived from mbuf data area.
+		 */
+		aligned_addr = (void *)RTE_ALIGN_FLOOR((uintptr_t)range.start, getpagesize());
+		umem_size = range.length + RTE_PTR_DIFF(range.start, aligned_addr);
+		ret = xsk_umem__create(&umem->umem, aligned_addr, umem_size,
 				&rxq->fq, &rxq->cq, &usr_config);
 		if (ret) {
 			AF_XDP_LOG(ERR, "Failed to create umem [%d]: [%s]\n",
 				   errno, strerror(errno));
 			goto err;
 		}
-		umem->buffer = base_addr;
+		umem->buffer = aligned_addr;
 
 		if (internals->shared_umem) {
 			umem->max_xsks = mb_pool->populated_size /
Signature · 1 line
2.34.1
Acked-by on v5 1/1
#27 lore raw

RE: [PATCH v5] net/af_xdp: parse umem map info from mempool range api

Quoted 12 lines

From: Frank Du [mailto:[email protected]]

The current calculation assumes that the mbufs are contiguous. However, this assumption is incorrect when the mbuf memory spans across huge page.

Correct to directly read with mempool get range API.

Fixes d8a210774e1d ("net/af_xdp: support unaligned umem chunks")
Quoted 11 lines

---
v2:
* Add virtual contiguous detect for for multiple memhdrs v3:
* Use RTE_ALIGN_FLOOR to get the aligned addr
* Add check on the first memhdr of memory chunks v4:
* Replace the iterating with simple nb_mem_chunks check v5:
* Use rte_mempool_get_mem_range to query the mempool range
---

Acked-by Applied
#28 lore raw

Re: [PATCH v5] net/af_xdp: parse umem map info from mempool range api

Quoted 24 lines · Morten Brørup
Quoted 12 lines

From: Frank Du [mailto:[email protected]]

The current calculation assumes that the mbufs are contiguous. However, this assumption is incorrect when the mbuf memory spans across huge page.

Correct to directly read with mempool get range API.

Fixes d8a210774e1d ("net/af_xdp: support unaligned umem chunks")
Quoted 11 lines

---
v2:
* Add virtual contiguous detect for for multiple memhdrs v3:
* Use RTE_ALIGN_FLOOR to get the aligned addr
* Add check on the first memhdr of memory chunks v4:
* Replace the iterating with simple nb_mem_chunks check v5:
* Use rte_mempool_get_mem_range to query the mempool range
---

Quoted 2 lines

Applied to dpdk-next-net/main, thanks.