Skip to content

Commit 0ec9552

Browse files
yonghong-songborkmann
authored andcommitted
samples/bpf: add error checking for perf ioctl calls in bpf loader
load_bpf_file() should fail if ioctl with command PERF_EVENT_IOC_ENABLE and PERF_EVENT_IOC_SET_BPF fails. When they do fail, proper error messages are printed. With this change, the below "syscall_tp" run shows that the maximum number of bpf progs attaching to the same perf tracepoint is indeed enforced. $ ./syscall_tp -i 64 prog #0: map ids 4 5 ... prog #63: map ids 382 383 $ ./syscall_tp -i 65 prog #0: map ids 4 5 ... prog #64: map ids 388 389 ioctl PERF_EVENT_IOC_SET_BPF failed err Argument list too long Signed-off-by: Yonghong Song <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent c8c088b commit 0ec9552

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

samples/bpf/bpf_load.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,18 @@ static int load_and_attach(const char *event, struct bpf_insn *prog, int size)
193193
return -1;
194194
}
195195
event_fd[prog_cnt - 1] = efd;
196-
ioctl(efd, PERF_EVENT_IOC_ENABLE, 0);
197-
ioctl(efd, PERF_EVENT_IOC_SET_BPF, fd);
196+
err = ioctl(efd, PERF_EVENT_IOC_ENABLE, 0);
197+
if (err < 0) {
198+
printf("ioctl PERF_EVENT_IOC_ENABLE failed err %s\n",
199+
strerror(errno));
200+
return -1;
201+
}
202+
err = ioctl(efd, PERF_EVENT_IOC_SET_BPF, fd);
203+
if (err < 0) {
204+
printf("ioctl PERF_EVENT_IOC_SET_BPF failed err %s\n",
205+
strerror(errno));
206+
return -1;
207+
}
198208

199209
return 0;
200210
}

0 commit comments

Comments
 (0)