Skip to contentSkip to conversation

bpf: Support ->fill_link_info for kprobe_multi and perf_event links

PATCH Superseded Yafang Shao 5 participants 33 messages Jun 8, 2023 – Jun 11, 2023

Subsystems (7)
BPF Tracing BPF core libbpf +3 more subsystems

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v2 cover bpf: Support ->fill_link_info for kprobe_multi and perf_event links

This patchset enhances the usability of kprobe_multi programs by introducing support for ->fill_link_info. This allows users to easily determine the probed functions associated with a kprobe_multi program. While bpftool perf show already provides information about functions probed by perf_event programs, supporting ->fill_link_info ensures consistent access to this information across all bpf links.

In addition, this patch extends support to generic perf events, which are currently not covered by bpftool perf show. While userspace is exposed to only the perf type and config, other attributes such as sample_period and sample_freq are disregarded.

To ensure accurate identification of probed functions, it is preferable to expose the address directly rather than relying solely on the symbol name. However, this implementation respects the kptr_restrict setting and avoids exposing the address if it is not permitted.

v1->v2:
- Fix sparse warning (Stanislav, [email protected])
- Fix BPF CI build error
- Reuse kernel_syms_load() (Alexei)
- Print 'name' instead of 'func' (Alexei)
- Show whether the probe is retprobe or not (Andrii)
- Add comment for the meaning of perf_event name (Andrii)
- Add support for generic perf event
- Adhere to the kptr_restrict setting

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 (11):
bpf: Support ->fill_link_info for kprobe_multi
bpftool: Add address filtering in kernel_syms_load()
bpftool: Show probed function in kprobe_multi link info
bpf: Protect probed address based on kptr_restrict setting
bpf: Clear the probe_addr for uprobe
bpf: Expose symbol addresses for precise identification
bpf: Add a common helper bpf_copy_to_user()
bpf: Support ->fill_link_info for perf_event
libbpf: Add perf event names
bpftool: Move get_prog_info() into do_show_link()
bpftool: Show probed function in perf_event link info

 include/uapi/linux/bpf.h          |  27 ++++
 kernel/bpf/syscall.c              | 132 +++++++++++++++--
 kernel/trace/bpf_trace.c          |  34 ++++-
 kernel/trace/trace_kprobe.c       |   6 +-
 tools/bpf/bpftool/link.c          | 299 +++++++++++++++++++++++++++++++++++---
 tools/bpf/bpftool/prog.c          |   6 +-
 tools/bpf/bpftool/xlated_dumper.c |  72 +++++++--
 tools/bpf/bpftool/xlated_dumper.h |   3 +-
 tools/include/uapi/linux/bpf.h    |  27 ++++
 tools/lib/bpf/libbpf.c            | 107 ++++++++++++++
 tools/lib/bpf/libbpf.h            |  56 +++++++
 tools/lib/bpf/libbpf.map          |   6 +
 12 files changed, 719 insertions(+), 56 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v2 1/11 bpf: Support ->fill_link_info for kprobe_multi

With the addition of support for fill_link_info to the kprobe_multi link, users will gain the ability to inspect it conveniently using the bpftool link show command. This enhancement provides valuable information to the user, including the count of probed functions and their respective addresses. It's important to note that if the kptr_restrict setting is set to 2, the probed addresses will not be exposed, ensuring security.

---
 include/uapi/linux/bpf.h       |  5 +++++
 kernel/trace/bpf_trace.c       | 30 ++++++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h |  5 +++++
 3 files changed, 40 insertions(+)
include/uapi/linux/bpf.h +5 -0
index a7b5e91..d99cc16 100644
@@ -6438,6 +6438,11 @@ struct bpf_link_info {
 			__s32 priority;
 			__u32 flags;
 		} netfilter;
+		struct {
+			__aligned_u64 addrs; /* in/out: addresses buffer ptr */
+			__u32 count;
+			__u8  retprobe;
+		} kprobe_multi;
 	};
 } __attribute__((aligned(8)));
 
kernel/trace/bpf_trace.c +30 -0
index 2bc41e6..738efcf 100644
@@ -2548,9 +2548,39 @@ 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 __user *uaddrs = 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;
+	info->kprobe_multi.retprobe = kmulti_link->fp.exit_handler ?
+				      true : false;
+	if (kptr_restrict == 2)
+		return 0;
+	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 +5 -0
index a7b5e91..d99cc16 100644
@@ -6438,6 +6438,11 @@ struct bpf_link_info {
 			__s32 priority;
 			__u32 flags;
 		} netfilter;
+		struct {
+			__aligned_u64 addrs; /* in/out: addresses buffer ptr */
+			__u32 count;
+			__u8  retprobe;
+		} kprobe_multi;
 	};
 } __attribute__((aligned(8)));
 
Signature · 1 line
1.8.3.1
#2 lore raw

Re: [PATCH v2 bpf-next 01/11] bpf: Support ->fill_link_info for kprobe_multi

Quoted 20 lines · Yafang Shao

With the addition of support for fill_link_info to the kprobe_multi link, users will gain the ability to inspect it conveniently using the bpftool link show command. This enhancement provides valuable information to the user, including the count of probed functions and their respective addresses. It's important to note that if the kptr_restrict setting is set to 2, the probed addresses will not be exposed, ensuring security.

---
 include/uapi/linux/bpf.h       |  5 +++++
 kernel/trace/bpf_trace.c       | 30 ++++++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h |  5 +++++
 3 files changed, 40 insertions(+)
include/uapi/linux/bpf.h +0 -0
index a7b5e91..d99cc16 100644

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

Quoted 7 lines
include/uapi/linux/bpf.h +4 -0
@@ -6438,6 +6438,11 @@ struct bpf_link_info {
                        __s32 priority;
                        __u32 flags;
                } netfilter;
+               struct {
+                       __aligned_u64 addrs; /* in/out: addresses buffer ptr */
+                       __u32 count;
+                       __u8  retprobe;

from kernel API side it's probably better to just expose flags? retprobe is determined by BPF_F_KPROBE_MULTI_RETURN flag

Quoted 22 lines
include/uapi/linux/bpf.h +1 -0
+               } kprobe_multi;
        };
 } __attribute__((aligned(8)));
 
kernel/trace/bpf_trace.c +10 -0
index 2bc41e6..738efcf 100644
@@ -2548,9 +2548,39 @@ 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 __user *uaddrs = 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;
+

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

Quoted 10 lines
kernel/trace/bpf_trace.c +10 -0
+       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;

should this just check that kmulti_link->cnt is <= ucount?...

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

Quoted 4 lines
kernel/trace/bpf_trace.c +4 -0
+       info->kprobe_multi.retprobe = kmulti_link->fp.exit_handler ?
+                                     true : false;
+       if (kptr_restrict == 2)
+               return 0;

use kallsyms_show_value() instead of hard-coding this?

Quoted 31 lines
kernel/trace/bpf_trace.c +6 -0
+       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 +5 -1
index a7b5e91..d99cc16 100644
@@ -6438,6 +6438,11 @@ struct bpf_link_info {
                        __s32 priority;
                        __u32 flags;
                } netfilter;
+               struct {
+                       __aligned_u64 addrs; /* in/out: addresses buffer ptr */
+                       __u32 count;
+                       __u8  retprobe;
+               } kprobe_multi;
        };
 } __attribute__((aligned(8)));
 
--

1.8.3.1

#3 lore raw

Re: [PATCH v2 bpf-next 01/11] bpf: Support ->fill_link_info for kprobe_multi

Quoted 17 lines · Andrii Nakryiko
Quoted 15 lines · Yafang Shao

With the addition of support for fill_link_info to the kprobe_multi link, users will gain the ability to inspect it conveniently using the bpftool link show command. This enhancement provides valuable information to the user, including the count of probed functions and their respective addresses. It's important to note that if the kptr_restrict setting is set to 2, the probed addresses will not be exposed, ensuring security.

---
 include/uapi/linux/bpf.h       |  5 +++++
 kernel/trace/bpf_trace.c       | 30 ++++++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h |  5 +++++
 3 files changed, 40 insertions(+)
Quoted 14 lines

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

Quoted 12 lines
include/uapi/linux/bpf.h +4 -0
index a7b5e91..d99cc16 100644
@@ -6438,6 +6438,11 @@ struct bpf_link_info {
                        __s32 priority;
                        __u32 flags;
                } netfilter;
+               struct {
+                       __aligned_u64 addrs; /* in/out: addresses buffer ptr */
+                       __u32 count;
+                       __u8  retprobe;

from kernel API side it's probably better to just expose flags?

Agreed. The flags will be extensible.

Quoted 1 line

retprobe is determined by BPF_F_KPROBE_MULTI_RETURN flag

Should we print 'flags' in bpftool link show directly? As we print it in bpftool map show.

Quoted 27 lines
Quoted 27 lines
include/uapi/linux/bpf.h +1 -0
+               } kprobe_multi;
        };
 } __attribute__((aligned(8)));
 
kernel/trace/bpf_trace.c +14 -0
index 2bc41e6..738efcf 100644
@@ -2548,9 +2548,39 @@ 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 __user *uaddrs = 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;
Quoted 8 lines

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

Quoted 6 lines
kernel/trace/bpf_trace.c +6 -0
+       }
+
+       if (!ucount)
+               return 0;
+       if (ucount != kmulti_link->cnt)
+               return -EINVAL;

should this just check that kmulti_link->cnt is <= ucount?...

Agreed.

Quoted 7 lines

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

Quoted 5 lines
kernel/trace/bpf_trace.c +4 -0
+       info->kprobe_multi.retprobe = kmulti_link->fp.exit_handler ?
+                                     true : false;
+       if (kptr_restrict == 2)
+               return 0;

use kallsyms_show_value() instead of hard-coding this?

Good point. Will use it.

Signature · 2 lines
Regards
Yafang
#4 lore raw

Re: [PATCH v2 bpf-next 01/11] bpf: Support ->fill_link_info for kprobe_multi

Quoted 35 lines · Yafang Shao
Quoted 17 lines · Andrii Nakryiko
Quoted 15 lines · Yafang Shao

With the addition of support for fill_link_info to the kprobe_multi link, users will gain the ability to inspect it conveniently using the bpftool link show command. This enhancement provides valuable information to the user, including the count of probed functions and their respective addresses. It's important to note that if the kptr_restrict setting is set to 2, the probed addresses will not be exposed, ensuring security.

---
 include/uapi/linux/bpf.h       |  5 +++++
 kernel/trace/bpf_trace.c       | 30 ++++++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h |  5 +++++
 3 files changed, 40 insertions(+)
Quoted 14 lines

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

Quoted 12 lines
include/uapi/linux/bpf.h +4 -0
index a7b5e91..d99cc16 100644
@@ -6438,6 +6438,11 @@ struct bpf_link_info {
                        __s32 priority;
                        __u32 flags;
                } netfilter;
+               struct {
+                       __aligned_u64 addrs; /* in/out: addresses buffer ptr */
+                       __u32 count;
+                       __u8  retprobe;

from kernel API side it's probably better to just expose flags?

Quoted 6 lines

Agreed. The flags will be extensible.

Quoted 1 line

retprobe is determined by BPF_F_KPROBE_MULTI_RETURN flag

Should we print 'flags' in bpftool link show directly? As we print it in bpftool map show.

specifically for kprobe vs kretprobe (and similarly uprobe vs uretprobe), if bpftool can make it human-readable it would be best. We can also additionally print flags, but I don't know how useful it would be.

Quoted 52 lines
Quoted 28 lines
Quoted 28 lines
include/uapi/linux/bpf.h +1 -0
+               } kprobe_multi;
        };
 } __attribute__((aligned(8)));
 
kernel/trace/bpf_trace.c +14 -0
index 2bc41e6..738efcf 100644
@@ -2548,9 +2548,39 @@ 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 __user *uaddrs = 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;
Quoted 8 lines

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

Quoted 6 lines
kernel/trace/bpf_trace.c +6 -0
+       }
+
+       if (!ucount)
+               return 0;
+       if (ucount != kmulti_link->cnt)
+               return -EINVAL;

should this just check that kmulti_link->cnt is <= ucount?...

Agreed.

Quoted 7 lines

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

Quoted 5 lines
kernel/trace/bpf_trace.c +4 -0
+       info->kprobe_multi.retprobe = kmulti_link->fp.exit_handler ?
+                                     true : false;
+       if (kptr_restrict == 2)
+               return 0;

use kallsyms_show_value() instead of hard-coding this?

Good point. Will use it.

--
Regards
Yafang

#5 lore raw

Re: [PATCH v2 bpf-next 01/11] bpf: Support ->fill_link_info for kprobe_multi

Quoted 41 lines · Andrii Nakryiko
Quoted 39 lines · Yafang Shao
Quoted 17 lines · Andrii Nakryiko
Quoted 15 lines · Yafang Shao

With the addition of support for fill_link_info to the kprobe_multi link, users will gain the ability to inspect it conveniently using the bpftool link show command. This enhancement provides valuable information to the user, including the count of probed functions and their respective addresses. It's important to note that if the kptr_restrict setting is set to 2, the probed addresses will not be exposed, ensuring security.

---
 include/uapi/linux/bpf.h       |  5 +++++
 kernel/trace/bpf_trace.c       | 30 ++++++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h |  5 +++++
 3 files changed, 40 insertions(+)
Quoted 14 lines

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

Quoted 12 lines
include/uapi/linux/bpf.h +4 -0
index a7b5e91..d99cc16 100644
@@ -6438,6 +6438,11 @@ struct bpf_link_info {
                        __s32 priority;
                        __u32 flags;
                } netfilter;
+               struct {
+                       __aligned_u64 addrs; /* in/out: addresses buffer ptr */
+                       __u32 count;
+                       __u8  retprobe;

from kernel API side it's probably better to just expose flags?

Agreed. The flags will be extensible.

Quoted 1 line

retprobe is determined by BPF_F_KPROBE_MULTI_RETURN flag

Quoted 7 lines
Quoted 2 lines

Should we print 'flags' in bpftool link show directly? As we print it in bpftool map show.

specifically for kprobe vs kretprobe (and similarly uprobe vs uretprobe), if bpftool can make it human-readable it would be best. We can also additionally print flags, but I don't know how useful it would be.

Got it. Thanks. Will print 'retprobe' only.

Signature · 2 lines
Regards
Yafang
#6 lore raw

PATCH v2 2/11 bpftool: Add address filtering in kernel_syms_load()

In reply to Yafang Shao

To enhance the functionality of kernel_syms_load(), an extension is proposed to allow filtering of specific addresses of interest. The extension will ensure that the addresses being filtered are present in the /proc/kallsyms file, which serves as a reference for valid kernel symbols. If an address is found to be invalid or not present in /proc/kallsyms, the function will return a value of -1, indicating an error condition.

---
 tools/bpf/bpftool/prog.c          |  6 ++--
 tools/bpf/bpftool/xlated_dumper.c | 72 +++++++++++++++++++++++++++++++--------
 tools/bpf/bpftool/xlated_dumper.h |  3 +-
 3 files changed, 62 insertions(+), 19 deletions(-)
tools/bpf/bpftool/prog.c +3 -3
index 8443a14..116b5b5 100644
@@ -777,7 +777,7 @@ static int do_show(int argc, char **argv)
 			__u32 *lens;
 			__u32 i;
 			if (info->nr_jited_ksyms) {
-				kernel_syms_load(&dd);
+				kernel_syms_load(&dd, NULL, 0);
 				ksyms = u64_to_ptr(info->jited_ksyms);
 			}
 
@@ -841,7 +841,7 @@ static int do_show(int argc, char **argv)
 				goto exit_free;
 		}
 	} else {
-		kernel_syms_load(&dd);
+		kernel_syms_load(&dd, NULL, 0);
 		dd.nr_jited_ksyms = info->nr_jited_ksyms;
 		dd.jited_ksyms = u64_to_ptr(info->jited_ksyms);
 		dd.btf = btf;
@@ -1927,7 +1927,7 @@ static int do_loader(int argc, char **argv)
 	if (verifier_logs) {
 		struct dump_data dd = {};
 
-		kernel_syms_load(&dd);
+		kernel_syms_load(&dd, NULL, 0);
 		dump_xlated_plain(&dd, (void *)gen.insns, gen.insns_sz, false, false);
 		kernel_syms_destroy(&dd);
 	}
tools/bpf/bpftool/xlated_dumper.c +57 -15

103 lines not on this page · raw message

tools/bpf/bpftool/xlated_dumper.h +2 -1
index 9a94637..14ecbd7 100644
@@ -26,7 +26,8 @@ struct dump_data {
 	char scratch_buff[SYM_MAX_NAME + 8];
 };
 
-void kernel_syms_load(struct dump_data *dd);
+int kernel_syms_load(struct dump_data *dd, const __u64 *filter_addrs,
+		     __u32 filter_cnt);
 void kernel_syms_destroy(struct dump_data *dd);
 struct kernel_sym *kernel_syms_search(struct dump_data *dd, unsigned long key);
 void dump_xlated_json(struct dump_data *dd, void *buf, unsigned int len,
Signature · 1 line
1.8.3.1
#7 lore raw

PATCH v2 3/11 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
4: kprobe_multi  prog 29
        retprobe 0  func_cnt 4
        addrs ffffffffb5d475b0  funcs schedule_timeout_interruptible
              ffffffffb5d475f0        schedule_timeout_killable
              ffffffffb5d47630        schedule_timeout_uninterruptible
              ffffffffb5d47670        schedule_timeout_idle
        pids trace(276245)
$ tools/bpf/bpftool/bpftool link show -j
[{"id":4,"type":"kprobe_multi","prog_id":29,"retprobe":0,"func_cnt":4,"funcs":[{"addr":18446744072465184176,"func":"schedule_timeout_interruptible"},{"addr":18446744072465184240,"func":"schedule_timeout_killable"},{"addr":18446744072465184304,"func":"schedule_timeout_uninterruptible"},{"addr":18446744072465184368,"func":"schedule_timeout_idle"}],"pids":[{"pid":276245,"comm":"trace"}]}]
---
 tools/bpf/bpftool/link.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 82 insertions(+)
tools/bpf/bpftool/link.c +82 -0
index 2d78607..c8033c3 100644
@@ -14,6 +14,7 @@
 
 #include "json_writer.h"
 #include "main.h"
+#include "xlated_dumper.h"
 
 static struct hashmap *link_table;
 
@@ -166,6 +167,34 @@ static int get_prog_info(int prog_id, struct bpf_prog_info *info)
 	return err;
 }
 
+static void
+show_kprobe_multi_json(struct bpf_link_info *info, json_writer_t *wtr)
+{
+	struct dump_data dd = {};
+	const __u64 *addrs;
+	__u32 i;
+	int err;
+
+	jsonw_uint_field(json_wtr, "retprobe", info->kprobe_multi.retprobe);
+	jsonw_uint_field(json_wtr, "func_cnt", info->kprobe_multi.count);
+	jsonw_name(json_wtr, "funcs");
+	jsonw_start_array(json_wtr);
+	addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs);
+	err = kernel_syms_load(&dd, addrs, info->kprobe_multi.count);
+	if (err) {
+		jsonw_end_array(json_wtr);
+		return;
+	}
+	for (i = 0; i < dd.sym_count; i++) {
+		jsonw_start_object(json_wtr);
+		jsonw_uint_field(json_wtr, "addr", dd.sym_mapping[i].address);
+		jsonw_string_field(json_wtr, "func", dd.sym_mapping[i].name);
+		jsonw_end_object(json_wtr);
+	}
+	jsonw_end_array(json_wtr);
+	kernel_syms_destroy(&dd);
+}
+
 static int show_link_close_json(int fd, struct bpf_link_info *info)
 {
 	struct bpf_prog_info prog_info;
@@ -218,6 +247,9 @@ 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:
+		show_kprobe_multi_json(info, json_wtr);
+		break;
 	default:
 		break;
 	}
@@ -351,6 +383,35 @@ void netfilter_dump_plain(const struct bpf_link_info *info)
 		printf(" flags 0x%x", info->netfilter.flags);
 }
 
+static void show_kprobe_multi_plain(struct bpf_link_info *info)
+{
+	struct dump_data dd = {};
+	const __u64 *addrs;
+	__u32 i;
+	int err;
+
+	if (!info->kprobe_multi.count)
+		return;
+
+	printf("\n\tretprobe %d  func_cnt %u  ",
+	       info->kprobe_multi.retprobe, info->kprobe_multi.count);
+	addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs);
+	err = kernel_syms_load(&dd, addrs, info->kprobe_multi.count);
+	if (err)
+		return;
+	for (i = 0; i < dd.sym_count; i++) {
+		if (!i)
+			printf("\n\taddrs %016lx  funcs %s  ",
+			       dd.sym_mapping[i].address,
+			       dd.sym_mapping[i].name);
+		else
+			printf("\n\t      %016lx        %s  ",
+			       dd.sym_mapping[i].address,
+			       dd.sym_mapping[i].name);
+	}
+	kernel_syms_destroy(&dd);
+}
+
 static int show_link_close_plain(int fd, struct bpf_link_info *info)
 {
 	struct bpf_prog_info prog_info;
@@ -396,6 +457,9 @@ 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:
+		show_kprobe_multi_plain(info);
+		break;
 	default:
 		break;
 	}
@@ -417,7 +481,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 +507,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
#8 lore raw

Re: [PATCH v2 bpf-next 03/11] bpftool: Show probed function in kprobe_multi link info

Quoted 11 lines · Yafang Shao

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

$ tools/bpf/bpftool/bpftool link show
4: kprobe_multi  prog 29
        retprobe 0  func_cnt 4
        addrs ffffffffb5d475b0  funcs schedule_timeout_interruptible
              ffffffffb5d475f0        schedule_timeout_killable
              ffffffffb5d47630        schedule_timeout_uninterruptible
              ffffffffb5d47670        schedule_timeout_idle

what about module names? kallsyms has this information and it is quite important, in addition to function name

Quoted 155 lines
        pids trace(276245)
$ tools/bpf/bpftool/bpftool link show -j
[{"id":4,"type":"kprobe_multi","prog_id":29,"retprobe":0,"func_cnt":4,"funcs":[{"addr":18446744072465184176,"func":"schedule_timeout_interruptible"},{"addr":18446744072465184240,"func":"schedule_timeout_killable"},{"addr":18446744072465184304,"func":"schedule_timeout_uninterruptible"},{"addr":18446744072465184368,"func":"schedule_timeout_idle"}],"pids":[{"pid":276245,"comm":"trace"}]}]
---
 tools/bpf/bpftool/link.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 82 insertions(+)
tools/bpf/bpftool/link.c +82 -1
index 2d78607..c8033c3 100644
@@ -14,6 +14,7 @@
 
 #include "json_writer.h"
 #include "main.h"
+#include "xlated_dumper.h"
 
 static struct hashmap *link_table;
 
@@ -166,6 +167,34 @@ static int get_prog_info(int prog_id, struct bpf_prog_info *info)
        return err;
 }
 
+static void
+show_kprobe_multi_json(struct bpf_link_info *info, json_writer_t *wtr)
+{
+       struct dump_data dd = {};
+       const __u64 *addrs;
+       __u32 i;
+       int err;
+
+       jsonw_uint_field(json_wtr, "retprobe", info->kprobe_multi.retprobe);
+       jsonw_uint_field(json_wtr, "func_cnt", info->kprobe_multi.count);
+       jsonw_name(json_wtr, "funcs");
+       jsonw_start_array(json_wtr);
+       addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs);
+       err = kernel_syms_load(&dd, addrs, info->kprobe_multi.count);
+       if (err) {
+               jsonw_end_array(json_wtr);
+               return;
+       }
+       for (i = 0; i < dd.sym_count; i++) {
+               jsonw_start_object(json_wtr);
+               jsonw_uint_field(json_wtr, "addr", dd.sym_mapping[i].address);
+               jsonw_string_field(json_wtr, "func", dd.sym_mapping[i].name);
+               jsonw_end_object(json_wtr);
+       }
+       jsonw_end_array(json_wtr);
+       kernel_syms_destroy(&dd);
+}
+
 static int show_link_close_json(int fd, struct bpf_link_info *info)
 {
        struct bpf_prog_info prog_info;
@@ -218,6 +247,9 @@ 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:
+               show_kprobe_multi_json(info, json_wtr);
+               break;
        default:
                break;
        }
@@ -351,6 +383,35 @@ void netfilter_dump_plain(const struct bpf_link_info *info)
                printf(" flags 0x%x", info->netfilter.flags);
 }
 
+static void show_kprobe_multi_plain(struct bpf_link_info *info)
+{
+       struct dump_data dd = {};
+       const __u64 *addrs;
+       __u32 i;
+       int err;
+
+       if (!info->kprobe_multi.count)
+               return;
+
+       printf("\n\tretprobe %d  func_cnt %u  ",
+              info->kprobe_multi.retprobe, info->kprobe_multi.count);
+       addrs = (const __u64 *)u64_to_ptr(info->kprobe_multi.addrs);
+       err = kernel_syms_load(&dd, addrs, info->kprobe_multi.count);
+       if (err)
+               return;
+       for (i = 0; i < dd.sym_count; i++) {
+               if (!i)
+                       printf("\n\taddrs %016lx  funcs %s  ",
+                              dd.sym_mapping[i].address,
+                              dd.sym_mapping[i].name);
+               else
+                       printf("\n\t      %016lx        %s  ",
+                              dd.sym_mapping[i].address,
+                              dd.sym_mapping[i].name);
+       }
+       kernel_syms_destroy(&dd);
+}
+
 static int show_link_close_plain(int fd, struct bpf_link_info *info)
 {
        struct bpf_prog_info prog_info;
@@ -396,6 +457,9 @@ 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:
+               show_kprobe_multi_plain(info);
+               break;
        default:
                break;
        }
@@ -417,7 +481,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 +507,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

#9 lore raw

Re: [PATCH v2 bpf-next 03/11] bpftool: Show probed function in kprobe_multi link info

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

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

$ tools/bpf/bpftool/bpftool link show
4: kprobe_multi  prog 29
        retprobe 0  func_cnt 4
        addrs ffffffffb5d475b0  funcs schedule_timeout_interruptible
              ffffffffb5d475f0        schedule_timeout_killable
              ffffffffb5d47630        schedule_timeout_uninterruptible
              ffffffffb5d47670        schedule_timeout_idle
Quoted 2 lines

what about module names? kallsyms has this information and it is quite important, in addition to function name

Thanks for your suggestion. I will add it.

Signature · 2 lines
Regards
Yafang
#10 lore raw

PATCH v2 4/11 bpf: Protect probed address based on kptr_restrict setting

In reply to Yafang Shao

The probed address can be accessed by userspace through querying the task file descriptor (fd). However, it is crucial to adhere to the kptr_restrict setting and refrain from exposing the address if it is not permitted.

---
 kernel/trace/trace_kprobe.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/trace/trace_kprobe.c +4 -1
index 59cda19..6564541 100644
@@ -1551,7 +1551,10 @@ int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type,
 	} else {
 		*symbol = NULL;
 		*probe_offset = 0;
-		*probe_addr = (unsigned long)tk->rp.kp.addr;
+		if (kptr_restrict != 2)
+			*probe_addr = (unsigned long)tk->rp.kp.addr;
+		else
+			*probe_addr = 0;
 	}
 	return 0;
 }
Signature · 1 line
1.8.3.1
#11 lore raw

Re: [PATCH v2 bpf-next 04/11] bpf: Protect probed address based on kptr_restrict setting

Quoted 15 lines · Yafang Shao

The probed address can be accessed by userspace through querying the task file descriptor (fd). However, it is crucial to adhere to the kptr_restrict setting and refrain from exposing the address if it is not permitted.

---
 kernel/trace/trace_kprobe.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/trace/trace_kprobe.c +0 -0
index 59cda19..6564541 100644

kernel/trace/trace_kprobe.c int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type, · line 1557

Quoted 8 lines
kernel/trace/trace_kprobe.c +4 -1
@@ -1551,7 +1551,10 @@ int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type,
        } else {
                *symbol = NULL;
                *probe_offset = 0;
-               *probe_addr = (unsigned long)tk->rp.kp.addr;
+               if (kptr_restrict != 2)
+                       *probe_addr = (unsigned long)tk->rp.kp.addr;
+               else
+                       *probe_addr = 0;

kallsyms_show_value ?

Quoted 6 lines
kernel/trace/trace_kprobe.c +0 -1
        }
        return 0;
 }
--

1.8.3.1

#12 lore raw

Re: [PATCH v2 bpf-next 04/11] bpf: Protect probed address based on kptr_restrict setting

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

The probed address can be accessed by userspace through querying the task file descriptor (fd). However, it is crucial to adhere to the kptr_restrict setting and refrain from exposing the address if it is not permitted.

---
 kernel/trace/trace_kprobe.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Quoted 15 lines

kernel/trace/trace_kprobe.c int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type, · line 1557

Quoted 13 lines
kernel/trace/trace_kprobe.c +4 -1
index 59cda19..6564541 100644
@@ -1551,7 +1551,10 @@ int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type,
        } else {
                *symbol = NULL;
                *probe_offset = 0;
-               *probe_addr = (unsigned long)tk->rp.kp.addr;
+               if (kptr_restrict != 2)
+                       *probe_addr = (unsigned long)tk->rp.kp.addr;
+               else
+                       *probe_addr = 0;

kallsyms_show_value ?

Will change it.

Signature · 2 lines
Regards
Yafang
#13 lore raw

PATCH v2 5/11 bpf: Clear the probe_addr for uprobe

In reply to Yafang Shao

To avoid returning uninitialized or random values when querying the file descriptor (fd) and accessing probe_addr, it is necessary to clear the variable prior to its use.

Fixes 41bdc4b40ed6 ("bpf: introduce bpf subcommand BPF_TASK_FD_QUERY")
Cc Yonghong Song <[email protected]>
---
 kernel/trace/bpf_trace.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/trace/bpf_trace.c +3 -1
index 738efcf..0cabd2e 100644
@@ -2372,10 +2372,12 @@ int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
 						  event->attr.type == PERF_TYPE_TRACEPOINT);
 #endif
 #ifdef CONFIG_UPROBE_EVENTS
-		if (flags & TRACE_EVENT_FL_UPROBE)
+		if (flags & TRACE_EVENT_FL_UPROBE) {
 			err = bpf_get_uprobe_info(event, fd_type, buf,
 						  probe_offset,
 						  event->attr.type == PERF_TYPE_TRACEPOINT);
+			*probe_addr = 0x0;
+		}
 #endif
 	}
 
Signature · 1 line
1.8.3.1
#14 lore raw

PATCH v2 6/11 bpf: Expose symbol addresses for precise identification

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 | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
kernel/trace/trace_kprobe.c +4 -5
index 6564541..61911e0 100644
@@ -1547,15 +1547,14 @@ 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;
 	} else {
 		*symbol = NULL;
 		*probe_offset = 0;
-		if (kptr_restrict != 2)
-			*probe_addr = (unsigned long)tk->rp.kp.addr;
-		else
-			*probe_addr = 0;
 	}
+	if (kptr_restrict != 2)
+		*probe_addr = (unsigned long)tk->rp.kp.addr;
+	else
+		*probe_addr = 0;
 	return 0;
 }
 #endif	/* CONFIG_PERF_EVENTS */
Signature · 1 line
1.8.3.1
#15 lore raw

PATCH v2 7/11 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.
No functional change.

---
 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
#16 lore raw

PATCH v2 8/11 bpf: Support ->fill_link_info for perf_event

In reply to Yafang Shao

By introducing support for ->fill_link_info to the perf_event link, users gain the ability to inspect it using bpftool link show. While the current approach involves accessing this information via bpftool perf show, consolidating link information for all link types in one place offers greater convenience. Additionally, this patch extends support to the generic perf event, which is not currently accommodated by bpftool perf show. While only the perf type and config are exposed to userspace, other attributes such as sample_period and sample_freq are ignored. It's important to note that if kptr_restrict is set to 2, the probed address will not be exposed, maintaining security measures.

---
 include/uapi/linux/bpf.h       | 22 ++++++++++
 kernel/bpf/syscall.c           | 98 ++++++++++++++++++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h | 22 ++++++++++
 3 files changed, 142 insertions(+)
include/uapi/linux/bpf.h +22 -0
index d99cc16..c3b821d 100644
@@ -6443,6 +6443,28 @@ struct bpf_link_info {
 			__u32 count;
 			__u8  retprobe;
 		} kprobe_multi;
+		union {
+			struct {
+				/* The name is:
+				 * a) uprobe: file name
+				 * b) kprobe: kernel function
+				 */
+				__aligned_u64 name; /* in/out: name buffer ptr */
+				__u32 name_len;
+				__u32 offset;	/* offset from the name */
+				__u64 addr;
+				__u8 retprobe;
+			} probe; /* uprobe, kprobe */
+			struct {
+				/* in/out: tracepoint name buffer ptr */
+				__aligned_u64 tp_name;
+				__u32 name_len;
+			} tp; /* tracepoint */
+			struct {
+				__u64 config;
+				__u32 type;
+			} event; /* generic perf event */
+		} perf_event;
 	};
 } __attribute__((aligned(8)));
 
kernel/bpf/syscall.c +98 -0

107 lines not on this page · raw message

tools/include/uapi/linux/bpf.h +22 -0
index d99cc16..c3b821d 100644
@@ -6443,6 +6443,28 @@ struct bpf_link_info {
 			__u32 count;
 			__u8  retprobe;
 		} kprobe_multi;
+		union {
+			struct {
+				/* The name is:
+				 * a) uprobe: file name
+				 * b) kprobe: kernel function
+				 */
+				__aligned_u64 name; /* in/out: name buffer ptr */
+				__u32 name_len;
+				__u32 offset;	/* offset from the name */
+				__u64 addr;
+				__u8 retprobe;
+			} probe; /* uprobe, kprobe */
+			struct {
+				/* in/out: tracepoint name buffer ptr */
+				__aligned_u64 tp_name;
+				__u32 name_len;
+			} tp; /* tracepoint */
+			struct {
+				__u64 config;
+				__u32 type;
+			} event; /* generic perf event */
+		} perf_event;
 	};
 } __attribute__((aligned(8)));
 
Signature · 1 line
1.8.3.1
#17 lore raw

Re: [PATCH v2 bpf-next 08/11] bpf: Support ->fill_link_info for perf_event

Quoted 38 lines · Yafang Shao

By introducing support for ->fill_link_info to the perf_event link, users gain the ability to inspect it using bpftool link show. While the current approach involves accessing this information via bpftool perf show, consolidating link information for all link types in one place offers greater convenience. Additionally, this patch extends support to the generic perf event, which is not currently accommodated by bpftool perf show. While only the perf type and config are exposed to userspace, other attributes such as sample_period and sample_freq are ignored. It's important to note that if kptr_restrict is set to 2, the probed address will not be exposed, maintaining security measures.

---
 include/uapi/linux/bpf.h       | 22 ++++++++++
 kernel/bpf/syscall.c           | 98 ++++++++++++++++++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h | 22 ++++++++++
 3 files changed, 142 insertions(+)
include/uapi/linux/bpf.h +11 -0
index d99cc16..c3b821d 100644
@@ -6443,6 +6443,28 @@ struct bpf_link_info {
                        __u32 count;
                        __u8  retprobe;
                } kprobe_multi;
+               union {
+                       struct {
+                               /* The name is:
+                                * a) uprobe: file name
+                                * b) kprobe: kernel function
+                                */
+                               __aligned_u64 name; /* in/out: name buffer ptr */
+                               __u32 name_len;
+                               __u32 offset;   /* offset from the name */
+                               __u64 addr;
+                               __u8 retprobe;

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

Quoted 10 lines
include/uapi/linux/bpf.h +10 -0
+                       } probe; /* uprobe, kprobe */
+                       struct {
+                               /* in/out: tracepoint name buffer ptr */
+                               __aligned_u64 tp_name;
+                               __u32 name_len;
+                       } tp; /* tracepoint */
+                       struct {
+                               __u64 config;
+                               __u32 type;
+                       } event; /* generic perf event */

how should the user know which of those structs are relevant? we need some enum to specify what kind of perf_event link it is?

Quoted 4 lines
include/uapi/linux/bpf.h +1 -0
+               } perf_event;
        };
 } __attribute__((aligned(8)));
 

[...]

#18 lore raw

Re: [PATCH v2 bpf-next 08/11] bpf: Support ->fill_link_info for perf_event

Quoted 45 lines · Andrii Nakryiko
Quoted 43 lines · Yafang Shao

By introducing support for ->fill_link_info to the perf_event link, users gain the ability to inspect it using bpftool link show. While the current approach involves accessing this information via bpftool perf show, consolidating link information for all link types in one place offers greater convenience. Additionally, this patch extends support to the generic perf event, which is not currently accommodated by bpftool perf show. While only the perf type and config are exposed to userspace, other attributes such as sample_period and sample_freq are ignored. It's important to note that if kptr_restrict is set to 2, the probed address will not be exposed, maintaining security measures.

---
 include/uapi/linux/bpf.h       | 22 ++++++++++
 kernel/bpf/syscall.c           | 98 ++++++++++++++++++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h | 22 ++++++++++
 3 files changed, 142 insertions(+)
include/uapi/linux/bpf.h +16 -0
index d99cc16..c3b821d 100644
@@ -6443,6 +6443,28 @@ struct bpf_link_info {
                        __u32 count;
                        __u8  retprobe;
                } kprobe_multi;
+               union {
+                       struct {
+                               /* The name is:
+                                * a) uprobe: file name
+                                * b) kprobe: kernel function
+                                */
+                               __aligned_u64 name; /* in/out: name buffer ptr */
+                               __u32 name_len;
+                               __u32 offset;   /* offset from the name */
+                               __u64 addr;
+                               __u8 retprobe;
+                       } probe; /* uprobe, kprobe */
+                       struct {
+                               /* in/out: tracepoint name buffer ptr */
+                               __aligned_u64 tp_name;
+                               __u32 name_len;
Quoted 9 lines

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

Quoted 5 lines
include/uapi/linux/bpf.h +5 -0
+                       } tp; /* tracepoint */
+                       struct {
+                               __u64 config;
+                               __u32 type;
+                       } event; /* generic perf event */

how should the user know which of those structs are relevant? we need some enum to specify what kind of perf_event link it is?

Do you mean that we add a new field 'type' into the union perf_event, as follows ?

    union {
        __u32 type;
        struct {} probe;  /* BPF_LINK_TYPE_PERF_EVENT_PROBE */
        struct {} tp; /* BPF_LINK_TYPE_PERF_EVENT_TP */
        struct {} event; /* BPF_LINK_TYPE_PERF_EVENT_EVENT */
    };
Signature · 2 lines
Regards
Yafang
#19 lore raw

Re: [PATCH v2 bpf-next 08/11] bpf: Support ->fill_link_info for perf_event

Quoted 58 lines · Yafang Shao
Quoted 45 lines · Andrii Nakryiko
Quoted 43 lines · Yafang Shao

By introducing support for ->fill_link_info to the perf_event link, users gain the ability to inspect it using bpftool link show. While the current approach involves accessing this information via bpftool perf show, consolidating link information for all link types in one place offers greater convenience. Additionally, this patch extends support to the generic perf event, which is not currently accommodated by bpftool perf show. While only the perf type and config are exposed to userspace, other attributes such as sample_period and sample_freq are ignored. It's important to note that if kptr_restrict is set to 2, the probed address will not be exposed, maintaining security measures.

---
 include/uapi/linux/bpf.h       | 22 ++++++++++
 kernel/bpf/syscall.c           | 98 ++++++++++++++++++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h | 22 ++++++++++
 3 files changed, 142 insertions(+)
include/uapi/linux/bpf.h +16 -0
index d99cc16..c3b821d 100644
@@ -6443,6 +6443,28 @@ struct bpf_link_info {
                        __u32 count;
                        __u8  retprobe;
                } kprobe_multi;
+               union {
+                       struct {
+                               /* The name is:
+                                * a) uprobe: file name
+                                * b) kprobe: kernel function
+                                */
+                               __aligned_u64 name; /* in/out: name buffer ptr */
+                               __u32 name_len;
+                               __u32 offset;   /* offset from the name */
+                               __u64 addr;
+                               __u8 retprobe;
+                       } probe; /* uprobe, kprobe */
+                       struct {
+                               /* in/out: tracepoint name buffer ptr */
+                               __aligned_u64 tp_name;
+                               __u32 name_len;
Quoted 9 lines

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

Quoted 5 lines
include/uapi/linux/bpf.h +5 -0
+                       } tp; /* tracepoint */
+                       struct {
+                               __u64 config;
+                               __u32 type;
+                       } event; /* generic perf event */

how should the user know which of those structs are relevant? we need some enum to specify what kind of perf_event link it is?

Quoted 9 lines

Do you mean that we add a new field 'type' into the union perf_event, as follows ?

    union {
        __u32 type;
        struct {} probe;  /* BPF_LINK_TYPE_PERF_EVENT_PROBE */
        struct {} tp; /* BPF_LINK_TYPE_PERF_EVENT_TP */
        struct {} event; /* BPF_LINK_TYPE_PERF_EVENT_EVENT */
    };

Correct it:

struct {
    __u32 type;
    union {
         struct {} probe;  /* BPF_LINK_TYPE_PERF_EVENT_PROBE */
         struct {} tp; /* BPF_LINK_TYPE_PERF_EVENT_TP */
         struct {} event; /* BPF_LINK_TYPE_PERF_EVENT_EVENT */
     };
} perf_event;
Signature · 2 lines
Regards
Yafang
#20 lore raw

Re: [PATCH v2 bpf-next 08/11] bpf: Support ->fill_link_info for perf_event

Quoted 72 lines · Yafang Shao
Quoted 58 lines · Yafang Shao
Quoted 45 lines · Andrii Nakryiko
Quoted 43 lines · Yafang Shao

By introducing support for ->fill_link_info to the perf_event link, users gain the ability to inspect it using bpftool link show. While the current approach involves accessing this information via bpftool perf show, consolidating link information for all link types in one place offers greater convenience. Additionally, this patch extends support to the generic perf event, which is not currently accommodated by bpftool perf show. While only the perf type and config are exposed to userspace, other attributes such as sample_period and sample_freq are ignored. It's important to note that if kptr_restrict is set to 2, the probed address will not be exposed, maintaining security measures.

---
 include/uapi/linux/bpf.h       | 22 ++++++++++
 kernel/bpf/syscall.c           | 98 ++++++++++++++++++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h | 22 ++++++++++
 3 files changed, 142 insertions(+)
include/uapi/linux/bpf.h +16 -0
index d99cc16..c3b821d 100644
@@ -6443,6 +6443,28 @@ struct bpf_link_info {
                        __u32 count;
                        __u8  retprobe;
                } kprobe_multi;
+               union {
+                       struct {
+                               /* The name is:
+                                * a) uprobe: file name
+                                * b) kprobe: kernel function
+                                */
+                               __aligned_u64 name; /* in/out: name buffer ptr */
+                               __u32 name_len;
+                               __u32 offset;   /* offset from the name */
+                               __u64 addr;
+                               __u8 retprobe;
+                       } probe; /* uprobe, kprobe */
+                       struct {
+                               /* in/out: tracepoint name buffer ptr */
+                               __aligned_u64 tp_name;
+                               __u32 name_len;
Quoted 9 lines

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

Quoted 5 lines
include/uapi/linux/bpf.h +5 -0
+                       } tp; /* tracepoint */
+                       struct {
+                               __u64 config;
+                               __u32 type;
+                       } event; /* generic perf event */

how should the user know which of those structs are relevant? we need some enum to specify what kind of perf_event link it is?

Quoted 9 lines

Do you mean that we add a new field 'type' into the union perf_event, as follows ?

    union {
        __u32 type;
        struct {} probe;  /* BPF_LINK_TYPE_PERF_EVENT_PROBE */
        struct {} tp; /* BPF_LINK_TYPE_PERF_EVENT_TP */
        struct {} event; /* BPF_LINK_TYPE_PERF_EVENT_EVENT */
    };

Correct it:

Quoted 8 lines
struct {
    __u32 type;
    union {
         struct {} probe;  /* BPF_LINK_TYPE_PERF_EVENT_PROBE */
         struct {} tp; /* BPF_LINK_TYPE_PERF_EVENT_TP */
         struct {} event; /* BPF_LINK_TYPE_PERF_EVENT_EVENT */
     };
} perf_event;

yes, something like this. Unless we want to leave perf_event {} to mean really perf event only, while kprobe/uprobe/tracepoint should be their own separate sections at the same level of nestedness as perf_Event and other cases. Not sure.

Quoted 4 lines

--
Regards
Yafang

#21 lore raw

Re: [PATCH v2 bpf-next 08/11] bpf: Support ->fill_link_info for perf_event

Quoted 83 lines · Andrii Nakryiko
Quoted 72 lines · Yafang Shao
Quoted 58 lines · Yafang Shao
Quoted 45 lines · Andrii Nakryiko
Quoted 43 lines · Yafang Shao

By introducing support for ->fill_link_info to the perf_event link, users gain the ability to inspect it using bpftool link show. While the current approach involves accessing this information via bpftool perf show, consolidating link information for all link types in one place offers greater convenience. Additionally, this patch extends support to the generic perf event, which is not currently accommodated by bpftool perf show. While only the perf type and config are exposed to userspace, other attributes such as sample_period and sample_freq are ignored. It's important to note that if kptr_restrict is set to 2, the probed address will not be exposed, maintaining security measures.

---
 include/uapi/linux/bpf.h       | 22 ++++++++++
 kernel/bpf/syscall.c           | 98 ++++++++++++++++++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h | 22 ++++++++++
 3 files changed, 142 insertions(+)
include/uapi/linux/bpf.h +16 -0
index d99cc16..c3b821d 100644
@@ -6443,6 +6443,28 @@ struct bpf_link_info {
                        __u32 count;
                        __u8  retprobe;
                } kprobe_multi;
+               union {
+                       struct {
+                               /* The name is:
+                                * a) uprobe: file name
+                                * b) kprobe: kernel function
+                                */
+                               __aligned_u64 name; /* in/out: name buffer ptr */
+                               __u32 name_len;
+                               __u32 offset;   /* offset from the name */
+                               __u64 addr;
+                               __u8 retprobe;
+                       } probe; /* uprobe, kprobe */
+                       struct {
+                               /* in/out: tracepoint name buffer ptr */
+                               __aligned_u64 tp_name;
+                               __u32 name_len;
Quoted 9 lines

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

Quoted 5 lines
include/uapi/linux/bpf.h +5 -0
+                       } tp; /* tracepoint */
+                       struct {
+                               __u64 config;
+                               __u32 type;
+                       } event; /* generic perf event */

how should the user know which of those structs are relevant? we need some enum to specify what kind of perf_event link it is?

Quoted 9 lines

Do you mean that we add a new field 'type' into the union perf_event, as follows ?

    union {
        __u32 type;
        struct {} probe;  /* BPF_LINK_TYPE_PERF_EVENT_PROBE */
        struct {} tp; /* BPF_LINK_TYPE_PERF_EVENT_TP */
        struct {} event; /* BPF_LINK_TYPE_PERF_EVENT_EVENT */
    };

Correct it:

Quoted 8 lines
struct {
    __u32 type;
    union {
         struct {} probe;  /* BPF_LINK_TYPE_PERF_EVENT_PROBE */
         struct {} tp; /* BPF_LINK_TYPE_PERF_EVENT_TP */
         struct {} event; /* BPF_LINK_TYPE_PERF_EVENT_EVENT */
     };
} perf_event;
Quoted 5 lines

yes, something like this. Unless we want to leave perf_event {} to mean really perf event only, while kprobe/uprobe/tracepoint should be their own separate sections at the same level of nestedness as perf_Event and other cases. Not sure.

Thanks for your explanation. I will think about it.

Signature · 2 lines
Regards
Yafang
#22 lore raw

PATCH v2 9/11 libbpf: Add perf event names

In reply to Yafang Shao

Add libbpf API to get generic perf event name.

---
 tools/lib/bpf/libbpf.c   | 107 +++++++++++++++++++++++++++++++++++++++++++++++
 tools/lib/bpf/libbpf.h   |  56 +++++++++++++++++++++++++
 tools/lib/bpf/libbpf.map |   6 +++
 3 files changed, 169 insertions(+)
tools/lib/bpf/libbpf.c +107 -0
index 47632606..27d396f 100644
@@ -119,6 +119,64 @@
 	[BPF_STRUCT_OPS]		= "struct_ops",
 };
 
+static const char * const perf_type_name[] = {
+	[PERF_TYPE_HARDWARE]		= "hardware",
+	[PERF_TYPE_SOFTWARE]		= "software",
+	[PERF_TYPE_TRACEPOINT]		= "tracepoint",
+	[PERF_TYPE_HW_CACHE]		= "hw_cache",
+	[PERF_TYPE_RAW]			= "raw",
+	[PERF_TYPE_BREAKPOINT]		= "breakpoint",
+};
+
+static const char * const perf_hw_name[] = {
+	[PERF_COUNT_HW_CPU_CYCLES]		= "cpu_cycles",
+	[PERF_COUNT_HW_INSTRUCTIONS]		= "instructions",
+	[PERF_COUNT_HW_CACHE_REFERENCES]	= "cache_references",
+	[PERF_COUNT_HW_CACHE_MISSES]		= "cache_misses",
+	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS]	= "branch_instructions",
+	[PERF_COUNT_HW_BRANCH_MISSES]		= "branch_misses",
+	[PERF_COUNT_HW_BUS_CYCLES]		= "bus_cycles",
+	[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND]	= "stalled_cycles_frontend",
+	[PERF_COUNT_HW_STALLED_CYCLES_BACKEND]	= "stalled_cycles_backend",
+	[PERF_COUNT_HW_REF_CPU_CYCLES]		= "ref_cpu_cycles",
+};
+
+static const char * const perf_hw_cache_name[] = {
+	[PERF_COUNT_HW_CACHE_L1D]		= "l1d",
+	[PERF_COUNT_HW_CACHE_L1I]		= "l1i",
+	[PERF_COUNT_HW_CACHE_LL]		= "ll",
+	[PERF_COUNT_HW_CACHE_DTLB]		= "dtlb",
+	[PERF_COUNT_HW_CACHE_ITLB]		= "itlb",
+	[PERF_COUNT_HW_CACHE_BPU]		= "bpu",
+	[PERF_COUNT_HW_CACHE_NODE]		= "node",
+};
+
+static const char * const perf_hw_cache_op_name[] = {
+	[PERF_COUNT_HW_CACHE_OP_READ]		= "read",
+	[PERF_COUNT_HW_CACHE_OP_WRITE]		= "write",
+	[PERF_COUNT_HW_CACHE_OP_PREFETCH]	= "prefetch",
+};
+
+static const char * const perf_hw_cache_op_result_name[] = {
+	[PERF_COUNT_HW_CACHE_RESULT_ACCESS]	= "access",
+	[PERF_COUNT_HW_CACHE_RESULT_MISS]	= "miss",
+};
+
+static const char * const perf_sw_name[] = {
+	[PERF_COUNT_SW_CPU_CLOCK]		= "cpu_clock",
+	[PERF_COUNT_SW_TASK_CLOCK]		= "task_clock",
+	[PERF_COUNT_SW_PAGE_FAULTS]		= "page_faults",
+	[PERF_COUNT_SW_CONTEXT_SWITCHES]	= "context_switches",
+	[PERF_COUNT_SW_CPU_MIGRATIONS]		= "cpu_migrations",
+	[PERF_COUNT_SW_PAGE_FAULTS_MIN]		= "page_faults_min",
+	[PERF_COUNT_SW_PAGE_FAULTS_MAJ]		= "page_faults_maj",
+	[PERF_COUNT_SW_ALIGNMENT_FAULTS]	= "alignment_faults",
+	[PERF_COUNT_SW_EMULATION_FAULTS]	= "emulation_faults",
+	[PERF_COUNT_SW_DUMMY]			= "dummy",
+	[PERF_COUNT_SW_BPF_OUTPUT]		= "bpf_output",
+	[PERF_COUNT_SW_CGROUP_SWITCHES]		= "cgroup_switches",
+};
+
 static const char * const link_type_name[] = {
 	[BPF_LINK_TYPE_UNSPEC]			= "unspec",
 	[BPF_LINK_TYPE_RAW_TRACEPOINT]		= "raw_tracepoint",
@@ -8953,6 +9011,55 @@ const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t)
 	return attach_type_name[t];
 }
 
+const char *libbpf_perf_type_str(enum perf_type_id t)
+{
+	if (t < 0 || t >= ARRAY_SIZE(perf_type_name))
+		return NULL;
+
+	return perf_type_name[t];
+}
+
+const char *libbpf_perf_hw_str(enum perf_hw_id t)
+{
+	if (t < 0 || t >= ARRAY_SIZE(perf_hw_name))
+		return NULL;
+
+	return perf_hw_name[t];
+}
+
+const char *libbpf_perf_hw_cache_str(enum perf_hw_cache_id t)
+{
+	if (t < 0 || t >= ARRAY_SIZE(perf_hw_cache_name))
+		return NULL;
+
+	return perf_hw_cache_name[t];
+}
+
+const char *libbpf_perf_hw_cache_op_str(enum perf_hw_cache_op_id t)
+{
+	if (t < 0 || t >= ARRAY_SIZE(perf_hw_cache_op_name))
+		return NULL;
+
+	return perf_hw_cache_op_name[t];
+}
+
+const char *
+libbpf_perf_hw_cache_op_result_str(enum perf_hw_cache_op_result_id t)
+{
+	if (t < 0 || t >= ARRAY_SIZE(perf_hw_cache_op_result_name))
+		return NULL;
+
+	return perf_hw_cache_op_result_name[t];
+}
+
+const char *libbpf_perf_sw_str(enum perf_sw_ids t)
+{
+	if (t < 0 || t >= ARRAY_SIZE(perf_sw_name))
+		return NULL;
+
+	return perf_sw_name[t];
+}
+
 const char *libbpf_bpf_link_type_str(enum bpf_link_type t)
 {
 	if (t < 0 || t >= ARRAY_SIZE(link_type_name))
tools/lib/bpf/libbpf.h +56 -0

68 lines not on this page · raw message

tools/lib/bpf/libbpf.map +6 -0
index 7521a2f..6ae0a36 100644
@@ -395,4 +395,10 @@ LIBBPF_1.2.0 {
 LIBBPF_1.3.0 {
 	global:
 		bpf_obj_pin_opts;
+		libbpf_perf_hw_cache_op_result_str;
+		libbpf_perf_hw_cache_op_str;
+		libbpf_perf_hw_cache_str;
+		libbpf_perf_hw_str;
+		libbpf_perf_sw_str;
+		libbpf_perf_type_str;
 } LIBBPF_1.2.0;
Signature · 1 line
1.8.3.1
#23 lore raw

Re: [PATCH v2 bpf-next 09/11] libbpf: Add perf event names

Quoted 5 lines · Yafang Shao

Add libbpf API to get generic perf event name.

---

I don't think this belongs in libbpf and shouldn't be exposed as public API. Please move it into bpftool and make it internal (if Quentin is fine with this in the first place).

Quoted 222 lines
 tools/lib/bpf/libbpf.c   | 107 +++++++++++++++++++++++++++++++++++++++++++++++
 tools/lib/bpf/libbpf.h   |  56 +++++++++++++++++++++++++
 tools/lib/bpf/libbpf.map |   6 +++
 3 files changed, 169 insertions(+)
tools/lib/bpf/libbpf.c +107 -0
index 47632606..27d396f 100644
@@ -119,6 +119,64 @@
        [BPF_STRUCT_OPS]                = "struct_ops",
 };
 
+static const char * const perf_type_name[] = {
+       [PERF_TYPE_HARDWARE]            = "hardware",
+       [PERF_TYPE_SOFTWARE]            = "software",
+       [PERF_TYPE_TRACEPOINT]          = "tracepoint",
+       [PERF_TYPE_HW_CACHE]            = "hw_cache",
+       [PERF_TYPE_RAW]                 = "raw",
+       [PERF_TYPE_BREAKPOINT]          = "breakpoint",
+};
+
+static const char * const perf_hw_name[] = {
+       [PERF_COUNT_HW_CPU_CYCLES]              = "cpu_cycles",
+       [PERF_COUNT_HW_INSTRUCTIONS]            = "instructions",
+       [PERF_COUNT_HW_CACHE_REFERENCES]        = "cache_references",
+       [PERF_COUNT_HW_CACHE_MISSES]            = "cache_misses",
+       [PERF_COUNT_HW_BRANCH_INSTRUCTIONS]     = "branch_instructions",
+       [PERF_COUNT_HW_BRANCH_MISSES]           = "branch_misses",
+       [PERF_COUNT_HW_BUS_CYCLES]              = "bus_cycles",
+       [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = "stalled_cycles_frontend",
+       [PERF_COUNT_HW_STALLED_CYCLES_BACKEND]  = "stalled_cycles_backend",
+       [PERF_COUNT_HW_REF_CPU_CYCLES]          = "ref_cpu_cycles",
+};
+
+static const char * const perf_hw_cache_name[] = {
+       [PERF_COUNT_HW_CACHE_L1D]               = "l1d",
+       [PERF_COUNT_HW_CACHE_L1I]               = "l1i",
+       [PERF_COUNT_HW_CACHE_LL]                = "ll",
+       [PERF_COUNT_HW_CACHE_DTLB]              = "dtlb",
+       [PERF_COUNT_HW_CACHE_ITLB]              = "itlb",
+       [PERF_COUNT_HW_CACHE_BPU]               = "bpu",
+       [PERF_COUNT_HW_CACHE_NODE]              = "node",
+};
+
+static const char * const perf_hw_cache_op_name[] = {
+       [PERF_COUNT_HW_CACHE_OP_READ]           = "read",
+       [PERF_COUNT_HW_CACHE_OP_WRITE]          = "write",
+       [PERF_COUNT_HW_CACHE_OP_PREFETCH]       = "prefetch",
+};
+
+static const char * const perf_hw_cache_op_result_name[] = {
+       [PERF_COUNT_HW_CACHE_RESULT_ACCESS]     = "access",
+       [PERF_COUNT_HW_CACHE_RESULT_MISS]       = "miss",
+};
+
+static const char * const perf_sw_name[] = {
+       [PERF_COUNT_SW_CPU_CLOCK]               = "cpu_clock",
+       [PERF_COUNT_SW_TASK_CLOCK]              = "task_clock",
+       [PERF_COUNT_SW_PAGE_FAULTS]             = "page_faults",
+       [PERF_COUNT_SW_CONTEXT_SWITCHES]        = "context_switches",
+       [PERF_COUNT_SW_CPU_MIGRATIONS]          = "cpu_migrations",
+       [PERF_COUNT_SW_PAGE_FAULTS_MIN]         = "page_faults_min",
+       [PERF_COUNT_SW_PAGE_FAULTS_MAJ]         = "page_faults_maj",
+       [PERF_COUNT_SW_ALIGNMENT_FAULTS]        = "alignment_faults",
+       [PERF_COUNT_SW_EMULATION_FAULTS]        = "emulation_faults",
+       [PERF_COUNT_SW_DUMMY]                   = "dummy",
+       [PERF_COUNT_SW_BPF_OUTPUT]              = "bpf_output",
+       [PERF_COUNT_SW_CGROUP_SWITCHES]         = "cgroup_switches",
+};
+
 static const char * const link_type_name[] = {
        [BPF_LINK_TYPE_UNSPEC]                  = "unspec",
        [BPF_LINK_TYPE_RAW_TRACEPOINT]          = "raw_tracepoint",
@@ -8953,6 +9011,55 @@ const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t)
        return attach_type_name[t];
 }
 
+const char *libbpf_perf_type_str(enum perf_type_id t)
+{
+       if (t < 0 || t >= ARRAY_SIZE(perf_type_name))
+               return NULL;
+
+       return perf_type_name[t];
+}
+
+const char *libbpf_perf_hw_str(enum perf_hw_id t)
+{
+       if (t < 0 || t >= ARRAY_SIZE(perf_hw_name))
+               return NULL;
+
+       return perf_hw_name[t];
+}
+
+const char *libbpf_perf_hw_cache_str(enum perf_hw_cache_id t)
+{
+       if (t < 0 || t >= ARRAY_SIZE(perf_hw_cache_name))
+               return NULL;
+
+       return perf_hw_cache_name[t];
+}
+
+const char *libbpf_perf_hw_cache_op_str(enum perf_hw_cache_op_id t)
+{
+       if (t < 0 || t >= ARRAY_SIZE(perf_hw_cache_op_name))
+               return NULL;
+
+       return perf_hw_cache_op_name[t];
+}
+
+const char *
+libbpf_perf_hw_cache_op_result_str(enum perf_hw_cache_op_result_id t)
+{
+       if (t < 0 || t >= ARRAY_SIZE(perf_hw_cache_op_result_name))
+               return NULL;
+
+       return perf_hw_cache_op_result_name[t];
+}
+
+const char *libbpf_perf_sw_str(enum perf_sw_ids t)
+{
+       if (t < 0 || t >= ARRAY_SIZE(perf_sw_name))
+               return NULL;
+
+       return perf_sw_name[t];
+}
+
 const char *libbpf_bpf_link_type_str(enum bpf_link_type t)
 {
        if (t < 0 || t >= ARRAY_SIZE(link_type_name))
tools/lib/bpf/libbpf.h +56 -0
index 754da73..4123e4c 100644
@@ -16,6 +16,7 @@
 #include <stdbool.h>
 #include <sys/types.h>  // for size_t
 #include <linux/bpf.h>
+#include <linux/perf_event.h>
 
 #include "libbpf_common.h"
 #include "libbpf_legacy.h"
@@ -61,6 +62,61 @@ enum libbpf_errno {
 LIBBPF_API const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t);
 
 /**
+ * @brief **libbpf_perf_type_str()** converts the provided perf type value
+ * into a textual representation.
+ * @param t The perf type.
+ * @return Pointer to a static string identifying the perf type. NULL is
+ * returned for unknown **perf_type_id** values.
+ */
+LIBBPF_API const char *libbpf_perf_type_str(enum perf_type_id t);
+
+/**
+ * @brief **libbpf_perf_hw_str()** converts the provided perf hw id
+ * into a textual representation.
+ * @param t The perf hw id.
+ * @return Pointer to a static string identifying the perf hw id. NULL is
+ * returned for unknown **perf_hw_id** values.
+ */
+LIBBPF_API const char *libbpf_perf_hw_str(enum perf_hw_id t);
+
+/**
+ * @brief **libbpf_perf_hw_cache_str()** converts the provided perf hw cache
+ * id into a textual representation.
+ * @param t The perf hw cache id.
+ * @return Pointer to a static string identifying the perf hw cache id.
+ * NULL is returned for unknown **perf_hw_cache_id** values.
+ */
+LIBBPF_API const char *libbpf_perf_hw_cache_str(enum perf_hw_cache_id t);
+
+/**
+ * @brief **libbpf_perf_hw_cache_op_str()** converts the provided perf hw
+ * cache op id into a textual representation.
+ * @param t The perf hw cache op id.
+ * @return Pointer to a static string identifying the perf hw cache op id.
+ * NULL is returned for unknown **perf_hw_cache_op_id** values.
+ */
+LIBBPF_API const char *libbpf_perf_hw_cache_op_str(enum perf_hw_cache_op_id t);
+
+/**
+ * @brief **libbpf_perf_hw_cache_op_result_str()** converts the provided
+ * perf hw cache op result id into a textual representation.
+ * @param t The perf hw cache op result id.
+ * @return Pointer to a static string identifying the perf hw cache op result
+ * id. NULL is returned for unknown **perf_hw_cache_op_result_id** values.
+ */
+LIBBPF_API const char *
+libbpf_perf_hw_cache_op_result_str(enum perf_hw_cache_op_result_id t);
+
+/**
+ * @brief **libbpf_perf_sw_str()** converts the provided perf sw id
+ * into a textual representation.
+ * @param t The perf sw id.
+ * @return Pointer to a static string identifying the perf sw id. NULL is
+ * returned for unknown **perf_sw_ids** values.
+ */
+LIBBPF_API const char *libbpf_perf_sw_str(enum perf_sw_ids t);
+
+/**
  * @brief **libbpf_bpf_link_type_str()** converts the provided link type value
  * into a textual representation.
  * @param t The link type.
tools/lib/bpf/libbpf.map +6 -1
index 7521a2f..6ae0a36 100644
@@ -395,4 +395,10 @@ LIBBPF_1.2.0 {
 LIBBPF_1.3.0 {
        global:
                bpf_obj_pin_opts;
+               libbpf_perf_hw_cache_op_result_str;
+               libbpf_perf_hw_cache_op_str;
+               libbpf_perf_hw_cache_str;
+               libbpf_perf_hw_str;
+               libbpf_perf_sw_str;
+               libbpf_perf_type_str;
 } LIBBPF_1.2.0;
--

1.8.3.1

maintainer
#24 lore raw

Re: [PATCH v2 bpf-next 09/11] libbpf: Add perf event names

Quoted 11 lines · Andrii Nakryiko
Quoted 5 lines · Yafang Shao

Add libbpf API to get generic perf event name.

---

I don't think this belongs in libbpf and shouldn't be exposed as public API. Please move it into bpftool and make it internal (if Quentin is fine with this in the first place).

Or maybe it belongs to libperf?

Thanks,
Song

#25 lore raw

Re: [PATCH v2 bpf-next 09/11] libbpf: Add perf event names

Quoted 8 lines · Song Liu
Quoted 5 lines · Andrii Nakryiko
Quoted 3 lines · Yafang Shao

Add libbpf API to get generic perf event name.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

---

I don't think this belongs in libbpf and shouldn't be exposed as public API. Please move it into bpftool and make it internal (if Quentin is fine with this in the first place).

Or maybe it belongs to libperf?

I prefer to move it into libperf. Then it may be reused by other tools.

Signature · 2 lines
Regards
Yafang
#26 lore raw

Re: [PATCH v2 bpf-next 09/11] libbpf: Add perf event names

Quoted 8 lines · Yafang Shao
Quoted 6 lines · Song Liu
Quoted 3 lines · Andrii Nakryiko
Quoted 1 line · Yafang Shao
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

Add libbpf API to get generic perf event name.

---

I don't think this belongs in libbpf and shouldn't be exposed as public API. Please move it into bpftool and make it internal (if Quentin is fine with this in the first place).

Fine by me

Quoted 4 lines
Quoted 2 lines

Or maybe it belongs to libperf?

I prefer to move it into libperf. Then it may be reused by other tools.

Libperf sounds like a good place to have it, and I'm all in favour of having the names available to other tools, too.

This being said, this would add a new dependency to bpftool. I'm not sure it's worth building libperf from bpftool's Makefile like we do for libbpf, just for getting the names: it would add to build time, and I don't see an easy way to replicate it on the GitHub mirror anyway. So the remaining option would be to use the library installed on the system if available, and to have a new feature detection in bpftool to figure out if the functions are available; but this also means most users compiling locally wouldn't get the names by default.

Looking at the list in the UAPI header, it seems to be relatively stable, so I'm fine with having it in bpftool, it shouldn't be too much overhead to keep up-to-date.

Too bad these are enums and not macros in the UAPI, or we'd be able to derive the name without these arrays, given that it's just trimming the prefix and turning to lowercase. But maybe an alternative solution would be to get the name of the enum members with BTF, and derive the names from there? We already (optionally) rely on vmlinux.h at build time, maybe we can just reuse it?

Quentin

#27 lore raw

Re: [PATCH v2 bpf-next 09/11] libbpf: Add perf event names

Quoted 12 lines · Quentin Monnet
Quoted 10 lines · Yafang Shao
Quoted 8 lines · Song Liu
Quoted 5 lines · Andrii Nakryiko
Quoted 3 lines · Yafang Shao

Add libbpf API to get generic perf event name.

Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

---

I don't think this belongs in libbpf and shouldn't be exposed as public API. Please move it into bpftool and make it internal (if Quentin is fine with this in the first place).

Fine by me

I will move them into bpftool.

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

Or maybe it belongs to libperf?

I prefer to move it into libperf. Then it may be reused by other tools.

Libperf sounds like a good place to have it, and I'm all in favour of having the names available to other tools, too.

Quoted 8 lines

This being said, this would add a new dependency to bpftool. I'm not sure it's worth building libperf from bpftool's Makefile like we do for libbpf, just for getting the names: it would add to build time, and I don't see an easy way to replicate it on the GitHub mirror anyway. So the remaining option would be to use the library installed on the system if available, and to have a new feature detection in bpftool to figure out if the functions are available; but this also means most users compiling locally wouldn't get the names by default.

Agree with you that dependency is a problem.

Quoted 5 lines

Looking at the list in the UAPI header, it seems to be relatively stable, so I'm fine with having it in bpftool, it shouldn't be too much overhead to keep up-to-date.

Quoted 7 lines

Too bad these are enums and not macros in the UAPI, or we'd be able to derive the name without these arrays, given that it's just trimming the prefix and turning to lowercase. But maybe an alternative solution would be to get the name of the enum members with BTF, and derive the names from there? We already (optionally) rely on vmlinux.h at build time, maybe we can just reuse it?

As Jiri suggested, I think we can just put these code into bpftool, and then try to improve it in the future.

Signature · 2 lines
Regards
Yafang
#28 lore raw

Re: [PATCH v2 bpf-next 09/11] libbpf: Add perf event names

In reply to Song Liu
Quoted 7 lines · Song Liu
Quoted 5 lines · Andrii Nakryiko
Quoted 3 lines · Yafang Shao

Add libbpf API to get generic perf event name.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

---

I don't think this belongs in libbpf and shouldn't be exposed as public API. Please move it into bpftool and make it internal (if Quentin is fine with this in the first place).

Or maybe it belongs to libperf?

we have several lookup arrays like that in perf and we keep also 'alias' names.. it'd be good idea to have that only in libperf, but that would require perf perf's parse-events cleanup as well and bpftool starting to link libperf

I'd keep it in bpftool for now and have that cleanup later

cc-ing Arnaldo

jirka

Quoted 3 lines

Thanks,
Song

#29 lore raw

Re: [PATCH v2 bpf-next 09/11] libbpf: Add perf event names

In reply to Yafang Shao
Quoted 18 lines · Yafang Shao

Add libbpf API to get generic perf event name.

---
 tools/lib/bpf/libbpf.c   | 107 +++++++++++++++++++++++++++++++++++++++++++++++
 tools/lib/bpf/libbpf.h   |  56 +++++++++++++++++++++++++
 tools/lib/bpf/libbpf.map |   6 +++
 3 files changed, 169 insertions(+)
tools/lib/bpf/libbpf.c +1 -0
index 47632606..27d396f 100644
@@ -119,6 +119,64 @@
 	[BPF_STRUCT_OPS]		= "struct_ops",
 };
 
+static const char * const perf_type_name[] = {

tools/lib/bpf/libbpf.c line 132

Quoted 10 lines
tools/lib/bpf/libbpf.c +10 -0
+	[PERF_TYPE_HARDWARE]		= "hardware",
+	[PERF_TYPE_SOFTWARE]		= "software",
+	[PERF_TYPE_TRACEPOINT]		= "tracepoint",
+	[PERF_TYPE_HW_CACHE]		= "hw_cache",
+	[PERF_TYPE_RAW]			= "raw",
+	[PERF_TYPE_BREAKPOINT]		= "breakpoint",
+};
+
+static const char * const perf_hw_name[] = {
+	[PERF_COUNT_HW_CPU_CYCLES]		= "cpu_cycles",

could you use '-' instead of '_' because that's what we do in perf

actually would be great if you could use same names like in
tool/perf/util/parse-events.c 'event_symbols_*' arrays
tool/perf/util/evsel.c 'evsel__hw_cache' array

so perf and bpftool would use same names and we have a chance to use same code for that in future

jirka

Quoted 198 lines
tools/lib/bpf/libbpf.c +96 -0
+	[PERF_COUNT_HW_INSTRUCTIONS]		= "instructions",
+	[PERF_COUNT_HW_CACHE_REFERENCES]	= "cache_references",
+	[PERF_COUNT_HW_CACHE_MISSES]		= "cache_misses",
+	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS]	= "branch_instructions",
+	[PERF_COUNT_HW_BRANCH_MISSES]		= "branch_misses",
+	[PERF_COUNT_HW_BUS_CYCLES]		= "bus_cycles",
+	[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND]	= "stalled_cycles_frontend",
+	[PERF_COUNT_HW_STALLED_CYCLES_BACKEND]	= "stalled_cycles_backend",
+	[PERF_COUNT_HW_REF_CPU_CYCLES]		= "ref_cpu_cycles",
+};
+
+static const char * const perf_hw_cache_name[] = {
+	[PERF_COUNT_HW_CACHE_L1D]		= "l1d",
+	[PERF_COUNT_HW_CACHE_L1I]		= "l1i",
+	[PERF_COUNT_HW_CACHE_LL]		= "ll",
+	[PERF_COUNT_HW_CACHE_DTLB]		= "dtlb",
+	[PERF_COUNT_HW_CACHE_ITLB]		= "itlb",
+	[PERF_COUNT_HW_CACHE_BPU]		= "bpu",
+	[PERF_COUNT_HW_CACHE_NODE]		= "node",
+};
+
+static const char * const perf_hw_cache_op_name[] = {
+	[PERF_COUNT_HW_CACHE_OP_READ]		= "read",
+	[PERF_COUNT_HW_CACHE_OP_WRITE]		= "write",
+	[PERF_COUNT_HW_CACHE_OP_PREFETCH]	= "prefetch",
+};
+
+static const char * const perf_hw_cache_op_result_name[] = {
+	[PERF_COUNT_HW_CACHE_RESULT_ACCESS]	= "access",
+	[PERF_COUNT_HW_CACHE_RESULT_MISS]	= "miss",
+};
+
+static const char * const perf_sw_name[] = {
+	[PERF_COUNT_SW_CPU_CLOCK]		= "cpu_clock",
+	[PERF_COUNT_SW_TASK_CLOCK]		= "task_clock",
+	[PERF_COUNT_SW_PAGE_FAULTS]		= "page_faults",
+	[PERF_COUNT_SW_CONTEXT_SWITCHES]	= "context_switches",
+	[PERF_COUNT_SW_CPU_MIGRATIONS]		= "cpu_migrations",
+	[PERF_COUNT_SW_PAGE_FAULTS_MIN]		= "page_faults_min",
+	[PERF_COUNT_SW_PAGE_FAULTS_MAJ]		= "page_faults_maj",
+	[PERF_COUNT_SW_ALIGNMENT_FAULTS]	= "alignment_faults",
+	[PERF_COUNT_SW_EMULATION_FAULTS]	= "emulation_faults",
+	[PERF_COUNT_SW_DUMMY]			= "dummy",
+	[PERF_COUNT_SW_BPF_OUTPUT]		= "bpf_output",
+	[PERF_COUNT_SW_CGROUP_SWITCHES]		= "cgroup_switches",
+};
+
 static const char * const link_type_name[] = {
 	[BPF_LINK_TYPE_UNSPEC]			= "unspec",
 	[BPF_LINK_TYPE_RAW_TRACEPOINT]		= "raw_tracepoint",
@@ -8953,6 +9011,55 @@ const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t)
 	return attach_type_name[t];
 }
 
+const char *libbpf_perf_type_str(enum perf_type_id t)
+{
+	if (t < 0 || t >= ARRAY_SIZE(perf_type_name))
+		return NULL;
+
+	return perf_type_name[t];
+}
+
+const char *libbpf_perf_hw_str(enum perf_hw_id t)
+{
+	if (t < 0 || t >= ARRAY_SIZE(perf_hw_name))
+		return NULL;
+
+	return perf_hw_name[t];
+}
+
+const char *libbpf_perf_hw_cache_str(enum perf_hw_cache_id t)
+{
+	if (t < 0 || t >= ARRAY_SIZE(perf_hw_cache_name))
+		return NULL;
+
+	return perf_hw_cache_name[t];
+}
+
+const char *libbpf_perf_hw_cache_op_str(enum perf_hw_cache_op_id t)
+{
+	if (t < 0 || t >= ARRAY_SIZE(perf_hw_cache_op_name))
+		return NULL;
+
+	return perf_hw_cache_op_name[t];
+}
+
+const char *
+libbpf_perf_hw_cache_op_result_str(enum perf_hw_cache_op_result_id t)
+{
+	if (t < 0 || t >= ARRAY_SIZE(perf_hw_cache_op_result_name))
+		return NULL;
+
+	return perf_hw_cache_op_result_name[t];
+}
+
+const char *libbpf_perf_sw_str(enum perf_sw_ids t)
+{
+	if (t < 0 || t >= ARRAY_SIZE(perf_sw_name))
+		return NULL;
+
+	return perf_sw_name[t];
+}
+
 const char *libbpf_bpf_link_type_str(enum bpf_link_type t)
 {
 	if (t < 0 || t >= ARRAY_SIZE(link_type_name))
tools/lib/bpf/libbpf.h +56 -0
index 754da73..4123e4c 100644
@@ -16,6 +16,7 @@
 #include <stdbool.h>
 #include <sys/types.h>  // for size_t
 #include <linux/bpf.h>
+#include <linux/perf_event.h>
 
 #include "libbpf_common.h"
 #include "libbpf_legacy.h"
@@ -61,6 +62,61 @@ enum libbpf_errno {
 LIBBPF_API const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t);
 
 /**
+ * @brief **libbpf_perf_type_str()** converts the provided perf type value
+ * into a textual representation.
+ * @param t The perf type.
+ * @return Pointer to a static string identifying the perf type. NULL is
+ * returned for unknown **perf_type_id** values.
+ */
+LIBBPF_API const char *libbpf_perf_type_str(enum perf_type_id t);
+
+/**
+ * @brief **libbpf_perf_hw_str()** converts the provided perf hw id
+ * into a textual representation.
+ * @param t The perf hw id.
+ * @return Pointer to a static string identifying the perf hw id. NULL is
+ * returned for unknown **perf_hw_id** values.
+ */
+LIBBPF_API const char *libbpf_perf_hw_str(enum perf_hw_id t);
+
+/**
+ * @brief **libbpf_perf_hw_cache_str()** converts the provided perf hw cache
+ * id into a textual representation.
+ * @param t The perf hw cache id.
+ * @return Pointer to a static string identifying the perf hw cache id.
+ * NULL is returned for unknown **perf_hw_cache_id** values.
+ */
+LIBBPF_API const char *libbpf_perf_hw_cache_str(enum perf_hw_cache_id t);
+
+/**
+ * @brief **libbpf_perf_hw_cache_op_str()** converts the provided perf hw
+ * cache op id into a textual representation.
+ * @param t The perf hw cache op id.
+ * @return Pointer to a static string identifying the perf hw cache op id.
+ * NULL is returned for unknown **perf_hw_cache_op_id** values.
+ */
+LIBBPF_API const char *libbpf_perf_hw_cache_op_str(enum perf_hw_cache_op_id t);
+
+/**
+ * @brief **libbpf_perf_hw_cache_op_result_str()** converts the provided
+ * perf hw cache op result id into a textual representation.
+ * @param t The perf hw cache op result id.
+ * @return Pointer to a static string identifying the perf hw cache op result
+ * id. NULL is returned for unknown **perf_hw_cache_op_result_id** values.
+ */
+LIBBPF_API const char *
+libbpf_perf_hw_cache_op_result_str(enum perf_hw_cache_op_result_id t);
+
+/**
+ * @brief **libbpf_perf_sw_str()** converts the provided perf sw id
+ * into a textual representation.
+ * @param t The perf sw id.
+ * @return Pointer to a static string identifying the perf sw id. NULL is
+ * returned for unknown **perf_sw_ids** values.
+ */
+LIBBPF_API const char *libbpf_perf_sw_str(enum perf_sw_ids t);
+
+/**
  * @brief **libbpf_bpf_link_type_str()** converts the provided link type value
  * into a textual representation.
  * @param t The link type.
tools/lib/bpf/libbpf.map +6 -0
index 7521a2f..6ae0a36 100644
@@ -395,4 +395,10 @@ LIBBPF_1.2.0 {
 LIBBPF_1.3.0 {
 	global:
 		bpf_obj_pin_opts;
+		libbpf_perf_hw_cache_op_result_str;
+		libbpf_perf_hw_cache_op_str;
+		libbpf_perf_hw_cache_str;
+		libbpf_perf_hw_str;
+		libbpf_perf_sw_str;
+		libbpf_perf_type_str;
 } LIBBPF_1.2.0;
Signature · 1 line
1.8.3.1
#30 lore raw

Re: [PATCH v2 bpf-next 09/11] libbpf: Add perf event names

Quoted 33 lines · Jiri Olsa
Quoted 18 lines · Yafang Shao

Add libbpf API to get generic perf event name.

---
 tools/lib/bpf/libbpf.c   | 107 +++++++++++++++++++++++++++++++++++++++++++++++
 tools/lib/bpf/libbpf.h   |  56 +++++++++++++++++++++++++
 tools/lib/bpf/libbpf.map |   6 +++
 3 files changed, 169 insertions(+)
tools/lib/bpf/libbpf.c +1 -0
index 47632606..27d396f 100644
@@ -119,6 +119,64 @@
      [BPF_STRUCT_OPS]                = "struct_ops",
 };
 
+static const char * const perf_type_name[] = {

tools/lib/bpf/libbpf.c line 132

Quoted 10 lines
tools/lib/bpf/libbpf.c +10 -0
+     [PERF_TYPE_HARDWARE]            = "hardware",
+     [PERF_TYPE_SOFTWARE]            = "software",
+     [PERF_TYPE_TRACEPOINT]          = "tracepoint",
+     [PERF_TYPE_HW_CACHE]            = "hw_cache",
+     [PERF_TYPE_RAW]                 = "raw",
+     [PERF_TYPE_BREAKPOINT]          = "breakpoint",
+};
+
+static const char * const perf_hw_name[] = {
+     [PERF_COUNT_HW_CPU_CYCLES]              = "cpu_cycles",

could you use '-' instead of '_' because that's what we do in perf

Quoted 6 lines

actually would be great if you could use same names like in
tool/perf/util/parse-events.c 'event_symbols_*' arrays
tool/perf/util/evsel.c 'evsel__hw_cache' array

so perf and bpftool would use same names and we have a chance to use same code for that in future

Good point. Will copy them :)

Signature · 2 lines
Regards
Yafang
#31 lore raw

PATCH v2 10/11 bpftool: Move get_prog_info() into do_show_link()

In reply to Yafang Shao

As a preparation for a subsequent change, the function get_prog_info() is moved into do_show_link() with no functional alteration. This adjustment paves the way for the upcoming modification.

---
 tools/bpf/bpftool/link.c | 39 +++++++++++++++++++--------------------
 1 file changed, 19 insertions(+), 20 deletions(-)
tools/bpf/bpftool/link.c +19 -20
index c8033c3..a2b75f4 100644
@@ -195,11 +195,10 @@ static int get_prog_info(int prog_id, struct bpf_prog_info *info)
 	kernel_syms_destroy(&dd);
 }
 
-static int show_link_close_json(int fd, struct bpf_link_info *info)
+static int show_link_close_json(int fd, struct bpf_link_info *info,
+				const struct bpf_prog_info *prog_info)
 {
-	struct bpf_prog_info prog_info;
 	const char *prog_type_str;
-	int err;
 
 	jsonw_start_object(json_wtr);
 
@@ -211,16 +210,12 @@ static int show_link_close_json(int fd, struct bpf_link_info *info)
 				   u64_to_ptr(info->raw_tracepoint.tp_name));
 		break;
 	case BPF_LINK_TYPE_TRACING:
-		err = get_prog_info(info->prog_id, &prog_info);
-		if (err)
-			return err;
-
-		prog_type_str = libbpf_bpf_prog_type_str(prog_info.type);
+		prog_type_str = libbpf_bpf_prog_type_str(prog_info->type);
 		/* libbpf will return NULL for variants unknown to it. */
 		if (prog_type_str)
 			jsonw_string_field(json_wtr, "prog_type", prog_type_str);
 		else
-			jsonw_uint_field(json_wtr, "prog_type", prog_info.type);
+			jsonw_uint_field(json_wtr, "prog_type", prog_info->type);
 
 		show_link_attach_type_json(info->tracing.attach_type,
 					   json_wtr);
@@ -412,11 +407,10 @@ static void show_kprobe_multi_plain(struct bpf_link_info *info)
 	kernel_syms_destroy(&dd);
 }
 
-static int show_link_close_plain(int fd, struct bpf_link_info *info)
+static int show_link_close_plain(int fd, struct bpf_link_info *info,
+				 const struct bpf_prog_info *prog_info)
 {
-	struct bpf_prog_info prog_info;
 	const char *prog_type_str;
-	int err;
 
 	show_link_header_plain(info);
 
@@ -426,16 +420,12 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info)
 		       (const char *)u64_to_ptr(info->raw_tracepoint.tp_name));
 		break;
 	case BPF_LINK_TYPE_TRACING:
-		err = get_prog_info(info->prog_id, &prog_info);
-		if (err)
-			return err;
-
-		prog_type_str = libbpf_bpf_prog_type_str(prog_info.type);
+		prog_type_str = libbpf_bpf_prog_type_str(prog_info->type);
 		/* libbpf will return NULL for variants unknown to it. */
 		if (prog_type_str)
 			printf("\n\tprog_type %s  ", prog_type_str);
 		else
-			printf("\n\tprog_type %u  ", prog_info.type);
+			printf("\n\tprog_type %u  ", prog_info->type);
 
 		show_link_attach_type_plain(info->tracing.attach_type);
 		if (info->tracing.target_obj_id || info->tracing.target_btf_id)
@@ -479,6 +469,7 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info)
 
 static int do_show_link(int fd)
 {
+	struct bpf_prog_info prog_info;
 	struct bpf_link_info info;
 	__u32 len = sizeof(info);
 	__u64 *addrs = NULL;
@@ -486,6 +477,7 @@ static int do_show_link(int fd)
 	int count;
 	int err;
 
+	memset(&prog_info, 0, sizeof(info));
 	memset(&info, 0, sizeof(info));
 again:
 	err = bpf_link_get_info_by_fd(fd, &info, &len);
@@ -495,6 +487,13 @@ static int do_show_link(int fd)
 		close(fd);
 		return err;
 	}
+
+	if (!prog_info.type) {
+		err = get_prog_info(info.prog_id, &prog_info);
+		if (err)
+			return err;
+	}
+
 	if (info.type == BPF_LINK_TYPE_RAW_TRACEPOINT &&
 	    !info.raw_tracepoint.tp_name) {
 		info.raw_tracepoint.tp_name = (unsigned long)&buf;
@@ -523,9 +522,9 @@ static int do_show_link(int fd)
 	}
 
 	if (json_output)
-		show_link_close_json(fd, &info);
+		show_link_close_json(fd, &info, &prog_info);
 	else
-		show_link_close_plain(fd, &info);
+		show_link_close_plain(fd, &info, &prog_info);
 
 	if (addrs)
 		free(addrs);
Signature · 1 line
1.8.3.1
#32 lore raw

PATCH v2 11/11 bpftool: Show probed function in perf_event link info

In reply to Yafang Shao

Enhance bpftool to display comprehensive information about exposed perf_event links, covering uprobe, kprobe, tracepoint, and generic perf event. The resulting output will include the following details:

$ tools/bpf/bpftool/bpftool link show
4: perf_event  prog 22
        retprobe 0  name kernel_clone  addr ffffffff940a9660
        bpf_cookie 0
        pids kprobe(133008)
5: perf_event  prog 28
        tp_name sched_switch
        bpf_cookie 0
        pids tracepoint(133199)
7: perf_event  prog 42
        type software  config cpu_clock
        bpf_cookie 0
        pids perf_event(134187)
8: perf_event  prog 42
        type hw_cache  config ll-read-miss
        bpf_cookie 0
        pids perf_event(134187)
9: perf_event  prog 42
        type hardware  config cpu_cycles
        bpf_cookie 0
        pids perf_event(134187)
13: perf_event  prog 72
        retprobe 0  name /home/waken/bpf/uprobe/a.out  offset 0x1338
        bpf_cookie 0
        pids uprobe(164506)
14: perf_event  prog 73
        retprobe 1  name /home/waken/bpf/uprobe/a.out  offset 0x1338
        bpf_cookie 0
        pids uprobe(164506)
$ tools/bpf/bpftool/bpftool link show -j
[{"id":4,"type":"perf_event","prog_id":22,"retprobe":0,"name":"kernel_clone","offset":0,"addr":18446744071898306144,"bpf_cookie":0,"pids":[{"pid":133008,"comm":"kprobe"}]},{"id":5,"type":"perf_event","prog_id":28,"tp_name":"sched_switch","bpf_cookie":0,"pids":[{"pid":133199,"comm":"tracepoint"}]},{"id":7,"type":"perf_event","prog_id":42,"type":"software","config":"cpu_clock","bpf_cookie":0,"pids":[{"pid":134187,"comm":"perf_event"}]},{"id":8,"type":"perf_event","prog_id":42,"type":"hw_cache","config":"ll-read-miss","bpf_cookie":0,"pids":[{"pid":134187,"comm":"perf_event"}]},{"id":9,"type":"perf_event","prog_id":42,"type":"hardware","config":"cpu_cycles","bpf_cookie":0,"pids":[{"pid":134187,"comm":"perf_event"}]},{"id":13,"type":"perf_event","prog_id":72,"retprobe":0,"name":"/home/waken/bpf/uprobe/a.out","offset":4920,"addr":0,"bpf_cookie":0,"pids":[{"pid":164506,"comm":"uprobe"}]},{"id":14,"type":"perf_event","prog_id":73,"retprobe":1,"name":"/home/waken/bpf/uprobe/a.out","offset":4920,"addr":0,"bpf_cookie":0,"pids":[{"pid":164506,"comm":"uprobe"}]}]

For generic perf events, the displayed information in bpftool is limited to the type and configuration, while other attributes such as sample_period, sample_freq, etc., are not included.

---
 tools/bpf/bpftool/link.c | 178 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 178 insertions(+)
tools/bpf/bpftool/link.c +178 -0
index a2b75f4..7c02540 100644
@@ -195,6 +195,100 @@ static int get_prog_info(int prog_id, struct bpf_prog_info *info)
 	kernel_syms_destroy(&dd);
 }
 
+static void
+show_perf_event_probe_json(struct bpf_link_info *info, json_writer_t *wtr)
+{
+	jsonw_uint_field(wtr, "retprobe", info->perf_event.probe.retprobe);
+	jsonw_string_field(wtr, "name",
+			   u64_to_ptr(info->perf_event.probe.name));
+	jsonw_uint_field(wtr, "offset", info->perf_event.probe.offset);
+	jsonw_uint_field(wtr, "addr", info->perf_event.probe.addr);
+}
+
+static void
+show_perf_event_tp_json(struct bpf_link_info *info, json_writer_t *wtr)
+{
+	jsonw_string_field(wtr, "tp_name",
+			   u64_to_ptr(info->perf_event.tp.tp_name));
+}
+
+static const char *perf_config_hw_cache_str(__u64 config)
+{
+#define PERF_HW_CACHE_LEN 128
+	const char *hw_cache, *result, *op;
+	char *str = malloc(PERF_HW_CACHE_LEN);
+
+	if (!str) {
+		p_err("mem alloc failed");
+		return NULL;
+	}
+	hw_cache = libbpf_perf_hw_cache_str(config & 0xff);
+	if (hw_cache)
+		snprintf(str, PERF_HW_CACHE_LEN, "%s-", hw_cache);
+	else
+		snprintf(str, PERF_HW_CACHE_LEN, "%lld-", config & 0xff);
+	op = libbpf_perf_hw_cache_op_str((config >> 8) & 0xff);
+	if (op)
+		snprintf(str + strlen(str), PERF_HW_CACHE_LEN - strlen(str),
+			 "%s-", op);
+	else
+		snprintf(str + strlen(str), PERF_HW_CACHE_LEN - strlen(str),
+			 "%lld-", (config >> 8) & 0xff);
+	result = libbpf_perf_hw_cache_op_result_str(config >> 16);
+	if (result)
+		snprintf(str + strlen(str), PERF_HW_CACHE_LEN - strlen(str),
+			 "%s", result);
+	else
+		snprintf(str + strlen(str), PERF_HW_CACHE_LEN - strlen(str),
+			 "%lld", config >> 16);
+
+	return str;
+}
+
+static const char *perf_config_str(__u32 type, __u64 config)
+{
+	const char *perf_config;
+
+	switch (type) {
+	case PERF_TYPE_HARDWARE:
+		perf_config = libbpf_perf_hw_str(config);
+		break;
+	case PERF_TYPE_SOFTWARE:
+		perf_config = libbpf_perf_sw_str(config);
+		break;
+	case PERF_TYPE_HW_CACHE:
+		perf_config = perf_config_hw_cache_str(config);
+		break;
+	default:
+		perf_config = NULL;
+		break;
+	}
+	return perf_config;
+}
+
+static void
+show_perf_event_event_json(struct bpf_link_info *info, json_writer_t *wtr)
+{
+	__u64 config = info->perf_event.event.config;
+	__u32 type = info->perf_event.event.type;
+	const char *perf_type, *perf_config;
+
+	perf_type = libbpf_perf_type_str(type);
+	if (perf_type)
+		jsonw_string_field(wtr, "type", perf_type);
+	else
+		jsonw_uint_field(wtr, "type", type);
+
+	perf_config = perf_config_str(type, config);
+	if (perf_config)
+		jsonw_string_field(wtr, "config", perf_config);
+	else
+		jsonw_uint_field(wtr, "config", config);
+
+	if (type == PERF_TYPE_HW_CACHE && perf_config)
+		free((void *)perf_config);
+}
+
 static int show_link_close_json(int fd, struct bpf_link_info *info,
 				const struct bpf_prog_info *prog_info)
 {
@@ -245,6 +339,14 @@ static int show_link_close_json(int fd, struct bpf_link_info *info,
 	case BPF_LINK_TYPE_KPROBE_MULTI:
 		show_kprobe_multi_json(info, json_wtr);
 		break;
+	case BPF_LINK_TYPE_PERF_EVENT:
+		if (prog_info->type == BPF_PROG_TYPE_PERF_EVENT)
+			show_perf_event_event_json(info, json_wtr);
+		else if (prog_info->type == BPF_PROG_TYPE_TRACEPOINT)
+			show_perf_event_tp_json(info, json_wtr);
+		else
+			show_perf_event_probe_json(info, json_wtr);
+		break;
 	default:
 		break;
 	}
@@ -407,6 +509,56 @@ static void show_kprobe_multi_plain(struct bpf_link_info *info)
 	kernel_syms_destroy(&dd);
 }
 
+static void show_perf_event_probe_plain(struct bpf_link_info *info)
+{
+	const char *buf;
+	__u32 retprobe;
+
+	buf = (const char *)u64_to_ptr(info->perf_event.probe.name);
+	if (buf[0] == '\0' && !info->perf_event.probe.addr)
+		return;
+
+	retprobe = info->perf_event.probe.retprobe;
+	printf("\n\tretprobe %u  name %s  ", retprobe, buf);
+	if (info->perf_event.probe.offset)
+		printf("offset %#x  ", info->perf_event.probe.offset);
+	if (info->perf_event.probe.addr)
+		printf("addr %llx  ", info->perf_event.probe.addr);
+}
+
+static void show_perf_event_tp_plain(struct bpf_link_info *info)
+{
+	const char *buf;
+
+	buf = (const char *)u64_to_ptr(info->perf_event.tp.tp_name);
+	if (buf[0] == '\0')
+		return;
+
+	printf("\n\ttp_name %s  ", buf);
+}
+
+static void show_perf_event_event_plain(struct bpf_link_info *info)
+{
+	__u64 config = info->perf_event.event.config;
+	__u32 type = info->perf_event.event.type;
+	const char *perf_type, *perf_config;
+
+	perf_type = libbpf_perf_type_str(type);
+	if (perf_type)
+		printf("\n\ttype %s  ", perf_type);
+	else
+		printf("\n\ttype %u  ", type);
+
+	perf_config = perf_config_str(type, config);
+	if (perf_config)
+		printf("config %s  ", perf_config);
+	else
+		printf("config %llu  ", config);
+
+	if (type == PERF_TYPE_HW_CACHE && perf_config)
+		free((void *)perf_config);
+}
+
 static int show_link_close_plain(int fd, struct bpf_link_info *info,
 				 const struct bpf_prog_info *prog_info)
 {
@@ -450,6 +602,14 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info,
 	case BPF_LINK_TYPE_KPROBE_MULTI:
 		show_kprobe_multi_plain(info);
 		break;
+	case BPF_LINK_TYPE_PERF_EVENT:
+		if (prog_info->type == BPF_PROG_TYPE_PERF_EVENT)
+			show_perf_event_event_plain(info);
+		else if (prog_info->type == BPF_PROG_TYPE_TRACEPOINT)
+			show_perf_event_tp_plain(info);
+		else
+			show_perf_event_probe_plain(info);
+		break;
 	default:
 		break;
 	}
@@ -479,6 +639,7 @@ static int do_show_link(int fd)
 
 	memset(&prog_info, 0, sizeof(info));
 	memset(&info, 0, sizeof(info));
+	buf[0] = '\0';
 again:
 	err = bpf_link_get_info_by_fd(fd, &info, &len);
 	if (err) {
@@ -520,7 +681,24 @@ static int do_show_link(int fd)
 			goto again;
 		}
 	}
+	if (info.type == BPF_LINK_TYPE_PERF_EVENT) {
+		if (prog_info.type == BPF_PROG_TYPE_PERF_EVENT)
+			goto out;
+		if (prog_info.type == BPF_PROG_TYPE_TRACEPOINT &&
+		    !info.perf_event.tp.tp_name) {
+			info.perf_event.tp.tp_name = (unsigned long)&buf;
+			info.perf_event.tp.name_len = sizeof(buf);
+			goto again;
+		}
+		if (prog_info.type == BPF_PROG_TYPE_KPROBE &&
+		    !info.perf_event.probe.name) {
+			info.perf_event.probe.name = (unsigned long)&buf;
+			info.perf_event.probe.name_len = sizeof(buf);
+			goto again;
+		}
+	}
 
+out:
 	if (json_output)
 		show_link_close_json(fd, &info, &prog_info);
 	else
Signature · 1 line
1.8.3.1