From: Eduard Zingerman <eddyz87@gmail.com>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>, davem@davemloft.net
Cc: daniel@iogearbox.net, andrii@kernel.org, martin.lau@kernel.org,
void@manifault.com, davemarchevsky@meta.com, tj@kernel.org,
memxor@gmail.com, netdev@vger.kernel.org, bpf@vger.kernel.org,
kernel-team@fb.com
Subject: Re: [PATCH bpf-next 1/2] bpf: Allow ld_imm64 instruction to point to kfunc.
Date: Thu, 16 Mar 2023 16:14:11 +0200 [thread overview]
Message-ID: <e3a68d87c4f7589ab19fe6ddf6b0341404108386.camel@gmail.com> (raw)
In-Reply-To: <20230315223607.50803-2-alexei.starovoitov@gmail.com>
On Wed, 2023-03-15 at 15:36 -0700, Alexei Starovoitov wrote:
> From: Alexei Starovoitov <ast@kernel.org>
>
> Allow ld_imm64 insn with BPF_PSEUDO_BTF_ID to hold the address of kfunc.
> PTR_MEM is already recognized for NULL-ness by is_branch_taken(),
> so unresolved kfuncs will be seen as zero.
> This allows BPF programs to detect at load time whether kfunc is present
> in the kernel with bpf_kfunc_exists() macro.
>
> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
> ---
> kernel/bpf/verifier.c | 7 +++++--
> tools/lib/bpf/bpf_helpers.h | 3 +++
> 2 files changed, 8 insertions(+), 2 deletions(-)
>
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 60793f793ca6..4e49d34d8cd6 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -15955,8 +15955,8 @@ static int check_pseudo_btf_id(struct bpf_verifier_env *env,
> goto err_put;
> }
>
> - if (!btf_type_is_var(t)) {
> - verbose(env, "pseudo btf_id %d in ldimm64 isn't KIND_VAR.\n", id);
> + if (!btf_type_is_var(t) && !btf_type_is_func(t)) {
> + verbose(env, "pseudo btf_id %d in ldimm64 isn't KIND_VAR or KIND_FUNC\n", id);
> err = -EINVAL;
> goto err_put;
> }
> @@ -15990,6 +15990,9 @@ static int check_pseudo_btf_id(struct bpf_verifier_env *env,
> aux->btf_var.reg_type = PTR_TO_BTF_ID | MEM_PERCPU;
> aux->btf_var.btf = btf;
> aux->btf_var.btf_id = type;
> + } else if (!btf_type_is_func(t)) {
> + aux->btf_var.reg_type = PTR_TO_MEM | MEM_RDONLY;
> + aux->btf_var.mem_size = 0;
This if statement has the following conditions in master:
if (percpu) {
// ...
} else if (!btf_type_is_struct(t)) {
// ...
} else {
// ...
}
Conditions `!btf_type_is_func()` and `!btf_type_is_struct()` are
not mutually exclusive, thus adding `if (!btf_type_is_func())`
would match certain conditions that were previously matched by struct
case, wouldn't it? E.g. if type is `BTF_KIND_INT`?
Although, I was not able to trigger it, as it seems that pahole only
encodes per-cpu vars in BTF.
> } else if (!btf_type_is_struct(t)) {
> const struct btf_type *ret;
> const char *tname;
> diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h
> index 7d12d3e620cc..43abe4c29409 100644
> --- a/tools/lib/bpf/bpf_helpers.h
> +++ b/tools/lib/bpf/bpf_helpers.h
> @@ -177,6 +177,9 @@ enum libbpf_tristate {
> #define __kptr_untrusted __attribute__((btf_type_tag("kptr_untrusted")))
> #define __kptr __attribute__((btf_type_tag("kptr")))
>
> +/* pass function pointer through asm otherwise compiler assumes that any function != 0 */
> +#define bpf_kfunc_exists(fn) ({ void *__p = fn; asm ("" : "+r"(__p)); __p; })
> +
> #ifndef ___bpf_concat
> #define ___bpf_concat(a, b) a ## b
> #endif
next prev parent reply other threads:[~2023-03-16 14:15 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-15 22:36 [PATCH bpf-next 0/2] bpf: Add detection of kfuncs Alexei Starovoitov
2023-03-15 22:36 ` [PATCH bpf-next 1/2] bpf: Allow ld_imm64 instruction to point to kfunc Alexei Starovoitov
2023-03-16 14:14 ` Eduard Zingerman [this message]
2023-03-16 16:45 ` Alexei Starovoitov
2023-03-16 20:34 ` Andrii Nakryiko
2023-03-16 22:25 ` Alexei Starovoitov
2023-03-16 23:06 ` Andrii Nakryiko
2023-03-17 1:39 ` Alexei Starovoitov
2023-03-17 16:28 ` Andrii Nakryiko
2023-03-15 22:36 ` [PATCH bpf-next 2/2] selftests/bpf: Add test for bpf_kfunc_exists() Alexei Starovoitov
2023-03-16 20:34 ` Andrii Nakryiko
2023-03-16 22:35 ` Alexei Starovoitov
2023-03-16 23:23 ` Andrii Nakryiko
2023-03-16 0:33 ` [PATCH bpf-next 0/2] bpf: Add detection of kfuncs Martin KaFai Lau
2023-03-16 5:34 ` John Fastabend
2023-03-16 10:31 ` Toke Høiland-Jørgensen
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=e3a68d87c4f7589ab19fe6ddf6b0341404108386.camel@gmail.com \
--to=eddyz87@gmail.com \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=davemarchevsky@meta.com \
--cc=kernel-team@fb.com \
--cc=martin.lau@kernel.org \
--cc=memxor@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=tj@kernel.org \
--cc=void@manifault.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).