All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dave Marchevsky <davemarchevsky@fb.com>
To: Andrii Nakryiko <andrii@kernel.org>, <bpf@vger.kernel.org>,
	<ast@kernel.org>, <daniel@iogearbox.net>
Cc: <kernel-team@fb.com>
Subject: Re: [PATCH v2 bpf-next 4/9] libbpf: refactor internal sec_def handling to enable pluggability
Date: Tue, 21 Sep 2021 20:42:28 -0400	[thread overview]
Message-ID: <2b9e2861-44ad-89bc-a2e9-04623f319a2a@fb.com> (raw)
In-Reply-To: <20210920234320.3312820-5-andrii@kernel.org>

On 9/20/21 7:43 PM, Andrii Nakryiko wrote:   
> Refactor internals of libbpf to allow adding custom SEC() handling logic
> easily from outside of libbpf. To that effect, each SEC()-handling
> registration sets mandatory program type/expected attach type for
> a given prefix and can provide three callbacks called at different
> points of BPF program lifetime:
> 
>   - init callback for right after bpf_program is initialized and
>   prog_type/expected_attach_type is set. This happens during
>   bpf_object__open() step, close to the very end of constructing
>   bpf_object, so all the libbpf APIs for querying and updating
>   bpf_program properties should be available;

Do you have a usecase in mind that would set this? USDT? 

>   - pre-load callback is called right before BPF_PROG_LOAD command is
>   called in the kernel. This callbacks has ability to set both
>   bpf_program properties, as well as program load attributes, overriding
>   and augmenting the standard libbpf handling of them;

[...]

> @@ -6094,6 +6100,44 @@ static int bpf_object__sanitize_prog(struct bpf_object *obj, struct bpf_program
>  	return 0;
>  }
>  
> +static int libbpf_find_attach_btf_id(struct bpf_program *prog, int *btf_obj_fd, int *btf_type_id);
> +
> +/* this is called as prog->sec_def->preload_fn for libbpf-supported sec_defs */
> +static int libbpf_preload_prog(struct bpf_program *prog,
> +			       struct bpf_prog_load_params *attr, long cookie)
> +{
> +	/* old kernels might not support specifying expected_attach_type */
> +	if (prog->sec_def->is_exp_attach_type_optional &&
> +	    !kernel_supports(prog->obj, FEAT_EXP_ATTACH_TYPE))
> +		attr->expected_attach_type = 0;
> +
> +	if (prog->sec_def->is_sleepable)
> +		attr->prog_flags |= BPF_F_SLEEPABLE;
> +
> +	if ((prog->type == BPF_PROG_TYPE_TRACING ||
> +	     prog->type == BPF_PROG_TYPE_LSM ||
> +	     prog->type == BPF_PROG_TYPE_EXT) && !prog->attach_btf_id) {
> +		int btf_obj_fd = 0, btf_type_id = 0, err;
> +
> +		err = libbpf_find_attach_btf_id(prog, &btf_obj_fd, &btf_type_id);
> +		if (err)
> +			return err;
> +
> +		/* cache resolved BTF FD and BTF type ID in the prog */
> +		prog->attach_btf_obj_fd = btf_obj_fd;
> +		prog->attach_btf_id = btf_type_id;
> +
> +		/* but by now libbpf common logic is not utilizing
> +		 * prog->atach_btf_obj_fd/prog->attach_btf_id anymore because
> +		 * this callback is called after attrs were populated by
> +		 * libbpf, so this callback has to update attr explicitly here
> +		 */
> +		attr->attach_btf_obj_fd = btf_obj_fd;
> +		attr->attach_btf_id = btf_type_id;
> +	}
> +	return 0;
> +}
> +
We talked on VC about some general approach questions I had here, will
summarize. Discussion touched on changes in patches 5 and 6 as well. I thought 
the pulling of these chunks into libbpf_preload_prog made sense, but wondered
whether some of this prog-type specific functionality would also be useful to
"average" custom sec_def writer even if it's not considered 'standard libbpf
handling', e.g. custom sec_def writer whose SEC produces a PROG_TYPE_TRACING
is likely to want the find_attach_btf_id niceness as well. So perhaps something
like the ability to chain the callbacks so that sec_def writer can use libbpf's
would be useful.

Your response was that you explicitly wanted to avoid doing this because this
would result in libbpf's callbacks becoming part of the API and stability 
requirements following from that. Furthermore, you don't anticipate libbpf's
preload callback becoming very complicated and expect that the average 
custom sec_def writer will be familiar enough with libbpf to be able to pull
out whatever they need.

Response made sense to me, LGTM

Acked-by: Dave Marchevsky <davemarchevsky@fb.com>

  reply	other threads:[~2021-09-22  0:42 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-20 23:43 [PATCH v2 bpf-next 0/9] libbpf: stricter BPF program section name handling Andrii Nakryiko
2021-09-20 23:43 ` [PATCH v2 bpf-next 1/9] selftests/bpf: normalize XDP section names in selftests Andrii Nakryiko
2021-09-21  4:55   ` Dave Marchevsky
2021-09-21 23:08     ` Andrii Nakryiko
2021-09-20 23:43 ` [PATCH v2 bpf-next 2/9] selftests/bpf: normalize SEC("classifier") usage Andrii Nakryiko
2021-09-21  5:20   ` Dave Marchevsky
2021-09-21 23:10     ` Andrii Nakryiko
2021-09-20 23:43 ` [PATCH v2 bpf-next 3/9] selftests/bpf: normalize all the rest SEC() uses Andrii Nakryiko
2021-09-21  5:41   ` Dave Marchevsky
2021-09-21 23:12     ` Andrii Nakryiko
2021-09-20 23:43 ` [PATCH v2 bpf-next 4/9] libbpf: refactor internal sec_def handling to enable pluggability Andrii Nakryiko
2021-09-22  0:42   ` Dave Marchevsky [this message]
2021-09-22 22:06     ` Andrii Nakryiko
2021-09-20 23:43 ` [PATCH v2 bpf-next 5/9] libbpf: reduce reliance of attach_fns on sec_def internals Andrii Nakryiko
2021-09-22  1:00   ` Dave Marchevsky
2021-09-20 23:43 ` [PATCH v2 bpf-next 6/9] libbpf: refactor ELF section handler definitions Andrii Nakryiko
2021-09-22  1:34   ` Dave Marchevsky
2021-09-22 21:54     ` Andrii Nakryiko
2021-09-20 23:43 ` [PATCH v2 bpf-next 7/9] libbpf: complete SEC() table unification for BPF_APROG_SEC/BPF_EAPROG_SEC Andrii Nakryiko
2021-09-22  1:42   ` Dave Marchevsky
2021-09-22 21:55     ` Andrii Nakryiko
2021-09-22 22:12       ` Andrii Nakryiko
2021-09-20 23:43 ` [PATCH v2 bpf-next 8/9] libbpf: add opt-in strict BPF program section name handling logic Andrii Nakryiko
2021-09-22  1:53   ` Dave Marchevsky
2021-09-22 21:57     ` Andrii Nakryiko
2021-09-20 23:43 ` [PATCH v2 bpf-next 9/9] selftests/bpf: switch sk_lookup selftests to strict SEC("sk_lookup") use Andrii Nakryiko
2021-09-22  2:37   ` Dave Marchevsky

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=2b9e2861-44ad-89bc-a2e9-04623f319a2a@fb.com \
    --to=davemarchevsky@fb.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 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.