BPF loads can share one copy of common functions across programs
Alexei Starovoitov's series cuts verification and JIT work for schedulers and datapaths, with libbpf doing the shared load by default on x86-64 and arm64.
A scheduler or a packet datapath is often many BPF programs that call the same functions. Until now each program carried its own copy, so the kernel verified and compiled that code once per program. Alexei Starovoitov has posted a second version of a series that lets one BPF program load share those functions, returning a file descriptor for each entry point from a single load.
Each entry still looks like a normal program: its own file descriptor, id, name, statistics, and attach point. The compiled code lives in an owner that user space cannot attach, pin, or run, and that owner is freed with the last entry. Tools can still dump the code by looking up the owner's id. The load needs CAP_BPF, and every entry still passes the usual token and LSM checks. For now only the struct_ops member and the sleepable flag may differ between entries.
The numbers are the point. Thirty-two traffic-control programs that call the same 65 global functions drop from about 1.06 million instructions and 4.5 MB of x86-64 code, loaded in 1.60 seconds, to about 33,000 instructions and 143 KB, loaded in 0.12 seconds. sched_ext schedulers shrink too: scx_qmap's generated code falls from about 110 KB to 50 KB across 26 programs, and scx_lavd from about 118 KB to 68 KB across 36. With the instruction cache hot, runtime barely moves. When tasks are forced to push the scheduler out of a 1 MB or 2 MB cache, Starovoitov measured up to about 9 percent less time in struct_ops programs per round trip, and throughput changes from flat to about 2.8 percent, inside the noise of the test.
libbpf will do this by default for programs of one type and attach type, or of one struct_ops map, that call the same functions. If the kernel has no support or rejects the shared load, it falls back to separate copies. Light skeletons, signed or not, try the shared load first and skip the copies once the programs already have file descriptors. Tracing, LSM, and extension programs, plus programs that use tail calls, keep their own copies, as do cpumap and devmap programs and objects that preserve map elements. An extension cannot replace a shared function, and a tracing program attached through any entry sees calls from all of them. The feature is limited to x86-64 and arm64, the architectures that already support BPF exceptions.