Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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

authored by

Daniel T. Lee and committed by
Daniel Borkmann
0efdcefb 96586dd9

+6 -15
+2 -5
samples/bpf/sampleip_user.c
··· 18 18 #include "perf-sys.h" 19 19 #include "trace_helpers.h" 20 20 21 - #define __must_check 22 - #include <linux/err.h> 23 - 24 21 #define DEFAULT_FREQ 99 25 22 #define DEFAULT_SECS 5 26 23 #define MAX_IPS 8192 ··· 54 57 return 1; 55 58 } 56 59 links[i] = bpf_program__attach_perf_event(prog, pmu_fd); 57 - if (IS_ERR(links[i])) { 60 + if (libbpf_get_error(links[i])) { 58 61 fprintf(stderr, "ERROR: Attach perf event\n"); 59 62 links[i] = NULL; 60 63 close(pmu_fd); ··· 179 182 180 183 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); 181 184 obj = bpf_object__open_file(filename, NULL); 182 - if (IS_ERR(obj)) { 185 + if (libbpf_get_error(obj)) { 183 186 fprintf(stderr, "ERROR: opening BPF object file failed\n"); 184 187 obj = NULL; 185 188 goto cleanup;
+3 -6
samples/bpf/trace_event_user.c
··· 16 16 #include "perf-sys.h" 17 17 #include "trace_helpers.h" 18 18 19 - #define __must_check 20 - #include <linux/err.h> 21 - 22 19 #define SAMPLE_FREQ 50 23 20 24 21 static int pid; ··· 156 159 goto all_cpu_err; 157 160 } 158 161 links[i] = bpf_program__attach_perf_event(prog, pmu_fd); 159 - if (IS_ERR(links[i])) { 162 + if (libbpf_get_error(links[i])) { 160 163 printf("bpf_program__attach_perf_event failed\n"); 161 164 links[i] = NULL; 162 165 close(pmu_fd); ··· 195 198 goto err; 196 199 } 197 200 link = bpf_program__attach_perf_event(prog, pmu_fd); 198 - if (IS_ERR(link)) { 201 + if (libbpf_get_error(link)) { 199 202 printf("bpf_program__attach_perf_event failed\n"); 200 203 link = NULL; 201 204 close(pmu_fd); ··· 311 314 } 312 315 313 316 obj = bpf_object__open_file(filename, NULL); 314 - if (IS_ERR(obj)) { 317 + if (libbpf_get_error(obj)) { 315 318 printf("opening BPF object file failed\n"); 316 319 obj = NULL; 317 320 goto cleanup;
+1 -4
samples/bpf/xdp_redirect_cpu_user.c
··· 19 19 #include <time.h> 20 20 #include <linux/limits.h> 21 21 22 - #define __must_check 23 - #include <linux/err.h> 24 - 25 22 #include <arpa/inet.h> 26 23 #include <linux/if_link.h> 27 24 ··· 619 622 } 620 623 621 624 link = bpf_program__attach_tracepoint(prog, tp_category, tp_name); 622 - if (IS_ERR(link)) 625 + if (libbpf_get_error(link)) 623 626 exit(EXIT_FAIL_BPF); 624 627 625 628 return link;