Skip to content

Commit 23127b3

Browse files
iamkafaiAlexei Starovoitov
authored andcommitted
bpf: Create a new btf_name_by_offset() for non type name use case
The current btf_name_by_offset() is returning "(anon)" type name for the offset == 0 case and "(invalid-name-offset)" for the out-of-bound offset case. It fits well for the internal BTF verbose log purpose which is focusing on type. For example, offset == 0 => "(anon)" => anonymous type/name. Returning non-NULL for the bad offset case is needed during the BTF verification process because the BTF verifier may complain about another field first before discovering the name_off is invalid. However, it may not be ideal for the newer use case which does not necessary mean type name. For example, when logging line_info in the BPF verifier in the next patch, it is better to log an empty src line instead of logging "(anon)". The existing bpf_name_by_offset() is renamed to __bpf_name_by_offset() and static to btf.c. A new bpf_name_by_offset() is added for generic context usage. It returns "\0" for name_off == 0 (note that btf->strings[0] is "\0") and NULL for invalid offset. It allows the caller to decide what is the best output in its context. The new btf_name_by_offset() is overlapped with btf_name_offset_valid(). Hence, btf_name_offset_valid() is removed from btf.h to keep the btf.h API minimal. The existing btf_name_offset_valid() usage in btf.c could also be replaced later. Signed-off-by: Martin KaFai Lau <kafai@fb.com> Acked-by: Yonghong Song <yhs@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 28c1272 commit 23127b3

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

include/linux/btf.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
4646
struct seq_file *m);
4747
int btf_get_fd_by_id(u32 id);
4848
u32 btf_id(const struct btf *btf);
49-
bool btf_name_offset_valid(const struct btf *btf, u32 offset);
5049
bool btf_type_is_reg_int(const struct btf_type *t, u32 expected_size);
5150

5251
#ifdef CONFIG_BPF_SYSCALL

kernel/bpf/btf.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ static bool btf_name_valid_identifier(const struct btf *btf, u32 offset)
474474
return !*src;
475475
}
476476

477-
const char *btf_name_by_offset(const struct btf *btf, u32 offset)
477+
static const char *__btf_name_by_offset(const struct btf *btf, u32 offset)
478478
{
479479
if (!offset)
480480
return "(anon)";
@@ -484,6 +484,14 @@ const char *btf_name_by_offset(const struct btf *btf, u32 offset)
484484
return "(invalid-name-offset)";
485485
}
486486

487+
const char *btf_name_by_offset(const struct btf *btf, u32 offset)
488+
{
489+
if (offset < btf->hdr.str_len)
490+
return &btf->strings[offset];
491+
492+
return NULL;
493+
}
494+
487495
const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id)
488496
{
489497
if (type_id > btf->nr_types)
@@ -576,7 +584,7 @@ __printf(4, 5) static void __btf_verifier_log_type(struct btf_verifier_env *env,
576584
__btf_verifier_log(log, "[%u] %s %s%s",
577585
env->log_type_id,
578586
btf_kind_str[kind],
579-
btf_name_by_offset(btf, t->name_off),
587+
__btf_name_by_offset(btf, t->name_off),
580588
log_details ? " " : "");
581589

582590
if (log_details)
@@ -620,7 +628,7 @@ static void btf_verifier_log_member(struct btf_verifier_env *env,
620628
btf_verifier_log_type(env, struct_type, NULL);
621629

622630
__btf_verifier_log(log, "\t%s type_id=%u bits_offset=%u",
623-
btf_name_by_offset(btf, member->name_off),
631+
__btf_name_by_offset(btf, member->name_off),
624632
member->type, member->offset);
625633

626634
if (fmt && *fmt) {
@@ -1872,7 +1880,7 @@ static s32 btf_enum_check_meta(struct btf_verifier_env *env,
18721880

18731881

18741882
btf_verifier_log(env, "\t%s val=%d\n",
1875-
btf_name_by_offset(btf, enums[i].name_off),
1883+
__btf_name_by_offset(btf, enums[i].name_off),
18761884
enums[i].val);
18771885
}
18781886

@@ -1896,7 +1904,8 @@ static void btf_enum_seq_show(const struct btf *btf, const struct btf_type *t,
18961904
for (i = 0; i < nr_enums; i++) {
18971905
if (v == enums[i].val) {
18981906
seq_printf(m, "%s",
1899-
btf_name_by_offset(btf, enums[i].name_off));
1907+
__btf_name_by_offset(btf,
1908+
enums[i].name_off));
19001909
return;
19011910
}
19021911
}
@@ -1954,20 +1963,20 @@ static void btf_func_proto_log(struct btf_verifier_env *env,
19541963
}
19551964

19561965
btf_verifier_log(env, "%u %s", args[0].type,
1957-
btf_name_by_offset(env->btf,
1958-
args[0].name_off));
1966+
__btf_name_by_offset(env->btf,
1967+
args[0].name_off));
19591968
for (i = 1; i < nr_args - 1; i++)
19601969
btf_verifier_log(env, ", %u %s", args[i].type,
1961-
btf_name_by_offset(env->btf,
1962-
args[i].name_off));
1970+
__btf_name_by_offset(env->btf,
1971+
args[i].name_off));
19631972

19641973
if (nr_args > 1) {
19651974
const struct btf_param *last_arg = &args[nr_args - 1];
19661975

19671976
if (last_arg->type)
19681977
btf_verifier_log(env, ", %u %s", last_arg->type,
1969-
btf_name_by_offset(env->btf,
1970-
last_arg->name_off));
1978+
__btf_name_by_offset(env->btf,
1979+
last_arg->name_off));
19711980
else
19721981
btf_verifier_log(env, ", vararg");
19731982
}

kernel/bpf/verifier.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4910,8 +4910,8 @@ static int check_btf_line(struct bpf_verifier_env *env,
49104910
goto err_free;
49114911
}
49124912

4913-
if (!btf_name_offset_valid(btf, linfo[i].line_off) ||
4914-
!btf_name_offset_valid(btf, linfo[i].file_name_off)) {
4913+
if (!btf_name_by_offset(btf, linfo[i].line_off) ||
4914+
!btf_name_by_offset(btf, linfo[i].file_name_off)) {
49154915
verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i);
49164916
err = -EINVAL;
49174917
goto err_free;

0 commit comments

Comments
 (0)