xsk, xdp: fix ZC buffer double free on failed redirect
Get series with b4
b4 am [email protected]Versions newest first
- v6 Latest2 patches 0 repliesNew
Changes in v6 from the cover letter
v5: https://lore.kernel.org/all/[email protected]/
- Add patch 2 for the devmap, cpumap and ifindex paths (Jakub).
- Patch 1: update the changelog paragraph about the devmap path, which
patch 2 now fixes. No code changes, rebased on current net.
Patches in v6 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/2 | xsk: Do not free ZC Rx buffer when the Rx ring is fulldrivers/ drivers/ drivers/ +2 more | +10 | 0 replies | |
| 2/2 | xdp: Do not free ZC buffer when a frame redirect failsinclude/ net/ net/ | +40 | 0 replies |
Reviews trailers per patch
1 of 2 patches has a Reviewed-by or Acked-by.
| Patch | Björn Töpel | Jason Xing | Nitin Shetty J | Jerome Tollet |
|---|---|---|---|---|
| 1/2 | R-b | R-b | R-b | T-b |
| 2/2 |
Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 4 reviewers.
Cover letter [PATCH net v6 0/2] xsk, xdp: fix ZC buffer double free on failed redirect
Changes in v6:
v5: https://lore.kernel.org/all/[email protected]/
- Add patch 2 for the devmap, cpumap and ifindex paths (Jakub).
- Patch 1: update the changelog paragraph about the devmap path, which
patch 2 now fixes. No code changes, rebased on current net.
Hi,
A failed xdp_do_redirect() leaves a zero-copy buffer with the driver, which then frees it. Two paths in the core free the buffer themselves before failing:
- xsk_rcv_zc(), when the Rx ring of the target XSK is full;
- xdp_convert_zc_to_xdp_frame(), which copies the buffer for a devmap, cpumap or ifindex target and frees it before the redirect of the copy can still fail. The copy then leaks as well.
The second free is harmless when the driver frees the buffer right away, as xp_free() skips a buffer that is already on the pool free list. It corrupts the pool when the driver frees it later, after the buffer may have been handed out again, as mlx5e does on Rx WQE refill.
Marcin's patch 1 fixes the first path, and its v1 to v5 were posted on their own. Jakub asked for all redirect targets to be fixed in one series [0], and with Marcin away I took it over [1].
Patch 1 is Marcin's v5 without code changes. I only updated the paragraph about the devmap path in its changelog, as patch 2 fixes it.
Patch 2 makes xdp_do_redirect() copy the buffer without freeing it, and free it only once the copy has been redirected. If the redirect fails, the copy is freed and the buffer is left to the driver. With both patches, a failed xdp_do_redirect() never frees a zero-copy buffer, whatever the target, so this complements original intent from patch 1.
I asked LLM to test it with virtio_net in zero-copy mode under QEMU, 20000 packets per case. The XDP program redirects either to lo through a devmap or an ifindex, which fails as lo has no ndo_xdp_xmit; to another virtio_net device through a devmap, which succeeds; or to an XSKMAP whose socket's Rx ring is full. xp_free() calls were counted with a kprobe and leaked pages derived from nr_free_pages (rounded, as it is noisy), both per packet:
xp_free() calls pages leaked
base p1 p1+p2 base p1 p1+p2
devmap, fails 2 2 1 1 1 0 ifindex, fails 2 2 1 1 1 0 XSKMAP, Rx ring full 2 1 1 0 0 0 devmap, succeeds 1 1 1 0 0 0 XDP_TX 1 1 1 0 0 0
[0] https://lore.kernel.org/r/[email protected]
[1] https://lore.kernel.org/r/av3tAd5kTyxfmo1S@boxer
Maciej Fijalkowski (1):
xdp: Do not free ZC buffer when a frame redirect fails
Marcin Knap (1):
xsk: Do not free ZC Rx buffer when the Rx ring is full
drivers/net/ethernet/google/gve/gve_rx_dqo.c | 4 +++- drivers/net/ethernet/intel/i40e/i40e_xsk.c | 1 + drivers/net/ethernet/intel/ice/ice_xsk.c | 1 + .../marvell/octeontx2/nic/otx2_txrx.c | 3 ++- include/net/xdp.h | 2 ++ net/core/filter.c | 23 +++++++++++++++++-- net/core/xdp.c | 19 +++++++++++++-- net/xdp/xsk.c | 15 +++--------- 8 files changed, 50 insertions(+), 18 deletions(-)
base-commit: e9d78fc6741ac06d438cf64e314c28e57014432a
Signature · 1 line
2.43.0