1 // SPDX-License-Identifier: GPL-2.0
3 * trace_events_filter - generic event filtering
5 * Copyright (C) 2009 Tom Zanussi <tzanussi@gmail.com>
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>
16 #include "trace_output.h"
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"
24 /* Due to token parsing '<=' must be before '<' and '>=' must be before '>' */
39 enum filter_op_ids { OPS };
44 static const char * ops[] = { OPS };
49 FILTER_PRED_FN_64_CPUMASK,
53 FILTER_PRED_FN_32_CPUMASK,
57 FILTER_PRED_FN_16_CPUMASK,
61 FILTER_PRED_FN_8_CPUMASK,
65 FILTER_PRED_FN_STRING,
66 FILTER_PRED_FN_STRLOC,
67 FILTER_PRED_FN_STRRELLOC,
68 FILTER_PRED_FN_PCHAR_USER,
71 FILTER_PRED_FN_CPU_CPUMASK,
72 FILTER_PRED_FN_CPUMASK,
73 FILTER_PRED_FN_CPUMASK_CPU,
74 FILTER_PRED_FN_FUNCTION,
76 FILTER_PRED_TEST_VISITED,
83 struct ftrace_event_field *field;
86 enum filter_pred_fn fn_num;
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.
96 #define PRED_FUNC_START OP_LE
97 #define PRED_FUNC_MAX (OP_BAND - PRED_FUNC_START)
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"), \
121 C(NO_FILTER, "No filter found")
124 #define C(a, b) FILT_ERR_##a
131 static const char *err_text[] = { ERRORS };
133 /* Called after a '!' character but "!=" and "!~" are not "not"s */
134 static bool is_not(const char *str)
145 * struct prog_entry - a singe 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.
153 struct filter_pred *pred;
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
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 "&&".
168 static void update_preds(struct prog_entry *prog, int N, int invert)
174 prog[t].when_to_branch = invert;
179 struct filter_parse_error {
184 static void parse_error(struct filter_parse_error *pe, int err, int pos)
187 pe->lasterr_pos = pos;
190 typedef int (*parse_pred_fn)(const char *str, void *data, int pos,
191 struct filter_parse_error *pe,
192 struct filter_pred **pred);
200 static void free_predicate(struct filter_pred *pred)
210 * Without going into a formal proof, this explains the method that is used in
211 * parsing the logical expressions.
213 * For example, if we have: "a && !(!b || (c && g)) || d || e && !f"
214 * The first pass will convert it into the following program:
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
226 * To do this, we use a data structure to represent each of the above
227 * predicate and conditions that has:
229 * predicate, when_to_branch, invert, target
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.
237 * A stack is created to hold values when parentheses are used.
239 * To simplify the logic, the labels will start at 0 and not 1.
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.
247 * The top of the stack and "invert" are initialized to zero.
251 * #1 A loop through all the tokens is done:
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"
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.
265 * The only other acceptable token is the predicate string.
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).
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".
274 * #6 The invert variable is reset to the current value saved on the top of
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.
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.
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
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 "&&".
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.
303 * #11 If this is the end of the input string "\0" then we break out of both
306 * #12 Otherwise, the next token is ")", where we pop the stack and continue
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 "||".
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
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;
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).
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
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 }
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:
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
374 * n0: r = a; if (r) goto T
375 * n1: r = b; if (!r) goto F
376 * n2: r = c; if (!r) goto F
380 * The First Pass is over the input string. The next too passes are over
381 * the program itself.
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:
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:
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:
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;
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;".
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
433 * Notice, all the "l#" labels are no longer used, and they can now
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
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
455 * n1: if (!a) goto n5;
456 * n2: if (!b) goto n5;
457 * n3: if (!c) goto T;
458 * n4: if (!g) goto T;
460 * n6: if (!e) goto F;
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.
469 static struct prog_entry *
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)
474 struct prog_entry *prog_stack;
475 struct prog_entry *prog;
476 const char *ptr = str;
477 char *inverts = NULL;
486 nr_preds += 2; /* For TRUE and FALSE */
488 op_stack = kmalloc_array(nr_parens, sizeof(*op_stack), GFP_KERNEL);
490 return ERR_PTR(-ENOMEM);
491 prog_stack = kcalloc(nr_preds, sizeof(*prog_stack), GFP_KERNEL);
493 parse_error(pe, -ENOMEM, 0);
496 inverts = kmalloc_array(nr_preds, sizeof(*inverts), GFP_KERNEL);
498 parse_error(pe, -ENOMEM, 0);
507 while (*ptr) { /* #1 */
508 const char *next = ptr++;
515 if (top - op_stack > nr_parens) {
529 parse_error(pe, FILT_ERR_TOO_MANY_PREDS, next - str);
533 inverts[N] = invert; /* #4 */
534 prog[N].target = N-1;
536 len = parse_pred(next, data, ptr - str, pe, &prog[N].pred);
557 /* accepting only "&&" or "||" */
558 if (next[1] == next[0]) {
564 parse_error(pe, FILT_ERR_TOO_MANY_PREDS,
569 invert = *top & INVERT;
571 if (*top & PROCESS_AND) { /* #7 */
572 update_preds(prog, N - 1, invert);
573 *top &= ~PROCESS_AND;
575 if (*next == '&') { /* #8 */
579 if (*top & PROCESS_OR) { /* #9 */
580 update_preds(prog, N - 1, !invert);
583 if (*next == '|') { /* #10 */
587 if (!*next) /* #11 */
590 if (top == op_stack) {
593 parse_error(pe, FILT_ERR_TOO_MANY_CLOSE, ptr - str);
600 if (top != op_stack) {
602 parse_error(pe, FILT_ERR_TOO_MANY_OPEN, ptr - str);
609 parse_error(pe, FILT_ERR_NO_FILTER, ptr - str);
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;
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;
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)) {
645 for (i = 0; prog_stack[i].pred; i++)
646 free_predicate(prog_stack[i].pred);
653 do_filter_cpumask(int op, const struct cpumask *mask, const struct cpumask *cmp)
657 return cpumask_equal(mask, cmp);
659 return !cpumask_equal(mask, cmp);
661 return cpumask_intersects(mask, cmp);
667 /* Optimisation of do_filter_cpumask() for scalar fields */
669 do_filter_scalar_cpumask(int op, unsigned int cpu, const struct cpumask *mask)
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.
681 return cpumask_test_cpu(cpu, mask);
688 do_filter_cpumask_scalar(int op, const struct cpumask *mask, unsigned int cpu)
692 return cpumask_test_cpu(cpu, mask) &&
693 cpumask_nth(1, mask) >= nr_cpu_ids;
695 return !cpumask_test_cpu(cpu, mask) ||
696 cpumask_nth(1, mask) < nr_cpu_ids;
698 return cpumask_test_cpu(cpu, mask);
704 enum pred_cmp_types {
713 #define DEFINE_COMPARISON_PRED(type) \
714 static int filter_pred_##type(struct filter_pred *pred, void *event) \
716 switch (pred->op) { \
718 type *addr = (type *)(event + pred->offset); \
719 type val = (type)pred->val; \
720 return *addr < val; \
723 type *addr = (type *)(event + pred->offset); \
724 type val = (type)pred->val; \
725 return *addr <= val; \
728 type *addr = (type *)(event + pred->offset); \
729 type val = (type)pred->val; \
730 return *addr > val; \
733 type *addr = (type *)(event + pred->offset); \
734 type val = (type)pred->val; \
735 return *addr >= val; \
738 type *addr = (type *)(event + pred->offset); \
739 type val = (type)pred->val; \
740 return !!(*addr & val); \
747 #define DEFINE_CPUMASK_COMPARISON_PRED(size) \
748 static int filter_pred_##size##_cpumask(struct filter_pred *pred, void *event) \
750 u##size *addr = (u##size *)(event + pred->offset); \
751 unsigned int cpu = *addr; \
753 if (cpu >= nr_cpu_ids) \
756 return do_filter_scalar_cpumask(pred->op, cpu, pred->mask); \
759 #define DEFINE_EQUALITY_PRED(size) \
760 static int filter_pred_##size(struct filter_pred *pred, void *event) \
762 u##size *addr = (u##size *)(event + pred->offset); \
763 u##size val = (u##size)pred->val; \
766 match = (val == *addr) ^ pred->not; \
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);
780 DEFINE_CPUMASK_COMPARISON_PRED(64);
781 DEFINE_CPUMASK_COMPARISON_PRED(32);
782 DEFINE_CPUMASK_COMPARISON_PRED(16);
783 DEFINE_CPUMASK_COMPARISON_PRED(8);
785 DEFINE_EQUALITY_PRED(64);
786 DEFINE_EQUALITY_PRED(32);
787 DEFINE_EQUALITY_PRED(16);
788 DEFINE_EQUALITY_PRED(8);
790 /* user space strings temp buffer */
791 #define USTRING_BUF_SIZE 1024
793 struct ustring_buffer {
794 char buffer[USTRING_BUF_SIZE];
797 static __percpu struct ustring_buffer *ustring_per_cpu;
799 static __always_inline char *test_string(char *str)
801 struct ustring_buffer *ubuf;
804 if (!ustring_per_cpu)
807 ubuf = this_cpu_ptr(ustring_per_cpu);
810 /* For safety, do not trust the string pointer */
811 if (!strncpy_from_kernel_nofault(kstr, str, USTRING_BUF_SIZE))
816 static __always_inline char *test_ustring(char *str)
818 struct ustring_buffer *ubuf;
822 if (!ustring_per_cpu)
825 ubuf = this_cpu_ptr(ustring_per_cpu);
828 /* user space address? */
829 ustr = (char __user *)str;
830 if (!strncpy_from_user_nofault(kstr, ustr, USTRING_BUF_SIZE))
836 /* Filter predicate for fixed sized arrays of characters */
837 static int filter_pred_string(struct filter_pred *pred, void *event)
839 char *addr = (char *)(event + pred->offset);
842 cmp = pred->regex->match(addr, pred->regex, pred->regex->field_len);
844 match = cmp ^ pred->not;
849 static __always_inline int filter_pchar(struct filter_pred *pred, char *str)
854 len = strlen(str) + 1; /* including tailing '\0' */
855 cmp = pred->regex->match(str, pred->regex, len);
857 match = cmp ^ pred->not;
861 /* Filter predicate for char * pointers */
862 static int filter_pred_pchar(struct filter_pred *pred, void *event)
864 char **addr = (char **)(event + pred->offset);
867 str = test_string(*addr);
871 return filter_pchar(pred, str);
874 /* Filter predicate for char * pointers in user space*/
875 static int filter_pred_pchar_user(struct filter_pred *pred, void *event)
877 char **addr = (char **)(event + pred->offset);
880 str = test_ustring(*addr);
884 return filter_pchar(pred, str);
888 * Filter predicate for dynamic sized arrays of characters.
889 * These are implemented through a list of strings at the end
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.
897 static int filter_pred_strloc(struct filter_pred *pred, void *event)
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);
905 cmp = pred->regex->match(addr, pred->regex, str_len);
907 match = cmp ^ pred->not;
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.
919 static int filter_pred_strrelloc(struct filter_pred *pred, void *event)
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;
928 cmp = pred->regex->match(addr, pred->regex, str_len);
930 match = cmp ^ pred->not;
935 /* Filter predicate for CPUs. */
936 static int filter_pred_cpu(struct filter_pred *pred, void *event)
940 cpu = raw_smp_processor_id();
961 /* Filter predicate for current CPU vs user-provided cpumask */
962 static int filter_pred_cpu_cpumask(struct filter_pred *pred, void *event)
964 int cpu = raw_smp_processor_id();
966 return do_filter_scalar_cpumask(pred->op, cpu, pred->mask);
969 /* Filter predicate for cpumask field vs user-provided cpumask */
970 static int filter_pred_cpumask(struct filter_pred *pred, void *event)
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;
977 return do_filter_cpumask(pred->op, mask, cmp);
980 /* Filter predicate for cpumask field vs user-provided scalar */
981 static int filter_pred_cpumask_cpu(struct filter_pred *pred, void *event)
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;
988 return do_filter_cpumask_scalar(pred->op, mask, cpu);
991 /* Filter predicate for COMM. */
992 static int filter_pred_comm(struct filter_pred *pred, void *event)
996 cmp = pred->regex->match(current->comm, pred->regex,
998 return cmp ^ pred->not;
1001 /* Filter predicate for functions. */
1002 static int filter_pred_function(struct filter_pred *pred, void *event)
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;
1009 return pred->op == OP_EQ ? ret : !ret;
1013 * regex_match_foo - Basic regex callbacks
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')
1020 * - @str might not be NULL-terminated if it's of type DYN_STRING
1021 * RDYN_STRING, or STATIC_STRING, unless @len is zero.
1024 static int regex_match_full(char *str, struct regex *r, int len)
1026 /* len of zero means str is dynamic and ends with '\0' */
1028 return strcmp(str, r->pattern) == 0;
1030 return strncmp(str, r->pattern, len) == 0;
1033 static int regex_match_front(char *str, struct regex *r, int len)
1035 if (len && len < r->len)
1038 return strncmp(str, r->pattern, r->len) == 0;
1041 static int regex_match_middle(char *str, struct regex *r, int len)
1044 return strstr(str, r->pattern) != NULL;
1046 return strnstr(str, r->pattern, len) != NULL;
1049 static int regex_match_end(char *str, struct regex *r, int len)
1051 int strlen = len - 1;
1053 if (strlen >= r->len &&
1054 memcmp(str + strlen - r->len, r->pattern, r->len) == 0)
1059 static int regex_match_glob(char *str, struct regex *r, int len __maybe_unused)
1061 if (glob_match(r->pattern, str))
1067 * filter_parse_regex - parse a basic regex
1068 * @buff: the raw regex
1069 * @len: length of the regex
1070 * @search: will point to the beginning of the string to compare
1071 * @not: tell whether the match will have to be inverted
1073 * This passes in a buffer containing a regex and this function will
1074 * set search to point to the search part of the buffer and
1075 * return the type of search it is (see enum above).
1076 * This does modify buff.
1078 * Returns enum type.
1079 * search returns the pointer to use for comparison.
1080 * not returns 1 if buff started with a '!'
1083 enum regex_type filter_parse_regex(char *buff, int len, char **search, int *not)
1085 int type = MATCH_FULL;
1088 if (buff[0] == '!') {
1097 if (isdigit(buff[0]))
1100 for (i = 0; i < len; i++) {
1101 if (buff[i] == '*') {
1103 type = MATCH_END_ONLY;
1104 } else if (i == len - 1) {
1105 if (type == MATCH_END_ONLY)
1106 type = MATCH_MIDDLE_ONLY;
1108 type = MATCH_FRONT_ONLY;
1111 } else { /* pattern continues, use full glob */
1114 } else if (strchr("[?\\", buff[i])) {
1124 static void filter_build_regex(struct filter_pred *pred)
1126 struct regex *r = pred->regex;
1128 enum regex_type type = MATCH_FULL;
1130 if (pred->op == OP_GLOB) {
1131 type = filter_parse_regex(r->pattern, r->len, &search, &pred->not);
1132 r->len = strlen(search);
1133 memmove(r->pattern, search, r->len+1);
1137 /* MATCH_INDEX should not happen, but if it does, match full */
1140 r->match = regex_match_full;
1142 case MATCH_FRONT_ONLY:
1143 r->match = regex_match_front;
1145 case MATCH_MIDDLE_ONLY:
1146 r->match = regex_match_middle;
1148 case MATCH_END_ONLY:
1149 r->match = regex_match_end;
1152 r->match = regex_match_glob;
1158 #ifdef CONFIG_FTRACE_STARTUP_TEST
1159 static int test_pred_visited_fn(struct filter_pred *pred, void *event);
1161 static int test_pred_visited_fn(struct filter_pred *pred, void *event)
1168 static int filter_pred_fn_call(struct filter_pred *pred, void *event);
1170 /* return 1 if event matches, 0 otherwise (discard) */
1171 int filter_match_preds(struct event_filter *filter, void *rec)
1173 struct prog_entry *prog;
1176 /* no filter is considered a match */
1180 /* Protected by either SRCU(tracepoint_srcu) or preempt_disable */
1181 prog = rcu_dereference_raw(filter->prog);
1185 for (i = 0; prog[i].pred; i++) {
1186 struct filter_pred *pred = prog[i].pred;
1187 int match = filter_pred_fn_call(pred, rec);
1188 if (match == prog[i].when_to_branch)
1191 return prog[i].target;
1193 EXPORT_SYMBOL_GPL(filter_match_preds);
1195 static void remove_filter_string(struct event_filter *filter)
1200 kfree(filter->filter_string);
1201 filter->filter_string = NULL;
1204 static void append_filter_err(struct trace_array *tr,
1205 struct filter_parse_error *pe,
1206 struct event_filter *filter)
1208 struct trace_seq *s;
1209 int pos = pe->lasterr_pos;
1213 if (WARN_ON(!filter->filter_string))
1216 s = kmalloc(sizeof(*s), GFP_KERNEL);
1221 len = strlen(filter->filter_string);
1225 /* indexing is off by one */
1229 trace_seq_puts(s, filter->filter_string);
1230 if (pe->lasterr > 0) {
1231 trace_seq_printf(s, "\n%*s", pos, "^");
1232 trace_seq_printf(s, "\nparse_error: %s\n", err_text[pe->lasterr]);
1233 tracing_log_err(tr, "event filter parse error",
1234 filter->filter_string, err_text,
1235 pe->lasterr, pe->lasterr_pos);
1237 trace_seq_printf(s, "\nError: (%d)\n", pe->lasterr);
1238 tracing_log_err(tr, "event filter parse error",
1239 filter->filter_string, err_text,
1242 trace_seq_putc(s, 0);
1243 buf = kmemdup_nul(s->buffer, s->seq.len, GFP_KERNEL);
1245 kfree(filter->filter_string);
1246 filter->filter_string = buf;
1251 static inline struct event_filter *event_filter(struct trace_event_file *file)
1253 return file->filter;
1256 /* caller must hold event_mutex */
1257 void print_event_filter(struct trace_event_file *file, struct trace_seq *s)
1259 struct event_filter *filter = event_filter(file);
1261 if (filter && filter->filter_string)
1262 trace_seq_printf(s, "%s\n", filter->filter_string);
1264 trace_seq_puts(s, "none\n");
1267 void print_subsystem_event_filter(struct event_subsystem *system,
1268 struct trace_seq *s)
1270 struct event_filter *filter;
1272 mutex_lock(&event_mutex);
1273 filter = system->filter;
1274 if (filter && filter->filter_string)
1275 trace_seq_printf(s, "%s\n", filter->filter_string);
1277 trace_seq_puts(s, DEFAULT_SYS_FILTER_MESSAGE "\n");
1278 mutex_unlock(&event_mutex);
1281 static void free_prog(struct event_filter *filter)
1283 struct prog_entry *prog;
1286 prog = rcu_access_pointer(filter->prog);
1290 for (i = 0; prog[i].pred; i++)
1291 free_predicate(prog[i].pred);
1295 static void filter_disable(struct trace_event_file *file)
1297 unsigned long old_flags = file->flags;
1299 file->flags &= ~EVENT_FILE_FL_FILTERED;
1301 if (old_flags != file->flags)
1302 trace_buffered_event_disable();
1305 static void __free_filter(struct event_filter *filter)
1311 kfree(filter->filter_string);
1315 void free_event_filter(struct event_filter *filter)
1317 __free_filter(filter);
1320 static inline void __remove_filter(struct trace_event_file *file)
1322 filter_disable(file);
1323 remove_filter_string(file->filter);
1326 static void filter_free_subsystem_preds(struct trace_subsystem_dir *dir,
1327 struct trace_array *tr)
1329 struct trace_event_file *file;
1331 list_for_each_entry(file, &tr->events, list) {
1332 if (file->system != dir)
1334 __remove_filter(file);
1338 static inline void __free_subsystem_filter(struct trace_event_file *file)
1340 __free_filter(file->filter);
1341 file->filter = NULL;
1344 static void filter_free_subsystem_filters(struct trace_subsystem_dir *dir,
1345 struct trace_array *tr)
1347 struct trace_event_file *file;
1349 list_for_each_entry(file, &tr->events, list) {
1350 if (file->system != dir)
1352 __free_subsystem_filter(file);
1356 int filter_assign_type(const char *type)
1358 if (strstr(type, "__data_loc")) {
1359 if (strstr(type, "char"))
1360 return FILTER_DYN_STRING;
1361 if (strstr(type, "cpumask_t"))
1362 return FILTER_CPUMASK;
1365 if (strstr(type, "__rel_loc") && strstr(type, "char"))
1366 return FILTER_RDYN_STRING;
1368 if (strchr(type, '[') && strstr(type, "char"))
1369 return FILTER_STATIC_STRING;
1371 if (strcmp(type, "char *") == 0 || strcmp(type, "const char *") == 0)
1372 return FILTER_PTR_STRING;
1374 return FILTER_OTHER;
1377 static enum filter_pred_fn select_comparison_fn(enum filter_op_ids op,
1378 int field_size, int field_is_signed)
1380 enum filter_pred_fn fn = FILTER_PRED_FN_NOP;
1381 int pred_func_index = -1;
1388 if (WARN_ON_ONCE(op < PRED_FUNC_START))
1390 pred_func_index = op - PRED_FUNC_START;
1391 if (WARN_ON_ONCE(pred_func_index > PRED_FUNC_MAX))
1395 switch (field_size) {
1397 if (pred_func_index < 0)
1398 fn = FILTER_PRED_FN_64;
1399 else if (field_is_signed)
1400 fn = FILTER_PRED_FN_S64;
1402 fn = FILTER_PRED_FN_U64;
1405 if (pred_func_index < 0)
1406 fn = FILTER_PRED_FN_32;
1407 else if (field_is_signed)
1408 fn = FILTER_PRED_FN_S32;
1410 fn = FILTER_PRED_FN_U32;
1413 if (pred_func_index < 0)
1414 fn = FILTER_PRED_FN_16;
1415 else if (field_is_signed)
1416 fn = FILTER_PRED_FN_S16;
1418 fn = FILTER_PRED_FN_U16;
1421 if (pred_func_index < 0)
1422 fn = FILTER_PRED_FN_8;
1423 else if (field_is_signed)
1424 fn = FILTER_PRED_FN_S8;
1426 fn = FILTER_PRED_FN_U8;
1434 static int filter_pred_fn_call(struct filter_pred *pred, void *event)
1436 switch (pred->fn_num) {
1437 case FILTER_PRED_FN_64:
1438 return filter_pred_64(pred, event);
1439 case FILTER_PRED_FN_64_CPUMASK:
1440 return filter_pred_64_cpumask(pred, event);
1441 case FILTER_PRED_FN_S64:
1442 return filter_pred_s64(pred, event);
1443 case FILTER_PRED_FN_U64:
1444 return filter_pred_u64(pred, event);
1445 case FILTER_PRED_FN_32:
1446 return filter_pred_32(pred, event);
1447 case FILTER_PRED_FN_32_CPUMASK:
1448 return filter_pred_32_cpumask(pred, event);
1449 case FILTER_PRED_FN_S32:
1450 return filter_pred_s32(pred, event);
1451 case FILTER_PRED_FN_U32:
1452 return filter_pred_u32(pred, event);
1453 case FILTER_PRED_FN_16:
1454 return filter_pred_16(pred, event);
1455 case FILTER_PRED_FN_16_CPUMASK:
1456 return filter_pred_16_cpumask(pred, event);
1457 case FILTER_PRED_FN_S16:
1458 return filter_pred_s16(pred, event);
1459 case FILTER_PRED_FN_U16:
1460 return filter_pred_u16(pred, event);
1461 case FILTER_PRED_FN_8:
1462 return filter_pred_8(pred, event);
1463 case FILTER_PRED_FN_8_CPUMASK:
1464 return filter_pred_8_cpumask(pred, event);
1465 case FILTER_PRED_FN_S8:
1466 return filter_pred_s8(pred, event);
1467 case FILTER_PRED_FN_U8:
1468 return filter_pred_u8(pred, event);
1469 case FILTER_PRED_FN_COMM:
1470 return filter_pred_comm(pred, event);
1471 case FILTER_PRED_FN_STRING:
1472 return filter_pred_string(pred, event);
1473 case FILTER_PRED_FN_STRLOC:
1474 return filter_pred_strloc(pred, event);
1475 case FILTER_PRED_FN_STRRELLOC:
1476 return filter_pred_strrelloc(pred, event);
1477 case FILTER_PRED_FN_PCHAR_USER:
1478 return filter_pred_pchar_user(pred, event);
1479 case FILTER_PRED_FN_PCHAR:
1480 return filter_pred_pchar(pred, event);
1481 case FILTER_PRED_FN_CPU:
1482 return filter_pred_cpu(pred, event);
1483 case FILTER_PRED_FN_CPU_CPUMASK:
1484 return filter_pred_cpu_cpumask(pred, event);
1485 case FILTER_PRED_FN_CPUMASK:
1486 return filter_pred_cpumask(pred, event);
1487 case FILTER_PRED_FN_CPUMASK_CPU:
1488 return filter_pred_cpumask_cpu(pred, event);
1489 case FILTER_PRED_FN_FUNCTION:
1490 return filter_pred_function(pred, event);
1491 case FILTER_PRED_TEST_VISITED:
1492 return test_pred_visited_fn(pred, event);
1498 /* Called when a predicate is encountered by predicate_parse() */
1499 static int parse_pred(const char *str, void *data,
1500 int pos, struct filter_parse_error *pe,
1501 struct filter_pred **pred_ptr)
1503 struct trace_event_call *call = data;
1504 struct ftrace_event_field *field;
1505 struct filter_pred *pred = NULL;
1506 unsigned long offset;
1509 char num_buf[24]; /* Big enough to hold an address */
1512 bool function = false;
1513 bool ustring = false;
1522 /* First find the field to associate to */
1523 while (isspace(str[i]))
1527 while (isalnum(str[i]) || str[i] == '_')
1535 field_name = kmemdup_nul(str + s, len, GFP_KERNEL);
1539 /* Make sure that the field exists */
1541 field = trace_find_event_field(call, field_name);
1544 parse_error(pe, FILT_ERR_FIELD_NOT_FOUND, pos + i);
1548 /* See if the field is a user space string */
1549 if ((len = str_has_prefix(str + i, ".ustring"))) {
1554 /* See if the field is a kernel function name */
1555 if ((len = str_has_prefix(str + i, ".function"))) {
1560 while (isspace(str[i]))
1563 /* Make sure this op is supported */
1564 for (op = 0; ops[op]; op++) {
1565 /* This is why '<=' must come before '<' in ops[] */
1566 if (strncmp(str + i, ops[op], strlen(ops[op])) == 0)
1571 parse_error(pe, FILT_ERR_INVALID_OP, pos + i);
1575 i += strlen(ops[op]);
1577 while (isspace(str[i]))
1582 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
1586 pred->field = field;
1587 pred->offset = field->offset;
1591 /* The field must be the same size as long */
1592 if (field->size != sizeof(long)) {
1593 parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
1597 /* Function only works with '==' or '!=' and an unquoted string */
1603 parse_error(pe, FILT_ERR_INVALID_OP, pos + i);
1607 if (isdigit(str[i])) {
1608 /* We allow 0xDEADBEEF */
1609 while (isalnum(str[i]))
1613 /* 0xfeedfacedeadbeef is 18 chars max */
1614 if (len >= sizeof(num_buf)) {
1615 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1619 strncpy(num_buf, str + s, len);
1622 ret = kstrtoul(num_buf, 0, &ip);
1624 parse_error(pe, FILT_ERR_INVALID_VALUE, pos + i);
1629 for (; str[i] && !isspace(str[i]); i++)
1633 name = kmemdup_nul(str + s, len, GFP_KERNEL);
1636 ip = kallsyms_lookup_name(name);
1639 parse_error(pe, FILT_ERR_NO_FUNCTION, pos + i);
1644 /* Now find the function start and end address */
1645 if (!kallsyms_lookup_size_offset(ip, &size, &offset)) {
1646 parse_error(pe, FILT_ERR_NO_FUNCTION, pos + i);
1650 pred->fn_num = FILTER_PRED_FN_FUNCTION;
1651 pred->val = ip - offset;
1652 pred->val2 = pred->val + size;
1654 } else if (ftrace_event_is_function(call)) {
1656 * Perf does things different with function events.
1657 * It only allows an "ip" field, and expects a string.
1658 * But the string does not need to be surrounded by quotes.
1659 * If it is a string, the assigned function as a nop,
1660 * (perf doesn't use it) and grab everything.
1662 if (strcmp(field->name, "ip") != 0) {
1663 parse_error(pe, FILT_ERR_IP_FIELD_ONLY, pos + i);
1666 pred->fn_num = FILTER_PRED_FN_NOP;
1669 * Quotes are not required, but if they exist then we need
1670 * to read them till we hit a matching one.
1672 if (str[i] == '\'' || str[i] == '"')
1677 for (i++; str[i]; i++) {
1678 if (q && str[i] == q)
1680 if (!q && (str[i] == ')' || str[i] == '&' ||
1688 if (len >= MAX_FILTER_STR_VAL) {
1689 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1693 pred->regex = kzalloc(sizeof(*pred->regex), GFP_KERNEL);
1696 pred->regex->len = len;
1697 strncpy(pred->regex->pattern, str + s, len);
1698 pred->regex->pattern[len] = 0;
1700 } else if (!strncmp(str + i, "CPUS", 4)) {
1701 unsigned int maskstart;
1705 switch (field->filter_type) {
1706 case FILTER_CPUMASK:
1711 parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
1721 parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
1727 if (str[i++] != '{') {
1728 parse_error(pe, FILT_ERR_MISSING_BRACE_OPEN, pos + i);
1733 /* Walk the cpulist until closing } */
1734 for (; str[i] && str[i] != '}'; i++)
1737 if (str[i] != '}') {
1738 parse_error(pe, FILT_ERR_MISSING_BRACE_CLOSE, pos + i);
1742 if (maskstart == i) {
1743 parse_error(pe, FILT_ERR_INVALID_CPULIST, pos + i);
1747 /* Copy the cpulist between { and } */
1748 tmp = kmalloc((i - maskstart) + 1, GFP_KERNEL);
1752 strscpy(tmp, str + maskstart, (i - maskstart) + 1);
1753 pred->mask = kzalloc(cpumask_size(), GFP_KERNEL);
1760 if (cpulist_parse(tmp, pred->mask)) {
1762 parse_error(pe, FILT_ERR_INVALID_CPULIST, pos + i);
1771 * Optimisation: if the user-provided mask has a weight of one
1772 * then we can treat it as a scalar input.
1774 single = cpumask_weight(pred->mask) == 1;
1776 pred->val = cpumask_first(pred->mask);
1781 if (field->filter_type == FILTER_CPUMASK) {
1782 pred->fn_num = single ?
1783 FILTER_PRED_FN_CPUMASK_CPU :
1784 FILTER_PRED_FN_CPUMASK;
1785 } else if (field->filter_type == FILTER_CPU) {
1787 if (pred->op == OP_BAND)
1790 pred->fn_num = FILTER_PRED_FN_CPU;
1792 pred->fn_num = FILTER_PRED_FN_CPU_CPUMASK;
1794 } else if (single) {
1795 if (pred->op == OP_BAND)
1798 pred->fn_num = select_comparison_fn(pred->op, field->size, false);
1799 if (pred->op == OP_NE)
1802 switch (field->size) {
1804 pred->fn_num = FILTER_PRED_FN_64_CPUMASK;
1807 pred->fn_num = FILTER_PRED_FN_32_CPUMASK;
1810 pred->fn_num = FILTER_PRED_FN_16_CPUMASK;
1813 pred->fn_num = FILTER_PRED_FN_8_CPUMASK;
1818 /* This is either a string, or an integer */
1819 } else if (str[i] == '\'' || str[i] == '"') {
1822 /* Make sure the op is OK for strings */
1831 parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
1835 /* Make sure the field is OK for strings */
1836 if (!is_string_field(field)) {
1837 parse_error(pe, FILT_ERR_EXPECT_DIGIT, pos + i);
1841 for (i++; str[i]; i++) {
1846 parse_error(pe, FILT_ERR_MISSING_QUOTE, pos + i);
1853 if (len >= MAX_FILTER_STR_VAL) {
1854 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1858 pred->regex = kzalloc(sizeof(*pred->regex), GFP_KERNEL);
1861 pred->regex->len = len;
1862 strncpy(pred->regex->pattern, str + s, len);
1863 pred->regex->pattern[len] = 0;
1865 filter_build_regex(pred);
1867 if (field->filter_type == FILTER_COMM) {
1868 pred->fn_num = FILTER_PRED_FN_COMM;
1870 } else if (field->filter_type == FILTER_STATIC_STRING) {
1871 pred->fn_num = FILTER_PRED_FN_STRING;
1872 pred->regex->field_len = field->size;
1874 } else if (field->filter_type == FILTER_DYN_STRING) {
1875 pred->fn_num = FILTER_PRED_FN_STRLOC;
1876 } else if (field->filter_type == FILTER_RDYN_STRING)
1877 pred->fn_num = FILTER_PRED_FN_STRRELLOC;
1880 if (!ustring_per_cpu) {
1881 /* Once allocated, keep it around for good */
1882 ustring_per_cpu = alloc_percpu(struct ustring_buffer);
1883 if (!ustring_per_cpu)
1888 pred->fn_num = FILTER_PRED_FN_PCHAR_USER;
1890 pred->fn_num = FILTER_PRED_FN_PCHAR;
1892 /* go past the last quote */
1895 } else if (isdigit(str[i]) || str[i] == '-') {
1897 /* Make sure the field is not a string */
1898 if (is_string_field(field)) {
1899 parse_error(pe, FILT_ERR_EXPECT_STRING, pos + i);
1903 if (op == OP_GLOB) {
1904 parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
1911 /* We allow 0xDEADBEEF */
1912 while (isalnum(str[i]))
1916 /* 0xfeedfacedeadbeef is 18 chars max */
1917 if (len >= sizeof(num_buf)) {
1918 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1922 strncpy(num_buf, str + s, len);
1925 /* Make sure it is a value */
1926 if (field->is_signed)
1927 ret = kstrtoll(num_buf, 0, &val);
1929 ret = kstrtoull(num_buf, 0, &val);
1931 parse_error(pe, FILT_ERR_ILLEGAL_INTVAL, pos + s);
1937 if (field->filter_type == FILTER_CPU)
1938 pred->fn_num = FILTER_PRED_FN_CPU;
1940 pred->fn_num = select_comparison_fn(pred->op, field->size,
1942 if (pred->op == OP_NE)
1947 parse_error(pe, FILT_ERR_INVALID_VALUE, pos + i);
1955 free_predicate(pred);
1958 free_predicate(pred);
1963 TOO_MANY_CLOSE = -1,
1969 * Read the filter string once to calculate the number of predicates
1970 * as well as how deep the parentheses go.
1973 * 0 - everything is fine (err is undefined)
1976 * -3 - No matching quote
1978 static int calc_stack(const char *str, int *parens, int *preds, int *err)
1980 bool is_pred = false;
1982 int open = 1; /* Count the expression as "(E)" */
1990 for (i = 0; str[i]; i++) {
1991 if (isspace(str[i]))
1994 if (str[i] == quote)
2007 if (str[i+1] != str[i])
2014 if (open > max_open)
2021 return TOO_MANY_CLOSE;
2034 return MISSING_QUOTE;
2040 /* find the bad open */
2043 if (str[i] == quote)
2049 if (level == open) {
2051 return TOO_MANY_OPEN;
2064 /* First character is the '(' with missing ')' */
2066 return TOO_MANY_OPEN;
2069 /* Set the size of the required stacks */
2075 static int process_preds(struct trace_event_call *call,
2076 const char *filter_string,
2077 struct event_filter *filter,
2078 struct filter_parse_error *pe)
2080 struct prog_entry *prog;
2086 ret = calc_stack(filter_string, &nr_parens, &nr_preds, &index);
2090 parse_error(pe, FILT_ERR_MISSING_QUOTE, index);
2093 parse_error(pe, FILT_ERR_TOO_MANY_OPEN, index);
2096 parse_error(pe, FILT_ERR_TOO_MANY_CLOSE, index);
2104 prog = predicate_parse(filter_string, nr_parens, nr_preds,
2105 parse_pred, call, pe);
2107 return PTR_ERR(prog);
2109 rcu_assign_pointer(filter->prog, prog);
2113 static inline void event_set_filtered_flag(struct trace_event_file *file)
2115 unsigned long old_flags = file->flags;
2117 file->flags |= EVENT_FILE_FL_FILTERED;
2119 if (old_flags != file->flags)
2120 trace_buffered_event_enable();
2123 static inline void event_set_filter(struct trace_event_file *file,
2124 struct event_filter *filter)
2126 rcu_assign_pointer(file->filter, filter);
2129 static inline void event_clear_filter(struct trace_event_file *file)
2131 RCU_INIT_POINTER(file->filter, NULL);
2134 struct filter_list {
2135 struct list_head list;
2136 struct event_filter *filter;
2139 static int process_system_preds(struct trace_subsystem_dir *dir,
2140 struct trace_array *tr,
2141 struct filter_parse_error *pe,
2142 char *filter_string)
2144 struct trace_event_file *file;
2145 struct filter_list *filter_item;
2146 struct event_filter *filter = NULL;
2147 struct filter_list *tmp;
2148 LIST_HEAD(filter_list);
2152 list_for_each_entry(file, &tr->events, list) {
2154 if (file->system != dir)
2157 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
2161 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
2162 if (!filter->filter_string)
2165 err = process_preds(file->event_call, filter_string, filter, pe);
2167 filter_disable(file);
2168 parse_error(pe, FILT_ERR_BAD_SUBSYS_FILTER, 0);
2169 append_filter_err(tr, pe, filter);
2171 event_set_filtered_flag(file);
2174 filter_item = kzalloc(sizeof(*filter_item), GFP_KERNEL);
2178 list_add_tail(&filter_item->list, &filter_list);
2180 * Regardless of if this returned an error, we still
2181 * replace the filter for the call.
2183 filter_item->filter = event_filter(file);
2184 event_set_filter(file, filter);
2194 * The calls can still be using the old filters.
2195 * Do a synchronize_rcu() and to ensure all calls are
2196 * done with them before we free them.
2198 tracepoint_synchronize_unregister();
2199 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
2200 __free_filter(filter_item->filter);
2201 list_del(&filter_item->list);
2206 /* No call succeeded */
2207 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
2208 list_del(&filter_item->list);
2211 parse_error(pe, FILT_ERR_BAD_SUBSYS_FILTER, 0);
2214 __free_filter(filter);
2215 /* If any call succeeded, we still need to sync */
2217 tracepoint_synchronize_unregister();
2218 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
2219 __free_filter(filter_item->filter);
2220 list_del(&filter_item->list);
2226 static int create_filter_start(char *filter_string, bool set_str,
2227 struct filter_parse_error **pse,
2228 struct event_filter **filterp)
2230 struct event_filter *filter;
2231 struct filter_parse_error *pe = NULL;
2234 if (WARN_ON_ONCE(*pse || *filterp))
2237 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
2238 if (filter && set_str) {
2239 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
2240 if (!filter->filter_string)
2244 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2246 if (!filter || !pe || err) {
2248 __free_filter(filter);
2252 /* we're committed to creating a new filter */
2259 static void create_filter_finish(struct filter_parse_error *pe)
2265 * create_filter - create a filter for a trace_event_call
2266 * @tr: the trace array associated with these events
2267 * @call: trace_event_call to create a filter for
2268 * @filter_string: filter string
2269 * @set_str: remember @filter_str and enable detailed error in filter
2270 * @filterp: out param for created filter (always updated on return)
2271 * Must be a pointer that references a NULL pointer.
2273 * Creates a filter for @call with @filter_str. If @set_str is %true,
2274 * @filter_str is copied and recorded in the new filter.
2276 * On success, returns 0 and *@filterp points to the new filter. On
2277 * failure, returns -errno and *@filterp may point to %NULL or to a new
2278 * filter. In the latter case, the returned filter contains error
2279 * information if @set_str is %true and the caller is responsible for
2282 static int create_filter(struct trace_array *tr,
2283 struct trace_event_call *call,
2284 char *filter_string, bool set_str,
2285 struct event_filter **filterp)
2287 struct filter_parse_error *pe = NULL;
2290 /* filterp must point to NULL */
2291 if (WARN_ON(*filterp))
2294 err = create_filter_start(filter_string, set_str, &pe, filterp);
2298 err = process_preds(call, filter_string, *filterp, pe);
2300 append_filter_err(tr, pe, *filterp);
2301 create_filter_finish(pe);
2306 int create_event_filter(struct trace_array *tr,
2307 struct trace_event_call *call,
2308 char *filter_str, bool set_str,
2309 struct event_filter **filterp)
2311 return create_filter(tr, call, filter_str, set_str, filterp);
2315 * create_system_filter - create a filter for an event subsystem
2316 * @dir: the descriptor for the subsystem directory
2317 * @filter_str: filter string
2318 * @filterp: out param for created filter (always updated on return)
2320 * Identical to create_filter() except that it creates a subsystem filter
2321 * and always remembers @filter_str.
2323 static int create_system_filter(struct trace_subsystem_dir *dir,
2324 char *filter_str, struct event_filter **filterp)
2326 struct filter_parse_error *pe = NULL;
2329 err = create_filter_start(filter_str, true, &pe, filterp);
2331 err = process_system_preds(dir, dir->tr, pe, filter_str);
2333 /* System filters just show a default message */
2334 kfree((*filterp)->filter_string);
2335 (*filterp)->filter_string = NULL;
2337 append_filter_err(dir->tr, pe, *filterp);
2340 create_filter_finish(pe);
2345 /* caller must hold event_mutex */
2346 int apply_event_filter(struct trace_event_file *file, char *filter_string)
2348 struct trace_event_call *call = file->event_call;
2349 struct event_filter *filter = NULL;
2352 if (!strcmp(strstrip(filter_string), "0")) {
2353 filter_disable(file);
2354 filter = event_filter(file);
2359 event_clear_filter(file);
2361 /* Make sure the filter is not being used */
2362 tracepoint_synchronize_unregister();
2363 __free_filter(filter);
2368 err = create_filter(file->tr, call, filter_string, true, &filter);
2371 * Always swap the call filter with the new filter
2372 * even if there was an error. If there was an error
2373 * in the filter, we disable the filter and show the error
2377 struct event_filter *tmp;
2379 tmp = event_filter(file);
2381 event_set_filtered_flag(file);
2383 filter_disable(file);
2385 event_set_filter(file, filter);
2388 /* Make sure the call is done with the filter */
2389 tracepoint_synchronize_unregister();
2397 int apply_subsystem_event_filter(struct trace_subsystem_dir *dir,
2398 char *filter_string)
2400 struct event_subsystem *system = dir->subsystem;
2401 struct trace_array *tr = dir->tr;
2402 struct event_filter *filter = NULL;
2405 mutex_lock(&event_mutex);
2407 /* Make sure the system still has events */
2408 if (!dir->nr_events) {
2413 if (!strcmp(strstrip(filter_string), "0")) {
2414 filter_free_subsystem_preds(dir, tr);
2415 remove_filter_string(system->filter);
2416 filter = system->filter;
2417 system->filter = NULL;
2418 /* Ensure all filters are no longer used */
2419 tracepoint_synchronize_unregister();
2420 filter_free_subsystem_filters(dir, tr);
2421 __free_filter(filter);
2425 err = create_system_filter(dir, filter_string, &filter);
2428 * No event actually uses the system filter
2429 * we can free it without synchronize_rcu().
2431 __free_filter(system->filter);
2432 system->filter = filter;
2435 mutex_unlock(&event_mutex);
2440 #ifdef CONFIG_PERF_EVENTS
2442 void ftrace_profile_free_filter(struct perf_event *event)
2444 struct event_filter *filter = event->filter;
2446 event->filter = NULL;
2447 __free_filter(filter);
2450 struct function_filter_data {
2451 struct ftrace_ops *ops;
2456 #ifdef CONFIG_FUNCTION_TRACER
2458 ftrace_function_filter_re(char *buf, int len, int *count)
2462 str = kstrndup(buf, len, GFP_KERNEL);
2467 * The argv_split function takes white space
2468 * as a separator, so convert ',' into spaces.
2470 strreplace(str, ',', ' ');
2472 re = argv_split(GFP_KERNEL, str, count);
2477 static int ftrace_function_set_regexp(struct ftrace_ops *ops, int filter,
2478 int reset, char *re, int len)
2483 ret = ftrace_set_filter(ops, re, len, reset);
2485 ret = ftrace_set_notrace(ops, re, len, reset);
2490 static int __ftrace_function_set_filter(int filter, char *buf, int len,
2491 struct function_filter_data *data)
2493 int i, re_cnt, ret = -EINVAL;
2497 reset = filter ? &data->first_filter : &data->first_notrace;
2500 * The 'ip' field could have multiple filters set, separated
2501 * either by space or comma. We first cut the filter and apply
2502 * all pieces separately.
2504 re = ftrace_function_filter_re(buf, len, &re_cnt);
2508 for (i = 0; i < re_cnt; i++) {
2509 ret = ftrace_function_set_regexp(data->ops, filter, *reset,
2510 re[i], strlen(re[i]));
2522 static int ftrace_function_check_pred(struct filter_pred *pred)
2524 struct ftrace_event_field *field = pred->field;
2527 * Check the predicate for function trace, verify:
2528 * - only '==' and '!=' is used
2529 * - the 'ip' field is used
2531 if ((pred->op != OP_EQ) && (pred->op != OP_NE))
2534 if (strcmp(field->name, "ip"))
2540 static int ftrace_function_set_filter_pred(struct filter_pred *pred,
2541 struct function_filter_data *data)
2545 /* Checking the node is valid for function trace. */
2546 ret = ftrace_function_check_pred(pred);
2550 return __ftrace_function_set_filter(pred->op == OP_EQ,
2551 pred->regex->pattern,
2556 static bool is_or(struct prog_entry *prog, int i)
2561 * Only "||" is allowed for function events, thus,
2562 * all true branches should jump to true, and any
2563 * false branch should jump to false.
2565 target = prog[i].target + 1;
2566 /* True and false have NULL preds (all prog entries should jump to one */
2567 if (prog[target].pred)
2570 /* prog[target].target is 1 for TRUE, 0 for FALSE */
2571 return prog[i].when_to_branch == prog[target].target;
2574 static int ftrace_function_set_filter(struct perf_event *event,
2575 struct event_filter *filter)
2577 struct prog_entry *prog = rcu_dereference_protected(filter->prog,
2578 lockdep_is_held(&event_mutex));
2579 struct function_filter_data data = {
2582 .ops = &event->ftrace_ops,
2586 for (i = 0; prog[i].pred; i++) {
2587 struct filter_pred *pred = prog[i].pred;
2589 if (!is_or(prog, i))
2592 if (ftrace_function_set_filter_pred(pred, &data) < 0)
2598 static int ftrace_function_set_filter(struct perf_event *event,
2599 struct event_filter *filter)
2603 #endif /* CONFIG_FUNCTION_TRACER */
2605 int ftrace_profile_set_filter(struct perf_event *event, int event_id,
2609 struct event_filter *filter = NULL;
2610 struct trace_event_call *call;
2612 mutex_lock(&event_mutex);
2614 call = event->tp_event;
2624 err = create_filter(NULL, call, filter_str, false, &filter);
2628 if (ftrace_event_is_function(call))
2629 err = ftrace_function_set_filter(event, filter);
2631 event->filter = filter;
2634 if (err || ftrace_event_is_function(call))
2635 __free_filter(filter);
2638 mutex_unlock(&event_mutex);
2643 #endif /* CONFIG_PERF_EVENTS */
2645 #ifdef CONFIG_FTRACE_STARTUP_TEST
2647 #include <linux/types.h>
2648 #include <linux/tracepoint.h>
2650 #define CREATE_TRACE_POINTS
2651 #include "trace_events_filter_test.h"
2653 #define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \
2656 .rec = { .a = va, .b = vb, .c = vc, .d = vd, \
2657 .e = ve, .f = vf, .g = vg, .h = vh }, \
2659 .not_visited = nvisit, \
2664 static struct test_filter_data_t {
2666 struct trace_event_raw_ftrace_test_filter rec;
2669 } test_filter_data[] = {
2670 #define FILTER "a == 1 && b == 1 && c == 1 && d == 1 && " \
2671 "e == 1 && f == 1 && g == 1 && h == 1"
2672 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, ""),
2673 DATA_REC(NO, 0, 1, 1, 1, 1, 1, 1, 1, "bcdefgh"),
2674 DATA_REC(NO, 1, 1, 1, 1, 1, 1, 1, 0, ""),
2676 #define FILTER "a == 1 || b == 1 || c == 1 || d == 1 || " \
2677 "e == 1 || f == 1 || g == 1 || h == 1"
2678 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2679 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2680 DATA_REC(YES, 1, 0, 0, 0, 0, 0, 0, 0, "bcdefgh"),
2682 #define FILTER "(a == 1 || b == 1) && (c == 1 || d == 1) && " \
2683 "(e == 1 || f == 1) && (g == 1 || h == 1)"
2684 DATA_REC(NO, 0, 0, 1, 1, 1, 1, 1, 1, "dfh"),
2685 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2686 DATA_REC(YES, 1, 0, 1, 0, 0, 1, 0, 1, "bd"),
2687 DATA_REC(NO, 1, 0, 1, 0, 0, 1, 0, 0, "bd"),
2689 #define FILTER "(a == 1 && b == 1) || (c == 1 && d == 1) || " \
2690 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2691 DATA_REC(YES, 1, 0, 1, 1, 1, 1, 1, 1, "efgh"),
2692 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 1, 1, ""),
2693 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2695 #define FILTER "(a == 1 && b == 1) && (c == 1 && d == 1) && " \
2696 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2697 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 0, "gh"),
2698 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2699 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, ""),
2701 #define FILTER "((a == 1 || b == 1) || (c == 1 || d == 1) || " \
2702 "(e == 1 || f == 1)) && (g == 1 || h == 1)"
2703 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 1, "bcdef"),
2704 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2705 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, "h"),
2707 #define FILTER "((((((((a == 1) && (b == 1)) || (c == 1)) && (d == 1)) || " \
2708 "(e == 1)) && (f == 1)) || (g == 1)) && (h == 1))"
2709 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "ceg"),
2710 DATA_REC(NO, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2711 DATA_REC(NO, 1, 0, 1, 0, 1, 0, 1, 0, ""),
2713 #define FILTER "((((((((a == 1) || (b == 1)) && (c == 1)) || (d == 1)) && " \
2714 "(e == 1)) || (f == 1)) && (g == 1)) || (h == 1))"
2715 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "bdfh"),
2716 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2717 DATA_REC(YES, 1, 0, 1, 0, 1, 0, 1, 0, "bdfh"),
2725 #define DATA_CNT ARRAY_SIZE(test_filter_data)
2727 static int test_pred_visited;
2729 static int test_pred_visited_fn(struct filter_pred *pred, void *event)
2731 struct ftrace_event_field *field = pred->field;
2733 test_pred_visited = 1;
2734 printk(KERN_INFO "\npred visited %s\n", field->name);
2738 static void update_pred_fn(struct event_filter *filter, char *fields)
2740 struct prog_entry *prog = rcu_dereference_protected(filter->prog,
2741 lockdep_is_held(&event_mutex));
2744 for (i = 0; prog[i].pred; i++) {
2745 struct filter_pred *pred = prog[i].pred;
2746 struct ftrace_event_field *field = pred->field;
2748 WARN_ON_ONCE(pred->fn_num == FILTER_PRED_FN_NOP);
2751 WARN_ONCE(1, "all leafs should have field defined %d", i);
2755 if (!strchr(fields, *field->name))
2758 pred->fn_num = FILTER_PRED_TEST_VISITED;
2762 static __init int ftrace_test_event_filter(void)
2766 printk(KERN_INFO "Testing ftrace filter: ");
2768 for (i = 0; i < DATA_CNT; i++) {
2769 struct event_filter *filter = NULL;
2770 struct test_filter_data_t *d = &test_filter_data[i];
2773 err = create_filter(NULL, &event_ftrace_test_filter,
2774 d->filter, false, &filter);
2777 "Failed to get filter for '%s', err %d\n",
2779 __free_filter(filter);
2783 /* Needed to dereference filter->prog */
2784 mutex_lock(&event_mutex);
2786 * The preemption disabling is not really needed for self
2787 * tests, but the rcu dereference will complain without it.
2790 if (*d->not_visited)
2791 update_pred_fn(filter, d->not_visited);
2793 test_pred_visited = 0;
2794 err = filter_match_preds(filter, &d->rec);
2797 mutex_unlock(&event_mutex);
2799 __free_filter(filter);
2801 if (test_pred_visited) {
2803 "Failed, unwanted pred visited for filter %s\n",
2808 if (err != d->match) {
2810 "Failed to match filter '%s', expected %d\n",
2811 d->filter, d->match);
2817 printk(KERN_CONT "OK\n");
2822 late_initcall(ftrace_test_event_filter);
2824 #endif /* CONFIG_FTRACE_STARTUP_TEST */