Skip to contentSkip to conversation

tracing: accelerate tracing filters with BPF

RFC PATCH In review Alexei Starovoitov 3 participants 8 messages May 14, 2014 – May 15, 2014

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

RFC PATCH 1/1 tracing: accelerate tracing filters with BPF

Tracing filters are parsing user supplied character string and constructing a predicate tree. filter_match_preds() was used to walk nodes of the tree to simulate matching of boolean expression.

BPF program obsoletes predicate tree walker. It is generated on the fly out of predicate tree.

Tested with built-in FTRACE_STARTUP_TEST.
The same 24 tests from trace_events_filter.c were used as boot-time benchmark.

Performance on x86_64:
predicate BPF JITed
tree walking interpreter BPF

 test #0:    82        44         4    nsec per call
 test #1:    60         9         3    nsec per call
 test #2:    83        45         4    nsec per call
 test #3:    82        45         5    nsec per call
 test #4:    81        26         5    nsec per call
 test #5:    60         9         4    nsec per call
 test #6:    95        11         3    nsec per call
 test #7:   144        49         6    nsec per call
 test #8:   139        47         6    nsec per call
 test #9:   139        24         5    nsec per call
 test #10:  113        18         4    nsec per call
 test #11:  137        21         6    nsec per call
 test #12:  180        41         5    nsec per call
 test #13:   86        39         4    nsec per call
 test #14:  101        12         4    nsec per call
 test #15:  121        45         5    nsec per call
 test #16:  103        15         4    nsec per call
 test #17:   87        39         4    nsec per call
 test #18:  101        12         4    nsec per call
 test #19:  161        41         6    nsec per call
 test #20:  159        40         6    nsec per call
 test #21:  206        63         6    nsec per call
 test #22:  160        39         6    nsec per call
 test #23:  204        59         6    nsec per call
 test #24:  160        39         6    nsec per call

Example 1:
string filter "field1 == 1 || field2 == 2" is converted into BPF:
r6 = r1
r0 = *(u16 *)(r6 +0) // field1.offset = 0, field1.size = 2 if r0 == 0x1 goto pc+3
r0 = *(u8 *)(r6 +2) // field2.offset = 2, field2.size = 1 if r0 == 0x2 goto pc+1
goto pc+2
r0 = 1
exit
r0 = 0
exit

Example 2:
filter "name ~ eth* && field2 == 1" is converted:
r6 = r1
r2 = 1234 // upper 32-bits of predicate addr
r2 <<= 32
r1 = 5678 // lower 32-bits of predicate addr
r1 |= r2
r2 = r6
// call wrapper around filter_pred_strloc(struct filter_pred *pred, void *event) call bpf_filter_pred_strloc
if r0 == 0x0 goto pc+5
r0 = *(u32 *)(r6 +16) // field2.offset = 16, field2.size = 4
if r0 == 0x1 goto pc+1
goto pc+2
r0 = 1
exit
r0 = 0
exit

Internal BPF has 'bpf_call' instruction for function calls from BPF program into in-kernel helper functions. It is used to do 'regex'-style filter matching in the example above. All generated BPF programs can be JITed.

---

The patches for x64_64 BPF JIT were sent separately. This one is an RFC.

 kernel/trace/Kconfig               |    1 +
 kernel/trace/trace.h               |    4 +-
 kernel/trace/trace_events_filter.c |  624 ++++++++++++++++++++----------------
 3 files changed, 343 insertions(+), 286 deletions(-)
kernel/trace/Kconfig +1 -0
index 8639819f6cef..a192acd8ae58 100644
@@ -79,6 +79,7 @@ config FTRACE_NMI_ENTER
        default y
 
 config EVENT_TRACING
+	depends on NET
 	select CONTEXT_SWITCH_TRACER
 	bool
 
kernel/trace/trace.h +3 -1
index 2e29d7ba5a52..bb73c0243f86 100644
@@ -970,12 +970,15 @@ struct ftrace_event_field {
 	int			is_signed;
 };
 
+struct sk_filter;
+
 struct event_filter {
 	int			n_preds;	/* Number assigned */
 	int			a_preds;	/* allocated */
 	struct filter_pred	*preds;
 	struct filter_pred	*root;
 	char			*filter_string;
+	struct sk_filter	*bpf_prog;
 };
 
 struct event_subsystem {
@@ -1032,7 +1035,6 @@ struct filter_pred {
 	filter_pred_fn_t 	fn;
 	u64 			val;
 	struct regex		regex;
-	unsigned short		*ops;
 	struct ftrace_event_field *field;
 	int 			offset;
 	int 			not;
kernel/trace/trace_events_filter.c +339 -285

744 lines not on this page · raw message

Signature · 1 line
1.7.9.5
maintainer
#1 lore raw

kernel/trace/Kconfig config FTRACE_NMI_ENTER · line 85

Quoted 10 lines · On Tue, 13 May 2014 19:55:11 -0700 Alexei Starovoitov <[email protected]> wrote:
kernel/trace/Kconfig +1 -0
@@ -79,6 +79,7 @@ config FTRACE_NMI_ENTER
        default y
 
 config EVENT_TRACING
+	depends on NET
 	select CONTEXT_SWITCH_TRACER
 	bool
 

This part makes me a little nervous. I know a lot of embedded devices depend on tracing, but do they all have network?

They may, but I would like to hear from others in the embedded space.

-- Steve

maintainer
#4 lore raw
In reply to Alexei Starovoitov
Quoted 4 lines · On Tue, 13 May 2014 19:55:11 -0700 Alexei Starovoitov <[email protected]> wrote:

Tracing filters are parsing user supplied character string and constructing a predicate tree. filter_match_preds() was used to walk nodes of the tree to simulate matching of boolean expression.

I applied this on top of net-next, from:

git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next.git
(master branch)

and got this compile error:

kernel/trace/trace_events_filter.c: In function ‘emit_cond’:
kernel/trace/trace_events_filter.c:1361:36: error: ‘__bpf_call_base’ undeclared (first use in this function) kernel/trace/trace_events_filter.c:1361:36: note: each undeclared identifier is reported only once for each function it appears in kernel/trace/trace_events_filter.c: In function ‘filter_gen_bpf’:
kernel/trace/trace_events_filter.c:1622:2: error: implicit declaration of function ‘bpf_int_jit_compile’ [-Werror=implicit-function-declaration]

Config attached.

-- Steve

#2 lore raw
In reply to Steven Rostedt
Quoted 8 lines · Steven Rostedt
Quoted 5 lines · On Tue, 13 May 2014 19:55:11 -0700 Alexei Starovoitov <[email protected]> wrote:
kernel/trace/Kconfig +0 -0
@@ -79,6 +79,7 @@ config FTRACE_NMI_ENTER
        default y
 
Quoted 8 lines

kernel/trace/Kconfig config FTRACE_NMI_ENTER · line 85

Quoted 5 lines
kernel/trace/Kconfig +1 -0
 config EVENT_TRACING
+     depends on NET
      select CONTEXT_SWITCH_TRACER
      bool
 

This part makes me a little nervous. I know a lot of embedded devices depend on tracing, but do they all have network?

I think we may want to split BPF into its own config, since it doesn't really depend on anything in net.

Quoted 3 lines

They may, but I would like to hear from others in the embedded space.

-- Steve

#5 lore raw
In reply to Steven Rostedt
Quoted 8 lines · Steven Rostedt
Quoted 4 lines · On Tue, 13 May 2014 19:55:11 -0700 Alexei Starovoitov <[email protected]> wrote:

Tracing filters are parsing user supplied character string and constructing a predicate tree. filter_match_preds() was used to walk nodes of the tree to simulate matching of boolean expression.

Quoted 6 lines

I applied this on top of net-next, from:

git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next.git
(master branch)

and got this compile error:

Sorry I wasn't clear in the log.
This patch depends on JIT patches that were posted separately. Since this one is RFC, I didn't want to mix them. JIT patches:
http://patchwork.ozlabs.org/patch/348558/
http://patchwork.ozlabs.org/patch/348559/
http://patchwork.ozlabs.org/patch/348560/

Quoted 9 lines

kernel/trace/trace_events_filter.c: In function ‘emit_cond’:
kernel/trace/trace_events_filter.c:1361:36: error: ‘__bpf_call_base’ undeclared (first use in this function) kernel/trace/trace_events_filter.c:1361:36: note: each undeclared identifier is reported only once for each function it appears in kernel/trace/trace_events_filter.c: In function ‘filter_gen_bpf’:
kernel/trace/trace_events_filter.c:1622:2: error: implicit declaration of function ‘bpf_int_jit_compile’ [-Werror=implicit-function-declaration]

Config attached.

-- Steve

#3 lore raw
In reply to Alexei Starovoitov
Quoted 18 lines · Alexei Starovoitov
Quoted 8 lines · Steven Rostedt
Quoted 5 lines · On Tue, 13 May 2014 19:55:11 -0700 Alexei Starovoitov <[email protected]> wrote:
kernel/trace/Kconfig +0 -0
@@ -79,6 +79,7 @@ config FTRACE_NMI_ENTER
        default y
 
Quoted 8 lines

kernel/trace/Kconfig config FTRACE_NMI_ENTER · line 85

Quoted 5 lines
kernel/trace/Kconfig +1 -0
 config EVENT_TRACING
+     depends on NET
      select CONTEXT_SWITCH_TRACER
      bool
 

This part makes me a little nervous. I know a lot of embedded devices depend on tracing, but do they all have network?

Quoted 2 lines

I think we may want to split BPF into its own config, since it doesn't really depend on anything in net.

Exactly, that's a good plan.

Thanks,

Ingo

#6 lore raw
In reply to Alexei Starovoitov
Quoted 16 lines · Alexei Starovoitov
Quoted 8 lines · Steven Rostedt
Quoted 4 lines · On Tue, 13 May 2014 19:55:11 -0700 Alexei Starovoitov <[email protected]> wrote:

Tracing filters are parsing user supplied character string and constructing a predicate tree. filter_match_preds() was used to walk nodes of the tree to simulate matching of boolean expression.

Quoted 6 lines

I applied this on top of net-next, from:

git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next.git
(master branch)

and got this compile error:

Quoted 7 lines

Sorry I wasn't clear in the log.
This patch depends on JIT patches that were posted separately. Since this one is RFC, I didn't want to mix them. JIT patches:
http://patchwork.ozlabs.org/patch/348558/
http://patchwork.ozlabs.org/patch/348559/
http://patchwork.ozlabs.org/patch/348560/

Might make sense for you to get a korg account and stick such bits into Git trees, to ease testing.

Thanks,

Ingo

#7 lore raw
Quoted 27 lines · Ingo Molnar
Quoted 16 lines · Alexei Starovoitov
Quoted 8 lines · Steven Rostedt
Quoted 4 lines · On Tue, 13 May 2014 19:55:11 -0700 Alexei Starovoitov <[email protected]> wrote:

Tracing filters are parsing user supplied character string and constructing a predicate tree. filter_match_preds() was used to walk nodes of the tree to simulate matching of boolean expression.

Quoted 6 lines

I applied this on top of net-next, from:

git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next.git
(master branch)

and got this compile error:

Quoted 7 lines

Sorry I wasn't clear in the log.
This patch depends on JIT patches that were posted separately. Since this one is RFC, I didn't want to mix them. JIT patches:
http://patchwork.ozlabs.org/patch/348558/
http://patchwork.ozlabs.org/patch/348559/
http://patchwork.ozlabs.org/patch/348560/

Quoted 2 lines

Might make sense for you to get a korg account and stick such bits into Git trees, to ease testing.

it would be an honor. Thank you.

What is the next step for this particular patch? Sounds like dependency on full net is a showstopper. I think splitting bpf into small component will clean up dependency for seccomp as well.
bpf can move into net/bpf/ dir and go via net-next tree. If Dave is ok with this, I can start hacking.
It looks to be more or less straight forward. There are few ways to split it. The only contentious piece is skb_copy_bits(). I think we can come up with something.