netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net 1/2] bpf: set maximum number of attached progs to 64 for a single perf tp
@ 2017-11-30 21:47 Yonghong Song
  2017-11-30 21:47 ` [PATCH net 2/2] samples/bpf: add error checking for perf ioctl calls in bpf loader Yonghong Song
  2017-12-01  2:07 ` [PATCH net 1/2] bpf: set maximum number of attached progs to 64 for a single perf tp Daniel Borkmann
  0 siblings, 2 replies; 4+ messages in thread
From: Yonghong Song @ 2017-11-30 21:47 UTC (permalink / raw)
  To: ast, daniel, netdev; +Cc: kernel-team

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

Signed-off-by: Yonghong Song <yhs@fb.com>
---
 kernel/bpf/core.c        | 3 ++-
 kernel/trace/bpf_trace.c | 8 ++++++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index b9f8686..86b50aa 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -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;
 }
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index 27d1f4f..0ce99c3 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -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;
-- 
2.9.5

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH net 2/2] samples/bpf: add error checking for perf ioctl calls in bpf loader
  2017-11-30 21:47 [PATCH net 1/2] bpf: set maximum number of attached progs to 64 for a single perf tp Yonghong Song
@ 2017-11-30 21:47 ` Yonghong Song
  2017-12-01  2:07 ` [PATCH net 1/2] bpf: set maximum number of attached progs to 64 for a single perf tp Daniel Borkmann
  1 sibling, 0 replies; 4+ messages in thread
From: Yonghong Song @ 2017-11-30 21:47 UTC (permalink / raw)
  To: ast, daniel, netdev; +Cc: kernel-team

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 <yhs@fb.com>
---
 samples/bpf/bpf_load.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/samples/bpf/bpf_load.c b/samples/bpf/bpf_load.c
index 522ca92..242631a 100644
--- a/samples/bpf/bpf_load.c
+++ b/samples/bpf/bpf_load.c
@@ -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;
 }
-- 
2.9.5

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH net 1/2] bpf: set maximum number of attached progs to 64 for a single perf tp
  2017-11-30 21:47 [PATCH net 1/2] bpf: set maximum number of attached progs to 64 for a single perf tp Yonghong Song
  2017-11-30 21:47 ` [PATCH net 2/2] samples/bpf: add error checking for perf ioctl calls in bpf loader Yonghong Song
@ 2017-12-01  2:07 ` Daniel Borkmann
  2017-12-01  3:19   ` Yonghong Song
  1 sibling, 1 reply; 4+ messages in thread
From: Daniel Borkmann @ 2017-12-01  2:07 UTC (permalink / raw)
  To: Yonghong Song, ast, netdev; +Cc: kernel-team

On 11/30/2017 10:47 PM, Yonghong Song wrote:
> cgropu+bpf prog array has a maximum number of 64 programs.
> Let us apply the same limit here.
> 
> Signed-off-by: Yonghong Song <yhs@fb.com>

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH net 1/2] bpf: set maximum number of attached progs to 64 for a single perf tp
  2017-12-01  2:07 ` [PATCH net 1/2] bpf: set maximum number of attached progs to 64 for a single perf tp Daniel Borkmann
@ 2017-12-01  3:19   ` Yonghong Song
  0 siblings, 0 replies; 4+ messages in thread
From: Yonghong Song @ 2017-12-01  3:19 UTC (permalink / raw)
  To: Daniel Borkmann, ast, netdev; +Cc: kernel-team



On 11/30/17 6:07 PM, Daniel Borkmann wrote:
> On 11/30/2017 10:47 PM, Yonghong Song wrote:
>> cgropu+bpf prog array has a maximum number of 64 programs.
>> Let us apply the same limit here.
>>
>> Signed-off-by: Yonghong Song <yhs@fb.com>
> 
> 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!

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-12-01  3:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-11-30 21:47 [PATCH net 1/2] bpf: set maximum number of attached progs to 64 for a single perf tp Yonghong Song
2017-11-30 21:47 ` [PATCH net 2/2] samples/bpf: add error checking for perf ioctl calls in bpf loader Yonghong Song
2017-12-01  2:07 ` [PATCH net 1/2] bpf: set maximum number of attached progs to 64 for a single perf tp Daniel Borkmann
2017-12-01  3:19   ` Yonghong Song

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).