LCOV - code coverage report
Current view: top level - kernel/trace - trace_events_filter.c (source / functions) Hit Total Coverage
Test: landlock.info Lines: 5 804 0.6 %
Date: 2021-04-22 12:43:58 Functions: 1 82 1.2 %

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

Generated by: LCOV version 1.14