Skip to content

Commit 0efdcef

Browse files
DanielTimLeeborkmann
authored andcommitted
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. Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Yonghong Song <yhs@fb.com> Link: https://lore.kernel.org/bpf/20200516040608.1377876-2-danieltimlee@gmail.com
1 parent 96586dd commit 0efdcef

3 files changed

Lines changed: 6 additions & 15 deletions

File tree

‎samples/bpf/sampleip_user.c‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
#include "perf-sys.h"
1919
#include "trace_helpers.h"
2020

21-
#define __must_check
22-
#include <linux/err.h>
23-
2421
#define DEFAULT_FREQ 99
2522
#define DEFAULT_SECS 5
2623
#define MAX_IPS 8192
@@ -57,7 +54,7 @@ static int sampling_start(int freq, struct bpf_program *prog,
5754
return 1;
5855
}
5956
links[i] = bpf_program__attach_perf_event(prog, pmu_fd);
60-
if (IS_ERR(links[i])) {
57+
if (libbpf_get_error(links[i])) {
6158
fprintf(stderr, "ERROR: Attach perf event\n");
6259
links[i] = NULL;
6360
close(pmu_fd);
@@ -182,7 +179,7 @@ int main(int argc, char **argv)
182179

183180
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
184181
obj = bpf_object__open_file(filename, NULL);
185-
if (IS_ERR(obj)) {
182+
if (libbpf_get_error(obj)) {
186183
fprintf(stderr, "ERROR: opening BPF object file failed\n");
187184
obj = NULL;
188185
goto cleanup;

‎samples/bpf/trace_event_user.c‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
#include "perf-sys.h"
1717
#include "trace_helpers.h"
1818

19-
#define __must_check
20-
#include <linux/err.h>
21-
2219
#define SAMPLE_FREQ 50
2320

2421
static int pid;
@@ -159,7 +156,7 @@ static void test_perf_event_all_cpu(struct perf_event_attr *attr)
159156
goto all_cpu_err;
160157
}
161158
links[i] = bpf_program__attach_perf_event(prog, pmu_fd);
162-
if (IS_ERR(links[i])) {
159+
if (libbpf_get_error(links[i])) {
163160
printf("bpf_program__attach_perf_event failed\n");
164161
links[i] = NULL;
165162
close(pmu_fd);
@@ -198,7 +195,7 @@ static void test_perf_event_task(struct perf_event_attr *attr)
198195
goto err;
199196
}
200197
link = bpf_program__attach_perf_event(prog, pmu_fd);
201-
if (IS_ERR(link)) {
198+
if (libbpf_get_error(link)) {
202199
printf("bpf_program__attach_perf_event failed\n");
203200
link = NULL;
204201
close(pmu_fd);
@@ -314,7 +311,7 @@ int main(int argc, char **argv)
314311
}
315312

316313
obj = bpf_object__open_file(filename, NULL);
317-
if (IS_ERR(obj)) {
314+
if (libbpf_get_error(obj)) {
318315
printf("opening BPF object file failed\n");
319316
obj = NULL;
320317
goto cleanup;

‎samples/bpf/xdp_redirect_cpu_user.c‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ static const char *__doc__ =
1919
#include <time.h>
2020
#include <linux/limits.h>
2121

22-
#define __must_check
23-
#include <linux/err.h>
24-
2522
#include <arpa/inet.h>
2623
#include <linux/if_link.h>
2724

@@ -622,7 +619,7 @@ static struct bpf_link * attach_tp(struct bpf_object *obj,
622619
}
623620

624621
link = bpf_program__attach_tracepoint(prog, tp_category, tp_name);
625-
if (IS_ERR(link))
622+
if (libbpf_get_error(link))
626623
exit(EXIT_FAIL_BPF);
627624

628625
return link;

0 commit comments

Comments
 (0)