Skip to content

refactor(libcgroups): replace libbpf-sys with direct bpf(2) syscalls - #3940

Open
nappa85 wants to merge 3 commits into
youki-dev:mainfrom
nappa85:libcgroups-aya
Open

nappa85 wants to merge 3 commits into
youki-dev:mainfrom
nappa85:libcgroups-aya

Conversation

@nappa85

@nappa85 nappa85 commented Nov 24, 2026 •

Copy link
Copy Markdown

Title: refactor(libcgroups): replace libbpf-sys with direct bpf(2) syscalls

Description

The eBPF device filter (cgroupsv2_devices, which systemd implies since #3721) uses libbpf-sys for only five thin wrappers around the bpf(2) system call: bpf_prog_load, bpf_prog_attach, bpf_prog_detach2, bpf_prog_query and bpf_prog_get_fd_by_id. The program itself is already assembled in Rust with rbpf. Yet libbpf-sys compiles libbpf from C source, so every build needs a C toolchain, make, and the libelf and zlib headers, for the host and for every cross target.

This PR replaces libbpf-sys with a small bpf_sys module:

  • It issues the same five commands with libc::syscall(libc::SYS_bpf, ...).
  • It defines the few kernel ABI types it needs locally (bpf_uapi.rs): the attr structs of the five commands, bpf_insn, the command, program-type and attach-type values, BPF_F_ALLOW_MULTI and the BPF_DEVCG_* constants, copied from include/uapi/linux/bpf.h with size_of/offset_of! assertions pinning every field to its kernel offset. No bindings crate is added, so the MSRV is unchanged.
  • Every command is issued from a zeroed 256-byte attr buffer, because the kernel writes query results back through the pointer (recent kernels also write query.revision).
  • Each function keeps the libbpf contract (a negative return value with errno set), so bpf.rs keeps its logic, its error handling (including ENOSPC on query) and its mockall structure. Only the mock module changes, to the new signatures.
  • Program loads retry EAGAIN up to five times, as libbpf and aya do.
  • The filter is still attached with BPF_PROG_ATTACH, not a BPF link, so it outlives the youki process as before. This is why I didn't use aya's high-level CgroupDevice: on kernels from 5.7 it attaches through bpf_link_create, and the link would close when youki exits. It also loads only from ELF objects.

Two small related changes:

  • The program license used to be passed as the Rust &str "Apache", which isn't NUL-terminated, so the kernel read past its end. It's now passed as a CString.
  • libelf-dev and elfutils-libelf-devel are removed from the build dependency lists in README.md and docs/src/user/basic_setup.md.

Nothing changes in behaviour or features: cgroupsv2_devices and systemd mean what they meant. This PR is independent of #3939: either can be merged without the other.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)
  • Performance improvement
  • Test updates
  • CI/CD related changes
  • Other (please describe):

Testing

  • Added new unit tests

  • Added new integration tests

  • Ran existing test suite

  • Tested manually (please provide steps)

  • Lint: cargo +nightly fmt --all -- --check and cargo clippy --all --all-targets --all-features -- -D warnings pass.

  • Tests: bpf_sys is compiled in tests with a test-only syscall hook, and 10 new unit tests cover the attr encoding, the EAGAIN retry and its limit, error passthrough, E2BIG, the query write-back (including on ENOSPC) and that kernel writes never leave the attr buffer. cargo test -p libcgroups --lib gives 270 passed; the same 9 systemd and D-Bus tests fail on my machine before and after, because it has no systemd.

  • Builds: libcgroups builds with v1, v2, systemd and v2 cgroupsv2_devices, and libcontainer with v2 cgroupsv2_devices and systemd. Cargo.lock only drops libbpf-sys. libcgroups also builds in a stock rust:1 container without libelf headers; its only -sys dependency left is the pure-Rust linux-raw-sys.

  • Not tested on a real kernel: loading and attaching the program needs root, and I couldn't run that on my machine. The contest device tests in the e2e workflow exercise it, and I'll follow their results on this PR.

MSRV: with cgroupsv2_devices on, rbpf 0.4.3 already needs about Rust 1.88 on main, above the declared 1.85; this PR doesn't change that. If you prefer bindings from a crate, aya-obj 0.2.1 builds on 1.85.

Related Issues

Related: #3721.

Additional Context

The CI workflows and container images (cross/Dockerfile.gnu, cross/Dockerfile.musl, .devcontainer, tests/k8s, tools/youki-deploy and the e2e, podman and runc workflows) still install libelf and zlib. I left them alone because some may be needed by other components, such as wasmedge for zlib, and I can't test those images here. I'm happy to clean them up in this PR or a follow-up if you confirm which ones were only for libbpf-sys.

🤖 Generated with Claude Code

The eBPF device filter used libbpf-sys only for five thin wrappers
(prog load, attach, detach, query, get fd by id), which made every
cgroupsv2_devices build compile libbpf from C source and need the libelf
and zlib headers on every host and cross target. The new bpf_sys module
issues those commands with libc::syscall(SYS_bpf), using the kernel ABI
types generated in aya-obj (pure Rust, MIT/Apache-2.0); program loads
retry EAGAIN as libbpf does. The program is still attached with
BPF_PROG_ATTACH, so it outlives the runtime process.

The license string is now passed NUL-terminated (it was a Rust &str).
libelf is dropped from the build dependency lists in the README and the
setup guide.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Marco Napetti <marco.napetti@proton.me>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The default systemd dependency requires Rust 1.98 despite the advertised 1.85 minimum, breaking supported builds, and the new syscall implementation lacks unit coverage.

2 open issues
What changed in this PR

This PR replaces libcgroups’ libbpf dependency with direct bpf(2) syscalls for cgroup v2 device filtering, removing the C-library build requirement while retaining the existing feature wiring.

Changes:

  • Add five syscall wrappers using aya-obj kernel bindings, including program-load retries.
  • NUL-terminate program licenses and adapt existing mocks and tests.
  • Replace dependency declarations and remove libelf from documented build prerequisites.
File Description
README.md Remove libelf build prerequisites.
docs/​src/​user/​basic_setup.md Update build dependency instructions.
crates/​libcgroups/​src/​v2/​devices/​program.rs Use device constants from aya-obj.
crates/​libcgroups/​src/​v2/​devices/​mod.rs Register the syscall module outside unit-test builds.
crates/​libcgroups/​src/​v2/​devices/​mocks.rs Adapt mocks to the new wrapper signatures.
crates/​libcgroups/​src/​v2/​devices/​bpf.rs Call new wrappers and NUL-terminate licenses.
crates/​libcgroups/​src/​v2/​devices/​bpf_sys.rs Implement direct BPF syscall wrappers.
crates/​libcgroups/​Cargo.toml Wire device filtering to aya-obj.
Cargo.toml Replace the workspace libbpf dependency.
Cargo.lock Record the dependency replacement and Windows dependency updates.

Review effort: Balanced

Comment thread crates/libcgroups/Cargo.toml Outdated
serde = { workspace = true, features = ["derive"] }
rbpf = { workspace = true, optional = true }
libbpf-sys = { workspace = true, optional = true }
aya-obj = { workspace = true, optional = true }

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aya-obj is gone (b9191f2): the few UAPI structs and constants are defined locally in bpf_uapi.rs, with offset_of! assertions against the kernel layout, so there is no bindings dependency and no new MSRV.

If you'd rather keep bindings from a crate: aya-obj 0.2.1 declares no rust-version and builds on 1.85 (0.3.0 needs 1.87, 0.5+ needs 1.98). I preferred local definitions because 0.2.1 brings about ten crates (object, hashbrown, thiserror 1, ...) for four structs, and any later bump would break the MSRV again. Happy to switch if you prefer it.

Note that 1.85 isn't met on main either once cgroupsv2_devices is on: rbpf 0.4.3 uses let chains and is_multiple_of (≈1.88), and cargo +1.85.0 check -p libcgroups fails on main the same way. That's outside this PR, but I'm happy to help if you want to raise rust-version or deal with rbpf.

Comment thread crates/libcgroups/src/v2/devices/mod.rs Outdated
@@ -1,4 +1,6 @@
pub mod bpf;
#[cfg(not(test))]

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added (b9191f2, f4eb3f9): bpf_sys is now compiled in tests with a test-only syscall hook. Ten tests cover:

  • the attr encoding per command;
  • success after EAGAIN, the five-attempt limit and immediate return on other errors;
  • E2BIG for an oversized instruction count;
  • the prog_cnt/attach_flags write-back, including on ENOSPC.

Every command is issued from a zeroed 256-byte attr buffer, because the kernel writes query results back through the pointer (recent kernels also write query.revision). One test has the fake kernel fill the whole buffer and checks that nothing beside the caller's struct changes.

aya-obj 0.6 requires Rust 1.98 while libcgroups declares 1.85. Drop it
and declare the needed kernel UAPI types in v2/devices/bpf_uapi.rs
(per-command attr structs, bpf_insn, command/prog-type/attach-type
values, BPF_F_ALLOW_MULTI, BPF_DEVCG_*), with const size/offset
assertions against include/uapi/linux/bpf.h. Each command now passes
size_of its own attr struct; the kernel zero-fills the rest.

bpf_sys is now compiled in tests with a thread-local syscall hook (test
builds only) and unit-tested: attr encoding per command, retry after
EAGAIN, immediate return on other errors, the five-attempt limit and
query output propagation including on ENOSPC.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Marco Napetti <marco.napetti@proton.me>
The kernel writes BPF_PROG_QUERY results back through the attr pointer
whatever size it is given: attach_flags and prog_cnt, and since the cgroup
revisions also query.revision at offset 56. A command struct shorter than
that would be overrun. Every command is now copied into a zeroed, 8-byte
aligned 256-byte buffer and issued with that size (the kernel only requires
the tail beyond its own union to be zero); the struct's fields are copied
back afterwards. ProgQueryAttr declares the fields up to revision, with
offset assertions.

Tests: the fake syscall checks the size and the zeroed tail, writes revision
on success and ENOSPC, and a 0xFF-filling kernel leaves memory beside the
caller's struct untouched.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Marco Napetti <marco.napetti@proton.me>
@nayuta723

Copy link
Copy Markdown
Contributor

Thank you for working on this!
We're generally in favor of this approach. That said, replacing libbpf-sys is a fairly significant change, and youki's current test coverage for devices isn't sufficient yet to be confident that all of our expected use cases are covered.
We're adding more device tests in the following PRs:

Once they're merged, could you rebase this PR onto them? We'll make the final call on merging after confirming that the tests pass with this change.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants