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