1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * ring buffer based function tracer 4 * 5 * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com> 6 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com> 7 * 8 * Originally taken from the RT patch by: 9 * Arnaldo Carvalho de Melo <acme@redhat.com> 10 * 11 * Based on code from the latency_tracer, that is: 12 * Copyright (C) 2004-2006 Ingo Molnar 13 * Copyright (C) 2004 Nadia Yvette Chambers 14 */ 15 #include <linux/ring_buffer.h> 16 #include <generated/utsrelease.h> 17 #include <linux/stacktrace.h> 18 #include <linux/writeback.h> 19 #include <linux/kallsyms.h> 20 #include <linux/security.h> 21 #include <linux/seq_file.h> 22 #include <linux/notifier.h> 23 #include <linux/irqflags.h> 24 #include <linux/debugfs.h> 25 #include <linux/tracefs.h> 26 #include <linux/pagemap.h> 27 #include <linux/hardirq.h> 28 #include <linux/linkage.h> 29 #include <linux/uaccess.h> 30 #include <linux/vmalloc.h> 31 #include <linux/ftrace.h> 32 #include <linux/module.h> 33 #include <linux/percpu.h> 34 #include <linux/splice.h> 35 #include <linux/kdebug.h> 36 #include <linux/string.h> 37 #include <linux/mount.h> 38 #include <linux/rwsem.h> 39 #include <linux/slab.h> 40 #include <linux/ctype.h> 41 #include <linux/init.h> 42 #include <linux/panic_notifier.h> 43 #include <linux/poll.h> 44 #include <linux/nmi.h> 45 #include <linux/fs.h> 46 #include <linux/trace.h> 47 #include <linux/sched/clock.h> 48 #include <linux/sched/rt.h> 49 #include <linux/fsnotify.h> 50 #include <linux/irq_work.h> 51 #include <linux/workqueue.h> 52 53 #include "trace.h" 54 #include "trace_output.h" 55 56 /* 57 * On boot up, the ring buffer is set to the minimum size, so that 58 * we do not waste memory on systems that are not using tracing. 59 */ 60 bool ring_buffer_expanded; 61 62 /* 63 * We need to change this state when a selftest is running. 64 * A selftest will lurk into the ring-buffer to count the 65 * entries inserted during the selftest although some concurrent 66 * insertions into the ring-buffer such as trace_printk could occurred 67 * at the same time, giving false positive or negative results. 68 */ 69 static bool __read_mostly tracing_selftest_running; 70 71 /* 72 * If boot-time tracing including tracers/events via kernel cmdline 73 * is running, we do not want to run SELFTEST. 74 */ 75 bool __read_mostly tracing_selftest_disabled; 76 77 #ifdef CONFIG_FTRACE_STARTUP_TEST 78 void __init disable_tracing_selftest(const char *reason) 79 { 80 if (!tracing_selftest_disabled) { 81 tracing_selftest_disabled = true; 82 pr_info("Ftrace startup test is disabled due to %s\n", reason); 83 } 84 } 85 #endif 86 87 /* Pipe tracepoints to printk */ 88 struct trace_iterator *tracepoint_print_iter; 89 int tracepoint_printk; 90 static bool tracepoint_printk_stop_on_boot __initdata; 91 static DEFINE_STATIC_KEY_FALSE(tracepoint_printk_key); 92 93 /* For tracers that don't implement custom flags */ 94 static struct tracer_opt dummy_tracer_opt[] = { 95 { } 96 }; 97 98 static int 99 dummy_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set) 100 { 101 return 0; 102 } 103 104 /* 105 * To prevent the comm cache from being overwritten when no 106 * tracing is active, only save the comm when a trace event 107 * occurred. 108 */ 109 static DEFINE_PER_CPU(bool, trace_taskinfo_save); 110 111 /* 112 * Kill all tracing for good (never come back). 113 * It is initialized to 1 but will turn to zero if the initialization 114 * of the tracer is successful. But that is the only place that sets 115 * this back to zero. 116 */ 117 static int tracing_disabled = 1; 118 119 cpumask_var_t __read_mostly tracing_buffer_mask; 120 121 /* 122 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops 123 * 124 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops 125 * is set, then ftrace_dump is called. This will output the contents 126 * of the ftrace buffers to the console. This is very useful for 127 * capturing traces that lead to crashes and outputing it to a 128 * serial console. 129 * 130 * It is default off, but you can enable it with either specifying 131 * "ftrace_dump_on_oops" in the kernel command line, or setting 132 * /proc/sys/kernel/ftrace_dump_on_oops 133 * Set 1 if you want to dump buffers of all CPUs 134 * Set 2 if you want to dump the buffer of the CPU that triggered oops 135 */ 136 137 enum ftrace_dump_mode ftrace_dump_on_oops; 138 139 /* When set, tracing will stop when a WARN*() is hit */ 140 int __disable_trace_on_warning; 141 142 #ifdef CONFIG_TRACE_EVAL_MAP_FILE 143 /* Map of enums to their values, for "eval_map" file */ 144 struct trace_eval_map_head { 145 struct module *mod; 146 unsigned long length; 147 }; 148 149 union trace_eval_map_item; 150 151 struct trace_eval_map_tail { 152 /* 153 * "end" is first and points to NULL as it must be different 154 * than "mod" or "eval_string" 155 */ 156 union trace_eval_map_item *next; 157 const char *end; /* points to NULL */ 158 }; 159 160 static DEFINE_MUTEX(trace_eval_mutex); 161 162 /* 163 * The trace_eval_maps are saved in an array with two extra elements, 164 * one at the beginning, and one at the end. The beginning item contains 165 * the count of the saved maps (head.length), and the module they 166 * belong to if not built in (head.mod). The ending item contains a 167 * pointer to the next array of saved eval_map items. 168 */ 169 union trace_eval_map_item { 170 struct trace_eval_map map; 171 struct trace_eval_map_head head; 172 struct trace_eval_map_tail tail; 173 }; 174 175 static union trace_eval_map_item *trace_eval_maps; 176 #endif /* CONFIG_TRACE_EVAL_MAP_FILE */ 177 178 int tracing_set_tracer(struct trace_array *tr, const char *buf); 179 static void ftrace_trace_userstack(struct trace_array *tr, 180 struct trace_buffer *buffer, 181 unsigned int trace_ctx); 182 183 #define MAX_TRACER_SIZE 100 184 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata; 185 static char *default_bootup_tracer; 186 187 static bool allocate_snapshot; 188 189 static int __init set_cmdline_ftrace(char *str) 190 { 191 strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE); 192 default_bootup_tracer = bootup_tracer_buf; 193 /* We are using ftrace early, expand it */ 194 ring_buffer_expanded = true; 195 return 1; 196 } 197 __setup("ftrace=", set_cmdline_ftrace); 198 199 static int __init set_ftrace_dump_on_oops(char *str) 200 { 201 if (*str++ != '=' || !*str || !strcmp("1", str)) { 202 ftrace_dump_on_oops = DUMP_ALL; 203 return 1; 204 } 205 206 if (!strcmp("orig_cpu", str) || !strcmp("2", str)) { 207 ftrace_dump_on_oops = DUMP_ORIG; 208 return 1; 209 } 210 211 return 0; 212 } 213 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops); 214 215 static int __init stop_trace_on_warning(char *str) 216 { 217 if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0)) 218 __disable_trace_on_warning = 1; 219 return 1; 220 } 221 __setup("traceoff_on_warning", stop_trace_on_warning); 222 223 static int __init boot_alloc_snapshot(char *str) 224 { 225 allocate_snapshot = true; 226 /* We also need the main ring buffer expanded */ 227 ring_buffer_expanded = true; 228 return 1; 229 } 230 __setup("alloc_snapshot", boot_alloc_snapshot); 231 232 233 static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata; 234 235 static int __init set_trace_boot_options(char *str) 236 { 237 strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE); 238 return 0; 239 } 240 __setup("trace_options=", set_trace_boot_options); 241 242 static char trace_boot_clock_buf[MAX_TRACER_SIZE] __initdata; 243 static char *trace_boot_clock __initdata; 244 245 static int __init set_trace_boot_clock(char *str) 246 { 247 strlcpy(trace_boot_clock_buf, str, MAX_TRACER_SIZE); 248 trace_boot_clock = trace_boot_clock_buf; 249 return 0; 250 } 251 __setup("trace_clock=", set_trace_boot_clock); 252 253 static int __init set_tracepoint_printk(char *str) 254 { 255 if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0)) 256 tracepoint_printk = 1; 257 return 1; 258 } 259 __setup("tp_printk", set_tracepoint_printk); 260 261 static int __init set_tracepoint_printk_stop(char *str) 262 { 263 tracepoint_printk_stop_on_boot = true; 264 return 1; 265 } 266 __setup("tp_printk_stop_on_boot", set_tracepoint_printk_stop); 267 268 unsigned long long ns2usecs(u64 nsec) 269 { 270 nsec += 500; 271 do_div(nsec, 1000); 272 return nsec; 273 } 274 275 static void 276 trace_process_export(struct trace_export *export, 277 struct ring_buffer_event *event, int flag) 278 { 279 struct trace_entry *entry; 280 unsigned int size = 0; 281 282 if (export->flags & flag) { 283 entry = ring_buffer_event_data(event); 284 size = ring_buffer_event_length(event); 285 export->write(export, entry, size); 286 } 287 } 288 289 static DEFINE_MUTEX(ftrace_export_lock); 290 291 static struct trace_export __rcu *ftrace_exports_list __read_mostly; 292 293 static DEFINE_STATIC_KEY_FALSE(trace_function_exports_enabled); 294 static DEFINE_STATIC_KEY_FALSE(trace_event_exports_enabled); 295 static DEFINE_STATIC_KEY_FALSE(trace_marker_exports_enabled); 296 297 static inline void ftrace_exports_enable(struct trace_export *export) 298 { 299 if (export->flags & TRACE_EXPORT_FUNCTION) 300 static_branch_inc(&trace_function_exports_enabled); 301 302 if (export->flags & TRACE_EXPORT_EVENT) 303 static_branch_inc(&trace_event_exports_enabled); 304 305 if (export->flags & TRACE_EXPORT_MARKER) 306 static_branch_inc(&trace_marker_exports_enabled); 307 } 308 309 static inline void ftrace_exports_disable(struct trace_export *export) 310 { 311 if (export->flags & TRACE_EXPORT_FUNCTION) 312 static_branch_dec(&trace_function_exports_enabled); 313 314 if (export->flags & TRACE_EXPORT_EVENT) 315 static_branch_dec(&trace_event_exports_enabled); 316 317 if (export->flags & TRACE_EXPORT_MARKER) 318 static_branch_dec(&trace_marker_exports_enabled); 319 } 320 321 static void ftrace_exports(struct ring_buffer_event *event, int flag) 322 { 323 struct trace_export *export; 324 325 preempt_disable_notrace(); 326 327 export = rcu_dereference_raw_check(ftrace_exports_list); 328 while (export) { 329 trace_process_export(export, event, flag); 330 export = rcu_dereference_raw_check(export->next); 331 } 332 333 preempt_enable_notrace(); 334 } 335 336 static inline void 337 add_trace_export(struct trace_export **list, struct trace_export *export) 338 { 339 rcu_assign_pointer(export->next, *list); 340 /* 341 * We are entering export into the list but another 342 * CPU might be walking that list. We need to make sure 343 * the export->next pointer is valid before another CPU sees 344 * the export pointer included into the list. 345 */ 346 rcu_assign_pointer(*list, export); 347 } 348 349 static inline int 350 rm_trace_export(struct trace_export **list, struct trace_export *export) 351 { 352 struct trace_export **p; 353 354 for (p = list; *p != NULL; p = &(*p)->next) 355 if (*p == export) 356 break; 357 358 if (*p != export) 359 return -1; 360 361 rcu_assign_pointer(*p, (*p)->next); 362 363 return 0; 364 } 365 366 static inline void 367 add_ftrace_export(struct trace_export **list, struct trace_export *export) 368 { 369 ftrace_exports_enable(export); 370 371 add_trace_export(list, export); 372 } 373 374 static inline int 375 rm_ftrace_export(struct trace_export **list, struct trace_export *export) 376 { 377 int ret; 378 379 ret = rm_trace_export(list, export); 380 ftrace_exports_disable(export); 381 382 return ret; 383 } 384 385 int register_ftrace_export(struct trace_export *export) 386 { 387 if (WARN_ON_ONCE(!export->write)) 388 return -1; 389 390 mutex_lock(&ftrace_export_lock); 391 392 add_ftrace_export(&ftrace_exports_list, export); 393 394 mutex_unlock(&ftrace_export_lock); 395 396 return 0; 397 } 398 EXPORT_SYMBOL_GPL(register_ftrace_export); 399 400 int unregister_ftrace_export(struct trace_export *export) 401 { 402 int ret; 403 404 mutex_lock(&ftrace_export_lock); 405 406 ret = rm_ftrace_export(&ftrace_exports_list, export); 407 408 mutex_unlock(&ftrace_export_lock); 409 410 return ret; 411 } 412 EXPORT_SYMBOL_GPL(unregister_ftrace_export); 413 414 /* trace_flags holds trace_options default values */ 415 #define TRACE_DEFAULT_FLAGS \ 416 (FUNCTION_DEFAULT_FLAGS | \ 417 TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK | \ 418 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | \ 419 TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE | \ 420 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS | \ 421 TRACE_ITER_HASH_PTR) 422 423 /* trace_options that are only supported by global_trace */ 424 #define TOP_LEVEL_TRACE_FLAGS (TRACE_ITER_PRINTK | \ 425 TRACE_ITER_PRINTK_MSGONLY | TRACE_ITER_RECORD_CMD) 426 427 /* trace_flags that are default zero for instances */ 428 #define ZEROED_TRACE_FLAGS \ 429 (TRACE_ITER_EVENT_FORK | TRACE_ITER_FUNC_FORK) 430 431 /* 432 * The global_trace is the descriptor that holds the top-level tracing 433 * buffers for the live tracing. 434 */ 435 static struct trace_array global_trace = { 436 .trace_flags = TRACE_DEFAULT_FLAGS, 437 }; 438 439 LIST_HEAD(ftrace_trace_arrays); 440 441 int trace_array_get(struct trace_array *this_tr) 442 { 443 struct trace_array *tr; 444 int ret = -ENODEV; 445 446 mutex_lock(&trace_types_lock); 447 list_for_each_entry(tr, &ftrace_trace_arrays, list) { 448 if (tr == this_tr) { 449 tr->ref++; 450 ret = 0; 451 break; 452 } 453 } 454 mutex_unlock(&trace_types_lock); 455 456 return ret; 457 } 458 459 static void __trace_array_put(struct trace_array *this_tr) 460 { 461 WARN_ON(!this_tr->ref); 462 this_tr->ref--; 463 } 464 465 /** 466 * trace_array_put - Decrement the reference counter for this trace array. 467 * @this_tr : pointer to the trace array 468 * 469 * NOTE: Use this when we no longer need the trace array returned by 470 * trace_array_get_by_name(). This ensures the trace array can be later 471 * destroyed. 472 * 473 */ 474 void trace_array_put(struct trace_array *this_tr) 475 { 476 if (!this_tr) 477 return; 478 479 mutex_lock(&trace_types_lock); 480 __trace_array_put(this_tr); 481 mutex_unlock(&trace_types_lock); 482 } 483 EXPORT_SYMBOL_GPL(trace_array_put); 484 485 int tracing_check_open_get_tr(struct trace_array *tr) 486 { 487 int ret; 488 489 ret = security_locked_down(LOCKDOWN_TRACEFS); 490 if (ret) 491 return ret; 492 493 if (tracing_disabled) 494 return -ENODEV; 495 496 if (tr && trace_array_get(tr) < 0) 497 return -ENODEV; 498 499 return 0; 500 } 501 502 int call_filter_check_discard(struct trace_event_call *call, void *rec, 503 struct trace_buffer *buffer, 504 struct ring_buffer_event *event) 505 { 506 if (unlikely(call->flags & TRACE_EVENT_FL_FILTERED) && 507 !filter_match_preds(call->filter, rec)) { 508 __trace_event_discard_commit(buffer, event); 509 return 1; 510 } 511 512 return 0; 513 } 514 515 /** 516 * trace_find_filtered_pid - check if a pid exists in a filtered_pid list 517 * @filtered_pids: The list of pids to check 518 * @search_pid: The PID to find in @filtered_pids 519 * 520 * Returns true if @search_pid is found in @filtered_pids, and false otherwise. 521 */ 522 bool 523 trace_find_filtered_pid(struct trace_pid_list *filtered_pids, pid_t search_pid) 524 { 525 return trace_pid_list_is_set(filtered_pids, search_pid); 526 } 527 528 /** 529 * trace_ignore_this_task - should a task be ignored for tracing 530 * @filtered_pids: The list of pids to check 531 * @filtered_no_pids: The list of pids not to be traced 532 * @task: The task that should be ignored if not filtered 533 * 534 * Checks if @task should be traced or not from @filtered_pids. 535 * Returns true if @task should *NOT* be traced. 536 * Returns false if @task should be traced. 537 */ 538 bool 539 trace_ignore_this_task(struct trace_pid_list *filtered_pids, 540 struct trace_pid_list *filtered_no_pids, 541 struct task_struct *task) 542 { 543 /* 544 * If filtered_no_pids is not empty, and the task's pid is listed 545 * in filtered_no_pids, then return true. 546 * Otherwise, if filtered_pids is empty, that means we can 547 * trace all tasks. If it has content, then only trace pids 548 * within filtered_pids. 549 */ 550 551 return (filtered_pids && 552 !trace_find_filtered_pid(filtered_pids, task->pid)) || 553 (filtered_no_pids && 554 trace_find_filtered_pid(filtered_no_pids, task->pid)); 555 } 556 557 /** 558 * trace_filter_add_remove_task - Add or remove a task from a pid_list 559 * @pid_list: The list to modify 560 * @self: The current task for fork or NULL for exit 561 * @task: The task to add or remove 562 * 563 * If adding a task, if @self is defined, the task is only added if @self 564 * is also included in @pid_list. This happens on fork and tasks should 565 * only be added when the parent is listed. If @self is NULL, then the 566 * @task pid will be removed from the list, which would happen on exit 567 * of a task. 568 */ 569 void trace_filter_add_remove_task(struct trace_pid_list *pid_list, 570 struct task_struct *self, 571 struct task_struct *task) 572 { 573 if (!pid_list) 574 return; 575 576 /* For forks, we only add if the forking task is listed */ 577 if (self) { 578 if (!trace_find_filtered_pid(pid_list, self->pid)) 579 return; 580 } 581 582 /* "self" is set for forks, and NULL for exits */ 583 if (self) 584 trace_pid_list_set(pid_list, task->pid); 585 else 586 trace_pid_list_clear(pid_list, task->pid); 587 } 588 589 /** 590 * trace_pid_next - Used for seq_file to get to the next pid of a pid_list 591 * @pid_list: The pid list to show 592 * @v: The last pid that was shown (+1 the actual pid to let zero be displayed) 593 * @pos: The position of the file 594 * 595 * This is used by the seq_file "next" operation to iterate the pids 596 * listed in a trace_pid_list structure. 597 * 598 * Returns the pid+1 as we want to display pid of zero, but NULL would 599 * stop the iteration. 600 */ 601 void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos) 602 { 603 long pid = (unsigned long)v; 604 unsigned int next; 605 606 (*pos)++; 607 608 /* pid already is +1 of the actual previous bit */ 609 if (trace_pid_list_next(pid_list, pid, &next) < 0) 610 return NULL; 611 612 pid = next; 613 614 /* Return pid + 1 to allow zero to be represented */ 615 return (void *)(pid + 1); 616 } 617 618 /** 619 * trace_pid_start - Used for seq_file to start reading pid lists 620 * @pid_list: The pid list to show 621 * @pos: The position of the file 622 * 623 * This is used by seq_file "start" operation to start the iteration 624 * of listing pids. 625 * 626 * Returns the pid+1 as we want to display pid of zero, but NULL would 627 * stop the iteration. 628 */ 629 void *trace_pid_start(struct trace_pid_list *pid_list, loff_t *pos) 630 { 631 unsigned long pid; 632 unsigned int first; 633 loff_t l = 0; 634 635 if (trace_pid_list_first(pid_list, &first) < 0) 636 return NULL; 637 638 pid = first; 639 640 /* Return pid + 1 so that zero can be the exit value */ 641 for (pid++; pid && l < *pos; 642 pid = (unsigned long)trace_pid_next(pid_list, (void *)pid, &l)) 643 ; 644 return (void *)pid; 645 } 646 647 /** 648 * trace_pid_show - show the current pid in seq_file processing 649 * @m: The seq_file structure to write into 650 * @v: A void pointer of the pid (+1) value to display 651 * 652 * Can be directly used by seq_file operations to display the current 653 * pid value. 654 */ 655 int trace_pid_show(struct seq_file *m, void *v) 656 { 657 unsigned long pid = (unsigned long)v - 1; 658 659 seq_printf(m, "%lu\n", pid); 660 return 0; 661 } 662 663 /* 128 should be much more than enough */ 664 #define PID_BUF_SIZE 127 665 666 int trace_pid_write(struct trace_pid_list *filtered_pids, 667 struct trace_pid_list **new_pid_list, 668 const char __user *ubuf, size_t cnt) 669 { 670 struct trace_pid_list *pid_list; 671 struct trace_parser parser; 672 unsigned long val; 673 int nr_pids = 0; 674 ssize_t read = 0; 675 ssize_t ret; 676 loff_t pos; 677 pid_t pid; 678 679 if (trace_parser_get_init(&parser, PID_BUF_SIZE + 1)) 680 return -ENOMEM; 681 682 /* 683 * Always recreate a new array. The write is an all or nothing 684 * operation. Always create a new array when adding new pids by 685 * the user. If the operation fails, then the current list is 686 * not modified. 687 */ 688 pid_list = trace_pid_list_alloc(); 689 if (!pid_list) { 690 trace_parser_put(&parser); 691 return -ENOMEM; 692 } 693 694 if (filtered_pids) { 695 /* copy the current bits to the new max */ 696 ret = trace_pid_list_first(filtered_pids, &pid); 697 while (!ret) { 698 trace_pid_list_set(pid_list, pid); 699 ret = trace_pid_list_next(filtered_pids, pid + 1, &pid); 700 nr_pids++; 701 } 702 } 703 704 ret = 0; 705 while (cnt > 0) { 706 707 pos = 0; 708 709 ret = trace_get_user(&parser, ubuf, cnt, &pos); 710 if (ret < 0 || !trace_parser_loaded(&parser)) 711 break; 712 713 read += ret; 714 ubuf += ret; 715 cnt -= ret; 716 717 ret = -EINVAL; 718 if (kstrtoul(parser.buffer, 0, &val)) 719 break; 720 721 pid = (pid_t)val; 722 723 if (trace_pid_list_set(pid_list, pid) < 0) { 724 ret = -1; 725 break; 726 } 727 nr_pids++; 728 729 trace_parser_clear(&parser); 730 ret = 0; 731 } 732 trace_parser_put(&parser); 733 734 if (ret < 0) { 735 trace_pid_list_free(pid_list); 736 return ret; 737 } 738 739 if (!nr_pids) { 740 /* Cleared the list of pids */ 741 trace_pid_list_free(pid_list); 742 read = ret; 743 pid_list = NULL; 744 } 745 746 *new_pid_list = pid_list; 747 748 return read; 749 } 750 751 static u64 buffer_ftrace_now(struct array_buffer *buf, int cpu) 752 { 753 u64 ts; 754 755 /* Early boot up does not have a buffer yet */ 756 if (!buf->buffer) 757 return trace_clock_local(); 758 759 ts = ring_buffer_time_stamp(buf->buffer); 760 ring_buffer_normalize_time_stamp(buf->buffer, cpu, &ts); 761 762 return ts; 763 } 764 765 u64 ftrace_now(int cpu) 766 { 767 return buffer_ftrace_now(&global_trace.array_buffer, cpu); 768 } 769 770 /** 771 * tracing_is_enabled - Show if global_trace has been enabled 772 * 773 * Shows if the global trace has been enabled or not. It uses the 774 * mirror flag "buffer_disabled" to be used in fast paths such as for 775 * the irqsoff tracer. But it may be inaccurate due to races. If you 776 * need to know the accurate state, use tracing_is_on() which is a little 777 * slower, but accurate. 778 */ 779 int tracing_is_enabled(void) 780 { 781 /* 782 * For quick access (irqsoff uses this in fast path), just 783 * return the mirror variable of the state of the ring buffer. 784 * It's a little racy, but we don't really care. 785 */ 786 smp_rmb(); 787 return !global_trace.buffer_disabled; 788 } 789 790 /* 791 * trace_buf_size is the size in bytes that is allocated 792 * for a buffer. Note, the number of bytes is always rounded 793 * to page size. 794 * 795 * This number is purposely set to a low number of 16384. 796 * If the dump on oops happens, it will be much appreciated 797 * to not have to wait for all that output. Anyway this can be 798 * boot time and run time configurable. 799 */ 800 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */ 801 802 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT; 803 804 /* trace_types holds a link list of available tracers. */ 805 static struct tracer *trace_types __read_mostly; 806 807 /* 808 * trace_types_lock is used to protect the trace_types list. 809 */ 810 DEFINE_MUTEX(trace_types_lock); 811 812 /* 813 * serialize the access of the ring buffer 814 * 815 * ring buffer serializes readers, but it is low level protection. 816 * The validity of the events (which returns by ring_buffer_peek() ..etc) 817 * are not protected by ring buffer. 818 * 819 * The content of events may become garbage if we allow other process consumes 820 * these events concurrently: 821 * A) the page of the consumed events may become a normal page 822 * (not reader page) in ring buffer, and this page will be rewritten 823 * by events producer. 824 * B) The page of the consumed events may become a page for splice_read, 825 * and this page will be returned to system. 826 * 827 * These primitives allow multi process access to different cpu ring buffer 828 * concurrently. 829 * 830 * These primitives don't distinguish read-only and read-consume access. 831 * Multi read-only access are also serialized. 832 */ 833 834 #ifdef CONFIG_SMP 835 static DECLARE_RWSEM(all_cpu_access_lock); 836 static DEFINE_PER_CPU(struct mutex, cpu_access_lock); 837 838 static inline void trace_access_lock(int cpu) 839 { 840 if (cpu == RING_BUFFER_ALL_CPUS) { 841 /* gain it for accessing the whole ring buffer. */ 842 down_write(&all_cpu_access_lock); 843 } else { 844 /* gain it for accessing a cpu ring buffer. */ 845 846 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */ 847 down_read(&all_cpu_access_lock); 848 849 /* Secondly block other access to this @cpu ring buffer. */ 850 mutex_lock(&per_cpu(cpu_access_lock, cpu)); 851 } 852 } 853 854 static inline void trace_access_unlock(int cpu) 855 { 856 if (cpu == RING_BUFFER_ALL_CPUS) { 857 up_write(&all_cpu_access_lock); 858 } else { 859 mutex_unlock(&per_cpu(cpu_access_lock, cpu)); 860 up_read(&all_cpu_access_lock); 861 } 862 } 863 864 static inline void trace_access_lock_init(void) 865 { 866 int cpu; 867 868 for_each_possible_cpu(cpu) 869 mutex_init(&per_cpu(cpu_access_lock, cpu)); 870 } 871 872 #else 873 874 static DEFINE_MUTEX(access_lock); 875 876 static inline void trace_access_lock(int cpu) 877 { 878 (void)cpu; 879 mutex_lock(&access_lock); 880 } 881 882 static inline void trace_access_unlock(int cpu) 883 { 884 (void)cpu; 885 mutex_unlock(&access_lock); 886 } 887 888 static inline void trace_access_lock_init(void) 889 { 890 } 891 892 #endif 893 894 #ifdef CONFIG_STACKTRACE 895 static void __ftrace_trace_stack(struct trace_buffer *buffer, 896 unsigned int trace_ctx, 897 int skip, struct pt_regs *regs); 898 static inline void ftrace_trace_stack(struct trace_array *tr, 899 struct trace_buffer *buffer, 900 unsigned int trace_ctx, 901 int skip, struct pt_regs *regs); 902 903 #else 904 static inline void __ftrace_trace_stack(struct trace_buffer *buffer, 905 unsigned int trace_ctx, 906 int skip, struct pt_regs *regs) 907 { 908 } 909 static inline void ftrace_trace_stack(struct trace_array *tr, 910 struct trace_buffer *buffer, 911 unsigned long trace_ctx, 912 int skip, struct pt_regs *regs) 913 { 914 } 915 916 #endif 917 918 static __always_inline void 919 trace_event_setup(struct ring_buffer_event *event, 920 int type, unsigned int trace_ctx) 921 { 922 struct trace_entry *ent = ring_buffer_event_data(event); 923 924 tracing_generic_entry_update(ent, type, trace_ctx); 925 } 926 927 static __always_inline struct ring_buffer_event * 928 __trace_buffer_lock_reserve(struct trace_buffer *buffer, 929 int type, 930 unsigned long len, 931 unsigned int trace_ctx) 932 { 933 struct ring_buffer_event *event; 934 935 event = ring_buffer_lock_reserve(buffer, len); 936 if (event != NULL) 937 trace_event_setup(event, type, trace_ctx); 938 939 return event; 940 } 941 942 void tracer_tracing_on(struct trace_array *tr) 943 { 944 if (tr->array_buffer.buffer) 945 ring_buffer_record_on(tr->array_buffer.buffer); 946 /* 947 * This flag is looked at when buffers haven't been allocated 948 * yet, or by some tracers (like irqsoff), that just want to 949 * know if the ring buffer has been disabled, but it can handle 950 * races of where it gets disabled but we still do a record. 951 * As the check is in the fast path of the tracers, it is more 952 * important to be fast than accurate. 953 */ 954 tr->buffer_disabled = 0; 955 /* Make the flag seen by readers */ 956 smp_wmb(); 957 } 958 959 /** 960 * tracing_on - enable tracing buffers 961 * 962 * This function enables tracing buffers that may have been 963 * disabled with tracing_off. 964 */ 965 void tracing_on(void) 966 { 967 tracer_tracing_on(&global_trace); 968 } 969 EXPORT_SYMBOL_GPL(tracing_on); 970 971 972 static __always_inline void 973 __buffer_unlock_commit(struct trace_buffer *buffer, struct ring_buffer_event *event) 974 { 975 __this_cpu_write(trace_taskinfo_save, true); 976 977 /* If this is the temp buffer, we need to commit fully */ 978 if (this_cpu_read(trace_buffered_event) == event) { 979 /* Length is in event->array[0] */ 980 ring_buffer_write(buffer, event->array[0], &event->array[1]); 981 /* Release the temp buffer */ 982 this_cpu_dec(trace_buffered_event_cnt); 983 } else 984 ring_buffer_unlock_commit(buffer, event); 985 } 986 987 /** 988 * __trace_puts - write a constant string into the trace buffer. 989 * @ip: The address of the caller 990 * @str: The constant string to write 991 * @size: The size of the string. 992 */ 993 int __trace_puts(unsigned long ip, const char *str, int size) 994 { 995 struct ring_buffer_event *event; 996 struct trace_buffer *buffer; 997 struct print_entry *entry; 998 unsigned int trace_ctx; 999 int alloc; 1000 1001 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK)) 1002 return 0; 1003 1004 if (unlikely(tracing_selftest_running || tracing_disabled)) 1005 return 0; 1006 1007 alloc = sizeof(*entry) + size + 2; /* possible \n added */ 1008 1009 trace_ctx = tracing_gen_ctx(); 1010 buffer = global_trace.array_buffer.buffer; 1011 ring_buffer_nest_start(buffer); 1012 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc, 1013 trace_ctx); 1014 if (!event) { 1015 size = 0; 1016 goto out; 1017 } 1018 1019 entry = ring_buffer_event_data(event); 1020 entry->ip = ip; 1021 1022 memcpy(&entry->buf, str, size); 1023 1024 /* Add a newline if necessary */ 1025 if (entry->buf[size - 1] != '\n') { 1026 entry->buf[size] = '\n'; 1027 entry->buf[size + 1] = '\0'; 1028 } else 1029 entry->buf[size] = '\0'; 1030 1031 __buffer_unlock_commit(buffer, event); 1032 ftrace_trace_stack(&global_trace, buffer, trace_ctx, 4, NULL); 1033 out: 1034 ring_buffer_nest_end(buffer); 1035 return size; 1036 } 1037 EXPORT_SYMBOL_GPL(__trace_puts); 1038 1039 /** 1040 * __trace_bputs - write the pointer to a constant string into trace buffer 1041 * @ip: The address of the caller 1042 * @str: The constant string to write to the buffer to 1043 */ 1044 int __trace_bputs(unsigned long ip, const char *str) 1045 { 1046 struct ring_buffer_event *event; 1047 struct trace_buffer *buffer; 1048 struct bputs_entry *entry; 1049 unsigned int trace_ctx; 1050 int size = sizeof(struct bputs_entry); 1051 int ret = 0; 1052 1053 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK)) 1054 return 0; 1055 1056 if (unlikely(tracing_selftest_running || tracing_disabled)) 1057 return 0; 1058 1059 trace_ctx = tracing_gen_ctx(); 1060 buffer = global_trace.array_buffer.buffer; 1061 1062 ring_buffer_nest_start(buffer); 1063 event = __trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size, 1064 trace_ctx); 1065 if (!event) 1066 goto out; 1067 1068 entry = ring_buffer_event_data(event); 1069 entry->ip = ip; 1070 entry->str = str; 1071 1072 __buffer_unlock_commit(buffer, event); 1073 ftrace_trace_stack(&global_trace, buffer, trace_ctx, 4, NULL); 1074 1075 ret = 1; 1076 out: 1077 ring_buffer_nest_end(buffer); 1078 return ret; 1079 } 1080 EXPORT_SYMBOL_GPL(__trace_bputs); 1081 1082 #ifdef CONFIG_TRACER_SNAPSHOT 1083 static void tracing_snapshot_instance_cond(struct trace_array *tr, 1084 void *cond_data) 1085 { 1086 struct tracer *tracer = tr->current_trace; 1087 unsigned long flags; 1088 1089 if (in_nmi()) { 1090 internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n"); 1091 internal_trace_puts("*** snapshot is being ignored ***\n"); 1092 return; 1093 } 1094 1095 if (!tr->allocated_snapshot) { 1096 internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n"); 1097 internal_trace_puts("*** stopping trace here! ***\n"); 1098 tracing_off(); 1099 return; 1100 } 1101 1102 /* Note, snapshot can not be used when the tracer uses it */ 1103 if (tracer->use_max_tr) { 1104 internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n"); 1105 internal_trace_puts("*** Can not use snapshot (sorry) ***\n"); 1106 return; 1107 } 1108 1109 local_irq_save(flags); 1110 update_max_tr(tr, current, smp_processor_id(), cond_data); 1111 local_irq_restore(flags); 1112 } 1113 1114 void tracing_snapshot_instance(struct trace_array *tr) 1115 { 1116 tracing_snapshot_instance_cond(tr, NULL); 1117 } 1118 1119 /** 1120 * tracing_snapshot - take a snapshot of the current buffer. 1121 * 1122 * This causes a swap between the snapshot buffer and the current live 1123 * tracing buffer. You can use this to take snapshots of the live 1124 * trace when some condition is triggered, but continue to trace. 1125 * 1126 * Note, make sure to allocate the snapshot with either 1127 * a tracing_snapshot_alloc(), or by doing it manually 1128 * with: echo 1 > /sys/kernel/debug/tracing/snapshot 1129 * 1130 * If the snapshot buffer is not allocated, it will stop tracing. 1131 * Basically making a permanent snapshot. 1132 */ 1133 void tracing_snapshot(void) 1134 { 1135 struct trace_array *tr = &global_trace; 1136 1137 tracing_snapshot_instance(tr); 1138 } 1139 EXPORT_SYMBOL_GPL(tracing_snapshot); 1140 1141 /** 1142 * tracing_snapshot_cond - conditionally take a snapshot of the current buffer. 1143 * @tr: The tracing instance to snapshot 1144 * @cond_data: The data to be tested conditionally, and possibly saved 1145 * 1146 * This is the same as tracing_snapshot() except that the snapshot is 1147 * conditional - the snapshot will only happen if the 1148 * cond_snapshot.update() implementation receiving the cond_data 1149 * returns true, which means that the trace array's cond_snapshot 1150 * update() operation used the cond_data to determine whether the 1151 * snapshot should be taken, and if it was, presumably saved it along 1152 * with the snapshot. 1153 */ 1154 void tracing_snapshot_cond(struct trace_array *tr, void *cond_data) 1155 { 1156 tracing_snapshot_instance_cond(tr, cond_data); 1157 } 1158 EXPORT_SYMBOL_GPL(tracing_snapshot_cond); 1159 1160 /** 1161 * tracing_snapshot_cond_data - get the user data associated with a snapshot 1162 * @tr: The tracing instance 1163 * 1164 * When the user enables a conditional snapshot using 1165 * tracing_snapshot_cond_enable(), the user-defined cond_data is saved 1166 * with the snapshot. This accessor is used to retrieve it. 1167 * 1168 * Should not be called from cond_snapshot.update(), since it takes 1169 * the tr->max_lock lock, which the code calling 1170 * cond_snapshot.update() has already done. 1171 * 1172 * Returns the cond_data associated with the trace array's snapshot. 1173 */ 1174 void *tracing_cond_snapshot_data(struct trace_array *tr) 1175 { 1176 void *cond_data = NULL; 1177 1178 arch_spin_lock(&tr->max_lock); 1179 1180 if (tr->cond_snapshot) 1181 cond_data = tr->cond_snapshot->cond_data; 1182 1183 arch_spin_unlock(&tr->max_lock); 1184 1185 return cond_data; 1186 } 1187 EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data); 1188 1189 static int resize_buffer_duplicate_size(struct array_buffer *trace_buf, 1190 struct array_buffer *size_buf, int cpu_id); 1191 static void set_buffer_entries(struct array_buffer *buf, unsigned long val); 1192 1193 int tracing_alloc_snapshot_instance(struct trace_array *tr) 1194 { 1195 int ret; 1196 1197 if (!tr->allocated_snapshot) { 1198 1199 /* allocate spare buffer */ 1200 ret = resize_buffer_duplicate_size(&tr->max_buffer, 1201 &tr->array_buffer, RING_BUFFER_ALL_CPUS); 1202 if (ret < 0) 1203 return ret; 1204 1205 tr->allocated_snapshot = true; 1206 } 1207 1208 return 0; 1209 } 1210 1211 static void free_snapshot(struct trace_array *tr) 1212 { 1213 /* 1214 * We don't free the ring buffer. instead, resize it because 1215 * The max_tr ring buffer has some state (e.g. ring->clock) and 1216 * we want preserve it. 1217 */ 1218 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS); 1219 set_buffer_entries(&tr->max_buffer, 1); 1220 tracing_reset_online_cpus(&tr->max_buffer); 1221 tr->allocated_snapshot = false; 1222 } 1223 1224 /** 1225 * tracing_alloc_snapshot - allocate snapshot buffer. 1226 * 1227 * This only allocates the snapshot buffer if it isn't already 1228 * allocated - it doesn't also take a snapshot. 1229 * 1230 * This is meant to be used in cases where the snapshot buffer needs 1231 * to be set up for events that can't sleep but need to be able to 1232 * trigger a snapshot. 1233 */ 1234 int tracing_alloc_snapshot(void) 1235 { 1236 struct trace_array *tr = &global_trace; 1237 int ret; 1238 1239 ret = tracing_alloc_snapshot_instance(tr); 1240 WARN_ON(ret < 0); 1241 1242 return ret; 1243 } 1244 EXPORT_SYMBOL_GPL(tracing_alloc_snapshot); 1245 1246 /** 1247 * tracing_snapshot_alloc - allocate and take a snapshot of the current buffer. 1248 * 1249 * This is similar to tracing_snapshot(), but it will allocate the 1250 * snapshot buffer if it isn't already allocated. Use this only 1251 * where it is safe to sleep, as the allocation may sleep. 1252 * 1253 * This causes a swap between the snapshot buffer and the current live 1254 * tracing buffer. You can use this to take snapshots of the live 1255 * trace when some condition is triggered, but continue to trace. 1256 */ 1257 void tracing_snapshot_alloc(void) 1258 { 1259 int ret; 1260 1261 ret = tracing_alloc_snapshot(); 1262 if (ret < 0) 1263 return; 1264 1265 tracing_snapshot(); 1266 } 1267 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc); 1268 1269 /** 1270 * tracing_snapshot_cond_enable - enable conditional snapshot for an instance 1271 * @tr: The tracing instance 1272 * @cond_data: User data to associate with the snapshot 1273 * @update: Implementation of the cond_snapshot update function 1274 * 1275 * Check whether the conditional snapshot for the given instance has 1276 * already been enabled, or if the current tracer is already using a 1277 * snapshot; if so, return -EBUSY, else create a cond_snapshot and 1278 * save the cond_data and update function inside. 1279 * 1280 * Returns 0 if successful, error otherwise. 1281 */ 1282 int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data, 1283 cond_update_fn_t update) 1284 { 1285 struct cond_snapshot *cond_snapshot; 1286 int ret = 0; 1287 1288 cond_snapshot = kzalloc(sizeof(*cond_snapshot), GFP_KERNEL); 1289 if (!cond_snapshot) 1290 return -ENOMEM; 1291 1292 cond_snapshot->cond_data = cond_data; 1293 cond_snapshot->update = update; 1294 1295 mutex_lock(&trace_types_lock); 1296 1297 ret = tracing_alloc_snapshot_instance(tr); 1298 if (ret) 1299 goto fail_unlock; 1300 1301 if (tr->current_trace->use_max_tr) { 1302 ret = -EBUSY; 1303 goto fail_unlock; 1304 } 1305 1306 /* 1307 * The cond_snapshot can only change to NULL without the 1308 * trace_types_lock. We don't care if we race with it going 1309 * to NULL, but we want to make sure that it's not set to 1310 * something other than NULL when we get here, which we can 1311 * do safely with only holding the trace_types_lock and not 1312 * having to take the max_lock. 1313 */ 1314 if (tr->cond_snapshot) { 1315 ret = -EBUSY; 1316 goto fail_unlock; 1317 } 1318 1319 arch_spin_lock(&tr->max_lock); 1320 tr->cond_snapshot = cond_snapshot; 1321 arch_spin_unlock(&tr->max_lock); 1322 1323 mutex_unlock(&trace_types_lock); 1324 1325 return ret; 1326 1327 fail_unlock: 1328 mutex_unlock(&trace_types_lock); 1329 kfree(cond_snapshot); 1330 return ret; 1331 } 1332 EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable); 1333 1334 /** 1335 * tracing_snapshot_cond_disable - disable conditional snapshot for an instance 1336 * @tr: The tracing instance 1337 * 1338 * Check whether the conditional snapshot for the given instance is 1339 * enabled; if so, free the cond_snapshot associated with it, 1340 * otherwise return -EINVAL. 1341 * 1342 * Returns 0 if successful, error otherwise. 1343 */ 1344 int tracing_snapshot_cond_disable(struct trace_array *tr) 1345 { 1346 int ret = 0; 1347 1348 arch_spin_lock(&tr->max_lock); 1349 1350 if (!tr->cond_snapshot) 1351 ret = -EINVAL; 1352 else { 1353 kfree(tr->cond_snapshot); 1354 tr->cond_snapshot = NULL; 1355 } 1356 1357 arch_spin_unlock(&tr->max_lock); 1358 1359 return ret; 1360 } 1361 EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable); 1362 #else 1363 void tracing_snapshot(void) 1364 { 1365 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used"); 1366 } 1367 EXPORT_SYMBOL_GPL(tracing_snapshot); 1368 void tracing_snapshot_cond(struct trace_array *tr, void *cond_data) 1369 { 1370 WARN_ONCE(1, "Snapshot feature not enabled, but internal conditional snapshot used"); 1371 } 1372 EXPORT_SYMBOL_GPL(tracing_snapshot_cond); 1373 int tracing_alloc_snapshot(void) 1374 { 1375 WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used"); 1376 return -ENODEV; 1377 } 1378 EXPORT_SYMBOL_GPL(tracing_alloc_snapshot); 1379 void tracing_snapshot_alloc(void) 1380 { 1381 /* Give warning */ 1382 tracing_snapshot(); 1383 } 1384 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc); 1385 void *tracing_cond_snapshot_data(struct trace_array *tr) 1386 { 1387 return NULL; 1388 } 1389 EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data); 1390 int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data, cond_update_fn_t update) 1391 { 1392 return -ENODEV; 1393 } 1394 EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable); 1395 int tracing_snapshot_cond_disable(struct trace_array *tr) 1396 { 1397 return false; 1398 } 1399 EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable); 1400 #endif /* CONFIG_TRACER_SNAPSHOT */ 1401 1402 void tracer_tracing_off(struct trace_array *tr) 1403 { 1404 if (tr->array_buffer.buffer) 1405 ring_buffer_record_off(tr->array_buffer.buffer); 1406 /* 1407 * This flag is looked at when buffers haven't been allocated 1408 * yet, or by some tracers (like irqsoff), that just want to 1409 * know if the ring buffer has been disabled, but it can handle 1410 * races of where it gets disabled but we still do a record. 1411 * As the check is in the fast path of the tracers, it is more 1412 * important to be fast than accurate. 1413 */ 1414 tr->buffer_disabled = 1; 1415 /* Make the flag seen by readers */ 1416 smp_wmb(); 1417 } 1418 1419 /** 1420 * tracing_off - turn off tracing buffers 1421 * 1422 * This function stops the tracing buffers from recording data. 1423 * It does not disable any overhead the tracers themselves may 1424 * be causing. This function simply causes all recording to 1425 * the ring buffers to fail. 1426 */ 1427 void tracing_off(void) 1428 { 1429 tracer_tracing_off(&global_trace); 1430 } 1431 EXPORT_SYMBOL_GPL(tracing_off); 1432 1433 void disable_trace_on_warning(void) 1434 { 1435 if (__disable_trace_on_warning) { 1436 trace_array_printk_buf(global_trace.array_buffer.buffer, _THIS_IP_, 1437 "Disabling tracing due to warning\n"); 1438 tracing_off(); 1439 } 1440 } 1441 1442 /** 1443 * tracer_tracing_is_on - show real state of ring buffer enabled 1444 * @tr : the trace array to know if ring buffer is enabled 1445 * 1446 * Shows real state of the ring buffer if it is enabled or not. 1447 */ 1448 bool tracer_tracing_is_on(struct trace_array *tr) 1449 { 1450 if (tr->array_buffer.buffer) 1451 return ring_buffer_record_is_on(tr->array_buffer.buffer); 1452 return !tr->buffer_disabled; 1453 } 1454 1455 /** 1456 * tracing_is_on - show state of ring buffers enabled 1457 */ 1458 int tracing_is_on(void) 1459 { 1460 return tracer_tracing_is_on(&global_trace); 1461 } 1462 EXPORT_SYMBOL_GPL(tracing_is_on); 1463 1464 static int __init set_buf_size(char *str) 1465 { 1466 unsigned long buf_size; 1467 1468 if (!str) 1469 return 0; 1470 buf_size = memparse(str, &str); 1471 /* nr_entries can not be zero */ 1472 if (buf_size == 0) 1473 return 0; 1474 trace_buf_size = buf_size; 1475 return 1; 1476 } 1477 __setup("trace_buf_size=", set_buf_size); 1478 1479 static int __init set_tracing_thresh(char *str) 1480 { 1481 unsigned long threshold; 1482 int ret; 1483 1484 if (!str) 1485 return 0; 1486 ret = kstrtoul(str, 0, &threshold); 1487 if (ret < 0) 1488 return 0; 1489 tracing_thresh = threshold * 1000; 1490 return 1; 1491 } 1492 __setup("tracing_thresh=", set_tracing_thresh); 1493 1494 unsigned long nsecs_to_usecs(unsigned long nsecs) 1495 { 1496 return nsecs / 1000; 1497 } 1498 1499 /* 1500 * TRACE_FLAGS is defined as a tuple matching bit masks with strings. 1501 * It uses C(a, b) where 'a' is the eval (enum) name and 'b' is the string that 1502 * matches it. By defining "C(a, b) b", TRACE_FLAGS becomes a list 1503 * of strings in the order that the evals (enum) were defined. 1504 */ 1505 #undef C 1506 #define C(a, b) b 1507 1508 /* These must match the bit positions in trace_iterator_flags */ 1509 static const char *trace_options[] = { 1510 TRACE_FLAGS 1511 NULL 1512 }; 1513 1514 static struct { 1515 u64 (*func)(void); 1516 const char *name; 1517 int in_ns; /* is this clock in nanoseconds? */ 1518 } trace_clocks[] = { 1519 { trace_clock_local, "local", 1 }, 1520 { trace_clock_global, "global", 1 }, 1521 { trace_clock_counter, "counter", 0 }, 1522 { trace_clock_jiffies, "uptime", 0 }, 1523 { trace_clock, "perf", 1 }, 1524 { ktime_get_mono_fast_ns, "mono", 1 }, 1525 { ktime_get_raw_fast_ns, "mono_raw", 1 }, 1526 { ktime_get_boot_fast_ns, "boot", 1 }, 1527 ARCH_TRACE_CLOCKS 1528 }; 1529 1530 bool trace_clock_in_ns(struct trace_array *tr) 1531 { 1532 if (trace_clocks[tr->clock_id].in_ns) 1533 return true; 1534 1535 return false; 1536 } 1537 1538 /* 1539 * trace_parser_get_init - gets the buffer for trace parser 1540 */ 1541 int trace_parser_get_init(struct trace_parser *parser, int size) 1542 { 1543 memset(parser, 0, sizeof(*parser)); 1544 1545 parser->buffer = kmalloc(size, GFP_KERNEL); 1546 if (!parser->buffer) 1547 return 1; 1548 1549 parser->size = size; 1550 return 0; 1551 } 1552 1553 /* 1554 * trace_parser_put - frees the buffer for trace parser 1555 */ 1556 void trace_parser_put(struct trace_parser *parser) 1557 { 1558 kfree(parser->buffer); 1559 parser->buffer = NULL; 1560 } 1561 1562 /* 1563 * trace_get_user - reads the user input string separated by space 1564 * (matched by isspace(ch)) 1565 * 1566 * For each string found the 'struct trace_parser' is updated, 1567 * and the function returns. 1568 * 1569 * Returns number of bytes read. 1570 * 1571 * See kernel/trace/trace.h for 'struct trace_parser' details. 1572 */ 1573 int trace_get_user(struct trace_parser *parser, const char __user *ubuf, 1574 size_t cnt, loff_t *ppos) 1575 { 1576 char ch; 1577 size_t read = 0; 1578 ssize_t ret; 1579 1580 if (!*ppos) 1581 trace_parser_clear(parser); 1582 1583 ret = get_user(ch, ubuf++); 1584 if (ret) 1585 goto out; 1586 1587 read++; 1588 cnt--; 1589 1590 /* 1591 * The parser is not finished with the last write, 1592 * continue reading the user input without skipping spaces. 1593 */ 1594 if (!parser->cont) { 1595 /* skip white space */ 1596 while (cnt && isspace(ch)) { 1597 ret = get_user(ch, ubuf++); 1598 if (ret) 1599 goto out; 1600 read++; 1601 cnt--; 1602 } 1603 1604 parser->idx = 0; 1605 1606 /* only spaces were written */ 1607 if (isspace(ch) || !ch) { 1608 *ppos += read; 1609 ret = read; 1610 goto out; 1611 } 1612 } 1613 1614 /* read the non-space input */ 1615 while (cnt && !isspace(ch) && ch) { 1616 if (parser->idx < parser->size - 1) 1617 parser->buffer[parser->idx++] = ch; 1618 else { 1619 ret = -EINVAL; 1620 goto out; 1621 } 1622 ret = get_user(ch, ubuf++); 1623 if (ret) 1624 goto out; 1625 read++; 1626 cnt--; 1627 } 1628 1629 /* We either got finished input or we have to wait for another call. */ 1630 if (isspace(ch) || !ch) { 1631 parser->buffer[parser->idx] = 0; 1632 parser->cont = false; 1633 } else if (parser->idx < parser->size - 1) { 1634 parser->cont = true; 1635 parser->buffer[parser->idx++] = ch; 1636 /* Make sure the parsed string always terminates with '\0'. */ 1637 parser->buffer[parser->idx] = 0; 1638 } else { 1639 ret = -EINVAL; 1640 goto out; 1641 } 1642 1643 *ppos += read; 1644 ret = read; 1645 1646 out: 1647 return ret; 1648 } 1649 1650 /* TODO add a seq_buf_to_buffer() */ 1651 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt) 1652 { 1653 int len; 1654 1655 if (trace_seq_used(s) <= s->seq.readpos) 1656 return -EBUSY; 1657 1658 len = trace_seq_used(s) - s->seq.readpos; 1659 if (cnt > len) 1660 cnt = len; 1661 memcpy(buf, s->buffer + s->seq.readpos, cnt); 1662 1663 s->seq.readpos += cnt; 1664 return cnt; 1665 } 1666 1667 unsigned long __read_mostly tracing_thresh; 1668 static const struct file_operations tracing_max_lat_fops; 1669 1670 #ifdef LATENCY_FS_NOTIFY 1671 1672 static struct workqueue_struct *fsnotify_wq; 1673 1674 static void latency_fsnotify_workfn(struct work_struct *work) 1675 { 1676 struct trace_array *tr = container_of(work, struct trace_array, 1677 fsnotify_work); 1678 fsnotify_inode(tr->d_max_latency->d_inode, FS_MODIFY); 1679 } 1680 1681 static void latency_fsnotify_workfn_irq(struct irq_work *iwork) 1682 { 1683 struct trace_array *tr = container_of(iwork, struct trace_array, 1684 fsnotify_irqwork); 1685 queue_work(fsnotify_wq, &tr->fsnotify_work); 1686 } 1687 1688 static void trace_create_maxlat_file(struct trace_array *tr, 1689 struct dentry *d_tracer) 1690 { 1691 INIT_WORK(&tr->fsnotify_work, latency_fsnotify_workfn); 1692 init_irq_work(&tr->fsnotify_irqwork, latency_fsnotify_workfn_irq); 1693 tr->d_max_latency = trace_create_file("tracing_max_latency", 1694 TRACE_MODE_WRITE, 1695 d_tracer, &tr->max_latency, 1696 &tracing_max_lat_fops); 1697 } 1698 1699 __init static int latency_fsnotify_init(void) 1700 { 1701 fsnotify_wq = alloc_workqueue("tr_max_lat_wq", 1702 WQ_UNBOUND | WQ_HIGHPRI, 0); 1703 if (!fsnotify_wq) { 1704 pr_err("Unable to allocate tr_max_lat_wq\n"); 1705 return -ENOMEM; 1706 } 1707 return 0; 1708 } 1709 1710 late_initcall_sync(latency_fsnotify_init); 1711 1712 void latency_fsnotify(struct trace_array *tr) 1713 { 1714 if (!fsnotify_wq) 1715 return; 1716 /* 1717 * We cannot call queue_work(&tr->fsnotify_work) from here because it's 1718 * possible that we are called from __schedule() or do_idle(), which 1719 * could cause a deadlock. 1720 */ 1721 irq_work_queue(&tr->fsnotify_irqwork); 1722 } 1723 1724 /* 1725 * (defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)) && \ 1726 * defined(CONFIG_FSNOTIFY) 1727 */ 1728 #else 1729 1730 #define trace_create_maxlat_file(tr, d_tracer) \ 1731 trace_create_file("tracing_max_latency", TRACE_MODE_WRITE, \ 1732 d_tracer, &tr->max_latency, &tracing_max_lat_fops) 1733 1734 #endif 1735 1736 #ifdef CONFIG_TRACER_MAX_TRACE 1737 /* 1738 * Copy the new maximum trace into the separate maximum-trace 1739 * structure. (this way the maximum trace is permanently saved, 1740 * for later retrieval via /sys/kernel/tracing/tracing_max_latency) 1741 */ 1742 static void 1743 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu) 1744 { 1745 struct array_buffer *trace_buf = &tr->array_buffer; 1746 struct array_buffer *max_buf = &tr->max_buffer; 1747 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu); 1748 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu); 1749 1750 max_buf->cpu = cpu; 1751 max_buf->time_start = data->preempt_timestamp; 1752 1753 max_data->saved_latency = tr->max_latency; 1754 max_data->critical_start = data->critical_start; 1755 max_data->critical_end = data->critical_end; 1756 1757 strncpy(max_data->comm, tsk->comm, TASK_COMM_LEN); 1758 max_data->pid = tsk->pid; 1759 /* 1760 * If tsk == current, then use current_uid(), as that does not use 1761 * RCU. The irq tracer can be called out of RCU scope. 1762 */ 1763 if (tsk == current) 1764 max_data->uid = current_uid(); 1765 else 1766 max_data->uid = task_uid(tsk); 1767 1768 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO; 1769 max_data->policy = tsk->policy; 1770 max_data->rt_priority = tsk->rt_priority; 1771 1772 /* record this tasks comm */ 1773 tracing_record_cmdline(tsk); 1774 latency_fsnotify(tr); 1775 } 1776 1777 /** 1778 * update_max_tr - snapshot all trace buffers from global_trace to max_tr 1779 * @tr: tracer 1780 * @tsk: the task with the latency 1781 * @cpu: The cpu that initiated the trace. 1782 * @cond_data: User data associated with a conditional snapshot 1783 * 1784 * Flip the buffers between the @tr and the max_tr and record information 1785 * about which task was the cause of this latency. 1786 */ 1787 void 1788 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu, 1789 void *cond_data) 1790 { 1791 if (tr->stop_count) 1792 return; 1793 1794 WARN_ON_ONCE(!irqs_disabled()); 1795 1796 if (!tr->allocated_snapshot) { 1797 /* Only the nop tracer should hit this when disabling */ 1798 WARN_ON_ONCE(tr->current_trace != &nop_trace); 1799 return; 1800 } 1801 1802 arch_spin_lock(&tr->max_lock); 1803 1804 /* Inherit the recordable setting from array_buffer */ 1805 if (ring_buffer_record_is_set_on(tr->array_buffer.buffer)) 1806 ring_buffer_record_on(tr->max_buffer.buffer); 1807 else 1808 ring_buffer_record_off(tr->max_buffer.buffer); 1809 1810 #ifdef CONFIG_TRACER_SNAPSHOT 1811 if (tr->cond_snapshot && !tr->cond_snapshot->update(tr, cond_data)) 1812 goto out_unlock; 1813 #endif 1814 swap(tr->array_buffer.buffer, tr->max_buffer.buffer); 1815 1816 __update_max_tr(tr, tsk, cpu); 1817 1818 out_unlock: 1819 arch_spin_unlock(&tr->max_lock); 1820 } 1821 1822 /** 1823 * update_max_tr_single - only copy one trace over, and reset the rest 1824 * @tr: tracer 1825 * @tsk: task with the latency 1826 * @cpu: the cpu of the buffer to copy. 1827 * 1828 * Flip the trace of a single CPU buffer between the @tr and the max_tr. 1829 */ 1830 void 1831 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu) 1832 { 1833 int ret; 1834 1835 if (tr->stop_count) 1836 return; 1837 1838 WARN_ON_ONCE(!irqs_disabled()); 1839 if (!tr->allocated_snapshot) { 1840 /* Only the nop tracer should hit this when disabling */ 1841 WARN_ON_ONCE(tr->current_trace != &nop_trace); 1842 return; 1843 } 1844 1845 arch_spin_lock(&tr->max_lock); 1846 1847 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->array_buffer.buffer, cpu); 1848 1849 if (ret == -EBUSY) { 1850 /* 1851 * We failed to swap the buffer due to a commit taking 1852 * place on this CPU. We fail to record, but we reset 1853 * the max trace buffer (no one writes directly to it) 1854 * and flag that it failed. 1855 */ 1856 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_, 1857 "Failed to swap buffers due to commit in progress\n"); 1858 } 1859 1860 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY); 1861 1862 __update_max_tr(tr, tsk, cpu); 1863 arch_spin_unlock(&tr->max_lock); 1864 } 1865 #endif /* CONFIG_TRACER_MAX_TRACE */ 1866 1867 static int wait_on_pipe(struct trace_iterator *iter, int full) 1868 { 1869 /* Iterators are static, they should be filled or empty */ 1870 if (trace_buffer_iter(iter, iter->cpu_file)) 1871 return 0; 1872 1873 return ring_buffer_wait(iter->array_buffer->buffer, iter->cpu_file, 1874 full); 1875 } 1876 1877 #ifdef CONFIG_FTRACE_STARTUP_TEST 1878 static bool selftests_can_run; 1879 1880 struct trace_selftests { 1881 struct list_head list; 1882 struct tracer *type; 1883 }; 1884 1885 static LIST_HEAD(postponed_selftests); 1886 1887 static int save_selftest(struct tracer *type) 1888 { 1889 struct trace_selftests *selftest; 1890 1891 selftest = kmalloc(sizeof(*selftest), GFP_KERNEL); 1892 if (!selftest) 1893 return -ENOMEM; 1894 1895 selftest->type = type; 1896 list_add(&selftest->list, &postponed_selftests); 1897 return 0; 1898 } 1899 1900 static int run_tracer_selftest(struct tracer *type) 1901 { 1902 struct trace_array *tr = &global_trace; 1903 struct tracer *saved_tracer = tr->current_trace; 1904 int ret; 1905 1906 if (!type->selftest || tracing_selftest_disabled) 1907 return 0; 1908 1909 /* 1910 * If a tracer registers early in boot up (before scheduling is 1911 * initialized and such), then do not run its selftests yet. 1912 * Instead, run it a little later in the boot process. 1913 */ 1914 if (!selftests_can_run) 1915 return save_selftest(type); 1916 1917 if (!tracing_is_on()) { 1918 pr_warn("Selftest for tracer %s skipped due to tracing disabled\n", 1919 type->name); 1920 return 0; 1921 } 1922 1923 /* 1924 * Run a selftest on this tracer. 1925 * Here we reset the trace buffer, and set the current 1926 * tracer to be this tracer. The tracer can then run some 1927 * internal tracing to verify that everything is in order. 1928 * If we fail, we do not register this tracer. 1929 */ 1930 tracing_reset_online_cpus(&tr->array_buffer); 1931 1932 tr->current_trace = type; 1933 1934 #ifdef CONFIG_TRACER_MAX_TRACE 1935 if (type->use_max_tr) { 1936 /* If we expanded the buffers, make sure the max is expanded too */ 1937 if (ring_buffer_expanded) 1938 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size, 1939 RING_BUFFER_ALL_CPUS); 1940 tr->allocated_snapshot = true; 1941 } 1942 #endif 1943 1944 /* the test is responsible for initializing and enabling */ 1945 pr_info("Testing tracer %s: ", type->name); 1946 ret = type->selftest(type, tr); 1947 /* the test is responsible for resetting too */ 1948 tr->current_trace = saved_tracer; 1949 if (ret) { 1950 printk(KERN_CONT "FAILED!\n"); 1951 /* Add the warning after printing 'FAILED' */ 1952 WARN_ON(1); 1953 return -1; 1954 } 1955 /* Only reset on passing, to avoid touching corrupted buffers */ 1956 tracing_reset_online_cpus(&tr->array_buffer); 1957 1958 #ifdef CONFIG_TRACER_MAX_TRACE 1959 if (type->use_max_tr) { 1960 tr->allocated_snapshot = false; 1961 1962 /* Shrink the max buffer again */ 1963 if (ring_buffer_expanded) 1964 ring_buffer_resize(tr->max_buffer.buffer, 1, 1965 RING_BUFFER_ALL_CPUS); 1966 } 1967 #endif 1968 1969 printk(KERN_CONT "PASSED\n"); 1970 return 0; 1971 } 1972 1973 static __init int init_trace_selftests(void) 1974 { 1975 struct trace_selftests *p, *n; 1976 struct tracer *t, **last; 1977 int ret; 1978 1979 selftests_can_run = true; 1980 1981 mutex_lock(&trace_types_lock); 1982 1983 if (list_empty(&postponed_selftests)) 1984 goto out; 1985 1986 pr_info("Running postponed tracer tests:\n"); 1987 1988 tracing_selftest_running = true; 1989 list_for_each_entry_safe(p, n, &postponed_selftests, list) { 1990 /* This loop can take minutes when sanitizers are enabled, so 1991 * lets make sure we allow RCU processing. 1992 */ 1993 cond_resched(); 1994 ret = run_tracer_selftest(p->type); 1995 /* If the test fails, then warn and remove from available_tracers */ 1996 if (ret < 0) { 1997 WARN(1, "tracer: %s failed selftest, disabling\n", 1998 p->type->name); 1999 last = &trace_types; 2000 for (t = trace_types; t; t = t->next) { 2001 if (t == p->type) { 2002 *last = t->next; 2003 break; 2004 } 2005 last = &t->next; 2006 } 2007 } 2008 list_del(&p->list); 2009 kfree(p); 2010 } 2011 tracing_selftest_running = false; 2012 2013 out: 2014 mutex_unlock(&trace_types_lock); 2015 2016 return 0; 2017 } 2018 core_initcall(init_trace_selftests); 2019 #else 2020 static inline int run_tracer_selftest(struct tracer *type) 2021 { 2022 return 0; 2023 } 2024 #endif /* CONFIG_FTRACE_STARTUP_TEST */ 2025 2026 static void add_tracer_options(struct trace_array *tr, struct tracer *t); 2027 2028 static void __init apply_trace_boot_options(void); 2029 2030 /** 2031 * register_tracer - register a tracer with the ftrace system. 2032 * @type: the plugin for the tracer 2033 * 2034 * Register a new plugin tracer. 2035 */ 2036 int __init register_tracer(struct tracer *type) 2037 { 2038 struct tracer *t; 2039 int ret = 0; 2040 2041 if (!type->name) { 2042 pr_info("Tracer must have a name\n"); 2043 return -1; 2044 } 2045 2046 if (strlen(type->name) >= MAX_TRACER_SIZE) { 2047 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE); 2048 return -1; 2049 } 2050 2051 if (security_locked_down(LOCKDOWN_TRACEFS)) { 2052 pr_warn("Can not register tracer %s due to lockdown\n", 2053 type->name); 2054 return -EPERM; 2055 } 2056 2057 mutex_lock(&trace_types_lock); 2058 2059 tracing_selftest_running = true; 2060 2061 for (t = trace_types; t; t = t->next) { 2062 if (strcmp(type->name, t->name) == 0) { 2063 /* already found */ 2064 pr_info("Tracer %s already registered\n", 2065 type->name); 2066 ret = -1; 2067 goto out; 2068 } 2069 } 2070 2071 if (!type->set_flag) 2072 type->set_flag = &dummy_set_flag; 2073 if (!type->flags) { 2074 /*allocate a dummy tracer_flags*/ 2075 type->flags = kmalloc(sizeof(*type->flags), GFP_KERNEL); 2076 if (!type->flags) { 2077 ret = -ENOMEM; 2078 goto out; 2079 } 2080 type->flags->val = 0; 2081 type->flags->opts = dummy_tracer_opt; 2082 } else 2083 if (!type->flags->opts) 2084 type->flags->opts = dummy_tracer_opt; 2085 2086 /* store the tracer for __set_tracer_option */ 2087 type->flags->trace = type; 2088 2089 ret = run_tracer_selftest(type); 2090 if (ret < 0) 2091 goto out; 2092 2093 type->next = trace_types; 2094 trace_types = type; 2095 add_tracer_options(&global_trace, type); 2096 2097 out: 2098 tracing_selftest_running = false; 2099 mutex_unlock(&trace_types_lock); 2100 2101 if (ret || !default_bootup_tracer) 2102 goto out_unlock; 2103 2104 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE)) 2105 goto out_unlock; 2106 2107 printk(KERN_INFO "Starting tracer '%s'\n", type->name); 2108 /* Do we want this tracer to start on bootup? */ 2109 tracing_set_tracer(&global_trace, type->name); 2110 default_bootup_tracer = NULL; 2111 2112 apply_trace_boot_options(); 2113 2114 /* disable other selftests, since this will break it. */ 2115 disable_tracing_selftest("running a tracer"); 2116 2117 out_unlock: 2118 return ret; 2119 } 2120 2121 static void tracing_reset_cpu(struct array_buffer *buf, int cpu) 2122 { 2123 struct trace_buffer *buffer = buf->buffer; 2124 2125 if (!buffer) 2126 return; 2127 2128 ring_buffer_record_disable(buffer); 2129 2130 /* Make sure all commits have finished */ 2131 synchronize_rcu(); 2132 ring_buffer_reset_cpu(buffer, cpu); 2133 2134 ring_buffer_record_enable(buffer); 2135 } 2136 2137 void tracing_reset_online_cpus(struct array_buffer *buf) 2138 { 2139 struct trace_buffer *buffer = buf->buffer; 2140 2141 if (!buffer) 2142 return; 2143 2144 ring_buffer_record_disable(buffer); 2145 2146 /* Make sure all commits have finished */ 2147 synchronize_rcu(); 2148 2149 buf->time_start = buffer_ftrace_now(buf, buf->cpu); 2150 2151 ring_buffer_reset_online_cpus(buffer); 2152 2153 ring_buffer_record_enable(buffer); 2154 } 2155 2156 /* Must have trace_types_lock held */ 2157 void tracing_reset_all_online_cpus(void) 2158 { 2159 struct trace_array *tr; 2160 2161 list_for_each_entry(tr, &ftrace_trace_arrays, list) { 2162 if (!tr->clear_trace) 2163 continue; 2164 tr->clear_trace = false; 2165 tracing_reset_online_cpus(&tr->array_buffer); 2166 #ifdef CONFIG_TRACER_MAX_TRACE 2167 tracing_reset_online_cpus(&tr->max_buffer); 2168 #endif 2169 } 2170 } 2171 2172 /* 2173 * The tgid_map array maps from pid to tgid; i.e. the value stored at index i 2174 * is the tgid last observed corresponding to pid=i. 2175 */ 2176 static int *tgid_map; 2177 2178 /* The maximum valid index into tgid_map. */ 2179 static size_t tgid_map_max; 2180 2181 #define SAVED_CMDLINES_DEFAULT 128 2182 #define NO_CMDLINE_MAP UINT_MAX 2183 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED; 2184 struct saved_cmdlines_buffer { 2185 unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1]; 2186 unsigned *map_cmdline_to_pid; 2187 unsigned cmdline_num; 2188 int cmdline_idx; 2189 char *saved_cmdlines; 2190 }; 2191 static struct saved_cmdlines_buffer *savedcmd; 2192 2193 static inline char *get_saved_cmdlines(int idx) 2194 { 2195 return &savedcmd->saved_cmdlines[idx * TASK_COMM_LEN]; 2196 } 2197 2198 static inline void set_cmdline(int idx, const char *cmdline) 2199 { 2200 strncpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN); 2201 } 2202 2203 static int allocate_cmdlines_buffer(unsigned int val, 2204 struct saved_cmdlines_buffer *s) 2205 { 2206 s->map_cmdline_to_pid = kmalloc_array(val, 2207 sizeof(*s->map_cmdline_to_pid), 2208 GFP_KERNEL); 2209 if (!s->map_cmdline_to_pid) 2210 return -ENOMEM; 2211 2212 s->saved_cmdlines = kmalloc_array(TASK_COMM_LEN, val, GFP_KERNEL); 2213 if (!s->saved_cmdlines) { 2214 kfree(s->map_cmdline_to_pid); 2215 return -ENOMEM; 2216 } 2217 2218 s->cmdline_idx = 0; 2219 s->cmdline_num = val; 2220 memset(&s->map_pid_to_cmdline, NO_CMDLINE_MAP, 2221 sizeof(s->map_pid_to_cmdline)); 2222 memset(s->map_cmdline_to_pid, NO_CMDLINE_MAP, 2223 val * sizeof(*s->map_cmdline_to_pid)); 2224 2225 return 0; 2226 } 2227 2228 static int trace_create_savedcmd(void) 2229 { 2230 int ret; 2231 2232 savedcmd = kmalloc(sizeof(*savedcmd), GFP_KERNEL); 2233 if (!savedcmd) 2234 return -ENOMEM; 2235 2236 ret = allocate_cmdlines_buffer(SAVED_CMDLINES_DEFAULT, savedcmd); 2237 if (ret < 0) { 2238 kfree(savedcmd); 2239 savedcmd = NULL; 2240 return -ENOMEM; 2241 } 2242 2243 return 0; 2244 } 2245 2246 int is_tracing_stopped(void) 2247 { 2248 return global_trace.stop_count; 2249 } 2250 2251 /** 2252 * tracing_start - quick start of the tracer 2253 * 2254 * If tracing is enabled but was stopped by tracing_stop, 2255 * this will start the tracer back up. 2256 */ 2257 void tracing_start(void) 2258 { 2259 struct trace_buffer *buffer; 2260 unsigned long flags; 2261 2262 if (tracing_disabled) 2263 return; 2264 2265 raw_spin_lock_irqsave(&global_trace.start_lock, flags); 2266 if (--global_trace.stop_count) { 2267 if (global_trace.stop_count < 0) { 2268 /* Someone screwed up their debugging */ 2269 WARN_ON_ONCE(1); 2270 global_trace.stop_count = 0; 2271 } 2272 goto out; 2273 } 2274 2275 /* Prevent the buffers from switching */ 2276 arch_spin_lock(&global_trace.max_lock); 2277 2278 buffer = global_trace.array_buffer.buffer; 2279 if (buffer) 2280 ring_buffer_record_enable(buffer); 2281 2282 #ifdef CONFIG_TRACER_MAX_TRACE 2283 buffer = global_trace.max_buffer.buffer; 2284 if (buffer) 2285 ring_buffer_record_enable(buffer); 2286 #endif 2287 2288 arch_spin_unlock(&global_trace.max_lock); 2289 2290 out: 2291 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags); 2292 } 2293 2294 static void tracing_start_tr(struct trace_array *tr) 2295 { 2296 struct trace_buffer *buffer; 2297 unsigned long flags; 2298 2299 if (tracing_disabled) 2300 return; 2301 2302 /* If global, we need to also start the max tracer */ 2303 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) 2304 return tracing_start(); 2305 2306 raw_spin_lock_irqsave(&tr->start_lock, flags); 2307 2308 if (--tr->stop_count) { 2309 if (tr->stop_count < 0) { 2310 /* Someone screwed up their debugging */ 2311 WARN_ON_ONCE(1); 2312 tr->stop_count = 0; 2313 } 2314 goto out; 2315 } 2316 2317 buffer = tr->array_buffer.buffer; 2318 if (buffer) 2319 ring_buffer_record_enable(buffer); 2320 2321 out: 2322 raw_spin_unlock_irqrestore(&tr->start_lock, flags); 2323 } 2324 2325 /** 2326 * tracing_stop - quick stop of the tracer 2327 * 2328 * Light weight way to stop tracing. Use in conjunction with 2329 * tracing_start. 2330 */ 2331 void tracing_stop(void) 2332 { 2333 struct trace_buffer *buffer; 2334 unsigned long flags; 2335 2336 raw_spin_lock_irqsave(&global_trace.start_lock, flags); 2337 if (global_trace.stop_count++) 2338 goto out; 2339 2340 /* Prevent the buffers from switching */ 2341 arch_spin_lock(&global_trace.max_lock); 2342 2343 buffer = global_trace.array_buffer.buffer; 2344 if (buffer) 2345 ring_buffer_record_disable(buffer); 2346 2347 #ifdef CONFIG_TRACER_MAX_TRACE 2348 buffer = global_trace.max_buffer.buffer; 2349 if (buffer) 2350 ring_buffer_record_disable(buffer); 2351 #endif 2352 2353 arch_spin_unlock(&global_trace.max_lock); 2354 2355 out: 2356 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags); 2357 } 2358 2359 static void tracing_stop_tr(struct trace_array *tr) 2360 { 2361 struct trace_buffer *buffer; 2362 unsigned long flags; 2363 2364 /* If global, we need to also stop the max tracer */ 2365 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) 2366 return tracing_stop(); 2367 2368 raw_spin_lock_irqsave(&tr->start_lock, flags); 2369 if (tr->stop_count++) 2370 goto out; 2371 2372 buffer = tr->array_buffer.buffer; 2373 if (buffer) 2374 ring_buffer_record_disable(buffer); 2375 2376 out: 2377 raw_spin_unlock_irqrestore(&tr->start_lock, flags); 2378 } 2379 2380 static int trace_save_cmdline(struct task_struct *tsk) 2381 { 2382 unsigned tpid, idx; 2383 2384 /* treat recording of idle task as a success */ 2385 if (!tsk->pid) 2386 return 1; 2387 2388 tpid = tsk->pid & (PID_MAX_DEFAULT - 1); 2389 2390 /* 2391 * It's not the end of the world if we don't get 2392 * the lock, but we also don't want to spin 2393 * nor do we want to disable interrupts, 2394 * so if we miss here, then better luck next time. 2395 */ 2396 if (!arch_spin_trylock(&trace_cmdline_lock)) 2397 return 0; 2398 2399 idx = savedcmd->map_pid_to_cmdline[tpid]; 2400 if (idx == NO_CMDLINE_MAP) { 2401 idx = (savedcmd->cmdline_idx + 1) % savedcmd->cmdline_num; 2402 2403 savedcmd->map_pid_to_cmdline[tpid] = idx; 2404 savedcmd->cmdline_idx = idx; 2405 } 2406 2407 savedcmd->map_cmdline_to_pid[idx] = tsk->pid; 2408 set_cmdline(idx, tsk->comm); 2409 2410 arch_spin_unlock(&trace_cmdline_lock); 2411 2412 return 1; 2413 } 2414 2415 static void __trace_find_cmdline(int pid, char comm[]) 2416 { 2417 unsigned map; 2418 int tpid; 2419 2420 if (!pid) { 2421 strcpy(comm, "<idle>"); 2422 return; 2423 } 2424 2425 if (WARN_ON_ONCE(pid < 0)) { 2426 strcpy(comm, "<XXX>"); 2427 return; 2428 } 2429 2430 tpid = pid & (PID_MAX_DEFAULT - 1); 2431 map = savedcmd->map_pid_to_cmdline[tpid]; 2432 if (map != NO_CMDLINE_MAP) { 2433 tpid = savedcmd->map_cmdline_to_pid[map]; 2434 if (tpid == pid) { 2435 strlcpy(comm, get_saved_cmdlines(map), TASK_COMM_LEN); 2436 return; 2437 } 2438 } 2439 strcpy(comm, "<...>"); 2440 } 2441 2442 void trace_find_cmdline(int pid, char comm[]) 2443 { 2444 preempt_disable(); 2445 arch_spin_lock(&trace_cmdline_lock); 2446 2447 __trace_find_cmdline(pid, comm); 2448 2449 arch_spin_unlock(&trace_cmdline_lock); 2450 preempt_enable(); 2451 } 2452 2453 static int *trace_find_tgid_ptr(int pid) 2454 { 2455 /* 2456 * Pairs with the smp_store_release in set_tracer_flag() to ensure that 2457 * if we observe a non-NULL tgid_map then we also observe the correct 2458 * tgid_map_max. 2459 */ 2460 int *map = smp_load_acquire(&tgid_map); 2461 2462 if (unlikely(!map || pid > tgid_map_max)) 2463 return NULL; 2464 2465 return &map[pid]; 2466 } 2467 2468 int trace_find_tgid(int pid) 2469 { 2470 int *ptr = trace_find_tgid_ptr(pid); 2471 2472 return ptr ? *ptr : 0; 2473 } 2474 2475 static int trace_save_tgid(struct task_struct *tsk) 2476 { 2477 int *ptr; 2478 2479 /* treat recording of idle task as a success */ 2480 if (!tsk->pid) 2481 return 1; 2482 2483 ptr = trace_find_tgid_ptr(tsk->pid); 2484 if (!ptr) 2485 return 0; 2486 2487 *ptr = tsk->tgid; 2488 return 1; 2489 } 2490 2491 static bool tracing_record_taskinfo_skip(int flags) 2492 { 2493 if (unlikely(!(flags & (TRACE_RECORD_CMDLINE | TRACE_RECORD_TGID)))) 2494 return true; 2495 if (!__this_cpu_read(trace_taskinfo_save)) 2496 return true; 2497 return false; 2498 } 2499 2500 /** 2501 * tracing_record_taskinfo - record the task info of a task 2502 * 2503 * @task: task to record 2504 * @flags: TRACE_RECORD_CMDLINE for recording comm 2505 * TRACE_RECORD_TGID for recording tgid 2506 */ 2507 void tracing_record_taskinfo(struct task_struct *task, int flags) 2508 { 2509 bool done; 2510 2511 if (tracing_record_taskinfo_skip(flags)) 2512 return; 2513 2514 /* 2515 * Record as much task information as possible. If some fail, continue 2516 * to try to record the others. 2517 */ 2518 done = !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(task); 2519 done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(task); 2520 2521 /* If recording any information failed, retry again soon. */ 2522 if (!done) 2523 return; 2524 2525 __this_cpu_write(trace_taskinfo_save, false); 2526 } 2527 2528 /** 2529 * tracing_record_taskinfo_sched_switch - record task info for sched_switch 2530 * 2531 * @prev: previous task during sched_switch 2532 * @next: next task during sched_switch 2533 * @flags: TRACE_RECORD_CMDLINE for recording comm 2534 * TRACE_RECORD_TGID for recording tgid 2535 */ 2536 void tracing_record_taskinfo_sched_switch(struct task_struct *prev, 2537 struct task_struct *next, int flags) 2538 { 2539 bool done; 2540 2541 if (tracing_record_taskinfo_skip(flags)) 2542 return; 2543 2544 /* 2545 * Record as much task information as possible. If some fail, continue 2546 * to try to record the others. 2547 */ 2548 done = !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(prev); 2549 done &= !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(next); 2550 done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(prev); 2551 done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(next); 2552 2553 /* If recording any information failed, retry again soon. */ 2554 if (!done) 2555 return; 2556 2557 __this_cpu_write(trace_taskinfo_save, false); 2558 } 2559 2560 /* Helpers to record a specific task information */ 2561 void tracing_record_cmdline(struct task_struct *task) 2562 { 2563 tracing_record_taskinfo(task, TRACE_RECORD_CMDLINE); 2564 } 2565 2566 void tracing_record_tgid(struct task_struct *task) 2567 { 2568 tracing_record_taskinfo(task, TRACE_RECORD_TGID); 2569 } 2570 2571 /* 2572 * Several functions return TRACE_TYPE_PARTIAL_LINE if the trace_seq 2573 * overflowed, and TRACE_TYPE_HANDLED otherwise. This helper function 2574 * simplifies those functions and keeps them in sync. 2575 */ 2576 enum print_line_t trace_handle_return(struct trace_seq *s) 2577 { 2578 return trace_seq_has_overflowed(s) ? 2579 TRACE_TYPE_PARTIAL_LINE : TRACE_TYPE_HANDLED; 2580 } 2581 EXPORT_SYMBOL_GPL(trace_handle_return); 2582 2583 static unsigned short migration_disable_value(void) 2584 { 2585 #if defined(CONFIG_SMP) 2586 return current->migration_disabled; 2587 #else 2588 return 0; 2589 #endif 2590 } 2591 2592 unsigned int tracing_gen_ctx_irq_test(unsigned int irqs_status) 2593 { 2594 unsigned int trace_flags = irqs_status; 2595 unsigned int pc; 2596 2597 pc = preempt_count(); 2598 2599 if (pc & NMI_MASK) 2600 trace_flags |= TRACE_FLAG_NMI; 2601 if (pc & HARDIRQ_MASK) 2602 trace_flags |= TRACE_FLAG_HARDIRQ; 2603 if (in_serving_softirq()) 2604 trace_flags |= TRACE_FLAG_SOFTIRQ; 2605 2606 if (tif_need_resched()) 2607 trace_flags |= TRACE_FLAG_NEED_RESCHED; 2608 if (test_preempt_need_resched()) 2609 trace_flags |= TRACE_FLAG_PREEMPT_RESCHED; 2610 return (trace_flags << 16) | (min_t(unsigned int, pc & 0xff, 0xf)) | 2611 (min_t(unsigned int, migration_disable_value(), 0xf)) << 4; 2612 } 2613 2614 struct ring_buffer_event * 2615 trace_buffer_lock_reserve(struct trace_buffer *buffer, 2616 int type, 2617 unsigned long len, 2618 unsigned int trace_ctx) 2619 { 2620 return __trace_buffer_lock_reserve(buffer, type, len, trace_ctx); 2621 } 2622 2623 DEFINE_PER_CPU(struct ring_buffer_event *, trace_buffered_event); 2624 DEFINE_PER_CPU(int, trace_buffered_event_cnt); 2625 static int trace_buffered_event_ref; 2626 2627 /** 2628 * trace_buffered_event_enable - enable buffering events 2629 * 2630 * When events are being filtered, it is quicker to use a temporary 2631 * buffer to write the event data into if there's a likely chance 2632 * that it will not be committed. The discard of the ring buffer 2633 * is not as fast as committing, and is much slower than copying 2634 * a commit. 2635 * 2636 * When an event is to be filtered, allocate per cpu buffers to 2637 * write the event data into, and if the event is filtered and discarded 2638 * it is simply dropped, otherwise, the entire data is to be committed 2639 * in one shot. 2640 */ 2641 void trace_buffered_event_enable(void) 2642 { 2643 struct ring_buffer_event *event; 2644 struct page *page; 2645 int cpu; 2646 2647 WARN_ON_ONCE(!mutex_is_locked(&event_mutex)); 2648 2649 if (trace_buffered_event_ref++) 2650 return; 2651 2652 for_each_tracing_cpu(cpu) { 2653 page = alloc_pages_node(cpu_to_node(cpu), 2654 GFP_KERNEL | __GFP_NORETRY, 0); 2655 if (!page) 2656 goto failed; 2657 2658 event = page_address(page); 2659 memset(event, 0, sizeof(*event)); 2660 2661 per_cpu(trace_buffered_event, cpu) = event; 2662 2663 preempt_disable(); 2664 if (cpu == smp_processor_id() && 2665 __this_cpu_read(trace_buffered_event) != 2666 per_cpu(trace_buffered_event, cpu)) 2667 WARN_ON_ONCE(1); 2668 preempt_enable(); 2669 } 2670 2671 return; 2672 failed: 2673 trace_buffered_event_disable(); 2674 } 2675 2676 static void enable_trace_buffered_event(void *data) 2677 { 2678 /* Probably not needed, but do it anyway */ 2679 smp_rmb(); 2680 this_cpu_dec(trace_buffered_event_cnt); 2681 } 2682 2683 static void disable_trace_buffered_event(void *data) 2684 { 2685 this_cpu_inc(trace_buffered_event_cnt); 2686 } 2687 2688 /** 2689 * trace_buffered_event_disable - disable buffering events 2690 * 2691 * When a filter is removed, it is faster to not use the buffered 2692 * events, and to commit directly into the ring buffer. Free up 2693 * the temp buffers when there are no more users. This requires 2694 * special synchronization with current events. 2695 */ 2696 void trace_buffered_event_disable(void) 2697 { 2698 int cpu; 2699 2700 WARN_ON_ONCE(!mutex_is_locked(&event_mutex)); 2701 2702 if (WARN_ON_ONCE(!trace_buffered_event_ref)) 2703 return; 2704 2705 if (--trace_buffered_event_ref) 2706 return; 2707 2708 preempt_disable(); 2709 /* For each CPU, set the buffer as used. */ 2710 smp_call_function_many(tracing_buffer_mask, 2711 disable_trace_buffered_event, NULL, 1); 2712 preempt_enable(); 2713 2714 /* Wait for all current users to finish */ 2715 synchronize_rcu(); 2716 2717 for_each_tracing_cpu(cpu) { 2718 free_page((unsigned long)per_cpu(trace_buffered_event, cpu)); 2719 per_cpu(trace_buffered_event, cpu) = NULL; 2720 } 2721 /* 2722 * Make sure trace_buffered_event is NULL before clearing 2723 * trace_buffered_event_cnt. 2724 */ 2725 smp_wmb(); 2726 2727 preempt_disable(); 2728 /* Do the work on each cpu */ 2729 smp_call_function_many(tracing_buffer_mask, 2730 enable_trace_buffered_event, NULL, 1); 2731 preempt_enable(); 2732 } 2733 2734 static struct trace_buffer *temp_buffer; 2735 2736 struct ring_buffer_event * 2737 trace_event_buffer_lock_reserve(struct trace_buffer **current_rb, 2738 struct trace_event_file *trace_file, 2739 int type, unsigned long len, 2740 unsigned int trace_ctx) 2741 { 2742 struct ring_buffer_event *entry; 2743 struct trace_array *tr = trace_file->tr; 2744 int val; 2745 2746 *current_rb = tr->array_buffer.buffer; 2747 2748 if (!tr->no_filter_buffering_ref && 2749 (trace_file->flags & (EVENT_FILE_FL_SOFT_DISABLED | EVENT_FILE_FL_FILTERED)) && 2750 (entry = this_cpu_read(trace_buffered_event))) { 2751 /* 2752 * Filtering is on, so try to use the per cpu buffer first. 2753 * This buffer will simulate a ring_buffer_event, 2754 * where the type_len is zero and the array[0] will 2755 * hold the full length. 2756 * (see include/linux/ring-buffer.h for details on 2757 * how the ring_buffer_event is structured). 2758 * 2759 * Using a temp buffer during filtering and copying it 2760 * on a matched filter is quicker than writing directly 2761 * into the ring buffer and then discarding it when 2762 * it doesn't match. That is because the discard 2763 * requires several atomic operations to get right. 2764 * Copying on match and doing nothing on a failed match 2765 * is still quicker than no copy on match, but having 2766 * to discard out of the ring buffer on a failed match. 2767 */ 2768 int max_len = PAGE_SIZE - struct_size(entry, array, 1); 2769 2770 val = this_cpu_inc_return(trace_buffered_event_cnt); 2771 2772 /* 2773 * Preemption is disabled, but interrupts and NMIs 2774 * can still come in now. If that happens after 2775 * the above increment, then it will have to go 2776 * back to the old method of allocating the event 2777 * on the ring buffer, and if the filter fails, it 2778 * will have to call ring_buffer_discard_commit() 2779 * to remove it. 2780 * 2781 * Need to also check the unlikely case that the 2782 * length is bigger than the temp buffer size. 2783 * If that happens, then the reserve is pretty much 2784 * guaranteed to fail, as the ring buffer currently 2785 * only allows events less than a page. But that may 2786 * change in the future, so let the ring buffer reserve 2787 * handle the failure in that case. 2788 */ 2789 if (val == 1 && likely(len <= max_len)) { 2790 trace_event_setup(entry, type, trace_ctx); 2791 entry->array[0] = len; 2792 return entry; 2793 } 2794 this_cpu_dec(trace_buffered_event_cnt); 2795 } 2796 2797 entry = __trace_buffer_lock_reserve(*current_rb, type, len, 2798 trace_ctx); 2799 /* 2800 * If tracing is off, but we have triggers enabled 2801 * we still need to look at the event data. Use the temp_buffer 2802 * to store the trace event for the trigger to use. It's recursive 2803 * safe and will not be recorded anywhere. 2804 */ 2805 if (!entry && trace_file->flags & EVENT_FILE_FL_TRIGGER_COND) { 2806 *current_rb = temp_buffer; 2807 entry = __trace_buffer_lock_reserve(*current_rb, type, len, 2808 trace_ctx); 2809 } 2810 return entry; 2811 } 2812 EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve); 2813 2814 static DEFINE_SPINLOCK(tracepoint_iter_lock); 2815 static DEFINE_MUTEX(tracepoint_printk_mutex); 2816 2817 static void output_printk(struct trace_event_buffer *fbuffer) 2818 { 2819 struct trace_event_call *event_call; 2820 struct trace_event_file *file; 2821 struct trace_event *event; 2822 unsigned long flags; 2823 struct trace_iterator *iter = tracepoint_print_iter; 2824 2825 /* We should never get here if iter is NULL */ 2826 if (WARN_ON_ONCE(!iter)) 2827 return; 2828 2829 event_call = fbuffer->trace_file->event_call; 2830 if (!event_call || !event_call->event.funcs || 2831 !event_call->event.funcs->trace) 2832 return; 2833 2834 file = fbuffer->trace_file; 2835 if (test_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags) || 2836 (unlikely(file->flags & EVENT_FILE_FL_FILTERED) && 2837 !filter_match_preds(file->filter, fbuffer->entry))) 2838 return; 2839 2840 event = &fbuffer->trace_file->event_call->event; 2841 2842 spin_lock_irqsave(&tracepoint_iter_lock, flags); 2843 trace_seq_init(&iter->seq); 2844 iter->ent = fbuffer->entry; 2845 event_call->event.funcs->trace(iter, 0, event); 2846 trace_seq_putc(&iter->seq, 0); 2847 printk("%s", iter->seq.buffer); 2848 2849 spin_unlock_irqrestore(&tracepoint_iter_lock, flags); 2850 } 2851 2852 int tracepoint_printk_sysctl(struct ctl_table *table, int write, 2853 void *buffer, size_t *lenp, 2854 loff_t *ppos) 2855 { 2856 int save_tracepoint_printk; 2857 int ret; 2858 2859 mutex_lock(&tracepoint_printk_mutex); 2860 save_tracepoint_printk = tracepoint_printk; 2861 2862 ret = proc_dointvec(table, write, buffer, lenp, ppos); 2863 2864 /* 2865 * This will force exiting early, as tracepoint_printk 2866 * is always zero when tracepoint_printk_iter is not allocated 2867 */ 2868 if (!tracepoint_print_iter) 2869 tracepoint_printk = 0; 2870 2871 if (save_tracepoint_printk == tracepoint_printk) 2872 goto out; 2873 2874 if (tracepoint_printk) 2875 static_key_enable(&tracepoint_printk_key.key); 2876 else 2877 static_key_disable(&tracepoint_printk_key.key); 2878 2879 out: 2880 mutex_unlock(&tracepoint_printk_mutex); 2881 2882 return ret; 2883 } 2884 2885 void trace_event_buffer_commit(struct trace_event_buffer *fbuffer) 2886 { 2887 enum event_trigger_type tt = ETT_NONE; 2888 struct trace_event_file *file = fbuffer->trace_file; 2889 2890 if (__event_trigger_test_discard(file, fbuffer->buffer, fbuffer->event, 2891 fbuffer->entry, &tt)) 2892 goto discard; 2893 2894 if (static_key_false(&tracepoint_printk_key.key)) 2895 output_printk(fbuffer); 2896 2897 if (static_branch_unlikely(&trace_event_exports_enabled)) 2898 ftrace_exports(fbuffer->event, TRACE_EXPORT_EVENT); 2899 2900 trace_buffer_unlock_commit_regs(file->tr, fbuffer->buffer, 2901 fbuffer->event, fbuffer->trace_ctx, fbuffer->regs); 2902 2903 discard: 2904 if (tt) 2905 event_triggers_post_call(file, tt); 2906 2907 } 2908 EXPORT_SYMBOL_GPL(trace_event_buffer_commit); 2909 2910 /* 2911 * Skip 3: 2912 * 2913 * trace_buffer_unlock_commit_regs() 2914 * trace_event_buffer_commit() 2915 * trace_event_raw_event_xxx() 2916 */ 2917 # define STACK_SKIP 3 2918 2919 void trace_buffer_unlock_commit_regs(struct trace_array *tr, 2920 struct trace_buffer *buffer, 2921 struct ring_buffer_event *event, 2922 unsigned int trace_ctx, 2923 struct pt_regs *regs) 2924 { 2925 __buffer_unlock_commit(buffer, event); 2926 2927 /* 2928 * If regs is not set, then skip the necessary functions. 2929 * Note, we can still get here via blktrace, wakeup tracer 2930 * and mmiotrace, but that's ok if they lose a function or 2931 * two. They are not that meaningful. 2932 */ 2933 ftrace_trace_stack(tr, buffer, trace_ctx, regs ? 0 : STACK_SKIP, regs); 2934 ftrace_trace_userstack(tr, buffer, trace_ctx); 2935 } 2936 2937 /* 2938 * Similar to trace_buffer_unlock_commit_regs() but do not dump stack. 2939 */ 2940 void 2941 trace_buffer_unlock_commit_nostack(struct trace_buffer *buffer, 2942 struct ring_buffer_event *event) 2943 { 2944 __buffer_unlock_commit(buffer, event); 2945 } 2946 2947 void 2948 trace_function(struct trace_array *tr, unsigned long ip, unsigned long 2949 parent_ip, unsigned int trace_ctx) 2950 { 2951 struct trace_event_call *call = &event_function; 2952 struct trace_buffer *buffer = tr->array_buffer.buffer; 2953 struct ring_buffer_event *event; 2954 struct ftrace_entry *entry; 2955 2956 event = __trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry), 2957 trace_ctx); 2958 if (!event) 2959 return; 2960 entry = ring_buffer_event_data(event); 2961 entry->ip = ip; 2962 entry->parent_ip = parent_ip; 2963 2964 if (!call_filter_check_discard(call, entry, buffer, event)) { 2965 if (static_branch_unlikely(&trace_function_exports_enabled)) 2966 ftrace_exports(event, TRACE_EXPORT_FUNCTION); 2967 __buffer_unlock_commit(buffer, event); 2968 } 2969 } 2970 2971 #ifdef CONFIG_STACKTRACE 2972 2973 /* Allow 4 levels of nesting: normal, softirq, irq, NMI */ 2974 #define FTRACE_KSTACK_NESTING 4 2975 2976 #define FTRACE_KSTACK_ENTRIES (PAGE_SIZE / FTRACE_KSTACK_NESTING) 2977 2978 struct ftrace_stack { 2979 unsigned long calls[FTRACE_KSTACK_ENTRIES]; 2980 }; 2981 2982 2983 struct ftrace_stacks { 2984 struct ftrace_stack stacks[FTRACE_KSTACK_NESTING]; 2985 }; 2986 2987 static DEFINE_PER_CPU(struct ftrace_stacks, ftrace_stacks); 2988 static DEFINE_PER_CPU(int, ftrace_stack_reserve); 2989 2990 static void __ftrace_trace_stack(struct trace_buffer *buffer, 2991 unsigned int trace_ctx, 2992 int skip, struct pt_regs *regs) 2993 { 2994 struct trace_event_call *call = &event_kernel_stack; 2995 struct ring_buffer_event *event; 2996 unsigned int size, nr_entries; 2997 struct ftrace_stack *fstack; 2998 struct stack_entry *entry; 2999 int stackidx; 3000 3001 /* 3002 * Add one, for this function and the call to save_stack_trace() 3003 * If regs is set, then these functions will not be in the way. 3004 */ 3005 #ifndef CONFIG_UNWINDER_ORC 3006 if (!regs) 3007 skip++; 3008 #endif 3009 3010 preempt_disable_notrace(); 3011 3012 stackidx = __this_cpu_inc_return(ftrace_stack_reserve) - 1; 3013 3014 /* This should never happen. If it does, yell once and skip */ 3015 if (WARN_ON_ONCE(stackidx >= FTRACE_KSTACK_NESTING)) 3016 goto out; 3017 3018 /* 3019 * The above __this_cpu_inc_return() is 'atomic' cpu local. An 3020 * interrupt will either see the value pre increment or post 3021 * increment. If the interrupt happens pre increment it will have 3022 * restored the counter when it returns. We just need a barrier to 3023 * keep gcc from moving things around. 3024 */ 3025 barrier(); 3026 3027 fstack = this_cpu_ptr(ftrace_stacks.stacks) + stackidx; 3028 size = ARRAY_SIZE(fstack->calls); 3029 3030 if (regs) { 3031 nr_entries = stack_trace_save_regs(regs, fstack->calls, 3032 size, skip); 3033 } else { 3034 nr_entries = stack_trace_save(fstack->calls, size, skip); 3035 } 3036 3037 size = nr_entries * sizeof(unsigned long); 3038 event = __trace_buffer_lock_reserve(buffer, TRACE_STACK, 3039 (sizeof(*entry) - sizeof(entry->caller)) + size, 3040 trace_ctx); 3041 if (!event) 3042 goto out; 3043 entry = ring_buffer_event_data(event); 3044 3045 memcpy(&entry->caller, fstack->calls, size); 3046 entry->size = nr_entries; 3047 3048 if (!call_filter_check_discard(call, entry, buffer, event)) 3049 __buffer_unlock_commit(buffer, event); 3050 3051 out: 3052 /* Again, don't let gcc optimize things here */ 3053 barrier(); 3054 __this_cpu_dec(ftrace_stack_reserve); 3055 preempt_enable_notrace(); 3056 3057 } 3058 3059 static inline void ftrace_trace_stack(struct trace_array *tr, 3060 struct trace_buffer *buffer, 3061 unsigned int trace_ctx, 3062 int skip, struct pt_regs *regs) 3063 { 3064 if (!(tr->trace_flags & TRACE_ITER_STACKTRACE)) 3065 return; 3066 3067 __ftrace_trace_stack(buffer, trace_ctx, skip, regs); 3068 } 3069 3070 void __trace_stack(struct trace_array *tr, unsigned int trace_ctx, 3071 int skip) 3072 { 3073 struct trace_buffer *buffer = tr->array_buffer.buffer; 3074 3075 if (rcu_is_watching()) { 3076 __ftrace_trace_stack(buffer, trace_ctx, skip, NULL); 3077 return; 3078 } 3079 3080 /* 3081 * When an NMI triggers, RCU is enabled via rcu_nmi_enter(), 3082 * but if the above rcu_is_watching() failed, then the NMI 3083 * triggered someplace critical, and rcu_irq_enter() should 3084 * not be called from NMI. 3085 */ 3086 if (unlikely(in_nmi())) 3087 return; 3088 3089 rcu_irq_enter_irqson(); 3090 __ftrace_trace_stack(buffer, trace_ctx, skip, NULL); 3091 rcu_irq_exit_irqson(); 3092 } 3093 3094 /** 3095 * trace_dump_stack - record a stack back trace in the trace buffer 3096 * @skip: Number of functions to skip (helper handlers) 3097 */ 3098 void trace_dump_stack(int skip) 3099 { 3100 if (tracing_disabled || tracing_selftest_running) 3101 return; 3102 3103 #ifndef CONFIG_UNWINDER_ORC 3104 /* Skip 1 to skip this function. */ 3105 skip++; 3106 #endif 3107 __ftrace_trace_stack(global_trace.array_buffer.buffer, 3108 tracing_gen_ctx(), skip, NULL); 3109 } 3110 EXPORT_SYMBOL_GPL(trace_dump_stack); 3111 3112 #ifdef CONFIG_USER_STACKTRACE_SUPPORT 3113 static DEFINE_PER_CPU(int, user_stack_count); 3114 3115 static void 3116 ftrace_trace_userstack(struct trace_array *tr, 3117 struct trace_buffer *buffer, unsigned int trace_ctx) 3118 { 3119 struct trace_event_call *call = &event_user_stack; 3120 struct ring_buffer_event *event; 3121 struct userstack_entry *entry; 3122 3123 if (!(tr->trace_flags & TRACE_ITER_USERSTACKTRACE)) 3124 return; 3125 3126 /* 3127 * NMIs can not handle page faults, even with fix ups. 3128 * The save user stack can (and often does) fault. 3129 */ 3130 if (unlikely(in_nmi())) 3131 return; 3132 3133 /* 3134 * prevent recursion, since the user stack tracing may 3135 * trigger other kernel events. 3136 */ 3137 preempt_disable(); 3138 if (__this_cpu_read(user_stack_count)) 3139 goto out; 3140 3141 __this_cpu_inc(user_stack_count); 3142 3143 event = __trace_buffer_lock_reserve(buffer, TRACE_USER_STACK, 3144 sizeof(*entry), trace_ctx); 3145 if (!event) 3146 goto out_drop_count; 3147 entry = ring_buffer_event_data(event); 3148 3149 entry->tgid = current->tgid; 3150 memset(&entry->caller, 0, sizeof(entry->caller)); 3151 3152 stack_trace_save_user(entry->caller, FTRACE_STACK_ENTRIES); 3153 if (!call_filter_check_discard(call, entry, buffer, event)) 3154 __buffer_unlock_commit(buffer, event); 3155 3156 out_drop_count: 3157 __this_cpu_dec(user_stack_count); 3158 out: 3159 preempt_enable(); 3160 } 3161 #else /* CONFIG_USER_STACKTRACE_SUPPORT */ 3162 static void ftrace_trace_userstack(struct trace_array *tr, 3163 struct trace_buffer *buffer, 3164 unsigned int trace_ctx) 3165 { 3166 } 3167 #endif /* !CONFIG_USER_STACKTRACE_SUPPORT */ 3168 3169 #endif /* CONFIG_STACKTRACE */ 3170 3171 static inline void 3172 func_repeats_set_delta_ts(struct func_repeats_entry *entry, 3173 unsigned long long delta) 3174 { 3175 entry->bottom_delta_ts = delta & U32_MAX; 3176 entry->top_delta_ts = (delta >> 32); 3177 } 3178 3179 void trace_last_func_repeats(struct trace_array *tr, 3180 struct trace_func_repeats *last_info, 3181 unsigned int trace_ctx) 3182 { 3183 struct trace_buffer *buffer = tr->array_buffer.buffer; 3184 struct func_repeats_entry *entry; 3185 struct ring_buffer_event *event; 3186 u64 delta; 3187 3188 event = __trace_buffer_lock_reserve(buffer, TRACE_FUNC_REPEATS, 3189 sizeof(*entry), trace_ctx); 3190 if (!event) 3191 return; 3192 3193 delta = ring_buffer_event_time_stamp(buffer, event) - 3194 last_info->ts_last_call; 3195 3196 entry = ring_buffer_event_data(event); 3197 entry->ip = last_info->ip; 3198 entry->parent_ip = last_info->parent_ip; 3199 entry->count = last_info->count; 3200 func_repeats_set_delta_ts(entry, delta); 3201 3202 __buffer_unlock_commit(buffer, event); 3203 } 3204 3205 /* created for use with alloc_percpu */ 3206 struct trace_buffer_struct { 3207 int nesting; 3208 char buffer[4][TRACE_BUF_SIZE]; 3209 }; 3210 3211 static struct trace_buffer_struct *trace_percpu_buffer; 3212 3213 /* 3214 * This allows for lockless recording. If we're nested too deeply, then 3215 * this returns NULL. 3216 */ 3217 static char *get_trace_buf(void) 3218 { 3219 struct trace_buffer_struct *buffer = this_cpu_ptr(trace_percpu_buffer); 3220 3221 if (!buffer || buffer->nesting >= 4) 3222 return NULL; 3223 3224 buffer->nesting++; 3225 3226 /* Interrupts must see nesting incremented before we use the buffer */ 3227 barrier(); 3228 return &buffer->buffer[buffer->nesting - 1][0]; 3229 } 3230 3231 static void put_trace_buf(void) 3232 { 3233 /* Don't let the decrement of nesting leak before this */ 3234 barrier(); 3235 this_cpu_dec(trace_percpu_buffer->nesting); 3236 } 3237 3238 static int alloc_percpu_trace_buffer(void) 3239 { 3240 struct trace_buffer_struct *buffers; 3241 3242 if (trace_percpu_buffer) 3243 return 0; 3244 3245 buffers = alloc_percpu(struct trace_buffer_struct); 3246 if (MEM_FAIL(!buffers, "Could not allocate percpu trace_printk buffer")) 3247 return -ENOMEM; 3248 3249 trace_percpu_buffer = buffers; 3250 return 0; 3251 } 3252 3253 static int buffers_allocated; 3254 3255 void trace_printk_init_buffers(void) 3256 { 3257 if (buffers_allocated) 3258 return; 3259 3260 if (alloc_percpu_trace_buffer()) 3261 return; 3262 3263 /* trace_printk() is for debug use only. Don't use it in production. */ 3264 3265 pr_warn("\n"); 3266 pr_warn("**********************************************************\n"); 3267 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); 3268 pr_warn("** **\n"); 3269 pr_warn("** trace_printk() being used. Allocating extra memory. **\n"); 3270 pr_warn("** **\n"); 3271 pr_warn("** This means that this is a DEBUG kernel and it is **\n"); 3272 pr_warn("** unsafe for production use. **\n"); 3273 pr_warn("** **\n"); 3274 pr_warn("** If you see this message and you are not debugging **\n"); 3275 pr_warn("** the kernel, report this immediately to your vendor! **\n"); 3276 pr_warn("** **\n"); 3277 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); 3278 pr_warn("**********************************************************\n"); 3279 3280 /* Expand the buffers to set size */ 3281 tracing_update_buffers(); 3282 3283 buffers_allocated = 1; 3284 3285 /* 3286 * trace_printk_init_buffers() can be called by modules. 3287 * If that happens, then we need to start cmdline recording 3288 * directly here. If the global_trace.buffer is already 3289 * allocated here, then this was called by module code. 3290 */ 3291 if (global_trace.array_buffer.buffer) 3292 tracing_start_cmdline_record(); 3293 } 3294 EXPORT_SYMBOL_GPL(trace_printk_init_buffers); 3295 3296 void trace_printk_start_comm(void) 3297 { 3298 /* Start tracing comms if trace printk is set */ 3299 if (!buffers_allocated) 3300 return; 3301 tracing_start_cmdline_record(); 3302 } 3303 3304 static void trace_printk_start_stop_comm(int enabled) 3305 { 3306 if (!buffers_allocated) 3307 return; 3308 3309 if (enabled) 3310 tracing_start_cmdline_record(); 3311 else 3312 tracing_stop_cmdline_record(); 3313 } 3314 3315 /** 3316 * trace_vbprintk - write binary msg to tracing buffer 3317 * @ip: The address of the caller 3318 * @fmt: The string format to write to the buffer 3319 * @args: Arguments for @fmt 3320 */ 3321 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args) 3322 { 3323 struct trace_event_call *call = &event_bprint; 3324 struct ring_buffer_event *event; 3325 struct trace_buffer *buffer; 3326 struct trace_array *tr = &global_trace; 3327 struct bprint_entry *entry; 3328 unsigned int trace_ctx; 3329 char *tbuffer; 3330 int len = 0, size; 3331 3332 if (unlikely(tracing_selftest_running || tracing_disabled)) 3333 return 0; 3334 3335 /* Don't pollute graph traces with trace_vprintk internals */ 3336 pause_graph_tracing(); 3337 3338 trace_ctx = tracing_gen_ctx(); 3339 preempt_disable_notrace(); 3340 3341 tbuffer = get_trace_buf(); 3342 if (!tbuffer) { 3343 len = 0; 3344 goto out_nobuffer; 3345 } 3346 3347 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args); 3348 3349 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0) 3350 goto out_put; 3351 3352 size = sizeof(*entry) + sizeof(u32) * len; 3353 buffer = tr->array_buffer.buffer; 3354 ring_buffer_nest_start(buffer); 3355 event = __trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size, 3356 trace_ctx); 3357 if (!event) 3358 goto out; 3359 entry = ring_buffer_event_data(event); 3360 entry->ip = ip; 3361 entry->fmt = fmt; 3362 3363 memcpy(entry->buf, tbuffer, sizeof(u32) * len); 3364 if (!call_filter_check_discard(call, entry, buffer, event)) { 3365 __buffer_unlock_commit(buffer, event); 3366 ftrace_trace_stack(tr, buffer, trace_ctx, 6, NULL); 3367 } 3368 3369 out: 3370 ring_buffer_nest_end(buffer); 3371 out_put: 3372 put_trace_buf(); 3373 3374 out_nobuffer: 3375 preempt_enable_notrace(); 3376 unpause_graph_tracing(); 3377 3378 return len; 3379 } 3380 EXPORT_SYMBOL_GPL(trace_vbprintk); 3381 3382 __printf(3, 0) 3383 static int 3384 __trace_array_vprintk(struct trace_buffer *buffer, 3385 unsigned long ip, const char *fmt, va_list args) 3386 { 3387 struct trace_event_call *call = &event_print; 3388 struct ring_buffer_event *event; 3389 int len = 0, size; 3390 struct print_entry *entry; 3391 unsigned int trace_ctx; 3392 char *tbuffer; 3393 3394 if (tracing_disabled || tracing_selftest_running) 3395 return 0; 3396 3397 /* Don't pollute graph traces with trace_vprintk internals */ 3398 pause_graph_tracing(); 3399 3400 trace_ctx = tracing_gen_ctx(); 3401 preempt_disable_notrace(); 3402 3403 3404 tbuffer = get_trace_buf(); 3405 if (!tbuffer) { 3406 len = 0; 3407 goto out_nobuffer; 3408 } 3409 3410 len = vscnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args); 3411 3412 size = sizeof(*entry) + len + 1; 3413 ring_buffer_nest_start(buffer); 3414 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size, 3415 trace_ctx); 3416 if (!event) 3417 goto out; 3418 entry = ring_buffer_event_data(event); 3419 entry->ip = ip; 3420 3421 memcpy(&entry->buf, tbuffer, len + 1); 3422 if (!call_filter_check_discard(call, entry, buffer, event)) { 3423 __buffer_unlock_commit(buffer, event); 3424 ftrace_trace_stack(&global_trace, buffer, trace_ctx, 6, NULL); 3425 } 3426 3427 out: 3428 ring_buffer_nest_end(buffer); 3429 put_trace_buf(); 3430 3431 out_nobuffer: 3432 preempt_enable_notrace(); 3433 unpause_graph_tracing(); 3434 3435 return len; 3436 } 3437 3438 __printf(3, 0) 3439 int trace_array_vprintk(struct trace_array *tr, 3440 unsigned long ip, const char *fmt, va_list args) 3441 { 3442 return __trace_array_vprintk(tr->array_buffer.buffer, ip, fmt, args); 3443 } 3444 3445 /** 3446 * trace_array_printk - Print a message to a specific instance 3447 * @tr: The instance trace_array descriptor 3448 * @ip: The instruction pointer that this is called from. 3449 * @fmt: The format to print (printf format) 3450 * 3451 * If a subsystem sets up its own instance, they have the right to 3452 * printk strings into their tracing instance buffer using this 3453 * function. Note, this function will not write into the top level 3454 * buffer (use trace_printk() for that), as writing into the top level 3455 * buffer should only have events that can be individually disabled. 3456 * trace_printk() is only used for debugging a kernel, and should not 3457 * be ever incorporated in normal use. 3458 * 3459 * trace_array_printk() can be used, as it will not add noise to the 3460 * top level tracing buffer. 3461 * 3462 * Note, trace_array_init_printk() must be called on @tr before this 3463 * can be used. 3464 */ 3465 __printf(3, 0) 3466 int trace_array_printk(struct trace_array *tr, 3467 unsigned long ip, const char *fmt, ...) 3468 { 3469 int ret; 3470 va_list ap; 3471 3472 if (!tr) 3473 return -ENOENT; 3474 3475 /* This is only allowed for created instances */ 3476 if (tr == &global_trace) 3477 return 0; 3478 3479 if (!(tr->trace_flags & TRACE_ITER_PRINTK)) 3480 return 0; 3481 3482 va_start(ap, fmt); 3483 ret = trace_array_vprintk(tr, ip, fmt, ap); 3484 va_end(ap); 3485 return ret; 3486 } 3487 EXPORT_SYMBOL_GPL(trace_array_printk); 3488 3489 /** 3490 * trace_array_init_printk - Initialize buffers for trace_array_printk() 3491 * @tr: The trace array to initialize the buffers for 3492 * 3493 * As trace_array_printk() only writes into instances, they are OK to 3494 * have in the kernel (unlike trace_printk()). This needs to be called 3495 * before trace_array_printk() can be used on a trace_array. 3496 */ 3497 int trace_array_init_printk(struct trace_array *tr) 3498 { 3499 if (!tr) 3500 return -ENOENT; 3501 3502 /* This is only allowed for created instances */ 3503 if (tr == &global_trace) 3504 return -EINVAL; 3505 3506 return alloc_percpu_trace_buffer(); 3507 } 3508 EXPORT_SYMBOL_GPL(trace_array_init_printk); 3509 3510 __printf(3, 4) 3511 int trace_array_printk_buf(struct trace_buffer *buffer, 3512 unsigned long ip, const char *fmt, ...) 3513 { 3514 int ret; 3515 va_list ap; 3516 3517 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK)) 3518 return 0; 3519 3520 va_start(ap, fmt); 3521 ret = __trace_array_vprintk(buffer, ip, fmt, ap); 3522 va_end(ap); 3523 return ret; 3524 } 3525 3526 __printf(2, 0) 3527 int trace_vprintk(unsigned long ip, const char *fmt, va_list args) 3528 { 3529 return trace_array_vprintk(&global_trace, ip, fmt, args); 3530 } 3531 EXPORT_SYMBOL_GPL(trace_vprintk); 3532 3533 static void trace_iterator_increment(struct trace_iterator *iter) 3534 { 3535 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu); 3536 3537 iter->idx++; 3538 if (buf_iter) 3539 ring_buffer_iter_advance(buf_iter); 3540 } 3541 3542 static struct trace_entry * 3543 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts, 3544 unsigned long *lost_events) 3545 { 3546 struct ring_buffer_event *event; 3547 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu); 3548 3549 if (buf_iter) { 3550 event = ring_buffer_iter_peek(buf_iter, ts); 3551 if (lost_events) 3552 *lost_events = ring_buffer_iter_dropped(buf_iter) ? 3553 (unsigned long)-1 : 0; 3554 } else { 3555 event = ring_buffer_peek(iter->array_buffer->buffer, cpu, ts, 3556 lost_events); 3557 } 3558 3559 if (event) { 3560 iter->ent_size = ring_buffer_event_length(event); 3561 return ring_buffer_event_data(event); 3562 } 3563 iter->ent_size = 0; 3564 return NULL; 3565 } 3566 3567 static struct trace_entry * 3568 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, 3569 unsigned long *missing_events, u64 *ent_ts) 3570 { 3571 struct trace_buffer *buffer = iter->array_buffer->buffer; 3572 struct trace_entry *ent, *next = NULL; 3573 unsigned long lost_events = 0, next_lost = 0; 3574 int cpu_file = iter->cpu_file; 3575 u64 next_ts = 0, ts; 3576 int next_cpu = -1; 3577 int next_size = 0; 3578 int cpu; 3579 3580 /* 3581 * If we are in a per_cpu trace file, don't bother by iterating over 3582 * all cpu and peek directly. 3583 */ 3584 if (cpu_file > RING_BUFFER_ALL_CPUS) { 3585 if (ring_buffer_empty_cpu(buffer, cpu_file)) 3586 return NULL; 3587 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events); 3588 if (ent_cpu) 3589 *ent_cpu = cpu_file; 3590 3591 return ent; 3592 } 3593 3594 for_each_tracing_cpu(cpu) { 3595 3596 if (ring_buffer_empty_cpu(buffer, cpu)) 3597 continue; 3598 3599 ent = peek_next_entry(iter, cpu, &ts, &lost_events); 3600 3601 /* 3602 * Pick the entry with the smallest timestamp: 3603 */ 3604 if (ent && (!next || ts < next_ts)) { 3605 next = ent; 3606 next_cpu = cpu; 3607 next_ts = ts; 3608 next_lost = lost_events; 3609 next_size = iter->ent_size; 3610 } 3611 } 3612 3613 iter->ent_size = next_size; 3614 3615 if (ent_cpu) 3616 *ent_cpu = next_cpu; 3617 3618 if (ent_ts) 3619 *ent_ts = next_ts; 3620 3621 if (missing_events) 3622 *missing_events = next_lost; 3623 3624 return next; 3625 } 3626 3627 #define STATIC_FMT_BUF_SIZE 128 3628 static char static_fmt_buf[STATIC_FMT_BUF_SIZE]; 3629 3630 static char *trace_iter_expand_format(struct trace_iterator *iter) 3631 { 3632 char *tmp; 3633 3634 /* 3635 * iter->tr is NULL when used with tp_printk, which makes 3636 * this get called where it is not safe to call krealloc(). 3637 */ 3638 if (!iter->tr || iter->fmt == static_fmt_buf) 3639 return NULL; 3640 3641 tmp = krealloc(iter->fmt, iter->fmt_size + STATIC_FMT_BUF_SIZE, 3642 GFP_KERNEL); 3643 if (tmp) { 3644 iter->fmt_size += STATIC_FMT_BUF_SIZE; 3645 iter->fmt = tmp; 3646 } 3647 3648 return tmp; 3649 } 3650 3651 /* Returns true if the string is safe to dereference from an event */ 3652 static bool trace_safe_str(struct trace_iterator *iter, const char *str) 3653 { 3654 unsigned long addr = (unsigned long)str; 3655 struct trace_event *trace_event; 3656 struct trace_event_call *event; 3657 3658 /* OK if part of the event data */ 3659 if ((addr >= (unsigned long)iter->ent) && 3660 (addr < (unsigned long)iter->ent + iter->ent_size)) 3661 return true; 3662 3663 /* OK if part of the temp seq buffer */ 3664 if ((addr >= (unsigned long)iter->tmp_seq.buffer) && 3665 (addr < (unsigned long)iter->tmp_seq.buffer + PAGE_SIZE)) 3666 return true; 3667 3668 /* Core rodata can not be freed */ 3669 if (is_kernel_rodata(addr)) 3670 return true; 3671 3672 if (trace_is_tracepoint_string(str)) 3673 return true; 3674 3675 /* 3676 * Now this could be a module event, referencing core module 3677 * data, which is OK. 3678 */ 3679 if (!iter->ent) 3680 return false; 3681 3682 trace_event = ftrace_find_event(iter->ent->type); 3683 if (!trace_event) 3684 return false; 3685 3686 event = container_of(trace_event, struct trace_event_call, event); 3687 if ((event->flags & TRACE_EVENT_FL_DYNAMIC) || !event->module) 3688 return false; 3689 3690 /* Would rather have rodata, but this will suffice */ 3691 if (within_module_core(addr, event->module)) 3692 return true; 3693 3694 return false; 3695 } 3696 3697 static const char *show_buffer(struct trace_seq *s) 3698 { 3699 struct seq_buf *seq = &s->seq; 3700 3701 seq_buf_terminate(seq); 3702 3703 return seq->buffer; 3704 } 3705 3706 static DEFINE_STATIC_KEY_FALSE(trace_no_verify); 3707 3708 static int test_can_verify_check(const char *fmt, ...) 3709 { 3710 char buf[16]; 3711 va_list ap; 3712 int ret; 3713 3714 /* 3715 * The verifier is dependent on vsnprintf() modifies the va_list 3716 * passed to it, where it is sent as a reference. Some architectures 3717 * (like x86_32) passes it by value, which means that vsnprintf() 3718 * does not modify the va_list passed to it, and the verifier 3719 * would then need to be able to understand all the values that 3720 * vsnprintf can use. If it is passed by value, then the verifier 3721 * is disabled. 3722 */ 3723 va_start(ap, fmt); 3724 vsnprintf(buf, 16, "%d", ap); 3725 ret = va_arg(ap, int); 3726 va_end(ap); 3727 3728 return ret; 3729 } 3730 3731 static void test_can_verify(void) 3732 { 3733 if (!test_can_verify_check("%d %d", 0, 1)) { 3734 pr_info("trace event string verifier disabled\n"); 3735 static_branch_inc(&trace_no_verify); 3736 } 3737 } 3738 3739 /** 3740 * trace_check_vprintf - Check dereferenced strings while writing to the seq buffer 3741 * @iter: The iterator that holds the seq buffer and the event being printed 3742 * @fmt: The format used to print the event 3743 * @ap: The va_list holding the data to print from @fmt. 3744 * 3745 * This writes the data into the @iter->seq buffer using the data from 3746 * @fmt and @ap. If the format has a %s, then the source of the string 3747 * is examined to make sure it is safe to print, otherwise it will 3748 * warn and print "[UNSAFE MEMORY]" in place of the dereferenced string 3749 * pointer. 3750 */ 3751 void trace_check_vprintf(struct trace_iterator *iter, const char *fmt, 3752 va_list ap) 3753 { 3754 const char *p = fmt; 3755 const char *str; 3756 int i, j; 3757 3758 if (WARN_ON_ONCE(!fmt)) 3759 return; 3760 3761 if (static_branch_unlikely(&trace_no_verify)) 3762 goto print; 3763 3764 /* Don't bother checking when doing a ftrace_dump() */ 3765 if (iter->fmt == static_fmt_buf) 3766 goto print; 3767 3768 while (*p) { 3769 bool star = false; 3770 int len = 0; 3771 3772 j = 0; 3773 3774 /* We only care about %s and variants */ 3775 for (i = 0; p[i]; i++) { 3776 if (i + 1 >= iter->fmt_size) { 3777 /* 3778 * If we can't expand the copy buffer, 3779 * just print it. 3780 */ 3781 if (!trace_iter_expand_format(iter)) 3782 goto print; 3783 } 3784 3785 if (p[i] == '\\' && p[i+1]) { 3786 i++; 3787 continue; 3788 } 3789 if (p[i] == '%') { 3790 /* Need to test cases like %08.*s */ 3791 for (j = 1; p[i+j]; j++) { 3792 if (isdigit(p[i+j]) || 3793 p[i+j] == '.') 3794 continue; 3795 if (p[i+j] == '*') { 3796 star = true; 3797 continue; 3798 } 3799 break; 3800 } 3801 if (p[i+j] == 's') 3802 break; 3803 star = false; 3804 } 3805 j = 0; 3806 } 3807 /* If no %s found then just print normally */ 3808 if (!p[i]) 3809 break; 3810 3811 /* Copy up to the %s, and print that */ 3812 strncpy(iter->fmt, p, i); 3813 iter->fmt[i] = '\0'; 3814 trace_seq_vprintf(&iter->seq, iter->fmt, ap); 3815 3816 if (star) 3817 len = va_arg(ap, int); 3818 3819 /* The ap now points to the string data of the %s */ 3820 str = va_arg(ap, const char *); 3821 3822 /* 3823 * If you hit this warning, it is likely that the 3824 * trace event in question used %s on a string that 3825 * was saved at the time of the event, but may not be 3826 * around when the trace is read. Use __string(), 3827 * __assign_str() and __get_str() helpers in the TRACE_EVENT() 3828 * instead. See samples/trace_events/trace-events-sample.h 3829 * for reference. 3830 */ 3831 if (WARN_ONCE(!trace_safe_str(iter, str), 3832 "fmt: '%s' current_buffer: '%s'", 3833 fmt, show_buffer(&iter->seq))) { 3834 int ret; 3835 3836 /* Try to safely read the string */ 3837 if (star) { 3838 if (len + 1 > iter->fmt_size) 3839 len = iter->fmt_size - 1; 3840 if (len < 0) 3841 len = 0; 3842 ret = copy_from_kernel_nofault(iter->fmt, str, len); 3843 iter->fmt[len] = 0; 3844 star = false; 3845 } else { 3846 ret = strncpy_from_kernel_nofault(iter->fmt, str, 3847 iter->fmt_size); 3848 } 3849 if (ret < 0) 3850 trace_seq_printf(&iter->seq, "(0x%px)", str); 3851 else 3852 trace_seq_printf(&iter->seq, "(0x%px:%s)", 3853 str, iter->fmt); 3854 str = "[UNSAFE-MEMORY]"; 3855 strcpy(iter->fmt, "%s"); 3856 } else { 3857 strncpy(iter->fmt, p + i, j + 1); 3858 iter->fmt[j+1] = '\0'; 3859 } 3860 if (star) 3861 trace_seq_printf(&iter->seq, iter->fmt, len, str); 3862 else 3863 trace_seq_printf(&iter->seq, iter->fmt, str); 3864 3865 p += i + j + 1; 3866 } 3867 print: 3868 if (*p) 3869 trace_seq_vprintf(&iter->seq, p, ap); 3870 } 3871 3872 const char *trace_event_format(struct trace_iterator *iter, const char *fmt) 3873 { 3874 const char *p, *new_fmt; 3875 char *q; 3876 3877 if (WARN_ON_ONCE(!fmt)) 3878 return fmt; 3879 3880 if (!iter->tr || iter->tr->trace_flags & TRACE_ITER_HASH_PTR) 3881 return fmt; 3882 3883 p = fmt; 3884 new_fmt = q = iter->fmt; 3885 while (*p) { 3886 if (unlikely(q - new_fmt + 3 > iter->fmt_size)) { 3887 if (!trace_iter_expand_format(iter)) 3888 return fmt; 3889 3890 q += iter->fmt - new_fmt; 3891 new_fmt = iter->fmt; 3892 } 3893 3894 *q++ = *p++; 3895 3896 /* Replace %p with %px */ 3897 if (p[-1] == '%') { 3898 if (p[0] == '%') { 3899 *q++ = *p++; 3900 } else if (p[0] == 'p' && !isalnum(p[1])) { 3901 *q++ = *p++; 3902 *q++ = 'x'; 3903 } 3904 } 3905 } 3906 *q = '\0'; 3907 3908 return new_fmt; 3909 } 3910 3911 #define STATIC_TEMP_BUF_SIZE 128 3912 static char static_temp_buf[STATIC_TEMP_BUF_SIZE] __aligned(4); 3913 3914 /* Find the next real entry, without updating the iterator itself */ 3915 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter, 3916 int *ent_cpu, u64 *ent_ts) 3917 { 3918 /* __find_next_entry will reset ent_size */ 3919 int ent_size = iter->ent_size; 3920 struct trace_entry *entry; 3921 3922 /* 3923 * If called from ftrace_dump(), then the iter->temp buffer 3924 * will be the static_temp_buf and not created from kmalloc. 3925 * If the entry size is greater than the buffer, we can 3926 * not save it. Just return NULL in that case. This is only 3927 * used to add markers when two consecutive events' time 3928 * stamps have a large delta. See trace_print_lat_context() 3929 */ 3930 if (iter->temp == static_temp_buf && 3931 STATIC_TEMP_BUF_SIZE < ent_size) 3932 return NULL; 3933 3934 /* 3935 * The __find_next_entry() may call peek_next_entry(), which may 3936 * call ring_buffer_peek() that may make the contents of iter->ent 3937 * undefined. Need to copy iter->ent now. 3938 */ 3939 if (iter->ent && iter->ent != iter->temp) { 3940 if ((!iter->temp || iter->temp_size < iter->ent_size) && 3941 !WARN_ON_ONCE(iter->temp == static_temp_buf)) { 3942 void *temp; 3943 temp = kmalloc(iter->ent_size, GFP_KERNEL); 3944 if (!temp) 3945 return NULL; 3946 kfree(iter->temp); 3947 iter->temp = temp; 3948 iter->temp_size = iter->ent_size; 3949 } 3950 memcpy(iter->temp, iter->ent, iter->ent_size); 3951 iter->ent = iter->temp; 3952 } 3953 entry = __find_next_entry(iter, ent_cpu, NULL, ent_ts); 3954 /* Put back the original ent_size */ 3955 iter->ent_size = ent_size; 3956 3957 return entry; 3958 } 3959 3960 /* Find the next real entry, and increment the iterator to the next entry */ 3961 void *trace_find_next_entry_inc(struct trace_iterator *iter) 3962 { 3963 iter->ent = __find_next_entry(iter, &iter->cpu, 3964 &iter->lost_events, &iter->ts); 3965 3966 if (iter->ent) 3967 trace_iterator_increment(iter); 3968 3969 return iter->ent ? iter : NULL; 3970 } 3971 3972 static void trace_consume(struct trace_iterator *iter) 3973 { 3974 ring_buffer_consume(iter->array_buffer->buffer, iter->cpu, &iter->ts, 3975 &iter->lost_events); 3976 } 3977 3978 static void *s_next(struct seq_file *m, void *v, loff_t *pos) 3979 { 3980 struct trace_iterator *iter = m->private; 3981 int i = (int)*pos; 3982 void *ent; 3983 3984 WARN_ON_ONCE(iter->leftover); 3985 3986 (*pos)++; 3987 3988 /* can't go backwards */ 3989 if (iter->idx > i) 3990 return NULL; 3991 3992 if (iter->idx < 0) 3993 ent = trace_find_next_entry_inc(iter); 3994 else 3995 ent = iter; 3996 3997 while (ent && iter->idx < i) 3998 ent = trace_find_next_entry_inc(iter); 3999 4000 iter->pos = *pos; 4001 4002 return ent; 4003 } 4004 4005 void tracing_iter_reset(struct trace_iterator *iter, int cpu) 4006 { 4007 struct ring_buffer_iter *buf_iter; 4008 unsigned long entries = 0; 4009 u64 ts; 4010 4011 per_cpu_ptr(iter->array_buffer->data, cpu)->skipped_entries = 0; 4012 4013 buf_iter = trace_buffer_iter(iter, cpu); 4014 if (!buf_iter) 4015 return; 4016 4017 ring_buffer_iter_reset(buf_iter); 4018 4019 /* 4020 * We could have the case with the max latency tracers 4021 * that a reset never took place on a cpu. This is evident 4022 * by the timestamp being before the start of the buffer. 4023 */ 4024 while (ring_buffer_iter_peek(buf_iter, &ts)) { 4025 if (ts >= iter->array_buffer->time_start) 4026 break; 4027 entries++; 4028 ring_buffer_iter_advance(buf_iter); 4029 } 4030 4031 per_cpu_ptr(iter->array_buffer->data, cpu)->skipped_entries = entries; 4032 } 4033 4034 /* 4035 * The current tracer is copied to avoid a global locking 4036 * all around. 4037 */ 4038 static void *s_start(struct seq_file *m, loff_t *pos) 4039 { 4040 struct trace_iterator *iter = m->private; 4041 struct trace_array *tr = iter->tr; 4042 int cpu_file = iter->cpu_file; 4043 void *p = NULL; 4044 loff_t l = 0; 4045 int cpu; 4046 4047 /* 4048 * copy the tracer to avoid using a global lock all around. 4049 * iter->trace is a copy of current_trace, the pointer to the 4050 * name may be used instead of a strcmp(), as iter->trace->name 4051 * will point to the same string as current_trace->name. 4052 */ 4053 mutex_lock(&trace_types_lock); 4054 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name)) 4055 *iter->trace = *tr->current_trace; 4056 mutex_unlock(&trace_types_lock); 4057 4058 #ifdef CONFIG_TRACER_MAX_TRACE 4059 if (iter->snapshot && iter->trace->use_max_tr) 4060 return ERR_PTR(-EBUSY); 4061 #endif 4062 4063 if (*pos != iter->pos) { 4064 iter->ent = NULL; 4065 iter->cpu = 0; 4066 iter->idx = -1; 4067 4068 if (cpu_file == RING_BUFFER_ALL_CPUS) { 4069 for_each_tracing_cpu(cpu) 4070 tracing_iter_reset(iter, cpu); 4071 } else 4072 tracing_iter_reset(iter, cpu_file); 4073 4074 iter->leftover = 0; 4075 for (p = iter; p && l < *pos; p = s_next(m, p, &l)) 4076 ; 4077 4078 } else { 4079 /* 4080 * If we overflowed the seq_file before, then we want 4081 * to just reuse the trace_seq buffer again. 4082 */ 4083 if (iter->leftover) 4084 p = iter; 4085 else { 4086 l = *pos - 1; 4087 p = s_next(m, p, &l); 4088 } 4089 } 4090 4091 trace_event_read_lock(); 4092 trace_access_lock(cpu_file); 4093 return p; 4094 } 4095 4096 static void s_stop(struct seq_file *m, void *p) 4097 { 4098 struct trace_iterator *iter = m->private; 4099 4100 #ifdef CONFIG_TRACER_MAX_TRACE 4101 if (iter->snapshot && iter->trace->use_max_tr) 4102 return; 4103 #endif 4104 4105 trace_access_unlock(iter->cpu_file); 4106 trace_event_read_unlock(); 4107 } 4108 4109 static void 4110 get_total_entries_cpu(struct array_buffer *buf, unsigned long *total, 4111 unsigned long *entries, int cpu) 4112 { 4113 unsigned long count; 4114 4115 count = ring_buffer_entries_cpu(buf->buffer, cpu); 4116 /* 4117 * If this buffer has skipped entries, then we hold all 4118 * entries for the trace and we need to ignore the 4119 * ones before the time stamp. 4120 */ 4121 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) { 4122 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries; 4123 /* total is the same as the entries */ 4124 *total = count; 4125 } else 4126 *total = count + 4127 ring_buffer_overrun_cpu(buf->buffer, cpu); 4128 *entries = count; 4129 } 4130 4131 static void 4132 get_total_entries(struct array_buffer *buf, 4133 unsigned long *total, unsigned long *entries) 4134 { 4135 unsigned long t, e; 4136 int cpu; 4137 4138 *total = 0; 4139 *entries = 0; 4140 4141 for_each_tracing_cpu(cpu) { 4142 get_total_entries_cpu(buf, &t, &e, cpu); 4143 *total += t; 4144 *entries += e; 4145 } 4146 } 4147 4148 unsigned long trace_total_entries_cpu(struct trace_array *tr, int cpu) 4149 { 4150 unsigned long total, entries; 4151 4152 if (!tr) 4153 tr = &global_trace; 4154 4155 get_total_entries_cpu(&tr->array_buffer, &total, &entries, cpu); 4156 4157 return entries; 4158 } 4159 4160 unsigned long trace_total_entries(struct trace_array *tr) 4161 { 4162 unsigned long total, entries; 4163 4164 if (!tr) 4165 tr = &global_trace; 4166 4167 get_total_entries(&tr->array_buffer, &total, &entries); 4168 4169 return entries; 4170 } 4171 4172 static void print_lat_help_header(struct seq_file *m) 4173 { 4174 seq_puts(m, "# _------=> CPU# \n" 4175 "# / _-----=> irqs-off \n" 4176 "# | / _----=> need-resched \n" 4177 "# || / _---=> hardirq/softirq \n" 4178 "# ||| / _--=> preempt-depth \n" 4179 "# |||| / _-=> migrate-disable \n" 4180 "# ||||| / delay \n" 4181 "# cmd pid |||||| time | caller \n" 4182 "# \\ / |||||| \\ | / \n"); 4183 } 4184 4185 static void print_event_info(struct array_buffer *buf, struct seq_file *m) 4186 { 4187 unsigned long total; 4188 unsigned long entries; 4189 4190 get_total_entries(buf, &total, &entries); 4191 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n", 4192 entries, total, num_online_cpus()); 4193 seq_puts(m, "#\n"); 4194 } 4195 4196 static void print_func_help_header(struct array_buffer *buf, struct seq_file *m, 4197 unsigned int flags) 4198 { 4199 bool tgid = flags & TRACE_ITER_RECORD_TGID; 4200 4201 print_event_info(buf, m); 4202 4203 seq_printf(m, "# TASK-PID %s CPU# TIMESTAMP FUNCTION\n", tgid ? " TGID " : ""); 4204 seq_printf(m, "# | | %s | | |\n", tgid ? " | " : ""); 4205 } 4206 4207 static void print_func_help_header_irq(struct array_buffer *buf, struct seq_file *m, 4208 unsigned int flags) 4209 { 4210 bool tgid = flags & TRACE_ITER_RECORD_TGID; 4211 const char *space = " "; 4212 int prec = tgid ? 12 : 2; 4213 4214 print_event_info(buf, m); 4215 4216 seq_printf(m, "# %.*s _-----=> irqs-off\n", prec, space); 4217 seq_printf(m, "# %.*s / _----=> need-resched\n", prec, space); 4218 seq_printf(m, "# %.*s| / _---=> hardirq/softirq\n", prec, space); 4219 seq_printf(m, "# %.*s|| / _--=> preempt-depth\n", prec, space); 4220 seq_printf(m, "# %.*s||| / _-=> migrate-disable\n", prec, space); 4221 seq_printf(m, "# %.*s|||| / delay\n", prec, space); 4222 seq_printf(m, "# TASK-PID %.*s CPU# ||||| TIMESTAMP FUNCTION\n", prec, " TGID "); 4223 seq_printf(m, "# | | %.*s | ||||| | |\n", prec, " | "); 4224 } 4225 4226 void 4227 print_trace_header(struct seq_file *m, struct trace_iterator *iter) 4228 { 4229 unsigned long sym_flags = (global_trace.trace_flags & TRACE_ITER_SYM_MASK); 4230 struct array_buffer *buf = iter->array_buffer; 4231 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu); 4232 struct tracer *type = iter->trace; 4233 unsigned long entries; 4234 unsigned long total; 4235 const char *name = "preemption"; 4236 4237 name = type->name; 4238 4239 get_total_entries(buf, &total, &entries); 4240 4241 seq_printf(m, "# %s latency trace v1.1.5 on %s\n", 4242 name, UTS_RELEASE); 4243 seq_puts(m, "# -----------------------------------" 4244 "---------------------------------\n"); 4245 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |" 4246 " (M:%s VP:%d, KP:%d, SP:%d HP:%d", 4247 nsecs_to_usecs(data->saved_latency), 4248 entries, 4249 total, 4250 buf->cpu, 4251 #if defined(CONFIG_PREEMPT_NONE) 4252 "server", 4253 #elif defined(CONFIG_PREEMPT_VOLUNTARY) 4254 "desktop", 4255 #elif defined(CONFIG_PREEMPT) 4256 "preempt", 4257 #elif defined(CONFIG_PREEMPT_RT) 4258 "preempt_rt", 4259 #else 4260 "unknown", 4261 #endif 4262 /* These are reserved for later use */ 4263 0, 0, 0, 0); 4264 #ifdef CONFIG_SMP 4265 seq_printf(m, " #P:%d)\n", num_online_cpus()); 4266 #else 4267 seq_puts(m, ")\n"); 4268 #endif 4269 seq_puts(m, "# -----------------\n"); 4270 seq_printf(m, "# | task: %.16s-%d " 4271 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n", 4272 data->comm, data->pid, 4273 from_kuid_munged(seq_user_ns(m), data->uid), data->nice, 4274 data->policy, data->rt_priority); 4275 seq_puts(m, "# -----------------\n"); 4276 4277 if (data->critical_start) { 4278 seq_puts(m, "# => started at: "); 4279 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags); 4280 trace_print_seq(m, &iter->seq); 4281 seq_puts(m, "\n# => ended at: "); 4282 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags); 4283 trace_print_seq(m, &iter->seq); 4284 seq_puts(m, "\n#\n"); 4285 } 4286 4287 seq_puts(m, "#\n"); 4288 } 4289 4290 static void test_cpu_buff_start(struct trace_iterator *iter) 4291 { 4292 struct trace_seq *s = &iter->seq; 4293 struct trace_array *tr = iter->tr; 4294 4295 if (!(tr->trace_flags & TRACE_ITER_ANNOTATE)) 4296 return; 4297 4298 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE)) 4299 return; 4300 4301 if (cpumask_available(iter->started) && 4302 cpumask_test_cpu(iter->cpu, iter->started)) 4303 return; 4304 4305 if (per_cpu_ptr(iter->array_buffer->data, iter->cpu)->skipped_entries) 4306 return; 4307 4308 if (cpumask_available(iter->started)) 4309 cpumask_set_cpu(iter->cpu, iter->started); 4310 4311 /* Don't print started cpu buffer for the first entry of the trace */ 4312 if (iter->idx > 1) 4313 trace_seq_printf(s, "##### CPU %u buffer started ####\n", 4314 iter->cpu); 4315 } 4316 4317 static enum print_line_t print_trace_fmt(struct trace_iterator *iter) 4318 { 4319 struct trace_array *tr = iter->tr; 4320 struct trace_seq *s = &iter->seq; 4321 unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK); 4322 struct trace_entry *entry; 4323 struct trace_event *event; 4324 4325 entry = iter->ent; 4326 4327 test_cpu_buff_start(iter); 4328 4329 event = ftrace_find_event(entry->type); 4330 4331 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) { 4332 if (iter->iter_flags & TRACE_FILE_LAT_FMT) 4333 trace_print_lat_context(iter); 4334 else 4335 trace_print_context(iter); 4336 } 4337 4338 if (trace_seq_has_overflowed(s)) 4339 return TRACE_TYPE_PARTIAL_LINE; 4340 4341 if (event) 4342 return event->funcs->trace(iter, sym_flags, event); 4343 4344 trace_seq_printf(s, "Unknown type %d\n", entry->type); 4345 4346 return trace_handle_return(s); 4347 } 4348 4349 static enum print_line_t print_raw_fmt(struct trace_iterator *iter) 4350 { 4351 struct trace_array *tr = iter->tr; 4352 struct trace_seq *s = &iter->seq; 4353 struct trace_entry *entry; 4354 struct trace_event *event; 4355 4356 entry = iter->ent; 4357 4358 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) 4359 trace_seq_printf(s, "%d %d %llu ", 4360 entry->pid, iter->cpu, iter->ts); 4361 4362 if (trace_seq_has_overflowed(s)) 4363 return TRACE_TYPE_PARTIAL_LINE; 4364 4365 event = ftrace_find_event(entry->type); 4366 if (event) 4367 return event->funcs->raw(iter, 0, event); 4368 4369 trace_seq_printf(s, "%d ?\n", entry->type); 4370 4371 return trace_handle_return(s); 4372 } 4373 4374 static enum print_line_t print_hex_fmt(struct trace_iterator *iter) 4375 { 4376 struct trace_array *tr = iter->tr; 4377 struct trace_seq *s = &iter->seq; 4378 unsigned char newline = '\n'; 4379 struct trace_entry *entry; 4380 struct trace_event *event; 4381 4382 entry = iter->ent; 4383 4384 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) { 4385 SEQ_PUT_HEX_FIELD(s, entry->pid); 4386 SEQ_PUT_HEX_FIELD(s, iter->cpu); 4387 SEQ_PUT_HEX_FIELD(s, iter->ts); 4388 if (trace_seq_has_overflowed(s)) 4389 return TRACE_TYPE_PARTIAL_LINE; 4390 } 4391 4392 event = ftrace_find_event(entry->type); 4393 if (event) { 4394 enum print_line_t ret = event->funcs->hex(iter, 0, event); 4395 if (ret != TRACE_TYPE_HANDLED) 4396 return ret; 4397 } 4398 4399 SEQ_PUT_FIELD(s, newline); 4400 4401 return trace_handle_return(s); 4402 } 4403 4404 static enum print_line_t print_bin_fmt(struct trace_iterator *iter) 4405 { 4406 struct trace_array *tr = iter->tr; 4407 struct trace_seq *s = &iter->seq; 4408 struct trace_entry *entry; 4409 struct trace_event *event; 4410 4411 entry = iter->ent; 4412 4413 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) { 4414 SEQ_PUT_FIELD(s, entry->pid); 4415 SEQ_PUT_FIELD(s, iter->cpu); 4416 SEQ_PUT_FIELD(s, iter->ts); 4417 if (trace_seq_has_overflowed(s)) 4418 return TRACE_TYPE_PARTIAL_LINE; 4419 } 4420 4421 event = ftrace_find_event(entry->type); 4422 return event ? event->funcs->binary(iter, 0, event) : 4423 TRACE_TYPE_HANDLED; 4424 } 4425 4426 int trace_empty(struct trace_iterator *iter) 4427 { 4428 struct ring_buffer_iter *buf_iter; 4429 int cpu; 4430 4431 /* If we are looking at one CPU buffer, only check that one */ 4432 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) { 4433 cpu = iter->cpu_file; 4434 buf_iter = trace_buffer_iter(iter, cpu); 4435 if (buf_iter) { 4436 if (!ring_buffer_iter_empty(buf_iter)) 4437 return 0; 4438 } else { 4439 if (!ring_buffer_empty_cpu(iter->array_buffer->buffer, cpu)) 4440 return 0; 4441 } 4442 return 1; 4443 } 4444 4445 for_each_tracing_cpu(cpu) { 4446 buf_iter = trace_buffer_iter(iter, cpu); 4447 if (buf_iter) { 4448 if (!ring_buffer_iter_empty(buf_iter)) 4449 return 0; 4450 } else { 4451 if (!ring_buffer_empty_cpu(iter->array_buffer->buffer, cpu)) 4452 return 0; 4453 } 4454 } 4455 4456 return 1; 4457 } 4458 4459 /* Called with trace_event_read_lock() held. */ 4460 enum print_line_t print_trace_line(struct trace_iterator *iter) 4461 { 4462 struct trace_array *tr = iter->tr; 4463 unsigned long trace_flags = tr->trace_flags; 4464 enum print_line_t ret; 4465 4466 if (iter->lost_events) { 4467 if (iter->lost_events == (unsigned long)-1) 4468 trace_seq_printf(&iter->seq, "CPU:%d [LOST EVENTS]\n", 4469 iter->cpu); 4470 else 4471 trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n", 4472 iter->cpu, iter->lost_events); 4473 if (trace_seq_has_overflowed(&iter->seq)) 4474 return TRACE_TYPE_PARTIAL_LINE; 4475 } 4476 4477 if (iter->trace && iter->trace->print_line) { 4478 ret = iter->trace->print_line(iter); 4479 if (ret != TRACE_TYPE_UNHANDLED) 4480 return ret; 4481 } 4482 4483 if (iter->ent->type == TRACE_BPUTS && 4484 trace_flags & TRACE_ITER_PRINTK && 4485 trace_flags & TRACE_ITER_PRINTK_MSGONLY) 4486 return trace_print_bputs_msg_only(iter); 4487 4488 if (iter->ent->type == TRACE_BPRINT && 4489 trace_flags & TRACE_ITER_PRINTK && 4490 trace_flags & TRACE_ITER_PRINTK_MSGONLY) 4491 return trace_print_bprintk_msg_only(iter); 4492 4493 if (iter->ent->type == TRACE_PRINT && 4494 trace_flags & TRACE_ITER_PRINTK && 4495 trace_flags & TRACE_ITER_PRINTK_MSGONLY) 4496 return trace_print_printk_msg_only(iter); 4497 4498 if (trace_flags & TRACE_ITER_BIN) 4499 return print_bin_fmt(iter); 4500 4501 if (trace_flags & TRACE_ITER_HEX) 4502 return print_hex_fmt(iter); 4503 4504 if (trace_flags & TRACE_ITER_RAW) 4505 return print_raw_fmt(iter); 4506 4507 return print_trace_fmt(iter); 4508 } 4509 4510 void trace_latency_header(struct seq_file *m) 4511 { 4512 struct trace_iterator *iter = m->private; 4513 struct trace_array *tr = iter->tr; 4514 4515 /* print nothing if the buffers are empty */ 4516 if (trace_empty(iter)) 4517 return; 4518 4519 if (iter->iter_flags & TRACE_FILE_LAT_FMT) 4520 print_trace_header(m, iter); 4521 4522 if (!(tr->trace_flags & TRACE_ITER_VERBOSE)) 4523 print_lat_help_header(m); 4524 } 4525 4526 void trace_default_header(struct seq_file *m) 4527 { 4528 struct trace_iterator *iter = m->private; 4529 struct trace_array *tr = iter->tr; 4530 unsigned long trace_flags = tr->trace_flags; 4531 4532 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO)) 4533 return; 4534 4535 if (iter->iter_flags & TRACE_FILE_LAT_FMT) { 4536 /* print nothing if the buffers are empty */ 4537 if (trace_empty(iter)) 4538 return; 4539 print_trace_header(m, iter); 4540 if (!(trace_flags & TRACE_ITER_VERBOSE)) 4541 print_lat_help_header(m); 4542 } else { 4543 if (!(trace_flags & TRACE_ITER_VERBOSE)) { 4544 if (trace_flags & TRACE_ITER_IRQ_INFO) 4545 print_func_help_header_irq(iter->array_buffer, 4546 m, trace_flags); 4547 else 4548 print_func_help_header(iter->array_buffer, m, 4549 trace_flags); 4550 } 4551 } 4552 } 4553 4554 static void test_ftrace_alive(struct seq_file *m) 4555 { 4556 if (!ftrace_is_dead()) 4557 return; 4558 seq_puts(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n" 4559 "# MAY BE MISSING FUNCTION EVENTS\n"); 4560 } 4561 4562 #ifdef CONFIG_TRACER_MAX_TRACE 4563 static void show_snapshot_main_help(struct seq_file *m) 4564 { 4565 seq_puts(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n" 4566 "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n" 4567 "# Takes a snapshot of the main buffer.\n" 4568 "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate or free)\n" 4569 "# (Doesn't have to be '2' works with any number that\n" 4570 "# is not a '0' or '1')\n"); 4571 } 4572 4573 static void show_snapshot_percpu_help(struct seq_file *m) 4574 { 4575 seq_puts(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n"); 4576 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP 4577 seq_puts(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n" 4578 "# Takes a snapshot of the main buffer for this cpu.\n"); 4579 #else 4580 seq_puts(m, "# echo 1 > snapshot : Not supported with this kernel.\n" 4581 "# Must use main snapshot file to allocate.\n"); 4582 #endif 4583 seq_puts(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n" 4584 "# (Doesn't have to be '2' works with any number that\n" 4585 "# is not a '0' or '1')\n"); 4586 } 4587 4588 static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) 4589 { 4590 if (iter->tr->allocated_snapshot) 4591 seq_puts(m, "#\n# * Snapshot is allocated *\n#\n"); 4592 else 4593 seq_puts(m, "#\n# * Snapshot is freed *\n#\n"); 4594 4595 seq_puts(m, "# Snapshot commands:\n"); 4596 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) 4597 show_snapshot_main_help(m); 4598 else 4599 show_snapshot_percpu_help(m); 4600 } 4601 #else 4602 /* Should never be called */ 4603 static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { } 4604 #endif 4605 4606 static int s_show(struct seq_file *m, void *v) 4607 { 4608 struct trace_iterator *iter = v; 4609 int ret; 4610 4611 if (iter->ent == NULL) { 4612 if (iter->tr) { 4613 seq_printf(m, "# tracer: %s\n", iter->trace->name); 4614 seq_puts(m, "#\n"); 4615 test_ftrace_alive(m); 4616 } 4617 if (iter->snapshot && trace_empty(iter)) 4618 print_snapshot_help(m, iter); 4619 else if (iter->trace && iter->trace->print_header) 4620 iter->trace->print_header(m); 4621 else 4622 trace_default_header(m); 4623 4624 } else if (iter->leftover) { 4625 /* 4626 * If we filled the seq_file buffer earlier, we 4627 * want to just show it now. 4628 */ 4629 ret = trace_print_seq(m, &iter->seq); 4630 4631 /* ret should this time be zero, but you never know */ 4632 iter->leftover = ret; 4633 4634 } else { 4635 print_trace_line(iter); 4636 ret = trace_print_seq(m, &iter->seq); 4637 /* 4638 * If we overflow the seq_file buffer, then it will 4639 * ask us for this data again at start up. 4640 * Use that instead. 4641 * ret is 0 if seq_file write succeeded. 4642 * -1 otherwise. 4643 */ 4644 iter->leftover = ret; 4645 } 4646 4647 return 0; 4648 } 4649 4650 /* 4651 * Should be used after trace_array_get(), trace_types_lock 4652 * ensures that i_cdev was already initialized. 4653 */ 4654 static inline int tracing_get_cpu(struct inode *inode) 4655 { 4656 if (inode->i_cdev) /* See trace_create_cpu_file() */ 4657 return (long)inode->i_cdev - 1; 4658 return RING_BUFFER_ALL_CPUS; 4659 } 4660 4661 static const struct seq_operations tracer_seq_ops = { 4662 .start = s_start, 4663 .next = s_next, 4664 .stop = s_stop, 4665 .show = s_show, 4666 }; 4667 4668 static struct trace_iterator * 4669 __tracing_open(struct inode *inode, struct file *file, bool snapshot) 4670 { 4671 struct trace_array *tr = inode->i_private; 4672 struct trace_iterator *iter; 4673 int cpu; 4674 4675 if (tracing_disabled) 4676 return ERR_PTR(-ENODEV); 4677 4678 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter)); 4679 if (!iter) 4680 return ERR_PTR(-ENOMEM); 4681 4682 iter->buffer_iter = kcalloc(nr_cpu_ids, sizeof(*iter->buffer_iter), 4683 GFP_KERNEL); 4684 if (!iter->buffer_iter) 4685 goto release; 4686 4687 /* 4688 * trace_find_next_entry() may need to save off iter->ent. 4689 * It will place it into the iter->temp buffer. As most 4690 * events are less than 128, allocate a buffer of that size. 4691 * If one is greater, then trace_find_next_entry() will 4692 * allocate a new buffer to adjust for the bigger iter->ent. 4693 * It's not critical if it fails to get allocated here. 4694 */ 4695 iter->temp = kmalloc(128, GFP_KERNEL); 4696 if (iter->temp) 4697 iter->temp_size = 128; 4698 4699 /* 4700 * trace_event_printf() may need to modify given format 4701 * string to replace %p with %px so that it shows real address 4702 * instead of hash value. However, that is only for the event 4703 * tracing, other tracer may not need. Defer the allocation 4704 * until it is needed. 4705 */ 4706 iter->fmt = NULL; 4707 iter->fmt_size = 0; 4708 4709 /* 4710 * We make a copy of the current tracer to avoid concurrent 4711 * changes on it while we are reading. 4712 */ 4713 mutex_lock(&trace_types_lock); 4714 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL); 4715 if (!iter->trace) 4716 goto fail; 4717 4718 *iter->trace = *tr->current_trace; 4719 4720 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL)) 4721 goto fail; 4722 4723 iter->tr = tr; 4724 4725 #ifdef CONFIG_TRACER_MAX_TRACE 4726 /* Currently only the top directory has a snapshot */ 4727 if (tr->current_trace->print_max || snapshot) 4728 iter->array_buffer = &tr->max_buffer; 4729 else 4730 #endif 4731 iter->array_buffer = &tr->array_buffer; 4732 iter->snapshot = snapshot; 4733 iter->pos = -1; 4734 iter->cpu_file = tracing_get_cpu(inode); 4735 mutex_init(&iter->mutex); 4736 4737 /* Notify the tracer early; before we stop tracing. */ 4738 if (iter->trace->open) 4739 iter->trace->open(iter); 4740 4741 /* Annotate start of buffers if we had overruns */ 4742 if (ring_buffer_overruns(iter->array_buffer->buffer)) 4743 iter->iter_flags |= TRACE_FILE_ANNOTATE; 4744 4745 /* Output in nanoseconds only if we are using a clock in nanoseconds. */ 4746 if (trace_clocks[tr->clock_id].in_ns) 4747 iter->iter_flags |= TRACE_FILE_TIME_IN_NS; 4748 4749 /* 4750 * If pause-on-trace is enabled, then stop the trace while 4751 * dumping, unless this is the "snapshot" file 4752 */ 4753 if (!iter->snapshot && (tr->trace_flags & TRACE_ITER_PAUSE_ON_TRACE)) 4754 tracing_stop_tr(tr); 4755 4756 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) { 4757 for_each_tracing_cpu(cpu) { 4758 iter->buffer_iter[cpu] = 4759 ring_buffer_read_prepare(iter->array_buffer->buffer, 4760 cpu, GFP_KERNEL); 4761 } 4762 ring_buffer_read_prepare_sync(); 4763 for_each_tracing_cpu(cpu) { 4764 ring_buffer_read_start(iter->buffer_iter[cpu]); 4765 tracing_iter_reset(iter, cpu); 4766 } 4767 } else { 4768 cpu = iter->cpu_file; 4769 iter->buffer_iter[cpu] = 4770 ring_buffer_read_prepare(iter->array_buffer->buffer, 4771 cpu, GFP_KERNEL); 4772 ring_buffer_read_prepare_sync(); 4773 ring_buffer_read_start(iter->buffer_iter[cpu]); 4774 tracing_iter_reset(iter, cpu); 4775 } 4776 4777 mutex_unlock(&trace_types_lock); 4778 4779 return iter; 4780 4781 fail: 4782 mutex_unlock(&trace_types_lock); 4783 kfree(iter->trace); 4784 kfree(iter->temp); 4785 kfree(iter->buffer_iter); 4786 release: 4787 seq_release_private(inode, file); 4788 return ERR_PTR(-ENOMEM); 4789 } 4790 4791 int tracing_open_generic(struct inode *inode, struct file *filp) 4792 { 4793 int ret; 4794 4795 ret = tracing_check_open_get_tr(NULL); 4796 if (ret) 4797 return ret; 4798 4799 filp->private_data = inode->i_private; 4800 return 0; 4801 } 4802 4803 bool tracing_is_disabled(void) 4804 { 4805 return (tracing_disabled) ? true: false; 4806 } 4807 4808 /* 4809 * Open and update trace_array ref count. 4810 * Must have the current trace_array passed to it. 4811 */ 4812 int tracing_open_generic_tr(struct inode *inode, struct file *filp) 4813 { 4814 struct trace_array *tr = inode->i_private; 4815 int ret; 4816 4817 ret = tracing_check_open_get_tr(tr); 4818 if (ret) 4819 return ret; 4820 4821 filp->private_data = inode->i_private; 4822 4823 return 0; 4824 } 4825 4826 static int tracing_release(struct inode *inode, struct file *file) 4827 { 4828 struct trace_array *tr = inode->i_private; 4829 struct seq_file *m = file->private_data; 4830 struct trace_iterator *iter; 4831 int cpu; 4832 4833 if (!(file->f_mode & FMODE_READ)) { 4834 trace_array_put(tr); 4835 return 0; 4836 } 4837 4838 /* Writes do not use seq_file */ 4839 iter = m->private; 4840 mutex_lock(&trace_types_lock); 4841 4842 for_each_tracing_cpu(cpu) { 4843 if (iter->buffer_iter[cpu]) 4844 ring_buffer_read_finish(iter->buffer_iter[cpu]); 4845 } 4846 4847 if (iter->trace && iter->trace->close) 4848 iter->trace->close(iter); 4849 4850 if (!iter->snapshot && tr->stop_count) 4851 /* reenable tracing if it was previously enabled */ 4852 tracing_start_tr(tr); 4853 4854 __trace_array_put(tr); 4855 4856 mutex_unlock(&trace_types_lock); 4857 4858 mutex_destroy(&iter->mutex); 4859 free_cpumask_var(iter->started); 4860 kfree(iter->fmt); 4861 kfree(iter->temp); 4862 kfree(iter->trace); 4863 kfree(iter->buffer_iter); 4864 seq_release_private(inode, file); 4865 4866 return 0; 4867 } 4868 4869 static int tracing_release_generic_tr(struct inode *inode, struct file *file) 4870 { 4871 struct trace_array *tr = inode->i_private; 4872 4873 trace_array_put(tr); 4874 return 0; 4875 } 4876 4877 static int tracing_single_release_tr(struct inode *inode, struct file *file) 4878 { 4879 struct trace_array *tr = inode->i_private; 4880 4881 trace_array_put(tr); 4882 4883 return single_release(inode, file); 4884 } 4885 4886 static int tracing_open(struct inode *inode, struct file *file) 4887 { 4888 struct trace_array *tr = inode->i_private; 4889 struct trace_iterator *iter; 4890 int ret; 4891 4892 ret = tracing_check_open_get_tr(tr); 4893 if (ret) 4894 return ret; 4895 4896 /* If this file was open for write, then erase contents */ 4897 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) { 4898 int cpu = tracing_get_cpu(inode); 4899 struct array_buffer *trace_buf = &tr->array_buffer; 4900 4901 #ifdef CONFIG_TRACER_MAX_TRACE 4902 if (tr->current_trace->print_max) 4903 trace_buf = &tr->max_buffer; 4904 #endif 4905 4906 if (cpu == RING_BUFFER_ALL_CPUS) 4907 tracing_reset_online_cpus(trace_buf); 4908 else 4909 tracing_reset_cpu(trace_buf, cpu); 4910 } 4911 4912 if (file->f_mode & FMODE_READ) { 4913 iter = __tracing_open(inode, file, false); 4914 if (IS_ERR(iter)) 4915 ret = PTR_ERR(iter); 4916 else if (tr->trace_flags & TRACE_ITER_LATENCY_FMT) 4917 iter->iter_flags |= TRACE_FILE_LAT_FMT; 4918 } 4919 4920 if (ret < 0) 4921 trace_array_put(tr); 4922 4923 return ret; 4924 } 4925 4926 /* 4927 * Some tracers are not suitable for instance buffers. 4928 * A tracer is always available for the global array (toplevel) 4929 * or if it explicitly states that it is. 4930 */ 4931 static bool 4932 trace_ok_for_array(struct tracer *t, struct trace_array *tr) 4933 { 4934 return (tr->flags & TRACE_ARRAY_FL_GLOBAL) || t->allow_instances; 4935 } 4936 4937 /* Find the next tracer that this trace array may use */ 4938 static struct tracer * 4939 get_tracer_for_array(struct trace_array *tr, struct tracer *t) 4940 { 4941 while (t && !trace_ok_for_array(t, tr)) 4942 t = t->next; 4943 4944 return t; 4945 } 4946 4947 static void * 4948 t_next(struct seq_file *m, void *v, loff_t *pos) 4949 { 4950 struct trace_array *tr = m->private; 4951 struct tracer *t = v; 4952 4953 (*pos)++; 4954 4955 if (t) 4956 t = get_tracer_for_array(tr, t->next); 4957 4958 return t; 4959 } 4960 4961 static void *t_start(struct seq_file *m, loff_t *pos) 4962 { 4963 struct trace_array *tr = m->private; 4964 struct tracer *t; 4965 loff_t l = 0; 4966 4967 mutex_lock(&trace_types_lock); 4968 4969 t = get_tracer_for_array(tr, trace_types); 4970 for (; t && l < *pos; t = t_next(m, t, &l)) 4971 ; 4972 4973 return t; 4974 } 4975 4976 static void t_stop(struct seq_file *m, void *p) 4977 { 4978 mutex_unlock(&trace_types_lock); 4979 } 4980 4981 static int t_show(struct seq_file *m, void *v) 4982 { 4983 struct tracer *t = v; 4984 4985 if (!t) 4986 return 0; 4987 4988 seq_puts(m, t->name); 4989 if (t->next) 4990 seq_putc(m, ' '); 4991 else 4992 seq_putc(m, '\n'); 4993 4994 return 0; 4995 } 4996 4997 static const struct seq_operations show_traces_seq_ops = { 4998 .start = t_start, 4999 .next = t_next, 5000 .stop = t_stop, 5001 .show = t_show, 5002 }; 5003 5004 static int show_traces_open(struct inode *inode, struct file *file) 5005 { 5006 struct trace_array *tr = inode->i_private; 5007 struct seq_file *m; 5008 int ret; 5009 5010 ret = tracing_check_open_get_tr(tr); 5011 if (ret) 5012 return ret; 5013 5014 ret = seq_open(file, &show_traces_seq_ops); 5015 if (ret) { 5016 trace_array_put(tr); 5017 return ret; 5018 } 5019 5020 m = file->private_data; 5021 m->private = tr; 5022 5023 return 0; 5024 } 5025 5026 static int show_traces_release(struct inode *inode, struct file *file) 5027 { 5028 struct trace_array *tr = inode->i_private; 5029 5030 trace_array_put(tr); 5031 return seq_release(inode, file); 5032 } 5033 5034 static ssize_t 5035 tracing_write_stub(struct file *filp, const char __user *ubuf, 5036 size_t count, loff_t *ppos) 5037 { 5038 return count; 5039 } 5040 5041 loff_t tracing_lseek(struct file *file, loff_t offset, int whence) 5042 { 5043 int ret; 5044 5045 if (file->f_mode & FMODE_READ) 5046 ret = seq_lseek(file, offset, whence); 5047 else 5048 file->f_pos = ret = 0; 5049 5050 return ret; 5051 } 5052 5053 static const struct file_operations tracing_fops = { 5054 .open = tracing_open, 5055 .read = seq_read, 5056 .write = tracing_write_stub, 5057 .llseek = tracing_lseek, 5058 .release = tracing_release, 5059 }; 5060 5061 static const struct file_operations show_traces_fops = { 5062 .open = show_traces_open, 5063 .read = seq_read, 5064 .llseek = seq_lseek, 5065 .release = show_traces_release, 5066 }; 5067 5068 static ssize_t 5069 tracing_cpumask_read(struct file *filp, char __user *ubuf, 5070 size_t count, loff_t *ppos) 5071 { 5072 struct trace_array *tr = file_inode(filp)->i_private; 5073 char *mask_str; 5074 int len; 5075 5076 len = snprintf(NULL, 0, "%*pb\n", 5077 cpumask_pr_args(tr->tracing_cpumask)) + 1; 5078 mask_str = kmalloc(len, GFP_KERNEL); 5079 if (!mask_str) 5080 return -ENOMEM; 5081 5082 len = snprintf(mask_str, len, "%*pb\n", 5083 cpumask_pr_args(tr->tracing_cpumask)); 5084 if (len >= count) { 5085 count = -EINVAL; 5086 goto out_err; 5087 } 5088 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, len); 5089 5090 out_err: 5091 kfree(mask_str); 5092 5093 return count; 5094 } 5095 5096 int tracing_set_cpumask(struct trace_array *tr, 5097 cpumask_var_t tracing_cpumask_new) 5098 { 5099 int cpu; 5100 5101 if (!tr) 5102 return -EINVAL; 5103 5104 local_irq_disable(); 5105 arch_spin_lock(&tr->max_lock); 5106 for_each_tracing_cpu(cpu) { 5107 /* 5108 * Increase/decrease the disabled counter if we are 5109 * about to flip a bit in the cpumask: 5110 */ 5111 if (cpumask_test_cpu(cpu, tr->tracing_cpumask) && 5112 !cpumask_test_cpu(cpu, tracing_cpumask_new)) { 5113 atomic_inc(&per_cpu_ptr(tr->array_buffer.data, cpu)->disabled); 5114 ring_buffer_record_disable_cpu(tr->array_buffer.buffer, cpu); 5115 } 5116 if (!cpumask_test_cpu(cpu, tr->tracing_cpumask) && 5117 cpumask_test_cpu(cpu, tracing_cpumask_new)) { 5118 atomic_dec(&per_cpu_ptr(tr->array_buffer.data, cpu)->disabled); 5119 ring_buffer_record_enable_cpu(tr->array_buffer.buffer, cpu); 5120 } 5121 } 5122 arch_spin_unlock(&tr->max_lock); 5123 local_irq_enable(); 5124 5125 cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new); 5126 5127 return 0; 5128 } 5129 5130 static ssize_t 5131 tracing_cpumask_write(struct file *filp, const char __user *ubuf, 5132 size_t count, loff_t *ppos) 5133 { 5134 struct trace_array *tr = file_inode(filp)->i_private; 5135 cpumask_var_t tracing_cpumask_new; 5136 int err; 5137 5138 if (!zalloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL)) 5139 return -ENOMEM; 5140 5141 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new); 5142 if (err) 5143 goto err_free; 5144 5145 err = tracing_set_cpumask(tr, tracing_cpumask_new); 5146 if (err) 5147 goto err_free; 5148 5149 free_cpumask_var(tracing_cpumask_new); 5150 5151 return count; 5152 5153 err_free: 5154 free_cpumask_var(tracing_cpumask_new); 5155 5156 return err; 5157 } 5158 5159 static const struct file_operations tracing_cpumask_fops = { 5160 .open = tracing_open_generic_tr, 5161 .read = tracing_cpumask_read, 5162 .write = tracing_cpumask_write, 5163 .release = tracing_release_generic_tr, 5164 .llseek = generic_file_llseek, 5165 }; 5166 5167 static int tracing_trace_options_show(struct seq_file *m, void *v) 5168 { 5169 struct tracer_opt *trace_opts; 5170 struct trace_array *tr = m->private; 5171 u32 tracer_flags; 5172 int i; 5173 5174 mutex_lock(&trace_types_lock); 5175 tracer_flags = tr->current_trace->flags->val; 5176 trace_opts = tr->current_trace->flags->opts; 5177 5178 for (i = 0; trace_options[i]; i++) { 5179 if (tr->trace_flags & (1 << i)) 5180 seq_printf(m, "%s\n", trace_options[i]); 5181 else 5182 seq_printf(m, "no%s\n", trace_options[i]); 5183 } 5184 5185 for (i = 0; trace_opts[i].name; i++) { 5186 if (tracer_flags & trace_opts[i].bit) 5187 seq_printf(m, "%s\n", trace_opts[i].name); 5188 else 5189 seq_printf(m, "no%s\n", trace_opts[i].name); 5190 } 5191 mutex_unlock(&trace_types_lock); 5192 5193 return 0; 5194 } 5195 5196 static int __set_tracer_option(struct trace_array *tr, 5197 struct tracer_flags *tracer_flags, 5198 struct tracer_opt *opts, int neg) 5199 { 5200 struct tracer *trace = tracer_flags->trace; 5201 int ret; 5202 5203 ret = trace->set_flag(tr, tracer_flags->val, opts->bit, !neg); 5204 if (ret) 5205 return ret; 5206 5207 if (neg) 5208 tracer_flags->val &= ~opts->bit; 5209 else 5210 tracer_flags->val |= opts->bit; 5211 return 0; 5212 } 5213 5214 /* Try to assign a tracer specific option */ 5215 static int set_tracer_option(struct trace_array *tr, char *cmp, int neg) 5216 { 5217 struct tracer *trace = tr->current_trace; 5218 struct tracer_flags *tracer_flags = trace->flags; 5219 struct tracer_opt *opts = NULL; 5220 int i; 5221 5222 for (i = 0; tracer_flags->opts[i].name; i++) { 5223 opts = &tracer_flags->opts[i]; 5224 5225 if (strcmp(cmp, opts->name) == 0) 5226 return __set_tracer_option(tr, trace->flags, opts, neg); 5227 } 5228 5229 return -EINVAL; 5230 } 5231 5232 /* Some tracers require overwrite to stay enabled */ 5233 int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set) 5234 { 5235 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set) 5236 return -1; 5237 5238 return 0; 5239 } 5240 5241 int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled) 5242 { 5243 int *map; 5244 5245 if ((mask == TRACE_ITER_RECORD_TGID) || 5246 (mask == TRACE_ITER_RECORD_CMD)) 5247 lockdep_assert_held(&event_mutex); 5248 5249 /* do nothing if flag is already set */ 5250 if (!!(tr->trace_flags & mask) == !!enabled) 5251 return 0; 5252 5253 /* Give the tracer a chance to approve the change */ 5254 if (tr->current_trace->flag_changed) 5255 if (tr->current_trace->flag_changed(tr, mask, !!enabled)) 5256 return -EINVAL; 5257 5258 if (enabled) 5259 tr->trace_flags |= mask; 5260 else 5261 tr->trace_flags &= ~mask; 5262 5263 if (mask == TRACE_ITER_RECORD_CMD) 5264 trace_event_enable_cmd_record(enabled); 5265 5266 if (mask == TRACE_ITER_RECORD_TGID) { 5267 if (!tgid_map) { 5268 tgid_map_max = pid_max; 5269 map = kvcalloc(tgid_map_max + 1, sizeof(*tgid_map), 5270 GFP_KERNEL); 5271 5272 /* 5273 * Pairs with smp_load_acquire() in 5274 * trace_find_tgid_ptr() to ensure that if it observes 5275 * the tgid_map we just allocated then it also observes 5276 * the corresponding tgid_map_max value. 5277 */ 5278 smp_store_release(&tgid_map, map); 5279 } 5280 if (!tgid_map) { 5281 tr->trace_flags &= ~TRACE_ITER_RECORD_TGID; 5282 return -ENOMEM; 5283 } 5284 5285 trace_event_enable_tgid_record(enabled); 5286 } 5287 5288 if (mask == TRACE_ITER_EVENT_FORK) 5289 trace_event_follow_fork(tr, enabled); 5290 5291 if (mask == TRACE_ITER_FUNC_FORK) 5292 ftrace_pid_follow_fork(tr, enabled); 5293 5294 if (mask == TRACE_ITER_OVERWRITE) { 5295 ring_buffer_change_overwrite(tr->array_buffer.buffer, enabled); 5296 #ifdef CONFIG_TRACER_MAX_TRACE 5297 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled); 5298 #endif 5299 } 5300 5301 if (mask == TRACE_ITER_PRINTK) { 5302 trace_printk_start_stop_comm(enabled); 5303 trace_printk_control(enabled); 5304 } 5305 5306 return 0; 5307 } 5308 5309 int trace_set_options(struct trace_array *tr, char *option) 5310 { 5311 char *cmp; 5312 int neg = 0; 5313 int ret; 5314 size_t orig_len = strlen(option); 5315 int len; 5316 5317 cmp = strstrip(option); 5318 5319 len = str_has_prefix(cmp, "no"); 5320 if (len) 5321 neg = 1; 5322 5323 cmp += len; 5324 5325 mutex_lock(&event_mutex); 5326 mutex_lock(&trace_types_lock); 5327 5328 ret = match_string(trace_options, -1, cmp); 5329 /* If no option could be set, test the specific tracer options */ 5330 if (ret < 0) 5331 ret = set_tracer_option(tr, cmp, neg); 5332 else 5333 ret = set_tracer_flag(tr, 1 << ret, !neg); 5334 5335 mutex_unlock(&trace_types_lock); 5336 mutex_unlock(&event_mutex); 5337 5338 /* 5339 * If the first trailing whitespace is replaced with '\0' by strstrip, 5340 * turn it back into a space. 5341 */ 5342 if (orig_len > strlen(option)) 5343 option[strlen(option)] = ' '; 5344 5345 return ret; 5346 } 5347 5348 static void __init apply_trace_boot_options(void) 5349 { 5350 char *buf = trace_boot_options_buf; 5351 char *option; 5352 5353 while (true) { 5354 option = strsep(&buf, ","); 5355 5356 if (!option) 5357 break; 5358 5359 if (*option) 5360 trace_set_options(&global_trace, option); 5361 5362 /* Put back the comma to allow this to be called again */ 5363 if (buf) 5364 *(buf - 1) = ','; 5365 } 5366 } 5367 5368 static ssize_t 5369 tracing_trace_options_write(struct file *filp, const char __user *ubuf, 5370 size_t cnt, loff_t *ppos) 5371 { 5372 struct seq_file *m = filp->private_data; 5373 struct trace_array *tr = m->private; 5374 char buf[64]; 5375 int ret; 5376 5377 if (cnt >= sizeof(buf)) 5378 return -EINVAL; 5379 5380 if (copy_from_user(buf, ubuf, cnt)) 5381 return -EFAULT; 5382 5383 buf[cnt] = 0; 5384 5385 ret = trace_set_options(tr, buf); 5386 if (ret < 0) 5387 return ret; 5388 5389 *ppos += cnt; 5390 5391 return cnt; 5392 } 5393 5394 static int tracing_trace_options_open(struct inode *inode, struct file *file) 5395 { 5396 struct trace_array *tr = inode->i_private; 5397 int ret; 5398 5399 ret = tracing_check_open_get_tr(tr); 5400 if (ret) 5401 return ret; 5402 5403 ret = single_open(file, tracing_trace_options_show, inode->i_private); 5404 if (ret < 0) 5405 trace_array_put(tr); 5406 5407 return ret; 5408 } 5409 5410 static const struct file_operations tracing_iter_fops = { 5411 .open = tracing_trace_options_open, 5412 .read = seq_read, 5413 .llseek = seq_lseek, 5414 .release = tracing_single_release_tr, 5415 .write = tracing_trace_options_write, 5416 }; 5417 5418 static const char readme_msg[] = 5419 "tracing mini-HOWTO:\n\n" 5420 "# echo 0 > tracing_on : quick way to disable tracing\n" 5421 "# echo 1 > tracing_on : quick way to re-enable tracing\n\n" 5422 " Important files:\n" 5423 " trace\t\t\t- The static contents of the buffer\n" 5424 "\t\t\t To clear the buffer write into this file: echo > trace\n" 5425 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n" 5426 " current_tracer\t- function and latency tracers\n" 5427 " available_tracers\t- list of configured tracers for current_tracer\n" 5428 " error_log\t- error log for failed commands (that support it)\n" 5429 " buffer_size_kb\t- view and modify size of per cpu buffer\n" 5430 " buffer_total_size_kb - view total size of all cpu buffers\n\n" 5431 " trace_clock\t\t-change the clock used to order events\n" 5432 " local: Per cpu clock but may not be synced across CPUs\n" 5433 " global: Synced across CPUs but slows tracing down.\n" 5434 " counter: Not a clock, but just an increment\n" 5435 " uptime: Jiffy counter from time of boot\n" 5436 " perf: Same clock that perf events use\n" 5437 #ifdef CONFIG_X86_64 5438 " x86-tsc: TSC cycle counter\n" 5439 #endif 5440 "\n timestamp_mode\t-view the mode used to timestamp events\n" 5441 " delta: Delta difference against a buffer-wide timestamp\n" 5442 " absolute: Absolute (standalone) timestamp\n" 5443 "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n" 5444 "\n trace_marker_raw\t\t- Writes into this file writes binary data into the kernel buffer\n" 5445 " tracing_cpumask\t- Limit which CPUs to trace\n" 5446 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n" 5447 "\t\t\t Remove sub-buffer with rmdir\n" 5448 " trace_options\t\t- Set format or modify how tracing happens\n" 5449 "\t\t\t Disable an option by prefixing 'no' to the\n" 5450 "\t\t\t option name\n" 5451 " saved_cmdlines_size\t- echo command number in here to store comm-pid list\n" 5452 #ifdef CONFIG_DYNAMIC_FTRACE 5453 "\n available_filter_functions - list of functions that can be filtered on\n" 5454 " set_ftrace_filter\t- echo function name in here to only trace these\n" 5455 "\t\t\t functions\n" 5456 "\t accepts: func_full_name or glob-matching-pattern\n" 5457 "\t modules: Can select a group via module\n" 5458 "\t Format: :mod:<module-name>\n" 5459 "\t example: echo :mod:ext3 > set_ftrace_filter\n" 5460 "\t triggers: a command to perform when function is hit\n" 5461 "\t Format: <function>:<trigger>[:count]\n" 5462 "\t trigger: traceon, traceoff\n" 5463 "\t\t enable_event:<system>:<event>\n" 5464 "\t\t disable_event:<system>:<event>\n" 5465 #ifdef CONFIG_STACKTRACE 5466 "\t\t stacktrace\n" 5467 #endif 5468 #ifdef CONFIG_TRACER_SNAPSHOT 5469 "\t\t snapshot\n" 5470 #endif 5471 "\t\t dump\n" 5472 "\t\t cpudump\n" 5473 "\t example: echo do_fault:traceoff > set_ftrace_filter\n" 5474 "\t echo do_trap:traceoff:3 > set_ftrace_filter\n" 5475 "\t The first one will disable tracing every time do_fault is hit\n" 5476 "\t The second will disable tracing at most 3 times when do_trap is hit\n" 5477 "\t The first time do trap is hit and it disables tracing, the\n" 5478 "\t counter will decrement to 2. If tracing is already disabled,\n" 5479 "\t the counter will not decrement. It only decrements when the\n" 5480 "\t trigger did work\n" 5481 "\t To remove trigger without count:\n" 5482 "\t echo '!<function>:<trigger> > set_ftrace_filter\n" 5483 "\t To remove trigger with a count:\n" 5484 "\t echo '!<function>:<trigger>:0 > set_ftrace_filter\n" 5485 " set_ftrace_notrace\t- echo function name in here to never trace.\n" 5486 "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n" 5487 "\t modules: Can select a group via module command :mod:\n" 5488 "\t Does not accept triggers\n" 5489 #endif /* CONFIG_DYNAMIC_FTRACE */ 5490 #ifdef CONFIG_FUNCTION_TRACER 5491 " set_ftrace_pid\t- Write pid(s) to only function trace those pids\n" 5492 "\t\t (function)\n" 5493 " set_ftrace_notrace_pid\t- Write pid(s) to not function trace those pids\n" 5494 "\t\t (function)\n" 5495 #endif 5496 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 5497 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n" 5498 " set_graph_notrace\t- Do not trace the nested calls of a function (function_graph)\n" 5499 " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n" 5500 #endif 5501 #ifdef CONFIG_TRACER_SNAPSHOT 5502 "\n snapshot\t\t- Like 'trace' but shows the content of the static\n" 5503 "\t\t\t snapshot buffer. Read the contents for more\n" 5504 "\t\t\t information\n" 5505 #endif 5506 #ifdef CONFIG_STACK_TRACER 5507 " stack_trace\t\t- Shows the max stack trace when active\n" 5508 " stack_max_size\t- Shows current max stack size that was traced\n" 5509 "\t\t\t Write into this file to reset the max size (trigger a\n" 5510 "\t\t\t new trace)\n" 5511 #ifdef CONFIG_DYNAMIC_FTRACE 5512 " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace\n" 5513 "\t\t\t traces\n" 5514 #endif 5515 #endif /* CONFIG_STACK_TRACER */ 5516 #ifdef CONFIG_DYNAMIC_EVENTS 5517 " dynamic_events\t\t- Create/append/remove/show the generic dynamic events\n" 5518 "\t\t\t Write into this file to define/undefine new trace events.\n" 5519 #endif 5520 #ifdef CONFIG_KPROBE_EVENTS 5521 " kprobe_events\t\t- Create/append/remove/show the kernel dynamic events\n" 5522 "\t\t\t Write into this file to define/undefine new trace events.\n" 5523 #endif 5524 #ifdef CONFIG_UPROBE_EVENTS 5525 " uprobe_events\t\t- Create/append/remove/show the userspace dynamic events\n" 5526 "\t\t\t Write into this file to define/undefine new trace events.\n" 5527 #endif 5528 #if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS) 5529 "\t accepts: event-definitions (one definition per line)\n" 5530 "\t Format: p[:[<group>/]<event>] <place> [<args>]\n" 5531 "\t r[maxactive][:[<group>/]<event>] <place> [<args>]\n" 5532 #ifdef CONFIG_HIST_TRIGGERS 5533 "\t s:[synthetic/]<event> <field> [<field>]\n" 5534 #endif 5535 "\t e[:[<group>/]<event>] <attached-group>.<attached-event> [<args>]\n" 5536 "\t -:[<group>/]<event>\n" 5537 #ifdef CONFIG_KPROBE_EVENTS 5538 "\t place: [<module>:]<symbol>[+<offset>]|<memaddr>\n" 5539 "place (kretprobe): [<module>:]<symbol>[+<offset>]%return|<memaddr>\n" 5540 #endif 5541 #ifdef CONFIG_UPROBE_EVENTS 5542 " place (uprobe): <path>:<offset>[%return][(ref_ctr_offset)]\n" 5543 #endif 5544 "\t args: <name>=fetcharg[:type]\n" 5545 "\t fetcharg: (%<register>|$<efield>), @<address>, @<symbol>[+|-<offset>],\n" 5546 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API 5547 "\t $stack<index>, $stack, $retval, $comm, $arg<N>,\n" 5548 #else 5549 "\t $stack<index>, $stack, $retval, $comm,\n" 5550 #endif 5551 "\t +|-[u]<offset>(<fetcharg>), \\imm-value, \\\"imm-string\"\n" 5552 "\t type: s8/16/32/64, u8/16/32/64, x8/16/32/64, string, symbol,\n" 5553 "\t b<bit-width>@<bit-offset>/<container-size>, ustring,\n" 5554 "\t <type>\\[<array-size>\\]\n" 5555 #ifdef CONFIG_HIST_TRIGGERS 5556 "\t field: <stype> <name>;\n" 5557 "\t stype: u8/u16/u32/u64, s8/s16/s32/s64, pid_t,\n" 5558 "\t [unsigned] char/int/long\n" 5559 #endif 5560 "\t efield: For event probes ('e' types), the field is on of the fields\n" 5561 "\t of the <attached-group>/<attached-event>.\n" 5562 #endif 5563 " events/\t\t- Directory containing all trace event subsystems:\n" 5564 " enable\t\t- Write 0/1 to enable/disable tracing of all events\n" 5565 " events/<system>/\t- Directory containing all trace events for <system>:\n" 5566 " enable\t\t- Write 0/1 to enable/disable tracing of all <system>\n" 5567 "\t\t\t events\n" 5568 " filter\t\t- If set, only events passing filter are traced\n" 5569 " events/<system>/<event>/\t- Directory containing control files for\n" 5570 "\t\t\t <event>:\n" 5571 " enable\t\t- Write 0/1 to enable/disable tracing of <event>\n" 5572 " filter\t\t- If set, only events passing filter are traced\n" 5573 " trigger\t\t- If set, a command to perform when event is hit\n" 5574 "\t Format: <trigger>[:count][if <filter>]\n" 5575 "\t trigger: traceon, traceoff\n" 5576 "\t enable_event:<system>:<event>\n" 5577 "\t disable_event:<system>:<event>\n" 5578 #ifdef CONFIG_HIST_TRIGGERS 5579 "\t enable_hist:<system>:<event>\n" 5580 "\t disable_hist:<system>:<event>\n" 5581 #endif 5582 #ifdef CONFIG_STACKTRACE 5583 "\t\t stacktrace\n" 5584 #endif 5585 #ifdef CONFIG_TRACER_SNAPSHOT 5586 "\t\t snapshot\n" 5587 #endif 5588 #ifdef CONFIG_HIST_TRIGGERS 5589 "\t\t hist (see below)\n" 5590 #endif 5591 "\t example: echo traceoff > events/block/block_unplug/trigger\n" 5592 "\t echo traceoff:3 > events/block/block_unplug/trigger\n" 5593 "\t echo 'enable_event:kmem:kmalloc:3 if nr_rq > 1' > \\\n" 5594 "\t events/block/block_unplug/trigger\n" 5595 "\t The first disables tracing every time block_unplug is hit.\n" 5596 "\t The second disables tracing the first 3 times block_unplug is hit.\n" 5597 "\t The third enables the kmalloc event the first 3 times block_unplug\n" 5598 "\t is hit and has value of greater than 1 for the 'nr_rq' event field.\n" 5599 "\t Like function triggers, the counter is only decremented if it\n" 5600 "\t enabled or disabled tracing.\n" 5601 "\t To remove a trigger without a count:\n" 5602 "\t echo '!<trigger> > <system>/<event>/trigger\n" 5603 "\t To remove a trigger with a count:\n" 5604 "\t echo '!<trigger>:0 > <system>/<event>/trigger\n" 5605 "\t Filters can be ignored when removing a trigger.\n" 5606 #ifdef CONFIG_HIST_TRIGGERS 5607 " hist trigger\t- If set, event hits are aggregated into a hash table\n" 5608 "\t Format: hist:keys=<field1[,field2,...]>\n" 5609 "\t [:values=<field1[,field2,...]>]\n" 5610 "\t [:sort=<field1[,field2,...]>]\n" 5611 "\t [:size=#entries]\n" 5612 "\t [:pause][:continue][:clear]\n" 5613 "\t [:name=histname1]\n" 5614 "\t [:<handler>.<action>]\n" 5615 "\t [if <filter>]\n\n" 5616 "\t Note, special fields can be used as well:\n" 5617 "\t common_timestamp - to record current timestamp\n" 5618 "\t common_cpu - to record the CPU the event happened on\n" 5619 "\n" 5620 "\t When a matching event is hit, an entry is added to a hash\n" 5621 "\t table using the key(s) and value(s) named, and the value of a\n" 5622 "\t sum called 'hitcount' is incremented. Keys and values\n" 5623 "\t correspond to fields in the event's format description. Keys\n" 5624 "\t can be any field, or the special string 'stacktrace'.\n" 5625 "\t Compound keys consisting of up to two fields can be specified\n" 5626 "\t by the 'keys' keyword. Values must correspond to numeric\n" 5627 "\t fields. Sort keys consisting of up to two fields can be\n" 5628 "\t specified using the 'sort' keyword. The sort direction can\n" 5629 "\t be modified by appending '.descending' or '.ascending' to a\n" 5630 "\t sort field. The 'size' parameter can be used to specify more\n" 5631 "\t or fewer than the default 2048 entries for the hashtable size.\n" 5632 "\t If a hist trigger is given a name using the 'name' parameter,\n" 5633 "\t its histogram data will be shared with other triggers of the\n" 5634 "\t same name, and trigger hits will update this common data.\n\n" 5635 "\t Reading the 'hist' file for the event will dump the hash\n" 5636 "\t table in its entirety to stdout. If there are multiple hist\n" 5637 "\t triggers attached to an event, there will be a table for each\n" 5638 "\t trigger in the output. The table displayed for a named\n" 5639 "\t trigger will be the same as any other instance having the\n" 5640 "\t same name. The default format used to display a given field\n" 5641 "\t can be modified by appending any of the following modifiers\n" 5642 "\t to the field name, as applicable:\n\n" 5643 "\t .hex display a number as a hex value\n" 5644 "\t .sym display an address as a symbol\n" 5645 "\t .sym-offset display an address as a symbol and offset\n" 5646 "\t .execname display a common_pid as a program name\n" 5647 "\t .syscall display a syscall id as a syscall name\n" 5648 "\t .log2 display log2 value rather than raw number\n" 5649 "\t .buckets=size display values in groups of size rather than raw number\n" 5650 "\t .usecs display a common_timestamp in microseconds\n\n" 5651 "\t The 'pause' parameter can be used to pause an existing hist\n" 5652 "\t trigger or to start a hist trigger but not log any events\n" 5653 "\t until told to do so. 'continue' can be used to start or\n" 5654 "\t restart a paused hist trigger.\n\n" 5655 "\t The 'clear' parameter will clear the contents of a running\n" 5656 "\t hist trigger and leave its current paused/active state\n" 5657 "\t unchanged.\n\n" 5658 "\t The enable_hist and disable_hist triggers can be used to\n" 5659 "\t have one event conditionally start and stop another event's\n" 5660 "\t already-attached hist trigger. The syntax is analogous to\n" 5661 "\t the enable_event and disable_event triggers.\n\n" 5662 "\t Hist trigger handlers and actions are executed whenever a\n" 5663 "\t a histogram entry is added or updated. They take the form:\n\n" 5664 "\t <handler>.<action>\n\n" 5665 "\t The available handlers are:\n\n" 5666 "\t onmatch(matching.event) - invoke on addition or update\n" 5667 "\t onmax(var) - invoke if var exceeds current max\n" 5668 "\t onchange(var) - invoke action if var changes\n\n" 5669 "\t The available actions are:\n\n" 5670 "\t trace(<synthetic_event>,param list) - generate synthetic event\n" 5671 "\t save(field,...) - save current event fields\n" 5672 #ifdef CONFIG_TRACER_SNAPSHOT 5673 "\t snapshot() - snapshot the trace buffer\n\n" 5674 #endif 5675 #ifdef CONFIG_SYNTH_EVENTS 5676 " events/synthetic_events\t- Create/append/remove/show synthetic events\n" 5677 "\t Write into this file to define/undefine new synthetic events.\n" 5678 "\t example: echo 'myevent u64 lat; char name[]' >> synthetic_events\n" 5679 #endif 5680 #endif 5681 ; 5682 5683 static ssize_t 5684 tracing_readme_read(struct file *filp, char __user *ubuf, 5685 size_t cnt, loff_t *ppos) 5686 { 5687 return simple_read_from_buffer(ubuf, cnt, ppos, 5688 readme_msg, strlen(readme_msg)); 5689 } 5690 5691 static const struct file_operations tracing_readme_fops = { 5692 .open = tracing_open_generic, 5693 .read = tracing_readme_read, 5694 .llseek = generic_file_llseek, 5695 }; 5696 5697 static void *saved_tgids_next(struct seq_file *m, void *v, loff_t *pos) 5698 { 5699 int pid = ++(*pos); 5700 5701 return trace_find_tgid_ptr(pid); 5702 } 5703 5704 static void *saved_tgids_start(struct seq_file *m, loff_t *pos) 5705 { 5706 int pid = *pos; 5707 5708 return trace_find_tgid_ptr(pid); 5709 } 5710 5711 static void saved_tgids_stop(struct seq_file *m, void *v) 5712 { 5713 } 5714 5715 static int saved_tgids_show(struct seq_file *m, void *v) 5716 { 5717 int *entry = (int *)v; 5718 int pid = entry - tgid_map; 5719 int tgid = *entry; 5720 5721 if (tgid == 0) 5722 return SEQ_SKIP; 5723 5724 seq_printf(m, "%d %d\n", pid, tgid); 5725 return 0; 5726 } 5727 5728 static const struct seq_operations tracing_saved_tgids_seq_ops = { 5729 .start = saved_tgids_start, 5730 .stop = saved_tgids_stop, 5731 .next = saved_tgids_next, 5732 .show = saved_tgids_show, 5733 }; 5734 5735 static int tracing_saved_tgids_open(struct inode *inode, struct file *filp) 5736 { 5737 int ret; 5738 5739 ret = tracing_check_open_get_tr(NULL); 5740 if (ret) 5741 return ret; 5742 5743 return seq_open(filp, &tracing_saved_tgids_seq_ops); 5744 } 5745 5746 5747 static const struct file_operations tracing_saved_tgids_fops = { 5748 .open = tracing_saved_tgids_open, 5749 .read = seq_read, 5750 .llseek = seq_lseek, 5751 .release = seq_release, 5752 }; 5753 5754 static void *saved_cmdlines_next(struct seq_file *m, void *v, loff_t *pos) 5755 { 5756 unsigned int *ptr = v; 5757 5758 if (*pos || m->count) 5759 ptr++; 5760 5761 (*pos)++; 5762 5763 for (; ptr < &savedcmd->map_cmdline_to_pid[savedcmd->cmdline_num]; 5764 ptr++) { 5765 if (*ptr == -1 || *ptr == NO_CMDLINE_MAP) 5766 continue; 5767 5768 return ptr; 5769 } 5770 5771 return NULL; 5772 } 5773 5774 static void *saved_cmdlines_start(struct seq_file *m, loff_t *pos) 5775 { 5776 void *v; 5777 loff_t l = 0; 5778 5779 preempt_disable(); 5780 arch_spin_lock(&trace_cmdline_lock); 5781 5782 v = &savedcmd->map_cmdline_to_pid[0]; 5783 while (l <= *pos) { 5784 v = saved_cmdlines_next(m, v, &l); 5785 if (!v) 5786 return NULL; 5787 } 5788 5789 return v; 5790 } 5791 5792 static void saved_cmdlines_stop(struct seq_file *m, void *v) 5793 { 5794 arch_spin_unlock(&trace_cmdline_lock); 5795 preempt_enable(); 5796 } 5797 5798 static int saved_cmdlines_show(struct seq_file *m, void *v) 5799 { 5800 char buf[TASK_COMM_LEN]; 5801 unsigned int *pid = v; 5802 5803 __trace_find_cmdline(*pid, buf); 5804 seq_printf(m, "%d %s\n", *pid, buf); 5805 return 0; 5806 } 5807 5808 static const struct seq_operations tracing_saved_cmdlines_seq_ops = { 5809 .start = saved_cmdlines_start, 5810 .next = saved_cmdlines_next, 5811 .stop = saved_cmdlines_stop, 5812 .show = saved_cmdlines_show, 5813 }; 5814 5815 static int tracing_saved_cmdlines_open(struct inode *inode, struct file *filp) 5816 { 5817 int ret; 5818 5819 ret = tracing_check_open_get_tr(NULL); 5820 if (ret) 5821 return ret; 5822 5823 return seq_open(filp, &tracing_saved_cmdlines_seq_ops); 5824 } 5825 5826 static const struct file_operations tracing_saved_cmdlines_fops = { 5827 .open = tracing_saved_cmdlines_open, 5828 .read = seq_read, 5829 .llseek = seq_lseek, 5830 .release = seq_release, 5831 }; 5832 5833 static ssize_t 5834 tracing_saved_cmdlines_size_read(struct file *filp, char __user *ubuf, 5835 size_t cnt, loff_t *ppos) 5836 { 5837 char buf[64]; 5838 int r; 5839 5840 arch_spin_lock(&trace_cmdline_lock); 5841 r = scnprintf(buf, sizeof(buf), "%u\n", savedcmd->cmdline_num); 5842 arch_spin_unlock(&trace_cmdline_lock); 5843 5844 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); 5845 } 5846 5847 static void free_saved_cmdlines_buffer(struct saved_cmdlines_buffer *s) 5848 { 5849 kfree(s->saved_cmdlines); 5850 kfree(s->map_cmdline_to_pid); 5851 kfree(s); 5852 } 5853 5854 static int tracing_resize_saved_cmdlines(unsigned int val) 5855 { 5856 struct saved_cmdlines_buffer *s, *savedcmd_temp; 5857 5858 s = kmalloc(sizeof(*s), GFP_KERNEL); 5859 if (!s) 5860 return -ENOMEM; 5861 5862 if (allocate_cmdlines_buffer(val, s) < 0) { 5863 kfree(s); 5864 return -ENOMEM; 5865 } 5866 5867 arch_spin_lock(&trace_cmdline_lock); 5868 savedcmd_temp = savedcmd; 5869 savedcmd = s; 5870 arch_spin_unlock(&trace_cmdline_lock); 5871 free_saved_cmdlines_buffer(savedcmd_temp); 5872 5873 return 0; 5874 } 5875 5876 static ssize_t 5877 tracing_saved_cmdlines_size_write(struct file *filp, const char __user *ubuf, 5878 size_t cnt, loff_t *ppos) 5879 { 5880 unsigned long val; 5881 int ret; 5882 5883 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 5884 if (ret) 5885 return ret; 5886 5887 /* must have at least 1 entry or less than PID_MAX_DEFAULT */ 5888 if (!val || val > PID_MAX_DEFAULT) 5889 return -EINVAL; 5890 5891 ret = tracing_resize_saved_cmdlines((unsigned int)val); 5892 if (ret < 0) 5893 return ret; 5894 5895 *ppos += cnt; 5896 5897 return cnt; 5898 } 5899 5900 static const struct file_operations tracing_saved_cmdlines_size_fops = { 5901 .open = tracing_open_generic, 5902 .read = tracing_saved_cmdlines_size_read, 5903 .write = tracing_saved_cmdlines_size_write, 5904 }; 5905 5906 #ifdef CONFIG_TRACE_EVAL_MAP_FILE 5907 static union trace_eval_map_item * 5908 update_eval_map(union trace_eval_map_item *ptr) 5909 { 5910 if (!ptr->map.eval_string) { 5911 if (ptr->tail.next) { 5912 ptr = ptr->tail.next; 5913 /* Set ptr to the next real item (skip head) */ 5914 ptr++; 5915 } else 5916 return NULL; 5917 } 5918 return ptr; 5919 } 5920 5921 static void *eval_map_next(struct seq_file *m, void *v, loff_t *pos) 5922 { 5923 union trace_eval_map_item *ptr = v; 5924 5925 /* 5926 * Paranoid! If ptr points to end, we don't want to increment past it. 5927 * This really should never happen. 5928 */ 5929 (*pos)++; 5930 ptr = update_eval_map(ptr); 5931 if (WARN_ON_ONCE(!ptr)) 5932 return NULL; 5933 5934 ptr++; 5935 ptr = update_eval_map(ptr); 5936 5937 return ptr; 5938 } 5939 5940 static void *eval_map_start(struct seq_file *m, loff_t *pos) 5941 { 5942 union trace_eval_map_item *v; 5943 loff_t l = 0; 5944 5945 mutex_lock(&trace_eval_mutex); 5946 5947 v = trace_eval_maps; 5948 if (v) 5949 v++; 5950 5951 while (v && l < *pos) { 5952 v = eval_map_next(m, v, &l); 5953 } 5954 5955 return v; 5956 } 5957 5958 static void eval_map_stop(struct seq_file *m, void *v) 5959 { 5960 mutex_unlock(&trace_eval_mutex); 5961 } 5962 5963 static int eval_map_show(struct seq_file *m, void *v) 5964 { 5965 union trace_eval_map_item *ptr = v; 5966 5967 seq_printf(m, "%s %ld (%s)\n", 5968 ptr->map.eval_string, ptr->map.eval_value, 5969 ptr->map.system); 5970 5971 return 0; 5972 } 5973 5974 static const struct seq_operations tracing_eval_map_seq_ops = { 5975 .start = eval_map_start, 5976 .next = eval_map_next, 5977 .stop = eval_map_stop, 5978 .show = eval_map_show, 5979 }; 5980 5981 static int tracing_eval_map_open(struct inode *inode, struct file *filp) 5982 { 5983 int ret; 5984 5985 ret = tracing_check_open_get_tr(NULL); 5986 if (ret) 5987 return ret; 5988 5989 return seq_open(filp, &tracing_eval_map_seq_ops); 5990 } 5991 5992 static const struct file_operations tracing_eval_map_fops = { 5993 .open = tracing_eval_map_open, 5994 .read = seq_read, 5995 .llseek = seq_lseek, 5996 .release = seq_release, 5997 }; 5998 5999 static inline union trace_eval_map_item * 6000 trace_eval_jmp_to_tail(union trace_eval_map_item *ptr) 6001 { 6002 /* Return tail of array given the head */ 6003 return ptr + ptr->head.length + 1; 6004 } 6005 6006 static void 6007 trace_insert_eval_map_file(struct module *mod, struct trace_eval_map **start, 6008 int len) 6009 { 6010 struct trace_eval_map **stop; 6011 struct trace_eval_map **map; 6012 union trace_eval_map_item *map_array; 6013 union trace_eval_map_item *ptr; 6014 6015 stop = start + len; 6016 6017 /* 6018 * The trace_eval_maps contains the map plus a head and tail item, 6019 * where the head holds the module and length of array, and the 6020 * tail holds a pointer to the next list. 6021 */ 6022 map_array = kmalloc_array(len + 2, sizeof(*map_array), GFP_KERNEL); 6023 if (!map_array) { 6024 pr_warn("Unable to allocate trace eval mapping\n"); 6025 return; 6026 } 6027 6028 mutex_lock(&trace_eval_mutex); 6029 6030 if (!trace_eval_maps) 6031 trace_eval_maps = map_array; 6032 else { 6033 ptr = trace_eval_maps; 6034 for (;;) { 6035 ptr = trace_eval_jmp_to_tail(ptr); 6036 if (!ptr->tail.next) 6037 break; 6038 ptr = ptr->tail.next; 6039 6040 } 6041 ptr->tail.next = map_array; 6042 } 6043 map_array->head.mod = mod; 6044 map_array->head.length = len; 6045 map_array++; 6046 6047 for (map = start; (unsigned long)map < (unsigned long)stop; map++) { 6048 map_array->map = **map; 6049 map_array++; 6050 } 6051 memset(map_array, 0, sizeof(*map_array)); 6052 6053 mutex_unlock(&trace_eval_mutex); 6054 } 6055 6056 static void trace_create_eval_file(struct dentry *d_tracer) 6057 { 6058 trace_create_file("eval_map", TRACE_MODE_READ, d_tracer, 6059 NULL, &tracing_eval_map_fops); 6060 } 6061 6062 #else /* CONFIG_TRACE_EVAL_MAP_FILE */ 6063 static inline void trace_create_eval_file(struct dentry *d_tracer) { } 6064 static inline void trace_insert_eval_map_file(struct module *mod, 6065 struct trace_eval_map **start, int len) { } 6066 #endif /* !CONFIG_TRACE_EVAL_MAP_FILE */ 6067 6068 static void trace_insert_eval_map(struct module *mod, 6069 struct trace_eval_map **start, int len) 6070 { 6071 struct trace_eval_map **map; 6072 6073 if (len <= 0) 6074 return; 6075 6076 map = start; 6077 6078 trace_event_eval_update(map, len); 6079 6080 trace_insert_eval_map_file(mod, start, len); 6081 } 6082 6083 static ssize_t 6084 tracing_set_trace_read(struct file *filp, char __user *ubuf, 6085 size_t cnt, loff_t *ppos) 6086 { 6087 struct trace_array *tr = filp->private_data; 6088 char buf[MAX_TRACER_SIZE+2]; 6089 int r; 6090 6091 mutex_lock(&trace_types_lock); 6092 r = sprintf(buf, "%s\n", tr->current_trace->name); 6093 mutex_unlock(&trace_types_lock); 6094 6095 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); 6096 } 6097 6098 int tracer_init(struct tracer *t, struct trace_array *tr) 6099 { 6100 tracing_reset_online_cpus(&tr->array_buffer); 6101 return t->init(tr); 6102 } 6103 6104 static void set_buffer_entries(struct array_buffer *buf, unsigned long val) 6105 { 6106 int cpu; 6107 6108 for_each_tracing_cpu(cpu) 6109 per_cpu_ptr(buf->data, cpu)->entries = val; 6110 } 6111 6112 #ifdef CONFIG_TRACER_MAX_TRACE 6113 /* resize @tr's buffer to the size of @size_tr's entries */ 6114 static int resize_buffer_duplicate_size(struct array_buffer *trace_buf, 6115 struct array_buffer *size_buf, int cpu_id) 6116 { 6117 int cpu, ret = 0; 6118 6119 if (cpu_id == RING_BUFFER_ALL_CPUS) { 6120 for_each_tracing_cpu(cpu) { 6121 ret = ring_buffer_resize(trace_buf->buffer, 6122 per_cpu_ptr(size_buf->data, cpu)->entries, cpu); 6123 if (ret < 0) 6124 break; 6125 per_cpu_ptr(trace_buf->data, cpu)->entries = 6126 per_cpu_ptr(size_buf->data, cpu)->entries; 6127 } 6128 } else { 6129 ret = ring_buffer_resize(trace_buf->buffer, 6130 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id); 6131 if (ret == 0) 6132 per_cpu_ptr(trace_buf->data, cpu_id)->entries = 6133 per_cpu_ptr(size_buf->data, cpu_id)->entries; 6134 } 6135 6136 return ret; 6137 } 6138 #endif /* CONFIG_TRACER_MAX_TRACE */ 6139 6140 static int __tracing_resize_ring_buffer(struct trace_array *tr, 6141 unsigned long size, int cpu) 6142 { 6143 int ret; 6144 6145 /* 6146 * If kernel or user changes the size of the ring buffer 6147 * we use the size that was given, and we can forget about 6148 * expanding it later. 6149 */ 6150 ring_buffer_expanded = true; 6151 6152 /* May be called before buffers are initialized */ 6153 if (!tr->array_buffer.buffer) 6154 return 0; 6155 6156 ret = ring_buffer_resize(tr->array_buffer.buffer, size, cpu); 6157 if (ret < 0) 6158 return ret; 6159 6160 #ifdef CONFIG_TRACER_MAX_TRACE 6161 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) || 6162 !tr->current_trace->use_max_tr) 6163 goto out; 6164 6165 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu); 6166 if (ret < 0) { 6167 int r = resize_buffer_duplicate_size(&tr->array_buffer, 6168 &tr->array_buffer, cpu); 6169 if (r < 0) { 6170 /* 6171 * AARGH! We are left with different 6172 * size max buffer!!!! 6173 * The max buffer is our "snapshot" buffer. 6174 * When a tracer needs a snapshot (one of the 6175 * latency tracers), it swaps the max buffer 6176 * with the saved snap shot. We succeeded to 6177 * update the size of the main buffer, but failed to 6178 * update the size of the max buffer. But when we tried 6179 * to reset the main buffer to the original size, we 6180 * failed there too. This is very unlikely to 6181 * happen, but if it does, warn and kill all 6182 * tracing. 6183 */ 6184 WARN_ON(1); 6185 tracing_disabled = 1; 6186 } 6187 return ret; 6188 } 6189 6190 if (cpu == RING_BUFFER_ALL_CPUS) 6191 set_buffer_entries(&tr->max_buffer, size); 6192 else 6193 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size; 6194 6195 out: 6196 #endif /* CONFIG_TRACER_MAX_TRACE */ 6197 6198 if (cpu == RING_BUFFER_ALL_CPUS) 6199 set_buffer_entries(&tr->array_buffer, size); 6200 else 6201 per_cpu_ptr(tr->array_buffer.data, cpu)->entries = size; 6202 6203 return ret; 6204 } 6205 6206 ssize_t tracing_resize_ring_buffer(struct trace_array *tr, 6207 unsigned long size, int cpu_id) 6208 { 6209 int ret; 6210 6211 mutex_lock(&trace_types_lock); 6212 6213 if (cpu_id != RING_BUFFER_ALL_CPUS) { 6214 /* make sure, this cpu is enabled in the mask */ 6215 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) { 6216 ret = -EINVAL; 6217 goto out; 6218 } 6219 } 6220 6221 ret = __tracing_resize_ring_buffer(tr, size, cpu_id); 6222 if (ret < 0) 6223 ret = -ENOMEM; 6224 6225 out: 6226 mutex_unlock(&trace_types_lock); 6227 6228 return ret; 6229 } 6230 6231 6232 /** 6233 * tracing_update_buffers - used by tracing facility to expand ring buffers 6234 * 6235 * To save on memory when the tracing is never used on a system with it 6236 * configured in. The ring buffers are set to a minimum size. But once 6237 * a user starts to use the tracing facility, then they need to grow 6238 * to their default size. 6239 * 6240 * This function is to be called when a tracer is about to be used. 6241 */ 6242 int tracing_update_buffers(void) 6243 { 6244 int ret = 0; 6245 6246 mutex_lock(&trace_types_lock); 6247 if (!ring_buffer_expanded) 6248 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size, 6249 RING_BUFFER_ALL_CPUS); 6250 mutex_unlock(&trace_types_lock); 6251 6252 return ret; 6253 } 6254 6255 struct trace_option_dentry; 6256 6257 static void 6258 create_trace_option_files(struct trace_array *tr, struct tracer *tracer); 6259 6260 /* 6261 * Used to clear out the tracer before deletion of an instance. 6262 * Must have trace_types_lock held. 6263 */ 6264 static void tracing_set_nop(struct trace_array *tr) 6265 { 6266 if (tr->current_trace == &nop_trace) 6267 return; 6268 6269 tr->current_trace->enabled--; 6270 6271 if (tr->current_trace->reset) 6272 tr->current_trace->reset(tr); 6273 6274 tr->current_trace = &nop_trace; 6275 } 6276 6277 static void add_tracer_options(struct trace_array *tr, struct tracer *t) 6278 { 6279 /* Only enable if the directory has been created already. */ 6280 if (!tr->dir) 6281 return; 6282 6283 create_trace_option_files(tr, t); 6284 } 6285 6286 int tracing_set_tracer(struct trace_array *tr, const char *buf) 6287 { 6288 struct tracer *t; 6289 #ifdef CONFIG_TRACER_MAX_TRACE 6290 bool had_max_tr; 6291 #endif 6292 int ret = 0; 6293 6294 mutex_lock(&trace_types_lock); 6295 6296 if (!ring_buffer_expanded) { 6297 ret = __tracing_resize_ring_buffer(tr, trace_buf_size, 6298 RING_BUFFER_ALL_CPUS); 6299 if (ret < 0) 6300 goto out; 6301 ret = 0; 6302 } 6303 6304 for (t = trace_types; t; t = t->next) { 6305 if (strcmp(t->name, buf) == 0) 6306 break; 6307 } 6308 if (!t) { 6309 ret = -EINVAL; 6310 goto out; 6311 } 6312 if (t == tr->current_trace) 6313 goto out; 6314 6315 #ifdef CONFIG_TRACER_SNAPSHOT 6316 if (t->use_max_tr) { 6317 arch_spin_lock(&tr->max_lock); 6318 if (tr->cond_snapshot) 6319 ret = -EBUSY; 6320 arch_spin_unlock(&tr->max_lock); 6321 if (ret) 6322 goto out; 6323 } 6324 #endif 6325 /* Some tracers won't work on kernel command line */ 6326 if (system_state < SYSTEM_RUNNING && t->noboot) { 6327 pr_warn("Tracer '%s' is not allowed on command line, ignored\n", 6328 t->name); 6329 goto out; 6330 } 6331 6332 /* Some tracers are only allowed for the top level buffer */ 6333 if (!trace_ok_for_array(t, tr)) { 6334 ret = -EINVAL; 6335 goto out; 6336 } 6337 6338 /* If trace pipe files are being read, we can't change the tracer */ 6339 if (tr->trace_ref) { 6340 ret = -EBUSY; 6341 goto out; 6342 } 6343 6344 trace_branch_disable(); 6345 6346 tr->current_trace->enabled--; 6347 6348 if (tr->current_trace->reset) 6349 tr->current_trace->reset(tr); 6350 6351 /* Current trace needs to be nop_trace before synchronize_rcu */ 6352 tr->current_trace = &nop_trace; 6353 6354 #ifdef CONFIG_TRACER_MAX_TRACE 6355 had_max_tr = tr->allocated_snapshot; 6356 6357 if (had_max_tr && !t->use_max_tr) { 6358 /* 6359 * We need to make sure that the update_max_tr sees that 6360 * current_trace changed to nop_trace to keep it from 6361 * swapping the buffers after we resize it. 6362 * The update_max_tr is called from interrupts disabled 6363 * so a synchronized_sched() is sufficient. 6364 */ 6365 synchronize_rcu(); 6366 free_snapshot(tr); 6367 } 6368 #endif 6369 6370 #ifdef CONFIG_TRACER_MAX_TRACE 6371 if (t->use_max_tr && !had_max_tr) { 6372 ret = tracing_alloc_snapshot_instance(tr); 6373 if (ret < 0) 6374 goto out; 6375 } 6376 #endif 6377 6378 if (t->init) { 6379 ret = tracer_init(t, tr); 6380 if (ret) 6381 goto out; 6382 } 6383 6384 tr->current_trace = t; 6385 tr->current_trace->enabled++; 6386 trace_branch_enable(tr); 6387 out: 6388 mutex_unlock(&trace_types_lock); 6389 6390 return ret; 6391 } 6392 6393 static ssize_t 6394 tracing_set_trace_write(struct file *filp, const char __user *ubuf, 6395 size_t cnt, loff_t *ppos) 6396 { 6397 struct trace_array *tr = filp->private_data; 6398 char buf[MAX_TRACER_SIZE+1]; 6399 int i; 6400 size_t ret; 6401 int err; 6402 6403 ret = cnt; 6404 6405 if (cnt > MAX_TRACER_SIZE) 6406 cnt = MAX_TRACER_SIZE; 6407 6408 if (copy_from_user(buf, ubuf, cnt)) 6409 return -EFAULT; 6410 6411 buf[cnt] = 0; 6412 6413 /* strip ending whitespace. */ 6414 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--) 6415 buf[i] = 0; 6416 6417 err = tracing_set_tracer(tr, buf); 6418 if (err) 6419 return err; 6420 6421 *ppos += ret; 6422 6423 return ret; 6424 } 6425 6426 static ssize_t 6427 tracing_nsecs_read(unsigned long *ptr, char __user *ubuf, 6428 size_t cnt, loff_t *ppos) 6429 { 6430 char buf[64]; 6431 int r; 6432 6433 r = snprintf(buf, sizeof(buf), "%ld\n", 6434 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr)); 6435 if (r > sizeof(buf)) 6436 r = sizeof(buf); 6437 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); 6438 } 6439 6440 static ssize_t 6441 tracing_nsecs_write(unsigned long *ptr, const char __user *ubuf, 6442 size_t cnt, loff_t *ppos) 6443 { 6444 unsigned long val; 6445 int ret; 6446 6447 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 6448 if (ret) 6449 return ret; 6450 6451 *ptr = val * 1000; 6452 6453 return cnt; 6454 } 6455 6456 static ssize_t 6457 tracing_thresh_read(struct file *filp, char __user *ubuf, 6458 size_t cnt, loff_t *ppos) 6459 { 6460 return tracing_nsecs_read(&tracing_thresh, ubuf, cnt, ppos); 6461 } 6462 6463 static ssize_t 6464 tracing_thresh_write(struct file *filp, const char __user *ubuf, 6465 size_t cnt, loff_t *ppos) 6466 { 6467 struct trace_array *tr = filp->private_data; 6468 int ret; 6469 6470 mutex_lock(&trace_types_lock); 6471 ret = tracing_nsecs_write(&tracing_thresh, ubuf, cnt, ppos); 6472 if (ret < 0) 6473 goto out; 6474 6475 if (tr->current_trace->update_thresh) { 6476 ret = tr->current_trace->update_thresh(tr); 6477 if (ret < 0) 6478 goto out; 6479 } 6480 6481 ret = cnt; 6482 out: 6483 mutex_unlock(&trace_types_lock); 6484 6485 return ret; 6486 } 6487 6488 #if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER) 6489 6490 static ssize_t 6491 tracing_max_lat_read(struct file *filp, char __user *ubuf, 6492 size_t cnt, loff_t *ppos) 6493 { 6494 return tracing_nsecs_read(filp->private_data, ubuf, cnt, ppos); 6495 } 6496 6497 static ssize_t 6498 tracing_max_lat_write(struct file *filp, const char __user *ubuf, 6499 size_t cnt, loff_t *ppos) 6500 { 6501 return tracing_nsecs_write(filp->private_data, ubuf, cnt, ppos); 6502 } 6503 6504 #endif 6505 6506 static int tracing_open_pipe(struct inode *inode, struct file *filp) 6507 { 6508 struct trace_array *tr = inode->i_private; 6509 struct trace_iterator *iter; 6510 int ret; 6511 6512 ret = tracing_check_open_get_tr(tr); 6513 if (ret) 6514 return ret; 6515 6516 mutex_lock(&trace_types_lock); 6517 6518 /* create a buffer to store the information to pass to userspace */ 6519 iter = kzalloc(sizeof(*iter), GFP_KERNEL); 6520 if (!iter) { 6521 ret = -ENOMEM; 6522 __trace_array_put(tr); 6523 goto out; 6524 } 6525 6526 trace_seq_init(&iter->seq); 6527 iter->trace = tr->current_trace; 6528 6529 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) { 6530 ret = -ENOMEM; 6531 goto fail; 6532 } 6533 6534 /* trace pipe does not show start of buffer */ 6535 cpumask_setall(iter->started); 6536 6537 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT) 6538 iter->iter_flags |= TRACE_FILE_LAT_FMT; 6539 6540 /* Output in nanoseconds only if we are using a clock in nanoseconds. */ 6541 if (trace_clocks[tr->clock_id].in_ns) 6542 iter->iter_flags |= TRACE_FILE_TIME_IN_NS; 6543 6544 iter->tr = tr; 6545 iter->array_buffer = &tr->array_buffer; 6546 iter->cpu_file = tracing_get_cpu(inode); 6547 mutex_init(&iter->mutex); 6548 filp->private_data = iter; 6549 6550 if (iter->trace->pipe_open) 6551 iter->trace->pipe_open(iter); 6552 6553 nonseekable_open(inode, filp); 6554 6555 tr->trace_ref++; 6556 out: 6557 mutex_unlock(&trace_types_lock); 6558 return ret; 6559 6560 fail: 6561 kfree(iter); 6562 __trace_array_put(tr); 6563 mutex_unlock(&trace_types_lock); 6564 return ret; 6565 } 6566 6567 static int tracing_release_pipe(struct inode *inode, struct file *file) 6568 { 6569 struct trace_iterator *iter = file->private_data; 6570 struct trace_array *tr = inode->i_private; 6571 6572 mutex_lock(&trace_types_lock); 6573 6574 tr->trace_ref--; 6575 6576 if (iter->trace->pipe_close) 6577 iter->trace->pipe_close(iter); 6578 6579 mutex_unlock(&trace_types_lock); 6580 6581 free_cpumask_var(iter->started); 6582 mutex_destroy(&iter->mutex); 6583 kfree(iter); 6584 6585 trace_array_put(tr); 6586 6587 return 0; 6588 } 6589 6590 static __poll_t 6591 trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table) 6592 { 6593 struct trace_array *tr = iter->tr; 6594 6595 /* Iterators are static, they should be filled or empty */ 6596 if (trace_buffer_iter(iter, iter->cpu_file)) 6597 return EPOLLIN | EPOLLRDNORM; 6598 6599 if (tr->trace_flags & TRACE_ITER_BLOCK) 6600 /* 6601 * Always select as readable when in blocking mode 6602 */ 6603 return EPOLLIN | EPOLLRDNORM; 6604 else 6605 return ring_buffer_poll_wait(iter->array_buffer->buffer, iter->cpu_file, 6606 filp, poll_table); 6607 } 6608 6609 static __poll_t 6610 tracing_poll_pipe(struct file *filp, poll_table *poll_table) 6611 { 6612 struct trace_iterator *iter = filp->private_data; 6613 6614 return trace_poll(iter, filp, poll_table); 6615 } 6616 6617 /* Must be called with iter->mutex held. */ 6618 static int tracing_wait_pipe(struct file *filp) 6619 { 6620 struct trace_iterator *iter = filp->private_data; 6621 int ret; 6622 6623 while (trace_empty(iter)) { 6624 6625 if ((filp->f_flags & O_NONBLOCK)) { 6626 return -EAGAIN; 6627 } 6628 6629 /* 6630 * We block until we read something and tracing is disabled. 6631 * We still block if tracing is disabled, but we have never 6632 * read anything. This allows a user to cat this file, and 6633 * then enable tracing. But after we have read something, 6634 * we give an EOF when tracing is again disabled. 6635 * 6636 * iter->pos will be 0 if we haven't read anything. 6637 */ 6638 if (!tracer_tracing_is_on(iter->tr) && iter->pos) 6639 break; 6640 6641 mutex_unlock(&iter->mutex); 6642 6643 ret = wait_on_pipe(iter, 0); 6644 6645 mutex_lock(&iter->mutex); 6646 6647 if (ret) 6648 return ret; 6649 } 6650 6651 return 1; 6652 } 6653 6654 /* 6655 * Consumer reader. 6656 */ 6657 static ssize_t 6658 tracing_read_pipe(struct file *filp, char __user *ubuf, 6659 size_t cnt, loff_t *ppos) 6660 { 6661 struct trace_iterator *iter = filp->private_data; 6662 ssize_t sret; 6663 6664 /* 6665 * Avoid more than one consumer on a single file descriptor 6666 * This is just a matter of traces coherency, the ring buffer itself 6667 * is protected. 6668 */ 6669 mutex_lock(&iter->mutex); 6670 6671 /* return any leftover data */ 6672 sret = trace_seq_to_user(&iter->seq, ubuf, cnt); 6673 if (sret != -EBUSY) 6674 goto out; 6675 6676 trace_seq_init(&iter->seq); 6677 6678 if (iter->trace->read) { 6679 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos); 6680 if (sret) 6681 goto out; 6682 } 6683 6684 waitagain: 6685 sret = tracing_wait_pipe(filp); 6686 if (sret <= 0) 6687 goto out; 6688 6689 /* stop when tracing is finished */ 6690 if (trace_empty(iter)) { 6691 sret = 0; 6692 goto out; 6693 } 6694 6695 if (cnt >= PAGE_SIZE) 6696 cnt = PAGE_SIZE - 1; 6697 6698 /* reset all but tr, trace, and overruns */ 6699 memset(&iter->seq, 0, 6700 sizeof(struct trace_iterator) - 6701 offsetof(struct trace_iterator, seq)); 6702 cpumask_clear(iter->started); 6703 trace_seq_init(&iter->seq); 6704 iter->pos = -1; 6705 6706 trace_event_read_lock(); 6707 trace_access_lock(iter->cpu_file); 6708 while (trace_find_next_entry_inc(iter) != NULL) { 6709 enum print_line_t ret; 6710 int save_len = iter->seq.seq.len; 6711 6712 ret = print_trace_line(iter); 6713 if (ret == TRACE_TYPE_PARTIAL_LINE) { 6714 /* don't print partial lines */ 6715 iter->seq.seq.len = save_len; 6716 break; 6717 } 6718 if (ret != TRACE_TYPE_NO_CONSUME) 6719 trace_consume(iter); 6720 6721 if (trace_seq_used(&iter->seq) >= cnt) 6722 break; 6723 6724 /* 6725 * Setting the full flag means we reached the trace_seq buffer 6726 * size and we should leave by partial output condition above. 6727 * One of the trace_seq_* functions is not used properly. 6728 */ 6729 WARN_ONCE(iter->seq.full, "full flag set for trace type %d", 6730 iter->ent->type); 6731 } 6732 trace_access_unlock(iter->cpu_file); 6733 trace_event_read_unlock(); 6734 6735 /* Now copy what we have to the user */ 6736 sret = trace_seq_to_user(&iter->seq, ubuf, cnt); 6737 if (iter->seq.seq.readpos >= trace_seq_used(&iter->seq)) 6738 trace_seq_init(&iter->seq); 6739 6740 /* 6741 * If there was nothing to send to user, in spite of consuming trace 6742 * entries, go back to wait for more entries. 6743 */ 6744 if (sret == -EBUSY) 6745 goto waitagain; 6746 6747 out: 6748 mutex_unlock(&iter->mutex); 6749 6750 return sret; 6751 } 6752 6753 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd, 6754 unsigned int idx) 6755 { 6756 __free_page(spd->pages[idx]); 6757 } 6758 6759 static size_t 6760 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter) 6761 { 6762 size_t count; 6763 int save_len; 6764 int ret; 6765 6766 /* Seq buffer is page-sized, exactly what we need. */ 6767 for (;;) { 6768 save_len = iter->seq.seq.len; 6769 ret = print_trace_line(iter); 6770 6771 if (trace_seq_has_overflowed(&iter->seq)) { 6772 iter->seq.seq.len = save_len; 6773 break; 6774 } 6775 6776 /* 6777 * This should not be hit, because it should only 6778 * be set if the iter->seq overflowed. But check it 6779 * anyway to be safe. 6780 */ 6781 if (ret == TRACE_TYPE_PARTIAL_LINE) { 6782 iter->seq.seq.len = save_len; 6783 break; 6784 } 6785 6786 count = trace_seq_used(&iter->seq) - save_len; 6787 if (rem < count) { 6788 rem = 0; 6789 iter->seq.seq.len = save_len; 6790 break; 6791 } 6792 6793 if (ret != TRACE_TYPE_NO_CONSUME) 6794 trace_consume(iter); 6795 rem -= count; 6796 if (!trace_find_next_entry_inc(iter)) { 6797 rem = 0; 6798 iter->ent = NULL; 6799 break; 6800 } 6801 } 6802 6803 return rem; 6804 } 6805 6806 static ssize_t tracing_splice_read_pipe(struct file *filp, 6807 loff_t *ppos, 6808 struct pipe_inode_info *pipe, 6809 size_t len, 6810 unsigned int flags) 6811 { 6812 struct page *pages_def[PIPE_DEF_BUFFERS]; 6813 struct partial_page partial_def[PIPE_DEF_BUFFERS]; 6814 struct trace_iterator *iter = filp->private_data; 6815 struct splice_pipe_desc spd = { 6816 .pages = pages_def, 6817 .partial = partial_def, 6818 .nr_pages = 0, /* This gets updated below. */ 6819 .nr_pages_max = PIPE_DEF_BUFFERS, 6820 .ops = &default_pipe_buf_ops, 6821 .spd_release = tracing_spd_release_pipe, 6822 }; 6823 ssize_t ret; 6824 size_t rem; 6825 unsigned int i; 6826 6827 if (splice_grow_spd(pipe, &spd)) 6828 return -ENOMEM; 6829 6830 mutex_lock(&iter->mutex); 6831 6832 if (iter->trace->splice_read) { 6833 ret = iter->trace->splice_read(iter, filp, 6834 ppos, pipe, len, flags); 6835 if (ret) 6836 goto out_err; 6837 } 6838 6839 ret = tracing_wait_pipe(filp); 6840 if (ret <= 0) 6841 goto out_err; 6842 6843 if (!iter->ent && !trace_find_next_entry_inc(iter)) { 6844 ret = -EFAULT; 6845 goto out_err; 6846 } 6847 6848 trace_event_read_lock(); 6849 trace_access_lock(iter->cpu_file); 6850 6851 /* Fill as many pages as possible. */ 6852 for (i = 0, rem = len; i < spd.nr_pages_max && rem; i++) { 6853 spd.pages[i] = alloc_page(GFP_KERNEL); 6854 if (!spd.pages[i]) 6855 break; 6856 6857 rem = tracing_fill_pipe_page(rem, iter); 6858 6859 /* Copy the data into the page, so we can start over. */ 6860 ret = trace_seq_to_buffer(&iter->seq, 6861 page_address(spd.pages[i]), 6862 trace_seq_used(&iter->seq)); 6863 if (ret < 0) { 6864 __free_page(spd.pages[i]); 6865 break; 6866 } 6867 spd.partial[i].offset = 0; 6868 spd.partial[i].len = trace_seq_used(&iter->seq); 6869 6870 trace_seq_init(&iter->seq); 6871 } 6872 6873 trace_access_unlock(iter->cpu_file); 6874 trace_event_read_unlock(); 6875 mutex_unlock(&iter->mutex); 6876 6877 spd.nr_pages = i; 6878 6879 if (i) 6880 ret = splice_to_pipe(pipe, &spd); 6881 else 6882 ret = 0; 6883 out: 6884 splice_shrink_spd(&spd); 6885 return ret; 6886 6887 out_err: 6888 mutex_unlock(&iter->mutex); 6889 goto out; 6890 } 6891 6892 static ssize_t 6893 tracing_entries_read(struct file *filp, char __user *ubuf, 6894 size_t cnt, loff_t *ppos) 6895 { 6896 struct inode *inode = file_inode(filp); 6897 struct trace_array *tr = inode->i_private; 6898 int cpu = tracing_get_cpu(inode); 6899 char buf[64]; 6900 int r = 0; 6901 ssize_t ret; 6902 6903 mutex_lock(&trace_types_lock); 6904 6905 if (cpu == RING_BUFFER_ALL_CPUS) { 6906 int cpu, buf_size_same; 6907 unsigned long size; 6908 6909 size = 0; 6910 buf_size_same = 1; 6911 /* check if all cpu sizes are same */ 6912 for_each_tracing_cpu(cpu) { 6913 /* fill in the size from first enabled cpu */ 6914 if (size == 0) 6915 size = per_cpu_ptr(tr->array_buffer.data, cpu)->entries; 6916 if (size != per_cpu_ptr(tr->array_buffer.data, cpu)->entries) { 6917 buf_size_same = 0; 6918 break; 6919 } 6920 } 6921 6922 if (buf_size_same) { 6923 if (!ring_buffer_expanded) 6924 r = sprintf(buf, "%lu (expanded: %lu)\n", 6925 size >> 10, 6926 trace_buf_size >> 10); 6927 else 6928 r = sprintf(buf, "%lu\n", size >> 10); 6929 } else 6930 r = sprintf(buf, "X\n"); 6931 } else 6932 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->array_buffer.data, cpu)->entries >> 10); 6933 6934 mutex_unlock(&trace_types_lock); 6935 6936 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r); 6937 return ret; 6938 } 6939 6940 static ssize_t 6941 tracing_entries_write(struct file *filp, const char __user *ubuf, 6942 size_t cnt, loff_t *ppos) 6943 { 6944 struct inode *inode = file_inode(filp); 6945 struct trace_array *tr = inode->i_private; 6946 unsigned long val; 6947 int ret; 6948 6949 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 6950 if (ret) 6951 return ret; 6952 6953 /* must have at least 1 entry */ 6954 if (!val) 6955 return -EINVAL; 6956 6957 /* value is in KB */ 6958 val <<= 10; 6959 ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode)); 6960 if (ret < 0) 6961 return ret; 6962 6963 *ppos += cnt; 6964 6965 return cnt; 6966 } 6967 6968 static ssize_t 6969 tracing_total_entries_read(struct file *filp, char __user *ubuf, 6970 size_t cnt, loff_t *ppos) 6971 { 6972 struct trace_array *tr = filp->private_data; 6973 char buf[64]; 6974 int r, cpu; 6975 unsigned long size = 0, expanded_size = 0; 6976 6977 mutex_lock(&trace_types_lock); 6978 for_each_tracing_cpu(cpu) { 6979 size += per_cpu_ptr(tr->array_buffer.data, cpu)->entries >> 10; 6980 if (!ring_buffer_expanded) 6981 expanded_size += trace_buf_size >> 10; 6982 } 6983 if (ring_buffer_expanded) 6984 r = sprintf(buf, "%lu\n", size); 6985 else 6986 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size); 6987 mutex_unlock(&trace_types_lock); 6988 6989 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); 6990 } 6991 6992 static ssize_t 6993 tracing_free_buffer_write(struct file *filp, const char __user *ubuf, 6994 size_t cnt, loff_t *ppos) 6995 { 6996 /* 6997 * There is no need to read what the user has written, this function 6998 * is just to make sure that there is no error when "echo" is used 6999 */ 7000 7001 *ppos += cnt; 7002 7003 return cnt; 7004 } 7005 7006 static int 7007 tracing_free_buffer_release(struct inode *inode, struct file *filp) 7008 { 7009 struct trace_array *tr = inode->i_private; 7010 7011 /* disable tracing ? */ 7012 if (tr->trace_flags & TRACE_ITER_STOP_ON_FREE) 7013 tracer_tracing_off(tr); 7014 /* resize the ring buffer to 0 */ 7015 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS); 7016 7017 trace_array_put(tr); 7018 7019 return 0; 7020 } 7021 7022 static ssize_t 7023 tracing_mark_write(struct file *filp, const char __user *ubuf, 7024 size_t cnt, loff_t *fpos) 7025 { 7026 struct trace_array *tr = filp->private_data; 7027 struct ring_buffer_event *event; 7028 enum event_trigger_type tt = ETT_NONE; 7029 struct trace_buffer *buffer; 7030 struct print_entry *entry; 7031 ssize_t written; 7032 int size; 7033 int len; 7034 7035 /* Used in tracing_mark_raw_write() as well */ 7036 #define FAULTED_STR "<faulted>" 7037 #define FAULTED_SIZE (sizeof(FAULTED_STR) - 1) /* '\0' is already accounted for */ 7038 7039 if (tracing_disabled) 7040 return -EINVAL; 7041 7042 if (!(tr->trace_flags & TRACE_ITER_MARKERS)) 7043 return -EINVAL; 7044 7045 if (cnt > TRACE_BUF_SIZE) 7046 cnt = TRACE_BUF_SIZE; 7047 7048 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE); 7049 7050 size = sizeof(*entry) + cnt + 2; /* add '\0' and possible '\n' */ 7051 7052 /* If less than "<faulted>", then make sure we can still add that */ 7053 if (cnt < FAULTED_SIZE) 7054 size += FAULTED_SIZE - cnt; 7055 7056 buffer = tr->array_buffer.buffer; 7057 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size, 7058 tracing_gen_ctx()); 7059 if (unlikely(!event)) 7060 /* Ring buffer disabled, return as if not open for write */ 7061 return -EBADF; 7062 7063 entry = ring_buffer_event_data(event); 7064 entry->ip = _THIS_IP_; 7065 7066 len = __copy_from_user_inatomic(&entry->buf, ubuf, cnt); 7067 if (len) { 7068 memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE); 7069 cnt = FAULTED_SIZE; 7070 written = -EFAULT; 7071 } else 7072 written = cnt; 7073 7074 if (tr->trace_marker_file && !list_empty(&tr->trace_marker_file->triggers)) { 7075 /* do not add \n before testing triggers, but add \0 */ 7076 entry->buf[cnt] = '\0'; 7077 tt = event_triggers_call(tr->trace_marker_file, buffer, entry, event); 7078 } 7079 7080 if (entry->buf[cnt - 1] != '\n') { 7081 entry->buf[cnt] = '\n'; 7082 entry->buf[cnt + 1] = '\0'; 7083 } else 7084 entry->buf[cnt] = '\0'; 7085 7086 if (static_branch_unlikely(&trace_marker_exports_enabled)) 7087 ftrace_exports(event, TRACE_EXPORT_MARKER); 7088 __buffer_unlock_commit(buffer, event); 7089 7090 if (tt) 7091 event_triggers_post_call(tr->trace_marker_file, tt); 7092 7093 if (written > 0) 7094 *fpos += written; 7095 7096 return written; 7097 } 7098 7099 /* Limit it for now to 3K (including tag) */ 7100 #define RAW_DATA_MAX_SIZE (1024*3) 7101 7102 static ssize_t 7103 tracing_mark_raw_write(struct file *filp, const char __user *ubuf, 7104 size_t cnt, loff_t *fpos) 7105 { 7106 struct trace_array *tr = filp->private_data; 7107 struct ring_buffer_event *event; 7108 struct trace_buffer *buffer; 7109 struct raw_data_entry *entry; 7110 ssize_t written; 7111 int size; 7112 int len; 7113 7114 #define FAULT_SIZE_ID (FAULTED_SIZE + sizeof(int)) 7115 7116 if (tracing_disabled) 7117 return -EINVAL; 7118 7119 if (!(tr->trace_flags & TRACE_ITER_MARKERS)) 7120 return -EINVAL; 7121 7122 /* The marker must at least have a tag id */ 7123 if (cnt < sizeof(unsigned int) || cnt > RAW_DATA_MAX_SIZE) 7124 return -EINVAL; 7125 7126 if (cnt > TRACE_BUF_SIZE) 7127 cnt = TRACE_BUF_SIZE; 7128 7129 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE); 7130 7131 size = sizeof(*entry) + cnt; 7132 if (cnt < FAULT_SIZE_ID) 7133 size += FAULT_SIZE_ID - cnt; 7134 7135 buffer = tr->array_buffer.buffer; 7136 event = __trace_buffer_lock_reserve(buffer, TRACE_RAW_DATA, size, 7137 tracing_gen_ctx()); 7138 if (!event) 7139 /* Ring buffer disabled, return as if not open for write */ 7140 return -EBADF; 7141 7142 entry = ring_buffer_event_data(event); 7143 7144 len = __copy_from_user_inatomic(&entry->id, ubuf, cnt); 7145 if (len) { 7146 entry->id = -1; 7147 memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE); 7148 written = -EFAULT; 7149 } else 7150 written = cnt; 7151 7152 __buffer_unlock_commit(buffer, event); 7153 7154 if (written > 0) 7155 *fpos += written; 7156 7157 return written; 7158 } 7159 7160 static int tracing_clock_show(struct seq_file *m, void *v) 7161 { 7162 struct trace_array *tr = m->private; 7163 int i; 7164 7165 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) 7166 seq_printf(m, 7167 "%s%s%s%s", i ? " " : "", 7168 i == tr->clock_id ? "[" : "", trace_clocks[i].name, 7169 i == tr->clock_id ? "]" : ""); 7170 seq_putc(m, '\n'); 7171 7172 return 0; 7173 } 7174 7175 int tracing_set_clock(struct trace_array *tr, const char *clockstr) 7176 { 7177 int i; 7178 7179 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) { 7180 if (strcmp(trace_clocks[i].name, clockstr) == 0) 7181 break; 7182 } 7183 if (i == ARRAY_SIZE(trace_clocks)) 7184 return -EINVAL; 7185 7186 mutex_lock(&trace_types_lock); 7187 7188 tr->clock_id = i; 7189 7190 ring_buffer_set_clock(tr->array_buffer.buffer, trace_clocks[i].func); 7191 7192 /* 7193 * New clock may not be consistent with the previous clock. 7194 * Reset the buffer so that it doesn't have incomparable timestamps. 7195 */ 7196 tracing_reset_online_cpus(&tr->array_buffer); 7197 7198 #ifdef CONFIG_TRACER_MAX_TRACE 7199 if (tr->max_buffer.buffer) 7200 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func); 7201 tracing_reset_online_cpus(&tr->max_buffer); 7202 #endif 7203 7204 mutex_unlock(&trace_types_lock); 7205 7206 return 0; 7207 } 7208 7209 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf, 7210 size_t cnt, loff_t *fpos) 7211 { 7212 struct seq_file *m = filp->private_data; 7213 struct trace_array *tr = m->private; 7214 char buf[64]; 7215 const char *clockstr; 7216 int ret; 7217 7218 if (cnt >= sizeof(buf)) 7219 return -EINVAL; 7220 7221 if (copy_from_user(buf, ubuf, cnt)) 7222 return -EFAULT; 7223 7224 buf[cnt] = 0; 7225 7226 clockstr = strstrip(buf); 7227 7228 ret = tracing_set_clock(tr, clockstr); 7229 if (ret) 7230 return ret; 7231 7232 *fpos += cnt; 7233 7234 return cnt; 7235 } 7236 7237 static int tracing_clock_open(struct inode *inode, struct file *file) 7238 { 7239 struct trace_array *tr = inode->i_private; 7240 int ret; 7241 7242 ret = tracing_check_open_get_tr(tr); 7243 if (ret) 7244 return ret; 7245 7246 ret = single_open(file, tracing_clock_show, inode->i_private); 7247 if (ret < 0) 7248 trace_array_put(tr); 7249 7250 return ret; 7251 } 7252 7253 static int tracing_time_stamp_mode_show(struct seq_file *m, void *v) 7254 { 7255 struct trace_array *tr = m->private; 7256 7257 mutex_lock(&trace_types_lock); 7258 7259 if (ring_buffer_time_stamp_abs(tr->array_buffer.buffer)) 7260 seq_puts(m, "delta [absolute]\n"); 7261 else 7262 seq_puts(m, "[delta] absolute\n"); 7263 7264 mutex_unlock(&trace_types_lock); 7265 7266 return 0; 7267 } 7268 7269 static int tracing_time_stamp_mode_open(struct inode *inode, struct file *file) 7270 { 7271 struct trace_array *tr = inode->i_private; 7272 int ret; 7273 7274 ret = tracing_check_open_get_tr(tr); 7275 if (ret) 7276 return ret; 7277 7278 ret = single_open(file, tracing_time_stamp_mode_show, inode->i_private); 7279 if (ret < 0) 7280 trace_array_put(tr); 7281 7282 return ret; 7283 } 7284 7285 u64 tracing_event_time_stamp(struct trace_buffer *buffer, struct ring_buffer_event *rbe) 7286 { 7287 if (rbe == this_cpu_read(trace_buffered_event)) 7288 return ring_buffer_time_stamp(buffer); 7289 7290 return ring_buffer_event_time_stamp(buffer, rbe); 7291 } 7292 7293 /* 7294 * Set or disable using the per CPU trace_buffer_event when possible. 7295 */ 7296 int tracing_set_filter_buffering(struct trace_array *tr, bool set) 7297 { 7298 int ret = 0; 7299 7300 mutex_lock(&trace_types_lock); 7301 7302 if (set && tr->no_filter_buffering_ref++) 7303 goto out; 7304 7305 if (!set) { 7306 if (WARN_ON_ONCE(!tr->no_filter_buffering_ref)) { 7307 ret = -EINVAL; 7308 goto out; 7309 } 7310 7311 --tr->no_filter_buffering_ref; 7312 } 7313 out: 7314 mutex_unlock(&trace_types_lock); 7315 7316 return ret; 7317 } 7318 7319 struct ftrace_buffer_info { 7320 struct trace_iterator iter; 7321 void *spare; 7322 unsigned int spare_cpu; 7323 unsigned int read; 7324 }; 7325 7326 #ifdef CONFIG_TRACER_SNAPSHOT 7327 static int tracing_snapshot_open(struct inode *inode, struct file *file) 7328 { 7329 struct trace_array *tr = inode->i_private; 7330 struct trace_iterator *iter; 7331 struct seq_file *m; 7332 int ret; 7333 7334 ret = tracing_check_open_get_tr(tr); 7335 if (ret) 7336 return ret; 7337 7338 if (file->f_mode & FMODE_READ) { 7339 iter = __tracing_open(inode, file, true); 7340 if (IS_ERR(iter)) 7341 ret = PTR_ERR(iter); 7342 } else { 7343 /* Writes still need the seq_file to hold the private data */ 7344 ret = -ENOMEM; 7345 m = kzalloc(sizeof(*m), GFP_KERNEL); 7346 if (!m) 7347 goto out; 7348 iter = kzalloc(sizeof(*iter), GFP_KERNEL); 7349 if (!iter) { 7350 kfree(m); 7351 goto out; 7352 } 7353 ret = 0; 7354 7355 iter->tr = tr; 7356 iter->array_buffer = &tr->max_buffer; 7357 iter->cpu_file = tracing_get_cpu(inode); 7358 m->private = iter; 7359 file->private_data = m; 7360 } 7361 out: 7362 if (ret < 0) 7363 trace_array_put(tr); 7364 7365 return ret; 7366 } 7367 7368 static ssize_t 7369 tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt, 7370 loff_t *ppos) 7371 { 7372 struct seq_file *m = filp->private_data; 7373 struct trace_iterator *iter = m->private; 7374 struct trace_array *tr = iter->tr; 7375 unsigned long val; 7376 int ret; 7377 7378 ret = tracing_update_buffers(); 7379 if (ret < 0) 7380 return ret; 7381 7382 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 7383 if (ret) 7384 return ret; 7385 7386 mutex_lock(&trace_types_lock); 7387 7388 if (tr->current_trace->use_max_tr) { 7389 ret = -EBUSY; 7390 goto out; 7391 } 7392 7393 arch_spin_lock(&tr->max_lock); 7394 if (tr->cond_snapshot) 7395 ret = -EBUSY; 7396 arch_spin_unlock(&tr->max_lock); 7397 if (ret) 7398 goto out; 7399 7400 switch (val) { 7401 case 0: 7402 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) { 7403 ret = -EINVAL; 7404 break; 7405 } 7406 if (tr->allocated_snapshot) 7407 free_snapshot(tr); 7408 break; 7409 case 1: 7410 /* Only allow per-cpu swap if the ring buffer supports it */ 7411 #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP 7412 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) { 7413 ret = -EINVAL; 7414 break; 7415 } 7416 #endif 7417 if (tr->allocated_snapshot) 7418 ret = resize_buffer_duplicate_size(&tr->max_buffer, 7419 &tr->array_buffer, iter->cpu_file); 7420 else 7421 ret = tracing_alloc_snapshot_instance(tr); 7422 if (ret < 0) 7423 break; 7424 local_irq_disable(); 7425 /* Now, we're going to swap */ 7426 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) 7427 update_max_tr(tr, current, smp_processor_id(), NULL); 7428 else 7429 update_max_tr_single(tr, current, iter->cpu_file); 7430 local_irq_enable(); 7431 break; 7432 default: 7433 if (tr->allocated_snapshot) { 7434 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) 7435 tracing_reset_online_cpus(&tr->max_buffer); 7436 else 7437 tracing_reset_cpu(&tr->max_buffer, iter->cpu_file); 7438 } 7439 break; 7440 } 7441 7442 if (ret >= 0) { 7443 *ppos += cnt; 7444 ret = cnt; 7445 } 7446 out: 7447 mutex_unlock(&trace_types_lock); 7448 return ret; 7449 } 7450 7451 static int tracing_snapshot_release(struct inode *inode, struct file *file) 7452 { 7453 struct seq_file *m = file->private_data; 7454 int ret; 7455 7456 ret = tracing_release(inode, file); 7457 7458 if (file->f_mode & FMODE_READ) 7459 return ret; 7460 7461 /* If write only, the seq_file is just a stub */ 7462 if (m) 7463 kfree(m->private); 7464 kfree(m); 7465 7466 return 0; 7467 } 7468 7469 static int tracing_buffers_open(struct inode *inode, struct file *filp); 7470 static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf, 7471 size_t count, loff_t *ppos); 7472 static int tracing_buffers_release(struct inode *inode, struct file *file); 7473 static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos, 7474 struct pipe_inode_info *pipe, size_t len, unsigned int flags); 7475 7476 static int snapshot_raw_open(struct inode *inode, struct file *filp) 7477 { 7478 struct ftrace_buffer_info *info; 7479 int ret; 7480 7481 /* The following checks for tracefs lockdown */ 7482 ret = tracing_buffers_open(inode, filp); 7483 if (ret < 0) 7484 return ret; 7485 7486 info = filp->private_data; 7487 7488 if (info->iter.trace->use_max_tr) { 7489 tracing_buffers_release(inode, filp); 7490 return -EBUSY; 7491 } 7492 7493 info->iter.snapshot = true; 7494 info->iter.array_buffer = &info->iter.tr->max_buffer; 7495 7496 return ret; 7497 } 7498 7499 #endif /* CONFIG_TRACER_SNAPSHOT */ 7500 7501 7502 static const struct file_operations tracing_thresh_fops = { 7503 .open = tracing_open_generic, 7504 .read = tracing_thresh_read, 7505 .write = tracing_thresh_write, 7506 .llseek = generic_file_llseek, 7507 }; 7508 7509 #if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER) 7510 static const struct file_operations tracing_max_lat_fops = { 7511 .open = tracing_open_generic, 7512 .read = tracing_max_lat_read, 7513 .write = tracing_max_lat_write, 7514 .llseek = generic_file_llseek, 7515 }; 7516 #endif 7517 7518 static const struct file_operations set_tracer_fops = { 7519 .open = tracing_open_generic, 7520 .read = tracing_set_trace_read, 7521 .write = tracing_set_trace_write, 7522 .llseek = generic_file_llseek, 7523 }; 7524 7525 static const struct file_operations tracing_pipe_fops = { 7526 .open = tracing_open_pipe, 7527 .poll = tracing_poll_pipe, 7528 .read = tracing_read_pipe, 7529 .splice_read = tracing_splice_read_pipe, 7530 .release = tracing_release_pipe, 7531 .llseek = no_llseek, 7532 }; 7533 7534 static const struct file_operations tracing_entries_fops = { 7535 .open = tracing_open_generic_tr, 7536 .read = tracing_entries_read, 7537 .write = tracing_entries_write, 7538 .llseek = generic_file_llseek, 7539 .release = tracing_release_generic_tr, 7540 }; 7541 7542 static const struct file_operations tracing_total_entries_fops = { 7543 .open = tracing_open_generic_tr, 7544 .read = tracing_total_entries_read, 7545 .llseek = generic_file_llseek, 7546 .release = tracing_release_generic_tr, 7547 }; 7548 7549 static const struct file_operations tracing_free_buffer_fops = { 7550 .open = tracing_open_generic_tr, 7551 .write = tracing_free_buffer_write, 7552 .release = tracing_free_buffer_release, 7553 }; 7554 7555 static const struct file_operations tracing_mark_fops = { 7556 .open = tracing_open_generic_tr, 7557 .write = tracing_mark_write, 7558 .llseek = generic_file_llseek, 7559 .release = tracing_release_generic_tr, 7560 }; 7561 7562 static const struct file_operations tracing_mark_raw_fops = { 7563 .open = tracing_open_generic_tr, 7564 .write = tracing_mark_raw_write, 7565 .llseek = generic_file_llseek, 7566 .release = tracing_release_generic_tr, 7567 }; 7568 7569 static const struct file_operations trace_clock_fops = { 7570 .open = tracing_clock_open, 7571 .read = seq_read, 7572 .llseek = seq_lseek, 7573 .release = tracing_single_release_tr, 7574 .write = tracing_clock_write, 7575 }; 7576 7577 static const struct file_operations trace_time_stamp_mode_fops = { 7578 .open = tracing_time_stamp_mode_open, 7579 .read = seq_read, 7580 .llseek = seq_lseek, 7581 .release = tracing_single_release_tr, 7582 }; 7583 7584 #ifdef CONFIG_TRACER_SNAPSHOT 7585 static const struct file_operations snapshot_fops = { 7586 .open = tracing_snapshot_open, 7587 .read = seq_read, 7588 .write = tracing_snapshot_write, 7589 .llseek = tracing_lseek, 7590 .release = tracing_snapshot_release, 7591 }; 7592 7593 static const struct file_operations snapshot_raw_fops = { 7594 .open = snapshot_raw_open, 7595 .read = tracing_buffers_read, 7596 .release = tracing_buffers_release, 7597 .splice_read = tracing_buffers_splice_read, 7598 .llseek = no_llseek, 7599 }; 7600 7601 #endif /* CONFIG_TRACER_SNAPSHOT */ 7602 7603 /* 7604 * trace_min_max_write - Write a u64 value to a trace_min_max_param struct 7605 * @filp: The active open file structure 7606 * @ubuf: The userspace provided buffer to read value into 7607 * @cnt: The maximum number of bytes to read 7608 * @ppos: The current "file" position 7609 * 7610 * This function implements the write interface for a struct trace_min_max_param. 7611 * The filp->private_data must point to a trace_min_max_param structure that 7612 * defines where to write the value, the min and the max acceptable values, 7613 * and a lock to protect the write. 7614 */ 7615 static ssize_t 7616 trace_min_max_write(struct file *filp, const char __user *ubuf, size_t cnt, loff_t *ppos) 7617 { 7618 struct trace_min_max_param *param = filp->private_data; 7619 u64 val; 7620 int err; 7621 7622 if (!param) 7623 return -EFAULT; 7624 7625 err = kstrtoull_from_user(ubuf, cnt, 10, &val); 7626 if (err) 7627 return err; 7628 7629 if (param->lock) 7630 mutex_lock(param->lock); 7631 7632 if (param->min && val < *param->min) 7633 err = -EINVAL; 7634 7635 if (param->max && val > *param->max) 7636 err = -EINVAL; 7637 7638 if (!err) 7639 *param->val = val; 7640 7641 if (param->lock) 7642 mutex_unlock(param->lock); 7643 7644 if (err) 7645 return err; 7646 7647 return cnt; 7648 } 7649 7650 /* 7651 * trace_min_max_read - Read a u64 value from a trace_min_max_param struct 7652 * @filp: The active open file structure 7653 * @ubuf: The userspace provided buffer to read value into 7654 * @cnt: The maximum number of bytes to read 7655 * @ppos: The current "file" position 7656 * 7657 * This function implements the read interface for a struct trace_min_max_param. 7658 * The filp->private_data must point to a trace_min_max_param struct with valid 7659 * data. 7660 */ 7661 static ssize_t 7662 trace_min_max_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos) 7663 { 7664 struct trace_min_max_param *param = filp->private_data; 7665 char buf[U64_STR_SIZE]; 7666 int len; 7667 u64 val; 7668 7669 if (!param) 7670 return -EFAULT; 7671 7672 val = *param->val; 7673 7674 if (cnt > sizeof(buf)) 7675 cnt = sizeof(buf); 7676 7677 len = snprintf(buf, sizeof(buf), "%llu\n", val); 7678 7679 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len); 7680 } 7681 7682 const struct file_operations trace_min_max_fops = { 7683 .open = tracing_open_generic, 7684 .read = trace_min_max_read, 7685 .write = trace_min_max_write, 7686 }; 7687 7688 #define TRACING_LOG_ERRS_MAX 8 7689 #define TRACING_LOG_LOC_MAX 128 7690 7691 #define CMD_PREFIX " Command: " 7692 7693 struct err_info { 7694 const char **errs; /* ptr to loc-specific array of err strings */ 7695 u8 type; /* index into errs -> specific err string */ 7696 u8 pos; /* MAX_FILTER_STR_VAL = 256 */ 7697 u64 ts; 7698 }; 7699 7700 struct tracing_log_err { 7701 struct list_head list; 7702 struct err_info info; 7703 char loc[TRACING_LOG_LOC_MAX]; /* err location */ 7704 char cmd[MAX_FILTER_STR_VAL]; /* what caused err */ 7705 }; 7706 7707 static DEFINE_MUTEX(tracing_err_log_lock); 7708 7709 static struct tracing_log_err *get_tracing_log_err(struct trace_array *tr) 7710 { 7711 struct tracing_log_err *err; 7712 7713 if (tr->n_err_log_entries < TRACING_LOG_ERRS_MAX) { 7714 err = kzalloc(sizeof(*err), GFP_KERNEL); 7715 if (!err) 7716 err = ERR_PTR(-ENOMEM); 7717 tr->n_err_log_entries++; 7718 7719 return err; 7720 } 7721 7722 err = list_first_entry(&tr->err_log, struct tracing_log_err, list); 7723 list_del(&err->list); 7724 7725 return err; 7726 } 7727 7728 /** 7729 * err_pos - find the position of a string within a command for error careting 7730 * @cmd: The tracing command that caused the error 7731 * @str: The string to position the caret at within @cmd 7732 * 7733 * Finds the position of the first occurrence of @str within @cmd. The 7734 * return value can be passed to tracing_log_err() for caret placement 7735 * within @cmd. 7736 * 7737 * Returns the index within @cmd of the first occurrence of @str or 0 7738 * if @str was not found. 7739 */ 7740 unsigned int err_pos(char *cmd, const char *str) 7741 { 7742 char *found; 7743 7744 if (WARN_ON(!strlen(cmd))) 7745 return 0; 7746 7747 found = strstr(cmd, str); 7748 if (found) 7749 return found - cmd; 7750 7751 return 0; 7752 } 7753 7754 /** 7755 * tracing_log_err - write an error to the tracing error log 7756 * @tr: The associated trace array for the error (NULL for top level array) 7757 * @loc: A string describing where the error occurred 7758 * @cmd: The tracing command that caused the error 7759 * @errs: The array of loc-specific static error strings 7760 * @type: The index into errs[], which produces the specific static err string 7761 * @pos: The position the caret should be placed in the cmd 7762 * 7763 * Writes an error into tracing/error_log of the form: 7764 * 7765 * <loc>: error: <text> 7766 * Command: <cmd> 7767 * ^ 7768 * 7769 * tracing/error_log is a small log file containing the last 7770 * TRACING_LOG_ERRS_MAX errors (8). Memory for errors isn't allocated 7771 * unless there has been a tracing error, and the error log can be 7772 * cleared and have its memory freed by writing the empty string in 7773 * truncation mode to it i.e. echo > tracing/error_log. 7774 * 7775 * NOTE: the @errs array along with the @type param are used to 7776 * produce a static error string - this string is not copied and saved 7777 * when the error is logged - only a pointer to it is saved. See 7778 * existing callers for examples of how static strings are typically 7779 * defined for use with tracing_log_err(). 7780 */ 7781 void tracing_log_err(struct trace_array *tr, 7782 const char *loc, const char *cmd, 7783 const char **errs, u8 type, u8 pos) 7784 { 7785 struct tracing_log_err *err; 7786 7787 if (!tr) 7788 tr = &global_trace; 7789 7790 mutex_lock(&tracing_err_log_lock); 7791 err = get_tracing_log_err(tr); 7792 if (PTR_ERR(err) == -ENOMEM) { 7793 mutex_unlock(&tracing_err_log_lock); 7794 return; 7795 } 7796 7797 snprintf(err->loc, TRACING_LOG_LOC_MAX, "%s: error: ", loc); 7798 snprintf(err->cmd, MAX_FILTER_STR_VAL,"\n" CMD_PREFIX "%s\n", cmd); 7799 7800 err->info.errs = errs; 7801 err->info.type = type; 7802 err->info.pos = pos; 7803 err->info.ts = local_clock(); 7804 7805 list_add_tail(&err->list, &tr->err_log); 7806 mutex_unlock(&tracing_err_log_lock); 7807 } 7808 7809 static void clear_tracing_err_log(struct trace_array *tr) 7810 { 7811 struct tracing_log_err *err, *next; 7812 7813 mutex_lock(&tracing_err_log_lock); 7814 list_for_each_entry_safe(err, next, &tr->err_log, list) { 7815 list_del(&err->list); 7816 kfree(err); 7817 } 7818 7819 tr->n_err_log_entries = 0; 7820 mutex_unlock(&tracing_err_log_lock); 7821 } 7822 7823 static void *tracing_err_log_seq_start(struct seq_file *m, loff_t *pos) 7824 { 7825 struct trace_array *tr = m->private; 7826 7827 mutex_lock(&tracing_err_log_lock); 7828 7829 return seq_list_start(&tr->err_log, *pos); 7830 } 7831 7832 static void *tracing_err_log_seq_next(struct seq_file *m, void *v, loff_t *pos) 7833 { 7834 struct trace_array *tr = m->private; 7835 7836 return seq_list_next(v, &tr->err_log, pos); 7837 } 7838 7839 static void tracing_err_log_seq_stop(struct seq_file *m, void *v) 7840 { 7841 mutex_unlock(&tracing_err_log_lock); 7842 } 7843 7844 static void tracing_err_log_show_pos(struct seq_file *m, u8 pos) 7845 { 7846 u8 i; 7847 7848 for (i = 0; i < sizeof(CMD_PREFIX) - 1; i++) 7849 seq_putc(m, ' '); 7850 for (i = 0; i < pos; i++) 7851 seq_putc(m, ' '); 7852 seq_puts(m, "^\n"); 7853 } 7854 7855 static int tracing_err_log_seq_show(struct seq_file *m, void *v) 7856 { 7857 struct tracing_log_err *err = v; 7858 7859 if (err) { 7860 const char *err_text = err->info.errs[err->info.type]; 7861 u64 sec = err->info.ts; 7862 u32 nsec; 7863 7864 nsec = do_div(sec, NSEC_PER_SEC); 7865 seq_printf(m, "[%5llu.%06u] %s%s", sec, nsec / 1000, 7866 err->loc, err_text); 7867 seq_printf(m, "%s", err->cmd); 7868 tracing_err_log_show_pos(m, err->info.pos); 7869 } 7870 7871 return 0; 7872 } 7873 7874 static const struct seq_operations tracing_err_log_seq_ops = { 7875 .start = tracing_err_log_seq_start, 7876 .next = tracing_err_log_seq_next, 7877 .stop = tracing_err_log_seq_stop, 7878 .show = tracing_err_log_seq_show 7879 }; 7880 7881 static int tracing_err_log_open(struct inode *inode, struct file *file) 7882 { 7883 struct trace_array *tr = inode->i_private; 7884 int ret = 0; 7885 7886 ret = tracing_check_open_get_tr(tr); 7887 if (ret) 7888 return ret; 7889 7890 /* If this file was opened for write, then erase contents */ 7891 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) 7892 clear_tracing_err_log(tr); 7893 7894 if (file->f_mode & FMODE_READ) { 7895 ret = seq_open(file, &tracing_err_log_seq_ops); 7896 if (!ret) { 7897 struct seq_file *m = file->private_data; 7898 m->private = tr; 7899 } else { 7900 trace_array_put(tr); 7901 } 7902 } 7903 return ret; 7904 } 7905 7906 static ssize_t tracing_err_log_write(struct file *file, 7907 const char __user *buffer, 7908 size_t count, loff_t *ppos) 7909 { 7910 return count; 7911 } 7912 7913 static int tracing_err_log_release(struct inode *inode, struct file *file) 7914 { 7915 struct trace_array *tr = inode->i_private; 7916 7917 trace_array_put(tr); 7918 7919 if (file->f_mode & FMODE_READ) 7920 seq_release(inode, file); 7921 7922 return 0; 7923 } 7924 7925 static const struct file_operations tracing_err_log_fops = { 7926 .open = tracing_err_log_open, 7927 .write = tracing_err_log_write, 7928 .read = seq_read, 7929 .llseek = seq_lseek, 7930 .release = tracing_err_log_release, 7931 }; 7932 7933 static int tracing_buffers_open(struct inode *inode, struct file *filp) 7934 { 7935 struct trace_array *tr = inode->i_private; 7936 struct ftrace_buffer_info *info; 7937 int ret; 7938 7939 ret = tracing_check_open_get_tr(tr); 7940 if (ret) 7941 return ret; 7942 7943 info = kvzalloc(sizeof(*info), GFP_KERNEL); 7944 if (!info) { 7945 trace_array_put(tr); 7946 return -ENOMEM; 7947 } 7948 7949 mutex_lock(&trace_types_lock); 7950 7951 info->iter.tr = tr; 7952 info->iter.cpu_file = tracing_get_cpu(inode); 7953 info->iter.trace = tr->current_trace; 7954 info->iter.array_buffer = &tr->array_buffer; 7955 info->spare = NULL; 7956 /* Force reading ring buffer for first read */ 7957 info->read = (unsigned int)-1; 7958 7959 filp->private_data = info; 7960 7961 tr->trace_ref++; 7962 7963 mutex_unlock(&trace_types_lock); 7964 7965 ret = nonseekable_open(inode, filp); 7966 if (ret < 0) 7967 trace_array_put(tr); 7968 7969 return ret; 7970 } 7971 7972 static __poll_t 7973 tracing_buffers_poll(struct file *filp, poll_table *poll_table) 7974 { 7975 struct ftrace_buffer_info *info = filp->private_data; 7976 struct trace_iterator *iter = &info->iter; 7977 7978 return trace_poll(iter, filp, poll_table); 7979 } 7980 7981 static ssize_t 7982 tracing_buffers_read(struct file *filp, char __user *ubuf, 7983 size_t count, loff_t *ppos) 7984 { 7985 struct ftrace_buffer_info *info = filp->private_data; 7986 struct trace_iterator *iter = &info->iter; 7987 ssize_t ret = 0; 7988 ssize_t size; 7989 7990 if (!count) 7991 return 0; 7992 7993 #ifdef CONFIG_TRACER_MAX_TRACE 7994 if (iter->snapshot && iter->tr->current_trace->use_max_tr) 7995 return -EBUSY; 7996 #endif 7997 7998 if (!info->spare) { 7999 info->spare = ring_buffer_alloc_read_page(iter->array_buffer->buffer, 8000 iter->cpu_file); 8001 if (IS_ERR(info->spare)) { 8002 ret = PTR_ERR(info->spare); 8003 info->spare = NULL; 8004 } else { 8005 info->spare_cpu = iter->cpu_file; 8006 } 8007 } 8008 if (!info->spare) 8009 return ret; 8010 8011 /* Do we have previous read data to read? */ 8012 if (info->read < PAGE_SIZE) 8013 goto read; 8014 8015 again: 8016 trace_access_lock(iter->cpu_file); 8017 ret = ring_buffer_read_page(iter->array_buffer->buffer, 8018 &info->spare, 8019 count, 8020 iter->cpu_file, 0); 8021 trace_access_unlock(iter->cpu_file); 8022 8023 if (ret < 0) { 8024 if (trace_empty(iter)) { 8025 if ((filp->f_flags & O_NONBLOCK)) 8026 return -EAGAIN; 8027 8028 ret = wait_on_pipe(iter, 0); 8029 if (ret) 8030 return ret; 8031 8032 goto again; 8033 } 8034 return 0; 8035 } 8036 8037 info->read = 0; 8038 read: 8039 size = PAGE_SIZE - info->read; 8040 if (size > count) 8041 size = count; 8042 8043 ret = copy_to_user(ubuf, info->spare + info->read, size); 8044 if (ret == size) 8045 return -EFAULT; 8046 8047 size -= ret; 8048 8049 *ppos += size; 8050 info->read += size; 8051 8052 return size; 8053 } 8054 8055 static int tracing_buffers_release(struct inode *inode, struct file *file) 8056 { 8057 struct ftrace_buffer_info *info = file->private_data; 8058 struct trace_iterator *iter = &info->iter; 8059 8060 mutex_lock(&trace_types_lock); 8061 8062 iter->tr->trace_ref--; 8063 8064 __trace_array_put(iter->tr); 8065 8066 if (info->spare) 8067 ring_buffer_free_read_page(iter->array_buffer->buffer, 8068 info->spare_cpu, info->spare); 8069 kvfree(info); 8070 8071 mutex_unlock(&trace_types_lock); 8072 8073 return 0; 8074 } 8075 8076 struct buffer_ref { 8077 struct trace_buffer *buffer; 8078 void *page; 8079 int cpu; 8080 refcount_t refcount; 8081 }; 8082 8083 static void buffer_ref_release(struct buffer_ref *ref) 8084 { 8085 if (!refcount_dec_and_test(&ref->refcount)) 8086 return; 8087 ring_buffer_free_read_page(ref->buffer, ref->cpu, ref->page); 8088 kfree(ref); 8089 } 8090 8091 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe, 8092 struct pipe_buffer *buf) 8093 { 8094 struct buffer_ref *ref = (struct buffer_ref *)buf->private; 8095 8096 buffer_ref_release(ref); 8097 buf->private = 0; 8098 } 8099 8100 static bool buffer_pipe_buf_get(struct pipe_inode_info *pipe, 8101 struct pipe_buffer *buf) 8102 { 8103 struct buffer_ref *ref = (struct buffer_ref *)buf->private; 8104 8105 if (refcount_read(&ref->refcount) > INT_MAX/2) 8106 return false; 8107 8108 refcount_inc(&ref->refcount); 8109 return true; 8110 } 8111 8112 /* Pipe buffer operations for a buffer. */ 8113 static const struct pipe_buf_operations buffer_pipe_buf_ops = { 8114 .release = buffer_pipe_buf_release, 8115 .get = buffer_pipe_buf_get, 8116 }; 8117 8118 /* 8119 * Callback from splice_to_pipe(), if we need to release some pages 8120 * at the end of the spd in case we error'ed out in filling the pipe. 8121 */ 8122 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i) 8123 { 8124 struct buffer_ref *ref = 8125 (struct buffer_ref *)spd->partial[i].private; 8126 8127 buffer_ref_release(ref); 8128 spd->partial[i].private = 0; 8129 } 8130 8131 static ssize_t 8132 tracing_buffers_splice_read(struct file *file, loff_t *ppos, 8133 struct pipe_inode_info *pipe, size_t len, 8134 unsigned int flags) 8135 { 8136 struct ftrace_buffer_info *info = file->private_data; 8137 struct trace_iterator *iter = &info->iter; 8138 struct partial_page partial_def[PIPE_DEF_BUFFERS]; 8139 struct page *pages_def[PIPE_DEF_BUFFERS]; 8140 struct splice_pipe_desc spd = { 8141 .pages = pages_def, 8142 .partial = partial_def, 8143 .nr_pages_max = PIPE_DEF_BUFFERS, 8144 .ops = &buffer_pipe_buf_ops, 8145 .spd_release = buffer_spd_release, 8146 }; 8147 struct buffer_ref *ref; 8148 int entries, i; 8149 ssize_t ret = 0; 8150 8151 #ifdef CONFIG_TRACER_MAX_TRACE 8152 if (iter->snapshot && iter->tr->current_trace->use_max_tr) 8153 return -EBUSY; 8154 #endif 8155 8156 if (*ppos & (PAGE_SIZE - 1)) 8157 return -EINVAL; 8158 8159 if (len & (PAGE_SIZE - 1)) { 8160 if (len < PAGE_SIZE) 8161 return -EINVAL; 8162 len &= PAGE_MASK; 8163 } 8164 8165 if (splice_grow_spd(pipe, &spd)) 8166 return -ENOMEM; 8167 8168 again: 8169 trace_access_lock(iter->cpu_file); 8170 entries = ring_buffer_entries_cpu(iter->array_buffer->buffer, iter->cpu_file); 8171 8172 for (i = 0; i < spd.nr_pages_max && len && entries; i++, len -= PAGE_SIZE) { 8173 struct page *page; 8174 int r; 8175 8176 ref = kzalloc(sizeof(*ref), GFP_KERNEL); 8177 if (!ref) { 8178 ret = -ENOMEM; 8179 break; 8180 } 8181 8182 refcount_set(&ref->refcount, 1); 8183 ref->buffer = iter->array_buffer->buffer; 8184 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file); 8185 if (IS_ERR(ref->page)) { 8186 ret = PTR_ERR(ref->page); 8187 ref->page = NULL; 8188 kfree(ref); 8189 break; 8190 } 8191 ref->cpu = iter->cpu_file; 8192 8193 r = ring_buffer_read_page(ref->buffer, &ref->page, 8194 len, iter->cpu_file, 1); 8195 if (r < 0) { 8196 ring_buffer_free_read_page(ref->buffer, ref->cpu, 8197 ref->page); 8198 kfree(ref); 8199 break; 8200 } 8201 8202 page = virt_to_page(ref->page); 8203 8204 spd.pages[i] = page; 8205 spd.partial[i].len = PAGE_SIZE; 8206 spd.partial[i].offset = 0; 8207 spd.partial[i].private = (unsigned long)ref; 8208 spd.nr_pages++; 8209 *ppos += PAGE_SIZE; 8210 8211 entries = ring_buffer_entries_cpu(iter->array_buffer->buffer, iter->cpu_file); 8212 } 8213 8214 trace_access_unlock(iter->cpu_file); 8215 spd.nr_pages = i; 8216 8217 /* did we read anything? */ 8218 if (!spd.nr_pages) { 8219 if (ret) 8220 goto out; 8221 8222 ret = -EAGAIN; 8223 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) 8224 goto out; 8225 8226 ret = wait_on_pipe(iter, iter->tr->buffer_percent); 8227 if (ret) 8228 goto out; 8229 8230 goto again; 8231 } 8232 8233 ret = splice_to_pipe(pipe, &spd); 8234 out: 8235 splice_shrink_spd(&spd); 8236 8237 return ret; 8238 } 8239 8240 static const struct file_operations tracing_buffers_fops = { 8241 .open = tracing_buffers_open, 8242 .read = tracing_buffers_read, 8243 .poll = tracing_buffers_poll, 8244 .release = tracing_buffers_release, 8245 .splice_read = tracing_buffers_splice_read, 8246 .llseek = no_llseek, 8247 }; 8248 8249 static ssize_t 8250 tracing_stats_read(struct file *filp, char __user *ubuf, 8251 size_t count, loff_t *ppos) 8252 { 8253 struct inode *inode = file_inode(filp); 8254 struct trace_array *tr = inode->i_private; 8255 struct array_buffer *trace_buf = &tr->array_buffer; 8256 int cpu = tracing_get_cpu(inode); 8257 struct trace_seq *s; 8258 unsigned long cnt; 8259 unsigned long long t; 8260 unsigned long usec_rem; 8261 8262 s = kmalloc(sizeof(*s), GFP_KERNEL); 8263 if (!s) 8264 return -ENOMEM; 8265 8266 trace_seq_init(s); 8267 8268 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu); 8269 trace_seq_printf(s, "entries: %ld\n", cnt); 8270 8271 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu); 8272 trace_seq_printf(s, "overrun: %ld\n", cnt); 8273 8274 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu); 8275 trace_seq_printf(s, "commit overrun: %ld\n", cnt); 8276 8277 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu); 8278 trace_seq_printf(s, "bytes: %ld\n", cnt); 8279 8280 if (trace_clocks[tr->clock_id].in_ns) { 8281 /* local or global for trace_clock */ 8282 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu)); 8283 usec_rem = do_div(t, USEC_PER_SEC); 8284 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n", 8285 t, usec_rem); 8286 8287 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer)); 8288 usec_rem = do_div(t, USEC_PER_SEC); 8289 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem); 8290 } else { 8291 /* counter or tsc mode for trace_clock */ 8292 trace_seq_printf(s, "oldest event ts: %llu\n", 8293 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu)); 8294 8295 trace_seq_printf(s, "now ts: %llu\n", 8296 ring_buffer_time_stamp(trace_buf->buffer)); 8297 } 8298 8299 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu); 8300 trace_seq_printf(s, "dropped events: %ld\n", cnt); 8301 8302 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu); 8303 trace_seq_printf(s, "read events: %ld\n", cnt); 8304 8305 count = simple_read_from_buffer(ubuf, count, ppos, 8306 s->buffer, trace_seq_used(s)); 8307 8308 kfree(s); 8309 8310 return count; 8311 } 8312 8313 static const struct file_operations tracing_stats_fops = { 8314 .open = tracing_open_generic_tr, 8315 .read = tracing_stats_read, 8316 .llseek = generic_file_llseek, 8317 .release = tracing_release_generic_tr, 8318 }; 8319 8320 #ifdef CONFIG_DYNAMIC_FTRACE 8321 8322 static ssize_t 8323 tracing_read_dyn_info(struct file *filp, char __user *ubuf, 8324 size_t cnt, loff_t *ppos) 8325 { 8326 ssize_t ret; 8327 char *buf; 8328 int r; 8329 8330 /* 256 should be plenty to hold the amount needed */ 8331 buf = kmalloc(256, GFP_KERNEL); 8332 if (!buf) 8333 return -ENOMEM; 8334 8335 r = scnprintf(buf, 256, "%ld pages:%ld groups: %ld\n", 8336 ftrace_update_tot_cnt, 8337 ftrace_number_of_pages, 8338 ftrace_number_of_groups); 8339 8340 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r); 8341 kfree(buf); 8342 return ret; 8343 } 8344 8345 static const struct file_operations tracing_dyn_info_fops = { 8346 .open = tracing_open_generic, 8347 .read = tracing_read_dyn_info, 8348 .llseek = generic_file_llseek, 8349 }; 8350 #endif /* CONFIG_DYNAMIC_FTRACE */ 8351 8352 #if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) 8353 static void 8354 ftrace_snapshot(unsigned long ip, unsigned long parent_ip, 8355 struct trace_array *tr, struct ftrace_probe_ops *ops, 8356 void *data) 8357 { 8358 tracing_snapshot_instance(tr); 8359 } 8360 8361 static void 8362 ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip, 8363 struct trace_array *tr, struct ftrace_probe_ops *ops, 8364 void *data) 8365 { 8366 struct ftrace_func_mapper *mapper = data; 8367 long *count = NULL; 8368 8369 if (mapper) 8370 count = (long *)ftrace_func_mapper_find_ip(mapper, ip); 8371 8372 if (count) { 8373 8374 if (*count <= 0) 8375 return; 8376 8377 (*count)--; 8378 } 8379 8380 tracing_snapshot_instance(tr); 8381 } 8382 8383 static int 8384 ftrace_snapshot_print(struct seq_file *m, unsigned long ip, 8385 struct ftrace_probe_ops *ops, void *data) 8386 { 8387 struct ftrace_func_mapper *mapper = data; 8388 long *count = NULL; 8389 8390 seq_printf(m, "%ps:", (void *)ip); 8391 8392 seq_puts(m, "snapshot"); 8393 8394 if (mapper) 8395 count = (long *)ftrace_func_mapper_find_ip(mapper, ip); 8396 8397 if (count) 8398 seq_printf(m, ":count=%ld\n", *count); 8399 else 8400 seq_puts(m, ":unlimited\n"); 8401 8402 return 0; 8403 } 8404 8405 static int 8406 ftrace_snapshot_init(struct ftrace_probe_ops *ops, struct trace_array *tr, 8407 unsigned long ip, void *init_data, void **data) 8408 { 8409 struct ftrace_func_mapper *mapper = *data; 8410 8411 if (!mapper) { 8412 mapper = allocate_ftrace_func_mapper(); 8413 if (!mapper) 8414 return -ENOMEM; 8415 *data = mapper; 8416 } 8417 8418 return ftrace_func_mapper_add_ip(mapper, ip, init_data); 8419 } 8420 8421 static void 8422 ftrace_snapshot_free(struct ftrace_probe_ops *ops, struct trace_array *tr, 8423 unsigned long ip, void *data) 8424 { 8425 struct ftrace_func_mapper *mapper = data; 8426 8427 if (!ip) { 8428 if (!mapper) 8429 return; 8430 free_ftrace_func_mapper(mapper, NULL); 8431 return; 8432 } 8433 8434 ftrace_func_mapper_remove_ip(mapper, ip); 8435 } 8436 8437 static struct ftrace_probe_ops snapshot_probe_ops = { 8438 .func = ftrace_snapshot, 8439 .print = ftrace_snapshot_print, 8440 }; 8441 8442 static struct ftrace_probe_ops snapshot_count_probe_ops = { 8443 .func = ftrace_count_snapshot, 8444 .print = ftrace_snapshot_print, 8445 .init = ftrace_snapshot_init, 8446 .free = ftrace_snapshot_free, 8447 }; 8448 8449 static int 8450 ftrace_trace_snapshot_callback(struct trace_array *tr, struct ftrace_hash *hash, 8451 char *glob, char *cmd, char *param, int enable) 8452 { 8453 struct ftrace_probe_ops *ops; 8454 void *count = (void *)-1; 8455 char *number; 8456 int ret; 8457 8458 if (!tr) 8459 return -ENODEV; 8460 8461 /* hash funcs only work with set_ftrace_filter */ 8462 if (!enable) 8463 return -EINVAL; 8464 8465 ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops; 8466 8467 if (glob[0] == '!') 8468 return unregister_ftrace_function_probe_func(glob+1, tr, ops); 8469 8470 if (!param) 8471 goto out_reg; 8472 8473 number = strsep(¶m, ":"); 8474 8475 if (!strlen(number)) 8476 goto out_reg; 8477 8478 /* 8479 * We use the callback data field (which is a pointer) 8480 * as our counter. 8481 */ 8482 ret = kstrtoul(number, 0, (unsigned long *)&count); 8483 if (ret) 8484 return ret; 8485 8486 out_reg: 8487 ret = tracing_alloc_snapshot_instance(tr); 8488 if (ret < 0) 8489 goto out; 8490 8491 ret = register_ftrace_function_probe(glob, tr, ops, count); 8492 8493 out: 8494 return ret < 0 ? ret : 0; 8495 } 8496 8497 static struct ftrace_func_command ftrace_snapshot_cmd = { 8498 .name = "snapshot", 8499 .func = ftrace_trace_snapshot_callback, 8500 }; 8501 8502 static __init int register_snapshot_cmd(void) 8503 { 8504 return register_ftrace_command(&ftrace_snapshot_cmd); 8505 } 8506 #else 8507 static inline __init int register_snapshot_cmd(void) { return 0; } 8508 #endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */ 8509 8510 static struct dentry *tracing_get_dentry(struct trace_array *tr) 8511 { 8512 if (WARN_ON(!tr->dir)) 8513 return ERR_PTR(-ENODEV); 8514 8515 /* Top directory uses NULL as the parent */ 8516 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) 8517 return NULL; 8518 8519 /* All sub buffers have a descriptor */ 8520 return tr->dir; 8521 } 8522 8523 static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu) 8524 { 8525 struct dentry *d_tracer; 8526 8527 if (tr->percpu_dir) 8528 return tr->percpu_dir; 8529 8530 d_tracer = tracing_get_dentry(tr); 8531 if (IS_ERR(d_tracer)) 8532 return NULL; 8533 8534 tr->percpu_dir = tracefs_create_dir("per_cpu", d_tracer); 8535 8536 MEM_FAIL(!tr->percpu_dir, 8537 "Could not create tracefs directory 'per_cpu/%d'\n", cpu); 8538 8539 return tr->percpu_dir; 8540 } 8541 8542 static struct dentry * 8543 trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent, 8544 void *data, long cpu, const struct file_operations *fops) 8545 { 8546 struct dentry *ret = trace_create_file(name, mode, parent, data, fops); 8547 8548 if (ret) /* See tracing_get_cpu() */ 8549 d_inode(ret)->i_cdev = (void *)(cpu + 1); 8550 return ret; 8551 } 8552 8553 static void 8554 tracing_init_tracefs_percpu(struct trace_array *tr, long cpu) 8555 { 8556 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu); 8557 struct dentry *d_cpu; 8558 char cpu_dir[30]; /* 30 characters should be more than enough */ 8559 8560 if (!d_percpu) 8561 return; 8562 8563 snprintf(cpu_dir, 30, "cpu%ld", cpu); 8564 d_cpu = tracefs_create_dir(cpu_dir, d_percpu); 8565 if (!d_cpu) { 8566 pr_warn("Could not create tracefs '%s' entry\n", cpu_dir); 8567 return; 8568 } 8569 8570 /* per cpu trace_pipe */ 8571 trace_create_cpu_file("trace_pipe", TRACE_MODE_READ, d_cpu, 8572 tr, cpu, &tracing_pipe_fops); 8573 8574 /* per cpu trace */ 8575 trace_create_cpu_file("trace", TRACE_MODE_WRITE, d_cpu, 8576 tr, cpu, &tracing_fops); 8577 8578 trace_create_cpu_file("trace_pipe_raw", TRACE_MODE_READ, d_cpu, 8579 tr, cpu, &tracing_buffers_fops); 8580 8581 trace_create_cpu_file("stats", TRACE_MODE_READ, d_cpu, 8582 tr, cpu, &tracing_stats_fops); 8583 8584 trace_create_cpu_file("buffer_size_kb", TRACE_MODE_READ, d_cpu, 8585 tr, cpu, &tracing_entries_fops); 8586 8587 #ifdef CONFIG_TRACER_SNAPSHOT 8588 trace_create_cpu_file("snapshot", TRACE_MODE_WRITE, d_cpu, 8589 tr, cpu, &snapshot_fops); 8590 8591 trace_create_cpu_file("snapshot_raw", TRACE_MODE_READ, d_cpu, 8592 tr, cpu, &snapshot_raw_fops); 8593 #endif 8594 } 8595 8596 #ifdef CONFIG_FTRACE_SELFTEST 8597 /* Let selftest have access to static functions in this file */ 8598 #include "trace_selftest.c" 8599 #endif 8600 8601 static ssize_t 8602 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt, 8603 loff_t *ppos) 8604 { 8605 struct trace_option_dentry *topt = filp->private_data; 8606 char *buf; 8607 8608 if (topt->flags->val & topt->opt->bit) 8609 buf = "1\n"; 8610 else 8611 buf = "0\n"; 8612 8613 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2); 8614 } 8615 8616 static ssize_t 8617 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt, 8618 loff_t *ppos) 8619 { 8620 struct trace_option_dentry *topt = filp->private_data; 8621 unsigned long val; 8622 int ret; 8623 8624 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 8625 if (ret) 8626 return ret; 8627 8628 if (val != 0 && val != 1) 8629 return -EINVAL; 8630 8631 if (!!(topt->flags->val & topt->opt->bit) != val) { 8632 mutex_lock(&trace_types_lock); 8633 ret = __set_tracer_option(topt->tr, topt->flags, 8634 topt->opt, !val); 8635 mutex_unlock(&trace_types_lock); 8636 if (ret) 8637 return ret; 8638 } 8639 8640 *ppos += cnt; 8641 8642 return cnt; 8643 } 8644 8645 8646 static const struct file_operations trace_options_fops = { 8647 .open = tracing_open_generic, 8648 .read = trace_options_read, 8649 .write = trace_options_write, 8650 .llseek = generic_file_llseek, 8651 }; 8652 8653 /* 8654 * In order to pass in both the trace_array descriptor as well as the index 8655 * to the flag that the trace option file represents, the trace_array 8656 * has a character array of trace_flags_index[], which holds the index 8657 * of the bit for the flag it represents. index[0] == 0, index[1] == 1, etc. 8658 * The address of this character array is passed to the flag option file 8659 * read/write callbacks. 8660 * 8661 * In order to extract both the index and the trace_array descriptor, 8662 * get_tr_index() uses the following algorithm. 8663 * 8664 * idx = *ptr; 8665 * 8666 * As the pointer itself contains the address of the index (remember 8667 * index[1] == 1). 8668 * 8669 * Then to get the trace_array descriptor, by subtracting that index 8670 * from the ptr, we get to the start of the index itself. 8671 * 8672 * ptr - idx == &index[0] 8673 * 8674 * Then a simple container_of() from that pointer gets us to the 8675 * trace_array descriptor. 8676 */ 8677 static void get_tr_index(void *data, struct trace_array **ptr, 8678 unsigned int *pindex) 8679 { 8680 *pindex = *(unsigned char *)data; 8681 8682 *ptr = container_of(data - *pindex, struct trace_array, 8683 trace_flags_index); 8684 } 8685 8686 static ssize_t 8687 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt, 8688 loff_t *ppos) 8689 { 8690 void *tr_index = filp->private_data; 8691 struct trace_array *tr; 8692 unsigned int index; 8693 char *buf; 8694 8695 get_tr_index(tr_index, &tr, &index); 8696 8697 if (tr->trace_flags & (1 << index)) 8698 buf = "1\n"; 8699 else 8700 buf = "0\n"; 8701 8702 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2); 8703 } 8704 8705 static ssize_t 8706 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt, 8707 loff_t *ppos) 8708 { 8709 void *tr_index = filp->private_data; 8710 struct trace_array *tr; 8711 unsigned int index; 8712 unsigned long val; 8713 int ret; 8714 8715 get_tr_index(tr_index, &tr, &index); 8716 8717 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 8718 if (ret) 8719 return ret; 8720 8721 if (val != 0 && val != 1) 8722 return -EINVAL; 8723 8724 mutex_lock(&event_mutex); 8725 mutex_lock(&trace_types_lock); 8726 ret = set_tracer_flag(tr, 1 << index, val); 8727 mutex_unlock(&trace_types_lock); 8728 mutex_unlock(&event_mutex); 8729 8730 if (ret < 0) 8731 return ret; 8732 8733 *ppos += cnt; 8734 8735 return cnt; 8736 } 8737 8738 static const struct file_operations trace_options_core_fops = { 8739 .open = tracing_open_generic, 8740 .read = trace_options_core_read, 8741 .write = trace_options_core_write, 8742 .llseek = generic_file_llseek, 8743 }; 8744 8745 struct dentry *trace_create_file(const char *name, 8746 umode_t mode, 8747 struct dentry *parent, 8748 void *data, 8749 const struct file_operations *fops) 8750 { 8751 struct dentry *ret; 8752 8753 ret = tracefs_create_file(name, mode, parent, data, fops); 8754 if (!ret) 8755 pr_warn("Could not create tracefs '%s' entry\n", name); 8756 8757 return ret; 8758 } 8759 8760 8761 static struct dentry *trace_options_init_dentry(struct trace_array *tr) 8762 { 8763 struct dentry *d_tracer; 8764 8765 if (tr->options) 8766 return tr->options; 8767 8768 d_tracer = tracing_get_dentry(tr); 8769 if (IS_ERR(d_tracer)) 8770 return NULL; 8771 8772 tr->options = tracefs_create_dir("options", d_tracer); 8773 if (!tr->options) { 8774 pr_warn("Could not create tracefs directory 'options'\n"); 8775 return NULL; 8776 } 8777 8778 return tr->options; 8779 } 8780 8781 static void 8782 create_trace_option_file(struct trace_array *tr, 8783 struct trace_option_dentry *topt, 8784 struct tracer_flags *flags, 8785 struct tracer_opt *opt) 8786 { 8787 struct dentry *t_options; 8788 8789 t_options = trace_options_init_dentry(tr); 8790 if (!t_options) 8791 return; 8792 8793 topt->flags = flags; 8794 topt->opt = opt; 8795 topt->tr = tr; 8796 8797 topt->entry = trace_create_file(opt->name, TRACE_MODE_WRITE, 8798 t_options, topt, &trace_options_fops); 8799 8800 } 8801 8802 static void 8803 create_trace_option_files(struct trace_array *tr, struct tracer *tracer) 8804 { 8805 struct trace_option_dentry *topts; 8806 struct trace_options *tr_topts; 8807 struct tracer_flags *flags; 8808 struct tracer_opt *opts; 8809 int cnt; 8810 int i; 8811 8812 if (!tracer) 8813 return; 8814 8815 flags = tracer->flags; 8816 8817 if (!flags || !flags->opts) 8818 return; 8819 8820 /* 8821 * If this is an instance, only create flags for tracers 8822 * the instance may have. 8823 */ 8824 if (!trace_ok_for_array(tracer, tr)) 8825 return; 8826 8827 for (i = 0; i < tr->nr_topts; i++) { 8828 /* Make sure there's no duplicate flags. */ 8829 if (WARN_ON_ONCE(tr->topts[i].tracer->flags == tracer->flags)) 8830 return; 8831 } 8832 8833 opts = flags->opts; 8834 8835 for (cnt = 0; opts[cnt].name; cnt++) 8836 ; 8837 8838 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL); 8839 if (!topts) 8840 return; 8841 8842 tr_topts = krealloc(tr->topts, sizeof(*tr->topts) * (tr->nr_topts + 1), 8843 GFP_KERNEL); 8844 if (!tr_topts) { 8845 kfree(topts); 8846 return; 8847 } 8848 8849 tr->topts = tr_topts; 8850 tr->topts[tr->nr_topts].tracer = tracer; 8851 tr->topts[tr->nr_topts].topts = topts; 8852 tr->nr_topts++; 8853 8854 for (cnt = 0; opts[cnt].name; cnt++) { 8855 create_trace_option_file(tr, &topts[cnt], flags, 8856 &opts[cnt]); 8857 MEM_FAIL(topts[cnt].entry == NULL, 8858 "Failed to create trace option: %s", 8859 opts[cnt].name); 8860 } 8861 } 8862 8863 static struct dentry * 8864 create_trace_option_core_file(struct trace_array *tr, 8865 const char *option, long index) 8866 { 8867 struct dentry *t_options; 8868 8869 t_options = trace_options_init_dentry(tr); 8870 if (!t_options) 8871 return NULL; 8872 8873 return trace_create_file(option, TRACE_MODE_WRITE, t_options, 8874 (void *)&tr->trace_flags_index[index], 8875 &trace_options_core_fops); 8876 } 8877 8878 static void create_trace_options_dir(struct trace_array *tr) 8879 { 8880 struct dentry *t_options; 8881 bool top_level = tr == &global_trace; 8882 int i; 8883 8884 t_options = trace_options_init_dentry(tr); 8885 if (!t_options) 8886 return; 8887 8888 for (i = 0; trace_options[i]; i++) { 8889 if (top_level || 8890 !((1 << i) & TOP_LEVEL_TRACE_FLAGS)) 8891 create_trace_option_core_file(tr, trace_options[i], i); 8892 } 8893 } 8894 8895 static ssize_t 8896 rb_simple_read(struct file *filp, char __user *ubuf, 8897 size_t cnt, loff_t *ppos) 8898 { 8899 struct trace_array *tr = filp->private_data; 8900 char buf[64]; 8901 int r; 8902 8903 r = tracer_tracing_is_on(tr); 8904 r = sprintf(buf, "%d\n", r); 8905 8906 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); 8907 } 8908 8909 static ssize_t 8910 rb_simple_write(struct file *filp, const char __user *ubuf, 8911 size_t cnt, loff_t *ppos) 8912 { 8913 struct trace_array *tr = filp->private_data; 8914 struct trace_buffer *buffer = tr->array_buffer.buffer; 8915 unsigned long val; 8916 int ret; 8917 8918 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 8919 if (ret) 8920 return ret; 8921 8922 if (buffer) { 8923 mutex_lock(&trace_types_lock); 8924 if (!!val == tracer_tracing_is_on(tr)) { 8925 val = 0; /* do nothing */ 8926 } else if (val) { 8927 tracer_tracing_on(tr); 8928 if (tr->current_trace->start) 8929 tr->current_trace->start(tr); 8930 } else { 8931 tracer_tracing_off(tr); 8932 if (tr->current_trace->stop) 8933 tr->current_trace->stop(tr); 8934 } 8935 mutex_unlock(&trace_types_lock); 8936 } 8937 8938 (*ppos)++; 8939 8940 return cnt; 8941 } 8942 8943 static const struct file_operations rb_simple_fops = { 8944 .open = tracing_open_generic_tr, 8945 .read = rb_simple_read, 8946 .write = rb_simple_write, 8947 .release = tracing_release_generic_tr, 8948 .llseek = default_llseek, 8949 }; 8950 8951 static ssize_t 8952 buffer_percent_read(struct file *filp, char __user *ubuf, 8953 size_t cnt, loff_t *ppos) 8954 { 8955 struct trace_array *tr = filp->private_data; 8956 char buf[64]; 8957 int r; 8958 8959 r = tr->buffer_percent; 8960 r = sprintf(buf, "%d\n", r); 8961 8962 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); 8963 } 8964 8965 static ssize_t 8966 buffer_percent_write(struct file *filp, const char __user *ubuf, 8967 size_t cnt, loff_t *ppos) 8968 { 8969 struct trace_array *tr = filp->private_data; 8970 unsigned long val; 8971 int ret; 8972 8973 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 8974 if (ret) 8975 return ret; 8976 8977 if (val > 100) 8978 return -EINVAL; 8979 8980 if (!val) 8981 val = 1; 8982 8983 tr->buffer_percent = val; 8984 8985 (*ppos)++; 8986 8987 return cnt; 8988 } 8989 8990 static const struct file_operations buffer_percent_fops = { 8991 .open = tracing_open_generic_tr, 8992 .read = buffer_percent_read, 8993 .write = buffer_percent_write, 8994 .release = tracing_release_generic_tr, 8995 .llseek = default_llseek, 8996 }; 8997 8998 static struct dentry *trace_instance_dir; 8999 9000 static void 9001 init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer); 9002 9003 static int 9004 allocate_trace_buffer(struct trace_array *tr, struct array_buffer *buf, int size) 9005 { 9006 enum ring_buffer_flags rb_flags; 9007 9008 rb_flags = tr->trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0; 9009 9010 buf->tr = tr; 9011 9012 buf->buffer = ring_buffer_alloc(size, rb_flags); 9013 if (!buf->buffer) 9014 return -ENOMEM; 9015 9016 buf->data = alloc_percpu(struct trace_array_cpu); 9017 if (!buf->data) { 9018 ring_buffer_free(buf->buffer); 9019 buf->buffer = NULL; 9020 return -ENOMEM; 9021 } 9022 9023 /* Allocate the first page for all buffers */ 9024 set_buffer_entries(&tr->array_buffer, 9025 ring_buffer_size(tr->array_buffer.buffer, 0)); 9026 9027 return 0; 9028 } 9029 9030 static int allocate_trace_buffers(struct trace_array *tr, int size) 9031 { 9032 int ret; 9033 9034 ret = allocate_trace_buffer(tr, &tr->array_buffer, size); 9035 if (ret) 9036 return ret; 9037 9038 #ifdef CONFIG_TRACER_MAX_TRACE 9039 ret = allocate_trace_buffer(tr, &tr->max_buffer, 9040 allocate_snapshot ? size : 1); 9041 if (MEM_FAIL(ret, "Failed to allocate trace buffer\n")) { 9042 ring_buffer_free(tr->array_buffer.buffer); 9043 tr->array_buffer.buffer = NULL; 9044 free_percpu(tr->array_buffer.data); 9045 tr->array_buffer.data = NULL; 9046 return -ENOMEM; 9047 } 9048 tr->allocated_snapshot = allocate_snapshot; 9049 9050 /* 9051 * Only the top level trace array gets its snapshot allocated 9052 * from the kernel command line. 9053 */ 9054 allocate_snapshot = false; 9055 #endif 9056 9057 return 0; 9058 } 9059 9060 static void free_trace_buffer(struct array_buffer *buf) 9061 { 9062 if (buf->buffer) { 9063 ring_buffer_free(buf->buffer); 9064 buf->buffer = NULL; 9065 free_percpu(buf->data); 9066 buf->data = NULL; 9067 } 9068 } 9069 9070 static void free_trace_buffers(struct trace_array *tr) 9071 { 9072 if (!tr) 9073 return; 9074 9075 free_trace_buffer(&tr->array_buffer); 9076 9077 #ifdef CONFIG_TRACER_MAX_TRACE 9078 free_trace_buffer(&tr->max_buffer); 9079 #endif 9080 } 9081 9082 static void init_trace_flags_index(struct trace_array *tr) 9083 { 9084 int i; 9085 9086 /* Used by the trace options files */ 9087 for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++) 9088 tr->trace_flags_index[i] = i; 9089 } 9090 9091 static void __update_tracer_options(struct trace_array *tr) 9092 { 9093 struct tracer *t; 9094 9095 for (t = trace_types; t; t = t->next) 9096 add_tracer_options(tr, t); 9097 } 9098 9099 static void update_tracer_options(struct trace_array *tr) 9100 { 9101 mutex_lock(&trace_types_lock); 9102 __update_tracer_options(tr); 9103 mutex_unlock(&trace_types_lock); 9104 } 9105 9106 /* Must have trace_types_lock held */ 9107 struct trace_array *trace_array_find(const char *instance) 9108 { 9109 struct trace_array *tr, *found = NULL; 9110 9111 list_for_each_entry(tr, &ftrace_trace_arrays, list) { 9112 if (tr->name && strcmp(tr->name, instance) == 0) { 9113 found = tr; 9114 break; 9115 } 9116 } 9117 9118 return found; 9119 } 9120 9121 struct trace_array *trace_array_find_get(const char *instance) 9122 { 9123 struct trace_array *tr; 9124 9125 mutex_lock(&trace_types_lock); 9126 tr = trace_array_find(instance); 9127 if (tr) 9128 tr->ref++; 9129 mutex_unlock(&trace_types_lock); 9130 9131 return tr; 9132 } 9133 9134 static int trace_array_create_dir(struct trace_array *tr) 9135 { 9136 int ret; 9137 9138 tr->dir = tracefs_create_dir(tr->name, trace_instance_dir); 9139 if (!tr->dir) 9140 return -EINVAL; 9141 9142 ret = event_trace_add_tracer(tr->dir, tr); 9143 if (ret) { 9144 tracefs_remove(tr->dir); 9145 return ret; 9146 } 9147 9148 init_tracer_tracefs(tr, tr->dir); 9149 __update_tracer_options(tr); 9150 9151 return ret; 9152 } 9153 9154 static struct trace_array *trace_array_create(const char *name) 9155 { 9156 struct trace_array *tr; 9157 int ret; 9158 9159 ret = -ENOMEM; 9160 tr = kzalloc(sizeof(*tr), GFP_KERNEL); 9161 if (!tr) 9162 return ERR_PTR(ret); 9163 9164 tr->name = kstrdup(name, GFP_KERNEL); 9165 if (!tr->name) 9166 goto out_free_tr; 9167 9168 if (!alloc_cpumask_var(&tr->tracing_cpumask, GFP_KERNEL)) 9169 goto out_free_tr; 9170 9171 tr->trace_flags = global_trace.trace_flags & ~ZEROED_TRACE_FLAGS; 9172 9173 cpumask_copy(tr->tracing_cpumask, cpu_all_mask); 9174 9175 raw_spin_lock_init(&tr->start_lock); 9176 9177 tr->max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; 9178 9179 tr->current_trace = &nop_trace; 9180 9181 INIT_LIST_HEAD(&tr->systems); 9182 INIT_LIST_HEAD(&tr->events); 9183 INIT_LIST_HEAD(&tr->hist_vars); 9184 INIT_LIST_HEAD(&tr->err_log); 9185 9186 if (allocate_trace_buffers(tr, trace_buf_size) < 0) 9187 goto out_free_tr; 9188 9189 if (ftrace_allocate_ftrace_ops(tr) < 0) 9190 goto out_free_tr; 9191 9192 ftrace_init_trace_array(tr); 9193 9194 init_trace_flags_index(tr); 9195 9196 if (trace_instance_dir) { 9197 ret = trace_array_create_dir(tr); 9198 if (ret) 9199 goto out_free_tr; 9200 } else 9201 __trace_early_add_events(tr); 9202 9203 list_add(&tr->list, &ftrace_trace_arrays); 9204 9205 tr->ref++; 9206 9207 return tr; 9208 9209 out_free_tr: 9210 ftrace_free_ftrace_ops(tr); 9211 free_trace_buffers(tr); 9212 free_cpumask_var(tr->tracing_cpumask); 9213 kfree(tr->name); 9214 kfree(tr); 9215 9216 return ERR_PTR(ret); 9217 } 9218 9219 static int instance_mkdir(const char *name) 9220 { 9221 struct trace_array *tr; 9222 int ret; 9223 9224 mutex_lock(&event_mutex); 9225 mutex_lock(&trace_types_lock); 9226 9227 ret = -EEXIST; 9228 if (trace_array_find(name)) 9229 goto out_unlock; 9230 9231 tr = trace_array_create(name); 9232 9233 ret = PTR_ERR_OR_ZERO(tr); 9234 9235 out_unlock: 9236 mutex_unlock(&trace_types_lock); 9237 mutex_unlock(&event_mutex); 9238 return ret; 9239 } 9240 9241 /** 9242 * trace_array_get_by_name - Create/Lookup a trace array, given its name. 9243 * @name: The name of the trace array to be looked up/created. 9244 * 9245 * Returns pointer to trace array with given name. 9246 * NULL, if it cannot be created. 9247 * 9248 * NOTE: This function increments the reference counter associated with the 9249 * trace array returned. This makes sure it cannot be freed while in use. 9250 * Use trace_array_put() once the trace array is no longer needed. 9251 * If the trace_array is to be freed, trace_array_destroy() needs to 9252 * be called after the trace_array_put(), or simply let user space delete 9253 * it from the tracefs instances directory. But until the 9254 * trace_array_put() is called, user space can not delete it. 9255 * 9256 */ 9257 struct trace_array *trace_array_get_by_name(const char *name) 9258 { 9259 struct trace_array *tr; 9260 9261 mutex_lock(&event_mutex); 9262 mutex_lock(&trace_types_lock); 9263 9264 list_for_each_entry(tr, &ftrace_trace_arrays, list) { 9265 if (tr->name && strcmp(tr->name, name) == 0) 9266 goto out_unlock; 9267 } 9268 9269 tr = trace_array_create(name); 9270 9271 if (IS_ERR(tr)) 9272 tr = NULL; 9273 out_unlock: 9274 if (tr) 9275 tr->ref++; 9276 9277 mutex_unlock(&trace_types_lock); 9278 mutex_unlock(&event_mutex); 9279 return tr; 9280 } 9281 EXPORT_SYMBOL_GPL(trace_array_get_by_name); 9282 9283 static int __remove_instance(struct trace_array *tr) 9284 { 9285 int i; 9286 9287 /* Reference counter for a newly created trace array = 1. */ 9288 if (tr->ref > 1 || (tr->current_trace && tr->trace_ref)) 9289 return -EBUSY; 9290 9291 list_del(&tr->list); 9292 9293 /* Disable all the flags that were enabled coming in */ 9294 for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++) { 9295 if ((1 << i) & ZEROED_TRACE_FLAGS) 9296 set_tracer_flag(tr, 1 << i, 0); 9297 } 9298 9299 tracing_set_nop(tr); 9300 clear_ftrace_function_probes(tr); 9301 event_trace_del_tracer(tr); 9302 ftrace_clear_pids(tr); 9303 ftrace_destroy_function_files(tr); 9304 tracefs_remove(tr->dir); 9305 free_percpu(tr->last_func_repeats); 9306 free_trace_buffers(tr); 9307 9308 for (i = 0; i < tr->nr_topts; i++) { 9309 kfree(tr->topts[i].topts); 9310 } 9311 kfree(tr->topts); 9312 9313 free_cpumask_var(tr->tracing_cpumask); 9314 kfree(tr->name); 9315 kfree(tr); 9316 9317 return 0; 9318 } 9319 9320 int trace_array_destroy(struct trace_array *this_tr) 9321 { 9322 struct trace_array *tr; 9323 int ret; 9324 9325 if (!this_tr) 9326 return -EINVAL; 9327 9328 mutex_lock(&event_mutex); 9329 mutex_lock(&trace_types_lock); 9330 9331 ret = -ENODEV; 9332 9333 /* Making sure trace array exists before destroying it. */ 9334 list_for_each_entry(tr, &ftrace_trace_arrays, list) { 9335 if (tr == this_tr) { 9336 ret = __remove_instance(tr); 9337 break; 9338 } 9339 } 9340 9341 mutex_unlock(&trace_types_lock); 9342 mutex_unlock(&event_mutex); 9343 9344 return ret; 9345 } 9346 EXPORT_SYMBOL_GPL(trace_array_destroy); 9347 9348 static int instance_rmdir(const char *name) 9349 { 9350 struct trace_array *tr; 9351 int ret; 9352 9353 mutex_lock(&event_mutex); 9354 mutex_lock(&trace_types_lock); 9355 9356 ret = -ENODEV; 9357 tr = trace_array_find(name); 9358 if (tr) 9359 ret = __remove_instance(tr); 9360 9361 mutex_unlock(&trace_types_lock); 9362 mutex_unlock(&event_mutex); 9363 9364 return ret; 9365 } 9366 9367 static __init void create_trace_instances(struct dentry *d_tracer) 9368 { 9369 struct trace_array *tr; 9370 9371 trace_instance_dir = tracefs_create_instance_dir("instances", d_tracer, 9372 instance_mkdir, 9373 instance_rmdir); 9374 if (MEM_FAIL(!trace_instance_dir, "Failed to create instances directory\n")) 9375 return; 9376 9377 mutex_lock(&event_mutex); 9378 mutex_lock(&trace_types_lock); 9379 9380 list_for_each_entry(tr, &ftrace_trace_arrays, list) { 9381 if (!tr->name) 9382 continue; 9383 if (MEM_FAIL(trace_array_create_dir(tr) < 0, 9384 "Failed to create instance directory\n")) 9385 break; 9386 } 9387 9388 mutex_unlock(&trace_types_lock); 9389 mutex_unlock(&event_mutex); 9390 } 9391 9392 static void 9393 init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer) 9394 { 9395 struct trace_event_file *file; 9396 int cpu; 9397 9398 trace_create_file("available_tracers", TRACE_MODE_READ, d_tracer, 9399 tr, &show_traces_fops); 9400 9401 trace_create_file("current_tracer", TRACE_MODE_WRITE, d_tracer, 9402 tr, &set_tracer_fops); 9403 9404 trace_create_file("tracing_cpumask", TRACE_MODE_WRITE, d_tracer, 9405 tr, &tracing_cpumask_fops); 9406 9407 trace_create_file("trace_options", TRACE_MODE_WRITE, d_tracer, 9408 tr, &tracing_iter_fops); 9409 9410 trace_create_file("trace", TRACE_MODE_WRITE, d_tracer, 9411 tr, &tracing_fops); 9412 9413 trace_create_file("trace_pipe", TRACE_MODE_READ, d_tracer, 9414 tr, &tracing_pipe_fops); 9415 9416 trace_create_file("buffer_size_kb", TRACE_MODE_WRITE, d_tracer, 9417 tr, &tracing_entries_fops); 9418 9419 trace_create_file("buffer_total_size_kb", TRACE_MODE_READ, d_tracer, 9420 tr, &tracing_total_entries_fops); 9421 9422 trace_create_file("free_buffer", 0200, d_tracer, 9423 tr, &tracing_free_buffer_fops); 9424 9425 trace_create_file("trace_marker", 0220, d_tracer, 9426 tr, &tracing_mark_fops); 9427 9428 file = __find_event_file(tr, "ftrace", "print"); 9429 if (file && file->dir) 9430 trace_create_file("trigger", TRACE_MODE_WRITE, file->dir, 9431 file, &event_trigger_fops); 9432 tr->trace_marker_file = file; 9433 9434 trace_create_file("trace_marker_raw", 0220, d_tracer, 9435 tr, &tracing_mark_raw_fops); 9436 9437 trace_create_file("trace_clock", TRACE_MODE_WRITE, d_tracer, tr, 9438 &trace_clock_fops); 9439 9440 trace_create_file("tracing_on", TRACE_MODE_WRITE, d_tracer, 9441 tr, &rb_simple_fops); 9442 9443 trace_create_file("timestamp_mode", TRACE_MODE_READ, d_tracer, tr, 9444 &trace_time_stamp_mode_fops); 9445 9446 tr->buffer_percent = 50; 9447 9448 trace_create_file("buffer_percent", TRACE_MODE_READ, d_tracer, 9449 tr, &buffer_percent_fops); 9450 9451 create_trace_options_dir(tr); 9452 9453 #if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER) 9454 trace_create_maxlat_file(tr, d_tracer); 9455 #endif 9456 9457 if (ftrace_create_function_files(tr, d_tracer)) 9458 MEM_FAIL(1, "Could not allocate function filter files"); 9459 9460 #ifdef CONFIG_TRACER_SNAPSHOT 9461 trace_create_file("snapshot", TRACE_MODE_WRITE, d_tracer, 9462 tr, &snapshot_fops); 9463 #endif 9464 9465 trace_create_file("error_log", TRACE_MODE_WRITE, d_tracer, 9466 tr, &tracing_err_log_fops); 9467 9468 for_each_tracing_cpu(cpu) 9469 tracing_init_tracefs_percpu(tr, cpu); 9470 9471 ftrace_init_tracefs(tr, d_tracer); 9472 } 9473 9474 static struct vfsmount *trace_automount(struct dentry *mntpt, void *ingore) 9475 { 9476 struct vfsmount *mnt; 9477 struct file_system_type *type; 9478 9479 /* 9480 * To maintain backward compatibility for tools that mount 9481 * debugfs to get to the tracing facility, tracefs is automatically 9482 * mounted to the debugfs/tracing directory. 9483 */ 9484 type = get_fs_type("tracefs"); 9485 if (!type) 9486 return NULL; 9487 mnt = vfs_submount(mntpt, type, "tracefs", NULL); 9488 put_filesystem(type); 9489 if (IS_ERR(mnt)) 9490 return NULL; 9491 mntget(mnt); 9492 9493 return mnt; 9494 } 9495 9496 /** 9497 * tracing_init_dentry - initialize top level trace array 9498 * 9499 * This is called when creating files or directories in the tracing 9500 * directory. It is called via fs_initcall() by any of the boot up code 9501 * and expects to return the dentry of the top level tracing directory. 9502 */ 9503 int tracing_init_dentry(void) 9504 { 9505 struct trace_array *tr = &global_trace; 9506 9507 if (security_locked_down(LOCKDOWN_TRACEFS)) { 9508 pr_warn("Tracing disabled due to lockdown\n"); 9509 return -EPERM; 9510 } 9511 9512 /* The top level trace array uses NULL as parent */ 9513 if (tr->dir) 9514 return 0; 9515 9516 if (WARN_ON(!tracefs_initialized())) 9517 return -ENODEV; 9518 9519 /* 9520 * As there may still be users that expect the tracing 9521 * files to exist in debugfs/tracing, we must automount 9522 * the tracefs file system there, so older tools still 9523 * work with the newer kernel. 9524 */ 9525 tr->dir = debugfs_create_automount("tracing", NULL, 9526 trace_automount, NULL); 9527 9528 return 0; 9529 } 9530 9531 extern struct trace_eval_map *__start_ftrace_eval_maps[]; 9532 extern struct trace_eval_map *__stop_ftrace_eval_maps[]; 9533 9534 static struct workqueue_struct *eval_map_wq __initdata; 9535 static struct work_struct eval_map_work __initdata; 9536 9537 static void __init eval_map_work_func(struct work_struct *work) 9538 { 9539 int len; 9540 9541 len = __stop_ftrace_eval_maps - __start_ftrace_eval_maps; 9542 trace_insert_eval_map(NULL, __start_ftrace_eval_maps, len); 9543 } 9544 9545 static int __init trace_eval_init(void) 9546 { 9547 INIT_WORK(&eval_map_work, eval_map_work_func); 9548 9549 eval_map_wq = alloc_workqueue("eval_map_wq", WQ_UNBOUND, 0); 9550 if (!eval_map_wq) { 9551 pr_err("Unable to allocate eval_map_wq\n"); 9552 /* Do work here */ 9553 eval_map_work_func(&eval_map_work); 9554 return -ENOMEM; 9555 } 9556 9557 queue_work(eval_map_wq, &eval_map_work); 9558 return 0; 9559 } 9560 9561 static int __init trace_eval_sync(void) 9562 { 9563 /* Make sure the eval map updates are finished */ 9564 if (eval_map_wq) 9565 destroy_workqueue(eval_map_wq); 9566 return 0; 9567 } 9568 9569 late_initcall_sync(trace_eval_sync); 9570 9571 9572 #ifdef CONFIG_MODULES 9573 static void trace_module_add_evals(struct module *mod) 9574 { 9575 if (!mod->num_trace_evals) 9576 return; 9577 9578 /* 9579 * Modules with bad taint do not have events created, do 9580 * not bother with enums either. 9581 */ 9582 if (trace_module_has_bad_taint(mod)) 9583 return; 9584 9585 trace_insert_eval_map(mod, mod->trace_evals, mod->num_trace_evals); 9586 } 9587 9588 #ifdef CONFIG_TRACE_EVAL_MAP_FILE 9589 static void trace_module_remove_evals(struct module *mod) 9590 { 9591 union trace_eval_map_item *map; 9592 union trace_eval_map_item **last = &trace_eval_maps; 9593 9594 if (!mod->num_trace_evals) 9595 return; 9596 9597 mutex_lock(&trace_eval_mutex); 9598 9599 map = trace_eval_maps; 9600 9601 while (map) { 9602 if (map->head.mod == mod) 9603 break; 9604 map = trace_eval_jmp_to_tail(map); 9605 last = &map->tail.next; 9606 map = map->tail.next; 9607 } 9608 if (!map) 9609 goto out; 9610 9611 *last = trace_eval_jmp_to_tail(map)->tail.next; 9612 kfree(map); 9613 out: 9614 mutex_unlock(&trace_eval_mutex); 9615 } 9616 #else 9617 static inline void trace_module_remove_evals(struct module *mod) { } 9618 #endif /* CONFIG_TRACE_EVAL_MAP_FILE */ 9619 9620 static int trace_module_notify(struct notifier_block *self, 9621 unsigned long val, void *data) 9622 { 9623 struct module *mod = data; 9624 9625 switch (val) { 9626 case MODULE_STATE_COMING: 9627 trace_module_add_evals(mod); 9628 break; 9629 case MODULE_STATE_GOING: 9630 trace_module_remove_evals(mod); 9631 break; 9632 } 9633 9634 return NOTIFY_OK; 9635 } 9636 9637 static struct notifier_block trace_module_nb = { 9638 .notifier_call = trace_module_notify, 9639 .priority = 0, 9640 }; 9641 #endif /* CONFIG_MODULES */ 9642 9643 static __init int tracer_init_tracefs(void) 9644 { 9645 int ret; 9646 9647 trace_access_lock_init(); 9648 9649 ret = tracing_init_dentry(); 9650 if (ret) 9651 return 0; 9652 9653 event_trace_init(); 9654 9655 init_tracer_tracefs(&global_trace, NULL); 9656 ftrace_init_tracefs_toplevel(&global_trace, NULL); 9657 9658 trace_create_file("tracing_thresh", TRACE_MODE_WRITE, NULL, 9659 &global_trace, &tracing_thresh_fops); 9660 9661 trace_create_file("README", TRACE_MODE_READ, NULL, 9662 NULL, &tracing_readme_fops); 9663 9664 trace_create_file("saved_cmdlines", TRACE_MODE_READ, NULL, 9665 NULL, &tracing_saved_cmdlines_fops); 9666 9667 trace_create_file("saved_cmdlines_size", TRACE_MODE_WRITE, NULL, 9668 NULL, &tracing_saved_cmdlines_size_fops); 9669 9670 trace_create_file("saved_tgids", TRACE_MODE_READ, NULL, 9671 NULL, &tracing_saved_tgids_fops); 9672 9673 trace_eval_init(); 9674 9675 trace_create_eval_file(NULL); 9676 9677 #ifdef CONFIG_MODULES 9678 register_module_notifier(&trace_module_nb); 9679 #endif 9680 9681 #ifdef CONFIG_DYNAMIC_FTRACE 9682 trace_create_file("dyn_ftrace_total_info", TRACE_MODE_READ, NULL, 9683 NULL, &tracing_dyn_info_fops); 9684 #endif 9685 9686 create_trace_instances(NULL); 9687 9688 update_tracer_options(&global_trace); 9689 9690 return 0; 9691 } 9692 9693 fs_initcall(tracer_init_tracefs); 9694 9695 static int trace_panic_handler(struct notifier_block *this, 9696 unsigned long event, void *unused) 9697 { 9698 if (ftrace_dump_on_oops) 9699 ftrace_dump(ftrace_dump_on_oops); 9700 return NOTIFY_OK; 9701 } 9702 9703 static struct notifier_block trace_panic_notifier = { 9704 .notifier_call = trace_panic_handler, 9705 .next = NULL, 9706 .priority = 150 /* priority: INT_MAX >= x >= 0 */ 9707 }; 9708 9709 static int trace_die_handler(struct notifier_block *self, 9710 unsigned long val, 9711 void *data) 9712 { 9713 switch (val) { 9714 case DIE_OOPS: 9715 if (ftrace_dump_on_oops) 9716 ftrace_dump(ftrace_dump_on_oops); 9717 break; 9718 default: 9719 break; 9720 } 9721 return NOTIFY_OK; 9722 } 9723 9724 static struct notifier_block trace_die_notifier = { 9725 .notifier_call = trace_die_handler, 9726 .priority = 200 9727 }; 9728 9729 /* 9730 * printk is set to max of 1024, we really don't need it that big. 9731 * Nothing should be printing 1000 characters anyway. 9732 */ 9733 #define TRACE_MAX_PRINT 1000 9734 9735 /* 9736 * Define here KERN_TRACE so that we have one place to modify 9737 * it if we decide to change what log level the ftrace dump 9738 * should be at. 9739 */ 9740 #define KERN_TRACE KERN_EMERG 9741 9742 void 9743 trace_printk_seq(struct trace_seq *s) 9744 { 9745 /* Probably should print a warning here. */ 9746 if (s->seq.len >= TRACE_MAX_PRINT) 9747 s->seq.len = TRACE_MAX_PRINT; 9748 9749 /* 9750 * More paranoid code. Although the buffer size is set to 9751 * PAGE_SIZE, and TRACE_MAX_PRINT is 1000, this is just 9752 * an extra layer of protection. 9753 */ 9754 if (WARN_ON_ONCE(s->seq.len >= s->seq.size)) 9755 s->seq.len = s->seq.size - 1; 9756 9757 /* should be zero ended, but we are paranoid. */ 9758 s->buffer[s->seq.len] = 0; 9759 9760 printk(KERN_TRACE "%s", s->buffer); 9761 9762 trace_seq_init(s); 9763 } 9764 9765 void trace_init_global_iter(struct trace_iterator *iter) 9766 { 9767 iter->tr = &global_trace; 9768 iter->trace = iter->tr->current_trace; 9769 iter->cpu_file = RING_BUFFER_ALL_CPUS; 9770 iter->array_buffer = &global_trace.array_buffer; 9771 9772 if (iter->trace && iter->trace->open) 9773 iter->trace->open(iter); 9774 9775 /* Annotate start of buffers if we had overruns */ 9776 if (ring_buffer_overruns(iter->array_buffer->buffer)) 9777 iter->iter_flags |= TRACE_FILE_ANNOTATE; 9778 9779 /* Output in nanoseconds only if we are using a clock in nanoseconds. */ 9780 if (trace_clocks[iter->tr->clock_id].in_ns) 9781 iter->iter_flags |= TRACE_FILE_TIME_IN_NS; 9782 } 9783 9784 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) 9785 { 9786 /* use static because iter can be a bit big for the stack */ 9787 static struct trace_iterator iter; 9788 static atomic_t dump_running; 9789 struct trace_array *tr = &global_trace; 9790 unsigned int old_userobj; 9791 unsigned long flags; 9792 int cnt = 0, cpu; 9793 9794 /* Only allow one dump user at a time. */ 9795 if (atomic_inc_return(&dump_running) != 1) { 9796 atomic_dec(&dump_running); 9797 return; 9798 } 9799 9800 /* 9801 * Always turn off tracing when we dump. 9802 * We don't need to show trace output of what happens 9803 * between multiple crashes. 9804 * 9805 * If the user does a sysrq-z, then they can re-enable 9806 * tracing with echo 1 > tracing_on. 9807 */ 9808 tracing_off(); 9809 9810 local_irq_save(flags); 9811 9812 /* Simulate the iterator */ 9813 trace_init_global_iter(&iter); 9814 /* Can not use kmalloc for iter.temp and iter.fmt */ 9815 iter.temp = static_temp_buf; 9816 iter.temp_size = STATIC_TEMP_BUF_SIZE; 9817 iter.fmt = static_fmt_buf; 9818 iter.fmt_size = STATIC_FMT_BUF_SIZE; 9819 9820 for_each_tracing_cpu(cpu) { 9821 atomic_inc(&per_cpu_ptr(iter.array_buffer->data, cpu)->disabled); 9822 } 9823 9824 old_userobj = tr->trace_flags & TRACE_ITER_SYM_USEROBJ; 9825 9826 /* don't look at user memory in panic mode */ 9827 tr->trace_flags &= ~TRACE_ITER_SYM_USEROBJ; 9828 9829 switch (oops_dump_mode) { 9830 case DUMP_ALL: 9831 iter.cpu_file = RING_BUFFER_ALL_CPUS; 9832 break; 9833 case DUMP_ORIG: 9834 iter.cpu_file = raw_smp_processor_id(); 9835 break; 9836 case DUMP_NONE: 9837 goto out_enable; 9838 default: 9839 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n"); 9840 iter.cpu_file = RING_BUFFER_ALL_CPUS; 9841 } 9842 9843 printk(KERN_TRACE "Dumping ftrace buffer:\n"); 9844 9845 /* Did function tracer already get disabled? */ 9846 if (ftrace_is_dead()) { 9847 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n"); 9848 printk("# MAY BE MISSING FUNCTION EVENTS\n"); 9849 } 9850 9851 /* 9852 * We need to stop all tracing on all CPUS to read 9853 * the next buffer. This is a bit expensive, but is 9854 * not done often. We fill all what we can read, 9855 * and then release the locks again. 9856 */ 9857 9858 while (!trace_empty(&iter)) { 9859 9860 if (!cnt) 9861 printk(KERN_TRACE "---------------------------------\n"); 9862 9863 cnt++; 9864 9865 trace_iterator_reset(&iter); 9866 iter.iter_flags |= TRACE_FILE_LAT_FMT; 9867 9868 if (trace_find_next_entry_inc(&iter) != NULL) { 9869 int ret; 9870 9871 ret = print_trace_line(&iter); 9872 if (ret != TRACE_TYPE_NO_CONSUME) 9873 trace_consume(&iter); 9874 } 9875 touch_nmi_watchdog(); 9876 9877 trace_printk_seq(&iter.seq); 9878 } 9879 9880 if (!cnt) 9881 printk(KERN_TRACE " (ftrace buffer empty)\n"); 9882 else 9883 printk(KERN_TRACE "---------------------------------\n"); 9884 9885 out_enable: 9886 tr->trace_flags |= old_userobj; 9887 9888 for_each_tracing_cpu(cpu) { 9889 atomic_dec(&per_cpu_ptr(iter.array_buffer->data, cpu)->disabled); 9890 } 9891 atomic_dec(&dump_running); 9892 local_irq_restore(flags); 9893 } 9894 EXPORT_SYMBOL_GPL(ftrace_dump); 9895 9896 #define WRITE_BUFSIZE 4096 9897 9898 ssize_t trace_parse_run_command(struct file *file, const char __user *buffer, 9899 size_t count, loff_t *ppos, 9900 int (*createfn)(const char *)) 9901 { 9902 char *kbuf, *buf, *tmp; 9903 int ret = 0; 9904 size_t done = 0; 9905 size_t size; 9906 9907 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL); 9908 if (!kbuf) 9909 return -ENOMEM; 9910 9911 while (done < count) { 9912 size = count - done; 9913 9914 if (size >= WRITE_BUFSIZE) 9915 size = WRITE_BUFSIZE - 1; 9916 9917 if (copy_from_user(kbuf, buffer + done, size)) { 9918 ret = -EFAULT; 9919 goto out; 9920 } 9921 kbuf[size] = '\0'; 9922 buf = kbuf; 9923 do { 9924 tmp = strchr(buf, '\n'); 9925 if (tmp) { 9926 *tmp = '\0'; 9927 size = tmp - buf + 1; 9928 } else { 9929 size = strlen(buf); 9930 if (done + size < count) { 9931 if (buf != kbuf) 9932 break; 9933 /* This can accept WRITE_BUFSIZE - 2 ('\n' + '\0') */ 9934 pr_warn("Line length is too long: Should be less than %d\n", 9935 WRITE_BUFSIZE - 2); 9936 ret = -EINVAL; 9937 goto out; 9938 } 9939 } 9940 done += size; 9941 9942 /* Remove comments */ 9943 tmp = strchr(buf, '#'); 9944 9945 if (tmp) 9946 *tmp = '\0'; 9947 9948 ret = createfn(buf); 9949 if (ret) 9950 goto out; 9951 buf += size; 9952 9953 } while (done < count); 9954 } 9955 ret = done; 9956 9957 out: 9958 kfree(kbuf); 9959 9960 return ret; 9961 } 9962 9963 __init static int tracer_alloc_buffers(void) 9964 { 9965 int ring_buf_size; 9966 int ret = -ENOMEM; 9967 9968 9969 if (security_locked_down(LOCKDOWN_TRACEFS)) { 9970 pr_warn("Tracing disabled due to lockdown\n"); 9971 return -EPERM; 9972 } 9973 9974 /* 9975 * Make sure we don't accidentally add more trace options 9976 * than we have bits for. 9977 */ 9978 BUILD_BUG_ON(TRACE_ITER_LAST_BIT > TRACE_FLAGS_MAX_SIZE); 9979 9980 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL)) 9981 goto out; 9982 9983 if (!alloc_cpumask_var(&global_trace.tracing_cpumask, GFP_KERNEL)) 9984 goto out_free_buffer_mask; 9985 9986 /* Only allocate trace_printk buffers if a trace_printk exists */ 9987 if (&__stop___trace_bprintk_fmt != &__start___trace_bprintk_fmt) 9988 /* Must be called before global_trace.buffer is allocated */ 9989 trace_printk_init_buffers(); 9990 9991 /* To save memory, keep the ring buffer size to its minimum */ 9992 if (ring_buffer_expanded) 9993 ring_buf_size = trace_buf_size; 9994 else 9995 ring_buf_size = 1; 9996 9997 cpumask_copy(tracing_buffer_mask, cpu_possible_mask); 9998 cpumask_copy(global_trace.tracing_cpumask, cpu_all_mask); 9999 10000 raw_spin_lock_init(&global_trace.start_lock); 10001 10002 /* 10003 * The prepare callbacks allocates some memory for the ring buffer. We 10004 * don't free the buffer if the CPU goes down. If we were to free 10005 * the buffer, then the user would lose any trace that was in the 10006 * buffer. The memory will be removed once the "instance" is removed. 10007 */ 10008 ret = cpuhp_setup_state_multi(CPUHP_TRACE_RB_PREPARE, 10009 "trace/RB:preapre", trace_rb_cpu_prepare, 10010 NULL); 10011 if (ret < 0) 10012 goto out_free_cpumask; 10013 /* Used for event triggers */ 10014 ret = -ENOMEM; 10015 temp_buffer = ring_buffer_alloc(PAGE_SIZE, RB_FL_OVERWRITE); 10016 if (!temp_buffer) 10017 goto out_rm_hp_state; 10018 10019 if (trace_create_savedcmd() < 0) 10020 goto out_free_temp_buffer; 10021 10022 /* TODO: make the number of buffers hot pluggable with CPUS */ 10023 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) { 10024 MEM_FAIL(1, "tracer: failed to allocate ring buffer!\n"); 10025 goto out_free_savedcmd; 10026 } 10027 10028 if (global_trace.buffer_disabled) 10029 tracing_off(); 10030 10031 if (trace_boot_clock) { 10032 ret = tracing_set_clock(&global_trace, trace_boot_clock); 10033 if (ret < 0) 10034 pr_warn("Trace clock %s not defined, going back to default\n", 10035 trace_boot_clock); 10036 } 10037 10038 /* 10039 * register_tracer() might reference current_trace, so it 10040 * needs to be set before we register anything. This is 10041 * just a bootstrap of current_trace anyway. 10042 */ 10043 global_trace.current_trace = &nop_trace; 10044 10045 global_trace.max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; 10046 10047 ftrace_init_global_array_ops(&global_trace); 10048 10049 init_trace_flags_index(&global_trace); 10050 10051 register_tracer(&nop_trace); 10052 10053 /* Function tracing may start here (via kernel command line) */ 10054 init_function_trace(); 10055 10056 /* All seems OK, enable tracing */ 10057 tracing_disabled = 0; 10058 10059 atomic_notifier_chain_register(&panic_notifier_list, 10060 &trace_panic_notifier); 10061 10062 register_die_notifier(&trace_die_notifier); 10063 10064 global_trace.flags = TRACE_ARRAY_FL_GLOBAL; 10065 10066 INIT_LIST_HEAD(&global_trace.systems); 10067 INIT_LIST_HEAD(&global_trace.events); 10068 INIT_LIST_HEAD(&global_trace.hist_vars); 10069 INIT_LIST_HEAD(&global_trace.err_log); 10070 list_add(&global_trace.list, &ftrace_trace_arrays); 10071 10072 apply_trace_boot_options(); 10073 10074 register_snapshot_cmd(); 10075 10076 test_can_verify(); 10077 10078 return 0; 10079 10080 out_free_savedcmd: 10081 free_saved_cmdlines_buffer(savedcmd); 10082 out_free_temp_buffer: 10083 ring_buffer_free(temp_buffer); 10084 out_rm_hp_state: 10085 cpuhp_remove_multi_state(CPUHP_TRACE_RB_PREPARE); 10086 out_free_cpumask: 10087 free_cpumask_var(global_trace.tracing_cpumask); 10088 out_free_buffer_mask: 10089 free_cpumask_var(tracing_buffer_mask); 10090 out: 10091 return ret; 10092 } 10093 10094 void __init early_trace_init(void) 10095 { 10096 if (tracepoint_printk) { 10097 tracepoint_print_iter = 10098 kzalloc(sizeof(*tracepoint_print_iter), GFP_KERNEL); 10099 if (MEM_FAIL(!tracepoint_print_iter, 10100 "Failed to allocate trace iterator\n")) 10101 tracepoint_printk = 0; 10102 else 10103 static_key_enable(&tracepoint_printk_key.key); 10104 } 10105 tracer_alloc_buffers(); 10106 } 10107 10108 void __init trace_init(void) 10109 { 10110 trace_event_init(); 10111 } 10112 10113 __init static void clear_boot_tracer(void) 10114 { 10115 /* 10116 * The default tracer at boot buffer is an init section. 10117 * This function is called in lateinit. If we did not 10118 * find the boot tracer, then clear it out, to prevent 10119 * later registration from accessing the buffer that is 10120 * about to be freed. 10121 */ 10122 if (!default_bootup_tracer) 10123 return; 10124 10125 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n", 10126 default_bootup_tracer); 10127 default_bootup_tracer = NULL; 10128 } 10129 10130 #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK 10131 __init static void tracing_set_default_clock(void) 10132 { 10133 /* sched_clock_stable() is determined in late_initcall */ 10134 if (!trace_boot_clock && !sched_clock_stable()) { 10135 if (security_locked_down(LOCKDOWN_TRACEFS)) { 10136 pr_warn("Can not set tracing clock due to lockdown\n"); 10137 return; 10138 } 10139 10140 printk(KERN_WARNING 10141 "Unstable clock detected, switching default tracing clock to \"global\"\n" 10142 "If you want to keep using the local clock, then add:\n" 10143 " \"trace_clock=local\"\n" 10144 "on the kernel command line\n"); 10145 tracing_set_clock(&global_trace, "global"); 10146 } 10147 } 10148 #else 10149 static inline void tracing_set_default_clock(void) { } 10150 #endif 10151 10152 __init static int late_trace_init(void) 10153 { 10154 if (tracepoint_printk && tracepoint_printk_stop_on_boot) { 10155 static_key_disable(&tracepoint_printk_key.key); 10156 tracepoint_printk = 0; 10157 } 10158 10159 tracing_set_default_clock(); 10160 clear_boot_tracer(); 10161 return 0; 10162 } 10163 10164 late_initcall_sync(late_trace_init); 10165