BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next v2 0/5] libbpf: BPF program dynamic loading
@ 2026-08-26 18:18 Andrey Grodzovsky
  2026-08-26 18:18 ` [PATCH bpf-next v2 1/5] libbpf: BPF program load type enum Andrey Grodzovsky
                   ` (5 more replies)
  0 siblings, 6 replies; 20+ messages in thread
From: Andrey Grodzovsky @ 2026-08-26 18:18 UTC (permalink / raw)
  To: bpf, andrii; +Cc: martin.kelly, slava.imameev, linux-open-source

This series was originally posted in January 2025 [1] as
a two-patch RFC introducing a per-program tri-state load type
(disabled/auto/dynamic) to let large BPF applications load a subset of
their programs lazily, after the initial bpf_object load.

We looked at the object-wide opt-in flag Andrii suggested as a
lighter-weight alternative to a per-program enum ("all programs may load
later, opt in via bpf_object_open_opts"). Problem was that it collapses two
states that need to stay distinguishable at the level of a single program:[2]

  - a program that is not currently loaded but is *expected* to load
    later, gated on runtime configuration; and
  - a program that is *permanently* not going to load on this kernel
    (e.g. it targets a symbol or struct field that doesn't exist on the
    running kernel, or it lost out to an alternative implementation and
    will never be used on this object instance).

A per-program enum keeps that distinction where it belongs: disabled
stays fully excluded from relocation/map-creation with isolated failure
handling, auto is unchanged, and the new dynamic state is the only one
that opts a given program into later loading -- without implicitly
promoting every other disabled program in the object along with it.

This v2 keeps the same programs and tests we originally proposed, and
addresses two additional requests from Andrii [3]:

1. bpf_program__set_autoload() is converted from bool to the new
   enum bpf_prog_load_type, so it remains source- and ABI-compatible
   with the ~200 existing bool callers (false->0, true->1 already match
   the enum's DISABLED/AUTO numbering), while becoming a thin forwarder
   to bpf_program__set_load_type(). It intentionally continues to reject
   BPF_PROG_LOAD_TYPE_DYNAMIC, to preserve its original on/off meaning --
   dynamic load is only reachable through set_load_type().

2. A declarative way to mark a program dynamic-load-eligible from its
   source, as an alternative to an imperative set_load_type() call, using
   a new BTF decl_tag.

Motivation:

Security tools built on top of libbpf commonly ship as a single large
BPF object containing many programs, only a subset of which are needed
on any given system -- the rest are gated on optional features or on
kernel/runtime capabilities that vary across deployments. For this class
of application, per-program dynamic loading helps in two ways:

  - it shortens the initial load, since only the programs actually
    needed for the running configuration are loaded and attached up
    front instead of the whole object; and
  - it lets a single failing program be unloaded and possibly
    reloaded (or a feature toggled at runtime) without tearing down
    and reloading the entire bpf_object.

Both reduce the time window during which the tool is partially loaded or
inactive -- for a security tool specifically, that is also the time
window during which the system it protects is unprotected.

We have been running this with our internal fork of libbpf in production
for about a year. Depending on kernel and configuration support, around
250 of our programs are potentially loadable on any given system; with
dynamic loading, only around 90 of those are actually loaded by default,
growing to the full 250 only when every optional feature is enabled.
Cutting the default set of loaded programs from 250 down to 90 measurably
reduces load time (we observed ~60% reduction in our own measurements),
and since unload time scales with the number of loaded programs, it
correspondingly shortens unload time as well -- which matters, since
slow unload can delay system shutdown. We believe this functionality
would benefit other libbpf consumers with similarly large, modular BPF
applications.


[1] https://lore.kernel.org/bpf/20250122215206.59859-1-slava.imameev@crowdstrike.com/
[2] https://lore.kernel.org/bpf/CAOu3gNjg5vQ=t0C5UmStNpK8zutFC7kcYTrKt9aiR8Gia+rdNw@mail.gmail.com/
[3] https://lore.kernel.org/bpf/CAEf4BzZKy5Wv_TxtEqsbzW+v-tO28kgY=9RhXrnbPhOZcVbOSA@mail.gmail.com/

Andrey Grodzovsky (2):
  libbpf: Convert bpf_program__set_autoload() to load-type enum
  libbpf: Support declarative dynamic load via BTF decl_tag

Slava Imameev (3):
  libbpf: BPF program load type enum
  libbpf: BPF programs dynamic loading and attaching
  selftests/bpf: Cover BPF program dynamic loading

 tools/lib/bpf/bpf_helpers.h                   |   5 +
 tools/lib/bpf/libbpf.c                        | 273 ++++++++++++++++--
 tools/lib/bpf/libbpf.h                        |  32 +-
 tools/lib/bpf/libbpf.map                      |   4 +
 .../selftests/bpf/prog_tests/dynamicload.c    | 222 ++++++++++++++
 .../selftests/bpf/prog_tests/load_type.c      | 167 +++++++++++
 .../selftests/bpf/progs/test_dynamicload.c    |  40 +++
 .../selftests/bpf/progs/test_load_type.c      |  31 ++
 8 files changed, 741 insertions(+), 33 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/dynamicload.c
 create mode 100644 tools/testing/selftests/bpf/prog_tests/load_type.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_dynamicload.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_load_type.c

-- 
2.34.1


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

end of thread, other threads:[~2026-09-11 23:44 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-26 18:18 [PATCH bpf-next v2 0/5] libbpf: BPF program dynamic loading Andrey Grodzovsky
2026-08-26 18:18 ` [PATCH bpf-next v2 1/5] libbpf: BPF program load type enum Andrey Grodzovsky
2026-08-26 19:25   ` bot+bpf-ci
2026-09-11 23:43   ` Andrii Nakryiko
2026-08-26 18:18 ` [PATCH bpf-next v2 2/5] libbpf: BPF programs dynamic loading and attaching Andrey Grodzovsky
2026-08-26 18:33   ` sashiko-bot
2026-08-26 19:38   ` bot+bpf-ci
2026-09-11 23:44   ` Andrii Nakryiko
2026-08-26 18:18 ` [PATCH bpf-next v2 3/5] libbpf: Convert bpf_program__set_autoload() to load-type enum Andrey Grodzovsky
2026-08-26 18:34   ` sashiko-bot
2026-08-26 19:25   ` bot+bpf-ci
2026-09-11 23:44   ` Andrii Nakryiko
2026-08-26 18:18 ` [PATCH bpf-next v2 4/5] libbpf: Support declarative dynamic load via BTF decl_tag Andrey Grodzovsky
2026-08-26 18:33   ` sashiko-bot
2026-08-26 19:25   ` bot+bpf-ci
2026-08-26 18:18 ` [PATCH bpf-next v2 5/5] selftests/bpf: Cover BPF program dynamic loading Andrey Grodzovsky
2026-08-26 18:33   ` sashiko-bot
2026-08-26 19:25   ` bot+bpf-ci
2026-09-11 23:44   ` Andrii Nakryiko
2026-09-11 23:43 ` [PATCH bpf-next v2 0/5] libbpf: " Andrii Nakryiko

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