All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Emil Tsalapatis" <emil@etsalapatis.com>
To: "Mykyta Yatsenko" <mykyta.yatsenko5@gmail.com>,
	<bpf@vger.kernel.org>, <ast@kernel.org>, <andrii@kernel.org>,
	<daniel@iogearbox.net>, <kafai@meta.com>, <kernel-team@meta.com>,
	<eddyz87@gmail.com>
Cc: "Mykyta Yatsenko" <yatsenko@meta.com>
Subject: Re: [PATCH bpf-next v4 3/3] selftests/bpf: Test bpf_program__clone() attach_btf_id override
Date: Tue, 17 Mar 2026 15:50:31 -0400	[thread overview]
Message-ID: <DH5BHB49LSWW.2K0INW3EFP8Z0@etsalapatis.com> (raw)
In-Reply-To: <20260317-veristat_prepare-v4-3-74193d4cc9d9@meta.com>

On Tue Mar 17, 2026 at 1:39 PM EDT, Mykyta Yatsenko wrote:
> From: Mykyta Yatsenko <yatsenko@meta.com>
>
> Add a test that verifies bpf_program__clone() respects caller-provided
> attach_btf_id in bpf_prog_load_opts.
>
> The BPF program has SEC("fentry/bpf_fentry_test1"). It is cloned twice
> from the same prepared object: first with no opts, verifying the
> callback resolves attach_btf_id from sec_name to bpf_fentry_test1;
> then with attach_btf_id overridden to bpf_fentry_test2, verifying the
> loaded program is actually attached to bpf_fentry_test2. Both results
> are checked via bpf_prog_get_info_by_fd().
>
> Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>

Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com>

> ---
>  .../selftests/bpf/prog_tests/clone_attach_btf_id.c | 78 ++++++++++++++++++++++
>  .../selftests/bpf/progs/clone_attach_btf_id.c      | 13 ++++
>  2 files changed, 91 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/clone_attach_btf_id.c b/tools/testing/selftests/bpf/prog_tests/clone_attach_btf_id.c
> new file mode 100644
> index 000000000000..1c3e28e74606
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/clone_attach_btf_id.c
> @@ -0,0 +1,78 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* Copyright (c) 2025 Meta */
> +#include <test_progs.h>
> +#include "clone_attach_btf_id.skel.h"
> +
> +/*
> + * Test that bpf_program__clone() respects caller-provided attach_btf_id
> + * override via bpf_prog_load_opts.
> + *
> + * The BPF program has SEC("fentry/bpf_fentry_test1"). Clone it twice
> + * from the same prepared object: first with no opts (callback resolves
> + * attach_btf_id from sec_name), then with attach_btf_id overridden to
> + * bpf_fentry_test2. Verify each loaded program's attach_btf_id via
> + * bpf_prog_get_info_by_fd().
> + */
> +
> +static int get_prog_attach_btf_id(int prog_fd)
> +{
> +	struct bpf_prog_info info = {};
Nit: I think this is not necessary because the fields are populated in
the kernel in bpf_prog_get_info_by_fd().
> +	__u32 info_len = sizeof(info);
> +	int err;
> +
> +	err = bpf_prog_get_info_by_fd(prog_fd, &info, &info_len);
> +	if (err)
> +		return err;
> +	return info.attach_btf_id;
> +}
> +
> +void test_clone_attach_btf_id(void)
> +{
> +	struct clone_attach_btf_id *skel;
> +	int fd1 = -1, fd2 = -1, err;
> +	int btf_id_test1, btf_id_test2;
> +
> +	btf_id_test1 = libbpf_find_vmlinux_btf_id("bpf_fentry_test1", BPF_TRACE_FENTRY);
> +	if (!ASSERT_GT(btf_id_test1, 0, "find_btf_id_test1"))
> +		return;
> +
> +	btf_id_test2 = libbpf_find_vmlinux_btf_id("bpf_fentry_test2", BPF_TRACE_FENTRY);
> +	if (!ASSERT_GT(btf_id_test2, 0, "find_btf_id_test2"))
> +		return;
> +
> +	skel = clone_attach_btf_id__open();
> +	if (!ASSERT_OK_PTR(skel, "skel_open"))
> +		return;
> +
> +	err = bpf_object__prepare(skel->obj);
> +	if (!ASSERT_OK(err, "obj_prepare"))
> +		goto out;
> +
> +	/* Clone with no opts — callback resolves BTF from sec_name */
> +	fd1 = bpf_program__clone(skel->progs.fentry_handler, NULL);
> +	if (!ASSERT_GE(fd1, 0, "clone_default"))
> +		goto out;
> +	ASSERT_EQ(get_prog_attach_btf_id(fd1), btf_id_test1,
> +		  "attach_btf_id_default");
> +
> +	/*
> +	 * Clone with attach_btf_id override pointing to a different
> +	 * function. The BPF program never accesses arguments, so the
> +	 * load succeeds regardless of signature mismatch.
> +	 */
> +	LIBBPF_OPTS(bpf_prog_load_opts, opts,
> +		    .attach_btf_id = btf_id_test2,
> +	);
> +	fd2 = bpf_program__clone(skel->progs.fentry_handler, &opts);
> +	if (!ASSERT_GE(fd2, 0, "clone_override"))
> +		goto out;
> +	ASSERT_EQ(get_prog_attach_btf_id(fd2), btf_id_test2,
> +		  "attach_btf_id_override");
> +
> +out:
> +	if (fd1 >= 0)
> +		close(fd1);
> +	if (fd2 >= 0)
> +		close(fd2);
> +	clone_attach_btf_id__destroy(skel);
> +}
> diff --git a/tools/testing/selftests/bpf/progs/clone_attach_btf_id.c b/tools/testing/selftests/bpf/progs/clone_attach_btf_id.c
> new file mode 100644
> index 000000000000..0ffa3ec3e1a0
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/clone_attach_btf_id.c
> @@ -0,0 +1,13 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* Copyright (c) 2025 Meta */
> +#include <linux/bpf.h>
> +#include <bpf/bpf_helpers.h>
> +#include <bpf/bpf_tracing.h>
> +
> +char _license[] SEC("license") = "GPL";
> +
> +SEC("fentry/bpf_fentry_test1")
> +int BPF_PROG(fentry_handler, int a)
> +{
> +	return 0;
> +}


  reply	other threads:[~2026-03-17 19:50 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-17 17:39 [PATCH bpf-next v4 0/3] libbpf: Add bpf_program__clone() for individual program loading Mykyta Yatsenko
2026-03-17 17:39 ` [PATCH bpf-next v4 1/3] libbpf: Introduce bpf_program__clone() Mykyta Yatsenko
2026-03-17 19:40   ` Emil Tsalapatis
2026-03-17 17:39 ` [PATCH bpf-next v4 2/3] selftests/bpf: Use bpf_program__clone() in veristat Mykyta Yatsenko
2026-03-17 17:39 ` [PATCH bpf-next v4 3/3] selftests/bpf: Test bpf_program__clone() attach_btf_id override Mykyta Yatsenko
2026-03-17 19:50   ` Emil Tsalapatis [this message]
2026-03-21 20:20 ` [PATCH bpf-next v4 0/3] libbpf: Add bpf_program__clone() for individual program loading patchwork-bot+netdevbpf

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=DH5BHB49LSWW.2K0INW3EFP8Z0@etsalapatis.com \
    --to=emil@etsalapatis.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=kafai@meta.com \
    --cc=kernel-team@meta.com \
    --cc=mykyta.yatsenko5@gmail.com \
    --cc=yatsenko@meta.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.