Skip to content

Commit

Permalink
libbpf,bpf: share BTF relocate-related code with kernel
Browse files Browse the repository at this point in the history
Share relocation implementation with the kernel.  As part of this,
we also need the type/string visitation functions so add them to a
btf_common.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; this
depends on struct btf internals so is different in kernel and
userspace.

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@oracle.com>
  • Loading branch information
alan-maguire authored and Kernel Patches Daemon committed May 13, 2024
1 parent 42e5f88 commit f5e22b5
Show file tree
Hide file tree
Showing 7 changed files with 402 additions and 175 deletions.
32 changes: 32 additions & 0 deletions include/linux/btf.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ bool btf_is_kernel(const struct btf *btf);
bool btf_is_module(const struct btf *btf);
struct module *btf_try_get_module(const struct btf *btf);
u32 btf_nr_types(const struct btf *btf);
struct btf *btf_base_btf(const struct btf *btf);
bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
const struct btf_member *m,
u32 expected_offset, u32 expected_size);
Expand Down Expand Up @@ -515,8 +516,15 @@ static inline const struct bpf_struct_ops_desc *bpf_struct_ops_find(struct btf *
}
#endif

typedef int (*type_id_visit_fn)(__u32 *type_id, void *ctx);
typedef int (*str_off_visit_fn)(__u32 *str_off, void *ctx);

#ifdef CONFIG_BPF_SYSCALL
const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
int btf_set_base_btf(struct btf *btf, struct btf *base_btf);
int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **map_ids);
int btf_type_visit_type_ids(struct btf_type *t, type_id_visit_fn visit, void *ctx);
int btf_type_visit_str_offs(struct btf_type *t, str_off_visit_fn visit, void *ctx);
const char *btf_name_by_offset(const struct btf *btf, u32 offset);
struct btf *btf_parse_vmlinux(void);
struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
Expand All @@ -543,6 +551,30 @@ static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
{
return NULL;
}

static inline int btf_set_base_btf(struct btf *btf, struct btf *base_btf)
{
return 0;
}

static inline int btf_relocate(void *log, struct btf *btf, const struct btf *base_btf,
__u32 **map_ids)
{
return 0;
}

static inline int btf_type_visit_type_ids(struct btf_type *t, type_id_visit_fn visit,
void *ctx)
{
return 0;
}

static inline int btf_type_visit_str_offs(struct btf_type *t, str_off_visit_fn visit,
void *ctx)
{
return 0;
}

static inline const char *btf_name_by_offset(const struct btf *btf,
u32 offset)
{
Expand Down
8 changes: 8 additions & 0 deletions kernel/bpf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,11 @@ obj-$(CONFIG_BPF_PRELOAD) += preload/
obj-$(CONFIG_BPF_SYSCALL) += relo_core.o
$(obj)/relo_core.o: $(srctree)/tools/lib/bpf/relo_core.c FORCE
$(call if_changed_rule,cc_o_c)

obj-$(CONFIG_BPF_SYSCALL) += btf_common.o
$(obj)/btf_common.o: $(srctree)/tools/lib/bpf/btf_common.c FORCE
$(call if_changed_rule,cc_o_c)

obj-$(CONFIG_BPF_SYSCALL) += btf_relocate.o
$(obj)/btf_relocate.o: $(srctree)/tools/lib/bpf/btf_relocate.c FORCE
$(call if_changed_rule,cc_o_c)
Loading

0 comments on commit f5e22b5

Please sign in to comment.