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