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