public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next v3 0/3] Add prog_kfunc feature probe
@ 2025-01-24 14:44 Tao Chen
  2025-01-24 14:44 ` [PATCH bpf-next v3 1/3] libbpf: Refactor libbpf_probe_bpf_helper Tao Chen
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Tao Chen @ 2025-01-24 14:44 UTC (permalink / raw)
  To: ast, daniel, andrii, eddyz87, haoluo, jolsa, qmo
  Cc: bpf, linux-kernel, chen.dylane

More and more kfunc functions are being added to the kernel.
Different prog types have different restrictions when using kfunc.
Therefore, prog_kfunc probe is added to check whether it is supported,
and the use of this api will be added to bpftool later.

Change list:
- v2 -> v3:
  - rename parameter off with btf_fd
  - extract the common part for libbpf_probe_bpf_{helper,kfunc}
- v2
  https://lore.kernel.org/bpf/20250123170555.291896-1-chen.dylane@gmail.com

- v1 -> v2:
  - check unsupported prog type like probe_bpf_helper
  - add off parameter for module btf
  - check verifier info when kfunc id invalid
- v1
  https://lore.kernel.org/bpf/20250122171359.232791-1-chen.dylane@gmail.com

Tao Chen (3):
  libbpf: Refactor libbpf_probe_bpf_helper
  libbpf: Add libbpf_probe_bpf_kfunc API
  selftests/bpf: Add libbpf_probe_bpf_kfunc API selftests

 tools/lib/bpf/libbpf.h                        | 17 ++++-
 tools/lib/bpf/libbpf.map                      |  1 +
 tools/lib/bpf/libbpf_probes.c                 | 68 +++++++++++++++----
 .../selftests/bpf/prog_tests/libbpf_probes.c  | 35 ++++++++++
 4 files changed, 108 insertions(+), 13 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [PATCH bpf-next v3 1/3] libbpf: Refactor libbpf_probe_bpf_helper
  2025-01-24 14:44 [PATCH bpf-next v3 0/3] Add prog_kfunc feature probe Tao Chen
@ 2025-01-24 14:44 ` Tao Chen
  2025-01-24 16:26   ` Jiri Olsa
  2025-01-24 18:44   ` Andrii Nakryiko
  2025-01-24 14:44 ` [PATCH bpf-next v3 2/3] libbpf: Add libbpf_probe_bpf_kfunc API Tao Chen
  2025-01-24 14:44 ` [PATCH bpf-next v3 3/3] selftests/bpf: Add libbpf_probe_bpf_kfunc API selftests Tao Chen
  2 siblings, 2 replies; 15+ messages in thread
From: Tao Chen @ 2025-01-24 14:44 UTC (permalink / raw)
  To: ast, daniel, andrii, eddyz87, haoluo, jolsa, qmo
  Cc: bpf, linux-kernel, chen.dylane

Extract the common part as probe_func_comm, which will be used in
both libbpf_probe_bpf_{helper, kfunc}

Signed-off-by: Tao Chen <chen.dylane@gmail.com>
---
 tools/lib/bpf/libbpf_probes.c | 38 ++++++++++++++++++++++++-----------
 1 file changed, 26 insertions(+), 12 deletions(-)

diff --git a/tools/lib/bpf/libbpf_probes.c b/tools/lib/bpf/libbpf_probes.c
index 9dfbe7750f56..b73345977b4e 100644
--- a/tools/lib/bpf/libbpf_probes.c
+++ b/tools/lib/bpf/libbpf_probes.c
@@ -413,22 +413,20 @@ int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts)
 	return libbpf_err(ret);
 }
 
-int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, enum bpf_func_id helper_id,
-			    const void *opts)
+static int probe_func_comm(enum bpf_prog_type prog_type, struct bpf_insn insn,
+			   char *accepted_msgs, size_t msgs_size)
 {
 	struct bpf_insn insns[] = {
-		BPF_EMIT_CALL((__u32)helper_id),
+		BPF_EXIT_INSN(),
 		BPF_EXIT_INSN(),
 	};
 	const size_t insn_cnt = ARRAY_SIZE(insns);
-	char buf[4096];
-	int ret;
+	int err;
 
-	if (opts)
-		return libbpf_err(-EINVAL);
+	insns[0] = insn;
 
 	/* we can't successfully load all prog types to check for BPF helper
-	 * support, so bail out with -EOPNOTSUPP error
+	 * and kfunc support, so bail out with -EOPNOTSUPP error
 	 */
 	switch (prog_type) {
 	case BPF_PROG_TYPE_TRACING:
@@ -440,10 +438,26 @@ int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, enum bpf_func_id helpe
 		break;
 	}
 
-	buf[0] = '\0';
-	ret = probe_prog_load(prog_type, insns, insn_cnt, buf, sizeof(buf));
-	if (ret < 0)
-		return libbpf_err(ret);
+	accepted_msgs[0] = '\0';
+	err = probe_prog_load(prog_type, insns, insn_cnt, accepted_msgs, msgs_size);
+	if (err < 0)
+		return libbpf_err(err);
+
+	return 0;
+}
+
+int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, enum bpf_func_id helper_id,
+			    const void *opts)
+{
+	char buf[4096];
+	int ret;
+
+	if (opts)
+		return libbpf_err(-EINVAL);
+
+	ret = probe_func_comm(prog_type, BPF_EMIT_CALL((__u32)helper_id), buf, sizeof(buf));
+	if (ret)
+		return ret;
 
 	/* If BPF verifier doesn't recognize BPF helper ID (enum bpf_func_id)
 	 * at all, it will emit something like "invalid func unknown#181".
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH bpf-next v3 2/3] libbpf: Add libbpf_probe_bpf_kfunc API
  2025-01-24 14:44 [PATCH bpf-next v3 0/3] Add prog_kfunc feature probe Tao Chen
  2025-01-24 14:44 ` [PATCH bpf-next v3 1/3] libbpf: Refactor libbpf_probe_bpf_helper Tao Chen
@ 2025-01-24 14:44 ` Tao Chen
  2025-01-24 16:27   ` Jiri Olsa
  2025-01-24 18:46   ` Andrii Nakryiko
  2025-01-24 14:44 ` [PATCH bpf-next v3 3/3] selftests/bpf: Add libbpf_probe_bpf_kfunc API selftests Tao Chen
  2 siblings, 2 replies; 15+ messages in thread
From: Tao Chen @ 2025-01-24 14:44 UTC (permalink / raw)
  To: ast, daniel, andrii, eddyz87, haoluo, jolsa, qmo
  Cc: bpf, linux-kernel, chen.dylane

Similarly to libbpf_probe_bpf_helper, the libbpf_probe_bpf_kfunc
used to test the availability of the different eBPF kfuncs on the
current system.

Signed-off-by: Tao Chen <chen.dylane@gmail.com>
---
 tools/lib/bpf/libbpf.h        | 17 ++++++++++++++++-
 tools/lib/bpf/libbpf.map      |  1 +
 tools/lib/bpf/libbpf_probes.c | 30 ++++++++++++++++++++++++++++++
 3 files changed, 47 insertions(+), 1 deletion(-)

diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 3020ee45303a..035829e22099 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -1680,7 +1680,22 @@ LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void
  */
 LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type,
 				       enum bpf_func_id helper_id, const void *opts);
-
+/**
+ * @brief **libbpf_probe_bpf_kfunc()** detects if host kernel supports the
+ * use of a given BPF kfunc from specified BPF program type.
+ * @param prog_type BPF program type used to check the support of BPF kfunc
+ * @param kfunc_id The btf ID of BPF kfunc to check support for
+ * @param btf_fd The module BTF FD, 0 for vmlinux
+ * @param opts reserved for future extensibility, should be NULL
+ * @return 1, if given combination of program type and kfunc is supported; 0,
+ * if the combination is not supported; negative error code if feature
+ * detection for provided input arguments failed or can't be performed
+ *
+ * Make sure the process has required set of CAP_* permissions (or runs as
+ * root) when performing feature checking.
+ */
+LIBBPF_API int libbpf_probe_bpf_kfunc(enum bpf_prog_type prog_type,
+				      int kfunc_id, __s16 btf_fd, const void *opts);
 /**
  * @brief **libbpf_num_possible_cpus()** is a helper function to get the
  * number of possible CPUs that the host kernel supports and expects.
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index a8b2936a1646..e93fae101efd 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -436,4 +436,5 @@ LIBBPF_1.6.0 {
 		bpf_linker__add_buf;
 		bpf_linker__add_fd;
 		bpf_linker__new_fd;
+		libbpf_probe_bpf_kfunc;
 } LIBBPF_1.5.0;
diff --git a/tools/lib/bpf/libbpf_probes.c b/tools/lib/bpf/libbpf_probes.c
index b73345977b4e..cd7d16c1cc49 100644
--- a/tools/lib/bpf/libbpf_probes.c
+++ b/tools/lib/bpf/libbpf_probes.c
@@ -446,6 +446,36 @@ static int probe_func_comm(enum bpf_prog_type prog_type, struct bpf_insn insn,
 	return 0;
 }
 
+int libbpf_probe_bpf_kfunc(enum bpf_prog_type prog_type, int kfunc_id,
+			   __s16 btf_fd, const void *opts)
+{
+	struct bpf_insn insn;
+	int err;
+	char buf[4096];
+
+	if (opts)
+		return libbpf_err(-EINVAL);
+
+	insn.code = BPF_JMP | BPF_CALL;
+	insn.src_reg = BPF_PSEUDO_KFUNC_CALL;
+	insn.imm = kfunc_id;
+	insn.off = btf_fd;
+
+	err = probe_func_comm(prog_type, insn, buf, sizeof(buf));
+	if (err)
+		return err;
+
+	/* If BPF verifier recognizes BPF kfunc but it's not supported for
+	 * given BPF program type, it will emit "calling kernel function
+	 * bpf_cpumask_create is not allowed", if the kfunc id is invalid,
+	 * it will emit "kernel btf_id 4294967295 is not a function".
+	 */
+	if (err == 0 && (strstr(buf, "not allowed") || strstr(buf, "not a function")))
+		return 0;
+
+	return 1; /* assume supported */
+}
+
 int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, enum bpf_func_id helper_id,
 			    const void *opts)
 {
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH bpf-next v3 3/3] selftests/bpf: Add libbpf_probe_bpf_kfunc API selftests
  2025-01-24 14:44 [PATCH bpf-next v3 0/3] Add prog_kfunc feature probe Tao Chen
  2025-01-24 14:44 ` [PATCH bpf-next v3 1/3] libbpf: Refactor libbpf_probe_bpf_helper Tao Chen
  2025-01-24 14:44 ` [PATCH bpf-next v3 2/3] libbpf: Add libbpf_probe_bpf_kfunc API Tao Chen
@ 2025-01-24 14:44 ` Tao Chen
  2025-01-24 16:27   ` Jiri Olsa
  2025-01-24 18:48   ` Andrii Nakryiko
  2 siblings, 2 replies; 15+ messages in thread
From: Tao Chen @ 2025-01-24 14:44 UTC (permalink / raw)
  To: ast, daniel, andrii, eddyz87, haoluo, jolsa, qmo
  Cc: bpf, linux-kernel, chen.dylane

Add selftests for prog_kfunc feature probing.
 ./test_progs -t libbpf_probe_kfuncs
 #153     libbpf_probe_kfuncs:OK
 Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED

Signed-off-by: Tao Chen <chen.dylane@gmail.com>
---
 .../selftests/bpf/prog_tests/libbpf_probes.c  | 35 +++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c b/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
index 4ed46ed58a7b..d9d69941f694 100644
--- a/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
+++ b/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
@@ -126,3 +126,38 @@ void test_libbpf_probe_helpers(void)
 		ASSERT_EQ(res, d->supported, buf);
 	}
 }
+
+void test_libbpf_probe_kfuncs(void)
+{
+	int ret, kfunc_id;
+	char *kfunc = "bpf_cpumask_create";
+	struct btf *btf;
+
+	btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);
+	if (!ASSERT_OK_PTR(btf, "btf_parse"))
+		return;
+
+	kfunc_id = btf__find_by_name_kind(btf, kfunc, BTF_KIND_FUNC);
+	if (!ASSERT_GT(kfunc_id, 0, kfunc))
+		goto cleanup;
+
+	/* prog BPF_PROG_TYPE_SYSCALL supports kfunc bpf_cpumask_create */
+	ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_SYSCALL, kfunc_id, 0, NULL);
+	ASSERT_EQ(ret, 1, kfunc);
+
+	/* prog BPF_PROG_TYPE_KPROBE does not support kfunc bpf_cpumask_create */
+	ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_KPROBE, kfunc_id, 0, NULL);
+	ASSERT_EQ(ret, 0, kfunc);
+
+	/* invalid kfunc id */
+	ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_KPROBE, -1, 0, NULL);
+	ASSERT_EQ(ret, 0, "invalid kfunc id:-1");
+
+	/* invalid prog type */
+	ret = libbpf_probe_bpf_kfunc(100000, kfunc_id, 0, NULL);
+	if (!ASSERT_LE(ret, 0, "invalid prog type:100000"))
+		goto cleanup;
+
+cleanup:
+	btf__free(btf);
+}
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* Re: [PATCH bpf-next v3 1/3] libbpf: Refactor libbpf_probe_bpf_helper
  2025-01-24 14:44 ` [PATCH bpf-next v3 1/3] libbpf: Refactor libbpf_probe_bpf_helper Tao Chen
@ 2025-01-24 16:26   ` Jiri Olsa
  2025-01-25 14:52     ` Tao Chen
  2025-01-24 18:44   ` Andrii Nakryiko
  1 sibling, 1 reply; 15+ messages in thread
From: Jiri Olsa @ 2025-01-24 16:26 UTC (permalink / raw)
  To: Tao Chen; +Cc: ast, daniel, andrii, eddyz87, haoluo, qmo, bpf, linux-kernel

On Fri, Jan 24, 2025 at 10:44:09PM +0800, Tao Chen wrote:
> Extract the common part as probe_func_comm, which will be used in
> both libbpf_probe_bpf_{helper, kfunc}
> 
> Signed-off-by: Tao Chen <chen.dylane@gmail.com>
> ---
>  tools/lib/bpf/libbpf_probes.c | 38 ++++++++++++++++++++++++-----------
>  1 file changed, 26 insertions(+), 12 deletions(-)
> 
> diff --git a/tools/lib/bpf/libbpf_probes.c b/tools/lib/bpf/libbpf_probes.c
> index 9dfbe7750f56..b73345977b4e 100644
> --- a/tools/lib/bpf/libbpf_probes.c
> +++ b/tools/lib/bpf/libbpf_probes.c
> @@ -413,22 +413,20 @@ int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts)
>  	return libbpf_err(ret);
>  }
>  
> -int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, enum bpf_func_id helper_id,
> -			    const void *opts)
> +static int probe_func_comm(enum bpf_prog_type prog_type, struct bpf_insn insn,
> +			   char *accepted_msgs, size_t msgs_size)
>  {
>  	struct bpf_insn insns[] = {
> -		BPF_EMIT_CALL((__u32)helper_id),
> +		BPF_EXIT_INSN(),
>  		BPF_EXIT_INSN(),

I'd just keep above in libbpf_probe_bpf_helper and pass insns to probe_func_comm,
seems easier

jirka

>  	};
>  	const size_t insn_cnt = ARRAY_SIZE(insns);
> -	char buf[4096];
> -	int ret;
> +	int err;
>  
> -	if (opts)
> -		return libbpf_err(-EINVAL);
> +	insns[0] = insn;
>  
>  	/* we can't successfully load all prog types to check for BPF helper
> -	 * support, so bail out with -EOPNOTSUPP error
> +	 * and kfunc support, so bail out with -EOPNOTSUPP error
>  	 */
>  	switch (prog_type) {
>  	case BPF_PROG_TYPE_TRACING:
> @@ -440,10 +438,26 @@ int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, enum bpf_func_id helpe
>  		break;
>  	}
>  
> -	buf[0] = '\0';
> -	ret = probe_prog_load(prog_type, insns, insn_cnt, buf, sizeof(buf));
> -	if (ret < 0)
> -		return libbpf_err(ret);
> +	accepted_msgs[0] = '\0';
> +	err = probe_prog_load(prog_type, insns, insn_cnt, accepted_msgs, msgs_size);
> +	if (err < 0)
> +		return libbpf_err(err);
> +
> +	return 0;
> +}
> +
> +int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, enum bpf_func_id helper_id,
> +			    const void *opts)
> +{
> +	char buf[4096];
> +	int ret;
> +
> +	if (opts)
> +		return libbpf_err(-EINVAL);
> +
> +	ret = probe_func_comm(prog_type, BPF_EMIT_CALL((__u32)helper_id), buf, sizeof(buf));
> +	if (ret)
> +		return ret;
>  
>  	/* If BPF verifier doesn't recognize BPF helper ID (enum bpf_func_id)
>  	 * at all, it will emit something like "invalid func unknown#181".
> -- 
> 2.43.0
> 

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH bpf-next v3 2/3] libbpf: Add libbpf_probe_bpf_kfunc API
  2025-01-24 14:44 ` [PATCH bpf-next v3 2/3] libbpf: Add libbpf_probe_bpf_kfunc API Tao Chen
@ 2025-01-24 16:27   ` Jiri Olsa
  2025-01-25 14:54     ` Tao Chen
  2025-01-24 18:46   ` Andrii Nakryiko
  1 sibling, 1 reply; 15+ messages in thread
From: Jiri Olsa @ 2025-01-24 16:27 UTC (permalink / raw)
  To: Tao Chen; +Cc: ast, daniel, andrii, eddyz87, haoluo, qmo, bpf, linux-kernel

On Fri, Jan 24, 2025 at 10:44:10PM +0800, Tao Chen wrote:
> Similarly to libbpf_probe_bpf_helper, the libbpf_probe_bpf_kfunc
> used to test the availability of the different eBPF kfuncs on the
> current system.
> 
> Signed-off-by: Tao Chen <chen.dylane@gmail.com>
> ---
>  tools/lib/bpf/libbpf.h        | 17 ++++++++++++++++-
>  tools/lib/bpf/libbpf.map      |  1 +
>  tools/lib/bpf/libbpf_probes.c | 30 ++++++++++++++++++++++++++++++
>  3 files changed, 47 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
> index 3020ee45303a..035829e22099 100644
> --- a/tools/lib/bpf/libbpf.h
> +++ b/tools/lib/bpf/libbpf.h
> @@ -1680,7 +1680,22 @@ LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void
>   */
>  LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type,
>  				       enum bpf_func_id helper_id, const void *opts);
> -
> +/**
> + * @brief **libbpf_probe_bpf_kfunc()** detects if host kernel supports the
> + * use of a given BPF kfunc from specified BPF program type.
> + * @param prog_type BPF program type used to check the support of BPF kfunc
> + * @param kfunc_id The btf ID of BPF kfunc to check support for
> + * @param btf_fd The module BTF FD, 0 for vmlinux
> + * @param opts reserved for future extensibility, should be NULL
> + * @return 1, if given combination of program type and kfunc is supported; 0,
> + * if the combination is not supported; negative error code if feature
> + * detection for provided input arguments failed or can't be performed
> + *
> + * Make sure the process has required set of CAP_* permissions (or runs as
> + * root) when performing feature checking.
> + */
> +LIBBPF_API int libbpf_probe_bpf_kfunc(enum bpf_prog_type prog_type,
> +				      int kfunc_id, __s16 btf_fd, const void *opts);
>  /**
>   * @brief **libbpf_num_possible_cpus()** is a helper function to get the
>   * number of possible CPUs that the host kernel supports and expects.
> diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
> index a8b2936a1646..e93fae101efd 100644
> --- a/tools/lib/bpf/libbpf.map
> +++ b/tools/lib/bpf/libbpf.map
> @@ -436,4 +436,5 @@ LIBBPF_1.6.0 {
>  		bpf_linker__add_buf;
>  		bpf_linker__add_fd;
>  		bpf_linker__new_fd;
> +		libbpf_probe_bpf_kfunc;
>  } LIBBPF_1.5.0;
> diff --git a/tools/lib/bpf/libbpf_probes.c b/tools/lib/bpf/libbpf_probes.c
> index b73345977b4e..cd7d16c1cc49 100644
> --- a/tools/lib/bpf/libbpf_probes.c
> +++ b/tools/lib/bpf/libbpf_probes.c
> @@ -446,6 +446,36 @@ static int probe_func_comm(enum bpf_prog_type prog_type, struct bpf_insn insn,
>  	return 0;
>  }
>  
> +int libbpf_probe_bpf_kfunc(enum bpf_prog_type prog_type, int kfunc_id,
> +			   __s16 btf_fd, const void *opts)
> +{
> +	struct bpf_insn insn;
> +	int err;
> +	char buf[4096];
> +
> +	if (opts)
> +		return libbpf_err(-EINVAL);
> +
> +	insn.code = BPF_JMP | BPF_CALL;
> +	insn.src_reg = BPF_PSEUDO_KFUNC_CALL;
> +	insn.imm = kfunc_id;
> +	insn.off = btf_fd;

nit, you could use

        struct bpf_insn insns[] = {
        	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, off, imm),
                BPF_EXIT_INSN(),
        };

jirka

> +
> +	err = probe_func_comm(prog_type, insn, buf, sizeof(buf));
> +	if (err)
> +		return err;
> +
> +	/* If BPF verifier recognizes BPF kfunc but it's not supported for
> +	 * given BPF program type, it will emit "calling kernel function
> +	 * bpf_cpumask_create is not allowed", if the kfunc id is invalid,
> +	 * it will emit "kernel btf_id 4294967295 is not a function".
> +	 */
> +	if (err == 0 && (strstr(buf, "not allowed") || strstr(buf, "not a function")))
> +		return 0;
> +
> +	return 1; /* assume supported */
> +}
> +
>  int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, enum bpf_func_id helper_id,
>  			    const void *opts)
>  {
> -- 
> 2.43.0
> 

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH bpf-next v3 3/3] selftests/bpf: Add libbpf_probe_bpf_kfunc API selftests
  2025-01-24 14:44 ` [PATCH bpf-next v3 3/3] selftests/bpf: Add libbpf_probe_bpf_kfunc API selftests Tao Chen
@ 2025-01-24 16:27   ` Jiri Olsa
  2025-01-25 14:56     ` Tao Chen
  2025-01-24 18:48   ` Andrii Nakryiko
  1 sibling, 1 reply; 15+ messages in thread
From: Jiri Olsa @ 2025-01-24 16:27 UTC (permalink / raw)
  To: Tao Chen; +Cc: ast, daniel, andrii, eddyz87, haoluo, qmo, bpf, linux-kernel

On Fri, Jan 24, 2025 at 10:44:11PM +0800, Tao Chen wrote:
> Add selftests for prog_kfunc feature probing.
>  ./test_progs -t libbpf_probe_kfuncs
>  #153     libbpf_probe_kfuncs:OK
>  Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED
> 
> Signed-off-by: Tao Chen <chen.dylane@gmail.com>
> ---
>  .../selftests/bpf/prog_tests/libbpf_probes.c  | 35 +++++++++++++++++++
>  1 file changed, 35 insertions(+)
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c b/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
> index 4ed46ed58a7b..d9d69941f694 100644
> --- a/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
> +++ b/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
> @@ -126,3 +126,38 @@ void test_libbpf_probe_helpers(void)
>  		ASSERT_EQ(res, d->supported, buf);
>  	}
>  }
> +
> +void test_libbpf_probe_kfuncs(void)
> +{
> +	int ret, kfunc_id;
> +	char *kfunc = "bpf_cpumask_create";
> +	struct btf *btf;
> +
> +	btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);
> +	if (!ASSERT_OK_PTR(btf, "btf_parse"))
> +		return;
> +
> +	kfunc_id = btf__find_by_name_kind(btf, kfunc, BTF_KIND_FUNC);
> +	if (!ASSERT_GT(kfunc_id, 0, kfunc))
> +		goto cleanup;
> +
> +	/* prog BPF_PROG_TYPE_SYSCALL supports kfunc bpf_cpumask_create */
> +	ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_SYSCALL, kfunc_id, 0, NULL);
> +	ASSERT_EQ(ret, 1, kfunc);
> +
> +	/* prog BPF_PROG_TYPE_KPROBE does not support kfunc bpf_cpumask_create */
> +	ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_KPROBE, kfunc_id, 0, NULL);
> +	ASSERT_EQ(ret, 0, kfunc);
> +
> +	/* invalid kfunc id */
> +	ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_KPROBE, -1, 0, NULL);
> +	ASSERT_EQ(ret, 0, "invalid kfunc id:-1");
> +
> +	/* invalid prog type */
> +	ret = libbpf_probe_bpf_kfunc(100000, kfunc_id, 0, NULL);
> +	if (!ASSERT_LE(ret, 0, "invalid prog type:100000"))
> +		goto cleanup;

nit no need for the goto

jirka

> +
> +cleanup:
> +	btf__free(btf);
> +}
> -- 
> 2.43.0
> 

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH bpf-next v3 1/3] libbpf: Refactor libbpf_probe_bpf_helper
  2025-01-24 14:44 ` [PATCH bpf-next v3 1/3] libbpf: Refactor libbpf_probe_bpf_helper Tao Chen
  2025-01-24 16:26   ` Jiri Olsa
@ 2025-01-24 18:44   ` Andrii Nakryiko
  2025-01-25 15:05     ` Tao Chen
  1 sibling, 1 reply; 15+ messages in thread
From: Andrii Nakryiko @ 2025-01-24 18:44 UTC (permalink / raw)
  To: Tao Chen
  Cc: ast, daniel, andrii, eddyz87, haoluo, jolsa, qmo, bpf,
	linux-kernel

On Fri, Jan 24, 2025 at 6:44 AM Tao Chen <chen.dylane@gmail.com> wrote:
>
> Extract the common part as probe_func_comm, which will be used in
> both libbpf_probe_bpf_{helper, kfunc}
>
> Signed-off-by: Tao Chen <chen.dylane@gmail.com>
> ---
>  tools/lib/bpf/libbpf_probes.c | 38 ++++++++++++++++++++++++-----------
>  1 file changed, 26 insertions(+), 12 deletions(-)
>
> diff --git a/tools/lib/bpf/libbpf_probes.c b/tools/lib/bpf/libbpf_probes.c
> index 9dfbe7750f56..b73345977b4e 100644
> --- a/tools/lib/bpf/libbpf_probes.c
> +++ b/tools/lib/bpf/libbpf_probes.c
> @@ -413,22 +413,20 @@ int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts)
>         return libbpf_err(ret);
>  }
>
> -int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, enum bpf_func_id helper_id,
> -                           const void *opts)
> +static int probe_func_comm(enum bpf_prog_type prog_type, struct bpf_insn insn,
> +                          char *accepted_msgs, size_t msgs_size)
>  {
>         struct bpf_insn insns[] = {
> -               BPF_EMIT_CALL((__u32)helper_id),
> +               BPF_EXIT_INSN(),
>                 BPF_EXIT_INSN(),
>         };
>         const size_t insn_cnt = ARRAY_SIZE(insns);
> -       char buf[4096];
> -       int ret;
> +       int err;
>
> -       if (opts)
> -               return libbpf_err(-EINVAL);
> +       insns[0] = insn;
>
>         /* we can't successfully load all prog types to check for BPF helper
> -        * support, so bail out with -EOPNOTSUPP error
> +        * and kfunc support, so bail out with -EOPNOTSUPP error
>          */
>         switch (prog_type) {
>         case BPF_PROG_TYPE_TRACING:

there isn't much logic that you will extract here besides this check
whether program type can even be successfully loaded, so I wouldn't
extract probe_func_comm(), but rather extract just the check:

static bool can_probe_prog_type(enum bpf_prog_type prog_type)
{
        /* we can't successfully load all prog types to check for BPF
helper/kfunc
         * support, so check this early and bail
         */
        switch (prog_type) {
            ...: return false
        default:
            return true;
}


And just check that can_probe_prog_type() inside
libbpf_probe_bpf_helper and libbpf_probe_bpf_kfunc

pw-bot: cr

> @@ -440,10 +438,26 @@ int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, enum bpf_func_id helpe
>                 break;
>         }
>
> -       buf[0] = '\0';
> -       ret = probe_prog_load(prog_type, insns, insn_cnt, buf, sizeof(buf));
> -       if (ret < 0)
> -               return libbpf_err(ret);
> +       accepted_msgs[0] = '\0';
> +       err = probe_prog_load(prog_type, insns, insn_cnt, accepted_msgs, msgs_size);
> +       if (err < 0)
> +               return libbpf_err(err);
> +
> +       return 0;
> +}
> +
> +int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, enum bpf_func_id helper_id,
> +                           const void *opts)
> +{
> +       char buf[4096];
> +       int ret;
> +
> +       if (opts)
> +               return libbpf_err(-EINVAL);
> +
> +       ret = probe_func_comm(prog_type, BPF_EMIT_CALL((__u32)helper_id), buf, sizeof(buf));
> +       if (ret)
> +               return ret;
>
>         /* If BPF verifier doesn't recognize BPF helper ID (enum bpf_func_id)
>          * at all, it will emit something like "invalid func unknown#181".
> --
> 2.43.0
>

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH bpf-next v3 2/3] libbpf: Add libbpf_probe_bpf_kfunc API
  2025-01-24 14:44 ` [PATCH bpf-next v3 2/3] libbpf: Add libbpf_probe_bpf_kfunc API Tao Chen
  2025-01-24 16:27   ` Jiri Olsa
@ 2025-01-24 18:46   ` Andrii Nakryiko
  1 sibling, 0 replies; 15+ messages in thread
From: Andrii Nakryiko @ 2025-01-24 18:46 UTC (permalink / raw)
  To: Tao Chen
  Cc: ast, daniel, andrii, eddyz87, haoluo, jolsa, qmo, bpf,
	linux-kernel

On Fri, Jan 24, 2025 at 6:44 AM Tao Chen <chen.dylane@gmail.com> wrote:
>
> Similarly to libbpf_probe_bpf_helper, the libbpf_probe_bpf_kfunc
> used to test the availability of the different eBPF kfuncs on the
> current system.
>
> Signed-off-by: Tao Chen <chen.dylane@gmail.com>
> ---
>  tools/lib/bpf/libbpf.h        | 17 ++++++++++++++++-
>  tools/lib/bpf/libbpf.map      |  1 +
>  tools/lib/bpf/libbpf_probes.c | 30 ++++++++++++++++++++++++++++++
>  3 files changed, 47 insertions(+), 1 deletion(-)
>
> diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
> index 3020ee45303a..035829e22099 100644
> --- a/tools/lib/bpf/libbpf.h
> +++ b/tools/lib/bpf/libbpf.h
> @@ -1680,7 +1680,22 @@ LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void
>   */
>  LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type,
>                                        enum bpf_func_id helper_id, const void *opts);
> -
> +/**
> + * @brief **libbpf_probe_bpf_kfunc()** detects if host kernel supports the
> + * use of a given BPF kfunc from specified BPF program type.
> + * @param prog_type BPF program type used to check the support of BPF kfunc
> + * @param kfunc_id The btf ID of BPF kfunc to check support for
> + * @param btf_fd The module BTF FD, 0 for vmlinux
> + * @param opts reserved for future extensibility, should be NULL
> + * @return 1, if given combination of program type and kfunc is supported; 0,
> + * if the combination is not supported; negative error code if feature
> + * detection for provided input arguments failed or can't be performed
> + *
> + * Make sure the process has required set of CAP_* permissions (or runs as
> + * root) when performing feature checking.
> + */
> +LIBBPF_API int libbpf_probe_bpf_kfunc(enum bpf_prog_type prog_type,
> +                                     int kfunc_id, __s16 btf_fd, const void *opts);
>  /**
>   * @brief **libbpf_num_possible_cpus()** is a helper function to get the
>   * number of possible CPUs that the host kernel supports and expects.
> diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
> index a8b2936a1646..e93fae101efd 100644
> --- a/tools/lib/bpf/libbpf.map
> +++ b/tools/lib/bpf/libbpf.map
> @@ -436,4 +436,5 @@ LIBBPF_1.6.0 {
>                 bpf_linker__add_buf;
>                 bpf_linker__add_fd;
>                 bpf_linker__new_fd;
> +               libbpf_probe_bpf_kfunc;
>  } LIBBPF_1.5.0;
> diff --git a/tools/lib/bpf/libbpf_probes.c b/tools/lib/bpf/libbpf_probes.c
> index b73345977b4e..cd7d16c1cc49 100644
> --- a/tools/lib/bpf/libbpf_probes.c
> +++ b/tools/lib/bpf/libbpf_probes.c
> @@ -446,6 +446,36 @@ static int probe_func_comm(enum bpf_prog_type prog_type, struct bpf_insn insn,
>         return 0;
>  }
>
> +int libbpf_probe_bpf_kfunc(enum bpf_prog_type prog_type, int kfunc_id,
> +                          __s16 btf_fd, const void *opts)

btf_fd should be int (and mention in documentation that if kfunc is
defined in kernel module btf_fd is used to point to module's BTF)

> +{
> +       struct bpf_insn insn;
> +       int err;
> +       char buf[4096];
> +
> +       if (opts)
> +               return libbpf_err(-EINVAL);
> +
> +       insn.code = BPF_JMP | BPF_CALL;
> +       insn.src_reg = BPF_PSEUDO_KFUNC_CALL;
> +       insn.imm = kfunc_id;
> +       insn.off = btf_fd;
> +
> +       err = probe_func_comm(prog_type, insn, buf, sizeof(buf));
> +       if (err)
> +               return err;
> +
> +       /* If BPF verifier recognizes BPF kfunc but it's not supported for
> +        * given BPF program type, it will emit "calling kernel function
> +        * bpf_cpumask_create is not allowed", if the kfunc id is invalid,
> +        * it will emit "kernel btf_id 4294967295 is not a function".
> +        */
> +       if (err == 0 && (strstr(buf, "not allowed") || strstr(buf, "not a function")))
> +               return 0;
> +
> +       return 1; /* assume supported */
> +}
> +
>  int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, enum bpf_func_id helper_id,
>                             const void *opts)
>  {
> --
> 2.43.0
>

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH bpf-next v3 3/3] selftests/bpf: Add libbpf_probe_bpf_kfunc API selftests
  2025-01-24 14:44 ` [PATCH bpf-next v3 3/3] selftests/bpf: Add libbpf_probe_bpf_kfunc API selftests Tao Chen
  2025-01-24 16:27   ` Jiri Olsa
@ 2025-01-24 18:48   ` Andrii Nakryiko
  2025-01-25 15:07     ` Tao Chen
  1 sibling, 1 reply; 15+ messages in thread
From: Andrii Nakryiko @ 2025-01-24 18:48 UTC (permalink / raw)
  To: Tao Chen
  Cc: ast, daniel, andrii, eddyz87, haoluo, jolsa, qmo, bpf,
	linux-kernel

On Fri, Jan 24, 2025 at 6:44 AM Tao Chen <chen.dylane@gmail.com> wrote:
>
> Add selftests for prog_kfunc feature probing.
>  ./test_progs -t libbpf_probe_kfuncs
>  #153     libbpf_probe_kfuncs:OK
>  Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED
>
> Signed-off-by: Tao Chen <chen.dylane@gmail.com>
> ---
>  .../selftests/bpf/prog_tests/libbpf_probes.c  | 35 +++++++++++++++++++
>  1 file changed, 35 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c b/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
> index 4ed46ed58a7b..d9d69941f694 100644
> --- a/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
> +++ b/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
> @@ -126,3 +126,38 @@ void test_libbpf_probe_helpers(void)
>                 ASSERT_EQ(res, d->supported, buf);
>         }
>  }
> +
> +void test_libbpf_probe_kfuncs(void)
> +{
> +       int ret, kfunc_id;
> +       char *kfunc = "bpf_cpumask_create";
> +       struct btf *btf;
> +
> +       btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);
> +       if (!ASSERT_OK_PTR(btf, "btf_parse"))
> +               return;
> +
> +       kfunc_id = btf__find_by_name_kind(btf, kfunc, BTF_KIND_FUNC);
> +       if (!ASSERT_GT(kfunc_id, 0, kfunc))
> +               goto cleanup;
> +
> +       /* prog BPF_PROG_TYPE_SYSCALL supports kfunc bpf_cpumask_create */
> +       ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_SYSCALL, kfunc_id, 0, NULL);
> +       ASSERT_EQ(ret, 1, kfunc);
> +
> +       /* prog BPF_PROG_TYPE_KPROBE does not support kfunc bpf_cpumask_create */
> +       ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_KPROBE, kfunc_id, 0, NULL);
> +       ASSERT_EQ(ret, 0, kfunc);
> +
> +       /* invalid kfunc id */
> +       ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_KPROBE, -1, 0, NULL);
> +       ASSERT_EQ(ret, 0, "invalid kfunc id:-1");
> +
> +       /* invalid prog type */
> +       ret = libbpf_probe_bpf_kfunc(100000, kfunc_id, 0, NULL);
> +       if (!ASSERT_LE(ret, 0, "invalid prog type:100000"))

we have ASSERT_ERR(), wouldn't it work here?


let's also add a test for kfunc in module (we have bpf_testmod, we
should be able to test something out of there)

> +               goto cleanup;
> +
> +cleanup:
> +       btf__free(btf);
> +}
> --
> 2.43.0
>

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH bpf-next v3 1/3] libbpf: Refactor libbpf_probe_bpf_helper
  2025-01-24 16:26   ` Jiri Olsa
@ 2025-01-25 14:52     ` Tao Chen
  0 siblings, 0 replies; 15+ messages in thread
From: Tao Chen @ 2025-01-25 14:52 UTC (permalink / raw)
  To: Jiri Olsa; +Cc: ast, daniel, andrii, eddyz87, haoluo, qmo, bpf, linux-kernel

在 2025/1/25 00:26, Jiri Olsa 写道:
> On Fri, Jan 24, 2025 at 10:44:09PM +0800, Tao Chen wrote:
>> Extract the common part as probe_func_comm, which will be used in
>> both libbpf_probe_bpf_{helper, kfunc}
>>
>> Signed-off-by: Tao Chen <chen.dylane@gmail.com>
>> ---
>>   tools/lib/bpf/libbpf_probes.c | 38 ++++++++++++++++++++++++-----------
>>   1 file changed, 26 insertions(+), 12 deletions(-)
>>
>> diff --git a/tools/lib/bpf/libbpf_probes.c b/tools/lib/bpf/libbpf_probes.c
>> index 9dfbe7750f56..b73345977b4e 100644
>> --- a/tools/lib/bpf/libbpf_probes.c
>> +++ b/tools/lib/bpf/libbpf_probes.c
>> @@ -413,22 +413,20 @@ int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts)
>>   	return libbpf_err(ret);
>>   }
>>   
>> -int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, enum bpf_func_id helper_id,
>> -			    const void *opts)
>> +static int probe_func_comm(enum bpf_prog_type prog_type, struct bpf_insn insn,
>> +			   char *accepted_msgs, size_t msgs_size)
>>   {
>>   	struct bpf_insn insns[] = {
>> -		BPF_EMIT_CALL((__u32)helper_id),
>> +		BPF_EXIT_INSN(),
>>   		BPF_EXIT_INSN(),
> 
> I'd just keep above in libbpf_probe_bpf_helper and pass insns to probe_func_comm,
> seems easier
> 
> jirka
> 

Hi jiri,
Thank you for your review, your suggestion seems better, i will
send it in v4.

>>   	};
>>   	const size_t insn_cnt = ARRAY_SIZE(insns);
>> -	char buf[4096];
>> -	int ret;
>> +	int err;
>>   
>> -	if (opts)
>> -		return libbpf_err(-EINVAL);
>> +	insns[0] = insn;
>>   
>>   	/* we can't successfully load all prog types to check for BPF helper
>> -	 * support, so bail out with -EOPNOTSUPP error
>> +	 * and kfunc support, so bail out with -EOPNOTSUPP error
>>   	 */
>>   	switch (prog_type) {
>>   	case BPF_PROG_TYPE_TRACING:
>> @@ -440,10 +438,26 @@ int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, enum bpf_func_id helpe
>>   		break;
>>   	}
>>   
>> -	buf[0] = '\0';
>> -	ret = probe_prog_load(prog_type, insns, insn_cnt, buf, sizeof(buf));
>> -	if (ret < 0)
>> -		return libbpf_err(ret);
>> +	accepted_msgs[0] = '\0';
>> +	err = probe_prog_load(prog_type, insns, insn_cnt, accepted_msgs, msgs_size);
>> +	if (err < 0)
>> +		return libbpf_err(err);
>> +
>> +	return 0;
>> +}
>> +
>> +int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, enum bpf_func_id helper_id,
>> +			    const void *opts)
>> +{
>> +	char buf[4096];
>> +	int ret;
>> +
>> +	if (opts)
>> +		return libbpf_err(-EINVAL);
>> +
>> +	ret = probe_func_comm(prog_type, BPF_EMIT_CALL((__u32)helper_id), buf, sizeof(buf));
>> +	if (ret)
>> +		return ret;
>>   
>>   	/* If BPF verifier doesn't recognize BPF helper ID (enum bpf_func_id)
>>   	 * at all, it will emit something like "invalid func unknown#181".
>> -- 
>> 2.43.0
>>


-- 
Best Regards
Dylane Chen

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH bpf-next v3 2/3] libbpf: Add libbpf_probe_bpf_kfunc API
  2025-01-24 16:27   ` Jiri Olsa
@ 2025-01-25 14:54     ` Tao Chen
  0 siblings, 0 replies; 15+ messages in thread
From: Tao Chen @ 2025-01-25 14:54 UTC (permalink / raw)
  To: Jiri Olsa; +Cc: ast, daniel, andrii, eddyz87, haoluo, qmo, bpf, linux-kernel

在 2025/1/25 00:27, Jiri Olsa 写道:
> On Fri, Jan 24, 2025 at 10:44:10PM +0800, Tao Chen wrote:
>> Similarly to libbpf_probe_bpf_helper, the libbpf_probe_bpf_kfunc
>> used to test the availability of the different eBPF kfuncs on the
>> current system.
>>
>> Signed-off-by: Tao Chen <chen.dylane@gmail.com>
>> ---
>>   tools/lib/bpf/libbpf.h        | 17 ++++++++++++++++-
>>   tools/lib/bpf/libbpf.map      |  1 +
>>   tools/lib/bpf/libbpf_probes.c | 30 ++++++++++++++++++++++++++++++
>>   3 files changed, 47 insertions(+), 1 deletion(-)
>>
>> diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
>> index 3020ee45303a..035829e22099 100644
>> --- a/tools/lib/bpf/libbpf.h
>> +++ b/tools/lib/bpf/libbpf.h
>> @@ -1680,7 +1680,22 @@ LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void
>>    */
>>   LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type,
>>   				       enum bpf_func_id helper_id, const void *opts);
>> -
>> +/**
>> + * @brief **libbpf_probe_bpf_kfunc()** detects if host kernel supports the
>> + * use of a given BPF kfunc from specified BPF program type.
>> + * @param prog_type BPF program type used to check the support of BPF kfunc
>> + * @param kfunc_id The btf ID of BPF kfunc to check support for
>> + * @param btf_fd The module BTF FD, 0 for vmlinux
>> + * @param opts reserved for future extensibility, should be NULL
>> + * @return 1, if given combination of program type and kfunc is supported; 0,
>> + * if the combination is not supported; negative error code if feature
>> + * detection for provided input arguments failed or can't be performed
>> + *
>> + * Make sure the process has required set of CAP_* permissions (or runs as
>> + * root) when performing feature checking.
>> + */
>> +LIBBPF_API int libbpf_probe_bpf_kfunc(enum bpf_prog_type prog_type,
>> +				      int kfunc_id, __s16 btf_fd, const void *opts);
>>   /**
>>    * @brief **libbpf_num_possible_cpus()** is a helper function to get the
>>    * number of possible CPUs that the host kernel supports and expects.
>> diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
>> index a8b2936a1646..e93fae101efd 100644
>> --- a/tools/lib/bpf/libbpf.map
>> +++ b/tools/lib/bpf/libbpf.map
>> @@ -436,4 +436,5 @@ LIBBPF_1.6.0 {
>>   		bpf_linker__add_buf;
>>   		bpf_linker__add_fd;
>>   		bpf_linker__new_fd;
>> +		libbpf_probe_bpf_kfunc;
>>   } LIBBPF_1.5.0;
>> diff --git a/tools/lib/bpf/libbpf_probes.c b/tools/lib/bpf/libbpf_probes.c
>> index b73345977b4e..cd7d16c1cc49 100644
>> --- a/tools/lib/bpf/libbpf_probes.c
>> +++ b/tools/lib/bpf/libbpf_probes.c
>> @@ -446,6 +446,36 @@ static int probe_func_comm(enum bpf_prog_type prog_type, struct bpf_insn insn,
>>   	return 0;
>>   }
>>   
>> +int libbpf_probe_bpf_kfunc(enum bpf_prog_type prog_type, int kfunc_id,
>> +			   __s16 btf_fd, const void *opts)
>> +{
>> +	struct bpf_insn insn;
>> +	int err;
>> +	char buf[4096];
>> +
>> +	if (opts)
>> +		return libbpf_err(-EINVAL);
>> +
>> +	insn.code = BPF_JMP | BPF_CALL;
>> +	insn.src_reg = BPF_PSEUDO_KFUNC_CALL;
>> +	insn.imm = kfunc_id;
>> +	insn.off = btf_fd;
> 
> nit, you could use
> 
>          struct bpf_insn insns[] = {
>          	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, off, imm),
>                  BPF_EXIT_INSN(),
>          };
> 
> jirka
> 

Yeah, it looks more concise, i will send it in v4. Thanks.

>> +
>> +	err = probe_func_comm(prog_type, insn, buf, sizeof(buf));
>> +	if (err)
>> +		return err;
>> +
>> +	/* If BPF verifier recognizes BPF kfunc but it's not supported for
>> +	 * given BPF program type, it will emit "calling kernel function
>> +	 * bpf_cpumask_create is not allowed", if the kfunc id is invalid,
>> +	 * it will emit "kernel btf_id 4294967295 is not a function".
>> +	 */
>> +	if (err == 0 && (strstr(buf, "not allowed") || strstr(buf, "not a function")))
>> +		return 0;
>> +
>> +	return 1; /* assume supported */
>> +}
>> +
>>   int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, enum bpf_func_id helper_id,
>>   			    const void *opts)
>>   {
>> -- 
>> 2.43.0
>>


-- 
Best Regards
Dylane Chen

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH bpf-next v3 3/3] selftests/bpf: Add libbpf_probe_bpf_kfunc API selftests
  2025-01-24 16:27   ` Jiri Olsa
@ 2025-01-25 14:56     ` Tao Chen
  0 siblings, 0 replies; 15+ messages in thread
From: Tao Chen @ 2025-01-25 14:56 UTC (permalink / raw)
  To: Jiri Olsa; +Cc: ast, daniel, andrii, eddyz87, haoluo, qmo, bpf, linux-kernel

在 2025/1/25 00:27, Jiri Olsa 写道:
> On Fri, Jan 24, 2025 at 10:44:11PM +0800, Tao Chen wrote:
>> Add selftests for prog_kfunc feature probing.
>>   ./test_progs -t libbpf_probe_kfuncs
>>   #153     libbpf_probe_kfuncs:OK
>>   Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED
>>
>> Signed-off-by: Tao Chen <chen.dylane@gmail.com>
>> ---
>>   .../selftests/bpf/prog_tests/libbpf_probes.c  | 35 +++++++++++++++++++
>>   1 file changed, 35 insertions(+)
>>
>> diff --git a/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c b/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
>> index 4ed46ed58a7b..d9d69941f694 100644
>> --- a/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
>> +++ b/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
>> @@ -126,3 +126,38 @@ void test_libbpf_probe_helpers(void)
>>   		ASSERT_EQ(res, d->supported, buf);
>>   	}
>>   }
>> +
>> +void test_libbpf_probe_kfuncs(void)
>> +{
>> +	int ret, kfunc_id;
>> +	char *kfunc = "bpf_cpumask_create";
>> +	struct btf *btf;
>> +
>> +	btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);
>> +	if (!ASSERT_OK_PTR(btf, "btf_parse"))
>> +		return;
>> +
>> +	kfunc_id = btf__find_by_name_kind(btf, kfunc, BTF_KIND_FUNC);
>> +	if (!ASSERT_GT(kfunc_id, 0, kfunc))
>> +		goto cleanup;
>> +
>> +	/* prog BPF_PROG_TYPE_SYSCALL supports kfunc bpf_cpumask_create */
>> +	ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_SYSCALL, kfunc_id, 0, NULL);
>> +	ASSERT_EQ(ret, 1, kfunc);
>> +
>> +	/* prog BPF_PROG_TYPE_KPROBE does not support kfunc bpf_cpumask_create */
>> +	ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_KPROBE, kfunc_id, 0, NULL);
>> +	ASSERT_EQ(ret, 0, kfunc);
>> +
>> +	/* invalid kfunc id */
>> +	ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_KPROBE, -1, 0, NULL);
>> +	ASSERT_EQ(ret, 0, "invalid kfunc id:-1");
>> +
>> +	/* invalid prog type */
>> +	ret = libbpf_probe_bpf_kfunc(100000, kfunc_id, 0, NULL);
>> +	if (!ASSERT_LE(ret, 0, "invalid prog type:100000"))
>> +		goto cleanup;
> 
> nit no need for the goto
> 
> jirka
> 

Ack. Will fix.

>> +
>> +cleanup:
>> +	btf__free(btf);
>> +}
>> -- 
>> 2.43.0
>>


-- 
Best Regards
Dylane Chen

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH bpf-next v3 1/3] libbpf: Refactor libbpf_probe_bpf_helper
  2025-01-24 18:44   ` Andrii Nakryiko
@ 2025-01-25 15:05     ` Tao Chen
  0 siblings, 0 replies; 15+ messages in thread
From: Tao Chen @ 2025-01-25 15:05 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: ast, daniel, andrii, eddyz87, haoluo, jolsa, qmo, bpf,
	linux-kernel

在 2025/1/25 02:44, Andrii Nakryiko 写道:
> On Fri, Jan 24, 2025 at 6:44 AM Tao Chen <chen.dylane@gmail.com> wrote:
>>
>> Extract the common part as probe_func_comm, which will be used in
>> both libbpf_probe_bpf_{helper, kfunc}
>>
>> Signed-off-by: Tao Chen <chen.dylane@gmail.com>
>> ---
>>   tools/lib/bpf/libbpf_probes.c | 38 ++++++++++++++++++++++++-----------
>>   1 file changed, 26 insertions(+), 12 deletions(-)
>>
>> diff --git a/tools/lib/bpf/libbpf_probes.c b/tools/lib/bpf/libbpf_probes.c
>> index 9dfbe7750f56..b73345977b4e 100644
>> --- a/tools/lib/bpf/libbpf_probes.c
>> +++ b/tools/lib/bpf/libbpf_probes.c
>> @@ -413,22 +413,20 @@ int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts)
>>          return libbpf_err(ret);
>>   }
>>
>> -int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, enum bpf_func_id helper_id,
>> -                           const void *opts)
>> +static int probe_func_comm(enum bpf_prog_type prog_type, struct bpf_insn insn,
>> +                          char *accepted_msgs, size_t msgs_size)
>>   {
>>          struct bpf_insn insns[] = {
>> -               BPF_EMIT_CALL((__u32)helper_id),
>> +               BPF_EXIT_INSN(),
>>                  BPF_EXIT_INSN(),
>>          };
>>          const size_t insn_cnt = ARRAY_SIZE(insns);
>> -       char buf[4096];
>> -       int ret;
>> +       int err;
>>
>> -       if (opts)
>> -               return libbpf_err(-EINVAL);
>> +       insns[0] = insn;
>>
>>          /* we can't successfully load all prog types to check for BPF helper
>> -        * support, so bail out with -EOPNOTSUPP error
>> +        * and kfunc support, so bail out with -EOPNOTSUPP error
>>           */
>>          switch (prog_type) {
>>          case BPF_PROG_TYPE_TRACING:
> 
> there isn't much logic that you will extract here besides this check
> whether program type can even be successfully loaded, so I wouldn't
> extract probe_func_comm(), but rather extract just the check:
> 
> static bool can_probe_prog_type(enum bpf_prog_type prog_type)
> {
>          /* we can't successfully load all prog types to check for BPF
> helper/kfunc
>           * support, so check this early and bail
>           */
>          switch (prog_type) {
>              ...: return false
>          default:
>              return true;
> }
> 
> 
> And just check that can_probe_prog_type() inside
> libbpf_probe_bpf_helper and libbpf_probe_bpf_kfunc
> 
> pw-bot: cr
> 

Hi Andrii,
Thank you for your review, jiri also suggested putting the insn part 
back into libbpf_bpf_probe_{helper, kfunc}, so I'll make the 
modifications as you suggested in v4.

>> @@ -440,10 +438,26 @@ int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, enum bpf_func_id helpe
>>                  break;
>>          }
>>
>> -       buf[0] = '\0';
>> -       ret = probe_prog_load(prog_type, insns, insn_cnt, buf, sizeof(buf));
>> -       if (ret < 0)
>> -               return libbpf_err(ret);
>> +       accepted_msgs[0] = '\0';
>> +       err = probe_prog_load(prog_type, insns, insn_cnt, accepted_msgs, msgs_size);
>> +       if (err < 0)
>> +               return libbpf_err(err);
>> +
>> +       return 0;
>> +}
>> +
>> +int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, enum bpf_func_id helper_id,
>> +                           const void *opts)
>> +{
>> +       char buf[4096];
>> +       int ret;
>> +
>> +       if (opts)
>> +               return libbpf_err(-EINVAL);
>> +
>> +       ret = probe_func_comm(prog_type, BPF_EMIT_CALL((__u32)helper_id), buf, sizeof(buf));
>> +       if (ret)
>> +               return ret;
>>
>>          /* If BPF verifier doesn't recognize BPF helper ID (enum bpf_func_id)
>>           * at all, it will emit something like "invalid func unknown#181".
>> --
>> 2.43.0
>>


-- 
Best Regards
Dylane Chen

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH bpf-next v3 3/3] selftests/bpf: Add libbpf_probe_bpf_kfunc API selftests
  2025-01-24 18:48   ` Andrii Nakryiko
@ 2025-01-25 15:07     ` Tao Chen
  0 siblings, 0 replies; 15+ messages in thread
From: Tao Chen @ 2025-01-25 15:07 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: ast, daniel, andrii, eddyz87, haoluo, jolsa, qmo, bpf,
	linux-kernel

在 2025/1/25 02:48, Andrii Nakryiko 写道:
> On Fri, Jan 24, 2025 at 6:44 AM Tao Chen <chen.dylane@gmail.com> wrote:
>>
>> Add selftests for prog_kfunc feature probing.
>>   ./test_progs -t libbpf_probe_kfuncs
>>   #153     libbpf_probe_kfuncs:OK
>>   Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED
>>
>> Signed-off-by: Tao Chen <chen.dylane@gmail.com>
>> ---
>>   .../selftests/bpf/prog_tests/libbpf_probes.c  | 35 +++++++++++++++++++
>>   1 file changed, 35 insertions(+)
>>
>> diff --git a/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c b/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
>> index 4ed46ed58a7b..d9d69941f694 100644
>> --- a/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
>> +++ b/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
>> @@ -126,3 +126,38 @@ void test_libbpf_probe_helpers(void)
>>                  ASSERT_EQ(res, d->supported, buf);
>>          }
>>   }
>> +
>> +void test_libbpf_probe_kfuncs(void)
>> +{
>> +       int ret, kfunc_id;
>> +       char *kfunc = "bpf_cpumask_create";
>> +       struct btf *btf;
>> +
>> +       btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);
>> +       if (!ASSERT_OK_PTR(btf, "btf_parse"))
>> +               return;
>> +
>> +       kfunc_id = btf__find_by_name_kind(btf, kfunc, BTF_KIND_FUNC);
>> +       if (!ASSERT_GT(kfunc_id, 0, kfunc))
>> +               goto cleanup;
>> +
>> +       /* prog BPF_PROG_TYPE_SYSCALL supports kfunc bpf_cpumask_create */
>> +       ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_SYSCALL, kfunc_id, 0, NULL);
>> +       ASSERT_EQ(ret, 1, kfunc);
>> +
>> +       /* prog BPF_PROG_TYPE_KPROBE does not support kfunc bpf_cpumask_create */
>> +       ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_KPROBE, kfunc_id, 0, NULL);
>> +       ASSERT_EQ(ret, 0, kfunc);
>> +
>> +       /* invalid kfunc id */
>> +       ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_KPROBE, -1, 0, NULL);
>> +       ASSERT_EQ(ret, 0, "invalid kfunc id:-1");
>> +
>> +       /* invalid prog type */
>> +       ret = libbpf_probe_bpf_kfunc(100000, kfunc_id, 0, NULL);
>> +       if (!ASSERT_LE(ret, 0, "invalid prog type:100000"))
> 
> we have ASSERT_ERR(), wouldn't it work here?
> 
> 
> let's also add a test for kfunc in module (we have bpf_testmod, we
> should be able to test something out of there)

Ok, i will add it in v4.

> 
>> +               goto cleanup;
>> +
>> +cleanup:
>> +       btf__free(btf);
>> +}
>> --
>> 2.43.0
>>


-- 
Best Regards
Dylane Chen

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2025-01-25 15:07 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-24 14:44 [PATCH bpf-next v3 0/3] Add prog_kfunc feature probe Tao Chen
2025-01-24 14:44 ` [PATCH bpf-next v3 1/3] libbpf: Refactor libbpf_probe_bpf_helper Tao Chen
2025-01-24 16:26   ` Jiri Olsa
2025-01-25 14:52     ` Tao Chen
2025-01-24 18:44   ` Andrii Nakryiko
2025-01-25 15:05     ` Tao Chen
2025-01-24 14:44 ` [PATCH bpf-next v3 2/3] libbpf: Add libbpf_probe_bpf_kfunc API Tao Chen
2025-01-24 16:27   ` Jiri Olsa
2025-01-25 14:54     ` Tao Chen
2025-01-24 18:46   ` Andrii Nakryiko
2025-01-24 14:44 ` [PATCH bpf-next v3 3/3] selftests/bpf: Add libbpf_probe_bpf_kfunc API selftests Tao Chen
2025-01-24 16:27   ` Jiri Olsa
2025-01-25 14:56     ` Tao Chen
2025-01-24 18:48   ` Andrii Nakryiko
2025-01-25 15:07     ` Tao Chen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox