All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stanislav Fomichev <sdf@google.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: bpf@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net,
	andrii@kernel.org, martin.lau@linux.dev, song@kernel.org,
	yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org,
	haoluo@google.com, jolsa@kernel.org
Subject: Re: [PATCH bpf-next v2 2/2] selftests/bpf: Excercise bpf_obj_get_info_by_fd for bpf2bpf
Date: Tue, 2 Aug 2022 09:21:39 -0700	[thread overview]
Message-ID: <CAKH8qBtiRtcicr4553k87s8YeJa_6qOz=-e9DCNwO9bfa2v35g@mail.gmail.com> (raw)
In-Reply-To: <CAEf4BzYfSoxeFrf7t73tKSOMrH==6ZnvV1dLaWK9OakkQg7MpA@mail.gmail.com>

On Mon, Aug 1, 2022 at 2:44 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Mon, Aug 1, 2022 at 10:39 AM Stanislav Fomichev <sdf@google.com> wrote:
> >
> > Apparently, no existing selftest covers it. Add a new one where
> > we load cgroup/bind4 program and attach fentry to it.
> > Calling bpf_obj_get_info_by_fd on the fentry program
> > should return non-zero btf_id/btf_obj_id instead of crashing the kernel.
> >
> > v2:
> > - use ret instead of err in find_prog_btf_id (Hao)
> > - remove verifier log (Hao)
> > - drop if conditional from ASSERT_OK(bpf_obj_get_info_by_fd(...)) (Hao)
> >
> > Signed-off-by: Stanislav Fomichev <sdf@google.com>
> > ---
> >  .../selftests/bpf/prog_tests/attach_to_bpf.c  | 97 +++++++++++++++++++
> >  .../selftests/bpf/progs/attach_to_bpf.c       | 12 +++
> >  2 files changed, 109 insertions(+)
> >  create mode 100644 tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c
> >  create mode 100644 tools/testing/selftests/bpf/progs/attach_to_bpf.c
> >
>
> [...]
>
> > +
> > +       ret = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
> > +       btf__free(btf);
> > +       return ret;
> > +}
> > +
> > +int load_fentry(int attach_prog_fd, int attach_btf_id)
>
> static?
>
> > +{
> > +       LIBBPF_OPTS(bpf_prog_load_opts, opts,
> > +                   .expected_attach_type = BPF_TRACE_FENTRY,
> > +                   .attach_prog_fd = attach_prog_fd,
> > +                   .attach_btf_id = attach_btf_id,
> > +       );
> > +       struct bpf_insn insns[] = {
> > +               BPF_MOV64_IMM(BPF_REG_0, 0),
> > +               BPF_EXIT_INSN(),
> > +       };
> > +
> > +       return bpf_prog_load(BPF_PROG_TYPE_TRACING,
> > +                            "bind4_fentry",
> > +                            "GPL",
> > +                            insns,
> > +                            ARRAY_SIZE(insns),
> > +                            &opts);
> > +}
> > +
> > +void test_attach_to_bpf(void)
> > +{
> > +       struct attach_to_bpf *skel = NULL;
> > +       struct bpf_prog_info info = {};
> > +       __u32 info_len = sizeof(info);
> > +       int cgroup_fd = -1;
> > +       int fentry_fd = -1;
> > +       int btf_id;
> > +
> > +       cgroup_fd = test__join_cgroup("/attach_to_bpf");
> > +       if (!ASSERT_GE(cgroup_fd, 0, "cgroup_fd"))
> > +               return;
> > +
> > +       skel = attach_to_bpf__open_and_load();
> > +       if (!ASSERT_OK_PTR(skel, "skel"))
> > +               goto cleanup;
> > +
> > +       skel->links.bind4 = bpf_program__attach_cgroup(skel->progs.bind4, cgroup_fd);
> > +       if (!ASSERT_OK_PTR(skel, "bpf_program__attach_cgroup"))
>
> you probably meant to check skel->links.bind4 instead of just skel
> (which you already checked)

Oh, good catch, thanks!

> > +               goto cleanup;
> > +
> > +       btf_id = find_prog_btf_id("bind4", bpf_program__fd(skel->progs.bind4));
> > +       if (!ASSERT_GE(btf_id, 0, "find_prog_btf_id"))
> > +               goto cleanup;
> > +
> > +       fentry_fd = load_fentry(bpf_program__fd(skel->progs.bind4), btf_id);
> > +       if (!ASSERT_GE(fentry_fd, 0, "load_fentry"))
> > +               goto cleanup;
> > +
> > +       /* Make sure bpf_obj_get_info_by_fd works correctly when attaching
> > +        * to another BPF program.
> > +        */
> > +
> > +       ASSERT_OK(bpf_obj_get_info_by_fd(fentry_fd, &info, &info_len),
> > +                 "bpf_obj_get_info_by_fd");
> > +
> > +       ASSERT_EQ(info.btf_id, 0, "info.btf_id");
> > +       ASSERT_GT(info.attach_btf_id, 0, "info.attach_btf_id");
> > +       ASSERT_GT(info.attach_btf_obj_id, 0, "info.attach_btf_obj_id");
> > +
> > +cleanup:
>
> if (cgroup_fd >= 0)
>
> > +       close(cgroup_fd);
>
> if (fentry_fd >= 0)

Should be safe to do unconditional close(-1), right? Why bother with
the checks here? Seems like a common pattern we do elsewhere?

> > +       close(fentry_fd);
> > +       attach_to_bpf__destroy(skel);
> > +}
> > diff --git a/tools/testing/selftests/bpf/progs/attach_to_bpf.c b/tools/testing/selftests/bpf/progs/attach_to_bpf.c
> > new file mode 100644
> > index 000000000000..3f111fe96f8f
> > --- /dev/null
> > +++ b/tools/testing/selftests/bpf/progs/attach_to_bpf.c
> > @@ -0,0 +1,12 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +
> > +#include <linux/bpf.h>
> > +#include <bpf/bpf_helpers.h>
> > +
> > +SEC("cgroup/bind4")
> > +int bind4(struct bpf_sock_addr *ctx)
> > +{
> > +       return 1;
> > +}
> > +
> > +char _license[] SEC("license") = "GPL";
> > --
> > 2.37.1.455.g008518b4e5-goog
> >

  reply	other threads:[~2022-08-02 16:21 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-01 17:39 [PATCH bpf-next v2 1/2] bpf: use proper target btf when exporting attach_btf_obj_id Stanislav Fomichev
2022-08-01 17:39 ` [PATCH bpf-next v2 2/2] selftests/bpf: Excercise bpf_obj_get_info_by_fd for bpf2bpf Stanislav Fomichev
2022-08-01 19:33   ` Hao Luo
2022-08-02 16:24     ` Stanislav Fomichev
2022-08-01 21:43   ` Andrii Nakryiko
2022-08-02 16:21     ` Stanislav Fomichev [this message]
2022-08-02 18:42       ` Andrii Nakryiko
2022-08-02 20:53         ` Stanislav Fomichev
2022-08-03  0:38           ` Andrii Nakryiko
2022-08-03 16:32 [PATCH bpf-next v2 1/2] bpf: use proper target btf when exporting attach_btf_obj_id Stanislav Fomichev
2022-08-03 16:32 ` [PATCH bpf-next v2 2/2] selftests/bpf: Excercise bpf_obj_get_info_by_fd for bpf2bpf Stanislav Fomichev
2022-08-03 16:51   ` Martin KaFai Lau
2022-08-03 17:10     ` Stanislav Fomichev
2022-08-03 17:19       ` Martin KaFai Lau
2022-08-03 18:27         ` Stanislav Fomichev

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='CAKH8qBtiRtcicr4553k87s8YeJa_6qOz=-e9DCNwO9bfa2v35g@mail.gmail.com' \
    --to=sdf@google.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=song@kernel.org \
    --cc=yhs@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.