Skip to contentSkip to conversation

net: ti: icssg-prueth: fix XDP_TX from the AF_XDP zero-copy RX path

PATCH Superseded David Carlier 3 participants 3 messages Jun 20 – Jun 22

Subsystems (5)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: ti: icssg-prueth: fix XDP_TX from the AF_XDP zero-copy RX path

On XDP_TX from the zero-copy RX path, emac_run_xdp() converts the xsk buffer via xdp_convert_zc_to_xdp_frame(), which clones the data into a fresh MEM_TYPE_PAGE_ORDER0 page that is not DMA mapped. Transmitting it as PRUETH_TX_BUFF_TYPE_XDP_TX derives the DMA address with page_pool_get_dma_addr(), reading an uninitialized page->dma_addr, so the device DMAs from a bogus address (corrupt TX, or an IOMMU fault).

Pick the TX buffer type from the frame's memory type: keep PRUETH_TX_BUFF_TYPE_XDP_TX for page_pool frames and use PRUETH_TX_BUFF_TYPE_XDP_NDO for the cloned zero-copy frame. The completion path already unmaps PRUETH_SWDATA_XDPF buffers.

Fixes 7a64bb388df3 ("net: ti: icssg-prueth: Add AF_XDP zero copy for RX")
---
 drivers/net/ethernet/ti/icssg/icssg_common.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
drivers/net/ethernet/ti/icssg/icssg_common.c +12 -1
index 82ddef9c17d5..302e700ea17d 100644
@@ -804,6 +804,7 @@ EXPORT_SYMBOL_GPL(emac_xmit_xdp_frame);
  */
 static u32 emac_run_xdp(struct prueth_emac *emac, struct xdp_buff *xdp, u32 *len)
 {
+	enum prueth_tx_buff_type tx_buff_type;
 	struct net_device *ndev = emac->ndev;
 	struct netdev_queue *netif_txq;
 	int cpu = smp_processor_id();
@@ -826,11 +827,21 @@ static u32 emac_run_xdp(struct prueth_emac *emac, struct xdp_buff *xdp, u32 *len
 			goto drop;
 		}
 
+		/* In AF_XDP zero-copy mode xdp_convert_buff_to_frame()
+		 * clones the xsk buffer into a fresh MEM_TYPE_PAGE_ORDER0
+		 * page that is not DMA mapped. Such a frame must be mapped
+		 * via the NDO path; only a page pool-backed frame already
+		 * carries a usable page_pool DMA address.
+		 */
+		tx_buff_type = xdpf->mem_type == MEM_TYPE_PAGE_POOL ?
+				PRUETH_TX_BUFF_TYPE_XDP_TX :
+				PRUETH_TX_BUFF_TYPE_XDP_NDO;
+
 		q_idx = cpu % emac->tx_ch_num;
 		netif_txq = netdev_get_tx_queue(ndev, q_idx);
 		__netif_tx_lock(netif_txq, cpu);
 		result = emac_xmit_xdp_frame(emac, xdpf, q_idx,
-					     PRUETH_TX_BUFF_TYPE_XDP_TX);
+					     tx_buff_type);
 		__netif_tx_unlock(netif_txq);
 		if (result == ICSSG_XDP_CONSUMED) {
 			ndev->stats.tx_dropped++;
Signature · 1 line
2.53.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

Pre-existing issues:
- [Critical] On XDP_TX transmission failure in AF_XDP zero-copy mode, the driver double-frees the original xsk_buff and leaks the newly allocated xdp_frame page.
- [Critical] The TX completion path unconditionally calls dma_unmap_single() on PRUETH_SWDATA_XDPF buffers, destroying the page pool's DMA mapping and using a wrong offset address.
- [High] Error paths in emac_xmit_xdp_frame() pass uninitialized hardware descriptors to prueth_xmit_free(), causing random/garbage addresses to be unmapped.
- [High] The TX completion routine frees network buffers before unmapping their DMA mappings, creating a use-after-free pattern if bounce buffers are involved.
- [High] Missing DMA synchronization for page pool XDP_TX frames before transmission.
--

commit fcfa7d9e02b3a0f883c4feee8ec375b550885d7d
Author: David Carlier <[email protected]>

net: ti: icssg-prueth: fix XDP_TX from the AF_XDP zero-copy RX path

On XDP_TX from the zero-copy RX path, emac_run_xdp() converts the xsk buffer via xdp_convert_zc_to_xdp_frame(), which clones the data into a fresh MEM_TYPE_PAGE_ORDER0 page that is not DMA mapped. Transmitting it as PRUETH_TX_BUFF_TYPE_XDP_TX derives the DMA address with page_pool_get_dma_addr(), reading an uninitialized page->dma_addr, so the device DMAs from a bogus address (corrupt TX, or an IOMMU fault).

Pick the TX buffer type from the frame's memory type: keep PRUETH_TX_BUFF_TYPE_XDP_TX for page_pool frames and use PRUETH_TX_BUFF_TYPE_XDP_NDO for the cloned zero-copy frame. The completion path already unmaps PRUETH_SWDATA_XDPF buffers.

Quoted 14 lines
drivers/net/ethernet/ti/icssg/icssg_common.c +6 -0
index 82ddef9c17d54..302e700ea17d8 100644
@@ -826,11 +827,21 @@ static u32 emac_run_xdp(struct prueth_emac *emac, struct xdp_buff *xdp, u32 *len
 			goto drop;
 		}
 
+		/* In AF_XDP zero-copy mode xdp_convert_buff_to_frame()
+		 * clones the xsk buffer into a fresh MEM_TYPE_PAGE_ORDER0
+		 * page that is not DMA mapped. Such a frame must be mapped
+		 * via the NDO path; only a page pool-backed frame already
+		 * carries a usable page_pool DMA address.
+		 */

drivers/net/ethernet/ti/icssg/icssg_common.c static u32 emac_run_xdp(struct prueth_emac *emac, struct xdp_buff *xdp, u32 *len · line 844

Quoted 10 lines
drivers/net/ethernet/ti/icssg/icssg_common.c +5 -1
+		tx_buff_type = xdpf->mem_type == MEM_TYPE_PAGE_POOL ?
+				PRUETH_TX_BUFF_TYPE_XDP_TX :
+				PRUETH_TX_BUFF_TYPE_XDP_NDO;
+
 		q_idx = cpu % emac->tx_ch_num;
 		netif_txq = netdev_get_tx_queue(ndev, q_idx);
 		__netif_tx_lock(netif_txq, cpu);
 		result = emac_xmit_xdp_frame(emac, xdpf, q_idx,
-					     PRUETH_TX_BUFF_TYPE_XDP_TX);
+					     tx_buff_type);

[Severity: Critical]
This is a pre-existing issue, but does emac_xmit_xdp_frame() mark page pool frames with a type that triggers an unconditional unmap?

In emac_xmit_xdp_frame(), PRUETH_TX_BUFF_TYPE_XDP_TX frames are marked as PRUETH_SWDATA_XDPF:

emac_xmit_xdp_frame() {
    ...
    if (buff_type == PRUETH_TX_BUFF_TYPE_XDP_TX) {
        buf_dma = page_pool_get_dma_addr(page);
        buf_dma += xdpf->headroom + sizeof(struct xdp_frame);
    }
    ...
    swdata = cppi5_hdesc_get_swdata(first_desc);
    swdata->type = PRUETH_SWDATA_XDPF;
    ...
}

Then in the completion path, prueth_xmit_free() unconditionally unmaps them:

prueth_xmit_free() {
    ...
    if (swdata->type == PRUETH_SWDATA_XDPF) {
        dma_unmap_single(tx_chn->dma_dev, buf_dma, buf_dma_len, DMA_TO_DEVICE);
    }
    ...
}

Does this unmap the page pool-owned DMA address using an offset address, destroying the mapping and potentially causing an IOMMU fault when the page is recycled?

[Severity: High]
This isn't a bug introduced by this patch, but does the error path in emac_xmit_xdp_frame() unmap garbage addresses?

If virt_to_head_page() or dma_map_single() fails early in emac_xmit_xdp_frame(), the code jumps to drop_free_descs before first_desc is fully initialized:

emac_xmit_xdp_frame() {
    first_desc = k3_cppi_desc_pool_alloc(tx_chn->desc_pool);
    ...
    if (unlikely(!page)) {
        goto drop_free_descs;
    }
    ...
drop_free_descs:
    prueth_xmit_free(tx_chn, first_desc);
    return ICSSG_XDP_CONSUMED;
}

Since k3_cppi_desc_pool_alloc() doesn't zero memory, could prueth_xmit_free() read an uninitialized swdata->type and unmap a random buf_dma address?

[Severity: High]
This is a pre-existing issue, but does emac_xmit_xdp_frame() fail to sync the DMA buffer for the device?

For PRUETH_TX_BUFF_TYPE_XDP_TX, it retrieves the page pool DMA address:

emac_xmit_xdp_frame() {
    if (buff_type == PRUETH_TX_BUFF_TYPE_XDP_TX) {
        buf_dma = page_pool_get_dma_addr(page);
    ...
}

Because XDP packet modifications reside in the CPU cache and the driver uses DMA_BIDIRECTIONAL mappings, is a dma_sync_single_for_device() call required here to ensure the hardware transmits the modified data instead of stale content?

[Severity: High]
This isn't a bug introduced by this patch, but does the TX completion path free the buffer before unmapping it?

In emac_tx_complete_packets():

emac_tx_complete_packets() {
    ...
    xdp_return_frame(xdpf);
    ...
    prueth_xmit_free(tx_chn, desc_tx);
    ...
}

Since prueth_xmit_free() performs dma_unmap_single(), does this free the memory back to the allocator before the DMA mapping is torn down?

If bounce buffers are used, could this allow the memory to be reallocated and re-mapped while the previous unmap is incomplete?

Quoted 4 lines
drivers/net/ethernet/ti/icssg/icssg_common.c +0 -0
 		__netif_tx_unlock(netif_txq);
 		if (result == ICSSG_XDP_CONSUMED) {
 			ndev->stats.tx_dropped++;

goto drop;

[Severity: Critical]
This is a pre-existing issue, but does the error path cause a double-free and a memory leak when XDP_TX fails in zero-copy mode?

When emac_run_xdp() processes an XDP_TX, it converts the buffer:

emac_run_xdp() {
    ...
    case XDP_TX:
        xdpf = xdp_convert_buff_to_frame(xdp);
    ...
}

In AF_XDP zero-copy mode, xdp_convert_zc_to_xdp_frame() allocates a new page and frees the original xdp buffer.

If emac_xmit_xdp_frame() fails, it returns ICSSG_XDP_CONSUMED and jumps to the drop label:

        if (result == ICSSG_XDP_CONSUMED) {
            ndev->stats.tx_dropped++;
            goto drop;
        }
    ...
drop:
    ...
    case XDP_DROP:
        ndev->stats.rx_dropped++;
        return ICSSG_XDP_CONSUMED;

The return value ICSSG_XDP_CONSUMED propagates to emac_rx_packet_zc(), which then frees the buffer again:

emac_rx_packet_zc() {
    ...
    ret = emac_run_xdp(emac, (struct xdp_buff *)xdp, &pkt_len);
    ...
    switch (ret) {
    case ICSSG_XDP_CONSUMED:
        xsk_buff_free(xdp);
        break;
    ...
}

Does this leak the newly allocated xdpf page and perform a double-free on the original xdp buffer?

Quoted 3 lines
drivers/net/ethernet/ti/icssg/icssg_common.c +0 -0
 		}
 
 		dev_sw_netstats_rx_add(ndev, xdpf->len);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to David Carlier

Hi David,

Thanks for the fix.

Quoted 7 lines · David Carlier

On XDP_TX from the zero-copy RX path, emac_run_xdp() converts the xsk buffer via xdp_convert_zc_to_xdp_frame(), which clones the data into a fresh MEM_TYPE_PAGE_ORDER0 page that is not DMA mapped. Transmitting it as PRUETH_TX_BUFF_TYPE_XDP_TX derives the DMA address with page_pool_get_dma_addr(), reading an uninitialized page->dma_addr, so the device DMAs from a bogus address (corrupt TX, or an IOMMU fault).

Quoted 5 lines

Pick the TX buffer type from the frame's memory type: keep PRUETH_TX_BUFF_TYPE_XDP_TX for page_pool frames and use PRUETH_TX_BUFF_TYPE_XDP_NDO for the cloned zero-copy frame. The completion path already unmaps PRUETH_SWDATA_XDPF buffers.

Is it safe to unconditionally unmap the buffer for the case where frame's memory type is PRUETH_TX_BUFF_TYPE_XDP_TX? In this case the DMA mapping is done with rx_chn->dma_dev, where as in completion path we are unmapping with tx_chn->dma_dev unconditionally.

Quoted 42 lines
Fixes 7a64bb388df3 ("net: ti: icssg-prueth: Add AF_XDP zero copy for RX")
---
  drivers/net/ethernet/ti/icssg/icssg_common.c | 13 ++++++++++++-
  1 file changed, 12 insertions(+), 1 deletion(-)
drivers/net/ethernet/ti/icssg/icssg_common.c +12 -1
index 82ddef9c17d5..302e700ea17d 100644
@@ -804,6 +804,7 @@ EXPORT_SYMBOL_GPL(emac_xmit_xdp_frame);
   */
  static u32 emac_run_xdp(struct prueth_emac *emac, struct xdp_buff *xdp, u32 *len)
  {
+	enum prueth_tx_buff_type tx_buff_type;
  	struct net_device *ndev = emac->ndev;
  	struct netdev_queue *netif_txq;
  	int cpu = smp_processor_id();
@@ -826,11 +827,21 @@ static u32 emac_run_xdp(struct prueth_emac *emac, struct xdp_buff *xdp, u32 *len
  			goto drop;
  		}
 
+		/* In AF_XDP zero-copy mode xdp_convert_buff_to_frame()
+		 * clones the xsk buffer into a fresh MEM_TYPE_PAGE_ORDER0
+		 * page that is not DMA mapped. Such a frame must be mapped
+		 * via the NDO path; only a page pool-backed frame already
+		 * carries a usable page_pool DMA address.
+		 */
+		tx_buff_type = xdpf->mem_type == MEM_TYPE_PAGE_POOL ?
+				PRUETH_TX_BUFF_TYPE_XDP_TX :
+				PRUETH_TX_BUFF_TYPE_XDP_NDO;
+
  		q_idx = cpu % emac->tx_ch_num;
  		netif_txq = netdev_get_tx_queue(ndev, q_idx);
  		__netif_tx_lock(netif_txq, cpu);
  		result = emac_xmit_xdp_frame(emac, xdpf, q_idx,
-					     PRUETH_TX_BUFF_TYPE_XDP_TX);
+					     tx_buff_type);
  		__netif_tx_unlock(netif_txq);
  		if (result == ICSSG_XDP_CONSUMED) {
  			ndev->stats.tx_dropped++;