From: Eduard Zingerman <eddyz87@gmail.com>
To: Kui-Feng Lee <thinker.li@gmail.com>,
bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev,
song@kernel.org, kernel-team@meta.com, andrii@kernel.org
Cc: sinquersw@gmail.com, kuifeng@meta.com
Subject: Re: [PATCH bpf-next v3 4/7] bpf: look into the types of the fields of a struct type recursively.
Date: Thu, 02 May 2024 12:34:31 -0700 [thread overview]
Message-ID: <ef932548808bd55dae8bccbbab63de60b86985ee.camel@gmail.com> (raw)
In-Reply-To: <20240501204729.484085-5-thinker.li@gmail.com>
On Wed, 2024-05-01 at 13:47 -0700, Kui-Feng Lee wrote:
> The verifier has field information for specific special types, such as
> kptr, rbtree root, and list head. These types are handled
> differently. However, we did not previously examine the types of fields of
> a struct type variable. Field information records were not generated for
> the kptrs, rbtree roots, and linked_list heads that are not located at the
> outermost struct type of a variable.
>
> For example,
>
> struct A {
> struct task_struct __kptr * task;
> };
>
> struct B {
> struct A mem_a;
> }
>
> struct B var_b;
>
> It did not examine "struct A" so as not to generate field information for
> the kptr in "struct A" for "var_b".
>
> This patch enables BPF programs to define fields of these special types in
> a struct type other than the direct type of a variable or in a struct type
> that is the type of a field in the value type of a map.
>
> Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
> ---
I think that the main logic of this commit is fine.
A few nitpicks about code organization below.
> kernel/bpf/btf.c | 118 +++++++++++++++++++++++++++++++++++++++--------
> 1 file changed, 98 insertions(+), 20 deletions(-)
>
> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> index 4a78cd28fab0..2ceff77b7e71 100644
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c
> @@ -3493,41 +3493,83 @@ static int btf_get_field_type(const char *name, u32 field_mask, u32 *seen_mask,
[...]
> +static int btf_find_struct_field(const struct btf *btf,
> + const struct btf_type *t, u32 field_mask,
> + struct btf_field_info *info, int info_cnt);
> +
> +/* Find special fields in the struct type of a field.
> + *
> + * This function is used to find fields of special types that is not a
> + * global variable or a direct field of a struct type. It also handles the
> + * repetition if it is the element type of an array.
> + */
> +static int btf_find_nested_struct(const struct btf *btf, const struct btf_type *t,
> + u32 off, u32 nelems,
> + u32 field_mask, struct btf_field_info *info,
> + int info_cnt)
> +{
> + int ret, err, i;
> +
> + ret = btf_find_struct_field(btf, t, field_mask, info, info_cnt);
btf_find_nested_struct() and btf_find_struct_field() are mutually recursive,
as far as I can see this is usually avoided in kernel source.
Would it be possible to make stack explicit or limit traversal depth here?
The 'info_cnt' field won't work as it could be unchanged in
btf_find_struct_field() if 'idx == 0'.
> +
> + if (ret <= 0)
> + return ret;
> +
> + /* Shift the offsets of the nested struct fields to the offsets
> + * related to the container.
> + */
> + for (i = 0; i < ret; i++)
> + info[i].off += off;
> +
> + if (nelems > 1) {
> + err = btf_repeat_fields(info, 0, ret, nelems - 1, t->size);
> + if (err == 0)
> + ret *= nelems;
> + else
> + ret = err;
> + }
> +
> + return ret;
> +}
> +
> static int btf_find_struct_field(const struct btf *btf,
> const struct btf_type *t, u32 field_mask,
> struct btf_field_info *info, int info_cnt)
[...]
> @@ -3644,6 +3707,21 @@ static int btf_find_datasec_var(const struct btf *btf, const struct btf_type *t,
>
> field_type = btf_get_field_type(__btf_name_by_offset(btf, var_type->name_off),
> field_mask, &seen_mask, &align, &sz);
Actions taken for members in btf_find_datasec_var() and
btf_find_struct_field() are almost identical, would it be possible to
add a refactoring commit this patch-set so that common logic is moved
to a separate function? It looks like this function would have to be
parameterized only by member size and offset.
> + /* Look into variables of struct types */
> + if ((field_type == BPF_KPTR_REF || !field_type) &&
> + __btf_type_is_struct(var_type)) {
> + sz = var_type->size;
> + if (vsi->size != sz * nelems)
> + continue;
> + off = vsi->offset;
> + ret = btf_find_nested_struct(btf, var_type, off, nelems, field_mask,
> + &info[idx], info_cnt - idx);
> + if (ret < 0)
> + return ret;
> + idx += ret;
> + continue;
> + }
> +
> if (field_type == 0)
> continue;
> if (field_type < 0)
[...]
next prev parent reply other threads:[~2024-05-02 19:34 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-01 20:47 [PATCH bpf-next v3 0/7] Enable BPF programs to declare arrays of kptr, bpf_rb_root, and bpf_list_head Kui-Feng Lee
2024-05-01 20:47 ` [PATCH bpf-next v3 1/7] bpf: Remove unnecessary checks on the offset of btf_field Kui-Feng Lee
2024-05-01 20:47 ` [PATCH bpf-next v3 2/7] bpf: Remove unnecessary call to btf_field_type_size() Kui-Feng Lee
2024-05-01 20:47 ` [PATCH bpf-next v3 3/7] bpf: create repeated fields for arrays Kui-Feng Lee
2024-05-02 17:20 ` Eduard Zingerman
2024-05-03 18:02 ` Kui-Feng Lee
2024-05-03 18:10 ` Eduard Zingerman
2024-05-01 20:47 ` [PATCH bpf-next v3 4/7] bpf: look into the types of the fields of a struct type recursively Kui-Feng Lee
2024-05-02 19:34 ` Eduard Zingerman [this message]
2024-05-03 18:07 ` Kui-Feng Lee
2024-05-01 20:47 ` [PATCH bpf-next v3 5/7] selftests/bpf: Test kptr arrays and kptrs in nested struct fields Kui-Feng Lee
2024-05-01 20:47 ` [PATCH bpf-next v3 6/7] selftests/bpf: Test global bpf_rb_root arrays and fields in nested struct types Kui-Feng Lee
2024-05-01 20:47 ` [PATCH bpf-next v3 7/7] selftests/bpf: Test global bpf_list_head arrays Kui-Feng Lee
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=ef932548808bd55dae8bccbbab63de60b86985ee.camel@gmail.com \
--to=eddyz87@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=kernel-team@meta.com \
--cc=kuifeng@meta.com \
--cc=martin.lau@linux.dev \
--cc=sinquersw@gmail.com \
--cc=song@kernel.org \
--cc=thinker.li@gmail.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).