> Thank you for your contribution! Sashiko AI review found 2 potential > issue(s) to consider. > > Critical: 0 · High: 0 (1) · Medium: 1 · Low: 1 > > If you believe none of the issues are legitimate, please reply to each > issue of Medium severity or higher and explain why the AI is wrong. > If any of them are legitimate and you plan to respin, please let patchwork > know by including "pw-bot: cr" as a separate line at the end of your reply > (one such reply per series is enough). > > - [Medium] TX descriptor buffer-length field overflow on XDP_TX of a > tail-grown fragment. > - [Low] net: stmmac: stale xdp_frags_truesize after > xdp_buff_shrink_tail() > > Pre-existing issues: > - [High] Concurrent lockless page_pool direct recycling from a different > NAPI context. > > -- > > net: stmmac: rework stmmac_rx to support XDP rx multi-buff > > This commit reworks stmmac_rx() to collect every descriptor of a frame > in an xdp_buff kept in rx_q->state, so the XDP program sees the whole > frame, including any fragments. Fragmented frames only go to programs > loaded with BPF_F_XDP_HAS_FRAGS, and the skb is built with > xdp_build_skb_from_buff(). The xdp_rxq is now registered with the > page_pool page size as frag_size, so bpf_xdp_adjust_tail() can grow > fragments. > > > diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c > > index c429cb9e1ccbe..0edc92d5fb306 100644 > > --- a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c > > +++ b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c > > [ ... ] > > > @@ -2351,6 +2349,10 @@ static int __alloc_dma_rx_desc_resources(struct stmmac_priv *priv, > > goto err_free_buf_pool; > > } > > > > + rx_q->queue_index = queue; > > + rx_q->priv_data = priv; > > + rx_q->napi_skb_frag_size = PAGE_SIZE << pp_params.order; > > + > > if (priv->extend_desc) > > rx_q->dma_erx = addr; > > else > > @@ -2362,7 +2364,8 @@ static int __alloc_dma_rx_desc_resources(struct stmmac_priv *priv, > > else > > napi_id = ch->rx_napi.napi_id; > > > > - ret = xdp_rxq_info_reg(&rx_q->xdp_rxq, priv->dev, queue, napi_id); > > + ret = __xdp_rxq_info_reg(&rx_q->xdp_rxq, priv->dev, queue, napi_id, > > + rx_q->napi_skb_frag_size); > > [Severity: Medium] > Registering a frag_size here turns on bpf_xdp_frags_increase_tail() for > this rxq. Can a frags-capable program now grow a fragment beyond what a > single TX descriptor can describe? > > stmmac_rx() attaches the buf2 payload as a fragment at offset 0: > > if (!stmmac_build_xdp_frags(priv, rx_q, buf2_len, > buf->sec_page, 0, > dma_dir, &ctx.xdp)) { > > This happens on any core with buf2 capability (GMAC4/XGMAC), not only > with SPH. GMAC4 also sets GMAC_CONFIG_JE, so frames longer than > dma_buf_sz can use buf2 or several descriptors even at MTU 1500. > > With frag_size equal to PAGE_SIZE << order, bpf_xdp_adjust_tail() can > grow the last fragment to the full page_pool page. That is 16384 bytes > on 16K-page kernels and up to about 64K on 64K-page kernels. > > If the program then returns XDP_TX, stmmac_xdp_xmit_xdpf() puts each > fragment into one descriptor and never checks the length: > > int len = frag ? skb_frag_size(frag) : xdpf->len; > ... > stmmac_prepare_tx_desc(priv, desc, !i, len, false, ... > > The descriptor helpers then mask the length to 14 bits: > > dwmac4_rd_prepare_tx_desc(): > p->des2 |= cpu_to_le32(len & TDES2_BUFFER1_SIZE_MASK); > > dwxgmac2_prepare_tx_desc(): > p->des2 |= cpu_to_le32(len & XGMAC_TDES2_B1L); > > Wouldn't a 16384-byte fragment be programmed as a 0-byte buffer, and > larger ones wrap modulo 16K? The descriptor would then disagree with the > mapped length and with the total frame length in TDES3. That could put > truncated or malformed frames on the wire, or cause TX DMA errors. > > On 64K-page kernels the linear head could already hit this before this > patch. The fragment case is new. > > Should frag_size be capped, or should stmmac_xdp_xmit_xdpf() reject > buffers over the hardware per-buffer limit? This issue is real, but it has not been introduced by this patch since we can have the same issue even on the head of the xdp_buff, not just on the fragment. I think we should fix it with a dedicated patch. > > [ ... ] > > > @@ -6005,120 +6020,79 @@ static int stmmac_rx(struct stmmac_priv *priv, int limit, u32 queue) > > [ ... ] > > > + ctx.desc = p; > > + ctx.ndesc = np; > > + > > + skb = stmmac_xdp_run_prog(priv, &ctx.xdp); > > + /* For Not XDP_PASS verdict */ > > + if (IS_ERR(skb)) { > > + unsigned int xdp_res = -PTR_ERR(skb); > > + > > + if (xdp_res & STMMAC_XDP_CONSUMED) { > > + __xdp_return_buff(&ctx.xdp, true); > > + rx_dropped++; > > + } else if (xdp_res & (STMMAC_XDP_TX | > > + STMMAC_XDP_REDIRECT)) { > > + xdp_status |= xdp_res; > > + } > > goto next; > > } > > [Severity: High] > This isn't a bug introduced by this patch. However, now that multi-buffer > frames can reach XDP_TX, the patch seems to make an existing page_pool > race worse. As pointed out by sashiko, this issue has not been introduced by this series. > > When an XDP_TX frame completes, stmmac_tx_clean() frees it with direct > recycling: > > if (xdpf && > tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XDP_TX) { > xdp_return_frame_rx_napi(xdpf); > tx_q->xdpf[entry] = NULL; > } > > This reaches the lockless alloc cache with allow_direct=true: > > xdp_return_frame_rx_napi() > __xdp_return(..., napi_direct=true) > page_pool_put_full_netmem(pool, netmem, true) > page_pool_put_unrefed_netmem() > __page_pool_put_page() > page_pool_recycle_in_cache() > > stmmac_tx_clean() runs from the TX NAPI (tx_napi or rxtx_napi) of the > queue that stmmac_xdp_xmit_back() picks: > > queue = stmmac_xdp_get_tx_queue(priv, cpu); > > That queue is cpu % tx_queues, so it need not be the RX queue that owns > the page_pool. Even when the queue matches, tx_napi and rx_napi are > separate napi_structs and do not exclude each other. > > At the same time, the owning RX NAPI can run on another CPU. It pops > from the same alloc cache in stmmac_rx_refill(). It also pushes to it > through __xdp_return_buff(&ctx.xdp, true) and > page_pool_put_full_page(..., true). Those RX paths do not take the > netdev TX queue lock that stmmac_tx_clean() holds. > > Can interleaved updates of pool->alloc.count and pool->alloc.cache[] > give the same page to two RX descriptors, or lose a page? This looks > reachable with per-channel MSI RX/TX IRQs on different CPUs, with the TX > coalesce hrtimer, or with threaded NAPI. > > With this patch, each XDP_TX completion now returns 1 + nr_frags pages > through that path instead of a single page. > > [ ... ] > > > @@ -6005,120 +6020,79 @@ static int stmmac_rx(struct stmmac_priv *priv, int limit, u32 queue) > > buf2_len = stmmac_rx_buf2_len(priv, p, status, len); > > len += buf2_len; > > > > - /* Drop the packet if it is split over buf1 and buf2 on a > > - * single descriptor if the interface is running in XDP mode. > > - */ > > - if (unlikely(stmmac_xdp_is_enabled(priv) && buf2_len)) { > > - rx_dropped++; > > - goto next; > > - } > > - > > - /* ACS is disabled; strip manually. */ > > - if (likely(!(status & rx_not_ls))) > > - len -= ETH_FCS_LEN; > > - > > - if (!skb) { > > - /* Each frame starts here: reset the FCS handling */ > > - fcs_stripped = false; > > - > > + if (!n_descs) { > > dma_sync_single_for_cpu(priv->device, buf->addr, > > buf1_len, dma_dir); > > net_prefetch(page_address(buf->page) + > > buf->page_offset); > > > > - if (stmmac_xdp_is_enabled(priv) && !buf2_len) { > > - buf1_len -= ETH_FCS_LEN; > > - fcs_stripped = true; > > - } > > - > > xdp_init_buff(&ctx.xdp, bufsz, &rx_q->xdp_rxq); > > xdp_prepare_buff(&ctx.xdp, page_address(buf->page), > > buf->page_offset, buf1_len, true); > > - > > - ctx.priv = priv; > > - ctx.desc = p; > > - ctx.ndesc = np; > > - > > - skb = stmmac_xdp_run_prog(priv, &ctx.xdp); > > - /* For Not XDP_PASS verdict */ > > - if (IS_ERR(skb)) { > > - unsigned int xdp_res = -PTR_ERR(skb); > > - > > - if (xdp_res & STMMAC_XDP_CONSUMED) { > > - page_pool_recycle_direct(rx_q->page_pool, > > - virt_to_head_page(ctx.xdp.data)); > > - buf->page = NULL; > > + buf->page = NULL; > > + } else if (buf1_len) { > > + if (!stmmac_build_xdp_frags(priv, rx_q, buf1_len, > > + buf->page, > > + buf->page_offset, > > + dma_dir, &ctx.xdp)) { > > + if (!error) > > rx_dropped++; > > - goto next; > > - } else if (xdp_res & (STMMAC_XDP_TX | > > - STMMAC_XDP_REDIRECT)) { > > - xdp_status |= xdp_res; > > - buf->page = NULL; > > - goto next; > > - } > > - } > > - } > > - > > - if (!skb) { > > - unsigned int metasize; > > - > > - /* XDP program may expand or reduce tail */ > > - buf1_len = ctx.xdp.data_end - ctx.xdp.data; > > - > > - skb = napi_build_skb(page_address(buf->page), > > - rx_q->napi_skb_frag_size); > > - if (!skb) { > > - page_pool_recycle_direct(rx_q->page_pool, > > - buf->page); > > - buf->page = NULL; > > - rx_dropped++; > > error = 1; > > - goto drain_data; > > } > > - > > - /* XDP program may adjust header */ > > - skb_reserve(skb, > > - ctx.xdp.data - ctx.xdp.data_hard_start); > > - skb_put(skb, buf1_len); > > - skb_mark_for_recycle(skb); > > - > > - metasize = ctx.xdp.data - ctx.xdp.data_meta; > > - if (metasize) > > - skb_metadata_set(skb, metasize); > > - buf->page = NULL; > > - } else if (buf1_len) { > > - dma_sync_single_for_cpu(priv->device, buf->addr, > > - buf1_len, dma_dir); > > - skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, > > - buf->page, buf->page_offset, buf1_len, > > - priv->dma_conf.dma_buf_sz); > > buf->page = NULL; > > } > > > > if (buf2_len) { > > - dma_sync_single_for_cpu(priv->device, buf->sec_addr, > > - buf2_len, dma_dir); > > - skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, > > - buf->sec_page, 0, buf2_len, > > - priv->dma_conf.dma_buf_sz); > > + if (!stmmac_build_xdp_frags(priv, rx_q, buf2_len, > > + buf->sec_page, 0, > > + dma_dir, &ctx.xdp)) { > > + if (!error) > > + rx_dropped++; > > + error = 1; > > + } > > buf->sec_page = NULL; > > } > > + n_descs++; > > > > -drain_data: > > if (likely(status & rx_not_ls)) > > goto read_again; > > - if (!skb) > > - goto next; > > > > - /* Got entire packet into SKB. Finish it. */ > > + /* ACS is disabled; strip manually. */ > > + len -= ETH_FCS_LEN; > > + if (xdp_buff_shrink_tail(&ctx.xdp, ETH_FCS_LEN)) { > > + if (!error) > > + rx_dropped++; > > + error = 1; > > + } > > > > - /* Remove FCS if needed */ > > - if (!fcs_stripped && pskb_trim(skb, len)) { > > - dev_kfree_skb_any(skb); > > - rx_dropped++; > > + if (unlikely(error)) > > + goto error_free_frag; > > + > > + ctx.desc = p; > > + ctx.ndesc = np; > > + > > + skb = stmmac_xdp_run_prog(priv, &ctx.xdp); > > + /* For Not XDP_PASS verdict */ > > + if (IS_ERR(skb)) { > > + unsigned int xdp_res = -PTR_ERR(skb); > > + > > + if (xdp_res & STMMAC_XDP_CONSUMED) { > > + __xdp_return_buff(&ctx.xdp, true); > > + rx_dropped++; > > + } else if (xdp_res & (STMMAC_XDP_TX | > > + STMMAC_XDP_REDIRECT)) { > > + xdp_status |= xdp_res; > > + } > > goto next; > > } > > > > + skb = xdp_build_skb_from_buff(&ctx.xdp); > > + if (!skb) { > > + rx_dropped++; > > + goto error_free_frag; > > + } > > + > > + /* Got entire packet into SKB. Finish it. */ > > stmmac_get_rx_hwtstamp(priv, p, np, skb); > > > > if (priv->hw->hw_vlan_en) { > > [Severity: Low] > Does the FCS strip here leave sinfo->xdp_frags_truesize stale when it > frees a trailing fragment? > > stmmac_build_xdp_frags() attaches every fragment with > xdp_buff_add_frag(..., xdp->frame_sz). That makes __xdp_buff_add_frag() > add one full page_pool allocation to sinfo->xdp_frags_truesize for each > fragment. > > Sometimes the last buffer of a multi-buffer frame holds only 1 to 4 of > the FCS bytes. In that case xdp_buff_shrink_tail() goes through > xdp_frags_shrink_tail(), which returns that page to the pool and updates > sinfo->nr_frags and sinfo->xdp_frags_size. It never decrements > sinfo->xdp_frags_truesize, though. > > If at least one fragment is left, xdp_build_skb_from_buff() then does: > > tsize = sinfo->xdp_frags_truesize ? : nr_frags * xdp->frame_sz; > > So skb->truesize is overcharged by xdp->frame_sz for a page the skb no > longer owns. That is 4K up to 64K, depending on page size and pool > order. > > This path runs for every non-XSK RX frame, whether or not an XDP program > is loaded. Before this patch, the FCS was removed with pskb_trim(). That > goes through skb_condense(), which recomputed truesize whenever the > remaining frag data fit in the head tailroom. So regular stmmac RX > traffic did not see this before. > > The impact is limited to socket receive buffer accounting. Nothing is > leaked or corrupted. > > Every stmmac fragment has the same truesize. Would it be enough to pass > 0 as truesize to xdp_buff_add_frag(), and let xdp_build_skb_from_buff() > fall back to nr_frags * xdp->frame_sz from the post-shrink nr_frags? > Or should xdp_frags_shrink_tail() subtract the truesize of the > fragments it frees? bpf_xdp_adjust_tail() shares that helper and seems > to have the same omission. As pointed out before, this is an issue already present in bpf_xdp_frags_shrink_tail(). This series is just moving the code in a helper routine to make it available to the drivers. We should post a dedicated fix for it. Regards, Lorenzo > > -- > Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204-stmmac-rx-mb-v14-0-5506796fd39e%40oss.qualcomm.com