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