All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next 0/6] bpf: Share subprogs between programs
@ 2026-12-02  0:16 Alexei Starovoitov
  2026-12-02  0:16 ` [PATCH bpf-next 1/6] bpf: Prepare for many main programs in one load Alexei Starovoitov
                   ` (5 more replies)
  0 siblings, 6 replies; 11+ messages in thread
From: Alexei Starovoitov @ 2026-12-02  0:16 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 JITs that support bpf_throw(). 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.15 s
  one copy      33118      31070    143034 bytes   0.08 s

sched_ext schedulers:

                        x86-64 code, bytes     verified insns
              progs     copies    shared       copies   shared
  scx_qmap       26     106139     48249        36855    36855
  scx_lavd       36     115018     66090        51788    54599
  scx_rusty      14      40451     24870        12707    12707
  scx_chaos      24      74870     55777        16325    16368
  scx_p2dq       23      55318     47068        12543    12543
  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 %

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

Alexei Starovoitov (6):
  bpf: Prepare for many main programs in one load
  bpf: Check many main programs in one set of insns
  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                |  91 +++-
 include/linux/bpf.h                           |  87 ++--
 include/linux/bpf_verifier.h                  |  40 +-
 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                           | 113 +++--
 kernel/bpf/flow.c                             |  54 ++-
 kernel/bpf/precision.c                        |   2 +
 kernel/bpf/stream.c                           |  33 +-
 kernel/bpf/syscall.c                          | 302 ++++++++++--
 kernel/bpf/verifier.c                         | 435 ++++++++++++++++--
 kernel/events/core.c                          |   4 +
 kernel/sched/ext/ext.c                        |   2 +-
 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                        | 341 ++++++++++++--
 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 | 134 ++++++
 .../selftests/bpf/prog_tests/unwind_attach.c  |   4 +-
 .../selftests/bpf/progs/pro_epilogue.c        |   4 +-
 .../selftests/bpf/progs/share_subprogs.c      | 143 ++++++
 .../bpf/progs/share_subprogs_signed.c         |  87 ++++
 36 files changed, 2023 insertions(+), 268 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_signed.c


base-commit: ac69c149f965b55e85798e516cb66e2bceb63f00
-- 
2.55.0


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

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

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  0:16 [PATCH bpf-next 0/6] bpf: Share subprogs between programs Alexei Starovoitov
2026-12-02  0:16 ` [PATCH bpf-next 1/6] bpf: Prepare for many main programs in one load Alexei Starovoitov
2026-12-02  0:16 ` [PATCH bpf-next 2/6] bpf: Check many main programs in one set of insns Alexei Starovoitov
2026-12-02  1:13   ` bot+bpf-ci
2026-12-02  1:18   ` sashiko-bot
2026-12-02  0:16 ` [PATCH bpf-next 3/6] bpf: Take entries at BPF_PROG_LOAD Alexei Starovoitov
2026-12-02  1:13   ` bot+bpf-ci
2026-12-02  0:16 ` [PATCH bpf-next 4/6] libbpf, bpftool: Share subprogs between programs Alexei Starovoitov
2026-12-02  1:13   ` bot+bpf-ci
2026-12-02  0:16 ` [PATCH bpf-next 5/6] libbpf: Share subprogs in light skeletons Alexei Starovoitov
2026-12-02  0:16 ` [PATCH bpf-next 6/6] 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.