Skip to contentSkip to conversation

net: ethernet: mtk_eth_soc: use prefetch methods

RFC PATCH Superseded Elad Yifee 2 participants 4 messages Jul 20, 2024 – Jul 22, 2024

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Elad Yifee

author
#0 lore raw

RFC PATCH 1/1 net: ethernet: mtk_eth_soc: use prefetch methods

Utilize kernel prefetch methods for faster cache line access. This change boosts driver performance,
allowing the CPU to handle about 5% more packets/sec.

---
 drivers/net/ethernet/mediatek/mtk_eth_soc.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/net/ethernet/mediatek/mtk_eth_soc.c +6 -1
index 0cc2dd85652f..1a0704166103 100644
@@ -1963,6 +1963,7 @@ static u32 mtk_xdp_run(struct mtk_eth *eth, struct mtk_rx_ring *ring,
 	if (!prog)
 		goto out;
 
+	prefetchw(xdp->data_hard_start);
 	act = bpf_prog_run_xdp(prog, xdp);
 	switch (act) {
 	case XDP_PASS:
@@ -2039,7 +2040,7 @@ static int mtk_poll_rx(struct napi_struct *napi, int budget,
 		idx = NEXT_DESP_IDX(ring->calc_idx, ring->dma_size);
 		rxd = ring->dma + idx * eth->soc->rx.desc_size;
 		data = ring->data[idx];
-
+		prefetch(rxd);
 		if (!mtk_rx_get_desc(eth, &trxd, rxd))
 			break;
 
@@ -2105,6 +2106,7 @@ static int mtk_poll_rx(struct napi_struct *napi, int budget,
 			if (ret != XDP_PASS)
 				goto skip_rx;
 
+			net_prefetch(xdp.data_meta);
 			skb = build_skb(data, PAGE_SIZE);
 			if (unlikely(!skb)) {
 				page_pool_put_full_page(ring->page_pool,
@@ -2113,6 +2115,7 @@ static int mtk_poll_rx(struct napi_struct *napi, int budget,
 				goto skip_rx;
 			}
 
+			prefetchw(skb->data);
 			skb_reserve(skb, xdp.data - xdp.data_hard_start);
 			skb_put(skb, xdp.data_end - xdp.data);
 			skb_mark_for_recycle(skb);
@@ -2143,6 +2146,7 @@ static int mtk_poll_rx(struct napi_struct *napi, int budget,
 			dma_unmap_single(eth->dma_dev, ((u64)trxd.rxd1 | addr64),
 					 ring->buf_size, DMA_FROM_DEVICE);
 
+			net_prefetch(data);
 			skb = build_skb(data, ring->frag_size);
 			if (unlikely(!skb)) {
 				netdev->stats.rx_dropped++;
@@ -2150,6 +2154,7 @@ static int mtk_poll_rx(struct napi_struct *napi, int budget,
 				goto skip_rx;
 			}
 
+			prefetchw(skb->data);
 			skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
 			skb_put(skb, pktlen);
 		}
Signature · 1 line
2.45.2
#1 lore raw
Quoted 3 lines · Elad Yifee

Utilize kernel prefetch methods for faster cache line access. This change boosts driver performance,
allowing the CPU to handle about 5% more packets/sec.

Nit: It'd be great to see before/after numbers and/or an explanation of how you measured this in the commit message.

Quoted 10 lines
---
 drivers/net/ethernet/mediatek/mtk_eth_soc.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/net/ethernet/mediatek/mtk_eth_soc.c +0 -0
index 0cc2dd85652f..1a0704166103 100644

drivers/net/ethernet/mediatek/mtk_eth_soc.c static u32 mtk_xdp_run(struct mtk_eth *eth, struct mtk_rx_ring *ring, · line 1966

Quoted 4 lines
drivers/net/ethernet/mediatek/mtk_eth_soc.c +1 -0
@@ -1963,6 +1963,7 @@ static u32 mtk_xdp_run(struct mtk_eth *eth, struct mtk_rx_ring *ring,
 	if (!prog)
 		goto out;
 
+	prefetchw(xdp->data_hard_start);

Is there any reason to mix net_prefetch (as you have below) with prefetch and prefetchw ?

IMHO: you should consider using net_prefetch and net_prefetchw everywhere instead of using both in your code.

drivers/net/ethernet/mediatek/mtk_eth_soc.c static int mtk_poll_rx(struct napi_struct *napi, int budget, · line 2043

Quoted 9 lines
drivers/net/ethernet/mediatek/mtk_eth_soc.c +1 -1
 	act = bpf_prog_run_xdp(prog, xdp);
 	switch (act) {
 	case XDP_PASS:
@@ -2039,7 +2040,7 @@ static int mtk_poll_rx(struct napi_struct *napi, int budget,
 		idx = NEXT_DESP_IDX(ring->calc_idx, ring->dma_size);
 		rxd = ring->dma + idx * eth->soc->rx.desc_size;
 		data = ring->data[idx];
-
+		prefetch(rxd);

Maybe net_prefetch instead, as mentioned above?

Quoted 12 lines
drivers/net/ethernet/mediatek/mtk_eth_soc.c +1 -0
 		if (!mtk_rx_get_desc(eth, &trxd, rxd))
 			break;
 
@@ -2105,6 +2106,7 @@ static int mtk_poll_rx(struct napi_struct *napi, int budget,
 			if (ret != XDP_PASS)
 				goto skip_rx;
 
+			net_prefetch(xdp.data_meta);
 			skb = build_skb(data, PAGE_SIZE);
 			if (unlikely(!skb)) {
 				page_pool_put_full_page(ring->page_pool,

drivers/net/ethernet/mediatek/mtk_eth_soc.c static int mtk_poll_rx(struct napi_struct *napi, int budget, · line 2118

Quoted 4 lines
drivers/net/ethernet/mediatek/mtk_eth_soc.c +1 -0
@@ -2113,6 +2115,7 @@ static int mtk_poll_rx(struct napi_struct *napi, int budget,
 				goto skip_rx;
 			}
 
+			prefetchw(skb->data);

Maybe net_prefetchw instead, as mentioned above?

Quoted 12 lines
drivers/net/ethernet/mediatek/mtk_eth_soc.c +1 -0
 			skb_reserve(skb, xdp.data - xdp.data_hard_start);
 			skb_put(skb, xdp.data_end - xdp.data);
 			skb_mark_for_recycle(skb);
@@ -2143,6 +2146,7 @@ static int mtk_poll_rx(struct napi_struct *napi, int budget,
 			dma_unmap_single(eth->dma_dev, ((u64)trxd.rxd1 | addr64),
 					 ring->buf_size, DMA_FROM_DEVICE);
 
+			net_prefetch(data);
 			skb = build_skb(data, ring->frag_size);
 			if (unlikely(!skb)) {
 				netdev->stats.rx_dropped++;

drivers/net/ethernet/mediatek/mtk_eth_soc.c static int mtk_poll_rx(struct napi_struct *napi, int budget, · line 2157

Quoted 4 lines
drivers/net/ethernet/mediatek/mtk_eth_soc.c +1 -0
@@ -2150,6 +2154,7 @@ static int mtk_poll_rx(struct napi_struct *napi, int budget,
 				goto skip_rx;
 			}
 
+			prefetchw(skb->data);

Maybe net_prefetchw instead, as mentioned above?

author
#2 lore raw
Quoted 8 lines · Joe Damato
Quoted 3 lines · Elad Yifee

Utilize kernel prefetch methods for faster cache line access. This change boosts driver performance,
allowing the CPU to handle about 5% more packets/sec.

Nit: It'd be great to see before/after numbers and/or an explanation of how you measured this in the commit message.

Sure, I'll add iperf3 results in the next version.

Quoted 5 lines

Is there any reason to mix net_prefetch (as you have below) with prefetch and prefetchw ?

IMHO: you should consider using net_prefetch and net_prefetchw everywhere instead of using both in your code.

You are right, honestly I didn't notice it exists. I'll replace all prefetchw with net_prefetchw.

Quoted 8 lines

drivers/net/ethernet/mediatek/mtk_eth_soc.c static int mtk_poll_rx(struct napi_struct *napi, int budget, · line 2043

Quoted 6 lines
drivers/net/ethernet/mediatek/mtk_eth_soc.c +1 -1
@@ -2039,7 +2040,7 @@ static int mtk_poll_rx(struct napi_struct *napi, int budget,
              idx = NEXT_DESP_IDX(ring->calc_idx, ring->dma_size);
              rxd = ring->dma + idx * eth->soc->rx.desc_size;
              data = ring->data[idx];
-
+             prefetch(rxd);

Maybe net_prefetch instead, as mentioned above?

This is the only case where I think prefetch should be used since it's only the descriptor.

Thank you for your suggestions

#3 lore raw
Quoted 10 lines · Elad Yifee
Quoted 8 lines · Joe Damato
Quoted 3 lines · Elad Yifee

Utilize kernel prefetch methods for faster cache line access. This change boosts driver performance,
allowing the CPU to handle about 5% more packets/sec.

Nit: It'd be great to see before/after numbers and/or an explanation of how you measured this in the commit message.

Sure, I'll add iperf3 results in the next version.

Thanks, that'd be helpful!

[...]

Quoted 10 lines
Quoted 8 lines

drivers/net/ethernet/mediatek/mtk_eth_soc.c static int mtk_poll_rx(struct napi_struct *napi, int budget, · line 2043

Quoted 6 lines
drivers/net/ethernet/mediatek/mtk_eth_soc.c +1 -1
@@ -2039,7 +2040,7 @@ static int mtk_poll_rx(struct napi_struct *napi, int budget,
              idx = NEXT_DESP_IDX(ring->calc_idx, ring->dma_size);
              rxd = ring->dma + idx * eth->soc->rx.desc_size;
              data = ring->data[idx];
-
+             prefetch(rxd);

Maybe net_prefetch instead, as mentioned above?

This is the only case where I think prefetch should be used since it's only the descriptor.

I think you are implying that the optimization in the case of L1_CACHE_BYTES < 128 is unnecessary because because the mtk_rx_dma_v2 descriptors will be too far (i * eth->soc->rx.desc_size) apart to get any benefit from prefetching more data ?

If my understanding is correct, then yes: I agree.

Quoted 1 line

Thank you for your suggestions

No problem!