public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
From: Eduard Zingerman <eddyz87@gmail.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
Cc: Mykyta Yatsenko <yatsenko@meta.com>
Subject: Re: [PATCH bpf-next v2 1/2] libbpf: Introduce bpf_program__clone()
Date: Tue, 24 Feb 2026 11:28:26 -0800	[thread overview]
Message-ID: <83cf0430bf164c46abe56d2ec6565ca57ead1663.camel@gmail.com> (raw)
In-Reply-To: <20260220-veristat_prepare-v2-1-15bff49022a7@meta.com>

On Fri, 2026-02-20 at 11:18 -0800, Mykyta Yatsenko wrote:
> From: Mykyta Yatsenko <yatsenko@meta.com>
> 
> Add bpf_program__clone() API that loads a single BPF program from a
> prepared BPF object into the kernel, returning a file descriptor owned
> by the caller.
> 
> After bpf_object__prepare(), callers can use bpf_program__clone() to
> load individual programs with custom bpf_prog_load_opts, instead of
> loading all programs at once via bpf_object__load(). Non-zero fields in
> opts override the defaults derived from the program and object
> internals; passing NULL opts populates everything automatically.
> 
> Internally, bpf_program__clone() resolves BTF-based attach targets
> (attach_btf_id, attach_btf_obj_fd) and the sleepable flag, fills
> func/line info, fd_array, license, and kern_version from the
> prepared object before calling bpf_prog_load().
> 
> Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
> ---
>  tools/lib/bpf/libbpf.c   | 64 ++++++++++++++++++++++++++++++++++++++++++++++++
>  tools/lib/bpf/libbpf.h   | 17 +++++++++++++
>  tools/lib/bpf/libbpf.map |  1 +
>  3 files changed, 82 insertions(+)
> 
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 0c8bf0b5cce4..4b084bda3f47 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -9793,6 +9793,70 @@ __u32 bpf_program__line_info_cnt(const struct bpf_program *prog)
>  	return prog->line_info_cnt;
>  }
>  
> +int bpf_program__clone(struct bpf_program *prog, const struct bpf_prog_load_opts *opts)
> +{
> +	LIBBPF_OPTS(bpf_prog_load_opts, attr);
> +	struct bpf_prog_load_opts *pattr = &attr;
> +	struct bpf_object *obj;
> +	int err, fd;
> +
> +	if (!prog)
> +		return libbpf_err(-EINVAL);
> +
> +	if (!OPTS_VALID(opts, bpf_prog_load_opts))
> +		return libbpf_err(-EINVAL);
> +
> +	obj = prog->obj;
> +	if (obj->state < OBJ_PREPARED)
> +		return libbpf_err(-EINVAL);
> +
> +	/* Copy caller opts, fall back to prog/object defaults */
> +	OPTS_SET(pattr, expected_attach_type,
> +		 OPTS_GET(opts, expected_attach_type, 0) ?: prog->expected_attach_type);
> +	OPTS_SET(pattr, attach_btf_id, OPTS_GET(opts, attach_btf_id, 0) ?: prog->attach_btf_id);
> +	OPTS_SET(pattr, attach_btf_obj_fd,
> +		 OPTS_GET(opts, attach_btf_obj_fd, 0) ?: prog->attach_btf_obj_fd);
> +	OPTS_SET(pattr, attach_prog_fd, OPTS_GET(opts, attach_prog_fd, 0) ?: prog->attach_prog_fd);
> +	OPTS_SET(pattr, prog_flags, OPTS_GET(opts, prog_flags, 0) ?: prog->prog_flags);
> +	OPTS_SET(pattr, prog_ifindex, OPTS_GET(opts, prog_ifindex, 0) ?: prog->prog_ifindex);
> +	OPTS_SET(pattr, kern_version, OPTS_GET(opts, kern_version, 0) ?: obj->kern_version);
> +	OPTS_SET(pattr, fd_array, OPTS_GET(opts, fd_array, NULL) ?: obj->fd_array);

It seems 'fd_array_cnt' is not copied, should it be?

> +	OPTS_SET(pattr, token_fd, OPTS_GET(opts, token_fd, 0) ?: obj->token_fd);
> +	if (attr.token_fd)
> +		attr.prog_flags |= BPF_F_TOKEN_FD;

Nit: should this be 'if (OPTS_GET(opts, token_fd, 0) && attr.token_fd)' ?

> +
> +	/* BTF func/line info */
> +	if (obj->btf && btf__fd(obj->btf) >= 0) {
> +		OPTS_SET(pattr, prog_btf_fd, OPTS_GET(opts, prog_btf_fd, 0) ?: btf__fd(obj->btf));
> +		OPTS_SET(pattr, func_info, OPTS_GET(opts, func_info, NULL) ?: prog->func_info);
> +		OPTS_SET(pattr, func_info_cnt,
> +			 OPTS_GET(opts, func_info_cnt, 0) ?: prog->func_info_cnt);
> +		OPTS_SET(pattr, func_info_rec_size,
> +			 OPTS_GET(opts, func_info_rec_size, 0) ?: prog->func_info_rec_size);
> +		OPTS_SET(pattr, line_info, OPTS_GET(opts, line_info, NULL) ?: prog->line_info);
> +		OPTS_SET(pattr, line_info_cnt,
> +			 OPTS_GET(opts, line_info_cnt, 0) ?: prog->line_info_cnt);
> +		OPTS_SET(pattr, line_info_rec_size,
> +			 OPTS_GET(opts, line_info_rec_size, 0) ?: prog->line_info_rec_size);
> +	}
> +
> +	OPTS_SET(pattr, log_buf, OPTS_GET(opts, log_buf, NULL));
> +	OPTS_SET(pattr, log_size, OPTS_GET(opts, log_size, 0));
> +	OPTS_SET(pattr, log_level, OPTS_GET(opts, log_level, 0));

Just curious why did you decide not to inherit logging properties from
the original program?
Unless overridden, the original program would point to the buffer
specified for the object in bpf_object_open_opts->kernel_log_buf, right?

> +
> +	/* Resolve BTF attach targets, set sleepable/XDP flags, etc. */
> +	if (prog->sec_def && prog->sec_def->prog_prepare_load_fn) {
> +		err = prog->sec_def->prog_prepare_load_fn(prog, pattr, prog->sec_def->cookie);
> +		if (err)
> +			return libbpf_err(err);
> +	}
> +
> +	fd = bpf_prog_load(prog->type, prog->name, obj->license, prog->insns, prog->insns_cnt,
> +			   pattr);
> +
> +	return libbpf_err(fd);
> +}
> +
>  #define SEC_DEF(sec_pfx, ptype, atype, flags, ...) {			    \
>  	.sec = (char *)sec_pfx,						    \
>  	.prog_type = BPF_PROG_TYPE_##ptype,				    \

  parent reply	other threads:[~2026-02-24 19:28 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-20 19:18 [PATCH bpf-next v2 0/2] libbpf: Add bpf_program__clone() for individual program loading Mykyta Yatsenko
2026-02-20 19:18 ` [PATCH bpf-next v2 1/2] libbpf: Introduce bpf_program__clone() Mykyta Yatsenko
2026-02-23 17:25   ` Emil Tsalapatis
2026-02-23 17:59     ` Mykyta Yatsenko
2026-02-23 18:04       ` Emil Tsalapatis
2026-02-24 19:28   ` Eduard Zingerman [this message]
2026-02-24 19:32     ` Eduard Zingerman
2026-02-24 20:47     ` Mykyta Yatsenko
2026-03-06 17:22   ` [External] " Andrey Grodzovsky
2026-03-10  0:08     ` Mykyta Yatsenko
2026-03-11 13:35       ` Andrey Grodzovsky
2026-03-11 22:52     ` Andrii Nakryiko
2026-03-16 14:23       ` Andrey Grodzovsky
2026-03-11 23:03   ` Andrii Nakryiko
2026-02-20 19:18 ` [PATCH bpf-next v2 2/2] selftests/bpf: Use bpf_program__clone() in veristat Mykyta Yatsenko
2026-02-23 17:49   ` Emil Tsalapatis
2026-02-23 18:39     ` Mykyta Yatsenko
2026-02-23 18:54       ` Emil Tsalapatis
2026-02-24  2:03   ` Eduard Zingerman
2026-02-24 12:20     ` Mykyta Yatsenko
2026-02-24 19:08       ` Eduard Zingerman
2026-02-24 19:12         ` Mykyta Yatsenko
2026-02-24 19:16           ` Eduard Zingerman
2026-02-20 22:48 ` [PATCH bpf-next v2 0/2] libbpf: Add bpf_program__clone() for individual program loading Alexei Starovoitov
2026-02-23 13:57   ` Mykyta Yatsenko

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=83cf0430bf164c46abe56d2ec6565ca57ead1663.camel@gmail.com \
    --to=eddyz87@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox