Skip to contentSkip to conversation

samples: bpf: refactor kprobe tracing progs with libbpf

PATCH Applied Daniel T. Lee 3 participants 8 messages May 16, 2020 – May 19, 2020

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover samples: bpf: refactor kprobe tracing progs with libbpf

Currently, the kprobe BPF program attachment method for bpf_load is pretty outdated. The implementation of bpf_load "directly" controls and manages(create, delete) the kprobe events of DEBUGFS. On the other hand, using using the libbpf automatically manages the kprobe event. (under bpf_link interface)

This patchset refactors kprobe tracing programs with using libbpf API for loading bpf program instead of previous bpf_load implementation.

---
Changes in V2:
- refactor pointer error check with libbpf_get_error
- on bpf object open failure, return instead jump to cleanup
- add macro for adding architecture prefix to system calls (sys_*)

Daniel T. Lee (5):
samples: bpf: refactor pointer error check with libbpf
samples: bpf: refactor kprobe tracing user progs with libbpf
samples: bpf: refactor tail call user progs with libbpf
samples: bpf: add tracex7 test file to .gitignore
samples: bpf: refactor kprobe, tail call kern progs map definition

 samples/bpf/.gitignore              |  1 +
 samples/bpf/Makefile                | 16 +++----
 samples/bpf/sampleip_kern.c         | 12 +++---
 samples/bpf/sampleip_user.c         |  7 +--
 samples/bpf/sockex3_kern.c          | 36 ++++++++--------
 samples/bpf/sockex3_user.c          | 64 +++++++++++++++++++---------
 samples/bpf/trace_common.h          | 13 ++++++
 samples/bpf/trace_event_kern.c      | 24 +++++------
 samples/bpf/trace_event_user.c      |  9 ++--
 samples/bpf/tracex1_user.c          | 37 +++++++++++++---
 samples/bpf/tracex2_kern.c          | 27 ++++++------
 samples/bpf/tracex2_user.c          | 51 ++++++++++++++++++----
 samples/bpf/tracex3_kern.c          | 24 +++++------
 samples/bpf/tracex3_user.c          | 61 +++++++++++++++++++-------
 samples/bpf/tracex4_kern.c          | 12 +++---
 samples/bpf/tracex4_user.c          | 51 +++++++++++++++++-----
 samples/bpf/tracex5_kern.c          | 14 +++---
 samples/bpf/tracex5_user.c          | 66 +++++++++++++++++++++++++----
 samples/bpf/tracex6_kern.c          | 38 +++++++++--------
 samples/bpf/tracex6_user.c          | 49 ++++++++++++++++++---
 samples/bpf/tracex7_user.c          | 39 +++++++++++++----
 samples/bpf/xdp_redirect_cpu_user.c |  5 +--
 22 files changed, 455 insertions(+), 201 deletions(-)
 create mode 100644 samples/bpf/trace_common.h
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 1/5 samples: bpf: refactor pointer error check with libbpf

Current method of checking pointer error is not user friendly. Especially the __must_check define makes this less intuitive.

Since, libbpf has an API libbpf_get_error() which checks pointer error, this commit refactors existing pointer error check logic with libbpf.

---
 samples/bpf/sampleip_user.c         | 7 ++-----
 samples/bpf/trace_event_user.c      | 9 +++------
 samples/bpf/xdp_redirect_cpu_user.c | 5 +----
 3 files changed, 6 insertions(+), 15 deletions(-)
samples/bpf/sampleip_user.c +2 -5
index 4372d2da2f9e..921c505bb567 100644
@@ -18,9 +18,6 @@
 #include "perf-sys.h"
 #include "trace_helpers.h"
 
-#define __must_check
-#include <linux/err.h>
-
 #define DEFAULT_FREQ	99
 #define DEFAULT_SECS	5
 #define MAX_IPS		8192
@@ -57,7 +54,7 @@ static int sampling_start(int freq, struct bpf_program *prog,
 			return 1;
 		}
 		links[i] = bpf_program__attach_perf_event(prog, pmu_fd);
-		if (IS_ERR(links[i])) {
+		if (libbpf_get_error(links[i])) {
 			fprintf(stderr, "ERROR: Attach perf event\n");
 			links[i] = NULL;
 			close(pmu_fd);
@@ -182,7 +179,7 @@ int main(int argc, char **argv)
 
 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 	obj = bpf_object__open_file(filename, NULL);
-	if (IS_ERR(obj)) {
+	if (libbpf_get_error(obj)) {
 		fprintf(stderr, "ERROR: opening BPF object file failed\n");
 		obj = NULL;
 		goto cleanup;
samples/bpf/trace_event_user.c +3 -6
index b6cd358d0418..ac1ba368195c 100644
@@ -16,9 +16,6 @@
 #include "perf-sys.h"
 #include "trace_helpers.h"
 
-#define __must_check
-#include <linux/err.h>
-
 #define SAMPLE_FREQ 50
 
 static int pid;
@@ -159,7 +156,7 @@ static void test_perf_event_all_cpu(struct perf_event_attr *attr)
 			goto all_cpu_err;
 		}
 		links[i] = bpf_program__attach_perf_event(prog, pmu_fd);
-		if (IS_ERR(links[i])) {
+		if (libbpf_get_error(links[i])) {
 			printf("bpf_program__attach_perf_event failed\n");
 			links[i] = NULL;
 			close(pmu_fd);
@@ -198,7 +195,7 @@ static void test_perf_event_task(struct perf_event_attr *attr)
 		goto err;
 	}
 	link = bpf_program__attach_perf_event(prog, pmu_fd);
-	if (IS_ERR(link)) {
+	if (libbpf_get_error(link)) {
 		printf("bpf_program__attach_perf_event failed\n");
 		link = NULL;
 		close(pmu_fd);
@@ -314,7 +311,7 @@ int main(int argc, char **argv)
 	}
 
 	obj = bpf_object__open_file(filename, NULL);
-	if (IS_ERR(obj)) {
+	if (libbpf_get_error(obj)) {
 		printf("opening BPF object file failed\n");
 		obj = NULL;
 		goto cleanup;
samples/bpf/xdp_redirect_cpu_user.c +1 -4
index 9b8f21abeac4..f3468168982e 100644
@@ -19,9 +19,6 @@ static const char *__doc__ =
 #include <time.h>
 #include <linux/limits.h>
 
-#define __must_check
-#include <linux/err.h>
-
 #include <arpa/inet.h>
 #include <linux/if_link.h>
 
@@ -622,7 +619,7 @@ static struct bpf_link * attach_tp(struct bpf_object *obj,
 	}
 
 	link = bpf_program__attach_tracepoint(prog, tp_category, tp_name);
-	if (IS_ERR(link))
+	if (libbpf_get_error(link))
 		exit(EXIT_FAIL_BPF);
 
 	return link;
Signature · 1 line
2.25.1
#2 lore raw

PATCH v2 2/5 samples: bpf: refactor kprobe tracing user progs with libbpf

In reply to Daniel T. Lee

Currently, the kprobe BPF program attachment method for bpf_load is quite old. The implementation of bpf_load "directly" controls and manages(create, delete) the kprobe events of DEBUGFS. On the other hand, using using the libbpf automatically manages the kprobe event. (under bpf_link interface)

By calling bpf_program__attach(_kprobe) in libbpf, the corresponding kprobe is created and the BPF program will be attached to this kprobe. To remove this, by simply invoking bpf_link__destroy will clean up the event.

This commit refactors kprobe tracing programs (tracex{1~7}_user.c) with libbpf using bpf_link interface and bpf_program__attach.

tracex2_kern.c, which tracks system calls (sys_*), has been modified to append prefix depending on architecture.

---
Changes in V2:
- refactor pointer error check with libbpf_get_error
- on bpf object open failure, return instead jump to cleanup
- add macro for adding architecture prefix to system calls (sys_*)

 samples/bpf/Makefile       | 12 ++++----
 samples/bpf/trace_common.h | 13 ++++++++
 samples/bpf/tracex1_user.c | 37 ++++++++++++++++++-----
 samples/bpf/tracex2_kern.c |  3 +-
 samples/bpf/tracex2_user.c | 51 +++++++++++++++++++++++++------
 samples/bpf/tracex3_user.c | 61 ++++++++++++++++++++++++++++----------
 samples/bpf/tracex4_user.c | 51 ++++++++++++++++++++++++-------
 samples/bpf/tracex6_user.c | 49 ++++++++++++++++++++++++++----
 samples/bpf/tracex7_user.c | 39 +++++++++++++++++++-----
 9 files changed, 252 insertions(+), 64 deletions(-)
 create mode 100644 samples/bpf/trace_common.h
samples/bpf/Makefile +6 -6
index 424f6fe7ce38..4c91e5914329 100644
@@ -64,13 +64,13 @@ 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 $(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
+tracex1-objs := tracex1_user.o $(TRACE_HELPERS)
+tracex2-objs := tracex2_user.o
+tracex3-objs := tracex3_user.o
+tracex4-objs := tracex4_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
+tracex6-objs := tracex6_user.o
+tracex7-objs := tracex7_user.o
 test_probe_write_user-objs := bpf_load.o test_probe_write_user_user.o
 trace_output-objs := bpf_load.o trace_output_user.o $(TRACE_HELPERS)
 lathist-objs := bpf_load.o lathist_user.o
samples/bpf/trace_common.h new file +13 -0
new file mode 100644
index 000000000000..8cb5400aed1f
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0
+#ifndef __TRACE_COMMON_H
+#define __TRACE_COMMON_H
+
+#ifdef __x86_64__
+#define SYSCALL(SYS) "__x64_" __stringify(SYS)
+#elif defined(__s390x__)
+#define SYSCALL(SYS) "__s390x_" __stringify(SYS)
+#else
+#define SYSCALL(SYS)  __stringify(SYS)
+#endif
+
+#endif
samples/bpf/tracex1_user.c +30 -7
index 55fddbd08702..9d4adb7fd834 100644
@@ -1,21 +1,41 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <stdio.h>
-#include <linux/bpf.h>
 #include <unistd.h>
-#include <bpf/bpf.h>
-#include "bpf_load.h"
+#include <bpf/libbpf.h>
 #include "trace_helpers.h"
 
 int main(int ac, char **argv)
 {
-	FILE *f;
+	struct bpf_link *link = NULL;
+	struct bpf_program *prog;
+	struct bpf_object *obj;
 	char filename[256];
+	FILE *f;
 
 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
+	obj = bpf_object__open_file(filename, NULL);
+	if (libbpf_get_error(obj)) {
+		fprintf(stderr, "ERROR: opening BPF object file failed\n");
+		return 0;
+	}
+
+	prog = bpf_object__find_program_by_name(obj, "bpf_prog1");
+	if (!prog) {
+		fprintf(stderr, "ERROR: finding a prog in obj file failed\n");
+		goto cleanup;
+	}
+
+	/* load BPF program */
+	if (bpf_object__load(obj)) {
+		fprintf(stderr, "ERROR: loading BPF object file failed\n");
+		goto cleanup;
+	}
 
-	if (load_bpf_file(filename)) {
-		printf("%s", bpf_log_buf);
-		return 1;
+	link = bpf_program__attach(prog);
+	if (libbpf_get_error(link)) {
+		fprintf(stderr, "ERROR: bpf_program__attach failed\n");
+		link = NULL;
+		goto cleanup;
 	}
 
 	f = popen("taskset 1 ping -c5 localhost", "r");
@@ -23,5 +43,8 @@ int main(int ac, char **argv)
 
 	read_trace_pipe();
 
+cleanup:
+	bpf_link__destroy(link);
+	bpf_object__close(obj);
 	return 0;
 }
samples/bpf/tracex2_kern.c +2 -1
index d865bb309bcb..cc5f94c098f8 100644
@@ -10,6 +10,7 @@
 #include <uapi/linux/bpf.h>
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_tracing.h>
+#include "trace_common.h"
 
 struct bpf_map_def SEC("maps") my_map = {
 	.type = BPF_MAP_TYPE_HASH,
@@ -77,7 +78,7 @@ struct bpf_map_def SEC("maps") my_hist_map = {
 	.max_entries = 1024,
 };
 
-SEC("kprobe/sys_write")
+SEC("kprobe/" SYSCALL(sys_write))
 int bpf_prog3(struct pt_regs *ctx)
 {
 	long write_size = PT_REGS_PARM3(ctx);
samples/bpf/tracex2_user.c +42 -9

91 lines not on this page · raw message

samples/bpf/tracex3_user.c +45 -16

91 lines not on this page · raw message

samples/bpf/tracex4_user.c +40 -11

84 lines not on this page · raw message

samples/bpf/tracex6_user.c +43 -6

78 lines not on this page · raw message

samples/bpf/tracex7_user.c +31 -8
index ea6dae78f0df..fdcd6580dd73 100644
@@ -1,28 +1,51 @@
 #define _GNU_SOURCE
 
 #include <stdio.h>
-#include <linux/bpf.h>
 #include <unistd.h>
-#include <bpf/bpf.h>
-#include "bpf_load.h"
+#include <bpf/libbpf.h>
 
 int main(int argc, char **argv)
 {
-	FILE *f;
+	struct bpf_link *link = NULL;
+	struct bpf_program *prog;
+	struct bpf_object *obj;
 	char filename[256];
 	char command[256];
-	int ret;
+	int ret = 0;
+	FILE *f;
 
 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
+	obj = bpf_object__open_file(filename, NULL);
+	if (libbpf_get_error(obj)) {
+		fprintf(stderr, "ERROR: opening BPF object file failed\n");
+		return 0;
+	}
+
+	prog = bpf_object__find_program_by_name(obj, "bpf_prog1");
+	if (!prog) {
+		fprintf(stderr, "ERROR: finding a prog in obj file failed\n");
+		goto cleanup;
+	}
+
+	/* load BPF program */
+	if (bpf_object__load(obj)) {
+		fprintf(stderr, "ERROR: loading BPF object file failed\n");
+		goto cleanup;
+	}
 
-	if (load_bpf_file(filename)) {
-		printf("%s", bpf_log_buf);
-		return 1;
+	link = bpf_program__attach(prog);
+	if (libbpf_get_error(link)) {
+		fprintf(stderr, "ERROR: bpf_program__attach failed\n");
+		link = NULL;
+		goto cleanup;
 	}
 
 	snprintf(command, 256, "mount %s tmpmnt/", argv[1]);
 	f = popen(command, "r");
 	ret = pclose(f);
 
+cleanup:
+	bpf_link__destroy(link);
+	bpf_object__close(obj);
 	return ret ? 0 : 1;
 }
Signature · 1 line
2.25.1
#3 lore raw

PATCH v2 3/5 samples: bpf: refactor tail call user progs with libbpf

In reply to Daniel T. Lee

BPF tail call uses the BPF_MAP_TYPE_PROG_ARRAY type map for calling into other BPF programs and this PROG_ARRAY should be filled prior to use. Currently, samples with the PROG_ARRAY type MAP fill this program array with bpf_load. For bpf_load to fill this map, kernel BPF program must specify the section with specific format of <prog_type>/<array_idx> (e.g. SEC("socket/0"))

But by using libbpf instead of bpf_load, user program can specify which programs should be added to PROG_ARRAY. The advantage of this approach is that you can selectively add only the programs you want, rather than adding all of them to PROG_ARRAY, and it's much more intuitive than the traditional approach.

This commit refactors user programs with the PROG_ARRAY type MAP with libbpf instead of using bpf_load.

---
Changes in V2:
- refactor pointer error check with libbpf_get_error
- on bpf object open failure, return instead jump to cleanup

 samples/bpf/Makefile       |  4 +--
 samples/bpf/sockex3_user.c | 64 ++++++++++++++++++++++++------------
 samples/bpf/tracex5_user.c | 66 +++++++++++++++++++++++++++++++++-----
 3 files changed, 103 insertions(+), 31 deletions(-)
samples/bpf/Makefile +2 -2
index 4c91e5914329..8403e4762306 100644
@@ -63,12 +63,12 @@ TRACE_HELPERS := ../../tools/testing/selftests/bpf/trace_helpers.o
 fds_example-objs := fds_example.o
 sockex1-objs := sockex1_user.o
 sockex2-objs := sockex2_user.o
-sockex3-objs := bpf_load.o sockex3_user.o
+sockex3-objs := sockex3_user.o
 tracex1-objs := tracex1_user.o $(TRACE_HELPERS)
 tracex2-objs := tracex2_user.o
 tracex3-objs := tracex3_user.o
 tracex4-objs := tracex4_user.o
-tracex5-objs := bpf_load.o tracex5_user.o $(TRACE_HELPERS)
+tracex5-objs := tracex5_user.o $(TRACE_HELPERS)
 tracex6-objs := tracex6_user.o
 tracex7-objs := tracex7_user.o
 test_probe_write_user-objs := bpf_load.o test_probe_write_user_user.o
samples/bpf/sockex3_user.c +43 -21

105 lines not on this page · raw message

samples/bpf/tracex5_user.c +58 -8

94 lines not on this page · raw message

Signature · 1 line
2.25.1
#4 lore raw

PATCH v2 4/5 samples: bpf: add tracex7 test file to .gitignore

In reply to Daniel T. Lee

This commit adds tracex7 test file (testfile.img) to .gitignore which comes from test_override_return.sh.

---
 samples/bpf/.gitignore | 1 +
 1 file changed, 1 insertion(+)
samples/bpf/.gitignore +1 -0
index 23837f2ed458..034800c4d1e6 100644
@@ -50,3 +50,4 @@ xdp_rxq_info
 xdp_sample_pkts
 xdp_tx_iptunnel
 xdpsock
+testfile.img
Signature · 1 line
2.25.1
#5 lore raw

PATCH v2 5/5 samples: bpf: refactor kprobe, tail call kern progs map definition

In reply to Daniel T. Lee

Because the previous two commit replaced the bpf_load implementation of the user program with libbpf, the corresponding kernel program's MAP definition can be replaced with new BTF-defined map syntax.

This commit only updates the samples which uses libbpf API for loading bpf program not with bpf_load.

---
 samples/bpf/sampleip_kern.c    | 12 +++++------
 samples/bpf/sockex3_kern.c     | 36 ++++++++++++++++----------------
 samples/bpf/trace_event_kern.c | 24 ++++++++++-----------
 samples/bpf/tracex2_kern.c     | 24 ++++++++++-----------
 samples/bpf/tracex3_kern.c     | 24 ++++++++++-----------
 samples/bpf/tracex4_kern.c     | 12 +++++------
 samples/bpf/tracex5_kern.c     | 14 ++++++-------
 samples/bpf/tracex6_kern.c     | 38 ++++++++++++++++++----------------
 8 files changed, 93 insertions(+), 91 deletions(-)
samples/bpf/sampleip_kern.c +6 -6
index e504dc308371..f24806ac24e7 100644
@@ -13,12 +13,12 @@
 
 #define MAX_IPS		8192
 
-struct bpf_map_def SEC("maps") ip_map = {
-	.type = BPF_MAP_TYPE_HASH,
-	.key_size = sizeof(u64),
-	.value_size = sizeof(u32),
-	.max_entries = MAX_IPS,
-};
+struct {
+	__uint(type, BPF_MAP_TYPE_HASH);
+	__type(key, u64);
+	__type(value, u32);
+	__uint(max_entries, MAX_IPS);
+} ip_map SEC(".maps");
 
 SEC("perf_event")
 int do_sample(struct bpf_perf_event_data *ctx)
samples/bpf/sockex3_kern.c +18 -18
index 779a5249c418..cab9cca0b8eb 100644
@@ -19,12 +19,12 @@
 
 #define PROG(F) SEC("socket/"__stringify(F)) int bpf_func_##F
 
-struct bpf_map_def SEC("maps") jmp_table = {
-	.type = BPF_MAP_TYPE_PROG_ARRAY,
-	.key_size = sizeof(u32),
-	.value_size = sizeof(u32),
-	.max_entries = 8,
-};
+struct {
+	__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+	__uint(key_size, sizeof(u32));
+	__uint(value_size, sizeof(u32));
+	__uint(max_entries, 8);
+} jmp_table SEC(".maps");
 
 #define PARSE_VLAN 1
 #define PARSE_MPLS 2
@@ -92,12 +92,12 @@ struct globals {
 	struct flow_key_record flow;
 };
 
-struct bpf_map_def SEC("maps") percpu_map = {
-	.type = BPF_MAP_TYPE_ARRAY,
-	.key_size = sizeof(__u32),
-	.value_size = sizeof(struct globals),
-	.max_entries = 32,
-};
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__type(key, __u32);
+	__type(value, struct globals);
+	__uint(max_entries, 32);
+} percpu_map SEC(".maps");
 
 /* user poor man's per_cpu until native support is ready */
 static struct globals *this_cpu_globals(void)
@@ -113,12 +113,12 @@ struct pair {
 	__u64 bytes;
 };
 
-struct bpf_map_def SEC("maps") hash_map = {
-	.type = BPF_MAP_TYPE_HASH,
-	.key_size = sizeof(struct flow_key_record),
-	.value_size = sizeof(struct pair),
-	.max_entries = 1024,
-};
+struct {
+	__uint(type, BPF_MAP_TYPE_HASH);
+	__type(key, struct flow_key_record);
+	__type(value, struct pair);
+	__uint(max_entries, 1024);
+} hash_map SEC(".maps");
 
 static void update_stats(struct __sk_buff *skb, struct globals *g)
 {
samples/bpf/trace_event_kern.c +12 -12
index da1d69e20645..7d3c66fb3f88 100644
@@ -18,19 +18,19 @@ struct key_t {
 	u32 userstack;
 };
 
-struct bpf_map_def SEC("maps") counts = {
-	.type = BPF_MAP_TYPE_HASH,
-	.key_size = sizeof(struct key_t),
-	.value_size = sizeof(u64),
-	.max_entries = 10000,
-};
+struct {
+	__uint(type, BPF_MAP_TYPE_HASH);
+	__type(key, struct key_t);
+	__type(value, u64);
+	__uint(max_entries, 10000);
+} counts SEC(".maps");
 
-struct bpf_map_def SEC("maps") stackmap = {
-	.type = BPF_MAP_TYPE_STACK_TRACE,
-	.key_size = sizeof(u32),
-	.value_size = PERF_MAX_STACK_DEPTH * sizeof(u64),
-	.max_entries = 10000,
-};
+struct {
+	__uint(type, BPF_MAP_TYPE_STACK_TRACE);
+	__uint(key_size, sizeof(u32));
+	__uint(value_size, PERF_MAX_STACK_DEPTH * sizeof(u64));
+	__uint(max_entries, 10000);
+} stackmap SEC(".maps");
 
 #define KERN_STACKID_FLAGS (0 | BPF_F_FAST_STACK_CMP)
 #define USER_STACKID_FLAGS (0 | BPF_F_FAST_STACK_CMP | BPF_F_USER_STACK)
samples/bpf/tracex2_kern.c +12 -12
index cc5f94c098f8..5bc696bac27d 100644
@@ -12,12 +12,12 @@
 #include <bpf/bpf_tracing.h>
 #include "trace_common.h"
 
-struct bpf_map_def SEC("maps") my_map = {
-	.type = BPF_MAP_TYPE_HASH,
-	.key_size = sizeof(long),
-	.value_size = sizeof(long),
-	.max_entries = 1024,
-};
+struct {
+	__uint(type, BPF_MAP_TYPE_HASH);
+	__type(key, long);
+	__type(value, long);
+	__uint(max_entries, 1024);
+} my_map SEC(".maps");
 
 /* kprobe is NOT a stable ABI. If kernel internals change this bpf+kprobe
  * example will no longer be meaningful
@@ -71,12 +71,12 @@ struct hist_key {
 	u64 index;
 };
 
-struct bpf_map_def SEC("maps") my_hist_map = {
-	.type = BPF_MAP_TYPE_PERCPU_HASH,
-	.key_size = sizeof(struct hist_key),
-	.value_size = sizeof(long),
-	.max_entries = 1024,
-};
+struct {
+	__uint(type, BPF_MAP_TYPE_PERCPU_HASH);
+	__uint(key_size, sizeof(struct hist_key));
+	__uint(value_size, sizeof(long));
+	__uint(max_entries, 1024);
+} my_hist_map SEC(".maps");
 
 SEC("kprobe/" SYSCALL(sys_write))
 int bpf_prog3(struct pt_regs *ctx)
samples/bpf/tracex3_kern.c +12 -12
index fe21c14feb8d..659613c19a82 100644
@@ -11,12 +11,12 @@
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_tracing.h>
 
-struct bpf_map_def SEC("maps") my_map = {
-	.type = BPF_MAP_TYPE_HASH,
-	.key_size = sizeof(long),
-	.value_size = sizeof(u64),
-	.max_entries = 4096,
-};
+struct {
+	__uint(type, BPF_MAP_TYPE_HASH);
+	__type(key, long);
+	__type(value, u64);
+	__uint(max_entries, 4096);
+} my_map SEC(".maps");
 
 /* kprobe is NOT a stable ABI. If kernel internals change this bpf+kprobe
  * example will no longer be meaningful
@@ -42,12 +42,12 @@ static unsigned int log2l(unsigned long long n)
 
 #define SLOTS 100
 
-struct bpf_map_def SEC("maps") lat_map = {
-	.type = BPF_MAP_TYPE_PERCPU_ARRAY,
-	.key_size = sizeof(u32),
-	.value_size = sizeof(u64),
-	.max_entries = SLOTS,
-};
+struct {
+	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
+	__uint(key_size, sizeof(u32));
+	__uint(value_size, sizeof(u64));
+	__uint(max_entries, SLOTS);
+} lat_map SEC(".maps");
 
 SEC("kprobe/blk_account_io_completion")
 int bpf_prog2(struct pt_regs *ctx)
samples/bpf/tracex4_kern.c +6 -6
index b1bb9df88f8e..eb0f8fdd14bf 100644
@@ -15,12 +15,12 @@ struct pair {
 	u64 ip;
 };
 
-struct bpf_map_def SEC("maps") my_map = {
-	.type = BPF_MAP_TYPE_HASH,
-	.key_size = sizeof(long),
-	.value_size = sizeof(struct pair),
-	.max_entries = 1000000,
-};
+struct {
+	__uint(type, BPF_MAP_TYPE_HASH);
+	__type(key, long);
+	__type(value, struct pair);
+	__uint(max_entries, 1000000);
+} my_map SEC(".maps");
 
 /* kprobe is NOT a stable ABI. If kernel internals change this bpf+kprobe
  * example will no longer be meaningful
samples/bpf/tracex5_kern.c +7 -7
index 481790fde864..32b49e8ab6bd 100644
@@ -15,16 +15,16 @@
 
 #define PROG(F) SEC("kprobe/"__stringify(F)) int bpf_func_##F
 
-struct bpf_map_def SEC("maps") progs = {
-	.type = BPF_MAP_TYPE_PROG_ARRAY,
-	.key_size = sizeof(u32),
-	.value_size = sizeof(u32),
+struct {
+	__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+	__uint(key_size, sizeof(u32));
+	__uint(value_size, sizeof(u32));
 #ifdef __mips__
-	.max_entries = 6000, /* MIPS n64 syscalls start at 5000 */
+	__uint(max_entries, 6000); /* MIPS n64 syscalls start at 5000 */
 #else
-	.max_entries = 1024,
+	__uint(max_entries, 1024);
 #endif
-};
+} progs SEC(".maps");
 
 SEC("kprobe/__seccomp_filter")
 int bpf_prog1(struct pt_regs *ctx)
samples/bpf/tracex6_kern.c +20 -18
index 96c234efa852..acad5712d8b4 100644
@@ -3,24 +3,26 @@
 #include <uapi/linux/bpf.h>
 #include <bpf/bpf_helpers.h>
 
-struct bpf_map_def SEC("maps") counters = {
-	.type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
-	.key_size = sizeof(int),
-	.value_size = sizeof(u32),
-	.max_entries = 64,
-};
-struct bpf_map_def SEC("maps") values = {
-	.type = BPF_MAP_TYPE_HASH,
-	.key_size = sizeof(int),
-	.value_size = sizeof(u64),
-	.max_entries = 64,
-};
-struct bpf_map_def SEC("maps") values2 = {
-	.type = BPF_MAP_TYPE_HASH,
-	.key_size = sizeof(int),
-	.value_size = sizeof(struct bpf_perf_event_value),
-	.max_entries = 64,
-};
+struct {
+	__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
+	__uint(key_size, sizeof(int));
+	__uint(value_size, sizeof(u32));
+	__uint(max_entries, 64);
+} counters SEC(".maps");
+
+struct {
+	__uint(type, BPF_MAP_TYPE_HASH);
+	__type(key, int);
+	__type(value, u64);
+	__uint(max_entries, 64);
+} values SEC(".maps");
+
+struct {
+	__uint(type, BPF_MAP_TYPE_HASH);
+	__type(key, int);
+	__type(value, struct bpf_perf_event_value);
+	__uint(max_entries, 64);
+} values2 SEC(".maps");
 
 SEC("kprobe/htab_map_get_next_key")
 int bpf_prog1(struct pt_regs *ctx)
Signature · 1 line
2.25.1
Acked-by on the v2 series
#6 lore raw
In reply to Daniel T. Lee
Quoted 44 lines · Daniel T. Lee

Currently, the kprobe BPF program attachment method for bpf_load is pretty outdated. The implementation of bpf_load "directly" controls and manages(create, delete) the kprobe events of DEBUGFS. On the other hand, using using the libbpf automatically manages the kprobe event. (under bpf_link interface)

This patchset refactors kprobe tracing programs with using libbpf API for loading bpf program instead of previous bpf_load implementation.

---
Changes in V2:
- refactor pointer error check with libbpf_get_error
- on bpf object open failure, return instead jump to cleanup
- add macro for adding architecture prefix to system calls (sys_*)

Daniel T. Lee (5):
samples: bpf: refactor pointer error check with libbpf
samples: bpf: refactor kprobe tracing user progs with libbpf
samples: bpf: refactor tail call user progs with libbpf
samples: bpf: add tracex7 test file to .gitignore
samples: bpf: refactor kprobe, tail call kern progs map definition

  samples/bpf/.gitignore              |  1 +
  samples/bpf/Makefile                | 16 +++---- >   samples/bpf/sampleip_kern.c         | 12 +++---
  samples/bpf/sampleip_user.c         |  7 +--
  samples/bpf/sockex3_kern.c          | 36 ++++++++--------
  samples/bpf/sockex3_user.c          | 64 +++++++++++++++++++---------
  samples/bpf/trace_common.h          | 13 ++++++
  samples/bpf/trace_event_kern.c      | 24 +++++------
  samples/bpf/trace_event_user.c      |  9 ++--
  samples/bpf/tracex1_user.c          | 37 +++++++++++++---
  samples/bpf/tracex2_kern.c          | 27 ++++++------
  samples/bpf/tracex2_user.c          | 51 ++++++++++++++++++----
  samples/bpf/tracex3_kern.c          | 24 +++++------
  samples/bpf/tracex3_user.c          | 61 +++++++++++++++++++-------
  samples/bpf/tracex4_kern.c          | 12 +++---
  samples/bpf/tracex4_user.c          | 51 +++++++++++++++++-----
  samples/bpf/tracex5_kern.c          | 14 +++---
  samples/bpf/tracex5_user.c          | 66 +++++++++++++++++++++++++----
  samples/bpf/tracex6_kern.c          | 38 +++++++++--------
  samples/bpf/tracex6_user.c          | 49 ++++++++++++++++++---
  samples/bpf/tracex7_user.c          | 39 +++++++++++++----
  samples/bpf/xdp_redirect_cpu_user.c |  5 +--
  22 files changed, 455 insertions(+), 201 deletions(-)
Quoted 1 line

create mode 100644 samples/bpf/trace_common.h

Thanks! LGTM. Ack for the whole series:

maintainer Applied
#7 lore raw
In reply to Daniel T. Lee
Quoted 45 lines · Daniel T. Lee

Currently, the kprobe BPF program attachment method for bpf_load is pretty outdated. The implementation of bpf_load "directly" controls and manages(create, delete) the kprobe events of DEBUGFS. On the other hand, using using the libbpf automatically manages the kprobe event. (under bpf_link interface)

This patchset refactors kprobe tracing programs with using libbpf API for loading bpf program instead of previous bpf_load implementation.

---
Changes in V2:
- refactor pointer error check with libbpf_get_error
- on bpf object open failure, return instead jump to cleanup
- add macro for adding architecture prefix to system calls (sys_*)

Daniel T. Lee (5):
samples: bpf: refactor pointer error check with libbpf
samples: bpf: refactor kprobe tracing user progs with libbpf
samples: bpf: refactor tail call user progs with libbpf
samples: bpf: add tracex7 test file to .gitignore
samples: bpf: refactor kprobe, tail call kern progs map definition

  samples/bpf/.gitignore              |  1 +
  samples/bpf/Makefile                | 16 +++----
  samples/bpf/sampleip_kern.c         | 12 +++---
  samples/bpf/sampleip_user.c         |  7 +--
  samples/bpf/sockex3_kern.c          | 36 ++++++++--------
  samples/bpf/sockex3_user.c          | 64 +++++++++++++++++++---------
  samples/bpf/trace_common.h          | 13 ++++++
  samples/bpf/trace_event_kern.c      | 24 +++++------
  samples/bpf/trace_event_user.c      |  9 ++--
  samples/bpf/tracex1_user.c          | 37 +++++++++++++---
  samples/bpf/tracex2_kern.c          | 27 ++++++------
  samples/bpf/tracex2_user.c          | 51 ++++++++++++++++++----
  samples/bpf/tracex3_kern.c          | 24 +++++------
  samples/bpf/tracex3_user.c          | 61 +++++++++++++++++++-------
  samples/bpf/tracex4_kern.c          | 12 +++---
  samples/bpf/tracex4_user.c          | 51 +++++++++++++++++-----
  samples/bpf/tracex5_kern.c          | 14 +++---
  samples/bpf/tracex5_user.c          | 66 +++++++++++++++++++++++++----
  samples/bpf/tracex6_kern.c          | 38 +++++++++--------
  samples/bpf/tracex6_user.c          | 49 ++++++++++++++++++---
  samples/bpf/tracex7_user.c          | 39 +++++++++++++----
  samples/bpf/xdp_redirect_cpu_user.c |  5 +--
  22 files changed, 455 insertions(+), 201 deletions(-)
Quoted 2 lines

create mode 100644 samples/bpf/trace_common.h

Applied, thanks!