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