Skip to content

net: unify symmetric netmem and page_pool refcounting

Mina Almasry bpf linux-rdma lkml netdev 3 versions – Applied

Status from this message: This series was applied to netdev/net-next.git (main)

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v3 Latest6 patches 14 repliesApplied 6/6
  2. v26 patches 3 repliesSuperseded
  3. v16 patches 2 repliesSuperseded

Changes in v3 from the cover letter

- Linearize straddling output segments in skb_segment() when frag_list
SKBs mix pp_recycle flags instead of clearing NETIF_F_SG, and add a
KUnit regression test (Sashiko).
- Link to v2: https://lore.kernel.org/netdev/[email protected]/

Patches in v3 All 6 applied

#Subject and filesSizeRepliesApplied
1/6netmem: rename __get/__put_netmem() to get/put_net_iov()include/net/netmem.h net/core/skbuff.c+46 -141 replyApplied
2/6net: skbuff: add napi_pp_get_page() and use it in tcp_recvmsg_dmabuf()include/linux/skbuff_ref.h net/core/skbuff.c net/ipv4/tcp.c+37 -21 replyApplied
3/6net: skbuff: replace skb_page_unref() and __skb_frag_unref() with skb_netmem_unref()drivers/net/ethernet/marvell/sky2.c drivers/net/ethernet/mellanox/mlx4/en_rx.c include/linux/skbuff_ref.h +5 more+29 -284 repliesApplied
4/6net: skbuff: replace __skb_frag_ref() with symmetric skb_netmem_ref()drivers/net/ethernet/chelsio/inline_crypto/ch_ktls/chcr_ktls.c drivers/net/ethernet/sun/cassini.c drivers/net/veth.c +4 more+51 -382 repliesApplied
5/6net: skbuff: use skb_frag_ref() in skb_try_coalesce()net/core/skbuff.c+2 -350 repliesApplied
6/6net: kunit: test netmem, page_pool, and skb frag refcountingnet/core/net_test.c net/core/skbuff.c+1224 -00 repliesApplied

Reviews trailers per patch

1 of 6 patches has a Reviewed-by or Acked-by of their own; the series has one for all of them.

PatchAleksandr LoktionovStanislav FomichevAntony Antony
SeriesA-b
1/6
2/6
3/6
4/6T-b
5/6
6/6R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 3 reviewers.

Read the v3 thread

Cover letter [PATCH net-next v3 0/6] net: unify symmetric netmem and page_pool refcounting

netmems (pages and net_iovs) have two independent refcounts: a non-pp refcount (page._refcount or binding->ref) governing backing memory lifetime, and pp_ref_count governing recycling within a page_pool while page_pool holds a single backing non-pp reference. SKBs with pp_recycle=1 own pp_ref_count references on page_pool fragments; SKBs with pp_recycle=0 own non-pp references.

Core helpers currently mix these two refcounts asymmetrically:
skb_frag_ref() always increments the non-pp refcount via get_netmem(), whereas skb_frag_unref() decrements pp_ref_count when skb->pp_recycle is set. Any path that refs a fragment on a pp_recycle=1 SKB (or unrefs with a hardcoded recycle=false) increments one counter and decrements the other, leaking the backing memory while underflowing pp_ref_count (e.g., IP-TFS [1], skb_split(), skb_shift(), and cloned SKB uncloning). Unclear core APIs also led drivers and ULPs to open-code pp_ref_count manipulations or add one-off helpers like skb_pp_frag_ref().

Unify fragment refcounting into symmetric pairs where each layer clearly specifies which refcount it touches:

- Non-PP refcount: get/put_page(), get/put_net_iov(), get/put_netmem()

- PP refcount:     napi_pp_get/put_page()
- SKB netmem:      skb_netmem_ref/unref(netmem, recycle)
- SKB fragment:    skb_frag_ref/unref(skb, f)

Found by code inspection while reviewing [1] and confirmed with the KUnit tests in patch 6; not observed in production. Driver and ULP call-site changes are compile-tested only.

---
v3:
- Linearize straddling output segments in skb_segment() when frag_list
SKBs mix pp_recycle flags instead of clearing NETIF_F_SG, and add a
KUnit regression test (Sashiko).
- Link to v2: https://lore.kernel.org/netdev/[email protected]/

v2:
- Clear NETIF_F_SG in skb_segment() when frag_list SKBs mix pp_recycle
flags (patch 4).
- Export skb_shift() via EXPORT_SYMBOL_IF_KUNIT() to fix the
CONFIG_NET_TEST=m modpost error reported by NIPA (patch 6).
- Reset mac_header before skb_segment() and check skb_clone() results in
KUnit (Sashiko), and extend KUnit coverage to compound tail pages, skb_try_coalesce(), TCP devmem recvmsg, skb_zerocopy() rollbacks, skb_split()/skb_shift() edge cases, and mixed pp_recycle frag_list GSO.
- Note how the issues were found and tested (netdev-bot), and add
Assisted-by tags.
- Link to v1: https://lore.kernel.org/netdev/[email protected]/

Mina Almasry (6):
netmem: rename __get/__put_netmem() to get/put_net_iov()
net: skbuff: add napi_pp_get_page() and use it in tcp_recvmsg_dmabuf()
net: skbuff: replace skb_page_unref() and __skb_frag_unref() with
skb_netmem_unref()
net: skbuff: replace __skb_frag_ref() with symmetric skb_netmem_ref()
net: skbuff: use skb_frag_ref() in skb_try_coalesce()
net: kunit: test netmem, page_pool, and skb frag refcounting

 .../chelsio/inline_crypto/ch_ktls/chcr_ktls.c |    2 +-
 drivers/net/ethernet/marvell/sky2.c           |    2 +-
 drivers/net/ethernet/mellanox/mlx4/en_rx.c    |    2 +-
 drivers/net/ethernet/sun/cassini.c            |    4 +-
 drivers/net/veth.c                            |    2 +-
 include/linux/skbuff_ref.h                    |   64 +-
 include/net/netmem.h                          |   24 +-
 net/core/net_test.c                           | 1222 +++++++++++++++++
 net/core/skbuff.c                             |  157 ++-
 net/ipv4/esp4.c                               |    4 +-
 net/ipv4/tcp.c                                |    6 +-
 net/ipv6/esp6.c                               |    4 +-
 net/tls/tls_device.c                          |    2 +-
 net/tls/tls_device_fallback.c                 |    2 +-
 net/tls/tls_strp.c                            |    2 +-
 net/xfrm/xfrm_iptfs.c                         |    3 +-
 16 files changed, 1387 insertions(+), 115 deletions(-)

base-commit: 05b5019b01fb9443b09ae4ed34af1135a80a84c8

Signature · 1 line
2.56.0.212.g129ede20d9-goog