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