1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * event tracer 4 * 5 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com> 6 * 7 * - Added format output of fields of the trace point. 8 * This was based off of work by Tom Zanussi <tzanussi@gmail.com>. 9 * 10 */ 11 12 #define pr_fmt(fmt) fmt 13 14 #include <linux/workqueue.h> 15 #include <linux/security.h> 16 #include <linux/spinlock.h> 17 #include <linux/seq_buf.h> 18 #include <linux/kthread.h> 19 #include <linux/tracefs.h> 20 #include <linux/uaccess.h> 21 #include <linux/module.h> 22 #include <linux/ctype.h> 23 #include <linux/sort.h> 24 #include <linux/slab.h> 25 #include <linux/delay.h> 26 #include <linux/btf.h> 27 28 #include <kunit/visibility.h> 29 30 #include <trace/events/sched.h> 31 #include <trace/syscall.h> 32 33 #include <asm/setup.h> 34 35 #include "trace_output.h" 36 37 #undef TRACE_SYSTEM 38 #define TRACE_SYSTEM "TRACE_SYSTEM" 39 40 DEFINE_MUTEX(event_mutex); 41 42 LIST_HEAD(ftrace_events); 43 static LIST_HEAD(ftrace_generic_fields); 44 static LIST_HEAD(ftrace_common_fields); 45 static bool eventdir_initialized; 46 47 static LIST_HEAD(module_strings); 48 49 struct module_string { 50 struct list_head next; 51 struct module *module; 52 char *str; 53 }; 54 55 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO) 56 57 static struct kmem_cache *field_cachep; 58 static struct kmem_cache *file_cachep; 59 60 static inline int system_refcount(struct event_subsystem *system) 61 { 62 return system->ref_count; 63 } 64 65 static int system_refcount_inc(struct event_subsystem *system) 66 { 67 return system->ref_count++; 68 } 69 70 static int system_refcount_dec(struct event_subsystem *system) 71 { 72 return --system->ref_count; 73 } 74 75 /* Double loops, do not use break, only goto's work */ 76 #define do_for_each_event_file(tr, file) \ 77 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \ 78 list_for_each_entry(file, &tr->events, list) 79 80 #define do_for_each_event_file_safe(tr, file) \ 81 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \ 82 struct trace_event_file *___n; \ 83 list_for_each_entry_safe(file, ___n, &tr->events, list) 84 85 #define while_for_each_event_file() \ 86 } 87 88 static struct ftrace_event_field * 89 __find_event_field(struct list_head *head, const char *name) 90 { 91 struct ftrace_event_field *field; 92 93 list_for_each_entry(field, head, link) { 94 if (!strcmp(field->name, name)) 95 return field; 96 } 97 98 return NULL; 99 } 100 101 struct ftrace_event_field * 102 trace_find_event_field(struct trace_event_call *call, char *name) 103 { 104 struct ftrace_event_field *field; 105 struct list_head *head; 106 107 head = trace_get_fields(call); 108 field = __find_event_field(head, name); 109 if (field) 110 return field; 111 112 field = __find_event_field(&ftrace_generic_fields, name); 113 if (field) 114 return field; 115 116 return __find_event_field(&ftrace_common_fields, name); 117 } 118 119 static int __trace_define_field(struct list_head *head, const char *type, 120 const char *name, int offset, int size, 121 int is_signed, int filter_type, int len, 122 int need_test) 123 { 124 struct ftrace_event_field *field; 125 126 field = kmem_cache_alloc(field_cachep, GFP_TRACE); 127 if (!field) 128 return -ENOMEM; 129 130 field->name = name; 131 field->type = type; 132 133 if (filter_type == FILTER_OTHER) 134 field->filter_type = filter_assign_type(type); 135 else 136 field->filter_type = filter_type; 137 138 field->offset = offset; 139 field->size = size; 140 field->is_signed = is_signed; 141 field->needs_test = need_test; 142 field->len = len; 143 144 list_add(&field->link, head); 145 146 return 0; 147 } 148 149 int trace_define_field(struct trace_event_call *call, const char *type, 150 const char *name, int offset, int size, int is_signed, 151 int filter_type) 152 { 153 struct list_head *head; 154 155 if (WARN_ON(!call->class)) 156 return 0; 157 158 head = trace_get_fields(call); 159 return __trace_define_field(head, type, name, offset, size, 160 is_signed, filter_type, 0, 0); 161 } 162 EXPORT_SYMBOL_GPL(trace_define_field); 163 164 static int trace_define_field_ext(struct trace_event_call *call, const char *type, 165 const char *name, int offset, int size, int is_signed, 166 int filter_type, int len, int need_test) 167 { 168 struct list_head *head; 169 170 if (WARN_ON(!call->class)) 171 return 0; 172 173 head = trace_get_fields(call); 174 return __trace_define_field(head, type, name, offset, size, 175 is_signed, filter_type, len, need_test); 176 } 177 178 #define __generic_field(type, item, filter_type) \ 179 ret = __trace_define_field(&ftrace_generic_fields, #type, \ 180 #item, 0, 0, is_signed_type(type), \ 181 filter_type, 0, 0); \ 182 if (ret) \ 183 return ret; 184 185 #define __common_field(type, item) \ 186 ret = __trace_define_field(&ftrace_common_fields, #type, \ 187 "common_" #item, \ 188 offsetof(typeof(ent), item), \ 189 sizeof(ent.item), \ 190 is_signed_type(type), FILTER_OTHER, \ 191 0, 0); \ 192 if (ret) \ 193 return ret; 194 195 static int trace_define_generic_fields(void) 196 { 197 int ret; 198 199 __generic_field(int, CPU, FILTER_CPU); 200 __generic_field(int, cpu, FILTER_CPU); 201 __generic_field(int, common_cpu, FILTER_CPU); 202 __generic_field(char *, COMM, FILTER_COMM); 203 __generic_field(char *, comm, FILTER_COMM); 204 __generic_field(char *, stacktrace, FILTER_STACKTRACE); 205 __generic_field(char *, STACKTRACE, FILTER_STACKTRACE); 206 #ifdef CONFIG_STACKTRACE 207 __generic_field(char *, within, FILTER_WITHIN); 208 #endif 209 210 return ret; 211 } 212 213 static int trace_define_common_fields(void) 214 { 215 int ret; 216 struct trace_entry ent; 217 218 __common_field(unsigned short, type); 219 __common_field(unsigned char, flags); 220 /* Holds both preempt_count and migrate_disable */ 221 __common_field(unsigned char, preempt_count); 222 __common_field(int, pid); 223 224 return ret; 225 } 226 227 static void trace_destroy_fields(struct trace_event_call *call) 228 { 229 struct ftrace_event_field *field, *next; 230 struct list_head *head; 231 232 head = trace_get_fields(call); 233 list_for_each_entry_safe(field, next, head, link) { 234 list_del(&field->link); 235 kmem_cache_free(field_cachep, field); 236 } 237 } 238 239 /* 240 * run-time version of trace_event_get_offsets_<call>() that returns the last 241 * accessible offset of trace fields excluding __dynamic_array bytes 242 */ 243 int trace_event_get_offsets(struct trace_event_call *call) 244 { 245 struct ftrace_event_field *tail; 246 struct list_head *head; 247 248 head = trace_get_fields(call); 249 250 if (list_empty(head)) 251 return sizeof(struct trace_entry); 252 253 /* 254 * head->next points to the last field with the largest offset, 255 * since it was added last by trace_define_field() 256 */ 257 tail = list_first_entry(head, struct ftrace_event_field, link); 258 return tail->offset + tail->size; 259 } 260 261 262 static struct trace_event_fields *find_event_field(const char *fmt, 263 struct trace_event_call *call) 264 { 265 struct trace_event_fields *field = call->class->fields_array; 266 const char *p = fmt; 267 int len; 268 269 if (!(len = str_has_prefix(fmt, "REC->"))) 270 return NULL; 271 fmt += len; 272 for (p = fmt; *p; p++) { 273 if (!isalnum(*p) && *p != '_') 274 break; 275 } 276 len = p - fmt; 277 278 for (; field->type; field++) { 279 if (strncmp(field->name, fmt, len) || field->name[len]) 280 continue; 281 282 return field; 283 } 284 return NULL; 285 } 286 287 /* 288 * Check if the referenced field is an array and return true, 289 * as arrays are OK to dereference. 290 */ 291 static bool test_field(const char *fmt, struct trace_event_call *call) 292 { 293 struct trace_event_fields *field; 294 295 field = find_event_field(fmt, call); 296 if (!field) 297 return false; 298 299 /* This is an array and is OK to dereference. */ 300 return strchr(field->type, '[') != NULL; 301 } 302 303 /* Look for a string within an argument */ 304 static bool find_print_string(const char *arg, const char *str, const char *end) 305 { 306 const char *r; 307 308 r = strstr(arg, str); 309 return r && r < end; 310 } 311 312 /* Return true if the argument pointer is safe */ 313 static bool process_pointer(const char *fmt, int len, struct trace_event_call *call) 314 { 315 const char *r, *e, *a; 316 317 e = fmt + len; 318 319 /* Find the REC-> in the argument */ 320 r = strstr(fmt, "REC->"); 321 if (r && r < e) { 322 /* 323 * Addresses of events on the buffer, or an array on the buffer is 324 * OK to dereference. There's ways to fool this, but 325 * this is to catch common mistakes, not malicious code. 326 */ 327 a = strchr(fmt, '&'); 328 if ((a && (a < r)) || test_field(r, call)) 329 return true; 330 } else if (find_print_string(fmt, "__get_dynamic_array(", e)) { 331 return true; 332 } else if (find_print_string(fmt, "__get_rel_dynamic_array(", e)) { 333 return true; 334 } else if (find_print_string(fmt, "__get_dynamic_array_len(", e)) { 335 return true; 336 } else if (find_print_string(fmt, "__get_rel_dynamic_array_len(", e)) { 337 return true; 338 } else if (find_print_string(fmt, "__get_sockaddr(", e)) { 339 return true; 340 } else if (find_print_string(fmt, "__get_rel_sockaddr(", e)) { 341 return true; 342 } 343 return false; 344 } 345 346 /* Return true if the string is safe */ 347 static bool process_string(const char *fmt, int len, struct trace_event_call *call) 348 { 349 struct trace_event_fields *field; 350 const char *r, *e, *s; 351 352 e = fmt + len; 353 354 /* 355 * There are several helper functions that return strings. 356 * If the argument contains a function, then assume its field is valid. 357 * It is considered that the argument has a function if it has: 358 * alphanumeric or '_' before a parenthesis. 359 */ 360 s = fmt; 361 do { 362 r = strstr(s, "("); 363 if (!r || r >= e) 364 break; 365 for (int i = 1; r - i >= s; i++) { 366 char ch = *(r - i); 367 if (isspace(ch)) 368 continue; 369 if (isalnum(ch) || ch == '_') 370 return true; 371 /* Anything else, this isn't a function */ 372 break; 373 } 374 /* A function could be wrapped in parenthesis, try the next one */ 375 s = r + 1; 376 } while (s < e); 377 378 /* 379 * Check for arrays. If the argument has: foo[REC->val] 380 * then it is very likely that foo is an array of strings 381 * that are safe to use. 382 */ 383 r = strstr(s, "["); 384 if (r && r < e) { 385 r = strstr(r, "REC->"); 386 if (r && r < e) 387 return true; 388 } 389 390 /* 391 * If there's any strings in the argument consider this arg OK as it 392 * could be: REC->field ? "foo" : "bar" and we don't want to get into 393 * verifying that logic here. 394 */ 395 if (find_print_string(fmt, "\"", e)) 396 return true; 397 398 /* Dereferenced strings are also valid like any other pointer */ 399 if (process_pointer(fmt, len, call)) 400 return true; 401 402 /* Make sure the field is found */ 403 field = find_event_field(fmt, call); 404 if (!field) 405 return false; 406 407 /* Test this field's string before printing the event */ 408 call->flags |= TRACE_EVENT_FL_TEST_STR; 409 field->needs_test = 1; 410 411 return true; 412 } 413 414 static void test_double_dereference(const char *str, int len, 415 struct trace_event_call *call) 416 { 417 const char *ptr; 418 const char *end = str + len; 419 420 ptr = strstr(str, "REC->"); 421 422 while (ptr && ptr < end) { 423 424 ptr += 5; 425 for (; ptr < end; ptr++) { 426 if (ptr[0] == '-' && ptr[1] == '>') { 427 pr_warn("TRACE EVENT ERROR: Event %s has double dereference in TP_printk: %.*s\n", 428 trace_event_name(call), len, str); 429 WARN_ONCE(1, "Event %s has double dereference in TP_printk: %.*s\n", 430 trace_event_name(call), len, str); 431 return; 432 } 433 if (!isalnum(*ptr) && *ptr != '_') 434 break; 435 } 436 437 ptr = strstr(ptr, "REC->"); 438 } 439 } 440 441 static void handle_dereference_arg(const char *arg_str, u64 string_flags, int len, 442 u64 *dereference_flags, int arg, 443 struct trace_event_call *call) 444 { 445 if (string_flags & (1ULL << arg)) { 446 if (process_string(arg_str, len, call)) 447 *dereference_flags &= ~(1ULL << arg); 448 } else if (process_pointer(arg_str, len, call)) 449 *dereference_flags &= ~(1ULL << arg); 450 else 451 pr_warn("TRACE EVENT ERROR: Bad dereference argument: '%.*s'\n", 452 len, arg_str); 453 } 454 455 /* 456 * Examine the print fmt of the event looking for unsafe dereference 457 * pointers using %p* that could be recorded in the trace event and 458 * much later referenced after the pointer was freed. Dereferencing 459 * pointers are OK, if it is dereferenced into the event itself. 460 */ 461 static void test_event_printk(struct trace_event_call *call) 462 { 463 u64 dereference_flags = 0; 464 u64 string_flags = 0; 465 bool first = true; 466 const char *fmt; 467 int parens = 0; 468 char in_quote = 0; 469 int start_arg = 0; 470 int arg = 0; 471 int i, e; 472 473 fmt = call->print_fmt; 474 475 if (!fmt) 476 return; 477 478 for (i = 0; fmt[i]; i++) { 479 switch (fmt[i]) { 480 case '\\': 481 i++; 482 if (!fmt[i]) 483 return; 484 continue; 485 case '"': 486 case '\'': 487 /* 488 * The print fmt starts with a string that 489 * is processed first to find %p* usage, 490 * then after the first string, the print fmt 491 * contains arguments that are used to check 492 * if the dereferenced %p* usage is safe. 493 */ 494 if (first) { 495 if (fmt[i] == '\'') 496 continue; 497 if (in_quote) { 498 arg = 0; 499 first = false; 500 } 501 } 502 if (in_quote) { 503 if (in_quote == fmt[i]) 504 in_quote = 0; 505 } else { 506 in_quote = fmt[i]; 507 } 508 continue; 509 case '%': 510 if (!first || !in_quote) 511 continue; 512 i++; 513 if (!fmt[i]) 514 return; 515 switch (fmt[i]) { 516 case '%': 517 continue; 518 case 'p': 519 do_pointer: 520 /* Find dereferencing fields */ 521 switch (fmt[i + 1]) { 522 case 'B': case 'R': case 'r': 523 case 'b': case 'M': case 'm': 524 case 'I': case 'i': case 'E': 525 case 'U': case 'V': case 'N': 526 case 'a': case 'd': case 'D': 527 case 'g': case 't': case 'C': 528 case 'O': case 'f': 529 if (WARN_ONCE(arg == 63, 530 "Too many args for event: %s", 531 trace_event_name(call))) 532 return; 533 dereference_flags |= 1ULL << arg; 534 } 535 break; 536 default: 537 { 538 bool star = false; 539 int j; 540 541 /* Increment arg if %*s exists. */ 542 for (j = 0; fmt[i + j]; j++) { 543 if (isdigit(fmt[i + j]) || 544 fmt[i + j] == '.') 545 continue; 546 if (fmt[i + j] == '*') { 547 star = true; 548 /* Handle %*pbl case */ 549 if (!j && fmt[i + 1] == 'p') { 550 arg++; 551 i++; 552 goto do_pointer; 553 } 554 continue; 555 } 556 if ((fmt[i + j] == 's')) { 557 if (star) 558 arg++; 559 if (WARN_ONCE(arg == 63, 560 "Too many args for event: %s", 561 trace_event_name(call))) 562 return; 563 dereference_flags |= 1ULL << arg; 564 string_flags |= 1ULL << arg; 565 } 566 break; 567 } 568 break; 569 } /* default */ 570 571 } /* switch */ 572 arg++; 573 continue; 574 case '(': 575 if (in_quote) 576 continue; 577 parens++; 578 continue; 579 case ')': 580 if (in_quote) 581 continue; 582 parens--; 583 if (WARN_ONCE(parens < 0, 584 "Paren mismatch for event: %s\narg='%s'\n%*s", 585 trace_event_name(call), 586 fmt + start_arg, 587 (i - start_arg) + 5, "^")) 588 return; 589 continue; 590 case ',': 591 if (in_quote || parens) 592 continue; 593 e = i; 594 i++; 595 while (isspace(fmt[i])) 596 i++; 597 598 /* 599 * If start_arg is zero, then this is the start of the 600 * first argument. The processing of the argument happens 601 * when the end of the argument is found, as it needs to 602 * handle parenthesis and such. 603 */ 604 if (!start_arg) { 605 start_arg = i; 606 /* Balance out the i++ in the for loop */ 607 i--; 608 continue; 609 } 610 611 test_double_dereference(fmt + start_arg, e - start_arg, call); 612 613 if (dereference_flags & (1ULL << arg)) { 614 handle_dereference_arg(fmt + start_arg, string_flags, 615 e - start_arg, 616 &dereference_flags, arg, call); 617 } 618 619 start_arg = i; 620 arg++; 621 /* Balance out the i++ in the for loop */ 622 i--; 623 } 624 } 625 626 test_double_dereference(fmt + start_arg, i - start_arg, call); 627 628 if (dereference_flags & (1ULL << arg)) { 629 handle_dereference_arg(fmt + start_arg, string_flags, 630 i - start_arg, 631 &dereference_flags, arg, call); 632 } 633 634 /* 635 * If you triggered the below warning, the trace event reported 636 * uses an unsafe dereference pointer %p*. As the data stored 637 * at the trace event time may no longer exist when the trace 638 * event is printed, dereferencing to the original source is 639 * unsafe. The source of the dereference must be copied into the 640 * event itself, and the dereference must access the copy instead. 641 */ 642 if (WARN_ON_ONCE(dereference_flags)) { 643 arg = 1; 644 while (!(dereference_flags & 1)) { 645 dereference_flags >>= 1; 646 arg++; 647 } 648 pr_warn("event %s has unsafe dereference of argument %d\n", 649 trace_event_name(call), arg); 650 pr_warn("print_fmt: %s\n", fmt); 651 } 652 } 653 654 int trace_event_raw_init(struct trace_event_call *call) 655 { 656 int id; 657 658 id = register_trace_event(&call->event); 659 if (!id) 660 return -ENODEV; 661 662 test_event_printk(call); 663 664 return 0; 665 } 666 EXPORT_SYMBOL_GPL(trace_event_raw_init); 667 668 bool trace_event_ignore_this_pid(struct trace_event_file *trace_file) 669 { 670 struct trace_array *tr = trace_file->tr; 671 struct trace_pid_list *no_pid_list; 672 struct trace_pid_list *pid_list; 673 674 pid_list = rcu_dereference_raw(tr->filtered_pids); 675 no_pid_list = rcu_dereference_raw(tr->filtered_no_pids); 676 677 if (!pid_list && !no_pid_list) 678 return false; 679 680 /* 681 * This is recorded at every sched_switch for this task. 682 * Thus, even if the task migrates the ignore value will be the same. 683 */ 684 return this_cpu_read(tr->array_buffer.data->ignore_pid) != 0; 685 } 686 EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid); 687 688 /** 689 * trace_event_buffer_reserve - reserve space on the ring buffer for an event 690 * @fbuffer: information about how to save the event 691 * @trace_file: the instance file descriptor for the event 692 * @len: The length of the event 693 * 694 * The @fbuffer has information about the ring buffer and data will 695 * be added to it to be used by the call to trace_event_buffer_commit(). 696 * The @trace_file is the desrciptor with information about the status 697 * of the given event for a specific trace_array instance. 698 * The @len is the length of data to save for the event. 699 * 700 * Returns a pointer to the data on the ring buffer or NULL if the 701 * event was not reserved (event was filtered, too big, or the buffer 702 * simply was disabled for write). 703 */ 704 void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer, 705 struct trace_event_file *trace_file, 706 unsigned long len) 707 { 708 struct trace_event_call *event_call = trace_file->event_call; 709 710 if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) && 711 trace_event_ignore_this_pid(trace_file)) 712 return NULL; 713 714 /* 715 * If CONFIG_PREEMPTION is enabled, then the tracepoint itself disables 716 * preemption (adding one to the preempt_count). Since we are 717 * interested in the preempt_count at the time the tracepoint was 718 * hit, we need to subtract one to offset the increment. 719 */ 720 fbuffer->trace_ctx = tracing_gen_ctx_dec(); 721 fbuffer->trace_file = trace_file; 722 723 fbuffer->event = 724 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file, 725 event_call->event.type, len, 726 fbuffer->trace_ctx); 727 if (!fbuffer->event) 728 return NULL; 729 730 fbuffer->regs = NULL; 731 fbuffer->entry = ring_buffer_event_data(fbuffer->event); 732 return fbuffer->entry; 733 } 734 EXPORT_SYMBOL_GPL(trace_event_buffer_reserve); 735 736 int trace_event_reg(struct trace_event_call *call, 737 enum trace_reg type, void *data) 738 { 739 struct trace_event_file *file = data; 740 741 WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT)); 742 switch (type) { 743 case TRACE_REG_REGISTER: 744 return tracepoint_probe_register(call->tp, 745 call->class->probe, 746 file); 747 case TRACE_REG_UNREGISTER: 748 tracepoint_probe_unregister(call->tp, 749 call->class->probe, 750 file); 751 return 0; 752 753 #ifdef CONFIG_PERF_EVENTS 754 case TRACE_REG_PERF_REGISTER: 755 if (!call->class->perf_probe) 756 return -ENODEV; 757 return tracepoint_probe_register(call->tp, 758 call->class->perf_probe, 759 call); 760 case TRACE_REG_PERF_UNREGISTER: 761 tracepoint_probe_unregister(call->tp, 762 call->class->perf_probe, 763 call); 764 return 0; 765 case TRACE_REG_PERF_OPEN: 766 case TRACE_REG_PERF_CLOSE: 767 case TRACE_REG_PERF_ADD: 768 case TRACE_REG_PERF_DEL: 769 return 0; 770 #endif 771 } 772 return 0; 773 } 774 EXPORT_SYMBOL_GPL(trace_event_reg); 775 776 void trace_event_enable_cmd_record(bool enable) 777 { 778 struct trace_event_file *file; 779 struct trace_array *tr; 780 781 lockdep_assert_held(&event_mutex); 782 783 do_for_each_event_file(tr, file) { 784 785 if (!(file->flags & EVENT_FILE_FL_ENABLED)) 786 continue; 787 788 if (enable) { 789 tracing_start_cmdline_record(); 790 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags); 791 } else { 792 tracing_stop_cmdline_record(); 793 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags); 794 } 795 } while_for_each_event_file(); 796 } 797 798 void trace_event_enable_tgid_record(bool enable) 799 { 800 struct trace_event_file *file; 801 struct trace_array *tr; 802 803 lockdep_assert_held(&event_mutex); 804 805 do_for_each_event_file(tr, file) { 806 if (!(file->flags & EVENT_FILE_FL_ENABLED)) 807 continue; 808 809 if (enable) { 810 tracing_start_tgid_record(); 811 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags); 812 } else { 813 tracing_stop_tgid_record(); 814 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, 815 &file->flags); 816 } 817 } while_for_each_event_file(); 818 } 819 820 static int __ftrace_event_enable_disable(struct trace_event_file *file, 821 int enable, int soft_disable) 822 { 823 struct trace_event_call *call = file->event_call; 824 struct trace_array *tr = file->tr; 825 bool soft_mode = atomic_read(&file->sm_ref) != 0; 826 int ret = 0; 827 int disable; 828 829 switch (enable) { 830 case 0: 831 /* 832 * When soft_disable is set and enable is cleared, the sm_ref 833 * reference counter is decremented. If it reaches 0, we want 834 * to clear the SOFT_DISABLED flag but leave the event in the 835 * state that it was. That is, if the event was enabled and 836 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED 837 * is set we do not want the event to be enabled before we 838 * clear the bit. 839 * 840 * When soft_disable is not set but the soft_mode is, 841 * we do nothing. Do not disable the tracepoint, otherwise 842 * "soft enable"s (clearing the SOFT_DISABLED bit) won't work. 843 */ 844 if (soft_disable) { 845 if (atomic_dec_return(&file->sm_ref) > 0) 846 break; 847 disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED; 848 soft_mode = false; 849 /* Disable use of trace_buffered_event */ 850 trace_buffered_event_disable(); 851 } else 852 disable = !soft_mode; 853 854 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) { 855 clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags); 856 if (file->flags & EVENT_FILE_FL_RECORDED_CMD) { 857 tracing_stop_cmdline_record(); 858 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags); 859 } 860 861 if (file->flags & EVENT_FILE_FL_RECORDED_TGID) { 862 tracing_stop_tgid_record(); 863 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags); 864 } 865 866 ret = call->class->reg(call, TRACE_REG_UNREGISTER, file); 867 868 WARN_ON_ONCE(ret); 869 } 870 /* If in soft mode, just set the SOFT_DISABLE_BIT, else clear it */ 871 if (soft_mode) 872 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags); 873 else 874 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags); 875 break; 876 case 1: 877 /* 878 * When soft_disable is set and enable is set, we want to 879 * register the tracepoint for the event, but leave the event 880 * as is. That means, if the event was already enabled, we do 881 * nothing. If the event is disabled, we set SOFT_DISABLED 882 * before enabling the event tracepoint, so it still seems 883 * to be disabled. 884 */ 885 if (!soft_disable) 886 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags); 887 else { 888 if (atomic_inc_return(&file->sm_ref) > 1) 889 break; 890 /* Enable use of trace_buffered_event */ 891 trace_buffered_event_enable(); 892 } 893 894 if (!(file->flags & EVENT_FILE_FL_ENABLED)) { 895 bool cmd = false, tgid = false; 896 897 /* Keep the event disabled, when going to soft mode. */ 898 if (soft_disable) 899 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags); 900 901 if (tr->trace_flags & TRACE_ITER(RECORD_CMD)) { 902 cmd = true; 903 tracing_start_cmdline_record(); 904 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags); 905 } 906 907 if (tr->trace_flags & TRACE_ITER(RECORD_TGID)) { 908 tgid = true; 909 tracing_start_tgid_record(); 910 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags); 911 } 912 913 ret = call->class->reg(call, TRACE_REG_REGISTER, file); 914 if (ret) { 915 if (cmd) 916 tracing_stop_cmdline_record(); 917 if (tgid) 918 tracing_stop_tgid_record(); 919 pr_info("event trace: Could not enable event " 920 "%s\n", trace_event_name(call)); 921 break; 922 } 923 set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags); 924 925 /* WAS_ENABLED gets set but never cleared. */ 926 set_bit(EVENT_FILE_FL_WAS_ENABLED_BIT, &file->flags); 927 } 928 break; 929 } 930 931 return ret; 932 } 933 934 int trace_event_enable_disable(struct trace_event_file *file, 935 int enable, int soft_disable) 936 { 937 return __ftrace_event_enable_disable(file, enable, soft_disable); 938 } 939 940 static int ftrace_event_enable_disable(struct trace_event_file *file, 941 int enable) 942 { 943 return __ftrace_event_enable_disable(file, enable, 0); 944 } 945 946 #ifdef CONFIG_MODULES 947 struct event_mod_load { 948 struct list_head list; 949 char *module; 950 char *match; 951 char *system; 952 char *event; 953 char *filter; 954 }; 955 956 static void free_event_mod(struct event_mod_load *event_mod) 957 { 958 list_del(&event_mod->list); 959 kfree(event_mod->module); 960 kfree(event_mod->match); 961 kfree(event_mod->system); 962 kfree(event_mod->event); 963 kfree(event_mod->filter); 964 kfree(event_mod); 965 } 966 967 static void clear_mod_events(struct trace_array *tr) 968 { 969 struct event_mod_load *event_mod, *n; 970 971 list_for_each_entry_safe(event_mod, n, &tr->mod_events, list) { 972 free_event_mod(event_mod); 973 } 974 } 975 976 static int remove_cache_mod(struct trace_array *tr, const char *mod, 977 const char *match, const char *system, const char *event) 978 { 979 struct event_mod_load *event_mod, *n; 980 int ret = -EINVAL; 981 982 list_for_each_entry_safe(event_mod, n, &tr->mod_events, list) { 983 if (strcmp(event_mod->module, mod) != 0) 984 continue; 985 986 if (match && (!event_mod->match || strcmp(event_mod->match, match) != 0)) 987 continue; 988 989 if (system && 990 (!event_mod->system || strcmp(event_mod->system, system) != 0)) 991 continue; 992 993 if (event && 994 (!event_mod->event || strcmp(event_mod->event, event) != 0)) 995 continue; 996 997 free_event_mod(event_mod); 998 ret = 0; 999 } 1000 1001 return ret; 1002 } 1003 1004 static int cache_mod(struct trace_array *tr, const char *mod, int set, 1005 const char *match, const char *system, const char *event, const char *filter) 1006 { 1007 struct event_mod_load *event_mod; 1008 1009 /* If the module exists, then this just failed to find an event */ 1010 if (module_exists(mod)) 1011 return -EINVAL; 1012 1013 /* See if this is to remove a cached filter */ 1014 if (!set) 1015 return remove_cache_mod(tr, mod, match, system, event); 1016 1017 event_mod = kzalloc_obj(*event_mod); 1018 if (!event_mod) 1019 return -ENOMEM; 1020 1021 INIT_LIST_HEAD(&event_mod->list); 1022 event_mod->module = kstrdup(mod, GFP_KERNEL); 1023 if (!event_mod->module) 1024 goto out_free; 1025 1026 if (match) { 1027 event_mod->match = kstrdup(match, GFP_KERNEL); 1028 if (!event_mod->match) 1029 goto out_free; 1030 } 1031 1032 if (system) { 1033 event_mod->system = kstrdup(system, GFP_KERNEL); 1034 if (!event_mod->system) 1035 goto out_free; 1036 } 1037 1038 if (event) { 1039 event_mod->event = kstrdup(event, GFP_KERNEL); 1040 if (!event_mod->event) 1041 goto out_free; 1042 } 1043 1044 if (filter) { 1045 event_mod->filter = kstrdup(filter, GFP_KERNEL); 1046 if (!event_mod->filter) 1047 goto out_free; 1048 } 1049 1050 list_add(&event_mod->list, &tr->mod_events); 1051 1052 return 0; 1053 1054 out_free: 1055 free_event_mod(event_mod); 1056 1057 return -ENOMEM; 1058 } 1059 #else /* CONFIG_MODULES */ 1060 static inline void clear_mod_events(struct trace_array *tr) { } 1061 static int cache_mod(struct trace_array *tr, const char *mod, int set, 1062 const char *match, const char *system, const char *event, const char *filter) 1063 { 1064 return -EINVAL; 1065 } 1066 #endif 1067 1068 static void ftrace_clear_events(struct trace_array *tr) 1069 { 1070 struct trace_event_file *file; 1071 1072 mutex_lock(&event_mutex); 1073 list_for_each_entry(file, &tr->events, list) { 1074 ftrace_event_enable_disable(file, 0); 1075 } 1076 clear_mod_events(tr); 1077 mutex_unlock(&event_mutex); 1078 } 1079 1080 static void 1081 event_filter_pid_sched_process_exit(void *data, struct task_struct *task) 1082 { 1083 struct trace_pid_list *pid_list; 1084 struct trace_array *tr = data; 1085 1086 guard(preempt)(); 1087 pid_list = rcu_dereference_raw(tr->filtered_pids); 1088 trace_filter_add_remove_task(pid_list, NULL, task); 1089 1090 pid_list = rcu_dereference_raw(tr->filtered_no_pids); 1091 trace_filter_add_remove_task(pid_list, NULL, task); 1092 } 1093 1094 static void 1095 event_filter_pid_sched_process_fork(void *data, 1096 struct task_struct *self, 1097 struct task_struct *task) 1098 { 1099 struct trace_pid_list *pid_list; 1100 struct trace_array *tr = data; 1101 1102 guard(preempt)(); 1103 pid_list = rcu_dereference_sched(tr->filtered_pids); 1104 trace_filter_add_remove_task(pid_list, self, task); 1105 1106 pid_list = rcu_dereference_sched(tr->filtered_no_pids); 1107 trace_filter_add_remove_task(pid_list, self, task); 1108 } 1109 1110 void trace_event_follow_fork(struct trace_array *tr, bool enable) 1111 { 1112 if (enable) { 1113 register_trace_prio_sched_process_fork(event_filter_pid_sched_process_fork, 1114 tr, INT_MIN); 1115 register_trace_prio_sched_process_free(event_filter_pid_sched_process_exit, 1116 tr, INT_MAX); 1117 } else { 1118 unregister_trace_sched_process_fork(event_filter_pid_sched_process_fork, 1119 tr); 1120 unregister_trace_sched_process_free(event_filter_pid_sched_process_exit, 1121 tr); 1122 } 1123 } 1124 1125 static void 1126 event_filter_pid_sched_switch_probe_pre(void *data, bool preempt, 1127 struct task_struct *prev, 1128 struct task_struct *next, 1129 unsigned int prev_state) 1130 { 1131 struct trace_array *tr = data; 1132 struct trace_pid_list *no_pid_list; 1133 struct trace_pid_list *pid_list; 1134 bool ret; 1135 1136 pid_list = rcu_dereference_sched(tr->filtered_pids); 1137 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids); 1138 1139 /* 1140 * Sched switch is funny, as we only want to ignore it 1141 * in the notrace case if both prev and next should be ignored. 1142 */ 1143 ret = trace_ignore_this_task(NULL, no_pid_list, prev) && 1144 trace_ignore_this_task(NULL, no_pid_list, next); 1145 1146 this_cpu_write(tr->array_buffer.data->ignore_pid, ret || 1147 (trace_ignore_this_task(pid_list, NULL, prev) && 1148 trace_ignore_this_task(pid_list, NULL, next))); 1149 } 1150 1151 static void 1152 event_filter_pid_sched_switch_probe_post(void *data, bool preempt, 1153 struct task_struct *prev, 1154 struct task_struct *next, 1155 unsigned int prev_state) 1156 { 1157 struct trace_array *tr = data; 1158 struct trace_pid_list *no_pid_list; 1159 struct trace_pid_list *pid_list; 1160 1161 pid_list = rcu_dereference_sched(tr->filtered_pids); 1162 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids); 1163 1164 this_cpu_write(tr->array_buffer.data->ignore_pid, 1165 trace_ignore_this_task(pid_list, no_pid_list, next)); 1166 } 1167 1168 static void 1169 event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task) 1170 { 1171 struct trace_array *tr = data; 1172 struct trace_pid_list *no_pid_list; 1173 struct trace_pid_list *pid_list; 1174 1175 /* Nothing to do if we are already tracing */ 1176 if (!this_cpu_read(tr->array_buffer.data->ignore_pid)) 1177 return; 1178 1179 pid_list = rcu_dereference_sched(tr->filtered_pids); 1180 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids); 1181 1182 this_cpu_write(tr->array_buffer.data->ignore_pid, 1183 trace_ignore_this_task(pid_list, no_pid_list, task)); 1184 } 1185 1186 static void 1187 event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task) 1188 { 1189 struct trace_array *tr = data; 1190 struct trace_pid_list *no_pid_list; 1191 struct trace_pid_list *pid_list; 1192 1193 /* Nothing to do if we are not tracing */ 1194 if (this_cpu_read(tr->array_buffer.data->ignore_pid)) 1195 return; 1196 1197 pid_list = rcu_dereference_sched(tr->filtered_pids); 1198 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids); 1199 1200 /* Set tracing if current is enabled */ 1201 this_cpu_write(tr->array_buffer.data->ignore_pid, 1202 trace_ignore_this_task(pid_list, no_pid_list, current)); 1203 } 1204 1205 static void unregister_pid_events(struct trace_array *tr) 1206 { 1207 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr); 1208 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr); 1209 1210 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr); 1211 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr); 1212 1213 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, tr); 1214 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, tr); 1215 1216 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre, tr); 1217 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post, tr); 1218 } 1219 1220 static void __ftrace_clear_event_pids(struct trace_array *tr, int type) 1221 { 1222 struct trace_pid_list *pid_list; 1223 struct trace_pid_list *no_pid_list; 1224 struct trace_event_file *file; 1225 int cpu; 1226 1227 pid_list = rcu_dereference_protected(tr->filtered_pids, 1228 lockdep_is_held(&event_mutex)); 1229 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids, 1230 lockdep_is_held(&event_mutex)); 1231 1232 /* Make sure there's something to do */ 1233 if (!pid_type_enabled(type, pid_list, no_pid_list)) 1234 return; 1235 1236 if (!still_need_pid_events(type, pid_list, no_pid_list)) { 1237 unregister_pid_events(tr); 1238 1239 list_for_each_entry(file, &tr->events, list) { 1240 clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags); 1241 } 1242 1243 for_each_possible_cpu(cpu) 1244 per_cpu_ptr(tr->array_buffer.data, cpu)->ignore_pid = false; 1245 } 1246 1247 if (type & TRACE_PIDS) 1248 rcu_assign_pointer(tr->filtered_pids, NULL); 1249 1250 if (type & TRACE_NO_PIDS) 1251 rcu_assign_pointer(tr->filtered_no_pids, NULL); 1252 1253 /* Wait till all users are no longer using pid filtering */ 1254 tracepoint_synchronize_unregister(); 1255 1256 if ((type & TRACE_PIDS) && pid_list) 1257 trace_pid_list_free(pid_list); 1258 1259 if ((type & TRACE_NO_PIDS) && no_pid_list) 1260 trace_pid_list_free(no_pid_list); 1261 } 1262 1263 static void ftrace_clear_event_pids(struct trace_array *tr, int type) 1264 { 1265 mutex_lock(&event_mutex); 1266 __ftrace_clear_event_pids(tr, type); 1267 mutex_unlock(&event_mutex); 1268 } 1269 1270 static void __put_system(struct event_subsystem *system) 1271 { 1272 struct event_filter *filter = system->filter; 1273 1274 WARN_ON_ONCE(system_refcount(system) == 0); 1275 if (system_refcount_dec(system)) 1276 return; 1277 1278 list_del(&system->list); 1279 1280 if (filter) { 1281 kfree(filter->filter_string); 1282 kfree(filter); 1283 } 1284 kfree_const(system->name); 1285 kfree(system); 1286 } 1287 1288 static void __get_system(struct event_subsystem *system) 1289 { 1290 WARN_ON_ONCE(system_refcount(system) == 0); 1291 system_refcount_inc(system); 1292 } 1293 1294 static void __get_system_dir(struct trace_subsystem_dir *dir) 1295 { 1296 WARN_ON_ONCE(dir->ref_count == 0); 1297 dir->ref_count++; 1298 __get_system(dir->subsystem); 1299 } 1300 1301 static void __put_system_dir(struct trace_subsystem_dir *dir) 1302 { 1303 WARN_ON_ONCE(dir->ref_count == 0); 1304 /* If the subsystem is about to be freed, the dir must be too */ 1305 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1); 1306 1307 __put_system(dir->subsystem); 1308 if (!--dir->ref_count) 1309 kfree(dir); 1310 } 1311 1312 static void put_system(struct trace_subsystem_dir *dir) 1313 { 1314 mutex_lock(&event_mutex); 1315 __put_system_dir(dir); 1316 mutex_unlock(&event_mutex); 1317 } 1318 1319 static void remove_subsystem(struct trace_subsystem_dir *dir) 1320 { 1321 if (!dir) 1322 return; 1323 1324 if (!--dir->nr_events) { 1325 eventfs_remove_dir(dir->ei); 1326 list_del(&dir->list); 1327 __put_system_dir(dir); 1328 } 1329 } 1330 1331 void event_file_get(struct trace_event_file *file) 1332 { 1333 refcount_inc(&file->ref); 1334 } 1335 1336 void event_file_put(struct trace_event_file *file) 1337 { 1338 if (WARN_ON_ONCE(!refcount_read(&file->ref))) { 1339 if (file->flags & EVENT_FILE_FL_FREED) 1340 kmem_cache_free(file_cachep, file); 1341 return; 1342 } 1343 1344 if (refcount_dec_and_test(&file->ref)) { 1345 /* Count should only go to zero when it is freed */ 1346 if (WARN_ON_ONCE(!(file->flags & EVENT_FILE_FL_FREED))) 1347 return; 1348 kmem_cache_free(file_cachep, file); 1349 } 1350 } 1351 1352 static void remove_event_file_dir(struct trace_event_file *file) 1353 { 1354 eventfs_remove_dir(file->ei); 1355 list_del(&file->list); 1356 remove_subsystem(file->system); 1357 free_event_filter(file->filter); 1358 file->flags |= EVENT_FILE_FL_FREED; 1359 event_file_put(file); 1360 1361 /* Wake up hist poll waiters to notice the EVENT_FILE_FL_FREED flag. */ 1362 hist_poll_wakeup(); 1363 } 1364 1365 /* 1366 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events. 1367 */ 1368 static int 1369 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match, 1370 const char *sub, const char *event, int set, 1371 const char *mod, char *filter) 1372 { 1373 struct trace_event_file *file; 1374 struct trace_event_call *call; 1375 char *module __free(kfree) = NULL; 1376 const char *name; 1377 int ret = -EINVAL; 1378 int eret = 0; 1379 1380 if (mod) { 1381 char *p; 1382 1383 module = kstrdup(mod, GFP_KERNEL); 1384 if (!module) 1385 return -ENOMEM; 1386 1387 /* Replace all '-' with '_' as that's what modules do */ 1388 for (p = strchr(module, '-'); p; p = strchr(p + 1, '-')) 1389 *p = '_'; 1390 } 1391 1392 list_for_each_entry(file, &tr->events, list) { 1393 1394 call = file->event_call; 1395 1396 /* If a module is specified, skip events that are not that module */ 1397 if (module && 1398 ((call->flags & TRACE_EVENT_FL_DYNAMIC) || 1399 !call->module || strcmp(module_name(call->module), module))) 1400 continue; 1401 1402 name = trace_event_name(call); 1403 1404 if (!name || !call->class || !call->class->reg) 1405 continue; 1406 1407 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) 1408 continue; 1409 1410 if (match && 1411 strcmp(match, name) != 0 && 1412 strcmp(match, call->class->system) != 0) 1413 continue; 1414 1415 if (sub && strcmp(sub, call->class->system) != 0) 1416 continue; 1417 1418 if (event && strcmp(event, name) != 0) 1419 continue; 1420 1421 if (filter) 1422 ret = apply_event_filter(file, filter); 1423 1424 if (!filter || !ret) 1425 ret = ftrace_event_enable_disable(file, set); 1426 1427 /* 1428 * Save the first error and return that. Some events 1429 * may still have been enabled, but let the user 1430 * know that something went wrong. 1431 */ 1432 if (ret && !eret) 1433 eret = ret; 1434 1435 ret = eret; 1436 } 1437 1438 /* 1439 * If this is a module setting and nothing was found, 1440 * check if the module was loaded. If it wasn't cache it. 1441 */ 1442 if (module && ret == -EINVAL && !eret) 1443 ret = cache_mod(tr, module, set, match, sub, event, filter); 1444 1445 return ret; 1446 } 1447 1448 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match, 1449 const char *sub, const char *event, int set, 1450 const char *mod, char *filter) 1451 { 1452 int ret; 1453 1454 if (trace_array_is_readonly(tr)) 1455 return -EACCES; 1456 1457 mutex_lock(&event_mutex); 1458 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set, mod, filter); 1459 mutex_unlock(&event_mutex); 1460 1461 return ret; 1462 } 1463 1464 VISIBLE_IF_KUNIT void 1465 ftrace_parse_event_string(char *buf, char **match, char **sub, char **event, char **mod, 1466 bool handle_filter, char **filter) 1467 { 1468 *event = NULL; 1469 *sub = NULL; 1470 *filter = NULL; 1471 1472 /* event filters can be appended with :filter:<filter> */ 1473 if (handle_filter) { 1474 *filter = strstr(buf, ":filter:"); 1475 if (*filter) { 1476 **filter = '\0'; 1477 *filter += 8; 1478 } 1479 } 1480 1481 /* Modules events can be appended with :mod:<module> */ 1482 *mod = strstr(buf, ":mod:"); 1483 if (*mod) { 1484 **mod = '\0'; 1485 /* move to the module name */ 1486 *mod += 5; 1487 } 1488 1489 /* 1490 * The buf format can be <subsystem>:<event-name> 1491 * *:<event-name> means any event by that name. 1492 * :<event-name> is the same. 1493 * 1494 * <subsystem>:* means all events in that subsystem 1495 * <subsystem>: means the same. 1496 * 1497 * <name> (no ':') means all events in a subsystem with 1498 * the name <name> or any event that matches <name> 1499 */ 1500 1501 *match = strsep(&buf, ":"); 1502 if (buf) { 1503 *sub = *match; 1504 *event = buf; 1505 *match = NULL; 1506 1507 if (!strlen(*sub) || strcmp(*sub, "*") == 0) 1508 *sub = NULL; 1509 if (!strlen(*event) || strcmp(*event, "*") == 0) 1510 *event = NULL; 1511 } else if (*mod) { 1512 /* Allow wildcard for no length or star */ 1513 if (!strlen(*match) || strcmp(*match, "*") == 0) 1514 *match = NULL; 1515 } 1516 } 1517 EXPORT_SYMBOL_IF_KUNIT(ftrace_parse_event_string); 1518 1519 int ftrace_set_clr_event(struct trace_array *tr, const char *arg_buf, int set, bool handle_filter) 1520 { 1521 char *event, *sub, *match, *mod, *filter; 1522 1523 if (!tr) 1524 return -ENOENT; 1525 1526 char *dupped_buf __free(kfree) = kstrdup(arg_buf, GFP_KERNEL); 1527 if (!dupped_buf) 1528 return -ENOMEM; 1529 1530 ftrace_parse_event_string(dupped_buf, &match, &sub, &event, &mod, handle_filter, &filter); 1531 1532 return __ftrace_set_clr_event(tr, match, sub, event, set, mod, filter); 1533 } 1534 1535 /** 1536 * trace_set_clr_event - enable or disable an event 1537 * @system: system name to match (NULL for any system) 1538 * @event: event name to match (NULL for all events, within system) 1539 * @set: 1 to enable, 0 to disable 1540 * 1541 * This is a way for other parts of the kernel to enable or disable 1542 * event recording. 1543 * 1544 * Returns 0 on success, -EINVAL if the parameters do not match any 1545 * registered events. 1546 */ 1547 int trace_set_clr_event(const char *system, const char *event, int set) 1548 { 1549 struct trace_array *tr = top_trace_array(); 1550 1551 if (!tr) 1552 return -ENODEV; 1553 1554 return __ftrace_set_clr_event(tr, NULL, system, event, set, NULL, NULL); 1555 } 1556 EXPORT_SYMBOL_GPL(trace_set_clr_event); 1557 1558 /** 1559 * trace_array_set_clr_event - enable or disable an event for a trace array. 1560 * @tr: concerned trace array. 1561 * @system: system name to match (NULL for any system) 1562 * @event: event name to match (NULL for all events, within system) 1563 * @enable: true to enable, false to disable 1564 * 1565 * This is a way for other parts of the kernel to enable or disable 1566 * event recording. 1567 * 1568 * Returns 0 on success, -EINVAL if the parameters do not match any 1569 * registered events. 1570 */ 1571 int trace_array_set_clr_event(struct trace_array *tr, const char *system, 1572 const char *event, bool enable) 1573 { 1574 int set; 1575 1576 if (!tr) 1577 return -ENOENT; 1578 1579 set = (enable == true) ? 1 : 0; 1580 return __ftrace_set_clr_event(tr, NULL, system, event, set, NULL, NULL); 1581 } 1582 EXPORT_SYMBOL_GPL(trace_array_set_clr_event); 1583 1584 /* 128 should be much more than enough */ 1585 #define EVENT_BUF_SIZE 127 1586 1587 static ssize_t 1588 ftrace_event_write(struct file *file, const char __user *ubuf, 1589 size_t cnt, loff_t *ppos) 1590 { 1591 struct trace_parser parser; 1592 struct seq_file *m = file->private_data; 1593 struct trace_array *tr = m->private; 1594 ssize_t read, ret; 1595 1596 if (!cnt) 1597 return 0; 1598 1599 ret = tracing_update_buffers(tr); 1600 if (ret < 0) 1601 return ret; 1602 1603 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1)) 1604 return -ENOMEM; 1605 1606 read = trace_get_user(&parser, ubuf, cnt, ppos); 1607 1608 if (read >= 0 && trace_parser_loaded((&parser))) { 1609 int set = 1; 1610 1611 if (*parser.buffer == '!') 1612 set = 0; 1613 1614 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set, false); 1615 if (ret) 1616 goto out_put; 1617 } 1618 1619 ret = read; 1620 1621 out_put: 1622 trace_parser_put(&parser); 1623 1624 return ret; 1625 } 1626 1627 static void * 1628 t_next(struct seq_file *m, void *v, loff_t *pos) 1629 { 1630 struct trace_event_file *file = v; 1631 struct trace_event_call *call; 1632 struct trace_array *tr = m->private; 1633 1634 (*pos)++; 1635 1636 list_for_each_entry_continue(file, &tr->events, list) { 1637 call = file->event_call; 1638 /* 1639 * The ftrace subsystem is for showing formats only. 1640 * They can not be enabled or disabled via the event files. 1641 */ 1642 if (call->class && call->class->reg && 1643 !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)) 1644 return file; 1645 } 1646 1647 return NULL; 1648 } 1649 1650 static void *t_start(struct seq_file *m, loff_t *pos) 1651 { 1652 struct trace_event_file *file; 1653 struct trace_array *tr = m->private; 1654 loff_t l; 1655 1656 mutex_lock(&event_mutex); 1657 1658 file = list_entry(&tr->events, struct trace_event_file, list); 1659 for (l = 0; l <= *pos; ) { 1660 file = t_next(m, file, &l); 1661 if (!file) 1662 break; 1663 } 1664 return file; 1665 } 1666 1667 enum set_event_iter_type { 1668 SET_EVENT_FILE, 1669 SET_EVENT_MOD, 1670 }; 1671 1672 struct set_event_iter { 1673 enum set_event_iter_type type; 1674 union { 1675 struct trace_event_file *file; 1676 struct event_mod_load *event_mod; 1677 }; 1678 }; 1679 1680 static void * 1681 s_next(struct seq_file *m, void *v, loff_t *pos) 1682 { 1683 struct set_event_iter *iter = v; 1684 struct trace_event_file *file; 1685 struct trace_array *tr = m->private; 1686 1687 (*pos)++; 1688 1689 if (iter->type == SET_EVENT_FILE) { 1690 file = iter->file; 1691 list_for_each_entry_continue(file, &tr->events, list) { 1692 if (file->flags & EVENT_FILE_FL_ENABLED) { 1693 iter->file = file; 1694 return iter; 1695 } 1696 } 1697 #ifdef CONFIG_MODULES 1698 iter->type = SET_EVENT_MOD; 1699 iter->event_mod = list_entry(&tr->mod_events, struct event_mod_load, list); 1700 #endif 1701 } 1702 1703 #ifdef CONFIG_MODULES 1704 list_for_each_entry_continue(iter->event_mod, &tr->mod_events, list) 1705 return iter; 1706 #endif 1707 1708 /* 1709 * The iter is allocated in s_start() and passed via the 'v' 1710 * parameter. To stop the iterator, NULL must be returned. But 1711 * the return value is what the 'v' parameter in s_stop() receives 1712 * and frees. Free iter here as it will no longer be used. 1713 */ 1714 kfree(iter); 1715 return NULL; 1716 } 1717 1718 static void *s_start(struct seq_file *m, loff_t *pos) 1719 { 1720 struct trace_array *tr = m->private; 1721 struct set_event_iter *iter; 1722 loff_t l; 1723 1724 iter = kzalloc_obj(*iter); 1725 mutex_lock(&event_mutex); 1726 if (!iter) 1727 return NULL; 1728 1729 iter->type = SET_EVENT_FILE; 1730 iter->file = list_entry(&tr->events, struct trace_event_file, list); 1731 1732 for (l = 0; l <= *pos; ) { 1733 iter = s_next(m, iter, &l); 1734 if (!iter) 1735 break; 1736 } 1737 return iter; 1738 } 1739 1740 static int t_show(struct seq_file *m, void *v) 1741 { 1742 struct trace_event_file *file = v; 1743 struct trace_event_call *call = file->event_call; 1744 1745 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) 1746 seq_printf(m, "%s:", call->class->system); 1747 seq_printf(m, "%s\n", trace_event_name(call)); 1748 1749 return 0; 1750 } 1751 1752 static void t_stop(struct seq_file *m, void *p) 1753 { 1754 mutex_unlock(&event_mutex); 1755 } 1756 1757 static int get_call_len(struct trace_event_call *call) 1758 { 1759 int len; 1760 1761 /* Get the length of "<system>:<event>" */ 1762 len = strlen(call->class->system) + 1; 1763 len += strlen(trace_event_name(call)); 1764 1765 /* Set the index to 32 bytes to separate event from data */ 1766 return len >= 32 ? 1 : 32 - len; 1767 } 1768 1769 /** 1770 * t_show_filters - seq_file callback to display active event filters 1771 * @m: The seq_file interface for formatted output 1772 * @v: The current trace_event_file being iterated 1773 * 1774 * Identifies and prints active filters for the current event file in the 1775 * iteration. If a filter is applied to the current event and, if so, 1776 * prints the system name, event name, and the filter string. 1777 */ 1778 static int t_show_filters(struct seq_file *m, void *v) 1779 { 1780 struct trace_event_file *file = v; 1781 struct trace_event_call *call = file->event_call; 1782 struct event_filter *filter; 1783 int len; 1784 1785 guard(rcu)(); 1786 filter = rcu_dereference(file->filter); 1787 if (!filter || !filter->filter_string) 1788 return 0; 1789 1790 len = get_call_len(call); 1791 1792 seq_printf(m, "%s:%s%*s%s\n", call->class->system, 1793 trace_event_name(call), len, "", filter->filter_string); 1794 1795 return 0; 1796 } 1797 1798 /** 1799 * t_show_triggers - seq_file callback to display active event triggers 1800 * @m: The seq_file interface for formatted output 1801 * @v: The current trace_event_file being iterated 1802 * 1803 * Iterates through the trigger list of the current event file and prints 1804 * each active trigger's configuration using its associated print 1805 * operation. 1806 */ 1807 static int t_show_triggers(struct seq_file *m, void *v) 1808 { 1809 struct trace_event_file *file = v; 1810 struct trace_event_call *call = file->event_call; 1811 struct event_trigger_data *data; 1812 int len; 1813 1814 /* 1815 * The event_mutex is held by t_start(), protecting the 1816 * file->triggers list traversal. 1817 */ 1818 if (list_empty(&file->triggers)) 1819 return 0; 1820 1821 len = get_call_len(call); 1822 1823 list_for_each_entry_rcu(data, &file->triggers, list) { 1824 seq_printf(m, "%s:%s%*s", call->class->system, 1825 trace_event_name(call), len, ""); 1826 1827 data->cmd_ops->print(m, data); 1828 } 1829 1830 return 0; 1831 } 1832 1833 #ifdef CONFIG_MODULES 1834 static int s_show(struct seq_file *m, void *v) 1835 { 1836 struct set_event_iter *iter = v; 1837 const char *system; 1838 const char *event; 1839 1840 if (iter->type == SET_EVENT_FILE) 1841 return t_show(m, iter->file); 1842 1843 /* When match is set, system and event are not */ 1844 if (iter->event_mod->match) { 1845 seq_printf(m, "%s:mod:%s\n", iter->event_mod->match, 1846 iter->event_mod->module); 1847 return 0; 1848 } 1849 1850 system = iter->event_mod->system ? : "*"; 1851 event = iter->event_mod->event ? : "*"; 1852 1853 seq_printf(m, "%s:%s:mod:%s\n", system, event, iter->event_mod->module); 1854 1855 return 0; 1856 } 1857 #else /* CONFIG_MODULES */ 1858 static int s_show(struct seq_file *m, void *v) 1859 { 1860 struct set_event_iter *iter = v; 1861 1862 return t_show(m, iter->file); 1863 } 1864 #endif 1865 1866 static void s_stop(struct seq_file *m, void *v) 1867 { 1868 kfree(v); 1869 t_stop(m, NULL); 1870 } 1871 1872 static void * 1873 __next(struct seq_file *m, void *v, loff_t *pos, int type) 1874 { 1875 struct trace_array *tr = m->private; 1876 struct trace_pid_list *pid_list; 1877 1878 if (type == TRACE_PIDS) 1879 pid_list = rcu_dereference_sched(tr->filtered_pids); 1880 else 1881 pid_list = rcu_dereference_sched(tr->filtered_no_pids); 1882 1883 return trace_pid_next(pid_list, v, pos); 1884 } 1885 1886 static void * 1887 p_next(struct seq_file *m, void *v, loff_t *pos) 1888 { 1889 return __next(m, v, pos, TRACE_PIDS); 1890 } 1891 1892 static void * 1893 np_next(struct seq_file *m, void *v, loff_t *pos) 1894 { 1895 return __next(m, v, pos, TRACE_NO_PIDS); 1896 } 1897 1898 static void *__start(struct seq_file *m, loff_t *pos, int type) 1899 __acquires(RCU) 1900 { 1901 struct trace_pid_list *pid_list; 1902 struct trace_array *tr = m->private; 1903 1904 /* 1905 * Grab the mutex, to keep calls to p_next() having the same 1906 * tr->filtered_pids as p_start() has. 1907 * If we just passed the tr->filtered_pids around, then RCU would 1908 * have been enough, but doing that makes things more complex. 1909 */ 1910 mutex_lock(&event_mutex); 1911 rcu_read_lock_sched(); 1912 1913 if (type == TRACE_PIDS) 1914 pid_list = rcu_dereference_sched(tr->filtered_pids); 1915 else 1916 pid_list = rcu_dereference_sched(tr->filtered_no_pids); 1917 1918 if (!pid_list) 1919 return NULL; 1920 1921 return trace_pid_start(pid_list, pos); 1922 } 1923 1924 static void *p_start(struct seq_file *m, loff_t *pos) 1925 __acquires(RCU) 1926 { 1927 return __start(m, pos, TRACE_PIDS); 1928 } 1929 1930 static void *np_start(struct seq_file *m, loff_t *pos) 1931 __acquires(RCU) 1932 { 1933 return __start(m, pos, TRACE_NO_PIDS); 1934 } 1935 1936 static void p_stop(struct seq_file *m, void *p) 1937 __releases(RCU) 1938 { 1939 rcu_read_unlock_sched(); 1940 mutex_unlock(&event_mutex); 1941 } 1942 1943 static ssize_t 1944 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt, 1945 loff_t *ppos) 1946 { 1947 struct trace_event_file *file; 1948 unsigned long flags; 1949 char buf[4] = "0"; 1950 1951 mutex_lock(&event_mutex); 1952 file = event_file_file(filp); 1953 if (likely(file)) 1954 flags = file->flags; 1955 mutex_unlock(&event_mutex); 1956 1957 if (!file) 1958 return -ENODEV; 1959 1960 if (flags & EVENT_FILE_FL_ENABLED && 1961 !(flags & EVENT_FILE_FL_SOFT_DISABLED)) 1962 strcpy(buf, "1"); 1963 1964 if (atomic_read(&file->sm_ref) != 0) 1965 strcat(buf, "*"); 1966 1967 strcat(buf, "\n"); 1968 1969 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf)); 1970 } 1971 1972 static ssize_t 1973 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt, 1974 loff_t *ppos) 1975 { 1976 struct trace_event_file *file; 1977 unsigned long val; 1978 int ret; 1979 1980 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 1981 if (ret) 1982 return ret; 1983 1984 guard(mutex)(&event_mutex); 1985 1986 switch (val) { 1987 case 0: 1988 case 1: 1989 file = event_file_file(filp); 1990 if (!file) 1991 return -ENODEV; 1992 ret = tracing_update_buffers(file->tr); 1993 if (ret < 0) 1994 return ret; 1995 ret = ftrace_event_enable_disable(file, val); 1996 if (ret < 0) 1997 return ret; 1998 break; 1999 2000 default: 2001 return -EINVAL; 2002 } 2003 2004 *ppos += cnt; 2005 2006 return cnt; 2007 } 2008 2009 /* 2010 * Returns: 2011 * 0 : no events exist? 2012 * 1 : all events are disabled 2013 * 2 : all events are enabled 2014 * 3 : some events are enabled and some are enabled 2015 */ 2016 int trace_events_enabled(struct trace_array *tr, const char *system) 2017 { 2018 struct trace_event_call *call; 2019 struct trace_event_file *file; 2020 int set = 0; 2021 2022 guard(mutex)(&event_mutex); 2023 2024 list_for_each_entry(file, &tr->events, list) { 2025 call = file->event_call; 2026 if ((call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) || 2027 !trace_event_name(call) || !call->class || !call->class->reg) 2028 continue; 2029 2030 if (system && strcmp(call->class->system, system) != 0) 2031 continue; 2032 2033 /* 2034 * We need to find out if all the events are set 2035 * or if all events or cleared, or if we have 2036 * a mixture. 2037 */ 2038 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED)); 2039 2040 /* 2041 * If we have a mixture, no need to look further. 2042 */ 2043 if (set == 3) 2044 break; 2045 } 2046 2047 return set; 2048 } 2049 2050 static ssize_t 2051 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt, 2052 loff_t *ppos) 2053 { 2054 const char set_to_char[4] = { '?', '0', '1', 'X' }; 2055 struct trace_subsystem_dir *dir = filp->private_data; 2056 struct event_subsystem *system = dir->subsystem; 2057 struct trace_array *tr = dir->tr; 2058 char buf[2]; 2059 int set; 2060 int ret; 2061 2062 set = trace_events_enabled(tr, system ? system->name : NULL); 2063 2064 buf[0] = set_to_char[set]; 2065 buf[1] = '\n'; 2066 2067 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2); 2068 2069 return ret; 2070 } 2071 2072 static ssize_t 2073 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt, 2074 loff_t *ppos) 2075 { 2076 struct trace_subsystem_dir *dir = filp->private_data; 2077 struct event_subsystem *system = dir->subsystem; 2078 const char *name = NULL; 2079 unsigned long val; 2080 ssize_t ret; 2081 2082 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 2083 if (ret) 2084 return ret; 2085 2086 ret = tracing_update_buffers(dir->tr); 2087 if (ret < 0) 2088 return ret; 2089 2090 if (val != 0 && val != 1) 2091 return -EINVAL; 2092 2093 /* 2094 * Opening of "enable" adds a ref count to system, 2095 * so the name is safe to use. 2096 */ 2097 if (system) 2098 name = system->name; 2099 2100 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val, NULL, NULL); 2101 if (ret) 2102 goto out; 2103 2104 ret = cnt; 2105 2106 out: 2107 *ppos += cnt; 2108 2109 return ret; 2110 } 2111 2112 enum { 2113 FORMAT_HEADER = 1, 2114 FORMAT_FIELD_SEPERATOR = 2, 2115 FORMAT_PRINTFMT = 3, 2116 }; 2117 2118 static void *f_next(struct seq_file *m, void *v, loff_t *pos) 2119 { 2120 struct trace_event_file *file = event_file_data(m->private); 2121 struct trace_event_call *call = file->event_call; 2122 struct list_head *common_head = &ftrace_common_fields; 2123 struct list_head *head = trace_get_fields(call); 2124 struct list_head *node = v; 2125 2126 (*pos)++; 2127 2128 switch ((unsigned long)v) { 2129 case FORMAT_HEADER: 2130 node = common_head; 2131 break; 2132 2133 case FORMAT_FIELD_SEPERATOR: 2134 node = head; 2135 break; 2136 2137 case FORMAT_PRINTFMT: 2138 /* all done */ 2139 return NULL; 2140 } 2141 2142 node = node->prev; 2143 if (node == common_head) 2144 return (void *)FORMAT_FIELD_SEPERATOR; 2145 else if (node == head) 2146 return (void *)FORMAT_PRINTFMT; 2147 else 2148 return node; 2149 } 2150 2151 static int f_show(struct seq_file *m, void *v) 2152 { 2153 struct trace_event_file *file = event_file_data(m->private); 2154 struct trace_event_call *call = file->event_call; 2155 struct ftrace_event_field *field; 2156 const char *array_descriptor; 2157 2158 switch ((unsigned long)v) { 2159 case FORMAT_HEADER: 2160 seq_printf(m, "name: %s\n", trace_event_name(call)); 2161 seq_printf(m, "ID: %d\n", call->event.type); 2162 seq_puts(m, "format:\n"); 2163 return 0; 2164 2165 case FORMAT_FIELD_SEPERATOR: 2166 seq_putc(m, '\n'); 2167 return 0; 2168 2169 case FORMAT_PRINTFMT: 2170 seq_printf(m, "\nprint fmt: %s\n", 2171 call->print_fmt); 2172 return 0; 2173 } 2174 2175 field = list_entry(v, struct ftrace_event_field, link); 2176 /* 2177 * Smartly shows the array type(except dynamic array). 2178 * Normal: 2179 * field:TYPE VAR 2180 * If TYPE := TYPE[LEN], it is shown: 2181 * field:TYPE VAR[LEN] 2182 */ 2183 array_descriptor = strchr(field->type, '['); 2184 2185 if (str_has_prefix(field->type, "__data_loc")) 2186 array_descriptor = NULL; 2187 2188 if (!array_descriptor) 2189 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n", 2190 field->type, field->name, field->offset, 2191 field->size, !!field->is_signed); 2192 else if (field->len) 2193 seq_printf(m, "\tfield:%.*s %s[%d];\toffset:%u;\tsize:%u;\tsigned:%d;\n", 2194 (int)(array_descriptor - field->type), 2195 field->type, field->name, 2196 field->len, field->offset, 2197 field->size, !!field->is_signed); 2198 else 2199 seq_printf(m, "\tfield:%.*s %s[];\toffset:%u;\tsize:%u;\tsigned:%d;\n", 2200 (int)(array_descriptor - field->type), 2201 field->type, field->name, 2202 field->offset, field->size, !!field->is_signed); 2203 2204 return 0; 2205 } 2206 2207 static void *f_start(struct seq_file *m, loff_t *pos) 2208 { 2209 struct trace_event_file *file; 2210 void *p = (void *)FORMAT_HEADER; 2211 loff_t l = 0; 2212 2213 /* ->stop() is called even if ->start() fails */ 2214 mutex_lock(&event_mutex); 2215 file = event_file_file(m->private); 2216 if (!file) 2217 return ERR_PTR(-ENODEV); 2218 2219 while (l < *pos && p) 2220 p = f_next(m, p, &l); 2221 2222 return p; 2223 } 2224 2225 static void f_stop(struct seq_file *m, void *p) 2226 { 2227 mutex_unlock(&event_mutex); 2228 } 2229 2230 static const struct seq_operations trace_format_seq_ops = { 2231 .start = f_start, 2232 .next = f_next, 2233 .stop = f_stop, 2234 .show = f_show, 2235 }; 2236 2237 static int trace_format_open(struct inode *inode, struct file *file) 2238 { 2239 struct seq_file *m; 2240 int ret; 2241 2242 /* Do we want to hide event format files on tracefs lockdown? */ 2243 2244 ret = seq_open(file, &trace_format_seq_ops); 2245 if (ret < 0) 2246 return ret; 2247 2248 m = file->private_data; 2249 m->private = file; 2250 2251 return 0; 2252 } 2253 2254 #ifdef CONFIG_PERF_EVENTS 2255 static ssize_t 2256 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos) 2257 { 2258 /* id is directly in i_private and available for inode's lifetime. */ 2259 int id = (long)file_inode(filp)->i_private; 2260 char buf[32]; 2261 int len; 2262 2263 WARN_ON(!id); 2264 2265 len = sprintf(buf, "%d\n", id); 2266 2267 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len); 2268 } 2269 #endif 2270 2271 #ifdef CONFIG_BPF_EVENTS 2272 static ssize_t 2273 event_btf_ids_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos) 2274 { 2275 struct trace_event_file *file; 2276 struct trace_event_call *call; 2277 const struct btf_type *t; 2278 struct module *mod = NULL; 2279 u32 raw_id = 0, tp_id = 0, obj_id = 0; 2280 const u32 *ids; 2281 struct btf *btf; 2282 char buf[128]; 2283 int len; 2284 2285 /* Module unload could free call->class and ids[] mid-read. */ 2286 scoped_guard(mutex, &event_mutex) { 2287 file = event_file_file(filp); 2288 if (!file) 2289 return -ENODEV; 2290 2291 call = file->event_call; 2292 ids = call->class->btf_ids; 2293 if (!ids) 2294 return -ENOENT; 2295 if (!(call->flags & TRACE_EVENT_FL_DYNAMIC)) 2296 mod = (struct module *)call->module; 2297 2298 btf = btf_get_module_btf(mod); 2299 if (IS_ERR_OR_NULL(btf)) 2300 return -ENOENT; 2301 2302 /* Module-local ids in ids[] need base+local relocation. */ 2303 tp_id = btf_relocate_id(btf, ids[1]); 2304 2305 /* 2306 * Without FL_TRACEPOINT the dispatcher is shared (e.g. all 2307 * per-syscall events fan out from __bpf_trace_sys_enter), so 2308 * raw_btf_id has no per-event attach point — report 0. 2309 */ 2310 if (call->flags & TRACE_EVENT_FL_TRACEPOINT) { 2311 t = btf_type_by_id(btf, btf_relocate_id(btf, ids[0])); 2312 raw_id = t ? t->type : 0; 2313 } 2314 obj_id = btf_obj_id(btf); 2315 btf_put(btf); 2316 } 2317 2318 len = scnprintf(buf, sizeof(buf), 2319 "btf_obj_id: %u\nraw_btf_id: %u\ntp_btf_id: %u\n", 2320 obj_id, raw_id, tp_id); 2321 2322 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len); 2323 } 2324 #endif 2325 2326 static ssize_t 2327 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt, 2328 loff_t *ppos) 2329 { 2330 struct trace_event_file *file; 2331 struct trace_seq *s; 2332 int r = -ENODEV; 2333 2334 if (*ppos) 2335 return 0; 2336 2337 s = kmalloc_obj(*s); 2338 2339 if (!s) 2340 return -ENOMEM; 2341 2342 trace_seq_init(s); 2343 2344 mutex_lock(&event_mutex); 2345 file = event_file_file(filp); 2346 if (file) 2347 print_event_filter(file, s); 2348 mutex_unlock(&event_mutex); 2349 2350 if (file) 2351 r = simple_read_from_buffer(ubuf, cnt, ppos, 2352 s->buffer, trace_seq_used(s)); 2353 2354 kfree(s); 2355 2356 return r; 2357 } 2358 2359 static ssize_t 2360 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt, 2361 loff_t *ppos) 2362 { 2363 struct trace_event_file *file; 2364 char *buf; 2365 int err = -ENODEV; 2366 2367 if (cnt >= PAGE_SIZE) 2368 return -EINVAL; 2369 2370 buf = memdup_user_nul(ubuf, cnt); 2371 if (IS_ERR(buf)) 2372 return PTR_ERR(buf); 2373 2374 mutex_lock(&event_mutex); 2375 file = event_file_file(filp); 2376 if (file) 2377 err = apply_event_filter(file, buf); 2378 mutex_unlock(&event_mutex); 2379 2380 kfree(buf); 2381 if (err < 0) 2382 return err; 2383 2384 *ppos += cnt; 2385 2386 return cnt; 2387 } 2388 2389 static LIST_HEAD(event_subsystems); 2390 2391 static int subsystem_open(struct inode *inode, struct file *filp) 2392 { 2393 struct trace_subsystem_dir *dir = NULL, *iter_dir; 2394 struct trace_array *tr = NULL, *iter_tr; 2395 struct event_subsystem *system = NULL; 2396 int ret; 2397 2398 if (unlikely(tracing_disabled)) 2399 return -ENODEV; 2400 2401 /* Make sure the system still exists */ 2402 mutex_lock(&event_mutex); 2403 mutex_lock(&trace_types_lock); 2404 list_for_each_entry(iter_tr, &ftrace_trace_arrays, list) { 2405 list_for_each_entry(iter_dir, &iter_tr->systems, list) { 2406 if (iter_dir == inode->i_private) { 2407 /* Don't open systems with no events */ 2408 tr = iter_tr; 2409 dir = iter_dir; 2410 if (dir->nr_events) { 2411 __get_system_dir(dir); 2412 system = dir->subsystem; 2413 } 2414 goto exit_loop; 2415 } 2416 } 2417 } 2418 exit_loop: 2419 mutex_unlock(&trace_types_lock); 2420 mutex_unlock(&event_mutex); 2421 2422 if (!system) 2423 return -ENODEV; 2424 2425 /* Still need to increment the ref count of the system */ 2426 if (trace_array_get(tr) < 0) { 2427 put_system(dir); 2428 return -ENODEV; 2429 } 2430 2431 ret = tracing_open_generic(inode, filp); 2432 if (ret < 0) { 2433 trace_array_put(tr); 2434 put_system(dir); 2435 } 2436 2437 return ret; 2438 } 2439 2440 static int system_tr_open(struct inode *inode, struct file *filp) 2441 { 2442 struct trace_subsystem_dir *dir; 2443 struct trace_array *tr = inode->i_private; 2444 int ret; 2445 2446 /* Make a temporary dir that has no system but points to tr */ 2447 dir = kzalloc_obj(*dir); 2448 if (!dir) 2449 return -ENOMEM; 2450 2451 ret = tracing_open_generic_tr(inode, filp); 2452 if (ret < 0) { 2453 kfree(dir); 2454 return ret; 2455 } 2456 dir->tr = tr; 2457 filp->private_data = dir; 2458 2459 return 0; 2460 } 2461 2462 static int subsystem_release(struct inode *inode, struct file *file) 2463 { 2464 struct trace_subsystem_dir *dir = file->private_data; 2465 2466 trace_array_put(dir->tr); 2467 2468 /* 2469 * If dir->subsystem is NULL, then this is a temporary 2470 * descriptor that was made for a trace_array to enable 2471 * all subsystems. 2472 */ 2473 if (dir->subsystem) 2474 put_system(dir); 2475 else 2476 kfree(dir); 2477 2478 return 0; 2479 } 2480 2481 static ssize_t 2482 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt, 2483 loff_t *ppos) 2484 { 2485 struct trace_subsystem_dir *dir = filp->private_data; 2486 struct event_subsystem *system = dir->subsystem; 2487 struct trace_seq *s; 2488 int r; 2489 2490 if (*ppos) 2491 return 0; 2492 2493 s = kmalloc_obj(*s); 2494 if (!s) 2495 return -ENOMEM; 2496 2497 trace_seq_init(s); 2498 2499 print_subsystem_event_filter(system, s); 2500 r = simple_read_from_buffer(ubuf, cnt, ppos, 2501 s->buffer, trace_seq_used(s)); 2502 2503 kfree(s); 2504 2505 return r; 2506 } 2507 2508 static ssize_t 2509 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt, 2510 loff_t *ppos) 2511 { 2512 struct trace_subsystem_dir *dir = filp->private_data; 2513 char *buf; 2514 int err; 2515 2516 if (cnt >= PAGE_SIZE) 2517 return -EINVAL; 2518 2519 buf = memdup_user_nul(ubuf, cnt); 2520 if (IS_ERR(buf)) 2521 return PTR_ERR(buf); 2522 2523 err = apply_subsystem_event_filter(dir, buf); 2524 kfree(buf); 2525 if (err < 0) 2526 return err; 2527 2528 *ppos += cnt; 2529 2530 return cnt; 2531 } 2532 2533 static ssize_t 2534 show_header_page_file(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos) 2535 { 2536 struct trace_array *tr = filp->private_data; 2537 struct trace_seq *s; 2538 int r; 2539 2540 if (*ppos) 2541 return 0; 2542 2543 s = kmalloc_obj(*s); 2544 if (!s) 2545 return -ENOMEM; 2546 2547 trace_seq_init(s); 2548 2549 ring_buffer_print_page_header(tr->array_buffer.buffer, s); 2550 r = simple_read_from_buffer(ubuf, cnt, ppos, 2551 s->buffer, trace_seq_used(s)); 2552 2553 kfree(s); 2554 2555 return r; 2556 } 2557 2558 static ssize_t 2559 show_header_event_file(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos) 2560 { 2561 struct trace_seq *s; 2562 int r; 2563 2564 if (*ppos) 2565 return 0; 2566 2567 s = kmalloc_obj(*s); 2568 if (!s) 2569 return -ENOMEM; 2570 2571 trace_seq_init(s); 2572 2573 ring_buffer_print_entry_header(s); 2574 r = simple_read_from_buffer(ubuf, cnt, ppos, 2575 s->buffer, trace_seq_used(s)); 2576 2577 kfree(s); 2578 2579 return r; 2580 } 2581 2582 static void ignore_task_cpu(void *data) 2583 { 2584 struct trace_array *tr = data; 2585 struct trace_pid_list *pid_list; 2586 struct trace_pid_list *no_pid_list; 2587 2588 /* 2589 * This function is called by on_each_cpu() while the 2590 * event_mutex is held. 2591 */ 2592 pid_list = rcu_dereference_protected(tr->filtered_pids, 2593 mutex_is_locked(&event_mutex)); 2594 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids, 2595 mutex_is_locked(&event_mutex)); 2596 2597 this_cpu_write(tr->array_buffer.data->ignore_pid, 2598 trace_ignore_this_task(pid_list, no_pid_list, current)); 2599 } 2600 2601 static void register_pid_events(struct trace_array *tr) 2602 { 2603 /* 2604 * Register a probe that is called before all other probes 2605 * to set ignore_pid if next or prev do not match. 2606 * Register a probe this is called after all other probes 2607 * to only keep ignore_pid set if next pid matches. 2608 */ 2609 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre, 2610 tr, INT_MAX); 2611 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post, 2612 tr, 0); 2613 2614 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, 2615 tr, INT_MAX); 2616 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, 2617 tr, 0); 2618 2619 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, 2620 tr, INT_MAX); 2621 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, 2622 tr, 0); 2623 2624 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre, 2625 tr, INT_MAX); 2626 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post, 2627 tr, 0); 2628 } 2629 2630 static ssize_t 2631 event_pid_write(struct file *filp, const char __user *ubuf, 2632 size_t cnt, loff_t *ppos, int type) 2633 { 2634 struct seq_file *m = filp->private_data; 2635 struct trace_array *tr = m->private; 2636 struct trace_pid_list *filtered_pids = NULL; 2637 struct trace_pid_list *other_pids = NULL; 2638 struct trace_pid_list *pid_list; 2639 struct trace_event_file *file; 2640 ssize_t ret; 2641 2642 if (!cnt) 2643 return 0; 2644 2645 ret = tracing_update_buffers(tr); 2646 if (ret < 0) 2647 return ret; 2648 2649 guard(mutex)(&event_mutex); 2650 2651 if (type == TRACE_PIDS) { 2652 filtered_pids = rcu_dereference_protected(tr->filtered_pids, 2653 lockdep_is_held(&event_mutex)); 2654 other_pids = rcu_dereference_protected(tr->filtered_no_pids, 2655 lockdep_is_held(&event_mutex)); 2656 } else { 2657 filtered_pids = rcu_dereference_protected(tr->filtered_no_pids, 2658 lockdep_is_held(&event_mutex)); 2659 other_pids = rcu_dereference_protected(tr->filtered_pids, 2660 lockdep_is_held(&event_mutex)); 2661 } 2662 2663 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt); 2664 if (ret < 0) 2665 return ret; 2666 2667 if (type == TRACE_PIDS) 2668 rcu_assign_pointer(tr->filtered_pids, pid_list); 2669 else 2670 rcu_assign_pointer(tr->filtered_no_pids, pid_list); 2671 2672 list_for_each_entry(file, &tr->events, list) { 2673 set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags); 2674 } 2675 2676 if (filtered_pids) { 2677 tracepoint_synchronize_unregister(); 2678 trace_pid_list_free(filtered_pids); 2679 } else if (pid_list && !other_pids) { 2680 register_pid_events(tr); 2681 } 2682 2683 /* 2684 * Ignoring of pids is done at task switch. But we have to 2685 * check for those tasks that are currently running. 2686 * Always do this in case a pid was appended or removed. 2687 */ 2688 on_each_cpu(ignore_task_cpu, tr, 1); 2689 2690 *ppos += ret; 2691 2692 return ret; 2693 } 2694 2695 static ssize_t 2696 ftrace_event_pid_write(struct file *filp, const char __user *ubuf, 2697 size_t cnt, loff_t *ppos) 2698 { 2699 return event_pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS); 2700 } 2701 2702 static ssize_t 2703 ftrace_event_npid_write(struct file *filp, const char __user *ubuf, 2704 size_t cnt, loff_t *ppos) 2705 { 2706 return event_pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS); 2707 } 2708 2709 static int ftrace_event_avail_open(struct inode *inode, struct file *file); 2710 static int ftrace_event_set_open(struct inode *inode, struct file *file); 2711 static int ftrace_event_show_filters_open(struct inode *inode, struct file *file); 2712 static int ftrace_event_show_triggers_open(struct inode *inode, struct file *file); 2713 static int ftrace_event_set_pid_open(struct inode *inode, struct file *file); 2714 static int ftrace_event_set_npid_open(struct inode *inode, struct file *file); 2715 static int ftrace_event_release(struct inode *inode, struct file *file); 2716 2717 static const struct seq_operations show_event_seq_ops = { 2718 .start = t_start, 2719 .next = t_next, 2720 .show = t_show, 2721 .stop = t_stop, 2722 }; 2723 2724 static const struct seq_operations show_set_event_seq_ops = { 2725 .start = s_start, 2726 .next = s_next, 2727 .show = s_show, 2728 .stop = s_stop, 2729 }; 2730 2731 static const struct seq_operations show_show_event_filters_seq_ops = { 2732 .start = t_start, 2733 .next = t_next, 2734 .show = t_show_filters, 2735 .stop = t_stop, 2736 }; 2737 2738 static const struct seq_operations show_show_event_triggers_seq_ops = { 2739 .start = t_start, 2740 .next = t_next, 2741 .show = t_show_triggers, 2742 .stop = t_stop, 2743 }; 2744 2745 static const struct seq_operations show_set_pid_seq_ops = { 2746 .start = p_start, 2747 .next = p_next, 2748 .show = trace_pid_show, 2749 .stop = p_stop, 2750 }; 2751 2752 static const struct seq_operations show_set_no_pid_seq_ops = { 2753 .start = np_start, 2754 .next = np_next, 2755 .show = trace_pid_show, 2756 .stop = p_stop, 2757 }; 2758 2759 static const struct file_operations ftrace_avail_fops = { 2760 .open = ftrace_event_avail_open, 2761 .read = seq_read, 2762 .llseek = seq_lseek, 2763 .release = seq_release, 2764 }; 2765 2766 static const struct file_operations ftrace_set_event_fops = { 2767 .open = ftrace_event_set_open, 2768 .read = seq_read, 2769 .write = ftrace_event_write, 2770 .llseek = seq_lseek, 2771 .release = ftrace_event_release, 2772 }; 2773 2774 static const struct file_operations ftrace_show_event_filters_fops = { 2775 .open = ftrace_event_show_filters_open, 2776 .read = seq_read, 2777 .llseek = seq_lseek, 2778 .release = ftrace_event_release, 2779 }; 2780 2781 static const struct file_operations ftrace_show_event_triggers_fops = { 2782 .open = ftrace_event_show_triggers_open, 2783 .read = seq_read, 2784 .llseek = seq_lseek, 2785 .release = ftrace_event_release, 2786 }; 2787 2788 static const struct file_operations ftrace_set_event_pid_fops = { 2789 .open = ftrace_event_set_pid_open, 2790 .read = seq_read, 2791 .write = ftrace_event_pid_write, 2792 .llseek = seq_lseek, 2793 .release = ftrace_event_release, 2794 }; 2795 2796 static const struct file_operations ftrace_set_event_notrace_pid_fops = { 2797 .open = ftrace_event_set_npid_open, 2798 .read = seq_read, 2799 .write = ftrace_event_npid_write, 2800 .llseek = seq_lseek, 2801 .release = ftrace_event_release, 2802 }; 2803 2804 static const struct file_operations ftrace_enable_fops = { 2805 .open = tracing_open_file_tr, 2806 .read = event_enable_read, 2807 .write = event_enable_write, 2808 .release = tracing_release_file_tr, 2809 .llseek = default_llseek, 2810 }; 2811 2812 static const struct file_operations ftrace_event_format_fops = { 2813 .open = trace_format_open, 2814 .read = seq_read, 2815 .llseek = seq_lseek, 2816 .release = seq_release, 2817 }; 2818 2819 #ifdef CONFIG_PERF_EVENTS 2820 static const struct file_operations ftrace_event_id_fops = { 2821 .read = event_id_read, 2822 .llseek = default_llseek, 2823 }; 2824 #endif 2825 2826 #ifdef CONFIG_BPF_EVENTS 2827 static const struct file_operations ftrace_event_btf_ids_fops = { 2828 .read = event_btf_ids_read, 2829 .llseek = default_llseek, 2830 }; 2831 #endif 2832 2833 static const struct file_operations ftrace_event_filter_fops = { 2834 .open = tracing_open_file_tr, 2835 .read = event_filter_read, 2836 .write = event_filter_write, 2837 .release = tracing_release_file_tr, 2838 .llseek = default_llseek, 2839 }; 2840 2841 static const struct file_operations ftrace_subsystem_filter_fops = { 2842 .open = subsystem_open, 2843 .read = subsystem_filter_read, 2844 .write = subsystem_filter_write, 2845 .llseek = default_llseek, 2846 .release = subsystem_release, 2847 }; 2848 2849 static const struct file_operations ftrace_system_enable_fops = { 2850 .open = subsystem_open, 2851 .read = system_enable_read, 2852 .write = system_enable_write, 2853 .llseek = default_llseek, 2854 .release = subsystem_release, 2855 }; 2856 2857 static const struct file_operations ftrace_tr_enable_fops = { 2858 .open = system_tr_open, 2859 .read = system_enable_read, 2860 .write = system_enable_write, 2861 .llseek = default_llseek, 2862 .release = subsystem_release, 2863 }; 2864 2865 static const struct file_operations ftrace_show_header_page_fops = { 2866 .open = tracing_open_generic_tr, 2867 .read = show_header_page_file, 2868 .llseek = default_llseek, 2869 .release = tracing_release_generic_tr, 2870 }; 2871 2872 static const struct file_operations ftrace_show_header_event_fops = { 2873 .open = tracing_open_generic_tr, 2874 .read = show_header_event_file, 2875 .llseek = default_llseek, 2876 .release = tracing_release_generic_tr, 2877 }; 2878 2879 static int 2880 ftrace_event_open(struct inode *inode, struct file *file, 2881 const struct seq_operations *seq_ops) 2882 { 2883 struct seq_file *m; 2884 int ret; 2885 2886 ret = security_locked_down(LOCKDOWN_TRACEFS); 2887 if (ret) 2888 return ret; 2889 2890 ret = seq_open(file, seq_ops); 2891 if (ret < 0) 2892 return ret; 2893 m = file->private_data; 2894 /* copy tr over to seq ops */ 2895 m->private = inode->i_private; 2896 2897 return ret; 2898 } 2899 2900 static int ftrace_event_release(struct inode *inode, struct file *file) 2901 { 2902 struct trace_array *tr = inode->i_private; 2903 2904 trace_array_put(tr); 2905 2906 return seq_release(inode, file); 2907 } 2908 2909 static int 2910 ftrace_event_avail_open(struct inode *inode, struct file *file) 2911 { 2912 const struct seq_operations *seq_ops = &show_event_seq_ops; 2913 2914 /* Checks for tracefs lockdown */ 2915 return ftrace_event_open(inode, file, seq_ops); 2916 } 2917 2918 static int 2919 ftrace_event_set_open(struct inode *inode, struct file *file) 2920 { 2921 const struct seq_operations *seq_ops = &show_set_event_seq_ops; 2922 struct trace_array *tr = inode->i_private; 2923 int ret; 2924 2925 ret = tracing_check_open_get_tr(tr); 2926 if (ret) 2927 return ret; 2928 2929 if ((file->f_mode & FMODE_WRITE) && 2930 (file->f_flags & O_TRUNC)) 2931 ftrace_clear_events(tr); 2932 2933 ret = ftrace_event_open(inode, file, seq_ops); 2934 if (ret < 0) 2935 trace_array_put(tr); 2936 return ret; 2937 } 2938 2939 /** 2940 * ftrace_event_show_filters_open - open interface for set_event_filters 2941 * @inode: The inode of the file 2942 * @file: The file being opened 2943 * 2944 * Connects the set_event_filters file to the sequence operations 2945 * required to iterate over and display active event filters. 2946 */ 2947 static int 2948 ftrace_event_show_filters_open(struct inode *inode, struct file *file) 2949 { 2950 struct trace_array *tr = inode->i_private; 2951 int ret; 2952 2953 ret = tracing_check_open_get_tr(tr); 2954 if (ret) 2955 return ret; 2956 2957 ret = ftrace_event_open(inode, file, &show_show_event_filters_seq_ops); 2958 if (ret < 0) 2959 trace_array_put(tr); 2960 return ret; 2961 } 2962 2963 /** 2964 * ftrace_event_show_triggers_open - open interface for show_event_triggers 2965 * @inode: The inode of the file 2966 * @file: The file being opened 2967 * 2968 * Connects the show_event_triggers file to the sequence operations 2969 * required to iterate over and display active event triggers. 2970 */ 2971 static int 2972 ftrace_event_show_triggers_open(struct inode *inode, struct file *file) 2973 { 2974 struct trace_array *tr = inode->i_private; 2975 int ret; 2976 2977 ret = tracing_check_open_get_tr(tr); 2978 if (ret) 2979 return ret; 2980 2981 ret = ftrace_event_open(inode, file, &show_show_event_triggers_seq_ops); 2982 if (ret < 0) 2983 trace_array_put(tr); 2984 return ret; 2985 } 2986 2987 static int 2988 ftrace_event_set_pid_open(struct inode *inode, struct file *file) 2989 { 2990 const struct seq_operations *seq_ops = &show_set_pid_seq_ops; 2991 struct trace_array *tr = inode->i_private; 2992 int ret; 2993 2994 ret = tracing_check_open_get_tr(tr); 2995 if (ret) 2996 return ret; 2997 2998 if ((file->f_mode & FMODE_WRITE) && 2999 (file->f_flags & O_TRUNC)) 3000 ftrace_clear_event_pids(tr, TRACE_PIDS); 3001 3002 ret = ftrace_event_open(inode, file, seq_ops); 3003 if (ret < 0) 3004 trace_array_put(tr); 3005 return ret; 3006 } 3007 3008 static int 3009 ftrace_event_set_npid_open(struct inode *inode, struct file *file) 3010 { 3011 const struct seq_operations *seq_ops = &show_set_no_pid_seq_ops; 3012 struct trace_array *tr = inode->i_private; 3013 int ret; 3014 3015 ret = tracing_check_open_get_tr(tr); 3016 if (ret) 3017 return ret; 3018 3019 if ((file->f_mode & FMODE_WRITE) && 3020 (file->f_flags & O_TRUNC)) 3021 ftrace_clear_event_pids(tr, TRACE_NO_PIDS); 3022 3023 ret = ftrace_event_open(inode, file, seq_ops); 3024 if (ret < 0) 3025 trace_array_put(tr); 3026 return ret; 3027 } 3028 3029 static struct event_subsystem * 3030 create_new_subsystem(const char *name) 3031 { 3032 struct event_subsystem *system; 3033 3034 /* need to create new entry */ 3035 system = kmalloc_obj(*system); 3036 if (!system) 3037 return NULL; 3038 3039 system->ref_count = 1; 3040 3041 /* Only allocate if dynamic (kprobes and modules) */ 3042 system->name = kstrdup_const(name, GFP_KERNEL); 3043 if (!system->name) 3044 goto out_free; 3045 3046 system->filter = kzalloc_obj(struct event_filter); 3047 if (!system->filter) 3048 goto out_free; 3049 3050 list_add(&system->list, &event_subsystems); 3051 3052 return system; 3053 3054 out_free: 3055 kfree_const(system->name); 3056 kfree(system); 3057 return NULL; 3058 } 3059 3060 static int system_callback(const char *name, umode_t *mode, void **data, 3061 const struct file_operations **fops) 3062 { 3063 if (strcmp(name, "filter") == 0) 3064 *fops = &ftrace_subsystem_filter_fops; 3065 3066 else if (strcmp(name, "enable") == 0) 3067 *fops = &ftrace_system_enable_fops; 3068 3069 else 3070 return 0; 3071 3072 *mode = TRACE_MODE_WRITE; 3073 return 1; 3074 } 3075 3076 static struct eventfs_inode * 3077 event_subsystem_dir(struct trace_array *tr, const char *name, 3078 struct trace_event_file *file, struct eventfs_inode *parent) 3079 { 3080 struct event_subsystem *system, *iter; 3081 struct trace_subsystem_dir *dir; 3082 struct eventfs_inode *ei; 3083 int nr_entries; 3084 static struct eventfs_entry system_entries[] = { 3085 { 3086 .name = "filter", 3087 .callback = system_callback, 3088 }, 3089 { 3090 .name = "enable", 3091 .callback = system_callback, 3092 } 3093 }; 3094 3095 /* First see if we did not already create this dir */ 3096 list_for_each_entry(dir, &tr->systems, list) { 3097 system = dir->subsystem; 3098 if (strcmp(system->name, name) == 0) { 3099 dir->nr_events++; 3100 file->system = dir; 3101 return dir->ei; 3102 } 3103 } 3104 3105 /* Now see if the system itself exists. */ 3106 system = NULL; 3107 list_for_each_entry(iter, &event_subsystems, list) { 3108 if (strcmp(iter->name, name) == 0) { 3109 system = iter; 3110 break; 3111 } 3112 } 3113 3114 dir = kmalloc_obj(*dir); 3115 if (!dir) 3116 goto out_fail; 3117 3118 if (!system) { 3119 system = create_new_subsystem(name); 3120 if (!system) 3121 goto out_free; 3122 } else 3123 __get_system(system); 3124 3125 /* ftrace only has directories no files, readonly instance too. */ 3126 if (strcmp(name, "ftrace") == 0 || trace_array_is_readonly(tr)) 3127 nr_entries = 0; 3128 else 3129 nr_entries = ARRAY_SIZE(system_entries); 3130 3131 ei = eventfs_create_dir(name, parent, system_entries, nr_entries, dir); 3132 if (IS_ERR(ei)) { 3133 pr_warn("Failed to create system directory %s\n", name); 3134 __put_system(system); 3135 goto out_free; 3136 } 3137 3138 dir->ei = ei; 3139 dir->tr = tr; 3140 dir->ref_count = 1; 3141 dir->nr_events = 1; 3142 dir->subsystem = system; 3143 file->system = dir; 3144 3145 list_add(&dir->list, &tr->systems); 3146 3147 return dir->ei; 3148 3149 out_free: 3150 kfree(dir); 3151 out_fail: 3152 /* Only print this message if failed on memory allocation */ 3153 if (!dir || !system) 3154 pr_warn("No memory to create event subsystem %s\n", name); 3155 return NULL; 3156 } 3157 3158 static int 3159 event_define_fields(struct trace_event_call *call) 3160 { 3161 struct list_head *head; 3162 int ret = 0; 3163 3164 /* 3165 * Other events may have the same class. Only update 3166 * the fields if they are not already defined. 3167 */ 3168 head = trace_get_fields(call); 3169 if (list_empty(head)) { 3170 struct trace_event_fields *field = call->class->fields_array; 3171 unsigned int offset = sizeof(struct trace_entry); 3172 3173 for (; field->type; field++) { 3174 if (field->type == TRACE_FUNCTION_TYPE) { 3175 field->define_fields(call); 3176 break; 3177 } 3178 3179 offset = ALIGN(offset, field->align); 3180 ret = trace_define_field_ext(call, field->type, field->name, 3181 offset, field->size, 3182 field->is_signed, field->filter_type, 3183 field->len, field->needs_test); 3184 if (WARN_ON_ONCE(ret)) { 3185 pr_err("error code is %d\n", ret); 3186 break; 3187 } 3188 3189 offset += field->size; 3190 } 3191 } 3192 3193 return ret; 3194 } 3195 3196 static int event_callback(const char *name, umode_t *mode, void **data, 3197 const struct file_operations **fops) 3198 { 3199 struct trace_event_file *file = *data; 3200 struct trace_event_call *call = file->event_call; 3201 3202 if (strcmp(name, "format") == 0) { 3203 *mode = TRACE_MODE_READ; 3204 *fops = &ftrace_event_format_fops; 3205 return 1; 3206 } 3207 3208 /* 3209 * Only event directories that can be enabled should have 3210 * triggers or filters, with the exception of the "print" 3211 * event that can have a "trigger" file. 3212 */ 3213 if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)) { 3214 if (call->class->reg && strcmp(name, "enable") == 0) { 3215 *mode = TRACE_MODE_WRITE; 3216 *fops = &ftrace_enable_fops; 3217 return 1; 3218 } 3219 3220 if (strcmp(name, "filter") == 0) { 3221 *mode = TRACE_MODE_WRITE; 3222 *fops = &ftrace_event_filter_fops; 3223 return 1; 3224 } 3225 } 3226 3227 if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) || 3228 strcmp(trace_event_name(call), "print") == 0) { 3229 if (strcmp(name, "trigger") == 0) { 3230 *mode = TRACE_MODE_WRITE; 3231 *fops = &event_trigger_fops; 3232 return 1; 3233 } 3234 } 3235 3236 #ifdef CONFIG_PERF_EVENTS 3237 if (call->event.type && call->class->reg && 3238 strcmp(name, "id") == 0) { 3239 *mode = TRACE_MODE_READ; 3240 *data = (void *)(long)call->event.type; 3241 *fops = &ftrace_event_id_fops; 3242 return 1; 3243 } 3244 #endif 3245 3246 #ifdef CONFIG_BPF_EVENTS 3247 if (call->class->btf_ids && strcmp(name, "btf_ids") == 0) { 3248 *mode = TRACE_MODE_READ; 3249 *fops = &ftrace_event_btf_ids_fops; 3250 return 1; 3251 } 3252 #endif 3253 3254 #ifdef CONFIG_HIST_TRIGGERS 3255 if (strcmp(name, "hist") == 0) { 3256 *mode = TRACE_MODE_READ; 3257 *fops = &event_hist_fops; 3258 return 1; 3259 } 3260 #endif 3261 #ifdef CONFIG_HIST_TRIGGERS_DEBUG 3262 if (strcmp(name, "hist_debug") == 0) { 3263 *mode = TRACE_MODE_READ; 3264 *fops = &event_hist_debug_fops; 3265 return 1; 3266 } 3267 #endif 3268 #ifdef CONFIG_TRACE_EVENT_INJECT 3269 if (call->event.type && call->class->reg && 3270 strcmp(name, "inject") == 0) { 3271 *mode = 0200; 3272 *fops = &event_inject_fops; 3273 return 1; 3274 } 3275 #endif 3276 return 0; 3277 } 3278 3279 /* The file is incremented on creation and freeing the enable file decrements it */ 3280 static void event_release(const char *name, void *data) 3281 { 3282 struct trace_event_file *file = data; 3283 3284 event_file_put(file); 3285 } 3286 3287 static int 3288 event_create_dir(struct eventfs_inode *parent, struct trace_event_file *file) 3289 { 3290 struct trace_event_call *call = file->event_call; 3291 struct trace_array *tr = file->tr; 3292 struct eventfs_inode *e_events; 3293 struct eventfs_inode *ei; 3294 const char *name; 3295 int nr_entries; 3296 int ret; 3297 static struct eventfs_entry event_entries[] = { 3298 { 3299 .name = "format", 3300 .callback = event_callback, 3301 }, 3302 #ifdef CONFIG_PERF_EVENTS 3303 { 3304 .name = "id", 3305 .callback = event_callback, 3306 }, 3307 #endif 3308 #ifdef CONFIG_BPF_EVENTS 3309 { 3310 .name = "btf_ids", 3311 .callback = event_callback, 3312 }, 3313 #endif 3314 #define NR_RO_EVENT_ENTRIES (1 + IS_ENABLED(CONFIG_PERF_EVENTS) + \ 3315 IS_ENABLED(CONFIG_BPF_EVENTS)) 3316 /* Readonly files must be above this line and counted by NR_RO_EVENT_ENTRIES. */ 3317 { 3318 .name = "enable", 3319 .callback = event_callback, 3320 .release = event_release, 3321 }, 3322 { 3323 .name = "filter", 3324 .callback = event_callback, 3325 }, 3326 { 3327 .name = "trigger", 3328 .callback = event_callback, 3329 }, 3330 #ifdef CONFIG_HIST_TRIGGERS 3331 { 3332 .name = "hist", 3333 .callback = event_callback, 3334 }, 3335 #endif 3336 #ifdef CONFIG_HIST_TRIGGERS_DEBUG 3337 { 3338 .name = "hist_debug", 3339 .callback = event_callback, 3340 }, 3341 #endif 3342 #ifdef CONFIG_TRACE_EVENT_INJECT 3343 { 3344 .name = "inject", 3345 .callback = event_callback, 3346 }, 3347 #endif 3348 }; 3349 3350 /* 3351 * If the trace point header did not define TRACE_SYSTEM 3352 * then the system would be called "TRACE_SYSTEM". This should 3353 * never happen. 3354 */ 3355 if (WARN_ON_ONCE(strcmp(call->class->system, TRACE_SYSTEM) == 0)) 3356 return -ENODEV; 3357 3358 ret = event_define_fields(call); 3359 if (ret < 0) { 3360 pr_warn("Could not initialize trace point events/%s\n", 3361 trace_event_name(call)); 3362 return ret; 3363 } 3364 3365 e_events = event_subsystem_dir(tr, call->class->system, file, parent); 3366 if (!e_events) 3367 return -ENOMEM; 3368 3369 if (trace_array_is_readonly(tr)) 3370 nr_entries = NR_RO_EVENT_ENTRIES; 3371 else 3372 nr_entries = ARRAY_SIZE(event_entries); 3373 3374 name = trace_event_name(call); 3375 ei = eventfs_create_dir(name, e_events, event_entries, nr_entries, file); 3376 if (IS_ERR(ei)) { 3377 pr_warn("Could not create tracefs '%s' directory\n", name); 3378 return -1; 3379 } 3380 3381 file->ei = ei; 3382 3383 /* Gets decremented on freeing of the "enable" file */ 3384 event_file_get(file); 3385 3386 return 0; 3387 } 3388 3389 static void remove_event_from_tracers(struct trace_event_call *call) 3390 { 3391 struct trace_event_file *file; 3392 struct trace_array *tr; 3393 3394 do_for_each_event_file_safe(tr, file) { 3395 if (file->event_call != call) 3396 continue; 3397 3398 remove_event_file_dir(file); 3399 /* 3400 * The do_for_each_event_file_safe() is 3401 * a double loop. After finding the call for this 3402 * trace_array, we use break to jump to the next 3403 * trace_array. 3404 */ 3405 break; 3406 } while_for_each_event_file(); 3407 } 3408 3409 static void event_remove(struct trace_event_call *call) 3410 { 3411 struct trace_array *tr; 3412 struct trace_event_file *file; 3413 3414 do_for_each_event_file(tr, file) { 3415 if (file->event_call != call) 3416 continue; 3417 3418 if (file->flags & EVENT_FILE_FL_WAS_ENABLED) 3419 tr->clear_trace = true; 3420 3421 ftrace_event_enable_disable(file, 0); 3422 /* 3423 * The do_for_each_event_file() is 3424 * a double loop. After finding the call for this 3425 * trace_array, we use break to jump to the next 3426 * trace_array. 3427 */ 3428 break; 3429 } while_for_each_event_file(); 3430 3431 if (call->event.funcs) 3432 __unregister_trace_event(&call->event); 3433 remove_event_from_tracers(call); 3434 list_del(&call->list); 3435 } 3436 3437 static int event_init(struct trace_event_call *call) 3438 { 3439 int ret = 0; 3440 const char *name; 3441 3442 name = trace_event_name(call); 3443 if (WARN_ON(!name)) 3444 return -EINVAL; 3445 3446 if (call->class->raw_init) { 3447 ret = call->class->raw_init(call); 3448 if (ret < 0 && ret != -ENOSYS) 3449 pr_warn("Could not initialize trace events/%s\n", name); 3450 } 3451 3452 return ret; 3453 } 3454 3455 static int 3456 __register_event(struct trace_event_call *call, struct module *mod) 3457 { 3458 int ret; 3459 3460 ret = event_init(call); 3461 if (ret < 0) 3462 return ret; 3463 3464 down_write(&trace_event_sem); 3465 list_add(&call->list, &ftrace_events); 3466 up_write(&trace_event_sem); 3467 3468 if (call->flags & TRACE_EVENT_FL_DYNAMIC) 3469 atomic_set(&call->refcnt, 0); 3470 else 3471 call->module = mod; 3472 3473 return 0; 3474 } 3475 3476 static char *eval_replace(char *ptr, struct trace_eval_map *map, int len) 3477 { 3478 int rlen; 3479 int elen; 3480 3481 /* Find the length of the eval value as a string */ 3482 elen = snprintf(ptr, 0, "%ld", map->eval_value); 3483 /* Make sure there's enough room to replace the string with the value */ 3484 if (len < elen) 3485 return NULL; 3486 3487 snprintf(ptr, elen + 1, "%ld", map->eval_value); 3488 3489 /* Get the rest of the string of ptr */ 3490 rlen = strlen(ptr + len); 3491 memmove(ptr + elen, ptr + len, rlen); 3492 /* Make sure we end the new string */ 3493 ptr[elen + rlen] = 0; 3494 3495 return ptr + elen; 3496 } 3497 3498 static void update_event_printk(struct trace_event_call *call, 3499 struct trace_eval_map *map) 3500 { 3501 char *ptr; 3502 int quote = 0; 3503 int len = strlen(map->eval_string); 3504 3505 for (ptr = call->print_fmt; *ptr; ptr++) { 3506 if (*ptr == '\\') { 3507 ptr++; 3508 /* paranoid */ 3509 if (!*ptr) 3510 break; 3511 continue; 3512 } 3513 if (*ptr == '"') { 3514 quote ^= 1; 3515 continue; 3516 } 3517 if (quote) 3518 continue; 3519 if (isdigit(*ptr)) { 3520 /* skip numbers */ 3521 do { 3522 ptr++; 3523 /* Check for alpha chars like ULL */ 3524 } while (isalnum(*ptr)); 3525 if (!*ptr) 3526 break; 3527 /* 3528 * A number must have some kind of delimiter after 3529 * it, and we can ignore that too. 3530 */ 3531 continue; 3532 } 3533 if (isalpha(*ptr) || *ptr == '_') { 3534 if (strncmp(map->eval_string, ptr, len) == 0 && 3535 !isalnum(ptr[len]) && ptr[len] != '_') { 3536 ptr = eval_replace(ptr, map, len); 3537 /* enum/sizeof string smaller than value */ 3538 if (WARN_ON_ONCE(!ptr)) 3539 return; 3540 /* 3541 * No need to decrement here, as eval_replace() 3542 * returns the pointer to the character passed 3543 * the eval, and two evals can not be placed 3544 * back to back without something in between. 3545 * We can skip that something in between. 3546 */ 3547 continue; 3548 } 3549 skip_more: 3550 do { 3551 ptr++; 3552 } while (isalnum(*ptr) || *ptr == '_'); 3553 if (!*ptr) 3554 break; 3555 /* 3556 * If what comes after this variable is a '.' or 3557 * '->' then we can continue to ignore that string. 3558 */ 3559 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) { 3560 ptr += *ptr == '.' ? 1 : 2; 3561 if (!*ptr) 3562 break; 3563 goto skip_more; 3564 } 3565 /* 3566 * Once again, we can skip the delimiter that came 3567 * after the string. 3568 */ 3569 continue; 3570 } 3571 } 3572 } 3573 3574 static void add_str_to_module(struct module *module, char *str) 3575 { 3576 struct module_string *modstr; 3577 3578 modstr = kmalloc_obj(*modstr); 3579 3580 /* 3581 * If we failed to allocate memory here, then we'll just 3582 * let the str memory leak when the module is removed. 3583 * If this fails to allocate, there's worse problems than 3584 * a leaked string on module removal. 3585 */ 3586 if (WARN_ON_ONCE(!modstr)) 3587 return; 3588 3589 modstr->module = module; 3590 modstr->str = str; 3591 3592 list_add(&modstr->next, &module_strings); 3593 } 3594 3595 #define ATTRIBUTE_STR "__attribute__(" 3596 #define ATTRIBUTE_STR_LEN (sizeof(ATTRIBUTE_STR) - 1) 3597 3598 /* Remove all __attribute__() from @type. Return allocated string or @type. */ 3599 static char *sanitize_field_type(const char *type) 3600 { 3601 char *attr, *tmp, *next, *ret = (char *)type; 3602 int depth; 3603 3604 next = (char *)type; 3605 while ((attr = strstr(next, ATTRIBUTE_STR))) { 3606 /* Retry if "__attribute__(" is a part of another word. */ 3607 if (attr != next && !isspace(attr[-1])) { 3608 next = attr + ATTRIBUTE_STR_LEN; 3609 continue; 3610 } 3611 3612 if (ret == type) { 3613 ret = kstrdup(type, GFP_KERNEL); 3614 if (WARN_ON_ONCE(!ret)) 3615 return NULL; 3616 attr = ret + (attr - type); 3617 } 3618 3619 /* the ATTRIBUTE_STR already has the first '(' */ 3620 depth = 1; 3621 next = attr + ATTRIBUTE_STR_LEN; 3622 do { 3623 tmp = strpbrk(next, "()"); 3624 /* There is unbalanced parentheses */ 3625 if (WARN_ON_ONCE(!tmp)) { 3626 kfree(ret); 3627 return (char *)type; 3628 } 3629 3630 if (*tmp == '(') 3631 depth++; 3632 else 3633 depth--; 3634 next = tmp + 1; 3635 } while (depth > 0); 3636 next = skip_spaces(next); 3637 strcpy(attr, next); 3638 next = attr; 3639 } 3640 return ret; 3641 } 3642 3643 static char *find_replacable_eval(const char *type, const char *eval_string, 3644 int len) 3645 { 3646 char *ptr; 3647 3648 if (!eval_string) 3649 return NULL; 3650 3651 ptr = strchr(type, '['); 3652 if (!ptr) 3653 return NULL; 3654 ptr++; 3655 3656 if (!isalpha(*ptr) && *ptr != '_') 3657 return NULL; 3658 3659 if (strncmp(eval_string, ptr, len) != 0) 3660 return NULL; 3661 3662 return ptr; 3663 } 3664 3665 static void update_event_fields(struct trace_event_call *call, 3666 struct trace_eval_map *map) 3667 { 3668 struct ftrace_event_field *field; 3669 const char *eval_string = NULL; 3670 struct list_head *head; 3671 int len = 0; 3672 char *ptr; 3673 char *str; 3674 3675 /* Dynamic events should never have field maps */ 3676 if (call->flags & TRACE_EVENT_FL_DYNAMIC) 3677 return; 3678 3679 if (map) { 3680 eval_string = map->eval_string; 3681 len = strlen(map->eval_string); 3682 } 3683 3684 head = trace_get_fields(call); 3685 list_for_each_entry(field, head, link) { 3686 str = sanitize_field_type(field->type); 3687 if (!str) 3688 return; 3689 3690 ptr = find_replacable_eval(str, eval_string, len); 3691 if (ptr) { 3692 if (str == field->type) { 3693 str = kstrdup(field->type, GFP_KERNEL); 3694 if (WARN_ON_ONCE(!str)) 3695 return; 3696 ptr = str + (ptr - field->type); 3697 } 3698 3699 ptr = eval_replace(ptr, map, len); 3700 /* enum/sizeof string smaller than value */ 3701 if (WARN_ON_ONCE(!ptr)) { 3702 kfree(str); 3703 continue; 3704 } 3705 } 3706 3707 if (str == field->type) 3708 continue; 3709 /* 3710 * If the event is part of a module, then we need to free the string 3711 * when the module is removed. Otherwise, it will stay allocated 3712 * until a reboot. 3713 */ 3714 if (call->module) 3715 add_str_to_module(call->module, str); 3716 3717 field->type = str; 3718 if (field->filter_type == FILTER_OTHER) 3719 field->filter_type = filter_assign_type(field->type); 3720 } 3721 } 3722 3723 /* Update all events for replacing eval and sanitizing */ 3724 void trace_event_update_all(struct trace_eval_map **map, int len, struct module *mod) 3725 { 3726 struct trace_event_call *call, *p; 3727 const char *last_system = NULL; 3728 bool first = false; 3729 bool updated; 3730 int last_i; 3731 int i; 3732 3733 mutex_lock(&event_mutex); 3734 down_write(&trace_event_sem); 3735 list_for_each_entry_safe(call, p, &ftrace_events, list) { 3736 3737 if (mod && call->module != mod) 3738 continue; 3739 3740 /* events are usually grouped together with systems */ 3741 if (!last_system || call->class->system != last_system) { 3742 first = true; 3743 last_i = 0; 3744 last_system = call->class->system; 3745 } 3746 3747 updated = false; 3748 /* 3749 * Since calls are grouped by systems, the likelihood that the 3750 * next call in the iteration belongs to the same system as the 3751 * previous call is high. As an optimization, we skip searching 3752 * for a map[] that matches the call's system if the last call 3753 * was from the same system. That's what last_i is for. If the 3754 * call has the same system as the previous call, then last_i 3755 * will be the index of the first map[] that has a matching 3756 * system. 3757 */ 3758 for (i = last_i; i < len; i++) { 3759 if (call->class->system == map[i]->system) { 3760 /* Save the first system if need be */ 3761 if (first) { 3762 last_i = i; 3763 first = false; 3764 } 3765 update_event_printk(call, map[i]); 3766 update_event_fields(call, map[i]); 3767 updated = true; 3768 } 3769 } 3770 /* If not updated yet, update field for sanitizing. */ 3771 if (!updated) 3772 update_event_fields(call, NULL); 3773 cond_resched(); 3774 } 3775 up_write(&trace_event_sem); 3776 mutex_unlock(&event_mutex); 3777 } 3778 3779 static bool event_in_systems(struct trace_event_call *call, 3780 const char *systems) 3781 { 3782 const char *system; 3783 const char *p; 3784 3785 if (!systems) 3786 return true; 3787 3788 system = call->class->system; 3789 p = strstr(systems, system); 3790 if (!p) 3791 return false; 3792 3793 if (p != systems && !isspace(*(p - 1)) && *(p - 1) != ',') 3794 return false; 3795 3796 p += strlen(system); 3797 return !*p || isspace(*p) || *p == ','; 3798 } 3799 3800 #ifdef CONFIG_HIST_TRIGGERS 3801 /* 3802 * Wake up waiter on the hist_poll_wq from irq_work because the hist trigger 3803 * may happen in any context. 3804 */ 3805 static void hist_poll_event_irq_work(struct irq_work *work) 3806 { 3807 wake_up_all(&hist_poll_wq); 3808 } 3809 3810 DEFINE_IRQ_WORK(hist_poll_work, hist_poll_event_irq_work); 3811 DECLARE_WAIT_QUEUE_HEAD(hist_poll_wq); 3812 #endif 3813 3814 static struct trace_event_file * 3815 trace_create_new_event(struct trace_event_call *call, 3816 struct trace_array *tr) 3817 { 3818 struct trace_pid_list *no_pid_list; 3819 struct trace_pid_list *pid_list; 3820 struct trace_event_file *file; 3821 unsigned int first; 3822 3823 if (!event_in_systems(call, tr->system_names)) 3824 return NULL; 3825 3826 file = kmem_cache_alloc(file_cachep, GFP_TRACE); 3827 if (!file) 3828 return ERR_PTR(-ENOMEM); 3829 3830 pid_list = rcu_dereference_protected(tr->filtered_pids, 3831 lockdep_is_held(&event_mutex)); 3832 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids, 3833 lockdep_is_held(&event_mutex)); 3834 3835 if (!trace_pid_list_first(pid_list, &first) || 3836 !trace_pid_list_first(no_pid_list, &first)) 3837 file->flags |= EVENT_FILE_FL_PID_FILTER; 3838 3839 file->event_call = call; 3840 file->tr = tr; 3841 atomic_set(&file->sm_ref, 0); 3842 atomic_set(&file->tm_ref, 0); 3843 INIT_LIST_HEAD(&file->triggers); 3844 list_add(&file->list, &tr->events); 3845 refcount_set(&file->ref, 1); 3846 3847 return file; 3848 } 3849 3850 #define MAX_BOOT_TRIGGERS 32 3851 3852 static struct boot_triggers { 3853 const char *event; 3854 char *trigger; 3855 } bootup_triggers[MAX_BOOT_TRIGGERS]; 3856 3857 static char bootup_trigger_buf[COMMAND_LINE_SIZE]; 3858 static int boot_trigger_buf_len; 3859 static int nr_boot_triggers; 3860 3861 static __init int setup_trace_triggers(char *str) 3862 { 3863 char *trigger; 3864 char *buf; 3865 int len = boot_trigger_buf_len; 3866 int i; 3867 3868 if (len >= COMMAND_LINE_SIZE) 3869 return 1; 3870 3871 strscpy(bootup_trigger_buf + len, str, COMMAND_LINE_SIZE - len); 3872 trace_set_ring_buffer_expanded(NULL); 3873 disable_tracing_selftest("running event triggers"); 3874 3875 buf = bootup_trigger_buf + len; 3876 boot_trigger_buf_len += strlen(buf) + 1; 3877 3878 for (i = nr_boot_triggers; i < MAX_BOOT_TRIGGERS; i++) { 3879 trigger = strsep(&buf, ","); 3880 if (!trigger) 3881 break; 3882 bootup_triggers[i].event = strsep(&trigger, "."); 3883 bootup_triggers[i].trigger = trigger; 3884 if (!bootup_triggers[i].trigger) 3885 break; 3886 } 3887 3888 nr_boot_triggers = i; 3889 return 1; 3890 } 3891 __setup("trace_trigger=", setup_trace_triggers); 3892 3893 /* Add an event to a trace directory */ 3894 static int 3895 __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr) 3896 { 3897 struct trace_event_file *file; 3898 3899 file = trace_create_new_event(call, tr); 3900 /* 3901 * trace_create_new_event() returns ERR_PTR(-ENOMEM) if failed 3902 * allocation, or NULL if the event is not part of the tr->system_names. 3903 * When the event is not part of the tr->system_names, return zero, not 3904 * an error. 3905 */ 3906 if (!file) 3907 return 0; 3908 3909 if (IS_ERR(file)) 3910 return PTR_ERR(file); 3911 3912 if (eventdir_initialized) 3913 return event_create_dir(tr->event_dir, file); 3914 else 3915 return event_define_fields(call); 3916 } 3917 3918 static void trace_early_triggers(struct trace_event_file *file, const char *name) 3919 { 3920 int ret; 3921 int i; 3922 3923 for (i = 0; i < nr_boot_triggers; i++) { 3924 if (strcmp(name, bootup_triggers[i].event)) 3925 continue; 3926 mutex_lock(&event_mutex); 3927 ret = trigger_process_regex(file, bootup_triggers[i].trigger); 3928 mutex_unlock(&event_mutex); 3929 if (ret) 3930 pr_err("Failed to register trigger '%s' on event %s\n", 3931 bootup_triggers[i].trigger, 3932 bootup_triggers[i].event); 3933 } 3934 } 3935 3936 /* 3937 * Just create a descriptor for early init. A descriptor is required 3938 * for enabling events at boot. We want to enable events before 3939 * the filesystem is initialized. 3940 */ 3941 static int 3942 __trace_early_add_new_event(struct trace_event_call *call, 3943 struct trace_array *tr) 3944 { 3945 struct trace_event_file *file; 3946 int ret; 3947 3948 file = trace_create_new_event(call, tr); 3949 /* 3950 * trace_create_new_event() returns ERR_PTR(-ENOMEM) if failed 3951 * allocation, or NULL if the event is not part of the tr->system_names. 3952 * When the event is not part of the tr->system_names, return zero, not 3953 * an error. 3954 */ 3955 if (!file) 3956 return 0; 3957 3958 if (IS_ERR(file)) 3959 return PTR_ERR(file); 3960 3961 ret = event_define_fields(call); 3962 if (ret) 3963 return ret; 3964 3965 trace_early_triggers(file, trace_event_name(call)); 3966 3967 return 0; 3968 } 3969 3970 struct ftrace_module_file_ops; 3971 static void __add_event_to_tracers(struct trace_event_call *call); 3972 3973 /* Add an additional event_call dynamically */ 3974 int trace_add_event_call(struct trace_event_call *call) 3975 { 3976 int ret; 3977 lockdep_assert_held(&event_mutex); 3978 3979 guard(mutex)(&trace_types_lock); 3980 3981 ret = __register_event(call, NULL); 3982 if (ret < 0) 3983 return ret; 3984 3985 __add_event_to_tracers(call); 3986 return ret; 3987 } 3988 EXPORT_SYMBOL_GPL(trace_add_event_call); 3989 3990 /* 3991 * Must be called under locking of trace_types_lock, event_mutex and 3992 * trace_event_sem. 3993 */ 3994 static void __trace_remove_event_call(struct trace_event_call *call) 3995 { 3996 event_remove(call); 3997 trace_destroy_fields(call); 3998 } 3999 4000 static int probe_remove_event_call(struct trace_event_call *call) 4001 { 4002 struct trace_array *tr; 4003 struct trace_event_file *file; 4004 4005 #ifdef CONFIG_PERF_EVENTS 4006 if (call->perf_refcount) 4007 return -EBUSY; 4008 #endif 4009 do_for_each_event_file(tr, file) { 4010 if (file->event_call != call) 4011 continue; 4012 /* 4013 * We can't rely on ftrace_event_enable_disable(enable => 0) 4014 * we are going to do, soft mode can suppress 4015 * TRACE_REG_UNREGISTER. 4016 */ 4017 if (file->flags & EVENT_FILE_FL_ENABLED) 4018 goto busy; 4019 4020 if (file->flags & EVENT_FILE_FL_WAS_ENABLED) 4021 tr->clear_trace = true; 4022 /* 4023 * The do_for_each_event_file_safe() is 4024 * a double loop. After finding the call for this 4025 * trace_array, we use break to jump to the next 4026 * trace_array. 4027 */ 4028 break; 4029 } while_for_each_event_file(); 4030 4031 __trace_remove_event_call(call); 4032 4033 return 0; 4034 busy: 4035 /* No need to clear the trace now */ 4036 list_for_each_entry(tr, &ftrace_trace_arrays, list) { 4037 tr->clear_trace = false; 4038 } 4039 return -EBUSY; 4040 } 4041 4042 /* Remove an event_call */ 4043 int trace_remove_event_call(struct trace_event_call *call) 4044 { 4045 int ret; 4046 4047 lockdep_assert_held(&event_mutex); 4048 4049 mutex_lock(&trace_types_lock); 4050 down_write(&trace_event_sem); 4051 ret = probe_remove_event_call(call); 4052 up_write(&trace_event_sem); 4053 mutex_unlock(&trace_types_lock); 4054 4055 return ret; 4056 } 4057 EXPORT_SYMBOL_GPL(trace_remove_event_call); 4058 4059 #define for_each_event(event, start, end) \ 4060 for (event = start; \ 4061 (unsigned long)event < (unsigned long)end; \ 4062 event++) 4063 4064 #ifdef CONFIG_MODULES 4065 static void update_mod_cache(struct trace_array *tr, struct module *mod) 4066 { 4067 struct event_mod_load *event_mod, *n; 4068 4069 list_for_each_entry_safe(event_mod, n, &tr->mod_events, list) { 4070 if (strcmp(event_mod->module, mod->name) != 0) 4071 continue; 4072 4073 __ftrace_set_clr_event_nolock(tr, event_mod->match, 4074 event_mod->system, 4075 event_mod->event, 1, mod->name, 4076 event_mod->filter); 4077 free_event_mod(event_mod); 4078 } 4079 } 4080 4081 static void update_cache_events(struct module *mod) 4082 { 4083 struct trace_array *tr; 4084 4085 list_for_each_entry(tr, &ftrace_trace_arrays, list) 4086 update_mod_cache(tr, mod); 4087 } 4088 4089 static void trace_module_add_events(struct module *mod) 4090 { 4091 struct trace_event_call **call, **start, **end; 4092 4093 if (!mod->num_trace_events) 4094 return; 4095 4096 /* Don't add infrastructure for mods without tracepoints */ 4097 if (trace_module_has_bad_taint(mod)) { 4098 pr_err("%s: module has bad taint, not creating trace events\n", 4099 mod->name); 4100 return; 4101 } 4102 4103 start = mod->trace_events; 4104 end = mod->trace_events + mod->num_trace_events; 4105 4106 for_each_event(call, start, end) { 4107 if (!__register_event(*call, mod)) 4108 __add_event_to_tracers(*call); 4109 } 4110 4111 update_cache_events(mod); 4112 } 4113 4114 static void trace_module_remove_events(struct module *mod) 4115 { 4116 struct trace_event_call *call, *p; 4117 struct module_string *modstr, *m; 4118 4119 down_write(&trace_event_sem); 4120 list_for_each_entry_safe(call, p, &ftrace_events, list) { 4121 if ((call->flags & TRACE_EVENT_FL_DYNAMIC) || !call->module) 4122 continue; 4123 if (call->module == mod) 4124 __trace_remove_event_call(call); 4125 } 4126 /* Check for any strings allocated for this module */ 4127 list_for_each_entry_safe(modstr, m, &module_strings, next) { 4128 if (modstr->module != mod) 4129 continue; 4130 list_del(&modstr->next); 4131 kfree(modstr->str); 4132 kfree(modstr); 4133 } 4134 up_write(&trace_event_sem); 4135 4136 /* 4137 * It is safest to reset the ring buffer if the module being unloaded 4138 * registered any events that were used. The only worry is if 4139 * a new module gets loaded, and takes on the same id as the events 4140 * of this module. When printing out the buffer, traced events left 4141 * over from this module may be passed to the new module events and 4142 * unexpected results may occur. 4143 */ 4144 tracing_reset_all_online_cpus_unlocked(); 4145 } 4146 4147 static int trace_module_notify(struct notifier_block *self, 4148 unsigned long val, void *data) 4149 { 4150 struct module *mod = data; 4151 4152 mutex_lock(&event_mutex); 4153 mutex_lock(&trace_types_lock); 4154 switch (val) { 4155 case MODULE_STATE_COMING: 4156 trace_module_add_events(mod); 4157 break; 4158 case MODULE_STATE_GOING: 4159 trace_module_remove_events(mod); 4160 break; 4161 } 4162 mutex_unlock(&trace_types_lock); 4163 mutex_unlock(&event_mutex); 4164 4165 return NOTIFY_OK; 4166 } 4167 4168 static struct notifier_block trace_module_nb = { 4169 .notifier_call = trace_module_notify, 4170 .priority = 1, /* higher than trace.c module notify */ 4171 }; 4172 #endif /* CONFIG_MODULES */ 4173 4174 /* Create a new event directory structure for a trace directory. */ 4175 static void 4176 __trace_add_event_dirs(struct trace_array *tr) 4177 { 4178 struct trace_event_call *call; 4179 int ret; 4180 4181 lockdep_assert_held(&trace_event_sem); 4182 4183 list_for_each_entry(call, &ftrace_events, list) { 4184 ret = __trace_add_new_event(call, tr); 4185 if (ret < 0) 4186 pr_warn("Could not create directory for event %s\n", 4187 trace_event_name(call)); 4188 } 4189 } 4190 4191 /* Returns any file that matches the system and event */ 4192 struct trace_event_file * 4193 __find_event_file(struct trace_array *tr, const char *system, const char *event) 4194 { 4195 struct trace_event_file *file; 4196 struct trace_event_call *call; 4197 const char *name; 4198 4199 list_for_each_entry(file, &tr->events, list) { 4200 4201 call = file->event_call; 4202 name = trace_event_name(call); 4203 4204 if (!name || !call->class) 4205 continue; 4206 4207 if (strcmp(event, name) == 0 && 4208 strcmp(system, call->class->system) == 0) 4209 return file; 4210 } 4211 return NULL; 4212 } 4213 4214 /* Returns valid trace event files that match system and event */ 4215 struct trace_event_file * 4216 find_event_file(struct trace_array *tr, const char *system, const char *event) 4217 { 4218 struct trace_event_file *file; 4219 4220 file = __find_event_file(tr, system, event); 4221 if (!file || !file->event_call->class->reg || 4222 file->event_call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) 4223 return NULL; 4224 4225 return file; 4226 } 4227 4228 /** 4229 * trace_get_event_file - Find and return a trace event file 4230 * @instance: The name of the trace instance containing the event 4231 * @system: The name of the system containing the event 4232 * @event: The name of the event 4233 * 4234 * Return a trace event file given the trace instance name, trace 4235 * system, and trace event name. If the instance name is NULL, it 4236 * refers to the top-level trace array. 4237 * 4238 * This function will look it up and return it if found, after calling 4239 * trace_array_get() to prevent the instance from going away, and 4240 * increment the event's module refcount to prevent it from being 4241 * removed. 4242 * 4243 * To release the file, call trace_put_event_file(), which will call 4244 * trace_array_put() and decrement the event's module refcount. 4245 * 4246 * Return: The trace event on success, ERR_PTR otherwise. 4247 */ 4248 struct trace_event_file *trace_get_event_file(const char *instance, 4249 const char *system, 4250 const char *event) 4251 { 4252 struct trace_array *tr = top_trace_array(); 4253 struct trace_event_file *file = NULL; 4254 int ret = -EINVAL; 4255 4256 if (instance) { 4257 tr = trace_array_find_get(instance); 4258 if (!tr) 4259 return ERR_PTR(-ENOENT); 4260 } else { 4261 ret = trace_array_get(tr); 4262 if (ret) 4263 return ERR_PTR(ret); 4264 } 4265 4266 guard(mutex)(&event_mutex); 4267 4268 file = find_event_file(tr, system, event); 4269 if (!file) { 4270 trace_array_put(tr); 4271 return ERR_PTR(-EINVAL); 4272 } 4273 4274 /* Don't let event modules unload while in use */ 4275 ret = trace_event_try_get_ref(file->event_call); 4276 if (!ret) { 4277 trace_array_put(tr); 4278 return ERR_PTR(-EBUSY); 4279 } 4280 4281 return file; 4282 } 4283 EXPORT_SYMBOL_GPL(trace_get_event_file); 4284 4285 /** 4286 * trace_put_event_file - Release a file from trace_get_event_file() 4287 * @file: The trace event file 4288 * 4289 * If a file was retrieved using trace_get_event_file(), this should 4290 * be called when it's no longer needed. It will cancel the previous 4291 * trace_array_get() called by that function, and decrement the 4292 * event's module refcount. 4293 */ 4294 void trace_put_event_file(struct trace_event_file *file) 4295 { 4296 mutex_lock(&event_mutex); 4297 trace_event_put_ref(file->event_call); 4298 mutex_unlock(&event_mutex); 4299 4300 trace_array_put(file->tr); 4301 } 4302 EXPORT_SYMBOL_GPL(trace_put_event_file); 4303 4304 #ifdef CONFIG_DYNAMIC_FTRACE 4305 struct event_probe_data { 4306 struct trace_event_file *file; 4307 unsigned long count; 4308 int ref; 4309 bool enable; 4310 }; 4311 4312 static void update_event_probe(struct event_probe_data *data) 4313 { 4314 if (data->enable) 4315 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags); 4316 else 4317 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags); 4318 } 4319 4320 static void notrace 4321 event_enable_probe(unsigned long ip, unsigned long parent_ip, 4322 struct trace_array *tr, struct ftrace_probe_ops *ops, 4323 void *data) 4324 { 4325 struct ftrace_func_mapper *mapper = data; 4326 struct event_probe_data *edata; 4327 void **pdata; 4328 4329 pdata = ftrace_func_mapper_find_ip(mapper, ip); 4330 if (!pdata || !*pdata) 4331 return; 4332 4333 edata = *pdata; 4334 update_event_probe(edata); 4335 } 4336 4337 static void notrace 4338 event_enable_count_probe(unsigned long ip, unsigned long parent_ip, 4339 struct trace_array *tr, struct ftrace_probe_ops *ops, 4340 void *data) 4341 { 4342 struct ftrace_func_mapper *mapper = data; 4343 struct event_probe_data *edata; 4344 void **pdata; 4345 4346 pdata = ftrace_func_mapper_find_ip(mapper, ip); 4347 if (!pdata || !*pdata) 4348 return; 4349 4350 edata = *pdata; 4351 4352 if (!edata->count) 4353 return; 4354 4355 /* Skip if the event is in a state we want to switch to */ 4356 if (edata->enable == !(edata->file->flags & EVENT_FILE_FL_SOFT_DISABLED)) 4357 return; 4358 4359 if (edata->count != -1) 4360 (edata->count)--; 4361 4362 update_event_probe(edata); 4363 } 4364 4365 static int 4366 event_enable_print(struct seq_file *m, unsigned long ip, 4367 struct ftrace_probe_ops *ops, void *data) 4368 { 4369 struct ftrace_func_mapper *mapper = data; 4370 struct event_probe_data *edata; 4371 void **pdata; 4372 4373 pdata = ftrace_func_mapper_find_ip(mapper, ip); 4374 4375 if (WARN_ON_ONCE(!pdata || !*pdata)) 4376 return 0; 4377 4378 edata = *pdata; 4379 4380 seq_printf(m, "%ps:", (void *)ip); 4381 4382 seq_printf(m, "%s:%s:%s", 4383 edata->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR, 4384 edata->file->event_call->class->system, 4385 trace_event_name(edata->file->event_call)); 4386 4387 if (edata->count == -1) 4388 seq_puts(m, ":unlimited\n"); 4389 else 4390 seq_printf(m, ":count=%ld\n", edata->count); 4391 4392 return 0; 4393 } 4394 4395 static int 4396 event_enable_init(struct ftrace_probe_ops *ops, struct trace_array *tr, 4397 unsigned long ip, void *init_data, void **data) 4398 { 4399 struct ftrace_func_mapper *mapper = *data; 4400 struct event_probe_data *edata = init_data; 4401 int ret; 4402 4403 if (!mapper) { 4404 mapper = allocate_ftrace_func_mapper(); 4405 if (!mapper) 4406 return -ENODEV; 4407 *data = mapper; 4408 } 4409 4410 ret = ftrace_func_mapper_add_ip(mapper, ip, edata); 4411 if (ret < 0) 4412 return ret; 4413 4414 edata->ref++; 4415 4416 return 0; 4417 } 4418 4419 static int free_probe_data(void *data) 4420 { 4421 struct event_probe_data *edata = data; 4422 4423 edata->ref--; 4424 if (!edata->ref) { 4425 /* Remove soft mode */ 4426 __ftrace_event_enable_disable(edata->file, 0, 1); 4427 trace_event_put_ref(edata->file->event_call); 4428 kfree(edata); 4429 } 4430 return 0; 4431 } 4432 4433 static void 4434 event_enable_free(struct ftrace_probe_ops *ops, struct trace_array *tr, 4435 unsigned long ip, void *data) 4436 { 4437 struct ftrace_func_mapper *mapper = data; 4438 struct event_probe_data *edata; 4439 4440 if (!ip) { 4441 if (!mapper) 4442 return; 4443 free_ftrace_func_mapper(mapper, free_probe_data); 4444 return; 4445 } 4446 4447 edata = ftrace_func_mapper_remove_ip(mapper, ip); 4448 4449 if (WARN_ON_ONCE(!edata)) 4450 return; 4451 4452 if (WARN_ON_ONCE(edata->ref <= 0)) 4453 return; 4454 4455 free_probe_data(edata); 4456 } 4457 4458 static struct ftrace_probe_ops event_enable_probe_ops = { 4459 .func = event_enable_probe, 4460 .print = event_enable_print, 4461 .init = event_enable_init, 4462 .free = event_enable_free, 4463 }; 4464 4465 static struct ftrace_probe_ops event_enable_count_probe_ops = { 4466 .func = event_enable_count_probe, 4467 .print = event_enable_print, 4468 .init = event_enable_init, 4469 .free = event_enable_free, 4470 }; 4471 4472 static struct ftrace_probe_ops event_disable_probe_ops = { 4473 .func = event_enable_probe, 4474 .print = event_enable_print, 4475 .init = event_enable_init, 4476 .free = event_enable_free, 4477 }; 4478 4479 static struct ftrace_probe_ops event_disable_count_probe_ops = { 4480 .func = event_enable_count_probe, 4481 .print = event_enable_print, 4482 .init = event_enable_init, 4483 .free = event_enable_free, 4484 }; 4485 4486 static int 4487 event_enable_func(struct trace_array *tr, struct ftrace_hash *hash, 4488 char *glob, char *cmd, char *param, int enabled) 4489 { 4490 struct trace_event_file *file; 4491 struct ftrace_probe_ops *ops; 4492 struct event_probe_data *data; 4493 unsigned long count = -1; 4494 const char *system; 4495 const char *event; 4496 char *number; 4497 bool enable; 4498 int ret; 4499 4500 if (!tr) 4501 return -ENODEV; 4502 4503 /* hash funcs only work with set_ftrace_filter */ 4504 if (!enabled || !param) 4505 return -EINVAL; 4506 4507 system = strsep(¶m, ":"); 4508 if (!param) 4509 return -EINVAL; 4510 4511 event = strsep(¶m, ":"); 4512 4513 guard(mutex)(&event_mutex); 4514 4515 file = find_event_file(tr, system, event); 4516 if (!file) 4517 return -EINVAL; 4518 4519 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0; 4520 4521 if (enable) 4522 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops; 4523 else 4524 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops; 4525 4526 if (glob[0] == '!') 4527 return unregister_ftrace_function_probe_func(glob+1, tr, ops); 4528 4529 if (param) { 4530 number = strsep(¶m, ":"); 4531 4532 if (!strlen(number)) 4533 return -EINVAL; 4534 4535 /* 4536 * We use the callback data field (which is a pointer) 4537 * as our counter. 4538 */ 4539 ret = kstrtoul(number, 0, &count); 4540 if (ret) 4541 return ret; 4542 } 4543 4544 /* Don't let event modules unload while probe registered */ 4545 ret = trace_event_try_get_ref(file->event_call); 4546 if (!ret) 4547 return -EBUSY; 4548 4549 ret = __ftrace_event_enable_disable(file, 1, 1); 4550 if (ret < 0) 4551 goto out_put; 4552 4553 ret = -ENOMEM; 4554 data = kzalloc_obj(*data); 4555 if (!data) 4556 goto out_put; 4557 4558 data->enable = enable; 4559 data->count = count; 4560 data->file = file; 4561 4562 ret = register_ftrace_function_probe(glob, tr, ops, data); 4563 /* 4564 * The above returns on success the # of functions enabled, 4565 * but if it didn't find any functions it returns zero. 4566 * Consider no functions a failure too. 4567 */ 4568 4569 /* Just return zero, not the number of enabled functions */ 4570 if (ret > 0) 4571 return 0; 4572 4573 kfree(data); 4574 4575 if (!ret) 4576 ret = -ENOENT; 4577 4578 __ftrace_event_enable_disable(file, 0, 1); 4579 out_put: 4580 trace_event_put_ref(file->event_call); 4581 return ret; 4582 } 4583 4584 static struct ftrace_func_command event_enable_cmd = { 4585 .name = ENABLE_EVENT_STR, 4586 .func = event_enable_func, 4587 }; 4588 4589 static struct ftrace_func_command event_disable_cmd = { 4590 .name = DISABLE_EVENT_STR, 4591 .func = event_enable_func, 4592 }; 4593 4594 static __init int register_event_cmds(void) 4595 { 4596 int ret; 4597 4598 ret = register_ftrace_command(&event_enable_cmd); 4599 if (WARN_ON(ret < 0)) 4600 return ret; 4601 ret = register_ftrace_command(&event_disable_cmd); 4602 if (WARN_ON(ret < 0)) 4603 unregister_ftrace_command(&event_enable_cmd); 4604 return ret; 4605 } 4606 #else 4607 static inline int register_event_cmds(void) { return 0; } 4608 #endif /* CONFIG_DYNAMIC_FTRACE */ 4609 4610 /* 4611 * The top level array and trace arrays created by boot-time tracing 4612 * have already had its trace_event_file descriptors created in order 4613 * to allow for early events to be recorded. 4614 * This function is called after the tracefs has been initialized, 4615 * and we now have to create the files associated to the events. 4616 */ 4617 static void __trace_early_add_event_dirs(struct trace_array *tr) 4618 { 4619 struct trace_event_file *file; 4620 int ret; 4621 4622 4623 list_for_each_entry(file, &tr->events, list) { 4624 ret = event_create_dir(tr->event_dir, file); 4625 if (ret < 0) 4626 pr_warn("Could not create directory for event %s\n", 4627 trace_event_name(file->event_call)); 4628 } 4629 } 4630 4631 /* 4632 * For early boot up, the top trace array and the trace arrays created 4633 * by boot-time tracing require to have a list of events that can be 4634 * enabled. This must be done before the filesystem is set up in order 4635 * to allow events to be traced early. 4636 */ 4637 void __trace_early_add_events(struct trace_array *tr) 4638 { 4639 struct trace_event_call *call; 4640 int ret; 4641 4642 list_for_each_entry(call, &ftrace_events, list) { 4643 /* Early boot up should not have any modules loaded */ 4644 if (!(call->flags & TRACE_EVENT_FL_DYNAMIC) && 4645 WARN_ON_ONCE(call->module)) 4646 continue; 4647 4648 ret = __trace_early_add_new_event(call, tr); 4649 if (ret < 0) 4650 pr_warn("Could not create early event %s\n", 4651 trace_event_name(call)); 4652 } 4653 } 4654 4655 /* Remove the event directory structure for a trace directory. */ 4656 static void 4657 __trace_remove_event_dirs(struct trace_array *tr) 4658 { 4659 struct trace_event_file *file, *next; 4660 4661 list_for_each_entry_safe(file, next, &tr->events, list) 4662 remove_event_file_dir(file); 4663 } 4664 4665 static void __add_event_to_tracers(struct trace_event_call *call) 4666 { 4667 struct trace_array *tr; 4668 4669 list_for_each_entry(tr, &ftrace_trace_arrays, list) 4670 __trace_add_new_event(call, tr); 4671 } 4672 4673 extern struct trace_event_call *__start_ftrace_events[]; 4674 extern struct trace_event_call *__stop_ftrace_events[]; 4675 4676 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata; 4677 static struct seq_buf bootup_event_seq __initdata = { 4678 .buffer = bootup_event_buf, 4679 .size = sizeof(bootup_event_buf), 4680 }; 4681 4682 static __init int setup_trace_event(char *str) 4683 { 4684 if (seq_buf_used(&bootup_event_seq) > 0) 4685 seq_buf_puts(&bootup_event_seq, ","); 4686 4687 seq_buf_puts(&bootup_event_seq, str); 4688 4689 if (seq_buf_has_overflowed(&bootup_event_seq)) 4690 return -ENOMEM; 4691 4692 trace_set_ring_buffer_expanded(NULL); 4693 disable_tracing_selftest("running event tracing"); 4694 4695 return 1; 4696 } 4697 __setup("trace_event=", setup_trace_event); 4698 4699 static int events_callback(const char *name, umode_t *mode, void **data, 4700 const struct file_operations **fops) 4701 { 4702 if (strcmp(name, "enable") == 0) { 4703 *mode = TRACE_MODE_WRITE; 4704 *fops = &ftrace_tr_enable_fops; 4705 return 1; 4706 } 4707 4708 if (strcmp(name, "header_page") == 0) { 4709 *mode = TRACE_MODE_READ; 4710 *fops = &ftrace_show_header_page_fops; 4711 4712 } else if (strcmp(name, "header_event") == 0) { 4713 *mode = TRACE_MODE_READ; 4714 *fops = &ftrace_show_header_event_fops; 4715 } else 4716 return 0; 4717 4718 return 1; 4719 } 4720 4721 /* Expects to have event_mutex held when called */ 4722 static int 4723 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr) 4724 { 4725 struct eventfs_inode *e_events; 4726 struct dentry *entry; 4727 int nr_entries; 4728 static struct eventfs_entry events_entries[] = { 4729 { 4730 .name = "header_page", 4731 .callback = events_callback, 4732 }, 4733 { 4734 .name = "header_event", 4735 .callback = events_callback, 4736 }, 4737 #define NR_RO_TOP_ENTRIES 2 4738 /* Readonly files must be above this line and counted by NR_RO_TOP_ENTRIES. */ 4739 { 4740 .name = "enable", 4741 .callback = events_callback, 4742 }, 4743 }; 4744 4745 if (!trace_array_is_readonly(tr)) { 4746 entry = trace_create_file("set_event", TRACE_MODE_WRITE, parent, 4747 tr, &ftrace_set_event_fops); 4748 if (!entry) 4749 return -ENOMEM; 4750 4751 /* There are not as crucial, just warn if they are not created */ 4752 trace_create_file("show_event_filters", TRACE_MODE_READ, parent, tr, 4753 &ftrace_show_event_filters_fops); 4754 4755 trace_create_file("show_event_triggers", TRACE_MODE_READ, parent, tr, 4756 &ftrace_show_event_triggers_fops); 4757 4758 trace_create_file("set_event_pid", TRACE_MODE_WRITE, parent, 4759 tr, &ftrace_set_event_pid_fops); 4760 4761 trace_create_file("set_event_notrace_pid", 4762 TRACE_MODE_WRITE, parent, tr, 4763 &ftrace_set_event_notrace_pid_fops); 4764 nr_entries = ARRAY_SIZE(events_entries); 4765 } else { 4766 nr_entries = NR_RO_TOP_ENTRIES; 4767 } 4768 4769 e_events = eventfs_create_events_dir("events", parent, events_entries, 4770 nr_entries, tr); 4771 if (IS_ERR(e_events)) { 4772 pr_warn("Could not create tracefs 'events' directory\n"); 4773 return -ENOMEM; 4774 } 4775 4776 tr->event_dir = e_events; 4777 4778 return 0; 4779 } 4780 4781 /** 4782 * event_trace_add_tracer - add a instance of a trace_array to events 4783 * @parent: The parent dentry to place the files/directories for events in 4784 * @tr: The trace array associated with these events 4785 * 4786 * When a new instance is created, it needs to set up its events 4787 * directory, as well as other files associated with events. It also 4788 * creates the event hierarchy in the @parent/events directory. 4789 * 4790 * Returns 0 on success. 4791 * 4792 * Must be called with event_mutex held. 4793 */ 4794 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr) 4795 { 4796 int ret; 4797 4798 lockdep_assert_held(&event_mutex); 4799 4800 ret = create_event_toplevel_files(parent, tr); 4801 if (ret) 4802 goto out; 4803 4804 down_write(&trace_event_sem); 4805 /* If tr already has the event list, it is initialized in early boot. */ 4806 if (unlikely(!list_empty(&tr->events))) 4807 __trace_early_add_event_dirs(tr); 4808 else 4809 __trace_add_event_dirs(tr); 4810 up_write(&trace_event_sem); 4811 4812 out: 4813 return ret; 4814 } 4815 4816 /* 4817 * The top trace array already had its file descriptors created. 4818 * Now the files themselves need to be created. 4819 */ 4820 static __init int 4821 early_event_add_tracer(struct dentry *parent, struct trace_array *tr) 4822 { 4823 int ret; 4824 4825 guard(mutex)(&event_mutex); 4826 4827 ret = create_event_toplevel_files(parent, tr); 4828 if (ret) 4829 return ret; 4830 4831 down_write(&trace_event_sem); 4832 __trace_early_add_event_dirs(tr); 4833 up_write(&trace_event_sem); 4834 4835 return 0; 4836 } 4837 4838 /* Must be called with event_mutex held */ 4839 int event_trace_del_tracer(struct trace_array *tr) 4840 { 4841 lockdep_assert_held(&event_mutex); 4842 4843 /* Disable any event triggers and associated soft-disabled events */ 4844 clear_event_triggers(tr); 4845 4846 /* Clear the pid list */ 4847 __ftrace_clear_event_pids(tr, TRACE_PIDS | TRACE_NO_PIDS); 4848 4849 /* Disable any running events */ 4850 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0, NULL, NULL); 4851 4852 /* Make sure no more events are being executed */ 4853 tracepoint_synchronize_unregister(); 4854 4855 down_write(&trace_event_sem); 4856 __trace_remove_event_dirs(tr); 4857 eventfs_remove_events_dir(tr->event_dir); 4858 up_write(&trace_event_sem); 4859 4860 tr->event_dir = NULL; 4861 4862 return 0; 4863 } 4864 4865 static __init int event_trace_memsetup(void) 4866 { 4867 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC); 4868 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC); 4869 return 0; 4870 } 4871 4872 /* 4873 * Helper function to enable or disable a comma-separated list of events 4874 * from the bootup buffer. 4875 */ 4876 static __init void __early_set_events(struct trace_array *tr, char *buf, bool enable) 4877 { 4878 char *token; 4879 4880 while ((token = strsep(&buf, ","))) { 4881 if (*token) { 4882 if (enable) { 4883 if (ftrace_set_clr_event(tr, token, 1, true)) 4884 pr_warn("Failed to enable trace event: %s\n", token); 4885 } else { 4886 ftrace_set_clr_event(tr, token, 0, true); 4887 } 4888 } 4889 4890 /* Put back the comma to allow this to be called again */ 4891 if (buf) 4892 *(buf - 1) = ','; 4893 } 4894 } 4895 4896 /** 4897 * early_enable_events - enable events from the bootup buffer 4898 * @tr: The trace array to enable the events in 4899 * @buf: The buffer containing the comma separated list of events 4900 * @disable_first: If true, disable all events in @buf before enabling them 4901 * 4902 * This function enables events from the bootup buffer. If @disable_first 4903 * is true, it will first disable all events in the buffer before enabling 4904 * them. 4905 * 4906 * For syscall events, which rely on a global refcount to register the 4907 * SYSCALL_WORK_SYSCALL_TRACEPOINT flag (especially for pid 1), we must 4908 * ensure the refcount hits zero before re-enabling them. A simple 4909 * "disable then enable" per-event is not enough if multiple syscalls are 4910 * used, as the refcount will stay above zero. Thus, we need a two-phase 4911 * approach: disable all, then enable all. 4912 */ 4913 __init void 4914 early_enable_events(struct trace_array *tr, char *buf, bool disable_first) 4915 { 4916 if (disable_first) 4917 __early_set_events(tr, buf, false); 4918 4919 __early_set_events(tr, buf, true); 4920 } 4921 4922 static __init int event_trace_enable(void) 4923 { 4924 struct trace_array *tr = top_trace_array(); 4925 struct trace_event_call **iter, *call; 4926 int ret; 4927 4928 if (!tr) 4929 return -ENODEV; 4930 4931 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) { 4932 4933 call = *iter; 4934 ret = event_init(call); 4935 if (!ret) 4936 list_add(&call->list, &ftrace_events); 4937 } 4938 4939 register_trigger_cmds(); 4940 4941 /* 4942 * We need the top trace array to have a working set of trace 4943 * points at early init, before the debug files and directories 4944 * are created. Create the file entries now, and attach them 4945 * to the actual file dentries later. 4946 */ 4947 __trace_early_add_events(tr); 4948 4949 seq_buf_terminate(&bootup_event_seq); 4950 early_enable_events(tr, bootup_event_buf, false); 4951 4952 trace_printk_start_comm(); 4953 4954 register_event_cmds(); 4955 4956 4957 return 0; 4958 } 4959 4960 /* 4961 * event_trace_enable() is called from trace_event_init() first to 4962 * initialize events and perhaps start any events that are on the 4963 * command line. Unfortunately, there are some events that will not 4964 * start this early, like the system call tracepoints that need 4965 * to set the %SYSCALL_WORK_SYSCALL_TRACEPOINT flag of pid 1. But 4966 * event_trace_enable() is called before pid 1 starts, and this flag 4967 * is never set, making the syscall tracepoint never get reached, but 4968 * the event is enabled regardless (and not doing anything). 4969 */ 4970 static __init int event_trace_enable_again(void) 4971 { 4972 struct trace_array *tr; 4973 4974 tr = top_trace_array(); 4975 if (!tr) 4976 return -ENODEV; 4977 4978 seq_buf_terminate(&bootup_event_seq); 4979 early_enable_events(tr, bootup_event_buf, true); 4980 4981 return 0; 4982 } 4983 4984 early_initcall(event_trace_enable_again); 4985 4986 /* Init fields which doesn't related to the tracefs */ 4987 static __init int event_trace_init_fields(void) 4988 { 4989 if (trace_define_generic_fields()) 4990 pr_warn("tracing: Failed to allocated generic fields"); 4991 4992 if (trace_define_common_fields()) 4993 pr_warn("tracing: Failed to allocate common fields"); 4994 4995 return 0; 4996 } 4997 4998 __init int event_trace_init(void) 4999 { 5000 struct trace_array *tr; 5001 int ret; 5002 5003 tr = top_trace_array(); 5004 if (!tr) 5005 return -ENODEV; 5006 5007 trace_create_file("available_events", TRACE_MODE_READ, 5008 NULL, tr, &ftrace_avail_fops); 5009 5010 ret = early_event_add_tracer(NULL, tr); 5011 if (ret) 5012 return ret; 5013 5014 #ifdef CONFIG_MODULES 5015 ret = register_module_notifier(&trace_module_nb); 5016 if (ret) 5017 pr_warn("Failed to register trace events module notifier\n"); 5018 #endif 5019 5020 eventdir_initialized = true; 5021 5022 return 0; 5023 } 5024 5025 void __init trace_event_init(void) 5026 { 5027 event_trace_memsetup(); 5028 init_ftrace_syscalls(); 5029 event_trace_init_fields(); 5030 event_trace_enable(); 5031 } 5032 5033 #ifdef CONFIG_EVENT_TRACE_STARTUP_TEST 5034 5035 static DEFINE_SPINLOCK(test_spinlock); 5036 static DEFINE_SPINLOCK(test_spinlock_irq); 5037 static DEFINE_MUTEX(test_mutex); 5038 5039 static __init void test_work(struct work_struct *dummy) 5040 { 5041 spin_lock(&test_spinlock); 5042 spin_lock_irq(&test_spinlock_irq); 5043 udelay(1); 5044 spin_unlock_irq(&test_spinlock_irq); 5045 spin_unlock(&test_spinlock); 5046 5047 mutex_lock(&test_mutex); 5048 msleep(1); 5049 mutex_unlock(&test_mutex); 5050 } 5051 5052 static __init int event_test_thread(void *unused) 5053 { 5054 void *test_malloc; 5055 5056 test_malloc = kmalloc(1234, GFP_KERNEL); 5057 if (!test_malloc) 5058 pr_info("failed to kmalloc\n"); 5059 5060 schedule_on_each_cpu(test_work); 5061 5062 kfree(test_malloc); 5063 5064 set_current_state(TASK_INTERRUPTIBLE); 5065 while (!kthread_should_stop()) { 5066 schedule(); 5067 set_current_state(TASK_INTERRUPTIBLE); 5068 } 5069 __set_current_state(TASK_RUNNING); 5070 5071 return 0; 5072 } 5073 5074 /* 5075 * Do various things that may trigger events. 5076 */ 5077 static __init void event_test_stuff(void) 5078 { 5079 struct task_struct *test_thread; 5080 5081 test_thread = kthread_run(event_test_thread, NULL, "test-events"); 5082 if (WARN_ON(IS_ERR(test_thread))) 5083 return; 5084 msleep(1); 5085 kthread_stop(test_thread); 5086 } 5087 5088 /* 5089 * For every trace event defined, we will test each trace point separately, 5090 * and then by groups, and finally all trace points. 5091 */ 5092 static __init void event_trace_self_tests(void) 5093 { 5094 struct trace_subsystem_dir *dir; 5095 struct trace_event_file *file; 5096 struct trace_event_call *call; 5097 struct event_subsystem *system; 5098 struct trace_array *tr; 5099 int ret; 5100 5101 tr = top_trace_array(); 5102 if (!tr) 5103 return; 5104 5105 pr_info("Running tests on trace events:\n"); 5106 5107 list_for_each_entry(file, &tr->events, list) { 5108 5109 call = file->event_call; 5110 5111 /* Only test those that have a probe */ 5112 if (!call->class || !call->class->probe) 5113 continue; 5114 5115 /* 5116 * Testing syscall events here is pretty useless, but 5117 * we still do it if configured. But this is time consuming. 5118 * What we really need is a user thread to perform the 5119 * syscalls as we test. 5120 */ 5121 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS 5122 if (call->class->system && 5123 strcmp(call->class->system, "syscalls") == 0) 5124 continue; 5125 #endif 5126 5127 pr_info("Testing event %s: ", trace_event_name(call)); 5128 5129 /* 5130 * If an event is already enabled, someone is using 5131 * it and the self test should not be on. 5132 */ 5133 if (file->flags & EVENT_FILE_FL_ENABLED) { 5134 pr_warn("Enabled event during self test!\n"); 5135 WARN_ON_ONCE(1); 5136 continue; 5137 } 5138 5139 ftrace_event_enable_disable(file, 1); 5140 event_test_stuff(); 5141 ftrace_event_enable_disable(file, 0); 5142 5143 pr_cont("OK\n"); 5144 } 5145 5146 /* Now test at the sub system level */ 5147 5148 pr_info("Running tests on trace event systems:\n"); 5149 5150 list_for_each_entry(dir, &tr->systems, list) { 5151 5152 system = dir->subsystem; 5153 5154 /* the ftrace system is special, skip it */ 5155 if (strcmp(system->name, "ftrace") == 0) 5156 continue; 5157 5158 pr_info("Testing event system %s: ", system->name); 5159 5160 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1, NULL, NULL); 5161 if (WARN_ON_ONCE(ret)) { 5162 pr_warn("error enabling system %s\n", 5163 system->name); 5164 continue; 5165 } 5166 5167 event_test_stuff(); 5168 5169 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0, NULL, NULL); 5170 if (WARN_ON_ONCE(ret)) { 5171 pr_warn("error disabling system %s\n", 5172 system->name); 5173 continue; 5174 } 5175 5176 pr_cont("OK\n"); 5177 } 5178 5179 /* Test with all events enabled */ 5180 5181 pr_info("Running tests on all trace events:\n"); 5182 pr_info("Testing all events: "); 5183 5184 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1, NULL, NULL); 5185 if (WARN_ON_ONCE(ret)) { 5186 pr_warn("error enabling all events\n"); 5187 return; 5188 } 5189 5190 event_test_stuff(); 5191 5192 /* reset sysname */ 5193 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0, NULL, NULL); 5194 if (WARN_ON_ONCE(ret)) { 5195 pr_warn("error disabling all events\n"); 5196 return; 5197 } 5198 5199 pr_cont("OK\n"); 5200 } 5201 5202 #ifdef CONFIG_FUNCTION_TRACER 5203 5204 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable); 5205 5206 static struct trace_event_file event_trace_file __initdata; 5207 5208 static void notrace __init 5209 function_test_events_call(unsigned long ip, unsigned long parent_ip, 5210 struct ftrace_ops *op, struct ftrace_regs *regs) 5211 { 5212 struct trace_buffer *buffer; 5213 struct ring_buffer_event *event; 5214 struct ftrace_entry *entry; 5215 unsigned int trace_ctx; 5216 long disabled; 5217 int cpu; 5218 5219 trace_ctx = tracing_gen_ctx(); 5220 preempt_disable_notrace(); 5221 cpu = raw_smp_processor_id(); 5222 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu)); 5223 5224 if (disabled != 1) 5225 goto out; 5226 5227 event = trace_event_buffer_lock_reserve(&buffer, &event_trace_file, 5228 TRACE_FN, sizeof(*entry), 5229 trace_ctx); 5230 if (!event) 5231 goto out; 5232 entry = ring_buffer_event_data(event); 5233 entry->ip = ip; 5234 entry->parent_ip = parent_ip; 5235 5236 event_trigger_unlock_commit(&event_trace_file, buffer, event, 5237 entry, trace_ctx); 5238 out: 5239 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu)); 5240 preempt_enable_notrace(); 5241 } 5242 5243 static struct ftrace_ops trace_ops __initdata = 5244 { 5245 .func = function_test_events_call, 5246 }; 5247 5248 static __init void event_trace_self_test_with_function(void) 5249 { 5250 int ret; 5251 5252 event_trace_file.tr = top_trace_array(); 5253 if (WARN_ON(!event_trace_file.tr)) 5254 return; 5255 5256 ret = register_ftrace_function(&trace_ops); 5257 if (WARN_ON(ret < 0)) { 5258 pr_info("Failed to enable function tracer for event tests\n"); 5259 return; 5260 } 5261 pr_info("Running tests again, along with the function tracer\n"); 5262 event_trace_self_tests(); 5263 unregister_ftrace_function(&trace_ops); 5264 } 5265 #else 5266 static __init void event_trace_self_test_with_function(void) 5267 { 5268 } 5269 #endif 5270 5271 static __init int event_trace_self_tests_init(void) 5272 { 5273 if (!tracing_selftest_disabled) { 5274 event_trace_self_tests(); 5275 event_trace_self_test_with_function(); 5276 } 5277 5278 return 0; 5279 } 5280 5281 late_initcall(event_trace_self_tests_init); 5282 5283 #endif 5284