1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * trace_events_filter - generic event filtering
4 *
5 * Copyright (C) 2009 Tom Zanussi <tzanussi@gmail.com>
6 */
7
8 #include <linux/uaccess.h>
9 #include <linux/module.h>
10 #include <linux/ctype.h>
11 #include <linux/mutex.h>
12 #include <linux/perf_event.h>
13 #include <linux/slab.h>
14
15 #include "trace.h"
16 #include "trace_output.h"
17
18 #define DEFAULT_SYS_FILTER_MESSAGE \
19 "### global filter ###\n" \
20 "# Use this to set filters for multiple events.\n" \
21 "# Only events with the given fields will be affected.\n" \
22 "# If no events are modified, an error message will be displayed here"
23
24 /* Due to token parsing '<=' must be before '<' and '>=' must be before '>' */
25 #define OPS \
26 C( OP_GLOB, "~" ), \
27 C( OP_NE, "!=" ), \
28 C( OP_EQ, "==" ), \
29 C( OP_LE, "<=" ), \
30 C( OP_LT, "<" ), \
31 C( OP_GE, ">=" ), \
32 C( OP_GT, ">" ), \
33 C( OP_BAND, "&" ), \
34 C( OP_MAX, NULL )
35
36 #undef C
37 #define C(a, b) a
38
39 enum filter_op_ids { OPS };
40
41 #undef C
42 #define C(a, b) b
43
44 static const char * ops[] = { OPS };
45
46 enum filter_pred_fn {
47 FILTER_PRED_FN_NOP,
48 FILTER_PRED_FN_64,
49 FILTER_PRED_FN_64_CPUMASK,
50 FILTER_PRED_FN_S64,
51 FILTER_PRED_FN_U64,
52 FILTER_PRED_FN_32,
53 FILTER_PRED_FN_32_CPUMASK,
54 FILTER_PRED_FN_S32,
55 FILTER_PRED_FN_U32,
56 FILTER_PRED_FN_16,
57 FILTER_PRED_FN_16_CPUMASK,
58 FILTER_PRED_FN_S16,
59 FILTER_PRED_FN_U16,
60 FILTER_PRED_FN_8,
61 FILTER_PRED_FN_8_CPUMASK,
62 FILTER_PRED_FN_S8,
63 FILTER_PRED_FN_U8,
64 FILTER_PRED_FN_COMM,
65 FILTER_PRED_FN_STRING,
66 FILTER_PRED_FN_STRLOC,
67 FILTER_PRED_FN_STRRELLOC,
68 FILTER_PRED_FN_PCHAR_USER,
69 FILTER_PRED_FN_PCHAR,
70 FILTER_PRED_FN_CPU,
71 FILTER_PRED_FN_CPU_CPUMASK,
72 FILTER_PRED_FN_CPUMASK,
73 FILTER_PRED_FN_CPUMASK_CPU,
74 FILTER_PRED_FN_FUNCTION,
75 FILTER_PRED_FN_,
76 FILTER_PRED_TEST_VISITED,
77 };
78
79 struct filter_pred {
80 struct regex *regex;
81 struct cpumask *mask;
82 unsigned short *ops;
83 struct ftrace_event_field *field;
84 u64 val;
85 u64 val2;
86 enum filter_pred_fn fn_num;
87 int offset;
88 int not;
89 int op;
90 };
91
92 /*
93 * pred functions are OP_LE, OP_LT, OP_GE, OP_GT, and OP_BAND
94 * pred_funcs_##type below must match the order of them above.
95 */
96 #define PRED_FUNC_START OP_LE
97 #define PRED_FUNC_MAX (OP_BAND - PRED_FUNC_START)
98
99 #define ERRORS \
100 C(NONE, "No error"), \
101 C(INVALID_OP, "Invalid operator"), \
102 C(TOO_MANY_OPEN, "Too many '('"), \
103 C(TOO_MANY_CLOSE, "Too few '('"), \
104 C(MISSING_QUOTE, "Missing matching quote"), \
105 C(MISSING_BRACE_OPEN, "Missing '{'"), \
106 C(MISSING_BRACE_CLOSE, "Missing '}'"), \
107 C(OPERAND_TOO_LONG, "Operand too long"), \
108 C(EXPECT_STRING, "Expecting string field"), \
109 C(EXPECT_DIGIT, "Expecting numeric field"), \
110 C(ILLEGAL_FIELD_OP, "Illegal operation for field type"), \
111 C(FIELD_NOT_FOUND, "Field not found"), \
112 C(ILLEGAL_INTVAL, "Illegal integer value"), \
113 C(BAD_SUBSYS_FILTER, "Couldn't find or set field in one of a subsystem's events"), \
114 C(TOO_MANY_PREDS, "Too many terms in predicate expression"), \
115 C(INVALID_FILTER, "Meaningless filter expression"), \
116 C(INVALID_CPULIST, "Invalid cpulist"), \
117 C(IP_FIELD_ONLY, "Only 'ip' field is supported for function trace"), \
118 C(INVALID_VALUE, "Invalid value (did you forget quotes)?"), \
119 C(NO_FUNCTION, "Function not found"), \
120 C(ERRNO, "Error"), \
121 C(NO_FILTER, "No filter found")
122
123 #undef C
124 #define C(a, b) FILT_ERR_##a
125
126 enum { ERRORS };
127
128 #undef C
129 #define C(a, b) b
130
131 static const char *err_text[] = { ERRORS };
132
133 /* Called after a '!' character but "!=" and "!~" are not "not"s */
is_not(const char * str)134 static bool is_not(const char *str)
135 {
136 switch (str[1]) {
137 case '=':
138 case '~':
139 return false;
140 }
141 return true;
142 }
143
144 /**
145 * struct prog_entry - a single entry in the filter program
146 * @target: Index to jump to on a branch (actually one minus the index)
147 * @when_to_branch: The value of the result of the predicate to do a branch
148 * @pred: The predicate to execute.
149 */
150 struct prog_entry {
151 int target;
152 int when_to_branch;
153 struct filter_pred *pred;
154 };
155
156 /**
157 * update_preds - assign a program entry a label target
158 * @prog: The program array
159 * @N: The index of the current entry in @prog
160 * @invert: What to assign a program entry for its branch condition
161 *
162 * The program entry at @N has a target that points to the index of a program
163 * entry that can have its target and when_to_branch fields updated.
164 * Update the current program entry denoted by index @N target field to be
165 * that of the updated entry. This will denote the entry to update if
166 * we are processing an "||" after an "&&".
167 */
update_preds(struct prog_entry * prog,int N,int invert)168 static void update_preds(struct prog_entry *prog, int N, int invert)
169 {
170 int t, s;
171
172 t = prog[N].target;
173 s = prog[t].target;
174 prog[t].when_to_branch = invert;
175 prog[t].target = N;
176 prog[N].target = s;
177 }
178
179 struct filter_parse_error {
180 int lasterr;
181 int lasterr_pos;
182 };
183
parse_error(struct filter_parse_error * pe,int err,int pos)184 static void parse_error(struct filter_parse_error *pe, int err, int pos)
185 {
186 pe->lasterr = err;
187 pe->lasterr_pos = pos;
188 }
189
190 typedef int (*parse_pred_fn)(const char *str, void *data, int pos,
191 struct filter_parse_error *pe,
192 struct filter_pred **pred);
193
194 enum {
195 INVERT = 1,
196 PROCESS_AND = 2,
197 PROCESS_OR = 4,
198 };
199
free_predicate(struct filter_pred * pred)200 static void free_predicate(struct filter_pred *pred)
201 {
202 if (pred) {
203 kfree(pred->regex);
204 kfree(pred->mask);
205 kfree(pred);
206 }
207 }
208
209 /*
210 * Without going into a formal proof, this explains the method that is used in
211 * parsing the logical expressions.
212 *
213 * For example, if we have: "a && !(!b || (c && g)) || d || e && !f"
214 * The first pass will convert it into the following program:
215 *
216 * n1: r=a; l1: if (!r) goto l4;
217 * n2: r=b; l2: if (!r) goto l4;
218 * n3: r=c; r=!r; l3: if (r) goto l4;
219 * n4: r=g; r=!r; l4: if (r) goto l5;
220 * n5: r=d; l5: if (r) goto T
221 * n6: r=e; l6: if (!r) goto l7;
222 * n7: r=f; r=!r; l7: if (!r) goto F
223 * T: return TRUE
224 * F: return FALSE
225 *
226 * To do this, we use a data structure to represent each of the above
227 * predicate and conditions that has:
228 *
229 * predicate, when_to_branch, invert, target
230 *
231 * The "predicate" will hold the function to determine the result "r".
232 * The "when_to_branch" denotes what "r" should be if a branch is to be taken
233 * "&&" would contain "!r" or (0) and "||" would contain "r" or (1).
234 * The "invert" holds whether the value should be reversed before testing.
235 * The "target" contains the label "l#" to jump to.
236 *
237 * A stack is created to hold values when parentheses are used.
238 *
239 * To simplify the logic, the labels will start at 0 and not 1.
240 *
241 * The possible invert values are 1 and 0. The number of "!"s that are in scope
242 * before the predicate determines the invert value, if the number is odd then
243 * the invert value is 1 and 0 otherwise. This means the invert value only
244 * needs to be toggled when a new "!" is introduced compared to what is stored
245 * on the stack, where parentheses were used.
246 *
247 * The top of the stack and "invert" are initialized to zero.
248 *
249 * ** FIRST PASS **
250 *
251 * #1 A loop through all the tokens is done:
252 *
253 * #2 If the token is an "(", the stack is push, and the current stack value
254 * gets the current invert value, and the loop continues to the next token.
255 * The top of the stack saves the "invert" value to keep track of what
256 * the current inversion is. As "!(a && !b || c)" would require all
257 * predicates being affected separately by the "!" before the parentheses.
258 * And that would end up being equivalent to "(!a || b) && !c"
259 *
260 * #3 If the token is an "!", the current "invert" value gets inverted, and
261 * the loop continues. Note, if the next token is a predicate, then
262 * this "invert" value is only valid for the current program entry,
263 * and does not affect other predicates later on.
264 *
265 * The only other acceptable token is the predicate string.
266 *
267 * #4 A new entry into the program is added saving: the predicate and the
268 * current value of "invert". The target is currently assigned to the
269 * previous program index (this will not be its final value).
270 *
271 * #5 We now enter another loop and look at the next token. The only valid
272 * tokens are ")", "&&", "||" or end of the input string "\0".
273 *
274 * #6 The invert variable is reset to the current value saved on the top of
275 * the stack.
276 *
277 * #7 The top of the stack holds not only the current invert value, but also
278 * if a "&&" or "||" needs to be processed. Note, the "&&" takes higher
279 * precedence than "||". That is "a && b || c && d" is equivalent to
280 * "(a && b) || (c && d)". Thus the first thing to do is to see if "&&" needs
281 * to be processed. This is the case if an "&&" was the last token. If it was
282 * then we call update_preds(). This takes the program, the current index in
283 * the program, and the current value of "invert". More will be described
284 * below about this function.
285 *
286 * #8 If the next token is "&&" then we set a flag in the top of the stack
287 * that denotes that "&&" needs to be processed, break out of this loop
288 * and continue with the outer loop.
289 *
290 * #9 Otherwise, if a "||" needs to be processed then update_preds() is called.
291 * This is called with the program, the current index in the program, but
292 * this time with an inverted value of "invert" (that is !invert). This is
293 * because the value taken will become the "when_to_branch" value of the
294 * program.
295 * Note, this is called when the next token is not an "&&". As stated before,
296 * "&&" takes higher precedence, and "||" should not be processed yet if the
297 * next logical operation is "&&".
298 *
299 * #10 If the next token is "||" then we set a flag in the top of the stack
300 * that denotes that "||" needs to be processed, break out of this loop
301 * and continue with the outer loop.
302 *
303 * #11 If this is the end of the input string "\0" then we break out of both
304 * loops.
305 *
306 * #12 Otherwise, the next token is ")", where we pop the stack and continue
307 * this inner loop.
308 *
309 * Now to discuss the update_pred() function, as that is key to the setting up
310 * of the program. Remember the "target" of the program is initialized to the
311 * previous index and not the "l" label. The target holds the index into the
312 * program that gets affected by the operand. Thus if we have something like
313 * "a || b && c", when we process "a" the target will be "-1" (undefined).
314 * When we process "b", its target is "0", which is the index of "a", as that's
315 * the predicate that is affected by "||". But because the next token after "b"
316 * is "&&" we don't call update_preds(). Instead continue to "c". As the
317 * next token after "c" is not "&&" but the end of input, we first process the
318 * "&&" by calling update_preds() for the "&&" then we process the "||" by
319 * calling updates_preds() with the values for processing "||".
320 *
321 * What does that mean? What update_preds() does is to first save the "target"
322 * of the program entry indexed by the current program entry's "target"
323 * (remember the "target" is initialized to previous program entry), and then
324 * sets that "target" to the current index which represents the label "l#".
325 * That entry's "when_to_branch" is set to the value passed in (the "invert"
326 * or "!invert"). Then it sets the current program entry's target to the saved
327 * "target" value (the old value of the program that had its "target" updated
328 * to the label).
329 *
330 * Looking back at "a || b && c", we have the following steps:
331 * "a" - prog[0] = { "a", X, -1 } // pred, when_to_branch, target
332 * "||" - flag that we need to process "||"; continue outer loop
333 * "b" - prog[1] = { "b", X, 0 }
334 * "&&" - flag that we need to process "&&"; continue outer loop
335 * (Notice we did not process "||")
336 * "c" - prog[2] = { "c", X, 1 }
337 * update_preds(prog, 2, 0); // invert = 0 as we are processing "&&"
338 * t = prog[2].target; // t = 1
339 * s = prog[t].target; // s = 0
340 * prog[t].target = 2; // Set target to "l2"
341 * prog[t].when_to_branch = 0;
342 * prog[2].target = s;
343 * update_preds(prog, 2, 1); // invert = 1 as we are now processing "||"
344 * t = prog[2].target; // t = 0
345 * s = prog[t].target; // s = -1
346 * prog[t].target = 2; // Set target to "l2"
347 * prog[t].when_to_branch = 1;
348 * prog[2].target = s;
349 *
350 * #13 Which brings us to the final step of the first pass, which is to set
351 * the last program entry's when_to_branch and target, which will be
352 * when_to_branch = 0; target = N; ( the label after the program entry after
353 * the last program entry processed above).
354 *
355 * If we denote "TRUE" to be the entry after the last program entry processed,
356 * and "FALSE" the program entry after that, we are now done with the first
357 * pass.
358 *
359 * Making the above "a || b && c" have a program of:
360 * prog[0] = { "a", 1, 2 }
361 * prog[1] = { "b", 0, 2 }
362 * prog[2] = { "c", 0, 3 }
363 *
364 * Which translates into:
365 * n0: r = a; l0: if (r) goto l2;
366 * n1: r = b; l1: if (!r) goto l2;
367 * n2: r = c; l2: if (!r) goto l3; // Which is the same as "goto F;"
368 * T: return TRUE; l3:
369 * F: return FALSE
370 *
371 * Although, after the first pass, the program is correct, it is
372 * inefficient. The simple sample of "a || b && c" could be easily been
373 * converted into:
374 * n0: r = a; if (r) goto T
375 * n1: r = b; if (!r) goto F
376 * n2: r = c; if (!r) goto F
377 * T: return TRUE;
378 * F: return FALSE;
379 *
380 * The First Pass is over the input string. The next too passes are over
381 * the program itself.
382 *
383 * ** SECOND PASS **
384 *
385 * Which brings us to the second pass. If a jump to a label has the
386 * same condition as that label, it can instead jump to its target.
387 * The original example of "a && !(!b || (c && g)) || d || e && !f"
388 * where the first pass gives us:
389 *
390 * n1: r=a; l1: if (!r) goto l4;
391 * n2: r=b; l2: if (!r) goto l4;
392 * n3: r=c; r=!r; l3: if (r) goto l4;
393 * n4: r=g; r=!r; l4: if (r) goto l5;
394 * n5: r=d; l5: if (r) goto T
395 * n6: r=e; l6: if (!r) goto l7;
396 * n7: r=f; r=!r; l7: if (!r) goto F:
397 * T: return TRUE;
398 * F: return FALSE
399 *
400 * We can see that "l3: if (r) goto l4;" and at l4, we have "if (r) goto l5;".
401 * And "l5: if (r) goto T", we could optimize this by converting l3 and l4
402 * to go directly to T. To accomplish this, we start from the last
403 * entry in the program and work our way back. If the target of the entry
404 * has the same "when_to_branch" then we could use that entry's target.
405 * Doing this, the above would end up as:
406 *
407 * n1: r=a; l1: if (!r) goto l4;
408 * n2: r=b; l2: if (!r) goto l4;
409 * n3: r=c; r=!r; l3: if (r) goto T;
410 * n4: r=g; r=!r; l4: if (r) goto T;
411 * n5: r=d; l5: if (r) goto T;
412 * n6: r=e; l6: if (!r) goto F;
413 * n7: r=f; r=!r; l7: if (!r) goto F;
414 * T: return TRUE
415 * F: return FALSE
416 *
417 * In that same pass, if the "when_to_branch" doesn't match, we can simply
418 * go to the program entry after the label. That is, "l2: if (!r) goto l4;"
419 * where "l4: if (r) goto T;", then we can convert l2 to be:
420 * "l2: if (!r) goto n5;".
421 *
422 * This will have the second pass give us:
423 * n1: r=a; l1: if (!r) goto n5;
424 * n2: r=b; l2: if (!r) goto n5;
425 * n3: r=c; r=!r; l3: if (r) goto T;
426 * n4: r=g; r=!r; l4: if (r) goto T;
427 * n5: r=d; l5: if (r) goto T
428 * n6: r=e; l6: if (!r) goto F;
429 * n7: r=f; r=!r; l7: if (!r) goto F
430 * T: return TRUE
431 * F: return FALSE
432 *
433 * Notice, all the "l#" labels are no longer used, and they can now
434 * be discarded.
435 *
436 * ** THIRD PASS **
437 *
438 * For the third pass we deal with the inverts. As they simply just
439 * make the "when_to_branch" get inverted, a simple loop over the
440 * program to that does: "when_to_branch ^= invert;" will do the
441 * job, leaving us with:
442 * n1: r=a; if (!r) goto n5;
443 * n2: r=b; if (!r) goto n5;
444 * n3: r=c: if (!r) goto T;
445 * n4: r=g; if (!r) goto T;
446 * n5: r=d; if (r) goto T
447 * n6: r=e; if (!r) goto F;
448 * n7: r=f; if (r) goto F
449 * T: return TRUE
450 * F: return FALSE
451 *
452 * As "r = a; if (!r) goto n5;" is obviously the same as
453 * "if (!a) goto n5;" without doing anything we can interpret the
454 * program as:
455 * n1: if (!a) goto n5;
456 * n2: if (!b) goto n5;
457 * n3: if (!c) goto T;
458 * n4: if (!g) goto T;
459 * n5: if (d) goto T
460 * n6: if (!e) goto F;
461 * n7: if (f) goto F
462 * T: return TRUE
463 * F: return FALSE
464 *
465 * Since the inverts are discarded at the end, there's no reason to store
466 * them in the program array (and waste memory). A separate array to hold
467 * the inverts is used and freed at the end.
468 */
469 static struct prog_entry *
predicate_parse(const char * str,int nr_parens,int nr_preds,parse_pred_fn parse_pred,void * data,struct filter_parse_error * pe)470 predicate_parse(const char *str, int nr_parens, int nr_preds,
471 parse_pred_fn parse_pred, void *data,
472 struct filter_parse_error *pe)
473 {
474 struct prog_entry *prog_stack;
475 struct prog_entry *prog;
476 const char *ptr = str;
477 char *inverts = NULL;
478 int *op_stack;
479 int *top;
480 int invert = 0;
481 int ret = -ENOMEM;
482 int len;
483 int N = 0;
484 int i;
485
486 nr_preds += 2; /* For TRUE and FALSE */
487
488 op_stack = kmalloc_objs(*op_stack, nr_parens);
489 if (!op_stack)
490 return ERR_PTR(-ENOMEM);
491 prog_stack = kzalloc_objs(*prog_stack, nr_preds);
492 if (!prog_stack) {
493 parse_error(pe, -ENOMEM, 0);
494 goto out_free;
495 }
496 inverts = kmalloc_array(nr_preds, sizeof(*inverts), GFP_KERNEL);
497 if (!inverts) {
498 parse_error(pe, -ENOMEM, 0);
499 goto out_free;
500 }
501
502 top = op_stack;
503 prog = prog_stack;
504 *top = 0;
505
506 /* First pass */
507 while (*ptr) { /* #1 */
508 const char *next = ptr++;
509
510 if (isspace(*next))
511 continue;
512
513 switch (*next) {
514 case '(': /* #2 */
515 if (top - op_stack > nr_parens) {
516 ret = -EINVAL;
517 goto out_free;
518 }
519 *(++top) = invert;
520 continue;
521 case '!': /* #3 */
522 if (!is_not(next))
523 break;
524 invert = !invert;
525 continue;
526 }
527
528 if (N >= nr_preds) {
529 parse_error(pe, FILT_ERR_TOO_MANY_PREDS, next - str);
530 goto out_free;
531 }
532
533 inverts[N] = invert; /* #4 */
534 prog[N].target = N-1;
535
536 len = parse_pred(next, data, ptr - str, pe, &prog[N].pred);
537 if (len < 0) {
538 ret = len;
539 goto out_free;
540 }
541 ptr = next + len;
542
543 N++;
544
545 ret = -1;
546 while (1) { /* #5 */
547 next = ptr++;
548 if (isspace(*next))
549 continue;
550
551 switch (*next) {
552 case ')':
553 case '\0':
554 break;
555 case '&':
556 case '|':
557 /* accepting only "&&" or "||" */
558 if (next[1] == next[0]) {
559 ptr++;
560 break;
561 }
562 fallthrough;
563 default:
564 parse_error(pe, FILT_ERR_TOO_MANY_PREDS,
565 next - str);
566 goto out_free;
567 }
568
569 invert = *top & INVERT;
570
571 if (*top & PROCESS_AND) { /* #7 */
572 update_preds(prog, N - 1, invert);
573 *top &= ~PROCESS_AND;
574 }
575 if (*next == '&') { /* #8 */
576 *top |= PROCESS_AND;
577 break;
578 }
579 if (*top & PROCESS_OR) { /* #9 */
580 update_preds(prog, N - 1, !invert);
581 *top &= ~PROCESS_OR;
582 }
583 if (*next == '|') { /* #10 */
584 *top |= PROCESS_OR;
585 break;
586 }
587 if (!*next) /* #11 */
588 goto out;
589
590 if (top == op_stack) {
591 ret = -1;
592 /* Too few '(' */
593 parse_error(pe, FILT_ERR_TOO_MANY_CLOSE, ptr - str);
594 goto out_free;
595 }
596 top--; /* #12 */
597 }
598 }
599 out:
600 if (top != op_stack) {
601 /* Too many '(' */
602 parse_error(pe, FILT_ERR_TOO_MANY_OPEN, ptr - str);
603 goto out_free;
604 }
605
606 if (!N) {
607 /* No program? */
608 ret = -EINVAL;
609 parse_error(pe, FILT_ERR_NO_FILTER, ptr - str);
610 goto out_free;
611 }
612
613 prog[N].pred = NULL; /* #13 */
614 prog[N].target = 1; /* TRUE */
615 prog[N+1].pred = NULL;
616 prog[N+1].target = 0; /* FALSE */
617 prog[N-1].target = N;
618 prog[N-1].when_to_branch = false;
619
620 /* Second Pass */
621 for (i = N-1 ; i--; ) {
622 int target = prog[i].target;
623 if (prog[i].when_to_branch == prog[target].when_to_branch)
624 prog[i].target = prog[target].target;
625 }
626
627 /* Third Pass */
628 for (i = 0; i < N; i++) {
629 invert = inverts[i] ^ prog[i].when_to_branch;
630 prog[i].when_to_branch = invert;
631 /* Make sure the program always moves forward */
632 if (WARN_ON(prog[i].target <= i)) {
633 ret = -EINVAL;
634 goto out_free;
635 }
636 }
637
638 kfree(op_stack);
639 kfree(inverts);
640 return prog;
641 out_free:
642 kfree(op_stack);
643 kfree(inverts);
644 if (prog_stack) {
645 for (i = 0; prog_stack[i].pred; i++)
646 free_predicate(prog_stack[i].pred);
647 kfree(prog_stack);
648 }
649 return ERR_PTR(ret);
650 }
651
652 static inline int
do_filter_cpumask(int op,const struct cpumask * mask,const struct cpumask * cmp)653 do_filter_cpumask(int op, const struct cpumask *mask, const struct cpumask *cmp)
654 {
655 switch (op) {
656 case OP_EQ:
657 return cpumask_equal(mask, cmp);
658 case OP_NE:
659 return !cpumask_equal(mask, cmp);
660 case OP_BAND:
661 return cpumask_intersects(mask, cmp);
662 default:
663 return 0;
664 }
665 }
666
667 /* Optimisation of do_filter_cpumask() for scalar fields */
668 static inline int
do_filter_scalar_cpumask(int op,unsigned int cpu,const struct cpumask * mask)669 do_filter_scalar_cpumask(int op, unsigned int cpu, const struct cpumask *mask)
670 {
671 /*
672 * Per the weight-of-one cpumask optimisations, the mask passed in this
673 * function has a weight >= 2, so it is never equal to a single scalar.
674 */
675 switch (op) {
676 case OP_EQ:
677 return false;
678 case OP_NE:
679 return true;
680 case OP_BAND:
681 return cpumask_test_cpu(cpu, mask);
682 default:
683 return 0;
684 }
685 }
686
687 static inline int
do_filter_cpumask_scalar(int op,const struct cpumask * mask,unsigned int cpu)688 do_filter_cpumask_scalar(int op, const struct cpumask *mask, unsigned int cpu)
689 {
690 switch (op) {
691 case OP_EQ:
692 return cpumask_test_cpu(cpu, mask) &&
693 cpumask_nth(1, mask) >= nr_cpu_ids;
694 case OP_NE:
695 return !cpumask_test_cpu(cpu, mask) ||
696 cpumask_nth(1, mask) < nr_cpu_ids;
697 case OP_BAND:
698 return cpumask_test_cpu(cpu, mask);
699 default:
700 return 0;
701 }
702 }
703
704 enum pred_cmp_types {
705 PRED_CMP_TYPE_NOP,
706 PRED_CMP_TYPE_LT,
707 PRED_CMP_TYPE_LE,
708 PRED_CMP_TYPE_GT,
709 PRED_CMP_TYPE_GE,
710 PRED_CMP_TYPE_BAND,
711 };
712
713 #define DEFINE_COMPARISON_PRED(type) \
714 static int filter_pred_##type(struct filter_pred *pred, void *event) \
715 { \
716 switch (pred->op) { \
717 case OP_LT: { \
718 type *addr = (type *)(event + pred->offset); \
719 type val = (type)pred->val; \
720 return *addr < val; \
721 } \
722 case OP_LE: { \
723 type *addr = (type *)(event + pred->offset); \
724 type val = (type)pred->val; \
725 return *addr <= val; \
726 } \
727 case OP_GT: { \
728 type *addr = (type *)(event + pred->offset); \
729 type val = (type)pred->val; \
730 return *addr > val; \
731 } \
732 case OP_GE: { \
733 type *addr = (type *)(event + pred->offset); \
734 type val = (type)pred->val; \
735 return *addr >= val; \
736 } \
737 case OP_BAND: { \
738 type *addr = (type *)(event + pred->offset); \
739 type val = (type)pred->val; \
740 return !!(*addr & val); \
741 } \
742 default: \
743 return 0; \
744 } \
745 }
746
747 #define DEFINE_CPUMASK_COMPARISON_PRED(size) \
748 static int filter_pred_##size##_cpumask(struct filter_pred *pred, void *event) \
749 { \
750 u##size *addr = (u##size *)(event + pred->offset); \
751 unsigned int cpu = *addr; \
752 \
753 if (cpu >= nr_cpu_ids) \
754 return 0; \
755 \
756 return do_filter_scalar_cpumask(pred->op, cpu, pred->mask); \
757 }
758
759 #define DEFINE_EQUALITY_PRED(size) \
760 static int filter_pred_##size(struct filter_pred *pred, void *event) \
761 { \
762 u##size *addr = (u##size *)(event + pred->offset); \
763 u##size val = (u##size)pred->val; \
764 int match; \
765 \
766 match = (val == *addr) ^ pred->not; \
767 \
768 return match; \
769 }
770
771 DEFINE_COMPARISON_PRED(s64);
772 DEFINE_COMPARISON_PRED(u64);
773 DEFINE_COMPARISON_PRED(s32);
774 DEFINE_COMPARISON_PRED(u32);
775 DEFINE_COMPARISON_PRED(s16);
776 DEFINE_COMPARISON_PRED(u16);
777 DEFINE_COMPARISON_PRED(s8);
778 DEFINE_COMPARISON_PRED(u8);
779
780 DEFINE_CPUMASK_COMPARISON_PRED(64);
781 DEFINE_CPUMASK_COMPARISON_PRED(32);
782 DEFINE_CPUMASK_COMPARISON_PRED(16);
783 DEFINE_CPUMASK_COMPARISON_PRED(8);
784
785 DEFINE_EQUALITY_PRED(64);
786 DEFINE_EQUALITY_PRED(32);
787 DEFINE_EQUALITY_PRED(16);
788 DEFINE_EQUALITY_PRED(8);
789
790 /* user space strings temp buffer */
791 #define USTRING_BUF_SIZE 1024
792
793 struct ustring_buffer {
794 char buffer[USTRING_BUF_SIZE];
795 };
796
797 static __percpu struct ustring_buffer *ustring_per_cpu;
798
test_string(char * str)799 static __always_inline char *test_string(char *str)
800 {
801 struct ustring_buffer *ubuf;
802 char *kstr;
803
804 if (!ustring_per_cpu)
805 return NULL;
806
807 ubuf = this_cpu_ptr(ustring_per_cpu);
808 kstr = ubuf->buffer;
809
810 /* For safety, do not trust the string pointer */
811 if (strncpy_from_kernel_nofault(kstr, str, USTRING_BUF_SIZE) < 0)
812 return NULL;
813 return kstr;
814 }
815
test_ustring(char * str)816 static __always_inline char *test_ustring(char *str)
817 {
818 struct ustring_buffer *ubuf;
819 char __user *ustr;
820 char *kstr;
821
822 if (!ustring_per_cpu)
823 return NULL;
824
825 ubuf = this_cpu_ptr(ustring_per_cpu);
826 kstr = ubuf->buffer;
827
828 /* user space address? */
829 ustr = (char __user *)str;
830 if (strncpy_from_user_nofault(kstr, ustr, USTRING_BUF_SIZE) < 0)
831 return NULL;
832
833 return kstr;
834 }
835
836 /* Filter predicate for fixed sized arrays of characters */
filter_pred_string(struct filter_pred * pred,void * event)837 static int filter_pred_string(struct filter_pred *pred, void *event)
838 {
839 char *addr = (char *)(event + pred->offset);
840 int cmp, match;
841
842 cmp = pred->regex->match(addr, pred->regex, pred->regex->field_len);
843
844 match = cmp ^ pred->not;
845
846 return match;
847 }
848
filter_pchar(struct filter_pred * pred,char * str)849 static __always_inline int filter_pchar(struct filter_pred *pred, char *str)
850 {
851 int cmp, match;
852 int len;
853
854 len = strlen(str) + 1; /* including tailing '\0' */
855 cmp = pred->regex->match(str, pred->regex, len);
856
857 match = cmp ^ pred->not;
858
859 return match;
860 }
861 /* Filter predicate for char * pointers */
filter_pred_pchar(struct filter_pred * pred,void * event)862 static int filter_pred_pchar(struct filter_pred *pred, void *event)
863 {
864 char **addr = (char **)(event + pred->offset);
865 char *str;
866
867 str = test_string(*addr);
868 if (!str)
869 return 0;
870
871 return filter_pchar(pred, str);
872 }
873
874 /* Filter predicate for char * pointers in user space*/
filter_pred_pchar_user(struct filter_pred * pred,void * event)875 static int filter_pred_pchar_user(struct filter_pred *pred, void *event)
876 {
877 char **addr = (char **)(event + pred->offset);
878 char *str;
879
880 str = test_ustring(*addr);
881 if (!str)
882 return 0;
883
884 return filter_pchar(pred, str);
885 }
886
887 /*
888 * Filter predicate for dynamic sized arrays of characters.
889 * These are implemented through a list of strings at the end
890 * of the entry.
891 * Also each of these strings have a field in the entry which
892 * contains its offset from the beginning of the entry.
893 * We have then first to get this field, dereference it
894 * and add it to the address of the entry, and at last we have
895 * the address of the string.
896 */
filter_pred_strloc(struct filter_pred * pred,void * event)897 static int filter_pred_strloc(struct filter_pred *pred, void *event)
898 {
899 u32 str_item = *(u32 *)(event + pred->offset);
900 int str_loc = str_item & 0xffff;
901 int str_len = str_item >> 16;
902 char *addr = (char *)(event + str_loc);
903 int cmp, match;
904
905 cmp = pred->regex->match(addr, pred->regex, str_len);
906
907 match = cmp ^ pred->not;
908
909 return match;
910 }
911
912 /*
913 * Filter predicate for relative dynamic sized arrays of characters.
914 * These are implemented through a list of strings at the end
915 * of the entry as same as dynamic string.
916 * The difference is that the relative one records the location offset
917 * from the field itself, not the event entry.
918 */
filter_pred_strrelloc(struct filter_pred * pred,void * event)919 static int filter_pred_strrelloc(struct filter_pred *pred, void *event)
920 {
921 u32 *item = (u32 *)(event + pred->offset);
922 u32 str_item = *item;
923 int str_loc = str_item & 0xffff;
924 int str_len = str_item >> 16;
925 char *addr = (char *)(&item[1]) + str_loc;
926 int cmp, match;
927
928 cmp = pred->regex->match(addr, pred->regex, str_len);
929
930 match = cmp ^ pred->not;
931
932 return match;
933 }
934
935 /* Filter predicate for CPUs. */
filter_pred_cpu(struct filter_pred * pred,void * event)936 static int filter_pred_cpu(struct filter_pred *pred, void *event)
937 {
938 int cpu, cmp;
939
940 cpu = raw_smp_processor_id();
941 cmp = pred->val;
942
943 switch (pred->op) {
944 case OP_EQ:
945 return cpu == cmp;
946 case OP_NE:
947 return cpu != cmp;
948 case OP_LT:
949 return cpu < cmp;
950 case OP_LE:
951 return cpu <= cmp;
952 case OP_GT:
953 return cpu > cmp;
954 case OP_GE:
955 return cpu >= cmp;
956 default:
957 return 0;
958 }
959 }
960
961 /* Filter predicate for current CPU vs user-provided cpumask */
filter_pred_cpu_cpumask(struct filter_pred * pred,void * event)962 static int filter_pred_cpu_cpumask(struct filter_pred *pred, void *event)
963 {
964 int cpu = raw_smp_processor_id();
965
966 return do_filter_scalar_cpumask(pred->op, cpu, pred->mask);
967 }
968
969 /* Filter predicate for cpumask field vs user-provided cpumask */
filter_pred_cpumask(struct filter_pred * pred,void * event)970 static int filter_pred_cpumask(struct filter_pred *pred, void *event)
971 {
972 u32 item = *(u32 *)(event + pred->offset);
973 int loc = item & 0xffff;
974 const struct cpumask *mask = (event + loc);
975 const struct cpumask *cmp = pred->mask;
976
977 return do_filter_cpumask(pred->op, mask, cmp);
978 }
979
980 /* Filter predicate for cpumask field vs user-provided scalar */
filter_pred_cpumask_cpu(struct filter_pred * pred,void * event)981 static int filter_pred_cpumask_cpu(struct filter_pred *pred, void *event)
982 {
983 u32 item = *(u32 *)(event + pred->offset);
984 int loc = item & 0xffff;
985 const struct cpumask *mask = (event + loc);
986 unsigned int cpu = pred->val;
987
988 return do_filter_cpumask_scalar(pred->op, mask, cpu);
989 }
990
991 /* Filter predicate for COMM. */
filter_pred_comm(struct filter_pred * pred,void * event)992 static int filter_pred_comm(struct filter_pred *pred, void *event)
993 {
994 int cmp;
995
996 cmp = pred->regex->match(current->comm, pred->regex,
997 TASK_COMM_LEN);
998 return cmp ^ pred->not;
999 }
1000
1001 /* Filter predicate for functions. */
filter_pred_function(struct filter_pred * pred,void * event)1002 static int filter_pred_function(struct filter_pred *pred, void *event)
1003 {
1004 unsigned long *addr = (unsigned long *)(event + pred->offset);
1005 unsigned long start = (unsigned long)pred->val;
1006 unsigned long end = (unsigned long)pred->val2;
1007 int ret = *addr >= start && *addr < end;
1008
1009 return pred->op == OP_EQ ? ret : !ret;
1010 }
1011
1012 /*
1013 * regex_match_foo - Basic regex callbacks
1014 *
1015 * @str: the string to be searched
1016 * @r: the regex structure containing the pattern string
1017 * @len: the length of the string to be searched (including '\0')
1018 *
1019 * Note:
1020 * - @str might not be NULL-terminated if it's of type DYN_STRING
1021 * RDYN_STRING, or STATIC_STRING, unless @len is zero.
1022 */
1023
regex_match_full(char * str,struct regex * r,int len)1024 static int regex_match_full(char *str, struct regex *r, int len)
1025 {
1026 /* len of zero means str is dynamic and ends with '\0' */
1027 if (!len)
1028 return strcmp(str, r->pattern) == 0;
1029
1030 if (len < r->len)
1031 return 0;
1032
1033 return strncmp(str, r->pattern, len) == 0;
1034 }
1035
regex_match_front(char * str,struct regex * r,int len)1036 static int regex_match_front(char *str, struct regex *r, int len)
1037 {
1038 if (len && len < r->len)
1039 return 0;
1040
1041 return strncmp(str, r->pattern, r->len) == 0;
1042 }
1043
regex_match_middle(char * str,struct regex * r,int len)1044 static int regex_match_middle(char *str, struct regex *r, int len)
1045 {
1046 if (!len)
1047 return strstr(str, r->pattern) != NULL;
1048
1049 return strnstr(str, r->pattern, len) != NULL;
1050 }
1051
regex_match_end(char * str,struct regex * r,int len)1052 static int regex_match_end(char *str, struct regex *r, int len)
1053 {
1054 int strlen = len - 1;
1055
1056 if (strlen >= r->len &&
1057 memcmp(str + strlen - r->len, r->pattern, r->len) == 0)
1058 return 1;
1059 return 0;
1060 }
1061
regex_match_glob(char * str,struct regex * r,int len)1062 static int regex_match_glob(char *str, struct regex *r, int len)
1063 {
1064 return glob_match_len(r->pattern, str, len) ? 1 : 0;
1065 }
1066
1067 /**
1068 * filter_parse_regex - parse a basic regex
1069 * @buff: the raw regex
1070 * @len: length of the regex
1071 * @search: will point to the beginning of the string to compare
1072 * @not: tell whether the match will have to be inverted
1073 *
1074 * This passes in a buffer containing a regex and this function will
1075 * set search to point to the search part of the buffer and
1076 * return the type of search it is (see enum above).
1077 * This does modify buff.
1078 *
1079 * Returns enum type.
1080 * search returns the pointer to use for comparison.
1081 * not returns 1 if buff started with a '!'
1082 * 0 otherwise.
1083 */
filter_parse_regex(char * buff,int len,char ** search,int * not)1084 enum regex_type filter_parse_regex(char *buff, int len, char **search, int *not)
1085 {
1086 int type = MATCH_FULL;
1087 int i;
1088
1089 if (buff[0] == '!') {
1090 *not = 1;
1091 buff++;
1092 len--;
1093 } else
1094 *not = 0;
1095
1096 *search = buff;
1097
1098 if (isdigit(buff[0]))
1099 return MATCH_INDEX;
1100
1101 for (i = 0; i < len; i++) {
1102 if (buff[i] == '*') {
1103 if (!i) {
1104 type = MATCH_END_ONLY;
1105 } else if (i == len - 1) {
1106 if (type == MATCH_END_ONLY)
1107 type = MATCH_MIDDLE_ONLY;
1108 else
1109 type = MATCH_FRONT_ONLY;
1110 buff[i] = 0;
1111 break;
1112 } else { /* pattern continues, use full glob */
1113 return MATCH_GLOB;
1114 }
1115 } else if (strchr("[?\\", buff[i])) {
1116 return MATCH_GLOB;
1117 }
1118 }
1119 if (buff[0] == '*')
1120 *search = buff + 1;
1121
1122 return type;
1123 }
1124
filter_build_regex(struct filter_pred * pred)1125 static void filter_build_regex(struct filter_pred *pred)
1126 {
1127 struct regex *r = pred->regex;
1128 char *search;
1129 enum regex_type type = MATCH_FULL;
1130
1131 if (pred->op == OP_GLOB) {
1132 type = filter_parse_regex(r->pattern, r->len, &search, &pred->not);
1133 r->len = strlen(search);
1134 memmove(r->pattern, search, r->len+1);
1135 }
1136
1137 switch (type) {
1138 /* MATCH_INDEX should not happen, but if it does, match full */
1139 case MATCH_INDEX:
1140 case MATCH_FULL:
1141 r->match = regex_match_full;
1142 break;
1143 case MATCH_FRONT_ONLY:
1144 r->match = regex_match_front;
1145 break;
1146 case MATCH_MIDDLE_ONLY:
1147 r->match = regex_match_middle;
1148 break;
1149 case MATCH_END_ONLY:
1150 r->match = regex_match_end;
1151 break;
1152 case MATCH_GLOB:
1153 r->match = regex_match_glob;
1154 break;
1155 }
1156 }
1157
1158
1159 #ifdef CONFIG_FTRACE_STARTUP_TEST
1160 static int test_pred_visited_fn(struct filter_pred *pred, void *event);
1161 #else
test_pred_visited_fn(struct filter_pred * pred,void * event)1162 static int test_pred_visited_fn(struct filter_pred *pred, void *event)
1163 {
1164 return 0;
1165 }
1166 #endif
1167
1168
1169 static int filter_pred_fn_call(struct filter_pred *pred, void *event);
1170
1171 /* return 1 if event matches, 0 otherwise (discard) */
filter_match_preds(struct event_filter * filter,void * rec)1172 int filter_match_preds(struct event_filter *filter, void *rec)
1173 {
1174 struct prog_entry *prog;
1175 int i;
1176
1177 /* no filter is considered a match */
1178 if (!filter)
1179 return 1;
1180
1181 /* Protected by either SRCU(tracepoint_srcu) or preempt_disable */
1182 prog = rcu_dereference_raw(filter->prog);
1183 if (!prog)
1184 return 1;
1185
1186 for (i = 0; prog[i].pred; i++) {
1187 struct filter_pred *pred = prog[i].pred;
1188 int match = filter_pred_fn_call(pred, rec);
1189 if (match == prog[i].when_to_branch)
1190 i = prog[i].target;
1191 }
1192 return prog[i].target;
1193 }
1194 EXPORT_SYMBOL_GPL(filter_match_preds);
1195
remove_filter_string(struct event_filter * filter)1196 static void remove_filter_string(struct event_filter *filter)
1197 {
1198 if (!filter)
1199 return;
1200
1201 kfree(filter->filter_string);
1202 filter->filter_string = NULL;
1203 }
1204
append_filter_err(struct trace_array * tr,struct filter_parse_error * pe,struct event_filter * filter)1205 static void append_filter_err(struct trace_array *tr,
1206 struct filter_parse_error *pe,
1207 struct event_filter *filter)
1208 {
1209 struct trace_seq *s;
1210 int pos = pe->lasterr_pos;
1211 char *buf;
1212 int len;
1213
1214 if (WARN_ON(!filter->filter_string))
1215 return;
1216
1217 s = kmalloc_obj(*s);
1218 if (!s)
1219 return;
1220 trace_seq_init(s);
1221
1222 len = strlen(filter->filter_string);
1223 if (pos > len)
1224 pos = len;
1225
1226 /* indexing is off by one */
1227 if (pos)
1228 pos++;
1229
1230 trace_seq_puts(s, filter->filter_string);
1231 if (pe->lasterr > 0) {
1232 trace_seq_printf(s, "\n%*s", pos, "^");
1233 trace_seq_printf(s, "\nparse_error: %s\n", err_text[pe->lasterr]);
1234 tracing_log_err(tr, "event filter parse error",
1235 filter->filter_string, err_text,
1236 pe->lasterr, pe->lasterr_pos);
1237 } else {
1238 trace_seq_printf(s, "\nError: (%d)\n", pe->lasterr);
1239 tracing_log_err(tr, "event filter parse error",
1240 filter->filter_string, err_text,
1241 FILT_ERR_ERRNO, 0);
1242 }
1243 trace_seq_putc(s, 0);
1244 buf = kmemdup_nul(s->buffer, s->seq.len, GFP_KERNEL);
1245 if (buf) {
1246 kfree(filter->filter_string);
1247 filter->filter_string = buf;
1248 }
1249 kfree(s);
1250 }
1251
event_filter(struct trace_event_file * file)1252 static inline struct event_filter *event_filter(struct trace_event_file *file)
1253 {
1254 return rcu_dereference_protected(file->filter,
1255 lockdep_is_held(&event_mutex));
1256
1257 }
1258
1259 /* caller must hold event_mutex */
print_event_filter(struct trace_event_file * file,struct trace_seq * s)1260 void print_event_filter(struct trace_event_file *file, struct trace_seq *s)
1261 {
1262 struct event_filter *filter = event_filter(file);
1263
1264 if (filter && filter->filter_string)
1265 trace_seq_printf(s, "%s\n", filter->filter_string);
1266 else
1267 trace_seq_puts(s, "none\n");
1268 }
1269
print_subsystem_event_filter(struct event_subsystem * system,struct trace_seq * s)1270 void print_subsystem_event_filter(struct event_subsystem *system,
1271 struct trace_seq *s)
1272 {
1273 struct event_filter *filter;
1274
1275 mutex_lock(&event_mutex);
1276 filter = system->filter;
1277 if (filter && filter->filter_string)
1278 trace_seq_printf(s, "%s\n", filter->filter_string);
1279 else
1280 trace_seq_puts(s, DEFAULT_SYS_FILTER_MESSAGE "\n");
1281 mutex_unlock(&event_mutex);
1282 }
1283
free_prog(struct event_filter * filter)1284 static void free_prog(struct event_filter *filter)
1285 {
1286 struct prog_entry *prog;
1287 int i;
1288
1289 prog = rcu_access_pointer(filter->prog);
1290 if (!prog)
1291 return;
1292
1293 for (i = 0; prog[i].pred; i++)
1294 free_predicate(prog[i].pred);
1295 kfree(prog);
1296 }
1297
filter_disable(struct trace_event_file * file)1298 static void filter_disable(struct trace_event_file *file)
1299 {
1300 unsigned long old_flags = file->flags;
1301
1302 file->flags &= ~EVENT_FILE_FL_FILTERED;
1303
1304 if (old_flags != file->flags)
1305 trace_buffered_event_disable();
1306 }
1307
__free_filter(struct event_filter * filter)1308 static void __free_filter(struct event_filter *filter)
1309 {
1310 if (!filter)
1311 return;
1312
1313 free_prog(filter);
1314 kfree(filter->filter_string);
1315 kfree(filter);
1316 }
1317
free_event_filter(struct event_filter * filter)1318 void free_event_filter(struct event_filter *filter)
1319 {
1320 __free_filter(filter);
1321 }
1322
__remove_filter(struct trace_event_file * file)1323 static inline void __remove_filter(struct trace_event_file *file)
1324 {
1325 filter_disable(file);
1326 remove_filter_string(event_filter(file));
1327 }
1328
filter_free_subsystem_preds(struct trace_subsystem_dir * dir,struct trace_array * tr)1329 static void filter_free_subsystem_preds(struct trace_subsystem_dir *dir,
1330 struct trace_array *tr)
1331 {
1332 struct trace_event_file *file;
1333
1334 list_for_each_entry(file, &tr->events, list) {
1335 if (file->system != dir)
1336 continue;
1337 __remove_filter(file);
1338 }
1339 }
1340
1341 struct filter_list {
1342 struct list_head list;
1343 struct event_filter *filter;
1344 };
1345
1346 struct filter_head {
1347 struct list_head list;
1348 union {
1349 struct rcu_head rcu;
1350 struct rcu_work rwork;
1351 };
1352 };
1353
free_filter_list(struct filter_head * filter_list)1354 static void free_filter_list(struct filter_head *filter_list)
1355 {
1356 struct filter_list *filter_item, *tmp;
1357
1358 list_for_each_entry_safe(filter_item, tmp, &filter_list->list, list) {
1359 __free_filter(filter_item->filter);
1360 list_del(&filter_item->list);
1361 kfree(filter_item);
1362 }
1363 kfree(filter_list);
1364 }
1365
free_filter_list_work(struct work_struct * work)1366 static void free_filter_list_work(struct work_struct *work)
1367 {
1368 struct filter_head *filter_list;
1369
1370 filter_list = container_of(to_rcu_work(work), struct filter_head, rwork);
1371 free_filter_list(filter_list);
1372 }
1373
free_filter_list_tasks(struct rcu_head * rhp)1374 static void free_filter_list_tasks(struct rcu_head *rhp)
1375 {
1376 struct filter_head *filter_list = container_of(rhp, struct filter_head, rcu);
1377
1378 INIT_RCU_WORK(&filter_list->rwork, free_filter_list_work);
1379 queue_rcu_work(system_dfl_wq, &filter_list->rwork);
1380 }
1381
1382 /*
1383 * The tracepoint_synchronize_unregister() is a double rcu call.
1384 * It calls synchronize_rcu_tasks_trace() followed by synchronize_rcu().
1385 * Instead of waiting for it, simply call these via the call_rcu*()
1386 * variants.
1387 */
delay_free_filter(struct filter_head * head)1388 static void delay_free_filter(struct filter_head *head)
1389 {
1390 call_rcu_tasks_trace(&head->rcu, free_filter_list_tasks);
1391 }
1392
try_delay_free_filter(struct event_filter * filter)1393 static void try_delay_free_filter(struct event_filter *filter)
1394 {
1395 struct filter_head *head;
1396 struct filter_list *item;
1397
1398 head = kmalloc_obj(*head);
1399 if (!head)
1400 goto free_now;
1401
1402 INIT_LIST_HEAD(&head->list);
1403
1404 item = kmalloc_obj(*item);
1405 if (!item) {
1406 kfree(head);
1407 goto free_now;
1408 }
1409
1410 item->filter = filter;
1411 list_add_tail(&item->list, &head->list);
1412 delay_free_filter(head);
1413 return;
1414
1415 free_now:
1416 /* Make sure the filter is not being used */
1417 tracepoint_synchronize_unregister();
1418 __free_filter(filter);
1419 }
1420
__free_subsystem_filter(struct trace_event_file * file)1421 static inline void __free_subsystem_filter(struct trace_event_file *file)
1422 {
1423 __free_filter(event_filter(file));
1424 file->filter = NULL;
1425 }
1426
event_set_filter(struct trace_event_file * file,struct event_filter * filter)1427 static inline void event_set_filter(struct trace_event_file *file,
1428 struct event_filter *filter)
1429 {
1430 rcu_assign_pointer(file->filter, filter);
1431 }
1432
event_clear_filter(struct trace_event_file * file)1433 static inline void event_clear_filter(struct trace_event_file *file)
1434 {
1435 RCU_INIT_POINTER(file->filter, NULL);
1436 }
1437
filter_free_subsystem_filters(struct trace_subsystem_dir * dir,struct trace_array * tr,struct event_filter * filter)1438 static void filter_free_subsystem_filters(struct trace_subsystem_dir *dir,
1439 struct trace_array *tr,
1440 struct event_filter *filter)
1441 {
1442 struct trace_event_file *file;
1443 struct filter_head *head;
1444 struct filter_list *item;
1445
1446 head = kmalloc_obj(*head);
1447 if (!head)
1448 goto free_now;
1449
1450 INIT_LIST_HEAD(&head->list);
1451
1452 list_for_each_entry(file, &tr->events, list) {
1453 if (file->system != dir)
1454 continue;
1455 item = kmalloc_obj(*item);
1456 if (!item)
1457 goto free_now;
1458 item->filter = event_filter(file);
1459 list_add_tail(&item->list, &head->list);
1460 event_clear_filter(file);
1461 }
1462
1463 item = kmalloc_obj(*item);
1464 if (!item)
1465 goto free_now;
1466
1467 item->filter = filter;
1468 list_add_tail(&item->list, &head->list);
1469
1470 delay_free_filter(head);
1471 return;
1472 free_now:
1473 tracepoint_synchronize_unregister();
1474
1475 if (head)
1476 free_filter_list(head);
1477
1478 list_for_each_entry(file, &tr->events, list) {
1479 if (file->system != dir || !file->filter)
1480 continue;
1481 __free_subsystem_filter(file);
1482 }
1483 __free_filter(filter);
1484 }
1485
filter_assign_type(const char * type)1486 int filter_assign_type(const char *type)
1487 {
1488 if (strstr(type, "__data_loc")) {
1489 if (strstr(type, "char"))
1490 return FILTER_DYN_STRING;
1491 if (strstr(type, "cpumask_t"))
1492 return FILTER_CPUMASK;
1493 }
1494
1495 if (strstr(type, "__rel_loc") && strstr(type, "char"))
1496 return FILTER_RDYN_STRING;
1497
1498 if (strchr(type, '[') && strstr(type, "char"))
1499 return FILTER_STATIC_STRING;
1500
1501 if (strcmp(type, "char *") == 0 || strcmp(type, "const char *") == 0)
1502 return FILTER_PTR_STRING;
1503
1504 return FILTER_OTHER;
1505 }
1506
select_comparison_fn(enum filter_op_ids op,int field_size,int field_is_signed)1507 static enum filter_pred_fn select_comparison_fn(enum filter_op_ids op,
1508 int field_size, int field_is_signed)
1509 {
1510 enum filter_pred_fn fn = FILTER_PRED_FN_NOP;
1511 int pred_func_index = -1;
1512
1513 switch (op) {
1514 case OP_EQ:
1515 case OP_NE:
1516 break;
1517 default:
1518 if (WARN_ON_ONCE(op < PRED_FUNC_START))
1519 return fn;
1520 pred_func_index = op - PRED_FUNC_START;
1521 if (WARN_ON_ONCE(pred_func_index > PRED_FUNC_MAX))
1522 return fn;
1523 }
1524
1525 switch (field_size) {
1526 case 8:
1527 if (pred_func_index < 0)
1528 fn = FILTER_PRED_FN_64;
1529 else if (field_is_signed)
1530 fn = FILTER_PRED_FN_S64;
1531 else
1532 fn = FILTER_PRED_FN_U64;
1533 break;
1534 case 4:
1535 if (pred_func_index < 0)
1536 fn = FILTER_PRED_FN_32;
1537 else if (field_is_signed)
1538 fn = FILTER_PRED_FN_S32;
1539 else
1540 fn = FILTER_PRED_FN_U32;
1541 break;
1542 case 2:
1543 if (pred_func_index < 0)
1544 fn = FILTER_PRED_FN_16;
1545 else if (field_is_signed)
1546 fn = FILTER_PRED_FN_S16;
1547 else
1548 fn = FILTER_PRED_FN_U16;
1549 break;
1550 case 1:
1551 if (pred_func_index < 0)
1552 fn = FILTER_PRED_FN_8;
1553 else if (field_is_signed)
1554 fn = FILTER_PRED_FN_S8;
1555 else
1556 fn = FILTER_PRED_FN_U8;
1557 break;
1558 }
1559
1560 return fn;
1561 }
1562
1563
filter_pred_fn_call(struct filter_pred * pred,void * event)1564 static int filter_pred_fn_call(struct filter_pred *pred, void *event)
1565 {
1566 switch (pred->fn_num) {
1567 case FILTER_PRED_FN_64:
1568 return filter_pred_64(pred, event);
1569 case FILTER_PRED_FN_64_CPUMASK:
1570 return filter_pred_64_cpumask(pred, event);
1571 case FILTER_PRED_FN_S64:
1572 return filter_pred_s64(pred, event);
1573 case FILTER_PRED_FN_U64:
1574 return filter_pred_u64(pred, event);
1575 case FILTER_PRED_FN_32:
1576 return filter_pred_32(pred, event);
1577 case FILTER_PRED_FN_32_CPUMASK:
1578 return filter_pred_32_cpumask(pred, event);
1579 case FILTER_PRED_FN_S32:
1580 return filter_pred_s32(pred, event);
1581 case FILTER_PRED_FN_U32:
1582 return filter_pred_u32(pred, event);
1583 case FILTER_PRED_FN_16:
1584 return filter_pred_16(pred, event);
1585 case FILTER_PRED_FN_16_CPUMASK:
1586 return filter_pred_16_cpumask(pred, event);
1587 case FILTER_PRED_FN_S16:
1588 return filter_pred_s16(pred, event);
1589 case FILTER_PRED_FN_U16:
1590 return filter_pred_u16(pred, event);
1591 case FILTER_PRED_FN_8:
1592 return filter_pred_8(pred, event);
1593 case FILTER_PRED_FN_8_CPUMASK:
1594 return filter_pred_8_cpumask(pred, event);
1595 case FILTER_PRED_FN_S8:
1596 return filter_pred_s8(pred, event);
1597 case FILTER_PRED_FN_U8:
1598 return filter_pred_u8(pred, event);
1599 case FILTER_PRED_FN_COMM:
1600 return filter_pred_comm(pred, event);
1601 case FILTER_PRED_FN_STRING:
1602 return filter_pred_string(pred, event);
1603 case FILTER_PRED_FN_STRLOC:
1604 return filter_pred_strloc(pred, event);
1605 case FILTER_PRED_FN_STRRELLOC:
1606 return filter_pred_strrelloc(pred, event);
1607 case FILTER_PRED_FN_PCHAR_USER:
1608 return filter_pred_pchar_user(pred, event);
1609 case FILTER_PRED_FN_PCHAR:
1610 return filter_pred_pchar(pred, event);
1611 case FILTER_PRED_FN_CPU:
1612 return filter_pred_cpu(pred, event);
1613 case FILTER_PRED_FN_CPU_CPUMASK:
1614 return filter_pred_cpu_cpumask(pred, event);
1615 case FILTER_PRED_FN_CPUMASK:
1616 return filter_pred_cpumask(pred, event);
1617 case FILTER_PRED_FN_CPUMASK_CPU:
1618 return filter_pred_cpumask_cpu(pred, event);
1619 case FILTER_PRED_FN_FUNCTION:
1620 return filter_pred_function(pred, event);
1621 case FILTER_PRED_TEST_VISITED:
1622 return test_pred_visited_fn(pred, event);
1623 default:
1624 return 0;
1625 }
1626 }
1627
1628 /* Called when a predicate is encountered by predicate_parse() */
parse_pred(const char * str,void * data,int pos,struct filter_parse_error * pe,struct filter_pred ** pred_ptr)1629 static int parse_pred(const char *str, void *data,
1630 int pos, struct filter_parse_error *pe,
1631 struct filter_pred **pred_ptr)
1632 {
1633 struct trace_event_call *call = data;
1634 struct ftrace_event_field *field;
1635 struct filter_pred *pred = NULL;
1636 unsigned long offset;
1637 unsigned long size;
1638 unsigned long ip;
1639 char num_buf[24]; /* Big enough to hold an address */
1640 char *field_name;
1641 char *name;
1642 bool function = false;
1643 bool ustring = false;
1644 char q;
1645 u64 val;
1646 int len;
1647 int ret;
1648 int op;
1649 int s;
1650 int i = 0;
1651
1652 /* First find the field to associate to */
1653 while (isspace(str[i]))
1654 i++;
1655 s = i;
1656
1657 while (isalnum(str[i]) || str[i] == '_')
1658 i++;
1659
1660 len = i - s;
1661
1662 if (!len)
1663 return -1;
1664
1665 field_name = kmemdup_nul(str + s, len, GFP_KERNEL);
1666 if (!field_name)
1667 return -ENOMEM;
1668
1669 /* Make sure that the field exists */
1670
1671 field = trace_find_event_field(call, field_name);
1672 kfree(field_name);
1673 if (!field) {
1674 parse_error(pe, FILT_ERR_FIELD_NOT_FOUND, pos + i);
1675 return -EINVAL;
1676 }
1677
1678 /* See if the field is a user space string */
1679 if ((len = str_has_prefix(str + i, ".ustring"))) {
1680 ustring = true;
1681 i += len;
1682 }
1683
1684 /* See if the field is a kernel function name */
1685 if ((len = str_has_prefix(str + i, ".function"))) {
1686 function = true;
1687 i += len;
1688 }
1689
1690 while (isspace(str[i]))
1691 i++;
1692
1693 /* Make sure this op is supported */
1694 for (op = 0; ops[op]; op++) {
1695 /* This is why '<=' must come before '<' in ops[] */
1696 if (strncmp(str + i, ops[op], strlen(ops[op])) == 0)
1697 break;
1698 }
1699
1700 if (!ops[op]) {
1701 parse_error(pe, FILT_ERR_INVALID_OP, pos + i);
1702 goto err_free;
1703 }
1704
1705 i += strlen(ops[op]);
1706
1707 while (isspace(str[i]))
1708 i++;
1709
1710 s = i;
1711
1712 pred = kzalloc_obj(*pred);
1713 if (!pred)
1714 return -ENOMEM;
1715
1716 pred->field = field;
1717 pred->offset = field->offset;
1718 pred->op = op;
1719
1720 if (function) {
1721 /* The field must be the same size as long */
1722 if (field->size != sizeof(long)) {
1723 parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
1724 goto err_free;
1725 }
1726
1727 /* Function only works with '==' or '!=' and an unquoted string */
1728 switch (op) {
1729 case OP_NE:
1730 case OP_EQ:
1731 break;
1732 default:
1733 parse_error(pe, FILT_ERR_INVALID_OP, pos + i);
1734 goto err_free;
1735 }
1736
1737 if (isdigit(str[i])) {
1738 /* We allow 0xDEADBEEF */
1739 while (isalnum(str[i]))
1740 i++;
1741
1742 len = i - s;
1743 /* 0xfeedfacedeadbeef is 18 chars max */
1744 if (len >= sizeof(num_buf)) {
1745 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1746 goto err_free;
1747 }
1748
1749 memcpy(num_buf, str + s, len);
1750 num_buf[len] = 0;
1751
1752 ret = kstrtoul(num_buf, 0, &ip);
1753 if (ret) {
1754 parse_error(pe, FILT_ERR_INVALID_VALUE, pos + i);
1755 goto err_free;
1756 }
1757 } else {
1758 s = i;
1759 for (; str[i] && !isspace(str[i]); i++)
1760 ;
1761
1762 len = i - s;
1763 name = kmemdup_nul(str + s, len, GFP_KERNEL);
1764 if (!name)
1765 goto err_mem;
1766 ip = kallsyms_lookup_name(name);
1767 kfree(name);
1768 if (!ip) {
1769 parse_error(pe, FILT_ERR_NO_FUNCTION, pos + i);
1770 goto err_free;
1771 }
1772 }
1773
1774 /* Now find the function start and end address */
1775 if (!kallsyms_lookup_size_offset(ip, &size, &offset)) {
1776 parse_error(pe, FILT_ERR_NO_FUNCTION, pos + i);
1777 goto err_free;
1778 }
1779
1780 pred->fn_num = FILTER_PRED_FN_FUNCTION;
1781 pred->val = ip - offset;
1782 pred->val2 = pred->val + size;
1783
1784 } else if (ftrace_event_is_function(call)) {
1785 /*
1786 * Perf does things different with function events.
1787 * It only allows an "ip" field, and expects a string.
1788 * But the string does not need to be surrounded by quotes.
1789 * If it is a string, the assigned function as a nop,
1790 * (perf doesn't use it) and grab everything.
1791 */
1792 if (strcmp(field->name, "ip") != 0) {
1793 parse_error(pe, FILT_ERR_IP_FIELD_ONLY, pos + i);
1794 goto err_free;
1795 }
1796 pred->fn_num = FILTER_PRED_FN_NOP;
1797
1798 /*
1799 * Quotes are not required, but if they exist then we need
1800 * to read them till we hit a matching one.
1801 */
1802 if (str[i] == '\'' || str[i] == '"')
1803 q = str[i];
1804 else
1805 q = 0;
1806
1807 for (i++; str[i]; i++) {
1808 if (q && str[i] == q)
1809 break;
1810 if (!q && (str[i] == ')' || str[i] == '&' ||
1811 str[i] == '|'))
1812 break;
1813 }
1814 /* Skip quotes */
1815 if (q)
1816 s++;
1817 len = i - s;
1818 if (len >= MAX_FILTER_STR_VAL) {
1819 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1820 goto err_free;
1821 }
1822
1823 pred->regex = kzalloc_obj(*pred->regex);
1824 if (!pred->regex)
1825 goto err_mem;
1826 pred->regex->len = len;
1827 memcpy(pred->regex->pattern, str + s, len);
1828 pred->regex->pattern[len] = 0;
1829
1830 } else if (!strncmp(str + i, "CPUS", 4)) {
1831 unsigned int maskstart;
1832 bool single;
1833 char *tmp;
1834
1835 switch (field->filter_type) {
1836 case FILTER_CPUMASK:
1837 case FILTER_CPU:
1838 case FILTER_OTHER:
1839 break;
1840 default:
1841 parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
1842 goto err_free;
1843 }
1844
1845 switch (op) {
1846 case OP_EQ:
1847 case OP_NE:
1848 case OP_BAND:
1849 break;
1850 default:
1851 parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
1852 goto err_free;
1853 }
1854
1855 /* Skip CPUS */
1856 i += 4;
1857 if (str[i++] != '{') {
1858 parse_error(pe, FILT_ERR_MISSING_BRACE_OPEN, pos + i);
1859 goto err_free;
1860 }
1861 maskstart = i;
1862
1863 /* Walk the cpulist until closing } */
1864 for (; str[i] && str[i] != '}'; i++)
1865 ;
1866
1867 if (str[i] != '}') {
1868 parse_error(pe, FILT_ERR_MISSING_BRACE_CLOSE, pos + i);
1869 goto err_free;
1870 }
1871
1872 if (maskstart == i) {
1873 parse_error(pe, FILT_ERR_INVALID_CPULIST, pos + i);
1874 goto err_free;
1875 }
1876
1877 /* Copy the cpulist between { and } */
1878 tmp = kmalloc((i - maskstart) + 1, GFP_KERNEL);
1879 if (!tmp)
1880 goto err_mem;
1881
1882 strscpy(tmp, str + maskstart, (i - maskstart) + 1);
1883 pred->mask = kzalloc(cpumask_size(), GFP_KERNEL);
1884 if (!pred->mask) {
1885 kfree(tmp);
1886 goto err_mem;
1887 }
1888
1889 /* Now parse it */
1890 if (cpulist_parse(tmp, pred->mask)) {
1891 kfree(tmp);
1892 parse_error(pe, FILT_ERR_INVALID_CPULIST, pos + i);
1893 goto err_free;
1894 }
1895 kfree(tmp);
1896
1897 /* Move along */
1898 i++;
1899
1900 /*
1901 * Optimisation: if the user-provided mask has a weight of one
1902 * then we can treat it as a scalar input.
1903 */
1904 single = cpumask_weight(pred->mask) == 1;
1905 if (single) {
1906 pred->val = cpumask_first(pred->mask);
1907 kfree(pred->mask);
1908 pred->mask = NULL;
1909 }
1910
1911 if (field->filter_type == FILTER_CPUMASK) {
1912 pred->fn_num = single ?
1913 FILTER_PRED_FN_CPUMASK_CPU :
1914 FILTER_PRED_FN_CPUMASK;
1915 } else if (field->filter_type == FILTER_CPU) {
1916 if (single) {
1917 if (pred->op == OP_BAND)
1918 pred->op = OP_EQ;
1919
1920 pred->fn_num = FILTER_PRED_FN_CPU;
1921 } else {
1922 pred->fn_num = FILTER_PRED_FN_CPU_CPUMASK;
1923 }
1924 } else if (single) {
1925 if (pred->op == OP_BAND)
1926 pred->op = OP_EQ;
1927
1928 pred->fn_num = select_comparison_fn(pred->op, field->size, false);
1929 if (pred->op == OP_NE)
1930 pred->not = 1;
1931 } else {
1932 switch (field->size) {
1933 case 8:
1934 pred->fn_num = FILTER_PRED_FN_64_CPUMASK;
1935 break;
1936 case 4:
1937 pred->fn_num = FILTER_PRED_FN_32_CPUMASK;
1938 break;
1939 case 2:
1940 pred->fn_num = FILTER_PRED_FN_16_CPUMASK;
1941 break;
1942 case 1:
1943 pred->fn_num = FILTER_PRED_FN_8_CPUMASK;
1944 break;
1945 }
1946 }
1947
1948 /* This is either a string, or an integer */
1949 } else if (str[i] == '\'' || str[i] == '"') {
1950 char q = str[i];
1951
1952 /* Make sure the op is OK for strings */
1953 switch (op) {
1954 case OP_NE:
1955 pred->not = 1;
1956 fallthrough;
1957 case OP_GLOB:
1958 case OP_EQ:
1959 break;
1960 default:
1961 parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
1962 goto err_free;
1963 }
1964
1965 /* Make sure the field is OK for strings */
1966 if (!is_string_field(field)) {
1967 parse_error(pe, FILT_ERR_EXPECT_DIGIT, pos + i);
1968 goto err_free;
1969 }
1970
1971 for (i++; str[i]; i++) {
1972 if (str[i] == q)
1973 break;
1974 }
1975 if (!str[i]) {
1976 parse_error(pe, FILT_ERR_MISSING_QUOTE, pos + i);
1977 goto err_free;
1978 }
1979
1980 /* Skip quotes */
1981 s++;
1982 len = i - s;
1983 if (len >= MAX_FILTER_STR_VAL) {
1984 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1985 goto err_free;
1986 }
1987
1988 pred->regex = kzalloc_obj(*pred->regex);
1989 if (!pred->regex)
1990 goto err_mem;
1991 pred->regex->len = len;
1992 memcpy(pred->regex->pattern, str + s, len);
1993 pred->regex->pattern[len] = 0;
1994
1995 filter_build_regex(pred);
1996
1997 if (field->filter_type == FILTER_COMM) {
1998 pred->fn_num = FILTER_PRED_FN_COMM;
1999
2000 } else if (field->filter_type == FILTER_STATIC_STRING) {
2001 pred->fn_num = FILTER_PRED_FN_STRING;
2002 pred->regex->field_len = field->size;
2003
2004 } else if (field->filter_type == FILTER_DYN_STRING) {
2005 pred->fn_num = FILTER_PRED_FN_STRLOC;
2006 } else if (field->filter_type == FILTER_RDYN_STRING)
2007 pred->fn_num = FILTER_PRED_FN_STRRELLOC;
2008 else {
2009
2010 if (!ustring_per_cpu) {
2011 /* Once allocated, keep it around for good */
2012 ustring_per_cpu = alloc_percpu(struct ustring_buffer);
2013 if (!ustring_per_cpu)
2014 goto err_mem;
2015 }
2016
2017 if (ustring)
2018 pred->fn_num = FILTER_PRED_FN_PCHAR_USER;
2019 else
2020 pred->fn_num = FILTER_PRED_FN_PCHAR;
2021 }
2022 /* go past the last quote */
2023 i++;
2024
2025 } else if (isdigit(str[i]) || str[i] == '-') {
2026
2027 /* Make sure the field is not a string */
2028 if (is_string_field(field)) {
2029 parse_error(pe, FILT_ERR_EXPECT_STRING, pos + i);
2030 goto err_free;
2031 }
2032
2033 if (op == OP_GLOB) {
2034 parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
2035 goto err_free;
2036 }
2037
2038 if (str[i] == '-')
2039 i++;
2040
2041 /* We allow 0xDEADBEEF */
2042 while (isalnum(str[i]))
2043 i++;
2044
2045 len = i - s;
2046 /* 0xfeedfacedeadbeef is 18 chars max */
2047 if (len >= sizeof(num_buf)) {
2048 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
2049 goto err_free;
2050 }
2051
2052 memcpy(num_buf, str + s, len);
2053 num_buf[len] = 0;
2054
2055 /* Make sure it is a value */
2056 if (field->is_signed)
2057 ret = kstrtoll(num_buf, 0, &val);
2058 else
2059 ret = kstrtoull(num_buf, 0, &val);
2060 if (ret) {
2061 parse_error(pe, FILT_ERR_ILLEGAL_INTVAL, pos + s);
2062 goto err_free;
2063 }
2064
2065 pred->val = val;
2066
2067 if (field->filter_type == FILTER_CPU)
2068 pred->fn_num = FILTER_PRED_FN_CPU;
2069 else {
2070 pred->fn_num = select_comparison_fn(pred->op, field->size,
2071 field->is_signed);
2072 if (pred->op == OP_NE)
2073 pred->not = 1;
2074 }
2075
2076 } else {
2077 parse_error(pe, FILT_ERR_INVALID_VALUE, pos + i);
2078 goto err_free;
2079 }
2080
2081 *pred_ptr = pred;
2082 return i;
2083
2084 err_free:
2085 free_predicate(pred);
2086 return -EINVAL;
2087 err_mem:
2088 free_predicate(pred);
2089 return -ENOMEM;
2090 }
2091
2092 enum {
2093 TOO_MANY_CLOSE = -1,
2094 TOO_MANY_OPEN = -2,
2095 MISSING_QUOTE = -3,
2096 };
2097
2098 /*
2099 * Read the filter string once to calculate the number of predicates
2100 * as well as how deep the parentheses go.
2101 *
2102 * Returns:
2103 * 0 - everything is fine (err is undefined)
2104 * -1 - too many ')'
2105 * -2 - too many '('
2106 * -3 - No matching quote
2107 */
calc_stack(const char * str,int * parens,int * preds,int * err)2108 static int calc_stack(const char *str, int *parens, int *preds, int *err)
2109 {
2110 bool is_pred = false;
2111 int nr_preds = 0;
2112 int open = 1; /* Count the expression as "(E)" */
2113 int last_quote = 0;
2114 int max_open = 1;
2115 int quote = 0;
2116 int i;
2117
2118 *err = 0;
2119
2120 for (i = 0; str[i]; i++) {
2121 if (isspace(str[i]))
2122 continue;
2123 if (quote) {
2124 if (str[i] == quote)
2125 quote = 0;
2126 continue;
2127 }
2128
2129 switch (str[i]) {
2130 case '\'':
2131 case '"':
2132 quote = str[i];
2133 last_quote = i;
2134 break;
2135 case '|':
2136 case '&':
2137 if (str[i+1] != str[i])
2138 break;
2139 is_pred = false;
2140 continue;
2141 case '(':
2142 is_pred = false;
2143 open++;
2144 if (open > max_open)
2145 max_open = open;
2146 continue;
2147 case ')':
2148 is_pred = false;
2149 if (open == 1) {
2150 *err = i;
2151 return TOO_MANY_CLOSE;
2152 }
2153 open--;
2154 continue;
2155 }
2156 if (!is_pred) {
2157 nr_preds++;
2158 is_pred = true;
2159 }
2160 }
2161
2162 if (quote) {
2163 *err = last_quote;
2164 return MISSING_QUOTE;
2165 }
2166
2167 if (open != 1) {
2168 int level = open;
2169
2170 /* find the bad open */
2171 for (i--; i; i--) {
2172 if (quote) {
2173 if (str[i] == quote)
2174 quote = 0;
2175 continue;
2176 }
2177 switch (str[i]) {
2178 case '(':
2179 if (level == open) {
2180 *err = i;
2181 return TOO_MANY_OPEN;
2182 }
2183 level--;
2184 break;
2185 case ')':
2186 level++;
2187 break;
2188 case '\'':
2189 case '"':
2190 quote = str[i];
2191 break;
2192 }
2193 }
2194 /* First character is the '(' with missing ')' */
2195 *err = 0;
2196 return TOO_MANY_OPEN;
2197 }
2198
2199 /* Set the size of the required stacks */
2200 *parens = max_open;
2201 *preds = nr_preds;
2202 return 0;
2203 }
2204
process_preds(struct trace_event_call * call,const char * filter_string,struct event_filter * filter,struct filter_parse_error * pe)2205 static int process_preds(struct trace_event_call *call,
2206 const char *filter_string,
2207 struct event_filter *filter,
2208 struct filter_parse_error *pe)
2209 {
2210 struct prog_entry *prog;
2211 int nr_parens;
2212 int nr_preds;
2213 int index;
2214 int ret;
2215
2216 ret = calc_stack(filter_string, &nr_parens, &nr_preds, &index);
2217 if (ret < 0) {
2218 switch (ret) {
2219 case MISSING_QUOTE:
2220 parse_error(pe, FILT_ERR_MISSING_QUOTE, index);
2221 break;
2222 case TOO_MANY_OPEN:
2223 parse_error(pe, FILT_ERR_TOO_MANY_OPEN, index);
2224 break;
2225 default:
2226 parse_error(pe, FILT_ERR_TOO_MANY_CLOSE, index);
2227 }
2228 return ret;
2229 }
2230
2231 if (!nr_preds)
2232 return -EINVAL;
2233
2234 prog = predicate_parse(filter_string, nr_parens, nr_preds,
2235 parse_pred, call, pe);
2236 if (IS_ERR(prog))
2237 return PTR_ERR(prog);
2238
2239 rcu_assign_pointer(filter->prog, prog);
2240 return 0;
2241 }
2242
event_set_filtered_flag(struct trace_event_file * file)2243 static inline void event_set_filtered_flag(struct trace_event_file *file)
2244 {
2245 unsigned long old_flags = file->flags;
2246
2247 file->flags |= EVENT_FILE_FL_FILTERED;
2248
2249 if (old_flags != file->flags)
2250 trace_buffered_event_enable();
2251 }
2252
process_system_preds(struct trace_subsystem_dir * dir,struct trace_array * tr,struct filter_parse_error * pe,char * filter_string)2253 static int process_system_preds(struct trace_subsystem_dir *dir,
2254 struct trace_array *tr,
2255 struct filter_parse_error *pe,
2256 char *filter_string)
2257 {
2258 struct trace_event_file *file;
2259 struct filter_list *filter_item;
2260 struct event_filter *filter = NULL;
2261 struct filter_head *filter_list;
2262 bool fail = true;
2263 int err;
2264
2265 filter_list = kmalloc_obj(*filter_list);
2266 if (!filter_list)
2267 return -ENOMEM;
2268
2269 INIT_LIST_HEAD(&filter_list->list);
2270
2271 list_for_each_entry(file, &tr->events, list) {
2272
2273 if (file->system != dir)
2274 continue;
2275
2276 filter = kzalloc_obj(*filter);
2277 if (!filter)
2278 goto fail_mem;
2279
2280 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
2281 if (!filter->filter_string)
2282 goto fail_mem;
2283
2284 err = process_preds(file->event_call, filter_string, filter, pe);
2285 if (err) {
2286 filter_disable(file);
2287 parse_error(pe, FILT_ERR_BAD_SUBSYS_FILTER, 0);
2288 append_filter_err(tr, pe, filter);
2289 } else
2290 event_set_filtered_flag(file);
2291
2292
2293 filter_item = kzalloc_obj(*filter_item);
2294 if (!filter_item)
2295 goto fail_mem;
2296
2297 list_add_tail(&filter_item->list, &filter_list->list);
2298 /*
2299 * Regardless of if this returned an error, we still
2300 * replace the filter for the call.
2301 */
2302 filter_item->filter = event_filter(file);
2303 event_set_filter(file, filter);
2304 filter = NULL;
2305
2306 fail = false;
2307 }
2308
2309 if (fail)
2310 goto fail;
2311
2312 /*
2313 * The calls can still be using the old filters.
2314 * Do a synchronize_rcu() and to ensure all calls are
2315 * done with them before we free them.
2316 */
2317 delay_free_filter(filter_list);
2318 return 0;
2319 fail:
2320 /* No call succeeded */
2321 free_filter_list(filter_list);
2322 parse_error(pe, FILT_ERR_BAD_SUBSYS_FILTER, 0);
2323 return -EINVAL;
2324 fail_mem:
2325 __free_filter(filter);
2326
2327 /* If any call succeeded, we still need to sync */
2328 if (!fail)
2329 delay_free_filter(filter_list);
2330 else
2331 free_filter_list(filter_list);
2332
2333 return -ENOMEM;
2334 }
2335
create_filter_start(char * filter_string,bool set_str,struct filter_parse_error ** pse,struct event_filter ** filterp)2336 static int create_filter_start(char *filter_string, bool set_str,
2337 struct filter_parse_error **pse,
2338 struct event_filter **filterp)
2339 {
2340 struct event_filter *filter;
2341 struct filter_parse_error *pe = NULL;
2342 int err = 0;
2343
2344 if (WARN_ON_ONCE(*pse || *filterp))
2345 return -EINVAL;
2346
2347 filter = kzalloc_obj(*filter);
2348 if (filter && set_str) {
2349 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
2350 if (!filter->filter_string)
2351 err = -ENOMEM;
2352 }
2353
2354 pe = kzalloc_obj(*pe);
2355
2356 if (!filter || !pe || err) {
2357 kfree(pe);
2358 __free_filter(filter);
2359 return -ENOMEM;
2360 }
2361
2362 /* we're committed to creating a new filter */
2363 *filterp = filter;
2364 *pse = pe;
2365
2366 return 0;
2367 }
2368
create_filter_finish(struct filter_parse_error * pe)2369 static void create_filter_finish(struct filter_parse_error *pe)
2370 {
2371 kfree(pe);
2372 }
2373
2374 /**
2375 * create_filter - create a filter for a trace_event_call
2376 * @tr: the trace array associated with these events
2377 * @call: trace_event_call to create a filter for
2378 * @filter_string: filter string
2379 * @set_str: remember @filter_str and enable detailed error in filter
2380 * @filterp: out param for created filter (always updated on return)
2381 * Must be a pointer that references a NULL pointer.
2382 *
2383 * Creates a filter for @call with @filter_str. If @set_str is %true,
2384 * @filter_str is copied and recorded in the new filter.
2385 *
2386 * On success, returns 0 and *@filterp points to the new filter. On
2387 * failure, returns -errno and *@filterp may point to %NULL or to a new
2388 * filter. In the latter case, the returned filter contains error
2389 * information if @set_str is %true and the caller is responsible for
2390 * freeing it.
2391 */
create_filter(struct trace_array * tr,struct trace_event_call * call,char * filter_string,bool set_str,struct event_filter ** filterp)2392 static int create_filter(struct trace_array *tr,
2393 struct trace_event_call *call,
2394 char *filter_string, bool set_str,
2395 struct event_filter **filterp)
2396 {
2397 struct filter_parse_error *pe = NULL;
2398 int err;
2399
2400 /* filterp must point to NULL */
2401 if (WARN_ON(*filterp))
2402 *filterp = NULL;
2403
2404 err = create_filter_start(filter_string, set_str, &pe, filterp);
2405 if (err)
2406 return err;
2407
2408 err = process_preds(call, filter_string, *filterp, pe);
2409 if (err && set_str)
2410 append_filter_err(tr, pe, *filterp);
2411 create_filter_finish(pe);
2412
2413 return err;
2414 }
2415
create_event_filter(struct trace_array * tr,struct trace_event_call * call,char * filter_str,bool set_str,struct event_filter ** filterp)2416 int create_event_filter(struct trace_array *tr,
2417 struct trace_event_call *call,
2418 char *filter_str, bool set_str,
2419 struct event_filter **filterp)
2420 {
2421 return create_filter(tr, call, filter_str, set_str, filterp);
2422 }
2423
2424 /**
2425 * create_system_filter - create a filter for an event subsystem
2426 * @dir: the descriptor for the subsystem directory
2427 * @filter_str: filter string
2428 * @filterp: out param for created filter (always updated on return)
2429 *
2430 * Identical to create_filter() except that it creates a subsystem filter
2431 * and always remembers @filter_str.
2432 */
create_system_filter(struct trace_subsystem_dir * dir,char * filter_str,struct event_filter ** filterp)2433 static int create_system_filter(struct trace_subsystem_dir *dir,
2434 char *filter_str, struct event_filter **filterp)
2435 {
2436 struct filter_parse_error *pe = NULL;
2437 int err;
2438
2439 err = create_filter_start(filter_str, true, &pe, filterp);
2440 if (!err) {
2441 err = process_system_preds(dir, dir->tr, pe, filter_str);
2442 if (!err) {
2443 /* System filters just show a default message */
2444 kfree((*filterp)->filter_string);
2445 (*filterp)->filter_string = NULL;
2446 } else {
2447 append_filter_err(dir->tr, pe, *filterp);
2448 }
2449 }
2450 create_filter_finish(pe);
2451
2452 return err;
2453 }
2454
2455 /* caller must hold event_mutex */
apply_event_filter(struct trace_event_file * file,char * filter_string)2456 int apply_event_filter(struct trace_event_file *file, char *filter_string)
2457 {
2458 struct trace_event_call *call = file->event_call;
2459 struct event_filter *filter = NULL;
2460 int err;
2461
2462 if (file->flags & EVENT_FILE_FL_FREED)
2463 return -ENODEV;
2464
2465 if (!strcmp(strstrip(filter_string), "0")) {
2466 filter_disable(file);
2467 filter = event_filter(file);
2468
2469 if (!filter)
2470 return 0;
2471
2472 event_clear_filter(file);
2473
2474 try_delay_free_filter(filter);
2475
2476 return 0;
2477 }
2478
2479 err = create_filter(file->tr, call, filter_string, true, &filter);
2480
2481 /*
2482 * Always swap the call filter with the new filter
2483 * even if there was an error. If there was an error
2484 * in the filter, we disable the filter and show the error
2485 * string
2486 */
2487 if (filter) {
2488 struct event_filter *tmp;
2489
2490 tmp = event_filter(file);
2491 if (!err)
2492 event_set_filtered_flag(file);
2493 else
2494 filter_disable(file);
2495
2496 event_set_filter(file, filter);
2497
2498 if (tmp)
2499 try_delay_free_filter(tmp);
2500 }
2501
2502 return err;
2503 }
2504
apply_subsystem_event_filter(struct trace_subsystem_dir * dir,char * filter_string)2505 int apply_subsystem_event_filter(struct trace_subsystem_dir *dir,
2506 char *filter_string)
2507 {
2508 struct event_subsystem *system = dir->subsystem;
2509 struct trace_array *tr = dir->tr;
2510 struct event_filter *filter = NULL;
2511 int err = 0;
2512
2513 guard(mutex)(&event_mutex);
2514
2515 /* Make sure the system still has events */
2516 if (!dir->nr_events)
2517 return -ENODEV;
2518
2519 if (!strcmp(strstrip(filter_string), "0")) {
2520 filter_free_subsystem_preds(dir, tr);
2521 remove_filter_string(system->filter);
2522 filter = system->filter;
2523 system->filter = NULL;
2524 /* Ensure all filters are no longer used */
2525 filter_free_subsystem_filters(dir, tr, filter);
2526 return 0;
2527 }
2528
2529 err = create_system_filter(dir, filter_string, &filter);
2530 if (filter) {
2531 /*
2532 * No event actually uses the system filter
2533 * we can free it without synchronize_rcu().
2534 */
2535 __free_filter(system->filter);
2536 system->filter = filter;
2537 }
2538
2539 return err;
2540 }
2541
2542 #ifdef CONFIG_PERF_EVENTS
2543
ftrace_profile_free_filter(struct perf_event * event)2544 void ftrace_profile_free_filter(struct perf_event *event)
2545 {
2546 struct event_filter *filter = event->filter;
2547
2548 event->filter = NULL;
2549 __free_filter(filter);
2550 }
2551
2552 struct function_filter_data {
2553 struct ftrace_ops *ops;
2554 int first_filter;
2555 int first_notrace;
2556 };
2557
2558 #ifdef CONFIG_FUNCTION_TRACER
2559 static char **
ftrace_function_filter_re(char * buf,int len,int * count)2560 ftrace_function_filter_re(char *buf, int len, int *count)
2561 {
2562 char *str, **re;
2563
2564 str = kstrndup(buf, len, GFP_KERNEL);
2565 if (!str)
2566 return NULL;
2567
2568 /*
2569 * The argv_split function takes white space
2570 * as a separator, so convert ',' into spaces.
2571 */
2572 strreplace(str, ',', ' ');
2573
2574 re = argv_split(GFP_KERNEL, str, count);
2575 kfree(str);
2576 return re;
2577 }
2578
ftrace_function_set_regexp(struct ftrace_ops * ops,int filter,int reset,char * re,int len)2579 static int ftrace_function_set_regexp(struct ftrace_ops *ops, int filter,
2580 int reset, char *re, int len)
2581 {
2582 int ret;
2583
2584 if (filter)
2585 ret = ftrace_set_filter(ops, re, len, reset);
2586 else
2587 ret = ftrace_set_notrace(ops, re, len, reset);
2588
2589 return ret;
2590 }
2591
__ftrace_function_set_filter(int filter,char * buf,int len,struct function_filter_data * data)2592 static int __ftrace_function_set_filter(int filter, char *buf, int len,
2593 struct function_filter_data *data)
2594 {
2595 int i, re_cnt, ret = -EINVAL;
2596 int *reset;
2597 char **re;
2598
2599 reset = filter ? &data->first_filter : &data->first_notrace;
2600
2601 /*
2602 * The 'ip' field could have multiple filters set, separated
2603 * either by space or comma. We first cut the filter and apply
2604 * all pieces separately.
2605 */
2606 re = ftrace_function_filter_re(buf, len, &re_cnt);
2607 if (!re)
2608 return -EINVAL;
2609
2610 for (i = 0; i < re_cnt; i++) {
2611 ret = ftrace_function_set_regexp(data->ops, filter, *reset,
2612 re[i], strlen(re[i]));
2613 if (ret)
2614 break;
2615
2616 if (*reset)
2617 *reset = 0;
2618 }
2619
2620 argv_free(re);
2621 return ret;
2622 }
2623
ftrace_function_check_pred(struct filter_pred * pred)2624 static int ftrace_function_check_pred(struct filter_pred *pred)
2625 {
2626 struct ftrace_event_field *field = pred->field;
2627
2628 /*
2629 * Check the predicate for function trace, verify:
2630 * - only '==' and '!=' is used
2631 * - the 'ip' field is used
2632 */
2633 if ((pred->op != OP_EQ) && (pred->op != OP_NE))
2634 return -EINVAL;
2635
2636 if (strcmp(field->name, "ip"))
2637 return -EINVAL;
2638
2639 return 0;
2640 }
2641
ftrace_function_set_filter_pred(struct filter_pred * pred,struct function_filter_data * data)2642 static int ftrace_function_set_filter_pred(struct filter_pred *pred,
2643 struct function_filter_data *data)
2644 {
2645 int ret;
2646
2647 /* Checking the node is valid for function trace. */
2648 ret = ftrace_function_check_pred(pred);
2649 if (ret)
2650 return ret;
2651
2652 return __ftrace_function_set_filter(pred->op == OP_EQ,
2653 pred->regex->pattern,
2654 pred->regex->len,
2655 data);
2656 }
2657
is_or(struct prog_entry * prog,int i)2658 static bool is_or(struct prog_entry *prog, int i)
2659 {
2660 int target;
2661
2662 /*
2663 * Only "||" is allowed for function events, thus,
2664 * all true branches should jump to true, and any
2665 * false branch should jump to false.
2666 */
2667 target = prog[i].target + 1;
2668 /* True and false have NULL preds (all prog entries should jump to one */
2669 if (prog[target].pred)
2670 return false;
2671
2672 /* prog[target].target is 1 for TRUE, 0 for FALSE */
2673 return prog[i].when_to_branch == prog[target].target;
2674 }
2675
ftrace_function_set_filter(struct perf_event * event,struct event_filter * filter)2676 static int ftrace_function_set_filter(struct perf_event *event,
2677 struct event_filter *filter)
2678 {
2679 struct prog_entry *prog = rcu_dereference_protected(filter->prog,
2680 lockdep_is_held(&event_mutex));
2681 struct function_filter_data data = {
2682 .first_filter = 1,
2683 .first_notrace = 1,
2684 .ops = &event->ftrace_ops,
2685 };
2686 int i;
2687
2688 for (i = 0; prog[i].pred; i++) {
2689 struct filter_pred *pred = prog[i].pred;
2690
2691 if (!is_or(prog, i))
2692 return -EINVAL;
2693
2694 if (ftrace_function_set_filter_pred(pred, &data) < 0)
2695 return -EINVAL;
2696 }
2697 return 0;
2698 }
2699 #else
ftrace_function_set_filter(struct perf_event * event,struct event_filter * filter)2700 static int ftrace_function_set_filter(struct perf_event *event,
2701 struct event_filter *filter)
2702 {
2703 return -ENODEV;
2704 }
2705 #endif /* CONFIG_FUNCTION_TRACER */
2706
ftrace_profile_set_filter(struct perf_event * event,int event_id,char * filter_str)2707 int ftrace_profile_set_filter(struct perf_event *event, int event_id,
2708 char *filter_str)
2709 {
2710 int err;
2711 struct event_filter *filter = NULL;
2712 struct trace_event_call *call;
2713
2714 guard(mutex)(&event_mutex);
2715
2716 call = event->tp_event;
2717
2718 if (!call)
2719 return -EINVAL;
2720
2721 if (event->filter)
2722 return -EEXIST;
2723
2724 err = create_filter(NULL, call, filter_str, false, &filter);
2725 if (err)
2726 goto free_filter;
2727
2728 if (ftrace_event_is_function(call))
2729 err = ftrace_function_set_filter(event, filter);
2730 else
2731 event->filter = filter;
2732
2733 free_filter:
2734 if (err || ftrace_event_is_function(call))
2735 __free_filter(filter);
2736
2737 return err;
2738 }
2739
2740 #endif /* CONFIG_PERF_EVENTS */
2741
2742 #ifdef CONFIG_FTRACE_STARTUP_TEST
2743
2744 #include <linux/types.h>
2745 #include <linux/tracepoint.h>
2746
2747 #define CREATE_TRACE_POINTS
2748 #include "trace_events_filter_test.h"
2749
2750 #define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \
2751 { \
2752 .filter = FILTER, \
2753 .rec = { .a = va, .b = vb, .c = vc, .d = vd, \
2754 .e = ve, .f = vf, .g = vg, .h = vh }, \
2755 .match = m, \
2756 .not_visited = nvisit, \
2757 }
2758 #define YES 1
2759 #define NO 0
2760
2761 static struct test_filter_data_t {
2762 char *filter;
2763 struct trace_event_raw_ftrace_test_filter rec;
2764 int match;
2765 char *not_visited;
2766 } test_filter_data[] = {
2767 #define FILTER "a == 1 && b == 1 && c == 1 && d == 1 && " \
2768 "e == 1 && f == 1 && g == 1 && h == 1"
2769 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, ""),
2770 DATA_REC(NO, 0, 1, 1, 1, 1, 1, 1, 1, "bcdefgh"),
2771 DATA_REC(NO, 1, 1, 1, 1, 1, 1, 1, 0, ""),
2772 #undef FILTER
2773 #define FILTER "a == 1 || b == 1 || c == 1 || d == 1 || " \
2774 "e == 1 || f == 1 || g == 1 || h == 1"
2775 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2776 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2777 DATA_REC(YES, 1, 0, 0, 0, 0, 0, 0, 0, "bcdefgh"),
2778 #undef FILTER
2779 #define FILTER "(a == 1 || b == 1) && (c == 1 || d == 1) && " \
2780 "(e == 1 || f == 1) && (g == 1 || h == 1)"
2781 DATA_REC(NO, 0, 0, 1, 1, 1, 1, 1, 1, "dfh"),
2782 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2783 DATA_REC(YES, 1, 0, 1, 0, 0, 1, 0, 1, "bd"),
2784 DATA_REC(NO, 1, 0, 1, 0, 0, 1, 0, 0, "bd"),
2785 #undef FILTER
2786 #define FILTER "(a == 1 && b == 1) || (c == 1 && d == 1) || " \
2787 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2788 DATA_REC(YES, 1, 0, 1, 1, 1, 1, 1, 1, "efgh"),
2789 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 1, 1, ""),
2790 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2791 #undef FILTER
2792 #define FILTER "(a == 1 && b == 1) && (c == 1 && d == 1) && " \
2793 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2794 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 0, "gh"),
2795 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2796 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, ""),
2797 #undef FILTER
2798 #define FILTER "((a == 1 || b == 1) || (c == 1 || d == 1) || " \
2799 "(e == 1 || f == 1)) && (g == 1 || h == 1)"
2800 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 1, "bcdef"),
2801 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2802 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, "h"),
2803 #undef FILTER
2804 #define FILTER "((((((((a == 1) && (b == 1)) || (c == 1)) && (d == 1)) || " \
2805 "(e == 1)) && (f == 1)) || (g == 1)) && (h == 1))"
2806 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "ceg"),
2807 DATA_REC(NO, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2808 DATA_REC(NO, 1, 0, 1, 0, 1, 0, 1, 0, ""),
2809 #undef FILTER
2810 #define FILTER "((((((((a == 1) || (b == 1)) && (c == 1)) || (d == 1)) && " \
2811 "(e == 1)) || (f == 1)) && (g == 1)) || (h == 1))"
2812 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "bdfh"),
2813 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2814 DATA_REC(YES, 1, 0, 1, 0, 1, 0, 1, 0, "bdfh"),
2815 };
2816
2817 #undef DATA_REC
2818 #undef FILTER
2819 #undef YES
2820 #undef NO
2821
2822 #define DATA_CNT ARRAY_SIZE(test_filter_data)
2823
2824 static int test_pred_visited;
2825
test_pred_visited_fn(struct filter_pred * pred,void * event)2826 static int test_pred_visited_fn(struct filter_pred *pred, void *event)
2827 {
2828 struct ftrace_event_field *field = pred->field;
2829
2830 test_pred_visited = 1;
2831 printk(KERN_INFO "\npred visited %s\n", field->name);
2832 return 1;
2833 }
2834
update_pred_fn(struct event_filter * filter,char * fields)2835 static void update_pred_fn(struct event_filter *filter, char *fields)
2836 {
2837 struct prog_entry *prog = rcu_dereference_protected(filter->prog,
2838 lockdep_is_held(&event_mutex));
2839 int i;
2840
2841 for (i = 0; prog[i].pred; i++) {
2842 struct filter_pred *pred = prog[i].pred;
2843 struct ftrace_event_field *field = pred->field;
2844
2845 WARN_ON_ONCE(pred->fn_num == FILTER_PRED_FN_NOP);
2846
2847 if (!field) {
2848 WARN_ONCE(1, "all leafs should have field defined %d", i);
2849 continue;
2850 }
2851
2852 if (!strchr(fields, *field->name))
2853 continue;
2854
2855 pred->fn_num = FILTER_PRED_TEST_VISITED;
2856 }
2857 }
2858
ftrace_test_event_filter(void)2859 static __init int ftrace_test_event_filter(void)
2860 {
2861 int i;
2862
2863 printk(KERN_INFO "Testing ftrace filter: ");
2864
2865 for (i = 0; i < DATA_CNT; i++) {
2866 struct event_filter *filter = NULL;
2867 struct test_filter_data_t *d = &test_filter_data[i];
2868 int err;
2869
2870 err = create_filter(NULL, &event_ftrace_test_filter,
2871 d->filter, false, &filter);
2872 if (err) {
2873 printk(KERN_INFO
2874 "Failed to get filter for '%s', err %d\n",
2875 d->filter, err);
2876 __free_filter(filter);
2877 break;
2878 }
2879
2880 /* Needed to dereference filter->prog */
2881 mutex_lock(&event_mutex);
2882 /*
2883 * The preemption disabling is not really needed for self
2884 * tests, but the rcu dereference will complain without it.
2885 */
2886 preempt_disable();
2887 if (*d->not_visited)
2888 update_pred_fn(filter, d->not_visited);
2889
2890 test_pred_visited = 0;
2891 err = filter_match_preds(filter, &d->rec);
2892 preempt_enable();
2893
2894 mutex_unlock(&event_mutex);
2895
2896 __free_filter(filter);
2897
2898 if (test_pred_visited) {
2899 printk(KERN_INFO
2900 "Failed, unwanted pred visited for filter %s\n",
2901 d->filter);
2902 break;
2903 }
2904
2905 if (err != d->match) {
2906 printk(KERN_INFO
2907 "Failed to match filter '%s', expected %d\n",
2908 d->filter, d->match);
2909 break;
2910 }
2911 }
2912
2913 if (i == DATA_CNT)
2914 printk(KERN_CONT "OK\n");
2915
2916 /* Need to call ftrace_test_filter to prevent a warning */
2917 if (!trace_ftrace_test_filter_enabled())
2918 trace_ftrace_test_filter(1, 2, 3, 4, 5, 6, 7, 8);
2919
2920 return 0;
2921 }
2922
2923 late_initcall(ftrace_test_event_filter);
2924
2925 #endif /* CONFIG_FTRACE_STARTUP_TEST */
2926