From: Yonghong Song <yhs@fb.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: bpf <bpf@vger.kernel.org>, Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Kernel Team <kernel-team@fb.com>
Subject: Re: [PATCH bpf-next v2 02/11] bpf: support for new btf kind BTF_KIND_TAG
Date: Tue, 14 Sep 2021 08:59:20 -0700 [thread overview]
Message-ID: <5a079457-88f9-5ed5-83bf-b0a456186323@fb.com> (raw)
In-Reply-To: <CAEf4Bza69r-Sp4nFZqd4i1xhD+Dy5u+Xb=FB7TNNSfHzNNvosg@mail.gmail.com>
On 9/13/21 10:08 PM, Andrii Nakryiko wrote:
> On Mon, Sep 13, 2021 at 8:51 AM Yonghong Song <yhs@fb.com> wrote:
>>
>> LLVM14 added support for a new C attribute ([1])
>> __attribute__((btf_tag("arbitrary_str")))
>> This attribute will be emitted to dwarf ([2]) and pahole
>> will convert it to BTF. Or for bpf target, this
>> attribute will be emitted to BTF directly ([3], [4]).
>> The attribute is intended to provide additional
>> information for
>> - struct/union type or struct/union member
>> - static/global variables
>> - static/global function or function parameter.
>>
>> For linux kernel, the btf_tag can be applied
>> in various places to specify user pointer,
>> function pre- or post- condition, function
>> allow/deny in certain context, etc. Such information
>> will be encoded in vmlinux BTF and can be used
>> by verifier.
>>
>> The btf_tag can also be applied to bpf programs
>> to help global verifiable functions, e.g.,
>> specifying preconditions, etc.
>>
>> This patch added basic parsing and checking support
>> in kernel for new BTF_KIND_TAG kind.
>>
>> [1] https://reviews.llvm.org/D106614
>> [2] https://reviews.llvm.org/D106621
>> [3] https://reviews.llvm.org/D106622
>> [4] https://reviews.llvm.org/D109560
>>
>> Signed-off-by: Yonghong Song <yhs@fb.com>
>> ---
>> include/uapi/linux/btf.h | 16 ++++-
>> kernel/bpf/btf.c | 120 +++++++++++++++++++++++++++++++++
>> tools/include/uapi/linux/btf.h | 16 ++++-
>> 3 files changed, 148 insertions(+), 4 deletions(-)
>>
>
> [...]
>
>>
>> +static s32 btf_tag_check_meta(struct btf_verifier_env *env,
>> + const struct btf_type *t,
>> + u32 meta_left)
>> +{
>> + const struct btf_tag *tag;
>> + u32 meta_needed = sizeof(*tag);
>> + const char *value;
>> +
>> + if (meta_left < meta_needed) {
>> + btf_verifier_log_basic(env, t,
>> + "meta_left:%u meta_needed:%u",
>> + meta_left, meta_needed);
>> + return -EINVAL;
>> + }
>> +
>> + value = btf_name_by_offset(env->btf, t->name_off);
>> + if (!value || !value[0]) {
>> + btf_verifier_log_type(env, t, "Invalid value");
>> + return -EINVAL;
>> + }
>> +
>> + if (btf_type_vlen(t)) {
>> + btf_verifier_log_type(env, t, "vlen != 0");
>> + return -EINVAL;
>> + }
>> +
>> + if (btf_type_kflag(t)) {
>> + btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
>> + return -EINVAL;
>> + }
>> +
>
> probably need to enforce that component_idx is >= -1? -2 is not a
> valid supported value right now.
I tested below. But I can test here for kernel practice, testing error
case earlier.
>
>> + btf_verifier_log_type(env, t, NULL);
>> +
>> + return meta_needed;
>> +}
>> +
>> +static int btf_tag_resolve(struct btf_verifier_env *env,
>> + const struct resolve_vertex *v)
>> +{
>> + const struct btf_type *next_type;
>> + const struct btf_type *t = v->t;
>> + u32 next_type_id = t->type;
>> + struct btf *btf = env->btf;
>> + s32 component_idx;
>> + u32 vlen;
>> +
>> + next_type = btf_type_by_id(btf, next_type_id);
>> + if (!next_type || !btf_type_is_tag_target(next_type)) {
>> + btf_verifier_log_type(env, v->t, "Invalid type_id");
>> + return -EINVAL;
>> + }
>> +
>> + if (!env_type_is_resolve_sink(env, next_type) &&
>> + !env_type_is_resolved(env, next_type_id))
>> + return env_stack_push(env, next_type, next_type_id);
>> +
>> + component_idx = btf_type_tag(t)->component_idx;
>> + if (component_idx != -1) {
>
> so here, if it's -2, that should be an error, but currently will be
> ignored, right?
It is not. See below. At this point, component_idx could be -2 or 0 or 1 ...
>
>> + if (btf_type_is_var(next_type) || component_idx < 0) {
>
> if is_var(next_type) then component_idx should only be -1, nothing
> else. Or am I missing some convention?
So if it is a variable, the error will return.
If it is not a variable and component_idx < 0 (-2 in this case), return
error. So we do test -2 here.
I will restructure the code to test < -1 earlier, so we won't have
confusion here.
>
>> + btf_verifier_log_type(env, v->t, "Invalid component_idx");
>> + return -EINVAL;
>> + }
>> +
>> + if (btf_type_is_struct(next_type)) {
>> + vlen = btf_type_vlen(next_type);
>> + } else {
>> + next_type = btf_type_by_id(btf, next_type->type);
>> + vlen = btf_type_vlen(next_type);
>> + }
>> +
>> + if ((u32)component_idx >= vlen) {
>> + btf_verifier_log_type(env, v->t, "Invalid component_idx");
>> + return -EINVAL;
>> + }
>> + }
>> +
>> + env_stack_pop_resolved(env, next_type_id, 0);
>> +
>> + return 0;
>> +}
>> +
>
> [...]
>
next prev parent reply other threads:[~2021-09-14 15:59 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-13 15:51 [PATCH bpf-next v2 00/11] bpf: add support for new btf kind BTF_KIND_TAG Yonghong Song
2021-09-13 15:51 ` [PATCH bpf-next v2 01/11] btf: change BTF_KIND_* macros to enums Yonghong Song
2021-09-14 4:59 ` Andrii Nakryiko
2021-09-14 15:53 ` Yonghong Song
2021-09-13 15:51 ` [PATCH bpf-next v2 02/11] bpf: support for new btf kind BTF_KIND_TAG Yonghong Song
2021-09-14 5:08 ` Andrii Nakryiko
2021-09-14 15:59 ` Yonghong Song [this message]
2021-09-14 23:30 ` Andrii Nakryiko
2021-09-13 15:51 ` [PATCH bpf-next v2 03/11] libbpf: rename btf_{hash,equal}_int to btf_{hash,equal}_int_tag Yonghong Song
2021-09-13 15:51 ` [PATCH bpf-next v2 04/11] libbpf: add support for BTF_KIND_TAG Yonghong Song
2021-09-14 5:15 ` Andrii Nakryiko
2021-09-14 16:42 ` Yonghong Song
2021-09-13 15:51 ` [PATCH bpf-next v2 05/11] bpftool: " Yonghong Song
2021-09-14 5:16 ` Andrii Nakryiko
2021-09-13 15:51 ` [PATCH bpf-next v2 06/11] selftests/bpf: test libbpf API function btf__add_tag() Yonghong Song
2021-09-14 5:18 ` Andrii Nakryiko
2021-09-13 15:52 ` [PATCH bpf-next v2 07/11] selftests/bpf: change NAME_NTH/IS_NAME_NTH for BTF_KIND_TAG format Yonghong Song
2021-09-14 5:23 ` Andrii Nakryiko
2021-09-13 15:52 ` [PATCH bpf-next v2 08/11] selftests/bpf: add BTF_KIND_TAG unit tests Yonghong Song
2021-09-14 5:31 ` Andrii Nakryiko
2021-09-14 17:00 ` Yonghong Song
2021-09-13 15:52 ` [PATCH bpf-next v2 09/11] selftests/bpf: test BTF_KIND_TAG for deduplication Yonghong Song
2021-09-14 5:38 ` Andrii Nakryiko
2021-09-14 17:15 ` Yonghong Song
2021-09-14 19:39 ` Yonghong Song
2021-09-14 23:31 ` Andrii Nakryiko
2021-09-13 15:52 ` [PATCH bpf-next v2 10/11] selftests/bpf: add a test with a bpf program with btf_tag attributes Yonghong Song
2021-09-13 15:52 ` [PATCH bpf-next v2 11/11] docs/bpf: add documentation for BTF_KIND_TAG Yonghong Song
2021-09-14 5:40 ` Andrii Nakryiko
2021-09-13 16:08 ` [PATCH bpf-next v2 00/11] bpf: add support for new btf kind BTF_KIND_TAG Yonghong Song
2021-09-13 16:40 ` Jose E. Marchesi
2021-12-16 21:52 ` Yonghong Song
2021-12-17 10:40 ` Jose E. Marchesi
2021-12-18 1:44 ` Alexei Starovoitov
2021-12-18 20:15 ` Yonghong Song
2021-12-20 9:49 ` Jose E. Marchesi
2021-12-20 15:52 ` Yonghong Song
2022-01-25 3:58 ` Yonghong Song
2022-01-27 15:38 ` Jose E. Marchesi
2022-01-27 16:42 ` Yonghong Song
2022-02-17 13:20 ` Jose E. Marchesi
2022-02-17 15:28 ` Alexei Starovoitov
2022-02-17 16:41 ` Jose E. Marchesi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5a079457-88f9-5ed5-83bf-b0a456186323@fb.com \
--to=yhs@fb.com \
--cc=andrii.nakryiko@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kernel-team@fb.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).