Commit 708cdc57 authored by Alexei Starovoitov's avatar Alexei Starovoitov Committed by Andrii Nakryiko

libbpf: Support kfunc detection in light skeleton.

Teach gen_loader to find {btf_id, btf_obj_fd} of kernel variables and kfuncs
and populate corresponding ld_imm64 and bpf_call insns.
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20230321203854.3035-4-alexei.starovoitov@gmail.com
parent 1057d299
...@@ -11,6 +11,7 @@ struct ksym_relo_desc { ...@@ -11,6 +11,7 @@ struct ksym_relo_desc {
int insn_idx; int insn_idx;
bool is_weak; bool is_weak;
bool is_typeless; bool is_typeless;
bool is_ld64;
}; };
struct ksym_desc { struct ksym_desc {
...@@ -24,6 +25,7 @@ struct ksym_desc { ...@@ -24,6 +25,7 @@ struct ksym_desc {
bool typeless; bool typeless;
}; };
int insn; int insn;
bool is_ld64;
}; };
struct bpf_gen { struct bpf_gen {
...@@ -65,7 +67,7 @@ void bpf_gen__map_update_elem(struct bpf_gen *gen, int map_idx, void *value, __u ...@@ -65,7 +67,7 @@ void bpf_gen__map_update_elem(struct bpf_gen *gen, int map_idx, void *value, __u
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, bool is_weak, void bpf_gen__record_extern(struct bpf_gen *gen, const char *name, bool is_weak,
bool is_typeless, int kind, int insn_idx); bool is_typeless, bool is_ld64, int kind, int insn_idx);
void bpf_gen__record_relo_core(struct bpf_gen *gen, const struct bpf_core_relo *core_relo); void bpf_gen__record_relo_core(struct bpf_gen *gen, const struct bpf_core_relo *core_relo);
void bpf_gen__populate_outer_map(struct bpf_gen *gen, int outer_map_idx, int key, int inner_map_idx); void bpf_gen__populate_outer_map(struct bpf_gen *gen, int outer_map_idx, int key, int inner_map_idx);
......
...@@ -560,7 +560,7 @@ static void emit_find_attach_target(struct bpf_gen *gen) ...@@ -560,7 +560,7 @@ static void emit_find_attach_target(struct bpf_gen *gen)
} }
void bpf_gen__record_extern(struct bpf_gen *gen, const char *name, bool is_weak, void bpf_gen__record_extern(struct bpf_gen *gen, const char *name, bool is_weak,
bool is_typeless, int kind, int insn_idx) bool is_typeless, bool is_ld64, int kind, int insn_idx)
{ {
struct ksym_relo_desc *relo; struct ksym_relo_desc *relo;
...@@ -574,6 +574,7 @@ void bpf_gen__record_extern(struct bpf_gen *gen, const char *name, bool is_weak, ...@@ -574,6 +574,7 @@ void bpf_gen__record_extern(struct bpf_gen *gen, const char *name, bool is_weak,
relo->name = name; relo->name = name;
relo->is_weak = is_weak; relo->is_weak = is_weak;
relo->is_typeless = is_typeless; relo->is_typeless = is_typeless;
relo->is_ld64 = is_ld64;
relo->kind = kind; relo->kind = kind;
relo->insn_idx = insn_idx; relo->insn_idx = insn_idx;
gen->relo_cnt++; gen->relo_cnt++;
...@@ -586,9 +587,11 @@ static struct ksym_desc *get_ksym_desc(struct bpf_gen *gen, struct ksym_relo_des ...@@ -586,9 +587,11 @@ static struct ksym_desc *get_ksym_desc(struct bpf_gen *gen, struct ksym_relo_des
int i; int i;
for (i = 0; i < gen->nr_ksyms; i++) { for (i = 0; i < gen->nr_ksyms; i++) {
if (!strcmp(gen->ksyms[i].name, relo->name)) { kdesc = &gen->ksyms[i];
gen->ksyms[i].ref++; if (kdesc->kind == relo->kind && kdesc->is_ld64 == relo->is_ld64 &&
return &gen->ksyms[i]; !strcmp(kdesc->name, relo->name)) {
kdesc->ref++;
return kdesc;
} }
} }
kdesc = libbpf_reallocarray(gen->ksyms, gen->nr_ksyms + 1, sizeof(*kdesc)); kdesc = libbpf_reallocarray(gen->ksyms, gen->nr_ksyms + 1, sizeof(*kdesc));
...@@ -603,6 +606,7 @@ static struct ksym_desc *get_ksym_desc(struct bpf_gen *gen, struct ksym_relo_des ...@@ -603,6 +606,7 @@ static struct ksym_desc *get_ksym_desc(struct bpf_gen *gen, struct ksym_relo_des
kdesc->ref = 1; kdesc->ref = 1;
kdesc->off = 0; kdesc->off = 0;
kdesc->insn = 0; kdesc->insn = 0;
kdesc->is_ld64 = relo->is_ld64;
return kdesc; return kdesc;
} }
...@@ -864,23 +868,17 @@ static void emit_relo(struct bpf_gen *gen, struct ksym_relo_desc *relo, int insn ...@@ -864,23 +868,17 @@ static void emit_relo(struct bpf_gen *gen, struct ksym_relo_desc *relo, int insn
{ {
int insn; int insn;
pr_debug("gen: emit_relo (%d): %s at %d\n", relo->kind, relo->name, relo->insn_idx); pr_debug("gen: emit_relo (%d): %s at %d %s\n",
relo->kind, relo->name, relo->insn_idx, relo->is_ld64 ? "ld64" : "call");
insn = insns + sizeof(struct bpf_insn) * relo->insn_idx; insn = insns + sizeof(struct bpf_insn) * relo->insn_idx;
emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_8, BPF_PSEUDO_MAP_IDX_VALUE, 0, 0, 0, insn)); emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_8, BPF_PSEUDO_MAP_IDX_VALUE, 0, 0, 0, insn));
switch (relo->kind) { if (relo->is_ld64) {
case BTF_KIND_VAR:
if (relo->is_typeless) if (relo->is_typeless)
emit_relo_ksym_typeless(gen, relo, insn); emit_relo_ksym_typeless(gen, relo, insn);
else else
emit_relo_ksym_btf(gen, relo, insn); emit_relo_ksym_btf(gen, relo, insn);
break; } else {
case BTF_KIND_FUNC:
emit_relo_kfunc_btf(gen, relo, insn); emit_relo_kfunc_btf(gen, relo, insn);
break;
default:
pr_warn("Unknown relocation kind '%d'\n", relo->kind);
gen->error = -EDOM;
return;
} }
} }
...@@ -903,18 +901,20 @@ static void cleanup_core_relo(struct bpf_gen *gen) ...@@ -903,18 +901,20 @@ static void cleanup_core_relo(struct bpf_gen *gen)
static void cleanup_relos(struct bpf_gen *gen, int insns) static void cleanup_relos(struct bpf_gen *gen, int insns)
{ {
struct ksym_desc *kdesc;
int i, insn; int i, insn;
for (i = 0; i < gen->nr_ksyms; i++) { for (i = 0; i < gen->nr_ksyms; i++) {
kdesc = &gen->ksyms[i];
/* only close fds for typed ksyms and kfuncs */ /* only close fds for typed ksyms and kfuncs */
if (gen->ksyms[i].kind == BTF_KIND_VAR && !gen->ksyms[i].typeless) { if (kdesc->is_ld64 && !kdesc->typeless) {
/* close fd recorded in insn[insn_idx + 1].imm */ /* close fd recorded in insn[insn_idx + 1].imm */
insn = gen->ksyms[i].insn; insn = kdesc->insn;
insn += sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm); insn += sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm);
emit_sys_close_blob(gen, insn); emit_sys_close_blob(gen, insn);
} else if (gen->ksyms[i].kind == BTF_KIND_FUNC) { } else if (!kdesc->is_ld64) {
emit_sys_close_blob(gen, blob_fd_array_off(gen, gen->ksyms[i].off)); emit_sys_close_blob(gen, blob_fd_array_off(gen, kdesc->off));
if (gen->ksyms[i].off < MAX_FD_ARRAY_SZ) if (kdesc->off < MAX_FD_ARRAY_SZ)
gen->nr_fd_array--; gen->nr_fd_array--;
} }
} }
......
...@@ -7070,18 +7070,21 @@ static int bpf_program_record_relos(struct bpf_program *prog) ...@@ -7070,18 +7070,21 @@ static int bpf_program_record_relos(struct bpf_program *prog)
for (i = 0; i < prog->nr_reloc; i++) { for (i = 0; i < prog->nr_reloc; i++) {
struct reloc_desc *relo = &prog->reloc_desc[i]; struct reloc_desc *relo = &prog->reloc_desc[i];
struct extern_desc *ext = &obj->externs[relo->sym_off]; struct extern_desc *ext = &obj->externs[relo->sym_off];
int kind;
switch (relo->type) { switch (relo->type) {
case RELO_EXTERN_LD64: case RELO_EXTERN_LD64:
if (ext->type != EXT_KSYM) if (ext->type != EXT_KSYM)
continue; continue;
kind = btf_is_var(btf__type_by_id(obj->btf, ext->btf_id)) ?
BTF_KIND_VAR : BTF_KIND_FUNC;
bpf_gen__record_extern(obj->gen_loader, ext->name, bpf_gen__record_extern(obj->gen_loader, ext->name,
ext->is_weak, !ext->ksym.type_id, ext->is_weak, !ext->ksym.type_id,
BTF_KIND_VAR, relo->insn_idx); true, kind, relo->insn_idx);
break; break;
case RELO_EXTERN_CALL: case RELO_EXTERN_CALL:
bpf_gen__record_extern(obj->gen_loader, ext->name, bpf_gen__record_extern(obj->gen_loader, ext->name,
ext->is_weak, false, BTF_KIND_FUNC, ext->is_weak, false, false, BTF_KIND_FUNC,
relo->insn_idx); relo->insn_idx);
break; break;
case RELO_CORE: { case RELO_CORE: {
......
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