net: macb: Add XDP support and page pool integration
Subsystems (3)
v6 cover net: macb: Add XDP support and page pool integration
Tested on Raspberry Pi 5.
With rpi5 is not possible to test the !RSC case. Tests are welcome as it potentially changes the offset of the first frame (hw shifts of NET_IP_ALIGN bytes on rx).
All the changes are intended for gem only.
The series consists of two main changes:
- Migration from netdev_alloc_skb() to page pool allocation model, enabling skb recycling.
This also adds support for multi-descriptor frame reception, removing the previous single-descriptor approach and avoiding potentially large contiguous allocations for e.g. jumbo frames with CONFIG_PAGE_SIZE_4KB.
- XDP support: Initial XDP implementation supporting major verdicts (XDP_PASS, XDP_DROP, XDP_REDIRECT, XDP_TX) along with the ndo_xdp_xmit function for packet redirection.
The driver now advertises NETDEV_XDP_ACT_BASIC, NETDEV_XDP_ACT_REDIRECT, NETDEV_XDP_ACT_NDO_XMIT capabilities.
Some numbers (Andrew)
=====================
Initially reported numbers (Mbps, w/ drops on the rx side):
https://lore.kernel.org/netdev/[email protected]/
The numbers are essentially always the same with these two packet sizes. cpu0 doesn't appear overloaded. Not sure if there's a bottlneck at the interface level on my hw, but it seems cpu can handle more.
Second test run (with no drops on the rx side):
tx side: iperf3 sending UDP (256B) packets roughly at line rate (no drops on the receiving side).
rx side:
- fixed cpu frequency
- single rxq on cpu 0
- iperf3 server on cpu 1-3
- average w/ mpstat 20s interval 20s count
Baseline:
Average: CPU %usr %sys %soft %idle Average: all 3.64 15.05 0.87 80.42 Average: 0 0.05 0.15 10.23 89.56 Average: 1 4.20 16.98 0.00 78.81 Average: 2 3.67 15.53 0.00 80.79 Average: 3 4.07 16.83 0.00 79.09
Page pool:
Average: CPU %usr %sys %soft %idle Average: all 3.76 14.91 0.74 80.57 Average: 0 0.05 0.12 7.81 91.98 Average: 1 3.88 15.53 0.00 80.58 Average: 2 4.35 17.23 0.00 78.42 Average: 3 4.22 16.63 0.00 79.11
Previous versions
=================
- v5: https://lore.kernel.org/netdev/[email protected]/
- v4: https://lore.kernel.org/netdev/[email protected]/
- v3: https://lore.kernel.org/netdev/[email protected]/
- v2: https://lore.kernel.org/netdev/[email protected]/
- v1: https://lore.kernel.org/netdev/[email protected]/
- RFC v2: https://lore.kernel.org/netdev/[email protected]/
- RFC v1: https://lore.kernel.org/netdev/[email protected]/
v5 -> v6
========
- Fix typo in commit description (Nicolai Buchwitz)
- Remove double space (Nicolai Buchwitz)
- Old [4/8] was sent separately (Nicolai Buchwitz)
Note: [1/7] slightly changed after conflict resolution. Reviewed-by from Nicolai
was not carried because of that.
v4 -> v5
========
- Call missing macb_tx_lpi_wake() in macb_xdp_submit_frame() (Nicolai Buchwitz)
- Restore old_prog (instead of NULL) in gem_xdp_setup() on macb_open() failure
v3 -> v4
========
- Remove inline from gem_rx_data_len() (Paolo Abeni)
- Rate limit the netdev_{err,warn} in gem_rx() and gem_rx_refill() with to avoid log flooding under error conditions (Paolo Abeni)
- Fix missing newline in netdev_err() for MTU validation in ndo_change_mtu (Paolo Abeni)
v2 -> v3
========
- Drop mog_init_rings() call in macb_open() reintroduced by mistake while rebasing (Jakub Kicinski)
- Move [5/8] down in the series (was [6/8])
- s/void/sk_buff for the skb parameter instead of updating the documentation because of its void type as this seems to better fit the scope of the patch (Jakub Kicinski)
- Move [6/8] down in the series (was [7/8])
- Remove misleading check on tx buff type (MACB_TYPE_SKB) to improve clarity as all buffers are MACB_TYPE_SKB (Jakub Kicinski)
- Update commit message to better fit with the patch content
- Move [7/8] up in the series (was [5/8]) as XDP_TX has now been folded in this patch honoring the advertised features (Jakub Kicinski)
- explicitly return 0 in gem_create_page_pool() instead of err (Jakub Kicinski)
- Revisit gem_xdp_valid_mtu() and related helpers lifting the dependency with
bp->rx_headroom. It now always uses XDP_PACKET_HEADROOM (Jakub Kicinski)
- Use xdp_return_frame() in workqueue context (Jakub Kicinski)
- macb_tx_error_task() now locks tx_ptr_lock (Jakub Kicinski)
- Remove redundant check for clarity (Jakub Kicinski)
- Take into account head adjustment in macb_xdp_submit_frame() while syncing in the XDP_TX path and for the XDP_PASS codepath.
- Some helpers renaming for clarity
- Do not to leave bp->prog pointer in gem_setup_xdp dangling on failure
- Revisit rx_buffer_size to better handle the RSC/non-RSC case
- Return error in ndo_xdp_xmit if the interface is not operating
- Handle packets count in macb_tx_complete() for the xdp case
v1 -> v2
========
- Fix potential NULL pointer dereference in gem_rx() when receiving an unexpected non-starting frame without an active skb (Jakub Kicinski).
- Make sure to release both the pending skb and the current buff in fragment overflow error path (Jakub Kicinski).
- Reuse the existing page pool instead of creating a new one in HRESP error recovery (Jakub Kicinski).
- s/page/buffer/ in netdev_err() print out (Théo Lebrun)
- Ensure pending queue->skb is released in gem_free_rx_buffers() (Théo Lebrun).
- Use NET_SKB_PAD for the headroom when no xdp program is attached
- Add netdev to pp_params (for pp stats)
- Move ip_summed handling when RX_EOF is set as RX_SUM may not be reliable when RX_EOF is not set.
- Sync for device no longer relies on PP_FLAG_DMA_SYNC_DEV reducing the sync size.
- Introduce rx_ip_align field in struct macb
- Fix incorrect DMA direction for page pool when attaching XDP program by restoring close/open sequence as in a previous version (Jakub Kicinski).
- Rename release_buff() to macb_tx_release_buff() for clarity (Théo Lebrun).
- Do not unmap dma for XDP_TX buffers by ensuring tx_buff->mapping is zero for MACB_TYPE_XDP_TX buff_type (Jakub Kicinski).
- Always return memory to pool on xdp_convert_buff_to_frame() failure (Jakub Kicinski).
- Remove skip_xdp label (Théo, this is a bit different from your suggestion, let me know if you have a strong preference here)
RFC v2 -> v1
============
- Removed bp->macbgem_ops.mog_init_rings(bp) call from macb_open()
- Fixed includes (remove unneeded, moved one from header to macb_main.c)
- Reverse xmas tree ordering (gem_rx, gem_rx_refill)
- print_hex_dump_debug() instead of print_hex_dump()
- Replaced rx frame length check with MACB_BIT(RX_EOF) for data_len calculation
- Removed NET_IP_ALIGN handling in rx buffer size calculation
- Updated debug format string to include rx_headroom and total size
- Changed types to unsigned int in helper functions and variable
- Removed unneeded line break
RFC v1 -> RFC v2
================
- Squashed 1/6 and 2/6
- Reworked rx_buffer_size computation. It no longer takes into
accounts extra room.
- A bunch of renaming (rx_offset => rx_headroom, removed MACB_MAX_PAD, MACB_PP_HEADROOM => XDP_PACKET_HEADROOM, data => ptr, xdp_q => xdp_rxq, macb_xdp() => gem_xdp(), macb_xdp_xmit() => gem_xdp_xmit())
- Deduplicated buffer size computation in gem_xdp_valid_mtu()
and gem_xdp_setup()
- gem_xdp_setup() no longer close()/open()
- Renaming from rx_skbuff to rx_buff is now got split in a separate commit
- Open-coded gem_page_pool_get_buff()
- Added missing rx_buff re-initialization in the error path during rx
- Page pool creation failure now fails the device open
- Moved xdp buff preparation inside gem_xdp_run()
- Added missing rcu_access_pointer()
- Turned return value in -EOPNOTSUPP for macb_xdp() on failure
- moved tx_skb to tx_buff renaming to a separate commit
- Removed some unneeded code and set MACB_TYPE_SKB for lp->rm9200_txq[desc].type as well
- Replaced !!addr with a dedicated bool in macb_xdp_submit_frame()
Paolo Valerio (6):
net: macb: rename rx_skbuff into rx_buff
net: macb: Add page pool support handle multi-descriptor frame rx
net: macb: make macb_tx_skb generic
net: macb: generalize tx buffer handling
net: macb: add XDP support for gem
net: macb: introduce ndo_xdp_xmit support
Théo Lebrun (1):
net: macb: move Rx buffers alloc from link up to open
drivers/net/ethernet/cadence/Kconfig | 1 + drivers/net/ethernet/cadence/macb.h | 43 +- drivers/net/ethernet/cadence/macb_main.c | 967 ++++++++++++++++++----- 3 files changed, 782 insertions(+), 229 deletions(-)
Signature · 1 line
2.53.0
PATCH v6 1/7 net: macb: move Rx buffers alloc from link up to open
From: Théo Lebrun <[email protected]>
mog_alloc_rx_buffers(), getting called at open, does not do rx buffer alloc on GEM. The bulk of the work is done by gem_rx_refill() filling up all slots with valid buffers.
gem_rx_refill() is called at link up by gem_init_rings() == bp->macbgem_ops.mog_init_rings().
Move operation to macb_open(), mostly to allow it to fail early and loudly rather than init the device with Rx mostly broken.
About bool fail_early:
- When called from macb_open(), ring init fails as soon as a queue cannot be refilled.
- When called from macb_hresp_error_task(), we do our best to reinit the device: we still iterate over all queues and try refilling all even if a previous queue failed.
--- drivers/net/ethernet/cadence/macb.h | 2 +- drivers/net/ethernet/cadence/macb_main.c | 53 ++++++++++++++++++------ 2 files changed, 41 insertions(+), 14 deletions(-)
drivers/net/ethernet/cadence/macb.h +1 -1
@@ -1195,7 +1195,7 @@ struct macb_queue;struct macb_or_gem_ops {int (*mog_alloc_rx_buffers)(struct macb *bp);void (*mog_free_rx_buffers)(struct macb *bp); -void (*mog_init_rings)(struct macb *bp); +int (*mog_init_rings)(struct macb *bp,bool fail_early);int (*mog_rx)(struct macb_queue *queue,struct napi_struct *napi,int budget); };
drivers/net/ethernet/cadence/macb_main.c +40 -13
143 lines not on this page · raw message
Signature · 1 line
2.53.0
Jakub Kicinski
maintainerRe: [PATCH net-next v6 1/7] net: macb: move Rx buffers alloc from link up to open
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
net: macb: move Rx buffers alloc from link up to open
This commit moves the Rx buffer allocation in the macb driver from link up to open. It allows the initialization to fail early and loudly if the Rx rings cannot be refilled, rather than initializing the device in a broken state.
[ ... ]
drivers/net/ethernet/cadence/macb_main.c static void gem_rx_refill(struct macb_queue *queue) · line 1510
Quoted 11 lines
drivers/net/ethernet/cadence/macb_main.c +1
-0@@ -1504,6 +1505,7 @@ static void gem_rx_refill(struct macb_queue *queue)if (unlikely (!skb)) {netdev_err (bp->dev,"Unable to allocate sk_buff\n" ); + err = -ENOMEM ;break ; }
If dma_map_single() fails shortly after this block, the code frees the allocated skb and breaks out of the loop:
paddr = dma_map_single(&bp->pdev->dev, skb->data,
bp->rx_buffer_size,
DMA_FROM_DEVICE);
if (dma_mapping_error(&bp->pdev->dev, paddr)) {
dev_kfree_skb(skb);
break;
}Does this cause the function to return 0 (success) despite the DMA mapping failure, bypassing the new fail-early design?
[ ... ]
Quoted 14 lines
drivers/net/ethernet/cadence/macb_main.c +7
-3@@ -6108,12 +6127,20 @@ static int __maybe_unused macb_resume(struct device *dev)for (q =0 , queue = bp->queues; q < bp->num_queues; ++q, ++queue) {if (!(bp->caps &MACB_CAPS_MACB_IS_EMAC )) { -if (macb_is_gem (bp)) -gem_init_rx_ring (queue); -else +if (macb_is_gem (bp)) { + err =gem_init_rx_ring (queue); +if (err) { +dev_err (dev,"Unable to init RX ring: %d\n" , err); +return err; + } + }else {
drivers/net/ethernet/cadence/macb_main.c static int __maybe_unused macb_resume(struct device *dev) · line 6146
Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +4
-0macb_init_rx_ring (queue); + } } + } +for (q =0 , queue = bp->queues; q < bp->num_queues; + ++q, ++queue) {napi_enable (&queue->napi_rx);napi_enable (&queue->napi_tx); }
If gem_init_rx_ring() fails and we return err early here, the subsequent loop that calls napi_enable() is skipped.
Since NAPI was disabled during suspend, the NAPI context remains in a disabled state. If the interface is later administratively brought down, .ndo_stop (macb_close) will unconditionally call napi_disable().
Can calling napi_disable() on an already disabled NAPI context cause an infinite loop and a hard kernel deadlock in this scenario?
Signature · 1 line
pw-bot: cr
PATCH v6 2/7 net: macb: rename rx_skbuff into rx_buff
In reply to Paolo ValerioThis is a preparation commit as the field in later patches will no longer accommodate skbuffs, but more generic frames.
--- drivers/net/ethernet/cadence/macb.h | 2 +- drivers/net/ethernet/cadence/macb_main.c | 24 ++++++++++++------------ 2 files changed, 13 insertions(+), 13 deletions(-)
drivers/net/ethernet/cadence/macb.h +1 -1
@@ -1277,7 +1277,7 @@ struct macb_queue {unsigned int rx_tail;unsigned int rx_prepared_head;struct macb_dma_desc *rx_ring; -struct sk_buff **rx_skbuff; +void **rx_buff;void *rx_buffers;struct napi_struct napi_rx;struct queue_stats stats;
drivers/net/ethernet/cadence/macb_main.c +12 -12
@@ -1499,7 +1499,7 @@ static int gem_rx_refill(struct macb_queue *queue) desc =macb_rx_desc (queue, entry); -if (!queue->rx_skbuff[entry]) { +if (!queue->rx_buff[entry]) {/* allocate sk_buff for this free entry in ring */ skb =netdev_alloc_skb (bp->dev, bp->rx_buffer_size);if (unlikely (!skb)) {
@@ -1518,7 +1518,7 @@ static int gem_rx_refill(struct macb_queue *queue)break ; } - queue->rx_skbuff[entry] = skb; + queue->rx_buff[entry] = skb;if (entry == bp->rx_ring_size -1 ) paddr |=MACB_BIT (RX_WRAP );
@@ -1621,7 +1621,7 @@ static int gem_rx(struct macb_queue *queue, struct napi_struct *napi, queue->stats.rx_dropped++;break ; } - skb = queue->rx_skbuff[entry]; + skb = queue->rx_buff[entry];if (unlikely (!skb)) {netdev_err (bp->dev,"inconsistent Rx descriptor chain\n" );
@@ -1630,7 +1630,7 @@ static int gem_rx(struct macb_queue *queue, struct napi_struct *napi,break ; }/* now everything is ready for receiving packet */ - queue->rx_skbuff[entry] =NULL ; + queue->rx_buff[entry] =NULL ; len = ctrl & bp->rx_frm_len_mask;netdev_vdbg (bp->dev,"gem_rx %u (len %u)\n" , entry, len);
@@ -2630,11 +2630,11 @@ static void gem_free_rx_buffers(struct macb *bp)int i;for (q =0 , queue = bp->queues; q < bp->num_queues; ++q, ++queue) { -if (!queue->rx_skbuff) +if (!queue->rx_buff)continue ;for (i =0 ; i < bp->rx_ring_size; i++) { - skb = queue->rx_skbuff[i]; + skb = queue->rx_buff[i];if (!skb)continue ;
@@ -2648,8 +2648,8 @@ static void gem_free_rx_buffers(struct macb *bp) skb =NULL ; } -kfree (queue->rx_skbuff); - queue->rx_skbuff =NULL ; +kfree (queue->rx_buff); + queue->rx_buff =NULL ; } }
@@ -2712,13 +2712,13 @@ static int gem_alloc_rx_buffers(struct macb *bp)for (q =0 , queue = bp->queues; q < bp->num_queues; ++q, ++queue) { size = bp->rx_ring_size *sizeof (struct sk_buff *); - queue->rx_skbuff =kzalloc (size,GFP_KERNEL ); -if (!queue->rx_skbuff) + queue->rx_buff =kzalloc (size,GFP_KERNEL ); +if (!queue->rx_buff)return -ENOMEM ;else netdev_dbg (bp->dev, -"Allocated %d RX struct sk_buff entries at %p\n" , - bp->rx_ring_size, queue->rx_skbuff); +"Allocated %d RX buff entries at %p\n" , + bp->rx_ring_size, queue->rx_buff); }return 0 ; }
Signature · 1 line
2.53.0
Jakub Kicinski
maintainerRe: [PATCH net-next v6 2/7] net: macb: rename rx_skbuff into rx_buff
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
No issues found.
PATCH v6 3/7 net: macb: Add page pool support handle multi-descriptor frame rx
In reply to Paolo ValerioUse the page pool allocator for the data buffers and enable skb recycling support, instead of relying on netdev_alloc_skb allocating the entire skb during the refill.
The patch also add support for receiving network frames that span multiple DMA descriptors in the Cadence MACB/GEM Ethernet driver.
The patch removes the requirement that limited frame reception to a single descriptor (RX_SOF && RX_EOF), also avoiding potential contiguous multi-page allocation for large frames.
--- drivers/net/ethernet/cadence/Kconfig | 1 + drivers/net/ethernet/cadence/macb.h | 5 + drivers/net/ethernet/cadence/macb_main.c | 412 ++++++++++++++++------- 3 files changed, 292 insertions(+), 126 deletions(-)
drivers/net/ethernet/cadence/Kconfig +1 -0
@@ -25,6 +25,7 @@ config MACB depends on PTP_1588_CLOCK_OPTIONAL select PHYLINK select CRC32 + select PAGE_POOL help The Cadence MACB ethernet interface is found on many Atmel AT32 and AT91 parts. This driver also supports the Cadence GEM (Gigabit
drivers/net/ethernet/cadence/macb.h +5 -0
@@ -14,6 +14,7 @@#include <linux/interrupt.h> #include <linux/phy/phy.h> #include <linux/workqueue.h> +#include <net/page_pool/helpers.h> #define MACB_GREGS_NBR 16#define MACB_GREGS_VERSION 2
@@ -1281,6 +1282,8 @@ struct macb_queue {void *rx_buffers;struct napi_struct napi_rx;struct queue_stats stats; +struct page_pool *page_pool; +struct sk_buff *skb; };struct ethtool_rx_fs_item {
@@ -1304,6 +1307,8 @@ struct macb {struct macb_dma_desc *rx_ring_tieoff;dma_addr_t rx_ring_tieoff_dma;size_t rx_buffer_size; +size_t rx_headroom; +unsigned int rx_ip_align;unsigned int rx_ring_size;unsigned int tx_ring_size;
drivers/net/ethernet/cadence/macb_main.c +286 -126
562 lines not on this page · raw message
Signature · 1 line
2.53.0
Simon Horman
reviewerRe: [PATCH net-next v6 3/7] net: macb: Add page pool support handle multi-descriptor frame rx
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
For local reproduction steps see https://netdev-ai.bots.linux.dev/ai-local.html
---
net: macb: Add page pool support handle multi-descriptor frame rx
This commit adds page pool support for the Cadence MACB/GEM Ethernet driver and enables multi-descriptor frame reception. It replaces netdev_alloc_skb() with page pool allocation and removes the limitation that frames must fit in a single descriptor.
Quoted 4 lines
drivers/net/ethernet/cadence/Kconfig +0
-0
[ ... ]
Quoted 4 lines
drivers/net/ethernet/cadence/macb_main.c +0
-0
[ ... ]
Quoted 15 lines
drivers/net/ethernet/cadence/macb_main.c +5
-2@@ -2600,12 +2700,22 @@ static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev)return ret; } -static void macb_init_rx_buffer_size (struct macb *bp,size_t size) +static void macb_init_rx_buffer_size (struct macb *bp,unsigned int mtu) { +unsigned int overhead; +size_t size; +if (!macb_is_gem (bp)) { bp->rx_buffer_size =MACB_RX_BUFFER_SIZE ; }else { - bp->rx_buffer_size =MIN (size,RX_BUFFER_MAX ); + size = mtu +ETH_HLEN +ETH_FCS_LEN ;
drivers/net/ethernet/cadence/macb_main.c static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev) · line 2721
Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +7
-0+ bp->rx_buffer_size =SKB_DATA_ALIGN (size + bp->rx_ip_align); +if (gem_total_rx_buffer_size (bp) >PAGE_SIZE ) { + overhead = bp->rx_headroom + +SKB_DATA_ALIGN (sizeof (struct skb_shared_info )); + bp->rx_buffer_size =rounddown (PAGE_SIZE - overhead, +RX_BUFFER_MULTIPLE ); + }if (bp->rx_buffer_size %RX_BUFFER_MULTIPLE ) { netdev_dbg(bp->dev,
Can this overflow the hardware register field? The old code clamped size against RX_BUFFER_MAX (16320), but the new code only guards against PAGE_SIZE. Looking at the RXBS field in the hardware (GEM_RXBS_SIZE=8 bits at macb.h:356), it can only represent values 0-255, encoding buffer sizes 0-16320 (value * RX_BUFFER_MULTIPLE where RX_BUFFER_MULTIPLE=64).
On systems with large page sizes (e.g., ARM64 with CONFIG_ARM64_64K_PAGES), the PAGE_SIZE guard doesn't trigger for jumbo MTU values. If rx_buffer_size reaches 16384 (SKB_DATA_ALIGN(16320+2)), then buffer_size = 16384/64 = 256, which overflows the 8-bit field to 0 when written via GEM_BF(RXBS, buffer_size) in macb_configure_dma():
drivers/net/ethernet/cadence/macb_main.c:macb_configure_dma() {
buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
...
for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
if (q)
queue_writel(queue, RBQS, buffer_size);
else
dmacfg |= GEM_BF(RXBS, buffer_size);
}
}This would cause the hardware to use an incorrect (zero) buffer size.
Should the code clamp rx_buffer_size to ensure buffer_size stays within 0-255 after the division?
Jakub Kicinski
maintainerRe: [PATCH net-next v6 3/7] net: macb: Add page pool support handle multi-descriptor frame rx
In reply to Paolo ValerioThis is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
Subject: net: macb: Add page pool support handle multi-descriptor frame rx
This patch adds page pool allocator and skb recycling support for the data buffers in the Cadence MACB/GEM Ethernet driver. It also adds support for receiving network frames that span multiple DMA descriptors.
Quoted 3 lines
drivers/net/ethernet/cadence/macb_main.c +0
-0
[ ... ]
drivers/net/ethernet/cadence/macb_main.c static int macb_tx_complete(struct macb_queue *queue, int budget) · line 1522
Quoted 4 lines
drivers/net/ethernet/cadence/macb_main.c +4
-0+static unsigned int gem_total_rx_buffer_size (struct macb *bp) +{ +return SKB_HEAD_ALIGN (bp->rx_buffer_size +NET_SKB_PAD ); +}
When calculating the size for napi_build_skb(), does this code account for the skb_shared_info overhead?
napi_build_skb() places the shared info at the end of the provided fragment size (frag_size - sizeof(skb_shared_info)). Without adding SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) to the total size, the shared info could overlap with the DMA buffer space. This could overwrite packet data and cause an skb_over_panic() on skb_put() for large frames.
[ ... ]
drivers/net/ethernet/cadence/macb_main.c static int gem_rx_refill(struct macb_queue *queue) · line 1549
Quoted 13 lines
drivers/net/ethernet/cadence/macb_main.c +4
-5@@ -1500,25 +1543,26 @@ static int gem_rx_refill(struct macb_queue *queue) desc =macb_rx_desc (queue, entry);if (!queue->rx_buff[entry]) { -/* allocate sk_buff for this free entry in ring */ - skb =netdev_alloc_skb (bp->dev, bp->rx_buffer_size); -if (unlikely (!skb)) { -netdev_err (bp->dev, -"Unable to allocate sk_buff\n" ); + gfp_alloc = napi ?GFP_ATOMIC :GFP_KERNEL ; + page =page_pool_alloc_frag (queue->page_pool, &offset, +gem_total_rx_buffer_size (bp), + gfp_alloc | __GFP_NOWARN);
Can gem_rx_refill() be called concurrently by both the NAPI poll loop and the error recovery workqueue?
The page pool allocator uses a lockless fast-path that requires execution from a single context. Looking at the driver, macb_hresp_error_task() is a workqueue that calls gem_init_rings() which in turn calls gem_rx_refill().
If the workqueue path calls gem_rx_refill() without first stopping NAPI (via napi_disable()), does this risk concurrent access and memory corruption in the page pool ring?
[ ... ]
Quoted 14 lines
drivers/net/ethernet/cadence/macb_main.c +4
-2@@ -2600,12 +2700,22 @@ static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev)return ret; } -static void macb_init_rx_buffer_size (struct macb *bp,size_t size) +static void macb_init_rx_buffer_size (struct macb *bp,unsigned int mtu) { +unsigned int overhead; +size_t size; +if (!macb_is_gem (bp)) { bp->rx_buffer_size =MACB_RX_BUFFER_SIZE ; }else { - bp->rx_buffer_size =MIN (size,RX_BUFFER_MAX );
drivers/net/ethernet/cadence/macb_main.c static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev) · line 2720
Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +8
-0+ size = mtu +ETH_HLEN +ETH_FCS_LEN ; + bp->rx_buffer_size =SKB_DATA_ALIGN (size + bp->rx_ip_align); +if (gem_total_rx_buffer_size (bp) >PAGE_SIZE ) { + overhead = bp->rx_headroom + +SKB_DATA_ALIGN (sizeof (struct skb_shared_info )); + bp->rx_buffer_size =rounddown (PAGE_SIZE - overhead, +RX_BUFFER_MULTIPLE ); + }if (bp->rx_buffer_size %RX_BUFFER_MULTIPLE ) {
Could this calculation result in a page pool allocation request that exceeds PAGE_SIZE?
Because the check for gem_total_rx_buffer_size(bp) > PAGE_SIZE happens before bp->rx_buffer_size is rounded up to RX_BUFFER_MULTIPLE (which occurs just below this block), specific MTU values might pass the initial check but exceed PAGE_SIZE after rounding.
Since the page pool is initialized with order = 0, requests larger than a page will unconditionally fail.
PATCH v6 4/7 net: macb: make macb_tx_skb generic
In reply to Paolo ValerioThe macb_tx_skb structure is renamed to macb_tx_buff with no functional changes.
This is a preparatory step for adding xdp xmit support.
--- drivers/net/ethernet/cadence/macb.h | 8 +- drivers/net/ethernet/cadence/macb_main.c | 112 +++++++++++------------ 2 files changed, 60 insertions(+), 60 deletions(-)
drivers/net/ethernet/cadence/macb.h +4 -4
@@ -965,7 +965,7 @@ struct macb_dma_desc_ptp {/* Scaled PPM fraction */ #define PPM_FRACTION 16 -/* struct macb_tx_skb - data about an skb which is being transmitted +/* struct macb_tx_buff - data about an skb which is being transmitted * @skb: skb currently being transmitted, only set for the last buffer * of the frame * @mapping: DMA address of the skb's fragment buffer
@@ -973,7 +973,7 @@ struct macb_dma_desc_ptp {* @mapped_as_page: true when buffer was mapped with skb_frag_dma_map(), * false when buffer was mapped with dma_map_single() */ -struct macb_tx_skb { +struct macb_tx_buff {struct sk_buff *skb;dma_addr_t mapping;size_t size;
@@ -1267,7 +1267,7 @@ struct macb_queue {spinlock_t tx_ptr_lock;unsigned int tx_head, tx_tail;struct macb_dma_desc *tx_ring; -struct macb_tx_skb *tx_skb; +struct macb_tx_buff *tx_buff;dma_addr_t tx_ring_dma;struct work_struct tx_error_task;bool txubr_pending;
@@ -1345,7 +1345,7 @@ struct macb {phy_interface_t phy_interface;/* AT91RM9200 transmit queue (1 on wire + 1 queued) */ -struct macb_tx_skb rm9200_txq[2 ]; +struct macb_tx_buff rm9200_txq[2 ];unsigned int max_tx_length;u64 ethtool_stats[GEM_STATS_LEN +QUEUE_STATS_LEN *MACB_MAX_QUEUES ];
drivers/net/ethernet/cadence/macb_main.c +56 -56
269 lines not on this page · raw message
Signature · 1 line
2.53.0
Jakub Kicinski
maintainerRe: [PATCH net-next v6 4/7] net: macb: make macb_tx_skb generic
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
net: macb: make macb_tx_skb generic
This commit renames the macb_tx_skb structure to macb_tx_buff with no functional changes. This is done as a preparatory step for adding XDP xmit support.
Quoted 3 lines
drivers/net/ethernet/cadence/macb_main.c +0
-0
[ ... ]
drivers/net/ethernet/cadence/macb_main.c static unsigned int macb_tx_map(struct macb *bp, · line 2410
Quoted 9 lines
drivers/net/ethernet/cadence/macb_main.c +1
-1@@ -2404,12 +2404,12 @@ static unsigned int macb_tx_map(struct macb *bp, }/* Should never happen */ -if (unlikely (!tx_skb)) { +if (unlikely (!tx_buff)) {netdev_err (bp->dev,"BUG! empty skb!\n" );return 0 ; }
Does returning 0 here cause a memory leak?
If macb_tx_map() returns 0 for an empty skb, the caller macb_start_xmit() will assume the packet was successfully enqueued and will not free it. Should this return an error code like -EINVAL instead to trigger the dev_kfree_skb_any() error path?
[ ... ]
drivers/net/ethernet/cadence/macb_main.c static void macb_free_consistent(struct macb *bp) · line 2812
Quoted 8 lines
drivers/net/ethernet/cadence/macb_main.c +2
-2@@ -2808,8 +2808,8 @@ static void macb_free_consistent(struct macb *bp)dma_free_coherent (dev, size, bp->queues[0 ].rx_ring, bp->queues[0 ].rx_ring_dma);for (q =0 , queue = bp->queues; q < bp->num_queues; ++q, ++queue) { -kfree (queue->tx_skb); - queue->tx_skb =NULL ; +kfree (queue->tx_buff); + queue->tx_buff =NULL ;
Is queue->tx_error_task guaranteed to be canceled before we free queue->tx_buff here?
Since macb_close() calls macb_free_consistent() without explicitly calling cancel_work_sync(&queue->tx_error_task), could a pending background error task execute and access the freed array, resulting in a use-after-free?
[ ... ]
drivers/net/ethernet/cadence/macb_main.c static int macb_alloc_consistent(struct macb *bp) · line 2894
Quoted 12 lines
drivers/net/ethernet/cadence/macb_main.c +3
-3@@ -2887,9 +2887,9 @@ static int macb_alloc_consistent(struct macb *bp) queue->rx_ring = rx +macb_rx_ring_size_per_queue (bp) * q; queue->rx_ring_dma = rx_dma +macb_rx_ring_size_per_queue (bp) * q; - size = bp->tx_ring_size *sizeof (struct macb_tx_skb ); - queue->tx_skb =kmalloc (size,GFP_KERNEL ); -if (!queue->tx_skb) + size = bp->tx_ring_size *sizeof (struct macb_tx_buff ); + queue->tx_buff =kmalloc (size,GFP_KERNEL ); +if (!queue->tx_buff) goto out_err; }
This isn't a bug, but would it be better to use kcalloc() or kmalloc_array() here instead of manual size multiplication?
PATCH v6 5/7 net: macb: generalize tx buffer handling
In reply to Paolo ValerioRename struct macb_tx_buff member skb to ptr and introduce macb_tx_buff_type to identify the buffer type macb_tx_buff represents.
Currently the buffer can only be MACB_TYPE_SKB, so the sk_buff case is handled unconditionally in the tx path.
The remaining type handling will be handled by subsequent patches.
This is the last preparatory step for XDP xmit support.
--- drivers/net/ethernet/cadence/macb.h | 25 ++++++++----- drivers/net/ethernet/cadence/macb_main.c | 45 ++++++++++++++---------- 2 files changed, 43 insertions(+), 27 deletions(-)
drivers/net/ethernet/cadence/macb.h +17 -8
@@ -965,19 +965,28 @@ struct macb_dma_desc_ptp {/* Scaled PPM fraction */ #define PPM_FRACTION 16 -/* struct macb_tx_buff - data about an skb which is being transmitted -* @skb: skb currently being transmitted, only set for the last buffer -* of the frame -* @mapping: DMA address of the skb's fragment buffer +enum macb_tx_buff_type { +MACB_TYPE_SKB , +MACB_TYPE_XDP_TX , +MACB_TYPE_XDP_NDO , +}; + +/* struct macb_tx_buff - data about an skb or xdp frame which is being +* transmitted. +* @ptr: pointer to skb or xdp frame being transmitted, only set +* for the last buffer for sk_buff +* @mapping: DMA address of the skb's fragment or xdp buffer * @size: size of the DMA mapped buffer * @mapped_as_page: true when buffer was mapped with skb_frag_dma_map(), * false when buffer was mapped with dma_map_single() +* @type: type of buffer (MACB_TYPE_SKB, MACB_TYPE_XDP_TX, MACB_TYPE_XDP_NDO) */ struct macb_tx_buff { -struct sk_buff *skb; -dma_addr_t mapping; -size_t size; -bool mapped_as_page; +void *ptr; +dma_addr_t mapping; +size_t size; +bool mapped_as_page; +enum macb_tx_buff_type type; };/* Hardware-collected statistics. Used when updating the network
drivers/net/ethernet/cadence/macb_main.c +26 -19
121 lines not on this page · raw message
Signature · 1 line
2.53.0
Jakub Kicinski
maintainerRe: [PATCH net-next v6 5/7] net: macb: generalize tx buffer handling
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
net: macb: generalize tx buffer handling
This commit renames the skb member in struct macb_tx_buff to ptr and introduces an enum macb_tx_buff_type. This prepares the driver for XDP xmit support by allowing the buffer to handle both sk_buff and xdp frames.
Quoted 12 lines
drivers/net/ethernet/cadence/macb_main.c +2
-1@@ -1197,7 +1197,8 @@ static int macb_halt_tx(struct macb *bp) bp, TSR); } -static void macb_tx_unmap (struct macb *bp,struct macb_tx_buff *tx_buff,int budget) +static void macb_tx_unmap (struct macb *bp,struct macb_tx_buff *tx_buff, +int budget) {
[ ... ]
drivers/net/ethernet/cadence/macb_main.c static void macb_tx_unmap(struct macb *bp, struct macb_tx_buff *tx_buff, int bud · line 1217
Quoted 8 lines
drivers/net/ethernet/cadence/macb_main.c +3
-3-if (tx_buff->skb) { -napi_consume_skb (tx_buff->skb, budget); - tx_buff->skb =NULL ; +if (tx_buff->ptr) { +napi_consume_skb (tx_buff->ptr, budget); + tx_buff->ptr =NULL ; } }
Does the teardown path iterate over the uncompleted descriptors to call macb_tx_unmap()?
When the interface is closed, the hardware is reset and macb_free_consistent() frees the tx_buff array directly via kfree(queue->tx_buff).
If the teardown path does not iterate from tx_tail to tx_head, will any packets (and future xdp frames) that are currently in the transmit ring when the interface is brought down be permanently leaked, along with their associated DMA mappings?
[ ... ]
drivers/net/ethernet/cadence/macb_main.c static void macb_tx_error_task(struct work_struct *work) · line 1302
Quoted 6 lines
drivers/net/ethernet/cadence/macb_main.c +1
-1@@ -1298,7 +1299,7 @@ static void macb_tx_error_task(struct work_struct *work) desc =macb_tx_desc (queue, tail); ctrl = desc->ctrl; tx_buff =macb_tx_buff (queue, tail); - skb = tx_buff->skb; + skb = tx_buff->ptr;
Is the tx_error_task workqueue canceled before freeing tx_buff during device teardown?
The macb_tx_error_task background task accesses the queue->tx_buff array and the hardware tx_ring. During device teardown, macb_free_consistent() frees queue->tx_buff using kfree().
If cancel_work_sync(&queue->tx_error_task) is not called in the teardown path, could a transmit error schedule this workqueue just before the device is brought down, causing the task to execute after the structures have been destroyed and resulting in a use-after-free?
[ ... ]
drivers/net/ethernet/cadence/macb_main.c static unsigned int macb_tx_map(struct macb *bp, · line 2372
Quoted 7 lines
drivers/net/ethernet/cadence/macb_main.c +2
-1@@ -2365,7 +2368,8 @@ static unsigned int macb_tx_map(struct macb *bp, goto dma_error;/* Save info to properly release resources */ - tx_buff->skb =NULL ; + tx_buff->ptr =NULL ; + tx_buff->type =MACB_TYPE_SKB ;
What happens if this function receives an empty skb and fails to find any fragments?
Looking at the rest of macb_tx_map(), if the packet has no data length, it appears the function logs an error and returns 0:
/* Should never happen */
if (unlikely(!tx_buff)) {
netdev_err(bp->dev, "BUG! empty skb!\n");
return 0;
}Since the caller treats 0 as success, it would skip the error path that frees the skb. As no descriptor was actually queued and tx_head wasn't updated, will the packet never be unmapped or freed by the completion path, causing a permanent memory leak? Should this return a non-zero error code instead?
[ ... ]
drivers/net/ethernet/cadence/macb_main.c static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb, · line 5295
Quoted 9 lines
drivers/net/ethernet/cadence/macb_main.c +2
-1@@ -5283,8 +5289,9 @@ static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb,netif_stop_queue (dev);/* Store packet information (to free when Tx completed) */ - lp->rm9200_txq[desc].skb = skb; + lp->rm9200_txq[desc].ptr = skb; lp->rm9200_txq[desc].size = skb->len; + lp->rm9200_txq[desc].type =MACB_TYPE_SKB ; lp->rm9200_txq[desc].mapping = dma_map_single(&lp->pdev->dev, skb->data,
Can a DMA mapping error here cause a permanent queue hang?
If dma_mapping_error() fails just below this, the driver calls dev_kfree_skb_any(skb) and returns NETDEV_TX_OK but leaves the queue stopped via netif_stop_queue(dev).
Because no transmission is started, the hardware will never generate a completion interrupt, leaving the queue permanently hung.
Additionally, ptr is left pointing to the freed memory, creating a dangling pointer. Does this need to be cleared when the error occurs?
PATCH v6 6/7 net: macb: add XDP support for gem
In reply to Paolo ValerioIntroduce basic XDP support for macb/gem with the XDP_TX, XDP_PASS, XDP_DROP, XDP_REDIRECT verdict support.
--- drivers/net/ethernet/cadence/macb.h | 3 + drivers/net/ethernet/cadence/macb_main.c | 366 ++++++++++++++++++++--- 2 files changed, 335 insertions(+), 34 deletions(-)
drivers/net/ethernet/cadence/macb.h +3 -0
@@ -15,6 +15,7 @@#include <linux/phy/phy.h> #include <linux/workqueue.h> #include <net/page_pool/helpers.h> +#include <net/xdp.h> #define MACB_GREGS_NBR 16#define MACB_GREGS_VERSION 2
@@ -1293,6 +1294,7 @@ struct macb_queue {struct queue_stats stats;struct page_pool *page_pool;struct sk_buff *skb; +struct xdp_rxq_info xdp_rxq; };struct ethtool_rx_fs_item {
@@ -1398,6 +1400,7 @@ struct macb {struct macb_pm_data pm_data;const struct macb_usrio_config *usrio; +struct bpf_prog __rcu *prog; };#ifdef CONFIG_MACB_USE_HWSTAMP
drivers/net/ethernet/cadence/macb_main.c +332 -34
608 lines not on this page · raw message
Signature · 1 line
2.53.0
Re: [PATCH net-next v6 6/7] net: macb: add XDP support for gem
Quoted 12 lines
drivers/net/ethernet/cadence/macb_main.c +3
-3- addr += first_frame ? bp->rx_ip_align :0 ; - -dma_sync_single_for_cpu (&bp->pdev->dev, addr, data_len, +dma_sync_single_for_cpu (&bp->pdev->dev, + addr + (first_frame ? bp->rx_ip_align :0 ), + data_len,page_pool_get_dma_dir (queue->page_pool));if (first_frame) {
drivers/net/ethernet/cadence/macb_main.c static int gem_rx(struct macb_queue *queue, struct napi_struct *napi, · line 1885
Quoted 8 lines
drivers/net/ethernet/cadence/macb_main.c +5
-0@@ -1683,6 +1878,18 @@ static int gem_rx(struct macb_queue *queue, struct napi_struct *napi, queue->stats.rx_dropped++; } + headroom = bp->rx_headroom; + +if (last_frame) { + ret =gem_xdp_run (queue, buff_head, &data_len, + &headroom, addr -gem_rx_pad (bp));
Just to clarify, Is xdp support only available for single-buff case? Is it possible to receive multi-buff frames? If yes, should these be allowed to bypass XDP or should these be dropped? The later case would probably require explicit handling?
Jakub Kicinski
maintainerRe: [PATCH net-next v6 6/7] net: macb: add XDP support for gem
In reply to Paolo ValerioThis is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
net: macb: add XDP support for gem
This commit introduces basic XDP support for macb/gem with the XDP_TX, XDP_PASS, XDP_DROP, and XDP_REDIRECT verdict support.
Quoted 4 lines
drivers/net/ethernet/cadence/macb_main.c +0
-0
[ ... ]
Quoted 5 lines
drivers/net/ethernet/cadence/macb_main.c +1
-0@@ -1197,6 +1198,18 @@ static int macb_halt_tx(struct macb *bp) bp, TSR); } +static void macb_tx_release_buff (void *buff,enum macb_tx_buff_type type,int budget)
drivers/net/ethernet/cadence/macb_main.c static int macb_halt_tx(struct macb *bp) · line 1211
Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +10
-0+{ +if (type ==MACB_TYPE_SKB ) { +napi_consume_skb (buff, budget); + }else { +if (!budget) +xdp_return_frame (buff); +else +xdp_return_frame_rx_napi (buff); + } +}
Does this code cause memory corruption in the page pool lockless cache? This function is called from macb_tx_complete(), which executes in the context of the TX NAPI poll routine (queue->napi_tx).
However, the page pool was created and bound to the RX NAPI context (queue->napi_rx). Since xdp_return_frame_rx_napi() bypasses safety checks and places the page directly into the pool's lockless cache, and these are independent NAPI instances that can be scheduled concurrently on different CPUs, shouldn't this use xdp_return_frame() to safely handle cross-CPU synchronization?
[ ... ]
Quoted 3 lines
drivers/net/ethernet/cadence/macb_main.c +3
-0+static int macb_xdp_submit_frame (struct macb *bp,struct xdp_frame *xdpf, +struct net_device *dev,dma_addr_t addr) +{
[ ... ]
drivers/net/ethernet/cadence/macb_main.c static void discard_partial_frame(struct macb_queue *queue, unsigned int begin, · line 1717
Quoted 8 lines
drivers/net/ethernet/cadence/macb_main.c +8
-0+ tx_buff->ptr = xdpf; + tx_buff->type =MACB_TYPE_XDP_TX ; + tx_buff->mapping =0 ; + tx_buff->size = xdpf->len; + tx_buff->mapped_as_page = false; + + ctrl = (u32 )tx_buff->size; + ctrl |=MACB_BIT (TX_LAST );
Could this write overwrite control bits in the descriptor if the XDP frame length exceeds the hardware's maximum limit?
The hardware uses a 14-bit field for the frame length, with higher bits reserved for control flags like TX_LAST and TX_WRAP. On architectures with a PAGE_SIZE larger than 16KB (e.g., ARM64 ZynqMP with 64KB pages), an XDP program can use bpf_xdp_adjust_tail() to legitimately increase the frame length beyond this boundary.
Does this need a bounds check against the hardware's maximum supported transmit size to prevent corrupted DMA operations?
[ ... ]
Quoted 7 lines
drivers/net/ethernet/cadence/macb_main.c +3
-0@@ -1683,6 +1878,18 @@ static int gem_rx(struct macb_queue *queue, struct napi_struct *napi, queue->stats.rx_dropped++; } + headroom = bp->rx_headroom; + +if (last_frame) {
drivers/net/ethernet/cadence/macb_main.c static int gem_rx(struct macb_queue *queue, struct napi_struct *napi, · line 1893
Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +9
-0+ ret =gem_xdp_run (queue, buff_head, &data_len, + &headroom, addr -gem_rx_pad (bp)); +if (ret ==XDP_REDIRECT ) + xdp_flush = true; + +if (ret !=XDP_PASS ) + goto next_frame; + } + queue->skb =napi_build_skb (buff_head,gem_total_rx_buffer_size (bp));
Will this sequence allow multi-descriptor RX packets to silently bypass the XDP program?
If a received packet spans multiple hardware descriptors (first_frame is true but last_frame is false), the code skips gem_xdp_run() completely. It then proceeds to allocate an sk_buff using napi_build_skb(), append subsequent fragments on following loop iterations, and pass the assembled packet to the network stack.
Shouldn't the driver explicitly drop packets that span multiple descriptors when an XDP program is attached?
Jakub Kicinski
maintainerRe: [PATCH net-next v6 6/7] net: macb: add XDP support for gem
In reply to Paolo Valeriodrivers/net/ethernet/cadence/macb_main.c static int macb_set_mac_addr(struct net_device *dev, void *addr) · line 3743
Quoted 9 lines · On Mon, 23 Mar 2026 23:10:46 +0100 Paolo Valerio wrote:
drivers/net/ethernet/cadence/macb_main.c +9
-0+ running =netif_running (dev); + need_update = !!bp->prog != !!prog; +if (running && need_update) +macb_close (dev); + + old_prog =rcu_replace_pointer (bp->prog, prog,lockdep_rtnl_is_held ()); + +if (running && need_update) { + err =macb_open (dev);
Full close + open is not allowed for new code.
PATCH v6 7/7 net: macb: introduce ndo_xdp_xmit support
In reply to Paolo ValerioIntroduce ndo_xdp_xmit function for redirection, update macb_xdp_submit_frame() accordingly, and advertise NETDEV_XDP_ACT_NDO_XMIT capability.
--- drivers/net/ethernet/cadence/macb_main.c | 73 +++++++++++++++++++----- 1 file changed, 58 insertions(+), 15 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +58 -15
@@ -1202,11 +1202,13 @@ static void macb_tx_release_buff(void *buff, enum macb_tx_buff_type type, int bu {if (type ==MACB_TYPE_SKB ) {napi_consume_skb (buff, budget); - }else { + }else if (type ==MACB_TYPE_XDP_TX ) {if (!budget)xdp_return_frame (buff);else xdp_return_frame_rx_napi (buff); + }else { +xdp_return_frame (buff); } }
@@ -1664,20 +1666,24 @@ static void discard_partial_frame(struct macb_queue *queue, unsigned int begin, }static int macb_xdp_submit_frame (struct macb *bp,struct xdp_frame *xdpf, -struct net_device *dev,dma_addr_t addr) +struct net_device *dev,bool dma_map, +dma_addr_t addr) { +enum macb_tx_buff_type buff_type;struct macb_tx_buff *tx_buff;int cpu =smp_processor_id ();struct macb_dma_desc *desc;struct macb_queue *queue;unsigned int next_head;unsigned long flags; +dma_addr_t mapping;u16 queue_index;int err =0 ;u32 ctrl; queue_index = cpu % bp->num_queues; queue = &bp->queues[queue_index]; + buff_type = dma_map ?MACB_TYPE_XDP_NDO :MACB_TYPE_XDP_TX ;spin_lock_irqsave (&queue->tx_ptr_lock, flags);
@@ -1690,14 +1696,23 @@ static int macb_xdp_submit_frame(struct macb *bp, struct xdp_frame *xdpf, goto unlock; } -/* progs can adjust the head. Sync and set the adjusted one. -* This also implicitly takes into account ip alignment, -* if present. -*/ - addr += xdpf->headroom +sizeof (*xdpf); - -dma_sync_single_for_device (&bp->pdev->dev, addr, - xdpf->len,DMA_BIDIRECTIONAL ); +if (dma_map) { + mapping =dma_map_single (&bp->pdev->dev, + xdpf->data, + xdpf->len,DMA_TO_DEVICE ); +if (unlikely (dma_mapping_error (&bp->pdev->dev, mapping))) { + err = -ENOMEM ; + goto unlock; + } + }else { +/* progs can adjust the head. Sync and set the adjusted one. +* This also implicitly takes into account ip alignment, +* if present. +*/ + mapping = addr + xdpf->headroom +sizeof (*xdpf); +dma_sync_single_for_device (&bp->pdev->dev, mapping, + xdpf->len,DMA_BIDIRECTIONAL ); + } next_head = queue->tx_head +1 ;
@@ -1708,8 +1723,8 @@ static int macb_xdp_submit_frame(struct macb *bp, struct xdp_frame *xdpf, desc =macb_tx_desc (queue, queue->tx_head); tx_buff =macb_tx_buff (queue, queue->tx_head); tx_buff->ptr = xdpf; - tx_buff->type =MACB_TYPE_XDP_TX ; - tx_buff->mapping =0 ; + tx_buff->type = buff_type; + tx_buff->mapping = dma_map ? mapping :0 ; tx_buff->size = xdpf->len; tx_buff->mapped_as_page = false;
@@ -1720,7 +1735,7 @@ static int macb_xdp_submit_frame(struct macb *bp, struct xdp_frame *xdpf, ctrl |=MACB_BIT (TX_WRAP );/* Set TX buffer descriptor */ -macb_set_addr (bp, desc, addr); +macb_set_addr (bp, desc, mapping);/* desc->addr must be visible to hardware before clearing * 'TX_USED' bit in desc->ctrl. */
@@ -1745,6 +1760,32 @@ static int macb_xdp_submit_frame(struct macb *bp, struct xdp_frame *xdpf,return err; } +static int gem_xdp_xmit (struct net_device *dev,int num_frame, +struct xdp_frame **frames,u32 flags) +{ +struct macb *bp =netdev_priv (dev); +u32 xmitted =0 ; +int i; + +if (!macb_is_gem (bp)) +return -EOPNOTSUPP ; + +if (unlikely (!netif_carrier_ok (dev))) +return -ENETDOWN ; + +if (unlikely (flags & ~XDP_XMIT_FLAGS_MASK )) +return -EINVAL ; + +for (i =0 ; i < num_frame; i++) { +if (macb_xdp_submit_frame (bp, frames[i], dev, true,0 )) +break ; + + xmitted++; + } + +return xmitted; +} +static u32 gem_xdp_run (struct macb_queue *queue,void *buff_head,unsigned int *len,unsigned int *headroom,dma_addr_t addr)
@@ -1782,7 +1823,7 @@ static u32 gem_xdp_run(struct macb_queue *queue, void *buff_head,case XDP_TX : xdpf =xdp_convert_buff_to_frame (&xdp);if (unlikely (!xdpf) ||macb_xdp_submit_frame (queue->bp, xdpf, - dev, addr)) { + dev, false, addr)) { act =XDP_DROP ;break ; }
@@ -5043,6 +5084,7 @@ static const struct net_device_ops macb_netdev_ops = { .ndo_hwtstamp_get = macb_hwtstamp_get, .ndo_setup_tc = macb_setup_tc, .ndo_bpf = gem_xdp, + .ndo_xdp_xmit = gem_xdp_xmit, };/* Configure peripheral capabilities according to device tree
@@ -6333,7 +6375,8 @@ static int macb_probe(struct platform_device *pdev) bp->rx_ip_align =NET_IP_ALIGN ; dev->xdp_features =NETDEV_XDP_ACT_BASIC | -NETDEV_XDP_ACT_REDIRECT ; +NETDEV_XDP_ACT_REDIRECT | +NETDEV_XDP_ACT_NDO_XMIT ; }netif_carrier_off (dev);
Signature · 1 line
2.53.0
Jakub Kicinski
maintainerRe: [PATCH net-next v6 7/7] net: macb: introduce ndo_xdp_xmit support
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
net: macb: introduce ndo_xdp_xmit support
This commit introduces ndo_xdp_xmit support for the macb driver to enable XDP redirection. It adds the gem_xdp_xmit function and updates macb_xdp_submit_frame to handle DMA mapping for redirected frames.
Quoted 4 lines
drivers/net/ethernet/cadence/macb_main.c +0
-0
[ ... ]
drivers/net/ethernet/cadence/macb_main.c static int macb_xdp_submit_frame(struct macb *bp, struct xdp_frame *xdpf, · line 1728
Quoted 9 lines
drivers/net/ethernet/cadence/macb_main.c +2
-2@@ -1708,8 +1723,8 @@ static int macb_xdp_submit_frame(struct macb *bp, struct xdp_frame *xdpf, desc =macb_tx_desc (queue, queue->tx_head); tx_buff =macb_tx_buff (queue, queue->tx_head); tx_buff->ptr = xdpf; - tx_buff->type =MACB_TYPE_XDP_TX ; - tx_buff->mapping =0 ; + tx_buff->type = buff_type; + tx_buff->mapping = dma_map ? mapping :0 ; tx_buff->size = xdpf->len;
Is a bounds check needed on the frame length here?
Since ndo_xdp_xmit can receive frames redirected from other devices, the frame length could potentially be up to ~64KB on architectures with 64KB pages.
macb_xdp_submit_frame() casts this length to a 32-bit integer and bitwise-ORs it into the descriptor's control word, which has a 14-bit length field (maximum 16383 bytes). Frame lengths exceeding this limit could silently overflow into adjacent control bits like TX_LAST, corrupting the descriptor ring.
[ ... ]
Quoted 9 lines
drivers/net/ethernet/cadence/macb_main.c +9
-0+static int gem_xdp_xmit (struct net_device *dev,int num_frame, +struct xdp_frame **frames,u32 flags) +{ +struct macb *bp =netdev_priv (dev); +u32 xmitted =0 ; +int i; + +if (!macb_is_gem (bp)) +return -EOPNOTSUPP ;
drivers/net/ethernet/cadence/macb_main.c static int macb_xdp_submit_frame(struct macb *bp, struct xdp_frame *xdpf, · line 1781
Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +10
-0+ +if (unlikely (!netif_carrier_ok (dev))) +return -ENETDOWN ; + +if (unlikely (flags & ~XDP_XMIT_FLAGS_MASK )) +return -EINVAL ; + +for (i =0 ; i < num_frame; i++) { +if (macb_xdp_submit_frame (bp, frames[i], dev, true,0 )) +break ;
Should this loop batch the doorbell rings based on the XDP_XMIT_FLUSH flag?
Currently, the code calls macb_xdp_submit_frame() for each frame, which independently acquires the global bp->lock and performs an MMIO write to the NCR register to ring the hardware doorbell per packet.
Doing this per packet ignores the batching semantics of the bulk transmission API and could cause severe lock contention and MMIO overhead.
Quoted 6 lines
drivers/net/ethernet/cadence/macb_main.c +6
-0+ + xmitted++; + } + +return xmitted; +}
AW: [PATCH net-next v6 7/7] net: macb: introduce ndo_xdp_xmit support
In reply to Paolo ValerioHi,
within the function gem_xdp_xmit, there should be a call to "xdp_return_frame" for each successful processing in "macb_xdp_submit_frame".
Otherwise, this driver does not work properly with Ethernet drivers that XDP_REDIRECT to this driver and use page_pools. In this case, the pages from the pools are not returned to the pool.
Best regards
Mathias
Quoted 26 lines
drivers/net/ethernet/cadence/macb_main.c +26
-0+static int gem_xdp_xmit (struct net_device *dev,int num_frame, + struct xdp_frame **frames, u32 flags) +{ + struct macb *bp = netdev_priv(dev); + u32 xmitted = 0; + int i; + + if (!macb_is_gem(bp)) + return -EOPNOTSUPP; + + if (unlikely(!netif_carrier_ok(dev))) + return -ENETDOWN; + + if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) + return -EINVAL; + + for (i = 0; i < num_frame; i++) { + if (macb_xdp_submit_frame(bp, frames[i], dev, true, 0)) + break; + + xmitted++; + } + + return xmitted; +} +
Re: [PATCH net-next v6 7/7] net: macb: introduce ndo_xdp_xmit support
Hi,
Quoted 4 lines
within the function
gem_xdp_xmit, there should be a call to "xdp_return_frame" for each successful processing in "macb_xdp_submit_frame".
Otherwise, this driver does not work properly with Ethernet drivers thatXDP_REDIRECTto this driver and usepage_pools. In this case, the pages from the pools are not returned to the pool.
Please ignore the complain.
I think the existing code is fine, I found a bug on my side. Sorry for the confusion.
Best regards
Mathias