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