[PATCH bpf-next v5 2/3] libbpf: Move insn_is_pseudo_func() to libbpf_internal.h

Tiezhu Yang posted 3 patches 1 year, 11 months ago
There is a newer version of this series
[PATCH bpf-next v5 2/3] libbpf: Move insn_is_pseudo_func() to libbpf_internal.h
Posted by Tiezhu Yang 1 year, 11 months ago
Currently, insn_is_pseudo_func() is only used in libbpf.c, move it
to libbpf_internal.h so that it can be used in test_verifier, this
is preparation for later patch.

Suggested-by: Song Liu <song@kernel.org>
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 tools/lib/bpf/libbpf.c          | 5 -----
 tools/lib/bpf/libbpf_internal.h | 5 +++++
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index c5a42ac309fd..259d585d6ff5 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -748,11 +748,6 @@ static bool is_call_insn(const struct bpf_insn *insn)
 	return insn->code == (BPF_JMP | BPF_CALL);
 }
 
-static bool insn_is_pseudo_func(struct bpf_insn *insn)
-{
-	return is_ldimm64_insn(insn) && insn->src_reg == BPF_PSEUDO_FUNC;
-}
-
 static int
 bpf_object__init_prog(struct bpf_object *obj, struct bpf_program *prog,
 		      const char *name, size_t sec_idx, const char *sec_name,
diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h
index 27e4e320e1a6..a9c337345aff 100644
--- a/tools/lib/bpf/libbpf_internal.h
+++ b/tools/lib/bpf/libbpf_internal.h
@@ -532,6 +532,11 @@ static inline bool is_ldimm64_insn(struct bpf_insn *insn)
 	return insn->code == (BPF_LD | BPF_IMM | BPF_DW);
 }
 
+static inline bool insn_is_pseudo_func(struct bpf_insn *insn)
+{
+	return is_ldimm64_insn(insn) && insn->src_reg == BPF_PSEUDO_FUNC;
+}
+
 /* if fd is stdin, stdout, or stderr, dup to a fd greater than 2
  * Takes ownership of the fd passed in, and closes it if calling
  * fcntl(fd, F_DUPFD_CLOEXEC, 3).
-- 
2.42.0
Re: [PATCH bpf-next v5 2/3] libbpf: Move insn_is_pseudo_func() to libbpf_internal.h
Posted by Andrii Nakryiko 1 year, 11 months ago
On Wed, Jan 17, 2024 at 3:10 AM Tiezhu Yang <yangtiezhu@loongson.cn> wrote:
>
> Currently, insn_is_pseudo_func() is only used in libbpf.c, move it
> to libbpf_internal.h so that it can be used in test_verifier, this
> is preparation for later patch.
>
> Suggested-by: Song Liu <song@kernel.org>
> Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
> ---
>  tools/lib/bpf/libbpf.c          | 5 -----
>  tools/lib/bpf/libbpf_internal.h | 5 +++++
>  2 files changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index c5a42ac309fd..259d585d6ff5 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -748,11 +748,6 @@ static bool is_call_insn(const struct bpf_insn *insn)
>         return insn->code == (BPF_JMP | BPF_CALL);
>  }
>
> -static bool insn_is_pseudo_func(struct bpf_insn *insn)
> -{
> -       return is_ldimm64_insn(insn) && insn->src_reg == BPF_PSEUDO_FUNC;
> -}
> -
>  static int
>  bpf_object__init_prog(struct bpf_object *obj, struct bpf_program *prog,
>                       const char *name, size_t sec_idx, const char *sec_name,
> diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h
> index 27e4e320e1a6..a9c337345aff 100644
> --- a/tools/lib/bpf/libbpf_internal.h
> +++ b/tools/lib/bpf/libbpf_internal.h
> @@ -532,6 +532,11 @@ static inline bool is_ldimm64_insn(struct bpf_insn *insn)
>         return insn->code == (BPF_LD | BPF_IMM | BPF_DW);
>  }
>
> +static inline bool insn_is_pseudo_func(struct bpf_insn *insn)
> +{
> +       return is_ldimm64_insn(insn) && insn->src_reg == BPF_PSEUDO_FUNC;
> +}
> +

This just adds more internal code of libbpf used by selftests. While
we've allowed it in some cases to avoid duplication of more complex
logic, I don't feel like it's justified in this case. These helpers
are trivial enough to copy/paste somewhere into selftests helpers
header, so please do that instead.

>  /* if fd is stdin, stdout, or stderr, dup to a fd greater than 2
>   * Takes ownership of the fd passed in, and closes it if calling
>   * fcntl(fd, F_DUPFD_CLOEXEC, 3).
> --
> 2.42.0
>
Re: [PATCH bpf-next v5 2/3] libbpf: Move insn_is_pseudo_func() to libbpf_internal.h
Posted by Hou Tao 1 year, 11 months ago

On 1/17/2024 7:09 PM, Tiezhu Yang wrote:
> Currently, insn_is_pseudo_func() is only used in libbpf.c, move it
> to libbpf_internal.h so that it can be used in test_verifier, this
> is preparation for later patch.
>
> Suggested-by: Song Liu <song@kernel.org>
> Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>

Acked-by: Hou Tao <houtao1@huawei.com>
Re: [PATCH bpf-next v5 2/3] libbpf: Move insn_is_pseudo_func() to libbpf_internal.h
Posted by Song Liu 1 year, 11 months ago
On Wed, Jan 17, 2024 at 3:10 AM Tiezhu Yang <yangtiezhu@loongson.cn> wrote:
>
> Currently, insn_is_pseudo_func() is only used in libbpf.c, move it
> to libbpf_internal.h so that it can be used in test_verifier, this
> is preparation for later patch.
>
> Suggested-by: Song Liu <song@kernel.org>
> Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>

Acked-by: Song Liu <song@kernel.org>