Skip to contentSkip to conversation

bpf: Support ->show_fdinfo and ->fill_link_info for kprobe prog

RFC PATCH In review Yafang Shao 5 participants 23 messages May 28, 2023 – May 31, 2023

Subsystems (6)
BPF Tracing BPF core bpftool +2 more subsystems
lore
Share & tools
lore
#0 lore raw

cover bpf: Support ->show_fdinfo and ->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 ->show_fdinfo and ->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 ->show_fdinfo and ->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.

Yafang Shao (8):
bpf: Support ->show_fdinfo for kprobe_multi
bpf: Support ->fill_link_info for kprobe_multi
bpftool: Show probed function in kprobe_multi link info
bpf: Always expose the probed address
bpf: Support ->show_fdinfo for perf_event
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           | 107 +++++++++++++++++++++++++++++++++++------
 kernel/trace/bpf_trace.c       |  48 ++++++++++++++++++
 kernel/trace/trace_kprobe.c    |   2 +-
 tools/bpf/bpftool/link.c       |  71 ++++++++++++++++++++++++++-
 tools/include/uapi/linux/bpf.h |  10 ++++
 6 files changed, 232 insertions(+), 16 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw

RFC PATCH 1/8 bpf: Support ->show_fdinfo for kprobe_multi

Currently, there is no way to check which functions are attached to a kprobe_multi link, causing confusion for users. It is important that we provide a means to expose these functions. The expected result is as follows,

$ cat /proc/10936/fdinfo/9
pos:    0
flags:  02000000
mnt_id: 15
ino:    2094
link_type:      kprobe_multi
link_id:        2
prog_tag:       a04f5eef06a7f555
prog_id:        11
func_count:     4
func_addrs:     ffffffffaad475c0
                ffffffffaad47600
                ffffffffaad47640
                ffffffffaad47680
$ cat /proc/10936/fdinfo/9 | grep "func_addrs" -A 4 | \
  awk '{ if (NR ==1) {print $2} else {print $1}}' | \
  awk '{"grep " $1 " /proc/kallsyms"| getline f; print f}'
ffffffffaad475c0 T schedule_timeout_interruptible
ffffffffaad47600 T schedule_timeout_killable
ffffffffaad47640 T schedule_timeout_uninterruptible
ffffffffaad47680 T schedule_timeout_idle
---
 kernel/trace/bpf_trace.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
kernel/trace/bpf_trace.c +17 -0
index 2bc41e6..0d84a7a 100644
@@ -2548,9 +2548,26 @@ static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link)
 	kfree(kmulti_link);
 }
 
+static void bpf_kprobe_multi_link_show_fdinfo(const struct bpf_link *link,
+				      struct seq_file *seq)
+{
+	struct bpf_kprobe_multi_link *kmulti_link;
+	int i;
+
+	kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
+	seq_printf(seq, "func_count:\t%d\n", kmulti_link->cnt);
+	for (i = 0; i < kmulti_link->cnt; i++) {
+		if (i == 0)
+			seq_printf(seq, "func_addrs:\t%lx\n", kmulti_link->addrs[i]);
+		else
+			seq_printf(seq, "           \t%lx\n", kmulti_link->addrs[i]);
+	}
+}
+
 static const struct bpf_link_ops bpf_kprobe_multi_link_lops = {
 	.release = bpf_kprobe_multi_link_release,
 	.dealloc = bpf_kprobe_multi_link_dealloc,
+	.show_fdinfo = bpf_kprobe_multi_link_show_fdinfo,
 };
 
 static void bpf_kprobe_multi_cookie_swap(void *a, void *b, int size, const void *priv)
Signature · 1 line
1.8.3.1
#2 lore raw

Re: [RFC PATCH bpf-next 1/8] bpf: Support ->show_fdinfo for kprobe_multi

Quoted 4 lines · Yafang Shao

Currently, there is no way to check which functions are attached to a kprobe_multi link, causing confusion for users. It is important that we provide a means to expose these functions. The expected result is as follows,

Quoted 14 lines
$ cat /proc/10936/fdinfo/9
pos:    0
flags:  02000000
mnt_id: 15
ino:    2094
link_type:      kprobe_multi
link_id:        2
prog_tag:       a04f5eef06a7f555
prog_id:        11
func_count:     4
func_addrs:     ffffffffaad475c0
                ffffffffaad47600
                ffffffffaad47640
                ffffffffaad47680

I like the idea of exposing this through the link_info syscall, but I'm bit concerned of potentially dumping thousands of addresses through fdinfo file, because I always thought of fdinfo as brief file info, but that might be just my problem ;-)

jirka

Quoted 48 lines
$ cat /proc/10936/fdinfo/9 | grep "func_addrs" -A 4 | \
  awk '{ if (NR ==1) {print $2} else {print $1}}' | \
  awk '{"grep " $1 " /proc/kallsyms"| getline f; print f}'
ffffffffaad475c0 T schedule_timeout_interruptible
ffffffffaad47600 T schedule_timeout_killable
ffffffffaad47640 T schedule_timeout_uninterruptible
ffffffffaad47680 T schedule_timeout_idle
---
 kernel/trace/bpf_trace.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
kernel/trace/bpf_trace.c +17 -0
index 2bc41e6..0d84a7a 100644
@@ -2548,9 +2548,26 @@ static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link)
 	kfree(kmulti_link);
 }
 
+static void bpf_kprobe_multi_link_show_fdinfo(const struct bpf_link *link,
+				      struct seq_file *seq)
+{
+	struct bpf_kprobe_multi_link *kmulti_link;
+	int i;
+
+	kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
+	seq_printf(seq, "func_count:\t%d\n", kmulti_link->cnt);
+	for (i = 0; i < kmulti_link->cnt; i++) {
+		if (i == 0)
+			seq_printf(seq, "func_addrs:\t%lx\n", kmulti_link->addrs[i]);
+		else
+			seq_printf(seq, "           \t%lx\n", kmulti_link->addrs[i]);
+	}
+}
+
 static const struct bpf_link_ops bpf_kprobe_multi_link_lops = {
 	.release = bpf_kprobe_multi_link_release,
 	.dealloc = bpf_kprobe_multi_link_dealloc,
+	.show_fdinfo = bpf_kprobe_multi_link_show_fdinfo,
 };
 
 static void bpf_kprobe_multi_cookie_swap(void *a, void *b, int size, const void *priv)
Signature · 1 line
1.8.3.1
#3 lore raw

Re: [RFC PATCH bpf-next 1/8] bpf: Support ->show_fdinfo for kprobe_multi

Quoted 21 lines · Jiri Olsa
Quoted 4 lines · Yafang Shao

Currently, there is no way to check which functions are attached to a kprobe_multi link, causing confusion for users. It is important that we provide a means to expose these functions. The expected result is as follows,

Quoted 14 lines
$ cat /proc/10936/fdinfo/9
pos:    0
flags:  02000000
mnt_id: 15
ino:    2094
link_type:      kprobe_multi
link_id:        2
prog_tag:       a04f5eef06a7f555
prog_id:        11
func_count:     4
func_addrs:     ffffffffaad475c0
                ffffffffaad47600
                ffffffffaad47640
                ffffffffaad47680
Quoted 4 lines

I like the idea of exposing this through the link_info syscall, but I'm bit concerned of potentially dumping thousands of addresses through fdinfo file, because I always thought of fdinfo as brief file info, but that might be just my problem ;-)

In most cases, there are only a few addresses, and it is uncommon to have thousands of addresses. To handle this, what about displaying a maximum of 16 addresses? For cases where the number of addresses exceeds 16, we can use '...' to represent the remaining addresses.

Signature · 2 lines
Regards
Yafang
#4 lore raw

Re: [RFC PATCH bpf-next 1/8] bpf: Support ->show_fdinfo for kprobe_multi

Quoted 22 lines · Yafang Shao
Quoted 21 lines · Jiri Olsa
Quoted 18 lines · Yafang Shao

Currently, there is no way to check which functions are attached to a kprobe_multi link, causing confusion for users. It is important that we provide a means to expose these functions. The expected result is as follows,

$ cat /proc/10936/fdinfo/9
pos:    0
flags:  02000000
mnt_id: 15
ino:    2094
link_type:      kprobe_multi
link_id:        2
prog_tag:       a04f5eef06a7f555
prog_id:        11
func_count:     4
func_addrs:     ffffffffaad475c0
                ffffffffaad47600
                ffffffffaad47640
                ffffffffaad47680
Quoted 6 lines
Quoted 4 lines

I like the idea of exposing this through the link_info syscall, but I'm bit concerned of potentially dumping thousands of addresses through fdinfo file, because I always thought of fdinfo as brief file info, but that might be just my problem ;-)

In most cases, there are only a few addresses, and it is uncommon to

I doubt you have data to prove that kprobe_multi is "few addresses in most cases", so please don't throw such arguments without proof.

Quoted 3 lines

have thousands of addresses. To handle this, what about displaying a maximum of 16 addresses? For cases where the number of addresses exceeds 16, we can use '...' to represent the remaining addresses.

at this point the kernel can pick random 16 kernel funcs and it won't be much worse.

Asking users to do

$ cat /proc/10936/fdinfo/9 | grep "func_addrs" -A 4 | \
  awk '{ if (NR ==1) {print $2} else {print $1}}' | \
  awk '{"grep " $1 " /proc/kallsyms"| getline f; print f}'
ffffffffaad475c0 T schedule_timeout_interruptible
ffffffffaad47600 T schedule_timeout_killable

isn't a great interface either.

The proper interface through fill_link_info and bpftool is good to have, but fdinfo shouldn't partially duplicate it. So drop this patch and others.

#5 lore raw

Re: [RFC PATCH bpf-next 1/8] bpf: Support ->show_fdinfo for kprobe_multi

Quoted 48 lines · Alexei Starovoitov
Quoted 22 lines · Yafang Shao
Quoted 21 lines · Jiri Olsa
Quoted 18 lines · Yafang Shao

Currently, there is no way to check which functions are attached to a kprobe_multi link, causing confusion for users. It is important that we provide a means to expose these functions. The expected result is as follows,

$ cat /proc/10936/fdinfo/9
pos:    0
flags:  02000000
mnt_id: 15
ino:    2094
link_type:      kprobe_multi
link_id:        2
prog_tag:       a04f5eef06a7f555
prog_id:        11
func_count:     4
func_addrs:     ffffffffaad475c0
                ffffffffaad47600
                ffffffffaad47640
                ffffffffaad47680
Quoted 6 lines
Quoted 4 lines

I like the idea of exposing this through the link_info syscall, but I'm bit concerned of potentially dumping thousands of addresses through fdinfo file, because I always thought of fdinfo as brief file info, but that might be just my problem ;-)

In most cases, there are only a few addresses, and it is uncommon to

I doubt you have data to prove that kprobe_multi is "few addresses in most cases", so please don't throw such arguments without proof.

Quoted 3 lines

have thousands of addresses. To handle this, what about displaying a maximum of 16 addresses? For cases where the number of addresses exceeds 16, we can use '...' to represent the remaining addresses.

at this point the kernel can pick random 16 kernel funcs and it won't be much worse.

Asking users to do

$ cat /proc/10936/fdinfo/9 | grep "func_addrs" -A 4 | \
  awk '{ if (NR ==1) {print $2} else {print $1}}' | \
  awk '{"grep " $1 " /proc/kallsyms"| getline f; print f}'
ffffffffaad475c0 T schedule_timeout_interruptible
ffffffffaad47600 T schedule_timeout_killable
Quoted 4 lines

isn't a great interface either.

The proper interface through fill_link_info and bpftool is good to have, but fdinfo shouldn't partially duplicate it. So drop this patch and others.

Sure, I will drop the ->show_fdinfo patches.

Signature · 2 lines
Regards
Yafang
#6 lore raw

RFC PATCH 2/8 bpf: Support ->fill_link_info for kprobe_multi

In reply to 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       | 31 +++++++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h |  4 ++++
 3 files changed, 39 insertions(+)
include/uapi/linux/bpf.h +4 -0
index 9273c65..6be9b1d 100644
@@ -6434,6 +6434,10 @@ struct bpf_link_info {
 			__s32 priority;
 			__u32 flags;
 		} netfilter;
+		struct {
+			__aligned_u64 addrs;
+			__u32 count;
+		} kprobe_multi;
 	};
 } __attribute__((aligned(8)));
 
kernel/trace/bpf_trace.c +31 -0
index 0d84a7a..00a0009 100644
@@ -2564,10 +2564,41 @@ static void bpf_kprobe_multi_link_show_fdinfo(const struct bpf_link *link,
 	}
 }
 
+static int bpf_kprobe_multi_link_fill_link_info(const struct bpf_link *link,
+						struct bpf_link_info *info)
+{
+	struct bpf_kprobe_multi_link *kmulti_link;
+	u64 *uaddrs = u64_to_user_ptr(info->kprobe_multi.addrs);
+	u32 ucount = info->kprobe_multi.count;
+	int i;
+
+	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;
+
+	for (i = 0; i < ucount; i++)
+		if (copy_to_user(uaddrs + i, kmulti_link->addrs + i,
+				 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,
 	.show_fdinfo = bpf_kprobe_multi_link_show_fdinfo,
+	.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 9273c65..6be9b1d 100644
@@ -6434,6 +6434,10 @@ struct bpf_link_info {
 			__s32 priority;
 			__u32 flags;
 		} netfilter;
+		struct {
+			__aligned_u64 addrs;
+			__u32 count;
+		} kprobe_multi;
 	};
 } __attribute__((aligned(8)));
 
Signature · 1 line
1.8.3.1
#7 lore raw

Re: [RFC PATCH bpf-next 2/8] bpf: Support ->fill_link_info for kprobe_multi

kernel test robot noticed the following build warnings:

Hi Yafang,

[This is a private test report for your RFC patch.] 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-show_fdinfo-for-kprobe_multi/20230528-222347
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/20230528142027.5585-3-laoar.shao%40gmail.com
patch subject: [RFC PATCH bpf-next 2/8] bpf: Support ->fill_link_info for kprobe_multi
config: x86_64-randconfig-s043-20230528 (https://download.01.org/0day-ci/archive/20230529/[email protected]/config)
compiler: gcc-11 (Debian 11.3.0-12) 11.3.0
reproduce:
        # apt-get install sparse
        # sparse version: v0.6.4-39-gce1a6720-dirty
        # https://github.com/intel-lab-lkp/linux/commit/025a35eef533c935bbcad95fd50bd973538ef4f7
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Yafang-Shao/bpf-Support-show_fdinfo-for-kprobe_multi/20230528-222347
        git checkout 025a35eef533c935bbcad95fd50bd973538ef4f7
        # 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=x86_64 olddefconfig
        make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=x86_64 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:2571:23: sparse: sparse: incorrect type in initializer (different address spaces) @@     expected unsigned long long [usertype] *uaddrs @@     got void [noderef] __user * @@
   kernel/trace/bpf_trace.c:2571:23: sparse:     expected unsigned long long [usertype] *uaddrs
   kernel/trace/bpf_trace.c:2571:23: sparse:     got void [noderef] __user *
>> kernel/trace/bpf_trace.c:2591:41: sparse: sparse: incorrect type in argument 1 (different address spaces) @@     expected void [noderef] __user *to @@     got unsigned long long [usertype] * @@

kernel/trace/bpf_trace.c:2591:41: sparse: expected void [noderef] __user *to
kernel/trace/bpf_trace.c:2591:41: sparse: got unsigned long long [usertype] *
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 +2571 kernel/trace/bpf_trace.c

2566
2567 static int bpf_kprobe_multi_link_fill_link_info(const struct bpf_link *link,
2568 struct bpf_link_info *info)
2569 {
2570 struct bpf_kprobe_multi_link *kmulti_link;

> 2571		u64 *uaddrs = u64_to_user_ptr(info->kprobe_multi.addrs);
  2572		u32 ucount = info->kprobe_multi.count;
  2573		int i;
  2574	
  2575		if (!uaddrs ^ !ucount)
  2576			return -EINVAL;
  2577	
  2578		kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
  2579		if (!uaddrs) {
  2580			info->kprobe_multi.count = kmulti_link->cnt;
  2581			return 0;
  2582		}
  2583	
  2584		if (!ucount)
  2585			return 0;
  2586	
  2587		if (ucount != kmulti_link->cnt)
  2588			return -EINVAL;
  2589	
  2590		for (i = 0; i < ucount; i++)
> 2591			if (copy_to_user(uaddrs + i, kmulti_link->addrs + i,

2592 sizeof(u64)))
2593 return -EFAULT;
2594 return 0;
2595 }
2596

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

Re: [RFC PATCH bpf-next 2/8] bpf: Support ->fill_link_info for kprobe_multi

Quoted 40 lines · kernel test robot

Hi Yafang,

[This is a private test report for your RFC patch.] 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-show_fdinfo-for-kprobe_multi/20230528-222347
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/20230528142027.5585-3-laoar.shao%40gmail.com
patch subject: [RFC PATCH bpf-next 2/8] bpf: Support ->fill_link_info for kprobe_multi
config: x86_64-randconfig-s043-20230528 (https://download.01.org/0day-ci/archive/20230529/[email protected]/config)
compiler: gcc-11 (Debian 11.3.0-12) 11.3.0
reproduce:
        # apt-get install sparse
        # sparse version: v0.6.4-39-gce1a6720-dirty
        # https://github.com/intel-lab-lkp/linux/commit/025a35eef533c935bbcad95fd50bd973538ef4f7
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Yafang-Shao/bpf-Support-show_fdinfo-for-kprobe_multi/20230528-222347
        git checkout 025a35eef533c935bbcad95fd50bd973538ef4f7
        # 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=x86_64 olddefconfig
        make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=x86_64 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 1 line
Quoted 1 line

kernel/trace/bpf_trace.c:2571:23: sparse: sparse: incorrect type in initializer (different address spaces) @@ expected unsigned long long [usertype] *uaddrs @@ got void [noderef] __user * @@

kernel/trace/bpf_trace.c:2571:23: sparse: expected unsigned long long [usertype] *uaddrs
kernel/trace/bpf_trace.c:2571:23: sparse: got void [noderef] __user *

Quoted 1 line
Quoted 1 line

kernel/trace/bpf_trace.c:2591:41: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected void [noderef] __user *to @@ got unsigned long long [usertype] * @@

kernel/trace/bpf_trace.c:2591:41: sparse: expected void [noderef] __user *to
kernel/trace/bpf_trace.c:2591:41: sparse: got unsigned long long [usertype] *
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 8 lines

vim +2571 kernel/trace/bpf_trace.c

2566
2567 static int bpf_kprobe_multi_link_fill_link_info(const struct bpf_link *link,
2568 struct bpf_link_info *info)
2569 {
2570 struct bpf_kprobe_multi_link *kmulti_link;

Quoted 1 line

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

Many thanks for the report!
Will correct it in the next version.

Quoted 29 lines

2572 u32 ucount = info->kprobe_multi.count;
2573 int i;
2574
2575 if (!uaddrs ^ !ucount)
2576 return -EINVAL;
2577
2578 kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
2579 if (!uaddrs) {
2580 info->kprobe_multi.count = kmulti_link->cnt;
2581 return 0;
2582 }
2583
2584 if (!ucount)
2585 return 0;
2586
2587 if (ucount != kmulti_link->cnt)
2588 return -EINVAL;
2589
2590 for (i = 0; i < ucount; i++)

Quoted 1 line

2591 if (copy_to_user(uaddrs + i, kmulti_link->addrs + i,

2592 sizeof(u64)))
2593 return -EFAULT;
2594 return 0;
2595 }
2596

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

Signature · 2 lines
Regards
Yafang
#9 lore raw

Re: [RFC PATCH bpf-next 2/8] bpf: Support ->fill_link_info for kprobe_multi

In reply to Yafang Shao

On Sun, May 28, 2023 at 02:20:21PM +0000, Yafang Shao wrote:

SNIP

Quoted 25 lines
kernel/trace/bpf_trace.c +17 -0
index 0d84a7a..00a0009 100644
@@ -2564,10 +2564,41 @@ static void bpf_kprobe_multi_link_show_fdinfo(const struct bpf_link *link,
 	}
 }
 
+static int bpf_kprobe_multi_link_fill_link_info(const struct bpf_link *link,
+						struct bpf_link_info *info)
+{
+	struct bpf_kprobe_multi_link *kmulti_link;
+	u64 *uaddrs = u64_to_user_ptr(info->kprobe_multi.addrs);
+	u32 ucount = info->kprobe_multi.count;
+	int i;
+
+	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;
+	}
+

kernel/trace/bpf_trace.c static void bpf_kprobe_multi_link_show_fdinfo(const struct bpf_link *link, · line 2593

Quoted 10 lines
kernel/trace/bpf_trace.c +10 -0
+	if (!ucount)
+		return 0;
+
+	if (ucount != kmulti_link->cnt)
+		return -EINVAL;
+
+	for (i = 0; i < ucount; i++)
+		if (copy_to_user(uaddrs + i, kmulti_link->addrs + i,
+				 sizeof(u64)))
+			return -EFAULT;

let's use put_user instead copy_to_user? or even better why not copy that with single copy_to_user from kmulti_link->addrs

jirka

Quoted 29 lines
kernel/trace/bpf_trace.c +4 -0
+	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,
 	.show_fdinfo = bpf_kprobe_multi_link_show_fdinfo,
+	.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 9273c65..6be9b1d 100644
@@ -6434,6 +6434,10 @@ struct bpf_link_info {
 			__s32 priority;
 			__u32 flags;
 		} netfilter;
+		struct {
+			__aligned_u64 addrs;
+			__u32 count;
+		} kprobe_multi;
 	};
 } __attribute__((aligned(8)));
 
Signature · 1 line
1.8.3.1
#10 lore raw

Re: [RFC PATCH bpf-next 2/8] bpf: Support ->fill_link_info for kprobe_multi

Quoted 35 lines · Jiri Olsa

On Sun, May 28, 2023 at 02:20:21PM +0000, Yafang Shao wrote:

SNIP

Quoted 30 lines
kernel/trace/bpf_trace.c +22 -0
index 0d84a7a..00a0009 100644
@@ -2564,10 +2564,41 @@ static void bpf_kprobe_multi_link_show_fdinfo(const struct bpf_link *link,
      }
 }
 
+static int bpf_kprobe_multi_link_fill_link_info(const struct bpf_link *link,
+                                             struct bpf_link_info *info)
+{
+     struct bpf_kprobe_multi_link *kmulti_link;
+     u64 *uaddrs = u64_to_user_ptr(info->kprobe_multi.addrs);
+     u32 ucount = info->kprobe_multi.count;
+     int i;
+
+     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 9 lines

kernel/trace/bpf_trace.c static void bpf_kprobe_multi_link_show_fdinfo(const struct bpf_link *link, · line 2593

Quoted 5 lines
kernel/trace/bpf_trace.c +5 -0
+
+     for (i = 0; i < ucount; i++)
+             if (copy_to_user(uaddrs + i, kmulti_link->addrs + i,
+                              sizeof(u64)))
+                     return -EFAULT;

let's use put_user instead copy_to_user? or even better why not copy that with single copy_to_user from kmulti_link->addrs

Good point. I will utilize a single copy_to_user instead.

Signature · 2 lines
Regards
Yafang
#11 lore raw

RFC PATCH 3/8 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,

$ bpftool link show
2: kprobe_multi  prog 11
        func_cnt 4  addrs ffffffffaad475c0 ffffffffaad47600
                          ffffffffaad47640 ffffffffaad47680
        pids trace(10936)
$ bpftool link show -j
[{"id":1,"type":"perf_event","prog_id":5,"bpf_cookie":0,"pids":[{"pid":10658,"comm":"trace"}]},{"id":2,"type":"kprobe_multi","prog_id":11,"func_cnt":4,"addrs":[18446744072280634816,18446744072280634880,18446744072280634944,18446744072280635008],"pids":[{"pid":10936,"comm":"trace"}]},{"id":120,"type":"iter","prog_id":266,"target_name":"bpf_map"},{"id":121,"type":"iter","prog_id":267,"target_name":"bpf_prog"}]
$ bpftool link show  | grep -A 1 "func_cnt" | \
  awk '{if (NR == 1) {print $4; print $5;} else {print $1; print $2} }' | \
  awk '{"grep " $1 " /proc/kallsyms" | getline f; print f;}'
ffffffffaad475c0 T schedule_timeout_interruptible
ffffffffaad47600 T schedule_timeout_killable
ffffffffaad47640 T schedule_timeout_uninterruptible
ffffffffaad47680 T schedule_timeout_idle
---
 tools/bpf/bpftool/link.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
tools/bpf/bpftool/link.c +50 -0
index 2d78607..76f1bb2 100644
@@ -218,6 +218,20 @@ 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;
+		__u32 i;
+
+		jsonw_uint_field(json_wtr, "func_cnt", info->kprobe_multi.count);
+		if (!info->kprobe_multi.count)
+			break;
+		jsonw_name(json_wtr, "addrs");
+		jsonw_start_array(json_wtr);
+		addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs);
+		for (i = 0; i < info->kprobe_multi.count; i++)
+			jsonw_lluint(json_wtr, addrs[i]);
+		jsonw_end_array(json_wtr);
+		break;
 	default:
 		break;
 	}
@@ -396,6 +410,24 @@ 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  addrs", cnt);
+		for (i = 0; cnt; i++)
+			cnt /= 10;
+		indent = strlen("func_cnt ") + i + strlen("  addrs");
+		addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs);
+		for (i = 0; i < info->kprobe_multi.count; i++) {
+			if (i && !(i & 0x1))
+				printf("\n\t%*s", indent, "");
+			printf(" %0*llx", 16, addrs[i]);
+		}
+		break;
 	default:
 		break;
 	}
@@ -417,7 +449,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 +475,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 = malloc(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
#12 lore raw

Re: [RFC PATCH bpf-next 3/8] bpftool: Show probed function in kprobe_multi link info

2023-05-28 14:20 UTC+0000 ~ Yafang Shao <[email protected]>

Quoted 12 lines

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

$ bpftool link show
2: kprobe_multi  prog 11
        func_cnt 4  addrs ffffffffaad475c0 ffffffffaad47600
                          ffffffffaad47640 ffffffffaad47680
        pids trace(10936)
$ bpftool link show -j
[{"id":1,"type":"perf_event","prog_id":5,"bpf_cookie":0,"pids":[{"pid":10658,"comm":"trace"}]},{"id":2,"type":"kprobe_multi","prog_id":11,"func_cnt":4,"addrs":[18446744072280634816,18446744072280634880,18446744072280634944,18446744072280635008],"pids":[{"pid":10936,"comm":"trace"}]},{"id":120,"type":"iter","prog_id":266,"target_name":"bpf_map"},{"id":121,"type":"iter","prog_id":267,"target_name":"bpf_prog"}]
Quoted 7 lines
$ bpftool link show  | grep -A 1 "func_cnt" | \
  awk '{if (NR == 1) {print $4; print $5;} else {print $1; print $2} }' | \
  awk '{"grep " $1 " /proc/kallsyms" | getline f; print f;}'
ffffffffaad475c0 T schedule_timeout_interruptible
ffffffffaad47600 T schedule_timeout_killable
ffffffffaad47640 T schedule_timeout_uninterruptible
ffffffffaad47680 T schedule_timeout_idle

Looks nice, thank you!

The address is a useful addition, but I feel like most of the time, this is the actual function name that we'd like to see. We could maybe print it directly in bpftool, what do you think? We already parse /proc/kallsyms elsewhere (to get the address of __bpf_call_base()). If we can parse the file only once for all func_cnt function, the overhead is maybe acceptable?

Quoted 11 lines
---
 tools/bpf/bpftool/link.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
tools/bpf/bpftool/link.c +0 -0
index 2d78607..76f1bb2 100644

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

Quoted 10 lines
tools/bpf/bpftool/link.c +7 -0
@@ -218,6 +218,20 @@ 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;
+		__u32 i;
+
+		jsonw_uint_field(json_wtr, "func_cnt", info->kprobe_multi.count);
+		if (!info->kprobe_multi.count)
+			break;

I'd as well avoid having conditional entries in the JSON output. Let's just keep 0 and empty array in this case?

Quoted 46 lines
tools/bpf/bpftool/link.c +27 -0
+		jsonw_name(json_wtr, "addrs");
+		jsonw_start_array(json_wtr);
+		addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs);
+		for (i = 0; i < info->kprobe_multi.count; i++)
+			jsonw_lluint(json_wtr, addrs[i]);
+		jsonw_end_array(json_wtr);
+		break;
 	default:
 		break;
 	}
@@ -396,6 +410,24 @@ 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  addrs", cnt);
+		for (i = 0; cnt; i++)
+			cnt /= 10;
+		indent = strlen("func_cnt ") + i + strlen("  addrs");
+		addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs);
+		for (i = 0; i < info->kprobe_multi.count; i++) {
+			if (i && !(i & 0x1))
+				printf("\n\t%*s", indent, "");
+			printf(" %0*llx", 16, addrs[i]);
+		}
+		break;
 	default:
 		break;
 	}
@@ -417,7 +449,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));

tools/bpf/bpftool/link.c static int do_show_link(int fd) · line 482

Quoted 8 lines
tools/bpf/bpftool/link.c +5 -0
@@ -441,12 +475,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 = malloc(count * sizeof(__u64));

Nit: calloc() instead?

Quoted 10 lines
tools/bpf/bpftool/link.c +9 -0
+			if (!addrs) {
+				p_err("mem alloc failed");
+				close(fd);
+				return -1;
+			}
+			info.kprobe_multi.addrs = (unsigned long)addrs;
+			goto again;
+		}
+	}
 

tools/bpf/bpftool/link.c static int do_show_link(int fd) · line 502

Quoted 10 lines
tools/bpf/bpftool/link.c +2 -0
 	if (json_output)
 		show_link_close_json(fd, &info);
 	else
 		show_link_close_plain(fd, &info);
 
+	if (addrs)
+		free(addrs);
 	close(fd);
 	return 0;
 }

The other bpftool patch (perf_event link) looks good to me.

Thanks,
Quentin

#13 lore raw

Re: [RFC PATCH bpf-next 3/8] bpftool: Show probed function in kprobe_multi link info

Quoted 22 lines · Quentin Monnet

2023-05-28 14:20 UTC+0000 ~ Yafang Shao <[email protected]>

Quoted 12 lines

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

$ bpftool link show
2: kprobe_multi  prog 11
        func_cnt 4  addrs ffffffffaad475c0 ffffffffaad47600
                          ffffffffaad47640 ffffffffaad47680
        pids trace(10936)
$ bpftool link show -j
[{"id":1,"type":"perf_event","prog_id":5,"bpf_cookie":0,"pids":[{"pid":10658,"comm":"trace"}]},{"id":2,"type":"kprobe_multi","prog_id":11,"func_cnt":4,"addrs":[18446744072280634816,18446744072280634880,18446744072280634944,18446744072280635008],"pids":[{"pid":10936,"comm":"trace"}]},{"id":120,"type":"iter","prog_id":266,"target_name":"bpf_map"},{"id":121,"type":"iter","prog_id":267,"target_name":"bpf_prog"}]
Quoted 7 lines
$ bpftool link show  | grep -A 1 "func_cnt" | \
  awk '{if (NR == 1) {print $4; print $5;} else {print $1; print $2} }' | \
  awk '{"grep " $1 " /proc/kallsyms" | getline f; print f;}'
ffffffffaad475c0 T schedule_timeout_interruptible
ffffffffaad47600 T schedule_timeout_killable
ffffffffaad47640 T schedule_timeout_uninterruptible
ffffffffaad47680 T schedule_timeout_idle
Quoted 9 lines

Looks nice, thank you!

The address is a useful addition, but I feel like most of the time, this is the actual function name that we'd like to see. We could maybe print it directly in bpftool, what do you think? We already parse /proc/kallsyms elsewhere (to get the address of __bpf_call_base()). If we can parse the file only once for all func_cnt function, the overhead is maybe acceptable?

Thanks for your suggestion. Will change it.

Quoted 16 lines
Quoted 16 lines
---
 tools/bpf/bpftool/link.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
tools/bpf/bpftool/link.c +2 -0
index 2d78607..76f1bb2 100644
@@ -218,6 +218,20 @@ 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;
Quoted 9 lines

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

Quoted 5 lines
tools/bpf/bpftool/link.c +5 -0
+             __u32 i;
+
+             jsonw_uint_field(json_wtr, "func_cnt", info->kprobe_multi.count);
+             if (!info->kprobe_multi.count)
+                     break;

I'd as well avoid having conditional entries in the JSON output. Let's just keep 0 and empty array in this case?

Will do it.

Quoted 44 lines
Quoted 44 lines
tools/bpf/bpftool/link.c +27 -0
+             jsonw_name(json_wtr, "addrs");
+             jsonw_start_array(json_wtr);
+             addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs);
+             for (i = 0; i < info->kprobe_multi.count; i++)
+                     jsonw_lluint(json_wtr, addrs[i]);
+             jsonw_end_array(json_wtr);
+             break;
      default:
              break;
      }
@@ -396,6 +410,24 @@ 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  addrs", cnt);
+             for (i = 0; cnt; i++)
+                     cnt /= 10;
+             indent = strlen("func_cnt ") + i + strlen("  addrs");
+             addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs);
+             for (i = 0; i < info->kprobe_multi.count; i++) {
+                     if (i && !(i & 0x1))
+                             printf("\n\t%*s", indent, "");
+                     printf(" %0*llx", 16, addrs[i]);
+             }
+             break;
      default:
              break;
      }
@@ -417,7 +449,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;
 
Quoted 12 lines

tools/bpf/bpftool/link.c static int do_show_link(int fd) · line 482

Quoted 10 lines
tools/bpf/bpftool/link.c +5 -0
      memset(&info, 0, sizeof(info));
@@ -441,12 +475,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 = malloc(count * sizeof(__u64));

Nit: calloc() instead?

Good point. Will do it.

Quoted 16 lines
Quoted 16 lines
tools/bpf/bpftool/link.c +9 -0
+                     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);
 
Quoted 8 lines

tools/bpf/bpftool/link.c static int do_show_link(int fd) · line 502

Quoted 5 lines
tools/bpf/bpftool/link.c +2 -0
+     if (addrs)
+             free(addrs);
      close(fd);
      return 0;
 }

The other bpftool patch (perf_event link) looks good to me.

Thanks for your review.

Signature · 2 lines
Regards
Yafang
#14 lore raw

Re: [RFC PATCH bpf-next 3/8] bpftool: Show probed function in kprobe_multi link info

In reply to Yafang Shao
Quoted 9 lines · Yafang Shao

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

$ bpftool link show
2: kprobe_multi  prog 11
        func_cnt 4  addrs ffffffffaad475c0 ffffffffaad47600
                          ffffffffaad47640 ffffffffaad47680
        pids trace(10936)
Quoted 6 lines
$ bpftool link show -j
[{"id":1,"type":"perf_event","prog_id":5,"bpf_cookie":0,"pids":[{"pid":10658,"comm":"trace"}]},{"id":2,"type":"kprobe_multi","prog_id":11,"func_cnt":4,"addrs":[18446744072280634816,18446744072280634880,18446744072280634944,18446744072280635008],"pids":[{"pid":10936,"comm":"trace"}]},{"id":120,"type":"iter","prog_id":266,"target_name":"bpf_map"},{"id":121,"type":"iter","prog_id":267,"target_name":"bpf_prog"}]
$ bpftool link show  | grep -A 1 "func_cnt" | \
  awk '{if (NR == 1) {print $4; print $5;} else {print $1; print $2} }' | \
  awk '{"grep " $1 " /proc/kallsyms" | getline f; print f;}'

This is not human friendly.
Either make bpftool print complete info or don't do it all.

Quoted 102 lines

ffffffffaad475c0 T schedule_timeout_interruptible ffffffffaad47600 T schedule_timeout_killable ffffffffaad47640 T schedule_timeout_uninterruptible ffffffffaad47680 T schedule_timeout_idle

---
 tools/bpf/bpftool/link.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
tools/bpf/bpftool/link.c +50 -0
index 2d78607..76f1bb2 100644
@@ -218,6 +218,20 @@ 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;
+		__u32 i;
+
+		jsonw_uint_field(json_wtr, "func_cnt", info->kprobe_multi.count);
+		if (!info->kprobe_multi.count)
+			break;
+		jsonw_name(json_wtr, "addrs");
+		jsonw_start_array(json_wtr);
+		addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs);
+		for (i = 0; i < info->kprobe_multi.count; i++)
+			jsonw_lluint(json_wtr, addrs[i]);
+		jsonw_end_array(json_wtr);
+		break;
 	default:
 		break;
 	}
@@ -396,6 +410,24 @@ 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  addrs", cnt);
+		for (i = 0; cnt; i++)
+			cnt /= 10;
+		indent = strlen("func_cnt ") + i + strlen("  addrs");
+		addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs);
+		for (i = 0; i < info->kprobe_multi.count; i++) {
+			if (i && !(i & 0x1))
+				printf("\n\t%*s", indent, "");
+			printf(" %0*llx", 16, addrs[i]);
+		}
+		break;
 	default:
 		break;
 	}
@@ -417,7 +449,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 +475,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 = malloc(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
#15 lore raw

Re: [RFC PATCH bpf-next 3/8] bpftool: Show probed function in kprobe_multi link info

Quoted 14 lines · Alexei Starovoitov
Quoted 12 lines · Yafang Shao

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

$ bpftool link show
2: kprobe_multi  prog 11
        func_cnt 4  addrs ffffffffaad475c0 ffffffffaad47600
                          ffffffffaad47640 ffffffffaad47680
        pids trace(10936)
$ bpftool link show -j
[{"id":1,"type":"perf_event","prog_id":5,"bpf_cookie":0,"pids":[{"pid":10658,"comm":"trace"}]},{"id":2,"type":"kprobe_multi","prog_id":11,"func_cnt":4,"addrs":[18446744072280634816,18446744072280634880,18446744072280634944,18446744072280635008],"pids":[{"pid":10936,"comm":"trace"}]},{"id":120,"type":"iter","prog_id":266,"target_name":"bpf_map"},{"id":121,"type":"iter","prog_id":267,"target_name":"bpf_prog"}]
Quoted 7 lines
Quoted 3 lines
$ bpftool link show  | grep -A 1 "func_cnt" | \
  awk '{if (NR == 1) {print $4; print $5;} else {print $1; print $2} }' | \
  awk '{"grep " $1 " /proc/kallsyms" | getline f; print f;}'

This is not human friendly.
Either make bpftool print complete info or don't do it all.

I will make bpftool print complete info in the next version.

Signature · 2 lines
Regards
Yafang
#16 lore raw

RFC PATCH 4/8 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
#17 lore raw

RFC PATCH 5/8 bpf: Support ->show_fdinfo for perf_event

In reply to Yafang Shao

By adding support for ->show_fdinfo to the perf_event link, users will be able to examine it through the task's fdinfo. The expected result is as follows:

$ cat /proc/9637/fdinfo/11
pos:    0
flags:  02000000
mnt_id: 15
ino:    2094
link_type:      perf
link_id:        1
prog_tag:       a04f5eef06a7f555
prog_id:        5
func:   kernel_clone
addr:   ffffffff8d0bc310
offset: 0
---
 kernel/bpf/syscall.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
kernel/bpf/syscall.c +27 -0
index 92a57ef..e6b5127 100644
@@ -3297,9 +3297,36 @@ static void bpf_perf_link_dealloc(struct bpf_link *link)
 	kfree(perf_link);
 }
 
+static void bpf_perf_link_show_fdinfo(const struct bpf_link *link,
+				      struct seq_file *seq)
+{
+	struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
+	const struct perf_event *event;
+	u64 probe_offset, probe_addr;
+	u32 prog_id, fd_type;
+	const char *buf;
+	int err;
+
+	event = perf_get_event(perf_link->perf_file);
+	if (IS_ERR(event))
+		return;
+
+	err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
+				      &buf, &probe_offset,
+				      &probe_addr);
+	if (err)
+		return;
+
+	if (buf)
+		seq_printf(seq, "func:\t%s\n", buf);
+	seq_printf(seq, "addr:\t%llx\n", probe_addr);
+	seq_printf(seq, "offset:\t%llu\n", probe_offset);
+}
+
 static const struct bpf_link_ops bpf_perf_link_lops = {
 	.release = bpf_perf_link_release,
 	.dealloc = bpf_perf_link_dealloc,
+	.show_fdinfo = bpf_perf_link_show_fdinfo,
 };
 
 static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
Signature · 1 line
1.8.3.1
#18 lore raw

RFC PATCH 6/8 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 e6b5127..33a72ec 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
#19 lore raw

RFC PATCH 7/8 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           | 46 ++++++++++++++++++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h |  6 ++++++
 3 files changed, 58 insertions(+)
include/uapi/linux/bpf.h +6 -0
index 6be9b1d..1f2be1d 100644
@@ -6438,6 +6438,12 @@ struct bpf_link_info {
 			__aligned_u64 addrs;
 			__u32 count;
 		} kprobe_multi;
+		struct {
+			__aligned_u64 name;
+			__aligned_u64 addr;
+			__u32 name_len;
+			__u32 offset;
+		} perf_event;
 	};
 } __attribute__((aligned(8)));
 
kernel/bpf/syscall.c +46 -0

56 lines not on this page · raw message

tools/include/uapi/linux/bpf.h +6 -0
index 6be9b1d..1f2be1d 100644
@@ -6438,6 +6438,12 @@ struct bpf_link_info {
 			__aligned_u64 addrs;
 			__u32 count;
 		} kprobe_multi;
+		struct {
+			__aligned_u64 name;
+			__aligned_u64 addr;
+			__u32 name_len;
+			__u32 offset;
+		} perf_event;
 	};
 } __attribute__((aligned(8)));
 
Signature · 1 line
1.8.3.1
#20 lore raw

Re: [RFC PATCH bpf-next 7/8] bpf: Support ->fill_link_info for perf_event

Quoted 17 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           | 46 ++++++++++++++++++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h |  6 ++++++
 3 files changed, 58 insertions(+)
include/uapi/linux/bpf.h +0 -0
index 6be9b1d..1f2be1d 100644

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

Quoted 6 lines
include/uapi/linux/bpf.h +3 -0
@@ -6438,6 +6438,12 @@ struct bpf_link_info {
 			__aligned_u64 addrs;
 			__u32 count;
 		} kprobe_multi;
+		struct {
+			__aligned_u64 name;
+			__aligned_u64 addr;

__aligned_u64 ? what is the reason?

Quoted 34 lines
include/uapi/linux/bpf.h +3 -0
+			__u32 name_len;
+			__u32 offset;
+		} perf_event;
 	};
 } __attribute__((aligned(8)));
 
kernel/bpf/syscall.c +20 -0
index 33a72ec..b12707e 100644
@@ -3329,10 +3329,56 @@ static void bpf_perf_link_show_fdinfo(const struct bpf_link *link,
 	seq_printf(seq, "offset:\t%llu\n", probe_offset);
 }
 
+static int bpf_perf_link_fill_link_info(const struct bpf_link *link,
+					struct bpf_link_info *info)
+{
+	struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
+	char __user *ubuf = u64_to_user_ptr(info->perf_event.name);
+	u32 ulen = info->perf_event.name_len;
+	const struct perf_event *event;
+	u64 probe_offset, probe_addr;
+	u32 prog_id, fd_type;
+	const char *buf;
+	size_t len;
+	int err;
+
+	if (!ulen ^ !ubuf)
+		return -EINVAL;
+	if (!ubuf)
+		return 0;
+
+	event = perf_get_event(perf_link->perf_file);
+	if (IS_ERR(event))

kernel/bpf/syscall.c static void bpf_perf_link_show_fdinfo(const struct bpf_link *link, · line 3361

Quoted 10 lines
kernel/bpf/syscall.c +10 -0
+		return PTR_ERR(event);
+
+	err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
+				      &buf, &probe_offset,
+				      &probe_addr);
+	if (err)
+		return err;
+
+	len = strlen(buf);
+	info->perf_event.name_len = len + 1;

this use of name_len contradicts with patch 8.
Is it 'in' or 'out' field?

Quoted 43 lines
kernel/bpf/syscall.c +16 -0
+	if (buf) {
+		err = bpf_copy_to_user(ubuf, buf, ulen, len);
+		if (err)
+			return err;
+	} else {
+		char zero = '\0';
+
+		if (put_user(zero, ubuf))
+			return -EFAULT;
+	}
+	info->perf_event.addr = probe_addr;
+	info->perf_event.offset = probe_offset;
+	return 0;
+}
+
 static const struct bpf_link_ops bpf_perf_link_lops = {
 	.release = bpf_perf_link_release,
 	.dealloc = bpf_perf_link_dealloc,
 	.show_fdinfo = bpf_perf_link_show_fdinfo,
+	.fill_link_info = bpf_perf_link_fill_link_info,
 };
 
 static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
tools/include/uapi/linux/bpf.h +6 -0
index 6be9b1d..1f2be1d 100644
@@ -6438,6 +6438,12 @@ struct bpf_link_info {
 			__aligned_u64 addrs;
 			__u32 count;
 		} kprobe_multi;
+		struct {
+			__aligned_u64 name;
+			__aligned_u64 addr;
+			__u32 name_len;
+			__u32 offset;
+		} perf_event;
 	};
 } __attribute__((aligned(8)));
 
Signature · 1 line
1.8.3.1
#21 lore raw

Re: [RFC PATCH bpf-next 7/8] bpf: Support ->fill_link_info for perf_event

Quoted 14 lines · Alexei Starovoitov
Quoted 12 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           | 46 ++++++++++++++++++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h |  6 ++++++
 3 files changed, 58 insertions(+)
Quoted 13 lines

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

Quoted 11 lines
include/uapi/linux/bpf.h +3 -0
index 6be9b1d..1f2be1d 100644
@@ -6438,6 +6438,12 @@ struct bpf_link_info {
                      __aligned_u64 addrs;
                      __u32 count;
              } kprobe_multi;
+             struct {
+                     __aligned_u64 name;
+                     __aligned_u64 addr;

__aligned_u64 ? what is the reason?

It is because of the copy-and-paste. Will use _u64 instead.

Quoted 40 lines
Quoted 40 lines
include/uapi/linux/bpf.h +3 -0
+                     __u32 name_len;
+                     __u32 offset;
+             } perf_event;
      };
 } __attribute__((aligned(8)));
 
kernel/bpf/syscall.c +25 -0
index 33a72ec..b12707e 100644
@@ -3329,10 +3329,56 @@ static void bpf_perf_link_show_fdinfo(const struct bpf_link *link,
      seq_printf(seq, "offset:\t%llu\n", probe_offset);
 }
 
+static int bpf_perf_link_fill_link_info(const struct bpf_link *link,
+                                     struct bpf_link_info *info)
+{
+     struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
+     char __user *ubuf = u64_to_user_ptr(info->perf_event.name);
+     u32 ulen = info->perf_event.name_len;
+     const struct perf_event *event;
+     u64 probe_offset, probe_addr;
+     u32 prog_id, fd_type;
+     const char *buf;
+     size_t len;
+     int err;
+
+     if (!ulen ^ !ubuf)
+             return -EINVAL;
+     if (!ubuf)
+             return 0;
+
+     event = perf_get_event(perf_link->perf_file);
+     if (IS_ERR(event))
+             return PTR_ERR(event);
+
+     err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
+                                   &buf, &probe_offset,
+                                   &probe_addr);
Quoted 8 lines

kernel/bpf/syscall.c static void bpf_perf_link_show_fdinfo(const struct bpf_link *link, · line 3361

Quoted 5 lines
kernel/bpf/syscall.c +5 -0
+     if (err)
+             return err;
+
+     len = strlen(buf);
+     info->perf_event.name_len = len + 1;

this use of name_len contradicts with patch 8.
Is it 'in' or 'out' field?

My mistake. I should remove this sentence.
The reason I didn't do it the same with bpf_raw_tp_link_fill_link_info() is that if we return the buf length to the userspace when the ubuf is NULL, we have to call bpf_get_perf_event_info() multiple times.

Signature · 2 lines
Regards
Yafang
#22 lore raw

RFC PATCH 8/8 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 76f1bb2..8493a05 100644
@@ -232,6 +232,12 @@ static int show_link_close_json(int fd, struct bpf_link_info *info)
 			jsonw_lluint(json_wtr, addrs[i]);
 		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;
 	}
@@ -368,7 +374,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);
@@ -428,6 +434,12 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info)
 			printf(" %0*llx", 16, addrs[i]);
 		}
 		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;
 	}
@@ -454,6 +466,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);
@@ -489,6 +502,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