Skip to contentSkip to conversation

samples: bpf: refactor perf_event user program with libbpf bpf_link

PATCH Superseded Daniel T. Lee 2 participants 6 messages Mar 10, 2020

Subsystems (5)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover samples: bpf: refactor perf_event user program with libbpf bpf_link

Currently, some samples are using ioctl for enabling perf_event and attaching BPF programs to this event. However, the bpf_program__attach of libbpf(using bpf_link) is much more intuitive than the previous method using ioctl.

bpf_program__attach_perf_event manages the enable of perf_event and attach of BPF programs to it, so there's no neeed to do this directly with ioctl.

In addition, bpf_link provides consistency in the use of API because it allows disable (detach, destroy) for multiple events to be treated as one bpf_link__destroy.

To refactor samples with using this libbpf API, the bpf_load in the samples were removed and migrated to libbbpf. Because read_trace_pipe is used in bpf_load, multiple samples cannot be migrated to libbpf, this function was moved to trace_helpers.

Daniel T. Lee (2):
samples: bpf: move read_trace_pipe to trace_helpers
samples: bpf: refactor perf_event user program with libbpf bpf_link

 samples/bpf/Makefile                        |  8 +--
 samples/bpf/bpf_load.c                      | 20 -------
 samples/bpf/bpf_load.h                      |  1 -
 samples/bpf/sampleip_user.c                 | 58 +++++++++++++--------
 samples/bpf/trace_event_user.c              | 57 +++++++++++++-------
 samples/bpf/tracex1_user.c                  |  1 +
 samples/bpf/tracex5_user.c                  |  1 +
 tools/testing/selftests/bpf/trace_helpers.c | 23 ++++++++
 tools/testing/selftests/bpf/trace_helpers.h |  1 +
 9 files changed, 106 insertions(+), 64 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/2 samples: bpf: move read_trace_pipe to trace_helpers

To reduce the reliance of trace samples (trace*_user) on bpf_load, move read_trace_pipe to trace_helpers. By moving this bpf_loader helper elsewhere, trace functions can be easily migrated to libbbpf.

---
 samples/bpf/Makefile                        |  4 ++--
 samples/bpf/bpf_load.c                      | 20 ------------------
 samples/bpf/bpf_load.h                      |  1 -
 samples/bpf/tracex1_user.c                  |  1 +
 samples/bpf/tracex5_user.c                  |  1 +
 tools/testing/selftests/bpf/trace_helpers.c | 23 +++++++++++++++++++++
 tools/testing/selftests/bpf/trace_helpers.h |  1 +
 7 files changed, 28 insertions(+), 23 deletions(-)
samples/bpf/Makefile +2 -2
index 79b0fee6943b..ff0061467dd3 100644
@@ -64,11 +64,11 @@ fds_example-objs := fds_example.o
 sockex1-objs := sockex1_user.o
 sockex2-objs := sockex2_user.o
 sockex3-objs := bpf_load.o sockex3_user.o
-tracex1-objs := bpf_load.o tracex1_user.o
+tracex1-objs := bpf_load.o tracex1_user.o $(TRACE_HELPERS)
 tracex2-objs := bpf_load.o tracex2_user.o
 tracex3-objs := bpf_load.o tracex3_user.o
 tracex4-objs := bpf_load.o tracex4_user.o
-tracex5-objs := bpf_load.o tracex5_user.o
+tracex5-objs := bpf_load.o tracex5_user.o $(TRACE_HELPERS)
 tracex6-objs := bpf_load.o tracex6_user.o
 tracex7-objs := bpf_load.o tracex7_user.o
 test_probe_write_user-objs := bpf_load.o test_probe_write_user_user.o
samples/bpf/bpf_load.c +0 -20
index 4574b1939e49..c5ad528f046e 100644
@@ -665,23 +665,3 @@ int load_bpf_file_fixup_map(const char *path, fixup_map_cb fixup_map)
 {
 	return do_load_bpf_file(path, fixup_map);
 }
-
-void read_trace_pipe(void)
-{
-	int trace_fd;
-
-	trace_fd = open(DEBUGFS "trace_pipe", O_RDONLY, 0);
-	if (trace_fd < 0)
-		return;
-
-	while (1) {
-		static char buf[4096];
-		ssize_t sz;
-
-		sz = read(trace_fd, buf, sizeof(buf) - 1);
-		if (sz > 0) {
-			buf[sz] = 0;
-			puts(buf);
-		}
-	}
-}
samples/bpf/bpf_load.h +0 -1
index 814894a12974..4fcd258c616f 100644
@@ -53,6 +53,5 @@ extern int map_data_count;
 int load_bpf_file(char *path);
 int load_bpf_file_fixup_map(const char *path, fixup_map_cb fixup_map);
 
-void read_trace_pipe(void);
 int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
 #endif
samples/bpf/tracex1_user.c +1 -0
index af8c20608ab5..55fddbd08702 100644
@@ -4,6 +4,7 @@
 #include <unistd.h>
 #include <bpf/bpf.h>
 #include "bpf_load.h"
+#include "trace_helpers.h"
 
 int main(int ac, char **argv)
 {
samples/bpf/tracex5_user.c +1 -0
index c4ab91c89494..c2317b39e0d2 100644
@@ -8,6 +8,7 @@
 #include <bpf/bpf.h>
 #include "bpf_load.h"
 #include <sys/resource.h>
+#include "trace_helpers.h"
 
 /* install fake seccomp program to enable seccomp code path inside the kernel,
  * so that our kprobe attached to seccomp_phase1() can be triggered
tools/testing/selftests/bpf/trace_helpers.c +23 -0
index 7f989b3e4e22..4d0e913bbb22 100644
@@ -4,12 +4,15 @@
 #include <string.h>
 #include <assert.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <poll.h>
 #include <unistd.h>
 #include <linux/perf_event.h>
 #include <sys/mman.h>
 #include "trace_helpers.h"
 
+#define DEBUGFS "/sys/kernel/debug/tracing/"
+
 #define MAX_SYMS 300000
 static struct ksym syms[MAX_SYMS];
 static int sym_cnt;
@@ -86,3 +89,23 @@ long ksym_get_addr(const char *name)
 
 	return 0;
 }
+
+void read_trace_pipe(void)
+{
+	int trace_fd;
+
+	trace_fd = open(DEBUGFS "trace_pipe", O_RDONLY, 0);
+	if (trace_fd < 0)
+		return;
+
+	while (1) {
+		static char buf[4096];
+		ssize_t sz;
+
+		sz = read(trace_fd, buf, sizeof(buf) - 1);
+		if (sz > 0) {
+			buf[sz] = 0;
+			puts(buf);
+		}
+	}
+}
tools/testing/selftests/bpf/trace_helpers.h +1 -0
index 0383c9b8adc1..25ef597dd03f 100644
@@ -12,5 +12,6 @@ struct ksym {
 int load_kallsyms(void);
 struct ksym *ksym_search(long key);
 long ksym_get_addr(const char *name);
+void read_trace_pipe(void);
 
 #endif
Signature · 1 line
2.25.1
#3 lore raw

PATCH 2/2 samples: bpf: refactor perf_event user program with libbpf bpf_link

In reply to Daniel T. Lee

The bpf_program__attach of libbpf(using bpf_link) is much more intuitive than the previous method using ioctl.

bpf_program__attach_perf_event manages the enable of perf_event and attach of BPF programs to it, so there's no neeed to do this directly with ioctl.

In addition, bpf_link provides consistency in the use of API because it allows disable (detach, destroy) for multiple events to be treated as one bpf_link__destroy.

This commit refactors samples that attach the bpf program to perf_event by using libbbpf instead of ioctl. Also the bpf_load in the samples were removed and migrated to use libbbpf API.

---
 samples/bpf/Makefile           |  4 +--
 samples/bpf/sampleip_user.c    | 58 ++++++++++++++++++++++------------
 samples/bpf/trace_event_user.c | 57 ++++++++++++++++++++++-----------
 3 files changed, 78 insertions(+), 41 deletions(-)
samples/bpf/Makefile +2 -2
index ff0061467dd3..424f6fe7ce38 100644
@@ -88,8 +88,8 @@ xdp2-objs := xdp1_user.o
 xdp_router_ipv4-objs := xdp_router_ipv4_user.o
 test_current_task_under_cgroup-objs := bpf_load.o $(CGROUP_HELPERS) \
 				       test_current_task_under_cgroup_user.o
-trace_event-objs := bpf_load.o trace_event_user.o $(TRACE_HELPERS)
-sampleip-objs := bpf_load.o sampleip_user.o $(TRACE_HELPERS)
+trace_event-objs := trace_event_user.o $(TRACE_HELPERS)
+sampleip-objs := sampleip_user.o $(TRACE_HELPERS)
 tc_l2_redirect-objs := bpf_load.o tc_l2_redirect_user.o
 lwt_len_hist-objs := bpf_load.o lwt_len_hist_user.o
 xdp_tx_iptunnel-objs := xdp_tx_iptunnel_user.o
samples/bpf/sampleip_user.c +37 -21

133 lines not on this page · raw message

samples/bpf/trace_event_user.c +39 -18

124 lines not on this page · raw message

Signature · 1 line
2.25.1
Acked-by on v1 1/2
#2 lore raw

RE: [PATCH bpf-next 1/2] samples: bpf: move read_trace_pipe to trace_helpers

In reply to Daniel T. Lee
Quoted 5 lines · Daniel T. Lee

To reduce the reliance of trace samples (trace*_user) on bpf_load, move read_trace_pipe to trace_helpers. By moving this bpf_loader helper elsewhere, trace functions can be easily migrated to libbbpf.

Quoted 10 lines
---
 samples/bpf/Makefile                        |  4 ++--
 samples/bpf/bpf_load.c                      | 20 ------------------
 samples/bpf/bpf_load.h                      |  1 -
 samples/bpf/tracex1_user.c                  |  1 +
 samples/bpf/tracex5_user.c                  |  1 +
 tools/testing/selftests/bpf/trace_helpers.c | 23 +++++++++++++++++++++
 tools/testing/selftests/bpf/trace_helpers.h |  1 +
 7 files changed, 28 insertions(+), 23 deletions(-)
#4 lore raw
In reply to Daniel T. Lee
Quoted 17 lines · Daniel T. Lee

The bpf_program__attach of libbpf(using bpf_link) is much more intuitive than the previous method using ioctl.

bpf_program__attach_perf_event manages the enable of perf_event and attach of BPF programs to it, so there's no neeed to do this directly with ioctl.

In addition, bpf_link provides consistency in the use of API because it allows disable (detach, destroy) for multiple events to be treated as one bpf_link__destroy.

This commit refactors samples that attach the bpf program to perf_event by using libbbpf instead of ioctl. Also the bpf_load in the samples were removed and migrated to use libbbpf API.

---

[...]

Quoted 53 lines

int main(int argc, char **argv)
{

samples/bpf/sampleip_user.c +21 -8
+	int prog_fd, *pmu_fd, opt, freq = DEFAULT_FREQ, secs = DEFAULT_SECS;
+	struct bpf_program *prog;
+	struct bpf_object *obj;
+	struct bpf_link **link;
 	char filename[256];
-	int *pmu_fd, opt, freq = DEFAULT_FREQ, secs = DEFAULT_SECS;
 
 	/* process arguments */
 	while ((opt = getopt(argc, argv, "F:h")) != -1) {
@@ -165,36 +170,47 @@ int main(int argc, char **argv)
 	/* create perf FDs for each CPU */
 	nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
 	pmu_fd = malloc(nr_cpus * sizeof(int));
-	if (pmu_fd == NULL) {
-		fprintf(stderr, "ERROR: malloc of pmu_fd\n");
+	link = malloc(nr_cpus * sizeof(struct bpf_link *));
+	if (pmu_fd == NULL || link == NULL) {
+		fprintf(stderr, "ERROR: malloc of pmu_fd/link\n");
 		return 1;
 	}
 
 	/* load BPF program */
 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
-	if (load_bpf_file(filename)) {
+	if (bpf_prog_load(filename, BPF_PROG_TYPE_PERF_EVENT, &obj, &prog_fd)) {
 		fprintf(stderr, "ERROR: loading BPF program (errno %d):\n",
 			errno);
-		if (strcmp(bpf_log_buf, "") == 0)
-			fprintf(stderr, "Try: ulimit -l unlimited\n");
-		else
-			fprintf(stderr, "%s", bpf_log_buf);
 		return 1;
 	}
+
+	prog = bpf_program__next(NULL, obj);
+	if (!prog) {
+		printf("finding a prog in obj file failed\n");
+		return 1;
+	}
+
+	map_fd = bpf_object__find_map_fd_by_name(obj, "ip_map");
+	if (map_fd < 0) {
+		printf("finding a ip_map map in obj file failed\n");
+		return 1;
+	}
+
 	signal(SIGINT, int_exit);
 	signal(SIGTERM, int_exit);
 
 	/* do sampling */

samples/bpf/sampleip_user.c int main(int argc, char **argv) · line 210

Quoted 10 lines
samples/bpf/sampleip_user.c +3 -2
 	printf("Sampling at %d Hertz for %d seconds. Ctrl-C also ends.\n",
 	       freq, secs);
-	if (sampling_start(pmu_fd, freq) != 0)
+	if (sampling_start(pmu_fd, freq, prog, link) != 0)
 		return 1;
 	sleep(secs);
-	sampling_end(pmu_fd);
+	sampling_end(link);
 	free(pmu_fd);
+	free(link);

Not really a problem with this patch but on error we don't free() memory but then on normal exit there is a free() its a bit inconsistent. How about adding free on errors as well?

Quoted 7 lines
samples/bpf/sampleip_user.c +1 -1
 	/* output sample counts */
-	print_ip_map(map_fd[0]);
+	print_ip_map(map_fd);
 
 	return 0;
 }

[...]

samples/bpf/trace_event_user.c static inline int generate_load(void) · line 139

Quoted 6 lines

static void print_ksym(__u64 addr)

samples/bpf/trace_event_user.c +1 -0
@@ -137,6 +136,7 @@ static inline int generate_load(void)
 static void test_perf_event_all_cpu(struct perf_event_attr *attr)
 {
 	int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
+	struct bpf_link **link = malloc(nr_cpus * sizeof(struct bpf_link *));

need to check if its null? Its not going to be very friendly to segfault later. Or maybe I'm missing the check.

Quoted 24 lines
samples/bpf/trace_event_user.c +6 -4
 	int *pmu_fd = malloc(nr_cpus * sizeof(int));
 	int i, error = 0;
 
@@ -151,8 +151,12 @@ static void test_perf_event_all_cpu(struct perf_event_attr *attr)
 			error = 1;
 			goto all_cpu_err;
 		}
-		assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_SET_BPF, prog_fd[0]) == 0);
-		assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_ENABLE) == 0);
+		link[i] = bpf_program__attach_perf_event(prog, pmu_fd[i]);
+		if (link[i] < 0) {
+			printf("bpf_program__attach_perf_event failed\n");
+			error = 1;
+			goto all_cpu_err;
+		}
 	}
 
 	if (generate_load() < 0) {
@@ -161,11 +165,11 @@ static void test_perf_event_all_cpu(struct perf_event_attr *attr)
 	}
 	print_stacks();
 all_cpu_err:
-	for (i--; i >= 0; i--) {
-		ioctl(pmu_fd[i], PERF_EVENT_IOC_DISABLE);

samples/bpf/trace_event_user.c static void test_perf_event_all_cpu(struct perf_event_attr *attr) · line 175

Quoted 10 lines
samples/bpf/trace_event_user.c +4 -2
-		close(pmu_fd[i]);
-	}
+	for (i--; i >= 0; i--)
+		bpf_link__destroy(link[i]);
+
 	free(pmu_fd);
+	free(link);
 	if (error)
 		int_exit(0);
 }

Thanks,
John

#5 lore raw
Quoted 74 lines · John Fastabend
Quoted 17 lines · Daniel T. Lee

The bpf_program__attach of libbpf(using bpf_link) is much more intuitive than the previous method using ioctl.

bpf_program__attach_perf_event manages the enable of perf_event and attach of BPF programs to it, so there's no neeed to do this directly with ioctl.

In addition, bpf_link provides consistency in the use of API because it allows disable (detach, destroy) for multiple events to be treated as one bpf_link__destroy.

This commit refactors samples that attach the bpf program to perf_event by using libbbpf instead of ioctl. Also the bpf_load in the samples were removed and migrated to use libbbpf API.

---

[...]

Quoted 52 lines

int main(int argc, char **argv)
{

samples/bpf/sampleip_user.c +21 -8
+     int prog_fd, *pmu_fd, opt, freq = DEFAULT_FREQ, secs = DEFAULT_SECS;
+     struct bpf_program *prog;
+     struct bpf_object *obj;
+     struct bpf_link **link;
      char filename[256];
-     int *pmu_fd, opt, freq = DEFAULT_FREQ, secs = DEFAULT_SECS;
 
      /* process arguments */
      while ((opt = getopt(argc, argv, "F:h")) != -1) {
@@ -165,36 +170,47 @@ int main(int argc, char **argv)
      /* create perf FDs for each CPU */
      nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
      pmu_fd = malloc(nr_cpus * sizeof(int));
-     if (pmu_fd == NULL) {
-             fprintf(stderr, "ERROR: malloc of pmu_fd\n");
+     link = malloc(nr_cpus * sizeof(struct bpf_link *));
+     if (pmu_fd == NULL || link == NULL) {
+             fprintf(stderr, "ERROR: malloc of pmu_fd/link\n");
              return 1;
      }
 
      /* load BPF program */
      snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
-     if (load_bpf_file(filename)) {
+     if (bpf_prog_load(filename, BPF_PROG_TYPE_PERF_EVENT, &obj, &prog_fd)) {
              fprintf(stderr, "ERROR: loading BPF program (errno %d):\n",
                      errno);
-             if (strcmp(bpf_log_buf, "") == 0)
-                     fprintf(stderr, "Try: ulimit -l unlimited\n");
-             else
-                     fprintf(stderr, "%s", bpf_log_buf);
              return 1;
      }
+
+     prog = bpf_program__next(NULL, obj);
+     if (!prog) {
+             printf("finding a prog in obj file failed\n");
+             return 1;
+     }
+
+     map_fd = bpf_object__find_map_fd_by_name(obj, "ip_map");
+     if (map_fd < 0) {
+             printf("finding a ip_map map in obj file failed\n");
+             return 1;
+     }
+
      signal(SIGINT, int_exit);
      signal(SIGTERM, int_exit);
 
Quoted 15 lines

samples/bpf/sampleip_user.c int main(int argc, char **argv) · line 210

Quoted 11 lines
samples/bpf/sampleip_user.c +3 -2
      /* do sampling */
      printf("Sampling at %d Hertz for %d seconds. Ctrl-C also ends.\n",
             freq, secs);
-     if (sampling_start(pmu_fd, freq) != 0)
+     if (sampling_start(pmu_fd, freq, prog, link) != 0)
              return 1;
      sleep(secs);
-     sampling_end(pmu_fd);
+     sampling_end(link);
      free(pmu_fd);
+     free(link);

Not really a problem with this patch but on error we don't free() memory but then on normal exit there is a free() its a bit inconsistent. How about adding free on errors as well?

I think you're right.
I'll add free() on errors to keep it consistent. Will apply feedback right away!

Quoted 11 lines
Quoted 8 lines
samples/bpf/sampleip_user.c +1 -1
      /* output sample counts */
-     print_ip_map(map_fd[0]);
+     print_ip_map(map_fd);
 
      return 0;
 }

[...]

Quoted 10 lines

samples/bpf/trace_event_user.c static inline int generate_load(void) · line 139

Quoted 6 lines

static void print_ksym(__u64 addr)

samples/bpf/trace_event_user.c +1 -0
@@ -137,6 +136,7 @@ static inline int generate_load(void)
 static void test_perf_event_all_cpu(struct perf_event_attr *attr)
 {
      int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
+     struct bpf_link **link = malloc(nr_cpus * sizeof(struct bpf_link *));

need to check if its null? Its not going to be very friendly to segfault later. Or maybe I'm missing the check.

Also, checking whether it is null will be more safe. I'll apply and send next version patch.

Quoted 29 lines
Quoted 29 lines
samples/bpf/trace_event_user.c +9 -6
      int *pmu_fd = malloc(nr_cpus * sizeof(int));
      int i, error = 0;
 
@@ -151,8 +151,12 @@ static void test_perf_event_all_cpu(struct perf_event_attr *attr)
                      error = 1;
                      goto all_cpu_err;
              }
-             assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_SET_BPF, prog_fd[0]) == 0);
-             assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_ENABLE) == 0);
+             link[i] = bpf_program__attach_perf_event(prog, pmu_fd[i]);
+             if (link[i] < 0) {
+                     printf("bpf_program__attach_perf_event failed\n");
+                     error = 1;
+                     goto all_cpu_err;
+             }
      }
 
      if (generate_load() < 0) {
@@ -161,11 +165,11 @@ static void test_perf_event_all_cpu(struct perf_event_attr *attr)
      }
      print_stacks();
 all_cpu_err:
-     for (i--; i >= 0; i--) {
-             ioctl(pmu_fd[i], PERF_EVENT_IOC_DISABLE);
-             close(pmu_fd[i]);
-     }
+     for (i--; i >= 0; i--)
+             bpf_link__destroy(link[i]);
+
Quoted 8 lines

samples/bpf/trace_event_user.c static void test_perf_event_all_cpu(struct perf_event_attr *attr) · line 175

Quoted 5 lines
samples/bpf/trace_event_user.c +1 -0
      free(pmu_fd);
+     free(link);
      if (error)
              int_exit(0);
 }

Thanks,
John

Thank you for your time and effort for the review.

Best,
Daniel