~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/kernel/trace/trace_events_filter.c

Version: ~ [ linux-5.7 ] ~ [ linux-5.6.15 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.43 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.125 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.182 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.225 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.225 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.84 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp