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