BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next v2 00/13] bpf: Embed local storage values in the storage object
@ 2026-11-23 15:15 Mykyta Yatsenko
  2026-11-23 15:15 ` [PATCH bpf-next v2 01/13] bpf: Free unpublished local storage with kfree_nolock() Mykyta Yatsenko
                   ` (13 more replies)
  0 siblings, 14 replies; 16+ messages in thread
From: Mykyta Yatsenko @ 2026-11-23 15:15 UTC (permalink / raw)
  To: bpf, ast, andrii, daniel, kernel-team, eddyz87; +Cc: Mykyta Yatsenko

A lookup in a local storage map reads the storage object, a cache slot
and then the selem that holds the value. When the owner is cold, each
of these reads can be a cache miss. Programs that do a few lookups for
each packet pay this cost on each packet.

This series adds the map flag BPF_F_INLINE_VALUE for sk, task, inode
and cgroup storage. The value of such a map is in the storage object,
at a fixed offset, so a lookup has one dependent load less.

Design
------
- Each owner type has one inline area at the end of struct
  bpf_local_storage. A storage object is at most 1 KB.
- Map create claims a slot (an 8 B tag and the value) in the area,
  first fit. A table of the live inline maps holds the layout, with at
  most 16 maps for each owner type. Map create fails with -ENOSPC if
  the area has no room now, and with -E2BIG if the value never fits.
- A storage object that is made while inline maps exist gets the
  area. If an object has no room for a slot, the value goes into a
  normal selem. Programs do not see the difference.
- The tag comes from a claim number. A lookup compares the tag with an
  acquire load, so a new map does not see the values of a freed map
  at the same offset.
- Programs can keep pointers to inline values, and these values have
  no selem. Thus an object with an inline area lives until its owner
  dies, and it is freed after an RCU tasks trace grace period.
- A lookup of a normal map tests one more field of the map, also on a
  host with no inline map.
- The sk storage map iterator is refused for inline maps, and sk diag
  shows inline values. A delete in NMI fails with -EOPNOTSUPP, as for
  normal maps. The flag needs a 64-bit kernel.
- libbpf does not retry a local storage map create without BTF any
  more. That retry always failed with -EINVAL and hid the real error.

Semantics and costs
-------------------
- A syscall update copies the value in place, under the storage lock.
  A reader that takes no lock can see a torn value, as with array
  maps. Without the flag, an update swaps in a new selem, so readers
  see the whole old or new value. BPF_F_LOCK is not available,
  because an inline value cannot have a spin lock.
- A pointer that a program keeps after an update or a delete points
  to the same slot. It sees, and can change, the next value of the
  same map. Without the flag, it sees the old value.
- The tag is predictable, and claim numbers wrap after 2^32 claims. A
  user that can create inline maps can make another map see bytes that
  it did not write, in objects made before that map.
- The area and the 16-map limit are shared by the whole host, also
  with BPF token users.
- While an inline map exists, new objects of its owner type are bigger
  and live until owner death. This is also true for owners that use
  only normal maps. A delete or a map unload does not give this memory
  back.

Numbers
-------
Host: AMD EPYC (Zen 5), pinned 24-vCPU KVM guest. Workload: two TC
egress programs look up two sk storage values for each packet (124 B
and 2 B), 9 sk storage maps for each socket, 131k connections with
request/response traffic. Paired A/B runs against the base kernel.

- Whole request, programs on, both values inline: -200 +- 108 ns
  (-2.9%).
- Sampled cost of the two lookups: 169 -> 58 ns and 90 -> 11 ns.
- bench_local_storage, hot loop with all data in L1, ns per lookup:
  normal maps: +0.39 to +0.76 (1 to 16 maps; 3 more instructions and
  1 more taken branch); inline hit: +0.32 to +0.67. The gain comes
  from cold owners, not from a hot loop.
- Memory: an object grows from 192 B to 256 B with inline values of up
  to 64 B, and to 1 KB with a 512 B inline value. Owners that use only
  normal maps pay the same while an inline map exists. In the workload
  above, unreclaimable slab grows by 629 B for each connection.
- Socket churn: no throughput cost. At about 230k closes each second,
  about 18 MB more objects wait for the tasks trace grace period.

Patches
-------
Patch 1 fixes an existing bug: a failed create in NMI freed storage
that was never published with kfree_rcu(). It can go in alone. It needs
commit bd897919e4dc, which is in bpf-next only. Patches 2-3 prepare the
code. Patches 4-10 add the inline area, its lifetime, lookup and
update, the iterator refusal, sk diag and the UAPI flag. Patch 11
changes libbpf, patch 12 adds a selftest and patch 13 adds the docs.

Testing
-------
- The selftest passes on normal and KASAN kernels, with no kmemleak
  report.
- Out of tree: races of create, update, delete, destroy and map free,
  also from sleepable and NMI programs; sk diag, clone, inner maps and
  owner death; fault injection
  that forces the storage lock to fail in destroy, with no leak and no
  KASAN report. KCSAN ran on an earlier revision with no report.

Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
---
Changes in v2:
- Removed RFC tag, simplified selftests.
- Link to v1: https://patch.msgid.link/20261113-sls_f1-v1-0-1b9492bd3f97@meta.com

---
Mykyta Yatsenko (13):
      bpf: Free unpublished local storage with kfree_nolock()
      bpf: Add obj_size to bpf_local_storage
      bpf: Add bpf_local_storage_delete()
      bpf: Add an inline slot allocator to local storage caches
      bpf: Size new local storage objects for the inline area
      bpf: Keep local storage objects with an inline area until owner death
      bpf: Keep values of BPF_F_INLINE_VALUE maps in the storage object
      bpf: Refuse the sk storage map iterator for BPF_F_INLINE_VALUE maps
      bpf: Show inline values in sk diag
      bpf: Allow BPF_F_INLINE_VALUE for local storage maps
      libbpf: Do not retry local storage map create without BTF
      selftests/bpf: Test BPF_F_INLINE_VALUE local storage maps
      bpf, docs: Document BPF_F_INLINE_VALUE for local storage maps

 Documentation/bpf/map_cgrp_storage.rst             |   3 +
 Documentation/bpf/map_sk_storage.rst               | 103 +++-
 include/linux/bpf_local_storage.h                  | 105 +++-
 include/uapi/linux/bpf.h                           |   7 +
 kernel/bpf/bpf_cgrp_storage.c                      |   8 +-
 kernel/bpf/bpf_inode_storage.c                     |   8 +-
 kernel/bpf/bpf_local_storage.c                     | 420 +++++++++++++--
 kernel/bpf/bpf_task_storage.c                      |   8 +-
 net/core/bpf_sk_storage.c                          |  71 ++-
 tools/include/uapi/linux/bpf.h                     |   7 +
 tools/lib/bpf/libbpf.c                             |  17 +-
 .../bpf/prog_tests/local_storage_inline.c          | 583 +++++++++++++++++++++
 .../selftests/bpf/progs/local_storage_inline.c     | 123 +++++
 13 files changed, 1388 insertions(+), 75 deletions(-)
---
base-commit: 4425c2a376e11caa4f01ea4ff2fbab61d197d67b
change-id: 20261113-sls_f1-abf16304e3ef

Best regards,
--  
Mykyta Yatsenko <yatsenko@meta.com>


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

end of thread, other threads:[~2026-11-30  4:58 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 15:15 [PATCH bpf-next v2 00/13] bpf: Embed local storage values in the storage object Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 01/13] bpf: Free unpublished local storage with kfree_nolock() Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 02/13] bpf: Add obj_size to bpf_local_storage Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 03/13] bpf: Add bpf_local_storage_delete() Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 04/13] bpf: Add an inline slot allocator to local storage caches Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 05/13] bpf: Size new local storage objects for the inline area Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 06/13] bpf: Keep local storage objects with an inline area until owner death Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 07/13] bpf: Keep values of BPF_F_INLINE_VALUE maps in the storage object Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 08/13] bpf: Refuse the sk storage map iterator for BPF_F_INLINE_VALUE maps Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 09/13] bpf: Show inline values in sk diag Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 10/13] bpf: Allow BPF_F_INLINE_VALUE for local storage maps Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 11/13] libbpf: Do not retry local storage map create without BTF Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 12/13] selftests/bpf: Test BPF_F_INLINE_VALUE local storage maps Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 13/13] bpf, docs: Document BPF_F_INLINE_VALUE for " Mykyta Yatsenko
2026-11-30  0:57 ` [PATCH bpf-next v2 00/13] bpf: Embed local storage values in the storage object Alexei Starovoitov
2026-11-30  4:58   ` Alexei Starovoitov

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