[PATCH 11/18] mlx4: use netmem descriptor and APIs for page pool

Byungchul Park
2025-05-23T03:26:27Z

To simplify struct page, the effort to seperate its own descriptor from
struct page is required and the work for page pool is on going.

Use netmem descriptor and APIs for page pool in mlx4 code.

Signed-off-by: Byungchul Park <byungchul@sk.com>
---
drivers/net/ethernet/mellanox/mlx4/en_rx.c | 46 +++++++++++---------
drivers/net/ethernet/mellanox/mlx4/en_tx.c | 8 ++--
drivers/net/ethernet/mellanox/mlx4/mlx4_en.h | 4 +-
3 files changed, 31 insertions(+), 27 deletions(-)

diff --git a/drivers/net/ethernet/mellanox/mlx4/en_rx.c b/drivers/net/ethernet/mellanox/mlx4/en_rx.c
index b33285d755b9..82c24931fa44 100644
--- a/drivers/net/ethernet/mellanox/mlx4/en_rx.c
+++ b/drivers/net/ethernet/mellanox/mlx4/en_rx.c
@@ -62,18 +62,18 @@ static int mlx4_en_alloc_frags(struct mlx4_en_priv *priv,
int i;

for (i = 0; i < priv->num_frags; i++, frags++) {
- if (!frags->page) {
- frags->page = page_pool_alloc_pages(ring->pp, gfp);
- if (!frags->page) {
+ if (!frags->netmem) {
+ frags->netmem = page_pool_alloc_netmems(ring->pp, gfp);
+ if (!frags->netmem) {
ring->alloc_fail++;
return -ENOMEM;
}
- page_pool_fragment_page(frags->page, 1);
+ page_pool_fragment_netmem(frags->netmem, 1);
frags->page_offset = priv->rx_headroom;

ring->rx_alloc_pages++;
}
- dma = page_pool_get_dma_addr(frags->page);
+ dma = page_pool_get_dma_addr_netmem(frags->netmem);
rx_desc->data[i].addr = cpu_to_be64(dma + frags->page_offset);
}
return 0;
@@ -83,10 +83,10 @@ static void mlx4_en_free_frag(const struct mlx4_en_priv *priv,
struct mlx4_en_rx_ring *ring,
struct mlx4_en_rx_alloc *frag)
{
- if (frag->page)
- page_pool_put_full_page(ring->pp, frag->page, false);
+ if (frag->netmem)
+ page_pool_put_full_netmem(ring->pp, frag->netmem, false);
/* We need to clear all fields, otherwise a change of priv->log_rx_info
- * could lead to see garbage later in frag->page.
+ * could lead to see garbage later in frag->netmem.
*/
memset(frag, 0, sizeof(*frag));
}
@@ -440,29 +440,33 @@ static int mlx4_en_complete_rx_desc(struct mlx4_en_priv *priv,
unsigned int truesize = 0;
bool release = true;
int nr, frag_size;
- struct page *page;
+ netmem_ref netmem;
dma_addr_t dma;

/* Collect used fragments while replacing them in the HW descriptors */
for (nr = 0;; frags++) {
frag_size = min_t(int, length, frag_info->frag_size);

- page = frags->page;
- if (unlikely(!page))
+ netmem = frags->netmem;
+ if (unlikely(!netmem))
goto fail;

- dma = page_pool_get_dma_addr(page);
+ dma = page_pool_get_dma_addr_netmem(netmem);
dma_sync_single_range_for_cpu(priv->ddev, dma, frags->page_offset,
frag_size, priv->dma_dir);

- __skb_fill_page_desc(skb, nr, page, frags->page_offset,
+ __skb_fill_netmem_desc(skb, nr, netmem, frags->page_offset,
frag_size);

truesize += frag_info->frag_stride;
if (frag_info->frag_stride == PAGE_SIZE / 2) {
+ struct page *page = netmem_to_page(netmem);
+ atomic_long_t *pp_ref_count =
+ netmem_get_pp_ref_count_ref(netmem);
+
frags->page_offset ^= PAGE_SIZE / 2;
release = page_count(page) != 1 ||
- atomic_long_read(&page->pp_ref_count) != 1 ||
+ atomic_long_read(pp_ref_count) != 1 ||
page_is_pfmemalloc(page) ||
page_to_nid(page) != numa_mem_id();
} else if (!priv->rx_headroom) {
@@ -476,9 +480,9 @@ static int mlx4_en_complete_rx_desc(struct mlx4_en_priv *priv,
release = frags->page_offset + frag_info->frag_size > PAGE_SIZE;
}
if (release) {
- frags->page = NULL;
+ frags->netmem = 0;
} else {
- page_pool_ref_page(page);
+ page_pool_ref_netmem(netmem);
}

nr++;
@@ -719,7 +723,7 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
int nr;

frags = ring->rx_info + (index << priv->log_rx_info);
- va = page_address(frags[0].page) + frags[0].page_offset;
+ va = netmem_address(frags[0].netmem) + frags[0].page_offset;
net_prefetchw(va);
/*
* make sure we read the CQE after we read the ownership bit
@@ -748,7 +752,7 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
/* Get pointer to first fragment since we haven't
* skb yet and cast it to ethhdr struct
*/
- dma = page_pool_get_dma_addr(frags[0].page);
+ dma = page_pool_get_dma_addr_netmem(frags[0].netmem);
dma += frags[0].page_offset;
dma_sync_single_for_cpu(priv->ddev, dma, sizeof(*ethh),
DMA_FROM_DEVICE);
@@ -788,7 +792,7 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
void *orig_data;
u32 act;

- dma = page_pool_get_dma_addr(frags[0].page);
+ dma = page_pool_get_dma_addr_netmem(frags[0].netmem);
dma += frags[0].page_offset;
dma_sync_single_for_cpu(priv->ddev, dma,
priv->frag_info[0].frag_size,
@@ -818,7 +822,7 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
if (likely(!xdp_do_redirect(dev, &mxbuf.xdp, xdp_prog))) {
ring->xdp_redirect++;
xdp_redir_flush = true;
- frags[0].page = NULL;
+ frags[0].netmem = 0;
goto next;
}
ring->xdp_redirect_fail++;
@@ -828,7 +832,7 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
if (likely(!mlx4_en_xmit_frame(ring, frags, priv,
length, cq_ring,
&doorbell_pending))) {
- frags[0].page = NULL;
+ frags[0].netmem = 0;
goto next;
}
trace_xdp_exception(dev, xdp_prog, act);
diff --git a/drivers/net/ethernet/mellanox/mlx4/en_tx.c b/drivers/net/ethernet/mellanox/mlx4/en_tx.c
index 87f35bcbeff8..b564a953da09 100644
--- a/drivers/net/ethernet/mellanox/mlx4/en_tx.c
+++ b/drivers/net/ethernet/mellanox/mlx4/en_tx.c
@@ -354,7 +354,7 @@ u32 mlx4_en_recycle_tx_desc(struct mlx4_en_priv *priv,
struct page_pool *pool = ring->recycle_ring->pp;

/* Note that napi_mode = 0 means ndo_close() path, not budget = 0 */
- page_pool_put_full_page(pool, tx_info->page, !!napi_mode);
+ page_pool_put_full_netmem(pool, tx_info->netmem, !!napi_mode);

return tx_info->nr_txbb;
}
@@ -1191,10 +1191,10 @@ netdev_tx_t mlx4_en_xmit_frame(struct mlx4_en_rx_ring *rx_ring,
tx_desc = ring->buf + (index << LOG_TXBB_SIZE);
data = &tx_desc->data;

- dma = page_pool_get_dma_addr(frame->page);
+ dma = page_pool_get_dma_addr_netmem(frame->netmem);

- tx_info->page = frame->page;
- frame->page = NULL;
+ tx_info->netmem = frame->netmem;
+ frame->netmem = 0;
tx_info->map0_dma = dma;
tx_info->nr_bytes = max_t(unsigned int, length, ETH_ZLEN);

diff --git a/drivers/net/ethernet/mellanox/mlx4/mlx4_en.h b/drivers/net/ethernet/mellanox/mlx4/mlx4_en.h
index ad0d91a75184..3ef9a0a1f783 100644
--- a/drivers/net/ethernet/mellanox/mlx4/mlx4_en.h
+++ b/drivers/net/ethernet/mellanox/mlx4/mlx4_en.h
@@ -213,7 +213,7 @@ enum cq_type {
struct mlx4_en_tx_info {
union {
struct sk_buff *skb;
- struct page *page;
+ netmem_ref netmem;
};
dma_addr_t map0_dma;
u32 map0_byte_count;
@@ -246,7 +246,7 @@ struct mlx4_en_tx_desc {
#define MLX4_EN_CX3_HIGH_ID 0x1005

struct mlx4_en_rx_alloc {
- struct page *page;
+ netmem_ref netmem;
u32 page_offset;
};

--
2.17.1