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