Skip to contentSkip to conversation

bpf: set maximum number of attached progs to 64 for a single perf tp

PATCH In review Yonghong Song 2 participants 4 messages Nov 30, 2017 – Dec 1, 2017

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

PATCH 1/2 bpf: set maximum number of attached progs to 64 for a single perf tp

cgropu+bpf prog array has a maximum number of 64 programs. Let us apply the same limit here.

---
 kernel/bpf/core.c        | 3 ++-
 kernel/trace/bpf_trace.c | 8 ++++++++
 2 files changed, 10 insertions(+), 1 deletion(-)
kernel/bpf/core.c +2 -1
index b9f8686..86b50aa 100644
@@ -1447,7 +1447,8 @@ int bpf_prog_array_length(struct bpf_prog_array __rcu *progs)
 	rcu_read_lock();
 	prog = rcu_dereference(progs)->progs;
 	for (; *prog; prog++)
-		cnt++;
+		if (*prog != &dummy_bpf_prog.prog)
+			cnt++;
 	rcu_read_unlock();
 	return cnt;
 }
kernel/trace/bpf_trace.c +8 -0
index 27d1f4f..0ce99c3 100644
@@ -759,6 +759,8 @@ const struct bpf_prog_ops perf_event_prog_ops = {
 
 static DEFINE_MUTEX(bpf_event_mutex);
 
+#define BPF_TRACE_MAX_PROGS 64
+
 int perf_event_attach_bpf_prog(struct perf_event *event,
 			       struct bpf_prog *prog)
 {
@@ -772,6 +774,12 @@ int perf_event_attach_bpf_prog(struct perf_event *event,
 		goto unlock;
 
 	old_array = event->tp_event->prog_array;
+	if (old_array &&
+	    bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
+		ret = -E2BIG;
+		goto unlock;
+	}
+
 	ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array);
 	if (ret < 0)
 		goto unlock;
Signature · 1 line
2.9.5
#1 lore raw

PATCH 2/2 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
---
 samples/bpf/bpf_load.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
samples/bpf/bpf_load.c +12 -2
index 522ca92..242631a 100644
@@ -193,8 +193,18 @@ static int load_and_attach(const char *event, struct bpf_insn *prog, int size)
 		return -1;
 	}
 	event_fd[prog_cnt - 1] = efd;
-	ioctl(efd, PERF_EVENT_IOC_ENABLE, 0);
-	ioctl(efd, PERF_EVENT_IOC_SET_BPF, fd);
+	err = ioctl(efd, PERF_EVENT_IOC_ENABLE, 0);
+	if (err < 0) {
+		printf("ioctl PERF_EVENT_IOC_ENABLE failed err %s\n",
+		       strerror(errno));
+		return -1;
+	}
+	err = ioctl(efd, PERF_EVENT_IOC_SET_BPF, fd);
+	if (err < 0) {
+		printf("ioctl PERF_EVENT_IOC_SET_BPF failed err %s\n",
+		       strerror(errno));
+		return -1;
+	}
 
 	return 0;
 }
Signature · 1 line
2.9.5
#3 lore raw
Quoted 8 lines · Daniel Borkmann
Quoted 4 lines · Yonghong Song

cgropu+bpf prog array has a maximum number of 64 programs. Let us apply the same limit here.

Both applied to bpf tree, thanks! Please add a proper Fixes tags in the future; took care of it this time.

Will try to remember next time :-).
Thanks for taking care of this!