Skip to content

Commit d9d93f3

Browse files
committed
Merge branch 'bpf: Support ksym detection in light skeleton.'
Alexei Starovoitov says: ==================== From: Alexei Starovoitov <[email protected]> v1->v2: update denylist on s390 Patch 1: Cleanup internal libbpf names. Patch 2: Teach the verifier that rdonly_mem != NULL. Patch 3: Fix gen_loader to support ksym detection. Patch 4: Selftest and update denylist. ==================== Signed-off-by: Andrii Nakryiko <[email protected]>
2 parents 9a321fd + 3b2ec21 commit d9d93f3

File tree

6 files changed

+61
-36
lines changed

6 files changed

+61
-36
lines changed

kernel/bpf/verifier.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,17 @@ static bool type_is_sk_pointer(enum bpf_reg_type type)
486486
type == PTR_TO_XDP_SOCK;
487487
}
488488

489+
static bool type_may_be_null(u32 type)
490+
{
491+
return type & PTR_MAYBE_NULL;
492+
}
493+
489494
static bool reg_type_not_null(enum bpf_reg_type type)
490495
{
496+
if (type_may_be_null(type))
497+
return false;
498+
499+
type = base_type(type);
491500
return type == PTR_TO_SOCKET ||
492501
type == PTR_TO_TCP_SOCK ||
493502
type == PTR_TO_MAP_VALUE ||
@@ -531,11 +540,6 @@ static bool type_is_rdonly_mem(u32 type)
531540
return type & MEM_RDONLY;
532541
}
533542

534-
static bool type_may_be_null(u32 type)
535-
{
536-
return type & PTR_MAYBE_NULL;
537-
}
538-
539543
static bool is_acquire_function(enum bpf_func_id func_id,
540544
const struct bpf_map *map)
541545
{

tools/lib/bpf/bpf_gen_internal.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ struct ksym_relo_desc {
1111
int insn_idx;
1212
bool is_weak;
1313
bool is_typeless;
14+
bool is_ld64;
1415
};
1516

1617
struct ksym_desc {
@@ -24,6 +25,7 @@ struct ksym_desc {
2425
bool typeless;
2526
};
2627
int insn;
28+
bool is_ld64;
2729
};
2830

2931
struct bpf_gen {
@@ -65,7 +67,7 @@ void bpf_gen__map_update_elem(struct bpf_gen *gen, int map_idx, void *value, __u
6567
void bpf_gen__map_freeze(struct bpf_gen *gen, int map_idx);
6668
void bpf_gen__record_attach_target(struct bpf_gen *gen, const char *name, enum bpf_attach_type type);
6769
void bpf_gen__record_extern(struct bpf_gen *gen, const char *name, bool is_weak,
68-
bool is_typeless, int kind, int insn_idx);
70+
bool is_typeless, bool is_ld64, int kind, int insn_idx);
6971
void bpf_gen__record_relo_core(struct bpf_gen *gen, const struct bpf_core_relo *core_relo);
7072
void bpf_gen__populate_outer_map(struct bpf_gen *gen, int outer_map_idx, int key, int inner_map_idx);
7173

tools/lib/bpf/gen_loader.c

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ static void emit_find_attach_target(struct bpf_gen *gen)
560560
}
561561

562562
void bpf_gen__record_extern(struct bpf_gen *gen, const char *name, bool is_weak,
563-
bool is_typeless, int kind, int insn_idx)
563+
bool is_typeless, bool is_ld64, int kind, int insn_idx)
564564
{
565565
struct ksym_relo_desc *relo;
566566

@@ -574,6 +574,7 @@ void bpf_gen__record_extern(struct bpf_gen *gen, const char *name, bool is_weak,
574574
relo->name = name;
575575
relo->is_weak = is_weak;
576576
relo->is_typeless = is_typeless;
577+
relo->is_ld64 = is_ld64;
577578
relo->kind = kind;
578579
relo->insn_idx = insn_idx;
579580
gen->relo_cnt++;
@@ -586,9 +587,11 @@ static struct ksym_desc *get_ksym_desc(struct bpf_gen *gen, struct ksym_relo_des
586587
int i;
587588

588589
for (i = 0; i < gen->nr_ksyms; i++) {
589-
if (!strcmp(gen->ksyms[i].name, relo->name)) {
590-
gen->ksyms[i].ref++;
591-
return &gen->ksyms[i];
590+
kdesc = &gen->ksyms[i];
591+
if (kdesc->kind == relo->kind && kdesc->is_ld64 == relo->is_ld64 &&
592+
!strcmp(kdesc->name, relo->name)) {
593+
kdesc->ref++;
594+
return kdesc;
592595
}
593596
}
594597
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
603606
kdesc->ref = 1;
604607
kdesc->off = 0;
605608
kdesc->insn = 0;
609+
kdesc->is_ld64 = relo->is_ld64;
606610
return kdesc;
607611
}
608612

@@ -864,23 +868,17 @@ static void emit_relo(struct bpf_gen *gen, struct ksym_relo_desc *relo, int insn
864868
{
865869
int insn;
866870

867-
pr_debug("gen: emit_relo (%d): %s at %d\n", relo->kind, relo->name, relo->insn_idx);
871+
pr_debug("gen: emit_relo (%d): %s at %d %s\n",
872+
relo->kind, relo->name, relo->insn_idx, relo->is_ld64 ? "ld64" : "call");
868873
insn = insns + sizeof(struct bpf_insn) * relo->insn_idx;
869874
emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_8, BPF_PSEUDO_MAP_IDX_VALUE, 0, 0, 0, insn));
870-
switch (relo->kind) {
871-
case BTF_KIND_VAR:
875+
if (relo->is_ld64) {
872876
if (relo->is_typeless)
873877
emit_relo_ksym_typeless(gen, relo, insn);
874878
else
875879
emit_relo_ksym_btf(gen, relo, insn);
876-
break;
877-
case BTF_KIND_FUNC:
880+
} else {
878881
emit_relo_kfunc_btf(gen, relo, insn);
879-
break;
880-
default:
881-
pr_warn("Unknown relocation kind '%d'\n", relo->kind);
882-
gen->error = -EDOM;
883-
return;
884882
}
885883
}
886884

@@ -903,18 +901,20 @@ static void cleanup_core_relo(struct bpf_gen *gen)
903901

904902
static void cleanup_relos(struct bpf_gen *gen, int insns)
905903
{
904+
struct ksym_desc *kdesc;
906905
int i, insn;
907906

908907
for (i = 0; i < gen->nr_ksyms; i++) {
908+
kdesc = &gen->ksyms[i];
909909
/* only close fds for typed ksyms and kfuncs */
910-
if (gen->ksyms[i].kind == BTF_KIND_VAR && !gen->ksyms[i].typeless) {
910+
if (kdesc->is_ld64 && !kdesc->typeless) {
911911
/* close fd recorded in insn[insn_idx + 1].imm */
912-
insn = gen->ksyms[i].insn;
912+
insn = kdesc->insn;
913913
insn += sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm);
914914
emit_sys_close_blob(gen, insn);
915-
} else if (gen->ksyms[i].kind == BTF_KIND_FUNC) {
916-
emit_sys_close_blob(gen, blob_fd_array_off(gen, gen->ksyms[i].off));
917-
if (gen->ksyms[i].off < MAX_FD_ARRAY_SZ)
915+
} else if (!kdesc->is_ld64) {
916+
emit_sys_close_blob(gen, blob_fd_array_off(gen, kdesc->off));
917+
if (kdesc->off < MAX_FD_ARRAY_SZ)
918918
gen->nr_fd_array--;
919919
}
920920
}

tools/lib/bpf/libbpf.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,8 @@ enum reloc_type {
315315
RELO_LD64,
316316
RELO_CALL,
317317
RELO_DATA,
318-
RELO_EXTERN_VAR,
319-
RELO_EXTERN_FUNC,
318+
RELO_EXTERN_LD64,
319+
RELO_EXTERN_CALL,
320320
RELO_SUBPROG_ADDR,
321321
RELO_CORE,
322322
};
@@ -4009,9 +4009,9 @@ static int bpf_program__record_reloc(struct bpf_program *prog,
40094009
pr_debug("prog '%s': found extern #%d '%s' (sym %d) for insn #%u\n",
40104010
prog->name, i, ext->name, ext->sym_idx, insn_idx);
40114011
if (insn->code == (BPF_JMP | BPF_CALL))
4012-
reloc_desc->type = RELO_EXTERN_FUNC;
4012+
reloc_desc->type = RELO_EXTERN_CALL;
40134013
else
4014-
reloc_desc->type = RELO_EXTERN_VAR;
4014+
reloc_desc->type = RELO_EXTERN_LD64;
40154015
reloc_desc->insn_idx = insn_idx;
40164016
reloc_desc->sym_off = i; /* sym_off stores extern index */
40174017
return 0;
@@ -5855,7 +5855,7 @@ bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog)
58555855
relo->map_idx, map);
58565856
}
58575857
break;
5858-
case RELO_EXTERN_VAR:
5858+
case RELO_EXTERN_LD64:
58595859
ext = &obj->externs[relo->sym_off];
58605860
if (ext->type == EXT_KCFG) {
58615861
if (obj->gen_loader) {
@@ -5877,7 +5877,7 @@ bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog)
58775877
}
58785878
}
58795879
break;
5880-
case RELO_EXTERN_FUNC:
5880+
case RELO_EXTERN_CALL:
58815881
ext = &obj->externs[relo->sym_off];
58825882
insn[0].src_reg = BPF_PSEUDO_KFUNC_CALL;
58835883
if (ext->is_set) {
@@ -6115,7 +6115,7 @@ bpf_object__reloc_code(struct bpf_object *obj, struct bpf_program *main_prog,
61156115
continue;
61166116

61176117
relo = find_prog_insn_relo(prog, insn_idx);
6118-
if (relo && relo->type == RELO_EXTERN_FUNC)
6118+
if (relo && relo->type == RELO_EXTERN_CALL)
61196119
/* kfunc relocations will be handled later
61206120
* in bpf_object__relocate_data()
61216121
*/
@@ -7070,18 +7070,21 @@ static int bpf_program_record_relos(struct bpf_program *prog)
70707070
for (i = 0; i < prog->nr_reloc; i++) {
70717071
struct reloc_desc *relo = &prog->reloc_desc[i];
70727072
struct extern_desc *ext = &obj->externs[relo->sym_off];
7073+
int kind;
70737074

70747075
switch (relo->type) {
7075-
case RELO_EXTERN_VAR:
7076+
case RELO_EXTERN_LD64:
70767077
if (ext->type != EXT_KSYM)
70777078
continue;
7079+
kind = btf_is_var(btf__type_by_id(obj->btf, ext->btf_id)) ?
7080+
BTF_KIND_VAR : BTF_KIND_FUNC;
70787081
bpf_gen__record_extern(obj->gen_loader, ext->name,
70797082
ext->is_weak, !ext->ksym.type_id,
7080-
BTF_KIND_VAR, relo->insn_idx);
7083+
true, kind, relo->insn_idx);
70817084
break;
7082-
case RELO_EXTERN_FUNC:
7085+
case RELO_EXTERN_CALL:
70837086
bpf_gen__record_extern(obj->gen_loader, ext->name,
7084-
ext->is_weak, false, BTF_KIND_FUNC,
7087+
ext->is_weak, false, false, BTF_KIND_FUNC,
70857088
relo->insn_idx);
70867089
break;
70877090
case RELO_CORE: {

tools/testing/selftests/bpf/DENYLIST.s390x

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ get_stack_raw_tp # user_stack corrupted user stack
1111
iters/testmod_seq* # s390x doesn't support kfuncs in modules yet
1212
kprobe_multi_bench_attach # bpf_program__attach_kprobe_multi_opts unexpected error: -95
1313
kprobe_multi_test # relies on fentry
14+
ksyms_btf/weak_ksyms* # test_ksyms_weak__open_and_load unexpected error: -22 (kfunc)
1415
ksyms_module # test_ksyms_module__open_and_load unexpected error: -9 (?)
1516
ksyms_module_libbpf # JIT does not support calling kernel function (kfunc)
1617
ksyms_module_lskel # test_ksyms_module_lskel__open_and_load unexpected error: -9 (?)

tools/testing/selftests/bpf/progs/test_ksyms_weak.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ __u64 out__non_existent_typed = -1;
2020
/* test existing weak symbols can be resolved. */
2121
extern const struct rq runqueues __ksym __weak; /* typed */
2222
extern const void bpf_prog_active __ksym __weak; /* typeless */
23+
struct task_struct *bpf_task_acquire(struct task_struct *p) __ksym __weak;
24+
void bpf_testmod_test_mod_kfunc(int i) __ksym __weak;
2325

2426

2527
/* non-existent weak symbols. */
@@ -29,6 +31,7 @@ extern const void bpf_link_fops1 __ksym __weak;
2931

3032
/* typed symbols, default to zero. */
3133
extern const int bpf_link_fops2 __ksym __weak;
34+
void invalid_kfunc(void) __ksym __weak;
3235

3336
SEC("raw_tp/sys_enter")
3437
int pass_handler(const void *ctx)
@@ -50,6 +53,18 @@ int pass_handler(const void *ctx)
5053
if (&bpf_link_fops2) /* can't happen */
5154
out__non_existent_typed = (__u64)bpf_per_cpu_ptr(&bpf_link_fops2, 0);
5255

56+
if (!bpf_ksym_exists(bpf_task_acquire))
57+
/* dead code won't be seen by the verifier */
58+
bpf_task_acquire(0);
59+
60+
if (!bpf_ksym_exists(bpf_testmod_test_mod_kfunc))
61+
/* dead code won't be seen by the verifier */
62+
bpf_testmod_test_mod_kfunc(0);
63+
64+
if (bpf_ksym_exists(invalid_kfunc))
65+
/* dead code won't be seen by the verifier */
66+
invalid_kfunc();
67+
5368
return 0;
5469
}
5570

0 commit comments

Comments
 (0)