Merge tag 'for-linus-20180920' of git://git.kernel.dk/linux-block
[linux-2.6-block.git] / kernel / trace / trace_events_filter.c
CommitLineData
bcea3f96 1// SPDX-License-Identifier: GPL-2.0
7ce7e424
TZ
2/*
3 * trace_events_filter - generic event filtering
4 *
7ce7e424
TZ
5 * Copyright (C) 2009 Tom Zanussi <tzanussi@gmail.com>
6 */
7
7ce7e424
TZ
8#include <linux/module.h>
9#include <linux/ctype.h>
ac1adc55 10#include <linux/mutex.h>
6fb2915d 11#include <linux/perf_event.h>
5a0e3ad6 12#include <linux/slab.h>
7ce7e424
TZ
13
14#include "trace.h"
4bda2d51 15#include "trace_output.h"
7ce7e424 16
49aa2951
SR
17#define DEFAULT_SYS_FILTER_MESSAGE \
18 "### global filter ###\n" \
19 "# Use this to set filters for multiple events.\n" \
20 "# Only events with the given fields will be affected.\n" \
21 "# If no events are modified, an error message will be displayed here"
22
80765597 23/* Due to token parsing '<=' must be before '<' and '>=' must be before '>' */
e9baef0d 24#define OPS \
80765597
SRV
25 C( OP_GLOB, "~" ), \
26 C( OP_NE, "!=" ), \
27 C( OP_EQ, "==" ), \
28 C( OP_LE, "<=" ), \
29 C( OP_LT, "<" ), \
30 C( OP_GE, ">=" ), \
31 C( OP_GT, ">" ), \
32 C( OP_BAND, "&" ), \
33 C( OP_MAX, NULL )
e9baef0d
SRV
34
35#undef C
80765597 36#define C(a, b) a
e9baef0d
SRV
37
38enum filter_op_ids { OPS };
8b372562 39
e9baef0d 40#undef C
80765597 41#define C(a, b) b
8b372562 42
80765597 43static const char * ops[] = { OPS };
8b372562 44
478325f1 45/*
80765597 46 * pred functions are OP_LE, OP_LT, OP_GE, OP_GT, and OP_BAND
478325f1
SRV
47 * pred_funcs_##type below must match the order of them above.
48 */
80765597 49#define PRED_FUNC_START OP_LE
478325f1
SRV
50#define PRED_FUNC_MAX (OP_BAND - PRED_FUNC_START)
51
e9baef0d 52#define ERRORS \
80765597
SRV
53 C(NONE, "No error"), \
54 C(INVALID_OP, "Invalid operator"), \
55 C(TOO_MANY_OPEN, "Too many '('"), \
56 C(TOO_MANY_CLOSE, "Too few '('"), \
57 C(MISSING_QUOTE, "Missing matching quote"), \
58 C(OPERAND_TOO_LONG, "Operand too long"), \
59 C(EXPECT_STRING, "Expecting string field"), \
60 C(EXPECT_DIGIT, "Expecting numeric field"), \
61 C(ILLEGAL_FIELD_OP, "Illegal operation for field type"), \
62 C(FIELD_NOT_FOUND, "Field not found"), \
63 C(ILLEGAL_INTVAL, "Illegal integer value"), \
64 C(BAD_SUBSYS_FILTER, "Couldn't find or set field in one of a subsystem's events"), \
65 C(TOO_MANY_PREDS, "Too many terms in predicate expression"), \
66 C(INVALID_FILTER, "Meaningless filter expression"), \
67 C(IP_FIELD_ONLY, "Only 'ip' field is supported for function trace"), \
70303420
SRV
68 C(INVALID_VALUE, "Invalid value (did you forget quotes)?"), \
69 C(NO_FILTER, "No filter found"),
e9baef0d
SRV
70
71#undef C
72#define C(a, b) FILT_ERR_##a
73
74enum { ERRORS };
75
76#undef C
77#define C(a, b) b
78
79static char *err_text[] = { ERRORS };
8b372562 80
80765597
SRV
81/* Called after a '!' character but "!=" and "!~" are not "not"s */
82static bool is_not(const char *str)
83{
84 switch (str[1]) {
85 case '=':
86 case '~':
87 return false;
88 }
89 return true;
90}
8b372562 91
80765597
SRV
92/**
93 * prog_entry - a singe entry in the filter program
94 * @target: Index to jump to on a branch (actually one minus the index)
95 * @when_to_branch: The value of the result of the predicate to do a branch
96 * @pred: The predicate to execute.
97 */
98struct prog_entry {
99 int target;
100 int when_to_branch;
101 struct filter_pred *pred;
8b372562
TZ
102};
103
80765597
SRV
104/**
105 * update_preds- assign a program entry a label target
106 * @prog: The program array
107 * @N: The index of the current entry in @prog
108 * @when_to_branch: What to assign a program entry for its branch condition
109 *
110 * The program entry at @N has a target that points to the index of a program
111 * entry that can have its target and when_to_branch fields updated.
112 * Update the current program entry denoted by index @N target field to be
113 * that of the updated entry. This will denote the entry to update if
114 * we are processing an "||" after an "&&"
115 */
116static void update_preds(struct prog_entry *prog, int N, int invert)
117{
118 int t, s;
119
120 t = prog[N].target;
121 s = prog[t].target;
122 prog[t].when_to_branch = invert;
123 prog[t].target = N;
124 prog[N].target = s;
125}
126
127struct filter_parse_error {
8b372562
TZ
128 int lasterr;
129 int lasterr_pos;
8b372562
TZ
130};
131
80765597
SRV
132static void parse_error(struct filter_parse_error *pe, int err, int pos)
133{
134 pe->lasterr = err;
135 pe->lasterr_pos = pos;
136}
137
138typedef int (*parse_pred_fn)(const char *str, void *data, int pos,
139 struct filter_parse_error *pe,
140 struct filter_pred **pred);
141
142enum {
143 INVERT = 1,
144 PROCESS_AND = 2,
145 PROCESS_OR = 4,
61e9dea2
SR
146};
147
80765597
SRV
148/*
149 * Without going into a formal proof, this explains the method that is used in
150 * parsing the logical expressions.
151 *
152 * For example, if we have: "a && !(!b || (c && g)) || d || e && !f"
153 * The first pass will convert it into the following program:
154 *
155 * n1: r=a; l1: if (!r) goto l4;
156 * n2: r=b; l2: if (!r) goto l4;
157 * n3: r=c; r=!r; l3: if (r) goto l4;
158 * n4: r=g; r=!r; l4: if (r) goto l5;
159 * n5: r=d; l5: if (r) goto T
160 * n6: r=e; l6: if (!r) goto l7;
161 * n7: r=f; r=!r; l7: if (!r) goto F
162 * T: return TRUE
163 * F: return FALSE
164 *
165 * To do this, we use a data structure to represent each of the above
166 * predicate and conditions that has:
167 *
168 * predicate, when_to_branch, invert, target
169 *
170 * The "predicate" will hold the function to determine the result "r".
171 * The "when_to_branch" denotes what "r" should be if a branch is to be taken
172 * "&&" would contain "!r" or (0) and "||" would contain "r" or (1).
173 * The "invert" holds whether the value should be reversed before testing.
174 * The "target" contains the label "l#" to jump to.
175 *
176 * A stack is created to hold values when parentheses are used.
177 *
178 * To simplify the logic, the labels will start at 0 and not 1.
179 *
180 * The possible invert values are 1 and 0. The number of "!"s that are in scope
181 * before the predicate determines the invert value, if the number is odd then
182 * the invert value is 1 and 0 otherwise. This means the invert value only
183 * needs to be toggled when a new "!" is introduced compared to what is stored
184 * on the stack, where parentheses were used.
185 *
186 * The top of the stack and "invert" are initialized to zero.
187 *
188 * ** FIRST PASS **
189 *
190 * #1 A loop through all the tokens is done:
191 *
192 * #2 If the token is an "(", the stack is push, and the current stack value
193 * gets the current invert value, and the loop continues to the next token.
194 * The top of the stack saves the "invert" value to keep track of what
195 * the current inversion is. As "!(a && !b || c)" would require all
196 * predicates being affected separately by the "!" before the parentheses.
197 * And that would end up being equivalent to "(!a || b) && !c"
198 *
199 * #3 If the token is an "!", the current "invert" value gets inverted, and
200 * the loop continues. Note, if the next token is a predicate, then
201 * this "invert" value is only valid for the current program entry,
202 * and does not affect other predicates later on.
203 *
204 * The only other acceptable token is the predicate string.
205 *
206 * #4 A new entry into the program is added saving: the predicate and the
207 * current value of "invert". The target is currently assigned to the
208 * previous program index (this will not be its final value).
209 *
210 * #5 We now enter another loop and look at the next token. The only valid
211 * tokens are ")", "&&", "||" or end of the input string "\0".
212 *
213 * #6 The invert variable is reset to the current value saved on the top of
214 * the stack.
215 *
216 * #7 The top of the stack holds not only the current invert value, but also
217 * if a "&&" or "||" needs to be processed. Note, the "&&" takes higher
218 * precedence than "||". That is "a && b || c && d" is equivalent to
219 * "(a && b) || (c && d)". Thus the first thing to do is to see if "&&" needs
220 * to be processed. This is the case if an "&&" was the last token. If it was
221 * then we call update_preds(). This takes the program, the current index in
222 * the program, and the current value of "invert". More will be described
223 * below about this function.
224 *
225 * #8 If the next token is "&&" then we set a flag in the top of the stack
226 * that denotes that "&&" needs to be processed, break out of this loop
227 * and continue with the outer loop.
228 *
229 * #9 Otherwise, if a "||" needs to be processed then update_preds() is called.
230 * This is called with the program, the current index in the program, but
231 * this time with an inverted value of "invert" (that is !invert). This is
232 * because the value taken will become the "when_to_branch" value of the
233 * program.
234 * Note, this is called when the next token is not an "&&". As stated before,
235 * "&&" takes higher precedence, and "||" should not be processed yet if the
236 * next logical operation is "&&".
237 *
238 * #10 If the next token is "||" then we set a flag in the top of the stack
239 * that denotes that "||" needs to be processed, break out of this loop
240 * and continue with the outer loop.
241 *
242 * #11 If this is the end of the input string "\0" then we break out of both
243 * loops.
244 *
245 * #12 Otherwise, the next token is ")", where we pop the stack and continue
246 * this inner loop.
247 *
248 * Now to discuss the update_pred() function, as that is key to the setting up
249 * of the program. Remember the "target" of the program is initialized to the
250 * previous index and not the "l" label. The target holds the index into the
251 * program that gets affected by the operand. Thus if we have something like
252 * "a || b && c", when we process "a" the target will be "-1" (undefined).
253 * When we process "b", its target is "0", which is the index of "a", as that's
254 * the predicate that is affected by "||". But because the next token after "b"
255 * is "&&" we don't call update_preds(). Instead continue to "c". As the
256 * next token after "c" is not "&&" but the end of input, we first process the
257 * "&&" by calling update_preds() for the "&&" then we process the "||" by
258 * callin updates_preds() with the values for processing "||".
259 *
260 * What does that mean? What update_preds() does is to first save the "target"
261 * of the program entry indexed by the current program entry's "target"
262 * (remember the "target" is initialized to previous program entry), and then
263 * sets that "target" to the current index which represents the label "l#".
264 * That entry's "when_to_branch" is set to the value passed in (the "invert"
265 * or "!invert"). Then it sets the current program entry's target to the saved
266 * "target" value (the old value of the program that had its "target" updated
267 * to the label).
268 *
269 * Looking back at "a || b && c", we have the following steps:
270 * "a" - prog[0] = { "a", X, -1 } // pred, when_to_branch, target
271 * "||" - flag that we need to process "||"; continue outer loop
272 * "b" - prog[1] = { "b", X, 0 }
273 * "&&" - flag that we need to process "&&"; continue outer loop
274 * (Notice we did not process "||")
275 * "c" - prog[2] = { "c", X, 1 }
276 * update_preds(prog, 2, 0); // invert = 0 as we are processing "&&"
277 * t = prog[2].target; // t = 1
278 * s = prog[t].target; // s = 0
279 * prog[t].target = 2; // Set target to "l2"
280 * prog[t].when_to_branch = 0;
281 * prog[2].target = s;
282 * update_preds(prog, 2, 1); // invert = 1 as we are now processing "||"
283 * t = prog[2].target; // t = 0
284 * s = prog[t].target; // s = -1
285 * prog[t].target = 2; // Set target to "l2"
286 * prog[t].when_to_branch = 1;
287 * prog[2].target = s;
288 *
289 * #13 Which brings us to the final step of the first pass, which is to set
290 * the last program entry's when_to_branch and target, which will be
291 * when_to_branch = 0; target = N; ( the label after the program entry after
292 * the last program entry processed above).
293 *
294 * If we denote "TRUE" to be the entry after the last program entry processed,
295 * and "FALSE" the program entry after that, we are now done with the first
296 * pass.
297 *
298 * Making the above "a || b && c" have a progam of:
299 * prog[0] = { "a", 1, 2 }
300 * prog[1] = { "b", 0, 2 }
301 * prog[2] = { "c", 0, 3 }
302 *
303 * Which translates into:
304 * n0: r = a; l0: if (r) goto l2;
305 * n1: r = b; l1: if (!r) goto l2;
306 * n2: r = c; l2: if (!r) goto l3; // Which is the same as "goto F;"
307 * T: return TRUE; l3:
308 * F: return FALSE
309 *
310 * Although, after the first pass, the program is correct, it is
311 * inefficient. The simple sample of "a || b && c" could be easily been
312 * converted into:
313 * n0: r = a; if (r) goto T
314 * n1: r = b; if (!r) goto F
315 * n2: r = c; if (!r) goto F
316 * T: return TRUE;
317 * F: return FALSE;
318 *
319 * The First Pass is over the input string. The next too passes are over
320 * the program itself.
321 *
322 * ** SECOND PASS **
323 *
324 * Which brings us to the second pass. If a jump to a label has the
325 * same condition as that label, it can instead jump to its target.
326 * The original example of "a && !(!b || (c && g)) || d || e && !f"
327 * where the first pass gives us:
328 *
329 * n1: r=a; l1: if (!r) goto l4;
330 * n2: r=b; l2: if (!r) goto l4;
331 * n3: r=c; r=!r; l3: if (r) goto l4;
332 * n4: r=g; r=!r; l4: if (r) goto l5;
333 * n5: r=d; l5: if (r) goto T
334 * n6: r=e; l6: if (!r) goto l7;
335 * n7: r=f; r=!r; l7: if (!r) goto F:
336 * T: return TRUE;
337 * F: return FALSE
338 *
339 * We can see that "l3: if (r) goto l4;" and at l4, we have "if (r) goto l5;".
340 * And "l5: if (r) goto T", we could optimize this by converting l3 and l4
341 * to go directly to T. To accomplish this, we start from the last
342 * entry in the program and work our way back. If the target of the entry
343 * has the same "when_to_branch" then we could use that entry's target.
344 * Doing this, the above would end up as:
345 *
346 * n1: r=a; l1: if (!r) goto l4;
347 * n2: r=b; l2: if (!r) goto l4;
348 * n3: r=c; r=!r; l3: if (r) goto T;
349 * n4: r=g; r=!r; l4: if (r) goto T;
350 * n5: r=d; l5: if (r) goto T;
351 * n6: r=e; l6: if (!r) goto F;
352 * n7: r=f; r=!r; l7: if (!r) goto F;
353 * T: return TRUE
354 * F: return FALSE
355 *
356 * In that same pass, if the "when_to_branch" doesn't match, we can simply
357 * go to the program entry after the label. That is, "l2: if (!r) goto l4;"
358 * where "l4: if (r) goto T;", then we can convert l2 to be:
359 * "l2: if (!r) goto n5;".
360 *
361 * This will have the second pass give us:
362 * n1: r=a; l1: if (!r) goto n5;
363 * n2: r=b; l2: if (!r) goto n5;
364 * n3: r=c; r=!r; l3: if (r) goto T;
365 * n4: r=g; r=!r; l4: if (r) goto T;
366 * n5: r=d; l5: if (r) goto T
367 * n6: r=e; l6: if (!r) goto F;
368 * n7: r=f; r=!r; l7: if (!r) goto F
369 * T: return TRUE
370 * F: return FALSE
371 *
372 * Notice, all the "l#" labels are no longer used, and they can now
373 * be discarded.
374 *
375 * ** THIRD PASS **
376 *
377 * For the third pass we deal with the inverts. As they simply just
378 * make the "when_to_branch" get inverted, a simple loop over the
379 * program to that does: "when_to_branch ^= invert;" will do the
380 * job, leaving us with:
381 * n1: r=a; if (!r) goto n5;
382 * n2: r=b; if (!r) goto n5;
383 * n3: r=c: if (!r) goto T;
384 * n4: r=g; if (!r) goto T;
385 * n5: r=d; if (r) goto T
386 * n6: r=e; if (!r) goto F;
387 * n7: r=f; if (r) goto F
388 * T: return TRUE
389 * F: return FALSE
390 *
391 * As "r = a; if (!r) goto n5;" is obviously the same as
392 * "if (!a) goto n5;" without doing anything we can interperate the
393 * program as:
394 * n1: if (!a) goto n5;
395 * n2: if (!b) goto n5;
396 * n3: if (!c) goto T;
397 * n4: if (!g) goto T;
398 * n5: if (d) goto T
399 * n6: if (!e) goto F;
400 * n7: if (f) goto F
401 * T: return TRUE
402 * F: return FALSE
403 *
404 * Since the inverts are discarded at the end, there's no reason to store
405 * them in the program array (and waste memory). A separate array to hold
406 * the inverts is used and freed at the end.
407 */
408static struct prog_entry *
409predicate_parse(const char *str, int nr_parens, int nr_preds,
410 parse_pred_fn parse_pred, void *data,
411 struct filter_parse_error *pe)
412{
413 struct prog_entry *prog_stack;
414 struct prog_entry *prog;
415 const char *ptr = str;
416 char *inverts = NULL;
417 int *op_stack;
418 int *top;
419 int invert = 0;
420 int ret = -ENOMEM;
421 int len;
422 int N = 0;
423 int i;
424
425 nr_preds += 2; /* For TRUE and FALSE */
426
6da2ec56 427 op_stack = kmalloc_array(nr_parens, sizeof(*op_stack), GFP_KERNEL);
80765597
SRV
428 if (!op_stack)
429 return ERR_PTR(-ENOMEM);
6da2ec56 430 prog_stack = kmalloc_array(nr_preds, sizeof(*prog_stack), GFP_KERNEL);
80765597
SRV
431 if (!prog_stack) {
432 parse_error(pe, -ENOMEM, 0);
433 goto out_free;
434 }
6da2ec56 435 inverts = kmalloc_array(nr_preds, sizeof(*inverts), GFP_KERNEL);
80765597
SRV
436 if (!inverts) {
437 parse_error(pe, -ENOMEM, 0);
438 goto out_free;
439 }
440
441 top = op_stack;
442 prog = prog_stack;
443 *top = 0;
444
445 /* First pass */
446 while (*ptr) { /* #1 */
447 const char *next = ptr++;
448
449 if (isspace(*next))
450 continue;
451
452 switch (*next) {
453 case '(': /* #2 */
454 if (top - op_stack > nr_parens)
455 return ERR_PTR(-EINVAL);
456 *(++top) = invert;
457 continue;
458 case '!': /* #3 */
459 if (!is_not(next))
460 break;
461 invert = !invert;
462 continue;
463 }
464
465 if (N >= nr_preds) {
466 parse_error(pe, FILT_ERR_TOO_MANY_PREDS, next - str);
467 goto out_free;
468 }
469
470 inverts[N] = invert; /* #4 */
471 prog[N].target = N-1;
472
473 len = parse_pred(next, data, ptr - str, pe, &prog[N].pred);
474 if (len < 0) {
475 ret = len;
476 goto out_free;
477 }
478 ptr = next + len;
479
480 N++;
481
482 ret = -1;
483 while (1) { /* #5 */
484 next = ptr++;
485 if (isspace(*next))
486 continue;
487
488 switch (*next) {
489 case ')':
490 case '\0':
491 break;
492 case '&':
493 case '|':
494 if (next[1] == next[0]) {
495 ptr++;
496 break;
497 }
498 default:
499 parse_error(pe, FILT_ERR_TOO_MANY_PREDS,
500 next - str);
501 goto out_free;
502 }
503
504 invert = *top & INVERT;
505
506 if (*top & PROCESS_AND) { /* #7 */
507 update_preds(prog, N - 1, invert);
508 *top &= ~PROCESS_AND;
509 }
510 if (*next == '&') { /* #8 */
511 *top |= PROCESS_AND;
512 break;
513 }
514 if (*top & PROCESS_OR) { /* #9 */
515 update_preds(prog, N - 1, !invert);
516 *top &= ~PROCESS_OR;
517 }
518 if (*next == '|') { /* #10 */
519 *top |= PROCESS_OR;
520 break;
521 }
522 if (!*next) /* #11 */
523 goto out;
524
525 if (top == op_stack) {
526 ret = -1;
527 /* Too few '(' */
528 parse_error(pe, FILT_ERR_TOO_MANY_CLOSE, ptr - str);
529 goto out_free;
530 }
531 top--; /* #12 */
532 }
533 }
534 out:
535 if (top != op_stack) {
536 /* Too many '(' */
537 parse_error(pe, FILT_ERR_TOO_MANY_OPEN, ptr - str);
538 goto out_free;
539 }
540
70303420
SRV
541 if (!N) {
542 /* No program? */
543 ret = -EINVAL;
544 parse_error(pe, FILT_ERR_NO_FILTER, ptr - str);
545 goto out_free;
546 }
547
80765597
SRV
548 prog[N].pred = NULL; /* #13 */
549 prog[N].target = 1; /* TRUE */
550 prog[N+1].pred = NULL;
551 prog[N+1].target = 0; /* FALSE */
552 prog[N-1].target = N;
553 prog[N-1].when_to_branch = false;
554
555 /* Second Pass */
556 for (i = N-1 ; i--; ) {
557 int target = prog[i].target;
558 if (prog[i].when_to_branch == prog[target].when_to_branch)
559 prog[i].target = prog[target].target;
560 }
561
562 /* Third Pass */
563 for (i = 0; i < N; i++) {
564 invert = inverts[i] ^ prog[i].when_to_branch;
565 prog[i].when_to_branch = invert;
566 /* Make sure the program always moves forward */
567 if (WARN_ON(prog[i].target <= i)) {
568 ret = -EINVAL;
569 goto out_free;
570 }
571 }
572
573 return prog;
574out_free:
575 kfree(op_stack);
576 kfree(prog_stack);
577 kfree(inverts);
578 return ERR_PTR(ret);
579}
580
197e2eab 581#define DEFINE_COMPARISON_PRED(type) \
fdf5b679 582static int filter_pred_LT_##type(struct filter_pred *pred, void *event) \
197e2eab
LZ
583{ \
584 type *addr = (type *)(event + pred->offset); \
585 type val = (type)pred->val; \
80765597 586 return *addr < val; \
fdf5b679
SRRH
587} \
588static int filter_pred_LE_##type(struct filter_pred *pred, void *event) \
589{ \
590 type *addr = (type *)(event + pred->offset); \
591 type val = (type)pred->val; \
80765597 592 return *addr <= val; \
fdf5b679
SRRH
593} \
594static int filter_pred_GT_##type(struct filter_pred *pred, void *event) \
595{ \
596 type *addr = (type *)(event + pred->offset); \
597 type val = (type)pred->val; \
80765597 598 return *addr > val; \
fdf5b679
SRRH
599} \
600static int filter_pred_GE_##type(struct filter_pred *pred, void *event) \
601{ \
602 type *addr = (type *)(event + pred->offset); \
603 type val = (type)pred->val; \
80765597 604 return *addr >= val; \
fdf5b679
SRRH
605} \
606static int filter_pred_BAND_##type(struct filter_pred *pred, void *event) \
607{ \
608 type *addr = (type *)(event + pred->offset); \
609 type val = (type)pred->val; \
80765597 610 return !!(*addr & val); \
fdf5b679
SRRH
611} \
612static const filter_pred_fn_t pred_funcs_##type[] = { \
fdf5b679 613 filter_pred_LE_##type, \
80765597 614 filter_pred_LT_##type, \
fdf5b679 615 filter_pred_GE_##type, \
80765597 616 filter_pred_GT_##type, \
fdf5b679
SRRH
617 filter_pred_BAND_##type, \
618};
619
197e2eab 620#define DEFINE_EQUALITY_PRED(size) \
58d9a597 621static int filter_pred_##size(struct filter_pred *pred, void *event) \
197e2eab
LZ
622{ \
623 u##size *addr = (u##size *)(event + pred->offset); \
624 u##size val = (u##size)pred->val; \
625 int match; \
626 \
627 match = (val == *addr) ^ pred->not; \
628 \
629 return match; \
630}
631
8b372562
TZ
632DEFINE_COMPARISON_PRED(s64);
633DEFINE_COMPARISON_PRED(u64);
634DEFINE_COMPARISON_PRED(s32);
635DEFINE_COMPARISON_PRED(u32);
636DEFINE_COMPARISON_PRED(s16);
637DEFINE_COMPARISON_PRED(u16);
638DEFINE_COMPARISON_PRED(s8);
639DEFINE_COMPARISON_PRED(u8);
640
641DEFINE_EQUALITY_PRED(64);
642DEFINE_EQUALITY_PRED(32);
643DEFINE_EQUALITY_PRED(16);
644DEFINE_EQUALITY_PRED(8);
645
e8808c10 646/* Filter predicate for fixed sized arrays of characters */
58d9a597 647static int filter_pred_string(struct filter_pred *pred, void *event)
7ce7e424
TZ
648{
649 char *addr = (char *)(event + pred->offset);
650 int cmp, match;
651
1889d209 652 cmp = pred->regex.match(addr, &pred->regex, pred->regex.field_len);
7ce7e424 653
1889d209 654 match = cmp ^ pred->not;
7ce7e424
TZ
655
656 return match;
657}
658
87a342f5 659/* Filter predicate for char * pointers */
58d9a597 660static int filter_pred_pchar(struct filter_pred *pred, void *event)
87a342f5
LZ
661{
662 char **addr = (char **)(event + pred->offset);
663 int cmp, match;
16da27a8 664 int len = strlen(*addr) + 1; /* including tailing '\0' */
87a342f5 665
16da27a8 666 cmp = pred->regex.match(*addr, &pred->regex, len);
87a342f5 667
1889d209 668 match = cmp ^ pred->not;
87a342f5
LZ
669
670 return match;
671}
672
e8808c10
FW
673/*
674 * Filter predicate for dynamic sized arrays of characters.
675 * These are implemented through a list of strings at the end
676 * of the entry.
677 * Also each of these strings have a field in the entry which
678 * contains its offset from the beginning of the entry.
679 * We have then first to get this field, dereference it
680 * and add it to the address of the entry, and at last we have
681 * the address of the string.
682 */
58d9a597 683static int filter_pred_strloc(struct filter_pred *pred, void *event)
e8808c10 684{
7d536cb3
LZ
685 u32 str_item = *(u32 *)(event + pred->offset);
686 int str_loc = str_item & 0xffff;
687 int str_len = str_item >> 16;
e8808c10
FW
688 char *addr = (char *)(event + str_loc);
689 int cmp, match;
690
1889d209 691 cmp = pred->regex.match(addr, &pred->regex, str_len);
e8808c10 692
1889d209 693 match = cmp ^ pred->not;
e8808c10
FW
694
695 return match;
696}
697
9f616680
DW
698/* Filter predicate for CPUs. */
699static int filter_pred_cpu(struct filter_pred *pred, void *event)
700{
701 int cpu, cmp;
9f616680
DW
702
703 cpu = raw_smp_processor_id();
704 cmp = pred->val;
705
706 switch (pred->op) {
707 case OP_EQ:
80765597
SRV
708 return cpu == cmp;
709 case OP_NE:
710 return cpu != cmp;
9f616680 711 case OP_LT:
80765597 712 return cpu < cmp;
9f616680 713 case OP_LE:
80765597 714 return cpu <= cmp;
9f616680 715 case OP_GT:
80765597 716 return cpu > cmp;
9f616680 717 case OP_GE:
80765597 718 return cpu >= cmp;
9f616680 719 default:
80765597 720 return 0;
9f616680 721 }
9f616680
DW
722}
723
724/* Filter predicate for COMM. */
725static int filter_pred_comm(struct filter_pred *pred, void *event)
726{
80765597 727 int cmp;
9f616680
DW
728
729 cmp = pred->regex.match(current->comm, &pred->regex,
80765597
SRV
730 TASK_COMM_LEN);
731 return cmp ^ pred->not;
9f616680
DW
732}
733
58d9a597 734static int filter_pred_none(struct filter_pred *pred, void *event)
0a19e53c
TZ
735{
736 return 0;
737}
738
d1303dd1
LZ
739/*
740 * regex_match_foo - Basic regex callbacks
741 *
742 * @str: the string to be searched
743 * @r: the regex structure containing the pattern string
744 * @len: the length of the string to be searched (including '\0')
745 *
746 * Note:
747 * - @str might not be NULL-terminated if it's of type DYN_STRING
10f20e9f 748 * or STATIC_STRING, unless @len is zero.
d1303dd1
LZ
749 */
750
1889d209
FW
751static int regex_match_full(char *str, struct regex *r, int len)
752{
10f20e9f
SRV
753 /* len of zero means str is dynamic and ends with '\0' */
754 if (!len)
755 return strcmp(str, r->pattern) == 0;
756
757 return strncmp(str, r->pattern, len) == 0;
1889d209
FW
758}
759
760static int regex_match_front(char *str, struct regex *r, int len)
761{
10f20e9f 762 if (len && len < r->len)
dc432c3d
SRV
763 return 0;
764
10f20e9f 765 return strncmp(str, r->pattern, r->len) == 0;
1889d209
FW
766}
767
768static int regex_match_middle(char *str, struct regex *r, int len)
769{
10f20e9f
SRV
770 if (!len)
771 return strstr(str, r->pattern) != NULL;
772
773 return strnstr(str, r->pattern, len) != NULL;
1889d209
FW
774}
775
776static int regex_match_end(char *str, struct regex *r, int len)
777{
a3291c14 778 int strlen = len - 1;
1889d209 779
a3291c14
LZ
780 if (strlen >= r->len &&
781 memcmp(str + strlen - r->len, r->pattern, r->len) == 0)
1889d209
FW
782 return 1;
783 return 0;
784}
785
60f1d5e3
MH
786static int regex_match_glob(char *str, struct regex *r, int len __maybe_unused)
787{
788 if (glob_match(r->pattern, str))
789 return 1;
790 return 0;
791}
80765597 792
3f6fe06d
FW
793/**
794 * filter_parse_regex - parse a basic regex
795 * @buff: the raw regex
796 * @len: length of the regex
797 * @search: will point to the beginning of the string to compare
798 * @not: tell whether the match will have to be inverted
799 *
800 * This passes in a buffer containing a regex and this function will
1889d209
FW
801 * set search to point to the search part of the buffer and
802 * return the type of search it is (see enum above).
803 * This does modify buff.
804 *
805 * Returns enum type.
806 * search returns the pointer to use for comparison.
807 * not returns 1 if buff started with a '!'
808 * 0 otherwise.
809 */
3f6fe06d 810enum regex_type filter_parse_regex(char *buff, int len, char **search, int *not)
1889d209
FW
811{
812 int type = MATCH_FULL;
813 int i;
814
815 if (buff[0] == '!') {
816 *not = 1;
817 buff++;
818 len--;
819 } else
820 *not = 0;
821
822 *search = buff;
823
824 for (i = 0; i < len; i++) {
825 if (buff[i] == '*') {
826 if (!i) {
1889d209 827 type = MATCH_END_ONLY;
60f1d5e3 828 } else if (i == len - 1) {
1889d209
FW
829 if (type == MATCH_END_ONLY)
830 type = MATCH_MIDDLE_ONLY;
831 else
832 type = MATCH_FRONT_ONLY;
833 buff[i] = 0;
834 break;
60f1d5e3 835 } else { /* pattern continues, use full glob */
07234021 836 return MATCH_GLOB;
1889d209 837 }
60f1d5e3 838 } else if (strchr("[?\\", buff[i])) {
07234021 839 return MATCH_GLOB;
1889d209
FW
840 }
841 }
07234021
SRV
842 if (buff[0] == '*')
843 *search = buff + 1;
1889d209
FW
844
845 return type;
846}
847
b0f1a59a 848static void filter_build_regex(struct filter_pred *pred)
1889d209
FW
849{
850 struct regex *r = &pred->regex;
b0f1a59a
LZ
851 char *search;
852 enum regex_type type = MATCH_FULL;
b0f1a59a
LZ
853
854 if (pred->op == OP_GLOB) {
80765597 855 type = filter_parse_regex(r->pattern, r->len, &search, &pred->not);
b0f1a59a
LZ
856 r->len = strlen(search);
857 memmove(r->pattern, search, r->len+1);
858 }
1889d209
FW
859
860 switch (type) {
861 case MATCH_FULL:
862 r->match = regex_match_full;
863 break;
864 case MATCH_FRONT_ONLY:
865 r->match = regex_match_front;
866 break;
867 case MATCH_MIDDLE_ONLY:
868 r->match = regex_match_middle;
869 break;
870 case MATCH_END_ONLY:
871 r->match = regex_match_end;
872 break;
60f1d5e3
MH
873 case MATCH_GLOB:
874 r->match = regex_match_glob;
875 break;
1889d209 876 }
f30120fc
JO
877}
878
7ce7e424 879/* return 1 if event matches, 0 otherwise (discard) */
6fb2915d 880int filter_match_preds(struct event_filter *filter, void *rec)
7ce7e424 881{
80765597
SRV
882 struct prog_entry *prog;
883 int i;
7ce7e424 884
6d54057d 885 /* no filter is considered a match */
75b8e982
SR
886 if (!filter)
887 return 1;
888
e0a568dc
SRV
889 /* Protected by either SRCU(tracepoint_srcu) or preempt_disable */
890 prog = rcu_dereference_raw(filter->prog);
80765597 891 if (!prog)
6d54057d
SR
892 return 1;
893
80765597
SRV
894 for (i = 0; prog[i].pred; i++) {
895 struct filter_pred *pred = prog[i].pred;
896 int match = pred->fn(pred, rec);
897 if (match == prog[i].when_to_branch)
898 i = prog[i].target;
899 }
900 return prog[i].target;
7ce7e424 901}
17c873ec 902EXPORT_SYMBOL_GPL(filter_match_preds);
7ce7e424 903
8b372562
TZ
904static void remove_filter_string(struct event_filter *filter)
905{
75b8e982
SR
906 if (!filter)
907 return;
908
8b372562
TZ
909 kfree(filter->filter_string);
910 filter->filter_string = NULL;
911}
912
80765597 913static void append_filter_err(struct filter_parse_error *pe,
8b372562
TZ
914 struct event_filter *filter)
915{
559d4212 916 struct trace_seq *s;
80765597 917 int pos = pe->lasterr_pos;
559d4212
SRV
918 char *buf;
919 int len;
8b372562 920
559d4212 921 if (WARN_ON(!filter->filter_string))
4bda2d51 922 return;
7ce7e424 923
559d4212
SRV
924 s = kmalloc(sizeof(*s), GFP_KERNEL);
925 if (!s)
926 return;
927 trace_seq_init(s);
928
929 len = strlen(filter->filter_string);
930 if (pos > len)
80765597
SRV
931 pos = len;
932
933 /* indexing is off by one */
934 if (pos)
935 pos++;
559d4212
SRV
936
937 trace_seq_puts(s, filter->filter_string);
80765597
SRV
938 if (pe->lasterr > 0) {
939 trace_seq_printf(s, "\n%*s", pos, "^");
940 trace_seq_printf(s, "\nparse_error: %s\n", err_text[pe->lasterr]);
941 } else {
942 trace_seq_printf(s, "\nError: (%d)\n", pe->lasterr);
943 }
559d4212
SRV
944 trace_seq_putc(s, 0);
945 buf = kmemdup_nul(s->buffer, s->seq.len, GFP_KERNEL);
946 if (buf) {
947 kfree(filter->filter_string);
948 filter->filter_string = buf;
949 }
950 kfree(s);
7ce7e424
TZ
951}
952
7f1d2f82 953static inline struct event_filter *event_filter(struct trace_event_file *file)
f306cc82 954{
dcb0b557 955 return file->filter;
f306cc82
TZ
956}
957
e2912b09 958/* caller must hold event_mutex */
7f1d2f82 959void print_event_filter(struct trace_event_file *file, struct trace_seq *s)
ac1adc55 960{
f306cc82 961 struct event_filter *filter = event_filter(file);
8b372562 962
8e254c1d 963 if (filter && filter->filter_string)
8b372562
TZ
964 trace_seq_printf(s, "%s\n", filter->filter_string);
965 else
146c3442 966 trace_seq_puts(s, "none\n");
ac1adc55
TZ
967}
968
8b372562 969void print_subsystem_event_filter(struct event_subsystem *system,
ac1adc55
TZ
970 struct trace_seq *s)
971{
75b8e982 972 struct event_filter *filter;
8b372562 973
00e95830 974 mutex_lock(&event_mutex);
75b8e982 975 filter = system->filter;
8e254c1d 976 if (filter && filter->filter_string)
8b372562
TZ
977 trace_seq_printf(s, "%s\n", filter->filter_string);
978 else
146c3442 979 trace_seq_puts(s, DEFAULT_SYS_FILTER_MESSAGE "\n");
00e95830 980 mutex_unlock(&event_mutex);
ac1adc55
TZ
981}
982
80765597 983static void free_prog(struct event_filter *filter)
c9c53ca0 984{
80765597 985 struct prog_entry *prog;
60705c89
SRRH
986 int i;
987
80765597
SRV
988 prog = rcu_access_pointer(filter->prog);
989 if (!prog)
990 return;
991
992 for (i = 0; prog[i].pred; i++)
993 kfree(prog[i].pred);
994 kfree(prog);
c9c53ca0
SR
995}
996
7f1d2f82 997static void filter_disable(struct trace_event_file *file)
f306cc82 998{
0fc1b09f
SRRH
999 unsigned long old_flags = file->flags;
1000
dcb0b557 1001 file->flags &= ~EVENT_FILE_FL_FILTERED;
0fc1b09f
SRRH
1002
1003 if (old_flags != file->flags)
1004 trace_buffered_event_disable();
f306cc82
TZ
1005}
1006
c9c53ca0 1007static void __free_filter(struct event_filter *filter)
2df75e41 1008{
8e254c1d
LZ
1009 if (!filter)
1010 return;
1011
80765597 1012 free_prog(filter);
57be8887 1013 kfree(filter->filter_string);
2df75e41 1014 kfree(filter);
6fb2915d
LZ
1015}
1016
bac5fb97
TZ
1017void free_event_filter(struct event_filter *filter)
1018{
1019 __free_filter(filter);
1020}
1021
7f1d2f82 1022static inline void __remove_filter(struct trace_event_file *file)
8e254c1d 1023{
f306cc82 1024 filter_disable(file);
dcb0b557 1025 remove_filter_string(file->filter);
f306cc82
TZ
1026}
1027
7967b3e0 1028static void filter_free_subsystem_preds(struct trace_subsystem_dir *dir,
f306cc82
TZ
1029 struct trace_array *tr)
1030{
7f1d2f82 1031 struct trace_event_file *file;
8e254c1d 1032
f306cc82 1033 list_for_each_entry(file, &tr->events, list) {
bb9ef1cb 1034 if (file->system != dir)
8e254c1d 1035 continue;
f306cc82 1036 __remove_filter(file);
8e254c1d 1037 }
8e254c1d 1038}
7ce7e424 1039
7f1d2f82 1040static inline void __free_subsystem_filter(struct trace_event_file *file)
cfb180f3 1041{
dcb0b557
SRRH
1042 __free_filter(file->filter);
1043 file->filter = NULL;
f306cc82
TZ
1044}
1045
7967b3e0 1046static void filter_free_subsystem_filters(struct trace_subsystem_dir *dir,
f306cc82
TZ
1047 struct trace_array *tr)
1048{
7f1d2f82 1049 struct trace_event_file *file;
cfb180f3 1050
f306cc82 1051 list_for_each_entry(file, &tr->events, list) {
80765597 1052 if (file->system != dir)
7ce7e424 1053 continue;
80765597 1054 __free_subsystem_filter(file);
8b372562 1055 }
80765597 1056}
8b372562 1057
80765597
SRV
1058int filter_assign_type(const char *type)
1059{
1060 if (strstr(type, "__data_loc") && strstr(type, "char"))
1061 return FILTER_DYN_STRING;
8b372562 1062
80765597
SRV
1063 if (strchr(type, '[') && strstr(type, "char"))
1064 return FILTER_STATIC_STRING;
8b372562 1065
80765597 1066 return FILTER_OTHER;
8b372562
TZ
1067}
1068
80765597
SRV
1069static filter_pred_fn_t select_comparison_fn(enum filter_op_ids op,
1070 int field_size, int field_is_signed)
8b372562 1071{
80765597
SRV
1072 filter_pred_fn_t fn = NULL;
1073 int pred_func_index = -1;
81570d9c 1074
80765597
SRV
1075 switch (op) {
1076 case OP_EQ:
1077 case OP_NE:
1078 break;
1079 default:
1080 if (WARN_ON_ONCE(op < PRED_FUNC_START))
1081 return NULL;
1082 pred_func_index = op - PRED_FUNC_START;
1083 if (WARN_ON_ONCE(pred_func_index > PRED_FUNC_MAX))
1084 return NULL;
8b372562
TZ
1085 }
1086
80765597
SRV
1087 switch (field_size) {
1088 case 8:
1089 if (pred_func_index < 0)
1090 fn = filter_pred_64;
1091 else if (field_is_signed)
1092 fn = pred_funcs_s64[pred_func_index];
1093 else
1094 fn = pred_funcs_u64[pred_func_index];
1095 break;
1096 case 4:
1097 if (pred_func_index < 0)
1098 fn = filter_pred_32;
1099 else if (field_is_signed)
1100 fn = pred_funcs_s32[pred_func_index];
1101 else
1102 fn = pred_funcs_u32[pred_func_index];
1103 break;
1104 case 2:
1105 if (pred_func_index < 0)
1106 fn = filter_pred_16;
1107 else if (field_is_signed)
1108 fn = pred_funcs_s16[pred_func_index];
1109 else
1110 fn = pred_funcs_u16[pred_func_index];
1111 break;
1112 case 1:
1113 if (pred_func_index < 0)
1114 fn = filter_pred_8;
1115 else if (field_is_signed)
1116 fn = pred_funcs_s8[pred_func_index];
1117 else
1118 fn = pred_funcs_u8[pred_func_index];
1119 break;
61aaef55 1120 }
8b372562 1121
80765597 1122 return fn;
8b372562
TZ
1123}
1124
80765597
SRV
1125/* Called when a predicate is encountered by predicate_parse() */
1126static int parse_pred(const char *str, void *data,
1127 int pos, struct filter_parse_error *pe,
1128 struct filter_pred **pred_ptr)
8b372562 1129{
80765597
SRV
1130 struct trace_event_call *call = data;
1131 struct ftrace_event_field *field;
1132 struct filter_pred *pred = NULL;
1133 char num_buf[24]; /* Big enough to hold an address */
1134 char *field_name;
1135 char q;
1136 u64 val;
1137 int len;
1138 int ret;
1139 int op;
1140 int s;
1141 int i = 0;
8b372562 1142
80765597
SRV
1143 /* First find the field to associate to */
1144 while (isspace(str[i]))
1145 i++;
1146 s = i;
8b372562 1147
80765597
SRV
1148 while (isalnum(str[i]) || str[i] == '_')
1149 i++;
1150
1151 len = i - s;
1152
1153 if (!len)
1154 return -1;
1155
1156 field_name = kmemdup_nul(str + s, len, GFP_KERNEL);
1157 if (!field_name)
1158 return -ENOMEM;
1159
1160 /* Make sure that the field exists */
7ce7e424 1161
80765597
SRV
1162 field = trace_find_event_field(call, field_name);
1163 kfree(field_name);
1164 if (!field) {
1165 parse_error(pe, FILT_ERR_FIELD_NOT_FOUND, pos + i);
bcabd91c
LZ
1166 return -EINVAL;
1167 }
1168
80765597
SRV
1169 while (isspace(str[i]))
1170 i++;
f66578a7 1171
80765597
SRV
1172 /* Make sure this op is supported */
1173 for (op = 0; ops[op]; op++) {
1174 /* This is why '<=' must come before '<' in ops[] */
1175 if (strncmp(str + i, ops[op], strlen(ops[op])) == 0)
1176 break;
1177 }
c9c53ca0 1178
80765597
SRV
1179 if (!ops[op]) {
1180 parse_error(pe, FILT_ERR_INVALID_OP, pos + i);
1181 goto err_free;
c9c53ca0
SR
1182 }
1183
80765597 1184 i += strlen(ops[op]);
c9c53ca0 1185
80765597
SRV
1186 while (isspace(str[i]))
1187 i++;
f03f5979 1188
80765597 1189 s = i;
f03f5979 1190
80765597
SRV
1191 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
1192 if (!pred)
1193 return -ENOMEM;
f03f5979 1194
80765597
SRV
1195 pred->field = field;
1196 pred->offset = field->offset;
1197 pred->op = op;
1198
1199 if (ftrace_event_is_function(call)) {
f03f5979 1200 /*
80765597
SRV
1201 * Perf does things different with function events.
1202 * It only allows an "ip" field, and expects a string.
1203 * But the string does not need to be surrounded by quotes.
1204 * If it is a string, the assigned function as a nop,
1205 * (perf doesn't use it) and grab everything.
f03f5979 1206 */
80765597
SRV
1207 if (strcmp(field->name, "ip") != 0) {
1208 parse_error(pe, FILT_ERR_IP_FIELD_ONLY, pos + i);
1209 goto err_free;
1210 }
1211 pred->fn = filter_pred_none;
1212
1213 /*
1214 * Quotes are not required, but if they exist then we need
1215 * to read them till we hit a matching one.
1216 */
1217 if (str[i] == '\'' || str[i] == '"')
1218 q = str[i];
1219 else
1220 q = 0;
1221
1222 for (i++; str[i]; i++) {
1223 if (q && str[i] == q)
1224 break;
1225 if (!q && (str[i] == ')' || str[i] == '&' ||
1226 str[i] == '|'))
1227 break;
1228 }
1229 /* Skip quotes */
1230 if (q)
1231 s++;
1232 len = i - s;
1233 if (len >= MAX_FILTER_STR_VAL) {
1234 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1235 goto err_free;
1236 }
ec126cac 1237
80765597
SRV
1238 pred->regex.len = len;
1239 strncpy(pred->regex.pattern, str + s, len);
1240 pred->regex.pattern[len] = 0;
1241
1242 /* This is either a string, or an integer */
1243 } else if (str[i] == '\'' || str[i] == '"') {
1244 char q = str[i];
1245
1246 /* Make sure the op is OK for strings */
1247 switch (op) {
1248 case OP_NE:
1249 pred->not = 1;
1250 /* Fall through */
1251 case OP_GLOB:
1252 case OP_EQ:
1253 break;
1254 default:
1255 parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
1256 goto err_free;
1257 }
ec126cac 1258
80765597
SRV
1259 /* Make sure the field is OK for strings */
1260 if (!is_string_field(field)) {
1261 parse_error(pe, FILT_ERR_EXPECT_DIGIT, pos + i);
1262 goto err_free;
1263 }
43cd4145 1264
80765597
SRV
1265 for (i++; str[i]; i++) {
1266 if (str[i] == q)
1267 break;
1268 }
1269 if (!str[i]) {
1270 parse_error(pe, FILT_ERR_MISSING_QUOTE, pos + i);
1271 goto err_free;
1272 }
43cd4145 1273
80765597
SRV
1274 /* Skip quotes */
1275 s++;
1276 len = i - s;
1277 if (len >= MAX_FILTER_STR_VAL) {
1278 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1279 goto err_free;
1280 }
c00b060f 1281
80765597
SRV
1282 pred->regex.len = len;
1283 strncpy(pred->regex.pattern, str + s, len);
1284 pred->regex.pattern[len] = 0;
43cd4145 1285
80765597 1286 filter_build_regex(pred);
43cd4145 1287
80765597
SRV
1288 if (field->filter_type == FILTER_COMM) {
1289 pred->fn = filter_pred_comm;
96bc293a 1290
80765597
SRV
1291 } else if (field->filter_type == FILTER_STATIC_STRING) {
1292 pred->fn = filter_pred_string;
1293 pred->regex.field_len = field->size;
96bc293a 1294
80765597
SRV
1295 } else if (field->filter_type == FILTER_DYN_STRING)
1296 pred->fn = filter_pred_strloc;
1297 else
1298 pred->fn = filter_pred_pchar;
1299 /* go past the last quote */
1300 i++;
96bc293a 1301
80765597 1302 } else if (isdigit(str[i])) {
96bc293a 1303
80765597
SRV
1304 /* Make sure the field is not a string */
1305 if (is_string_field(field)) {
1306 parse_error(pe, FILT_ERR_EXPECT_STRING, pos + i);
1307 goto err_free;
1308 }
96bc293a 1309
80765597
SRV
1310 if (op == OP_GLOB) {
1311 parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
1312 goto err_free;
1313 }
43cd4145 1314
80765597
SRV
1315 /* We allow 0xDEADBEEF */
1316 while (isalnum(str[i]))
1317 i++;
43cd4145 1318
80765597
SRV
1319 len = i - s;
1320 /* 0xfeedfacedeadbeef is 18 chars max */
1321 if (len >= sizeof(num_buf)) {
1322 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1323 goto err_free;
1324 }
43cd4145 1325
80765597
SRV
1326 strncpy(num_buf, str + s, len);
1327 num_buf[len] = 0;
43cd4145 1328
80765597
SRV
1329 /* Make sure it is a value */
1330 if (field->is_signed)
1331 ret = kstrtoll(num_buf, 0, &val);
1332 else
1333 ret = kstrtoull(num_buf, 0, &val);
1334 if (ret) {
1335 parse_error(pe, FILT_ERR_ILLEGAL_INTVAL, pos + s);
1336 goto err_free;
1337 }
43cd4145 1338
80765597 1339 pred->val = val;
43cd4145 1340
80765597
SRV
1341 if (field->filter_type == FILTER_CPU)
1342 pred->fn = filter_pred_cpu;
1343 else {
1344 pred->fn = select_comparison_fn(pred->op, field->size,
1345 field->is_signed);
1346 if (pred->op == OP_NE)
1347 pred->not = 1;
1348 }
1b797fe5 1349
80765597
SRV
1350 } else {
1351 parse_error(pe, FILT_ERR_INVALID_VALUE, pos + i);
1352 goto err_free;
1353 }
1b797fe5 1354
80765597
SRV
1355 *pred_ptr = pred;
1356 return i;
1b797fe5 1357
80765597
SRV
1358err_free:
1359 kfree(pred);
1360 return -EINVAL;
1b797fe5
JO
1361}
1362
80765597
SRV
1363enum {
1364 TOO_MANY_CLOSE = -1,
1365 TOO_MANY_OPEN = -2,
1366 MISSING_QUOTE = -3,
1367};
1368
43cd4145 1369/*
80765597
SRV
1370 * Read the filter string once to calculate the number of predicates
1371 * as well as how deep the parentheses go.
1372 *
1373 * Returns:
1374 * 0 - everything is fine (err is undefined)
1375 * -1 - too many ')'
1376 * -2 - too many '('
1377 * -3 - No matching quote
43cd4145 1378 */
80765597
SRV
1379static int calc_stack(const char *str, int *parens, int *preds, int *err)
1380{
1381 bool is_pred = false;
1382 int nr_preds = 0;
1383 int open = 1; /* Count the expression as "(E)" */
1384 int last_quote = 0;
1385 int max_open = 1;
1386 int quote = 0;
1387 int i;
8b372562 1388
80765597 1389 *err = 0;
c9c53ca0 1390
80765597
SRV
1391 for (i = 0; str[i]; i++) {
1392 if (isspace(str[i]))
1393 continue;
1394 if (quote) {
1395 if (str[i] == quote)
1396 quote = 0;
8b372562
TZ
1397 continue;
1398 }
1399
80765597
SRV
1400 switch (str[i]) {
1401 case '\'':
1402 case '"':
1403 quote = str[i];
1404 last_quote = i;
1405 break;
1406 case '|':
1407 case '&':
1408 if (str[i+1] != str[i])
1409 break;
1410 is_pred = false;
1411 continue;
1412 case '(':
1413 is_pred = false;
1414 open++;
1415 if (open > max_open)
1416 max_open = open;
1417 continue;
1418 case ')':
1419 is_pred = false;
1420 if (open == 1) {
1421 *err = i;
1422 return TOO_MANY_CLOSE;
e12c09cf 1423 }
80765597 1424 open--;
e12c09cf
SRRH
1425 continue;
1426 }
80765597
SRV
1427 if (!is_pred) {
1428 nr_preds++;
1429 is_pred = true;
1f9963cb 1430 }
80765597 1431 }
1f9963cb 1432
80765597
SRV
1433 if (quote) {
1434 *err = last_quote;
1435 return MISSING_QUOTE;
1436 }
61aaef55 1437
80765597
SRV
1438 if (open != 1) {
1439 int level = open;
8b372562 1440
80765597
SRV
1441 /* find the bad open */
1442 for (i--; i; i--) {
1443 if (quote) {
1444 if (str[i] == quote)
1445 quote = 0;
1446 continue;
1447 }
1448 switch (str[i]) {
1449 case '(':
1450 if (level == open) {
1451 *err = i;
1452 return TOO_MANY_OPEN;
1453 }
1454 level--;
1455 break;
1456 case ')':
1457 level++;
1458 break;
1459 case '\'':
1460 case '"':
1461 quote = str[i];
1462 break;
1463 }
1464 }
1465 /* First character is the '(' with missing ')' */
1466 *err = 0;
1467 return TOO_MANY_OPEN;
8b372562 1468 }
7ce7e424 1469
80765597
SRV
1470 /* Set the size of the required stacks */
1471 *parens = max_open;
1472 *preds = nr_preds;
1473 return 0;
1474}
1475
1476static int process_preds(struct trace_event_call *call,
1477 const char *filter_string,
1478 struct event_filter *filter,
1479 struct filter_parse_error *pe)
1480{
1481 struct prog_entry *prog;
1482 int nr_parens;
1483 int nr_preds;
1484 int index;
1485 int ret;
1486
1487 ret = calc_stack(filter_string, &nr_parens, &nr_preds, &index);
1488 if (ret < 0) {
1489 switch (ret) {
1490 case MISSING_QUOTE:
1491 parse_error(pe, FILT_ERR_MISSING_QUOTE, index);
1492 break;
1493 case TOO_MANY_OPEN:
1494 parse_error(pe, FILT_ERR_TOO_MANY_OPEN, index);
1495 break;
1496 default:
1497 parse_error(pe, FILT_ERR_TOO_MANY_CLOSE, index);
61e9dea2 1498 }
80765597 1499 return ret;
61e9dea2
SR
1500 }
1501
ba16293d
RB
1502 if (!nr_preds)
1503 return -EINVAL;
1504
1505 prog = predicate_parse(filter_string, nr_parens, nr_preds,
80765597 1506 parse_pred, call, pe);
ba16293d
RB
1507 if (IS_ERR(prog))
1508 return PTR_ERR(prog);
1509
80765597
SRV
1510 rcu_assign_pointer(filter->prog, prog);
1511 return 0;
7ce7e424
TZ
1512}
1513
7f1d2f82 1514static inline void event_set_filtered_flag(struct trace_event_file *file)
f306cc82 1515{
0fc1b09f
SRRH
1516 unsigned long old_flags = file->flags;
1517
dcb0b557 1518 file->flags |= EVENT_FILE_FL_FILTERED;
0fc1b09f
SRRH
1519
1520 if (old_flags != file->flags)
1521 trace_buffered_event_enable();
f306cc82
TZ
1522}
1523
7f1d2f82 1524static inline void event_set_filter(struct trace_event_file *file,
f306cc82
TZ
1525 struct event_filter *filter)
1526{
dcb0b557 1527 rcu_assign_pointer(file->filter, filter);
f306cc82
TZ
1528}
1529
7f1d2f82 1530static inline void event_clear_filter(struct trace_event_file *file)
f306cc82 1531{
dcb0b557 1532 RCU_INIT_POINTER(file->filter, NULL);
f306cc82
TZ
1533}
1534
1535static inline void
7f1d2f82 1536event_set_no_set_filter_flag(struct trace_event_file *file)
f306cc82 1537{
dcb0b557 1538 file->flags |= EVENT_FILE_FL_NO_SET_FILTER;
f306cc82
TZ
1539}
1540
1541static inline void
7f1d2f82 1542event_clear_no_set_filter_flag(struct trace_event_file *file)
f306cc82 1543{
dcb0b557 1544 file->flags &= ~EVENT_FILE_FL_NO_SET_FILTER;
f306cc82
TZ
1545}
1546
1547static inline bool
7f1d2f82 1548event_no_set_filter_flag(struct trace_event_file *file)
f306cc82 1549{
5d6ad960 1550 if (file->flags & EVENT_FILE_FL_NO_SET_FILTER)
f306cc82
TZ
1551 return true;
1552
f306cc82
TZ
1553 return false;
1554}
1555
75b8e982
SR
1556struct filter_list {
1557 struct list_head list;
1558 struct event_filter *filter;
1559};
1560
80765597 1561static int process_system_preds(struct trace_subsystem_dir *dir,
f306cc82 1562 struct trace_array *tr,
80765597 1563 struct filter_parse_error *pe,
fce29d15
LZ
1564 char *filter_string)
1565{
7f1d2f82 1566 struct trace_event_file *file;
75b8e982 1567 struct filter_list *filter_item;
404a3add 1568 struct event_filter *filter = NULL;
75b8e982
SR
1569 struct filter_list *tmp;
1570 LIST_HEAD(filter_list);
fce29d15 1571 bool fail = true;
a66abe7f 1572 int err;
fce29d15 1573
f306cc82 1574 list_for_each_entry(file, &tr->events, list) {
0fc3ca9a 1575
bb9ef1cb 1576 if (file->system != dir)
0fc3ca9a
SR
1577 continue;
1578
404a3add
SRV
1579 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
1580 if (!filter)
75b8e982 1581 goto fail_mem;
0fc3ca9a 1582
567f6989
SRV
1583 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
1584 if (!filter->filter_string)
75b8e982 1585 goto fail_mem;
fce29d15 1586
80765597 1587 err = process_preds(file->event_call, filter_string, filter, pe);
75b8e982 1588 if (err) {
f306cc82 1589 filter_disable(file);
80765597
SRV
1590 parse_error(pe, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1591 append_filter_err(pe, filter);
75b8e982 1592 } else
f306cc82 1593 event_set_filtered_flag(file);
404a3add
SRV
1594
1595
1596 filter_item = kzalloc(sizeof(*filter_item), GFP_KERNEL);
1597 if (!filter_item)
1598 goto fail_mem;
1599
1600 list_add_tail(&filter_item->list, &filter_list);
75b8e982
SR
1601 /*
1602 * Regardless of if this returned an error, we still
1603 * replace the filter for the call.
1604 */
404a3add
SRV
1605 filter_item->filter = event_filter(file);
1606 event_set_filter(file, filter);
1607 filter = NULL;
75b8e982 1608
fce29d15
LZ
1609 fail = false;
1610 }
1611
0fc3ca9a
SR
1612 if (fail)
1613 goto fail;
1614
75b8e982
SR
1615 /*
1616 * The calls can still be using the old filters.
e0a568dc 1617 * Do a synchronize_sched() and to ensure all calls are
75b8e982
SR
1618 * done with them before we free them.
1619 */
e0a568dc 1620 tracepoint_synchronize_unregister();
75b8e982
SR
1621 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1622 __free_filter(filter_item->filter);
1623 list_del(&filter_item->list);
1624 kfree(filter_item);
1625 }
fce29d15 1626 return 0;
0fc3ca9a 1627 fail:
75b8e982
SR
1628 /* No call succeeded */
1629 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1630 list_del(&filter_item->list);
1631 kfree(filter_item);
1632 }
80765597 1633 parse_error(pe, FILT_ERR_BAD_SUBSYS_FILTER, 0);
0fc3ca9a 1634 return -EINVAL;
75b8e982 1635 fail_mem:
404a3add 1636 kfree(filter);
75b8e982
SR
1637 /* If any call succeeded, we still need to sync */
1638 if (!fail)
e0a568dc 1639 tracepoint_synchronize_unregister();
75b8e982
SR
1640 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1641 __free_filter(filter_item->filter);
1642 list_del(&filter_item->list);
1643 kfree(filter_item);
1644 }
1645 return -ENOMEM;
fce29d15
LZ
1646}
1647
567f6989 1648static int create_filter_start(char *filter_string, bool set_str,
80765597 1649 struct filter_parse_error **pse,
38b78eb8
TH
1650 struct event_filter **filterp)
1651{
1652 struct event_filter *filter;
80765597 1653 struct filter_parse_error *pe = NULL;
38b78eb8
TH
1654 int err = 0;
1655
80765597
SRV
1656 if (WARN_ON_ONCE(*pse || *filterp))
1657 return -EINVAL;
38b78eb8 1658
c7399708 1659 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
567f6989
SRV
1660 if (filter && set_str) {
1661 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
1662 if (!filter->filter_string)
1663 err = -ENOMEM;
1664 }
38b78eb8 1665
80765597 1666 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
38b78eb8 1667
80765597
SRV
1668 if (!filter || !pe || err) {
1669 kfree(pe);
38b78eb8
TH
1670 __free_filter(filter);
1671 return -ENOMEM;
1672 }
1673
1674 /* we're committed to creating a new filter */
1675 *filterp = filter;
80765597 1676 *pse = pe;
38b78eb8 1677
80765597 1678 return 0;
38b78eb8
TH
1679}
1680
80765597 1681static void create_filter_finish(struct filter_parse_error *pe)
38b78eb8 1682{
80765597 1683 kfree(pe);
38b78eb8
TH
1684}
1685
1686/**
2425bcb9
SRRH
1687 * create_filter - create a filter for a trace_event_call
1688 * @call: trace_event_call to create a filter for
38b78eb8
TH
1689 * @filter_str: filter string
1690 * @set_str: remember @filter_str and enable detailed error in filter
1691 * @filterp: out param for created filter (always updated on return)
f9065872 1692 * Must be a pointer that references a NULL pointer.
38b78eb8
TH
1693 *
1694 * Creates a filter for @call with @filter_str. If @set_str is %true,
1695 * @filter_str is copied and recorded in the new filter.
1696 *
1697 * On success, returns 0 and *@filterp points to the new filter. On
1698 * failure, returns -errno and *@filterp may point to %NULL or to a new
1699 * filter. In the latter case, the returned filter contains error
1700 * information if @set_str is %true and the caller is responsible for
1701 * freeing it.
1702 */
2425bcb9 1703static int create_filter(struct trace_event_call *call,
80765597 1704 char *filter_string, bool set_str,
38b78eb8
TH
1705 struct event_filter **filterp)
1706{
80765597 1707 struct filter_parse_error *pe = NULL;
38b78eb8
TH
1708 int err;
1709
f9065872
SRV
1710 /* filterp must point to NULL */
1711 if (WARN_ON(*filterp))
1712 *filterp = NULL;
1713
0b3dec05 1714 err = create_filter_start(filter_string, set_str, &pe, filterp);
80765597
SRV
1715 if (err)
1716 return err;
1717
0b3dec05 1718 err = process_preds(call, filter_string, *filterp, pe);
80765597 1719 if (err && set_str)
0b3dec05 1720 append_filter_err(pe, *filterp);
38b78eb8 1721
38b78eb8
TH
1722 return err;
1723}
1724
2425bcb9 1725int create_event_filter(struct trace_event_call *call,
bac5fb97
TZ
1726 char *filter_str, bool set_str,
1727 struct event_filter **filterp)
1728{
1729 return create_filter(call, filter_str, set_str, filterp);
1730}
1731
38b78eb8
TH
1732/**
1733 * create_system_filter - create a filter for an event_subsystem
1734 * @system: event_subsystem to create a filter for
1735 * @filter_str: filter string
1736 * @filterp: out param for created filter (always updated on return)
1737 *
1738 * Identical to create_filter() except that it creates a subsystem filter
1739 * and always remembers @filter_str.
1740 */
7967b3e0 1741static int create_system_filter(struct trace_subsystem_dir *dir,
f306cc82 1742 struct trace_array *tr,
38b78eb8
TH
1743 char *filter_str, struct event_filter **filterp)
1744{
80765597 1745 struct filter_parse_error *pe = NULL;
38b78eb8
TH
1746 int err;
1747
0b3dec05 1748 err = create_filter_start(filter_str, true, &pe, filterp);
38b78eb8 1749 if (!err) {
80765597 1750 err = process_system_preds(dir, tr, pe, filter_str);
38b78eb8
TH
1751 if (!err) {
1752 /* System filters just show a default message */
0b3dec05
SRV
1753 kfree((*filterp)->filter_string);
1754 (*filterp)->filter_string = NULL;
38b78eb8 1755 } else {
0b3dec05 1756 append_filter_err(pe, *filterp);
38b78eb8
TH
1757 }
1758 }
80765597 1759 create_filter_finish(pe);
38b78eb8 1760
38b78eb8
TH
1761 return err;
1762}
1763
e2912b09 1764/* caller must hold event_mutex */
7f1d2f82 1765int apply_event_filter(struct trace_event_file *file, char *filter_string)
8b372562 1766{
2425bcb9 1767 struct trace_event_call *call = file->event_call;
0b3dec05 1768 struct event_filter *filter = NULL;
e2912b09 1769 int err;
8b372562
TZ
1770
1771 if (!strcmp(strstrip(filter_string), "0")) {
f306cc82
TZ
1772 filter_disable(file);
1773 filter = event_filter(file);
1774
75b8e982 1775 if (!filter)
e2912b09 1776 return 0;
f306cc82
TZ
1777
1778 event_clear_filter(file);
1779
f76690af 1780 /* Make sure the filter is not being used */
e0a568dc 1781 tracepoint_synchronize_unregister();
75b8e982 1782 __free_filter(filter);
f306cc82 1783
e2912b09 1784 return 0;
8b372562
TZ
1785 }
1786
38b78eb8 1787 err = create_filter(call, filter_string, true, &filter);
8b372562 1788
75b8e982
SR
1789 /*
1790 * Always swap the call filter with the new filter
1791 * even if there was an error. If there was an error
1792 * in the filter, we disable the filter and show the error
1793 * string
1794 */
38b78eb8 1795 if (filter) {
f306cc82 1796 struct event_filter *tmp;
38b78eb8 1797
f306cc82 1798 tmp = event_filter(file);
38b78eb8 1799 if (!err)
f306cc82 1800 event_set_filtered_flag(file);
38b78eb8 1801 else
f306cc82 1802 filter_disable(file);
38b78eb8 1803
f306cc82 1804 event_set_filter(file, filter);
38b78eb8
TH
1805
1806 if (tmp) {
1807 /* Make sure the call is done with the filter */
e0a568dc 1808 tracepoint_synchronize_unregister();
38b78eb8
TH
1809 __free_filter(tmp);
1810 }
75b8e982 1811 }
8b372562
TZ
1812
1813 return err;
1814}
1815
7967b3e0 1816int apply_subsystem_event_filter(struct trace_subsystem_dir *dir,
8b372562
TZ
1817 char *filter_string)
1818{
ae63b31e 1819 struct event_subsystem *system = dir->subsystem;
f306cc82 1820 struct trace_array *tr = dir->tr;
0b3dec05 1821 struct event_filter *filter = NULL;
75b8e982 1822 int err = 0;
8b372562 1823
00e95830 1824 mutex_lock(&event_mutex);
8b372562 1825
e9dbfae5 1826 /* Make sure the system still has events */
ae63b31e 1827 if (!dir->nr_events) {
e9dbfae5
SR
1828 err = -ENODEV;
1829 goto out_unlock;
1830 }
1831
8b372562 1832 if (!strcmp(strstrip(filter_string), "0")) {
bb9ef1cb 1833 filter_free_subsystem_preds(dir, tr);
8b372562 1834 remove_filter_string(system->filter);
75b8e982
SR
1835 filter = system->filter;
1836 system->filter = NULL;
1837 /* Ensure all filters are no longer used */
e0a568dc 1838 tracepoint_synchronize_unregister();
bb9ef1cb 1839 filter_free_subsystem_filters(dir, tr);
75b8e982 1840 __free_filter(filter);
a66abe7f 1841 goto out_unlock;
8b372562
TZ
1842 }
1843
bb9ef1cb 1844 err = create_system_filter(dir, tr, filter_string, &filter);
38b78eb8
TH
1845 if (filter) {
1846 /*
1847 * No event actually uses the system filter
1848 * we can free it without synchronize_sched().
1849 */
1850 __free_filter(system->filter);
1851 system->filter = filter;
1852 }
8cd995b6 1853out_unlock:
00e95830 1854 mutex_unlock(&event_mutex);
8b372562
TZ
1855
1856 return err;
1857}
7ce7e424 1858
07b139c8 1859#ifdef CONFIG_PERF_EVENTS
6fb2915d
LZ
1860
1861void ftrace_profile_free_filter(struct perf_event *event)
1862{
1863 struct event_filter *filter = event->filter;
1864
1865 event->filter = NULL;
c9c53ca0 1866 __free_filter(filter);
6fb2915d
LZ
1867}
1868
5500fa51
JO
1869struct function_filter_data {
1870 struct ftrace_ops *ops;
1871 int first_filter;
1872 int first_notrace;
1873};
1874
1875#ifdef CONFIG_FUNCTION_TRACER
1876static char **
1877ftrace_function_filter_re(char *buf, int len, int *count)
1878{
1bb56471 1879 char *str, **re;
5500fa51
JO
1880
1881 str = kstrndup(buf, len, GFP_KERNEL);
1882 if (!str)
1883 return NULL;
1884
1885 /*
1886 * The argv_split function takes white space
1887 * as a separator, so convert ',' into spaces.
1888 */
1bb56471 1889 strreplace(str, ',', ' ');
5500fa51
JO
1890
1891 re = argv_split(GFP_KERNEL, str, count);
1892 kfree(str);
1893 return re;
1894}
1895
1896static int ftrace_function_set_regexp(struct ftrace_ops *ops, int filter,
1897 int reset, char *re, int len)
1898{
1899 int ret;
1900
1901 if (filter)
1902 ret = ftrace_set_filter(ops, re, len, reset);
1903 else
1904 ret = ftrace_set_notrace(ops, re, len, reset);
1905
1906 return ret;
1907}
1908
1909static int __ftrace_function_set_filter(int filter, char *buf, int len,
1910 struct function_filter_data *data)
1911{
92d8d4a8 1912 int i, re_cnt, ret = -EINVAL;
5500fa51
JO
1913 int *reset;
1914 char **re;
1915
1916 reset = filter ? &data->first_filter : &data->first_notrace;
1917
1918 /*
1919 * The 'ip' field could have multiple filters set, separated
1920 * either by space or comma. We first cut the filter and apply
1921 * all pieces separatelly.
1922 */
1923 re = ftrace_function_filter_re(buf, len, &re_cnt);
1924 if (!re)
1925 return -EINVAL;
1926
1927 for (i = 0; i < re_cnt; i++) {
1928 ret = ftrace_function_set_regexp(data->ops, filter, *reset,
1929 re[i], strlen(re[i]));
1930 if (ret)
1931 break;
1932
1933 if (*reset)
1934 *reset = 0;
1935 }
1936
1937 argv_free(re);
1938 return ret;
1939}
1940
80765597 1941static int ftrace_function_check_pred(struct filter_pred *pred)
5500fa51
JO
1942{
1943 struct ftrace_event_field *field = pred->field;
1944
80765597
SRV
1945 /*
1946 * Check the predicate for function trace, verify:
1947 * - only '==' and '!=' is used
1948 * - the 'ip' field is used
1949 */
1950 if ((pred->op != OP_EQ) && (pred->op != OP_NE))
1951 return -EINVAL;
5500fa51 1952
80765597
SRV
1953 if (strcmp(field->name, "ip"))
1954 return -EINVAL;
5500fa51
JO
1955
1956 return 0;
1957}
1958
80765597
SRV
1959static int ftrace_function_set_filter_pred(struct filter_pred *pred,
1960 struct function_filter_data *data)
5500fa51 1961{
80765597
SRV
1962 int ret;
1963
5500fa51 1964 /* Checking the node is valid for function trace. */
80765597
SRV
1965 ret = ftrace_function_check_pred(pred);
1966 if (ret)
1967 return ret;
1968
1969 return __ftrace_function_set_filter(pred->op == OP_EQ,
1970 pred->regex.pattern,
1971 pred->regex.len,
1972 data);
1973}
1974
1975static bool is_or(struct prog_entry *prog, int i)
1976{
1977 int target;
5500fa51 1978
80765597
SRV
1979 /*
1980 * Only "||" is allowed for function events, thus,
1981 * all true branches should jump to true, and any
1982 * false branch should jump to false.
1983 */
1984 target = prog[i].target + 1;
1985 /* True and false have NULL preds (all prog entries should jump to one */
1986 if (prog[target].pred)
1987 return false;
1988
1989 /* prog[target].target is 1 for TRUE, 0 for FALSE */
1990 return prog[i].when_to_branch == prog[target].target;
5500fa51
JO
1991}
1992
1993static int ftrace_function_set_filter(struct perf_event *event,
1994 struct event_filter *filter)
1995{
1f3b0faa
SRV
1996 struct prog_entry *prog = rcu_dereference_protected(filter->prog,
1997 lockdep_is_held(&event_mutex));
5500fa51
JO
1998 struct function_filter_data data = {
1999 .first_filter = 1,
2000 .first_notrace = 1,
2001 .ops = &event->ftrace_ops,
2002 };
80765597
SRV
2003 int i;
2004
2005 for (i = 0; prog[i].pred; i++) {
2006 struct filter_pred *pred = prog[i].pred;
2007
2008 if (!is_or(prog, i))
2009 return -EINVAL;
5500fa51 2010
80765597
SRV
2011 if (ftrace_function_set_filter_pred(pred, &data) < 0)
2012 return -EINVAL;
2013 }
2014 return 0;
5500fa51
JO
2015}
2016#else
2017static int ftrace_function_set_filter(struct perf_event *event,
2018 struct event_filter *filter)
2019{
2020 return -ENODEV;
2021}
2022#endif /* CONFIG_FUNCTION_TRACER */
2023
6fb2915d
LZ
2024int ftrace_profile_set_filter(struct perf_event *event, int event_id,
2025 char *filter_str)
2026{
2027 int err;
0b3dec05 2028 struct event_filter *filter = NULL;
2425bcb9 2029 struct trace_event_call *call;
6fb2915d
LZ
2030
2031 mutex_lock(&event_mutex);
2032
3f78f935 2033 call = event->tp_event;
a66abe7f
IM
2034
2035 err = -EINVAL;
3f78f935 2036 if (!call)
a66abe7f 2037 goto out_unlock;
6fb2915d 2038
a66abe7f 2039 err = -EEXIST;
6fb2915d 2040 if (event->filter)
a66abe7f 2041 goto out_unlock;
6fb2915d 2042
38b78eb8 2043 err = create_filter(call, filter_str, false, &filter);
5500fa51
JO
2044 if (err)
2045 goto free_filter;
2046
2047 if (ftrace_event_is_function(call))
2048 err = ftrace_function_set_filter(event, filter);
38b78eb8 2049 else
5500fa51
JO
2050 event->filter = filter;
2051
2052free_filter:
2053 if (err || ftrace_event_is_function(call))
c9c53ca0 2054 __free_filter(filter);
6fb2915d 2055
a66abe7f 2056out_unlock:
6fb2915d
LZ
2057 mutex_unlock(&event_mutex);
2058
2059 return err;
2060}
2061
07b139c8 2062#endif /* CONFIG_PERF_EVENTS */
6fb2915d 2063
1d0e78e3
JO
2064#ifdef CONFIG_FTRACE_STARTUP_TEST
2065
2066#include <linux/types.h>
2067#include <linux/tracepoint.h>
2068
2069#define CREATE_TRACE_POINTS
2070#include "trace_events_filter_test.h"
2071
1d0e78e3
JO
2072#define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \
2073{ \
2074 .filter = FILTER, \
2075 .rec = { .a = va, .b = vb, .c = vc, .d = vd, \
2076 .e = ve, .f = vf, .g = vg, .h = vh }, \
2077 .match = m, \
2078 .not_visited = nvisit, \
2079}
2080#define YES 1
2081#define NO 0
2082
2083static struct test_filter_data_t {
2084 char *filter;
a7237765 2085 struct trace_event_raw_ftrace_test_filter rec;
1d0e78e3
JO
2086 int match;
2087 char *not_visited;
2088} test_filter_data[] = {
2089#define FILTER "a == 1 && b == 1 && c == 1 && d == 1 && " \
2090 "e == 1 && f == 1 && g == 1 && h == 1"
2091 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, ""),
2092 DATA_REC(NO, 0, 1, 1, 1, 1, 1, 1, 1, "bcdefgh"),
2093 DATA_REC(NO, 1, 1, 1, 1, 1, 1, 1, 0, ""),
2094#undef FILTER
2095#define FILTER "a == 1 || b == 1 || c == 1 || d == 1 || " \
2096 "e == 1 || f == 1 || g == 1 || h == 1"
2097 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2098 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2099 DATA_REC(YES, 1, 0, 0, 0, 0, 0, 0, 0, "bcdefgh"),
2100#undef FILTER
2101#define FILTER "(a == 1 || b == 1) && (c == 1 || d == 1) && " \
2102 "(e == 1 || f == 1) && (g == 1 || h == 1)"
2103 DATA_REC(NO, 0, 0, 1, 1, 1, 1, 1, 1, "dfh"),
2104 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2105 DATA_REC(YES, 1, 0, 1, 0, 0, 1, 0, 1, "bd"),
2106 DATA_REC(NO, 1, 0, 1, 0, 0, 1, 0, 0, "bd"),
2107#undef FILTER
2108#define FILTER "(a == 1 && b == 1) || (c == 1 && d == 1) || " \
2109 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2110 DATA_REC(YES, 1, 0, 1, 1, 1, 1, 1, 1, "efgh"),
2111 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 1, 1, ""),
2112 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2113#undef FILTER
2114#define FILTER "(a == 1 && b == 1) && (c == 1 && d == 1) && " \
2115 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2116 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 0, "gh"),
2117 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2118 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, ""),
2119#undef FILTER
2120#define FILTER "((a == 1 || b == 1) || (c == 1 || d == 1) || " \
2121 "(e == 1 || f == 1)) && (g == 1 || h == 1)"
2122 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 1, "bcdef"),
2123 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2124 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, "h"),
2125#undef FILTER
2126#define FILTER "((((((((a == 1) && (b == 1)) || (c == 1)) && (d == 1)) || " \
2127 "(e == 1)) && (f == 1)) || (g == 1)) && (h == 1))"
2128 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "ceg"),
2129 DATA_REC(NO, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2130 DATA_REC(NO, 1, 0, 1, 0, 1, 0, 1, 0, ""),
2131#undef FILTER
2132#define FILTER "((((((((a == 1) || (b == 1)) && (c == 1)) || (d == 1)) && " \
2133 "(e == 1)) || (f == 1)) && (g == 1)) || (h == 1))"
2134 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "bdfh"),
2135 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2136 DATA_REC(YES, 1, 0, 1, 0, 1, 0, 1, 0, "bdfh"),
2137};
2138
2139#undef DATA_REC
2140#undef FILTER
2141#undef YES
2142#undef NO
2143
0a4d0564 2144#define DATA_CNT ARRAY_SIZE(test_filter_data)
1d0e78e3
JO
2145
2146static int test_pred_visited;
2147
2148static int test_pred_visited_fn(struct filter_pred *pred, void *event)
2149{
2150 struct ftrace_event_field *field = pred->field;
2151
2152 test_pred_visited = 1;
2153 printk(KERN_INFO "\npred visited %s\n", field->name);
2154 return 1;
2155}
2156
80765597 2157static void update_pred_fn(struct event_filter *filter, char *fields)
1d0e78e3 2158{
8ec8405f
SRV
2159 struct prog_entry *prog = rcu_dereference_protected(filter->prog,
2160 lockdep_is_held(&event_mutex));
80765597 2161 int i;
1d0e78e3 2162
80765597
SRV
2163 for (i = 0; prog[i].pred; i++) {
2164 struct filter_pred *pred = prog[i].pred;
1d0e78e3
JO
2165 struct ftrace_event_field *field = pred->field;
2166
80765597
SRV
2167 WARN_ON_ONCE(!pred->fn);
2168
1d0e78e3 2169 if (!field) {
80765597
SRV
2170 WARN_ONCE(1, "all leafs should have field defined %d", i);
2171 continue;
1d0e78e3 2172 }
80765597 2173
1d0e78e3 2174 if (!strchr(fields, *field->name))
80765597 2175 continue;
1d0e78e3 2176
1d0e78e3
JO
2177 pred->fn = test_pred_visited_fn;
2178 }
1d0e78e3
JO
2179}
2180
2181static __init int ftrace_test_event_filter(void)
2182{
2183 int i;
2184
2185 printk(KERN_INFO "Testing ftrace filter: ");
2186
2187 for (i = 0; i < DATA_CNT; i++) {
2188 struct event_filter *filter = NULL;
2189 struct test_filter_data_t *d = &test_filter_data[i];
2190 int err;
2191
38b78eb8
TH
2192 err = create_filter(&event_ftrace_test_filter, d->filter,
2193 false, &filter);
1d0e78e3
JO
2194 if (err) {
2195 printk(KERN_INFO
2196 "Failed to get filter for '%s', err %d\n",
2197 d->filter, err);
38b78eb8 2198 __free_filter(filter);
1d0e78e3
JO
2199 break;
2200 }
2201
8ec8405f
SRV
2202 /* Needed to dereference filter->prog */
2203 mutex_lock(&event_mutex);
86b6ef21
SR
2204 /*
2205 * The preemption disabling is not really needed for self
2206 * tests, but the rcu dereference will complain without it.
2207 */
2208 preempt_disable();
1d0e78e3 2209 if (*d->not_visited)
80765597 2210 update_pred_fn(filter, d->not_visited);
1d0e78e3
JO
2211
2212 test_pred_visited = 0;
2213 err = filter_match_preds(filter, &d->rec);
86b6ef21 2214 preempt_enable();
1d0e78e3 2215
8ec8405f
SRV
2216 mutex_unlock(&event_mutex);
2217
1d0e78e3
JO
2218 __free_filter(filter);
2219
2220 if (test_pred_visited) {
2221 printk(KERN_INFO
2222 "Failed, unwanted pred visited for filter %s\n",
2223 d->filter);
2224 break;
2225 }
2226
2227 if (err != d->match) {
2228 printk(KERN_INFO
2229 "Failed to match filter '%s', expected %d\n",
2230 d->filter, d->match);
2231 break;
2232 }
2233 }
2234
2235 if (i == DATA_CNT)
2236 printk(KERN_CONT "OK\n");
2237
2238 return 0;
2239}
2240
2241late_initcall(ftrace_test_event_filter);
2242
2243#endif /* CONFIG_FTRACE_STARTUP_TEST */