All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 bpf-next 0/7] bpf: Share subprogs between programs
@ 2026-12-02 21:27 Alexei Starovoitov
  2026-12-02 21:27 ` [PATCH v2 bpf-next 1/7] bpf: Prepare for many main programs in one load Alexei Starovoitov
                   ` (6 more replies)
  0 siblings, 7 replies; 13+ messages in thread
From: Alexei Starovoitov @ 2026-12-02 21:27 UTC (permalink / raw)
  To: bpf; +Cc: daniel, andrii, eddyz87, memxor

From: Alexei Starovoitov <ast@kernel.org>

A scheduler or a datapath is many programs that call the same functions.
Each program gets a copy of them, so they are verified and JITed N times.
Load such programs together, with one copy.

  before                           after

  prog1: main1 | f | g             entry1: main1 --+
  prog2: main2 | f | g             entry2: main2 --+--> owner: f | g
  prog3: main3 | f | g             entry3: main3 --+

  3 x BPF_PROG_LOAD, 3 fds         1 x BPF_PROG_LOAD, 3 fds

An entry is a program with its own fd, id, name, stats and streams. It's
attached, pinned and closed like any other program. The owner has the code
and goes away with the last entry.

libbpf does it by default for programs of one type and attach type, or of
one struct_ops map, that call the same functions. It makes copies when the
kernel has no entries or rejects the load. Light skeletons, signed or not,
do the same.
It works on x86-64 and arm64. Tracing, LSM and extension programs, and
programs with tail calls keep their copies. See patch 2 for the full list.

32 tc programs that call the same 65 global functions:

                insns   verified   x86-64 code     load
  copies      1056831     991295   4547452 bytes   1.60 s
  one copy      33118      31070    143034 bytes   0.12 s

sched_ext schedulers:

                        x86-64 code, bytes     verified insns
              progs     copies    shared       copies   shared
  scx_qmap       26     109732     50129        36855    36855
  scx_lavd       36     118101     68345        51763    54574
  scx_rusty      14      42107     25912        12697    12697
  scx_chaos      24      76758     57123        16299    16342
  scx_p2dq       23      56888     48299        12531    12531
  scx_layered    24      58401     52662        26795    26626

One copy takes less cache. 32 pairs of tasks ping-pong over pipes on
8 cpus. To evict the scheduler from the caches each task jumps through
1 MB or 2 MB of code per message, L2 is 1 MB. For hot cache it spins as
long as 1 MB takes. run_time_ns of all struct_ops programs per round trip,
copies -> shared:

              hot cache            1 MB of code         2 MB of code
  scx_lavd    2326 -> 2332  0 %    2940 -> 2812 -4 %    10075 -> 9133 -9 %
  scx_rusty   1476 -> 1469  0 %    1741 -> 1731 -1 %     5936 -> 5530 -7 %
  scx_p2dq    2014 -> 2006  0 %    2354 -> 2308 -2 %     7054 -> 6551 -7 %

Round trips per second change from 0 % to +2.8 %, noise is 3 %.

v1 -> v2:
- Rebase on cancellations. Patch 3 is new: landing pads for many main
  programs.
- bpf_jit_supports_exceptions() is gone, use bpf_jit_supports_unwind().
- The limit on cost is for each main program, not for the load.
- libbpf shares when a log level is set too, and prints the verifier log
  of a rejected load.
- Fix the build of the selftest on s390.
v1: https://lore.kernel.org/bpf/20261202001639.2301557-1-alexei.starovoitov@gmail.com/

Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Alexei Starovoitov (7):
  bpf: Prepare for many main programs in one load
  bpf: Check many main programs in one set of insns
  bpf: Generate landing pads for many main programs
  bpf: Take entries at BPF_PROG_LOAD
  libbpf, bpftool: Share subprogs between programs
  libbpf: Share subprogs in light skeletons
  selftests/bpf: Test programs that share subprogs

 Documentation/bpf/verifier.rst                |  47 +-
 include/linux/bpf.h                           |  89 +--
 include/linux/bpf_verifier.h                  |  55 +-
 include/uapi/linux/bpf.h                      |  32 +-
 kernel/bpf/arena.c                            |   2 +-
 kernel/bpf/bpf_struct_ops.c                   |  64 ++-
 kernel/bpf/btf.c                              |  10 +-
 kernel/bpf/cfg.c                              |   8 +-
 kernel/bpf/core.c                             |  50 +-
 kernel/bpf/failing_path.c                     |   6 +-
 kernel/bpf/fixups.c                           | 115 ++--
 kernel/bpf/flow.c                             |  48 +-
 kernel/bpf/pads.c                             |  63 ++-
 kernel/bpf/precision.c                        |   2 +
 kernel/bpf/stream.c                           |  33 +-
 kernel/bpf/syscall.c                          | 302 ++++++++--
 kernel/bpf/verifier.c                         | 534 +++++++++++++++---
 net/ipv4/bpf_tcp_ops.c                        |   3 +-
 tools/bpf/bpftool/cfg.c                       |   4 +
 tools/bpf/bpftool/common.c                    |  26 +-
 tools/bpf/bpftool/prog.c                      |  37 ++
 tools/include/uapi/linux/bpf.h                |  32 +-
 tools/lib/bpf/bpf.c                           |  40 +-
 tools/lib/bpf/bpf.h                           |   9 +-
 tools/lib/bpf/bpf_gen_internal.h              |   3 +-
 tools/lib/bpf/gen_loader.c                    |  83 ++-
 tools/lib/bpf/libbpf.c                        | 349 +++++++++++-
 tools/testing/selftests/bpf/Makefile.skel     |   2 +-
 .../selftests/bpf/prog_tests/bpf_attr_size.c  |   2 +-
 .../selftests/bpf/prog_tests/call_rcu.c       |   2 +-
 .../selftests/bpf/prog_tests/share_subprogs.c | 190 +++++++
 .../selftests/bpf/prog_tests/unwind_attach.c  |   4 +-
 .../selftests/bpf/progs/pro_epilogue.c        |   4 +-
 .../selftests/bpf/progs/share_subprogs.c      | 245 ++++++++
 .../bpf/progs/share_subprogs_arena.c          |  41 ++
 .../bpf/progs/share_subprogs_signed.c         |  87 +++
 36 files changed, 2324 insertions(+), 299 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/share_subprogs.c
 create mode 100644 tools/testing/selftests/bpf/progs/share_subprogs.c
 create mode 100644 tools/testing/selftests/bpf/progs/share_subprogs_arena.c
 create mode 100644 tools/testing/selftests/bpf/progs/share_subprogs_signed.c


base-commit: f7d8db28387ba2732c541caae20b5362b7dd5b7c
-- 
2.55.0


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

end of thread, other threads:[~2026-12-02 22:35 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 21:27 [PATCH v2 bpf-next 0/7] bpf: Share subprogs between programs Alexei Starovoitov
2026-12-02 21:27 ` [PATCH v2 bpf-next 1/7] bpf: Prepare for many main programs in one load Alexei Starovoitov
2026-12-02 22:20   ` bot+bpf-ci
2026-12-02 21:27 ` [PATCH v2 bpf-next 2/7] bpf: Check many main programs in one set of insns Alexei Starovoitov
2026-12-02 22:15   ` sashiko-bot
2026-12-02 22:35   ` bot+bpf-ci
2026-12-02 21:27 ` [PATCH v2 bpf-next 3/7] bpf: Generate landing pads for many main programs Alexei Starovoitov
2026-12-02 22:21   ` bot+bpf-ci
2026-12-02 21:27 ` [PATCH v2 bpf-next 4/7] bpf: Take entries at BPF_PROG_LOAD Alexei Starovoitov
2026-12-02 22:20   ` bot+bpf-ci
2026-12-02 21:27 ` [PATCH v2 bpf-next 5/7] libbpf, bpftool: Share subprogs between programs Alexei Starovoitov
2026-12-02 21:27 ` [PATCH v2 bpf-next 6/7] libbpf: Share subprogs in light skeletons Alexei Starovoitov
2026-12-02 21:27 ` [PATCH v2 bpf-next 7/7] selftests/bpf: Test programs that share subprogs Alexei Starovoitov

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.