On Tue, Jun 18, 2024 at 9:25 AM Alan Maguire <alan.maguire@xxxxxxxxxx> wrote: > > Share relocation implementation with the kernel. As part of this, > we also need the type/string iteration functions so add them to a > btf_iter.c file that also gets shared with the kernel. Relocation > code in kernel and userspace is identical save for the impementation > of the reparenting of split BTF to the relocated base BTF and > retrieval of BTF header from "struct btf"; these small functions > need separate user-space and kernel implementations. > > One other wrinkle on the kernel side is we have to map .BTF.ids in > modules as they were generated with the type ids used at BTF encoding > time. btf_relocate() optionally returns an array mapping from old BTF > ids to relocated ids, so we use that to fix up these references where > needed for kfuncs. > > Signed-off-by: Alan Maguire <alan.maguire@xxxxxxxxxx> > --- > include/linux/btf.h | 64 +++++++++++++ > kernel/bpf/Makefile | 10 +- > kernel/bpf/btf.c | 176 ++++++++++++++++++++++++---------- > tools/lib/bpf/Build | 2 +- > tools/lib/bpf/btf.c | 162 -------------------------------- > tools/lib/bpf/btf_iter.c | 177 +++++++++++++++++++++++++++++++++++ > tools/lib/bpf/btf_relocate.c | 23 +++++ > 7 files changed, 398 insertions(+), 216 deletions(-) > create mode 100644 tools/lib/bpf/btf_iter.c > I'd do btf_iter.c addition in a separate patch, but other than that looks good. See a nit below. > diff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile > index 7eb9ad3a3ae6..d9d148992fbf 100644 > --- a/kernel/bpf/Makefile > +++ b/kernel/bpf/Makefile > @@ -50,5 +50,13 @@ endif > obj-$(CONFIG_BPF_PRELOAD) += preload/ > > obj-$(CONFIG_BPF_SYSCALL) += relo_core.o > -$(obj)/relo_core.o: $(srctree)/tools/lib/bpf/relo_core.c FORCE > + > +obj-$(CONFIG_BPF_SYSCALL) += btf_iter.o > + > +obj-$(CONFIG_BPF_SYSCALL) += btf_relocate.o nit: do we need those empty lines above? let's keep all the shared kernel/libbpf object files in one group without empty lines > + > +# Some source files are common to libbpf. > +vpath %.c $(srctree)/kernel/bpf:$(srctree)/tools/lib/bpf this is something new, what does vpath do? (sorry if this was discussed before and I missed it) > + > +$(obj)/%.o: %.c FORCE > $(call if_changed_rule,cc_o_c) > diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c > index ce4707968217..8e12cb80ba73 100644 > --- a/kernel/bpf/btf.c > +++ b/kernel/bpf/btf.c > @@ -274,6 +274,7 @@ struct btf { > u32 start_str_off; /* first string offset (0 for base BTF) */ > char name[MODULE_NAME_LEN]; > bool kernel_btf; > + __u32 *base_id_map; /* map from distilled base BTF -> vmlinux BTF ids */ > }; > [...]