Skip to contentSkip to conversation

Add prog_kfunc feature probe

PATCH Superseded Tao Chen 3 participants 15 messages Jan 24, 2025 – Jan 25, 2025

Subsystems (4)

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Tao Chen

author
#0 lore raw

v3 cover Add prog_kfunc feature probe

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/[email protected]

- 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/[email protected]

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(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v3 1/3 libbpf: Refactor libbpf_probe_bpf_helper

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

---
 tools/lib/bpf/libbpf_probes.c | 38 ++++++++++++++++++++++++-----------
 1 file changed, 26 insertions(+), 12 deletions(-)
tools/lib/bpf/libbpf_probes.c +26 -12
index 9dfbe7750f56..b73345977b4e 100644
@@ -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".
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH bpf-next v3 1/3] libbpf: Refactor libbpf_probe_bpf_helper

Quoted 15 lines · Tao Chen

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

---
 tools/lib/bpf/libbpf_probes.c | 38 ++++++++++++++++++++++++-----------
 1 file changed, 26 insertions(+), 12 deletions(-)
tools/lib/bpf/libbpf_probes.c +0 -0
index 9dfbe7750f56..b73345977b4e 100644
@@ -413,22 +413,20 @@ int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts)
 	return libbpf_err(ret);
 }

tools/lib/bpf/libbpf_probes.c int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts) · line 421

Quoted 10 lines
tools/lib/bpf/libbpf_probes.c +3 -3
 
-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

Quoted 50 lines
tools/lib/bpf/libbpf_probes.c +23 -9
 	};
 	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".
Signature · 1 line
2.43.0
author
#3 lore raw

Re: [PATCH bpf-next v3 1/3] libbpf: Refactor libbpf_probe_bpf_helper

Quoted 30 lines · Jiri Olsa
Quoted 15 lines · Tao Chen

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

---
  tools/lib/bpf/libbpf_probes.c | 38 ++++++++++++++++++++++++-----------
  1 file changed, 26 insertions(+), 12 deletions(-)
tools/lib/bpf/libbpf_probes.c +0 -0
index 9dfbe7750f56..b73345977b4e 100644
@@ -413,22 +413,20 @@ int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts)
  	return libbpf_err(ret);
  }

tools/lib/bpf/libbpf_probes.c int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts) · line 421

Quoted 10 lines
tools/lib/bpf/libbpf_probes.c +3 -3
 
-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

Quoted 2 lines

jirka

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

Quoted 50 lines
Quoted 50 lines
tools/lib/bpf/libbpf_probes.c +23 -9
  	};
  	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".
Signature · 1 line
2.43.0
Signature · 2 lines
Best Regards
Dylane Chen
#4 lore raw

Re: [PATCH bpf-next v3 1/3] libbpf: Refactor libbpf_probe_bpf_helper

In reply to Tao Chen
Quoted 32 lines · Tao Chen

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

---
 tools/lib/bpf/libbpf_probes.c | 38 ++++++++++++++++++++++++-----------
 1 file changed, 26 insertions(+), 12 deletions(-)
tools/lib/bpf/libbpf_probes.c +4 -5
index 9dfbe7750f56..b73345977b4e 100644
@@ -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;
 

tools/lib/bpf/libbpf_probes.c int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts) · line 432

Quoted 10 lines
tools/lib/bpf/libbpf_probes.c +2 -3
-       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

Quoted 34 lines
tools/lib/bpf/libbpf_probes.c +20 -5
@@ -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

author
#5 lore raw

Re: [PATCH bpf-next v3 1/3] libbpf: Refactor libbpf_probe_bpf_helper

Quoted 61 lines · Andrii Nakryiko
Quoted 32 lines · Tao Chen

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

---
  tools/lib/bpf/libbpf_probes.c | 38 ++++++++++++++++++++++++-----------
  1 file changed, 26 insertions(+), 12 deletions(-)
tools/lib/bpf/libbpf_probes.c +4 -5
index 9dfbe7750f56..b73345977b4e 100644
@@ -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;
 

tools/lib/bpf/libbpf_probes.c int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts) · line 432

Quoted 10 lines
tools/lib/bpf/libbpf_probes.c +2 -3
-       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;
}
Quoted 5 lines

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.

Quoted 34 lines
Quoted 34 lines
tools/lib/bpf/libbpf_probes.c +20 -5
@@ -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

Signature · 2 lines
Best Regards
Dylane Chen
author
#6 lore raw

PATCH v3 2/3 libbpf: Add libbpf_probe_bpf_kfunc API

In reply to Tao Chen

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.

---
 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(-)
tools/lib/bpf/libbpf.h +16 -1
index 3020ee45303a..035829e22099 100644
@@ -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.
tools/lib/bpf/libbpf.map +1 -0
index a8b2936a1646..e93fae101efd 100644
@@ -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;
tools/lib/bpf/libbpf_probes.c +30 -0
index b73345977b4e..cd7d16c1cc49 100644
@@ -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)
 {
Signature · 1 line
2.43.0
#7 lore raw

Re: [PATCH bpf-next v3 2/3] libbpf: Add libbpf_probe_bpf_kfunc API

Quoted 61 lines · Tao Chen

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.

---
 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(-)
tools/lib/bpf/libbpf.h +16 -1
index 3020ee45303a..035829e22099 100644
@@ -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.
tools/lib/bpf/libbpf.map +1 -0
index a8b2936a1646..e93fae101efd 100644
@@ -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;
tools/lib/bpf/libbpf_probes.c +4 -0
index b73345977b4e..cd7d16c1cc49 100644
@@ -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;

tools/lib/bpf/libbpf_probes.c static int probe_func_comm(enum bpf_prog_type prog_type, struct bpf_insn insn, · line 462

Quoted 10 lines
tools/lib/bpf/libbpf_probes.c +10 -0
+	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

Quoted 22 lines
tools/lib/bpf/libbpf_probes.c +16 -0
+
+	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)
 {
Signature · 1 line
2.43.0
author
#8 lore raw

Re: [PATCH bpf-next v3 2/3] libbpf: Add libbpf_probe_bpf_kfunc API

Quoted 75 lines · Jiri Olsa
Quoted 61 lines · Tao Chen

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.

---
  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(-)
tools/lib/bpf/libbpf.h +16 -1
index 3020ee45303a..035829e22099 100644
@@ -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.
tools/lib/bpf/libbpf.map +1 -0
index a8b2936a1646..e93fae101efd 100644
@@ -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;
tools/lib/bpf/libbpf_probes.c +4 -0
index b73345977b4e..cd7d16c1cc49 100644
@@ -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;

tools/lib/bpf/libbpf_probes.c static int probe_func_comm(enum bpf_prog_type prog_type, struct bpf_insn insn, · line 462

Quoted 10 lines
tools/lib/bpf/libbpf_probes.c +10 -0
+	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

Quoted 7 lines
         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.

Quoted 22 lines
Quoted 22 lines
tools/lib/bpf/libbpf_probes.c +16 -0
+
+	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)
  {
Signature · 1 line
2.43.0
Signature · 2 lines
Best Regards
Dylane Chen
#9 lore raw

Re: [PATCH bpf-next v3 2/3] libbpf: Add libbpf_probe_bpf_kfunc API

In reply to Tao Chen
Quoted 55 lines · Tao Chen

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.

---
 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(-)
tools/lib/bpf/libbpf.h +16 -1
index 3020ee45303a..035829e22099 100644
@@ -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.
tools/lib/bpf/libbpf.map +1 -0
index a8b2936a1646..e93fae101efd 100644
@@ -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;
tools/lib/bpf/libbpf_probes.c +0 -0
index b73345977b4e..cd7d16c1cc49 100644

tools/lib/bpf/libbpf_probes.c static int probe_func_comm(enum bpf_prog_type prog_type, struct bpf_insn insn, · line 450

Quoted 5 lines
tools/lib/bpf/libbpf_probes.c +2 -0
@@ -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)

Quoted 34 lines
tools/lib/bpf/libbpf_probes.c +28 -1
+{
+       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

author
#10 lore raw

PATCH v3 3/3 selftests/bpf: Add libbpf_probe_bpf_kfunc API selftests

In reply to Tao Chen

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

---
 .../selftests/bpf/prog_tests/libbpf_probes.c  | 35 +++++++++++++++++++
 1 file changed, 35 insertions(+)
tools/testing/selftests/bpf/prog_tests/libbpf_probes.c +35 -0
index 4ed46ed58a7b..d9d69941f694 100644
@@ -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);
+}
Signature · 1 line
2.43.0
#11 lore raw

Re: [PATCH bpf-next v3 3/3] selftests/bpf: Add libbpf_probe_bpf_kfunc API selftests

Quoted 39 lines · Tao Chen

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

---
 .../selftests/bpf/prog_tests/libbpf_probes.c  | 35 +++++++++++++++++++
 1 file changed, 35 insertions(+)
tools/testing/selftests/bpf/prog_tests/libbpf_probes.c +21 -0
index 4ed46ed58a7b..d9d69941f694 100644
@@ -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);

tools/testing/selftests/bpf/prog_tests/libbpf_probes.c void test_libbpf_probe_helpers(void) · line 159

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/libbpf_probes.c +10 -0
+	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

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/libbpf_probes.c +4 -0
+
+cleanup:
+	btf__free(btf);
+}
Signature · 1 line
2.43.0
author
#12 lore raw

Re: [PATCH bpf-next v3 3/3] selftests/bpf: Add libbpf_probe_bpf_kfunc API selftests

Quoted 53 lines · Jiri Olsa
Quoted 39 lines · Tao Chen

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

---
  .../selftests/bpf/prog_tests/libbpf_probes.c  | 35 +++++++++++++++++++
  1 file changed, 35 insertions(+)
tools/testing/selftests/bpf/prog_tests/libbpf_probes.c +21 -0
index 4ed46ed58a7b..d9d69941f694 100644
@@ -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);

tools/testing/selftests/bpf/prog_tests/libbpf_probes.c void test_libbpf_probe_helpers(void) · line 159

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/libbpf_probes.c +10 -0
+	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

Quoted 2 lines

jirka

Ack. Will fix.

Quoted 7 lines
Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/libbpf_probes.c +4 -0
+
+cleanup:
+	btf__free(btf);
+}
Signature · 1 line
2.43.0
Signature · 2 lines
Best Regards
Dylane Chen
#13 lore raw

Re: [PATCH bpf-next v3 3/3] selftests/bpf: Add libbpf_probe_bpf_kfunc API selftests

In reply to Tao Chen
Quoted 39 lines · Tao Chen

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

---
 .../selftests/bpf/prog_tests/libbpf_probes.c  | 35 +++++++++++++++++++
 1 file changed, 35 insertions(+)
tools/testing/selftests/bpf/prog_tests/libbpf_probes.c +20 -0
index 4ed46ed58a7b..d9d69941f694 100644
@@ -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 */

tools/testing/selftests/bpf/prog_tests/libbpf_probes.c void test_libbpf_probe_helpers(void) · line 158

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/libbpf_probes.c +10 -0
+       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)

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/libbpf_probes.c +5 -1
+               goto cleanup;
+
+cleanup:
+       btf__free(btf);
+}
--

2.43.0

author
#14 lore raw

Re: [PATCH bpf-next v3 3/3] selftests/bpf: Add libbpf_probe_bpf_kfunc API selftests

Quoted 54 lines · Andrii Nakryiko
Quoted 39 lines · Tao Chen

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

---
  .../selftests/bpf/prog_tests/libbpf_probes.c  | 35 +++++++++++++++++++
  1 file changed, 35 insertions(+)
tools/testing/selftests/bpf/prog_tests/libbpf_probes.c +20 -0
index 4ed46ed58a7b..d9d69941f694 100644
@@ -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 */

tools/testing/selftests/bpf/prog_tests/libbpf_probes.c void test_libbpf_probe_helpers(void) · line 158

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/libbpf_probes.c +10 -0
+       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?

Quoted 2 lines

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.

Quoted 9 lines
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/libbpf_probes.c +5 -1
+               goto cleanup;
+
+cleanup:
+       btf__free(btf);
+}
--

2.43.0

Signature · 2 lines
Best Regards
Dylane Chen