Repository navigation
Remove dependency on vmlinux.h - #40482
dramforever wants to merge 6 commits into
Conversation
Instead of relying on vmlinux.h, bring in types from UAPI headers and define the remaining types ourselves, similar to what we do in restrict-fs.bpf.c.
Instead of relying on vmlinux.h, bring in types from UAPI headers.
Since vmlinux.h is no longer required, this feature is now meaningless. Remove it. There was a test checking for BTF. Change it to check BPF_FRAMEWORK instead.
Since vmlinux.h is no longer required, HAVE_VMLINUX_H is now meaningless. Check BPF_FRAMEWORK instead.
c5b1b40 to
e9b183c
Compare
Since vmlinux.h is no longer required, all related build system code can be removed. Make ENABLE_SYSCTL_BPF depend on BPF_FRAMEWORK instead of HAVE_VMLINUX_H.
Nothing includes vmlinux.h now. Remove it from .clang-format.
e9b183c to
8db7572
Compare
|
Very strongly disagree with this. All main distributions now provide an appropriate vmlinux.h in a package that can be trivially used. It's very simple to generate and package when building the kernel. |
|
I agree with @bluca. If there is no good way to get vmlinux.h in your environment, that's something to solve in your environment, not in every project that uses vmlinux.h for its BPF programs. |
As a distribution, we support different kernels. There's equally no guarantee that a vmlinux.h we generate from one kernel will "work on any [other] kernel past, future or present". Are you saying we need to build one systemd per kernel? |
It's up to you to make sure you use one that guarantees the highest compatibility range. What that means is unclear to me, probably you want to pick the kernel build with the largest number of enabled features among the ones you have, and hope there's no incompatibility. But you definitely have higher chances to make that work, than hardcoding the one generated on someone's laptop, and hoping it never breaks and works everywhere, backward and forward in time. |
Perhaps there was a misunderstanding? I didn't generate a libbpf's CO-RE support handles actually accessing kernel structure fields such that the BPF object code is resilient to changes in the kernel-internal This isn't even a new thing in-tree. Of the 6 Of course whether explicitly defining used types and fields is preferable remains up to discussion, but I thought I should make it clear that this isn't vendoring a generated |
you don't, ebpf knows how to deal with slight differences in the structures. hence: you need a somewhat recent vmlinuz.h, but not one that strictly matches your kernel, unless the structures we bind to are massively changed, i.e. more than just offset changes. |
It's pretty much equivalent though. There is no guarantee whatsoever that what you generated/copied/manually written here is going to be compatible with a riscv32 kernel 3 years from now with a different kconfig, and viceversa. There is no guarantee of compatibility made by these bpf headers, we don't know what it will be a year from now, what it was a year ago, what it's going to be once the next 1, 2, 3 modules get added. The safest approach is the one we took - get distributions to generate a vmlinux.h when the target kernels are built, so that there is a reasonable hope of compatibility since there is as-near-as-it-can-be 1:1 matching between what is built and what is ran, with the undertanding that it will always be best-effort. So far it worked out pretty well. It took a great deal of effort to get all of this infrastructure sorted out and in place, and I very strongly disagree with throwing all of that away and go back to hope things might just work out somehow with hardcoding definitions, just because one distribution hasn't done the work to package the vmlinux.h yet. |
|
We absolutely can add it. We're not bringing this because packaging the header is difficult, but because to us it looks less safe than the approach suggested here. (@dramforever can probably explain the logic behind that better than I can.) |
|
@alyssais I don't think it is less safe, since BTF type checking should catch exactly the same set of problems. My main point is build complexity. Perhaps I should elaborate one use case. Let's say I want to cross compile a source distro, and I want to use systemd on it with unprivileged nspawn enabled, because there's no reason it shouldn't work. Here, I can't use the build machine's And many different machines need their own kernels (16 KiB page arm64, kernel forks for random platforms, some mips64 machine-specific configs...), so I have a few options on wiring this up:
All of these approaches are relatively easy with Nixpkgs, but it is my opinion that none of these are necessary. Not having to depend on the kernel is the reason many projects maintain a |
Yes, but that's why distributions now generate and ship it as part of their kernel builds in a package that you can install and reference. You don't need to come up with anything, just use what the kernel package build provides. In fact one of the reason to package this is to avoid everyone having to figure out what they are supposed to do every time, from scratch, and take wild guesses. You have generated/written/copied a subset of headers here that you have tested on nixos x86-64 with a current kernel, I assume. You have no way to know that this is going to work out of the box 2 years from now on a debian loongarch kernel. You have no way to know that it works with a fedora armv7 kernel from 2 years argo. This is not a whack-a-mole game that we can play and win, it is not sustainable, it risks breaking every time someone changes arch/kconfig/distro, and every time a new bpf program is added. Using a header generated by the distro from a kernel close enough to the one that is going to be deployed is not a perfect solution, but it's by far the safest and most-compatible one that we have. If your distro has multiple kernels at the same time, that's not really a difficult problem - just pick one and go with it, and the result is not really any worse than hardcoding everything. Maybe one day the BPF internal interfaces will have the same stability guarantees as UAPI interfaces, but for now that's not the case, and we have to deal with what we have, unfortunately. |
Using For compatibility with multiple kernel versions, we need to use CO-RE queries to check for variations and pick the correct types to use. When that happens we need to explicitly define multiple type variations and can't rely on a generated See https://docs.ebpf.io/ebpf-library/libbpf/ebpf/ "CO-RE queries", noting in particular the example for |
|
I've found a recent-ish real example of eBPF handling of kernel internal ABI changes. I hope this will, if not convince you, at least provide a reference to what I have been talking about. This is in https://github.com/loresuso/psc and AFAICT is not yet completely resolved:
What happened was that in Linux 6.3 the type // Linux < 6.3
typedef struct kernel_cap_struct {
__u32 cap[_KERNEL_CAPABILITY_U32S];
} kernel_cap_t;To: // Linux >= 6.3
typedef struct { u64 val; } kernel_cap_t;Therefore, if you want your eBPF program to be built with a new kernel's struct kernel_cap_struct___old {
__u32 cap[2];
} __attribute__((preserve_access_index));
if (bpf_core_field_exists(cred->cap_effective.val)) {
/* (Indirectly) use kernel_cap_t from vmlinux.h */
} else {
/* Use struct kernel_cap_struct___old */
}If, however, you want the eBPF program to be buildable with an old kernel's typedef struct {
u64 val;
} __attribute__((preserve_access_index)) kernel_cap_t___new;And the This is why I said that "Using (Even if it doesn't change that much, say just a So, even though I can acknowledge that |
Terrible. I do not think it is not 'internal', as it is exposed through vmlinux.h ... |
Sadly not the world we live in, and unfortunately I don't think there's anything we can do about this. Most? "advanced" eBPF programs (HID, LSM, kprobe...) are basically slightly safer kernel modules and "enjoy" the same source and binary compat breakages. We make do with what we can with CO-RE relocations and queries. Also even if the types don't change, the kprobe in BPF-LSM at least is more of an interface. Still kernel internal though... The only way to "truly" fix these is to somehow have the kernel support this inherently. The fact that these eBPF CO-RE stuff even exists speaks to the difficulty of that, though... |
|
I am not sure I follow? ebpf works the way it works: the kernel's internals are not generally stable, and the ebpf code inserts itself into that world, so it has to accept that. Our ebpf programs hence are not completely and universally and forever stable, because they can't be, because they use kernel constructs which are not considered stable forever. But then again, the interfaces that we use, have been very stable in in the past years, at least to the level that CO-RE can handle them fine. So it's empiric stability, not political stability, if you so will. But this all has nothing to do with vmlinux.h, because that just is a convenient way for us to make use of kernel structures. If you do eBPF it's kinda what you have to do, unless you copy the relevant structs into your own code, but this wins you nothing, they do not become stable kernel's ABI. Hence, I am not impressed by @dramforever's points at all: they might be worth something if the discussion was about "should we use ebpf at all?" or about "should the kernel have an internal stable ABI?". But this is not the discussion we are having here, we have one about vmlinux.h and that's orthogonal to both questions. Hence, sorry, I really don#t get what the issue is supposed to be. |
|
I mean, you are just copying the relevant structs from vmlinux.h into our codebase. how does that change anything about anything? you just renamed the file where the structs that come with weak abi guarantees are placed. that makes no effective difference. hence, sorry, but this PR seems entirely unnecessary? also, systemd is not the only subsystem using ebpf. Are you patching vmlinux.h out of everything? who is helped by that??? |
|
My original point was the point made in the pull request description: Depending on a If among:
The systemd team thinks the convenience of 2 outweighs the build process complication, then I can accept that and consider this a wontfix. However, the discussion was muddled when
Most of the typing has been spent on arguing this...
Exactly. I agree that this doesn't change anything regarding compatibility. |
In this patch series, some types are copied ("vendored") into our source. This drops the dependency on (EDIT: I wrote this in parallel with the discussion happening above. My question is answered.) |
That may be the case on NIX, but it is certainly not the case anywhere else. We just got the file packaged in the relevant distros, and then it's just a build dependency like any other, and like the other kernel UAPI headers or so. There's no complication whatsoever, just a new build-depends. |
|
Also the Fedora packaging guidelines are going to require using the prebuilt vmlinux.h, rather than hardcoding and vendoring, so more and more software will require a shipped vmlinux.h: https://fosdem.org/2026/schedule/event/VSXPA8-packaging-ebpf-in-linux-distros/ |
Imagine if every rpm that depends on libsystemd had to be rebuilt every time the kernel was updated. That's essentially what happens in an input-addressed build system. Nixpkgs (and other similarly shaped systems) need a way out to avoid having the kernel appear as a transitive dependency through systemd for many other packages. |
|
Sorry, but hardcoding and vendoring generated code from someone's laptop is really not something worth doing to solve an internal nix build issue (that doesn't happen on any other distro, fyi). If you want to vendor this kernel header, you can just do it in your nix package. You don't even need a patch, you just point the meson option to where you vendor it in the source tree. |
|
I don't mind if systemd maintainers don't care about making Nixpkgs's life easier with this. If it's just for Nixpkgs I wouldn't have bothered posting this PR here. However, systemd isn't just used in "normal" binary distros. For any source distro, as what is currently implemented, we can't build a fully featured systemd, a core, userland program, without having to also build a kernel. If the systemd maintainers think that's desirable, then I don't mind keeping whatever stuff we need to get this working within Nixpkgs/NixOS. |
Understood, and I am not pushing for any particular solution. Just wanted to highlight that the issue at hand. I'm not sure it's true that nixpkgs is the only distro with this issue for what it's worth. It would be nice if there was a proper solution; as @pottering suggests maybe such a thing would have to be in kernel space. Finding the common ground, long term solution that can be deployed today would be nice, but if it requires planting trees then that is worth acknowledging as well. |
|
Something that all you systemd maintainers have missed is that what @dramforever is trying to do is not some weird thing us Nix people have made up, but in fact established CO-RE best practice. Here's my evidence:
Also to address two things @poettering said
The key point is that we're not coppying the entire struct whole hog --- that would indeed be mindless vendoring. We're instead just putting in the data definitions that the programs actually used --- small portions of the relevant structs. This is much better --- it is documenting exactly to what the assumptions on the kernel interfaces are. Or, in the original since of "interface" as the intersection of what's wanted/provided, it is exactly the interlace between the BFP program and the kernel, explicitly written down. (If anyone is familiar with https://ocaml.org/docs/functors, that is a useful comparison to the above.)
You can think of the above as codifying the "empirically stable" bits, rather than keeping it an unspoken assumption as to which aspects of
Yes ideally every program would do this, but systemd is especially early in our bootstrap / dependency graph, because of the core libraries it provides. We have some hacks to build the libraries and the executables separately, and we might end up using them if this PR is not accepted. But it would be much nicer if you all would use e.g. Meson subprojects to natively support building different parts of systemd separately --- really, that would be fantastic. Then things like |
Sorry, but you are incorrect. This is neither hardcoding nor vendoring, at least not to the extent more than what's already conveyed in-tree. This PR doesn't change that fact in any way, other than it clearly defines the names of used structure fields before they are used, and removes the (arguably, messy) dependency on a |
|
Can someone read the latest comments? I think I wrote a pretty air-tight, backed-with-evidenced argument, if I may so myself, but it's been crickets ever since (though I do appreciate @LekKit's agreement :)). |
Don't depend on
vmlinux.hfor types in*.bpf.c. Instead, follow what was done inrestrict-fs.bpf.c, and include what we can from ABI stable user headers, and define the rest ourselves, with__attribute__((preserve_access_index))on structures.Depending on
vmlinux.hcomplicates the build process. There doesn't seem to be a clean way to get avmlinux.h:vmlinux.hfrom the running kernel with/sys/kernel/btf/vmlinuxdoesn't work for environments that deliberately hide/sysfor build reproducibility.vmlinux.hwithout building an entire kernel at the same time, which complicates the build dependencies of systemd. This is especially painful for building systemd from source, including cross compilation use cases.vmlinux.hbut has a "Development-only" warning. Even if you ignore that warning, it doesn't enable enough configs to providestruct bpf_sysctl.So IMO not depending on
vmlinux.his the better choice. Using our own types is no worse than loading these BPF programs on the kernel version different from the one that generatedvmlinux.h, and, if anything, makes it more explicit what implementation details of the kernel we actually depend on.Summary of what was done:
userns-restrict.bpf.candsysctl-monitor.bpf.cno longer depends onvmlinux.hfor types.vmlinux-handvmlinux-h-pathand associated handling are removed since they're unused.HAVE_VMLINUX_His removed. References are removed or changed to the more generalBPF_FRAMEWORK.+/-BTFfeature is removed since it's now meaningless.test/units/TEST-29-PORTABLE.user.shreferred to it, so it was changed to checkBPF_FRAMEWORK.