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