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 bool event_in_systems(struct trace_event_call *call, 3060 const char *systems) 3061 { 3062 const char *system; 3063 const char *p; 3064 3065 if (!systems) 3066 return true; 3067 3068 system = call->class->system; 3069 p = strstr(systems, system); 3070 if (!p) 3071 return false; 3072 3073 if (p != systems && !isspace(*(p - 1)) && *(p - 1) != ',') 3074 return false; 3075 3076 p += strlen(system); 3077 return !*p || isspace(*p) || *p == ','; 3078 } 3079 3080 #ifdef CONFIG_HIST_TRIGGERS 3081 /* 3082 * Wake up waiter on the hist_poll_wq from irq_work because the hist trigger 3083 * may happen in any context. 3084 */ 3085 static void hist_poll_event_irq_work(struct irq_work *work) 3086 { 3087 wake_up_all(&hist_poll_wq); 3088 } 3089 3090 DEFINE_IRQ_WORK(hist_poll_work, hist_poll_event_irq_work); 3091 DECLARE_WAIT_QUEUE_HEAD(hist_poll_wq); 3092 #endif 3093 3094 static struct trace_event_file * 3095 trace_create_new_event(struct trace_event_call *call, 3096 struct trace_array *tr) 3097 { 3098 struct trace_pid_list *no_pid_list; 3099 struct trace_pid_list *pid_list; 3100 struct trace_event_file *file; 3101 unsigned int first; 3102 3103 if (!event_in_systems(call, tr->system_names)) 3104 return NULL; 3105 3106 file = kmem_cache_alloc(file_cachep, GFP_TRACE); 3107 if (!file) 3108 return ERR_PTR(-ENOMEM); 3109 3110 pid_list = rcu_dereference_protected(tr->filtered_pids, 3111 lockdep_is_held(&event_mutex)); 3112 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids, 3113 lockdep_is_held(&event_mutex)); 3114 3115 if (!trace_pid_list_first(pid_list, &first) || 3116 !trace_pid_list_first(no_pid_list, &first)) 3117 file->flags |= EVENT_FILE_FL_PID_FILTER; 3118 3119 file->event_call = call; 3120 file->tr = tr; 3121 atomic_set(&file->sm_ref, 0); 3122 atomic_set(&file->tm_ref, 0); 3123 INIT_LIST_HEAD(&file->triggers); 3124 list_add(&file->list, &tr->events); 3125 event_file_get(file); 3126 3127 return file; 3128 } 3129 3130 #define MAX_BOOT_TRIGGERS 32 3131 3132 static struct boot_triggers { 3133 const char *event; 3134 char *trigger; 3135 } bootup_triggers[MAX_BOOT_TRIGGERS]; 3136 3137 static char bootup_trigger_buf[COMMAND_LINE_SIZE]; 3138 static int nr_boot_triggers; 3139 3140 static __init int setup_trace_triggers(char *str) 3141 { 3142 char *trigger; 3143 char *buf; 3144 int i; 3145 3146 strscpy(bootup_trigger_buf, str, COMMAND_LINE_SIZE); 3147 ring_buffer_expanded = true; 3148 disable_tracing_selftest("running event triggers"); 3149 3150 buf = bootup_trigger_buf; 3151 for (i = 0; i < MAX_BOOT_TRIGGERS; i++) { 3152 trigger = strsep(&buf, ","); 3153 if (!trigger) 3154 break; 3155 bootup_triggers[i].event = strsep(&trigger, "."); 3156 bootup_triggers[i].trigger = trigger; 3157 if (!bootup_triggers[i].trigger) 3158 break; 3159 } 3160 3161 nr_boot_triggers = i; 3162 return 1; 3163 } 3164 __setup("trace_trigger=", setup_trace_triggers); 3165 3166 /* Add an event to a trace directory */ 3167 static int 3168 __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr) 3169 { 3170 struct trace_event_file *file; 3171 3172 file = trace_create_new_event(call, tr); 3173 /* 3174 * trace_create_new_event() returns ERR_PTR(-ENOMEM) if failed 3175 * allocation, or NULL if the event is not part of the tr->system_names. 3176 * When the event is not part of the tr->system_names, return zero, not 3177 * an error. 3178 */ 3179 if (!file) 3180 return 0; 3181 3182 if (IS_ERR(file)) 3183 return PTR_ERR(file); 3184 3185 if (eventdir_initialized) 3186 return event_create_dir(tr->event_dir, file); 3187 else 3188 return event_define_fields(call); 3189 } 3190 3191 static void trace_early_triggers(struct trace_event_file *file, const char *name) 3192 { 3193 int ret; 3194 int i; 3195 3196 for (i = 0; i < nr_boot_triggers; i++) { 3197 if (strcmp(name, bootup_triggers[i].event)) 3198 continue; 3199 mutex_lock(&event_mutex); 3200 ret = trigger_process_regex(file, bootup_triggers[i].trigger); 3201 mutex_unlock(&event_mutex); 3202 if (ret) 3203 pr_err("Failed to register trigger '%s' on event %s\n", 3204 bootup_triggers[i].trigger, 3205 bootup_triggers[i].event); 3206 } 3207 } 3208 3209 /* 3210 * Just create a descriptor for early init. A descriptor is required 3211 * for enabling events at boot. We want to enable events before 3212 * the filesystem is initialized. 3213 */ 3214 static int 3215 __trace_early_add_new_event(struct trace_event_call *call, 3216 struct trace_array *tr) 3217 { 3218 struct trace_event_file *file; 3219 int ret; 3220 3221 file = trace_create_new_event(call, tr); 3222 /* 3223 * trace_create_new_event() returns ERR_PTR(-ENOMEM) if failed 3224 * allocation, or NULL if the event is not part of the tr->system_names. 3225 * When the event is not part of the tr->system_names, return zero, not 3226 * an error. 3227 */ 3228 if (!file) 3229 return 0; 3230 3231 if (IS_ERR(file)) 3232 return PTR_ERR(file); 3233 3234 ret = event_define_fields(call); 3235 if (ret) 3236 return ret; 3237 3238 trace_early_triggers(file, trace_event_name(call)); 3239 3240 return 0; 3241 } 3242 3243 struct ftrace_module_file_ops; 3244 static void __add_event_to_tracers(struct trace_event_call *call); 3245 3246 /* Add an additional event_call dynamically */ 3247 int trace_add_event_call(struct trace_event_call *call) 3248 { 3249 int ret; 3250 lockdep_assert_held(&event_mutex); 3251 3252 mutex_lock(&trace_types_lock); 3253 3254 ret = __register_event(call, NULL); 3255 if (ret >= 0) 3256 __add_event_to_tracers(call); 3257 3258 mutex_unlock(&trace_types_lock); 3259 return ret; 3260 } 3261 EXPORT_SYMBOL_GPL(trace_add_event_call); 3262 3263 /* 3264 * Must be called under locking of trace_types_lock, event_mutex and 3265 * trace_event_sem. 3266 */ 3267 static void __trace_remove_event_call(struct trace_event_call *call) 3268 { 3269 event_remove(call); 3270 trace_destroy_fields(call); 3271 free_event_filter(call->filter); 3272 call->filter = NULL; 3273 } 3274 3275 static int probe_remove_event_call(struct trace_event_call *call) 3276 { 3277 struct trace_array *tr; 3278 struct trace_event_file *file; 3279 3280 #ifdef CONFIG_PERF_EVENTS 3281 if (call->perf_refcount) 3282 return -EBUSY; 3283 #endif 3284 do_for_each_event_file(tr, file) { 3285 if (file->event_call != call) 3286 continue; 3287 /* 3288 * We can't rely on ftrace_event_enable_disable(enable => 0) 3289 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress 3290 * TRACE_REG_UNREGISTER. 3291 */ 3292 if (file->flags & EVENT_FILE_FL_ENABLED) 3293 goto busy; 3294 3295 if (file->flags & EVENT_FILE_FL_WAS_ENABLED) 3296 tr->clear_trace = true; 3297 /* 3298 * The do_for_each_event_file_safe() is 3299 * a double loop. After finding the call for this 3300 * trace_array, we use break to jump to the next 3301 * trace_array. 3302 */ 3303 break; 3304 } while_for_each_event_file(); 3305 3306 __trace_remove_event_call(call); 3307 3308 return 0; 3309 busy: 3310 /* No need to clear the trace now */ 3311 list_for_each_entry(tr, &ftrace_trace_arrays, list) { 3312 tr->clear_trace = false; 3313 } 3314 return -EBUSY; 3315 } 3316 3317 /* Remove an event_call */ 3318 int trace_remove_event_call(struct trace_event_call *call) 3319 { 3320 int ret; 3321 3322 lockdep_assert_held(&event_mutex); 3323 3324 mutex_lock(&trace_types_lock); 3325 down_write(&trace_event_sem); 3326 ret = probe_remove_event_call(call); 3327 up_write(&trace_event_sem); 3328 mutex_unlock(&trace_types_lock); 3329 3330 return ret; 3331 } 3332 EXPORT_SYMBOL_GPL(trace_remove_event_call); 3333 3334 #define for_each_event(event, start, end) \ 3335 for (event = start; \ 3336 (unsigned long)event < (unsigned long)end; \ 3337 event++) 3338 3339 #ifdef CONFIG_MODULES 3340 3341 static void trace_module_add_events(struct module *mod) 3342 { 3343 struct trace_event_call **call, **start, **end; 3344 3345 if (!mod->num_trace_events) 3346 return; 3347 3348 /* Don't add infrastructure for mods without tracepoints */ 3349 if (trace_module_has_bad_taint(mod)) { 3350 pr_err("%s: module has bad taint, not creating trace events\n", 3351 mod->name); 3352 return; 3353 } 3354 3355 start = mod->trace_events; 3356 end = mod->trace_events + mod->num_trace_events; 3357 3358 for_each_event(call, start, end) { 3359 __register_event(*call, mod); 3360 __add_event_to_tracers(*call); 3361 } 3362 } 3363 3364 static void trace_module_remove_events(struct module *mod) 3365 { 3366 struct trace_event_call *call, *p; 3367 struct module_string *modstr, *m; 3368 3369 down_write(&trace_event_sem); 3370 list_for_each_entry_safe(call, p, &ftrace_events, list) { 3371 if ((call->flags & TRACE_EVENT_FL_DYNAMIC) || !call->module) 3372 continue; 3373 if (call->module == mod) 3374 __trace_remove_event_call(call); 3375 } 3376 /* Check for any strings allocade for this module */ 3377 list_for_each_entry_safe(modstr, m, &module_strings, next) { 3378 if (modstr->module != mod) 3379 continue; 3380 list_del(&modstr->next); 3381 kfree(modstr->str); 3382 kfree(modstr); 3383 } 3384 up_write(&trace_event_sem); 3385 3386 /* 3387 * It is safest to reset the ring buffer if the module being unloaded 3388 * registered any events that were used. The only worry is if 3389 * a new module gets loaded, and takes on the same id as the events 3390 * of this module. When printing out the buffer, traced events left 3391 * over from this module may be passed to the new module events and 3392 * unexpected results may occur. 3393 */ 3394 tracing_reset_all_online_cpus_unlocked(); 3395 } 3396 3397 static int trace_module_notify(struct notifier_block *self, 3398 unsigned long val, void *data) 3399 { 3400 struct module *mod = data; 3401 3402 mutex_lock(&event_mutex); 3403 mutex_lock(&trace_types_lock); 3404 switch (val) { 3405 case MODULE_STATE_COMING: 3406 trace_module_add_events(mod); 3407 break; 3408 case MODULE_STATE_GOING: 3409 trace_module_remove_events(mod); 3410 break; 3411 } 3412 mutex_unlock(&trace_types_lock); 3413 mutex_unlock(&event_mutex); 3414 3415 return NOTIFY_OK; 3416 } 3417 3418 static struct notifier_block trace_module_nb = { 3419 .notifier_call = trace_module_notify, 3420 .priority = 1, /* higher than trace.c module notify */ 3421 }; 3422 #endif /* CONFIG_MODULES */ 3423 3424 /* Create a new event directory structure for a trace directory. */ 3425 static void 3426 __trace_add_event_dirs(struct trace_array *tr) 3427 { 3428 struct trace_event_call *call; 3429 int ret; 3430 3431 list_for_each_entry(call, &ftrace_events, list) { 3432 ret = __trace_add_new_event(call, tr); 3433 if (ret < 0) 3434 pr_warn("Could not create directory for event %s\n", 3435 trace_event_name(call)); 3436 } 3437 } 3438 3439 /* Returns any file that matches the system and event */ 3440 struct trace_event_file * 3441 __find_event_file(struct trace_array *tr, const char *system, const char *event) 3442 { 3443 struct trace_event_file *file; 3444 struct trace_event_call *call; 3445 const char *name; 3446 3447 list_for_each_entry(file, &tr->events, list) { 3448 3449 call = file->event_call; 3450 name = trace_event_name(call); 3451 3452 if (!name || !call->class) 3453 continue; 3454 3455 if (strcmp(event, name) == 0 && 3456 strcmp(system, call->class->system) == 0) 3457 return file; 3458 } 3459 return NULL; 3460 } 3461 3462 /* Returns valid trace event files that match system and event */ 3463 struct trace_event_file * 3464 find_event_file(struct trace_array *tr, const char *system, const char *event) 3465 { 3466 struct trace_event_file *file; 3467 3468 file = __find_event_file(tr, system, event); 3469 if (!file || !file->event_call->class->reg || 3470 file->event_call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) 3471 return NULL; 3472 3473 return file; 3474 } 3475 3476 /** 3477 * trace_get_event_file - Find and return a trace event file 3478 * @instance: The name of the trace instance containing the event 3479 * @system: The name of the system containing the event 3480 * @event: The name of the event 3481 * 3482 * Return a trace event file given the trace instance name, trace 3483 * system, and trace event name. If the instance name is NULL, it 3484 * refers to the top-level trace array. 3485 * 3486 * This function will look it up and return it if found, after calling 3487 * trace_array_get() to prevent the instance from going away, and 3488 * increment the event's module refcount to prevent it from being 3489 * removed. 3490 * 3491 * To release the file, call trace_put_event_file(), which will call 3492 * trace_array_put() and decrement the event's module refcount. 3493 * 3494 * Return: The trace event on success, ERR_PTR otherwise. 3495 */ 3496 struct trace_event_file *trace_get_event_file(const char *instance, 3497 const char *system, 3498 const char *event) 3499 { 3500 struct trace_array *tr = top_trace_array(); 3501 struct trace_event_file *file = NULL; 3502 int ret = -EINVAL; 3503 3504 if (instance) { 3505 tr = trace_array_find_get(instance); 3506 if (!tr) 3507 return ERR_PTR(-ENOENT); 3508 } else { 3509 ret = trace_array_get(tr); 3510 if (ret) 3511 return ERR_PTR(ret); 3512 } 3513 3514 mutex_lock(&event_mutex); 3515 3516 file = find_event_file(tr, system, event); 3517 if (!file) { 3518 trace_array_put(tr); 3519 ret = -EINVAL; 3520 goto out; 3521 } 3522 3523 /* Don't let event modules unload while in use */ 3524 ret = trace_event_try_get_ref(file->event_call); 3525 if (!ret) { 3526 trace_array_put(tr); 3527 ret = -EBUSY; 3528 goto out; 3529 } 3530 3531 ret = 0; 3532 out: 3533 mutex_unlock(&event_mutex); 3534 3535 if (ret) 3536 file = ERR_PTR(ret); 3537 3538 return file; 3539 } 3540 EXPORT_SYMBOL_GPL(trace_get_event_file); 3541 3542 /** 3543 * trace_put_event_file - Release a file from trace_get_event_file() 3544 * @file: The trace event file 3545 * 3546 * If a file was retrieved using trace_get_event_file(), this should 3547 * be called when it's no longer needed. It will cancel the previous 3548 * trace_array_get() called by that function, and decrement the 3549 * event's module refcount. 3550 */ 3551 void trace_put_event_file(struct trace_event_file *file) 3552 { 3553 mutex_lock(&event_mutex); 3554 trace_event_put_ref(file->event_call); 3555 mutex_unlock(&event_mutex); 3556 3557 trace_array_put(file->tr); 3558 } 3559 EXPORT_SYMBOL_GPL(trace_put_event_file); 3560 3561 #ifdef CONFIG_DYNAMIC_FTRACE 3562 3563 /* Avoid typos */ 3564 #define ENABLE_EVENT_STR "enable_event" 3565 #define DISABLE_EVENT_STR "disable_event" 3566 3567 struct event_probe_data { 3568 struct trace_event_file *file; 3569 unsigned long count; 3570 int ref; 3571 bool enable; 3572 }; 3573 3574 static void update_event_probe(struct event_probe_data *data) 3575 { 3576 if (data->enable) 3577 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags); 3578 else 3579 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags); 3580 } 3581 3582 static void 3583 event_enable_probe(unsigned long ip, unsigned long parent_ip, 3584 struct trace_array *tr, struct ftrace_probe_ops *ops, 3585 void *data) 3586 { 3587 struct ftrace_func_mapper *mapper = data; 3588 struct event_probe_data *edata; 3589 void **pdata; 3590 3591 pdata = ftrace_func_mapper_find_ip(mapper, ip); 3592 if (!pdata || !*pdata) 3593 return; 3594 3595 edata = *pdata; 3596 update_event_probe(edata); 3597 } 3598 3599 static void 3600 event_enable_count_probe(unsigned long ip, unsigned long parent_ip, 3601 struct trace_array *tr, struct ftrace_probe_ops *ops, 3602 void *data) 3603 { 3604 struct ftrace_func_mapper *mapper = data; 3605 struct event_probe_data *edata; 3606 void **pdata; 3607 3608 pdata = ftrace_func_mapper_find_ip(mapper, ip); 3609 if (!pdata || !*pdata) 3610 return; 3611 3612 edata = *pdata; 3613 3614 if (!edata->count) 3615 return; 3616 3617 /* Skip if the event is in a state we want to switch to */ 3618 if (edata->enable == !(edata->file->flags & EVENT_FILE_FL_SOFT_DISABLED)) 3619 return; 3620 3621 if (edata->count != -1) 3622 (edata->count)--; 3623 3624 update_event_probe(edata); 3625 } 3626 3627 static int 3628 event_enable_print(struct seq_file *m, unsigned long ip, 3629 struct ftrace_probe_ops *ops, void *data) 3630 { 3631 struct ftrace_func_mapper *mapper = data; 3632 struct event_probe_data *edata; 3633 void **pdata; 3634 3635 pdata = ftrace_func_mapper_find_ip(mapper, ip); 3636 3637 if (WARN_ON_ONCE(!pdata || !*pdata)) 3638 return 0; 3639 3640 edata = *pdata; 3641 3642 seq_printf(m, "%ps:", (void *)ip); 3643 3644 seq_printf(m, "%s:%s:%s", 3645 edata->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR, 3646 edata->file->event_call->class->system, 3647 trace_event_name(edata->file->event_call)); 3648 3649 if (edata->count == -1) 3650 seq_puts(m, ":unlimited\n"); 3651 else 3652 seq_printf(m, ":count=%ld\n", edata->count); 3653 3654 return 0; 3655 } 3656 3657 static int 3658 event_enable_init(struct ftrace_probe_ops *ops, struct trace_array *tr, 3659 unsigned long ip, void *init_data, void **data) 3660 { 3661 struct ftrace_func_mapper *mapper = *data; 3662 struct event_probe_data *edata = init_data; 3663 int ret; 3664 3665 if (!mapper) { 3666 mapper = allocate_ftrace_func_mapper(); 3667 if (!mapper) 3668 return -ENODEV; 3669 *data = mapper; 3670 } 3671 3672 ret = ftrace_func_mapper_add_ip(mapper, ip, edata); 3673 if (ret < 0) 3674 return ret; 3675 3676 edata->ref++; 3677 3678 return 0; 3679 } 3680 3681 static int free_probe_data(void *data) 3682 { 3683 struct event_probe_data *edata = data; 3684 3685 edata->ref--; 3686 if (!edata->ref) { 3687 /* Remove the SOFT_MODE flag */ 3688 __ftrace_event_enable_disable(edata->file, 0, 1); 3689 trace_event_put_ref(edata->file->event_call); 3690 kfree(edata); 3691 } 3692 return 0; 3693 } 3694 3695 static void 3696 event_enable_free(struct ftrace_probe_ops *ops, struct trace_array *tr, 3697 unsigned long ip, void *data) 3698 { 3699 struct ftrace_func_mapper *mapper = data; 3700 struct event_probe_data *edata; 3701 3702 if (!ip) { 3703 if (!mapper) 3704 return; 3705 free_ftrace_func_mapper(mapper, free_probe_data); 3706 return; 3707 } 3708 3709 edata = ftrace_func_mapper_remove_ip(mapper, ip); 3710 3711 if (WARN_ON_ONCE(!edata)) 3712 return; 3713 3714 if (WARN_ON_ONCE(edata->ref <= 0)) 3715 return; 3716 3717 free_probe_data(edata); 3718 } 3719 3720 static struct ftrace_probe_ops event_enable_probe_ops = { 3721 .func = event_enable_probe, 3722 .print = event_enable_print, 3723 .init = event_enable_init, 3724 .free = event_enable_free, 3725 }; 3726 3727 static struct ftrace_probe_ops event_enable_count_probe_ops = { 3728 .func = event_enable_count_probe, 3729 .print = event_enable_print, 3730 .init = event_enable_init, 3731 .free = event_enable_free, 3732 }; 3733 3734 static struct ftrace_probe_ops event_disable_probe_ops = { 3735 .func = event_enable_probe, 3736 .print = event_enable_print, 3737 .init = event_enable_init, 3738 .free = event_enable_free, 3739 }; 3740 3741 static struct ftrace_probe_ops event_disable_count_probe_ops = { 3742 .func = event_enable_count_probe, 3743 .print = event_enable_print, 3744 .init = event_enable_init, 3745 .free = event_enable_free, 3746 }; 3747 3748 static int 3749 event_enable_func(struct trace_array *tr, struct ftrace_hash *hash, 3750 char *glob, char *cmd, char *param, int enabled) 3751 { 3752 struct trace_event_file *file; 3753 struct ftrace_probe_ops *ops; 3754 struct event_probe_data *data; 3755 const char *system; 3756 const char *event; 3757 char *number; 3758 bool enable; 3759 int ret; 3760 3761 if (!tr) 3762 return -ENODEV; 3763 3764 /* hash funcs only work with set_ftrace_filter */ 3765 if (!enabled || !param) 3766 return -EINVAL; 3767 3768 system = strsep(¶m, ":"); 3769 if (!param) 3770 return -EINVAL; 3771 3772 event = strsep(¶m, ":"); 3773 3774 mutex_lock(&event_mutex); 3775 3776 ret = -EINVAL; 3777 file = find_event_file(tr, system, event); 3778 if (!file) 3779 goto out; 3780 3781 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0; 3782 3783 if (enable) 3784 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops; 3785 else 3786 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops; 3787 3788 if (glob[0] == '!') { 3789 ret = unregister_ftrace_function_probe_func(glob+1, tr, ops); 3790 goto out; 3791 } 3792 3793 ret = -ENOMEM; 3794 3795 data = kzalloc(sizeof(*data), GFP_KERNEL); 3796 if (!data) 3797 goto out; 3798 3799 data->enable = enable; 3800 data->count = -1; 3801 data->file = file; 3802 3803 if (!param) 3804 goto out_reg; 3805 3806 number = strsep(¶m, ":"); 3807 3808 ret = -EINVAL; 3809 if (!strlen(number)) 3810 goto out_free; 3811 3812 /* 3813 * We use the callback data field (which is a pointer) 3814 * as our counter. 3815 */ 3816 ret = kstrtoul(number, 0, &data->count); 3817 if (ret) 3818 goto out_free; 3819 3820 out_reg: 3821 /* Don't let event modules unload while probe registered */ 3822 ret = trace_event_try_get_ref(file->event_call); 3823 if (!ret) { 3824 ret = -EBUSY; 3825 goto out_free; 3826 } 3827 3828 ret = __ftrace_event_enable_disable(file, 1, 1); 3829 if (ret < 0) 3830 goto out_put; 3831 3832 ret = register_ftrace_function_probe(glob, tr, ops, data); 3833 /* 3834 * The above returns on success the # of functions enabled, 3835 * but if it didn't find any functions it returns zero. 3836 * Consider no functions a failure too. 3837 */ 3838 if (!ret) { 3839 ret = -ENOENT; 3840 goto out_disable; 3841 } else if (ret < 0) 3842 goto out_disable; 3843 /* Just return zero, not the number of enabled functions */ 3844 ret = 0; 3845 out: 3846 mutex_unlock(&event_mutex); 3847 return ret; 3848 3849 out_disable: 3850 __ftrace_event_enable_disable(file, 0, 1); 3851 out_put: 3852 trace_event_put_ref(file->event_call); 3853 out_free: 3854 kfree(data); 3855 goto out; 3856 } 3857 3858 static struct ftrace_func_command event_enable_cmd = { 3859 .name = ENABLE_EVENT_STR, 3860 .func = event_enable_func, 3861 }; 3862 3863 static struct ftrace_func_command event_disable_cmd = { 3864 .name = DISABLE_EVENT_STR, 3865 .func = event_enable_func, 3866 }; 3867 3868 static __init int register_event_cmds(void) 3869 { 3870 int ret; 3871 3872 ret = register_ftrace_command(&event_enable_cmd); 3873 if (WARN_ON(ret < 0)) 3874 return ret; 3875 ret = register_ftrace_command(&event_disable_cmd); 3876 if (WARN_ON(ret < 0)) 3877 unregister_ftrace_command(&event_enable_cmd); 3878 return ret; 3879 } 3880 #else 3881 static inline int register_event_cmds(void) { return 0; } 3882 #endif /* CONFIG_DYNAMIC_FTRACE */ 3883 3884 /* 3885 * The top level array and trace arrays created by boot-time tracing 3886 * have already had its trace_event_file descriptors created in order 3887 * to allow for early events to be recorded. 3888 * This function is called after the tracefs has been initialized, 3889 * and we now have to create the files associated to the events. 3890 */ 3891 static void __trace_early_add_event_dirs(struct trace_array *tr) 3892 { 3893 struct trace_event_file *file; 3894 int ret; 3895 3896 3897 list_for_each_entry(file, &tr->events, list) { 3898 ret = event_create_dir(tr->event_dir, file); 3899 if (ret < 0) 3900 pr_warn("Could not create directory for event %s\n", 3901 trace_event_name(file->event_call)); 3902 } 3903 } 3904 3905 /* 3906 * For early boot up, the top trace array and the trace arrays created 3907 * by boot-time tracing require to have a list of events that can be 3908 * enabled. This must be done before the filesystem is set up in order 3909 * to allow events to be traced early. 3910 */ 3911 void __trace_early_add_events(struct trace_array *tr) 3912 { 3913 struct trace_event_call *call; 3914 int ret; 3915 3916 list_for_each_entry(call, &ftrace_events, list) { 3917 /* Early boot up should not have any modules loaded */ 3918 if (!(call->flags & TRACE_EVENT_FL_DYNAMIC) && 3919 WARN_ON_ONCE(call->module)) 3920 continue; 3921 3922 ret = __trace_early_add_new_event(call, tr); 3923 if (ret < 0) 3924 pr_warn("Could not create early event %s\n", 3925 trace_event_name(call)); 3926 } 3927 } 3928 3929 /* Remove the event directory structure for a trace directory. */ 3930 static void 3931 __trace_remove_event_dirs(struct trace_array *tr) 3932 { 3933 struct trace_event_file *file, *next; 3934 3935 list_for_each_entry_safe(file, next, &tr->events, list) 3936 remove_event_file_dir(file); 3937 } 3938 3939 static void __add_event_to_tracers(struct trace_event_call *call) 3940 { 3941 struct trace_array *tr; 3942 3943 list_for_each_entry(tr, &ftrace_trace_arrays, list) 3944 __trace_add_new_event(call, tr); 3945 } 3946 3947 extern struct trace_event_call *__start_ftrace_events[]; 3948 extern struct trace_event_call *__stop_ftrace_events[]; 3949 3950 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata; 3951 3952 static __init int setup_trace_event(char *str) 3953 { 3954 strscpy(bootup_event_buf, str, COMMAND_LINE_SIZE); 3955 ring_buffer_expanded = true; 3956 disable_tracing_selftest("running event tracing"); 3957 3958 return 1; 3959 } 3960 __setup("trace_event=", setup_trace_event); 3961 3962 static int events_callback(const char *name, umode_t *mode, void **data, 3963 const struct file_operations **fops) 3964 { 3965 if (strcmp(name, "enable") == 0) { 3966 *mode = TRACE_MODE_WRITE; 3967 *fops = &ftrace_tr_enable_fops; 3968 return 1; 3969 } 3970 3971 if (strcmp(name, "header_page") == 0) 3972 *data = ring_buffer_print_page_header; 3973 3974 else if (strcmp(name, "header_event") == 0) 3975 *data = ring_buffer_print_entry_header; 3976 3977 else 3978 return 0; 3979 3980 *mode = TRACE_MODE_READ; 3981 *fops = &ftrace_show_header_fops; 3982 return 1; 3983 } 3984 3985 /* Expects to have event_mutex held when called */ 3986 static int 3987 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr) 3988 { 3989 struct eventfs_inode *e_events; 3990 struct dentry *entry; 3991 int nr_entries; 3992 static struct eventfs_entry events_entries[] = { 3993 { 3994 .name = "enable", 3995 .callback = events_callback, 3996 }, 3997 { 3998 .name = "header_page", 3999 .callback = events_callback, 4000 }, 4001 { 4002 .name = "header_event", 4003 .callback = events_callback, 4004 }, 4005 }; 4006 4007 entry = trace_create_file("set_event", TRACE_MODE_WRITE, parent, 4008 tr, &ftrace_set_event_fops); 4009 if (!entry) 4010 return -ENOMEM; 4011 4012 nr_entries = ARRAY_SIZE(events_entries); 4013 4014 e_events = eventfs_create_events_dir("events", parent, events_entries, 4015 nr_entries, tr); 4016 if (IS_ERR(e_events)) { 4017 pr_warn("Could not create tracefs 'events' directory\n"); 4018 return -ENOMEM; 4019 } 4020 4021 /* There are not as crucial, just warn if they are not created */ 4022 4023 trace_create_file("set_event_pid", TRACE_MODE_WRITE, parent, 4024 tr, &ftrace_set_event_pid_fops); 4025 4026 trace_create_file("set_event_notrace_pid", 4027 TRACE_MODE_WRITE, parent, tr, 4028 &ftrace_set_event_notrace_pid_fops); 4029 4030 tr->event_dir = e_events; 4031 4032 return 0; 4033 } 4034 4035 /** 4036 * event_trace_add_tracer - add a instance of a trace_array to events 4037 * @parent: The parent dentry to place the files/directories for events in 4038 * @tr: The trace array associated with these events 4039 * 4040 * When a new instance is created, it needs to set up its events 4041 * directory, as well as other files associated with events. It also 4042 * creates the event hierarchy in the @parent/events directory. 4043 * 4044 * Returns 0 on success. 4045 * 4046 * Must be called with event_mutex held. 4047 */ 4048 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr) 4049 { 4050 int ret; 4051 4052 lockdep_assert_held(&event_mutex); 4053 4054 ret = create_event_toplevel_files(parent, tr); 4055 if (ret) 4056 goto out; 4057 4058 down_write(&trace_event_sem); 4059 /* If tr already has the event list, it is initialized in early boot. */ 4060 if (unlikely(!list_empty(&tr->events))) 4061 __trace_early_add_event_dirs(tr); 4062 else 4063 __trace_add_event_dirs(tr); 4064 up_write(&trace_event_sem); 4065 4066 out: 4067 return ret; 4068 } 4069 4070 /* 4071 * The top trace array already had its file descriptors created. 4072 * Now the files themselves need to be created. 4073 */ 4074 static __init int 4075 early_event_add_tracer(struct dentry *parent, struct trace_array *tr) 4076 { 4077 int ret; 4078 4079 mutex_lock(&event_mutex); 4080 4081 ret = create_event_toplevel_files(parent, tr); 4082 if (ret) 4083 goto out_unlock; 4084 4085 down_write(&trace_event_sem); 4086 __trace_early_add_event_dirs(tr); 4087 up_write(&trace_event_sem); 4088 4089 out_unlock: 4090 mutex_unlock(&event_mutex); 4091 4092 return ret; 4093 } 4094 4095 /* Must be called with event_mutex held */ 4096 int event_trace_del_tracer(struct trace_array *tr) 4097 { 4098 lockdep_assert_held(&event_mutex); 4099 4100 /* Disable any event triggers and associated soft-disabled events */ 4101 clear_event_triggers(tr); 4102 4103 /* Clear the pid list */ 4104 __ftrace_clear_event_pids(tr, TRACE_PIDS | TRACE_NO_PIDS); 4105 4106 /* Disable any running events */ 4107 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0); 4108 4109 /* Make sure no more events are being executed */ 4110 tracepoint_synchronize_unregister(); 4111 4112 down_write(&trace_event_sem); 4113 __trace_remove_event_dirs(tr); 4114 eventfs_remove_events_dir(tr->event_dir); 4115 up_write(&trace_event_sem); 4116 4117 tr->event_dir = NULL; 4118 4119 return 0; 4120 } 4121 4122 static __init int event_trace_memsetup(void) 4123 { 4124 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC); 4125 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC); 4126 return 0; 4127 } 4128 4129 __init void 4130 early_enable_events(struct trace_array *tr, char *buf, bool disable_first) 4131 { 4132 char *token; 4133 int ret; 4134 4135 while (true) { 4136 token = strsep(&buf, ","); 4137 4138 if (!token) 4139 break; 4140 4141 if (*token) { 4142 /* Restarting syscalls requires that we stop them first */ 4143 if (disable_first) 4144 ftrace_set_clr_event(tr, token, 0); 4145 4146 ret = ftrace_set_clr_event(tr, token, 1); 4147 if (ret) 4148 pr_warn("Failed to enable trace event: %s\n", token); 4149 } 4150 4151 /* Put back the comma to allow this to be called again */ 4152 if (buf) 4153 *(buf - 1) = ','; 4154 } 4155 } 4156 4157 static __init int event_trace_enable(void) 4158 { 4159 struct trace_array *tr = top_trace_array(); 4160 struct trace_event_call **iter, *call; 4161 int ret; 4162 4163 if (!tr) 4164 return -ENODEV; 4165 4166 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) { 4167 4168 call = *iter; 4169 ret = event_init(call); 4170 if (!ret) 4171 list_add(&call->list, &ftrace_events); 4172 } 4173 4174 register_trigger_cmds(); 4175 4176 /* 4177 * We need the top trace array to have a working set of trace 4178 * points at early init, before the debug files and directories 4179 * are created. Create the file entries now, and attach them 4180 * to the actual file dentries later. 4181 */ 4182 __trace_early_add_events(tr); 4183 4184 early_enable_events(tr, bootup_event_buf, false); 4185 4186 trace_printk_start_comm(); 4187 4188 register_event_cmds(); 4189 4190 4191 return 0; 4192 } 4193 4194 /* 4195 * event_trace_enable() is called from trace_event_init() first to 4196 * initialize events and perhaps start any events that are on the 4197 * command line. Unfortunately, there are some events that will not 4198 * start this early, like the system call tracepoints that need 4199 * to set the %SYSCALL_WORK_SYSCALL_TRACEPOINT flag of pid 1. But 4200 * event_trace_enable() is called before pid 1 starts, and this flag 4201 * is never set, making the syscall tracepoint never get reached, but 4202 * the event is enabled regardless (and not doing anything). 4203 */ 4204 static __init int event_trace_enable_again(void) 4205 { 4206 struct trace_array *tr; 4207 4208 tr = top_trace_array(); 4209 if (!tr) 4210 return -ENODEV; 4211 4212 early_enable_events(tr, bootup_event_buf, true); 4213 4214 return 0; 4215 } 4216 4217 early_initcall(event_trace_enable_again); 4218 4219 /* Init fields which doesn't related to the tracefs */ 4220 static __init int event_trace_init_fields(void) 4221 { 4222 if (trace_define_generic_fields()) 4223 pr_warn("tracing: Failed to allocated generic fields"); 4224 4225 if (trace_define_common_fields()) 4226 pr_warn("tracing: Failed to allocate common fields"); 4227 4228 return 0; 4229 } 4230 4231 __init int event_trace_init(void) 4232 { 4233 struct trace_array *tr; 4234 int ret; 4235 4236 tr = top_trace_array(); 4237 if (!tr) 4238 return -ENODEV; 4239 4240 trace_create_file("available_events", TRACE_MODE_READ, 4241 NULL, tr, &ftrace_avail_fops); 4242 4243 ret = early_event_add_tracer(NULL, tr); 4244 if (ret) 4245 return ret; 4246 4247 #ifdef CONFIG_MODULES 4248 ret = register_module_notifier(&trace_module_nb); 4249 if (ret) 4250 pr_warn("Failed to register trace events module notifier\n"); 4251 #endif 4252 4253 eventdir_initialized = true; 4254 4255 return 0; 4256 } 4257 4258 void __init trace_event_init(void) 4259 { 4260 event_trace_memsetup(); 4261 init_ftrace_syscalls(); 4262 event_trace_enable(); 4263 event_trace_init_fields(); 4264 } 4265 4266 #ifdef CONFIG_EVENT_TRACE_STARTUP_TEST 4267 4268 static DEFINE_SPINLOCK(test_spinlock); 4269 static DEFINE_SPINLOCK(test_spinlock_irq); 4270 static DEFINE_MUTEX(test_mutex); 4271 4272 static __init void test_work(struct work_struct *dummy) 4273 { 4274 spin_lock(&test_spinlock); 4275 spin_lock_irq(&test_spinlock_irq); 4276 udelay(1); 4277 spin_unlock_irq(&test_spinlock_irq); 4278 spin_unlock(&test_spinlock); 4279 4280 mutex_lock(&test_mutex); 4281 msleep(1); 4282 mutex_unlock(&test_mutex); 4283 } 4284 4285 static __init int event_test_thread(void *unused) 4286 { 4287 void *test_malloc; 4288 4289 test_malloc = kmalloc(1234, GFP_KERNEL); 4290 if (!test_malloc) 4291 pr_info("failed to kmalloc\n"); 4292 4293 schedule_on_each_cpu(test_work); 4294 4295 kfree(test_malloc); 4296 4297 set_current_state(TASK_INTERRUPTIBLE); 4298 while (!kthread_should_stop()) { 4299 schedule(); 4300 set_current_state(TASK_INTERRUPTIBLE); 4301 } 4302 __set_current_state(TASK_RUNNING); 4303 4304 return 0; 4305 } 4306 4307 /* 4308 * Do various things that may trigger events. 4309 */ 4310 static __init void event_test_stuff(void) 4311 { 4312 struct task_struct *test_thread; 4313 4314 test_thread = kthread_run(event_test_thread, NULL, "test-events"); 4315 msleep(1); 4316 kthread_stop(test_thread); 4317 } 4318 4319 /* 4320 * For every trace event defined, we will test each trace point separately, 4321 * and then by groups, and finally all trace points. 4322 */ 4323 static __init void event_trace_self_tests(void) 4324 { 4325 struct trace_subsystem_dir *dir; 4326 struct trace_event_file *file; 4327 struct trace_event_call *call; 4328 struct event_subsystem *system; 4329 struct trace_array *tr; 4330 int ret; 4331 4332 tr = top_trace_array(); 4333 if (!tr) 4334 return; 4335 4336 pr_info("Running tests on trace events:\n"); 4337 4338 list_for_each_entry(file, &tr->events, list) { 4339 4340 call = file->event_call; 4341 4342 /* Only test those that have a probe */ 4343 if (!call->class || !call->class->probe) 4344 continue; 4345 4346 /* 4347 * Testing syscall events here is pretty useless, but 4348 * we still do it if configured. But this is time consuming. 4349 * What we really need is a user thread to perform the 4350 * syscalls as we test. 4351 */ 4352 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS 4353 if (call->class->system && 4354 strcmp(call->class->system, "syscalls") == 0) 4355 continue; 4356 #endif 4357 4358 pr_info("Testing event %s: ", trace_event_name(call)); 4359 4360 /* 4361 * If an event is already enabled, someone is using 4362 * it and the self test should not be on. 4363 */ 4364 if (file->flags & EVENT_FILE_FL_ENABLED) { 4365 pr_warn("Enabled event during self test!\n"); 4366 WARN_ON_ONCE(1); 4367 continue; 4368 } 4369 4370 ftrace_event_enable_disable(file, 1); 4371 event_test_stuff(); 4372 ftrace_event_enable_disable(file, 0); 4373 4374 pr_cont("OK\n"); 4375 } 4376 4377 /* Now test at the sub system level */ 4378 4379 pr_info("Running tests on trace event systems:\n"); 4380 4381 list_for_each_entry(dir, &tr->systems, list) { 4382 4383 system = dir->subsystem; 4384 4385 /* the ftrace system is special, skip it */ 4386 if (strcmp(system->name, "ftrace") == 0) 4387 continue; 4388 4389 pr_info("Testing event system %s: ", system->name); 4390 4391 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1); 4392 if (WARN_ON_ONCE(ret)) { 4393 pr_warn("error enabling system %s\n", 4394 system->name); 4395 continue; 4396 } 4397 4398 event_test_stuff(); 4399 4400 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0); 4401 if (WARN_ON_ONCE(ret)) { 4402 pr_warn("error disabling system %s\n", 4403 system->name); 4404 continue; 4405 } 4406 4407 pr_cont("OK\n"); 4408 } 4409 4410 /* Test with all events enabled */ 4411 4412 pr_info("Running tests on all trace events:\n"); 4413 pr_info("Testing all events: "); 4414 4415 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1); 4416 if (WARN_ON_ONCE(ret)) { 4417 pr_warn("error enabling all events\n"); 4418 return; 4419 } 4420 4421 event_test_stuff(); 4422 4423 /* reset sysname */ 4424 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0); 4425 if (WARN_ON_ONCE(ret)) { 4426 pr_warn("error disabling all events\n"); 4427 return; 4428 } 4429 4430 pr_cont("OK\n"); 4431 } 4432 4433 #ifdef CONFIG_FUNCTION_TRACER 4434 4435 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable); 4436 4437 static struct trace_event_file event_trace_file __initdata; 4438 4439 static void __init 4440 function_test_events_call(unsigned long ip, unsigned long parent_ip, 4441 struct ftrace_ops *op, struct ftrace_regs *regs) 4442 { 4443 struct trace_buffer *buffer; 4444 struct ring_buffer_event *event; 4445 struct ftrace_entry *entry; 4446 unsigned int trace_ctx; 4447 long disabled; 4448 int cpu; 4449 4450 trace_ctx = tracing_gen_ctx(); 4451 preempt_disable_notrace(); 4452 cpu = raw_smp_processor_id(); 4453 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu)); 4454 4455 if (disabled != 1) 4456 goto out; 4457 4458 event = trace_event_buffer_lock_reserve(&buffer, &event_trace_file, 4459 TRACE_FN, sizeof(*entry), 4460 trace_ctx); 4461 if (!event) 4462 goto out; 4463 entry = ring_buffer_event_data(event); 4464 entry->ip = ip; 4465 entry->parent_ip = parent_ip; 4466 4467 event_trigger_unlock_commit(&event_trace_file, buffer, event, 4468 entry, trace_ctx); 4469 out: 4470 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu)); 4471 preempt_enable_notrace(); 4472 } 4473 4474 static struct ftrace_ops trace_ops __initdata = 4475 { 4476 .func = function_test_events_call, 4477 }; 4478 4479 static __init void event_trace_self_test_with_function(void) 4480 { 4481 int ret; 4482 4483 event_trace_file.tr = top_trace_array(); 4484 if (WARN_ON(!event_trace_file.tr)) 4485 return; 4486 4487 ret = register_ftrace_function(&trace_ops); 4488 if (WARN_ON(ret < 0)) { 4489 pr_info("Failed to enable function tracer for event tests\n"); 4490 return; 4491 } 4492 pr_info("Running tests again, along with the function tracer\n"); 4493 event_trace_self_tests(); 4494 unregister_ftrace_function(&trace_ops); 4495 } 4496 #else 4497 static __init void event_trace_self_test_with_function(void) 4498 { 4499 } 4500 #endif 4501 4502 static __init int event_trace_self_tests_init(void) 4503 { 4504 if (!tracing_selftest_disabled) { 4505 event_trace_self_tests(); 4506 event_trace_self_test_with_function(); 4507 } 4508 4509 return 0; 4510 } 4511 4512 late_initcall(event_trace_self_tests_init); 4513 4514 #endif 4515