1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * trace_events_filter - generic event filtering 4 * 5 * Copyright (C) 2009 Tom Zanussi <tzanussi@gmail.com> 6 */ 7 8 #include <linux/uaccess.h> 9 #include <linux/module.h> 10 #include <linux/ctype.h> 11 #include <linux/mutex.h> 12 #include <linux/perf_event.h> 13 #include <linux/slab.h> 14 15 #include "trace.h" 16 #include "trace_output.h" 17 18 #define DEFAULT_SYS_FILTER_MESSAGE \ 19 "### global filter ###\n" \ 20 "# Use this to set filters for multiple events.\n" \ 21 "# Only events with the given fields will be affected.\n" \ 22 "# If no events are modified, an error message will be displayed here" 23 24 /* Due to token parsing '<=' must be before '<' and '>=' must be before '>' */ 25 #define OPS \ 26 C( OP_GLOB, "~" ), \ 27 C( OP_NE, "!=" ), \ 28 C( OP_EQ, "==" ), \ 29 C( OP_LE, "<=" ), \ 30 C( OP_LT, "<" ), \ 31 C( OP_GE, ">=" ), \ 32 C( OP_GT, ">" ), \ 33 C( OP_BAND, "&" ), \ 34 C( OP_MAX, NULL ) 35 36 #undef C 37 #define C(a, b) a 38 39 enum filter_op_ids { OPS }; 40 41 #undef C 42 #define C(a, b) b 43 44 static const char * ops[] = { OPS }; 45 46 enum filter_pred_fn { 47 FILTER_PRED_FN_NOP, 48 FILTER_PRED_FN_64, 49 FILTER_PRED_FN_64_CPUMASK, 50 FILTER_PRED_FN_S64, 51 FILTER_PRED_FN_U64, 52 FILTER_PRED_FN_32, 53 FILTER_PRED_FN_32_CPUMASK, 54 FILTER_PRED_FN_S32, 55 FILTER_PRED_FN_U32, 56 FILTER_PRED_FN_16, 57 FILTER_PRED_FN_16_CPUMASK, 58 FILTER_PRED_FN_S16, 59 FILTER_PRED_FN_U16, 60 FILTER_PRED_FN_8, 61 FILTER_PRED_FN_8_CPUMASK, 62 FILTER_PRED_FN_S8, 63 FILTER_PRED_FN_U8, 64 FILTER_PRED_FN_COMM, 65 FILTER_PRED_FN_STRING, 66 FILTER_PRED_FN_STRLOC, 67 FILTER_PRED_FN_STRRELLOC, 68 FILTER_PRED_FN_PCHAR_USER, 69 FILTER_PRED_FN_PCHAR, 70 FILTER_PRED_FN_CPU, 71 FILTER_PRED_FN_CPU_CPUMASK, 72 FILTER_PRED_FN_CPUMASK, 73 FILTER_PRED_FN_CPUMASK_CPU, 74 FILTER_PRED_FN_FUNCTION, 75 FILTER_PRED_FN_WITHIN, 76 FILTER_PRED_FN_, 77 FILTER_PRED_TEST_VISITED, 78 }; 79 80 struct filter_pred { 81 struct regex *regex; 82 struct cpumask *mask; 83 unsigned short *ops; 84 struct ftrace_event_field *field; 85 u64 val; 86 u64 val2; 87 enum filter_pred_fn fn_num; 88 int offset; 89 int not; 90 int op; 91 }; 92 93 /* 94 * pred functions are OP_LE, OP_LT, OP_GE, OP_GT, and OP_BAND 95 * pred_funcs_##type below must match the order of them above. 96 */ 97 #define PRED_FUNC_START OP_LE 98 #define PRED_FUNC_MAX (OP_BAND - PRED_FUNC_START) 99 100 #define ERRORS \ 101 C(NONE, "No error"), \ 102 C(INVALID_OP, "Invalid operator"), \ 103 C(TOO_MANY_OPEN, "Too many '('"), \ 104 C(TOO_MANY_CLOSE, "Too few '('"), \ 105 C(MISSING_QUOTE, "Missing matching quote"), \ 106 C(MISSING_BRACE_OPEN, "Missing '{'"), \ 107 C(MISSING_BRACE_CLOSE, "Missing '}'"), \ 108 C(OPERAND_TOO_LONG, "Operand too long"), \ 109 C(EXPECT_STRING, "Expecting string field"), \ 110 C(EXPECT_DIGIT, "Expecting numeric field"), \ 111 C(ILLEGAL_FIELD_OP, "Illegal operation for field type"), \ 112 C(FIELD_NOT_FOUND, "Field not found"), \ 113 C(ILLEGAL_INTVAL, "Illegal integer value"), \ 114 C(BAD_SUBSYS_FILTER, "Couldn't find or set field in one of a subsystem's events"), \ 115 C(TOO_MANY_PREDS, "Too many terms in predicate expression"), \ 116 C(INVALID_FILTER, "Meaningless filter expression"), \ 117 C(INVALID_CPULIST, "Invalid cpulist"), \ 118 C(IP_FIELD_ONLY, "Only 'ip' field is supported for function trace"), \ 119 C(INVALID_VALUE, "Invalid value (did you forget quotes)?"), \ 120 C(NO_FUNCTION, "Function not found"), \ 121 C(GLOB_WITHIN, "'within' filter cannot take glob expressions"), \ 122 C(ERRNO, "Error"), \ 123 C(NO_FILTER, "No filter found") 124 125 #undef C 126 #define C(a, b) FILT_ERR_##a 127 128 enum { ERRORS }; 129 130 #undef C 131 #define C(a, b) b 132 133 static const char *err_text[] = { ERRORS }; 134 135 /* Called after a '!' character but "!=" and "!~" are not "not"s */ 136 static bool is_not(const char *str) 137 { 138 switch (str[1]) { 139 case '=': 140 case '~': 141 return false; 142 } 143 return true; 144 } 145 146 /** 147 * struct prog_entry - a single entry in the filter program 148 * @target: Index to jump to on a branch (actually one minus the index) 149 * @when_to_branch: The value of the result of the predicate to do a branch 150 * @pred: The predicate to execute. 151 */ 152 struct prog_entry { 153 int target; 154 int when_to_branch; 155 struct filter_pred *pred; 156 }; 157 158 /** 159 * update_preds - assign a program entry a label target 160 * @prog: The program array 161 * @N: The index of the current entry in @prog 162 * @invert: What to assign a program entry for its branch condition 163 * 164 * The program entry at @N has a target that points to the index of a program 165 * entry that can have its target and when_to_branch fields updated. 166 * Update the current program entry denoted by index @N target field to be 167 * that of the updated entry. This will denote the entry to update if 168 * we are processing an "||" after an "&&". 169 */ 170 static void update_preds(struct prog_entry *prog, int N, int invert) 171 { 172 int t, s; 173 174 t = prog[N].target; 175 s = prog[t].target; 176 prog[t].when_to_branch = invert; 177 prog[t].target = N; 178 prog[N].target = s; 179 } 180 181 struct filter_parse_error { 182 int lasterr; 183 int lasterr_pos; 184 }; 185 186 static void parse_error(struct filter_parse_error *pe, int err, int pos) 187 { 188 pe->lasterr = err; 189 pe->lasterr_pos = pos; 190 } 191 192 typedef int (*parse_pred_fn)(const char *str, void *data, int pos, 193 struct filter_parse_error *pe, 194 struct filter_pred **pred); 195 196 enum { 197 INVERT = 1, 198 PROCESS_AND = 2, 199 PROCESS_OR = 4, 200 }; 201 202 static void free_predicate(struct filter_pred *pred) 203 { 204 if (pred) { 205 kfree(pred->regex); 206 kfree(pred->mask); 207 kfree(pred); 208 } 209 } 210 211 /* 212 * Without going into a formal proof, this explains the method that is used in 213 * parsing the logical expressions. 214 * 215 * For example, if we have: "a && !(!b || (c && g)) || d || e && !f" 216 * The first pass will convert it into the following program: 217 * 218 * n1: r=a; l1: if (!r) goto l4; 219 * n2: r=b; l2: if (!r) goto l4; 220 * n3: r=c; r=!r; l3: if (r) goto l4; 221 * n4: r=g; r=!r; l4: if (r) goto l5; 222 * n5: r=d; l5: if (r) goto T 223 * n6: r=e; l6: if (!r) goto l7; 224 * n7: r=f; r=!r; l7: if (!r) goto F 225 * T: return TRUE 226 * F: return FALSE 227 * 228 * To do this, we use a data structure to represent each of the above 229 * predicate and conditions that has: 230 * 231 * predicate, when_to_branch, invert, target 232 * 233 * The "predicate" will hold the function to determine the result "r". 234 * The "when_to_branch" denotes what "r" should be if a branch is to be taken 235 * "&&" would contain "!r" or (0) and "||" would contain "r" or (1). 236 * The "invert" holds whether the value should be reversed before testing. 237 * The "target" contains the label "l#" to jump to. 238 * 239 * A stack is created to hold values when parentheses are used. 240 * 241 * To simplify the logic, the labels will start at 0 and not 1. 242 * 243 * The possible invert values are 1 and 0. The number of "!"s that are in scope 244 * before the predicate determines the invert value, if the number is odd then 245 * the invert value is 1 and 0 otherwise. This means the invert value only 246 * needs to be toggled when a new "!" is introduced compared to what is stored 247 * on the stack, where parentheses were used. 248 * 249 * The top of the stack and "invert" are initialized to zero. 250 * 251 * ** FIRST PASS ** 252 * 253 * #1 A loop through all the tokens is done: 254 * 255 * #2 If the token is an "(", the stack is push, and the current stack value 256 * gets the current invert value, and the loop continues to the next token. 257 * The top of the stack saves the "invert" value to keep track of what 258 * the current inversion is. As "!(a && !b || c)" would require all 259 * predicates being affected separately by the "!" before the parentheses. 260 * And that would end up being equivalent to "(!a || b) && !c" 261 * 262 * #3 If the token is an "!", the current "invert" value gets inverted, and 263 * the loop continues. Note, if the next token is a predicate, then 264 * this "invert" value is only valid for the current program entry, 265 * and does not affect other predicates later on. 266 * 267 * The only other acceptable token is the predicate string. 268 * 269 * #4 A new entry into the program is added saving: the predicate and the 270 * current value of "invert". The target is currently assigned to the 271 * previous program index (this will not be its final value). 272 * 273 * #5 We now enter another loop and look at the next token. The only valid 274 * tokens are ")", "&&", "||" or end of the input string "\0". 275 * 276 * #6 The invert variable is reset to the current value saved on the top of 277 * the stack. 278 * 279 * #7 The top of the stack holds not only the current invert value, but also 280 * if a "&&" or "||" needs to be processed. Note, the "&&" takes higher 281 * precedence than "||". That is "a && b || c && d" is equivalent to 282 * "(a && b) || (c && d)". Thus the first thing to do is to see if "&&" needs 283 * to be processed. This is the case if an "&&" was the last token. If it was 284 * then we call update_preds(). This takes the program, the current index in 285 * the program, and the current value of "invert". More will be described 286 * below about this function. 287 * 288 * #8 If the next token is "&&" then we set a flag in the top of the stack 289 * that denotes that "&&" needs to be processed, break out of this loop 290 * and continue with the outer loop. 291 * 292 * #9 Otherwise, if a "||" needs to be processed then update_preds() is called. 293 * This is called with the program, the current index in the program, but 294 * this time with an inverted value of "invert" (that is !invert). This is 295 * because the value taken will become the "when_to_branch" value of the 296 * program. 297 * Note, this is called when the next token is not an "&&". As stated before, 298 * "&&" takes higher precedence, and "||" should not be processed yet if the 299 * next logical operation is "&&". 300 * 301 * #10 If the next token is "||" then we set a flag in the top of the stack 302 * that denotes that "||" needs to be processed, break out of this loop 303 * and continue with the outer loop. 304 * 305 * #11 If this is the end of the input string "\0" then we break out of both 306 * loops. 307 * 308 * #12 Otherwise, the next token is ")", where we pop the stack and continue 309 * this inner loop. 310 * 311 * Now to discuss the update_pred() function, as that is key to the setting up 312 * of the program. Remember the "target" of the program is initialized to the 313 * previous index and not the "l" label. The target holds the index into the 314 * program that gets affected by the operand. Thus if we have something like 315 * "a || b && c", when we process "a" the target will be "-1" (undefined). 316 * When we process "b", its target is "0", which is the index of "a", as that's 317 * the predicate that is affected by "||". But because the next token after "b" 318 * is "&&" we don't call update_preds(). Instead continue to "c". As the 319 * next token after "c" is not "&&" but the end of input, we first process the 320 * "&&" by calling update_preds() for the "&&" then we process the "||" by 321 * calling updates_preds() with the values for processing "||". 322 * 323 * What does that mean? What update_preds() does is to first save the "target" 324 * of the program entry indexed by the current program entry's "target" 325 * (remember the "target" is initialized to previous program entry), and then 326 * sets that "target" to the current index which represents the label "l#". 327 * That entry's "when_to_branch" is set to the value passed in (the "invert" 328 * or "!invert"). Then it sets the current program entry's target to the saved 329 * "target" value (the old value of the program that had its "target" updated 330 * to the label). 331 * 332 * Looking back at "a || b && c", we have the following steps: 333 * "a" - prog[0] = { "a", X, -1 } // pred, when_to_branch, target 334 * "||" - flag that we need to process "||"; continue outer loop 335 * "b" - prog[1] = { "b", X, 0 } 336 * "&&" - flag that we need to process "&&"; continue outer loop 337 * (Notice we did not process "||") 338 * "c" - prog[2] = { "c", X, 1 } 339 * update_preds(prog, 2, 0); // invert = 0 as we are processing "&&" 340 * t = prog[2].target; // t = 1 341 * s = prog[t].target; // s = 0 342 * prog[t].target = 2; // Set target to "l2" 343 * prog[t].when_to_branch = 0; 344 * prog[2].target = s; 345 * update_preds(prog, 2, 1); // invert = 1 as we are now processing "||" 346 * t = prog[2].target; // t = 0 347 * s = prog[t].target; // s = -1 348 * prog[t].target = 2; // Set target to "l2" 349 * prog[t].when_to_branch = 1; 350 * prog[2].target = s; 351 * 352 * #13 Which brings us to the final step of the first pass, which is to set 353 * the last program entry's when_to_branch and target, which will be 354 * when_to_branch = 0; target = N; ( the label after the program entry after 355 * the last program entry processed above). 356 * 357 * If we denote "TRUE" to be the entry after the last program entry processed, 358 * and "FALSE" the program entry after that, we are now done with the first 359 * pass. 360 * 361 * Making the above "a || b && c" have a program of: 362 * prog[0] = { "a", 1, 2 } 363 * prog[1] = { "b", 0, 2 } 364 * prog[2] = { "c", 0, 3 } 365 * 366 * Which translates into: 367 * n0: r = a; l0: if (r) goto l2; 368 * n1: r = b; l1: if (!r) goto l2; 369 * n2: r = c; l2: if (!r) goto l3; // Which is the same as "goto F;" 370 * T: return TRUE; l3: 371 * F: return FALSE 372 * 373 * Although, after the first pass, the program is correct, it is 374 * inefficient. The simple sample of "a || b && c" could be easily been 375 * converted into: 376 * n0: r = a; if (r) goto T 377 * n1: r = b; if (!r) goto F 378 * n2: r = c; if (!r) goto F 379 * T: return TRUE; 380 * F: return FALSE; 381 * 382 * The First Pass is over the input string. The next too passes are over 383 * the program itself. 384 * 385 * ** SECOND PASS ** 386 * 387 * Which brings us to the second pass. If a jump to a label has the 388 * same condition as that label, it can instead jump to its target. 389 * The original example of "a && !(!b || (c && g)) || d || e && !f" 390 * where the first pass gives us: 391 * 392 * n1: r=a; l1: if (!r) goto l4; 393 * n2: r=b; l2: if (!r) goto l4; 394 * n3: r=c; r=!r; l3: if (r) goto l4; 395 * n4: r=g; r=!r; l4: if (r) goto l5; 396 * n5: r=d; l5: if (r) goto T 397 * n6: r=e; l6: if (!r) goto l7; 398 * n7: r=f; r=!r; l7: if (!r) goto F: 399 * T: return TRUE; 400 * F: return FALSE 401 * 402 * We can see that "l3: if (r) goto l4;" and at l4, we have "if (r) goto l5;". 403 * And "l5: if (r) goto T", we could optimize this by converting l3 and l4 404 * to go directly to T. To accomplish this, we start from the last 405 * entry in the program and work our way back. If the target of the entry 406 * has the same "when_to_branch" then we could use that entry's target. 407 * Doing this, the above would end up as: 408 * 409 * n1: r=a; l1: if (!r) goto l4; 410 * n2: r=b; l2: if (!r) goto l4; 411 * n3: r=c; r=!r; l3: if (r) goto T; 412 * n4: r=g; r=!r; l4: if (r) goto T; 413 * n5: r=d; l5: if (r) goto T; 414 * n6: r=e; l6: if (!r) goto F; 415 * n7: r=f; r=!r; l7: if (!r) goto F; 416 * T: return TRUE 417 * F: return FALSE 418 * 419 * In that same pass, if the "when_to_branch" doesn't match, we can simply 420 * go to the program entry after the label. That is, "l2: if (!r) goto l4;" 421 * where "l4: if (r) goto T;", then we can convert l2 to be: 422 * "l2: if (!r) goto n5;". 423 * 424 * This will have the second pass give us: 425 * n1: r=a; l1: if (!r) goto n5; 426 * n2: r=b; l2: if (!r) goto n5; 427 * n3: r=c; r=!r; l3: if (r) goto T; 428 * n4: r=g; r=!r; l4: if (r) goto T; 429 * n5: r=d; l5: if (r) goto T 430 * n6: r=e; l6: if (!r) goto F; 431 * n7: r=f; r=!r; l7: if (!r) goto F 432 * T: return TRUE 433 * F: return FALSE 434 * 435 * Notice, all the "l#" labels are no longer used, and they can now 436 * be discarded. 437 * 438 * ** THIRD PASS ** 439 * 440 * For the third pass we deal with the inverts. As they simply just 441 * make the "when_to_branch" get inverted, a simple loop over the 442 * program to that does: "when_to_branch ^= invert;" will do the 443 * job, leaving us with: 444 * n1: r=a; if (!r) goto n5; 445 * n2: r=b; if (!r) goto n5; 446 * n3: r=c: if (!r) goto T; 447 * n4: r=g; if (!r) goto T; 448 * n5: r=d; if (r) goto T 449 * n6: r=e; if (!r) goto F; 450 * n7: r=f; if (r) goto F 451 * T: return TRUE 452 * F: return FALSE 453 * 454 * As "r = a; if (!r) goto n5;" is obviously the same as 455 * "if (!a) goto n5;" without doing anything we can interpret the 456 * program as: 457 * n1: if (!a) goto n5; 458 * n2: if (!b) goto n5; 459 * n3: if (!c) goto T; 460 * n4: if (!g) goto T; 461 * n5: if (d) goto T 462 * n6: if (!e) goto F; 463 * n7: if (f) goto F 464 * T: return TRUE 465 * F: return FALSE 466 * 467 * Since the inverts are discarded at the end, there's no reason to store 468 * them in the program array (and waste memory). A separate array to hold 469 * the inverts is used and freed at the end. 470 */ 471 static struct prog_entry * 472 predicate_parse(const char *str, int nr_parens, int nr_preds, 473 parse_pred_fn parse_pred, void *data, 474 struct filter_parse_error *pe) 475 { 476 struct prog_entry *prog_stack; 477 struct prog_entry *prog; 478 const char *ptr = str; 479 char *inverts = NULL; 480 int *op_stack; 481 int *top; 482 int invert = 0; 483 int ret = -ENOMEM; 484 int len; 485 int N = 0; 486 int i; 487 int nr_preds_alloc = nr_preds + 2; /* For TRUE and FALSE */ 488 489 op_stack = kmalloc_objs(*op_stack, nr_parens); 490 if (!op_stack) 491 return ERR_PTR(-ENOMEM); 492 prog_stack = kzalloc_objs(*prog_stack, nr_preds_alloc); 493 if (!prog_stack) { 494 parse_error(pe, -ENOMEM, 0); 495 goto out_free; 496 } 497 inverts = kmalloc_array(nr_preds_alloc, sizeof(*inverts), GFP_KERNEL); 498 if (!inverts) { 499 parse_error(pe, -ENOMEM, 0); 500 goto out_free; 501 } 502 503 top = op_stack; 504 prog = prog_stack; 505 *top = 0; 506 507 /* First pass */ 508 while (*ptr) { /* #1 */ 509 const char *next = ptr++; 510 511 if (isspace(*next)) 512 continue; 513 514 switch (*next) { 515 case '(': /* #2 */ 516 if (top - op_stack >= nr_parens - 1) { 517 ret = -EINVAL; 518 goto out_free; 519 } 520 *(++top) = invert; 521 continue; 522 case '!': /* #3 */ 523 if (!is_not(next)) 524 break; 525 invert = !invert; 526 continue; 527 } 528 529 if (N >= nr_preds) { 530 parse_error(pe, FILT_ERR_TOO_MANY_PREDS, next - str); 531 goto out_free; 532 } 533 534 inverts[N] = invert; /* #4 */ 535 prog[N].target = N-1; 536 537 len = parse_pred(next, data, ptr - str, pe, &prog[N].pred); 538 if (len < 0) { 539 ret = len; 540 goto out_free; 541 } 542 ptr = next + len; 543 544 N++; 545 546 ret = -1; 547 while (1) { /* #5 */ 548 next = ptr++; 549 if (isspace(*next)) 550 continue; 551 552 switch (*next) { 553 case ')': 554 case '\0': 555 break; 556 case '&': 557 case '|': 558 /* accepting only "&&" or "||" */ 559 if (next[1] == next[0]) { 560 ptr++; 561 break; 562 } 563 fallthrough; 564 default: 565 parse_error(pe, FILT_ERR_TOO_MANY_PREDS, 566 next - str); 567 goto out_free; 568 } 569 570 invert = *top & INVERT; 571 572 if (*top & PROCESS_AND) { /* #7 */ 573 update_preds(prog, N - 1, invert); 574 *top &= ~PROCESS_AND; 575 } 576 if (*next == '&') { /* #8 */ 577 *top |= PROCESS_AND; 578 break; 579 } 580 if (*top & PROCESS_OR) { /* #9 */ 581 update_preds(prog, N - 1, !invert); 582 *top &= ~PROCESS_OR; 583 } 584 if (*next == '|') { /* #10 */ 585 *top |= PROCESS_OR; 586 break; 587 } 588 if (!*next) /* #11 */ 589 goto out; 590 591 if (top == op_stack) { 592 ret = -1; 593 /* Too few '(' */ 594 parse_error(pe, FILT_ERR_TOO_MANY_CLOSE, ptr - str); 595 goto out_free; 596 } 597 top--; /* #12 */ 598 } 599 } 600 /* Only the inner loop can complete a non-empty expression. */ 601 if (N) { 602 ret = -EINVAL; 603 parse_error(pe, FILT_ERR_INVALID_FILTER, ptr - str); 604 goto out_free; 605 } 606 out: 607 if (top != op_stack) { 608 /* Too many '(' */ 609 parse_error(pe, FILT_ERR_TOO_MANY_OPEN, ptr - str); 610 goto out_free; 611 } 612 613 if (!N) { 614 /* No program? */ 615 ret = -EINVAL; 616 parse_error(pe, FILT_ERR_NO_FILTER, ptr - str); 617 goto out_free; 618 } 619 620 prog[N].pred = NULL; /* #13 */ 621 prog[N].target = 1; /* TRUE */ 622 prog[N+1].pred = NULL; 623 prog[N+1].target = 0; /* FALSE */ 624 prog[N-1].target = N; 625 prog[N-1].when_to_branch = false; 626 627 /* Second Pass */ 628 for (i = N-1 ; i--; ) { 629 int target = prog[i].target; 630 if (prog[i].when_to_branch == prog[target].when_to_branch) 631 prog[i].target = prog[target].target; 632 } 633 634 /* Third Pass */ 635 for (i = 0; i < N; i++) { 636 invert = inverts[i] ^ prog[i].when_to_branch; 637 prog[i].when_to_branch = invert; 638 /* Make sure the program always moves forward */ 639 if (WARN_ON(prog[i].target <= i)) { 640 ret = -EINVAL; 641 goto out_free; 642 } 643 } 644 645 kfree(op_stack); 646 kfree(inverts); 647 return prog; 648 out_free: 649 kfree(op_stack); 650 kfree(inverts); 651 if (prog_stack) { 652 for (i = 0; prog_stack[i].pred; i++) 653 free_predicate(prog_stack[i].pred); 654 kfree(prog_stack); 655 } 656 return ERR_PTR(ret); 657 } 658 659 static inline int 660 do_filter_cpumask(int op, const struct cpumask *mask, const struct cpumask *cmp) 661 { 662 switch (op) { 663 case OP_EQ: 664 return cpumask_equal(mask, cmp); 665 case OP_NE: 666 return !cpumask_equal(mask, cmp); 667 case OP_BAND: 668 return cpumask_intersects(mask, cmp); 669 default: 670 return 0; 671 } 672 } 673 674 /* Optimisation of do_filter_cpumask() for scalar fields */ 675 static inline int 676 do_filter_scalar_cpumask(int op, unsigned int cpu, const struct cpumask *mask) 677 { 678 /* 679 * Per the weight-of-one cpumask optimisations, the mask passed in this 680 * function has a weight >= 2, so it is never equal to a single scalar. 681 */ 682 switch (op) { 683 case OP_EQ: 684 return false; 685 case OP_NE: 686 return true; 687 case OP_BAND: 688 return cpumask_test_cpu(cpu, mask); 689 default: 690 return 0; 691 } 692 } 693 694 static inline int 695 do_filter_cpumask_scalar(int op, const struct cpumask *mask, unsigned int cpu) 696 { 697 switch (op) { 698 case OP_EQ: 699 return cpumask_test_cpu(cpu, mask) && 700 cpumask_nth(1, mask) >= nr_cpu_ids; 701 case OP_NE: 702 return !cpumask_test_cpu(cpu, mask) || 703 cpumask_nth(1, mask) < nr_cpu_ids; 704 case OP_BAND: 705 return cpumask_test_cpu(cpu, mask); 706 default: 707 return 0; 708 } 709 } 710 711 enum pred_cmp_types { 712 PRED_CMP_TYPE_NOP, 713 PRED_CMP_TYPE_LT, 714 PRED_CMP_TYPE_LE, 715 PRED_CMP_TYPE_GT, 716 PRED_CMP_TYPE_GE, 717 PRED_CMP_TYPE_BAND, 718 }; 719 720 #define DEFINE_COMPARISON_PRED(type) \ 721 static int filter_pred_##type(struct filter_pred *pred, void *event) \ 722 { \ 723 switch (pred->op) { \ 724 case OP_LT: { \ 725 type *addr = (type *)(event + pred->offset); \ 726 type val = (type)pred->val; \ 727 return *addr < val; \ 728 } \ 729 case OP_LE: { \ 730 type *addr = (type *)(event + pred->offset); \ 731 type val = (type)pred->val; \ 732 return *addr <= val; \ 733 } \ 734 case OP_GT: { \ 735 type *addr = (type *)(event + pred->offset); \ 736 type val = (type)pred->val; \ 737 return *addr > val; \ 738 } \ 739 case OP_GE: { \ 740 type *addr = (type *)(event + pred->offset); \ 741 type val = (type)pred->val; \ 742 return *addr >= val; \ 743 } \ 744 case OP_BAND: { \ 745 type *addr = (type *)(event + pred->offset); \ 746 type val = (type)pred->val; \ 747 return !!(*addr & val); \ 748 } \ 749 default: \ 750 return 0; \ 751 } \ 752 } 753 754 #define DEFINE_CPUMASK_COMPARISON_PRED(size) \ 755 static int filter_pred_##size##_cpumask(struct filter_pred *pred, void *event) \ 756 { \ 757 u##size *addr = (u##size *)(event + pred->offset); \ 758 unsigned int cpu = *addr; \ 759 \ 760 if (cpu >= nr_cpu_ids) \ 761 return pred->op == OP_NE; \ 762 \ 763 return do_filter_scalar_cpumask(pred->op, cpu, pred->mask); \ 764 } 765 766 #define DEFINE_EQUALITY_PRED(size) \ 767 static int filter_pred_##size(struct filter_pred *pred, void *event) \ 768 { \ 769 u##size *addr = (u##size *)(event + pred->offset); \ 770 u##size val = (u##size)pred->val; \ 771 int match; \ 772 \ 773 match = (val == *addr) ^ pred->not; \ 774 \ 775 return match; \ 776 } 777 778 DEFINE_COMPARISON_PRED(s64); 779 DEFINE_COMPARISON_PRED(u64); 780 DEFINE_COMPARISON_PRED(s32); 781 DEFINE_COMPARISON_PRED(u32); 782 DEFINE_COMPARISON_PRED(s16); 783 DEFINE_COMPARISON_PRED(u16); 784 DEFINE_COMPARISON_PRED(s8); 785 DEFINE_COMPARISON_PRED(u8); 786 787 DEFINE_CPUMASK_COMPARISON_PRED(64); 788 DEFINE_CPUMASK_COMPARISON_PRED(32); 789 DEFINE_CPUMASK_COMPARISON_PRED(16); 790 DEFINE_CPUMASK_COMPARISON_PRED(8); 791 792 DEFINE_EQUALITY_PRED(64); 793 DEFINE_EQUALITY_PRED(32); 794 DEFINE_EQUALITY_PRED(16); 795 DEFINE_EQUALITY_PRED(8); 796 797 /* user space strings temp buffer */ 798 #define USTRING_BUF_SIZE 1024 799 800 struct ustring_buffer { 801 char buffer[USTRING_BUF_SIZE]; 802 }; 803 804 static __percpu struct ustring_buffer *ustring_per_cpu; 805 806 static __always_inline char *test_string(char *str) 807 { 808 struct ustring_buffer *ubuf; 809 char *kstr; 810 811 if (!ustring_per_cpu) 812 return NULL; 813 814 ubuf = this_cpu_ptr(ustring_per_cpu); 815 kstr = ubuf->buffer; 816 817 /* For safety, do not trust the string pointer */ 818 if (strncpy_from_kernel_nofault(kstr, str, USTRING_BUF_SIZE) < 0) 819 return NULL; 820 return kstr; 821 } 822 823 static __always_inline char *test_ustring(char *str) 824 { 825 struct ustring_buffer *ubuf; 826 char __user *ustr; 827 char *kstr; 828 829 if (!ustring_per_cpu) 830 return NULL; 831 832 ubuf = this_cpu_ptr(ustring_per_cpu); 833 kstr = ubuf->buffer; 834 835 /* user space address? */ 836 ustr = (char __user *)str; 837 if (strncpy_from_user_nofault(kstr, ustr, USTRING_BUF_SIZE) < 0) 838 return NULL; 839 840 return kstr; 841 } 842 843 /* Filter predicate for fixed sized arrays of characters */ 844 static int filter_pred_string(struct filter_pred *pred, void *event) 845 { 846 char *addr = (char *)(event + pred->offset); 847 int cmp, match; 848 849 cmp = pred->regex->match(addr, pred->regex, pred->regex->field_len); 850 851 match = cmp ^ pred->not; 852 853 return match; 854 } 855 856 static __always_inline int filter_pchar(struct filter_pred *pred, char *str) 857 { 858 int cmp, match; 859 int len; 860 861 len = strlen(str) + 1; /* including tailing '\0' */ 862 cmp = pred->regex->match(str, pred->regex, len); 863 864 match = cmp ^ pred->not; 865 866 return match; 867 } 868 /* Filter predicate for char * pointers */ 869 static int filter_pred_pchar(struct filter_pred *pred, void *event) 870 { 871 char **addr = (char **)(event + pred->offset); 872 char *str; 873 874 str = test_string(*addr); 875 if (!str) 876 return 0; 877 878 return filter_pchar(pred, str); 879 } 880 881 /* Filter predicate for char * pointers in user space*/ 882 static int filter_pred_pchar_user(struct filter_pred *pred, void *event) 883 { 884 char **addr = (char **)(event + pred->offset); 885 char *str; 886 887 str = test_ustring(*addr); 888 if (!str) 889 return 0; 890 891 return filter_pchar(pred, str); 892 } 893 894 static __always_inline int filter_dyn_string(struct filter_pred *pred, 895 char *str, int len) 896 { 897 int cmp; 898 899 /* 900 * The regex callbacks use zero for an unknown-length, NUL-terminated 901 * string. A zero-length data_loc or rel_loc field has no bytes at all. 902 * Match an empty string instead of reading past the end of the field. 903 */ 904 if (!len) { 905 str = ""; 906 len = 1; 907 } 908 909 cmp = pred->regex->match(str, pred->regex, len); 910 return cmp ^ pred->not; 911 } 912 913 /* 914 * Filter predicate for dynamic sized arrays of characters. 915 * These are implemented through a list of strings at the end 916 * of the entry. 917 * Also each of these strings have a field in the entry which 918 * contains its offset from the beginning of the entry. 919 * We have then first to get this field, dereference it 920 * and add it to the address of the entry, and at last we have 921 * the address of the string. 922 */ 923 static int filter_pred_strloc(struct filter_pred *pred, void *event) 924 { 925 u32 str_item = *(u32 *)(event + pred->offset); 926 int str_loc = str_item & 0xffff; 927 int str_len = str_item >> 16; 928 char *addr = (char *)(event + str_loc); 929 930 return filter_dyn_string(pred, addr, str_len); 931 } 932 933 /* 934 * Filter predicate for relative dynamic sized arrays of characters. 935 * These are implemented through a list of strings at the end 936 * of the entry as same as dynamic string. 937 * The difference is that the relative one records the location offset 938 * from the field itself, not the event entry. 939 */ 940 static int filter_pred_strrelloc(struct filter_pred *pred, void *event) 941 { 942 u32 *item = (u32 *)(event + pred->offset); 943 u32 str_item = *item; 944 int str_loc = str_item & 0xffff; 945 int str_len = str_item >> 16; 946 char *addr = (char *)(&item[1]) + str_loc; 947 948 return filter_dyn_string(pred, addr, str_len); 949 } 950 951 /* Filter predicate for CPUs. */ 952 static int filter_pred_cpu(struct filter_pred *pred, void *event) 953 { 954 int cpu, cmp; 955 956 cpu = raw_smp_processor_id(); 957 cmp = pred->val; 958 959 switch (pred->op) { 960 case OP_EQ: 961 return cpu == cmp; 962 case OP_NE: 963 return cpu != cmp; 964 case OP_LT: 965 return cpu < cmp; 966 case OP_LE: 967 return cpu <= cmp; 968 case OP_GT: 969 return cpu > cmp; 970 case OP_GE: 971 return cpu >= cmp; 972 default: 973 return 0; 974 } 975 } 976 977 /* Filter predicate for current CPU vs user-provided cpumask */ 978 static int filter_pred_cpu_cpumask(struct filter_pred *pred, void *event) 979 { 980 int cpu = raw_smp_processor_id(); 981 982 return do_filter_scalar_cpumask(pred->op, cpu, pred->mask); 983 } 984 985 /* Filter predicate for cpumask field vs user-provided cpumask */ 986 static int filter_pred_cpumask(struct filter_pred *pred, void *event) 987 { 988 u32 item = *(u32 *)(event + pred->offset); 989 int loc = item & 0xffff; 990 const struct cpumask *mask = (event + loc); 991 const struct cpumask *cmp = pred->mask; 992 993 return do_filter_cpumask(pred->op, mask, cmp); 994 } 995 996 /* Filter predicate for cpumask field vs user-provided scalar */ 997 static int filter_pred_cpumask_cpu(struct filter_pred *pred, void *event) 998 { 999 u32 item = *(u32 *)(event + pred->offset); 1000 int loc = item & 0xffff; 1001 const struct cpumask *mask = (event + loc); 1002 unsigned int cpu = pred->val; 1003 1004 return do_filter_cpumask_scalar(pred->op, mask, cpu); 1005 } 1006 1007 /* Filter predicate for COMM. */ 1008 static int filter_pred_comm(struct filter_pred *pred, void *event) 1009 { 1010 int cmp; 1011 1012 cmp = pred->regex->match(current->comm, pred->regex, 1013 TASK_COMM_LEN); 1014 return cmp ^ pred->not; 1015 } 1016 1017 /* Filter predicate for functions. */ 1018 static int filter_pred_function(struct filter_pred *pred, void *event) 1019 { 1020 unsigned long *addr = (unsigned long *)(event + pred->offset); 1021 unsigned long start = (unsigned long)pred->val; 1022 unsigned long end = (unsigned long)pred->val2; 1023 int ret = *addr >= start && *addr < end; 1024 1025 return pred->op == OP_EQ ? ret : !ret; 1026 } 1027 1028 /* Filter predicate for within. */ 1029 static int filter_pred_within(struct filter_pred *pred, void *event) 1030 { 1031 #ifdef CONFIG_STACKTRACE 1032 unsigned long entries[16]; 1033 unsigned int nr_entries; 1034 int i; 1035 1036 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0); 1037 for (i = 0; i < nr_entries; i++) 1038 if (pred->val <= entries[i] && entries[i] < pred->val2) 1039 return !pred->not; 1040 #endif 1041 return pred->not; 1042 } 1043 1044 /* 1045 * regex_match_foo - Basic regex callbacks 1046 * 1047 * @str: the string to be searched 1048 * @r: the regex structure containing the pattern string 1049 * @len: the length of the string to be searched (including '\0'), or zero 1050 * for a NUL-terminated string of unknown length 1051 * 1052 * Note: 1053 * - @str might not be NULL-terminated if it's of type DYN_STRING 1054 * RDYN_STRING, or STATIC_STRING, unless @len is zero. 1055 */ 1056 1057 static int regex_match_full(char *str, struct regex *r, int len) 1058 { 1059 /* Zero is reserved for a NUL-terminated string of unknown length. */ 1060 if (!len) 1061 return strcmp(str, r->pattern) == 0; 1062 1063 if (len < r->len) 1064 return 0; 1065 1066 return strncmp(str, r->pattern, len) == 0; 1067 } 1068 1069 static int regex_match_front(char *str, struct regex *r, int len) 1070 { 1071 if (len && len < r->len) 1072 return 0; 1073 1074 return strncmp(str, r->pattern, r->len) == 0; 1075 } 1076 1077 static int regex_match_middle(char *str, struct regex *r, int len) 1078 { 1079 if (!len) 1080 return strstr(str, r->pattern) != NULL; 1081 1082 return strnstr(str, r->pattern, len) != NULL; 1083 } 1084 1085 static int regex_match_end(char *str, struct regex *r, int len) 1086 { 1087 int strlen = len - 1; 1088 1089 if (strlen >= r->len && 1090 memcmp(str + strlen - r->len, r->pattern, r->len) == 0) 1091 return 1; 1092 return 0; 1093 } 1094 1095 static int regex_match_end_str(char *str, struct regex *r, int len) 1096 { 1097 int slen; 1098 1099 if (len < r->len) 1100 return 0; 1101 1102 slen = strnlen(str, len); 1103 if (slen >= r->len && 1104 memcmp(str + slen - r->len, r->pattern, r->len) == 0) 1105 return 1; 1106 return 0; 1107 } 1108 1109 static int regex_match_end_dynstr(char *str, struct regex *r, int len) 1110 { 1111 int slen = strlen(str); 1112 1113 if (slen >= r->len && 1114 memcmp(str + slen - r->len, r->pattern, r->len) == 0) 1115 return 1; 1116 return 0; 1117 } 1118 1119 static int regex_match_glob(char *str, struct regex *r, int len) 1120 { 1121 return glob_match_len(r->pattern, str, len) ? 1 : 0; 1122 } 1123 1124 /** 1125 * filter_parse_regex - parse a basic regex 1126 * @buff: the raw regex 1127 * @len: length of the regex 1128 * @search: will point to the beginning of the string to compare 1129 * @not: tell whether the match will have to be inverted 1130 * 1131 * This passes in a buffer containing a regex and this function will 1132 * set search to point to the search part of the buffer and 1133 * return the type of search it is (see enum above). 1134 * This does modify buff. 1135 * 1136 * Returns enum type. 1137 * search returns the pointer to use for comparison. 1138 * not returns 1 if buff started with a '!' 1139 * 0 otherwise. 1140 */ 1141 enum regex_type filter_parse_regex(char *buff, int len, char **search, int *not) 1142 { 1143 int type = MATCH_FULL; 1144 int i; 1145 1146 if (buff[0] == '!') { 1147 *not = 1; 1148 buff++; 1149 len--; 1150 } else 1151 *not = 0; 1152 1153 *search = buff; 1154 1155 for (i = 0; i < len; i++) { 1156 if (buff[i] == '*') { 1157 if (!i) { 1158 type = MATCH_END_ONLY; 1159 } else if (i == len - 1) { 1160 if (type == MATCH_END_ONLY) 1161 type = MATCH_MIDDLE_ONLY; 1162 else 1163 type = MATCH_FRONT_ONLY; 1164 buff[i] = 0; 1165 break; 1166 } else { /* pattern continues, use full glob */ 1167 return MATCH_GLOB; 1168 } 1169 } else if (strchr("[?\\", buff[i])) { 1170 return MATCH_GLOB; 1171 } 1172 } 1173 if (buff[0] == '*') 1174 *search = buff + 1; 1175 1176 return type; 1177 } 1178 1179 static void filter_build_regex(struct filter_pred *pred) 1180 { 1181 struct regex *r = pred->regex; 1182 char *search; 1183 enum regex_type type = MATCH_FULL; 1184 1185 if (pred->op == OP_GLOB) { 1186 type = filter_parse_regex(r->pattern, r->len, &search, &pred->not); 1187 r->len = strlen(search); 1188 memmove(r->pattern, search, r->len+1); 1189 } 1190 1191 switch (type) { 1192 case MATCH_FULL: 1193 r->match = regex_match_full; 1194 break; 1195 case MATCH_FRONT_ONLY: 1196 r->match = regex_match_front; 1197 break; 1198 case MATCH_MIDDLE_ONLY: 1199 r->match = regex_match_middle; 1200 break; 1201 case MATCH_END_ONLY: 1202 r->match = regex_match_end; 1203 break; 1204 case MATCH_GLOB: 1205 r->match = regex_match_glob; 1206 break; 1207 } 1208 } 1209 1210 1211 #ifdef CONFIG_FTRACE_STARTUP_TEST 1212 static int test_pred_visited_fn(struct filter_pred *pred, void *event); 1213 #else 1214 static int test_pred_visited_fn(struct filter_pred *pred, void *event) 1215 { 1216 return 0; 1217 } 1218 #endif 1219 1220 1221 static int filter_pred_fn_call(struct filter_pred *pred, void *event); 1222 1223 /* return 1 if event matches, 0 otherwise (discard) */ 1224 int filter_match_preds(struct event_filter *filter, void *rec) 1225 { 1226 struct prog_entry *prog; 1227 int i; 1228 1229 /* no filter is considered a match */ 1230 if (!filter) 1231 return 1; 1232 1233 /* Protected by either SRCU(tracepoint_srcu) or preempt_disable */ 1234 prog = rcu_dereference_raw(filter->prog); 1235 if (!prog) 1236 return 1; 1237 1238 for (i = 0; prog[i].pred; i++) { 1239 struct filter_pred *pred = prog[i].pred; 1240 int match = filter_pred_fn_call(pred, rec); 1241 if (match == prog[i].when_to_branch) 1242 i = prog[i].target; 1243 } 1244 return prog[i].target; 1245 } 1246 EXPORT_SYMBOL_GPL(filter_match_preds); 1247 1248 static void remove_filter_string(struct event_filter *filter) 1249 { 1250 if (!filter) 1251 return; 1252 1253 kfree(filter->filter_string); 1254 filter->filter_string = NULL; 1255 } 1256 1257 static void append_filter_err(struct trace_array *tr, 1258 struct filter_parse_error *pe, 1259 struct event_filter *filter) 1260 { 1261 struct trace_seq *s; 1262 int pos = pe->lasterr_pos; 1263 char *buf; 1264 int len; 1265 1266 if (WARN_ON(!filter->filter_string)) 1267 return; 1268 1269 s = kmalloc_obj(*s); 1270 if (!s) 1271 return; 1272 trace_seq_init(s); 1273 1274 len = strlen(filter->filter_string); 1275 if (pos > len) 1276 pos = len; 1277 1278 /* indexing is off by one */ 1279 if (pos) 1280 pos++; 1281 1282 trace_seq_puts(s, filter->filter_string); 1283 if (pe->lasterr > 0) { 1284 trace_seq_printf(s, "\n%*s", pos, "^"); 1285 trace_seq_printf(s, "\nparse_error: %s\n", err_text[pe->lasterr]); 1286 tracing_log_err(tr, "event filter parse error", 1287 filter->filter_string, err_text, 1288 pe->lasterr, pe->lasterr_pos); 1289 } else { 1290 trace_seq_printf(s, "\nError: (%d)\n", pe->lasterr); 1291 tracing_log_err(tr, "event filter parse error", 1292 filter->filter_string, err_text, 1293 FILT_ERR_ERRNO, 0); 1294 } 1295 trace_seq_putc(s, 0); 1296 buf = kmemdup_nul(s->buffer, s->seq.len, GFP_KERNEL); 1297 if (buf) { 1298 kfree(filter->filter_string); 1299 filter->filter_string = buf; 1300 } 1301 kfree(s); 1302 } 1303 1304 static inline struct event_filter *event_filter(struct trace_event_file *file) 1305 { 1306 return rcu_dereference_protected(file->filter, 1307 lockdep_is_held(&event_mutex)); 1308 1309 } 1310 1311 /* caller must hold event_mutex */ 1312 void print_event_filter(struct trace_event_file *file, struct trace_seq *s) 1313 { 1314 struct event_filter *filter = event_filter(file); 1315 1316 if (filter && filter->filter_string) 1317 trace_seq_printf(s, "%s\n", filter->filter_string); 1318 else 1319 trace_seq_puts(s, "none\n"); 1320 } 1321 1322 void print_subsystem_event_filter(struct event_subsystem *system, 1323 struct trace_seq *s) 1324 { 1325 struct event_filter *filter; 1326 1327 mutex_lock(&event_mutex); 1328 filter = system->filter; 1329 if (filter && filter->filter_string) 1330 trace_seq_printf(s, "%s\n", filter->filter_string); 1331 else 1332 trace_seq_puts(s, DEFAULT_SYS_FILTER_MESSAGE "\n"); 1333 mutex_unlock(&event_mutex); 1334 } 1335 1336 static void free_prog(struct event_filter *filter) 1337 { 1338 struct prog_entry *prog; 1339 int i; 1340 1341 prog = rcu_access_pointer(filter->prog); 1342 if (!prog) 1343 return; 1344 1345 for (i = 0; prog[i].pred; i++) 1346 free_predicate(prog[i].pred); 1347 kfree(prog); 1348 } 1349 1350 static void filter_disable(struct trace_event_file *file) 1351 { 1352 unsigned long old_flags = file->flags; 1353 1354 file->flags &= ~EVENT_FILE_FL_FILTERED; 1355 1356 if (old_flags != file->flags) 1357 trace_buffered_event_disable(); 1358 } 1359 1360 static void __free_filter(struct event_filter *filter) 1361 { 1362 if (!filter) 1363 return; 1364 1365 free_prog(filter); 1366 kfree(filter->filter_string); 1367 kfree(filter); 1368 } 1369 1370 void free_event_filter(struct event_filter *filter) 1371 { 1372 __free_filter(filter); 1373 } 1374 1375 static inline void __remove_filter(struct trace_event_file *file) 1376 { 1377 filter_disable(file); 1378 remove_filter_string(event_filter(file)); 1379 } 1380 1381 static void filter_free_subsystem_preds(struct trace_subsystem_dir *dir, 1382 struct trace_array *tr) 1383 { 1384 struct trace_event_file *file; 1385 1386 list_for_each_entry(file, &tr->events, list) { 1387 if (file->system != dir) 1388 continue; 1389 __remove_filter(file); 1390 } 1391 } 1392 1393 struct filter_list { 1394 struct list_head list; 1395 struct event_filter *filter; 1396 }; 1397 1398 struct filter_head { 1399 struct list_head list; 1400 union { 1401 struct rcu_head rcu; 1402 struct rcu_work rwork; 1403 }; 1404 }; 1405 1406 static void free_filter_list(struct filter_head *filter_list) 1407 { 1408 struct filter_list *filter_item, *tmp; 1409 1410 list_for_each_entry_safe(filter_item, tmp, &filter_list->list, list) { 1411 __free_filter(filter_item->filter); 1412 list_del(&filter_item->list); 1413 kfree(filter_item); 1414 } 1415 kfree(filter_list); 1416 } 1417 1418 static void free_filter_list_work(struct work_struct *work) 1419 { 1420 struct filter_head *filter_list; 1421 1422 filter_list = container_of(to_rcu_work(work), struct filter_head, rwork); 1423 free_filter_list(filter_list); 1424 } 1425 1426 static void free_filter_list_tasks(struct rcu_head *rhp) 1427 { 1428 struct filter_head *filter_list = container_of(rhp, struct filter_head, rcu); 1429 1430 INIT_RCU_WORK(&filter_list->rwork, free_filter_list_work); 1431 queue_rcu_work(system_dfl_wq, &filter_list->rwork); 1432 } 1433 1434 /* 1435 * The tracepoint_synchronize_unregister() is a double rcu call. 1436 * It calls synchronize_rcu_tasks_trace() followed by synchronize_rcu(). 1437 * Instead of waiting for it, simply call these via the call_rcu*() 1438 * variants. 1439 */ 1440 static void delay_free_filter(struct filter_head *head) 1441 { 1442 call_rcu_tasks_trace(&head->rcu, free_filter_list_tasks); 1443 } 1444 1445 static void try_delay_free_filter(struct event_filter *filter) 1446 { 1447 struct filter_head *head; 1448 struct filter_list *item; 1449 1450 head = kmalloc_obj(*head); 1451 if (!head) 1452 goto free_now; 1453 1454 INIT_LIST_HEAD(&head->list); 1455 1456 item = kmalloc_obj(*item); 1457 if (!item) { 1458 kfree(head); 1459 goto free_now; 1460 } 1461 1462 item->filter = filter; 1463 list_add_tail(&item->list, &head->list); 1464 delay_free_filter(head); 1465 return; 1466 1467 free_now: 1468 /* Make sure the filter is not being used */ 1469 tracepoint_synchronize_unregister(); 1470 __free_filter(filter); 1471 } 1472 1473 static inline void __free_subsystem_filter(struct trace_event_file *file) 1474 { 1475 __free_filter(event_filter(file)); 1476 file->filter = NULL; 1477 } 1478 1479 static inline void event_set_filter(struct trace_event_file *file, 1480 struct event_filter *filter) 1481 { 1482 rcu_assign_pointer(file->filter, filter); 1483 } 1484 1485 static inline void event_clear_filter(struct trace_event_file *file) 1486 { 1487 RCU_INIT_POINTER(file->filter, NULL); 1488 } 1489 1490 static void filter_free_subsystem_filters(struct trace_subsystem_dir *dir, 1491 struct trace_array *tr, 1492 struct event_filter *filter) 1493 { 1494 struct trace_event_file *file; 1495 struct filter_head *head; 1496 struct filter_list *item; 1497 1498 head = kmalloc_obj(*head); 1499 if (!head) 1500 goto free_now; 1501 1502 INIT_LIST_HEAD(&head->list); 1503 1504 list_for_each_entry(file, &tr->events, list) { 1505 if (file->system != dir) 1506 continue; 1507 item = kmalloc_obj(*item); 1508 if (!item) 1509 goto free_now; 1510 item->filter = event_filter(file); 1511 list_add_tail(&item->list, &head->list); 1512 event_clear_filter(file); 1513 } 1514 1515 item = kmalloc_obj(*item); 1516 if (!item) 1517 goto free_now; 1518 1519 item->filter = filter; 1520 list_add_tail(&item->list, &head->list); 1521 1522 delay_free_filter(head); 1523 return; 1524 free_now: 1525 tracepoint_synchronize_unregister(); 1526 1527 if (head) 1528 free_filter_list(head); 1529 1530 list_for_each_entry(file, &tr->events, list) { 1531 if (file->system != dir || !file->filter) 1532 continue; 1533 __free_subsystem_filter(file); 1534 } 1535 __free_filter(filter); 1536 } 1537 1538 int filter_assign_type(const char *type) 1539 { 1540 if (strstr(type, "__data_loc")) { 1541 if (strstr(type, "char")) 1542 return FILTER_DYN_STRING; 1543 if (strstr(type, "cpumask_t")) 1544 return FILTER_CPUMASK; 1545 } 1546 1547 if (strstr(type, "__rel_loc") && strstr(type, "char")) 1548 return FILTER_RDYN_STRING; 1549 1550 if (strchr(type, '[') && strstr(type, "char")) 1551 return FILTER_STATIC_STRING; 1552 1553 if (strcmp(type, "char *") == 0 || strcmp(type, "const char *") == 0) 1554 return FILTER_PTR_STRING; 1555 1556 return FILTER_OTHER; 1557 } 1558 1559 static enum filter_pred_fn select_comparison_fn(enum filter_op_ids op, 1560 int field_size, int field_is_signed) 1561 { 1562 enum filter_pred_fn fn = FILTER_PRED_FN_NOP; 1563 int pred_func_index = -1; 1564 1565 switch (op) { 1566 case OP_EQ: 1567 case OP_NE: 1568 break; 1569 default: 1570 if (WARN_ON_ONCE(op < PRED_FUNC_START)) 1571 return fn; 1572 pred_func_index = op - PRED_FUNC_START; 1573 if (WARN_ON_ONCE(pred_func_index > PRED_FUNC_MAX)) 1574 return fn; 1575 } 1576 1577 switch (field_size) { 1578 case 8: 1579 if (pred_func_index < 0) 1580 fn = FILTER_PRED_FN_64; 1581 else if (field_is_signed) 1582 fn = FILTER_PRED_FN_S64; 1583 else 1584 fn = FILTER_PRED_FN_U64; 1585 break; 1586 case 4: 1587 if (pred_func_index < 0) 1588 fn = FILTER_PRED_FN_32; 1589 else if (field_is_signed) 1590 fn = FILTER_PRED_FN_S32; 1591 else 1592 fn = FILTER_PRED_FN_U32; 1593 break; 1594 case 2: 1595 if (pred_func_index < 0) 1596 fn = FILTER_PRED_FN_16; 1597 else if (field_is_signed) 1598 fn = FILTER_PRED_FN_S16; 1599 else 1600 fn = FILTER_PRED_FN_U16; 1601 break; 1602 case 1: 1603 if (pred_func_index < 0) 1604 fn = FILTER_PRED_FN_8; 1605 else if (field_is_signed) 1606 fn = FILTER_PRED_FN_S8; 1607 else 1608 fn = FILTER_PRED_FN_U8; 1609 break; 1610 } 1611 1612 return fn; 1613 } 1614 1615 1616 static int filter_pred_fn_call(struct filter_pred *pred, void *event) 1617 { 1618 switch (pred->fn_num) { 1619 case FILTER_PRED_FN_64: 1620 return filter_pred_64(pred, event); 1621 case FILTER_PRED_FN_64_CPUMASK: 1622 return filter_pred_64_cpumask(pred, event); 1623 case FILTER_PRED_FN_S64: 1624 return filter_pred_s64(pred, event); 1625 case FILTER_PRED_FN_U64: 1626 return filter_pred_u64(pred, event); 1627 case FILTER_PRED_FN_32: 1628 return filter_pred_32(pred, event); 1629 case FILTER_PRED_FN_32_CPUMASK: 1630 return filter_pred_32_cpumask(pred, event); 1631 case FILTER_PRED_FN_S32: 1632 return filter_pred_s32(pred, event); 1633 case FILTER_PRED_FN_U32: 1634 return filter_pred_u32(pred, event); 1635 case FILTER_PRED_FN_16: 1636 return filter_pred_16(pred, event); 1637 case FILTER_PRED_FN_16_CPUMASK: 1638 return filter_pred_16_cpumask(pred, event); 1639 case FILTER_PRED_FN_S16: 1640 return filter_pred_s16(pred, event); 1641 case FILTER_PRED_FN_U16: 1642 return filter_pred_u16(pred, event); 1643 case FILTER_PRED_FN_8: 1644 return filter_pred_8(pred, event); 1645 case FILTER_PRED_FN_8_CPUMASK: 1646 return filter_pred_8_cpumask(pred, event); 1647 case FILTER_PRED_FN_S8: 1648 return filter_pred_s8(pred, event); 1649 case FILTER_PRED_FN_U8: 1650 return filter_pred_u8(pred, event); 1651 case FILTER_PRED_FN_COMM: 1652 return filter_pred_comm(pred, event); 1653 case FILTER_PRED_FN_STRING: 1654 return filter_pred_string(pred, event); 1655 case FILTER_PRED_FN_STRLOC: 1656 return filter_pred_strloc(pred, event); 1657 case FILTER_PRED_FN_STRRELLOC: 1658 return filter_pred_strrelloc(pred, event); 1659 case FILTER_PRED_FN_PCHAR_USER: 1660 return filter_pred_pchar_user(pred, event); 1661 case FILTER_PRED_FN_PCHAR: 1662 return filter_pred_pchar(pred, event); 1663 case FILTER_PRED_FN_CPU: 1664 return filter_pred_cpu(pred, event); 1665 case FILTER_PRED_FN_CPU_CPUMASK: 1666 return filter_pred_cpu_cpumask(pred, event); 1667 case FILTER_PRED_FN_CPUMASK: 1668 return filter_pred_cpumask(pred, event); 1669 case FILTER_PRED_FN_CPUMASK_CPU: 1670 return filter_pred_cpumask_cpu(pred, event); 1671 case FILTER_PRED_FN_FUNCTION: 1672 return filter_pred_function(pred, event); 1673 case FILTER_PRED_FN_WITHIN: 1674 return filter_pred_within(pred, event); 1675 case FILTER_PRED_TEST_VISITED: 1676 return test_pred_visited_fn(pred, event); 1677 default: 1678 return 0; 1679 } 1680 } 1681 1682 /* Called when a predicate is encountered by predicate_parse() */ 1683 static int parse_pred(const char *str, void *data, 1684 int pos, struct filter_parse_error *pe, 1685 struct filter_pred **pred_ptr) 1686 { 1687 struct trace_event_call *call = data; 1688 struct ftrace_event_field *field; 1689 struct filter_pred *pred = NULL; 1690 unsigned long offset; 1691 unsigned long size; 1692 unsigned long ip; 1693 char num_buf[24]; /* Big enough to hold an address */ 1694 char *field_name; 1695 char *name; 1696 bool function = false; 1697 bool ustring = false; 1698 char q; 1699 u64 val; 1700 int len; 1701 int ret; 1702 int op; 1703 int s; 1704 int i = 0; 1705 1706 /* First find the field to associate to */ 1707 while (isspace(str[i])) 1708 i++; 1709 s = i; 1710 1711 while (isalnum(str[i]) || str[i] == '_') 1712 i++; 1713 1714 len = i - s; 1715 1716 if (!len) 1717 return -1; 1718 1719 field_name = kmemdup_nul(str + s, len, GFP_KERNEL); 1720 if (!field_name) 1721 return -ENOMEM; 1722 1723 /* Make sure that the field exists */ 1724 1725 field = trace_find_event_field(call, field_name); 1726 kfree(field_name); 1727 if (!field) { 1728 parse_error(pe, FILT_ERR_FIELD_NOT_FOUND, pos + i); 1729 return -EINVAL; 1730 } 1731 1732 /* See if the field is a user space string */ 1733 if ((len = str_has_prefix(str + i, ".ustring"))) { 1734 ustring = true; 1735 i += len; 1736 } 1737 1738 /* See if the field is a kernel function name */ 1739 if ((len = str_has_prefix(str + i, ".function"))) { 1740 /* Ignore if event is already a function */ 1741 if (!ftrace_event_is_function(call)) 1742 function = true; 1743 i += len; 1744 } 1745 1746 while (isspace(str[i])) 1747 i++; 1748 1749 /* Make sure this op is supported */ 1750 for (op = 0; ops[op]; op++) { 1751 /* This is why '<=' must come before '<' in ops[] */ 1752 if (strncmp(str + i, ops[op], strlen(ops[op])) == 0) 1753 break; 1754 } 1755 1756 if (!ops[op]) { 1757 parse_error(pe, FILT_ERR_INVALID_OP, pos + i); 1758 goto err_free; 1759 } 1760 1761 i += strlen(ops[op]); 1762 1763 while (isspace(str[i])) 1764 i++; 1765 1766 if (!str[i]) { 1767 parse_error(pe, FILT_ERR_INVALID_FILTER, pos + i); 1768 goto err_free; 1769 } 1770 1771 s = i; 1772 1773 pred = kzalloc_obj(*pred); 1774 if (!pred) 1775 return -ENOMEM; 1776 1777 pred->field = field; 1778 pred->offset = field->offset; 1779 pred->op = op; 1780 1781 if (function) { 1782 /* The field must be the same size as long */ 1783 if (field->size != sizeof(long)) { 1784 parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i); 1785 goto err_free; 1786 } 1787 1788 /* Function only works with '==' or '!=' and an unquoted string */ 1789 switch (op) { 1790 case OP_NE: 1791 case OP_EQ: 1792 break; 1793 default: 1794 parse_error(pe, FILT_ERR_INVALID_OP, pos + i); 1795 goto err_free; 1796 } 1797 1798 if (isdigit(str[i])) { 1799 /* We allow 0xDEADBEEF */ 1800 while (isalnum(str[i])) 1801 i++; 1802 1803 len = i - s; 1804 /* 0xfeedfacedeadbeef is 18 chars max */ 1805 if (len >= sizeof(num_buf)) { 1806 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i); 1807 goto err_free; 1808 } 1809 1810 memcpy(num_buf, str + s, len); 1811 num_buf[len] = 0; 1812 1813 ret = kstrtoul(num_buf, 0, &ip); 1814 if (ret) { 1815 parse_error(pe, FILT_ERR_INVALID_VALUE, pos + i); 1816 goto err_free; 1817 } 1818 } else { 1819 s = i; 1820 for (; str[i] && !isspace(str[i]); i++) 1821 ; 1822 1823 len = i - s; 1824 name = kmemdup_nul(str + s, len, GFP_KERNEL); 1825 if (!name) 1826 goto err_mem; 1827 ip = kallsyms_lookup_name(name); 1828 kfree(name); 1829 if (!ip) { 1830 parse_error(pe, FILT_ERR_NO_FUNCTION, pos + i); 1831 goto err_free; 1832 } 1833 } 1834 1835 /* Now find the function start and end address */ 1836 if (!kallsyms_lookup_size_offset(ip, &size, &offset)) { 1837 parse_error(pe, FILT_ERR_NO_FUNCTION, pos + i); 1838 goto err_free; 1839 } 1840 1841 pred->fn_num = FILTER_PRED_FN_FUNCTION; 1842 pred->val = ip - offset; 1843 pred->val2 = pred->val + size; 1844 1845 } else if (ftrace_event_is_function(call)) { 1846 /* 1847 * Perf does things different with function events. 1848 * It only allows an "ip" field, and expects a string. 1849 * But the string does not need to be surrounded by quotes. 1850 * If it is a string, the assigned function as a nop, 1851 * (perf doesn't use it) and grab everything. 1852 */ 1853 if (strcmp(field->name, "ip") != 0) { 1854 parse_error(pe, FILT_ERR_IP_FIELD_ONLY, pos + i); 1855 goto err_free; 1856 } 1857 pred->fn_num = FILTER_PRED_FN_NOP; 1858 1859 /* 1860 * Quotes are not required, but if they exist then we need 1861 * to read them till we hit a matching one. 1862 */ 1863 if (str[i] == '\'' || str[i] == '"') 1864 q = str[i]; 1865 else 1866 q = 0; 1867 1868 for (i++; str[i]; i++) { 1869 if (q && str[i] == q) 1870 break; 1871 if (!q && (str[i] == ')' || str[i] == '&' || 1872 str[i] == '|')) 1873 break; 1874 } 1875 /* Skip quotes */ 1876 if (q) 1877 s++; 1878 len = i - s; 1879 if (len >= MAX_FILTER_STR_VAL) { 1880 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i); 1881 goto err_free; 1882 } 1883 1884 pred->regex = kzalloc_obj(*pred->regex); 1885 if (!pred->regex) 1886 goto err_mem; 1887 pred->regex->len = len; 1888 memcpy(pred->regex->pattern, str + s, len); 1889 pred->regex->pattern[len] = 0; 1890 1891 } else if (!strncmp(str + i, "CPUS", 4)) { 1892 unsigned int maskstart; 1893 bool single; 1894 char *tmp; 1895 1896 switch (field->filter_type) { 1897 case FILTER_CPUMASK: 1898 case FILTER_CPU: 1899 case FILTER_OTHER: 1900 break; 1901 default: 1902 parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i); 1903 goto err_free; 1904 } 1905 1906 switch (op) { 1907 case OP_EQ: 1908 case OP_NE: 1909 case OP_BAND: 1910 break; 1911 default: 1912 parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i); 1913 goto err_free; 1914 } 1915 1916 /* Skip CPUS */ 1917 i += 4; 1918 if (str[i++] != '{') { 1919 parse_error(pe, FILT_ERR_MISSING_BRACE_OPEN, pos + i); 1920 goto err_free; 1921 } 1922 maskstart = i; 1923 1924 /* Walk the cpulist until closing } */ 1925 for (; str[i] && str[i] != '}'; i++) 1926 ; 1927 1928 if (str[i] != '}') { 1929 parse_error(pe, FILT_ERR_MISSING_BRACE_CLOSE, pos + i); 1930 goto err_free; 1931 } 1932 1933 if (maskstart == i) { 1934 parse_error(pe, FILT_ERR_INVALID_CPULIST, pos + i); 1935 goto err_free; 1936 } 1937 1938 /* Copy the cpulist between { and } */ 1939 tmp = kmalloc((i - maskstart) + 1, GFP_KERNEL); 1940 if (!tmp) 1941 goto err_mem; 1942 1943 strscpy(tmp, str + maskstart, (i - maskstart) + 1); 1944 pred->mask = kzalloc(cpumask_size(), GFP_KERNEL); 1945 if (!pred->mask) { 1946 kfree(tmp); 1947 goto err_mem; 1948 } 1949 1950 /* Now parse it */ 1951 if (cpulist_parse(tmp, pred->mask)) { 1952 kfree(tmp); 1953 parse_error(pe, FILT_ERR_INVALID_CPULIST, pos + i); 1954 goto err_free; 1955 } 1956 kfree(tmp); 1957 1958 /* Move along */ 1959 i++; 1960 1961 /* 1962 * Optimisation: if the user-provided mask has a weight of one 1963 * then we can treat it as a scalar input. 1964 */ 1965 single = cpumask_weight(pred->mask) == 1; 1966 if (single) { 1967 pred->val = cpumask_first(pred->mask); 1968 kfree(pred->mask); 1969 pred->mask = NULL; 1970 } 1971 1972 if (field->filter_type == FILTER_CPUMASK) { 1973 pred->fn_num = single ? 1974 FILTER_PRED_FN_CPUMASK_CPU : 1975 FILTER_PRED_FN_CPUMASK; 1976 } else if (field->filter_type == FILTER_CPU) { 1977 if (single) { 1978 if (pred->op == OP_BAND) 1979 pred->op = OP_EQ; 1980 1981 pred->fn_num = FILTER_PRED_FN_CPU; 1982 } else { 1983 pred->fn_num = FILTER_PRED_FN_CPU_CPUMASK; 1984 } 1985 } else if (single) { 1986 if (pred->op == OP_BAND) 1987 pred->op = OP_EQ; 1988 1989 pred->fn_num = select_comparison_fn(pred->op, field->size, false); 1990 if (pred->op == OP_NE) 1991 pred->not = 1; 1992 } else { 1993 switch (field->size) { 1994 case 8: 1995 pred->fn_num = FILTER_PRED_FN_64_CPUMASK; 1996 break; 1997 case 4: 1998 pred->fn_num = FILTER_PRED_FN_32_CPUMASK; 1999 break; 2000 case 2: 2001 pred->fn_num = FILTER_PRED_FN_16_CPUMASK; 2002 break; 2003 case 1: 2004 pred->fn_num = FILTER_PRED_FN_8_CPUMASK; 2005 break; 2006 } 2007 } 2008 2009 /* This is either a string, or an integer */ 2010 } else if (str[i] == '\'' || str[i] == '"') { 2011 char q = str[i]; 2012 2013 /* Make sure the op is OK for strings */ 2014 switch (op) { 2015 case OP_NE: 2016 pred->not = 1; 2017 fallthrough; 2018 case OP_GLOB: 2019 case OP_EQ: 2020 break; 2021 default: 2022 parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i); 2023 goto err_free; 2024 } 2025 2026 /* Make sure the field is OK for strings */ 2027 if (!is_string_field(field)) { 2028 parse_error(pe, FILT_ERR_EXPECT_DIGIT, pos + i); 2029 goto err_free; 2030 } 2031 2032 for (i++; str[i]; i++) { 2033 if (str[i] == q) 2034 break; 2035 } 2036 if (!str[i]) { 2037 parse_error(pe, FILT_ERR_MISSING_QUOTE, pos + i); 2038 goto err_free; 2039 } 2040 2041 /* Skip quotes */ 2042 s++; 2043 len = i - s; 2044 if (len >= MAX_FILTER_STR_VAL) { 2045 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i); 2046 goto err_free; 2047 } 2048 2049 pred->regex = kzalloc_obj(*pred->regex); 2050 if (!pred->regex) 2051 goto err_mem; 2052 pred->regex->len = len; 2053 memcpy(pred->regex->pattern, str + s, len); 2054 pred->regex->pattern[len] = 0; 2055 2056 filter_build_regex(pred); 2057 2058 if (field->filter_type == FILTER_COMM) { 2059 pred->fn_num = FILTER_PRED_FN_COMM; 2060 if (pred->regex->match == regex_match_end) 2061 pred->regex->match = regex_match_end_str; 2062 2063 } else if (field->filter_type == FILTER_STATIC_STRING) { 2064 pred->fn_num = FILTER_PRED_FN_STRING; 2065 pred->regex->field_len = field->size; 2066 if (pred->regex->match == regex_match_end) { 2067 if (!field->size) 2068 pred->regex->match = regex_match_end_dynstr; 2069 else 2070 pred->regex->match = regex_match_end_str; 2071 } 2072 2073 } else if (field->filter_type == FILTER_DYN_STRING) { 2074 pred->fn_num = FILTER_PRED_FN_STRLOC; 2075 } else if (field->filter_type == FILTER_RDYN_STRING) { 2076 pred->fn_num = FILTER_PRED_FN_STRRELLOC; 2077 } else if (field->filter_type == FILTER_WITHIN) { 2078 unsigned long func; 2079 2080 if (op == OP_GLOB) { 2081 parse_error(pe, FILT_ERR_GLOB_WITHIN, pos + i); 2082 goto err_free; 2083 } 2084 2085 pred->fn_num = FILTER_PRED_FN_WITHIN; 2086 func = kallsyms_lookup_name(pred->regex->pattern); 2087 if (!func) { 2088 parse_error(pe, FILT_ERR_NO_FUNCTION, pos + i); 2089 goto err_free; 2090 } 2091 /* Now find the function start and end address */ 2092 if (!kallsyms_lookup_size_offset(func, &size, &offset)) { 2093 parse_error(pe, FILT_ERR_NO_FUNCTION, pos + i); 2094 goto err_free; 2095 } 2096 pred->val = func - offset; 2097 pred->val2 = pred->val + size; 2098 } else { 2099 if (!ustring_per_cpu) { 2100 /* Once allocated, keep it around for good */ 2101 ustring_per_cpu = alloc_percpu(struct ustring_buffer); 2102 if (!ustring_per_cpu) 2103 goto err_mem; 2104 } 2105 2106 if (ustring) 2107 pred->fn_num = FILTER_PRED_FN_PCHAR_USER; 2108 else 2109 pred->fn_num = FILTER_PRED_FN_PCHAR; 2110 } 2111 /* go past the last quote */ 2112 i++; 2113 2114 } else if (isdigit(str[i]) || str[i] == '-') { 2115 2116 /* Make sure the field is not a string */ 2117 if (is_string_field(field)) { 2118 parse_error(pe, FILT_ERR_EXPECT_STRING, pos + i); 2119 goto err_free; 2120 } 2121 2122 if (op == OP_GLOB) { 2123 parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i); 2124 goto err_free; 2125 } 2126 2127 if (str[i] == '-') 2128 i++; 2129 2130 /* We allow 0xDEADBEEF */ 2131 while (isalnum(str[i])) 2132 i++; 2133 2134 len = i - s; 2135 /* 0xfeedfacedeadbeef is 18 chars max */ 2136 if (len >= sizeof(num_buf)) { 2137 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i); 2138 goto err_free; 2139 } 2140 2141 memcpy(num_buf, str + s, len); 2142 num_buf[len] = 0; 2143 2144 /* Make sure it is a value */ 2145 if (field->is_signed) 2146 ret = kstrtoll(num_buf, 0, &val); 2147 else 2148 ret = kstrtoull(num_buf, 0, &val); 2149 if (ret) { 2150 parse_error(pe, FILT_ERR_ILLEGAL_INTVAL, pos + s); 2151 goto err_free; 2152 } 2153 2154 pred->val = val; 2155 2156 if (field->filter_type == FILTER_CPU) 2157 pred->fn_num = FILTER_PRED_FN_CPU; 2158 else { 2159 pred->fn_num = select_comparison_fn(pred->op, field->size, 2160 field->is_signed); 2161 if (pred->op == OP_NE) 2162 pred->not = 1; 2163 } 2164 2165 } else { 2166 parse_error(pe, FILT_ERR_INVALID_VALUE, pos + i); 2167 goto err_free; 2168 } 2169 2170 *pred_ptr = pred; 2171 return i; 2172 2173 err_free: 2174 free_predicate(pred); 2175 return -EINVAL; 2176 err_mem: 2177 free_predicate(pred); 2178 return -ENOMEM; 2179 } 2180 2181 enum { 2182 TOO_MANY_CLOSE = -1, 2183 TOO_MANY_OPEN = -2, 2184 MISSING_QUOTE = -3, 2185 }; 2186 2187 /* 2188 * Read the filter string once to calculate the number of predicates 2189 * as well as how deep the parentheses go. 2190 * 2191 * Returns: 2192 * 0 - everything is fine (err is undefined) 2193 * -1 - too many ')' 2194 * -2 - too many '(' 2195 * -3 - No matching quote 2196 */ 2197 static int calc_stack(const char *str, int *parens, int *preds, int *err) 2198 { 2199 bool is_pred = false; 2200 int nr_preds = 0; 2201 int open = 1; /* Count the expression as "(E)" */ 2202 int last_quote = 0; 2203 int max_open = 1; 2204 int quote = 0; 2205 int i; 2206 2207 *err = 0; 2208 2209 for (i = 0; str[i]; i++) { 2210 if (isspace(str[i])) 2211 continue; 2212 if (quote) { 2213 if (str[i] == quote) 2214 quote = 0; 2215 continue; 2216 } 2217 2218 switch (str[i]) { 2219 case '\'': 2220 case '"': 2221 quote = str[i]; 2222 last_quote = i; 2223 break; 2224 case '|': 2225 case '&': 2226 if (str[i+1] != str[i]) 2227 break; 2228 is_pred = false; 2229 continue; 2230 case '(': 2231 is_pred = false; 2232 open++; 2233 if (open > max_open) 2234 max_open = open; 2235 continue; 2236 case ')': 2237 is_pred = false; 2238 if (open == 1) { 2239 *err = i; 2240 return TOO_MANY_CLOSE; 2241 } 2242 open--; 2243 continue; 2244 } 2245 if (!is_pred) { 2246 nr_preds++; 2247 is_pred = true; 2248 } 2249 } 2250 2251 if (quote) { 2252 *err = last_quote; 2253 return MISSING_QUOTE; 2254 } 2255 2256 if (open != 1) { 2257 int level = open; 2258 2259 /* find the bad open */ 2260 for (i--; i; i--) { 2261 if (quote) { 2262 if (str[i] == quote) 2263 quote = 0; 2264 continue; 2265 } 2266 switch (str[i]) { 2267 case '(': 2268 if (level == open) { 2269 *err = i; 2270 return TOO_MANY_OPEN; 2271 } 2272 level--; 2273 break; 2274 case ')': 2275 level++; 2276 break; 2277 case '\'': 2278 case '"': 2279 quote = str[i]; 2280 break; 2281 } 2282 } 2283 /* First character is the '(' with missing ')' */ 2284 *err = 0; 2285 return TOO_MANY_OPEN; 2286 } 2287 2288 /* Set the size of the required stacks */ 2289 *parens = max_open; 2290 *preds = nr_preds; 2291 return 0; 2292 } 2293 2294 static int process_preds(struct trace_event_call *call, 2295 const char *filter_string, 2296 struct event_filter *filter, 2297 struct filter_parse_error *pe) 2298 { 2299 struct prog_entry *prog; 2300 int nr_parens; 2301 int nr_preds; 2302 int index; 2303 int ret; 2304 2305 ret = calc_stack(filter_string, &nr_parens, &nr_preds, &index); 2306 if (ret < 0) { 2307 switch (ret) { 2308 case MISSING_QUOTE: 2309 parse_error(pe, FILT_ERR_MISSING_QUOTE, index); 2310 break; 2311 case TOO_MANY_OPEN: 2312 parse_error(pe, FILT_ERR_TOO_MANY_OPEN, index); 2313 break; 2314 default: 2315 parse_error(pe, FILT_ERR_TOO_MANY_CLOSE, index); 2316 } 2317 return ret; 2318 } 2319 2320 if (!nr_preds) 2321 return -EINVAL; 2322 2323 prog = predicate_parse(filter_string, nr_parens, nr_preds, 2324 parse_pred, call, pe); 2325 if (IS_ERR(prog)) 2326 return PTR_ERR(prog); 2327 2328 rcu_assign_pointer(filter->prog, prog); 2329 return 0; 2330 } 2331 2332 static inline void event_set_filtered_flag(struct trace_event_file *file) 2333 { 2334 unsigned long old_flags = file->flags; 2335 2336 file->flags |= EVENT_FILE_FL_FILTERED; 2337 2338 if (old_flags != file->flags) 2339 trace_buffered_event_enable(); 2340 } 2341 2342 static int process_system_preds(struct trace_subsystem_dir *dir, 2343 struct trace_array *tr, 2344 struct filter_parse_error *pe, 2345 char *filter_string) 2346 { 2347 struct trace_event_file *file; 2348 struct filter_list *filter_item; 2349 struct event_filter *filter = NULL; 2350 struct filter_head *filter_list; 2351 bool fail = true; 2352 int ret = -ENOMEM; 2353 int err; 2354 2355 filter_list = kmalloc_obj(*filter_list); 2356 if (!filter_list) 2357 return -ENOMEM; 2358 2359 INIT_LIST_HEAD(&filter_list->list); 2360 2361 list_for_each_entry(file, &tr->events, list) { 2362 2363 if (file->system != dir) 2364 continue; 2365 2366 filter = kzalloc_obj(*filter); 2367 if (!filter) 2368 goto fail_mem; 2369 2370 filter->filter_string = kstrdup(filter_string, GFP_KERNEL); 2371 if (!filter->filter_string) 2372 goto fail_mem; 2373 2374 err = process_preds(file->event_call, filter_string, filter, pe); 2375 if (err) { 2376 filter_disable(file); 2377 parse_error(pe, FILT_ERR_BAD_SUBSYS_FILTER, 0); 2378 append_filter_err(tr, pe, filter); 2379 } else { 2380 event_set_filtered_flag(file); 2381 fail = false; 2382 } 2383 2384 filter_item = kzalloc_obj(*filter_item); 2385 if (!filter_item) 2386 goto fail_mem; 2387 2388 list_add_tail(&filter_item->list, &filter_list->list); 2389 /* 2390 * Regardless of if this returned an error, we still 2391 * replace the filter for the call. 2392 */ 2393 filter_item->filter = event_filter(file); 2394 event_set_filter(file, filter); 2395 filter = NULL; 2396 } 2397 2398 if (fail) 2399 goto fail; 2400 2401 /* 2402 * The calls can still be using the old filters. 2403 * Do a synchronize_rcu() and to ensure all calls are 2404 * done with them before we free them. 2405 */ 2406 delay_free_filter(filter_list); 2407 return 0; 2408 fail: 2409 /* No call succeeded */ 2410 parse_error(pe, FILT_ERR_BAD_SUBSYS_FILTER, 0); 2411 ret = -EINVAL; 2412 fail_mem: 2413 __free_filter(filter); 2414 2415 /* If any filter was replaced, we still need to sync */ 2416 if (!list_empty(&filter_list->list)) 2417 delay_free_filter(filter_list); 2418 else 2419 free_filter_list(filter_list); 2420 2421 return ret; 2422 } 2423 2424 static int create_filter_start(char *filter_string, bool set_str, 2425 struct filter_parse_error **pse, 2426 struct event_filter **filterp) 2427 { 2428 struct event_filter *filter; 2429 struct filter_parse_error *pe = NULL; 2430 int err = 0; 2431 2432 if (WARN_ON_ONCE(*pse || *filterp)) 2433 return -EINVAL; 2434 2435 filter = kzalloc_obj(*filter); 2436 if (filter && set_str) { 2437 filter->filter_string = kstrdup(filter_string, GFP_KERNEL); 2438 if (!filter->filter_string) 2439 err = -ENOMEM; 2440 } 2441 2442 pe = kzalloc_obj(*pe); 2443 2444 if (!filter || !pe || err) { 2445 kfree(pe); 2446 __free_filter(filter); 2447 return -ENOMEM; 2448 } 2449 2450 /* we're committed to creating a new filter */ 2451 *filterp = filter; 2452 *pse = pe; 2453 2454 return 0; 2455 } 2456 2457 static void create_filter_finish(struct filter_parse_error *pe) 2458 { 2459 kfree(pe); 2460 } 2461 2462 /** 2463 * create_filter - create a filter for a trace_event_call 2464 * @tr: the trace array associated with these events 2465 * @call: trace_event_call to create a filter for 2466 * @filter_string: filter string 2467 * @set_str: remember @filter_str and enable detailed error in filter 2468 * @filterp: out param for created filter (always updated on return) 2469 * Must be a pointer that references a NULL pointer. 2470 * 2471 * Creates a filter for @call with @filter_str. If @set_str is %true, 2472 * @filter_str is copied and recorded in the new filter. 2473 * 2474 * On success, returns 0 and *@filterp points to the new filter. On 2475 * failure, returns -errno and *@filterp may point to %NULL or to a new 2476 * filter. In the latter case, the returned filter contains error 2477 * information if @set_str is %true and the caller is responsible for 2478 * freeing it. 2479 */ 2480 static int create_filter(struct trace_array *tr, 2481 struct trace_event_call *call, 2482 char *filter_string, bool set_str, 2483 struct event_filter **filterp) 2484 { 2485 struct filter_parse_error *pe = NULL; 2486 int err; 2487 2488 /* filterp must point to NULL */ 2489 if (WARN_ON(*filterp)) 2490 *filterp = NULL; 2491 2492 err = create_filter_start(filter_string, set_str, &pe, filterp); 2493 if (err) 2494 return err; 2495 2496 err = process_preds(call, filter_string, *filterp, pe); 2497 if (err && set_str) 2498 append_filter_err(tr, pe, *filterp); 2499 create_filter_finish(pe); 2500 2501 return err; 2502 } 2503 2504 int create_event_filter(struct trace_array *tr, 2505 struct trace_event_call *call, 2506 char *filter_str, bool set_str, 2507 struct event_filter **filterp) 2508 { 2509 return create_filter(tr, call, filter_str, set_str, filterp); 2510 } 2511 2512 /** 2513 * create_system_filter - create a filter for an event subsystem 2514 * @dir: the descriptor for the subsystem directory 2515 * @filter_str: filter string 2516 * @filterp: out param for created filter (always updated on return) 2517 * 2518 * Identical to create_filter() except that it creates a subsystem filter 2519 * and always remembers @filter_str. 2520 */ 2521 static int create_system_filter(struct trace_subsystem_dir *dir, 2522 char *filter_str, struct event_filter **filterp) 2523 { 2524 struct filter_parse_error *pe = NULL; 2525 int err; 2526 2527 err = create_filter_start(filter_str, true, &pe, filterp); 2528 if (!err) { 2529 err = process_system_preds(dir, dir->tr, pe, filter_str); 2530 if (!err) { 2531 /* System filters just show a default message */ 2532 kfree((*filterp)->filter_string); 2533 (*filterp)->filter_string = NULL; 2534 } else { 2535 append_filter_err(dir->tr, pe, *filterp); 2536 } 2537 } 2538 create_filter_finish(pe); 2539 2540 return err; 2541 } 2542 2543 /* caller must hold event_mutex */ 2544 int apply_event_filter(struct trace_event_file *file, char *filter_string) 2545 { 2546 struct trace_event_call *call = file->event_call; 2547 struct event_filter *filter = NULL; 2548 int err; 2549 2550 if (file->flags & EVENT_FILE_FL_FREED) 2551 return -ENODEV; 2552 2553 if (!strcmp(strstrip(filter_string), "0")) { 2554 filter_disable(file); 2555 filter = event_filter(file); 2556 2557 if (!filter) 2558 return 0; 2559 2560 event_clear_filter(file); 2561 2562 try_delay_free_filter(filter); 2563 2564 return 0; 2565 } 2566 2567 err = create_filter(file->tr, call, filter_string, true, &filter); 2568 2569 /* 2570 * Always swap the call filter with the new filter 2571 * even if there was an error. If there was an error 2572 * in the filter, we disable the filter and show the error 2573 * string 2574 */ 2575 if (filter) { 2576 struct event_filter *tmp; 2577 2578 tmp = event_filter(file); 2579 if (!err) 2580 event_set_filtered_flag(file); 2581 else 2582 filter_disable(file); 2583 2584 event_set_filter(file, filter); 2585 2586 if (tmp) 2587 try_delay_free_filter(tmp); 2588 } 2589 2590 return err; 2591 } 2592 2593 int apply_subsystem_event_filter(struct trace_subsystem_dir *dir, 2594 char *filter_string) 2595 { 2596 struct event_subsystem *system = dir->subsystem; 2597 struct trace_array *tr = dir->tr; 2598 struct event_filter *filter = NULL; 2599 int err = 0; 2600 2601 guard(mutex)(&event_mutex); 2602 2603 /* Make sure the system still has events */ 2604 if (!dir->nr_events) 2605 return -ENODEV; 2606 2607 if (!strcmp(strstrip(filter_string), "0")) { 2608 filter_free_subsystem_preds(dir, tr); 2609 remove_filter_string(system->filter); 2610 filter = system->filter; 2611 system->filter = NULL; 2612 /* Ensure all filters are no longer used */ 2613 filter_free_subsystem_filters(dir, tr, filter); 2614 return 0; 2615 } 2616 2617 err = create_system_filter(dir, filter_string, &filter); 2618 if (filter) { 2619 /* 2620 * No event actually uses the system filter 2621 * we can free it without synchronize_rcu(). 2622 */ 2623 __free_filter(system->filter); 2624 system->filter = filter; 2625 } 2626 2627 return err; 2628 } 2629 2630 #ifdef CONFIG_PERF_EVENTS 2631 2632 void ftrace_profile_free_filter(struct perf_event *event) 2633 { 2634 struct event_filter *filter = event->filter; 2635 2636 event->filter = NULL; 2637 __free_filter(filter); 2638 } 2639 2640 struct function_filter_data { 2641 struct ftrace_ops *ops; 2642 int first_filter; 2643 int first_notrace; 2644 }; 2645 2646 #ifdef CONFIG_FUNCTION_TRACER 2647 static char ** 2648 ftrace_function_filter_re(char *buf, int len, int *count) 2649 { 2650 char *str, **re; 2651 2652 str = kstrndup(buf, len, GFP_KERNEL); 2653 if (!str) 2654 return NULL; 2655 2656 /* 2657 * The argv_split function takes white space 2658 * as a separator, so convert ',' into spaces. 2659 */ 2660 strreplace(str, ',', ' '); 2661 2662 re = argv_split(GFP_KERNEL, str, count); 2663 kfree(str); 2664 return re; 2665 } 2666 2667 static int ftrace_function_set_regexp(struct ftrace_ops *ops, int filter, 2668 int reset, char *re, int len) 2669 { 2670 int ret; 2671 2672 if (filter) 2673 ret = ftrace_set_filter(ops, re, len, reset); 2674 else 2675 ret = ftrace_set_notrace(ops, re, len, reset); 2676 2677 return ret; 2678 } 2679 2680 static int __ftrace_function_set_filter(int filter, char *buf, int len, 2681 struct function_filter_data *data) 2682 { 2683 int i, re_cnt, ret = -EINVAL; 2684 int *reset; 2685 char **re; 2686 2687 reset = filter ? &data->first_filter : &data->first_notrace; 2688 2689 /* 2690 * The 'ip' field could have multiple filters set, separated 2691 * either by space or comma. We first cut the filter and apply 2692 * all pieces separately. 2693 */ 2694 re = ftrace_function_filter_re(buf, len, &re_cnt); 2695 if (!re) 2696 return -EINVAL; 2697 2698 for (i = 0; i < re_cnt; i++) { 2699 ret = ftrace_function_set_regexp(data->ops, filter, *reset, 2700 re[i], strlen(re[i])); 2701 if (ret) 2702 break; 2703 2704 if (*reset) 2705 *reset = 0; 2706 } 2707 2708 argv_free(re); 2709 return ret; 2710 } 2711 2712 static int ftrace_function_check_pred(struct filter_pred *pred) 2713 { 2714 struct ftrace_event_field *field = pred->field; 2715 2716 /* 2717 * Check the predicate for function trace, verify: 2718 * - only '==' and '!=' is used 2719 * - the 'ip' field is used 2720 */ 2721 if ((pred->op != OP_EQ) && (pred->op != OP_NE)) 2722 return -EINVAL; 2723 2724 if (strcmp(field->name, "ip")) 2725 return -EINVAL; 2726 2727 return 0; 2728 } 2729 2730 static int ftrace_function_set_filter_pred(struct filter_pred *pred, 2731 struct function_filter_data *data) 2732 { 2733 int ret; 2734 2735 /* Checking the node is valid for function trace. */ 2736 ret = ftrace_function_check_pred(pred); 2737 if (ret) 2738 return ret; 2739 2740 return __ftrace_function_set_filter(pred->op == OP_EQ, 2741 pred->regex->pattern, 2742 pred->regex->len, 2743 data); 2744 } 2745 2746 static bool is_or(struct prog_entry *prog, int i) 2747 { 2748 int target; 2749 2750 /* 2751 * Only "||" is allowed for function events, thus, 2752 * all true branches should jump to true, and any 2753 * false branch should jump to false. 2754 */ 2755 target = prog[i].target + 1; 2756 /* True and false have NULL preds (all prog entries should jump to one */ 2757 if (prog[target].pred) 2758 return false; 2759 2760 /* prog[target].target is 1 for TRUE, 0 for FALSE */ 2761 return prog[i].when_to_branch == prog[target].target; 2762 } 2763 2764 static int ftrace_function_set_filter(struct perf_event *event, 2765 struct event_filter *filter) 2766 { 2767 struct prog_entry *prog = rcu_dereference_protected(filter->prog, 2768 lockdep_is_held(&event_mutex)); 2769 struct function_filter_data data = { 2770 .first_filter = 1, 2771 .first_notrace = 1, 2772 .ops = &event->ftrace_ops, 2773 }; 2774 int i; 2775 2776 for (i = 0; prog[i].pred; i++) { 2777 struct filter_pred *pred = prog[i].pred; 2778 2779 if (!is_or(prog, i)) 2780 return -EINVAL; 2781 2782 if (ftrace_function_set_filter_pred(pred, &data) < 0) 2783 return -EINVAL; 2784 } 2785 return 0; 2786 } 2787 #else 2788 static int ftrace_function_set_filter(struct perf_event *event, 2789 struct event_filter *filter) 2790 { 2791 return -ENODEV; 2792 } 2793 #endif /* CONFIG_FUNCTION_TRACER */ 2794 2795 static int ftrace_profile_filter_perm(struct event_filter *filter) 2796 { 2797 struct prog_entry *prog = rcu_dereference_protected(filter->prog, 2798 lockdep_is_held(&event_mutex)); 2799 int i, err; 2800 2801 for (i = 0; prog[i].pred; i++) { 2802 if (prog[i].pred->fn_num != FILTER_PRED_FN_PCHAR) 2803 continue; 2804 2805 /* 2806 * Even a counting filter can reveal kernel string data. 2807 * Require the same access as raw kernel tracepoints. 2808 */ 2809 err = perf_allow_kernel(); 2810 if (err) 2811 return err; 2812 return perf_allow_tracepoint(); 2813 } 2814 return 0; 2815 } 2816 2817 int ftrace_profile_set_filter(struct perf_event *event, int event_id, 2818 char *filter_str) 2819 { 2820 int err; 2821 struct event_filter *filter = NULL; 2822 struct trace_event_call *call; 2823 2824 guard(mutex)(&event_mutex); 2825 2826 call = event->tp_event; 2827 2828 if (!call) 2829 return -EINVAL; 2830 2831 if (event->filter) 2832 return -EEXIST; 2833 2834 err = create_filter(NULL, call, filter_str, false, &filter); 2835 if (err) 2836 goto free_filter; 2837 2838 err = ftrace_profile_filter_perm(filter); 2839 if (err) 2840 goto free_filter; 2841 2842 if (ftrace_event_is_function(call)) 2843 err = ftrace_function_set_filter(event, filter); 2844 else 2845 event->filter = filter; 2846 2847 free_filter: 2848 if (err || ftrace_event_is_function(call)) 2849 __free_filter(filter); 2850 2851 return err; 2852 } 2853 2854 #endif /* CONFIG_PERF_EVENTS */ 2855 2856 #ifdef CONFIG_FTRACE_STARTUP_TEST 2857 2858 #include <linux/types.h> 2859 #include <linux/tracepoint.h> 2860 2861 #include "trace_probe.h" 2862 2863 #define CREATE_TRACE_POINTS 2864 #include "trace_events_filter_test.h" 2865 2866 #define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \ 2867 { \ 2868 .filter = FILTER, \ 2869 .rec = { .a = va, .b = vb, .c = vc, .d = vd, \ 2870 .e = ve, .f = vf, .g = vg, .h = vh }, \ 2871 .match = m, \ 2872 .not_visited = nvisit, \ 2873 } 2874 2875 #define DATA_STR(m, s) \ 2876 { \ 2877 .filter = FILTER, \ 2878 .str = s, \ 2879 .match = m, \ 2880 .not_visited = "", \ 2881 } 2882 2883 #define YES 1 2884 #define NO 0 2885 2886 static struct test_filter_data_t { 2887 char *filter; 2888 struct trace_event_raw_ftrace_test_filter rec; 2889 const char *str; 2890 int match; 2891 char *not_visited; 2892 } test_filter_data[] = { 2893 #define FILTER "a == 1 && b == 1 && c == 1 && d == 1 && " \ 2894 "e == 1 && f == 1 && g == 1 && h == 1" 2895 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, ""), 2896 DATA_REC(NO, 0, 1, 1, 1, 1, 1, 1, 1, "bcdefgh"), 2897 DATA_REC(NO, 1, 1, 1, 1, 1, 1, 1, 0, ""), 2898 #undef FILTER 2899 #define FILTER "a == 1 || b == 1 || c == 1 || d == 1 || " \ 2900 "e == 1 || f == 1 || g == 1 || h == 1" 2901 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""), 2902 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 0, 1, ""), 2903 DATA_REC(YES, 1, 0, 0, 0, 0, 0, 0, 0, "bcdefgh"), 2904 #undef FILTER 2905 #define FILTER "(a == 1 || b == 1) && (c == 1 || d == 1) && " \ 2906 "(e == 1 || f == 1) && (g == 1 || h == 1)" 2907 DATA_REC(NO, 0, 0, 1, 1, 1, 1, 1, 1, "dfh"), 2908 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""), 2909 DATA_REC(YES, 1, 0, 1, 0, 0, 1, 0, 1, "bd"), 2910 DATA_REC(NO, 1, 0, 1, 0, 0, 1, 0, 0, "bd"), 2911 #undef FILTER 2912 #define FILTER "(a == 1 && b == 1) || (c == 1 && d == 1) || " \ 2913 "(e == 1 && f == 1) || (g == 1 && h == 1)" 2914 DATA_REC(YES, 1, 0, 1, 1, 1, 1, 1, 1, "efgh"), 2915 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 1, 1, ""), 2916 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""), 2917 #undef FILTER 2918 #define FILTER "(a == 1 && b == 1) && (c == 1 && d == 1) && " \ 2919 "(e == 1 && f == 1) || (g == 1 && h == 1)" 2920 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 0, "gh"), 2921 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""), 2922 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, ""), 2923 #undef FILTER 2924 #define FILTER "((a == 1 || b == 1) || (c == 1 || d == 1) || " \ 2925 "(e == 1 || f == 1)) && (g == 1 || h == 1)" 2926 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 1, "bcdef"), 2927 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""), 2928 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, "h"), 2929 #undef FILTER 2930 #define FILTER "((((((((a == 1) && (b == 1)) || (c == 1)) && (d == 1)) || " \ 2931 "(e == 1)) && (f == 1)) || (g == 1)) && (h == 1))" 2932 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "ceg"), 2933 DATA_REC(NO, 0, 1, 0, 1, 0, 1, 0, 1, ""), 2934 DATA_REC(NO, 1, 0, 1, 0, 1, 0, 1, 0, ""), 2935 #undef FILTER 2936 #define FILTER "((((((((a == 1) || (b == 1)) && (c == 1)) || (d == 1)) && " \ 2937 "(e == 1)) || (f == 1)) && (g == 1)) || (h == 1))" 2938 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "bdfh"), 2939 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""), 2940 DATA_REC(YES, 1, 0, 1, 0, 1, 0, 1, 0, "bdfh"), 2941 #undef FILTER 2942 #define FILTER "str ~ \"0001:*\"" 2943 DATA_STR(YES, "0001:01:00.0"), 2944 DATA_STR(YES, "0001:"), 2945 DATA_STR(NO, "0002:01:00.0"), 2946 DATA_STR(NO, "pci:0001:01:00.0"), 2947 DATA_STR(NO, "0001"), 2948 #undef FILTER 2949 #define FILTER "str ~ \"!0001:*\"" 2950 DATA_STR(NO, "0001:01:00.0"), 2951 DATA_STR(YES, "0002:01:00.0"), 2952 #undef FILTER 2953 #define FILTER "str ~ \"0001:*:00.[0-9]\"" 2954 DATA_STR(YES, "0001:01:00.0"), 2955 DATA_STR(YES, "0001:ff:00.7"), 2956 DATA_STR(NO, "0001:01:00.a"), 2957 DATA_STR(NO, "0002:01:00.0"), 2958 #undef FILTER 2959 #define FILTER "str ~ \"0001:01:00.0\"" 2960 DATA_STR(YES, "0001:01:00.0"), 2961 DATA_STR(NO, "0001:01:00.1"), 2962 DATA_STR(NO, "0001:01:00.00"), 2963 DATA_STR(NO, "0001:01:00"), 2964 #undef FILTER 2965 #define FILTER "str == \"0001:01:00.0\"" 2966 DATA_STR(YES, "0001:01:00.0"), 2967 DATA_STR(NO, "0001:01:00.1"), 2968 #undef FILTER 2969 #define FILTER "str ~ \"*0001:*\"" 2970 DATA_STR(YES, "0001:01:00.0"), 2971 DATA_STR(YES, "pci:0001:01:00.0"), 2972 DATA_STR(NO, "0002:01:00.0"), 2973 #undef FILTER 2974 #define FILTER "str ~ \"*:00.0\"" 2975 DATA_STR(YES, "0001:01:00.0"), 2976 DATA_STR(NO, "0001:01:00.1"), 2977 }; 2978 2979 #undef DATA_REC 2980 #undef DATA_STR 2981 #undef FILTER 2982 #undef YES 2983 #undef NO 2984 2985 #define DATA_CNT ARRAY_SIZE(test_filter_data) 2986 2987 static int test_pred_visited; 2988 2989 static int test_pred_visited_fn(struct filter_pred *pred, void *event) 2990 { 2991 struct ftrace_event_field *field = pred->field; 2992 2993 test_pred_visited = 1; 2994 printk(KERN_INFO "\npred visited %s\n", field->name); 2995 return 1; 2996 } 2997 2998 static void update_pred_fn(struct event_filter *filter, char *fields) 2999 { 3000 struct prog_entry *prog = rcu_dereference_protected(filter->prog, 3001 lockdep_is_held(&event_mutex)); 3002 int i; 3003 3004 for (i = 0; prog[i].pred; i++) { 3005 struct filter_pred *pred = prog[i].pred; 3006 struct ftrace_event_field *field = pred->field; 3007 3008 WARN_ON_ONCE(pred->fn_num == FILTER_PRED_FN_NOP); 3009 3010 if (!field) { 3011 WARN_ONCE(1, "all leafs should have field defined %d", i); 3012 continue; 3013 } 3014 3015 if (!strchr(fields, *field->name)) 3016 continue; 3017 3018 pred->fn_num = FILTER_PRED_TEST_VISITED; 3019 } 3020 } 3021 3022 static __init int ftrace_test_event_filter(void) 3023 { 3024 int i; 3025 3026 printk(KERN_INFO "Testing ftrace filter: "); 3027 3028 for (i = 0; i < DATA_CNT; i++) { 3029 struct event_filter *filter = NULL; 3030 struct test_filter_data_t *d = &test_filter_data[i]; 3031 union { 3032 struct trace_event_raw_ftrace_test_filter rec; 3033 /* Add some spare, to append the string contents. */ 3034 char buf[sizeof(struct trace_event_raw_ftrace_test_filter) + 32]; 3035 } event = { .rec = d->rec }; 3036 int err; 3037 3038 if (d->str) { 3039 struct ftrace_event_field *field; 3040 u32 offset = offsetof(typeof(event.rec), __data); 3041 u32 len = strlen(d->str) + 1; 3042 u32 *str_item; 3043 3044 field = trace_find_event_field(&event_ftrace_test_filter, "str"); 3045 if (WARN_ON_ONCE(!field)) 3046 break; 3047 if (WARN_ON_ONCE(offset + len > sizeof(event.buf))) 3048 break; 3049 str_item = (u32 *)&event.buf[field->offset]; 3050 *str_item = make_data_loc(len, offset); 3051 memcpy(&event.buf[offset], d->str, len); 3052 } 3053 3054 err = create_filter(NULL, &event_ftrace_test_filter, 3055 d->filter, false, &filter); 3056 if (err) { 3057 printk(KERN_INFO 3058 "Failed to get filter for '%s', err %d\n", 3059 d->filter, err); 3060 __free_filter(filter); 3061 break; 3062 } 3063 3064 /* Needed to dereference filter->prog */ 3065 mutex_lock(&event_mutex); 3066 /* 3067 * The preemption disabling is not really needed for self 3068 * tests, but the rcu dereference will complain without it. 3069 */ 3070 preempt_disable(); 3071 if (*d->not_visited) 3072 update_pred_fn(filter, d->not_visited); 3073 3074 test_pred_visited = 0; 3075 err = filter_match_preds(filter, &event.rec); 3076 preempt_enable(); 3077 3078 mutex_unlock(&event_mutex); 3079 3080 __free_filter(filter); 3081 3082 if (test_pred_visited) { 3083 printk(KERN_INFO 3084 "Failed, unwanted pred visited for filter %s\n", 3085 d->filter); 3086 break; 3087 } 3088 3089 if (err != d->match) { 3090 printk(KERN_INFO 3091 "Failed to match filter '%s', expected %d\n", 3092 d->filter, d->match); 3093 break; 3094 } 3095 } 3096 3097 if (i == DATA_CNT) 3098 printk(KERN_CONT "OK\n"); 3099 3100 /* Need to call ftrace_test_filter to prevent a warning */ 3101 if (!trace_ftrace_test_filter_enabled()) 3102 trace_ftrace_test_filter(1, 2, 3, 4, 5, 6, 7, 8, ""); 3103 3104 return 0; 3105 } 3106 3107 late_initcall(ftrace_test_event_filter); 3108 3109 #endif /* CONFIG_FTRACE_STARTUP_TEST */ 3110