bpf: tcp: Prefetch sk storage before TCP transmit
Get series with b4
b4 am [email protected] change-id 20261130-sk_storage_prefetch-575e7df22179Versions newest first
- v1 Latest5 patches 4 repliesIn review
Patches in v1 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/5 | bpf: Make a NULL cache slot mean that local storage has no valueinclude/ kernel/ | +33 | 0 replies | |
| 2/5 | selftests/bpf: Test sk storage maps that share cache slotstools/ | +57 | 0 replies | |
| 3/5 | bpf: tcp: Prefetch sk storage cache[] before TCP transmitinclude/ net/ net/ +1 more | +60 | 1 reply | |
| 4/5 | bpf: tcp: Prefetch values of hot sk storage maps before transmitinclude/ net/ net/ | +67 | 1 reply | |
| 5/5 | selftests/bpf: Test sk storage lookups during TCP transmittools/ tools/ | +199 | 1 reply |
Reviews trailers per patch
No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.
Cover letter [PATCH bpf-next 0/5] bpf: tcp: Prefetch sk storage before TCP transmit
An egress program that looks up the sk storage of a cold socket waits for two cache misses in a row. The first miss is on cache[] of the storage, and the second miss is on the element. A lookup of a value that the socket does not have is worse: it walks the whole list of the storage, with one miss for each element. With many connections, these misses are a large part of the program cost.
This series replaces the BPF_F_INLINE_VALUE series [1] with the design that Alexei proposed [2], [3]:
- Patch 1 makes a NULL cache[] slot mean that the storage has no value for the slot, so that a lookup of an absent value stops at the first load [3]. This patch is generic: task, inode and cgroup storage get it too. Patch 2 tests maps that share slots.
- Patch 3 prefetches cache[] when sendmsg starts and when a transmit starts, because TCP knows the socket before the egress programs run.
- Patch 4 learns on each CPU which cache[] slots the programs look up, and prefetches the elements of these slots just before ->queue_xmit(). Patch 5 tests the hooks under traffic.
Patches 1-2 do not need patches 3-5. There is no UAPI change and no new limit.
Changes from the prototype in [2]:
- A work item changes the static key from a count of the maps. Map create can run, and map free always runs, with migration disabled, and a change of a static key takes cpus_read_lock(). This can deadlock with a CPU offline: the key code of the prototype hung in a CPU hotplug and light skeleton stress test.
- Only bpf_sk_storage_get() marks a slot, for any socket. The prototype marked the lookups of the last socket that sent on the CPU.
- One mask of marked slots, cleared every 256 transmits. The prototype kept a second mask with the slots of the last round.
- The cache[] prefetch runs only on a CPU that has a marked slot.
- The sendmsg prefetch is in tcp_sendmsg_locked(), so that kTLS, sockmap and espintcp get it too.
- this_cpu operations, because the hooks can run in preemptible process context.
Limits:
- Patches 3-4 cover only TCP transmit and sk storage. The lookups of all programs mark slots, so a map that only ingress or sock_ops programs use also gets a prefetch on each transmit.
- The static key is on while any sk storage map exists. While a CPU has a hot slot, each transmit on that CPU loads sk->sk_bpf_storage. A gate for each socket can come later, if a workload needs it.
- With more than 16 maps, maps share cache[] slots, and a lookup of an absent value in a shared slot can still walk the list.
On x86-64, clang emits no instruction for prefetch(), because the kernel builds with -mno-sse [4], [5]. Thus the hooks use prefetchw().
Results: AMD Zen 5 KVM guest, 4 server vCPUs, IPv6 over veth, 131072 connections, 9 sk storage maps for each socket, bpf-next 2bf3351c5f89. 2 tc egress programs do 2 sk storage lookups on each packet. The change of the time in each lookup against base, from perf profiles, mean of 3 runs:
lookup 1 lookup 2
both values present -95.7% -93.7% lookup 2 finds no value -96.4% -99.5%
With patch 1 alone, the lookup that finds no value takes 98.7% less time, and the other lookups change by less than 4%. The local storage benchmarks in selftests show no change larger than the noise between runs.
[1] https://lore.kernel.org/bpf/[email protected]/
[2] https://lore.kernel.org/bpf/[email protected]/
[3] https://lore.kernel.org/bpf/[email protected]/
[4] https://lore.kernel.org/all/[email protected]/
[5] https://github.com/llvm/llvm-project/pull/248194
[6] https://lore.kernel.org/bpf/[email protected]/
---
Mykyta Yatsenko (5):
bpf: Make a NULL cache slot mean that local storage has no value selftests/bpf: Test sk storage maps that share cache slots
bpf: tcp: Prefetch sk storage cache[] before TCP transmit
bpf: tcp: Prefetch values of hot sk storage maps before transmit selftests/bpf: Test sk storage lookups during TCP transmit
include/linux/bpf_local_storage.h | 14 +- include/net/bpf_sk_storage.h | 53 +++++++ kernel/bpf/bpf_local_storage.c | 23 +++- net/core/bpf_sk_storage.c | 69 +++++++++- net/ipv4/tcp.c | 2 + net/ipv4/tcp_output.c | 3 + .../bpf/prog_tests/sk_storage_shared_slots.c | 57 ++++++++ .../selftests/bpf/prog_tests/sk_storage_xmit.c | 153 +++++++++++++++++++++ .../testing/selftests/bpf/progs/sk_storage_xmit.c | 46 +++++++ 9 files changed, 415 insertions(+), 5 deletions(-)
---
base-commit: 94299e4ad749bd5b361498b97b59f326bedac6fe
change-id: 20261130-sk_storage_prefetch-575e7df22179
Best regards,
Signature · 1 line
Mykyta Yatsenko <[email protected]>