Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * trace_events_filter - generic event filtering
0004  *
0005  * Copyright (C) 2009 Tom Zanussi <tzanussi@gmail.com>
0006  */
0007 
0008 #include <linux/uaccess.h>
0009 #include <linux/module.h>
0010 #include <linux/ctype.h>
0011 #include <linux/mutex.h>
0012 #include <linux/perf_event.h>
0013 #include <linux/slab.h>
0014 
0015 #include "trace.h"
0016 #include "trace_output.h"
0017 
0018 #define DEFAULT_SYS_FILTER_MESSAGE                  \
0019     "### global filter ###\n"                   \
0020     "# Use this to set filters for multiple events.\n"      \
0021     "# Only events with the given fields will be affected.\n"   \
0022     "# If no events are modified, an error message will be displayed here"
0023 
0024 /* Due to token parsing '<=' must be before '<' and '>=' must be before '>' */
0025 #define OPS                 \
0026     C( OP_GLOB, "~"  ),         \
0027     C( OP_NE,   "!=" ),         \
0028     C( OP_EQ,   "==" ),         \
0029     C( OP_LE,   "<=" ),         \
0030     C( OP_LT,   "<"  ),         \
0031     C( OP_GE,   ">=" ),         \
0032     C( OP_GT,   ">"  ),         \
0033     C( OP_BAND, "&"  ),         \
0034     C( OP_MAX,  NULL )
0035 
0036 #undef C
0037 #define C(a, b) a
0038 
0039 enum filter_op_ids { OPS };
0040 
0041 #undef C
0042 #define C(a, b) b
0043 
0044 static const char * ops[] = { OPS };
0045 
0046 /*
0047  * pred functions are OP_LE, OP_LT, OP_GE, OP_GT, and OP_BAND
0048  * pred_funcs_##type below must match the order of them above.
0049  */
0050 #define PRED_FUNC_START         OP_LE
0051 #define PRED_FUNC_MAX           (OP_BAND - PRED_FUNC_START)
0052 
0053 #define ERRORS                              \
0054     C(NONE,         "No error"),                \
0055     C(INVALID_OP,       "Invalid operator"),            \
0056     C(TOO_MANY_OPEN,    "Too many '('"),            \
0057     C(TOO_MANY_CLOSE,   "Too few '('"),             \
0058     C(MISSING_QUOTE,    "Missing matching quote"),      \
0059     C(OPERAND_TOO_LONG, "Operand too long"),            \
0060     C(EXPECT_STRING,    "Expecting string field"),      \
0061     C(EXPECT_DIGIT,     "Expecting numeric field"),     \
0062     C(ILLEGAL_FIELD_OP, "Illegal operation for field type"),    \
0063     C(FIELD_NOT_FOUND,  "Field not found"),         \
0064     C(ILLEGAL_INTVAL,   "Illegal integer value"),       \
0065     C(BAD_SUBSYS_FILTER,    "Couldn't find or set field in one of a subsystem's events"), \
0066     C(TOO_MANY_PREDS,   "Too many terms in predicate expression"), \
0067     C(INVALID_FILTER,   "Meaningless filter expression"),   \
0068     C(IP_FIELD_ONLY,    "Only 'ip' field is supported for function trace"), \
0069     C(INVALID_VALUE,    "Invalid value (did you forget quotes)?"), \
0070     C(ERRNO,        "Error"),               \
0071     C(NO_FILTER,        "No filter found")
0072 
0073 #undef C
0074 #define C(a, b)     FILT_ERR_##a
0075 
0076 enum { ERRORS };
0077 
0078 #undef C
0079 #define C(a, b)     b
0080 
0081 static const char *err_text[] = { ERRORS };
0082 
0083 /* Called after a '!' character but "!=" and "!~" are not "not"s */
0084 static bool is_not(const char *str)
0085 {
0086     switch (str[1]) {
0087     case '=':
0088     case '~':
0089         return false;
0090     }
0091     return true;
0092 }
0093 
0094 /**
0095  * prog_entry - a singe entry in the filter program
0096  * @target:      Index to jump to on a branch (actually one minus the index)
0097  * @when_to_branch:  The value of the result of the predicate to do a branch
0098  * @pred:        The predicate to execute.
0099  */
0100 struct prog_entry {
0101     int         target;
0102     int         when_to_branch;
0103     struct filter_pred  *pred;
0104 };
0105 
0106 /**
0107  * update_preds- assign a program entry a label target
0108  * @prog: The program array
0109  * @N: The index of the current entry in @prog
0110  * @when_to_branch: What to assign a program entry for its branch condition
0111  *
0112  * The program entry at @N has a target that points to the index of a program
0113  * entry that can have its target and when_to_branch fields updated.
0114  * Update the current program entry denoted by index @N target field to be
0115  * that of the updated entry. This will denote the entry to update if
0116  * we are processing an "||" after an "&&"
0117  */
0118 static void update_preds(struct prog_entry *prog, int N, int invert)
0119 {
0120     int t, s;
0121 
0122     t = prog[N].target;
0123     s = prog[t].target;
0124     prog[t].when_to_branch = invert;
0125     prog[t].target = N;
0126     prog[N].target = s;
0127 }
0128 
0129 struct filter_parse_error {
0130     int lasterr;
0131     int lasterr_pos;
0132 };
0133 
0134 static void parse_error(struct filter_parse_error *pe, int err, int pos)
0135 {
0136     pe->lasterr = err;
0137     pe->lasterr_pos = pos;
0138 }
0139 
0140 typedef int (*parse_pred_fn)(const char *str, void *data, int pos,
0141                  struct filter_parse_error *pe,
0142                  struct filter_pred **pred);
0143 
0144 enum {
0145     INVERT      = 1,
0146     PROCESS_AND = 2,
0147     PROCESS_OR  = 4,
0148 };
0149 
0150 /*
0151  * Without going into a formal proof, this explains the method that is used in
0152  * parsing the logical expressions.
0153  *
0154  * For example, if we have: "a && !(!b || (c && g)) || d || e && !f"
0155  * The first pass will convert it into the following program:
0156  *
0157  * n1: r=a;       l1: if (!r) goto l4;
0158  * n2: r=b;       l2: if (!r) goto l4;
0159  * n3: r=c; r=!r; l3: if (r) goto l4;
0160  * n4: r=g; r=!r; l4: if (r) goto l5;
0161  * n5: r=d;       l5: if (r) goto T
0162  * n6: r=e;       l6: if (!r) goto l7;
0163  * n7: r=f; r=!r; l7: if (!r) goto F
0164  * T: return TRUE
0165  * F: return FALSE
0166  *
0167  * To do this, we use a data structure to represent each of the above
0168  * predicate and conditions that has:
0169  *
0170  *  predicate, when_to_branch, invert, target
0171  *
0172  * The "predicate" will hold the function to determine the result "r".
0173  * The "when_to_branch" denotes what "r" should be if a branch is to be taken
0174  * "&&" would contain "!r" or (0) and "||" would contain "r" or (1).
0175  * The "invert" holds whether the value should be reversed before testing.
0176  * The "target" contains the label "l#" to jump to.
0177  *
0178  * A stack is created to hold values when parentheses are used.
0179  *
0180  * To simplify the logic, the labels will start at 0 and not 1.
0181  *
0182  * The possible invert values are 1 and 0. The number of "!"s that are in scope
0183  * before the predicate determines the invert value, if the number is odd then
0184  * the invert value is 1 and 0 otherwise. This means the invert value only
0185  * needs to be toggled when a new "!" is introduced compared to what is stored
0186  * on the stack, where parentheses were used.
0187  *
0188  * The top of the stack and "invert" are initialized to zero.
0189  *
0190  * ** FIRST PASS **
0191  *
0192  * #1 A loop through all the tokens is done:
0193  *
0194  * #2 If the token is an "(", the stack is push, and the current stack value
0195  *    gets the current invert value, and the loop continues to the next token.
0196  *    The top of the stack saves the "invert" value to keep track of what
0197  *    the current inversion is. As "!(a && !b || c)" would require all
0198  *    predicates being affected separately by the "!" before the parentheses.
0199  *    And that would end up being equivalent to "(!a || b) && !c"
0200  *
0201  * #3 If the token is an "!", the current "invert" value gets inverted, and
0202  *    the loop continues. Note, if the next token is a predicate, then
0203  *    this "invert" value is only valid for the current program entry,
0204  *    and does not affect other predicates later on.
0205  *
0206  * The only other acceptable token is the predicate string.
0207  *
0208  * #4 A new entry into the program is added saving: the predicate and the
0209  *    current value of "invert". The target is currently assigned to the
0210  *    previous program index (this will not be its final value).
0211  *
0212  * #5 We now enter another loop and look at the next token. The only valid
0213  *    tokens are ")", "&&", "||" or end of the input string "\0".
0214  *
0215  * #6 The invert variable is reset to the current value saved on the top of
0216  *    the stack.
0217  *
0218  * #7 The top of the stack holds not only the current invert value, but also
0219  *    if a "&&" or "||" needs to be processed. Note, the "&&" takes higher
0220  *    precedence than "||". That is "a && b || c && d" is equivalent to
0221  *    "(a && b) || (c && d)". Thus the first thing to do is to see if "&&" needs
0222  *    to be processed. This is the case if an "&&" was the last token. If it was
0223  *    then we call update_preds(). This takes the program, the current index in
0224  *    the program, and the current value of "invert".  More will be described
0225  *    below about this function.
0226  *
0227  * #8 If the next token is "&&" then we set a flag in the top of the stack
0228  *    that denotes that "&&" needs to be processed, break out of this loop
0229  *    and continue with the outer loop.
0230  *
0231  * #9 Otherwise, if a "||" needs to be processed then update_preds() is called.
0232  *    This is called with the program, the current index in the program, but
0233  *    this time with an inverted value of "invert" (that is !invert). This is
0234  *    because the value taken will become the "when_to_branch" value of the
0235  *    program.
0236  *    Note, this is called when the next token is not an "&&". As stated before,
0237  *    "&&" takes higher precedence, and "||" should not be processed yet if the
0238  *    next logical operation is "&&".
0239  *
0240  * #10 If the next token is "||" then we set a flag in the top of the stack
0241  *     that denotes that "||" needs to be processed, break out of this loop
0242  *     and continue with the outer loop.
0243  *
0244  * #11 If this is the end of the input string "\0" then we break out of both
0245  *     loops.
0246  *
0247  * #12 Otherwise, the next token is ")", where we pop the stack and continue
0248  *     this inner loop.
0249  *
0250  * Now to discuss the update_pred() function, as that is key to the setting up
0251  * of the program. Remember the "target" of the program is initialized to the
0252  * previous index and not the "l" label. The target holds the index into the
0253  * program that gets affected by the operand. Thus if we have something like
0254  *  "a || b && c", when we process "a" the target will be "-1" (undefined).
0255  * When we process "b", its target is "0", which is the index of "a", as that's
0256  * the predicate that is affected by "||". But because the next token after "b"
0257  * is "&&" we don't call update_preds(). Instead continue to "c". As the
0258  * next token after "c" is not "&&" but the end of input, we first process the
0259  * "&&" by calling update_preds() for the "&&" then we process the "||" by
0260  * calling updates_preds() with the values for processing "||".
0261  *
0262  * What does that mean? What update_preds() does is to first save the "target"
0263  * of the program entry indexed by the current program entry's "target"
0264  * (remember the "target" is initialized to previous program entry), and then
0265  * sets that "target" to the current index which represents the label "l#".
0266  * That entry's "when_to_branch" is set to the value passed in (the "invert"
0267  * or "!invert"). Then it sets the current program entry's target to the saved
0268  * "target" value (the old value of the program that had its "target" updated
0269  * to the label).
0270  *
0271  * Looking back at "a || b && c", we have the following steps:
0272  *  "a"  - prog[0] = { "a", X, -1 } // pred, when_to_branch, target
0273  *  "||" - flag that we need to process "||"; continue outer loop
0274  *  "b"  - prog[1] = { "b", X, 0 }
0275  *  "&&" - flag that we need to process "&&"; continue outer loop
0276  * (Notice we did not process "||")
0277  *  "c"  - prog[2] = { "c", X, 1 }
0278  *  update_preds(prog, 2, 0); // invert = 0 as we are processing "&&"
0279  *    t = prog[2].target; // t = 1
0280  *    s = prog[t].target; // s = 0
0281  *    prog[t].target = 2; // Set target to "l2"
0282  *    prog[t].when_to_branch = 0;
0283  *    prog[2].target = s;
0284  * update_preds(prog, 2, 1); // invert = 1 as we are now processing "||"
0285  *    t = prog[2].target; // t = 0
0286  *    s = prog[t].target; // s = -1
0287  *    prog[t].target = 2; // Set target to "l2"
0288  *    prog[t].when_to_branch = 1;
0289  *    prog[2].target = s;
0290  *
0291  * #13 Which brings us to the final step of the first pass, which is to set
0292  *     the last program entry's when_to_branch and target, which will be
0293  *     when_to_branch = 0; target = N; ( the label after the program entry after
0294  *     the last program entry processed above).
0295  *
0296  * If we denote "TRUE" to be the entry after the last program entry processed,
0297  * and "FALSE" the program entry after that, we are now done with the first
0298  * pass.
0299  *
0300  * Making the above "a || b && c" have a program of:
0301  *  prog[0] = { "a", 1, 2 }
0302  *  prog[1] = { "b", 0, 2 }
0303  *  prog[2] = { "c", 0, 3 }
0304  *
0305  * Which translates into:
0306  * n0: r = a; l0: if (r) goto l2;
0307  * n1: r = b; l1: if (!r) goto l2;
0308  * n2: r = c; l2: if (!r) goto l3;  // Which is the same as "goto F;"
0309  * T: return TRUE; l3:
0310  * F: return FALSE
0311  *
0312  * Although, after the first pass, the program is correct, it is
0313  * inefficient. The simple sample of "a || b && c" could be easily been
0314  * converted into:
0315  * n0: r = a; if (r) goto T
0316  * n1: r = b; if (!r) goto F
0317  * n2: r = c; if (!r) goto F
0318  * T: return TRUE;
0319  * F: return FALSE;
0320  *
0321  * The First Pass is over the input string. The next too passes are over
0322  * the program itself.
0323  *
0324  * ** SECOND PASS **
0325  *
0326  * Which brings us to the second pass. If a jump to a label has the
0327  * same condition as that label, it can instead jump to its target.
0328  * The original example of "a && !(!b || (c && g)) || d || e && !f"
0329  * where the first pass gives us:
0330  *
0331  * n1: r=a;       l1: if (!r) goto l4;
0332  * n2: r=b;       l2: if (!r) goto l4;
0333  * n3: r=c; r=!r; l3: if (r) goto l4;
0334  * n4: r=g; r=!r; l4: if (r) goto l5;
0335  * n5: r=d;       l5: if (r) goto T
0336  * n6: r=e;       l6: if (!r) goto l7;
0337  * n7: r=f; r=!r; l7: if (!r) goto F:
0338  * T: return TRUE;
0339  * F: return FALSE
0340  *
0341  * We can see that "l3: if (r) goto l4;" and at l4, we have "if (r) goto l5;".
0342  * And "l5: if (r) goto T", we could optimize this by converting l3 and l4
0343  * to go directly to T. To accomplish this, we start from the last
0344  * entry in the program and work our way back. If the target of the entry
0345  * has the same "when_to_branch" then we could use that entry's target.
0346  * Doing this, the above would end up as:
0347  *
0348  * n1: r=a;       l1: if (!r) goto l4;
0349  * n2: r=b;       l2: if (!r) goto l4;
0350  * n3: r=c; r=!r; l3: if (r) goto T;
0351  * n4: r=g; r=!r; l4: if (r) goto T;
0352  * n5: r=d;       l5: if (r) goto T;
0353  * n6: r=e;       l6: if (!r) goto F;
0354  * n7: r=f; r=!r; l7: if (!r) goto F;
0355  * T: return TRUE
0356  * F: return FALSE
0357  *
0358  * In that same pass, if the "when_to_branch" doesn't match, we can simply
0359  * go to the program entry after the label. That is, "l2: if (!r) goto l4;"
0360  * where "l4: if (r) goto T;", then we can convert l2 to be:
0361  * "l2: if (!r) goto n5;".
0362  *
0363  * This will have the second pass give us:
0364  * n1: r=a;       l1: if (!r) goto n5;
0365  * n2: r=b;       l2: if (!r) goto n5;
0366  * n3: r=c; r=!r; l3: if (r) goto T;
0367  * n4: r=g; r=!r; l4: if (r) goto T;
0368  * n5: r=d;       l5: if (r) goto T
0369  * n6: r=e;       l6: if (!r) goto F;
0370  * n7: r=f; r=!r; l7: if (!r) goto F
0371  * T: return TRUE
0372  * F: return FALSE
0373  *
0374  * Notice, all the "l#" labels are no longer used, and they can now
0375  * be discarded.
0376  *
0377  * ** THIRD PASS **
0378  *
0379  * For the third pass we deal with the inverts. As they simply just
0380  * make the "when_to_branch" get inverted, a simple loop over the
0381  * program to that does: "when_to_branch ^= invert;" will do the
0382  * job, leaving us with:
0383  * n1: r=a; if (!r) goto n5;
0384  * n2: r=b; if (!r) goto n5;
0385  * n3: r=c: if (!r) goto T;
0386  * n4: r=g; if (!r) goto T;
0387  * n5: r=d; if (r) goto T
0388  * n6: r=e; if (!r) goto F;
0389  * n7: r=f; if (r) goto F
0390  * T: return TRUE
0391  * F: return FALSE
0392  *
0393  * As "r = a; if (!r) goto n5;" is obviously the same as
0394  * "if (!a) goto n5;" without doing anything we can interpret the
0395  * program as:
0396  * n1: if (!a) goto n5;
0397  * n2: if (!b) goto n5;
0398  * n3: if (!c) goto T;
0399  * n4: if (!g) goto T;
0400  * n5: if (d) goto T
0401  * n6: if (!e) goto F;
0402  * n7: if (f) goto F
0403  * T: return TRUE
0404  * F: return FALSE
0405  *
0406  * Since the inverts are discarded at the end, there's no reason to store
0407  * them in the program array (and waste memory). A separate array to hold
0408  * the inverts is used and freed at the end.
0409  */
0410 static struct prog_entry *
0411 predicate_parse(const char *str, int nr_parens, int nr_preds,
0412         parse_pred_fn parse_pred, void *data,
0413         struct filter_parse_error *pe)
0414 {
0415     struct prog_entry *prog_stack;
0416     struct prog_entry *prog;
0417     const char *ptr = str;
0418     char *inverts = NULL;
0419     int *op_stack;
0420     int *top;
0421     int invert = 0;
0422     int ret = -ENOMEM;
0423     int len;
0424     int N = 0;
0425     int i;
0426 
0427     nr_preds += 2; /* For TRUE and FALSE */
0428 
0429     op_stack = kmalloc_array(nr_parens, sizeof(*op_stack), GFP_KERNEL);
0430     if (!op_stack)
0431         return ERR_PTR(-ENOMEM);
0432     prog_stack = kcalloc(nr_preds, sizeof(*prog_stack), GFP_KERNEL);
0433     if (!prog_stack) {
0434         parse_error(pe, -ENOMEM, 0);
0435         goto out_free;
0436     }
0437     inverts = kmalloc_array(nr_preds, sizeof(*inverts), GFP_KERNEL);
0438     if (!inverts) {
0439         parse_error(pe, -ENOMEM, 0);
0440         goto out_free;
0441     }
0442 
0443     top = op_stack;
0444     prog = prog_stack;
0445     *top = 0;
0446 
0447     /* First pass */
0448     while (*ptr) {                      /* #1 */
0449         const char *next = ptr++;
0450 
0451         if (isspace(*next))
0452             continue;
0453 
0454         switch (*next) {
0455         case '(':                   /* #2 */
0456             if (top - op_stack > nr_parens) {
0457                 ret = -EINVAL;
0458                 goto out_free;
0459             }
0460             *(++top) = invert;
0461             continue;
0462         case '!':                   /* #3 */
0463             if (!is_not(next))
0464                 break;
0465             invert = !invert;
0466             continue;
0467         }
0468 
0469         if (N >= nr_preds) {
0470             parse_error(pe, FILT_ERR_TOO_MANY_PREDS, next - str);
0471             goto out_free;
0472         }
0473 
0474         inverts[N] = invert;                /* #4 */
0475         prog[N].target = N-1;
0476 
0477         len = parse_pred(next, data, ptr - str, pe, &prog[N].pred);
0478         if (len < 0) {
0479             ret = len;
0480             goto out_free;
0481         }
0482         ptr = next + len;
0483 
0484         N++;
0485 
0486         ret = -1;
0487         while (1) {                 /* #5 */
0488             next = ptr++;
0489             if (isspace(*next))
0490                 continue;
0491 
0492             switch (*next) {
0493             case ')':
0494             case '\0':
0495                 break;
0496             case '&':
0497             case '|':
0498                 /* accepting only "&&" or "||" */
0499                 if (next[1] == next[0]) {
0500                     ptr++;
0501                     break;
0502                 }
0503                 fallthrough;
0504             default:
0505                 parse_error(pe, FILT_ERR_TOO_MANY_PREDS,
0506                         next - str);
0507                 goto out_free;
0508             }
0509 
0510             invert = *top & INVERT;
0511 
0512             if (*top & PROCESS_AND) {       /* #7 */
0513                 update_preds(prog, N - 1, invert);
0514                 *top &= ~PROCESS_AND;
0515             }
0516             if (*next == '&') {         /* #8 */
0517                 *top |= PROCESS_AND;
0518                 break;
0519             }
0520             if (*top & PROCESS_OR) {        /* #9 */
0521                 update_preds(prog, N - 1, !invert);
0522                 *top &= ~PROCESS_OR;
0523             }
0524             if (*next == '|') {         /* #10 */
0525                 *top |= PROCESS_OR;
0526                 break;
0527             }
0528             if (!*next)             /* #11 */
0529                 goto out;
0530 
0531             if (top == op_stack) {
0532                 ret = -1;
0533                 /* Too few '(' */
0534                 parse_error(pe, FILT_ERR_TOO_MANY_CLOSE, ptr - str);
0535                 goto out_free;
0536             }
0537             top--;                  /* #12 */
0538         }
0539     }
0540  out:
0541     if (top != op_stack) {
0542         /* Too many '(' */
0543         parse_error(pe, FILT_ERR_TOO_MANY_OPEN, ptr - str);
0544         goto out_free;
0545     }
0546 
0547     if (!N) {
0548         /* No program? */
0549         ret = -EINVAL;
0550         parse_error(pe, FILT_ERR_NO_FILTER, ptr - str);
0551         goto out_free;
0552     }
0553 
0554     prog[N].pred = NULL;                    /* #13 */
0555     prog[N].target = 1;     /* TRUE */
0556     prog[N+1].pred = NULL;
0557     prog[N+1].target = 0;       /* FALSE */
0558     prog[N-1].target = N;
0559     prog[N-1].when_to_branch = false;
0560 
0561     /* Second Pass */
0562     for (i = N-1 ; i--; ) {
0563         int target = prog[i].target;
0564         if (prog[i].when_to_branch == prog[target].when_to_branch)
0565             prog[i].target = prog[target].target;
0566     }
0567 
0568     /* Third Pass */
0569     for (i = 0; i < N; i++) {
0570         invert = inverts[i] ^ prog[i].when_to_branch;
0571         prog[i].when_to_branch = invert;
0572         /* Make sure the program always moves forward */
0573         if (WARN_ON(prog[i].target <= i)) {
0574             ret = -EINVAL;
0575             goto out_free;
0576         }
0577     }
0578 
0579     kfree(op_stack);
0580     kfree(inverts);
0581     return prog;
0582 out_free:
0583     kfree(op_stack);
0584     kfree(inverts);
0585     if (prog_stack) {
0586         for (i = 0; prog_stack[i].pred; i++)
0587             kfree(prog_stack[i].pred);
0588         kfree(prog_stack);
0589     }
0590     return ERR_PTR(ret);
0591 }
0592 
0593 #define DEFINE_COMPARISON_PRED(type)                    \
0594 static int filter_pred_LT_##type(struct filter_pred *pred, void *event) \
0595 {                                   \
0596     type *addr = (type *)(event + pred->offset);            \
0597     type val = (type)pred->val;                 \
0598     return *addr < val;                     \
0599 }                                   \
0600 static int filter_pred_LE_##type(struct filter_pred *pred, void *event) \
0601 {                                   \
0602     type *addr = (type *)(event + pred->offset);            \
0603     type val = (type)pred->val;                 \
0604     return *addr <= val;                        \
0605 }                                   \
0606 static int filter_pred_GT_##type(struct filter_pred *pred, void *event) \
0607 {                                   \
0608     type *addr = (type *)(event + pred->offset);            \
0609     type val = (type)pred->val;                 \
0610     return *addr > val;                 \
0611 }                                   \
0612 static int filter_pred_GE_##type(struct filter_pred *pred, void *event) \
0613 {                                   \
0614     type *addr = (type *)(event + pred->offset);            \
0615     type val = (type)pred->val;                 \
0616     return *addr >= val;                        \
0617 }                                   \
0618 static int filter_pred_BAND_##type(struct filter_pred *pred, void *event) \
0619 {                                   \
0620     type *addr = (type *)(event + pred->offset);            \
0621     type val = (type)pred->val;                 \
0622     return !!(*addr & val);                     \
0623 }                                   \
0624 static const filter_pred_fn_t pred_funcs_##type[] = {           \
0625     filter_pred_LE_##type,                      \
0626     filter_pred_LT_##type,                      \
0627     filter_pred_GE_##type,                      \
0628     filter_pred_GT_##type,                      \
0629     filter_pred_BAND_##type,                    \
0630 };
0631 
0632 #define DEFINE_EQUALITY_PRED(size)                  \
0633 static int filter_pred_##size(struct filter_pred *pred, void *event)    \
0634 {                                   \
0635     u##size *addr = (u##size *)(event + pred->offset);      \
0636     u##size val = (u##size)pred->val;               \
0637     int match;                          \
0638                                     \
0639     match = (val == *addr) ^ pred->not;             \
0640                                     \
0641     return match;                           \
0642 }
0643 
0644 DEFINE_COMPARISON_PRED(s64);
0645 DEFINE_COMPARISON_PRED(u64);
0646 DEFINE_COMPARISON_PRED(s32);
0647 DEFINE_COMPARISON_PRED(u32);
0648 DEFINE_COMPARISON_PRED(s16);
0649 DEFINE_COMPARISON_PRED(u16);
0650 DEFINE_COMPARISON_PRED(s8);
0651 DEFINE_COMPARISON_PRED(u8);
0652 
0653 DEFINE_EQUALITY_PRED(64);
0654 DEFINE_EQUALITY_PRED(32);
0655 DEFINE_EQUALITY_PRED(16);
0656 DEFINE_EQUALITY_PRED(8);
0657 
0658 /* user space strings temp buffer */
0659 #define USTRING_BUF_SIZE    1024
0660 
0661 struct ustring_buffer {
0662     char        buffer[USTRING_BUF_SIZE];
0663 };
0664 
0665 static __percpu struct ustring_buffer *ustring_per_cpu;
0666 
0667 static __always_inline char *test_string(char *str)
0668 {
0669     struct ustring_buffer *ubuf;
0670     char *kstr;
0671 
0672     if (!ustring_per_cpu)
0673         return NULL;
0674 
0675     ubuf = this_cpu_ptr(ustring_per_cpu);
0676     kstr = ubuf->buffer;
0677 
0678     /* For safety, do not trust the string pointer */
0679     if (!strncpy_from_kernel_nofault(kstr, str, USTRING_BUF_SIZE))
0680         return NULL;
0681     return kstr;
0682 }
0683 
0684 static __always_inline char *test_ustring(char *str)
0685 {
0686     struct ustring_buffer *ubuf;
0687     char __user *ustr;
0688     char *kstr;
0689 
0690     if (!ustring_per_cpu)
0691         return NULL;
0692 
0693     ubuf = this_cpu_ptr(ustring_per_cpu);
0694     kstr = ubuf->buffer;
0695 
0696     /* user space address? */
0697     ustr = (char __user *)str;
0698     if (!strncpy_from_user_nofault(kstr, ustr, USTRING_BUF_SIZE))
0699         return NULL;
0700 
0701     return kstr;
0702 }
0703 
0704 /* Filter predicate for fixed sized arrays of characters */
0705 static int filter_pred_string(struct filter_pred *pred, void *event)
0706 {
0707     char *addr = (char *)(event + pred->offset);
0708     int cmp, match;
0709 
0710     cmp = pred->regex.match(addr, &pred->regex, pred->regex.field_len);
0711 
0712     match = cmp ^ pred->not;
0713 
0714     return match;
0715 }
0716 
0717 static __always_inline int filter_pchar(struct filter_pred *pred, char *str)
0718 {
0719     int cmp, match;
0720     int len;
0721 
0722     len = strlen(str) + 1;  /* including tailing '\0' */
0723     cmp = pred->regex.match(str, &pred->regex, len);
0724 
0725     match = cmp ^ pred->not;
0726 
0727     return match;
0728 }
0729 /* Filter predicate for char * pointers */
0730 static int filter_pred_pchar(struct filter_pred *pred, void *event)
0731 {
0732     char **addr = (char **)(event + pred->offset);
0733     char *str;
0734 
0735     str = test_string(*addr);
0736     if (!str)
0737         return 0;
0738 
0739     return filter_pchar(pred, str);
0740 }
0741 
0742 /* Filter predicate for char * pointers in user space*/
0743 static int filter_pred_pchar_user(struct filter_pred *pred, void *event)
0744 {
0745     char **addr = (char **)(event + pred->offset);
0746     char *str;
0747 
0748     str = test_ustring(*addr);
0749     if (!str)
0750         return 0;
0751 
0752     return filter_pchar(pred, str);
0753 }
0754 
0755 /*
0756  * Filter predicate for dynamic sized arrays of characters.
0757  * These are implemented through a list of strings at the end
0758  * of the entry.
0759  * Also each of these strings have a field in the entry which
0760  * contains its offset from the beginning of the entry.
0761  * We have then first to get this field, dereference it
0762  * and add it to the address of the entry, and at last we have
0763  * the address of the string.
0764  */
0765 static int filter_pred_strloc(struct filter_pred *pred, void *event)
0766 {
0767     u32 str_item = *(u32 *)(event + pred->offset);
0768     int str_loc = str_item & 0xffff;
0769     int str_len = str_item >> 16;
0770     char *addr = (char *)(event + str_loc);
0771     int cmp, match;
0772 
0773     cmp = pred->regex.match(addr, &pred->regex, str_len);
0774 
0775     match = cmp ^ pred->not;
0776 
0777     return match;
0778 }
0779 
0780 /*
0781  * Filter predicate for relative dynamic sized arrays of characters.
0782  * These are implemented through a list of strings at the end
0783  * of the entry as same as dynamic string.
0784  * The difference is that the relative one records the location offset
0785  * from the field itself, not the event entry.
0786  */
0787 static int filter_pred_strrelloc(struct filter_pred *pred, void *event)
0788 {
0789     u32 *item = (u32 *)(event + pred->offset);
0790     u32 str_item = *item;
0791     int str_loc = str_item & 0xffff;
0792     int str_len = str_item >> 16;
0793     char *addr = (char *)(&item[1]) + str_loc;
0794     int cmp, match;
0795 
0796     cmp = pred->regex.match(addr, &pred->regex, str_len);
0797 
0798     match = cmp ^ pred->not;
0799 
0800     return match;
0801 }
0802 
0803 /* Filter predicate for CPUs. */
0804 static int filter_pred_cpu(struct filter_pred *pred, void *event)
0805 {
0806     int cpu, cmp;
0807 
0808     cpu = raw_smp_processor_id();
0809     cmp = pred->val;
0810 
0811     switch (pred->op) {
0812     case OP_EQ:
0813         return cpu == cmp;
0814     case OP_NE:
0815         return cpu != cmp;
0816     case OP_LT:
0817         return cpu < cmp;
0818     case OP_LE:
0819         return cpu <= cmp;
0820     case OP_GT:
0821         return cpu > cmp;
0822     case OP_GE:
0823         return cpu >= cmp;
0824     default:
0825         return 0;
0826     }
0827 }
0828 
0829 /* Filter predicate for COMM. */
0830 static int filter_pred_comm(struct filter_pred *pred, void *event)
0831 {
0832     int cmp;
0833 
0834     cmp = pred->regex.match(current->comm, &pred->regex,
0835                 TASK_COMM_LEN);
0836     return cmp ^ pred->not;
0837 }
0838 
0839 static int filter_pred_none(struct filter_pred *pred, void *event)
0840 {
0841     return 0;
0842 }
0843 
0844 /*
0845  * regex_match_foo - Basic regex callbacks
0846  *
0847  * @str: the string to be searched
0848  * @r:   the regex structure containing the pattern string
0849  * @len: the length of the string to be searched (including '\0')
0850  *
0851  * Note:
0852  * - @str might not be NULL-terminated if it's of type DYN_STRING
0853  *   RDYN_STRING, or STATIC_STRING, unless @len is zero.
0854  */
0855 
0856 static int regex_match_full(char *str, struct regex *r, int len)
0857 {
0858     /* len of zero means str is dynamic and ends with '\0' */
0859     if (!len)
0860         return strcmp(str, r->pattern) == 0;
0861 
0862     return strncmp(str, r->pattern, len) == 0;
0863 }
0864 
0865 static int regex_match_front(char *str, struct regex *r, int len)
0866 {
0867     if (len && len < r->len)
0868         return 0;
0869 
0870     return strncmp(str, r->pattern, r->len) == 0;
0871 }
0872 
0873 static int regex_match_middle(char *str, struct regex *r, int len)
0874 {
0875     if (!len)
0876         return strstr(str, r->pattern) != NULL;
0877 
0878     return strnstr(str, r->pattern, len) != NULL;
0879 }
0880 
0881 static int regex_match_end(char *str, struct regex *r, int len)
0882 {
0883     int strlen = len - 1;
0884 
0885     if (strlen >= r->len &&
0886         memcmp(str + strlen - r->len, r->pattern, r->len) == 0)
0887         return 1;
0888     return 0;
0889 }
0890 
0891 static int regex_match_glob(char *str, struct regex *r, int len __maybe_unused)
0892 {
0893     if (glob_match(r->pattern, str))
0894         return 1;
0895     return 0;
0896 }
0897 
0898 /**
0899  * filter_parse_regex - parse a basic regex
0900  * @buff:   the raw regex
0901  * @len:    length of the regex
0902  * @search: will point to the beginning of the string to compare
0903  * @not:    tell whether the match will have to be inverted
0904  *
0905  * This passes in a buffer containing a regex and this function will
0906  * set search to point to the search part of the buffer and
0907  * return the type of search it is (see enum above).
0908  * This does modify buff.
0909  *
0910  * Returns enum type.
0911  *  search returns the pointer to use for comparison.
0912  *  not returns 1 if buff started with a '!'
0913  *     0 otherwise.
0914  */
0915 enum regex_type filter_parse_regex(char *buff, int len, char **search, int *not)
0916 {
0917     int type = MATCH_FULL;
0918     int i;
0919 
0920     if (buff[0] == '!') {
0921         *not = 1;
0922         buff++;
0923         len--;
0924     } else
0925         *not = 0;
0926 
0927     *search = buff;
0928 
0929     if (isdigit(buff[0]))
0930         return MATCH_INDEX;
0931 
0932     for (i = 0; i < len; i++) {
0933         if (buff[i] == '*') {
0934             if (!i) {
0935                 type = MATCH_END_ONLY;
0936             } else if (i == len - 1) {
0937                 if (type == MATCH_END_ONLY)
0938                     type = MATCH_MIDDLE_ONLY;
0939                 else
0940                     type = MATCH_FRONT_ONLY;
0941                 buff[i] = 0;
0942                 break;
0943             } else {    /* pattern continues, use full glob */
0944                 return MATCH_GLOB;
0945             }
0946         } else if (strchr("[?\\", buff[i])) {
0947             return MATCH_GLOB;
0948         }
0949     }
0950     if (buff[0] == '*')
0951         *search = buff + 1;
0952 
0953     return type;
0954 }
0955 
0956 static void filter_build_regex(struct filter_pred *pred)
0957 {
0958     struct regex *r = &pred->regex;
0959     char *search;
0960     enum regex_type type = MATCH_FULL;
0961 
0962     if (pred->op == OP_GLOB) {
0963         type = filter_parse_regex(r->pattern, r->len, &search, &pred->not);
0964         r->len = strlen(search);
0965         memmove(r->pattern, search, r->len+1);
0966     }
0967 
0968     switch (type) {
0969     /* MATCH_INDEX should not happen, but if it does, match full */
0970     case MATCH_INDEX:
0971     case MATCH_FULL:
0972         r->match = regex_match_full;
0973         break;
0974     case MATCH_FRONT_ONLY:
0975         r->match = regex_match_front;
0976         break;
0977     case MATCH_MIDDLE_ONLY:
0978         r->match = regex_match_middle;
0979         break;
0980     case MATCH_END_ONLY:
0981         r->match = regex_match_end;
0982         break;
0983     case MATCH_GLOB:
0984         r->match = regex_match_glob;
0985         break;
0986     }
0987 }
0988 
0989 /* return 1 if event matches, 0 otherwise (discard) */
0990 int filter_match_preds(struct event_filter *filter, void *rec)
0991 {
0992     struct prog_entry *prog;
0993     int i;
0994 
0995     /* no filter is considered a match */
0996     if (!filter)
0997         return 1;
0998 
0999     /* Protected by either SRCU(tracepoint_srcu) or preempt_disable */
1000     prog = rcu_dereference_raw(filter->prog);
1001     if (!prog)
1002         return 1;
1003 
1004     for (i = 0; prog[i].pred; i++) {
1005         struct filter_pred *pred = prog[i].pred;
1006         int match = pred->fn(pred, rec);
1007         if (match == prog[i].when_to_branch)
1008             i = prog[i].target;
1009     }
1010     return prog[i].target;
1011 }
1012 EXPORT_SYMBOL_GPL(filter_match_preds);
1013 
1014 static void remove_filter_string(struct event_filter *filter)
1015 {
1016     if (!filter)
1017         return;
1018 
1019     kfree(filter->filter_string);
1020     filter->filter_string = NULL;
1021 }
1022 
1023 static void append_filter_err(struct trace_array *tr,
1024                   struct filter_parse_error *pe,
1025                   struct event_filter *filter)
1026 {
1027     struct trace_seq *s;
1028     int pos = pe->lasterr_pos;
1029     char *buf;
1030     int len;
1031 
1032     if (WARN_ON(!filter->filter_string))
1033         return;
1034 
1035     s = kmalloc(sizeof(*s), GFP_KERNEL);
1036     if (!s)
1037         return;
1038     trace_seq_init(s);
1039 
1040     len = strlen(filter->filter_string);
1041     if (pos > len)
1042         pos = len;
1043 
1044     /* indexing is off by one */
1045     if (pos)
1046         pos++;
1047 
1048     trace_seq_puts(s, filter->filter_string);
1049     if (pe->lasterr > 0) {
1050         trace_seq_printf(s, "\n%*s", pos, "^");
1051         trace_seq_printf(s, "\nparse_error: %s\n", err_text[pe->lasterr]);
1052         tracing_log_err(tr, "event filter parse error",
1053                 filter->filter_string, err_text,
1054                 pe->lasterr, pe->lasterr_pos);
1055     } else {
1056         trace_seq_printf(s, "\nError: (%d)\n", pe->lasterr);
1057         tracing_log_err(tr, "event filter parse error",
1058                 filter->filter_string, err_text,
1059                 FILT_ERR_ERRNO, 0);
1060     }
1061     trace_seq_putc(s, 0);
1062     buf = kmemdup_nul(s->buffer, s->seq.len, GFP_KERNEL);
1063     if (buf) {
1064         kfree(filter->filter_string);
1065         filter->filter_string = buf;
1066     }
1067     kfree(s);
1068 }
1069 
1070 static inline struct event_filter *event_filter(struct trace_event_file *file)
1071 {
1072     return file->filter;
1073 }
1074 
1075 /* caller must hold event_mutex */
1076 void print_event_filter(struct trace_event_file *file, struct trace_seq *s)
1077 {
1078     struct event_filter *filter = event_filter(file);
1079 
1080     if (filter && filter->filter_string)
1081         trace_seq_printf(s, "%s\n", filter->filter_string);
1082     else
1083         trace_seq_puts(s, "none\n");
1084 }
1085 
1086 void print_subsystem_event_filter(struct event_subsystem *system,
1087                   struct trace_seq *s)
1088 {
1089     struct event_filter *filter;
1090 
1091     mutex_lock(&event_mutex);
1092     filter = system->filter;
1093     if (filter && filter->filter_string)
1094         trace_seq_printf(s, "%s\n", filter->filter_string);
1095     else
1096         trace_seq_puts(s, DEFAULT_SYS_FILTER_MESSAGE "\n");
1097     mutex_unlock(&event_mutex);
1098 }
1099 
1100 static void free_prog(struct event_filter *filter)
1101 {
1102     struct prog_entry *prog;
1103     int i;
1104 
1105     prog = rcu_access_pointer(filter->prog);
1106     if (!prog)
1107         return;
1108 
1109     for (i = 0; prog[i].pred; i++)
1110         kfree(prog[i].pred);
1111     kfree(prog);
1112 }
1113 
1114 static void filter_disable(struct trace_event_file *file)
1115 {
1116     unsigned long old_flags = file->flags;
1117 
1118     file->flags &= ~EVENT_FILE_FL_FILTERED;
1119 
1120     if (old_flags != file->flags)
1121         trace_buffered_event_disable();
1122 }
1123 
1124 static void __free_filter(struct event_filter *filter)
1125 {
1126     if (!filter)
1127         return;
1128 
1129     free_prog(filter);
1130     kfree(filter->filter_string);
1131     kfree(filter);
1132 }
1133 
1134 void free_event_filter(struct event_filter *filter)
1135 {
1136     __free_filter(filter);
1137 }
1138 
1139 static inline void __remove_filter(struct trace_event_file *file)
1140 {
1141     filter_disable(file);
1142     remove_filter_string(file->filter);
1143 }
1144 
1145 static void filter_free_subsystem_preds(struct trace_subsystem_dir *dir,
1146                     struct trace_array *tr)
1147 {
1148     struct trace_event_file *file;
1149 
1150     list_for_each_entry(file, &tr->events, list) {
1151         if (file->system != dir)
1152             continue;
1153         __remove_filter(file);
1154     }
1155 }
1156 
1157 static inline void __free_subsystem_filter(struct trace_event_file *file)
1158 {
1159     __free_filter(file->filter);
1160     file->filter = NULL;
1161 }
1162 
1163 static void filter_free_subsystem_filters(struct trace_subsystem_dir *dir,
1164                       struct trace_array *tr)
1165 {
1166     struct trace_event_file *file;
1167 
1168     list_for_each_entry(file, &tr->events, list) {
1169         if (file->system != dir)
1170             continue;
1171         __free_subsystem_filter(file);
1172     }
1173 }
1174 
1175 int filter_assign_type(const char *type)
1176 {
1177     if (strstr(type, "__data_loc") && strstr(type, "char"))
1178         return FILTER_DYN_STRING;
1179 
1180     if (strstr(type, "__rel_loc") && strstr(type, "char"))
1181         return FILTER_RDYN_STRING;
1182 
1183     if (strchr(type, '[') && strstr(type, "char"))
1184         return FILTER_STATIC_STRING;
1185 
1186     if (strcmp(type, "char *") == 0 || strcmp(type, "const char *") == 0)
1187         return FILTER_PTR_STRING;
1188 
1189     return FILTER_OTHER;
1190 }
1191 
1192 static filter_pred_fn_t select_comparison_fn(enum filter_op_ids op,
1193                         int field_size, int field_is_signed)
1194 {
1195     filter_pred_fn_t fn = NULL;
1196     int pred_func_index = -1;
1197 
1198     switch (op) {
1199     case OP_EQ:
1200     case OP_NE:
1201         break;
1202     default:
1203         if (WARN_ON_ONCE(op < PRED_FUNC_START))
1204             return NULL;
1205         pred_func_index = op - PRED_FUNC_START;
1206         if (WARN_ON_ONCE(pred_func_index > PRED_FUNC_MAX))
1207             return NULL;
1208     }
1209 
1210     switch (field_size) {
1211     case 8:
1212         if (pred_func_index < 0)
1213             fn = filter_pred_64;
1214         else if (field_is_signed)
1215             fn = pred_funcs_s64[pred_func_index];
1216         else
1217             fn = pred_funcs_u64[pred_func_index];
1218         break;
1219     case 4:
1220         if (pred_func_index < 0)
1221             fn = filter_pred_32;
1222         else if (field_is_signed)
1223             fn = pred_funcs_s32[pred_func_index];
1224         else
1225             fn = pred_funcs_u32[pred_func_index];
1226         break;
1227     case 2:
1228         if (pred_func_index < 0)
1229             fn = filter_pred_16;
1230         else if (field_is_signed)
1231             fn = pred_funcs_s16[pred_func_index];
1232         else
1233             fn = pred_funcs_u16[pred_func_index];
1234         break;
1235     case 1:
1236         if (pred_func_index < 0)
1237             fn = filter_pred_8;
1238         else if (field_is_signed)
1239             fn = pred_funcs_s8[pred_func_index];
1240         else
1241             fn = pred_funcs_u8[pred_func_index];
1242         break;
1243     }
1244 
1245     return fn;
1246 }
1247 
1248 /* Called when a predicate is encountered by predicate_parse() */
1249 static int parse_pred(const char *str, void *data,
1250               int pos, struct filter_parse_error *pe,
1251               struct filter_pred **pred_ptr)
1252 {
1253     struct trace_event_call *call = data;
1254     struct ftrace_event_field *field;
1255     struct filter_pred *pred = NULL;
1256     char num_buf[24];   /* Big enough to hold an address */
1257     char *field_name;
1258     bool ustring = false;
1259     char q;
1260     u64 val;
1261     int len;
1262     int ret;
1263     int op;
1264     int s;
1265     int i = 0;
1266 
1267     /* First find the field to associate to */
1268     while (isspace(str[i]))
1269         i++;
1270     s = i;
1271 
1272     while (isalnum(str[i]) || str[i] == '_')
1273         i++;
1274 
1275     len = i - s;
1276 
1277     if (!len)
1278         return -1;
1279 
1280     field_name = kmemdup_nul(str + s, len, GFP_KERNEL);
1281     if (!field_name)
1282         return -ENOMEM;
1283 
1284     /* Make sure that the field exists */
1285 
1286     field = trace_find_event_field(call, field_name);
1287     kfree(field_name);
1288     if (!field) {
1289         parse_error(pe, FILT_ERR_FIELD_NOT_FOUND, pos + i);
1290         return -EINVAL;
1291     }
1292 
1293     /* See if the field is a user space string */
1294     if ((len = str_has_prefix(str + i, ".ustring"))) {
1295         ustring = true;
1296         i += len;
1297     }
1298 
1299     while (isspace(str[i]))
1300         i++;
1301 
1302     /* Make sure this op is supported */
1303     for (op = 0; ops[op]; op++) {
1304         /* This is why '<=' must come before '<' in ops[] */
1305         if (strncmp(str + i, ops[op], strlen(ops[op])) == 0)
1306             break;
1307     }
1308 
1309     if (!ops[op]) {
1310         parse_error(pe, FILT_ERR_INVALID_OP, pos + i);
1311         goto err_free;
1312     }
1313 
1314     i += strlen(ops[op]);
1315 
1316     while (isspace(str[i]))
1317         i++;
1318 
1319     s = i;
1320 
1321     pred = kzalloc(sizeof(*pred), GFP_KERNEL);
1322     if (!pred)
1323         return -ENOMEM;
1324 
1325     pred->field = field;
1326     pred->offset = field->offset;
1327     pred->op = op;
1328 
1329     if (ftrace_event_is_function(call)) {
1330         /*
1331          * Perf does things different with function events.
1332          * It only allows an "ip" field, and expects a string.
1333          * But the string does not need to be surrounded by quotes.
1334          * If it is a string, the assigned function as a nop,
1335          * (perf doesn't use it) and grab everything.
1336          */
1337         if (strcmp(field->name, "ip") != 0) {
1338             parse_error(pe, FILT_ERR_IP_FIELD_ONLY, pos + i);
1339             goto err_free;
1340         }
1341         pred->fn = filter_pred_none;
1342 
1343         /*
1344          * Quotes are not required, but if they exist then we need
1345          * to read them till we hit a matching one.
1346          */
1347         if (str[i] == '\'' || str[i] == '"')
1348             q = str[i];
1349         else
1350             q = 0;
1351 
1352         for (i++; str[i]; i++) {
1353             if (q && str[i] == q)
1354                 break;
1355             if (!q && (str[i] == ')' || str[i] == '&' ||
1356                    str[i] == '|'))
1357                 break;
1358         }
1359         /* Skip quotes */
1360         if (q)
1361             s++;
1362         len = i - s;
1363         if (len >= MAX_FILTER_STR_VAL) {
1364             parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1365             goto err_free;
1366         }
1367 
1368         pred->regex.len = len;
1369         strncpy(pred->regex.pattern, str + s, len);
1370         pred->regex.pattern[len] = 0;
1371 
1372     /* This is either a string, or an integer */
1373     } else if (str[i] == '\'' || str[i] == '"') {
1374         char q = str[i];
1375 
1376         /* Make sure the op is OK for strings */
1377         switch (op) {
1378         case OP_NE:
1379             pred->not = 1;
1380             fallthrough;
1381         case OP_GLOB:
1382         case OP_EQ:
1383             break;
1384         default:
1385             parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
1386             goto err_free;
1387         }
1388 
1389         /* Make sure the field is OK for strings */
1390         if (!is_string_field(field)) {
1391             parse_error(pe, FILT_ERR_EXPECT_DIGIT, pos + i);
1392             goto err_free;
1393         }
1394 
1395         for (i++; str[i]; i++) {
1396             if (str[i] == q)
1397                 break;
1398         }
1399         if (!str[i]) {
1400             parse_error(pe, FILT_ERR_MISSING_QUOTE, pos + i);
1401             goto err_free;
1402         }
1403 
1404         /* Skip quotes */
1405         s++;
1406         len = i - s;
1407         if (len >= MAX_FILTER_STR_VAL) {
1408             parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1409             goto err_free;
1410         }
1411 
1412         pred->regex.len = len;
1413         strncpy(pred->regex.pattern, str + s, len);
1414         pred->regex.pattern[len] = 0;
1415 
1416         filter_build_regex(pred);
1417 
1418         if (field->filter_type == FILTER_COMM) {
1419             pred->fn = filter_pred_comm;
1420 
1421         } else if (field->filter_type == FILTER_STATIC_STRING) {
1422             pred->fn = filter_pred_string;
1423             pred->regex.field_len = field->size;
1424 
1425         } else if (field->filter_type == FILTER_DYN_STRING) {
1426             pred->fn = filter_pred_strloc;
1427         } else if (field->filter_type == FILTER_RDYN_STRING)
1428             pred->fn = filter_pred_strrelloc;
1429         else {
1430 
1431             if (!ustring_per_cpu) {
1432                 /* Once allocated, keep it around for good */
1433                 ustring_per_cpu = alloc_percpu(struct ustring_buffer);
1434                 if (!ustring_per_cpu)
1435                     goto err_mem;
1436             }
1437 
1438             if (ustring)
1439                 pred->fn = filter_pred_pchar_user;
1440             else
1441                 pred->fn = filter_pred_pchar;
1442         }
1443         /* go past the last quote */
1444         i++;
1445 
1446     } else if (isdigit(str[i]) || str[i] == '-') {
1447 
1448         /* Make sure the field is not a string */
1449         if (is_string_field(field)) {
1450             parse_error(pe, FILT_ERR_EXPECT_STRING, pos + i);
1451             goto err_free;
1452         }
1453 
1454         if (op == OP_GLOB) {
1455             parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
1456             goto err_free;
1457         }
1458 
1459         if (str[i] == '-')
1460             i++;
1461 
1462         /* We allow 0xDEADBEEF */
1463         while (isalnum(str[i]))
1464             i++;
1465 
1466         len = i - s;
1467         /* 0xfeedfacedeadbeef is 18 chars max */
1468         if (len >= sizeof(num_buf)) {
1469             parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1470             goto err_free;
1471         }
1472 
1473         strncpy(num_buf, str + s, len);
1474         num_buf[len] = 0;
1475 
1476         /* Make sure it is a value */
1477         if (field->is_signed)
1478             ret = kstrtoll(num_buf, 0, &val);
1479         else
1480             ret = kstrtoull(num_buf, 0, &val);
1481         if (ret) {
1482             parse_error(pe, FILT_ERR_ILLEGAL_INTVAL, pos + s);
1483             goto err_free;
1484         }
1485 
1486         pred->val = val;
1487 
1488         if (field->filter_type == FILTER_CPU)
1489             pred->fn = filter_pred_cpu;
1490         else {
1491             pred->fn = select_comparison_fn(pred->op, field->size,
1492                             field->is_signed);
1493             if (pred->op == OP_NE)
1494                 pred->not = 1;
1495         }
1496 
1497     } else {
1498         parse_error(pe, FILT_ERR_INVALID_VALUE, pos + i);
1499         goto err_free;
1500     }
1501 
1502     *pred_ptr = pred;
1503     return i;
1504 
1505 err_free:
1506     kfree(pred);
1507     return -EINVAL;
1508 err_mem:
1509     kfree(pred);
1510     return -ENOMEM;
1511 }
1512 
1513 enum {
1514     TOO_MANY_CLOSE      = -1,
1515     TOO_MANY_OPEN       = -2,
1516     MISSING_QUOTE       = -3,
1517 };
1518 
1519 /*
1520  * Read the filter string once to calculate the number of predicates
1521  * as well as how deep the parentheses go.
1522  *
1523  * Returns:
1524  *   0 - everything is fine (err is undefined)
1525  *  -1 - too many ')'
1526  *  -2 - too many '('
1527  *  -3 - No matching quote
1528  */
1529 static int calc_stack(const char *str, int *parens, int *preds, int *err)
1530 {
1531     bool is_pred = false;
1532     int nr_preds = 0;
1533     int open = 1; /* Count the expression as "(E)" */
1534     int last_quote = 0;
1535     int max_open = 1;
1536     int quote = 0;
1537     int i;
1538 
1539     *err = 0;
1540 
1541     for (i = 0; str[i]; i++) {
1542         if (isspace(str[i]))
1543             continue;
1544         if (quote) {
1545             if (str[i] == quote)
1546                    quote = 0;
1547             continue;
1548         }
1549 
1550         switch (str[i]) {
1551         case '\'':
1552         case '"':
1553             quote = str[i];
1554             last_quote = i;
1555             break;
1556         case '|':
1557         case '&':
1558             if (str[i+1] != str[i])
1559                 break;
1560             is_pred = false;
1561             continue;
1562         case '(':
1563             is_pred = false;
1564             open++;
1565             if (open > max_open)
1566                 max_open = open;
1567             continue;
1568         case ')':
1569             is_pred = false;
1570             if (open == 1) {
1571                 *err = i;
1572                 return TOO_MANY_CLOSE;
1573             }
1574             open--;
1575             continue;
1576         }
1577         if (!is_pred) {
1578             nr_preds++;
1579             is_pred = true;
1580         }
1581     }
1582 
1583     if (quote) {
1584         *err = last_quote;
1585         return MISSING_QUOTE;
1586     }
1587 
1588     if (open != 1) {
1589         int level = open;
1590 
1591         /* find the bad open */
1592         for (i--; i; i--) {
1593             if (quote) {
1594                 if (str[i] == quote)
1595                     quote = 0;
1596                 continue;
1597             }
1598             switch (str[i]) {
1599             case '(':
1600                 if (level == open) {
1601                     *err = i;
1602                     return TOO_MANY_OPEN;
1603                 }
1604                 level--;
1605                 break;
1606             case ')':
1607                 level++;
1608                 break;
1609             case '\'':
1610             case '"':
1611                 quote = str[i];
1612                 break;
1613             }
1614         }
1615         /* First character is the '(' with missing ')' */
1616         *err = 0;
1617         return TOO_MANY_OPEN;
1618     }
1619 
1620     /* Set the size of the required stacks */
1621     *parens = max_open;
1622     *preds = nr_preds;
1623     return 0;
1624 }
1625 
1626 static int process_preds(struct trace_event_call *call,
1627              const char *filter_string,
1628              struct event_filter *filter,
1629              struct filter_parse_error *pe)
1630 {
1631     struct prog_entry *prog;
1632     int nr_parens;
1633     int nr_preds;
1634     int index;
1635     int ret;
1636 
1637     ret = calc_stack(filter_string, &nr_parens, &nr_preds, &index);
1638     if (ret < 0) {
1639         switch (ret) {
1640         case MISSING_QUOTE:
1641             parse_error(pe, FILT_ERR_MISSING_QUOTE, index);
1642             break;
1643         case TOO_MANY_OPEN:
1644             parse_error(pe, FILT_ERR_TOO_MANY_OPEN, index);
1645             break;
1646         default:
1647             parse_error(pe, FILT_ERR_TOO_MANY_CLOSE, index);
1648         }
1649         return ret;
1650     }
1651 
1652     if (!nr_preds)
1653         return -EINVAL;
1654 
1655     prog = predicate_parse(filter_string, nr_parens, nr_preds,
1656                    parse_pred, call, pe);
1657     if (IS_ERR(prog))
1658         return PTR_ERR(prog);
1659 
1660     rcu_assign_pointer(filter->prog, prog);
1661     return 0;
1662 }
1663 
1664 static inline void event_set_filtered_flag(struct trace_event_file *file)
1665 {
1666     unsigned long old_flags = file->flags;
1667 
1668     file->flags |= EVENT_FILE_FL_FILTERED;
1669 
1670     if (old_flags != file->flags)
1671         trace_buffered_event_enable();
1672 }
1673 
1674 static inline void event_set_filter(struct trace_event_file *file,
1675                     struct event_filter *filter)
1676 {
1677     rcu_assign_pointer(file->filter, filter);
1678 }
1679 
1680 static inline void event_clear_filter(struct trace_event_file *file)
1681 {
1682     RCU_INIT_POINTER(file->filter, NULL);
1683 }
1684 
1685 struct filter_list {
1686     struct list_head    list;
1687     struct event_filter *filter;
1688 };
1689 
1690 static int process_system_preds(struct trace_subsystem_dir *dir,
1691                 struct trace_array *tr,
1692                 struct filter_parse_error *pe,
1693                 char *filter_string)
1694 {
1695     struct trace_event_file *file;
1696     struct filter_list *filter_item;
1697     struct event_filter *filter = NULL;
1698     struct filter_list *tmp;
1699     LIST_HEAD(filter_list);
1700     bool fail = true;
1701     int err;
1702 
1703     list_for_each_entry(file, &tr->events, list) {
1704 
1705         if (file->system != dir)
1706             continue;
1707 
1708         filter = kzalloc(sizeof(*filter), GFP_KERNEL);
1709         if (!filter)
1710             goto fail_mem;
1711 
1712         filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
1713         if (!filter->filter_string)
1714             goto fail_mem;
1715 
1716         err = process_preds(file->event_call, filter_string, filter, pe);
1717         if (err) {
1718             filter_disable(file);
1719             parse_error(pe, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1720             append_filter_err(tr, pe, filter);
1721         } else
1722             event_set_filtered_flag(file);
1723 
1724 
1725         filter_item = kzalloc(sizeof(*filter_item), GFP_KERNEL);
1726         if (!filter_item)
1727             goto fail_mem;
1728 
1729         list_add_tail(&filter_item->list, &filter_list);
1730         /*
1731          * Regardless of if this returned an error, we still
1732          * replace the filter for the call.
1733          */
1734         filter_item->filter = event_filter(file);
1735         event_set_filter(file, filter);
1736         filter = NULL;
1737 
1738         fail = false;
1739     }
1740 
1741     if (fail)
1742         goto fail;
1743 
1744     /*
1745      * The calls can still be using the old filters.
1746      * Do a synchronize_rcu() and to ensure all calls are
1747      * done with them before we free them.
1748      */
1749     tracepoint_synchronize_unregister();
1750     list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1751         __free_filter(filter_item->filter);
1752         list_del(&filter_item->list);
1753         kfree(filter_item);
1754     }
1755     return 0;
1756  fail:
1757     /* No call succeeded */
1758     list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1759         list_del(&filter_item->list);
1760         kfree(filter_item);
1761     }
1762     parse_error(pe, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1763     return -EINVAL;
1764  fail_mem:
1765     __free_filter(filter);
1766     /* If any call succeeded, we still need to sync */
1767     if (!fail)
1768         tracepoint_synchronize_unregister();
1769     list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1770         __free_filter(filter_item->filter);
1771         list_del(&filter_item->list);
1772         kfree(filter_item);
1773     }
1774     return -ENOMEM;
1775 }
1776 
1777 static int create_filter_start(char *filter_string, bool set_str,
1778                    struct filter_parse_error **pse,
1779                    struct event_filter **filterp)
1780 {
1781     struct event_filter *filter;
1782     struct filter_parse_error *pe = NULL;
1783     int err = 0;
1784 
1785     if (WARN_ON_ONCE(*pse || *filterp))
1786         return -EINVAL;
1787 
1788     filter = kzalloc(sizeof(*filter), GFP_KERNEL);
1789     if (filter && set_str) {
1790         filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
1791         if (!filter->filter_string)
1792             err = -ENOMEM;
1793     }
1794 
1795     pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1796 
1797     if (!filter || !pe || err) {
1798         kfree(pe);
1799         __free_filter(filter);
1800         return -ENOMEM;
1801     }
1802 
1803     /* we're committed to creating a new filter */
1804     *filterp = filter;
1805     *pse = pe;
1806 
1807     return 0;
1808 }
1809 
1810 static void create_filter_finish(struct filter_parse_error *pe)
1811 {
1812     kfree(pe);
1813 }
1814 
1815 /**
1816  * create_filter - create a filter for a trace_event_call
1817  * @tr: the trace array associated with these events
1818  * @call: trace_event_call to create a filter for
1819  * @filter_string: filter string
1820  * @set_str: remember @filter_str and enable detailed error in filter
1821  * @filterp: out param for created filter (always updated on return)
1822  *           Must be a pointer that references a NULL pointer.
1823  *
1824  * Creates a filter for @call with @filter_str.  If @set_str is %true,
1825  * @filter_str is copied and recorded in the new filter.
1826  *
1827  * On success, returns 0 and *@filterp points to the new filter.  On
1828  * failure, returns -errno and *@filterp may point to %NULL or to a new
1829  * filter.  In the latter case, the returned filter contains error
1830  * information if @set_str is %true and the caller is responsible for
1831  * freeing it.
1832  */
1833 static int create_filter(struct trace_array *tr,
1834              struct trace_event_call *call,
1835              char *filter_string, bool set_str,
1836              struct event_filter **filterp)
1837 {
1838     struct filter_parse_error *pe = NULL;
1839     int err;
1840 
1841     /* filterp must point to NULL */
1842     if (WARN_ON(*filterp))
1843         *filterp = NULL;
1844 
1845     err = create_filter_start(filter_string, set_str, &pe, filterp);
1846     if (err)
1847         return err;
1848 
1849     err = process_preds(call, filter_string, *filterp, pe);
1850     if (err && set_str)
1851         append_filter_err(tr, pe, *filterp);
1852     create_filter_finish(pe);
1853 
1854     return err;
1855 }
1856 
1857 int create_event_filter(struct trace_array *tr,
1858             struct trace_event_call *call,
1859             char *filter_str, bool set_str,
1860             struct event_filter **filterp)
1861 {
1862     return create_filter(tr, call, filter_str, set_str, filterp);
1863 }
1864 
1865 /**
1866  * create_system_filter - create a filter for an event subsystem
1867  * @dir: the descriptor for the subsystem directory
1868  * @filter_str: filter string
1869  * @filterp: out param for created filter (always updated on return)
1870  *
1871  * Identical to create_filter() except that it creates a subsystem filter
1872  * and always remembers @filter_str.
1873  */
1874 static int create_system_filter(struct trace_subsystem_dir *dir,
1875                 char *filter_str, struct event_filter **filterp)
1876 {
1877     struct filter_parse_error *pe = NULL;
1878     int err;
1879 
1880     err = create_filter_start(filter_str, true, &pe, filterp);
1881     if (!err) {
1882         err = process_system_preds(dir, dir->tr, pe, filter_str);
1883         if (!err) {
1884             /* System filters just show a default message */
1885             kfree((*filterp)->filter_string);
1886             (*filterp)->filter_string = NULL;
1887         } else {
1888             append_filter_err(dir->tr, pe, *filterp);
1889         }
1890     }
1891     create_filter_finish(pe);
1892 
1893     return err;
1894 }
1895 
1896 /* caller must hold event_mutex */
1897 int apply_event_filter(struct trace_event_file *file, char *filter_string)
1898 {
1899     struct trace_event_call *call = file->event_call;
1900     struct event_filter *filter = NULL;
1901     int err;
1902 
1903     if (!strcmp(strstrip(filter_string), "0")) {
1904         filter_disable(file);
1905         filter = event_filter(file);
1906 
1907         if (!filter)
1908             return 0;
1909 
1910         event_clear_filter(file);
1911 
1912         /* Make sure the filter is not being used */
1913         tracepoint_synchronize_unregister();
1914         __free_filter(filter);
1915 
1916         return 0;
1917     }
1918 
1919     err = create_filter(file->tr, call, filter_string, true, &filter);
1920 
1921     /*
1922      * Always swap the call filter with the new filter
1923      * even if there was an error. If there was an error
1924      * in the filter, we disable the filter and show the error
1925      * string
1926      */
1927     if (filter) {
1928         struct event_filter *tmp;
1929 
1930         tmp = event_filter(file);
1931         if (!err)
1932             event_set_filtered_flag(file);
1933         else
1934             filter_disable(file);
1935 
1936         event_set_filter(file, filter);
1937 
1938         if (tmp) {
1939             /* Make sure the call is done with the filter */
1940             tracepoint_synchronize_unregister();
1941             __free_filter(tmp);
1942         }
1943     }
1944 
1945     return err;
1946 }
1947 
1948 int apply_subsystem_event_filter(struct trace_subsystem_dir *dir,
1949                  char *filter_string)
1950 {
1951     struct event_subsystem *system = dir->subsystem;
1952     struct trace_array *tr = dir->tr;
1953     struct event_filter *filter = NULL;
1954     int err = 0;
1955 
1956     mutex_lock(&event_mutex);
1957 
1958     /* Make sure the system still has events */
1959     if (!dir->nr_events) {
1960         err = -ENODEV;
1961         goto out_unlock;
1962     }
1963 
1964     if (!strcmp(strstrip(filter_string), "0")) {
1965         filter_free_subsystem_preds(dir, tr);
1966         remove_filter_string(system->filter);
1967         filter = system->filter;
1968         system->filter = NULL;
1969         /* Ensure all filters are no longer used */
1970         tracepoint_synchronize_unregister();
1971         filter_free_subsystem_filters(dir, tr);
1972         __free_filter(filter);
1973         goto out_unlock;
1974     }
1975 
1976     err = create_system_filter(dir, filter_string, &filter);
1977     if (filter) {
1978         /*
1979          * No event actually uses the system filter
1980          * we can free it without synchronize_rcu().
1981          */
1982         __free_filter(system->filter);
1983         system->filter = filter;
1984     }
1985 out_unlock:
1986     mutex_unlock(&event_mutex);
1987 
1988     return err;
1989 }
1990 
1991 #ifdef CONFIG_PERF_EVENTS
1992 
1993 void ftrace_profile_free_filter(struct perf_event *event)
1994 {
1995     struct event_filter *filter = event->filter;
1996 
1997     event->filter = NULL;
1998     __free_filter(filter);
1999 }
2000 
2001 struct function_filter_data {
2002     struct ftrace_ops *ops;
2003     int first_filter;
2004     int first_notrace;
2005 };
2006 
2007 #ifdef CONFIG_FUNCTION_TRACER
2008 static char **
2009 ftrace_function_filter_re(char *buf, int len, int *count)
2010 {
2011     char *str, **re;
2012 
2013     str = kstrndup(buf, len, GFP_KERNEL);
2014     if (!str)
2015         return NULL;
2016 
2017     /*
2018      * The argv_split function takes white space
2019      * as a separator, so convert ',' into spaces.
2020      */
2021     strreplace(str, ',', ' ');
2022 
2023     re = argv_split(GFP_KERNEL, str, count);
2024     kfree(str);
2025     return re;
2026 }
2027 
2028 static int ftrace_function_set_regexp(struct ftrace_ops *ops, int filter,
2029                       int reset, char *re, int len)
2030 {
2031     int ret;
2032 
2033     if (filter)
2034         ret = ftrace_set_filter(ops, re, len, reset);
2035     else
2036         ret = ftrace_set_notrace(ops, re, len, reset);
2037 
2038     return ret;
2039 }
2040 
2041 static int __ftrace_function_set_filter(int filter, char *buf, int len,
2042                     struct function_filter_data *data)
2043 {
2044     int i, re_cnt, ret = -EINVAL;
2045     int *reset;
2046     char **re;
2047 
2048     reset = filter ? &data->first_filter : &data->first_notrace;
2049 
2050     /*
2051      * The 'ip' field could have multiple filters set, separated
2052      * either by space or comma. We first cut the filter and apply
2053      * all pieces separately.
2054      */
2055     re = ftrace_function_filter_re(buf, len, &re_cnt);
2056     if (!re)
2057         return -EINVAL;
2058 
2059     for (i = 0; i < re_cnt; i++) {
2060         ret = ftrace_function_set_regexp(data->ops, filter, *reset,
2061                          re[i], strlen(re[i]));
2062         if (ret)
2063             break;
2064 
2065         if (*reset)
2066             *reset = 0;
2067     }
2068 
2069     argv_free(re);
2070     return ret;
2071 }
2072 
2073 static int ftrace_function_check_pred(struct filter_pred *pred)
2074 {
2075     struct ftrace_event_field *field = pred->field;
2076 
2077     /*
2078      * Check the predicate for function trace, verify:
2079      *  - only '==' and '!=' is used
2080      *  - the 'ip' field is used
2081      */
2082     if ((pred->op != OP_EQ) && (pred->op != OP_NE))
2083         return -EINVAL;
2084 
2085     if (strcmp(field->name, "ip"))
2086         return -EINVAL;
2087 
2088     return 0;
2089 }
2090 
2091 static int ftrace_function_set_filter_pred(struct filter_pred *pred,
2092                        struct function_filter_data *data)
2093 {
2094     int ret;
2095 
2096     /* Checking the node is valid for function trace. */
2097     ret = ftrace_function_check_pred(pred);
2098     if (ret)
2099         return ret;
2100 
2101     return __ftrace_function_set_filter(pred->op == OP_EQ,
2102                         pred->regex.pattern,
2103                         pred->regex.len,
2104                         data);
2105 }
2106 
2107 static bool is_or(struct prog_entry *prog, int i)
2108 {
2109     int target;
2110 
2111     /*
2112      * Only "||" is allowed for function events, thus,
2113      * all true branches should jump to true, and any
2114      * false branch should jump to false.
2115      */
2116     target = prog[i].target + 1;
2117     /* True and false have NULL preds (all prog entries should jump to one */
2118     if (prog[target].pred)
2119         return false;
2120 
2121     /* prog[target].target is 1 for TRUE, 0 for FALSE */
2122     return prog[i].when_to_branch == prog[target].target;
2123 }
2124 
2125 static int ftrace_function_set_filter(struct perf_event *event,
2126                       struct event_filter *filter)
2127 {
2128     struct prog_entry *prog = rcu_dereference_protected(filter->prog,
2129                         lockdep_is_held(&event_mutex));
2130     struct function_filter_data data = {
2131         .first_filter  = 1,
2132         .first_notrace = 1,
2133         .ops           = &event->ftrace_ops,
2134     };
2135     int i;
2136 
2137     for (i = 0; prog[i].pred; i++) {
2138         struct filter_pred *pred = prog[i].pred;
2139 
2140         if (!is_or(prog, i))
2141             return -EINVAL;
2142 
2143         if (ftrace_function_set_filter_pred(pred, &data) < 0)
2144             return -EINVAL;
2145     }
2146     return 0;
2147 }
2148 #else
2149 static int ftrace_function_set_filter(struct perf_event *event,
2150                       struct event_filter *filter)
2151 {
2152     return -ENODEV;
2153 }
2154 #endif /* CONFIG_FUNCTION_TRACER */
2155 
2156 int ftrace_profile_set_filter(struct perf_event *event, int event_id,
2157                   char *filter_str)
2158 {
2159     int err;
2160     struct event_filter *filter = NULL;
2161     struct trace_event_call *call;
2162 
2163     mutex_lock(&event_mutex);
2164 
2165     call = event->tp_event;
2166 
2167     err = -EINVAL;
2168     if (!call)
2169         goto out_unlock;
2170 
2171     err = -EEXIST;
2172     if (event->filter)
2173         goto out_unlock;
2174 
2175     err = create_filter(NULL, call, filter_str, false, &filter);
2176     if (err)
2177         goto free_filter;
2178 
2179     if (ftrace_event_is_function(call))
2180         err = ftrace_function_set_filter(event, filter);
2181     else
2182         event->filter = filter;
2183 
2184 free_filter:
2185     if (err || ftrace_event_is_function(call))
2186         __free_filter(filter);
2187 
2188 out_unlock:
2189     mutex_unlock(&event_mutex);
2190 
2191     return err;
2192 }
2193 
2194 #endif /* CONFIG_PERF_EVENTS */
2195 
2196 #ifdef CONFIG_FTRACE_STARTUP_TEST
2197 
2198 #include <linux/types.h>
2199 #include <linux/tracepoint.h>
2200 
2201 #define CREATE_TRACE_POINTS
2202 #include "trace_events_filter_test.h"
2203 
2204 #define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \
2205 { \
2206     .filter = FILTER, \
2207     .rec    = { .a = va, .b = vb, .c = vc, .d = vd, \
2208             .e = ve, .f = vf, .g = vg, .h = vh }, \
2209     .match  = m, \
2210     .not_visited = nvisit, \
2211 }
2212 #define YES 1
2213 #define NO  0
2214 
2215 static struct test_filter_data_t {
2216     char *filter;
2217     struct trace_event_raw_ftrace_test_filter rec;
2218     int match;
2219     char *not_visited;
2220 } test_filter_data[] = {
2221 #define FILTER "a == 1 && b == 1 && c == 1 && d == 1 && " \
2222            "e == 1 && f == 1 && g == 1 && h == 1"
2223     DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, ""),
2224     DATA_REC(NO,  0, 1, 1, 1, 1, 1, 1, 1, "bcdefgh"),
2225     DATA_REC(NO,  1, 1, 1, 1, 1, 1, 1, 0, ""),
2226 #undef FILTER
2227 #define FILTER "a == 1 || b == 1 || c == 1 || d == 1 || " \
2228            "e == 1 || f == 1 || g == 1 || h == 1"
2229     DATA_REC(NO,  0, 0, 0, 0, 0, 0, 0, 0, ""),
2230     DATA_REC(YES, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2231     DATA_REC(YES, 1, 0, 0, 0, 0, 0, 0, 0, "bcdefgh"),
2232 #undef FILTER
2233 #define FILTER "(a == 1 || b == 1) && (c == 1 || d == 1) && " \
2234            "(e == 1 || f == 1) && (g == 1 || h == 1)"
2235     DATA_REC(NO,  0, 0, 1, 1, 1, 1, 1, 1, "dfh"),
2236     DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2237     DATA_REC(YES, 1, 0, 1, 0, 0, 1, 0, 1, "bd"),
2238     DATA_REC(NO,  1, 0, 1, 0, 0, 1, 0, 0, "bd"),
2239 #undef FILTER
2240 #define FILTER "(a == 1 && b == 1) || (c == 1 && d == 1) || " \
2241            "(e == 1 && f == 1) || (g == 1 && h == 1)"
2242     DATA_REC(YES, 1, 0, 1, 1, 1, 1, 1, 1, "efgh"),
2243     DATA_REC(YES, 0, 0, 0, 0, 0, 0, 1, 1, ""),
2244     DATA_REC(NO,  0, 0, 0, 0, 0, 0, 0, 1, ""),
2245 #undef FILTER
2246 #define FILTER "(a == 1 && b == 1) && (c == 1 && d == 1) && " \
2247            "(e == 1 && f == 1) || (g == 1 && h == 1)"
2248     DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 0, "gh"),
2249     DATA_REC(NO,  0, 0, 0, 0, 0, 0, 0, 1, ""),
2250     DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, ""),
2251 #undef FILTER
2252 #define FILTER "((a == 1 || b == 1) || (c == 1 || d == 1) || " \
2253            "(e == 1 || f == 1)) && (g == 1 || h == 1)"
2254     DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 1, "bcdef"),
2255     DATA_REC(NO,  0, 0, 0, 0, 0, 0, 0, 0, ""),
2256     DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, "h"),
2257 #undef FILTER
2258 #define FILTER "((((((((a == 1) && (b == 1)) || (c == 1)) && (d == 1)) || " \
2259            "(e == 1)) && (f == 1)) || (g == 1)) && (h == 1))"
2260     DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "ceg"),
2261     DATA_REC(NO,  0, 1, 0, 1, 0, 1, 0, 1, ""),
2262     DATA_REC(NO,  1, 0, 1, 0, 1, 0, 1, 0, ""),
2263 #undef FILTER
2264 #define FILTER "((((((((a == 1) || (b == 1)) && (c == 1)) || (d == 1)) && " \
2265            "(e == 1)) || (f == 1)) && (g == 1)) || (h == 1))"
2266     DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "bdfh"),
2267     DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2268     DATA_REC(YES, 1, 0, 1, 0, 1, 0, 1, 0, "bdfh"),
2269 };
2270 
2271 #undef DATA_REC
2272 #undef FILTER
2273 #undef YES
2274 #undef NO
2275 
2276 #define DATA_CNT ARRAY_SIZE(test_filter_data)
2277 
2278 static int test_pred_visited;
2279 
2280 static int test_pred_visited_fn(struct filter_pred *pred, void *event)
2281 {
2282     struct ftrace_event_field *field = pred->field;
2283 
2284     test_pred_visited = 1;
2285     printk(KERN_INFO "\npred visited %s\n", field->name);
2286     return 1;
2287 }
2288 
2289 static void update_pred_fn(struct event_filter *filter, char *fields)
2290 {
2291     struct prog_entry *prog = rcu_dereference_protected(filter->prog,
2292                         lockdep_is_held(&event_mutex));
2293     int i;
2294 
2295     for (i = 0; prog[i].pred; i++) {
2296         struct filter_pred *pred = prog[i].pred;
2297         struct ftrace_event_field *field = pred->field;
2298 
2299         WARN_ON_ONCE(!pred->fn);
2300 
2301         if (!field) {
2302             WARN_ONCE(1, "all leafs should have field defined %d", i);
2303             continue;
2304         }
2305 
2306         if (!strchr(fields, *field->name))
2307             continue;
2308 
2309         pred->fn = test_pred_visited_fn;
2310     }
2311 }
2312 
2313 static __init int ftrace_test_event_filter(void)
2314 {
2315     int i;
2316 
2317     printk(KERN_INFO "Testing ftrace filter: ");
2318 
2319     for (i = 0; i < DATA_CNT; i++) {
2320         struct event_filter *filter = NULL;
2321         struct test_filter_data_t *d = &test_filter_data[i];
2322         int err;
2323 
2324         err = create_filter(NULL, &event_ftrace_test_filter,
2325                     d->filter, false, &filter);
2326         if (err) {
2327             printk(KERN_INFO
2328                    "Failed to get filter for '%s', err %d\n",
2329                    d->filter, err);
2330             __free_filter(filter);
2331             break;
2332         }
2333 
2334         /* Needed to dereference filter->prog */
2335         mutex_lock(&event_mutex);
2336         /*
2337          * The preemption disabling is not really needed for self
2338          * tests, but the rcu dereference will complain without it.
2339          */
2340         preempt_disable();
2341         if (*d->not_visited)
2342             update_pred_fn(filter, d->not_visited);
2343 
2344         test_pred_visited = 0;
2345         err = filter_match_preds(filter, &d->rec);
2346         preempt_enable();
2347 
2348         mutex_unlock(&event_mutex);
2349 
2350         __free_filter(filter);
2351 
2352         if (test_pred_visited) {
2353             printk(KERN_INFO
2354                    "Failed, unwanted pred visited for filter %s\n",
2355                    d->filter);
2356             break;
2357         }
2358 
2359         if (err != d->match) {
2360             printk(KERN_INFO
2361                    "Failed to match filter '%s', expected %d\n",
2362                    d->filter, d->match);
2363             break;
2364         }
2365     }
2366 
2367     if (i == DATA_CNT)
2368         printk(KERN_CONT "OK\n");
2369 
2370     return 0;
2371 }
2372 
2373 late_initcall(ftrace_test_event_filter);
2374 
2375 #endif /* CONFIG_FTRACE_STARTUP_TEST */