Skip to contentSkip to conversation

bpf: Support ->fill_link_info for kprobe prog

PATCH Superseded Yafang Shao 5 participants 19 messages Jun 2, 2023 – Jun 4, 2023

Subsystems (6)
BPF Tracing BPF core bpftool +2 more subsystems

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

cover bpf: Support ->fill_link_info for kprobe prog

Currently, it is not easy to determine which functions are probed by a kprobe_multi program. This patchset supports ->fill_link_info for it, allowing the user to easily obtain the probed functions.

Although the user can retrieve the functions probed by a perf_event program using bpftool perf show, it would be beneficial to also support ->fill_link_info. This way, the user can obtain it in the same manner as other bpf links.

It would be preferable to expose the address directly rather than the symbol name, as multiple functions may share the same name. These addresses will be parsed by bpftool.

RFC->v1:
- Use a single copy_to_user() instead (Jiri)
- Show also the symbol name in bpftool (Quentin, Alexei)
- Use calloc() instead of malloc() in bpftool (Quentin)
- Avoid having conditional entries in the JSON output (Quentin)
- Drop ->show_fdinfo (Alexei)
- Use __u64 instead of __aligned_u64 for the field addr (Alexei)
- Avoid the contradiction in perf_event name length (Alexei)
- Address a build warning reported by kernel test robot <[email protected]>

Yafang Shao (6):
bpf: Support ->fill_link_info for kprobe_multi
bpftool: Show probed function in kprobe_multi link info
bpf: Always expose the probed address
bpf: Add a common helper bpf_copy_to_user()
bpf: Support ->fill_link_info for perf_event
bpftool: Show probed function in perf_event link info

 include/uapi/linux/bpf.h       |  10 ++++
 kernel/bpf/syscall.c           |  79 +++++++++++++++++++++++-----
 kernel/trace/bpf_trace.c       |  26 ++++++++++
 kernel/trace/trace_kprobe.c    |   2 +-
 tools/bpf/bpftool/link.c       | 115 ++++++++++++++++++++++++++++++++++++++++-
 tools/include/uapi/linux/bpf.h |  10 ++++
 6 files changed, 226 insertions(+), 16 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/6 bpf: Support ->fill_link_info for kprobe_multi

By adding support for ->fill_link_info to the kprobe_multi link, users will be able to inspect it using bpftool link show. This enhancement will expose both the count of probed functions and their respective addresses to the user.

---
 include/uapi/linux/bpf.h       |  4 ++++
 kernel/trace/bpf_trace.c       | 26 ++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h |  4 ++++
 3 files changed, 34 insertions(+)
include/uapi/linux/bpf.h +4 -0
index a7b5e91..22c8168 100644
@@ -6438,6 +6438,10 @@ struct bpf_link_info {
 			__s32 priority;
 			__u32 flags;
 		} netfilter;
+		struct {
+			__u64 addrs;
+			__u32 count;
+		} kprobe_multi;
 	};
 } __attribute__((aligned(8)));
 
kernel/trace/bpf_trace.c +26 -0
index 2bc41e6..ec53bc9 100644
@@ -2548,9 +2548,35 @@ static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link)
 	kfree(kmulti_link);
 }
 
+static int bpf_kprobe_multi_link_fill_link_info(const struct bpf_link *link,
+						struct bpf_link_info *info)
+{
+	u64 *uaddrs = (u64 *)u64_to_user_ptr(info->kprobe_multi.addrs);
+	struct bpf_kprobe_multi_link *kmulti_link;
+	u32 ucount = info->kprobe_multi.count;
+
+	if (!uaddrs ^ !ucount)
+		return -EINVAL;
+
+	kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
+	if (!uaddrs) {
+		info->kprobe_multi.count = kmulti_link->cnt;
+		return 0;
+	}
+
+	if (!ucount)
+		return 0;
+	if (ucount != kmulti_link->cnt)
+		return -EINVAL;
+	if (copy_to_user(uaddrs, kmulti_link->addrs, ucount * sizeof(u64)))
+		return -EFAULT;
+	return 0;
+}
+
 static const struct bpf_link_ops bpf_kprobe_multi_link_lops = {
 	.release = bpf_kprobe_multi_link_release,
 	.dealloc = bpf_kprobe_multi_link_dealloc,
+	.fill_link_info = bpf_kprobe_multi_link_fill_link_info,
 };
 
 static void bpf_kprobe_multi_cookie_swap(void *a, void *b, int size, const void *priv)
tools/include/uapi/linux/bpf.h +4 -0
index a7b5e91..22c8168 100644
@@ -6438,6 +6438,10 @@ struct bpf_link_info {
 			__s32 priority;
 			__u32 flags;
 		} netfilter;
+		struct {
+			__u64 addrs;
+			__u32 count;
+		} kprobe_multi;
 	};
 } __attribute__((aligned(8)));
 
Signature · 1 line
1.8.3.1
#2 lore raw

Re: [PATCH bpf-next 1/6] bpf: Support ->fill_link_info for kprobe_multi

Quoted 32 lines · Yafang Shao

By adding support for ->fill_link_info to the kprobe_multi link, users will be able to inspect it using bpftool link show. This enhancement will expose both the count of probed functions and their respective addresses to the user.

---
 include/uapi/linux/bpf.h       |  4 ++++
 kernel/trace/bpf_trace.c       | 26 ++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h |  4 ++++
 3 files changed, 34 insertions(+)
include/uapi/linux/bpf.h +4 -0
index a7b5e91..22c8168 100644
@@ -6438,6 +6438,10 @@ struct bpf_link_info {
 			__s32 priority;
 			__u32 flags;
 		} netfilter;
+		struct {
+			__u64 addrs;
+			__u32 count;
+		} kprobe_multi;
 	};
 } __attribute__((aligned(8)));
 
kernel/trace/bpf_trace.c +0 -0
index 2bc41e6..ec53bc9 100644

kernel/trace/bpf_trace.c static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link) · line 2554

Quoted 7 lines
kernel/trace/bpf_trace.c +4 -0
@@ -2548,9 +2548,35 @@ static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link)
 	kfree(kmulti_link);
 }
 
+static int bpf_kprobe_multi_link_fill_link_info(const struct bpf_link *link,
+						struct bpf_link_info *info)
+{
+	u64 *uaddrs = (u64 *)u64_to_user_ptr(info->kprobe_multi.addrs);

Maybe tag this as __user as well?

	u64 __user *uaddrs = u64_to_user_ptr(info->kprobe_multi.addrs);

copy_to_user expects __user tagged memory, so most likely sparse tool will complain on your current approach.

Quoted 16 lines
kernel/trace/bpf_trace.c +16 -0
+	struct bpf_kprobe_multi_link *kmulti_link;
+	u32 ucount = info->kprobe_multi.count;
+
+	if (!uaddrs ^ !ucount)
+		return -EINVAL;
+
+	kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
+	if (!uaddrs) {
+		info->kprobe_multi.count = kmulti_link->cnt;
+		return 0;
+	}
+
+	if (!ucount)
+		return 0;
+	if (ucount != kmulti_link->cnt)
+		return -EINVAL;

[..]

kernel/trace/bpf_trace.c static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link) · line 2572

Quoted 2 lines
kernel/trace/bpf_trace.c +2 -0
+	if (copy_to_user(uaddrs, kmulti_link->addrs, ucount * sizeof(u64)))
+		return -EFAULT;

I'm not super familiar with this part, so maybe stupid question:
do we need to hold any locks during the copy? IOW, can kmulti_link->addrs be updated concurrently?

#3 lore raw

Re: [PATCH bpf-next 1/6] bpf: Support ->fill_link_info for kprobe_multi

Quoted 44 lines · Stanislav Fomichev
Quoted 32 lines · Yafang Shao

By adding support for ->fill_link_info to the kprobe_multi link, users will be able to inspect it using bpftool link show. This enhancement will expose both the count of probed functions and their respective addresses to the user.

---
 include/uapi/linux/bpf.h       |  4 ++++
 kernel/trace/bpf_trace.c       | 26 ++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h |  4 ++++
 3 files changed, 34 insertions(+)
include/uapi/linux/bpf.h +4 -0
index a7b5e91..22c8168 100644
@@ -6438,6 +6438,10 @@ struct bpf_link_info {
                      __s32 priority;
                      __u32 flags;
              } netfilter;
+             struct {
+                     __u64 addrs;
+                     __u32 count;
+             } kprobe_multi;
      };
 } __attribute__((aligned(8)));
 
kernel/trace/bpf_trace.c +0 -0
index 2bc41e6..ec53bc9 100644

kernel/trace/bpf_trace.c static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link) · line 2554

Quoted 7 lines
kernel/trace/bpf_trace.c +4 -0
@@ -2548,9 +2548,35 @@ static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link)
      kfree(kmulti_link);
 }
 
+static int bpf_kprobe_multi_link_fill_link_info(const struct bpf_link *link,
+                                             struct bpf_link_info *info)
+{
+     u64 *uaddrs = (u64 *)u64_to_user_ptr(info->kprobe_multi.addrs);

Maybe tag this as __user as well?

Quoted 4 lines
        u64 __user *uaddrs = u64_to_user_ptr(info->kprobe_multi.addrs);

copy_to_user expects __user tagged memory, so most likely sparse tool will complain on your current approach.

Thanks for pointing it out. Will correct it.

Quoted 20 lines
Quoted 17 lines
kernel/trace/bpf_trace.c +16 -0
+     struct bpf_kprobe_multi_link *kmulti_link;
+     u32 ucount = info->kprobe_multi.count;
+
+     if (!uaddrs ^ !ucount)
+             return -EINVAL;
+
+     kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
+     if (!uaddrs) {
+             info->kprobe_multi.count = kmulti_link->cnt;
+             return 0;
+     }
+
+     if (!ucount)
+             return 0;
+     if (ucount != kmulti_link->cnt)
+             return -EINVAL;

[..]

Quoted 6 lines

kernel/trace/bpf_trace.c static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link) · line 2572

Quoted 2 lines
kernel/trace/bpf_trace.c +2 -0
+     if (copy_to_user(uaddrs, kmulti_link->addrs, ucount * sizeof(u64)))
+             return -EFAULT;

I'm not super familiar with this part, so maybe stupid question:
do we need to hold any locks during the copy? IOW, can kmulti_link->addrs be updated concurrently?

No, we can't update kmulti_link->addrs concurrently. Once a fprobe is registered, we can't update it unless we unregister it. When we reach the ->fill_link_info, it can't be released, so it is safe.

--
Regards
Yafang

#4 lore raw

Re: [PATCH bpf-next 1/6] bpf: Support ->fill_link_info for kprobe_multi

In reply to Yafang Shao
kernel test robot noticed the following build warnings:

Hi Yafang,

kernel test robot noticed the following build warnings:

[auto build test WARNING on bpf-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Yafang-Shao/bpf-Support-fill_link_info-for-kprobe_multi/20230602-165455
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/20230602085239.91138-2-laoar.shao%40gmail.com
patch subject: [PATCH bpf-next 1/6] bpf: Support ->fill_link_info for kprobe_multi
config: i386-randconfig-s002-20230601 (https://download.01.org/0day-ci/archive/20230603/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce:
        # apt-get install sparse
        # sparse version: v0.6.4-39-gce1a6720-dirty
        # https://github.com/intel-lab-lkp/linux/commit/270f3eb6c142f1f0ec7d800b8ecaab1b101682a0
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Yafang-Shao/bpf-Support-fill_link_info-for-kprobe_multi/20230602-165455
        git checkout 270f3eb6c142f1f0ec7d800b8ecaab1b101682a0
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=i386 olddefconfig
        make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=i386 SHELL=/bin/bash kernel/trace/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

sparse warnings: (new ones prefixed by >>)

>> kernel/trace/bpf_trace.c:2554:24: sparse: sparse: cast removes address space '__user' of expression
>> kernel/trace/bpf_trace.c:2571:26: sparse: sparse: incorrect type in argument 1 (different address spaces) @@     expected void [noderef] __user *to @@     got unsigned long long [usertype] *uaddrs @@

kernel/trace/bpf_trace.c:2571:26: sparse: expected void [noderef] __user *to
kernel/trace/bpf_trace.c:2571:26: sparse: got unsigned long long [usertype] *uaddrs
kernel/trace/bpf_trace.c:2492:21: sparse: sparse: dereference of noderef expression kernel/trace/bpf_trace.c:2496:66: sparse: sparse: dereference of noderef expression

vim +/__user +2554 kernel/trace/bpf_trace.c

2550
2551 static int bpf_kprobe_multi_link_fill_link_info(const struct bpf_link *link,
2552 struct bpf_link_info *info)
2553 {

> 2554		u64 *uaddrs = (u64 *)u64_to_user_ptr(info->kprobe_multi.addrs);
  2555		struct bpf_kprobe_multi_link *kmulti_link;
  2556		u32 ucount = info->kprobe_multi.count;
  2557	
  2558		if (!uaddrs ^ !ucount)
  2559			return -EINVAL;
  2560	
  2561		kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
  2562		if (!uaddrs) {
  2563			info->kprobe_multi.count = kmulti_link->cnt;
  2564			return 0;
  2565		}
  2566	
  2567		if (!ucount)
  2568			return 0;
  2569		if (ucount != kmulti_link->cnt)
  2570			return -EINVAL;
> 2571		if (copy_to_user(uaddrs, kmulti_link->addrs, ucount * sizeof(u64)))

2572 return -EFAULT;
2573 return 0;
2574 }
2575

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#5 lore raw

Re: [PATCH bpf-next 1/6] bpf: Support ->fill_link_info for kprobe_multi

Quoted 37 lines · kernel test robot

Hi Yafang,

kernel test robot noticed the following build warnings:

[auto build test WARNING on bpf-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Yafang-Shao/bpf-Support-fill_link_info-for-kprobe_multi/20230602-165455
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/20230602085239.91138-2-laoar.shao%40gmail.com
patch subject: [PATCH bpf-next 1/6] bpf: Support ->fill_link_info for kprobe_multi
config: i386-randconfig-s002-20230601 (https://download.01.org/0day-ci/archive/20230603/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce:
        # apt-get install sparse
        # sparse version: v0.6.4-39-gce1a6720-dirty
        # https://github.com/intel-lab-lkp/linux/commit/270f3eb6c142f1f0ec7d800b8ecaab1b101682a0
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Yafang-Shao/bpf-Support-fill_link_info-for-kprobe_multi/20230602-165455
        git checkout 270f3eb6c142f1f0ec7d800b8ecaab1b101682a0
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=i386 olddefconfig
        make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=i386 SHELL=/bin/bash kernel/trace/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

sparse warnings: (new ones prefixed by >>)

Quoted 2 lines
Quoted 2 lines

kernel/trace/bpf_trace.c:2554:24: sparse: sparse: cast removes address space '__user' of expression
kernel/trace/bpf_trace.c:2571:26: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected void [noderef] __user *to @@ got unsigned long long [usertype] *uaddrs @@

kernel/trace/bpf_trace.c:2571:26: sparse: expected void [noderef] __user *to
kernel/trace/bpf_trace.c:2571:26: sparse: got unsigned long long [usertype] *uaddrs
kernel/trace/bpf_trace.c:2492:21: sparse: sparse: dereference of noderef expression kernel/trace/bpf_trace.c:2496:66: sparse: sparse: dereference of noderef expression

Quoted 7 lines

vim +/__user +2554 kernel/trace/bpf_trace.c

2550
2551 static int bpf_kprobe_multi_link_fill_link_info(const struct bpf_link *link,
2552 struct bpf_link_info *info)
2553 {

Quoted 1 line

2554 u64 *uaddrs = (u64 *)u64_to_user_ptr(info->kprobe_multi.addrs);

Thanks for the report. Will fix it.

Quoted 25 lines

2555 struct bpf_kprobe_multi_link *kmulti_link;
2556 u32 ucount = info->kprobe_multi.count;
2557
2558 if (!uaddrs ^ !ucount)
2559 return -EINVAL;
2560
2561 kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
2562 if (!uaddrs) {
2563 info->kprobe_multi.count = kmulti_link->cnt;
2564 return 0;
2565 }
2566
2567 if (!ucount)
2568 return 0;
2569 if (ucount != kmulti_link->cnt)
2570 return -EINVAL;

Quoted 1 line

2571 if (copy_to_user(uaddrs, kmulti_link->addrs, ucount * sizeof(u64)))

2572 return -EFAULT;
2573 return 0;
2574 }
2575

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

Signature · 2 lines
Regards
Yafang
#6 lore raw

PATCH 2/6 bpftool: Show probed function in kprobe_multi link info

In reply to Yafang Shao

Show the already expose kprobe_multi link info in bpftool. The result as follows,

$ tools/bpf/bpftool/bpftool link show
1: kprobe_multi  prog 5
        func_cnt 4  addrs            symbols
                    ffffffffb4d465b0 schedule_timeout_interruptible
                    ffffffffb4d465f0 schedule_timeout_killable
                    ffffffffb4d46630 schedule_timeout_uninterruptible
                    ffffffffb4d46670 schedule_timeout_idle
        pids trace(8729)
$ tools/bpf/bpftool/bpftool link show -j
[{"id":1,"type":"kprobe_multi","prog_id":5,"func_cnt":4,"addrs":[{"addr":18446744072448402864,"symbol":"schedule_timeout_interruptible"},{"addr":18446744072448402928,"symbol":"schedule_timeout_killable"},{"addr":18446744072448402992,"symbol":"schedule_timeout_uninterruptible"},{"addr":18446744072448403056,"symbol":"schedule_timeout_idle"}],"pids":[{"pid":8729,"comm":"trace"}]}]
---
 tools/bpf/bpftool/link.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 94 insertions(+)
tools/bpf/bpftool/link.c +94 -0
index 2d78607..3b00c07 100644
@@ -166,6 +166,57 @@ static int get_prog_info(int prog_id, struct bpf_prog_info *info)
 	return err;
 }
 
+static int cmp_u64(const void *A, const void *B)
+{
+	const __u64 *a = A, *b = B;
+
+	return *a - *b;
+}
+
+static void kprobe_multi_print_plain(__u64 addr, char *sym, __u32 indent)
+{
+	printf("\n\t%*s  %0*llx %s", indent, "", 16, addr, sym);
+}
+
+static void kprobe_multi_print_json(__u64 addr, char *sym)
+{
+	jsonw_start_object(json_wtr);
+	jsonw_uint_field(json_wtr, "addr", addr);
+	jsonw_string_field(json_wtr, "symbol", sym);
+	jsonw_end_object(json_wtr);
+}
+
+static void kernel_syms_show(const __u64 *addrs, __u32 cnt, __u32 indent)
+{
+	char buff[256], sym[256];
+	__u64 addr;
+	int i = 0;
+	FILE *fp;
+
+	fp = fopen("/proc/kallsyms", "r");
+	if (!fp)
+		return;
+
+	/* Each address is guaranteed to be unique. */
+	qsort((void *)addrs, cnt, sizeof(__u64), cmp_u64);
+	/* The addresses in /proc/kallsyms are already sorted. */
+	while (fgets(buff, sizeof(buff), fp)) {
+		if (sscanf(buff, "%llx %*c %s", &addr, sym) != 2)
+			continue;
+		/* The addr probed by kprobe_multi is always in
+		 * /proc/kallsyms, so we can ignore some edge cases.
+		 */
+		if (addr != addrs[i])
+			continue;
+		if (indent)
+			kprobe_multi_print_plain(addr, sym, indent);
+		else
+			kprobe_multi_print_json(addr, sym);
+		i++;
+	}
+	fclose(fp);
+}
+
 static int show_link_close_json(int fd, struct bpf_link_info *info)
 {
 	struct bpf_prog_info prog_info;
@@ -218,6 +269,17 @@ static int show_link_close_json(int fd, struct bpf_link_info *info)
 		jsonw_uint_field(json_wtr, "map_id",
 				 info->struct_ops.map_id);
 		break;
+	case BPF_LINK_TYPE_KPROBE_MULTI:
+		const __u64 *addrs;
+
+		jsonw_uint_field(json_wtr, "func_cnt", info->kprobe_multi.count);
+		jsonw_name(json_wtr, "addrs");
+		jsonw_start_array(json_wtr);
+		addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs);
+		if (info->kprobe_multi.count)
+			kernel_syms_show(addrs, info->kprobe_multi.count, 0);
+		jsonw_end_array(json_wtr);
+		break;
 	default:
 		break;
 	}
@@ -396,6 +458,20 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info)
 	case BPF_LINK_TYPE_NETFILTER:
 		netfilter_dump_plain(info);
 		break;
+	case BPF_LINK_TYPE_KPROBE_MULTI:
+		__u32 indent, cnt, i;
+		const __u64 *addrs;
+
+		cnt = info->kprobe_multi.count;
+		if (!cnt)
+			break;
+		printf("\n\tfunc_cnt %d  %-16s %s", cnt, "addrs", "symbols");
+		for (i = 0; cnt; i++)
+			cnt /= 10;
+		indent = strlen("func_cnt ") + i;
+		addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs);
+		kernel_syms_show(addrs, cnt, indent);
+		break;
 	default:
 		break;
 	}
@@ -417,7 +493,9 @@ static int do_show_link(int fd)
 {
 	struct bpf_link_info info;
 	__u32 len = sizeof(info);
+	__u64 *addrs = NULL;
 	char buf[256];
+	int count;
 	int err;
 
 	memset(&info, 0, sizeof(info));
@@ -441,12 +519,28 @@ static int do_show_link(int fd)
 		info.iter.target_name_len = sizeof(buf);
 		goto again;
 	}
+	if (info.type == BPF_LINK_TYPE_KPROBE_MULTI &&
+	    !info.kprobe_multi.addrs) {
+		count = info.kprobe_multi.count;
+		if (count) {
+			addrs = calloc(count, sizeof(__u64));
+			if (!addrs) {
+				p_err("mem alloc failed");
+				close(fd);
+				return -1;
+			}
+			info.kprobe_multi.addrs = (unsigned long)addrs;
+			goto again;
+		}
+	}
 
 	if (json_output)
 		show_link_close_json(fd, &info);
 	else
 		show_link_close_plain(fd, &info);
 
+	if (addrs)
+		free(addrs);
 	close(fd);
 	return 0;
 }
Signature · 1 line
1.8.3.1
#7 lore raw

Re: [PATCH bpf-next 2/6] bpftool: Show probed function in kprobe_multi link info

Quoted 66 lines · Yafang Shao

Show the already expose kprobe_multi link info in bpftool. The result as follows,

$ tools/bpf/bpftool/bpftool link show
1: kprobe_multi  prog 5
        func_cnt 4  addrs            symbols
                    ffffffffb4d465b0 schedule_timeout_interruptible
                    ffffffffb4d465f0 schedule_timeout_killable
                    ffffffffb4d46630 schedule_timeout_uninterruptible
                    ffffffffb4d46670 schedule_timeout_idle
        pids trace(8729)
$ tools/bpf/bpftool/bpftool link show -j
[{"id":1,"type":"kprobe_multi","prog_id":5,"func_cnt":4,"addrs":[{"addr":18446744072448402864,"symbol":"schedule_timeout_interruptible"},{"addr":18446744072448402928,"symbol":"schedule_timeout_killable"},{"addr":18446744072448402992,"symbol":"schedule_timeout_uninterruptible"},{"addr":18446744072448403056,"symbol":"schedule_timeout_idle"}],"pids":[{"pid":8729,"comm":"trace"}]}]
---
 tools/bpf/bpftool/link.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 94 insertions(+)
tools/bpf/bpftool/link.c +39 -0
index 2d78607..3b00c07 100644
@@ -166,6 +166,57 @@ static int get_prog_info(int prog_id, struct bpf_prog_info *info)
 	return err;
 }
 
+static int cmp_u64(const void *A, const void *B)
+{
+	const __u64 *a = A, *b = B;
+
+	return *a - *b;
+}
+
+static void kprobe_multi_print_plain(__u64 addr, char *sym, __u32 indent)
+{
+	printf("\n\t%*s  %0*llx %s", indent, "", 16, addr, sym);
+}
+
+static void kprobe_multi_print_json(__u64 addr, char *sym)
+{
+	jsonw_start_object(json_wtr);
+	jsonw_uint_field(json_wtr, "addr", addr);
+	jsonw_string_field(json_wtr, "symbol", sym);
+	jsonw_end_object(json_wtr);
+}
+
+static void kernel_syms_show(const __u64 *addrs, __u32 cnt, __u32 indent)
+{
+	char buff[256], sym[256];
+	__u64 addr;
+	int i = 0;
+	FILE *fp;
+
+	fp = fopen("/proc/kallsyms", "r");
+	if (!fp)
+		return;
+
+	/* Each address is guaranteed to be unique. */
+	qsort((void *)addrs, cnt, sizeof(__u64), cmp_u64);
+	/* The addresses in /proc/kallsyms are already sorted. */
+	while (fgets(buff, sizeof(buff), fp)) {
+		if (sscanf(buff, "%llx %*c %s", &addr, sym) != 2)
+			continue;
+		/* The addr probed by kprobe_multi is always in
+		 * /proc/kallsyms, so we can ignore some edge cases.

tools/bpf/bpftool/link.c static int get_prog_info(int prog_id, struct bpf_prog_info *info) · line 217

Quoted 10 lines
tools/bpf/bpftool/link.c +10 -0
+		 */
+		if (addr != addrs[i])
+			continue;
+		if (indent)
+			kprobe_multi_print_plain(addr, sym, indent);
+		else
+			kprobe_multi_print_json(addr, sym);
+		i++;
+	}
+	fclose(fp);

There is kernel_syms_load().
Let's reuse it instead of reimplementing kallsysm parsing?

#8 lore raw

Re: [PATCH bpf-next 2/6] bpftool: Show probed function in kprobe_multi link info

Quoted 73 lines · Alexei Starovoitov
Quoted 71 lines · Yafang Shao

Show the already expose kprobe_multi link info in bpftool. The result as follows,

$ tools/bpf/bpftool/bpftool link show
1: kprobe_multi  prog 5
        func_cnt 4  addrs            symbols
                    ffffffffb4d465b0 schedule_timeout_interruptible
                    ffffffffb4d465f0 schedule_timeout_killable
                    ffffffffb4d46630 schedule_timeout_uninterruptible
                    ffffffffb4d46670 schedule_timeout_idle
        pids trace(8729)
$ tools/bpf/bpftool/bpftool link show -j
[{"id":1,"type":"kprobe_multi","prog_id":5,"func_cnt":4,"addrs":[{"addr":18446744072448402864,"symbol":"schedule_timeout_interruptible"},{"addr":18446744072448402928,"symbol":"schedule_timeout_killable"},{"addr":18446744072448402992,"symbol":"schedule_timeout_uninterruptible"},{"addr":18446744072448403056,"symbol":"schedule_timeout_idle"}],"pids":[{"pid":8729,"comm":"trace"}]}]
---
 tools/bpf/bpftool/link.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 94 insertions(+)
tools/bpf/bpftool/link.c +44 -0
index 2d78607..3b00c07 100644
@@ -166,6 +166,57 @@ static int get_prog_info(int prog_id, struct bpf_prog_info *info)
      return err;
 }
 
+static int cmp_u64(const void *A, const void *B)
+{
+     const __u64 *a = A, *b = B;
+
+     return *a - *b;
+}
+
+static void kprobe_multi_print_plain(__u64 addr, char *sym, __u32 indent)
+{
+     printf("\n\t%*s  %0*llx %s", indent, "", 16, addr, sym);
+}
+
+static void kprobe_multi_print_json(__u64 addr, char *sym)
+{
+     jsonw_start_object(json_wtr);
+     jsonw_uint_field(json_wtr, "addr", addr);
+     jsonw_string_field(json_wtr, "symbol", sym);
+     jsonw_end_object(json_wtr);
+}
+
+static void kernel_syms_show(const __u64 *addrs, __u32 cnt, __u32 indent)
+{
+     char buff[256], sym[256];
+     __u64 addr;
+     int i = 0;
+     FILE *fp;
+
+     fp = fopen("/proc/kallsyms", "r");
+     if (!fp)
+             return;
+
+     /* Each address is guaranteed to be unique. */
+     qsort((void *)addrs, cnt, sizeof(__u64), cmp_u64);
+     /* The addresses in /proc/kallsyms are already sorted. */
+     while (fgets(buff, sizeof(buff), fp)) {
+             if (sscanf(buff, "%llx %*c %s", &addr, sym) != 2)
+                     continue;
+             /* The addr probed by kprobe_multi is always in
+              * /proc/kallsyms, so we can ignore some edge cases.
+              */
+             if (addr != addrs[i])
+                     continue;
+             if (indent)
+                     kprobe_multi_print_plain(addr, sym, indent);
Quoted 8 lines

tools/bpf/bpftool/link.c static int get_prog_info(int prog_id, struct bpf_prog_info *info) · line 217

Quoted 5 lines
tools/bpf/bpftool/link.c +5 -0
+             else
+                     kprobe_multi_print_json(addr, sym);
+             i++;
+     }
+     fclose(fp);

There is kernel_syms_load().
Let's reuse it instead of reimplementing kallsysm parsing?

I will think about it. Thanks for your suggestion.

Signature · 2 lines
Regards
Yafang
#9 lore raw

Re: [PATCH bpf-next 2/6] bpftool: Show probed function in kprobe_multi link info

In reply to Yafang Shao
Quoted 12 lines · Yafang Shao

Show the already expose kprobe_multi link info in bpftool. The result as follows,

$ tools/bpf/bpftool/bpftool link show
1: kprobe_multi  prog 5
        func_cnt 4  addrs            symbols
                    ffffffffb4d465b0 schedule_timeout_interruptible
                    ffffffffb4d465f0 schedule_timeout_killable
                    ffffffffb4d46630 schedule_timeout_uninterruptible
                    ffffffffb4d46670 schedule_timeout_idle
        pids trace(8729)
Quoted 2 lines
$ tools/bpf/bpftool/bpftool link show -j
[{"id":1,"type":"kprobe_multi","prog_id":5,"func_cnt":4,"addrs":[{"addr":18446744072448402864,"symbol":"schedule_timeout_interruptible"},{"addr":18446744072448402928,"symbol":"schedule_timeout_killable"},{"addr":18446744072448402992,"symbol":"schedule_timeout_uninterruptible"},{"addr":18446744072448403056,"symbol":"schedule_timeout_idle"}],"pids":[{"pid":8729,"comm":"trace"}]}]

probably a good idea to also show whether it's retprobe or not?

Quoted 149 lines
---
 tools/bpf/bpftool/link.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 94 insertions(+)
tools/bpf/bpftool/link.c +94 -1
index 2d78607..3b00c07 100644
@@ -166,6 +166,57 @@ static int get_prog_info(int prog_id, struct bpf_prog_info *info)
        return err;
 }
 
+static int cmp_u64(const void *A, const void *B)
+{
+       const __u64 *a = A, *b = B;
+
+       return *a - *b;
+}
+
+static void kprobe_multi_print_plain(__u64 addr, char *sym, __u32 indent)
+{
+       printf("\n\t%*s  %0*llx %s", indent, "", 16, addr, sym);
+}
+
+static void kprobe_multi_print_json(__u64 addr, char *sym)
+{
+       jsonw_start_object(json_wtr);
+       jsonw_uint_field(json_wtr, "addr", addr);
+       jsonw_string_field(json_wtr, "symbol", sym);
+       jsonw_end_object(json_wtr);
+}
+
+static void kernel_syms_show(const __u64 *addrs, __u32 cnt, __u32 indent)
+{
+       char buff[256], sym[256];
+       __u64 addr;
+       int i = 0;
+       FILE *fp;
+
+       fp = fopen("/proc/kallsyms", "r");
+       if (!fp)
+               return;
+
+       /* Each address is guaranteed to be unique. */
+       qsort((void *)addrs, cnt, sizeof(__u64), cmp_u64);
+       /* The addresses in /proc/kallsyms are already sorted. */
+       while (fgets(buff, sizeof(buff), fp)) {
+               if (sscanf(buff, "%llx %*c %s", &addr, sym) != 2)
+                       continue;
+               /* The addr probed by kprobe_multi is always in
+                * /proc/kallsyms, so we can ignore some edge cases.
+                */
+               if (addr != addrs[i])
+                       continue;
+               if (indent)
+                       kprobe_multi_print_plain(addr, sym, indent);
+               else
+                       kprobe_multi_print_json(addr, sym);
+               i++;
+       }
+       fclose(fp);
+}
+
 static int show_link_close_json(int fd, struct bpf_link_info *info)
 {
        struct bpf_prog_info prog_info;
@@ -218,6 +269,17 @@ static int show_link_close_json(int fd, struct bpf_link_info *info)
                jsonw_uint_field(json_wtr, "map_id",
                                 info->struct_ops.map_id);
                break;
+       case BPF_LINK_TYPE_KPROBE_MULTI:
+               const __u64 *addrs;
+
+               jsonw_uint_field(json_wtr, "func_cnt", info->kprobe_multi.count);
+               jsonw_name(json_wtr, "addrs");
+               jsonw_start_array(json_wtr);
+               addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs);
+               if (info->kprobe_multi.count)
+                       kernel_syms_show(addrs, info->kprobe_multi.count, 0);
+               jsonw_end_array(json_wtr);
+               break;
        default:
                break;
        }
@@ -396,6 +458,20 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info)
        case BPF_LINK_TYPE_NETFILTER:
                netfilter_dump_plain(info);
                break;
+       case BPF_LINK_TYPE_KPROBE_MULTI:
+               __u32 indent, cnt, i;
+               const __u64 *addrs;
+
+               cnt = info->kprobe_multi.count;
+               if (!cnt)
+                       break;
+               printf("\n\tfunc_cnt %d  %-16s %s", cnt, "addrs", "symbols");
+               for (i = 0; cnt; i++)
+                       cnt /= 10;
+               indent = strlen("func_cnt ") + i;
+               addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs);
+               kernel_syms_show(addrs, cnt, indent);
+               break;
        default:
                break;
        }
@@ -417,7 +493,9 @@ static int do_show_link(int fd)
 {
        struct bpf_link_info info;
        __u32 len = sizeof(info);
+       __u64 *addrs = NULL;
        char buf[256];
+       int count;
        int err;
 
        memset(&info, 0, sizeof(info));
@@ -441,12 +519,28 @@ static int do_show_link(int fd)
                info.iter.target_name_len = sizeof(buf);
                goto again;
        }
+       if (info.type == BPF_LINK_TYPE_KPROBE_MULTI &&
+           !info.kprobe_multi.addrs) {
+               count = info.kprobe_multi.count;
+               if (count) {
+                       addrs = calloc(count, sizeof(__u64));
+                       if (!addrs) {
+                               p_err("mem alloc failed");
+                               close(fd);
+                               return -1;
+                       }
+                       info.kprobe_multi.addrs = (unsigned long)addrs;
+                       goto again;
+               }
+       }
 
        if (json_output)
                show_link_close_json(fd, &info);
        else
                show_link_close_plain(fd, &info);
 
+       if (addrs)
+               free(addrs);
        close(fd);
        return 0;
 }
--

1.8.3.1

#10 lore raw

Re: [PATCH bpf-next 2/6] bpftool: Show probed function in kprobe_multi link info

Quoted 14 lines · Andrii Nakryiko
Quoted 12 lines · Yafang Shao

Show the already expose kprobe_multi link info in bpftool. The result as follows,

$ tools/bpf/bpftool/bpftool link show
1: kprobe_multi  prog 5
        func_cnt 4  addrs            symbols
                    ffffffffb4d465b0 schedule_timeout_interruptible
                    ffffffffb4d465f0 schedule_timeout_killable
                    ffffffffb4d46630 schedule_timeout_uninterruptible
                    ffffffffb4d46670 schedule_timeout_idle
        pids trace(8729)
Quoted 5 lines
Quoted 2 lines
$ tools/bpf/bpftool/bpftool link show -j
[{"id":1,"type":"kprobe_multi","prog_id":5,"func_cnt":4,"addrs":[{"addr":18446744072448402864,"symbol":"schedule_timeout_interruptible"},{"addr":18446744072448402928,"symbol":"schedule_timeout_killable"},{"addr":18446744072448402992,"symbol":"schedule_timeout_uninterruptible"},{"addr":18446744072448403056,"symbol":"schedule_timeout_idle"}],"pids":[{"pid":8729,"comm":"trace"}]}]

probably a good idea to also show whether it's retprobe or not?

Good point. Will add it.

Signature · 2 lines
Regards
Yafang
#11 lore raw

PATCH 3/6 bpf: Always expose the probed address

In reply to Yafang Shao

Since different symbols can share the same name, it is insufficient to only expose the symbol name. It is essential to also expose the symbol address so that users can accurately identify which one is being probed.

---
 kernel/trace/trace_kprobe.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/trace/trace_kprobe.c +1 -1
index 59cda19..a7a905a 100644
@@ -1547,7 +1547,7 @@ int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type,
 	if (tk->symbol) {
 		*symbol = tk->symbol;
 		*probe_offset = tk->rp.kp.offset;
-		*probe_addr = 0;
+		*probe_addr = (unsigned long)tk->rp.kp.addr;
 	} else {
 		*symbol = NULL;
 		*probe_offset = 0;
Signature · 1 line
1.8.3.1
#12 lore raw

PATCH 4/6 bpf: Add a common helper bpf_copy_to_user()

In reply to Yafang Shao

Add a common helper bpf_copy_to_user(), which will be used at multiple places.

---
 kernel/bpf/syscall.c | 34 ++++++++++++++++++++--------------
 1 file changed, 20 insertions(+), 14 deletions(-)
kernel/bpf/syscall.c +20 -14
index 92a57ef..80c9ec0 100644
@@ -3234,6 +3234,25 @@ static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
 		   raw_tp_link->btp->tp->name);
 }
 
+static int bpf_copy_to_user(char __user *ubuf, const char *buf, u32 ulen,
+			    u32 len)
+{
+	if (ulen >= len + 1) {
+		if (copy_to_user(ubuf, buf, len + 1))
+			return -EFAULT;
+	} else {
+		char zero = '\0';
+
+		if (copy_to_user(ubuf, buf, ulen - 1))
+			return -EFAULT;
+		if (put_user(zero, ubuf + ulen - 1))
+			return -EFAULT;
+		return -ENOSPC;
+	}
+
+	return 0;
+}
+
 static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
 					  struct bpf_link_info *info)
 {
@@ -3252,20 +3271,7 @@ static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
 	if (!ubuf)
 		return 0;
 
-	if (ulen >= tp_len + 1) {
-		if (copy_to_user(ubuf, tp_name, tp_len + 1))
-			return -EFAULT;
-	} else {
-		char zero = '\0';
-
-		if (copy_to_user(ubuf, tp_name, ulen - 1))
-			return -EFAULT;
-		if (put_user(zero, ubuf + ulen - 1))
-			return -EFAULT;
-		return -ENOSPC;
-	}
-
-	return 0;
+	return bpf_copy_to_user(ubuf, tp_name, ulen, tp_len);
 }
 
 static const struct bpf_link_ops bpf_raw_tp_link_lops = {
Signature · 1 line
1.8.3.1
#13 lore raw

PATCH 5/6 bpf: Support ->fill_link_info for perf_event

In reply to Yafang Shao

By adding support for ->fill_link_info to the perf_event link, users will be able to inspect it using bpftool link show. While users can currently access this information via bpftool perf show, consolidating the link information for all link types in one place would be more convenient.

---
 include/uapi/linux/bpf.h       |  6 ++++++
 kernel/bpf/syscall.c           | 45 ++++++++++++++++++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h |  6 ++++++
 3 files changed, 57 insertions(+)
include/uapi/linux/bpf.h +6 -0
index 22c8168..87ecf8b 100644
@@ -6442,6 +6442,12 @@ struct bpf_link_info {
 			__u64 addrs;
 			__u32 count;
 		} kprobe_multi;
+		struct {
+			__aligned_u64 name; /* in/out: symbol name buffer ptr */
+			__u64 addr;
+			__u32 name_len;
+			__u32 offset;
+		} perf_event;
 	};
 } __attribute__((aligned(8)));
 
kernel/bpf/syscall.c +45 -0

54 lines not on this page · raw message

tools/include/uapi/linux/bpf.h +6 -0
index 22c8168..87ecf8b 100644
@@ -6442,6 +6442,12 @@ struct bpf_link_info {
 			__u64 addrs;
 			__u32 count;
 		} kprobe_multi;
+		struct {
+			__aligned_u64 name; /* in/out: symbol name buffer ptr */
+			__u64 addr;
+			__u32 name_len;
+			__u32 offset;
+		} perf_event;
 	};
 } __attribute__((aligned(8)));
 
Signature · 1 line
1.8.3.1
#14 lore raw

Re: [PATCH bpf-next 5/6] bpf: Support ->fill_link_info for perf_event

Quoted 18 lines · Yafang Shao

By adding support for ->fill_link_info to the perf_event link, users will be able to inspect it using bpftool link show. While users can currently access this information via bpftool perf show, consolidating the link information for all link types in one place would be more convenient.

---
 include/uapi/linux/bpf.h       |  6 ++++++
 kernel/bpf/syscall.c           | 45 ++++++++++++++++++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h |  6 ++++++
 3 files changed, 57 insertions(+)
include/uapi/linux/bpf.h +0 -0
index 22c8168..87ecf8b 100644

include/uapi/linux/bpf.h struct bpf_link_info { · line 6450

Quoted 9 lines
include/uapi/linux/bpf.h +6 -0
@@ -6442,6 +6442,12 @@ struct bpf_link_info {
                        __u64 addrs;
                        __u32 count;
                } kprobe_multi;
+               struct {
+                       __aligned_u64 name; /* in/out: symbol name buffer ptr */
+                       __u64 addr;
+                       __u32 name_len;
+                       __u32 offset;
+               } perf_event;

perf_event link could be:

a) uprobe
b) kprobe
c) tracepoint
d) generic perf_event (e.g., cpu_cycles)

This interface doesn't make it very clear which one it is. And what's "name" for cpu_cycles event? What is the name for uprobe? Let's actually document this, otherwise it's hard to understand how this information can be interpreted...

Quoted 11 lines
include/uapi/linux/bpf.h +0 -0
        };
 } __attribute__((aligned(8)));
 
kernel/bpf/syscall.c +0 -0
index 80c9ec0..da2de8e 100644
@@ -3303,9 +3303,54 @@ static void bpf_perf_link_dealloc(struct bpf_link *link)
        kfree(perf_link);
 }
 

[...]

#15 lore raw

Re: [PATCH bpf-next 5/6] bpf: Support ->fill_link_info for perf_event

Quoted 37 lines · Andrii Nakryiko
Quoted 18 lines · Yafang Shao

By adding support for ->fill_link_info to the perf_event link, users will be able to inspect it using bpftool link show. While users can currently access this information via bpftool perf show, consolidating the link information for all link types in one place would be more convenient.

---
 include/uapi/linux/bpf.h       |  6 ++++++
 kernel/bpf/syscall.c           | 45 ++++++++++++++++++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h |  6 ++++++
 3 files changed, 57 insertions(+)
include/uapi/linux/bpf.h +0 -0
index 22c8168..87ecf8b 100644

include/uapi/linux/bpf.h struct bpf_link_info { · line 6450

Quoted 9 lines
include/uapi/linux/bpf.h +6 -0
@@ -6442,6 +6442,12 @@ struct bpf_link_info {
                        __u64 addrs;
                        __u32 count;
                } kprobe_multi;
+               struct {
+                       __aligned_u64 name; /* in/out: symbol name buffer ptr */
+                       __u64 addr;
+                       __u32 name_len;
+                       __u32 offset;
+               } perf_event;

perf_event link could be:

a) uprobe
b) kprobe
c) tracepoint
d) generic perf_event (e.g., cpu_cycles)

Quoted 4 lines

This interface doesn't make it very clear which one it is. And what's "name" for cpu_cycles event? What is the name for uprobe? Let's actually document this, otherwise it's hard to understand how this information can be interpreted...

Agreed. It would be better to have a document on it. I will do it.

Signature · 2 lines
Regards
Yafang
#16 lore raw

PATCH 6/6 bpftool: Show probed function in perf_event link info

In reply to Yafang Shao

Show the exposed perf_event link info in bpftool. The result as follows,

$ bpftool link show
1: perf_event  prog 5
        func kernel_clone  addr ffffffffb40bc310  offset 0
        bpf_cookie 0
        pids trace(9726)
$ bpftool link show -j
[{"id":1,"type":"perf_event","prog_id":5,"func":"kernel_clone","addr":18446744072435254032,"offset":0,"bpf_cookie":0,"pids":[{"pid":9726,"comm":"trace"}]}]
---
 tools/bpf/bpftool/link.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
tools/bpf/bpftool/link.c +20 -1
index 3b00c07..045f59f 100644
@@ -280,6 +280,12 @@ static int show_link_close_json(int fd, struct bpf_link_info *info)
 			kernel_syms_show(addrs, info->kprobe_multi.count, 0);
 		jsonw_end_array(json_wtr);
 		break;
+	case BPF_LINK_TYPE_PERF_EVENT:
+		jsonw_string_field(json_wtr, "func",
+				   u64_to_ptr(info->perf_event.name));
+		jsonw_uint_field(json_wtr, "addr", info->perf_event.addr);
+		jsonw_uint_field(json_wtr, "offset", info->perf_event.offset);
+		break;
 	default:
 		break;
 	}
@@ -416,7 +422,7 @@ void netfilter_dump_plain(const struct bpf_link_info *info)
 static int show_link_close_plain(int fd, struct bpf_link_info *info)
 {
 	struct bpf_prog_info prog_info;
-	const char *prog_type_str;
+	const char *prog_type_str, *buf;
 	int err;
 
 	show_link_header_plain(info);
@@ -472,6 +478,12 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info)
 		addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs);
 		kernel_syms_show(addrs, cnt, indent);
 		break;
+	case BPF_LINK_TYPE_PERF_EVENT:
+		buf = (const char *)u64_to_ptr(info->perf_event.name);
+		if (buf[0] != '\0' || info->perf_event.addr)
+			printf("\n\tfunc %s  addr %llx  offset %d  ", buf,
+			       info->perf_event.addr, info->perf_event.offset);
+		break;
 	default:
 		break;
 	}
@@ -498,6 +510,7 @@ static int do_show_link(int fd)
 	int count;
 	int err;
 
+	buf[0] = '\0';
 	memset(&info, 0, sizeof(info));
 again:
 	err = bpf_link_get_info_by_fd(fd, &info, &len);
@@ -533,6 +546,12 @@ static int do_show_link(int fd)
 			goto again;
 		}
 	}
+	if (info.type == BPF_LINK_TYPE_PERF_EVENT &&
+	    !info.perf_event.name) {
+		info.perf_event.name = (unsigned long)&buf;
+		info.perf_event.name_len = sizeof(buf);
+		goto again;
+	}
 
 	if (json_output)
 		show_link_close_json(fd, &info);
Signature · 1 line
1.8.3.1
#17 lore raw

Re: [PATCH bpf-next 6/6] bpftool: Show probed function in perf_event link info

Quoted 42 lines · Yafang Shao

Show the exposed perf_event link info in bpftool. The result as follows,

$ bpftool link show
1: perf_event  prog 5
        func kernel_clone  addr ffffffffb40bc310  offset 0
        bpf_cookie 0
        pids trace(9726)
$ bpftool link show -j
[{"id":1,"type":"perf_event","prog_id":5,"func":"kernel_clone","addr":18446744072435254032,"offset":0,"bpf_cookie":0,"pids":[{"pid":9726,"comm":"trace"}]}]
---
 tools/bpf/bpftool/link.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
tools/bpf/bpftool/link.c +7 -1
index 3b00c07..045f59f 100644
@@ -280,6 +280,12 @@ static int show_link_close_json(int fd, struct bpf_link_info *info)
 			kernel_syms_show(addrs, info->kprobe_multi.count, 0);
 		jsonw_end_array(json_wtr);
 		break;
+	case BPF_LINK_TYPE_PERF_EVENT:
+		jsonw_string_field(json_wtr, "func",
+				   u64_to_ptr(info->perf_event.name));
+		jsonw_uint_field(json_wtr, "addr", info->perf_event.addr);
+		jsonw_uint_field(json_wtr, "offset", info->perf_event.offset);
+		break;
 	default:
 		break;
 	}
@@ -416,7 +422,7 @@ void netfilter_dump_plain(const struct bpf_link_info *info)
 static int show_link_close_plain(int fd, struct bpf_link_info *info)
 {
 	struct bpf_prog_info prog_info;
-	const char *prog_type_str;
+	const char *prog_type_str, *buf;
 	int err;
 
 	show_link_header_plain(info);

tools/bpf/bpftool/link.c static int show_link_close_plain(int fd, struct bpf_link_info *info) · line 485

Quoted 8 lines
tools/bpf/bpftool/link.c +5 -0
@@ -472,6 +478,12 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info)
 		addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs);
 		kernel_syms_show(addrs, cnt, indent);
 		break;
+	case BPF_LINK_TYPE_PERF_EVENT:
+		buf = (const char *)u64_to_ptr(info->perf_event.name);
+		if (buf[0] != '\0' || info->perf_event.addr)
+			printf("\n\tfunc %s  addr %llx  offset %d  ", buf,
+			       info->perf_event.addr, info->perf_event.offset);

Let's print the name here as well?

#18 lore raw

Re: [PATCH bpf-next 6/6] bpftool: Show probed function in perf_event link info

Quoted 42 lines · Alexei Starovoitov
Quoted 40 lines · Yafang Shao

Show the exposed perf_event link info in bpftool. The result as follows,

$ bpftool link show
1: perf_event  prog 5
        func kernel_clone  addr ffffffffb40bc310  offset 0
        bpf_cookie 0
        pids trace(9726)
$ bpftool link show -j
[{"id":1,"type":"perf_event","prog_id":5,"func":"kernel_clone","addr":18446744072435254032,"offset":0,"bpf_cookie":0,"pids":[{"pid":9726,"comm":"trace"}]}]
---
 tools/bpf/bpftool/link.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
tools/bpf/bpftool/link.c +7 -1
index 3b00c07..045f59f 100644
@@ -280,6 +280,12 @@ static int show_link_close_json(int fd, struct bpf_link_info *info)
                      kernel_syms_show(addrs, info->kprobe_multi.count, 0);
              jsonw_end_array(json_wtr);
              break;
+     case BPF_LINK_TYPE_PERF_EVENT:
+             jsonw_string_field(json_wtr, "func",
+                                u64_to_ptr(info->perf_event.name));
+             jsonw_uint_field(json_wtr, "addr", info->perf_event.addr);
+             jsonw_uint_field(json_wtr, "offset", info->perf_event.offset);
+             break;
      default:
              break;
      }
@@ -416,7 +422,7 @@ void netfilter_dump_plain(const struct bpf_link_info *info)
 static int show_link_close_plain(int fd, struct bpf_link_info *info)
 {
      struct bpf_prog_info prog_info;
-     const char *prog_type_str;
+     const char *prog_type_str, *buf;
      int err;
 
Quoted 12 lines

tools/bpf/bpftool/link.c static int show_link_close_plain(int fd, struct bpf_link_info *info) · line 485

Quoted 10 lines
tools/bpf/bpftool/link.c +5 -0
      show_link_header_plain(info);
@@ -472,6 +478,12 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info)
              addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs);
              kernel_syms_show(addrs, cnt, indent);
              break;
+     case BPF_LINK_TYPE_PERF_EVENT:
+             buf = (const char *)u64_to_ptr(info->perf_event.name);
+             if (buf[0] != '\0' || info->perf_event.addr)
+                     printf("\n\tfunc %s  addr %llx  offset %d  ", buf,
+                            info->perf_event.addr, info->perf_event.offset);

Let's print the name here as well?

Sure, I will use 'name' instead.

Signature · 2 lines
Regards
Yafang