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