Skip to content

tun: optimize SKB allocation with NAPI cache

Jon Kohler bpf lkml netdev 2 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest9 patches 19 repliesIn review
  2. v14 patches 11 repliesSuperseded

Changes in v2 from the cover letter

- Added drop statistics cleanup series, else merge conflicts abound
- Removed xdp_prog change (Willem)
- Clarified drop scenario in tun_put_user, where it is an extention of current behavior (Willem comment from v1)
- Export skb_defer_free_flush
- Use deferred skb free to immediately refill cache prior to bulk alloc, which also prevents IPIs from pounding TX heavy / TX only cores

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/9tun: cleanup out label in tun_xdp_onedrivers/net/tun.c+3 -70 replies
2/9tun: correct drop statistics in tun_xdp_onedrivers/net/tun.c+9 -30 replies
3/9tun: correct drop statistics in tun_put_userdrivers/net/tun.c+34 -174 replies
4/9tun: correct drop statistics in tun_get_userdrivers/net/tun.c+40 -130 replies
5/9tun: use bulk NAPI cache allocation in tun_xdp_onedrivers/net/tun.c+24 -613 replies
6/9tun: use napi_build_skb in __tun_build_skbdrivers/net/tun.c+5 -10 replies
7/9tun: use napi_consume_skb() in tun_put_userdrivers/net/tun.c+3 -10 replies
8/9net: core: export skb_defer_free_flushinclude/linux/skbuff.h net/core/dev.c+3 -10 replies
9/9tun: flush deferred skb free list before bulk NAPI cache getdrivers/net/tun.c+6 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v2 yet.

Read the v2 thread

Cover letter [PATCH net-next v2 0/9] tun: optimize SKB allocation with NAPI cache

Use the per-CPU NAPI cache for SKB allocation in most places, and leverage bulk allocation for tun_xdp_one since the batch size is known at submission time. Additionally, utilize napi_build_skb and napi_consume_skb to further benefit from the NAPI cache. This all improves efficiency by reducing allocation overhead.

Note: This series does not address the large payload path in tun_alloc_skb, which spans sock.c and skbuff.c,A separate series will handle privatizing the allocation code in tun and integrating the NAPI cache for that path.

Results using basic iperf3 UDP test:
TX guest: taskset -c 2 iperf3 -c rx-ip-here -t 30 -p 5200 -b 0 -u -i 30 RX guest: taskset -c 2 iperf3 -s -p 5200 -D

Bitrate
Before: 6.08 Gbits/sec
After : 6.36 Gbits/sec

However, the basic test doesn't tell the whole story. Looking at a flamegraph from before and after, less cycles are spent both on RX vhost thread in the guest-to-guest on a single host case, but also less cycles in the guest-to-guest case when on separate hosts, as the host NIC handlers benefit from these NAPI-allocated SKBs (and deferred free) as well.

Speaking of deferred free, v2 adds exporting deferred free from net core and using immediately prior in tun_put_user. This not only keeps the cache as warm as you can get, but also prevents a TX heavy vhost thread from getting IPI'd like its going out of style. This approach is similar in concept to what happens from NAPI loop in net_rx_action.

I've also merged this series with a small series about cleaning up packet drop statistics along the various error paths in tun, as I want to make sure those all go through kfree_skb_reason(), and we'd have merge conflicts separating the two. If the maintainers want to take them separately, happy to break them apart if needed. It is fairly clean keeping them together otherwise.

Thanks all,
Jon

v2:
- Added drop statistics cleanup series, else merge conflicts abound
- Removed xdp_prog change (Willem)
- Clarified drop scenario in tun_put_user, where it is an extention of current behavior (Willem comment from v1)
- Export skb_defer_free_flush
- Use deferred skb free to immediately refill cache prior to bulk alloc, which also prevents IPIs from pounding TX heavy / TX only cores

v1: https://patchwork.kernel.org/project/netdevbpf/cover/[email protected]/

Jon Kohler (9):
tun: cleanup out label in tun_xdp_one
tun: correct drop statistics in tun_xdp_one
tun: correct drop statistics in tun_put_user
tun: correct drop statistics in tun_get_user
tun: use bulk NAPI cache allocation in tun_xdp_one
tun: use napi_build_skb in __tun_build_skb
tun: use napi_consume_skb() in tun_put_user
net: core: export skb_defer_free_flush
tun: flush deferred skb free list before bulk NAPI cache get

 drivers/net/tun.c      | 170 +++++++++++++++++++++++++++++------------
 include/linux/skbuff.h |   1 +
 net/core/dev.c         |   3 +-
 3 files changed, 126 insertions(+), 48 deletions(-)
Signature · 1 line
2.43.0