Commit 18f4fccb authored by Kumar Kartikeya Dwivedi's avatar Kumar Kartikeya Dwivedi Committed by Alexei Starovoitov

libbpf: Update gen_loader to emit BTF_KIND_FUNC relocations

This change updates the BPF syscall loader to relocate BTF_KIND_FUNC
relocations, with support for weak kfunc relocations. The general idea
is to move map_fds to loader map, and also use the data for storing
kfunc BTF fds. Since both reuse the fd_array parameter, they need to be
kept together.

For map_fds, we reserve MAX_USED_MAPS slots in a region, and for kfunc,
we reserve MAX_KFUNC_DESCS. This is done so that insn->off has more
chances of being <= INT16_MAX than treating data map as a sparse array
and adding fd as needed.

When the MAX_KFUNC_DESCS limit is reached, we fall back to the sparse
array model, so that as long as it does remain <= INT16_MAX, we pass an
index relative to the start of fd_array.

We store all ksyms in an array where we try to avoid calling the
bpf_btf_find_by_name_kind helper, and also reuse the BTF fd that was
already stored. This also speeds up the loading process compared to
emitting calls in all cases, in later tests.
Signed-off-by: default avatarKumar Kartikeya Dwivedi <memxor@gmail.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20211002011757.311265-9-memxor@gmail.com
parent 466b2e13
...@@ -7,6 +7,15 @@ struct ksym_relo_desc { ...@@ -7,6 +7,15 @@ struct ksym_relo_desc {
const char *name; const char *name;
int kind; int kind;
int insn_idx; int insn_idx;
bool is_weak;
};
struct ksym_desc {
const char *name;
int ref;
int kind;
int off;
int insn;
}; };
struct bpf_gen { struct bpf_gen {
...@@ -24,6 +33,10 @@ struct bpf_gen { ...@@ -24,6 +33,10 @@ struct bpf_gen {
int relo_cnt; int relo_cnt;
char attach_target[128]; char attach_target[128];
int attach_kind; int attach_kind;
struct ksym_desc *ksyms;
__u32 nr_ksyms;
int fd_array;
int nr_fd_array;
}; };
void bpf_gen__init(struct bpf_gen *gen, int log_level); void bpf_gen__init(struct bpf_gen *gen, int log_level);
...@@ -36,6 +49,7 @@ void bpf_gen__prog_load(struct bpf_gen *gen, struct bpf_prog_load_params *load_a ...@@ -36,6 +49,7 @@ void bpf_gen__prog_load(struct bpf_gen *gen, struct bpf_prog_load_params *load_a
void bpf_gen__map_update_elem(struct bpf_gen *gen, int map_idx, void *value, __u32 value_size); void bpf_gen__map_update_elem(struct bpf_gen *gen, int map_idx, void *value, __u32 value_size);
void bpf_gen__map_freeze(struct bpf_gen *gen, int map_idx); void bpf_gen__map_freeze(struct bpf_gen *gen, int map_idx);
void bpf_gen__record_attach_target(struct bpf_gen *gen, const char *name, enum bpf_attach_type type); void bpf_gen__record_attach_target(struct bpf_gen *gen, const char *name, enum bpf_attach_type type);
void bpf_gen__record_extern(struct bpf_gen *gen, const char *name, int kind, int insn_idx); void bpf_gen__record_extern(struct bpf_gen *gen, const char *name, bool is_weak, int kind,
int insn_idx);
#endif #endif
This diff is collapsed.
...@@ -6360,12 +6360,12 @@ static int bpf_program__record_externs(struct bpf_program *prog) ...@@ -6360,12 +6360,12 @@ static int bpf_program__record_externs(struct bpf_program *prog)
ext->name); ext->name);
return -ENOTSUP; return -ENOTSUP;
} }
bpf_gen__record_extern(obj->gen_loader, ext->name, BTF_KIND_VAR, bpf_gen__record_extern(obj->gen_loader, ext->name, ext->is_weak,
relo->insn_idx); BTF_KIND_VAR, relo->insn_idx);
break; break;
case RELO_EXTERN_FUNC: case RELO_EXTERN_FUNC:
bpf_gen__record_extern(obj->gen_loader, ext->name, BTF_KIND_FUNC, bpf_gen__record_extern(obj->gen_loader, ext->name, ext->is_weak,
relo->insn_idx); BTF_KIND_FUNC, relo->insn_idx);
break; break;
default: default:
continue; continue;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment