BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next 0/5] bpf: tcp: Prefetch sk storage before TCP transmit
@ 2026-12-04 20:03 Mykyta Yatsenko
  2026-12-04 20:03 ` [PATCH bpf-next 1/5] bpf: Make a NULL cache slot mean that local storage has no value Mykyta Yatsenko
                   ` (5 more replies)
  0 siblings, 6 replies; 13+ messages in thread
From: Mykyta Yatsenko @ 2026-12-04 20:03 UTC (permalink / raw)
  To: bpf, ast, andrii, daniel, kernel-team, eddyz87, memxor; +Cc: Mykyta Yatsenko

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.

Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>

[1] https://lore.kernel.org/bpf/20261123-sls_f1-v2-0-76474de9b052@meta.com/
[2] https://lore.kernel.org/bpf/DN8A36W906WG.1BTUYRC2FIIR@gmail.com/
[3] https://lore.kernel.org/bpf/DN92BUAQMX7H.3NKLFY0FRORT7@gmail.com/
[4] https://lore.kernel.org/all/20261103060658.696994-1-martin.ankerl@gmail.com/
[5] https://github.com/llvm/llvm-project/pull/248194
[6] https://lore.kernel.org/bpf/DNAPOYTT6B0G.2RZJJ80T3HFKU@gmail.com/

---
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,
--  
Mykyta Yatsenko <yatsenko@meta.com>


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2026-12-04 21:40 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 20:03 [PATCH bpf-next 0/5] bpf: tcp: Prefetch sk storage before TCP transmit Mykyta Yatsenko
2026-12-04 20:03 ` [PATCH bpf-next 1/5] bpf: Make a NULL cache slot mean that local storage has no value Mykyta Yatsenko
2026-12-04 20:03 ` [PATCH bpf-next 2/5] selftests/bpf: Test sk storage maps that share cache slots Mykyta Yatsenko
2026-12-04 20:03 ` [PATCH bpf-next 3/5] bpf: tcp: Prefetch sk storage cache[] before TCP transmit Mykyta Yatsenko
2026-12-04 20:23   ` sashiko-bot
2026-12-04 21:25     ` Mykyta Yatsenko
2026-12-04 20:03 ` [PATCH bpf-next 4/5] bpf: tcp: Prefetch values of hot sk storage maps before transmit Mykyta Yatsenko
2026-12-04 20:18   ` sashiko-bot
2026-12-04 21:35     ` Mykyta Yatsenko
2026-12-04 20:03 ` [PATCH bpf-next 5/5] selftests/bpf: Test sk storage lookups during TCP transmit Mykyta Yatsenko
2026-12-04 20:17   ` sashiko-bot
2026-12-04 21:40     ` Mykyta Yatsenko
2026-12-04 21:38 ` [PATCH bpf-next 0/5] bpf: tcp: Prefetch sk storage before " Andrii Nakryiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox