Commit d0e92887 authored by Alexei Starovoitov's avatar Alexei Starovoitov Committed by Andrii Nakryiko

libbpf: Use CO-RE in the kernel in light skeleton.

Without lskel the CO-RE relocations are processed by libbpf before any other
work is done. Instead, when lskel is needed, remember relocation as RELO_CORE
kind. Then when loader prog is generated for a given bpf program pass CO-RE
relos of that program to gen loader via bpf_gen__record_relo_core(). The gen
loader will remember them as-is and pass it later as-is into the kernel.

The normal libbpf flow is to process CO-RE early before call relos happen. In
case of gen_loader the core relos have to be added to other relos to be copied
together when bpf static function is appended in different places to other main
bpf progs. During the copy the append_subprog_relos() will adjust insn_idx for
normal relos and for RELO_CORE kind too. When that is done each struct
reloc_desc has good relos for specific main prog.
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Acked-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20211201181040.23337-10-alexei.starovoitov@gmail.com
parent 1e89106d
...@@ -39,6 +39,8 @@ struct bpf_gen { ...@@ -39,6 +39,8 @@ struct bpf_gen {
int error; int error;
struct ksym_relo_desc *relos; struct ksym_relo_desc *relos;
int relo_cnt; int relo_cnt;
struct bpf_core_relo *core_relos;
int core_relo_cnt;
char attach_target[128]; char attach_target[128];
int attach_kind; int attach_kind;
struct ksym_desc *ksyms; struct ksym_desc *ksyms;
...@@ -64,5 +66,6 @@ void bpf_gen__map_freeze(struct bpf_gen *gen, int map_idx); ...@@ -64,5 +66,6 @@ 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, int kind, int insn_idx);
void bpf_gen__record_relo_core(struct bpf_gen *gen, const struct bpf_core_relo *core_relo);
#endif #endif
...@@ -829,6 +829,22 @@ static void emit_relo_ksym_btf(struct bpf_gen *gen, struct ksym_relo_desc *relo, ...@@ -829,6 +829,22 @@ static void emit_relo_ksym_btf(struct bpf_gen *gen, struct ksym_relo_desc *relo,
emit_ksym_relo_log(gen, relo, kdesc->ref); emit_ksym_relo_log(gen, relo, kdesc->ref);
} }
void bpf_gen__record_relo_core(struct bpf_gen *gen,
const struct bpf_core_relo *core_relo)
{
struct bpf_core_relo *relos;
relos = libbpf_reallocarray(gen->core_relos, gen->core_relo_cnt + 1, sizeof(*relos));
if (!relos) {
gen->error = -ENOMEM;
return;
}
gen->core_relos = relos;
relos += gen->core_relo_cnt;
memcpy(relos, core_relo, sizeof(*relos));
gen->core_relo_cnt++;
}
static void emit_relo(struct bpf_gen *gen, struct ksym_relo_desc *relo, int insns) static void emit_relo(struct bpf_gen *gen, struct ksym_relo_desc *relo, int insns)
{ {
int insn; int insn;
...@@ -861,6 +877,15 @@ static void emit_relos(struct bpf_gen *gen, int insns) ...@@ -861,6 +877,15 @@ static void emit_relos(struct bpf_gen *gen, int insns)
emit_relo(gen, gen->relos + i, insns); emit_relo(gen, gen->relos + i, insns);
} }
static void cleanup_core_relo(struct bpf_gen *gen)
{
if (!gen->core_relo_cnt)
return;
free(gen->core_relos);
gen->core_relo_cnt = 0;
gen->core_relos = NULL;
}
static void cleanup_relos(struct bpf_gen *gen, int insns) static void cleanup_relos(struct bpf_gen *gen, int insns)
{ {
int i, insn; int i, insn;
...@@ -888,6 +913,7 @@ static void cleanup_relos(struct bpf_gen *gen, int insns) ...@@ -888,6 +913,7 @@ static void cleanup_relos(struct bpf_gen *gen, int insns)
gen->relo_cnt = 0; gen->relo_cnt = 0;
gen->relos = NULL; gen->relos = NULL;
} }
cleanup_core_relo(gen);
} }
void bpf_gen__prog_load(struct bpf_gen *gen, void bpf_gen__prog_load(struct bpf_gen *gen,
...@@ -895,12 +921,13 @@ void bpf_gen__prog_load(struct bpf_gen *gen, ...@@ -895,12 +921,13 @@ void bpf_gen__prog_load(struct bpf_gen *gen,
const char *license, struct bpf_insn *insns, size_t insn_cnt, const char *license, struct bpf_insn *insns, size_t insn_cnt,
struct bpf_prog_load_opts *load_attr, int prog_idx) struct bpf_prog_load_opts *load_attr, int prog_idx)
{ {
int attr_size = offsetofend(union bpf_attr, fd_array); int prog_load_attr, license_off, insns_off, func_info, line_info, core_relos;
int prog_load_attr, license_off, insns_off, func_info, line_info; int attr_size = offsetofend(union bpf_attr, core_relo_rec_size);
union bpf_attr attr; union bpf_attr attr;
memset(&attr, 0, attr_size); memset(&attr, 0, attr_size);
pr_debug("gen: prog_load: type %d insns_cnt %zd\n", prog_type, insn_cnt); pr_debug("gen: prog_load: type %d insns_cnt %zd progi_idx %d\n",
prog_type, insn_cnt, prog_idx);
/* add license string to blob of bytes */ /* add license string to blob of bytes */
license_off = add_data(gen, license, strlen(license) + 1); license_off = add_data(gen, license, strlen(license) + 1);
/* add insns to blob of bytes */ /* add insns to blob of bytes */
...@@ -924,6 +951,11 @@ void bpf_gen__prog_load(struct bpf_gen *gen, ...@@ -924,6 +951,11 @@ void bpf_gen__prog_load(struct bpf_gen *gen,
line_info = add_data(gen, load_attr->line_info, line_info = add_data(gen, load_attr->line_info,
attr.line_info_cnt * attr.line_info_rec_size); attr.line_info_cnt * attr.line_info_rec_size);
attr.core_relo_rec_size = sizeof(struct bpf_core_relo);
attr.core_relo_cnt = gen->core_relo_cnt;
core_relos = add_data(gen, gen->core_relos,
attr.core_relo_cnt * attr.core_relo_rec_size);
memcpy(attr.prog_name, prog_name, memcpy(attr.prog_name, prog_name,
min((unsigned)strlen(prog_name), BPF_OBJ_NAME_LEN - 1)); min((unsigned)strlen(prog_name), BPF_OBJ_NAME_LEN - 1));
prog_load_attr = add_data(gen, &attr, attr_size); prog_load_attr = add_data(gen, &attr, attr_size);
...@@ -940,6 +972,9 @@ void bpf_gen__prog_load(struct bpf_gen *gen, ...@@ -940,6 +972,9 @@ void bpf_gen__prog_load(struct bpf_gen *gen,
/* populate union bpf_attr with a pointer to line_info */ /* populate union bpf_attr with a pointer to line_info */
emit_rel_store(gen, attr_field(prog_load_attr, line_info), line_info); emit_rel_store(gen, attr_field(prog_load_attr, line_info), line_info);
/* populate union bpf_attr with a pointer to core_relos */
emit_rel_store(gen, attr_field(prog_load_attr, core_relos), core_relos);
/* populate union bpf_attr fd_array with a pointer to data where map_fds are saved */ /* populate union bpf_attr fd_array with a pointer to data where map_fds are saved */
emit_rel_store(gen, attr_field(prog_load_attr, fd_array), gen->fd_array); emit_rel_store(gen, attr_field(prog_load_attr, fd_array), gen->fd_array);
......
...@@ -230,13 +230,19 @@ enum reloc_type { ...@@ -230,13 +230,19 @@ enum reloc_type {
RELO_EXTERN_VAR, RELO_EXTERN_VAR,
RELO_EXTERN_FUNC, RELO_EXTERN_FUNC,
RELO_SUBPROG_ADDR, RELO_SUBPROG_ADDR,
RELO_CORE,
}; };
struct reloc_desc { struct reloc_desc {
enum reloc_type type; enum reloc_type type;
int insn_idx; int insn_idx;
int map_idx; union {
int sym_off; const struct bpf_core_relo *core_relo; /* used when type == RELO_CORE */
struct {
int map_idx;
int sym_off;
};
};
}; };
struct bpf_sec_def; struct bpf_sec_def;
...@@ -5485,6 +5491,24 @@ static void *u32_as_hash_key(__u32 x) ...@@ -5485,6 +5491,24 @@ static void *u32_as_hash_key(__u32 x)
return (void *)(uintptr_t)x; return (void *)(uintptr_t)x;
} }
static int record_relo_core(struct bpf_program *prog,
const struct bpf_core_relo *core_relo, int insn_idx)
{
struct reloc_desc *relos, *relo;
relos = libbpf_reallocarray(prog->reloc_desc,
prog->nr_reloc + 1, sizeof(*relos));
if (!relos)
return -ENOMEM;
relo = &relos[prog->nr_reloc];
relo->type = RELO_CORE;
relo->insn_idx = insn_idx;
relo->core_relo = core_relo;
prog->reloc_desc = relos;
prog->nr_reloc++;
return 0;
}
static int bpf_core_apply_relo(struct bpf_program *prog, static int bpf_core_apply_relo(struct bpf_program *prog,
const struct bpf_core_relo *relo, const struct bpf_core_relo *relo,
int relo_idx, int relo_idx,
...@@ -5521,10 +5545,12 @@ static int bpf_core_apply_relo(struct bpf_program *prog, ...@@ -5521,10 +5545,12 @@ static int bpf_core_apply_relo(struct bpf_program *prog,
return -EINVAL; return -EINVAL;
if (prog->obj->gen_loader) { if (prog->obj->gen_loader) {
pr_warn("// TODO core_relo: prog %td insn[%d] %s kind %d\n", const char *spec_str = btf__name_by_offset(local_btf, relo->access_str_off);
pr_debug("record_relo_core: prog %td insn[%d] %s %s %s final insn_idx %d\n",
prog - prog->obj->programs, relo->insn_off / 8, prog - prog->obj->programs, relo->insn_off / 8,
local_name, relo->kind); btf_kind_str(local_type), local_name, spec_str, insn_idx);
return -ENOTSUP; return record_relo_core(prog, relo, insn_idx);
} }
if (relo->kind != BPF_CORE_TYPE_ID_LOCAL && if (relo->kind != BPF_CORE_TYPE_ID_LOCAL &&
...@@ -5729,6 +5755,9 @@ bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog) ...@@ -5729,6 +5755,9 @@ bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog)
case RELO_CALL: case RELO_CALL:
/* handled already */ /* handled already */
break; break;
case RELO_CORE:
/* will be handled by bpf_program_record_relos() */
break;
default: default:
pr_warn("prog '%s': relo #%d: bad relo type %d\n", pr_warn("prog '%s': relo #%d: bad relo type %d\n",
prog->name, i, relo->type); prog->name, i, relo->type);
...@@ -6169,6 +6198,35 @@ bpf_object__free_relocs(struct bpf_object *obj) ...@@ -6169,6 +6198,35 @@ bpf_object__free_relocs(struct bpf_object *obj)
} }
} }
static int cmp_relocs(const void *_a, const void *_b)
{
const struct reloc_desc *a = _a;
const struct reloc_desc *b = _b;
if (a->insn_idx != b->insn_idx)
return a->insn_idx < b->insn_idx ? -1 : 1;
/* no two relocations should have the same insn_idx, but ... */
if (a->type != b->type)
return a->type < b->type ? -1 : 1;
return 0;
}
static void bpf_object__sort_relos(struct bpf_object *obj)
{
int i;
for (i = 0; i < obj->nr_programs; i++) {
struct bpf_program *p = &obj->programs[i];
if (!p->nr_reloc)
continue;
qsort(p->reloc_desc, p->nr_reloc, sizeof(*p->reloc_desc), cmp_relocs);
}
}
static int static int
bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path) bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path)
{ {
...@@ -6183,6 +6241,8 @@ bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path) ...@@ -6183,6 +6241,8 @@ bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path)
err); err);
return err; return err;
} }
if (obj->gen_loader)
bpf_object__sort_relos(obj);
} }
/* Before relocating calls pre-process relocations and mark /* Before relocating calls pre-process relocations and mark
...@@ -6387,21 +6447,6 @@ static int bpf_object__collect_map_relos(struct bpf_object *obj, ...@@ -6387,21 +6447,6 @@ static int bpf_object__collect_map_relos(struct bpf_object *obj,
return 0; return 0;
} }
static int cmp_relocs(const void *_a, const void *_b)
{
const struct reloc_desc *a = _a;
const struct reloc_desc *b = _b;
if (a->insn_idx != b->insn_idx)
return a->insn_idx < b->insn_idx ? -1 : 1;
/* no two relocations should have the same insn_idx, but ... */
if (a->type != b->type)
return a->type < b->type ? -1 : 1;
return 0;
}
static int bpf_object__collect_relos(struct bpf_object *obj) static int bpf_object__collect_relos(struct bpf_object *obj)
{ {
int i, err; int i, err;
...@@ -6434,14 +6479,7 @@ static int bpf_object__collect_relos(struct bpf_object *obj) ...@@ -6434,14 +6479,7 @@ static int bpf_object__collect_relos(struct bpf_object *obj)
return err; return err;
} }
for (i = 0; i < obj->nr_programs; i++) { bpf_object__sort_relos(obj);
struct bpf_program *p = &obj->programs[i];
if (!p->nr_reloc)
continue;
qsort(p->reloc_desc, p->nr_reloc, sizeof(*p->reloc_desc), cmp_relocs);
}
return 0; return 0;
} }
...@@ -6683,7 +6721,7 @@ static int bpf_object_load_prog_instance(struct bpf_object *obj, struct bpf_prog ...@@ -6683,7 +6721,7 @@ static int bpf_object_load_prog_instance(struct bpf_object *obj, struct bpf_prog
return ret; return ret;
} }
static int bpf_program__record_externs(struct bpf_program *prog) static int bpf_program_record_relos(struct bpf_program *prog)
{ {
struct bpf_object *obj = prog->obj; struct bpf_object *obj = prog->obj;
int i; int i;
...@@ -6705,6 +6743,17 @@ static int bpf_program__record_externs(struct bpf_program *prog) ...@@ -6705,6 +6743,17 @@ static int bpf_program__record_externs(struct bpf_program *prog)
ext->is_weak, false, BTF_KIND_FUNC, ext->is_weak, false, BTF_KIND_FUNC,
relo->insn_idx); relo->insn_idx);
break; break;
case RELO_CORE: {
struct bpf_core_relo cr = {
.insn_off = relo->insn_idx * 8,
.type_id = relo->core_relo->type_id,
.access_str_off = relo->core_relo->access_str_off,
.kind = relo->core_relo->kind,
};
bpf_gen__record_relo_core(obj->gen_loader, &cr);
break;
}
default: default:
continue; continue;
} }
...@@ -6744,7 +6793,7 @@ static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog ...@@ -6744,7 +6793,7 @@ static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog
prog->name, prog->instances.nr); prog->name, prog->instances.nr);
} }
if (obj->gen_loader) if (obj->gen_loader)
bpf_program__record_externs(prog); bpf_program_record_relos(prog);
err = bpf_object_load_prog_instance(obj, prog, err = bpf_object_load_prog_instance(obj, prog,
prog->insns, prog->insns_cnt, prog->insns, prog->insns_cnt,
license, kern_ver, &fd); license, kern_ver, &fd);
......
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