On Sat, Nov 16, 2019 at 11:08 PM Andrii Nakryiko <andriin@xxxxxx> wrote: > > It's often important for BPF program to know kernel version or some specific > config values (e.g., CONFIG_HZ to convert jiffies to seconds) and change or > adjust program logic based on their values. As of today, any such need has to > be resolved by recompiling BPF program for specific kernel and kernel > configuration. In practice this is usually achieved by using BCC and its > embedded LLVM/Clang. With such set up #ifdef CONFIG_XXX and similar > compile-time constructs allow to deal with kernel varieties. > > With CO-RE (Compile Once – Run Everywhere) approach, this is not an option, > unfortunately. All such logic variations have to be done as a normal > C language constructs (i.e., if/else, variables, etc), not a preprocessor > directives. This patch series add support for such advanced scenarios through > C extern variables. These extern variables will be recognized by libbpf and > supplied through extra .extern internal map, similarly to global data. This > .extern map is read-only, which allows BPF verifier to track its content > precisely as constants. That gives an opportunity to have pre-compiled BPF > program, which can potentially use BPF functionality (e.g., BPF helpers) or > kernel features (types, fields, etc), that are available only on a subset of > targeted kernels, while effectively eleminating (through verifier's dead code > detection) such unsupported functionality for other kernels (typically, older > versions). Patch #5 contains all the details. Patch #5 explicitly tests > a scenario of using unsupported BPF helper, to validate the approach. > > As part of this patch set, libbpf also allows usage of initialized global > (non-static) variables, which provides better Clang semantics, which is closer > and better aligned witht kernel vs userspace BPF map contents sharing. > > Outline of the patch set: > - patches #1-#3 do some preliminary refactorings of libbpf relocation logic > and some more clean ups; > - patch #4 allows non-static variables and converts few tests to use them; > - patch #5 adds support for externs to libbpf; > - patch #6 adds tests for externs. Forgot to mention, this patch set is based off of patch set adding mmap()-support for BPF maps, because I use that functionality for selftests. In the worst case scenario, I can switch tests to old-fashioned bpf_map_update_elem()/bpf_map_lookup_elem() interface. [...]